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.
73 #include <sys/types.h>
75 #include <sys/queue.h>
80 #include <sys/ioctl.h>
84 #include <rte_memory.h>
85 #include <rte_memzone.h>
86 #include <rte_launch.h>
87 #include <rte_tailq.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"
102 * Huge page mapping under linux
104 * To reserve a big contiguous amount of memory, we use the hugepage
105 * feature of linux. For that, we need to have hugetlbfs mounted. This
106 * code will create many files in this directory (one per page) and
107 * map them in virtual memory. For each page, we will retrieve its
108 * physical address and remap it in order to have a virtual contiguous
109 * zone as well as a physical contiguous zone.
112 static uint64_t baseaddr_offset;
114 #define RANDOMIZE_VA_SPACE_FILE "/proc/sys/kernel/randomize_va_space"
117 get_physaddr(void * virtaddr)
120 uint64_t page, physaddr;
121 unsigned long virt_pfn;
124 /* standard page size */
125 page_size = getpagesize();
127 fd = open("/proc/self/pagemap", O_RDONLY);
129 RTE_LOG(ERR, EAL, "%s(): cannot open /proc/self/pagemap: %s\n",
130 __func__, strerror(errno));
131 return (uint64_t) -1;
135 virt_pfn = (unsigned long)virtaddr / page_size;
136 offset = sizeof(uint64_t) * virt_pfn;
137 if (lseek(fd, offset, SEEK_SET) == (off_t) -1) {
138 RTE_LOG(ERR, EAL, "%s(): seek error in /proc/self/pagemap: %s\n",
139 __func__, strerror(errno));
141 return (uint64_t) -1;
143 if (read(fd, &page, sizeof(uint64_t)) < 0) {
144 RTE_LOG(ERR, EAL, "%s(): cannot read /proc/self/pagemap: %s\n",
145 __func__, strerror(errno));
147 return (uint64_t) -1;
151 * the pfn (page frame number) are bits 0-54 (see
152 * pagemap.txt in linux Documentation)
154 physaddr = ((page & 0x7fffffffffffffULL) * page_size);
160 * For each hugepage in hugepg_tbl, fill the physaddr value. We find
161 * it by browsing the /proc/self/pagemap special file.
164 find_physaddrs(struct hugepage_file *hugepg_tbl, struct hugepage_info *hpi)
169 for (i = 0; i < hpi->num_pages[0]; i++) {
170 addr = get_physaddr(hugepg_tbl[i].orig_va);
171 if (addr == (phys_addr_t) -1)
173 hugepg_tbl[i].physaddr = addr;
179 * Check whether address-space layout randomization is enabled in
180 * the kernel. This is important for multi-process as it can prevent
181 * two processes mapping data to the same virtual address
183 * 0 - address space randomization disabled
184 * 1/2 - address space randomization enabled
185 * negative error code on error
191 int retval, fd = open(RANDOMIZE_VA_SPACE_FILE, O_RDONLY);
194 retval = read(fd, &c, 1);
204 default: return -EINVAL;
209 * Try to mmap *size bytes in /dev/zero. If it is succesful, return the
210 * pointer to the mmap'd area and keep *size unmodified. Else, retry
211 * with a smaller zone: decrease *size by hugepage_sz until it reaches
212 * 0. In this case, return NULL. Note: this function returns an address
213 * which is a multiple of hugepage size.
216 get_virtual_area(size_t *size, size_t hugepage_sz)
222 if (internal_config.base_virtaddr != 0) {
223 addr = (void*) (uintptr_t) (internal_config.base_virtaddr +
228 RTE_LOG(INFO, EAL, "Ask a virtual area of 0x%zx bytes\n", *size);
230 fd = open("/dev/zero", O_RDONLY);
232 RTE_LOG(ERR, EAL, "Cannot open /dev/zero\n");
237 (*size) + hugepage_sz, PROT_READ, MAP_PRIVATE, fd, 0);
238 if (addr == MAP_FAILED)
239 *size -= hugepage_sz;
240 } while (addr == MAP_FAILED && *size > 0);
242 if (addr == MAP_FAILED) {
244 RTE_LOG(INFO, EAL, "Cannot get a virtual area\n");
248 munmap(addr, (*size) + hugepage_sz);
251 /* align addr to a huge page size boundary */
252 aligned_addr = (long)addr;
253 aligned_addr += (hugepage_sz - 1);
254 aligned_addr &= (~(hugepage_sz - 1));
255 addr = (void *)(aligned_addr);
257 RTE_LOG(INFO, EAL, "Virtual area found at %p (size = 0x%zx)\n",
260 /* increment offset */
261 baseaddr_offset += *size;
267 * Mmap all hugepages of hugepage table: it first open a file in
268 * hugetlbfs, then mmap() hugepage_sz data in it. If orig is set, the
269 * virtual address is stored in hugepg_tbl[i].orig_va, else it is stored
270 * in hugepg_tbl[i].final_va. The second mapping (when orig is 0) tries to
271 * map continguous physical blocks in contiguous virtual blocks.
274 map_all_hugepages(struct hugepage_file *hugepg_tbl,
275 struct hugepage_info *hpi, int orig)
280 void *vma_addr = NULL;
283 #ifdef RTE_EAL_SINGLE_FILE_SEGMENTS
284 RTE_SET_USED(vma_len);
287 for (i = 0; i < hpi->num_pages[0]; i++) {
288 size_t hugepage_sz = hpi->hugepage_sz;
291 hugepg_tbl[i].file_id = i;
292 hugepg_tbl[i].size = hugepage_sz;
293 #ifdef RTE_EAL_SINGLE_FILE_SEGMENTS
294 eal_get_hugefile_temp_path(hugepg_tbl[i].filepath,
295 sizeof(hugepg_tbl[i].filepath), hpi->hugedir,
296 hugepg_tbl[i].file_id);
298 eal_get_hugefile_path(hugepg_tbl[i].filepath,
299 sizeof(hugepg_tbl[i].filepath), hpi->hugedir,
300 hugepg_tbl[i].file_id);
302 hugepg_tbl[i].filepath[sizeof(hugepg_tbl[i].filepath) - 1] = '\0';
304 #ifndef RTE_ARCH_X86_64
305 /* for 32-bit systems, don't remap 1G pages, just reuse original
306 * map address as final map address.
308 else if (hugepage_sz == RTE_PGSIZE_1G){
309 hugepg_tbl[i].final_va = hugepg_tbl[i].orig_va;
310 hugepg_tbl[i].orig_va = NULL;
315 #ifndef RTE_EAL_SINGLE_FILE_SEGMENTS
316 else if (vma_len == 0) {
317 unsigned j, num_pages;
319 /* reserve a virtual area for next contiguous
320 * physical block: count the number of
321 * contiguous physical pages. */
322 for (j = i+1; j < hpi->num_pages[0] ; j++) {
323 if (hugepg_tbl[j].physaddr !=
324 hugepg_tbl[j-1].physaddr + hugepage_sz)
328 vma_len = num_pages * hugepage_sz;
330 /* get the biggest virtual memory area up to
331 * vma_len. If it fails, vma_addr is NULL, so
332 * let the kernel provide the address. */
333 vma_addr = get_virtual_area(&vma_len, hpi->hugepage_sz);
334 if (vma_addr == NULL)
335 vma_len = hugepage_sz;
339 /* try to create hugepage file */
340 fd = open(hugepg_tbl[i].filepath, O_CREAT | O_RDWR, 0755);
342 RTE_LOG(ERR, EAL, "%s(): open failed: %s\n", __func__,
347 virtaddr = mmap(vma_addr, hugepage_sz, PROT_READ | PROT_WRITE,
349 if (virtaddr == MAP_FAILED) {
350 RTE_LOG(ERR, EAL, "%s(): mmap failed: %s\n", __func__,
357 hugepg_tbl[i].orig_va = virtaddr;
358 memset(virtaddr, 0, hugepage_sz);
361 hugepg_tbl[i].final_va = virtaddr;
364 /* set shared flock on the file. */
365 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
366 RTE_LOG(ERR, EAL, "%s(): Locking file failed:%s \n",
367 __func__, strerror(errno));
374 vma_addr = (char *)vma_addr + hugepage_sz;
375 vma_len -= hugepage_sz;
380 #ifdef RTE_EAL_SINGLE_FILE_SEGMENTS
383 * Remaps all hugepages into single file segments
386 remap_all_hugepages(struct hugepage_file *hugepg_tbl, struct hugepage_info *hpi)
389 unsigned i = 0, j, num_pages, page_idx = 0;
390 void *vma_addr = NULL, *old_addr = NULL, *page_addr = NULL;
392 size_t hugepage_sz = hpi->hugepage_sz;
393 size_t total_size, offset;
394 char filepath[MAX_HUGEPAGE_PATH];
395 phys_addr_t physaddr;
398 while (i < hpi->num_pages[0]) {
400 #ifndef RTE_ARCH_X86_64
401 /* for 32-bit systems, don't remap 1G pages, just reuse original
402 * map address as final map address.
404 if (hugepage_sz == RTE_PGSIZE_1G){
405 hugepg_tbl[i].final_va = hugepg_tbl[i].orig_va;
406 hugepg_tbl[i].orig_va = NULL;
412 /* reserve a virtual area for next contiguous
413 * physical block: count the number of
414 * contiguous physical pages. */
415 for (j = i+1; j < hpi->num_pages[0] ; j++) {
416 if (hugepg_tbl[j].physaddr != hugepg_tbl[j-1].physaddr + hugepage_sz)
420 vma_len = num_pages * hugepage_sz;
422 socket = hugepg_tbl[i].socket_id;
424 /* get the biggest virtual memory area up to
425 * vma_len. If it fails, vma_addr is NULL, so
426 * let the kernel provide the address. */
427 vma_addr = get_virtual_area(&vma_len, hpi->hugepage_sz);
429 /* If we can't find a big enough virtual area, work out how many pages
430 * we are going to get */
431 if (vma_addr == NULL)
433 else if (vma_len != num_pages * hugepage_sz) {
434 num_pages = vma_len / hugepage_sz;
439 hugepg_tbl[page_idx].file_id = page_idx;
440 eal_get_hugefile_path(filepath,
443 hugepg_tbl[page_idx].file_id);
445 /* try to create hugepage file */
446 fd = open(filepath, O_CREAT | O_RDWR, 0755);
448 RTE_LOG(ERR, EAL, "%s(): open failed: %s\n", __func__, strerror(errno));
455 /* unmap current segment */
457 munmap(vma_addr, total_size);
459 /* unmap original page */
460 munmap(hugepg_tbl[i].orig_va, hugepage_sz);
461 unlink(hugepg_tbl[i].filepath);
463 total_size += hugepage_sz;
467 /* map new, bigger segment */
468 vma_addr = mmap(vma_addr, total_size,
469 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
471 if (vma_addr == MAP_FAILED || vma_addr != old_addr) {
472 RTE_LOG(ERR, EAL, "%s(): mmap failed: %s\n", __func__, strerror(errno));
477 /* touch the page. this is needed because kernel postpones mapping
478 * creation until the first page fault. with this, we pin down
479 * the page and it is marked as used and gets into process' pagemap.
481 for (offset = 0; offset < total_size; offset += hugepage_sz)
482 *((volatile uint8_t*) RTE_PTR_ADD(vma_addr, offset));
485 /* set shared flock on the file. */
486 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
487 RTE_LOG(ERR, EAL, "%s(): Locking file failed:%s \n",
488 __func__, strerror(errno));
493 rte_snprintf(hugepg_tbl[page_idx].filepath, MAX_HUGEPAGE_PATH, "%s",
496 physaddr = get_physaddr(vma_addr);
498 if (physaddr == (phys_addr_t) -1)
501 hugepg_tbl[page_idx].final_va = vma_addr;
503 hugepg_tbl[page_idx].physaddr = physaddr;
505 hugepg_tbl[page_idx].repeated = num_pages;
507 hugepg_tbl[page_idx].socket_id = socket;
511 /* verify the memory segment - that is, check that every VA corresponds
512 * to the physical address we expect to see
514 for (offset = 0; offset < vma_len; offset += hugepage_sz) {
515 uint64_t expected_physaddr;
517 expected_physaddr = hugepg_tbl[page_idx].physaddr + offset;
518 page_addr = RTE_PTR_ADD(vma_addr, offset);
519 physaddr = get_physaddr(page_addr);
521 if (physaddr != expected_physaddr) {
522 RTE_LOG(ERR, EAL, "Segment sanity check failed: wrong physaddr "
523 "at %p (offset 0x%" PRIx64 ": 0x%" PRIx64
524 " (expected 0x%" PRIx64 ")\n",
525 page_addr, offset, physaddr, expected_physaddr);
530 /* zero out the whole segment */
531 memset(hugepg_tbl[page_idx].final_va, 0, total_size);
536 /* zero out the rest */
537 memset(&hugepg_tbl[page_idx], 0, (hpi->num_pages[0] - page_idx) * sizeof(struct hugepage_file));
540 #else/* RTE_EAL_SINGLE_FILE_SEGMENTS=n */
542 /* Unmap all hugepages from original mapping */
544 unmap_all_hugepages_orig(struct hugepage_file *hugepg_tbl, struct hugepage_info *hpi)
547 for (i = 0; i < hpi->num_pages[0]; i++) {
548 if (hugepg_tbl[i].orig_va) {
549 munmap(hugepg_tbl[i].orig_va, hpi->hugepage_sz);
550 hugepg_tbl[i].orig_va = NULL;
555 #endif /* RTE_EAL_SINGLE_FILE_SEGMENTS */
558 * Parse /proc/self/numa_maps to get the NUMA socket ID for each huge
562 find_numasocket(struct hugepage_file *hugepg_tbl, struct hugepage_info *hpi)
566 unsigned i, hp_count = 0;
569 char hugedir_str[PATH_MAX];
572 f = fopen("/proc/self/numa_maps", "r");
574 RTE_LOG(INFO, EAL, "cannot open /proc/self/numa_maps,"
575 " consider that all memory is in socket_id 0\n");
579 rte_snprintf(hugedir_str, sizeof(hugedir_str),
580 "%s/", hpi->hugedir);
583 while (fgets(buf, sizeof(buf), f) != NULL) {
585 /* ignore non huge page */
586 if (strstr(buf, " huge ") == NULL &&
587 strstr(buf, hugedir_str) == NULL)
591 virt_addr = strtoull(buf, &end, 16);
592 if (virt_addr == 0 || end == buf) {
593 RTE_LOG(ERR, EAL, "%s(): error in numa_maps parsing\n", __func__);
597 /* get node id (socket id) */
598 nodestr = strstr(buf, " N");
599 if (nodestr == NULL) {
600 RTE_LOG(ERR, EAL, "%s(): error in numa_maps parsing\n", __func__);
604 end = strstr(nodestr, "=");
606 RTE_LOG(ERR, EAL, "%s(): error in numa_maps parsing\n", __func__);
612 socket_id = strtoul(nodestr, &end, 0);
613 if ((nodestr[0] == '\0') || (end == NULL) || (*end != '\0')) {
614 RTE_LOG(ERR, EAL, "%s(): error in numa_maps parsing\n", __func__);
618 /* if we find this page in our mappings, set socket_id */
619 for (i = 0; i < hpi->num_pages[0]; i++) {
620 void *va = (void *)(unsigned long)virt_addr;
621 if (hugepg_tbl[i].orig_va == va) {
622 hugepg_tbl[i].socket_id = socket_id;
628 if (hp_count < hpi->num_pages[0])
640 * Sort the hugepg_tbl by physical address (lower addresses first). We
641 * use a slow algorithm, but we won't have millions of pages, and this
642 * is only done at init time.
645 sort_by_physaddr(struct hugepage_file *hugepg_tbl, struct hugepage_info *hpi)
649 uint64_t smallest_addr;
650 struct hugepage_file tmp;
652 for (i = 0; i < hpi->num_pages[0]; i++) {
657 * browse all entries starting at 'i', and find the
658 * entry with the smallest addr
660 for (j=i; j< hpi->num_pages[0]; j++) {
662 if (smallest_addr == 0 ||
663 hugepg_tbl[j].physaddr < smallest_addr) {
664 smallest_addr = hugepg_tbl[j].physaddr;
669 /* should not happen */
670 if (smallest_idx == -1) {
671 RTE_LOG(ERR, EAL, "%s(): error in physaddr sorting\n", __func__);
675 /* swap the 2 entries in the table */
676 memcpy(&tmp, &hugepg_tbl[smallest_idx], sizeof(struct hugepage_file));
677 memcpy(&hugepg_tbl[smallest_idx], &hugepg_tbl[i],
678 sizeof(struct hugepage_file));
679 memcpy(&hugepg_tbl[i], &tmp, sizeof(struct hugepage_file));
685 * Uses mmap to create a shared memory area for storage of data
686 * Used in this file to store the hugepage file map on disk
689 create_shared_memory(const char *filename, const size_t mem_size)
692 int fd = open(filename, O_CREAT | O_RDWR, 0666);
695 if (ftruncate(fd, mem_size) < 0) {
699 retval = mmap(NULL, mem_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
705 * this copies *active* hugepages from one hugepage table to another.
706 * destination is typically the shared memory.
709 copy_hugepages_to_shared_mem(struct hugepage_file * dst, int dest_size,
710 const struct hugepage_file * src, int src_size)
712 int src_pos, dst_pos = 0;
714 for (src_pos = 0; src_pos < src_size; src_pos++) {
715 if (src[src_pos].final_va != NULL) {
716 /* error on overflow attempt */
717 if (dst_pos == dest_size)
719 memcpy(&dst[dst_pos], &src[src_pos], sizeof(struct hugepage_file));
727 * unmaps hugepages that are not going to be used. since we originally allocate
728 * ALL hugepages (not just those we need), additional unmapping needs to be done.
731 unmap_unneeded_hugepages(struct hugepage_file *hugepg_tbl,
732 struct hugepage_info *hpi,
733 unsigned num_hp_info)
735 unsigned socket, size;
736 int page, nrpages = 0;
738 /* get total number of hugepages */
739 for (size = 0; size < num_hp_info; size++)
740 for (socket = 0; socket < RTE_MAX_NUMA_NODES; socket++)
741 nrpages += internal_config.hugepage_info[size].num_pages[socket];
743 for (size = 0; size < num_hp_info; size++) {
744 for (socket = 0; socket < RTE_MAX_NUMA_NODES; socket++) {
745 unsigned pages_found = 0;
747 /* traverse until we have unmapped all the unused pages */
748 for (page = 0; page < nrpages; page++) {
749 struct hugepage_file *hp = &hugepg_tbl[page];
751 #ifdef RTE_EAL_SINGLE_FILE_SEGMENTS
752 /* if this page was already cleared */
753 if (hp->final_va == NULL)
757 /* find a page that matches the criteria */
758 if ((hp->size == hpi[size].hugepage_sz) &&
759 (hp->socket_id == (int) socket)) {
761 /* if we skipped enough pages, unmap the rest */
762 if (pages_found == hpi[size].num_pages[socket]) {
765 #ifdef RTE_EAL_SINGLE_FILE_SEGMENTS
766 unmap_len = hp->size * hp->repeated;
768 unmap_len = hp->size;
771 /* get start addr and len of the remaining segment */
772 munmap(hp->final_va, (size_t) unmap_len);
775 if (unlink(hp->filepath) == -1) {
776 RTE_LOG(ERR, EAL, "%s(): Removing %s failed: %s\n",
777 __func__, hp->filepath, strerror(errno));
781 #ifdef RTE_EAL_SINGLE_FILE_SEGMENTS
782 /* else, check how much do we need to map */
785 hpi[size].num_pages[socket] - pages_found;
787 /* if we need enough memory to fit into the segment */
788 if (hp->repeated <= nr_pg_left) {
789 pages_found += hp->repeated;
791 /* truncate the segment */
793 uint64_t final_size = nr_pg_left * hp->size;
794 uint64_t seg_size = hp->repeated * hp->size;
796 void * unmap_va = RTE_PTR_ADD(hp->final_va,
800 munmap(unmap_va, seg_size - final_size);
802 fd = open(hp->filepath, O_RDWR);
804 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
805 hp->filepath, strerror(errno));
808 if (ftruncate(fd, final_size) < 0) {
809 RTE_LOG(ERR, EAL, "Cannot truncate %s: %s\n",
810 hp->filepath, strerror(errno));
815 pages_found += nr_pg_left;
816 hp->repeated = nr_pg_left;
820 /* else, lock the page and skip */
827 } /* foreach socket */
828 } /* foreach pagesize */
833 static inline uint64_t
834 get_socket_mem_size(int socket)
839 for (i = 0; i < internal_config.num_hugepage_sizes; i++){
840 struct hugepage_info *hpi = &internal_config.hugepage_info[i];
841 if (hpi->hugedir != NULL)
842 size += hpi->hugepage_sz * hpi->num_pages[socket];
849 * This function is a NUMA-aware equivalent of calc_num_pages.
850 * It takes in the list of hugepage sizes and the
851 * number of pages thereof, and calculates the best number of
852 * pages of each size to fulfill the request for <memory> ram
855 calc_num_pages_per_socket(uint64_t * memory,
856 struct hugepage_info *hp_info,
857 struct hugepage_info *hp_used,
858 unsigned num_hp_info)
860 unsigned socket, j, i = 0;
861 unsigned requested, available;
862 int total_num_pages = 0;
863 uint64_t remaining_mem, cur_mem;
864 uint64_t total_mem = internal_config.memory;
866 if (num_hp_info == 0)
869 for (socket = 0; socket < RTE_MAX_NUMA_NODES && total_mem != 0; socket++) {
870 /* if specific memory amounts per socket weren't requested */
871 if (internal_config.force_sockets == 0) {
872 /* take whatever is available */
873 memory[socket] = RTE_MIN(get_socket_mem_size(socket),
876 /* skips if the memory on specific socket wasn't requested */
877 for (i = 0; i < num_hp_info && memory[socket] != 0; i++){
878 hp_used[i].hugedir = hp_info[i].hugedir;
879 hp_used[i].num_pages[socket] = RTE_MIN(
880 memory[socket] / hp_info[i].hugepage_sz,
881 hp_info[i].num_pages[socket]);
883 cur_mem = hp_used[i].num_pages[socket] *
884 hp_used[i].hugepage_sz;
886 memory[socket] -= cur_mem;
887 total_mem -= cur_mem;
889 total_num_pages += hp_used[i].num_pages[socket];
891 /* check if we have met all memory requests */
892 if (memory[socket] == 0)
895 /* check if we have any more pages left at this size, if so
896 * move on to next size */
897 if (hp_used[i].num_pages[socket] == hp_info[i].num_pages[socket])
899 /* At this point we know that there are more pages available that are
900 * bigger than the memory we want, so lets see if we can get enough
901 * from other page sizes.
904 for (j = i+1; j < num_hp_info; j++)
905 remaining_mem += hp_info[j].hugepage_sz *
906 hp_info[j].num_pages[socket];
908 /* is there enough other memory, if not allocate another page and quit */
909 if (remaining_mem < memory[socket]){
910 cur_mem = RTE_MIN(memory[socket],
911 hp_info[i].hugepage_sz);
912 memory[socket] -= cur_mem;
913 total_mem -= cur_mem;
914 hp_used[i].num_pages[socket]++;
916 break; /* we are done with this socket*/
919 /* if we didn't satisfy all memory requirements per socket */
920 if (memory[socket] > 0) {
921 /* to prevent icc errors */
922 requested = (unsigned) (internal_config.socket_mem[socket] /
924 available = requested -
925 ((unsigned) (memory[socket] / 0x100000));
926 RTE_LOG(INFO, EAL, "Not enough memory available on socket %u! "
927 "Requested: %uMB, available: %uMB\n", socket,
928 requested, available);
933 /* if we didn't satisfy total memory requirements */
935 requested = (unsigned) (internal_config.memory / 0x100000);
936 available = requested - (unsigned) (total_mem / 0x100000);
937 RTE_LOG(INFO, EAL, "Not enough memory available! Requested: %uMB,"
938 " available: %uMB\n", requested, available);
941 return total_num_pages;
945 * Prepare physical memory mapping: fill configuration structure with
946 * these infos, return 0 on success.
947 * 1. map N huge pages in separate files in hugetlbfs
948 * 2. find associated physical addr
949 * 3. find associated NUMA socket ID
950 * 4. sort all huge pages by physical address
951 * 5. remap these N huge pages in the correct order
952 * 6. unmap the first mapping
953 * 7. fill memsegs in configuration with contiguous zones
956 rte_eal_hugepage_init(void)
958 struct rte_mem_config *mcfg;
959 struct hugepage_file *hugepage, *tmp_hp = NULL;
960 struct hugepage_info used_hp[MAX_HUGEPAGE_SIZES];
962 uint64_t memory[RTE_MAX_NUMA_NODES];
965 int i, j, new_memseg;
966 int nr_hugefiles, nr_hugepages = 0;
968 #ifdef RTE_EAL_SINGLE_FILE_SEGMENTS
969 int new_pages_count[MAX_HUGEPAGE_SIZES];
972 memset(used_hp, 0, sizeof(used_hp));
974 /* get pointer to global configuration */
975 mcfg = rte_eal_get_configuration()->mem_config;
977 /* for debug purposes, hugetlbfs can be disabled */
978 if (internal_config.no_hugetlbfs) {
979 addr = malloc(internal_config.memory);
980 mcfg->memseg[0].phys_addr = (phys_addr_t)(uintptr_t)addr;
981 mcfg->memseg[0].addr = addr;
982 mcfg->memseg[0].len = internal_config.memory;
983 mcfg->memseg[0].socket_id = 0;
988 /* calculate total number of hugepages available. at this point we haven't
989 * yet started sorting them so they all are on socket 0 */
990 for (i = 0; i < (int) internal_config.num_hugepage_sizes; i++) {
991 /* meanwhile, also initialize used_hp hugepage sizes in used_hp */
992 used_hp[i].hugepage_sz = internal_config.hugepage_info[i].hugepage_sz;
994 nr_hugepages += internal_config.hugepage_info[i].num_pages[0];
998 * allocate a memory area for hugepage table.
999 * this isn't shared memory yet. due to the fact that we need some
1000 * processing done on these pages, shared memory will be created
1003 tmp_hp = malloc(nr_hugepages * sizeof(struct hugepage_file));
1007 memset(tmp_hp, 0, nr_hugepages * sizeof(struct hugepage_file));
1009 hp_offset = 0; /* where we start the current page size entries */
1011 /* map all hugepages and sort them */
1012 for (i = 0; i < (int)internal_config.num_hugepage_sizes; i ++){
1013 struct hugepage_info *hpi;
1016 * we don't yet mark hugepages as used at this stage, so
1017 * we just map all hugepages available to the system
1018 * all hugepages are still located on socket 0
1020 hpi = &internal_config.hugepage_info[i];
1022 if (hpi->num_pages[0] == 0)
1025 /* map all hugepages available */
1026 if (map_all_hugepages(&tmp_hp[hp_offset], hpi, 1) < 0){
1027 RTE_LOG(DEBUG, EAL, "Failed to mmap %u MB hugepages\n",
1028 (unsigned)(hpi->hugepage_sz / 0x100000));
1032 /* find physical addresses and sockets for each hugepage */
1033 if (find_physaddrs(&tmp_hp[hp_offset], hpi) < 0){
1034 RTE_LOG(DEBUG, EAL, "Failed to find phys addr for %u MB pages\n",
1035 (unsigned)(hpi->hugepage_sz / 0x100000));
1039 if (find_numasocket(&tmp_hp[hp_offset], hpi) < 0){
1040 RTE_LOG(DEBUG, EAL, "Failed to find NUMA socket for %u MB pages\n",
1041 (unsigned)(hpi->hugepage_sz / 0x100000));
1045 if (sort_by_physaddr(&tmp_hp[hp_offset], hpi) < 0)
1048 #ifdef RTE_EAL_SINGLE_FILE_SEGMENTS
1049 /* remap all hugepages into single file segments */
1050 new_pages_count[i] = remap_all_hugepages(&tmp_hp[hp_offset], hpi);
1051 if (new_pages_count[i] < 0){
1052 RTE_LOG(DEBUG, EAL, "Failed to remap %u MB pages\n",
1053 (unsigned)(hpi->hugepage_sz / 0x100000));
1057 /* we have processed a num of hugepages of this size, so inc offset */
1058 hp_offset += new_pages_count[i];
1060 /* remap all hugepages */
1061 if (map_all_hugepages(&tmp_hp[hp_offset], hpi, 0) < 0){
1062 RTE_LOG(DEBUG, EAL, "Failed to remap %u MB pages\n",
1063 (unsigned)(hpi->hugepage_sz / 0x100000));
1067 /* unmap original mappings */
1068 if (unmap_all_hugepages_orig(&tmp_hp[hp_offset], hpi) < 0)
1071 /* we have processed a num of hugepages of this size, so inc offset */
1072 hp_offset += hpi->num_pages[0];
1076 #ifdef RTE_EAL_SINGLE_FILE_SEGMENTS
1078 for (i = 0; i < (int) internal_config.num_hugepage_sizes; i++) {
1079 nr_hugefiles += new_pages_count[i];
1082 nr_hugefiles = nr_hugepages;
1086 /* clean out the numbers of pages */
1087 for (i = 0; i < (int) internal_config.num_hugepage_sizes; i++)
1088 for (j = 0; j < RTE_MAX_NUMA_NODES; j++)
1089 internal_config.hugepage_info[i].num_pages[j] = 0;
1091 /* get hugepages for each socket */
1092 for (i = 0; i < nr_hugefiles; i++) {
1093 int socket = tmp_hp[i].socket_id;
1095 /* find a hugepage info with right size and increment num_pages */
1096 for (j = 0; j < (int) internal_config.num_hugepage_sizes; j++) {
1097 if (tmp_hp[i].size ==
1098 internal_config.hugepage_info[j].hugepage_sz) {
1099 #ifdef RTE_EAL_SINGLE_FILE_SEGMENTS
1100 internal_config.hugepage_info[j].num_pages[socket] +=
1103 internal_config.hugepage_info[j].num_pages[socket]++;
1109 /* make a copy of socket_mem, needed for number of pages calculation */
1110 for (i = 0; i < RTE_MAX_NUMA_NODES; i++)
1111 memory[i] = internal_config.socket_mem[i];
1113 /* calculate final number of pages */
1114 nr_hugepages = calc_num_pages_per_socket(memory,
1115 internal_config.hugepage_info, used_hp,
1116 internal_config.num_hugepage_sizes);
1118 /* error if not enough memory available */
1119 if (nr_hugepages < 0)
1123 for (i = 0; i < (int) internal_config.num_hugepage_sizes; i++) {
1124 for (j = 0; j < RTE_MAX_NUMA_NODES; j++) {
1125 if (used_hp[i].num_pages[j] > 0) {
1127 "Requesting %u pages of size %uMB"
1128 " from socket %i\n",
1129 used_hp[i].num_pages[j],
1131 (used_hp[i].hugepage_sz / 0x100000),
1137 /* create shared memory */
1138 hugepage = create_shared_memory(eal_hugepage_info_path(),
1139 nr_hugefiles * sizeof(struct hugepage_file));
1141 if (hugepage == NULL) {
1142 RTE_LOG(ERR, EAL, "Failed to create shared memory!\n");
1145 memset(hugepage, 0, nr_hugefiles * sizeof(struct hugepage_file));
1148 * unmap pages that we won't need (looks at used_hp).
1149 * also, sets final_va to NULL on pages that were unmapped.
1151 if (unmap_unneeded_hugepages(tmp_hp, used_hp,
1152 internal_config.num_hugepage_sizes) < 0) {
1153 RTE_LOG(ERR, EAL, "Unmapping and locking hugepages failed!\n");
1158 * copy stuff from malloc'd hugepage* to the actual shared memory.
1159 * this procedure only copies those hugepages that have final_va
1160 * not NULL. has overflow protection.
1162 if (copy_hugepages_to_shared_mem(hugepage, nr_hugefiles,
1163 tmp_hp, nr_hugefiles) < 0) {
1164 RTE_LOG(ERR, EAL, "Copying tables to shared memory failed!\n");
1168 /* free the temporary hugepage table */
1172 /* find earliest free memseg - this is needed because in case of IVSHMEM,
1173 * segments might have already been initialized */
1174 for (j = 0; j < RTE_MAX_MEMSEG; j++)
1175 if (mcfg->memseg[j].addr == NULL) {
1176 /* move to previous segment and exit loop */
1181 for (i = 0; i < nr_hugefiles; i++) {
1184 /* if this is a new section, create a new memseg */
1187 else if (hugepage[i].socket_id != hugepage[i-1].socket_id)
1189 else if (hugepage[i].size != hugepage[i-1].size)
1191 else if ((hugepage[i].physaddr - hugepage[i-1].physaddr) !=
1194 else if (((unsigned long)hugepage[i].final_va -
1195 (unsigned long)hugepage[i-1].final_va) != hugepage[i].size)
1200 if (j == RTE_MAX_MEMSEG)
1203 mcfg->memseg[j].phys_addr = hugepage[i].physaddr;
1204 mcfg->memseg[j].addr = hugepage[i].final_va;
1205 #ifdef RTE_EAL_SINGLE_FILE_SEGMENTS
1206 mcfg->memseg[j].len = hugepage[i].size * hugepage[i].repeated;
1208 mcfg->memseg[j].len = hugepage[i].size;
1210 mcfg->memseg[j].socket_id = hugepage[i].socket_id;
1211 mcfg->memseg[j].hugepage_sz = hugepage[i].size;
1213 /* continuation of previous memseg */
1215 mcfg->memseg[j].len += mcfg->memseg[j].hugepage_sz;
1217 hugepage[i].memseg_id = j;
1220 if (i < nr_hugefiles) {
1221 RTE_LOG(ERR, EAL, "Can only reserve %d pages "
1222 "from %d requested\n"
1223 "Current %s=%d is not enough\n"
1224 "Please either increase it or request less amount "
1226 i, nr_hugefiles, RTE_STR(CONFIG_RTE_MAX_MEMSEG),
1240 * uses fstat to report the size of a file on disk
1246 if (fstat(fd, &st) < 0)
1252 * This creates the memory mappings in the secondary process to match that of
1253 * the server process. It goes through each memory segment in the DPDK runtime
1254 * configuration and finds the hugepages which form that segment, mapping them
1255 * in order to form a contiguous block in the virtual memory space
1258 rte_eal_hugepage_attach(void)
1260 const struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
1261 const struct hugepage_file *hp = NULL;
1262 unsigned num_hp = 0;
1263 unsigned i, s = 0; /* s used to track the segment number */
1265 int fd, fd_zero = -1, fd_hugepage = -1;
1267 if (aslr_enabled() > 0) {
1268 RTE_LOG(WARNING, EAL, "WARNING: Address Space Layout Randomization "
1269 "(ASLR) is enabled in the kernel.\n");
1270 RTE_LOG(WARNING, EAL, " This may cause issues with mapping memory "
1271 "into secondary processes\n");
1274 fd_zero = open("/dev/zero", O_RDONLY);
1276 RTE_LOG(ERR, EAL, "Could not open /dev/zero\n");
1279 fd_hugepage = open(eal_hugepage_info_path(), O_RDONLY);
1280 if (fd_hugepage < 0) {
1281 RTE_LOG(ERR, EAL, "Could not open %s\n", eal_hugepage_info_path());
1285 /* map all segments into memory to make sure we get the addrs */
1286 for (s = 0; s < RTE_MAX_MEMSEG; ++s) {
1290 * the first memory segment with len==0 is the one that
1291 * follows the last valid segment.
1293 if (mcfg->memseg[s].len == 0)
1296 #ifdef RTE_LIBRTE_IVSHMEM
1298 * if segment has ioremap address set, it's an IVSHMEM segment and
1299 * doesn't need mapping as it was already mapped earlier
1301 if (mcfg->memseg[s].ioremap_addr != 0)
1306 * fdzero is mmapped to get a contiguous block of virtual
1307 * addresses of the appropriate memseg size.
1308 * use mmap to get identical addresses as the primary process.
1310 base_addr = mmap(mcfg->memseg[s].addr, mcfg->memseg[s].len,
1311 PROT_READ, MAP_PRIVATE, fd_zero, 0);
1312 if (base_addr == MAP_FAILED ||
1313 base_addr != mcfg->memseg[s].addr) {
1314 RTE_LOG(ERR, EAL, "Could not mmap %llu bytes "
1315 "in /dev/zero to requested address [%p]: '%s'\n",
1316 (unsigned long long)mcfg->memseg[s].len,
1317 mcfg->memseg[s].addr, strerror(errno));
1318 if (aslr_enabled() > 0) {
1319 RTE_LOG(ERR, EAL, "It is recommended to "
1320 "disable ASLR in the kernel "
1321 "and retry running both primary "
1322 "and secondary processes\n");
1328 size = getFileSize(fd_hugepage);
1329 hp = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd_hugepage, 0);
1331 RTE_LOG(ERR, EAL, "Could not mmap %s\n", eal_hugepage_info_path());
1335 num_hp = size / sizeof(struct hugepage_file);
1336 RTE_LOG(DEBUG, EAL, "Analysing %u files\n", num_hp);
1339 while (s < RTE_MAX_MEMSEG && mcfg->memseg[s].len > 0){
1340 void *addr, *base_addr;
1341 uintptr_t offset = 0;
1342 size_t mapping_size;
1343 #ifdef RTE_LIBRTE_IVSHMEM
1345 * if segment has ioremap address set, it's an IVSHMEM segment and
1346 * doesn't need mapping as it was already mapped earlier
1348 if (mcfg->memseg[s].ioremap_addr != 0) {
1354 * free previously mapped memory so we can map the
1355 * hugepages into the space
1357 base_addr = mcfg->memseg[s].addr;
1358 munmap(base_addr, mcfg->memseg[s].len);
1360 /* find the hugepages for this segment and map them
1361 * we don't need to worry about order, as the server sorted the
1362 * entries before it did the second mmap of them */
1363 for (i = 0; i < num_hp && offset < mcfg->memseg[s].len; i++){
1364 if (hp[i].memseg_id == (int)s){
1365 fd = open(hp[i].filepath, O_RDWR);
1367 RTE_LOG(ERR, EAL, "Could not open %s\n",
1371 #ifdef RTE_EAL_SINGLE_FILE_SEGMENTS
1372 mapping_size = hp[i].size * hp[i].repeated;
1374 mapping_size = hp[i].size;
1376 addr = mmap(RTE_PTR_ADD(base_addr, offset),
1377 mapping_size, PROT_READ | PROT_WRITE,
1379 close(fd); /* close file both on success and on failure */
1380 if (addr == MAP_FAILED ||
1381 addr != RTE_PTR_ADD(base_addr, offset)) {
1382 RTE_LOG(ERR, EAL, "Could not mmap %s\n",
1386 offset+=mapping_size;
1389 RTE_LOG(DEBUG, EAL, "Mapped segment %u of size 0x%llx\n", s,
1390 (unsigned long long)mcfg->memseg[s].len);
1393 /* unmap the hugepage config file, since we are done using it */
1394 munmap((void *)(uintptr_t)hp, size);
1402 if (fd_hugepage >= 0)
1408 rte_eal_memdevice_init(void)
1410 struct rte_config *config;
1412 if (rte_eal_process_type() == RTE_PROC_SECONDARY)
1415 config = rte_eal_get_configuration();
1416 config->mem_config->nchannel = internal_config.force_nchannel;
1417 config->mem_config->nrank = internal_config.force_nrank;
1423 /* init memory subsystem */
1425 rte_eal_memory_init(void)
1427 RTE_LOG(INFO, EAL, "Setting up memory...\n");
1428 const int retval = rte_eal_process_type() == RTE_PROC_PRIMARY ?
1429 rte_eal_hugepage_init() :
1430 rte_eal_hugepage_attach();
1434 if (internal_config.no_shconf == 0 && rte_eal_memdevice_init() < 0)