Linux kernel and device drivers for NXP i.MX platforms
 help / color / mirror / Atom feed
* [PATCH v2] usb: chipidea: stop asynchronous work before destroying roles
@ 2026-12-01  9:24 Xu Yang
  2026-12-01  9:36 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Xu Yang @ 2026-12-01  9:24 UTC (permalink / raw)
  To: peter.chen, gregkh
  Cc: alexander.shishkin, linux-usb, linux-kernel, jun.li, imx

From: Xu Yang <xu.yang_2@nxp.com>

Role teardown currently removes the gadget and host roles while OTG
interrupts, FSM processing and work items may still be active. Such
asynchronous processing can queue or execute role-switch operations
against partially destroyed roles. In particular, a gadget instance
may remain registered after the controller has been removed.

Disable power_lost_work before starting role teardown. During OTG
destruction, disable the IRQ and ci->work, clear canceled ID and
B-session-valid events, and remove the OTG FSM before destroying the
workqueue.

Finally, destroy the gadget and host roles only after the OTG machinery
has been stopped. This establishes a teardown order in which event
sources are quiesced before their target resources are released.

Fixes: cbec6bd55a45 ("usb: chipidea: move otg related things to otg file")
Cc: stable@vger.kernel.org
Signed-off-by: Xu Yang <xu.yang_2@nxp.com>

---
Changes in v2:
 - add Cc stable
---
 drivers/usb/chipidea/core.c |  5 +++--
 drivers/usb/chipidea/otg.c  | 17 +++++++++++++----
 2 files changed, 16 insertions(+), 6 deletions(-)

diff --git a/drivers/usb/chipidea/core.c b/drivers/usb/chipidea/core.c
index a024e11da3b2..d0a140a2a7fe 100644
--- a/drivers/usb/chipidea/core.c
+++ b/drivers/usb/chipidea/core.c
@@ -942,10 +942,11 @@ EXPORT_SYMBOL_GPL(ci_hdrc_query_available_role);
 
 static inline void ci_role_destroy(struct ci_hdrc *ci)
 {
-	ci_hdrc_gadget_destroy(ci);
-	ci_hdrc_host_destroy(ci);
+	disable_work_sync(&ci->power_lost_work);
 	if (ci->is_otg && ci->roles[CI_ROLE_GADGET])
 		ci_hdrc_otg_destroy(ci);
+	ci_hdrc_gadget_destroy(ci);
+	ci_hdrc_host_destroy(ci);
 }
 
 static void ci_get_otg_capable(struct ci_hdrc *ci)
diff --git a/drivers/usb/chipidea/otg.c b/drivers/usb/chipidea/otg.c
index 89a6a37815f0..8ac8969a45f0 100644
--- a/drivers/usb/chipidea/otg.c
+++ b/drivers/usb/chipidea/otg.c
@@ -272,12 +272,21 @@ int ci_hdrc_otg_init(struct ci_hdrc *ci)
  */
 void ci_hdrc_otg_destroy(struct ci_hdrc *ci)
 {
-	if (ci->wq)
-		destroy_workqueue(ci->wq);
+	disable_irq(ci->irq);
+	if (disable_work_sync(&ci->work)) {
+		ci->id_event = false;
+		ci->b_sess_valid_event = false;
+		/* Balance disable_irq_nosync() performed in ci_otg_queue_work() */
+		enable_irq(ci->irq);
+	}
+
+	if (ci_otg_is_fsm_mode(ci))
+		ci_hdrc_otg_fsm_remove(ci);
 
 	/* Disable all OTG irq and clear status */
 	hw_write_otgsc(ci, OTGSC_INT_EN_BITS | OTGSC_INT_STATUS_BITS,
 						OTGSC_INT_STATUS_BITS);
-	if (ci_otg_is_fsm_mode(ci))
-		ci_hdrc_otg_fsm_remove(ci);
+	if (ci->wq)
+		destroy_workqueue(ci->wq);
+	enable_irq(ci->irq);
 }
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH v2] usb: chipidea: stop asynchronous work before destroying roles
  2026-12-01  9:24 [PATCH v2] usb: chipidea: stop asynchronous work before destroying roles Xu Yang
@ 2026-12-01  9:36 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-12-01  9:36 UTC (permalink / raw)
  To: Xu Yang; +Cc: linux-usb, imx, Frank.Li

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] usb: chipidea: stale OTG event flags and teardown race in
  ci_hdrc_otg_destroy()
