[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <aF0-tYomGe7nB5vX@pollux>
Date: Thu, 26 Jun 2025 14:36:05 +0200
From: Danilo Krummrich <dakr@...nel.org>
To: Vitaly Wool <vitaly.wool@...sulko.se>
Cc: linux-mm@...ck.org, akpm@...ux-foundation.org,
linux-kernel@...r.kernel.org, Uladzislau Rezki <urezki@...il.com>,
Alice Ryhl <aliceryhl@...gle.com>, rust-for-linux@...r.kernel.org
Subject: Re: [PATCH v4 3/4] rust: support large alignments in allocations
On Thu, Jun 26, 2025 at 10:36:42AM +0200, Vitaly Wool wrote:
> void * __must_check __realloc_size(2)
> -rust_helper_krealloc(const void *objp, size_t new_size, gfp_t flags)
> +rust_helper_krealloc(const void *objp, size_t new_size, unsigned long align, gfp_t flags)
> {
> return krealloc(objp, new_size, flags);
> }
>
> void * __must_check __realloc_size(2)
> -rust_helper_kvrealloc(const void *p, size_t size, gfp_t flags)
> +rust_helper_kvrealloc(const void *p, size_t size, unsigned long align, gfp_t flags)
> {
> return kvrealloc(p, size, flags);
> }
I think you forgot to add comments explaining why we have the additional
discarded align argument.
Also please keep those helpers as they are. You can write an identical inline
function in Rust that discards the align argument and calls bindings::krealloc,
etc.
For instance:
unsafe extern "C" fn krealloc_align(
ptr: *const c_void,
size: usize,
_align: c_ulong
flags: u32,
) -> *mut c_void {
bindings::krealloc(ptr, size, flags)
}
> diff --git a/rust/helpers/vmalloc.c b/rust/helpers/vmalloc.c
> index 80d34501bbc0..4618c0b79283 100644
> --- a/rust/helpers/vmalloc.c
> +++ b/rust/helpers/vmalloc.c
> @@ -3,7 +3,7 @@
> #include <linux/vmalloc.h>
>
> void * __must_check __realloc_size(2)
> -rust_helper_vrealloc(const void *p, size_t size, gfp_t flags)
> +rust_helper_vrealloc(const void *p, size_t size, unsigned long align, gfp_t flags)
> {
> - return vrealloc(p, size, flags);
> + return vrealloc_node(p, size, align, flags, NUMA_NO_NODE);
> }
Same here, just make this a "real" helper for vrealloc_node() and create a Rust
function vrealloc_align() like in the example above.
> diff --git a/rust/kernel/alloc/allocator.rs b/rust/kernel/alloc/allocator.rs
> index aa2dfa9dca4c..a0d78c497974 100644
> --- a/rust/kernel/alloc/allocator.rs
> +++ b/rust/kernel/alloc/allocator.rs
> @@ -58,7 +58,7 @@ fn aligned_size(new_layout: Layout) -> usize {
> ///
> /// One of the following: `krealloc`, `vrealloc`, `kvrealloc`.
> struct ReallocFunc(
> - unsafe extern "C" fn(*const crate::ffi::c_void, usize, u32) -> *mut crate::ffi::c_void,
> + unsafe extern "C" fn(*const crate::ffi::c_void, usize, usize, u32) -> *mut crate::ffi::c_void,
Should be c_ulong instead of usize.
> );
>
> impl ReallocFunc {
> @@ -110,7 +110,7 @@ unsafe fn call(
> // - Those functions provide the guarantees of this function.
> let raw_ptr = unsafe {
> // If `size == 0` and `ptr != NULL` the memory behind the pointer is freed.
> - self.0(ptr.cast(), size, flags.0).cast()
> + self.0(ptr.cast(), size, layout.align(), flags.0).cast()
> };
>
> let ptr = if size == 0 {
> @@ -152,12 +152,6 @@ unsafe fn realloc(
> old_layout: Layout,
> flags: Flags,
> ) -> Result<NonNull<[u8]>, AllocError> {
> - // TODO: Support alignments larger than PAGE_SIZE.
> - if layout.align() > bindings::PAGE_SIZE {
> - pr_warn!("Vmalloc does not support alignments larger than PAGE_SIZE yet.\n");
> - return Err(AllocError);
> - }
> -
> // SAFETY: If not `None`, `ptr` is guaranteed to point to valid memory, which was previously
> // allocated with this `Allocator`.
> unsafe { ReallocFunc::VREALLOC.call(ptr, layout, old_layout, flags) }
> @@ -176,12 +170,6 @@ unsafe fn realloc(
> old_layout: Layout,
> flags: Flags,
> ) -> Result<NonNull<[u8]>, AllocError> {
> - // TODO: Support alignments larger than PAGE_SIZE.
> - if layout.align() > bindings::PAGE_SIZE {
> - pr_warn!("KVmalloc does not support alignments larger than PAGE_SIZE yet.\n");
> - return Err(AllocError);
> - }
Didn't you propose to use VREALLOC if layout.align() > bindings::PAGE_SIZE?
> // SAFETY: If not `None`, `ptr` is guaranteed to point to valid memory, which was previously
> // allocated with this `Allocator`.
> unsafe { ReallocFunc::KVREALLOC.call(ptr, layout, old_layout, flags) }
Powered by blists - more mailing lists