[dpdk-dev] eal/linuxapp: fix resource leak

Message ID 1462982465-129762-1-git-send-email-danielx.t.mrzyglod@intel.com (mailing list archive)
State Superseded, archived
Headers

Commit Message

Daniel Mrzyglod May 11, 2016, 4:01 p.m. UTC
  Fix issue reported by Coverity.
Coverity ID 97920

munmap structure of hugepage

leaked_storage: Variable hugepage going out of scope leaks the storage
it points to.

The system resource will not be reclaimed and reused, reducing the future
availability of the resource.

In rte_eal_hugepage_init: Leak of memory or pointers to system resources

Fixes: b6a468ad41d5 ("memory: add --socket-mem option")

Signed-off-by: Daniel Mrzyglod <danielx.t.mrzyglod@intel.com>
---
 lib/librte_eal/linuxapp/eal/eal_memory.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
  

Comments

Sergio Gonzalez Monroy May 12, 2016, 7:55 a.m. UTC | #1
Hi,

On 11/05/2016 17:01, Daniel Mrzyglod wrote:
> Fix issue reported by Coverity.
> Coverity ID 97920
>
> munmap structure of hugepage
>
> leaked_storage: Variable hugepage going out of scope leaks the storage
> it points to.
>
> The system resource will not be reclaimed and reused, reducing the future
> availability of the resource.

I'm not really fond of this commit messages, but if no one minds them, 
so be it.

> In rte_eal_hugepage_init: Leak of memory or pointers to system resources
>
> Fixes: b6a468ad41d5 ("memory: add --socket-mem option")
>
> Signed-off-by: Daniel Mrzyglod <danielx.t.mrzyglod@intel.com>
> ---
>   lib/librte_eal/linuxapp/eal/eal_memory.c | 4 +++-
>   1 file changed, 3 insertions(+), 1 deletion(-)
>
> diff --git a/lib/librte_eal/linuxapp/eal/eal_memory.c b/lib/librte_eal/linuxapp/eal/eal_memory.c
> index 5b9132c..cd40cc9 100644
> --- a/lib/librte_eal/linuxapp/eal/eal_memory.c
> +++ b/lib/librte_eal/linuxapp/eal/eal_memory.c
> @@ -1051,7 +1051,7 @@ int
>   rte_eal_hugepage_init(void)
>   {
>   	struct rte_mem_config *mcfg;
> -	struct hugepage_file *hugepage, *tmp_hp = NULL;
> +	struct hugepage_file *hugepage = NULL, *tmp_hp = NULL;
>   	struct hugepage_info used_hp[MAX_HUGEPAGE_SIZES];
>   
>   	uint64_t memory[RTE_MAX_NUMA_NODES];
> @@ -1374,6 +1374,8 @@ rte_eal_hugepage_init(void)
>   
>   fail:
>   	free(tmp_hp);
> +	if (hugepage != NULL)
> +		munmap(hugepage, nr_hugefiles * sizeof(struct hugepage_file));
>   	return -1;
>   }
>   

You missed the last conditional if(...0 of the function where it returns 
directly with error value.

Looking at the code a bit, we never check for anything other than < 0 
return value, so I'd just do a 'goto fail' instead.

Sergio
  
Thomas Monjalon May 13, 2016, 10:28 a.m. UTC | #2
2016-05-12 08:55, Sergio Gonzalez Monroy:
> On 11/05/2016 17:01, Daniel Mrzyglod wrote:
> > Fix issue reported by Coverity.
> > Coverity ID 97920
> >
> > munmap structure of hugepage
> >
> > leaked_storage: Variable hugepage going out of scope leaks the storage
> > it points to.
> >
> > The system resource will not be reclaimed and reused, reducing the future
> > availability of the resource.
> 
> I'm not really fond of this commit messages, but if no one minds them, 
> so be it.

Me too. It looks like a Machine2Machine message.
I'd prefer an explanation of what is wrong first, then an explanation of
the fix, and at the end, the Coverity ID (can be a tag like
Fixes-Coverity-ID: 97920).

> > In rte_eal_hugepage_init: Leak of memory or pointers to system resources
> >
> > Fixes: b6a468ad41d5 ("memory: add --socket-mem option")
> >
> > Signed-off-by: Daniel Mrzyglod <danielx.t.mrzyglod@intel.com>
  

Patch

diff --git a/lib/librte_eal/linuxapp/eal/eal_memory.c b/lib/librte_eal/linuxapp/eal/eal_memory.c
index 5b9132c..cd40cc9 100644
--- a/lib/librte_eal/linuxapp/eal/eal_memory.c
+++ b/lib/librte_eal/linuxapp/eal/eal_memory.c
@@ -1051,7 +1051,7 @@  int
 rte_eal_hugepage_init(void)
 {
 	struct rte_mem_config *mcfg;
-	struct hugepage_file *hugepage, *tmp_hp = NULL;
+	struct hugepage_file *hugepage = NULL, *tmp_hp = NULL;
 	struct hugepage_info used_hp[MAX_HUGEPAGE_SIZES];
 
 	uint64_t memory[RTE_MAX_NUMA_NODES];
@@ -1374,6 +1374,8 @@  rte_eal_hugepage_init(void)
 
 fail:
 	free(tmp_hp);
+	if (hugepage != NULL)
+		munmap(hugepage, nr_hugefiles * sizeof(struct hugepage_file));
 	return -1;
 }