mm: accelerate gigantic folio allocation
Subsystems (4)
cover mm: accelerate gigantic folio allocation
Optimize pfn_range_valid_contig() and replace_free_hugepage_folios() in alloc_contig_frozen_pages() to speed up gigantic folio allocation. The allocation time for 120*1G folios drops from 3.605s to 0.431s.
resend:
- no function changes,
- split out the page_is_unmovable() into a separate patch, per Andrew
- mark two global values with __ro_after_init in patch5
---
This is part of [patch v2 0/8] mm: hugetlb: allocate frozen gigantic folio[1], and as suggested by David[2], reuses some logic from the has_unmovable_pages().
[1] https://lore.kernel.org/linux-mm/[email protected]/
[2] https://lore.kernel.org/linux-mm/[email protected]/
Kefeng Wang (5):
mm: page_isolation: introduce page_is_unmovable()
mm: page_alloc: optimize pfn_range_valid_contig()
mm: hugetlb: optimize replace_free_hugepage_folios()
mm: hugetlb_cma: optimize hugetlb_cma_alloc_frozen_folio()
mm: hugetlb_cma: mark hugetlb_cma{_only} as __ro_after_init
include/linux/page-isolation.h | 2 + mm/hugetlb.c | 54 +++++++--- mm/hugetlb_cma.c | 9 +- mm/page_alloc.c | 25 ++--- mm/page_isolation.c | 187 +++++++++++++++++---------------- 5 files changed, 157 insertions(+), 120 deletions(-)
Signature · 1 line
2.27.0
PATCH 1/5 mm: page_isolation: introduce page_is_unmovable()
Factor out the check if a page is unmovable into a new helper, and will be reused in the following patch.
No functional change intended, the minor changes are as follows,
1) Avoid unnecessary calls by checking CONFIG_ARCH_ENABLE_HUGEPAGE_MIGRATION
2) Directly call PageCompound since PageTransCompound may be dropped
3) Using folio_test_hugetlb()
--- include/linux/page-isolation.h | 2 + mm/page_isolation.c | 187 +++++++++++++++++---------------- 2 files changed, 101 insertions(+), 88 deletions(-)
include/linux/page-isolation.h +2 -0
@@ -67,4 +67,6 @@ void undo_isolate_page_range(unsigned long start_pfn, unsigned long end_pfn);int test_pages_isolated (unsigned long start_pfn,unsigned long end_pfn,enum pb_isolate_mode mode); +bool page_is_unmovable (struct zone *zone,struct page *page, +enum pb_isolate_mode mode,unsigned long *step);#endif
mm/page_isolation.c +99 -88
208 lines not on this page · raw message
Signature · 1 line
2.27.0
Re: [PATCH 1/5] mm: page_isolation: introduce page_is_unmovable()
Quoted 8 lines · Kefeng Wang
Factor out the check if a page is unmovable into a new helper, and will be reused in the following patch.
No functional change intended, the minor changes are as follows,
1) Avoid unnecessary calls by checkingCONFIG_ARCH_ENABLE_HUGEPAGE_MIGRATION
2) Directly call PageCompound since PageTransCompound may be dropped
3) Usingfolio_test_hugetlb()
Quoted 6 lines
Signed-off-by Kefeng Wang <[email protected]>--- include/linux/page-isolation.h | 2 + mm/page_isolation.c | 187 +++++++++++++++++---------------- 2 files changed, 101 insertions(+), 88 deletions(-)
LGTM.
Best Regards,
Yan, Zi
Re: [PATCH 1/5] mm: page_isolation: introduce page_is_unmovable()
In reply to Kefeng WangQuoted 9 lines · Kefeng Wang
Factor out the check if a page is unmovable into a new helper, and will be reused in the following patch.
No functional change intended, the minor changes are as follows,
1) Avoid unnecessary calls by checkingCONFIG_ARCH_ENABLE_HUGEPAGE_MIGRATION
2) Directly call PageCompound since PageTransCompound may be dropped
3) Usingfolio_test_hugetlb()Signed-off-by Kefeng Wang <[email protected]>
Looks good to me, I was a bit confused why we cannot rely directly on hugepage_migration_supported() without having to check for CONFIG_ARCH_ENABLE_HUGEPAGE_MIGRATION first, but I guess it speed up things.
Signature · 2 lines
Oscar Salvador SUSE Labs
PATCH 2/5 mm: page_alloc: optimize pfn_range_valid_contig()
In reply to Kefeng WangThe alloc_contig_pages() spends a significant amount of time within pfn_range_valid_contig().
- set_max_huge_pages
- 99.98% alloc_pool_huge_folio only_alloc_fresh_hugetlb_folio.isra.0
- alloc_contig_frozen_pages_noprof
- 87.00% pfn_range_valid_contig pfn_to_online_page
- 12.91% alloc_contig_frozen_range_noprof 4.51% replace_free_hugepage_folios
- 4.02% prep_new_page prep_compound_page
- 2.98% undo_isolate_page_range
- 2.79% unset_migratetype_isolate
- 2.75% __move_freepages_block_isolate 2.71% __move_freepages_block
- 0.98% start_isolate_page_range 0.66% set_migratetype_isolate
To optimize this process, use the new helper has_unmovable_pages() to avoid more unnecessary iterations for compound pages, such as THP, and high-order buddy pages, which significantly improving the efficiency of contiguous memory allocation.
A simple test on machine with 114G free memory, allocate 120 * 1G HugeTLB folios(104 successfully returned),
time echo 120 > /sys/kernel/mm/hugepages/hugepages-1048576kB/nr_hugepages
Before: 0m3.605s After: 0m0.602s
--- mm/page_alloc.c | 25 ++++++++----------------- 1 file changed, 8 insertions(+), 17 deletions(-)
mm/page_alloc.c +8 -17
@@ -7157,18 +7157,20 @@ static bool pfn_range_valid_contig(struct zone *z, unsigned long start_pfn, unsigned long nr_pages, bool skip_hugetlb, bool *skipped_hugetlb) { - unsigned long i, end_pfn = start_pfn + nr_pages; + unsigned long end_pfn = start_pfn + nr_pages;struct page *page; -for (i = start_pfn; i < end_pfn; i++) { - page =pfn_to_online_page (i); +while (start_pfn < end_pfn) { +unsigned long step =1 ; + + page =pfn_to_online_page (start_pfn);if (!page)return false;if (page_zone (page) != z)return false; -if (PageReserved (page)) +if (page_is_unmovable (z, page,PB_ISOLATE_MODE_OTHER , &step))return false;/*
@@ -7183,9 +7185,6 @@ static bool pfn_range_valid_contig(struct zone *z, unsigned long start_pfn,if (PageHuge (page)) {unsigned int order; -if (!IS_ENABLED (CONFIG_ARCH_ENABLE_HUGEPAGE_MIGRATION )) -return false; -if (skip_hugetlb) { *skipped_hugetlb = true;return false;
@@ -7196,17 +7195,9 @@ static bool pfn_range_valid_contig(struct zone *z, unsigned long start_pfn,if ((order >=MAX_FOLIO_ORDER ) || (nr_pages <= (1 << order)))return false; - -/* -* Reaching this point means we've encounted a huge page -* smaller than nr_pages, skip all pfn's for that page. -* -* We can't get here from a tail-PageHuge, as it implies -* we started a scan in the middle of a hugepage larger -* than nr_pages - which the prior check filters for. -*/ - i += (1 << order) -1 ; } + + start_pfn += step; }return true; }
Signature · 1 line
2.27.0
Zi Yan
reviewerRe: [PATCH 2/5] mm: page_alloc: optimize pfn_range_valid_contig()
Quoted 20 lines · Kefeng Wang
The
alloc_contig_pages()spends a significant amount of time withinpfn_range_valid_contig().-
set_max_huge_pages
- 99.98%alloc_pool_huge_folioonly_alloc_fresh_hugetlb_folio.isra.0
-alloc_contig_frozen_pages_noprof
- 87.00%pfn_range_valid_contigpfn_to_online_page
- 12.91%alloc_contig_frozen_range_noprof4.51%replace_free_hugepage_folios
- 4.02%prep_new_pageprep_compound_page
- 2.98%undo_isolate_page_range
- 2.79%unset_migratetype_isolate
- 2.75%__move_freepages_block_isolate2.71%__move_freepages_block
- 0.98%start_isolate_page_range0.66%set_migratetype_isolate
Quoted 1 line
To optimize this process, use the new helper
has_unmovable_pages()
s/has_unmovable_pages/page_is_unmovable
Quoted 2 lines
to avoid more unnecessary iterations for compound pages, such as THP, and high-order buddy pages, which significantly improving the
s/THP/THP not on LRU/
Quoted 55 lines
efficiency of contiguous memory allocation.
A simple test on machine with 114G free memory, allocate 120 * 1G HugeTLB folios(104 successfully returned),
time echo 120 >
/sys/kernel/mm/hugepages/hugepages-1048576kB/nr_hugepagesBefore: 0m3.605s After: 0m0.602sSigned-off-by Kefeng Wang <[email protected]>--- mm/page_alloc.c | 25 ++++++++----------------- 1 file changed, 8 insertions(+), 17 deletions(-)mm/page_alloc.c +6
-7@@ -7157,18 +7157,20 @@ static bool pfn_range_valid_contig(struct zone *z, unsigned long start_pfn, unsigned long nr_pages, bool skip_hugetlb, bool *skipped_hugetlb) { - unsigned long i, end_pfn = start_pfn + nr_pages; + unsigned long end_pfn = start_pfn + nr_pages;struct page *page; -for (i = start_pfn; i < end_pfn; i++) { - page =pfn_to_online_page (i); +while (start_pfn < end_pfn) { +unsigned long step =1 ; + + page =pfn_to_online_page (start_pfn);if (!page)return false;if (page_zone (page) != z)return false; -if (PageReserved (page)) +if (page_is_unmovable (z, page,PB_ISOLATE_MODE_OTHER , &step))return false;/* @@ -7183,9 +7185,6 @@ static bool pfn_range_valid_contig(struct zone *z, unsigned long start_pfn,if (PageHuge (page)) {unsigned int order; -if (!IS_ENABLED (CONFIG_ARCH_ENABLE_HUGEPAGE_MIGRATION )) -return false; -if (skip_hugetlb) { *skipped_hugetlb = true;return false;
mm/page_alloc.c static bool pfn_range_valid_contig(struct zone *z, unsigned long start_pfn, · line 7197
Quoted 3 lines
mm/page_alloc.c +0
-0@@ -7196,17 +7195,9 @@ static bool pfn_range_valid_contig(struct zone *z, unsigned long start_pfn,if ((order >=MAX_FOLIO_ORDER ) || (nr_pages <= (1 << order)))return false;
How does page_is_unmovable() interact with the code inside “if (PageHuge(page))”? page_is_unmovable() only identify 1GB hugetlb as unmovable, so skip_hugetlb still works?
Quoted 18 lines
mm/page_alloc.c +2
-10- -/* -* Reaching this point means we've encounted a huge page -* smaller than nr_pages, skip all pfn's for that page. -* -* We can't get here from a tail-PageHuge, as it implies -* we started a scan in the middle of a hugepage larger -* than nr_pages - which the prior check filters for. -*/ - i += (1 << order) -1 ; } + + start_pfn += step; }return true; }Signature · 1 line
2.27.0
Best Regards,
Yan, Zi
Re: [PATCH 2/5] mm: page_alloc: optimize pfn_range_valid_contig()
Quoted 22 lines · Zi Yan
Quoted 20 lines · Kefeng Wang
The
alloc_contig_pages()spends a significant amount of time withinpfn_range_valid_contig().-
set_max_huge_pages
- 99.98%alloc_pool_huge_folioonly_alloc_fresh_hugetlb_folio.isra.0
-alloc_contig_frozen_pages_noprof
- 87.00%pfn_range_valid_contigpfn_to_online_page
- 12.91%alloc_contig_frozen_range_noprof4.51%replace_free_hugepage_folios
- 4.02%prep_new_pageprep_compound_page
- 2.98%undo_isolate_page_range
- 2.79%unset_migratetype_isolate
- 2.75%__move_freepages_block_isolate2.71%__move_freepages_block
- 0.98%start_isolate_page_range0.66%set_migratetype_isolate
Quoted 3 lines
Quoted 1 line
To optimize this process, use the new helper
has_unmovable_pages()s/has_unmovable_pages/page_is_unmovable
Indeed.
Quoted 5 lines
Quoted 3 lines
to avoid more unnecessary iterations for compound pages, such as THP, and high-order buddy pages, which significantly improving the
s/THP/THP not on LRU/
Sure
Quoted 49 lines
Quoted 49 lines
efficiency of contiguous memory allocation.
A simple test on machine with 114G free memory, allocate 120 * 1G HugeTLB folios(104 successfully returned),
time echo 120 > /sys/kernel/mm/hugepages/hugepages-1048576kB/nr_hugepagesBefore: 0m3.605s After: 0m0.602sSigned-off-by Kefeng Wang <[email protected]>--- mm/page_alloc.c | 25 ++++++++----------------- 1 file changed, 8 insertions(+), 17 deletions(-)mm/page_alloc.c +6
-4@@ -7157,18 +7157,20 @@ static bool pfn_range_valid_contig(struct zone *z, unsigned long start_pfn, unsigned long nr_pages, bool skip_hugetlb, bool *skipped_hugetlb) { - unsigned long i, end_pfn = start_pfn + nr_pages; + unsigned long end_pfn = start_pfn + nr_pages;struct page *page; -for (i = start_pfn; i < end_pfn; i++) { - page =pfn_to_online_page (i); +while (start_pfn < end_pfn) { +unsigned long step =1 ; + + page =pfn_to_online_page (start_pfn);if (!page)return false;if (page_zone (page) != z)return false; -if (PageReserved (page)) +if (page_is_unmovable (z, page,PB_ISOLATE_MODE_OTHER , &step))return false;/* @@ -7183,9 +7185,6 @@ static bool pfn_range_valid_contig(struct zone *z, unsigned long start_pfn,if (PageHuge (page)) {unsigned int order;
Quoted 14 lines
mm/page_alloc.c static bool pfn_range_valid_contig(struct zone *z, unsigned long start_pfn, · line 7197
Quoted 10 lines
mm/page_alloc.c +0
-3-if (!IS_ENABLED (CONFIG_ARCH_ENABLE_HUGEPAGE_MIGRATION )) -return false; -if (skip_hugetlb) { *skipped_hugetlb = true;return false;@@ -7196,17 +7195,9 @@ static bool pfn_range_valid_contig(struct zone *z, unsigned long start_pfn,if ((order >=MAX_FOLIO_ORDER ) || (nr_pages <= (1 << order)))return false;How does
page_is_unmovable()interact with the code inside “if (PageHuge(page))”?page_is_unmovable()only identify 1GB hugetlb as unmovable, soskip_hugetlbstill works?
Initially, I wanted to move the skip_hugetlb processing into a new page_is_unmovable() by introducing a new PB_ISOLATE_MODE, passing the skip_hugetlb/skipped_hugetlb/nr_pages to page_is_unmovable(), it looks very complicated/ugly.
if (PageHuage()) {
if(page is unmovable)
return;
skip_hugetlb processing
}Back to the current code before I made any changes, skip_hugetlb logical only works for movable huge pages by checking CONFIG_ARCH_ENABLE_HUGEPAGE_MIGRATION, the checking is not incomplete since no runtime check, but the new helper made a better judgment.
And after changes,
if (page_is_unmovale())
return
if (PageHuge())
skip_hugetlb processing
I don' change the skip hugetlb logical, the only drawback is the PageHuge is checked twice, Maybe I miss something?
Quoted 23 lines
Quoted 19 lines
mm/page_alloc.c +2
-10- -/* -* Reaching this point means we've encounted a huge page -* smaller than nr_pages, skip all pfn's for that page. -* -* We can't get here from a tail-PageHuge, as it implies -* we started a scan in the middle of a hugepage larger -* than nr_pages - which the prior check filters for. -*/ - i += (1 << order) -1 ; } + + start_pfn += step; }return true; }Signature · 1 line
2.27.0Best Regards,
Yan, Zi
Re: [PATCH 2/5] mm: page_alloc: optimize pfn_range_valid_contig()
Quoted 121 lines · Kefeng Wang
Quoted 22 lines · Zi Yan
Quoted 20 lines · Kefeng Wang
The
alloc_contig_pages()spends a significant amount of time withinpfn_range_valid_contig().-
set_max_huge_pages
- 99.98%alloc_pool_huge_folioonly_alloc_fresh_hugetlb_folio.isra.0
-alloc_contig_frozen_pages_noprof
- 87.00%pfn_range_valid_contigpfn_to_online_page
- 12.91%alloc_contig_frozen_range_noprof4.51%replace_free_hugepage_folios
- 4.02%prep_new_pageprep_compound_page
- 2.98%undo_isolate_page_range
- 2.79%unset_migratetype_isolate
- 2.75%__move_freepages_block_isolate2.71%__move_freepages_block
- 0.98%start_isolate_page_range0.66%set_migratetype_isolateQuoted 3 lines
Quoted 1 line
To optimize this process, use the new helper
has_unmovable_pages()s/has_unmovable_pages/page_is_unmovable
Indeed.
Quoted 5 lines
Quoted 3 lines
to avoid more unnecessary iterations for compound pages, such as THP, and high-order buddy pages, which significantly improving the
s/THP/THP not on LRU/
Sure
Quoted 49 lines
Quoted 49 lines
efficiency of contiguous memory allocation.
A simple test on machine with 114G free memory, allocate 120 * 1G HugeTLB folios(104 successfully returned),
time echo 120 > /sys/kernel/mm/hugepages/hugepages-1048576kB/nr_hugepagesBefore: 0m3.605s After: 0m0.602sSigned-off-by Kefeng Wang <[email protected]>--- mm/page_alloc.c | 25 ++++++++----------------- 1 file changed, 8 insertions(+), 17 deletions(-)mm/page_alloc.c +6
-4@@ -7157,18 +7157,20 @@ static bool pfn_range_valid_contig(struct zone *z, unsigned long start_pfn, unsigned long nr_pages, bool skip_hugetlb, bool *skipped_hugetlb) { - unsigned long i, end_pfn = start_pfn + nr_pages; + unsigned long end_pfn = start_pfn + nr_pages;struct page *page; -for (i = start_pfn; i < end_pfn; i++) { - page =pfn_to_online_page (i); +while (start_pfn < end_pfn) { +unsigned long step =1 ; + + page =pfn_to_online_page (start_pfn);if (!page)return false;if (page_zone (page) != z)return false; -if (PageReserved (page)) +if (page_is_unmovable (z, page,PB_ISOLATE_MODE_OTHER , &step))return false;/* @@ -7183,9 +7185,6 @@ static bool pfn_range_valid_contig(struct zone *z, unsigned long start_pfn,if (PageHuge (page)) {unsigned int order;Quoted 14 lines
mm/page_alloc.c static bool pfn_range_valid_contig(struct zone *z, unsigned long start_pfn, · line 7197
Quoted 10 lines
mm/page_alloc.c +0
-3-if (!IS_ENABLED (CONFIG_ARCH_ENABLE_HUGEPAGE_MIGRATION )) -return false; -if (skip_hugetlb) { *skipped_hugetlb = true;return false;@@ -7196,17 +7195,9 @@ static bool pfn_range_valid_contig(struct zone *z, unsigned long start_pfn,if ((order >=MAX_FOLIO_ORDER ) || (nr_pages <= (1 << order)))return false;How does
page_is_unmovable()interact with the code inside “if (PageHuge(page))”?page_is_unmovable()only identify 1GB hugetlb as unmovable, soskip_hugetlbstill works?Initially, I wanted to move the
skip_hugetlbprocessing into a newpage_is_unmovable()by introducing a newPB_ISOLATE_MODE, passing the skip_hugetlb/skipped_hugetlb/nr_pages topage_is_unmovable(), it looks very complicated/ugly.if (PageHuage()) { if(page is unmovable) return; skip_hugetlb processing }Back to the current code before I made any changes,
skip_hugetlblogical only works for movable huge pages by checkingCONFIG_ARCH_ENABLE_HUGEPAGE_MIGRATION, the checking is not incomplete since no runtime check, but the new helper made a better judgment.And after changes,
Quoted 8 lines
if (
page_is_unmovale())
returnif (
PageHuge())
skip_hugetlbprocessingI don' change the skip hugetlb logical, the only drawback is the PageHuge is checked twice, Maybe I miss something?
Sounds good to me. Thanks. I just want to double check with you.
With the changes to the commit message, feel free to add
Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
mm/page_alloc.c +2
-10- -/* -* Reaching this point means we've encounted a huge page -* smaller than nr_pages, skip all pfn's for that page. -* -* We can't get here from a tail-PageHuge, as it implies -* we started a scan in the middle of a hugepage larger -* than nr_pages - which the prior check filters for. -*/ - i += (1 << order) -1 ; } + + start_pfn += step; }
Quoted 8 lines
Quoted 8 lines
Quoted 4 lines
mm/page_alloc.c +0
-0return true; }Signature · 1 line
2.27.0Best Regards,
Yan, Zi
Best Regards,
Yan, Zi
Re: [PATCH 2/5] mm: page_alloc: optimize pfn_range_valid_contig()
Quoted 129 lines · Zi Yan
Quoted 127 lines · Kefeng Wang
Quoted 22 lines · Zi Yan
Quoted 20 lines · Kefeng Wang
The
alloc_contig_pages()spends a significant amount of time withinpfn_range_valid_contig().-
set_max_huge_pages
- 99.98%alloc_pool_huge_folioonly_alloc_fresh_hugetlb_folio.isra.0
-alloc_contig_frozen_pages_noprof
- 87.00%pfn_range_valid_contigpfn_to_online_page
- 12.91%alloc_contig_frozen_range_noprof4.51%replace_free_hugepage_folios
- 4.02%prep_new_pageprep_compound_page
- 2.98%undo_isolate_page_range
- 2.79%unset_migratetype_isolate
- 2.75%__move_freepages_block_isolate2.71%__move_freepages_block
- 0.98%start_isolate_page_range0.66%set_migratetype_isolateQuoted 3 lines
Quoted 1 line
To optimize this process, use the new helper
has_unmovable_pages()s/has_unmovable_pages/page_is_unmovable
Indeed.
Quoted 5 lines
Quoted 3 lines
to avoid more unnecessary iterations for compound pages, such as THP, and high-order buddy pages, which significantly improving the
s/THP/THP not on LRU/
Sure
Quoted 49 lines
Quoted 49 lines
efficiency of contiguous memory allocation.
A simple test on machine with 114G free memory, allocate 120 * 1G HugeTLB folios(104 successfully returned),
time echo 120 > /sys/kernel/mm/hugepages/hugepages-1048576kB/nr_hugepagesBefore: 0m3.605s After: 0m0.602sSigned-off-by Kefeng Wang <[email protected]>--- mm/page_alloc.c | 25 ++++++++----------------- 1 file changed, 8 insertions(+), 17 deletions(-)mm/page_alloc.c +6
-4@@ -7157,18 +7157,20 @@ static bool pfn_range_valid_contig(struct zone *z, unsigned long start_pfn, unsigned long nr_pages, bool skip_hugetlb, bool *skipped_hugetlb) { - unsigned long i, end_pfn = start_pfn + nr_pages; + unsigned long end_pfn = start_pfn + nr_pages;struct page *page; -for (i = start_pfn; i < end_pfn; i++) { - page =pfn_to_online_page (i); +while (start_pfn < end_pfn) { +unsigned long step =1 ; + + page =pfn_to_online_page (start_pfn);if (!page)return false;if (page_zone (page) != z)return false; -if (PageReserved (page)) +if (page_is_unmovable (z, page,PB_ISOLATE_MODE_OTHER , &step))return false;/* @@ -7183,9 +7185,6 @@ static bool pfn_range_valid_contig(struct zone *z, unsigned long start_pfn,if (PageHuge (page)) {unsigned int order;Quoted 14 lines
mm/page_alloc.c static bool pfn_range_valid_contig(struct zone *z, unsigned long start_pfn, · line 7197
Quoted 10 lines
mm/page_alloc.c +0
-3-if (!IS_ENABLED (CONFIG_ARCH_ENABLE_HUGEPAGE_MIGRATION )) -return false; -if (skip_hugetlb) { *skipped_hugetlb = true;return false;@@ -7196,17 +7195,9 @@ static bool pfn_range_valid_contig(struct zone *z, unsigned long start_pfn,if ((order >=MAX_FOLIO_ORDER ) || (nr_pages <= (1 << order)))return false;How does
page_is_unmovable()interact with the code inside “if (PageHuge(page))”?page_is_unmovable()only identify 1GB hugetlb as unmovable, soskip_hugetlbstill works?Initially, I wanted to move the
skip_hugetlbprocessing into a newpage_is_unmovable()by introducing a newPB_ISOLATE_MODE, passing the skip_hugetlb/skipped_hugetlb/nr_pages topage_is_unmovable(), it looks very complicated/ugly.if (PageHuage()) { if(page is unmovable) return; skip_hugetlb processing }Back to the current code before I made any changes,
skip_hugetlblogical only works for movable huge pages by checkingCONFIG_ARCH_ENABLE_HUGEPAGE_MIGRATION, the checking is not incomplete since no runtime check, but the new helper made a better judgment.And after changes,
if (
page_is_unmovale())
returnif (
PageHuge())
skip_hugetlbprocessing
Quoted 6 lines
Quoted 2 lines
I don' change the skip hugetlb logical, the only drawback is the PageHuge is checked twice, Maybe I miss something?
Sounds good to me. Thanks. I just want to double check with you.
With the changes to the commit message, feel free to add
OK, I will update it.
Quoted 3 lines
Reviewed-by Zi Yan <[email protected]>
Thanks.
Re: [PATCH 2/5] mm: page_alloc: optimize pfn_range_valid_contig()
Quoted 5 lines · On Tue, 13 Jan 2026 17:55:43 +0800 Kefeng Wang <[email protected]>…
Quoted 3 lines
With the changes to the commit message, feel free to add
OK, I will update it.
akpmbot already did that.
Quoted 2 lines
Quoted 2 lines
Reviewed-by Zi Yan <[email protected]>
Thanks both.
From: Kefeng Wang <[email protected]>
Subject: mm: page_alloc: optimize pfn_range_valid_contig()
Date: Mon, 12 Jan 2026 23:09:51 +0800
The alloc_contig_pages() spends a significant amount of time within pfn_range_valid_contig().
- set_max_huge_pages
- 99.98% alloc_pool_huge_folio only_alloc_fresh_hugetlb_folio.isra.0
- alloc_contig_frozen_pages_noprof
- 87.00% pfn_range_valid_contig pfn_to_online_page
- 12.91% alloc_contig_frozen_range_noprof 4.51% replace_free_hugepage_folios
- 4.02% prep_new_page prep_compound_page
- 2.98% undo_isolate_page_range
- 2.79% unset_migratetype_isolate
- 2.75% __move_freepages_block_isolate 2.71% __move_freepages_block
- 0.98% start_isolate_page_range 0.66% set_migratetype_isolate
To optimize this process, use the new helper page_is_unmovable() to avoid more unnecessary iterations for compound pages, such as THP not on LRU, and high-order buddy pages, which significantly improving the efficiency of contiguous memory allocation.
A simple test on machine with 114G free memory, allocate 120 * 1G HugeTLB folios(104 successfully returned),
time echo 120 > /sys/kernel/mm/hugepages/hugepages-1048576kB/nr_hugepages
Before: 0m3.605s After: 0m0.602s
---
mm/page_alloc.c | 25 ++++++++----------------- 1 file changed, 8 insertions(+), 17 deletions(-)
mm/page_alloc.c renamed from mm/page_alloc.c~mm-page_alloc-optimize-pfn_range_valid_contig +8 -17
@@ -7157,18 +7157,20 @@ static bool pfn_range_valid_contig(struc unsigned long nr_pages, bool skip_hugetlb, bool *skipped_hugetlb) { - unsigned long i, end_pfn = start_pfn + nr_pages; + unsigned long end_pfn = start_pfn + nr_pages;struct page *page; -for (i = start_pfn; i < end_pfn; i++) { - page =pfn_to_online_page (i); +while (start_pfn < end_pfn) { +unsigned long step =1 ; + + page =pfn_to_online_page (start_pfn);if (!page)return false;if (page_zone (page) != z)return false; -if (PageReserved (page)) +if (page_is_unmovable (z, page,PB_ISOLATE_MODE_OTHER , &step))return false;/*
@@ -7183,9 +7185,6 @@ static bool pfn_range_valid_contig(strucif (PageHuge (page)) {unsigned int order; -if (!IS_ENABLED (CONFIG_ARCH_ENABLE_HUGEPAGE_MIGRATION )) -return false; -if (skip_hugetlb) { *skipped_hugetlb = true;return false;
@@ -7196,17 +7195,9 @@ static bool pfn_range_valid_contig(strucif ((order >=MAX_FOLIO_ORDER ) || (nr_pages <= (1 << order)))return false; - -/* -* Reaching this point means we've encounted a huge page -* smaller than nr_pages, skip all pfn's for that page. -* -* We can't get here from a tail-PageHuge, as it implies -* we started a scan in the middle of a hugepage larger -* than nr_pages - which the prior check filters for. -*/ - i += (1 << order) -1 ; } + + start_pfn += step; }return true; }
_
Re: [PATCH 2/5] mm: page_alloc: optimize pfn_range_valid_contig()
In reply to Kefeng WangQuoted 28 lines · Kefeng Wang
The
alloc_contig_pages()spends a significant amount of time withinpfn_range_valid_contig().-
set_max_huge_pages
- 99.98%alloc_pool_huge_folioonly_alloc_fresh_hugetlb_folio.isra.0
-alloc_contig_frozen_pages_noprof
- 87.00%pfn_range_valid_contigpfn_to_online_page
- 12.91%alloc_contig_frozen_range_noprof4.51%replace_free_hugepage_folios
- 4.02%prep_new_pageprep_compound_page
- 2.98%undo_isolate_page_range
- 2.79%unset_migratetype_isolate
- 2.75%__move_freepages_block_isolate2.71%__move_freepages_block
- 0.98%start_isolate_page_range0.66%set_migratetype_isolateTo optimize this process, use the new helper
has_unmovable_pages()to avoid more unnecessary iterations for compound pages, such as THP, and high-order buddy pages, which significantly improving the efficiency of contiguous memory allocation.A simple test on machine with 114G free memory, allocate 120 * 1G HugeTLB folios(104 successfully returned),
Quoted 6 lines
time echo 120 >
/sys/kernel/mm/hugepages/hugepages-1048576kB/nr_hugepagesBefore: 0m3.605s After: 0m0.602sSigned-off-by Kefeng Wang <[email protected]>
Looks good to me, just have a question below:
Quoted 18 lines
mm/page_alloc.c | 25 ++++++++----------------- 1 file changed, 8 insertions(+), 17 deletions(-)mm/page_alloc.c +0
-3@@ -7183,9 +7185,6 @@ static bool pfn_range_valid_contig(struct zone *z, unsigned long start_pfn,if (PageHuge (page)) {unsigned int order; -if (!IS_ENABLED (CONFIG_ARCH_ENABLE_HUGEPAGE_MIGRATION )) -return false; -if (skip_hugetlb) { *skipped_hugetlb = true;return false;
mm/page_alloc.c static bool pfn_range_valid_contig(struct zone *z, unsigned long start_pfn, · line 7195
Quoted 1 line
mm/page_alloc.c +0
-0@@ -7196,17 +7195,9 @@ static bool pfn_range_valid_contig(struct zone *z, unsigned long start_pfn, if ((order >= MAX_FOLIO_ORDER) ||
Does this not get already covered in page_is_unmovable() somehow? size_to_hstate() would return no state for order >= MAX_FOLIO_ORDER folio sizes?
I mean, it is subtle, and I am perfectly fine leaving as is, just asking.
Signature · 2 lines
Oscar Salvador SUSE Labs
Re: [PATCH 2/5] mm: page_alloc: optimize pfn_range_valid_contig()
Quoted 51 lines · Oscar Salvador
Quoted 28 lines · Kefeng Wang
The
alloc_contig_pages()spends a significant amount of time withinpfn_range_valid_contig().-
set_max_huge_pages
- 99.98%alloc_pool_huge_folioonly_alloc_fresh_hugetlb_folio.isra.0
-alloc_contig_frozen_pages_noprof
- 87.00%pfn_range_valid_contigpfn_to_online_page
- 12.91%alloc_contig_frozen_range_noprof4.51%replace_free_hugepage_folios
- 4.02%prep_new_pageprep_compound_page
- 2.98%undo_isolate_page_range
- 2.79%unset_migratetype_isolate
- 2.75%__move_freepages_block_isolate2.71%__move_freepages_block
- 0.98%start_isolate_page_range0.66%set_migratetype_isolateTo optimize this process, use the new helper
has_unmovable_pages()to avoid more unnecessary iterations for compound pages, such as THP, and high-order buddy pages, which significantly improving the efficiency of contiguous memory allocation.A simple test on machine with 114G free memory, allocate 120 * 1G HugeTLB folios(104 successfully returned),
Quoted 6 lines
time echo 120 > /sys/kernel/mm/hugepages/hugepages-1048576kB/nr_hugepagesBefore: 0m3.605s After: 0m0.602sSigned-off-by Kefeng Wang <[email protected]>Looks good to me, just have a question below:
Reviewed-by Oscar Salvador <[email protected]>Quoted 11 lines
mm/page_alloc.c | 25 ++++++++----------------- 1 file changed, 8 insertions(+), 17 deletions(-)mm/page_alloc.c +0
-0@@ -7183,9 +7185,6 @@ static bool pfn_range_valid_contig(struct zone *z, unsigned long start_pfn,if (PageHuge (page)) {unsigned int order;
Quoted 12 lines
mm/page_alloc.c static bool pfn_range_valid_contig(struct zone *z, unsigned long start_pfn, · line 7195
Quoted 8 lines
mm/page_alloc.c +0
-3-if (!IS_ENABLED (CONFIG_ARCH_ENABLE_HUGEPAGE_MIGRATION )) -return false; -if (skip_hugetlb) { *skipped_hugetlb = true;return false;@@ -7196,17 +7195,9 @@ static bool pfn_range_valid_contig(struct zone *z, unsigned long start_pfn, if ((order >= MAX_FOLIO_ORDER) ||Does this not get already covered in
page_is_unmovable()somehow?size_to_hstate()would return no state for order >=MAX_FOLIO_ORDERfolio sizes?
Yes, I think so.
Quoted 3 lines
I mean, it is subtle, and I am perfectly fine leaving as is, just asking.
Let's keep it as is for explicit checking.
PATCH 3/5 mm: hugetlb: optimize replace_free_hugepage_folios()
In reply to Kefeng WangIf no free hugepage folios are available, there is no need to perform any replacement operations. Additionally, gigantic folios should not be replaced under any circumstances. Therefore, we only check for the presence of non-gigantic folios.
To ensure that gigantic folios are not mistakenly replaced, we utilize isolate_or_dissolve_huge_folio().
Lastly, to optimize performance, we skip unnecessary iterations over pfn for compound pages and high-order buddy pages to save processing time.
A simple test on machine with 114G free memory, allocate 120 * 1G HugeTLB folios(104 successfully returned),
time echo 120 > /sys/kernel/mm/hugepages/hugepages-1048576kB/nr_hugepages
Before: 0m0.602s After: 0m0.431s
--- mm/hugetlb.c | 54 ++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 42 insertions(+), 12 deletions(-)
mm/hugetlb.c +42 -12
@@ -2806,26 +2806,56 @@ int isolate_or_dissolve_huge_folio(struct folio *folio, struct list_head *list)*/ int replace_free_hugepage_folios (unsigned long start_pfn,unsigned long end_pfn) { -struct folio *folio; -int ret =0 ; +unsigned long nr =0 ; +struct page *page; +struct hstate *h; +LIST_HEAD (list); -LIST_HEAD (isolate_list); +/* Avoid pfn iterations if no free non-gigantic huge pages */ +for_each_hstate (h) { +if (hstate_is_gigantic (h)) +continue ; + + nr += h->free_huge_pages; +if (nr) +break ; + } + +if (!nr) +return 0 ;while (start_pfn < end_pfn) { - folio =pfn_folio (start_pfn); + page =pfn_to_page (start_pfn); + nr =1 ; -/* Not to disrupt normal path by vainly holding hugetlb_lock */ -if (folio_test_hugetlb (folio) && !folio_ref_count (folio)) { - ret =alloc_and_dissolve_hugetlb_folio (folio, &isolate_list); -if (ret) -break ; +if (PageHuge (page) ||PageCompound (page)) { +struct folio *folio =page_folio (page); + + nr =folio_nr_pages (folio) -folio_page_idx (folio, page); + +/* Not to disrupt normal path by vainly holding hugetlb_lock */ +if (folio_test_hugetlb (folio) && !folio_ref_count (folio)) { +if (isolate_or_dissolve_huge_folio (folio, &list)) +return -ENOMEM ; -putback_movable_pages (&isolate_list); +putback_movable_pages (&list); + } + }else if (PageBuddy (page)) { +/* +* Buddy order check without zone lock is unsafe and +* the order is maybe invalid, but race should be +* small, and the worst thing is skipping free hugetlb. +*/ +const unsigned int order =buddy_order_unsafe (page); + +if (order <=MAX_PAGE_ORDER ) + nr =1UL << order; } - start_pfn++; + + start_pfn += nr; } -return ret; +return 0 ; }void wait_for_freed_hugetlb_folios (void )
Signature · 1 line
2.27.0
Oscar Salvador
maintainerRe: [PATCH 3/5] mm: hugetlb: optimize replace_free_hugepage_folios()
Quoted 20 lines · Kefeng Wang
If no free hugepage folios are available, there is no need to perform any replacement operations. Additionally, gigantic folios should not be replaced under any circumstances. Therefore, we only check for the presence of non-gigantic folios.
To ensure that gigantic folios are not mistakenly replaced, we utilize
isolate_or_dissolve_huge_folio().Lastly, to optimize performance, we skip unnecessary iterations over pfn for compound pages and high-order buddy pages to save processing time.
A simple test on machine with 114G free memory, allocate 120 * 1G HugeTLB folios(104 successfully returned),
time echo 120 >
/sys/kernel/mm/hugepages/hugepages-1048576kB/nr_hugepagesBefore: 0m0.602s After: 0m0.431sSigned-off-by Kefeng Wang <[email protected]>
...
Quoted 31 lines
int
replace_free_hugepage_folios(unsigned long start_pfn, unsigned long end_pfn){mm/hugetlb.c +18
-7-struct folio *folio; -int ret =0 ; +unsigned long nr =0 ; +struct page *page; +struct hstate *h; +LIST_HEAD (list); -LIST_HEAD (isolate_list); +/* Avoid pfn iterations if no free non-gigantic huge pages */ +for_each_hstate (h) { +if (hstate_is_gigantic (h)) +continue ; + + nr += h->free_huge_pages; +if (nr) +break ; + } + +if (!nr) +return 0 ;while (start_pfn < end_pfn) { - folio =pfn_folio (start_pfn); + page =pfn_to_page (start_pfn); + nr =1 ; -/* Not to disrupt normal path by vainly holding hugetlb_lock */ -if (folio_test_hugetlb (folio) && !folio_ref_count (folio)) { - ret =alloc_and_dissolve_hugetlb_folio (folio, &isolate_list);
mm/hugetlb.c int isolate_or_dissolve_huge_folio(struct folio *folio, struct list_head *list) · line 2838
Quoted 10 lines
mm/hugetlb.c +8
-2-if (ret) -break ; +if (PageHuge (page) ||PageCompound (page)) { +struct folio *folio =page_folio (page); + + nr =folio_nr_pages (folio) -folio_page_idx (folio, page); + +/* Not to disrupt normal path by vainly holding hugetlb_lock */ +if (folio_test_hugetlb (folio) && !folio_ref_count (folio)) { +if (isolate_or_dissolve_huge_folio (folio, &list))
isolate_or_dissolve_huge_folio() can also return -EBUSY, but you supersed that with ENOMEM in case it fails. Is that alright?
Maybe it is because down the chain we ignore the type of error as long as it is an error but just brining it up because the log now differs.
Signature · 2 lines
Oscar Salvador SUSE Labs
Re: [PATCH 3/5] mm: hugetlb: optimize replace_free_hugepage_folios()
Quoted 50 lines · Oscar Salvador
Quoted 20 lines · Kefeng Wang
If no free hugepage folios are available, there is no need to perform any replacement operations. Additionally, gigantic folios should not be replaced under any circumstances. Therefore, we only check for the presence of non-gigantic folios.
To ensure that gigantic folios are not mistakenly replaced, we utilize
isolate_or_dissolve_huge_folio().Lastly, to optimize performance, we skip unnecessary iterations over pfn for compound pages and high-order buddy pages to save processing time.
A simple test on machine with 114G free memory, allocate 120 * 1G HugeTLB folios(104 successfully returned),
time echo 120 > /sys/kernel/mm/hugepages/hugepages-1048576kB/nr_hugepagesBefore: 0m0.602s After: 0m0.431sSigned-off-by Kefeng Wang <[email protected]>...
Quoted 28 lines
int replace_free_hugepage_folios(unsigned long start_pfn, unsigned long end_pfn) {mm/hugetlb.c +18
-4-struct folio *folio; -int ret =0 ; +unsigned long nr =0 ; +struct page *page; +struct hstate *h; +LIST_HEAD (list); -LIST_HEAD (isolate_list); +/* Avoid pfn iterations if no free non-gigantic huge pages */ +for_each_hstate (h) { +if (hstate_is_gigantic (h)) +continue ; + + nr += h->free_huge_pages; +if (nr) +break ; + } + +if (!nr) +return 0 ;while (start_pfn < end_pfn) { - folio =pfn_folio (start_pfn); + page =pfn_to_page (start_pfn); + nr =1 ;
Quoted 16 lines
mm/hugetlb.c int isolate_or_dissolve_huge_folio(struct folio *folio, struct list_head *list) · line 2838
Quoted 13 lines
mm/hugetlb.c +8
-5-/* Not to disrupt normal path by vainly holding hugetlb_lock */ -if (folio_test_hugetlb (folio) && !folio_ref_count (folio)) { - ret =alloc_and_dissolve_hugetlb_folio (folio, &isolate_list); -if (ret) -break ; +if (PageHuge (page) ||PageCompound (page)) { +struct folio *folio =page_folio (page); + + nr =folio_nr_pages (folio) -folio_page_idx (folio, page); + +/* Not to disrupt normal path by vainly holding hugetlb_lock */ +if (folio_test_hugetlb (folio) && !folio_ref_count (folio)) { +if (isolate_or_dissolve_huge_folio (folio, &list))
isolate_or_dissolve_huge_folio()can also return-EBUSY, but you supersed that with
ENOMEM in case it fails. Is that alright?> Maybe it is because down the chain we ignore the type of error as
long as it is
Quoted 1 line
an error but just brining it up because the log now differs.
The caller(i.e. virtio_mem_fake_offline) may perform some operations based on different error codes, I will restore this one.
Thanks.
PATCH v2 3/5 mm: hugetlb: optimize replace_free_hugepage_folios()
In reply to Kefeng WangIf no free hugepage folios are available, there is no need to perform any replacement operations. Additionally, gigantic folios should not be replaced under any circumstances. Therefore, we only check for the presence of non-gigantic folios, also adding the gigantic folio check to avoid accidental replacement.
To optimize performance, we skip unnecessary iterations over pfn for compound pages and high-order buddy pages to save processing time.
A simple test on machine with 114G free memory, allocate 120 * 1G HugeTLB folios(104 successfully returned),
time echo 120 > /sys/kernel/mm/hugepages/hugepages-1048576kB/nr_hugepages
Before: 0m0.602s After: 0m0.431s
---
v2:
- Turn back to use alloc_and_dissolve_hugetlb_folio() since Oscar pointed out that the return value is different. Then adding gigantic folio check independently, also update changelog.
mm/hugetlb.c | 54 ++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 44 insertions(+), 10 deletions(-)
mm/hugetlb.c +44 -10
@@ -2806,23 +2806,57 @@ int isolate_or_dissolve_huge_folio(struct folio *folio, struct list_head *list)*/ int replace_free_hugepage_folios (unsigned long start_pfn,unsigned long end_pfn) { -struct folio *folio; +unsigned long nr =0 ; +struct page *page; +struct hstate *h; +LIST_HEAD (list);int ret =0 ; -LIST_HEAD (isolate_list); +/* Avoid pfn iterations if no free non-gigantic huge pages */ +for_each_hstate (h) { +if (hstate_is_gigantic (h)) +continue ; + + nr += h->free_huge_pages; +if (nr) +break ; + } + +if (!nr) +return 0 ;while (start_pfn < end_pfn) { - folio =pfn_folio (start_pfn); + page =pfn_to_page (start_pfn); + nr =1 ; -/* Not to disrupt normal path by vainly holding hugetlb_lock */ -if (folio_test_hugetlb (folio) && !folio_ref_count (folio)) { - ret =alloc_and_dissolve_hugetlb_folio (folio, &isolate_list); -if (ret) -break ; +if (PageHuge (page) ||PageCompound (page)) { +struct folio *folio =page_folio (page); + + nr =folio_nr_pages (folio) -folio_page_idx (folio, page); + +/* Not to disrupt normal path by vainly holding hugetlb_lock */ +if (folio_test_hugetlb (folio) && !folio_ref_count (folio)) { +if (order_is_gigantic (folio_order (folio))) +return -ENOMEM ; + + ret =alloc_and_dissolve_hugetlb_folio (folio, &list); +if (ret) +break ; + +putback_movable_pages (&list); + } + }else if (PageBuddy (page)) { +/* +* Buddy order check without zone lock is unsafe and +* the order is maybe invalid, but race should be +* small, and the worst thing is skipping free hugetlb. +*/ +const unsigned int order =buddy_order_unsafe (page); -putback_movable_pages (&isolate_list); +if (order <=MAX_PAGE_ORDER ) + nr =1UL << order; } - start_pfn++; + start_pfn += nr; }return ret;
Signature · 1 line
2.27.0
Andrew Morton
maintainerRe: [PATCH v2 3/5] mm: hugetlb: optimize replace_free_hugepage_folios()
Quoted 9 lines · On Wed, 14 Jan 2026 21:55:12 +0800 Kefeng Wang <[email protected]>…
If no free hugepage folios are available, there is no need to perform any replacement operations. Additionally, gigantic folios should not be replaced under any circumstances. Therefore, we only check for the presence of non-gigantic folios, also adding the gigantic folio check to avoid accidental replacement.
To optimize performance, we skip unnecessary iterations over pfn for compound pages and high-order buddy pages to save processing time.
Quoted 8 lines
A simple test on machine with 114G free memory, allocate 120 * 1G HugeTLB folios(104 successfully returned),
time echo 120 >
/sys/kernel/mm/hugepages/hugepages-1048576kB/nr_hugepagesBefore: 0m0.602s After: 0m0.431s
Thanks, I'll queue this as a -fix, below.
nit: the function has become rather a jumble of bailout styles:
: if (order_is_gigantic(folio_order(folio)))
: return -ENOMEM;
This could have done ret=-ENOMEM;break;
:
: ret = alloc_and_dissolve_hugetlb_folio(folio, &list);
: if (ret)
: break;
While this could have done `return ret;'!
Single-return-point is preferred style, I think. Mainly to help avoid leakage of resources and locks as the code evolves.
How does this look on top?
From: Andrew Morton <[email protected]>
Subject: mm-hugetlb-optimize-replace_free_hugepage_folios-v2-fix
Date: Thu Jan 15 02:34:07 PM PST 2026
use single-return-point style, tweak comment
---
mm/hugetlb.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-)
mm/hugetlb.c renamed from mm/hugetlb.c~mm-hugetlb-optimize-replace_free_hugepage_folios-v2-fix +8 -3
@@ -2834,10 +2834,15 @@ int replace_free_hugepage_folios(unsigne nr =folio_nr_pages (folio) -folio_page_idx (folio, page); -/* Not to disrupt normal path by vainly holding hugetlb_lock */ +/* +* Don't disrupt normal path by vainly holding +* hugetlb_lock +*/ if (folio_test_hugetlb (folio) && !folio_ref_count (folio)) { -if (order_is_gigantic (folio_order (folio))) -return -ENOMEM ; +if (order_is_gigantic (folio_order (folio))) { + ret = -ENOMEM ; +break ; + } ret =alloc_and_dissolve_hugetlb_folio (folio, &list);if (ret)
_
From: Kefeng Wang <[email protected]>
Subject: mm: hugetlb: optimize replace_free_hugepage_folios()
Date: Wed, 14 Jan 2026 21:55:12 +0800
Turn back to use alloc_and_dissolve_hugetlb_folio() since Oscar pointed out that the return value is different. Then adding gigantic folio check independently, also update changelog.
---
mm/hugetlb.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-)
mm/hugetlb.c renamed from mm/hugetlb.c~mm-hugetlb-optimize-replace_free_hugepage_folios-v2 +7 -3
@@ -2810,6 +2810,7 @@ int replace_free_hugepage_folios(unsignestruct page *page;struct hstate *h;LIST_HEAD (list); +int ret =0 ;/* Avoid pfn iterations if no free non-gigantic huge pages */ for_each_hstate (h) {
@@ -2835,9 +2836,13 @@ int replace_free_hugepage_folios(unsigne/* Not to disrupt normal path by vainly holding hugetlb_lock */ if (folio_test_hugetlb (folio) && !folio_ref_count (folio)) { -if (isolate_or_dissolve_huge_folio (folio, &list)) +if (order_is_gigantic (folio_order (folio)))return -ENOMEM ; + ret =alloc_and_dissolve_hugetlb_folio (folio, &list); +if (ret) +break ; +putback_movable_pages (&list); } }else if (PageBuddy (page)) {
@@ -2851,11 +2856,10 @@ int replace_free_hugepage_folios(unsigneif (order <=MAX_PAGE_ORDER ) nr =1UL << order; } - start_pfn += nr; } -return 0 ; +return ret; }void wait_for_freed_hugetlb_folios (void )
_
Re: [PATCH v2 3/5] mm: hugetlb: optimize replace_free_hugepage_folios()
Quoted 34 lines · Andrew Morton
Quoted 9 lines · On Wed, 14 Jan 2026 21:55:12 +0800 Kefeng Wang <[email protected]>…
If no free hugepage folios are available, there is no need to perform any replacement operations. Additionally, gigantic folios should not be replaced under any circumstances. Therefore, we only check for the presence of non-gigantic folios, also adding the gigantic folio check to avoid accidental replacement.
To optimize performance, we skip unnecessary iterations over pfn for compound pages and high-order buddy pages to save processing time.
Quoted 8 lines
A simple test on machine with 114G free memory, allocate 120 * 1G HugeTLB folios(104 successfully returned),
time echo 120 > /sys/kernel/mm/hugepages/hugepages-1048576kB/nr_hugepagesBefore: 0m0.602s After: 0m0.431sThanks, I'll queue this as a -fix, below.
nit: the function has become rather a jumble of bailout styles:
: if (order_is_gigantic(folio_order(folio)))
: return-ENOMEM;This could have done ret=-ENOMEM;break;
:
: ret =alloc_and_dissolve_hugetlb_folio(folio, &list);
: if (ret)
: break;
Quoted 6 lines
While this could have done `return ret;'!
Single-return-point is preferred style, I think. Mainly to help avoid leakage of resources and locks as the code evolves.
How does this look on top?
Hi Andrew, I'm fine with it, thanks your for the update.
Quoted 119 lines
From: Andrew Morton <[email protected]>
Subject: mm-hugetlb-optimize-replace_free_hugepage_folios-v2-fix
Date: Thu Jan 15 02:34:07 PM PST 2026use single-return-point style, tweak comment
Cc Kefeng Wang <[email protected]>Cc Brendan Jackman <[email protected]>Cc David Hildenbrand <[email protected]>Cc Jane Chu <[email protected]>Cc Johannes Weiner <[email protected]>Cc Matthew Wilcox (Oracle) <[email protected]>Cc Muchun Song <[email protected]>Cc Oscar Salvador <[email protected]>Cc Sidhartha Kumar <[email protected]>Cc Vlastimil Babka <[email protected]>Cc Zi Yan <[email protected]>Signed-off-by Andrew Morton <[email protected]>---
mm/hugetlb.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-)mm/hugetlb.c renamed from mm/hugetlb.c~mm-hugetlb-optimize-replace_free_hugepage_folios-v2-fix +8
-3@@ -2834,10 +2834,15 @@ int replace_free_hugepage_folios(unsigne nr =folio_nr_pages (folio) -folio_page_idx (folio, page); -/* Not to disrupt normal path by vainly holding hugetlb_lock */ +/* +* Don't disrupt normal path by vainly holding +* hugetlb_lock +*/ if (folio_test_hugetlb (folio) && !folio_ref_count (folio)) { -if (order_is_gigantic (folio_order (folio))) -return -ENOMEM ; +if (order_is_gigantic (folio_order (folio))) { + ret = -ENOMEM ; +break ; + } ret =alloc_and_dissolve_hugetlb_folio (folio, &list);if (ret)_
From: Kefeng Wang <[email protected]>
Subject: mm: hugetlb: optimizereplace_free_hugepage_folios()
Date: Wed, 14 Jan 2026 21:55:12 +0800Turn back to use
alloc_and_dissolve_hugetlb_folio()since Oscar pointed out that the return value is different. Then adding gigantic folio check independently, also update changelog.Signed-off-by Kefeng Wang <[email protected]>Cc Brendan Jackman <[email protected]>Cc David Hildenbrand <[email protected]>Cc Jane Chu <[email protected]>Cc Johannes Weiner <[email protected]>Cc Matthew Wilcox (Oracle) <[email protected]>Cc Muchun Song <[email protected]>Cc Oscar Salvador <[email protected]>Cc Sidhartha Kumar <[email protected]>Cc Vlastimil Babka <[email protected]>Cc Zi Yan <[email protected]>Signed-off-by Andrew Morton <[email protected]>---
mm/hugetlb.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-)mm/hugetlb.c renamed from mm/hugetlb.c~mm-hugetlb-optimize-replace_free_hugepage_folios-v2 +7
-3@@ -2810,6 +2810,7 @@ int replace_free_hugepage_folios(unsignestruct page *page;struct hstate *h;LIST_HEAD (list); +int ret =0 ;/* Avoid pfn iterations if no free non-gigantic huge pages */ for_each_hstate (h) {@@ -2835,9 +2836,13 @@ int replace_free_hugepage_folios(unsigne/* Not to disrupt normal path by vainly holding hugetlb_lock */ if (folio_test_hugetlb (folio) && !folio_ref_count (folio)) { -if (isolate_or_dissolve_huge_folio (folio, &list)) +if (order_is_gigantic (folio_order (folio)))return -ENOMEM ; + ret =alloc_and_dissolve_hugetlb_folio (folio, &list); +if (ret) +break ; +putback_movable_pages (&list); } }else if (PageBuddy (page)) {@@ -2851,11 +2856,10 @@ int replace_free_hugepage_folios(unsigneif (order <=MAX_PAGE_ORDER ) nr =1UL << order; } - start_pfn += nr; } -return 0 ; +return ret; }void wait_for_freed_hugetlb_folios (void )_
PATCH 4/5 mm: hugetlb_cma: optimize hugetlb_cma_alloc_frozen_folio()
In reply to Kefeng WangCheck hugetlb_cma_size which helps to avoid unnecessary gfp check or nodemask traversal.
--- mm/hugetlb_cma.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
mm/hugetlb_cma.c +4 -1
@@ -16,7 +16,7 @@static struct cma *hugetlb_cma[MAX_NUMNODES ];static unsigned long hugetlb_cma_size_in_node[MAX_NUMNODES ]__initdata ;static bool hugetlb_cma_only; -static unsigned long hugetlb_cma_size__initdata ; +static unsigned long hugetlb_cma_size__ro_after_init ;void hugetlb_cma_free_frozen_folio (struct folio *folio) {
@@ -31,6 +31,9 @@ struct folio *hugetlb_cma_alloc_frozen_folio(int order, gfp_t gfp_mask,struct folio *folio;struct page *page =NULL ; +if (!hugetlb_cma_size) +return NULL ; +if (hugetlb_cma[nid]) page =cma_alloc_frozen_compound (hugetlb_cma[nid], order);
Signature · 1 line
2.27.0
Re: [PATCH 4/5] mm: hugetlb_cma: optimize hugetlb_cma_alloc_frozen_folio()
Quoted 8 lines · Kefeng Wang
Check
hugetlb_cma_sizewhich helps to avoid unnecessary gfp check or nodemask traversal.Signed-off-by Kefeng Wang <[email protected]>--- mm/hugetlb_cma.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
LGTM.
Best Regards,
Yan, Zi
PATCH 5/5 mm: hugetlb_cma: mark hugetlb_cma{_only} as __ro_after_init
In reply to Kefeng WangThe hugetlb_cma and hugetlb_cma_only are initialized once during init and never changed.
--- mm/hugetlb_cma.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
mm/hugetlb_cma.c +2 -2
@@ -13,9 +13,9 @@#include "hugetlb_cma.h" -static struct cma *hugetlb_cma[MAX_NUMNODES ]; +static struct cma *hugetlb_cma[MAX_NUMNODES ]__ro_after_init ;static unsigned long hugetlb_cma_size_in_node[MAX_NUMNODES ]__initdata ; -static bool hugetlb_cma_only; +static bool hugetlb_cma_only__ro_after_init ;static unsigned long hugetlb_cma_size__ro_after_init ;void hugetlb_cma_free_frozen_folio (struct folio *folio)
Signature · 1 line
2.27.0
Re: [PATCH 5/5] mm: hugetlb_cma: mark hugetlb_cma{_only} as __ro_after_init
Quoted 8 lines · Kefeng Wang
The
hugetlb_cmaandhugetlb_cma_onlyare initialized once during init and never changed.Signed-off-by Kefeng Wang <[email protected]>--- mm/hugetlb_cma.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
Sounds reasonable to me.
Best Regards,
Yan, Zi
Re: [PATCH 5/5] mm: hugetlb_cma: mark hugetlb_cma{_only} as __ro_after_init
In reply to Kefeng WangQuoted 8 lines · Kefeng Wang
The
hugetlb_cmaandhugetlb_cma_onlyare initialized once during init and never changed.Signed-off-by Kefeng Wang <[email protected]>