rust: add a few helpers
Subsystems (2)
Andreas Hindborg
reviewerv2 cover rust: add a few helpers
This series adds two helpers for obtaining information about the number of numa domains and cpu count of the system.
---
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]
To: Miguel Ojeda <[email protected]>
To: Boqun Feng <[email protected]>
To: Gary Guo <[email protected]>
To: Björn Roy Baron <[email protected]>
To: Benno Lossin <[email protected]>
To: Andreas Hindborg <[email protected]>
To: Alice Ryhl <[email protected]>
To: Trevor Gross <[email protected]>
To: Danilo Krummrich <[email protected]>
To: Thomas Gleixner <[email protected]>
To: Peter Zijlstra <[email protected]>
Cc: [email protected]
Cc: [email protected]
---
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,
Signature · 1 line
Andreas Hindborg <[email protected]>
Andreas Hindborg
reviewerPATCH v2 1/2 rust: add a wrapper for the `num_possible_cpus` C function
This function returns the maximum number of CPUs that can be online.
The wrapper is needed by the Rust null block driver.
--- rust/helpers/helpers.c | 1 + rust/helpers/num_cpus.c | 8 ++++++++ rust/kernel/cpu.rs | 7 +++++++ 3 files changed, 16 insertions(+)
rust/helpers/helpers.c +1 -0
@@ -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"
rust/helpers/num_cpus.c new file +8 -0
@@ -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 (); +}
rust/kernel/cpu.rs +7 -0
@@ -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 {
Signature · 1 line
2.51.2
Re: [PATCH v2 1/2] rust: add a wrapper for the `num_possible_cpus` C function
Quoted 5 lines · 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 <[email protected]>
Re: [PATCH v2 1/2] rust: add a wrapper for the `num_possible_cpus` C function
In reply to Andreas HindborgQuoted 5 lines · 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 <[email protected]>
Quoted 5 lines
--- rust/helpers/helpers.c | 1 + rust/helpers/num_cpus.c | 8 ++++++++ rust/kernel/cpu.rs | 7 +++++++ 3 files changed, 16 insertions(+)
Andreas Hindborg
reviewerPATCH v2 2/2 rust: add a wrapper for the `nr_online_nodes` C function
In reply to Andreas HindborgThis 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 | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+)
rust/kernel/lib.rs +1 -0
@@ -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;
rust/kernel/numa.rs new file +19 -0
@@ -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 + } +}
Signature · 1 line
2.51.2
Re: [PATCH v2 2/2] rust: add a wrapper for the `nr_online_nodes` C function
Quoted 5 lines · 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 <[email protected]>
I guess the NumaNode struct should be moved to this new file.
Gary Guo
reviewerRe: [PATCH v2 2/2] rust: add a wrapper for the `nr_online_nodes` C function
In reply to Andreas HindborgQuoted 32 lines · 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 <[email protected]>--- rust/kernel/lib.rs | 1 + rust/kernel/numa.rs | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+)rust/kernel/lib.rs +1
-0@@ -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;rust/kernel/numa.rs new file +4
-0@@ -0,0 +1,19 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! NUMA topology utilities. +//!
rust/kernel/numa.rs line 14
Quoted 10 lines
rust/kernel/numa.rs new file +10
-0+//! 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
Quoted 5 lines
rust/kernel/numa.rs new file +5
-0+#[allow(unused_unsafe)] +unsafe { + bindings::nr_online_nodes + } +}
Andreas Hindborg
reviewerRe: [PATCH v2 2/2] rust: add a wrapper for the `nr_online_nodes` C function
Quoted 37 lines · Gary Guo
Quoted 36 lines · 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 <[email protected]>--- rust/kernel/lib.rs | 1 + rust/kernel/numa.rs | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+)rust/kernel/lib.rs +1
-0@@ -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;rust/kernel/numa.rs new file +8
-0@@ -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; +
Quoted 8 lines
rust/kernel/numa.rs line 14
Quoted 6 lines
rust/kernel/numa.rs new file +6
-0+/// 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
Gary Guo
reviewerRe: [PATCH v2 2/2] rust: add a wrapper for the `nr_online_nodes` C function
Quoted 48 lines · Andreas Hindborg
Quoted 37 lines · Gary Guo
Quoted 36 lines · 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 <[email protected]>--- rust/kernel/lib.rs | 1 + rust/kernel/numa.rs | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+)rust/kernel/lib.rs +1
-0@@ -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;rust/kernel/numa.rs new file +8
-0@@ -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; +Quoted 8 lines
rust/kernel/numa.rs line 14
Quoted 6 lines
rust/kernel/numa.rs new file +6
-0+/// 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?
Quoted 4 lines
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