Linux USB
 help / color / mirror / Atom feed
* [PATCH] usb: phy: twl6030: init work before comparator and clear comparator on teardown
@ 2026-11-26  6:51 Bin Luo (University of Electronic Science and Technology of China)
  2026-11-26  7:01 ` Greg KH
  2026-11-26  7:02 ` sashiko-bot
  0 siblings, 2 replies; 3+ messages in thread
From: Bin Luo (University of Electronic Science and Technology of China) @ 2026-11-26  6:51 UTC (permalink / raw)
  To: gregkh; +Cc: linux-usb, linux-kernel

twl6030_usb_probe() publishes &twl->comparator via
omap_usb2_set_comparator() before initializing twl->set_vbus_work and
twl->lock. If the OMAP USB2 PHY invokes twl6030_set_vbus() before
INIT_WORK() runs, schedule_work(&twl->set_vbus_work) operates on an
uninitialized work_struct.

In addition, neither the probe error paths nor twl6030_usb_remove()
clears the comparator via omap_usb2_set_comparator(NULL). Because twl
is devm-allocated, a subsequent VBUS callback from the OMAP USB2 PHY
dereferences freed twl memory and schedules set_vbus_work after
teardown.

Initialize twl->lock and work items before registering the comparator,
and clear the comparator with omap_usb2_set_comparator(NULL) before
canceling set_vbus_work in both probe failure paths and
twl6030_usb_remove().

Fixes: 0e98de67bacb ("usb: otg: make twl6030_usb as a comparator driver to omap_usb2")
Signed-off-by: Bin Luo (University of Electronic Science and Technology of China) <luob87709@gmail.com>
---
Found by code inspection; not compile-tested on ARM/OMAP or runtime-tested on TWL6030 hardware.

 drivers/usb/phy/phy-twl6030-usb.c | 18 +++++++++++-------
 1 file changed, 11 insertions(+), 7 deletions(-)

diff --git a/drivers/usb/phy/phy-twl6030-usb.c b/drivers/usb/phy/phy-twl6030-usb.c
index 9bfcb2c..a9da7aa 100644
--- a/drivers/usb/phy/phy-twl6030-usb.c
+++ b/drivers/usb/phy/phy-twl6030-usb.c
@@ -355,26 +355,26 @@ static int twl6030_usb_probe(struct platform_device *pdev)
 	twl->comparator.set_vbus	= twl6030_set_vbus;
 	twl->comparator.start_srp	= twl6030_start_srp;
 
+	/* init spinlock and work items before publishing comparator */
+	spin_lock_init(&twl->lock);
+	INIT_WORK(&twl->set_vbus_work, otg_set_vbus_work);
+	INIT_DELAYED_WORK(&twl->get_status_work, twl6030_status_work);
+
 	ret = omap_usb2_set_comparator(&twl->comparator);
 	if (ret == -ENODEV) {
 		dev_info(&pdev->dev, "phy not ready, deferring probe");
 		return -EPROBE_DEFER;
 	}
 
-	/* init spinlock for workqueue */
-	spin_lock_init(&twl->lock);
-
 	err = twl6030_usb_ldo_init(twl);
 	if (err) {
 		dev_err(&pdev->dev, "ldo init failed\n");
-		return err;
+		status = err;
+		goto err_clear_comparator;
 	}
 
 	platform_set_drvdata(pdev, twl);
 
-	INIT_WORK(&twl->set_vbus_work, otg_set_vbus_work);
-	INIT_DELAYED_WORK(&twl->get_status_work, twl6030_status_work);
-
 	status = request_threaded_irq(twl->irq1, NULL, twl6030_usbotg_irq,
 			IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING | IRQF_ONESHOT,
 			"twl6030_usb", twl);
@@ -404,6 +404,9 @@ err_free_irq1:
 	free_irq(twl->irq1, twl);
 err_put_regulator:
 	regulator_put(twl->usb3v3);
+err_clear_comparator:
+	omap_usb2_set_comparator(NULL);
+	cancel_work_sync(&twl->set_vbus_work);
 
 	return status;
 }
