Re: [PATCH] asm-generic/atomic: Add try_cmpxchg() fallbacks

From: Peter Zijlstra
Date: Thu Feb 20 2020 - 05:37:37 EST


On Thu, Feb 20, 2020 at 10:30:45AM +0000, Will Deacon wrote:
> On Tue, Feb 18, 2020 at 03:27:00PM +0100, Peter Zijlstra wrote:
> > On Tue, Feb 18, 2020 at 02:13:10PM +0100, Peter Zijlstra wrote:
> > > (with the caveat that try_cmpxchg() doesn't seem available on !x86 -- I
> > > should go fix that)
> >
> > Completely untested (lemme go do that shortly), but something like so I
> > suppose.
> >
> > ---
> > Subject: asm-generic/atomic: Add try_cmpxchg() fallbacks
> >
> > Only x86 provides try_cmpxchg() outside of the atomic_t interfaces,
> > provide generic fallbacks to create this interface from the widely
> > available cmpxchg() function.
> >
> > Signed-off-by: Peter Zijlstra (Intel) <peterz@xxxxxxxxxxxxx>
> > ---
> > diff --git a/include/linux/atomic-fallback.h b/include/linux/atomic-fallback.h
> > index 656b5489b673..243f61d6c35f 100644
> > --- a/include/linux/atomic-fallback.h
> > +++ b/include/linux/atomic-fallback.h
> > @@ -77,6 +77,50 @@
> >
> > #endif /* cmpxchg64_relaxed */
> >
> > +#ifndef try_cmpxchg
> > +#define try_cmpxchg(_ptr, _oldp, _new) \
> > +({ \
> > + typeof(*ptr) ___r, ___o = *(_oldp); \
>
> Probably worth pointing out that this will have the nasty behaviour
> for volatile pointers that we're tackling for READ_ONCE. Obviously no
> need to hold this up, but just mentioning it here in the hope that one
> of us remembers to fix it later on.

Right :/

> > diff --git a/scripts/atomic/gen-atomic-fallback.sh b/scripts/atomic/gen-atomic-fallback.sh
> > index b6c6f5d306a7..3c9be8d550e0 100755
> > --- a/scripts/atomic/gen-atomic-fallback.sh
> > +++ b/scripts/atomic/gen-atomic-fallback.sh
> > @@ -140,6 +140,32 @@ cat <<EOF
> > EOF
> > }
> >
> > +gen_try_cmpxchg_fallback()
> > +{
> > + local order="$1"; shift;
> > +
> > +cat <<EOF
> > +#ifndef try_cmpxchg${order}
> > +#define try_cmpxchg${order}(_ptr, _oldp, _new) \\
> > +({ \\
> > + typeof(*ptr) ___r, ___o = *(_oldp); \\
> > + ___r = cmpxchg${order}((_ptr), ___o, (_new)); \\
> > + if (unlikely(___r != ___o)) \\
> > + *(_old) = ___r; \\
>
> This doesn't compile because _old isn't declared. Probably best to avoid
> evaluating _oldp twice though.

Compiler had already spotted that, I'll make it something like:

typeof(*ptr) *___op = (_oldp), ___o = *___op;

...

*___op = ___r;

Which avoids the double eval.