public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] Fix memory leaks in wave5_vpu_open_enc() and wave5_vpu_open_dec()
@ 2023-12-04  8:39 Zeng Chi
  2023-12-04 13:54 ` Nicolas Dufresne
  0 siblings, 1 reply; 6+ messages in thread
From: Zeng Chi @ 2023-12-04  8:39 UTC (permalink / raw)
  To: nas.chung, jackson.lee, mchehab, sebastian.fricke,
	nicolas.dufresne, hverkuil-cisco, bob.beckett
  Cc: linux-media, linux-kernel, zengchi

This patch fixes memory leaks on error escapes in wave5_vpu_open_enc()
and wave5_vpu_open_dec().

Fixes: 9707a6254a8a ("media: chips-media: wave5: Add the v4l2 layer")
Signed-off-by: Zeng Chi <zengchi@kylinos.cn>
---
 drivers/media/platform/chips-media/wave5/wave5-vpu-dec.c | 4 +++-
 drivers/media/platform/chips-media/wave5/wave5-vpu-enc.c | 4 +++-
 2 files changed, 6 insertions(+), 2 deletions(-)

diff --git a/drivers/media/platform/chips-media/wave5/wave5-vpu-dec.c b/drivers/media/platform/chips-media/wave5/wave5-vpu-dec.c
index 8b1417ece96e..2d3e8a184f93 100644
--- a/drivers/media/platform/chips-media/wave5/wave5-vpu-dec.c
+++ b/drivers/media/platform/chips-media/wave5/wave5-vpu-dec.c
@@ -1792,8 +1792,10 @@ static int wave5_vpu_open_dec(struct file *filp)
 	int ret = 0;
 
 	inst = kzalloc(sizeof(*inst), GFP_KERNEL);
-	if (!inst)
+	if (!inst) {
+		kfree(inst);
 		return -ENOMEM;
+	}
 
 	inst->dev = dev;
 	inst->type = VPU_INST_TYPE_DEC;
diff --git a/drivers/media/platform/chips-media/wave5/wave5-vpu-enc.c b/drivers/media/platform/chips-media/wave5/wave5-vpu-enc.c
index f29cfa3af94a..cbf6cb6e07aa 100644
--- a/drivers/media/platform/chips-media/wave5/wave5-vpu-enc.c
+++ b/drivers/media/platform/chips-media/wave5/wave5-vpu-enc.c
@@ -1537,8 +1537,10 @@ static int wave5_vpu_open_enc(struct file *filp)
 	int ret = 0;
 
 	inst = kzalloc(sizeof(*inst), GFP_KERNEL);
-	if (!inst)
+	if (!inst) {
+		kfree(inst);
 		return -ENOMEM;
+	}
 	v4l2_ctrl_hdl = &inst->v4l2_ctrl_hdl;
 
 	inst->dev = dev;
-- 
2.25.1


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

* [PATCH] Fix memory leaks in wave5_vpu_open_enc() and wave5_vpu_open_dec()
@ 2023-12-04  9:16 Zeng Chi
  2023-12-04 13:55 ` Nicolas Dufresne
  0 siblings, 1 reply; 6+ messages in thread
From: Zeng Chi @ 2023-12-04  9:16 UTC (permalink / raw)
  To: nas.chung, jackson.lee, mchehab, sebastian.fricke,
	nicolas.dufresne, hverkuil-cisco, bob.beckett
  Cc: linux-media, linux-kernel, zengchi

This patch fixes memory leaks on error escapes in wave5_vpu_open_enc()
and wave5_vpu_open_dec().

Fixes: 9707a6254a8a ("media: chips-media: wave5: Add the v4l2 layer")
Signed-off-by: Zeng Chi <zengchi@kylinos.cn>
---
 drivers/media/platform/chips-media/wave5/wave5-vpu-dec.c | 5 +++--
 drivers/media/platform/chips-media/wave5/wave5-vpu-enc.c | 5 +++--
 2 files changed, 6 insertions(+), 4 deletions(-)

diff --git a/drivers/media/platform/chips-media/wave5/wave5-vpu-dec.c b/drivers/media/platform/chips-media/wave5/wave5-vpu-dec.c
index 8b1417ece96e..b0a045346bb7 100644
--- a/drivers/media/platform/chips-media/wave5/wave5-vpu-dec.c
+++ b/drivers/media/platform/chips-media/wave5/wave5-vpu-dec.c
@@ -1802,9 +1802,10 @@ static int wave5_vpu_open_dec(struct file *filp)
 	spin_lock_init(&inst->state_spinlock);
 
 	inst->codec_info = kzalloc(sizeof(*inst->codec_info), GFP_KERNEL);
