From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pf1-f175.google.com (mail-pf1-f175.google.com [209.85.210.175]) (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 173B6253F14 for ; Mon, 9 Jun 2025 10:51:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.175 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749466283; cv=none; b=ZSiSNn9hPC6w6j642WYVHz8nYaq563ojH6ODvg7ptZoERIElXLPL6UQM7PlbyBttyPR/POK55/T6Gs/4onN0oUOmzum94J3TKGYTTszI7AHPP1fHPtehGQkGo+yLvMvyAsnmutGQzi9EYdfcEFV3CbdJg2d72V8BrP6D6gJXSyA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749466283; c=relaxed/simple; bh=XrghL7Anp61mSscRnEBfEyNV90p7S5EayNDGXNwRoKM=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=dNvUM6/mwt0qoF8qj9UNu2e2UtzKwvuA7bFyxhw3V1H9Kb1TxFNDl6WUEdGOkweiPRmpquABt5gO/90hgCOWIy4+gEz+kmvCkaZBv7qvMPaWRend93giaHH6cv9z2GsFEHsUp9N5mUEvjuYXbVJf726MiVmbwiAbxQF3uCJPeAE= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org; spf=pass smtp.mailfrom=linaro.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b=sWjUeL6j; arc=none smtp.client-ip=209.85.210.175 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linaro.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="sWjUeL6j" Received: by mail-pf1-f175.google.com with SMTP id d2e1a72fcca58-747fba9f962so3463452b3a.0 for ; Mon, 09 Jun 2025 03:51:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1749466281; x=1750071081; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=mc2uNagszMlJu32uWlAF6xHcdKDRl2wpqVFIK+wVb70=; b=sWjUeL6jfMPgbYey0A/YKhqDevz8E9ohG6GeId94nsgdmAGqeLu3XkxAMTl20HlPOD XTeYsCV6x5+sVBxJ7D2M1vNsZe/rYmBztnzHVqwfHuxvNgpSXC689ULP7HZJVEAMElNR 3EgxUp0aYJ4lEzPDKVLVqTsOE8QxJHTbAVY1xpcF87p+7exYXiWhi0CLJfVea29fUVrB G0Y0Tnwh8LCMovW36DL8raEGjru/R0woGI+dle2Ux7a/FQNmEbav1dXTd1E5TLuLLAUL n9c54+M9AiaKlU6lfFN8TmjOOEGZFO2DRAsh4kOQ4i+Nr6uUP+bA2KjVnRqoYf+XJ/wd V9PQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1749466281; x=1750071081; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=mc2uNagszMlJu32uWlAF6xHcdKDRl2wpqVFIK+wVb70=; b=pj1H1TECQ6KhS8dYbM4+phITKN7ifs9Ht1PYYjokOz+kQlVaibmjr2aj29ed62Kkk+ P/W7IjoZ1k9DFFs8PXdKkzOSdwN0Bai9JQoooRath5mMnxGjWL4ZN7Qg6L8PVWMsGgRt ttR9na3mFyD0rojaROwqPYhngnIKq3VW9kTDg/igvk6bZMP63/PitBprX21qK8XzlzD5 1HNMY+2wuVMM8zz7RhW81ne95TfeTpXVUKLVpXFla4wM++89jGkhmGRWq910vRsQz2zR TsxLSHzFO1ocV7/M53dpvA9dsyrjwZVccrVccHZkJF7KF6ygHxAyD3ZsV7L8N5Vmwgyt UFkQ== X-Forwarded-Encrypted: i=1; AJvYcCUstfQsVztIUmBXxzFMOIRGWuC6CKBQPDnRtVxOsKExBuzoBqtz9vo2w9lL9+Q22MzoQAE2IBK+gBHnI9INAg==@vger.kernel.org X-Gm-Message-State: AOJu0YyIdPCDAxIXGgz7and4c9HKLxGSK+mnbxXtGC03VkbA5oo1jtO4 PdV3cMzFhytf7l0cs2qP3CYJmQJxgkFasGDAsVNJ/MbsTeRmoYoAW7vgqb4ZIAtdSGc= X-Gm-Gg: ASbGncsIU3ytWktLnd2Gl6L1ipWmIFbGVWMkNCdldRifGEBALUq05UEXH9nXpjVAz5a vUFjaXsitVg3n+/Hb99nGYSE/75k3sNtJ03j8PsQodJjVcXQ6mPYTkWGF2+qWFYrcD0/sKaqpcc 0qovvlIJn3STSg8+RFXSk/EweFDPzNlPoOGK5OVbr/QM4qqdK8WBwaycknX8Lgw6K+r4/+a9Erv TRejCE//i9o4C7g6i48Q6jBNRbi88is983maNNjy4B3R5MVyBxgSDIVjyy2E81OZLkFQXXSALgt 0PaU+bxnseyb+S03zuocOKwbxZXDKEyJFxAZJzM5DxPs2Svlcwr4CEn0xhyC3l8= X-Google-Smtp-Source: AGHT+IG6kaISh+EtPBIg/Q3w1tVj/21ta5hFi8682AcPrVymf61ekN9kwGXxyVoMkvT4Ut7edFYNMQ== X-Received: by 2002:a05:6a00:94a0:b0:746:2217:5863 with SMTP id d2e1a72fcca58-74839af6c88mr8878390b3a.6.1749466281380; Mon, 09 Jun 2025 03:51:21 -0700 (PDT) Received: from localhost ([122.172.81.72]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7482b0bd815sm5625169b3a.109.2025.06.09.03.51.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Jun 2025 03:51:20 -0700 (PDT) From: Viresh Kumar To: Boqun Feng , "Rafael J. Wysocki" , Thomas Gleixner , Peter Zijlstra , Miguel Ojeda , Alex Gaynor , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich Cc: Viresh Kumar , Vincent Guittot , Yury Norov , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH V2 1/2] rust: cpu: Introduce CpuId abstraction Date: Mon, 9 Jun 2025 16:21:05 +0530 Message-Id: X-Mailer: git-send-email 2.31.1.272.g89b43f80a514 In-Reply-To: References: Precedence: bulk X-Mailing-List: rust-for-linux@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit This adds abstraction for representing a CPU identifier. Suggested-by: Boqun Feng Signed-off-by: Viresh Kumar --- rust/kernel/cpu.rs | 102 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) diff --git a/rust/kernel/cpu.rs b/rust/kernel/cpu.rs index 10c5c3b25873..0b545dbf5c83 100644 --- a/rust/kernel/cpu.rs +++ b/rust/kernel/cpu.rs @@ -6,6 +6,108 @@ use crate::{bindings, device::Device, error::Result, prelude::ENODEV}; +/// Returns the maximum number of possible CPUs in the current system configuration. +#[inline] +pub fn nr_cpu_ids() -> u32 { + #[cfg(any(NR_CPUS_1, CONFIG_FORCE_NR_CPUS))] + { + bindings::NR_CPUS + } + + #[cfg(not(any(NR_CPUS_1, CONFIG_FORCE_NR_CPUS)))] + // SAFETY: `nr_cpu_ids` is a valid global provided by the kernel. + unsafe { + bindings::nr_cpu_ids + } +} + +/// The CPU ID. +/// +/// Represents a CPU identifier as a wrapper around an `u32`. +/// +/// # Invariants +/// +/// The CPU ID must always lie within the range `[0, nr_cpu_ids())`. +/// +/// ## Examples +/// +/// ``` +/// use kernel::cpu::CpuId; +/// +/// let cpu = 0; +/// +/// // SAFETY: 0 is always a valid CPU number. +/// let id = unsafe { CpuId::from_u32_unchecked(cpu) }; +/// +/// assert_eq!(id.as_u32(), cpu); +/// assert!(CpuId::from_i32(0).is_some()); +/// assert!(CpuId::from_i32(-1).is_none()); +/// ``` +#[derive(Copy, Clone, PartialEq, Eq, Debug)] +pub struct CpuId(u32); + +impl CpuId { + /// Creates a new [`CpuId`] from the given `id` without checking bounds. + /// + /// # Safety + /// + /// 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 { + // INVARIANT: The function safety guarantees `id` is a valid CPU id. + Self(id as u32) + } + + /// Creates a new [`CpuId`] from the given `id`, checking that it is valid. + pub fn from_i32(id: i32) -> Option { + 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) }) + } + } + + /// Creates a new [`CpuId`] from the given `id` without checking bounds. + /// + /// # Safety + /// + /// 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 { + // INVARIANT: The function safety guarantees `id` is a valid CPU id. + Self(id) + } + + /// Creates a new [`CpuId`] from the given `id`, checking that it is valid. + pub fn from_u32(id: u32) -> Option { + 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) }) + } + } + + /// Returns CPU number. + #[inline] + pub fn as_u32(&self) -> u32 { + self.0 + } +} + +impl From for u32 { + fn from(id: CpuId) -> Self { + id.as_u32() + } +} + +impl From for i32 { + fn from(id: CpuId) -> Self { + id.as_u32() as i32 + } +} + /// Creates a new instance of CPU's device. /// /// # Safety -- 2.31.1.272.g89b43f80a514