[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <DB93KSS3F3AK.3R9RFOHJ4FSAQ@kernel.org>
Date: Fri, 11 Jul 2025 10:53:45 +0200
From: "Benno Lossin" <lossin@...nel.org>
To: "Boqun Feng" <boqun.feng@...il.com>, <linux-kernel@...r.kernel.org>,
<rust-for-linux@...r.kernel.org>, <lkmm@...ts.linux.dev>,
<linux-arch@...r.kernel.org>
Cc: "Miguel Ojeda" <ojeda@...nel.org>, "Alex Gaynor"
<alex.gaynor@...il.com>, "Gary Guo" <gary@...yguo.net>,
Björn Roy Baron <bjorn3_gh@...tonmail.com>, "Andreas
Hindborg" <a.hindborg@...nel.org>, "Alice Ryhl" <aliceryhl@...gle.com>,
"Trevor Gross" <tmgross@...ch.edu>, "Danilo Krummrich" <dakr@...nel.org>,
"Will Deacon" <will@...nel.org>, "Peter Zijlstra" <peterz@...radead.org>,
"Mark Rutland" <mark.rutland@....com>, "Wedson Almeida Filho"
<wedsonaf@...il.com>, "Viresh Kumar" <viresh.kumar@...aro.org>, "Lyude
Paul" <lyude@...hat.com>, "Ingo Molnar" <mingo@...nel.org>, "Mitchell Levy"
<levymitchell0@...il.com>, "Paul E. McKenney" <paulmck@...nel.org>, "Greg
Kroah-Hartman" <gregkh@...uxfoundation.org>, "Linus Torvalds"
<torvalds@...ux-foundation.org>, "Thomas Gleixner" <tglx@...utronix.de>,
"Alan Stern" <stern@...land.harvard.edu>
Subject: Re: [PATCH v6 6/9] rust: sync: atomic: Add the framework of
arithmetic operations
On Thu Jul 10, 2025 at 8:00 AM CEST, Boqun Feng wrote:
> One important set of atomic operations is the arithmetic operations,
> i.e. add(), sub(), fetch_add(), add_return(), etc. However it may not
> make senses for all the types that `AllowAtomic` to have arithmetic
> operations, for example a `Foo(u32)` may not have a reasonable add() or
> sub(), plus subword types (`u8` and `u16`) currently don't have
> atomic arithmetic operations even on C side and might not have them in
> the future in Rust (because they are usually suboptimal on a few
> architecures). Therefore add a subtrait of `AllowAtomic` describing
> which types have and can do atomic arithemtic operations.
>
> Trait `AllowAtomicArithmetic` has an associate type `Delta` instead of
> using `AllowAllowAtomic::Repr` because, a `Bar(u32)` (whose `Repr` is
> `i32`) may not wants an `add(&self, i32)`, but an `add(&self, u32)`.
>
> Only add() and fetch_add() are added. The rest will be added in the
> future.
>
> Reviewed-by: Alice Ryhl <aliceryhl@...gle.com>
> Signed-off-by: Boqun Feng <boqun.feng@...il.com>
> ---
> rust/kernel/sync/atomic.rs | 18 +++++
> rust/kernel/sync/atomic/generic.rs | 108 +++++++++++++++++++++++++++++
> 2 files changed, 126 insertions(+)
I think it's better to name this trait `AtomicAdd` and make it generic:
pub unsafe trait AtomicAdd<Rhs = Self>: AllowAtomic {
fn rhs_into_repr(rhs: Rhs) -> Self::Repr;
}
`sub` and `fetch_sub` can be added using a similar trait.
The generic allows you to implement it multiple times with different
meanings, for example:
pub struct Nanos(u64);
pub struct Micros(u64);
pub struct Millis(u64);
impl AllowAtomic for Nanos {
type Repr = i64;
}
impl AtomicAdd<Millis> for Nanos {
fn rhs_into_repr(rhs: Millis) -> i64 {
transmute(rhs.0 * 1000_000)
}
}
impl AtomicAdd<Micros> for Nanos {
fn rhs_into_repr(rhs: Micros) -> i64 {
transmute(rhs.0 * 1000)
}
}
impl AtomicAdd<Nanos> for Nanos {
fn rhs_into_repr(rhs: Nanos) -> i64 {
transmute(rhs.0)
}
}
For the safety requirement on the `AtomicAdd` trait, we might just
require bi-directional transmutability... Or can you imagine a case
where that is not guaranteed, but a weaker form is?
---
Cheers,
Benno
Powered by blists - more mailing lists