linux-serial.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [Patch] serial: 8250_bcm7271: fix leak in `brcmuart_probe`
@ 2023-04-24 12:51 XuDong Liu
  2023-04-24 14:16 ` Christophe JAILLET
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: XuDong Liu @ 2023-04-24 12:51 UTC (permalink / raw)
  To: Al Cooper, Broadcom internal kernel review list,
	Greg Kroah-Hartman, Jiri Slaby, Doug Berger, Florian Fainelli
  Cc: XuDong Liu, Dongliang Mu, linux-serial, linux-kernel

Smatch reports:
drivers/tty/serial/8250/8250_bcm7271.c:1120 brcmuart_probe() warn: 
'baud_mux_clk' from clk_prepare_enable() not released on lines: 1032.

In the function brcmuart_probe(), baud_mux_clk was not correctly released
in subsequent error handling, which may cause memory leaks.

To fix this issue, an error handling branch, err_clk_put, is added to
release the variable using clk_put(), and an err_disable branch is added
to meet the requirement of balancing clk_disable and clk_enable calls.

Fixes: 15ac1122fd6d ("serial: 8250_bcm7271: Fix arbitration handling")
Signed-off-by: XuDong Liu <m202071377@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/tty/serial/8250/8250_bcm7271.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/drivers/tty/serial/8250/8250_bcm7271.c b/drivers/tty/serial/8250/8250_bcm7271.c
index f801b1f5b46c..b1670558868b 100644
--- a/drivers/tty/serial/8250/8250_bcm7271.c
+++ b/drivers/tty/serial/8250/8250_bcm7271.c
@@ -1023,7 +1023,7 @@ static int brcmuart_probe(struct platform_device *pdev)
 		dev_dbg(dev, "BAUD MUX clock found\n");
 		ret = clk_prepare_enable(baud_mux_clk);
 		if (ret)
-			goto release_dma;
+			goto err_clk_put;
 		priv->baud_mux_clk = baud_mux_clk;
 		init_real_clk_rates(dev, priv);
 		clk_rate = priv->default_mux_rate;
@@ -1032,7 +1032,7 @@ static int brcmuart_probe(struct platform_device *pdev)
 	if (clk_rate == 0) {
 		dev_err(dev, "clock-frequency or clk not defined\n");
 		ret = -EINVAL;
-		goto release_dma;
+		goto err_clk_disable;
 	}
 
 	dev_dbg(dev, "DMA is %senabled\n", priv->dma_enabled ? "" : "not ");
@@ -1119,6 +1119,10 @@ static int brcmuart_probe(struct platform_device *pdev)
 	serial8250_unregister_port(priv->line);
 err:
 	brcmuart_free_bufs(dev, priv);
+err_clk_disable:
+	clk_disable_unprepare(baud_mux_clk);
+err_clk_put:
+	clk_put(baud_mux_clk);
 release_dma:
 	if (priv->dma_enabled)
 		brcmuart_arbitration(priv, 0);
-- 
2.34.1


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

* Re: [Patch] serial: 8250_bcm7271: fix leak in `brcmuart_probe`
  2023-04-24 12:51 [Patch] serial: 8250_bcm7271: fix leak in `brcmuart_probe` XuDong Liu
@ 2023-04-24 14:16 ` Christophe JAILLET
  2023-04-27 18:29 ` Doug Berger
  2023-05-13 10:25 ` Greg Kroah-Hartman
  2 siblings, 0 replies; 4+ messages in thread
From: Christophe JAILLET @ 2023-04-24 14:16 UTC (permalink / raw)
  To: XuDong Liu, Al Cooper, Broadcom internal kernel review list,
	Greg Kroah-Hartman, Jiri Slaby, Doug Berger, Florian Fainelli
  Cc: Dongliang Mu, linux-serial, linux-kernel

