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"
44 * not all kernel version support fallocate on hugetlbfs, so fall back to
45 * ftruncate and disallow deallocation if fallocate is not supported.
47 static int fallocate_supported = -1; /* unknown */
50 * If each page is in a separate file, we can close fd's since we need each fd
51 * only once. However, in single file segments mode, we can get away with using
52 * a single fd for entire segments, but we need to store them somewhere. Each
53 * fd is different within each process, so we'll store them in a local tailq.
56 TAILQ_ENTRY(msl_entry) next;
61 /** Double linked list of memseg list fd's. */
62 TAILQ_HEAD(msl_entry_list, msl_entry);
64 static struct msl_entry_list msl_entry_list =
65 TAILQ_HEAD_INITIALIZER(msl_entry_list);
66 static rte_spinlock_t tailq_lock = RTE_SPINLOCK_INITIALIZER;
68 /** local copy of a memory map, used to synchronize memory hotplug in MP */
69 static struct rte_memseg_list local_memsegs[RTE_MAX_MEMSEG_LISTS];
71 static sigjmp_buf huge_jmpenv;
73 static void __rte_unused huge_sigbus_handler(int signo __rte_unused)
75 siglongjmp(huge_jmpenv, 1);
78 /* Put setjmp into a wrap method to avoid compiling error. Any non-volatile,
79 * non-static local variable in the stack frame calling sigsetjmp might be
80 * clobbered by a call to longjmp.
82 static int __rte_unused huge_wrap_sigsetjmp(void)
84 return sigsetjmp(huge_jmpenv, 1);
87 static struct sigaction huge_action_old;
88 static int huge_need_recover;
90 static void __rte_unused
91 huge_register_sigbus(void)
94 struct sigaction action;
97 sigaddset(&mask, SIGBUS);
99 action.sa_mask = mask;
100 action.sa_handler = huge_sigbus_handler;
102 huge_need_recover = !sigaction(SIGBUS, &action, &huge_action_old);
105 static void __rte_unused
106 huge_recover_sigbus(void)
108 if (huge_need_recover) {
109 sigaction(SIGBUS, &huge_action_old, NULL);
110 huge_need_recover = 0;
114 #ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
119 /* Check if kernel supports NUMA. */
120 if (numa_available() != 0) {
121 RTE_LOG(DEBUG, EAL, "NUMA is not supported.\n");
128 prepare_numa(int *oldpolicy, struct bitmask *oldmask, int socket_id)
130 RTE_LOG(DEBUG, EAL, "Trying to obtain current memory policy.\n");
131 if (get_mempolicy(oldpolicy, oldmask->maskp,
132 oldmask->size + 1, 0, 0) < 0) {
134 "Failed to get current mempolicy: %s. "
135 "Assuming MPOL_DEFAULT.\n", strerror(errno));
136 oldpolicy = MPOL_DEFAULT;
139 "Setting policy MPOL_PREFERRED for socket %d\n",
141 numa_set_preferred(socket_id);
145 resotre_numa(int *oldpolicy, struct bitmask *oldmask)
148 "Restoring previous memory policy: %d\n", *oldpolicy);
149 if (oldpolicy == MPOL_DEFAULT) {
150 numa_set_localalloc();
151 } else if (set_mempolicy(*oldpolicy, oldmask->maskp,
152 oldmask->size + 1) < 0) {
153 RTE_LOG(ERR, EAL, "Failed to restore mempolicy: %s\n",
155 numa_set_localalloc();
157 numa_free_cpumask(oldmask);
161 static struct msl_entry *
162 get_msl_entry_by_idx(unsigned int list_idx)
164 struct msl_entry *te;
166 rte_spinlock_lock(&tailq_lock);
168 TAILQ_FOREACH(te, &msl_entry_list, next) {
169 if (te->msl_idx == list_idx)
173 /* doesn't exist, so create it and set fd to -1 */
175 te = malloc(sizeof(*te));
177 RTE_LOG(ERR, EAL, "%s(): cannot allocate tailq entry for memseg list\n",
181 te->msl_idx = list_idx;
183 TAILQ_INSERT_TAIL(&msl_entry_list, te, next);
186 rte_spinlock_unlock(&tailq_lock);
191 * uses fstat to report the size of a file on disk
194 get_file_size(int fd)
197 if (fstat(fd, &st) < 0)
203 * uses fstat to check if file size on disk is zero (regular fstat won't show
204 * true file size due to how fallocate works)
207 is_zero_length(int fd)
210 if (fstat(fd, &st) < 0)
212 return st.st_blocks == 0;
215 /* we cannot use rte_memseg_list_walk() here because we will be holding a
216 * write lock whenever we enter every function in this file, however copying
217 * the same iteration code everywhere is not ideal as well. so, use a lockless
218 * copy of memseg list walk here.
221 memseg_list_walk_thread_unsafe(rte_memseg_list_walk_t func, void *arg)
223 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
226 for (i = 0; i < RTE_MAX_MEMSEG_LISTS; i++) {
227 struct rte_memseg_list *msl = &mcfg->memsegs[i];
229 if (msl->base_va == NULL)
232 ret = func(msl, arg);
242 get_seg_fd(char *path, int buflen, struct hugepage_info *hi,
243 unsigned int list_idx, unsigned int seg_idx)
247 if (internal_config.single_file_segments) {
249 * try to find a tailq entry, for this memseg list, or create
250 * one if it doesn't exist.
252 struct msl_entry *te = get_msl_entry_by_idx(list_idx);
254 RTE_LOG(ERR, EAL, "%s(): cannot allocate tailq entry for memseg list\n",
257 } else if (te->fd < 0) {
258 /* create a hugepage file */
259 eal_get_hugefile_path(path, buflen, hi->hugedir,
261 fd = open(path, O_CREAT | O_RDWR, 0600);
263 RTE_LOG(DEBUG, EAL, "%s(): open failed: %s\n",
264 __func__, strerror(errno));
272 /* one file per page, just create it */
273 eal_get_hugefile_path(path, buflen, hi->hugedir,
274 list_idx * RTE_MAX_MEMSEG_PER_LIST + seg_idx);
275 fd = open(path, O_CREAT | O_RDWR, 0600);
277 RTE_LOG(DEBUG, EAL, "%s(): open failed: %s\n", __func__,
285 /* returns 1 on successful lock, 0 on unsuccessful lock, -1 on error */
286 static int lock(int fd, uint64_t offset, uint64_t len, int type)
291 memset(&lck, 0, sizeof(lck));
294 lck.l_whence = SEEK_SET;
295 lck.l_start = offset;
298 ret = fcntl(fd, F_SETLK, &lck);
300 if (ret && (errno == EAGAIN || errno == EACCES)) {
301 /* locked by another process, not an error */
304 RTE_LOG(ERR, EAL, "%s(): error calling fcntl(): %s\n",
305 __func__, strerror(errno));
306 /* we've encountered an unexpected error */
313 resize_hugefile(int fd, uint64_t fa_offset, uint64_t page_sz,
318 if (fallocate_supported == 0) {
319 /* we cannot deallocate memory if fallocate() is not
320 * supported, but locks are still needed to prevent
321 * primary process' initialization from clearing out
322 * huge pages used by this process.
326 RTE_LOG(DEBUG, EAL, "%s(): fallocate not supported, not freeing page back to the system\n",
330 uint64_t new_size = fa_offset + page_sz;
331 uint64_t cur_size = get_file_size(fd);
333 /* fallocate isn't supported, fall back to ftruncate */
334 if (new_size > cur_size &&
335 ftruncate(fd, new_size) < 0) {
336 RTE_LOG(DEBUG, EAL, "%s(): ftruncate() failed: %s\n",
337 __func__, strerror(errno));
340 /* not being able to take out a read lock is an error */
341 if (lock(fd, fa_offset, page_sz, F_RDLCK) != 1)
344 int flags = grow ? 0 : FALLOC_FL_PUNCH_HOLE |
348 /* if fallocate() is supported, we need to take out a
349 * read lock on allocate (to prevent other processes
350 * from deallocating this page), and take out a write
351 * lock on deallocate (to ensure nobody else is using
354 * we can't use flock() for this, as we actually need to
355 * lock part of the file, not the entire file.
359 ret = lock(fd, fa_offset, page_sz, F_WRLCK);
364 /* failed to lock, not an error */
367 if (fallocate(fd, flags, fa_offset, page_sz) < 0) {
368 if (fallocate_supported == -1 &&
370 RTE_LOG(ERR, EAL, "%s(): fallocate() not supported, hugepage deallocation will be disabled\n",
373 fallocate_supported = 0;
375 RTE_LOG(DEBUG, EAL, "%s(): fallocate() failed: %s\n",
381 fallocate_supported = 1;
384 /* if can't read lock, it's an error */
385 if (lock(fd, fa_offset, page_sz,
389 /* if can't unlock, it's an error */
390 if (lock(fd, fa_offset, page_sz,
401 alloc_seg(struct rte_memseg *ms, void *addr, int socket_id,
402 struct hugepage_info *hi, unsigned int list_idx,
403 unsigned int seg_idx)
405 #ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
406 int cur_socket_id = 0;
414 fd = get_seg_fd(path, sizeof(path), hi, list_idx, seg_idx);
418 alloc_sz = hi->hugepage_sz;
419 if (internal_config.single_file_segments) {
420 map_offset = seg_idx * alloc_sz;
421 ret = resize_hugefile(fd, map_offset, alloc_sz, true);
426 if (ftruncate(fd, alloc_sz) < 0) {
427 RTE_LOG(DEBUG, EAL, "%s(): ftruncate() failed: %s\n",
428 __func__, strerror(errno));
431 /* we've allocated a page - take out a read lock. we're using
432 * fcntl() locks rather than flock() here because doing that
433 * gives us one huge advantage - fcntl() locks are per-process,
434 * not per-file descriptor, which means that we don't have to
435 * keep the original fd's around to keep a lock on the file.
437 * this is useful, because when it comes to unmapping pages, we
438 * will have to take out a write lock (to figure out if another
439 * process still has this page mapped), and to do itwith flock()
440 * we'll have to use original fd, as lock is associated with
441 * that particular fd. with fcntl(), this is not necessary - we
442 * can open a new fd and use fcntl() on that.
444 ret = lock(fd, map_offset, alloc_sz, F_RDLCK);
446 /* this should not fail */
448 RTE_LOG(ERR, EAL, "%s(): error locking file: %s\n",
456 * map the segment, and populate page tables, the kernel fills this
457 * segment with zeros if it's a new page.
459 void *va = mmap(addr, alloc_sz, PROT_READ | PROT_WRITE,
460 MAP_SHARED | MAP_POPULATE | MAP_FIXED, fd, map_offset);
461 /* for non-single file segments, we can close fd here */
462 if (!internal_config.single_file_segments)
465 if (va == MAP_FAILED) {
466 RTE_LOG(DEBUG, EAL, "%s(): mmap() failed: %s\n", __func__,
471 RTE_LOG(DEBUG, EAL, "%s(): wrong mmap() address\n", __func__);
475 rte_iova_t iova = rte_mem_virt2iova(addr);
476 if (iova == RTE_BAD_PHYS_ADDR) {
477 RTE_LOG(DEBUG, EAL, "%s(): can't get IOVA addr\n",
482 #ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
483 move_pages(getpid(), 1, &addr, NULL, &cur_socket_id, 0);
485 if (cur_socket_id != socket_id) {
487 "%s(): allocation happened on wrong socket (wanted %d, got %d)\n",
488 __func__, socket_id, cur_socket_id);
493 /* In linux, hugetlb limitations, like cgroup, are
494 * enforced at fault time instead of mmap(), even
495 * with the option of MAP_POPULATE. Kernel will send
496 * a SIGBUS signal. To avoid to be killed, save stack
497 * environment here, if SIGBUS happens, we can jump
500 if (huge_wrap_sigsetjmp()) {
501 RTE_LOG(DEBUG, EAL, "SIGBUS: Cannot mmap more hugepages of size %uMB\n",
502 (unsigned int)(alloc_sz >> 20));
505 *(int *)addr = *(int *)addr;
508 ms->hugepage_sz = alloc_sz;
510 ms->nchannel = rte_memory_get_nchannel();
511 ms->nrank = rte_memory_get_nrank();
513 ms->socket_id = socket_id;
518 munmap(addr, alloc_sz);
520 if (internal_config.single_file_segments) {
521 resize_hugefile(fd, map_offset, alloc_sz, false);
522 if (is_zero_length(fd)) {
523 struct msl_entry *te = get_msl_entry_by_idx(list_idx);
524 if (te != NULL && te->fd >= 0) {
528 /* ignore errors, can't make it any worse */
539 free_seg(struct rte_memseg *ms, struct hugepage_info *hi,
540 unsigned int list_idx, unsigned int seg_idx)
546 /* erase page data */
547 memset(ms->addr, 0, ms->len);
549 if (mmap(ms->addr, ms->len, PROT_READ,
550 MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0) ==
552 RTE_LOG(DEBUG, EAL, "couldn't unmap page\n");
556 fd = get_seg_fd(path, sizeof(path), hi, list_idx, seg_idx);
560 if (internal_config.single_file_segments) {
561 map_offset = seg_idx * ms->len;
562 if (resize_hugefile(fd, map_offset, ms->len, false))
564 /* if file is zero-length, we've already shrunk it, so it's
567 if (is_zero_length(fd)) {
568 struct msl_entry *te = get_msl_entry_by_idx(list_idx);
569 if (te != NULL && te->fd >= 0) {
577 /* if we're able to take out a write lock, we're the last one
578 * holding onto this page.
581 ret = lock(fd, 0, ms->len, F_WRLCK);
583 /* no one else is using this page */
586 ret = lock(fd, 0, ms->len, F_UNLCK);
588 RTE_LOG(ERR, EAL, "%s(): unable to unlock file %s\n",
594 memset(ms, 0, sizeof(*ms));
599 struct alloc_walk_param {
600 struct hugepage_info *hi;
601 struct rte_memseg **ms;
603 unsigned int segs_allocated;
609 alloc_seg_walk(const struct rte_memseg_list *msl, void *arg)
611 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
612 struct alloc_walk_param *wa = arg;
613 struct rte_memseg_list *cur_msl;
615 int cur_idx, start_idx, j;
616 unsigned int msl_idx, need, i;
618 if (msl->page_sz != wa->page_sz)
620 if (msl->socket_id != wa->socket)
623 page_sz = (size_t)msl->page_sz;
625 msl_idx = msl - mcfg->memsegs;
626 cur_msl = &mcfg->memsegs[msl_idx];
630 /* try finding space in memseg list */
631 cur_idx = rte_fbarray_find_next_n_free(&cur_msl->memseg_arr, 0, need);
636 for (i = 0; i < need; i++, cur_idx++) {
637 struct rte_memseg *cur;
640 cur = rte_fbarray_get(&cur_msl->memseg_arr, cur_idx);
641 map_addr = RTE_PTR_ADD(cur_msl->base_va,
644 if (alloc_seg(cur, map_addr, wa->socket, wa->hi,
646 RTE_LOG(DEBUG, EAL, "attempted to allocate %i segments, but only %i were allocated\n",
649 /* if exact number wasn't requested, stop */
654 for (j = start_idx; j < cur_idx; j++) {
655 struct rte_memseg *tmp;
656 struct rte_fbarray *arr =
657 &cur_msl->memseg_arr;
659 tmp = rte_fbarray_get(arr, j);
660 if (free_seg(tmp, wa->hi, msl_idx,
662 RTE_LOG(ERR, EAL, "Cannot free page\n");
666 rte_fbarray_set_free(arr, j);
670 memset(wa->ms, 0, sizeof(*wa->ms) * wa->n_segs);
676 rte_fbarray_set_used(&cur_msl->memseg_arr, cur_idx);
679 wa->segs_allocated = i;
685 struct free_walk_param {
686 struct hugepage_info *hi;
687 struct rte_memseg *ms;
690 free_seg_walk(const struct rte_memseg_list *msl, void *arg)
692 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
693 struct rte_memseg_list *found_msl;
694 struct free_walk_param *wa = arg;
695 uintptr_t start_addr, end_addr;
696 int msl_idx, seg_idx;
698 start_addr = (uintptr_t) msl->base_va;
699 end_addr = start_addr + msl->memseg_arr.len * (size_t)msl->page_sz;
701 if ((uintptr_t)wa->ms->addr < start_addr ||
702 (uintptr_t)wa->ms->addr >= end_addr)
705 msl_idx = msl - mcfg->memsegs;
706 seg_idx = RTE_PTR_DIFF(wa->ms->addr, start_addr) / msl->page_sz;
709 found_msl = &mcfg->memsegs[msl_idx];
711 found_msl->version++;
713 rte_fbarray_set_free(&found_msl->memseg_arr, seg_idx);
715 if (free_seg(wa->ms, wa->hi, msl_idx, seg_idx))
722 eal_memalloc_alloc_seg_bulk(struct rte_memseg **ms, int n_segs, size_t page_sz,
723 int socket, bool exact)
726 #ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
727 bool have_numa = false;
729 struct bitmask *oldmask;
731 struct alloc_walk_param wa;
732 struct hugepage_info *hi = NULL;
734 memset(&wa, 0, sizeof(wa));
736 /* dynamic allocation not supported in legacy mode */
737 if (internal_config.legacy_mem)
740 for (i = 0; i < (int) RTE_DIM(internal_config.hugepage_info); i++) {
742 internal_config.hugepage_info[i].hugepage_sz) {
743 hi = &internal_config.hugepage_info[i];
748 RTE_LOG(ERR, EAL, "%s(): can't find relevant hugepage_info entry\n",
753 #ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
755 oldmask = numa_allocate_nodemask();
756 prepare_numa(&oldpolicy, oldmask, socket);
765 wa.page_sz = page_sz;
767 wa.segs_allocated = 0;
769 ret = memseg_list_walk_thread_unsafe(alloc_seg_walk, &wa);
771 RTE_LOG(ERR, EAL, "%s(): couldn't find suitable memseg_list\n",
774 } else if (ret > 0) {
775 ret = (int)wa.segs_allocated;
778 #ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
780 resotre_numa(&oldpolicy, oldmask);
786 eal_memalloc_alloc_seg(size_t page_sz, int socket)
788 struct rte_memseg *ms;
789 if (eal_memalloc_alloc_seg_bulk(&ms, 1, page_sz, socket, true) < 0)
791 /* return pointer to newly allocated memseg */
796 eal_memalloc_free_seg_bulk(struct rte_memseg **ms, int n_segs)
800 /* dynamic free not supported in legacy mode */
801 if (internal_config.legacy_mem)
804 for (seg = 0; seg < n_segs; seg++) {
805 struct rte_memseg *cur = ms[seg];
806 struct hugepage_info *hi = NULL;
807 struct free_walk_param wa;
810 memset(&wa, 0, sizeof(wa));
812 for (i = 0; i < (int)RTE_DIM(internal_config.hugepage_info);
814 hi = &internal_config.hugepage_info[i];
815 if (cur->hugepage_sz == hi->hugepage_sz)
818 if (i == (int)RTE_DIM(internal_config.hugepage_info)) {
819 RTE_LOG(ERR, EAL, "Can't find relevant hugepage_info entry\n");
827 walk_res = memseg_list_walk_thread_unsafe(free_seg_walk, &wa);
831 RTE_LOG(ERR, EAL, "Couldn't find memseg list\n");
838 eal_memalloc_free_seg(struct rte_memseg *ms)
840 /* dynamic free not supported in legacy mode */
841 if (internal_config.legacy_mem)
844 return eal_memalloc_free_seg_bulk(&ms, 1);
848 sync_chunk(struct rte_memseg_list *primary_msl,
849 struct rte_memseg_list *local_msl, struct hugepage_info *hi,
850 unsigned int msl_idx, bool used, int start, int end)
852 struct rte_fbarray *l_arr, *p_arr;
853 int i, ret, chunk_len, diff_len;
855 l_arr = &local_msl->memseg_arr;
856 p_arr = &primary_msl->memseg_arr;
858 /* we need to aggregate allocations/deallocations into bigger chunks,
859 * as we don't want to spam the user with per-page callbacks.
861 * to avoid any potential issues, we also want to trigger
862 * deallocation callbacks *before* we actually deallocate
863 * memory, so that the user application could wrap up its use
864 * before it goes away.
867 chunk_len = end - start;
869 /* find how many contiguous pages we can map/unmap for this chunk */
871 rte_fbarray_find_contig_free(l_arr, start) :
872 rte_fbarray_find_contig_used(l_arr, start);
874 /* has to be at least one page */
878 diff_len = RTE_MIN(chunk_len, diff_len);
880 /* if we are freeing memory, notify the application */
882 struct rte_memseg *ms;
886 ms = rte_fbarray_get(l_arr, start);
888 page_sz = (size_t)primary_msl->page_sz;
889 len = page_sz * diff_len;
891 eal_memalloc_mem_event_notify(RTE_MEM_EVENT_FREE,
895 for (i = 0; i < diff_len; i++) {
896 struct rte_memseg *p_ms, *l_ms;
897 int seg_idx = start + i;
899 l_ms = rte_fbarray_get(l_arr, seg_idx);
900 p_ms = rte_fbarray_get(p_arr, seg_idx);
902 if (l_ms == NULL || p_ms == NULL)
906 ret = alloc_seg(l_ms, p_ms->addr,
911 rte_fbarray_set_used(l_arr, seg_idx);
913 ret = free_seg(l_ms, hi, msl_idx, seg_idx);
914 rte_fbarray_set_free(l_arr, seg_idx);
920 /* if we just allocated memory, notify the application */
922 struct rte_memseg *ms;
926 ms = rte_fbarray_get(l_arr, start);
928 page_sz = (size_t)primary_msl->page_sz;
929 len = page_sz * diff_len;
931 eal_memalloc_mem_event_notify(RTE_MEM_EVENT_ALLOC,
935 /* calculate how much we can advance until next chunk */
937 rte_fbarray_find_contig_used(l_arr, start) :
938 rte_fbarray_find_contig_free(l_arr, start);
939 ret = RTE_MIN(chunk_len, diff_len);
945 sync_status(struct rte_memseg_list *primary_msl,
946 struct rte_memseg_list *local_msl, struct hugepage_info *hi,
947 unsigned int msl_idx, bool used)
949 struct rte_fbarray *l_arr, *p_arr;
950 int p_idx, l_chunk_len, p_chunk_len, ret;
953 /* this is a little bit tricky, but the basic idea is - walk both lists
954 * and spot any places where there are discrepancies. walking both lists
955 * and noting discrepancies in a single go is a hard problem, so we do
956 * it in two passes - first we spot any places where allocated segments
957 * mismatch (i.e. ensure that everything that's allocated in the primary
958 * is also allocated in the secondary), and then we do it by looking at
959 * free segments instead.
961 * we also need to aggregate changes into chunks, as we have to call
962 * callbacks per allocation, not per page.
964 l_arr = &local_msl->memseg_arr;
965 p_arr = &primary_msl->memseg_arr;
968 p_idx = rte_fbarray_find_next_used(p_arr, 0);
970 p_idx = rte_fbarray_find_next_free(p_arr, 0);
973 int next_chunk_search_idx;
976 p_chunk_len = rte_fbarray_find_contig_used(p_arr,
978 l_chunk_len = rte_fbarray_find_contig_used(l_arr,
981 p_chunk_len = rte_fbarray_find_contig_free(p_arr,
983 l_chunk_len = rte_fbarray_find_contig_free(l_arr,
986 /* best case scenario - no differences (or bigger, which will be
987 * fixed during next iteration), look for next chunk
989 if (l_chunk_len >= p_chunk_len) {
990 next_chunk_search_idx = p_idx + p_chunk_len;
994 /* if both chunks start at the same point, skip parts we know
995 * are identical, and sync the rest. each call to sync_chunk
996 * will only sync contiguous segments, so we need to call this
997 * until we are sure there are no more differences in this
1000 start = p_idx + l_chunk_len;
1001 end = p_idx + p_chunk_len;
1003 ret = sync_chunk(primary_msl, local_msl, hi, msl_idx,
1006 } while (start < end && ret >= 0);
1007 /* if ret is negative, something went wrong */
1011 next_chunk_search_idx = p_idx + p_chunk_len;
1013 /* skip to end of this chunk */
1015 p_idx = rte_fbarray_find_next_used(p_arr,
1016 next_chunk_search_idx);
1018 p_idx = rte_fbarray_find_next_free(p_arr,
1019 next_chunk_search_idx);
1026 sync_existing(struct rte_memseg_list *primary_msl,
1027 struct rte_memseg_list *local_msl, struct hugepage_info *hi,
1028 unsigned int msl_idx)
1032 /* ensure all allocated space is the same in both lists */
1033 ret = sync_status(primary_msl, local_msl, hi, msl_idx, true);
1037 /* ensure all unallocated space is the same in both lists */
1038 ret = sync_status(primary_msl, local_msl, hi, msl_idx, false);
1042 /* update version number */
1043 local_msl->version = primary_msl->version;
1049 sync_walk(const struct rte_memseg_list *msl, void *arg __rte_unused)
1051 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
1052 struct rte_memseg_list *primary_msl, *local_msl;
1053 struct hugepage_info *hi = NULL;
1056 bool new_msl = false;
1058 msl_idx = msl - mcfg->memsegs;
1059 primary_msl = &mcfg->memsegs[msl_idx];
1060 local_msl = &local_memsegs[msl_idx];
1062 /* check if secondary has this memseg list set up */
1063 if (local_msl->base_va == NULL) {
1064 char name[PATH_MAX];
1068 /* create distinct fbarrays for each secondary */
1069 snprintf(name, RTE_FBARRAY_NAME_LEN, "%s_%i",
1070 primary_msl->memseg_arr.name, getpid());
1072 ret = rte_fbarray_init(&local_msl->memseg_arr, name,
1073 primary_msl->memseg_arr.len,
1074 primary_msl->memseg_arr.elt_sz);
1076 RTE_LOG(ERR, EAL, "Cannot initialize local memory map\n");
1080 local_msl->base_va = primary_msl->base_va;
1083 for (i = 0; i < RTE_DIM(internal_config.hugepage_info); i++) {
1085 internal_config.hugepage_info[i].hugepage_sz;
1086 uint64_t msl_sz = primary_msl->page_sz;
1087 if (msl_sz == cur_sz) {
1088 hi = &internal_config.hugepage_info[i];
1093 RTE_LOG(ERR, EAL, "Can't find relevant hugepage_info entry\n");
1097 /* if versions don't match or if we have just allocated a new
1098 * memseg list, synchronize everything
1100 if ((new_msl || local_msl->version != primary_msl->version) &&
1101 sync_existing(primary_msl, local_msl, hi, msl_idx))
1108 eal_memalloc_sync_with_primary(void)
1110 /* nothing to be done in primary */
1111 if (rte_eal_process_type() == RTE_PROC_PRIMARY)
1114 if (memseg_list_walk_thread_unsafe(sync_walk, NULL))