Skip to contentSkip to conversation

mmc: sdhci-of-dwcmshc: check bus clock enable result in the, probe() method

PATCH Superseded Sergey Shtylyov 2 participants 6 messages May 29 – Jun 1

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 mmc: sdhci-of-dwcmshc: check bus clock enable result in the, probe() method

In the driver's probe() method, clk_disable_unprepare() for the bus clock is called on the error path even if the prior clk_prepare_enable() call has failed (and the same thing happens in the remove() method as well) -- that would cause the prepare/enable counter imbalance. Also, the same problem can happen in the driver's suspend() method; note that the resume() method does check the clk_prepare_enable()'s result -- let's be consistent and do that in probe() method as well. BTW, I don't know for sure what does the bus clock control -- if it affects the register accesses, the driver will likely to cause (e.g. on ARM) a kernel oops if it fails to prepare/enable the bus clock in the probe() method...

Found by Linux Verification Center (linuxtesting.org) with the Svace static analysis tool.

Fixes e438cf49b305 ("mmc: sdhci-of-dwcmshc: add SDHCI OF Synopsys DWC MSHC driver")
Fixes bccce2ec7790 ("mmc: sdhci-of-dwcmshc: add suspend/resume support")

---
This patch is against the fixes branch of Ulf Hansson's mmc.git repo.

Changes in version 2:
- mentioned the problem in the remove() method as well, somewhat rephrased the patch description.

 drivers/mmc/host/sdhci-of-dwcmshc.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
drivers/mmc/host/sdhci-of-dwcmshc.c +9 -5
index b9ecd91f44ad..29af07561a86 100644
@@ -2441,13 +2441,16 @@ static int dwcmshc_probe(struct platform_device *pdev)
 			return err;
 
 		priv->bus_clk = devm_clk_get(dev, "bus");
-		if (!IS_ERR(priv->bus_clk))
-			clk_prepare_enable(priv->bus_clk);
+		if (!IS_ERR(priv->bus_clk)) {
+			err = clk_prepare_enable(priv->bus_clk);
+			if (err)
+				goto err_clk;
+		}
 	}
 
 	err = mmc_of_parse(host->mmc);
 	if (err)
-		goto err_clk;
+		goto err_bus_clk;
 
 	sdhci_get_of_property(pdev);
 
@@ -2461,7 +2464,7 @@ static int dwcmshc_probe(struct platform_device *pdev)
 	if (pltfm_data->init) {
 		err = pltfm_data->init(&pdev->dev, host, priv);
 		if (err)
-			goto err_clk;
+			goto err_bus_clk;
 	}
 
 #ifdef CONFIG_ACPI
@@ -2507,9 +2510,10 @@ static int dwcmshc_probe(struct platform_device *pdev)
 err_rpm:
 	pm_runtime_disable(dev);
 	pm_runtime_put_noidle(dev);
+err_bus_clk:
+	clk_disable_unprepare(priv->bus_clk);
 err_clk:
 	clk_disable_unprepare(pltfm_host->clk);
-	clk_disable_unprepare(priv->bus_clk);
 	clk_bulk_disable_unprepare(priv->num_other_clks, priv->other_clks);
 	return err;
 }
Signature · 1 line
2.54.0
#1 lore raw

Hello!

Oops, a stray comma sneaked into the subject again (I had to use Thunderbird again)...

MBR, Sergey

maintainer
#2 lore raw
In reply to Sergey Shtylyov
Quoted 4 lines · Sergey Shtylyov

In the driver's probe() method, clk_disable_unprepare() for the bus clock is called on the error path even if the prior clk_prepare_enable() call has failed (and the same thing happens in the remove() method as well) -- that would cause the prepare/enable counter imbalance. Also, the same problem

Doesn't clk_disable_unprepare() do nothing if clk is IS_ERR_OR_NULL()?

Quoted 69 lines

can happen in the driver's suspend() method; note that the resume() method does check the clk_prepare_enable()'s result -- let's be consistent and do that in probe() method as well. BTW, I don't know for sure what does the bus clock control -- if it affects the register accesses, the driver will likely to cause (e.g. on ARM) a kernel oops if it fails to prepare/enable the bus clock in the probe() method...

