From: Danilo Krummrich <dakr@kernel.org>
To: Vitaly Wool <vitaly.wool@konsulko.se>
Cc: linux-mm@kvack.org, akpm@linux-foundation.org,
linux-kernel@vger.kernel.org, Uladzislau Rezki <urezki@gmail.com>,
Alice Ryhl <aliceryhl@google.com>,
rust-for-linux@vger.kernel.org
Subject: Re: [PATCH v5 3/4] rust: add support for NUMA ids in allocations
Date: Fri, 27 Jun 2025 13:20:11 +0200 [thread overview]
Message-ID: <aF5-a-bUp1pD5tiS@pollux> (raw)
In-Reply-To: <20250627093858.413855-1-vitaly.wool@konsulko.se>
On Fri, Jun 27, 2025 at 11:38:58AM +0200, Vitaly Wool wrote:
> + /// Allocate memory based on `layout`, `flags` and `nid`.
The semantical meanting of layout and flags is rather obvious due to its type.
We should probably introduce a new type NumaNode with a constant for
NUMA_NODE_NONE.
> + ///
> + /// On success, returns a buffer represented as `NonNull<[u8]>` that satisfies the layout
> + /// constraints (i.e. minimum size and alignment as specified by `layout`).
> + ///
> + /// This function is equivalent to `realloc` when called with `None`.
> + ///
> + /// # Guarantees
> + ///
> + /// When the return value is `Ok(ptr)`, then `ptr` is
> + /// - valid for reads and writes for `layout.size()` bytes, until it is passed to
> + /// [`Allocator::free`] or [`Allocator::realloc`],
> + /// - aligned to `layout.align()`,
> + ///
> + /// Additionally, `Flags` are honored as documented in
> + /// <https://docs.kernel.org/core-api/mm-api.html#mm-api-gfp-flags>.
> + fn alloc_node(layout: Layout, flags: Flags, nid: Option<i32>)
> + -> Result<NonNull<[u8]>, AllocError> {
> + // SAFETY: Passing `None` to `realloc` is valid by its safety requirements and asks for a
> + // new memory allocation.
> + unsafe { Self::realloc(None, layout, Layout::new::<()>(), flags, nid) }
> }
>
> /// Re-allocate an existing memory allocation to satisfy the requested `layout`.
> @@ -196,6 +219,7 @@ unsafe fn realloc(
> layout: Layout,
> old_layout: Layout,
> flags: Flags,
> + nid: Option<i32>,
> ) -> Result<NonNull<[u8]>, AllocError>;
Please rename to realloc_node() and expand the documentation explaining what
happens for invalid nid numbers and what happens if nid changes throughout
invocations.
Also, please introduce realloc() which calls realloc_node() with NUMA_NODE_NONE.
>
> /// Free an existing memory allocation.
> @@ -211,7 +235,7 @@ unsafe fn free(ptr: NonNull<u8>, layout: Layout) {
> // SAFETY: The caller guarantees that `ptr` points at a valid allocation created by this
> // allocator. We are passing a `Layout` with the smallest possible alignment, so it is
> // smaller than or equal to the alignment previously used with this allocation.
> - let _ = unsafe { Self::realloc(Some(ptr), Layout::new::<()>(), layout, Flags(0)) };
> + let _ = unsafe { Self::realloc(Some(ptr), Layout::new::<()>(), layout, Flags(0), None) };
> }
> }
>
> diff --git a/rust/kernel/alloc/allocator.rs b/rust/kernel/alloc/allocator.rs
> index aa2dfa9dca4c..4f0fe2b67593 100644
> --- a/rust/kernel/alloc/allocator.rs
> +++ b/rust/kernel/alloc/allocator.rs
> @@ -58,18 +58,20 @@ 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, crate::ffi::c_ulong, u32,
> + crate::ffi::c_int
> + ) -> *mut crate::ffi::c_void,
> );
>
> impl ReallocFunc {
> - // INVARIANT: `krealloc` satisfies the type invariants.
> - const KREALLOC: Self = Self(bindings::krealloc);
> + // INVARIANT: `krealloc_node` satisfies the type invariants.
> + const KREALLOC_NODE: Self = Self(bindings::krealloc_node);
>
> - // INVARIANT: `vrealloc` satisfies the type invariants.
> - const VREALLOC: Self = Self(bindings::vrealloc);
> + // INVARIANT: `vrealloc_node` satisfies the type invariants.
> + const VREALLOC_NODE: Self = Self(bindings::vrealloc_node);
>
> - // INVARIANT: `kvrealloc` satisfies the type invariants.
> - const KVREALLOC: Self = Self(bindings::kvrealloc);
> + // INVARIANT: `kvrealloc_node` satisfies the type invariants.
> + const KVREALLOC_NODE: Self = Self(bindings::kvrealloc_node);
I think those constants can remain to be named just KREALLOC, etc.
next prev parent reply other threads:[~2025-06-27 11:20 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-06-27 9:29 [PATCH v5 0/4] support large align and nid in Rust allocators Vitaly Wool
2025-06-27 9:35 ` [PATCH v5 1/4] mm/vmalloc: allow to set node and align in vrealloc Vitaly Wool
2025-06-27 9:37 ` [PATCH 2/4] mm/slub: allow to set node and align in k[v]realloc Vitaly Wool
2025-06-27 10:41 ` Danilo Krummrich
2025-06-27 11:42 ` Uladzislau Rezki
2025-06-27 12:01 ` Vitaly Wool
2025-06-27 17:54 ` Uladzislau Rezki
2025-06-27 22:14 ` Vitaly Wool
2025-06-27 11:57 ` Vitaly Wool
2025-06-27 9:38 ` [PATCH v5 3/4] rust: add support for NUMA ids in allocations Vitaly Wool
2025-06-27 11:20 ` Danilo Krummrich [this message]
2025-06-27 9:39 ` [PATCH v5 4/4] rust: support large alignments " Vitaly Wool
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=aF5-a-bUp1pD5tiS@pollux \
--to=dakr@kernel.org \
--cc=akpm@linux-foundation.org \
--cc=aliceryhl@google.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=urezki@gmail.com \
--cc=vitaly.wool@konsulko.se \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).