Skip to contentSkip to conversation

powerpc: mm: Replace p{u,m,4}d_is_leaf with p{u,m,4}_leaf

PATCH Superseded Rohan McLure 2 participants 6 messages Nov 6, 2022 – Nov 7, 2022

Subsystems (2)

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#1 lore raw

PATCH 2/4 powerpc: mm: Add common pud_pfn stub for all platforms

In reply to Rohan McLure

Prior to this commit, pud_pfn was implemented with BUILD_BUG as the inline function for 64-bit Book3S systems but is never included, as its invocations in generic code are guarded by calls to pud_devmap which return zero on such systems. A future patch will provide support for page table checks, the generic code for which depends on a pud_pfn stub being implemented, even while the patch will not interact with puds directly.

Remove the 64-bit Book3S stub and define pud_pfn to warn on all platforms. pud_pfn may be defined properly on a per-platform basis should it grow real usages in future.

---
V2: Remove conditional BUILD_BUG and BUG. Instead warn on usage.
V3: Replace WARN with WARN_ONCE, which should suffice to demonstrate misuse of puds.

---
 arch/powerpc/include/asm/book3s/64/pgtable.h | 10 ----------
 arch/powerpc/include/asm/pgtable.h           | 14 ++++++++++++++
 2 files changed, 14 insertions(+), 10 deletions(-)
arch/powerpc/include/asm/book3s/64/pgtable.h +0 -10
index 3f51de24e4fc..788a9effb03e 100644
@@ -1394,16 +1394,6 @@ static inline int pgd_devmap(pgd_t pgd)
 }
 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
 
-static inline int pud_pfn(pud_t pud)
-{
-	/*
-	 * Currently all calls to pud_pfn() are gated around a pud_devmap()
-	 * check so this should never be used. If it grows another user we
-	 * want to know about it.
-	 */
-	BUILD_BUG();
-	return 0;
-}
 #define __HAVE_ARCH_PTEP_MODIFY_PROT_TRANSACTION
 pte_t ptep_modify_prot_start(struct vm_area_struct *, unsigned long, pte_t *);
 void ptep_modify_prot_commit(struct vm_area_struct *, unsigned long,
arch/powerpc/include/asm/pgtable.h +14 -0
index 8e7625a89922..c9ce5019f8a8 100644
@@ -177,6 +177,20 @@ struct seq_file;
 void arch_report_meminfo(struct seq_file *m);
 #endif /* CONFIG_PPC64 */
 
+/*
+ * Currently only consumed by page_table_check_pud_{set,clear}. Since clears
+ * and sets to page table entries at any level are done through
+ * page_table_check_pte_{set,clear}, provide stub implementation.
+ */
+#ifndef pud_pfn
+#define pud_pfn pud_pfn
+static inline int pud_pfn(pud_t pud)
+{
+	WARN_ONCE(1, "pud: platform does not use pud entries directly");
+	return 0;
+}
+#endif
+
 #endif /* __ASSEMBLY__ */
 
 #endif /* _ASM_POWERPC_PGTABLE_H */
Signature · 1 line
2.34.1
#0 lore raw

PATCH 1/4 powerpc: mm: Replace p{u,m,4}d_is_leaf with p{u,m,4}_leaf

Replace occurrences of p{u,m,4}d_is_leaf with p{u,m,4}_leaf, as the latter is the name given to checking that a higher-level entry in multi-level paging contains a page translation entry (pte). This commit allows for p{u,m,4}d_leaf to be used on all powerpc platforms.

Prior to this commit, the two names have both been present in the kernel, having as far as I can tell the same exact purpose. While the 'is' in the title may better indicate that the macro/function is a boolean returning check, the former naming scheme is standard through all other architectures.

32-bit systems import pgtable-nop4d.h which defines a default pud_leaf. Define pud_leaf preprocessor macro on both Book3E/S 32-bit to avoid including the default definition in asm/pgtable.h.

---
V4: new patch.

---
 arch/powerpc/include/asm/book3s/32/pgtable.h |  2 +-
 arch/powerpc/include/asm/book3s/64/pgtable.h | 10 ++++------
 arch/powerpc/include/asm/nohash/32/pgtable.h |  1 +
 arch/powerpc/include/asm/pgtable.h           | 18 +++++++++---------
 arch/powerpc/kvm/book3s_64_mmu_radix.c       | 12 ++++++------
 arch/powerpc/mm/book3s64/radix_pgtable.c     | 14 +++++++-------
 arch/powerpc/mm/pgtable.c                    |  6 +++---
 arch/powerpc/mm/pgtable_64.c                 |  6 +++---
 arch/powerpc/xmon/xmon.c                     |  6 +++---
 9 files changed, 37 insertions(+), 38 deletions(-)
arch/powerpc/include/asm/book3s/32/pgtable.h +1 -1
index 75823f39e042..f1b91ad8f3a5 100644
@@ -234,6 +234,7 @@ void unmap_kernel_page(unsigned long va);
 #define pte_clear(mm, addr, ptep) \
 	do { pte_update(mm, addr, ptep, ~_PAGE_HASHPTE, 0, 0); } while (0)
 
+#define pud_leaf		pud_leaf
 #define pmd_none(pmd)		(!pmd_val(pmd))
 #define	pmd_bad(pmd)		(pmd_val(pmd) & _PMD_BAD)
 #define	pmd_present(pmd)	(pmd_val(pmd) & _PMD_PRESENT_MASK)
@@ -242,7 +243,6 @@ static inline void pmd_clear(pmd_t *pmdp)
 	*pmdp = __pmd(0);
 }
 
-
 /*
  * When flushing the tlb entry for a page, we also need to flush the hash
  * table entry.  flush_hash_pages is assembler (for speed) in hashtable.S.
arch/powerpc/include/asm/book3s/64/pgtable.h +4 -6
index c436d8422654..3f51de24e4fc 100644
@@ -1426,16 +1426,14 @@ static inline bool is_pte_rw_upgrade(unsigned long old_val, unsigned long new_va
 /*
  * Like pmd_huge() and pmd_large(), but works regardless of config options
  */
-#define pmd_is_leaf pmd_is_leaf
-#define pmd_leaf pmd_is_leaf
-static inline bool pmd_is_leaf(pmd_t pmd)
+#define pmd_leaf pmd_leaf
+static inline bool pmd_leaf(pmd_t pmd)
 {
 	return !!(pmd_raw(pmd) & cpu_to_be64(_PAGE_PTE));
 }
 
-#define pud_is_leaf pud_is_leaf
-#define pud_leaf pud_is_leaf
-static inline bool pud_is_leaf(pud_t pud)
+#define pud_leaf pud_leaf
+static inline bool pud_leaf(pud_t pud)
 {
 	return !!(pud_raw(pud) & cpu_to_be64(_PAGE_PTE));
 }
arch/powerpc/include/asm/nohash/32/pgtable.h +1 -0
index 0d40b33184eb..04a3b0b128eb 100644
@@ -201,6 +201,7 @@ static inline pte_t pte_mkexec(pte_t pte)
 }
 #endif
 
+#define pud_leaf		pud_leaf
 #define pmd_none(pmd)		(!pmd_val(pmd))
 #define	pmd_bad(pmd)		(pmd_val(pmd) & _PMD_BAD)
 #define	pmd_present(pmd)	(pmd_val(pmd) & _PMD_PRESENT_MASK)
arch/powerpc/include/asm/pgtable.h +9 -9
index 283f40d05a4d..8e7625a89922 100644
@@ -134,25 +134,25 @@ static inline void pte_frag_set(mm_context_t *ctx, void *p)
 }
 #endif
 
-#ifndef pmd_is_leaf
-#define pmd_is_leaf pmd_is_leaf
-static inline bool pmd_is_leaf(pmd_t pmd)
+#ifndef pmd_leaf
+#define pmd_leaf pmd_leaf
+static inline bool pmd_leaf(pmd_t pmd)
 {
 	return false;
 }
 #endif
 
