2012-09-04 13:54:00 +01:00
|
|
|
/*-
|
|
|
|
* BSD LICENSE
|
2014-06-04 00:42:50 +01:00
|
|
|
*
|
2014-02-10 11:46:50 +00:00
|
|
|
* Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
|
2012-09-04 13:54:00 +01:00
|
|
|
* All rights reserved.
|
2014-06-04 00:42:50 +01:00
|
|
|
*
|
2013-09-18 12:00:00 +02:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
2012-09-04 13:54:00 +01:00
|
|
|
* are met:
|
2014-06-04 00:42:50 +01:00
|
|
|
*
|
2013-09-18 12:00:00 +02:00
|
|
|
* * Redistributions of source code must retain the above copyright
|
2012-09-04 13:54:00 +01:00
|
|
|
* notice, this list of conditions and the following disclaimer.
|
2013-09-18 12:00:00 +02:00
|
|
|
* * Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
2012-09-04 13:54:00 +01:00
|
|
|
* distribution.
|
2013-09-18 12:00:00 +02:00
|
|
|
* * Neither the name of Intel Corporation nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived
|
2012-09-04 13:54:00 +01:00
|
|
|
* from this software without specific prior written permission.
|
2014-06-04 00:42:50 +01:00
|
|
|
*
|
2013-09-18 12:00:00 +02:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
2012-09-04 13:54:00 +01:00
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
2012-08-02 17:17:37 +02:00
|
|
|
/* BSD LICENSE
|
|
|
|
*
|
|
|
|
* Copyright(c) 2013 6WIND.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* * Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
* * Neither the name of 6WIND S.A. nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
2012-09-04 13:54:00 +01:00
|
|
|
|
2014-03-20 15:05:16 +01:00
|
|
|
#define _FILE_OFFSET_BITS 64
|
2012-09-04 13:54:00 +01:00
|
|
|
#include <errno.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <inttypes.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/queue.h>
|
2012-12-20 00:00:00 +01:00
|
|
|
#include <sys/file.h>
|
2012-09-04 13:54:00 +01:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/ioctl.h>
|
2012-12-20 00:00:00 +01:00
|
|
|
#include <sys/time.h>
|
2012-09-04 13:54:00 +01:00
|
|
|
|
|
|
|
#include <rte_log.h>
|
|
|
|
#include <rte_memory.h>
|
|
|
|
#include <rte_memzone.h>
|
|
|
|
#include <rte_launch.h>
|
|
|
|
#include <rte_eal.h>
|
2012-12-20 00:00:00 +01:00
|
|
|
#include <rte_eal_memconfig.h>
|
2012-09-04 13:54:00 +01:00
|
|
|
#include <rte_per_lcore.h>
|
|
|
|
#include <rte_lcore.h>
|
|
|
|
#include <rte_common.h>
|
|
|
|
#include <rte_string_fns.h>
|
|
|
|
|
|
|
|
#include "eal_private.h"
|
|
|
|
#include "eal_internal_cfg.h"
|
2012-12-20 00:00:00 +01:00
|
|
|
#include "eal_filesystem.h"
|
2012-09-04 13:54:00 +01:00
|
|
|
#include "eal_hugepages.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* Huge page mapping under linux
|
|
|
|
*
|
|
|
|
* To reserve a big contiguous amount of memory, we use the hugepage
|
|
|
|
* feature of linux. For that, we need to have hugetlbfs mounted. This
|
|
|
|
* code will create many files in this directory (one per page) and
|
|
|
|
* map them in virtual memory. For each page, we will retrieve its
|
|
|
|
* physical address and remap it in order to have a virtual contiguous
|
|
|
|
* zone as well as a physical contiguous zone.
|
|
|
|
*/
|
|
|
|
|
2014-02-12 13:38:45 +00:00
|
|
|
static uint64_t baseaddr_offset;
|
2012-09-04 13:54:00 +01:00
|
|
|
|
|
|
|
#define RANDOMIZE_VA_SPACE_FILE "/proc/sys/kernel/randomize_va_space"
|
|
|
|
|
2014-03-20 15:05:16 +01:00
|
|
|
/* Lock page in physical memory and prevent from swapping. */
|
|
|
|
int
|
|
|
|
rte_mem_lock_page(const void *virt)
|
|
|
|
{
|
|
|
|
unsigned long virtual = (unsigned long)virt;
|
|
|
|
int page_size = getpagesize();
|
|
|
|
unsigned long aligned = (virtual & ~ (page_size - 1));
|
|
|
|
return mlock((void*)aligned, page_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get physical address of any mapped virtual address in the current process.
|
|
|
|
*/
|
|
|
|
phys_addr_t
|
|
|
|
rte_mem_virt2phy(const void *virtaddr)
|
2014-02-11 10:28:51 +00:00
|
|
|
{
|
|
|
|
int fd;
|
2014-03-20 12:15:23 +01:00
|
|
|
uint64_t page, physaddr;
|
2014-02-11 10:28:51 +00:00
|
|
|
unsigned long virt_pfn;
|
|
|
|
int page_size;
|
2014-05-28 16:06:36 +08:00
|
|
|
off_t offset;
|
2014-02-11 10:28:51 +00:00
|
|
|
|
|
|
|
/* standard page size */
|
|
|
|
page_size = getpagesize();
|
|
|
|
|
|
|
|
fd = open("/proc/self/pagemap", O_RDONLY);
|
|
|
|
if (fd < 0) {
|
|
|
|
RTE_LOG(ERR, EAL, "%s(): cannot open /proc/self/pagemap: %s\n",
|
|
|
|
__func__, strerror(errno));
|
2014-03-20 15:05:16 +01:00
|
|
|
return RTE_BAD_PHYS_ADDR;
|
2014-02-11 10:28:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virt_pfn = (unsigned long)virtaddr / page_size;
|
|
|
|
offset = sizeof(uint64_t) * virt_pfn;
|
|
|
|
if (lseek(fd, offset, SEEK_SET) == (off_t) -1) {
|
|
|
|
RTE_LOG(ERR, EAL, "%s(): seek error in /proc/self/pagemap: %s\n",
|
|
|
|
__func__, strerror(errno));
|
|
|
|
close(fd);
|
2014-03-20 15:05:16 +01:00
|
|
|
return RTE_BAD_PHYS_ADDR;
|
2014-02-11 10:28:51 +00:00
|
|
|
}
|
|
|
|
if (read(fd, &page, sizeof(uint64_t)) < 0) {
|
|
|
|
RTE_LOG(ERR, EAL, "%s(): cannot read /proc/self/pagemap: %s\n",
|
|
|
|
__func__, strerror(errno));
|
|
|
|
close(fd);
|
2014-03-20 15:05:16 +01:00
|
|
|
return RTE_BAD_PHYS_ADDR;
|
2014-02-11 10:28:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* the pfn (page frame number) are bits 0-54 (see
|
|
|
|
* pagemap.txt in linux Documentation)
|
|
|
|
*/
|
2014-03-20 15:05:16 +01:00
|
|
|
physaddr = ((page & 0x7fffffffffffffULL) * page_size)
|
|
|
|
+ ((unsigned long)virtaddr % page_size);
|
2014-02-11 10:28:51 +00:00
|
|
|
close(fd);
|
|
|
|
return physaddr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* For each hugepage in hugepg_tbl, fill the physaddr value. We find
|
|
|
|
* it by browsing the /proc/self/pagemap special file.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
find_physaddrs(struct hugepage_file *hugepg_tbl, struct hugepage_info *hpi)
|
|
|
|
{
|
|
|
|
unsigned i;
|
|
|
|
phys_addr_t addr;
|
|
|
|
|
|
|
|
for (i = 0; i < hpi->num_pages[0]; i++) {
|
2014-03-20 15:05:16 +01:00
|
|
|
addr = rte_mem_virt2phy(hugepg_tbl[i].orig_va);
|
|
|
|
if (addr == RTE_BAD_PHYS_ADDR)
|
2014-02-11 10:28:51 +00:00
|
|
|
return -1;
|
|
|
|
hugepg_tbl[i].physaddr = addr;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-09-04 13:54:00 +01:00
|
|
|
/*
|
|
|
|
* Check whether address-space layout randomization is enabled in
|
|
|
|
* the kernel. This is important for multi-process as it can prevent
|
|
|
|
* two processes mapping data to the same virtual address
|
|
|
|
* Returns:
|
|
|
|
* 0 - address space randomization disabled
|
|
|
|
* 1/2 - address space randomization enabled
|
|
|
|
* negative error code on error
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
aslr_enabled(void)
|
|
|
|
{
|
|
|
|
char c;
|
|
|
|
int retval, fd = open(RANDOMIZE_VA_SPACE_FILE, O_RDONLY);
|
|
|
|
if (fd < 0)
|
|
|
|
return -errno;
|
|
|
|
retval = read(fd, &c, 1);
|
|
|
|
close(fd);
|
|
|
|
if (retval < 0)
|
|
|
|
return -errno;
|
|
|
|
if (retval == 0)
|
|
|
|
return -EIO;
|
|
|
|
switch (c) {
|
|
|
|
case '0' : return 0;
|
|
|
|
case '1' : return 1;
|
|
|
|
case '2' : return 2;
|
|
|
|
default: return -EINVAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2014-05-02 16:42:52 -07:00
|
|
|
* Try to mmap *size bytes in /dev/zero. If it is successful, return the
|
2012-09-04 13:54:00 +01:00
|
|
|
* 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
|
|
|
|
* which is a multiple of hugepage size.
|
|
|
|
*/
|
|
|
|
static void *
|
2013-06-03 00:00:00 +00:00
|
|
|
get_virtual_area(size_t *size, size_t hugepage_sz)
|
2012-09-04 13:54:00 +01:00
|
|
|
{
|
|
|
|
void *addr;
|
|
|
|
int fd;
|
|
|
|
long aligned_addr;
|
|
|
|
|
2014-02-12 13:38:45 +00:00
|
|
|
if (internal_config.base_virtaddr != 0) {
|
|
|
|
addr = (void*) (uintptr_t) (internal_config.base_virtaddr +
|
|
|
|
baseaddr_offset);
|
|
|
|
}
|
|
|
|
else addr = NULL;
|
|
|
|
|
|
|
|
RTE_LOG(INFO, EAL, "Ask a virtual area of 0x%zx bytes\n", *size);
|
2012-09-04 13:54:00 +01:00
|
|
|
|
|
|
|
fd = open("/dev/zero", O_RDONLY);
|
|
|
|
if (fd < 0){
|
|
|
|
RTE_LOG(ERR, EAL, "Cannot open /dev/zero\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
do {
|
2014-02-12 13:38:45 +00:00
|
|
|
addr = mmap(addr,
|
|
|
|
(*size) + hugepage_sz, PROT_READ, MAP_PRIVATE, fd, 0);
|
2012-09-04 13:54:00 +01:00
|
|
|
if (addr == MAP_FAILED)
|
|
|
|
*size -= hugepage_sz;
|
|
|
|
} while (addr == MAP_FAILED && *size > 0);
|
|
|
|
|
|
|
|
if (addr == MAP_FAILED) {
|
|
|
|
close(fd);
|
|
|
|
RTE_LOG(INFO, EAL, "Cannot get a virtual area\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
munmap(addr, (*size) + hugepage_sz);
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
/* align addr to a huge page size boundary */
|
|
|
|
aligned_addr = (long)addr;
|
|
|
|
aligned_addr += (hugepage_sz - 1);
|
|
|
|
aligned_addr &= (~(hugepage_sz - 1));
|
|
|
|
addr = (void *)(aligned_addr);
|
|
|
|
|
2013-06-03 00:00:00 +00:00
|
|
|
RTE_LOG(INFO, EAL, "Virtual area found at %p (size = 0x%zx)\n",
|
2012-09-04 13:54:00 +01:00
|
|
|
addr, *size);
|
|
|
|
|
2014-02-12 13:38:45 +00:00
|
|
|
/* increment offset */
|
|
|
|
baseaddr_offset += *size;
|
|
|
|
|
2012-09-04 13:54:00 +01:00
|
|
|
return addr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Mmap all hugepages of hugepage table: it first open a file in
|
|
|
|
* hugetlbfs, then mmap() hugepage_sz data in it. If orig is set, the
|
|
|
|
* virtual address is stored in hugepg_tbl[i].orig_va, else it is stored
|
|
|
|
* in hugepg_tbl[i].final_va. The second mapping (when orig is 0) tries to
|
|
|
|
* map continguous physical blocks in contiguous virtual blocks.
|
|
|
|
*/
|
|
|
|
static int
|
2014-02-11 10:28:51 +00:00
|
|
|
map_all_hugepages(struct hugepage_file *hugepg_tbl,
|
2012-09-04 13:54:00 +01:00
|
|
|
struct hugepage_info *hpi, int orig)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
unsigned i;
|
|
|
|
void *virtaddr;
|
|
|
|
void *vma_addr = NULL;
|
2013-06-03 00:00:00 +00:00
|
|
|
size_t vma_len = 0;
|
2012-09-04 13:54:00 +01:00
|
|
|
|
2014-02-11 10:28:51 +00:00
|
|
|
#ifdef RTE_EAL_SINGLE_FILE_SEGMENTS
|
|
|
|
RTE_SET_USED(vma_len);
|
|
|
|
#endif
|
|
|
|
|
2012-12-20 00:00:00 +01:00
|
|
|
for (i = 0; i < hpi->num_pages[0]; i++) {
|
2014-12-10 18:46:41 +08:00
|
|
|
uint64_t hugepage_sz = hpi->hugepage_sz;
|
2012-09-04 13:54:00 +01:00
|
|
|
|
|
|
|
if (orig) {
|
|
|
|
hugepg_tbl[i].file_id = i;
|
|
|
|
hugepg_tbl[i].size = hugepage_sz;
|
2014-02-11 10:28:51 +00:00
|
|
|
#ifdef RTE_EAL_SINGLE_FILE_SEGMENTS
|
|
|
|
eal_get_hugefile_temp_path(hugepg_tbl[i].filepath,
|
|
|
|
sizeof(hugepg_tbl[i].filepath), hpi->hugedir,
|
|
|
|
hugepg_tbl[i].file_id);
|
|
|
|
#else
|
2012-09-04 13:54:00 +01:00
|
|
|
eal_get_hugefile_path(hugepg_tbl[i].filepath,
|
|
|
|
sizeof(hugepg_tbl[i].filepath), hpi->hugedir,
|
|
|
|
hugepg_tbl[i].file_id);
|
2014-02-11 10:28:51 +00:00
|
|
|
#endif
|
2012-09-04 13:54:00 +01:00
|
|
|
hugepg_tbl[i].filepath[sizeof(hugepg_tbl[i].filepath) - 1] = '\0';
|
|
|
|
}
|
2014-11-25 17:17:15 -05:00
|
|
|
#ifndef RTE_ARCH_64
|
|
|
|
/* for 32-bit systems, don't remap 1G and 16G pages, just reuse
|
|
|
|
* original map address as final map address.
|
2012-09-04 13:54:00 +01:00
|
|
|
*/
|
2014-11-25 17:17:15 -05:00
|
|
|
else if ((hugepage_sz == RTE_PGSIZE_1G)
|
|
|
|
|| (hugepage_sz == RTE_PGSIZE_16G)) {
|
2012-09-04 13:54:00 +01:00
|
|
|
hugepg_tbl[i].final_va = hugepg_tbl[i].orig_va;
|
|
|
|
hugepg_tbl[i].orig_va = NULL;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
#endif
|
2014-02-11 10:28:51 +00:00
|
|
|
|
|
|
|
#ifndef RTE_EAL_SINGLE_FILE_SEGMENTS
|
2012-09-04 13:54:00 +01:00
|
|
|
else if (vma_len == 0) {
|
|
|
|
unsigned j, num_pages;
|
|
|
|
|
|
|
|
/* reserve a virtual area for next contiguous
|
|
|
|
* physical block: count the number of
|
|
|
|
* contiguous physical pages. */
|
2012-12-20 00:00:00 +01:00
|
|
|
for (j = i+1; j < hpi->num_pages[0] ; j++) {
|
2014-11-25 17:17:15 -05:00
|
|
|
#ifdef RTE_ARCH_PPC_64
|
|
|
|
/* The physical addresses are sorted in
|
|
|
|
* descending order on PPC64 */
|
|
|
|
if (hugepg_tbl[j].physaddr !=
|
|
|
|
hugepg_tbl[j-1].physaddr - hugepage_sz)
|
|
|
|
break;
|
|
|
|
#else
|
2012-09-04 13:54:00 +01:00
|
|
|
if (hugepg_tbl[j].physaddr !=
|
|
|
|
hugepg_tbl[j-1].physaddr + hugepage_sz)
|
|
|
|
break;
|
2014-11-25 17:17:15 -05:00
|
|
|
#endif
|
2012-09-04 13:54:00 +01:00
|
|
|
}
|
|
|
|
num_pages = j - i;
|
|
|
|
vma_len = num_pages * hugepage_sz;
|
|
|
|
|
|
|
|
/* get the biggest virtual memory area up to
|
|
|
|
* vma_len. If it fails, vma_addr is NULL, so
|
|
|
|
* let the kernel provide the address. */
|
|
|
|
vma_addr = get_virtual_area(&vma_len, hpi->hugepage_sz);
|
|
|
|
if (vma_addr == NULL)
|
|
|
|
vma_len = hugepage_sz;
|
|
|
|
}
|
2014-02-11 10:28:51 +00:00
|
|
|
#endif
|
2012-09-04 13:54:00 +01:00
|
|
|
|
2012-12-20 00:00:00 +01:00
|
|
|
/* try to create hugepage file */
|
2012-09-04 13:54:00 +01:00
|
|
|
fd = open(hugepg_tbl[i].filepath, O_CREAT | O_RDWR, 0755);
|
|
|
|
if (fd < 0) {
|
2012-12-20 00:00:00 +01:00
|
|
|
RTE_LOG(ERR, EAL, "%s(): open failed: %s\n", __func__,
|
2012-09-04 13:54:00 +01:00
|
|
|
strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtaddr = mmap(vma_addr, hugepage_sz, PROT_READ | PROT_WRITE,
|
|
|
|
MAP_SHARED, fd, 0);
|
|
|
|
if (virtaddr == MAP_FAILED) {
|
2012-12-20 00:00:00 +01:00
|
|
|
RTE_LOG(ERR, EAL, "%s(): mmap failed: %s\n", __func__,
|
2012-09-04 13:54:00 +01:00
|
|
|
strerror(errno));
|
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
2012-12-20 00:00:00 +01:00
|
|
|
|
2012-09-04 13:54:00 +01:00
|
|
|
if (orig) {
|
|
|
|
hugepg_tbl[i].orig_va = virtaddr;
|
|
|
|
memset(virtaddr, 0, hugepage_sz);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
hugepg_tbl[i].final_va = virtaddr;
|
|
|
|
}
|
|
|
|
|
2013-06-03 00:00:00 +00:00
|
|
|
/* set shared flock on the file. */
|
|
|
|
if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
|
|
|
|
RTE_LOG(ERR, EAL, "%s(): Locking file failed:%s \n",
|
|
|
|
__func__, strerror(errno));
|
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-12-20 00:00:00 +01:00
|
|
|
close(fd);
|
|
|
|
|
2012-09-04 13:54:00 +01:00
|
|
|
vma_addr = (char *)vma_addr + hugepage_sz;
|
|
|
|
vma_len -= hugepage_sz;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-02-11 10:28:51 +00:00
|
|
|
#ifdef RTE_EAL_SINGLE_FILE_SEGMENTS
|
2012-09-04 13:54:00 +01:00
|
|
|
|
|
|
|
/*
|
2014-02-11 10:28:51 +00:00
|
|
|
* Remaps all hugepages into single file segments
|
2012-09-04 13:54:00 +01:00
|
|
|
*/
|
|
|
|
static int
|
2014-02-11 10:28:51 +00:00
|
|
|
remap_all_hugepages(struct hugepage_file *hugepg_tbl, struct hugepage_info *hpi)
|
2012-09-04 13:54:00 +01:00
|
|
|
{
|
|
|
|
int fd;
|
2014-02-11 10:28:51 +00:00
|
|
|
unsigned i = 0, j, num_pages, page_idx = 0;
|
|
|
|
void *vma_addr = NULL, *old_addr = NULL, *page_addr = NULL;
|
|
|
|
size_t vma_len = 0;
|
|
|
|
size_t hugepage_sz = hpi->hugepage_sz;
|
|
|
|
size_t total_size, offset;
|
|
|
|
char filepath[MAX_HUGEPAGE_PATH];
|
|
|
|
phys_addr_t physaddr;
|
|
|
|
int socket;
|
2012-09-04 13:54:00 +01:00
|
|
|
|
2014-02-11 10:28:51 +00:00
|
|
|
while (i < hpi->num_pages[0]) {
|
2012-09-04 13:54:00 +01:00
|
|
|
|
2014-11-25 17:17:15 -05:00
|
|
|
#ifndef RTE_ARCH_64
|
|
|
|
/* for 32-bit systems, don't remap 1G pages and 16G pages,
|
|
|
|
* just reuse original map address as final map address.
|
2014-02-11 10:28:51 +00:00
|
|
|
*/
|
2014-11-25 17:17:15 -05:00
|
|
|
if ((hugepage_sz == RTE_PGSIZE_1G)
|
|
|
|
|| (hugepage_sz == RTE_PGSIZE_16G)) {
|
2014-02-11 10:28:51 +00:00
|
|
|
hugepg_tbl[i].final_va = hugepg_tbl[i].orig_va;
|
|
|
|
hugepg_tbl[i].orig_va = NULL;
|
|
|
|
i++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
#endif
|
2012-09-04 13:54:00 +01:00
|
|
|
|
2014-02-11 10:28:51 +00:00
|
|
|
/* reserve a virtual area for next contiguous
|
|
|
|
* physical block: count the number of
|
|
|
|
* contiguous physical pages. */
|
|
|
|
for (j = i+1; j < hpi->num_pages[0] ; j++) {
|
2014-11-25 17:17:15 -05:00
|
|
|
#ifdef RTE_ARCH_PPC_64
|
|
|
|
/* The physical addresses are sorted in descending
|
|
|
|
* order on PPC64 */
|
|
|
|
if (hugepg_tbl[j].physaddr !=
|
|
|
|
hugepg_tbl[j-1].physaddr - hugepage_sz)
|
2014-02-11 10:28:51 +00:00
|
|
|
break;
|
2014-11-25 17:17:15 -05:00
|
|
|
#else
|
|
|
|
if (hugepg_tbl[j].physaddr !=
|
|
|
|
hugepg_tbl[j-1].physaddr + hugepage_sz)
|
|
|
|
break;
|
|
|
|
#endif
|
2014-02-11 10:28:51 +00:00
|
|
|
}
|
|
|
|
num_pages = j - i;
|
|
|
|
vma_len = num_pages * hugepage_sz;
|
|
|
|
|
|
|
|
socket = hugepg_tbl[i].socket_id;
|
|
|
|
|
|
|
|
/* get the biggest virtual memory area up to
|
|
|
|
* vma_len. If it fails, vma_addr is NULL, so
|
|
|
|
* let the kernel provide the address. */
|
|
|
|
vma_addr = get_virtual_area(&vma_len, hpi->hugepage_sz);
|
|
|
|
|
|
|
|
/* If we can't find a big enough virtual area, work out how many pages
|
|
|
|
* we are going to get */
|
|
|
|
if (vma_addr == NULL)
|
|
|
|
j = i + 1;
|
|
|
|
else if (vma_len != num_pages * hugepage_sz) {
|
|
|
|
num_pages = vma_len / hugepage_sz;
|
|
|
|
j = i + num_pages;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
hugepg_tbl[page_idx].file_id = page_idx;
|
|
|
|
eal_get_hugefile_path(filepath,
|
|
|
|
sizeof(filepath),
|
|
|
|
hpi->hugedir,
|
|
|
|
hugepg_tbl[page_idx].file_id);
|
|
|
|
|
|
|
|
/* try to create hugepage file */
|
|
|
|
fd = open(filepath, O_CREAT | O_RDWR, 0755);
|
|
|
|
if (fd < 0) {
|
|
|
|
RTE_LOG(ERR, EAL, "%s(): open failed: %s\n", __func__, strerror(errno));
|
2012-09-04 13:54:00 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2014-02-11 10:28:51 +00:00
|
|
|
|
|
|
|
total_size = 0;
|
|
|
|
for (;i < j; i++) {
|
|
|
|
|
|
|
|
/* unmap current segment */
|
|
|
|
if (total_size > 0)
|
|
|
|
munmap(vma_addr, total_size);
|
|
|
|
|
|
|
|
/* unmap original page */
|
|
|
|
munmap(hugepg_tbl[i].orig_va, hugepage_sz);
|
|
|
|
unlink(hugepg_tbl[i].filepath);
|
|
|
|
|
|
|
|
total_size += hugepage_sz;
|
|
|
|
|
|
|
|
old_addr = vma_addr;
|
|
|
|
|
|
|
|
/* map new, bigger segment */
|
|
|
|
vma_addr = mmap(vma_addr, total_size,
|
|
|
|
PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
|
|
|
|
|
|
|
|
if (vma_addr == MAP_FAILED || vma_addr != old_addr) {
|
|
|
|
RTE_LOG(ERR, EAL, "%s(): mmap failed: %s\n", __func__, strerror(errno));
|
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* touch the page. this is needed because kernel postpones mapping
|
|
|
|
* creation until the first page fault. with this, we pin down
|
|
|
|
* the page and it is marked as used and gets into process' pagemap.
|
|
|
|
*/
|
|
|
|
for (offset = 0; offset < total_size; offset += hugepage_sz)
|
|
|
|
*((volatile uint8_t*) RTE_PTR_ADD(vma_addr, offset));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set shared flock on the file. */
|
|
|
|
if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
|
|
|
|
RTE_LOG(ERR, EAL, "%s(): Locking file failed:%s \n",
|
|
|
|
__func__, strerror(errno));
|
2012-09-04 13:54:00 +01:00
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-06-24 11:15:28 -07:00
|
|
|
snprintf(hugepg_tbl[page_idx].filepath, MAX_HUGEPAGE_PATH, "%s",
|
2014-02-11 10:28:51 +00:00
|
|
|
filepath);
|
|
|
|
|
2014-03-20 15:05:16 +01:00
|
|
|
physaddr = rte_mem_virt2phy(vma_addr);
|
2014-02-11 10:28:51 +00:00
|
|
|
|
2014-03-20 15:05:16 +01:00
|
|
|
if (physaddr == RTE_BAD_PHYS_ADDR)
|
2014-02-11 10:28:51 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
hugepg_tbl[page_idx].final_va = vma_addr;
|
|
|
|
|
|
|
|
hugepg_tbl[page_idx].physaddr = physaddr;
|
|
|
|
|
|
|
|
hugepg_tbl[page_idx].repeated = num_pages;
|
|
|
|
|
|
|
|
hugepg_tbl[page_idx].socket_id = socket;
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
/* verify the memory segment - that is, check that every VA corresponds
|
|
|
|
* to the physical address we expect to see
|
2012-09-04 13:54:00 +01:00
|
|
|
*/
|
2014-02-11 10:28:51 +00:00
|
|
|
for (offset = 0; offset < vma_len; offset += hugepage_sz) {
|
|
|
|
uint64_t expected_physaddr;
|
|
|
|
|
|
|
|
expected_physaddr = hugepg_tbl[page_idx].physaddr + offset;
|
|
|
|
page_addr = RTE_PTR_ADD(vma_addr, offset);
|
2014-03-20 15:05:16 +01:00
|
|
|
physaddr = rte_mem_virt2phy(page_addr);
|
2014-02-11 10:28:51 +00:00
|
|
|
|
|
|
|
if (physaddr != expected_physaddr) {
|
|
|
|
RTE_LOG(ERR, EAL, "Segment sanity check failed: wrong physaddr "
|
|
|
|
"at %p (offset 0x%" PRIx64 ": 0x%" PRIx64
|
|
|
|
" (expected 0x%" PRIx64 ")\n",
|
|
|
|
page_addr, offset, physaddr, expected_physaddr);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* zero out the whole segment */
|
|
|
|
memset(hugepg_tbl[page_idx].final_va, 0, total_size);
|
|
|
|
|
|
|
|
page_idx++;
|
2012-09-04 13:54:00 +01:00
|
|
|
}
|
2014-02-11 10:28:51 +00:00
|
|
|
|
|
|
|
/* zero out the rest */
|
|
|
|
memset(&hugepg_tbl[page_idx], 0, (hpi->num_pages[0] - page_idx) * sizeof(struct hugepage_file));
|
|
|
|
return page_idx;
|
2012-09-04 13:54:00 +01:00
|
|
|
}
|
2014-02-11 10:28:51 +00:00
|
|
|
#else/* RTE_EAL_SINGLE_FILE_SEGMENTS=n */
|
|
|
|
|
|
|
|
/* Unmap all hugepages from original mapping */
|
|
|
|
static int
|
|
|
|
unmap_all_hugepages_orig(struct hugepage_file *hugepg_tbl, struct hugepage_info *hpi)
|
|
|
|
{
|
|
|
|
unsigned i;
|
|
|
|
for (i = 0; i < hpi->num_pages[0]; i++) {
|
|
|
|
if (hugepg_tbl[i].orig_va) {
|
|
|
|
munmap(hugepg_tbl[i].orig_va, hpi->hugepage_sz);
|
|
|
|
hugepg_tbl[i].orig_va = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif /* RTE_EAL_SINGLE_FILE_SEGMENTS */
|
2012-09-04 13:54:00 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Parse /proc/self/numa_maps to get the NUMA socket ID for each huge
|
|
|
|
* page.
|
|
|
|
*/
|
|
|
|
static int
|
2014-02-11 10:28:51 +00:00
|
|
|
find_numasocket(struct hugepage_file *hugepg_tbl, struct hugepage_info *hpi)
|
2012-09-04 13:54:00 +01:00
|
|
|
{
|
|
|
|
int socket_id;
|
|
|
|
char *end, *nodestr;
|
|
|
|
unsigned i, hp_count = 0;
|
|
|
|
uint64_t virt_addr;
|
|
|
|
char buf[BUFSIZ];
|
|
|
|
char hugedir_str[PATH_MAX];
|
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
f = fopen("/proc/self/numa_maps", "r");
|
|
|
|
if (f == NULL) {
|
|
|
|
RTE_LOG(INFO, EAL, "cannot open /proc/self/numa_maps,"
|
2012-12-20 00:00:00 +01:00
|
|
|
" consider that all memory is in socket_id 0\n");
|
2012-09-04 13:54:00 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-06-24 11:15:28 -07:00
|
|
|
snprintf(hugedir_str, sizeof(hugedir_str),
|
2014-12-23 19:23:17 +02:00
|
|
|
"%s/%s", hpi->hugedir, internal_config.hugefile_prefix);
|
2012-09-04 13:54:00 +01:00
|
|
|
|
|
|
|
/* parse numa map */
|
|
|
|
while (fgets(buf, sizeof(buf), f) != NULL) {
|
|
|
|
|
|
|
|
/* ignore non huge page */
|
|
|
|
if (strstr(buf, " huge ") == NULL &&
|
|
|
|
strstr(buf, hugedir_str) == NULL)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* get zone addr */
|
|
|
|
virt_addr = strtoull(buf, &end, 16);
|
|
|
|
if (virt_addr == 0 || end == buf) {
|
|
|
|
RTE_LOG(ERR, EAL, "%s(): error in numa_maps parsing\n", __func__);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get node id (socket id) */
|
|
|
|
nodestr = strstr(buf, " N");
|
|
|
|
if (nodestr == NULL) {
|
|
|
|
RTE_LOG(ERR, EAL, "%s(): error in numa_maps parsing\n", __func__);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
nodestr += 2;
|
|
|
|
end = strstr(nodestr, "=");
|
|
|
|
if (end == NULL) {
|
|
|
|
RTE_LOG(ERR, EAL, "%s(): error in numa_maps parsing\n", __func__);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
end[0] = '\0';
|
|
|
|
end = NULL;
|
|
|
|
|
|
|
|
socket_id = strtoul(nodestr, &end, 0);
|
|
|
|
if ((nodestr[0] == '\0') || (end == NULL) || (*end != '\0')) {
|
|
|
|
RTE_LOG(ERR, EAL, "%s(): error in numa_maps parsing\n", __func__);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if we find this page in our mappings, set socket_id */
|
2012-12-20 00:00:00 +01:00
|
|
|
for (i = 0; i < hpi->num_pages[0]; i++) {
|
2012-09-04 13:54:00 +01:00
|
|
|
void *va = (void *)(unsigned long)virt_addr;
|
|
|
|
if (hugepg_tbl[i].orig_va == va) {
|
|
|
|
hugepg_tbl[i].socket_id = socket_id;
|
|
|
|
hp_count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-12-20 00:00:00 +01:00
|
|
|
|
|
|
|
if (hp_count < hpi->num_pages[0])
|
2012-09-04 13:54:00 +01:00
|
|
|
goto error;
|
2012-12-20 00:00:00 +01:00
|
|
|
|
2012-09-04 13:54:00 +01:00
|
|
|
fclose(f);
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
error:
|
|
|
|
fclose(f);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2014-11-25 17:17:15 -05:00
|
|
|
* Sort the hugepg_tbl by physical address (lower addresses first on x86,
|
|
|
|
* higher address first on powerpc). We use a slow algorithm, but we won't
|
|
|
|
* have millions of pages, and this is only done at init time.
|
2012-09-04 13:54:00 +01:00
|
|
|
*/
|
|
|
|
static int
|
2014-02-11 10:28:51 +00:00
|
|
|
sort_by_physaddr(struct hugepage_file *hugepg_tbl, struct hugepage_info *hpi)
|
2012-09-04 13:54:00 +01:00
|
|
|
{
|
|
|
|
unsigned i, j;
|
2014-11-25 17:17:15 -05:00
|
|
|
int compare_idx;
|
|
|
|
uint64_t compare_addr;
|
2014-02-11 10:28:51 +00:00
|
|
|
struct hugepage_file tmp;
|
2012-09-04 13:54:00 +01:00
|
|
|
|
2012-12-20 00:00:00 +01:00
|
|
|
for (i = 0; i < hpi->num_pages[0]; i++) {
|
2014-11-25 17:17:15 -05:00
|
|
|
compare_addr = 0;
|
|
|
|
compare_idx = -1;
|
2012-09-04 13:54:00 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* browse all entries starting at 'i', and find the
|
|
|
|
* entry with the smallest addr
|
|
|
|
*/
|
2012-12-20 00:00:00 +01:00
|
|
|
for (j=i; j< hpi->num_pages[0]; j++) {
|
2012-09-04 13:54:00 +01:00
|
|
|
|
2014-11-25 17:17:15 -05:00
|
|
|
if (compare_addr == 0 ||
|
|
|
|
#ifdef RTE_ARCH_PPC_64
|
|
|
|
hugepg_tbl[j].physaddr > compare_addr) {
|
|
|
|
#else
|
|
|
|
hugepg_tbl[j].physaddr < compare_addr) {
|
|
|
|
#endif
|
|
|
|
compare_addr = hugepg_tbl[j].physaddr;
|
|
|
|
compare_idx = j;
|
2012-09-04 13:54:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* should not happen */
|
2014-11-25 17:17:15 -05:00
|
|
|
if (compare_idx == -1) {
|
2012-09-04 13:54:00 +01:00
|
|
|
RTE_LOG(ERR, EAL, "%s(): error in physaddr sorting\n", __func__);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* swap the 2 entries in the table */
|
2014-11-25 17:17:15 -05:00
|
|
|
memcpy(&tmp, &hugepg_tbl[compare_idx],
|
|
|
|
sizeof(struct hugepage_file));
|
|
|
|
memcpy(&hugepg_tbl[compare_idx], &hugepg_tbl[i],
|
|
|
|
sizeof(struct hugepage_file));
|
2014-02-11 10:28:51 +00:00
|
|
|
memcpy(&hugepg_tbl[i], &tmp, sizeof(struct hugepage_file));
|
2012-09-04 13:54:00 +01:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Uses mmap to create a shared memory area for storage of data
|
2012-12-20 00:00:00 +01:00
|
|
|
* Used in this file to store the hugepage file map on disk
|
2012-09-04 13:54:00 +01:00
|
|
|
*/
|
|
|
|
static void *
|
|
|
|
create_shared_memory(const char *filename, const size_t mem_size)
|
|
|
|
{
|
|
|
|
void *retval;
|
|
|
|
int fd = open(filename, O_CREAT | O_RDWR, 0666);
|
|
|
|
if (fd < 0)
|
|
|
|
return NULL;
|
|
|
|
if (ftruncate(fd, mem_size) < 0) {
|
|
|
|
close(fd);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
retval = mmap(NULL, mem_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
|
|
|
|
close(fd);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2012-12-20 00:00:00 +01:00
|
|
|
* this copies *active* hugepages from one hugepage table to another.
|
|
|
|
* destination is typically the shared memory.
|
|
|
|
*/
|
|
|
|
static int
|
2014-02-11 10:28:51 +00:00
|
|
|
copy_hugepages_to_shared_mem(struct hugepage_file * dst, int dest_size,
|
|
|
|
const struct hugepage_file * src, int src_size)
|
2012-12-20 00:00:00 +01:00
|
|
|
{
|
|
|
|
int src_pos, dst_pos = 0;
|
|
|
|
|
|
|
|
for (src_pos = 0; src_pos < src_size; src_pos++) {
|
|
|
|
if (src[src_pos].final_va != NULL) {
|
|
|
|
/* error on overflow attempt */
|
|
|
|
if (dst_pos == dest_size)
|
|
|
|
return -1;
|
2014-02-11 10:28:51 +00:00
|
|
|
memcpy(&dst[dst_pos], &src[src_pos], sizeof(struct hugepage_file));
|
2012-12-20 00:00:00 +01:00
|
|
|
dst_pos++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* unmaps hugepages that are not going to be used. since we originally allocate
|
|
|
|
* ALL hugepages (not just those we need), additional unmapping needs to be done.
|
|
|
|
*/
|
|
|
|
static int
|
2014-02-11 10:28:51 +00:00
|
|
|
unmap_unneeded_hugepages(struct hugepage_file *hugepg_tbl,
|
2012-12-20 00:00:00 +01:00
|
|
|
struct hugepage_info *hpi,
|
|
|
|
unsigned num_hp_info)
|
|
|
|
{
|
|
|
|
unsigned socket, size;
|
|
|
|
int page, nrpages = 0;
|
|
|
|
|
|
|
|
/* get total number of hugepages */
|
|
|
|
for (size = 0; size < num_hp_info; size++)
|
|
|
|
for (socket = 0; socket < RTE_MAX_NUMA_NODES; socket++)
|
|
|
|
nrpages += internal_config.hugepage_info[size].num_pages[socket];
|
|
|
|
|
|
|
|
for (size = 0; size < num_hp_info; size++) {
|
|
|
|
for (socket = 0; socket < RTE_MAX_NUMA_NODES; socket++) {
|
|
|
|
unsigned pages_found = 0;
|
2014-02-11 10:28:51 +00:00
|
|
|
|
2012-12-20 00:00:00 +01:00
|
|
|
/* traverse until we have unmapped all the unused pages */
|
|
|
|
for (page = 0; page < nrpages; page++) {
|
2014-02-11 10:28:51 +00:00
|
|
|
struct hugepage_file *hp = &hugepg_tbl[page];
|
|
|
|
|
|
|
|
#ifdef RTE_EAL_SINGLE_FILE_SEGMENTS
|
|
|
|
/* if this page was already cleared */
|
|
|
|
if (hp->final_va == NULL)
|
|
|
|
continue;
|
|
|
|
#endif
|
2012-12-20 00:00:00 +01:00
|
|
|
|
|
|
|
/* find a page that matches the criteria */
|
|
|
|
if ((hp->size == hpi[size].hugepage_sz) &&
|
|
|
|
(hp->socket_id == (int) socket)) {
|
|
|
|
|
|
|
|
/* if we skipped enough pages, unmap the rest */
|
|
|
|
if (pages_found == hpi[size].num_pages[socket]) {
|
2014-02-11 10:28:51 +00:00
|
|
|
uint64_t unmap_len;
|
|
|
|
|
|
|
|
#ifdef RTE_EAL_SINGLE_FILE_SEGMENTS
|
|
|
|
unmap_len = hp->size * hp->repeated;
|
|
|
|
#else
|
|
|
|
unmap_len = hp->size;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* get start addr and len of the remaining segment */
|
|
|
|
munmap(hp->final_va, (size_t) unmap_len);
|
|
|
|
|
2012-12-20 00:00:00 +01:00
|
|
|
hp->final_va = NULL;
|
2014-02-11 10:28:51 +00:00
|
|
|
if (unlink(hp->filepath) == -1) {
|
2013-11-08 03:00:00 +01:00
|
|
|
RTE_LOG(ERR, EAL, "%s(): Removing %s failed: %s\n",
|
|
|
|
__func__, hp->filepath, strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
2012-12-20 00:00:00 +01:00
|
|
|
}
|
2014-02-11 10:28:51 +00:00
|
|
|
#ifdef RTE_EAL_SINGLE_FILE_SEGMENTS
|
|
|
|
/* else, check how much do we need to map */
|
|
|
|
else {
|
|
|
|
int nr_pg_left =
|
|
|
|
hpi[size].num_pages[socket] - pages_found;
|
|
|
|
|
|
|
|
/* if we need enough memory to fit into the segment */
|
|
|
|
if (hp->repeated <= nr_pg_left) {
|
|
|
|
pages_found += hp->repeated;
|
|
|
|
}
|
|
|
|
/* truncate the segment */
|
|
|
|
else {
|
|
|
|
uint64_t final_size = nr_pg_left * hp->size;
|
|
|
|
uint64_t seg_size = hp->repeated * hp->size;
|
|
|
|
|
|
|
|
void * unmap_va = RTE_PTR_ADD(hp->final_va,
|
|
|
|
final_size);
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
munmap(unmap_va, seg_size - final_size);
|
|
|
|
|
|
|
|
fd = open(hp->filepath, O_RDWR);
|
|
|
|
if (fd < 0) {
|
|
|
|
RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
|
|
|
|
hp->filepath, strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (ftruncate(fd, final_size) < 0) {
|
|
|
|
RTE_LOG(ERR, EAL, "Cannot truncate %s: %s\n",
|
|
|
|
hp->filepath, strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
pages_found += nr_pg_left;
|
|
|
|
hp->repeated = nr_pg_left;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
/* else, lock the page and skip */
|
2013-06-03 00:00:00 +00:00
|
|
|
else
|
2012-12-20 00:00:00 +01:00
|
|
|
pages_found++;
|
2014-02-11 10:28:51 +00:00
|
|
|
#endif
|
2013-06-03 00:00:00 +00:00
|
|
|
|
2012-12-20 00:00:00 +01:00
|
|
|
} /* match page */
|
|
|
|
} /* foreach page */
|
|
|
|
} /* foreach socket */
|
|
|
|
} /* foreach pagesize */
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline uint64_t
|
|
|
|
get_socket_mem_size(int socket)
|
|
|
|
{
|
|
|
|
uint64_t size = 0;
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
for (i = 0; i < internal_config.num_hugepage_sizes; i++){
|
|
|
|
struct hugepage_info *hpi = &internal_config.hugepage_info[i];
|
|
|
|
if (hpi->hugedir != NULL)
|
|
|
|
size += hpi->hugepage_sz * hpi->num_pages[socket];
|
|
|
|
}
|
|
|
|
|
2015-05-11 17:10:25 +03:00
|
|
|
return size;
|
2012-12-20 00:00:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This function is a NUMA-aware equivalent of calc_num_pages.
|
|
|
|
* It takes in the list of hugepage sizes and the
|
2012-09-04 13:54:00 +01:00
|
|
|
* number of pages thereof, and calculates the best number of
|
|
|
|
* pages of each size to fulfill the request for <memory> ram
|
|
|
|
*/
|
|
|
|
static int
|
2012-12-20 00:00:00 +01:00
|
|
|
calc_num_pages_per_socket(uint64_t * memory,
|
2012-09-04 13:54:00 +01:00
|
|
|
struct hugepage_info *hp_info,
|
|
|
|
struct hugepage_info *hp_used,
|
|
|
|
unsigned num_hp_info)
|
|
|
|
{
|
2012-12-20 00:00:00 +01:00
|
|
|
unsigned socket, j, i = 0;
|
|
|
|
unsigned requested, available;
|
2012-09-04 13:54:00 +01:00
|
|
|
int total_num_pages = 0;
|
2012-12-20 00:00:00 +01:00
|
|
|
uint64_t remaining_mem, cur_mem;
|
|
|
|
uint64_t total_mem = internal_config.memory;
|
|
|
|
|
2012-09-04 13:54:00 +01:00
|
|
|
if (num_hp_info == 0)
|
|
|
|
return -1;
|
|
|
|
|
2014-05-09 15:30:42 +02:00
|
|
|
/* if specific memory amounts per socket weren't requested */
|
|
|
|
if (internal_config.force_sockets == 0) {
|
|
|
|
int cpu_per_socket[RTE_MAX_NUMA_NODES];
|
|
|
|
size_t default_size, total_size;
|
|
|
|
unsigned lcore_id;
|
|
|
|
|
|
|
|
/* Compute number of cores per socket */
|
|
|
|
memset(cpu_per_socket, 0, sizeof(cpu_per_socket));
|
|
|
|
RTE_LCORE_FOREACH(lcore_id) {
|
|
|
|
cpu_per_socket[rte_lcore_to_socket_id(lcore_id)]++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Automatically spread requested memory amongst detected sockets according
|
|
|
|
* to number of cores from cpu mask present on each socket
|
|
|
|
*/
|
|
|
|
total_size = internal_config.memory;
|
|
|
|
for (socket = 0; socket < RTE_MAX_NUMA_NODES && total_size != 0; socket++) {
|
|
|
|
|
|
|
|
/* Set memory amount per socket */
|
|
|
|
default_size = (internal_config.memory * cpu_per_socket[socket])
|
|
|
|
/ rte_lcore_count();
|
|
|
|
|
|
|
|
/* Limit to maximum available memory on socket */
|
|
|
|
default_size = RTE_MIN(default_size, get_socket_mem_size(socket));
|
|
|
|
|
|
|
|
/* Update sizes */
|
|
|
|
memory[socket] = default_size;
|
|
|
|
total_size -= default_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If some memory is remaining, try to allocate it by getting all
|
|
|
|
* available memory from sockets, one after the other
|
|
|
|
*/
|
|
|
|
for (socket = 0; socket < RTE_MAX_NUMA_NODES && total_size != 0; socket++) {
|
2012-12-20 00:00:00 +01:00
|
|
|
/* take whatever is available */
|
2014-05-09 15:30:42 +02:00
|
|
|
default_size = RTE_MIN(get_socket_mem_size(socket) - memory[socket],
|
|
|
|
total_size);
|
|
|
|
|
|
|
|
/* Update sizes */
|
|
|
|
memory[socket] += default_size;
|
|
|
|
total_size -= default_size;
|
2012-12-20 00:00:00 +01:00
|
|
|
}
|
2014-05-09 15:30:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (socket = 0; socket < RTE_MAX_NUMA_NODES && total_mem != 0; socket++) {
|
2012-12-20 00:00:00 +01:00
|
|
|
/* skips if the memory on specific socket wasn't requested */
|
|
|
|
for (i = 0; i < num_hp_info && memory[socket] != 0; i++){
|
|
|
|
hp_used[i].hugedir = hp_info[i].hugedir;
|
|
|
|
hp_used[i].num_pages[socket] = RTE_MIN(
|
|
|
|
memory[socket] / hp_info[i].hugepage_sz,
|
|
|
|
hp_info[i].num_pages[socket]);
|
|
|
|
|
|
|
|
cur_mem = hp_used[i].num_pages[socket] *
|
|
|
|
hp_used[i].hugepage_sz;
|
|
|
|
|
|
|
|
memory[socket] -= cur_mem;
|
|
|
|
total_mem -= cur_mem;
|
|
|
|
|
|
|
|
total_num_pages += hp_used[i].num_pages[socket];
|
|
|
|
|
|
|
|
/* check if we have met all memory requests */
|
|
|
|
if (memory[socket] == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* check if we have any more pages left at this size, if so
|
|
|
|
* move on to next size */
|
|
|
|
if (hp_used[i].num_pages[socket] == hp_info[i].num_pages[socket])
|
|
|
|
continue;
|
|
|
|
/* At this point we know that there are more pages available that are
|
|
|
|
* bigger than the memory we want, so lets see if we can get enough
|
|
|
|
* from other page sizes.
|
|
|
|
*/
|
|
|
|
remaining_mem = 0;
|
|
|
|
for (j = i+1; j < num_hp_info; j++)
|
|
|
|
remaining_mem += hp_info[j].hugepage_sz *
|
|
|
|
hp_info[j].num_pages[socket];
|
|
|
|
|
|
|
|
/* is there enough other memory, if not allocate another page and quit */
|
|
|
|
if (remaining_mem < memory[socket]){
|
|
|
|
cur_mem = RTE_MIN(memory[socket],
|
|
|
|
hp_info[i].hugepage_sz);
|
|
|
|
memory[socket] -= cur_mem;
|
|
|
|
total_mem -= cur_mem;
|
|
|
|
hp_used[i].num_pages[socket]++;
|
|
|
|
total_num_pages++;
|
|
|
|
break; /* we are done with this socket*/
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* if we didn't satisfy all memory requirements per socket */
|
|
|
|
if (memory[socket] > 0) {
|
|
|
|
/* to prevent icc errors */
|
|
|
|
requested = (unsigned) (internal_config.socket_mem[socket] /
|
|
|
|
0x100000);
|
|
|
|
available = requested -
|
|
|
|
((unsigned) (memory[socket] / 0x100000));
|
|
|
|
RTE_LOG(INFO, EAL, "Not enough memory available on socket %u! "
|
|
|
|
"Requested: %uMB, available: %uMB\n", socket,
|
|
|
|
requested, available);
|
|
|
|
return -1;
|
2012-09-04 13:54:00 +01:00
|
|
|
}
|
|
|
|
}
|
2012-12-20 00:00:00 +01:00
|
|
|
|
|
|
|
/* if we didn't satisfy total memory requirements */
|
|
|
|
if (total_mem > 0) {
|
|
|
|
requested = (unsigned) (internal_config.memory / 0x100000);
|
|
|
|
available = requested - (unsigned) (total_mem / 0x100000);
|
|
|
|
RTE_LOG(INFO, EAL, "Not enough memory available! Requested: %uMB,"
|
|
|
|
" available: %uMB\n", requested, available);
|
|
|
|
return -1;
|
|
|
|
}
|
2012-09-04 13:54:00 +01:00
|
|
|
return total_num_pages;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Prepare physical memory mapping: fill configuration structure with
|
|
|
|
* these infos, return 0 on success.
|
|
|
|
* 1. map N huge pages in separate files in hugetlbfs
|
|
|
|
* 2. find associated physical addr
|
|
|
|
* 3. find associated NUMA socket ID
|
|
|
|
* 4. sort all huge pages by physical address
|
|
|
|
* 5. remap these N huge pages in the correct order
|
|
|
|
* 6. unmap the first mapping
|
|
|
|
* 7. fill memsegs in configuration with contiguous zones
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
rte_eal_hugepage_init(void)
|
|
|
|
{
|
|
|
|
struct rte_mem_config *mcfg;
|
2014-02-11 10:28:51 +00:00
|
|
|
struct hugepage_file *hugepage, *tmp_hp = NULL;
|
2012-09-04 13:54:00 +01:00
|
|
|
struct hugepage_info used_hp[MAX_HUGEPAGE_SIZES];
|
2012-12-20 00:00:00 +01:00
|
|
|
|
|
|
|
uint64_t memory[RTE_MAX_NUMA_NODES];
|
|
|
|
|
|
|
|
unsigned hp_offset;
|
2012-09-04 13:54:00 +01:00
|
|
|
int i, j, new_memseg;
|
2014-02-11 10:28:51 +00:00
|
|
|
int nr_hugefiles, nr_hugepages = 0;
|
2012-09-04 13:54:00 +01:00
|
|
|
void *addr;
|
2014-02-11 10:28:51 +00:00
|
|
|
#ifdef RTE_EAL_SINGLE_FILE_SEGMENTS
|
|
|
|
int new_pages_count[MAX_HUGEPAGE_SIZES];
|
|
|
|
#endif
|
2012-09-04 13:54:00 +01:00
|
|
|
|
|
|
|
memset(used_hp, 0, sizeof(used_hp));
|
|
|
|
|
|
|
|
/* get pointer to global configuration */
|
|
|
|
mcfg = rte_eal_get_configuration()->mem_config;
|
|
|
|
|
2013-03-01 16:10:57 +01:00
|
|
|
/* hugetlbfs can be disabled */
|
2012-09-04 13:54:00 +01:00
|
|
|
if (internal_config.no_hugetlbfs) {
|
2014-06-13 15:52:50 +01:00
|
|
|
addr = mmap(NULL, internal_config.memory, PROT_READ | PROT_WRITE,
|
|
|
|
MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
|
|
|
|
if (addr == MAP_FAILED) {
|
|
|
|
RTE_LOG(ERR, EAL, "%s: mmap() failed: %s\n", __func__,
|
|
|
|
strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
2012-12-20 00:00:00 +01:00
|
|
|
mcfg->memseg[0].phys_addr = (phys_addr_t)(uintptr_t)addr;
|
2012-09-04 13:54:00 +01:00
|
|
|
mcfg->memseg[0].addr = addr;
|
|
|
|
mcfg->memseg[0].len = internal_config.memory;
|
2013-02-21 17:39:23 +01:00
|
|
|
mcfg->memseg[0].socket_id = SOCKET_ID_ANY;
|
2012-09-04 13:54:00 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-02-12 15:32:25 +00:00
|
|
|
/* check if app runs on Xen Dom0 */
|
|
|
|
if (internal_config.xen_dom0_support) {
|
|
|
|
#ifdef RTE_LIBRTE_XEN_DOM0
|
|
|
|
/* use dom0_mm kernel driver to init memory */
|
|
|
|
if (rte_xen_dom0_memory_init() < 0)
|
|
|
|
return -1;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-09-04 13:54:00 +01:00
|
|
|
|
2012-12-20 00:00:00 +01:00
|
|
|
/* calculate total number of hugepages available. at this point we haven't
|
|
|
|
* yet started sorting them so they all are on socket 0 */
|
|
|
|
for (i = 0; i < (int) internal_config.num_hugepage_sizes; i++) {
|
|
|
|
/* meanwhile, also initialize used_hp hugepage sizes in used_hp */
|
|
|
|
used_hp[i].hugepage_sz = internal_config.hugepage_info[i].hugepage_sz;
|
|
|
|
|
2014-02-11 10:28:51 +00:00
|
|
|
nr_hugepages += internal_config.hugepage_info[i].num_pages[0];
|
2012-12-20 00:00:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* allocate a memory area for hugepage table.
|
|
|
|
* this isn't shared memory yet. due to the fact that we need some
|
|
|
|
* processing done on these pages, shared memory will be created
|
|
|
|
* at a later stage.
|
|
|
|
*/
|
2014-02-11 10:28:51 +00:00
|
|
|
tmp_hp = malloc(nr_hugepages * sizeof(struct hugepage_file));
|
2012-12-20 00:00:00 +01:00
|
|
|
if (tmp_hp == NULL)
|
|
|
|
goto fail;
|
|
|
|
|
2014-02-11 10:28:51 +00:00
|
|
|
memset(tmp_hp, 0, nr_hugepages * sizeof(struct hugepage_file));
|
2012-09-04 13:54:00 +01:00
|
|
|
|
2012-12-20 00:00:00 +01:00
|
|
|
hp_offset = 0; /* where we start the current page size entries */
|
|
|
|
|
|
|
|
/* map all hugepages and sort them */
|
2012-09-04 13:54:00 +01:00
|
|
|
for (i = 0; i < (int)internal_config.num_hugepage_sizes; i ++){
|
2012-12-20 00:00:00 +01:00
|
|
|
struct hugepage_info *hpi;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* we don't yet mark hugepages as used at this stage, so
|
|
|
|
* we just map all hugepages available to the system
|
|
|
|
* all hugepages are still located on socket 0
|
|
|
|
*/
|
|
|
|
hpi = &internal_config.hugepage_info[i];
|
|
|
|
|
2014-02-11 10:28:51 +00:00
|
|
|
if (hpi->num_pages[0] == 0)
|
2012-09-04 13:54:00 +01:00
|
|
|
continue;
|
|
|
|
|
2012-12-20 00:00:00 +01:00
|
|
|
/* map all hugepages available */
|
|
|
|
if (map_all_hugepages(&tmp_hp[hp_offset], hpi, 1) < 0){
|
2012-09-04 13:54:00 +01:00
|
|
|
RTE_LOG(DEBUG, EAL, "Failed to mmap %u MB hugepages\n",
|
|
|
|
(unsigned)(hpi->hugepage_sz / 0x100000));
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2012-12-20 00:00:00 +01:00
|
|
|
/* find physical addresses and sockets for each hugepage */
|
2014-02-11 10:28:51 +00:00
|
|
|
if (find_physaddrs(&tmp_hp[hp_offset], hpi) < 0){
|
2012-09-04 13:54:00 +01:00
|
|
|
RTE_LOG(DEBUG, EAL, "Failed to find phys addr for %u MB pages\n",
|
|
|
|
(unsigned)(hpi->hugepage_sz / 0x100000));
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2012-12-20 00:00:00 +01:00
|
|
|
if (find_numasocket(&tmp_hp[hp_offset], hpi) < 0){
|
2012-09-04 13:54:00 +01:00
|
|
|
RTE_LOG(DEBUG, EAL, "Failed to find NUMA socket for %u MB pages\n",
|
|
|
|
(unsigned)(hpi->hugepage_sz / 0x100000));
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2012-12-20 00:00:00 +01:00
|
|
|
if (sort_by_physaddr(&tmp_hp[hp_offset], hpi) < 0)
|
2012-09-04 13:54:00 +01:00
|
|
|
goto fail;
|
|
|
|
|
2014-02-11 10:28:51 +00:00
|
|
|
#ifdef RTE_EAL_SINGLE_FILE_SEGMENTS
|
|
|
|
/* remap all hugepages into single file segments */
|
|
|
|
new_pages_count[i] = remap_all_hugepages(&tmp_hp[hp_offset], hpi);
|
|
|
|
if (new_pages_count[i] < 0){
|
|
|
|
RTE_LOG(DEBUG, EAL, "Failed to remap %u MB pages\n",
|
|
|
|
(unsigned)(hpi->hugepage_sz / 0x100000));
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* we have processed a num of hugepages of this size, so inc offset */
|
|
|
|
hp_offset += new_pages_count[i];
|
|
|
|
#else
|
2012-12-20 00:00:00 +01:00
|
|
|
/* remap all hugepages */
|
|
|
|
if (map_all_hugepages(&tmp_hp[hp_offset], hpi, 0) < 0){
|
2012-09-04 13:54:00 +01:00
|
|
|
RTE_LOG(DEBUG, EAL, "Failed to remap %u MB pages\n",
|
|
|
|
(unsigned)(hpi->hugepage_sz / 0x100000));
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2012-12-20 00:00:00 +01:00
|
|
|
/* unmap original mappings */
|
|
|
|
if (unmap_all_hugepages_orig(&tmp_hp[hp_offset], hpi) < 0)
|
2012-09-04 13:54:00 +01:00
|
|
|
goto fail;
|
|
|
|
|
|
|
|
/* we have processed a num of hugepages of this size, so inc offset */
|
2012-12-20 00:00:00 +01:00
|
|
|
hp_offset += hpi->num_pages[0];
|
2014-02-11 10:28:51 +00:00
|
|
|
#endif
|
2012-12-20 00:00:00 +01:00
|
|
|
}
|
|
|
|
|
2014-02-11 10:28:51 +00:00
|
|
|
#ifdef RTE_EAL_SINGLE_FILE_SEGMENTS
|
|
|
|
nr_hugefiles = 0;
|
|
|
|
for (i = 0; i < (int) internal_config.num_hugepage_sizes; i++) {
|
|
|
|
nr_hugefiles += new_pages_count[i];
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
nr_hugefiles = nr_hugepages;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2012-12-20 00:00:00 +01:00
|
|
|
/* clean out the numbers of pages */
|
|
|
|
for (i = 0; i < (int) internal_config.num_hugepage_sizes; i++)
|
|
|
|
for (j = 0; j < RTE_MAX_NUMA_NODES; j++)
|
|
|
|
internal_config.hugepage_info[i].num_pages[j] = 0;
|
|
|
|
|
|
|
|
/* get hugepages for each socket */
|
2014-02-11 10:28:51 +00:00
|
|
|
for (i = 0; i < nr_hugefiles; i++) {
|
2012-12-20 00:00:00 +01:00
|
|
|
int socket = tmp_hp[i].socket_id;
|
|
|
|
|
|
|
|
/* find a hugepage info with right size and increment num_pages */
|
2015-05-29 15:34:13 +01:00
|
|
|
const int nb_hpsizes = RTE_MIN(MAX_HUGEPAGE_SIZES,
|
|
|
|
(int)internal_config.num_hugepage_sizes);
|
|
|
|
for (j = 0; j < nb_hpsizes; j++) {
|
2012-12-20 00:00:00 +01:00
|
|
|
if (tmp_hp[i].size ==
|
|
|
|
internal_config.hugepage_info[j].hugepage_sz) {
|
2014-02-11 10:28:51 +00:00
|
|
|
#ifdef RTE_EAL_SINGLE_FILE_SEGMENTS
|
|
|
|
internal_config.hugepage_info[j].num_pages[socket] +=
|
|
|
|
tmp_hp[i].repeated;
|
|
|
|
#else
|
2012-12-20 00:00:00 +01:00
|
|
|
internal_config.hugepage_info[j].num_pages[socket]++;
|
2014-02-11 10:28:51 +00:00
|
|
|
#endif
|
2012-12-20 00:00:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* make a copy of socket_mem, needed for number of pages calculation */
|
|
|
|
for (i = 0; i < RTE_MAX_NUMA_NODES; i++)
|
|
|
|
memory[i] = internal_config.socket_mem[i];
|
|
|
|
|
|
|
|
/* calculate final number of pages */
|
2014-02-11 10:28:51 +00:00
|
|
|
nr_hugepages = calc_num_pages_per_socket(memory,
|
2012-12-20 00:00:00 +01:00
|
|
|
internal_config.hugepage_info, used_hp,
|
|
|
|
internal_config.num_hugepage_sizes);
|
|
|
|
|
|
|
|
/* error if not enough memory available */
|
2014-02-11 10:28:51 +00:00
|
|
|
if (nr_hugepages < 0)
|
2012-12-20 00:00:00 +01:00
|
|
|
goto fail;
|
|
|
|
|
|
|
|
/* reporting in! */
|
|
|
|
for (i = 0; i < (int) internal_config.num_hugepage_sizes; i++) {
|
|
|
|
for (j = 0; j < RTE_MAX_NUMA_NODES; j++) {
|
|
|
|
if (used_hp[i].num_pages[j] > 0) {
|
|
|
|
RTE_LOG(INFO, EAL,
|
|
|
|
"Requesting %u pages of size %uMB"
|
|
|
|
" from socket %i\n",
|
|
|
|
used_hp[i].num_pages[j],
|
|
|
|
(unsigned)
|
|
|
|
(used_hp[i].hugepage_sz / 0x100000),
|
|
|
|
j);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* create shared memory */
|
|
|
|
hugepage = create_shared_memory(eal_hugepage_info_path(),
|
2014-02-11 10:28:51 +00:00
|
|
|
nr_hugefiles * sizeof(struct hugepage_file));
|
2012-12-20 00:00:00 +01:00
|
|
|
|
|
|
|
if (hugepage == NULL) {
|
|
|
|
RTE_LOG(ERR, EAL, "Failed to create shared memory!\n");
|
|
|
|
goto fail;
|
2012-09-04 13:54:00 +01:00
|
|
|
}
|
2014-02-11 10:28:51 +00:00
|
|
|
memset(hugepage, 0, nr_hugefiles * sizeof(struct hugepage_file));
|
2012-09-04 13:54:00 +01:00
|
|
|
|
2012-12-20 00:00:00 +01:00
|
|
|
/*
|
|
|
|
* unmap pages that we won't need (looks at used_hp).
|
|
|
|
* also, sets final_va to NULL on pages that were unmapped.
|
|
|
|
*/
|
|
|
|
if (unmap_unneeded_hugepages(tmp_hp, used_hp,
|
|
|
|
internal_config.num_hugepage_sizes) < 0) {
|
|
|
|
RTE_LOG(ERR, EAL, "Unmapping and locking hugepages failed!\n");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* copy stuff from malloc'd hugepage* to the actual shared memory.
|
|
|
|
* this procedure only copies those hugepages that have final_va
|
|
|
|
* not NULL. has overflow protection.
|
|
|
|
*/
|
2014-02-11 10:28:51 +00:00
|
|
|
if (copy_hugepages_to_shared_mem(hugepage, nr_hugefiles,
|
|
|
|
tmp_hp, nr_hugefiles) < 0) {
|
2012-12-20 00:00:00 +01:00
|
|
|
RTE_LOG(ERR, EAL, "Copying tables to shared memory failed!\n");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* free the temporary hugepage table */
|
|
|
|
free(tmp_hp);
|
|
|
|
tmp_hp = NULL;
|
|
|
|
|
2014-02-11 10:28:51 +00:00
|
|
|
/* find earliest free memseg - this is needed because in case of IVSHMEM,
|
|
|
|
* segments might have already been initialized */
|
|
|
|
for (j = 0; j < RTE_MAX_MEMSEG; j++)
|
|
|
|
if (mcfg->memseg[j].addr == NULL) {
|
|
|
|
/* move to previous segment and exit loop */
|
|
|
|
j--;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < nr_hugefiles; i++) {
|
2012-09-04 13:54:00 +01:00
|
|
|
new_memseg = 0;
|
|
|
|
|
|
|
|
/* if this is a new section, create a new memseg */
|
|
|
|
if (i == 0)
|
|
|
|
new_memseg = 1;
|
|
|
|
else if (hugepage[i].socket_id != hugepage[i-1].socket_id)
|
|
|
|
new_memseg = 1;
|
|
|
|
else if (hugepage[i].size != hugepage[i-1].size)
|
|
|
|
new_memseg = 1;
|
2014-11-25 17:17:15 -05:00
|
|
|
|
|
|
|
#ifdef RTE_ARCH_PPC_64
|
|
|
|
/* On PPC64 architecture, the mmap always start from higher
|
|
|
|
* virtual address to lower address. Here, both the physical
|
|
|
|
* address and virtual address are in descending order */
|
|
|
|
else if ((hugepage[i-1].physaddr - hugepage[i].physaddr) !=
|
|
|
|
hugepage[i].size)
|
|
|
|
new_memseg = 1;
|
|
|
|
else if (((unsigned long)hugepage[i-1].final_va -
|
|
|
|
(unsigned long)hugepage[i].final_va) != hugepage[i].size)
|
|
|
|
new_memseg = 1;
|
|
|
|
#else
|
2012-09-04 13:54:00 +01:00
|
|
|
else if ((hugepage[i].physaddr - hugepage[i-1].physaddr) !=
|
2012-12-20 00:00:00 +01:00
|
|
|
hugepage[i].size)
|
2012-09-04 13:54:00 +01:00
|
|
|
new_memseg = 1;
|
|
|
|
else if (((unsigned long)hugepage[i].final_va -
|
2012-12-20 00:00:00 +01:00
|
|
|
(unsigned long)hugepage[i-1].final_va) != hugepage[i].size)
|
2012-09-04 13:54:00 +01:00
|
|
|
new_memseg = 1;
|
2014-11-25 17:17:15 -05:00
|
|
|
#endif
|
2012-09-04 13:54:00 +01:00
|
|
|
|
|
|
|
if (new_memseg) {
|
|
|
|
j += 1;
|
|
|
|
if (j == RTE_MAX_MEMSEG)
|
|
|
|
break;
|
|
|
|
|
|
|
|
mcfg->memseg[j].phys_addr = hugepage[i].physaddr;
|
|
|
|
mcfg->memseg[j].addr = hugepage[i].final_va;
|
2014-02-11 10:28:51 +00:00
|
|
|
#ifdef RTE_EAL_SINGLE_FILE_SEGMENTS
|
|
|
|
mcfg->memseg[j].len = hugepage[i].size * hugepage[i].repeated;
|
|
|
|
#else
|
2012-09-04 13:54:00 +01:00
|
|
|
mcfg->memseg[j].len = hugepage[i].size;
|
2014-02-11 10:28:51 +00:00
|
|
|
#endif
|
2012-09-04 13:54:00 +01:00
|
|
|
mcfg->memseg[j].socket_id = hugepage[i].socket_id;
|
|
|
|
mcfg->memseg[j].hugepage_sz = hugepage[i].size;
|
|
|
|
}
|
|
|
|
/* continuation of previous memseg */
|
|
|
|
else {
|
2014-11-25 17:17:15 -05:00
|
|
|
#ifdef RTE_ARCH_PPC_64
|
|
|
|
/* Use the phy and virt address of the last page as segment
|
|
|
|
* address for IBM Power architecture */
|
|
|
|
mcfg->memseg[j].phys_addr = hugepage[i].physaddr;
|
|
|
|
mcfg->memseg[j].addr = hugepage[i].final_va;
|
|
|
|
#endif
|
2012-09-04 13:54:00 +01:00
|
|
|
mcfg->memseg[j].len += mcfg->memseg[j].hugepage_sz;
|
|
|
|
}
|
|
|
|
hugepage[i].memseg_id = j;
|
|
|
|
}
|
|
|
|
|
2014-02-11 10:28:51 +00:00
|
|
|
if (i < nr_hugefiles) {
|
2013-03-12 12:03:00 +01:00
|
|
|
RTE_LOG(ERR, EAL, "Can only reserve %d pages "
|
|
|
|
"from %d requested\n"
|
|
|
|
"Current %s=%d is not enough\n"
|
|
|
|
"Please either increase it or request less amount "
|
|
|
|
"of memory.\n",
|
2014-02-11 10:28:51 +00:00
|
|
|
i, nr_hugefiles, RTE_STR(CONFIG_RTE_MAX_MEMSEG),
|
2013-03-12 12:03:00 +01:00
|
|
|
RTE_MAX_MEMSEG);
|
2015-05-11 17:10:25 +03:00
|
|
|
return -ENOMEM;
|
2013-03-12 12:03:00 +01:00
|
|
|
}
|
|
|
|
|
2012-09-04 13:54:00 +01:00
|
|
|
return 0;
|
|
|
|
|
2012-12-20 00:00:00 +01:00
|
|
|
fail:
|
|
|
|
if (tmp_hp)
|
|
|
|
free(tmp_hp);
|
2012-09-04 13:54:00 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* uses fstat to report the size of a file on disk
|
|
|
|
*/
|
|
|
|
static off_t
|
|
|
|
getFileSize(int fd)
|
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
if (fstat(fd, &st) < 0)
|
|
|
|
return 0;
|
|
|
|
return st.st_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This creates the memory mappings in the secondary process to match that of
|
|
|
|
* the server process. It goes through each memory segment in the DPDK runtime
|
|
|
|
* configuration and finds the hugepages which form that segment, mapping them
|
|
|
|
* in order to form a contiguous block in the virtual memory space
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
rte_eal_hugepage_attach(void)
|
|
|
|
{
|
|
|
|
const struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
|
2014-02-11 10:28:51 +00:00
|
|
|
const struct hugepage_file *hp = NULL;
|
2012-09-04 13:54:00 +01:00
|
|
|
unsigned num_hp = 0;
|
|
|
|
unsigned i, s = 0; /* s used to track the segment number */
|
|
|
|
off_t size;
|
|
|
|
int fd, fd_zero = -1, fd_hugepage = -1;
|
|
|
|
|
|
|
|
if (aslr_enabled() > 0) {
|
|
|
|
RTE_LOG(WARNING, EAL, "WARNING: Address Space Layout Randomization "
|
|
|
|
"(ASLR) is enabled in the kernel.\n");
|
|
|
|
RTE_LOG(WARNING, EAL, " This may cause issues with mapping memory "
|
|
|
|
"into secondary processes\n");
|
|
|
|
}
|
|
|
|
|
2014-02-12 15:32:25 +00:00
|
|
|
if (internal_config.xen_dom0_support) {
|
|
|
|
#ifdef RTE_LIBRTE_XEN_DOM0
|
|
|
|
if (rte_xen_dom0_memory_attach() < 0) {
|
|
|
|
RTE_LOG(ERR, EAL,"Failed to attach memory setments of primay "
|
|
|
|
"process\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-09-04 13:54:00 +01:00
|
|
|
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());
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2013-07-23 00:00:00 +02:00
|
|
|
/* map all segments into memory to make sure we get the addrs */
|
|
|
|
for (s = 0; s < RTE_MAX_MEMSEG; ++s) {
|
|
|
|
void *base_addr;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* the first memory segment with len==0 is the one that
|
|
|
|
* follows the last valid segment.
|
|
|
|
*/
|
|
|
|
if (mcfg->memseg[s].len == 0)
|
|
|
|
break;
|
|
|
|
|
2014-02-11 10:28:51 +00:00
|
|
|
#ifdef RTE_LIBRTE_IVSHMEM
|
|
|
|
/*
|
|
|
|
* if segment has ioremap address set, it's an IVSHMEM segment and
|
|
|
|
* doesn't need mapping as it was already mapped earlier
|
|
|
|
*/
|
|
|
|
if (mcfg->memseg[s].ioremap_addr != 0)
|
|
|
|
continue;
|
|
|
|
#endif
|
|
|
|
|
2013-07-23 00:00:00 +02:00
|
|
|
/*
|
|
|
|
* 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,
|
|
|
|
PROT_READ, MAP_PRIVATE, fd_zero, 0);
|
|
|
|
if (base_addr == MAP_FAILED ||
|
|
|
|
base_addr != mcfg->memseg[s].addr) {
|
|
|
|
RTE_LOG(ERR, EAL, "Could not mmap %llu bytes "
|
2014-02-11 10:28:51 +00:00
|
|
|
"in /dev/zero to requested address [%p]: '%s'\n",
|
2013-07-23 00:00:00 +02:00
|
|
|
(unsigned long long)mcfg->memseg[s].len,
|
2014-02-11 10:28:51 +00:00
|
|
|
mcfg->memseg[s].addr, strerror(errno));
|
2013-07-23 00:00:00 +02:00
|
|
|
if (aslr_enabled() > 0) {
|
|
|
|
RTE_LOG(ERR, EAL, "It is recommended to "
|
|
|
|
"disable ASLR in the kernel "
|
|
|
|
"and retry running both primary "
|
|
|
|
"and secondary processes\n");
|
|
|
|
}
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-04 13:54:00 +01:00
|
|
|
size = getFileSize(fd_hugepage);
|
|
|
|
hp = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd_hugepage, 0);
|
|
|
|
if (hp == NULL) {
|
|
|
|
RTE_LOG(ERR, EAL, "Could not mmap %s\n", eal_hugepage_info_path());
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2014-02-11 10:28:51 +00:00
|
|
|
num_hp = size / sizeof(struct hugepage_file);
|
|
|
|
RTE_LOG(DEBUG, EAL, "Analysing %u files\n", num_hp);
|
2012-09-04 13:54:00 +01:00
|
|
|
|
2013-07-23 00:00:00 +02:00
|
|
|
s = 0;
|
2012-09-04 13:54:00 +01:00
|
|
|
while (s < RTE_MAX_MEMSEG && mcfg->memseg[s].len > 0){
|
|
|
|
void *addr, *base_addr;
|
|
|
|
uintptr_t offset = 0;
|
2014-02-11 10:28:51 +00:00
|
|
|
size_t mapping_size;
|
|
|
|
#ifdef RTE_LIBRTE_IVSHMEM
|
|
|
|
/*
|
|
|
|
* if segment has ioremap address set, it's an IVSHMEM segment and
|
|
|
|
* doesn't need mapping as it was already mapped earlier
|
|
|
|
*/
|
|
|
|
if (mcfg->memseg[s].ioremap_addr != 0) {
|
|
|
|
s++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
#endif
|
2013-07-23 00:00:00 +02:00
|
|
|
/*
|
|
|
|
* free previously mapped memory so we can map the
|
|
|
|
* hugepages into the space
|
2012-09-04 13:54:00 +01:00
|
|
|
*/
|
2013-07-23 00:00:00 +02:00
|
|
|
base_addr = mcfg->memseg[s].addr;
|
2012-09-04 13:54:00 +01:00
|
|
|
munmap(base_addr, mcfg->memseg[s].len);
|
|
|
|
|
|
|
|
/* find the hugepages for this segment and map them
|
|
|
|
* we don't need to worry about order, as the server sorted the
|
|
|
|
* entries before it did the second mmap of them */
|
|
|
|
for (i = 0; i < num_hp && offset < mcfg->memseg[s].len; i++){
|
|
|
|
if (hp[i].memseg_id == (int)s){
|
|
|
|
fd = open(hp[i].filepath, O_RDWR);
|
|
|
|
if (fd < 0) {
|
|
|
|
RTE_LOG(ERR, EAL, "Could not open %s\n",
|
|
|
|
hp[i].filepath);
|
|
|
|
goto error;
|
|
|
|
}
|
2014-02-11 10:28:51 +00:00
|
|
|
#ifdef RTE_EAL_SINGLE_FILE_SEGMENTS
|
|
|
|
mapping_size = hp[i].size * hp[i].repeated;
|
|
|
|
#else
|
|
|
|
mapping_size = hp[i].size;
|
|
|
|
#endif
|
2012-09-04 13:54:00 +01:00
|
|
|
addr = mmap(RTE_PTR_ADD(base_addr, offset),
|
2014-02-11 10:28:51 +00:00
|
|
|
mapping_size, PROT_READ | PROT_WRITE,
|
|
|
|
MAP_SHARED, fd, 0);
|
2012-09-04 13:54:00 +01:00
|
|
|
close(fd); /* close file both on success and on failure */
|
2014-02-11 10:28:51 +00:00
|
|
|
if (addr == MAP_FAILED ||
|
|
|
|
addr != RTE_PTR_ADD(base_addr, offset)) {
|
2012-09-04 13:54:00 +01:00
|
|
|
RTE_LOG(ERR, EAL, "Could not mmap %s\n",
|
|
|
|
hp[i].filepath);
|
|
|
|
goto error;
|
|
|
|
}
|
2014-02-11 10:28:51 +00:00
|
|
|
offset+=mapping_size;
|
2012-09-04 13:54:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
RTE_LOG(DEBUG, EAL, "Mapped segment %u of size 0x%llx\n", s,
|
|
|
|
(unsigned long long)mcfg->memseg[s].len);
|
|
|
|
s++;
|
|
|
|
}
|
2013-07-23 00:00:00 +02:00
|
|
|
/* unmap the hugepage config file, since we are done using it */
|
|
|
|
munmap((void *)(uintptr_t)hp, size);
|
2012-09-04 13:54:00 +01:00
|
|
|
close(fd_zero);
|
|
|
|
close(fd_hugepage);
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
error:
|
|
|
|
if (fd_zero >= 0)
|
|
|
|
close(fd_zero);
|
|
|
|
if (fd_hugepage >= 0)
|
|
|
|
close(fd_hugepage);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
rte_eal_memdevice_init(void)
|
|
|
|
{
|
|
|
|
struct rte_config *config;
|
|
|
|
|
|
|
|
if (rte_eal_process_type() == RTE_PROC_SECONDARY)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
config = rte_eal_get_configuration();
|
|
|
|
config->mem_config->nchannel = internal_config.force_nchannel;
|
|
|
|
config->mem_config->nrank = internal_config.force_nrank;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* init memory subsystem */
|
|
|
|
int
|
|
|
|
rte_eal_memory_init(void)
|
|
|
|
{
|
2013-04-08 18:13:06 +02:00
|
|
|
RTE_LOG(INFO, EAL, "Setting up memory...\n");
|
2012-09-04 13:54:00 +01:00
|
|
|
const int retval = rte_eal_process_type() == RTE_PROC_PRIMARY ?
|
|
|
|
rte_eal_hugepage_init() :
|
|
|
|
rte_eal_hugepage_attach();
|
|
|
|
if (retval < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (internal_config.no_shconf == 0 && rte_eal_memdevice_init() < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|