summaryrefslogtreecommitdiff
path: root/rust/kernel
diff options
context:
space:
mode:
authorViresh Kumar <viresh.kumar@linaro.org>2025-06-09 16:44:16 +0530
committerViresh Kumar <viresh.kumar@linaro.org>2025-06-12 10:31:28 +0530
commitc7f005f70d22cd5613cac30bf6d34867189e36a9 (patch)
treeca697ecc8168ed4d9ca9315ac08ed530ac204c61 /rust/kernel
parent33db8c97b4cfa0328054fb755dfbcd6e7f3c7a9d (diff)
rust: cpu: Add CpuId::current() to retrieve current CPU ID
Introduce `CpuId::current()`, a constructor that wraps the C function `raw_smp_processor_id()` to retrieve the current CPU identifier without guaranteeing stability. This function should be used only when the caller can ensure that the CPU ID won't change unexpectedly due to preemption or migration. Suggested-by: Boqun Feng <boqun.feng@gmail.com> Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org> Reviewed-by: Boqun Feng <boqun.feng@gmail.com>
Diffstat (limited to 'rust/kernel')
-rw-r--r--rust/kernel/cpu.rs11
1 files changed, 11 insertions, 0 deletions
diff --git a/rust/kernel/cpu.rs b/rust/kernel/cpu.rs
index abc780d7a8ec..b75403b0eb56 100644
--- a/rust/kernel/cpu.rs
+++ b/rust/kernel/cpu.rs
@@ -102,6 +102,17 @@ impl CpuId {
pub fn as_u32(&self) -> u32 {
self.0
}
+
+ /// Returns the ID of the CPU the code is currently running on.
+ ///
+ /// The returned value is considered unstable because it may change
+ /// unexpectedly due to preemption or CPU migration. It should only be
+ /// used when the context ensures that the task remains on the same CPU
+ /// or the users could use a stale (yet valid) CPU ID.
+ pub fn current() -> Self {
+ // SAFETY: raw_smp_processor_id() always returns a valid CPU ID.
+ unsafe { Self::from_u32_unchecked(bindings::raw_smp_processor_id()) }
+ }
}
impl From<CpuId> for u32 {