mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] media: mediatek: vcodec: Add scp_put() to free the scp
@ 2025-01-31  1:28 Jiasheng Jiang
  2025-02-14 20:20 ` Sebastian Fricke
  0 siblings, 1 reply; 4+ messages in thread
From: Jiasheng Jiang @ 2025-01-31  1:28 UTC (permalink / raw)
  To: tiffany.lin, andrew-ct.chen, yunfei.dong, mchehab, matthias.bgg,
	angelogioacchino.delregno, fullwaywang
  Cc: linux-media, linux-kernel, linux-arm-kernel, linux-mediatek,
	Jiasheng Jiang

Add scp_put() to free the scp if devm_kzalloc() fails to avoid memory
leak.

Fixes: 53dbe0850444 ("media: mtk-vcodec: potential null pointer deference in SCP")
Signed-off-by: Jiasheng Jiang <jiashengjiangcool@gmail.com>
---
 .../platform/mediatek/vcodec/common/mtk_vcodec_fw_scp.c      | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/drivers/media/platform/mediatek/vcodec/common/mtk_vcodec_fw_scp.c b/drivers/media/platform/mediatek/vcodec/common/mtk_vcodec_fw_scp.c
index ff23b225db70..1b0bc47355c0 100644
--- a/drivers/media/platform/mediatek/vcodec/common/mtk_vcodec_fw_scp.c
+++ b/drivers/media/platform/mediatek/vcodec/common/mtk_vcodec_fw_scp.c
@@ -79,8 +79,11 @@ struct mtk_vcodec_fw *mtk_vcodec_fw_scp_init(void *priv, enum mtk_vcodec_fw_use
 	}
 
 	fw = devm_kzalloc(&plat_dev->dev, sizeof(*fw), GFP_KERNEL);
-	if (!fw)
+	if (!fw) {
+		scp_put(scp);
 		return ERR_PTR(-ENOMEM);
+	}
+
 	fw->type = SCP;
 	fw->ops = &mtk_vcodec_rproc_msg;
 	fw->scp = scp;
-- 
2.25.1


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

* Re: [PATCH] media: mediatek: vcodec: Add scp_put() to free the scp
  2025-01-31  1:28 [PATCH] media: mediatek: vcodec: Add scp_put() to free the scp Jiasheng Jiang
@ 2025-02-14 20:20 ` Sebastian Fricke
  2025-02-18 18:22   ` Jiasheng Jiang
  2025-02-18 18:58   ` [PATCH v2] media: mediatek: vcodec: Fix a resource leak related to the scp device in FW initialization Jiasheng Jiang
  0 siblings, 2 replies; 4+ messages in thread
From: Sebastian Fricke @ 2025-02-14 20:20 UTC (permalink / raw)
  To: Jiasheng Jiang
  Cc: tiffany.lin, andrew-ct.chen, yunfei.dong, mchehab, matthias.bgg,
	angelogioacchino.delregno, fullwaywang, linux-media,
	linux-kernel, linux-arm-kernel, linux-mediatek

Hey Jiasheng,

 ---- On Fri, 31 Jan 2025 02:28:30 +0100  Jiasheng Jiang <jiashengjiangcool@gmail.com> wrote --- 
 > Add scp_put() to free the scp if devm_kzalloc() fails to avoid memory
 > leak.

Your commit message sounds a bit like you copy-pasted your code into the commit message.
What kind of memory is leaking here? Are we talking about SRAM memory?
I'd reword the commit message to something like this to give a bit more context:

On Mediatek devices with a system companion processor (SCP) the mtk_scp structure has to be removed explicitly to avoid a memory leak.
Free the structure in case the allocation of the firmware structure fails during the firmware initialization.

---

Additionally, the commit title says close to nothing to the reader as well.
How about: Fix memory leak in FW initialization

But as I stated above you should clarify what kind of memory we are talking about here.

Also just out of interest have you ever actually experienced issues with this?
It seems to me that the situation where you run out of Kernel memory should be quite rare.