-	if (!inst->codec_info)
+	if (!inst->codec_info) {
+		kfree(inst);
 		return -ENOMEM;
-
+	}
 	v4l2_fh_init(&inst->v4l2_fh, vdev);
 	filp->private_data = &inst->v4l2_fh;
 	v4l2_fh_add(&inst->v4l2_fh);
diff --git a/drivers/media/platform/chips-media/wave5/wave5-vpu-enc.c b/drivers/media/platform/chips-media/wave5/wave5-vpu-enc.c
index f29cfa3af94a..bc94de9ea546 100644
--- a/drivers/media/platform/chips-media/wave5/wave5-vpu-enc.c
+++ b/drivers/media/platform/chips-media/wave5/wave5-vpu-enc.c
@@ -1546,9 +1546,10 @@ static int wave5_vpu_open_enc(struct file *filp)
 	inst->ops = &wave5_vpu_enc_inst_ops;
 
 	inst->codec_info = kzalloc(sizeof(*inst->codec_info), GFP_KERNEL);
-	if (!inst->codec_info)
+	if (!inst->codec_info) {
+		kfree(inst);
 		return -ENOMEM;
-
+	}
 	v4l2_fh_init(&inst->v4l2_fh, vdev);
 	filp->private_data = &inst->v4l2_fh;
 	v4l2_fh_add(&inst->v4l2_fh);
-- 
2.25.1


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

* Re: [PATCH] Fix memory leaks in wave5_vpu_open_enc() and wave5_vpu_open_dec()
  2023-12-04  8:39 [PATCH] Fix memory leaks in wave5_vpu_open_enc() and wave5_vpu_open_dec() Zeng Chi
