Skip to contentSkip to conversation

clk: imx: imx95-blk-ctl: Fix clock leak in error paths

PATCH Superseded Haotian Zhang 3 participants 4 messages Nov 11, 2025 – Nov 12, 2025

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

PATCH 1/1 clk: imx: imx95-blk-ctl: Fix clock leak in error paths

The probe function enables bc->clk_apb early but fails to disable it when bc_data is NULL or clk_hw_data allocation fails. The cleanup path also misses pm_runtime_put_sync() when rpm is enabled.

Add err_disable_clk label to ensure clk_disable_unprepare() is called on all error paths. Add pm_runtime_put_sync() in cleanup path when rpm is enabled.

Fixes 5224b189462f ("clk: imx: add i.MX95 BLK CTL clk driver")
---
 drivers/clk/imx/clk-imx95-blk-ctl.c | 18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)
drivers/clk/imx/clk-imx95-blk-ctl.c +14 -4
index 7e88877a6245..b12aded2b304 100644
@@ -380,13 +380,19 @@ static int imx95_bc_probe(struct platform_device *pdev)
 	}
 
 	bc_data = of_device_get_match_data(dev);
-	if (!bc_data)
-		return devm_of_platform_populate(dev);
+	if (!bc_data) {
+		ret = devm_of_platform_populate(dev);
+		if (ret)
+			clk_disable_unprepare(bc->clk_apb);
+		return ret;
+	}
 
 	clk_hw_data = devm_kzalloc(dev, struct_size(clk_hw_data, hws, bc_data->num_clks),
 				   GFP_KERNEL);
-	if (!clk_hw_data)
-		return -ENOMEM;
+	if (!clk_hw_data) {
+		ret = -ENOMEM;
+		goto err_disable_clk;
+	}
 
 	if (bc_data->rpm_enabled) {
 		devm_pm_runtime_enable(&pdev->dev);
@@ -444,7 +450,11 @@ static int imx95_bc_probe(struct platform_device *pdev)
 			continue;
 		clk_hw_unregister(hws[i]);
 	}
+	if (bc_data->rpm_enabled && pm_runtime_enabled(bc->dev))
+		pm_runtime_put_sync(&pdev->dev);
 
+err_disable_clk:
+	clk_disable_unprepare(bc->clk_apb);
 	return ret;
 }
 
Signature · 1 line
2.50.1.windows.1
reviewer
#1 lore raw
Quoted 24 lines

Subject: [PATCH] clk: imx: imx95-blk-ctl: Fix clock leak in error paths

The probe function enables bc->clk_apb early but fails to disable it when bc_data is NULL or clk_hw_data allocation fails. The cleanup path also misses pm_runtime_put_sync() when rpm is enabled.

Add err_disable_clk label to ensure clk_disable_unprepare() is called on all error paths. Add pm_runtime_put_sync() in cleanup path when rpm is enabled.

Fixes 5224b189462f ("clk: imx: add i.MX95 BLK CTL clk driver")
---
 drivers/clk/imx/clk-imx95-blk-ctl.c | 18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)
drivers/clk/imx/clk- renamed from drivers/clk/imx/clk-imx95-blk-ctl.c +0 -0

imx95-blk-ctl.c
index 7e88877a6245..b12aded2b304 100644

drivers/clk/imx/clk-imx95-blk-ctl.c +0 -0
@@ -380,13 +380,19 @@ static int imx95_bc_probe(struct
 platform_device *pdev)
 	}

