Skip to contentSkip to conversation

mm: hugetlb: allocate frozen gigantic folio

PATCH Superseded Kefeng Wang 5 participants 25 messages Sep 18, 2025 – Oct 9, 2025

Subsystems (5)

v2 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v2 cover mm: hugetlb: allocate frozen gigantic folio

Firstly, optimize pfn_range_valid_contig()/replace_free_hugepage_folios() to accelerate the gigantic folio allocation, time reduced from 2.124s to 0.429s when allocate 200*1G.

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().

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 (8):
mm: page_alloc: optimize pfn_range_valid_contig()
mm: hugetlb: optimize replace_free_hugepage_folios()
mm: debug_vm_pgtable: add debug_vm_pgtable_free_huge_page()
mm: page_alloc: add split_non_compound_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              | 104 +++++++++-----------
 mm/debug_vm_pgtable.c |  38 ++++---
 mm/hugetlb.c          | 103 +++++++++----------
 mm/hugetlb_cma.c      |  27 ++---
 mm/hugetlb_cma.h      |  10 +-
 mm/internal.h         |   6 ++
 mm/page_alloc.c       | 224 +++++++++++++++++++++++++++++-------------
 9 files changed, 318 insertions(+), 272 deletions(-)
Signature · 1 line
2.27.0
#1 lore raw

PATCH v2 1/8 mm: page_alloc: optimize pfn_range_valid_contig()

The alloc_contig_pages() spends a lot of time in pfn_range_valid_contig(), we could check whether the page in this pfn range could be allocated before alloc_contig_range(), if the page can't be migrated, no further action is required, and also skip some unnecessary iterations for compound pages such as THP and non-compound high order buddy, which save times a lot too. The check is racy, but the only danger is skipping too much.

A simple test on machine with 116G free memory, allocate 120 * 1G HugeTLB folios(107 successfully returned),

time echo 120 > /sys/kernel/mm/hugepages/hugepages-1048576kB/nr_hugepages

Before: 0m2.124s
After:  0m0.602s
---
 mm/page_alloc.c | 20 ++++++++++++++++++--
 1 file changed, 18 insertions(+), 2 deletions(-)
