From nobody Sat Apr 11 04:50:31 2026 Received: from mail-pg1-f177.google.com (mail-pg1-f177.google.com [209.85.215.177]) (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 DCC2C3DB65F for ; Fri, 10 Apr 2026 21:36:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.177 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775856990; cv=none; b=jrIwgvAPLeleIf8g1i3XnFo0UzYVB5rfQTKE2GeSGkb7OGW1smR80P1U/8fETSszzYuMDOcrNjYkcqQVeTEUrsrfGbZW8YVp/urqCnBRYoL11BaM1aFpdZIpp+9hSdyexyZA5i85PBEQhqIC6AO3pAaWLkXh+eiWbxOFa1tdEZI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775856990; c=relaxed/simple; bh=IyeQBCcNFTW60JZl2i8GXrsguuamIFDOED+ecW44t6k=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=dRJ/gTz6DtWQwH+qrrjHRHgmQ5QqP+UQFEgEB2uvwYtapxnIEGmKFEJjj02Tm6LHLxcYCRfwQ6neD6JTB3IXjtqlV1E+O2rKmuCpDQxm139xVc3bOX4CUllwLDrFghGRhNI5OMiTY0OVVzGvBuHd8DzdbxW6aJMebA6VBj9JYvc= 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=tDz5wGIv; arc=none smtp.client-ip=209.85.215.177 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="tDz5wGIv" Received: by mail-pg1-f177.google.com with SMTP id 41be03b00d2f7-c76e702e01aso893100a12.3 for ; Fri, 10 Apr 2026 14:36:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1775856988; x=1776461788; 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=+JzMtJCfhE/cOyRCL4deN82RoA6oSUseqvroceFE1d8=; b=tDz5wGIvoS7Vswqgc5VP3oVtqwBcqJ1zz0Z72nuIczcbg8l17HxtOFKyGnjhVgpY1U J8NKVLP93VFTGVebqUDdzTVgkGk6kcCBkvILVT2fU8MRihS7Et66xmKhHAaqNtroSqua Nu89omNpi7D2bwIWmtTWBLKyuhI38I1t+rxy9ttb/RgkL/YvVHRDF75P2UT2zV/49v3M tRwslg4qgRFbKi5O874S5GD2O6blceu+QU844b9MemiPskXlH5MpWnZQ1jIOXLbBECe1 HD0RXUBVHhMgz+NCj2E0GNlIn/rN2kKggnxIHV8FAqmvEwzkOdrcPO93bXr/M6gEOBu0 fP4Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1775856988; x=1776461788; 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=+JzMtJCfhE/cOyRCL4deN82RoA6oSUseqvroceFE1d8=; b=cBlRjbp9uV/X3GEeng6/iB4Gk0fBBFJVWlBVTcL0CHYvh3LwkkYY8EoZaOsTL/kFxC 8JiHbUXZ0XIGuXIabC2YkL5Q7U8FMqaZocuKw5F2jsMEC7PFcWMeNaAtEDdIjxVTrzUK jH22C7lxd5OILFu7dUAqrfrK9FaRVmTwFPI1fQxizn6RITlpLK/22FOfUX42pSpDlOjo Li3ApEtOXKTROOQNWpjVvCnuxIB8hlo53f4JTI25nU9f6UXfn4Nlq4xBTalKnKyQDBdL stLOkLomicLO5npLeEIV2PW4PGZCdfb5tQMlyOPHAR01b8bRpajgtfxc0hKQ80B6U19t C3VQ== X-Forwarded-Encrypted: i=1; AJvYcCUh8oNuDspnzGqkQ5qsOOyhx4h9cb691zSFjjCU366GO9NnzBDAOdF0u1yglS9z2Uwzox5/cxCrp3qf98M=@vger.kernel.org X-Gm-Message-State: AOJu0YySGZO18IdFJtYvYdFoXZwxAWMVSThjR+xLChuAcVs6VSsudR9b 8+Zgkd2r6TQ7YrKURiQ4aeF0x7cS6MtAFfajWZp9k+/2gcIyn3Mj/eHB X-Gm-Gg: AeBDietRkeBIVr9Ri9t4JzQUGrjymAQu87zK8oEItJGLVpnRR1l3Z2xD8yWLzsKk5hi a37ftuyZyB7W/A046DVmly04TfY0fUKLoGHu4irGXlGBR9Opxr0FaCtqa1Y9LUoPTjmBYVOgrYB RCS7yG5Qq6gkCZS32s2688a/DXzwQfWVihRvEZ3+7bxwaKwz1S4S2053GtQJELWj5Q+WdOJqADn QLJn67PsQPeLNxMDxEtRW3TjpKKlT7mDbl70Xw60wDcrdgYnLoZiVI45UW6nJtD54raFTiDtSKL Y7loKY0yLwPwtuT2K9IXcry617/8NvxEUb6TqC3MhymUwqz1p65RbqMsHBwrKQsADmBnCSv9Ry7 b6yDhAdM5FbL0vdefiM+mHB2ETDst5guLLu4duJPZRPLWsviUDkZ+Ke4FLK52CRJI5OG5ZttXxT W4HuBu4vbQ4oFpWWJmuLerlK3CqvmGmkOTIXrtZZGC87Ok/aodhrN4Aok= X-Received: by 2002:a05:6a20:2588:b0:398:a5c7:3dc9 with SMTP id adf61e73a8af0-39fe405dd2amr5667454637.49.1775856988108; 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.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Apr 2026 14:36:27 -0700 (PDT) From: Mitchell Levy Date: Fri, 10 Apr 2026 14:35:37 -0700 Subject: [PATCH v5 7/8] rust: percpu: Add pin-hole optimizations for numerics 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-7-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=10544; i=levymitchell0@gmail.com; s=20240719; h=from:subject:message-id; bh=IyeQBCcNFTW60JZl2i8GXrsguuamIFDOED+ecW44t6k=; b=1sEnwXSt8TG44DrXsJ2xdTtZ9Hvqtj8T/1xbiPRQKEcLDvTwN0VJ1fuUJa/x5cbQVfQfLhG1a 2UllwHEsDBTB8lfz/5JZy2p3OjK9kiS+VnYge4/oR35vnsTFhL0mRoI X-Developer-Key: i=levymitchell0@gmail.com; a=ed25519; pk=n6kBmUnb+UNmjVkTnDwrLwTJAEKUfs2e8E+MFPZI93E= The C implementations of `this_cpu_add`, `this_cpu_sub`, etc., are optimized to save an instruction by avoiding having to compute `this_cpu_ptr(&x)` for some per-CPU variable `x`. For example, rather than u64 *x_ptr =3D this_cpu_ptr(&x); *x_ptr +=3D 5; the implementation of `this_cpu_add` is clever enough to make use of the fact that per-CPU variables are implemented on x86 via segment registers, and so we can use only a single instruction (where we assume `&x` is already in `rax`) add gs:[rax], 5 Add this optimization via a `PerCpuNumeric` type to enable code-reuse between `DynamicPerCpu` and `StaticPerCpu`. Signed-off-by: Mitchell Levy --- rust/kernel/percpu.rs | 1 + rust/kernel/percpu/dynamic.rs | 10 ++- rust/kernel/percpu/numeric.rs | 138 ++++++++++++++++++++++++++++++++++++++= ++++ samples/rust/rust_percpu.rs | 36 +++++++++++ 4 files changed, 184 insertions(+), 1 deletion(-) diff --git a/rust/kernel/percpu.rs b/rust/kernel/percpu.rs index 72c83fef68ee..ff04607ee047 100644 --- a/rust/kernel/percpu.rs +++ b/rust/kernel/percpu.rs @@ -6,6 +6,7 @@ =20 pub mod cpu_guard; mod dynamic; +pub mod numeric; mod static_; =20 #[doc(inline)] diff --git a/rust/kernel/percpu/dynamic.rs b/rust/kernel/percpu/dynamic.rs index 40514704b3d0..a717138b93dc 100644 --- a/rust/kernel/percpu/dynamic.rs +++ b/rust/kernel/percpu/dynamic.rs @@ -28,7 +28,7 @@ /// the memory location on any particular CPU has been initialized. This m= eans that it cannot tell /// whether it should drop the *contents* of the allocation when it is dro= pped. It is up to the /// user to do this via something like [`core::ptr::drop_in_place`]. -pub struct PerCpuAllocation(PerCpuPtr); +pub struct PerCpuAllocation(pub(super) PerCpuPtr); =20 impl PerCpuAllocation { /// Dynamically allocates a space in the per-CPU area suitably sized a= nd aligned to hold a `T`, @@ -162,6 +162,14 @@ pub fn new_from(mut initer: impl FnMut(CpuId) -> T, fl= ags: 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() } + } +} + impl PerCpu for DynamicPerCpu { unsafe fn get_mut(&mut self, guard: CpuGuard) -> PerCpuToken<'_, T> { // SAFETY: diff --git a/rust/kernel/percpu/numeric.rs b/rust/kernel/percpu/numeric.rs new file mode 100644 index 000000000000..13b4ab4a794d --- /dev/null +++ b/rust/kernel/percpu/numeric.rs @@ -0,0 +1,138 @@ +// SPDX-License-Identifier: GPL-2.0 +//! Pin-hole optimizations for [`PerCpu`] where T is a numeric type. + +use super::*; +use core::arch::asm; + +/// Represents a per-CPU variable that can be manipulated with machine-int= rinsic numeric +/// operations. +pub struct PerCpuNumeric<'a, T> { + // INVARIANT: `ptr.0` is a valid offset into the per-CPU area and is i= nitialized on all CPUs + // (since we don't have a CPU guard, we have to be pessimistic and ass= ume we could be on any + // CPU). + ptr: &'a PerCpuPtr, +} + +macro_rules! impl_ops { + ($ty:ty, $reg:tt) =3D> { + impl DynamicPerCpu<$ty> { + /// Returns a [`PerCpuNumeric`] that can be used to manipulate= the underlying per-CPU + /// variable. + #[inline] + 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 } + } + } + impl StaticPerCpu<$ty> { + /// Returns a [`PerCpuNumeric`] that can be used to manipulate= the underlying per-CPU + /// variable. + #[inline] + pub fn num(&mut self) -> PerCpuNumeric<'_, $ty> { + // The invariant is satisfied because `StaticPerCpu`'s inv= ariant guarantees that + // this pointer is valid and initialized on all CPUs. + PerCpuNumeric { ptr: &self.0 } + } + } + + impl PerCpuNumeric<'_, $ty> { + /// Adds `rhs` to the per-CPU variable. + #[inline] + pub fn add(&mut self, rhs: $ty) { + // SAFETY: `self.ptr.0` is a valid offset into the per-CPU= area (i.e., valid as a + // pointer relative to the `gs` segment register) by the i= nvariants of this type. + unsafe { + asm!( + concat!("add gs:[{off}], {val:", $reg, "}"), + off =3D in(reg) self.ptr.0.cast::<$ty>(), + val =3D in(reg) rhs, + ); + } + } + } + impl PerCpuNumeric<'_, $ty> { + /// Subtracts `rhs` from the per-CPU variable. + #[inline] + pub fn sub(&mut self, rhs: $ty) { + // SAFETY: `self.ptr.0` is a valid offset into the per-CPU= area (i.e., valid as a + // pointer relative to the `gs` segment register) by the i= nvariants of this type. + unsafe { + asm!( + concat!("sub gs:[{off}], {val:", $reg, "}"), + off =3D in(reg) self.ptr.0.cast::<$ty>(), + val =3D in(reg) rhs, + ); + } + } + } + }; +} + +macro_rules! impl_ops_byte { + ($ty:ty) =3D> { + impl DynamicPerCpu<$ty> { + /// Returns a [`PerCpuNumeric`] that can be used to manipulate= the underlying per-CPU + /// variable. + #[inline] + 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 } + } + } + impl StaticPerCpu<$ty> { + /// Returns a [`PerCpuNumeric`] that can be used to manipulate= the underlying per-CPU + /// variable. + #[inline] + pub fn num(&mut self) -> PerCpuNumeric<'_, $ty> { + // The invariant is satisfied because `StaticPerCpu`'s inv= ariant guarantees that + // this pointer is valid and initialized on all CPUs. + PerCpuNumeric { ptr: &self.0 } + } + } + + impl PerCpuNumeric<'_, $ty> { + /// Adds `rhs` to the per-CPU variable. + #[inline] + pub fn add(&mut self, rhs: $ty) { + // SAFETY: `self.ptr.0` is a valid offset into the per-CPU= area (i.e., valid as a + // pointer relative to the `gs` segment register) by the i= nvariants of this type. + unsafe { + asm!( + "add gs:[{off}], {val}", + off =3D in(reg) self.ptr.0.cast::<$ty>(), + val =3D in(reg_byte) rhs, + ); + } + } + } + impl PerCpuNumeric<'_, $ty> { + /// Subtracts `rhs` from the per-CPU variable. + #[inline] + pub fn sub(&mut self, rhs: $ty) { + // SAFETY: `self.ptr.0` is a valid offset into the per-CPU= area (i.e., valid as a + // pointer relative to the `gs` segment register) by the i= nvariants of this type. + unsafe { + asm!( + "sub gs:[{off}], {val}", + off =3D in(reg) self.ptr.0.cast::<$ty>(), + val =3D in(reg_byte) rhs, + ); + } + } + } + }; +} + +impl_ops_byte!(i8); +impl_ops!(i16, "x"); +impl_ops!(i32, "e"); +impl_ops!(i64, "r"); +impl_ops!(isize, "r"); + +impl_ops_byte!(u8); +impl_ops!(u16, "x"); +impl_ops!(u32, "e"); +impl_ops!(u64, "r"); +impl_ops!(usize, "r"); diff --git a/samples/rust/rust_percpu.rs b/samples/rust/rust_percpu.rs index 5adb30509bd4..90f5debd3c7a 100644 --- a/samples/rust/rust_percpu.rs +++ b/samples/rust/rust_percpu.rs @@ -28,6 +28,26 @@ define_per_cpu!(UPERCPU: u64 =3D 0); define_per_cpu!(CHECKED: RefCell =3D RefCell::new(0)); =20 +macro_rules! make_optimization_test { + ($ty:ty) =3D> { + let mut test: DynamicPerCpu<$ty> =3D DynamicPerCpu::new_zero(GFP_K= ERNEL).unwrap(); + { + let _guard =3D CpuGuard::new(); + // SAFETY: No other usage of `test` + unsafe { test.get_mut(CpuGuard::new()) }.with(|val: &mut $ty| = *val =3D 10); + test.num().add(1); + // SAFETY: No other usage of `test` + unsafe { test.get_mut(CpuGuard::new()) }.with(|val: &mut $ty| = assert_eq!(*val, 11)); + test.num().add(10); + // SAFETY: No other usage of `test` + unsafe { test.get_mut(CpuGuard::new()) }.with(|val: &mut $ty| = assert_eq!(*val, 21)); + test.num().sub(5); + // SAFETY: No other usage of `test` + unsafe { test.get_mut(CpuGuard::new()) }.with(|val: &mut $ty| = assert_eq!(*val, 16)); + } + }; +} + impl kernel::Module for PerCpuMod { fn init(_module: &'static ThisModule) -> Result { pr_info!("rust percpu test start\n"); @@ -228,6 +248,22 @@ fn init(_module: &'static ThisModule) -> Result { =20 pr_info!("rust dynamic percpu test done\n"); =20 + pr_info!("rust numeric optimizations test start\n"); + + make_optimization_test!(u8); + make_optimization_test!(u16); + make_optimization_test!(u32); + make_optimization_test!(u64); + make_optimization_test!(usize); + + make_optimization_test!(i8); + make_optimization_test!(i16); + make_optimization_test!(i32); + make_optimization_test!(i64); + make_optimization_test!(isize); + + pr_info!("rust numeric optimizations test done\n"); + // Return Err to unload the module Result::Err(EINVAL) } --=20 2.34.1