rust: add a few helpers
Subsystems (3)
Andreas Hindborg
reviewerv4 cover rust: add a few helpers
This series adds a helper for obtaining information about the cpu count of the system. It also moves the NumaNode struct from alloc to numa.
---
Changes in v4:
- Dropped patch 1 of v3 (moved to separate series).
- Rebase on v7.3.
- Link to v3: https://msgid.link/[email protected]
Changes in v3:
- Drop Alice tag on patch 2 due to restructuring.
- Use a relaxed load to read nr_online_nodes (Gary).
- Move NumaNode from the alloc module to the numa module in a new patch (Alice).
- Rebase on v7.3-rc5.
- Link to v2: https://msgid.link/[email protected]
Changes in v2:
- Move helpers out of the crate root into the cpu and numa modules (Miguel).
- Mark Rust wrappers #[inline] (Alice).
- Mark the num_possible_cpus C helper __rust_helper (Dirk).
- Mention the Rust null block driver as the consumer in commit messages (Miguel).
- Link to v1: https://msgid.link/[email protected]
---
Andreas Hindborg (2):
rust: add a wrapper for the nr_online_nodes C function
rust: alloc: move NumaNode to the numa module
rust/kernel/alloc.rs | 30 +++----------------- rust/kernel/alloc/allocator.rs | 6 ++-- rust/kernel/alloc/kbox.rs | 4 +-- rust/kernel/alloc/kvec.rs | 4 +-- rust/kernel/lib.rs | 1 + rust/kernel/numa.rs | 62 ++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 74 insertions(+), 33 deletions(-)
---
base-commit: c03c1a14507dfaf8b856c2608f9ac216021fc222
change-id: 20260215-cpu-helpers-08efb2572487
Best regards,
Signature · 1 line
Andreas Hindborg <[email protected]>
Andreas Hindborg
reviewerPATCH v4 1/2 rust: add a wrapper for the `nr_online_nodes` C function
This function returns the number of online NUMA nodes.
The wrapper is needed by the Rust null block driver.
--- rust/kernel/lib.rs | 1 + rust/kernel/numa.rs | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+)
rust/kernel/lib.rs +1 -0
@@ -105,6 +105,7 @@#[cfg( CONFIG_NET )] pub mod net;pub mod num; +pub mod numa;pub mod of;#[cfg( CONFIG_PM_OPP )] pub mod opp;
rust/kernel/numa.rs new file +29 -0
@@ -0,0 +1,29 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! NUMA topology utilities. +//! +//! C header: [`include/linux/nodemask.h`](srctree/include/linux/nodemask.h) + +use crate ::bindings; +#[cfg( CONFIG_NUMA )] +use crate ::sync::atomic::{ +Atomic , +Relaxed ,// +}; + +/// Returns the number of online NUMA nodes. +#[inline] +pub fn num_online_nodes () ->u32 { +#[cfg( CONFIG_NUMA )] + { +// SAFETY: `nr_online_nodes` is a global variable that is valid for the lifetime of the +// kernel. The C side updates it with plain writes during node hotplug and reads it with +// plain reads. A relaxed load is the Rust equivalent of `READ_ONCE()`, which is the best +// we can do on the reader side. +unsafe {Atomic ::from_ptr (&raw mut bindings::nr_online_nodes) }.load (Relaxed ) + } +#[cfg(not( CONFIG_NUMA ))] + { + bindings::nr_online_nodes + } +}
Signature · 1 line
2.54.0
Andreas Hindborg
reviewerPATCH v4 2/2 rust: alloc: move `NumaNode` to the `numa` module
In reply to Andreas HindborgNumaNode identifies a NUMA node and is not specific to the allocator API. Move it from the alloc module to the numa module so that NUMA related items live together.
No functional change.
--- rust/kernel/alloc.rs | 30 ++++-------------------------- rust/kernel/alloc/allocator.rs | 6 +++--- rust/kernel/alloc/kbox.rs | 4 ++-- rust/kernel/alloc/kvec.rs | 4 ++-- rust/kernel/numa.rs | 35 ++++++++++++++++++++++++++++++++++- 5 files changed, 45 insertions(+), 34 deletions(-)
rust/kernel/alloc.rs +4 -26
@@ -22,7 +22,10 @@#[derive( Copy , Clone , PartialEq , Eq , Debug )] pub struct AllocError ; -use crate ::prelude::*; +use crate ::{ + numa::NumaNode , + prelude::*,// +};use core::{ alloc::Layout ,
@@ -114,31 +117,6 @@ 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 identifier (non-negative integer). -/// -/// Returns [`EINVAL`] if a negative id or an id exceeding [`bindings::MAX_NUMNODES`] is -/// specified. -pub fn new (node:i32 ) ->Result <Self > { -// 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 { -/// No node preference. -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
rust/kernel/alloc/allocator.rs +3 -3
@@ -11,12 +11,12 @@use super ::{AllocError ,Allocator , -Flags , -NumaNode ,// +Flags ,// };use crate ::{ bindings, + numa::NumaNode , page,// };
@@ -122,7 +122,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, layout.align (), flags.0 , nid.0 ).cast () + self.0 (ptr.cast (), size, layout.align (), flags.0 , nid.as_raw ()).cast () };let ptr =if size ==0 {
rust/kernel/alloc/kbox.rs +2 -2
@@ -13,12 +13,12 @@use super ::{AllocError ,Allocator , -Flags , -NumaNode ,// +Flags ,// };use crate ::{ fmt, + numa::NumaNode , page::AsPageIter , prelude::*, types::ForeignOwnable ,//
rust/kernel/alloc/kvec.rs +2 -2
@@ -14,12 +14,12 @@ AllocError, Allocator, Box, - Flags, - NumaNode, // + Flags, // };use crate ::{ fmt, + numa::NumaNode , page::{AsPageIter ,PAGE_SIZE ,//
rust/kernel/numa.rs +34 -1
@@ -4,12 +4,15 @@//! //! C header: [`include/linux/nodemask.h`](srctree/include/linux/nodemask.h) -use crate ::bindings;#[cfg( CONFIG_NUMA )] use crate ::sync::atomic::{Atomic ,Relaxed ,// }; +use crate ::{ + bindings, + prelude::*,// +};/// Returns the number of online NUMA nodes. #[inline]
@@ -27,3 +30,33 @@ pub fn num_online_nodes() -> u32 { bindings::nr_online_nodes } } + +/// Non Uniform Memory Access (NUMA) node identifier. +#[derive( Clone , Copy , PartialEq )] +pub struct NumaNode (i32 ); + +impl NumaNode { +/// Create a new NUMA node identifier (non-negative integer). +/// +/// Returns [`EINVAL`] if a negative id or an id exceeding [`bindings::MAX_NUMNODES`] is +/// specified. +pub fn new (node:i32 ) ->Result <Self > { +// 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)) + } + +/// Get the raw representation of this node identifier. +pub (crate )fn as_raw (self) ->i32 { + self.0 + } +} + +/// 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 { +/// No node preference. +pub const NO_NODE :NumaNode =NumaNode (bindings::NUMA_NO_NODE ); +}
Signature · 1 line
2.54.0
Re: [PATCH v4 1/2] rust: add a wrapper for the `nr_online_nodes` C function
In reply to Andreas HindborgQuoted 44 lines · Andreas Hindborg
This function returns the number of online NUMA nodes.
The wrapper is needed by the Rust null block driver.
Reviewed-by Gary Guo <[email protected]>Reviewed-by Yury Norov <[email protected]>Assisted-by LLMSigned-off-by Andreas Hindborg <[email protected]>--- rust/kernel/lib.rs | 1 + rust/kernel/numa.rs | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+)rust/kernel/lib.rs +1
-0@@ -105,6 +105,7 @@#[cfg( CONFIG_NET )] pub mod net;pub mod num; +pub mod numa;pub mod of;#[cfg( CONFIG_PM_OPP )] pub mod opp;rust/kernel/numa.rs new file +13
-0@@ -0,0 +1,29 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! NUMA topology utilities. +//! +//! C header: [`include/linux/nodemask.h`](srctree/include/linux/nodemask.h) + +use crate ::bindings; +#[cfg( CONFIG_NUMA )] +use crate ::sync::atomic::{ +Atomic , +Relaxed ,// +}; +
rust/kernel/numa.rs line 23
Quoted 10 lines
rust/kernel/numa.rs new file +10
-0+/// Returns the number of online NUMA nodes. +#[inline] +pub fn num_online_nodes () ->u32 { +#[cfg( CONFIG_NUMA )] + { +// SAFETY: `nr_online_nodes` is a global variable that is valid for the lifetime of the +// kernel. The C side updates it with plain writes during node hotplug and reads it with +// plain reads. A relaxed load is the Rust equivalent of `READ_ONCE()`, which is the best +// we can do on the reader side. +unsafe {Atomic ::from_ptr (&raw mut bindings::nr_online_nodes) }.load (Relaxed )
Just so you know you can use atomic_load()[1]:
unsafe { atomic_load(&raw mut bindings::nr_online_nodes, Relaxed) }With or without this:
[1]: https://rust.docs.kernel.org/kernel/sync/atomic/fn.atomic_load.html
Regards,
Boqun
Quoted 11 lines
rust/kernel/numa.rs new file +6
-0+ } +#[cfg(not( CONFIG_NUMA ))] + { + bindings::nr_online_nodes + } +}Signature · 2 lines
-- 2.54.0
[email protected]
AI reviewRe: [PATCH v4 2/2] rust: alloc: move `NumaNode` to the `numa` module
In reply to Andreas HindborgThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] rust: missing #[inline] on NumaNode::as_raw() --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] rust: missing #[inline] on NumaNode::as_raw()
--
commit 642cdfa1b2261df4d8bb7af89b382fd608fe93f2
Author: Andreas Hindborg <[email protected]>
rust: alloc: move NumaNode to the numa module
This commit moves the NumaNode type from the alloc module to the numa module and adds an as_raw() accessor so callers outside the numa module can obtain the raw node ID.
Quoted 4 lines
rust/kernel/numa.rs +0
-0
[ ... ]
Quoted 16 lines
rust/kernel/numa.rs +12
-0@@ -27,3 +30,33 @@ pub fn num_online_nodes() -> u32 { bindings::nr_online_nodes } } + +/// Non Uniform Memory Access (NUMA) node identifier. +#[derive( Clone , Copy , PartialEq )] +pub struct NumaNode (i32 ); + +impl NumaNode { +/// Create a new NUMA node identifier (non-negative integer). +/// +/// Returns [`EINVAL`] if a negative id or an id exceeding [`bindings::MAX_NUMNODES`] is +/// specified. +pub fn new (node:i32 ) ->Result <Self > { +// MAX_NUMNODES never exceeds 2**10 because NODES_SHIFT is 0..10.
rust/kernel/numa.rs pub fn num_online_nodes() -> u32 { · line 54
Quoted 10 lines
rust/kernel/numa.rs +10
-0+if node <0 || node >= bindings::MAX_NUMNODES as i32 { +return Err (EINVAL ); + } +Ok (Self (node)) + } + +/// Get the raw representation of this node identifier. +pub (crate )fn as_raw (self) ->i32 { + self.0 + }
[Severity: Low]
As reported by Sashiko on v3 [1], this isn't a bug, but should NumaNode::as_raw() in rust/kernel/numa.rs be marked #[inline] since it is a small accessor function in the kernel crate called by ReallocFunc::call() in rust/kernel/alloc/allocator.rs?
[1] https://lore.kernel.org/r/[email protected]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2