locking/qrwlock: Prevent slowpath writers getting held up by fastpath
When a prospective writer takes the qrwlock locking slowpath due to the lock being held, it attempts to cmpxchg the wmode field from 0 to _QW_WAITING so that concurrent lockers also take the slowpath and queue on the spinlock accordingly, allowing the lockers to drain. Unfortunately, this isn't fair, because a fastpath writer that comes in after the lock is made available but before the _QW_WAITING flag is set can effectively jump the queue. If there is a steady stream of prospective writers, then the waiter will be held off indefinitely. This patch restores fairness by separating _QW_WAITING and _QW_LOCKED into two distinct fields: _QW_LOCKED continues to occupy the bottom byte of the lockword so that it can be cleared unconditionally when unlocking, but _QW_WAITING now occupies what used to be the bottom bit of the reader count. This then forces the slow-path for concurrent lockers. Tested-by: Waiman Long <longman@redhat.com> Tested-by: Jeremy Linton <jeremy.linton@arm.com> Tested-by: Adam Wallis <awallis@codeaurora.org> Tested-by: Jan Glauber <jglauber@cavium.com> Signed-off-by: Will Deacon <will.deacon@arm.com> Acked-by: Peter 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-6-git-send-email-will.deacon@arm.com Signed-off-by: Ingo Molnar <mingo@kernel.org>
This commit is contained in:
@@ -39,8 +39,7 @@ void queued_read_lock_slowpath(struct qrwlock *lock)
|
||||
* so spin with ACQUIRE semantics until the lock is available
|
||||
* without waiting in the queue.
|
||||
*/
|
||||
atomic_cond_read_acquire(&lock->cnts, (VAL & _QW_WMASK)
|
||||
!= _QW_LOCKED);
|
||||
atomic_cond_read_acquire(&lock->cnts, !(VAL & _QW_LOCKED));
|
||||
return;
|
||||
}
|
||||
atomic_sub(_QR_BIAS, &lock->cnts);
|
||||
@@ -56,7 +55,7 @@ void queued_read_lock_slowpath(struct qrwlock *lock)
|
||||
* that accesses can't leak upwards out of our subsequent critical
|
||||
* section in the case that the lock is currently held for write.
|
||||
*/
|
||||
atomic_cond_read_acquire(&lock->cnts, (VAL & _QW_WMASK) != _QW_LOCKED);
|
||||
atomic_cond_read_acquire(&lock->cnts, !(VAL & _QW_LOCKED));
|
||||
|
||||
/*
|
||||
* Signal the next one in queue to become queue head
|
||||
@@ -79,19 +78,10 @@ void queued_write_lock_slowpath(struct qrwlock *lock)
|
||||
(atomic_cmpxchg_acquire(&lock->cnts, 0, _QW_LOCKED) == 0))
|
||||
goto unlock;
|
||||
|
||||
/*
|
||||
* Set the waiting flag to notify readers that a writer is pending,
|
||||
* or wait for a previous writer to go away.
|
||||
*/
|
||||
for (;;) {
|
||||
if (!READ_ONCE(lock->wmode) &&
|
||||
(cmpxchg_relaxed(&lock->wmode, 0, _QW_WAITING) == 0))
|
||||
break;
|
||||
/* Set the waiting flag to notify readers that a writer is pending */
|
||||
atomic_add(_QW_WAITING, &lock->cnts);
|
||||
|
||||
cpu_relax();
|
||||
}
|
||||
|
||||
/* When no more readers, set the locked flag */
|
||||
/* When no more readers or writers, set the locked flag */
|
||||
do {
|
||||
atomic_cond_read_acquire(&lock->cnts, VAL == _QW_WAITING);
|
||||
} while (atomic_cmpxchg_relaxed(&lock->cnts, _QW_WAITING,
|
||||
|
Reference in New Issue
Block a user