Skip to contentSkip to conversation

rust: add a few helpers

PATCH Superseded Andreas Hindborg 4 participants 7 messages Feb 15 – Feb 16 Rust

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Andreas Hindborg

author reviewer
#0 lore raw

cover rust: add a few helpers

This series adds two helpers for obtaining information about the number of numa domans and cpu count of the system.

---
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/lib.rs      | 17 +++++++++++++++++
 3 files changed, 26 insertions(+)

---
base-commit: 05f7e89ab9731565d8a62e3b5d1ec206485eeb0b
change-id: 20260215-cpu-helpers-08efb2572487

Best regards,

Signature · 1 line
Andreas Hindborg <[email protected]>
author reviewer
#1 lore raw

PATCH 1/2 rust: add a wrapper for the `num_possible_cpus` C function

This function returns the max number of CPUs that can be online.

---
 rust/helpers/helpers.c  | 1 +
 rust/helpers/num_cpus.c | 8 ++++++++
 rust/kernel/lib.rs      | 6 ++++++
 3 files changed, 15 insertions(+)
rust/helpers/helpers.c +1 -0
index 79c72762ad9c4..a19d6562fcb3a 100644
@@ -35,6 +35,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 0000000000000..daddacb0418d8
@@ -0,0 +1,8 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <linux/cpumask.h>
+
+unsigned int rust_helper_num_possible_cpus(void)
+{
+	return  num_possible_cpus();
+}
rust/kernel/lib.rs +6 -0
index f812cf1200428..7040e105dd3b9 100644
@@ -366,3 +366,9 @@ pub fn file_from_location<'a>(loc: &'a core::panic::Location<'a>) -> &'a core::f
         c"<Location::file_as_c_str() not supported>"
     }
 }
+
+/// Returns maximum number of CPUs that may be online on the system.
+pub fn num_possible_cpus() -> u32 {
+    // SAFETY: FFI call with no additional requirements.
+    unsafe { bindings::num_possible_cpus() }
+}
Signature · 1 line
2.51.2
#2 lore raw

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

Quoted 27 lines · Andreas Hindborg

This function returns the max number of CPUs that can be online.

---
  rust/helpers/helpers.c  | 1 +
  rust/helpers/num_cpus.c | 8 ++++++++
  rust/kernel/lib.rs      | 6 ++++++
  3 files changed, 15 insertions(+)
rust/helpers/helpers.c +1 -0
index 79c72762ad9c4..a19d6562fcb3a 100644
@@ -35,6 +35,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 +0 -0
new file mode 100644
index 0000000000000..daddacb0418d8

rust/helpers/num_cpus.c line 5

Quoted 5 lines
rust/helpers/num_cpus.c new file +5 -0
@@ -0,0 +1,8 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <linux/cpumask.h>
+
+unsigned int rust_helper_num_possible_cpus(void)

Should we add __rust_helper here?

Thanks

Dirk

author reviewer
#3 lore raw

PATCH 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.

---
 rust/kernel/lib.rs | 11 +++++++++++
 1 file changed, 11 insertions(+)
rust/kernel/lib.rs +11 -0
index 7040e105dd3b9..4e9f97f03adf2 100644
@@ -372,3 +372,14 @@ pub fn num_possible_cpus() -> u32 {
     // SAFETY: FFI call with no additional requirements.
     unsafe { bindings::num_possible_cpus() }
 }
+
+/// Returns the number of online numa nodes.
+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
#4 lore raw
In reply to Andreas Hindborg
Quoted 2 lines · Andreas Hindborg
 rust/kernel/lib.rs      | 17 +++++++++++++++++

Please avoid adding things to the root of the crate.

Also, for both patches, please mention for what it will be used for.

Thanks!

Cheers,
Miguel

author reviewer
#5 lore raw
Quoted 5 lines · Miguel Ojeda
Quoted 2 lines · Andreas Hindborg
 rust/kernel/lib.rs      | 17 +++++++++++++++++

Please avoid adding things to the root of the crate.

Ok, I'll move them.

Quoted 2 lines

Also, for both patches, please mention for what it will be used for.

The are for rust null block. I have sent a few today already without this mention. I can send a follow up email for each with the details.

Best regards,
Andreas Hindborg