Re: [RFC] locking: rwbase: Take care of ordering guarantee for fastpath reader

From: Boqun Feng
Date: Wed Sep 08 2021 - 09:01:59 EST


On Wed, Sep 08, 2021 at 02:14:28PM +0200, Peter Zijlstra wrote:
> On Wed, Sep 08, 2021 at 01:51:24PM +0200, Peter Zijlstra wrote:
> > On Wed, Sep 01, 2021 at 11:06:27PM +0800, Boqun Feng wrote:
> > @@ -201,23 +207,30 @@ static int __sched rwbase_write_lock(struct rwbase_rt *rwb,
> > {
> > struct rt_mutex_base *rtm = &rwb->rtmutex;
> > unsigned long flags;
> > + int readers;
> >
> > /* Take the rtmutex as a first step */
> > if (rwbase_rtmutex_lock_state(rtm, state))
> > return -EINTR;
> >
> > /* Force readers into slow path */
> > - atomic_sub(READER_BIAS, &rwb->readers);
> > + readers = atomic_sub_return_relaxed(READER_BIAS, &rwb->readers);
>
> Hurmph... the above really begs for something like
>
> if (!readers)
> return 0;
>


I don't think we can return early here, don't we need to set WRITER_BIAS
to grab the write lock? And we can only do that with ->wait_lock held,
otherwise we race with the slowpath of readers.

Regards,
Boqun

> But then we needs that _acquire() thing again :/
>