Re: [PATCH 2/6] locking/atomic/x86: Rewrite x86_32 arch_atomic64_{,fetch}_{and,or,xor}() functions

From: Uros Bizjak
Date: Tue Apr 09 2024 - 08:04:21 EST


On Tue, Apr 9, 2024 at 1:13 PM Mark Rutland <mark.rutland@xxxxxxx> wrote:

> > static __always_inline void arch_atomic64_and(s64 i, atomic64_t *v)
> > {
> > - s64 old, c = 0;
> > + s64 val = __READ_ONCE(v->counter);
>
> I reckon it's worth placing this in a helper with a big comment, e.g.
>
> static __always_inline s64 arch_atomic64_read_tearable(atomic64_t *v)
> {
> /*
> * TODO: explain that this might be torn, but it occurs *once*, and can
> * safely be consumed by atomic64_try_cmpxchg().
> *
> * TODO: point to the existing commentary regarding why we use
> * __READ_ONCE() for KASAN reasons.
> */
> return __READ_ONCE(v->counter);
> }
>
> ... and then use that in each of the instances below.
>
> That way the subtlety is clearly documented, and it'd more clearly align with
> the x86_64 verions.

This is an excellent idea. The separate definitions needs to be placed
in atomic64_32.h and atomic_64_64.h (due to use of atomic64_t
typedef), but it will allow the same unification of functions between
x64_32 and x64_64 as the approach with __READ_ONCE().

Thanks,
Uros.