[PATCH v4] rust: add a wrapper for the `num_possible_cpus` C function

From: Andreas Hindborg

Date: Fri Oct 30 2026 - 12:11:53 EST


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

Reviewed-by: Alice Ryhl <aliceryhl@xxxxxxxxxx>
Reviewed-by: Gary Guo <gary@xxxxxxxxxxx>
Assisted-by: LLM
Signed-off-by: Andreas Hindborg <a.hindborg@xxxxxxxxxx>
---
The Rust null block driver needs this wrapper to put an upper bound on
the number of submit and poll queues it sets up: it never needs more
queues than the number of CPUs that can come online.

This patch was part of the `rust: add a few helpers` series up to v3.
It is independent of the other two patches in that series, so it is
sent on its own from v4 on.

Changes in v4:
- Move the C helper into `rust/helpers/cpumask.c` and the Rust wrapper
into `kernel::cpumask`, to mirror the C side (Miguel, Yury).
- Put `__rust_helper` on a line of its own for greppability (Yury).
- Document that the return value counts possible CPUs and does not
bound CPU IDs (Peter).
- Split this patch out of the `rust: add a few helpers` series, as it
is independent of the remaining patches (Miguel).
- Rebase on v7.3.
- Link to v3: https://msgid.link/20260929-cpu-helpers-v3-0-e968ce5ee613@xxxxxxxxxx
---
rust/helpers/cpumask.c | 6 ++++++
rust/kernel/cpumask.rs | 13 +++++++++++++
2 files changed, 19 insertions(+)

diff --git a/rust/helpers/cpumask.c b/rust/helpers/cpumask.c
index 5deced5b975e..43e9d40e0460 100644
--- a/rust/helpers/cpumask.c
+++ b/rust/helpers/cpumask.c
@@ -81,3 +81,9 @@ void rust_helper_free_cpumask_var(cpumask_var_t mask)
free_cpumask_var(mask);
}
#endif
+
+__rust_helper
+unsigned int rust_helper_num_possible_cpus(void)
+{
+ return num_possible_cpus();
+}
diff --git a/rust/kernel/cpumask.rs b/rust/kernel/cpumask.rs
index 44bb36636ee3..67664ec8fbd7 100644
--- a/rust/kernel/cpumask.rs
+++ b/rust/kernel/cpumask.rs
@@ -16,6 +16,19 @@

use core::ops::{Deref, DerefMut};

+/// Returns the maximum number of CPUs that may be online on the system.
+///
+/// This is a count of CPUs, not a bound on CPU IDs. Possible CPUs need not occupy a contiguous
+/// range of IDs, so a CPU ID must be bounded by [`nr_cpu_ids()`] instead. That is,
+/// `num_possible_cpus() <= nr_cpu_ids()`.
+///
+/// [`nr_cpu_ids()`]: crate::cpu::nr_cpu_ids
+#[inline]
+pub fn num_possible_cpus() -> u32 {
+ // SAFETY: FFI call with no additional requirements.
+ unsafe { bindings::num_possible_cpus() }
+}
+
/// A CPU Mask.
///
/// Rust abstraction for the C `struct cpumask`.

---
base-commit: c03c1a14507dfaf8b856c2608f9ac216021fc222
change-id: 20261028-num-cpus-e51694605371

Best regards,
--
Andreas Hindborg <a.hindborg@xxxxxxxxxx>