mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v2] soc: mediatek: svs: Fix memory leak in svs_enable_debug_write()
@ 2025-12-28 16:26 Zilin Guan
  2025-12-29  8:19 ` Markus Elfring
  2026-01-08  9:16 ` AngeloGioacchino Del Regno
  0 siblings, 2 replies; 4+ messages in thread
From: Zilin Guan @ 2025-12-28 16:26 UTC (permalink / raw)
  To: matthias.bgg
  Cc: angelogioacchino.delregno, linux-kernel, linux-arm-kernel,
	linux-mediatek, Zilin Guan, Jianhao Xu

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


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

* Re: [PATCH v2] soc: mediatek: svs: Fix memory leak in svs_enable_debug_write()
  2025-12-28 16:26 [PATCH v2] soc: mediatek: svs: Fix memory leak in svs_enable_debug_write() Zilin Guan
@ 2025-12-29  8:19 ` Markus Elfring
  2025-12-29 13:17   ` Zilin Guan
  2026-01-08  9:16 ` AngeloGioacchino Del Regno
  1 sibling, 1 reply; 4+ messages in thread
From: Markus Elfring @ 2025-12-29  8:19 UTC (permalink / raw)
  To: Jianhao Xu, Zilin Guan, linux-mediatek, linux-arm-kernel,
	Matthias Brugger
  Cc: LKML, kernel-janitors, Angelo Gioacchino Del Regno

…
> +++ 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

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

* Re: [PATCH v2] soc: mediatek: svs: Fix memory leak in svs_enable_debug_write()
  2025-12-29  8:19 ` Markus Elfring
@ 2025-12-29 13:17   ` Zilin Guan
  0 siblings, 0 replies; 4+ messages in thread
From: Zilin Guan @ 2025-12-29 13:17 UTC (permalink / raw)
  To: markus.elfring
  Cc: angelogioacchino.delregno, jianhao.xu, kernel-janitors,
	linux-arm-kernel, linux-kernel, linux-mediatek, matthias.bgg,
	zilin

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

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

* Re: [PATCH v2] soc: mediatek: svs: Fix memory leak in svs_enable_debug_write()
  2025-12-28 16:26 [PATCH v2] soc: mediatek: svs: Fix memory leak in svs_enable_debug_write() Zilin Guan
  2025-12-29  8:19 ` Markus Elfring
@ 2026-01-08  9:16 ` AngeloGioacchino Del Regno
  1 sibling, 0 replies; 4+ messages in thread
From: AngeloGioacchino Del Regno @ 2026-01-08  9:16 UTC (permalink / raw)
  To: matthias.bgg, Zilin Guan
  Cc: linux-kernel, linux-arm-kernel, linux-mediatek, Jianhao Xu

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



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

end of thread, other threads:[~2026-01-08  9:17 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-12-28 16:26 [PATCH v2] soc: mediatek: svs: Fix memory leak in svs_enable_debug_write() Zilin Guan
2025-12-29  8:19 ` Markus Elfring
2025-12-29 13:17   ` Zilin Guan
2026-01-08  9:16 ` AngeloGioacchino Del Regno

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®