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 A123DC83F1D for ; Sat, 12 Jul 2025 21:32:18 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6F2E26B00DE; Sat, 12 Jul 2025 17:32:12 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 67DA36B00DF; Sat, 12 Jul 2025 17:32:12 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5436C6B00E0; Sat, 12 Jul 2025 17:32:12 -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 3DDA36B00DE for ; Sat, 12 Jul 2025 17:32:12 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id DDDC01D80E3 for ; Sat, 12 Jul 2025 21:32:11 +0000 (UTC) X-FDA: 83656910862.14.5531AA5 Received: from mail-pj1-f49.google.com (mail-pj1-f49.google.com [209.85.216.49]) by imf20.hostedemail.com (Postfix) with ESMTP id 06BDB1C0012 for ; Sat, 12 Jul 2025 21:32:09 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="Ygb00y/H"; spf=pass (imf20.hostedemail.com: domain of levymitchell0@gmail.com designates 209.85.216.49 as permitted sender) smtp.mailfrom=levymitchell0@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1752355930; 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=7j+nFClWMYu+z4RxyaBNy1i8eSeqUJQPpZ3XTZUjlQs=; b=YajDbxluFO5TQyd/Lnp+jULq+FYTYJZUp3hfS1vY8qLltJ0EYuTfk1GQsn6FbR4JOQ0x+e LzilQrgjc8Er3mFpWUcLjDAQ0BCagf0a3SWQCavrYrl29tc2maPkWul8oAqhmntL2MC5wp 0k3qUQY2uRgBbeE/0IMooO7NBrrCuVU= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1752355930; a=rsa-sha256; cv=none; b=PTaVSbCgpCTNSBpfG6K/zCgztaNWkHNqKBVWX8G2rePYpWQ0EwoBH74e9a3PwFNGfkrzDt Mi4CtgTLkJYjXKojY25tMsWwrDUC/RaGrtblYzcMJeQsG9p48K+y77jqisvHnALAoJN59J Q4SVyle94ErhsTm6rDvZD9KPNSKir0I= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="Ygb00y/H"; spf=pass (imf20.hostedemail.com: domain of levymitchell0@gmail.com designates 209.85.216.49 as permitted sender) smtp.mailfrom=levymitchell0@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-pj1-f49.google.com with SMTP id 98e67ed59e1d1-313eeb77b1fso2719188a91.1 for ; Sat, 12 Jul 2025 14:32:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1752355929; x=1752960729; 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=7j+nFClWMYu+z4RxyaBNy1i8eSeqUJQPpZ3XTZUjlQs=; b=Ygb00y/HpiIOLL/2Et1VTZEhUuX3XtDz+wz1Y6LaKwyCHfV65YuVj6nuqVqb0QS0mC dZfqBesFnx/GYJnqvIJFsVXxasXBfZmjaYY/g/T1ExxMgrvEiyCdod/1GYj8XAv73YgH Tqw6GpE1+9YruGi5DyE1HfHAEr3SZaPYkH+pl4xjLsoaB+3ajdomlODXev85I6boPzVM mIi9/1M64ulpzFwm4pn8FHA6LLww8TtwsL6+VCCg8G7rwUfE8zOAiEl4dwNfXTVcenCp qkaVXNSoWIqq1yEGzrvSay3JUipqsp/oZ86tc5jD4kYp2PAkxD4UUAS07ivNtyKHLM5a 21vQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1752355929; x=1752960729; 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=7j+nFClWMYu+z4RxyaBNy1i8eSeqUJQPpZ3XTZUjlQs=; b=mnjHbqFRO2LYPLmXuzy21vFuzgkkR3ZrzCxLxCR2yaEz5WzfexuzVUFpmnZfADgncy c1jgRoOYeQ6DxJGYK4073evCEUzM040Q1JcYc9x/PMjjrXyicfX+c0bexLIDrkvxyxEE XdxFtu2XpfOwamwiai31gSWkEisk+zmWQaH9zqUJwTN2UWrVXxCgU/ZaQ1867njRGC2t 0rI4H0wBEp9/6PL29RxVODpxmmgXuFBWrIhxsEQ6vx7eb0nC2PN/9/Od4hd88M684bPM wGmZe1d6Lp+cv6iKAjpB3dmskKpWmfTams6lBng+1yCySxd2nGsYwpDkxKfuVUiD1bKw vtyw== X-Forwarded-Encrypted: i=1; AJvYcCVUugkU1B3/YvuaRHndEWUBdg40gpYtT/WMYX4ITjncUkKz4cfmupfJDW0jyH9uuzWIiCcHbUePzg==@kvack.org X-Gm-Message-State: AOJu0YzJARKMkRLNau3M3VoAVLBbjEcmt76IVtn/KX6VqRSQ3CtSY098 TASld/lBJiqTp9pCybUtHcy98xNckiWwDzpfwuzLQ4Q8R/mwjaHWyXjf X-Gm-Gg: ASbGncswSqdamIadDmIdftuz7Fdsl7nEc9otUFe8BcQQlrF7BKDtKtVnFecbjPLKKM6 OVfQcDFarQtAslZE/7H4vgD6VPtmqbpq3jBEl2wh2dgwBZ+jJpDIbXQd9ks3SXZpTg3Obbe6OUx WM/4Ry7c0baJ/NsfdaZZOKOwtgQtV0tsWpWXOdE9uQA/s3EXYDbd5TJWYJON5DAsKhGEJOxL/MW ZNJjmlcRnna4XG0OjTYeIrFc+gSUeopaFl5xE79ySdvCo5FQglJnkW0BgbCRkrRrL4OddCQ9KiI F2TBgmPcagWGTExJYujjKCEAAeREmVtBLOZ72zzNTv3iWERlOE4+Fy+Cn+Q4iG+1GsRElaeHZdJ efTJ3rouUYvbiW9k6g/Y/nQ2aH0UQ59oftjgrOCI+k/prUQ== X-Google-Smtp-Source: AGHT+IE4OVGv0ZHONTY8jo3BV9GHDYUu3GtoFhSxfwHUuz7H8XXTW3Jjoc+7pdproO8lCLYVAmLqDw== X-Received: by 2002:a17:90b:3503:b0:31c:260e:5603 with SMTP id 98e67ed59e1d1-31c4f562f15mr11707603a91.24.1752355928711; Sat, 12 Jul 2025 14:32:08 -0700 (PDT) Received: from mitchelllevy.localdomain ([174.127.224.194]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-31c3eb65aa2sm8003935a91.40.2025.07.12.14.32.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 12 Jul 2025 14:32:08 -0700 (PDT) From: Mitchell Levy Date: Sat, 12 Jul 2025 14:31:15 -0700 Subject: [PATCH v2 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: <20250712-rust-percpu-v2-4-826f2567521b@gmail.com> References: <20250712-rust-percpu-v2-0-826f2567521b@gmail.com> In-Reply-To: <20250712-rust-percpu-v2-0-826f2567521b@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=1752355923; l=8164; i=levymitchell0@gmail.com; s=20240719; h=from:subject:message-id; bh=Yhc2AZNWstxq/JmR6oLcJC1JQH1zZ60L+8ZJ13d/Yes=; b=7PlUlkr00EGSTFBjGecI5Y1jCbBQRf4K+CpEu8vYyLOv26zqXz/i9wBlJRC3sha4iMw1CGAtR dmDSdeDAdtXCDQ1FfikH92Lsn9VQUB/B0uiLGstCcKlAmie94nNufey X-Developer-Key: i=levymitchell0@gmail.com; a=ed25519; pk=n6kBmUnb+UNmjVkTnDwrLwTJAEKUfs2e8E+MFPZI93E= X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 06BDB1C0012 X-Stat-Signature: xqucggmwfb7dfdk46ukfyej8tbc1fdq9 X-Rspam-User: X-HE-Tag: 1752355929-230880 X-HE-Meta: U2FsdGVkX18UFkEtDVnz6ERnYSqRBgxlr+vllM7fSh9Hv8uHQtTMcs3mEaYdeKw8QclF42F9lwKEbVngUz8XvtPnvULmDUeqH1Si/xZ5+/IrT0uro3ZPARbG6KrIBtLlLaSIQeDi2vnVZCDcHD4zs6zn3Jv8MZ7EtSOWZ0LuWvwIKKc39egZYYPzneQp9Djzi3TKg2RHd6GuRH6yz4SL1ZOoBIkZkI838Tbr5AeqXAqiBXcgoeyx/pC1jUq0zcdiKPC3bFz9yueNWxM3Ndrd9J2WoEEUmoaf+o79gRzrhOUJ/ARXnL61P2SiEgnZuBR1+7lNWJLSSu5a8cVIDrS8IlTtlYi53eqCpWyhX1Op+vsrTlwXD6n7QRrFCXl3VrzBbO2N2VHhVKtuKhgwPUFBZcX17rWyd2VLQYfZ16r56LuSwN669x+bqGCR0PpRnYDy7HOi0L/LoeDls9xM14bp9mqJr/LrpCyym4SSxEhPO9gGv7qBCTJBeaK2YE8tzrSRzWrID1b5Jbm9m6eYsvvw3au+5BHYLXf3SY9jWMJZD010vJllJrHqP3HIgSf2mjEiifvMAJQVgi/SOnbQ+/x/OKnjwUt0ZNzvoqJhk//NQDTSjWr6BsQrP/94SEJczD6fsFdit3MwVnbPRAqCDwlgNqdZcETiQjPlpLfq8iW6Nw/D85XDyauOgcSSuBO60PbXhvkcBOpN3EYD1XIz6ofILWuxCowdK7zR32d6l5VmUDGBUIvtHTCGLY2sht/vbJWx+iYoUm+blqkPupUWvFG8J4a77aDOWH5yzdhAu3z9TrspIYPeCRT8PoO4/FwOxC7425VF3+PCEtWgiZLAHPr+TPZ87MTZMeoKjYuIH3M6GlTdrexoKn34hSD9PrMqgcjMAScYv1jFQz1ZfcyNjhYsdRQb7s+zzKlySZG2P/pVT1B9wa8Y8GhLJ3zQEfQi7YWlOHOpM/dH0rTyIZ6P7BG CfG41y/w Gq7+JZjSw3MDAll5yyiNJj6WPqKeAJLRjpkG6FYZC5mlVRkEAQv2Z6fNb2i3HjHkcHvaF+qtXa8pX+M44HVlwAE/rBw== 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 7dfceb6aefd7..b97d1d07a614 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 DynamicPerCpu type, as well as the PerCpu trait. 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