From: Andreas Hindborg <a.hindborg@kernel.org>
To: "Miguel Ojeda" <ojeda@kernel.org>,
"Boqun Feng" <boqun@kernel.org>, "Gary Guo" <gary@garyguo.net>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Benno Lossin" <lossin@kernel.org>,
"Alice Ryhl" <aliceryhl@google.com>,
"Trevor Gross" <tmgross@umich.edu>,
"Danilo Krummrich" <dakr@kernel.org>,
"Daniel Almeida" <daniel.almeida@collabora.com>,
"Tamir Duberstein" <tamird@kernel.org>,
"Alexandre Courbot" <acourbot@nvidia.com>,
"Onur Özkan" <work@onurozkan.dev>,
"Thomas Gleixner" <tglx@kernel.org>,
"Peter Zijlstra" <peterz@infradead.org>,
"Yury Norov" <yury.norov@gmail.com>,
"Viresh Kumar" <viresh.kumar@linaro.org>
Cc: Dirk Behme <dirk.behme@de.bosch.com>,
Yury Norov <ynorov@nvidia.com>,
linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org,
Andreas Hindborg <a.hindborg@kernel.org>
Subject: [PATCH v4] rust: add a wrapper for the `num_possible_cpus` C function
Date: Fri, 30 Oct 2026 17:06:49 +0100 [thread overview]
Message-ID: <20261030-num-cpus-v4-1-6b59d5470da2@kernel.org> (raw)
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>
reply other threads:[~2026-10-30 16:08 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261030-num-cpus-v4-1-6b59d5470da2@kernel.org \
--to=a.hindborg@kernel.org \
--cc=acourbot@nvidia.com \
--cc=aliceryhl@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun@kernel.org \
--cc=dakr@kernel.org \
--cc=daniel.almeida@collabora.com \
--cc=dirk.behme@de.bosch.com \
--cc=gary@garyguo.net \
--cc=linux-kernel@vger.kernel.org \
--cc=lossin@kernel.org \
--cc=ojeda@kernel.org \
--cc=peterz@infradead.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=tamird@kernel.org \
--cc=tglx@kernel.org \
--cc=tmgross@umich.edu \
--cc=viresh.kumar@linaro.org \
--cc=work@onurozkan.dev \
--cc=ynorov@nvidia.com \
--cc=yury.norov@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox