linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Vitaly Wool <vitaly.wool@konsulko.se>
To: Danilo Krummrich <dakr@kernel.org>
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 v10 3/4] rust: add support for NUMA ids in allocations
Date: Sat, 5 Jul 2025 09:17:10 +0200	[thread overview]
Message-ID: <5BDED328-4E79-4E39-95C3-21C3FB80593C@konsulko.se> (raw)
In-Reply-To: <aGfx6c72FgHn3NNW@pollux>



> On Jul 4, 2025, at 5:23 PM, Danilo Krummrich <dakr@kernel.org> wrote:
> 
> On Wed, Jul 02, 2025 at 06:09:10PM +0200, Vitaly Wool wrote:
>> /// The kernel's [`Allocator`] trait.
>> ///
>> /// An implementation of [`Allocator`] can allocate, re-allocate and free memory buffers described
>> @@ -148,7 +172,7 @@ pub unsafe trait Allocator {
>>     ///
>>     /// 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`],
>> +    ///   [`Allocator::free`], [`Allocator::realloc`] or [`Allocator::realloc_node`],
>>     /// - aligned to `layout.align()`,
>>     ///
>>     /// Additionally, `Flags` are honored as documented in
>> @@ -159,7 +183,41 @@ fn alloc(layout: Layout, flags: Flags) -> Result<NonNull<[u8]>, AllocError> {
>>         unsafe { Self::realloc(None, layout, Layout::new::<()>(), flags) }
>>     }
>> 
>> -    /// Re-allocate an existing memory allocation to satisfy the requested `layout`.
>> +    /// Allocate memory based on `layout`, `flags` and `nid`.
>> +    ///
>> +    /// 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_node` 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`], [`Allocator::realloc`] or [`Allocator::realloc_node`],
>> +    /// - 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: NumaNode,
>> +    ) -> Result<NonNull<[u8]>, AllocError> {
>> +        // SAFETY: Passing `None` to `realloc_node` is valid by its safety requirements and
>> +        // asks for a new memory allocation.
>> +        unsafe { Self::realloc_node(None, layout, Layout::new::<()>(), flags, nid) }
>> +    }
>> +
>> +    /// Re-allocate an existing memory allocation to satisfy the requested `layout` and
>> +    /// a specific NUMA node request to allocate the memory for.
>> +    ///
>> +    /// Systems employing a Non Uniform Memory Access (NUMA) architecture contain collections of
>> +    /// hardware resources including processors, memory, and I/O buses, that comprise what is
>> +    /// commonly known as a NUMA node.
>> +    ///
>> +    /// `nid` stands for NUMA id, i. e. NUMA node identifier, which is a non-negative
>> +    /// integer if a node needs to be specified, or NO_NODE if the caller doesn't care.
>>     ///
>>     /// If the requested size is zero, `realloc` behaves equivalent to `free`.
>>     ///
>> @@ -191,13 +249,27 @@ fn alloc(layout: Layout, flags: Flags) -> Result<NonNull<[u8]>, AllocError> {
>>     ///   and old size, i.e. `ret_ptr[0..min(layout.size(), old_layout.size())] ==
>>     ///   p[0..min(layout.size(), old_layout.size())]`.
>>     /// - when the return value is `Err(AllocError)`, then `ptr` is still valid.
>> -    unsafe fn realloc(
>> +    unsafe fn realloc_node(
>>         ptr: Option<NonNull<u8>>,
>>         layout: Layout,
>>         old_layout: Layout,
>>         flags: Flags,
>> +        nid: NumaNode,
>>     ) -> Result<NonNull<[u8]>, AllocError>;
>> 
>> +    /// Re-allocate an existing memory allocation to satisfy the requested `layout`. This
>> +    /// function works exactly as realloc_node() but it doesn't give the ability to specify
>> +    /// the NUMA node in the call.
>> +    unsafe fn realloc(
>> +        ptr: Option<NonNull<u8>>,
>> +        layout: Layout,
>> +        old_layout: Layout,
>> +        flags: Flags,
>> +    ) -> Result<NonNull<[u8]>, AllocError> {
>> +        // SAFETY: guaranteed by realloc_node()
>> +        unsafe { Self::realloc_node(ptr, layout, old_layout, flags, NumaNode::NO_NODE) }
>> +    }
> 
> I think Alice suggested to just drop alloc_node() and realloc_node() and make
> alloc() and realloc() always take a NumaNode argument.
> 
> I don't have a strong preference, but keeping only alloc() and realloc() for
> seems indeed simpler, so let's remove the _node() variants.

I don’t have a strong preference either but if I modify alloc() and realloc(), I’ll have to change KVec and KBox in the same patch, would that be okay with you?

~Vitaly

<snip>


  reply	other threads:[~2025-07-05  7:17 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-07-02 16:07 [PATCH v10 0/4] support large align and nid in Rust allocators Vitaly Wool
2025-07-02 16:08 ` [PATCH v10 1/4] mm/vmalloc: allow to set node and align in vrealloc Vitaly Wool
2025-07-04 10:10   ` Uladzislau Rezki
2025-07-02 16:08 ` [PATCH v10 2/4] mm/slub: allow to set node and align in k[v]realloc Vitaly Wool
2025-07-02 16:09 ` [PATCH v10 3/4] rust: add support for NUMA ids in allocations Vitaly Wool
2025-07-04 15:23   ` Danilo Krummrich
2025-07-05  7:17     ` Vitaly Wool [this message]
2025-07-07  7:25       ` Alice Ryhl
2025-07-06 15:18   ` kernel test robot
2025-07-02 16:09 ` [PATCH v10 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=5BDED328-4E79-4E39-95C3-21C3FB80593C@konsulko.se \
    --to=vitaly.wool@konsulko.se \
    --cc=akpm@linux-foundation.org \
    --cc=aliceryhl@google.com \
    --cc=dakr@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=urezki@gmail.com \
    /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