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

Andreas Hindborg posted 2 patches 1 month, 2 weeks ago
[PATCH 1/2] rust: add a wrapper for the `num_possible_cpus` C function
Posted by Andreas Hindborg 1 month, 2 weeks ago
This function returns the max number of CPUs that can be online.

Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org>
---
 rust/helpers/helpers.c  | 1 +
 rust/helpers/num_cpus.c | 8 ++++++++
 rust/kernel/lib.rs      | 6 ++++++
 3 files changed, 15 insertions(+)

diff --git a/rust/helpers/helpers.c b/rust/helpers/helpers.c
index 79c72762ad9c4..a19d6562fcb3a 100644
--- a/rust/helpers/helpers.c
+++ b/rust/helpers/helpers.c
@@ -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"
diff --git a/rust/helpers/num_cpus.c b/rust/helpers/num_cpus.c
new file mode 100644
index 0000000000000..daddacb0418d8
--- /dev/null
+++ b/rust/helpers/num_cpus.c
@@ -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();
+}
diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs
index f812cf1200428..7040e105dd3b9 100644
--- a/rust/kernel/lib.rs
+++ b/rust/kernel/lib.rs
@@ -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() }
+}

-- 
2.51.2
Re: [PATCH 1/2] rust: add a wrapper for the `num_possible_cpus` C function
Posted by Dirk Behme 1 month, 2 weeks ago
On 15.02.2026 21:10, Andreas Hindborg wrote:
> This function returns the max number of CPUs that can be online.
> 
> Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org>
> ---
>   rust/helpers/helpers.c  | 1 +
>   rust/helpers/num_cpus.c | 8 ++++++++
>   rust/kernel/lib.rs      | 6 ++++++
>   3 files changed, 15 insertions(+)
> 
> diff --git a/rust/helpers/helpers.c b/rust/helpers/helpers.c
> index 79c72762ad9c4..a19d6562fcb3a 100644
> --- a/rust/helpers/helpers.c
> +++ b/rust/helpers/helpers.c
> @@ -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"
> diff --git a/rust/helpers/num_cpus.c b/rust/helpers/num_cpus.c
> new file mode 100644
> index 0000000000000..daddacb0418d8
> --- /dev/null
> +++ b/rust/helpers/num_cpus.c
> @@ -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