Received: by 2002:a89:413:0:b0:1fd:dba5:e537 with SMTP id m19csp716733lqs; Fri, 14 Jun 2024 03:48:43 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCX1nmgSEY+eqHjIyHQbgQutlYJ5PGduhwt2I3x3VfJb7bDMyycXoR93LYzlZp4d7x9SK7kNNGvG4qALiouQGGo4iFdFoSxk+X+gbE6Ytw== X-Google-Smtp-Source: AGHT+IHhIdh19zKjTDTu0X3ft43ffG2zBXMxmwjTj+9HZFm+Nrr1r4eu4Rs2M3chBBo0QFfwc3Ut X-Received: by 2002:a50:d4c5:0:b0:57c:5874:4f74 with SMTP id 4fb4d7f45d1cf-57cbd69e7ecmr1348261a12.28.1718362123062; Fri, 14 Jun 2024 03:48:43 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1718362123; cv=pass; d=google.com; s=arc-20160816; b=b/2JXBvBnmQuGESlVS81XJNSF2yMob+pMubyJnX8eBQc9PLuIOGK64qBCyBRBU0xMi ApJMlAFu5+ezCpvr2Wfl3ID1cF36hFocE6A2Ob/lJZa250JzFNfHMUJrHe8D5/Hii5oZ xdh9RGBZPMQXwoQAKDM8FJuM50+kmxNo9H0AgAiliFM/QqO+Q0kLbn23FJrHT1e8je33 l2JknT4k7adyaVyv5S2YfOkfEVp8Scm5dKt/gaBQ/4ZArux6lqb03piXDCID/eXCUpOP 3A/v/fVd/krx2twPlbS/q8QqOmOi1Egsztbv34/XZzRdZ8EJ9OEigfDOAnoZcGK8SB9x KxNA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:date; bh=szOmQCYztl4J9m4j/Llg2C/BpBkQvvulyERZmGvdSRs=; fh=ntTAZX4PTnAGrhGn4J7adVlQM4PL5Z4oztw7WpjKpYE=; b=e9Z7N7UTjD6q8zvhwqCxr41TO92owwO5NzeXaTRUB0IXx+bvoxVcqWZ2c1SC8mgZge DeX41hklg/+8vii4liw+yftF9xjYdG/SU4TZVMAxoPmeRuLd21arnKkaeI6LG05GsIxk CT4pvJBqXvoYpd7vnvlXKPLaf3qt78SkCg6DkQwm1cCHBxt2VvdMYBPekV3ko7hjZkaT RjjESw7Xo/3aPuSstm3MjDLbJ5xO8+TJfkKxL4MzwQSGXdgWegs21hG+08uchRS0nTu1 t+4ICTJL/qKQNsdzp2m0GkR8KcMG9X9LnV+pZRXNUsodcZlGqLLT/zYer1pgF0KWwBGJ iQAQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=arm.com dmarc=pass fromdomain=arm.com); spf=pass (google.com: domain of linux-kernel+bounces-214806-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-214806-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id 4fb4d7f45d1cf-57cb745fb6dsi1651030a12.536.2024.06.14.03.48.42 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Jun 2024 03:48:43 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-214806-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=arm.com dmarc=pass fromdomain=arm.com); spf=pass (google.com: domain of linux-kernel+bounces-214806-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-214806-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 4E4CC1F2B99B for ; Fri, 14 Jun 2024 10:41:17 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 356851946C4; Fri, 14 Jun 2024 10:41:05 +0000 (UTC) Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 5A8F31946A7; Fri, 14 Jun 2024 10:41:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718361664; cv=none; b=Sx5l7sE2zfq1/c30R0zmXkpDbzgbGGkJN9C1M1VoLJlSnQXpkjoEXjm+f8TIc41WnBAhDzMSQ5ncdmmjxYWyNJIhqXHgPzXHR6zsZVPZII3MzxyWTqA9+Yrp1jsqJONyNgFo/JHpy9v0TOUg03LxRNgg2Njqgxv2VhZdoGAyEKA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718361664; c=relaxed/simple; bh=AmUgqvZgbKj2NQIuvs3Dt8TOXhMLfPmQOpUxQzf8iYk=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=Evrx9fNGU2jVeYxwLjvjMGFXC+H+z4aLP/LKiZRCe0CFw0uL1wELo5Zx8soyJQHg5iLj5qgP+iZZVBuGOvIhos/UwR12rWu/21tDb8u/q3gPn+dZaRZYaVyKwEPIP94yRxuKtTzjHnBCQOArvPUW6OdhdcHHzatf/KQCwbRrX34= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 1476B1480; Fri, 14 Jun 2024 03:41:20 -0700 (PDT) Received: from J2N7QTR9R3.cambridge.arm.com (usa-sjc-imap-foss1.foss.arm.com [10.121.207.14]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id B4E003F5A1; Fri, 14 Jun 2024 03:40:49 -0700 (PDT) Date: Fri, 14 Jun 2024 11:40:47 +0100 From: Mark Rutland To: Boqun Feng Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arch@vger.kernel.org, llvm@lists.linux.dev, Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Gary Guo , =?utf-8?B?QmrDtnJu?= Roy Baron , 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, 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 Subject: Re: [RFC 2/2] rust: sync: Add atomic support Message-ID: References: <20240612223025.1158537-1-boqun.feng@gmail.com> <20240612223025.1158537-3-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-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20240612223025.1158537-3-boqun.feng@gmail.com> On Wed, Jun 12, 2024 at 03:30:25PM -0700, Boqun Feng wrote: > Provide two atomic types: AtomicI32 and AtomicI64 with the existing > implemenation of C atomics. These atomics have the same semantics of the > corresponding LKMM C atomics, and using one memory (ordering) model > certainly reduces the reasoning difficulty and potential bugs from the > interaction of two different memory models. > > Also bump my role to the maintainer of ATOMIC INFRASTRUCTURE to reflect > my responsiblity on these Rust APIs. > > Note that `Atomic*::new()`s are implemented vi open coding on struct > atomic*_t. This allows `new()` being a `const` function, so that it can > be used in constant contexts. > > Signed-off-by: Boqun Feng I have a few minor comments below. > --- > MAINTAINERS | 4 +- > arch/arm64/kernel/cpufeature.c | 2 + > rust/kernel/sync.rs | 1 + > rust/kernel/sync/atomic.rs | 63 ++ > rust/kernel/sync/atomic/impl.rs | 1375 +++++++++++++++++++++++++++++ > scripts/atomic/gen-atomics.sh | 1 + > scripts/atomic/gen-rust-atomic.sh | 136 +++ > 7 files changed, 1581 insertions(+), 1 deletion(-) > create mode 100644 rust/kernel/sync/atomic.rs > create mode 100644 rust/kernel/sync/atomic/impl.rs > create mode 100755 scripts/atomic/gen-rust-atomic.sh > > diff --git a/MAINTAINERS b/MAINTAINERS > index d6c90161c7bf..a8528d27b260 100644 > --- a/MAINTAINERS > +++ b/MAINTAINERS > @@ -3458,7 +3458,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 > @@ -3467,6 +3467,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/ > > ATTO EXPRESSSAS SAS/SATA RAID SCSI DRIVER > M: Bradley Grove > diff --git a/arch/arm64/kernel/cpufeature.c b/arch/arm64/kernel/cpufeature.c > index 48e7029f1054..99e6e2b2867f 100644 > --- a/arch/arm64/kernel/cpufeature.c > +++ b/arch/arm64/kernel/cpufeature.c > @@ -1601,6 +1601,8 @@ static bool > has_cpuid_feature(const struct arm64_cpu_capabilities *entry, int scope) > { > u64 val = read_scoped_sysreg(entry, scope); > + if (entry->capability == ARM64_HAS_LSE_ATOMICS) > + return false; > return feature_matches(val, entry); > } As per other replies, this'll obviously need to go. > diff --git a/rust/kernel/sync/atomic.rs b/rust/kernel/sync/atomic.rs > new file mode 100644 > index 000000000000..b0f852cf1741 > --- /dev/null > +++ b/rust/kernel/sync/atomic.rs > @@ -0,0 +1,63 @@ > +// SPDX-License-Identifier: GPL-2.0 > + > +//! Atomic primitives. > +//! > +//! These primitives have the same semantics as their C counterparts, for precise definitions of > +//! the semantics, please refer to tools/memory-model. Note that Linux Kernel Memory (Consistency) > +//! Model is the only model for Rust development in kernel right now, please avoid to use Rust's > +//! own atomics. > + > +use crate::bindings::{atomic64_t, atomic_t}; As with the last patch, why no atomic_long_t? [...] > +#gen_proto_order_variant(meta, pfx, name, sfx, order, atomic, ty, int, raw, arg...) > +gen_proto_order_variant() > +{ > + local meta="$1"; shift > + local pfx="$1"; shift > + local name="$1"; shift > + local sfx="$1"; shift > + local order="$1"; shift > + local atomic="$1"; shift > + local ty="$1"; shift > + local int="$1"; shift > + local raw="$1"; shift > + > + local fn_name="${raw}${pfx}${name}${sfx}${order}" > + local atomicname="${raw}${atomic}_${pfx}${name}${sfx}${order}" > + > + local ret="$(gen_ret_type "${meta}" "${int}")" > + local params="$(gen_params "${int}" $@)" > + local args="$(gen_args "$@")" > + local retstmt="$(gen_ret_stmt "${meta}")" > + > +cat < + /// See \`${atomicname}\`. > + #[inline(always)] > + pub fn ${fn_name}(&self${params}) ${ret}{ > + // SAFETY:\`self.0.get()\` is a valid pointer. > + unsafe { > + ${retstmt}${atomicname}(${args}); > + } > + } > +EOF > +} AFAICT the 'ty' argument (AtomicI32/AtomicI64) isn't used and can be removed. Likewise for 'raw'. > + > +cat << EOF > +// SPDX-License-Identifier: GPL-2.0 > + > +//! Generated by $0 > +//! DO NOT MODIFY THIS FILE DIRECTLY > + > +use super::*; > +use crate::bindings::*; > + > +impl AtomicI32 { > +EOF > + > +grep '^[a-z]' "$1" | while read name meta args; do > + gen_proto "${meta}" "${name}" "atomic" "AtomicI32" "i32" "" ${args} With 'ty' and 'raw' gone, this'd be: gen_proto "${meta}" "${name}" "atomic" "i32" ${args} > +done > + > +cat << EOF > +} > + > +impl AtomicI64 { > +EOF > + > +grep '^[a-z]' "$1" | while read name meta args; do > + gen_proto "${meta}" "${name}" "atomic64" "AtomicI64" "i64" "" ${args} With 'ty' and 'raw' gone, this'd be: gen_proto "${meta}" "${name}" "atomic64" "i64" ${args} Mark. > +done > + > +cat << EOF > +} > + > +EOF > -- > 2.45.2 >