[PATCH] lib/test_vmalloc: check vrealloc page counts and data ranges

From: Xuewen Wang

Date: Sat Nov 28 2026 - 01:36:47 EST


From: Xuewen Wang <wangxuewen@xxxxxxxxxx>

The vrealloc test currently checks only the first byte after each resize.
It can pass even if shrinking retains all tail pages or a later byte in the
preserved prefix is corrupted.

Check the backing page count for the ordinary order-0 allocation path and
validate the entire preserved prefix with an offset-dependent pattern. Add
a non-page-aligned shrink and a grow beyond the pages retained after
shrink.

Run the sequence both with GFP_KERNEL and with __GFP_ZERO used consistently
from the initial allocation. In the latter case, also check that each newly
exposed range is zero, including when growing within a retained page.

Export find_vm_area() under the existing CONFIG_TEST_VMALLOC_MODULE guard
so the test can inspect backing page counts in modular builds as well.

The additional resize operations and full-range data checks increase the
work per iteration, so timings are not directly comparable with those
from the original test. When running vrealloc_test alone, use a smaller
test_loop_count, such as 32, to keep correctness runs short instead of
using the default million iterations.

Signed-off-by: Xuewen Wang <wangxuewen@xxxxxxxxxx>
---
Validation:
- x86-64, 4 KiB pages: built-in one/two workers and module two workers
passed, with three repeats of 32 loops per worker. Every loop runs
both GFP_KERNEL and consistently used __GFP_ZERO sequences.
- Private x86-64 mutants suppressed tail-page freeing, corrupted the
final copied byte, or skipped shrink-time zeroing. The enhanced test
rejected all three; the original first-byte-only test passed all three.
These were artificial defects, not discovered allocator bugs.
- arm64, 64 KiB pages: built-in one/two workers and module two workers
passed with the same repeats/loops. Guest userspace checked the actual
page size.
- The same arm64 matrix passed with KASAN_GENERIC and KASAN_VMALLOC
enabled. test_vmalloc.o contained __asan_load/store references; full
serial logs contained no KASAN reports or kernel warnings/oopses.
- With TEST_VMALLOC=n, find_vm_area has no export in vmalloc.o.

lib/test_vmalloc.c | 104 ++++++++++++++++++++++++++++-----------------
mm/vmalloc.c | 1 +
2 files changed, 67 insertions(+), 38 deletions(-)

diff --git a/lib/test_vmalloc.c b/lib/test_vmalloc.c
index b23f85e8f8ca..420b4bd5bf2f 100644
--- a/lib/test_vmalloc.c
+++ b/lib/test_vmalloc.c
@@ -17,6 +17,7 @@
#include <linux/rcupdate.h>
#include <linux/srcu.h>
#include <linux/slab.h>
+#include <linux/string.h>

#define __param(type, name, init, msg) \
static type name = init; \
@@ -422,55 +423,71 @@ vm_map_ram_test(void)
return nr_allocated != map_nr_pages;
}