Le 24/04/2023 à 14:51, XuDong Liu a écrit :
> Smatch reports:
> drivers/tty/serial/8250/8250_bcm7271.c:1120 brcmuart_probe() warn:
> 'baud_mux_clk' from clk_prepare_enable() not released on lines: 1032.
> 
> In the function brcmuart_probe(), baud_mux_clk was not correctly released
> in subsequent error handling, which may cause memory leaks.
> 
> To fix this issue, an error handling branch, err_clk_put, is added to
> release the variable using clk_put(), and an err_disable branch is added
> to meet the requirement of balancing clk_disable and clk_enable calls.
> 
> Fixes: 15ac1122fd6d ("serial: 8250_bcm7271: Fix arbitration handling")
> Signed-off-by: XuDong Liu <m202071377@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/tty/serial/8250/8250_bcm7271.c | 8 ++++++--
>   1 file changed, 6 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/tty/serial/8250/8250_bcm7271.c b/drivers/tty/serial/8250/8250_bcm7271.c
> index f801b1f5b46c..b1670558868b 100644
> --- a/drivers/tty/serial/8250/8250_bcm7271.c
> +++ b/drivers/tty/serial/8250/8250_bcm7271.c
> @@ -1023,7 +1023,7 @@ static int brcmuart_probe(struct platform_device *pdev)
>   		dev_dbg(dev, "BAUD MUX clock found\n");
>   		ret = clk_prepare_enable(baud_mux_clk);
>   		if (ret)
> -			goto release_dma;
> +			goto err_clk_put;
>   		priv->baud_mux_clk = baud_mux_clk;
>   		init_real_clk_rates(dev, priv);
>   		clk_rate = priv->default_mux_rate;
> @@ -1032,7 +1032,7 @@ static int brcmuart_probe(struct platform_device *pdev)
>   	if (clk_rate == 0) {
>   		dev_err(dev, "clock-frequency or clk not defined\n");
>   		ret = -EINVAL;
> -		goto release_dma;
> +		goto err_clk_disable;
>   	}
>   
>   	dev_dbg(dev, "DMA is %senabled\n", priv->dma_enabled ? "" : "not ");
> @@ -1119,6 +1119,10 @@ static int brcmuart_probe(struct platform_device *pdev)
>   	serial8250_unregister_port(priv->line);
>   err:
>   	brcmuart_free_bufs(dev, priv);
> +err_clk_disable:
> +	clk_disable_unprepare(baud_mux_clk);
> +err_clk_put:
> +	clk_put(baud_mux_clk);
>   release_dma:
>   	if (priv->dma_enabled)
>   		brcmuart_arbitration(priv, 0);

Hi,

it is likely that it should also be added to the rmove function.

CJ

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

