Skip to contentSkip to conversation

cpufreq: Fix leaks on probe error paths in three drivers

PATCH Superseded Suyuan Wang 2 participants 7 messages Nov 22 – Nov 23

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover cpufreq: Fix leaks on probe error paths in three drivers

Three small fixes for resources that are not released when a cpufreq driver fails to probe: the cpu clock in highbank, the three clocks in kirkwood (also on unbind), and the regulators and register mappings in s5pv210, and its reboot notifier, when cpufreq_register_driver() fails.

Found with a static analysis tool and checked by hand. Compile tested on arm (W=1, multi_v5 for kirkwood); I don't have the hardware.

---
Suyuan Wang (3):
cpufreq: highbank: Put cpu clock if clk notifier registration fails
cpufreq: kirkwood: Put clocks on error and in remove
cpufreq: s5pv210: Fix resource leaks on probe error paths

 drivers/cpufreq/highbank-cpufreq.c |  1 +
 drivers/cpufreq/kirkwood-cpufreq.c | 15 ++++++++++++---
 drivers/cpufreq/s5pv210-cpufreq.c  | 12 +++++++++---
 3 files changed, 22 insertions(+), 6 deletions(-)
#1 lore raw

PATCH 1/3 cpufreq: highbank: Put cpu clock if clk notifier registration fails

From: Suyuan Wang <[email protected]>

hb_cpufreq_driver_init() gets the cpu0 clock with clk_get() and then registers a clk notifier on it. If clk_notifier_register() fails, the function jumps to out_put_node and returns the error, but the clock reference is never dropped.

Call clk_put() on that error path.

Found with a static analysis tool and checked by hand.

Fixes 6754f556103b ("cpufreq / highbank: add support for highbank cpufreq")
---
 drivers/cpufreq/highbank-cpufreq.c | 1 +
 1 file changed, 1 insertion(+)
drivers/cpufreq/highbank-cpufreq.c +1 -0
index a45864701143..ccdaff4384d2 100644
@@ -89,6 +89,7 @@ static int __init hb_cpufreq_driver_init(void)
 	ret = clk_notifier_register(cpu_clk, &hb_cpufreq_clk_nb);
 	if (ret) {
 		pr_err("failed to register clk notifier: %d\n", ret);
+		clk_put(cpu_clk);
 		goto out_put_node;
 	}
 
Signature · 1 line
2.34.1
Reviewed-by on v1 1/3
#2 lore raw

Re: [PATCH 1/3] cpufreq: highbank: Put cpu clock if clk notifier registration fails

Quoted 7 lines · On 11/22/2026 7:55 PM, Suyuan Wang via B4 Relay wrote:

From: Suyuan Wang <[email protected]>

hb_cpufreq_driver_init() gets the cpu0 clock with clk_get() and then registers a clk notifier on it. If clk_notifier_register() fails, the function jumps to out_put_node and returns the error, but the clock reference is never dropped.

Quoted 8 lines

Call clk_put() on that error path.

Found with a static analysis tool and checked by hand.

Fixes 6754f556103b ("cpufreq / highbank: add support for highbank cpufreq")
Quoted 17 lines
---
  drivers/cpufreq/highbank-cpufreq.c | 1 +
  1 file changed, 1 insertion(+)
drivers/cpufreq/highbank-cpufreq.c +1 -0
index a45864701143..ccdaff4384d2 100644
@@ -89,6 +89,7 @@ static int __init hb_cpufreq_driver_init(void)
  	ret = clk_notifier_register(cpu_clk, &hb_cpufreq_clk_nb);
  	if (ret) {
  		pr_err("failed to register clk notifier: %d\n", ret);
+		clk_put(cpu_clk);
  		goto out_put_node;
  	}
 
Signature · 2 lines
Thx and BRs,
Zhongqiu Han
#3 lore raw

PATCH 2/3 cpufreq: kirkwood: Put clocks on error and in remove

In reply to Suyuan Wang

From: Suyuan Wang <[email protected]>

kirkwood_cpufreq_probe() gets three clocks with of_clk_get_by_name():
cpu_clk, ddrclk and powersave. None of them is ever released with clk_put(). The error paths only disable and unprepare the clocks, and kirkwood_cpufreq_remove() does the same. So the clock references leak when probe fails (for example on -EPROBE_DEFER) and on every unbind.

Add the missing clk_put() calls to the error paths and to remove. The "Unable to prepare" paths now jump to new labels that only put the clock that failed to enable.

Found with a static analysis tool and checked by hand.

