linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] mm/vmalloc: allow to set node and align in vrealloc
@ 2025-06-24  9:51 Vitaly Wool
  2025-06-24 12:02 ` Uladzislau Rezki
  0 siblings, 1 reply; 7+ messages in thread
From: Vitaly Wool @ 2025-06-24  9:51 UTC (permalink / raw)
  To: linux-mm
  Cc: akpm, linux-kernel, Uladzislau Rezki, Danilo Krummrich,
	Alice Ryhl, rust-for-linux, Vitaly Wool

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



^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH] mm/vmalloc: allow to set node and align in vrealloc
  2025-06-24  9:51 [PATCH] mm/vmalloc: allow to set node and align in vrealloc Vitaly Wool
@ 2025-06-24 12:02 ` Uladzislau Rezki
  2025-06-24 12:15   ` Vitaly Wool
  0 siblings, 1 reply; 7+ messages in thread
From: Uladzislau Rezki @ 2025-06-24 12:02 UTC (permalink / raw)
  To: Vitaly Wool
  Cc: linux-mm, akpm, linux-kernel, Uladzislau Rezki, Danilo Krummrich,
	Alice Ryhl, rust-for-linux

On Tue, Jun 24, 2025 at 11:51:21AM +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() 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
> 
Do we have users which require alignment and nid? I see that it is part
of kvrealloc() API only.

--
Uladzislau Rezki


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] mm/vmalloc: allow to set node and align in vrealloc
  2025-06-24 12:02 ` Uladzislau Rezki
@ 2025-06-24 12:15   ` Vitaly Wool
  2025-06-24 12:21     ` Danilo Krummrich
  2025-06-24 12:22     ` Alice Ryhl
  0 siblings, 2 replies; 7+ messages in thread
From: Vitaly Wool @ 2025-06-24 12:15 UTC (permalink / raw)
  To: Uladzislau Rezki
  Cc: linux-mm, akpm, linux-kernel, Danilo Krummrich, Alice Ryhl,
	rust-for-linux



> On Jun 24, 2025, at 2:02 PM, Uladzislau Rezki <urezki@gmail.com> wrote:
> 
> On Tue, Jun 24, 2025 at 11:51:21AM +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() 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
>> 
> Do we have users which require alignment and nid? I see that it is part
> of kvrealloc() API only.
> 

There’s a patch pending inclusion of this one which will be the user of this change. I was reluctant
to combine these 2 in one series because the second one is on the Rust side, but it can be found
here: https://github.com/vwool/linux-mm/pull/new/realloc-extend.

~Vitaly

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] mm/vmalloc: allow to set node and align in vrealloc
  2025-06-24 12:15   ` Vitaly Wool
@ 2025-06-24 12:21     ` Danilo Krummrich
  2025-06-24 12:30       ` Vitaly Wool
  2025-06-24 12:22     ` Alice Ryhl
  1 sibling, 1 reply; 7+ messages in thread
From: Danilo Krummrich @ 2025-06-24 12:21 UTC (permalink / raw)
  To: Vitaly Wool
  Cc: Uladzislau Rezki, linux-mm, akpm, linux-kernel, Alice Ryhl,
	rust-for-linux

On Tue, Jun 24, 2025 at 02:15:01PM +0200, Vitaly Wool wrote:
> There’s a patch pending inclusion of this one which will be the user of this change. I was reluctant
> to combine these 2 in one series because the second one is on the Rust side, but it can be found
> here: https://github.com/vwool/linux-mm/pull/new/realloc-extend.

I think the link is broken, I suppose you wanted to refer to the patch in [1].

[1] https://github.com/vwool/linux-mm/commit/5af47ac6f9010814263617b2136203a54ed4c25e


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] mm/vmalloc: allow to set node and align in vrealloc
  2025-06-24 12:15   ` Vitaly Wool
  2025-06-24 12:21     ` Danilo Krummrich
@ 2025-06-24 12:22     ` Alice Ryhl
  2025-06-24 12:28       ` Vitaly Wool
  1 sibling, 1 reply; 7+ messages in thread
From: Alice Ryhl @ 2025-06-24 12:22 UTC (permalink / raw)
  To: Vitaly Wool
  Cc: Uladzislau Rezki, linux-mm, akpm, linux-kernel, Danilo Krummrich,
	rust-for-linux

On Tue, Jun 24, 2025 at 1:15 PM Vitaly Wool <vitaly.wool@konsulko.se> wrote:
>
>
>
> > On Jun 24, 2025, at 2:02 PM, Uladzislau Rezki <urezki@gmail.com> wrote:
> > Do we have users which require alignment and nid? I see that it is part
> > of kvrealloc() API only.
> >
>
> There’s a patch pending inclusion of this one which will be the user of this change. I was reluctant
> to combine these 2 in one series because the second one is on the Rust side, but it can be found
> here: https://github.com/vwool/linux-mm/pull/new/realloc-extend.

I think it is useful to include the immediate user in the same series
so we can see how it will be used.

Alice


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] mm/vmalloc: allow to set node and align in vrealloc
  2025-06-24 12:22     ` Alice Ryhl
