[v3,1/2] eal: fix memory leak when removing event_cb

Message ID 1593690428-12708-1-git-send-email-wangyunjian@huawei.com (mailing list archive)
State Superseded, archived
Delegated to: David Marchand
Headers
Series [v3,1/2] eal: fix memory leak when removing event_cb |

Checks

Context Check Description
ci/checkpatch success coding style OK
ci/iol-broadcom-Performance success Performance Testing PASS
ci/iol-intel-Performance success Performance Testing PASS
ci/iol-mellanox-Performance success Performance Testing PASS
ci/iol-testing success Testing PASS

Commit Message

Yunjian Wang July 2, 2020, 11:47 a.m. UTC
  From: Yunjian Wang <wangyunjian@huawei.com>

The event_cb->dev_name is not freed when freeing event_cb,
and this causes a memory leak.

Fixes: a753e53d517b ("eal: add device event monitor framework")
Cc: stable@dpdk.org

Signed-off-by: Yunjian Wang <wangyunjian@huawei.com>
---
 lib/librte_eal/common/eal_common_dev.c | 2 ++
 1 file changed, 2 insertions(+)
  

Comments

Guo, Jia July 3, 2020, 6:04 a.m. UTC | #1
hi, yunjian

On 7/2/2020 7:47 PM, wangyunjian wrote:
> From: Yunjian Wang <wangyunjian@huawei.com>
>
> The event_cb->dev_name is not freed when freeing event_cb,
> and this causes a memory leak.
>
> Fixes: a753e53d517b ("eal: add device event monitor framework")
> Cc: stable@dpdk.org
>
> Signed-off-by: Yunjian Wang <wangyunjian@huawei.com>
> ---
>   lib/librte_eal/common/eal_common_dev.c | 2 ++
>   1 file changed, 2 insertions(+)
>
> diff --git a/lib/librte_eal/common/eal_common_dev.c b/lib/librte_eal/common/eal_common_dev.c
> index 9e4f09d..4cfdb80 100644
> --- a/lib/librte_eal/common/eal_common_dev.c
> +++ b/lib/librte_eal/common/eal_common_dev.c
> @@ -526,6 +526,8 @@ static int cmp_dev_name(const struct rte_device *dev, const void *_name)
>   		 */
>   		if (event_cb->active == 0) {
>   			TAILQ_REMOVE(&dev_event_cbs, event_cb, next);
> +			if (event_cb->dev_name)
> +				free(event_cb->dev_name);
>   			free(event_cb);
>   			ret++;
>   		} else {


After you check, don't you think the memory leak would not occur in 
rte_dev_event_callback_register when free event_cb? And if you have find 
other same problem, suggest to fix it wholly by this good chance. Thanks.

int
rte_dev_event_callback_register(const char *device_name,
                 rte_dev_event_cb_fn cb_fn,
                 void *cb_arg)
{

error:
     free(event_cb);
     rte_spinlock_unlock(&dev_event_lock);
     return ret;

}
  
Yunjian Wang July 3, 2020, 7 a.m. UTC | #2
Hi, Jeff

> -----Original Message-----
> From: Jeff Guo [mailto:jia.guo@intel.com]
> Sent: Friday, July 3, 2020 2:05 PM
> To: wangyunjian <wangyunjian@huawei.com>; dev@dpdk.org
> Cc: Lilijun (Jerry) <jerry.lilijun@huawei.com>; xudingke
> <xudingke@huawei.com>; stable@dpdk.org
> Subject: Re: [dpdk-dev] [PATCH v3 1/2] eal: fix memory leak when removing
> event_cb
> 
> hi, yunjian
> 
> On 7/2/2020 7:47 PM, wangyunjian wrote:
> > From: Yunjian Wang <wangyunjian@huawei.com>
> >
> > The event_cb->dev_name is not freed when freeing event_cb, and this
> > causes a memory leak.
> >
> > Fixes: a753e53d517b ("eal: add device event monitor framework")
> > Cc: stable@dpdk.org
> >
> > Signed-off-by: Yunjian Wang <wangyunjian@huawei.com>
> > ---
> >   lib/librte_eal/common/eal_common_dev.c | 2 ++
> >   1 file changed, 2 insertions(+)
> >
> > diff --git a/lib/librte_eal/common/eal_common_dev.c
> > b/lib/librte_eal/common/eal_common_dev.c
> > index 9e4f09d..4cfdb80 100644
> > --- a/lib/librte_eal/common/eal_common_dev.c
> > +++ b/lib/librte_eal/common/eal_common_dev.c
> > @@ -526,6 +526,8 @@ static int cmp_dev_name(const struct rte_device
> *dev, const void *_name)
> >   		 */
> >   		if (event_cb->active == 0) {
> >   			TAILQ_REMOVE(&dev_event_cbs, event_cb, next);
> > +			if (event_cb->dev_name)
> > +				free(event_cb->dev_name);
> >   			free(event_cb);
> >   			ret++;
> >   		} else {
> 
> 
> After you check, don't you think the memory leak would not occur in
> rte_dev_event_callback_register when free event_cb? And if you have find
> other same problem, suggest to fix it wholly by this good chance. Thanks.
> 

Yes, I've confirmed that it's not necessary. The 'event_cb->dev_name' is not allocated
memory on error path in rte_dev_event_callback_register(). But I find the return value
is wrong, when the callback is already exist, will include it in next version.

The similar bugs I found in other codes will be fixed in another patches.

Thanks,
Yunjian

> int
> rte_dev_event_callback_register(const char *device_name,
>                  rte_dev_event_cb_fn cb_fn,
>                  void *cb_arg)
> {
> 
> error:
>      free(event_cb);
>      rte_spinlock_unlock(&dev_event_lock);
>      return ret;
> 
> }
  
David Marchand July 3, 2020, 7:23 a.m. UTC | #3
On Thu, Jul 2, 2020 at 1:47 PM wangyunjian <wangyunjian@huawei.com> wrote:
>
> From: Yunjian Wang <wangyunjian@huawei.com>
>
> The event_cb->dev_name is not freed when freeing event_cb,
> and this causes a memory leak.
>
> Fixes: a753e53d517b ("eal: add device event monitor framework")
> Cc: stable@dpdk.org
>
> Signed-off-by: Yunjian Wang <wangyunjian@huawei.com>
> ---
>  lib/librte_eal/common/eal_common_dev.c | 2 ++
>  1 file changed, 2 insertions(+)
>
> diff --git a/lib/librte_eal/common/eal_common_dev.c b/lib/librte_eal/common/eal_common_dev.c
> index 9e4f09d..4cfdb80 100644
> --- a/lib/librte_eal/common/eal_common_dev.c
> +++ b/lib/librte_eal/common/eal_common_dev.c
> @@ -526,6 +526,8 @@ static int cmp_dev_name(const struct rte_device *dev, const void *_name)
>                  */
>                 if (event_cb->active == 0) {
>                         TAILQ_REMOVE(&dev_event_cbs, event_cb, next);
> +                       if (event_cb->dev_name)
> +                               free(event_cb->dev_name);

No need for the check, free handles a NULL pointer just fine.

Please, could you update your series/patches status in patchwork?
I am a bit lost at what is superseded or not.


Thanks.
  
Yunjian Wang July 3, 2020, 7:52 a.m. UTC | #4
> -----Original Message-----
> From: David Marchand [mailto:david.marchand@redhat.com]
> Sent: Friday, July 3, 2020 3:23 PM
> To: wangyunjian <wangyunjian@huawei.com>
> Cc: dev <dev@dpdk.org>; Jeff Guo <jia.guo@intel.com>; Lilijun (Jerry)
> <jerry.lilijun@huawei.com>; xudingke <xudingke@huawei.com>; dpdk stable
> <stable@dpdk.org>
> Subject: Re: [dpdk-stable] [dpdk-dev] [PATCH v3 1/2] eal: fix memory leak when
> removing event_cb
> 
> On Thu, Jul 2, 2020 at 1:47 PM wangyunjian <wangyunjian@huawei.com>
> wrote:
> >
> > From: Yunjian Wang <wangyunjian@huawei.com>
> >
> > The event_cb->dev_name is not freed when freeing event_cb, and this
> > causes a memory leak.
> >
> > Fixes: a753e53d517b ("eal: add device event monitor framework")
> > Cc: stable@dpdk.org
> >
> > Signed-off-by: Yunjian Wang <wangyunjian@huawei.com>
> > ---
> >  lib/librte_eal/common/eal_common_dev.c | 2 ++
> >  1 file changed, 2 insertions(+)
> >
> > diff --git a/lib/librte_eal/common/eal_common_dev.c
> > b/lib/librte_eal/common/eal_common_dev.c
> > index 9e4f09d..4cfdb80 100644
> > --- a/lib/librte_eal/common/eal_common_dev.c
> > +++ b/lib/librte_eal/common/eal_common_dev.c
> > @@ -526,6 +526,8 @@ static int cmp_dev_name(const struct rte_device
> *dev, const void *_name)
> >                  */
> >                 if (event_cb->active == 0) {
> >                         TAILQ_REMOVE(&dev_event_cbs, event_cb,
> next);
> > +                       if (event_cb->dev_name)
> > +                               free(event_cb->dev_name);
> 
> No need for the check, free handles a NULL pointer just fine.

Thanks for your suggestion, will send the v4 later.

> 
> Please, could you update your series/patches status in patchwork?
> I am a bit lost at what is superseded or not.

My mistake, please discard them.
https://patchwork.dpdk.org/patch/70824/
https://patchwork.dpdk.org/patch/70825/
https://patchwork.dpdk.org/patch/70826/
https://patchwork.dpdk.org/patch/72452/
https://patchwork.dpdk.org/patch/72825/
https://patchwork.dpdk.org/patch/72826/

Thanks,
Yunjian

> 
> 
> Thanks.
> 
> --
> David Marchand
  
David Marchand July 3, 2020, 8:01 a.m. UTC | #5
On Fri, Jul 3, 2020 at 9:52 AM wangyunjian <wangyunjian@huawei.com> wrote:
> > Please, could you update your series/patches status in patchwork?
> > I am a bit lost at what is superseded or not.
>
> My mistake, please discard them.
> https://patchwork.dpdk.org/patch/70824/
> https://patchwork.dpdk.org/patch/70825/
> https://patchwork.dpdk.org/patch/70826/
> https://patchwork.dpdk.org/patch/72452/
> https://patchwork.dpdk.org/patch/72825/
> https://patchwork.dpdk.org/patch/72826/

We have been doing this kind of cleanup with Thomas, Ferruh and other
maintainers for some time but this does not scale.
We waste time at figuring out which revision of patches is relevant,
or a duplicate, or a mistake...

I'll do it this time (again), but please register to patchwork and
handle this for your next patches.
Thanks.
  

Patch

diff --git a/lib/librte_eal/common/eal_common_dev.c b/lib/librte_eal/common/eal_common_dev.c
index 9e4f09d..4cfdb80 100644
--- a/lib/librte_eal/common/eal_common_dev.c
+++ b/lib/librte_eal/common/eal_common_dev.c
@@ -526,6 +526,8 @@  static int cmp_dev_name(const struct rte_device *dev, const void *_name)
 		 */
 		if (event_cb->active == 0) {
 			TAILQ_REMOVE(&dev_event_cbs, event_cb, next);
+			if (event_cb->dev_name)
+				free(event_cb->dev_name);
 			free(event_cb);
 			ret++;
 		} else {