Skip to contentSkip to conversation

clocksource/drivers: Fix memory leak in ttc_setup_clockevent

PATCH In review Navid Emamdoost 4 participants 14 messages Oct 21, 2019 – Dec 17, 2019

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

PATCH 1/1 clocksource/drivers: Fix memory leak in ttc_setup_clockevent

In the impelementation of ttc_setup_clockevent() the allocated memory for ttcce should be released if clk_notifier_register() fails.

Fixes 70504f311d4b ("clocksource/drivers/cadence_ttc: Convert init function to return error")
---
 drivers/clocksource/timer-cadence-ttc.c | 1 +
 1 file changed, 1 insertion(+)
drivers/clocksource/timer-cadence-ttc.c +1 -0
index 88fe2e9ba9a3..b40fc6581389 100644
@@ -424,6 +424,7 @@ static int __init ttc_setup_clockevent(struct clk *clk,
 				    &ttcce->ttc.clk_rate_change_nb);
 	if (err) {
 		pr_warn("Unable to register clock notifier.\n");
+		kfree(ttcce);
 		return err;
 	}
 
Signature · 1 line
2.17.1
#1 lore raw
Quoted 2 lines

In the impelementation of ttc_setup_clockevent() the allocated memory for ttcce should be released if clk_notifier_register() fails.

* Please avoid the copying of typos from previous change descriptions.

* Under which circumstances will an “imperative mood” matter for you here?
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst?id=7d194c2100ad2a6dded545887d02754948ca5241#n151

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

Quoted 8 lines

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

drivers/clocksource/timer-cadence-ttc.c +1 -0
@@ -424,6 +424,7 @@ static int __init ttc_setup_clockevent(struct clk *clk,
 				    &ttcce->ttc.clk_rate_change_nb);
 	if (err) {
 		pr_warn("Unable to register clock notifier.\n");
+		kfree(ttcce);
 		return err;
 	}

This addition looks correct.
But I would prefer to move such exception handling code to the end of this function implementation so that duplicate source code will be reduced.
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/coding-style.rst?id=7d194c2100ad2a6dded545887d02754948ca5241#n450

Regards,
Markus

#2 lore raw
Quoted 9 lines · Markus Elfring
Quoted 2 lines

In the impelementation of ttc_setup_clockevent() the allocated memory for ttcce should be released if clk_notifier_register() fails.

* Please avoid the copying of typos from previous change descriptions.

* Under which circumstances will an “imperative mood” matter for you here?
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst?id}194c2100ad2a6dded545887d02754948ca5241#n151

Quoted 13 lines

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

Quoted 8 lines

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

drivers/clocksource/timer-cadence-ttc.c +1 -0
@@ -424,6 +424,7 @@ static int __init ttc_setup_clockevent(struct clk *clk,
 				    &ttcce->ttc.clk_rate_change_nb);
 	if (err) {
 		pr_warn("Unable to register clock notifier.\n");
+		kfree(ttcce);
 		return err;
 	}

This addition looks correct.
But I would prefer to move such exception handling code to the end of this function implementation so that duplicate source code will be reduced.
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/coding-style.rst?id}194c2100ad2a6dded545887d02754948ca5241#n450

Just a note. Maybe you should also consider to fix this error path in ttc_setup_clocksource() when notifier also can fail that there is no need to continue with code execution.

Thanks,
Michal

#3 lore raw

PATCH v2 1/1 clocksource/drivers: Fix memory leak in ttc_setup_clockevent

In the implementation of ttc_setup_clockevent() release the allocated memory for ttcce if clk_notifier_register() fails.

Fixes 70504f311d4b ("clocksource/drivers/cadence_ttc: Convert init function to return error")

---
Changes in v2:
- Added goto label for error handling
- Update description and fix typo

 drivers/clocksource/timer-cadence-ttc.c | 19 ++++++++++---------
 1 file changed, 10 insertions(+), 9 deletions(-)