@ 2023-12-04 13:54 ` Nicolas Dufresne
  0 siblings, 0 replies; 6+ messages in thread
From: Nicolas Dufresne @ 2023-12-04 13:54 UTC (permalink / raw)
  To: Zeng Chi, nas.chung, jackson.lee, mchehab, sebastian.fricke,
	hverkuil-cisco, bob.beckett
  Cc: linux-media, linux-kernel

Hi,

Le lundi 04 décembre 2023 à 16:39 +0800, Zeng Chi a écrit :
> This patch fixes memory leaks on error escapes in wave5_vpu_open_enc()
> and wave5_vpu_open_dec().
> 
> Fixes: 9707a6254a8a ("media: chips-media: wave5: Add the v4l2 layer")
> Signed-off-by: Zeng Chi <zengchi@kylinos.cn>
> ---
>  drivers/media/platform/chips-media/wave5/wave5-vpu-dec.c | 4 +++-
>  drivers/media/platform/chips-media/wave5/wave5-vpu-enc.c | 4 +++-
>  2 files changed, 6 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/media/platform/chips-media/wave5/wave5-vpu-dec.c b/drivers/media/platform/chips-media/wave5/wave5-vpu-dec.c
> index 8b1417ece96e..2d3e8a184f93 100644
> --- a/drivers/media/platform/chips-media/wave5/wave5-vpu-dec.c
> +++ b/drivers/media/platform/chips-media/wave5/wave5-vpu-dec.c
> @@ -1792,8 +1792,10 @@ static int wave5_vpu_open_dec(struct file *filp)
>  	int ret = 0;
>  
>  	inst = kzalloc(sizeof(*inst), GFP_KERNEL);
> -	if (!inst)
> +	if (!inst) {
> +		kfree(inst);

The allocation failed, there is nothing to free here (kfree(null) is a no-op).
This patch does not do what it pretends to do.

Nicolas

>  		return -ENOMEM;
> +	}
>  
>  	inst->dev = dev;
>  	inst->type = VPU_INST_TYPE_DEC;
> diff --git a/drivers/media/platform/chips-media/wave5/wave5-vpu-enc.c b/drivers/media/platform/chips-media/wave5/wave5-vpu-enc.c
> index f29cfa3af94a..cbf6cb6e07aa 100644
> --- a/drivers/media/platform/chips-media/wave5/wave5-vpu-enc.c
> +++ b/drivers/media/platform/chips-media/wave5/wave5-vpu-enc.c
> @@ -1537,8 +1537,10 @@ static int wave5_vpu_open_enc(struct file *filp)
>  	int ret = 0;
>  
>  	inst = kzalloc(sizeof(*inst), GFP_KERNEL);
> -	if (!inst)
> +	if (!inst) {
> +		kfree(inst);
>  		return -ENOMEM;
> +	}
>  	v4l2_ctrl_hdl = &inst->v4l2_ctrl_hdl;
>  
>  	inst->dev = dev;



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

* Re: [PATCH] Fix memory leaks in wave5_vpu_open_enc() and wave5_vpu_open_dec()
  2023-12-04  9:16 Zeng Chi
@ 2023-12-04 13:55 ` Nicolas Dufresne
  2023-12-05 17:36   ` Robert Beckett
  0 siblings, 1 reply; 6+ messages in thread
From: Nicolas Dufresne @ 2023-12-04 13:55 UTC (permalink / raw)
  To: Zeng Chi, nas.chung, jackson.lee, mchehab, sebastian.fricke,
	hverkuil-cisco, bob.beckett
  Cc: linux-media, linux-kernel

Hi,

Le lundi 04 décembre 2023 à 17:16 +0800, Zeng Chi a écrit :
> This patch fixes memory leaks on error escapes in wave5_vpu_open_enc()
> and wave5_vpu_open_dec().

Please avoid sending twice the same patch. This is still a NAK.

regards,
Nicolas

> 
> Fixes: 9707a6254a8a ("media: chips-media: wave5: Add the v4l2 layer")
> Signed-off-by: Zeng Chi <zengchi@kylinos.cn>
> ---
>  drivers/media/platform/chips-media/wave5/wave5-vpu-dec.c | 5 +++--
>  drivers/media/platform/chips-media/wave5/wave5-vpu-enc.c | 5 +++--
>  2 files changed, 6 insertions(+), 4 deletions(-)
> 
> diff --git a/drivers/media/platform/chips-media/wave5/wave5-vpu-dec.c b/drivers/media/platform/chips-media/wave5/wave5-vpu-dec.c
> index 8b1417ece96e..b0a045346bb7 100644
> --- a/drivers/media/platform/chips-media/wave5/wave5-vpu-dec.c
> +++ b/drivers/media/platform/chips-media/wave5/wave5-vpu-dec.c
> @@ -1802,9 +1802,10 @@ static int wave5_vpu_open_dec(struct file *filp)
>  	spin_lock_init(&inst->state_spinlock);
>  
>  	inst->codec_info = kzalloc(sizeof(*inst->codec_info), GFP_KERNEL);
> -	if (!inst->codec_info)
> +	if (!inst->codec_info) {
> +		kfree(inst);
>  		return -ENOMEM;
> -
> +	}
>  	v4l2_fh_init(&inst->v4l2_fh, vdev);
>  	filp->private_data = &inst->v4l2_fh;
>  	v4l2_fh_add(&inst->v4l2_fh);
> diff --git a/drivers/media/platform/chips-media/wave5/wave5-vpu-enc.c b/drivers/media/platform/chips-media/wave5/wave5-vpu-enc.c
> index f29cfa3af94a..bc94de9ea546 100644
> --- a/drivers/media/platform/chips-media/wave5/wave5-vpu-enc.c
> +++ b/drivers/media/platform/chips-media/wave5/wave5-vpu-enc.c
> @@ -1546,9 +1546,10 @@ static int wave5_vpu_open_enc(struct file *filp)
>  	inst->ops = &wave5_vpu_enc_inst_ops;
>  
>  	inst->codec_info = kzalloc(sizeof(*inst->codec_info), GFP_KERNEL);
> -	if (!inst->codec_info)
> +	if (!inst->codec_info) {
> +		kfree(inst);
>  		return -ENOMEM;
> -
> +	}
>  	v4l2_fh_init(&inst->v4l2_fh, vdev);
>  	filp->private_data = &inst->v4l2_fh;
>  	v4l2_fh_add(&inst->v4l2_fh);


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

* Re: [PATCH] Fix memory leaks in wave5_vpu_open_enc() and wave5_vpu_open_dec()
  2023-12-04 13:55 ` Nicolas Dufresne
@ 2023-12-05 17:36   ` Robert Beckett
  2023-12-07 18:40     ` Nicolas Dufresne
  0 siblings, 1 reply; 6+ messages in thread
