[PATCH v3 28/31] driver core: Allow driver attachment with the device lock held
From: Leon Romanovsky
Date: Wed Nov 18 2026 - 14:35:35 EST
From: Leon Romanovsky <leonro@xxxxxxxxxx>
USB authorization needs to keep driver attachment and its configuration
result under the same device lock. Dropping the lock to call
device_attach() would let another request change that result before the
authorizing caller can inspect it.
Provide synchronous attachment for callers that already hold the device
lock. Keep asynchronous scheduling outside the lock in the existing path.
Signed-off-by: Leon Romanovsky <leonro@xxxxxxxxxx>
---
drivers/base/dd.c | 34 ++++++++++++++++++++++++++++++----
include/linux/device.h | 1 +
2 files changed, 31 insertions(+), 4 deletions(-)
diff --git a/drivers/base/dd.c b/drivers/base/dd.c
index 4c4723384dc66..2f45b53e5ba91 100644
--- a/drivers/base/dd.c
+++ b/drivers/base/dd.c
@@ -1222,12 +1222,11 @@ static void __device_attach_async_helper(void *_dev, async_cookie_t cookie)
put_device(dev);
}
-static int __device_attach(struct device *dev, bool allow_async)
+static int __device_attach_locked(struct device *dev, bool allow_async,
+ bool *async)
{
int ret = 0;
- bool async = false;
- device_lock(dev);
mutex_lock(&dev->p->driver_lock);
if (dev->p->dead) {
goto out_unlock;
@@ -1265,7 +1264,7 @@ static int __device_attach(struct device *dev, bool allow_async)
*/
dev_dbg(dev, "scheduling asynchronous probe\n");
get_device(dev);
- async = true;
+ *async = true;
} else {
pm_request_idle(dev);
}
@@ -1275,6 +1274,16 @@ static int __device_attach(struct device *dev, bool allow_async)
}
out_unlock:
mutex_unlock(&dev->p->driver_lock);
+ return ret;
+}
+
+static int __device_attach(struct device *dev, bool allow_async)
+{
+ bool async = false;
+ int ret;
+
+ device_lock(dev);
+ ret = __device_attach_locked(dev, allow_async, &async);
device_unlock(dev);
if (async && !async_schedule_dev_nocall(__device_attach_async_helper, dev)) {
put_device(dev);
@@ -1284,6 +1293,23 @@ static int __device_attach(struct device *dev, bool allow_async)
return ret;
}
+/**
+ * device_attach_locked - try to attach a driver with the device locked
+ * @dev: device to attach
+ *
+ * Synchronous device_attach() with the device lock held by the caller.
+ * When called for a USB interface, the parent lock must also be held.
+ * The device lock remains held on return.
+ *
+ * Return: 1 if bound, 0 if no driver bound, or a negative error.
+ */
+int device_attach_locked(struct device *dev)
+{
+ device_lock_assert(dev);
+ return __device_attach_locked(dev, false, NULL);
+}
+EXPORT_SYMBOL_GPL(device_attach_locked);
+
/**
* device_attach - try to attach device to a driver.
* @dev: device.
diff --git a/include/linux/device.h b/include/linux/device.h
index 3db6657dffe41..05b8f4af36873 100644
--- a/include/linux/device.h
+++ b/include/linux/device.h
@@ -1337,6 +1337,7 @@ int __must_check device_driver_attach_unrestricted(
int __must_check device_bind_driver(struct device *dev);
void device_release_driver(struct device *dev);
int __must_check device_attach(struct device *dev);
+int __must_check device_attach_locked(struct device *dev);
int __must_check driver_attach(const struct device_driver *drv);
void device_initial_probe(struct device *dev);
int __must_check device_reprobe(struct device *dev);
--
2.56.1