Re: [PATCH v8 3/4] rust: add support for NUMA ids in allocations

From: Danilo Krummrich
Date: Sat Jun 28 2025 - 08:21:16 EST


On Sat, Jun 28, 2025 at 12:26:11PM +0200, Vitaly Wool wrote:
> +/// 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 EINVAL if a negative id is specified
> + pub fn new(node: i32) -> Result<Self> {
> + if node < 0 {
> + return Err(EINVAL);
> + }

Should we also check for MAX_NUMNODES?

> + Ok(Self(node))
> + }
> +}

<snip>

> + /// Re-allocate an existing memory allocation to satisfy the requested `layout` and
> + /// optionally a specific NUMA node request to allocate the memory for.

It's not an Option anymore, so we may want to drop 'optionally'. Also please
leave an empty line here.

> + /// 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.

Please also explain what happens when the NumaNode changes between calls to
realloc_node().

Does it have to remain the same NumaNode? Do we need a safety requirement for
that?

(Btw. no need to send a new version right away, leave a few days for people to
catch up and comment on this one or the other patches before resending.)

> ///
> /// If the requested size is zero, `realloc` behaves equivalent to `free`.
> ///
> @@ -191,13 +246,29 @@ 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>;