From: Ritvik Gupta <ritvikfoss@gmail.com>
To: tglx@kernel.org, peterz@infradead.org, ojeda@kernel.org,
boqun@kernel.org, gary@garyguo.net, bjorn3_gh@protonmail.com,
lossin@kernel.org, a.hindborg@kernel.org, aliceryhl@google.com,
tmgross@umich.edu, dakr@kernel.org
Cc: linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org,
skhan@linuxfoundation.org
Subject: [PATCH v2 2/2] rust: cpu: use `unsafe_precondition_assert!`
Date: Tue, 19 May 2026 04:31:25 +0530 [thread overview]
Message-ID: <20260518230125.476165-3-ritvikfoss@gmail.com> (raw)
In-Reply-To: <20260518230125.476165-1-ritvikfoss@gmail.com>
Replace `debug_assert!` invocations with
`unsafe_precondition_assert!` in `from_i32_unchecked`
and `from_u32_unchecked` unsafe functions.
Suggested-by: Miguel Ojeda <ojeda@kernel.org>
Link: https://github.com/Rust-for-Linux/linux/issues/1232
Signed-off-by: Ritvik Gupta <ritvikfoss@gmail.com>
---
rust/kernel/cpu.rs | 22 +++++++++++++++++-----
1 file changed, 17 insertions(+), 5 deletions(-)
diff --git a/rust/kernel/cpu.rs b/rust/kernel/cpu.rs
index cb6c0338ef5a..57bad2d8c788 100644
--- a/rust/kernel/cpu.rs
+++ b/rust/kernel/cpu.rs
@@ -4,7 +4,7 @@
//!
//! C header: [`include/linux/cpu.h`](srctree/include/linux/cpu.h)
-use crate::{bindings, device::Device, error::Result, prelude::ENODEV};
+use crate::{bindings, device::Device, error::Result, prelude::*};
/// Returns the maximum number of possible CPUs in the current system configuration.
#[inline]
@@ -54,8 +54,14 @@ 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());
+ unsafe_precondition_assert!(
+ id >= 0,
+ "`CpuId::from_i32_unchecked` requires a non-negative `id`"
+ );
+ unsafe_precondition_assert!(
+ (id as u32) < nr_cpu_ids(),
+ "`CpuId::from_i32_unchecked` requires a valid `id` (i.e., `0 <= id < nr_cpu_ids()`)"
+ );
// INVARIANT: The function safety guarantees `id` is a valid CPU id.
Self(id as u32)
@@ -78,10 +84,16 @@ 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());
+ unsafe_precondition_assert!(
+ id < nr_cpu_ids(),
+ "`CpuId::from_u32_unchecked` requires a valid `id` (i.e., `0 <= id < nr_cpu_ids()`)"
+ );
// Ensure the `id` fits in an [`i32`] as it's also representable that way.
- debug_assert!(id <= i32::MAX as u32);
+ unsafe_precondition_assert!(
+ id <= i32::MAX as u32,
+ "`CpuId::from_u32_unchecked` requires the `id` to not overflow `i32::MAX`"
+ );
// INVARIANT: The function safety guarantees `id` is a valid CPU id.
Self(id)
--
2.54.0
next prev parent reply other threads:[~2026-05-18 22:57 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-05-18 23:01 [PATCH v2 0/2] rust: re-export `unsafe_precondition_assert!` and use in cpu module Ritvik Gupta
2026-05-18 23:01 ` [PATCH v2 1/2] rust: prelude: re-export `unsafe_precondition_assert!` Ritvik Gupta
2026-05-18 23:01 ` Ritvik Gupta [this message]
2026-05-19 5:41 ` [PATCH v2 2/2] rust: cpu: use `unsafe_precondition_assert!` Miguel Ojeda
2026-05-21 17:02 ` Ritvik Gupta
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=20260518230125.476165-3-ritvikfoss@gmail.com \
--to=ritvikfoss@gmail.com \
--cc=a.hindborg@kernel.org \
--cc=aliceryhl@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun@kernel.org \
--cc=dakr@kernel.org \
--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=skhan@linuxfoundation.org \
--cc=tglx@kernel.org \
--cc=tmgross@umich.edu \
/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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.