@@ -412,6 +415,7 @@ static void twl6030_usb_remove(struct platform_device *pdev)
 {
 	struct twl6030_usb *twl = platform_get_drvdata(pdev);
 
+	omap_usb2_set_comparator(NULL);
 	cancel_delayed_work_sync(&twl->get_status_work);
 	twl6030_interrupt_mask(TWL6030_USBOTG_INT_MASK,
 		REG_INT_MSK_LINE_C);
-- 
2.43.0


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

* Re: [PATCH] usb: phy: twl6030: init work before comparator and clear comparator on teardown
  2026-11-26  6:51 [PATCH] usb: phy: twl6030: init work before comparator and clear comparator on teardown Bin Luo (University of Electronic Science and Technology of China)
@ 2026-11-26  7:01 ` Greg KH
  2026-11-26  7:02 ` sashiko-bot
  1 sibling, 0 replies; 3+ messages in thread
From: Greg KH @ 2026-11-26  7:01 UTC (permalink / raw)
  To: Bin Luo (University of Electronic Science and Technology of China)
  Cc: linux-usb, linux-kernel

On Thu, Nov 26, 2026 at 02:51:39PM +0800, Bin Luo (University of Electronic Science and Technology of China) wrote:
> twl6030_usb_probe() publishes &twl->comparator via
> omap_usb2_set_comparator() before initializing twl->set_vbus_work and
> twl->lock. If the OMAP USB2 PHY invokes twl6030_set_vbus() before
> INIT_WORK() runs, schedule_work(&twl->set_vbus_work) operates on an
> uninitialized work_struct.
> 
> In addition, neither the probe error paths nor twl6030_usb_remove()
> clears the comparator via omap_usb2_set_comparator(NULL). Because twl
> is devm-allocated, a subsequent VBUS callback from the OMAP USB2 PHY
> dereferences freed twl memory and schedules set_vbus_work after
> teardown.
> 
> Initialize twl->lock and work items before registering the comparator,
> and clear the comparator with omap_usb2_set_comparator(NULL) before
> canceling set_vbus_work in both probe failure paths and
> twl6030_usb_remove().
> 
> Fixes: 0e98de67bacb ("usb: otg: make twl6030_usb as a comparator driver to omap_usb2")
> Signed-off-by: Bin Luo (University of Electronic Science and Technology of China) <luob87709@gmail.com>
> ---
> Found by code inspection; not compile-tested on ARM/OMAP or runtime-tested on TWL6030 hardware.
> 
>  drivers/usb/phy/phy-twl6030-usb.c | 18 +++++++++++-------
>  1 file changed, 11 insertions(+), 7 deletions(-)
> 
> diff --git a/drivers/usb/phy/phy-twl6030-usb.c b/drivers/usb/phy/phy-twl6030-usb.c
> index 9bfcb2c..a9da7aa 100644
> --- a/drivers/usb/phy/phy-twl6030-usb.c
> +++ b/drivers/usb/phy/phy-twl6030-usb.c
> @@ -355,26 +355,26 @@ static int twl6030_usb_probe(struct platform_device *pdev)
>  	twl->comparator.set_vbus	= twl6030_set_vbus;
>  	twl->comparator.start_srp	= twl6030_start_srp;
>  
> +	/* init spinlock and work items before publishing comparator */

No need for this comment, right?

> +	spin_lock_init(&twl->lock);
> +	INIT_WORK(&twl->set_vbus_work, otg_set_vbus_work);
> +	INIT_DELAYED_WORK(&twl->get_status_work, twl6030_status_work);
> +
>  	ret = omap_usb2_set_comparator(&twl->comparator);
>  	if (ret == -ENODEV) {
>  		dev_info(&pdev->dev, "phy not ready, deferring probe");
>  		return -EPROBE_DEFER;
>  	}
>  
> -	/* init spinlock for workqueue */
> -	spin_lock_init(&twl->lock);
> -
>  	err = twl6030_usb_ldo_init(twl);
>  	if (err) {
>  		dev_err(&pdev->dev, "ldo init failed\n");
> -		return err;
> +		status = err;
> +		goto err_clear_comparator;
>  	}
>  
>  	platform_set_drvdata(pdev, twl);
>  
> -	INIT_WORK(&twl->set_vbus_work, otg_set_vbus_work);
> -	INIT_DELAYED_WORK(&twl->get_status_work, twl6030_status_work);
> -
>  	status = request_threaded_irq(twl->irq1, NULL, twl6030_usbotg_irq,
>  			IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING | IRQF_ONESHOT,
>  			"twl6030_usb", twl);
> @@ -404,6 +404,9 @@ err_free_irq1:
>  	free_irq(twl->irq1, twl);
>  err_put_regulator:
>  	regulator_put(twl->usb3v3);
> +err_clear_comparator:
> +	omap_usb2_set_comparator(NULL);
> +	cancel_work_sync(&twl->set_vbus_work);

Why these changes?  You are doing multiplke things in the same patch,
pleaase break this up into smaller patches.

thanks,

greg k-h

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

* Re: [PATCH] usb: phy: twl6030: init work before comparator and clear comparator on teardown
  2026-11-26  6:51 [PATCH] usb: phy: twl6030: init work before comparator and clear comparator on teardown Bin Luo (University of Electronic Science and Technology of China)
  2026-11-26  7:01 ` Greg KH
@ 2026-11-26  7:02 ` sashiko-bot
  1 sibling, 0 replies; 3+ messages in thread
From: sashiko-bot @ 2026-11-26  7:02 UTC (permalink / raw)
  To: Bin Luo (University of Electronic Science and Technology of China)
  Cc: linux-usb

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] usb: phy: twl6030: unsynchronized comparator clearing in
  probe and remove
--

commit 7291457bcd76118c22b37c6b874744fe4ac3dc1c
Author: luob87709@gmail.com <luob87709@gmail.com>

