linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] clocksource: cadence-ttc: fix memory leak in ttc_timer_probe
@ 2023-04-25  6:56 Feng Mingxi
  2023-04-26 14:49 ` Michal Simek
  2023-06-26  9:11 ` [tip: timers/core] clocksource/drivers/cadence-ttc: Fix " tip-bot2 for Feng Mingxi
  0 siblings, 2 replies; 3+ messages in thread
From: Feng Mingxi @ 2023-04-25  6:56 UTC (permalink / raw)
  To: Michal Simek, Daniel Lezcano, Thomas Gleixner, Sören Brinkmann
  Cc: hust-os-kernel-patches, Feng Mingxi, Dongliang Mu,
	linux-arm-kernel, linux-kernel

Smatch reports:
drivers/clocksource/timer-cadence-ttc.c:529 ttc_timer_probe()
warn: 'timer_baseaddr' from of_iomap() not released on lines: 498,508,516.

timer_baseaddr may have the problem of not being released after use,
I replaced it with the devm_of_iomap() function and added the clk_put()
function to cleanup the "clk_ce" and "clk_cs".

Fixes: e932900a3279 ("arm: zynq: Use standard timer binding")
Fixes: 70504f311d4b ("clocksource/drivers/cadence_ttc: Convert init function to return error")
Signed-off-by: Feng Mingxi <m202271825@hust.edu.cn>
Reviewed-by: Dongliang Mu <dzm91@hust.edu.cn>
---
The issue is discovered by static analysis, and the patch is not tested yet.
---
 drivers/clocksource/timer-cadence-ttc.c | 19 +++++++++++++------
 1 file changed, 13 insertions(+), 6 deletions(-)

diff --git a/drivers/clocksource/timer-cadence-ttc.c b/drivers/clocksource/timer-cadence-ttc.c
index 4efd0cf3b602..0d52e28fea4d 100644
--- a/drivers/clocksource/timer-cadence-ttc.c
+++ b/drivers/clocksource/timer-cadence-ttc.c
@@ -486,10 +486,10 @@ static int __init ttc_timer_probe(struct platform_device *pdev)
 	 * and use it. Note that the event timer uses the interrupt and it's the
 	 * 2nd TTC hence the irq_of_parse_and_map(,1)
 	 */
-	timer_baseaddr = of_iomap(timer, 0);
-	if (!timer_baseaddr) {
+	timer_baseaddr = devm_of_iomap(&pdev->dev, timer, 0, NULL);
+	if (IS_ERR(timer_baseaddr)) {
 		pr_err("ERROR: invalid timer base address\n");
-		return -ENXIO;
+		return PTR_ERR(timer_baseaddr);
 	}
 
 	irq = irq_of_parse_and_map(timer, 1);
@@ -513,20 +513,27 @@ static int __init ttc_timer_probe(struct platform_device *pdev)
 	clk_ce = of_clk_get(timer, clksel);
 	if (IS_ERR(clk_ce)) {
 		pr_err("ERROR: timer input clock not found\n");
-		return PTR_ERR(clk_ce);
+		ret = PTR_ERR(clk_ce);
+		goto put_clk_cs;
 	}
 
 	ret = ttc_setup_clocksource(clk_cs, timer_baseaddr, timer_width);
 	if (ret)
-		return ret;
+		goto put_clk_ce;
 
 	ret = ttc_setup_clockevent(clk_ce, timer_baseaddr + 4, irq);
 	if (ret)
-		return ret;
+		goto put_clk_ce;
 
 	pr_info("%pOFn #0 at %p, irq=%d\n", timer, timer_baseaddr, irq);
 
 	return 0;
+
+put_clk_ce:
+	clk_put(clk_ce);
+put_clk_cs:
+	clk_put(clk_cs);
+	return ret;
 }
 
 static const struct of_device_id ttc_timer_of_match[] = {
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] clocksource: cadence-ttc: fix memory leak in ttc_timer_probe
  2023-04-25  6:56 [PATCH] clocksource: cadence-ttc: fix memory leak in ttc_timer_probe Feng Mingxi
@ 2023-04-26 14:49 ` Michal Simek
  2023-06-26  9:11 ` [tip: timers/core] clocksource/drivers/cadence-ttc: Fix " tip-bot2 for Feng Mingxi
  1 sibling, 0 replies; 3+ messages in thread