@ 2025-06-24 12:28       ` Vitaly Wool
  0 siblings, 0 replies; 7+ messages in thread
From: Vitaly Wool @ 2025-06-24 12:28 UTC (permalink / raw)
  To: Alice Ryhl
  Cc: Uladzislau Rezki, linux-mm, akpm, linux-kernel, Danilo Krummrich,
	rust-for-linux



> On Jun 24, 2025, at 2:22 PM, Alice Ryhl <aliceryhl@google.com> wrote:
> 
> On Tue, Jun 24, 2025 at 1:15 PM Vitaly Wool <vitaly.wool@konsulko.se> wrote:
>> 
>> 
>> 
>>> On Jun 24, 2025, at 2:02 PM, Uladzislau Rezki <urezki@gmail.com> wrote:
>>> Do we have users which require alignment and nid? I see that it is part
>>> of kvrealloc() API only.
>>> 
>> 
>> There’s a patch pending inclusion of this one which will be the user of this change. I was reluctant
>> to combine these 2 in one series because the second one is on the Rust side, but it can be found
>> here: https://github.com/vwool/linux-mm/pull/new/realloc-extend.
> 
> I think it is useful to include the immediate user in the same series
> so we can see how it will be used.
> 
> Alice
> 

Alright, stay tuned.

~Vitaly

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] mm/vmalloc: allow to set node and align in vrealloc
  2025-06-24 12:21     ` Danilo Krummrich
@ 2025-06-24 12:30       ` Vitaly Wool
  0 siblings, 0 replies; 7+ messages in thread
From: Vitaly Wool @ 2025-06-24 12:30 UTC (permalink / raw)
  To: Danilo Krummrich
  Cc: Uladzislau Rezki, linux-mm, akpm, linux-kernel, Alice Ryhl,
	rust-for-linux



> On Jun 24, 2025, at 2:21 PM, Danilo Krummrich <dakr@kernel.org> wrote:
> 
> On Tue, Jun 24, 2025 at 02:15:01PM +0200, Vitaly Wool wrote:
>> There’s a patch pending inclusion of this one which will be the user of this change. I was reluctant
>> to combine these 2 in one series because the second one is on the Rust side, but it can be found
>> here: https://github.com/vwool/linux-mm/pull/new/realloc-extend.
> 
> I think the link is broken, I suppose you wanted to refer to the patch in [1].
> 
> [1] https://github.com/vwool/linux-mm/commit/5af47ac6f9010814263617b2136203a54ed4c25e
> 

You’re right, thanks a lot!

~Vitaly

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2025-06-24 12:30 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-06-24  9:51 [PATCH] mm/vmalloc: allow to set node and align in vrealloc Vitaly Wool
2025-06-24 12:02 ` Uladzislau Rezki
2025-06-24 12:15   ` Vitaly Wool
2025-06-24 12:21     ` Danilo Krummrich
2025-06-24 12:30       ` Vitaly Wool
2025-06-24 12:22     ` Alice Ryhl
2025-06-24 12:28       ` Vitaly Wool

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).