* [PATCH v4 0/2] rust: add a few helpers
@ 2026-10-30 16:08 Andreas Hindborg
2026-10-30 16:08 ` [PATCH v4 1/2] rust: add a wrapper for the `nr_online_nodes` C function Andreas Hindborg
2026-10-30 16:08 ` [PATCH v4 2/2] rust: alloc: move `NumaNode` to the `numa` module Andreas Hindborg
0 siblings, 2 replies; 4+ messages in thread
From: Andreas Hindborg @ 2026-10-30 16:08 UTC (permalink / raw)
To: Miguel Ojeda, Boqun Feng, Gary Guo, Björn Roy Baron,
Benno Lossin, Andreas Hindborg, Alice Ryhl, Trevor Gross,
Danilo Krummrich, Daniel Almeida, Tamir Duberstein,
Alexandre Courbot, Onur Özkan, Thomas Gleixner,
Peter Zijlstra, Lorenzo Stoakes, Vlastimil Babka, Liam R. Howlett,
Uladzislau Rezki, Yury Norov, Yury Norov, Andrew Morton
Cc: Rasmus Villemoes, Suren Baghdasaryan, Michal Hocko,
Brendan Jackman, Johannes Weiner, Zi Yan, linux-kernel,
rust-for-linux, linux-mm
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`.
Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org>
---
Changes in v4:
- Dropped patch 1 of v3 (moved to separate series).
- Rebase on v7.3.
- Link to v3: https://msgid.link/20260929-cpu-helpers-v3-0-e968ce5ee613@kernel.org
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/20260605-cpu-helpers-v2-0-0ad227f0d91b@kernel.org
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/20260215-cpu-helpers-v1-0-fab13d817a5e@kernel.org
---
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,
--
Andreas Hindborg <a.hindborg@kernel.org>
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH v4 1/2] rust: add a wrapper for the `nr_online_nodes` C function
2026-10-30 16:08 [PATCH v4 0/2] rust: add a few helpers Andreas Hindborg
@ 2026-10-30 16:08 ` Andreas Hindborg
2026-10-30 19:03 ` Boqun Feng
2026-10-30 16:08 ` [PATCH v4 2/2] rust: alloc: move `NumaNode` to the `numa` module Andreas Hindborg
1 sibling, 1 reply; 4+ messages in thread
From: Andreas Hindborg @ 2026-10-30 16:08 UTC (permalink / raw)
To: Miguel Ojeda, Boqun Feng, Gary Guo, Björn Roy Baron,
Benno Lossin, Andreas Hindborg, Alice Ryhl, Trevor Gross,
Danilo Krummrich, Daniel Almeida, Tamir Duberstein,
Alexandre Courbot, Onur Özkan, Thomas Gleixner,
Peter Zijlstra, Lorenzo Stoakes, Vlastimil Babka, Liam R. Howlett,
Uladzislau Rezki, Yury Norov, Yury Norov, Andrew Morton
Cc: Rasmus Villemoes, Suren Baghdasaryan, Michal Hocko,
Brendan Jackman, Johannes Weiner, Zi Yan, linux-kernel,
rust-for-linux, linux-mm
This function returns the number of online NUMA nodes.
The wrapper is needed by the Rust null block driver.
Reviewed-by: Gary Guo <gary@garyguo.net>
Reviewed-by: Yury Norov <ynorov@nvidia.com>
Assisted-by: LLM
Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org>
---
rust/kernel/lib.rs | 1 +
rust/kernel/numa.rs | 29 +++++++++++++++++++++++++++++
2 files changed, 30 insertions(+)
diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs
index 4d5c96ddc49c..a7b1c9cf454f 100644
--- a/rust/kernel/lib.rs
+++ b/rust/kernel/lib.rs
@@ -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;
diff --git a/rust/kernel/numa.rs b/rust/kernel/numa.rs
new file mode 100644
index 000000000000..164e622d325f
--- /dev/null
+++ b/rust/kernel/numa.rs
@@ -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
+ }
+}
--
2.54.0
^ permalink raw reply related [flat|nested] 4+ messages in thread
* [PATCH v4 2/2] rust: alloc: move `NumaNode` to the `numa` module
2026-10-30 16:08 [PATCH v4 0/2] rust: add a few helpers Andreas Hindborg
2026-10-30 16:08 ` [PATCH v4 1/2] rust: add a wrapper for the `nr_online_nodes` C function Andreas Hindborg
@ 2026-10-30 16:08 ` Andreas Hindborg
1 sibling, 0 replies; 4+ messages in thread
From: Andreas Hindborg @ 2026-10-30 16:08 UTC (permalink / raw)
To: Miguel Ojeda, Boqun Feng, Gary Guo, Björn Roy Baron,
Benno Lossin, Andreas Hindborg, Alice Ryhl, Trevor Gross,
Danilo Krummrich, Daniel Almeida, Tamir Duberstein,
Alexandre Courbot, Onur Özkan, Thomas Gleixner,
Peter Zijlstra, Lorenzo Stoakes, Vlastimil Babka, Liam R. Howlett,
Uladzislau Rezki, Yury Norov, Yury Norov, Andrew Morton
Cc: Rasmus Villemoes, Suren Baghdasaryan, Michal Hocko,
Brendan Jackman, Johannes Weiner, Zi Yan, linux-kernel,
rust-for-linux, linux-mm
`NumaNode` 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.
Suggested-by: Alice Ryhl <aliceryhl@google.com>
Link: https://lore.kernel.org/all/aiZvKPFCkqJUsP0a@google.com/
Reviewed-by: Yury Norov <ynorov@nvidia.com>
Assisted-by: LLM
Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org>
---
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(-)
diff --git a/rust/kernel/alloc.rs b/rust/kernel/alloc.rs
index 21067bde6860..9daddf540e7d 100644
--- a/rust/kernel/alloc.rs
+++ b/rust/kernel/alloc.rs
@@ -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
diff --git a/rust/kernel/alloc/allocator.rs b/rust/kernel/alloc/allocator.rs
index cd4203f27aed..4af50e20933b 100644
--- a/rust/kernel/alloc/allocator.rs
+++ b/rust/kernel/alloc/allocator.rs
@@ -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 {
diff --git a/rust/kernel/alloc/kbox.rs b/rust/kernel/alloc/kbox.rs
index c63d6acdbb6f..0fa736c81287 100644
--- a/rust/kernel/alloc/kbox.rs
+++ b/rust/kernel/alloc/kbox.rs
@@ -13,12 +13,12 @@
use super::{
AllocError,
Allocator,
- Flags,
- NumaNode, //
+ Flags, //
};
use crate::{
fmt,
+ numa::NumaNode,
page::AsPageIter,
prelude::*,
types::ForeignOwnable, //
diff --git a/rust/kernel/alloc/kvec.rs b/rust/kernel/alloc/kvec.rs
index c7546b9da4fa..b669ca10b3f7 100644
--- a/rust/kernel/alloc/kvec.rs
+++ b/rust/kernel/alloc/kvec.rs
@@ -14,12 +14,12 @@
AllocError,
Allocator,
Box,
- Flags,
- NumaNode, //
+ Flags, //
};
use crate::{
fmt,
+ numa::NumaNode,
page::{
AsPageIter,
PAGE_SIZE, //
diff --git a/rust/kernel/numa.rs b/rust/kernel/numa.rs
index 164e622d325f..4a3a880308f3 100644
--- a/rust/kernel/numa.rs
+++ b/rust/kernel/numa.rs
@@ -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);
+}
--
2.54.0
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH v4 1/2] rust: add a wrapper for the `nr_online_nodes` C function
2026-10-30 16:08 ` [PATCH v4 1/2] rust: add a wrapper for the `nr_online_nodes` C function Andreas Hindborg
@ 2026-10-30 19:03 ` Boqun Feng
0 siblings, 0 replies; 4+ messages in thread
From: Boqun Feng @ 2026-10-30 19:03 UTC (permalink / raw)
To: Andreas Hindborg
Cc: Miguel Ojeda, Gary Guo, Björn Roy Baron, Benno Lossin,
Alice Ryhl, Trevor Gross, Danilo Krummrich, Daniel Almeida,
Tamir Duberstein, Alexandre Courbot, Onur Özkan,
Thomas Gleixner, Peter Zijlstra, Lorenzo Stoakes, Vlastimil Babka,
Liam R. Howlett, Uladzislau Rezki, Yury Norov, Yury Norov,
Andrew Morton, Rasmus Villemoes, Suren Baghdasaryan, Michal Hocko,
Brendan Jackman, Johannes Weiner, Zi Yan, linux-kernel,
rust-for-linux, linux-mm
On Fri, Oct 30, 2026 at 05:08:03PM +0100, Andreas Hindborg wrote:
> This function returns the number of online NUMA nodes.
>
> The wrapper is needed by the Rust null block driver.
>
> Reviewed-by: Gary Guo <gary@garyguo.net>
> Reviewed-by: Yury Norov <ynorov@nvidia.com>
> Assisted-by: LLM
> Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org>
> ---
> rust/kernel/lib.rs | 1 +
> rust/kernel/numa.rs | 29 +++++++++++++++++++++++++++++
> 2 files changed, 30 insertions(+)
>
> diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs
> index 4d5c96ddc49c..a7b1c9cf454f 100644
> --- a/rust/kernel/lib.rs
> +++ b/rust/kernel/lib.rs
> @@ -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;
> diff --git a/rust/kernel/numa.rs b/rust/kernel/numa.rs
> new file mode 100644
> index 000000000000..164e622d325f
> --- /dev/null
> +++ b/rust/kernel/numa.rs
> @@ -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)
Just so you know you can use `atomic_load()`[1]:
unsafe { atomic_load(&raw mut bindings::nr_online_nodes, Relaxed) }
With or without this:
Reviewed-by: Boqun Feng <boqun@kernel.org>
[1]: https://rust.docs.kernel.org/kernel/sync/atomic/fn.atomic_load.html
Regards,
Boqun
> + }
> + #[cfg(not(CONFIG_NUMA))]
> + {
> + bindings::nr_online_nodes
> + }
> +}
>
> --
> 2.54.0
>
>
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-10-30 19:03 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-10-30 16:08 [PATCH v4 0/2] rust: add a few helpers Andreas Hindborg
2026-10-30 16:08 ` [PATCH v4 1/2] rust: add a wrapper for the `nr_online_nodes` C function Andreas Hindborg
2026-10-30 19:03 ` Boqun Feng
2026-10-30 16:08 ` [PATCH v4 2/2] rust: alloc: move `NumaNode` to the `numa` module Andreas Hindborg
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox