1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2017-2018 Intel Corporation
5 #define _FILE_OFFSET_BITS 64
15 #include <sys/types.h>
17 #include <sys/queue.h>
22 #include <sys/ioctl.h>
26 #ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
30 #include <linux/falloc.h>
31 #include <linux/mman.h> /* for hugetlb-related mmap flags */
33 #include <rte_common.h>
35 #include <rte_eal_memconfig.h>
37 #include <rte_errno.h>
38 #include <rte_memory.h>
39 #include <rte_spinlock.h>
41 #include "eal_filesystem.h"
42 #include "eal_internal_cfg.h"
43 #include "eal_memalloc.h"
44 #include "eal_private.h"
46 const int anonymous_hugepages_supported =
49 #define RTE_MAP_HUGE_SHIFT MAP_HUGE_SHIFT
52 #define RTE_MAP_HUGE_SHIFT 26
56 * not all kernel version support fallocate on hugetlbfs, so fall back to
57 * ftruncate and disallow deallocation if fallocate is not supported.
59 static int fallocate_supported = -1; /* unknown */
62 * we have two modes - single file segments, and file-per-page mode.
64 * for single-file segments, we need some kind of mechanism to keep track of
65 * which hugepages can be freed back to the system, and which cannot. we cannot
66 * use flock() because they don't allow locking parts of a file, and we cannot
67 * use fcntl() due to issues with their semantics, so we will have to rely on a
68 * bunch of lockfiles for each page. so, we will use 'fds' array to keep track
69 * of per-page lockfiles. we will store the actual segment list fd in the
70 * 'memseg_list_fd' field.
72 * for file-per-page mode, each page will have its own fd, so 'memseg_list_fd'
73 * will be invalid (set to -1), and we'll use 'fds' to keep track of page fd's.
75 * we cannot know how many pages a system will have in advance, but we do know
76 * that they come in lists, and we know lengths of these lists. so, simply store
77 * a malloc'd array of fd's indexed by list and segment index.
79 * they will be initialized at startup, and filled as we allocate/deallocate
83 int *fds; /**< dynamically allocated array of segment lock fd's */
84 int memseg_list_fd; /**< memseg list fd */
85 int len; /**< total length of the array */
86 int count; /**< entries used in an array */
87 } fd_list[RTE_MAX_MEMSEG_LISTS];
89 /** local copy of a memory map, used to synchronize memory hotplug in MP */
90 static struct rte_memseg_list local_memsegs[RTE_MAX_MEMSEG_LISTS];
92 static sigjmp_buf huge_jmpenv;
94 static void __rte_unused huge_sigbus_handler(int signo __rte_unused)
96 siglongjmp(huge_jmpenv, 1);
99 /* Put setjmp into a wrap method to avoid compiling error. Any non-volatile,
100 * non-static local variable in the stack frame calling sigsetjmp might be
101 * clobbered by a call to longjmp.
103 static int __rte_unused huge_wrap_sigsetjmp(void)
105 return sigsetjmp(huge_jmpenv, 1);
108 static struct sigaction huge_action_old;
109 static int huge_need_recover;
111 static void __rte_unused
112 huge_register_sigbus(void)
115 struct sigaction action;
118 sigaddset(&mask, SIGBUS);
120 action.sa_mask = mask;
121 action.sa_handler = huge_sigbus_handler;
123 huge_need_recover = !sigaction(SIGBUS, &action, &huge_action_old);
126 static void __rte_unused
127 huge_recover_sigbus(void)
129 if (huge_need_recover) {
130 sigaction(SIGBUS, &huge_action_old, NULL);
131 huge_need_recover = 0;
135 #ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
140 /* Check if kernel supports NUMA. */
141 if (numa_available() != 0) {
142 RTE_LOG(DEBUG, EAL, "NUMA is not supported.\n");
149 prepare_numa(int *oldpolicy, struct bitmask *oldmask, int socket_id)
151 RTE_LOG(DEBUG, EAL, "Trying to obtain current memory policy.\n");
152 if (get_mempolicy(oldpolicy, oldmask->maskp,
153 oldmask->size + 1, 0, 0) < 0) {
155 "Failed to get current mempolicy: %s. "
156 "Assuming MPOL_DEFAULT.\n", strerror(errno));
157 oldpolicy = MPOL_DEFAULT;
160 "Setting policy MPOL_PREFERRED for socket %d\n",
162 numa_set_preferred(socket_id);
166 restore_numa(int *oldpolicy, struct bitmask *oldmask)
169 "Restoring previous memory policy: %d\n", *oldpolicy);
170 if (*oldpolicy == MPOL_DEFAULT) {
171 numa_set_localalloc();
172 } else if (set_mempolicy(*oldpolicy, oldmask->maskp,
173 oldmask->size + 1) < 0) {
174 RTE_LOG(ERR, EAL, "Failed to restore mempolicy: %s\n",
176 numa_set_localalloc();
178 numa_free_cpumask(oldmask);
183 * uses fstat to report the size of a file on disk
186 get_file_size(int fd)
189 if (fstat(fd, &st) < 0)
194 /* returns 1 on successful lock, 0 on unsuccessful lock, -1 on error */
195 static int lock(int fd, int type)
199 /* flock may be interrupted */
201 ret = flock(fd, type | LOCK_NB);
202 } while (ret && errno == EINTR);
204 if (ret && errno == EWOULDBLOCK) {
208 RTE_LOG(ERR, EAL, "%s(): error calling flock(): %s\n",
209 __func__, strerror(errno));
212 /* lock was successful */
216 static int get_segment_lock_fd(int list_idx, int seg_idx)
218 char path[PATH_MAX] = {0};
221 if (list_idx < 0 || list_idx >= (int)RTE_DIM(fd_list))
223 if (seg_idx < 0 || seg_idx >= fd_list[list_idx].len)
226 fd = fd_list[list_idx].fds[seg_idx];
227 /* does this lock already exist? */
231 eal_get_hugefile_lock_path(path, sizeof(path),
232 list_idx * RTE_MAX_MEMSEG_PER_LIST + seg_idx);
234 fd = open(path, O_CREAT | O_RDWR, 0660);
236 RTE_LOG(ERR, EAL, "%s(): error creating lockfile '%s': %s\n",
237 __func__, path, strerror(errno));
240 /* take out a read lock */
241 if (lock(fd, LOCK_SH) != 1) {
242 RTE_LOG(ERR, EAL, "%s(): failed to take out a readlock on '%s': %s\n",
243 __func__, path, strerror(errno));
247 /* store it for future reference */
248 fd_list[list_idx].fds[seg_idx] = fd;
249 fd_list[list_idx].count++;
253 static int unlock_segment(int list_idx, int seg_idx)
257 if (list_idx < 0 || list_idx >= (int)RTE_DIM(fd_list))
259 if (seg_idx < 0 || seg_idx >= fd_list[list_idx].len)
262 fd = fd_list[list_idx].fds[seg_idx];
264 /* upgrade lock to exclusive to see if we can remove the lockfile */
265 ret = lock(fd, LOCK_EX);
267 /* we've succeeded in taking exclusive lock, this lockfile may
270 char path[PATH_MAX] = {0};
271 eal_get_hugefile_lock_path(path, sizeof(path),
272 list_idx * RTE_MAX_MEMSEG_PER_LIST + seg_idx);
274 RTE_LOG(ERR, EAL, "%s(): error removing lockfile '%s': %s\n",
275 __func__, path, strerror(errno));
278 /* we don't want to leak the fd, so even if we fail to lock, close fd
279 * and remove it from list anyway.
282 fd_list[list_idx].fds[seg_idx] = -1;
283 fd_list[list_idx].count--;
291 get_seg_fd(char *path, int buflen, struct hugepage_info *hi,
292 unsigned int list_idx, unsigned int seg_idx)
296 if (internal_config.single_file_segments) {
297 /* create a hugepage file path */
298 eal_get_hugefile_path(path, buflen, hi->hugedir, list_idx);
300 fd = fd_list[list_idx].memseg_list_fd;
303 fd = open(path, O_CREAT | O_RDWR, 0600);
305 RTE_LOG(ERR, EAL, "%s(): open failed: %s\n",
306 __func__, strerror(errno));
309 /* take out a read lock and keep it indefinitely */
310 if (lock(fd, LOCK_SH) < 0) {
311 RTE_LOG(ERR, EAL, "%s(): lock failed: %s\n",
312 __func__, strerror(errno));
316 fd_list[list_idx].memseg_list_fd = fd;
319 /* create a hugepage file path */
320 eal_get_hugefile_path(path, buflen, hi->hugedir,
321 list_idx * RTE_MAX_MEMSEG_PER_LIST + seg_idx);
323 fd = fd_list[list_idx].fds[seg_idx];
326 fd = open(path, O_CREAT | O_RDWR, 0600);
328 RTE_LOG(DEBUG, EAL, "%s(): open failed: %s\n",
329 __func__, strerror(errno));
332 /* take out a read lock */
333 if (lock(fd, LOCK_SH) < 0) {
334 RTE_LOG(ERR, EAL, "%s(): lock failed: %s\n",
335 __func__, strerror(errno));
339 fd_list[list_idx].fds[seg_idx] = fd;
346 resize_hugefile(int fd, char *path, int list_idx, int seg_idx,
347 uint64_t fa_offset, uint64_t page_sz, bool grow)
351 if (fallocate_supported == 0) {
352 /* we cannot deallocate memory if fallocate() is not
353 * supported, and hugepage file is already locked at
354 * creation, so no further synchronization needed.
358 RTE_LOG(DEBUG, EAL, "%s(): fallocate not supported, not freeing page back to the system\n",
362 uint64_t new_size = fa_offset + page_sz;
363 uint64_t cur_size = get_file_size(fd);
365 /* fallocate isn't supported, fall back to ftruncate */
366 if (new_size > cur_size &&
367 ftruncate(fd, new_size) < 0) {
368 RTE_LOG(DEBUG, EAL, "%s(): ftruncate() failed: %s\n",
369 __func__, strerror(errno));
373 int flags = grow ? 0 : FALLOC_FL_PUNCH_HOLE |
377 /* if fallocate() is supported, we need to take out a
378 * read lock on allocate (to prevent other processes
379 * from deallocating this page), and take out a write
380 * lock on deallocate (to ensure nobody else is using
383 * read locks on page itself are already taken out at
384 * file creation, in get_seg_fd().
386 * we cannot rely on simple use of flock() call, because
387 * we need to be able to lock a section of the file,
388 * and we cannot use fcntl() locks, because of numerous
389 * problems with their semantics, so we will use
390 * deterministically named lock files for each section
393 * if we're shrinking the file, we want to upgrade our
394 * lock from shared to exclusive.
396 * lock_fd is an fd for a lockfile, not for the segment
399 lock_fd = get_segment_lock_fd(list_idx, seg_idx);
402 /* we are using this lockfile to determine
403 * whether this particular page is locked, as we
404 * are in single file segments mode and thus
405 * cannot use regular flock() to get this info.
407 * we want to try and take out an exclusive lock
408 * on the lock file to determine if we're the
409 * last ones using this page, and if not, we
410 * won't be shrinking it, and will instead exit
413 ret = lock(lock_fd, LOCK_EX);
415 /* drop the lock on the lockfile, so that even
416 * if we couldn't shrink the file ourselves, we
417 * are signalling to other processes that we're
418 * no longer using this page.
420 if (unlock_segment(list_idx, seg_idx))
421 RTE_LOG(ERR, EAL, "Could not unlock segment\n");
423 /* additionally, if this was the last lock on
424 * this segment list, we can safely close the
425 * page file fd, so that one of the processes
426 * could then delete the file after shrinking.
428 if (ret < 1 && fd_list[list_idx].count == 0) {
430 fd_list[list_idx].memseg_list_fd = -1;
434 RTE_LOG(ERR, EAL, "Could not lock segment\n");
438 /* failed to lock, not an error. */
442 /* grow or shrink the file */
443 ret = fallocate(fd, flags, fa_offset, page_sz);
446 if (fallocate_supported == -1 &&
448 RTE_LOG(ERR, EAL, "%s(): fallocate() not supported, hugepage deallocation will be disabled\n",
451 fallocate_supported = 0;
453 RTE_LOG(DEBUG, EAL, "%s(): fallocate() failed: %s\n",
459 fallocate_supported = 1;
461 /* we've grew/shrunk the file, and we hold an
462 * exclusive lock now. check if there are no
463 * more segments active in this segment list,
464 * and remove the file if there aren't.
466 if (fd_list[list_idx].count == 0) {
468 RTE_LOG(ERR, EAL, "%s(): unlinking '%s' failed: %s\n",
472 fd_list[list_idx].memseg_list_fd = -1;
481 alloc_seg(struct rte_memseg *ms, void *addr, int socket_id,
482 struct hugepage_info *hi, unsigned int list_idx,
483 unsigned int seg_idx)
485 #ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
486 int cur_socket_id = 0;
498 alloc_sz = hi->hugepage_sz;
499 if (!internal_config.single_file_segments &&
500 internal_config.in_memory &&
501 anonymous_hugepages_supported) {
504 log2 = rte_log2_u32(alloc_sz);
505 /* as per mmap() manpage, all page sizes are log2 of page size
506 * shifted by MAP_HUGE_SHIFT
508 flags = (log2 << RTE_MAP_HUGE_SHIFT) | MAP_HUGETLB | MAP_FIXED |
509 MAP_PRIVATE | MAP_ANONYMOUS;
511 va = mmap(addr, alloc_sz, PROT_READ | PROT_WRITE, flags, -1, 0);
513 /* single-file segments codepath will never be active because
514 * in-memory mode is incompatible with it and it's stopped at
515 * EAL initialization stage, however the compiler doesn't know
516 * that and complains about map_offset being used uninitialized
517 * on failure codepaths while having in-memory mode enabled. so,
518 * assign a value here.
522 /* takes out a read lock on segment or segment list */
523 fd = get_seg_fd(path, sizeof(path), hi, list_idx, seg_idx);
525 RTE_LOG(ERR, EAL, "Couldn't get fd on hugepage file\n");
529 if (internal_config.single_file_segments) {
530 map_offset = seg_idx * alloc_sz;
531 ret = resize_hugefile(fd, path, list_idx, seg_idx,
532 map_offset, alloc_sz, true);
537 if (ftruncate(fd, alloc_sz) < 0) {
538 RTE_LOG(DEBUG, EAL, "%s(): ftruncate() failed: %s\n",
539 __func__, strerror(errno));
542 if (internal_config.hugepage_unlink) {
544 RTE_LOG(DEBUG, EAL, "%s(): unlink() failed: %s\n",
545 __func__, strerror(errno));
552 * map the segment, and populate page tables, the kernel fills
553 * this segment with zeros if it's a new page.
555 va = mmap(addr, alloc_sz, PROT_READ | PROT_WRITE,
556 MAP_SHARED | MAP_POPULATE | MAP_FIXED, fd,
560 if (va == MAP_FAILED) {
561 RTE_LOG(DEBUG, EAL, "%s(): mmap() failed: %s\n", __func__,
563 /* mmap failed, but the previous region might have been
564 * unmapped anyway. try to remap it
569 RTE_LOG(DEBUG, EAL, "%s(): wrong mmap() address\n", __func__);
570 munmap(va, alloc_sz);
574 /* In linux, hugetlb limitations, like cgroup, are
575 * enforced at fault time instead of mmap(), even
576 * with the option of MAP_POPULATE. Kernel will send
577 * a SIGBUS signal. To avoid to be killed, save stack
578 * environment here, if SIGBUS happens, we can jump
581 if (huge_wrap_sigsetjmp()) {
582 RTE_LOG(DEBUG, EAL, "SIGBUS: Cannot mmap more hugepages of size %uMB\n",
583 (unsigned int)(alloc_sz >> 20));
587 /* we need to trigger a write to the page to enforce page fault and
588 * ensure that page is accessible to us, but we can't overwrite value
589 * that is already there, so read the old value, and write itback.
590 * kernel populates the page with zeroes initially.
592 *(volatile int *)addr = *(volatile int *)addr;
594 iova = rte_mem_virt2iova(addr);
595 if (iova == RTE_BAD_PHYS_ADDR) {
596 RTE_LOG(DEBUG, EAL, "%s(): can't get IOVA addr\n",
601 #ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
602 move_pages(getpid(), 1, &addr, NULL, &cur_socket_id, 0);
604 if (cur_socket_id != socket_id) {
606 "%s(): allocation happened on wrong socket (wanted %d, got %d)\n",
607 __func__, socket_id, cur_socket_id);
613 ms->hugepage_sz = alloc_sz;
615 ms->nchannel = rte_memory_get_nchannel();
616 ms->nrank = rte_memory_get_nrank();
618 ms->socket_id = socket_id;
623 munmap(addr, alloc_sz);
626 #ifdef RTE_ARCH_PPC_64
627 flags |= MAP_HUGETLB;
629 new_addr = eal_get_virtual_area(addr, &alloc_sz, alloc_sz, 0, flags);
630 if (new_addr != addr) {
631 if (new_addr != NULL)
632 munmap(new_addr, alloc_sz);
633 /* we're leaving a hole in our virtual address space. if
634 * somebody else maps this hole now, we could accidentally
635 * override it in the future.
637 RTE_LOG(CRIT, EAL, "Can't mmap holes in our virtual address space\n");
640 /* some codepaths will return negative fd, so exit early */
644 if (internal_config.single_file_segments) {
645 resize_hugefile(fd, path, list_idx, seg_idx, map_offset,
647 /* ignore failure, can't make it any worse */
649 /* only remove file if we can take out a write lock */
650 if (internal_config.hugepage_unlink == 0 &&
651 internal_config.in_memory == 0 &&
652 lock(fd, LOCK_EX) == 1)
655 fd_list[list_idx].fds[seg_idx] = -1;
661 free_seg(struct rte_memseg *ms, struct hugepage_info *hi,
662 unsigned int list_idx, unsigned int seg_idx)
668 /* erase page data */
669 memset(ms->addr, 0, ms->len);
671 if (mmap(ms->addr, ms->len, PROT_READ,
672 MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0) ==
674 RTE_LOG(DEBUG, EAL, "couldn't unmap page\n");
678 /* if we've already unlinked the page, nothing needs to be done */
679 if (internal_config.hugepage_unlink) {
680 memset(ms, 0, sizeof(*ms));
684 /* if we are not in single file segments mode, we're going to unmap the
685 * segment and thus drop the lock on original fd, but hugepage dir is
686 * now locked so we can take out another one without races.
688 fd = get_seg_fd(path, sizeof(path), hi, list_idx, seg_idx);
692 if (internal_config.single_file_segments) {
693 map_offset = seg_idx * ms->len;
694 if (resize_hugefile(fd, path, list_idx, seg_idx, map_offset,
699 /* if we're able to take out a write lock, we're the last one
700 * holding onto this page.
702 ret = lock(fd, LOCK_EX);
704 /* no one else is using this page */
708 /* closing fd will drop the lock */
710 fd_list[list_idx].fds[seg_idx] = -1;
713 memset(ms, 0, sizeof(*ms));
715 return ret < 0 ? -1 : 0;
718 struct alloc_walk_param {
719 struct hugepage_info *hi;
720 struct rte_memseg **ms;
722 unsigned int segs_allocated;
728 alloc_seg_walk(const struct rte_memseg_list *msl, void *arg)
730 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
731 struct alloc_walk_param *wa = arg;
732 struct rte_memseg_list *cur_msl;
734 int cur_idx, start_idx, j, dir_fd = -1;
735 unsigned int msl_idx, need, i;
737 if (msl->page_sz != wa->page_sz)
739 if (msl->socket_id != wa->socket)
742 page_sz = (size_t)msl->page_sz;
744 msl_idx = msl - mcfg->memsegs;
745 cur_msl = &mcfg->memsegs[msl_idx];
749 /* try finding space in memseg list */
750 cur_idx = rte_fbarray_find_next_n_free(&cur_msl->memseg_arr, 0, need);
755 /* do not allow any page allocations during the time we're allocating,
756 * because file creation and locking operations are not atomic,
757 * and we might be the first or the last ones to use a particular page,
758 * so we need to ensure atomicity of every operation.
760 * during init, we already hold a write lock, so don't try to take out
763 if (wa->hi->lock_descriptor == -1 && !internal_config.in_memory) {
764 dir_fd = open(wa->hi->hugedir, O_RDONLY);
766 RTE_LOG(ERR, EAL, "%s(): Cannot open '%s': %s\n",
767 __func__, wa->hi->hugedir, strerror(errno));
770 /* blocking writelock */
771 if (flock(dir_fd, LOCK_EX)) {
772 RTE_LOG(ERR, EAL, "%s(): Cannot lock '%s': %s\n",
773 __func__, wa->hi->hugedir, strerror(errno));
779 for (i = 0; i < need; i++, cur_idx++) {
780 struct rte_memseg *cur;
783 cur = rte_fbarray_get(&cur_msl->memseg_arr, cur_idx);
784 map_addr = RTE_PTR_ADD(cur_msl->base_va,
787 if (alloc_seg(cur, map_addr, wa->socket, wa->hi,
789 RTE_LOG(DEBUG, EAL, "attempted to allocate %i segments, but only %i were allocated\n",
792 /* if exact number wasn't requested, stop */
797 for (j = start_idx; j < cur_idx; j++) {
798 struct rte_memseg *tmp;
799 struct rte_fbarray *arr =
800 &cur_msl->memseg_arr;
802 tmp = rte_fbarray_get(arr, j);
803 rte_fbarray_set_free(arr, j);
805 /* free_seg may attempt to create a file, which
808 if (free_seg(tmp, wa->hi, msl_idx, j))
809 RTE_LOG(DEBUG, EAL, "Cannot free page\n");
813 memset(wa->ms, 0, sizeof(*wa->ms) * wa->n_segs);
822 rte_fbarray_set_used(&cur_msl->memseg_arr, cur_idx);
825 wa->segs_allocated = i;
833 struct free_walk_param {
834 struct hugepage_info *hi;
835 struct rte_memseg *ms;
838 free_seg_walk(const struct rte_memseg_list *msl, void *arg)
840 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
841 struct rte_memseg_list *found_msl;
842 struct free_walk_param *wa = arg;
843 uintptr_t start_addr, end_addr;
844 int msl_idx, seg_idx, ret, dir_fd = -1;
846 start_addr = (uintptr_t) msl->base_va;
847 end_addr = start_addr + msl->memseg_arr.len * (size_t)msl->page_sz;
849 if ((uintptr_t)wa->ms->addr < start_addr ||
850 (uintptr_t)wa->ms->addr >= end_addr)
853 msl_idx = msl - mcfg->memsegs;
854 seg_idx = RTE_PTR_DIFF(wa->ms->addr, start_addr) / msl->page_sz;
857 found_msl = &mcfg->memsegs[msl_idx];
859 /* do not allow any page allocations during the time we're freeing,
860 * because file creation and locking operations are not atomic,
861 * and we might be the first or the last ones to use a particular page,
862 * so we need to ensure atomicity of every operation.
864 * during init, we already hold a write lock, so don't try to take out
867 if (wa->hi->lock_descriptor == -1 && !internal_config.in_memory) {
868 dir_fd = open(wa->hi->hugedir, O_RDONLY);
870 RTE_LOG(ERR, EAL, "%s(): Cannot open '%s': %s\n",
871 __func__, wa->hi->hugedir, strerror(errno));
874 /* blocking writelock */
875 if (flock(dir_fd, LOCK_EX)) {
876 RTE_LOG(ERR, EAL, "%s(): Cannot lock '%s': %s\n",
877 __func__, wa->hi->hugedir, strerror(errno));
883 found_msl->version++;
885 rte_fbarray_set_free(&found_msl->memseg_arr, seg_idx);
887 ret = free_seg(wa->ms, wa->hi, msl_idx, seg_idx);
899 eal_memalloc_alloc_seg_bulk(struct rte_memseg **ms, int n_segs, size_t page_sz,
900 int socket, bool exact)
903 #ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
904 bool have_numa = false;
906 struct bitmask *oldmask;
908 struct alloc_walk_param wa;
909 struct hugepage_info *hi = NULL;
911 memset(&wa, 0, sizeof(wa));
913 /* dynamic allocation not supported in legacy mode */
914 if (internal_config.legacy_mem)
917 for (i = 0; i < (int) RTE_DIM(internal_config.hugepage_info); i++) {
919 internal_config.hugepage_info[i].hugepage_sz) {
920 hi = &internal_config.hugepage_info[i];
925 RTE_LOG(ERR, EAL, "%s(): can't find relevant hugepage_info entry\n",
930 #ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
932 oldmask = numa_allocate_nodemask();
933 prepare_numa(&oldpolicy, oldmask, socket);
942 wa.page_sz = page_sz;
944 wa.segs_allocated = 0;
946 /* memalloc is locked, so it's safe to use thread-unsafe version */
947 ret = rte_memseg_list_walk_thread_unsafe(alloc_seg_walk, &wa);
949 RTE_LOG(ERR, EAL, "%s(): couldn't find suitable memseg_list\n",
952 } else if (ret > 0) {
953 ret = (int)wa.segs_allocated;
956 #ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
958 restore_numa(&oldpolicy, oldmask);
964 eal_memalloc_alloc_seg(size_t page_sz, int socket)
966 struct rte_memseg *ms;
967 if (eal_memalloc_alloc_seg_bulk(&ms, 1, page_sz, socket, true) < 0)
969 /* return pointer to newly allocated memseg */
974 eal_memalloc_free_seg_bulk(struct rte_memseg **ms, int n_segs)
978 /* dynamic free not supported in legacy mode */
979 if (internal_config.legacy_mem)
982 for (seg = 0; seg < n_segs; seg++) {
983 struct rte_memseg *cur = ms[seg];
984 struct hugepage_info *hi = NULL;
985 struct free_walk_param wa;
988 /* if this page is marked as unfreeable, fail */
989 if (cur->flags & RTE_MEMSEG_FLAG_DO_NOT_FREE) {
990 RTE_LOG(DEBUG, EAL, "Page is not allowed to be freed\n");
995 memset(&wa, 0, sizeof(wa));
997 for (i = 0; i < (int)RTE_DIM(internal_config.hugepage_info);
999 hi = &internal_config.hugepage_info[i];
1000 if (cur->hugepage_sz == hi->hugepage_sz)
1003 if (i == (int)RTE_DIM(internal_config.hugepage_info)) {
1004 RTE_LOG(ERR, EAL, "Can't find relevant hugepage_info entry\n");
1012 /* memalloc is locked, so it's safe to use thread-unsafe version
1014 walk_res = rte_memseg_list_walk_thread_unsafe(free_seg_walk,
1019 RTE_LOG(ERR, EAL, "Couldn't find memseg list\n");
1026 eal_memalloc_free_seg(struct rte_memseg *ms)
1028 /* dynamic free not supported in legacy mode */
1029 if (internal_config.legacy_mem)
1032 return eal_memalloc_free_seg_bulk(&ms, 1);
1036 sync_chunk(struct rte_memseg_list *primary_msl,
1037 struct rte_memseg_list *local_msl, struct hugepage_info *hi,
1038 unsigned int msl_idx, bool used, int start, int end)
1040 struct rte_fbarray *l_arr, *p_arr;
1041 int i, ret, chunk_len, diff_len;
1043 l_arr = &local_msl->memseg_arr;
1044 p_arr = &primary_msl->memseg_arr;
1046 /* we need to aggregate allocations/deallocations into bigger chunks,
1047 * as we don't want to spam the user with per-page callbacks.
1049 * to avoid any potential issues, we also want to trigger
1050 * deallocation callbacks *before* we actually deallocate
1051 * memory, so that the user application could wrap up its use
1052 * before it goes away.
1055 chunk_len = end - start;
1057 /* find how many contiguous pages we can map/unmap for this chunk */
1059 rte_fbarray_find_contig_free(l_arr, start) :
1060 rte_fbarray_find_contig_used(l_arr, start);
1062 /* has to be at least one page */
1066 diff_len = RTE_MIN(chunk_len, diff_len);
1068 /* if we are freeing memory, notify the application */
1070 struct rte_memseg *ms;
1072 size_t len, page_sz;
1074 ms = rte_fbarray_get(l_arr, start);
1075 start_va = ms->addr;
1076 page_sz = (size_t)primary_msl->page_sz;
1077 len = page_sz * diff_len;
1079 eal_memalloc_mem_event_notify(RTE_MEM_EVENT_FREE,
1083 for (i = 0; i < diff_len; i++) {
1084 struct rte_memseg *p_ms, *l_ms;
1085 int seg_idx = start + i;
1087 l_ms = rte_fbarray_get(l_arr, seg_idx);
1088 p_ms = rte_fbarray_get(p_arr, seg_idx);
1090 if (l_ms == NULL || p_ms == NULL)
1094 ret = alloc_seg(l_ms, p_ms->addr,
1095 p_ms->socket_id, hi,
1099 rte_fbarray_set_used(l_arr, seg_idx);
1101 ret = free_seg(l_ms, hi, msl_idx, seg_idx);
1102 rte_fbarray_set_free(l_arr, seg_idx);
1108 /* if we just allocated memory, notify the application */
1110 struct rte_memseg *ms;
1112 size_t len, page_sz;
1114 ms = rte_fbarray_get(l_arr, start);
1115 start_va = ms->addr;
1116 page_sz = (size_t)primary_msl->page_sz;
1117 len = page_sz * diff_len;
1119 eal_memalloc_mem_event_notify(RTE_MEM_EVENT_ALLOC,
1123 /* calculate how much we can advance until next chunk */
1125 rte_fbarray_find_contig_used(l_arr, start) :
1126 rte_fbarray_find_contig_free(l_arr, start);
1127 ret = RTE_MIN(chunk_len, diff_len);
1133 sync_status(struct rte_memseg_list *primary_msl,
1134 struct rte_memseg_list *local_msl, struct hugepage_info *hi,
1135 unsigned int msl_idx, bool used)
1137 struct rte_fbarray *l_arr, *p_arr;
1138 int p_idx, l_chunk_len, p_chunk_len, ret;
1141 /* this is a little bit tricky, but the basic idea is - walk both lists
1142 * and spot any places where there are discrepancies. walking both lists
1143 * and noting discrepancies in a single go is a hard problem, so we do
1144 * it in two passes - first we spot any places where allocated segments
1145 * mismatch (i.e. ensure that everything that's allocated in the primary
1146 * is also allocated in the secondary), and then we do it by looking at
1147 * free segments instead.
1149 * we also need to aggregate changes into chunks, as we have to call
1150 * callbacks per allocation, not per page.
1152 l_arr = &local_msl->memseg_arr;
1153 p_arr = &primary_msl->memseg_arr;
1156 p_idx = rte_fbarray_find_next_used(p_arr, 0);
1158 p_idx = rte_fbarray_find_next_free(p_arr, 0);
1160 while (p_idx >= 0) {
1161 int next_chunk_search_idx;
1164 p_chunk_len = rte_fbarray_find_contig_used(p_arr,
1166 l_chunk_len = rte_fbarray_find_contig_used(l_arr,
1169 p_chunk_len = rte_fbarray_find_contig_free(p_arr,
1171 l_chunk_len = rte_fbarray_find_contig_free(l_arr,
1174 /* best case scenario - no differences (or bigger, which will be
1175 * fixed during next iteration), look for next chunk
1177 if (l_chunk_len >= p_chunk_len) {
1178 next_chunk_search_idx = p_idx + p_chunk_len;
1182 /* if both chunks start at the same point, skip parts we know
1183 * are identical, and sync the rest. each call to sync_chunk
1184 * will only sync contiguous segments, so we need to call this
1185 * until we are sure there are no more differences in this
1188 start = p_idx + l_chunk_len;
1189 end = p_idx + p_chunk_len;
1191 ret = sync_chunk(primary_msl, local_msl, hi, msl_idx,
1194 } while (start < end && ret >= 0);
1195 /* if ret is negative, something went wrong */
1199 next_chunk_search_idx = p_idx + p_chunk_len;
1201 /* skip to end of this chunk */
1203 p_idx = rte_fbarray_find_next_used(p_arr,
1204 next_chunk_search_idx);
1206 p_idx = rte_fbarray_find_next_free(p_arr,
1207 next_chunk_search_idx);
1214 sync_existing(struct rte_memseg_list *primary_msl,
1215 struct rte_memseg_list *local_msl, struct hugepage_info *hi,
1216 unsigned int msl_idx)
1220 /* do not allow any page allocations during the time we're allocating,
1221 * because file creation and locking operations are not atomic,
1222 * and we might be the first or the last ones to use a particular page,
1223 * so we need to ensure atomicity of every operation.
1225 dir_fd = open(hi->hugedir, O_RDONLY);
1227 RTE_LOG(ERR, EAL, "%s(): Cannot open '%s': %s\n", __func__,
1228 hi->hugedir, strerror(errno));
1231 /* blocking writelock */
1232 if (flock(dir_fd, LOCK_EX)) {
1233 RTE_LOG(ERR, EAL, "%s(): Cannot lock '%s': %s\n", __func__,
1234 hi->hugedir, strerror(errno));
1239 /* ensure all allocated space is the same in both lists */
1240 ret = sync_status(primary_msl, local_msl, hi, msl_idx, true);
1244 /* ensure all unallocated space is the same in both lists */
1245 ret = sync_status(primary_msl, local_msl, hi, msl_idx, false);
1249 /* update version number */
1250 local_msl->version = primary_msl->version;
1261 sync_walk(const struct rte_memseg_list *msl, void *arg __rte_unused)
1263 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
1264 struct rte_memseg_list *primary_msl, *local_msl;
1265 struct hugepage_info *hi = NULL;
1269 msl_idx = msl - mcfg->memsegs;
1270 primary_msl = &mcfg->memsegs[msl_idx];
1271 local_msl = &local_memsegs[msl_idx];
1273 for (i = 0; i < RTE_DIM(internal_config.hugepage_info); i++) {
1275 internal_config.hugepage_info[i].hugepage_sz;
1276 uint64_t msl_sz = primary_msl->page_sz;
1277 if (msl_sz == cur_sz) {
1278 hi = &internal_config.hugepage_info[i];
1283 RTE_LOG(ERR, EAL, "Can't find relevant hugepage_info entry\n");
1287 /* if versions don't match, synchronize everything */
1288 if (local_msl->version != primary_msl->version &&
1289 sync_existing(primary_msl, local_msl, hi, msl_idx))
1296 eal_memalloc_sync_with_primary(void)
1298 /* nothing to be done in primary */
1299 if (rte_eal_process_type() == RTE_PROC_PRIMARY)
1302 /* memalloc is locked, so it's safe to call thread-unsafe version */
1303 if (rte_memseg_list_walk_thread_unsafe(sync_walk, NULL))
1309 secondary_msl_create_walk(const struct rte_memseg_list *msl,
1310 void *arg __rte_unused)
1312 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
1313 struct rte_memseg_list *primary_msl, *local_msl;
1314 char name[PATH_MAX];
1317 msl_idx = msl - mcfg->memsegs;
1318 primary_msl = &mcfg->memsegs[msl_idx];
1319 local_msl = &local_memsegs[msl_idx];
1321 /* create distinct fbarrays for each secondary */
1322 snprintf(name, RTE_FBARRAY_NAME_LEN, "%s_%i",
1323 primary_msl->memseg_arr.name, getpid());
1325 ret = rte_fbarray_init(&local_msl->memseg_arr, name,
1326 primary_msl->memseg_arr.len,
1327 primary_msl->memseg_arr.elt_sz);
1329 RTE_LOG(ERR, EAL, "Cannot initialize local memory map\n");
1332 local_msl->base_va = primary_msl->base_va;
1338 alloc_list(int list_idx, int len)
1343 /* ensure we have space to store fd per each possible segment */
1344 data = malloc(sizeof(int) * len);
1346 RTE_LOG(ERR, EAL, "Unable to allocate space for file descriptors\n");
1349 /* set all fd's as invalid */
1350 for (i = 0; i < len; i++)
1353 fd_list[list_idx].fds = data;
1354 fd_list[list_idx].len = len;
1355 fd_list[list_idx].count = 0;
1356 fd_list[list_idx].memseg_list_fd = -1;
1362 fd_list_create_walk(const struct rte_memseg_list *msl,
1363 void *arg __rte_unused)
1365 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
1369 msl_idx = msl - mcfg->memsegs;
1370 len = msl->memseg_arr.len;
1372 return alloc_list(msl_idx, len);
1376 eal_memalloc_set_seg_fd(int list_idx, int seg_idx, int fd)
1378 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
1380 /* if list is not allocated, allocate it */
1381 if (fd_list[list_idx].len == 0) {
1382 int len = mcfg->memsegs[list_idx].memseg_arr.len;
1384 if (alloc_list(list_idx, len) < 0)
1387 fd_list[list_idx].fds[seg_idx] = fd;
1393 eal_memalloc_get_seg_fd(int list_idx, int seg_idx)
1396 if (internal_config.single_file_segments) {
1397 fd = fd_list[list_idx].memseg_list_fd;
1398 } else if (fd_list[list_idx].len == 0) {
1399 /* list not initialized */
1402 fd = fd_list[list_idx].fds[seg_idx];
1410 eal_memalloc_init(void)
1412 if (rte_eal_process_type() == RTE_PROC_SECONDARY)
1413 if (rte_memseg_list_walk(secondary_msl_create_walk, NULL) < 0)
1416 /* initialize all of the fd lists */
1417 if (rte_memseg_list_walk(fd_list_create_walk, NULL))