[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <ZmykUtq45z0fGn26@boqun-archlinux>
Date: Fri, 14 Jun 2024 13:13:06 -0700
From: Boqun Feng <boqun.feng@...il.com>
To: Mark Rutland <mark.rutland@....com>
Cc: rust-for-linux@...r.kernel.org, linux-kernel@...r.kernel.org,
linux-arch@...r.kernel.org, llvm@...ts.linux.dev,
Miguel Ojeda <ojeda@...nel.org>, Alex Gaynor <alex.gaynor@...il.com>,
Wedson Almeida Filho <wedsonaf@...il.com>, Gary Guo <gary@...yguo.net>,
Björn Roy Baron <bjorn3_gh@...tonmail.com>,
Benno Lossin <benno.lossin@...ton.me>,
Andreas Hindborg <a.hindborg@...sung.com>,
Alice Ryhl <aliceryhl@...gle.com>,
Alan Stern <stern@...land.harvard.edu>,
Andrea Parri <parri.andrea@...il.com>, Will Deacon <will@...nel.org>,
Peter Zijlstra <peterz@...radead.org>,
Nicholas Piggin <npiggin@...il.com>, David Howells <dhowells@...hat.com>,
Jade Alglave <j.alglave@....ac.uk>, Luc Maranget <luc.maranget@...ia.fr>,
"Paul E. McKenney" <paulmck@...nel.org>,
Akira Yokosawa <akiyks@...il.com>, Daniel Lustig <dlustig@...dia.com>,
Joel Fernandes <joel@...lfernandes.org>,
Nathan Chancellor <nathan@...nel.org>,
Nick Desaulniers <ndesaulniers@...gle.com>, kent.overstreet@...il.com,
Greg Kroah-Hartman <gregkh@...uxfoundation.org>, elver@...gle.com,
Thomas Gleixner <tglx@...utronix.de>, Ingo Molnar <mingo@...hat.com>,
Borislav Petkov <bp@...en8.de>,
Dave Hansen <dave.hansen@...ux.intel.com>, x86@...nel.org,
"H. Peter Anvin" <hpa@...or.com>,
Catalin Marinas <catalin.marinas@....com>, torvalds@...ux-foundation.org,
linux-arm-kernel@...ts.infradead.org, linux-fsdevel@...r.kernel.org,
Trevor Gross <tmgross@...ch.edu>, dakr@...hat.com
Subject: Re: [RFC 1/2] rust: Introduce atomic API helpers
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@....com>
> > Signed-off-by: Boqun Feng <boqun.feng@...il.com>
>
> 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.
Powered by blists - more mailing lists