From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id C9528C77B7F for ; Tue, 24 Jun 2025 22:11:49 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8953C6B00AE; Tue, 24 Jun 2025 18:11:45 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 86D486B00AF; Tue, 24 Jun 2025 18:11:45 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 735E96B00B1; Tue, 24 Jun 2025 18:11:45 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 597A66B00AE for ; Tue, 24 Jun 2025 18:11:45 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id CACE11D90CB for ; Tue, 24 Jun 2025 22:11:44 +0000 (UTC) X-FDA: 83591692128.10.29A5156 Received: from mail-pl1-f180.google.com (mail-pl1-f180.google.com [209.85.214.180]) by imf08.hostedemail.com (Postfix) with ESMTP id BBA3B160006 for ; Tue, 24 Jun 2025 22:11:42 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=YfEkuxqg; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf08.hostedemail.com: domain of levymitchell0@gmail.com designates 209.85.214.180 as permitted sender) smtp.mailfrom=levymitchell0@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1750803102; a=rsa-sha256; cv=none; b=WGFhOzDrVYShbzqA97FcKI9f3ybhAH5B5tTok+E/M3bvfc5rxopU8pbn+XjCv5RIDEIKJ9 FpUNDuUQFvjoqpiRY0LWyITYHtbF47QjNXStPMSBGWV/zNtFgfPnwQkfT7HBUxBLEvNXII dpjri/OUp37680UtiRPp/aPIbe5o/7U= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=YfEkuxqg; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf08.hostedemail.com: domain of levymitchell0@gmail.com designates 209.85.214.180 as permitted sender) smtp.mailfrom=levymitchell0@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1750803102; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=MZlCvx1CGiBR7gpIxHEH/qkJGdg3V+G7iQKh210683E=; b=R4SizwlUHz7uXPvb5iaWm4+J9LXH5HdwscrzNUbBcDj0K0dc6KIP1T/5bKO1zs4WYwf3Wa gSoFG2kbLdrB8pR9VTbfTQIIUbM0PIgRqL2AWluRpNxcm28mBV5E1QWqndOqNZhIQchVkL w/BXBJ0OnpCQbgLP3DzAy8iQq6/zBck= Received: by mail-pl1-f180.google.com with SMTP id d9443c01a7336-23633a6ac50so15992185ad.2 for ; Tue, 24 Jun 2025 15:11:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1750803101; x=1751407901; darn=kvack.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=MZlCvx1CGiBR7gpIxHEH/qkJGdg3V+G7iQKh210683E=; b=YfEkuxqgSlxHpZihH/Hy8A5bRYilykBy+xZRvMpLOw7qSr+mPBd7fdtkYuMl+bWsgs r+Tu/rdAIFUombXjBCa+AB8V2tDEqlekThvBiDfzPZGOkAUd3Iu4zGLd8bmcEyBp+wkY A2FnhTjIqW618lN/yCrXP5sKX6TGGETUOovOKxG3oziekPQA7j5ze5KWNSmiAulpUe5Y deg0Fw1hY8mjQe54CIFPVKplO+ejMr1kupwOs8OFC4BR5zjZ+nPGNp4MEwnI4YIW3DAC qBuwtaHNnUZMsm0wYqJys+aoUx+DAGT9eviBjYwls8JrQPJegUYgUK7C0IbQ5EkBz9+1 enLA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1750803101; x=1751407901; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=MZlCvx1CGiBR7gpIxHEH/qkJGdg3V+G7iQKh210683E=; b=dhexKzfpudjowZB1hDLpHOj35zHuEOEgkMCISbn8z8N/q0yDfF8H+ILDAn+reLjo3w pkn5zEL5ojLsw5s3khFOvoDcA1Opr8U/AvFvWUxYVur7SULdaQYnWdQi6XNX9Cp1EIdd Sr8aW3D04Ue0nbD4Yo887i+dtXdc1osk8YvprQEA+u+Peh0B0+nus0XQAaHQroNTz/kA EnHONwj/7swYLN3nWFwYhle4Z3xYo+0k68vzfhjZInAOvU6cq8S67YX5LOkfJ4dliyN5 xm13MjcTvHErcskrTG4fL/ajf1apzQbR7pEyAylMM3yiuv5AFhhE9ry9jbZEZHVIhpZX sRLA== X-Forwarded-Encrypted: i=1; AJvYcCXm3hHjqZI2CkVLIPYNdsji9DjAtjMglWTVDL9NxJ0zJWqh+NdHXX4pjsbf9a9VuHlaTUdg4cLNpg==@kvack.org X-Gm-Message-State: AOJu0YzGbyO4Iv/Ac50wD3/dD7DHpf9XG8NLCPilqJ5/s6VHMQxw/3f/ 184+i7Ye+pIbh1mfo5u2+Z5OO7035oz8q0bxU3Jxi1frM1CjJcS+H3l8 X-Gm-Gg: ASbGncvcHT8qXRg9hrgHZyJaV4gDT4D6rcJGPgAT2NRiZ4KSyOlY365U+kLdP74cOqG mkGVSExTMYq1/gONgtkCRtKMjk7hDXB92kOvHe/8Wz6dt+KTOj9b/iRKqTU9a3xA8XB+DkCqbZO TNFycCYWYsIaucbBTlkmhUVZuvmIQI3tOb07i66JpQkLWX7zacRwuPr3OTgANcEjFiY1pbnAmVp Q5OvSBxH7VfTRTZVkBj2Nq7GuE4V9wo7/mSZGD+Yd3gYTn5L1dvaqekv864FUdvF3Cv/SvyaCHo hv6hZP+JawzjIjiBnNQVQ2fqVwFsOAh87dE7lSGdT1MUIoAvIJicnfuH3VGLsvWCX8GAZ8O3ySP fe3BgwKpqdVxScwrm+JORgBME9N15oX3+kI20 X-Google-Smtp-Source: AGHT+IEbmYtniKHMhkflwBj9MjSeO29aJCWX2mEvIk6KM9I9b4UHYOZVqVk9S+c5+SdXbRQY80QERQ== X-Received: by 2002:a17:903:198b:b0:233:ab04:27a with SMTP id d9443c01a7336-23824094ce2mr16400005ad.53.1750803101519; Tue, 24 Jun 2025 15:11:41 -0700 (PDT) Received: from mitchelllevy.localdomain (82.sub-174-224-205.myvzw.com. [174.224.205.82]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-237d8393741sm114580555ad.15.2025.06.24.15.11.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Jun 2025 15:11:41 -0700 (PDT) From: Mitchell Levy Date: Tue, 24 Jun 2025 15:10:42 -0700 Subject: [PATCH 4/5] rust: percpu: Add pin-hole optimizations for numerics MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20250624-rust-percpu-v1-4-9c59b07d2a9c@gmail.com> References: <20250624-rust-percpu-v1-0-9c59b07d2a9c@gmail.com> In-Reply-To: <20250624-rust-percpu-v1-0-9c59b07d2a9c@gmail.com> To: Miguel Ojeda , Alex Gaynor , Boqun Feng , 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 Cc: linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-mm@kvack.org, Mitchell Levy X-Mailer: b4 0.14.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1750803093; l=8125; i=levymitchell0@gmail.com; s=20240719; h=from:subject:message-id; bh=YPpQN+Ft5ii+z9LN9DxUYct6G7gy1L7BlJzPkvs+pa4=; b=72RLOn49lWvHD5dQo1lgVE6C04np3Nahgwl+XN/0/nxjN+3p+Qplvw1K9y82XK52dWg0C2OKr PpCyjPq8zViBVyeVjLEG/ASI20aH/vtoMvdllAXAaTrbYHwk+l5t3qu X-Developer-Key: i=levymitchell0@gmail.com; a=ed25519; pk=n6kBmUnb+UNmjVkTnDwrLwTJAEKUfs2e8E+MFPZI93E= X-Rspam-User: X-Rspamd-Queue-Id: BBA3B160006 X-Rspamd-Server: rspam10 X-Stat-Signature: iiztbb5yzewcsnb4t4mq6rddc7fpnymd X-HE-Tag: 1750803102-584859 X-HE-Meta: U2FsdGVkX1/zaor4dPWL+knWKIRoef4TvlfooTcxEfO/9uM1d5a2DH6rPlcIifc4E2yFRXc9hEZfqkdVFB5352DbdoB11lsqMTlbCfeMPjbCgxryIblIlnPeJnNVsKQYM/7vu2Y4MAsBYG/hr55LTAeD7bVz/3gzRP0dT9VUW7+Rl9IRN1tsE+n0gwhNq2179u6ISedxbbjHIyqBxU1iKVWlpf7IyQGthJYhxZ6HLkpv1dgeDjH9U5OHeP7QtXwyAT6FENDqmHqLU3a6zzaJA+/tGLicouGkxSCbdHShGwvvKGkcMRtvTgtF5kDxvlIdHrf/YdXdqTmFQBYwuSSDwPDPQ2teXdBE2KFbPWmP+enSCxbrD6jFKyNidNaLbh/oxEhPDDiruXL/Y1nPARS42GjTQaJ0zqYpZobO0Ziwv98i8FWKTlY08L5fnbbUUHj40h1KCi+/0PQJLdLkubXdIiBS/dHXsW2qTQ6XoGP0nN2rZZctfVAUCp2mzpakU8BsgJFQRr8Jcihlj12XEVyLMnoXrfTaMZmNaMB5B3d9ZEKOgZlQxvNQtJffLibykbn5mG+n5RXNNzC7bn5WcpJJHk52aDSCgie8jo0I3oyy5qxqu7kr7TWmckhVN27/kg/zrxKzDuDeO0J2XRS+163kxDuKG9sDq8CN8LpLfas2tt3Vt+PRNgzy6oZ0YG6u56S9GgiHvAR3BqWRhI0XjF+uTVh+yHDYiX9ETP/X1tFXsSqcvXW0XeZPWD52pcOIuxBsFmGItgaFF6Y0TGrVqDuuocYjbzcWJTivR/fZSOqlsOMZ+ovBv26boXGd/M5QGv+D2R1Cm1mqhj+bdnoccs9xeyGHGnalI57XdI+iEwrmCT81UiNFjzGQtI5Ybk/FtoBhZYJ8+VpifyWq/KVtclmYwPJxEAutGU7tbgvCdgJWYmqIqWDRYG7/6W/vFINCYDJUB35DyK3voOfewkoP/j7 Ua+CwGcS 3CKoQri1NPbMfUP68EDN6msoI6eU4ieNR43kQr0q+aFLIqUfcw7kQi0ZIUyvKOFPj3huZWPQ8//bzQE9BpAV0Q9SqPZs8tG/UxYTmRPA1IBKhcMyhLOCtOgwAfpIks3PjMCKLUrFBluGD5eGSuTYUMvSSGv4g8nQ5YzC1BaK7GjgbcraPbJ0CCSogH5GPdcS/gWxzps+9OHUxQhIbicKDwTpk8rfgiSCv8PU8HfHan/4Et3JZMYW4FN2YP/12olT1ADEGkXFBlPBIy8weTOpoTLZ+NKwK6g6J8u6jcaw2HWLJ7zC664rNuOY9vj3eYm1TkrgTWpp68yBMYKaX1hOApnTSXffHywBqT1pSZ38JKTBnBsGDdijZxmi9s4oqqfxNuwdetQDHGk941cVn1ikLEZX99rsbPO4c9QDSx5iHKvyN1Iw1BDIyOZLzzH8dJC94xvGi/UPzCdHg7Co= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: 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 = this_cpu_ptr(&x); *x_ptr += 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 --- lib/percpu_test_rust.rs | 36 +++++++++++++ rust/kernel/percpu.rs | 1 + rust/kernel/percpu/numeric.rs | 117 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 154 insertions(+) diff --git a/lib/percpu_test_rust.rs b/lib/percpu_test_rust.rs index a9652e6ece08..114015435a85 100644 --- a/lib/percpu_test_rust.rs +++ b/lib/percpu_test_rust.rs @@ -25,6 +25,26 @@ define_per_cpu!(PERCPU: i64 = 0); define_per_cpu!(UPERCPU: u64 = 0); +macro_rules! make_optimization_test { + ($ty:ty) => { + let mut test: DynamicPerCpu<$ty> = DynamicPerCpu::new(GFP_KERNEL).unwrap(); + { + let _ = CpuGuard::new(); + // SAFETY: No other usage of `test` + unsafe { test.get(CpuGuard::new()) }.with(|val: &mut $ty| *val = 10); + test.num().add(1); + // SAFETY: No other usage of `test` + unsafe { test.get(CpuGuard::new()) }.with(|val: &mut $ty| assert_eq!(*val, 11)); + test.num().add(10); + // SAFETY: No other usage of `test` + unsafe { test.get(CpuGuard::new()) }.with(|val: &mut $ty| assert_eq!(*val, 21)); + test.num().sub(5); + // SAFETY: No other usage of `test` + unsafe { test.get(CpuGuard::new()) }.with(|val: &mut $ty| assert_eq!(*val, 16)); + } + }; +} + impl kernel::Module for PerCpuTestModule { fn init(_module: &'static ThisModule) -> Result { pr_info!("rust percpu test start\n"); @@ -94,6 +114,22 @@ fn init(_module: &'static ThisModule) -> Result { pr_info!("rust dynamic percpu test done\n"); + 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) } diff --git a/rust/kernel/percpu.rs b/rust/kernel/percpu.rs index a912f74349e0..9659bff2d889 100644 --- a/rust/kernel/percpu.rs +++ b/rust/kernel/percpu.rs @@ -2,6 +2,7 @@ //! This module contains abstractions for creating and using per-CPU variables from Rust. //! See the define_per_cpu! macro and the PerCpu type. pub mod cpu_guard; +pub mod numeric; use bindings::{alloc_percpu, free_percpu}; diff --git a/rust/kernel/percpu/numeric.rs b/rust/kernel/percpu/numeric.rs new file mode 100644 index 000000000000..e4008f872af1 --- /dev/null +++ b/rust/kernel/percpu/numeric.rs @@ -0,0 +1,117 @@ +// SPDX-License-Identifier: GPL-2.0 +//! Pin-hole optimizations for PerCpu where T is a numeric type. + +use crate::percpu::*; +use core::arch::asm; + +/// Represents a per-CPU variable that can be manipulated with machine-intrinsic numeric +/// operations. +pub struct PerCpuNumeric<'a, T> { + ptr: &'a PerCpuPtr, +} + +macro_rules! impl_ops { + ($ty:ty, $reg:tt) => { + 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 } + } + } + impl StaticPerCpu<$ty> { + /// Returns a `PerCpuNumeric` that can be used to manipulate the underlying per-CPU variable. + pub fn num(&self) -> PerCpuNumeric<'_, $ty> { + PerCpuNumeric { ptr: &self.0 } + } + } + + impl PerCpuNumeric<'_, $ty> { + /// Adds `rhs` to the per-CPU variable. + 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 invariants of PerCpu. + unsafe { + asm!( + concat!("add gs:[{off}], {val:", $reg, "}"), + off = in(reg) self.ptr.0 as *mut $ty, + val = in(reg) rhs, + ); + } + } + } + impl PerCpuNumeric<'_, $ty> { + /// Subtracts `rhs` from the per-CPU variable. + 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 invariants of PerCpu. + unsafe { + asm!( + concat!("sub gs:[{off}], {val:", $reg, "}"), + off = in(reg) self.ptr.0 as *mut $ty, + val = in(reg) rhs, + ); + } + } + } + }; +} + +macro_rules! impl_ops_byte { + ($ty:ty) => { + 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 } + } + } + impl StaticPerCpu<$ty> { + /// Returns a `PerCpuNumeric` that can be used to manipulate the underlying per-CPU + /// variable. + pub fn num(&self) -> PerCpuNumeric<'_, $ty> { + PerCpuNumeric { ptr: &self.0 } + } + } + + impl PerCpuNumeric<'_, $ty> { + /// Adds `rhs` to the per-CPU variable. + 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 invariants of PerCpu. + unsafe { + asm!( + concat!("add gs:[{off}], {val}"), + off = in(reg) self.ptr.0 as *mut $ty, + val = in(reg_byte) rhs, + ); + } + } + } + impl PerCpuNumeric<'_, $ty> { + /// Subtracts `rhs` from the per-CPU variable. + 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 invariants of PerCpu. + unsafe { + asm!( + concat!("sub gs:[{off}], {val}"), + off = in(reg) self.ptr.0 as *mut $ty, + val = 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"); -- 2.34.1