Skip to contentSkip to conversation

driver core: Make sure device detached from driver before deleting it

PATCH In review Jeffy Chen 4 participants 12 messages Oct 18, 2017 Driver core

lore
Share & tools
lore

Jeffy Chen

author
#0 lore raw

PATCH 1/1 driver core: Make sure device detached from driver before deleting it

There are cases we call device_del() without detaching it from the driver(e.g. spi core del children devices).

---

 drivers/base/core.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/base/core.c +2 -0
index 12ebd055724c..717efc3020af 100644
@@ -1951,6 +1951,8 @@ void device_del(struct device *dev)
 	struct kobject *glue_dir = NULL;
 	struct class_interface *class_intf;
 
+	device_release_driver(dev);
+
 	/* Notify clients of device removal.  This call must come
 	 * before dpm_sysfs_remove().
 	 */
Signature · 1 line
2.11.0
#1 lore raw
Quoted 2 lines · Jeffy Chen

There are cases we call device_del() without detaching it from the driver(e.g. spi core del children devices).

Why would you do that? Shouldn't that be fixed instead of this odd work-around for a broken bus?

thanks,

greg k-h

author
#2 lore raw

Hi Greg,

Thanks for your reply.

Quoted 6 lines · Greg Kroah-Hartman
Quoted 2 lines · Jeffy Chen
Quoted 2 lines

There are cases we call device_del() without detaching it from the driver(e.g. spi core del children devices).

Why would you do that? Shouldn't that be fixed instead of this odd work-around for a broken bus?

i was thinking since the device_unregister() is called everywhere, maybe there are some other drivers missing that too?

and the driver calls device_add(), then the core attaches the device to the driver automatically, maybe it would make sense to let the core automatic detach it somehow...

but i know nothing about driver core, maybe i should fix it in spi core, i'll send new patch for that :)

Quoted 6 lines

thanks,

greg k-h

#3 lore raw
Quoted 14 lines · jeffy

Hi Greg,

Thanks for your reply.

Quoted 6 lines · Greg Kroah-Hartman
Quoted 2 lines · Jeffy Chen
Quoted 2 lines

There are cases we call device_del() without detaching it from the driver(e.g. spi core del children devices).

Why would you do that? Shouldn't that be fixed instead of this odd work-around for a broken bus?

i was thinking since the device_unregister() is called everywhere, maybe there are some other drivers missing that too?

Quoted 6 lines

and the driver calls device_add(), then the core attaches the device to the driver automatically, maybe it would make sense to let the core automatic detach it somehow...

but i know nothing about driver core, maybe i should fix it in spi core, i'll send new patch for that :)

The SPI core should have some internal housekeeping to do there as well, right? Is this the only bus that gets this "wrong"?

thanks,

greg k-h

author
#4 lore raw

Hi Greg,

Quoted 3 lines · Greg Kroah-Hartman

The SPI core should have some internal housekeeping to do there as well, right? Is this the only bus that gets this "wrong"?

hmm, i checked i2c code, it has the same issue:

void i2c_unregister_device(struct i2c_client *client)

{
        if (client->dev.of_node)
                of_node_clear_flag(client->dev.of_node, OF_POPULATED);
        if (ACPI_COMPANION(&client->dev))
                acpi_device_clear_enumerated(ACPI_COMPANION(&client->dev));
        device_unregister(&client->dev);
}

not sure about others, maybe mfd-core too...

Quoted 6 lines

thanks,

greg k-h

#5 lore raw
In reply to Greg Kroah-Hartman
Quoted 6 lines · Greg Kroah-Hartman
Quoted 2 lines · Jeffy Chen

There are cases we call device_del() without detaching it from the driver(e.g. spi core del children devices).

Why would you do that? Shouldn't that be fixed instead of this odd work-around for a broken bus?

Not that I ever looked at that bit of the SPI stack before but this feels like an interface bug in the driver core, it's really surprising that unregistering a device doesn't clean it up. That's what other unregister interfaces do. If this is buggy it looks like the platform bus will also be buggy, it's just doing a del and a put (plus some stuff to free resources) which is all device_unregster() does.

#6 lore raw
Quoted 13 lines · CEST Mark Brown

--3mkxuqf5z23bfztf
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

Quoted 6 lines · Greg Kroah-Hartman
Quoted 2 lines · Jeffy Chen

There are cases we call device_del() without detaching it from the driver(e.g. spi core del children devices).

Why would you do that? Shouldn't that be fixed instead of this odd work-around for a broken bus?

Quoted 6 lines

Not that I ever looked at that bit of the SPI stack before but this feels like an interface bug in the driver core, it's really surprising that unregistering a device doesn't clean it up. That's what other unregister interfaces do. If this is buggy it looks like the platform bus will also be buggy, it's just doing a del and a put (plus some stuff to free resources) which is all device_unregster() does.

device_del() calls bus_remove_device() which then calls device_release_driver() eventually.

So there is something going wrong, but that's not a missing device_release_driver() call. :-)

#7 lore raw
In reply to Jeffy Chen
Quoted 2 lines · CEST Jeffy Chen

