[PATCH v2] soc: mediatek: svs: Fix memory leak in svs_enable_debug_write()

Zilin Guan posted 1 patch 1 month, 1 week ago
There is a newer version of this series
drivers/soc/mediatek/mtk-svs.c | 4 +---
1 file changed, 1 insertion(+), 3 deletions(-)
[PATCH v2] soc: mediatek: svs: Fix memory leak in svs_enable_debug_write()
Posted by Zilin Guan 1 month, 1 week ago
In svs_enable_debug_write(), the buf allocated by memdup_user_nul()
is leaked if kstrtoint() fails.

Fix this by using __free(kfree) to automatically free buf, eliminating
the need for explicit kfree() calls and preventing leaks.

Fixes: 13f1bbcfb582 ("soc: mediatek: SVS: add debug commands")
Co-developed-by: Jianhao Xu <jianhao.xu@seu.edu.cn>
Signed-off-by: Jianhao Xu <jianhao.xu@seu.edu.cn>
Signed-off-by: Zilin Guan <zilin@seu.edu.cn>
---
Changes in v2:
- Use __free(kfree) to simplify memory management.
---
 drivers/soc/mediatek/mtk-svs.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

diff --git a/drivers/soc/mediatek/mtk-svs.c b/drivers/soc/mediatek/mtk-svs.c
index f45537546553..5ede59299613 100644
--- a/drivers/soc/mediatek/mtk-svs.c
+++ b/drivers/soc/mediatek/mtk-svs.c
@@ -789,7 +789,7 @@ static ssize_t svs_enable_debug_write(struct file *filp,
 	struct svs_bank *svsb = file_inode(filp)->i_private;
 	struct svs_platform *svsp = dev_get_drvdata(svsb->dev);
 	int enabled, ret;
-	char *buf = NULL;
+	char *buf __free(kfree) = NULL;
 
 	if (count >= PAGE_SIZE)
 		return -EINVAL;
@@ -807,8 +807,6 @@ static ssize_t svs_enable_debug_write(struct file *filp,
 		svsb->mode_support = SVSB_MODE_ALL_DISABLE;
 	}
 
-	kfree(buf);
-
 	return count;
 }
 
-- 
2.34.1
Re: [PATCH v2] soc: mediatek: svs: Fix memory leak in svs_enable_debug_write()
Posted by AngeloGioacchino Del Regno 1 month ago
On Sun, 28 Dec 2025 16:26:36 +0000, Zilin Guan wrote:
> In svs_enable_debug_write(), the buf allocated by memdup_user_nul()
> is leaked if kstrtoint() fails.
> 
> Fix this by using __free(kfree) to automatically free buf, eliminating
> the need for explicit kfree() calls and preventing leaks.
> 
> 
> [...]

Applied to v6.19-next/soc, thanks!

[1/1] soc: mediatek: svs: Fix memory leak in svs_enable_debug_write()
      commit: ebede2ddeb047bdf7ee660a18403592c45d020d9

Cheers,
Angelo
Re: [PATCH v2] soc: mediatek: svs: Fix memory leak in svs_enable_debug_write()
Posted by Markus Elfring 1 month, 1 week ago
…
> +++ b/drivers/soc/mediatek/mtk-svs.c
> @@ -789,7 +789,7 @@ static ssize_t svs_enable_debug_write(struct file *filp,
>  	struct svs_bank *svsb = file_inode(filp)->i_private;
>  	struct svs_platform *svsp = dev_get_drvdata(svsb->dev);
>  	int enabled, ret;
> -	char *buf = NULL;
> +	char *buf __free(kfree) = NULL;
>  
>  	if (count >= PAGE_SIZE)
>  		return -EINVAL;
…

You may reduce the scopes for involved local variables,
don't you?


See also:
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst?h=v6.19-rc3#n262

Regards,
Markus
Re: [PATCH v2] soc: mediatek: svs: Fix memory leak in svs_enable_debug_write()
Posted by Zilin Guan 1 month, 1 week ago
On Mon, Dec 29, 2025 at 09:19:19AM +0100, Markus Elfring wrote:
> …
> > +++ b/drivers/soc/mediatek/mtk-svs.c
> > @@ -789,7 +789,7 @@ static ssize_t svs_enable_debug_write(struct file *filp,
> >  	struct svs_bank *svsb = file_inode(filp)->i_private;
> >  	struct svs_platform *svsp = dev_get_drvdata(svsb->dev);
> >  	int enabled, ret;
> > -	char *buf = NULL;
> > +	char *buf __free(kfree) = NULL;
> >  
> >  	if (count >= PAGE_SIZE)
> >  		return -EINVAL;
> …
> 
> You may reduce the scopes for involved local variables,
> don't you?

Thanks for your suggestion. I will reduce the scope of the local variables 
in v3.

> See also:
> https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst?h=v6.19-rc3#n262
> 
> Regards,
> Markus

Thanks, I will add your suggested Cc tag in v3.

Regards,
Zilin Guan