From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f52.google.com (mail-wm1-f52.google.com [209.85.128.52]) (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 4332221146F for ; Thu, 12 Dec 2024 10:51:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.52 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734000699; cv=none; b=CZxuWA1OYeBnPxiqiJfKuXlSJ7Aao7HclBgO/ZQq7dRQo1RrsWtDiZsR+L3SwFzT5ZsuMzDVH/oRc/fricTSwzU8iTYvXVPXCb8ZVd2BFyaBPJR6E7YjR2BI8l1eiDg9hiPiFvzli9aFJTayg5toXd3wivZ4TDXDPQJ+oGlZ4sY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734000699; c=relaxed/simple; bh=r8PedwbATslMjHn8HdkHK7/SPqYwOBI7VaWxxuig9FE=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=RHB5TQ8uMIqfDGU2eeT/3RLk/a9cs9DR7K5a62m1dAz/fL5YWqg/CPpsFRsDa+X0DGLAX7X6CIsuCCajPsn+0AAT2fMwatIdKtm92X1q7XGiDrFx2YhD+Pj2sOy67mzM/Usl/FbHAOmz+ooOTL45dfolBVVrc5gNy7sYINV9atI= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=sav9PnrJ; arc=none smtp.client-ip=209.85.128.52 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="sav9PnrJ" Received: by mail-wm1-f52.google.com with SMTP id 5b1f17b1804b1-4361dc6322fso2918765e9.3 for ; Thu, 12 Dec 2024 02:51:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1734000695; x=1734605495; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=rtpsjC48mE/nmTjT+Q7/MwVuoD24o44fCuhvsg4pi50=; b=sav9PnrJNBwmfEgRCNJBkS1L2H6Dkv1gJFXcd0QcZV3XczZJSQBOcjyyH1PWWnzth8 X6wh6yv2dJVaPM56v55qUMWZ4zJhym6NhmImyD9wAcAhkzUrXK6HaOSbC5N0A7kiq1mb xYB38FlKaUI+tkGZy41ZfXiS1RfS5HxANFaeSQfz27T3SKxlne80Wb3HUqZSLHlXMnKk t3Ch4eCLeX2YTah4oRyjjRKzhrCpovzYTpQKrJZW0yGxpFY46FfQ2cgCzHlV1GPhq4zf UJhGWwhXuPMvb0OGhloM6gNEl2qfJQuD0cFDyjJ9gNUr+XnDusBOyYTkWswNl/aPgS6Q P4XQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734000695; x=1734605495; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=rtpsjC48mE/nmTjT+Q7/MwVuoD24o44fCuhvsg4pi50=; b=rl82btGWuk5Gez2+WMDc6IWIsPW6SYwM3rxkXTvzv0VkZxY23AN+5E3sRoJUopMtfE jCKwH7T4LWDPBIs3aybxfj2fUcMAzi+hWwxRobVZbmF2LCIEK/CfRQ8t7yQ41UQeGxgq HLpQBo79wpEcfS+7Ya38t0fqKhY8XLcVP/rAtq+QuOIcgZydRLUWMz7qptXGXMnzZlqS 70v3pu9by5vv0Zgh4mK6Cim1PpVyPoK/aQ2EoU0tK2YzMKrk91Bdfop4GOVKSXth65o0 NgEnzP68LZnyHYRF9F+AK8u2glnwNAF+mi5C5E7GHSFnUSmROBHY9A1FTz5ujzRUnqgv wHwQ== X-Gm-Message-State: AOJu0YzJAcomeZf9MjwPfRKXo3ZC0ZZXBCmRh02hweerfy0mrk+7w6Yd 9A6P6um6vsz8Qi76CnkZVcTGoJV/9nB35fPXfLprzvquB/9tgxAVF3Sk/Tf3rsDpcVXixiod46Y 2ugi+lyYb2KOSoWf2GieX6Hlsu/YfBIEhRKAi1xqHjiAZ1CgfUVJ9CHIjWA== X-Gm-Gg: ASbGncvu/cxmFego8yv+CT9nmamJO7muSePDPzlHN7s4xSzc58JAoFPu5q5BVv0Yiio yWSC2jhtvqqOJp8316HaheIhfkkSEX9qxq7hZghSo+oPwzp1uBn0b8AwuzRAxQD1OZnms X-Google-Smtp-Source: AGHT+IHyygeSxJyNs1AB/t8h2oWE3zrmWEnc+tMnMhN05YyXp5Tg5F9O0X+SggrM6je5HiwFGxlS9nrxUkLK1wXEwpM= X-Received: by 2002:a05:600c:5101:b0:434:a1d3:a331 with SMTP id 5b1f17b1804b1-4361c3e2350mr42431505e9.22.1734000695440; Thu, 12 Dec 2024 02:51:35 -0800 (PST) Precedence: bulk X-Mailing-List: rust-for-linux@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20241101060237.1185533-1-boqun.feng@gmail.com> <20241101060237.1185533-3-boqun.feng@gmail.com> In-Reply-To: <20241101060237.1185533-3-boqun.feng@gmail.com> From: Alice Ryhl Date: Thu, 12 Dec 2024 11:51:23 +0100 Message-ID: Subject: Re: [RFC v2 02/13] rust: sync: Add basic atomic operation mapping framework To: Boqun Feng Cc: 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, Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Gary Guo , =?UTF-8?Q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , 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 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Fri, Nov 1, 2024 at 7:03=E2=80=AFAM Boqun Feng wr= ote: > > Preparation for generic atomic implementation. To unify the > ipmlementation of a generic method over `i32` and `i64`, the C side > atomic methods need to be grouped so that in a generic method, they can > be referred as ::, otherwise their parameters and return > value are different between `i32` and `i64`, which would require using > `transmute()` to unify the type into a `T`. > > Introduce `AtomicIpml` to represent a basic type in Rust that has the > direct mapping to an atomic implementation from C. This trait is sealed, > and currently only `i32` and `i64` ipml this. There seems to be quite a few instances of "impl" spelled as "ipml" here. > Further, different methods are put into different `*Ops` trait groups, > and this is for the future when smaller types like `i8`/`i16` are > supported but only with a limited set of API (e.g. only set(), load(), > xchg() and cmpxchg(), no add() or sub() etc). > > While the atomic mod is introduced, documentation is also added for > memory models and data races. > > Also bump my role to the maintainer of ATOMIC INFRASTRUCTURE to reflect > my responsiblity on the Rust atomic mod. > > Signed-off-by: Boqun Feng > --- > MAINTAINERS | 4 +- > rust/kernel/sync.rs | 1 + > rust/kernel/sync/atomic.rs | 19 ++++ > rust/kernel/sync/atomic/ops.rs | 199 +++++++++++++++++++++++++++++++++ > 4 files changed, 222 insertions(+), 1 deletion(-) > create mode 100644 rust/kernel/sync/atomic.rs > create mode 100644 rust/kernel/sync/atomic/ops.rs > > diff --git a/MAINTAINERS b/MAINTAINERS > index b77f4495dcf4..e09471027a63 100644 > --- a/MAINTAINERS > +++ b/MAINTAINERS > @@ -3635,7 +3635,7 @@ F: drivers/input/touchscreen/atmel_mxt_ts.c > ATOMIC INFRASTRUCTURE > M: Will Deacon > M: Peter Zijlstra > -R: Boqun Feng > +M: Boqun Feng > R: Mark Rutland > L: linux-kernel@vger.kernel.org > S: Maintained > @@ -3644,6 +3644,8 @@ F: arch/*/include/asm/atomic*.h > F: include/*/atomic*.h > F: include/linux/refcount.h > F: scripts/atomic/ > +F: rust/kernel/sync/atomic.rs > +F: rust/kernel/sync/atomic/ This is why mod.rs files are superior :) > @@ -0,0 +1,19 @@ > +// SPDX-License-Identifier: GPL-2.0 > + > +//! Atomic primitives. > +//! > +//! These primitives have the same semantics as their C counterparts: an= d the precise definitions of > +//! semantics can be found at [`LKMM`]. Note that Linux Kernel Memory (C= onsistency) Model is the > +//! only model for Rust code in kernel, and Rust's own atomics should be= avoided. > +//! > +//! # Data races > +//! > +//! [`LKMM`] atomics have different rules regarding data races: > +//! > +//! - A normal read doesn't data-race with an atomic read. This was fixed: https://github.com/rust-lang/rust/pull/128778 > +mod private { > + /// Sealed trait marker to disable customized impls on atomic implem= entation traits. > + pub trait Sealed {} > +} Just make the trait unsafe? Alice