Re: arch/x86/include/asm/cmpxchg_32.h:149:9: error: inline assembly requires more registers than available

From: Uros Bizjak
Date: Wed Jun 26 2024 - 05:33:32 EST


On Wed, Jun 26, 2024 at 11:28 AM Peter Zijlstra <peterz@xxxxxxxxxxxxx> wrote:
>
> On Tue, Jun 25, 2024 at 11:31:05AM -0700, Linus Torvalds wrote:
>
> > The thing is, when cmpxchg doesn't fail, then oldp should already be "old", no?
>
> Correct.
>
> > I mean, by the very definition, atomic_try_cmpxchg() can *not* be
> > successful if the new value didn't match the old one.
> >
> > I mean, just look at the very doc you point to - the "definition" is
> >
> > bool atomic_try_cmpxchg(atomic_t *ptr, int *oldp, int new)
> > {
> > int ret, old = *oldp;
> > ret = atomic_cmpxchg(ptr, old, new);
> > if (ret != old)
> > *oldp = ret;
> > return ret == old;
> > }
> >
> > iow, it only returns success of "ret == old", and "old" by definition
> > is "the contents of oldp".
> >
> > (Here "oldp" is a local variable, not something that can be changing).
> >
> > So I *think* the whole
> >
> > if (ret != old)
> > *oldp = ret;
> >
> > is actually counter-productive, and could/should be just that simpler
> > unconditional *oldp = ret,
>
> IIRC the reason I added that conditional is because at the time the GCC
> compiler I tried it on generated slightly better code like this.

Please see the thread at [1].

[1] https://lore.kernel.org/lkml/CACT4Y+bG+a0w6j6v1AmBE7fqqMSPyPEm4QimCzCouicmHT8FqA@xxxxxxxxxxxxxx/

Uros.

>
> ISTR it emitting some superfluous assignments with the unconditional
> store variant. Typically what seemed to happen is that since the
> cmpxchg() user would have a loop termination on ret == old, it was able
> to recognise it only needed that assignment in the failure case. Without
> the condition on it would also do that assignment in the success case.
>
> But yeah, otherwise it doesn't matter.