Skip to contentSkip to conversation

rust: add a few helpers

PATCH Superseded Andreas Hindborg 3 participants 9 messages Jun 5 – Jun 8

Subsystems (2)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Andreas Hindborg

author reviewer
#0 lore raw

v2 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]>
author reviewer
#1 lore raw

PATCH 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
index 625921e27dfb..bb250648e0a9 100644
@@ -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
new file mode 100644
index 000000000000..54e8bacb46c3
@@ -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
index cb6c0338ef5a..b38b6914b12e 100644
@@ -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
reviewer Reviewed-by on v2 1/2
#3 lore raw

Re: [PATCH v2 1/2] rust: add a wrapper for the `num_possible_cpus` C function

In reply to Andreas Hindborg
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.

Quoted 5 lines
---
 rust/helpers/helpers.c  | 1 +
 rust/helpers/num_cpus.c | 8 ++++++++
 rust/kernel/cpu.rs      | 7 +++++++
 3 files changed, 16 insertions(+)
author reviewer
#4 lore raw

PATCH v2 2/2 rust: add a wrapper for the `nr_online_nodes` C function

In reply to Andreas Hindborg

This 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
index b72b2fbe046d..00713b2adcac 100644
@@ -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
new file mode 100644
index 000000000000..760db072a6c1
@@ -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
reviewer Reviewed-by on v2 2/2
#5 lore raw

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.

I guess the NumaNode struct should be moved to this new file.

reviewer
#6 lore raw

Re: [PATCH v2 2/2] rust: add a wrapper for the `nr_online_nodes` C function

In reply to Andreas Hindborg
Quoted 32 lines · Andreas Hindborg

This 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
index b72b2fbe046d..00713b2adcac 100644
@@ -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
new file mode 100644
index 000000000000..760db072a6c1
@@ -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
+    }
+}
author reviewer
#7 lore raw

Re: [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.

---
 rust/kernel/lib.rs  |  1 +
 rust/kernel/numa.rs | 19 +++++++++++++++++++
 2 files changed, 20 insertions(+)
rust/kernel/lib.rs +1 -0
index b72b2fbe046d..00713b2adcac 100644
@@ -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
new file mode 100644
index 000000000000..760db072a6c1
@@ -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

reviewer
#8 lore raw

Re: [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.

---
 rust/kernel/lib.rs  |  1 +
 rust/kernel/numa.rs | 19 +++++++++++++++++++
 2 files changed, 20 insertions(+)
rust/kernel/lib.rs +1 -0
index b72b2fbe046d..00713b2adcac 100644
@@ -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
new file mode 100644
index 000000000000..760db072a6c1
@@ -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