netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] net: sfp: fix memory leak in sfp_probe()
@ 2022-06-23  7:09 Jianglei Nie
  2022-06-23  7:20 ` Michael Walle
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Jianglei Nie @ 2022-06-23  7:09 UTC (permalink / raw)
  To: linux, andrew, hkallweit1, davem, edumazet, kuba, pabeni
  Cc: netdev, linux-kernel, Jianglei Nie

sfp_probe() allocates a memory chunk from sfp with sfp_alloc(), when
devm_add_action() fails, sfp is not freed, which leads to a memory leak.

We should free the sfp with sfp_cleanup() when devm_add_action() fails.

Signed-off-by: Jianglei Nie <niejianglei2021@163.com>
---
 drivers/net/phy/sfp.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/drivers/net/phy/sfp.c b/drivers/net/phy/sfp.c
index 9a5d5a10560f..366a89adabf5 100644
--- a/drivers/net/phy/sfp.c
+++ b/drivers/net/phy/sfp.c
@@ -2517,8 +2517,10 @@ static int sfp_probe(struct platform_device *pdev)
 	platform_set_drvdata(pdev, sfp);
 
 	err = devm_add_action(sfp->dev, sfp_cleanup, sfp);
-	if (err < 0)
+	if (err < 0) {
+		sfp_cleanup(sfp);
 		return err;
+	}
 
 	sff = sfp->type = &sfp_data;
 
-- 
2.25.1


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

* Re: [PATCH] net: sfp: fix memory leak in sfp_probe()
  2022-06-23  7:09 [PATCH] net: sfp: fix memory leak in sfp_probe() Jianglei Nie
@ 2022-06-23  7:20 ` Michael Walle
  2022-06-23  8:24 ` Christophe JAILLET
  2022-06-23 19:14 ` Jakub Kicinski
  2 siblings, 0 replies; 4+ messages in thread
From: Michael Walle @ 2022-06-23  7:20 UTC (permalink / raw)
  To: niejianglei2021
  Cc: andrew, davem, edumazet, hkallweit1, kuba, linux-kernel, linux,
	netdev, pabeni, Michael Walle

Hi,

>  	err = devm_add_action(sfp->dev, sfp_cleanup, sfp);
> -	if (err < 0)
> +	if (err < 0) {
> +		sfp_cleanup(sfp);
>  		return err;
> +	}

Better use devm_add_action_or_reset(), no?

-michael

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

* Re: [PATCH] net: sfp: fix memory leak in sfp_probe()
  2022-06-23  7:09 [PATCH] net: sfp: fix memory leak in sfp_probe() Jianglei Nie
  2022-06-23  7:20 ` Michael Walle
@ 2022-06-23  8:24 ` Christophe JAILLET
  2022-06-23 19:14 ` Jakub Kicinski
  2 siblings, 0 replies; 4+ messages in thread
From: Christophe JAILLET @ 2022-06-23  8:24 UTC (permalink / raw)
  To: Jianglei Nie, linux, andrew, hkallweit1, davem, edumazet, kuba,
	pabeni
  Cc: netdev, linux-kernel

Le 23/06/2022 à 09:09, Jianglei Nie a écrit :
> sfp_probe() allocates a memory chunk from sfp with sfp_alloc(), when
> devm_add_action() fails, sfp is not freed, which leads to a memory leak.
> 
> We should free the sfp with sfp_cleanup() when devm_add_action() fails.
> 
> Signed-off-by: Jianglei Nie <niejianglei2021@163.com>
> ---
>   drivers/net/phy/sfp.c | 4 +++-
>   1 file changed, 3 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/net/phy/sfp.c b/drivers/net/phy/sfp.c
> index 9a5d5a10560f..366a89adabf5 100644
> --- a/drivers/net/phy/sfp.c
> +++ b/drivers/net/phy/sfp.c
> @@ -2517,8 +2517,10 @@ static int sfp_probe(struct platform_device *pdev)
>   	platform_set_drvdata(pdev, sfp);
>   
>   	err = devm_add_action(sfp->dev, sfp_cleanup, sfp);
> -	if (err < 0)
> +	if (err < 0) {
> +		sfp_cleanup(sfp);
>   		return err;
> +	}

Hi,

or use devm_add_action_or_reset() instead?

Just my 2c,

CJ

>   
>   	sff = sfp->type = &sfp_data;
>   


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

* Re: [PATCH] net: sfp: fix memory leak in sfp_probe()
  2022-06-23  7:09 [PATCH] net: sfp: fix memory leak in sfp_probe() Jianglei Nie
  2022-06-23  7:20 ` Michael Walle
  2022-06-23  8:24 ` Christophe JAILLET
@ 2022-06-23 19:14 ` Jakub Kicinski
  2 siblings, 0 replies; 4+ messages in thread
From: Jakub Kicinski @ 2022-06-23 19:14 UTC (permalink / raw)
  To: Jianglei Nie
  Cc: linux, andrew, hkallweit1, davem, edumazet, pabeni, netdev,
	linux-kernel

On Thu, 23 Jun 2022 15:09:14 +0800 Jianglei Nie wrote:
> sfp_probe() allocates a memory chunk from sfp with sfp_alloc(), when
> devm_add_action() fails, sfp is not freed, which leads to a memory leak.
> 
> We should free the sfp with sfp_cleanup() when devm_add_action() fails.

.. and please add a Fixes tag

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

end of thread, other threads:[~2022-06-23 19:34 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-06-23  7:09 [PATCH] net: sfp: fix memory leak in sfp_probe() Jianglei Nie
2022-06-23  7:20 ` Michael Walle
2022-06-23  8:24 ` Christophe JAILLET
2022-06-23 19:14 ` Jakub Kicinski

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