usb: phy: twl6030: init work before comparator and clear comparator on teardown

This commit initializes twl->lock and work items in twl6030_usb_probe()
before registering &twl->comparator with omap_usb2_set_comparator(), and
clears the comparator on probe failure and in twl6030_usb_remove().

> diff --git a/drivers/usb/phy/phy-twl6030-usb.c b/drivers/usb/phy/phy-twl6030-usb.c
> index 9bfcb2c1dfd0e..a9da7aa5f5f14 100644
> --- a/drivers/usb/phy/phy-twl6030-usb.c
> +++ b/drivers/usb/phy/phy-twl6030-usb.c
> @@ -355,26 +355,26 @@ static int twl6030_usb_probe(struct platform_device *pdev)
>  	twl->comparator.set_vbus	= twl6030_set_vbus;
>  	twl->comparator.start_srp	= twl6030_start_srp;
>  
> +	/* init spinlock and work items before publishing comparator */
> +	spin_lock_init(&twl->lock);
> +	INIT_WORK(&twl->set_vbus_work, otg_set_vbus_work);
> +	INIT_DELAYED_WORK(&twl->get_status_work, twl6030_status_work);
> +
>  	ret = omap_usb2_set_comparator(&twl->comparator);
>  	if (ret == -ENODEV) {
>  		dev_info(&pdev->dev, "phy not ready, deferring probe");
>  		return -EPROBE_DEFER;
>  	}
>  
> -	/* init spinlock for workqueue */
> -	spin_lock_init(&twl->lock);
> -
>  	err = twl6030_usb_ldo_init(twl);
>  	if (err) {
>  		dev_err(&pdev->dev, "ldo init failed\n");
> -		return err;
> +		status = err;
> +		goto err_clear_comparator;
>  	}
>  
>  	platform_set_drvdata(pdev, twl);
>  
> -	INIT_WORK(&twl->set_vbus_work, otg_set_vbus_work);
> -	INIT_DELAYED_WORK(&twl->get_status_work, twl6030_status_work);
> -
>  	status = request_threaded_irq(twl->irq1, NULL, twl6030_usbotg_irq,
>  			IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING | IRQF_ONESHOT,
>  			"twl6030_usb", twl);
> @@ -404,6 +404,9 @@ static int twl6030_usb_probe(struct platform_device *pdev)
>  	free_irq(twl->irq1, twl);
>  err_put_regulator:
>  	regulator_put(twl->usb3v3);
> +err_clear_comparator:
> +	omap_usb2_set_comparator(NULL);
> +	cancel_work_sync(&twl->set_vbus_work);
>  
>  	return status;
>  }
> @@ -412,6 +415,7 @@ static void twl6030_usb_remove(struct platform_device *pdev)
>  {
>  	struct twl6030_usb *twl = platform_get_drvdata(pdev);
>  
> +	omap_usb2_set_comparator(NULL);
>  	cancel_delayed_work_sync(&twl->get_status_work);
>  	twl6030_interrupt_mask(TWL6030_USBOTG_INT_MASK,
>  		REG_INT_MSK_LINE_C);

[Severity: Medium]
Can an in-flight omap_usb_set_vbus() or omap_usb_start_srp() call still
race with omap_usb2_set_comparator(NULL) on probe error or remove?

drivers/phy/ti/phy-omap-usb2.c:omap_usb_set_vbus() {
	...
	if (!phy->comparator || !phy->comparator->set_vbus)
		return -ENODEV;

	return phy->comparator->set_vbus(phy->comparator, enabled);
}

omap_usb2_set_comparator() does a plain unlocked assignment to
phy->comparator, and omap_usb_set_vbus() and omap_usb_start_srp() read and
dereference phy->comparator without locking or lifetime tracking:

CPU0                              CPU1
                                  omap_usb_set_vbus()
                                    if (!phy->comparator || ...)
twl6030_usb_remove()
  omap_usb2_set_comparator(NULL)
  ...
  cancel_work_sync(&twl->set_vbus_work)
  /* devres frees twl */
                                    twl6030_set_vbus()
                                      twl->vbus_enable = enabled
                                      schedule_work(&twl->set_vbus_work)
                                        <-- use-after-free

If the compiler reloads phy->comparator after the check, CPU1 can also hit
a NULL pointer dereference, and omap_usb_start_srp() -> twl6030_start_srp()
spends 100 ms in mdelay() before accessing twl while teardown does not
wait for it.  In addition, twl6030_usb_probe() still publishes
&twl->comparator before fallible steps in twl6030_usb_ldo_init() and
request_threaded_irq().

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261126065139.127-1-luob87709@gmail.com?part=1

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

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

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-26  6:51 [PATCH] usb: phy: twl6030: init work before comparator and clear comparator on teardown Bin Luo (University of Electronic Science and Technology of China)
2026-11-26  7:01 ` Greg KH
2026-11-26  7:02 ` sashiko-bot

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