From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtp.tuxdriver.com (charlotte.tuxdriver.com [70.61.120.58]) by dpdk.org (Postfix) with ESMTP id D8172AAD1 for ; Mon, 2 Apr 2018 14:07:19 +0200 (CEST) Received: from cpe-2606-a000-111b-40b7-640c-26a-4e16-9225.dyn6.twc.com ([2606:a000:111b:40b7:640c:26a:4e16:9225] helo=hmswarspite.think-freely.org) by smtp.tuxdriver.com with esmtpsa (TLSv1:AES256-SHA:256) (Exim 4.63) (envelope-from ) id 1f2yEp-000721-Lh; Mon, 02 Apr 2018 08:07:16 -0400 Received: from hmswarspite.think-freely.org (localhost [127.0.0.1]) by hmswarspite.think-freely.org (8.15.2/8.15.2) with ESMTP id w32C6UcS030451; Mon, 2 Apr 2018 08:06:30 -0400 Received: (from nhorman@localhost) by hmswarspite.think-freely.org (8.15.2/8.15.2/Submit) id w32C6TxH030449; Mon, 2 Apr 2018 08:06:29 -0400 From: Neil Horman To: dev@dpdk.org Cc: Neil Horman , Thomas Monjalon , Ferruh Yigit Date: Mon, 2 Apr 2018 08:06:19 -0400 Message-Id: <20180402120619.30404-1-nhorman@tuxdriver.com> X-Mailer: git-send-email 2.14.3 X-Spam-Score: -2.9 (--) X-Spam-Status: No Subject: [dpdk-dev] [PATCH] linuxapp eal: set fd to -1 for MAP_ANONYMOUS cases X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 02 Apr 2018 12:07:20 -0000 https://dpdk.org/tracker/show_bug.cgi?id=18 Indicated that several mmap call sites in the linuxapp eal code used an fd that was not -1 in their calls while using MAP_ANONYMOUS. While probably not a huge deal, the man page does say the fd should be -1 for portability, as some implementations don't ignore fd as they should for MAP_ANONYMOUS, and seting -1 here allows us to eliminate some code. Signed-off-by: Neil Horman CC: Thomas Monjalon CC: Ferruh Yigit --- lib/librte_eal/linuxapp/eal/eal_memory.c | 32 +++++++------------------------- 1 file changed, 7 insertions(+), 25 deletions(-) diff --git a/lib/librte_eal/linuxapp/eal/eal_memory.c b/lib/librte_eal/linuxapp/eal/eal_memory.c index 38853b753..acb553243 100644 --- a/lib/librte_eal/linuxapp/eal/eal_memory.c +++ b/lib/librte_eal/linuxapp/eal/eal_memory.c @@ -222,7 +222,7 @@ aslr_enabled(void) } /* - * Try to mmap *size bytes in /dev/zero. If it is successful, return the + * Try to mmap *size bytes. If it is successful, return the * pointer to the mmap'd area and keep *size unmodified. Else, retry * with a smaller zone: decrease *size by hugepage_sz until it reaches * 0. In this case, return NULL. Note: this function returns an address @@ -233,7 +233,6 @@ get_virtual_area(size_t *size, size_t hugepage_sz) { void *addr; void *addr_hint; - int fd; long aligned_addr; if (internal_config.base_virtaddr != 0) { @@ -248,11 +247,6 @@ get_virtual_area(size_t *size, size_t hugepage_sz) RTE_LOG(DEBUG, EAL, "Ask a virtual area of 0x%zx bytes\n", *size); - fd = open("/dev/zero", O_RDONLY); - if (fd < 0){ - RTE_LOG(ERR, EAL, "Cannot open /dev/zero\n"); - return NULL; - } do { addr = mmap(addr_hint, (*size) + hugepage_sz, PROT_READ, #ifdef RTE_ARCH_PPC_64 @@ -260,7 +254,7 @@ get_virtual_area(size_t *size, size_t hugepage_sz) #else MAP_PRIVATE, #endif - fd, 0); + -1, 0); if (addr == MAP_FAILED) { *size -= hugepage_sz; } else if (addr_hint != NULL && addr != addr_hint) { @@ -273,14 +267,12 @@ get_virtual_area(size_t *size, size_t hugepage_sz) } while (addr == MAP_FAILED && *size > 0); if (addr == MAP_FAILED) { - close(fd); RTE_LOG(ERR, EAL, "Cannot get a virtual area: %s\n", strerror(errno)); return NULL; } munmap(addr, (*size) + hugepage_sz); - close(fd); /* align addr to a huge page size boundary */ aligned_addr = (long)addr; @@ -1011,7 +1003,7 @@ rte_eal_hugepage_init(void) /* hugetlbfs can be disabled */ if (internal_config.no_hugetlbfs) { addr = mmap(NULL, internal_config.memory, PROT_READ | PROT_WRITE, - MAP_PRIVATE | MAP_ANONYMOUS, 0, 0); + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); if (addr == MAP_FAILED) { RTE_LOG(ERR, EAL, "%s: mmap() failed: %s\n", __func__, strerror(errno)); @@ -1339,7 +1331,7 @@ rte_eal_hugepage_attach(void) unsigned i, s = 0; /* s used to track the segment number */ unsigned max_seg = RTE_MAX_MEMSEG; off_t size = 0; - int fd, fd_zero = -1, fd_hugepage = -1; + int fd, fd_hugepage = -1; if (aslr_enabled() > 0) { RTE_LOG(WARNING, EAL, "WARNING: Address Space Layout Randomization " @@ -1350,11 +1342,6 @@ rte_eal_hugepage_attach(void) test_phys_addrs_available(); - fd_zero = open("/dev/zero", O_RDONLY); - if (fd_zero < 0) { - RTE_LOG(ERR, EAL, "Could not open /dev/zero\n"); - goto error; - } fd_hugepage = open(eal_hugepage_info_path(), O_RDONLY); if (fd_hugepage < 0) { RTE_LOG(ERR, EAL, "Could not open %s\n", eal_hugepage_info_path()); @@ -1373,8 +1360,6 @@ rte_eal_hugepage_attach(void) break; /* - * fdzero is mmapped to get a contiguous block of virtual - * addresses of the appropriate memseg size. * use mmap to get identical addresses as the primary process. */ base_addr = mmap(mcfg->memseg[s].addr, mcfg->memseg[s].len, @@ -1384,21 +1369,21 @@ rte_eal_hugepage_attach(void) #else MAP_PRIVATE, #endif - fd_zero, 0); + -1, 0); if (base_addr == MAP_FAILED || base_addr != mcfg->memseg[s].addr) { max_seg = s; if (base_addr != MAP_FAILED) { /* errno is stale, don't use */ RTE_LOG(ERR, EAL, "Could not mmap %llu bytes " - "in /dev/zero at [%p], got [%p] - " + "at [%p], got [%p] - " "please use '--base-virtaddr' option\n", (unsigned long long)mcfg->memseg[s].len, mcfg->memseg[s].addr, base_addr); munmap(base_addr, mcfg->memseg[s].len); } else { RTE_LOG(ERR, EAL, "Could not mmap %llu bytes " - "in /dev/zero at [%p]: '%s'\n", + "at [%p]: '%s'\n", (unsigned long long)mcfg->memseg[s].len, mcfg->memseg[s].addr, strerror(errno)); } @@ -1465,7 +1450,6 @@ rte_eal_hugepage_attach(void) } /* unmap the hugepage config file, since we are done using it */ munmap(hp, size); - close(fd_zero); close(fd_hugepage); return 0; @@ -1474,8 +1458,6 @@ rte_eal_hugepage_attach(void) munmap(mcfg->memseg[i].addr, mcfg->memseg[i].len); if (hp != NULL && hp != MAP_FAILED) munmap(hp, size); - if (fd_zero >= 0) - close(fd_zero); if (fd_hugepage >= 0) close(fd_hugepage); return -1; -- 2.14.3