Regards,
Sebastian

 > 
 > Fixes: 53dbe0850444 ("media: mtk-vcodec: potential null pointer deference in SCP")
 > Signed-off-by: Jiasheng Jiang <jiashengjiangcool@gmail.com>
 > ---
 >  .../platform/mediatek/vcodec/common/mtk_vcodec_fw_scp.c      | 5 ++++-
 >  1 file changed, 4 insertions(+), 1 deletion(-)
 > 
 > diff --git a/drivers/media/platform/mediatek/vcodec/common/mtk_vcodec_fw_scp.c b/drivers/media/platform/mediatek/vcodec/common/mtk_vcodec_fw_scp.c
 > index ff23b225db70..1b0bc47355c0 100644
 > --- a/drivers/media/platform/mediatek/vcodec/common/mtk_vcodec_fw_scp.c
 > +++ b/drivers/media/platform/mediatek/vcodec/common/mtk_vcodec_fw_scp.c
 > @@ -79,8 +79,11 @@ struct mtk_vcodec_fw *mtk_vcodec_fw_scp_init(void *priv, enum mtk_vcodec_fw_use
 >      }
 >  
 >      fw = devm_kzalloc(&plat_dev->dev, sizeof(*fw), GFP_KERNEL);
 > -    if (!fw)
 > +    if (!fw) {
 > +        scp_put(scp);
 >          return ERR_PTR(-ENOMEM);
 > +    }
 > +
 >      fw->type = SCP;
 >      fw->ops = &mtk_vcodec_rproc_msg;
 >      fw->scp = scp;
 > -- 
 > 2.25.1
 > 
 > 
 > 


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

* Re: [PATCH] media: mediatek: vcodec: Add scp_put() to free the scp
  2025-02-14 20:20 ` Sebastian Fricke
@ 2025-02-18 18:22   ` Jiasheng Jiang
  2025-02-18 18:58   ` [PATCH v2] media: mediatek: vcodec: Fix a resource leak related to the scp device in FW initialization Jiasheng Jiang
  1 sibling, 0 replies; 4+ messages in thread
From: Jiasheng Jiang @ 2025-02-18 18:22 UTC (permalink / raw)
  To: Sebastian Fricke
  Cc: tiffany.lin, andrew-ct.chen, yunfei.dong, mchehab, matthias.bgg,
	angelogioacchino.delregno, fullwaywang, linux-media,
	linux-kernel, linux-arm-kernel, linux-mediatek

Hi Sebastian,

On Fri, Feb 14, 2025 at 3:20 PM Sebastian Fricke
<sebastian.fricke@collabora.com> wrote:
>
> Hey Jiasheng,
>
>  ---- On Fri, 31 Jan 2025 02:28:30 +0100  Jiasheng Jiang <jiashengjiangcool@gmail.com> wrote ---
>  > Add scp_put() to free the scp if devm_kzalloc() fails to avoid memory
>  > leak.
>
> Your commit message sounds a bit like you copy-pasted your code into the commit message.
> What kind of memory is leaking here? Are we talking about SRAM memory?
> I'd reword the commit message to something like this to give a bit more context:
>
> On Mediatek devices with a system companion processor (SCP) the mtk_scp structure has to be removed explicitly to avoid a memory leak.
> Free the structure in case the allocation of the firmware structure fails during the firmware initialization.
>
> ---
>
> Additionally, the commit title says close to nothing to the reader as well.
> How about: Fix memory leak in FW initialization
>
> But as I stated above you should clarify what kind of memory we are talking about here.
>
> Also just out of interest have you ever actually experienced issues with this?
> It seems to me that the situation where you run out of Kernel memory should be quite rare.

Yes, I just detected such a bug with a static analysis tool and I have
no idea how to trigger it.
I will submit a v2 to reword the commit message.

Thanks,
Jiasheng

>
> Regards,
> Sebastian
>
>  >
>  > Fixes: 53dbe0850444 ("media: mtk-vcodec: potential null pointer deference in SCP")
>  > Signed-off-by: Jiasheng Jiang <jiashengjiangcool@gmail.com>
>  > ---
>  >  .../platform/mediatek/vcodec/common/mtk_vcodec_fw_scp.c      | 5 ++++-
>  >  1 file changed, 4 insertions(+), 1 deletion(-)
>  >
>  > diff --git a/drivers/media/platform/mediatek/vcodec/common/mtk_vcodec_fw_scp.c b/drivers/media/platform/mediatek/vcodec/common/mtk_vcodec_fw_scp.c
>  > index ff23b225db70..1b0bc47355c0 100644
>  > --- a/drivers/media/platform/mediatek/vcodec/common/mtk_vcodec_fw_scp.c
>  > +++ b/drivers/media/platform/mediatek/vcodec/common/mtk_vcodec_fw_scp.c
>  > @@ -79,8 +79,11 @@ struct mtk_vcodec_fw *mtk_vcodec_fw_scp_init(void *priv, enum mtk_vcodec_fw_use
>  >      }
>  >
>  >      fw = devm_kzalloc(&plat_dev->dev, sizeof(*fw), GFP_KERNEL);
>  > -    if (!fw)
>  > +    if (!fw) {
>  > +        scp_put(scp);
>  >          return ERR_PTR(-ENOMEM);
>  > +    }
>  > +
>  >      fw->type = SCP;
>  >      fw->ops = &mtk_vcodec_rproc_msg;
>  >      fw->scp = scp;
>  > --
>  > 2.25.1
>  >
>  >
>  >
>

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

* [PATCH v2] media: mediatek: vcodec: Fix a resource leak related to the scp device in FW initialization
  2025-02-14 20:20 ` Sebastian Fricke
  2025-02-18 18:22   ` Jiasheng Jiang
@ 2025-02-18 18:58   ` Jiasheng Jiang
  1 sibling, 0 replies; 4+ messages in thread
