Skip to content

Commit a9ebf30

Browse files
Peter Zijlstraingomolnar
authored andcommitted
locking/atomic: Introduce atomic_try_cmpxchg()
Add a new cmpxchg interface: bool try_cmpxchg(u{8,16,32,64} *ptr, u{8,16,32,64} *val, u{8,16,32,64} new); Where the boolean returns the result of the compare; and thus if the exchange happened; and in case of failure, the new value of *ptr is returned in *val. This allows simplification/improvement of loops like: for (;;) { new = val $op $imm; old = cmpxchg(ptr, val, new); if (old == val) break; val = old; } into: do { } while (!try_cmpxchg(ptr, &val, val $op $imm)); while also generating better code (GCC6 and onwards). Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Cc: Andrew Morton <akpm@linux-foundation.org> Cc: Andy Lutomirski <luto@kernel.org> Cc: Borislav Petkov <bp@alien8.de> Cc: Brian Gerst <brgerst@gmail.com> Cc: Denys Vlasenko <dvlasenk@redhat.com> Cc: H. Peter Anvin <hpa@zytor.com> Cc: Josh Poimboeuf <jpoimboe@redhat.com> 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 Signed-off-by: Ingo Molnar <mingo@kernel.org>
1 parent bf7b3ac commit a9ebf30

4 files changed

Lines changed: 123 additions & 0 deletions

File tree

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,12 @@ static __always_inline int atomic_cmpxchg(atomic_t *v, int old, int new)
186186
return cmpxchg(&v->counter, old, new);
187187
}
188188

189+
#define atomic_try_cmpxchg atomic_try_cmpxchg
190+
static __always_inline bool atomic_try_cmpxchg(atomic_t *v, int *old, int new)
191+
{
192+
return try_cmpxchg(&v->counter, old, new);
193+
}
194+
189195
static inline int atomic_xchg(atomic_t *v, int new)
190196
{
191197
return xchg(&v->counter, new);

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,12 @@ static inline long atomic64_cmpxchg(atomic64_t *v, long old, long new)
176176
return cmpxchg(&v->counter, old, new);
177177
}
178178