From: Robert Beckett @ 2023-12-05 17:36 UTC (permalink / raw)
  To: Nicolas Dufresne, Zeng Chi, nas.chung, jackson.lee, mchehab,
	sebastian.fricke, hverkuil-cisco
  Cc: linux-media, linux-kernel


On 04/12/2023 13:55, Nicolas Dufresne wrote:
> Hi,
>
> Le lundi 04 décembre 2023 à 17:16 +0800, Zeng Chi a écrit :
>> This patch fixes memory leaks on error escapes in wave5_vpu_open_enc()
>> and wave5_vpu_open_dec().
> Please avoid sending twice the same patch. This is still a NAK.

tbf, this is a different patch, concerning the allocation of the 
codec_info within inst, not inst itself.

> regards,
> Nicolas
>
>> Fixes: 9707a6254a8a ("media: chips-media: wave5: Add the v4l2 layer")
>> Signed-off-by: Zeng Chi<zengchi@kylinos.cn>
>> ---
>>   drivers/media/platform/chips-media/wave5/wave5-vpu-dec.c | 5 +++--
>>   drivers/media/platform/chips-media/wave5/wave5-vpu-enc.c | 5 +++--
>>   2 files changed, 6 insertions(+), 4 deletions(-)
>>
>> diff --git a/drivers/media/platform/chips-media/wave5/wave5-vpu-dec.c b/drivers/media/platform/chips-media/wave5/wave5-vpu-dec.c
>> index 8b1417ece96e..b0a045346bb7 100644
>> --- a/drivers/media/platform/chips-media/wave5/wave5-vpu-dec.c
>> +++ b/drivers/media/platform/chips-media/wave5/wave5-vpu-dec.c
>> @@ -1802,9 +1802,10 @@ static int wave5_vpu_open_dec(struct file *filp)
>>   	spin_lock_init(&inst->state_spinlock);
>>   
>>   	inst->codec_info = kzalloc(sizeof(*inst->codec_info), GFP_KERNEL);
>> -	if (!inst->codec_info)
>> +	if (!inst->codec_info) {
>> +		kfree(inst);

for consistency, would be better to jump to cleanup_inst.

Also, maybe consider embedding codec_info  in to struct vpu_instance to 
avoid the double alloc. I've not checked whether this is viable 
throughout the code, but from a quick scan of the original patch, it 
looks like it is always allocated and freed alongside inst.

>>   		return -ENOMEM;
>> -
>> +	}
>>   	v4l2_fh_init(&inst->v4l2_fh, vdev);
>>   	filp->private_data = &inst->v4l2_fh;
>>   	v4l2_fh_add(&inst->v4l2_fh);
>> diff --git a/drivers/media/platform/chips-media/wave5/wave5-vpu-enc.c b/drivers/media/platform/chips-media/wave5/wave5-vpu-enc.c
>> index f29cfa3af94a..bc94de9ea546 100644
>> --- a/drivers/media/platform/chips-media/wave5/wave5-vpu-enc.c
>> +++ b/drivers/media/platform/chips-media/wave5/wave5-vpu-enc.c
>> @@ -1546,9 +1546,10 @@ static int wave5_vpu_open_enc(struct file *filp)
>>   	inst->ops = &wave5_vpu_enc_inst_ops;
>>   
>>   	inst->codec_info = kzalloc(sizeof(*inst->codec_info), GFP_KERNEL);
>> -	if (!inst->codec_info)
>> +	if (!inst->codec_info) {
>> +		kfree(inst);
>>   		return -ENOMEM;
>> -
>> +	}
>>   	v4l2_fh_init(&inst->v4l2_fh, vdev);
>>   	filp->private_data = &inst->v4l2_fh;
>>   	v4l2_fh_add(&inst->v4l2_fh);

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

* Re: [PATCH] Fix memory leaks in wave5_vpu_open_enc() and wave5_vpu_open_dec()
  2023-12-05 17:36   ` Robert Beckett
@ 2023-12-07 18:40     ` Nicolas Dufresne
  0 siblings, 0 replies; 6+ messages in thread
From: Nicolas Dufresne @ 2023-12-07 18:40 UTC (permalink / raw)
  To: Robert Beckett, Zeng Chi, nas.chung, jackson.lee, mchehab,
	sebastian.fricke, hverkuil-cisco
  Cc: linux-media, linux-kernel

