linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] dmaengine: idxd: Fix memory leak in idxd_alloc()
@ 2022-09-14 23:08 Rafael Mendonca
  2022-09-16 15:36 ` Jerry Snitselaar
                   ` (3 more replies)
  0 siblings, 4 replies; 7+ messages in thread
From: Rafael Mendonca @ 2022-09-14 23:08 UTC (permalink / raw)
  To: Fenghua Yu, Dave Jiang, Vinod Koul
  Cc: Rafael Mendonca, dmaengine, linux-kernel

If the IDA id allocation fails, then the allocated memory for the
idxd_device struct doesn't get freed before returning NULL, which leads to
a memleak.

Fixes: 47c16ac27d4c ("dmaengine: idxd: fix idxd conf_dev 'struct device' lifetime")
Signed-off-by: Rafael Mendonca <rafaelmendsr@gmail.com>
---
 drivers/dma/idxd/init.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/drivers/dma/idxd/init.c b/drivers/dma/idxd/init.c
index aa3478257ddb..fdc97519b8fb 100644
--- a/drivers/dma/idxd/init.c
+++ b/drivers/dma/idxd/init.c
@@ -445,8 +445,10 @@ static struct idxd_device *idxd_alloc(struct pci_dev *pdev, struct idxd_driver_d
 	idxd->data = data;
 	idxd_dev_set_type(&idxd->idxd_dev, idxd->data->type);
 	idxd->id = ida_alloc(&idxd_ida, GFP_KERNEL);
-	if (idxd->id < 0)
+	if (idxd->id < 0) {
+		kfree(idxd);
 		return NULL;
+	}
 
 	device_initialize(conf_dev);
 	conf_dev->parent = dev;
-- 
2.34.1


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

* Re: [PATCH] dmaengine: idxd: Fix memory leak in idxd_alloc()
  2022-09-14 23:08 [PATCH] dmaengine: idxd: Fix memory leak in idxd_alloc() Rafael Mendonca
@ 2022-09-16 15:36 ` Jerry Snitselaar
  2022-09-16 15:49   ` Dave Jiang
  2022-09-16 15:50 ` Dave Jiang
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 7+ messages in thread
From: Jerry Snitselaar @ 2022-09-16 15:36 UTC (permalink / raw)
  To: Rafael Mendonca
  Cc: Fenghua Yu, Dave Jiang, Vinod Koul, dmaengine, linux-kernel

On Wed, Sep 14, 2022 at 08:08:14PM -0300, Rafael Mendonca wrote:
> If the IDA id allocation fails, then the allocated memory for the
> idxd_device struct doesn't get freed before returning NULL, which leads to
> a memleak.
> 
> Fixes: 47c16ac27d4c ("dmaengine: idxd: fix idxd conf_dev 'struct device' lifetime")
> Signed-off-by: Rafael Mendonca <rafaelmendsr@gmail.com>

I think there needs to be a kfree(idxd) where it checks rc < 0 after the call to dev_set_name() as well, yes?

Regards,
Jerry

> ---
>  drivers/dma/idxd/init.c | 4 +++-
>  1 file changed, 3 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/dma/idxd/init.c b/drivers/dma/idxd/init.c
> index aa3478257ddb..fdc97519b8fb 100644
> --- a/drivers/dma/idxd/init.c
> +++ b/drivers/dma/idxd/init.c
> @@ -445,8 +445,10 @@ static struct idxd_device *idxd_alloc(struct pci_dev *pdev, struct idxd_driver_d
>  	idxd->data = data;
>  	idxd_dev_set_type(&idxd->idxd_dev, idxd->data->type);
>  	idxd->id = ida_alloc(&idxd_ida, GFP_KERNEL);
> -	if (idxd->id < 0)
> +	if (idxd->id < 0) {
> +		kfree(idxd);
>  		return NULL;
> +	}
>  
>  	device_initialize(conf_dev);
>  	conf_dev->parent = dev;
> -- 
> 2.34.1
> 


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

* Re: [PATCH] dmaengine: idxd: Fix memory leak in idxd_alloc()
  2022-09-16 15:36 ` Jerry Snitselaar
@ 2022-09-16 15:49   ` Dave Jiang
  2022-09-16 16:16     ` Jerry Snitselaar
  0 siblings, 1 reply; 7+ messages in thread
From: Dave Jiang @ 2022-09-16 15:49 UTC (permalink / raw)
  To: Jerry Snitselaar, Rafael Mendonca
  Cc: Fenghua Yu, Vinod Koul, dmaengine, linux-kernel


On 9/16/2022 8:36 AM, Jerry Snitselaar wrote:
> On Wed, Sep 14, 2022 at 08:08:14PM -0300, Rafael Mendonca wrote:
>> If the IDA id allocation fails, then the allocated memory for the
>> idxd_device struct doesn't get freed before returning NULL, which leads to
>> a memleak.
>>
>> Fixes: 47c16ac27d4c ("dmaengine: idxd: fix idxd conf_dev 'struct device' lifetime")
>> Signed-off-by: Rafael Mendonca <rafaelmendsr@gmail.com>
> I think there needs to be a kfree(idxd) where it checks rc < 0 after the call to dev_set_name() as well, yes?
The idxd_conf_device_release() should take care of freeing idxd with the 
put_device(). So I think we are good here.
>
> Regards,
> Jerry
>
>> ---
>>   drivers/dma/idxd/init.c | 4 +++-
>>   1 file changed, 3 insertions(+), 1 deletion(-)
>>
>> diff --git a/drivers/dma/idxd/init.c b/drivers/dma/idxd/init.c
>> index aa3478257ddb..fdc97519b8fb 100644
>> --- a/drivers/dma/idxd/init.c
>> +++ b/drivers/dma/idxd/init.c
>> @@ -445,8 +445,10 @@ static struct idxd_device *idxd_alloc(struct pci_dev *pdev, struct idxd_driver_d
>>   	idxd->data = data;
>>   	idxd_dev_set_type(&idxd->idxd_dev, idxd->data->type);
>>   	idxd->id = ida_alloc(&idxd_ida, GFP_KERNEL);
>> -	if (idxd->id < 0)
>> +	if (idxd->id < 0) {
>> +		kfree(idxd);
>>   		return NULL;
>> +	}
>>   
>>   	device_initialize(conf_dev);
>>   	conf_dev->parent = dev;
>> -- 
>> 2.34.1
>>

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

* Re: [PATCH] dmaengine: idxd: Fix memory leak in idxd_alloc()
  2022-09-14 23:08 [PATCH] dmaengine: idxd: Fix memory leak in idxd_alloc() Rafael Mendonca
  2022-09-16 15:36 ` Jerry Snitselaar
@ 2022-09-16 15:50 ` Dave Jiang
  2022-09-16 16:17 ` Jerry Snitselaar
  2022-09-29 16:37 ` Vinod Koul
  3 siblings, 0 replies; 7+ messages in thread
From: Dave Jiang @ 2022-09-16 15:50 UTC (permalink / raw)
  To: Rafael Mendonca, Fenghua Yu, Vinod Koul; +Cc: dmaengine, linux-kernel


On 9/14/2022 4:08 PM, Rafael Mendonca wrote:
> If the IDA id allocation fails, then the allocated memory for the
> idxd_device struct doesn't get freed before returning NULL, which leads to
> a memleak.
>
> Fixes: 47c16ac27d4c ("dmaengine: idxd: fix idxd conf_dev 'struct device' lifetime")
> Signed-off-by: Rafael Mendonca <rafaelmendsr@gmail.com>

Thanks!

Reviewed-by: Dave Jiang <dave.jiang@intel.com>

> ---
>   drivers/dma/idxd/init.c | 4 +++-
>   1 file changed, 3 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/dma/idxd/init.c b/drivers/dma/idxd/init.c
> index aa3478257ddb..fdc97519b8fb 100644
> --- a/drivers/dma/idxd/init.c
> +++ b/drivers/dma/idxd/init.c
> @@ -445,8 +445,10 @@ static struct idxd_device *idxd_alloc(struct pci_dev *pdev, struct idxd_driver_d
>   	idxd->data = data;
>   	idxd_dev_set_type(&idxd->idxd_dev, idxd->data->type);
>   	idxd->id = ida_alloc(&idxd_ida, GFP_KERNEL);
> -	if (idxd->id < 0)
> +	if (idxd->id < 0) {
> +		kfree(idxd);
>   		return NULL;
> +	}
>   
>   	device_initialize(conf_dev);
>   	conf_dev->parent = dev;

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

* Re: [PATCH] dmaengine: idxd: Fix memory leak in idxd_alloc()
  2022-09-16 15:49   ` Dave Jiang
@ 2022-09-16 16:16     ` Jerry Snitselaar
  0 siblings, 0 replies; 7+ messages in thread
From: Jerry Snitselaar @ 2022-09-16 16:16 UTC (permalink / raw)
  To: Dave Jiang
  Cc: Rafael Mendonca, Fenghua Yu, Vinod Koul, dmaengine, linux-kernel

On Fri, Sep 16, 2022 at 08:49:25AM -0700, Dave Jiang wrote:
> 
> On 9/16/2022 8:36 AM, Jerry Snitselaar wrote:
> > On Wed, Sep 14, 2022 at 08:08:14PM -0300, Rafael Mendonca wrote:
> > > If the IDA id allocation fails, then the allocated memory for the
> > > idxd_device struct doesn't get freed before returning NULL, which leads to
> > > a memleak.
> > > 
> > > Fixes: 47c16ac27d4c ("dmaengine: idxd: fix idxd conf_dev 'struct device' lifetime")
> > > Signed-off-by: Rafael Mendonca <rafaelmendsr@gmail.com>
> > I think there needs to be a kfree(idxd) where it checks rc < 0 after the call to dev_set_name() as well, yes?
> The idxd_conf_device_release() should take care of freeing idxd with the
> put_device(). So I think we are good here.

Ah, right. Thanks.

Jerry

> > 
> > Regards,
> > Jerry
> > 
> > > ---
> > >   drivers/dma/idxd/init.c | 4 +++-
> > >   1 file changed, 3 insertions(+), 1 deletion(-)
> > > 
> > > diff --git a/drivers/dma/idxd/init.c b/drivers/dma/idxd/init.c
> > > index aa3478257ddb..fdc97519b8fb 100644
> > > --- a/drivers/dma/idxd/init.c
> > > +++ b/drivers/dma/idxd/init.c
> > > @@ -445,8 +445,10 @@ static struct idxd_device *idxd_alloc(struct pci_dev *pdev, struct idxd_driver_d
> > >   	idxd->data = data;
> > >   	idxd_dev_set_type(&idxd->idxd_dev, idxd->data->type);
> > >   	idxd->id = ida_alloc(&idxd_ida, GFP_KERNEL);
> > > -	if (idxd->id < 0)
> > > +	if (idxd->id < 0) {
> > > +		kfree(idxd);
> > >   		return NULL;
> > > +	}
> > >   	device_initialize(conf_dev);
> > >   	conf_dev->parent = dev;
> > > -- 
> > > 2.34.1
> > > 


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

* Re: [PATCH] dmaengine: idxd: Fix memory leak in idxd_alloc()
  2022-09-14 23:08 [PATCH] dmaengine: idxd: Fix memory leak in idxd_alloc() Rafael Mendonca
  2022-09-16 15:36 ` Jerry Snitselaar
  2022-09-16 15:50 ` Dave Jiang
@ 2022-09-16 16:17 ` Jerry Snitselaar
  2022-09-29 16:37 ` Vinod Koul
  3 siblings, 0 replies; 7+ messages in thread
From: Jerry Snitselaar @ 2022-09-16 16:17 UTC (permalink / raw)
  To: Rafael Mendonca
  Cc: Fenghua Yu, Dave Jiang, Vinod Koul, dmaengine, linux-kernel

On Wed, Sep 14, 2022 at 08:08:14PM -0300, Rafael Mendonca wrote:
> If the IDA id allocation fails, then the allocated memory for the
> idxd_device struct doesn't get freed before returning NULL, which leads to
> a memleak.
> 
> Fixes: 47c16ac27d4c ("dmaengine: idxd: fix idxd conf_dev 'struct device' lifetime")
> Signed-off-by: Rafael Mendonca <rafaelmendsr@gmail.com>

Reviewed-by: Jerry Snitselaar <jsnitsel@redhat.com

> ---
>  drivers/dma/idxd/init.c | 4 +++-
>  1 file changed, 3 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/dma/idxd/init.c b/drivers/dma/idxd/init.c
> index aa3478257ddb..fdc97519b8fb 100644
> --- a/drivers/dma/idxd/init.c
> +++ b/drivers/dma/idxd/init.c
> @@ -445,8 +445,10 @@ static struct idxd_device *idxd_alloc(struct pci_dev *pdev, struct idxd_driver_d
>  	idxd->data = data;
>  	idxd_dev_set_type(&idxd->idxd_dev, idxd->data->type);
>  	idxd->id = ida_alloc(&idxd_ida, GFP_KERNEL);
> -	if (idxd->id < 0)
> +	if (idxd->id < 0) {
> +		kfree(idxd);
>  		return NULL;
> +	}
>  
>  	device_initialize(conf_dev);
>  	conf_dev->parent = dev;
> -- 
> 2.34.1
> 


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

* Re: [PATCH] dmaengine: idxd: Fix memory leak in idxd_alloc()
  2022-09-14 23:08 [PATCH] dmaengine: idxd: Fix memory leak in idxd_alloc() Rafael Mendonca
                   ` (2 preceding siblings ...)
  2022-09-16 16:17 ` Jerry Snitselaar
@ 2022-09-29 16:37 ` Vinod Koul
  3 siblings, 0 replies; 7+ messages in thread
From: Vinod Koul @ 2022-09-29 16:37 UTC (permalink / raw)
  To: Rafael Mendonca; +Cc: Fenghua Yu, Dave Jiang, dmaengine, linux-kernel

On 14-09-22, 20:08, Rafael Mendonca wrote:
> If the IDA id allocation fails, then the allocated memory for the
> idxd_device struct doesn't get freed before returning NULL, which leads to
> a memleak.

Applied, thanks

-- 
~Vinod

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

end of thread, other threads:[~2022-09-29 16:37 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-09-14 23:08 [PATCH] dmaengine: idxd: Fix memory leak in idxd_alloc() Rafael Mendonca
2022-09-16 15:36 ` Jerry Snitselaar
2022-09-16 15:49   ` Dave Jiang
2022-09-16 16:16     ` Jerry Snitselaar
2022-09-16 15:50 ` Dave Jiang
2022-09-16 16:17 ` Jerry Snitselaar
2022-09-29 16:37 ` Vinod Koul

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).