From nobody Sat Apr 11 04:50:31 2026 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 B1B273DC4A7 for ; Fri, 10 Apr 2026 21:36:29 +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=1775856991; cv=none; b=U5462ie23ASVkkHtv5fc9ODJdPBIlfP2+6vXHJpfRiMnBvL69GGocF1hqbG0doDV/XN1eaBqXjUcvTOY6nO8FNW2seVGWCCtRFHyGBT5irFr1z1LrdC39gItf5Xwx0pSZdjtxj3Nyt2eYkno/am3frxcjX99tSh4u/a+wwQRFgE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775856991; c=relaxed/simple; bh=FHBR4Wnqy/ydHjiZKFZpGeMrvO4ZgH/nNhnHVQU7j4U=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=H5anK4TCE1LFfrvCD653U7SXWLG/k0rM63y69iIeqHERMfToVGRM71wdjP2lb45Gyr1jV6UM03I43GylY6fFzuMsrOxkxduXditRk0j+fQX6s4g/WRsOn2prPmphJYRVonUHuThQXv9tsFeqAfgqnoaHyyFpkpvN9XKNk0cBMYc= 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=A/9eqKRy; arc=none smtp.client-ip=209.85.210.175 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="A/9eqKRy" Received: by mail-pf1-f175.google.com with SMTP id d2e1a72fcca58-82cdb4ab547so1339779b3a.2 for ; Fri, 10 Apr 2026 14:36:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1775856989; x=1776461789; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=1kY8tNpZZWpISJZ66yxQbfZrkvSCDqL724L2ETykFfw=; b=A/9eqKRyDCSXzrefsdDxmEQ6XVGgvwrdyCdJqpEsR3AzQDUmce7MMeSJLtLVZG7GCb lpA2tAh3MxquIMuz/+E4j7Fp8wXe4TgPVLRVCi3LI5WDmTny7ZIWMlzqBtRu7beWArZi +yjCht1GSerzTygfXGWYNp+fZw/ZecfMbzy6xiYOH/XDDOQwdwcwbBAOu4YAzukkOBeM 2iPYPlYuew8eQ/WuAZn29D0TL7qB5aevChSbWnA9WhERdWFmWuVtSFUtS7ZRv4Rs+Ezs netD+X+yPyMhyAh1lRKsHGl3Kb0E/A4SYgYf6jciYtrhWClr3g5K9PrP1b41AgFKcWf7 g3Ig== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1775856989; x=1776461789; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=1kY8tNpZZWpISJZ66yxQbfZrkvSCDqL724L2ETykFfw=; b=rTY9YYT0CAOvEnOMSAqpOvSzn1koEzJiS4W/U7jXl21lmRX8cfWws3Br3odIE1tCty UtpWlsUFjWDK+/+cFALX+RFokP3amFxwQWNDXv8wqiWR9Dxj3lzh7RJs4aL+QOVR/8a4 q9bCPoARGPnnExmVrGLltzVQw5my8tBjAl2kOjt+FvKPrqG+gkrfVNplAKm6Amw5CX0J pUdtReSKwRC0QFeIO/c3V4jQqtRsJRrfyCMlLJmSOHbWMxoufKOpkUHux8ZYx/Ppu+6u nnqjJXE2dRvYpcVSVjgMB8t72kJNq0RnFvpJr7IhUU5wwtACK+L8BD2ZPjbwYrxUOkb8 7jQw== X-Forwarded-Encrypted: i=1; AJvYcCUsz9MjR8ql7n8Vq/8TBQ+Z7X9IQj+ubsD9qmynP3G13VSJiC8ETXunennPmR6NwCQQU/UErdCU6dIDsoE=@vger.kernel.org X-Gm-Message-State: AOJu0YyPYPBDHZleQ5E2hfxTsCDWk8rj0a6a5S00dAV/d2Y+lS3c/M8T g1QvjNXblH/IbDG8HQEeS/vyQAORzjgXgcoheAufrK/3+umeonEVWIo6 X-Gm-Gg: AeBDieukSWTT2OXCHYtHVkepJzkHrJQciyL9LIAEvMmFyz12uIuqhr2O9cwgS3wiCPa yqQd5q3y70W9PjjG3sisAnXeor9IP3HBf80RQ3IkPEjgLe7tI0ca+dXHCYlvSdMOnBVhTzjpXff sMKiCI6dPET+6pAIjRBJR/4Yfd1iBHupIcDSrKwuOBhHTYCW9RMDKXnTmtqGvMj4u1orTbwW4eP I9RWoB8nHnAKBypwjMNSrNhQUOBG/IAVPCXBlqvpEjTPHyj5wy/4Tv3O5MSxkY8nf5fCRx+BWwE FnqYCBnlbbuZqruCu/tMB05Hqx9VTROx0qimEIh0+/0IZvu2H833hK13hNM7U2pxZ6iEqc48oqD ZWZ4jaGlKcJH1KseroIvAmlIkFyaAcZ2VTOAThr4Hcs9rYZJvIoUJjcBKjTPeTdQeY6ZPfi5reB 0r35Z71SRTM9L6vry8s5KIXNfqOX2LgqG0Q1rPs5jBRmaD X-Received: by 2002:a05:6a00:1827:b0:82c:f041:765d with SMTP id d2e1a72fcca58-82f0c2a1cccmr5690404b3a.16.1775856988978; Fri, 10 Apr 2026 14:36:28 -0700 (PDT) Received: from mitchelllevy.localdomain ([131.107.1.135]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-82f0c4e3d41sm5111551b3a.48.2026.04.10.14.36.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Apr 2026 14:36:28 -0700 (PDT) From: Mitchell Levy Date: Fri, 10 Apr 2026 14:35:38 -0700 Subject: [PATCH v5 8/8] rust: percpu: cache per-CPU pointers in the dynamic case Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260410-rust-percpu-v5-8-4292380d7a41@gmail.com> References: <20260410-rust-percpu-v5-0-4292380d7a41@gmail.com> In-Reply-To: <20260410-rust-percpu-v5-0-4292380d7a41@gmail.com> To: Miguel Ojeda , Alex Gaynor , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Andreas Hindborg , Alice Ryhl , Trevor Gross , Andrew Morton , Dennis Zhou , Tejun Heo , Christoph Lameter , Danilo Krummrich , Benno Lossin , Yury Norov , Viresh Kumar , Boqun Feng Cc: Tyler Hicks , Allen Pais , linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-mm@kvack.org, Mitchell Levy X-Mailer: b4 0.15.1 X-Developer-Signature: v=1; a=ed25519-sha256; t=1775856978; l=5201; i=levymitchell0@gmail.com; s=20240719; h=from:subject:message-id; bh=FHBR4Wnqy/ydHjiZKFZpGeMrvO4ZgH/nNhnHVQU7j4U=; b=ogoO04gAaNoTx75+C8BHdZL3inRGIgFDQvjKAC93cMw2HAcpU+LGo/rKWNrCe0NdbiWcyOwiV B35F+4kaHNtDVfLlWS+JakD7AUAmkILOvgE7I4iFqEvMzB6fhBKWQAz X-Developer-Key: i=levymitchell0@gmail.com; a=ed25519; pk=n6kBmUnb+UNmjVkTnDwrLwTJAEKUfs2e8E+MFPZI93E= 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. We might as well also use this speed-up in the standard `get` and `get_mut` accessors that give a `{Checked,}PerCpuToken` Signed-off-by: Mitchell Levy --- rust/kernel/percpu/dynamic.rs | 34 ++++++++++++++++++---------------- rust/kernel/percpu/numeric.rs | 4 ++-- 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/rust/kernel/percpu/dynamic.rs b/rust/kernel/percpu/dynamic.rs index a717138b93dc..675210098ed8 100644 --- a/rust/kernel/percpu/dynamic.rs +++ b/rust/kernel/percpu/dynamic.rs @@ -101,6 +101,9 @@ pub struct DynamicPerCpu { // INVARIANT: The memory location in each CPU's per-CPU area pointed a= t by the alloc is // initialized. alloc: Option>>, + // INVARIANT: `ptr` is the per-CPU pointer managed by `alloc`, which d= oes not change for the + // lifetime of `self`. + pub(super) ptr: PerCpuPtr, } =20 impl DynamicPerCpu { @@ -112,9 +115,13 @@ impl DynamicPerCpu { pub fn new_zero(flags: Flags) -> Option { let alloc: PerCpuAllocation =3D PerCpuAllocation::new_zero()?; =20 + let ptr =3D alloc.0; let arc =3D Arc::new(alloc, flags).ok()?; =20 - Some(Self { alloc: Some(arc) }) + Some(Self { + alloc: Some(arc), + ptr, + }) } } =20 @@ -158,15 +165,10 @@ pub fn new_from(mut initer: impl FnMut(CpuId) -> T, f= lags: Flags) -> Option 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 `Som= e`. - unsafe { self.alloc.as_ref().unwrap_unchecked() } + Some(Self { + alloc: Some(arc), + ptr, + }) } } =20 @@ -178,11 +180,11 @@ unsafe fn get_mut(&mut self, guard: CpuGuard) -> PerC= puToken<'_, T> { // exists on the current CPU. // 3. The invariants of `DynamicPerCpu` ensure that the contents o= f the allocation are // initialized on each CPU. - // 4. The existence of a reference to the `PerCpuAllocation` ensur= es that the allocation is - // live. + // 4. `&mut self` holds a reference to the `PerCpuAllocation`, so = the allocation it manages + // is live. // 5. The invariants of `DynamicPerCpu` ensure that the allocation= is sized and aligned for // a `T`. - unsafe { PerCpuToken::new(guard, &self.alloc.as_ref().unwrap_unche= cked().0) } + unsafe { PerCpuToken::new(guard, &self.ptr) } } } =20 @@ -192,11 +194,11 @@ fn get(&self, guard: CpuGuard) -> CheckedPerCpuToken<= '_, T> { // 1. Invariants of this type assure that `alloc` is `Some`. // 2. The invariants of `DynamicPerCpu` ensure that the contents o= f the allocation are // initialized on each CPU. - // 3. The existence of a reference to the `PerCpuAllocation` ensur= es that the allocation is - // live. + // 3. `&mut self` holds a reference to the `PerCpuAllocation`, so = the allocation it manages + // is live. // 4. The invariants of `DynamicPerCpu` ensure that the allocation= is sized and aligned for // a `T`. - unsafe { CheckedPerCpuToken::new(guard, &self.alloc.as_ref().unwra= p_unchecked().0) } + unsafe { CheckedPerCpuToken::new(guard, &self.ptr) } } } =20 diff --git a/rust/kernel/percpu/numeric.rs b/rust/kernel/percpu/numeric.rs index 13b4ab4a794d..4841843b05f7 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 in= variant 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 in= variant guarantees that // this pointer is valid and initialized on all CPUs. - PerCpuNumeric { ptr: &self.alloc().0 } + PerCpuNumeric { ptr: &self.ptr } } } impl StaticPerCpu<$ty> { --=20 2.34.1