* [PATCH] usb: musb: mediatek: Release the usb_phy before unregistering it
@ 2026-12-04 9:29 Lorenzo Ferrari
2026-12-04 9:43 ` sashiko-bot
0 siblings, 1 reply; 2+ messages in thread
From: Lorenzo Ferrari @ 2026-12-04 9:29 UTC (permalink / raw)
To: Bin Liu, Greg Kroah-Hartman
Cc: linux-usb, Rajan Yadav, Lorenzo Ferrari, stable
mtk_musb_probe() registers a usb_phy_generic platform device and then
takes a reference to it with devm_usb_get_phy(). mtk_musb_remove() and
the probe error paths unregister that device by hand, which unbinds the
phy-generic driver and frees the struct usb_phy, but the devm reference
is only dropped later, when devres runs. devm_usb_phy_release() then
calls usb_put_phy() on freed memory, which dereferences
x->dev->driver->owner with a NULL driver. This happens whenever musb-mtk
is unbound or removed, and when its probe fails after
devm_usb_get_phy().
Let devres do both steps, in the right order. Register the unregistration
of the usb_phy_generic device as a devm action right after creating it,
before devm_usb_get_phy(). devres releases resources in reverse order, so
the reference is dropped first and the device is unregistered afterwards.
This is the same fix as the one posted for the sunxi glue, where Rajan
Yadav also pointed out that mediatek.c has the same problem.
Fixes: 0990366bab3c ("usb: musb: Add support for MediaTek musb controller")
Cc: stable@vger.kernel.org
Reported-by: Rajan Yadav <rajanyadav0307@gmail.com>
Link: https://lore.kernel.org/all/20261110-musb-sunxi-unbind-v1-0-d821584fc4a4@gmail.com/
Assisted-by: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Lorenzo Ferrari <loreferro01@gmail.com>
---
Found by code inspection, after the same issue in sunxi.c. Not tested
on MediaTek hardware.
Tested on 4405c18c95d2 in QEMU (x86_64, KASAN) with the real phy-generic
driver and a test module laid out like mtk_musb_probe()/remove(): the
same steps and error labels, and a child device that uses the usb_phy
like musb-hdrc does. Without this patch, unbinding the glue, or failing
its probe at the clk_bulk_prepare_enable() or musb-hdrc registration
step, gives a KASAN slab-use-after-free in devm_usb_phy_release(). With
it, KASAN reports nothing, the devres log (devres.log=1) shows
devm_usb_phy_release() running before the device is unregistered, no
usb_phy_generic device is left behind, and the child can be unbound and
bound again while the glue stays bound. The same tests, plus ten
unbind/bind cycles, also pass with lockdep, DEBUG_ATOMIC_SLEEP,
DETECT_HUNG_TASK and DEBUG_OBJECTS enabled.
Compile-tested with W=1 for ARM (multi_v7_defconfig, clang) and x86_64
(COMPILE_TEST); the clang static analyzer reports nothing in mediatek.c
before or after this patch.
drivers/usb/musb/mediatek.c | 20 +++++++++++++++-----
1 file changed, 15 insertions(+), 5 deletions(-)
diff --git a/drivers/usb/musb/mediatek.c b/drivers/usb/musb/mediatek.c
index 45eea9aaa8..a3821505dd 100644
--- a/drivers/usb/musb/mediatek.c
+++ b/drivers/usb/musb/mediatek.c
@@ -397,6 +397,11 @@ static const struct platform_device_info mtk_dev_info = {
.dma_mask = DMA_BIT_MASK(32),
};
+static void mtk_musb_unregister_usb_phy(void *usb_phy)
+{
+ usb_phy_generic_unregister(usb_phy);
+}
+
static int mtk_musb_probe(struct platform_device *pdev)
{
struct musb_hdrc_platform_data *pdata;
@@ -461,11 +466,20 @@ static int mtk_musb_probe(struct platform_device *pdev)
return dev_err_probe(dev, PTR_ERR(glue->usb_phy),
"fail to registering usb-phy\n");
+ /*
+ * Registered before devm_usb_get_phy(), so that devres drops the
+ * reference to the PHY before the PHY device is unregistered.
+ */
+ ret = devm_add_action_or_reset(dev, mtk_musb_unregister_usb_phy,
+ glue->usb_phy);
+ if (ret)
+ return ret;
+
glue->xceiv = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
if (IS_ERR(glue->xceiv)) {
ret = PTR_ERR(glue->xceiv);
dev_err(dev, "fail to getting usb-phy %d\n", ret);
- goto err_unregister_usb_phy;
+ return ret;
}
platform_set_drvdata(pdev, glue);
@@ -499,18 +513,14 @@ static int mtk_musb_probe(struct platform_device *pdev)
err_enable_clk:
pm_runtime_put_sync(dev);
pm_runtime_disable(dev);
-err_unregister_usb_phy:
- usb_phy_generic_unregister(glue->usb_phy);
return ret;
}
static void mtk_musb_remove(struct platform_device *pdev)
{
struct mtk_glue *glue = platform_get_drvdata(pdev);
- struct platform_device *usb_phy = glue->usb_phy;
platform_device_unregister(glue->musb_pdev);
- usb_phy_generic_unregister(usb_phy);
}
#ifdef CONFIG_OF
base-commit: 4405c18c95d2d0f99f6c581ef0ee8591a1aa6ccd
--
2.56.1
^ permalink raw reply related [flat|nested] 2+ messages in thread* Re: [PATCH] usb: musb: mediatek: Release the usb_phy before unregistering it
2026-12-04 9:29 [PATCH] usb: musb: mediatek: Release the usb_phy before unregistering it Lorenzo Ferrari
@ 2026-12-04 9:43 ` sashiko-bot
0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-12-04 9:43 UTC (permalink / raw)
To: Lorenzo Ferrari; +Cc: linux-usb
> mtk_musb_probe() registers a usb_phy_generic platform device and then
> takes a reference to it with devm_usb_get_phy(). mtk_musb_remove() and
> the probe error paths unregister that device by hand, which unbinds the
> phy-generic driver and frees the struct usb_phy, but the devm reference
> is only dropped later, when devres runs. devm_usb_phy_release() then
> calls usb_put_phy() on freed memory, which dereferences
> x->dev->driver->owner with a NULL driver. This happens whenever musb-mtk
> is unbound or removed, and when its probe fails after
> devm_usb_get_phy().
>
> Let devres do both steps, in the right order. Register the unregistration
> of the usb_phy_generic device as a devm action right after creating it,
> before devm_usb_get_phy(). devres releases resources in reverse order, so
> the reference is dropped first and the device is unregistered afterwards.
>
> This is the same fix as the one posted for the sunxi glue, where Rajan
> Yadav also pointed out that mediatek.c has the same problem.
>
> Fixes: 0990366bab3c ("usb: musb: Add support for MediaTek musb controller")
> Cc: stable@vger.kernel.org
> Reported-by: Rajan Yadav <rajanyadav0307@gmail.com>
> Link: https://lore.kernel.org/all/20261110-musb-sunxi-unbind-v1-0-d821584fc4a4@gmail.com/
> Assisted-by: Claude Opus 5.5 <noreply@anthropic.com>
> Signed-off-by: Lorenzo Ferrari <loreferro01@gmail.com>
Sashiko has reviewed this patch and found no issues.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261204092947.272427-1-loreferro01@gmail.com?part=1
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-12-04 9:43 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-04 9:29 [PATCH] usb: musb: mediatek: Release the usb_phy before unregistering it Lorenzo Ferrari
2026-12-04 9:43 ` sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox