* [PATCH] usb: phy: Link consumers to the PHY device in the devm helpers
@ 2026-12-02 15:33 Lorenzo Ferrari
2026-12-02 15:49 ` sashiko-bot
0 siblings, 1 reply; 3+ messages in thread
From: Lorenzo Ferrari @ 2026-12-02 15:33 UTC (permalink / raw)
To: Greg Kroah-Hartman; +Cc: linux-usb, Lorenzo Ferrari
At probe time, the PHY driver creates the struct usb_phy that describes
the PHY: phy-generic, for instance, embeds it in a devm_kzalloc()'d
struct usb_phy_generic. A consumer, such as a USB controller, obtains a
pointer to it through devm_usb_get_phy() or devm_usb_get_phy_by_node()
and uses it for as long as it is bound.
This object belongs to the PHY driver, and devres frees it as soon as
the PHY driver is unbound from its device. The references taken when
the consumer gets the PHY, try_module_get() and get_device(), only pin
the PHY driver module and the PHY struct device. They do not block the
one event that actually frees the object, the unbinding of the PHY
driver, and the consumer is not notified when it happens.
The crash can be triggered in two steps:
1) Unbind the PHY driver: devres frees the struct usb_phy and
dev->driver is cleared, but the consumer still holds a pointer that
now points to freed memory.
2) Unbind the consumer: devm_usb_phy_release() calls usb_put_phy() on
the freed struct usb_phy, which dereferences x->dev->driver->owner
with a NULL driver:
BUG: kernel NULL pointer dereference, address: 0000000000000010
RIP: 0010:devm_usb_phy_release+0x14/0x30
Call Trace:
devres_release_all
device_unbind_cleanup
device_release_driver_internal
unbind_store
The NULL pointer dereference is only the symptom of a use-after-free:
with KASAN, the first bad access is reported as a slab-use-after-free
in devm_usb_phy_release(), on memory freed by the PHY driver unbind.
Add a managed device link from the consumer to the PHY device in the
devm helpers, which know the consumer device. The driver core then
unbinds the consumer, releasing its PHY reference, before the PHY
driver. DL_FLAG_AUTOREMOVE_CONSUMER drops the link when the consumer is
unbound, together with the devres entry holding the reference. Failing
to create the link is not fatal and keeps the current behaviour.
usb_get_phy() has no consumer device and is left unchanged.
As a side effect, unbinding the PHY driver now also unbinds its
consumers, and the consumer is moved after the PHY in the suspend,
resume and shutdown order. The link must be managed: a stateless one,
as used by the generic PHY framework, does not unbind consumers.
Fixes: 410219dcd2ba ("usb: otg: utils: devres: Add API's to associate a device with the phy")
Assisted-by: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Lorenzo Ferrari <loreferro01@gmail.com>
---
Reproduced on 4405c18c95d2 in QEMU (x86_64, KASAN) with phy-generic and a
small test module whose probe calls devm_usb_get_phy(dev, USB_PHY_TYPE_USB2):
echo -n usb_phy_generic.0.auto > /sys/bus/platform/drivers/usb_phy_generic/unbind
echo -n usbphy_consumer > /sys/bus/platform/drivers/usbphy_consumer/unbind
Without this patch: the oops above, or a KASAN slab-use-after-free.
With it: the PHY unbind unbinds the consumer first, KASAN reports nothing,
and the link is removed on consumer unbind and recreated on rebind.
First seen on an ARM32 board (same oops, faulting address 0x8).
The test module is available on request.
drivers/usb/phy/phy.c | 24 ++++++++++++++++++++++--
1 file changed, 22 insertions(+), 2 deletions(-)
diff --git a/drivers/usb/phy/phy.c b/drivers/usb/phy/phy.c
index 5a9b9353f3..6e0f21f9a6 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));
+}
+
static void usb_charger_init(struct usb_phy *usb_phy)
{
usb_phy->chg_type = UNKNOWN_TYPE;
@@ -450,7 +464,8 @@ static int usb_add_extcon(struct usb_phy *x)
*
* Gets the phy using usb_get_phy(), and associates a device with it using
* devres. On driver detach, release function is invoked on the devres data,
- * then, devres data is freed.
+ * then, devres data is freed. The device is also linked to the phy device,
+ * so that it is unbound before the phy driver.
*
* For use by USB host and peripheral drivers.
*/
@@ -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);
@@ -520,7 +536,8 @@ EXPORT_SYMBOL_GPL(usb_get_phy);
* -EPROBE_DEFER if the device is not yet loaded. While at that, it
* also associates the device with
* the phy using devres. On driver detach, release function is invoked
- * on the devres data, then, devres data is freed.
+ * on the devres data, then, devres data is freed. The device is also
+ * linked to the phy device, so that it is unbound before the phy driver.
*
* For use by peripheral drivers for devices related to a phy,
* such as a charger.
@@ -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;
base-commit: 4405c18c95d2d0f99f6c581ef0ee8591a1aa6ccd
--
2.56.1
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH] usb: phy: Link consumers to the PHY device in the devm helpers
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
2026-12-02 16:08 ` lorenzo ferrari
0 siblings, 1 reply; 3+ messages in thread
From: sashiko-bot @ 2026-12-02 15:49 UTC (permalink / raw)
To: Lorenzo Ferrari; +Cc: linux-usb
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
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] usb: phy: Link consumers to the PHY device in the devm helpers
2026-12-02 15:49 ` sashiko-bot
@ 2026-12-02 16:08 ` lorenzo ferrari
0 siblings, 0 replies; 3+ messages in thread
From: lorenzo ferrari @ 2026-12-02 16:08 UTC (permalink / raw)
To: sashiko-reviews, Greg Kroah-Hartman; +Cc: linux-usb
On Wed, 2 Dec 2026, sashiko-bot@kernel.org wrote:
> - [High] usb: phy: self-deadlock on unbind or probe error via
> usb_phy_link_consumer()
Confirmed. I reproduced it in QEMU with a consumer that registers its
own usb_phy_generic in probe() and unregisters it in remove(), as
mtk_musb_probe() and sunxi_musb_probe() do: unbinding the consumer now
hangs in device_links_unbind_consumers(), waiting for its own device
lock.
> - [High] usb: phy: broken managed link to phy_class device in
> usb_phy_link_consumer()
Right as far as I can tell from the code: on Tegra the usb_phy device
is a generic PHY class device that is never bound through the driver
core, so the managed link starts DORMANT.
Greg, please drop this patch. A managed device link in the generic devm
helpers is not safe for consumers that own their PHY device. I will
rework the fix and send a v2.
Thanks,
Lorenzo
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-12-02 16:08 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
2026-12-02 16:08 ` lorenzo ferrari
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox