linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] drivers core: Free dma_range_map when driver probe failed
@ 2020-12-25  5:58 Meng.Li
       [not found] ` <CA+-6iNw1pixdamS1HSPiifrppWp=qsOmxVu6NYhDHr61pC6Gkw@mail.gmail.com>
  0 siblings, 1 reply; 4+ messages in thread
From: Meng.Li @ 2020-12-25  5:58 UTC (permalink / raw)
  To: linux-kernel; +Cc: gregkh, rafael, james.quinlan, Kexin.Hao, meng.li

From: Limeng <Meng.Li@windriver.com>

There will be memory leak if driver probe failed. Trace as below:
  backtrace:
    [<000000002415258f>] kmemleak_alloc+0x3c/0x50
    [<00000000f447ebe4>] __kmalloc+0x208/0x530
    [<0000000048bc7b3a>] of_dma_get_range+0xe4/0x1b0
    [<0000000041e39065>] of_dma_configure_id+0x58/0x27c
    [<000000006356866a>] platform_dma_configure+0x2c/0x40
    ......
    [<000000000afcf9b5>] ret_from_fork+0x10/0x3c

This issue is introduced by commit e0d072782c73("dma-mapping:
introduce DMA range map, supplanting dma_pfn_offset "). It doesn't
free dma_range_map when driver probe failed and cause above
memory leak. So, add code to free it in error path.

Fixes: e0d072782c73("dma-mapping: introduce DMA range map, supplanting dma_pfn_offset ")
Cc: stable@vger.kernel.org
Signed-off-by: Meng Li <Meng.Li@windriver.com>
---
 drivers/base/dd.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/drivers/base/dd.c b/drivers/base/dd.c
index 148e81969e04..8e4871aa34bc 100644
--- a/drivers/base/dd.c
+++ b/drivers/base/dd.c
@@ -612,6 +612,7 @@ static int really_probe(struct device *dev, struct device_driver *drv)
 	else if (drv->remove)
 		drv->remove(dev);
 probe_failed:
+	kfree(dev->dma_range_map);
 	if (dev->bus)
 		blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
 					     BUS_NOTIFY_DRIVER_NOT_BOUND, dev);
-- 
2.17.1


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

* Re: [PATCH] drivers core: Free dma_range_map when driver probe failed
       [not found] ` <CA+-6iNw1pixdamS1HSPiifrppWp=qsOmxVu6NYhDHr61pC6Gkw@mail.gmail.com>
@ 2021-01-04 15:55   ` Jim Quinlan
  2021-01-05  2:46     ` Li, Meng
  2021-01-05  4:33     ` Li, Meng
  0 siblings, 2 replies; 4+ messages in thread
From: Jim Quinlan @ 2021-01-04 15:55 UTC (permalink / raw)
  To: Jim Quinlan, Meng.Li, open list
  Cc: Greg Kroah-Hartman, rafael, Kexin.Hao, Jim Quinlan, Christoph Hellwig

> Subject: [PATCH] drivers core: Free dma_range_map when driver probe failed
>
> There will be memory leak if driver probe failed. Trace as below:
>   backtrace:
>     [<000000002415258f>] kmemleak_alloc+0x3c/0x50
>     [<00000000f447ebe4>] __kmalloc+0x208/0x530
>     [<0000000048bc7b3a>] of_dma_get_range+0xe4/0x1b0
>     [<0000000041e39065>] of_dma_configure_id+0x58/0x27c
>     [<000000006356866a>] platform_dma_configure+0x2c/0x40
>     ......
>     [<000000000afcf9b5>] ret_from_fork+0x10/0x3c
>
> This issue is introduced by commit e0d072782c73("dma-mapping:
> introduce DMA range map, supplanting dma_pfn_offset "). It doesn't
> free dma_range_map when driver probe failed and cause above
> memory leak. So, add code to free it in error path.
>
> Fixes: e0d072782c73("dma-mapping: introduce DMA range map, supplanting
> dma_pfn_offset ")
> Cc: stable@vger.kernel.org
> Signed-off-by: Meng Li <Meng.Li@windriver.com>
> ---
>  drivers/base/dd.c | 1 +
>  1 file changed, 1 insertion(+)
>
> diff --git a/drivers/base/dd.c b/drivers/base/dd.c
> index 148e81969e04..8e4871aa34bc 100644
> --- a/drivers/base/dd.c
> +++ b/drivers/base/dd.c
> @@ -612,6 +612,7 @@ static int really_probe(struct device *dev, struct
> device_driver *drv)
>         else if (drv->remove)
>                 drv->remove(dev);
>  probe_failed:
> +       kfree(dev->dma_range_map);
>         if (dev->bus)
>                 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
>                                              BUS_NOTIFY_DRIVER_NOT_BOUND, dev);
> --
> 2.17.1
>
> --

Hi Meng,

Sorry for the delay -- I was on vacation.  I agree with you -- thanks.
However, note that in function  device_release()  in
drivers/base/core.c  there is this line:

kfree(dev->dma_range_map);

Won't this also be called  if all of the appropriate drivers' probes
fail for this device, effecting a double kfree?  Perhaps your patch
could also set "dev->dma_range_map" to NULL after calling kfree()?

Thanks much,
Jim Quinlan
Broadcom STB

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

* RE: [PATCH] drivers core: Free dma_range_map when driver probe failed
  2021-01-04 15:55   ` Jim Quinlan
@ 2021-01-05  2:46     ` Li, Meng
  2021-01-05  4:33     ` Li, Meng
  1 sibling, 0 replies; 4+ messages in thread
From: Li, Meng @ 2021-01-05  2:46 UTC (permalink / raw)
  To: Jim Quinlan, Jim Quinlan, open list
  Cc: Greg Kroah-Hartman, rafael, Hao, Kexin, Christoph Hellwig



> -----Original Message-----
> From: Jim Quinlan <jim2101024@gmail.com>
> Sent: Monday, January 4, 2021 11:56 PM
> To: Jim Quinlan <james.quinlan@broadcom.com>; Li, Meng
> <Meng.Li@windriver.com>; open list <linux-kernel@vger.kernel.org>
> Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>; rafael@kernel.org;
> Hao, Kexin <Kexin.Hao@windriver.com>; Jim Quinlan
> <jim2101024@gmail.com>; Christoph Hellwig <hch@lst.de>
> Subject: Re: [PATCH] drivers core: Free dma_range_map when driver probe
> failed
> 
> > Subject: [PATCH] drivers core: Free dma_range_map when driver probe
> > failed
> >
> > There will be memory leak if driver probe failed. Trace as below:
> >   backtrace:
> >     [<000000002415258f>] kmemleak_alloc+0x3c/0x50
> >     [<00000000f447ebe4>] __kmalloc+0x208/0x530
> >     [<0000000048bc7b3a>] of_dma_get_range+0xe4/0x1b0
> >     [<0000000041e39065>] of_dma_configure_id+0x58/0x27c
> >     [<000000006356866a>] platform_dma_configure+0x2c/0x40
> >     ......
> >     [<000000000afcf9b5>] ret_from_fork+0x10/0x3c
> >
> > This issue is introduced by commit e0d072782c73("dma-mapping:
> > introduce DMA range map, supplanting dma_pfn_offset "). It doesn't
> > free dma_range_map when driver probe failed and cause above memory
> > leak. So, add code to free it in error path.
> >
> > Fixes: e0d072782c73("dma-mapping: introduce DMA range map,
> supplanting
> > dma_pfn_offset ")
> > Cc: stable@vger.kernel.org
> > Signed-off-by: Meng Li <Meng.Li@windriver.com>
> > ---
> >  drivers/base/dd.c | 1 +
> >  1 file changed, 1 insertion(+)
> >
> > diff --git a/drivers/base/dd.c b/drivers/base/dd.c index
> > 148e81969e04..8e4871aa34bc 100644
> > --- a/drivers/base/dd.c
> > +++ b/drivers/base/dd.c
> > @@ -612,6 +612,7 @@ static int really_probe(struct device *dev, struct
> > device_driver *drv)
> >         else if (drv->remove)
> >                 drv->remove(dev);
> >  probe_failed:
> > +       kfree(dev->dma_range_map);
> >         if (dev->bus)
> >                 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
> >
> > BUS_NOTIFY_DRIVER_NOT_BOUND, dev);
> > --
> > 2.17.1
> >
> > --
> 
> Hi Meng,
> 
> Sorry for the delay -- I was on vacation.  I agree with you -- thanks.
> However, note that in function  device_release()  in drivers/base/core.c
> there is this line:
> 
> kfree(dev->dma_range_map);
> 
> Won't this also be called  if all of the appropriate drivers' probes fail for this
> device, effecting a double kfree? 

If there are more than one driver compatible with the device, all the drivers will allocate memory for dma_range_map in function dev->bus->dma_configure().
If all the drivers probe failed, they all need to free memory. So, I think there is no double kfree case.

 Perhaps your patch could also set "dev-
> >dma_range_map" to NULL after calling kfree()?

I agree with you. Based on all drivers probe failed case, it is need to set dma_range_map  as NULL after calling kfree() to avoid the NULL pointer.
In additional, could you please help to check whether we also need to set dma_range_map  as NULL in device_release()?
Because I found out some other cases(for example, all drivers register failed) may free dma_range_map  multiple times.

Thanks,
Limeng


> 
> Thanks much,
> Jim Quinlan
> Broadcom STB

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

* RE: [PATCH] drivers core: Free dma_range_map when driver probe failed
  2021-01-04 15:55   ` Jim Quinlan
  2021-01-05  2:46     ` Li, Meng
@ 2021-01-05  4:33     ` Li, Meng
  1 sibling, 0 replies; 4+ messages in thread
From: Li, Meng @ 2021-01-05  4:33 UTC (permalink / raw)
  To: Jim Quinlan, Jim Quinlan, open list
  Cc: Greg Kroah-Hartman, rafael, Hao, Kexin, Christoph Hellwig



> -----Original Message-----
> From: Jim Quinlan <jim2101024@gmail.com>
> Sent: Monday, January 4, 2021 11:56 PM
> To: Jim Quinlan <james.quinlan@broadcom.com>; Li, Meng
> <Meng.Li@windriver.com>; open list <linux-kernel@vger.kernel.org>
> Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>; rafael@kernel.org;
> Hao, Kexin <Kexin.Hao@windriver.com>; Jim Quinlan
> <jim2101024@gmail.com>; Christoph Hellwig <hch@lst.de>
> Subject: Re: [PATCH] drivers core: Free dma_range_map when driver probe
> failed
> 
> > Subject: [PATCH] drivers core: Free dma_range_map when driver probe
> > failed
> >
> > There will be memory leak if driver probe failed. Trace as below:
> >   backtrace:
> >     [<000000002415258f>] kmemleak_alloc+0x3c/0x50
> >     [<00000000f447ebe4>] __kmalloc+0x208/0x530
> >     [<0000000048bc7b3a>] of_dma_get_range+0xe4/0x1b0
> >     [<0000000041e39065>] of_dma_configure_id+0x58/0x27c
> >     [<000000006356866a>] platform_dma_configure+0x2c/0x40
> >     ......
> >     [<000000000afcf9b5>] ret_from_fork+0x10/0x3c
> >
> > This issue is introduced by commit e0d072782c73("dma-mapping:
> > introduce DMA range map, supplanting dma_pfn_offset "). It doesn't
> > free dma_range_map when driver probe failed and cause above memory
> > leak. So, add code to free it in error path.
> >
> > Fixes: e0d072782c73("dma-mapping: introduce DMA range map,
> supplanting
> > dma_pfn_offset ")
> > Cc: stable@vger.kernel.org
> > Signed-off-by: Meng Li <Meng.Li@windriver.com>
> > ---
> >  drivers/base/dd.c | 1 +
> >  1 file changed, 1 insertion(+)
> >
> > diff --git a/drivers/base/dd.c b/drivers/base/dd.c index
> > 148e81969e04..8e4871aa34bc 100644
> > --- a/drivers/base/dd.c
> > +++ b/drivers/base/dd.c
> > @@ -612,6 +612,7 @@ static int really_probe(struct device *dev, struct
> > device_driver *drv)
> >         else if (drv->remove)
> >                 drv->remove(dev);
> >  probe_failed:
> > +       kfree(dev->dma_range_map);
> >         if (dev->bus)
> >                 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
> >
> > BUS_NOTIFY_DRIVER_NOT_BOUND, dev);
> > --
> > 2.17.1
> >
> > --
> 
> Hi Meng,
> 
> Sorry for the delay -- I was on vacation.  I agree with you -- thanks.
> However, note that in function  device_release()  in drivers/base/core.c
> there is this line:
> 
> kfree(dev->dma_range_map);
> 
> Won't this also be called  if all of the appropriate drivers' probes fail for this
> device, effecting a double kfree?  Perhaps your patch could also set "dev-
> >dma_range_map" to NULL after calling kfree()?
> 

Hi Jim,

Please discard my previous reply, because I had a misunderstand about what you suggested.
I agree with you total, and I will set dma_range_map as NULL in really_probe() function when driver probe failed.
I will sent V2 patch.

Thanks,
Limeng

> Thanks much,
> Jim Quinlan
> Broadcom STB

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

end of thread, other threads:[~2021-01-05  4:34 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-12-25  5:58 [PATCH] drivers core: Free dma_range_map when driver probe failed Meng.Li
     [not found] ` <CA+-6iNw1pixdamS1HSPiifrppWp=qsOmxVu6NYhDHr61pC6Gkw@mail.gmail.com>
2021-01-04 15:55   ` Jim Quinlan
2021-01-05  2:46     ` Li, Meng
2021-01-05  4:33     ` Li, Meng

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).