From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CE49C2E92D9; Fri, 11 Jul 2025 18:35:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752258932; cv=none; b=bWhLyzP0iDlSITiuzvQEP0GHct2cLprXMSdVheJKWMjkWGiekiHP26WbJZGhUGIc0A7lMWT/yzvyiWCesib2RLv3liIjJla9wrekgCqBB5IQolM65qPa5eruD2IsHO1sKoETptA9A0ObPu2orXDYtPLbpyuGdGtBHd/84rYwcC0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752258932; c=relaxed/simple; bh=fkK5mrBlQnRoJ2IhD+uQlyziIqN01tapyYfzf2qJJnM=; h=Mime-Version:Content-Type:Date:Message-Id:From:To:Cc:Subject: References:In-Reply-To; b=JrdI81o3h44JXmKY9CuH6DxRtkA4FtlNsefEPLKTVc3kT5LhoJ3O4gtuL7ktNrHbEuZxdE/FcKOimiXUbdYRyZ4FG65l35qGPVSPJUtLjuOQUSgz0kQzlS68yN/N2uyFp347RwZi+Reo7Bn5Ig3TiyF7I/46vlZaol9G0rGmZ/U= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=feIkAqcH; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="feIkAqcH" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 76E2FC4CEED; Fri, 11 Jul 2025 18:35:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1752258932; bh=fkK5mrBlQnRoJ2IhD+uQlyziIqN01tapyYfzf2qJJnM=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=feIkAqcHhWZyEVDLlRU1khjIgmSxKdHspSi+vck2tF/LN12wYuT7DUD8FGi83/DTt vvz7/zbg4DcbkXGmmx/rCotrLMLWIMklLMywPFuSWJo0CbUYrovqMQDhAQ52Gn55lx WzN07CKoLJPNm/uoWUNVvRBXZKvGGO0Y8Xtg7+3GGrcXDlM+7OztoW1NW291SB/dec UPvXf2w8dYRpSG0HANRxaPeQoEpTmqpBHOe0FQxU7hfuGiS2LJmtsNon2YfiHxvfro pNqQQR/sUggBkviDGcfd/QfzsOQlpF81MpP36bJ4Bt9JIFg7XqOD9Za0pZ4ViKBngX 8ntkFAEQ49QeQ== Precedence: bulk X-Mailing-List: rust-for-linux@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 Date: Fri, 11 Jul 2025 20:35:25 +0200 Message-Id: From: "Benno Lossin" To: "Boqun Feng" Cc: , , , , "Miguel Ojeda" , "Alex Gaynor" , "Gary Guo" , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , "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: Re: [PATCH v6 4/9] rust: sync: atomic: Add generic atomics X-Mailer: aerc 0.20.1 References: <20250710060052.11955-1-boqun.feng@gmail.com> <20250710060052.11955-5-boqun.feng@gmail.com> In-Reply-To: On Fri Jul 11, 2025 at 3:58 PM CEST, Boqun Feng wrote: > On Fri, Jul 11, 2025 at 10:03:07AM +0200, Benno Lossin wrote: >> On Thu Jul 10, 2025 at 8:00 AM CEST, Boqun Feng wrote: >> > diff --git a/rust/kernel/sync/atomic/generic.rs b/rust/kernel/sync/ato= mic/generic.rs >> > new file mode 100644 >> > index 000000000000..e044fe21b128 >> > --- /dev/null >> > +++ b/rust/kernel/sync/atomic/generic.rs >> > @@ -0,0 +1,289 @@ >> > +// SPDX-License-Identifier: GPL-2.0 >> > + >> > +//! Generic atomic primitives. >> > + >> > +use super::ops::*; >> > +use super::ordering::*; >> > +use crate::build_error; >> > +use core::cell::UnsafeCell; >> > + >> > +/// A generic atomic variable. > [...] >>=20 >> > +/// >> > +/// # Guarantees >> > +/// >> > +/// Doing an atomic operation while holding a reference of [`Self`] w= on't cause a data race, >> > +/// this is guaranteed by the safety requirement of [`Self::from_ptr(= )`] and the extra safety >> > +/// requirement of the usage on pointers returned by [`Self::as_ptr()= `]. >>=20 >> I'd rather think we turn this into an invariant that says any operations >> on `self.0` through a shared reference is atomic. >>=20 > [...] >> > +/// unit-only enums: >>=20 >> What are "unit-only" enums? Do you mean enums that don't have associated >> data? >>=20 > > Yes, I used the term mentioned at: > > https://doc.rust-lang.org/reference/items/enumerations.html#r-items.enum= .unit-only Ahhh, never saw that before :) >> > +/// >> > +/// ``` >> > +/// #[repr(i32)] >> > +/// enum State { Init =3D 0, Working =3D 1, Done =3D 2, } >> > +/// ``` >> > +/// >> > +/// # Safety >> > +/// >> > +/// - [`Self`] must have the same size and alignment as [`Self::Repr`= ]. >> > +/// - [`Self`] and [`Self::Repr`] must have the [round-trip transmuta= bility]. > [...] >> > + let a =3D self.as_ptr().cast::(); >> > + >> > + // SAFETY: >> > + // - For calling the atomic_read*() function: >> > + // - `a` is a valid pointer for the function per the CAST j= ustification above. >> > + // - Per the type guarantees, the following atomic operatio= n won't cause data races. >>=20 >> Which type guarantees? `Self`? >>=20 > > The above "# Guarantees" of `Atomic`, but yeah I think it should be > "# Invariants". Yeah and if we use invariants/guarantees always mention the type that they are of and don't assume the reader will "know" :) --- Cheers, Benno