* [RFC PATCH 1/4] mm/vmalloc: extract vmalloc_area_map_range()
2026-12-02 9:12 [RFC PATCH 0/4] mm/vmalloc: Reduce memory usage and speed up huge vmalloc Wen Jiang
@ 2026-12-02 9:12 ` Wen Jiang
2026-12-02 9:12 ` [RFC PATCH 2/4] mm/vmalloc: add VM_HAS_HUGE_VMAP Wen Jiang
` (2 subsequent siblings)
3 siblings, 0 replies; 5+ messages in thread
From: Wen Jiang @ 2026-12-02 9:12 UTC (permalink / raw)
To: Andrew Morton, Uladzislau Rezki, linux-mm
Cc: Barry Song, Ryan Roberts, Dev Jain, Anshuman Khandual,
David Hildenbrand, Catalin Marinas, Will Deacon, Leo Yan,
Xueyuan Chen, Mike Rapoport, Pasha Tatashin, Pratyush Yadav,
Alexander Graf, kexec, linux-arm-kernel, linux-kernel, Wen Jiang
From: Wen Jiang <jiangwen6@xiaomi.com>
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@kernel.org>
Assisted-by: LLM
Signed-off-by: Wen Jiang <jiangwen6@xiaomi.com>
---
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
^ permalink raw reply related [flat|nested] 5+ messages in thread* [RFC PATCH 2/4] mm/vmalloc: add VM_HAS_HUGE_VMAP
2026-12-02 9:12 [RFC PATCH 0/4] mm/vmalloc: Reduce memory usage and speed up huge vmalloc Wen Jiang
2026-12-02 9:12 ` [RFC PATCH 1/4] mm/vmalloc: extract vmalloc_area_map_range() Wen Jiang
@ 2026-12-02 9:12 ` Wen Jiang
2026-12-02 9:12 ` [RFC PATCH 3/4] mm/vmalloc: try smaller block sizes before base pages Wen Jiang
2026-12-02 9:12 ` [RFC PATCH 4/4] mm/vmalloc: page align huge vmalloc sizes Wen Jiang
3 siblings, 0 replies; 5+ messages in thread
From: Wen Jiang @ 2026-12-02 9:12 UTC (permalink / raw)
To: Andrew Morton, Uladzislau Rezki, linux-mm
Cc: Barry Song, Ryan Roberts, Dev Jain, Anshuman Khandual,
David Hildenbrand, Catalin Marinas, Will Deacon, Leo Yan,
Xueyuan Chen, Mike Rapoport, Pasha Tatashin, Pratyush Yadav,
Alexander Graf, kexec, linux-arm-kernel, linux-kernel, Wen Jiang
From: Wen Jiang <jiangwen6@xiaomi.com>
vrealloc() frees the tail pages of a shrinking area unless its
page_order is non-zero, as partial freeing would require splitting a
huge mapping, and is_vm_area_hugepages() reports huge mappings from
page_order as well. Both rely on a huge vmalloc area being mapped
either entirely with blocks of 1 << page_order pages or entirely with
base pages.
A follow-up change lets a huge vmalloc area fall back to smaller block
sizes for part of the area. page_order then has to be the smallest
order used, so that the page array can still be walked in steps of
1 << page_order. It can be 0 for an area that still contains PMD or
contiguous PTE mappings.
Add an internal VM_HAS_HUGE_VMAP flag, set when at least part of an
area is mapped with entries larger than PAGE_SIZE, and use it in
vrealloc() and is_vm_area_hugepages() instead of page_order. Together
with page_order it describes the layout:
!VM_HAS_HUGE_VMAP base pages only
VM_HAS_HUGE_VMAP, page_order == 0 huge mappings plus base pages
VM_HAS_HUGE_VMAP, page_order > 0 huge mappings only
For vrealloc() and the powerpc set_memory_*() guard, any huge mapping
is reason enough to back off. alloc_large_system_hash() only uses the
result for its boot message.
set_vm_area_page_order() sets the flag for a non-zero order, so for
now the flag is set exactly when page_order is non-zero and there is no
functional change.
Like page_order, the flag is only set by the vmalloc allocation path.
Huge mappings created by vmap() or ioremap() are tracked by neither.
Let kho_preserve_vmalloc() accept the new flag, as it rejects any flag
it does not know about.
Suggested-by: Barry Song <baohua@kernel.org>
Assisted-by: LLM
Signed-off-by: Wen Jiang <jiangwen6@xiaomi.com>
---
include/linux/vmalloc.h | 7 ++++---
kernel/liveupdate/kexec_handover.c | 3 ++-
mm/vmalloc.c | 9 ++++++---
3 files changed, 12 insertions(+), 7 deletions(-)
diff --git a/include/linux/vmalloc.h b/include/linux/vmalloc.h
index 034a693777ca0..31be61ad88550 100644
--- a/include/linux/vmalloc.h
+++ b/include/linux/vmalloc.h
@@ -40,6 +40,7 @@ struct iov_iter; /* in uio.h */
#endif
#define VM_SPARSE 0x00001000 /* sparse vm_area. not all pages are present. */
#define VM_REQUIRE_HUGE_VMAP 0x00002000 /* huge page mapping or nothing */
+#define VM_HAS_HUGE_VMAP 0x00004000 /* part of the area has huge mappings */
/* bits [20..32] reserved for arch specific ioremap internals */
@@ -265,13 +266,13 @@ static inline bool is_vm_area_hugepages(const void *addr)
* This may not 100% tell if the area is mapped with > PAGE_SIZE
* page table entries, if for some reason the architecture indicates
* larger sizes are available but decides not to use them, nothing
- * prevents that. This only indicates the size of the physical page
- * allocated in the vmalloc layer.
+ * prevents that. This only indicates that the vmalloc layer mapped
+ * at least part of the area with such entries.
*/
#ifdef CONFIG_HAVE_ARCH_HUGE_VMALLOC
struct vm_struct *area = find_vm_area(addr);
- return area && area->page_order > 0;
+ return area && (area->flags & VM_HAS_HUGE_VMAP);
#else
return false;
#endif
diff --git a/kernel/liveupdate/kexec_handover.c b/kernel/liveupdate/kexec_handover.c
index 42c6609a1766a..c2d5823b498a3 100644
--- a/kernel/liveupdate/kexec_handover.c
+++ b/kernel/liveupdate/kexec_handover.c
@@ -1288,7 +1288,8 @@ void kho_unpreserve_pages(struct page *page, unsigned long nr_pages)
EXPORT_SYMBOL_GPL(kho_unpreserve_pages);
/* vmalloc flags KHO supports */
-#define KHO_VMALLOC_SUPPORTED_FLAGS (VM_ALLOC | VM_ALLOW_HUGE_VMAP)
+#define KHO_VMALLOC_SUPPORTED_FLAGS (VM_ALLOC | VM_ALLOW_HUGE_VMAP | \
+ VM_HAS_HUGE_VMAP)
/* KHO internal flags for vmalloc preservations */
#define KHO_VMALLOC_ALLOC 0x0001
diff --git a/mm/vmalloc.c b/mm/vmalloc.c
index eb28dbd29c2e1..595b07a6cc3d1 100644
--- a/mm/vmalloc.c
+++ b/mm/vmalloc.c
@@ -3222,6 +3222,8 @@ static inline void set_vm_area_page_order(struct vm_struct *vm, unsigned int ord
{
#ifdef CONFIG_HAVE_ARCH_HUGE_VMALLOC
vm->page_order = order;
+ if (order)
+ vm->flags |= VM_HAS_HUGE_VMAP;
#else
BUG_ON(order != 0);
#endif
@@ -4613,7 +4615,7 @@ void *vrealloc_node_align_noprof(const void *p, size_t size, unsigned long align
/*
* Free tail pages when shrink crosses a page boundary.
*
- * Skip huge page allocations (page_order > 0) as partial
+ * Skip areas with huge mappings (VM_HAS_HUGE_VMAP) as partial
* freeing would require splitting.
*
* Skip VM_FLUSH_RESET_PERMS, as direct-map permissions must
@@ -4629,8 +4631,9 @@ void *vrealloc_node_align_noprof(const void *p, size_t size, unsigned long align
* GFP_KERNEL, which could trigger a recursive deadlock
* if we are under filesystem or I/O reclaim.
*/
- if (new_nr_pages < vm->nr_pages && !vm_area_page_order(vm) &&
- !(vm->flags & (VM_FLUSH_RESET_PERMS | VM_USERMAP)) &&
+ if (new_nr_pages < vm->nr_pages &&
+ !(vm->flags & (VM_HAS_HUGE_VMAP | VM_FLUSH_RESET_PERMS |
+ VM_USERMAP)) &&
gfp_has_io_fs(flags)) {
unsigned long addr = (unsigned long)kasan_reset_tag(p);
unsigned long old_nr_pages = vm->nr_pages;
--
2.34.1
^ permalink raw reply related [flat|nested] 5+ messages in thread* [RFC PATCH 3/4] mm/vmalloc: try smaller block sizes before base pages
2026-12-02 9:12 [RFC PATCH 0/4] mm/vmalloc: Reduce memory usage and speed up huge vmalloc Wen Jiang
2026-12-02 9:12 ` [RFC PATCH 1/4] mm/vmalloc: extract vmalloc_area_map_range() Wen Jiang
2026-12-02 9:12 ` [RFC PATCH 2/4] mm/vmalloc: add VM_HAS_HUGE_VMAP Wen Jiang
@ 2026-12-02 9:12 ` Wen Jiang
2026-12-02 9:12 ` [RFC PATCH 4/4] mm/vmalloc: page align huge vmalloc sizes Wen Jiang
3 siblings, 0 replies; 5+ messages in thread
From: Wen Jiang @ 2026-12-02 9:12 UTC (permalink / raw)
To: Andrew Morton, Uladzislau Rezki, linux-mm
Cc: Barry Song, Ryan Roberts, Dev Jain, Anshuman Khandual,
David Hildenbrand, Catalin Marinas, Will Deacon, Leo Yan,
Xueyuan Chen, Mike Rapoport, Pasha Tatashin, Pratyush Yadav,
Alexander Graf, kexec, linux-arm-kernel, linux-kernel, Wen Jiang
From: Wen Jiang <jiangwen6@xiaomi.com>
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@kernel.org>
Assisted-by: LLM
Signed-off-by: Wen Jiang <jiangwen6@xiaomi.com>
---
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
^ permalink raw reply related [flat|nested] 5+ messages in thread* [RFC PATCH 4/4] mm/vmalloc: page align huge vmalloc sizes
2026-12-02 9:12 [RFC PATCH 0/4] mm/vmalloc: Reduce memory usage and speed up huge vmalloc Wen Jiang
` (2 preceding siblings ...)
2026-12-02 9:12 ` [RFC PATCH 3/4] mm/vmalloc: try smaller block sizes before base pages Wen Jiang
@ 2026-12-02 9:12 ` Wen Jiang
3 siblings, 0 replies; 5+ messages in thread
From: Wen Jiang @ 2026-12-02 9:12 UTC (permalink / raw)
To: Andrew Morton, Uladzislau Rezki, linux-mm
Cc: Barry Song, Ryan Roberts, Dev Jain, Anshuman Khandual,
David Hildenbrand, Catalin Marinas, Will Deacon, Leo Yan,
Xueyuan Chen, Mike Rapoport, Pasha Tatashin, Pratyush Yadav,
Alexander Graf, kexec, linux-arm-kernel, linux-kernel, Wen Jiang
From: Wen Jiang <jiangwen6@xiaomi.com>
__get_vm_area_node() rounds the size of a huge vmalloc area up to its
mapping size, e.g. a 2.1MB kvmalloc() on arm64 reserves and backs 4MB,
wasting almost 2MB of memory. The rounding was needed because each
huge page allocation filled a whole block, so the page count had to be
a multiple of the block size.
Now that the allocation falls back level by level and each level only
asks for whole blocks that fit the remainder, the tail of an area can
be backed by smaller blocks or base pages. Round the size up to
PAGE_SIZE only. The alignment of the area is unchanged, as it is
passed separately.
Keep the rounding for VM_REQUIRE_HUGE_VMAP, which must be backed by
huge pages entirely.
Suggested-by: Barry Song <baohua@kernel.org>
Assisted-by: LLM
Signed-off-by: Wen Jiang <jiangwen6@xiaomi.com>
---
mm/vmalloc.c | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
diff --git a/mm/vmalloc.c b/mm/vmalloc.c
index 5edc6bd20969b..f5d9f61892b2a 100644
--- a/mm/vmalloc.c
+++ b/mm/vmalloc.c
@@ -3313,7 +3313,10 @@ struct vm_struct *__get_vm_area_node(unsigned long size,
if (WARN_ON_ONCE(in_nmi() || in_hardirq() || softirq_count()))
return NULL;
- size = ALIGN(size, 1ul << shift);
+ if (flags & VM_REQUIRE_HUGE_VMAP)
+ size = ALIGN(size, 1ul << shift);
+ else
+ size = PAGE_ALIGN(size);
if (unlikely(!size))
return NULL;
--
2.34.1
^ permalink raw reply related [flat|nested] 5+ messages in thread