Re: [RFC][PATCH 7/7] lockdep: spin_lock_nest_lock()

Peter Zijlstra
2008-08-04T18:54:55Z

On Mon, 2008-08-04 at 11:06 -0700, Jeremy Fitzhardinge wrote:
> Peter Zijlstra wrote:
> > Expose the new lock protection lock.
> >
> > This can be used to annotate places where we take multiple locks of the
> > same class and avoid deadlocks by always taking another (top-level) lock
> > first.
> >
>
> OK, so the expected usage is:
>
> spin_lock(&outer_lock);
> /* take in any order */
> spin_lock_nest_lock(&inner[0], &outer_lock);
> spin_lock_nest_lock(&inner[2], &outer_lock);
> spin_lock_nest_lock(&inner[1], &outer_lock);
> ...
>
> ?

Yes (there it no requirement that the outer lock is a spinlock_t, just
that it has a ->dep_map member - so: mutex, rwsem and spinlock will do).

> And it's OK to
>
> 1. take inner locks one at a time without holding the outer lock

Yes

> 2. use plain spin_lock on inner locks when you're taking them one at
> a time, and

Yes

> 3. release the outer lock before releasing the inner locks

Only if you then release the inner locks in the reverse order you took
them - the nested release code (releasing a lock that is not on the top
of the stack) basically pops and pushes all the locks, the push will
fail if the outer lock is released.

> but it's not OK to try to use different outer locks for a given inner lock.

It doesn't validate this part - as with most lockdep annotations you can
annotate away real deadlocks.