Found by Linux Verification Center (linuxtesting.org) with the Svace static analysis tool.

Fixes e438cf49b305 ("mmc: sdhci-of-dwcmshc: add SDHCI OF Synopsys DWC MSHC driver")
Fixes bccce2ec7790 ("mmc: sdhci-of-dwcmshc: add suspend/resume support")

---
This patch is against the fixes branch of Ulf Hansson's mmc.git repo.

Changes in version 2:
- mentioned the problem in the remove() method as well, somewhat rephrased the patch description.

 drivers/mmc/host/sdhci-of-dwcmshc.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
drivers/mmc/host/sdhci-of-dwcmshc.c +9 -5
index b9ecd91f44ad..29af07561a86 100644
@@ -2441,13 +2441,16 @@ static int dwcmshc_probe(struct platform_device *pdev)
 			return err;
 
 		priv->bus_clk = devm_clk_get(dev, "bus");
-		if (!IS_ERR(priv->bus_clk))
-			clk_prepare_enable(priv->bus_clk);
+		if (!IS_ERR(priv->bus_clk)) {
+			err = clk_prepare_enable(priv->bus_clk);
+			if (err)
+				goto err_clk;
+		}
 	}
 
 	err = mmc_of_parse(host->mmc);
 	if (err)
-		goto err_clk;
+		goto err_bus_clk;
 
 	sdhci_get_of_property(pdev);
 
@@ -2461,7 +2464,7 @@ static int dwcmshc_probe(struct platform_device *pdev)
 	if (pltfm_data->init) {
 		err = pltfm_data->init(&pdev->dev, host, priv);
 		if (err)
-			goto err_clk;
+			goto err_bus_clk;
 	}
 
 #ifdef CONFIG_ACPI
@@ -2507,9 +2510,10 @@ static int dwcmshc_probe(struct platform_device *pdev)
 err_rpm:
 	pm_runtime_disable(dev);
 	pm_runtime_put_noidle(dev);
+err_bus_clk:
+	clk_disable_unprepare(priv->bus_clk);
 err_clk:
 	clk_disable_unprepare(pltfm_host->clk);
-	clk_disable_unprepare(priv->bus_clk);
 	clk_bulk_disable_unprepare(priv->num_other_clks, priv->other_clks);
 	return err;
 }
#3 lore raw
Quoted 7 lines · Adrian Hunter
Quoted 4 lines · Sergey Shtylyov

In the driver's probe() method, clk_disable_unprepare() for the bus clock is called on the error path even if the prior clk_prepare_enable() call has failed (and the same thing happens in the remove() method as well) -- that would cause the prepare/enable counter imbalance. Also, the same problem

Doesn't clk_disable_unprepare() do nothing if clk is IS_ERR_OR_NULL()?

Yes, I does. But my patch is not about this case, it's about the case when
clk_prepare_enable() for the bus clock fails...

Quoted 7 lines
Quoted 7 lines

can happen in the driver's suspend() method; note that the resume() method does check the clk_prepare_enable()'s result -- let's be consistent and do that in probe() method as well. BTW, I don't know for sure what does the bus clock control -- if it affects the register accesses, the driver will likely to cause (e.g. on ARM) a kernel oops if it fails to prepare/enable the bus clock in the probe() method...

Quoted 6 lines
Quoted 6 lines

Found by Linux Verification Center (linuxtesting.org) with the Svace static analysis tool.

Fixes e438cf49b305 ("mmc: sdhci-of-dwcmshc: add SDHCI OF Synopsys DWC MSHC driver")
Fixes bccce2ec7790 ("mmc: sdhci-of-dwcmshc: add suspend/resume support")

[...]

MBR, Sergey

maintainer Acked-by on v2 1/1
#4 lore raw
In reply to Sergey Shtylyov
Quoted 11 lines · Sergey Shtylyov

