From nobody Sun Feb 8 20:33:11 2026 Received: from mail-qk1-f193.google.com (mail-qk1-f193.google.com [209.85.222.193]) (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 108542D780A for ; Sat, 17 Jan 2026 12:23:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.193 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768652591; cv=none; b=n4eSdjgBUMFEFgUxE34WnlfEZ+Fr4FtvHW0RiqIF44T5ee/S/sUgKSKpNqb/GU8fZ6IRUu+O5cYh0Orp9DOWkEgNFuwsnMDsmPfMrvQT94da0rzJM9ijbTkQE99HOnW+Yd2K2HLDAj3GMeyIhmOyHkwY939Qke2pkHhSuIxF7zE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768652591; c=relaxed/simple; bh=Mn9K21+uoF4cJ3TSX9t5OyIIQAr4QjA54LiStkUJdz8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=d+Fgn5EOj2CHNS8kSNuGHjLC0a4Kqfzut4WlwDlqp3OWSrOYTY4zQ22XWcX0jplwmtxxdHTdTjn2Np8pZvi5OTzpBdET5dcS8MHSERyIZ9hgfZAJRhu8+f0wnnC5I23vn7/Aujyq4sTSC7oIzTkVGgKpLTOrjB/m4K/Eq0wPbeY= 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=iYLk7iNb; arc=none smtp.client-ip=209.85.222.193 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="iYLk7iNb" Received: by mail-qk1-f193.google.com with SMTP id af79cd13be357-8c52f15c5b3so337891585a.3 for ; Sat, 17 Jan 2026 04:23:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1768652586; x=1769257386; 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=JqkORS96nGwm/tYZHUw31tz+3dE4qDqicBtgX0AoLrQ=; b=iYLk7iNblRbM46CW1cZBLTffa+NPDJqDFmr+Z2fqsm0zr5jw0pPMP9OCVwfxzDC7yy BqosSPt3A5lCtVzFsIqKM1nQ16UJRrcsqFJTDuo241s2FQZfqF5IPwQY8uH18qEno+f9 A7D0cdOFsnG+YVtoCjfweZ0pl/jOqi2sQ7U3d53fy7EuPNJUsQSw+JSwL1Wpe/XyKC1W 1MjTi76ZzuuvAmjy4Gu9DF8ZcKN8qk+PtKITj0tObmwzW0dFZ5+LVb/IFhh0oCY2w8rh mMtNNR3XeUQGBhAKIWgstcYkC0DXGSL0bkRexBCZOZmpFIniEqkfcOKK3WmA4btQOUVG rvng== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1768652586; x=1769257386; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:feedback-id:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=JqkORS96nGwm/tYZHUw31tz+3dE4qDqicBtgX0AoLrQ=; b=lIIjYXtvS00F4pkfhKbAuancU/r+LaFT8EP3+4xnsRdEYJPProD4Z5w3HKJqOlqgqv kKdesVwpmB1q5ZnEKb2qK/iR+X+V4Jv7MUNLqR5F5z6lswcGbhv/EqoQuuWZSi5Ko8g1 2PhdxJCsaLdWywwKsXlu4O4Bm2+RzwLuWpl3jgbmMKSqvlMBBRuxV77+bVpHBmrsWenK zNx/z25sWx+Gc7IXEZ+U5VlIN3LqatZUBXJ2V+uJrGhN1mvHYCzli6FwTBXqdruTxrwX fDXcr2APmlojTg1JpzD+xuPtV7xA6HhOKQj0vUGRoK5HKweGi+6XyihOlWrGxCtd6p+6 rY8A== X-Forwarded-Encrypted: i=1; AJvYcCXJyZA6Ief0V27t7MRECGpDUAG0/V0qWI7HDvTMnXwKywosg+FXt8Mn5ywG4p0MmIulr0hjI/nAzt6z8Tc=@vger.kernel.org X-Gm-Message-State: AOJu0YxUGG066rdExFOuJ/hU3wUru7c3Sf5cz3yBVQHu1OR2JnzDtbCt KEZAsrii0onULrRuoU8a7iF3pz7oi/KDMeN0LB6lbnHVfYPxjAWS7QGg X-Gm-Gg: AY/fxX56c0rKBbPuAgnMfvFUfgRmtbuJihNmcHMuQJzHNehygMpkksBqdHiGbjpRBFM gpcsaod/sofYS2koix2swybTDQ4/yLvEs/oPabrn86iTC/a5XNNNCGnhe9c/ePTc60edFFPXsTa UIec+ewjnBBvcL2FolqmzCzC6SNQgzOrmxtf//WQHr99p6hqRc1eCySW1y8T7SreB4bb72rteZt o4M/0V7DHf+kbCeNXlkZf1sPZ0q0o2vRPrSCL4WsR2oAGKUKIfHxdzqmG4cDPZ+8RhLWkRHUMB+ nnQXC+ebhCC96pRYu4lFl+vlEPTUlzZ1sR306gis0M6TdW6J/UALomPRvZn013ROGuGFa/tRATm vEhnPjE3pIWxgTUOgyH6tw38uPC9ZtmQJwyskl95z7RSzL1Te1TqfUcWV2nn1vEkoUf4HU6EmpH vVGgsouAdvV1O4tv6tGtg/ZXiWeY+3iqnqoqhMyyIlLrakcOaiMcmPjADmqI0JYsuuZzrEg6271 mB8wqOaYubte3o= X-Received: by 2002:a05:620a:710c:b0:8b2:d72d:e41b with SMTP id af79cd13be357-8c6a676de84mr823908785a.44.1768652585732; Sat, 17 Jan 2026 04:23:05 -0800 (PST) Received: from fauth-a1-smtp.messagingengine.com (fauth-a1-smtp.messagingengine.com. [103.168.172.200]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-8942e5e535dsm46061576d6.4.2026.01.17.04.23.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 17 Jan 2026 04:23:05 -0800 (PST) Received: from phl-compute-01.internal (phl-compute-01.internal [10.202.2.41]) by mailfauth.phl.internal (Postfix) with ESMTP id 86362F4007E; Sat, 17 Jan 2026 07:23:04 -0500 (EST) Received: from phl-frontend-03 ([10.202.2.162]) by phl-compute-01.internal (MEProxy); Sat, 17 Jan 2026 07:23:04 -0500 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefgedrtddtgddufedukedtucetufdoteggodetrf dotffvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfurfetoffkrfgpnffqhgenuceu rghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmnecujf gurhephffvvefufffkofgjfhgggfestdekredtredttdenucfhrhhomhepuehoqhhunhcu hfgvnhhguceosghoqhhunhdrfhgvnhhgsehgmhgrihhlrdgtohhmqeenucggtffrrghtth gvrhhnpeegleejiedthedvheeggfejveefjeejkefgveffieeujefhueeigfegueehgeeg gfenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegsoh hquhhnodhmvghsmhhtphgruhhthhhpvghrshhonhgrlhhithihqdeiledvgeehtdeigedq udejjeekheehhedvqdgsohhquhhnrdhfvghngheppehgmhgrihhlrdgtohhmsehfihigmh gvrdhnrghmvgdpnhgspghrtghpthhtohepvdeipdhmohguvgepshhmthhpohhuthdprhgt phhtthhopehruhhsthdqfhhorhdqlhhinhhugiesvhhgvghrrdhkvghrnhgvlhdrohhrgh dprhgtphhtthhopehlihhnuhigqdhkvghrnhgvlhesvhhgvghrrdhkvghrnhgvlhdrohhr ghdprhgtphhtthhopehrtghusehvghgvrhdrkhgvrhhnvghlrdhorhhgpdhrtghpthhtoh epohhjvggurgeskhgvrhhnvghlrdhorhhgpdhrtghpthhtohepsghoqhhunhdrfhgvnhhg sehgmhgrihhlrdgtohhmpdhrtghpthhtohepghgrrhihsehgrghrhihguhhordhnvghtpd hrtghpthhtohepsghjohhrnhefpghghhesphhrohhtohhnmhgrihhlrdgtohhmpdhrtghp thhtoheplhhoshhsihhnsehkvghrnhgvlhdrohhrghdprhgtphhtthhopegrrdhhihhnug gsohhrgheskhgvrhhnvghlrdhorhhg X-ME-Proxy: Feedback-ID: iad51458e:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Sat, 17 Jan 2026 07:23:03 -0500 (EST) From: Boqun Feng To: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, rcu@vger.kernel.org Cc: Miguel Ojeda , 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 , "Paul E. McKenney" , Frederic Weisbecker , Neeraj Upadhyay , Joel Fernandes , Josh Triplett , Uladzislau Rezki , Steven Rostedt , Mathieu Desnoyers , Lai Jiangshan , Zqiang , FUJITA Tomonori Subject: [PATCH 4/5] rust: sync: atomic: Add Atomic<*mut T> support Date: Sat, 17 Jan 2026 20:22:42 +0800 Message-ID: <20260117122243.24404-5-boqun.feng@gmail.com> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20260117122243.24404-1-boqun.feng@gmail.com> References: <20260117122243.24404-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" Atomic pointer support is an important piece of synchronization algorithm, e.g. RCU, hence provide the support for that. Note that instead of relying on atomic_long or the implementation of `Atomic`, a new set of helpers (atomic_ptr_*) is introduced for atomic pointer specifically, this is because ptr2int casting would lose the provenance of a pointer and even though in theory there are a few tricks the provenance can be restored, it'll still be a simpler implementation if C could provide atomic pointers directly. The side effects of this approach are: we don't have the arithmetic and logical operations for pointers yet and the current implementation only works on ARCH_SUPPORTS_ATOMIC_RMW architectures, but these are implementation issues and can be added later. Signed-off-by: Boqun Feng Reviewed-by: FUJITA Tomonori Reviewed-by: Gary Guo --- rust/helpers/atomic_ext.c | 3 +++ rust/kernel/sync/atomic.rs | 12 +++++++++++- rust/kernel/sync/atomic/internal.rs | 21 +++++++++++++++------ rust/kernel/sync/atomic/predefine.rs | 23 +++++++++++++++++++++++ 4 files changed, 52 insertions(+), 7 deletions(-) diff --git a/rust/helpers/atomic_ext.c b/rust/helpers/atomic_ext.c index 240218e2e708..c267d5190529 100644 --- a/rust/helpers/atomic_ext.c +++ b/rust/helpers/atomic_ext.c @@ -36,6 +36,7 @@ __rust_helper void rust_helper_atomic_##tname##_set_relea= se(type *ptr, type val) =20 GEN_READ_SET_HELPERS(i8, s8) GEN_READ_SET_HELPERS(i16, s16) +GEN_READ_SET_HELPERS(ptr, const void *) =20 /* * xchg helpers depend on ARCH_SUPPORTS_ATOMIC_RMW and on the @@ -59,6 +60,7 @@ rust_helper_atomic_##tname##_xchg##suffix(type *ptr, type= new) \ =20 GEN_XCHG_HELPERS(i8, s8) GEN_XCHG_HELPERS(i16, s16) +GEN_XCHG_HELPERS(ptr, const void *) =20 /* * try_cmpxchg helpers depend on ARCH_SUPPORTS_ATOMIC_RMW and on the @@ -82,3 +84,4 @@ rust_helper_atomic_##tname##_try_cmpxchg##suffix(type *pt= r, type *old, type new) =20 GEN_TRY_CMPXCHG_HELPERS(i8, s8) GEN_TRY_CMPXCHG_HELPERS(i16, s16) +GEN_TRY_CMPXCHG_HELPERS(ptr, const void *) diff --git a/rust/kernel/sync/atomic.rs b/rust/kernel/sync/atomic.rs index 4aebeacb961a..4d2a5228c2e4 100644 --- a/rust/kernel/sync/atomic.rs +++ b/rust/kernel/sync/atomic.rs @@ -51,6 +51,10 @@ #[repr(transparent)] pub struct Atomic(AtomicRepr); =20 +// SAFETY: `Atomic` is safe to transfer between execution contexts beca= use of the safety +// requirement of `AtomicType`. +unsafe impl Send for Atomic {} + // SAFETY: `Atomic` is safe to share among execution contexts because a= ll accesses are atomic. unsafe impl Sync for Atomic {} =20 @@ -68,6 +72,11 @@ unsafe impl Sync for Atomic {} /// /// - [`Self`] must have the same size and alignment as [`Self::Repr`]. /// - [`Self`] must be [round-trip transmutable] to [`Self::Repr`]. +/// - [`Self`] must be safe to transfer between execution contexts, if it'= s [`Send`], this is +/// automatically satisfied. The exception is pointer types that are eve= n though marked as +/// `!Send` (e.g. raw pointers and [`NonNull`]) but requiring `unsafe= ` to do anything +/// meaningful on them. This is because transferring pointer values betw= een execution contexts is +/// safe as long as the actual `unsafe` dereferencing is justified. /// /// Note that this is more relaxed than requiring the bi-directional trans= mutability (i.e. /// [`transmute()`] is always sound between `U` and `T`) because of the su= pport for atomic @@ -108,7 +117,8 @@ unsafe impl Sync for Atomic {} /// [`transmute()`]: core::mem::transmute /// [round-trip transmutable]: AtomicType#round-trip-transmutability /// [Examples]: AtomicType#examples -pub unsafe trait AtomicType: Sized + Send + Copy { +/// [`NonNull`]: core::ptr::NonNull +pub unsafe trait AtomicType: Sized + Copy { /// The backing atomic implementation type. type Repr: AtomicImpl; } diff --git a/rust/kernel/sync/atomic/internal.rs b/rust/kernel/sync/atomic/= internal.rs index 0dac58bca2b3..93f5a7846645 100644 --- a/rust/kernel/sync/atomic/internal.rs +++ b/rust/kernel/sync/atomic/internal.rs @@ -7,6 +7,7 @@ use crate::bindings; use crate::macros::paste; use core::cell::UnsafeCell; +use ffi::c_void; =20 mod private { /// Sealed trait marker to disable customized impls on atomic implemen= tation traits. @@ -14,10 +15,11 @@ pub trait Sealed {} } =20 // The C side supports atomic primitives only for `i32` and `i64` (`atomic= _t` and `atomic64_t`), -// while the Rust side also layers provides atomic support for `i8` and `i= 16` -// on top of lower-level C primitives. +// while the Rust side also provides atomic support for `i8`, `i16` and `*= const c_void` on top of +// lower-level C primitives. impl private::Sealed for i8 {} impl private::Sealed for i16 {} +impl private::Sealed for *const c_void {} impl private::Sealed for i32 {} impl private::Sealed for i64 {} =20 @@ -26,10 +28,10 @@ impl private::Sealed for i64 {} /// This trait is sealed, and only types that map directly to the C side a= tomics /// or can be implemented with lower-level C primitives are allowed to imp= lement this: /// -/// - `i8` and `i16` are implemented with lower-level C primitives. +/// - `i8`, `i16` and `*const c_void` are implemented with lower-level C p= rimitives. /// - `i32` map to `atomic_t` /// - `i64` map to `atomic64_t` -pub trait AtomicImpl: Sized + Send + Copy + private::Sealed { +pub trait AtomicImpl: Sized + Copy + private::Sealed { /// The type of the delta in arithmetic or logical operations. /// /// For example, in `atomic_add(ptr, v)`, it's the type of `v`. Usuall= y it's the same type of @@ -51,6 +53,13 @@ impl AtomicImpl for i16 { type Delta =3D Self; } =20 +// The current helpers of load/store uses `{WRITE,READ}_ONCE()` hence the = atomicity is only +// guaranteed against read-modify-write operations if the architecture sup= ports native atomic RmW. +#[cfg(CONFIG_ARCH_SUPPORTS_ATOMIC_RMW)] +impl AtomicImpl for *const c_void { + type Delta =3D isize; +} + // `atomic_t` implements atomic operations on `i32`. impl AtomicImpl for i32 { type Delta =3D Self; @@ -262,7 +271,7 @@ macro_rules! declare_and_impl_atomic_methods { } =20 declare_and_impl_atomic_methods!( - [ i8 =3D> atomic_i8, i16 =3D> atomic_i16, i32 =3D> atomic, i64 =3D> at= omic64 ] + [ i8 =3D> atomic_i8, i16 =3D> atomic_i16, *const c_void =3D> atomic_pt= r, i32 =3D> atomic, i64 =3D> atomic64 ] /// Basic atomic operations pub trait AtomicBasicOps { /// Atomic read (load). @@ -280,7 +289,7 @@ fn set[release](a: &AtomicRepr, v: Self) { ); =20 declare_and_impl_atomic_methods!( - [ i8 =3D> atomic_i8, i16 =3D> atomic_i16, i32 =3D> atomic, i64 =3D> at= omic64 ] + [ i8 =3D> atomic_i8, i16 =3D> atomic_i16, *const c_void =3D> atomic_pt= r, i32 =3D> atomic, i64 =3D> atomic64 ] /// Exchange and compare-and-exchange atomic operations pub trait AtomicExchangeOps { /// Atomic exchange. diff --git a/rust/kernel/sync/atomic/predefine.rs b/rust/kernel/sync/atomic= /predefine.rs index 42067c6a266c..1a4670d225b5 100644 --- a/rust/kernel/sync/atomic/predefine.rs +++ b/rust/kernel/sync/atomic/predefine.rs @@ -4,6 +4,7 @@ =20 use crate::static_assert; use core::mem::{align_of, size_of}; +use ffi::c_void; =20 // Ensure size and alignment requirements are checked. static_assert!(size_of::() =3D=3D size_of::()); @@ -28,6 +29,16 @@ unsafe impl super::AtomicType for i16 { type Repr =3D i16; } =20 +// SAFETY: +// +// - `*mut T` has the same size and alignment with `*const c_void`, and is= round-trip +// transmutable to `*const c_void`. +// - `*mut T` is safe to transfer between execution contexts. See the safe= ty requirement of +// [`AtomicType`]. +unsafe impl super::AtomicType for *mut T { + type Repr =3D *const c_void; +} + // SAFETY: `i32` has the same size and alignment with itself, and is round= -trip transmutable to // itself. unsafe impl super::AtomicType for i32 { @@ -215,4 +226,16 @@ fn atomic_bool_tests() { assert_eq!(false, x.load(Relaxed)); assert_eq!(Ok(false), x.cmpxchg(false, true, Full)); } + + #[test] + fn atomic_ptr_tests() { + let mut v =3D 42; + let mut u =3D 43; + let x =3D Atomic::new(&raw mut v); + + assert_eq!(x.load(Acquire), &raw mut v); + assert_eq!(x.cmpxchg(&raw mut u, &raw mut u, Relaxed), Err(&raw mu= t v)); + assert_eq!(x.cmpxchg(&raw mut v, &raw mut u, Relaxed), Ok(&raw mut= v)); + assert_eq!(x.load(Relaxed), &raw mut u); + } } --=20 2.51.0