--

commit 2edcac291a5d2ee72c3e18a7f2116a86d7b33779
Author: Xu Yang <xu.yang_2@nxp.com>

usb: chipidea: stop asynchronous work before destroying roles

This commit reorders role teardown to disable power_lost_work and stop OTG
interrupts, FSM processing, and ci->work before destroying the gadget and
host roles.

> diff --git a/drivers/usb/chipidea/otg.c b/drivers/usb/chipidea/otg.c
> index fecc7d7e2f0d7..ec11056dddfaf 100644
> --- a/drivers/usb/chipidea/otg.c
> +++ b/drivers/usb/chipidea/otg.c
> @@ -264,12 +264,21 @@ int ci_hdrc_otg_init(struct ci_hdrc *ci)
>   */
>  void ci_hdrc_otg_destroy(struct ci_hdrc *ci)
>  {
> -	if (ci->wq)
> -		destroy_workqueue(ci->wq);
> +	disable_irq(ci->irq);
> +	if (disable_work_sync(&ci->work)) {
> +		ci->id_event = false;
> +		ci->b_sess_valid_event = false;
> +		/* Balance disable_irq_nosync() performed in ci_otg_queue_work() */
> +		enable_irq(ci->irq);
> +	}
> +
> +	if (ci_otg_is_fsm_mode(ci))
> +		ci_hdrc_otg_fsm_remove(ci);
>  
>  	/* Disable all OTG irq and clear status */
>  	hw_write_otgsc(ci, OTGSC_INT_EN_BITS | OTGSC_INT_STATUS_BITS,
>  						OTGSC_INT_STATUS_BITS);
> -	if (ci_otg_is_fsm_mode(ci))
> -		ci_hdrc_otg_fsm_remove(ci);
> +	if (ci->wq)
> +		destroy_workqueue(ci->wq);
> +	enable_irq(ci->irq);
>  }

[Severity: Medium]
Can ci_hdrc_otg_destroy() leave ci->id_event or ci->b_sess_valid_event set
when re-enabling ci->irq, causing a hardirq storm during role teardown?

ci->id_event and ci->b_sess_valid_event are only cleared when
disable_work_sync() cancels a pending work item, and hw_write_otgsc() does
not clear cable->enabled until after disable_work_sync() returns:

1. In OTG FSM mode, ci_otg_fsm_work() only clears ci->id_event when the
   state machine remains in OTG_STATE_A_IDLE.  If a completed work run
   transitioned to OTG_STATE_A_WAIT_VRISE, ci->id_event is still true when
   disable_work_sync() returns false.
2. disable_irq(ci->irq) does not block extcon notifications via
   ci_cable_notifier() -> ci_irq() -> ci_irq_handler(), which can still
   see cable->enabled == true and set ci->id_event or
   ci->b_sess_valid_event before hw_write_otgsc() runs:

CPU0                              CPU1
ci_hdrc_otg_destroy()
  disable_irq(ci->irq)
  disable_work_sync(&ci->work)
                                  ci_cable_notifier()
                                    ci_irq(ci)
                                      ci_otg_irq(ci)
                                        ci->id_event = true
                                        ci_otg_queue_work(ci)
  hw_write_otgsc(...)
  enable_irq(ci->irq)
ci_hdrc_gadget_destroy()
  <hardirq>
    ci_irq_handler()
      if (ci->id_event || ...)
        ci_otg_queue_work(ci)
        return IRQ_HANDLED;       <-- skips ci_role(ci)->irq(ci), IRQ storm

Because ci->work is now disabled, queue_work() in ci_otg_queue_work()
always returns false and immediately calls enable_irq(ci->irq), while
ci_irq_handler() returns IRQ_HANDLED before calling ci_role(ci)->irq(ci)
to clear the device or host interrupt status.

Calling ci_hdrc_otg_fsm_remove() and hw_write_otgsc() before
disable_work_sync(&ci->work), and clearing ci->id_event and
ci->b_sess_valid_event unconditionally afterwards, would avoid this.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261201092430.2212461-1-xu.yang_2@oss.nxp.com?part=1

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-12-01  9:36 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-01  9:24 [PATCH v2] usb: chipidea: stop asynchronous work before destroying roles Xu Yang
2026-12-01  9:36 ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox