[2/2] usb: musb: sunxi: Unregister the extcon notifier in sunxi_musb_exit()

Message ID 20261110-musb-sunxi-unbind-v1-2-d821584fc4a4@gmail.com (mailing list archive)
State New
Headers
Series usb: musb: sunxi: Fix unbinding and rebinding |

Commit Message

Rajan Yadav via B4 Relay Nov. 10, 2026, 7:08 a.m. UTC
From: Rajan Yadav <rajanyadav0307@gmail.com>

Binding the musb-hdrc child again after it was unbound fails on v7.4-rc1
(KickPi K5C, Allwinner A133):

  # echo musb-hdrc.1.auto > /sys/bus/platform/drivers/musb-hdrc/unbind
  # echo musb-hdrc.1.auto > /sys/bus/platform/drivers/musb-hdrc/bind
  notifier callback sunxi_musb_host_notifier already registered
  WARNING: kernel/notifier.c:24 at notifier_chain_register+0x98/0xf8, CPU#1: sh/312
  ...
   notifier_chain_register+0x98/0xf8 (P)
   raw_notifier_chain_register+0x14/0x20
   extcon_register_notifier+0xa4/0xe0
   devm_extcon_register_notifier+0x64/0xe0
   sunxi_musb_init+0x170/0x280
   musb_probe+0x330/0x125c
  ...
  musb-hdrc musb-hdrc.1.auto: error -EEXIST: musb_init_controller failed
  musb-hdrc musb-hdrc.1.auto: probe with driver musb-hdrc failed with error -17

sunxi_musb_init() runs each time the child is probed, but it registers
the extcon notifier with devm_extcon_register_notifier() on the glue
device. The notifier stays registered after the child is unbound, and
the next sunxi_musb_init() registers the same notifier_block again. It
also stays registered while no musb exists, so an extcon event can
still queue glue->work after sunxi_musb_exit() cancelled it.

Commit bb1d1ce8c74e ("usb: musb: sunxi: Uses the resource-managed
extcon API when registering extcon notifier") introduced this; before
it, the notifier was unregistered in sunxi_musb_exit() and on the
sunxi_musb_init() error path. Restore that, and unregister it before
cancel_work_sync() so that the notifier cannot queue the work again.

Tested on the K5C with v7.4-rc1 and KASAN. The bind failed as above
both without and with the previous patch. With both patches, three
unbind/bind cycles of musb-hdrc bring back the UDC each time, without
warnings or KASAN reports, and unbinding the glue device afterwards is
clean as well. This patch was not tested without the previous one, and
the sunxi_musb_init() error path was not exercised.

Claude Code (Anthropic) found the cause, wrote this fix and the commit
message, and helped with the tests; I reviewed the fix and ran the
tests on the board.

Fixes: bb1d1ce8c74e ("usb: musb: sunxi: Uses the resource-managed extcon API when registering extcon notifier")
Cc: stable@vger.kernel.org
Signed-off-by: Rajan Yadav <rajanyadav0307@gmail.com>
---
 drivers/usb/musb/sunxi.c | 18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)
  

Comments

sashiko-bot@kernel.org Nov. 10, 2026, 7:26 a.m. UTC | #1
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] usb: musb: sunxi: missing cancel_work_sync() in
  sunxi_musb_init()
--

commit b343ab879ad454784f848e3901439d97d89167a9
Author: Rajan Yadav <rajanyadav0307@gmail.com>

usb: musb: sunxi: Unregister the extcon notifier in sunxi_musb_exit()

This commit switches sunxi_musb_init() from devm_extcon_register_notifier()
to extcon_register_notifier() and unregisters the notifier in
sunxi_musb_exit() and on the sunxi_musb_init() error path.

