Re: [PATCH RFC] drivers/core: Replace lockdep_set_novalidate_class() with unique class keys
From: Kent Overstreet
Date: Sat Feb 11 2023 - 18:36:30 EST
On Sat, Feb 11, 2023 at 06:06:28PM -0500, Kent Overstreet wrote:
> On 2/11/23 16:51, Linus Torvalds wrote:
> > On Sat, Feb 11, 2023 at 1:41 PM Alan Stern <stern@xxxxxxxxxxxxxxxxxxx> wrote:
> > >
> > > @@ -2941,7 +2944,10 @@ void device_initialize(struct device *de
> > > kobject_init(&dev->kobj, &device_ktype);
> > > INIT_LIST_HEAD(&dev->dma_pools);
> > > mutex_init(&dev->mutex);
> > > - lockdep_set_novalidate_class(&dev->mutex);
> > > + if (!lockdep_static_obj(dev)) {
> > > + lockdep_register_key(&dev->mutex_key);
> > > + lockdep_set_class(&dev->mutex, &dev->mutex_key);
> > > + }
> > > spin_lock_init(&dev->devres_lock);
> > > INIT_LIST_HEAD(&dev->devres_head);
> > > device_pm_init(dev);
> >
> > So I think this is the right thing to do, but I note that while that
> > lockdep_set_novalidate_class() was "documented" to only be for
> > 'dev->mutex' by scripts/checkpatch.pl, that horrific thing is also
> > used by md/bcache/btree.c for the mca_bucket_alloc().
> >
> > Can we *please* get rid of it there too (it was added by the initial
> > code, and never had any explicit excuse for it), possibly by using the
> > same model.
> >
> > And then we could get rid of lockdep_set_novalidate_class() entirely.
> > That would be a good thing.
>
> Yeah, what bcache really needs (and presumably dev->mutex as well) is just
> to disable lockdep checking for self-deadlock of that lock type, since it's
> got its own deadlock avoidance and the subclass thing isn't good enough.
>
> I've got a patch that should do what we want, replying from my other account
> with it.
After scanning the rest of the thread: I don't think you want to create
separate lockdep classes for each bus and device type, that's defeating
how lockdep works. Maybe if it was only a small, _static_ number of new
classes, but the basic premesis of lockdep is that there are static
human understandable lock ordering rules, so lockdep figures out what
they are and checks them: if you create a bunch of dynamic classes, the
classes are going to be different for everyone in practice and won't
have any real bearing on the structure of the code - that is, given a
lockdep splat, you won't be able to do anything with it.
If static lock ordering rules aren't working (say, because the lock
ordering rules are determined by hardware relationships or what
userspace is doing), then you have to do something more sophisticated.
Wait/wound mutexes would be the next thing to look at, and DRM ended up
needing them for similar reasons as what you're running up against so I
think they bear serious consideration.
ww mutexes are the simple version of dynamic deadlock avoidance -
instead of doing full cycle detection they just compare transaction
start times, so they come at the cost of more frequent aborts. If this
is an issue for you, here's what full cycle detection looks like:
https://evilpiepirate.org/git/bcachefs.git/tree/fs/bcachefs/btree_locking.c#n53