drivers/base: use a worker for sysfs unbind
diff mbox series

Message ID
State New
Headers show
  • drivers/base: use a worker for sysfs unbind
Related show

Commit Message

Daniel Vetter Dec. 7, 2018, 4:01 p.m. UTC
Drivers might want to remove some sysfs files, which needs the same
locks and ends up angering lockdep. Relevant snippet of the stack

  i915_driver_unload+0x42/0x130 [i915]
  i915_pci_remove+0x19/0x30 [i915]

I've stumbled over this because some new patches by Ram connect the
snd-hda-intel unload (where we do use sysfs unbind) with the locking
chains in the i915 unload code (but without creating a new loop),
which upset our CI. But the bug is already there and can be easily
reproduced by unbind i915 directly.

No idea whether this is the correct place to fix this, should at least
get CI happy again.

Note that the bus locking is already done by device_release_driver ->
device_release_driver_internal, so I dropped that part. Also note that
we don't recheck that the device is still bound by the same driver,
but neither does the current code do that without races. And I figured
that's a obscure enough corner case to not bother.

Cc: Ramalingam C <>
Signed-off-by: Daniel Vetter <>
 drivers/base/bus.c | 35 +++++++++++++++++++++++++++++------
 1 file changed, 29 insertions(+), 6 deletions(-)

diff mbox series

diff --git a/drivers/base/bus.c b/drivers/base/bus.c
index 8bfd27ec73d6..2cc18545918a 100644
--- a/drivers/base/bus.c
+++ b/drivers/base/bus.c
@@ -174,22 +174,45 @@  static const struct kset_uevent_ops bus_uevent_ops = {
 static struct kset *bus_kset;
+struct unbind_work {
+	struct work_struct work;
+	struct device *dev;
+void unbind_work_fn(struct work_struct *work)
+	struct unbind_work *unbind_work =
+		container_of(work, struct unbind_work, work);
+	device_release_driver_internal(unbind_work->dev, NULL,
+				       unbind_work->dev->parent);
+	put_device(unbind_work->dev);
+	kfree(unbind_work);
 /* Manually detach a device from its associated driver. */
 static ssize_t unbind_store(struct device_driver *drv, const char *buf,
 			    size_t count)
 	struct bus_type *bus = bus_get(drv->bus);
+	struct unbind_work *unbind_work;
 	struct device *dev;
 	int err = -ENODEV;
 	dev = bus_find_device_by_name(bus, NULL, buf);
 	if (dev && dev->driver == drv) {
-		if (dev->parent && dev->bus->need_parent_lock)
-			device_lock(dev->parent);
-		device_release_driver(dev);
-		if (dev->parent && dev->bus->need_parent_lock)
-			device_unlock(dev->parent);
-		err = count;
+		unbind_work = kmalloc(sizeof(*unbind_work), GFP_KERNEL);
+		if (unbind_work) {
+			unbind_work->dev = dev;
+			get_device(dev);
+			INIT_WORK(&unbind_work->work, unbind_work_fn);
+			schedule_work(&unbind_work->work);
+			err = count;
+		} else {
+			err = -ENOMEM;
+		}