1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation.
3 * Copyright(c) 2013 6WIND S.A.
17 #include <sys/resource.h>
22 #ifdef F_ADD_SEALS /* if file sealing is supported, so is memfd */
23 #define MEMFD_SUPPORTED
25 #ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
30 #include <rte_errno.h>
32 #include <rte_memory.h>
34 #include <rte_lcore.h>
35 #include <rte_common.h>
37 #include "eal_private.h"
38 #include "eal_memalloc.h"
39 #include "eal_memcfg.h"
40 #include "eal_internal_cfg.h"
41 #include "eal_filesystem.h"
42 #include "eal_hugepages.h"
43 #include "eal_options.h"
45 #define PFN_MASK_SIZE 8
49 * Huge page mapping under linux
51 * To reserve a big contiguous amount of memory, we use the hugepage
52 * feature of linux. For that, we need to have hugetlbfs mounted. This
53 * code will create many files in this directory (one per page) and
54 * map them in virtual memory. For each page, we will retrieve its
55 * physical address and remap it in order to have a virtual contiguous
56 * zone as well as a physical contiguous zone.
59 static int phys_addrs_available = -1;
61 #define RANDOMIZE_VA_SPACE_FILE "/proc/sys/kernel/randomize_va_space"
63 uint64_t eal_get_baseaddr(void)
66 * Linux kernel uses a really high address as starting address for
67 * serving mmaps calls. If there exists addressing limitations and IOVA
68 * mode is VA, this starting address is likely too high for those
69 * devices. However, it is possible to use a lower address in the
70 * process virtual address space as with 64 bits there is a lot of
73 * Current known limitations are 39 or 40 bits. Setting the starting
74 * address at 4GB implies there are 508GB or 1020GB for mapping the
75 * available hugepages. This is likely enough for most systems, although
76 * a device with addressing limitations should call
77 * rte_mem_check_dma_mask for ensuring all memory is within supported
80 return 0x100000000ULL;
84 * Get physical address of any mapped virtual address in the current process.
87 rte_mem_virt2phy(const void *virtaddr)
90 uint64_t page, physaddr;
91 unsigned long virt_pfn;
95 if (phys_addrs_available == 0)
98 /* standard page size */
99 page_size = getpagesize();
101 fd = open("/proc/self/pagemap", O_RDONLY);
103 RTE_LOG(INFO, EAL, "%s(): cannot open /proc/self/pagemap: %s\n",
104 __func__, strerror(errno));
108 virt_pfn = (unsigned long)virtaddr / page_size;
109 offset = sizeof(uint64_t) * virt_pfn;
110 if (lseek(fd, offset, SEEK_SET) == (off_t) -1) {
111 RTE_LOG(INFO, EAL, "%s(): seek error in /proc/self/pagemap: %s\n",
112 __func__, strerror(errno));
117 retval = read(fd, &page, PFN_MASK_SIZE);
120 RTE_LOG(INFO, EAL, "%s(): cannot read /proc/self/pagemap: %s\n",
121 __func__, strerror(errno));
123 } else if (retval != PFN_MASK_SIZE) {
124 RTE_LOG(INFO, EAL, "%s(): read %d bytes from /proc/self/pagemap "
125 "but expected %d:\n",
126 __func__, retval, PFN_MASK_SIZE);
131 * the pfn (page frame number) are bits 0-54 (see
132 * pagemap.txt in linux Documentation)
134 if ((page & 0x7fffffffffffffULL) == 0)
137 physaddr = ((page & 0x7fffffffffffffULL) * page_size)
138 + ((unsigned long)virtaddr % page_size);
144 rte_mem_virt2iova(const void *virtaddr)
146 if (rte_eal_iova_mode() == RTE_IOVA_VA)
147 return (uintptr_t)virtaddr;
148 return rte_mem_virt2phy(virtaddr);
152 * For each hugepage in hugepg_tbl, fill the physaddr value. We find
153 * it by browsing the /proc/self/pagemap special file.
156 find_physaddrs(struct hugepage_file *hugepg_tbl, struct hugepage_info *hpi)
161 for (i = 0; i < hpi->num_pages[0]; i++) {
162 addr = rte_mem_virt2phy(hugepg_tbl[i].orig_va);
163 if (addr == RTE_BAD_PHYS_ADDR)
165 hugepg_tbl[i].physaddr = addr;
171 * For each hugepage in hugepg_tbl, fill the physaddr value sequentially.
174 set_physaddrs(struct hugepage_file *hugepg_tbl, struct hugepage_info *hpi)
177 static phys_addr_t addr;
179 for (i = 0; i < hpi->num_pages[0]; i++) {
180 hugepg_tbl[i].physaddr = addr;
181 addr += hugepg_tbl[i].size;
187 * Check whether address-space layout randomization is enabled in
188 * the kernel. This is important for multi-process as it can prevent
189 * two processes mapping data to the same virtual address
191 * 0 - address space randomization disabled
192 * 1/2 - address space randomization enabled
193 * negative error code on error
199 int retval, fd = open(RANDOMIZE_VA_SPACE_FILE, O_RDONLY);
202 retval = read(fd, &c, 1);
212 default: return -EINVAL;
216 static sigjmp_buf huge_jmpenv;
218 static void huge_sigbus_handler(int signo __rte_unused)
220 siglongjmp(huge_jmpenv, 1);
223 /* Put setjmp into a wrap method to avoid compiling error. Any non-volatile,
224 * non-static local variable in the stack frame calling sigsetjmp might be
225 * clobbered by a call to longjmp.
227 static int huge_wrap_sigsetjmp(void)
229 return sigsetjmp(huge_jmpenv, 1);
232 #ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
233 /* Callback for numa library. */
234 void numa_error(char *where)
236 RTE_LOG(ERR, EAL, "%s failed: %s\n", where, strerror(errno));
241 * Mmap all hugepages of hugepage table: it first open a file in
242 * hugetlbfs, then mmap() hugepage_sz data in it. If orig is set, the
243 * virtual address is stored in hugepg_tbl[i].orig_va, else it is stored
244 * in hugepg_tbl[i].final_va. The second mapping (when orig is 0) tries to
245 * map contiguous physical blocks in contiguous virtual blocks.
248 map_all_hugepages(struct hugepage_file *hugepg_tbl, struct hugepage_info *hpi,
249 uint64_t *essential_memory __rte_unused)
254 #ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
256 int essential_prev = 0;
258 struct bitmask *oldmask = NULL;
259 bool have_numa = true;
260 unsigned long maxnode = 0;
261 const struct internal_config *internal_conf =
262 eal_get_internal_configuration();
264 /* Check if kernel supports NUMA. */
265 if (numa_available() != 0) {
266 RTE_LOG(DEBUG, EAL, "NUMA is not supported.\n");
271 RTE_LOG(DEBUG, EAL, "Trying to obtain current memory policy.\n");
272 oldmask = numa_allocate_nodemask();
273 if (get_mempolicy(&oldpolicy, oldmask->maskp,
274 oldmask->size + 1, 0, 0) < 0) {
276 "Failed to get current mempolicy: %s. "
277 "Assuming MPOL_DEFAULT.\n", strerror(errno));
278 oldpolicy = MPOL_DEFAULT;
280 for (i = 0; i < RTE_MAX_NUMA_NODES; i++)
281 if (internal_conf->socket_mem[i])
286 for (i = 0; i < hpi->num_pages[0]; i++) {
287 struct hugepage_file *hf = &hugepg_tbl[i];
288 uint64_t hugepage_sz = hpi->hugepage_sz;
290 #ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
294 for (j = 0; j < maxnode; j++)
295 if (essential_memory[j])
299 node_id = (node_id + 1) % maxnode;
300 while (!internal_conf->socket_mem[node_id]) {
307 essential_prev = essential_memory[j];
309 if (essential_memory[j] < hugepage_sz)
310 essential_memory[j] = 0;
312 essential_memory[j] -= hugepage_sz;
316 "Setting policy MPOL_PREFERRED for socket %d\n",
318 numa_set_preferred(node_id);
323 hf->size = hugepage_sz;
324 eal_get_hugefile_path(hf->filepath, sizeof(hf->filepath),
325 hpi->hugedir, hf->file_id);
326 hf->filepath[sizeof(hf->filepath) - 1] = '\0';
328 /* try to create hugepage file */
329 fd = open(hf->filepath, O_CREAT | O_RDWR, 0600);
331 RTE_LOG(DEBUG, EAL, "%s(): open failed: %s\n", __func__,
336 /* map the segment, and populate page tables,
337 * the kernel fills this segment with zeros. we don't care where
338 * this gets mapped - we already have contiguous memory areas
339 * ready for us to map into.
341 virtaddr = mmap(NULL, hugepage_sz, PROT_READ | PROT_WRITE,
342 MAP_SHARED | MAP_POPULATE, fd, 0);
343 if (virtaddr == MAP_FAILED) {
344 RTE_LOG(DEBUG, EAL, "%s(): mmap failed: %s\n", __func__,
350 hf->orig_va = virtaddr;
352 /* In linux, hugetlb limitations, like cgroup, are
353 * enforced at fault time instead of mmap(), even
354 * with the option of MAP_POPULATE. Kernel will send
355 * a SIGBUS signal. To avoid to be killed, save stack
356 * environment here, if SIGBUS happens, we can jump
359 if (huge_wrap_sigsetjmp()) {
360 RTE_LOG(DEBUG, EAL, "SIGBUS: Cannot mmap more "
361 "hugepages of size %u MB\n",
362 (unsigned int)(hugepage_sz / 0x100000));
363 munmap(virtaddr, hugepage_sz);
365 unlink(hugepg_tbl[i].filepath);
366 #ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
368 essential_memory[node_id] =
373 *(int *)virtaddr = 0;
375 /* set shared lock on the file. */
376 if (flock(fd, LOCK_SH) < 0) {
377 RTE_LOG(DEBUG, EAL, "%s(): Locking file failed:%s \n",
378 __func__, strerror(errno));
387 #ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
390 "Restoring previous memory policy: %d\n", oldpolicy);
391 if (oldpolicy == MPOL_DEFAULT) {
392 numa_set_localalloc();
393 } else if (set_mempolicy(oldpolicy, oldmask->maskp,
394 oldmask->size + 1) < 0) {
395 RTE_LOG(ERR, EAL, "Failed to restore mempolicy: %s\n",
397 numa_set_localalloc();
401 numa_free_cpumask(oldmask);
407 * Parse /proc/self/numa_maps to get the NUMA socket ID for each huge
411 find_numasocket(struct hugepage_file *hugepg_tbl, struct hugepage_info *hpi)
415 unsigned i, hp_count = 0;
418 char hugedir_str[PATH_MAX];
421 f = fopen("/proc/self/numa_maps", "r");
423 RTE_LOG(NOTICE, EAL, "NUMA support not available"
424 " consider that all memory is in socket_id 0\n");
428 snprintf(hugedir_str, sizeof(hugedir_str),
429 "%s/%s", hpi->hugedir, eal_get_hugefile_prefix());
432 while (fgets(buf, sizeof(buf), f) != NULL) {
434 /* ignore non huge page */
435 if (strstr(buf, " huge ") == NULL &&
436 strstr(buf, hugedir_str) == NULL)
440 virt_addr = strtoull(buf, &end, 16);
441 if (virt_addr == 0 || end == buf) {
442 RTE_LOG(ERR, EAL, "%s(): error in numa_maps parsing\n", __func__);
446 /* get node id (socket id) */
447 nodestr = strstr(buf, " N");
448 if (nodestr == NULL) {
449 RTE_LOG(ERR, EAL, "%s(): error in numa_maps parsing\n", __func__);
453 end = strstr(nodestr, "=");
455 RTE_LOG(ERR, EAL, "%s(): error in numa_maps parsing\n", __func__);
461 socket_id = strtoul(nodestr, &end, 0);
462 if ((nodestr[0] == '\0') || (end == NULL) || (*end != '\0')) {
463 RTE_LOG(ERR, EAL, "%s(): error in numa_maps parsing\n", __func__);
467 /* if we find this page in our mappings, set socket_id */
468 for (i = 0; i < hpi->num_pages[0]; i++) {
469 void *va = (void *)(unsigned long)virt_addr;
470 if (hugepg_tbl[i].orig_va == va) {
471 hugepg_tbl[i].socket_id = socket_id;
473 #ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
475 "Hugepage %s is on socket %d\n",
476 hugepg_tbl[i].filepath, socket_id);
482 if (hp_count < hpi->num_pages[0])
494 cmp_physaddr(const void *a, const void *b)
496 #ifndef RTE_ARCH_PPC_64
497 const struct hugepage_file *p1 = a;
498 const struct hugepage_file *p2 = b;
500 /* PowerPC needs memory sorted in reverse order from x86 */
501 const struct hugepage_file *p1 = b;
502 const struct hugepage_file *p2 = a;
504 if (p1->physaddr < p2->physaddr)
506 else if (p1->physaddr > p2->physaddr)
513 * Uses mmap to create a shared memory area for storage of data
514 * Used in this file to store the hugepage file map on disk
517 create_shared_memory(const char *filename, const size_t mem_size)
521 const struct internal_config *internal_conf =
522 eal_get_internal_configuration();
524 /* if no shared files mode is used, create anonymous memory instead */
525 if (internal_conf->no_shconf) {
526 retval = mmap(NULL, mem_size, PROT_READ | PROT_WRITE,
527 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
528 if (retval == MAP_FAILED)
533 fd = open(filename, O_CREAT | O_RDWR, 0600);
536 if (ftruncate(fd, mem_size) < 0) {
540 retval = mmap(NULL, mem_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
542 if (retval == MAP_FAILED)
548 * this copies *active* hugepages from one hugepage table to another.
549 * destination is typically the shared memory.
552 copy_hugepages_to_shared_mem(struct hugepage_file * dst, int dest_size,
553 const struct hugepage_file * src, int src_size)
555 int src_pos, dst_pos = 0;
557 for (src_pos = 0; src_pos < src_size; src_pos++) {
558 if (src[src_pos].orig_va != NULL) {
559 /* error on overflow attempt */
560 if (dst_pos == dest_size)
562 memcpy(&dst[dst_pos], &src[src_pos], sizeof(struct hugepage_file));
570 unlink_hugepage_files(struct hugepage_file *hugepg_tbl,
571 unsigned num_hp_info)
573 unsigned socket, size;
574 int page, nrpages = 0;
575 const struct internal_config *internal_conf =
576 eal_get_internal_configuration();
578 /* get total number of hugepages */
579 for (size = 0; size < num_hp_info; size++)
580 for (socket = 0; socket < RTE_MAX_NUMA_NODES; socket++)
582 internal_conf->hugepage_info[size].num_pages[socket];
584 for (page = 0; page < nrpages; page++) {
585 struct hugepage_file *hp = &hugepg_tbl[page];
587 if (hp->orig_va != NULL && unlink(hp->filepath)) {
588 RTE_LOG(WARNING, EAL, "%s(): Removing %s failed: %s\n",
589 __func__, hp->filepath, strerror(errno));
596 * unmaps hugepages that are not going to be used. since we originally allocate
597 * ALL hugepages (not just those we need), additional unmapping needs to be done.
600 unmap_unneeded_hugepages(struct hugepage_file *hugepg_tbl,
601 struct hugepage_info *hpi,
602 unsigned num_hp_info)
604 unsigned socket, size;
605 int page, nrpages = 0;
606 const struct internal_config *internal_conf =
607 eal_get_internal_configuration();
609 /* get total number of hugepages */
610 for (size = 0; size < num_hp_info; size++)
611 for (socket = 0; socket < RTE_MAX_NUMA_NODES; socket++)
612 nrpages += internal_conf->hugepage_info[size].num_pages[socket];
614 for (size = 0; size < num_hp_info; size++) {
615 for (socket = 0; socket < RTE_MAX_NUMA_NODES; socket++) {
616 unsigned pages_found = 0;
618 /* traverse until we have unmapped all the unused pages */
619 for (page = 0; page < nrpages; page++) {
620 struct hugepage_file *hp = &hugepg_tbl[page];
622 /* find a page that matches the criteria */
623 if ((hp->size == hpi[size].hugepage_sz) &&
624 (hp->socket_id == (int) socket)) {
626 /* if we skipped enough pages, unmap the rest */
627 if (pages_found == hpi[size].num_pages[socket]) {
630 unmap_len = hp->size;
632 /* get start addr and len of the remaining segment */
637 if (unlink(hp->filepath) == -1) {
638 RTE_LOG(ERR, EAL, "%s(): Removing %s failed: %s\n",
639 __func__, hp->filepath, strerror(errno));
643 /* lock the page and skip */
649 } /* foreach socket */
650 } /* foreach pagesize */
656 remap_segment(struct hugepage_file *hugepages, int seg_start, int seg_end)
658 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
659 struct rte_memseg_list *msl;
660 struct rte_fbarray *arr;
661 int cur_page, seg_len;
662 unsigned int msl_idx;
668 const struct internal_config *internal_conf =
669 eal_get_internal_configuration();
671 page_sz = hugepages[seg_start].size;
672 socket_id = hugepages[seg_start].socket_id;
673 seg_len = seg_end - seg_start;
675 RTE_LOG(DEBUG, EAL, "Attempting to map %" PRIu64 "M on socket %i\n",
676 (seg_len * page_sz) >> 20ULL, socket_id);
678 /* find free space in memseg lists */
679 for (msl_idx = 0; msl_idx < RTE_MAX_MEMSEG_LISTS; msl_idx++) {
681 msl = &mcfg->memsegs[msl_idx];
682 arr = &msl->memseg_arr;
684 if (msl->page_sz != page_sz)
686 if (msl->socket_id != socket_id)
689 /* leave space for a hole if array is not empty */
690 empty = arr->count == 0;
691 ms_idx = rte_fbarray_find_next_n_free(arr, 0,
692 seg_len + (empty ? 0 : 1));
694 /* memseg list is full? */
698 /* leave some space between memsegs, they are not IOVA
699 * contiguous, so they shouldn't be VA contiguous either.
705 if (msl_idx == RTE_MAX_MEMSEG_LISTS) {
706 RTE_LOG(ERR, EAL, "Could not find space for memseg. Please increase %s and/or %s in configuration.\n",
707 RTE_STR(RTE_MAX_MEMSEG_PER_TYPE),
708 RTE_STR(RTE_MAX_MEM_MB_PER_TYPE));
712 #ifdef RTE_ARCH_PPC_64
713 /* for PPC64 we go through the list backwards */
714 for (cur_page = seg_end - 1; cur_page >= seg_start;
715 cur_page--, ms_idx++) {
717 for (cur_page = seg_start; cur_page < seg_end; cur_page++, ms_idx++) {
719 struct hugepage_file *hfile = &hugepages[cur_page];
720 struct rte_memseg *ms = rte_fbarray_get(arr, ms_idx);
724 fd = open(hfile->filepath, O_RDWR);
726 RTE_LOG(ERR, EAL, "Could not open '%s': %s\n",
727 hfile->filepath, strerror(errno));
730 /* set shared lock on the file. */
731 if (flock(fd, LOCK_SH) < 0) {
732 RTE_LOG(DEBUG, EAL, "Could not lock '%s': %s\n",
733 hfile->filepath, strerror(errno));
737 memseg_len = (size_t)page_sz;
738 addr = RTE_PTR_ADD(msl->base_va, ms_idx * memseg_len);
740 /* we know this address is already mmapped by memseg list, so
741 * using MAP_FIXED here is safe
743 addr = mmap(addr, page_sz, PROT_READ | PROT_WRITE,
744 MAP_SHARED | MAP_POPULATE | MAP_FIXED, fd, 0);
745 if (addr == MAP_FAILED) {
746 RTE_LOG(ERR, EAL, "Couldn't remap '%s': %s\n",
747 hfile->filepath, strerror(errno));
752 /* we have a new address, so unmap previous one */
754 /* in 32-bit legacy mode, we have already unmapped the page */
755 if (!internal_conf->legacy_mem)
756 munmap(hfile->orig_va, page_sz);
758 munmap(hfile->orig_va, page_sz);
761 hfile->orig_va = NULL;
762 hfile->final_va = addr;
764 /* rewrite physical addresses in IOVA as VA mode */
765 if (rte_eal_iova_mode() == RTE_IOVA_VA)
766 hfile->physaddr = (uintptr_t)addr;
768 /* set up memseg data */
770 ms->hugepage_sz = page_sz;
771 ms->len = memseg_len;
772 ms->iova = hfile->physaddr;
773 ms->socket_id = hfile->socket_id;
774 ms->nchannel = rte_memory_get_nchannel();
775 ms->nrank = rte_memory_get_nrank();
777 rte_fbarray_set_used(arr, ms_idx);
779 /* store segment fd internally */
780 if (eal_memalloc_set_seg_fd(msl_idx, ms_idx, fd) < 0)
781 RTE_LOG(ERR, EAL, "Could not store segment fd: %s\n",
782 rte_strerror(rte_errno));
784 RTE_LOG(DEBUG, EAL, "Allocated %" PRIu64 "M on socket %i\n",
785 (seg_len * page_sz) >> 20, socket_id);
790 get_mem_amount(uint64_t page_sz, uint64_t max_mem)
792 uint64_t area_sz, max_pages;
794 /* limit to RTE_MAX_MEMSEG_PER_LIST pages or RTE_MAX_MEM_MB_PER_LIST */
795 max_pages = RTE_MAX_MEMSEG_PER_LIST;
796 max_mem = RTE_MIN((uint64_t)RTE_MAX_MEM_MB_PER_LIST << 20, max_mem);
798 area_sz = RTE_MIN(page_sz * max_pages, max_mem);
800 /* make sure the list isn't smaller than the page size */
801 area_sz = RTE_MAX(area_sz, page_sz);
803 return RTE_ALIGN(area_sz, page_sz);
807 memseg_list_free(struct rte_memseg_list *msl)
809 if (rte_fbarray_destroy(&msl->memseg_arr)) {
810 RTE_LOG(ERR, EAL, "Cannot destroy memseg list\n");
813 memset(msl, 0, sizeof(*msl));
818 * Our VA space is not preallocated yet, so preallocate it here. We need to know
819 * how many segments there are in order to map all pages into one address space,
820 * and leave appropriate holes between segments so that rte_malloc does not
821 * concatenate them into one big segment.
823 * we also need to unmap original pages to free up address space.
825 static int __rte_unused
826 prealloc_segments(struct hugepage_file *hugepages, int n_pages)
828 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
829 int cur_page, seg_start_page, end_seg, new_memseg;
830 unsigned int hpi_idx, socket, i;
831 int n_contig_segs, n_segs;
833 const struct internal_config *internal_conf =
834 eal_get_internal_configuration();
836 /* before we preallocate segments, we need to free up our VA space.
837 * we're not removing files, and we already have information about
838 * PA-contiguousness, so it is safe to unmap everything.
840 for (cur_page = 0; cur_page < n_pages; cur_page++) {
841 struct hugepage_file *hpi = &hugepages[cur_page];
842 munmap(hpi->orig_va, hpi->size);
846 /* we cannot know how many page sizes and sockets we have discovered, so
847 * loop over all of them
849 for (hpi_idx = 0; hpi_idx < internal_conf->num_hugepage_sizes;
852 internal_conf->hugepage_info[hpi_idx].hugepage_sz;
854 for (i = 0; i < rte_socket_count(); i++) {
855 struct rte_memseg_list *msl;
857 socket = rte_socket_id_by_idx(i);
862 for (cur_page = 0; cur_page < n_pages; cur_page++) {
863 struct hugepage_file *prev, *cur;
864 int prev_seg_start_page = -1;
866 cur = &hugepages[cur_page];
867 prev = cur_page == 0 ? NULL :
868 &hugepages[cur_page - 1];
875 else if (cur->socket_id != (int) socket)
877 else if (cur->size != page_sz)
879 else if (cur_page == 0)
881 #ifdef RTE_ARCH_PPC_64
882 /* On PPC64 architecture, the mmap always start
883 * from higher address to lower address. Here,
884 * physical addresses are in descending order.
886 else if ((prev->physaddr - cur->physaddr) !=
890 else if ((cur->physaddr - prev->physaddr) !=
895 /* if we're already inside a segment,
896 * new segment means end of current one
898 if (seg_start_page != -1) {
900 prev_seg_start_page =
903 seg_start_page = cur_page;
907 if (prev_seg_start_page != -1) {
908 /* we've found a new segment */
912 } else if (seg_start_page != -1) {
913 /* we didn't find new segment,
914 * but did end current one
922 /* we're skipping this page */
926 /* segment continues */
928 /* check if we missed last segment */
929 if (seg_start_page != -1) {
931 n_segs += cur_page - seg_start_page;
934 /* if no segments were found, do not preallocate */
938 /* we now have total number of pages that we will
939 * allocate for this segment list. add separator pages
940 * to the total count, and preallocate VA space.
942 n_segs += n_contig_segs - 1;
944 /* now, preallocate VA space for these segments */
946 /* first, find suitable memseg list for this */
947 for (msl_idx = 0; msl_idx < RTE_MAX_MEMSEG_LISTS;
949 msl = &mcfg->memsegs[msl_idx];
951 if (msl->base_va != NULL)
955 if (msl_idx == RTE_MAX_MEMSEG_LISTS) {
956 RTE_LOG(ERR, EAL, "Not enough space in memseg lists, please increase %s\n",
957 RTE_STR(RTE_MAX_MEMSEG_LISTS));
961 /* now, allocate fbarray itself */
962 if (eal_memseg_list_init(msl, page_sz, n_segs,
963 socket, msl_idx, true) < 0)
966 /* finally, allocate VA space */
967 if (eal_memseg_list_alloc(msl, 0) < 0) {
968 RTE_LOG(ERR, EAL, "Cannot preallocate 0x%"PRIx64"kB hugepages\n",
978 * We cannot reallocate memseg lists on the fly because PPC64 stores pages
979 * backwards, therefore we have to process the entire memseg first before
980 * remapping it into memseg list VA space.
983 remap_needed_hugepages(struct hugepage_file *hugepages, int n_pages)
985 int cur_page, seg_start_page, new_memseg, ret;
988 for (cur_page = 0; cur_page < n_pages; cur_page++) {
989 struct hugepage_file *prev, *cur;
993 cur = &hugepages[cur_page];
994 prev = cur_page == 0 ? NULL : &hugepages[cur_page - 1];
996 /* if size is zero, no more pages left */
1002 else if (cur->socket_id != prev->socket_id)
1004 else if (cur->size != prev->size)
1006 #ifdef RTE_ARCH_PPC_64
1007 /* On PPC64 architecture, the mmap always start from higher
1008 * address to lower address. Here, physical addresses are in
1011 else if ((prev->physaddr - cur->physaddr) != cur->size)
1014 else if ((cur->physaddr - prev->physaddr) != cur->size)
1019 /* if this isn't the first time, remap segment */
1020 if (cur_page != 0) {
1021 ret = remap_segment(hugepages, seg_start_page,
1026 /* remember where we started */
1027 seg_start_page = cur_page;
1029 /* continuation of previous memseg */
1031 /* we were stopped, but we didn't remap the last segment, do it now */
1032 if (cur_page != 0) {
1033 ret = remap_segment(hugepages, seg_start_page,
1041 static inline size_t
1042 eal_get_hugepage_mem_size(void)
1046 struct internal_config *internal_conf =
1047 eal_get_internal_configuration();
1049 for (i = 0; i < internal_conf->num_hugepage_sizes; i++) {
1050 struct hugepage_info *hpi = &internal_conf->hugepage_info[i];
1051 if (strnlen(hpi->hugedir, sizeof(hpi->hugedir)) != 0) {
1052 for (j = 0; j < RTE_MAX_NUMA_NODES; j++) {
1053 size += hpi->hugepage_sz * hpi->num_pages[j];
1058 return (size < SIZE_MAX) ? (size_t)(size) : SIZE_MAX;
1061 static struct sigaction huge_action_old;
1062 static int huge_need_recover;
1065 huge_register_sigbus(void)
1068 struct sigaction action;
1071 sigaddset(&mask, SIGBUS);
1072 action.sa_flags = 0;
1073 action.sa_mask = mask;
1074 action.sa_handler = huge_sigbus_handler;
1076 huge_need_recover = !sigaction(SIGBUS, &action, &huge_action_old);
1080 huge_recover_sigbus(void)
1082 if (huge_need_recover) {
1083 sigaction(SIGBUS, &huge_action_old, NULL);
1084 huge_need_recover = 0;
1089 * Prepare physical memory mapping: fill configuration structure with
1090 * these infos, return 0 on success.
1091 * 1. map N huge pages in separate files in hugetlbfs
1092 * 2. find associated physical addr
1093 * 3. find associated NUMA socket ID
1094 * 4. sort all huge pages by physical address
1095 * 5. remap these N huge pages in the correct order
1096 * 6. unmap the first mapping
1097 * 7. fill memsegs in configuration with contiguous zones
1100 eal_legacy_hugepage_init(void)
1102 struct rte_mem_config *mcfg;
1103 struct hugepage_file *hugepage = NULL, *tmp_hp = NULL;
1104 struct hugepage_info used_hp[MAX_HUGEPAGE_SIZES];
1105 struct internal_config *internal_conf =
1106 eal_get_internal_configuration();
1108 uint64_t memory[RTE_MAX_NUMA_NODES];
1112 int nr_hugefiles, nr_hugepages = 0;
1115 memset(used_hp, 0, sizeof(used_hp));
1117 /* get pointer to global configuration */
1118 mcfg = rte_eal_get_configuration()->mem_config;
1120 /* hugetlbfs can be disabled */
1121 if (internal_conf->no_hugetlbfs) {
1122 void *prealloc_addr;
1124 struct rte_memseg_list *msl;
1125 int n_segs, fd, flags;
1126 #ifdef MEMFD_SUPPORTED
1131 /* nohuge mode is legacy mode */
1132 internal_conf->legacy_mem = 1;
1134 /* nohuge mode is single-file segments mode */
1135 internal_conf->single_file_segments = 1;
1137 /* create a memseg list */
1138 msl = &mcfg->memsegs[0];
1140 mem_sz = internal_conf->memory;
1141 page_sz = RTE_PGSIZE_4K;
1142 n_segs = mem_sz / page_sz;
1144 if (eal_memseg_list_init_named(
1145 msl, "nohugemem", page_sz, n_segs, 0, true)) {
1149 /* set up parameters for anonymous mmap */
1151 flags = MAP_PRIVATE | MAP_ANONYMOUS;
1153 #ifdef MEMFD_SUPPORTED
1154 /* create a memfd and store it in the segment fd table */
1155 memfd = memfd_create("nohuge", 0);
1157 RTE_LOG(DEBUG, EAL, "Cannot create memfd: %s\n",
1159 RTE_LOG(DEBUG, EAL, "Falling back to anonymous map\n");
1161 /* we got an fd - now resize it */
1162 if (ftruncate(memfd, internal_conf->memory) < 0) {
1163 RTE_LOG(ERR, EAL, "Cannot resize memfd: %s\n",
1165 RTE_LOG(ERR, EAL, "Falling back to anonymous map\n");
1168 /* creating memfd-backed file was successful.
1169 * we want changes to memfd to be visible to
1170 * other processes (such as vhost backend), so
1171 * map it as shared memory.
1173 RTE_LOG(DEBUG, EAL, "Using memfd for anonymous memory\n");
1179 /* preallocate address space for the memory, so that it can be
1180 * fit into the DMA mask.
1182 if (eal_memseg_list_alloc(msl, 0)) {
1183 RTE_LOG(ERR, EAL, "Cannot preallocate VA space for hugepage memory\n");
1187 prealloc_addr = msl->base_va;
1188 addr = mmap(prealloc_addr, mem_sz, PROT_READ | PROT_WRITE,
1189 flags | MAP_FIXED, fd, 0);
1190 if (addr == MAP_FAILED || addr != prealloc_addr) {
1191 RTE_LOG(ERR, EAL, "%s: mmap() failed: %s\n", __func__,
1193 munmap(prealloc_addr, mem_sz);
1197 /* we're in single-file segments mode, so only the segment list
1198 * fd needs to be set up.
1201 if (eal_memalloc_set_seg_list_fd(0, fd) < 0) {
1202 RTE_LOG(ERR, EAL, "Cannot set up segment list fd\n");
1203 /* not a serious error, proceed */
1207 eal_memseg_list_populate(msl, addr, n_segs);
1209 if (mcfg->dma_maskbits &&
1210 rte_mem_check_dma_mask_thread_unsafe(mcfg->dma_maskbits)) {
1212 "%s(): couldn't allocate memory due to IOVA exceeding limits of current DMA mask.\n",
1214 if (rte_eal_iova_mode() == RTE_IOVA_VA &&
1215 rte_eal_using_phys_addrs())
1217 "%s(): Please try initializing EAL with --iova-mode=pa parameter.\n",
1224 /* calculate total number of hugepages available. at this point we haven't
1225 * yet started sorting them so they all are on socket 0 */
1226 for (i = 0; i < (int) internal_conf->num_hugepage_sizes; i++) {
1227 /* meanwhile, also initialize used_hp hugepage sizes in used_hp */
1228 used_hp[i].hugepage_sz = internal_conf->hugepage_info[i].hugepage_sz;
1230 nr_hugepages += internal_conf->hugepage_info[i].num_pages[0];
1234 * allocate a memory area for hugepage table.
1235 * this isn't shared memory yet. due to the fact that we need some
1236 * processing done on these pages, shared memory will be created
1239 tmp_hp = malloc(nr_hugepages * sizeof(struct hugepage_file));
1243 memset(tmp_hp, 0, nr_hugepages * sizeof(struct hugepage_file));
1245 hp_offset = 0; /* where we start the current page size entries */
1247 huge_register_sigbus();
1249 /* make a copy of socket_mem, needed for balanced allocation. */
1250 for (i = 0; i < RTE_MAX_NUMA_NODES; i++)
1251 memory[i] = internal_conf->socket_mem[i];
1253 /* map all hugepages and sort them */
1254 for (i = 0; i < (int)internal_conf->num_hugepage_sizes; i++) {
1255 unsigned pages_old, pages_new;
1256 struct hugepage_info *hpi;
1259 * we don't yet mark hugepages as used at this stage, so
1260 * we just map all hugepages available to the system
1261 * all hugepages are still located on socket 0
1263 hpi = &internal_conf->hugepage_info[i];
1265 if (hpi->num_pages[0] == 0)
1268 /* map all hugepages available */
1269 pages_old = hpi->num_pages[0];
1270 pages_new = map_all_hugepages(&tmp_hp[hp_offset], hpi, memory);
1271 if (pages_new < pages_old) {
1273 "%d not %d hugepages of size %u MB allocated\n",
1274 pages_new, pages_old,
1275 (unsigned)(hpi->hugepage_sz / 0x100000));
1277 int pages = pages_old - pages_new;
1279 nr_hugepages -= pages;
1280 hpi->num_pages[0] = pages_new;
1285 if (rte_eal_using_phys_addrs() &&
1286 rte_eal_iova_mode() != RTE_IOVA_VA) {
1287 /* find physical addresses for each hugepage */
1288 if (find_physaddrs(&tmp_hp[hp_offset], hpi) < 0) {
1289 RTE_LOG(DEBUG, EAL, "Failed to find phys addr "
1290 "for %u MB pages\n",
1291 (unsigned int)(hpi->hugepage_sz / 0x100000));
1295 /* set physical addresses for each hugepage */
1296 if (set_physaddrs(&tmp_hp[hp_offset], hpi) < 0) {
1297 RTE_LOG(DEBUG, EAL, "Failed to set phys addr "
1298 "for %u MB pages\n",
1299 (unsigned int)(hpi->hugepage_sz / 0x100000));
1304 if (find_numasocket(&tmp_hp[hp_offset], hpi) < 0){
1305 RTE_LOG(DEBUG, EAL, "Failed to find NUMA socket for %u MB pages\n",
1306 (unsigned)(hpi->hugepage_sz / 0x100000));
1310 qsort(&tmp_hp[hp_offset], hpi->num_pages[0],
1311 sizeof(struct hugepage_file), cmp_physaddr);
1313 /* we have processed a num of hugepages of this size, so inc offset */
1314 hp_offset += hpi->num_pages[0];
1317 huge_recover_sigbus();
1319 if (internal_conf->memory == 0 && internal_conf->force_sockets == 0)
1320 internal_conf->memory = eal_get_hugepage_mem_size();
1322 nr_hugefiles = nr_hugepages;
1325 /* clean out the numbers of pages */
1326 for (i = 0; i < (int) internal_conf->num_hugepage_sizes; i++)
1327 for (j = 0; j < RTE_MAX_NUMA_NODES; j++)
1328 internal_conf->hugepage_info[i].num_pages[j] = 0;
1330 /* get hugepages for each socket */
1331 for (i = 0; i < nr_hugefiles; i++) {
1332 int socket = tmp_hp[i].socket_id;
1334 /* find a hugepage info with right size and increment num_pages */
1335 const int nb_hpsizes = RTE_MIN(MAX_HUGEPAGE_SIZES,
1336 (int)internal_conf->num_hugepage_sizes);
1337 for (j = 0; j < nb_hpsizes; j++) {
1338 if (tmp_hp[i].size ==
1339 internal_conf->hugepage_info[j].hugepage_sz) {
1340 internal_conf->hugepage_info[j].num_pages[socket]++;
1345 /* make a copy of socket_mem, needed for number of pages calculation */
1346 for (i = 0; i < RTE_MAX_NUMA_NODES; i++)
1347 memory[i] = internal_conf->socket_mem[i];
1349 /* calculate final number of pages */
1350 nr_hugepages = eal_dynmem_calc_num_pages_per_socket(memory,
1351 internal_conf->hugepage_info, used_hp,
1352 internal_conf->num_hugepage_sizes);
1354 /* error if not enough memory available */
1355 if (nr_hugepages < 0)
1359 for (i = 0; i < (int) internal_conf->num_hugepage_sizes; i++) {
1360 for (j = 0; j < RTE_MAX_NUMA_NODES; j++) {
1361 if (used_hp[i].num_pages[j] > 0) {
1363 "Requesting %u pages of size %uMB"
1364 " from socket %i\n",
1365 used_hp[i].num_pages[j],
1367 (used_hp[i].hugepage_sz / 0x100000),
1373 /* create shared memory */
1374 hugepage = create_shared_memory(eal_hugepage_data_path(),
1375 nr_hugefiles * sizeof(struct hugepage_file));
1377 if (hugepage == NULL) {
1378 RTE_LOG(ERR, EAL, "Failed to create shared memory!\n");
1381 memset(hugepage, 0, nr_hugefiles * sizeof(struct hugepage_file));
1384 * unmap pages that we won't need (looks at used_hp).
1385 * also, sets final_va to NULL on pages that were unmapped.
1387 if (unmap_unneeded_hugepages(tmp_hp, used_hp,
1388 internal_conf->num_hugepage_sizes) < 0) {
1389 RTE_LOG(ERR, EAL, "Unmapping and locking hugepages failed!\n");
1394 * copy stuff from malloc'd hugepage* to the actual shared memory.
1395 * this procedure only copies those hugepages that have orig_va
1396 * not NULL. has overflow protection.
1398 if (copy_hugepages_to_shared_mem(hugepage, nr_hugefiles,
1399 tmp_hp, nr_hugefiles) < 0) {
1400 RTE_LOG(ERR, EAL, "Copying tables to shared memory failed!\n");
1405 /* for legacy 32-bit mode, we did not preallocate VA space, so do it */
1406 if (internal_conf->legacy_mem &&
1407 prealloc_segments(hugepage, nr_hugefiles)) {
1408 RTE_LOG(ERR, EAL, "Could not preallocate VA space for hugepages\n");
1413 /* remap all pages we do need into memseg list VA space, so that those
1414 * pages become first-class citizens in DPDK memory subsystem
1416 if (remap_needed_hugepages(hugepage, nr_hugefiles)) {
1417 RTE_LOG(ERR, EAL, "Couldn't remap hugepage files into memseg lists\n");
1421 /* free the hugepage backing files */
1422 if (internal_conf->hugepage_file.unlink_before_mapping &&
1423 unlink_hugepage_files(tmp_hp, internal_conf->num_hugepage_sizes) < 0) {
1424 RTE_LOG(ERR, EAL, "Unlinking hugepage files failed!\n");
1428 /* free the temporary hugepage table */
1432 munmap(hugepage, nr_hugefiles * sizeof(struct hugepage_file));
1435 /* we're not going to allocate more pages, so release VA space for
1436 * unused memseg lists
1438 for (i = 0; i < RTE_MAX_MEMSEG_LISTS; i++) {
1439 struct rte_memseg_list *msl = &mcfg->memsegs[i];
1442 /* skip inactive lists */
1443 if (msl->base_va == NULL)
1445 /* skip lists where there is at least one page allocated */
1446 if (msl->memseg_arr.count > 0)
1448 /* this is an unused list, deallocate it */
1450 munmap(msl->base_va, mem_sz);
1451 msl->base_va = NULL;
1454 /* destroy backing fbarray */
1455 rte_fbarray_destroy(&msl->memseg_arr);
1458 if (mcfg->dma_maskbits &&
1459 rte_mem_check_dma_mask_thread_unsafe(mcfg->dma_maskbits)) {
1461 "%s(): couldn't allocate memory due to IOVA exceeding limits of current DMA mask.\n",
1469 huge_recover_sigbus();
1471 if (hugepage != NULL)
1472 munmap(hugepage, nr_hugefiles * sizeof(struct hugepage_file));
1478 * uses fstat to report the size of a file on disk
1484 if (fstat(fd, &st) < 0)
1490 * This creates the memory mappings in the secondary process to match that of
1491 * the server process. It goes through each memory segment in the DPDK runtime
1492 * configuration and finds the hugepages which form that segment, mapping them
1493 * in order to form a contiguous block in the virtual memory space
1496 eal_legacy_hugepage_attach(void)
1498 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
1499 struct hugepage_file *hp = NULL;
1500 unsigned int num_hp = 0;
1502 unsigned int cur_seg;
1504 int fd, fd_hugepage = -1;
1506 if (aslr_enabled() > 0) {
1507 RTE_LOG(WARNING, EAL, "WARNING: Address Space Layout Randomization "
1508 "(ASLR) is enabled in the kernel.\n");
1509 RTE_LOG(WARNING, EAL, " This may cause issues with mapping memory "
1510 "into secondary processes\n");
1513 fd_hugepage = open(eal_hugepage_data_path(), O_RDONLY);
1514 if (fd_hugepage < 0) {
1515 RTE_LOG(ERR, EAL, "Could not open %s\n",
1516 eal_hugepage_data_path());
1520 size = getFileSize(fd_hugepage);
1521 hp = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd_hugepage, 0);
1522 if (hp == MAP_FAILED) {
1523 RTE_LOG(ERR, EAL, "Could not mmap %s\n",
1524 eal_hugepage_data_path());
1528 num_hp = size / sizeof(struct hugepage_file);
1529 RTE_LOG(DEBUG, EAL, "Analysing %u files\n", num_hp);
1531 /* map all segments into memory to make sure we get the addrs. the
1532 * segments themselves are already in memseg list (which is shared and
1533 * has its VA space already preallocated), so we just need to map
1534 * everything into correct addresses.
1536 for (i = 0; i < num_hp; i++) {
1537 struct hugepage_file *hf = &hp[i];
1538 size_t map_sz = hf->size;
1539 void *map_addr = hf->final_va;
1540 int msl_idx, ms_idx;
1541 struct rte_memseg_list *msl;
1542 struct rte_memseg *ms;
1544 /* if size is zero, no more pages left */
1548 fd = open(hf->filepath, O_RDWR);
1550 RTE_LOG(ERR, EAL, "Could not open %s: %s\n",
1551 hf->filepath, strerror(errno));
1555 map_addr = mmap(map_addr, map_sz, PROT_READ | PROT_WRITE,
1556 MAP_SHARED | MAP_FIXED, fd, 0);
1557 if (map_addr == MAP_FAILED) {
1558 RTE_LOG(ERR, EAL, "Could not map %s: %s\n",
1559 hf->filepath, strerror(errno));
1563 /* set shared lock on the file. */
1564 if (flock(fd, LOCK_SH) < 0) {
1565 RTE_LOG(DEBUG, EAL, "%s(): Locking file failed: %s\n",
1566 __func__, strerror(errno));
1570 /* find segment data */
1571 msl = rte_mem_virt2memseg_list(map_addr);
1573 RTE_LOG(DEBUG, EAL, "%s(): Cannot find memseg list\n",
1577 ms = rte_mem_virt2memseg(map_addr, msl);
1579 RTE_LOG(DEBUG, EAL, "%s(): Cannot find memseg\n",
1584 msl_idx = msl - mcfg->memsegs;
1585 ms_idx = rte_fbarray_find_idx(&msl->memseg_arr, ms);
1587 RTE_LOG(DEBUG, EAL, "%s(): Cannot find memseg idx\n",
1592 /* store segment fd internally */
1593 if (eal_memalloc_set_seg_fd(msl_idx, ms_idx, fd) < 0)
1594 RTE_LOG(ERR, EAL, "Could not store segment fd: %s\n",
1595 rte_strerror(rte_errno));
1597 /* unmap the hugepage config file, since we are done using it */
1603 munmap(hp[i].final_va, hp[i].size);
1607 /* unwind mmap's done so far */
1608 for (cur_seg = 0; cur_seg < i; cur_seg++)
1609 munmap(hp[cur_seg].final_va, hp[cur_seg].size);
1611 if (hp != NULL && hp != MAP_FAILED)
1613 if (fd_hugepage >= 0)
1619 eal_hugepage_attach(void)
1621 if (eal_memalloc_sync_with_primary()) {
1622 RTE_LOG(ERR, EAL, "Could not map memory from primary process\n");
1623 if (aslr_enabled() > 0)
1624 RTE_LOG(ERR, EAL, "It is recommended to disable ASLR in the kernel and retry running both primary and secondary processes\n");
1631 rte_eal_hugepage_init(void)
1633 const struct internal_config *internal_conf =
1634 eal_get_internal_configuration();
1636 return internal_conf->legacy_mem ?
1637 eal_legacy_hugepage_init() :
1638 eal_dynmem_hugepage_init();
1642 rte_eal_hugepage_attach(void)
1644 const struct internal_config *internal_conf =
1645 eal_get_internal_configuration();
1647 return internal_conf->legacy_mem ?
1648 eal_legacy_hugepage_attach() :
1649 eal_hugepage_attach();
1653 rte_eal_using_phys_addrs(void)
1655 if (phys_addrs_available == -1) {
1658 if (rte_eal_has_hugepages() != 0 &&
1659 rte_mem_virt2phy(&tmp) != RTE_BAD_PHYS_ADDR)
1660 phys_addrs_available = 1;
1662 phys_addrs_available = 0;
1664 return phys_addrs_available;
1667 static int __rte_unused
1668 memseg_primary_init_32(void)
1670 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
1671 int active_sockets, hpi_idx, msl_idx = 0;
1672 unsigned int socket_id, i;
1673 struct rte_memseg_list *msl;
1674 uint64_t extra_mem_per_socket, total_extra_mem, total_requested_mem;
1676 struct internal_config *internal_conf =
1677 eal_get_internal_configuration();
1679 /* no-huge does not need this at all */
1680 if (internal_conf->no_hugetlbfs)
1683 /* this is a giant hack, but desperate times call for desperate
1684 * measures. in legacy 32-bit mode, we cannot preallocate VA space,
1685 * because having upwards of 2 gigabytes of VA space already mapped will
1686 * interfere with our ability to map and sort hugepages.
1688 * therefore, in legacy 32-bit mode, we will be initializing memseg
1689 * lists much later - in eal_memory.c, right after we unmap all the
1690 * unneeded pages. this will not affect secondary processes, as those
1691 * should be able to mmap the space without (too many) problems.
1693 if (internal_conf->legacy_mem)
1696 /* 32-bit mode is a very special case. we cannot know in advance where
1697 * the user will want to allocate their memory, so we have to do some
1701 total_requested_mem = 0;
1702 if (internal_conf->force_sockets)
1703 for (i = 0; i < rte_socket_count(); i++) {
1706 socket_id = rte_socket_id_by_idx(i);
1707 mem = internal_conf->socket_mem[socket_id];
1713 total_requested_mem += mem;
1716 total_requested_mem = internal_conf->memory;
1718 max_mem = (uint64_t)RTE_MAX_MEM_MB << 20;
1719 if (total_requested_mem > max_mem) {
1720 RTE_LOG(ERR, EAL, "Invalid parameters: 32-bit process can at most use %uM of memory\n",
1721 (unsigned int)(max_mem >> 20));
1724 total_extra_mem = max_mem - total_requested_mem;
1725 extra_mem_per_socket = active_sockets == 0 ? total_extra_mem :
1726 total_extra_mem / active_sockets;
1728 /* the allocation logic is a little bit convoluted, but here's how it
1729 * works, in a nutshell:
1730 * - if user hasn't specified on which sockets to allocate memory via
1731 * --socket-mem, we allocate all of our memory on main core socket.
1732 * - if user has specified sockets to allocate memory on, there may be
1733 * some "unused" memory left (e.g. if user has specified --socket-mem
1734 * such that not all memory adds up to 2 gigabytes), so add it to all
1735 * sockets that are in use equally.
1737 * page sizes are sorted by size in descending order, so we can safely
1738 * assume that we dispense with bigger page sizes first.
1741 /* create memseg lists */
1742 for (i = 0; i < rte_socket_count(); i++) {
1743 int hp_sizes = (int) internal_conf->num_hugepage_sizes;
1744 uint64_t max_socket_mem, cur_socket_mem;
1745 unsigned int main_lcore_socket;
1746 struct rte_config *cfg = rte_eal_get_configuration();
1749 socket_id = rte_socket_id_by_idx(i);
1751 #ifndef RTE_EAL_NUMA_AWARE_HUGEPAGES
1752 /* we can still sort pages by socket in legacy mode */
1753 if (!internal_conf->legacy_mem && socket_id > 0)
1757 /* if we didn't specifically request memory on this socket */
1758 skip = active_sockets != 0 &&
1759 internal_conf->socket_mem[socket_id] == 0;
1760 /* ...or if we didn't specifically request memory on *any*
1761 * socket, and this is not main lcore
1763 main_lcore_socket = rte_lcore_to_socket_id(cfg->main_lcore);
1764 skip |= active_sockets == 0 && socket_id != main_lcore_socket;
1767 RTE_LOG(DEBUG, EAL, "Will not preallocate memory on socket %u\n",
1772 /* max amount of memory on this socket */
1773 max_socket_mem = (active_sockets != 0 ?
1774 internal_conf->socket_mem[socket_id] :
1775 internal_conf->memory) +
1776 extra_mem_per_socket;
1779 for (hpi_idx = 0; hpi_idx < hp_sizes; hpi_idx++) {
1780 uint64_t max_pagesz_mem, cur_pagesz_mem = 0;
1781 uint64_t hugepage_sz;
1782 struct hugepage_info *hpi;
1783 int type_msl_idx, max_segs, total_segs = 0;
1785 hpi = &internal_conf->hugepage_info[hpi_idx];
1786 hugepage_sz = hpi->hugepage_sz;
1788 /* check if pages are actually available */
1789 if (hpi->num_pages[socket_id] == 0)
1792 max_segs = RTE_MAX_MEMSEG_PER_TYPE;
1793 max_pagesz_mem = max_socket_mem - cur_socket_mem;
1795 /* make it multiple of page size */
1796 max_pagesz_mem = RTE_ALIGN_FLOOR(max_pagesz_mem,
1799 RTE_LOG(DEBUG, EAL, "Attempting to preallocate "
1800 "%" PRIu64 "M on socket %i\n",
1801 max_pagesz_mem >> 20, socket_id);
1804 while (cur_pagesz_mem < max_pagesz_mem &&
1805 total_segs < max_segs) {
1807 unsigned int n_segs;
1809 if (msl_idx >= RTE_MAX_MEMSEG_LISTS) {
1811 "No more space in memseg lists, please increase %s\n",
1812 RTE_STR(RTE_MAX_MEMSEG_LISTS));
1816 msl = &mcfg->memsegs[msl_idx];
1818 cur_mem = get_mem_amount(hugepage_sz,
1820 n_segs = cur_mem / hugepage_sz;
1822 if (eal_memseg_list_init(msl, hugepage_sz,
1823 n_segs, socket_id, type_msl_idx,
1825 /* failing to allocate a memseg list is
1828 RTE_LOG(ERR, EAL, "Cannot allocate memseg list\n");
1832 if (eal_memseg_list_alloc(msl, 0)) {
1833 /* if we couldn't allocate VA space, we
1834 * can try with smaller page sizes.
1836 RTE_LOG(ERR, EAL, "Cannot allocate VA space for memseg list, retrying with different page size\n");
1837 /* deallocate memseg list */
1838 if (memseg_list_free(msl))
1843 total_segs += msl->memseg_arr.len;
1844 cur_pagesz_mem = total_segs * hugepage_sz;
1848 cur_socket_mem += cur_pagesz_mem;
1850 if (cur_socket_mem == 0) {
1851 RTE_LOG(ERR, EAL, "Cannot allocate VA space on socket %u\n",
1860 static int __rte_unused
1861 memseg_primary_init(void)
1863 return eal_dynmem_memseg_lists_init();
1867 memseg_secondary_init(void)
1869 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
1871 struct rte_memseg_list *msl;
1873 for (msl_idx = 0; msl_idx < RTE_MAX_MEMSEG_LISTS; msl_idx++) {
1875 msl = &mcfg->memsegs[msl_idx];
1877 /* skip empty memseg lists */
1878 if (msl->memseg_arr.len == 0)
1881 if (rte_fbarray_attach(&msl->memseg_arr)) {
1882 RTE_LOG(ERR, EAL, "Cannot attach to primary process memseg lists\n");
1886 /* preallocate VA space */
1887 if (eal_memseg_list_alloc(msl, 0)) {
1888 RTE_LOG(ERR, EAL, "Cannot preallocate VA space for hugepage memory\n");
1897 rte_eal_memseg_init(void)
1899 /* increase rlimit to maximum */
1902 #ifndef RTE_EAL_NUMA_AWARE_HUGEPAGES
1903 const struct internal_config *internal_conf =
1904 eal_get_internal_configuration();
1906 if (getrlimit(RLIMIT_NOFILE, &lim) == 0) {
1907 /* set limit to maximum */
1908 lim.rlim_cur = lim.rlim_max;
1910 if (setrlimit(RLIMIT_NOFILE, &lim) < 0) {
1911 RTE_LOG(DEBUG, EAL, "Setting maximum number of open files failed: %s\n",
1914 RTE_LOG(DEBUG, EAL, "Setting maximum number of open files to %"
1916 (uint64_t)lim.rlim_cur);
1919 RTE_LOG(ERR, EAL, "Cannot get current resource limits\n");
1921 #ifndef RTE_EAL_NUMA_AWARE_HUGEPAGES
1922 if (!internal_conf->legacy_mem && rte_socket_count() > 1) {
1923 RTE_LOG(WARNING, EAL, "DPDK is running on a NUMA system, but is compiled without NUMA support.\n");
1924 RTE_LOG(WARNING, EAL, "This will have adverse consequences for performance and usability.\n");
1925 RTE_LOG(WARNING, EAL, "Please use --"OPT_LEGACY_MEM" option, or recompile with NUMA support.\n");
1929 return rte_eal_process_type() == RTE_PROC_PRIMARY ?
1931 memseg_primary_init_32() :
1933 memseg_primary_init() :
1935 memseg_secondary_init();