Re: [PATCH 2/3] rcu: Equip sleepable RCU with lockdep dependency graph checks

From: Paul E. McKenney
Date: Mon Jan 16 2023 - 13:56:41 EST


On Mon, Jan 16, 2023 at 09:54:32AM -0800, Boqun Feng wrote:
> On Mon, Jan 16, 2023 at 06:36:43PM +0100, Paolo Bonzini wrote:
> > On 1/13/23 20:11, Paul E. McKenney wrote:
> > > On Fri, Jan 13, 2023 at 10:05:22AM -0800, Boqun Feng wrote:
> > > > On Fri, Jan 13, 2023 at 03:29:49AM -0800, Paul E. McKenney wrote:
> > > > I prefer that the first two patches go through your tree, because it
> > > > reduces the synchronization among locking, rcu and KVM trees to the
> > > > synchronization betwen rcu and KVM trees.
> > >
> > > Very well, I have queued and pushed these with the usual wordsmithing,
> > > thank you!
> >
> > I'm worried about this case:
> >
> > CPU 0 CPU 1
> > -------------------- ------------------
> > lock A srcu lock B
> > srcu lock B lock A
> > srcu unlock B unlock A
> > unlock A srcu unlock B
> >
> > While a bit unclean, there is nothing that downright forbids this; as long
> > as synchronize_srcu does not happen inside lock A, no deadlock can occur.
> >
>
> First, even with my change, lockdep won't report this as a deadlock
> because srcu_read_lock() is annotated as a recursive (unfair) read lock
> (the "read" parameter for lock_acquire() is 2) and in this case lockdep
> knows that it won't cause deadlock.
>
> For SRCU, the annotation mapping that is 1) srcu_read_lock() is marked
> as recursive read lock and 2) synchronize_srcu() is marked as write lock
> sync, recursive read locks themselves cannot cause deadlocks and lockdep
> is aware of it.
>
> Will add a selftest for this later.
>
> > However, if srcu is replaced with an rwlock then lockdep should and does
> > report a deadlock. Boqun, do you get a false positive or do your patches
>
> To be more precise, to have a deadlock, the read lock on CPU 0 has to be
> a *fair* read lock (i.e. non-recursive reader, the "read" parameter for
> lock_acquire is 1)
>
> > correctly suppress this?
>
> I'm pretty sure that lockdep handles this ;-)

And lockdep agrees, refusing to complain about the following:

idx = srcu_read_lock(&srcu1);
mutex_lock(&mut1);
mutex_unlock(&mut1);
srcu_read_unlock(&srcu1, idx);

mutex_lock(&mut1);
idx = srcu_read_lock(&srcu1);
srcu_read_unlock(&srcu1, idx);
mutex_unlock(&mut1);

Thanx, Paul