locking/atomics: Use atomic_try_cmpxchg_release() to micro-optimize rcuref_put_slowpath()
authorUros Bizjak <ubizjak@gmail.com>
Tue, 9 May 2023 15:02:55 +0000 (17:02 +0200)
committerIngo Molnar <mingo@kernel.org>
Tue, 10 Oct 2023 08:14:27 +0000 (10:14 +0200)
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 <ubizjak@gmail.com>
Signed-off-by: Ingo Molnar <mingo@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Paul E. McKenney <paulmck@kernel.org>
Link: https://lore.kernel.org/r/20230509150255.3691-1-ubizjak@gmail.com
lib/rcuref.c

index 5ec00a4a64d11cadcef29e953d1b388cc41dfcc7..97f300eca927ced7f36fe0c932d2a9d3759809b8 100644 (file)
@@ -248,7 +248,7 @@ bool rcuref_put_slowpath(rcuref_t *ref)
                 * require a retry. If this fails the caller is not
                 * allowed to deconstruct the object.
                 */
-               if (atomic_cmpxchg_release(&ref->refcnt, RCUREF_NOREF, RCUREF_DEAD) != RCUREF_NOREF)
+               if (!atomic_try_cmpxchg_release(&ref->refcnt, &cnt, RCUREF_DEAD))
                        return false;
 
                /*