1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation.
3 * Copyright(c) 2013 6WIND S.A.
6 #define _FILE_OFFSET_BITS 64
17 #include <sys/types.h>
19 #include <sys/queue.h>
21 #include <sys/resource.h>
24 #include <sys/ioctl.h>
28 #ifdef F_ADD_SEALS /* if file sealing is supported, so is memfd */
29 #include <linux/memfd.h>
30 #define MEMFD_SUPPORTED
32 #ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
37 #include <rte_errno.h>
39 #include <rte_memory.h>
40 #include <rte_launch.h>
42 #include <rte_eal_memconfig.h>
43 #include <rte_per_lcore.h>
44 #include <rte_lcore.h>
45 #include <rte_common.h>
46 #include <rte_string_fns.h>
48 #include "eal_private.h"
49 #include "eal_memalloc.h"
50 #include "eal_internal_cfg.h"
51 #include "eal_filesystem.h"
52 #include "eal_hugepages.h"
54 #define PFN_MASK_SIZE 8
58 * Huge page mapping under linux
60 * To reserve a big contiguous amount of memory, we use the hugepage
61 * feature of linux. For that, we need to have hugetlbfs mounted. This
62 * code will create many files in this directory (one per page) and
63 * map them in virtual memory. For each page, we will retrieve its
64 * physical address and remap it in order to have a virtual contiguous
65 * zone as well as a physical contiguous zone.
68 static bool phys_addrs_available = true;
70 #define RANDOMIZE_VA_SPACE_FILE "/proc/sys/kernel/randomize_va_space"
73 test_phys_addrs_available(void)
78 if (!rte_eal_has_hugepages()) {
80 "Started without hugepages support, physical addresses not available\n");
81 phys_addrs_available = false;
85 physaddr = rte_mem_virt2phy(&tmp);
86 if (physaddr == RTE_BAD_PHYS_ADDR) {
87 if (rte_eal_iova_mode() == RTE_IOVA_PA)
89 "Cannot obtain physical addresses: %s. "
90 "Only vfio will function.\n",
92 phys_addrs_available = false;
97 * Get physical address of any mapped virtual address in the current process.
100 rte_mem_virt2phy(const void *virtaddr)
103 uint64_t page, physaddr;
104 unsigned long virt_pfn;
108 /* Cannot parse /proc/self/pagemap, no need to log errors everywhere */
109 if (!phys_addrs_available)
112 /* standard page size */
113 page_size = getpagesize();
115 fd = open("/proc/self/pagemap", O_RDONLY);
117 RTE_LOG(INFO, EAL, "%s(): cannot open /proc/self/pagemap: %s\n",
118 __func__, strerror(errno));
122 virt_pfn = (unsigned long)virtaddr / page_size;
123 offset = sizeof(uint64_t) * virt_pfn;
124 if (lseek(fd, offset, SEEK_SET) == (off_t) -1) {
125 RTE_LOG(INFO, EAL, "%s(): seek error in /proc/self/pagemap: %s\n",
126 __func__, strerror(errno));
131 retval = read(fd, &page, PFN_MASK_SIZE);
134 RTE_LOG(INFO, EAL, "%s(): cannot read /proc/self/pagemap: %s\n",
135 __func__, strerror(errno));
137 } else if (retval != PFN_MASK_SIZE) {
138 RTE_LOG(INFO, EAL, "%s(): read %d bytes from /proc/self/pagemap "
139 "but expected %d:\n",
140 __func__, retval, PFN_MASK_SIZE);
145 * the pfn (page frame number) are bits 0-54 (see
146 * pagemap.txt in linux Documentation)
148 if ((page & 0x7fffffffffffffULL) == 0)
151 physaddr = ((page & 0x7fffffffffffffULL) * page_size)
152 + ((unsigned long)virtaddr % page_size);
158 rte_mem_virt2iova(const void *virtaddr)
160 if (rte_eal_iova_mode() == RTE_IOVA_VA)
161 return (uintptr_t)virtaddr;
162 return rte_mem_virt2phy(virtaddr);
166 * For each hugepage in hugepg_tbl, fill the physaddr value. We find
167 * it by browsing the /proc/self/pagemap special file.
170 find_physaddrs(struct hugepage_file *hugepg_tbl, struct hugepage_info *hpi)
175 for (i = 0; i < hpi->num_pages[0]; i++) {
176 addr = rte_mem_virt2phy(hugepg_tbl[i].orig_va);
177 if (addr == RTE_BAD_PHYS_ADDR)
179 hugepg_tbl[i].physaddr = addr;
185 * For each hugepage in hugepg_tbl, fill the physaddr value sequentially.
188 set_physaddrs(struct hugepage_file *hugepg_tbl, struct hugepage_info *hpi)
191 static phys_addr_t addr;
193 for (i = 0; i < hpi->num_pages[0]; i++) {
194 hugepg_tbl[i].physaddr = addr;
195 addr += hugepg_tbl[i].size;
201 * Check whether address-space layout randomization is enabled in
202 * the kernel. This is important for multi-process as it can prevent
203 * two processes mapping data to the same virtual address
205 * 0 - address space randomization disabled
206 * 1/2 - address space randomization enabled
207 * negative error code on error
213 int retval, fd = open(RANDOMIZE_VA_SPACE_FILE, O_RDONLY);
216 retval = read(fd, &c, 1);
226 default: return -EINVAL;
230 static sigjmp_buf huge_jmpenv;
232 static void huge_sigbus_handler(int signo __rte_unused)
234 siglongjmp(huge_jmpenv, 1);
237 /* Put setjmp into a wrap method to avoid compiling error. Any non-volatile,
238 * non-static local variable in the stack frame calling sigsetjmp might be
239 * clobbered by a call to longjmp.
241 static int huge_wrap_sigsetjmp(void)
243 return sigsetjmp(huge_jmpenv, 1);
246 #ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
247 /* Callback for numa library. */
248 void numa_error(char *where)
250 RTE_LOG(ERR, EAL, "%s failed: %s\n", where, strerror(errno));
255 * Mmap all hugepages of hugepage table: it first open a file in
256 * hugetlbfs, then mmap() hugepage_sz data in it. If orig is set, the
257 * virtual address is stored in hugepg_tbl[i].orig_va, else it is stored
258 * in hugepg_tbl[i].final_va. The second mapping (when orig is 0) tries to
259 * map contiguous physical blocks in contiguous virtual blocks.
262 map_all_hugepages(struct hugepage_file *hugepg_tbl, struct hugepage_info *hpi,
263 uint64_t *essential_memory __rte_unused)
268 #ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
270 int essential_prev = 0;
272 struct bitmask *oldmask = NULL;
273 bool have_numa = true;
274 unsigned long maxnode = 0;
276 /* Check if kernel supports NUMA. */
277 if (numa_available() != 0) {
278 RTE_LOG(DEBUG, EAL, "NUMA is not supported.\n");
283 RTE_LOG(DEBUG, EAL, "Trying to obtain current memory policy.\n");
284 oldmask = numa_allocate_nodemask();
285 if (get_mempolicy(&oldpolicy, oldmask->maskp,
286 oldmask->size + 1, 0, 0) < 0) {
288 "Failed to get current mempolicy: %s. "
289 "Assuming MPOL_DEFAULT.\n", strerror(errno));
290 oldpolicy = MPOL_DEFAULT;
292 for (i = 0; i < RTE_MAX_NUMA_NODES; i++)
293 if (internal_config.socket_mem[i])
298 for (i = 0; i < hpi->num_pages[0]; i++) {
299 struct hugepage_file *hf = &hugepg_tbl[i];
300 uint64_t hugepage_sz = hpi->hugepage_sz;
302 #ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
306 for (j = 0; j < maxnode; j++)
307 if (essential_memory[j])
311 node_id = (node_id + 1) % maxnode;
312 while (!internal_config.socket_mem[node_id]) {
319 essential_prev = essential_memory[j];
321 if (essential_memory[j] < hugepage_sz)
322 essential_memory[j] = 0;
324 essential_memory[j] -= hugepage_sz;
328 "Setting policy MPOL_PREFERRED for socket %d\n",
330 numa_set_preferred(node_id);
335 hf->size = hugepage_sz;
336 eal_get_hugefile_path(hf->filepath, sizeof(hf->filepath),
337 hpi->hugedir, hf->file_id);
338 hf->filepath[sizeof(hf->filepath) - 1] = '\0';
340 /* try to create hugepage file */
341 fd = open(hf->filepath, O_CREAT | O_RDWR, 0600);
343 RTE_LOG(DEBUG, EAL, "%s(): open failed: %s\n", __func__,
348 /* map the segment, and populate page tables,
349 * the kernel fills this segment with zeros. we don't care where
350 * this gets mapped - we already have contiguous memory areas
351 * ready for us to map into.
353 virtaddr = mmap(NULL, hugepage_sz, PROT_READ | PROT_WRITE,
354 MAP_SHARED | MAP_POPULATE, fd, 0);
355 if (virtaddr == MAP_FAILED) {
356 RTE_LOG(DEBUG, EAL, "%s(): mmap failed: %s\n", __func__,
362 hf->orig_va = virtaddr;
364 /* In linux, hugetlb limitations, like cgroup, are
365 * enforced at fault time instead of mmap(), even
366 * with the option of MAP_POPULATE. Kernel will send
367 * a SIGBUS signal. To avoid to be killed, save stack
368 * environment here, if SIGBUS happens, we can jump
371 if (huge_wrap_sigsetjmp()) {
372 RTE_LOG(DEBUG, EAL, "SIGBUS: Cannot mmap more "
373 "hugepages of size %u MB\n",
374 (unsigned int)(hugepage_sz / 0x100000));
375 munmap(virtaddr, hugepage_sz);
377 unlink(hugepg_tbl[i].filepath);
378 #ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
380 essential_memory[node_id] =
385 *(int *)virtaddr = 0;
387 /* set shared lock on the file. */
388 if (flock(fd, LOCK_SH) < 0) {
389 RTE_LOG(DEBUG, EAL, "%s(): Locking file failed:%s \n",
390 __func__, strerror(errno));
399 #ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
402 "Restoring previous memory policy: %d\n", oldpolicy);
403 if (oldpolicy == MPOL_DEFAULT) {
404 numa_set_localalloc();
405 } else if (set_mempolicy(oldpolicy, oldmask->maskp,
406 oldmask->size + 1) < 0) {
407 RTE_LOG(ERR, EAL, "Failed to restore mempolicy: %s\n",
409 numa_set_localalloc();
413 numa_free_cpumask(oldmask);
419 * Parse /proc/self/numa_maps to get the NUMA socket ID for each huge
423 find_numasocket(struct hugepage_file *hugepg_tbl, struct hugepage_info *hpi)
427 unsigned i, hp_count = 0;
430 char hugedir_str[PATH_MAX];
433 f = fopen("/proc/self/numa_maps", "r");
435 RTE_LOG(NOTICE, EAL, "NUMA support not available"
436 " consider that all memory is in socket_id 0\n");
440 snprintf(hugedir_str, sizeof(hugedir_str),
441 "%s/%s", hpi->hugedir, eal_get_hugefile_prefix());
444 while (fgets(buf, sizeof(buf), f) != NULL) {
446 /* ignore non huge page */
447 if (strstr(buf, " huge ") == NULL &&
448 strstr(buf, hugedir_str) == NULL)
452 virt_addr = strtoull(buf, &end, 16);
453 if (virt_addr == 0 || end == buf) {
454 RTE_LOG(ERR, EAL, "%s(): error in numa_maps parsing\n", __func__);
458 /* get node id (socket id) */
459 nodestr = strstr(buf, " N");
460 if (nodestr == NULL) {
461 RTE_LOG(ERR, EAL, "%s(): error in numa_maps parsing\n", __func__);
465 end = strstr(nodestr, "=");
467 RTE_LOG(ERR, EAL, "%s(): error in numa_maps parsing\n", __func__);
473 socket_id = strtoul(nodestr, &end, 0);
474 if ((nodestr[0] == '\0') || (end == NULL) || (*end != '\0')) {
475 RTE_LOG(ERR, EAL, "%s(): error in numa_maps parsing\n", __func__);
479 /* if we find this page in our mappings, set socket_id */
480 for (i = 0; i < hpi->num_pages[0]; i++) {
481 void *va = (void *)(unsigned long)virt_addr;
482 if (hugepg_tbl[i].orig_va == va) {
483 hugepg_tbl[i].socket_id = socket_id;
485 #ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
487 "Hugepage %s is on socket %d\n",
488 hugepg_tbl[i].filepath, socket_id);
494 if (hp_count < hpi->num_pages[0])
506 cmp_physaddr(const void *a, const void *b)
508 #ifndef RTE_ARCH_PPC_64
509 const struct hugepage_file *p1 = a;
510 const struct hugepage_file *p2 = b;
512 /* PowerPC needs memory sorted in reverse order from x86 */
513 const struct hugepage_file *p1 = b;
514 const struct hugepage_file *p2 = a;
516 if (p1->physaddr < p2->physaddr)
518 else if (p1->physaddr > p2->physaddr)
525 * Uses mmap to create a shared memory area for storage of data
526 * Used in this file to store the hugepage file map on disk
529 create_shared_memory(const char *filename, const size_t mem_size)
534 /* if no shared files mode is used, create anonymous memory instead */
535 if (internal_config.no_shconf) {
536 retval = mmap(NULL, mem_size, PROT_READ | PROT_WRITE,
537 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
538 if (retval == MAP_FAILED)
543 fd = open(filename, O_CREAT | O_RDWR, 0666);
546 if (ftruncate(fd, mem_size) < 0) {
550 retval = mmap(NULL, mem_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
552 if (retval == MAP_FAILED)
558 * this copies *active* hugepages from one hugepage table to another.
559 * destination is typically the shared memory.
562 copy_hugepages_to_shared_mem(struct hugepage_file * dst, int dest_size,
563 const struct hugepage_file * src, int src_size)
565 int src_pos, dst_pos = 0;
567 for (src_pos = 0; src_pos < src_size; src_pos++) {
568 if (src[src_pos].orig_va != NULL) {
569 /* error on overflow attempt */
570 if (dst_pos == dest_size)
572 memcpy(&dst[dst_pos], &src[src_pos], sizeof(struct hugepage_file));
580 unlink_hugepage_files(struct hugepage_file *hugepg_tbl,
581 unsigned num_hp_info)
583 unsigned socket, size;
584 int page, nrpages = 0;
586 /* get total number of hugepages */
587 for (size = 0; size < num_hp_info; size++)
588 for (socket = 0; socket < RTE_MAX_NUMA_NODES; socket++)
590 internal_config.hugepage_info[size].num_pages[socket];
592 for (page = 0; page < nrpages; page++) {
593 struct hugepage_file *hp = &hugepg_tbl[page];
595 if (hp->orig_va != NULL && unlink(hp->filepath)) {
596 RTE_LOG(WARNING, EAL, "%s(): Removing %s failed: %s\n",
597 __func__, hp->filepath, strerror(errno));
604 * unmaps hugepages that are not going to be used. since we originally allocate
605 * ALL hugepages (not just those we need), additional unmapping needs to be done.
608 unmap_unneeded_hugepages(struct hugepage_file *hugepg_tbl,
609 struct hugepage_info *hpi,
610 unsigned num_hp_info)
612 unsigned socket, size;
613 int page, nrpages = 0;
615 /* get total number of hugepages */
616 for (size = 0; size < num_hp_info; size++)
617 for (socket = 0; socket < RTE_MAX_NUMA_NODES; socket++)
618 nrpages += internal_config.hugepage_info[size].num_pages[socket];
620 for (size = 0; size < num_hp_info; size++) {
621 for (socket = 0; socket < RTE_MAX_NUMA_NODES; socket++) {
622 unsigned pages_found = 0;
624 /* traverse until we have unmapped all the unused pages */
625 for (page = 0; page < nrpages; page++) {
626 struct hugepage_file *hp = &hugepg_tbl[page];
628 /* find a page that matches the criteria */
629 if ((hp->size == hpi[size].hugepage_sz) &&
630 (hp->socket_id == (int) socket)) {
632 /* if we skipped enough pages, unmap the rest */
633 if (pages_found == hpi[size].num_pages[socket]) {
636 unmap_len = hp->size;
638 /* get start addr and len of the remaining segment */
643 if (unlink(hp->filepath) == -1) {
644 RTE_LOG(ERR, EAL, "%s(): Removing %s failed: %s\n",
645 __func__, hp->filepath, strerror(errno));
649 /* lock the page and skip */
655 } /* foreach socket */
656 } /* foreach pagesize */
662 remap_segment(struct hugepage_file *hugepages, int seg_start, int seg_end)
664 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
665 struct rte_memseg_list *msl;
666 struct rte_fbarray *arr;
667 int cur_page, seg_len;
668 unsigned int msl_idx;
674 page_sz = hugepages[seg_start].size;
675 socket_id = hugepages[seg_start].socket_id;
676 seg_len = seg_end - seg_start;
678 RTE_LOG(DEBUG, EAL, "Attempting to map %" PRIu64 "M on socket %i\n",
679 (seg_len * page_sz) >> 20ULL, socket_id);
681 /* find free space in memseg lists */
682 for (msl_idx = 0; msl_idx < RTE_MAX_MEMSEG_LISTS; msl_idx++) {
684 msl = &mcfg->memsegs[msl_idx];
685 arr = &msl->memseg_arr;
687 if (msl->page_sz != page_sz)
689 if (msl->socket_id != socket_id)
692 /* leave space for a hole if array is not empty */
693 empty = arr->count == 0;
694 ms_idx = rte_fbarray_find_next_n_free(arr, 0,
695 seg_len + (empty ? 0 : 1));
697 /* memseg list is full? */
701 /* leave some space between memsegs, they are not IOVA
702 * contiguous, so they shouldn't be VA contiguous either.
708 if (msl_idx == RTE_MAX_MEMSEG_LISTS) {
709 RTE_LOG(ERR, EAL, "Could not find space for memseg. Please increase %s and/or %s in configuration.\n",
710 RTE_STR(CONFIG_RTE_MAX_MEMSEG_PER_TYPE),
711 RTE_STR(CONFIG_RTE_MAX_MEM_PER_TYPE));
715 #ifdef RTE_ARCH_PPC64
716 /* for PPC64 we go through the list backwards */
717 for (cur_page = seg_end - 1; cur_page >= seg_start;
718 cur_page--, ms_idx++) {
720 for (cur_page = seg_start; cur_page < seg_end; cur_page++, ms_idx++) {
722 struct hugepage_file *hfile = &hugepages[cur_page];
723 struct rte_memseg *ms = rte_fbarray_get(arr, ms_idx);
727 fd = open(hfile->filepath, O_RDWR);
729 RTE_LOG(ERR, EAL, "Could not open '%s': %s\n",
730 hfile->filepath, strerror(errno));
733 /* set shared lock on the file. */
734 if (flock(fd, LOCK_SH) < 0) {
735 RTE_LOG(DEBUG, EAL, "Could not lock '%s': %s\n",
736 hfile->filepath, strerror(errno));
740 memseg_len = (size_t)page_sz;
741 addr = RTE_PTR_ADD(msl->base_va, ms_idx * memseg_len);
743 /* we know this address is already mmapped by memseg list, so
744 * using MAP_FIXED here is safe
746 addr = mmap(addr, page_sz, PROT_READ | PROT_WRITE,
747 MAP_SHARED | MAP_POPULATE | MAP_FIXED, fd, 0);
748 if (addr == MAP_FAILED) {
749 RTE_LOG(ERR, EAL, "Couldn't remap '%s': %s\n",
750 hfile->filepath, strerror(errno));
755 /* we have a new address, so unmap previous one */
757 /* in 32-bit legacy mode, we have already unmapped the page */
758 if (!internal_config.legacy_mem)
759 munmap(hfile->orig_va, page_sz);
761 munmap(hfile->orig_va, page_sz);
764 hfile->orig_va = NULL;
765 hfile->final_va = addr;
767 /* rewrite physical addresses in IOVA as VA mode */
768 if (rte_eal_iova_mode() == RTE_IOVA_VA)
769 hfile->physaddr = (uintptr_t)addr;
771 /* set up memseg data */
773 ms->hugepage_sz = page_sz;
774 ms->len = memseg_len;
775 ms->iova = hfile->physaddr;
776 ms->socket_id = hfile->socket_id;
777 ms->nchannel = rte_memory_get_nchannel();
778 ms->nrank = rte_memory_get_nrank();
780 rte_fbarray_set_used(arr, ms_idx);
782 /* store segment fd internally */
783 if (eal_memalloc_set_seg_fd(msl_idx, ms_idx, fd) < 0)
784 RTE_LOG(ERR, EAL, "Could not store segment fd: %s\n",
785 rte_strerror(rte_errno));
787 RTE_LOG(DEBUG, EAL, "Allocated %" PRIu64 "M on socket %i\n",
788 (seg_len * page_sz) >> 20, socket_id);
793 get_mem_amount(uint64_t page_sz, uint64_t max_mem)
795 uint64_t area_sz, max_pages;
797 /* limit to RTE_MAX_MEMSEG_PER_LIST pages or RTE_MAX_MEM_MB_PER_LIST */
798 max_pages = RTE_MAX_MEMSEG_PER_LIST;
799 max_mem = RTE_MIN((uint64_t)RTE_MAX_MEM_MB_PER_LIST << 20, max_mem);
801 area_sz = RTE_MIN(page_sz * max_pages, max_mem);
803 /* make sure the list isn't smaller than the page size */
804 area_sz = RTE_MAX(area_sz, page_sz);
806 return RTE_ALIGN(area_sz, page_sz);
810 free_memseg_list(struct rte_memseg_list *msl)
812 if (rte_fbarray_destroy(&msl->memseg_arr)) {
813 RTE_LOG(ERR, EAL, "Cannot destroy memseg list\n");
816 memset(msl, 0, sizeof(*msl));
820 #define MEMSEG_LIST_FMT "memseg-%" PRIu64 "k-%i-%i"
822 alloc_memseg_list(struct rte_memseg_list *msl, uint64_t page_sz,
823 int n_segs, int socket_id, int type_msl_idx)
825 char name[RTE_FBARRAY_NAME_LEN];
827 snprintf(name, sizeof(name), MEMSEG_LIST_FMT, page_sz >> 10, socket_id,
829 if (rte_fbarray_init(&msl->memseg_arr, name, n_segs,
830 sizeof(struct rte_memseg))) {
831 RTE_LOG(ERR, EAL, "Cannot allocate memseg list: %s\n",
832 rte_strerror(rte_errno));
836 msl->page_sz = page_sz;
837 msl->socket_id = socket_id;
840 RTE_LOG(DEBUG, EAL, "Memseg list allocated: 0x%zxkB at socket %i\n",
841 (size_t)page_sz >> 10, socket_id);
847 alloc_va_space(struct rte_memseg_list *msl)
854 page_sz = msl->page_sz;
855 mem_sz = page_sz * msl->memseg_arr.len;
857 addr = eal_get_virtual_area(msl->base_va, &mem_sz, page_sz, 0, flags);
859 if (rte_errno == EADDRNOTAVAIL)
860 RTE_LOG(ERR, EAL, "Could not mmap %llu bytes at [%p] - please use '--base-virtaddr' option\n",
861 (unsigned long long)mem_sz, msl->base_va);
863 RTE_LOG(ERR, EAL, "Cannot reserve memory\n");
873 * Our VA space is not preallocated yet, so preallocate it here. We need to know
874 * how many segments there are in order to map all pages into one address space,
875 * and leave appropriate holes between segments so that rte_malloc does not
876 * concatenate them into one big segment.
878 * we also need to unmap original pages to free up address space.
880 static int __rte_unused
881 prealloc_segments(struct hugepage_file *hugepages, int n_pages)
883 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
884 int cur_page, seg_start_page, end_seg, new_memseg;
885 unsigned int hpi_idx, socket, i;
886 int n_contig_segs, n_segs;
889 /* before we preallocate segments, we need to free up our VA space.
890 * we're not removing files, and we already have information about
891 * PA-contiguousness, so it is safe to unmap everything.
893 for (cur_page = 0; cur_page < n_pages; cur_page++) {
894 struct hugepage_file *hpi = &hugepages[cur_page];
895 munmap(hpi->orig_va, hpi->size);
899 /* we cannot know how many page sizes and sockets we have discovered, so
900 * loop over all of them
902 for (hpi_idx = 0; hpi_idx < internal_config.num_hugepage_sizes;
905 internal_config.hugepage_info[hpi_idx].hugepage_sz;
907 for (i = 0; i < rte_socket_count(); i++) {
908 struct rte_memseg_list *msl;
910 socket = rte_socket_id_by_idx(i);
915 for (cur_page = 0; cur_page < n_pages; cur_page++) {
916 struct hugepage_file *prev, *cur;
917 int prev_seg_start_page = -1;
919 cur = &hugepages[cur_page];
920 prev = cur_page == 0 ? NULL :
921 &hugepages[cur_page - 1];
928 else if (cur->socket_id != (int) socket)
930 else if (cur->size != page_sz)
932 else if (cur_page == 0)
934 #ifdef RTE_ARCH_PPC_64
935 /* On PPC64 architecture, the mmap always start
936 * from higher address to lower address. Here,
937 * physical addresses are in descending order.
939 else if ((prev->physaddr - cur->physaddr) !=
943 else if ((cur->physaddr - prev->physaddr) !=
948 /* if we're already inside a segment,
949 * new segment means end of current one
951 if (seg_start_page != -1) {
953 prev_seg_start_page =
956 seg_start_page = cur_page;
960 if (prev_seg_start_page != -1) {
961 /* we've found a new segment */
965 } else if (seg_start_page != -1) {
966 /* we didn't find new segment,
967 * but did end current one
975 /* we're skipping this page */
979 /* segment continues */
981 /* check if we missed last segment */
982 if (seg_start_page != -1) {
984 n_segs += cur_page - seg_start_page;
987 /* if no segments were found, do not preallocate */
991 /* we now have total number of pages that we will
992 * allocate for this segment list. add separator pages
993 * to the total count, and preallocate VA space.
995 n_segs += n_contig_segs - 1;
997 /* now, preallocate VA space for these segments */
999 /* first, find suitable memseg list for this */
1000 for (msl_idx = 0; msl_idx < RTE_MAX_MEMSEG_LISTS;
1002 msl = &mcfg->memsegs[msl_idx];
1004 if (msl->base_va != NULL)
1008 if (msl_idx == RTE_MAX_MEMSEG_LISTS) {
1009 RTE_LOG(ERR, EAL, "Not enough space in memseg lists, please increase %s\n",
1010 RTE_STR(CONFIG_RTE_MAX_MEMSEG_LISTS));
1014 /* now, allocate fbarray itself */
1015 if (alloc_memseg_list(msl, page_sz, n_segs, socket,
1019 /* finally, allocate VA space */
1020 if (alloc_va_space(msl) < 0)
1028 * We cannot reallocate memseg lists on the fly because PPC64 stores pages
1029 * backwards, therefore we have to process the entire memseg first before
1030 * remapping it into memseg list VA space.
1033 remap_needed_hugepages(struct hugepage_file *hugepages, int n_pages)
1035 int cur_page, seg_start_page, new_memseg, ret;
1038 for (cur_page = 0; cur_page < n_pages; cur_page++) {
1039 struct hugepage_file *prev, *cur;
1043 cur = &hugepages[cur_page];
1044 prev = cur_page == 0 ? NULL : &hugepages[cur_page - 1];
1046 /* if size is zero, no more pages left */
1052 else if (cur->socket_id != prev->socket_id)
1054 else if (cur->size != prev->size)
1056 #ifdef RTE_ARCH_PPC_64
1057 /* On PPC64 architecture, the mmap always start from higher
1058 * address to lower address. Here, physical addresses are in
1061 else if ((prev->physaddr - cur->physaddr) != cur->size)
1064 else if ((cur->physaddr - prev->physaddr) != cur->size)
1069 /* if this isn't the first time, remap segment */
1070 if (cur_page != 0) {
1071 ret = remap_segment(hugepages, seg_start_page,
1076 /* remember where we started */
1077 seg_start_page = cur_page;
1079 /* continuation of previous memseg */
1081 /* we were stopped, but we didn't remap the last segment, do it now */
1082 if (cur_page != 0) {
1083 ret = remap_segment(hugepages, seg_start_page,
1091 static inline uint64_t
1092 get_socket_mem_size(int socket)
1097 for (i = 0; i < internal_config.num_hugepage_sizes; i++){
1098 struct hugepage_info *hpi = &internal_config.hugepage_info[i];
1099 size += hpi->hugepage_sz * hpi->num_pages[socket];
1106 * This function is a NUMA-aware equivalent of calc_num_pages.
1107 * It takes in the list of hugepage sizes and the
1108 * number of pages thereof, and calculates the best number of
1109 * pages of each size to fulfill the request for <memory> ram
1112 calc_num_pages_per_socket(uint64_t * memory,
1113 struct hugepage_info *hp_info,
1114 struct hugepage_info *hp_used,
1115 unsigned num_hp_info)
1117 unsigned socket, j, i = 0;
1118 unsigned requested, available;
1119 int total_num_pages = 0;
1120 uint64_t remaining_mem, cur_mem;
1121 uint64_t total_mem = internal_config.memory;
1123 if (num_hp_info == 0)
1126 /* if specific memory amounts per socket weren't requested */
1127 if (internal_config.force_sockets == 0) {
1130 int cpu_per_socket[RTE_MAX_NUMA_NODES];
1131 size_t default_size;
1134 /* Compute number of cores per socket */
1135 memset(cpu_per_socket, 0, sizeof(cpu_per_socket));
1136 RTE_LCORE_FOREACH(lcore_id) {
1137 cpu_per_socket[rte_lcore_to_socket_id(lcore_id)]++;
1141 * Automatically spread requested memory amongst detected sockets according
1142 * to number of cores from cpu mask present on each socket
1144 total_size = internal_config.memory;
1145 for (socket = 0; socket < RTE_MAX_NUMA_NODES && total_size != 0; socket++) {
1147 /* Set memory amount per socket */
1148 default_size = (internal_config.memory * cpu_per_socket[socket])
1149 / rte_lcore_count();
1151 /* Limit to maximum available memory on socket */
1152 default_size = RTE_MIN(default_size, get_socket_mem_size(socket));
1155 memory[socket] = default_size;
1156 total_size -= default_size;
1160 * If some memory is remaining, try to allocate it by getting all
1161 * available memory from sockets, one after the other
1163 for (socket = 0; socket < RTE_MAX_NUMA_NODES && total_size != 0; socket++) {
1164 /* take whatever is available */
1165 default_size = RTE_MIN(get_socket_mem_size(socket) - memory[socket],
1169 memory[socket] += default_size;
1170 total_size -= default_size;
1173 /* in 32-bit mode, allocate all of the memory only on master
1176 total_size = internal_config.memory;
1177 for (socket = 0; socket < RTE_MAX_NUMA_NODES && total_size != 0;
1179 struct rte_config *cfg = rte_eal_get_configuration();
1180 unsigned int master_lcore_socket;
1182 master_lcore_socket =
1183 rte_lcore_to_socket_id(cfg->master_lcore);
1185 if (master_lcore_socket != socket)
1189 memory[socket] = total_size;
1195 for (socket = 0; socket < RTE_MAX_NUMA_NODES && total_mem != 0; socket++) {
1196 /* skips if the memory on specific socket wasn't requested */
1197 for (i = 0; i < num_hp_info && memory[socket] != 0; i++){
1198 strlcpy(hp_used[i].hugedir, hp_info[i].hugedir,
1199 sizeof(hp_used[i].hugedir));
1200 hp_used[i].num_pages[socket] = RTE_MIN(
1201 memory[socket] / hp_info[i].hugepage_sz,
1202 hp_info[i].num_pages[socket]);
1204 cur_mem = hp_used[i].num_pages[socket] *
1205 hp_used[i].hugepage_sz;
1207 memory[socket] -= cur_mem;
1208 total_mem -= cur_mem;
1210 total_num_pages += hp_used[i].num_pages[socket];
1212 /* check if we have met all memory requests */
1213 if (memory[socket] == 0)
1216 /* check if we have any more pages left at this size, if so
1217 * move on to next size */
1218 if (hp_used[i].num_pages[socket] == hp_info[i].num_pages[socket])
1220 /* At this point we know that there are more pages available that are
1221 * bigger than the memory we want, so lets see if we can get enough
1222 * from other page sizes.
1225 for (j = i+1; j < num_hp_info; j++)
1226 remaining_mem += hp_info[j].hugepage_sz *
1227 hp_info[j].num_pages[socket];
1229 /* is there enough other memory, if not allocate another page and quit */
1230 if (remaining_mem < memory[socket]){
1231 cur_mem = RTE_MIN(memory[socket],
1232 hp_info[i].hugepage_sz);
1233 memory[socket] -= cur_mem;
1234 total_mem -= cur_mem;
1235 hp_used[i].num_pages[socket]++;
1237 break; /* we are done with this socket*/
1240 /* if we didn't satisfy all memory requirements per socket */
1241 if (memory[socket] > 0 &&
1242 internal_config.socket_mem[socket] != 0) {
1243 /* to prevent icc errors */
1244 requested = (unsigned) (internal_config.socket_mem[socket] /
1246 available = requested -
1247 ((unsigned) (memory[socket] / 0x100000));
1248 RTE_LOG(ERR, EAL, "Not enough memory available on socket %u! "
1249 "Requested: %uMB, available: %uMB\n", socket,
1250 requested, available);
1255 /* if we didn't satisfy total memory requirements */
1256 if (total_mem > 0) {
1257 requested = (unsigned) (internal_config.memory / 0x100000);
1258 available = requested - (unsigned) (total_mem / 0x100000);
1259 RTE_LOG(ERR, EAL, "Not enough memory available! Requested: %uMB,"
1260 " available: %uMB\n", requested, available);
1263 return total_num_pages;
1266 static inline size_t
1267 eal_get_hugepage_mem_size(void)
1272 for (i = 0; i < internal_config.num_hugepage_sizes; i++) {
1273 struct hugepage_info *hpi = &internal_config.hugepage_info[i];
1274 if (strnlen(hpi->hugedir, sizeof(hpi->hugedir)) != 0) {
1275 for (j = 0; j < RTE_MAX_NUMA_NODES; j++) {
1276 size += hpi->hugepage_sz * hpi->num_pages[j];
1281 return (size < SIZE_MAX) ? (size_t)(size) : SIZE_MAX;
1284 static struct sigaction huge_action_old;
1285 static int huge_need_recover;
1288 huge_register_sigbus(void)
1291 struct sigaction action;
1294 sigaddset(&mask, SIGBUS);
1295 action.sa_flags = 0;
1296 action.sa_mask = mask;
1297 action.sa_handler = huge_sigbus_handler;
1299 huge_need_recover = !sigaction(SIGBUS, &action, &huge_action_old);
1303 huge_recover_sigbus(void)
1305 if (huge_need_recover) {
1306 sigaction(SIGBUS, &huge_action_old, NULL);
1307 huge_need_recover = 0;
1312 * Prepare physical memory mapping: fill configuration structure with
1313 * these infos, return 0 on success.
1314 * 1. map N huge pages in separate files in hugetlbfs
1315 * 2. find associated physical addr
1316 * 3. find associated NUMA socket ID
1317 * 4. sort all huge pages by physical address
1318 * 5. remap these N huge pages in the correct order
1319 * 6. unmap the first mapping
1320 * 7. fill memsegs in configuration with contiguous zones
1323 eal_legacy_hugepage_init(void)
1325 struct rte_mem_config *mcfg;
1326 struct hugepage_file *hugepage = NULL, *tmp_hp = NULL;
1327 struct hugepage_info used_hp[MAX_HUGEPAGE_SIZES];
1328 struct rte_fbarray *arr;
1329 struct rte_memseg *ms;
1331 uint64_t memory[RTE_MAX_NUMA_NODES];
1335 int nr_hugefiles, nr_hugepages = 0;
1338 test_phys_addrs_available();
1340 memset(used_hp, 0, sizeof(used_hp));
1342 /* get pointer to global configuration */
1343 mcfg = rte_eal_get_configuration()->mem_config;
1345 /* hugetlbfs can be disabled */
1346 if (internal_config.no_hugetlbfs) {
1347 struct rte_memseg_list *msl;
1348 int n_segs, cur_seg, fd, flags;
1349 #ifdef MEMFD_SUPPORTED
1354 /* nohuge mode is legacy mode */
1355 internal_config.legacy_mem = 1;
1357 /* nohuge mode is single-file segments mode */
1358 internal_config.single_file_segments = 1;
1360 /* create a memseg list */
1361 msl = &mcfg->memsegs[0];
1363 page_sz = RTE_PGSIZE_4K;
1364 n_segs = internal_config.memory / page_sz;
1366 if (rte_fbarray_init(&msl->memseg_arr, "nohugemem", n_segs,
1367 sizeof(struct rte_memseg))) {
1368 RTE_LOG(ERR, EAL, "Cannot allocate memseg list\n");
1372 /* set up parameters for anonymous mmap */
1374 flags = MAP_PRIVATE | MAP_ANONYMOUS;
1376 #ifdef MEMFD_SUPPORTED
1377 /* create a memfd and store it in the segment fd table */
1378 memfd = memfd_create("nohuge", 0);
1380 RTE_LOG(DEBUG, EAL, "Cannot create memfd: %s\n",
1382 RTE_LOG(DEBUG, EAL, "Falling back to anonymous map\n");
1384 /* we got an fd - now resize it */
1385 if (ftruncate(memfd, internal_config.memory) < 0) {
1386 RTE_LOG(ERR, EAL, "Cannot resize memfd: %s\n",
1388 RTE_LOG(ERR, EAL, "Falling back to anonymous map\n");
1391 /* creating memfd-backed file was successful.
1392 * we want changes to memfd to be visible to
1393 * other processes (such as vhost backend), so
1394 * map it as shared memory.
1396 RTE_LOG(DEBUG, EAL, "Using memfd for anonymous memory\n");
1402 addr = mmap(NULL, internal_config.memory, PROT_READ | PROT_WRITE,
1404 if (addr == MAP_FAILED) {
1405 RTE_LOG(ERR, EAL, "%s: mmap() failed: %s\n", __func__,
1409 msl->base_va = addr;
1410 msl->page_sz = page_sz;
1412 msl->len = internal_config.memory;
1414 /* we're in single-file segments mode, so only the segment list
1415 * fd needs to be set up.
1418 if (eal_memalloc_set_seg_list_fd(0, fd) < 0) {
1419 RTE_LOG(ERR, EAL, "Cannot set up segment list fd\n");
1420 /* not a serious error, proceed */
1424 /* populate memsegs. each memseg is one page long */
1425 for (cur_seg = 0; cur_seg < n_segs; cur_seg++) {
1426 arr = &msl->memseg_arr;
1428 ms = rte_fbarray_get(arr, cur_seg);
1429 if (rte_eal_iova_mode() == RTE_IOVA_VA)
1430 ms->iova = (uintptr_t)addr;
1432 ms->iova = RTE_BAD_IOVA;
1434 ms->hugepage_sz = page_sz;
1438 rte_fbarray_set_used(arr, cur_seg);
1440 addr = RTE_PTR_ADD(addr, (size_t)page_sz);
1442 if (mcfg->dma_maskbits &&
1443 rte_mem_check_dma_mask_thread_unsafe(mcfg->dma_maskbits)) {
1445 "%s(): couldnt allocate memory due to IOVA exceeding limits of current DMA mask.\n",
1447 if (rte_eal_iova_mode() == RTE_IOVA_VA &&
1448 rte_eal_using_phys_addrs())
1450 "%s(): Please try initializing EAL with --iova-mode=pa parameter.\n",
1457 /* calculate total number of hugepages available. at this point we haven't
1458 * yet started sorting them so they all are on socket 0 */
1459 for (i = 0; i < (int) internal_config.num_hugepage_sizes; i++) {
1460 /* meanwhile, also initialize used_hp hugepage sizes in used_hp */
1461 used_hp[i].hugepage_sz = internal_config.hugepage_info[i].hugepage_sz;
1463 nr_hugepages += internal_config.hugepage_info[i].num_pages[0];
1467 * allocate a memory area for hugepage table.
1468 * this isn't shared memory yet. due to the fact that we need some
1469 * processing done on these pages, shared memory will be created
1472 tmp_hp = malloc(nr_hugepages * sizeof(struct hugepage_file));
1476 memset(tmp_hp, 0, nr_hugepages * sizeof(struct hugepage_file));
1478 hp_offset = 0; /* where we start the current page size entries */
1480 huge_register_sigbus();
1482 /* make a copy of socket_mem, needed for balanced allocation. */
1483 for (i = 0; i < RTE_MAX_NUMA_NODES; i++)
1484 memory[i] = internal_config.socket_mem[i];
1486 /* map all hugepages and sort them */
1487 for (i = 0; i < (int)internal_config.num_hugepage_sizes; i ++){
1488 unsigned pages_old, pages_new;
1489 struct hugepage_info *hpi;
1492 * we don't yet mark hugepages as used at this stage, so
1493 * we just map all hugepages available to the system
1494 * all hugepages are still located on socket 0
1496 hpi = &internal_config.hugepage_info[i];
1498 if (hpi->num_pages[0] == 0)
1501 /* map all hugepages available */
1502 pages_old = hpi->num_pages[0];
1503 pages_new = map_all_hugepages(&tmp_hp[hp_offset], hpi, memory);
1504 if (pages_new < pages_old) {
1506 "%d not %d hugepages of size %u MB allocated\n",
1507 pages_new, pages_old,
1508 (unsigned)(hpi->hugepage_sz / 0x100000));
1510 int pages = pages_old - pages_new;
1512 nr_hugepages -= pages;
1513 hpi->num_pages[0] = pages_new;
1518 if (phys_addrs_available &&
1519 rte_eal_iova_mode() != RTE_IOVA_VA) {
1520 /* find physical addresses for each hugepage */
1521 if (find_physaddrs(&tmp_hp[hp_offset], hpi) < 0) {
1522 RTE_LOG(DEBUG, EAL, "Failed to find phys addr "
1523 "for %u MB pages\n",
1524 (unsigned int)(hpi->hugepage_sz / 0x100000));
1528 /* set physical addresses for each hugepage */
1529 if (set_physaddrs(&tmp_hp[hp_offset], hpi) < 0) {
1530 RTE_LOG(DEBUG, EAL, "Failed to set phys addr "
1531 "for %u MB pages\n",
1532 (unsigned int)(hpi->hugepage_sz / 0x100000));
1537 if (find_numasocket(&tmp_hp[hp_offset], hpi) < 0){
1538 RTE_LOG(DEBUG, EAL, "Failed to find NUMA socket for %u MB pages\n",
1539 (unsigned)(hpi->hugepage_sz / 0x100000));
1543 qsort(&tmp_hp[hp_offset], hpi->num_pages[0],
1544 sizeof(struct hugepage_file), cmp_physaddr);
1546 /* we have processed a num of hugepages of this size, so inc offset */
1547 hp_offset += hpi->num_pages[0];
1550 huge_recover_sigbus();
1552 if (internal_config.memory == 0 && internal_config.force_sockets == 0)
1553 internal_config.memory = eal_get_hugepage_mem_size();
1555 nr_hugefiles = nr_hugepages;
1558 /* clean out the numbers of pages */
1559 for (i = 0; i < (int) internal_config.num_hugepage_sizes; i++)
1560 for (j = 0; j < RTE_MAX_NUMA_NODES; j++)
1561 internal_config.hugepage_info[i].num_pages[j] = 0;
1563 /* get hugepages for each socket */
1564 for (i = 0; i < nr_hugefiles; i++) {
1565 int socket = tmp_hp[i].socket_id;
1567 /* find a hugepage info with right size and increment num_pages */
1568 const int nb_hpsizes = RTE_MIN(MAX_HUGEPAGE_SIZES,
1569 (int)internal_config.num_hugepage_sizes);
1570 for (j = 0; j < nb_hpsizes; j++) {
1571 if (tmp_hp[i].size ==
1572 internal_config.hugepage_info[j].hugepage_sz) {
1573 internal_config.hugepage_info[j].num_pages[socket]++;
1578 /* make a copy of socket_mem, needed for number of pages calculation */
1579 for (i = 0; i < RTE_MAX_NUMA_NODES; i++)
1580 memory[i] = internal_config.socket_mem[i];
1582 /* calculate final number of pages */
1583 nr_hugepages = calc_num_pages_per_socket(memory,
1584 internal_config.hugepage_info, used_hp,
1585 internal_config.num_hugepage_sizes);
1587 /* error if not enough memory available */
1588 if (nr_hugepages < 0)
1592 for (i = 0; i < (int) internal_config.num_hugepage_sizes; i++) {
1593 for (j = 0; j < RTE_MAX_NUMA_NODES; j++) {
1594 if (used_hp[i].num_pages[j] > 0) {
1596 "Requesting %u pages of size %uMB"
1597 " from socket %i\n",
1598 used_hp[i].num_pages[j],
1600 (used_hp[i].hugepage_sz / 0x100000),
1606 /* create shared memory */
1607 hugepage = create_shared_memory(eal_hugepage_data_path(),
1608 nr_hugefiles * sizeof(struct hugepage_file));
1610 if (hugepage == NULL) {
1611 RTE_LOG(ERR, EAL, "Failed to create shared memory!\n");
1614 memset(hugepage, 0, nr_hugefiles * sizeof(struct hugepage_file));
1617 * unmap pages that we won't need (looks at used_hp).
1618 * also, sets final_va to NULL on pages that were unmapped.
1620 if (unmap_unneeded_hugepages(tmp_hp, used_hp,
1621 internal_config.num_hugepage_sizes) < 0) {
1622 RTE_LOG(ERR, EAL, "Unmapping and locking hugepages failed!\n");
1627 * copy stuff from malloc'd hugepage* to the actual shared memory.
1628 * this procedure only copies those hugepages that have orig_va
1629 * not NULL. has overflow protection.
1631 if (copy_hugepages_to_shared_mem(hugepage, nr_hugefiles,
1632 tmp_hp, nr_hugefiles) < 0) {
1633 RTE_LOG(ERR, EAL, "Copying tables to shared memory failed!\n");
1638 /* for legacy 32-bit mode, we did not preallocate VA space, so do it */
1639 if (internal_config.legacy_mem &&
1640 prealloc_segments(hugepage, nr_hugefiles)) {
1641 RTE_LOG(ERR, EAL, "Could not preallocate VA space for hugepages\n");
1646 /* remap all pages we do need into memseg list VA space, so that those
1647 * pages become first-class citizens in DPDK memory subsystem
1649 if (remap_needed_hugepages(hugepage, nr_hugefiles)) {
1650 RTE_LOG(ERR, EAL, "Couldn't remap hugepage files into memseg lists\n");
1654 /* free the hugepage backing files */
1655 if (internal_config.hugepage_unlink &&
1656 unlink_hugepage_files(tmp_hp, internal_config.num_hugepage_sizes) < 0) {
1657 RTE_LOG(ERR, EAL, "Unlinking hugepage files failed!\n");
1661 /* free the temporary hugepage table */
1665 munmap(hugepage, nr_hugefiles * sizeof(struct hugepage_file));
1668 /* we're not going to allocate more pages, so release VA space for
1669 * unused memseg lists
1671 for (i = 0; i < RTE_MAX_MEMSEG_LISTS; i++) {
1672 struct rte_memseg_list *msl = &mcfg->memsegs[i];
1675 /* skip inactive lists */
1676 if (msl->base_va == NULL)
1678 /* skip lists where there is at least one page allocated */
1679 if (msl->memseg_arr.count > 0)
1681 /* this is an unused list, deallocate it */
1683 munmap(msl->base_va, mem_sz);
1684 msl->base_va = NULL;
1686 /* destroy backing fbarray */
1687 rte_fbarray_destroy(&msl->memseg_arr);
1690 if (mcfg->dma_maskbits &&
1691 rte_mem_check_dma_mask_thread_unsafe(mcfg->dma_maskbits)) {
1693 "%s(): couldn't allocate memory due to IOVA exceeding limits of current DMA mask.\n",
1701 huge_recover_sigbus();
1703 if (hugepage != NULL)
1704 munmap(hugepage, nr_hugefiles * sizeof(struct hugepage_file));
1709 static int __rte_unused
1710 hugepage_count_walk(const struct rte_memseg_list *msl, void *arg)
1712 struct hugepage_info *hpi = arg;
1714 if (msl->page_sz != hpi->hugepage_sz)
1717 hpi->num_pages[msl->socket_id] += msl->memseg_arr.len;
1722 limits_callback(int socket_id, size_t cur_limit, size_t new_len)
1724 RTE_SET_USED(socket_id);
1725 RTE_SET_USED(cur_limit);
1726 RTE_SET_USED(new_len);
1731 eal_hugepage_init(void)
1733 struct hugepage_info used_hp[MAX_HUGEPAGE_SIZES];
1734 uint64_t memory[RTE_MAX_NUMA_NODES];
1735 int hp_sz_idx, socket_id;
1737 test_phys_addrs_available();
1739 memset(used_hp, 0, sizeof(used_hp));
1742 hp_sz_idx < (int) internal_config.num_hugepage_sizes;
1745 struct hugepage_info dummy;
1748 /* also initialize used_hp hugepage sizes in used_hp */
1749 struct hugepage_info *hpi;
1750 hpi = &internal_config.hugepage_info[hp_sz_idx];
1751 used_hp[hp_sz_idx].hugepage_sz = hpi->hugepage_sz;
1754 /* for 32-bit, limit number of pages on socket to whatever we've
1755 * preallocated, as we cannot allocate more.
1757 memset(&dummy, 0, sizeof(dummy));
1758 dummy.hugepage_sz = hpi->hugepage_sz;
1759 if (rte_memseg_list_walk(hugepage_count_walk, &dummy) < 0)
1762 for (i = 0; i < RTE_DIM(dummy.num_pages); i++) {
1763 hpi->num_pages[i] = RTE_MIN(hpi->num_pages[i],
1764 dummy.num_pages[i]);
1769 /* make a copy of socket_mem, needed for balanced allocation. */
1770 for (hp_sz_idx = 0; hp_sz_idx < RTE_MAX_NUMA_NODES; hp_sz_idx++)
1771 memory[hp_sz_idx] = internal_config.socket_mem[hp_sz_idx];
1773 /* calculate final number of pages */
1774 if (calc_num_pages_per_socket(memory,
1775 internal_config.hugepage_info, used_hp,
1776 internal_config.num_hugepage_sizes) < 0)
1780 hp_sz_idx < (int)internal_config.num_hugepage_sizes;
1782 for (socket_id = 0; socket_id < RTE_MAX_NUMA_NODES;
1784 struct rte_memseg **pages;
1785 struct hugepage_info *hpi = &used_hp[hp_sz_idx];
1786 unsigned int num_pages = hpi->num_pages[socket_id];
1787 unsigned int num_pages_alloc;
1792 RTE_LOG(DEBUG, EAL, "Allocating %u pages of size %" PRIu64 "M on socket %i\n",
1793 num_pages, hpi->hugepage_sz >> 20, socket_id);
1795 /* we may not be able to allocate all pages in one go,
1796 * because we break up our memory map into multiple
1797 * memseg lists. therefore, try allocating multiple
1798 * times and see if we can get the desired number of
1799 * pages from multiple allocations.
1802 num_pages_alloc = 0;
1804 int i, cur_pages, needed;
1806 needed = num_pages - num_pages_alloc;
1808 pages = malloc(sizeof(*pages) * needed);
1810 /* do not request exact number of pages */
1811 cur_pages = eal_memalloc_alloc_seg_bulk(pages,
1812 needed, hpi->hugepage_sz,
1814 if (cur_pages <= 0) {
1819 /* mark preallocated pages as unfreeable */
1820 for (i = 0; i < cur_pages; i++) {
1821 struct rte_memseg *ms = pages[i];
1822 ms->flags |= RTE_MEMSEG_FLAG_DO_NOT_FREE;
1826 num_pages_alloc += cur_pages;
1827 } while (num_pages_alloc != num_pages);
1830 /* if socket limits were specified, set them */
1831 if (internal_config.force_socket_limits) {
1833 for (i = 0; i < RTE_MAX_NUMA_NODES; i++) {
1834 uint64_t limit = internal_config.socket_limit[i];
1837 if (rte_mem_alloc_validator_register("socket-limit",
1838 limits_callback, i, limit))
1839 RTE_LOG(ERR, EAL, "Failed to register socket limits validator callback\n");
1846 * uses fstat to report the size of a file on disk
1852 if (fstat(fd, &st) < 0)
1858 * This creates the memory mappings in the secondary process to match that of
1859 * the server process. It goes through each memory segment in the DPDK runtime
1860 * configuration and finds the hugepages which form that segment, mapping them
1861 * in order to form a contiguous block in the virtual memory space
1864 eal_legacy_hugepage_attach(void)
1866 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
1867 struct hugepage_file *hp = NULL;
1868 unsigned int num_hp = 0;
1870 unsigned int cur_seg;
1872 int fd, fd_hugepage = -1;
1874 if (aslr_enabled() > 0) {
1875 RTE_LOG(WARNING, EAL, "WARNING: Address Space Layout Randomization "
1876 "(ASLR) is enabled in the kernel.\n");
1877 RTE_LOG(WARNING, EAL, " This may cause issues with mapping memory "
1878 "into secondary processes\n");
1881 test_phys_addrs_available();
1883 fd_hugepage = open(eal_hugepage_data_path(), O_RDONLY);
1884 if (fd_hugepage < 0) {
1885 RTE_LOG(ERR, EAL, "Could not open %s\n",
1886 eal_hugepage_data_path());
1890 size = getFileSize(fd_hugepage);
1891 hp = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd_hugepage, 0);
1892 if (hp == MAP_FAILED) {
1893 RTE_LOG(ERR, EAL, "Could not mmap %s\n",
1894 eal_hugepage_data_path());
1898 num_hp = size / sizeof(struct hugepage_file);
1899 RTE_LOG(DEBUG, EAL, "Analysing %u files\n", num_hp);
1901 /* map all segments into memory to make sure we get the addrs. the
1902 * segments themselves are already in memseg list (which is shared and
1903 * has its VA space already preallocated), so we just need to map
1904 * everything into correct addresses.
1906 for (i = 0; i < num_hp; i++) {
1907 struct hugepage_file *hf = &hp[i];
1908 size_t map_sz = hf->size;
1909 void *map_addr = hf->final_va;
1910 int msl_idx, ms_idx;
1911 struct rte_memseg_list *msl;
1912 struct rte_memseg *ms;
1914 /* if size is zero, no more pages left */
1918 fd = open(hf->filepath, O_RDWR);
1920 RTE_LOG(ERR, EAL, "Could not open %s: %s\n",
1921 hf->filepath, strerror(errno));
1925 map_addr = mmap(map_addr, map_sz, PROT_READ | PROT_WRITE,
1926 MAP_SHARED | MAP_FIXED, fd, 0);
1927 if (map_addr == MAP_FAILED) {
1928 RTE_LOG(ERR, EAL, "Could not map %s: %s\n",
1929 hf->filepath, strerror(errno));
1933 /* set shared lock on the file. */
1934 if (flock(fd, LOCK_SH) < 0) {
1935 RTE_LOG(DEBUG, EAL, "%s(): Locking file failed: %s\n",
1936 __func__, strerror(errno));
1940 /* find segment data */
1941 msl = rte_mem_virt2memseg_list(map_addr);
1943 RTE_LOG(DEBUG, EAL, "%s(): Cannot find memseg list\n",
1947 ms = rte_mem_virt2memseg(map_addr, msl);
1949 RTE_LOG(DEBUG, EAL, "%s(): Cannot find memseg\n",
1954 msl_idx = msl - mcfg->memsegs;
1955 ms_idx = rte_fbarray_find_idx(&msl->memseg_arr, ms);
1957 RTE_LOG(DEBUG, EAL, "%s(): Cannot find memseg idx\n",
1962 /* store segment fd internally */
1963 if (eal_memalloc_set_seg_fd(msl_idx, ms_idx, fd) < 0)
1964 RTE_LOG(ERR, EAL, "Could not store segment fd: %s\n",
1965 rte_strerror(rte_errno));
1967 /* unmap the hugepage config file, since we are done using it */
1975 /* map all segments into memory to make sure we get the addrs */
1977 for (cur_seg = 0; cur_seg < i; cur_seg++) {
1978 struct hugepage_file *hf = &hp[i];
1979 size_t map_sz = hf->size;
1980 void *map_addr = hf->final_va;
1982 munmap(map_addr, map_sz);
1984 if (hp != NULL && hp != MAP_FAILED)
1986 if (fd_hugepage >= 0)
1992 eal_hugepage_attach(void)
1994 if (eal_memalloc_sync_with_primary()) {
1995 RTE_LOG(ERR, EAL, "Could not map memory from primary process\n");
1996 if (aslr_enabled() > 0)
1997 RTE_LOG(ERR, EAL, "It is recommended to disable ASLR in the kernel and retry running both primary and secondary processes\n");
2004 rte_eal_hugepage_init(void)
2006 return internal_config.legacy_mem ?
2007 eal_legacy_hugepage_init() :
2008 eal_hugepage_init();
2012 rte_eal_hugepage_attach(void)
2014 return internal_config.legacy_mem ?
2015 eal_legacy_hugepage_attach() :
2016 eal_hugepage_attach();
2020 rte_eal_using_phys_addrs(void)
2022 return phys_addrs_available;
2025 static int __rte_unused
2026 memseg_primary_init_32(void)
2028 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
2029 int active_sockets, hpi_idx, msl_idx = 0;
2030 unsigned int socket_id, i;
2031 struct rte_memseg_list *msl;
2032 uint64_t extra_mem_per_socket, total_extra_mem, total_requested_mem;
2035 /* no-huge does not need this at all */
2036 if (internal_config.no_hugetlbfs)
2039 /* this is a giant hack, but desperate times call for desperate
2040 * measures. in legacy 32-bit mode, we cannot preallocate VA space,
2041 * because having upwards of 2 gigabytes of VA space already mapped will
2042 * interfere with our ability to map and sort hugepages.
2044 * therefore, in legacy 32-bit mode, we will be initializing memseg
2045 * lists much later - in eal_memory.c, right after we unmap all the
2046 * unneeded pages. this will not affect secondary processes, as those
2047 * should be able to mmap the space without (too many) problems.
2049 if (internal_config.legacy_mem)
2052 /* 32-bit mode is a very special case. we cannot know in advance where
2053 * the user will want to allocate their memory, so we have to do some
2057 total_requested_mem = 0;
2058 if (internal_config.force_sockets)
2059 for (i = 0; i < rte_socket_count(); i++) {
2062 socket_id = rte_socket_id_by_idx(i);
2063 mem = internal_config.socket_mem[socket_id];
2069 total_requested_mem += mem;
2072 total_requested_mem = internal_config.memory;
2074 max_mem = (uint64_t)RTE_MAX_MEM_MB << 20;
2075 if (total_requested_mem > max_mem) {
2076 RTE_LOG(ERR, EAL, "Invalid parameters: 32-bit process can at most use %uM of memory\n",
2077 (unsigned int)(max_mem >> 20));
2080 total_extra_mem = max_mem - total_requested_mem;
2081 extra_mem_per_socket = active_sockets == 0 ? total_extra_mem :
2082 total_extra_mem / active_sockets;
2084 /* the allocation logic is a little bit convoluted, but here's how it
2085 * works, in a nutshell:
2086 * - if user hasn't specified on which sockets to allocate memory via
2087 * --socket-mem, we allocate all of our memory on master core socket.
2088 * - if user has specified sockets to allocate memory on, there may be
2089 * some "unused" memory left (e.g. if user has specified --socket-mem
2090 * such that not all memory adds up to 2 gigabytes), so add it to all
2091 * sockets that are in use equally.
2093 * page sizes are sorted by size in descending order, so we can safely
2094 * assume that we dispense with bigger page sizes first.
2097 /* create memseg lists */
2098 for (i = 0; i < rte_socket_count(); i++) {
2099 int hp_sizes = (int) internal_config.num_hugepage_sizes;
2100 uint64_t max_socket_mem, cur_socket_mem;
2101 unsigned int master_lcore_socket;
2102 struct rte_config *cfg = rte_eal_get_configuration();
2105 socket_id = rte_socket_id_by_idx(i);
2107 #ifndef RTE_EAL_NUMA_AWARE_HUGEPAGES
2112 /* if we didn't specifically request memory on this socket */
2113 skip = active_sockets != 0 &&
2114 internal_config.socket_mem[socket_id] == 0;
2115 /* ...or if we didn't specifically request memory on *any*
2116 * socket, and this is not master lcore
2118 master_lcore_socket = rte_lcore_to_socket_id(cfg->master_lcore);
2119 skip |= active_sockets == 0 && socket_id != master_lcore_socket;
2122 RTE_LOG(DEBUG, EAL, "Will not preallocate memory on socket %u\n",
2127 /* max amount of memory on this socket */
2128 max_socket_mem = (active_sockets != 0 ?
2129 internal_config.socket_mem[socket_id] :
2130 internal_config.memory) +
2131 extra_mem_per_socket;
2134 for (hpi_idx = 0; hpi_idx < hp_sizes; hpi_idx++) {
2135 uint64_t max_pagesz_mem, cur_pagesz_mem = 0;
2136 uint64_t hugepage_sz;
2137 struct hugepage_info *hpi;
2138 int type_msl_idx, max_segs, total_segs = 0;
2140 hpi = &internal_config.hugepage_info[hpi_idx];
2141 hugepage_sz = hpi->hugepage_sz;
2143 /* check if pages are actually available */
2144 if (hpi->num_pages[socket_id] == 0)
2147 max_segs = RTE_MAX_MEMSEG_PER_TYPE;
2148 max_pagesz_mem = max_socket_mem - cur_socket_mem;
2150 /* make it multiple of page size */
2151 max_pagesz_mem = RTE_ALIGN_FLOOR(max_pagesz_mem,
2154 RTE_LOG(DEBUG, EAL, "Attempting to preallocate "
2155 "%" PRIu64 "M on socket %i\n",
2156 max_pagesz_mem >> 20, socket_id);
2159 while (cur_pagesz_mem < max_pagesz_mem &&
2160 total_segs < max_segs) {
2162 unsigned int n_segs;
2164 if (msl_idx >= RTE_MAX_MEMSEG_LISTS) {
2166 "No more space in memseg lists, please increase %s\n",
2167 RTE_STR(CONFIG_RTE_MAX_MEMSEG_LISTS));
2171 msl = &mcfg->memsegs[msl_idx];
2173 cur_mem = get_mem_amount(hugepage_sz,
2175 n_segs = cur_mem / hugepage_sz;
2177 if (alloc_memseg_list(msl, hugepage_sz, n_segs,
2178 socket_id, type_msl_idx)) {
2179 /* failing to allocate a memseg list is
2182 RTE_LOG(ERR, EAL, "Cannot allocate memseg list\n");
2186 if (alloc_va_space(msl)) {
2187 /* if we couldn't allocate VA space, we
2188 * can try with smaller page sizes.
2190 RTE_LOG(ERR, EAL, "Cannot allocate VA space for memseg list, retrying with different page size\n");
2191 /* deallocate memseg list */
2192 if (free_memseg_list(msl))
2197 total_segs += msl->memseg_arr.len;
2198 cur_pagesz_mem = total_segs * hugepage_sz;
2202 cur_socket_mem += cur_pagesz_mem;
2204 if (cur_socket_mem == 0) {
2205 RTE_LOG(ERR, EAL, "Cannot allocate VA space on socket %u\n",
2214 static int __rte_unused
2215 memseg_primary_init(void)
2217 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
2222 int i, hpi_idx, msl_idx, ret = -1; /* fail unless told to succeed */
2223 struct rte_memseg_list *msl;
2224 uint64_t max_mem, max_mem_per_type;
2225 unsigned int max_seglists_per_type;
2226 unsigned int n_memtypes, cur_type;
2228 /* no-huge does not need this at all */
2229 if (internal_config.no_hugetlbfs)
2233 * figuring out amount of memory we're going to have is a long and very
2234 * involved process. the basic element we're operating with is a memory
2235 * type, defined as a combination of NUMA node ID and page size (so that
2236 * e.g. 2 sockets with 2 page sizes yield 4 memory types in total).
2238 * deciding amount of memory going towards each memory type is a
2239 * balancing act between maximum segments per type, maximum memory per
2240 * type, and number of detected NUMA nodes. the goal is to make sure
2241 * each memory type gets at least one memseg list.
2243 * the total amount of memory is limited by RTE_MAX_MEM_MB value.
2245 * the total amount of memory per type is limited by either
2246 * RTE_MAX_MEM_MB_PER_TYPE, or by RTE_MAX_MEM_MB divided by the number
2247 * of detected NUMA nodes. additionally, maximum number of segments per
2248 * type is also limited by RTE_MAX_MEMSEG_PER_TYPE. this is because for
2249 * smaller page sizes, it can take hundreds of thousands of segments to
2250 * reach the above specified per-type memory limits.
2252 * additionally, each type may have multiple memseg lists associated
2253 * with it, each limited by either RTE_MAX_MEM_MB_PER_LIST for bigger
2254 * page sizes, or RTE_MAX_MEMSEG_PER_LIST segments for smaller ones.
2256 * the number of memseg lists per type is decided based on the above
2257 * limits, and also taking number of detected NUMA nodes, to make sure
2258 * that we don't run out of memseg lists before we populate all NUMA
2259 * nodes with memory.
2261 * we do this in three stages. first, we collect the number of types.
2262 * then, we figure out memory constraints and populate the list of
2263 * would-be memseg lists. then, we go ahead and allocate the memseg
2267 /* create space for mem types */
2268 n_memtypes = internal_config.num_hugepage_sizes * rte_socket_count();
2269 memtypes = calloc(n_memtypes, sizeof(*memtypes));
2270 if (memtypes == NULL) {
2271 RTE_LOG(ERR, EAL, "Cannot allocate space for memory types\n");
2275 /* populate mem types */
2277 for (hpi_idx = 0; hpi_idx < (int) internal_config.num_hugepage_sizes;
2279 struct hugepage_info *hpi;
2280 uint64_t hugepage_sz;
2282 hpi = &internal_config.hugepage_info[hpi_idx];
2283 hugepage_sz = hpi->hugepage_sz;
2285 for (i = 0; i < (int) rte_socket_count(); i++, cur_type++) {
2286 int socket_id = rte_socket_id_by_idx(i);
2288 #ifndef RTE_EAL_NUMA_AWARE_HUGEPAGES
2292 memtypes[cur_type].page_sz = hugepage_sz;
2293 memtypes[cur_type].socket_id = socket_id;
2295 RTE_LOG(DEBUG, EAL, "Detected memory type: "
2296 "socket_id:%u hugepage_sz:%" PRIu64 "\n",
2297 socket_id, hugepage_sz);
2300 /* number of memtypes could have been lower due to no NUMA support */
2301 n_memtypes = cur_type;
2303 /* set up limits for types */
2304 max_mem = (uint64_t)RTE_MAX_MEM_MB << 20;
2305 max_mem_per_type = RTE_MIN((uint64_t)RTE_MAX_MEM_MB_PER_TYPE << 20,
2306 max_mem / n_memtypes);
2308 * limit maximum number of segment lists per type to ensure there's
2309 * space for memseg lists for all NUMA nodes with all page sizes
2311 max_seglists_per_type = RTE_MAX_MEMSEG_LISTS / n_memtypes;
2313 if (max_seglists_per_type == 0) {
2314 RTE_LOG(ERR, EAL, "Cannot accommodate all memory types, please increase %s\n",
2315 RTE_STR(CONFIG_RTE_MAX_MEMSEG_LISTS));
2319 /* go through all mem types and create segment lists */
2321 for (cur_type = 0; cur_type < n_memtypes; cur_type++) {
2322 unsigned int cur_seglist, n_seglists, n_segs;
2323 unsigned int max_segs_per_type, max_segs_per_list;
2324 struct memtype *type = &memtypes[cur_type];
2325 uint64_t max_mem_per_list, pagesz;
2328 pagesz = type->page_sz;
2329 socket_id = type->socket_id;
2332 * we need to create segment lists for this type. we must take
2333 * into account the following things:
2335 * 1. total amount of memory we can use for this memory type
2336 * 2. total amount of memory per memseg list allowed
2337 * 3. number of segments needed to fit the amount of memory
2338 * 4. number of segments allowed per type
2339 * 5. number of segments allowed per memseg list
2340 * 6. number of memseg lists we are allowed to take up
2343 /* calculate how much segments we will need in total */
2344 max_segs_per_type = max_mem_per_type / pagesz;
2345 /* limit number of segments to maximum allowed per type */
2346 max_segs_per_type = RTE_MIN(max_segs_per_type,
2347 (unsigned int)RTE_MAX_MEMSEG_PER_TYPE);
2348 /* limit number of segments to maximum allowed per list */
2349 max_segs_per_list = RTE_MIN(max_segs_per_type,
2350 (unsigned int)RTE_MAX_MEMSEG_PER_LIST);
2352 /* calculate how much memory we can have per segment list */
2353 max_mem_per_list = RTE_MIN(max_segs_per_list * pagesz,
2354 (uint64_t)RTE_MAX_MEM_MB_PER_LIST << 20);
2356 /* calculate how many segments each segment list will have */
2357 n_segs = RTE_MIN(max_segs_per_list, max_mem_per_list / pagesz);
2359 /* calculate how many segment lists we can have */
2360 n_seglists = RTE_MIN(max_segs_per_type / n_segs,
2361 max_mem_per_type / max_mem_per_list);
2363 /* limit number of segment lists according to our maximum */
2364 n_seglists = RTE_MIN(n_seglists, max_seglists_per_type);
2366 RTE_LOG(DEBUG, EAL, "Creating %i segment lists: "
2367 "n_segs:%i socket_id:%i hugepage_sz:%" PRIu64 "\n",
2368 n_seglists, n_segs, socket_id, pagesz);
2370 /* create all segment lists */
2371 for (cur_seglist = 0; cur_seglist < n_seglists; cur_seglist++) {
2372 if (msl_idx >= RTE_MAX_MEMSEG_LISTS) {
2374 "No more space in memseg lists, please increase %s\n",
2375 RTE_STR(CONFIG_RTE_MAX_MEMSEG_LISTS));
2378 msl = &mcfg->memsegs[msl_idx++];
2380 if (alloc_memseg_list(msl, pagesz, n_segs,
2381 socket_id, cur_seglist))
2384 if (alloc_va_space(msl)) {
2385 RTE_LOG(ERR, EAL, "Cannot allocate VA space for memseg list\n");
2390 /* we're successful */
2398 memseg_secondary_init(void)
2400 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
2402 struct rte_memseg_list *msl;
2404 for (msl_idx = 0; msl_idx < RTE_MAX_MEMSEG_LISTS; msl_idx++) {
2406 msl = &mcfg->memsegs[msl_idx];
2408 /* skip empty memseg lists */
2409 if (msl->memseg_arr.len == 0)
2412 if (rte_fbarray_attach(&msl->memseg_arr)) {
2413 RTE_LOG(ERR, EAL, "Cannot attach to primary process memseg lists\n");
2417 /* preallocate VA space */
2418 if (alloc_va_space(msl)) {
2419 RTE_LOG(ERR, EAL, "Cannot preallocate VA space for hugepage memory\n");
2428 rte_eal_memseg_init(void)
2430 /* increase rlimit to maximum */
2433 if (getrlimit(RLIMIT_NOFILE, &lim) == 0) {
2434 /* set limit to maximum */
2435 lim.rlim_cur = lim.rlim_max;
2437 if (setrlimit(RLIMIT_NOFILE, &lim) < 0) {
2438 RTE_LOG(DEBUG, EAL, "Setting maximum number of open files failed: %s\n",
2441 RTE_LOG(DEBUG, EAL, "Setting maximum number of open files to %"
2443 (uint64_t)lim.rlim_cur);
2446 RTE_LOG(ERR, EAL, "Cannot get current resource limits\n");
2449 return rte_eal_process_type() == RTE_PROC_PRIMARY ?
2451 memseg_primary_init_32() :
2453 memseg_primary_init() :
2455 memseg_secondary_init();