Re: locking/atomic: Introduce atomic_try_cmpxchg()

From: Peter Zijlstra
Date: Mon Mar 27 2017 - 05:53:38 EST


On Sat, Mar 25, 2017 at 03:08:10PM -0700, Linus Torvalds wrote:
> On Sat, Mar 25, 2017 at 2:13 PM, Peter Zijlstra <peterz@xxxxxxxxxxxxx> wrote:
> > On Sat, Mar 25, 2017 at 11:34:32AM -0700, Linus Torvalds wrote:
> >>
> >> Oh, I just noticed that at least your other one didn't mark "success"
> >> as being likely.
> >
> > 10730509 4540256 843776 16114541 f5e36d defconfig-build/vmlinux
>
> Ok, that seems to be the exact same size as with the patch using the
> "goto label" approach. So maybe the code generation is the same now.


OK, so I went and build myself a GCC-7 compiler and constructed the
below table. From this I would propose we do the "try_cmpxchg + if"
thing, also below. Because, while the interface is icky, it is what C11
does for this construct.


GCC-6.3.0:

10735757 (cmpxchg)
10726413 (try_cmpxchg)
10730701 (try_cmpxchg + likely)
10730509 (try_cmpxchg + if)
10730445 (try_cmpxchg-linus)

GCC-7 (20170327):

10709514 (cmpxchg)
10704266 (try_cmpxchg)
10704458 (try_cmpxchg + likely)
10704266 (try_cmpxchg + if)
10704394 (try_cmpxchg-linus)



---
arch/x86/include/asm/cmpxchg.h | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/arch/x86/include/asm/cmpxchg.h b/arch/x86/include/asm/cmpxchg.h
index fb961db..d90296d 100644
--- a/arch/x86/include/asm/cmpxchg.h
+++ b/arch/x86/include/asm/cmpxchg.h
@@ -212,8 +212,9 @@ extern void __add_wrong_size(void)
default: \
__cmpxchg_wrong_size(); \
} \
- *_old = __old; \
- success; \
+ if (unlikely(!success)) \
+ *_old = __old; \
+ likely(success); \
})

#define __try_cmpxchg(ptr, pold, new, size) \