Skip to content

Commit 00f3c5a

Browse files
Waiman-LongIngo Molnar
authored andcommitted
locking/rwsem: Always release wait_lock before waking up tasks
With the use of wake_q, we can do task wakeups without holding the wait_lock. There is one exception in the rwsem code, though. It is when the writer in the slowpath detects that there are waiters ahead but the rwsem is not held by a writer. This can lead to a long wait_lock hold time especially when a large number of readers are to be woken up. Remediate this situation by releasing the wait_lock before waking up tasks and re-acquiring it afterward. The rwsem_try_write_lock() function is also modified to read the rwsem count directly to avoid stale count value. Suggested-by: Peter Zijlstra <peterz@infradead.org> Signed-off-by: Waiman Long <longman@redhat.com> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Cc: Borislav Petkov <bp@alien8.de> Cc: Davidlohr Bueso <dave@stgolabs.net> Cc: H. Peter Anvin <hpa@zytor.com> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: Tim Chen <tim.c.chen@linux.intel.com> Cc: Will Deacon <will.deacon@arm.com> Cc: huang ying <huang.ying.caritas@gmail.com> Link: https://lkml.kernel.org/r/20190520205918.22251-9-longman@redhat.com Signed-off-by: Ingo Molnar <mingo@kernel.org>
1 parent 4f23dbc commit 00f3c5a

File tree

2 files changed

+20
-16
lines changed

2 files changed

+20
-16
lines changed

include/linux/sched/wake_q.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ static inline void wake_q_init(struct wake_q_head *head)
5151
head->lastp = &head->first;
5252
}
5353

54+
static inline bool wake_q_empty(struct wake_q_head *head)
55+
{
56+
return head->first == WAKE_Q_TAIL;
57+
}
58+
5459
extern void wake_q_add(struct wake_q_head *head, struct task_struct *task);
5560
extern void wake_q_add_safe(struct wake_q_head *head, struct task_struct *task);
5661
extern void wake_up_q(struct wake_q_head *head);

kernel/locking/rwsem.c

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -400,13 +400,14 @@ static void rwsem_mark_wake(struct rw_semaphore *sem,
400400
* If wstate is WRITER_HANDOFF, it will make sure that either the handoff
401401
* bit is set or the lock is acquired with handoff bit cleared.
402402
*/
403-
static inline bool rwsem_try_write_lock(long count, struct rw_semaphore *sem,
403+
static inline bool rwsem_try_write_lock(struct rw_semaphore *sem,
404404
enum writer_wait_state wstate)
405405
{
406-
long new;
406+
long count, new;
407407

408408
lockdep_assert_held(&sem->wait_lock);
409409

410+
count = atomic_long_read(&sem->count);
410411
do {
411412
bool has_handoff = !!(count & RWSEM_FLAG_HANDOFF);
412413

@@ -751,26 +752,25 @@ rwsem_down_write_slowpath(struct rw_semaphore *sem, int state)
751752
? RWSEM_WAKE_READERS
752753
: RWSEM_WAKE_ANY, &wake_q);
753754

754-
/*
755-
* The wakeup is normally called _after_ the wait_lock
756-
* is released, but given that we are proactively waking
757-
* readers we can deal with the wake_q overhead as it is
758-
* similar to releasing and taking the wait_lock again
759-
* for attempting rwsem_try_write_lock().
760-
*/
761-
wake_up_q(&wake_q);
762-
763-
/* We need wake_q again below, reinitialize */
764-
wake_q_init(&wake_q);
755+
if (!wake_q_empty(&wake_q)) {
756+
/*
757+
* We want to minimize wait_lock hold time especially
758+
* when a large number of readers are to be woken up.
759+
*/
760+
raw_spin_unlock_irq(&sem->wait_lock);
761+
wake_up_q(&wake_q);
762+
wake_q_init(&wake_q); /* Used again, reinit */
763+
raw_spin_lock_irq(&sem->wait_lock);
764+
}
765765
} else {
766-
count = atomic_long_add_return(RWSEM_FLAG_WAITERS, &sem->count);
766+
atomic_long_or(RWSEM_FLAG_WAITERS, &sem->count);
767767
}
768768

769769
wait:
770770
/* wait until we successfully acquire the lock */
771771
set_current_state(state);
772772
while (true) {
773-
if (rwsem_try_write_lock(count, sem, wstate))
773+
if (rwsem_try_write_lock(sem, wstate))
774774
break;
775775

776776
raw_spin_unlock_irq(&sem->wait_lock);
@@ -811,7 +811,6 @@ rwsem_down_write_slowpath(struct rw_semaphore *sem, int state)
811811
}
812812

813813
raw_spin_lock_irq(&sem->wait_lock);
814-
count = atomic_long_read(&sem->count);
815814
}
816815
__set_current_state(TASK_RUNNING);
817816
list_del(&waiter.list);

0 commit comments

Comments
 (0)