mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] usb: chipidea: stop asynchronous work before destroying roles
@ 2026-11-12  9:25 Xu Yang
  2026-11-26  5:51 ` Peter Chen (Qualcomm)
  0 siblings, 1 reply; 3+ messages in thread
From: Xu Yang @ 2026-11-12  9:25 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")
Signed-off-by: Xu Yang <xu.yang_2@nxp.com>
---
 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 3461dd763a6e..eb7b9c0746d8 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	[flat|nested] 3+ messages in thread

* Re: [PATCH] usb: chipidea: stop asynchronous work before destroying roles
  2026-11-12  9:25 [PATCH] usb: chipidea: stop asynchronous work before destroying roles Xu Yang
@ 2026-11-26  5:51 ` Peter Chen (Qualcomm)
  2026-11-26  8:26   ` Xu Yang
  0 siblings, 1 reply; 3+ messages in thread
From: Peter Chen (Qualcomm) @ 2026-11-26  5:51 UTC (permalink / raw)
  To: Xu Yang; +Cc: gregkh, alexander.shishkin, linux-usb, linux-kernel, jun.li, imx

On 26-11-12 17:25:56, Xu Yang wrote:
> 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")
> Signed-off-by: Xu Yang <xu.yang_2@nxp.com>

Do you need to Cc stable tree?

Peter

> ---
>  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 3461dd763a6e..eb7b9c0746d8 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
> 

-- 

Thanks,
Peter Chen

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

* Re: [PATCH] usb: chipidea: stop asynchronous work before destroying roles
  2026-11-26  5:51 ` Peter Chen (Qualcomm)
@ 2026-11-26  8:26   ` Xu Yang
  0 siblings, 0 replies; 3+ messages in thread
From: Xu Yang @ 2026-11-26  8:26 UTC (permalink / raw)
  To: Peter Chen (Qualcomm)
  Cc: gregkh, alexander.shishkin, linux-usb, linux-kernel, jun.li, imx

Hi Peter,

On Wed, Nov 25, 2026 at 09:51:25PM -0800, Peter Chen (Qualcomm) wrote:
> On 26-11-12 17:25:56, Xu Yang wrote:
> > 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")
> > Signed-off-by: Xu Yang <xu.yang_2@nxp.com>
> 
> Do you need to Cc stable tree?

I worry the fix tag is a bit old, but this patch use a relative new disable_work_sync() API.
However, it seems still valid to cc stable in this case after I read the kernel doc.

Will add it in v2.

Thanks,
Xu Yang

> 
> Peter
> 
> > ---
> >  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 3461dd763a6e..eb7b9c0746d8 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
> > 
> 
> -- 
> 
> Thanks,
> Peter Chen

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

end of thread, other threads:[~2026-11-26  8:29 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-12  9:25 [PATCH] usb: chipidea: stop asynchronous work before destroying roles Xu Yang
2026-11-26  5:51 ` Peter Chen (Qualcomm)
2026-11-26  8:26   ` Xu Yang

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

all inboxes | Powered by JetHome®