drivers/usb/cdns3/cdns3-gadget.c | 4 ++++ 1 file changed, 4 insertions(+)
cdns3_gadget_start() arms two works on system_freezable_wq:
pending_status_wq for the deferred ep0 status stage and aligned_buf_wq
for realigned request buffers. Both handlers use the cdns3_device the
works are embedded in, and cdns3_pending_setup_status_handler() also
calls the ep0 request completion.
cdns3_gadget_exit() does not wait for these works. It frees all
endpoints and aligned buffers and drops the last reference to the
gadget device, which frees priv_dev, so a work queued before the exit
can run after the free.
Fix this by waiting for both works after the gadget driver is unbound
and the IRQ is freed, when no new work can be queued, and before the
endpoints and buffers are released.
This issue was found by an in-house static analysis tool.
Fixes: 7733f6c32e36 ("usb: cdns3: Add Cadence USB3 DRD Driver")
Cc: stable@vger.kernel.org
Reported-by: Sicong Huang <congei42@163.com>
Closes: https://lore.kernel.org/linux-usb/7f5719b.8700.18f67b324d3.Coremail.congei42@163.com/
Suggested-by: Sicong Huang <congei42@163.com>
Assisted-by: Codex:gpt-5.6
Co-developed-by: Song Li <songl@zju.edu.cn>
Signed-off-by: Song Li <songl@zju.edu.cn>
Signed-off-by: Fan Wu <fanwu01@zju.edu.cn>
---
drivers/usb/cdns3/cdns3-gadget.c | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/drivers/usb/cdns3/cdns3-gadget.c b/drivers/usb/cdns3/cdns3-gadget.c
index 42311c1bf..0d272e9c5 100644
--- a/drivers/usb/cdns3/cdns3-gadget.c
+++ b/drivers/usb/cdns3/cdns3-gadget.c
@@ -3269,6 +3269,10 @@ static void cdns3_gadget_exit(struct cdns *cdns)
usb_del_gadget(&priv_dev->gadget);
devm_free_irq(cdns->dev, cdns->dev_irq, priv_dev);
+ /* The works can still be queued until the IRQ is freed. */
+ cancel_work_sync(&priv_dev->pending_status_wq);
+ cancel_work_sync(&priv_dev->aligned_buf_wq);
+
cdns3_free_all_eps(priv_dev);
while (!list_empty(&priv_dev->aligned_buf_list)) {
On 26-09-09 09:56:55, Fan Wu wrote:
> cdns3_gadget_start() arms two works on system_freezable_wq:
> pending_status_wq for the deferred ep0 status stage and aligned_buf_wq
> for realigned request buffers. Both handlers use the cdns3_device the
> works are embedded in, and cdns3_pending_setup_status_handler() also
> calls the ep0 request completion.
>
> cdns3_gadget_exit() does not wait for these works. It frees all
> endpoints and aligned buffers and drops the last reference to the
> gadget device, which frees priv_dev, so a work queued before the exit
> can run after the free.
>
> Fix this by waiting for both works after the gadget driver is unbound
> and the IRQ is freed, when no new work can be queued, and before the
> endpoints and buffers are released.
>
> This issue was found by an in-house static analysis tool.
>
> Fixes: 7733f6c32e36 ("usb: cdns3: Add Cadence USB3 DRD Driver")
> Cc: stable@vger.kernel.org
> Reported-by: Sicong Huang <congei42@163.com>
> Closes: https://lore.kernel.org/linux-usb/7f5719b.8700.18f67b324d3.Coremail.congei42@163.com/
> Suggested-by: Sicong Huang <congei42@163.com>
> Assisted-by: Codex:gpt-5.6
> Co-developed-by: Song Li <songl@zju.edu.cn>
> Signed-off-by: Song Li <songl@zju.edu.cn>
> Signed-off-by: Fan Wu <fanwu01@zju.edu.cn>
Acked-by: Peter Chen <peter.chen@kernel.org>
Peter
> ---
> drivers/usb/cdns3/cdns3-gadget.c | 4 ++++
> 1 file changed, 4 insertions(+)
>
> diff --git a/drivers/usb/cdns3/cdns3-gadget.c b/drivers/usb/cdns3/cdns3-gadget.c
> index 42311c1bf..0d272e9c5 100644
> --- a/drivers/usb/cdns3/cdns3-gadget.c
> +++ b/drivers/usb/cdns3/cdns3-gadget.c
> @@ -3269,6 +3269,10 @@ static void cdns3_gadget_exit(struct cdns *cdns)
> usb_del_gadget(&priv_dev->gadget);
> devm_free_irq(cdns->dev, cdns->dev_irq, priv_dev);
>
> + /* The works can still be queued until the IRQ is freed. */
> + cancel_work_sync(&priv_dev->pending_status_wq);
> + cancel_work_sync(&priv_dev->aligned_buf_wq);
> +
> cdns3_free_all_eps(priv_dev);
>
> while (!list_empty(&priv_dev->aligned_buf_list)) {
>
--
Thanks,
Peter Chen
On 26-09-09 09:56:55, Fan Wu wrote:
> cdns3_gadget_start() arms two works on system_freezable_wq:
> pending_status_wq for the deferred ep0 status stage and aligned_buf_wq
> for realigned request buffers. Both handlers use the cdns3_device the
> works are embedded in, and cdns3_pending_setup_status_handler() also
> calls the ep0 request completion.
>
> cdns3_gadget_exit() does not wait for these works. It frees all
> endpoints and aligned buffers and drops the last reference to the
> gadget device, which frees priv_dev, so a work queued before the exit
> can run after the free.
>
> Fix this by waiting for both works after the gadget driver is unbound
> and the IRQ is freed, when no new work can be queued, and before the
> endpoints and buffers are released.
>
> This issue was found by an in-house static analysis tool.
>
> Fixes: 7733f6c32e36 ("usb: cdns3: Add Cadence USB3 DRD Driver")
> Cc: stable@vger.kernel.org
> Reported-by: Sicong Huang <congei42@163.com>
> Closes: https://lore.kernel.org/linux-usb/7f5719b.8700.18f67b324d3.Coremail.congei42@163.com/
> Suggested-by: Sicong Huang <congei42@163.com>
> Assisted-by: Codex:gpt-5.6
> Co-developed-by: Song Li <songl@zju.edu.cn>
> Signed-off-by: Song Li <songl@zju.edu.cn>
> Signed-off-by: Fan Wu <fanwu01@zju.edu.cn>
> ---
> drivers/usb/cdns3/cdns3-gadget.c | 4 ++++
> 1 file changed, 4 insertions(+)
>
> diff --git a/drivers/usb/cdns3/cdns3-gadget.c b/drivers/usb/cdns3/cdns3-gadget.c
> index 42311c1bf..0d272e9c5 100644
> --- a/drivers/usb/cdns3/cdns3-gadget.c
> +++ b/drivers/usb/cdns3/cdns3-gadget.c
> @@ -3269,6 +3269,10 @@ static void cdns3_gadget_exit(struct cdns *cdns)
> usb_del_gadget(&priv_dev->gadget);
> devm_free_irq(cdns->dev, cdns->dev_irq, priv_dev);
>
> + /* The works can still be queued until the IRQ is freed. */
> + cancel_work_sync(&priv_dev->pending_status_wq);
> + cancel_work_sync(&priv_dev->aligned_buf_wq);
> +
We may cancel work before going to de-init. How about move them before
pm_runtime_put_autosuspend(cdns->dev);
--
Thanks,
Peter Chen
> On Sep 10, 2026, at 10:28, Peter Chen <peter.chen@kernel.org> wrote: > > We may cancel work before going to de-init. How about move them before > > pm_runtime_put_autosuspend(cdns->dev); > > Hi Peter, Thanks for the review. I think the work items need to be cancelled only after the gadget has been quiesced. Moving the cancellations before pm_runtime_put_autosuspend() would run them while the gadget driver is still bound and requests may still be queued. In particular, an EP0 status request can queue pending_status_wq from cdns3_gadget_ep0_queue(), and an endpoint request can queue aligned_buf_wq from cdns3_prepare_aligned_request_buf(). Thus either work can be re-queued after an early cancellation and before usb_del_gadget() unbinds the gadget driver. usb_del_gadget() performs the gadget unbind and synchronizes the gadget IRQ; devm_free_irq() then removes the driver's IRQ handler. Keeping the cancellations after these teardown steps ensures that both works are drained before cdns3_free_all_eps() and the aligned buffers are freed. Could we keep the current placement? Thanks, Fan
On 26-09-10 13:50:46, Fan Wu wrote: > > On Sep 10, 2026, at 10:28, Peter Chen <peter.chen@kernel.org> wrote: > > > > We may cancel work before going to de-init. How about move them before > > > > pm_runtime_put_autosuspend(cdns->dev); > > > > > > Hi Peter, > > Thanks for the review. > > I think the work items need to be cancelled only after the gadget has > been quiesced. Moving the cancellations before > pm_runtime_put_autosuspend() would run them while the gadget driver is > still bound and requests may still be queued. > > In particular, an EP0 status request can queue pending_status_wq from > cdns3_gadget_ep0_queue(), and an endpoint request can queue > aligned_buf_wq from cdns3_prepare_aligned_request_buf(). Thus either > work can be re-queued after an early cancellation and before > usb_del_gadget() unbinds the gadget driver. > > usb_del_gadget() performs the gadget unbind and synchronizes the gadget > IRQ; devm_free_irq() then removes the driver's IRQ handler. Keeping the > cancellations after these teardown steps ensures that both works are > drained before cdns3_free_all_eps() and the aligned buffers are freed. > > Could we keep the current placement? Sure, your comment is reasonable. -- Thanks, Peter Chen
© 2016 - 2026 Red Hat, Inc.