rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Boqun Feng <boqun.feng@gmail.com>
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>,
	Danilo Krummrich <dakr@kernel.org>,
	Alice Ryhl <aliceryhl@google.com>,
	rust-for-linux@vger.kernel.org
Subject: Re: [PATCH v7 3/4] rust: add support for NUMA ids in allocations
Date: Fri, 27 Jun 2025 15:32:06 -0700	[thread overview]
Message-ID: <aF8b5q0qJvUQ-Q8s@tardis.local> (raw)
In-Reply-To: <20250627212556.2181749-1-vitaly.wool@konsulko.se>

On Fri, Jun 27, 2025 at 11:25:56PM +0200, Vitaly Wool wrote:
[...]
> +/// Indicates an attempt to specify wrong NUMA node
> +#[derive(Copy, Clone, PartialEq, Eq, Debug)]
> +pub struct NumaError;

Do we need a separate error? I think we can just use EINVAL...

>  use core::{alloc::Layout, ptr::NonNull};
>  
>  /// Flags to be used when allocating memory.
> @@ -115,6 +119,28 @@ pub mod flags {
>      pub const __GFP_NOWARN: Flags = Flags(bindings::__GFP_NOWARN);
>  }
>  
> +/// Non Uniform Memory Access (NUMA) node identifier
> +#[derive(Clone, Copy, PartialEq)]
> +pub struct NumaNode(i32);
> +
> +impl NumaNode {
> +    /// create a new NUMA node identifer (non-negative integer)
> +    /// returns NumaError if a negative id is specified
> +    pub fn new(node: i32) -> Result<Self,NumaError> {

.. then this will be just -> Result<Self>


> +        if node >= 0 { Ok(Self(node)) }
> +        else { Err(NumaError) }

Have you run `make rustfmt`? This code style looks weird to me.

> +    }
> +}
> +
> +/// Specify necessary constant to pass the information to Allocator that the caller doesn't care
> +/// about the NUMA node to allocate memory from
> +pub mod numa_node {

Nit: I think we can name this mod as just 'numa'.

> +    use super::NumaNode;
> +
> +    /// No preference for NUMA node
> +    pub const NUMA_NO_NODE: NumaNode = NumaNode(bindings::NUMA_NO_NODE);
> +}
> +
>  /// The kernel's [`Allocator`] trait.
>  ///
>  /// An implementation of [`Allocator`] can allocate, re-allocate and free memory buffers described
> @@ -156,10 +182,41 @@ pub unsafe trait Allocator {
>      fn alloc(layout: Layout, flags: Flags) -> 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) }
> +        unsafe {
> +            Self::realloc_node(None, layout, Layout::new::<()>(), flags, numa_node::NUMA_NO_NODE)
> +        }

I don't think this change is necessary, because you implement
`Self::relloc()` below based on `Self::realloc_node()`?

>      }
>  
> -    /// 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` when called with `None`.

s/realloc/realloc_node

> +    ///
> +    /// # 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`],

/// [`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` is valid by its safety requirements and asks for a

s/realloc/realloc_node

Regards,
Boqun

> +        // 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
> +    /// optionally 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 NUMA_NO_NODE if the caller doesn't care.
>      ///
>      /// If the requested size is zero, `realloc` behaves equivalent to `free`.
>      ///
[...]

  reply	other threads:[~2025-06-27 22:32 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-06-27 21:24 [PATCH v7 0/4] support large align and nid in Rust allocators Vitaly Wool
2025-06-27 21:25 ` [PATCH v7 1/4] mm/vmalloc: allow to set node and align in vrealloc Vitaly Wool
2025-06-27 21:25 ` [PATCH v7 2/4] mm/slub: allow to set node and align in k[v]realloc Vitaly Wool
2025-06-27 21:25 ` [PATCH v7 3/4] rust: add support for NUMA ids in allocations Vitaly Wool
2025-06-27 22:32   ` Boqun Feng [this message]
2025-06-27 21:26 ` [PATCH v7 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=aF8b5q0qJvUQ-Q8s@tardis.local \
    --to=boqun.feng@gmail.com \
    --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 \
    --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).