From mboxrd@z Thu Jan 1 00:00:00 1970 From: Marcin Kerlin Subject: [PATCH v3 1/1] eal: fix resource leak of mapped memory Date: Wed, 15 Jun 2016 12:45:10 +0200 Message-ID: <1465987510-11491-1-git-send-email-marcinx.kerlin@intel.com> References: <1465918435-30973-1-git-send-email-marcinx.kerlin@intel.com> Cc: dev@dpdk.org, sergio.gonzalez.monroy@intel.com, Marcin Kerlin To: david.marchand@6wind.com Return-path: Received: from mga09.intel.com (mga09.intel.com [134.134.136.24]) by dpdk.org (Postfix) with ESMTP id 4C48BC3E2 for ; Wed, 15 Jun 2016 11:34:32 +0200 (CEST) In-Reply-To: <1465918435-30973-1-git-send-email-marcinx.kerlin@intel.com> List-Id: patches and discussions about DPDK List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" Patch fixes resource leak in rte_eal_hugepage_attach() where mapped files were not freed back to the OS in case of failure. Patch uses the behavior of Linux munmap: "It is not an error if the indicated range does not contain any mapped pages". V3: 1)removed redundant casting 2)removed update error message V2: 1)unmapping also previous addresses Coverity issue: 13295, 13296, 13303 Fixes: af75078fece3 ("first public release") Signed-off-by: Marcin Kerlin --- lib/librte_eal/linuxapp/eal/eal_memory.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/librte_eal/linuxapp/eal/eal_memory.c b/lib/librte_eal/linuxapp/eal/eal_memory.c index 79d1d2d..44ff8e1 100644 --- a/lib/librte_eal/linuxapp/eal/eal_memory.c +++ b/lib/librte_eal/linuxapp/eal/eal_memory.c @@ -1481,7 +1481,7 @@ rte_eal_hugepage_attach(void) size = getFileSize(fd_hugepage); hp = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd_hugepage, 0); - if (hp == NULL) { + if (hp == MAP_FAILED) { RTE_LOG(ERR, EAL, "Could not mmap %s\n", eal_hugepage_info_path()); goto error; } @@ -1545,12 +1545,19 @@ rte_eal_hugepage_attach(void) s++; } /* unmap the hugepage config file, since we are done using it */ - munmap((void *)(uintptr_t)hp, size); + munmap((void *)hp, size); close(fd_zero); close(fd_hugepage); return 0; error: + s = 0; + while (s < RTE_MAX_MEMSEG && mcfg->memseg[s].len > 0) { + munmap(mcfg->memseg[s].addr, mcfg->memseg[s].len); + s++; + } + if (hp != NULL && hp != MAP_FAILED) + munmap((void *)hp, size); if (fd_zero >= 0) close(fd_zero); if (fd_hugepage >= 0) -- 1.9.1