Re: [PATCH v6 6/9] rust: sync: atomic: Add the framework of arithmetic operations

Benno Lossin
2025-07-11T18:55:49Z

On Fri Jul 11, 2025 at 4:39 PM CEST, Boqun Feng wrote:
> On Fri, Jul 11, 2025 at 10:53:45AM +0200, Benno Lossin wrote:
>> 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@google.com>
>> > Signed-off-by: Boqun Feng <boqun.feng@gmail.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.
>>
>
> Seems a good idea, I will see what I can do. Thanks!
>
>> 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;

By the way, I find this a bit unfortunate... I think it would be nice to
be able to use `u64` and `u32` as reprs too.

Maybe we can add an additional trait `AtomicRepr` that gets implemented
by all integer types and then we can use that in the `Repr` instead.

This should definitely be a future patch series though.

>> }
>>
>> impl AtomicAdd<Millis> for Nanos {
>> fn rhs_into_repr(rhs: Millis) -> i64 {
>> transmute(rhs.0 * 1000_000)
>
> We probably want to use `as` in real code?

I thought that `as` would panic on over/underflow... But it doesn't and
indeed just converts between the two same-sized types.

By the way, should we ask for `Repr` to always be of the same size as
`Self` when implementing `AllowAtomic`?

That might already be implied from the round-trip transmutability:
* `Self` can't have a smaller size, because transmuting `Self` into
`Repr` would result in uninit bytes.
* `Repr` can't have a smaller size, because then transmuting a `Repr`
(that was once a `Self`) back into `Self` will result in uninit bytes

We probably should mention this in the docs somewhere?

>> }
>> }
>>
>> 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?
>
> I have a case that I don't think it's that useful, but it's similar to
> the `Micros` and `Millis` above, an `Even<T>` where `Even<i32>` is a
> `i32` but it's always an even number ;-) So transmute<i32, Even<i32>>()
> is not always sound. Maybe we could add a "TODO" in the safety section
> of `AtomicAdd`, and revisit this later? Like:
>
> /// (in # Safety)
> /// TODO: The safety requirement may be tightened to bi-directional
> /// transmutability.
>
> And maybe also add the `Even` example there?

Ahh that's interesting... I don't think the comment in the tightening
direction makes sense, either we start out with bi-directional
transmutability, or we don't do it at all.

I think an `Even` example is motivation enough to have it. So let's not
tighten it. But I think we should improve the safety requirement:

/// The valid bit patterns of `Self` must be a superset of the bit patterns reachable through
/// addition on any values of type [`Self::Repr`] obtained by transmuting values of type `Self`.

or

/// Adding any two values of type [`Self::Repr`] obtained through transmuting values of type `Self`
/// must yield a value with a bit pattern also valid for `Self`.

I feel like the second one sounds better.

Also is overflowing an atomic variable UB in LKMM? Because if it is,
then `struct MultipleOf<const M: u64>(u64)` is also something that would
be supported. Otherwise only powers of two would be supported.

---
Cheers,
Benno