drivers/clocksource/timer-cadence-ttc.c +10 -9
index 88fe2e9ba9a3..0caacbc67102 100644
@@ -411,10 +411,8 @@ static int __init ttc_setup_clockevent(struct clk *clk,
 	ttcce->ttc.clk = clk;
 
 	err = clk_prepare_enable(ttcce->ttc.clk);
-	if (err) {
-		kfree(ttcce);
-		return err;
-	}
+	if (err)
+		goto release_ttcce;
 
 	ttcce->ttc.clk_rate_change_nb.notifier_call =
 		ttc_rate_change_clockevent_cb;
@@ -424,7 +422,7 @@ static int __init ttc_setup_clockevent(struct clk *clk,
 				    &ttcce->ttc.clk_rate_change_nb);
 	if (err) {
 		pr_warn("Unable to register clock notifier.\n");
-		return err;
+		goto release_ttcce;
 	}
 
 	ttcce->ttc.freq = clk_get_rate(ttcce->ttc.clk);
@@ -453,15 +451,18 @@ 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) {
-		kfree(ttcce);
-		return err;
-	}
+	if (err)
+		goto release_ttcce;
 
 	clockevents_config_and_register(&ttcce->ce,
 			ttcce->ttc.freq / PRESCALE, 1, 0xfffe);
 
 	return 0;
