Reimplement vrealloc() to be able to set node and alignment should
a user need to do so. Rename the function to vrealloc_node() to
better match what it actually does now and introduce a macro for
vrealloc() for backward compatibility.
With that change we also provide the ability for the Rust part of
the kernel to set node and aligmnent in its allocations.
Signed-off-by: Vitaly Wool <vitaly.wool@konsulko.se>
---
include/linux/vmalloc.h | 8 +++++---
mm/vmalloc.c | 16 +++++++++++++---
2 files changed, 18 insertions(+), 6 deletions(-)
diff --git a/include/linux/vmalloc.h b/include/linux/vmalloc.h
index fdc9aeb74a44..7d5251287687 100644
--- a/include/linux/vmalloc.h
+++ b/include/linux/vmalloc.h
@@ -197,9 +197,11 @@ extern void *__vcalloc_noprof(size_t n, size_t size, gfp_t flags) __alloc_size(1
extern void *vcalloc_noprof(size_t n, size_t size) __alloc_size(1, 2);
#define vcalloc(...) alloc_hooks(vcalloc_noprof(__VA_ARGS__))
-void * __must_check vrealloc_noprof(const void *p, size_t size, gfp_t flags)
- __realloc_size(2);
-#define vrealloc(...) alloc_hooks(vrealloc_noprof(__VA_ARGS__))
+void *__must_check vrealloc_node_noprof(const void *p, size_t size,
+ unsigned long align, gfp_t flags, int nid) __realloc_size(2);
+#define vrealloc_noprof(p, s, f) vrealloc_node_noprof(p, s, 1, f, NUMA_NO_NODE)
+#define vrealloc_node(...) alloc_hooks(vrealloc_node_noprof(__VA_ARGS__))
+#define vrealloc(...) alloc_hooks(vrealloc_noprof(__VA_ARGS__))
extern void vfree(const void *addr);
extern void vfree_atomic(const void *addr);
diff --git a/mm/vmalloc.c b/mm/vmalloc.c
index ab986dd09b6a..117894301db1 100644
--- a/mm/vmalloc.c
+++ b/mm/vmalloc.c
@@ -4081,10 +4081,12 @@ void *vzalloc_node_noprof(unsigned long size, int node)
EXPORT_SYMBOL(vzalloc_node_noprof);
/**
- * vrealloc - reallocate virtually contiguous memory; contents remain unchanged
+ * vrealloc_node - reallocate virtually contiguous memory; contents remain unchanged
* @p: object to reallocate memory for
* @size: the size to reallocate
+ * @align: requested alignment
* @flags: the flags for the page level allocator
+ * @nid: node id
*
* If @p is %NULL, vrealloc() behaves exactly like vmalloc(). If @size is 0 and
* @p is not a %NULL pointer, the object pointed to is freed.
@@ -4103,7 +4105,7 @@ EXPORT_SYMBOL(vzalloc_node_noprof);
* Return: pointer to the allocated memory; %NULL if @size is zero or in case of
* failure
*/
-void *vrealloc_noprof(const void *p, size_t size, gfp_t flags)
+void *vrealloc_node_noprof(const void *p, size_t size, unsigned long align, gfp_t flags, int nid)
{
struct vm_struct *vm = NULL;
size_t alloced_size = 0;
@@ -4127,6 +4129,13 @@ void *vrealloc_noprof(const void *p, size_t size, gfp_t flags)
if (WARN(alloced_size < old_size,
"vrealloc() has mismatched area vs requested sizes (%p)\n", p))
return NULL;
+ if (WARN(nid != NUMA_NO_NODE && nid != page_to_nid(vmalloc_to_page(p)),
+ "vrealloc() has mismatched nids\n"))
+ return NULL;
+ if (WARN((uintptr_t)p & (align - 1),
+ "will not reallocate with a bigger alignment (0x%lx)\n",
+ align))
+ return NULL;
}
/*
@@ -4158,7 +4167,8 @@ void *vrealloc_noprof(const void *p, size_t size, gfp_t flags)
}
/* TODO: Grow the vm_area, i.e. allocate and map additional pages. */
- n = __vmalloc_noprof(size, flags);
+ n = __vmalloc_node_noprof(size, align, flags, nid, __builtin_return_address(0));
+
if (!n)
return NULL;
--
2.39.2
Hi Vitaly, kernel test robot noticed the following build errors: [auto build test ERROR on akpm-mm/mm-everything] [also build test ERROR on rust/alloc-next rust/rust-next linus/master v6.16-rc3 next-20250624] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in https://git-scm.com/docs/git-format-patch#_base_tree_information] url: https://github.com/intel-lab-lkp/linux/commits/Vitaly-Wool/mm-vmalloc-allow-to-set-node-and-align-in-vrealloc/20250624-204140 base: https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-everything patch link: https://lore.kernel.org/r/20250624123859.3258172-1-vitaly.wool%40konsulko.se patch subject: [PATCH v2 1/2] mm/vmalloc: allow to set node and align in vrealloc config: m68k-allnoconfig (https://download.01.org/0day-ci/archive/20250625/202506250832.Ixp27fE5-lkp@intel.com/config) compiler: m68k-linux-gcc (GCC) 15.1.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250625/202506250832.Ixp27fE5-lkp@intel.com/reproduce) If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags | Reported-by: kernel test robot <lkp@intel.com> | Closes: https://lore.kernel.org/oe-kbuild-all/202506250832.Ixp27fE5-lkp@intel.com/ All errors (new ones prefixed by >>): In file included from mm/nommu.c:28: >> include/linux/vmalloc.h:202:68: error: expected declaration specifiers or '...' before numeric constant 202 | #define vrealloc_noprof(p, s, f) vrealloc_node_noprof(p, s, 1, f, NUMA_NO_NODE) | ^ mm/nommu.c:122:7: note: in expansion of macro 'vrealloc_noprof' 122 | void *vrealloc_noprof(const void *p, size_t size, gfp_t flags) | ^~~~~~~~~~~~~~~ In file included from include/linux/nodemask.h:93, from include/linux/numa.h:6, from include/linux/cpumask.h:17, from include/linux/smp.h:13, from include/linux/lockdep.h:14, from include/linux/spinlock.h:63, from include/linux/mmzone.h:8, from include/linux/gfp.h:7, from include/linux/mm.h:7, from mm/nommu.c:20: >> include/linux/nodemask_types.h:15:25: error: expected declaration specifiers or '...' before '(' token 15 | #define NUMA_NO_NODE (-1) | ^ include/linux/vmalloc.h:202:74: note: in expansion of macro 'NUMA_NO_NODE' 202 | #define vrealloc_noprof(p, s, f) vrealloc_node_noprof(p, s, 1, f, NUMA_NO_NODE) | ^~~~~~~~~~~~ mm/nommu.c:122:7: note: in expansion of macro 'vrealloc_noprof' 122 | void *vrealloc_noprof(const void *p, size_t size, gfp_t flags) | ^~~~~~~~~~~~~~~ vim +202 include/linux/vmalloc.h 199 200 void *__must_check vrealloc_node_noprof(const void *p, size_t size, 201 unsigned long align, gfp_t flags, int nid) __realloc_size(2); > 202 #define vrealloc_noprof(p, s, f) vrealloc_node_noprof(p, s, 1, f, NUMA_NO_NODE) 203 #define vrealloc_node(...) alloc_hooks(vrealloc_node_noprof(__VA_ARGS__)) 204 #define vrealloc(...) alloc_hooks(vrealloc_noprof(__VA_ARGS__)) 205 -- 0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
© 2016 - 2025 Red Hat, Inc.