mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v4] rust: add a wrapper for the `num_possible_cpus` C function
@ 2026-10-30 16:06 Andreas Hindborg
  0 siblings, 0 replies; only message in thread
From: Andreas Hindborg @ 2026-10-30 16:06 UTC (permalink / raw)
  To: Miguel Ojeda, Boqun Feng, Gary Guo, Björn Roy Baron,
	Benno Lossin, Alice Ryhl, Trevor Gross, Danilo Krummrich,
	Daniel Almeida, Tamir Duberstein, Alexandre Courbot,
	Onur Özkan, Thomas Gleixner, Peter Zijlstra, Yury Norov,
	Viresh Kumar
  Cc: Dirk Behme, Yury Norov, linux-kernel, rust-for-linux, Andreas Hindborg

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

Reviewed-by: Alice Ryhl <aliceryhl@google.com>
Reviewed-by: Gary Guo <gary@garyguo.net>
Assisted-by: LLM
Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org>
---
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@kernel.org
---
 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@kernel.org>



^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2026-10-30 16:08 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-10-30 16:06 [PATCH v4] rust: add a wrapper for the `num_possible_cpus` C function Andreas Hindborg

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®