Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

mm/powerpc: redefine pXd_huge() with pXd_leaf()

PowerPC book3s 4K mostly has the same definition on both, except
pXd_huge() constantly returns 0 for hash MMUs. As Michael Ellerman
pointed out [1], it is safe to check _PAGE_PTE on hash MMUs, as the bit
will never be set so it will keep returning false.

As a reference, __p[mu]d_mkhuge() will trigger a BUG_ON trying to create
such huge mappings for 4K hash MMUs. Meanwhile, the major powerpc hugetlb
pgtable walker __find_linux_pte() already used pXd_leaf() to check leaf
hugetlb mappings.

The goal should be that we will have one API pXd_leaf() to detect all
kinds of huge mappings (hugepd is still special in this case, though).
AFAICT we need to use the pXd_leaf() impl (rather than pXd_huge()'s) to
make sure ie. THPs on hash MMU will also return true.

This helps to simplify a follow up patch to drop pXd_huge() treewide.

NOTE: *_leaf() definition need to be moved before the inclusion of
asm/book3s/64/pgtable-4k.h, which defines pXd_huge() with it.

[1] https://lore.kernel.org/r/87v85zo6w7.fsf@mail.lhotse

Link: https://lkml.kernel.org/r/20240318200404.448346-10-peterx@redhat.com
Signed-off-by: Peter Xu <peterx@redhat.com>
Cc: Michael Ellerman <mpe@ellerman.id.au>
Cc: Nicholas Piggin <npiggin@gmail.com>
Cc: Christophe Leroy <christophe.leroy@csgroup.eu>
Cc: "Aneesh Kumar K.V" <aneesh.kumar@kernel.org>
Cc: "Naveen N. Rao" <naveen.n.rao@linux.ibm.com>
Cc: Alistair Popple <apopple@nvidia.com>
Cc: Andreas Larsson <andreas@gaisler.com>
Cc: Arnd Bergmann <arnd@arndb.de>
Cc: Bjorn Andersson <andersson@kernel.org>
Cc: Borislav Petkov <bp@alien8.de>
Cc: Catalin Marinas <catalin.marinas@arm.com>
Cc: Dave Hansen <dave.hansen@linux.intel.com>
Cc: David S. Miller <davem@davemloft.net>
Cc: Fabio Estevam <festevam@denx.de>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Jason Gunthorpe <jgg@nvidia.com>
Cc: Konrad Dybcio <konrad.dybcio@linaro.org>
Cc: Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org>
Cc: Lucas Stach <l.stach@pengutronix.de>
Cc: Mark Salter <msalter@redhat.com>
Cc: "Matthew Wilcox (Oracle)" <willy@infradead.org>
Cc: Mike Rapoport (IBM) <rppt@kernel.org>
Cc: Muchun Song <muchun.song@linux.dev>
Cc: Naoya Horiguchi <nao.horiguchi@gmail.com>
Cc: Russell King <linux@armlinux.org.uk>
Cc: Shawn Guo <shawnguo@kernel.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Will Deacon <will@kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

Peter Xu and committed by
Andrew Morton
460b9adc 961a6ee5

+14 -27
+2 -12
arch/powerpc/include/asm/book3s/64/pgtable-4k.h
··· 8 8 #ifdef CONFIG_HUGETLB_PAGE 9 9 static inline int pmd_huge(pmd_t pmd) 10 10 { 11 - /* 12 - * leaf pte for huge page 13 - */ 14 - if (radix_enabled()) 15 - return !!(pmd_raw(pmd) & cpu_to_be64(_PAGE_PTE)); 16 - return 0; 11 + return pmd_leaf(pmd); 17 12 } 18 13 19 14 static inline int pud_huge(pud_t pud) 20 15 { 21 - /* 22 - * leaf pte for huge page 23 - */ 24 - if (radix_enabled()) 25 - return !!(pud_raw(pud) & cpu_to_be64(_PAGE_PTE)); 26 - return 0; 16 + return pud_leaf(pud); 27 17 } 28 18 29 19 /*
+12 -15
arch/powerpc/include/asm/book3s/64/pgtable.h
··· 262 262 263 263 extern struct page *vmemmap; 264 264 extern unsigned long pci_io_base; 265 + 266 + #define pmd_leaf pmd_leaf 267 + static inline bool pmd_leaf(pmd_t pmd) 268 + { 269 + return !!(pmd_raw(pmd) & cpu_to_be64(_PAGE_PTE)); 270 + } 271 + 272 + #define pud_leaf pud_leaf 273 + static inline bool pud_leaf(pud_t pud) 274 + { 275 + return !!(pud_raw(pud) & cpu_to_be64(_PAGE_PTE)); 276 + } 265 277 #endif /* __ASSEMBLY__ */ 266 278 267 279 #include <asm/book3s/64/hash.h> ··· 1436 1424 return true; 1437 1425 1438 1426 return false; 1439 - } 1440 - 1441 - /* 1442 - * Like pmd_huge(), but works regardless of config options 1443 - */ 1444 - #define pmd_leaf pmd_leaf 1445 - static inline bool pmd_leaf(pmd_t pmd) 1446 - { 1447 - return !!(pmd_raw(pmd) & cpu_to_be64(_PAGE_PTE)); 1448 - } 1449 - 1450 - #define pud_leaf pud_leaf 1451 - static inline bool pud_leaf(pud_t pud) 1452 - { 1453 - return !!(pud_raw(pud) & cpu_to_be64(_PAGE_PTE)); 1454 1427 } 1455 1428 1456 1429 #endif /* __ASSEMBLY__ */