From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1793831950; b=q1riMhrmHFPn40Z8sQaFsvIUfNrCTx8n4mowRovck3WCV5BqjHaiQs9+ZwAvmQIbfV5h mFe10JxqKQKUJcF/myOd6GU5K7NxbWuMPd8j1rga5AmWpb3h+/Z5fJs/JKKZ4gdRs/R58 aOgK0VKJLJwfkZYyY5TYS/qg/2mnf3naGgDNnXWzAvPrc/BB4E0i6AB/B4xS4h8ZuUR9I AUHVqdIvjxmEjYjDOPLoYFBPipGbrLCkD6ofFWr2kdzsQee5f2NZvbE71RxKnkqIJNmxB sENzdpqb3mBGfBhzOrba53wyq0KfVrmxY4vG+K0yhloH9ySDzsS9OfGIrXihcw/D+RQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1793831950; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:User-Agent:X-stable:X-Patchwork-Hint: MIME-Version:Content-Transfer-Encoding; bh=bVrqBi24UWGSnFyxrzZUylAEnCsZAzuGZuQ2Vnjw9yY=; b=FGkr5n9MKGbw+rjT7fuJEFEKXt3tnDZnOrPlaVmF2EHRIcpW5lSXmtAvZt+Py71o/EKv H7KcGzm04cFbGm3MPPrcw31cUZ5SY05p6vi/0AJmIEFQQdUnHLjqGFPEyELk9tfPUNIIP U2PADfhqw24fLLSRrfFD44tukTcoctZ7Z2iBozRScoZVfMQo6FOaV5e9kBUIBNdK/LDSm m2Ffz+4akidcVsRHUN9tql6lizxBl+D9IVBCy+ArifZTj/EiSuIBkDSHge9ZLDCDwoaZn qZDdaVf0KDWoait1k3myrQwhTjbu66M8Zo+v8EhF2c6h7uX8bY79ERsJDyAuv2R4V7Q== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=MQwRrQqn; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2E9A9606A871; Wed, 4 Nov 2026 22:39:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key, unprotected) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=MQwRrQqn Received: by smtp.kernel.org (Postfix) with ESMTPSA id BE8101F00919; Wed, 4 Nov 2026 22:39:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1793831950; bh=bVrqBi24UWGSnFyxrzZUylAEnCsZAzuGZuQ2Vnjw9yY=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=MQwRrQqnDgc7P5gHjmBPOunDButxc5VU4T+7rL3AdaSEW2kVwVyTH/tOit46cqWpN Aty40hlIJbGEVw2KPSAvjrNh7WEMgYrvMUzNxxmDGs7MeuGviiBS7LInCLtZqjBoZL om6QvyvA9wABDoi7FmXifBiGnW2V9GEC7gh+NAyk= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Mario Peter , Dmitry Torokhov , Johan Hovold , Danilo Krummrich , Sasha Levin Subject: [PATCH 6.18 0985/1005] driver core: take parent lock in async device attach Date: Wed, 4 Nov 2026 16:11:58 -0500 Message-ID: <20261104211110.632134139@linuxfoundation.org> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261104211043.516122098@linuxfoundation.org> References: <20261104211043.516122098@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: patches@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 6.18-stable review patch. If anyone has any objections, please let me know. ------------------ From: Mario Peter [ Upstream commit 187a62f99da29b1a55602b0d401a783d8dc86161 ] For device types with need_parent_lock set (only USB interfaces), probe() must run with the parent device locked. A synchronous attach after device_add() gets this from the caller, e.g. usb_set_configuration() holds the udev lock while adding interfaces. __device_attach_async_helper() runs the probe from an async worker instead, where that lock isn't held, and only takes device_lock(dev). With async probing enabled for the hub driver (e.g. driver_async_probe=* or driver_async_probe=hub), this races when a multi-TT hub is configured. For multi-TT hubs, hub_probe() calls usb_set_interface(hdev, 0, 1) to select the TT-per-port altsetting, and usb_set_interface() creates the endpoint devices of the interface a second time: usb_set_configuration() async worker [udev locked] device_add(intf) queues the probe ----------> __device_attach_async_helper() [intf locked, udev not locked] hub_probe() usb_set_interface(hdev, 0, 1) create_intf_ep_devs(intf) create_intf_ep_devs(intf) create_intf_ep_devs() checks and sets intf->ep_devs_created without a lock of its own. If usb_set_configuration() has created ep_81 but not yet set the flag, usb_set_interface() skips removing the endpoint devices of the old altsetting and creates ep_81 again. With a synchronous probe, hub_probe() runs inside device_add() with the udev lock held, and usb_set_configuration() then finds ep_devs_created already set. Single-TT hubs don't call usb_set_interface() from hub_probe() and don't show this warning, but their probe runs without the udev lock as well. On an i.MX8MM board with an onboard USB2514 hub this hit 11 of 100 boots: sysfs: cannot create duplicate filename '.../1-1/1-1:1.0/ep_81' Call trace: sysfs_warn_dup usb_create_ep_devs create_intf_ep_devs usb_set_interface hub_probe usb_probe_interface really_probe __device_attach_async_helper async_run_entry_fn It also shows on v7.3-rc6 in QEMU, with dummy_hcd and a raw-gadget program emulating a multi-TT hub: re-authorizing the hub in a loop, while a SCHED_FIFO task preempts the writing task at random points for 3 ms, hit the warning in 29 of 6000 iterations. Take the parent lock in __device_attach_async_helper() as well, like __driver_attach_async_helper() does, and move __device_driver_lock/unlock() up for that. With this the warning was gone in 300 boots on the board and in 6000 iterations in QEMU. Fixes: 765230b5f084 ("driver-core: add asynchronous probing support for drivers") Signed-off-by: Mario Peter Cc: stable@vger.kernel.org # 4.2 Cc: Dmitry Torokhov [ johan: switch to async_schedule_dev_nocall() to avoid deadlock if async_schedule_dev() runs synchronously when parent lock is held ] Signed-off-by: Johan Hovold Link: https://patch.msgid.link/20261014133109.1392265-4-johan@kernel.org [ Fold the nested conditions into a single if statement; correct commit message to mention "device types" rather than "buses" for "need_parent_lock". - Danilo ] Signed-off-by: Danilo Krummrich Signed-off-by: Sasha Levin Signed-off-by: Greg Kroah-Hartman --- drivers/base/dd.c | 83 +++++++++++++++++++++++++++++------------------------- 1 file changed, 45 insertions(+), 38 deletions(-) --- a/drivers/base/dd.c +++ b/drivers/base/dd.c @@ -1042,6 +1042,38 @@ static int __device_attach_driver(struct return ret == 0; } +/* + * __device_driver_lock - acquire locks needed to manipulate dev->drv + * @dev: Device we will update driver info for + * @parent: Parent device. Needed if the device type requires parent lock + * + * This function will take the required locks for manipulating dev->drv. + * Normally this will just be the @dev lock, but when called for a USB + * interface, @parent lock will be held as well. + */ +static void __device_driver_lock(struct device *dev, struct device *parent) +{ + if (dev->type && dev->type->need_parent_lock && parent) + device_lock(parent); + device_lock(dev); +} + +/* + * __device_driver_unlock - release locks needed to manipulate dev->drv + * @dev: Device we will update driver info for + * @parent: Parent device. Needed if the device type requires parent lock + * + * This function will release the required locks for manipulating dev->drv. + * Normally this will just be the @dev lock, but when called for a + * USB interface, @parent lock will be released as well. + */ +static void __device_driver_unlock(struct device *dev, struct device *parent) +{ + device_unlock(dev); + if (dev->type && dev->type->need_parent_lock && parent) + device_unlock(parent); +} + static void __device_attach_async_helper(void *_dev, async_cookie_t cookie) { struct device *dev = _dev; @@ -1051,7 +1083,7 @@ static void __device_attach_async_helper .want_async = true, }; - device_lock(dev); + __device_driver_lock(dev, dev->parent); /* * Check if device has already been removed or claimed. This may @@ -1073,7 +1105,7 @@ static void __device_attach_async_helper if (dev->parent) pm_runtime_put(dev->parent); out_unlock: - device_unlock(dev); + __device_driver_unlock(dev, dev->parent); put_device(dev); } @@ -1130,8 +1162,11 @@ static int __device_attach(struct device } out_unlock: device_unlock(dev); - if (async) - async_schedule_dev(__device_attach_async_helper, dev); + if (async && !async_schedule_dev_nocall(__device_attach_async_helper, dev)) { + put_device(dev); + ret = 2; /* retry synchronously */ + } + return ret; } @@ -1158,48 +1193,20 @@ EXPORT_SYMBOL_GPL(device_attach); void device_initial_probe(struct device *dev) { struct subsys_private *sp = bus_to_subsys(dev->bus); + int ret; if (!sp) return; - if (sp->drivers_autoprobe) - __device_attach(dev, true); + if (sp->drivers_autoprobe) { + ret = __device_attach(dev, true); + if (ret == 2) + __device_attach(dev, false); + } subsys_put(sp); } -/* - * __device_driver_lock - acquire locks needed to manipulate dev->drv - * @dev: Device we will update driver info for - * @parent: Parent device. Needed if the device type requires parent lock - * - * This function will take the required locks for manipulating dev->drv. - * Normally this will just be the @dev lock, but when called for a USB - * interface, @parent lock will be held as well. - */ -static void __device_driver_lock(struct device *dev, struct device *parent) -{ - if (dev->type && dev->type->need_parent_lock && parent) - device_lock(parent); - device_lock(dev); -} - -/* - * __device_driver_unlock - release locks needed to manipulate dev->drv - * @dev: Device we will update driver info for - * @parent: Parent device. Needed if the device type requires parent lock - * - * This function will release the required locks for manipulating dev->drv. - * Normally this will just be the @dev lock, but when called for a - * USB interface, @parent lock will be released as well. - */ -static void __device_driver_unlock(struct device *dev, struct device *parent) -{ - device_unlock(dev); - if (dev->type && dev->type->need_parent_lock && parent) - device_unlock(parent); -} - /** * device_driver_attach - attach a specific driver to a specific device * @drv: Driver to attach