179+
#define atomic64_try_cmpxchg atomic64_try_cmpxchg
180+
static __always_inline bool atomic64_try_cmpxchg(atomic64_t *v, long *old, long new)
181+
{
182+
return try_cmpxchg(&v->counter, old, new);
183+
}
184+
179185
static inline long atomic64_xchg(atomic64_t *v, long new)
180186
{
181187
return xchg(&v->counter, new);

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

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,75 @@ extern void __add_wrong_size(void)
153153
#define cmpxchg_local(ptr, old, new) \
154154
__cmpxchg_local(ptr, old, new, sizeof(*(ptr)))
155155

156+
157+
#define __raw_try_cmpxchg(_ptr, _pold, _new, size, lock) \
158+
({ \
159+
bool success; \
160+
__typeof__(_ptr) _old = (_pold); \
161+
__typeof__(*(_ptr)) __old = *_old; \
162+
__typeof__(*(_ptr)) __new = (_new); \
163+
switch (size) { \
164+
case __X86_CASE_B: \
165+
{ \
166+
volatile u8 *__ptr = (volatile u8 *)(_ptr); \
167+
asm volatile(lock "cmpxchgb %[new], %[ptr]" \
168+
CC_SET(z) \
169+
: CC_OUT(z) (success), \
170+
[ptr] "+m" (*__ptr), \
171+
[old] "+a" (__old) \
172+
: [new] "q" (__new) \
173+
: "memory"); \
174+
break; \
175+
} \
176+
case __X86_CASE_W: \
177+
{ \
178+
volatile u16 *__ptr = (volatile u16 *)(_ptr); \
179+
asm volatile(lock "cmpxchgw %[new], %[ptr]" \
180+
CC_SET(z) \
181+
: CC_OUT(z) (success), \
182+
[ptr] "+m" (*__ptr), \
183+
[old] "+a" (__old) \
184+
: [new] "r" (__new) \
185+
: "memory"); \
186+
break; \
187+
} \
188+
case __X86_CASE_L: \
189+
{ \
190+
volatile u32 *__ptr = (volatile u32 *)(_ptr); \
191+
asm volatile(lock "cmpxchgl %[new], %[ptr]" \
192+
CC_SET(z) \
193+
: CC_OUT(z) (success), \
194+
[ptr] "+m" (*__ptr), \
195+
[old] "+a" (__old) \
196+
: [new] "r" (__new) \
197+
: "memory"); \
198+
break; \
199+
} \
200+
case __X86_CASE_Q: \
201+
{ \
202+
volatile u64 *__ptr = (volatile u64 *)(_ptr); \
203+
asm volatile(lock "cmpxchgq %[new], %[ptr]" \
204+
CC_SET(z) \
205+
: CC_OUT(z) (success), \
206+
[ptr] "+m" (*__ptr), \
207+
[old] "+a" (__old) \
208+
: [new] "r" (__new) \
209+
: "memory"); \
210+
break; \
211+
} \
212+
default: \
213+
__cmpxchg_wrong_size(); \
214+
} \
215+
*_old = __old; \
216+
success; \
217+
})
218+
219+
#define __try_cmpxchg(ptr, pold, new, size) \
220+
__raw_try_cmpxchg((ptr), (pold), (new), (size), LOCK_PREFIX)
221+
222+
#define try_cmpxchg(ptr, pold, new) \
223+
__try_cmpxchg((ptr), (pold), (new), sizeof(*(ptr)))
224+
156225
/*
157226
* xadd() adds "inc" to "*ptr" and atomically returns the previous
158227
* value of "*ptr".

‎include/linux/atomic.h‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,27 @@
423423
#endif
424424
#endif /* atomic_cmpxchg_relaxed */
425425

426+
#ifndef atomic_try_cmpxchg
427+
428+
#define __atomic_try_cmpxchg(type, _p, _po, _n) \
429+
({ \
430+
typeof(_po) __po = (_po); \
431+
typeof(*(_po)) __o = *__po; \
432+
*__po = atomic_cmpxchg##type((_p), __o, (_n)); \
433+
(*__po == __o); \
434+
})
435+
436+
#define atomic_try_cmpxchg(_p, _po, _n) __atomic_try_cmpxchg(, _p, _po, _n)
437+
#define atomic_try_cmpxchg_relaxed(_p, _po, _n) __atomic_try_cmpxchg(_relaxed, _p, _po, _n)
438+
#define atomic_try_cmpxchg_acquire(_p, _po, _n) __atomic_try_cmpxchg(_acquire, _p, _po, _n)
439+
#define atomic_try_cmpxchg_release(_p, _po, _n) __atomic_try_cmpxchg(_release, _p, _po, _n)
440+
441+
#else /* atomic_try_cmpxchg */
442+
#define atomic_try_cmpxchg_relaxed atomic_try_cmpxchg
443+
#define atomic_try_cmpxchg_acquire atomic_try_cmpxchg
444+
#define atomic_try_cmpxchg_release atomic_try_cmpxchg
445+
#endif /* atomic_try_cmpxchg */
446+
426447
/* cmpxchg_relaxed */
427448
#ifndef cmpxchg_relaxed
428449
#define cmpxchg_relaxed cmpxchg
@@ -996,6 +1017,27 @@ static inline int atomic_dec_if_positive(atomic_t *v)
9961017
#endif
9971018
#endif /* atomic64_cmpxchg_relaxed */
9981019

1020+
#ifndef atomic64_try_cmpxchg
1021+
1022+
#define __atomic64_try_cmpxchg(type, _p, _po, _n) \
1023+
({ \
1024+
typeof(_po) __po = (_po); \
1025+
typeof(*(_po)) __o = *__po; \
1026+
*__po = atomic64_cmpxchg##type((_p), __o, (_n)); \
1027+
(*__po == __o); \
1028+
})
1029+
1030+
#define atomic64_try_cmpxchg(_p, _po, _n) __atomic64_try_cmpxchg(, _p, _po, _n)
1031+
#define atomic64_try_cmpxchg_relaxed(_p, _po, _n) __atomic64_try_cmpxchg(_relaxed, _p, _po, _n)
1032+
#define atomic64_try_cmpxchg_acquire(_p, _po, _n) __atomic64_try_cmpxchg(_acquire, _p, _po, _n)
1033+
#define atomic64_try_cmpxchg_release(_p, _po, _n) __atomic64_try_cmpxchg(_release, _p, _po, _n)
1034+
1035+
#else /* atomic64_try_cmpxchg */
1036+
#define atomic64_try_cmpxchg_relaxed atomic64_try_cmpxchg
1037+
#define atomic64_try_cmpxchg_acquire atomic64_try_cmpxchg
1038+
#define atomic64_try_cmpxchg_release atomic64_try_cmpxchg
1039+
#endif /* atomic64_try_cmpxchg */
1040+
9991041
#ifndef atomic64_andnot
10001042
static inline void atomic64_andnot(long long i, atomic64_t *v)
10011043
{

0 commit comments

Comments
 (0)