mem: support --in-memory mode
[dpdk.git] / lib / librte_eal / linuxapp / eal / eal_memalloc.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2017-2018 Intel Corporation
3  */
4
5 #define _FILE_OFFSET_BITS 64
6 #include <errno.h>
7 #include <stdarg.h>
8 #include <stdbool.h>
9 #include <stdlib.h>
10 #include <stdio.h>
11 #include <stdint.h>
12 #include <inttypes.h>
13 #include <string.h>
14 #include <sys/mman.h>
15 #include <sys/types.h>
16 #include <sys/stat.h>
17 #include <sys/queue.h>
18 #include <sys/file.h>
19 #include <unistd.h>
20 #include <limits.h>
21 #include <fcntl.h>
22 #include <sys/ioctl.h>
23 #include <sys/time.h>
24 #include <signal.h>
25 #include <setjmp.h>
26 #ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
27 #include <numa.h>
28 #include <numaif.h>
29 #endif
30 #include <linux/falloc.h>
31 #include <linux/mman.h> /* for hugetlb-related mmap flags */
32
33 #include <rte_common.h>
34 #include <rte_log.h>
35 #include <rte_eal_memconfig.h>
36 #include <rte_eal.h>
37 #include <rte_memory.h>
38 #include <rte_spinlock.h>
39
40 #include "eal_filesystem.h"
41 #include "eal_internal_cfg.h"
42 #include "eal_memalloc.h"
43 #include "eal_private.h"
44
45 const int anonymous_hugepages_supported =
46 #ifdef MAP_HUGE_SHIFT
47                 1;
48 #define RTE_MAP_HUGE_SHIFT MAP_HUGE_SHIFT
49 #else
50                 0;
51 #define RTE_MAP_HUGE_SHIFT 26
52 #endif
53
54 /*
55  * not all kernel version support fallocate on hugetlbfs, so fall back to
56  * ftruncate and disallow deallocation if fallocate is not supported.
57  */
58 static int fallocate_supported = -1; /* unknown */
59
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.
65  *
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.
69  *
70  * they will be initialized at startup, and filled as we allocate/deallocate
71  * segments. also, use this to track memseg list proper fd.
72  */
73 static struct {
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];
79
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];
82
83 static sigjmp_buf huge_jmpenv;
84
85 static void __rte_unused huge_sigbus_handler(int signo __rte_unused)
86 {
87         siglongjmp(huge_jmpenv, 1);
88 }
89
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.
93  */
94 static int __rte_unused huge_wrap_sigsetjmp(void)
95 {
96         return sigsetjmp(huge_jmpenv, 1);
97 }
98
99 static struct sigaction huge_action_old;
100 static int huge_need_recover;
101
102 static void __rte_unused
103 huge_register_sigbus(void)
104 {
105         sigset_t mask;
106         struct sigaction action;
107
108         sigemptyset(&mask);
109         sigaddset(&mask, SIGBUS);
110         action.sa_flags = 0;
111         action.sa_mask = mask;
112         action.sa_handler = huge_sigbus_handler;
113
114         huge_need_recover = !sigaction(SIGBUS, &action, &huge_action_old);
115 }
116
117 static void __rte_unused
118 huge_recover_sigbus(void)
119 {
120         if (huge_need_recover) {
121                 sigaction(SIGBUS, &huge_action_old, NULL);
122                 huge_need_recover = 0;
123         }
124 }
125
126 #ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
127 static bool
128 check_numa(void)
129 {
130         bool ret = true;
131         /* Check if kernel supports NUMA. */
132         if (numa_available() != 0) {
133                 RTE_LOG(DEBUG, EAL, "NUMA is not supported.\n");
134                 ret = false;
135         }
136         return ret;
137 }
138
139 static void
140 prepare_numa(int *oldpolicy, struct bitmask *oldmask, int socket_id)
141 {
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) {
145                 RTE_LOG(ERR, EAL,
146                         "Failed to get current mempolicy: %s. "
147                         "Assuming MPOL_DEFAULT.\n", strerror(errno));
148                 oldpolicy = MPOL_DEFAULT;
149         }
150         RTE_LOG(DEBUG, EAL,
151                 "Setting policy MPOL_PREFERRED for socket %d\n",
152                 socket_id);
153         numa_set_preferred(socket_id);
154 }
155
156 static void
157 restore_numa(int *oldpolicy, struct bitmask *oldmask)
158 {
159         RTE_LOG(DEBUG, EAL,
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",
166                         strerror(errno));
167                 numa_set_localalloc();
168         }
169         numa_free_cpumask(oldmask);
170 }
171 #endif
172
173 /*
174  * uses fstat to report the size of a file on disk
175  */
176 static off_t
177 get_file_size(int fd)
178 {
179         struct stat st;
180         if (fstat(fd, &st) < 0)
181                 return 0;
182         return st.st_size;
183 }
184
185 /* returns 1 on successful lock, 0 on unsuccessful lock, -1 on error */
186 static int lock(int fd, int type)
187 {
188         int ret;
189
190         /* flock may be interrupted */
191         do {
192                 ret = flock(fd, type | LOCK_NB);
193         } while (ret && errno == EINTR);
194
195         if (ret && errno == EWOULDBLOCK) {
196                 /* couldn't lock */
197                 return 0;
198         } else if (ret) {
199                 RTE_LOG(ERR, EAL, "%s(): error calling flock(): %s\n",
200                         __func__, strerror(errno));
201                 return -1;
202         }
203         /* lock was successful */
204         return 1;
205 }
206
207 static int get_segment_lock_fd(int list_idx, int seg_idx)
208 {
209         char path[PATH_MAX] = {0};
210         int fd;
211
212         if (list_idx < 0 || list_idx >= (int)RTE_DIM(lock_fds))
213                 return -1;
214         if (seg_idx < 0 || seg_idx >= lock_fds[list_idx].len)
215                 return -1;
216
217         fd = lock_fds[list_idx].fds[seg_idx];
218         /* does this lock already exist? */
219         if (fd >= 0)
220                 return fd;
221
222         eal_get_hugefile_lock_path(path, sizeof(path),
223                         list_idx * RTE_MAX_MEMSEG_PER_LIST + seg_idx);
224
225         fd = open(path, O_CREAT | O_RDWR, 0660);
226         if (fd < 0) {
227                 RTE_LOG(ERR, EAL, "%s(): error creating lockfile '%s': %s\n",
228                         __func__, path, strerror(errno));
229                 return -1;
230         }
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));
235                 close(fd);
236                 return -1;
237         }
238         /* store it for future reference */
239         lock_fds[list_idx].fds[seg_idx] = fd;
240         lock_fds[list_idx].count++;
241         return fd;
242 }
243
244 static int unlock_segment(int list_idx, int seg_idx)
245 {
246         int fd, ret;
247
248         if (list_idx < 0 || list_idx >= (int)RTE_DIM(lock_fds))
249                 return -1;
250         if (seg_idx < 0 || seg_idx >= lock_fds[list_idx].len)
251                 return -1;
252
253         fd = lock_fds[list_idx].fds[seg_idx];
254
255         /* upgrade lock to exclusive to see if we can remove the lockfile */
256         ret = lock(fd, LOCK_EX);
257         if (ret == 1) {
258                 /* we've succeeded in taking exclusive lock, this lockfile may
259                  * be removed.
260                  */
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);
264                 if (unlink(path)) {
265                         RTE_LOG(ERR, EAL, "%s(): error removing lockfile '%s': %s\n",
266                                         __func__, path, strerror(errno));
267                 }
268         }
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.
271          */
272         close(fd);
273         lock_fds[list_idx].fds[seg_idx] = -1;
274         lock_fds[list_idx].count--;
275
276         if (ret < 0)
277                 return -1;
278         return 0;
279 }
280
281 static int
282 get_seg_fd(char *path, int buflen, struct hugepage_info *hi,
283                 unsigned int list_idx, unsigned int seg_idx)
284 {
285         int fd;
286
287         if (internal_config.single_file_segments) {
288                 /* create a hugepage file path */
289                 eal_get_hugefile_path(path, buflen, hi->hugedir, list_idx);
290
291                 fd = lock_fds[list_idx].memseg_list_fd;
292
293                 if (fd < 0) {
294                         fd = open(path, O_CREAT | O_RDWR, 0600);
295                         if (fd < 0) {
296                                 RTE_LOG(ERR, EAL, "%s(): open failed: %s\n",
297                                         __func__, strerror(errno));
298                                 return -1;
299                         }
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));
304                                 close(fd);
305                                 return -1;
306                         }
307                         lock_fds[list_idx].memseg_list_fd = fd;
308                 }
309         } else {
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);
314                 if (fd < 0) {
315                         RTE_LOG(DEBUG, EAL, "%s(): open failed: %s\n", __func__,
316                                         strerror(errno));
317                         return -1;
318                 }
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));
323                         close(fd);
324                         return -1;
325                 }
326         }
327         return fd;
328 }
329
330 static int
331 resize_hugefile(int fd, char *path, int list_idx, int seg_idx,
332                 uint64_t fa_offset, uint64_t page_sz, bool grow)
333 {
334         bool again = false;
335         do {
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.
340                          */
341
342                         if (!grow) {
343                                 RTE_LOG(DEBUG, EAL, "%s(): fallocate not supported, not freeing page back to the system\n",
344                                         __func__);
345                                 return -1;
346                         }
347                         uint64_t new_size = fa_offset + page_sz;
348                         uint64_t cur_size = get_file_size(fd);
349
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));
355                                 return -1;
356                         }
357                 } else {
358                         int flags = grow ? 0 : FALLOC_FL_PUNCH_HOLE |
359                                         FALLOC_FL_KEEP_SIZE;
360                         int ret, lock_fd;
361
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
366                          * this page).
367                          *
368                          * read locks on page itself are already taken out at
369                          * file creation, in get_seg_fd().
370                          *
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
376                          * of the file.
377                          *
378                          * if we're shrinking the file, we want to upgrade our
379                          * lock from shared to exclusive.
380                          *
381                          * lock_fd is an fd for a lockfile, not for the segment
382                          * list.
383                          */
384                         lock_fd = get_segment_lock_fd(list_idx, seg_idx);
385
386                         if (!grow) {
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.
391                                  *
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
396                                  * prematurely.
397                                  */
398                                 ret = lock(lock_fd, LOCK_EX);
399
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.
404                                  */
405                                 if (unlock_segment(list_idx, seg_idx))
406                                         RTE_LOG(ERR, EAL, "Could not unlock segment\n");
407
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.
412                                  */
413                                 if (ret < 1 && lock_fds[list_idx].count == 0) {
414                                         close(fd);
415                                         lock_fds[list_idx].memseg_list_fd = -1;
416                                 }
417
418                                 if (ret < 0) {
419                                         RTE_LOG(ERR, EAL, "Could not lock segment\n");
420                                         return -1;
421                                 }
422                                 if (ret == 0)
423                                         /* failed to lock, not an error. */
424                                         return 0;
425                         }
426
427                         /* grow or shrink the file */
428                         ret = fallocate(fd, flags, fa_offset, page_sz);
429
430                         if (ret < 0) {
431                                 if (fallocate_supported == -1 &&
432                                                 errno == ENOTSUP) {
433                                         RTE_LOG(ERR, EAL, "%s(): fallocate() not supported, hugepage deallocation will be disabled\n",
434                                                 __func__);
435                                         again = true;
436                                         fallocate_supported = 0;
437                                 } else {
438                                         RTE_LOG(DEBUG, EAL, "%s(): fallocate() failed: %s\n",
439                                                 __func__,
440                                                 strerror(errno));
441                                         return -1;
442                                 }
443                         } else {
444                                 fallocate_supported = 1;
445
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.
450                                  */
451                                 if (lock_fds[list_idx].count == 0) {
452                                         if (unlink(path))
453                                                 RTE_LOG(ERR, EAL, "%s(): unlinking '%s' failed: %s\n",
454                                                         __func__, path,
455                                                         strerror(errno));
456                                         close(fd);
457                                         lock_fds[list_idx].memseg_list_fd = -1;
458                                 }
459                         }
460                 }
461         } while (again);
462         return 0;
463 }
464
465 static int
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)
469 {
470 #ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
471         int cur_socket_id = 0;
472 #endif
473         uint64_t map_offset;
474         rte_iova_t iova;
475         void *va;
476         char path[PATH_MAX];
477         int ret = 0;
478         int fd;
479         size_t alloc_sz;
480         int flags;
481         void *new_addr;
482
483         alloc_sz = hi->hugepage_sz;
484         if (internal_config.in_memory && anonymous_hugepages_supported) {
485                 int log2, flags;
486
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
490                  */
491                 flags = (log2 << RTE_MAP_HUGE_SHIFT) | MAP_HUGETLB | MAP_FIXED |
492                                 MAP_PRIVATE | MAP_ANONYMOUS;
493                 fd = -1;
494                 va = mmap(addr, alloc_sz, PROT_READ | PROT_WRITE, flags, -1, 0);
495
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.
502                  */
503                 map_offset = 0;
504         } else {
505                 /* takes out a read lock on segment or segment list */
506                 fd = get_seg_fd(path, sizeof(path), hi, list_idx, seg_idx);
507                 if (fd < 0) {
508                         RTE_LOG(ERR, EAL, "Couldn't get fd on hugepage file\n");
509                         return -1;
510                 }
511
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);
516                         if (ret < 0)
517                                 goto resized;
518                 } else {
519                         map_offset = 0;
520                         if (ftruncate(fd, alloc_sz) < 0) {
521                                 RTE_LOG(DEBUG, EAL, "%s(): ftruncate() failed: %s\n",
522                                         __func__, strerror(errno));
523                                 goto resized;
524                         }
525                         if (internal_config.hugepage_unlink) {
526                                 if (unlink(path)) {
527                                         RTE_LOG(DEBUG, EAL, "%s(): unlink() failed: %s\n",
528                                                 __func__, strerror(errno));
529                                         goto resized;
530                                 }
531                         }
532                 }
533
534                 /*
535                  * map the segment, and populate page tables, the kernel fills
536                  * this segment with zeros if it's a new page.
537                  */
538                 va = mmap(addr, alloc_sz, PROT_READ | PROT_WRITE,
539                                 MAP_SHARED | MAP_POPULATE | MAP_FIXED, fd,
540                                 map_offset);
541         }
542
543         if (va == MAP_FAILED) {
544                 RTE_LOG(DEBUG, EAL, "%s(): mmap() failed: %s\n", __func__,
545                         strerror(errno));
546                 /* mmap failed, but the previous region might have been
547                  * unmapped anyway. try to remap it
548                  */
549                 goto unmapped;
550         }
551         if (va != addr) {
552                 RTE_LOG(DEBUG, EAL, "%s(): wrong mmap() address\n", __func__);
553                 munmap(va, alloc_sz);
554                 goto resized;
555         }
556
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
562          * back here.
563          */
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));
567                 goto mapped;
568         }
569
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.
574          */
575         *(volatile int *)addr = *(volatile int *)addr;
576
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",
580                         __func__);
581                 goto mapped;
582         }
583
584 #ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
585         move_pages(getpid(), 1, &addr, NULL, &cur_socket_id, 0);
586
587         if (cur_socket_id != socket_id) {
588                 RTE_LOG(DEBUG, EAL,
589                                 "%s(): allocation happened on wrong socket (wanted %d, got %d)\n",
590                         __func__, socket_id, cur_socket_id);
591                 goto mapped;
592         }
593 #endif
594         /* for non-single file segments that aren't in-memory, we can close fd
595          * here */
596         if (!internal_config.single_file_segments && !internal_config.in_memory)
597                 close(fd);
598
599         ms->addr = addr;
600         ms->hugepage_sz = alloc_sz;
601         ms->len = alloc_sz;
602         ms->nchannel = rte_memory_get_nchannel();
603         ms->nrank = rte_memory_get_nrank();
604         ms->iova = iova;
605         ms->socket_id = socket_id;
606
607         return 0;
608
609 mapped:
610         munmap(addr, alloc_sz);
611 unmapped:
612         flags = MAP_FIXED;
613 #ifdef RTE_ARCH_PPC_64
614         flags |= MAP_HUGETLB;
615 #endif
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.
623                  */
624                 RTE_LOG(CRIT, EAL, "Can't mmap holes in our virtual address space\n");
625         }
626 resized:
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,
630                                 alloc_sz, false);
631                 /* ignore failure, can't make it any worse */
632         } else {
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)
637                         unlink(path);
638                 close(fd);
639         }
640         return -1;
641 }
642
643 static int
644 free_seg(struct rte_memseg *ms, struct hugepage_info *hi,
645                 unsigned int list_idx, unsigned int seg_idx)
646 {
647         uint64_t map_offset;
648         char path[PATH_MAX];
649         int fd, ret;
650
651         /* erase page data */
652         memset(ms->addr, 0, ms->len);
653
654         if (mmap(ms->addr, ms->len, PROT_READ,
655                         MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0) ==
656                                 MAP_FAILED) {
657                 RTE_LOG(DEBUG, EAL, "couldn't unmap page\n");
658                 return -1;
659         }
660
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));
664                 return 0;
665         }
666
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.
670          */
671         fd = get_seg_fd(path, sizeof(path), hi, list_idx, seg_idx);
672         if (fd < 0)
673                 return -1;
674
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,
678                                 ms->len, false))
679                         return -1;
680                 ret = 0;
681         } else {
682                 /* if we're able to take out a write lock, we're the last one
683                  * holding onto this page.
684                  */
685                 ret = lock(fd, LOCK_EX);
686                 if (ret >= 0) {
687                         /* no one else is using this page */
688                         if (ret == 1)
689                                 unlink(path);
690                 }
691                 /* closing fd will drop the lock */
692                 close(fd);
693         }
694
695         memset(ms, 0, sizeof(*ms));
696
697         return ret < 0 ? -1 : 0;
698 }
699
700 struct alloc_walk_param {
701         struct hugepage_info *hi;
702         struct rte_memseg **ms;
703         size_t page_sz;
704         unsigned int segs_allocated;
705         unsigned int n_segs;
706         int socket;
707         bool exact;
708 };
709 static int
710 alloc_seg_walk(const struct rte_memseg_list *msl, void *arg)
711 {
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;
715         size_t page_sz;
716         int cur_idx, start_idx, j, dir_fd = -1;
717         unsigned int msl_idx, need, i;
718
719         if (msl->page_sz != wa->page_sz)
720                 return 0;
721         if (msl->socket_id != wa->socket)
722                 return 0;
723
724         page_sz = (size_t)msl->page_sz;
725
726         msl_idx = msl - mcfg->memsegs;
727         cur_msl = &mcfg->memsegs[msl_idx];
728
729         need = wa->n_segs;
730
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);
734         if (cur_idx < 0)
735                 return 0;
736         start_idx = cur_idx;
737
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.
742          *
743          * during init, we already hold a write lock, so don't try to take out
744          * another one.
745          */
746         if (wa->hi->lock_descriptor == -1 && !internal_config.in_memory) {
747                 dir_fd = open(wa->hi->hugedir, O_RDONLY);
748                 if (dir_fd < 0) {
749                         RTE_LOG(ERR, EAL, "%s(): Cannot open '%s': %s\n",
750                                 __func__, wa->hi->hugedir, strerror(errno));
751                         return -1;
752                 }
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));
757                         close(dir_fd);
758                         return -1;
759                 }
760         }
761
762         for (i = 0; i < need; i++, cur_idx++) {
763                 struct rte_memseg *cur;
764                 void *map_addr;
765
766                 cur = rte_fbarray_get(&cur_msl->memseg_arr, cur_idx);
767                 map_addr = RTE_PTR_ADD(cur_msl->base_va,
768                                 cur_idx * page_sz);
769
770                 if (alloc_seg(cur, map_addr, wa->socket, wa->hi,
771                                 msl_idx, cur_idx)) {
772                         RTE_LOG(DEBUG, EAL, "attempted to allocate %i segments, but only %i were allocated\n",
773                                 need, i);
774
775                         /* if exact number wasn't requested, stop */
776                         if (!wa->exact)
777                                 goto out;
778
779                         /* clean up */
780                         for (j = start_idx; j < cur_idx; j++) {
781                                 struct rte_memseg *tmp;
782                                 struct rte_fbarray *arr =
783                                                 &cur_msl->memseg_arr;
784
785                                 tmp = rte_fbarray_get(arr, j);
786                                 rte_fbarray_set_free(arr, j);
787
788                                 /* free_seg may attempt to create a file, which
789                                  * may fail.
790                                  */
791                                 if (free_seg(tmp, wa->hi, msl_idx, j))
792                                         RTE_LOG(DEBUG, EAL, "Cannot free page\n");
793                         }
794                         /* clear the list */
795                         if (wa->ms)
796                                 memset(wa->ms, 0, sizeof(*wa->ms) * wa->n_segs);
797
798                         if (dir_fd >= 0)
799                                 close(dir_fd);
800                         return -1;
801                 }
802                 if (wa->ms)
803                         wa->ms[i] = cur;
804
805                 rte_fbarray_set_used(&cur_msl->memseg_arr, cur_idx);
806         }
807 out:
808         wa->segs_allocated = i;
809         if (i > 0)
810                 cur_msl->version++;
811         if (dir_fd >= 0)
812                 close(dir_fd);
813         return 1;
814 }
815
816 struct free_walk_param {
817         struct hugepage_info *hi;
818         struct rte_memseg *ms;
819 };
820 static int
821 free_seg_walk(const struct rte_memseg_list *msl, void *arg)
822 {
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;
828
829         start_addr = (uintptr_t) msl->base_va;
830         end_addr = start_addr + msl->memseg_arr.len * (size_t)msl->page_sz;
831
832         if ((uintptr_t)wa->ms->addr < start_addr ||
833                         (uintptr_t)wa->ms->addr >= end_addr)
834                 return 0;
835
836         msl_idx = msl - mcfg->memsegs;
837         seg_idx = RTE_PTR_DIFF(wa->ms->addr, start_addr) / msl->page_sz;
838
839         /* msl is const */
840         found_msl = &mcfg->memsegs[msl_idx];
841
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.
846          *
847          * during init, we already hold a write lock, so don't try to take out
848          * another one.
849          */
850         if (wa->hi->lock_descriptor == -1 && !internal_config.in_memory) {
851                 dir_fd = open(wa->hi->hugedir, O_RDONLY);
852                 if (dir_fd < 0) {
853                         RTE_LOG(ERR, EAL, "%s(): Cannot open '%s': %s\n",
854                                 __func__, wa->hi->hugedir, strerror(errno));
855                         return -1;
856                 }
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));
861                         close(dir_fd);
862                         return -1;
863                 }
864         }
865
866         found_msl->version++;
867
868         rte_fbarray_set_free(&found_msl->memseg_arr, seg_idx);
869
870         ret = free_seg(wa->ms, wa->hi, msl_idx, seg_idx);
871
872         if (dir_fd >= 0)
873                 close(dir_fd);
874
875         if (ret < 0)
876                 return -1;
877
878         return 1;
879 }
880
881 int
882 eal_memalloc_alloc_seg_bulk(struct rte_memseg **ms, int n_segs, size_t page_sz,
883                 int socket, bool exact)
884 {
885         int i, ret = -1;
886 #ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
887         bool have_numa = false;
888         int oldpolicy;
889         struct bitmask *oldmask;
890 #endif
891         struct alloc_walk_param wa;
892         struct hugepage_info *hi = NULL;
893
894         memset(&wa, 0, sizeof(wa));
895
896         /* dynamic allocation not supported in legacy mode */
897         if (internal_config.legacy_mem)
898                 return -1;
899
900         for (i = 0; i < (int) RTE_DIM(internal_config.hugepage_info); i++) {
901                 if (page_sz ==
902                                 internal_config.hugepage_info[i].hugepage_sz) {
903                         hi = &internal_config.hugepage_info[i];
904                         break;
905                 }
906         }
907         if (!hi) {
908                 RTE_LOG(ERR, EAL, "%s(): can't find relevant hugepage_info entry\n",
909                         __func__);
910                 return -1;
911         }
912
913 #ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
914         if (check_numa()) {
915                 oldmask = numa_allocate_nodemask();
916                 prepare_numa(&oldpolicy, oldmask, socket);
917                 have_numa = true;
918         }
919 #endif
920
921         wa.exact = exact;
922         wa.hi = hi;
923         wa.ms = ms;
924         wa.n_segs = n_segs;
925         wa.page_sz = page_sz;
926         wa.socket = socket;
927         wa.segs_allocated = 0;
928
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);
931         if (ret == 0) {
932                 RTE_LOG(ERR, EAL, "%s(): couldn't find suitable memseg_list\n",
933                         __func__);
934                 ret = -1;
935         } else if (ret > 0) {
936                 ret = (int)wa.segs_allocated;
937         }
938
939 #ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
940         if (have_numa)
941                 restore_numa(&oldpolicy, oldmask);
942 #endif
943         return ret;
944 }
945
946 struct rte_memseg *
947 eal_memalloc_alloc_seg(size_t page_sz, int socket)
948 {
949         struct rte_memseg *ms;
950         if (eal_memalloc_alloc_seg_bulk(&ms, 1, page_sz, socket, true) < 0)
951                 return NULL;
952         /* return pointer to newly allocated memseg */
953         return ms;
954 }
955
956 int
957 eal_memalloc_free_seg_bulk(struct rte_memseg **ms, int n_segs)
958 {
959         int seg, ret = 0;
960
961         /* dynamic free not supported in legacy mode */
962         if (internal_config.legacy_mem)
963                 return -1;
964
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;
969                 int i, walk_res;
970
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");
974                         ret = -1;
975                         continue;
976                 }
977
978                 memset(&wa, 0, sizeof(wa));
979
980                 for (i = 0; i < (int)RTE_DIM(internal_config.hugepage_info);
981                                 i++) {
982                         hi = &internal_config.hugepage_info[i];
983                         if (cur->hugepage_sz == hi->hugepage_sz)
984                                 break;
985                 }
986                 if (i == (int)RTE_DIM(internal_config.hugepage_info)) {
987                         RTE_LOG(ERR, EAL, "Can't find relevant hugepage_info entry\n");
988                         ret = -1;
989                         continue;
990                 }
991
992                 wa.ms = cur;
993                 wa.hi = hi;
994
995                 /* memalloc is locked, so it's safe to use thread-unsafe version
996                  */
997                 walk_res = rte_memseg_list_walk_thread_unsafe(free_seg_walk,
998                                 &wa);
999                 if (walk_res == 1)
1000                         continue;
1001                 if (walk_res == 0)
1002                         RTE_LOG(ERR, EAL, "Couldn't find memseg list\n");
1003                 ret = -1;
1004         }
1005         return ret;
1006 }
1007
1008 int
1009 eal_memalloc_free_seg(struct rte_memseg *ms)
1010 {
1011         /* dynamic free not supported in legacy mode */
1012         if (internal_config.legacy_mem)
1013                 return -1;
1014
1015         return eal_memalloc_free_seg_bulk(&ms, 1);
1016 }
1017
1018 static int
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)
1022 {
1023         struct rte_fbarray *l_arr, *p_arr;
1024         int i, ret, chunk_len, diff_len;
1025
1026         l_arr = &local_msl->memseg_arr;
1027         p_arr = &primary_msl->memseg_arr;
1028
1029         /* we need to aggregate allocations/deallocations into bigger chunks,
1030          * as we don't want to spam the user with per-page callbacks.
1031          *
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.
1036          */
1037
1038         chunk_len = end - start;
1039
1040         /* find how many contiguous pages we can map/unmap for this chunk */
1041         diff_len = used ?
1042                         rte_fbarray_find_contig_free(l_arr, start) :
1043                         rte_fbarray_find_contig_used(l_arr, start);
1044
1045         /* has to be at least one page */
1046         if (diff_len < 1)
1047                 return -1;
1048
1049         diff_len = RTE_MIN(chunk_len, diff_len);
1050
1051         /* if we are freeing memory, notify the application */
1052         if (!used) {
1053                 struct rte_memseg *ms;
1054                 void *start_va;
1055                 size_t len, page_sz;
1056
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;
1061
1062                 eal_memalloc_mem_event_notify(RTE_MEM_EVENT_FREE,
1063                                 start_va, len);
1064         }
1065
1066         for (i = 0; i < diff_len; i++) {
1067                 struct rte_memseg *p_ms, *l_ms;
1068                 int seg_idx = start + i;
1069
1070                 l_ms = rte_fbarray_get(l_arr, seg_idx);
1071                 p_ms = rte_fbarray_get(p_arr, seg_idx);
1072
1073                 if (l_ms == NULL || p_ms == NULL)
1074                         return -1;
1075
1076                 if (used) {
1077                         ret = alloc_seg(l_ms, p_ms->addr,
1078                                         p_ms->socket_id, hi,
1079                                         msl_idx, seg_idx);
1080                         if (ret < 0)
1081                                 return -1;
1082                         rte_fbarray_set_used(l_arr, seg_idx);
1083                 } else {
1084                         ret = free_seg(l_ms, hi, msl_idx, seg_idx);
1085                         rte_fbarray_set_free(l_arr, seg_idx);
1086                         if (ret < 0)
1087                                 return -1;
1088                 }
1089         }
1090
1091         /* if we just allocated memory, notify the application */
1092         if (used) {
1093                 struct rte_memseg *ms;
1094                 void *start_va;
1095                 size_t len, page_sz;
1096
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;
1101
1102                 eal_memalloc_mem_event_notify(RTE_MEM_EVENT_ALLOC,
1103                                 start_va, len);
1104         }
1105
1106         /* calculate how much we can advance until next chunk */
1107         diff_len = used ?
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);
1111
1112         return ret;
1113 }
1114
1115 static int
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)
1119 {
1120         struct rte_fbarray *l_arr, *p_arr;
1121         int p_idx, l_chunk_len, p_chunk_len, ret;
1122         int start, end;
1123
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.
1131          *
1132          * we also need to aggregate changes into chunks, as we have to call
1133          * callbacks per allocation, not per page.
1134          */
1135         l_arr = &local_msl->memseg_arr;
1136         p_arr = &primary_msl->memseg_arr;
1137
1138         if (used)
1139                 p_idx = rte_fbarray_find_next_used(p_arr, 0);
1140         else
1141                 p_idx = rte_fbarray_find_next_free(p_arr, 0);
1142
1143         while (p_idx >= 0) {
1144                 int next_chunk_search_idx;
1145
1146                 if (used) {
1147                         p_chunk_len = rte_fbarray_find_contig_used(p_arr,
1148                                         p_idx);
1149                         l_chunk_len = rte_fbarray_find_contig_used(l_arr,
1150                                         p_idx);
1151                 } else {
1152                         p_chunk_len = rte_fbarray_find_contig_free(p_arr,
1153                                         p_idx);
1154                         l_chunk_len = rte_fbarray_find_contig_free(l_arr,
1155                                         p_idx);
1156                 }
1157                 /* best case scenario - no differences (or bigger, which will be
1158                  * fixed during next iteration), look for next chunk
1159                  */
1160                 if (l_chunk_len >= p_chunk_len) {
1161                         next_chunk_search_idx = p_idx + p_chunk_len;
1162                         goto next_chunk;
1163                 }
1164
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
1169                  * chunk.
1170                  */
1171                 start = p_idx + l_chunk_len;
1172                 end = p_idx + p_chunk_len;
1173                 do {
1174                         ret = sync_chunk(primary_msl, local_msl, hi, msl_idx,
1175                                         used, start, end);
1176                         start += ret;
1177                 } while (start < end && ret >= 0);
1178                 /* if ret is negative, something went wrong */
1179                 if (ret < 0)
1180                         return -1;
1181
1182                 next_chunk_search_idx = p_idx + p_chunk_len;
1183 next_chunk:
1184                 /* skip to end of this chunk */
1185                 if (used) {
1186                         p_idx = rte_fbarray_find_next_used(p_arr,
1187                                         next_chunk_search_idx);
1188                 } else {
1189                         p_idx = rte_fbarray_find_next_free(p_arr,
1190                                         next_chunk_search_idx);
1191                 }
1192         }
1193         return 0;
1194 }
1195
1196 static int
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)
1200 {
1201         int ret, dir_fd;
1202
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.
1207          */
1208         dir_fd = open(hi->hugedir, O_RDONLY);
1209         if (dir_fd < 0) {
1210                 RTE_LOG(ERR, EAL, "%s(): Cannot open '%s': %s\n", __func__,
1211                         hi->hugedir, strerror(errno));
1212                 return -1;
1213         }
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));
1218                 close(dir_fd);
1219                 return -1;
1220         }
1221
1222         /* ensure all allocated space is the same in both lists */
1223         ret = sync_status(primary_msl, local_msl, hi, msl_idx, true);
1224         if (ret < 0)
1225                 goto fail;
1226
1227         /* ensure all unallocated space is the same in both lists */
1228         ret = sync_status(primary_msl, local_msl, hi, msl_idx, false);
1229         if (ret < 0)
1230                 goto fail;
1231
1232         /* update version number */
1233         local_msl->version = primary_msl->version;
1234
1235         close(dir_fd);
1236
1237         return 0;
1238 fail:
1239         close(dir_fd);
1240         return -1;
1241 }
1242
1243 static int
1244 sync_walk(const struct rte_memseg_list *msl, void *arg __rte_unused)
1245 {
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;
1249         unsigned int i;
1250         int msl_idx;
1251
1252         msl_idx = msl - mcfg->memsegs;
1253         primary_msl = &mcfg->memsegs[msl_idx];
1254         local_msl = &local_memsegs[msl_idx];
1255
1256         for (i = 0; i < RTE_DIM(internal_config.hugepage_info); i++) {
1257                 uint64_t cur_sz =
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];
1262                         break;
1263                 }
1264         }
1265         if (!hi) {
1266                 RTE_LOG(ERR, EAL, "Can't find relevant hugepage_info entry\n");
1267                 return -1;
1268         }
1269
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))
1273                 return -1;
1274         return 0;
1275 }
1276
1277
1278 int
1279 eal_memalloc_sync_with_primary(void)
1280 {
1281         /* nothing to be done in primary */
1282         if (rte_eal_process_type() == RTE_PROC_PRIMARY)
1283                 return 0;
1284
1285         /* memalloc is locked, so it's safe to call thread-unsafe version */
1286         if (rte_memseg_list_walk_thread_unsafe(sync_walk, NULL))
1287                 return -1;
1288         return 0;
1289 }
1290
1291 static int
1292 secondary_msl_create_walk(const struct rte_memseg_list *msl,
1293                 void *arg __rte_unused)
1294 {
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];
1298         int msl_idx, ret;
1299
1300         msl_idx = msl - mcfg->memsegs;
1301         primary_msl = &mcfg->memsegs[msl_idx];
1302         local_msl = &local_memsegs[msl_idx];
1303
1304         /* create distinct fbarrays for each secondary */
1305         snprintf(name, RTE_FBARRAY_NAME_LEN, "%s_%i",
1306                 primary_msl->memseg_arr.name, getpid());
1307
1308         ret = rte_fbarray_init(&local_msl->memseg_arr, name,
1309                 primary_msl->memseg_arr.len,
1310                 primary_msl->memseg_arr.elt_sz);
1311         if (ret < 0) {
1312                 RTE_LOG(ERR, EAL, "Cannot initialize local memory map\n");
1313                 return -1;
1314         }
1315         local_msl->base_va = primary_msl->base_va;
1316
1317         return 0;
1318 }
1319
1320 static int
1321 secondary_lock_list_create_walk(const struct rte_memseg_list *msl,
1322                 void *arg __rte_unused)
1323 {
1324         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
1325         unsigned int i, len;
1326         int msl_idx;
1327         int *data;
1328
1329         msl_idx = msl - mcfg->memsegs;
1330         len = msl->memseg_arr.len;
1331
1332         /* ensure we have space to store lock fd per each possible segment */
1333         data = malloc(sizeof(int) * len);
1334         if (data == NULL) {
1335                 RTE_LOG(ERR, EAL, "Unable to allocate space for lock descriptors\n");
1336                 return -1;
1337         }
1338         /* set all fd's as invalid */
1339         for (i = 0; i < len; i++)
1340                 data[i] = -1;
1341
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;
1346
1347         return 0;
1348 }
1349
1350 int
1351 eal_memalloc_init(void)
1352 {
1353         if (rte_eal_process_type() == RTE_PROC_SECONDARY)
1354                 if (rte_memseg_list_walk(secondary_msl_create_walk, NULL) < 0)
1355                         return -1;
1356
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))
1360                         return -1;
1361         return 0;
1362 }