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 */
60 /* for single-file segments, we need some kind of mechanism to keep track of
61 * which hugepages can be freed back to the system, and which cannot. we cannot
62 * use flock() because they don't allow locking parts of a file, and we cannot
63 * use fcntl() due to issues with their semantics, so we will have to rely on a
64 * bunch of lockfiles for each page.
66 * we cannot know how many pages a system will have in advance, but we do know
67 * that they come in lists, and we know lengths of these lists. so, simply store
68 * a malloc'd array of fd's indexed by list and segment index.
70 * they will be initialized at startup, and filled as we allocate/deallocate
71 * segments. also, use this to track memseg list proper fd.
74 int *fds; /**< dynamically allocated array of segment lock fd's */
75 int memseg_list_fd; /**< memseg list fd */
76 int len; /**< total length of the array */
77 int count; /**< entries used in an array */
78 } lock_fds[RTE_MAX_MEMSEG_LISTS];
80 /** local copy of a memory map, used to synchronize memory hotplug in MP */
81 static struct rte_memseg_list local_memsegs[RTE_MAX_MEMSEG_LISTS];
83 static sigjmp_buf huge_jmpenv;
85 static void __rte_unused huge_sigbus_handler(int signo __rte_unused)
87 siglongjmp(huge_jmpenv, 1);
90 /* Put setjmp into a wrap method to avoid compiling error. Any non-volatile,
91 * non-static local variable in the stack frame calling sigsetjmp might be
92 * clobbered by a call to longjmp.
94 static int __rte_unused huge_wrap_sigsetjmp(void)
96 return sigsetjmp(huge_jmpenv, 1);
99 static struct sigaction huge_action_old;
100 static int huge_need_recover;
102 static void __rte_unused
103 huge_register_sigbus(void)
106 struct sigaction action;
109 sigaddset(&mask, SIGBUS);
111 action.sa_mask = mask;
112 action.sa_handler = huge_sigbus_handler;
114 huge_need_recover = !sigaction(SIGBUS, &action, &huge_action_old);
117 static void __rte_unused
118 huge_recover_sigbus(void)
120 if (huge_need_recover) {
121 sigaction(SIGBUS, &huge_action_old, NULL);
122 huge_need_recover = 0;
126 #ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
131 /* Check if kernel supports NUMA. */
132 if (numa_available() != 0) {
133 RTE_LOG(DEBUG, EAL, "NUMA is not supported.\n");
140 prepare_numa(int *oldpolicy, struct bitmask *oldmask, int socket_id)
142 RTE_LOG(DEBUG, EAL, "Trying to obtain current memory policy.\n");
143 if (get_mempolicy(oldpolicy, oldmask->maskp,
144 oldmask->size + 1, 0, 0) < 0) {
146 "Failed to get current mempolicy: %s. "
147 "Assuming MPOL_DEFAULT.\n", strerror(errno));
148 oldpolicy = MPOL_DEFAULT;
151 "Setting policy MPOL_PREFERRED for socket %d\n",
153 numa_set_preferred(socket_id);
157 restore_numa(int *oldpolicy, struct bitmask *oldmask)
160 "Restoring previous memory policy: %d\n", *oldpolicy);
161 if (*oldpolicy == MPOL_DEFAULT) {
162 numa_set_localalloc();
163 } else if (set_mempolicy(*oldpolicy, oldmask->maskp,
164 oldmask->size + 1) < 0) {
165 RTE_LOG(ERR, EAL, "Failed to restore mempolicy: %s\n",
167 numa_set_localalloc();
169 numa_free_cpumask(oldmask);
174 * uses fstat to report the size of a file on disk
177 get_file_size(int fd)
180 if (fstat(fd, &st) < 0)
185 /* returns 1 on successful lock, 0 on unsuccessful lock, -1 on error */
186 static int lock(int fd, int type)
190 /* flock may be interrupted */
192 ret = flock(fd, type | LOCK_NB);
193 } while (ret && errno == EINTR);
195 if (ret && errno == EWOULDBLOCK) {
199 RTE_LOG(ERR, EAL, "%s(): error calling flock(): %s\n",
200 __func__, strerror(errno));
203 /* lock was successful */
207 static int get_segment_lock_fd(int list_idx, int seg_idx)
209 char path[PATH_MAX] = {0};
212 if (list_idx < 0 || list_idx >= (int)RTE_DIM(lock_fds))
214 if (seg_idx < 0 || seg_idx >= lock_fds[list_idx].len)
217 fd = lock_fds[list_idx].fds[seg_idx];
218 /* does this lock already exist? */
222 eal_get_hugefile_lock_path(path, sizeof(path),
223 list_idx * RTE_MAX_MEMSEG_PER_LIST + seg_idx);
225 fd = open(path, O_CREAT | O_RDWR, 0660);
227 RTE_LOG(ERR, EAL, "%s(): error creating lockfile '%s': %s\n",
228 __func__, path, strerror(errno));
231 /* take out a read lock */
232 if (lock(fd, LOCK_SH) != 1) {
233 RTE_LOG(ERR, EAL, "%s(): failed to take out a readlock on '%s': %s\n",
234 __func__, path, strerror(errno));
238 /* store it for future reference */
239 lock_fds[list_idx].fds[seg_idx] = fd;
240 lock_fds[list_idx].count++;
244 static int unlock_segment(int list_idx, int seg_idx)
248 if (list_idx < 0 || list_idx >= (int)RTE_DIM(lock_fds))
250 if (seg_idx < 0 || seg_idx >= lock_fds[list_idx].len)
253 fd = lock_fds[list_idx].fds[seg_idx];
255 /* upgrade lock to exclusive to see if we can remove the lockfile */
256 ret = lock(fd, LOCK_EX);
258 /* we've succeeded in taking exclusive lock, this lockfile may
261 char path[PATH_MAX] = {0};
262 eal_get_hugefile_lock_path(path, sizeof(path),
263 list_idx * RTE_MAX_MEMSEG_PER_LIST + seg_idx);
265 RTE_LOG(ERR, EAL, "%s(): error removing lockfile '%s': %s\n",
266 __func__, path, strerror(errno));
269 /* we don't want to leak the fd, so even if we fail to lock, close fd
270 * and remove it from list anyway.
273 lock_fds[list_idx].fds[seg_idx] = -1;
274 lock_fds[list_idx].count--;
282 get_seg_fd(char *path, int buflen, struct hugepage_info *hi,
283 unsigned int list_idx, unsigned int seg_idx)
287 if (internal_config.single_file_segments) {
288 /* create a hugepage file path */
289 eal_get_hugefile_path(path, buflen, hi->hugedir, list_idx);
291 fd = lock_fds[list_idx].memseg_list_fd;
294 fd = open(path, O_CREAT | O_RDWR, 0600);
296 RTE_LOG(ERR, EAL, "%s(): open failed: %s\n",
297 __func__, strerror(errno));
300 /* take out a read lock and keep it indefinitely */
301 if (lock(fd, LOCK_SH) < 0) {
302 RTE_LOG(ERR, EAL, "%s(): lock failed: %s\n",
303 __func__, strerror(errno));
307 lock_fds[list_idx].memseg_list_fd = fd;
310 /* create a hugepage file path */
311 eal_get_hugefile_path(path, buflen, hi->hugedir,
312 list_idx * RTE_MAX_MEMSEG_PER_LIST + seg_idx);
313 fd = open(path, O_CREAT | O_RDWR, 0600);
315 RTE_LOG(DEBUG, EAL, "%s(): open failed: %s\n", __func__,
319 /* take out a read lock */
320 if (lock(fd, LOCK_SH) < 0) {
321 RTE_LOG(ERR, EAL, "%s(): lock failed: %s\n",
322 __func__, strerror(errno));
331 resize_hugefile(int fd, char *path, int list_idx, int seg_idx,
332 uint64_t fa_offset, uint64_t page_sz, bool grow)
336 if (fallocate_supported == 0) {
337 /* we cannot deallocate memory if fallocate() is not
338 * supported, and hugepage file is already locked at
339 * creation, so no further synchronization needed.
343 RTE_LOG(DEBUG, EAL, "%s(): fallocate not supported, not freeing page back to the system\n",
347 uint64_t new_size = fa_offset + page_sz;
348 uint64_t cur_size = get_file_size(fd);
350 /* fallocate isn't supported, fall back to ftruncate */
351 if (new_size > cur_size &&
352 ftruncate(fd, new_size) < 0) {
353 RTE_LOG(DEBUG, EAL, "%s(): ftruncate() failed: %s\n",
354 __func__, strerror(errno));
358 int flags = grow ? 0 : FALLOC_FL_PUNCH_HOLE |
362 /* if fallocate() is supported, we need to take out a
363 * read lock on allocate (to prevent other processes
364 * from deallocating this page), and take out a write
365 * lock on deallocate (to ensure nobody else is using
368 * read locks on page itself are already taken out at
369 * file creation, in get_seg_fd().
371 * we cannot rely on simple use of flock() call, because
372 * we need to be able to lock a section of the file,
373 * and we cannot use fcntl() locks, because of numerous
374 * problems with their semantics, so we will use
375 * deterministically named lock files for each section
378 * if we're shrinking the file, we want to upgrade our
379 * lock from shared to exclusive.
381 * lock_fd is an fd for a lockfile, not for the segment
384 lock_fd = get_segment_lock_fd(list_idx, seg_idx);
387 /* we are using this lockfile to determine
388 * whether this particular page is locked, as we
389 * are in single file segments mode and thus
390 * cannot use regular flock() to get this info.
392 * we want to try and take out an exclusive lock
393 * on the lock file to determine if we're the
394 * last ones using this page, and if not, we
395 * won't be shrinking it, and will instead exit
398 ret = lock(lock_fd, LOCK_EX);
400 /* drop the lock on the lockfile, so that even
401 * if we couldn't shrink the file ourselves, we
402 * are signalling to other processes that we're
403 * no longer using this page.
405 if (unlock_segment(list_idx, seg_idx))
406 RTE_LOG(ERR, EAL, "Could not unlock segment\n");
408 /* additionally, if this was the last lock on
409 * this segment list, we can safely close the
410 * page file fd, so that one of the processes
411 * could then delete the file after shrinking.
413 if (ret < 1 && lock_fds[list_idx].count == 0) {
415 lock_fds[list_idx].memseg_list_fd = -1;
419 RTE_LOG(ERR, EAL, "Could not lock segment\n");
423 /* failed to lock, not an error. */
427 /* grow or shrink the file */
428 ret = fallocate(fd, flags, fa_offset, page_sz);
431 if (fallocate_supported == -1 &&
433 RTE_LOG(ERR, EAL, "%s(): fallocate() not supported, hugepage deallocation will be disabled\n",
436 fallocate_supported = 0;
438 RTE_LOG(DEBUG, EAL, "%s(): fallocate() failed: %s\n",
444 fallocate_supported = 1;
446 /* we've grew/shrunk the file, and we hold an
447 * exclusive lock now. check if there are no
448 * more segments active in this segment list,
449 * and remove the file if there aren't.
451 if (lock_fds[list_idx].count == 0) {
453 RTE_LOG(ERR, EAL, "%s(): unlinking '%s' failed: %s\n",
457 lock_fds[list_idx].memseg_list_fd = -1;
466 alloc_seg(struct rte_memseg *ms, void *addr, int socket_id,
467 struct hugepage_info *hi, unsigned int list_idx,
468 unsigned int seg_idx)
470 #ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
471 int cur_socket_id = 0;
483 alloc_sz = hi->hugepage_sz;
484 if (internal_config.in_memory && anonymous_hugepages_supported) {
487 log2 = rte_log2_u32(alloc_sz);
488 /* as per mmap() manpage, all page sizes are log2 of page size
489 * shifted by MAP_HUGE_SHIFT
491 flags = (log2 << RTE_MAP_HUGE_SHIFT) | MAP_HUGETLB | MAP_FIXED |
492 MAP_PRIVATE | MAP_ANONYMOUS;
494 va = mmap(addr, alloc_sz, PROT_READ | PROT_WRITE, flags, -1, 0);
496 /* single-file segments codepath will never be active because
497 * in-memory mode is incompatible with it and it's stopped at
498 * EAL initialization stage, however the compiler doesn't know
499 * that and complains about map_offset being used uninitialized
500 * on failure codepaths while having in-memory mode enabled. so,
501 * assign a value here.
505 /* takes out a read lock on segment or segment list */
506 fd = get_seg_fd(path, sizeof(path), hi, list_idx, seg_idx);
508 RTE_LOG(ERR, EAL, "Couldn't get fd on hugepage file\n");
512 if (internal_config.single_file_segments) {
513 map_offset = seg_idx * alloc_sz;
514 ret = resize_hugefile(fd, path, list_idx, seg_idx,
515 map_offset, alloc_sz, true);
520 if (ftruncate(fd, alloc_sz) < 0) {
521 RTE_LOG(DEBUG, EAL, "%s(): ftruncate() failed: %s\n",
522 __func__, strerror(errno));
525 if (internal_config.hugepage_unlink) {
527 RTE_LOG(DEBUG, EAL, "%s(): unlink() failed: %s\n",
528 __func__, strerror(errno));
535 * map the segment, and populate page tables, the kernel fills
536 * this segment with zeros if it's a new page.
538 va = mmap(addr, alloc_sz, PROT_READ | PROT_WRITE,
539 MAP_SHARED | MAP_POPULATE | MAP_FIXED, fd,
543 if (va == MAP_FAILED) {
544 RTE_LOG(DEBUG, EAL, "%s(): mmap() failed: %s\n", __func__,
546 /* mmap failed, but the previous region might have been
547 * unmapped anyway. try to remap it
552 RTE_LOG(DEBUG, EAL, "%s(): wrong mmap() address\n", __func__);
553 munmap(va, alloc_sz);
557 /* In linux, hugetlb limitations, like cgroup, are
558 * enforced at fault time instead of mmap(), even
559 * with the option of MAP_POPULATE. Kernel will send
560 * a SIGBUS signal. To avoid to be killed, save stack
561 * environment here, if SIGBUS happens, we can jump
564 if (huge_wrap_sigsetjmp()) {
565 RTE_LOG(DEBUG, EAL, "SIGBUS: Cannot mmap more hugepages of size %uMB\n",
566 (unsigned int)(alloc_sz >> 20));
570 /* we need to trigger a write to the page to enforce page fault and
571 * ensure that page is accessible to us, but we can't overwrite value
572 * that is already there, so read the old value, and write itback.
573 * kernel populates the page with zeroes initially.
575 *(volatile int *)addr = *(volatile int *)addr;
577 iova = rte_mem_virt2iova(addr);
578 if (iova == RTE_BAD_PHYS_ADDR) {
579 RTE_LOG(DEBUG, EAL, "%s(): can't get IOVA addr\n",
584 #ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
585 move_pages(getpid(), 1, &addr, NULL, &cur_socket_id, 0);
587 if (cur_socket_id != socket_id) {
589 "%s(): allocation happened on wrong socket (wanted %d, got %d)\n",
590 __func__, socket_id, cur_socket_id);
594 /* for non-single file segments that aren't in-memory, we can close fd
596 if (!internal_config.single_file_segments && !internal_config.in_memory)
600 ms->hugepage_sz = alloc_sz;
602 ms->nchannel = rte_memory_get_nchannel();
603 ms->nrank = rte_memory_get_nrank();
605 ms->socket_id = socket_id;
610 munmap(addr, alloc_sz);
613 #ifdef RTE_ARCH_PPC_64
614 flags |= MAP_HUGETLB;
616 new_addr = eal_get_virtual_area(addr, &alloc_sz, alloc_sz, 0, flags);
617 if (new_addr != addr) {
618 if (new_addr != NULL)
619 munmap(new_addr, alloc_sz);
620 /* we're leaving a hole in our virtual address space. if
621 * somebody else maps this hole now, we could accidentally
622 * override it in the future.
624 RTE_LOG(CRIT, EAL, "Can't mmap holes in our virtual address space\n");
627 /* in-memory mode will never be single-file-segments mode */
628 if (internal_config.single_file_segments) {
629 resize_hugefile(fd, path, list_idx, seg_idx, map_offset,
631 /* ignore failure, can't make it any worse */
633 /* only remove file if we can take out a write lock */
634 if (internal_config.hugepage_unlink == 0 &&
635 internal_config.in_memory == 0 &&
636 lock(fd, LOCK_EX) == 1)
644 free_seg(struct rte_memseg *ms, struct hugepage_info *hi,
645 unsigned int list_idx, unsigned int seg_idx)
651 /* erase page data */
652 memset(ms->addr, 0, ms->len);
654 if (mmap(ms->addr, ms->len, PROT_READ,
655 MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0) ==
657 RTE_LOG(DEBUG, EAL, "couldn't unmap page\n");
661 /* if we've already unlinked the page, nothing needs to be done */
662 if (internal_config.hugepage_unlink) {
663 memset(ms, 0, sizeof(*ms));
667 /* if we are not in single file segments mode, we're going to unmap the
668 * segment and thus drop the lock on original fd, but hugepage dir is
669 * now locked so we can take out another one without races.
671 fd = get_seg_fd(path, sizeof(path), hi, list_idx, seg_idx);
675 if (internal_config.single_file_segments) {
676 map_offset = seg_idx * ms->len;
677 if (resize_hugefile(fd, path, list_idx, seg_idx, map_offset,
682 /* if we're able to take out a write lock, we're the last one
683 * holding onto this page.
685 ret = lock(fd, LOCK_EX);
687 /* no one else is using this page */
691 /* closing fd will drop the lock */
695 memset(ms, 0, sizeof(*ms));
697 return ret < 0 ? -1 : 0;
700 struct alloc_walk_param {
701 struct hugepage_info *hi;
702 struct rte_memseg **ms;
704 unsigned int segs_allocated;
710 alloc_seg_walk(const struct rte_memseg_list *msl, void *arg)
712 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
713 struct alloc_walk_param *wa = arg;
714 struct rte_memseg_list *cur_msl;
716 int cur_idx, start_idx, j, dir_fd = -1;
717 unsigned int msl_idx, need, i;
719 if (msl->page_sz != wa->page_sz)
721 if (msl->socket_id != wa->socket)
724 page_sz = (size_t)msl->page_sz;
726 msl_idx = msl - mcfg->memsegs;
727 cur_msl = &mcfg->memsegs[msl_idx];
731 /* try finding space in memseg list */
732 cur_idx = rte_fbarray_find_prev_n_free(&cur_msl->memseg_arr,
733 cur_msl->memseg_arr.len - 1, need);
738 /* do not allow any page allocations during the time we're allocating,
739 * because file creation and locking operations are not atomic,
740 * and we might be the first or the last ones to use a particular page,
741 * so we need to ensure atomicity of every operation.
743 * during init, we already hold a write lock, so don't try to take out
746 if (wa->hi->lock_descriptor == -1 && !internal_config.in_memory) {
747 dir_fd = open(wa->hi->hugedir, O_RDONLY);
749 RTE_LOG(ERR, EAL, "%s(): Cannot open '%s': %s\n",
750 __func__, wa->hi->hugedir, strerror(errno));
753 /* blocking writelock */
754 if (flock(dir_fd, LOCK_EX)) {
755 RTE_LOG(ERR, EAL, "%s(): Cannot lock '%s': %s\n",
756 __func__, wa->hi->hugedir, strerror(errno));
762 for (i = 0; i < need; i++, cur_idx++) {
763 struct rte_memseg *cur;
766 cur = rte_fbarray_get(&cur_msl->memseg_arr, cur_idx);
767 map_addr = RTE_PTR_ADD(cur_msl->base_va,
770 if (alloc_seg(cur, map_addr, wa->socket, wa->hi,
772 RTE_LOG(DEBUG, EAL, "attempted to allocate %i segments, but only %i were allocated\n",
775 /* if exact number wasn't requested, stop */
780 for (j = start_idx; j < cur_idx; j++) {
781 struct rte_memseg *tmp;
782 struct rte_fbarray *arr =
783 &cur_msl->memseg_arr;
785 tmp = rte_fbarray_get(arr, j);
786 rte_fbarray_set_free(arr, j);
788 /* free_seg may attempt to create a file, which
791 if (free_seg(tmp, wa->hi, msl_idx, j))
792 RTE_LOG(DEBUG, EAL, "Cannot free page\n");
796 memset(wa->ms, 0, sizeof(*wa->ms) * wa->n_segs);
805 rte_fbarray_set_used(&cur_msl->memseg_arr, cur_idx);
808 wa->segs_allocated = i;
816 struct free_walk_param {
817 struct hugepage_info *hi;
818 struct rte_memseg *ms;
821 free_seg_walk(const struct rte_memseg_list *msl, void *arg)
823 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
824 struct rte_memseg_list *found_msl;
825 struct free_walk_param *wa = arg;
826 uintptr_t start_addr, end_addr;
827 int msl_idx, seg_idx, ret, dir_fd = -1;
829 start_addr = (uintptr_t) msl->base_va;
830 end_addr = start_addr + msl->memseg_arr.len * (size_t)msl->page_sz;
832 if ((uintptr_t)wa->ms->addr < start_addr ||
833 (uintptr_t)wa->ms->addr >= end_addr)
836 msl_idx = msl - mcfg->memsegs;
837 seg_idx = RTE_PTR_DIFF(wa->ms->addr, start_addr) / msl->page_sz;
840 found_msl = &mcfg->memsegs[msl_idx];
842 /* do not allow any page allocations during the time we're freeing,
843 * because file creation and locking operations are not atomic,
844 * and we might be the first or the last ones to use a particular page,
845 * so we need to ensure atomicity of every operation.
847 * during init, we already hold a write lock, so don't try to take out
850 if (wa->hi->lock_descriptor == -1 && !internal_config.in_memory) {
851 dir_fd = open(wa->hi->hugedir, O_RDONLY);
853 RTE_LOG(ERR, EAL, "%s(): Cannot open '%s': %s\n",
854 __func__, wa->hi->hugedir, strerror(errno));
857 /* blocking writelock */
858 if (flock(dir_fd, LOCK_EX)) {
859 RTE_LOG(ERR, EAL, "%s(): Cannot lock '%s': %s\n",
860 __func__, wa->hi->hugedir, strerror(errno));
866 found_msl->version++;
868 rte_fbarray_set_free(&found_msl->memseg_arr, seg_idx);
870 ret = free_seg(wa->ms, wa->hi, msl_idx, seg_idx);
882 eal_memalloc_alloc_seg_bulk(struct rte_memseg **ms, int n_segs, size_t page_sz,
883 int socket, bool exact)
886 #ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
887 bool have_numa = false;
889 struct bitmask *oldmask;
891 struct alloc_walk_param wa;
892 struct hugepage_info *hi = NULL;
894 memset(&wa, 0, sizeof(wa));
896 /* dynamic allocation not supported in legacy mode */
897 if (internal_config.legacy_mem)
900 for (i = 0; i < (int) RTE_DIM(internal_config.hugepage_info); i++) {
902 internal_config.hugepage_info[i].hugepage_sz) {
903 hi = &internal_config.hugepage_info[i];
908 RTE_LOG(ERR, EAL, "%s(): can't find relevant hugepage_info entry\n",
913 #ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
915 oldmask = numa_allocate_nodemask();
916 prepare_numa(&oldpolicy, oldmask, socket);
925 wa.page_sz = page_sz;
927 wa.segs_allocated = 0;
929 /* memalloc is locked, so it's safe to use thread-unsafe version */
930 ret = rte_memseg_list_walk_thread_unsafe(alloc_seg_walk, &wa);
932 RTE_LOG(ERR, EAL, "%s(): couldn't find suitable memseg_list\n",
935 } else if (ret > 0) {
936 ret = (int)wa.segs_allocated;
939 #ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
941 restore_numa(&oldpolicy, oldmask);
947 eal_memalloc_alloc_seg(size_t page_sz, int socket)
949 struct rte_memseg *ms;
950 if (eal_memalloc_alloc_seg_bulk(&ms, 1, page_sz, socket, true) < 0)
952 /* return pointer to newly allocated memseg */
957 eal_memalloc_free_seg_bulk(struct rte_memseg **ms, int n_segs)
961 /* dynamic free not supported in legacy mode */
962 if (internal_config.legacy_mem)
965 for (seg = 0; seg < n_segs; seg++) {
966 struct rte_memseg *cur = ms[seg];
967 struct hugepage_info *hi = NULL;
968 struct free_walk_param wa;
971 /* if this page is marked as unfreeable, fail */
972 if (cur->flags & RTE_MEMSEG_FLAG_DO_NOT_FREE) {
973 RTE_LOG(DEBUG, EAL, "Page is not allowed to be freed\n");
978 memset(&wa, 0, sizeof(wa));
980 for (i = 0; i < (int)RTE_DIM(internal_config.hugepage_info);
982 hi = &internal_config.hugepage_info[i];
983 if (cur->hugepage_sz == hi->hugepage_sz)
986 if (i == (int)RTE_DIM(internal_config.hugepage_info)) {
987 RTE_LOG(ERR, EAL, "Can't find relevant hugepage_info entry\n");
995 /* memalloc is locked, so it's safe to use thread-unsafe version
997 walk_res = rte_memseg_list_walk_thread_unsafe(free_seg_walk,
1002 RTE_LOG(ERR, EAL, "Couldn't find memseg list\n");
1009 eal_memalloc_free_seg(struct rte_memseg *ms)
1011 /* dynamic free not supported in legacy mode */
1012 if (internal_config.legacy_mem)
1015 return eal_memalloc_free_seg_bulk(&ms, 1);
1019 sync_chunk(struct rte_memseg_list *primary_msl,
1020 struct rte_memseg_list *local_msl, struct hugepage_info *hi,
1021 unsigned int msl_idx, bool used, int start, int end)
1023 struct rte_fbarray *l_arr, *p_arr;
1024 int i, ret, chunk_len, diff_len;
1026 l_arr = &local_msl->memseg_arr;
1027 p_arr = &primary_msl->memseg_arr;
1029 /* we need to aggregate allocations/deallocations into bigger chunks,
1030 * as we don't want to spam the user with per-page callbacks.
1032 * to avoid any potential issues, we also want to trigger
1033 * deallocation callbacks *before* we actually deallocate
1034 * memory, so that the user application could wrap up its use
1035 * before it goes away.
1038 chunk_len = end - start;
1040 /* find how many contiguous pages we can map/unmap for this chunk */
1042 rte_fbarray_find_contig_free(l_arr, start) :
1043 rte_fbarray_find_contig_used(l_arr, start);
1045 /* has to be at least one page */
1049 diff_len = RTE_MIN(chunk_len, diff_len);
1051 /* if we are freeing memory, notify the application */
1053 struct rte_memseg *ms;
1055 size_t len, page_sz;
1057 ms = rte_fbarray_get(l_arr, start);
1058 start_va = ms->addr;
1059 page_sz = (size_t)primary_msl->page_sz;
1060 len = page_sz * diff_len;
1062 eal_memalloc_mem_event_notify(RTE_MEM_EVENT_FREE,
1066 for (i = 0; i < diff_len; i++) {
1067 struct rte_memseg *p_ms, *l_ms;
1068 int seg_idx = start + i;
1070 l_ms = rte_fbarray_get(l_arr, seg_idx);
1071 p_ms = rte_fbarray_get(p_arr, seg_idx);
1073 if (l_ms == NULL || p_ms == NULL)
1077 ret = alloc_seg(l_ms, p_ms->addr,
1078 p_ms->socket_id, hi,
1082 rte_fbarray_set_used(l_arr, seg_idx);
1084 ret = free_seg(l_ms, hi, msl_idx, seg_idx);
1085 rte_fbarray_set_free(l_arr, seg_idx);
1091 /* if we just allocated memory, notify the application */
1093 struct rte_memseg *ms;
1095 size_t len, page_sz;
1097 ms = rte_fbarray_get(l_arr, start);
1098 start_va = ms->addr;
1099 page_sz = (size_t)primary_msl->page_sz;
1100 len = page_sz * diff_len;
1102 eal_memalloc_mem_event_notify(RTE_MEM_EVENT_ALLOC,
1106 /* calculate how much we can advance until next chunk */
1108 rte_fbarray_find_contig_used(l_arr, start) :
1109 rte_fbarray_find_contig_free(l_arr, start);
1110 ret = RTE_MIN(chunk_len, diff_len);
1116 sync_status(struct rte_memseg_list *primary_msl,
1117 struct rte_memseg_list *local_msl, struct hugepage_info *hi,
1118 unsigned int msl_idx, bool used)
1120 struct rte_fbarray *l_arr, *p_arr;
1121 int p_idx, l_chunk_len, p_chunk_len, ret;
1124 /* this is a little bit tricky, but the basic idea is - walk both lists
1125 * and spot any places where there are discrepancies. walking both lists
1126 * and noting discrepancies in a single go is a hard problem, so we do
1127 * it in two passes - first we spot any places where allocated segments
1128 * mismatch (i.e. ensure that everything that's allocated in the primary
1129 * is also allocated in the secondary), and then we do it by looking at
1130 * free segments instead.
1132 * we also need to aggregate changes into chunks, as we have to call
1133 * callbacks per allocation, not per page.
1135 l_arr = &local_msl->memseg_arr;
1136 p_arr = &primary_msl->memseg_arr;
1139 p_idx = rte_fbarray_find_next_used(p_arr, 0);
1141 p_idx = rte_fbarray_find_next_free(p_arr, 0);
1143 while (p_idx >= 0) {
1144 int next_chunk_search_idx;
1147 p_chunk_len = rte_fbarray_find_contig_used(p_arr,
1149 l_chunk_len = rte_fbarray_find_contig_used(l_arr,
1152 p_chunk_len = rte_fbarray_find_contig_free(p_arr,
1154 l_chunk_len = rte_fbarray_find_contig_free(l_arr,
1157 /* best case scenario - no differences (or bigger, which will be
1158 * fixed during next iteration), look for next chunk
1160 if (l_chunk_len >= p_chunk_len) {
1161 next_chunk_search_idx = p_idx + p_chunk_len;
1165 /* if both chunks start at the same point, skip parts we know
1166 * are identical, and sync the rest. each call to sync_chunk
1167 * will only sync contiguous segments, so we need to call this
1168 * until we are sure there are no more differences in this
1171 start = p_idx + l_chunk_len;
1172 end = p_idx + p_chunk_len;
1174 ret = sync_chunk(primary_msl, local_msl, hi, msl_idx,
1177 } while (start < end && ret >= 0);
1178 /* if ret is negative, something went wrong */
1182 next_chunk_search_idx = p_idx + p_chunk_len;
1184 /* skip to end of this chunk */
1186 p_idx = rte_fbarray_find_next_used(p_arr,
1187 next_chunk_search_idx);
1189 p_idx = rte_fbarray_find_next_free(p_arr,
1190 next_chunk_search_idx);
1197 sync_existing(struct rte_memseg_list *primary_msl,
1198 struct rte_memseg_list *local_msl, struct hugepage_info *hi,
1199 unsigned int msl_idx)
1203 /* do not allow any page allocations during the time we're allocating,
1204 * because file creation and locking operations are not atomic,
1205 * and we might be the first or the last ones to use a particular page,
1206 * so we need to ensure atomicity of every operation.
1208 dir_fd = open(hi->hugedir, O_RDONLY);
1210 RTE_LOG(ERR, EAL, "%s(): Cannot open '%s': %s\n", __func__,
1211 hi->hugedir, strerror(errno));
1214 /* blocking writelock */
1215 if (flock(dir_fd, LOCK_EX)) {
1216 RTE_LOG(ERR, EAL, "%s(): Cannot lock '%s': %s\n", __func__,
1217 hi->hugedir, strerror(errno));
1222 /* ensure all allocated space is the same in both lists */
1223 ret = sync_status(primary_msl, local_msl, hi, msl_idx, true);
1227 /* ensure all unallocated space is the same in both lists */
1228 ret = sync_status(primary_msl, local_msl, hi, msl_idx, false);
1232 /* update version number */
1233 local_msl->version = primary_msl->version;
1244 sync_walk(const struct rte_memseg_list *msl, void *arg __rte_unused)
1246 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
1247 struct rte_memseg_list *primary_msl, *local_msl;
1248 struct hugepage_info *hi = NULL;
1252 msl_idx = msl - mcfg->memsegs;
1253 primary_msl = &mcfg->memsegs[msl_idx];
1254 local_msl = &local_memsegs[msl_idx];
1256 for (i = 0; i < RTE_DIM(internal_config.hugepage_info); i++) {
1258 internal_config.hugepage_info[i].hugepage_sz;
1259 uint64_t msl_sz = primary_msl->page_sz;
1260 if (msl_sz == cur_sz) {
1261 hi = &internal_config.hugepage_info[i];
1266 RTE_LOG(ERR, EAL, "Can't find relevant hugepage_info entry\n");
1270 /* if versions don't match, synchronize everything */
1271 if (local_msl->version != primary_msl->version &&
1272 sync_existing(primary_msl, local_msl, hi, msl_idx))
1279 eal_memalloc_sync_with_primary(void)
1281 /* nothing to be done in primary */
1282 if (rte_eal_process_type() == RTE_PROC_PRIMARY)
1285 /* memalloc is locked, so it's safe to call thread-unsafe version */
1286 if (rte_memseg_list_walk_thread_unsafe(sync_walk, NULL))
1292 secondary_msl_create_walk(const struct rte_memseg_list *msl,
1293 void *arg __rte_unused)
1295 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
1296 struct rte_memseg_list *primary_msl, *local_msl;
1297 char name[PATH_MAX];
1300 msl_idx = msl - mcfg->memsegs;
1301 primary_msl = &mcfg->memsegs[msl_idx];
1302 local_msl = &local_memsegs[msl_idx];
1304 /* create distinct fbarrays for each secondary */
1305 snprintf(name, RTE_FBARRAY_NAME_LEN, "%s_%i",
1306 primary_msl->memseg_arr.name, getpid());
1308 ret = rte_fbarray_init(&local_msl->memseg_arr, name,
1309 primary_msl->memseg_arr.len,
1310 primary_msl->memseg_arr.elt_sz);
1312 RTE_LOG(ERR, EAL, "Cannot initialize local memory map\n");
1315 local_msl->base_va = primary_msl->base_va;
1321 secondary_lock_list_create_walk(const struct rte_memseg_list *msl,
1322 void *arg __rte_unused)
1324 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
1325 unsigned int i, len;
1329 msl_idx = msl - mcfg->memsegs;
1330 len = msl->memseg_arr.len;
1332 /* ensure we have space to store lock fd per each possible segment */
1333 data = malloc(sizeof(int) * len);
1335 RTE_LOG(ERR, EAL, "Unable to allocate space for lock descriptors\n");
1338 /* set all fd's as invalid */
1339 for (i = 0; i < len; i++)
1342 lock_fds[msl_idx].fds = data;
1343 lock_fds[msl_idx].len = len;
1344 lock_fds[msl_idx].count = 0;
1345 lock_fds[msl_idx].memseg_list_fd = -1;
1351 eal_memalloc_init(void)
1353 if (rte_eal_process_type() == RTE_PROC_SECONDARY)
1354 if (rte_memseg_list_walk(secondary_msl_create_walk, NULL) < 0)
1357 /* initialize all of the lock fd lists */
1358 if (internal_config.single_file_segments)
1359 if (rte_memseg_list_walk(secondary_lock_list_create_walk, NULL))