From: Michal Simek @ 2023-04-26 14:49 UTC (permalink / raw)
  To: Feng Mingxi, Michal Simek, Daniel Lezcano, Thomas Gleixner,
	Sören Brinkmann
  Cc: hust-os-kernel-patches, Dongliang Mu, linux-arm-kernel, linux-kernel



On 4/25/23 08:56, Feng Mingxi wrote:
> 
> Smatch reports:
> drivers/clocksource/timer-cadence-ttc.c:529 ttc_timer_probe()
> warn: 'timer_baseaddr' from of_iomap() not released on lines: 498,508,516.
> 
> timer_baseaddr may have the problem of not being released after use,
> I replaced it with the devm_of_iomap() function and added the clk_put()
> function to cleanup the "clk_ce" and "clk_cs".
> 
> Fixes: e932900a3279 ("arm: zynq: Use standard timer binding")
> Fixes: 70504f311d4b ("clocksource/drivers/cadence_ttc: Convert init function to return error")
> Signed-off-by: Feng Mingxi <m202271825@hust.edu.cn>
> Reviewed-by: Dongliang Mu <dzm91@hust.edu.cn>
> ---
> The issue is discovered by static analysis, and the patch is not tested yet.
> ---
>   drivers/clocksource/timer-cadence-ttc.c | 19 +++++++++++++------
>   1 file changed, 13 insertions(+), 6 deletions(-)
> 
> diff --git a/drivers/clocksource/timer-cadence-ttc.c b/drivers/clocksource/timer-cadence-ttc.c
> index 4efd0cf3b602..0d52e28fea4d 100644
> --- a/drivers/clocksource/timer-cadence-ttc.c
> +++ b/drivers/clocksource/timer-cadence-ttc.c
> @@ -486,10 +486,10 @@ static int __init ttc_timer_probe(struct platform_device *pdev)
>           * and use it. Note that the event timer uses the interrupt and it's the
>           * 2nd TTC hence the irq_of_parse_and_map(,1)
>           */
> -       timer_baseaddr = of_iomap(timer, 0);
> -       if (!timer_baseaddr) {
> +       timer_baseaddr = devm_of_iomap(&pdev->dev, timer, 0, NULL);
> +       if (IS_ERR(timer_baseaddr)) {
>                  pr_err("ERROR: invalid timer base address\n");
> -               return -ENXIO;
> +               return PTR_ERR(timer_baseaddr);
>          }
> 
>          irq = irq_of_parse_and_map(timer, 1);
> @@ -513,20 +513,27 @@ static int __init ttc_timer_probe(struct platform_device *pdev)
>          clk_ce = of_clk_get(timer, clksel);
>          if (IS_ERR(clk_ce)) {
>                  pr_err("ERROR: timer input clock not found\n");
> -               return PTR_ERR(clk_ce);
> +               ret = PTR_ERR(clk_ce);
> +               goto put_clk_cs;
>          }
> 
>          ret = ttc_setup_clocksource(clk_cs, timer_baseaddr, timer_width);
>          if (ret)
> -               return ret;
> +               goto put_clk_ce;
> 
>          ret = ttc_setup_clockevent(clk_ce, timer_baseaddr + 4, irq);
>          if (ret)
> -               return ret;
> +               goto put_clk_ce;
> 
>          pr_info("%pOFn #0 at %p, irq=%d\n", timer, timer_baseaddr, irq);
> 
>          return 0;
> +
> +put_clk_ce:
> +       clk_put(clk_ce);
> +put_clk_cs:
> +       clk_put(clk_cs);
> +       return ret;
>   }
> 
>   static const struct of_device_id ttc_timer_of_match[] = {
> --
> 2.34.1
> 

Acked-by: Michal Simek <michal.simek@amd.com>

Thanks,
Michal

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [tip: timers/core] clocksource/drivers/cadence-ttc: Fix memory leak in ttc_timer_probe
  2023-04-25  6:56 [PATCH] clocksource: cadence-ttc: fix memory leak in ttc_timer_probe Feng Mingxi
  2023-04-26 14:49 ` Michal Simek
@ 2023-06-26  9:11 ` tip-bot2 for Feng Mingxi
  1 sibling, 0 replies; 3+ messages in thread
From: tip-bot2 for Feng Mingxi @ 2023-06-26  9:11 UTC (permalink / raw)
  To: linux-tip-commits
  Cc: Feng Mingxi, Dongliang Mu, Michal Simek, Daniel Lezcano, x86,
	linux-kernel

The following commit has been merged into the timers/core branch of tip:

Commit-ID:     8b5bf64c89c7100c921bd807ba39b2eb003061ab
Gitweb:        https://git.kernel.org/tip/8b5bf64c89c7100c921bd807ba39b2eb003061ab
Author:        Feng Mingxi <m202271825@hust.edu.cn>
AuthorDate:    Tue, 25 Apr 2023 06:56:11 
Committer:     Daniel Lezcano <daniel.lezcano@linaro.org>
CommitterDate: Fri, 23 Jun 2023 09:33:44 +02:00

clocksource/drivers/cadence-ttc: Fix memory leak in ttc_timer_probe

Smatch reports:
drivers/clocksource/timer-cadence-ttc.c:529 ttc_timer_probe()
warn: 'timer_baseaddr' from of_iomap() not released on lines: 498,508,516.

timer_baseaddr may have the problem of not being released after use,
I replaced it with the devm_of_iomap() function and added the clk_put()
function to cleanup the "clk_ce" and "clk_cs".

Fixes: e932900a3279 ("arm: zynq: Use standard timer binding")
Fixes: 70504f311d4b ("clocksource/drivers/cadence_ttc: Convert init function to return error")
Signed-off-by: Feng Mingxi <m202271825@hust.edu.cn>
Reviewed-by: Dongliang Mu <dzm91@hust.edu.cn>
Acked-by: Michal Simek <michal.simek@amd.com>
Signed-off-by: Daniel Lezcano <daniel.lezcano@linaro.org>
Link: https://lore.kernel.org/r/20230425065611.702917-1-m202271825@hust.edu.cn
---
 drivers/clocksource/timer-cadence-ttc.c | 19 +++++++++++++------
 1 file changed, 13 insertions(+), 6 deletions(-)

diff --git a/drivers/clocksource/timer-cadence-ttc.c b/drivers/clocksource/timer-cadence-ttc.c
index 4efd0cf..0d52e28 100644
--- a/drivers/clocksource/timer-cadence-ttc.c
+++ b/drivers/clocksource/timer-cadence-ttc.c
@@ -486,10 +486,10 @@ static int __init ttc_timer_probe(struct platform_device *pdev)
 	 * and use it. Note that the event timer uses the interrupt and it's the
 	 * 2nd TTC hence the irq_of_parse_and_map(,1)
 	 */
-	timer_baseaddr = of_iomap(timer, 0);
-	if (!timer_baseaddr) {
+	timer_baseaddr = devm_of_iomap(&pdev->dev, timer, 0, NULL);
+	if (IS_ERR(timer_baseaddr)) {
 		pr_err("ERROR: invalid timer base address\n");
-		return -ENXIO;
+		return PTR_ERR(timer_baseaddr);
 	}
 
 	irq = irq_of_parse_and_map(timer, 1);
@@ -513,20 +513,27 @@ static int __init ttc_timer_probe(struct platform_device *pdev)
 	clk_ce = of_clk_get(timer, clksel);
 	if (IS_ERR(clk_ce)) {
 		pr_err("ERROR: timer input clock not found\n");
-		return PTR_ERR(clk_ce);
+		ret = PTR_ERR(clk_ce);
+		goto put_clk_cs;
 	}
 
 	ret = ttc_setup_clocksource(clk_cs, timer_baseaddr, timer_width);
 	if (ret)
-		return ret;
+		goto put_clk_ce;
 
 	ret = ttc_setup_clockevent(clk_ce, timer_baseaddr + 4, irq);
 	if (ret)
-		return ret;
+		goto put_clk_ce;
 
 	pr_info("%pOFn #0 at %p, irq=%d\n", timer, timer_baseaddr, irq);
 
 	return 0;
+
+put_clk_ce:
+	clk_put(clk_ce);
+put_clk_cs:
+	clk_put(clk_cs);
+	return ret;
 }
 
 static const struct of_device_id ttc_timer_of_match[] = {

^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2023-06-26  9:13 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-04-25  6:56 [PATCH] clocksource: cadence-ttc: fix memory leak in ttc_timer_probe Feng Mingxi
2023-04-26 14:49 ` Michal Simek
2023-06-26  9:11 ` [tip: timers/core] clocksource/drivers/cadence-ttc: Fix " tip-bot2 for Feng Mingxi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).