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 --- rust/kernel/percpu/dynamic.rs | 22 ++++++++++++---------- rust/kernel/percpu/numeric.rs | 4 ++-- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/rust/kernel/percpu/dynamic.rs b/rust/kernel/percpu/dynamic.rs index 99acbf6363f5..dcf3e1c4f7a9 100644 --- a/rust/kernel/percpu/dynamic.rs +++ b/rust/kernel/percpu/dynamic.rs @@ -72,6 +72,9 @@ pub struct DynamicPerCpu { // INVARIANT: The memory location in each CPU's per-CPU area pointed at by the alloc is // initialized. alloc: Option>>, + // INVARIANT: `ptr` is the per-CPU pointer managed by `alloc`, which does not change for the + // lifetime of `self`. + pub(super) ptr: PerCpuPtr, } impl DynamicPerCpu { @@ -83,9 +86,13 @@ impl DynamicPerCpu { pub fn new_zero(flags: Flags) -> Option { let alloc: PerCpuAllocation = PerCpuAllocation::new_zero()?; + let ptr = alloc.0; let arc = Arc::new(alloc, flags).ok()?; - Some(Self { alloc: Some(arc) }) + Some(Self { + alloc: Some(arc), + ptr, + }) } } @@ -115,15 +122,10 @@ pub fn new_with(val: &T, flags: Flags) -> Option { let arc = Arc::new(alloc, flags).ok()?; - Some(Self { alloc: Some(arc) }) - } -} - -impl DynamicPerCpu { - /// Gets the allocation backing this per-CPU variable. - pub(crate) fn alloc(&self) -> &Arc> { - // SAFETY: This type's invariant ensures that `self.alloc` is `Some`. - unsafe { self.alloc.as_ref().unwrap_unchecked() } + Some(Self { + alloc: Some(arc), + ptr, + }) } } diff --git a/rust/kernel/percpu/numeric.rs b/rust/kernel/percpu/numeric.rs index e76461f05c66..23a7a09216d0 100644 --- a/rust/kernel/percpu/numeric.rs +++ b/rust/kernel/percpu/numeric.rs @@ -22,7 +22,7 @@ impl DynamicPerCpu<$ty> { pub fn num(&mut self) -> PerCpuNumeric<'_, $ty> { // The invariant is satisfied because `DynamicPerCpu`'s invariant guarantees that // this pointer is valid and initialized on all CPUs. - PerCpuNumeric { ptr: &self.alloc().0 } + PerCpuNumeric { ptr: &self.ptr } } } impl StaticPerCpu<$ty> { @@ -78,7 +78,7 @@ impl DynamicPerCpu<$ty> { pub fn num(&mut self) -> PerCpuNumeric<'_, $ty> { // The invariant is satisfied because `DynamicPerCpu`'s invariant guarantees that // this pointer is valid and initialized on all CPUs. - PerCpuNumeric { ptr: &self.alloc().0 } + PerCpuNumeric { ptr: &self.ptr } } } impl StaticPerCpu<$ty> { -- 2.34.1