Re: [RFC 1/2] rust: Introduce atomic API helpers

From: Boqun Feng
Date: Fri Jun 14 2024 - 16:13:29 EST


On Fri, Jun 14, 2024 at 11:31:33AM +0100, Mark Rutland wrote:
> On Wed, Jun 12, 2024 at 03:30:24PM -0700, Boqun Feng wrote:
> > In order to support LKMM atomics in Rust, add rust_helper_* for atomic
> > APIs. These helpers ensure the implementation of LKMM atomics in Rust is
> > the same as in C. This could save the maintenance burden of having two
> > similar atomic implementations in asm.
> >
> > Originally-by: Mark Rutland <mark.rutland@xxxxxxx>
> > Signed-off-by: Boqun Feng <boqun.feng@xxxxxxxxx>
>
> FWIW, I'm happy with the concept; I have a couple of minor comments

;-)

> below.
>
> > ---
> > rust/atomic_helpers.h | 1035 +++++++++++++++++++++
> > rust/helpers.c | 2 +
> > scripts/atomic/gen-atomics.sh | 1 +
> > scripts/atomic/gen-rust-atomic-helpers.sh | 64 ++
> > 4 files changed, 1102 insertions(+)
> > create mode 100644 rust/atomic_helpers.h
> > create mode 100755 scripts/atomic/gen-rust-atomic-helpers.sh
>
> [...]
>
> > +#gen_proto_order_variant(meta, pfx, name, sfx, order, atomic, 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 int="$1"; shift
> > + local raw="$1"; shift
> > + local attrs="${raw:+noinstr }"
>
> You removed the 'raw_' atomic generation below, so you can drop the
> 'raw' parameter and the 'attrs' variable (both here and in the
> template)...
>
> > + local atomicname="${raw}${atomic}_${pfx}${name}${sfx}${order}"
> > +
> > + local ret="$(gen_ret_type "${meta}" "${int}")"
> > + local params="$(gen_params "${int}" "${atomic}" "$@")"
> > + local args="$(gen_args "$@")"
> > + local retstmt="$(gen_ret_stmt "${meta}")"
> > +
> > +cat <<EOF
> > +__rust_helper ${attrs}${ret}
>
> ... e.g. you can remove '${attrs}' here.
>
> [...]
>
> > +grep '^[a-z]' "$1" | while read name meta args; do
> > + gen_proto "${meta}" "${name}" "atomic" "int" "" ${args}
> > +done
> > +
> > +grep '^[a-z]' "$1" | while read name meta args; do
> > + gen_proto "${meta}" "${name}" "atomic64" "s64" "" ${args}
> > +done
>
> With the 'raw' parameter removed above, the '""' argument can be
> dropped.
>

Fix all above locally.

> Any reason to not have the atomic_long_*() API? It seems like an odd
> ommision.
>

See my reply to Peter, but there's also a more technical reason: right
now, we use core::ffi::c_long for bindgen to translate C's long. But
instead of `isize` (Rust's version of pointer-sized integer)
core::ffi::c_long is `i64` on 64bit and `i32` on 32bit. So right now,
atomic_long_add_return()'s helper signature would be (on 64bit):

extern "C" {
#[link_name="rust_helper_atomic_long_add_return"]
pub fn atomic_long_add_return(
i: i64,
v: *mut atomic_long_t,
) -> i64;
}

and I would need to cast the types in order to put it in an
`AtomicIsize` method:

impl AtomicIsize {
pub fn add_return(&self, i: isize) -> isize {
unsafe {
return atomic_long_add_return(i as i64, self.0.get()) as isize
}
}
}

see these two `as`s. I want to avoid handling them in a bash script ;-)

A better solution would be what Gary has:

https://github.com/nbdd0121/linux/commit/b604a43db56f149a90084fa8aed7988a8066894b

, which defines kernel's own ffi types and teach bindgen to pick the
right type for c_long. If we prefer script code generating to Rust macro
code generating, I will work with Gary on getting that done first and
then add atomic_long_t support unless we feel atomic_long_t support is
urgent.

Regards,
Boqun

> Mark.