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_memory.h>
38 #include <rte_spinlock.h>
40 #include "eal_filesystem.h"
41 #include "eal_internal_cfg.h"
42 #include "eal_memalloc.h"
43 #include "eal_private.h"
45 const int anonymous_hugepages_supported =
48 #define RTE_MAP_HUGE_SHIFT MAP_HUGE_SHIFT
51 #define RTE_MAP_HUGE_SHIFT 26
55 * not all kernel version support fallocate on hugetlbfs, so fall back to
56 * ftruncate and disallow deallocation if fallocate is not supported.
58 static int fallocate_supported = -1; /* unknown */
61 * we have two modes - single file segments, and file-per-page mode.
63 * for single-file segments, we need some kind of mechanism to keep track of
64 * which hugepages can be freed back to the system, and which cannot. we cannot
65 * use flock() because they don't allow locking parts of a file, and we cannot
66 * use fcntl() due to issues with their semantics, so we will have to rely on a
67 * bunch of lockfiles for each page. so, we will use 'fds' array to keep track
68 * of per-page lockfiles. we will store the actual segment list fd in the
69 * 'memseg_list_fd' field.
71 * for file-per-page mode, each page will have its own fd, so 'memseg_list_fd'
72 * will be invalid (set to -1), and we'll use 'fds' to keep track of page fd's.
74 * we cannot know how many pages a system will have in advance, but we do know
75 * that they come in lists, and we know lengths of these lists. so, simply store
76 * a malloc'd array of fd's indexed by list and segment index.
78 * they will be initialized at startup, and filled as we allocate/deallocate
82 int *fds; /**< dynamically allocated array of segment lock fd's */
83 int memseg_list_fd; /**< memseg list fd */
84 int len; /**< total length of the array */
85 int count; /**< entries used in an array */
86 } fd_list[RTE_MAX_MEMSEG_LISTS];
88 /** local copy of a memory map, used to synchronize memory hotplug in MP */
89 static struct rte_memseg_list local_memsegs[RTE_MAX_MEMSEG_LISTS];
91 static sigjmp_buf huge_jmpenv;
93 static void __rte_unused huge_sigbus_handler(int signo __rte_unused)
95 siglongjmp(huge_jmpenv, 1);
98 /* Put setjmp into a wrap method to avoid compiling error. Any non-volatile,
99 * non-static local variable in the stack frame calling sigsetjmp might be
100 * clobbered by a call to longjmp.
102 static int __rte_unused huge_wrap_sigsetjmp(void)
104 return sigsetjmp(huge_jmpenv, 1);
107 static struct sigaction huge_action_old;
108 static int huge_need_recover;
110 static void __rte_unused
111 huge_register_sigbus(void)
114 struct sigaction action;
117 sigaddset(&mask, SIGBUS);
119 action.sa_mask = mask;
120 action.sa_handler = huge_sigbus_handler;
122 huge_need_recover = !sigaction(SIGBUS, &action, &huge_action_old);
125 static void __rte_unused
126 huge_recover_sigbus(void)
128 if (huge_need_recover) {
129 sigaction(SIGBUS, &huge_action_old, NULL);
130 huge_need_recover = 0;
134 #ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
139 /* Check if kernel supports NUMA. */
140 if (numa_available() != 0) {
141 RTE_LOG(DEBUG, EAL, "NUMA is not supported.\n");
148 prepare_numa(int *oldpolicy, struct bitmask *oldmask, int socket_id)
150 RTE_LOG(DEBUG, EAL, "Trying to obtain current memory policy.\n");
151 if (get_mempolicy(oldpolicy, oldmask->maskp,
152 oldmask->size + 1, 0, 0) < 0) {
154 "Failed to get current mempolicy: %s. "
155 "Assuming MPOL_DEFAULT.\n", strerror(errno));
156 oldpolicy = MPOL_DEFAULT;
159 "Setting policy MPOL_PREFERRED for socket %d\n",
161 numa_set_preferred(socket_id);
165 restore_numa(int *oldpolicy, struct bitmask *oldmask)
168 "Restoring previous memory policy: %d\n", *oldpolicy);
169 if (*oldpolicy == MPOL_DEFAULT) {
170 numa_set_localalloc();
171 } else if (set_mempolicy(*oldpolicy, oldmask->maskp,
172 oldmask->size + 1) < 0) {
173 RTE_LOG(ERR, EAL, "Failed to restore mempolicy: %s\n",
175 numa_set_localalloc();
177 numa_free_cpumask(oldmask);
182 * uses fstat to report the size of a file on disk
185 get_file_size(int fd)
188 if (fstat(fd, &st) < 0)
193 /* returns 1 on successful lock, 0 on unsuccessful lock, -1 on error */
194 static int lock(int fd, int type)
198 /* flock may be interrupted */
200 ret = flock(fd, type | LOCK_NB);
201 } while (ret && errno == EINTR);
203 if (ret && errno == EWOULDBLOCK) {
207 RTE_LOG(ERR, EAL, "%s(): error calling flock(): %s\n",
208 __func__, strerror(errno));
211 /* lock was successful */
215 static int get_segment_lock_fd(int list_idx, int seg_idx)
217 char path[PATH_MAX] = {0};
220 if (list_idx < 0 || list_idx >= (int)RTE_DIM(fd_list))
222 if (seg_idx < 0 || seg_idx >= fd_list[list_idx].len)
225 fd = fd_list[list_idx].fds[seg_idx];
226 /* does this lock already exist? */
230 eal_get_hugefile_lock_path(path, sizeof(path),
231 list_idx * RTE_MAX_MEMSEG_PER_LIST + seg_idx);
233 fd = open(path, O_CREAT | O_RDWR, 0660);
235 RTE_LOG(ERR, EAL, "%s(): error creating lockfile '%s': %s\n",
236 __func__, path, strerror(errno));
239 /* take out a read lock */
240 if (lock(fd, LOCK_SH) != 1) {
241 RTE_LOG(ERR, EAL, "%s(): failed to take out a readlock on '%s': %s\n",
242 __func__, path, strerror(errno));
246 /* store it for future reference */
247 fd_list[list_idx].fds[seg_idx] = fd;
248 fd_list[list_idx].count++;
252 static int unlock_segment(int list_idx, int seg_idx)
256 if (list_idx < 0 || list_idx >= (int)RTE_DIM(fd_list))
258 if (seg_idx < 0 || seg_idx >= fd_list[list_idx].len)
261 fd = fd_list[list_idx].fds[seg_idx];
263 /* upgrade lock to exclusive to see if we can remove the lockfile */
264 ret = lock(fd, LOCK_EX);
266 /* we've succeeded in taking exclusive lock, this lockfile may
269 char path[PATH_MAX] = {0};
270 eal_get_hugefile_lock_path(path, sizeof(path),
271 list_idx * RTE_MAX_MEMSEG_PER_LIST + seg_idx);
273 RTE_LOG(ERR, EAL, "%s(): error removing lockfile '%s': %s\n",
274 __func__, path, strerror(errno));
277 /* we don't want to leak the fd, so even if we fail to lock, close fd
278 * and remove it from list anyway.
281 fd_list[list_idx].fds[seg_idx] = -1;
282 fd_list[list_idx].count--;
290 get_seg_fd(char *path, int buflen, struct hugepage_info *hi,
291 unsigned int list_idx, unsigned int seg_idx)
295 if (internal_config.single_file_segments) {
296 /* create a hugepage file path */
297 eal_get_hugefile_path(path, buflen, hi->hugedir, list_idx);
299 fd = fd_list[list_idx].memseg_list_fd;
302 fd = open(path, O_CREAT | O_RDWR, 0600);
304 RTE_LOG(ERR, EAL, "%s(): open failed: %s\n",
305 __func__, strerror(errno));
308 /* take out a read lock and keep it indefinitely */
309 if (lock(fd, LOCK_SH) < 0) {
310 RTE_LOG(ERR, EAL, "%s(): lock failed: %s\n",
311 __func__, strerror(errno));
315 fd_list[list_idx].memseg_list_fd = fd;
318 /* create a hugepage file path */
319 eal_get_hugefile_path(path, buflen, hi->hugedir,
320 list_idx * RTE_MAX_MEMSEG_PER_LIST + seg_idx);
321 fd = open(path, O_CREAT | O_RDWR, 0600);
323 RTE_LOG(DEBUG, EAL, "%s(): open failed: %s\n", __func__,
327 /* take out a read lock */
328 if (lock(fd, LOCK_SH) < 0) {
329 RTE_LOG(ERR, EAL, "%s(): lock failed: %s\n",
330 __func__, strerror(errno));
339 resize_hugefile(int fd, char *path, int list_idx, int seg_idx,
340 uint64_t fa_offset, uint64_t page_sz, bool grow)
344 if (fallocate_supported == 0) {
345 /* we cannot deallocate memory if fallocate() is not
346 * supported, and hugepage file is already locked at
347 * creation, so no further synchronization needed.
351 RTE_LOG(DEBUG, EAL, "%s(): fallocate not supported, not freeing page back to the system\n",
355 uint64_t new_size = fa_offset + page_sz;
356 uint64_t cur_size = get_file_size(fd);
358 /* fallocate isn't supported, fall back to ftruncate */
359 if (new_size > cur_size &&
360 ftruncate(fd, new_size) < 0) {
361 RTE_LOG(DEBUG, EAL, "%s(): ftruncate() failed: %s\n",
362 __func__, strerror(errno));
366 int flags = grow ? 0 : FALLOC_FL_PUNCH_HOLE |
370 /* if fallocate() is supported, we need to take out a
371 * read lock on allocate (to prevent other processes
372 * from deallocating this page), and take out a write
373 * lock on deallocate (to ensure nobody else is using
376 * read locks on page itself are already taken out at
377 * file creation, in get_seg_fd().
379 * we cannot rely on simple use of flock() call, because
380 * we need to be able to lock a section of the file,
381 * and we cannot use fcntl() locks, because of numerous
382 * problems with their semantics, so we will use
383 * deterministically named lock files for each section
386 * if we're shrinking the file, we want to upgrade our
387 * lock from shared to exclusive.
389 * lock_fd is an fd for a lockfile, not for the segment
392 lock_fd = get_segment_lock_fd(list_idx, seg_idx);
395 /* we are using this lockfile to determine
396 * whether this particular page is locked, as we
397 * are in single file segments mode and thus
398 * cannot use regular flock() to get this info.
400 * we want to try and take out an exclusive lock
401 * on the lock file to determine if we're the
402 * last ones using this page, and if not, we
403 * won't be shrinking it, and will instead exit
406 ret = lock(lock_fd, LOCK_EX);
408 /* drop the lock on the lockfile, so that even
409 * if we couldn't shrink the file ourselves, we
410 * are signalling to other processes that we're
411 * no longer using this page.
413 if (unlock_segment(list_idx, seg_idx))
414 RTE_LOG(ERR, EAL, "Could not unlock segment\n");
416 /* additionally, if this was the last lock on
417 * this segment list, we can safely close the
418 * page file fd, so that one of the processes
419 * could then delete the file after shrinking.
421 if (ret < 1 && fd_list[list_idx].count == 0) {
423 fd_list[list_idx].memseg_list_fd = -1;
427 RTE_LOG(ERR, EAL, "Could not lock segment\n");
431 /* failed to lock, not an error. */
435 /* grow or shrink the file */
436 ret = fallocate(fd, flags, fa_offset, page_sz);
439 if (fallocate_supported == -1 &&
441 RTE_LOG(ERR, EAL, "%s(): fallocate() not supported, hugepage deallocation will be disabled\n",
444 fallocate_supported = 0;
446 RTE_LOG(DEBUG, EAL, "%s(): fallocate() failed: %s\n",
452 fallocate_supported = 1;
454 /* we've grew/shrunk the file, and we hold an
455 * exclusive lock now. check if there are no
456 * more segments active in this segment list,
457 * and remove the file if there aren't.
459 if (fd_list[list_idx].count == 0) {
461 RTE_LOG(ERR, EAL, "%s(): unlinking '%s' failed: %s\n",
465 fd_list[list_idx].memseg_list_fd = -1;
474 alloc_seg(struct rte_memseg *ms, void *addr, int socket_id,
475 struct hugepage_info *hi, unsigned int list_idx,
476 unsigned int seg_idx)
478 #ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
479 int cur_socket_id = 0;
491 alloc_sz = hi->hugepage_sz;
492 if (!internal_config.single_file_segments &&
493 internal_config.in_memory &&
494 anonymous_hugepages_supported) {
497 log2 = rte_log2_u32(alloc_sz);
498 /* as per mmap() manpage, all page sizes are log2 of page size
499 * shifted by MAP_HUGE_SHIFT
501 flags = (log2 << RTE_MAP_HUGE_SHIFT) | MAP_HUGETLB | MAP_FIXED |
502 MAP_PRIVATE | MAP_ANONYMOUS;
504 va = mmap(addr, alloc_sz, PROT_READ | PROT_WRITE, flags, -1, 0);
506 /* single-file segments codepath will never be active because
507 * in-memory mode is incompatible with it and it's stopped at
508 * EAL initialization stage, however the compiler doesn't know
509 * that and complains about map_offset being used uninitialized
510 * on failure codepaths while having in-memory mode enabled. so,
511 * assign a value here.
515 /* takes out a read lock on segment or segment list */
516 fd = get_seg_fd(path, sizeof(path), hi, list_idx, seg_idx);
518 RTE_LOG(ERR, EAL, "Couldn't get fd on hugepage file\n");
522 if (internal_config.single_file_segments) {
523 map_offset = seg_idx * alloc_sz;
524 ret = resize_hugefile(fd, path, list_idx, seg_idx,
525 map_offset, alloc_sz, true);
530 if (ftruncate(fd, alloc_sz) < 0) {
531 RTE_LOG(DEBUG, EAL, "%s(): ftruncate() failed: %s\n",
532 __func__, strerror(errno));
535 if (internal_config.hugepage_unlink) {
537 RTE_LOG(DEBUG, EAL, "%s(): unlink() failed: %s\n",
538 __func__, strerror(errno));
545 * map the segment, and populate page tables, the kernel fills
546 * this segment with zeros if it's a new page.
548 va = mmap(addr, alloc_sz, PROT_READ | PROT_WRITE,
549 MAP_SHARED | MAP_POPULATE | MAP_FIXED, fd,
553 if (va == MAP_FAILED) {
554 RTE_LOG(DEBUG, EAL, "%s(): mmap() failed: %s\n", __func__,
556 /* mmap failed, but the previous region might have been
557 * unmapped anyway. try to remap it
562 RTE_LOG(DEBUG, EAL, "%s(): wrong mmap() address\n", __func__);
563 munmap(va, alloc_sz);
567 /* In linux, hugetlb limitations, like cgroup, are
568 * enforced at fault time instead of mmap(), even
569 * with the option of MAP_POPULATE. Kernel will send
570 * a SIGBUS signal. To avoid to be killed, save stack
571 * environment here, if SIGBUS happens, we can jump
574 if (huge_wrap_sigsetjmp()) {
575 RTE_LOG(DEBUG, EAL, "SIGBUS: Cannot mmap more hugepages of size %uMB\n",
576 (unsigned int)(alloc_sz >> 20));
580 /* we need to trigger a write to the page to enforce page fault and
581 * ensure that page is accessible to us, but we can't overwrite value
582 * that is already there, so read the old value, and write itback.
583 * kernel populates the page with zeroes initially.
585 *(volatile int *)addr = *(volatile int *)addr;
587 iova = rte_mem_virt2iova(addr);
588 if (iova == RTE_BAD_PHYS_ADDR) {
589 RTE_LOG(DEBUG, EAL, "%s(): can't get IOVA addr\n",
594 #ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
595 move_pages(getpid(), 1, &addr, NULL, &cur_socket_id, 0);
597 if (cur_socket_id != socket_id) {
599 "%s(): allocation happened on wrong socket (wanted %d, got %d)\n",
600 __func__, socket_id, cur_socket_id);
604 /* for non-single file segments that aren't in-memory, we can close fd
606 if (!internal_config.single_file_segments && !internal_config.in_memory)
610 ms->hugepage_sz = alloc_sz;
612 ms->nchannel = rte_memory_get_nchannel();
613 ms->nrank = rte_memory_get_nrank();
615 ms->socket_id = socket_id;
620 munmap(addr, alloc_sz);
623 #ifdef RTE_ARCH_PPC_64
624 flags |= MAP_HUGETLB;
626 new_addr = eal_get_virtual_area(addr, &alloc_sz, alloc_sz, 0, flags);
627 if (new_addr != addr) {
628 if (new_addr != NULL)
629 munmap(new_addr, alloc_sz);
630 /* we're leaving a hole in our virtual address space. if
631 * somebody else maps this hole now, we could accidentally
632 * override it in the future.
634 RTE_LOG(CRIT, EAL, "Can't mmap holes in our virtual address space\n");
637 /* in-memory mode will never be single-file-segments mode */
638 if (internal_config.single_file_segments) {
639 resize_hugefile(fd, path, list_idx, seg_idx, map_offset,
641 /* ignore failure, can't make it any worse */
643 /* only remove file if we can take out a write lock */
644 if (internal_config.hugepage_unlink == 0 &&
645 internal_config.in_memory == 0 &&
646 lock(fd, LOCK_EX) == 1)
654 free_seg(struct rte_memseg *ms, struct hugepage_info *hi,
655 unsigned int list_idx, unsigned int seg_idx)
661 /* erase page data */
662 memset(ms->addr, 0, ms->len);
664 if (mmap(ms->addr, ms->len, PROT_READ,
665 MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0) ==
667 RTE_LOG(DEBUG, EAL, "couldn't unmap page\n");
671 /* if we've already unlinked the page, nothing needs to be done */
672 if (internal_config.hugepage_unlink) {
673 memset(ms, 0, sizeof(*ms));
677 /* if we are not in single file segments mode, we're going to unmap the
678 * segment and thus drop the lock on original fd, but hugepage dir is
679 * now locked so we can take out another one without races.
681 fd = get_seg_fd(path, sizeof(path), hi, list_idx, seg_idx);
685 if (internal_config.single_file_segments) {
686 map_offset = seg_idx * ms->len;
687 if (resize_hugefile(fd, path, list_idx, seg_idx, map_offset,
692 /* if we're able to take out a write lock, we're the last one
693 * holding onto this page.
695 ret = lock(fd, LOCK_EX);
697 /* no one else is using this page */
701 /* closing fd will drop the lock */
705 memset(ms, 0, sizeof(*ms));
707 return ret < 0 ? -1 : 0;
710 struct alloc_walk_param {
711 struct hugepage_info *hi;
712 struct rte_memseg **ms;
714 unsigned int segs_allocated;
720 alloc_seg_walk(const struct rte_memseg_list *msl, void *arg)
722 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
723 struct alloc_walk_param *wa = arg;
724 struct rte_memseg_list *cur_msl;
726 int cur_idx, start_idx, j, dir_fd = -1;
727 unsigned int msl_idx, need, i;
729 if (msl->page_sz != wa->page_sz)
731 if (msl->socket_id != wa->socket)
734 page_sz = (size_t)msl->page_sz;
736 msl_idx = msl - mcfg->memsegs;
737 cur_msl = &mcfg->memsegs[msl_idx];
741 /* try finding space in memseg list */
742 cur_idx = rte_fbarray_find_next_n_free(&cur_msl->memseg_arr, 0, need);
747 /* do not allow any page allocations during the time we're allocating,
748 * because file creation and locking operations are not atomic,
749 * and we might be the first or the last ones to use a particular page,
750 * so we need to ensure atomicity of every operation.
752 * during init, we already hold a write lock, so don't try to take out
755 if (wa->hi->lock_descriptor == -1 && !internal_config.in_memory) {
756 dir_fd = open(wa->hi->hugedir, O_RDONLY);
758 RTE_LOG(ERR, EAL, "%s(): Cannot open '%s': %s\n",
759 __func__, wa->hi->hugedir, strerror(errno));
762 /* blocking writelock */
763 if (flock(dir_fd, LOCK_EX)) {
764 RTE_LOG(ERR, EAL, "%s(): Cannot lock '%s': %s\n",
765 __func__, wa->hi->hugedir, strerror(errno));
771 for (i = 0; i < need; i++, cur_idx++) {
772 struct rte_memseg *cur;
775 cur = rte_fbarray_get(&cur_msl->memseg_arr, cur_idx);
776 map_addr = RTE_PTR_ADD(cur_msl->base_va,
779 if (alloc_seg(cur, map_addr, wa->socket, wa->hi,
781 RTE_LOG(DEBUG, EAL, "attempted to allocate %i segments, but only %i were allocated\n",
784 /* if exact number wasn't requested, stop */
789 for (j = start_idx; j < cur_idx; j++) {
790 struct rte_memseg *tmp;
791 struct rte_fbarray *arr =
792 &cur_msl->memseg_arr;
794 tmp = rte_fbarray_get(arr, j);
795 rte_fbarray_set_free(arr, j);
797 /* free_seg may attempt to create a file, which
800 if (free_seg(tmp, wa->hi, msl_idx, j))
801 RTE_LOG(DEBUG, EAL, "Cannot free page\n");
805 memset(wa->ms, 0, sizeof(*wa->ms) * wa->n_segs);
814 rte_fbarray_set_used(&cur_msl->memseg_arr, cur_idx);
817 wa->segs_allocated = i;
825 struct free_walk_param {
826 struct hugepage_info *hi;
827 struct rte_memseg *ms;
830 free_seg_walk(const struct rte_memseg_list *msl, void *arg)
832 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
833 struct rte_memseg_list *found_msl;
834 struct free_walk_param *wa = arg;
835 uintptr_t start_addr, end_addr;
836 int msl_idx, seg_idx, ret, dir_fd = -1;
838 start_addr = (uintptr_t) msl->base_va;
839 end_addr = start_addr + msl->memseg_arr.len * (size_t)msl->page_sz;
841 if ((uintptr_t)wa->ms->addr < start_addr ||
842 (uintptr_t)wa->ms->addr >= end_addr)
845 msl_idx = msl - mcfg->memsegs;
846 seg_idx = RTE_PTR_DIFF(wa->ms->addr, start_addr) / msl->page_sz;
849 found_msl = &mcfg->memsegs[msl_idx];
851 /* do not allow any page allocations during the time we're freeing,
852 * because file creation and locking operations are not atomic,
853 * and we might be the first or the last ones to use a particular page,
854 * so we need to ensure atomicity of every operation.
856 * during init, we already hold a write lock, so don't try to take out
859 if (wa->hi->lock_descriptor == -1 && !internal_config.in_memory) {
860 dir_fd = open(wa->hi->hugedir, O_RDONLY);
862 RTE_LOG(ERR, EAL, "%s(): Cannot open '%s': %s\n",
863 __func__, wa->hi->hugedir, strerror(errno));
866 /* blocking writelock */
867 if (flock(dir_fd, LOCK_EX)) {
868 RTE_LOG(ERR, EAL, "%s(): Cannot lock '%s': %s\n",
869 __func__, wa->hi->hugedir, strerror(errno));
875 found_msl->version++;
877 rte_fbarray_set_free(&found_msl->memseg_arr, seg_idx);
879 ret = free_seg(wa->ms, wa->hi, msl_idx, seg_idx);
891 eal_memalloc_alloc_seg_bulk(struct rte_memseg **ms, int n_segs, size_t page_sz,
892 int socket, bool exact)
895 #ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
896 bool have_numa = false;
898 struct bitmask *oldmask;
900 struct alloc_walk_param wa;
901 struct hugepage_info *hi = NULL;
903 memset(&wa, 0, sizeof(wa));
905 /* dynamic allocation not supported in legacy mode */
906 if (internal_config.legacy_mem)
909 for (i = 0; i < (int) RTE_DIM(internal_config.hugepage_info); i++) {
911 internal_config.hugepage_info[i].hugepage_sz) {
912 hi = &internal_config.hugepage_info[i];
917 RTE_LOG(ERR, EAL, "%s(): can't find relevant hugepage_info entry\n",
922 #ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
924 oldmask = numa_allocate_nodemask();
925 prepare_numa(&oldpolicy, oldmask, socket);
934 wa.page_sz = page_sz;
936 wa.segs_allocated = 0;
938 /* memalloc is locked, so it's safe to use thread-unsafe version */
939 ret = rte_memseg_list_walk_thread_unsafe(alloc_seg_walk, &wa);
941 RTE_LOG(ERR, EAL, "%s(): couldn't find suitable memseg_list\n",
944 } else if (ret > 0) {
945 ret = (int)wa.segs_allocated;
948 #ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
950 restore_numa(&oldpolicy, oldmask);
956 eal_memalloc_alloc_seg(size_t page_sz, int socket)
958 struct rte_memseg *ms;
959 if (eal_memalloc_alloc_seg_bulk(&ms, 1, page_sz, socket, true) < 0)
961 /* return pointer to newly allocated memseg */
966 eal_memalloc_free_seg_bulk(struct rte_memseg **ms, int n_segs)
970 /* dynamic free not supported in legacy mode */
971 if (internal_config.legacy_mem)
974 for (seg = 0; seg < n_segs; seg++) {
975 struct rte_memseg *cur = ms[seg];
976 struct hugepage_info *hi = NULL;
977 struct free_walk_param wa;
980 /* if this page is marked as unfreeable, fail */
981 if (cur->flags & RTE_MEMSEG_FLAG_DO_NOT_FREE) {
982 RTE_LOG(DEBUG, EAL, "Page is not allowed to be freed\n");
987 memset(&wa, 0, sizeof(wa));
989 for (i = 0; i < (int)RTE_DIM(internal_config.hugepage_info);
991 hi = &internal_config.hugepage_info[i];
992 if (cur->hugepage_sz == hi->hugepage_sz)
995 if (i == (int)RTE_DIM(internal_config.hugepage_info)) {
996 RTE_LOG(ERR, EAL, "Can't find relevant hugepage_info entry\n");
1004 /* memalloc is locked, so it's safe to use thread-unsafe version
1006 walk_res = rte_memseg_list_walk_thread_unsafe(free_seg_walk,
1011 RTE_LOG(ERR, EAL, "Couldn't find memseg list\n");
1018 eal_memalloc_free_seg(struct rte_memseg *ms)
1020 /* dynamic free not supported in legacy mode */
1021 if (internal_config.legacy_mem)
1024 return eal_memalloc_free_seg_bulk(&ms, 1);
1028 sync_chunk(struct rte_memseg_list *primary_msl,
1029 struct rte_memseg_list *local_msl, struct hugepage_info *hi,
1030 unsigned int msl_idx, bool used, int start, int end)
1032 struct rte_fbarray *l_arr, *p_arr;
1033 int i, ret, chunk_len, diff_len;
1035 l_arr = &local_msl->memseg_arr;
1036 p_arr = &primary_msl->memseg_arr;
1038 /* we need to aggregate allocations/deallocations into bigger chunks,
1039 * as we don't want to spam the user with per-page callbacks.
1041 * to avoid any potential issues, we also want to trigger
1042 * deallocation callbacks *before* we actually deallocate
1043 * memory, so that the user application could wrap up its use
1044 * before it goes away.
1047 chunk_len = end - start;
1049 /* find how many contiguous pages we can map/unmap for this chunk */
1051 rte_fbarray_find_contig_free(l_arr, start) :
1052 rte_fbarray_find_contig_used(l_arr, start);
1054 /* has to be at least one page */
1058 diff_len = RTE_MIN(chunk_len, diff_len);
1060 /* if we are freeing memory, notify the application */
1062 struct rte_memseg *ms;
1064 size_t len, page_sz;
1066 ms = rte_fbarray_get(l_arr, start);
1067 start_va = ms->addr;
1068 page_sz = (size_t)primary_msl->page_sz;
1069 len = page_sz * diff_len;
1071 eal_memalloc_mem_event_notify(RTE_MEM_EVENT_FREE,
1075 for (i = 0; i < diff_len; i++) {
1076 struct rte_memseg *p_ms, *l_ms;
1077 int seg_idx = start + i;
1079 l_ms = rte_fbarray_get(l_arr, seg_idx);
1080 p_ms = rte_fbarray_get(p_arr, seg_idx);
1082 if (l_ms == NULL || p_ms == NULL)
1086 ret = alloc_seg(l_ms, p_ms->addr,
1087 p_ms->socket_id, hi,
1091 rte_fbarray_set_used(l_arr, seg_idx);
1093 ret = free_seg(l_ms, hi, msl_idx, seg_idx);
1094 rte_fbarray_set_free(l_arr, seg_idx);
1100 /* if we just allocated memory, notify the application */
1102 struct rte_memseg *ms;
1104 size_t len, page_sz;
1106 ms = rte_fbarray_get(l_arr, start);
1107 start_va = ms->addr;
1108 page_sz = (size_t)primary_msl->page_sz;
1109 len = page_sz * diff_len;
1111 eal_memalloc_mem_event_notify(RTE_MEM_EVENT_ALLOC,
1115 /* calculate how much we can advance until next chunk */
1117 rte_fbarray_find_contig_used(l_arr, start) :
1118 rte_fbarray_find_contig_free(l_arr, start);
1119 ret = RTE_MIN(chunk_len, diff_len);
1125 sync_status(struct rte_memseg_list *primary_msl,
1126 struct rte_memseg_list *local_msl, struct hugepage_info *hi,
1127 unsigned int msl_idx, bool used)
1129 struct rte_fbarray *l_arr, *p_arr;
1130 int p_idx, l_chunk_len, p_chunk_len, ret;
1133 /* this is a little bit tricky, but the basic idea is - walk both lists
1134 * and spot any places where there are discrepancies. walking both lists
1135 * and noting discrepancies in a single go is a hard problem, so we do
1136 * it in two passes - first we spot any places where allocated segments
1137 * mismatch (i.e. ensure that everything that's allocated in the primary
1138 * is also allocated in the secondary), and then we do it by looking at
1139 * free segments instead.
1141 * we also need to aggregate changes into chunks, as we have to call
1142 * callbacks per allocation, not per page.
1144 l_arr = &local_msl->memseg_arr;
1145 p_arr = &primary_msl->memseg_arr;
1148 p_idx = rte_fbarray_find_next_used(p_arr, 0);
1150 p_idx = rte_fbarray_find_next_free(p_arr, 0);
1152 while (p_idx >= 0) {
1153 int next_chunk_search_idx;
1156 p_chunk_len = rte_fbarray_find_contig_used(p_arr,
1158 l_chunk_len = rte_fbarray_find_contig_used(l_arr,
1161 p_chunk_len = rte_fbarray_find_contig_free(p_arr,
1163 l_chunk_len = rte_fbarray_find_contig_free(l_arr,
1166 /* best case scenario - no differences (or bigger, which will be
1167 * fixed during next iteration), look for next chunk
1169 if (l_chunk_len >= p_chunk_len) {
1170 next_chunk_search_idx = p_idx + p_chunk_len;
1174 /* if both chunks start at the same point, skip parts we know
1175 * are identical, and sync the rest. each call to sync_chunk
1176 * will only sync contiguous segments, so we need to call this
1177 * until we are sure there are no more differences in this
1180 start = p_idx + l_chunk_len;
1181 end = p_idx + p_chunk_len;
1183 ret = sync_chunk(primary_msl, local_msl, hi, msl_idx,
1186 } while (start < end && ret >= 0);
1187 /* if ret is negative, something went wrong */
1191 next_chunk_search_idx = p_idx + p_chunk_len;
1193 /* skip to end of this chunk */
1195 p_idx = rte_fbarray_find_next_used(p_arr,
1196 next_chunk_search_idx);
1198 p_idx = rte_fbarray_find_next_free(p_arr,
1199 next_chunk_search_idx);
1206 sync_existing(struct rte_memseg_list *primary_msl,
1207 struct rte_memseg_list *local_msl, struct hugepage_info *hi,
1208 unsigned int msl_idx)
1212 /* do not allow any page allocations during the time we're allocating,
1213 * because file creation and locking operations are not atomic,
1214 * and we might be the first or the last ones to use a particular page,
1215 * so we need to ensure atomicity of every operation.
1217 dir_fd = open(hi->hugedir, O_RDONLY);
1219 RTE_LOG(ERR, EAL, "%s(): Cannot open '%s': %s\n", __func__,
1220 hi->hugedir, strerror(errno));
1223 /* blocking writelock */
1224 if (flock(dir_fd, LOCK_EX)) {
1225 RTE_LOG(ERR, EAL, "%s(): Cannot lock '%s': %s\n", __func__,
1226 hi->hugedir, strerror(errno));
1231 /* ensure all allocated space is the same in both lists */
1232 ret = sync_status(primary_msl, local_msl, hi, msl_idx, true);
1236 /* ensure all unallocated space is the same in both lists */
1237 ret = sync_status(primary_msl, local_msl, hi, msl_idx, false);
1241 /* update version number */
1242 local_msl->version = primary_msl->version;
1253 sync_walk(const struct rte_memseg_list *msl, void *arg __rte_unused)
1255 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
1256 struct rte_memseg_list *primary_msl, *local_msl;
1257 struct hugepage_info *hi = NULL;
1261 msl_idx = msl - mcfg->memsegs;
1262 primary_msl = &mcfg->memsegs[msl_idx];
1263 local_msl = &local_memsegs[msl_idx];
1265 for (i = 0; i < RTE_DIM(internal_config.hugepage_info); i++) {
1267 internal_config.hugepage_info[i].hugepage_sz;
1268 uint64_t msl_sz = primary_msl->page_sz;
1269 if (msl_sz == cur_sz) {
1270 hi = &internal_config.hugepage_info[i];
1275 RTE_LOG(ERR, EAL, "Can't find relevant hugepage_info entry\n");
1279 /* if versions don't match, synchronize everything */
1280 if (local_msl->version != primary_msl->version &&
1281 sync_existing(primary_msl, local_msl, hi, msl_idx))
1288 eal_memalloc_sync_with_primary(void)
1290 /* nothing to be done in primary */
1291 if (rte_eal_process_type() == RTE_PROC_PRIMARY)
1294 /* memalloc is locked, so it's safe to call thread-unsafe version */
1295 if (rte_memseg_list_walk_thread_unsafe(sync_walk, NULL))
1301 secondary_msl_create_walk(const struct rte_memseg_list *msl,
1302 void *arg __rte_unused)
1304 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
1305 struct rte_memseg_list *primary_msl, *local_msl;
1306 char name[PATH_MAX];
1309 msl_idx = msl - mcfg->memsegs;
1310 primary_msl = &mcfg->memsegs[msl_idx];
1311 local_msl = &local_memsegs[msl_idx];
1313 /* create distinct fbarrays for each secondary */
1314 snprintf(name, RTE_FBARRAY_NAME_LEN, "%s_%i",
1315 primary_msl->memseg_arr.name, getpid());
1317 ret = rte_fbarray_init(&local_msl->memseg_arr, name,
1318 primary_msl->memseg_arr.len,
1319 primary_msl->memseg_arr.elt_sz);
1321 RTE_LOG(ERR, EAL, "Cannot initialize local memory map\n");
1324 local_msl->base_va = primary_msl->base_va;
1330 fd_list_create_walk(const struct rte_memseg_list *msl,
1331 void *arg __rte_unused)
1333 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
1334 unsigned int i, len;
1338 msl_idx = msl - mcfg->memsegs;
1339 len = msl->memseg_arr.len;
1341 /* ensure we have space to store fd per each possible segment */
1342 data = malloc(sizeof(int) * len);
1344 RTE_LOG(ERR, EAL, "Unable to allocate space for file descriptors\n");
1347 /* set all fd's as invalid */
1348 for (i = 0; i < len; i++)
1351 fd_list[msl_idx].fds = data;
1352 fd_list[msl_idx].len = len;
1353 fd_list[msl_idx].count = 0;
1354 fd_list[msl_idx].memseg_list_fd = -1;
1360 eal_memalloc_init(void)
1362 if (rte_eal_process_type() == RTE_PROC_SECONDARY)
1363 if (rte_memseg_list_walk(secondary_msl_create_walk, NULL) < 0)
1366 /* initialize all of the fd lists */
1367 if (internal_config.single_file_segments)
1368 if (rte_memseg_list_walk(fd_list_create_walk, NULL))