* [PATCH v2 0/2] rust: add a few helpers
@ 2026-06-05 12:54 Andreas Hindborg
2026-06-05 12:54 ` [PATCH v2 1/2] rust: add a wrapper for the `num_possible_cpus` C function Andreas Hindborg
2026-06-05 12:54 ` [PATCH v2 2/2] rust: add a wrapper for the `nr_online_nodes` " Andreas Hindborg
0 siblings, 2 replies; 9+ messages in thread
From: Andreas Hindborg @ 2026-06-05 12:54 UTC (permalink / raw)
To: Miguel Ojeda, Gary Guo, Björn Roy Baron, Benno Lossin,
Alice Ryhl, Trevor Gross, Danilo Krummrich, Boqun Feng,
Thomas Gleixner, Peter Zijlstra, Boqun Feng
Cc: Dirk Behme, linux-kernel, rust-for-linux, Andreas Hindborg
This series adds two helpers for obtaining information about the number
of numa domains and cpu count of the system.
Signed-off-by: Andreas Hindborg <a.hindborg@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
To: Miguel Ojeda <ojeda@kernel.org>
To: Boqun Feng <boqun@kernel.org>
To: Gary Guo <gary@garyguo.net>
To: Björn Roy Baron <bjorn3_gh@protonmail.com>
To: Benno Lossin <lossin@kernel.org>
To: Andreas Hindborg <a.hindborg@kernel.org>
To: Alice Ryhl <aliceryhl@google.com>
To: Trevor Gross <tmgross@umich.edu>
To: Danilo Krummrich <dakr@kernel.org>
To: Thomas Gleixner <tglx@kernel.org>
To: Peter Zijlstra <peterz@infradead.org>
Cc: linux-kernel@vger.kernel.org
Cc: rust-for-linux@vger.kernel.org
---
Andreas Hindborg (2):
rust: add a wrapper for the `num_possible_cpus` C function
rust: add a wrapper for the `nr_online_nodes` C function
rust/helpers/helpers.c | 1 +
rust/helpers/num_cpus.c | 8 ++++++++
rust/kernel/cpu.rs | 7 +++++++
rust/kernel/lib.rs | 1 +
rust/kernel/numa.rs | 19 +++++++++++++++++++
5 files changed, 36 insertions(+)
---
base-commit: 7fd2df204f342fc17d1a0bfcd474b24232fb0f32
change-id: 20260215-cpu-helpers-08efb2572487
Best regards,
--
Andreas Hindborg <a.hindborg@kernel.org>
^ permalink raw reply [flat|nested] 9+ messages in thread* [PATCH v2 1/2] rust: add a wrapper for the `num_possible_cpus` C function 2026-06-05 12:54 [PATCH v2 0/2] rust: add a few helpers Andreas Hindborg @ 2026-06-05 12:54 ` Andreas Hindborg 2026-06-08 7:28 ` Alice Ryhl 2026-06-08 11:06 ` Gary Guo 2026-06-05 12:54 ` [PATCH v2 2/2] rust: add a wrapper for the `nr_online_nodes` " Andreas Hindborg 1 sibling, 2 replies; 9+ messages in thread From: Andreas Hindborg @ 2026-06-05 12:54 UTC (permalink / raw) To: Miguel Ojeda, Gary Guo, Björn Roy Baron, Benno Lossin, Alice Ryhl, Trevor Gross, Danilo Krummrich, Boqun Feng, Thomas Gleixner, Peter Zijlstra, Boqun Feng Cc: Dirk Behme, linux-kernel, rust-for-linux, Andreas Hindborg This function returns the maximum number of CPUs that can be online. The wrapper is needed by the Rust null block driver. Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org> --- rust/helpers/helpers.c | 1 + rust/helpers/num_cpus.c | 8 ++++++++ rust/kernel/cpu.rs | 7 +++++++ 3 files changed, 16 insertions(+) diff --git a/rust/helpers/helpers.c b/rust/helpers/helpers.c index 625921e27dfb..bb250648e0a9 100644 --- a/rust/helpers/helpers.c +++ b/rust/helpers/helpers.c @@ -70,6 +70,7 @@ #include "maple_tree.c" #include "mm.c" #include "mutex.c" +#include "num_cpus.c" #include "of.c" #include "page.c" #include "pci.c" diff --git a/rust/helpers/num_cpus.c b/rust/helpers/num_cpus.c new file mode 100644 index 000000000000..54e8bacb46c3 --- /dev/null +++ b/rust/helpers/num_cpus.c @@ -0,0 +1,8 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include <linux/cpumask.h> + +__rust_helper unsigned int rust_helper_num_possible_cpus(void) +{ + return num_possible_cpus(); +} diff --git a/rust/kernel/cpu.rs b/rust/kernel/cpu.rs index cb6c0338ef5a..b38b6914b12e 100644 --- a/rust/kernel/cpu.rs +++ b/rust/kernel/cpu.rs @@ -6,6 +6,13 @@ use crate::{bindings, device::Device, error::Result, prelude::ENODEV}; +/// Returns the maximum number of CPUs that may be online on the system. +#[inline] +pub fn num_possible_cpus() -> u32 { + // SAFETY: FFI call with no additional requirements. + unsafe { bindings::num_possible_cpus() } +} + /// Returns the maximum number of possible CPUs in the current system configuration. #[inline] pub fn nr_cpu_ids() -> u32 { -- 2.51.2 ^ permalink raw reply related [flat|nested] 9+ messages in thread
* Re: [PATCH v2 1/2] rust: add a wrapper for the `num_possible_cpus` C function 2026-06-05 12:54 ` [PATCH v2 1/2] rust: add a wrapper for the `num_possible_cpus` C function Andreas Hindborg @ 2026-06-08 7:28 ` Alice Ryhl 2026-06-08 11:06 ` Gary Guo 1 sibling, 0 replies; 9+ messages in thread From: Alice Ryhl @ 2026-06-08 7:28 UTC (permalink / raw) To: Andreas Hindborg Cc: Miguel Ojeda, Gary Guo, Björn Roy Baron, Benno Lossin, Trevor Gross, Danilo Krummrich, Boqun Feng, Thomas Gleixner, Peter Zijlstra, Dirk Behme, linux-kernel, rust-for-linux On Fri, Jun 05, 2026 at 02:54:56PM +0200, Andreas Hindborg wrote: > This function returns the maximum number of CPUs that can be online. > > The wrapper is needed by the Rust null block driver. > > Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org> Reviewed-by: Alice Ryhl <aliceryhl@google.com> ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH v2 1/2] rust: add a wrapper for the `num_possible_cpus` C function 2026-06-05 12:54 ` [PATCH v2 1/2] rust: add a wrapper for the `num_possible_cpus` C function Andreas Hindborg 2026-06-08 7:28 ` Alice Ryhl @ 2026-06-08 11:06 ` Gary Guo 1 sibling, 0 replies; 9+ messages in thread From: Gary Guo @ 2026-06-08 11:06 UTC (permalink / raw) To: Andreas Hindborg, Miguel Ojeda, Gary Guo, Björn Roy Baron, Benno Lossin, Alice Ryhl, Trevor Gross, Danilo Krummrich, Boqun Feng, Thomas Gleixner, Peter Zijlstra Cc: Dirk Behme, linux-kernel, rust-for-linux On Fri Jun 5, 2026 at 1:54 PM BST, Andreas Hindborg wrote: > This function returns the maximum number of CPUs that can be online. > > The wrapper is needed by the Rust null block driver. > > Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org> Reviewed-by: Gary Guo <gary@garyguo.net> > --- > rust/helpers/helpers.c | 1 + > rust/helpers/num_cpus.c | 8 ++++++++ > rust/kernel/cpu.rs | 7 +++++++ > 3 files changed, 16 insertions(+) ^ permalink raw reply [flat|nested] 9+ messages in thread
* [PATCH v2 2/2] rust: add a wrapper for the `nr_online_nodes` C function 2026-06-05 12:54 [PATCH v2 0/2] rust: add a few helpers Andreas Hindborg 2026-06-05 12:54 ` [PATCH v2 1/2] rust: add a wrapper for the `num_possible_cpus` C function Andreas Hindborg @ 2026-06-05 12:54 ` Andreas Hindborg 2026-06-08 7:28 ` Alice Ryhl 2026-06-08 11:05 ` Gary Guo 1 sibling, 2 replies; 9+ messages in thread From: Andreas Hindborg @ 2026-06-05 12:54 UTC (permalink / raw) To: Miguel Ojeda, Gary Guo, Björn Roy Baron, Benno Lossin, Alice Ryhl, Trevor Gross, Danilo Krummrich, Boqun Feng, Thomas Gleixner, Peter Zijlstra, Boqun Feng Cc: Dirk Behme, linux-kernel, rust-for-linux, Andreas Hindborg This function returns the number of online NUMA nodes. The wrapper is needed by the Rust null block driver. Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org> --- rust/kernel/lib.rs | 1 + rust/kernel/numa.rs | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs index b72b2fbe046d..00713b2adcac 100644 --- a/rust/kernel/lib.rs +++ b/rust/kernel/lib.rs @@ -97,6 +97,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..760db072a6c1 --- /dev/null +++ b/rust/kernel/numa.rs @@ -0,0 +1,19 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! NUMA topology utilities. +//! +//! C header: [`include/linux/nodemask.h`](srctree/include/linux/nodemask.h) + +use crate::bindings; + +/// Returns the number of online NUMA nodes. +#[inline] +pub fn num_online_nodes() -> u32 { + // NOTE: In some configurations, we can read this variable without an unsafe block. + // SAFETY: When NUMA is enabled, this is a global mutable static. We do as C and just read it, + // even though it might race. + #[allow(unused_unsafe)] + unsafe { + bindings::nr_online_nodes + } +} -- 2.51.2 ^ permalink raw reply related [flat|nested] 9+ messages in thread
* Re: [PATCH v2 2/2] rust: add a wrapper for the `nr_online_nodes` C function 2026-06-05 12:54 ` [PATCH v2 2/2] rust: add a wrapper for the `nr_online_nodes` " Andreas Hindborg @ 2026-06-08 7:28 ` Alice Ryhl 2026-06-08 11:05 ` Gary Guo 1 sibling, 0 replies; 9+ messages in thread From: Alice Ryhl @ 2026-06-08 7:28 UTC (permalink / raw) To: Andreas Hindborg Cc: Miguel Ojeda, Gary Guo, Björn Roy Baron, Benno Lossin, Trevor Gross, Danilo Krummrich, Boqun Feng, Thomas Gleixner, Peter Zijlstra, Dirk Behme, linux-kernel, rust-for-linux On Fri, Jun 05, 2026 at 02:54:57PM +0200, Andreas Hindborg wrote: > This function returns the number of online NUMA nodes. > > The wrapper is needed by the Rust null block driver. > > Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org> I guess the NumaNode struct should be moved to this new file. Reviewed-by: Alice Ryhl <aliceryhl@google.com> ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH v2 2/2] rust: add a wrapper for the `nr_online_nodes` C function 2026-06-05 12:54 ` [PATCH v2 2/2] rust: add a wrapper for the `nr_online_nodes` " Andreas Hindborg 2026-06-08 7:28 ` Alice Ryhl @ 2026-06-08 11:05 ` Gary Guo 2026-06-08 11:52 ` Andreas Hindborg 1 sibling, 1 reply; 9+ messages in thread From: Gary Guo @ 2026-06-08 11:05 UTC (permalink / raw) To: Andreas Hindborg, Miguel Ojeda, Gary Guo, Björn Roy Baron, Benno Lossin, Alice Ryhl, Trevor Gross, Danilo Krummrich, Boqun Feng, Thomas Gleixner, Peter Zijlstra Cc: Dirk Behme, linux-kernel, rust-for-linux On Fri Jun 5, 2026 at 1:54 PM BST, Andreas Hindborg wrote: > This function returns the number of online NUMA nodes. > > The wrapper is needed by the Rust null block driver. > > Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org> > --- > rust/kernel/lib.rs | 1 + > rust/kernel/numa.rs | 19 +++++++++++++++++++ > 2 files changed, 20 insertions(+) > > diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs > index b72b2fbe046d..00713b2adcac 100644 > --- a/rust/kernel/lib.rs > +++ b/rust/kernel/lib.rs > @@ -97,6 +97,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..760db072a6c1 > --- /dev/null > +++ b/rust/kernel/numa.rs > @@ -0,0 +1,19 @@ > +// SPDX-License-Identifier: GPL-2.0 > + > +//! NUMA topology utilities. > +//! > +//! C header: [`include/linux/nodemask.h`](srctree/include/linux/nodemask.h) > + > +use crate::bindings; > + > +/// Returns the number of online NUMA nodes. > +#[inline] > +pub fn num_online_nodes() -> u32 { > + // NOTE: In some configurations, we can read this variable without an unsafe block. > + // SAFETY: When NUMA is enabled, this is a global mutable static. We do as C and just read it, > + // even though it might race. Can we do better and do a relaxed load instead? Best, Gary > + #[allow(unused_unsafe)] > + unsafe { > + bindings::nr_online_nodes > + } > +} ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH v2 2/2] rust: add a wrapper for the `nr_online_nodes` C function 2026-06-08 11:05 ` Gary Guo @ 2026-06-08 11:52 ` Andreas Hindborg 2026-06-08 11:57 ` Gary Guo 0 siblings, 1 reply; 9+ messages in thread From: Andreas Hindborg @ 2026-06-08 11:52 UTC (permalink / raw) To: Gary Guo, Miguel Ojeda, Gary Guo, Björn Roy Baron, Benno Lossin, Alice Ryhl, Trevor Gross, Danilo Krummrich, Boqun Feng, Thomas Gleixner, Peter Zijlstra Cc: Dirk Behme, linux-kernel, rust-for-linux "Gary Guo" <gary@garyguo.net> writes: > On Fri Jun 5, 2026 at 1:54 PM BST, Andreas Hindborg wrote: >> This function returns the number of online NUMA nodes. >> >> The wrapper is needed by the Rust null block driver. >> >> Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org> >> --- >> rust/kernel/lib.rs | 1 + >> rust/kernel/numa.rs | 19 +++++++++++++++++++ >> 2 files changed, 20 insertions(+) >> >> diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs >> index b72b2fbe046d..00713b2adcac 100644 >> --- a/rust/kernel/lib.rs >> +++ b/rust/kernel/lib.rs >> @@ -97,6 +97,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..760db072a6c1 >> --- /dev/null >> +++ b/rust/kernel/numa.rs >> @@ -0,0 +1,19 @@ >> +// SPDX-License-Identifier: GPL-2.0 >> + >> +//! NUMA topology utilities. >> +//! >> +//! C header: [`include/linux/nodemask.h`](srctree/include/linux/nodemask.h) >> + >> +use crate::bindings; >> + >> +/// Returns the number of online NUMA nodes. >> +#[inline] >> +pub fn num_online_nodes() -> u32 { >> + // NOTE: In some configurations, we can read this variable without an unsafe block. >> + // SAFETY: When NUMA is enabled, this is a global mutable static. We do as C and just read it, >> + // even though it might race. > > Can we do better and do a relaxed load instead? Yes, that sounds reasonable. But what are the semantics of a relaxed load when other writers are not using atomic primitives? Does it make a difference? Best regards, Andreas Hindborg ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH v2 2/2] rust: add a wrapper for the `nr_online_nodes` C function 2026-06-08 11:52 ` Andreas Hindborg @ 2026-06-08 11:57 ` Gary Guo 0 siblings, 0 replies; 9+ messages in thread From: Gary Guo @ 2026-06-08 11:57 UTC (permalink / raw) To: Andreas Hindborg, Gary Guo, Miguel Ojeda, Björn Roy Baron, Benno Lossin, Alice Ryhl, Trevor Gross, Danilo Krummrich, Boqun Feng, Thomas Gleixner, Peter Zijlstra Cc: Dirk Behme, linux-kernel, rust-for-linux On Mon Jun 8, 2026 at 12:52 PM BST, Andreas Hindborg wrote: > "Gary Guo" <gary@garyguo.net> writes: > >> On Fri Jun 5, 2026 at 1:54 PM BST, Andreas Hindborg wrote: >>> This function returns the number of online NUMA nodes. >>> >>> The wrapper is needed by the Rust null block driver. >>> >>> Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org> >>> --- >>> rust/kernel/lib.rs | 1 + >>> rust/kernel/numa.rs | 19 +++++++++++++++++++ >>> 2 files changed, 20 insertions(+) >>> >>> diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs >>> index b72b2fbe046d..00713b2adcac 100644 >>> --- a/rust/kernel/lib.rs >>> +++ b/rust/kernel/lib.rs >>> @@ -97,6 +97,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..760db072a6c1 >>> --- /dev/null >>> +++ b/rust/kernel/numa.rs >>> @@ -0,0 +1,19 @@ >>> +// SPDX-License-Identifier: GPL-2.0 >>> + >>> +//! NUMA topology utilities. >>> +//! >>> +//! C header: [`include/linux/nodemask.h`](srctree/include/linux/nodemask.h) >>> + >>> +use crate::bindings; >>> + >>> +/// Returns the number of online NUMA nodes. >>> +#[inline] >>> +pub fn num_online_nodes() -> u32 { >>> + // NOTE: In some configurations, we can read this variable without an unsafe block. >>> + // SAFETY: When NUMA is enabled, this is a global mutable static. We do as C and just read it, >>> + // even though it might race. >> >> Can we do better and do a relaxed load instead? > > Yes, that sounds reasonable. > > But what are the semantics of a relaxed load when other writers are not > using atomic primitives? Does it make a difference? This is the whole CONFIG_KCSAN_ASSUME_PLAIN_WRITES_ATOMIC again. Technically not okay, but this happens common in C code that people can choose to ignore it :) But let's not advocate for that in Rust code. Best, Gary ^ permalink raw reply [flat|nested] 9+ messages in thread
end of thread, other threads:[~2026-06-08 11:58 UTC | newest] Thread overview: 9+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2026-06-05 12:54 [PATCH v2 0/2] rust: add a few helpers Andreas Hindborg 2026-06-05 12:54 ` [PATCH v2 1/2] rust: add a wrapper for the `num_possible_cpus` C function Andreas Hindborg 2026-06-08 7:28 ` Alice Ryhl 2026-06-08 11:06 ` Gary Guo 2026-06-05 12:54 ` [PATCH v2 2/2] rust: add a wrapper for the `nr_online_nodes` " Andreas Hindborg 2026-06-08 7:28 ` Alice Ryhl 2026-06-08 11:05 ` Gary Guo 2026-06-08 11:52 ` Andreas Hindborg 2026-06-08 11:57 ` Gary Guo
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox