Skip to contentSkip to conversation

page_alloc: allow migration of smaller hugepages during contig_alloc

PATCH Superseded Gregory Price 3 participants 4 messages Nov 21, 2025 – Dec 1, 2025

Subsystems (2)

v3 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 page_alloc: allow migration of smaller hugepages during contig_alloc

We presently skip regions with hugepages entirely when trying to do contiguous page allocation. Instead, if hugepage migration is enabled, consider regions with hugepages smaller than the target contiguous allocation request as valid targets for allocation.

isolate_migrate_pages_block() already expects requests with hugepages to originate from alloc_contig, and hugetlb code also does a migratable check when isolating in folio_isolate_hugetlb().

---
 mm/page_alloc.c | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)

---
v3: changelog updates and tags, no code changes

mm/page_alloc.c +13 -2
index a6fe1e9b9594..3b0f47f1f144 100644
@@ -6849,8 +6849,19 @@ static bool pfn_range_valid_contig(struct zone *z, unsigned long start_pfn,
 		if (PageReserved(page))
 			return false;
 
-		if (PageHuge(page))
-			return false;
+		if (PageHuge(page)) {
+			unsigned int order;
+
+			if (!IS_ENABLED(CONFIG_ARCH_ENABLE_HUGEPAGE_MIGRATION))
+				return false;
+
+			/* Don't consider moving same size/larger pages */
+			page = compound_head(page);
+			order = compound_order(page);
+			if ((order >= MAX_FOLIO_ORDER) ||
+			    (nr_pages <= (1 << order)))
+				return false;
+		}
 	}
 	return true;
 }
Signature · 1 line
2.51.1
maintainer
#1 lore raw
Quoted 4 lines · On Fri, 21 Nov 2025 14:15:40 -0500 Gregory Price <[email protected]> wrote:

We presently skip regions with hugepages entirely when trying to do contiguous page allocation. Instead, if hugepage migration is enabled, consider regions with hugepages smaller than the target contiguous allocation request as valid targets for allocation.

Why? What benefit does this have to our users?

Some runtime testing results might be helpful?

Quoted 5 lines

isolate_migrate_pages_block() already expects requests with hugepages to originate from alloc_contig, and hugetlb code also does a migratable check when isolating in folio_isolate_hugetlb().

A Link: here might be illuminating.

Quoted 5 lines
mm/page_alloc.c +0 -0
@@ -6849,8 +6849,19 @@ static bool pfn_range_valid_contig(struct zone *z, unsigned long start_pfn,
 		if (PageReserved(page))
 			return false;

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

Quoted 10 lines
mm/page_alloc.c +7 -2
 
-		if (PageHuge(page))
-			return false;
+		if (PageHuge(page)) {
+			unsigned int order;
+
+			if (!IS_ENABLED(CONFIG_ARCH_ENABLE_HUGEPAGE_MIGRATION))
+				return false;
+
+			/* Don't consider moving same size/larger pages */

Comment says "what" (which was fairly obvious). Please reveal "why".

Quoted 9 lines
mm/page_alloc.c +6 -0
+			page = compound_head(page);
+			order = compound_order(page);
+			if ((order >= MAX_FOLIO_ORDER) ||
+			    (nr_pages <= (1 << order)))
+				return false;
+		}
 	}
 	return true;
 }
#2 lore raw
Quoted 10 lines · Andrew Morton
Quoted 4 lines · On Fri, 21 Nov 2025 14:15:40 -0500 Gregory Price <[email protected]> wrote:

We presently skip regions with hugepages entirely when trying to do contiguous page allocation. Instead, if hugepage migration is enabled, consider regions with hugepages smaller than the target contiguous allocation request as valid targets for allocation.

Why? What benefit does this have to our users?

Some runtime testing results might be helpful?

If multiple types of hugepages are in use, alloc_contig is less reliable. In particular when 2MB and 1GB HugeTLB pages are present on the same system.

The same logic is actually present in isolate_migrate_pages_block() as pointed out by David which is called in the stack from alloc_contig - but it's unreachable because this filters those regions.

I allude to this in the second paragraph, but it is worth spelling out explicitly. Will update.

Quoted 8 lines
Quoted 6 lines

isolate_migrate_pages_block() already expects requests with hugepages to originate from alloc_contig, and hugetlb code also does a migratable check when isolating in folio_isolate_hugetlb().

A Link: here might be illuminating.

Ah, fair point

"""
However, it also means that we won't try moving 2MB folios to free up a 1GB folio.

That could be supported by allowing for moving hugetlb folios when their size is small enough to be served by the buddy, and the size we are allocating is larger than the one of these folios.
"""

Quoted 7 lines
Quoted 7 lines
mm/page_alloc.c +0 -0
@@ -6849,8 +6849,19 @@ static bool pfn_range_valid_contig(struct zone *z, unsigned long start_pfn,
 		if (PageReserved(page))
 			return false;
 
Quoted 12 lines

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

Quoted 9 lines
mm/page_alloc.c +7 -2
-		if (PageHuge(page))
-			return false;
+		if (PageHuge(page)) {
+			unsigned int order;
+
+			if (!IS_ENABLED(CONFIG_ARCH_ENABLE_HUGEPAGE_MIGRATION))
+				return false;
+
+			/* Don't consider moving same size/larger pages */

Comment says "what" (which was fairly obvious). Please reveal "why".

ack.

Quoted 10 lines
Quoted 9 lines
mm/page_alloc.c +6 -0
+			page = compound_head(page);
+			order = compound_order(page);
+			if ((order >= MAX_FOLIO_ORDER) ||
+			    (nr_pages <= (1 << order)))
+				return false;
+		}
 	}
 	return true;
 }
Tested-by on v3 1/1
#3 lore raw
In reply to Gregory Price
Quoted 9 lines · On Fri, 21 Nov 2025 14:15:40 -0500 Gregory Price <[email protected]> wrote:

We presently skip regions with hugepages entirely when trying to do contiguous page allocation. Instead, if hugepage migration is enabled, consider regions with hugepages smaller than the target contiguous allocation request as valid targets for allocation.

isolate_migrate_pages_block() already expects requests with hugepages to originate from alloc_contig, and hugetlb code also does a migratable check when isolating in folio_isolate_hugetlb().

Quoted 7 lines

Hello folks, sorry for arriving late to the party, it seems like the patch has gotten a lot of reviews already. I thought I would stop by to share some simple testing that I've done: On a machine with 62GiB memory, I tried to see if I could allocate a bunch of 2MB hugeTLB pages, then allocate 1GB hugeTLB pages on top to see if that attempt would succeed.

To test this, I made a really simple setup:
1. Allocate 48GB worth of 2MB hugeTLB pages (24576)
2. Allocate 4 1G hugeTLB pages

Before this patch, I get 0 1G hugeTLB pages.
After this patch, I can get all 4 requested 1G hugeTLB pages!

I would share the script, but it really is just as simple as echoing 24576 and 4 to .../hugepages-{2048kB, 1048576kB}/nr_hugepages, respsectively. If you want to reproduce this at home, you might have to change how many 2MB pages to allocate to see this difference, depending on the size of your machine.

With this, please feel free to add:

Have a great day, everyone!
Joshua