Re: RFC: hold i_rwsem until aio completes

From: Jason Gunthorpe
Date: Wed Jan 15 2020 - 08:24:32 EST


On Wed, Jan 15, 2020 at 07:56:14AM +0100, Christoph Hellwig wrote:
> On Tue, Jan 14, 2020 at 03:27:00PM -0400, Jason Gunthorpe wrote:
> > I've seen similar locking patterns quite a lot, enough I've thought
> > about having a dedicated locking primitive to do it. It really wants
> > to be a rwsem, but as here the rwsem rules don't allow it.
> >
> > The common pattern I'm looking at looks something like this:
> >
> > 'try begin read'() // aka down_read_trylock()
> >
> > /* The lockdep release hackery you describe,
> > the rwsem remains read locked */
> > 'exit reader'()
> >
> > .. delegate unlock to work queue, timer, irq, etc ..
> >
> > in the new context:
> >
> > 're_enter reader'() // Get our lockdep tracking back
> >
> > 'end reader'() // aka up_read()
> >
> > vs a typical write side:
> >
> > 'begin write'() // aka down_write()
> >
> > /* There is no reason to unlock it before kfree of the rwsem memory.
> > Somehow the user prevents any new down_read_trylock()'s */
> > 'abandon writer'() // The object will be kfree'd with a locked writer
> > kfree()
> >
> > The typical goal is to provide an object destruction path that can
> > serialize and fence all readers wherever they may be before proceeding
> > to some synchronous destruction.
> >
> > Usually this gets open coded with some atomic/kref/refcount and a
> > completion or wait queue. Often implemented wrongly, lacking the write
> > favoring bias in the rwsem, and lacking any lockdep tracking on the
> > naked completion.
> >
> > Not to discourage your patch, but to ask if we can make the solution
> > more broadly applicable?
>
> Your requirement seems a little different, and in fact in many ways
> similar to the percpu_ref primitive.

I was interested because you are talking about allowing the read/write side
of a rw sem to be held across a return to user space/etc, which is the
same basic problem.

precpu refcount looks more like a typical refcount with a release that
is called by whatever context does the final put. The point above is
to basically move the release of a refcount into a synchrnous path by
introducing some barrier to wait for the refcount to go to zero. In
the above the barrier is the down_write() as it is really closer to a
rwsem than a refcount.

Thanks,
Jason