Re: [PATCH 5/6] locking/rwsem: Unify wait loop

From: Boqun Feng
Date: Sun Feb 26 2023 - 13:23:18 EST


On Sun, Feb 26, 2023 at 01:01:10PM +0100, Peter Zijlstra wrote:
> On Thu, Feb 23, 2023 at 05:33:53PM -0800, Boqun Feng wrote:
> > On Thu, Feb 23, 2023 at 11:31:47AM -0800, Boqun Feng wrote:
> > [..]
> > > > +#define waiter_type(_waiter, _r, _w) \
> > > > + ((_waiter)->type == RWSEM_WAITING_FOR_READ ? (_r) : (_w))
> > > > +
> > > > +static __always_inline struct rw_semaphore *
> > > > +rwsem_waiter_wait(struct rw_semaphore *sem, struct rwsem_waiter *waiter, int state)
> > > > +{
> > > > + trace_contention_begin(sem, waiter_type(waiter, LCB_F_READ, LCB_F_WRITE));
> > > > +
> > > > + /* wait to be given the lock */
> > > > + for (;;) {
> > > > + set_current_state(state);
> > > > + if (!smp_load_acquire(&waiter->task)) {
> > > > + /* Matches rwsem_waiter_wake()'s smp_store_release(). */
> > > > + break;
> > > > + }
> > > > + if (signal_pending_state(state, current)) {
> > > > + raw_spin_lock_irq(&sem->wait_lock);
> > >
> > > Move the below __set_current_state(TASK_RUNNING)s up here? I think we
> > > need the preemption protection when changing the task state here.
> > >
> >
> > Nevermind since we have the preemption protection for the whole
> > function... but merging two __set_current_state()s into one still looks
> > good.
>
> Even if it were not; I still don't understand the concern. Preemption
> ignores task state.

Because I missed the exact thing you just mentioned... ;-)

I was worried about the following case:

ttwu();
set_current_state(TASK_UNINTERRUPTIBLE);
....
<preemption enable>
<preempted>
preempt_schedule_irq():
__schedule(...):
deactivate_task(); // Wakeup missed.

However this is not true, since __schedule() in preempt_schedule_irq()
is a SM_PREEMPT one.

Sorry for the noise then. But good for me to revisit these stuffs ;-)

Regards,
Boqun