Skip to content

Commit 2f6560e

Browse files
npigginmpe
authored andcommitted
powerpc/qspinlock: Optimised atomic_try_cmpxchg_lock() that adds the lock hint
This brings the behaviour of the uncontended fast path back to roughly equivalent to simple spinlocks -- a single atomic op with lock hint. Signed-off-by: Nicholas Piggin <npiggin@gmail.com> Acked-by: Waiman Long <longman@redhat.com> Signed-off-by: Michael Ellerman <mpe@ellerman.id.au> Link: https://lore.kernel.org/r/20200724131423.1362108-6-npiggin@gmail.com
1 parent 20c0e82 commit 2f6560e

2 files changed

Lines changed: 29 additions & 1 deletion

File tree

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,34 @@ static __inline__ int atomic_dec_return_relaxed(atomic_t *v)
193193
#define atomic_xchg(v, new) (xchg(&((v)->counter), new))
194194
#define atomic_xchg_relaxed(v, new) xchg_relaxed(&((v)->counter), (new))
195195

196+
/*
197+
* Don't want to override the generic atomic_try_cmpxchg_acquire, because
198+
* we add a lock hint to the lwarx, which may not be wanted for the
199+
* _acquire case (and is not used by the other _acquire variants so it
200+
* would be a surprise).
201+
*/
202+
static __always_inline bool
203+
atomic_try_cmpxchg_lock(atomic_t *v, int *old, int new)
204+
{
205+
int r, o = *old;
206+
207+
__asm__ __volatile__ (
208+
"1:\t" PPC_LWARX(%0,0,%2,1) " # atomic_try_cmpxchg_acquire \n"
209+
" cmpw 0,%0,%3 \n"
210+
" bne- 2f \n"
211+
" stwcx. %4,0,%2 \n"
212+
" bne- 1b \n"
213+
"\t" PPC_ACQUIRE_BARRIER " \n"
214+
"2: \n"
215+
: "=&r" (r), "+m" (v->counter)
216+
: "r" (&v->counter), "r" (o), "r" (new)
217+
: "cr0", "memory");
218+
219+
if (unlikely(r != o))
220+
*old = r;
221+
return likely(r == o);
222+
}
223+
196224
/**
197225
* atomic_fetch_add_unless - add unless the number is a given value
198226
* @v: pointer of type atomic_t

‎arch/powerpc/include/asm/qspinlock.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ static __always_inline void queued_spin_lock(struct qspinlock *lock)
3737
{
3838
u32 val = 0;
3939

40-
if (likely(atomic_try_cmpxchg_acquire(&lock->val, &val, _Q_LOCKED_VAL)))
40+
if (likely(atomic_try_cmpxchg_lock(&lock->val, &val, _Q_LOCKED_VAL)))
4141
return;
4242

4343
queued_spin_lock_slowpath(lock, val);

0 commit comments

Comments
 (0)