Linux USB
 help / color / mirror / Atom feed
* [PATCH] usb: renesas_usbhs: Stop an active host on removal
@ 2026-11-29 16:31 Guangshuo Li
  2026-11-29 16:48 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Guangshuo Li @ 2026-11-29 16:31 UTC (permalink / raw)
  To: gregkh, kuninori.morimoto.gx, balbi; +Cc: linux-usb

The platform remove path drops the host controller reference without
deregistering an active root hub. Disable hotplug work before teardown
and stop the current host while its registers and power are still
available.

A failed usb_add_hcd() leaves no root hub. Skip host teardown in that
case, as on a host that was never started.

This issue was found by our static analysis tool under development.

Fixes: 034d7c13a79c ("usb: gadget: renesas_usbhs: add mod_host support")
Signed-off-by: Guangshuo Li <liguangshuo@mail.sdu.edu.cn>
---
 drivers/usb/renesas_usbhs/common.c   | 9 ++++++++-
 drivers/usb/renesas_usbhs/mod_host.c | 3 +++
 2 files changed, 11 insertions(+), 1 deletion(-)

diff --git a/drivers/usb/renesas_usbhs/common.c b/drivers/usb/renesas_usbhs/common.c
index 22a5175dcceb..6e3c6891b6b4 100644
--- a/drivers/usb/renesas_usbhs/common.c
+++ b/drivers/usb/renesas_usbhs/common.c
@@ -811,7 +811,7 @@ static void usbhs_remove(struct platform_device *pdev)
 
 	dev_dbg(&pdev->dev, "usb remove\n");
 
-	flush_delayed_work(&priv->notify_hotplug_work);
+	disable_delayed_work_sync(&priv->notify_hotplug_work);
 
 	/*
 	 * Explicitly free the IRQ to ensure the interrupt handler is
@@ -821,6 +821,13 @@ static void usbhs_remove(struct platform_device *pdev)
 	 */
 	devm_free_irq(&pdev->dev, priv->irq, priv);
 
+	if (usbhs_mod_is_host(priv) > 0) {
+		usbhs_mod_call(priv, stop, priv);
+		usbhs_mod_change(priv, -1);
+		if (usbhs_get_dparam(priv, runtime_pwctrl))
+			usbhsc_power_ctrl(priv, 0);
+	}
+
 	usbhs_mod_remove(priv);
 	usbhs_fifo_remove(priv);
 	usbhs_pipe_remove(priv);
diff --git a/drivers/usb/renesas_usbhs/mod_host.c b/drivers/usb/renesas_usbhs/mod_host.c
index f7ef3a9f82a4..8caa17282061 100644
--- a/drivers/usb/renesas_usbhs/mod_host.c
+++ b/drivers/usb/renesas_usbhs/mod_host.c
@@ -1496,6 +1496,9 @@ static int usbhsh_stop(struct usbhs_priv *priv)
 	struct usbhs_mod *mod = usbhs_mod_get_current(priv);
 	struct device *dev = usbhs_priv_to_dev(priv);
 
+	if (!hcd->self.root_hub)
+		return 0;
+
 	/*
 	 * disable irq callback
 	 */

base-commit: 90d6263e867be7089b3d3323bef8d2accd327c77
-- 
2.43.0


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

* Re: [PATCH] usb: renesas_usbhs: Stop an active host on removal
  2026-11-29 16:31 [PATCH] usb: renesas_usbhs: Stop an active host on removal Guangshuo Li
@ 2026-11-29 16:48 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-29 16:48 UTC (permalink / raw)
  To: Guangshuo Li; +Cc: linux-usb

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] usb: renesas_usbhs: wrong power-off order and leak in
  usbhs_remove()
- [Medium] usb: renesas_usbhs: deadlock on setup_ack_done in
  usbhs_remove()
It also noticed 1 related pre-existing issue this patch does not
introduce:
- [High] usb: renesas_usbhs: unchecked pm_runtime_get_sync() in
  usbhsc_power_ctrl() [1]
--

commit 25a84d9054639b6536f6c92a19f9076160ea996e
Author: Guangshuo Li <liguangshuo@mail.sdu.edu.cn>

usb: renesas_usbhs: Stop an active host on removal

This commit disables hotplug work and stops an active host controller in
usbhs_remove() while skipping host teardown in usbhsh_stop() if the root
hub was never registered.