In the driver's probe() method, clk_disable_unprepare() for the bus clock is called on the error path even if the prior clk_prepare_enable() call has failed (and the same thing happens in the remove() method as well) -- that would cause the prepare/enable counter imbalance. Also, the same problem can happen in the driver's suspend() method; note that the resume() method does check the clk_prepare_enable()'s result -- let's be consistent and do that in probe() method as well. BTW, I don't know for sure what does the bus clock control -- if it affects the register accesses, the driver will likely to cause (e.g. on ARM) a kernel oops if it fails to prepare/enable the bus clock in the probe() method...

Quoted 6 lines

Found by Linux Verification Center (linuxtesting.org) with the Svace static analysis tool.

Fixes e438cf49b305 ("mmc: sdhci-of-dwcmshc: add SDHCI OF Synopsys DWC MSHC driver")
Fixes bccce2ec7790 ("mmc: sdhci-of-dwcmshc: add suspend/resume support")
Quoted 56 lines

---
This patch is against the fixes branch of Ulf Hansson's mmc.git repo.

Changes in version 2:
- mentioned the problem in the remove() method as well, somewhat rephrased the patch description.

 drivers/mmc/host/sdhci-of-dwcmshc.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
drivers/mmc/host/sdhci-of-dwcmshc.c +9 -5
index b9ecd91f44ad..29af07561a86 100644
@@ -2441,13 +2441,16 @@ static int dwcmshc_probe(struct platform_device *pdev)
 			return err;
 
 		priv->bus_clk = devm_clk_get(dev, "bus");
-		if (!IS_ERR(priv->bus_clk))
-			clk_prepare_enable(priv->bus_clk);
+		if (!IS_ERR(priv->bus_clk)) {
+			err = clk_prepare_enable(priv->bus_clk);
+			if (err)
+				goto err_clk;
+		}
 	}
 
 	err = mmc_of_parse(host->mmc);
 	if (err)
-		goto err_clk;
+		goto err_bus_clk;
 
 	sdhci_get_of_property(pdev);
 
@@ -2461,7 +2464,7 @@ static int dwcmshc_probe(struct platform_device *pdev)
 	if (pltfm_data->init) {
 		err = pltfm_data->init(&pdev->dev, host, priv);
 		if (err)
-			goto err_clk;
+			goto err_bus_clk;
 	}
 
 #ifdef CONFIG_ACPI
@@ -2507,9 +2510,10 @@ static int dwcmshc_probe(struct platform_device *pdev)
 err_rpm:
 	pm_runtime_disable(dev);
 	pm_runtime_put_noidle(dev);
+err_bus_clk:
+	clk_disable_unprepare(priv->bus_clk);
 err_clk:
 	clk_disable_unprepare(pltfm_host->clk);
-	clk_disable_unprepare(priv->bus_clk);
 	clk_bulk_disable_unprepare(priv->num_other_clks, priv->other_clks);
 	return err;
 }
#5 lore raw

On 6/1/26 12:19 PM, Adrian Hunter wrote:
[...]

Quoted 9 lines
Quoted 9 lines

In the driver's probe() method, clk_disable_unprepare() for the bus clock is called on the error path even if the prior clk_prepare_enable() call has failed (and the same thing happens in the remove() method as well) -- that would cause the prepare/enable counter imbalance. Also, the same problem can happen in the driver's suspend() method; note that the resume() method does check the clk_prepare_enable()'s result -- let's be consistent and do that in probe() method as well. BTW, I don't know for sure what does the bus clock control -- if it affects the register accesses, the driver will likely to cause (e.g. on ARM) a kernel oops if it fails to prepare/enable

Oops, "to" wasn't need here. I'll post v4 (with the stray comma removed as
well)...

Quoted 10 lines
Quoted 8 lines

the bus clock in the probe() method...

Found by Linux Verification Center (linuxtesting.org) with the Svace static analysis tool.

Fixes e438cf49b305 ("mmc: sdhci-of-dwcmshc: add SDHCI OF Synopsys DWC MSHC driver")
Fixes bccce2ec7790 ("mmc: sdhci-of-dwcmshc: add suspend/resume support")

Thanks!

[...]

MBR, Sergey