From: Viresh Kumar <viresh.kumar@linaro.org>
To: Miguel Ojeda <miguel.ojeda.sandonis@gmail.com>
Cc: "Boqun Feng" <boqun.feng@gmail.com>,
"Rafael J. Wysocki" <rafael@kernel.org>,
"Thomas Gleixner" <tglx@linutronix.de>,
"Peter Zijlstra" <peterz@infradead.org>,
"Miguel Ojeda" <ojeda@kernel.org>,
"Alex Gaynor" <alex.gaynor@gmail.com>,
"Gary Guo" <gary@garyguo.net>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Benno Lossin" <lossin@kernel.org>,
"Andreas Hindborg" <a.hindborg@kernel.org>,
"Alice Ryhl" <aliceryhl@google.com>,
"Trevor Gross" <tmgross@umich.edu>,
"Danilo Krummrich" <dakr@kernel.org>,
"Vincent Guittot" <vincent.guittot@linaro.org>,
"Yury Norov" <yury.norov@gmail.com>,
rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH V2 1/2] rust: cpu: Introduce CpuId abstraction
Date: Tue, 10 Jun 2025 11:37:11 +0530 [thread overview]
Message-ID: <20250610060711.zp6lua4kcwi2z777@vireshk-i7> (raw)
In-Reply-To: <CANiq72=WpuGELzLbH-fxdOeJy9fiDFwatz6ynERDh=HP2z2MBw@mail.gmail.com>
Thanks Miguel for the feedback.
On 09-06-25, 14:01, Miguel Ojeda wrote:
> On Mon, Jun 9, 2025 at 12:51 PM Viresh Kumar <viresh.kumar@linaro.org> wrote:
> > + pub unsafe fn from_i32_unchecked(id: i32) -> Self {
>
> Why do we need the `i32` versions?
>
> Is it just for `bios_limit_callback`? If so, I would just convert there.
It looked like that there are few interfaces in the kernel which
accept an `i32` CPU ID. One example is cpumask layer, where
`cpumask_set_cpu()` expects an `unsigned int` but
`cpumask_{clear|test}_cpu()` expects an `int`.
Though the only user of the `i32` version for now is
`bios_limit_callback`, which I can handle without it too.
What do you suggest ?
> Relatedly, why isn't that callback's type `c_int` on the Rust side?
Sorry, not sure I understood that. Are you talking about type of the
`cpu` argument ?
extern "C" fn bios_limit_callback(cpu: i32, limit: *mut u32) -> kernel::ffi::c_int
> Finally, can we add a `debug_assert!()` on the `_unchecked()` variant,
> since it is something we can easily check?
Changes so far:
diff --git a/rust/kernel/cpu.rs b/rust/kernel/cpu.rs
index 68d7d86df27c..8d48946090e3 100644
--- a/rust/kernel/cpu.rs
+++ b/rust/kernel/cpu.rs
@@ -23,11 +23,11 @@ pub fn nr_cpu_ids() -> u32 {
/// The CPU ID.
///
-/// Represents a CPU identifier as a wrapper around an `u32`.
+/// Represents a CPU identifier as a wrapper around an [`u32`].
///
/// # Invariants
///
-/// The CPU ID must always lie within the range `[0, nr_cpu_ids())`.
+/// The CPU ID lies within the range `[0, nr_cpu_ids())`.
///
/// # Examples
///
@@ -54,6 +54,9 @@ impl CpuId {
/// The caller must ensure that `id` is a valid CPU ID (i.e., `0 <= id < nr_cpu_ids()`).
#[inline]
pub unsafe fn from_i32_unchecked(id: i32) -> Self {
+ debug_assert!(id >= 0);
+ debug_assert!((id as u32) < nr_cpu_ids());
+
// INVARIANT: The function safety guarantees `id` is a valid CPU id.
Self(id as u32)
}
@@ -63,8 +66,8 @@ pub fn from_i32(id: i32) -> Option<Self> {
if id < 0 || id as u32 >= nr_cpu_ids() {
None
} else {
- // SAFETY: `id` has just been checked as a valid CPU ID.
- Some(unsafe { Self::from_i32_unchecked(id) })
+ // INVARIANT: `id` has just been checked as a valid CPU ID.
+ Some(Self(id as u32))
}
}
@@ -75,6 +78,8 @@ pub fn from_i32(id: i32) -> Option<Self> {
/// The caller must ensure that `id` is a valid CPU ID (i.e., `0 <= id < nr_cpu_ids()`).
#[inline]
pub unsafe fn from_u32_unchecked(id: u32) -> Self {
+ debug_assert!(id < nr_cpu_ids());
+
// INVARIANT: The function safety guarantees `id` is a valid CPU id.
Self(id)
}
@@ -84,8 +89,8 @@ pub fn from_u32(id: u32) -> Option<Self> {
if id >= nr_cpu_ids() {
None
} else {
- // SAFETY: `id` has just been checked as a valid CPU ID.
- Some(unsafe { Self::from_u32_unchecked(id) })
+ // INVARIANT: `id` has just been checked as a valid CPU ID.
+ Some(Self(id))
}
}
--
viresh
next prev parent reply other threads:[~2025-06-10 6:07 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-06-09 10:51 [PATCH V2 0/2] rust: Introduce CpuId and fix cpumask doctest Viresh Kumar
2025-06-09 10:51 ` [PATCH V2 1/2] rust: cpu: Introduce CpuId abstraction Viresh Kumar
2025-06-09 11:18 ` Viresh Kumar
2025-06-09 14:04 ` Boqun Feng
2025-06-10 8:39 ` Viresh Kumar
2025-06-09 12:01 ` Miguel Ojeda
2025-06-10 6:07 ` Viresh Kumar [this message]
2025-06-10 9:43 ` Miguel Ojeda
2025-06-10 10:18 ` Viresh Kumar
2025-06-10 10:23 ` Miguel Ojeda
2025-06-10 10:00 ` Viresh Kumar
2025-06-10 10:14 ` Miguel Ojeda
2025-06-10 10:21 ` Miguel Ojeda
2025-06-10 10:38 ` Viresh Kumar
2025-06-10 10:19 ` Viresh Kumar
2025-06-10 10:50 ` Miguel Ojeda
2025-06-09 10:51 ` [PATCH V2 2/2] rust: Use CpuId in place of raw CPU numbers Viresh Kumar
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=20250610060711.zp6lua4kcwi2z777@vireshk-i7 \
--to=viresh.kumar@linaro.org \
--cc=a.hindborg@kernel.org \
--cc=alex.gaynor@gmail.com \
--cc=aliceryhl@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun.feng@gmail.com \
--cc=dakr@kernel.org \
--cc=gary@garyguo.net \
--cc=linux-kernel@vger.kernel.org \
--cc=lossin@kernel.org \
--cc=miguel.ojeda.sandonis@gmail.com \
--cc=ojeda@kernel.org \
--cc=peterz@infradead.org \
--cc=rafael@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=tglx@linutronix.de \
--cc=tmgross@umich.edu \
--cc=vincent.guittot@linaro.org \
--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
all inboxes | Powered by JetHome®