[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20240613181910.74db809c@eugeo>
Date: Thu, 13 Jun 2024 18:19:10 +0100
From: Gary Guo <gary@...yguo.net>
To: Boqun Feng <boqun.feng@...il.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>, 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, Mark Rutland
<mark.rutland@....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 2/2] rust: sync: Add atomic support
On Thu, 13 Jun 2024 09:30:26 -0700
Boqun Feng <boqun.feng@...il.com> wrote:
> > > 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};
> > > +use crate::types::Opaque;
> > > +
> > > +mod r#impl;
> > > +
> > > +/// Atomic 32bit signed integers.
> > > +pub struct AtomicI32(Opaque<atomic_t>);
> > > +
> > > +/// Atomic 64bit signed integers.
> > > +pub struct AtomicI64(Opaque<atomic64_t>);
> >
> >
> > Can we avoid two types and use a generic `Atomic<T>` and then implement
> > on `Atomic<i32>` and `Atomic<i64>` instead? Like the recent
> > generic NonZero in Rust standard library or the atomic crate
> > (https://docs.rs/atomic/).
> >
>
> We can always add a layer on top of what we have here to provide the
> generic `Atomic<T>`. However, I personally don't think generic
> `Atomic<T>` is a good idea, for a few reasons:
>
> * I'm not sure it will bring benefits to users, the current atomic
> users in kernel are pretty specific on the size of atomic they
> use, so they want to directly use AtomicI32 or AtomicI64 in
> their type definitions rather than use a `Atomic<T>` where their
> users can provide type later.
You can write `Atomic<i32>` and `Atomic<i64>`.
>
> * I can also see the future where we have different APIs on
> different types of atomics, for example, we could have a:
>
> impl AtomicI64 {
> pub fn split(&self) -> (&AtomicI32, &AtomicI32)
> }
>
> which doesn't exist for AtomicI32. Note this is not a UB because
> we write our atomic implementation in asm, so it's perfectly
> fine for mix-sized atomics.
You can still have
impl Atomic<i64> {
pub fn split(&self) -> (&Atomic<i32>, &Atomic<i32>)
}
I see `Atomic<i32>/Atomic<i64>` being generally more flexible than
`AtomicI32/AtomicI64`, without very little downsides. We may not have
generic users but I think it doesn't do any harm to have it in a form
that makes future generics easy.
Best,
Gary
Powered by blists - more mailing lists