mm/page_alloc.c +18 -2
index 478beaf95f84..5b7d705e9710 100644
@@ -7012,6 +7012,7 @@ static bool pfn_range_valid_contig(struct zone *z, unsigned long start_pfn,
 {
 	unsigned long i, end_pfn = start_pfn + nr_pages;
 	struct page *page;
+	struct folio *folio;
 
 	for (i = start_pfn; i < end_pfn; i++) {
 		page = pfn_to_online_page(i);
@@ -7021,11 +7022,26 @@ static bool pfn_range_valid_contig(struct zone *z, unsigned long start_pfn,
 		if (page_zone(page) != z)
 			return false;
 
-		if (PageReserved(page))
+		folio = page_folio(page);
+		if (folio_test_reserved(folio))
 			return false;
 
-		if (PageHuge(page))
+		if (folio_test_hugetlb(folio))
 			return false;
+
+		/* The following type of folios aren't migrated */
+		if (folio_test_pgtable(folio) | folio_test_stack(folio))
+			return false;
+
+		/*
+		 * For compound pages such as THP and non-compound high
+		 * order buddy pages, save potentially a lot of iterations
+		 * if we can skip them at once.
+		 */
+		if (PageCompound(page))
+			i += (1UL << compound_order(page)) - 1;
+		else if (PageBuddy(page))
+			i += (1UL << buddy_order(page)) - 1;
 	}
 	return true;
 }
Signature · 1 line
2.27.0
reviewer
#2 lore raw

Re: [PATCH v2 1/8] mm: page_alloc: optimize pfn_range_valid_contig()

Quoted 37 lines · Kefeng Wang

The alloc_contig_pages() spends a lot of time in pfn_range_valid_contig(), we could check whether the page in this pfn range could be allocated before alloc_contig_range(), if the page can't be migrated, no further action is required, and also skip some unnecessary iterations for compound pages such as THP and non-compound high order buddy, which save times a lot too. The check is racy, but the only danger is skipping too much.

A simple test on machine with 116G free memory, allocate 120 * 1G HugeTLB folios(107 successfully returned),

time echo 120 > /sys/kernel/mm/hugepages/hugepages-1048576kB/nr_hugepages

Before: 0m2.124s
After:  0m0.602s
---
 mm/page_alloc.c | 20 ++++++++++++++++++--
 1 file changed, 18 insertions(+), 2 deletions(-)
mm/page_alloc.c +1 -0
index 478beaf95f84..5b7d705e9710 100644
@@ -7012,6 +7012,7 @@ static bool pfn_range_valid_contig(struct zone *z, unsigned long start_pfn,
 {
 	unsigned long i, end_pfn = start_pfn + nr_pages;
 	struct page *page;
+	struct folio *folio;
 
 	for (i = start_pfn; i < end_pfn; i++) {
 		page = pfn_to_online_page(i);
@@ -7021,11 +7022,26 @@ static bool pfn_range_valid_contig(struct zone *z, unsigned long start_pfn,
 		if (page_zone(page) != z)
 			return false;
 

mm/page_alloc.c static bool pfn_range_valid_contig(struct zone *z, unsigned long start_pfn, · line 7032

Quoted 10 lines
mm/page_alloc.c +5 -2
-		if (PageReserved(page))
+		folio = page_folio(page);
+		if (folio_test_reserved(folio))
 			return false;
 
-		if (PageHuge(page))
+		if (folio_test_hugetlb(folio))
 			return false;
+
+		/* The following type of folios aren't migrated */

s/aren’t/cannot be/

mm/page_alloc.c static bool pfn_range_valid_contig(struct zone *z, unsigned long start_pfn, · line 7034

Quoted 2 lines
mm/page_alloc.c +2 -0
+		if (folio_test_pgtable(folio) | folio_test_stack(folio))
+			return false;

Maybe worth explicitly stating these two types of pages in the commit log.

mm/page_alloc.c static bool pfn_range_valid_contig(struct zone *z, unsigned long start_pfn, · line 7042

Quoted 8 lines
mm/page_alloc.c +8 -0
+
+		/*
+		 * For compound pages such as THP and non-compound high
+		 * order buddy pages, save potentially a lot of iterations
+		 * if we can skip them at once.
+		 */
+		if (PageCompound(page))
+			i += (1UL << compound_order(page)) - 1;

Just a note here, if page is tail, this just move i to the next page instead of next folio.

Quoted 7 lines
mm/page_alloc.c +2 -0
+		else if (PageBuddy(page))
+			i += (1UL << buddy_order(page)) - 1;
 	}
 	return true;
 }
Signature · 1 line
2.27.0

Otherwise, LGTM. Reviewed-by: Zi Yan <[email protected]>

Best Regards,
Yan, Zi

#3 lore raw

Re: [PATCH v2 1/8] mm: page_alloc: optimize pfn_range_valid_contig()

Quoted 44 lines · Zi Yan
Quoted 42 lines · Kefeng Wang

The alloc_contig_pages() spends a lot of time in pfn_range_valid_contig(), we could check whether the page in this pfn range could be allocated before alloc_contig_range(), if the page can't be migrated, no further action is required, and also skip some unnecessary iterations for compound pages such as THP and non-compound high order buddy, which save times a lot too. The check is racy, but the only danger is skipping too much.

A simple test on machine with 116G free memory, allocate 120 * 1G HugeTLB folios(107 successfully returned),

   time echo 120 > /sys/kernel/mm/hugepages/hugepages-1048576kB/nr_hugepages
Before: 0m2.124s
After:  0m0.602s
---
  mm/page_alloc.c | 20 ++++++++++++++++++--
  1 file changed, 18 insertions(+), 2 deletions(-)
mm/page_alloc.c +3 -1
index 478beaf95f84..5b7d705e9710 100644
@@ -7012,6 +7012,7 @@ static bool pfn_range_valid_contig(struct zone *z, unsigned long start_pfn,
  {
  	unsigned long i, end_pfn = start_pfn + nr_pages;
  	struct page *page;
+	struct folio *folio;
 
  	for (i = start_pfn; i < end_pfn; i++) {
  		page = pfn_to_online_page(i);
@@ -7021,11 +7022,26 @@ static bool pfn_range_valid_contig(struct zone *z, unsigned long start_pfn,
  		if (page_zone(page) != z)
  			return false;
 
-		if (PageReserved(page))
+		folio = page_folio(page);
+		if (folio_test_reserved(folio))
  			return false;
 
Quoted 7 lines

mm/page_alloc.c static bool pfn_range_valid_contig(struct zone *z, unsigned long start_pfn, · line 7032

Quoted 5 lines
mm/page_alloc.c +3 -1
-		if (PageHuge(page))
+		if (folio_test_hugetlb(folio))
  			return false;
+
+		/* The following type of folios aren't migrated */

s/aren’t/cannot be/

ACK.

Quoted 3 lines

mm/page_alloc.c static bool pfn_range_valid_contig(struct zone *z, unsigned long start_pfn, · line 7034

Quoted 3 lines
mm/page_alloc.c +2 -0
+		if (folio_test_pgtable(folio) | folio_test_stack(folio))
+			return false;

should be "||", will fix.

Quoted 3 lines

Maybe worth explicitly stating these two types of pages in the commit log.

OK, will update.

Quoted 11 lines

mm/page_alloc.c static bool pfn_range_valid_contig(struct zone *z, unsigned long start_pfn, · line 7042

Quoted 8 lines
mm/page_alloc.c +8 -0
+
+		/*
+		 * For compound pages such as THP and non-compound high
+		 * order buddy pages, save potentially a lot of iterations
+		 * if we can skip them at once.
+		 */
+		if (PageCompound(page))
+			i += (1UL << compound_order(page)) - 1;

Just a note here, if page is tail, this just move i to the next page instead of next folio.

As no reference held,it is not too precise but we optimize for most scenarios.

Quoted 12 lines
Quoted 8 lines
mm/page_alloc.c +2 -0
+		else if (PageBuddy(page))
+			i += (1UL << buddy_order(page)) - 1;
  	}
  	return true;
  }
Signature · 1 line
2.27.0

Otherwise, LGTM. Reviewed-by: Zi Yan <[email protected]>

Thanks.

Quoted 3 lines

Best Regards,
Yan, Zi

#4 lore raw

Re: [PATCH v2 1/8] mm: page_alloc: optimize pfn_range_valid_contig()

In reply to Kefeng Wang
kernel test robot noticed the following build warnings:

Hi Kefeng,

kernel test robot noticed the following build warnings:

[auto build test WARNING on akpm-mm/mm-everything]

url:    https://github.com/intel-lab-lkp/linux/commits/Kefeng-Wang/mm-page_alloc-optimize-pfn_range_valid_contig/20250918-212431
base:   https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-everything
patch link:    https://lore.kernel.org/r/20250918132000.1951232-2-wangkefeng.wang%40huawei.com
patch subject: [PATCH v2 1/8] mm: page_alloc: optimize pfn_range_valid_contig()
config: x86_64-kexec (https://download.01.org/0day-ci/archive/20250919/[email protected]/config)
compiler: clang version 20.1.8 (https://github.com/llvm/llvm-project 87f0227cb60147a26a1eeb4fb06e3b505e9c7261)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250919/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

>> mm/page_alloc.c:7033:7: warning: use of bitwise '|' with boolean operands [-Wbitwise-instead-of-logical]
    7033 |                 if (folio_test_pgtable(folio) | folio_test_stack(folio))
         |                     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
         |                                               ||

mm/page_alloc.c:7033:7: note: cast one or both operands to int to silence this warning 1 warning generated.

vim +7033 mm/page_alloc.c

7009
7010 static bool pfn_range_valid_contig(struct zone *z, unsigned long start_pfn,
7011 unsigned long nr_pages)
7012 {
7013 unsigned long i, end_pfn = start_pfn + nr_pages;
7014 struct page *page;
7015 struct folio *folio;
7016
7017 for (i = start_pfn; i < end_pfn; i++) {
7018 page = pfn_to_online_page(i);
7019 if (!page)
7020 return false;
7021
7022 if (page_zone(page) != z)
7023 return false;
7024
7025 folio = page_folio(page);
7026 if (folio_test_reserved(folio))
7027 return false;
7028
7029 if (folio_test_hugetlb(folio))
7030 return false;
7031
7032 /* The following type of folios aren't migrated */

> 7033			if (folio_test_pgtable(folio) | folio_test_stack(folio))

7034 return false;
7035
7036 /*
7037 * For compound pages such as THP and non-compound high
7038 * order buddy pages, save potentially a lot of iterations
7039 * if we can skip them at once.
7040 */
7041 if (PageCompound(page))
7042 i += (1UL << compound_order(page)) - 1;
7043 else if (PageBuddy(page))
7044 i += (1UL << buddy_order(page)) - 1;
7045 }
7046 return true;
7047 }
7048

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#5 lore raw

Re: [PATCH v2 1/8] mm: page_alloc: optimize pfn_range_valid_contig()

In reply to Kefeng Wang
Quoted 47 lines · Kefeng Wang

The alloc_contig_pages() spends a lot of time in pfn_range_valid_contig(), we could check whether the page in this pfn range could be allocated before alloc_contig_range(), if the page can't be migrated, no further action is required, and also skip some unnecessary iterations for compound pages such as THP and non-compound high order buddy, which save times a lot too. The check is racy, but the only danger is skipping too much.

A simple test on machine with 116G free memory, allocate 120 * 1G HugeTLB folios(107 successfully returned),

   time echo 120 > /sys/kernel/mm/hugepages/hugepages-1048576kB/nr_hugepages
Before: 0m2.124s
After:  0m0.602s
---
  mm/page_alloc.c | 20 ++++++++++++++++++--
  1 file changed, 18 insertions(+), 2 deletions(-)
mm/page_alloc.c +6 -2
index 478beaf95f84..5b7d705e9710 100644
@@ -7012,6 +7012,7 @@ static bool pfn_range_valid_contig(struct zone *z, unsigned long start_pfn,
  {
  	unsigned long i, end_pfn = start_pfn + nr_pages;
  	struct page *page;
+	struct folio *folio;
 
  	for (i = start_pfn; i < end_pfn; i++) {
  		page = pfn_to_online_page(i);
@@ -7021,11 +7022,26 @@ static bool pfn_range_valid_contig(struct zone *z, unsigned long start_pfn,
  		if (page_zone(page) != z)
  			return false;
 
-		if (PageReserved(page))
+		folio = page_folio(page);
+		if (folio_test_reserved(folio))
  			return false;
 
-		if (PageHuge(page))
+		if (folio_test_hugetlb(folio))
  			return false;
+
+		/* The following type of folios aren't migrated */

mm/page_alloc.c static bool pfn_range_valid_contig(struct zone *z, unsigned long start_pfn, · line 7042

Quoted 10 lines
mm/page_alloc.c +10 -0
+		if (folio_test_pgtable(folio) | folio_test_stack(folio))
+			return false;
+
+		/*
+		 * For compound pages such as THP and non-compound high
+		 * order buddy pages, save potentially a lot of iterations
+		 * if we can skip them at once.
+		 */
+		if (PageCompound(page))
+			i += (1UL << compound_order(page)) - 1;

Can we instead do
if (folio_test_large(folio))

    i += folio_nr_pages(folio) - folio_page_idx(folio, page).
Quoted 5 lines
mm/page_alloc.c +2 -0
+		else if (PageBuddy(page))
+			i += (1UL << buddy_order(page)) - 1;
  	}
  	return true;
  }
#6 lore raw

Re: [PATCH v2 1/8] mm: page_alloc: optimize pfn_range_valid_contig()

Quoted 54 lines · Dev Jain
Quoted 52 lines · Kefeng Wang

The alloc_contig_pages() spends a lot of time in pfn_range_valid_contig(), we could check whether the page in this pfn range could be allocated before alloc_contig_range(), if the page can't be migrated, no further action is required, and also skip some unnecessary iterations for compound pages such as THP and non-compound high order buddy, which save times a lot too. The check is racy, but the only danger is skipping too much.

A simple test on machine with 116G free memory, allocate 120 * 1G HugeTLB folios(107 successfully returned),

   time echo 120 > /sys/kernel/mm/hugepages/hugepages-1048576kB/ nr_hugepages

Before: 0m2.124s
After:  0m0.602s

---
  mm/page_alloc.c | 20 ++++++++++++++++++--

  1 file changed, 18 insertions(+), 2 deletions(-)
mm/page_alloc.c +12 -2
index 478beaf95f84..5b7d705e9710 100644
@@ -7012,6 +7012,7 @@ static bool pfn_range_valid_contig(struct zone 
 *z, unsigned long start_pfn,
   {
       unsigned long i, end_pfn = start_pfn + nr_pages;
       struct page *page;
+    struct folio *folio;
       for (i = start_pfn; i < end_pfn; i++) {
           page = pfn_to_online_page(i);
@@ -7021,11 +7022,26 @@ static bool pfn_range_valid_contig(struct zone 
 *z, unsigned long start_pfn,
           if (page_zone(page) != z)
               return false;
-        if (PageReserved(page))
+        folio = page_folio(page);
+        if (folio_test_reserved(folio))
               return false;
-        if (PageHuge(page))
+        if (folio_test_hugetlb(folio))
               return false;
+
+        /* The following type of folios aren't migrated */
+        if (folio_test_pgtable(folio) | folio_test_stack(folio))
+            return false;
+
+        /*
+         * For compound pages such as THP and non-compound high
+         * order buddy pages, save potentially a lot of iterations
Quoted 8 lines

mm/page_alloc.c static bool pfn_range_valid_contig(struct zone *z, unsigned long start_pfn, · line 7042

Quoted 4 lines
mm/page_alloc.c +4 -0
+         * if we can skip them at once.
+         */
+        if (PageCompound(page))
+            i += (1UL << compound_order(page)) - 1;

Can we instead do
if (folio_test_large(folio))
    i += folio_nr_pages(folio) - folio_page_idx(folio, page).

I'm afraid not, see 9342bc134ae7 ("mm/memory_hotplug: fix call folio_test_large with tail page in do_migrate_range").

#7 lore raw

Re: [PATCH v2 1/8] mm: page_alloc: optimize pfn_range_valid_contig()

In reply to Kefeng Wang
Quoted 40 lines · Kefeng Wang

The alloc_contig_pages() spends a lot of time in pfn_range_valid_contig(), we could check whether the page in this pfn range could be allocated before alloc_contig_range(), if the page can't be migrated, no further action is required, and also skip some unnecessary iterations for compound pages such as THP and non-compound high order buddy, which save times a lot too. The check is racy, but the only danger is skipping too much.

A simple test on machine with 116G free memory, allocate 120 * 1G HugeTLB folios(107 successfully returned),

   time echo 120 > /sys/kernel/mm/hugepages/hugepages-1048576kB/nr_hugepages
Before: 0m2.124s
After:  0m0.602s
---
  mm/page_alloc.c | 20 ++++++++++++++++++--
  1 file changed, 18 insertions(+), 2 deletions(-)
mm/page_alloc.c +3 -1
index 478beaf95f84..5b7d705e9710 100644
@@ -7012,6 +7012,7 @@ static bool pfn_range_valid_contig(struct zone *z, unsigned long start_pfn,
  {
  	unsigned long i, end_pfn = start_pfn + nr_pages;
  	struct page *page;
+	struct folio *folio;
 
  	for (i = start_pfn; i < end_pfn; i++) {
  		page = pfn_to_online_page(i);
@@ -7021,11 +7022,26 @@ static bool pfn_range_valid_contig(struct zone *z, unsigned long start_pfn,
  		if (page_zone(page) != z)
  			return false;
 
-		if (PageReserved(page))
+		folio = page_folio(page);
+		if (folio_test_reserved(folio))

mm/page_alloc.c static bool pfn_range_valid_contig(struct zone *z, unsigned long start_pfn, · line 7035

Quoted 10 lines
mm/page_alloc.c +6 -1
  			return false;
 
-		if (PageHuge(page))
+		if (folio_test_hugetlb(folio))
  			return false;
+
+		/* The following type of folios aren't migrated */
+		if (folio_test_pgtable(folio) | folio_test_stack(folio))
+			return false;
+

I don't enjoy us open coding this here. has_unmovable_pages() has a much better heuristics.

I suggest you drop this patch for now from this series, as it seems to be independent from the rest, and instead see if you could reuse some of the has_unmovable_pages() logic instead.

Signature · 3 lines
Cheers

David / dhildenb
#8 lore raw

Re: [PATCH v2 1/8] mm: page_alloc: optimize pfn_range_valid_contig()

Quoted 53 lines · David Hildenbrand
Quoted 39 lines · Kefeng Wang

The alloc_contig_pages() spends a lot of time in pfn_range_valid_contig(), we could check whether the page in this pfn range could be allocated before alloc_contig_range(), if the page can't be migrated, no further action is required, and also skip some unnecessary iterations for compound pages such as THP and non-compound high order buddy, which save times a lot too. The check is racy, but the only danger is skipping too much.

A simple test on machine with 116G free memory, allocate 120 * 1G HugeTLB folios(107 successfully returned),

   time echo 120 > /sys/kernel/mm/hugepages/hugepages-1048576kB/ nr_hugepages

Before: 0m2.124s
After:  0m0.602s

---
  mm/page_alloc.c | 20 ++++++++++++++++++--

  1 file changed, 18 insertions(+), 2 deletions(-)
mm/page_alloc.c +2 -1
index 478beaf95f84..5b7d705e9710 100644
@@ -7012,6 +7012,7 @@ static bool pfn_range_valid_contig(struct zone 
 *z, unsigned long start_pfn,
   {
       unsigned long i, end_pfn = start_pfn + nr_pages;
       struct page *page;
+    struct folio *folio;
       for (i = start_pfn; i < end_pfn; i++) {
           page = pfn_to_online_page(i);
@@ -7021,11 +7022,26 @@ static bool pfn_range_valid_contig(struct zone 
 *z, unsigned long start_pfn,
           if (page_zone(page) != z)
               return false;
-        if (PageReserved(page))
+        folio = page_folio(page);

mm/page_alloc.c static bool pfn_range_valid_contig(struct zone · line 7034

Quoted 10 lines
mm/page_alloc.c +7 -1
+        if (folio_test_reserved(folio))
               return false;
-        if (PageHuge(page))
+        if (folio_test_hugetlb(folio))
               return false;
+
+        /* The following type of folios aren't migrated */
+        if (folio_test_pgtable(folio) | folio_test_stack(folio))
+            return false;
+

I don't enjoy us open coding this here. has_unmovable_pages() has a much better heuristics.

Quoted 2 lines

I suggest you drop this patch for now from this series, as it seems to be independent from the rest, and instead see if you could reuse some of the has_unmovable_pages() logic instead.

OK,I will try to check if has_unmovable_pages() could be used. The new patches are added when I test alloc_contig_pages/ alloc_contig_frozen_pages with different GFP flags.

Let me remove them and resend them separately.

[1] https://lore.kernel.org/linux-mm/[email protected]/

#9 lore raw

PATCH v2 2/8 mm: hugetlb: optimize replace_free_hugepage_folios()

In reply to Kefeng Wang

No need to replace free hugepage folios if no free hugetlb folios, we don't replace gigantic folio, so use isolate_or_dissolve_huge_folio(), also skip some pfn iterations for compound pages such as THP and non-compound high order buddy to save time.

A simple test on machine with 116G free memory, allocate 120 * 1G HugeTLB folios(107 successfully returned),

time echo 120 > /sys/kernel/mm/hugepages/hugepages-1048576kB/nr_hugepages

Before: 0m0.602s
After:  0m0.429s
---
 mm/hugetlb.c | 49 +++++++++++++++++++++++++++++++++++++------------
 1 file changed, 37 insertions(+), 12 deletions(-)
mm/hugetlb.c +37 -12
index 1806685ea326..bc88b659a88b 100644
@@ -2890,26 +2890,51 @@ 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);
+
+	/* Avoid pfn iterations if no free non-gigantic huge pages */
+	for_each_hstate(h) {
+		if (!hstate_is_gigantic(h))
+			nr += h->free_huge_pages;
+	}
 
-	LIST_HEAD(isolate_list);
+	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 = 1UL << compound_order(page);
 
-			putback_movable_pages(&isolate_list);
+			if (folio_test_hugetlb(folio) && !folio_ref_count(folio)) {
+				if (isolate_or_dissolve_huge_folio(folio, &list))
+					return -ENOMEM;
+
+				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
#10 lore raw

Re: [PATCH v2 2/8] mm: hugetlb: optimize replace_free_hugepage_folios()

Quoted 5 lines · Kefeng Wang

No need to replace free hugepage folios if no free hugetlb folios, we don't replace gigantic folio, so use isolate_or_dissolve_huge_folio(), also skip some pfn iterations for compound pages such as THP and non-compound high order buddy to save time.

Quoted 7 lines

A simple test on machine with 116G free memory, allocate 120 * 1G HugeTLB folios(107 successfully returned),

   time echo 120 > /sys/kernel/mm/hugepages/hugepages-1048576kB/nr_hugepages
Before: 0m0.602s
After:  0m0.429s

Also this patch feels misplaced in this series. I suggest you send that out separately.

Or is there anything important that I am missing?

Signature · 3 lines
Cheers

David / dhildenb
#11 lore raw

Re: [PATCH v2 2/8] mm: hugetlb: optimize replace_free_hugepage_folios()

Quoted 9 lines · David Hildenbrand
Quoted 8 lines · Kefeng Wang

No need to replace free hugepage folios if no free hugetlb folios, we don't replace gigantic folio, so use isolate_or_dissolve_huge_folio(), also skip some pfn iterations for compound pages such as THP and non-compound high order buddy to save time.

A simple test on machine with 116G free memory, allocate 120 * 1G HugeTLB folios(107 successfully returned),

Quoted 9 lines
Quoted 4 lines

   time echo 120 > /sys/kernel/mm/hugepages/hugepages-1048576kB/ nr_hugepages

Before: 0m0.602s
After:  0m0.429s

Also this patch feels misplaced in this series. I suggest you send that out separately.

Or is there anything important that I am missing?

Sure, let me do it separately.

#12 lore raw

PATCH v2 3/8 mm: debug_vm_pgtable: add debug_vm_pgtable_free_huge_page()

In reply to Kefeng Wang

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
index 0ceb4e09306c..1fefb63e0480 100644
@@ -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
index 830107b6dd08..d7f82aa58711 100644
@@ -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
index 5b7d705e9710..b6eeae39f4d0 100644
@@ -7113,7 +7113,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)
 {
@@ -7140,6 +7139,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
Acked-by on v2 3/8
#13 lore raw

Re: [PATCH v2 3/8] mm: debug_vm_pgtable: add debug_vm_pgtable_free_huge_page()

Quoted 9 lines · Kefeng Wang

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.

---

Signature · 3 lines
Cheers

David / dhildenb
#14 lore raw

PATCH v2 4/8 mm: page_alloc: add split_non_compound_page()

In reply to Kefeng Wang

Add new split_non_compound_page() to simplify make_alloc_exact().

---
 mm/page_alloc.c | 19 ++++++++++++-------
 1 file changed, 12 insertions(+), 7 deletions(-)
mm/page_alloc.c +12 -7
index b6eeae39f4d0..e1d229b75f27 100644
@@ -3042,6 +3042,15 @@ void free_unref_folios(struct folio_batch *folios)
 	folio_batch_reinit(folios);
 }
 
+static void split_non_compound_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_non_compound_page(page, order);
 }
 EXPORT_SYMBOL_GPL(split_page);
 
@@ -5315,9 +5322,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_non_compound_page(page, order);
 		while (page < --last)
 			set_page_refcounted(last);
 
Signature · 1 line
2.27.0
#15 lore raw

Re: [PATCH v2 4/8] mm: page_alloc: add split_non_compound_page()

Quoted 2 lines · Kefeng Wang

Add new split_non_compound_page() to simplify make_alloc_exact().

"Factor out the splitting of non-compound page from make_alloc_exact() and split_page() into a new helper function split_non_compound_page()".

Not sure I enjoy the name "split_non_compound_page()", but it matches the existing theme of split_page(): we're not really splitting any pages, we're just adjusting tracking metadata for pages part of the original-higher-order-page so it can be freed separately later.

But now I think of it, the terminology is bad if you look at the description of split_page(): "split_page takes a non-compound higher-order page, and splits it into n (1<<order) sub-pages: page[0..n]". It's unclear how split_non_compound_page() would really differ.

I would suggest you call the new helper simply "__split_page" ?

Signature · 3 lines
Cheers

David / dhildenb
#16 lore raw

Re: [PATCH v2 4/8] mm: page_alloc: add split_non_compound_page()

Quoted 6 lines · David Hildenbrand
Quoted 2 lines · Kefeng Wang

Add new split_non_compound_page() to simplify make_alloc_exact().

"Factor out the splitting of non-compound page from make_alloc_exact() and split_page() into a new helper function split_non_compound_page()".

Thanks, will update the changelog.

Quoted 7 lines

Not sure I enjoy the name "split_non_compound_page()", but it matches the existing theme of split_page(): we're not really splitting any pages, we're just adjusting tracking metadata for pages part of the original-higher-order-page so it can be freed separately later.

But now I think of it, the terminology is bad if you look at the description of split_page(): "split_page takes a non-compound higher- order page, and splits it into n (1<<order) sub-pages: page[0..n]". It's unclear how split_non_compound_page() would really differ.

I would suggest you call the new helper simply "__split_page" ?

OK, the naming always beats me, will change it.

#17 lore raw

PATCH v2 5/8 mm: page_alloc: add alloc_contig_{range_frozen,frozen_pages}()

In reply to 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 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
index 1fefb63e0480..fbbdd8c88483 100644
@@ -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
#18 lore raw

PATCH v2 6/8 mm: cma: add __cma_release()

In reply to Kefeng Wang

Kill 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            | 57 ++++++++++++---------------------------------
 2 files changed, 15 insertions(+), 43 deletions(-)
include/linux/cma.h +0 -1
index 62d9c1cf6326..e5745d2aec55 100644
@@ -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 +15 -42

92 lines not on this page · raw message

Signature · 1 line
2.27.0
#19 lore raw

Re: [PATCH v2 6/8] mm: cma: add __cma_release()

Quoted 46 lines · Kefeng Wang

Kill 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            | 57 ++++++++++++---------------------------------
  2 files changed, 15 insertions(+), 43 deletions(-)
include/linux/cma.h +0 -1
index 62d9c1cf6326..e5745d2aec55 100644
@@ -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 +4 -3
index 813e6dc7b095..2af8c5bc58dd 100644
@@ -942,34 +942,36 @@ 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;

mm/cma.c struct folio *cma_alloc_folio(struct cma *cma, int order, gfp_t gfp) · line 962

Quoted 10 lines
mm/cma.c +1 -3
 
  	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;
+		if (pfn >= cmr->base_pfn && pfn < end && pfn + count <= end)

Are you afraid of overflows here, or why can't it simply be

	if (pfn >= cmr->base_pfn && pfn + count <= end)

But I wonder if we want to keep here

	if (pfn >= cmr->base_pfn && pfn < end)

And VM_WARN if the area does not completely fit into the range. See below.

mm/cma.c struct folio *cma_alloc_folio(struct cma *cma, int order, gfp_t gfp) · line 967

Quoted 9 lines
mm/cma.c +2 -4
  			break;
-		}
  	}
 
-	if (!ret)
-		pr_debug("%s(page %p, count %lu)\n",
-				__func__, (void *)pages, count);
+	if (r == cma->nranges)
+		return false;

Would we want to warn one way or the other in that case? Is it valid that someone tries to free a wrong range?

Note that the original code had this pr_debug() in case no range for the start pfn was found (IIUC, it's confusing) and this VM_BUG_ON(end_pfn > cmr->base_pfn + cmr->count) in case a range was found but it would not completely match the

You're not discussing that behavioral change in the changelog, and I think we would want to keep some sanity checks, likely in a VM_WARN_ON_ONCE() form.

Signature · 3 lines
Cheers

David / dhildenb
#20 lore raw

Re: [PATCH v2 6/8] mm: cma: add __cma_release()

Quoted 77 lines · David Hildenbrand
Quoted 45 lines · Kefeng Wang

Kill 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            | 57 ++++++++++++---------------------------------

  2 files changed, 15 insertions(+), 43 deletions(-)
include/linux/cma.h +0 -1
index 62d9c1cf6326..e5745d2aec55 100644
@@ -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 +4 -3
index 813e6dc7b095..2af8c5bc58dd 100644
@@ -942,34 +942,36 @@ 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);

mm/cma.c struct folio *cma_alloc_folio(struct cma *cma, · line 960

Quoted 10 lines
mm/cma.c +1 -3
       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;
+        if (pfn >= cmr->base_pfn && pfn < end && pfn + count <= end)

Are you afraid of overflows here, or why can't it simply be

    if (pfn >= cmr->base_pfn && pfn + count <= end)

But I wonder if we want to keep here

    if (pfn >= cmr->base_pfn && pfn < end)

And VM_WARN if the area does not completely fit into the range. See below.

mm/cma.c struct folio *cma_alloc_folio(struct cma *cma, · line 964

Quoted 8 lines
mm/cma.c +2 -4
               break;
-        }
       }
-    if (!ret)
-        pr_debug("%s(page %p, count %lu)\n",
-                __func__, (void *)pages, count);
+    if (r == cma->nranges)
+        return false;
Quoted 1 line

Would we want to warn one way or the other in that case? Is it valid that someone tries to free a wrong range?

The original cma_pages_valid() check start pfn whether it is in cma range or not, and the range must be within the complete cma range. The repeatedly check "VM_BUG_ON(pfn + count > end)" in cma_release() is never performed since we return early after cma_pages_valid().

Quoted 2 lines

Note that the original code had this pr_debug() in case no range for the start pfn was found (IIUC, it's confusing) and this  VM_BUG_ON(end_pfn > cmr->base_pfn + cmr->count) in case a range was found but it would not completely match the

So the VM_BUG_ON is not useful,

Quoted 4 lines

You're not discussing that behavioral change in the changelog, and I think we would want to keep some sanity checks, likely in a VM_WARN_ON_ONCE() form.

But for the error path, adding some debug info is better, a quick diff based on this patch, what do you think?

mm/cma.c +10 -3
index 2af8c5bc58dd..88016f4aef7f 100644
@@ -959,12 +959,19 @@ static bool __cma_release(struct cma *cma, const struct page *pages,
        for (r = 0; r < cma->nranges; r++) {
                cmr = &cma->ranges[r];
                end = cmr->base_pfn + cmr->count;
-               if (pfn >= cmr->base_pfn && pfn < end && pfn + count <= end)
-                       break;
+               if (pfn >= cmr->base_pfn && pfn < end) {
+                       if (pfn + count <= end)
+                               break;
+
+                       VM_WARN_ON_ONCE(1);
+               }
        }
 
-       if (r == cma->nranges)
+       if (r == cma->nranges) {
+               pr_debug("%s(no cma range match the page %p)\n",
+                        __func__, (void *)pages);
                return false;
+       }
 
        free_contig_range(pfn, count);
        cma_clear_bitmap(cma, cmr, pfn, count);
#21 lore raw

PATCH v2 7/8 mm: cma: add cma_alloc_frozen{_compound}()

In reply to Kefeng Wang

Introduce 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
index e5745d2aec55..4981c151ef84 100644
@@ -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
index e8e4dc7182d5..fc41f3b949f8 100644
@@ -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
index 1561fc2ff5b8..ffcfde60059e 100644
@@ -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
#22 lore raw

PATCH v2 8/8 mm: hugetlb: allocate frozen pages in alloc_gigantic_folio()

In reply to Kefeng Wang

The 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        | 54 ++++++++++-----------------------------------
 mm/hugetlb_cma.c    | 11 +++++----
 mm/hugetlb_cma.h    | 10 ++++-----
 4 files changed, 22 insertions(+), 76 deletions(-)
include/linux/gfp.h +0 -23
index fbbdd8c88483..82aba162f352 100644
@@ -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 +12 -42

81 lines not on this page · raw message

mm/hugetlb_cma.c +5 -6
index fc41f3b949f8..af9caaf007e4 100644
@@ -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
index 2c2ec8a7e134..3bc295c8c38e 100644
@@ -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
#23 lore raw

Re: [PATCH v2 8/8] mm: hugetlb: allocate frozen pages in alloc_gigantic_folio()

kernel test robot noticed the following build errors:

Hi Kefeng,

kernel test robot noticed the following build errors:

[auto build test ERROR on akpm-mm/mm-everything]

url:    https://github.com/intel-lab-lkp/linux/commits/Kefeng-Wang/mm-page_alloc-optimize-pfn_range_valid_contig/20250918-212431
base:   https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-everything
patch link:    https://lore.kernel.org/r/20250918132000.1951232-9-wangkefeng.wang%40huawei.com
patch subject: [PATCH v2 8/8] mm: hugetlb: allocate frozen pages in alloc_gigantic_folio()
config: riscv-randconfig-001-20250919 (https://download.01.org/0day-ci/archive/20250919/[email protected]/config)
compiler: riscv64-linux-gcc (GCC) 10.5.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250919/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All error/warnings (new ones prefixed by >>):

mm/hugetlb.c: In function 'alloc_gigantic_folio':

>> mm/hugetlb.c:1497:26: error: implicit declaration of function 'alloc_contig_frozen_pages'; did you mean 'alloc_frozen_pages'? [-Werror=implicit-function-declaration]
    1497 |  folio = (struct folio *)alloc_contig_frozen_pages(1 << order, gfp_mask,
         |                          ^~~~~~~~~~~~~~~~~~~~~~~~~
         |                          alloc_frozen_pages
>> mm/hugetlb.c:1497:10: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]

1497 | folio = (struct folio *)alloc_contig_frozen_pages(1 << order, gfp_mask,
| ^
cc1: some warnings being treated as errors

vim +1497 mm/hugetlb.c

1471
1472 #define for_each_node_mask_to_alloc(next_node, nr_nodes, node, mask) \
1473 for (nr_nodes = nodes_weight(*mask); \
1474 nr_nodes > 0 && \
1475 ((node = hstate_next_node_to_alloc(next_node, mask)) || 1); \
1476 nr_nodes--)
1477
1478 #define for_each_node_mask_to_free(hs, nr_nodes, node, mask) \
1479 for (nr_nodes = nodes_weight(*mask); \
1480 nr_nodes > 0 && \
1481 ((node = hstate_next_node_to_free(hs, mask)) || 1); \
1482 nr_nodes--)
1483
1484 #ifdef CONFIG_ARCH_HAS_GIGANTIC_PAGE
1485 static struct folio *alloc_gigantic_folio(int order, gfp_t gfp_mask,
1486 int nid, nodemask_t *nodemask)
1487 {
1488 struct folio *folio;
1489
1490 folio = hugetlb_cma_alloc_frozen_folio(order, gfp_mask, nid, nodemask);
1491 if (folio)
1492 return folio;
1493
1494 if (hugetlb_cma_exclusive_alloc())
1495 return NULL;
1496

> 1497		folio = (struct folio *)alloc_contig_frozen_pages(1 << order, gfp_mask,

1498 nid, nodemask);
1499 return folio;
1500 }
1501 #else /* !CONFIG_ARCH_HAS_GIGANTIC_PAGE */
1502 static struct folio *alloc_gigantic_folio(int order, gfp_t gfp_mask, int nid,
1503 nodemask_t *nodemask)
1504 {
1505 return NULL;
1506 }
1507 #endif
1508

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#24 lore raw

Re: [PATCH v2 8/8] mm: hugetlb: allocate frozen pages in alloc_gigantic_folio()

In reply to Kefeng Wang
kernel test robot noticed the following build warnings:

Hi Kefeng,

kernel test robot noticed the following build warnings:

[auto build test WARNING on akpm-mm/mm-everything]

url:    https://github.com/intel-lab-lkp/linux/commits/Kefeng-Wang/mm-page_alloc-optimize-pfn_range_valid_contig/20250918-212431
base:   https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-everything
patch link:    https://lore.kernel.org/r/20250918132000.1951232-9-wangkefeng.wang%40huawei.com
patch subject: [PATCH v2 8/8] mm: hugetlb: allocate frozen pages in alloc_gigantic_folio()
config: riscv-defconfig (https://download.01.org/0day-ci/archive/20250919/[email protected]/config)
compiler: clang version 22.0.0git (https://github.com/llvm/llvm-project 7c861bcedf61607b6c087380ac711eb7ff918ca6)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250919/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

mm/hugetlb.c:1497:26: error: call to undeclared function 'alloc_contig_frozen_pages'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
1497 | folio = (struct folio *)alloc_contig_frozen_pages(1 << order, gfp_mask,
| ^

>> mm/hugetlb.c:1497:10: warning: cast to 'struct folio *' from smaller integer type 'int' [-Wint-to-pointer-cast]
    1497 |         folio = (struct folio *)alloc_contig_frozen_pages(1 << order, gfp_mask,
         |                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    1498 |                                                           nid, nodemask);
         |                                                           ~~~~~~~~~~~~~~

1 warning and 1 error generated.

vim +1497 mm/hugetlb.c

1471
1472 #define for_each_node_mask_to_alloc(next_node, nr_nodes, node, mask) \
1473 for (nr_nodes = nodes_weight(*mask); \
1474 nr_nodes > 0 && \
1475 ((node = hstate_next_node_to_alloc(next_node, mask)) || 1); \
1476 nr_nodes--)
1477
1478 #define for_each_node_mask_to_free(hs, nr_nodes, node, mask) \
1479 for (nr_nodes = nodes_weight(*mask); \
1480 nr_nodes > 0 && \
1481 ((node = hstate_next_node_to_free(hs, mask)) || 1); \
1482 nr_nodes--)
1483
1484 #ifdef CONFIG_ARCH_HAS_GIGANTIC_PAGE
1485 static struct folio *alloc_gigantic_folio(int order, gfp_t gfp_mask,
1486 int nid, nodemask_t *nodemask)
1487 {
1488 struct folio *folio;
1489
1490 folio = hugetlb_cma_alloc_frozen_folio(order, gfp_mask, nid, nodemask);
1491 if (folio)
1492 return folio;
1493
1494 if (hugetlb_cma_exclusive_alloc())
1495 return NULL;
1496

> 1497		folio = (struct folio *)alloc_contig_frozen_pages(1 << order, gfp_mask,

1498 nid, nodemask);
1499 return folio;
1500 }
1501 #else /* !CONFIG_ARCH_HAS_GIGANTIC_PAGE */
1502 static struct folio *alloc_gigantic_folio(int order, gfp_t gfp_mask, int nid,
1503 nodemask_t *nodemask)
1504 {
1505 return NULL;
1506 }
1507 #endif
1508

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki