From: sashiko-bot@kernel.org
To: "Lorenzo Ferrari" <loreferro01@gmail.com>
Cc: linux-usb@vger.kernel.org
Subject: Re: [PATCH] usb: phy: Link consumers to the PHY device in the devm helpers
Date: Wed, 02 Dec 2026 15:49:50 +0000 [thread overview]
Message-ID: <sashiko-outbox-246151@kernel.org> (raw)
In-Reply-To: <20261202153345.165581-1-loreferro01@gmail.com>
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 <loreferro01@gmail.com>
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);
> }
>
> +/*
> + * 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 reference,
> + * before the PHY driver goes away. The link is dropped when the consumer is
> + * unbound, together with the devres entry holding the reference.
> + */
> +static void usb_phy_link_consumer(struct device *dev, struct usb_phy *phy)
> +{
> + 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 = &lane->pad->lanes[port->index]->dev;
port->usb_phy.dev->driver = port->dev.driver;
...
err = 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 == DL_DEV_NO_DRIVER
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 != 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 = phy;
> devres_add(dev, ptr);
> + usb_phy_link_consumer(dev, phy);
> } else
> devres_free(ptr);
>
[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 = 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 = 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 device *dev,
> err1:
> spin_unlock_irqrestore(&phy_lock, flags);
>
> + if (!IS_ERR(phy))
> + usb_phy_link_consumer(dev, phy);
> +
> err0:
>
> 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 = __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().
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261202153345.165581-1-loreferro01@gmail.com?part=1
next prev parent reply other threads:[~2026-12-02 15:49 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-02 15:33 [PATCH] usb: phy: Link consumers to the PHY device in the devm helpers Lorenzo Ferrari
2026-12-02 15:49 ` sashiko-bot [this message]
2026-12-02 16:08 ` lorenzo ferrari
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=sashiko-outbox-246151@kernel.org \
--to=sashiko-bot@kernel.org \
--cc=linux-usb@vger.kernel.org \
--cc=loreferro01@gmail.com \
--cc=sashiko-reviews@lists.linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.