Re: [RFC 2/2] rust: sync: Add atomic support

From: Mark Rutland
Date: Fri Jun 14 2024 - 06:41:17 EST


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 <boqun.feng@xxxxxxxxx>

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 <will@xxxxxxxxxx>
> M: Peter Zijlstra <peterz@xxxxxxxxxxxxx>
> -R: Boqun Feng <boqun.feng@xxxxxxxxx>
> +M: Boqun Feng <boqun.feng@xxxxxxxxx>
> R: Mark Rutland <mark.rutland@xxxxxxx>
> L: linux-kernel@xxxxxxxxxxxxxxx
> 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 <linuxdrivers@xxxxxxxxxxxx>
> 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 <<EOF
> + /// 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
>