Skip to content

Commit 44fe844

Browse files
Peter Zijlstraingomolnar
authored andcommitted
locking/atomic: Fix atomic_try_cmpxchg() semantics
Dmitry noted that the new atomic_try_cmpxchg() primitive is broken when the old pointer doesn't point to the local stack. He writes: "Consider a classical lock-free stack push: node->next = atomic_read(&head); do { } while (!atomic_try_cmpxchg(&head, &node->next, node)); This code is broken with the current implementation, the problem is with unconditional update of *__po. In case of success it writes the same value back into *__po, but in case of cmpxchg success we might have lose ownership of some memory locations and potentially over what __po has pointed to. The same holds for the re-read of *__po. " He also points out that this makes it surprisingly different from the similar C/C++ atomic operation. After investigating the code-gen differences caused by this patch; and a number of alternatives (Linus dislikes this interface lots), we arrived at these results (size x86_64-defconfig/vmlinux): GCC-6.3.0: 10735757 cmpxchg 10726413 try_cmpxchg 10730509 try_cmpxchg + patch 10730445 try_cmpxchg-linus GCC-7 (20170327): 10709514 cmpxchg 10704266 try_cmpxchg 10704266 try_cmpxchg + patch 10704394 try_cmpxchg-linus From this we see that the patch has the advantage of better code-gen on GCC-7 and keeps the interface roughly consistent with the C language variant. Reported-by: Dmitry Vyukov <dvyukov@google.com> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Cc: Andrew Morton <akpm@linux-foundation.org> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Paul E. McKenney <paulmck@linux.vnet.ibm.com> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: linux-kernel@vger.kernel.org Fixes: a9ebf30 ("locking/atomic: Introduce atomic_try_cmpxchg()") Signed-off-by: Ingo Molnar <mingo@kernel.org>
1 parent 8ce371f commit 44fe844

2 files changed

Lines changed: 13 additions & 8 deletions

File tree

‎arch/x86/include/asm/cmpxchg.h‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,9 @@ extern void __add_wrong_size(void)
212212
default: \
213213
__cmpxchg_wrong_size(); \
214214
} \
215-
*_old = __old; \
216-
success; \
215+
if (unlikely(!success)) \
216+
*_old = __old; \
217+
likely(success); \
217218
})
218219

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

‎include/linux/atomic.h‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -428,9 +428,11 @@
428428
#define __atomic_try_cmpxchg(type, _p, _po, _n) \
429429
({ \
430430
typeof(_po) __po = (_po); \
431-
typeof(*(_po)) __o = *__po; \
432-
*__po = atomic_cmpxchg##type((_p), __o, (_n)); \
433-
(*__po == __o); \
431+
typeof(*(_po)) __r, __o = *__po; \
432+
__r = atomic_cmpxchg##type((_p), __o, (_n)); \
433+
if (unlikely(__r != __o)) \
434+
*__po = __r; \
435+
likely(__r == __o); \
434436
})
435437

436438
#define atomic_try_cmpxchg(_p, _po, _n) __atomic_try_cmpxchg(, _p, _po, _n)
@@ -1022,9 +1024,11 @@ static inline int atomic_dec_if_positive(atomic_t *v)
10221024
#define __atomic64_try_cmpxchg(type, _p, _po, _n) \
10231025
({ \
10241026
typeof(_po) __po = (_po); \
1025-
typeof(*(_po)) __o = *__po; \
1026-
*__po = atomic64_cmpxchg##type((_p), __o, (_n)); \
1027-
(*__po == __o); \
1027+
typeof(*(_po)) __r, __o = *__po; \
1028+
__r = atomic64_cmpxchg##type((_p), __o, (_n)); \
1029+
if (unlikely(__r != __o)) \
1030+
*__po = __r; \
1031+
likely(__r == __o); \
10281032
})
10291033

10301034
#define atomic64_try_cmpxchg(_p, _po, _n) __atomic64_try_cmpxchg(, _p, _po, _n)

0 commit comments

Comments
 (0)