diff mbox series

net: sfp: fix memory leak in sfp_probe()

Message ID 20220623070914.1781700-1-niejianglei2021@163.com (mailing list archive)
State Superseded
Delegated to: Netdev Maintainers
Headers show
Series net: sfp: fix memory leak in sfp_probe() | expand

Checks

Context Check Description
netdev/tree_selection success Guessed tree name to be net-next
netdev/fixes_present success Fixes tag not required for -next series
netdev/subject_prefix warning Target tree name not specified in the subject
netdev/cover_letter success Single patches do not need cover letters
netdev/patch_count success Link
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 0 this patch: 0
netdev/cc_maintainers success CCed 8 of 8 maintainers
netdev/build_clang success Errors and warnings before: 0 this patch: 0
netdev/module_param success Was 0 now: 0
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 0 this patch: 0
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 11 lines checked
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0

Commit Message

Jianglei Nie June 23, 2022, 7:09 a.m. UTC
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(-)

Comments

Michael Walle June 23, 2022, 7:20 a.m. UTC | #1
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
Christophe JAILLET June 23, 2022, 8:24 a.m. UTC | #2
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;
>
Jakub Kicinski June 23, 2022, 7:14 p.m. UTC | #3
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
diff mbox series

Patch

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;