There are cases we call device_del() without detaching it from the driver(e.g. spi core del children devices).

But device_del() itself detaches the device from its driver.

Quoted 12 lines

---

 drivers/base/core.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/base/core.c +0 -0
index 12ebd055724c..717efc3020af 100644

drivers/base/core.c void device_del(struct device *dev) · line 1958

Quoted 8 lines
drivers/base/core.c +2 -0
@@ -1951,6 +1951,8 @@ void device_del(struct device *dev)
 	struct kobject *glue_dir = NULL;
 	struct class_interface *class_intf;
 
+	device_release_driver(dev);
+
 	/* Notify clients of device removal.  This call must come
 	 * before dpm_sysfs_remove().
 	 */

But device_del() calls bus_remove_device() which in turn calls device_release_driver(), so this looks like an ordering issue to me.

What *exactly* is not working? Or rather, what symptoms do you see?

Thanks,
Rafael

author
#8 lore raw

Hi Rafael,

Thanks for your reply.

Quoted 3 lines · Rafael J. Wysocki

But device_del() calls bus_remove_device() which in turn calls device_release_driver(), so this looks like an ordering issue to me.

uh, right, didn't notice that, it indeed to be a ordering issue...

it turns out in device_del() we are calling device_links_purge() before bus_remove_device()...

Quoted 2 lines

What*exactly* is not working? Or rather, what symptoms do you see?

my board has these devices:
spi master device->spi child device->spi based pwm->pwm_bl

and i add a device link to the pwm and pwm_bl, and got a warning about the pwm not unbound:

static void device_links_purge(struct device *dev) {
...
list_for_each_entry_safe_reverse(link, ln,

&dev->links.consumers, s_node) {
                WARN_ON(link->status != DL_STATE_DORMANT &&
                        link->status != DL_STATE_NONE); <-- warning here!
                __device_link_del(link);
        }

and i was confused by:
static void mac80211_hwsim_del_radio(struct mac80211_hwsim_data *data,
const char *hwname, struct genl_info *info)
{
...

        device_release_driver(data->dev);
        device_unregister(data->dev);

and adding that device_release_driver hide that issue(oops...)

so we should move device_links_purge() after bus_remove_device() in device_del() right? maybe after device_remove_properties(), and that did solve the issue :)

Quoted 2 lines

Thanks,
Rafael

author
#9 lore raw

Hi Rafael,

Quoted 6 lines · jeffy
Quoted 1 line

my board has these devices:
spi master device->spi child device->spi based pwm->pwm_bl

and i add a device link to the pwm and pwm_bl, and got a warning about the pwm not unbound:

sorry, it happens when i try to unbind the spi child device, and it's warning about the consumer(pwm_bl) not unbound.

Quoted 10 lines

static void device_links_purge(struct device *dev) {
...
list_for_each_entry_safe_reverse(link, ln,

&dev->links.consumers, s_node) {
                 WARN_ON(link->status != DL_STATE_DORMANT &&
                         link->status != DL_STATE_NONE); <-- warning here!
                 __device_link_del(link);
         }

and i've send a new patch to reorder the device_links_purge() and bus_remove_device, thanks again :)

#10 lore raw
Quoted 22 lines · CEST jeffy

Hi Rafael,

Quoted 6 lines · jeffy
Quoted 1 line

my board has these devices:
spi master device->spi child device->spi based pwm->pwm_bl

and i add a device link to the pwm and pwm_bl, and got a warning about the pwm not unbound:

sorry, it happens when i try to unbind the spi child device, and it's warning about the consumer(pwm_bl) not unbound.

Quoted 10 lines

static void device_links_purge(struct device *dev) {
...
list_for_each_entry_safe_reverse(link, ln,

&dev->links.consumers, s_node) {
                 WARN_ON(link->status != DL_STATE_DORMANT &&
                         link->status != DL_STATE_NONE); <-- warning here!
                 __device_link_del(link);
         }
Quoted 2 lines

and i've send a new patch to reorder the device_links_purge() and bus_remove_device, thanks again :)

Which I'm not sure is the right approach just yet.

I'll follow up in the patch thread in any case.

Thanks,
Rafael

#11 lore raw
In reply to jeffy
Quoted 22 lines · CEST jeffy

Hi Rafael,

Quoted 6 lines · jeffy
Quoted 1 line

my board has these devices:
spi master device->spi child device->spi based pwm->pwm_bl

and i add a device link to the pwm and pwm_bl, and got a warning about the pwm not unbound:

sorry, it happens when i try to unbind the spi child device, and it's warning about the consumer(pwm_bl) not unbound.

Quoted 10 lines

static void device_links_purge(struct device *dev) {
...
list_for_each_entry_safe_reverse(link, ln,

&dev->links.consumers, s_node) {
                 WARN_ON(link->status != DL_STATE_DORMANT &&
                         link->status != DL_STATE_NONE); <-- warning here!
                 __device_link_del(link);
         }
Quoted 2 lines

and i've send a new patch to reorder the device_links_purge() and bus_remove_device, thanks again :)

Which I'm not sure is the right approach just yet.

I'll follow up in the patch thread in any case.

Thanks,
Rafael