Commit b519b56e authored by Will Deacon's avatar Will Deacon Committed by Ingo Molnar

locking/qrwlock: Use atomic_cond_read_acquire() when spinning in qrwlock

The qrwlock slowpaths involve spinning when either a prospective reader
is waiting for a concurrent writer to drain, or a prospective writer is
waiting for concurrent readers to drain. In both of these situations,
atomic_cond_read_acquire() can be used to avoid busy-waiting and make use
of any backoff functionality provided by the architecture.

This patch replaces the open-code loops and rspin_until_writer_unlock()
implementation with atomic_cond_read_acquire(). The write mode transition
zero to _QW_WAITING is left alone, since (a) this doesn't need acquire
semantics and (b) should be fast.
Tested-by: default avatarWaiman Long <longman@redhat.com>
Tested-by: default avatarJeremy Linton <jeremy.linton@arm.com>
Tested-by: default avatarAdam Wallis <awallis@codeaurora.org>
Tested-by: default avatarJan Glauber <jglauber@cavium.com>
Signed-off-by: default avatarWill Deacon <will.deacon@arm.com>
Acked-by: default avatarPeter Zijlstra <peterz@infradead.org>
Cc: Boqun Feng <boqun.feng@gmail.com>
Cc: Jeremy.Linton@arm.com
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: linux-arm-kernel@lists.infradead.org
Link: http://lkml.kernel.org/r/1507810851-306-4-git-send-email-will.deacon@arm.comSigned-off-by: default avatarIngo Molnar <mingo@kernel.org>
parent 4df714be
...@@ -49,7 +49,7 @@ ...@@ -49,7 +49,7 @@
/* /*
* External function declarations * External function declarations
*/ */
extern void queued_read_lock_slowpath(struct qrwlock *lock, u32 cnts); extern void queued_read_lock_slowpath(struct qrwlock *lock);
extern void queued_write_lock_slowpath(struct qrwlock *lock); extern void queued_write_lock_slowpath(struct qrwlock *lock);
/** /**
...@@ -100,7 +100,7 @@ static inline void queued_read_lock(struct qrwlock *lock) ...@@ -100,7 +100,7 @@ static inline void queued_read_lock(struct qrwlock *lock)
return; return;
/* The slowpath will decrement the reader count, if necessary. */ /* The slowpath will decrement the reader count, if necessary. */
queued_read_lock_slowpath(lock, cnts); queued_read_lock_slowpath(lock);
} }
/** /**
......
...@@ -23,29 +23,11 @@ ...@@ -23,29 +23,11 @@
#include <linux/spinlock.h> #include <linux/spinlock.h>
#include <asm/qrwlock.h> #include <asm/qrwlock.h>
/**
* rspin_until_writer_unlock - inc reader count & spin until writer is gone
* @lock : Pointer to queue rwlock structure
* @writer: Current queue rwlock writer status byte
*
* In interrupt context or at the head of the queue, the reader will just
* increment the reader count & wait until the writer releases the lock.
*/
static __always_inline void
rspin_until_writer_unlock(struct qrwlock *lock, u32 cnts)
{
while ((cnts & _QW_WMASK) == _QW_LOCKED) {
cpu_relax();
cnts = atomic_read_acquire(&lock->cnts);
}
}
/** /**
* queued_read_lock_slowpath - acquire read lock of a queue rwlock * queued_read_lock_slowpath - acquire read lock of a queue rwlock
* @lock: Pointer to queue rwlock structure * @lock: Pointer to queue rwlock structure
* @cnts: Current qrwlock lock value
*/ */
void queued_read_lock_slowpath(struct qrwlock *lock, u32 cnts) void queued_read_lock_slowpath(struct qrwlock *lock)
{ {
/* /*
* Readers come here when they cannot get the lock without waiting * Readers come here when they cannot get the lock without waiting
...@@ -53,13 +35,12 @@ void queued_read_lock_slowpath(struct qrwlock *lock, u32 cnts) ...@@ -53,13 +35,12 @@ void queued_read_lock_slowpath(struct qrwlock *lock, u32 cnts)
if (unlikely(in_interrupt())) { if (unlikely(in_interrupt())) {
/* /*
* Readers in interrupt context will get the lock immediately * Readers in interrupt context will get the lock immediately
* if the writer is just waiting (not holding the lock yet). * if the writer is just waiting (not holding the lock yet),
* The rspin_until_writer_unlock() function returns immediately * so spin with ACQUIRE semantics until the lock is available
* in this case. Otherwise, they will spin (with ACQUIRE * without waiting in the queue.
* semantics) until the lock is available without waiting in
* the queue.
*/ */
rspin_until_writer_unlock(lock, cnts); atomic_cond_read_acquire(&lock->cnts, (VAL & _QW_WMASK)
!= _QW_LOCKED);
return; return;
} }
atomic_sub(_QR_BIAS, &lock->cnts); atomic_sub(_QR_BIAS, &lock->cnts);
...@@ -68,14 +49,14 @@ void queued_read_lock_slowpath(struct qrwlock *lock, u32 cnts) ...@@ -68,14 +49,14 @@ void queued_read_lock_slowpath(struct qrwlock *lock, u32 cnts)
* Put the reader into the wait queue * Put the reader into the wait queue
*/ */
arch_spin_lock(&lock->wait_lock); arch_spin_lock(&lock->wait_lock);
atomic_add(_QR_BIAS, &lock->cnts);
/* /*
* The ACQUIRE semantics of the following spinning code ensure * The ACQUIRE semantics of the following spinning code ensure
* that accesses can't leak upwards out of our subsequent critical * that accesses can't leak upwards out of our subsequent critical
* section in the case that the lock is currently held for write. * section in the case that the lock is currently held for write.
*/ */
cnts = atomic_fetch_add_acquire(_QR_BIAS, &lock->cnts); atomic_cond_read_acquire(&lock->cnts, (VAL & _QW_WMASK) != _QW_LOCKED);
rspin_until_writer_unlock(lock, cnts);
/* /*
* Signal the next one in queue to become queue head * Signal the next one in queue to become queue head
...@@ -90,8 +71,6 @@ EXPORT_SYMBOL(queued_read_lock_slowpath); ...@@ -90,8 +71,6 @@ EXPORT_SYMBOL(queued_read_lock_slowpath);
*/ */
void queued_write_lock_slowpath(struct qrwlock *lock) void queued_write_lock_slowpath(struct qrwlock *lock)
{ {
u32 cnts;
/* Put the writer into the wait queue */ /* Put the writer into the wait queue */
arch_spin_lock(&lock->wait_lock); arch_spin_lock(&lock->wait_lock);
...@@ -113,15 +92,10 @@ void queued_write_lock_slowpath(struct qrwlock *lock) ...@@ -113,15 +92,10 @@ void queued_write_lock_slowpath(struct qrwlock *lock)
} }
/* When no more readers, set the locked flag */ /* When no more readers, set the locked flag */
for (;;) { do {
cnts = atomic_read(&lock->cnts); atomic_cond_read_acquire(&lock->cnts, VAL == _QW_WAITING);
if ((cnts == _QW_WAITING) && } while (atomic_cmpxchg_relaxed(&lock->cnts, _QW_WAITING,
(atomic_cmpxchg_acquire(&lock->cnts, _QW_WAITING, _QW_LOCKED) != _QW_WAITING);
_QW_LOCKED) == _QW_WAITING))
break;
cpu_relax();
}
unlock: unlock:
arch_spin_unlock(&lock->wait_lock); arch_spin_unlock(&lock->wait_lock);
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment