[RFC PATCH 1/4] mm/vmalloc: extract vmalloc_area_map_range()

From: Wen Jiang

Date: Wed Dec 02 2026 - 04:13:06 EST


From: Wen Jiang <jiangwen6@xxxxxxxxxx>

Move the mapping of a vmalloc area's pages, including the gfp scope,
the __GFP_NOFAIL retry loop and the failure warning, out of
__vmalloc_area_node() into vmalloc_area_map_range(). It maps the pages
[start, end) of the area, where start and end are indices into the page
array, with the given shift.

The warning for a failed mapping reports the size of the mapped range
instead of area->nr_pages. Both are the same at the only call site,
which maps the whole area.

__vmalloc_area_node() still maps the whole area in one go. A follow-up
change will map an area in several runs with different shifts.

No functional change.

Suggested-by: Barry Song <baohua@xxxxxxxxxx>
Assisted-by: LLM
Signed-off-by: Wen Jiang <jiangwen6@xxxxxxxxxx>
---
mm/vmalloc.c | 64 +++++++++++++++++++++++++++++++---------------------
1 file changed, 38 insertions(+), 26 deletions(-)

diff --git a/mm/vmalloc.c b/mm/vmalloc.c
index aeb1cd4a53850..eb28dbd29c2e1 100644
--- a/mm/vmalloc.c
+++ b/mm/vmalloc.c
@@ -4005,6 +4005,42 @@ vm_area_alloc_pages(gfp_t gfp, int nid,
return nr_allocated;
}

+static int vmalloc_area_map_range(struct vm_struct *area,
+ unsigned long start, unsigned long end, unsigned int shift,
+ pgprot_t prot, gfp_t gfp)
+{
+ const gfp_t nested_gfp = (gfp & GFP_RECLAIM_MASK) | __GFP_ZERO;
+ unsigned long addr = (unsigned long)area->addr;
+ bool nofail = gfp & __GFP_NOFAIL;
+ unsigned int flags;
+ int ret;
+
+ /* __GFP_NOFAIL and "noblock" flags are mutually exclusive. */
+ if (!gfpflags_allow_blocking(gfp))
+ nofail = false;
+
+ /*
+ * page tables allocations ignore external gfp mask, enforce it
+ * by the scope API
+ */
+ flags = memalloc_apply_gfp_scope(gfp);
+ do {
+ ret = __vmap_pages_range(addr + (start << PAGE_SHIFT),
+ addr + (end << PAGE_SHIFT), prot,
+ area->pages + start, shift, nested_gfp);
+ if (nofail && (ret < 0))
+ schedule_timeout_uninterruptible(1);
+ } while (nofail && (ret < 0));
+ memalloc_restore_scope(flags);
+
+ if (ret < 0)
+ warn_alloc(gfp, NULL,
+ "vmalloc error: size %lu, failed to map pages",
+ (end - start) << PAGE_SHIFT);
+
+ return ret;
+}
+
static LLIST_HEAD(pending_vm_area_cleanup);
static void cleanup_vm_area_work(struct work_struct *work)
{
@@ -4078,21 +4114,13 @@ static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
int node)
{
const gfp_t nested_gfp = (gfp_mask & GFP_RECLAIM_MASK) | __GFP_ZERO;
- bool nofail = gfp_mask & __GFP_NOFAIL;
- unsigned long addr = (unsigned long)area->addr;
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;
- unsigned int flags;
- int ret;

array_size = nr_small_pages * sizeof(struct page *);

- /* __GFP_NOFAIL and "noblock" flags are mutually exclusive. */
- if (!gfpflags_allow_blocking(gfp_mask))
- nofail = false;
-
if (!(gfp_mask & (GFP_DMA | GFP_DMA32)))
gfp_mask |= __GFP_HIGHMEM;

@@ -4145,25 +4173,9 @@ static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
goto fail;
}

- /*
- * page tables allocations ignore external gfp mask, enforce it
- * by the scope API
- */
- flags = memalloc_apply_gfp_scope(gfp_mask);
- do {
- ret = __vmap_pages_range(addr, addr + size, prot, area->pages,
- page_shift, nested_gfp);
- if (nofail && (ret < 0))
- schedule_timeout_uninterruptible(1);
- } while (nofail && (ret < 0));
- memalloc_restore_scope(flags);
-
- if (ret < 0) {
- warn_alloc(gfp_mask, NULL,
- "vmalloc error: size %lu, failed to map pages",
- area->nr_pages * PAGE_SIZE);
+ 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;
--
2.34.1