Fixes 2a4bd9f0db24 ("cpufreq: kirkwood: Add a cpufreq driver for Marvell Kirkwood SoCs")
---
 drivers/cpufreq/kirkwood-cpufreq.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)
drivers/cpufreq/kirkwood-cpufreq.c +12 -3
index 24b285cbeb8d..e85658081c07 100644
@@ -125,7 +125,7 @@ static int kirkwood_cpufreq_probe(struct platform_device *pdev)
 	err = clk_prepare_enable(priv.cpu_clk);
 	if (err) {
 		dev_err(priv.dev, "Unable to prepare cpuclk\n");
-		goto out_node;
+		goto out_put_cpu;
 	}
 
 	kirkwood_freq_table[0].frequency = clk_get_rate(priv.cpu_clk) / 1000;
@@ -140,7 +140,7 @@ static int kirkwood_cpufreq_probe(struct platform_device *pdev)
 	err = clk_prepare_enable(priv.ddr_clk);
 	if (err) {
 		dev_err(priv.dev, "Unable to prepare ddrclk\n");
-		goto out_cpu;
+		goto out_put_ddr;
 	}
 	kirkwood_freq_table[1].frequency = clk_get_rate(priv.ddr_clk) / 1000;
 
@@ -153,7 +153,7 @@ static int kirkwood_cpufreq_probe(struct platform_device *pdev)
 	err = clk_prepare_enable(priv.powersave_clk);
 	if (err) {
 		dev_err(priv.dev, "Unable to prepare powersave clk\n");
-		goto out_ddr;
+		goto out_put_powersave;
 	}
 
 	err = cpufreq_register_driver(&kirkwood_cpufreq_driver);
@@ -167,10 +167,16 @@ static int kirkwood_cpufreq_probe(struct platform_device *pdev)
 
 out_powersave:
 	clk_disable_unprepare(priv.powersave_clk);
+out_put_powersave:
+	clk_put(priv.powersave_clk);
 out_ddr:
 	clk_disable_unprepare(priv.ddr_clk);
+out_put_ddr:
+	clk_put(priv.ddr_clk);
 out_cpu:
 	clk_disable_unprepare(priv.cpu_clk);
+out_put_cpu:
+	clk_put(priv.cpu_clk);
 out_node:
 	of_node_put(np);
 
@@ -182,8 +188,11 @@ static void kirkwood_cpufreq_remove(struct platform_device *pdev)
 	cpufreq_unregister_driver(&kirkwood_cpufreq_driver);
 
 	clk_disable_unprepare(priv.powersave_clk);
+	clk_put(priv.powersave_clk);
 	clk_disable_unprepare(priv.ddr_clk);
+	clk_put(priv.ddr_clk);
 	clk_disable_unprepare(priv.cpu_clk);
