From nobody Mon Nov 25 01:57:02 2024 Received: from mail-qt1-f170.google.com (mail-qt1-f170.google.com [209.85.160.170]) (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 1112C15747D; Fri, 1 Nov 2024 06:04:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.170 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730441049; cv=none; b=HfCJaRJc4jsuIeQfFH7uHp8QI+DZ6ruL75l98H8ppQ7u3mTg0ePxb7ucL8Uc5bEcwnDMMoGP029Mb+tqzZ2L1dt1ymQYproWQDv9S/HUFgwnzkxwx/NYZFDiHA9fkgu++YrIZtRlhxvEn96EbWRi+xeeqG0lmt1A6Yd37N9Tcag= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730441049; c=relaxed/simple; bh=dbFh5tZ5JSCZ2km4t7r3WkaZizr4S5DDdwDqMMZTjO0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=OUXBJiHZo9B6ps3nBF6caYOIjAB0lcQOVN5J3k1yLsP1ijTkXd8IgmcVk5NCL8hP0xGlJ9Efwn0TnQSuBI0r3GycrXBj7ocmwJfsw2XO8bSdhcSvgc24ldkDizK8WUAVLu7JiYuHbhcyPGRPX8GqrIp1zidOiCIV+GGFy6VV1ec= 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=ONiENVIL; arc=none smtp.client-ip=209.85.160.170 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="ONiENVIL" Received: by mail-qt1-f170.google.com with SMTP id d75a77b69052e-460dce6fff9so11624721cf.1; Thu, 31 Oct 2024 23:04:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1730441046; x=1731045846; 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=JH7KGrs5tWNaElymjoHhwHajdPBdI1O/huzZLgMbScw=; b=ONiENVIL2LWdgx2RCBwcRc87pmsUyS8swFD5U6mxEXUbdU4D1IOQcVSYWxTTsfkP/T B9p0sfIT/+8rCUPtpv77v7GM+46xfPHZZTppWuc2TDMF0Du7gpvkrFbN/z1TGKImTlDq x0aHf4cYpp5LKLYZvfClykvAnxUhNNDucI4nERkNJs5u58/Haja+/dvK+1hQzAiKbOpm a2ZIDvfgNwtLRT2QKykTC38sdVxUWwmF+jaAp9wLUJsvmms9KJyPqvAmnodr4sgCiIg+ QLbbXorfflaMwG8KBbx9vqMVjHKCwfM2MwIER0O9fP485xtJG0+OgfSKOJ+GWVENIY8Y ARrA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730441046; x=1731045846; 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=JH7KGrs5tWNaElymjoHhwHajdPBdI1O/huzZLgMbScw=; b=XuvHoGNMKUD12MQVCRgcP9Av9jPedC+vJhrq1GRKn/DENrpAkic4qGu/P1pLqmKbKT toV+x8s4rQCJF/O9QrDGghjRwJK/LS0CYFB5Y6RiUOYjLS8cwPOx4fMnzU7y4XGk6GiI /McS7BC4d8qdFE8KYU6eWQv74sxV8LMZiS+PCHQTecgKeIHDlfwQaFOzAJ/MAJfifkdl QUOKQmJi0TfGwcHc9G28ekZOXvFVvcrtxAXxM47o6KZQRY/5j2p9j5DHTcgR647kV1NR SnAnC+08Gg5yLWFHfYjBlYzc8CAkWNylZ3yAX0I/mv+zcY2EroYfyK+lwkOsT1839APd wbIQ== X-Forwarded-Encrypted: i=1; AJvYcCUfOFD056xKHKOgKQbzSHxJeN+JoCau8JzMZMpBsGUgT+XFkwKFZ4hzIa1I05yH3aj2fHeLKLK+3GJW@vger.kernel.org, AJvYcCVF6GlqDKXiDnMd9joRJC6tH1q11tnaWNwm75BJmDPaduC/8m4Yq2icfu1ZmbGBdGYaBgPihcE2LQTSMREj6Q==@vger.kernel.org, AJvYcCVqQ3rM9EN5ypXBj40tMpU/8kGQS1KbfHE3Zbqri3AQoM45RJwtg6EV96Mw5Tk8wRucNYm1@vger.kernel.org, AJvYcCWlZZgMFjI/woN4w4mxCO4TZig6sOJRSgPmqvXy5MvKNmD0C9HS8jvbmeDAfOxcrCs75rBDHXCKIGy15Ksl@vger.kernel.org X-Gm-Message-State: AOJu0YwUiXuO4kUJfXXBDG0r4N5fbRCn2nQcSY6YrZdAqCChg1hakG86 gDRPNTEBZMf5ayl5+TGIF3yIPfvl4aXAqQt0Jl2E0sWPtlfLAgZO X-Google-Smtp-Source: AGHT+IETHN8t913Neds+Flo2tBJr2gQ2uOlumxv7dWk8jfF6Hj1+Hl306SbJkBvaA4RYy6WrLaswjw== X-Received: by 2002:ac8:7dc8:0:b0:460:a9ec:b4fd with SMTP id d75a77b69052e-462b8759878mr19769901cf.42.1730441045924; Thu, 31 Oct 2024 23:04:05 -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-462ad1a0f59sm15188921cf.81.2024.10.31.23.04.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 31 Oct 2024 23:04:05 -0700 (PDT) Received: from phl-compute-09.internal (phl-compute-09.phl.internal [10.202.2.49]) by mailfauth.phl.internal (Postfix) with ESMTP id 1BDFE1200043; Fri, 1 Nov 2024 02:04:05 -0400 (EDT) Received: from phl-mailfrontend-01 ([10.202.2.162]) by phl-compute-09.internal (MEProxy); Fri, 01 Nov 2024 02:04:05 -0400 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrvdekkedgkeekucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggvpdfu rfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnh htshculddquddttddmnecujfgurhephffvvefufffkofgjfhgggfestdekredtredttden ucfhrhhomhepuehoqhhunhcuhfgvnhhguceosghoqhhunhdrfhgvnhhgsehgmhgrihhlrd gtohhmqeenucggtffrrghtthgvrhhnpeegleejiedthedvheeggfejveefjeejkefgveff ieeujefhueeigfegueehgeeggfenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmh epmhgrihhlfhhrohhmpegsohhquhhnodhmvghsmhhtphgruhhthhhpvghrshhonhgrlhhi thihqdeiledvgeehtdeigedqudejjeekheehhedvqdgsohhquhhnrdhfvghngheppehgmh grihhlrdgtohhmsehfihigmhgvrdhnrghmvgdpnhgspghrtghpthhtohepheejpdhmohgu vgepshhmthhpohhuthdprhgtphhtthhopehruhhsthdqfhhorhdqlhhinhhugiesvhhgvg hrrdhkvghrnhgvlhdrohhrghdprhgtphhtthhopehrtghusehvghgvrhdrkhgvrhhnvghl rdhorhhgpdhrtghpthhtoheplhhinhhugidqkhgvrhhnvghlsehvghgvrhdrkhgvrhhnvg hlrdhorhhgpdhrtghpthhtoheplhhinhhugidqrghrtghhsehvghgvrhdrkhgvrhhnvghl rdhorhhgpdhrtghpthhtoheplhhlvhhmsehlihhsthhsrdhlihhnuhigrdguvghvpdhrtg hpthhtoheplhhkmhhmsehlihhsthhsrdhlihhnuhigrdguvghvpdhrtghpthhtohepohhj vggurgeskhgvrhhnvghlrdhorhhgpdhrtghpthhtoheprghlvgigrdhgrgihnhhorhesgh hmrghilhdrtghomhdprhgtphhtthhopeifvggushhonhgrfhesghhmrghilhdrtghomh X-ME-Proxy: Feedback-ID: iad51458e:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Fri, 1 Nov 2024 02:04:04 -0400 (EDT) From: Boqun Feng To: rust-for-linux@vger.kernel.org, rcu@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arch@vger.kernel.org, llvm@lists.linux.dev, lkmm@lists.linux.dev Cc: Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Alan Stern , Andrea Parri , Will Deacon , Peter Zijlstra , Nicholas Piggin , David Howells , Jade Alglave , Luc Maranget , "Paul E. McKenney" , Akira Yokosawa , Daniel Lustig , Joel Fernandes , Nathan Chancellor , Nick Desaulniers , kent.overstreet@gmail.com, Greg Kroah-Hartman , elver@google.com, Mark Rutland , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Catalin Marinas , torvalds@linux-foundation.org, linux-arm-kernel@lists.infradead.org, linux-fsdevel@vger.kernel.org, Trevor Gross , dakr@redhat.com, Frederic Weisbecker , Neeraj Upadhyay , Josh Triplett , Uladzislau Rezki , Steven Rostedt , Mathieu Desnoyers , Lai Jiangshan , Zqiang , Paul Walmsley , Palmer Dabbelt , Albert Ou , linux-riscv@lists.infradead.org Subject: [RFC v2 09/13] rust: sync: atomic: Add Atomic<*mut T> Date: Thu, 31 Oct 2024 23:02:32 -0700 Message-ID: <20241101060237.1185533-10-boqun.feng@gmail.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20241101060237.1185533-1-boqun.feng@gmail.com> References: <20241101060237.1185533-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" Add atomic support for raw pointer values, similar to `isize` and `usize`, the representation type is selected based on CONFIG_64BIT. `*mut T` is not `Send`, however `Atomic<*mut T>` definitely needs to be a `Sync`, and that's the whole point of atomics: being able to have multiple shared references in different threads so that they can sync with each other. As a result, a pointer value will be transferred from one thread to another via `Atomic<*mut T>`: x.store(p1, Relaxed); let p =3D x.load(p1, Relaxed); This means a raw pointer value (`*mut T`) needs to be able to transfer across thread boundaries, which is essentially `Send`. To reflect this in the type system, and based on the fact that pointer values can be transferred safely (only using them to dereference is unsafe), as suggested by Alice, extend the `AllowAtomic` trait to include a customized `Send` semantics, that is: `impl AllowAtomic` has to be safe to be transferred across thread boundaries. Suggested-by: Alice Ryhl Signed-off-by: Boqun Feng --- rust/kernel/sync/atomic.rs | 24 ++++++++++++++++++++++++ rust/kernel/sync/atomic/generic.rs | 16 +++++++++++++--- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/rust/kernel/sync/atomic.rs b/rust/kernel/sync/atomic.rs index 4166ad48604f..e62c3cd1d3ca 100644 --- a/rust/kernel/sync/atomic.rs +++ b/rust/kernel/sync/atomic.rs @@ -173,3 +173,27 @@ fn delta_into_repr(d: Self::Delta) -> Self::Repr { d as _ } } + +/// ```rust +/// use kernel::sync::atomic::{Atomic, Relaxed}; +/// +/// let x =3D Atomic::new(core::ptr::null_mut::()); +/// +/// assert!(x.load(Relaxed).is_null()); +/// ``` +// SAFETY: A `*mut T` has the same size and the alignment as `i64` for 64b= it and the same as `i32` +// for 32bit. And it's safe to transfer the ownership of a pointer value t= o another thread. +unsafe impl generic::AllowAtomic for *mut T { + #[cfg(CONFIG_64BIT)] + type Repr =3D i64; + #[cfg(not(CONFIG_64BIT))] + type Repr =3D i32; + + fn into_repr(self) -> Self::Repr { + self as _ + } + + fn from_repr(repr: Self::Repr) -> Self { + repr as _ + } +} diff --git a/rust/kernel/sync/atomic/generic.rs b/rust/kernel/sync/atomic/g= eneric.rs index a75c3e9f4c89..cff98469ed35 100644 --- a/rust/kernel/sync/atomic/generic.rs +++ b/rust/kernel/sync/atomic/generic.rs @@ -19,6 +19,10 @@ #[repr(transparent)] pub struct Atomic(Opaque); =20 +// SAFETY: `Atomic` is safe to send between execution contexts, because= `T` is `AllowAtomic` and +// `AllowAtomic`'s safety requirement guarantees that. +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 @@ -30,8 +34,13 @@ unsafe impl Sync for Atomic {} /// /// # Safety /// -/// [`Self`] must have the same size and alignment as [`Self::Repr`]. -pub unsafe trait AllowAtomic: Sized + Send + Copy { +/// - [`Self`] must have the same size and alignment as [`Self::Repr`]. +/// - The implementer must guarantee it's safe to transfer ownership from = one execution context to +/// another, this means it has to be a [`Send`], but because `*mut T` is= not [`Send`] and that's +/// the basic type needs to support atomic operations, so this safety re= quirement is added to +/// [`AllowAtomic`] trait. This safety requirement is automatically sati= sfied if the type is a +/// [`Send`]. +pub unsafe trait AllowAtomic: Sized + Copy { /// The backing atomic implementation type. type Repr: AtomicImpl; =20 @@ -42,7 +51,8 @@ pub unsafe trait AllowAtomic: Sized + Send + Copy { fn from_repr(repr: Self::Repr) -> Self; } =20 -// SAFETY: `T::Repr` is `Self` (i.e. `T`), so they have the same size and = alignment. +// SAFETY: `T::Repr` is `Self` (i.e. `T`), so they have the same size and = alignment. And all +// `AtomicImpl` types are `Send`. unsafe impl AllowAtomic for T { type Repr =3D Self; =20 --=20 2.45.2