drivers/clk/imx/clk-imx95-blk-ctl.c static int imx95_bc_probe(struct · line 389

Quoted 10 lines
drivers/clk/imx/clk-imx95-blk-ctl.c +6 -2
 
 	bc_data = of_device_get_match_data(dev);
-	if (!bc_data)
-		return devm_of_platform_populate(dev);
+	if (!bc_data) {
+		ret = devm_of_platform_populate(dev);
+		if (ret)
+			clk_disable_unprepare(bc->clk_apb);
+		return ret;
+	}

Use devm_clk_get_enabled to replace devm_clk_get and clk_prepare_enable. Then things could be simplified.

Regards
Peng.

Quoted 28 lines
drivers/clk/imx/clk-imx95-blk-ctl.c +4 -2
 	clk_hw_data = devm_kzalloc(dev, struct_size(clk_hw_data,
 hws, bc_data->num_clks),
 				   GFP_KERNEL);
-	if (!clk_hw_data)
-		return -ENOMEM;
+	if (!clk_hw_data) {
+		ret = -ENOMEM;
+		goto err_disable_clk;
+	}
 
 	if (bc_data->rpm_enabled) {
drivers/clk/imx/clk-imx95-blk-ctl.c +4 -1
 		devm_pm_runtime_enable(&pdev->dev);
@@ -444,7 +450,11 @@ static int imx95_bc_probe(struct
 platform_device *pdev)
 			continue;
 		clk_hw_unregister(hws[i]);
 	}
+	if (bc_data->rpm_enabled && pm_runtime_enabled(bc->dev))
+		pm_runtime_put_sync(&pdev->dev);
 
+err_disable_clk:
+	clk_disable_unprepare(bc->clk_apb);
 	return ret;
 }
 
--

2.50.1.windows.1

#2 lore raw

PATCH v2 1/1 clk: imx: imx95-blk-ctl: Fix clock leak in error paths

In reply to Haotian Zhang

The probe function enables bc->clk_apb early but fails to disable it when bc_data is NULL or clk_hw_data allocation fails. The cleanup path also misses pm_runtime_put_sync() when rpm is enabled.

Switch to devm_clk_get_enabled() to automatically manage the clock resource. Add pm_runtime_put_sync() in cleanup path when rpm is enabled.

Fixes 5224b189462f ("clk: imx: add i.MX95 BLK CTL clk driver")

---
Changes in v2:
-Use devm_clk_get_enabled() instead of manual error path cleanup

---
 drivers/clk/imx/clk-imx95-blk-ctl.c | 14 ++++----------
 1 file changed, 4 insertions(+), 10 deletions(-)
drivers/clk/imx/clk-imx95-blk-ctl.c +4 -10
index 7e88877a6245..5fdc2af7176e 100644
@@ -369,16 +369,10 @@ static int imx95_bc_probe(struct platform_device *pdev)
 		return PTR_ERR(base);
 
 	bc->base = base;
-	bc->clk_apb = devm_clk_get(dev, NULL);
+	bc->clk_apb = devm_clk_get_enabled(dev, NULL);
 	if (IS_ERR(bc->clk_apb))
 		return dev_err_probe(dev, PTR_ERR(bc->clk_apb), "failed to get APB clock\n");
 
-	ret = clk_prepare_enable(bc->clk_apb);
-	if (ret) {
-		dev_err(dev, "failed to enable apb clock: %d\n", ret);
-		return ret;
-	}
-
 	bc_data = of_device_get_match_data(dev);
 	if (!bc_data)
 		return devm_of_platform_populate(dev);
@@ -431,10 +425,8 @@ static int imx95_bc_probe(struct platform_device *pdev)
 		goto cleanup;
 	}
 
-	if (pm_runtime_enabled(bc->dev)) {
+	if (pm_runtime_enabled(bc->dev))
 		pm_runtime_put_sync(&pdev->dev);
-		clk_disable_unprepare(bc->clk_apb);
-	}
 
 	return 0;
 
@@ -444,6 +436,8 @@ static int imx95_bc_probe(struct platform_device *pdev)
 			continue;
 		clk_hw_unregister(hws[i]);
 	}
+	if (bc_data->rpm_enabled && pm_runtime_enabled(bc->dev))
+		pm_runtime_put_sync(&pdev->dev);
 
 	return ret;
 }
Signature · 1 line
2.50.1.windows.1
Reviewed-by on v2 1/1
#3 lore raw
Quoted 11 lines · Haotian Zhang

The probe function enables bc->clk_apb early but fails to disable it when bc_data is NULL or clk_hw_data allocation fails. The cleanup path also misses pm_runtime_put_sync() when rpm is enabled.

Switch to devm_clk_get_enabled() to automatically manage the clock resource. Add pm_runtime_put_sync() in cleanup path when rpm is enabled.

Fixes 5224b189462f ("clk: imx: add i.MX95 BLK CTL clk driver")

Next time, please do not send the new version as a reply to the old one.

Everything else LGTM: