mm: hugetlb: allocate frozen gigantic folio
Subsystems (5)
v3 cover mm: hugetlb: allocate frozen gigantic folio
Introduce alloc_contig_frozen_pages() and cma_alloc_frozen_compound() which avoid atomic operation about page refcount, and then convert to allocate frozen gigantic folio by the new helpers in hugetlb to cleanup the alloc_gigantic_folio().
v3:
- Fix built warn/err, found by lkp test
- Address some David's comments,
- Force on frozen part and drop the optimization part
- Rename split_non_compound_pages() to __split_pages()
- Adding back debug print/WARN_ON if no cma range found or the pfn range of page is not full match the cma range.
v2:
- Optimize gigantic folio allocation speed
- Using HPAGE_PUD_ORDER in debug_vm_pgtable
- Address some David's comments,
- kill folio_alloc_gigantic()
- add generic cma_alloc_frozen{_compound}() instead of cma_{alloc,free}_folio
Kefeng Wang (6):
mm: debug_vm_pgtable: add debug_vm_pgtable_free_huge_page()
mm: page_alloc: add __split_page()
mm: page_alloc: add alloc_contig_{range_frozen,frozen_pages}()
mm: cma: add __cma_release()
mm: cma: add cma_alloc_frozen{_compound}()
mm: hugetlb: allocate frozen pages in alloc_gigantic_folio()
include/linux/cma.h | 26 ++---- include/linux/gfp.h | 52 +++++------ mm/cma.c | 109 +++++++++++----------- mm/debug_vm_pgtable.c | 38 ++++---- mm/hugetlb.c | 58 +++--------- mm/hugetlb_cma.c | 27 +++--- mm/hugetlb_cma.h | 10 +-- mm/internal.h | 6 ++ mm/page_alloc.c | 204 ++++++++++++++++++++++++++++-------------- 9 files changed, 271 insertions(+), 259 deletions(-)
Signature · 1 line
2.27.0
PATCH v3 1/6 mm: debug_vm_pgtable: add debug_vm_pgtable_free_huge_page()
Add a new helper to free huge page to be consistency to debug_vm_pgtable_alloc_huge_page(), and use HPAGE_PUD_ORDER instead of open-code.
Also move the free_contig_range() under CONFIG_ALLOC_CONTIG since all caller are built with CONFIG_ALLOC_CONTIG.
--- include/linux/gfp.h | 2 +- mm/debug_vm_pgtable.c | 38 +++++++++++++++++--------------------- mm/page_alloc.c | 2 +- 3 files changed, 19 insertions(+), 23 deletions(-)
include/linux/gfp.h +1 -1
@@ -437,8 +437,8 @@ extern struct page *alloc_contig_pages_noprof(unsigned long nr_pages, gfp_t gfp_ int nid, nodemask_t *nodemask);#define alloc_contig_pages (...) alloc_hooks(alloc_contig_pages_noprof(__VA_ARGS__)) -#endif void free_contig_range (unsigned long pfn,unsigned long nr_pages); +#endif #ifdef CONFIG_CONTIG_ALLOC static inline struct folio *folio_alloc_gigantic_noprof(int order, gfp_t gfp,
mm/debug_vm_pgtable.c +17 -21
@@ -946,22 +946,26 @@ static unsigned long __init get_random_vaddr(void)return random_vaddr; } -static void __init destroy_args (struct pgtable_debug_args *args) +static void __init +debug_vm_pgtable_free_huge_page (struct pgtable_debug_args *args, +unsigned long pfn,int order) { -struct page *page =NULL ; +#ifdef CONFIG_CONTIG_ALLOC +if (args->is_contiguous_page) { +free_contig_range (pfn,1 << order); +return ; + } +#endif +__free_pages (pfn_to_page (pfn), order); +} +static void __init destroy_args (struct pgtable_debug_args *args) +{/* Free (huge) page */ if (IS_ENABLED (CONFIG_TRANSPARENT_HUGEPAGE ) &&has_transparent_pud_hugepage () && args->pud_pfn !=ULONG_MAX ) { -if (args->is_contiguous_page) { -free_contig_range (args->pud_pfn, - (1 << (HPAGE_PUD_SHIFT -PAGE_SHIFT ))); - }else { - page =pfn_to_page (args->pud_pfn); -__free_pages (page,HPAGE_PUD_SHIFT -PAGE_SHIFT ); - } - +debug_vm_pgtable_free_huge_page (args, args->pud_pfn,HPAGE_PUD_ORDER ); args->pud_pfn =ULONG_MAX ; args->pmd_pfn =ULONG_MAX ; args->pte_pfn =ULONG_MAX ;
@@ -970,20 +974,13 @@ static void __init destroy_args(struct pgtable_debug_args *args)if (IS_ENABLED (CONFIG_TRANSPARENT_HUGEPAGE ) &&has_transparent_hugepage () && args->pmd_pfn !=ULONG_MAX ) { -if (args->is_contiguous_page) { -free_contig_range (args->pmd_pfn, (1 <<HPAGE_PMD_ORDER )); - }else { - page =pfn_to_page (args->pmd_pfn); -__free_pages (page,HPAGE_PMD_ORDER ); - } - +debug_vm_pgtable_free_huge_page (args, args->pmd_pfn,HPAGE_PMD_ORDER ); args->pmd_pfn =ULONG_MAX ; args->pte_pfn =ULONG_MAX ; }if (args->pte_pfn !=ULONG_MAX ) { - page =pfn_to_page (args->pte_pfn); -__free_page (page); +__free_page (pfn_to_page (args->pte_pfn)); args->pte_pfn =ULONG_MAX ; }
@@ -1215,8 +1212,7 @@ static int __init init_args(struct pgtable_debug_args *args)*/ if (IS_ENABLED (CONFIG_TRANSPARENT_HUGEPAGE ) &&has_transparent_pud_hugepage ()) { - page =debug_vm_pgtable_alloc_huge_page (args, -HPAGE_PUD_SHIFT -PAGE_SHIFT ); + page =debug_vm_pgtable_alloc_huge_page (args,HPAGE_PUD_ORDER );if (page) { args->pud_pfn =page_to_pfn (page); args->pmd_pfn = args->pud_pfn;
mm/page_alloc.c +1 -1
@@ -7121,7 +7121,6 @@ struct page *alloc_contig_pages_noprof(unsigned long nr_pages, gfp_t gfp_mask, }return NULL ; } -#endif /* CONFIG_CONTIG_ALLOC */ void free_contig_range (unsigned long pfn,unsigned long nr_pages) {
@@ -7148,6 +7147,7 @@ void free_contig_range(unsigned long pfn, unsigned long nr_pages) WARN(count != 0, "%lu pages are still in use!\n", count); }EXPORT_SYMBOL (free_contig_range); +#endif /* CONFIG_CONTIG_ALLOC */ /* * Effectively disable pcplists for the zone by setting the high limit to 0
Signature · 1 line
2.27.0
PATCH v3 2/6 mm: page_alloc: add __split_page()
In reply to Kefeng WangFactor out the splitting of non-compound page from make_alloc_exact() and split_page() into a new helper function __split_page().
--- mm/page_alloc.c | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-)
mm/page_alloc.c +12 -7
@@ -3042,6 +3042,15 @@ void free_unref_folios(struct folio_batch *folios)folio_batch_reinit (folios); } +static void __split_page (struct page *page,unsigned int order) +{ +VM_BUG_ON_PAGE (PageCompound (page), page); + +split_page_owner (page, order,0 ); +pgalloc_tag_split (page_folio (page), order,0 ); +split_page_memcg (page, order); +} +/* * split_page takes a non-compound higher-order page, and splits it into * n (1<<order) sub-pages: page[0..n]
@@ -3054,14 +3063,12 @@ void split_page(struct page *page, unsigned int order) {int i; -VM_BUG_ON_PAGE (PageCompound (page), page);VM_BUG_ON_PAGE (!page_count (page), page);for (i =1 ; i < (1 << order); i++)set_page_refcounted (page + i); -split_page_owner (page, order,0 ); -pgalloc_tag_split (page_folio (page), order,0 ); -split_page_memcg (page, order); + +__split_page (page, order); }EXPORT_SYMBOL_GPL (split_page);
@@ -5339,9 +5346,7 @@ static void *make_alloc_exact(unsigned long addr, unsigned int order,struct page *page =virt_to_page ((void *)addr);struct page *last = page + nr; -split_page_owner (page, order,0 ); -pgalloc_tag_split (page_folio (page), order,0 ); -split_page_memcg (page, order); +__split_page (page, order);while (page < --last)set_page_refcounted (last);
Signature · 1 line
2.27.0
Re: [PATCH v3 2/6] mm: page_alloc: add __split_page()
Quoted 13 lines · Kefeng Wang
Factor out the splitting of non-compound page from
make_alloc_exact()andsplit_page()into a new helper function__split_page().Signed-off-by Kefeng Wang <[email protected]>--- mm/page_alloc.c | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-)mm/page_alloc.c +0
-0
mm/page_alloc.c void free_unref_folios(struct folio_batch *folios) · line 3047
Quoted 6 lines
mm/page_alloc.c +3
-0@@ -3042,6 +3042,15 @@ void free_unref_folios(struct folio_batch *folios)folio_batch_reinit (folios); } +static void __split_page (struct page *page,unsigned int order) +{ +VM_BUG_ON_PAGE (PageCompound (page), page);
While at it, make that a WM_WARN_ON_ONCE(). Unfortunately there is no _PAGE() variant, maybe there should be one.
With that
Signature · 3 lines
Cheers David / dhildenb
Re: [PATCH v3 2/6] mm: page_alloc: add __split_page()
Quoted 9 lines · David Hildenbrand
Quoted 8 lines · Kefeng Wang
Factor out the splitting of non-compound page from
make_alloc_exact()andsplit_page()into a new helper function__split_page().Signed-off-by Kefeng Wang <[email protected]>---
mm/page_alloc.c| 19 ++++++++++++-------1 file changed, 12 insertions(+), 7 deletions(-)
Quoted 12 lines
mm/page_alloc.c void free_unref_folios(struct folio_batch *folios) · line 3046
Quoted 10 lines
mm/page_alloc.c +3
-0@@ -3042,6 +3042,15 @@ void free_unref_folios(struct folio_batch *folios) folio_batch_reinit(folios); } +static void __split_page (struct page *page,unsigned int order) +{ + VM_BUG_ON_PAGE(PageCompound(page), page);While at it, make that a
WM_WARN_ON_ONCE(). Unfortunately there is no_PAGE()variant, maybe there should be one.
OK,it's strange, we have VM_WARN_ON_FOLIO, but not _PAGE(), I will add a new and use it.
Quoted 5 lines
With that
Thanks
PATCH v3 3/6 mm: page_alloc: add alloc_contig_{range_frozen,frozen_pages}()
In reply to Kefeng WangIn order to allocate given range of pages or allocate compound pages without incrementing their refcount, adding two new helper alloc_contig_{range_frozen,frozen_pages}() which may be beneficial to some users (eg hugetlb), also free_contig_range_frozen() is provided to match alloc_contig_range_frozen(), but it is better to use free_frozen_pages() to free frozen compound pages.
--- include/linux/gfp.h | 29 +++++-- mm/page_alloc.c | 183 +++++++++++++++++++++++++++++--------------- 2 files changed, 143 insertions(+), 69 deletions(-)
include/linux/gfp.h +21 -8
@@ -429,14 +429,27 @@ typedef unsigned int __bitwise acr_flags_t;#define ACR_FLAGS_CMA ((__force acr_flags_t)BIT(0)) // allocate for CMA/* The below functions must be run on a range from a single zone. */ -extern int alloc_contig_range_noprof (unsigned long start,unsigned long end, -acr_flags_t alloc_flags,gfp_t gfp_mask); -#define alloc_contig_range (...) alloc_hooks(alloc_contig_range_noprof(__VA_ARGS__)) - -extern struct page *alloc_contig_pages_noprof (unsigned long nr_pages,gfp_t gfp_mask, -int nid,nodemask_t *nodemask); -#define alloc_contig_pages (...) alloc_hooks(alloc_contig_pages_noprof(__VA_ARGS__)) - +int alloc_contig_range_frozen_noprof (unsigned long start,unsigned long end, +acr_flags_t alloc_flags,gfp_t gfp_mask); +#define alloc_contig_range_frozen (...) \ + alloc_hooks(alloc_contig_range_frozen_noprof(__VA_ARGS__)) + +int alloc_contig_range_noprof (unsigned long start,unsigned long end, +acr_flags_t alloc_flags,gfp_t gfp_mask); +#define alloc_contig_range (...) \ + alloc_hooks(alloc_contig_range_noprof(__VA_ARGS__)) + +struct page *alloc_contig_frozen_pages_noprof (unsigned long nr_pages, +gfp_t gfp_mask,int nid,nodemask_t *nodemask); +#define alloc_contig_frozen_pages (...) \ + alloc_hooks(alloc_contig_frozen_pages_noprof(__VA_ARGS__)) + +struct page *alloc_contig_pages_noprof (unsigned long nr_pages,gfp_t gfp_mask, +int nid,nodemask_t *nodemask); +#define alloc_contig_pages (...) \ + alloc_hooks(alloc_contig_pages_noprof(__VA_ARGS__)) + +void free_contig_range_frozen (unsigned long pfn,unsigned long nr_pages);void free_contig_range (unsigned long pfn,unsigned long nr_pages);#endif
mm/page_alloc.c +122 -61
247 lines not on this page · raw message
Signature · 1 line
2.27.0
Re: [PATCH v3 3/6] mm: page_alloc: add alloc_contig_{range_frozen,frozen_pages}()
Quoted 21 lines · Kefeng Wang
In order to allocate given range of pages or allocate compound pages without incrementing their refcount, adding two new helper alloc_contig_{range_frozen,frozen_pages}() which may be beneficial to some users (eg hugetlb), also
free_contig_range_frozen()is provided to matchalloc_contig_range_frozen(), but it is better to usefree_frozen_pages()to free frozen compound pages.Signed-off-by Kefeng Wang <[email protected]>--- include/linux/gfp.h | 29 +++++-- mm/page_alloc.c | 183 +++++++++++++++++++++++++++++--------------- 2 files changed, 143 insertions(+), 69 deletions(-)include/linux/gfp.h +0
-0@@ -429,14 +429,27 @@ typedef unsigned int __bitwise acr_flags_t;#define ACR_FLAGS_CMA ((__force acr_flags_t)BIT(0)) // allocate for CMA/* The below functions must be run on a range from a single zone. */
include/linux/gfp.h typedef unsigned int __bitwise acr_flags_t; · line 433
Quoted 10 lines
include/linux/gfp.h +2
-8-extern int alloc_contig_range_noprof (unsigned long start,unsigned long end, -acr_flags_t alloc_flags,gfp_t gfp_mask); -#define alloc_contig_range (...) alloc_hooks(alloc_contig_range_noprof(__VA_ARGS__)) - -extern struct page *alloc_contig_pages_noprof (unsigned long nr_pages,gfp_t gfp_mask, -int nid,nodemask_t *nodemask); -#define alloc_contig_pages (...) alloc_hooks(alloc_contig_pages_noprof(__VA_ARGS__)) - +int alloc_contig_range_frozen_noprof (unsigned long start,unsigned long end, +acr_flags_t alloc_flags,gfp_t gfp_mask);
Just wondering: given alloc_contig_pages() vs. alloc_contig_frozen_pages_()
Shouldn't it be alloc_contig_range() vs. alloc_contig_frozen_range()
And then free_contig_frozen_range()?
Do we want kerneldoc here as well?
Quoted 64 lines
include/linux/gfp.h +31
-8+int alloc_contig_range_frozen_noprof (unsigned long start,unsigned long end, +acr_flags_t alloc_flags,gfp_t gfp_mask) {const unsigned int order =ilog2 (end - start);unsigned long outer_start, outer_end;@@ -7003,19 +6982,18 @@ int alloc_contig_range_noprof(unsigned long start, unsigned long end, }if (!(gfp_mask & __GFP_COMP)) { -split_free_pages (cc.freepages, gfp_mask); +split_free_frozen_pages (cc.freepages, gfp_mask);/* Free head and tail (if any) */ if (start != outer_start) -free_contig_range (outer_start, start - outer_start); +free_contig_range_frozen (outer_start, start - outer_start);if (end != outer_end) -free_contig_range (end, outer_end - end); +free_contig_range_frozen (end, outer_end - end); }else if (start == outer_start && end == outer_end &&is_power_of_2 (end - start)) {struct page *head =pfn_to_page (start);check_new_pages (head, order);prep_new_page (head, order, gfp_mask,0 ); -set_page_refcounted (head); }else { ret = -EINVAL ; WARN(true, "PFN range: requested [%lu, %lu), allocated [%lu, %lu)\n",@@ -7025,16 +7003,48 @@ int alloc_contig_range_noprof(unsigned long start, unsigned long end,undo_isolate_page_range (start, end);return ret; } -EXPORT_SYMBOL (alloc_contig_range_noprof); -static int __alloc_contig_pages (unsigned long start_pfn, -unsigned long nr_pages,gfp_t gfp_mask) +/** +* alloc_contig_range() -- tries to allocate given range of pages +* @start: start PFN to allocate +* @end: one-past-the-last PFN to allocate +* @alloc_flags: allocation information +* @gfp_mask: GFP mask. Node/zone/placement hints are ignored; only some +* action and reclaim modifiers are supported. Reclaim modifiers +* control allocation behavior during compaction/migration/reclaim. +* +* The PFN range does not have to be pageblock aligned. The PFN range must +* belong to a single zone. +* +* The first thing this routine does is attempt to MIGRATE_ISOLATE all +* pageblocks in the range. Once isolated, the pageblocks should not +* be modified by others. +* +* Return: zero on success or negative error code. On success all +* pages which PFN is in [start, end) are allocated for the caller and +* need to be freed with free_contig_range(). +*/ +int alloc_contig_range_noprof (unsigned long start,unsigned long end, +acr_flags_t alloc_flags,gfp_t gfp_mask) { -unsigned long end_pfn = start_pfn + nr_pages; +int ret; + + ret =alloc_contig_range_frozen_noprof (start, end, alloc_flags, gfp_mask); +if (ret)
include/linux/gfp.h int alloc_contig_range_noprof(unsigned long start, unsigned long end, · line 7043
Quoted 10 lines
include/linux/gfp.h +10
-0+return ret; + +if (gfp_mask & __GFP_COMP) { +set_page_refcounted (pfn_to_page (start)); + }else { +unsigned long pfn; + +for (pfn = start; pfn < end; pfn++) +set_page_refcounted (pfn_to_page (pfn)); + }
Might read better as
unsigned long pfn;
...
if (gfp_mask & __GFP_COMP) {
set_page_refcounted(pfn_to_page(start));
return 0;
}for (pfn = start; pfn < end; pfn++)
set_page_refcounted(pfn_to_page(pfn));
return 0;
One could also do something fancy like
unsigned long pfn;
...
for (pfn = start; pfn < end; pfn++) {
set_page_refcounted(pfn_to_page(pfn));
if (gfp_mask & __GFP_COMP)
break;
}return 0:
include/linux/gfp.h static bool zone_spans_last_pfn(const struct zone *zone, · line 7078
Quoted 12 lines
include/linux/gfp.h +2
-2-return alloc_contig_range_noprof (start_pfn, end_pfn,ACR_FLAGS_NONE , - gfp_mask); +return 0 ; } +EXPORT_SYMBOL (alloc_contig_range_noprof);static bool pfn_range_valid_contig (struct zone *z,unsigned long start_pfn,unsigned long nr_pages)@@ -7067,31 +7077,8 @@ static bool zone_spans_last_pfn(const struct zone *zone,return zone_spans_pfn (zone, last_pfn); }
... kerneldoc? :)
Quoted 17 lines
include/linux/gfp.h +5
-1+struct page *alloc_contig_frozen_pages_noprof (unsigned long nr_pages, +gfp_t gfp_mask,int nid,nodemask_t *nodemask) {unsigned long ret, pfn, flags;struct zonelist *zonelist;@@ -7114,7 +7101,9 @@ struct page *alloc_contig_pages_noprof(unsigned long nr_pages, gfp_t gfp_mask,* and cause alloc_contig_range() to fail... */ spin_unlock_irqrestore (&zone->lock, flags); - ret =__alloc_contig_pages (pfn, nr_pages, + ret =alloc_contig_range_frozen_noprof (pfn, + pfn + nr_pages, +ACR_FLAGS_NONE , gfp_mask);if (!ret)return pfn_to_page (pfn);
include/linux/gfp.h struct page *alloc_contig_pages_noprof(unsigned long nr_pages, gfp_t gfp_mask, · line 7119
Quoted 5 lines
include/linux/gfp.h +2
-0@@ -7126,6 +7115,78 @@ struct page *alloc_contig_pages_noprof(unsigned long nr_pages, gfp_t gfp_mask, }return NULL ; } +EXPORT_SYMBOL (alloc_contig_range_frozen_noprof); +
kerneldoc? :)
Quoted 13 lines
include/linux/gfp.h +13
-0+void free_contig_range_frozen (unsigned long pfn,unsigned long nr_pages) +{ +struct folio *folio =pfn_folio (pfn); + +if (folio_test_large (folio)) { +int expected =folio_nr_pages (folio); + +WARN_ON (folio_ref_count (folio)); + +if (nr_pages == expected) +free_frozen_pages (&folio->page,folio_order (folio)); +else + WARN(true, "PFN %lu: nr_pages %lu != expected %d\n",
include/linux/gfp.h struct page *alloc_contig_pages_noprof(unsigned long nr_pages, gfp_t gfp_mask, · line 7142
Quoted 10 lines
include/linux/gfp.h +10
-0+ pfn, nr_pages, expected); +return ; + } + +for (; nr_pages--; pfn++) { +struct page *page =pfn_to_page (pfn); + +WARN_ON (page_ref_count (page)); +free_frozen_pages (page,0 ); + }
That's mostly a copy-and-paste of free_contig_range().
I wonder if there is some way to avoid duplicating a lot of free_contig_range() here. Hmmm.
Also, the folio stuff in there looks a bit weird I'm afraid.
Can't we just refuse to free compound pages throught this interface and free_contig_range() ? IIRC only hugetlb uses it and uses folio_put() either way?
Then we can just document that compound allocations are to be freed differently.
And do something like
mm/page_alloc.c +13 -17
@@ -7123,29 +7123,25 @@ struct page *alloc_contig_pages_noprof(unsigned long nr_pages, gfp_t gfp_mask, }#endif /* CONFIG_CONTIG_ALLOC */ -void free_contig_range (unsigned long pfn,unsigned long nr_pages) +static inline void __free_contig_range (unsigned long pfn,unsigned long nr_pages,bool put_ref) { -unsigned long count =0 ; -struct folio *folio =pfn_folio (pfn); - -if (folio_test_large (folio)) { -int expected =folio_nr_pages (folio); - -if (nr_pages == expected) -folio_put (folio); -else -WARN (true,"PFN %lu: nr_pages %lu != expected %d\n" , - pfn, nr_pages, expected); -return ; - } +if (WARN_ON_ONCE (PageHead (pfn_to_page (pfn)))) +break ;for (; nr_pages--; pfn++) {struct page *page =pfn_to_page (pfn); - count +=page_count (page) !=1 ; -__free_page (page); +if (put_ref) +page_ref_dec (page); +if (WARN_ON_ONCE (page_count (page))) +continue ; +free_frozen_pages (page,0 ); } -WARN (count !=0 ,"%lu pages are still in use!\n" , count); +} + +void free_contig_range (unsigned long pfn,unsigned long nr_pages) +{ +return __free_contig_range (pfn, nr_pages,/* put_ref= */ true); }EXPORT_SYMBOL (free_contig_range);
Just a thought, I dislike current free_contig_range() and the duplicated variant.
Quoted 36 lines
include/linux/gfp.h +36
-0+} +EXPORT_SYMBOL (free_contig_range_frozen); + +/** +* alloc_contig_pages() -- tries to find and allocate contiguous range of pages +* @nr_pages: Number of contiguous pages to allocate +* @gfp_mask: GFP mask. Node/zone/placement hints limit the search; only some +* action and reclaim modifiers are supported. Reclaim modifiers +* control allocation behavior during compaction/migration/reclaim. +* @nid: Target node +* @nodemask: Mask for other possible nodes +* +* This routine is a wrapper around alloc_contig_range(). It scans over zones +* on an applicable zonelist to find a contiguous pfn range which can then be +* tried for allocation with alloc_contig_range(). This routine is intended +* for allocation requests which can not be fulfilled with the buddy allocator. +* +* The allocated memory is always aligned to a page boundary. If nr_pages is a +* power of two, then allocated range is also guaranteed to be aligned to same +* nr_pages (e.g. 1GB request would be aligned to 1GB). +* +* Allocated pages can be freed with free_contig_range() or by manually calling +* __free_page() on each allocated page. +* +* Return: pointer to contiguous pages on success, or NULL if not successful. +*/ +struct page *alloc_contig_pages_noprof (unsigned long nr_pages,gfp_t gfp_mask, +int nid,nodemask_t *nodemask) +{ +struct page *page; + + page =alloc_contig_frozen_pages_noprof (nr_pages, gfp_mask, nid, + nodemask); +if (!page) +return NULL ; +
include/linux/gfp.h struct page *alloc_contig_pages_noprof(unsigned long nr_pages, gfp_t gfp_mask, · line 7188
Quoted 10 lines
include/linux/gfp.h +10
-0+if (gfp_mask & __GFP_COMP) { +set_page_refcounted (page); + }else { +unsigned long pfn =page_to_pfn (page); + +for (; nr_pages--; pfn++) +set_page_refcounted (pfn_to_page (pfn)); + } + +return page;
Same here, might be able to make it easier to read like I suggested for the alloc_contig_range_noprof().
Or that part can just be factored out?
void set_pages_refcounted(struct page *page, unsigned long nr_pages, gfp_t gfp_mask)
or better
void set_pages_refcounted(struct page *page, unsigned long nr_pages)
And deriving __GFP_COMP from PageHead().
Signature · 3 lines
Cheers David / dhildenb
Re: [PATCH v3 3/6] mm: page_alloc: add alloc_contig_{range_frozen,frozen_pages}()
Quoted 39 lines · David Hildenbrand
Quoted 33 lines · Kefeng Wang
In order to allocate given range of pages or allocate compound pages without incrementing their refcount, adding two new helper alloc_contig_{range_frozen,frozen_pages}() which may be beneficial to some users (eg hugetlb), also
free_contig_range_frozen()is provided to matchalloc_contig_range_frozen(), but it is better to usefree_frozen_pages()to free frozen compound pages.Signed-off-by Kefeng Wang <[email protected]>---
include/linux/gfp.h| 29 +++++--
mm/page_alloc.c | 183 +++++++++++++++++++++++++++++---------------2 files changed, 143 insertions(+), 69 deletions(-)include/linux/gfp.h +1
-8@@ -429,14 +429,27 @@ typedef unsigned int __bitwise acr_flags_t; #define ACR_FLAGS_CMA ((__force acr_flags_t)BIT(0)) // allocate for CMA /* The below functions must be run on a range from a single zone. */ -extern int alloc_contig_range_noprof (unsigned long start,unsigned long end, - acr_flags_t alloc_flags, gfp_t gfp_mask); -#define alloc_contig_range (...) alloc_hooks(alloc_contig_range_noprof(__VA_ARGS__)) - -extern struct page *alloc_contig_pages_noprof (unsigned long nr_pages, gfp_t gfp_mask, - int nid, nodemask_t *nodemask); -#define alloc_contig_pages (...) alloc_hooks(alloc_contig_pages_noprof(__VA_ARGS__)) - +int alloc_contig_range_frozen_noprof(unsigned long start, unsignedQuoted 2 lines
long end,
+acr_flags_talloc_flags,gfp_tgfp_mask);Just wondering: given
alloc_contig_pages()vs.alloc_contig_frozen_pages_()
Quoted 4 lines
Shouldn't it be
alloc_contig_range()vs.alloc_contig_frozen_range()And then
free_contig_frozen_range()?
Sure, it's better to align them, not thought much about the naming.
Quoted 2 lines
Do we want kerneldoc here as well?
OK, will add a short kerneldoc for this one.
Quoted 85 lines
Quoted 69 lines
+int alloc_contig_range_frozen_noprof(unsigned long start, unsigned long end,
+acr_flags_talloc_flags,gfp_tgfp_mask)
{
const unsigned int order = ilog2(end - start); unsigned longouter_start,outer_end;include/linux/gfp.h +9
-7@@ -7003,19 +6982,18 @@ int alloc_contig_range_noprof(unsigned long start, unsigned long end, } if (!(gfp_mask & __GFP_COMP)) { - split_free_pages(cc.freepages, gfp_mask); + split_free_frozen_pages(cc.freepages, gfp_mask); /* Free head and tail (if any) */ if (start != outer_start) - free_contig_range(outer_start, start - outer_start); + free_contig_range_frozen(outer_start, start - outer_start); if (end != outer_end) - free_contig_range(end, outer_end - end); + free_contig_range_frozen(end, outer_end - end); } else if (start == outer_start && end == outer_end && is_power_of_2(end - start)) { struct page *head = pfn_to_page(start); check_new_pages(head, order); prep_new_page(head, order, gfp_mask, 0); - set_page_refcounted(head); } else { ret = -EINVAL; WARN(true, "PFN range: requested [%lu, %lu), allocated [%lu, %lu)\n",@@ -7025,16 +7003,48 @@ int alloc_contig_range_noprof(unsigned long start, unsigned long end, undo_isolate_page_range(start, end); return ret; } -EXPORT_SYMBOL (alloc_contig_range_noprof); -static int __alloc_contig_pages (unsigned long start_pfn, - unsigned long nr_pages, gfp_t gfp_mask) +/** +* alloc_contig_range() -- tries to allocate given range of pages +* @start: start PFN to allocate +* @end: one-past-the-last PFN to allocate +* @alloc_flags: allocation information +* @gfp_mask: GFP mask. Node/zone/placement hints are ignored; only some
+ * action and reclaim modifiers are supported. Reclaim modifiers
+ * control allocation behavior during compaction/migration/ reclaim.
+ *
+ * The PFN range does not have to be pageblock aligned. The PFN range must
+ * belong to a single zone.
+ *
+ * The first thing this routine does is attempt toMIGRATE_ISOLATEall
+ * pageblocks in the range. Once isolated, the pageblocks should not
+ * be modified by others.
+ *
+ * Return: zero on success or negative error code. On success all
+ * pages which PFN is in [start, end) are allocated for the caller and
+ * need to be freed withfree_contig_range().
+ */
+int alloc_contig_range_noprof(unsigned long start, unsigned long end, +acr_flags_talloc_flags,gfp_tgfp_mask) {
- unsigned longend_pfn=start_pfn+nr_pages;
+ int ret;
+
+ ret =alloc_contig_range_frozen_noprof(start, end, alloc_flags, gfp_mask);
+ if (ret)
+ return ret;include/linux/gfp.h
Quoted 9 lines
include/linux/gfp.h +9
-0+ + if (gfp_mask & __GFP_COMP) { + set_page_refcounted(pfn_to_page(start)); + } else { + unsigned long pfn; + + for (pfn = start; pfn < end; pfn++) + set_page_refcounted(pfn_to_page(pfn)); + }Might read better as
unsigned long pfn;
...
Quoted 10 lines
if (
gfp_mask&__GFP_COMP) {
set_page_refcounted(pfn_to_page(start));
return 0;
}for (pfn = start; pfn < end; pfn++)
set_page_refcounted(pfn_to_page(pfn));
return 0;
I like this one, avoid gfp_mask check in the loop.
Quoted 13 lines
One could also do something fancy like
unsigned long pfn;
...for (pfn = start; pfn < end; pfn++) {
set_page_refcounted(pfn_to_page(pfn));
if (gfp_mask&__GFP_COMP)
break;
}
return 0:
Quoted 14 lines
include/linux/gfp.h static bool zone_spans_last_pfn(const struct · line 7079
Quoted 11 lines
- return
alloc_contig_range_noprof(start_pfn, end_pfn, ACR_FLAGS_NONE, - gfp_mask);
+ return 0;
}
+EXPORT_SYMBOL(alloc_contig_range_noprof);
static boolpfn_range_valid_contig(struct zone *z, unsigned long start_pfn, unsigned long nr_pages)include/linux/gfp.h +0
-0@@ -7067,31 +7077,8 @@ static bool zone_spans_last_pfn(const structzone *zone, return zone_spans_pfn(zone, last_pfn); }... kerneldoc? :)
Sure.
Quoted 57 lines
Quoted 19 lines
include/linux/gfp.h +5
-1+struct page *alloc_contig_frozen_pages_noprof (unsigned long nr_pages, + gfp_t gfp_mask, int nid, nodemask_t *nodemask) { unsigned long ret, pfn, flags; struct zonelist *zonelist;@@ -7114,7 +7101,9 @@ struct page *alloc_contig_pages_noprof(unsignedlong nr_pages, gfp_t gfp_mask, * and cause alloc_contig_range() to fail... */ spin_unlock_irqrestore(&zone->lock, flags); - ret = __alloc_contig_pages(pfn, nr_pages, + ret = alloc_contig_range_frozen_noprof(pfn, + pfn + nr_pages, + ACR_FLAGS_NONE, gfp_mask); if (!ret)return
pfn_to_page(pfn);include/linux/gfp.h struct page *alloc_contig_pages_noprof(unsigned · line 7120
Quoted 6 lines
include/linux/gfp.h +2
-0@@ -7126,6 +7115,78 @@ struct page *alloc_contig_pages_noprof(unsigned long nr_pages, gfp_t gfp_mask, } return NULL; } +EXPORT_SYMBOL (alloc_contig_range_frozen_noprof ); +kerneldoc? :)
Quoted 13 lines
include/linux/gfp.h +13
-0+void free_contig_range_frozen (unsigned long pfn,unsigned long nr_pages) +{ + struct folio *folio = pfn_folio(pfn); + + if (folio_test_large(folio)) { + int expected = folio_nr_pages(folio); + + WARN_ON(folio_ref_count(folio)); + + if (nr_pages == expected) + free_frozen_pages(&folio->page, folio_order(folio)); + else + WARN(true, "PFN %lu: nr_pages %lu != expected %d\n",include/linux/gfp.h struct page *alloc_contig_pages_noprof(unsigned · line 7143
Quoted 10 lines
include/linux/gfp.h +10
-0+ pfn, nr_pages, expected); + return; + } + + for (; nr_pages--; pfn++) { + struct page *page = pfn_to_page(pfn); + + WARN_ON(page_ref_count(page)); + free_frozen_pages(page, 0); + }That's mostly a copy-and-paste of
free_contig_range().I wonder if there is some way to avoid duplicating a lot of
free_contig_range()here. Hmmm.
Quoted 6 lines
Also, the folio stuff in there looks a bit weird I'm afraid.
Can't we just refuse to free compound pages throught this interface and
free_contig_range()? IIRC only hugetlb uses it and usesfolio_put()either way?Then we can just document that compound allocations are to be freed differently.
There is a case for cma_free_folio, which calls free_contig_range for both in cma_release(), but I will try to check whether we could avoid the folio stuff in free_contig_range().
Quoted 121 lines
And do something like
mm/page_alloc.c +13
-17@@ -7123,29 +7123,25 @@ struct page *alloc_contig_pages_noprof(unsigned long nr_pages, gfp_t gfp_mask, } #endif /* CONFIG_CONTIG_ALLOC */ -void free_contig_range(unsigned long pfn, unsigned long nr_pages) +static inline void __free_contig_range (unsigned long pfn,unsigned long nr_pages,bool put_ref) { - unsigned long count = 0; - struct folio *folio = pfn_folio(pfn); - - if (folio_test_large(folio)) { - int expected = folio_nr_pages(folio); - - if (nr_pages == expected) - folio_put(folio); - else - WARN(true, "PFN %lu: nr_pages %lu != expected %d\n", - pfn, nr_pages, expected); - return; - } + if (WARN_ON_ONCE(PageHead(pfn_to_page(pfn)))) + break; for (; nr_pages--; pfn++) { struct page *page = pfn_to_page(pfn); - count += page_count(page) != 1; - __free_page(page); + if (put_ref) + page_ref_dec(page); + if (WARN_ON_ONCE(page_count(page))) + continue; + free_frozen_pages(page, 0); } - WARN(count != 0, "%lu pages are still in use!\n", count); +} + +void free_contig_range (unsigned long pfn,unsigned long nr_pages) +{ + return __free_contig_range(pfn, nr_pages, /* put_ref= */ true);}
EXPORT_SYMBOL(free_contig_range);Just a thought, I dislike current
free_contig_range()and the duplicated variant.Quoted 48 lines
include/linux/gfp.h +5
-0+} +EXPORT_SYMBOL (free_contig_range_frozen); + +/** +* alloc_contig_pages() -- tries to find and allocate contiguous range of pages
+ * @nr_pages: Number of contiguous pages to allocate
+ * @gfp_mask: GFP mask. Node/zone/placement hints limit the search; only some
+ * action and reclaim modifiers are supported. Reclaim modifiers
+ * control allocation behavior during compaction/migration/ reclaim.
+ * @nid: Target node
+ * @nodemask: Mask for other possible nodes
+ *
+ * This routine is a wrapper aroundalloc_contig_range(). It scans over zones
+ * on an applicable zonelist to find a contiguous pfn range which can then be
+ * tried for allocation withalloc_contig_range(). This routine is intended
+ * for allocation requests which can not be fulfilled with the buddy allocator.
+ *
+ * The allocated memory is always aligned to a page boundary. Ifnr_pagesis a
+ * power of two, then allocated range is also guaranteed to be aligned to same
+ *nr_pages(e.g. 1GB request would be aligned to 1GB).
+ *
+ * Allocated pages can be freed withfree_contig_range()or by manually calling
+ *__free_page()on each allocated page.
+ *
+ * Return: pointer to contiguous pages on success, or NULL if not successful.
+ */
+struct page *alloc_contig_pages_noprof(unsigned longnr_pages,gfp_tgfp_mask,
+ int nid,nodemask_t*nodemask)include/linux/gfp.h +8
-0+{ + struct page *page; + + page = alloc_contig_frozen_pages_noprof(nr_pages, gfp_mask, nid, + nodemask); + if (!page) + return NULL; +include/linux/gfp.h
Quoted 10 lines
include/linux/gfp.h +10
-0+ if (gfp_mask & __GFP_COMP) { + set_page_refcounted(page); + } else { + unsigned long pfn = page_to_pfn(page); + + for (; nr_pages--; pfn++) + set_page_refcounted(pfn_to_page(pfn)); + } + + return page;Same here, might be able to make it easier to read like I suggested for the
alloc_contig_range_noprof().Or that part can just be factored out?
Quoted 8 lines
void
set_pages_refcounted(struct page *page, unsigned long nr_pages, gfp_t gfp_mask)or better
void
set_pages_refcounted(struct page *page, unsigned long nr_pages)And deriving
__GFP_COMPfromPageHead().
Will try to fact it out.
Thanks.
Re: [PATCH v3 3/6] mm: page_alloc: add alloc_contig_{range_frozen,frozen_pages}()
Quoted 37 lines
Quoted 37 lines
Quoted 15 lines
(quoted diff) +13
-0+void free_contig_range_frozen(unsigned long pfn, unsigned long nr_pages) +{ + struct folio *folio = pfn_folio(pfn); + + if (folio_test_large(folio)) { + int expected = folio_nr_pages(folio); + + WARN_ON(folio_ref_count(folio)); + + if (nr_pages == expected) + free_frozen_pages(&folio->page, folio_order(folio)); + else + WARN(true, "PFN %lu: nr_pages %lu != expected %d\n",Review
Quoted 10 lines
(quoted diff) +10
-0+ pfn, nr_pages, expected); + return; + } + + for (; nr_pages--; pfn++) { + struct page *page = pfn_to_page(pfn); + + WARN_ON(page_ref_count(page)); + free_frozen_pages(page, 0); + }That's mostly a copy-and-paste of
free_contig_range().I wonder if there is some way to avoid duplicating a lot of
free_contig_range()here. Hmmm.Also, the folio stuff in there looks a bit weird I'm afraid.
Can't we just refuse to free compound pages throught this interface and
free_contig_range()? IIRC only hugetlb uses it and usesfolio_put()either way?
Quoted 7 lines
Quoted 2 lines
Then we can just document that compound allocations are to be freed differently.
There is a case for
cma_free_folio, which callsfree_contig_rangefor both incma_release(), but I will try to check whether we could avoid the folio stuff infree_contig_range().
Ah, right, there is hugetlb_cma_free_folio()->cma_free_folio().
And we need that, because we have to make sure that CMA stats are updated properly.
All compound page handling in the freeing path is just nasty and not particularly future-proof regarding memdescs.
I wonder if we could just teach alloc_contig to never hand out compound pages and then let the freeing path similarly assert that there are no compound pages.
Whoever wants a compound page (currently only hugetlb?) can create that from a frozen range. Before returning the frozen range the compound page can be dissolved. That way also any memdesc can be allocated/freed by the caller later.
The only nasty thing is the handing of splitting/merging of set_page_owner/page_table_check_alloc etc. :(
As an alternative, we could only allow compound pages for frozen pages. This way, we'd force any caller to handle the allocation/freeing of the memdesc in the future manually.
Essentially, only allow GFP_COMPOUND on the frozen interface, which we would convert hugetlb to.
That means that we can simplify free_contig_range() [no need to handle compound pages]. For free_contig_frozen_range() we would skip refcount checks on that level and do something like:
void free_contig_frozen_range(unsigned long pfn, unsigned long nr_pages)
{
struct page *first_page = pfn_to_page(pfn)
const unsigned int order = ilog2(nr_pages);
if (PageHead(first_page)) {
WARN_ON_ONCE(order != compound_order(first_page));
free_frozen_pages(first_page, order);
return;
}
for (; nr_pages--; pfn++)
free_frozen_pages(pfn_to_page(pfn), 0);
}CCing Willy, I don't know yet what will be better in the future. But the folio stuff in there screams for problems.
Signature · 3 lines
Cheers David / dhildenb
Re: [PATCH v3 3/6] mm: page_alloc: add alloc_contig_{range_frozen,frozen_pages}()
+ Matthew
Quoted 78 lines · David Hildenbrand
Quoted 38 lines
Quoted 38 lines
Quoted 16 lines
+void
free_contig_range_frozen(unsigned long pfn, unsigned long nr_pages)(quoted diff) +12
-0+{ + struct folio *folio = pfn_folio(pfn); + + if (folio_test_large(folio)) { + int expected = folio_nr_pages(folio); + + WARN_ON(folio_ref_count(folio)); + + if (nr_pages == expected) + free_frozen_pages(&folio->page, folio_order(folio)); + else + WARN(true, "PFN %lu: nr_pages %lu != expected %d\n",Review
Quoted 10 lines
(quoted diff) +10
-0+ pfn, nr_pages, expected); + return; + } + + for (; nr_pages--; pfn++) { + struct page *page = pfn_to_page(pfn); + + WARN_ON(page_ref_count(page)); + free_frozen_pages(page, 0); + }That's mostly a copy-and-paste of
free_contig_range().I wonder if there is some way to avoid duplicating a lot of
free_contig_range()here. Hmmm.Also, the folio stuff in there looks a bit weird I'm afraid.
Can't we just refuse to free compound pages throught this interface and
free_contig_range()? IIRC only hugetlb uses it and usesfolio_put()either way?Quoted 7 lines
Quoted 2 lines
Then we can just document that compound allocations are to be freed differently.
There is a case for
cma_free_folio, which callsfree_contig_rangefor both incma_release(), but I will try to check whether we could avoid the folio stuff infree_contig_range().Ah, right, there is hugetlb_cma_free_folio()->cma_free_folio().
And we need that, because we have to make sure that CMA stats are updated properly.
All compound page handling in the freeing path is just nasty and not particularly future-proof regarding memdescs.
I wonder if we could just teach
alloc_contigto never hand out compound pages and then let the freeing path similarly assert that there are no compound pages.Whoever wants a compound page (currently only hugetlb?) can create that from a frozen range. Before returning the frozen range the compound page can be dissolved. That way also any memdesc can be allocated/freed by the caller later.
The only nasty thing is the handing of splitting/merging of set_page_owner/page_table_check_alloc etc. :(
As an alternative, we could only allow compound pages for frozen pages. This way, we'd force any caller to handle the allocation/freeing of the memdesc in the future manually.
Essentially, only allow
GFP_COMPOUNDon the frozen interface, which we would convert hugetlb to.That means that we can simplify
free_contig_range()[no need to handle compound pages]. Forfree_contig_frozen_range()we would skip refcount checks on that level and do something like:void
free_contig_frozen_range(unsigned long pfn, unsigned long nr_pages){
struct page*first_page=pfn_to_page(pfn)
const unsigned int order =ilog2(nr_pages);if (
PageHead(first_page)) {WARN_ON_ONCE(order != compound_order(first_page));free_frozen_pages(first_page, order); return;
}
Quoted 6 lines
for (; nr_pages--; pfn++)
free_frozen_pages(pfn_to_page(pfn), 0);
}CCing Willy, I don't know yet what will be better in the future. But the folio stuff in there screams for problems.
Sorry forget to add cc in v3, the full link[1],
Re: [PATCH v3 3/6] mm: page_alloc: add alloc_contig_{range_frozen,frozen_pages}()
Quoted 69 lines · Kefeng Wang
+ Matthew
Quoted 66 lines · David Hildenbrand
Quoted 38 lines
Quoted 38 lines
Quoted 16 lines
+void
free_contig_range_frozen(unsigned long pfn, unsigned long nr_pages)(quoted diff) +12
-0+{ + struct folio *folio = pfn_folio(pfn); + + if (folio_test_large(folio)) { + int expected = folio_nr_pages(folio); + + WARN_ON(folio_ref_count(folio)); + + if (nr_pages == expected) + free_frozen_pages(&folio->page, folio_order(folio)); + else + WARN(true, "PFN %lu: nr_pages %lu != expected %d\n",Review
Quoted 10 lines
(quoted diff) +10
-0+ pfn, nr_pages, expected); + return; + } + + for (; nr_pages--; pfn++) { + struct page *page = pfn_to_page(pfn); + + WARN_ON(page_ref_count(page)); + free_frozen_pages(page, 0); + }That's mostly a copy-and-paste of
free_contig_range().I wonder if there is some way to avoid duplicating a lot of
free_contig_range()here. Hmmm.Also, the folio stuff in there looks a bit weird I'm afraid.
Can't we just refuse to free compound pages throught this interface and
free_contig_range()? IIRC only hugetlb uses it and usesfolio_put()either way?Quoted 7 lines
Quoted 2 lines
Then we can just document that compound allocations are to be freed differently.
There is a case for
cma_free_folio, which callsfree_contig_rangefor both incma_release(), but I will try to check whether we could avoid the folio stuff infree_contig_range().Ah, right, there is hugetlb_cma_free_folio()->cma_free_folio().
And we need that, because we have to make sure that CMA stats are updated properly.
All compound page handling in the freeing path is just nasty and not particularly future-proof regarding memdescs.
I wonder if we could just teach
alloc_contigto never hand out compound pages and then let the freeing path similarly assert that there are no compound pages.Whoever wants a compound page (currently only hugetlb?) can create that from a frozen range. Before returning the frozen range the compound page can be dissolved. That way also any memdesc can be allocated/freed by the caller later.
The only nasty thing is the handing of splitting/merging of set_page_owner/page_table_check_alloc etc. :(
As an alternative, we could only allow compound pages for frozen pages. This way, we'd force any caller to handle the allocation/ freeing of the memdesc in the future manually.
Essentially, only allow
GFP_COMPOUNDon the frozen interface, which we would convert hugetlb to.That means that we can simplify
free_contig_range()[no need to handle compound pages]. Forfree_contig_frozen_range()we would skip refcount checks on that level and do something like:
I tried to only allocate/free non-compound pages in alloc_contig_{range,pages}() and free_contig_range(),
The new added alloc_contig_frozen_{range,pages} can allocate compound/ non-compound frozen pages, let's discuss in the new version[1].
[1] https://lore.kernel.org/linux-mm/[email protected]/
Quoted 25 lines
Quoted 12 lines
void
free_contig_frozen_range(unsigned long pfn, unsigned long nr_pages){
struct page*first_page=pfn_to_page(pfn)
const unsigned int order =ilog2(nr_pages);if (
PageHead(first_page)) {WARN_ON_ONCE(order != compound_order(first_page));free_frozen_pages(first_page, order); return;
}Quoted 6 lines
for (; nr_pages--; pfn++)
free_frozen_pages(pfn_to_page(pfn), 0);
}CCing Willy, I don't know yet what will be better in the future. But the folio stuff in there screams for problems.
Sorry forget to add cc in v3, the full link[1],
[1] https://lore.kernel.org/linux-mm/20251013133854.2466530-1- [email protected]/
PATCH v3 4/6 mm: cma: add __cma_release()
In reply to Kefeng WangKill cma_pages_valid() which only used in cma_release(), also cleanup code duplication between cma pages valid checking and cma memrange finding, add __cma_release() helper to prepare for the upcoming frozen page release.
--- include/linux/cma.h | 1 - mm/cma.c | 62 +++++++++++++++------------------------------ 2 files changed, 21 insertions(+), 42 deletions(-)
include/linux/cma.h +0 -1
@@ -49,7 +49,6 @@ extern int cma_init_reserved_mem(phys_addr_t base, phys_addr_t size, struct cma **res_cma);extern struct page *cma_alloc (struct cma *cma,unsigned long count,unsigned int align,bool no_warn); -extern bool cma_pages_valid (struct cma *cma,const struct page *pages,unsigned long count);extern bool cma_release (struct cma *cma,const struct page *pages,unsigned long count);extern int cma_for_each_area (int (*it)(struct cma *cma,void *data),void *data);
mm/cma.c +21 -41
98 lines not on this page · raw message
Signature · 1 line
2.27.0
Re: [PATCH v3 4/6] mm: cma: add __cma_release()
Quoted 59 lines · Kefeng Wang
Kill
cma_pages_valid()which only used incma_release(), also cleanup code duplication between cma pages valid checking and cma memrange finding, add__cma_release()helper to prepare for the upcoming frozen page release.Reviewed-by Jane Chu <[email protected]>Signed-off-by Kefeng Wang <[email protected]>--- include/linux/cma.h | 1 - mm/cma.c | 62 +++++++++++++++------------------------------ 2 files changed, 21 insertions(+), 42 deletions(-)include/linux/cma.h +0
-1@@ -49,7 +49,6 @@ extern int cma_init_reserved_mem(phys_addr_t base, phys_addr_t size, struct cma **res_cma);extern struct page *cma_alloc (struct cma *cma,unsigned long count,unsigned int align,bool no_warn); -extern bool cma_pages_valid (struct cma *cma,const struct page *pages,unsigned long count);extern bool cma_release (struct cma *cma,const struct page *pages,unsigned long count);extern int cma_for_each_area (int (*it)(struct cma *cma,void *data),void *data);mm/cma.c +7
-6@@ -942,34 +942,43 @@ struct folio *cma_alloc_folio(struct cma *cma, int order, gfp_t gfp)return page ?page_folio (page) :NULL ; } -bool cma_pages_valid (struct cma *cma,const struct page *pages, -unsigned long count) +static bool __cma_release (struct cma *cma,const struct page *pages, +unsigned long count) {unsigned long pfn, end;int r;struct cma_memrange *cmr; -bool ret; + +pr_debug ("%s(page %p, count %lu)\n" , __func__, (void *)pages, count);if (!cma || !pages || count > cma->count)return false; pfn =page_to_pfn (pages); - ret = false;for (r =0 ; r < cma->nranges; r++) { cmr = &cma->ranges[r]; end = cmr->base_pfn + cmr->count;if (pfn >= cmr->base_pfn && pfn < end) { - ret = pfn + count <= end; -break ; +if (pfn + count <= end) +break ; +
mm/cma.c struct folio *cma_alloc_folio(struct cma *cma, int order, gfp_t gfp) · line 972
Quoted 10 lines
mm/cma.c +3
-3+VM_WARN_ON_ONCE (1 ); } } -if (!ret) -pr_debug ("%s(page %p, count %lu)\n" , - __func__, (void *)pages, count); +if (r == cma->nranges) { + pr_debug("%s(no cma range match the page %p)\n",
".. matches the page range ..." ?
With that
Signature · 3 lines
Cheers David / dhildenb
Re: [PATCH v3 4/6] mm: cma: add __cma_release()
Quoted 62 lines · David Hildenbrand
Quoted 61 lines · Kefeng Wang
Kill
cma_pages_valid()which only used incma_release(), also cleanup code duplication between cma pages valid checking and cma memrange finding, add__cma_release()helper to prepare for the upcoming frozen page release.Reviewed-by Jane Chu <[email protected]>Signed-off-by Kefeng Wang <[email protected]>---
include/linux/cma.h| 1 -
mm/cma.c | 62 +++++++++++++++------------------------------2 files changed, 21 insertions(+), 42 deletions(-)include/linux/cma.h +0
-1@@ -49,7 +49,6 @@ extern int cma_init_reserved_mem(phys_addr_t base, phys_addr_t size, struct cma **res_cma); extern struct page *cma_alloc(struct cma *cma, unsigned long count, unsigned int align, bool no_warn); -extern bool cma_pages_valid (struct cma *cma,const struct page *pages, unsigned long count); extern bool cma_release(struct cma *cma, const struct page *pages, unsigned long count);extern int
cma_for_each_area(int (*it)(struct cma *cma, void *data), void *data);mm/cma.c +8
-6@@ -942,34 +942,43 @@ struct folio *cma_alloc_folio(struct cma *cma, int order, gfp_t gfp) return page ? page_folio(page) : NULL; } -bool cma_pages_valid (struct cma *cma,const struct page *pages, - unsigned long count) +static bool __cma_release (struct cma *cma,const struct page *pages, + unsigned long count) { unsigned long pfn, end; int r; struct cma_memrange *cmr; - bool ret; + + pr_debug("%s(page %p, count %lu)\n", __func__, (void *)pages, count); if (!cma || !pages || count > cma->count) return false; pfn = page_to_pfn(pages); - ret = false; for (r = 0; r < cma->nranges; r++) { cmr = &cma->ranges[r]; end = cmr->base_pfn + cmr->count; if (pfn >= cmr->base_pfn && pfn < end) { - ret = pfn + count <= end; - break; + if (pfn + count <= end) + break; + + VM_WARN_ON_ONCE(1); } }
Quoted 8 lines
mm/cma.c struct folio *cma_alloc_folio(struct cma *cma, int order, gfp_t gfp) · line 975
Quoted 6 lines
mm/cma.c +2
-3- if (!ret) - pr_debug("%s(page %p, count %lu)\n", - __func__, (void *)pages, count); + if (r == cma->nranges) { + pr_debug("%s(no cma range match the page %p)\n",".. matches the page range ..." ?
Yeah,will update, thanks
Quoted 5 lines
With that
PATCH v3 5/6 mm: cma: add cma_alloc_frozen{_compound}()
In reply to Kefeng WangIntroduce cma_alloc_frozen{_compound}() helper to alloc pages without incrementing their refcount, and convert hugetlb cma to use cma_alloc_frozen_compound, also move cma_validate_zones() into mm/internal.h since no outside user.
--- include/linux/cma.h | 25 +++++---------------- mm/cma.c | 55 +++++++++++++++++++++++++++++---------------- mm/hugetlb_cma.c | 22 ++++++++++-------- mm/internal.h | 6 +++++ 4 files changed, 60 insertions(+), 48 deletions(-)
include/linux/cma.h +5 -20
@@ -51,29 +51,14 @@ extern struct page *cma_alloc(struct cma *cma, unsigned long count, unsigned int bool no_warn);extern bool cma_release (struct cma *cma,const struct page *pages,unsigned long count); +struct page *cma_alloc_frozen (struct cma *cma,unsigned long count, +unsigned int align,bool no_warn); +bool cma_release_frozen (struct cma *cma,const struct page *pages, +unsigned long count); +extern int cma_for_each_area (int (*it)(struct cma *cma,void *data),void *data);extern bool cma_intersects (struct cma *cma,unsigned long start,unsigned long end);extern void cma_reserve_pages_on_error (struct cma *cma); -#ifdef CONFIG_CMA -struct folio *cma_alloc_folio (struct cma *cma,int order,gfp_t gfp); -bool cma_free_folio (struct cma *cma,const struct folio *folio); -bool cma_validate_zones (struct cma *cma); -#else -static inline struct folio *cma_alloc_folio (struct cma *cma,int order,gfp_t gfp) -{ -return NULL ; -} - -static inline bool cma_free_folio (struct cma *cma,const struct folio *folio) -{ -return false; -} -static inline bool cma_validate_zones (struct cma *cma) -{ -return false; -} -#endif -#endif
mm/cma.c +36 -19
102 lines not on this page · raw message
mm/hugetlb_cma.c +13 -9
@@ -22,33 +22,37 @@ void hugetlb_cma_free_folio(struct folio *folio) {int nid =folio_nid (folio); -WARN_ON_ONCE (!cma_free_folio (hugetlb_cma[nid], folio)); +WARN_ON_ONCE (!cma_release (hugetlb_cma[nid], &folio->page, +folio_nr_pages (folio))); } -struct folio *hugetlb_cma_alloc_folio (int order,gfp_t gfp_mask,int nid,nodemask_t *nodemask) {int node; -struct folio *folio =NULL ; +struct folio *folio; +struct page *page =NULL ;if (hugetlb_cma[nid]) - folio =cma_alloc_folio (hugetlb_cma[nid], order, gfp_mask); + page =cma_alloc_frozen_compound (hugetlb_cma[nid], order); -if (!folio && !(gfp_mask & __GFP_THISNODE)) { +if (!page && !(gfp_mask & __GFP_THISNODE)) {for_each_node_mask (node, *nodemask) {if (node == nid || !hugetlb_cma[node])continue ; - folio =cma_alloc_folio (hugetlb_cma[node], order, gfp_mask); -if (folio) + page =cma_alloc_frozen_compound (hugetlb_cma[nid], order); +if (page)break ; } } -if (folio) -folio_set_hugetlb_cma (folio); +if (!page) +return NULL ; +set_page_refcounted (page); + folio =page_folio (page); +folio_set_hugetlb_cma (folio);return folio; }
mm/internal.h +6 -0
@@ -936,9 +936,15 @@ void init_cma_reserved_pageblock(struct page *page);struct cma ;#ifdef CONFIG_CMA +struct page *cma_alloc_frozen_compound (struct cma *cma,unsigned int order); +bool cma_validate_zones (struct cma *cma);void *cma_reserve_early (struct cma *cma,unsigned long size);void init_cma_pageblock (struct page *page);#else +static inline bool cma_validate_zones (struct cma *cma) +{ +return false; +}static inline void *cma_reserve_early (struct cma *cma,unsigned long size) {return NULL ;
Signature · 1 line
2.27.0
PATCH v3 6/6 mm: hugetlb: allocate frozen pages in alloc_gigantic_folio()
In reply to Kefeng WangThe alloc_gigantic_folio() allocates a folio by alloc_contig_range() with refcount increated and then freeze it, convert to allocate a frozen folio to remove the atomic operation about folio refcount, and saving atomic operation during __update_and_free_hugetlb_folio too. Also rename hugetlb_cma_{alloc,free}_folio() with frozen which make them more self-explanatory.
--- include/linux/gfp.h | 23 ------------------ mm/hugetlb.c | 58 +++++++++++---------------------------------- mm/hugetlb_cma.c | 11 ++++----- mm/hugetlb_cma.h | 10 ++++---- 4 files changed, 24 insertions(+), 78 deletions(-)
include/linux/gfp.h +0 -23
@@ -453,27 +453,4 @@ void free_contig_range_frozen(unsigned long pfn, unsigned long nr_pages);void free_contig_range (unsigned long pfn,unsigned long nr_pages);#endif -#ifdef CONFIG_CONTIG_ALLOC -static inline struct folio *folio_alloc_gigantic_noprof (int order,gfp_t gfp, -int nid,nodemask_t *node) -{ -struct page *page; - -if (WARN_ON (!order || !(gfp & __GFP_COMP))) -return NULL ; - - page =alloc_contig_pages_noprof (1 << order, gfp, nid, node); - -return page ?page_folio (page) :NULL ; -} -#else -static inline struct folio *folio_alloc_gigantic_noprof (int order,gfp_t gfp, -int nid,nodemask_t *node) -{ -return NULL ; -} -#endif -/* This should be paired with folio_put() rather than free_contig_range(). */ -#define folio_alloc_gigantic (...) alloc_hooks(folio_alloc_gigantic_noprof(__VA_ARGS__)) -#endif /* __LINUX_GFP_H */
mm/hugetlb.c +14 -44
85 lines not on this page · raw message
mm/hugetlb_cma.c +5 -6
@@ -18,16 +18,16 @@ static unsigned long hugetlb_cma_size_in_node[MAX_NUMNODES] __initdata;static bool hugetlb_cma_only;static unsigned long hugetlb_cma_size__initdata ; -void hugetlb_cma_free_folio (struct folio *folio) +void hugetlb_cma_free_frozen_folio (struct folio *folio) {int nid =folio_nid (folio); -WARN_ON_ONCE (!cma_release (hugetlb_cma[nid], &folio->page, -folio_nr_pages (folio))); +WARN_ON_ONCE (!cma_release_frozen (hugetlb_cma[nid], &folio->page, +folio_nr_pages (folio))); } -struct folio *hugetlb_cma_alloc_folio (int order,gfp_t gfp_mask, -int nid,nodemask_t *nodemask) +struct folio *hugetlb_cma_alloc_frozen_folio (int order,gfp_t gfp_mask, +int nid,nodemask_t *nodemask) {int node;struct folio *folio;
@@ -50,7 +50,6 @@ struct folio *hugetlb_cma_alloc_folio(int order, gfp_t gfp_mask,if (!page)return NULL ; -set_page_refcounted (page); folio =page_folio (page);folio_set_hugetlb_cma (folio);return folio;
mm/hugetlb_cma.h +5 -5
@@ -3,8 +3,8 @@#define _LINUX_HUGETLB_CMA_H#ifdef CONFIG_CMA -void hugetlb_cma_free_folio (struct folio *folio); -struct folio *hugetlb_cma_alloc_folio (int order,gfp_t gfp_mask, +void hugetlb_cma_free_frozen_folio (struct folio *folio); +struct folio *hugetlb_cma_alloc_frozen_folio (int order,gfp_t gfp_mask,int nid,nodemask_t *nodemask);struct huge_bootmem_page *hugetlb_cma_alloc_bootmem (struct hstate *h,int *nid,bool node_exact);
@@ -14,12 +14,12 @@ unsigned long hugetlb_cma_total_size(void);void hugetlb_cma_validate_params (void );bool hugetlb_early_cma (struct hstate *h);#else -static inline void hugetlb_cma_free_folio (struct folio *folio) +static inline void hugetlb_cma_free_frozen_folio (struct folio *folio) { } -static inline struct folio *hugetlb_cma_alloc_folio (int order,gfp_t gfp_mask, -int nid,nodemask_t *nodemask) +static inline struct folio *hugetlb_cma_alloc_frozen_folio (int order, +gfp_t gfp_mask,int nid,nodemask_t *nodemask) {return NULL ; }
Signature · 1 line
2.27.0
Hello,
Quoted 21 lines · Kefeng Wang
Introduce
alloc_contig_frozen_pages()andcma_alloc_frozen_compound()which avoid atomic operation about page refcount, and then convert to allocate frozen gigantic folio by the new helpers in hugetlb to cleanup thealloc_gigantic_folio().v3:
- Fix built warn/err, found by lkp test
- Address some David's comments,
- Force on frozen part and drop the optimization part
- Renamesplit_non_compound_pages()to__split_pages()
- Adding back debug print/WARN_ON if no cma range found or the pfn range of page is not full match the cma range.v2:
- Optimize gigantic folio allocation speed
- UsingHPAGE_PUD_ORDERindebug_vm_pgtable
- Address some David's comments,
- killfolio_alloc_gigantic()
- add generic cma_alloc_frozen{_compound}() instead of cma_{alloc,free}_folio
Quoted 7 lines
Kefeng Wang (6):
mm:debug_vm_pgtable: adddebug_vm_pgtable_free_huge_page()
mm:page_alloc: add__split_page()
mm:page_alloc: add alloc_contig_{range_frozen,frozen_pages}()
mm: cma: add__cma_release()
mm: cma: add cma_alloc_frozen{_compound}()
mm: hugetlb: allocate frozen pages inalloc_gigantic_folio()
Any comments about patch3/5/6, I will update a new version in this week.
Thanks for all the comments.
Quoted 12 lines
include/linux/cma.h | 26 ++---- include/linux/gfp.h | 52 +++++------ mm/cma.c | 109 +++++++++++----------- mm/debug_vm_pgtable.c | 38 ++++---- mm/hugetlb.c | 58 +++--------- mm/hugetlb_cma.c | 27 +++--- mm/hugetlb_cma.h | 10 +-- mm/internal.h | 6 ++ mm/page_alloc.c | 204 ++++++++++++++++++++++++++++-------------- 9 files changed, 271 insertions(+), 259 deletions(-)