Re: [PATCH v3 2/3] rust: add a wrapper for the `nr_online_nodes` C function
From: Yury Norov
Date: Mon Oct 12 2026 - 13:01:48 EST
On Tue, Sep 29, 2026 at 07:56:49PM +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@xxxxxxxxxx>
Reviewed-by: Yury Norov <ynorov@xxxxxxxxxx>
> ---
> 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
>
>