Add a new type to support specifying NUMA identifiers in Rust
allocators and extend the allocators to have NUMA id as a
parameter. Thus, modify ReallocFunc to use the new extended realloc
primitives from the C side of the kernel (i. e.
k[v]realloc_node_align/vrealloc_node_align) and add the new function
alloc_node to the Allocator trait while keeping the existing one
(alloc) for backward compatibility.
This will allow to specify node to use for allocation of e. g.
{KV}Box, as well as for future NUMA aware users of the API.
Signed-off-by: Vitaly Wool <vitaly.wool@konsulko.se>
---
rust/helpers/slab.c | 8 ++--
rust/helpers/vmalloc.c | 4 +-
rust/kernel/alloc.rs | 78 ++++++++++++++++++++++++++++++++--
rust/kernel/alloc/allocator.rs | 45 ++++++++++++--------
4 files changed, 108 insertions(+), 27 deletions(-)
diff --git a/rust/helpers/slab.c b/rust/helpers/slab.c
index a842bfbddcba..8472370a4338 100644
--- a/rust/helpers/slab.c
+++ b/rust/helpers/slab.c
@@ -3,13 +3,13 @@
#include <linux/slab.h>
void * __must_check __realloc_size(2)
-rust_helper_krealloc(const void *objp, size_t new_size, gfp_t flags)
+rust_helper_krealloc_node(const void *objp, size_t new_size, gfp_t flags, int node)
{
- return krealloc(objp, new_size, flags);
+ return krealloc_node(objp, new_size, flags, node);
}
void * __must_check __realloc_size(2)
-rust_helper_kvrealloc(const void *p, size_t size, gfp_t flags)
+rust_helper_kvrealloc_node(const void *p, size_t size, gfp_t flags, int node)
{
- return kvrealloc(p, size, flags);
+ return kvrealloc_node(p, size, flags, node);
}
diff --git a/rust/helpers/vmalloc.c b/rust/helpers/vmalloc.c
index 80d34501bbc0..62d30db9a1a6 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_node(const void *p, size_t size, gfp_t flags, int node)
{
- return vrealloc(p, size, flags);
+ return vrealloc_node(p, size, flags, node);
}
diff --git a/rust/kernel/alloc.rs b/rust/kernel/alloc.rs
index a2c49e5494d3..b178ebdf2d87 100644
--- a/rust/kernel/alloc.rs
+++ b/rust/kernel/alloc.rs
@@ -28,6 +28,8 @@
/// Indicates an allocation error.
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub struct AllocError;
+
+use crate::error::{code::EINVAL, Result};
use core::{alloc::Layout, ptr::NonNull};
/// Flags to be used when allocating memory.
@@ -115,6 +117,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 EINVAL if a negative id or an id exceeding MAX_NUMNODES is specified
+ pub fn new(node: i32) -> Result<Self> {
+ // SAFETY: MAX_NUMNODES never exceeds 2**10 because NODES_SHIFT is 0..10
+ if node < 0 || node >= bindings::MAX_NUMNODES as i32 {
+ return Err(EINVAL);
+ }
+ Ok(Self(node))
+ }
+}
+
+/// Specify necessary constant to pass the information to Allocator that the caller doesn't care
+/// about the NUMA node to allocate memory from.
+impl NumaNode {
+ pub const 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
@@ -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) }
+ }
+
/// Free an existing memory allocation.
///
/// # Safety
diff --git a/rust/kernel/alloc/allocator.rs b/rust/kernel/alloc/allocator.rs
index aa2dfa9dca4c..15d737fe89ed 100644
--- a/rust/kernel/alloc/allocator.rs
+++ b/rust/kernel/alloc/allocator.rs
@@ -13,7 +13,7 @@
use core::ptr;
use core::ptr::NonNull;
-use crate::alloc::{AllocError, Allocator};
+use crate::alloc::{AllocError, Allocator, NumaNode};
use crate::bindings;
use crate::pr_warn;
@@ -58,18 +58,23 @@ 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,
+ 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: 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: 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: Self = Self(bindings::kvrealloc_node);
/// # Safety
///
@@ -87,6 +92,7 @@ unsafe fn call(
layout: Layout,
old_layout: Layout,
flags: Flags,
+ nid: NumaNode,
) -> Result<NonNull<[u8]>, AllocError> {
let size = aligned_size(layout);
let ptr = match ptr {
@@ -110,7 +116,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, flags.0, nid.0).cast()
};
let ptr = if size == 0 {
@@ -123,34 +129,36 @@ unsafe fn call(
}
}
-// SAFETY: `realloc` delegates to `ReallocFunc::call`, which guarantees that
+// SAFETY: `realloc_node` delegates to `ReallocFunc::call`, which guarantees that
// - memory remains valid until it is explicitly freed,
// - passing a pointer to a valid memory allocation is OK,
// - `realloc` satisfies the guarantees, since `ReallocFunc::call` has the same.
unsafe impl Allocator for Kmalloc {
#[inline]
- unsafe fn realloc(
+ unsafe fn realloc_node(
ptr: Option<NonNull<u8>>,
layout: Layout,
old_layout: Layout,
flags: Flags,
+ nid: NumaNode,
) -> Result<NonNull<[u8]>, AllocError> {
// SAFETY: `ReallocFunc::call` has the same safety requirements as `Allocator::realloc`.
- unsafe { ReallocFunc::KREALLOC.call(ptr, layout, old_layout, flags) }
+ unsafe { ReallocFunc::KREALLOC.call(ptr, layout, old_layout, flags, nid) }
}
}
-// SAFETY: `realloc` delegates to `ReallocFunc::call`, which guarantees that
+// SAFETY: `realloc_node` delegates to `ReallocFunc::call`, which guarantees that
// - memory remains valid until it is explicitly freed,
// - passing a pointer to a valid memory allocation is OK,
// - `realloc` satisfies the guarantees, since `ReallocFunc::call` has the same.
unsafe impl Allocator for Vmalloc {
#[inline]
- unsafe fn realloc(
+ unsafe fn realloc_node(
ptr: Option<NonNull<u8>>,
layout: Layout,
old_layout: Layout,
flags: Flags,
+ nid: NumaNode,
) -> Result<NonNull<[u8]>, AllocError> {
// TODO: Support alignments larger than PAGE_SIZE.
if layout.align() > bindings::PAGE_SIZE {
@@ -160,21 +168,22 @@ unsafe fn realloc(
// 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) }
+ unsafe { ReallocFunc::VREALLOC.call(ptr, layout, old_layout, flags, nid) }
}
}
-// SAFETY: `realloc` delegates to `ReallocFunc::call`, which guarantees that
+// SAFETY: `realloc_node` delegates to `ReallocFunc::call`, which guarantees that
// - memory remains valid until it is explicitly freed,
// - passing a pointer to a valid memory allocation is OK,
// - `realloc` satisfies the guarantees, since `ReallocFunc::call` has the same.
unsafe impl Allocator for KVmalloc {
#[inline]
- unsafe fn realloc(
+ unsafe fn realloc_node(
ptr: Option<NonNull<u8>>,
layout: Layout,
old_layout: Layout,
flags: Flags,
+ nid: NumaNode,
) -> Result<NonNull<[u8]>, AllocError> {
// TODO: Support alignments larger than PAGE_SIZE.
if layout.align() > bindings::PAGE_SIZE {
@@ -184,6 +193,6 @@ unsafe fn realloc(
// 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) }
+ unsafe { ReallocFunc::KVREALLOC.call(ptr, layout, old_layout, flags, nid) }
}
}
--
2.39.2
Hi Vitaly, kernel test robot noticed the following build warnings: [auto build test WARNING on akpm-mm/mm-everything] [also build test WARNING on rust/rust-next linus/master v6.16-rc4 next-20250704] [cannot apply to vbabka-slab/for-next] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in https://git-scm.com/docs/git-format-patch#_base_tree_information] url: https://github.com/intel-lab-lkp/linux/commits/Vitaly-Wool/mm-vmalloc-allow-to-set-node-and-align-in-vrealloc/20250703-001116 base: https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-everything patch link: https://lore.kernel.org/r/20250702160910.3610134-1-vitaly.wool%40konsulko.se patch subject: [PATCH v10 3/4] rust: add support for NUMA ids in allocations config: x86_64-rhel-9.4-rust (https://download.01.org/0day-ci/archive/20250706/202507062327.TsP3JVhL-lkp@intel.com/config) compiler: clang version 20.1.7 (https://github.com/llvm/llvm-project 6146a88f60492b520a36f8f8f3231e15f3cc6082) rustc: rustc 1.88.0 (6b00bc388 2025-06-23) reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250706/202507062327.TsP3JVhL-lkp@intel.com/reproduce) If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags | Reported-by: kernel test robot <lkp@intel.com> | Closes: https://lore.kernel.org/oe-kbuild-all/202507062327.TsP3JVhL-lkp@intel.com/ All warnings (new ones prefixed by >>): >> warning: missing documentation for an associated constant --> rust/kernel/alloc.rs:139:5 | 139 | pub const NO_NODE: NumaNode = NumaNode(bindings::NUMA_NO_NODE); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: requested on the command line with `-W missing-docs` -- 0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
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.
Regardless, please note that realloc() as you implemented it above misses the
safaety requirement, which should just mention that the safety requirements are
identical to realloc_node().
The safety comment on the subsequent call to realloc_node() would then be
justified with realloc() having identical safety requirements as realloc_node().
> 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>
On Sat, Jul 05, 2025 at 09:17:10AM +0200, Vitaly Wool wrote:
>
>
> > 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?
That's okay with me.
Alice
© 2016 - 2026 Red Hat, Inc.