* [PATCH v2 1/4] usb: musb: cancel the delayed works after quiescing the interrupt
[not found] <20261113063409.47485-1-lui2012qwq@gmail.com>
@ 2026-11-13 6:34 ` Chengyu Liu
2026-11-13 6:34 ` [PATCH v2 2/4] usb: musb: dsps: free the VBUS interrupt before the child teardown Chengyu Liu
` (2 subsequent siblings)
3 siblings, 0 replies; 4+ messages in thread
From: Chengyu Liu @ 2026-11-13 6:34 UTC (permalink / raw)
To: linux-usb
Cc: b-liu, gregkh, nkapron, tony, bigeasy, balbi, shimoda.yoshihiro,
robert.jarzmik, Chengyu Liu, stable
musb_remove() cancels finish_resume_work and deassert_reset_work up
front, while the interrupt is still enabled and the root hub is still
registered, and cancels irq_work only later. All three works can be
armed past their cancel:
- musb_handle_intr_resume() schedules finish_resume_work from
interrupt context (RESUME in OTG_STATE_A_SUSPEND, via
musb_stage0_irq());
- musb_hub_control() schedules finish_resume_work (ClearPortFeature
SUSPEND -> musb_port_suspend()) and deassert_reset_work
(SetPortFeature RESET -> musb_port_reset()) from process context,
and the root hub is only torn down by musb_host_cleanup();
- musb_stage0_irq() schedules irq_work, which can also re-arm itself
from the session recheck path (musb_pm_runtime_check_session()).
A work armed after its cancel runs musb_host_finish_resume(),
musb_deassert_reset() or musb_irq_work() after musb_free() has
released the struct.
Tear down the host and gadget sides first so no process-context
producer remains, mask the interrupts and synchronize the IRQ line so
no interrupt handler can be running, and only then cancel all three
works. This also restores the pre-f730f205cc51 ordering, which had
the cancels behind the interrupt masking.
Found by static analysis.
Suggested-by: Neill Kapron <nkapron@google.com>
Fixes: f730f205cc51 ("usb: musb: Split PM runtime between wrapper IP and musb core")
Cc: stable@vger.kernel.org
Signed-off-by: Chengyu Liu <lui2012qwq@gmail.com>
---
v1 -> v2:
- also move the finish_resume_work and deassert_reset_work cancels
behind the interrupt quiesce, per Neill Kapron's review:
finish_resume_work is armed from interrupt context as well
(musb_handle_intr_resume()), and both it and deassert_reset_work
can additionally be re-armed via musb_hub_control() until
musb_host_cleanup() tears the host down.
- correct the Fixes tag to f730f205cc51 (the 2016 PM-runtime split
moved the cancels ahead of the interrupt masking)
v1: https://lore.kernel.org/linux-usb/20261112160448.41566-2-lui2012qwq@gmail.com/
Build-tested on x86_64: musb_core.o compiles clean
(USB_MUSB_HDRC). No runtime reproducer: requires MUSB-based OTG
hardware. Fix written with AI assistance.
drivers/usb/musb/musb_core.c | 15 ++++++++++++---
1 file changed, 12 insertions(+), 3 deletions(-)
diff --git a/drivers/usb/musb/musb_core.c b/drivers/usb/musb/musb_core.c
index 9b63b56edc45..25115217e0af 100644
--- a/drivers/usb/musb/musb_core.c
+++ b/drivers/usb/musb/musb_core.c
@@ -2630,9 +2630,6 @@ static void musb_remove(struct platform_device *pdev)
*/
musb_exit_debugfs(musb);
- cancel_delayed_work_sync(&musb->irq_work);
- cancel_delayed_work_sync(&musb->finish_resume_work);
- cancel_delayed_work_sync(&musb->deassert_reset_work);
pm_runtime_get_sync(musb->controller);
musb_host_cleanup(musb);
musb_gadget_cleanup(musb);
@@ -2642,6 +2639,18 @@ static void musb_remove(struct platform_device *pdev)
musb_disable_interrupts(musb);
musb_writeb(musb->mregs, MUSB_DEVCTL, 0);
spin_unlock_irqrestore(&musb->lock, flags);
+
+ /*
+ * musb_stage0_irq() arms irq_work (and finish_resume_work on
+ * remote wakeup) from interrupt context, so the works can only be
+ * cancelled for good once interrupts are masked and any in-flight
+ * handler has completed.
+ */
+ synchronize_irq(musb->nIrq);
+ cancel_delayed_work_sync(&musb->irq_work);
+ cancel_delayed_work_sync(&musb->finish_resume_work);
+ cancel_delayed_work_sync(&musb->deassert_reset_work);
+
musb_platform_exit(musb);
pm_runtime_dont_use_autosuspend(musb->controller);
--
2.25.1
^ permalink raw reply related [flat|nested] 4+ messages in thread
* [PATCH v2 2/4] usb: musb: dsps: free the VBUS interrupt before the child teardown
[not found] <20261113063409.47485-1-lui2012qwq@gmail.com>
2026-11-13 6:34 ` [PATCH v2 1/4] usb: musb: cancel the delayed works after quiescing the interrupt Chengyu Liu
@ 2026-11-13 6:34 ` Chengyu Liu
2026-11-13 6:34 ` [PATCH v2 3/4] usb: gadget: r8a66597: free the IRQ before deleting the timer Chengyu Liu
2026-11-13 6:34 ` [PATCH v2 4/4] usb: phy: gpio-vbus: free the interrupt before cancelling the work Chengyu Liu
3 siblings, 0 replies; 4+ messages in thread
From: Chengyu Liu @ 2026-11-13 6:34 UTC (permalink / raw)
To: linux-usb
Cc: b-liu, gregkh, nkapron, tony, bigeasy, balbi, shimoda.yoshihiro,
robert.jarzmik, Chengyu Liu, stable
dsps_remove() unregisters the musb child, which deletes
musb->dev_timer and frees the structure, while the glue's VBUS
interrupt is devm-managed and only freed after dsps_remove() returns.
dsps_vbus_threaded_irq() re-arms the timer via dsps_mod_timer(), so
a VBUS event in between re-arms dev_timer past its deletion and
otg_timer() runs on freed memory.
Free the VBUS interrupt first, which also waits for any in-flight
handler, so nothing can re-arm the timer once the child teardown
runs.
Found by static analysis.
Fixes: 369469a92393 ("usb: musb: Add support for optional VBUS irq to dsps glue layer")
Cc: stable@vger.kernel.org
Reviewed-by: Neill Kapron <nkapron@google.com>
Signed-off-by: Chengyu Liu <lui2012qwq@gmail.com>
---
v1 -> v2: no code change; collected Neill Kapron's Reviewed-by.
Build-tested on x86_64: musb_dsps.o compiles clean
(USB_MUSB_DSPS). No runtime reproducer: requires AM33xx/DSPS
hardware. Fix written with AI assistance.
drivers/usb/musb/musb_dsps.c | 11 +++++++++++
1 file changed, 11 insertions(+)
diff --git a/drivers/usb/musb/musb_dsps.c b/drivers/usb/musb/musb_dsps.c
index 91d8712efa41..69c8b4b3ffa7 100644
--- a/drivers/usb/musb/musb_dsps.c
+++ b/drivers/usb/musb/musb_dsps.c
@@ -942,6 +942,17 @@ static void dsps_remove(struct platform_device *pdev)
{
struct dsps_glue *glue = platform_get_drvdata(pdev);
+ /*
+ * The VBUS interrupt handler re-arms musb->dev_timer via
+ * dsps_mod_timer(); the IRQ is devm-managed and would only be
+ * freed after this function returns, while the child teardown
+ * below deletes the timer and frees the musb structure. Free
+ * the interrupt first so no VBUS event can re-arm the timer
+ * past that point.
+ */
+ if (glue->vbus_irq)
+ devm_free_irq(glue->dev, glue->vbus_irq, glue);
+
platform_device_unregister(glue->musb);
pm_runtime_disable(&pdev->dev);
--
2.25.1
^ permalink raw reply related [flat|nested] 4+ messages in thread
* [PATCH v2 3/4] usb: gadget: r8a66597: free the IRQ before deleting the timer
[not found] <20261113063409.47485-1-lui2012qwq@gmail.com>
2026-11-13 6:34 ` [PATCH v2 1/4] usb: musb: cancel the delayed works after quiescing the interrupt Chengyu Liu
2026-11-13 6:34 ` [PATCH v2 2/4] usb: musb: dsps: free the VBUS interrupt before the child teardown Chengyu Liu
@ 2026-11-13 6:34 ` Chengyu Liu
2026-11-13 6:34 ` [PATCH v2 4/4] usb: phy: gpio-vbus: free the interrupt before cancelling the work Chengyu Liu
3 siblings, 0 replies; 4+ messages in thread
From: Chengyu Liu @ 2026-11-13 6:34 UTC (permalink / raw)
To: linux-usb
Cc: b-liu, gregkh, nkapron, tony, bigeasy, balbi, shimoda.yoshihiro,
robert.jarzmik, Chengyu Liu, stable
r8a66597_remove() deletes r8a66597->timer while the interrupt is
still live: the IRQ is devm-managed and only freed at detach, and
r8a66597_irq() re-arms the timer when sampling VBUS (VBINT). An
interrupt landing after timer_delete_sync() re-arms the timer, and
r8a66597_timer() then runs on freed memory.
Store the requested IRQ number at probe time and free it before
deleting the timer; freeing also waits for any running handler, so
nothing can re-arm the timer past the delete.
Found by static analysis.
Fixes: c41442474a26 ("usb: gadget: R8A66597 peripheral controller support.")
Cc: stable@vger.kernel.org
Reviewed-by: Neill Kapron <nkapron@google.com>
Signed-off-by: Chengyu Liu <lui2012qwq@gmail.com>
---
v1 -> v2: no code change; collected Neill Kapron's Reviewed-by.
Build-tested on x86_64: r8a66597-udc.o compiles clean
(USB_R8A66597 + COMPILE_TEST). No runtime reproducer: requires
R8A66597 hardware. Fix written with AI assistance.
drivers/usb/gadget/udc/r8a66597-udc.c | 8 ++++++++
drivers/usb/gadget/udc/r8a66597-udc.h | 1 +
2 files changed, 9 insertions(+)
diff --git a/drivers/usb/gadget/udc/r8a66597-udc.c b/drivers/usb/gadget/udc/r8a66597-udc.c
index 469075b064ba..bfccc6565e0a 100644
--- a/drivers/usb/gadget/udc/r8a66597-udc.c
+++ b/drivers/usb/gadget/udc/r8a66597-udc.c
@@ -1810,6 +1810,13 @@ static void r8a66597_remove(struct platform_device *pdev)
struct r8a66597 *r8a66597 = platform_get_drvdata(pdev);
usb_del_gadget_udc(&r8a66597->gadget);
+
+ /*
+ * The interrupt handler re-arms the timer on VBUS changes; the
+ * IRQ is devm-managed and freed only at detach, so free it
+ * before deleting the timer.
+ */
+ devm_free_irq(&pdev->dev, r8a66597->irq, r8a66597);
timer_delete_sync(&r8a66597->timer);
r8a66597_free_request(&r8a66597->ep[0].ep, r8a66597->ep0_req);
@@ -1896,6 +1903,7 @@ static int r8a66597_probe(struct platform_device *pdev)
udc_name, r8a66597);
if (ret < 0)
goto clean_up2;
+ r8a66597->irq = irq;
INIT_LIST_HEAD(&r8a66597->gadget.ep_list);
r8a66597->gadget.ep0 = &r8a66597->ep[0].ep;
diff --git a/drivers/usb/gadget/udc/r8a66597-udc.h b/drivers/usb/gadget/udc/r8a66597-udc.h
index 9a115caba661..08acabe02421 100644
--- a/drivers/usb/gadget/udc/r8a66597-udc.h
+++ b/drivers/usb/gadget/udc/r8a66597-udc.h
@@ -110,6 +110,7 @@ struct r8a66597 {
unsigned char interrupt;
unsigned char isochronous;
unsigned char num_dma;
+ int irq;
unsigned irq_sense_low:1;
};
--
2.25.1
^ permalink raw reply related [flat|nested] 4+ messages in thread
* [PATCH v2 4/4] usb: phy: gpio-vbus: free the interrupt before cancelling the work
[not found] <20261113063409.47485-1-lui2012qwq@gmail.com>
` (2 preceding siblings ...)
2026-11-13 6:34 ` [PATCH v2 3/4] usb: gadget: r8a66597: free the IRQ before deleting the timer Chengyu Liu
@ 2026-11-13 6:34 ` Chengyu Liu
3 siblings, 0 replies; 4+ messages in thread
From: Chengyu Liu @ 2026-11-13 6:34 UTC (permalink / raw)
To: linux-usb
Cc: b-liu, gregkh, nkapron, tony, bigeasy, balbi, shimoda.yoshihiro,
robert.jarzmik, Chengyu Liu, stable
gpio_vbus_remove() cancels gpio_vbus->work while the VBUS sensing GPIO
interrupt is still live: it is devm-managed and freed only at detach,
and gpio_vbus_irq() schedules the work with a 100 ms debounce delay.
A plug event landing after the cancel re-arms the work, and its timer
then fires on the freed gpio_vbus_data.
Free the interrupt first, which also waits for any running handler,
so nothing can re-arm the work past the cancel.
Found by static analysis and confirmed with a KASAN reproducer:
slab-use-after-free write in __run_timer_base() within one debounce
interval of unregistering the device while its VBUS GPIO is toggled
(object allocated by gpio_vbus_probe() via devm_kmalloc(), freed by
devres_release_all() at unbind).
Fixes: c2344f13b59e ("USB: gpio_vbus: add delayed vbus_session calls")
Cc: stable@vger.kernel.org
Reviewed-by: Neill Kapron <nkapron@google.com>
Signed-off-by: Chengyu Liu <lui2012qwq@gmail.com>
---
v1 -> v2: no code change; collected Neill Kapron's Reviewed-by.
The reproducer instantiates the unmodified driver on a gpio-sim
chip in QEMU and toggles the line while the device is unregistered;
it widens the remove()-to-detach window with a 500 ms sleep, logic
otherwise unmodified. Repeated runs: 5/5 KASAN splats without the
fix, 0/5 with it. The KASAN report and the reproducer kit follow
in this thread. Also build-tested on x86_64 (USB_GPIO_VBUS).
Fix written with AI assistance.
drivers/usb/phy/phy-gpio-vbus-usb.c | 8 ++++++++
1 file changed, 8 insertions(+)
diff --git a/drivers/usb/phy/phy-gpio-vbus-usb.c b/drivers/usb/phy/phy-gpio-vbus-usb.c
index 60b7c1219dd2..82d2301c3804 100644
--- a/drivers/usb/phy/phy-gpio-vbus-usb.c
+++ b/drivers/usb/phy/phy-gpio-vbus-usb.c
@@ -325,6 +325,14 @@ static void gpio_vbus_remove(struct platform_device *pdev)
struct gpio_vbus_data *gpio_vbus = platform_get_drvdata(pdev);
device_init_wakeup(&pdev->dev, 0);
+
+ /*
+ * The VBUS sensing GPIO interrupt is devm-managed and freed only
+ * at detach, and its handler schedules the work with a 100ms
+ * delay; free the interrupt before cancelling the work so a
+ * late plug event cannot re-arm it past the cancel.
+ */
+ devm_free_irq(&pdev->dev, gpio_vbus->irq, pdev);
cancel_delayed_work_sync(&gpio_vbus->work);
usb_remove_phy(&gpio_vbus->phy);
--
2.25.1
^ permalink raw reply related [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-11-13 6:34 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
[not found] <20261113063409.47485-1-lui2012qwq@gmail.com>
2026-11-13 6:34 ` [PATCH v2 1/4] usb: musb: cancel the delayed works after quiescing the interrupt Chengyu Liu
2026-11-13 6:34 ` [PATCH v2 2/4] usb: musb: dsps: free the VBUS interrupt before the child teardown Chengyu Liu
2026-11-13 6:34 ` [PATCH v2 3/4] usb: gadget: r8a66597: free the IRQ before deleting the timer Chengyu Liu
2026-11-13 6:34 ` [PATCH v2 4/4] usb: phy: gpio-vbus: free the interrupt before cancelling the work Chengyu Liu
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox