Skip to contentSkip to conversation

usb: bdc: Use devm_clk_get_optional_enabled() in bdc_probe()

PATCH In review Markus Elfring 4 participants 6 messages Nov 19 – Nov 20

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 usb: bdc: Use devm_clk_get_optional_enabled() in bdc_probe()

From: Markus Elfring <[email protected]>
Date: Thu, 19 Nov 2026 20:00:07 +0100

Reuse existing functionality from devm_clk_get_optional_enabled() instead of keeping duplicate source code.

The source code was transformed by using the Coccinelle software.

---
 drivers/usb/gadget/udc/bdc/bdc_core.c | 8 +-------
 1 file changed, 1 insertion(+), 7 deletions(-)
drivers/usb/gadget/udc/bdc/bdc_core.c +1 -7
index 5149c98bf9bf..ce09327250f4 100644
@@ -530,16 +530,10 @@ static int bdc_probe(struct platform_device *pdev)
 		}
 	}
 
-	bdc->clk = devm_clk_get_optional(dev, "sw_usbd");
+	bdc->clk = devm_clk_get_optional_enabled(dev, "sw_usbd");
 	if (IS_ERR(bdc->clk))
 		return PTR_ERR(bdc->clk);
 
-	ret = clk_prepare_enable(bdc->clk);
-	if (ret) {
-		dev_err(dev, "could not enable clock\n");
-		return ret;
-	}
-
 	ret = bdc_phy_init(bdc);
 	if (ret) {
 		dev_err(bdc->dev, "BDC phy init failure:%d\n", ret);
Signature · 1 line
2.56.1
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] usb: bdc: double clk_disable_unprepare() in bdc_probe() and bdc_remove() --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] usb: bdc: double clk_disable_unprepare() in bdc_probe() and bdc_remove()
--

commit c38d4d54fe3bb2b28c05aba44e9d088e2b2d7d63
Author: Markus Elfring <[email protected]>

usb: bdc: Use devm_clk_get_optional_enabled() in bdc_probe()

This commit replaces devm_clk_get_optional() and clk_prepare_enable() in bdc_probe() with devm_clk_get_optional_enabled().

Quoted 12 lines
drivers/usb/gadget/udc/bdc/bdc_core.c +1 -1
index 5149c98bf9bf9..ce09327250f43 100644
@@ -530,16 +530,10 @@ static int bdc_probe(struct platform_device *pdev)
 		}
 	}
 
-	bdc->clk = devm_clk_get_optional(dev, "sw_usbd");
+	bdc->clk = devm_clk_get_optional_enabled(dev, "sw_usbd");
 	if (IS_ERR(bdc->clk))
 		return PTR_ERR(bdc->clk);

drivers/usb/gadget/udc/bdc/bdc_core.c static int bdc_probe(struct platform_device *pdev) · line 539

Quoted 10 lines
drivers/usb/gadget/udc/bdc/bdc_core.c +0 -6
 
-	ret = clk_prepare_enable(bdc->clk);
-	if (ret) {
-		dev_err(dev, "could not enable clock\n");
-		return ret;
-	}
-
 	ret = bdc_phy_init(bdc);
 	if (ret) {
 		dev_err(bdc->dev, "BDC phy init failure:%d\n", ret);

[Severity: High]
Does this cause clk_disable_unprepare(bdc->clk) to be called twice on probe failure or driver removal?

devm_clk_get_optional_enabled() registers a devres action that calls clk_disable_unprepare(bdc->clk) on probe failure or device unbind, but the manual clk_disable_unprepare(bdc->clk) calls remain in the disable_clk error path of bdc_probe() and in bdc_remove(). That underflows the clock enable and prepare counts, triggering WARN_ON() in clk_core_disable() and clk_core_unprepare().

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw
In reply to Markus Elfring

Le 19/11/2026 à 20:06, Markus Elfring a écrit :

Quoted 25 lines

From: Markus Elfring <[email protected]>
Date: Thu, 19 Nov 2026 20:00:07 +0100

Reuse existing functionality from devm_clk_get_optional_enabled() instead of keeping duplicate source code.

The source code was transformed by using the Coccinelle software.

---
  drivers/usb/gadget/udc/bdc/bdc_core.c | 8 +-------
  1 file changed, 1 insertion(+), 7 deletions(-)
drivers/usb/gadget/udc/bdc/bdc_core.c +1 -1
index 5149c98bf9bf..ce09327250f4 100644
@@ -530,16 +530,10 @@ static int bdc_probe(struct platform_device *pdev)
  		}
  	}
 
-	bdc->clk = devm_clk_get_optional(dev, "sw_usbd");
+	bdc->clk = devm_clk_get_optional_enabled(dev, "sw_usbd");
  	if (IS_ERR(bdc->clk))
  		return PTR_ERR(bdc->clk);

drivers/usb/gadget/udc/bdc/bdc_core.c static int bdc_probe(struct platform_device *pdev) · line 539

Quoted 10 lines
drivers/usb/gadget/udc/bdc/bdc_core.c +0 -6
 
-	ret = clk_prepare_enable(bdc->clk);
-	if (ret) {
-		dev_err(dev, "could not enable clock\n");
-		return ret;
-	}
-
  	ret = bdc_phy_init(bdc);
  	if (ret) {
  		dev_err(bdc->dev, "BDC phy init failure:%d\n", ret);

Hi,

this patch is wrong because the remove function and the error handling path of the probe need to be updated.

CJ

#3 lore raw
Quoted 37 lines · Christophe JAILLET

Le 19/11/2026 à 20:06, Markus Elfring a écrit :

Quoted 23 lines

From: Markus Elfring <[email protected]>
Date: Thu, 19 Nov 2026 20:00:07 +0100

Reuse existing functionality from devm_clk_get_optional_enabled() instead of keeping duplicate source code.

The source code was transformed by using the Coccinelle software.

---
  drivers/usb/gadget/udc/bdc/bdc_core.c | 8 +-------
  1 file changed, 1 insertion(+), 7 deletions(-)
drivers/usb/gadget/udc/bdc/bdc_core.c +1 -1
index 5149c98bf9bf..ce09327250f4 100644
@@ -530,16 +530,10 @@ static int bdc_probe(struct platform_device *pdev)
  		}
  	}
-	bdc->clk = devm_clk_get_optional(dev, "sw_usbd");
+	bdc->clk = devm_clk_get_optional_enabled(dev, "sw_usbd");
  	if (IS_ERR(bdc->clk))

drivers/usb/gadget/udc/bdc/bdc_core.c static int bdc_probe(struct platform_device *pdev) · line 537

Quoted 10 lines
drivers/usb/gadget/udc/bdc/bdc_core.c +0 -6
  		return PTR_ERR(bdc->clk);
-	ret = clk_prepare_enable(bdc->clk);
-	if (ret) {
-		dev_err(dev, "could not enable clock\n");
-		return ret;
-	}
-
  	ret = bdc_phy_init(bdc);
  	if (ret) {
  		dev_err(bdc->dev, "BDC phy init failure:%d\n", ret);

Hi,

Quoted 2 lines

this patch is wrong because the remove function and the error handling path of the probe need to be updated.

Don't worry, we don't apply patches from this bot :)

#4 lore raw
Quoted 3 lines
Quoted 2 lines

this patch is wrong because the remove function and the error handling path of the probe need to be updated.

Don't worry, we don't apply patches from this bot 🙂

I guess that collateral evolution can become more constructive again.
https://git.kernel.org/pub/scm/linux/kernel/git/next/linux-next.git/log/?qt=author&q=Elfring

Would further contributors dare to pick corresponding change opportunities up?
https://elixir.bootlin.com/linux/v7.4-rc1/source/drivers/usb/gadget/udc/bdc/bdc_core.c#L483-L583

https://elixir.bootlin.com/linux/v7.4-rc1/source/include/linux/clk.h#L680-L699
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/driver-api/driver-model/devres.rst?id=d220c9443db6e5f20a60cad55c6a0ae7a75c63b8#n242

Regards,
Markus