Re: [PATCH RFC] drivers/core: Replace lockdep_set_novalidate_class() with unique class keys

From: Boqun Feng
Date: Mon Feb 13 2023 - 20:52:00 EST


On Mon, Feb 13, 2023 at 05:29:49PM +0100, Peter Zijlstra wrote:
> On Mon, Feb 13, 2023 at 10:25:59AM -0500, Alan Stern wrote:
> > On Mon, Feb 13, 2023 at 10:24:13AM +0100, Peter Zijlstra wrote:
> > > On Sun, Feb 12, 2023 at 10:23:44AM -0500, Alan Stern wrote:
> > > > Provided it acquires the parent device's lock first, this is
> > > > utterly safe no matter what order the children are locked in. Try
> > > > telling that to lockdep!
> > >
> > > mutex_lock_next_lock(child->lock, parent->lock) is there to express this
> > > exact pattern, it allows taking multiple child->lock class locks (in any
> > > order) provided parent->lock is held.
> >
> > Ah, this is news to me. Is this sort of thing documented somewhere?

Basically if you have two lock instances A and B with the same class,
and you know that locking ordering is always A -> B, then you can do

mutex_lock(A);
mutex_lock_nest_lock(B, A); // lock B.

to tell the lockdep this is not deadlock, plus lockdep will treat the
acquisition of A and the precondition of acquisition B, so the following
is not a deadlock as well:

T1:
mutex_lock(A);
mutex_lock(C);
mutex_lock_nest_lock(B, A);

T2:
mutex_lock(A);
mutex_lock_nest_lock(B, A);
mutex_lock(C);

Regards,
Boqun

>
> Probably not :/