* Re: [Patch] serial: 8250_bcm7271: fix leak in `brcmuart_probe`
  2023-04-24 12:51 [Patch] serial: 8250_bcm7271: fix leak in `brcmuart_probe` XuDong Liu
  2023-04-24 14:16 ` Christophe JAILLET
@ 2023-04-27 18:29 ` Doug Berger
  2023-05-13 10:25 ` Greg Kroah-Hartman
  2 siblings, 0 replies; 4+ messages in thread
From: Doug Berger @ 2023-04-27 18:29 UTC (permalink / raw)
  To: XuDong Liu, Al Cooper, Broadcom internal kernel review list,
	Greg Kroah-Hartman, Jiri Slaby, Florian Fainelli
  Cc: Dongliang Mu, linux-serial, linux-kernel

On 4/24/2023 5:51 AM, XuDong Liu wrote:
> Smatch reports:
> drivers/tty/serial/8250/8250_bcm7271.c:1120 brcmuart_probe() warn:
> 'baud_mux_clk' from clk_prepare_enable() not released on lines: 1032.
> 
> In the function brcmuart_probe(), baud_mux_clk was not correctly released
> in subsequent error handling, which may cause memory leaks.
> 
> To fix this issue, an error handling branch, err_clk_put, is added to
> release the variable using clk_put(), and an err_disable branch is added
> to meet the requirement of balancing clk_disable and clk_enable calls.
> 
> Fixes: 15ac1122fd6d ("serial: 8250_bcm7271: Fix arbitration handling")
> Signed-off-by: XuDong Liu <m202071377@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/tty/serial/8250/8250_bcm7271.c | 8 ++++++--
>   1 file changed, 6 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/tty/serial/8250/8250_bcm7271.c b/drivers/tty/serial/8250/8250_bcm7271.c
> index f801b1f5b46c..b1670558868b 100644
> --- a/drivers/tty/serial/8250/8250_bcm7271.c
> +++ b/drivers/tty/serial/8250/8250_bcm7271.c
> @@ -1023,7 +1023,7 @@ static int brcmuart_probe(struct platform_device *pdev)
>   		dev_dbg(dev, "BAUD MUX clock found\n");
>   		ret = clk_prepare_enable(baud_mux_clk);
>   		if (ret)
> -			goto release_dma;
> +			goto err_clk_put;
>   		priv->baud_mux_clk = baud_mux_clk;
>   		init_real_clk_rates(dev, priv);
>   		clk_rate = priv->default_mux_rate;
> @@ -1032,7 +1032,7 @@ static int brcmuart_probe(struct platform_device *pdev)
>   	if (clk_rate == 0) {
>   		dev_err(dev, "clock-frequency or clk not defined\n");
>   		ret = -EINVAL;
> -		goto release_dma;
> +		goto err_clk_disable;
>   	}
>   
>   	dev_dbg(dev, "DMA is %senabled\n", priv->dma_enabled ? "" : "not ");
> @@ -1119,6 +1119,10 @@ static int brcmuart_probe(struct platform_device *pdev)
>   	serial8250_unregister_port(priv->line);
>   err:
>   	brcmuart_free_bufs(dev, priv);
> +err_clk_disable:
> +	clk_disable_unprepare(baud_mux_clk);
> +err_clk_put:
> +	clk_put(baud_mux_clk);
>   release_dma:
>   	if (priv->dma_enabled)
>   		brcmuart_arbitration(priv, 0);
Thank you for bringing this to my attention.

-Doug

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

* Re: [Patch] serial: 8250_bcm7271: fix leak in `brcmuart_probe`
  2023-04-24 12:51 [Patch] serial: 8250_bcm7271: fix leak in `brcmuart_probe` XuDong Liu
  2023-04-24 14:16 ` Christophe JAILLET
  2023-04-27 18:29 ` Doug Berger
@ 2023-05-13 10:25 ` Greg Kroah-Hartman
  2 siblings, 0 replies; 4+ messages in thread
From: Greg Kroah-Hartman @ 2023-05-13 10:25 UTC (permalink / raw)
  To: XuDong Liu
  Cc: Al Cooper, Broadcom internal kernel review list, Jiri Slaby,
	Doug Berger, Florian Fainelli, Dongliang Mu, linux-serial,
	linux-kernel

On Mon, Apr 24, 2023 at 08:51:00PM +0800, XuDong Liu wrote:
> Smatch reports:
> drivers/tty/serial/8250/8250_bcm7271.c:1120 brcmuart_probe() warn: 
> 'baud_mux_clk' from clk_prepare_enable() not released on lines: 1032.
> 
> In the function brcmuart_probe(), baud_mux_clk was not correctly released
> in subsequent error handling, which may cause memory leaks.
> 
> To fix this issue, an error handling branch, err_clk_put, is added to
> release the variable using clk_put(), and an err_disable branch is added
> to meet the requirement of balancing clk_disable and clk_enable calls.
> 
> Fixes: 15ac1122fd6d ("serial: 8250_bcm7271: Fix arbitration handling")
> Signed-off-by: XuDong Liu <m202071377@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.

Please test it.

thanks,

greg k-h

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

end of thread, other threads:[~2023-05-13 10:32 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-04-24 12:51 [Patch] serial: 8250_bcm7271: fix leak in `brcmuart_probe` XuDong Liu
2023-04-24 14:16 ` Christophe JAILLET
2023-04-27 18:29 ` Doug Berger
2023-05-13 10:25 ` Greg Kroah-Hartman

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).