powerpc: ideas to improve page table frag allocator
Nicholas Piggin
reviewerRFC PATCH 1/1 powerpc: ideas to improve page table frag allocator
The page table fragment allocator is a simple per-mm slab allocator. It can be quite wasteful of space for small processes, as well as being expensive to initialise. It does not do well at NUMA awareness.
This is a quick hack at addressing some of those problems, but it's not complete. It doesn't support THP because it doesn't deal with the page table deposit. It has has certain cases where cross-CPU locking could be increased (but also a reduction in other cases including reduction on ptl). NUMA still has some corner case issues, but it is improved. So it's not mergeable yet or necessarily the best way to solve the problems. Just a quick hack for some testing.
It save 1-2MB on a simple distro boot on a small (4 CPU) system. The powerpc fork selftests benchmark with --fork performance is improved by 15% on a POWER9 (14.5k/s -> 17k/s). This is just about a worst-case microbenchmark, but would still be good to fix it.
What would really be nice is if we could avoid writing our own allocator and use the slab allocator. The problem being we need a page table lock spinlock associated with the page table, and that must be able to be derived from the page table pointer, and I don't think slab has anything that fits the bill.
Thanks,
Nick
arch/powerpc/mm/book3s64/pgtable.c +0 -88
@@ -281,94 +281,6 @@ void mmu_partition_table_set_entry(unsigned int lpid, unsigned long dw0, }EXPORT_SYMBOL_GPL (mmu_partition_table_set_entry); -static pmd_t *get_pmd_from_cache (struct mm_struct *mm) -{ -void *pmd_frag, *ret; - -if (PMD_FRAG_NR ==1 ) -return NULL ; - -spin_lock (&mm->page_table_lock); - ret = mm->context.pmd_frag; -if (ret) { - pmd_frag = ret +PMD_FRAG_SIZE ; -/* -* If we have taken up all the fragments mark PTE page NULL -*/ -if (((unsigned long )pmd_frag & ~PAGE_MASK ) ==0 ) - pmd_frag =NULL ; - mm->context.pmd_frag = pmd_frag; - } -spin_unlock (&mm->page_table_lock); -return (pmd_t *)ret; -} - -static pmd_t *__alloc_for_pmdcache (struct mm_struct *mm) -{ -void *ret =NULL ; -struct page *page; -gfp_t gfp =GFP_KERNEL_ACCOUNT | __GFP_ZERO; - -if (mm == &init_mm) - gfp &= ~__GFP_ACCOUNT; - page =alloc_page (gfp); -if (!page) -return NULL ; -if (!pgtable_pmd_page_ctor (page)) { -__free_pages (page,0 ); -return NULL ; - } - -atomic_set (&page->pt_frag_refcount,1 ); - - ret =page_address (page); -/* -* if we support only one fragment just return the -* allocated page. -*/ -if (PMD_FRAG_NR ==1 ) -return ret; - -spin_lock (&mm->page_table_lock); -/* -* If we find pgtable_page set, we return -* the allocated page with single fragment -* count. -*/ -if (likely (!mm->context.pmd_frag)) { -atomic_set (&page->pt_frag_refcount,PMD_FRAG_NR ); - mm->context.pmd_frag = ret +PMD_FRAG_SIZE ; - } -spin_unlock (&mm->page_table_lock); - -return (pmd_t *)ret; -} - -pmd_t *pmd_fragment_alloc (struct mm_struct *mm,unsigned long vmaddr) -{ -pmd_t *pmd; - - pmd =get_pmd_from_cache (mm); -if (pmd) -return pmd; - -return __alloc_for_pmdcache (mm); -} - -void pmd_fragment_free (unsigned long *pmd) -{ -struct page *page =virt_to_page (pmd); - -if (PageReserved (page)) -return free_reserved_page (page); - -BUG_ON (atomic_read (&page->pt_frag_refcount) <=0 ); -if (atomic_dec_and_test (&page->pt_frag_refcount)) { -pgtable_pmd_page_dtor (page); -__free_page (page); - } -} -static inline void pgtable_free (void *table,int index) {switch (index) {
arch/powerpc/mm/init-common.c +4 -0
@@ -150,6 +150,8 @@ void pgtable_cache_add(unsigned int shift) }EXPORT_SYMBOL_GPL (pgtable_cache_add);/* used by kvm_hv module */ +void pt_frag_init (void ); +void pgtable_cache_init (void ) {pgtable_cache_add (PGD_INDEX_SIZE );
@@ -163,4 +165,6 @@ void pgtable_cache_init(void)*/ if (PUD_CACHE_INDEX )pgtable_cache_add (PUD_CACHE_INDEX ); + +pt_frag_init (); }
arch/powerpc/mm/pgtable-frag.c +276 -67
384 lines not on this page · raw message
include/linux/mm.h +9 -1
@@ -2286,6 +2286,10 @@ static inline spinlock_t *ptlock_ptr(struct page *page) }#endif /* ALLOC_SPLIT_PTLOCKS */ +spinlock_t *pte_lockptr (struct mm_struct *mm,pmd_t *pmd); +bool ptlock_init (struct page *page); + +#if 0 static inline spinlock_t *pte_lockptr (struct mm_struct *mm,pmd_t *pmd) {return ptlock_ptr (pmd_page (*pmd));
@@ -2306,6 +2310,7 @@ static inline bool ptlock_init(struct page *page)spin_lock_init (ptlock_ptr (page)); return true; } +#endif #else /* !USE_SPLIT_PTE_PTLOCKS */ /*
@@ -2371,16 +2376,19 @@ static inline void pgtable_pte_page_dtor(struct page *page)#if USE_SPLIT_PMD_PTLOCKS -static struct page *pmd_to_page (pmd_t *pmd) +static inline struct page *pmd_to_page (pmd_t *pmd) {unsigned long mask = ~(PTRS_PER_PMD *sizeof (pmd_t) -1 );return virt_to_page ((void *)((unsigned long ) pmd & mask)); } +#if 0 static inline spinlock_t *pmd_lockptr (struct mm_struct *mm,pmd_t *pmd) {return ptlock_ptr (pmd_to_page (pmd)); } +#endif +spinlock_t *pmd_lockptr (struct mm_struct *mm,pmd_t *pmd);static inline bool pmd_ptlock_init (struct page *page) {
include/linux/mm_types.h +1 -0
@@ -152,6 +152,7 @@ struct page {unsigned long _pt_pad_2;/* mapping */ union {struct mm_struct *pt_mm;/* x86 pgds only */ +void *pt_frag;/* powerpc */ atomic_t pt_frag_refcount;/* powerpc */ };#if ALLOC_SPLIT_PTLOCKS
Le 05/09/2022 à 10:50, Nicholas Piggin a écrit :
Quoted 17 lines
The page table fragment allocator is a simple per-mm slab allocator. It can be quite wasteful of space for small processes, as well as being expensive to initialise. It does not do well at NUMA awareness.
This is a quick hack at addressing some of those problems, but it's not complete. It doesn't support THP because it doesn't deal with the page table deposit. It has has certain cases where cross-CPU locking could be increased (but also a reduction in other cases including reduction on ptl). NUMA still has some corner case issues, but it is improved. So it's not mergeable yet or necessarily the best way to solve the problems. Just a quick hack for some testing.
It save 1-2MB on a simple distro boot on a small (4 CPU) system. The powerpc fork selftests benchmark with --fork performance is improved by 15% on a POWER9 (14.5k/s -> 17k/s). This is just about a worst-case microbenchmark, but would still be good to fix it.
Quoted 5 lines
What would really be nice is if we could avoid writing our own allocator and use the slab allocator. The problem being we need a page table lock spinlock associated with the page table, and that must be able to be derived from the page table pointer, and I don't think slab has anything that fits the bill.
I have not looked at it in details yet, but I have the feeling that the handling of single-fragment architectures have disappeared.
That's commit 2a146533bf96 ("powerpc/mm: Avoid useless lock with single page fragments").
Thanks to that optimisation, all platforms were converted to page fragments with:
- commit 32ea4c149990 ("powerpc/mm: Extend pte_fragment functionality to PPC32")
- commit 737b434d3d55 ("powerpc/mm: convert Book3E 64 to pte_fragment")
But if the optimisation is removed then I guess the cost will likely be higher than before.
Christophe
Nicholas Piggin
reviewerQuoted 20 lines · Christophe Leroy
Le 05/09/2022 à 10:50, Nicholas Piggin a écrit :
Quoted 17 lines
The page table fragment allocator is a simple per-mm slab allocator. It can be quite wasteful of space for small processes, as well as being expensive to initialise. It does not do well at NUMA awareness.
This is a quick hack at addressing some of those problems, but it's not complete. It doesn't support THP because it doesn't deal with the page table deposit. It has has certain cases where cross-CPU locking could be increased (but also a reduction in other cases including reduction on ptl). NUMA still has some corner case issues, but it is improved. So it's not mergeable yet or necessarily the best way to solve the problems. Just a quick hack for some testing.
It save 1-2MB on a simple distro boot on a small (4 CPU) system. The powerpc fork selftests benchmark with --fork performance is improved by 15% on a POWER9 (14.5k/s -> 17k/s). This is just about a worst-case microbenchmark, but would still be good to fix it.
Quoted 8 lines
Quoted 5 lines
What would really be nice is if we could avoid writing our own allocator and use the slab allocator. The problem being we need a page table lock spinlock associated with the page table, and that must be able to be derived from the page table pointer, and I don't think slab has anything that fits the bill.
I have not looked at it in details yet, but I have the feeling that the handling of single-fragment architectures have disappeared.
Yes that's gone from my hack, it should be special-cased of course to reduce or avoid unnecessary overhead.
Thanks,
Nick
Quoted 15 lines
That's commit
2a146533bf96("powerpc/mm: Avoid useless lock with single page fragments").Thanks to that optimisation, all platforms were converted to page fragments with:
- commit32ea4c149990("powerpc/mm: Extendpte_fragmentfunctionality to PPC32")
- commit737b434d3d55("powerpc/mm: convert Book3E 64 topte_fragment")But if the optimisation is removed then I guess the cost will likely be higher than before.
Christophe