4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 * Copyright(c) 2013 6WIND.
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
41 * * Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * * Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in
45 * the documentation and/or other materials provided with the
47 * * Neither the name of 6WIND S.A. nor the names of its
48 * contributors may be used to endorse or promote products derived
49 * from this software without specific prior written permission.
51 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
52 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
53 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
54 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
55 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
56 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
57 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
58 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
59 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
60 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
61 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
64 #define _FILE_OFFSET_BITS 64
74 #include <sys/types.h>
76 #include <sys/queue.h>
81 #include <sys/ioctl.h>
85 #include <rte_memory.h>
86 #include <rte_memzone.h>
87 #include <rte_launch.h>
89 #include <rte_eal_memconfig.h>
90 #include <rte_per_lcore.h>
91 #include <rte_lcore.h>
92 #include <rte_common.h>
93 #include <rte_string_fns.h>
95 #include "eal_private.h"
96 #include "eal_internal_cfg.h"
97 #include "eal_filesystem.h"
98 #include "eal_hugepages.h"
100 #ifdef RTE_LIBRTE_XEN_DOM0
101 int rte_xen_dom0_supported(void)
103 return internal_config.xen_dom0_support;
109 * Huge page mapping under linux
111 * To reserve a big contiguous amount of memory, we use the hugepage
112 * feature of linux. For that, we need to have hugetlbfs mounted. This
113 * code will create many files in this directory (one per page) and
114 * map them in virtual memory. For each page, we will retrieve its
115 * physical address and remap it in order to have a virtual contiguous
116 * zone as well as a physical contiguous zone.
119 static uint64_t baseaddr_offset;
121 static unsigned proc_pagemap_readable;
123 #define RANDOMIZE_VA_SPACE_FILE "/proc/sys/kernel/randomize_va_space"
126 test_proc_pagemap_readable(void)
128 int fd = open("/proc/self/pagemap", O_RDONLY);
132 "Cannot open /proc/self/pagemap: %s. "
133 "virt2phys address translation will not work\n",
140 proc_pagemap_readable = 1;
143 /* Lock page in physical memory and prevent from swapping. */
145 rte_mem_lock_page(const void *virt)
147 unsigned long virtual = (unsigned long)virt;
148 int page_size = getpagesize();
149 unsigned long aligned = (virtual & ~ (page_size - 1));
150 return mlock((void*)aligned, page_size);
154 * Get physical address of any mapped virtual address in the current process.
157 rte_mem_virt2phy(const void *virtaddr)
160 uint64_t page, physaddr;
161 unsigned long virt_pfn;
165 /* Cannot parse /proc/self/pagemap, no need to log errors everywhere */
166 if (!proc_pagemap_readable)
167 return RTE_BAD_PHYS_ADDR;
169 /* standard page size */
170 page_size = getpagesize();
172 fd = open("/proc/self/pagemap", O_RDONLY);
174 RTE_LOG(ERR, EAL, "%s(): cannot open /proc/self/pagemap: %s\n",
175 __func__, strerror(errno));
176 return RTE_BAD_PHYS_ADDR;
179 virt_pfn = (unsigned long)virtaddr / page_size;
180 offset = sizeof(uint64_t) * virt_pfn;
181 if (lseek(fd, offset, SEEK_SET) == (off_t) -1) {
182 RTE_LOG(ERR, EAL, "%s(): seek error in /proc/self/pagemap: %s\n",
183 __func__, strerror(errno));
185 return RTE_BAD_PHYS_ADDR;
187 if (read(fd, &page, sizeof(uint64_t)) < 0) {
188 RTE_LOG(ERR, EAL, "%s(): cannot read /proc/self/pagemap: %s\n",
189 __func__, strerror(errno));
191 return RTE_BAD_PHYS_ADDR;
195 * the pfn (page frame number) are bits 0-54 (see
196 * pagemap.txt in linux Documentation)
198 physaddr = ((page & 0x7fffffffffffffULL) * page_size)
199 + ((unsigned long)virtaddr % page_size);
205 * For each hugepage in hugepg_tbl, fill the physaddr value. We find
206 * it by browsing the /proc/self/pagemap special file.
209 find_physaddrs(struct hugepage_file *hugepg_tbl, struct hugepage_info *hpi)
214 for (i = 0; i < hpi->num_pages[0]; i++) {
215 addr = rte_mem_virt2phy(hugepg_tbl[i].orig_va);
216 if (addr == RTE_BAD_PHYS_ADDR)
218 hugepg_tbl[i].physaddr = addr;
224 * Check whether address-space layout randomization is enabled in
225 * the kernel. This is important for multi-process as it can prevent
226 * two processes mapping data to the same virtual address
228 * 0 - address space randomization disabled
229 * 1/2 - address space randomization enabled
230 * negative error code on error
236 int retval, fd = open(RANDOMIZE_VA_SPACE_FILE, O_RDONLY);
239 retval = read(fd, &c, 1);
249 default: return -EINVAL;
254 * Try to mmap *size bytes in /dev/zero. If it is successful, return the
255 * pointer to the mmap'd area and keep *size unmodified. Else, retry
256 * with a smaller zone: decrease *size by hugepage_sz until it reaches
257 * 0. In this case, return NULL. Note: this function returns an address
258 * which is a multiple of hugepage size.
261 get_virtual_area(size_t *size, size_t hugepage_sz)
267 if (internal_config.base_virtaddr != 0) {
268 addr = (void*) (uintptr_t) (internal_config.base_virtaddr +
273 RTE_LOG(DEBUG, EAL, "Ask a virtual area of 0x%zx bytes\n", *size);
275 fd = open("/dev/zero", O_RDONLY);
277 RTE_LOG(ERR, EAL, "Cannot open /dev/zero\n");
282 (*size) + hugepage_sz, PROT_READ, MAP_PRIVATE, fd, 0);
283 if (addr == MAP_FAILED)
284 *size -= hugepage_sz;
285 } while (addr == MAP_FAILED && *size > 0);
287 if (addr == MAP_FAILED) {
289 RTE_LOG(ERR, EAL, "Cannot get a virtual area: %s\n",
294 munmap(addr, (*size) + hugepage_sz);
297 /* align addr to a huge page size boundary */
298 aligned_addr = (long)addr;
299 aligned_addr += (hugepage_sz - 1);
300 aligned_addr &= (~(hugepage_sz - 1));
301 addr = (void *)(aligned_addr);
303 RTE_LOG(DEBUG, EAL, "Virtual area found at %p (size = 0x%zx)\n",
306 /* increment offset */
307 baseaddr_offset += *size;
313 * Mmap all hugepages of hugepage table: it first open a file in
314 * hugetlbfs, then mmap() hugepage_sz data in it. If orig is set, the
315 * virtual address is stored in hugepg_tbl[i].orig_va, else it is stored
316 * in hugepg_tbl[i].final_va. The second mapping (when orig is 0) tries to
317 * map continguous physical blocks in contiguous virtual blocks.
320 map_all_hugepages(struct hugepage_file *hugepg_tbl,
321 struct hugepage_info *hpi, int orig)
326 void *vma_addr = NULL;
329 #ifdef RTE_EAL_SINGLE_FILE_SEGMENTS
330 RTE_SET_USED(vma_len);
333 for (i = 0; i < hpi->num_pages[0]; i++) {
334 uint64_t hugepage_sz = hpi->hugepage_sz;
337 hugepg_tbl[i].file_id = i;
338 hugepg_tbl[i].size = hugepage_sz;
339 #ifdef RTE_EAL_SINGLE_FILE_SEGMENTS
340 eal_get_hugefile_temp_path(hugepg_tbl[i].filepath,
341 sizeof(hugepg_tbl[i].filepath), hpi->hugedir,
342 hugepg_tbl[i].file_id);
344 eal_get_hugefile_path(hugepg_tbl[i].filepath,
345 sizeof(hugepg_tbl[i].filepath), hpi->hugedir,
346 hugepg_tbl[i].file_id);
348 hugepg_tbl[i].filepath[sizeof(hugepg_tbl[i].filepath) - 1] = '\0';
351 /* for 32-bit systems, don't remap 1G and 16G pages, just reuse
352 * original map address as final map address.
354 else if ((hugepage_sz == RTE_PGSIZE_1G)
355 || (hugepage_sz == RTE_PGSIZE_16G)) {
356 hugepg_tbl[i].final_va = hugepg_tbl[i].orig_va;
357 hugepg_tbl[i].orig_va = NULL;
362 #ifndef RTE_EAL_SINGLE_FILE_SEGMENTS
363 else if (vma_len == 0) {
364 unsigned j, num_pages;
366 /* reserve a virtual area for next contiguous
367 * physical block: count the number of
368 * contiguous physical pages. */
369 for (j = i+1; j < hpi->num_pages[0] ; j++) {
370 #ifdef RTE_ARCH_PPC_64
371 /* The physical addresses are sorted in
372 * descending order on PPC64 */
373 if (hugepg_tbl[j].physaddr !=
374 hugepg_tbl[j-1].physaddr - hugepage_sz)
377 if (hugepg_tbl[j].physaddr !=
378 hugepg_tbl[j-1].physaddr + hugepage_sz)
383 vma_len = num_pages * hugepage_sz;
385 /* get the biggest virtual memory area up to
386 * vma_len. If it fails, vma_addr is NULL, so
387 * let the kernel provide the address. */
388 vma_addr = get_virtual_area(&vma_len, hpi->hugepage_sz);
389 if (vma_addr == NULL)
390 vma_len = hugepage_sz;
394 /* try to create hugepage file */
395 fd = open(hugepg_tbl[i].filepath, O_CREAT | O_RDWR, 0755);
397 RTE_LOG(ERR, EAL, "%s(): open failed: %s\n", __func__,
402 /* map the segment, and populate page tables,
403 * the kernel fills this segment with zeros */
404 virtaddr = mmap(vma_addr, hugepage_sz, PROT_READ | PROT_WRITE,
405 MAP_SHARED | MAP_POPULATE, fd, 0);
406 if (virtaddr == MAP_FAILED) {
407 RTE_LOG(ERR, EAL, "%s(): mmap failed: %s\n", __func__,
414 hugepg_tbl[i].orig_va = virtaddr;
417 hugepg_tbl[i].final_va = virtaddr;
420 /* set shared flock on the file. */
421 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
422 RTE_LOG(ERR, EAL, "%s(): Locking file failed:%s \n",
423 __func__, strerror(errno));
430 vma_addr = (char *)vma_addr + hugepage_sz;
431 vma_len -= hugepage_sz;
436 #ifdef RTE_EAL_SINGLE_FILE_SEGMENTS
439 * Remaps all hugepages into single file segments
442 remap_all_hugepages(struct hugepage_file *hugepg_tbl, struct hugepage_info *hpi)
445 unsigned i = 0, j, num_pages, page_idx = 0;
446 void *vma_addr = NULL, *old_addr = NULL, *page_addr = NULL;
448 size_t hugepage_sz = hpi->hugepage_sz;
449 size_t total_size, offset;
450 char filepath[MAX_HUGEPAGE_PATH];
451 phys_addr_t physaddr;
454 while (i < hpi->num_pages[0]) {
457 /* for 32-bit systems, don't remap 1G pages and 16G pages,
458 * just reuse original map address as final map address.
460 if ((hugepage_sz == RTE_PGSIZE_1G)
461 || (hugepage_sz == RTE_PGSIZE_16G)) {
462 hugepg_tbl[i].final_va = hugepg_tbl[i].orig_va;
463 hugepg_tbl[i].orig_va = NULL;
469 /* reserve a virtual area for next contiguous
470 * physical block: count the number of
471 * contiguous physical pages. */
472 for (j = i+1; j < hpi->num_pages[0] ; j++) {
473 #ifdef RTE_ARCH_PPC_64
474 /* The physical addresses are sorted in descending
476 if (hugepg_tbl[j].physaddr !=
477 hugepg_tbl[j-1].physaddr - hugepage_sz)
480 if (hugepg_tbl[j].physaddr !=
481 hugepg_tbl[j-1].physaddr + hugepage_sz)
486 vma_len = num_pages * hugepage_sz;
488 socket = hugepg_tbl[i].socket_id;
490 /* get the biggest virtual memory area up to
491 * vma_len. If it fails, vma_addr is NULL, so
492 * let the kernel provide the address. */
493 vma_addr = get_virtual_area(&vma_len, hpi->hugepage_sz);
495 /* If we can't find a big enough virtual area, work out how many pages
496 * we are going to get */
497 if (vma_addr == NULL)
499 else if (vma_len != num_pages * hugepage_sz) {
500 num_pages = vma_len / hugepage_sz;
505 hugepg_tbl[page_idx].file_id = page_idx;
506 eal_get_hugefile_path(filepath,
509 hugepg_tbl[page_idx].file_id);
511 /* try to create hugepage file */
512 fd = open(filepath, O_CREAT | O_RDWR, 0755);
514 RTE_LOG(ERR, EAL, "%s(): open failed: %s\n", __func__, strerror(errno));
521 /* unmap current segment */
523 munmap(vma_addr, total_size);
525 /* unmap original page */
526 munmap(hugepg_tbl[i].orig_va, hugepage_sz);
527 unlink(hugepg_tbl[i].filepath);
529 total_size += hugepage_sz;
533 /* map new, bigger segment, and populate page tables,
534 * the kernel fills this segment with zeros */
535 vma_addr = mmap(vma_addr, total_size,
536 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd, 0);
538 if (vma_addr == MAP_FAILED || vma_addr != old_addr) {
539 RTE_LOG(ERR, EAL, "%s(): mmap failed: %s\n", __func__, strerror(errno));
545 /* set shared flock on the file. */
546 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
547 RTE_LOG(ERR, EAL, "%s(): Locking file failed:%s \n",
548 __func__, strerror(errno));
553 snprintf(hugepg_tbl[page_idx].filepath, MAX_HUGEPAGE_PATH, "%s",
556 physaddr = rte_mem_virt2phy(vma_addr);
558 if (physaddr == RTE_BAD_PHYS_ADDR)
561 hugepg_tbl[page_idx].final_va = vma_addr;
563 hugepg_tbl[page_idx].physaddr = physaddr;
565 hugepg_tbl[page_idx].repeated = num_pages;
567 hugepg_tbl[page_idx].socket_id = socket;
571 /* verify the memory segment - that is, check that every VA corresponds
572 * to the physical address we expect to see
574 for (offset = 0; offset < vma_len; offset += hugepage_sz) {
575 uint64_t expected_physaddr;
577 expected_physaddr = hugepg_tbl[page_idx].physaddr + offset;
578 page_addr = RTE_PTR_ADD(vma_addr, offset);
579 physaddr = rte_mem_virt2phy(page_addr);
581 if (physaddr != expected_physaddr) {
582 RTE_LOG(ERR, EAL, "Segment sanity check failed: wrong physaddr "
583 "at %p (offset 0x%" PRIx64 ": 0x%" PRIx64
584 " (expected 0x%" PRIx64 ")\n",
585 page_addr, offset, physaddr, expected_physaddr);
593 /* zero out the rest */
594 memset(&hugepg_tbl[page_idx], 0, (hpi->num_pages[0] - page_idx) * sizeof(struct hugepage_file));
597 #else/* RTE_EAL_SINGLE_FILE_SEGMENTS=n */
599 /* Unmap all hugepages from original mapping */
601 unmap_all_hugepages_orig(struct hugepage_file *hugepg_tbl, struct hugepage_info *hpi)
604 for (i = 0; i < hpi->num_pages[0]; i++) {
605 if (hugepg_tbl[i].orig_va) {
606 munmap(hugepg_tbl[i].orig_va, hpi->hugepage_sz);
607 hugepg_tbl[i].orig_va = NULL;
612 #endif /* RTE_EAL_SINGLE_FILE_SEGMENTS */
615 * Parse /proc/self/numa_maps to get the NUMA socket ID for each huge
619 find_numasocket(struct hugepage_file *hugepg_tbl, struct hugepage_info *hpi)
623 unsigned i, hp_count = 0;
626 char hugedir_str[PATH_MAX];
629 f = fopen("/proc/self/numa_maps", "r");
631 RTE_LOG(NOTICE, EAL, "cannot open /proc/self/numa_maps,"
632 " consider that all memory is in socket_id 0\n");
636 snprintf(hugedir_str, sizeof(hugedir_str),
637 "%s/%s", hpi->hugedir, internal_config.hugefile_prefix);
640 while (fgets(buf, sizeof(buf), f) != NULL) {
642 /* ignore non huge page */
643 if (strstr(buf, " huge ") == NULL &&
644 strstr(buf, hugedir_str) == NULL)
648 virt_addr = strtoull(buf, &end, 16);
649 if (virt_addr == 0 || end == buf) {
650 RTE_LOG(ERR, EAL, "%s(): error in numa_maps parsing\n", __func__);
654 /* get node id (socket id) */
655 nodestr = strstr(buf, " N");
656 if (nodestr == NULL) {
657 RTE_LOG(ERR, EAL, "%s(): error in numa_maps parsing\n", __func__);
661 end = strstr(nodestr, "=");
663 RTE_LOG(ERR, EAL, "%s(): error in numa_maps parsing\n", __func__);
669 socket_id = strtoul(nodestr, &end, 0);
670 if ((nodestr[0] == '\0') || (end == NULL) || (*end != '\0')) {
671 RTE_LOG(ERR, EAL, "%s(): error in numa_maps parsing\n", __func__);
675 /* if we find this page in our mappings, set socket_id */
676 for (i = 0; i < hpi->num_pages[0]; i++) {
677 void *va = (void *)(unsigned long)virt_addr;
678 if (hugepg_tbl[i].orig_va == va) {
679 hugepg_tbl[i].socket_id = socket_id;
685 if (hp_count < hpi->num_pages[0])
697 cmp_physaddr(const void *a, const void *b)
699 #ifndef RTE_ARCH_PPC_64
700 const struct hugepage_file *p1 = (const struct hugepage_file *)a;
701 const struct hugepage_file *p2 = (const struct hugepage_file *)b;
703 /* PowerPC needs memory sorted in reverse order from x86 */
704 const struct hugepage_file *p1 = (const struct hugepage_file *)b;
705 const struct hugepage_file *p2 = (const struct hugepage_file *)a;
707 if (p1->physaddr < p2->physaddr)
709 else if (p1->physaddr > p2->physaddr)
716 * Uses mmap to create a shared memory area for storage of data
717 * Used in this file to store the hugepage file map on disk
720 create_shared_memory(const char *filename, const size_t mem_size)
723 int fd = open(filename, O_CREAT | O_RDWR, 0666);
726 if (ftruncate(fd, mem_size) < 0) {
730 retval = mmap(NULL, mem_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
736 * this copies *active* hugepages from one hugepage table to another.
737 * destination is typically the shared memory.
740 copy_hugepages_to_shared_mem(struct hugepage_file * dst, int dest_size,
741 const struct hugepage_file * src, int src_size)
743 int src_pos, dst_pos = 0;
745 for (src_pos = 0; src_pos < src_size; src_pos++) {
746 if (src[src_pos].final_va != NULL) {
747 /* error on overflow attempt */
748 if (dst_pos == dest_size)
750 memcpy(&dst[dst_pos], &src[src_pos], sizeof(struct hugepage_file));
758 unlink_hugepage_files(struct hugepage_file *hugepg_tbl,
759 unsigned num_hp_info)
761 unsigned socket, size;
762 int page, nrpages = 0;
764 /* get total number of hugepages */
765 for (size = 0; size < num_hp_info; size++)
766 for (socket = 0; socket < RTE_MAX_NUMA_NODES; socket++)
768 internal_config.hugepage_info[size].num_pages[socket];
770 for (page = 0; page < nrpages; page++) {
771 struct hugepage_file *hp = &hugepg_tbl[page];
773 if (hp->final_va != NULL && unlink(hp->filepath)) {
774 RTE_LOG(WARNING, EAL, "%s(): Removing %s failed: %s\n",
775 __func__, hp->filepath, strerror(errno));
782 * unmaps hugepages that are not going to be used. since we originally allocate
783 * ALL hugepages (not just those we need), additional unmapping needs to be done.
786 unmap_unneeded_hugepages(struct hugepage_file *hugepg_tbl,
787 struct hugepage_info *hpi,
788 unsigned num_hp_info)
790 unsigned socket, size;
791 int page, nrpages = 0;
793 /* get total number of hugepages */
794 for (size = 0; size < num_hp_info; size++)
795 for (socket = 0; socket < RTE_MAX_NUMA_NODES; socket++)
796 nrpages += internal_config.hugepage_info[size].num_pages[socket];
798 for (size = 0; size < num_hp_info; size++) {
799 for (socket = 0; socket < RTE_MAX_NUMA_NODES; socket++) {
800 unsigned pages_found = 0;
802 /* traverse until we have unmapped all the unused pages */
803 for (page = 0; page < nrpages; page++) {
804 struct hugepage_file *hp = &hugepg_tbl[page];
806 #ifdef RTE_EAL_SINGLE_FILE_SEGMENTS
807 /* if this page was already cleared */
808 if (hp->final_va == NULL)
812 /* find a page that matches the criteria */
813 if ((hp->size == hpi[size].hugepage_sz) &&
814 (hp->socket_id == (int) socket)) {
816 /* if we skipped enough pages, unmap the rest */
817 if (pages_found == hpi[size].num_pages[socket]) {
820 #ifdef RTE_EAL_SINGLE_FILE_SEGMENTS
821 unmap_len = hp->size * hp->repeated;
823 unmap_len = hp->size;
826 /* get start addr and len of the remaining segment */
827 munmap(hp->final_va, (size_t) unmap_len);
830 if (unlink(hp->filepath) == -1) {
831 RTE_LOG(ERR, EAL, "%s(): Removing %s failed: %s\n",
832 __func__, hp->filepath, strerror(errno));
836 #ifdef RTE_EAL_SINGLE_FILE_SEGMENTS
837 /* else, check how much do we need to map */
840 hpi[size].num_pages[socket] - pages_found;
842 /* if we need enough memory to fit into the segment */
843 if (hp->repeated <= nr_pg_left) {
844 pages_found += hp->repeated;
846 /* truncate the segment */
848 uint64_t final_size = nr_pg_left * hp->size;
849 uint64_t seg_size = hp->repeated * hp->size;
851 void * unmap_va = RTE_PTR_ADD(hp->final_va,
855 munmap(unmap_va, seg_size - final_size);
857 fd = open(hp->filepath, O_RDWR);
859 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
860 hp->filepath, strerror(errno));
863 if (ftruncate(fd, final_size) < 0) {
864 RTE_LOG(ERR, EAL, "Cannot truncate %s: %s\n",
865 hp->filepath, strerror(errno));
870 pages_found += nr_pg_left;
871 hp->repeated = nr_pg_left;
875 /* else, lock the page and skip */
882 } /* foreach socket */
883 } /* foreach pagesize */
888 static inline uint64_t
889 get_socket_mem_size(int socket)
894 for (i = 0; i < internal_config.num_hugepage_sizes; i++){
895 struct hugepage_info *hpi = &internal_config.hugepage_info[i];
896 if (hpi->hugedir != NULL)
897 size += hpi->hugepage_sz * hpi->num_pages[socket];
904 * This function is a NUMA-aware equivalent of calc_num_pages.
905 * It takes in the list of hugepage sizes and the
906 * number of pages thereof, and calculates the best number of
907 * pages of each size to fulfill the request for <memory> ram
910 calc_num_pages_per_socket(uint64_t * memory,
911 struct hugepage_info *hp_info,
912 struct hugepage_info *hp_used,
913 unsigned num_hp_info)
915 unsigned socket, j, i = 0;
916 unsigned requested, available;
917 int total_num_pages = 0;
918 uint64_t remaining_mem, cur_mem;
919 uint64_t total_mem = internal_config.memory;
921 if (num_hp_info == 0)
924 /* if specific memory amounts per socket weren't requested */
925 if (internal_config.force_sockets == 0) {
926 int cpu_per_socket[RTE_MAX_NUMA_NODES];
927 size_t default_size, total_size;
930 /* Compute number of cores per socket */
931 memset(cpu_per_socket, 0, sizeof(cpu_per_socket));
932 RTE_LCORE_FOREACH(lcore_id) {
933 cpu_per_socket[rte_lcore_to_socket_id(lcore_id)]++;
937 * Automatically spread requested memory amongst detected sockets according
938 * to number of cores from cpu mask present on each socket
940 total_size = internal_config.memory;
941 for (socket = 0; socket < RTE_MAX_NUMA_NODES && total_size != 0; socket++) {
943 /* Set memory amount per socket */
944 default_size = (internal_config.memory * cpu_per_socket[socket])
947 /* Limit to maximum available memory on socket */
948 default_size = RTE_MIN(default_size, get_socket_mem_size(socket));
951 memory[socket] = default_size;
952 total_size -= default_size;
956 * If some memory is remaining, try to allocate it by getting all
957 * available memory from sockets, one after the other
959 for (socket = 0; socket < RTE_MAX_NUMA_NODES && total_size != 0; socket++) {
960 /* take whatever is available */
961 default_size = RTE_MIN(get_socket_mem_size(socket) - memory[socket],
965 memory[socket] += default_size;
966 total_size -= default_size;
970 for (socket = 0; socket < RTE_MAX_NUMA_NODES && total_mem != 0; socket++) {
971 /* skips if the memory on specific socket wasn't requested */
972 for (i = 0; i < num_hp_info && memory[socket] != 0; i++){
973 hp_used[i].hugedir = hp_info[i].hugedir;
974 hp_used[i].num_pages[socket] = RTE_MIN(
975 memory[socket] / hp_info[i].hugepage_sz,
976 hp_info[i].num_pages[socket]);
978 cur_mem = hp_used[i].num_pages[socket] *
979 hp_used[i].hugepage_sz;
981 memory[socket] -= cur_mem;
982 total_mem -= cur_mem;
984 total_num_pages += hp_used[i].num_pages[socket];
986 /* check if we have met all memory requests */
987 if (memory[socket] == 0)
990 /* check if we have any more pages left at this size, if so
991 * move on to next size */
992 if (hp_used[i].num_pages[socket] == hp_info[i].num_pages[socket])
994 /* At this point we know that there are more pages available that are
995 * bigger than the memory we want, so lets see if we can get enough
996 * from other page sizes.
999 for (j = i+1; j < num_hp_info; j++)
1000 remaining_mem += hp_info[j].hugepage_sz *
1001 hp_info[j].num_pages[socket];
1003 /* is there enough other memory, if not allocate another page and quit */
1004 if (remaining_mem < memory[socket]){
1005 cur_mem = RTE_MIN(memory[socket],
1006 hp_info[i].hugepage_sz);
1007 memory[socket] -= cur_mem;
1008 total_mem -= cur_mem;
1009 hp_used[i].num_pages[socket]++;
1011 break; /* we are done with this socket*/
1014 /* if we didn't satisfy all memory requirements per socket */
1015 if (memory[socket] > 0) {
1016 /* to prevent icc errors */
1017 requested = (unsigned) (internal_config.socket_mem[socket] /
1019 available = requested -
1020 ((unsigned) (memory[socket] / 0x100000));
1021 RTE_LOG(ERR, EAL, "Not enough memory available on socket %u! "
1022 "Requested: %uMB, available: %uMB\n", socket,
1023 requested, available);
1028 /* if we didn't satisfy total memory requirements */
1029 if (total_mem > 0) {
1030 requested = (unsigned) (internal_config.memory / 0x100000);
1031 available = requested - (unsigned) (total_mem / 0x100000);
1032 RTE_LOG(ERR, EAL, "Not enough memory available! Requested: %uMB,"
1033 " available: %uMB\n", requested, available);
1036 return total_num_pages;
1040 * Prepare physical memory mapping: fill configuration structure with
1041 * these infos, return 0 on success.
1042 * 1. map N huge pages in separate files in hugetlbfs
1043 * 2. find associated physical addr
1044 * 3. find associated NUMA socket ID
1045 * 4. sort all huge pages by physical address
1046 * 5. remap these N huge pages in the correct order
1047 * 6. unmap the first mapping
1048 * 7. fill memsegs in configuration with contiguous zones
1051 rte_eal_hugepage_init(void)
1053 struct rte_mem_config *mcfg;
1054 struct hugepage_file *hugepage, *tmp_hp = NULL;
1055 struct hugepage_info used_hp[MAX_HUGEPAGE_SIZES];
1057 uint64_t memory[RTE_MAX_NUMA_NODES];
1060 int i, j, new_memseg;
1061 int nr_hugefiles, nr_hugepages = 0;
1063 #ifdef RTE_EAL_SINGLE_FILE_SEGMENTS
1064 int new_pages_count[MAX_HUGEPAGE_SIZES];
1067 test_proc_pagemap_readable();
1069 memset(used_hp, 0, sizeof(used_hp));
1071 /* get pointer to global configuration */
1072 mcfg = rte_eal_get_configuration()->mem_config;
1074 /* hugetlbfs can be disabled */
1075 if (internal_config.no_hugetlbfs) {
1076 addr = mmap(NULL, internal_config.memory, PROT_READ | PROT_WRITE,
1077 MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
1078 if (addr == MAP_FAILED) {
1079 RTE_LOG(ERR, EAL, "%s: mmap() failed: %s\n", __func__,
1083 mcfg->memseg[0].phys_addr = (phys_addr_t)(uintptr_t)addr;
1084 mcfg->memseg[0].addr = addr;
1085 mcfg->memseg[0].hugepage_sz = RTE_PGSIZE_4K;
1086 mcfg->memseg[0].len = internal_config.memory;
1087 mcfg->memseg[0].socket_id = 0;
1091 /* check if app runs on Xen Dom0 */
1092 if (internal_config.xen_dom0_support) {
1093 #ifdef RTE_LIBRTE_XEN_DOM0
1094 /* use dom0_mm kernel driver to init memory */
1095 if (rte_xen_dom0_memory_init() < 0)
1102 /* calculate total number of hugepages available. at this point we haven't
1103 * yet started sorting them so they all are on socket 0 */
1104 for (i = 0; i < (int) internal_config.num_hugepage_sizes; i++) {
1105 /* meanwhile, also initialize used_hp hugepage sizes in used_hp */
1106 used_hp[i].hugepage_sz = internal_config.hugepage_info[i].hugepage_sz;
1108 nr_hugepages += internal_config.hugepage_info[i].num_pages[0];
1112 * allocate a memory area for hugepage table.
1113 * this isn't shared memory yet. due to the fact that we need some
1114 * processing done on these pages, shared memory will be created
1117 tmp_hp = malloc(nr_hugepages * sizeof(struct hugepage_file));
1121 memset(tmp_hp, 0, nr_hugepages * sizeof(struct hugepage_file));
1123 hp_offset = 0; /* where we start the current page size entries */
1125 /* map all hugepages and sort them */
1126 for (i = 0; i < (int)internal_config.num_hugepage_sizes; i ++){
1127 struct hugepage_info *hpi;
1130 * we don't yet mark hugepages as used at this stage, so
1131 * we just map all hugepages available to the system
1132 * all hugepages are still located on socket 0
1134 hpi = &internal_config.hugepage_info[i];
1136 if (hpi->num_pages[0] == 0)
1139 /* map all hugepages available */
1140 if (map_all_hugepages(&tmp_hp[hp_offset], hpi, 1) < 0){
1141 RTE_LOG(DEBUG, EAL, "Failed to mmap %u MB hugepages\n",
1142 (unsigned)(hpi->hugepage_sz / 0x100000));
1146 /* find physical addresses and sockets for each hugepage */
1147 if (find_physaddrs(&tmp_hp[hp_offset], hpi) < 0){
1148 RTE_LOG(DEBUG, EAL, "Failed to find phys addr for %u MB pages\n",
1149 (unsigned)(hpi->hugepage_sz / 0x100000));
1153 if (find_numasocket(&tmp_hp[hp_offset], hpi) < 0){
1154 RTE_LOG(DEBUG, EAL, "Failed to find NUMA socket for %u MB pages\n",
1155 (unsigned)(hpi->hugepage_sz / 0x100000));
1159 qsort(&tmp_hp[hp_offset], hpi->num_pages[0],
1160 sizeof(struct hugepage_file), cmp_physaddr);
1162 #ifdef RTE_EAL_SINGLE_FILE_SEGMENTS
1163 /* remap all hugepages into single file segments */
1164 new_pages_count[i] = remap_all_hugepages(&tmp_hp[hp_offset], hpi);
1165 if (new_pages_count[i] < 0){
1166 RTE_LOG(DEBUG, EAL, "Failed to remap %u MB pages\n",
1167 (unsigned)(hpi->hugepage_sz / 0x100000));
1171 /* we have processed a num of hugepages of this size, so inc offset */
1172 hp_offset += new_pages_count[i];
1174 /* remap all hugepages */
1175 if (map_all_hugepages(&tmp_hp[hp_offset], hpi, 0) < 0){
1176 RTE_LOG(DEBUG, EAL, "Failed to remap %u MB pages\n",
1177 (unsigned)(hpi->hugepage_sz / 0x100000));
1181 /* unmap original mappings */
1182 if (unmap_all_hugepages_orig(&tmp_hp[hp_offset], hpi) < 0)
1185 /* we have processed a num of hugepages of this size, so inc offset */
1186 hp_offset += hpi->num_pages[0];
1190 #ifdef RTE_EAL_SINGLE_FILE_SEGMENTS
1192 for (i = 0; i < (int) internal_config.num_hugepage_sizes; i++) {
1193 nr_hugefiles += new_pages_count[i];
1196 nr_hugefiles = nr_hugepages;
1200 /* clean out the numbers of pages */
1201 for (i = 0; i < (int) internal_config.num_hugepage_sizes; i++)
1202 for (j = 0; j < RTE_MAX_NUMA_NODES; j++)
1203 internal_config.hugepage_info[i].num_pages[j] = 0;
1205 /* get hugepages for each socket */
1206 for (i = 0; i < nr_hugefiles; i++) {
1207 int socket = tmp_hp[i].socket_id;
1209 /* find a hugepage info with right size and increment num_pages */
1210 const int nb_hpsizes = RTE_MIN(MAX_HUGEPAGE_SIZES,
1211 (int)internal_config.num_hugepage_sizes);
1212 for (j = 0; j < nb_hpsizes; j++) {
1213 if (tmp_hp[i].size ==
1214 internal_config.hugepage_info[j].hugepage_sz) {
1215 #ifdef RTE_EAL_SINGLE_FILE_SEGMENTS
1216 internal_config.hugepage_info[j].num_pages[socket] +=
1219 internal_config.hugepage_info[j].num_pages[socket]++;
1225 /* make a copy of socket_mem, needed for number of pages calculation */
1226 for (i = 0; i < RTE_MAX_NUMA_NODES; i++)
1227 memory[i] = internal_config.socket_mem[i];
1229 /* calculate final number of pages */
1230 nr_hugepages = calc_num_pages_per_socket(memory,
1231 internal_config.hugepage_info, used_hp,
1232 internal_config.num_hugepage_sizes);
1234 /* error if not enough memory available */
1235 if (nr_hugepages < 0)
1239 for (i = 0; i < (int) internal_config.num_hugepage_sizes; i++) {
1240 for (j = 0; j < RTE_MAX_NUMA_NODES; j++) {
1241 if (used_hp[i].num_pages[j] > 0) {
1243 "Requesting %u pages of size %uMB"
1244 " from socket %i\n",
1245 used_hp[i].num_pages[j],
1247 (used_hp[i].hugepage_sz / 0x100000),
1253 /* create shared memory */
1254 hugepage = create_shared_memory(eal_hugepage_info_path(),
1255 nr_hugefiles * sizeof(struct hugepage_file));
1257 if (hugepage == NULL) {
1258 RTE_LOG(ERR, EAL, "Failed to create shared memory!\n");
1261 memset(hugepage, 0, nr_hugefiles * sizeof(struct hugepage_file));
1264 * unmap pages that we won't need (looks at used_hp).
1265 * also, sets final_va to NULL on pages that were unmapped.
1267 if (unmap_unneeded_hugepages(tmp_hp, used_hp,
1268 internal_config.num_hugepage_sizes) < 0) {
1269 RTE_LOG(ERR, EAL, "Unmapping and locking hugepages failed!\n");
1274 * copy stuff from malloc'd hugepage* to the actual shared memory.
1275 * this procedure only copies those hugepages that have final_va
1276 * not NULL. has overflow protection.
1278 if (copy_hugepages_to_shared_mem(hugepage, nr_hugefiles,
1279 tmp_hp, nr_hugefiles) < 0) {
1280 RTE_LOG(ERR, EAL, "Copying tables to shared memory failed!\n");
1284 /* free the hugepage backing files */
1285 if (internal_config.hugepage_unlink &&
1286 unlink_hugepage_files(tmp_hp, internal_config.num_hugepage_sizes) < 0) {
1287 RTE_LOG(ERR, EAL, "Unlinking hugepage files failed!\n");
1291 /* free the temporary hugepage table */
1295 /* find earliest free memseg - this is needed because in case of IVSHMEM,
1296 * segments might have already been initialized */
1297 for (j = 0; j < RTE_MAX_MEMSEG; j++)
1298 if (mcfg->memseg[j].addr == NULL) {
1299 /* move to previous segment and exit loop */
1304 for (i = 0; i < nr_hugefiles; i++) {
1307 /* if this is a new section, create a new memseg */
1310 else if (hugepage[i].socket_id != hugepage[i-1].socket_id)
1312 else if (hugepage[i].size != hugepage[i-1].size)
1315 #ifdef RTE_ARCH_PPC_64
1316 /* On PPC64 architecture, the mmap always start from higher
1317 * virtual address to lower address. Here, both the physical
1318 * address and virtual address are in descending order */
1319 else if ((hugepage[i-1].physaddr - hugepage[i].physaddr) !=
1322 else if (((unsigned long)hugepage[i-1].final_va -
1323 (unsigned long)hugepage[i].final_va) != hugepage[i].size)
1326 else if ((hugepage[i].physaddr - hugepage[i-1].physaddr) !=
1329 else if (((unsigned long)hugepage[i].final_va -
1330 (unsigned long)hugepage[i-1].final_va) != hugepage[i].size)
1336 if (j == RTE_MAX_MEMSEG)
1339 mcfg->memseg[j].phys_addr = hugepage[i].physaddr;
1340 mcfg->memseg[j].addr = hugepage[i].final_va;
1341 #ifdef RTE_EAL_SINGLE_FILE_SEGMENTS
1342 mcfg->memseg[j].len = hugepage[i].size * hugepage[i].repeated;
1344 mcfg->memseg[j].len = hugepage[i].size;
1346 mcfg->memseg[j].socket_id = hugepage[i].socket_id;
1347 mcfg->memseg[j].hugepage_sz = hugepage[i].size;
1349 /* continuation of previous memseg */
1351 #ifdef RTE_ARCH_PPC_64
1352 /* Use the phy and virt address of the last page as segment
1353 * address for IBM Power architecture */
1354 mcfg->memseg[j].phys_addr = hugepage[i].physaddr;
1355 mcfg->memseg[j].addr = hugepage[i].final_va;
1357 mcfg->memseg[j].len += mcfg->memseg[j].hugepage_sz;
1359 hugepage[i].memseg_id = j;
1362 if (i < nr_hugefiles) {
1363 RTE_LOG(ERR, EAL, "Can only reserve %d pages "
1364 "from %d requested\n"
1365 "Current %s=%d is not enough\n"
1366 "Please either increase it or request less amount "
1368 i, nr_hugefiles, RTE_STR(CONFIG_RTE_MAX_MEMSEG),
1381 * uses fstat to report the size of a file on disk
1387 if (fstat(fd, &st) < 0)
1393 * This creates the memory mappings in the secondary process to match that of
1394 * the server process. It goes through each memory segment in the DPDK runtime
1395 * configuration and finds the hugepages which form that segment, mapping them
1396 * in order to form a contiguous block in the virtual memory space
1399 rte_eal_hugepage_attach(void)
1401 const struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
1402 const struct hugepage_file *hp = NULL;
1403 unsigned num_hp = 0;
1404 unsigned i, s = 0; /* s used to track the segment number */
1406 int fd, fd_zero = -1, fd_hugepage = -1;
1408 if (aslr_enabled() > 0) {
1409 RTE_LOG(WARNING, EAL, "WARNING: Address Space Layout Randomization "
1410 "(ASLR) is enabled in the kernel.\n");
1411 RTE_LOG(WARNING, EAL, " This may cause issues with mapping memory "
1412 "into secondary processes\n");
1415 test_proc_pagemap_readable();
1417 if (internal_config.xen_dom0_support) {
1418 #ifdef RTE_LIBRTE_XEN_DOM0
1419 if (rte_xen_dom0_memory_attach() < 0) {
1420 RTE_LOG(ERR, EAL,"Failed to attach memory setments of primay "
1428 fd_zero = open("/dev/zero", O_RDONLY);
1430 RTE_LOG(ERR, EAL, "Could not open /dev/zero\n");
1433 fd_hugepage = open(eal_hugepage_info_path(), O_RDONLY);
1434 if (fd_hugepage < 0) {
1435 RTE_LOG(ERR, EAL, "Could not open %s\n", eal_hugepage_info_path());
1439 /* map all segments into memory to make sure we get the addrs */
1440 for (s = 0; s < RTE_MAX_MEMSEG; ++s) {
1444 * the first memory segment with len==0 is the one that
1445 * follows the last valid segment.
1447 if (mcfg->memseg[s].len == 0)
1450 #ifdef RTE_LIBRTE_IVSHMEM
1452 * if segment has ioremap address set, it's an IVSHMEM segment and
1453 * doesn't need mapping as it was already mapped earlier
1455 if (mcfg->memseg[s].ioremap_addr != 0)
1460 * fdzero is mmapped to get a contiguous block of virtual
1461 * addresses of the appropriate memseg size.
1462 * use mmap to get identical addresses as the primary process.
1464 base_addr = mmap(mcfg->memseg[s].addr, mcfg->memseg[s].len,
1465 PROT_READ, MAP_PRIVATE, fd_zero, 0);
1466 if (base_addr == MAP_FAILED ||
1467 base_addr != mcfg->memseg[s].addr) {
1468 RTE_LOG(ERR, EAL, "Could not mmap %llu bytes "
1469 "in /dev/zero to requested address [%p]: '%s'\n",
1470 (unsigned long long)mcfg->memseg[s].len,
1471 mcfg->memseg[s].addr, strerror(errno));
1472 if (aslr_enabled() > 0) {
1473 RTE_LOG(ERR, EAL, "It is recommended to "
1474 "disable ASLR in the kernel "
1475 "and retry running both primary "
1476 "and secondary processes\n");
1482 size = getFileSize(fd_hugepage);
1483 hp = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd_hugepage, 0);
1485 RTE_LOG(ERR, EAL, "Could not mmap %s\n", eal_hugepage_info_path());
1489 num_hp = size / sizeof(struct hugepage_file);
1490 RTE_LOG(DEBUG, EAL, "Analysing %u files\n", num_hp);
1493 while (s < RTE_MAX_MEMSEG && mcfg->memseg[s].len > 0){
1494 void *addr, *base_addr;
1495 uintptr_t offset = 0;
1496 size_t mapping_size;
1497 #ifdef RTE_LIBRTE_IVSHMEM
1499 * if segment has ioremap address set, it's an IVSHMEM segment and
1500 * doesn't need mapping as it was already mapped earlier
1502 if (mcfg->memseg[s].ioremap_addr != 0) {
1508 * free previously mapped memory so we can map the
1509 * hugepages into the space
1511 base_addr = mcfg->memseg[s].addr;
1512 munmap(base_addr, mcfg->memseg[s].len);
1514 /* find the hugepages for this segment and map them
1515 * we don't need to worry about order, as the server sorted the
1516 * entries before it did the second mmap of them */
1517 for (i = 0; i < num_hp && offset < mcfg->memseg[s].len; i++){
1518 if (hp[i].memseg_id == (int)s){
1519 fd = open(hp[i].filepath, O_RDWR);
1521 RTE_LOG(ERR, EAL, "Could not open %s\n",
1525 #ifdef RTE_EAL_SINGLE_FILE_SEGMENTS
1526 mapping_size = hp[i].size * hp[i].repeated;
1528 mapping_size = hp[i].size;
1530 addr = mmap(RTE_PTR_ADD(base_addr, offset),
1531 mapping_size, PROT_READ | PROT_WRITE,
1533 close(fd); /* close file both on success and on failure */
1534 if (addr == MAP_FAILED ||
1535 addr != RTE_PTR_ADD(base_addr, offset)) {
1536 RTE_LOG(ERR, EAL, "Could not mmap %s\n",
1540 offset+=mapping_size;
1543 RTE_LOG(DEBUG, EAL, "Mapped segment %u of size 0x%llx\n", s,
1544 (unsigned long long)mcfg->memseg[s].len);
1547 /* unmap the hugepage config file, since we are done using it */
1548 munmap((void *)(uintptr_t)hp, size);
1556 if (fd_hugepage >= 0)