> diff --git a/drivers/usb/musb/sunxi.c b/drivers/usb/musb/sunxi.c
> index d880c3758c91b..e9107ae9d3417 100644
> --- a/drivers/usb/musb/sunxi.c
> +++ b/drivers/usb/musb/sunxi.c
> @@ -246,15 +246,19 @@ static int sunxi_musb_init(struct musb *musb)
>  
>  	writeb(SUNXI_MUSB_VEND0_PIO_MODE, musb->mregs + SUNXI_MUSB_VEND0);
>  
> -	/* Register notifier before calling phy_init() */
> -	ret = devm_extcon_register_notifier(glue->dev, glue->extcon,
> -					EXTCON_USB_HOST, &glue->host_nb);
> +	/*
> +	 * Register notifier before calling phy_init(). It is unregistered in
> +	 * sunxi_musb_exit(), as this runs again each time the musb-hdrc child
> +	 * is bound.
> +	 */
> +	ret = extcon_register_notifier(glue->extcon, EXTCON_USB_HOST,
> +				       &glue->host_nb);
>  	if (ret)
>  		goto error_reset_assert;
>  
>  	ret = phy_init(glue->phy);
>  	if (ret)
> -		goto error_reset_assert;
> +		goto error_unregister_notifier;
>  
>  	musb->isr = sunxi_musb_interrupt;
>  
> @@ -263,6 +267,9 @@ static int sunxi_musb_init(struct musb *musb)
>  
>  	return 0;
>  
> +error_unregister_notifier:
> +	extcon_unregister_notifier(glue->extcon, EXTCON_USB_HOST,
> +				   &glue->host_nb);
>  error_reset_assert:
>  	if (test_bit(SUNXI_MUSB_FL_HAS_RESET, &glue->flags))
>  		reset_control_assert(glue->rst);

[Severity: Medium]
Should the error_unregister_notifier path also call
cancel_work_sync(&glue->work) after extcon_unregister_notifier(), just like
sunxi_musb_exit() does?

An EXTCON_USB_HOST event arriving between extcon_register_notifier() and a
failing phy_init() schedules glue->work via sunxi_musb_host_notifier().
Because musb-hdrc fails to bind, unbinding the glue device in
sunxi_musb_remove() will not run sunxi_musb_exit() and devres can free
glue while glue->work is still queued or running.
  

Patch

diff --git a/drivers/usb/musb/sunxi.c b/drivers/usb/musb/sunxi.c
index d880c3758c9..e9107ae9d34 100644
--- a/drivers/usb/musb/sunxi.c
+++ b/drivers/usb/musb/sunxi.c
@@ -246,15 +246,19 @@  static int sunxi_musb_init(struct musb *musb)
 
 	writeb(SUNXI_MUSB_VEND0_PIO_MODE, musb->mregs + SUNXI_MUSB_VEND0);
 
-	/* Register notifier before calling phy_init() */
-	ret = devm_extcon_register_notifier(glue->dev, glue->extcon,
-					EXTCON_USB_HOST, &glue->host_nb);
+	/*
+	 * Register notifier before calling phy_init(). It is unregistered in
+	 * sunxi_musb_exit(), as this runs again each time the musb-hdrc child
+	 * is bound.
+	 */
+	ret = extcon_register_notifier(glue->extcon, EXTCON_USB_HOST,
+				       &glue->host_nb);
 	if (ret)
 		goto error_reset_assert;
 
 	ret = phy_init(glue->phy);
 	if (ret)
-		goto error_reset_assert;
+		goto error_unregister_notifier;
 
 	musb->isr = sunxi_musb_interrupt;
 
@@ -263,6 +267,9 @@  static int sunxi_musb_init(struct musb *musb)
 
 	return 0;
 
+error_unregister_notifier:
+	extcon_unregister_notifier(glue->extcon, EXTCON_USB_HOST,
+				   &glue->host_nb);
 error_reset_assert:
 	if (test_bit(SUNXI_MUSB_FL_HAS_RESET, &glue->flags))
 		reset_control_assert(glue->rst);
@@ -280,6 +287,9 @@  static int sunxi_musb_exit(struct musb *musb)
 
 	pm_runtime_put(musb->controller);
 
+	/* Before cancel_work_sync(), so the notifier cannot requeue the work */
+	extcon_unregister_notifier(glue->extcon, EXTCON_USB_HOST,
+				   &glue->host_nb);
 	cancel_work_sync(&glue->work);
 	if (test_bit(SUNXI_MUSB_FL_PHY_ON, &glue->flags))
 		phy_power_off(glue->phy);