Skip to content

Commit 4fbf8b1

Browse files
ubizjakIngo Molnar
authored and
Ingo Molnar
committed
locking/atomics: Use atomic_try_cmpxchg_release() to micro-optimize rcuref_put_slowpath()
Use atomic_try_cmpxchg() instead of atomic_cmpxchg(*ptr, old, new) == old in rcuref_put_slowpath(). On x86 the CMPXCHG instruction returns success in the ZF flag, so this change saves a compare after CMPXCHG. Additionaly, the compiler reorders some code blocks to follow likely/unlikely annotations in the atomic_try_cmpxchg() macro, improving the code from: 9a: f0 0f b1 0b lock cmpxchg %ecx,(%rbx) 9e: 83 f8 ff cmp $0xffffffff,%eax a1: 74 04 je a7 <rcuref_put_slowpath+0x27> a3: 31 c0 xor %eax,%eax to: 9a: f0 0f b1 0b lock cmpxchg %ecx,(%rbx) 9e: 75 4c jne ec <rcuref_put_slowpath+0x6c> a0: b0 01 mov $0x1,%al No functional change intended. Signed-off-by: Uros Bizjak <[email protected]> Signed-off-by: Ingo Molnar <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Paul E. McKenney <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent ad0a2e4 commit 4fbf8b1

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

lib/rcuref.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ bool rcuref_put_slowpath(rcuref_t *ref)
248248
* require a retry. If this fails the caller is not
249249
* allowed to deconstruct the object.
250250
*/
251-
if (atomic_cmpxchg_release(&ref->refcnt, RCUREF_NOREF, RCUREF_DEAD) != RCUREF_NOREF)
251+
if (!atomic_try_cmpxchg_release(&ref->refcnt, &cnt, RCUREF_DEAD))
252252
return false;
253253

254254
/*

0 commit comments

Comments
 (0)