+
+release_ttcce:
+
+	kfree(ttcce);
+	return err;
 }
 
 /**
Signature · 1 line
2.17.1
Acked-by on v2 1/1
#4 lore raw
Quoted 53 lines · Navid Emamdoost

In the implementation of ttc_setup_clockevent() release the allocated memory for ttcce if clk_notifier_register() fails.

Fixes 70504f311d4b ("clocksource/drivers/cadence_ttc: Convert init function to return error")

---
Changes in v2:
- Added goto label for error handling
- Update description and fix typo

 drivers/clocksource/timer-cadence-ttc.c | 19 ++++++++++---------
 1 file changed, 10 insertions(+), 9 deletions(-)
drivers/clocksource/timer-cadence-ttc.c +5 -9
index 88fe2e9ba9a3..0caacbc67102 100644
@@ -411,10 +411,8 @@ static int __init ttc_setup_clockevent(struct clk *clk,
 	ttcce->ttc.clk = clk;
 
 	err = clk_prepare_enable(ttcce->ttc.clk);
-	if (err) {
-		kfree(ttcce);
-		return err;
-	}
+	if (err)
+		goto release_ttcce;
 
 	ttcce->ttc.clk_rate_change_nb.notifier_call =
 		ttc_rate_change_clockevent_cb;
@@ -424,7 +422,7 @@ static int __init ttc_setup_clockevent(struct clk *clk,
 				    &ttcce->ttc.clk_rate_change_nb);
 	if (err) {
 		pr_warn("Unable to register clock notifier.\n");
-		return err;
+		goto release_ttcce;
 	}
 
 	ttcce->ttc.freq = clk_get_rate(ttcce->ttc.clk);
@@ -453,15 +451,18 @@ 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) {
-		kfree(ttcce);
-		return err;
-	}
+	if (err)
+		goto release_ttcce;
 
 	clockevents_config_and_register(&ttcce->ce,
 			ttcce->ttc.freq / PRESCALE, 1, 0xfffe);

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

Quoted 10 lines
drivers/clocksource/timer-cadence-ttc.c +5 -0
 
 	return 0;
+
+release_ttcce:
+
+	kfree(ttcce);
+	return err;
 }
 
 /**

Thanks,
Michal

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

#5 lore raw
In reply to Navid Emamdoost
Quoted 2 lines

In the implementation of ttc_setup_clockevent() release the allocated memory for ttcce if clk_notifier_register() fails.

I got other wording preferences. Thus I imagine that such a change description can still be improved another bit.
Would you like to express the addition of a jump target (according to the Linux coding style) for the completion of desired exception handling in a different way?

…

Quoted 1 line

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

…

Quoted 1 line
(quoted diff) +0 -0
@@ -453,15 +451,18 @@ 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 464

Quoted 3 lines
drivers/clocksource/timer-cadence-ttc.c +3 -0
+release_ttcce:
+
+	kfree(ttcce);

…

I would prefer that a blank line will not be added directly after such a label.

Regards,
Markus

#6 lore raw

PATCH 1/1 clocksource/drivers: Fix error handling in ttc_setup_clocksource

In reply to Michal Simek

In the implementation of ttc_setup_clocksource() when clk_notifier_register() fails the execution should go to error handling. Additionally, to avoid memory leak the allocated memory for ttccs should be released, too. So, goto error handling to release the memory and return.

Fixes e932900a3279 ("arm: zynq: Use standard timer binding")
---
 drivers/clocksource/timer-cadence-ttc.c | 20 +++++++++++---------
 1 file changed, 11 insertions(+), 9 deletions(-)
drivers/clocksource/timer-cadence-ttc.c +11 -9
index 88fe2e9ba9a3..035e16bc17d3 100644
@@ -328,10 +328,8 @@ static int __init ttc_setup_clocksource(struct clk *clk, void __iomem *base,
 	ttccs->ttc.clk = clk;
 
 	err = clk_prepare_enable(ttccs->ttc.clk);
-	if (err) {
-		kfree(ttccs);
-		return err;
-	}
+	if (err)
+		goto release_ttccs;
 
 	ttccs->ttc.freq = clk_get_rate(ttccs->ttc.clk);
 
@@ -341,8 +339,10 @@ static int __init ttc_setup_clocksource(struct clk *clk, void __iomem *base,
 
 	err = clk_notifier_register(ttccs->ttc.clk,
 				    &ttccs->ttc.clk_rate_change_nb);
-	if (err)
+	if (err) {
 		pr_warn("Unable to register clock notifier.\n");
+		goto release_ttccs;
+	}
 
 	ttccs->ttc.base_addr = base;
 	ttccs->cs.name = "ttc_clocksource";
@@ -363,16 +363,18 @@ static int __init ttc_setup_clocksource(struct clk *clk, void __iomem *base,
 		     ttccs->ttc.base_addr + TTC_CNT_CNTRL_OFFSET);
 
 	err = clocksource_register_hz(&ttccs->cs, ttccs->ttc.freq / PRESCALE);
-	if (err) {
-		kfree(ttccs);
-		return err;
-	}
+	if (err)
+		goto release_ttccs;
 
 	ttc_sched_clock_val_reg = base + TTC_COUNT_VAL_OFFSET;
 	sched_clock_register(ttc_sched_clock_read, timer_width,
 			     ttccs->ttc.freq / PRESCALE);
 
 	return 0;
+
+release_ttccs:
+	kfree(ttccs);
+	return err;
 }
 
 static int ttc_rate_change_clockevent_cb(struct notifier_block *nb,
Signature · 1 line
2.17.1
#7 lore raw

Re: [PATCH] clocksource/drivers: Fix error handling in ttc_setup_clocksource

Quoted 4 lines

In the implementation of ttc_setup_clocksource() when clk_notifier_register() fails the execution should go to error handling. Additionally, to avoid memory leak the allocated memory for ttccs should be released, too.

I got other wording preferences. Thus I imagine that such a change description can still be improved another bit.

How do you think about to omit the word “should” for describing the previous software situation?

Quoted 1 line

So, goto error handling to release the memory and return.

Would you like to express the addition of a jump target (according to the Linux coding style) for the completion of desired exception handling in a different way?

Regards,
Markus

#8 lore raw

Re: [PATCH] clocksource/drivers: Fix error handling in ttc_setup_clocksource

In reply to Navid Emamdoost
Quoted 1 line
Fixes e932900a3279 ("arm: zynq: Use standard timer binding")

How do you think about to add the tag “Reported-by” for Michal Simek?
https://lore.kernel.org/linux-arm-kernel/[email protected]/
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst?id=3b7c59a1950c75f2c0152e5a9cd77675b09233d6#n584

Regards,
Markus

#9 lore raw

Re: [PATCH] clocksource/drivers: Fix error handling in ttc_setup_clocksource

In reply to Navid Emamdoost
Quoted 1 line
Fixes e932900a3279 ("arm: zynq: Use standard timer binding")

I find the commit 70504f311d4bd5b6a6d494f50c5ab0bd30fdf75c ("clocksource/drivers/cadence_ttc: Convert init function to return error" from 2016-06-28) also interesting (besides the contribution from 2013-04-04) for your software update.

Regards,
Markus

#10 lore raw

Re: [PATCH] clocksource/drivers: Fix error handling in ttc_setup_clocksource

In reply to Navid Emamdoost

Would you review this patch, please?

Quoted 68 lines · Navid Emamdoost

In the implementation of ttc_setup_clocksource() when clk_notifier_register() fails the execution should go to error handling. Additionally, to avoid memory leak the allocated memory for ttccs should be released, too. So, goto error handling to release the memory and return.

Fixes e932900a3279 ("arm: zynq: Use standard timer binding")
---
 drivers/clocksource/timer-cadence-ttc.c | 20 +++++++++++---------
 1 file changed, 11 insertions(+), 9 deletions(-)
drivers/clocksource/timer-cadence-ttc.c +11 -10
index 88fe2e9ba9a3..035e16bc17d3 100644
@@ -328,10 +328,8 @@ static int __init ttc_setup_clocksource(struct clk *clk, void __iomem *base,
        ttccs->ttc.clk = clk;
 
        err = clk_prepare_enable(ttccs->ttc.clk);
-       if (err) {
-               kfree(ttccs);
-               return err;
-       }
+       if (err)
+               goto release_ttccs;
 
        ttccs->ttc.freq = clk_get_rate(ttccs->ttc.clk);
 
@@ -341,8 +339,10 @@ static int __init ttc_setup_clocksource(struct clk *clk, void __iomem *base,
 
        err = clk_notifier_register(ttccs->ttc.clk,
                                    &ttccs->ttc.clk_rate_change_nb);
-       if (err)
+       if (err) {
                pr_warn("Unable to register clock notifier.\n");
+               goto release_ttccs;
+       }
 
        ttccs->ttc.base_addr = base;
        ttccs->cs.name = "ttc_clocksource";
@@ -363,16 +363,18 @@ static int __init ttc_setup_clocksource(struct clk *clk, void __iomem *base,
                     ttccs->ttc.base_addr + TTC_CNT_CNTRL_OFFSET);
 
        err = clocksource_register_hz(&ttccs->cs, ttccs->ttc.freq / PRESCALE);
-       if (err) {
-               kfree(ttccs);
-               return err;
-       }
+       if (err)
+               goto release_ttccs;
 
        ttc_sched_clock_val_reg = base + TTC_COUNT_VAL_OFFSET;
        sched_clock_register(ttc_sched_clock_read, timer_width,
                             ttccs->ttc.freq / PRESCALE);
 
        return 0;
+
+release_ttccs:
+       kfree(ttccs);
+       return err;
 }
 
 static int ttc_rate_change_clockevent_cb(struct notifier_block *nb,
--

2.17.1

Signature · 1 line
Navid.
#11 lore raw

Re: [PATCH] clocksource/drivers: Fix error handling in ttc_setup_clocksource

Hi Navid,

Quoted 1 line · Navid Emamdoost

Would you review this patch, please?

please fix the version number, send without in-reply-to.

Do the same for the other patch:

clocksource/drivers: Fix memory leak in ttc_setup_clockevent

It is a bit confuse what patch is ok or not.

Thanks

-- Daniel

Quoted 72 lines
Quoted 68 lines · Navid Emamdoost

In the implementation of ttc_setup_clocksource() when clk_notifier_register() fails the execution should go to error handling. Additionally, to avoid memory leak the allocated memory for ttccs should be released, too. So, goto error handling to release the memory and return.

Fixes e932900a3279 ("arm: zynq: Use standard timer binding")
---
 drivers/clocksource/timer-cadence-ttc.c | 20 +++++++++++---------
 1 file changed, 11 insertions(+), 9 deletions(-)
drivers/clocksource/timer-cadence-ttc.c +11 -10
index 88fe2e9ba9a3..035e16bc17d3 100644
@@ -328,10 +328,8 @@ static int __init ttc_setup_clocksource(struct clk *clk, void __iomem *base,
        ttccs->ttc.clk = clk;
 
        err = clk_prepare_enable(ttccs->ttc.clk);
-       if (err) {
-               kfree(ttccs);
-               return err;
-       }
+       if (err)
+               goto release_ttccs;
 
        ttccs->ttc.freq = clk_get_rate(ttccs->ttc.clk);
 
@@ -341,8 +339,10 @@ static int __init ttc_setup_clocksource(struct clk *clk, void __iomem *base,
 
        err = clk_notifier_register(ttccs->ttc.clk,
                                    &ttccs->ttc.clk_rate_change_nb);
-       if (err)
+       if (err) {
                pr_warn("Unable to register clock notifier.\n");
+               goto release_ttccs;
+       }
 
        ttccs->ttc.base_addr = base;
        ttccs->cs.name = "ttc_clocksource";
@@ -363,16 +363,18 @@ static int __init ttc_setup_clocksource(struct clk *clk, void __iomem *base,
                     ttccs->ttc.base_addr + TTC_CNT_CNTRL_OFFSET);
 
        err = clocksource_register_hz(&ttccs->cs, ttccs->ttc.freq / PRESCALE);
-       if (err) {
-               kfree(ttccs);
-               return err;
-       }
+       if (err)
+               goto release_ttccs;
 
        ttc_sched_clock_val_reg = base + TTC_COUNT_VAL_OFFSET;
        sched_clock_register(ttc_sched_clock_read, timer_width,
                             ttccs->ttc.freq / PRESCALE);
 
        return 0;
+
+release_ttccs:
+       kfree(ttccs);
+       return err;
 }
 
 static int ttc_rate_change_clockevent_cb(struct notifier_block *nb,
--

2.17.1

Signature · 5 lines
 <http://www.linaro.org/> Linaro.org │ Open source software for ARM SoCs

Follow Linaro:  <http://www.facebook.com/pages/Linaro> Facebook |
<http://twitter.com/#!/linaroorg> Twitter |
<http://www.linaro.org/linaro-blog/> Blog
#12 lore raw

Re: [PATCH] clocksource/drivers: Fix error handling in ttc_setup_clocksource

Quoted 6 lines · Daniel Lezcano

Hi Navid,

Quoted 1 line · Navid Emamdoost

Would you review this patch, please?

Quoted 7 lines

please fix the version number, send without in-reply-to.

Do the same for the other patch:

clocksource/drivers: Fix memory leak in ttc_setup_clockevent

It is a bit confuse what patch is ok or not.

+1 on this. And patch looks good to me but as I said before the same changes should be done also in ttc_setup_clockevent. It means v2 with these two things together is the best way to go.

Thanks,
Michal

#13 lore raw
In reply to Michal Simek

Thanks for the feedback, I updated this patch and sent v2. Also, I submitted a patch to fix the error handling path in ttc_setup_clocksource(). Here is the link to it:
https://lore.kernel.org/patchwork/patch/1143242/

Quoted 31 lines · Michal Simek
Quoted 9 lines · Markus Elfring
Quoted 2 lines

In the impelementation of ttc_setup_clockevent() the allocated memory for ttcce should be released if clk_notifier_register() fails.

* Please avoid the copying of typos from previous change descriptions.

* Under which circumstances will an “imperative mood” matter for you here?
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst?id=7d194c2100ad2a6dded545887d02754948ca5241#n151

Quoted 13 lines

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

Quoted 8 lines

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

drivers/clocksource/timer-cadence-ttc.c +1 -0
@@ -424,6 +424,7 @@ static int __init ttc_setup_clockevent(struct clk *clk,
                                 &ttcce->ttc.clk_rate_change_nb);
     if (err) {
             pr_warn("Unable to register clock notifier.\n");
+            kfree(ttcce);
             return err;
     }

This addition looks correct.
But I would prefer to move such exception handling code to the end of this function implementation so that duplicate source code will be reduced.
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/coding-style.rst?id=7d194c2100ad2a6dded545887d02754948ca5241#n450

Just a note. Maybe you should also consider to fix this error path in ttc_setup_clocksource() when notifier also can fail that there is no need to continue with code execution.

Thanks,
Michal

Signature · 1 line
Navid.