Re: [PATCH v5 05/10] rust: sync: atomic: Add atomic {cmp,}xchg operations

Andreas Hindborg
2025-06-30T10:16:56Z

"Boqun Feng" <boqun.feng@gmail.com> writes:

> On Thu, Jun 26, 2025 at 03:12:12PM +0200, Andreas Hindborg wrote:
>> "Boqun Feng" <boqun.feng@gmail.com> writes:
>>
>> > xchg() and cmpxchg() are basic operations on atomic. Provide these based
>> > on C APIs.
>> >
>> > Note that cmpxchg() use the similar function signature as
>> > compare_exchange() in Rust std: returning a `Result`, `Ok(old)` means
>> > the operation succeeds and `Err(old)` means the operation fails.
>> >
>> > Signed-off-by: Boqun Feng <boqun.feng@gmail.com>
>> > ---
>> > rust/kernel/sync/atomic/generic.rs | 154 +++++++++++++++++++++++++++++
>> > 1 file changed, 154 insertions(+)
>> >
>> > diff --git a/rust/kernel/sync/atomic/generic.rs b/rust/kernel/sync/atomic/generic.rs
>> > index 73c26f9cf6b8..bcdbeea45dd8 100644
>> > --- a/rust/kernel/sync/atomic/generic.rs
>> > +++ b/rust/kernel/sync/atomic/generic.rs
>> > @@ -256,3 +256,157 @@ pub fn store<Ordering: ReleaseOrRelaxed>(&self, v: T, _: Ordering) {
>> > };
>> > }
>> > }
>> > +
>> > +impl<T: AllowAtomic> Atomic<T>
>> > +where
>> > + T::Repr: AtomicHasXchgOps,
>> > +{
>> > + /// Atomic exchange.
>> > + ///
>> > + /// # Examples
>> > + ///
>> > + /// ```rust
>> > + /// use kernel::sync::atomic::{Atomic, Acquire, Relaxed};
>> > + ///
>> > + /// let x = Atomic::new(42);
>> > + ///
>> > + /// assert_eq!(42, x.xchg(52, Acquire));
>> > + /// assert_eq!(52, x.load(Relaxed));
>> > + /// ```
>> > + #[doc(alias("atomic_xchg", "atomic64_xchg"))]
>> > + #[inline(always)]
>> > + pub fn xchg<Ordering: All>(&self, v: T, _: Ordering) -> T {
>> > + let v = T::into_repr(v);
>> > + let a = self.as_ptr().cast::<T::Repr>();
>> > +
>> > + // SAFETY:
>> > + // - For calling the atomic_xchg*() function:
>> > + // - `self.as_ptr()` is a valid pointer, and per the safety requirement of `AllocAtomic`,
>>
>> Typo: `AllowAtomic`.
>>
>
> Fixed.
>
>> > + // a `*mut T` is a valid `*mut T::Repr`. Therefore `a` is a valid pointer,
>> > + // - per the type invariants, the following atomic operation won't cause data races.
>> > + // - For extra safety requirement of usage on pointers returned by `self.as_ptr():
>> > + // - atomic operations are used here.
>> > + let ret = unsafe {
>> > + match Ordering::TYPE {
>> > + OrderingType::Full => T::Repr::atomic_xchg(a, v),
>> > + OrderingType::Acquire => T::Repr::atomic_xchg_acquire(a, v),
>> > + OrderingType::Release => T::Repr::atomic_xchg_release(a, v),
>> > + OrderingType::Relaxed => T::Repr::atomic_xchg_relaxed(a, v),
>> > + }
>> > + };
>> > +
>> > + T::from_repr(ret)
>> > + }
>> > +
>> > + /// Atomic compare and exchange.
>> > + ///
>> > + /// Compare: The comparison is done via the byte level comparison between the atomic variables
>> > + /// with the `old` value.
>> > + ///
>> > + /// Ordering: When succeeds, provides the corresponding ordering as the `Ordering` type
>> > + /// parameter indicates, and a failed one doesn't provide any ordering, the read part of a
>> > + /// failed cmpxchg should be treated as a relaxed read.
>>
>> Rust `core::ptr` functions have this sentence on success ordering for
>> compare_exchange:
>>
>> Using Acquire as success ordering makes the store part of this
>> operation Relaxed, and using Release makes the successful load
>> Relaxed.
>>
>> Does this translate to LKMM cmpxchg operations? If so, I think we should
>> include this sentence. This also applies to `Atomic::xchg`.
>>
>
> I see this as a different style of documenting, so in my next version,
> I have the following:
>
> //! - [`Acquire`] provides ordering between the load part of the annotated operation and all the
> //! following memory accesses.
> //! - [`Release`] provides ordering between all the preceding memory accesses and the store part of
> //! the annotated operation.
>
> in atomic/ordering.rs, I think I can extend it to:
>
> //! - [`Acquire`] provides ordering between the load part of the annotated operation and all the
> //! following memory accesses, and if there is a store part, it has Relaxed ordering.
> //! - [`Release`] provides ordering between all the preceding memory accesses and the store part of
> //! the annotated operation, and if there is load part, it has Relaxed ordering
>
> This aligns with what we usually describe things in tool/memory-model/.

Cool. When you start to go into details of ordering concepts, I feel
like something is missing though. For example for this sentence:

[`Release`] provides ordering between all the preceding memory
accesses and the store part of the annotated operation.

I guess this provided ordering is only guaranteed to be observable for
threads that read the same location with `Acquire` or stronger ordering?

If we start expanding on the orderings, rather than deferring to LKMM,
we should include this info.

Best regards,
Andreas Hindborg