+	clk_put(priv.cpu_clk);
 }
 
 static struct platform_driver kirkwood_cpufreq_platform_driver = {
Signature · 1 line
2.34.1
Reviewed-by on v1 2/3
#4 lore raw

Re: [PATCH 2/3] cpufreq: kirkwood: Put clocks on error and in remove

Quoted 12 lines · On 11/22/2026 7:55 PM, Suyuan Wang via B4 Relay wrote:

From: Suyuan Wang <[email protected]>

kirkwood_cpufreq_probe() gets three clocks with of_clk_get_by_name():
cpu_clk, ddrclk and powersave. None of them is ever released with clk_put(). The error paths only disable and unprepare the clocks, and kirkwood_cpufreq_remove() does the same. So the clock references leak when probe fails (for example on -EPROBE_DEFER) and on every unbind.

Add the missing clk_put() calls to the error paths and to remove. The "Unable to prepare" paths now jump to new labels that only put the clock that failed to enable.

Quoted 6 lines

Found with a static analysis tool and checked by hand.

Fixes 2a4bd9f0db24 ("cpufreq: kirkwood: Add a cpufreq driver for Marvell Kirkwood SoCs")
Quoted 65 lines
---
  drivers/cpufreq/kirkwood-cpufreq.c | 15 ++++++++++++---
  1 file changed, 12 insertions(+), 3 deletions(-)
drivers/cpufreq/kirkwood-cpufreq.c +12 -3
index 24b285cbeb8d..e85658081c07 100644
@@ -125,7 +125,7 @@ static int kirkwood_cpufreq_probe(struct platform_device *pdev)
  	err = clk_prepare_enable(priv.cpu_clk);
  	if (err) {
  		dev_err(priv.dev, "Unable to prepare cpuclk\n");
-		goto out_node;
+		goto out_put_cpu;
  	}
 
  	kirkwood_freq_table[0].frequency = clk_get_rate(priv.cpu_clk) / 1000;
@@ -140,7 +140,7 @@ static int kirkwood_cpufreq_probe(struct platform_device *pdev)
  	err = clk_prepare_enable(priv.ddr_clk);
  	if (err) {
  		dev_err(priv.dev, "Unable to prepare ddrclk\n");
-		goto out_cpu;
+		goto out_put_ddr;
  	}
  	kirkwood_freq_table[1].frequency = clk_get_rate(priv.ddr_clk) / 1000;
 
@@ -153,7 +153,7 @@ static int kirkwood_cpufreq_probe(struct platform_device *pdev)
  	err = clk_prepare_enable(priv.powersave_clk);
  	if (err) {
  		dev_err(priv.dev, "Unable to prepare powersave clk\n");
-		goto out_ddr;
+		goto out_put_powersave;
  	}
 
  	err = cpufreq_register_driver(&kirkwood_cpufreq_driver);
@@ -167,10 +167,16 @@ static int kirkwood_cpufreq_probe(struct platform_device *pdev)
 
  out_powersave:
  	clk_disable_unprepare(priv.powersave_clk);
+out_put_powersave:
+	clk_put(priv.powersave_clk);
  out_ddr:
  	clk_disable_unprepare(priv.ddr_clk);
+out_put_ddr:
+	clk_put(priv.ddr_clk);
  out_cpu:
  	clk_disable_unprepare(priv.cpu_clk);
+out_put_cpu:
+	clk_put(priv.cpu_clk);
  out_node:
  	of_node_put(np);
 
@@ -182,8 +188,11 @@ static void kirkwood_cpufreq_remove(struct platform_device *pdev)
  	cpufreq_unregister_driver(&kirkwood_cpufreq_driver);
 
  	clk_disable_unprepare(priv.powersave_clk);
+	clk_put(priv.powersave_clk);
  	clk_disable_unprepare(priv.ddr_clk);
+	clk_put(priv.ddr_clk);
  	clk_disable_unprepare(priv.cpu_clk);
+	clk_put(priv.cpu_clk);
  }
 
  static struct platform_driver kirkwood_cpufreq_platform_driver = {
Signature · 2 lines
Thx and BRs,
Zhongqiu Han
#5 lore raw

PATCH 3/3 cpufreq: s5pv210: Fix resource leaks on probe error paths

In reply to Suyuan Wang

From: Suyuan Wang <[email protected]>

s5pv210_cpufreq_probe() returns the result of cpufreq_register_driver() directly:

	register_reboot_notifier(&s5pv210_cpufreq_reboot_notifier);

	return cpufreq_register_driver(&s5pv210_driver);

If the registration fails, for example because s5pv210_cpu_init() fails, the resources taken by the probe itself are leaked: the two regulators and the clock and DMC register mappings. The reboot notifier also stays registered. (s5pv210_cpu_init() releases its own clocks on its error paths, and the cpufreq core tears down its own state.)

Also, when a DMC node has a bad alias, the code jumps to err_clk_base and skips unmapping DMC registers that were already mapped for an earlier node.

Unregister the reboot notifier and go through the existing unwind code when cpufreq_register_driver() fails, and jump to err_dmc on the alias error. The err_clk_base label has no users after this, so drop it.

Found with a static analysis tool and checked by hand.

Fixes 6d4ed0f46af6 ("cpufreq: s5pv210: Make the driver multiplatform aware")
Fixes e0e5b2b4f427 ("cpufreq: s5pv210: Defer probe if getting regulators fail")
---
 drivers/cpufreq/s5pv210-cpufreq.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
drivers/cpufreq/s5pv210-cpufreq.c +9 -3
index e64e84e1ee79..29b1c5d71a8b 100644
@@ -634,7 +634,7 @@ static int s5pv210_cpufreq_probe(struct platform_device *pdev)
 		if (id < 0 || id >= ARRAY_SIZE(dmc_base)) {
 			dev_err(dev, "failed to get alias of dmc node '%pOFn'\n", dmc);
 			result = id;
-			goto err_clk_base;
+			goto err_dmc;
 		}
 
 		dmc_base[id] = of_iomap(dmc, 0);
@@ -655,7 +655,14 @@ static int s5pv210_cpufreq_probe(struct platform_device *pdev)
 
 	register_reboot_notifier(&s5pv210_cpufreq_reboot_notifier);
 
-	return cpufreq_register_driver(&s5pv210_driver);
+	result = cpufreq_register_driver(&s5pv210_driver);
+	if (result)
+		goto err_reboot_notifier;
+
+	return 0;
+
+err_reboot_notifier:
+	unregister_reboot_notifier(&s5pv210_cpufreq_reboot_notifier);
 
 err_dmc:
 	for (id = 0; id < ARRAY_SIZE(dmc_base); ++id)
@@ -664,7 +671,6 @@ static int s5pv210_cpufreq_probe(struct platform_device *pdev)
 			dmc_base[id] = NULL;
 		}
 
