usb: chipidea: stop asynchronous work before destroying roles
PATCH v2 1/1 usb: chipidea: stop asynchronous work before destroying roles
From: Xu Yang <[email protected]>
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.
---
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(-)
drivers/usb/chipidea/core.c +3 -2
@@ -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)
drivers/usb/chipidea/otg.c +13 -4
@@ -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); }
Signature · 1 line
2.34.1
[email protected]
AI reviewThank 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() --
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 <[email protected]>
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.
Quoted 20 lines
drivers/usb/chipidea/otg.c +10
-2@@ -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);
drivers/usb/chipidea/otg.c int ci_hdrc_otg_init(struct ci_hdrc *ci) · line 284
Quoted 10 lines
drivers/usb/chipidea/otg.c +3
-2/* 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 CPU1ci_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 stormBecause 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.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1