Le mardi 05 décembre 2023 à 17:36 +0000, Robert Beckett a écrit :
> On 04/12/2023 13:55, Nicolas Dufresne wrote:
> > Hi,
> > 
> > Le lundi 04 décembre 2023 à 17:16 +0800, Zeng Chi a écrit :
> > > This patch fixes memory leaks on error escapes in wave5_vpu_open_enc()
> > > and wave5_vpu_open_dec().
> > Please avoid sending twice the same patch. This is still a NAK.
> 
> tbf, this is a different patch, concerning the allocation of the 
> codec_info within inst, not inst itself.

I've stopped after reading an identical subject line. I can apology for not
noticing the difference, but I think an effort from the submitter could
certainly help in the future.

Nicolas

> 
> > regards,
> > Nicolas
> > 
> > > Fixes: 9707a6254a8a ("media: chips-media: wave5: Add the v4l2 layer")
> > > Signed-off-by: Zeng Chi<zengchi@kylinos.cn>
> > > ---
> > >   drivers/media/platform/chips-media/wave5/wave5-vpu-dec.c | 5 +++--
> > >   drivers/media/platform/chips-media/wave5/wave5-vpu-enc.c | 5 +++--
> > >   2 files changed, 6 insertions(+), 4 deletions(-)
> > > 
> > > diff --git a/drivers/media/platform/chips-media/wave5/wave5-vpu-dec.c b/drivers/media/platform/chips-media/wave5/wave5-vpu-dec.c
> > > index 8b1417ece96e..b0a045346bb7 100644
> > > --- a/drivers/media/platform/chips-media/wave5/wave5-vpu-dec.c
> > > +++ b/drivers/media/platform/chips-media/wave5/wave5-vpu-dec.c
> > > @@ -1802,9 +1802,10 @@ static int wave5_vpu_open_dec(struct file *filp)
> > >   	spin_lock_init(&inst->state_spinlock);
> > >   
> > >   	inst->codec_info = kzalloc(sizeof(*inst->codec_info), GFP_KERNEL);
> > > -	if (!inst->codec_info)
> > > +	if (!inst->codec_info) {
> > > +		kfree(inst);
> 
> for consistency, would be better to jump to cleanup_inst.
> 
> Also, maybe consider embedding codec_info  in to struct vpu_instance to 
> avoid the double alloc. I've not checked whether this is viable 
> throughout the code, but from a quick scan of the original patch, it 
> looks like it is always allocated and freed alongside inst.
> 
> > >   		return -ENOMEM;
> > > -
> > > +	}
> > >   	v4l2_fh_init(&inst->v4l2_fh, vdev);
> > >   	filp->private_data = &inst->v4l2_fh;
> > >   	v4l2_fh_add(&inst->v4l2_fh);
> > > diff --git a/drivers/media/platform/chips-media/wave5/wave5-vpu-enc.c b/drivers/media/platform/chips-media/wave5/wave5-vpu-enc.c
> > > index f29cfa3af94a..bc94de9ea546 100644
> > > --- a/drivers/media/platform/chips-media/wave5/wave5-vpu-enc.c
> > > +++ b/drivers/media/platform/chips-media/wave5/wave5-vpu-enc.c
> > > @@ -1546,9 +1546,10 @@ static int wave5_vpu_open_enc(struct file *filp)
> > >   	inst->ops = &wave5_vpu_enc_inst_ops;
> > >   
> > >   	inst->codec_info = kzalloc(sizeof(*inst->codec_info), GFP_KERNEL);
> > > -	if (!inst->codec_info)
> > > +	if (!inst->codec_info) {
> > > +		kfree(inst);
> > >   		return -ENOMEM;
> > > -
> > > +	}
> > >   	v4l2_fh_init(&inst->v4l2_fh, vdev);
> > >   	filp->private_data = &inst->v4l2_fh;
> > >   	v4l2_fh_add(&inst->v4l2_fh);
> 


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

end of thread, other threads:[~2023-12-07 18:41 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-12-04  8:39 [PATCH] Fix memory leaks in wave5_vpu_open_enc() and wave5_vpu_open_dec() Zeng Chi
2023-12-04 13:54 ` Nicolas Dufresne
  -- strict thread matches above, loose matches on Subject: below --
2023-12-04  9:16 Zeng Chi
2023-12-04 13:55 ` Nicolas Dufresne
2023-12-05 17:36   ` Robert Beckett
2023-12-07 18:40     ` Nicolas Dufresne

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