Skip to content

Commit 5c8563d

Browse files
kaysieversgregkh
authored andcommitted
Driver Core: do not oops when driver_unregister() is called for unregistered drivers
We also fix a problem with cleaning up properly when initializing drivers and devices, so checks like this will work successfully. Portions of the patch by Linus and Greg and Ingo. Reported-by: Ozan Çağlayan <ozan@pardus.org.tr> Signed-off-by: Kay Sievers <kay.sievers@vrfy.org> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Ingo Molnar <mingo@elte.hu> Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
1 parent 086a377 commit 5c8563d

3 files changed

Lines changed: 11 additions & 2 deletions

File tree

‎drivers/base/bus.c‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -700,8 +700,10 @@ int bus_add_driver(struct device_driver *drv)
700700
}
701701

702702
kobject_uevent(&priv->kobj, KOBJ_ADD);
703-
return error;
703+
return 0;
704704
out_unregister:
705+
kfree(drv->p);
706+
drv->p = NULL;
705707
kobject_put(&priv->kobj);
706708
out_put_bus:
707709
bus_put(bus);

‎drivers/base/core.c‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -879,7 +879,7 @@ int device_add(struct device *dev)
879879
}
880880

881881
if (!dev_name(dev))
882-
goto done;
882+
goto name_error;
883883

884884
pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
885885

@@ -978,6 +978,9 @@ int device_add(struct device *dev)
978978
cleanup_device_parent(dev);
979979
if (parent)
980980
put_device(parent);
981+
name_error:
982+
kfree(dev->p);
983+
dev->p = NULL;
981984
goto done;
982985
}
983986

‎drivers/base/driver.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,10 @@ EXPORT_SYMBOL_GPL(driver_register);
257257
*/
258258
void driver_unregister(struct device_driver *drv)
259259
{
260+
if (!drv || !drv->p) {
261+
WARN(1, "Unexpected driver unregister!\n");
262+
return;
263+
}
260264
driver_remove_groups(drv, drv->groups);
261265
bus_remove_driver(drv);
262266
}

0 commit comments

Comments
 (0)