Messages in this thread | | | Subject | Re: [PATCH 1/2] drivers: core: Don't try to use a dead glue_dir | From | Benjamin Herrenschmidt <> | Date | Tue, 10 Jul 2018 09:50:29 +1000 |
| |
On Sat, 2018-07-07 at 18:51 +0200, Greg Kroah-Hartman wrote: > > > diff --git a/drivers/base/core.c b/drivers/base/core.c > > index b610816eb887..e9eff2099896 100644 > > --- a/drivers/base/core.c > > +++ b/drivers/base/core.c > > @@ -1517,11 +1517,13 @@ static struct kobject *get_device_parent(struct device *dev, > > > > /* find our class-directory at the parent and reference it */ > > spin_lock(&dev->class->p->glue_dirs.list_lock); > > - list_for_each_entry(k, &dev->class->p->glue_dirs.list, entry) > > + list_for_each_entry(k, &dev->class->p->glue_dirs.list, entry) { > > if (k->parent == parent_kobj) { > > - kobj = kobject_get(k); > > - break; > > + kobj = kobject_get_unless_zero(k); > > + if (kobj) > > + break; > > A parent directory _should_ not ever be able to be removed before the > object being removed was, as we should have had a reference to it, > right? So I don't see how this can get hit "in real life". > > Yes, enabling kobject debugging does keep objects around for a long time > in order to try to help figure out where people are messing up their > usage of them. What subsystem is doing this in a way that causes > problems here? Shouldn't we fix that up instead?
The broken subsystem is the driver core itself :-) See the descriptions here and in patch 2/2.
Note: This is a more generic problem with ksets vs relying on the magic sysfs cleanup happening in kobject_release().
Any kobject that is a member of a kset and doesn't get explicitely removed from sysfs with kobject_del() prior to dropping the last reference with kobject_put() (and thus relies instead on the automatic cleanup done by kobject_release()) will be exposed to the race:
The last kobject_put() will drop the refcount to 0 while the object is still in the kset. Only some amount of time later (which can be very short or very long if you enable kobject debugging), will kobject_release() take it out of sysfs and out of the kset.
Thus the object will be visible, with a 0 refcount, to anything that "walks" the kset during that period.
This is exactly what happens with the gluedirs in the device core, but it could happen elsewhere for all I know.
Cheers, Ben.
| |