rust: add a few helpers
Andreas Hindborg
reviewercover 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]>
Andreas Hindborg
reviewerPATCH 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
@@ -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
@@ -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
@@ -366,3 +366,9 @@ pub fn file_from_location<'a>(loc: &'a core::panic::Location<'a>) -> &'a core::fc"<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
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.
Signed-off-by Andreas Hindborg <[email protected]>--- 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@@ -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
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
Andreas Hindborg
reviewerPATCH 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.
--- rust/kernel/lib.rs | 11 +++++++++++ 1 file changed, 11 insertions(+)
rust/kernel/lib.rs +11 -0
@@ -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
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
Andreas Hindborg
reviewerQuoted 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
Alice Ryhl
reviewerQuoted 4 lines · Andreas Hindborg
This series adds two helpers for obtaining information about the number of numa domans and cpu count of the system.
Signed-off-by Andreas Hindborg <[email protected]>
I'd make both #[inline].
Alice