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

From: Boqun Feng
Date: Thu Feb 23 2023 - 20:34:43 EST


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.

Regards,
Boqun

> > + if (waiter->task)
> > + goto out_nolock;
>
[...]
> > +
> > + __set_current_state(TASK_RUNNING);
> > + lockevent_inc(waiter_type(waiter, rwsem_rlock, rwsem_wlock));
> > + trace_contention_end(sem, 0);
> > + return sem;
> > +
> > +out_nolock:
> > + rwsem_del_wake_waiter(sem, waiter);
> > + __set_current_state(TASK_RUNNING);
> > + lockevent_inc(waiter_type(waiter, rwsem_rlock_fail, rwsem_wlock_fail));
> > + trace_contention_end(sem, -EINTR);
> > + return ERR_PTR(-EINTR);
> > +}
> > +
> > /*
> > * Wait for the read lock to be granted
> > */
> > @@ -1071,38 +1109,7 @@ rwsem_down_read_slowpath(struct rw_semap
> > if (!wake_q_empty(&wake_q))
> > wake_up_q(&wake_q);
> >
> > - trace_contention_begin(sem, LCB_F_READ);
> > -
> > - /* 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);
> > - if (waiter.task)
> > - goto out_nolock;
> > - raw_spin_unlock_irq(&sem->wait_lock);
> > - /* Ordered by sem->wait_lock against rwsem_mark_wake(). */
> > - break;
> > - }
> > - schedule_preempt_disabled();
> > - lockevent_inc(rwsem_sleep_reader);
> > - }
> > -
> > - __set_current_state(TASK_RUNNING);
> > - lockevent_inc(rwsem_rlock);
> > - trace_contention_end(sem, 0);
> > - return sem;
> > -
> > -out_nolock:
> > - rwsem_del_wake_waiter(sem, &waiter, &wake_q);
> > - __set_current_state(TASK_RUNNING);
> > - lockevent_inc(rwsem_rlock_fail);
> > - trace_contention_end(sem, -EINTR);
> > - return ERR_PTR(-EINTR);
> > + return rwsem_waiter_wait(sem, &waiter, state);
> > }
> >
> > /*
> > @@ -1150,37 +1157,7 @@ rwsem_down_write_slowpath(struct rw_sema
> > }
> > raw_spin_unlock_irq(&sem->wait_lock);
> >
> > - /* wait until we successfully acquire the lock */
> > - trace_contention_begin(sem, LCB_F_WRITE);
> > -
> > - 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);
> > - if (waiter.task)
> > - goto out_nolock;
> > - raw_spin_unlock_irq(&sem->wait_lock);
> > - /* Ordered by sem->wait_lock against rwsem_mark_wake(). */
> > - break;
> > - }
> > - schedule_preempt_disabled();
> > - lockevent_inc(rwsem_sleep_writer);
> > - }
> > - __set_current_state(TASK_RUNNING);
> > - lockevent_inc(rwsem_wlock);
> > - trace_contention_end(sem, 0);
> > - return sem;
> > -
> > -out_nolock:
> > - rwsem_del_wake_waiter(sem, &waiter, &wake_q);
> > - __set_current_state(TASK_RUNNING);
> > - lockevent_inc(rwsem_wlock_fail);
> > - trace_contention_end(sem, -EINTR);
> > - return ERR_PTR(-EINTR);
> > + return rwsem_waiter_wait(sem, &waiter, state);
> > }
> >
> > /*
> >
> >