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>
32 #include <rte_common.h>
34 #include <rte_eal_memconfig.h>
36 #include <rte_memory.h>
37 #include <rte_spinlock.h>
39 #include "eal_filesystem.h"
40 #include "eal_internal_cfg.h"
41 #include "eal_memalloc.h"
42 #include "eal_private.h"
45 * not all kernel version support fallocate on hugetlbfs, so fall back to
46 * ftruncate and disallow deallocation if fallocate is not supported.
48 static int fallocate_supported = -1; /* unknown */
50 /* for single-file segments, we need some kind of mechanism to keep track of
51 * which hugepages can be freed back to the system, and which cannot. we cannot
52 * use flock() because they don't allow locking parts of a file, and we cannot
53 * use fcntl() due to issues with their semantics, so we will have to rely on a
54 * bunch of lockfiles for each page.
56 * we cannot know how many pages a system will have in advance, but we do know
57 * that they come in lists, and we know lengths of these lists. so, simply store
58 * a malloc'd array of fd's indexed by list and segment index.
60 * they will be initialized at startup, and filled as we allocate/deallocate
61 * segments. also, use this to track memseg list proper fd.
64 int *fds; /**< dynamically allocated array of segment lock fd's */
65 int memseg_list_fd; /**< memseg list fd */
66 int len; /**< total length of the array */
67 int count; /**< entries used in an array */
68 } lock_fds[RTE_MAX_MEMSEG_LISTS];
70 /** local copy of a memory map, used to synchronize memory hotplug in MP */
71 static struct rte_memseg_list local_memsegs[RTE_MAX_MEMSEG_LISTS];
73 static sigjmp_buf huge_jmpenv;
75 static void __rte_unused huge_sigbus_handler(int signo __rte_unused)
77 siglongjmp(huge_jmpenv, 1);
80 /* Put setjmp into a wrap method to avoid compiling error. Any non-volatile,
81 * non-static local variable in the stack frame calling sigsetjmp might be
82 * clobbered by a call to longjmp.
84 static int __rte_unused huge_wrap_sigsetjmp(void)
86 return sigsetjmp(huge_jmpenv, 1);
89 static struct sigaction huge_action_old;
90 static int huge_need_recover;
92 static void __rte_unused
93 huge_register_sigbus(void)
96 struct sigaction action;
99 sigaddset(&mask, SIGBUS);
101 action.sa_mask = mask;
102 action.sa_handler = huge_sigbus_handler;
104 huge_need_recover = !sigaction(SIGBUS, &action, &huge_action_old);
107 static void __rte_unused
108 huge_recover_sigbus(void)
110 if (huge_need_recover) {
111 sigaction(SIGBUS, &huge_action_old, NULL);
112 huge_need_recover = 0;
116 #ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
121 /* Check if kernel supports NUMA. */
122 if (numa_available() != 0) {
123 RTE_LOG(DEBUG, EAL, "NUMA is not supported.\n");
130 prepare_numa(int *oldpolicy, struct bitmask *oldmask, int socket_id)
132 RTE_LOG(DEBUG, EAL, "Trying to obtain current memory policy.\n");
133 if (get_mempolicy(oldpolicy, oldmask->maskp,
134 oldmask->size + 1, 0, 0) < 0) {
136 "Failed to get current mempolicy: %s. "
137 "Assuming MPOL_DEFAULT.\n", strerror(errno));
138 oldpolicy = MPOL_DEFAULT;
141 "Setting policy MPOL_PREFERRED for socket %d\n",
143 numa_set_preferred(socket_id);
147 restore_numa(int *oldpolicy, struct bitmask *oldmask)
150 "Restoring previous memory policy: %d\n", *oldpolicy);
151 if (*oldpolicy == MPOL_DEFAULT) {
152 numa_set_localalloc();
153 } else if (set_mempolicy(*oldpolicy, oldmask->maskp,
154 oldmask->size + 1) < 0) {
155 RTE_LOG(ERR, EAL, "Failed to restore mempolicy: %s\n",
157 numa_set_localalloc();
159 numa_free_cpumask(oldmask);
164 * uses fstat to report the size of a file on disk
167 get_file_size(int fd)
170 if (fstat(fd, &st) < 0)
175 /* returns 1 on successful lock, 0 on unsuccessful lock, -1 on error */
176 static int lock(int fd, int type)
180 /* flock may be interrupted */
182 ret = flock(fd, type | LOCK_NB);
183 } while (ret && errno == EINTR);
185 if (ret && errno == EWOULDBLOCK) {
189 RTE_LOG(ERR, EAL, "%s(): error calling flock(): %s\n",
190 __func__, strerror(errno));
193 /* lock was successful */
197 static int get_segment_lock_fd(int list_idx, int seg_idx)
199 char path[PATH_MAX] = {0};
202 if (list_idx < 0 || list_idx >= (int)RTE_DIM(lock_fds))
204 if (seg_idx < 0 || seg_idx >= lock_fds[list_idx].len)
207 fd = lock_fds[list_idx].fds[seg_idx];
208 /* does this lock already exist? */
212 eal_get_hugefile_lock_path(path, sizeof(path),
213 list_idx * RTE_MAX_MEMSEG_PER_LIST + seg_idx);
215 fd = open(path, O_CREAT | O_RDWR, 0660);
217 RTE_LOG(ERR, EAL, "%s(): error creating lockfile '%s': %s\n",
218 __func__, path, strerror(errno));
221 /* take out a read lock */
222 if (lock(fd, LOCK_SH) != 1) {
223 RTE_LOG(ERR, EAL, "%s(): failed to take out a readlock on '%s': %s\n",
224 __func__, path, strerror(errno));
228 /* store it for future reference */
229 lock_fds[list_idx].fds[seg_idx] = fd;
230 lock_fds[list_idx].count++;
234 static int unlock_segment(int list_idx, int seg_idx)
238 if (list_idx < 0 || list_idx >= (int)RTE_DIM(lock_fds))
240 if (seg_idx < 0 || seg_idx >= lock_fds[list_idx].len)
243 fd = lock_fds[list_idx].fds[seg_idx];
245 /* upgrade lock to exclusive to see if we can remove the lockfile */
246 ret = lock(fd, LOCK_EX);
248 /* we've succeeded in taking exclusive lock, this lockfile may
251 char path[PATH_MAX] = {0};
252 eal_get_hugefile_lock_path(path, sizeof(path),
253 list_idx * RTE_MAX_MEMSEG_PER_LIST + seg_idx);
255 RTE_LOG(ERR, EAL, "%s(): error removing lockfile '%s': %s\n",
256 __func__, path, strerror(errno));
259 /* we don't want to leak the fd, so even if we fail to lock, close fd
260 * and remove it from list anyway.
263 lock_fds[list_idx].fds[seg_idx] = -1;
264 lock_fds[list_idx].count--;
272 get_seg_fd(char *path, int buflen, struct hugepage_info *hi,
273 unsigned int list_idx, unsigned int seg_idx)
277 if (internal_config.single_file_segments) {
278 /* create a hugepage file path */
279 eal_get_hugefile_path(path, buflen, hi->hugedir, list_idx);
281 fd = lock_fds[list_idx].memseg_list_fd;
284 fd = open(path, O_CREAT | O_RDWR, 0600);
286 RTE_LOG(ERR, EAL, "%s(): open failed: %s\n",
287 __func__, strerror(errno));
290 /* take out a read lock and keep it indefinitely */
291 if (lock(fd, LOCK_SH) < 0) {
292 RTE_LOG(ERR, EAL, "%s(): lock failed: %s\n",
293 __func__, strerror(errno));
297 lock_fds[list_idx].memseg_list_fd = fd;
300 /* create a hugepage file path */
301 eal_get_hugefile_path(path, buflen, hi->hugedir,
302 list_idx * RTE_MAX_MEMSEG_PER_LIST + seg_idx);
303 fd = open(path, O_CREAT | O_RDWR, 0600);
305 RTE_LOG(DEBUG, EAL, "%s(): open failed: %s\n", __func__,
309 /* take out a read lock */
310 if (lock(fd, LOCK_SH) < 0) {
311 RTE_LOG(ERR, EAL, "%s(): lock failed: %s\n",
312 __func__, strerror(errno));
321 resize_hugefile(int fd, char *path, int list_idx, int seg_idx,
322 uint64_t fa_offset, uint64_t page_sz, bool grow)
326 if (fallocate_supported == 0) {
327 /* we cannot deallocate memory if fallocate() is not
328 * supported, and hugepage file is already locked at
329 * creation, so no further synchronization needed.
333 RTE_LOG(DEBUG, EAL, "%s(): fallocate not supported, not freeing page back to the system\n",
337 uint64_t new_size = fa_offset + page_sz;
338 uint64_t cur_size = get_file_size(fd);
340 /* fallocate isn't supported, fall back to ftruncate */
341 if (new_size > cur_size &&
342 ftruncate(fd, new_size) < 0) {
343 RTE_LOG(DEBUG, EAL, "%s(): ftruncate() failed: %s\n",
344 __func__, strerror(errno));
348 int flags = grow ? 0 : FALLOC_FL_PUNCH_HOLE |
352 /* if fallocate() is supported, we need to take out a
353 * read lock on allocate (to prevent other processes
354 * from deallocating this page), and take out a write
355 * lock on deallocate (to ensure nobody else is using
358 * read locks on page itself are already taken out at
359 * file creation, in get_seg_fd().
361 * we cannot rely on simple use of flock() call, because
362 * we need to be able to lock a section of the file,
363 * and we cannot use fcntl() locks, because of numerous
364 * problems with their semantics, so we will use
365 * deterministically named lock files for each section
368 * if we're shrinking the file, we want to upgrade our
369 * lock from shared to exclusive.
371 * lock_fd is an fd for a lockfile, not for the segment
374 lock_fd = get_segment_lock_fd(list_idx, seg_idx);
377 /* we are using this lockfile to determine
378 * whether this particular page is locked, as we
379 * are in single file segments mode and thus
380 * cannot use regular flock() to get this info.
382 * we want to try and take out an exclusive lock
383 * on the lock file to determine if we're the
384 * last ones using this page, and if not, we
385 * won't be shrinking it, and will instead exit
388 ret = lock(lock_fd, LOCK_EX);
390 /* drop the lock on the lockfile, so that even
391 * if we couldn't shrink the file ourselves, we
392 * are signalling to other processes that we're
393 * no longer using this page.
395 if (unlock_segment(list_idx, seg_idx))
396 RTE_LOG(ERR, EAL, "Could not unlock segment\n");
398 /* additionally, if this was the last lock on
399 * this segment list, we can safely close the
400 * page file fd, so that one of the processes
401 * could then delete the file after shrinking.
403 if (ret < 1 && lock_fds[list_idx].count == 0) {
405 lock_fds[list_idx].memseg_list_fd = -1;
409 RTE_LOG(ERR, EAL, "Could not lock segment\n");
413 /* failed to lock, not an error. */
417 /* grow or shrink the file */
418 ret = fallocate(fd, flags, fa_offset, page_sz);
421 if (fallocate_supported == -1 &&
423 RTE_LOG(ERR, EAL, "%s(): fallocate() not supported, hugepage deallocation will be disabled\n",
426 fallocate_supported = 0;
428 RTE_LOG(DEBUG, EAL, "%s(): fallocate() failed: %s\n",
434 fallocate_supported = 1;
436 /* we've grew/shrunk the file, and we hold an
437 * exclusive lock now. check if there are no
438 * more segments active in this segment list,
439 * and remove the file if there aren't.
441 if (lock_fds[list_idx].count == 0) {
443 RTE_LOG(ERR, EAL, "%s(): unlinking '%s' failed: %s\n",
447 lock_fds[list_idx].memseg_list_fd = -1;
456 alloc_seg(struct rte_memseg *ms, void *addr, int socket_id,
457 struct hugepage_info *hi, unsigned int list_idx,
458 unsigned int seg_idx)
460 #ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
461 int cur_socket_id = 0;
471 /* takes out a read lock on segment or segment list */
472 fd = get_seg_fd(path, sizeof(path), hi, list_idx, seg_idx);
474 RTE_LOG(ERR, EAL, "Couldn't get fd on hugepage file\n");
478 alloc_sz = hi->hugepage_sz;
479 if (internal_config.single_file_segments) {
480 map_offset = seg_idx * alloc_sz;
481 ret = resize_hugefile(fd, path, list_idx, seg_idx, map_offset,
487 if (ftruncate(fd, alloc_sz) < 0) {
488 RTE_LOG(DEBUG, EAL, "%s(): ftruncate() failed: %s\n",
489 __func__, strerror(errno));
495 * map the segment, and populate page tables, the kernel fills this
496 * segment with zeros if it's a new page.
498 void *va = mmap(addr, alloc_sz, PROT_READ | PROT_WRITE,
499 MAP_SHARED | MAP_POPULATE | MAP_FIXED, fd, map_offset);
501 if (va == MAP_FAILED) {
502 RTE_LOG(DEBUG, EAL, "%s(): mmap() failed: %s\n", __func__,
504 /* mmap failed, but the previous region might have been
505 * unmapped anyway. try to remap it
510 RTE_LOG(DEBUG, EAL, "%s(): wrong mmap() address\n", __func__);
511 munmap(va, alloc_sz);
515 rte_iova_t iova = rte_mem_virt2iova(addr);
516 if (iova == RTE_BAD_PHYS_ADDR) {
517 RTE_LOG(DEBUG, EAL, "%s(): can't get IOVA addr\n",
522 #ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
523 move_pages(getpid(), 1, &addr, NULL, &cur_socket_id, 0);
525 if (cur_socket_id != socket_id) {
527 "%s(): allocation happened on wrong socket (wanted %d, got %d)\n",
528 __func__, socket_id, cur_socket_id);
533 /* In linux, hugetlb limitations, like cgroup, are
534 * enforced at fault time instead of mmap(), even
535 * with the option of MAP_POPULATE. Kernel will send
536 * a SIGBUS signal. To avoid to be killed, save stack
537 * environment here, if SIGBUS happens, we can jump
540 if (huge_wrap_sigsetjmp()) {
541 RTE_LOG(DEBUG, EAL, "SIGBUS: Cannot mmap more hugepages of size %uMB\n",
542 (unsigned int)(alloc_sz >> 20));
545 /* for non-single file segments, we can close fd here */
546 if (!internal_config.single_file_segments)
549 /* we need to trigger a write to the page to enforce page fault and
550 * ensure that page is accessible to us, but we can't overwrite value
551 * that is already there, so read the old value, and write itback.
552 * kernel populates the page with zeroes initially.
554 *(volatile int *)addr = *(volatile int *)addr;
557 ms->hugepage_sz = alloc_sz;
559 ms->nchannel = rte_memory_get_nchannel();
560 ms->nrank = rte_memory_get_nrank();
562 ms->socket_id = socket_id;
567 munmap(addr, alloc_sz);
570 #ifdef RTE_ARCH_PPC_64
571 flags |= MAP_HUGETLB;
573 new_addr = eal_get_virtual_area(addr, &alloc_sz, alloc_sz, 0, flags);
574 if (new_addr != addr) {
575 if (new_addr != NULL)
576 munmap(new_addr, alloc_sz);
577 /* we're leaving a hole in our virtual address space. if
578 * somebody else maps this hole now, we could accidentally
579 * override it in the future.
581 RTE_LOG(CRIT, EAL, "Can't mmap holes in our virtual address space\n");
584 if (internal_config.single_file_segments) {
585 resize_hugefile(fd, path, list_idx, seg_idx, map_offset,
587 /* ignore failure, can't make it any worse */
589 /* only remove file if we can take out a write lock */
590 if (lock(fd, LOCK_EX) == 1)
598 free_seg(struct rte_memseg *ms, struct hugepage_info *hi,
599 unsigned int list_idx, unsigned int seg_idx)
605 /* erase page data */
606 memset(ms->addr, 0, ms->len);
608 if (mmap(ms->addr, ms->len, PROT_READ,
609 MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0) ==
611 RTE_LOG(DEBUG, EAL, "couldn't unmap page\n");
615 /* if we are not in single file segments mode, we're going to unmap the
616 * segment and thus drop the lock on original fd, but hugepage dir is
617 * now locked so we can take out another one without races.
619 fd = get_seg_fd(path, sizeof(path), hi, list_idx, seg_idx);
623 if (internal_config.single_file_segments) {
624 map_offset = seg_idx * ms->len;
625 if (resize_hugefile(fd, path, list_idx, seg_idx, map_offset,
630 /* if we're able to take out a write lock, we're the last one
631 * holding onto this page.
633 ret = lock(fd, LOCK_EX);
635 /* no one else is using this page */
639 /* closing fd will drop the lock */
643 memset(ms, 0, sizeof(*ms));
645 return ret < 0 ? -1 : 0;
648 struct alloc_walk_param {
649 struct hugepage_info *hi;
650 struct rte_memseg **ms;
652 unsigned int segs_allocated;
658 alloc_seg_walk(const struct rte_memseg_list *msl, void *arg)
660 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
661 struct alloc_walk_param *wa = arg;
662 struct rte_memseg_list *cur_msl;
664 int cur_idx, start_idx, j, dir_fd = -1;
665 unsigned int msl_idx, need, i;
667 if (msl->page_sz != wa->page_sz)
669 if (msl->socket_id != wa->socket)
672 page_sz = (size_t)msl->page_sz;
674 msl_idx = msl - mcfg->memsegs;
675 cur_msl = &mcfg->memsegs[msl_idx];
679 /* try finding space in memseg list */
680 cur_idx = rte_fbarray_find_prev_n_free(&cur_msl->memseg_arr,
681 cur_msl->memseg_arr.len - 1, need);
686 /* do not allow any page allocations during the time we're allocating,
687 * because file creation and locking operations are not atomic,
688 * and we might be the first or the last ones to use a particular page,
689 * so we need to ensure atomicity of every operation.
691 * during init, we already hold a write lock, so don't try to take out
694 if (wa->hi->lock_descriptor == -1) {
695 dir_fd = open(wa->hi->hugedir, O_RDONLY);
697 RTE_LOG(ERR, EAL, "%s(): Cannot open '%s': %s\n",
698 __func__, wa->hi->hugedir, strerror(errno));
701 /* blocking writelock */
702 if (flock(dir_fd, LOCK_EX)) {
703 RTE_LOG(ERR, EAL, "%s(): Cannot lock '%s': %s\n",
704 __func__, wa->hi->hugedir, strerror(errno));
710 for (i = 0; i < need; i++, cur_idx++) {
711 struct rte_memseg *cur;
714 cur = rte_fbarray_get(&cur_msl->memseg_arr, cur_idx);
715 map_addr = RTE_PTR_ADD(cur_msl->base_va,
718 if (alloc_seg(cur, map_addr, wa->socket, wa->hi,
720 RTE_LOG(DEBUG, EAL, "attempted to allocate %i segments, but only %i were allocated\n",
723 /* if exact number wasn't requested, stop */
728 for (j = start_idx; j < cur_idx; j++) {
729 struct rte_memseg *tmp;
730 struct rte_fbarray *arr =
731 &cur_msl->memseg_arr;
733 tmp = rte_fbarray_get(arr, j);
734 rte_fbarray_set_free(arr, j);
736 /* free_seg may attempt to create a file, which
739 if (free_seg(tmp, wa->hi, msl_idx, j))
740 RTE_LOG(DEBUG, EAL, "Cannot free page\n");
744 memset(wa->ms, 0, sizeof(*wa->ms) * wa->n_segs);
753 rte_fbarray_set_used(&cur_msl->memseg_arr, cur_idx);
756 wa->segs_allocated = i;
764 struct free_walk_param {
765 struct hugepage_info *hi;
766 struct rte_memseg *ms;
769 free_seg_walk(const struct rte_memseg_list *msl, void *arg)
771 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
772 struct rte_memseg_list *found_msl;
773 struct free_walk_param *wa = arg;
774 uintptr_t start_addr, end_addr;
775 int msl_idx, seg_idx, ret, dir_fd = -1;
777 start_addr = (uintptr_t) msl->base_va;
778 end_addr = start_addr + msl->memseg_arr.len * (size_t)msl->page_sz;
780 if ((uintptr_t)wa->ms->addr < start_addr ||
781 (uintptr_t)wa->ms->addr >= end_addr)
784 msl_idx = msl - mcfg->memsegs;
785 seg_idx = RTE_PTR_DIFF(wa->ms->addr, start_addr) / msl->page_sz;
788 found_msl = &mcfg->memsegs[msl_idx];
790 /* do not allow any page allocations during the time we're freeing,
791 * because file creation and locking operations are not atomic,
792 * and we might be the first or the last ones to use a particular page,
793 * so we need to ensure atomicity of every operation.
795 * during init, we already hold a write lock, so don't try to take out
798 if (wa->hi->lock_descriptor == -1) {
799 dir_fd = open(wa->hi->hugedir, O_RDONLY);
801 RTE_LOG(ERR, EAL, "%s(): Cannot open '%s': %s\n",
802 __func__, wa->hi->hugedir, strerror(errno));
805 /* blocking writelock */
806 if (flock(dir_fd, LOCK_EX)) {
807 RTE_LOG(ERR, EAL, "%s(): Cannot lock '%s': %s\n",
808 __func__, wa->hi->hugedir, strerror(errno));
814 found_msl->version++;
816 rte_fbarray_set_free(&found_msl->memseg_arr, seg_idx);
818 ret = free_seg(wa->ms, wa->hi, msl_idx, seg_idx);
830 eal_memalloc_alloc_seg_bulk(struct rte_memseg **ms, int n_segs, size_t page_sz,
831 int socket, bool exact)
834 #ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
835 bool have_numa = false;
837 struct bitmask *oldmask;
839 struct alloc_walk_param wa;
840 struct hugepage_info *hi = NULL;
842 memset(&wa, 0, sizeof(wa));
844 /* dynamic allocation not supported in legacy mode */
845 if (internal_config.legacy_mem)
848 for (i = 0; i < (int) RTE_DIM(internal_config.hugepage_info); i++) {
850 internal_config.hugepage_info[i].hugepage_sz) {
851 hi = &internal_config.hugepage_info[i];
856 RTE_LOG(ERR, EAL, "%s(): can't find relevant hugepage_info entry\n",
861 #ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
863 oldmask = numa_allocate_nodemask();
864 prepare_numa(&oldpolicy, oldmask, socket);
873 wa.page_sz = page_sz;
875 wa.segs_allocated = 0;
877 /* memalloc is locked, so it's safe to use thread-unsafe version */
878 ret = rte_memseg_list_walk_thread_unsafe(alloc_seg_walk, &wa);
880 RTE_LOG(ERR, EAL, "%s(): couldn't find suitable memseg_list\n",
883 } else if (ret > 0) {
884 ret = (int)wa.segs_allocated;
887 #ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
889 restore_numa(&oldpolicy, oldmask);
895 eal_memalloc_alloc_seg(size_t page_sz, int socket)
897 struct rte_memseg *ms;
898 if (eal_memalloc_alloc_seg_bulk(&ms, 1, page_sz, socket, true) < 0)
900 /* return pointer to newly allocated memseg */
905 eal_memalloc_free_seg_bulk(struct rte_memseg **ms, int n_segs)
909 /* dynamic free not supported in legacy mode */
910 if (internal_config.legacy_mem)
913 for (seg = 0; seg < n_segs; seg++) {
914 struct rte_memseg *cur = ms[seg];
915 struct hugepage_info *hi = NULL;
916 struct free_walk_param wa;
919 /* if this page is marked as unfreeable, fail */
920 if (cur->flags & RTE_MEMSEG_FLAG_DO_NOT_FREE) {
921 RTE_LOG(DEBUG, EAL, "Page is not allowed to be freed\n");
926 memset(&wa, 0, sizeof(wa));
928 for (i = 0; i < (int)RTE_DIM(internal_config.hugepage_info);
930 hi = &internal_config.hugepage_info[i];
931 if (cur->hugepage_sz == hi->hugepage_sz)
934 if (i == (int)RTE_DIM(internal_config.hugepage_info)) {
935 RTE_LOG(ERR, EAL, "Can't find relevant hugepage_info entry\n");
943 /* memalloc is locked, so it's safe to use thread-unsafe version
945 walk_res = rte_memseg_list_walk_thread_unsafe(free_seg_walk,
950 RTE_LOG(ERR, EAL, "Couldn't find memseg list\n");
957 eal_memalloc_free_seg(struct rte_memseg *ms)
959 /* dynamic free not supported in legacy mode */
960 if (internal_config.legacy_mem)
963 return eal_memalloc_free_seg_bulk(&ms, 1);
967 sync_chunk(struct rte_memseg_list *primary_msl,
968 struct rte_memseg_list *local_msl, struct hugepage_info *hi,
969 unsigned int msl_idx, bool used, int start, int end)
971 struct rte_fbarray *l_arr, *p_arr;
972 int i, ret, chunk_len, diff_len;
974 l_arr = &local_msl->memseg_arr;
975 p_arr = &primary_msl->memseg_arr;
977 /* we need to aggregate allocations/deallocations into bigger chunks,
978 * as we don't want to spam the user with per-page callbacks.
980 * to avoid any potential issues, we also want to trigger
981 * deallocation callbacks *before* we actually deallocate
982 * memory, so that the user application could wrap up its use
983 * before it goes away.
986 chunk_len = end - start;
988 /* find how many contiguous pages we can map/unmap for this chunk */
990 rte_fbarray_find_contig_free(l_arr, start) :
991 rte_fbarray_find_contig_used(l_arr, start);
993 /* has to be at least one page */
997 diff_len = RTE_MIN(chunk_len, diff_len);
999 /* if we are freeing memory, notify the application */
1001 struct rte_memseg *ms;
1003 size_t len, page_sz;
1005 ms = rte_fbarray_get(l_arr, start);
1006 start_va = ms->addr;
1007 page_sz = (size_t)primary_msl->page_sz;
1008 len = page_sz * diff_len;
1010 eal_memalloc_mem_event_notify(RTE_MEM_EVENT_FREE,
1014 for (i = 0; i < diff_len; i++) {
1015 struct rte_memseg *p_ms, *l_ms;
1016 int seg_idx = start + i;
1018 l_ms = rte_fbarray_get(l_arr, seg_idx);
1019 p_ms = rte_fbarray_get(p_arr, seg_idx);
1021 if (l_ms == NULL || p_ms == NULL)
1025 ret = alloc_seg(l_ms, p_ms->addr,
1026 p_ms->socket_id, hi,
1030 rte_fbarray_set_used(l_arr, seg_idx);
1032 ret = free_seg(l_ms, hi, msl_idx, seg_idx);
1033 rte_fbarray_set_free(l_arr, seg_idx);
1039 /* if we just allocated memory, notify the application */
1041 struct rte_memseg *ms;
1043 size_t len, page_sz;
1045 ms = rte_fbarray_get(l_arr, start);
1046 start_va = ms->addr;
1047 page_sz = (size_t)primary_msl->page_sz;
1048 len = page_sz * diff_len;
1050 eal_memalloc_mem_event_notify(RTE_MEM_EVENT_ALLOC,
1054 /* calculate how much we can advance until next chunk */
1056 rte_fbarray_find_contig_used(l_arr, start) :
1057 rte_fbarray_find_contig_free(l_arr, start);
1058 ret = RTE_MIN(chunk_len, diff_len);
1064 sync_status(struct rte_memseg_list *primary_msl,
1065 struct rte_memseg_list *local_msl, struct hugepage_info *hi,
1066 unsigned int msl_idx, bool used)
1068 struct rte_fbarray *l_arr, *p_arr;
1069 int p_idx, l_chunk_len, p_chunk_len, ret;
1072 /* this is a little bit tricky, but the basic idea is - walk both lists
1073 * and spot any places where there are discrepancies. walking both lists
1074 * and noting discrepancies in a single go is a hard problem, so we do
1075 * it in two passes - first we spot any places where allocated segments
1076 * mismatch (i.e. ensure that everything that's allocated in the primary
1077 * is also allocated in the secondary), and then we do it by looking at
1078 * free segments instead.
1080 * we also need to aggregate changes into chunks, as we have to call
1081 * callbacks per allocation, not per page.
1083 l_arr = &local_msl->memseg_arr;
1084 p_arr = &primary_msl->memseg_arr;
1087 p_idx = rte_fbarray_find_next_used(p_arr, 0);
1089 p_idx = rte_fbarray_find_next_free(p_arr, 0);
1091 while (p_idx >= 0) {
1092 int next_chunk_search_idx;
1095 p_chunk_len = rte_fbarray_find_contig_used(p_arr,
1097 l_chunk_len = rte_fbarray_find_contig_used(l_arr,
1100 p_chunk_len = rte_fbarray_find_contig_free(p_arr,
1102 l_chunk_len = rte_fbarray_find_contig_free(l_arr,
1105 /* best case scenario - no differences (or bigger, which will be
1106 * fixed during next iteration), look for next chunk
1108 if (l_chunk_len >= p_chunk_len) {
1109 next_chunk_search_idx = p_idx + p_chunk_len;
1113 /* if both chunks start at the same point, skip parts we know
1114 * are identical, and sync the rest. each call to sync_chunk
1115 * will only sync contiguous segments, so we need to call this
1116 * until we are sure there are no more differences in this
1119 start = p_idx + l_chunk_len;
1120 end = p_idx + p_chunk_len;
1122 ret = sync_chunk(primary_msl, local_msl, hi, msl_idx,
1125 } while (start < end && ret >= 0);
1126 /* if ret is negative, something went wrong */
1130 next_chunk_search_idx = p_idx + p_chunk_len;
1132 /* skip to end of this chunk */
1134 p_idx = rte_fbarray_find_next_used(p_arr,
1135 next_chunk_search_idx);
1137 p_idx = rte_fbarray_find_next_free(p_arr,
1138 next_chunk_search_idx);
1145 sync_existing(struct rte_memseg_list *primary_msl,
1146 struct rte_memseg_list *local_msl, struct hugepage_info *hi,
1147 unsigned int msl_idx)
1151 /* do not allow any page allocations during the time we're allocating,
1152 * because file creation and locking operations are not atomic,
1153 * and we might be the first or the last ones to use a particular page,
1154 * so we need to ensure atomicity of every operation.
1156 dir_fd = open(hi->hugedir, O_RDONLY);
1158 RTE_LOG(ERR, EAL, "%s(): Cannot open '%s': %s\n", __func__,
1159 hi->hugedir, strerror(errno));
1162 /* blocking writelock */
1163 if (flock(dir_fd, LOCK_EX)) {
1164 RTE_LOG(ERR, EAL, "%s(): Cannot lock '%s': %s\n", __func__,
1165 hi->hugedir, strerror(errno));
1170 /* ensure all allocated space is the same in both lists */
1171 ret = sync_status(primary_msl, local_msl, hi, msl_idx, true);
1175 /* ensure all unallocated space is the same in both lists */
1176 ret = sync_status(primary_msl, local_msl, hi, msl_idx, false);
1180 /* update version number */
1181 local_msl->version = primary_msl->version;
1192 sync_walk(const struct rte_memseg_list *msl, void *arg __rte_unused)
1194 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
1195 struct rte_memseg_list *primary_msl, *local_msl;
1196 struct hugepage_info *hi = NULL;
1200 msl_idx = msl - mcfg->memsegs;
1201 primary_msl = &mcfg->memsegs[msl_idx];
1202 local_msl = &local_memsegs[msl_idx];
1204 for (i = 0; i < RTE_DIM(internal_config.hugepage_info); i++) {
1206 internal_config.hugepage_info[i].hugepage_sz;
1207 uint64_t msl_sz = primary_msl->page_sz;
1208 if (msl_sz == cur_sz) {
1209 hi = &internal_config.hugepage_info[i];
1214 RTE_LOG(ERR, EAL, "Can't find relevant hugepage_info entry\n");
1218 /* if versions don't match, synchronize everything */
1219 if (local_msl->version != primary_msl->version &&
1220 sync_existing(primary_msl, local_msl, hi, msl_idx))
1227 eal_memalloc_sync_with_primary(void)
1229 /* nothing to be done in primary */
1230 if (rte_eal_process_type() == RTE_PROC_PRIMARY)
1233 /* memalloc is locked, so it's safe to call thread-unsafe version */
1234 if (rte_memseg_list_walk_thread_unsafe(sync_walk, NULL))
1240 secondary_msl_create_walk(const struct rte_memseg_list *msl,
1241 void *arg __rte_unused)
1243 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
1244 struct rte_memseg_list *primary_msl, *local_msl;
1245 char name[PATH_MAX];
1248 msl_idx = msl - mcfg->memsegs;
1249 primary_msl = &mcfg->memsegs[msl_idx];
1250 local_msl = &local_memsegs[msl_idx];
1252 /* create distinct fbarrays for each secondary */
1253 snprintf(name, RTE_FBARRAY_NAME_LEN, "%s_%i",
1254 primary_msl->memseg_arr.name, getpid());
1256 ret = rte_fbarray_init(&local_msl->memseg_arr, name,
1257 primary_msl->memseg_arr.len,
1258 primary_msl->memseg_arr.elt_sz);
1260 RTE_LOG(ERR, EAL, "Cannot initialize local memory map\n");
1263 local_msl->base_va = primary_msl->base_va;
1269 secondary_lock_list_create_walk(const struct rte_memseg_list *msl,
1270 void *arg __rte_unused)
1272 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
1273 unsigned int i, len;
1277 msl_idx = msl - mcfg->memsegs;
1278 len = msl->memseg_arr.len;
1280 /* ensure we have space to store lock fd per each possible segment */
1281 data = malloc(sizeof(int) * len);
1283 RTE_LOG(ERR, EAL, "Unable to allocate space for lock descriptors\n");
1286 /* set all fd's as invalid */
1287 for (i = 0; i < len; i++)
1290 lock_fds[msl_idx].fds = data;
1291 lock_fds[msl_idx].len = len;
1292 lock_fds[msl_idx].count = 0;
1293 lock_fds[msl_idx].memseg_list_fd = -1;
1299 eal_memalloc_init(void)
1301 if (rte_eal_process_type() == RTE_PROC_SECONDARY)
1302 if (rte_memseg_list_walk(secondary_msl_create_walk, NULL) < 0)
1305 /* initialize all of the lock fd lists */
1306 if (internal_config.single_file_segments)
1307 if (rte_memseg_list_walk(secondary_lock_list_create_walk, NULL))