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=1796226591; b=qeGwCBeruSI82+WfG6uwAw3ApevwoSTjkWKkDQnJxeHE34vXNPXkRpBW5hvVLdTwkBT0 TN2TY93ISWMRBaoZyYs3qdGRQsaj6WATb2+qQ8UTxUdbL+ysQHdO08ihIDuq48Qf2BJfZ zapJJGQYbOphMWq/vqtSKEDnW40il6YCKE4kiOQQestKKVP+JisdnTUIA36plaqC5Te3Z grZo/T0ADBz4zGNA05bsMUQXJTGNEUPxJTzBzpTrqQ5q69jye+qUAKMuv7UH/dOkrs6dv VZY8nAp0cXvEtI4xeSVjT3ABaNvJk16kxHGXCDUYiF3ZJyVJBQ8sHzN9iT2q4hSN3Bw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796226591; h=Received:DKIM-Signature:Message-ID:From:Subject:Reply-To:To:Cc: In-Reply-To:References:Content-Type:Content-Transfer-Encoding:Date; bh=iPfTvew+t/hZ68BL3A1uA1WYlHcgeWxA1ELNPgddJFA=; b=Xkv/7ls0wheSJLkAsXMSPiH9yKPbTSIe3iCZKNQlEIbq/te4QV1piNX/BdnoZ0DPYQ7i yvZZBoUIXC82lk+Bi9DXaS9rA0rbRCKqcShHno/yUQcPkdHEuL62VIXvK2r4efGWlhk9b CyqaXoTT53PFG/h/5vKDMZ4eazuTcLSZRfbScI9GoB7FmZrsfI9QnluHA+Vl7IiJqM07D h3Cv0gAAT5EI3iuILrokMsc9Ma8svhKqjHS+qseb8+/NL9ucJHlB3LixvFnYU7/u01+uh Nrd+d3IpJqO9yZ0g8XPcRrh8QCtczEvTmYJaxzjr3EiXs+r03SufY0/TJxgPvwN17YA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=LHZ39aRh; 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 10DD5606A322 for ; Wed, 2 Dec 2026 15:49:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=LHZ39aRh Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8A72D1F000C1; Wed, 2 Dec 2026 15:49:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796226590; bh=iPfTvew+t/hZ68BL3A1uA1WYlHcgeWxA1ELNPgddJFA=; h=From:Subject:Reply-To:To:Cc:In-Reply-To:References:Date; b=LHZ39aRhv4/4mbyyjUf+qoPqOGTFnq3UpOEMGz9yAp0fleFyB5nlGZRFC2/O/NFyB +TyxXXjlq6TUk7gIGqIDOcnY4pwnPMF1fkeakRJ77wkIJPz4LFF7PRhwOKgHkPa+SS lGKtHyX7KgUfzmKXXK6oMoXRQWL6XlpG//SofhU3Z+4Ew3gOOZxe8cAeS0Sp8A0boM Z47DtLqhh9NMF5sBIj7aLAiYi+D3zTJ1V1zT7A0rI1SpDgPbvQ4ZlRqIKDqV7VhuYG Ujy5VLhTNMt7tvKl37XxJVvvZcpiwQIqkDRHJUWcPPWnaQ0OsJ4RoVLgTQkEMAj8/Z ZJG+oIPkFf0WQ== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH] usb: phy: Link consumers to the PHY device in the devm helpers Reply-To: sashiko-reviews@lists.linux.dev To: "Lorenzo Ferrari" Cc: linux-usb@vger.kernel.org In-Reply-To: <20261202153345.165581-1-loreferro01@gmail.com> References: <20261202153345.165581-1-loreferro01@gmail.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Wed, 02 Dec 2026 15:49:50 +0000 X-Mailing-List: linux-usb@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Thank you for your contribution! Sashiko AI review found 3 potential issues to consider: - [High] usb: phy: self-deadlock on unbind or probe error via usb_phy_link_consumer() - [High] usb: phy: broken managed link to phy_class device in usb_phy_link_consumer() - [Medium] usb: phy: race between phy_lock release and usb_phy_link_consumer() on unbind -- commit 914be83c8acbaf1276d7d528190fab249213209d Author: Lorenzo Ferrari usb: phy: Link consumers to the PHY device in the devm helpers This commit adds a managed device link (DL_FLAG_AUTOREMOVE_CONSUMER) from the consumer device to the PHY device in devm_usb_get_phy() and devm_usb_get_phy_by_node() so that the consumer is unbound before the PHY driver releases struct usb_phy. > diff --git a/drivers/usb/phy/phy.c b/drivers/usb/phy/phy.c > index 5a9b9353f343d..6e0f21f9a6b0a 100644 > --- a/drivers/usb/phy/phy.c > +++ b/drivers/usb/phy/phy.c > @@ -346,6 +346,20 @@ static void devm_usb_phy_release2(struct device *dev= , void *_res) > usb_put_phy(res->phy); > } > =20 > +/* > + * The struct usb_phy belongs to the PHY driver, which usually allocates= it > + * with devm and frees it on unbind. Link the consumer to the PHY device= so > + * that the driver core unbinds the consumer, and thus drops its referen= ce, > + * before the PHY driver goes away. The link is dropped when the consume= r is > + * unbound, together with the devres entry holding the reference. > + */ > +static void usb_phy_link_consumer(struct device *dev, struct usb_phy *ph= y) > +{ > + if (!device_link_add(dev, phy->dev, DL_FLAG_AUTOREMOVE_CONSUMER)) > + dev_dbg(dev, "failed to create device link to %s\n", > + dev_name(phy->dev)); > +} [Severity: High] Does calling device_link_add() with DL_FLAG_AUTOREMOVE_CONSUMER here break consumers when phy->dev is a class device that never binds to a driver through the driver core? In tegra_xusb_setup_usb_role_switch(), port->usb_phy.dev is set to a generic struct phy class device created by phy_create() (with a dummy dev->driver pointer), so phy->dev->links.status remains DL_DEV_NO_DRIVER: drivers/phy/tegra/xusb.c:tegra_xusb_setup_usb_role_switch() { ... port->usb_phy.dev =3D &lane->pad->lanes[port->index]->dev; port->usb_phy.dev->driver =3D port->dev.driver; ... err =3D usb_add_phy_dev(&port->usb_phy); ... } When tegra_xusb_init_usb_phy() in xhci-tegra (or tegra_xudc_phy_get() in tegra-xudc) probes, devm_phy_optional_get() first creates a DL_FLAG_STATELESS link to &phy->dev, and devm_usb_get_phy_by_node() then calls usb_phy_link_consumer() on the same pair of devices. device_link_add() finds the existing stateless link and upgrades it to DL_FLAG_MANAGED without setting DL_FLAG_AUTOREMOVE_CONSUMER, while device_link_init_status() sees supplier->links.status =3D=3D DL_DEV_NO_DRIV= ER and initializes link->status to DL_STATE_DORMANT instead of DL_STATE_CONSUMER_PROBE. When the consumer finishes probing, device_links_driver_bound() hits WARN_ON(link->status !=3D DL_STATE_CONSUMER_PROBE). If probe later fails or defers, or if the consumer is unbound, the managed reference is never dropped (because DL_FLAG_AUTOREMOVE_CONSUMER was not set on the upgraded link) and the leaked link stays in DL_STATE_DORMANT permanently, causing device_links_check_suppliers() to reject all subsequent probe or rebind attempts of the USB controller with -EPROBE_DEFER. [ ... ] > @@ -466,6 +481,7 @@ struct usb_phy *devm_usb_get_phy(struct device *dev, = enum usb_phy_type type) > if (!IS_ERR(phy)) { > *ptr =3D phy; > devres_add(dev, ptr); > + usb_phy_link_consumer(dev, phy); > } else > devres_free(ptr); > =20 [Severity: High] Can this cause a self-deadlock on unbind or probe error for glue drivers that register and consume their own usb_phy_generic platform device? In mtk_musb_probe() (and similarly sunxi_musb_probe() and ci_hdrc_pci_probe()), the driver calls usb_phy_generic_register(), which registers a platform device under platform_bus, and then passes its own device to devm_usb_get_phy(): drivers/usb/musb/mediatek.c:mtk_musb_probe() { ... glue->usb_phy =3D usb_phy_generic_register(); if (IS_ERR(glue->usb_phy)) return dev_err_probe(dev, PTR_ERR(glue->usb_phy), "fail to registering usb-phy\n"); glue->xceiv =3D devm_usb_get_phy(dev, USB_PHY_TYPE_USB2); ... } Because glue->usb_phy->dev is not a child of the glue device, usb_phy_link_consumer() creates a managed link with the glue device as consumer and glue->usb_phy->dev as supplier. When the glue driver is unbound, device_remove() runs before device_links_driver_cleanup() removes the link: device_release_driver_internal(&pdev->dev, ...) device_lock(&pdev->dev) __device_release_driver(&pdev->dev, ...) device_remove(&pdev->dev) mtk_musb_remove(pdev) usb_phy_generic_unregister(glue->usb_phy) ... __device_release_driver(&usb_phy->dev, NULL) device_links_unbind_consumers(&usb_phy->dev) device_release_driver_internal(&pdev->dev, ...) device_lock(&pdev->dev) <-- deadlocks on self ... device_links_driver_cleanup(&pdev->dev) Likewise, if mtk_musb_probe() fails after devm_usb_get_phy() and calls usb_phy_generic_unregister() in its error path while the link is still in DL_STATE_CONSUMER_PROBE, device_links_unbind_consumers() calls wait_for_device_probe() and blocks forever waiting for the current probe to finish. [ ... ] > @@ -563,6 +580,9 @@ struct usb_phy *devm_usb_get_phy_by_node(struct devi= ce *dev, > err1: > spin_unlock_irqrestore(&phy_lock, flags); > =20 > + if (!IS_ERR(phy)) > + usb_phy_link_consumer(dev, phy); > + > err0: > =20 > return phy; [Severity: Medium] Can the PHY driver still unbind between dropping phy_lock and calling device_link_add() in usb_phy_link_consumer(), both here and in devm_usb_get_phy()? CPU0 CPU1 devm_usb_get_phy_by_node() spin_lock_irqsave(&phy_lock, ...) phy =3D __of_usb_find_phy(node) try_module_get(...) get_device(phy->dev) spin_unlock_irqrestore(&phy_lock, ...) __device_release_driver(phy->dev) device_links_busy(phy->dev) device_remove(phy->dev) usb_remove_phy(phy) device_unbind_cleanup(phy->dev) devres_release_all(phy->dev) /* frees struct usb_phy */ usb_phy_link_consumer(dev, phy) device_link_add(dev, phy->dev, ...) <-- use-after-free on phy Because no device link exists yet when phy_lock is released, a concurrent unbind of the PHY driver on CPU1 can pass device_links_busy() and free the devm-allocated struct usb_phy before CPU0 calls device_link_add(). --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261202153345.1655= 81-1-loreferro01@gmail.com?part=3D1