[PATCH v2 2/2] rust: cpu: use `unsafe_precondition_assert!`

From: Ritvik Gupta

Date: Mon May 18 2026 - 18:58:22 EST


Replace `debug_assert!` invocations with
`unsafe_precondition_assert!` in `from_i32_unchecked`
and `from_u32_unchecked` unsafe functions.

Suggested-by: Miguel Ojeda <ojeda@xxxxxxxxxx>
Link: https://github.com/Rust-for-Linux/linux/issues/1232
Signed-off-by: Ritvik Gupta <ritvikfoss@xxxxxxxxx>
---
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