Re: [PATCH v9 1/4] mm/vmalloc: allow to set node and align in vrealloc

From: Uladzislau Rezki
Date: Tue Jul 01 2025 - 07:16:19 EST


On Tue, Jul 01, 2025 at 12:54:36PM +0200, Vitaly Wool wrote:
>
>
> > On Jul 1, 2025, at 12:50 PM, Uladzislau Rezki <urezki@xxxxxxxxx> wrote:
> >
> > On Tue, Jul 01, 2025 at 12:16:15AM +0200, Vitaly Wool wrote:
> >> Reimplement vrealloc() to be able to set node and alignment should
> >> a user need to do so. Rename the function to vrealloc_node_align()
> >> to better match what it actually does now and introduce macros for
> >> vrealloc() and friends 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@xxxxxxxxxxx>
> >> ---
> >> include/linux/vmalloc.h | 12 +++++++++---
> >> mm/vmalloc.c | 19 +++++++++++++++----
> >> 2 files changed, 24 insertions(+), 7 deletions(-)
> >>
> >> diff --git a/include/linux/vmalloc.h b/include/linux/vmalloc.h
> >> index fdc9aeb74a44..68791f7cb3ba 100644
> >> --- a/include/linux/vmalloc.h
> >> +++ b/include/linux/vmalloc.h
> >> @@ -197,9 +197,15 @@ 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_align_noprof(const void *p, size_t size,
> >> + unsigned long align, gfp_t flags, int nid) __realloc_size(2);
> >> +#define vrealloc_node_noprof(_p, _s, _f, _nid) \
> >> + vrealloc_node_align_noprof(_p, _s, 1, _f, _nid)
> >> +#define vrealloc_noprof(_p, _s, _f) \
> >> + vrealloc_node_align_noprof(_p, _s, 1, _f, NUMA_NO_NODE)
> >> +#define vrealloc_node_align(...) alloc_hooks(vrealloc_node_align_noprof(__VA_ARGS__))
> >> +#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 6dbcdceecae1..776c68f84ce2 100644
> >> --- a/mm/vmalloc.c
> >> +++ b/mm/vmalloc.c
> >> @@ -4089,12 +4089,15 @@ void *vzalloc_node_noprof(unsigned long size, int node)
> >> EXPORT_SYMBOL(vzalloc_node_noprof);
> >>
> >> /**
> >> - * vrealloc - reallocate virtually contiguous memory; contents remain unchanged
> >> + * vrealloc_node_align_noprof - 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
> >> + * If @p is %NULL, vrealloc_XXX() behaves exactly like vmalloc(). If @size is 0 and
> >> * @p is not a %NULL pointer, the object pointed to is freed.
> >> *
> >> * If __GFP_ZERO logic is requested, callers must ensure that, starting with the
> >> @@ -4111,7 +4114,8 @@ 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_align_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;
> >> @@ -4135,6 +4139,11 @@ 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(!IS_ALIGNED((unsigned long)p, align),
> >> + "will not reallocate with a bigger alignment (0x%lx)\n", align))
> >> + return NULL;
> >> + if (nid != NUMA_NO_NODE && nid != page_to_nid(vmalloc_to_page(p)))
> >> + goto need_realloc;
> >>
> > By this goto change, you bypass the two important checks below. For
> > example if you shrink the allocated size, you do not need to perform
> > any allocations. Instead the patch goes and allocates a new area.
> >
> > You just need to remove:
> >
> > - if (nid != NUMA_NO_NODE && nid != page_to_nid(vmalloc_to_page(p)))
> > - goto need_realloc;
> >
> > to make it working.
> >
>
> I am not sure I’m following. If we get a request to reallocate for a different node then we should either reject it or do the new allocation for this new node and copy the data to the new place. Shrinking the allocation on the old node doesn’t seem to be right. Or am I missing something?
>
If your process migrates to a new NODE, which is fine, it does not mean
that you have to perform all this bouncing movement(reallocate on a new node).
Next time it can be migrated back. Process are allowed to migrate and access
to a remote memory.

Let's keep it simple.

--
Uladzislau Rezki