cpufreq: Fix leaks on probe error paths in three drivers
Subsystems (2)
v2 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.
Changes in v2:
- 3/3: fix the Fixes tags. The regulator and reboot notifier leaks come from e8b4c1986efb and fe7f1bcbff91, and e0e5b2b4f427 did not add a leak (Zhongqiu Han). No code change.
- 1/3, 2/3: unchanged.
v1: https://lore.kernel.org/all/[email protected]/
---
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(-)
PATCH v2 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.
--- drivers/cpufreq/highbank-cpufreq.c | 1 + 1 file changed, 1 insertion(+)
drivers/cpufreq/highbank-cpufreq.c +1 -0
@@ -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
PATCH v2 2/3 cpufreq: kirkwood: Put clocks on error and in remove
In reply to Suyuan WangFrom: 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.
--- drivers/cpufreq/kirkwood-cpufreq.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-)
drivers/cpufreq/kirkwood-cpufreq.c +12 -3
@@ -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
PATCH v2 3/3 cpufreq: s5pv210: Fix resource leaks on probe error paths
In reply to Suyuan WangFrom: 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.
--- drivers/cpufreq/s5pv210-cpufreq.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-)
drivers/cpufreq/s5pv210-cpufreq.c +9 -3
@@ -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
Viresh Kumar
maintainerQuoted 21 lines · On 23-11-26, 05:21, Suyuan Wang via B4 Relay wrote:
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_v5for kirkwood); I don't have the hardware.Changes in v2:
- 3/3: fix the Fixes tags. The regulator and reboot notifier leaks come frome8b4c1986efbandfe7f1bcbff91, ande0e5b2b4f427did not add a leak (Zhongqiu Han). No code change.
- 1/3, 2/3: unchanged.v1: https://lore.kernel.org/all/[email protected]/
---
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
Quoted 4 lines
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(-)
While sending a new version, you need to add the tags you have received so far. Please resend with Reviewed by tags you have received.
Signature · 1 line
viresh