Skip to contentSkip to conversation

rust: add a wrapper for the `num_possible_cpus` C function

PATCH In review Andreas Hindborg 2 participants 2 messages Oct 30 – Nov 2

Subsystems (3)
lore
Share & tools
lore

Andreas Hindborg

author reviewer
#0 lore raw

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

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

---
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/[email protected]

---
 rust/helpers/cpumask.c |  6 ++++++
 rust/kernel/cpumask.rs | 13 +++++++++++++
 2 files changed, 19 insertions(+)
rust/helpers/cpumask.c +6 -0
index 5deced5b975e..43e9d40e0460 100644
@@ -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();
+}
rust/kernel/cpumask.rs +13 -0
index 44bb36636ee3..67664ec8fbd7 100644
@@ -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,

Signature · 1 line
Andreas Hindborg <[email protected]>
maintainer Acked-by on v4 1/1
#1 lore raw
Quoted 54 lines · Andreas Hindborg

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

---
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/[email protected]

---
 rust/helpers/cpumask.c |  6 ++++++
 rust/kernel/cpumask.rs | 13 +++++++++++++
 2 files changed, 19 insertions(+)
rust/helpers/cpumask.c +6 -0
index 5deced5b975e..43e9d40e0460 100644
@@ -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();
+}
rust/kernel/cpumask.rs +2 -0
index 44bb36636ee3..67664ec8fbd7 100644
@@ -16,6 +16,19 @@
 
 use core::ops::{Deref, DerefMut};
 
+/// Returns the maximum number of CPUs that may be online on the system.
+///

rust/kernel/cpumask.rs line 30

Quoted 10 lines
rust/kernel/cpumask.rs +10 -0
+/// 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() }
+}
Signature · 1 line
viresh