> diff --git a/drivers/usb/renesas_usbhs/common.c b/drivers/usb/renesas_usbhs/common.c
> index 22a5175dcceb9..6e3c6891b6b4b 100644
> --- a/drivers/usb/renesas_usbhs/common.c
> +++ b/drivers/usb/renesas_usbhs/common.c
> @@ -811,7 +811,7 @@ static void usbhs_remove(struct platform_device *pdev)
>  
>  	dev_dbg(&pdev->dev, "usb remove\n");
>  
> -	flush_delayed_work(&priv->notify_hotplug_work);
> +	disable_delayed_work_sync(&priv->notify_hotplug_work);
>  
>  	/*
>  	 * Explicitly free the IRQ to ensure the interrupt handler is
> @@ -821,6 +821,13 @@ static void usbhs_remove(struct platform_device *pdev)
>  	 */
>  	devm_free_irq(&pdev->dev, priv->irq, priv);
>  
> +	if (usbhs_mod_is_host(priv) > 0) {
> +		usbhs_mod_call(priv, stop, priv);

[Severity: Medium]
Can freeing priv->irq before stopping the host hang usb_remove_hcd() when
a control transfer is in the setup stage?

usbhsh_urb_enqueue() links the control URB onto ep->urb_list and waits
uninterruptibly on hpriv->setup_ack_done in
usbhsh_setup_stage_packet_push() before setting urb->hcpriv.  Because
setup_ack_done is only completed from usbhs_interrupt() via
usbhsh_irq_setup_ack() or usbhsh_irq_setup_err(), freeing the IRQ (and
clearing mod->irq_sack and mod->irq_sign in usbhsh_stop()) before
usb_remove_hcd() prevents the setup stage from completing, while
usbhsh_urb_dequeue() cannot cancel the URB because urb->hcpriv is still
NULL.

> +		usbhs_mod_change(priv, -1);
> +		if (usbhs_get_dparam(priv, runtime_pwctrl))
> +			usbhsc_power_ctrl(priv, 0);
> +	}
> +
>  	usbhs_mod_remove(priv);

[Severity: High]
When runtime_pwctrl is enabled, does calling usbhsc_power_ctrl(priv, 0)
before usbhs_mod_remove(priv) access SYSCFG after clocks are disabled,
and leak power and clocks when removing in gadget mode?

If a gadget driver is bound to the UDC while in host mode,
usbhs_mod_remove() -> usb_del_gadget_udc() calls usbhsg_pullup() ->
usbhs_sys_function_pullup(), which reads and writes SYSCFG after
usbhsc_power_ctrl(priv, 0) has already disabled the clocks and suspended
the device.  Conversely, if the controller is active in gadget mode when
removed under runtime_pwctrl, usbhs_mod_is_host(priv) is false and the
later !usbhs_get_dparam(priv, runtime_pwctrl) check is also false, so
usbhsc_power_ctrl(priv, 0) is never called.

Should this follow usbhsc_suspend() by stopping any active module and
calling usbhsc_power_ctrl(priv, 0) after usbhs_mod_remove(priv) whenever
a module was active or runtime_pwctrl is disabled?

[Severity: High]
This problem wasn't introduced by this patch, but usbhsc_power_ctrl()
does not check the return value of pm_runtime_get_sync() [1]:

drivers/usb/renesas_usbhs/common.c:usbhsc_power_ctrl() {
	if (enable) {
		/* enable PM */
		pm_runtime_get_sync(dev);

		/* enable clks */
		if (usbhsc_clk_prepare_enable(priv))
			return;

		/* enable platform power */
		usbhs_platform_call(priv, power_ctrl, pdev, priv->base, enable);

		/* USB on */
		usbhs_sys_clock_ctrl(priv, enable);
}

If runtime resume fails, usbhsc_power_ctrl() still accesses hardware
registers while the device is unpowered and leaves the runtime PM usage
counter elevated.

[1] https://sashiko.dev/#/bug/linux-7b7fec09-1915-42cd-b05f-29a47b39550d

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261130-7242f82796-u58-0@mail.sdu.edu.cn?part=1

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

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

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-29 16:31 [PATCH] usb: renesas_usbhs: Stop an active host on removal Guangshuo Li
2026-11-29 16:48 ` sashiko-bot

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