[RFC PATCH 4/4] mm/vmalloc: page align huge vmalloc sizes

From: Wen Jiang

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


From: Wen Jiang <jiangwen6@xxxxxxxxxx>

__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@xxxxxxxxxx>
Assisted-by: LLM
Signed-off-by: Wen Jiang <jiangwen6@xxxxxxxxxx>
---
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