-static int vrealloc_test(void)
+static void vrealloc_fill(u8 *ptr, size_t size)
{
- void *ptr, *tmp;
- int i;
-
- for (i = 0; i < test_loop_count; i++) {
- int err = -1;
+ for (size_t i = 0; i < size; i++)
+ ptr[i] = i % 251 + 1;
+}

- ptr = vrealloc(NULL, PAGE_SIZE, GFP_KERNEL);
- if (!ptr)
- return -1;
+static int vrealloc_check(const u8 *ptr, size_t size, size_t preserved,
+ gfp_t flags)
+{
+ struct vm_struct *vm = find_vm_area(ptr);

- *((__u8 *)ptr) = 'a';
+ /* vrealloc() allocates order-0 pages, allowing tail pages to be freed. */
+ if (!vm || vm->nr_pages != DIV_ROUND_UP(size, PAGE_SIZE)) {
+ pr_err("vrealloc: unexpected page count for size %zu\n", size);
+ return -1;
+ }

- /* Grow: beyond allocated pages, triggers full realloc. */
- tmp = vrealloc(ptr, 4 * PAGE_SIZE, GFP_KERNEL);
- if (!tmp)
- goto error;
- ptr = tmp;
+ for (size_t i = 0; i < preserved; i++) {
+ if (ptr[i] != i % 251 + 1) {
+ pr_err("vrealloc: data mismatch at offset %zu\n", i);
+ return -1;
+ }
+ }

- if (*((__u8 *)ptr) != 'a')
- goto error;
+ if ((flags & __GFP_ZERO) &&
+ memchr_inv(ptr + preserved, 0, size - preserved)) {
+ pr_err("vrealloc: nonzero bytes in newly exposed range\n");
+ return -1;
+ }

- /* Shrink: crosses page boundary, frees tail pages. */
- tmp = vrealloc(ptr, PAGE_SIZE, GFP_KERNEL);
- if (!tmp)
- goto error;
- ptr = tmp;
+ return 0;
+}

- if (*((__u8 *)ptr) != 'a')
- goto error;
+static int vrealloc_test_flags(gfp_t flags)
+{
+ static const size_t sizes[] = {
+ PAGE_SIZE,
+ 4 * PAGE_SIZE, /* Grow beyond allocated pages. */
+ PAGE_SIZE + 17, /* Shrink to a partial final page. */
+ PAGE_SIZE, /* Free the remaining tail page. */
+ PAGE_SIZE / 2, /* Shrink within the same page. */
+ PAGE_SIZE, /* Grow within the allocated page. */
+ 4 * PAGE_SIZE, /* Grow beyond pages retained after shrink. */
+ };
+ int i;

- /* Shrink: within same page, no page freeing. */
- tmp = vrealloc(ptr, PAGE_SIZE / 2, GFP_KERNEL);
- if (!tmp)
- goto error;
- ptr = tmp;
+ for (i = 0; i < test_loop_count; i++) {
+ void *ptr = NULL, *tmp;
+ size_t old_size = 0;
+ int err = -1;

- if (*((__u8 *)ptr) != 'a')
- goto error;
+ for (size_t j = 0; j < ARRAY_SIZE(sizes); j++) {
+ /* __GFP_ZERO must be used consistently, including at allocation. */
+ tmp = vrealloc(ptr, sizes[j], flags);
+ if (!tmp)
+ goto error;
+ ptr = tmp;

- /* Grow: within allocated page, in-place, no realloc. */
- tmp = vrealloc(ptr, PAGE_SIZE, GFP_KERNEL);
- if (!tmp)
- goto error;
- ptr = tmp;
+ if (vrealloc_check(ptr, sizes[j], min(old_size, sizes[j]),
+ flags))
+ goto error;

- if (*((__u8 *)ptr) != 'a')
- goto error;
+ vrealloc_fill(ptr, sizes[j]);
+ old_size = sizes[j];
+ }

err = 0;
error:
@@ -482,6 +499,17 @@ static int vrealloc_test(void)
return 0;
}

+static int vrealloc_test(void)
+{
+ int ret;
+
+ ret = vrealloc_test_flags(GFP_KERNEL);
+ if (ret)
+ return ret;
+
+ return vrealloc_test_flags(GFP_KERNEL | __GFP_ZERO);
+}
+
struct test_case_desc {
const char *test_name;
int (*test_func)(void);
diff --git a/mm/vmalloc.c b/mm/vmalloc.c
index f7aa4686fe5b..a0ee9dc742de 100644
--- a/mm/vmalloc.c
+++ b/mm/vmalloc.c
@@ -4220,6 +4220,7 @@ void *__vmalloc_node_noprof(unsigned long size, unsigned long align,
*/
#ifdef CONFIG_TEST_VMALLOC_MODULE
EXPORT_SYMBOL_GPL(__vmalloc_node_noprof);
+EXPORT_SYMBOL_GPL(find_vm_area);
#endif

void *__vmalloc_noprof(unsigned long size, gfp_t gfp_mask)
--
2.25.1