Skip to contentSkip to conversation

clocksource: timer-cadence-ttc: Fix resource leaks in error paths

PATCH In review Haotian Zhang 2 participants 2 messages Nov 26, 2025

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

PATCH 1/1 clocksource: timer-cadence-ttc: Fix resource leaks in error paths

The ttc_setup_clocksource() and ttc_setup_clockevent() fail to unregister the clock notifier in their error paths, potentially leading to a resource leak. Additionally, ttc_setup_clocksource() leaks the prepared clock reference upon failure.

Unregister the clock notifier and disable the clock in the error path of ttc_setup_clocksource(). Also, add the missing clock notifier unregistration in the error path of ttc_setup_clockevent() to ensure proper cleanup.

Fixes e932900a3279 ("arm: zynq: Use standard timer binding")
Fixes 91dc985c5e51 ("ARM: zynq: add clk binding support to the ttc")
---
 drivers/clocksource/timer-cadence-ttc.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
drivers/clocksource/timer-cadence-ttc.c +11 -1
index b8a1cf59b9d6..70384faad66d 100644
@@ -333,6 +333,7 @@ static int __init ttc_setup_clocksource(struct clk *clk, void __iomem *base,
 {
 	struct ttc_timer_clocksource *ttccs;
 	int err;
+	bool notifier_registered = false;
 
 	ttccs = kzalloc(sizeof(*ttccs), GFP_KERNEL);
 	if (!ttccs)
@@ -356,6 +357,8 @@ static int __init ttc_setup_clocksource(struct clk *clk, void __iomem *base,
 				    &ttccs->ttc.clk_rate_change_nb);
 	if (err)
 		pr_warn("Unable to register clock notifier.\n");
+	else
+		notifier_registered = true;
 
 	ttccs->ttc.base_addr = base;
 	ttccs->cs.name = "ttc_clocksource";
@@ -377,6 +380,10 @@ static int __init ttc_setup_clocksource(struct clk *clk, void __iomem *base,
 
 	err = clocksource_register_hz(&ttccs->cs, ttccs->ttc.freq / PRESCALE);
 	if (err) {
+		if (notifier_registered)
+			clk_notifier_unregister(ttccs->ttc.clk,
+					&ttccs->ttc.clk_rate_change_nb);
+		clk_disable_unprepare(ttccs->ttc.clk);
 		kfree(ttccs);
 		return err;
 	}
@@ -465,13 +472,16 @@ static int __init ttc_setup_clockevent(struct clk *clk,
 	err = request_irq(irq, ttc_clock_event_interrupt,
 			  IRQF_TIMER, ttcce->ce.name, ttcce);
 	if (err)
-		goto out_clk_unprepare;
+		goto out_clk_notifier_unregister;
 
 	clockevents_config_and_register(&ttcce->ce,
 			ttcce->ttc.freq / PRESCALE, 1, 0xfffe);
 
 	return 0;
 
+out_clk_notifier_unregister:
+	clk_notifier_unregister(ttcce->ttc.clk,
+			&ttcce->ttc.clk_rate_change_nb);
 out_clk_unprepare:
 	clk_disable_unprepare(ttcce->ttc.clk);
 out_kfree:
Signature · 1 line
2.50.1.windows.1
#1 lore raw
Quoted 4 lines

The ttc_setup_clocksource() and ttc_setup_clockevent() fail to unregister the clock notifier in their error paths, potentially leading to a resource leak. Additionally, ttc_setup_clocksource() leaks the prepared clock reference upon failure.

…

You may occasionally put more than 65 characters into text lines of such a change description.
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst?h=v6.18-rc7#n658

…

Quoted 1 line

+++ b/drivers/clocksource/timer-cadence-ttc.c

…

drivers/clocksource/timer-cadence-ttc.c static int __init ttc_setup_clocksource(struct clk *clk, void __iomem *base, · line 389

Quoted 11 lines
drivers/clocksource/timer-cadence-ttc.c +4 -0
@@ -377,6 +380,10 @@ static int __init ttc_setup_clocksource(struct clk *clk, void __iomem *base,
 
 	err = clocksource_register_hz(&ttccs->cs, ttccs->ttc.freq / PRESCALE);
 	if (err) {
+		if (notifier_registered)
+			clk_notifier_unregister(ttccs->ttc.clk,
+					&ttccs->ttc.clk_rate_change_nb);
+		clk_disable_unprepare(ttccs->ttc.clk);
 		kfree(ttccs);
 		return err;
 	}

How do you think about to avoid repeated kfree() calls in such a function implementation?

Quoted 1 line
(quoted diff) +0 -0
@@ -465,13 +472,16 @@ static int __init ttc_setup_clockevent(struct clk *clk,

…

drivers/clocksource/timer-cadence-ttc.c static int __init ttc_setup_clockevent(struct clk *clk, · line 484

Quoted 3 lines
drivers/clocksource/timer-cadence-ttc.c +3 -0
+out_clk_notifier_unregister:
+	clk_notifier_unregister(ttcce->ttc.clk,
+			&ttcce->ttc.clk_rate_change_nb);

…

You may specify this function call in a single line.

Regards,
Markus