Skip to content

Commit c6f4a90

Browse files
guoren83Ingo Molnar
authored and
Ingo Molnar
committed
asm-generic: ticket-lock: Optimize arch_spin_value_unlocked()
The arch_spin_value_unlocked() of ticket-lock would cause the compiler to generate inefficient asm code in riscv architecture because of unnecessary memory access to the contended value. Before the patch: void lockref_get(struct lockref *lockref) { 78: fd010113 add sp,sp,-48 7c: 02813023 sd s0,32(sp) 80: 02113423 sd ra,40(sp) 84: 03010413 add s0,sp,48 0000000000000088 <.LBB296>: CMPXCHG_LOOP( 88: 00053783 ld a5,0(a0) After the patch: void lockref_get(struct lockref *lockref) { CMPXCHG_LOOP( 78: 00053783 ld a5,0(a0) After the patch, the lockref_get() could get in a fast path instead of the function's prologue. This is because ticket lock complex logic would limit compiler optimization for the spinlock fast path, and qspinlock won't. The caller of arch_spin_value_unlocked() could benefit from this change. Currently, the only caller is lockref. Signed-off-by: Guo Ren <[email protected]> Signed-off-by: Guo Ren <[email protected]> Signed-off-by: Ingo Molnar <[email protected]> Acked-by: Waiman Long <[email protected]> Acked-by: Will Deacon <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent fbeb558 commit c6f4a90

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

include/asm-generic/spinlock.h

+9-7
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,18 @@ static __always_inline void arch_spin_unlock(arch_spinlock_t *lock)
6868
smp_store_release(ptr, (u16)val + 1);
6969
}
7070

71+
static __always_inline int arch_spin_value_unlocked(arch_spinlock_t lock)
72+
{
73+
u32 val = lock.counter;
74+
75+
return ((val >> 16) == (val & 0xffff));
76+
}
77+
7178
static __always_inline int arch_spin_is_locked(arch_spinlock_t *lock)
7279
{
73-
u32 val = atomic_read(lock);
80+
arch_spinlock_t val = READ_ONCE(*lock);
7481

75-
return ((val >> 16) != (val & 0xffff));
82+
return !arch_spin_value_unlocked(val);
7683
}
7784

7885
static __always_inline int arch_spin_is_contended(arch_spinlock_t *lock)
@@ -82,11 +89,6 @@ static __always_inline int arch_spin_is_contended(arch_spinlock_t *lock)
8289
return (s16)((val >> 16) - (val & 0xffff)) > 1;
8390
}
8491

85-
static __always_inline int arch_spin_value_unlocked(arch_spinlock_t lock)
86-
{
87-
return !arch_spin_is_locked(&lock);
88-
}
89-
9092
#include <asm/qrwlock.h>
9193

9294
#endif /* __ASM_GENERIC_SPINLOCK_H */

0 commit comments

Comments
 (0)