From: Jiasheng Jiang @ 2025-02-18 18:58 UTC (permalink / raw)
  To: sebastian.fricke
  Cc: andrew-ct.chen, angelogioacchino.delregno, fullwaywang,
	jiashengjiangcool, linux-arm-kernel, linux-kernel, linux-media,
	linux-mediatek, matthias.bgg, mchehab, tiffany.lin, yunfei.dong

On Mediatek devices with a system companion processor (SCP) the mtk_scp
structure has to be removed explicitly to avoid a resource leak.
Free the structure in case the allocation of the firmware structure fails
during the firmware initialization.

Fixes: 53dbe0850444 ("media: mtk-vcodec: potential null pointer deference in SCP")
Signed-off-by: Jiasheng Jiang <jiashengjiangcool@gmail.com>
---
Changelog:

v1 -> v2:

1. Reword the commit message to give more context.
---
 .../platform/mediatek/vcodec/common/mtk_vcodec_fw_scp.c      | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/drivers/media/platform/mediatek/vcodec/common/mtk_vcodec_fw_scp.c b/drivers/media/platform/mediatek/vcodec/common/mtk_vcodec_fw_scp.c
index ff23b225db70..1b0bc47355c0 100644
--- a/drivers/media/platform/mediatek/vcodec/common/mtk_vcodec_fw_scp.c
+++ b/drivers/media/platform/mediatek/vcodec/common/mtk_vcodec_fw_scp.c
@@ -79,8 +79,11 @@ struct mtk_vcodec_fw *mtk_vcodec_fw_scp_init(void *priv, enum mtk_vcodec_fw_use
 	}
 
 	fw = devm_kzalloc(&plat_dev->dev, sizeof(*fw), GFP_KERNEL);
-	if (!fw)
+	if (!fw) {
+		scp_put(scp);
 		return ERR_PTR(-ENOMEM);
+	}
+
 	fw->type = SCP;
 	fw->ops = &mtk_vcodec_rproc_msg;
 	fw->scp = scp;
-- 
2.25.1


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

end of thread, other threads:[~2025-02-18 18:58 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-01-31  1:28 [PATCH] media: mediatek: vcodec: Add scp_put() to free the scp Jiasheng Jiang
2025-02-14 20:20 ` Sebastian Fricke
2025-02-18 18:22   ` Jiasheng Jiang
2025-02-18 18:58   ` [PATCH v2] media: mediatek: vcodec: Fix a resource leak related to the scp device in FW initialization Jiasheng Jiang

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®