Re: [PATCH 1/2] lockdep: lock_set_lock_cmp_fn()

From: Kent Overstreet
Date: Mon Feb 20 2023 - 17:45:55 EST


On Mon, Feb 20, 2023 at 04:09:33PM +0100, Peter Zijlstra wrote:
> On Fri, Feb 17, 2023 at 10:21:16PM -0500, Kent Overstreet wrote:
>
> > @@ -2982,6 +2989,10 @@ print_deadlock_bug(struct task_struct *curr, struct held_lock *prev,
> > pr_warn("\nbut task is already holding lock:\n");
> > print_lock(prev);
> >
> > + if (class->cmp_fn)
> > + pr_warn("and the lock comparison function returns %i:\n",
> > + class->cmp_fn(prev->instance, next->instance));
> > +
>
> Please, use {} for any actual multi-line.
>
> But is this sufficient data to debug a splat? Given an inversion on this
> class, we'll get something like:
>
> A
> A -a
> A -b
>
> vs
>
> A
> A c
>
> which is I suppose sufficient to say that A messed up, but not much
> more. With subclasses we would've gotten
>
> A/0
> A/1
> A/2
>
> vs
>
> A/2
> A/0
>
> which is much simpler to work with. Can we improve on this? Give the
> cmp_fn an additinoal optional argument of a string pointer or so to fill
> out with actual details to be printed?

Yes. This is where printbufs and %pf() would've been really nice, it'll
be doable but ugly with what we have now for string output. We just need
to add another callback, either .lock_print() or .lock_to_text(), and
that can print the information about the lock that's relevant for lock
ordering.

For bcache, that'd be something like

static void btree_lock_to_text()
{
struct btree *b = container_of()

seq_buf_printf("l=%u %llu:%llu", b->level, KEY_INODE(), KEY_OFFSET()...)
}