Skip to content

Commit bf7b4c4

Browse files
Waiman Longingomolnar
authored andcommitted
locking/rwsem: Improve reader wakeup code
In __rwsem_do_wake(), the reader wakeup code will assume a writer has stolen the lock if the active reader/writer count is not 0. However, this is not as reliable an indicator as the original "< RWSEM_WAITING_BIAS" check. If another reader is present, the code will still break out and exit even if the writer is gone. This patch changes it to check the same "< RWSEM_WAITING_BIAS" condition to reduce the chance of false positive. Signed-off-by: Waiman Long <Waiman.Long@hpe.com> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Reviewed-by: Peter Hurley <peter@hurleysoftware.com> Cc: Andrew Morton <akpm@linux-foundation.org> Cc: Dave Chinner <david@fromorbit.com> Cc: Davidlohr Bueso <dave@stgolabs.net> Cc: Douglas Hatch <doug.hatch@hpe.com> Cc: Jason Low <jason.low2@hp.com> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Paul E. McKenney <paulmck@linux.vnet.ibm.com> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Scott J Norton <scott.norton@hpe.com> Cc: Thomas Gleixner <tglx@linutronix.de> Link: http://lkml.kernel.org/r/1463534783-38814-5-git-send-email-Waiman.Long@hpe.com Signed-off-by: Ingo Molnar <mingo@kernel.org>
1 parent fb6a44f commit bf7b4c4

1 file changed

Lines changed: 8 additions & 3 deletions

File tree

‎kernel/locking/rwsem-xadd.c‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,14 @@ __rwsem_mark_wake(struct rw_semaphore *sem,
156156
oldcount = atomic_long_add_return(adjustment, &sem->count) - adjustment;
157157

158158
if (unlikely(oldcount < RWSEM_WAITING_BIAS)) {
159-
/* A writer stole the lock. Undo our reader grant. */
160-
if (atomic_long_sub_return(adjustment, &sem->count) &
161-
RWSEM_ACTIVE_MASK)
159+
/*
160+
* If the count is still less than RWSEM_WAITING_BIAS
161+
* after removing the adjustment, it is assumed that
162+
* a writer has stolen the lock. We have to undo our
163+
* reader grant.
164+
*/
165+
if (atomic_long_add_return(-adjustment, &sem->count) <
166+
RWSEM_WAITING_BIAS)
162167
goto out;
163168
/* Last active locker left. Retry waking readers. */
164169
goto try_reader_grant;

0 commit comments

Comments
 (0)