[PATCH v4 05/13] mm: hugetlb_vmemmap: generalize vmemmap page allocation

From: Muchun Song

Date: Fri Nov 06 2026 - 21:35:25 EST


Vmemmap page allocation is currently tied to restoring a virtual range
and assumes an empty destination list. These constraints prevent it from
being reused when pages are requested independently of a vmemmap range or
appended to an existing list.

Generalize the allocator and give it an all-or-nothing list contract so
the restore and optimization paths can share the same helper.

Signed-off-by: Muchun Song <songmuchun@xxxxxxxxxxxxx>
Reviewed-by: Lance Yang <lance.yang@xxxxxxxxx>
Reviewed-by: Qi Zheng <qi.zheng@xxxxxxxxx>
Reviewed-by: Oscar Salvador <osalvador@xxxxxxx>
Reviewed-by: Mike Rapoport (Microsoft) <rppt@xxxxxxxxxx>
Tested-by: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx>
---
v4:
- Split the OOM-avoidance mask change into a prerequisite fix and pass
allocation policy through the generalized helper
- Propagate alloc_vmemmap_page_list() errors (suggested by Oscar
Salvador)
- Update the commit message after splitting the OOM fix
- Collect Reviewed-by from Oscar Salvador and Mike Rapoport
- Collect Tested-by from Andrew Morton

v3:
- Use two tabs for continued function parameters (suggested by Mike
Rapoport)
- Rename the local allocation list to new_pages (suggested by Mike
Rapoport)
- Use a goto cleanup path for allocation failures (suggested by Mike
Rapoport)
- Free failed allocations directly and retain batched accounting (suggested
by Mike Rapoport)

v2:
- Collect Reviewed-by from Lance Yang and Qi Zheng
---
mm/hugetlb_vmemmap.c | 41 ++++++++++++++++++++---------------------
1 file changed, 20 insertions(+), 21 deletions(-)

diff --git a/mm/hugetlb_vmemmap.c b/mm/hugetlb_vmemmap.c
index beb420c57ddc..290dd8f31738 100644
--- a/mm/hugetlb_vmemmap.c
+++ b/mm/hugetlb_vmemmap.c
@@ -334,26 +334,24 @@ static int vmemmap_remap_free(unsigned long start, unsigned long end,
return ret;
}

-static int alloc_vmemmap_page_list(unsigned long start, unsigned long end,
- struct list_head *list)
+static int alloc_vmemmap_page_list(struct list_head *head,
+ unsigned int nr_pages, int nid, gfp_t gfp_mask)
{
- gfp_t gfp_mask = GFP_KERNEL | __GFP_RETRY_MAYFAIL;
- unsigned long nr_pages = (end - start) >> PAGE_SHIFT;
- int nid = page_to_nid((struct page *)start);
struct page *page, *next;
- int i;
+ LIST_HEAD(new_pages);

- for (i = 0; i < nr_pages; i++) {
- page = alloc_pages_node(nid, gfp_mask, 0);
+ for (unsigned int i = 0; i < nr_pages; i++) {
+ page = alloc_pages_node(nid, gfp_mask | __GFP_RETRY_MAYFAIL, 0);
if (!page)
- goto out;
- list_add(&page->lru, list);
+ goto free_pages;
+ list_add(&page->lru, &new_pages);
}
+ list_splice(&new_pages, head);
memmap_pages_add(nr_pages);

return 0;
-out:
- list_for_each_entry_safe(page, next, list, lru)
+free_pages:
+ list_for_each_entry_safe(page, next, &new_pages, lru)
__free_page(page);
return -ENOMEM;
}
@@ -373,6 +371,7 @@ static int alloc_vmemmap_page_list(unsigned long start, unsigned long end,
static int vmemmap_remap_alloc(unsigned long start, unsigned long end,
unsigned long flags)
{
+ int ret, nid = page_to_nid((struct page *)start);
LIST_HEAD(vmemmap_pages);
struct vmemmap_remap_walk walk = {
.remap_pte = vmemmap_restore_pte,
@@ -380,8 +379,10 @@ static int vmemmap_remap_alloc(unsigned long start, unsigned long end,
.flags = flags,
};

- if (alloc_vmemmap_page_list(start, end, &vmemmap_pages))
- return -ENOMEM;
+ ret = alloc_vmemmap_page_list(&vmemmap_pages, (end - start) / PAGE_SIZE,
+ nid, GFP_KERNEL);
+ if (ret)
+ return ret;

return vmemmap_remap_range(start, end, &walk);
}
@@ -501,7 +502,7 @@ static int __hugetlb_vmemmap_optimize_folio(const struct hstate *h,
{
unsigned long vmemmap_start, vmemmap_end;
struct page *vmemmap_head, *vmemmap_tail;
- gfp_t gfp_mask = GFP_KERNEL | __GFP_NOWARN | __GFP_RETRY_MAYFAIL;
+ gfp_t gfp_mask = GFP_KERNEL | __GFP_NOWARN;
int nid, ret = 0;

VM_WARN_ON_ONCE_FOLIO(!folio_test_hugetlb(folio), folio);
@@ -528,15 +529,13 @@ static int __hugetlb_vmemmap_optimize_folio(const struct hstate *h,
*/
folio_set_hugetlb_vmemmap_optimized(folio);

- vmemmap_head = alloc_pages_node(nid, gfp_mask, 0);
- if (!vmemmap_head) {
- ret = -ENOMEM;
+ ret = alloc_vmemmap_page_list(vmemmap_pages, VMEMMAP_OPTIMIZATION_PAGES,
+ nid, gfp_mask);
+ if (ret)
goto out;
- }

+ vmemmap_head = list_first_entry(vmemmap_pages, struct page, lru);
copy_page(page_to_virt(vmemmap_head), folio);
- list_add(&vmemmap_head->lru, vmemmap_pages);
- memmap_pages_add(1);

vmemmap_start = (unsigned long)&folio->page;
vmemmap_end = vmemmap_start + hugetlb_vmemmap_size(h);
--
2.54.0