From nobody Tue Oct 7 07:18:17 2025 Received: from mail-qk1-f179.google.com (mail-qk1-f179.google.com [209.85.222.179]) (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 908BB2253FE; Mon, 14 Jul 2025 05:37:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752471439; cv=none; b=mFsTAn1z489HSfYCArtAR/E1/ITk5EtGOKVoYn4/kNwIkMbvRz7zMpC8lWrn0FW+MVGbLl9HyWFIrq7kI9WZ62P3lX672cB2oN/jccBUyl56cwMvc/aq8Njir3ithxyt6AWrjjisG1Faa7F7cyu+30dMKmVGqGTH5BIiCW3sjww= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752471439; c=relaxed/simple; bh=RdqOL7HUpVhGhmtZOWeFb6/5oGV3e0YDMIsRXjOH6lg=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=TphP8XOsAhKwtJSaDT9ZTQjKhsPyjhbOpnDNHz1BS3+XFCetnrXUrooc0NHEXMBm5GHqtyf4eLLB8BgHItGvjb1j2sqDznUykC+b5CJuYN/6hovrKeym+Y/umnEvEv9PkNaBT6DtPo/ukbS6gcjmZEV4XZPQqPoBqcjlDUWja9U= 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=nVlXjbjQ; arc=none smtp.client-ip=209.85.222.179 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="nVlXjbjQ" Received: by mail-qk1-f179.google.com with SMTP id af79cd13be357-7e32c5a174cso30139485a.1; Sun, 13 Jul 2025 22:37:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1752471435; x=1753076235; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:feedback-id:from:to:cc:subject :date:message-id:reply-to; bh=B9wJ4c2U301QOkQuCdVz7fDf/TBtX7Q85JWYXNeFCIg=; b=nVlXjbjQJ5mkPg1UHhvsxfj3dfTzJSAPykyyv2Mnw1CjteNFOhNTsNJ5X37kc0AjmP X9Qx10OAjbkkeJIIEGApyDDq8ZBgkgRao246hng4zZAMkHiAH/Bfrs4u1c82fxQ1QCi2 sXpEZ8B8VWUd82Kz1E8ByYGPnCEyflM27zswqie5w+XV3wr/cZT7hGCWEH7U3TCQYJRv MUp6r5SnOZI0sR7Icjl+zWSd0U61+NRUKO3KTB6i1a6pw3nYeR/kKiW8TybRwU5Erp/i JEoaOrIF2nmlb1kcmsG8/pnuigi0Z47c/tgv8N1gzzgR3W1GP9XZhbuUu195EHG5Ip6D c68Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1752471435; x=1753076235; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:feedback-id:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=B9wJ4c2U301QOkQuCdVz7fDf/TBtX7Q85JWYXNeFCIg=; b=iVBV6vxAbtO3DUmxgMQ+9N/pjKaG2+ZfYZanRnDMhRB1HbWQhzm2IMdv2X7cfFaL4s WSnO6UfaBeEtvoOXMaA+dHQtVj+lbqGUTZ2mr9IM9mDHe9WmUCddvhRqqYN2VA7ag6iN rGKtZmc4/K/DLGMF3FkUGbyXzRn3BAFR56da+SBJPh6Fi4RXj4Ff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2k3A== X-Received: by 2002:ac8:5dc6:0:b0:4ab:6b08:9db8 with SMTP id d75a77b69052e-4ab6b08ad01mr53376821cf.11.1752471435261; Sun, 13 Jul 2025 22:37:15 -0700 (PDT) Received: from fauth-a2-smtp.messagingengine.com (fauth-a2-smtp.messagingengine.com. [103.168.172.201]) by smtp.gmail.com with ESMTPSA id d75a77b69052e-4a9edea72cesm46260791cf.57.2025.07.13.22.37.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 13 Jul 2025 22:37:14 -0700 (PDT) Received: from phl-compute-12.internal (phl-compute-12.phl.internal [10.202.2.52]) by mailfauth.phl.internal (Postfix) with ESMTP id 3A4A0F40066; Mon, 14 Jul 2025 01:37:14 -0400 (EDT) Received: from phl-mailfrontend-01 ([10.202.2.162]) by phl-compute-12.internal (MEProxy); Mon, 14 Jul 2025 01:37:14 -0400 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeffedrtdefgdehuddufecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpuffrtefokffrpgfnqfghnecuuegr ihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenucfjug hrpefhvfevufffkffojghfggfgsedtkeertdertddtnecuhfhrohhmpeeuohhquhhnucfh vghnghcuoegsohhquhhnrdhfvghnghesghhmrghilhdrtghomheqnecuggftrfgrthhtvg hrnhepgeeljeeitdehvdehgefgjeevfeejjeekgfevffeiueejhfeuiefggeeuheeggefg necuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomhepsghoqh hunhdomhgvshhmthhprghuthhhphgvrhhsohhnrghlihhthidqieelvdeghedtieegqddu jeejkeehheehvddqsghoqhhunhdrfhgvnhhgpeepghhmrghilhdrtghomhesfhhigihmvg drnhgrmhgvpdhnsggprhgtphhtthhopedvjedpmhhouggvpehsmhhtphhouhhtpdhrtghp thhtoheplhhinhhugidqkhgvrhhnvghlsehvghgvrhdrkhgvrhhnvghlrdhorhhgpdhrtg hpthhtoheprhhushhtqdhfohhrqdhlihhnuhigsehvghgvrhdrkhgvrhhnvghlrdhorhhg pdhrtghpthhtoheplhhkmhhmsehlihhsthhsrdhlihhnuhigrdguvghvpdhrtghpthhtoh eplhhinhhugidqrghrtghhsehvghgvrhdrkhgvrhhnvghlrdhorhhgpdhrtghpthhtohep ohhjvggurgeskhgvrhhnvghlrdhorhhgpdhrtghpthhtoheprghlvgigrdhgrgihnhhorh esghhmrghilhdrtghomhdprhgtphhtthhopegsohhquhhnrdhfvghnghesghhmrghilhdr tghomhdprhgtphhtthhopehgrghrhiesghgrrhihghhuohdrnhgvthdprhgtphhtthhope gsjhhorhhnfegpghhhsehprhhothhonhhmrghilhdrtghomh X-ME-Proxy: Feedback-ID: iad51458e:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Mon, 14 Jul 2025 01:37:13 -0400 (EDT) From: Boqun Feng To: linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, lkmm@lists.linux.dev, linux-arch@vger.kernel.org Cc: "Miguel Ojeda" , "Alex Gaynor" , "Boqun Feng" , "Gary Guo" , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , "Benno Lossin" , "Andreas Hindborg" , "Alice Ryhl" , "Trevor Gross" , "Danilo Krummrich" , "Will Deacon" , "Peter Zijlstra" , "Mark Rutland" , "Wedson Almeida Filho" , "Viresh Kumar" , "Lyude Paul" , "Ingo Molnar" , "Mitchell Levy" , "Paul E. McKenney" , "Greg Kroah-Hartman" , "Linus Torvalds" , "Thomas Gleixner" , Alan Stern Subject: [PATCH v7 6/9] rust: sync: atomic: Add the framework of arithmetic operations Date: Sun, 13 Jul 2025 22:36:53 -0700 Message-Id: <20250714053656.66712-7-boqun.feng@gmail.com> X-Mailer: git-send-email 2.39.5 (Apple Git-154) In-Reply-To: <20250714053656.66712-1-boqun.feng@gmail.com> References: <20250714053656.66712-1-boqun.feng@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" One important set of atomic operations is the arithmetic operations, i.e. add(), sub(), fetch_add(), add_return(), etc. However it may not make senses for all the types that `AllowAtomic` to have arithmetic operations, for example a `Foo(u32)` may not have a reasonable add() or sub(), plus subword types (`u8` and `u16`) currently don't have atomic arithmetic operations even on C side and might not have them in the future in Rust (because they are usually suboptimal on a few architecures). Therefore the plan is to add a few subtraits of `AllowAtomic` describing which types have and can do atomic arithemtic operations. One trait `AllowAtomicAdd` is added, and only add() and fetch_add() are added. The rest will be added in the future. Reviewed-by: Alice Ryhl Signed-off-by: Boqun Feng --- rust/kernel/sync/atomic.rs | 14 ++++ rust/kernel/sync/atomic/generic.rs | 111 ++++++++++++++++++++++++++++- 2 files changed, 124 insertions(+), 1 deletion(-) diff --git a/rust/kernel/sync/atomic.rs b/rust/kernel/sync/atomic.rs index c5193c1c90fe..54f5b4618337 100644 --- a/rust/kernel/sync/atomic.rs +++ b/rust/kernel/sync/atomic.rs @@ -29,8 +29,22 @@ unsafe impl generic::AllowAtomic for i32 { type Repr =3D i32; } =20 +// SAFETY: The wrapping add result of two `i32`s is a valid `i32`. +unsafe impl generic::AllowAtomicAdd for i32 { + fn rhs_into_delta(rhs: i32) -> i32 { + rhs + } +} + // SAFETY: `i64` has the same size and alignment with itself, and is round= -trip transmutable to // itself. unsafe impl generic::AllowAtomic for i64 { type Repr =3D i64; } + +// SAFETY: The wrapping add result of two `i64`s is a valid `i64`. +unsafe impl generic::AllowAtomicAdd for i64 { + fn rhs_into_delta(rhs: i64) -> i64 { + rhs + } +} diff --git a/rust/kernel/sync/atomic/generic.rs b/rust/kernel/sync/atomic/g= eneric.rs index 4e45d594d8ef..9e2394017202 100644 --- a/rust/kernel/sync/atomic/generic.rs +++ b/rust/kernel/sync/atomic/generic.rs @@ -2,7 +2,7 @@ =20 //! Generic atomic primitives. =20 -use super::ops::{AtomicHasBasicOps, AtomicHasXchgOps, AtomicImpl}; +use super::ops::{AtomicHasArithmeticOps, AtomicHasBasicOps, AtomicHasXchgO= ps, AtomicImpl}; use super::{ordering, ordering::OrderingType}; use crate::build_error; use core::cell::UnsafeCell; @@ -104,6 +104,18 @@ const fn into_repr(v: T) -> T::Repr { unsafe { core::mem::transmute_copy(&r) } } =20 +/// Types that support atomic add operations. +/// +/// # Safety +/// +/// Wrapping adding any value of type `Self::Repr::Delta` obtained by [`Se= lf::rhs_into_delta()`] to +/// any value of type `Self::Repr` obtained through transmuting a value of= type `Self` to must +/// yield a value with a bit pattern also valid for `Self`. +pub unsafe trait AllowAtomicAdd: AllowAtomic { + /// Converts `Rhs` into the `Delta` type of the atomic implementation. + fn rhs_into_delta(rhs: Rhs) -> ::Delta; +} + impl Atomic { /// Creates a new atomic `T`. pub const fn new(v: T) -> Self { @@ -462,3 +474,100 @@ fn try_cmpxchg(&self, old: &= mut T, new: T, _: Ordering) ret } } + +impl Atomic +where + T::Repr: AtomicHasArithmeticOps, +{ + /// Atomic add. + /// + /// Atomically updates `*self` to `(*self).wrapping_add(v)`. + /// + /// # Examples + /// + /// ``` + /// use kernel::sync::atomic::{Atomic, Relaxed}; + /// + /// let x =3D Atomic::new(42); + /// + /// assert_eq!(42, x.load(Relaxed)); + /// + /// x.add(12, Relaxed); + /// + /// assert_eq!(54, x.load(Relaxed)); + /// ``` + #[inline(always)] + pub fn add(&self, v: Rhs, _: Ord= ering) + where + T: AllowAtomicAdd, + { + let v =3D T::rhs_into_delta(v); + // CAST: Per the safety requirement of `AllowAtomic`, a valid poin= ter of `T` is a valid + // pointer of `T::Repr` for reads and valid for writes of values t= ransmutable to `T`. + let a =3D self.as_ptr().cast::(); + + // `*self` remains valid after `atomic_add()` because of the safet= y requirement of + // `AllowAtomicAdd`. + // + // SAFETY: + // - `a` is aligned to `align_of::()` because of the safe= ty requirement of + // `AllowAtomic` and the guarantee of `Atomic::as_ptr()`. + // - `a` is a valid pointer per the CAST justification above. + unsafe { + T::Repr::atomic_add(a, v); + } + } + + /// Atomic fetch and add. + /// + /// Atomically updates `*self` to `(*self).wrapping_add(v)`, and retur= ns the value of `*self` + /// before the update. + /// + /// # Examples + /// + /// ``` + /// use kernel::sync::atomic::{Atomic, Acquire, Full, Relaxed}; + /// + /// let x =3D Atomic::new(42); + /// + /// assert_eq!(42, x.load(Relaxed)); + /// + /// assert_eq!(54, { x.fetch_add(12, Acquire); x.load(Relaxed) }); + /// + /// let x =3D Atomic::new(42); + /// + /// assert_eq!(42, x.load(Relaxed)); + /// + /// assert_eq!(54, { x.fetch_add(12, Full); x.load(Relaxed) } ); + /// ``` + #[inline(always)] + pub fn fetch_add(&self, v: Rhs, _: Order= ing) -> T + where + T: AllowAtomicAdd, + { + let v =3D T::rhs_into_delta(v); + // CAST: Per the safety requirement of `AllowAtomic`, a valid poin= ter of `T` is a valid + // pointer of `T::Repr` for reads and valid for writes of values t= ransmutable to `T`. + let a =3D self.as_ptr().cast::(); + + // `*self` remains valid after `atomic_fetch_add*()` because of th= e safety requirement of + // `AllowAtomicAdd`. + // + // SAFETY: + // - `a` is aligned to `align_of::()` because of the safe= ty requirement of + // `AllowAtomic` and the guarantee of `Atomic::as_ptr()`. + // - `a` is a valid pointer per the CAST justification above. + let ret =3D unsafe { + match Ordering::TYPE { + OrderingType::Full =3D> T::Repr::atomic_fetch_add(a, v), + OrderingType::Acquire =3D> T::Repr::atomic_fetch_add_acqui= re(a, v), + OrderingType::Release =3D> T::Repr::atomic_fetch_add_relea= se(a, v), + OrderingType::Relaxed =3D> T::Repr::atomic_fetch_add_relax= ed(a, v), + } + }; + + // SAFETY: `ret` comes from reading `a` which was derived from `se= lf.as_ptr()` which points + // at a valid `T`. + unsafe { from_repr(ret) } + } +} --=20 2.39.5 (Apple Git-154)