From nobody Mon May 25 04:33:41 2026 Received: from mail-pj1-f54.google.com (mail-pj1-f54.google.com [209.85.216.54]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C26B5382F31 for ; Mon, 18 May 2026 17:40:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.54 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779126004; cv=none; b=U2h0Er81Jt3BlOW921jZ6ZsDqJH6c65QhAdaw0v8RW22o94EhwpGnWYETHNJDjBqi54fJGIfD3U0+kZZdNRLEKdHQNVhToHjfNepkzGQ4T7ukapF+UCwSEB9VcvWNCAMz8g3uezTJfxM6xpbiBBQkzk4fzmo89qHRmsJw4qThpM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779126004; c=relaxed/simple; bh=TdgqM4RiUccFwijsliiJ8OCoq9d8bDGE9FXsLFVxT2Y=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=YJ+GF5D0zoCCXOZ2roithk478t8cr9+og4EibmARKTc2TcAgvy9qIBAWqyHkQuM/ezk25CGpi38WiuVJ7sEJkHDJ5DxLMUbBL2Xk4IVRRkjp4A23WsGn01LFuDv8GCjTI29uhKpT/Ujr2Y2AK08tJaDlZYF1nx+E84Wh0TNkdbA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=j4It2TOG; arc=none smtp.client-ip=209.85.216.54 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="j4It2TOG" Received: by mail-pj1-f54.google.com with SMTP id 98e67ed59e1d1-36974217d4eso1451200a91.2 for ; Mon, 18 May 2026 10:40:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1779126002; x=1779730802; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=aTNSk1VdfU2RERnkAfCi3v1No0pcJQOMmaZgRrBJNn8=; b=j4It2TOGJwVrUgOed66aV/bVVw/FCp/O3rTecyssDiqbK2jaqSJlsKk4L0RtmrzoK9 VcggdrDPFs3xFPnxO065GZyRLba3LQWijHLdz1Z1fzYySdy52n1Bu5XSzDwxbz/IbB0I o+ihY0SdDMQuUpDfbMauP0utvQObtp14bc+S4p2x7YFJAj8KvXoSgXNNvHEZUsKMSAwH txIzXAGChbtmg/5W892Hu4Ic0ttV/0UbrqQSCT40RYbyePjOnEGyMiu48cDKh3gxVC8V /VrnaRDXiSej0rjNSaNgmnqSDzYjcWfdO/Dc21ak4hDltb6eTAoDgAM+ToYQBy0nfASt 7s9A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1779126002; x=1779730802; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=aTNSk1VdfU2RERnkAfCi3v1No0pcJQOMmaZgRrBJNn8=; b=hH8IruSx8GbxBRuHQklYkQwPoLYQa5R9HAYaIBXX3u3gcLZrt5W6IbEJxjiQGyUQi7 nqkx5mbtZz4BjzvYYUBlXLCXB/ekX5eV0SeoXtEa1ugrRGO7UxhkX941W6819SnE5K1r JzqvEUcADhSA3ktXsr3zt9cK6vVHwU3EUdFdDNz0mEMuRO2i3DNbV8A5+RNNBs1rbvts NmCS6Mnig1TOcrudBMWcW6qW+ad6gmJ+a8NTllOQqYpoA3LYD86fF7kVLPIZmVgeflN9 fNKzcgzygkVJl/JmFMwxjEdzDKZnt6MkP+XKnQR3082CZDdWHOZtxebg+cfiiNa9vhaL sH7Q== X-Gm-Message-State: AOJu0YzbliFooFmigDMfyv1SXo719HUZkQ4pcKlzHwTQlw4ULNgfWlaQ gtVwWCbl4OsJzRVwAkL/xG2dLkuhKKjt/q52c9Cl7PbRDTrkZLxLtOB/eR2HJ//t X-Gm-Gg: Acq92OEBdp2EQH2yZIf5Mw+ivIq681gFTkyOx1mPp6H5Ha6ttZ7Ks/DkOpqte/ZNkaO gP6Z1kfFdsKniw08tZ9DaP5H9Zb1DBgYDtCjYmu+E4jZmAGEfNLEzubihB2pNxt2tkVSEGsr/6n TbBciq2cjqzGARgBsqGPAyyzgpcsFIMQ3HysNrdlSRFolxF8yfmMYTYDED27rMAMWekm5Zdfedx HAADsfvva6+487IPs/pSZbSGWKfnGliaG7zG0bwZrUB3mV7483ISZ77b16e53qEOf6nf2RqImiP qp/RF9LQ/3fnpc9/NWyDYIAVWBF2V8/b2P29KPNUqElKOHMs8Nh3CbwrzTrTyFT+YejGze4m+Ih Q2OuYpYUk60fLMBeY9KtEpmK8D9dpelis3jBHup8o4geKHnYQcPSldoMqhaBP8uqgobNbfOYOuE CDRskHzI6gd4t6uf7/jErE+e7wPpge4/anoVh8zRKR X-Received: by 2002:a17:90b:3d0a:b0:366:479e:63a5 with SMTP id 98e67ed59e1d1-369518b25cemr15826231a91.2.1779126001970; Mon, 18 May 2026 10:40:01 -0700 (PDT) Received: from fedora.localdomain ([49.36.193.91]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-36951584654sm11887646a91.7.2026.05.18.10.40.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 May 2026 10:40:01 -0700 (PDT) Received: by fedora.localdomain (Postfix, from userid 1000) id 0CDC76508A7; Mon, 18 May 2026 23:14:15 +0530 (IST) From: Ritvik Gupta 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] rust: cpu: use `unsafe_precondition_assert!` Date: Mon, 18 May 2026 23:14:14 +0530 Message-ID: <20260518174414.204573-1-ritvikfoss@gmail.com> X-Mailer: git-send-email 2.54.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Replace `debug_assert!` invocations with `unsafe_precondition_assert!` in `from_i32_unchecked` and `from_u32_unchecked` unsafe functions. Signed-off-by: Ritvik Gupta Suggested-by: Miguel Ojeda --- 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..e33a87304c59 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) =20 -use crate::{bindings, device::Device, error::Result, prelude::ENODEV}; +use crate::{bindings, device::Device, error::Result, prelude::ENODEV, unsa= fe_precondition_assert}; =20 /// Returns the maximum number of possible CPUs in the current system conf= iguration. #[inline] @@ -54,8 +54,14 @@ impl CpuId { /// The caller must ensure that `id` is a valid CPU ID (i.e., `0 <=3D = id < nr_cpu_ids()`). #[inline] pub unsafe fn from_i32_unchecked(id: i32) -> Self { - debug_assert!(id >=3D 0); - debug_assert!((id as u32) < nr_cpu_ids()); + unsafe_precondition_assert!( + id >=3D 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 <= =3D id < nr_cpu_ids()`)" + ); =20 // INVARIANT: The function safety guarantees `id` is a valid CPU i= d. Self(id as u32) @@ -78,10 +84,16 @@ pub fn from_i32(id: i32) -> Option { /// The caller must ensure that `id` is a valid CPU ID (i.e., `0 <=3D = 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_i32_unchecked requires a valid 'id' (i.e., `0 <= =3D id < nr_cpu_ids()`)" + ); =20 // Ensure the `id` fits in an [`i32`] as it's also representable t= hat way. - debug_assert!(id <=3D i32::MAX as u32); + unsafe_precondition_assert!( + id <=3D i32::MAX as u32, + "CpuId::from_u32_unchecked requires the 'id' to not overflow i= 32::MAX" + ); =20 // INVARIANT: The function safety guarantees `id` is a valid CPU i= d. Self(id) --=20 2.54.0