[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20250624-rust-percpu-v1-5-9c59b07d2a9c@gmail.com>
Date: Tue, 24 Jun 2025 15:10:43 -0700
From: Mitchell Levy <levymitchell0@...il.com>
To: Miguel Ojeda <ojeda@...nel.org>, Alex Gaynor <alex.gaynor@...il.com>,
Boqun Feng <boqun.feng@...il.com>, Gary Guo <gary@...yguo.net>,
Björn Roy Baron <bjorn3_gh@...tonmail.com>,
Andreas Hindborg <a.hindborg@...nel.org>, Alice Ryhl <aliceryhl@...gle.com>,
Trevor Gross <tmgross@...ch.edu>, Andrew Morton <akpm@...ux-foundation.org>,
Dennis Zhou <dennis@...nel.org>, Tejun Heo <tj@...nel.org>,
Christoph Lameter <cl@...ux.com>, Danilo Krummrich <dakr@...nel.org>,
Benno Lossin <lossin@...nel.org>
Cc: linux-kernel@...r.kernel.org, rust-for-linux@...r.kernel.org,
linux-mm@...ck.org, Mitchell Levy <levymitchell0@...il.com>
Subject: [PATCH 5/5] rust: percpu: cache per-CPU pointers in the dynamic
case
Currently, the creation of a `PerCpuNumeric` requires a memory read via
the `Arc` managing the dynamic allocation. While the compiler might be
clever enough to consolidate these reads in some cases, the read must
happen *somewhere*, which, when we're concerning ourselves with
individual instructions, is a very high burden.
Instead, cache the `PerCpuPointer` inside the `DynamicPerCpu` structure;
then, the `Arc` is used solely to manage the allocation.
Signed-off-by: Mitchell Levy <levymitchell0@...il.com>
---
rust/kernel/percpu.rs | 12 +++++++++---
rust/kernel/percpu/numeric.rs | 2 +-
2 files changed, 10 insertions(+), 4 deletions(-)
diff --git a/rust/kernel/percpu.rs b/rust/kernel/percpu.rs
index 9659bff2d889..ffcfe02124ab 100644
--- a/rust/kernel/percpu.rs
+++ b/rust/kernel/percpu.rs
@@ -23,7 +23,10 @@
/// Holds a dynamically-allocated per-CPU variable.
pub struct DynamicPerCpu<T> {
+ // INVARIANT: `ptr` is managed by `alloc` and the value of `ptr` does not change for the
+ // lifetime of `self`.
alloc: Arc<PerCpuAllocation<T>>,
+ ptr: PerCpuPtr<T>,
}
/// Holds a statically-allocated per-CPU variable.
@@ -199,9 +202,10 @@ impl<T> DynamicPerCpu<T> {
pub fn new(flags: Flags) -> Option<Self> {
let alloc: PerCpuAllocation<T> = PerCpuAllocation::new()?;
+ let ptr = alloc.0;
let arc = Arc::new(alloc, flags).ok()?;
- Some(Self { alloc: arc })
+ Some(Self { alloc: arc, ptr })
}
/// Wraps a `PerCpuAllocation<T>` in a `PerCpu<T>`
@@ -210,8 +214,9 @@ pub fn new(flags: Flags) -> Option<Self> {
/// * `alloc` - The allocation to use
/// * `flags` - The flags used to allocate an `Arc` that keeps track of the `PerCpuAllocation`.
pub fn new_from_allocation(alloc: PerCpuAllocation<T>, flags: Flags) -> Option<Self> {
+ let ptr = alloc.0;
let arc = Arc::new(alloc, flags).ok()?;
- Some(Self { alloc: arc })
+ Some(Self { alloc: arc, ptr })
}
}
@@ -219,7 +224,7 @@ pub fn new_from_allocation(alloc: PerCpuAllocation<T>, flags: Flags) -> Option<S
// don't deallocate the underlying `PerCpuAllocation` until `self` is dropped.
unsafe impl<T> PerCpu<T> for DynamicPerCpu<T> {
unsafe fn ptr(&mut self) -> &PerCpuPtr<T> {
- &self.alloc.0
+ &self.ptr
}
}
@@ -227,6 +232,7 @@ impl<T> Clone for DynamicPerCpu<T> {
fn clone(&self) -> Self {
Self {
alloc: self.alloc.clone(),
+ ptr: self.ptr,
}
}
}
diff --git a/rust/kernel/percpu/numeric.rs b/rust/kernel/percpu/numeric.rs
index e4008f872af1..1b37cc7e5c19 100644
--- a/rust/kernel/percpu/numeric.rs
+++ b/rust/kernel/percpu/numeric.rs
@@ -62,7 +62,7 @@ impl DynamicPerCpu<$ty> {
/// Returns a `PerCpuNumeric` that can be used to manipulate the underlying per-CPU
/// variable.
pub fn num(&self) -> PerCpuNumeric<'_, $ty> {
- PerCpuNumeric { ptr: &self.alloc.0 }
+ PerCpuNumeric { ptr: &self.ptr }
}
}
impl StaticPerCpu<$ty> {
--
2.34.1
Powered by blists - more mailing lists