* [PATCH v3 0/3] rust: add a few helpers
@ 2026-09-29 17:56 Andreas Hindborg
2026-09-29 17:56 ` [PATCH v3 1/3] rust: add a wrapper for the `num_possible_cpus` C function Andreas Hindborg
` (2 more replies)
0 siblings, 3 replies; 11+ messages in thread
From: Andreas Hindborg @ 2026-09-29 17:56 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
Cc: linux-kernel, rust-for-linux
This series adds two helpers for obtaining information about the number
of numa domains and 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 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 (3):
rust: add a wrapper for the `num_possible_cpus` C function
rust: add a wrapper for the `nr_online_nodes` C function
rust: alloc: move `NumaNode` to the `numa` module
rust/helpers/helpers.c | 1 +
rust/helpers/num_cpus.c | 8 ++++++
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/cpu.rs | 7 +++++
rust/kernel/lib.rs | 1 +
rust/kernel/numa.rs | 62 ++++++++++++++++++++++++++++++++++++++++++
9 files changed, 90 insertions(+), 33 deletions(-)
---
base-commit: 72d3fcf802c45d00b300f25b848a93c3a2bd7c7e
change-id: 20260215-cpu-helpers-08efb2572487
Best regards,
--
Andreas Hindborg <a.hindborg@kernel.org>
^ permalink raw reply [flat|nested] 11+ messages in thread* [PATCH v3 1/3] rust: add a wrapper for the `num_possible_cpus` C function 2026-09-29 17:56 [PATCH v3 0/3] rust: add a few helpers Andreas Hindborg @ 2026-09-29 17:56 ` Andreas Hindborg 2026-09-29 18:30 ` Miguel Ojeda 2026-09-29 17:56 ` [PATCH v3 2/3] rust: add a wrapper for the `nr_online_nodes` " Andreas Hindborg 2026-09-29 17:56 ` [PATCH v3 3/3] rust: alloc: move `NumaNode` to the `numa` module Andreas Hindborg 2 siblings, 1 reply; 11+ messages in thread From: Andreas Hindborg @ 2026-09-29 17:56 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 Cc: linux-kernel, rust-for-linux This function returns the maximum number of CPUs that can be online. The wrapper is needed by the Rust null block driver. Reviewed-by: Alice Ryhl <aliceryhl@google.com> Reviewed-by: Gary Guo <gary@garyguo.net> 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 440fb7638e3c..89e38b08fd17 100644 --- a/rust/helpers/helpers.c +++ b/rust/helpers/helpers.c @@ -76,6 +76,7 @@ #include "mm.c" #include "mutex.c" #include "net/genetlink.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.54.0 ^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH v3 1/3] rust: add a wrapper for the `num_possible_cpus` C function 2026-09-29 17:56 ` [PATCH v3 1/3] rust: add a wrapper for the `num_possible_cpus` C function Andreas Hindborg @ 2026-09-29 18:30 ` Miguel Ojeda 2026-09-30 7:46 ` Andreas Hindborg 0 siblings, 1 reply; 11+ messages in thread From: Miguel Ojeda @ 2026-09-29 18:30 UTC (permalink / raw) To: Andreas Hindborg Cc: Miguel Ojeda, Boqun Feng, 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, linux-kernel, rust-for-linux On Tue, Sep 29, 2026 at 7:58 PM Andreas Hindborg <a.hindborg@kernel.org> wrote: > > This function returns the maximum number of CPUs that can be online. > > The wrapper is needed by the Rust null block driver. > > Reviewed-by: Alice Ryhl <aliceryhl@google.com> > Reviewed-by: Gary Guo <gary@garyguo.net> > Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org> I can pick this one -- it could have been an independent patch anyway, no? Thanks! Cheers, Miguel ^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH v3 1/3] rust: add a wrapper for the `num_possible_cpus` C function 2026-09-29 18:30 ` Miguel Ojeda @ 2026-09-30 7:46 ` Andreas Hindborg 2026-09-30 10:25 ` Miguel Ojeda 0 siblings, 1 reply; 11+ messages in thread From: Andreas Hindborg @ 2026-09-30 7:46 UTC (permalink / raw) To: Miguel Ojeda Cc: Miguel Ojeda, Boqun Feng, 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, linux-kernel, rust-for-linux Miguel Ojeda <miguel.ojeda.sandonis@gmail.com> writes: > On Tue, Sep 29, 2026 at 7:58 PM Andreas Hindborg <a.hindborg@kernel.org> wrote: >> >> This function returns the maximum number of CPUs that can be online. >> >> The wrapper is needed by the Rust null block driver. >> >> Reviewed-by: Alice Ryhl <aliceryhl@google.com> >> Reviewed-by: Gary Guo <gary@garyguo.net> >> Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org> > > I can pick this one -- it could have been an independent patch anyway, no? Yes, this one is independent. At v1 I had 2 patches adding helpers, that was the only thing relating them. I thought it was better to lump them, but maybe not? Best regards, Andreas Hindborg ^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH v3 1/3] rust: add a wrapper for the `num_possible_cpus` C function 2026-09-30 7:46 ` Andreas Hindborg @ 2026-09-30 10:25 ` Miguel Ojeda 2026-09-30 11:34 ` Andreas Hindborg 0 siblings, 1 reply; 11+ messages in thread From: Miguel Ojeda @ 2026-09-30 10:25 UTC (permalink / raw) To: Andreas Hindborg, Peter Zijlstra, Thomas Gleixner Cc: Miguel Ojeda, Boqun Feng, Gary Guo, Björn Roy Baron, Benno Lossin, Alice Ryhl, Trevor Gross, Danilo Krummrich, Daniel Almeida, Tamir Duberstein, Alexandre Courbot, Onur Özkan, Lorenzo Stoakes, Vlastimil Babka, Liam R. Howlett, Uladzislau Rezki, linux-kernel, rust-for-linux On Wed, Sep 30, 2026 at 9:46 AM Andreas Hindborg <a.hindborg@kernel.org> wrote: > > Yes, this one is independent. At v1 I had 2 patches adding helpers, that > was the only thing relating them. I thought it was better to lump them, > but maybe not? Thanks for clarifying! By the way, do you think the new header is worth it instead of using the existing `cpu.c` one? If we do that, then we should probably add it under their `MAINTAINERS` file. tip may want to take a look or give an Acked-by... Cheers, Miguel ^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH v3 1/3] rust: add a wrapper for the `num_possible_cpus` C function 2026-09-30 10:25 ` Miguel Ojeda @ 2026-09-30 11:34 ` Andreas Hindborg 2026-09-30 11:38 ` Miguel Ojeda 0 siblings, 1 reply; 11+ messages in thread From: Andreas Hindborg @ 2026-09-30 11:34 UTC (permalink / raw) To: Miguel Ojeda, Peter Zijlstra, Thomas Gleixner Cc: Miguel Ojeda, Boqun Feng, Gary Guo, Björn Roy Baron, Benno Lossin, Alice Ryhl, Trevor Gross, Danilo Krummrich, Daniel Almeida, Tamir Duberstein, Alexandre Courbot, Onur Özkan, Lorenzo Stoakes, Vlastimil Babka, Liam R. Howlett, Uladzislau Rezki, linux-kernel, rust-for-linux Miguel Ojeda <miguel.ojeda.sandonis@gmail.com> writes: > On Wed, Sep 30, 2026 at 9:46 AM Andreas Hindborg <a.hindborg@kernel.org> wrote: >> >> Yes, this one is independent. At v1 I had 2 patches adding helpers, that >> was the only thing relating them. I thought it was better to lump them, >> but maybe not? > > Thanks for clarifying! > > By the way, do you think the new header is worth it instead of using > the existing `cpu.c` one? If we do that, then we should probably add > it under their `MAINTAINERS` file. The helpers should go in cpu.c, that makes sense. Not sure why I put in a separate file, maybe cpu.c was not present when I sent v1. Do you mind moving the code when applying or do you want a new version? Best regards, Andreas Hindborg ^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH v3 1/3] rust: add a wrapper for the `num_possible_cpus` C function 2026-09-30 11:34 ` Andreas Hindborg @ 2026-09-30 11:38 ` Miguel Ojeda 0 siblings, 0 replies; 11+ messages in thread From: Miguel Ojeda @ 2026-09-30 11:38 UTC (permalink / raw) To: Andreas Hindborg Cc: Peter Zijlstra, Thomas Gleixner, Miguel Ojeda, Boqun Feng, Gary Guo, Björn Roy Baron, Benno Lossin, Alice Ryhl, Trevor Gross, Danilo Krummrich, Daniel Almeida, Tamir Duberstein, Alexandre Courbot, Onur Özkan, Lorenzo Stoakes, Vlastimil Babka, Liam R. Howlett, Uladzislau Rezki, linux-kernel, rust-for-linux On Wed, Sep 30, 2026 at 1:34 PM Andreas Hindborg <a.hindborg@kernel.org> wrote: > > The helpers should go in cpu.c, that makes sense. Not sure why I put in > a separate file, maybe cpu.c was not present when I sent v1. > > Do you mind moving the code when applying or do you want a new version? I can do it. @tip / Peter / Thomas: please shout if you wanted to take this patch instead. Cheers, Miguel ^ permalink raw reply [flat|nested] 11+ messages in thread
* [PATCH v3 2/3] rust: add a wrapper for the `nr_online_nodes` C function 2026-09-29 17:56 [PATCH v3 0/3] rust: add a few helpers Andreas Hindborg 2026-09-29 17:56 ` [PATCH v3 1/3] rust: add a wrapper for the `num_possible_cpus` C function Andreas Hindborg @ 2026-09-29 17:56 ` Andreas Hindborg 2026-09-30 15:14 ` Gary Guo 2026-09-29 17:56 ` [PATCH v3 3/3] rust: alloc: move `NumaNode` to the `numa` module Andreas Hindborg 2 siblings, 1 reply; 11+ messages in thread From: Andreas Hindborg @ 2026-09-29 17:56 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 Cc: linux-kernel, rust-for-linux 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 | 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 [flat|nested] 11+ messages in thread
* Re: [PATCH v3 2/3] rust: add a wrapper for the `nr_online_nodes` C function 2026-09-29 17:56 ` [PATCH v3 2/3] rust: add a wrapper for the `nr_online_nodes` " Andreas Hindborg @ 2026-09-30 15:14 ` Gary Guo 0 siblings, 0 replies; 11+ messages in thread From: Gary Guo @ 2026-09-30 15:14 UTC (permalink / raw) To: Andreas Hindborg, Miguel Ojeda, Boqun Feng, 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 Cc: linux-kernel, rust-for-linux On Tue Sep 29, 2026 at 6:56 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> It's fine to ignore Sashiko. Although, ideally we change C code to do WRITE_ONCE(). Reviewed-by: Gary Guo <gary@garyguo.net> Best, Gary > --- > rust/kernel/lib.rs | 1 + > rust/kernel/numa.rs | 29 +++++++++++++++++++++++++++++ > 2 files changed, 30 insertions(+) ^ permalink raw reply [flat|nested] 11+ messages in thread
* [PATCH v3 3/3] rust: alloc: move `NumaNode` to the `numa` module 2026-09-29 17:56 [PATCH v3 0/3] rust: add a few helpers Andreas Hindborg 2026-09-29 17:56 ` [PATCH v3 1/3] rust: add a wrapper for the `num_possible_cpus` C function Andreas Hindborg 2026-09-29 17:56 ` [PATCH v3 2/3] rust: add a wrapper for the `nr_online_nodes` " Andreas Hindborg @ 2026-09-29 17:56 ` Andreas Hindborg 2026-09-30 15:12 ` Gary Guo 2 siblings, 1 reply; 11+ messages in thread From: Andreas Hindborg @ 2026-09-29 17:56 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 Cc: linux-kernel, rust-for-linux `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/ 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 [flat|nested] 11+ messages in thread
* Re: [PATCH v3 3/3] rust: alloc: move `NumaNode` to the `numa` module 2026-09-29 17:56 ` [PATCH v3 3/3] rust: alloc: move `NumaNode` to the `numa` module Andreas Hindborg @ 2026-09-30 15:12 ` Gary Guo 0 siblings, 0 replies; 11+ messages in thread From: Gary Guo @ 2026-09-30 15:12 UTC (permalink / raw) To: Andreas Hindborg, Miguel Ojeda, Boqun Feng, 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 Cc: linux-kernel, rust-for-linux On Tue Sep 29, 2026 at 6:56 PM BST, Andreas Hindborg wrote: > `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/ > 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); FWIW the C type is nodemask_t and this should have mask in the name too. It is not an identifier. That said, this is an existing issue, so shouldn't block the move. Best, Gary > + > +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); > +} ^ permalink raw reply [flat|nested] 11+ messages in thread
end of thread, other threads:[~2026-09-30 15:14 UTC | newest] Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2026-09-29 17:56 [PATCH v3 0/3] rust: add a few helpers Andreas Hindborg 2026-09-29 17:56 ` [PATCH v3 1/3] rust: add a wrapper for the `num_possible_cpus` C function Andreas Hindborg 2026-09-29 18:30 ` Miguel Ojeda 2026-09-30 7:46 ` Andreas Hindborg 2026-09-30 10:25 ` Miguel Ojeda 2026-09-30 11:34 ` Andreas Hindborg 2026-09-30 11:38 ` Miguel Ojeda 2026-09-29 17:56 ` [PATCH v3 2/3] rust: add a wrapper for the `nr_online_nodes` " Andreas Hindborg 2026-09-30 15:14 ` Gary Guo 2026-09-29 17:56 ` [PATCH v3 3/3] rust: alloc: move `NumaNode` to the `numa` module Andreas Hindborg 2026-09-30 15:12 ` Gary Guo
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®