All of lore.kernel.org
 help / color / mirror / Atom feed
From: Lorenzo Ferrari <loreferro01@gmail.com>
To: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: linux-usb@vger.kernel.org, Lorenzo Ferrari <loreferro01@gmail.com>
Subject: [PATCH] usb: phy: Link consumers to the PHY device in the devm helpers
Date: Wed,  2 Dec 2026 16:33:45 +0100	[thread overview]
Message-ID: <20261202153345.165581-1-loreferro01@gmail.com> (raw)

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


             reply	other threads:[~2026-12-02 15:33 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-02 15:33 Lorenzo Ferrari [this message]
2026-12-02 15:49 ` [PATCH] usb: phy: Link consumers to the PHY device in the devm helpers sashiko-bot
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=20261202153345.165581-1-loreferro01@gmail.com \
    --to=loreferro01@gmail.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-usb@vger.kernel.org \
    /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.