Re: [PATCH v4 07/16] locking/rwsem: Implement lock handoff to prevent lock starvation

From: Peter Zijlstra
Date: Thu Apr 18 2019 - 04:22:14 EST


On Wed, Apr 17, 2019 at 12:39:19PM -0400, Waiman Long wrote:
> On 04/17/2019 04:05 AM, Peter Zijlstra wrote:
> > So what is wrong with the below?
> >
> > --- a/include/linux/sched/wake_q.h
> > +++ b/include/linux/sched/wake_q.h
> > @@ -51,6 +51,11 @@ static inline void wake_q_init(struct wa
> > head->lastp = &head->first;
> > }
> >
> > +static inline bool wake_q_empty(struct wake_q_head *head)
> > +{
> > + return head->first == WAKE_Q_TAIL;
> > +}
> > +
> > extern void wake_q_add(struct wake_q_head *head, struct task_struct *task);
> > extern void wake_q_add_safe(struct wake_q_head *head, struct task_struct *task);
> > extern void wake_up_q(struct wake_q_head *head);
> > --- a/kernel/locking/rwsem.c
> > +++ b/kernel/locking/rwsem.c
> > @@ -700,25 +700,22 @@ __rwsem_down_write_failed_common(struct
> > * must be read owned; so we try to wake any read lock
> > * waiters that were queued ahead of us.
> > */
> > - if (!(count & RWSEM_LOCKED_MASK))
> > - __rwsem_mark_wake(sem, RWSEM_WAKE_ANY, &wake_q);
> > - else if (!(count & RWSEM_WRITER_MASK) &&
> > - (count & RWSEM_READER_MASK))
> > - __rwsem_mark_wake(sem, RWSEM_WAKE_READERS, &wake_q);
> > - else
> > + if (count & RWSEM_WRITER_LOCKED)
> > goto wait;
> > - /*
> > - * The wakeup is normally called _after_ the wait_lock
> > - * is released, but given that we are proactively waking
> > - * readers we can deal with the wake_q overhead as it is
> > - * similar to releasing and taking the wait_lock again
> > - * for attempting rwsem_try_write_lock().
> > - */
> > - wake_up_q(&wake_q);
> > - /*
> > - * Reinitialize wake_q after use.
> > - */
> > - wake_q_init(&wake_q);
> > +
> > + __rwsem_mark_wake(sem, (count & RWSEM_READER_MASK) ?
> > + RWSEM_WAKE_READERS :
> > + RWSEM_WAKE_ANY, &wake_q);
> > +
> > + if (!wake_q_empty(&wake_q)) {
> > + raw_spin_unlock_irq(&sem->wait_lock);
> > + wake_up_q(&wake_q);
> > + /* used again, reinit */
> > + wake_q_init(&wake_q);
> > + raw_spin_lock_irq(&sem->wait_lock);
> > + if (rwsem_waiter_is_first(sem, &waiter))
> > + wstate = WRITER_FIRST;
> > + }
> > } else {
> > count = atomic_long_add_return(RWSEM_FLAG_WAITERS, &sem->count);
> > }
>
> Yes, we can certainly do that. My point is that I haven't changed the
> existing logic regarding that wakeup, I only move it around in the
> patch. As it is not related to lock handoff, we can do it as a separate
> patch.

Ah, I missed that the old code did that too (too much looking at the new
code I suppose). Then yes, a separate patch fixing this would be good.