-#ifndef pud_is_leaf
-#define pud_is_leaf pud_is_leaf
-static inline bool pud_is_leaf(pud_t pud)
+#ifndef pud_leaf
+#define pud_leaf pud_leaf
+static inline bool pud_leaf(pud_t pud)
 {
 	return false;
 }
 #endif
 
-#ifndef p4d_is_leaf
-#define p4d_is_leaf p4d_is_leaf
-static inline bool p4d_is_leaf(p4d_t p4d)
+#ifndef p4d_leaf
+#define p4d_leaf p4d_leaf
+static inline bool p4d_leaf(p4d_t p4d)
 {
 	return false;
 }
arch/powerpc/kvm/book3s_64_mmu_radix.c +6 -6
index 5d5e12f3bf86..d29f8d1d97a6 100644
@@ -497,7 +497,7 @@ static void kvmppc_unmap_free_pmd(struct kvm *kvm, pmd_t *pmd, bool full,
 	for (im = 0; im < PTRS_PER_PMD; ++im, ++p) {
 		if (!pmd_present(*p))
 			continue;
-		if (pmd_is_leaf(*p)) {
+		if (pmd_leaf(*p)) {
 			if (full) {
 				pmd_clear(p);
 			} else {
@@ -526,7 +526,7 @@ static void kvmppc_unmap_free_pud(struct kvm *kvm, pud_t *pud,
 	for (iu = 0; iu < PTRS_PER_PUD; ++iu, ++p) {
 		if (!pud_present(*p))
 			continue;
-		if (pud_is_leaf(*p)) {
+		if (pud_leaf(*p)) {
 			pud_clear(p);
 		} else {
 			pmd_t *pmd;
@@ -629,12 +629,12 @@ int kvmppc_create_pte(struct kvm *kvm, pgd_t *pgtable, pte_t pte,
 		new_pud = pud_alloc_one(kvm->mm, gpa);
 
 	pmd = NULL;
-	if (pud && pud_present(*pud) && !pud_is_leaf(*pud))
+	if (pud && pud_present(*pud) && !pud_leaf(*pud))
 		pmd = pmd_offset(pud, gpa);
 	else if (level <= 1)
 		new_pmd = kvmppc_pmd_alloc();
 
-	if (level == 0 && !(pmd && pmd_present(*pmd) && !pmd_is_leaf(*pmd)))
+	if (level == 0 && !(pmd && pmd_present(*pmd) && !pmd_leaf(*pmd)))
 		new_ptep = kvmppc_pte_alloc();
 
 	/* Check if we might have been invalidated; let the guest retry if so */
@@ -652,7 +652,7 @@ int kvmppc_create_pte(struct kvm *kvm, pgd_t *pgtable, pte_t pte,
 		new_pud = NULL;
 	}
 	pud = pud_offset(p4d, gpa);
-	if (pud_is_leaf(*pud)) {
+	if (pud_leaf(*pud)) {
 		unsigned long hgpa = gpa & PUD_MASK;
 
 		/* Check if we raced and someone else has set the same thing */
@@ -703,7 +703,7 @@ int kvmppc_create_pte(struct kvm *kvm, pgd_t *pgtable, pte_t pte,
 		new_pmd = NULL;
 	}
 	pmd = pmd_offset(pud, gpa);
-	if (pmd_is_leaf(*pmd)) {
+	if (pmd_leaf(*pmd)) {
 		unsigned long lgpa = gpa & PMD_MASK;
 
 		/* Check if we raced and someone else has set the same thing */
arch/powerpc/mm/book3s64/radix_pgtable.c +7 -7
index cac727b01799..8ac27e031ff4 100644
@@ -205,14 +205,14 @@ static void radix__change_memory_range(unsigned long start, unsigned long end,
 		pudp = pud_alloc(&init_mm, p4dp, idx);
 		if (!pudp)
 			continue;
-		if (pud_is_leaf(*pudp)) {
+		if (pud_leaf(*pudp)) {
 			ptep = (pte_t *)pudp;
 			goto update_the_pte;
 		}
 		pmdp = pmd_alloc(&init_mm, pudp, idx);
 		if (!pmdp)
 			continue;
-		if (pmd_is_leaf(*pmdp)) {
+		if (pmd_leaf(*pmdp)) {
 			ptep = pmdp_ptep(pmdp);
 			goto update_the_pte;
 		}
@@ -762,7 +762,7 @@ static void __meminit remove_pmd_table(pmd_t *pmd_start, unsigned long addr,
 		if (!pmd_present(*pmd))
 			continue;
 
-		if (pmd_is_leaf(*pmd)) {
+		if (pmd_leaf(*pmd)) {
 			if (!IS_ALIGNED(addr, PMD_SIZE) ||
 			    !IS_ALIGNED(next, PMD_SIZE)) {
 				WARN_ONCE(1, "%s: unaligned range\n", __func__);
@@ -792,7 +792,7 @@ static void __meminit remove_pud_table(pud_t *pud_start, unsigned long addr,
 		if (!pud_present(*pud))
 			continue;
 
-		if (pud_is_leaf(*pud)) {
+		if (pud_leaf(*pud)) {
 			if (!IS_ALIGNED(addr, PUD_SIZE) ||
 			    !IS_ALIGNED(next, PUD_SIZE)) {
 				WARN_ONCE(1, "%s: unaligned range\n", __func__);
@@ -825,7 +825,7 @@ static void __meminit remove_pagetable(unsigned long start, unsigned long end)
 		if (!p4d_present(*p4d))
 			continue;
 
-		if (p4d_is_leaf(*p4d)) {
+		if (p4d_leaf(*p4d)) {
 			if (!IS_ALIGNED(addr, P4D_SIZE) ||
 			    !IS_ALIGNED(next, P4D_SIZE)) {
 				WARN_ONCE(1, "%s: unaligned range\n", __func__);
@@ -1088,7 +1088,7 @@ int pud_set_huge(pud_t *pud, phys_addr_t addr, pgprot_t prot)
 
 int pud_clear_huge(pud_t *pud)
 {
-	if (pud_is_leaf(*pud)) {
+	if (pud_leaf(*pud)) {
 		pud_clear(pud);
 		return 1;
 	}
@@ -1135,7 +1135,7 @@ int pmd_set_huge(pmd_t *pmd, phys_addr_t addr, pgprot_t prot)
 
 int pmd_clear_huge(pmd_t *pmd)
 {
-	if (pmd_is_leaf(*pmd)) {
+	if (pmd_leaf(*pmd)) {
 		pmd_clear(pmd);
 		return 1;
 	}
arch/powerpc/mm/pgtable.c +3 -3
index cb2dcdb18f8e..35b9677b9553 100644
@@ -387,7 +387,7 @@ pte_t *__find_linux_pte(pgd_t *pgdir, unsigned long ea,
 	if (p4d_none(p4d))
 		return NULL;
 
-	if (p4d_is_leaf(p4d)) {
+	if (p4d_leaf(p4d)) {
 		ret_pte = (pte_t *)p4dp;
 		goto out;
 	}
@@ -409,7 +409,7 @@ pte_t *__find_linux_pte(pgd_t *pgdir, unsigned long ea,
 	if (pud_none(pud))
 		return NULL;
 
-	if (pud_is_leaf(pud)) {
+	if (pud_leaf(pud)) {
 		ret_pte = (pte_t *)pudp;
 		goto out;
 	}
@@ -448,7 +448,7 @@ pte_t *__find_linux_pte(pgd_t *pgdir, unsigned long ea,
 		goto out;
 	}
 
-	if (pmd_is_leaf(pmd)) {
+	if (pmd_leaf(pmd)) {
 		ret_pte = (pte_t *)pmdp;
 		goto out;
 	}
arch/powerpc/mm/pgtable_64.c +3 -3
index 5ac1fd30341b..0604c80dae66 100644
@@ -100,7 +100,7 @@ EXPORT_SYMBOL(__pte_frag_size_shift);
 /* 4 level page table */
 struct page *p4d_page(p4d_t p4d)
 {
-	if (p4d_is_leaf(p4d)) {
+	if (p4d_leaf(p4d)) {
 		if (!IS_ENABLED(CONFIG_HAVE_ARCH_HUGE_VMAP))
 			VM_WARN_ON(!p4d_huge(p4d));
 		return pte_page(p4d_pte(p4d));
@@ -111,7 +111,7 @@ struct page *p4d_page(p4d_t p4d)
 
 struct page *pud_page(pud_t pud)
 {
-	if (pud_is_leaf(pud)) {
+	if (pud_leaf(pud)) {
 		if (!IS_ENABLED(CONFIG_HAVE_ARCH_HUGE_VMAP))
 			VM_WARN_ON(!pud_huge(pud));
 		return pte_page(pud_pte(pud));
@@ -125,7 +125,7 @@ struct page *pud_page(pud_t pud)
  */
 struct page *pmd_page(pmd_t pmd)
 {
-	if (pmd_is_leaf(pmd)) {
+	if (pmd_leaf(pmd)) {
 		/*
 		 * vmalloc_to_page may be called on any vmap address (not only
 		 * vmalloc), and it uses pmd_page() etc., when huge vmap is
arch/powerpc/xmon/xmon.c +3 -3
index f51c882bf902..705c230dd4f5 100644
@@ -3342,7 +3342,7 @@ static void show_pte(unsigned long addr)
 		return;
 	}
 
-	if (p4d_is_leaf(*p4dp)) {
+	if (p4d_leaf(*p4dp)) {
 		format_pte(p4dp, p4d_val(*p4dp));
 		return;
 	}
@@ -3356,7 +3356,7 @@ static void show_pte(unsigned long addr)
 		return;
 	}
 
-	if (pud_is_leaf(*pudp)) {
+	if (pud_leaf(*pudp)) {
 		format_pte(pudp, pud_val(*pudp));
 		return;
 	}
@@ -3370,7 +3370,7 @@ static void show_pte(unsigned long addr)
 		return;
 	}
 
-	if (pmd_is_leaf(*pmdp)) {
+	if (pmd_leaf(*pmdp)) {
 		format_pte(pmdp, pmd_val(*pmdp));
 		return;
 	}
Signature · 1 line
2.34.1
#3 lore raw

PATCH 4/4 powerpc: mm: support page table check

On creation and clearing of a page table mapping, instrument such calls by invoking page_table_check_pte_set and page_table_check_pte_clear respectively. These calls serve as a sanity check against illegal mappings.

Enable ARCH_SUPPORTS_PAGE_TABLE_CHECK for all ppc64, and 32-bit platforms implementing Book3S.

Change pud_pfn to be a runtime bug rather than a build bug as it is consumed by page_table_check_pud_{clear,set} which are not called.

See also:

riscv support in commit 3fee229a8eb9 ("riscv/mm: enable ARCH_SUPPORTS_PAGE_TABLE_CHECK")
arm64 in commit 42b2547137f5 ("arm64/mm: enable ARCH_SUPPORTS_PAGE_TABLE_CHECK")
x86_64 in commit d283d422c6c4 ("x86: mm: add x86_64 support for page table check")

---
V2: Update spacing and types assigned to pte_update calls.
V3: Update one last pte_update call to remove __pte invocation.

---
 arch/powerpc/Kconfig                         |  1 +
 arch/powerpc/include/asm/book3s/32/pgtable.h |  9 ++++++++-
 arch/powerpc/include/asm/book3s/64/pgtable.h | 18 +++++++++++++++---
 arch/powerpc/include/asm/nohash/32/pgtable.h |  7 ++++++-
 arch/powerpc/include/asm/nohash/64/pgtable.h |  8 ++++++--
 arch/powerpc/include/asm/nohash/pgtable.h    |  1 +
 6 files changed, 37 insertions(+), 7 deletions(-)
arch/powerpc/Kconfig +1 -0
index 699df27b0e2f..1d943a13a204 100644
@@ -150,6 +150,7 @@ config PPC
 	select ARCH_STACKWALK
 	select ARCH_SUPPORTS_ATOMIC_RMW
 	select ARCH_SUPPORTS_DEBUG_PAGEALLOC	if PPC_BOOK3S || PPC_8xx || 40x
+	select ARCH_SUPPORTS_PAGE_TABLE_CHECK
 	select ARCH_USE_BUILTIN_BSWAP
 	select ARCH_USE_CMPXCHG_LOCKREF		if PPC64
 	select ARCH_USE_MEMTEST
arch/powerpc/include/asm/book3s/32/pgtable.h +8 -1
index 06e89bc4418a..6614170b6c17 100644
@@ -53,6 +53,8 @@
 
 #ifndef __ASSEMBLY__
 
+#include <linux/page_table_check.h>
+
 static inline bool pte_user(pte_t pte)
 {
 	return pte_val(pte) & _PAGE_USER;
@@ -333,7 +335,11 @@ static inline int __ptep_test_and_clear_young(struct mm_struct *mm,
 static inline pte_t ptep_get_and_clear(struct mm_struct *mm, unsigned long addr,
 				       pte_t *ptep)
 {
-	return __pte(pte_update(mm, addr, ptep, ~_PAGE_HASHPTE, 0, 0));
+	pte_t old_pte = __pte(pte_update(mm, addr, ptep, ~_PAGE_HASHPTE, 0, 0));
+
+	page_table_check_pte_clear(mm, addr, old_pte);
+
+	return old_pte;
 }
 
 #define __HAVE_ARCH_PTEP_SET_WRPROTECT
@@ -525,6 +531,7 @@ static inline bool pmd_user(pmd_t pmd)
 static inline void __set_pte_at(struct mm_struct *mm, unsigned long addr,
 				pte_t *ptep, pte_t pte, int percpu)
 {
+	page_table_check_pte_set(mm, addr, ptep, pte);
 #if defined(CONFIG_SMP) && !defined(CONFIG_PTE_64BIT)
 	/* First case is 32-bit Hash MMU in SMP mode with 32-bit PTEs. We use the
 	 * helper pte_update() which does an atomic update. We need to do that
arch/powerpc/include/asm/book3s/64/pgtable.h +15 -3
index acbb37e5f82f..f00aa2d203c2 100644
@@ -162,6 +162,8 @@
 #define PAGE_KERNEL_ROX	__pgprot(_PAGE_BASE | _PAGE_KERNEL_ROX)
 
 #ifndef __ASSEMBLY__
+#include <linux/page_table_check.h>
+
 /*
  * page table defines
  */
@@ -465,8 +467,11 @@ static inline void huge_ptep_set_wrprotect(struct mm_struct *mm,
 static inline pte_t ptep_get_and_clear(struct mm_struct *mm,
 				       unsigned long addr, pte_t *ptep)
 {
-	unsigned long old = pte_update(mm, addr, ptep, ~0UL, 0, 0);
-	return __pte(old);
+	pte_t old_pte = __pte(pte_update(mm, addr, ptep, ~0UL, 0, 0));
+
+	page_table_check_pte_clear(mm, addr, old_pte);
+
+	return old_pte;
 }
 
 #define __HAVE_ARCH_PTEP_GET_AND_CLEAR_FULL
@@ -475,11 +480,16 @@ static inline pte_t ptep_get_and_clear_full(struct mm_struct *mm,
 					    pte_t *ptep, int full)
 {
 	if (full && radix_enabled()) {
+		pte_t old_pte;
+
 		/*
 		 * We know that this is a full mm pte clear and
 		 * hence can be sure there is no parallel set_pte.
 		 */
-		return radix__ptep_get_and_clear_full(mm, addr, ptep, full);
+		old_pte = radix__ptep_get_and_clear_full(mm, addr, ptep, full);
+		page_table_check_pte_clear(mm, addr, old_pte);
+
+		return old_pte;
 	}
 	return ptep_get_and_clear(mm, addr, ptep);
 }
@@ -865,6 +875,8 @@ static inline void __set_pte_at(struct mm_struct *mm, unsigned long addr,
 	 */
 	pte = __pte_raw(pte_raw(pte) | cpu_to_be64(_PAGE_PTE));
 
+	page_table_check_pte_set(mm, addr, ptep, pte);
+
 	if (radix_enabled())
 		return radix__set_pte_at(mm, addr, ptep, pte, percpu);
 	return hash__set_pte_at(mm, addr, ptep, pte, percpu);
arch/powerpc/include/asm/nohash/32/pgtable.h +6 -1
index 1d3cf7fe60ec..2b23c5195d42 100644
@@ -166,6 +166,7 @@ void unmap_kernel_page(unsigned long va);
 #define _PAGE_CHG_MASK	(PTE_RPN_MASK | _PAGE_DIRTY | _PAGE_ACCESSED | _PAGE_SPECIAL)
 
 #ifndef __ASSEMBLY__
+#include <linux/page_table_check.h>
 
 #define pte_clear(mm, addr, ptep) \
 	do { pte_update(mm, addr, ptep, ~0, 0, 0); } while (0)
@@ -311,7 +312,11 @@ static inline int __ptep_test_and_clear_young(struct mm_struct *mm,
 static inline pte_t ptep_get_and_clear(struct mm_struct *mm, unsigned long addr,
 				       pte_t *ptep)
 {
-	return __pte(pte_update(mm, addr, ptep, ~0, 0, 0));
+	pte_t old_pte = __pte(pte_update(mm, addr, ptep, ~0, 0, 0));
+
+	page_table_check_pte_clear(mm, addr, old_pte);
+
+	return old_pte;
 }
 
 #define __HAVE_ARCH_PTEP_SET_WRPROTECT
arch/powerpc/include/asm/nohash/64/pgtable.h +6 -2
index 2f6b5124e64c..2da5d5a36192 100644
@@ -83,6 +83,7 @@
 #define H_PAGE_4K_PFN 0
 
 #ifndef __ASSEMBLY__
+#include <linux/page_table_check.h>
 /* pte_clear moved to later in this file */
 
 static inline pte_t pte_mkwrite(pte_t pte)
@@ -253,8 +254,11 @@ static inline void huge_ptep_set_wrprotect(struct mm_struct *mm,
 static inline pte_t ptep_get_and_clear(struct mm_struct *mm,
 				       unsigned long addr, pte_t *ptep)
 {
-	unsigned long old = pte_update(mm, addr, ptep, ~0UL, 0, 0);
-	return __pte(old);
+	pte_t old_pte = __pte(pte_update(mm, addr, ptep, ~0UL, 0, 0));
+
+	page_table_check_pte_clear(mm, addr, old_pte);
+
+	return old_pte;
 }
 
 static inline void pte_clear(struct mm_struct *mm, unsigned long addr,
arch/powerpc/include/asm/nohash/pgtable.h +1 -0
index d9067dfc531c..57371b52d9cd 100644
@@ -165,6 +165,7 @@ extern void set_pte_at(struct mm_struct *mm, unsigned long addr, pte_t *ptep,
 static inline void __set_pte_at(struct mm_struct *mm, unsigned long addr,
 				pte_t *ptep, pte_t pte, int percpu)
 {
+	page_table_check_pte_set(mm, addr, ptep, pte);
 	/* Second case is 32-bit with 64-bit PTE.  In this case, we
 	 * can just store as long as we do the two halves in the right order
 	 * with a barrier in between.
Signature · 1 line
2.34.1
#2 lore raw

PATCH 3/4 powerpc: mm: add p{te,md,ud}_user_accessible_page helpers

In reply to Rohan McLure

Add the following helpers for detecting whether a page table entry is a leaf and is accessible to user space.

* pte_user_accessible_page
* pmd_user_accessible_page
* pud_user_accessible_page

Also implement missing pud_user definitions for both Book3S/nohash 64-bit systems, and pmd_user for Book3S/nohash 32-bit systems.

---
V2: Provide missing pud_user implementations, use p{u,m}d_is_leaf.
V3: Provide missing pmd_user implementations as stubs in 32-bit.
V4: Use pmd_leaf, pud_leaf, and define pmd_user for 32 Book3E with static inline method rather than macro.

---
 arch/powerpc/include/asm/book3s/32/pgtable.h |  4 ++++
 arch/powerpc/include/asm/book3s/64/pgtable.h | 10 ++++++++++
 arch/powerpc/include/asm/nohash/32/pgtable.h |  5 +++++
 arch/powerpc/include/asm/nohash/64/pgtable.h | 10 ++++++++++
 arch/powerpc/include/asm/pgtable.h           | 15 +++++++++++++++
 5 files changed, 44 insertions(+)
arch/powerpc/include/asm/book3s/32/pgtable.h +4 -0
index f1b91ad8f3a5..06e89bc4418a 100644
@@ -511,6 +511,10 @@ static inline pte_t pte_modify(pte_t pte, pgprot_t newprot)
 	return __pte((pte_val(pte) & _PAGE_CHG_MASK) | pgprot_val(newprot));
 }
 
+static inline bool pmd_user(pmd_t pmd)
+{
+	return 0;
+}
 
 
 /* This low level function performs the actual PTE insertion
arch/powerpc/include/asm/book3s/64/pgtable.h +10 -0
index 788a9effb03e..acbb37e5f82f 100644
@@ -602,6 +602,16 @@ static inline bool pte_user(pte_t pte)
 	return !(pte_raw(pte) & cpu_to_be64(_PAGE_PRIVILEGED));
 }
 
+static inline bool pmd_user(pmd_t pmd)
+{
+	return !(pmd_raw(pmd) & cpu_to_be64(_PAGE_PRIVILEGED));
+}
+
+static inline bool pud_user(pud_t pud)
+{
+	return !(pud_raw(pud) & cpu_to_be64(_PAGE_PRIVILEGED));
+}
+
 #define pte_access_permitted pte_access_permitted
 static inline bool pte_access_permitted(pte_t pte, bool write)
 {
arch/powerpc/include/asm/nohash/32/pgtable.h +5 -0
index 04a3b0b128eb..1d3cf7fe60ec 100644
@@ -210,6 +210,11 @@ static inline void pmd_clear(pmd_t *pmdp)
 	*pmdp = __pmd(0);
 }
 
+static inline bool pmd_user(pmd_t pmd)
+{
+	return false;
+}
+
 /*
  * PTE updates. This function is called whenever an existing
  * valid PTE is updated. This does -not- include set_pte_at()
arch/powerpc/include/asm/nohash/64/pgtable.h +10 -0
index 879e9a6e5a87..2f6b5124e64c 100644
@@ -123,6 +123,11 @@ static inline pte_t pmd_pte(pmd_t pmd)
 	return __pte(pmd_val(pmd));
 }
 
+static inline bool pmd_user(pmd_t pmd)
+{
+	return (pmd_val(pmd) & _PAGE_USER) == _PAGE_USER;
+}
+
 #define pmd_none(pmd)		(!pmd_val(pmd))
 #define	pmd_bad(pmd)		(!is_kernel_addr(pmd_val(pmd)) \
 				 || (pmd_val(pmd) & PMD_BAD_BITS))
@@ -158,6 +163,11 @@ static inline pte_t pud_pte(pud_t pud)
 	return __pte(pud_val(pud));
 }
 
+static inline bool pud_user(pud_t pud)
+{
+	return (pud_val(pud) & _PAGE_USER) == _PAGE_USER;
+}
+
 static inline pud_t pte_pud(pte_t pte)
 {
 	return __pud(pte_val(pte));
arch/powerpc/include/asm/pgtable.h +15 -0
index c9ce5019f8a8..6851a7a90553 100644
@@ -191,6 +191,21 @@ static inline int pud_pfn(pud_t pud)
 }
 #endif
 
+static inline bool pte_user_accessible_page(pte_t pte)
+{
+	return pte_present(pte) && pte_user(pte);
+}
+
+static inline bool pmd_user_accessible_page(pmd_t pmd)
+{
+	return pmd_leaf(pmd) && pmd_present(pmd) && pmd_user(pmd);
+}
+
+static inline bool pud_user_accessible_page(pud_t pud)
+{
+	return pud_leaf(pud) && pud_present(pud) && pud_user(pud);
+}
+
 #endif /* __ASSEMBLY__ */
 
 #endif /* _ASM_POWERPC_PGTABLE_H */
Signature · 1 line
2.34.1
#4 lore raw
In reply to Rohan McLure
Quoted 345 lines · Rohan McLure

Replace occurrences of p{u,m,4}d_is_leaf with p{u,m,4}_leaf, as the latter is the name given to checking that a higher-level entry in multi-level paging contains a page translation entry (pte). This commit allows for p{u,m,4}d_leaf to be used on all powerpc platforms.

Prior to this commit, the two names have both been present in the kernel, having as far as I can tell the same exact purpose. While the 'is' in the title may better indicate that the macro/function is a boolean returning check, the former naming scheme is standard through all other architectures.

32-bit systems import pgtable-nop4d.h which defines a default pud_leaf. Define pud_leaf preprocessor macro on both Book3E/S 32-bit to avoid including the default definition in asm/pgtable.h.

---
V4: new patch.
---

arch/powerpc/include/asm/book3s/32/pgtable.h |  2 +-
arch/powerpc/include/asm/book3s/64/pgtable.h | 10 ++++------
arch/powerpc/include/asm/nohash/32/pgtable.h |  1 +
arch/powerpc/include/asm/pgtable.h           | 18 +++++++++---------
arch/powerpc/kvm/book3s_64_mmu_radix.c       | 12 ++++++------
arch/powerpc/mm/book3s64/radix_pgtable.c     | 14 +++++++-------
arch/powerpc/mm/pgtable.c                    |  6 +++---
arch/powerpc/mm/pgtable_64.c                 |  6 +++---
arch/powerpc/xmon/xmon.c                     |  6 +++---
9 files changed, 37 insertions(+), 38 deletions(-)
arch/powerpc/include/asm/book3s/32/pgtable.h +1 -1
index 75823f39e042..f1b91ad8f3a5 100644
@@ -234,6 +234,7 @@ void unmap_kernel_page(unsigned long va);
 #define pte_clear(mm, addr, ptep) \
 do { pte_update(mm, addr, ptep, ~_PAGE_HASHPTE, 0, 0); } while (0)
 
+#define pud_leaf pud_leaf
 #define pmd_none(pmd) (!pmd_val(pmd))
 #define pmd_bad(pmd) (pmd_val(pmd) & _PMD_BAD)
 #define pmd_present(pmd) (pmd_val(pmd) & _PMD_PRESENT_MASK)
@@ -242,7 +243,6 @@ static inline void pmd_clear(pmd_t *pmdp)
 *pmdp = __pmd(0);
 }
 
-
 /*
 * When flushing the tlb entry for a page, we also need to flush the hash
 * table entry.  flush_hash_pages is assembler (for speed) in hashtable.S.
arch/powerpc/include/asm/book3s/64/pgtable.h +4 -6
index c436d8422654..3f51de24e4fc 100644
@@ -1426,16 +1426,14 @@ static inline bool is_pte_rw_upgrade(unsigned long old_val, unsigned long new_va
 /*
 * Like pmd_huge() and pmd_large(), but works regardless of config options
 */
-#define pmd_is_leaf pmd_is_leaf
-#define pmd_leaf pmd_is_leaf
-static inline bool pmd_is_leaf(pmd_t pmd)
+#define pmd_leaf pmd_leaf
+static inline bool pmd_leaf(pmd_t pmd)
 {
 return !!(pmd_raw(pmd) & cpu_to_be64(_PAGE_PTE));
 }
 
-#define pud_is_leaf pud_is_leaf
-#define pud_leaf pud_is_leaf
-static inline bool pud_is_leaf(pud_t pud)
+#define pud_leaf pud_leaf
+static inline bool pud_leaf(pud_t pud)
 {
 return !!(pud_raw(pud) & cpu_to_be64(_PAGE_PTE));
 }
arch/powerpc/include/asm/nohash/32/pgtable.h +1 -0
index 0d40b33184eb..04a3b0b128eb 100644
@@ -201,6 +201,7 @@ static inline pte_t pte_mkexec(pte_t pte)
 }
 #endif
 
+#define pud_leaf pud_leaf
 #define pmd_none(pmd) (!pmd_val(pmd))
 #define pmd_bad(pmd) (pmd_val(pmd) & _PMD_BAD)
 #define pmd_present(pmd) (pmd_val(pmd) & _PMD_PRESENT_MASK)
arch/powerpc/include/asm/pgtable.h +9 -9
index 283f40d05a4d..8e7625a89922 100644
@@ -134,25 +134,25 @@ static inline void pte_frag_set(mm_context_t *ctx, void *p)
 }
 #endif
 
-#ifndef pmd_is_leaf
-#define pmd_is_leaf pmd_is_leaf
-static inline bool pmd_is_leaf(pmd_t pmd)
+#ifndef pmd_leaf
+#define pmd_leaf pmd_leaf
+static inline bool pmd_leaf(pmd_t pmd)
 {
 return false;
 }
 #endif
 
-#ifndef pud_is_leaf
-#define pud_is_leaf pud_is_leaf
-static inline bool pud_is_leaf(pud_t pud)
+#ifndef pud_leaf
+#define pud_leaf pud_leaf
+static inline bool pud_leaf(pud_t pud)
 {
 return false;
 }
 #endif
 
-#ifndef p4d_is_leaf
-#define p4d_is_leaf p4d_is_leaf
-static inline bool p4d_is_leaf(p4d_t p4d)
+#ifndef p4d_leaf
+#define p4d_leaf p4d_leaf
+static inline bool p4d_leaf(p4d_t p4d)
 {
 return false;
 }
arch/powerpc/kvm/book3s_64_mmu_radix.c +6 -6
index 5d5e12f3bf86..d29f8d1d97a6 100644
@@ -497,7 +497,7 @@ static void kvmppc_unmap_free_pmd(struct kvm *kvm, pmd_t *pmd, bool full,
 for (im = 0; im < PTRS_PER_PMD; ++im, ++p) {
 if (!pmd_present(*p))
 continue;
- if (pmd_is_leaf(*p)) {
+ if (pmd_leaf(*p)) {
 if (full) {
 pmd_clear(p);
 } else {
@@ -526,7 +526,7 @@ static void kvmppc_unmap_free_pud(struct kvm *kvm, pud_t *pud,
 for (iu = 0; iu < PTRS_PER_PUD; ++iu, ++p) {
 if (!pud_present(*p))
 continue;
- if (pud_is_leaf(*p)) {
+ if (pud_leaf(*p)) {
 pud_clear(p);
 } else {
 pmd_t *pmd;
@@ -629,12 +629,12 @@ int kvmppc_create_pte(struct kvm *kvm, pgd_t *pgtable, pte_t pte,
 new_pud = pud_alloc_one(kvm->mm, gpa);
 
 pmd = NULL;
- if (pud && pud_present(*pud) && !pud_is_leaf(*pud))
+ if (pud && pud_present(*pud) && !pud_leaf(*pud))
 pmd = pmd_offset(pud, gpa);
 else if (level <= 1)
 new_pmd = kvmppc_pmd_alloc();
 
- if (level == 0 && !(pmd && pmd_present(*pmd) && !pmd_is_leaf(*pmd)))
+ if (level == 0 && !(pmd && pmd_present(*pmd) && !pmd_leaf(*pmd)))
 new_ptep = kvmppc_pte_alloc();
 
 /* Check if we might have been invalidated; let the guest retry if so */
@@ -652,7 +652,7 @@ int kvmppc_create_pte(struct kvm *kvm, pgd_t *pgtable, pte_t pte,
 new_pud = NULL;
 }
 pud = pud_offset(p4d, gpa);
- if (pud_is_leaf(*pud)) {
+ if (pud_leaf(*pud)) {
 unsigned long hgpa = gpa & PUD_MASK;
 
 /* Check if we raced and someone else has set the same thing */
@@ -703,7 +703,7 @@ int kvmppc_create_pte(struct kvm *kvm, pgd_t *pgtable, pte_t pte,
 new_pmd = NULL;
 }
 pmd = pmd_offset(pud, gpa);
- if (pmd_is_leaf(*pmd)) {
+ if (pmd_leaf(*pmd)) {
 unsigned long lgpa = gpa & PMD_MASK;
 
 /* Check if we raced and someone else has set the same thing */
arch/powerpc/mm/book3s64/radix_pgtable.c +3 -3
index cac727b01799..8ac27e031ff4 100644
@@ -205,14 +205,14 @@ static void radix__change_memory_range(unsigned long start, unsigned long end,
 pudp = pud_alloc(&init_mm, p4dp, idx);
 if (!pudp)
 continue;
- if (pud_is_leaf(*pudp)) {
+ if (pud_leaf(*pudp)) {
 ptep = (pte_t *)pudp;
 goto update_the_pte;
 }
 pmdp = pmd_alloc(&init_mm, pudp, idx);
 if (!pmdp)
 continue;
- if (pmd_is_leaf(*pmdp)) {
+ if (pmd_leaf(*pmdp)) {
 ptep = pmdp_ptep(pmdp);
 goto update_the_pte;
 }
@@ -762,7 +762,7 @@ static void __meminit remove_pmd_table(pmd_t *pmd_start, unsigned long addr,
 if (!pmd_present(*pmd))
 continue;
 
- if (pmd_is_leaf(*pmd)) {
+ if (pmd_leaf(*pmd)) {

if (!IS_ALIGNED(addr, PMD_SIZE) ||
!IS_ALIGNED(next, PMD_SIZE)) {
WARN_ONCE(1, "%s: unaligned range\n", __func__);

arch/powerpc/mm/book3s64/radix_pgtable.c +1 -1
@@ -792,7 +792,7 @@ static void __meminit remove_pud_table(pud_t *pud_start, unsigned long addr,
 if (!pud_present(*pud))
 continue;
 
- if (pud_is_leaf(*pud)) {
+ if (pud_leaf(*pud)) {

if (!IS_ALIGNED(addr, PUD_SIZE) ||
!IS_ALIGNED(next, PUD_SIZE)) {
WARN_ONCE(1, "%s: unaligned range\n", __func__);

arch/powerpc/mm/book3s64/radix_pgtable.c +1 -1
@@ -825,7 +825,7 @@ static void __meminit remove_pagetable(unsigned long start, unsigned long end)
 if (!p4d_present(*p4d))
 continue;
 
- if (p4d_is_leaf(*p4d)) {
+ if (p4d_leaf(*p4d)) {

if (!IS_ALIGNED(addr, P4D_SIZE) ||
!IS_ALIGNED(next, P4D_SIZE)) {
WARN_ONCE(1, "%s: unaligned range\n", __func__);

arch/powerpc/mm/book3s64/radix_pgtable.c +2 -2
@@ -1088,7 +1088,7 @@ int pud_set_huge(pud_t *pud, phys_addr_t addr, pgprot_t prot)
 
 int pud_clear_huge(pud_t *pud)
 {
- if (pud_is_leaf(*pud)) {
+ if (pud_leaf(*pud)) {
 pud_clear(pud);
 return 1;
 }
@@ -1135,7 +1135,7 @@ int pmd_set_huge(pmd_t *pmd, phys_addr_t addr, pgprot_t prot)
 
 int pmd_clear_huge(pmd_t *pmd)
 {
- if (pmd_is_leaf(*pmd)) {
+ if (pmd_leaf(*pmd)) {
 pmd_clear(pmd);
 return 1;
 }
arch/powerpc/mm/pgtable.c +3 -3
index cb2dcdb18f8e..35b9677b9553 100644
@@ -387,7 +387,7 @@ pte_t *__find_linux_pte(pgd_t *pgdir, unsigned long ea,
 if (p4d_none(p4d))
 return NULL;
 
- if (p4d_is_leaf(p4d)) {
+ if (p4d_leaf(p4d)) {
 ret_pte = (pte_t *)p4dp;
 goto out;
 }
@@ -409,7 +409,7 @@ pte_t *__find_linux_pte(pgd_t *pgdir, unsigned long ea,
 if (pud_none(pud))
 return NULL;
 
- if (pud_is_leaf(pud)) {
+ if (pud_leaf(pud)) {
 ret_pte = (pte_t *)pudp;
 goto out;
 }
@@ -448,7 +448,7 @@ pte_t *__find_linux_pte(pgd_t *pgdir, unsigned long ea,
 goto out;
 }
 
- if (pmd_is_leaf(pmd)) {
+ if (pmd_leaf(pmd)) {
 ret_pte = (pte_t *)pmdp;
 goto out;
 }
arch/powerpc/mm/pgtable_64.c +3 -3
index 5ac1fd30341b..0604c80dae66 100644
@@ -100,7 +100,7 @@ EXPORT_SYMBOL(__pte_frag_size_shift);
 /* 4 level page table */
 struct page *p4d_page(p4d_t p4d)
 {
- if (p4d_is_leaf(p4d)) {
+ if (p4d_leaf(p4d)) {
 if (!IS_ENABLED(CONFIG_HAVE_ARCH_HUGE_VMAP))
 VM_WARN_ON(!p4d_huge(p4d));
 return pte_page(p4d_pte(p4d));
@@ -111,7 +111,7 @@ struct page *p4d_page(p4d_t p4d)
 
 struct page *pud_page(pud_t pud)
 {
- if (pud_is_leaf(pud)) {
+ if (pud_leaf(pud)) {
 if (!IS_ENABLED(CONFIG_HAVE_ARCH_HUGE_VMAP))
 VM_WARN_ON(!pud_huge(pud));
 return pte_page(pud_pte(pud));
@@ -125,7 +125,7 @@ struct page *pud_page(pud_t pud)
 */
 struct page *pmd_page(pmd_t pmd)
 {
- if (pmd_is_leaf(pmd)) {
+ if (pmd_leaf(pmd)) {
 /*
 * vmalloc_to_page may be called on any vmap address (not only
 * vmalloc), and it uses pmd_page() etc., when huge vmap is
arch/powerpc/xmon/xmon.c +3 -3
index f51c882bf902..705c230dd4f5 100644
@@ -3342,7 +3342,7 @@ static void show_pte(unsigned long addr)
 return;
 }
 
- if (p4d_is_leaf(*p4dp)) {
+ if (p4d_leaf(*p4dp)) {
 format_pte(p4dp, p4d_val(*p4dp));
 return;
 }
@@ -3356,7 +3356,7 @@ static void show_pte(unsigned long addr)
 return;
 }
 
- if (pud_is_leaf(*pudp)) {
+ if (pud_leaf(*pudp)) {
 format_pte(pudp, pud_val(*pudp));
 return;
 }
@@ -3370,7 +3370,7 @@ static void show_pte(unsigned long addr)
 return;
 }
 
- if (pmd_is_leaf(*pmdp)) {
+ if (pmd_leaf(*pmdp)) {
 format_pte(pmdp, pmd_val(*pmdp));
 return;
 }
Signature · 1 line
2.34.1

This should in fact be a v4. v3 available here:

#5 lore raw
In reply to Rohan McLure

Le 06/11/2022 à 23:49, Rohan McLure a écrit :

Quoted 10 lines

Replace occurrences of p{u,m,4}d_is_leaf with p{u,m,4}_leaf, as the latter is the name given to checking that a higher-level entry in multi-level paging contains a page translation entry (pte). This commit allows for p{u,m,4}d_leaf to be used on all powerpc platforms.

Prior to this commit, the two names have both been present in the kernel, having as far as I can tell the same exact purpose. While the 'is' in the title may better indicate that the macro/function is a boolean returning check, the former naming scheme is standard through all other architectures.

Would be easier to understand and review if you split in two patches:
1/ Replace all uses of p{u,m,4}d_is_leaf by p{u,m,4}_leaf 2/ Properly implement p{u,m,4}_leaf and remove p{u,m,4}d_is_leaf

Quoted 4 lines

32-bit systems import pgtable-nop4d.h which defines a default pud_leaf. Define pud_leaf preprocessor macro on both Book3E/S 32-bit to avoid including the default definition in asm/pgtable.h.

I think you should do it the other way round: Move it away from asm/pgtable.h.

pud_leaf(), you only have to add a stub in asm/nohash/64/pgtable.h

pmd_leaf(), you have to add a stub in asm/nohash/pgtable.h and asm/book3s/32/pgtable.h

I think doing like that would be cleaner.

Christophe

Quoted 326 lines

---
V4: new patch.

---
  arch/powerpc/include/asm/book3s/32/pgtable.h |  2 +-
  arch/powerpc/include/asm/book3s/64/pgtable.h | 10 ++++------
  arch/powerpc/include/asm/nohash/32/pgtable.h |  1 +
  arch/powerpc/include/asm/pgtable.h           | 18 +++++++++---------
  arch/powerpc/kvm/book3s_64_mmu_radix.c       | 12 ++++++------
  arch/powerpc/mm/book3s64/radix_pgtable.c     | 14 +++++++-------
  arch/powerpc/mm/pgtable.c                    |  6 +++---
  arch/powerpc/mm/pgtable_64.c                 |  6 +++---
  arch/powerpc/xmon/xmon.c                     |  6 +++---
  9 files changed, 37 insertions(+), 38 deletions(-)
arch/powerpc/include/asm/book3s/32/pgtable.h +1 -1
index 75823f39e042..f1b91ad8f3a5 100644
@@ -234,6 +234,7 @@ void unmap_kernel_page(unsigned long va);
  #define pte_clear(mm, addr, ptep) \
  	do { pte_update(mm, addr, ptep, ~_PAGE_HASHPTE, 0, 0); } while (0)
 
+#define pud_leaf		pud_leaf
  #define pmd_none(pmd)		(!pmd_val(pmd))
  #define	pmd_bad(pmd)		(pmd_val(pmd) & _PMD_BAD)
  #define	pmd_present(pmd)	(pmd_val(pmd) & _PMD_PRESENT_MASK)
@@ -242,7 +243,6 @@ static inline void pmd_clear(pmd_t *pmdp)
  	*pmdp = __pmd(0);
  }
 
-
  /*
   * When flushing the tlb entry for a page, we also need to flush the hash
   * table entry.  flush_hash_pages is assembler (for speed) in hashtable.S.
arch/powerpc/include/asm/book3s/64/pgtable.h +4 -6
index c436d8422654..3f51de24e4fc 100644
@@ -1426,16 +1426,14 @@ static inline bool is_pte_rw_upgrade(unsigned long old_val, unsigned long new_va
  /*
   * Like pmd_huge() and pmd_large(), but works regardless of config options
   */
-#define pmd_is_leaf pmd_is_leaf
-#define pmd_leaf pmd_is_leaf
-static inline bool pmd_is_leaf(pmd_t pmd)
+#define pmd_leaf pmd_leaf
+static inline bool pmd_leaf(pmd_t pmd)
  {
  	return !!(pmd_raw(pmd) & cpu_to_be64(_PAGE_PTE));
  }
 
-#define pud_is_leaf pud_is_leaf
-#define pud_leaf pud_is_leaf
-static inline bool pud_is_leaf(pud_t pud)
+#define pud_leaf pud_leaf
+static inline bool pud_leaf(pud_t pud)
  {
  	return !!(pud_raw(pud) & cpu_to_be64(_PAGE_PTE));
  }
arch/powerpc/include/asm/nohash/32/pgtable.h +1 -0
index 0d40b33184eb..04a3b0b128eb 100644
@@ -201,6 +201,7 @@ static inline pte_t pte_mkexec(pte_t pte)
  }
  #endif
 
+#define pud_leaf		pud_leaf
  #define pmd_none(pmd)		(!pmd_val(pmd))
  #define	pmd_bad(pmd)		(pmd_val(pmd) & _PMD_BAD)
  #define	pmd_present(pmd)	(pmd_val(pmd) & _PMD_PRESENT_MASK)
arch/powerpc/include/asm/pgtable.h +9 -9
index 283f40d05a4d..8e7625a89922 100644
@@ -134,25 +134,25 @@ static inline void pte_frag_set(mm_context_t *ctx, void *p)
  }
  #endif
 
-#ifndef pmd_is_leaf
-#define pmd_is_leaf pmd_is_leaf
-static inline bool pmd_is_leaf(pmd_t pmd)
+#ifndef pmd_leaf
+#define pmd_leaf pmd_leaf
+static inline bool pmd_leaf(pmd_t pmd)
  {
  	return false;
  }
  #endif
 
-#ifndef pud_is_leaf
-#define pud_is_leaf pud_is_leaf
-static inline bool pud_is_leaf(pud_t pud)
+#ifndef pud_leaf
+#define pud_leaf pud_leaf
+static inline bool pud_leaf(pud_t pud)
  {
  	return false;
  }
  #endif
 
-#ifndef p4d_is_leaf
-#define p4d_is_leaf p4d_is_leaf
-static inline bool p4d_is_leaf(p4d_t p4d)
+#ifndef p4d_leaf
+#define p4d_leaf p4d_leaf
+static inline bool p4d_leaf(p4d_t p4d)
  {
  	return false;
  }
arch/powerpc/kvm/book3s_64_mmu_radix.c +6 -6
index 5d5e12f3bf86..d29f8d1d97a6 100644
@@ -497,7 +497,7 @@ static void kvmppc_unmap_free_pmd(struct kvm *kvm, pmd_t *pmd, bool full,
  	for (im = 0; im < PTRS_PER_PMD; ++im, ++p) {
  		if (!pmd_present(*p))
  			continue;
-		if (pmd_is_leaf(*p)) {
+		if (pmd_leaf(*p)) {
  			if (full) {
  				pmd_clear(p);
  			} else {
@@ -526,7 +526,7 @@ static void kvmppc_unmap_free_pud(struct kvm *kvm, pud_t *pud,
  	for (iu = 0; iu < PTRS_PER_PUD; ++iu, ++p) {
  		if (!pud_present(*p))
  			continue;
-		if (pud_is_leaf(*p)) {
+		if (pud_leaf(*p)) {
  			pud_clear(p);
  		} else {
  			pmd_t *pmd;
@@ -629,12 +629,12 @@ int kvmppc_create_pte(struct kvm *kvm, pgd_t *pgtable, pte_t pte,
  		new_pud = pud_alloc_one(kvm->mm, gpa);
 
  	pmd = NULL;
-	if (pud && pud_present(*pud) && !pud_is_leaf(*pud))
+	if (pud && pud_present(*pud) && !pud_leaf(*pud))
  		pmd = pmd_offset(pud, gpa);
  	else if (level <= 1)
  		new_pmd = kvmppc_pmd_alloc();
 
-	if (level == 0 && !(pmd && pmd_present(*pmd) && !pmd_is_leaf(*pmd)))
+	if (level == 0 && !(pmd && pmd_present(*pmd) && !pmd_leaf(*pmd)))
  		new_ptep = kvmppc_pte_alloc();
 
  	/* Check if we might have been invalidated; let the guest retry if so */
@@ -652,7 +652,7 @@ int kvmppc_create_pte(struct kvm *kvm, pgd_t *pgtable, pte_t pte,
  		new_pud = NULL;
  	}
  	pud = pud_offset(p4d, gpa);
-	if (pud_is_leaf(*pud)) {
+	if (pud_leaf(*pud)) {
  		unsigned long hgpa = gpa & PUD_MASK;
 
  		/* Check if we raced and someone else has set the same thing */
@@ -703,7 +703,7 @@ int kvmppc_create_pte(struct kvm *kvm, pgd_t *pgtable, pte_t pte,
  		new_pmd = NULL;
  	}
  	pmd = pmd_offset(pud, gpa);
-	if (pmd_is_leaf(*pmd)) {
+	if (pmd_leaf(*pmd)) {
  		unsigned long lgpa = gpa & PMD_MASK;
 
  		/* Check if we raced and someone else has set the same thing */
arch/powerpc/mm/book3s64/radix_pgtable.c +7 -7
index cac727b01799..8ac27e031ff4 100644
@@ -205,14 +205,14 @@ static void radix__change_memory_range(unsigned long start, unsigned long end,
  		pudp = pud_alloc(&init_mm, p4dp, idx);
  		if (!pudp)
  			continue;
-		if (pud_is_leaf(*pudp)) {
+		if (pud_leaf(*pudp)) {
  			ptep = (pte_t *)pudp;
  			goto update_the_pte;
  		}
  		pmdp = pmd_alloc(&init_mm, pudp, idx);
  		if (!pmdp)
  			continue;
-		if (pmd_is_leaf(*pmdp)) {
+		if (pmd_leaf(*pmdp)) {
  			ptep = pmdp_ptep(pmdp);
  			goto update_the_pte;
  		}
@@ -762,7 +762,7 @@ static void __meminit remove_pmd_table(pmd_t *pmd_start, unsigned long addr,
  		if (!pmd_present(*pmd))
  			continue;
 
-		if (pmd_is_leaf(*pmd)) {
+		if (pmd_leaf(*pmd)) {
  			if (!IS_ALIGNED(addr, PMD_SIZE) ||
  			    !IS_ALIGNED(next, PMD_SIZE)) {
  				WARN_ONCE(1, "%s: unaligned range\n", __func__);
@@ -792,7 +792,7 @@ static void __meminit remove_pud_table(pud_t *pud_start, unsigned long addr,
  		if (!pud_present(*pud))
  			continue;
 
-		if (pud_is_leaf(*pud)) {
+		if (pud_leaf(*pud)) {
  			if (!IS_ALIGNED(addr, PUD_SIZE) ||
  			    !IS_ALIGNED(next, PUD_SIZE)) {
  				WARN_ONCE(1, "%s: unaligned range\n", __func__);
@@ -825,7 +825,7 @@ static void __meminit remove_pagetable(unsigned long start, unsigned long end)
  		if (!p4d_present(*p4d))
  			continue;
 
-		if (p4d_is_leaf(*p4d)) {
+		if (p4d_leaf(*p4d)) {
  			if (!IS_ALIGNED(addr, P4D_SIZE) ||
  			    !IS_ALIGNED(next, P4D_SIZE)) {
  				WARN_ONCE(1, "%s: unaligned range\n", __func__);
@@ -1088,7 +1088,7 @@ int pud_set_huge(pud_t *pud, phys_addr_t addr, pgprot_t prot)
 
  int pud_clear_huge(pud_t *pud)
  {
-	if (pud_is_leaf(*pud)) {
+	if (pud_leaf(*pud)) {
  		pud_clear(pud);
  		return 1;
  	}
@@ -1135,7 +1135,7 @@ int pmd_set_huge(pmd_t *pmd, phys_addr_t addr, pgprot_t prot)
 
  int pmd_clear_huge(pmd_t *pmd)
  {
-	if (pmd_is_leaf(*pmd)) {
+	if (pmd_leaf(*pmd)) {
  		pmd_clear(pmd);
  		return 1;
  	}
arch/powerpc/mm/pgtable.c +3 -3
index cb2dcdb18f8e..35b9677b9553 100644
@@ -387,7 +387,7 @@ pte_t *__find_linux_pte(pgd_t *pgdir, unsigned long ea,
  	if (p4d_none(p4d))
  		return NULL;
 
-	if (p4d_is_leaf(p4d)) {
+	if (p4d_leaf(p4d)) {
  		ret_pte = (pte_t *)p4dp;
  		goto out;
  	}
@@ -409,7 +409,7 @@ pte_t *__find_linux_pte(pgd_t *pgdir, unsigned long ea,
  	if (pud_none(pud))
  		return NULL;
 
-	if (pud_is_leaf(pud)) {
+	if (pud_leaf(pud)) {
  		ret_pte = (pte_t *)pudp;
  		goto out;
  	}
@@ -448,7 +448,7 @@ pte_t *__find_linux_pte(pgd_t *pgdir, unsigned long ea,
  		goto out;
  	}
 
-	if (pmd_is_leaf(pmd)) {
+	if (pmd_leaf(pmd)) {
  		ret_pte = (pte_t *)pmdp;
  		goto out;
  	}
arch/powerpc/mm/pgtable_64.c +3 -3
index 5ac1fd30341b..0604c80dae66 100644
@@ -100,7 +100,7 @@ EXPORT_SYMBOL(__pte_frag_size_shift);
  /* 4 level page table */
  struct page *p4d_page(p4d_t p4d)
  {
-	if (p4d_is_leaf(p4d)) {
+	if (p4d_leaf(p4d)) {
  		if (!IS_ENABLED(CONFIG_HAVE_ARCH_HUGE_VMAP))
  			VM_WARN_ON(!p4d_huge(p4d));
  		return pte_page(p4d_pte(p4d));
@@ -111,7 +111,7 @@ struct page *p4d_page(p4d_t p4d)
 
  struct page *pud_page(pud_t pud)
  {
-	if (pud_is_leaf(pud)) {
+	if (pud_leaf(pud)) {
  		if (!IS_ENABLED(CONFIG_HAVE_ARCH_HUGE_VMAP))
  			VM_WARN_ON(!pud_huge(pud));
  		return pte_page(pud_pte(pud));
@@ -125,7 +125,7 @@ struct page *pud_page(pud_t pud)
   */
  struct page *pmd_page(pmd_t pmd)
  {
-	if (pmd_is_leaf(pmd)) {
+	if (pmd_leaf(pmd)) {
  		/*
  		 * vmalloc_to_page may be called on any vmap address (not only
  		 * vmalloc), and it uses pmd_page() etc., when huge vmap is
arch/powerpc/xmon/xmon.c +3 -3
index f51c882bf902..705c230dd4f5 100644
@@ -3342,7 +3342,7 @@ static void show_pte(unsigned long addr)
  		return;
  	}
 
-	if (p4d_is_leaf(*p4dp)) {
+	if (p4d_leaf(*p4dp)) {
  		format_pte(p4dp, p4d_val(*p4dp));
  		return;
  	}
@@ -3356,7 +3356,7 @@ static void show_pte(unsigned long addr)
  		return;
  	}
 
-	if (pud_is_leaf(*pudp)) {
+	if (pud_leaf(*pudp)) {
  		format_pte(pudp, pud_val(*pudp));
  		return;
  	}
@@ -3370,7 +3370,7 @@ static void show_pte(unsigned long addr)
  		return;
  	}
 
-	if (pmd_is_leaf(*pmdp)) {
+	if (pmd_leaf(*pmdp)) {
  		format_pte(pmdp, pmd_val(*pmdp));
  		return;
  	}