[RFC PATCH 3/4] mm/vmalloc: try smaller block sizes before base pages
From: Wen Jiang
Date: Wed Dec 02 2026 - 04:13:19 EST
From: Wen Jiang <jiangwen6@xxxxxxxxxx>
When a huge vmalloc allocation (VM_ALLOW_HUGE_VMAP) cannot get enough
pages of the requested order, __vmalloc_node_range_noprof() frees
everything it got, including the huge pages that did succeed, and
starts over with base pages. Sizes in between, such as arm64's
CONT_PTE, are never tried, so under fragmentation the area ends up
fully mapped with PTEs even though much of it could have been covered
by larger blocks.
Instead, allocate the area level by level, from the requested mapping
size down through the smaller sizes the architecture can map, e.g.
PMD -> CONT_PTE -> PAGE, keeping whatever was already obtained. Each
level asks vm_area_alloc_pages() for as many whole blocks of its size
as fit the remainder, and maps what it got with its own shift; the
rest is left to the next level. The next level is picked from what is
left or, if the level could not be filled, is the next smaller size.
VM_REQUIRE_HUGE_VMAP does not fall back below the requested level.
Since the level only goes down, each level starts at an index that is
a multiple of its block size, and the area is aligned to the requested
mapping size, so every run is suitably aligned for its shift. An area
that is fully backed at the requested level is mapped in one go, as
before.
page_order is set to the smallest level that was used, so the page
array can still be walked in steps of 1 << page_order. Since it is set
after each run, VM_HAS_HUGE_VMAP gets set as soon as one run is mapped
with a shift larger than PAGE_SHIFT.
__vmalloc_node_range_noprof() no longer retries the whole allocation
with base pages when __vmalloc_area_node() fails. For a page allocation
failure the fallback has already ended at base pages. Mapping with
base pages needs more page table pages than with huge pages, so
retrying after a mapping failure is unlikely to help either, and the
page array would only shrink by the rounding up of the size, which the
next change removes. __GFP_NOFAIL is still honoured by
the order-0 level, the page array allocation and the mapping. The
retry is kept for the case where the vm area cannot be found with the
huge alignment.
Suggested-by: Barry Song <baohua@xxxxxxxxxx>
Assisted-by: LLM
Signed-off-by: Wen Jiang <jiangwen6@xxxxxxxxxx>
---
mm/vmalloc.c | 50 +++++++++++++++++++++++++++++++++-----------------
1 file changed, 33 insertions(+), 17 deletions(-)
diff --git a/mm/vmalloc.c b/mm/vmalloc.c
index 595b07a6cc3d1..5edc6bd20969b 100644
--- a/mm/vmalloc.c
+++ b/mm/vmalloc.c
@@ -4112,14 +4112,12 @@ memalloc_restore_scope(unsigned int flags)
}
static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
- pgprot_t prot, unsigned int page_shift,
- int node)
+ pgprot_t prot, unsigned int shift, int node)
{
const gfp_t nested_gfp = (gfp_mask & GFP_RECLAIM_MASK) | __GFP_ZERO;
unsigned long size = get_vm_area_size(area);
unsigned long array_size;
unsigned long nr_small_pages = size >> PAGE_SHIFT;
- unsigned int page_order = page_shift - PAGE_SHIFT;
array_size = nr_small_pages * sizeof(struct page *);
@@ -4146,12 +4144,34 @@ static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
* potentially dangerous (pre-mature OOM, disruptive reclaim
* and compaction etc.
*
- * Please note, the __vmalloc_node_range_noprof() falls-back
- * to order-0 pages if high-order attempt is unsuccessful.
+ * Please note, the allocation falls back to smaller orders and
+ * finally to order-0 pages if high-order attempt is unsuccessful.
*/
- area->nr_pages = vm_area_alloc_pages(
- vmalloc_gfp_adjust(gfp_mask, page_order), node,
- page_order, nr_small_pages, area->pages);
+ while (area->nr_pages < nr_small_pages) {
+ unsigned int order = shift - PAGE_SHIFT;
+ unsigned long left = nr_small_pages - area->nr_pages;
+ unsigned long nr;
+
+ nr = vm_area_alloc_pages(vmalloc_gfp_adjust(gfp_mask, order),
+ node, order, round_down(left, 1UL << order),
+ area->pages + area->nr_pages);
+ if (nr) {
+ area->nr_pages += nr;
+ if (vmalloc_area_map_range(area, area->nr_pages - nr,
+ area->nr_pages, shift, prot, gfp_mask))
+ goto fail;
+ set_vm_area_page_order(area, order);
+ }
+
+ /* No smaller size left, or huge pages are required. */
+ if (shift == PAGE_SHIFT || (area->flags & VM_REQUIRE_HUGE_VMAP))
+ break;
+
+ if (left - nr < 1UL << order)
+ shift = vm_shift(prot, (left - nr) << PAGE_SHIFT);
+ else /* this level failed, step down */
+ shift = vm_shift(prot, 1UL << (shift - 1));
+ }
/*
* If not enough pages were obtained to accomplish an
@@ -4165,21 +4185,17 @@ static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
* - a pending fatal signal
* - insufficient huge page-order pages
*
- * Since we always retry allocations at order-0 in the huge page
- * case a warning for either is spurious.
+ * Huge page-order allocations only fail here when huge pages
+ * are required, so a warning for either is spurious.
*/
- if (!fatal_signal_pending(current) && page_order == 0)
+ if (!fatal_signal_pending(current) &&
+ !(area->flags & VM_REQUIRE_HUGE_VMAP))
warn_alloc(gfp_mask, NULL,
"vmalloc error: size %lu, failed to allocate pages",
nr_small_pages * PAGE_SIZE);
goto fail;
}
- if (vmalloc_area_map_range(area, 0, nr_small_pages, page_shift, prot,
- gfp_mask))
- goto fail;
-
- set_vm_area_page_order(area, page_order);
return area->addr;
fail:
@@ -4335,7 +4351,7 @@ void *__vmalloc_node_range_noprof(unsigned long size, unsigned long align,
/* Allocate physical pages and map them into vmalloc space. */
ret = __vmalloc_area_node(area, gfp_mask, prot, shift, node);
if (!ret)
- goto fail;
+ return NULL;
/*
* Mark the pages as accessible, now that they are mapped.
--
2.34.1