-err_clk_base:
 	iounmap(clk_base);
 
 err_clock:
Signature · 1 line
2.34.1
#6 lore raw

Re: [PATCH 3/3] cpufreq: s5pv210: Fix resource leaks on probe error paths

Quoted 19 lines · On 11/22/2026 7:55 PM, Suyuan Wang via B4 Relay wrote:

From: Suyuan Wang <[email protected]>

s5pv210_cpufreq_probe() returns the result of cpufreq_register_driver() directly:

	register_reboot_notifier(&s5pv210_cpufreq_reboot_notifier);

	return cpufreq_register_driver(&s5pv210_driver);

If the registration fails, for example because s5pv210_cpu_init() fails, the resources taken by the probe itself are leaked: the two regulators and the clock and DMC register mappings. The reboot notifier also stays registered. (s5pv210_cpu_init() releases its own clocks on its error paths, and the cpufreq core tears down its own state.)

Also, when a DMC node has a bad alias, the code jumps to err_clk_base and skips unmapping DMC registers that were already mapped for an earlier node.

Quoted 8 lines

Unregister the reboot notifier and go through the existing unwind code when cpufreq_register_driver() fails, and jump to err_dmc on the alias error. The err_clk_base label has no users after this, so drop it.

Found with a static analysis tool and checked by hand.

Fixes 6d4ed0f46af6 ("cpufreq: s5pv210: Make the driver multiplatform aware")
Fixes e0e5b2b4f427 ("cpufreq: s5pv210: Defer probe if getting regulators fail")

e0e5b2b4f427 didn't introduce either leak; the alias path already leaked via "return id;" before it.

AFAIK, e8b4c1986efb added the two regulator_get() calls before the unchecked "return cpufreq_register_driver()", and fe7f1bcbff91 added the reboot notifier there, so those two leaks predate 6d4ed0f46af6, which only added the register mappings.

So the tags should be:

Fixes e8b4c1986efb ("[CPUFREQ] S5PV210: Add arm/int voltage control support")
Fixes fe7f1bcbff91 ("[CPUFREQ] S5PV210: Add reboot notifier to prevent system hang")
Fixes 6d4ed0f46af6 ("cpufreq: s5pv210: Make the driver multiplatform aware")

Could you please check and update the tags accordingly?

Quoted 45 lines
---
  drivers/cpufreq/s5pv210-cpufreq.c | 12 +++++++++---
  1 file changed, 9 insertions(+), 3 deletions(-)
drivers/cpufreq/s5pv210-cpufreq.c +9 -3
index e64e84e1ee79..29b1c5d71a8b 100644
@@ -634,7 +634,7 @@ static int s5pv210_cpufreq_probe(struct platform_device *pdev)
  		if (id < 0 || id >= ARRAY_SIZE(dmc_base)) {
  			dev_err(dev, "failed to get alias of dmc node '%pOFn'\n", dmc);
  			result = id;
-			goto err_clk_base;
+			goto err_dmc;
  		}
 
  		dmc_base[id] = of_iomap(dmc, 0);
@@ -655,7 +655,14 @@ static int s5pv210_cpufreq_probe(struct platform_device *pdev)
 
  	register_reboot_notifier(&s5pv210_cpufreq_reboot_notifier);
 
-	return cpufreq_register_driver(&s5pv210_driver);
+	result = cpufreq_register_driver(&s5pv210_driver);
+	if (result)
+		goto err_reboot_notifier;
+
+	return 0;
+
+err_reboot_notifier:
+	unregister_reboot_notifier(&s5pv210_cpufreq_reboot_notifier);
 
  err_dmc:
  	for (id = 0; id < ARRAY_SIZE(dmc_base); ++id)
@@ -664,7 +671,6 @@ static int s5pv210_cpufreq_probe(struct platform_device *pdev)
  			dmc_base[id] = NULL;
  		}
 
-err_clk_base:
  	iounmap(clk_base);
 
  err_clock:
Signature · 2 lines
Thx and BRs,
Zhongqiu Han