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));
492 if (internal_config.hugepage_unlink) {
494 RTE_LOG(DEBUG, EAL, "%s(): unlink() failed: %s\n",
495 __func__, strerror(errno));
502 * map the segment, and populate page tables, the kernel fills this
503 * segment with zeros if it's a new page.
505 void *va = mmap(addr, alloc_sz, PROT_READ | PROT_WRITE,
506 MAP_SHARED | MAP_POPULATE | MAP_FIXED, fd, map_offset);
508 if (va == MAP_FAILED) {
509 RTE_LOG(DEBUG, EAL, "%s(): mmap() failed: %s\n", __func__,
511 /* mmap failed, but the previous region might have been
512 * unmapped anyway. try to remap it
517 RTE_LOG(DEBUG, EAL, "%s(): wrong mmap() address\n", __func__);
518 munmap(va, alloc_sz);
522 rte_iova_t iova = rte_mem_virt2iova(addr);
523 if (iova == RTE_BAD_PHYS_ADDR) {
524 RTE_LOG(DEBUG, EAL, "%s(): can't get IOVA addr\n",
529 #ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
530 move_pages(getpid(), 1, &addr, NULL, &cur_socket_id, 0);
532 if (cur_socket_id != socket_id) {
534 "%s(): allocation happened on wrong socket (wanted %d, got %d)\n",
535 __func__, socket_id, cur_socket_id);
540 /* In linux, hugetlb limitations, like cgroup, are
541 * enforced at fault time instead of mmap(), even
542 * with the option of MAP_POPULATE. Kernel will send
543 * a SIGBUS signal. To avoid to be killed, save stack
544 * environment here, if SIGBUS happens, we can jump
547 if (huge_wrap_sigsetjmp()) {
548 RTE_LOG(DEBUG, EAL, "SIGBUS: Cannot mmap more hugepages of size %uMB\n",
549 (unsigned int)(alloc_sz >> 20));
552 /* for non-single file segments, we can close fd here */
553 if (!internal_config.single_file_segments)
556 /* we need to trigger a write to the page to enforce page fault and
557 * ensure that page is accessible to us, but we can't overwrite value
558 * that is already there, so read the old value, and write itback.
559 * kernel populates the page with zeroes initially.
561 *(volatile int *)addr = *(volatile int *)addr;
564 ms->hugepage_sz = alloc_sz;
566 ms->nchannel = rte_memory_get_nchannel();
567 ms->nrank = rte_memory_get_nrank();
569 ms->socket_id = socket_id;
574 munmap(addr, alloc_sz);
577 #ifdef RTE_ARCH_PPC_64
578 flags |= MAP_HUGETLB;
580 new_addr = eal_get_virtual_area(addr, &alloc_sz, alloc_sz, 0, flags);
581 if (new_addr != addr) {
582 if (new_addr != NULL)
583 munmap(new_addr, alloc_sz);
584 /* we're leaving a hole in our virtual address space. if
585 * somebody else maps this hole now, we could accidentally
586 * override it in the future.
588 RTE_LOG(CRIT, EAL, "Can't mmap holes in our virtual address space\n");
591 if (internal_config.single_file_segments) {
592 resize_hugefile(fd, path, list_idx, seg_idx, map_offset,
594 /* ignore failure, can't make it any worse */
596 /* only remove file if we can take out a write lock */
597 if (internal_config.hugepage_unlink == 0 &&
598 lock(fd, LOCK_EX) == 1)
606 free_seg(struct rte_memseg *ms, struct hugepage_info *hi,
607 unsigned int list_idx, unsigned int seg_idx)
613 /* erase page data */
614 memset(ms->addr, 0, ms->len);
616 if (mmap(ms->addr, ms->len, PROT_READ,
617 MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0) ==
619 RTE_LOG(DEBUG, EAL, "couldn't unmap page\n");
623 /* if we've already unlinked the page, nothing needs to be done */
624 if (internal_config.hugepage_unlink) {
625 memset(ms, 0, sizeof(*ms));
629 /* if we are not in single file segments mode, we're going to unmap the
630 * segment and thus drop the lock on original fd, but hugepage dir is
631 * now locked so we can take out another one without races.
633 fd = get_seg_fd(path, sizeof(path), hi, list_idx, seg_idx);
637 if (internal_config.single_file_segments) {
638 map_offset = seg_idx * ms->len;
639 if (resize_hugefile(fd, path, list_idx, seg_idx, map_offset,
644 /* if we're able to take out a write lock, we're the last one
645 * holding onto this page.
647 ret = lock(fd, LOCK_EX);
649 /* no one else is using this page */
653 /* closing fd will drop the lock */
657 memset(ms, 0, sizeof(*ms));
659 return ret < 0 ? -1 : 0;
662 struct alloc_walk_param {
663 struct hugepage_info *hi;
664 struct rte_memseg **ms;
666 unsigned int segs_allocated;
672 alloc_seg_walk(const struct rte_memseg_list *msl, void *arg)
674 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
675 struct alloc_walk_param *wa = arg;
676 struct rte_memseg_list *cur_msl;
678 int cur_idx, start_idx, j, dir_fd = -1;
679 unsigned int msl_idx, need, i;
681 if (msl->page_sz != wa->page_sz)
683 if (msl->socket_id != wa->socket)
686 page_sz = (size_t)msl->page_sz;
688 msl_idx = msl - mcfg->memsegs;
689 cur_msl = &mcfg->memsegs[msl_idx];
693 /* try finding space in memseg list */
694 cur_idx = rte_fbarray_find_prev_n_free(&cur_msl->memseg_arr,
695 cur_msl->memseg_arr.len - 1, need);
700 /* do not allow any page allocations during the time we're allocating,
701 * because file creation and locking operations are not atomic,
702 * and we might be the first or the last ones to use a particular page,
703 * so we need to ensure atomicity of every operation.
705 * during init, we already hold a write lock, so don't try to take out
708 if (wa->hi->lock_descriptor == -1) {
709 dir_fd = open(wa->hi->hugedir, O_RDONLY);
711 RTE_LOG(ERR, EAL, "%s(): Cannot open '%s': %s\n",
712 __func__, wa->hi->hugedir, strerror(errno));
715 /* blocking writelock */
716 if (flock(dir_fd, LOCK_EX)) {
717 RTE_LOG(ERR, EAL, "%s(): Cannot lock '%s': %s\n",
718 __func__, wa->hi->hugedir, strerror(errno));
724 for (i = 0; i < need; i++, cur_idx++) {
725 struct rte_memseg *cur;
728 cur = rte_fbarray_get(&cur_msl->memseg_arr, cur_idx);
729 map_addr = RTE_PTR_ADD(cur_msl->base_va,
732 if (alloc_seg(cur, map_addr, wa->socket, wa->hi,
734 RTE_LOG(DEBUG, EAL, "attempted to allocate %i segments, but only %i were allocated\n",
737 /* if exact number wasn't requested, stop */
742 for (j = start_idx; j < cur_idx; j++) {
743 struct rte_memseg *tmp;
744 struct rte_fbarray *arr =
745 &cur_msl->memseg_arr;
747 tmp = rte_fbarray_get(arr, j);
748 rte_fbarray_set_free(arr, j);
750 /* free_seg may attempt to create a file, which
753 if (free_seg(tmp, wa->hi, msl_idx, j))
754 RTE_LOG(DEBUG, EAL, "Cannot free page\n");
758 memset(wa->ms, 0, sizeof(*wa->ms) * wa->n_segs);
767 rte_fbarray_set_used(&cur_msl->memseg_arr, cur_idx);
770 wa->segs_allocated = i;
778 struct free_walk_param {
779 struct hugepage_info *hi;
780 struct rte_memseg *ms;
783 free_seg_walk(const struct rte_memseg_list *msl, void *arg)
785 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
786 struct rte_memseg_list *found_msl;
787 struct free_walk_param *wa = arg;
788 uintptr_t start_addr, end_addr;
789 int msl_idx, seg_idx, ret, dir_fd = -1;
791 start_addr = (uintptr_t) msl->base_va;
792 end_addr = start_addr + msl->memseg_arr.len * (size_t)msl->page_sz;
794 if ((uintptr_t)wa->ms->addr < start_addr ||
795 (uintptr_t)wa->ms->addr >= end_addr)
798 msl_idx = msl - mcfg->memsegs;
799 seg_idx = RTE_PTR_DIFF(wa->ms->addr, start_addr) / msl->page_sz;
802 found_msl = &mcfg->memsegs[msl_idx];
804 /* do not allow any page allocations during the time we're freeing,
805 * because file creation and locking operations are not atomic,
806 * and we might be the first or the last ones to use a particular page,
807 * so we need to ensure atomicity of every operation.
809 * during init, we already hold a write lock, so don't try to take out
812 if (wa->hi->lock_descriptor == -1) {
813 dir_fd = open(wa->hi->hugedir, O_RDONLY);
815 RTE_LOG(ERR, EAL, "%s(): Cannot open '%s': %s\n",
816 __func__, wa->hi->hugedir, strerror(errno));
819 /* blocking writelock */
820 if (flock(dir_fd, LOCK_EX)) {
821 RTE_LOG(ERR, EAL, "%s(): Cannot lock '%s': %s\n",
822 __func__, wa->hi->hugedir, strerror(errno));
828 found_msl->version++;
830 rte_fbarray_set_free(&found_msl->memseg_arr, seg_idx);
832 ret = free_seg(wa->ms, wa->hi, msl_idx, seg_idx);
844 eal_memalloc_alloc_seg_bulk(struct rte_memseg **ms, int n_segs, size_t page_sz,
845 int socket, bool exact)
848 #ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
849 bool have_numa = false;
851 struct bitmask *oldmask;
853 struct alloc_walk_param wa;
854 struct hugepage_info *hi = NULL;
856 memset(&wa, 0, sizeof(wa));
858 /* dynamic allocation not supported in legacy mode */
859 if (internal_config.legacy_mem)
862 for (i = 0; i < (int) RTE_DIM(internal_config.hugepage_info); i++) {
864 internal_config.hugepage_info[i].hugepage_sz) {
865 hi = &internal_config.hugepage_info[i];
870 RTE_LOG(ERR, EAL, "%s(): can't find relevant hugepage_info entry\n",
875 #ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
877 oldmask = numa_allocate_nodemask();
878 prepare_numa(&oldpolicy, oldmask, socket);
887 wa.page_sz = page_sz;
889 wa.segs_allocated = 0;
891 /* memalloc is locked, so it's safe to use thread-unsafe version */
892 ret = rte_memseg_list_walk_thread_unsafe(alloc_seg_walk, &wa);
894 RTE_LOG(ERR, EAL, "%s(): couldn't find suitable memseg_list\n",
897 } else if (ret > 0) {
898 ret = (int)wa.segs_allocated;
901 #ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
903 restore_numa(&oldpolicy, oldmask);
909 eal_memalloc_alloc_seg(size_t page_sz, int socket)
911 struct rte_memseg *ms;
912 if (eal_memalloc_alloc_seg_bulk(&ms, 1, page_sz, socket, true) < 0)
914 /* return pointer to newly allocated memseg */
919 eal_memalloc_free_seg_bulk(struct rte_memseg **ms, int n_segs)
923 /* dynamic free not supported in legacy mode */
924 if (internal_config.legacy_mem)
927 for (seg = 0; seg < n_segs; seg++) {
928 struct rte_memseg *cur = ms[seg];
929 struct hugepage_info *hi = NULL;
930 struct free_walk_param wa;
933 /* if this page is marked as unfreeable, fail */
934 if (cur->flags & RTE_MEMSEG_FLAG_DO_NOT_FREE) {
935 RTE_LOG(DEBUG, EAL, "Page is not allowed to be freed\n");
940 memset(&wa, 0, sizeof(wa));
942 for (i = 0; i < (int)RTE_DIM(internal_config.hugepage_info);
944 hi = &internal_config.hugepage_info[i];
945 if (cur->hugepage_sz == hi->hugepage_sz)
948 if (i == (int)RTE_DIM(internal_config.hugepage_info)) {
949 RTE_LOG(ERR, EAL, "Can't find relevant hugepage_info entry\n");
957 /* memalloc is locked, so it's safe to use thread-unsafe version
959 walk_res = rte_memseg_list_walk_thread_unsafe(free_seg_walk,
964 RTE_LOG(ERR, EAL, "Couldn't find memseg list\n");
971 eal_memalloc_free_seg(struct rte_memseg *ms)
973 /* dynamic free not supported in legacy mode */
974 if (internal_config.legacy_mem)
977 return eal_memalloc_free_seg_bulk(&ms, 1);
981 sync_chunk(struct rte_memseg_list *primary_msl,
982 struct rte_memseg_list *local_msl, struct hugepage_info *hi,
983 unsigned int msl_idx, bool used, int start, int end)
985 struct rte_fbarray *l_arr, *p_arr;
986 int i, ret, chunk_len, diff_len;
988 l_arr = &local_msl->memseg_arr;
989 p_arr = &primary_msl->memseg_arr;
991 /* we need to aggregate allocations/deallocations into bigger chunks,
992 * as we don't want to spam the user with per-page callbacks.
994 * to avoid any potential issues, we also want to trigger
995 * deallocation callbacks *before* we actually deallocate
996 * memory, so that the user application could wrap up its use
997 * before it goes away.
1000 chunk_len = end - start;
1002 /* find how many contiguous pages we can map/unmap for this chunk */
1004 rte_fbarray_find_contig_free(l_arr, start) :
1005 rte_fbarray_find_contig_used(l_arr, start);
1007 /* has to be at least one page */
1011 diff_len = RTE_MIN(chunk_len, diff_len);
1013 /* if we are freeing memory, notify the application */
1015 struct rte_memseg *ms;
1017 size_t len, page_sz;
1019 ms = rte_fbarray_get(l_arr, start);
1020 start_va = ms->addr;
1021 page_sz = (size_t)primary_msl->page_sz;
1022 len = page_sz * diff_len;
1024 eal_memalloc_mem_event_notify(RTE_MEM_EVENT_FREE,
1028 for (i = 0; i < diff_len; i++) {
1029 struct rte_memseg *p_ms, *l_ms;
1030 int seg_idx = start + i;
1032 l_ms = rte_fbarray_get(l_arr, seg_idx);
1033 p_ms = rte_fbarray_get(p_arr, seg_idx);
1035 if (l_ms == NULL || p_ms == NULL)
1039 ret = alloc_seg(l_ms, p_ms->addr,
1040 p_ms->socket_id, hi,
1044 rte_fbarray_set_used(l_arr, seg_idx);
1046 ret = free_seg(l_ms, hi, msl_idx, seg_idx);
1047 rte_fbarray_set_free(l_arr, seg_idx);
1053 /* if we just allocated memory, notify the application */
1055 struct rte_memseg *ms;
1057 size_t len, page_sz;
1059 ms = rte_fbarray_get(l_arr, start);
1060 start_va = ms->addr;
1061 page_sz = (size_t)primary_msl->page_sz;
1062 len = page_sz * diff_len;
1064 eal_memalloc_mem_event_notify(RTE_MEM_EVENT_ALLOC,
1068 /* calculate how much we can advance until next chunk */
1070 rte_fbarray_find_contig_used(l_arr, start) :
1071 rte_fbarray_find_contig_free(l_arr, start);
1072 ret = RTE_MIN(chunk_len, diff_len);
1078 sync_status(struct rte_memseg_list *primary_msl,
1079 struct rte_memseg_list *local_msl, struct hugepage_info *hi,
1080 unsigned int msl_idx, bool used)
1082 struct rte_fbarray *l_arr, *p_arr;
1083 int p_idx, l_chunk_len, p_chunk_len, ret;
1086 /* this is a little bit tricky, but the basic idea is - walk both lists
1087 * and spot any places where there are discrepancies. walking both lists
1088 * and noting discrepancies in a single go is a hard problem, so we do
1089 * it in two passes - first we spot any places where allocated segments
1090 * mismatch (i.e. ensure that everything that's allocated in the primary
1091 * is also allocated in the secondary), and then we do it by looking at
1092 * free segments instead.
1094 * we also need to aggregate changes into chunks, as we have to call
1095 * callbacks per allocation, not per page.
1097 l_arr = &local_msl->memseg_arr;
1098 p_arr = &primary_msl->memseg_arr;
1101 p_idx = rte_fbarray_find_next_used(p_arr, 0);
1103 p_idx = rte_fbarray_find_next_free(p_arr, 0);
1105 while (p_idx >= 0) {
1106 int next_chunk_search_idx;
1109 p_chunk_len = rte_fbarray_find_contig_used(p_arr,
1111 l_chunk_len = rte_fbarray_find_contig_used(l_arr,
1114 p_chunk_len = rte_fbarray_find_contig_free(p_arr,
1116 l_chunk_len = rte_fbarray_find_contig_free(l_arr,
1119 /* best case scenario - no differences (or bigger, which will be
1120 * fixed during next iteration), look for next chunk
1122 if (l_chunk_len >= p_chunk_len) {
1123 next_chunk_search_idx = p_idx + p_chunk_len;
1127 /* if both chunks start at the same point, skip parts we know
1128 * are identical, and sync the rest. each call to sync_chunk
1129 * will only sync contiguous segments, so we need to call this
1130 * until we are sure there are no more differences in this
1133 start = p_idx + l_chunk_len;
1134 end = p_idx + p_chunk_len;
1136 ret = sync_chunk(primary_msl, local_msl, hi, msl_idx,
1139 } while (start < end && ret >= 0);
1140 /* if ret is negative, something went wrong */
1144 next_chunk_search_idx = p_idx + p_chunk_len;
1146 /* skip to end of this chunk */
1148 p_idx = rte_fbarray_find_next_used(p_arr,
1149 next_chunk_search_idx);
1151 p_idx = rte_fbarray_find_next_free(p_arr,
1152 next_chunk_search_idx);
1159 sync_existing(struct rte_memseg_list *primary_msl,
1160 struct rte_memseg_list *local_msl, struct hugepage_info *hi,
1161 unsigned int msl_idx)
1165 /* do not allow any page allocations during the time we're allocating,
1166 * because file creation and locking operations are not atomic,
1167 * and we might be the first or the last ones to use a particular page,
1168 * so we need to ensure atomicity of every operation.
1170 dir_fd = open(hi->hugedir, O_RDONLY);
1172 RTE_LOG(ERR, EAL, "%s(): Cannot open '%s': %s\n", __func__,
1173 hi->hugedir, strerror(errno));
1176 /* blocking writelock */
1177 if (flock(dir_fd, LOCK_EX)) {
1178 RTE_LOG(ERR, EAL, "%s(): Cannot lock '%s': %s\n", __func__,
1179 hi->hugedir, strerror(errno));
1184 /* ensure all allocated space is the same in both lists */
1185 ret = sync_status(primary_msl, local_msl, hi, msl_idx, true);
1189 /* ensure all unallocated space is the same in both lists */
1190 ret = sync_status(primary_msl, local_msl, hi, msl_idx, false);
1194 /* update version number */
1195 local_msl->version = primary_msl->version;
1206 sync_walk(const struct rte_memseg_list *msl, void *arg __rte_unused)
1208 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
1209 struct rte_memseg_list *primary_msl, *local_msl;
1210 struct hugepage_info *hi = NULL;
1214 msl_idx = msl - mcfg->memsegs;
1215 primary_msl = &mcfg->memsegs[msl_idx];
1216 local_msl = &local_memsegs[msl_idx];
1218 for (i = 0; i < RTE_DIM(internal_config.hugepage_info); i++) {
1220 internal_config.hugepage_info[i].hugepage_sz;
1221 uint64_t msl_sz = primary_msl->page_sz;
1222 if (msl_sz == cur_sz) {
1223 hi = &internal_config.hugepage_info[i];
1228 RTE_LOG(ERR, EAL, "Can't find relevant hugepage_info entry\n");
1232 /* if versions don't match, synchronize everything */
1233 if (local_msl->version != primary_msl->version &&
1234 sync_existing(primary_msl, local_msl, hi, msl_idx))
1241 eal_memalloc_sync_with_primary(void)
1243 /* nothing to be done in primary */
1244 if (rte_eal_process_type() == RTE_PROC_PRIMARY)
1247 /* memalloc is locked, so it's safe to call thread-unsafe version */
1248 if (rte_memseg_list_walk_thread_unsafe(sync_walk, NULL))
1254 secondary_msl_create_walk(const struct rte_memseg_list *msl,
1255 void *arg __rte_unused)
1257 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
1258 struct rte_memseg_list *primary_msl, *local_msl;
1259 char name[PATH_MAX];
1262 msl_idx = msl - mcfg->memsegs;
1263 primary_msl = &mcfg->memsegs[msl_idx];
1264 local_msl = &local_memsegs[msl_idx];
1266 /* create distinct fbarrays for each secondary */
1267 snprintf(name, RTE_FBARRAY_NAME_LEN, "%s_%i",
1268 primary_msl->memseg_arr.name, getpid());
1270 ret = rte_fbarray_init(&local_msl->memseg_arr, name,
1271 primary_msl->memseg_arr.len,
1272 primary_msl->memseg_arr.elt_sz);
1274 RTE_LOG(ERR, EAL, "Cannot initialize local memory map\n");
1277 local_msl->base_va = primary_msl->base_va;
1283 secondary_lock_list_create_walk(const struct rte_memseg_list *msl,
1284 void *arg __rte_unused)
1286 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
1287 unsigned int i, len;
1291 msl_idx = msl - mcfg->memsegs;
1292 len = msl->memseg_arr.len;
1294 /* ensure we have space to store lock fd per each possible segment */
1295 data = malloc(sizeof(int) * len);
1297 RTE_LOG(ERR, EAL, "Unable to allocate space for lock descriptors\n");
1300 /* set all fd's as invalid */
1301 for (i = 0; i < len; i++)
1304 lock_fds[msl_idx].fds = data;
1305 lock_fds[msl_idx].len = len;
1306 lock_fds[msl_idx].count = 0;
1307 lock_fds[msl_idx].memseg_list_fd = -1;
1313 eal_memalloc_init(void)
1315 if (rte_eal_process_type() == RTE_PROC_SECONDARY)
1316 if (rte_memseg_list_walk(secondary_msl_create_walk, NULL) < 0)
1319 /* initialize all of the lock fd lists */
1320 if (internal_config.single_file_segments)
1321 if (rte_memseg_list_walk(secondary_lock_list_create_walk, NULL))