Re: locking/atomic: Introduce atomic_try_cmpxchg()
From: Peter Zijlstra
Date: Fri Mar 24 2017 - 17:25:08 EST
On Fri, Mar 24, 2017 at 12:17:28PM -0700, Linus Torvalds wrote:
> On Fri, Mar 24, 2017 at 11:45 AM, Andy Lutomirski <luto@xxxxxxxxxxxxxx> wrote:
> >
> > Is there some hack like if __builtin_is_unescaped(*val) *val = old;
> > that would work?
>
> See my recent email suggesting a completely different interface, which
> avoids this problem.
>
> My interface generates:
>
> 0000000000000000 <T_refcount_inc>:
> 0: 8b 07 mov (%rdi),%eax
> 2: 83 f8 ff cmp $0xffffffff,%eax
> 5: 74 12 je 19 <T_refcount_inc+0x19>
> 7: 85 c0 test %eax,%eax
> 9: 74 0a je 15 <T_refcount_inc+0x15>
> b: 8d 50 01 lea 0x1(%rax),%edx
> e: f0 0f b1 17 lock cmpxchg %edx,(%rdi)
> 12: 75 ee jne 2 <T_refcount_inc+0x2>
> 14: c3 retq
> 15: 31 c0 xor %eax,%eax
> 17: 0f 0b ud2
> 19: c3 retq
>
> for PeterZ's test-case, which seems optimal.
Right; now my GCC emits more or less the same code (its a slightly
different compiler and instead of 12: jne, it does: 12 je ; 14: jmp 2.
But maybe that's the likely() you added later.
Also, see how at 7 we test if eax is 0 and then at 9 jump to 15 where we
make eax 0. Pretty daft code-gen.
In any case, you lost one branch into ud2; your success: return, should
be success: if (new == UINT_MAX), such that when we newly saturate the
count we also raise an exception.
With that, the code is still larger than it used to be. I'll have a play
around. I do like this interface better, but getting GCC to generate
sensible code seems 'interesting'.
I'll try and redo the patches that landed in tip and see what it does
for total vmlinux size somewhere tomorrow.