mem: rename lock list to fd list
[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 /*
61  * we have two modes - single file segments, and file-per-page mode.
62  *
63  * for single-file segments, we need some kind of mechanism to keep track of
64  * which hugepages can be freed back to the system, and which cannot. we cannot
65  * use flock() because they don't allow locking parts of a file, and we cannot
66  * use fcntl() due to issues with their semantics, so we will have to rely on a
67  * bunch of lockfiles for each page. so, we will use 'fds' array to keep track
68  * of per-page lockfiles. we will store the actual segment list fd in the
69  * 'memseg_list_fd' field.
70  *
71  * for file-per-page mode, each page will have its own fd, so 'memseg_list_fd'
72  * will be invalid (set to -1), and we'll use 'fds' to keep track of page fd's.
73  *
74  * we cannot know how many pages a system will have in advance, but we do know
75  * that they come in lists, and we know lengths of these lists. so, simply store
76  * a malloc'd array of fd's indexed by list and segment index.
77  *
78  * they will be initialized at startup, and filled as we allocate/deallocate
79  * segments.
80  */
81 static struct {
82         int *fds; /**< dynamically allocated array of segment lock fd's */
83         int memseg_list_fd; /**< memseg list fd */
84         int len; /**< total length of the array */
85         int count; /**< entries used in an array */
86 } fd_list[RTE_MAX_MEMSEG_LISTS];
87
88 /** local copy of a memory map, used to synchronize memory hotplug in MP */
89 static struct rte_memseg_list local_memsegs[RTE_MAX_MEMSEG_LISTS];
90
91 static sigjmp_buf huge_jmpenv;
92
93 static void __rte_unused huge_sigbus_handler(int signo __rte_unused)
94 {
95         siglongjmp(huge_jmpenv, 1);
96 }
97
98 /* Put setjmp into a wrap method to avoid compiling error. Any non-volatile,
99  * non-static local variable in the stack frame calling sigsetjmp might be
100  * clobbered by a call to longjmp.
101  */
102 static int __rte_unused huge_wrap_sigsetjmp(void)
103 {
104         return sigsetjmp(huge_jmpenv, 1);
105 }
106
107 static struct sigaction huge_action_old;
108 static int huge_need_recover;
109
110 static void __rte_unused
111 huge_register_sigbus(void)
112 {
113         sigset_t mask;
114         struct sigaction action;
115
116         sigemptyset(&mask);
117         sigaddset(&mask, SIGBUS);
118         action.sa_flags = 0;
119         action.sa_mask = mask;
120         action.sa_handler = huge_sigbus_handler;
121
122         huge_need_recover = !sigaction(SIGBUS, &action, &huge_action_old);
123 }
124
125 static void __rte_unused
126 huge_recover_sigbus(void)
127 {
128         if (huge_need_recover) {
129                 sigaction(SIGBUS, &huge_action_old, NULL);
130                 huge_need_recover = 0;
131         }
132 }
133
134 #ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
135 static bool
136 check_numa(void)
137 {
138         bool ret = true;
139         /* Check if kernel supports NUMA. */
140         if (numa_available() != 0) {
141                 RTE_LOG(DEBUG, EAL, "NUMA is not supported.\n");
142                 ret = false;
143         }
144         return ret;
145 }
146
147 static void
148 prepare_numa(int *oldpolicy, struct bitmask *oldmask, int socket_id)
149 {
150         RTE_LOG(DEBUG, EAL, "Trying to obtain current memory policy.\n");
151         if (get_mempolicy(oldpolicy, oldmask->maskp,
152                           oldmask->size + 1, 0, 0) < 0) {
153                 RTE_LOG(ERR, EAL,
154                         "Failed to get current mempolicy: %s. "
155                         "Assuming MPOL_DEFAULT.\n", strerror(errno));
156                 oldpolicy = MPOL_DEFAULT;
157         }
158         RTE_LOG(DEBUG, EAL,
159                 "Setting policy MPOL_PREFERRED for socket %d\n",
160                 socket_id);
161         numa_set_preferred(socket_id);
162 }
163
164 static void
165 restore_numa(int *oldpolicy, struct bitmask *oldmask)
166 {
167         RTE_LOG(DEBUG, EAL,
168                 "Restoring previous memory policy: %d\n", *oldpolicy);
169         if (*oldpolicy == MPOL_DEFAULT) {
170                 numa_set_localalloc();
171         } else if (set_mempolicy(*oldpolicy, oldmask->maskp,
172                                  oldmask->size + 1) < 0) {
173                 RTE_LOG(ERR, EAL, "Failed to restore mempolicy: %s\n",
174                         strerror(errno));
175                 numa_set_localalloc();
176         }
177         numa_free_cpumask(oldmask);
178 }
179 #endif
180
181 /*
182  * uses fstat to report the size of a file on disk
183  */
184 static off_t
185 get_file_size(int fd)
186 {
187         struct stat st;
188         if (fstat(fd, &st) < 0)
189                 return 0;
190         return st.st_size;
191 }
192
193 /* returns 1 on successful lock, 0 on unsuccessful lock, -1 on error */
194 static int lock(int fd, int type)
195 {
196         int ret;
197
198         /* flock may be interrupted */
199         do {
200                 ret = flock(fd, type | LOCK_NB);
201         } while (ret && errno == EINTR);
202
203         if (ret && errno == EWOULDBLOCK) {
204                 /* couldn't lock */
205                 return 0;
206         } else if (ret) {
207                 RTE_LOG(ERR, EAL, "%s(): error calling flock(): %s\n",
208                         __func__, strerror(errno));
209                 return -1;
210         }
211         /* lock was successful */
212         return 1;
213 }
214
215 static int get_segment_lock_fd(int list_idx, int seg_idx)
216 {
217         char path[PATH_MAX] = {0};
218         int fd;
219
220         if (list_idx < 0 || list_idx >= (int)RTE_DIM(fd_list))
221                 return -1;
222         if (seg_idx < 0 || seg_idx >= fd_list[list_idx].len)
223                 return -1;
224
225         fd = fd_list[list_idx].fds[seg_idx];
226         /* does this lock already exist? */
227         if (fd >= 0)
228                 return fd;
229
230         eal_get_hugefile_lock_path(path, sizeof(path),
231                         list_idx * RTE_MAX_MEMSEG_PER_LIST + seg_idx);
232
233         fd = open(path, O_CREAT | O_RDWR, 0660);
234         if (fd < 0) {
235                 RTE_LOG(ERR, EAL, "%s(): error creating lockfile '%s': %s\n",
236                         __func__, path, strerror(errno));
237                 return -1;
238         }
239         /* take out a read lock */
240         if (lock(fd, LOCK_SH) != 1) {
241                 RTE_LOG(ERR, EAL, "%s(): failed to take out a readlock on '%s': %s\n",
242                         __func__, path, strerror(errno));
243                 close(fd);
244                 return -1;
245         }
246         /* store it for future reference */
247         fd_list[list_idx].fds[seg_idx] = fd;
248         fd_list[list_idx].count++;
249         return fd;
250 }
251
252 static int unlock_segment(int list_idx, int seg_idx)
253 {
254         int fd, ret;
255
256         if (list_idx < 0 || list_idx >= (int)RTE_DIM(fd_list))
257                 return -1;
258         if (seg_idx < 0 || seg_idx >= fd_list[list_idx].len)
259                 return -1;
260
261         fd = fd_list[list_idx].fds[seg_idx];
262
263         /* upgrade lock to exclusive to see if we can remove the lockfile */
264         ret = lock(fd, LOCK_EX);
265         if (ret == 1) {
266                 /* we've succeeded in taking exclusive lock, this lockfile may
267                  * be removed.
268                  */
269                 char path[PATH_MAX] = {0};
270                 eal_get_hugefile_lock_path(path, sizeof(path),
271                                 list_idx * RTE_MAX_MEMSEG_PER_LIST + seg_idx);
272                 if (unlink(path)) {
273                         RTE_LOG(ERR, EAL, "%s(): error removing lockfile '%s': %s\n",
274                                         __func__, path, strerror(errno));
275                 }
276         }
277         /* we don't want to leak the fd, so even if we fail to lock, close fd
278          * and remove it from list anyway.
279          */
280         close(fd);
281         fd_list[list_idx].fds[seg_idx] = -1;
282         fd_list[list_idx].count--;
283
284         if (ret < 0)
285                 return -1;
286         return 0;
287 }
288
289 static int
290 get_seg_fd(char *path, int buflen, struct hugepage_info *hi,
291                 unsigned int list_idx, unsigned int seg_idx)
292 {
293         int fd;
294
295         if (internal_config.single_file_segments) {
296                 /* create a hugepage file path */
297                 eal_get_hugefile_path(path, buflen, hi->hugedir, list_idx);
298
299                 fd = fd_list[list_idx].memseg_list_fd;
300
301                 if (fd < 0) {
302                         fd = open(path, O_CREAT | O_RDWR, 0600);
303                         if (fd < 0) {
304                                 RTE_LOG(ERR, EAL, "%s(): open failed: %s\n",
305                                         __func__, strerror(errno));
306                                 return -1;
307                         }
308                         /* take out a read lock and keep it indefinitely */
309                         if (lock(fd, LOCK_SH) < 0) {
310                                 RTE_LOG(ERR, EAL, "%s(): lock failed: %s\n",
311                                         __func__, strerror(errno));
312                                 close(fd);
313                                 return -1;
314                         }
315                         fd_list[list_idx].memseg_list_fd = fd;
316                 }
317         } else {
318                 /* create a hugepage file path */
319                 eal_get_hugefile_path(path, buflen, hi->hugedir,
320                                 list_idx * RTE_MAX_MEMSEG_PER_LIST + seg_idx);
321                 fd = open(path, O_CREAT | O_RDWR, 0600);
322                 if (fd < 0) {
323                         RTE_LOG(DEBUG, EAL, "%s(): open failed: %s\n", __func__,
324                                         strerror(errno));
325                         return -1;
326                 }
327                 /* take out a read lock */
328                 if (lock(fd, LOCK_SH) < 0) {
329                         RTE_LOG(ERR, EAL, "%s(): lock failed: %s\n",
330                                 __func__, strerror(errno));
331                         close(fd);
332                         return -1;
333                 }
334         }
335         return fd;
336 }
337
338 static int
339 resize_hugefile(int fd, char *path, int list_idx, int seg_idx,
340                 uint64_t fa_offset, uint64_t page_sz, bool grow)
341 {
342         bool again = false;
343         do {
344                 if (fallocate_supported == 0) {
345                         /* we cannot deallocate memory if fallocate() is not
346                          * supported, and hugepage file is already locked at
347                          * creation, so no further synchronization needed.
348                          */
349
350                         if (!grow) {
351                                 RTE_LOG(DEBUG, EAL, "%s(): fallocate not supported, not freeing page back to the system\n",
352                                         __func__);
353                                 return -1;
354                         }
355                         uint64_t new_size = fa_offset + page_sz;
356                         uint64_t cur_size = get_file_size(fd);
357
358                         /* fallocate isn't supported, fall back to ftruncate */
359                         if (new_size > cur_size &&
360                                         ftruncate(fd, new_size) < 0) {
361                                 RTE_LOG(DEBUG, EAL, "%s(): ftruncate() failed: %s\n",
362                                         __func__, strerror(errno));
363                                 return -1;
364                         }
365                 } else {
366                         int flags = grow ? 0 : FALLOC_FL_PUNCH_HOLE |
367                                         FALLOC_FL_KEEP_SIZE;
368                         int ret, lock_fd;
369
370                         /* if fallocate() is supported, we need to take out a
371                          * read lock on allocate (to prevent other processes
372                          * from deallocating this page), and take out a write
373                          * lock on deallocate (to ensure nobody else is using
374                          * this page).
375                          *
376                          * read locks on page itself are already taken out at
377                          * file creation, in get_seg_fd().
378                          *
379                          * we cannot rely on simple use of flock() call, because
380                          * we need to be able to lock a section of the file,
381                          * and we cannot use fcntl() locks, because of numerous
382                          * problems with their semantics, so we will use
383                          * deterministically named lock files for each section
384                          * of the file.
385                          *
386                          * if we're shrinking the file, we want to upgrade our
387                          * lock from shared to exclusive.
388                          *
389                          * lock_fd is an fd for a lockfile, not for the segment
390                          * list.
391                          */
392                         lock_fd = get_segment_lock_fd(list_idx, seg_idx);
393
394                         if (!grow) {
395                                 /* we are using this lockfile to determine
396                                  * whether this particular page is locked, as we
397                                  * are in single file segments mode and thus
398                                  * cannot use regular flock() to get this info.
399                                  *
400                                  * we want to try and take out an exclusive lock
401                                  * on the lock file to determine if we're the
402                                  * last ones using this page, and if not, we
403                                  * won't be shrinking it, and will instead exit
404                                  * prematurely.
405                                  */
406                                 ret = lock(lock_fd, LOCK_EX);
407
408                                 /* drop the lock on the lockfile, so that even
409                                  * if we couldn't shrink the file ourselves, we
410                                  * are signalling to other processes that we're
411                                  * no longer using this page.
412                                  */
413                                 if (unlock_segment(list_idx, seg_idx))
414                                         RTE_LOG(ERR, EAL, "Could not unlock segment\n");
415
416                                 /* additionally, if this was the last lock on
417                                  * this segment list, we can safely close the
418                                  * page file fd, so that one of the processes
419                                  * could then delete the file after shrinking.
420                                  */
421                                 if (ret < 1 && fd_list[list_idx].count == 0) {
422                                         close(fd);
423                                         fd_list[list_idx].memseg_list_fd = -1;
424                                 }
425
426                                 if (ret < 0) {
427                                         RTE_LOG(ERR, EAL, "Could not lock segment\n");
428                                         return -1;
429                                 }
430                                 if (ret == 0)
431                                         /* failed to lock, not an error. */
432                                         return 0;
433                         }
434
435                         /* grow or shrink the file */
436                         ret = fallocate(fd, flags, fa_offset, page_sz);
437
438                         if (ret < 0) {
439                                 if (fallocate_supported == -1 &&
440                                                 errno == ENOTSUP) {
441                                         RTE_LOG(ERR, EAL, "%s(): fallocate() not supported, hugepage deallocation will be disabled\n",
442                                                 __func__);
443                                         again = true;
444                                         fallocate_supported = 0;
445                                 } else {
446                                         RTE_LOG(DEBUG, EAL, "%s(): fallocate() failed: %s\n",
447                                                 __func__,
448                                                 strerror(errno));
449                                         return -1;
450                                 }
451                         } else {
452                                 fallocate_supported = 1;
453
454                                 /* we've grew/shrunk the file, and we hold an
455                                  * exclusive lock now. check if there are no
456                                  * more segments active in this segment list,
457                                  * and remove the file if there aren't.
458                                  */
459                                 if (fd_list[list_idx].count == 0) {
460                                         if (unlink(path))
461                                                 RTE_LOG(ERR, EAL, "%s(): unlinking '%s' failed: %s\n",
462                                                         __func__, path,
463                                                         strerror(errno));
464                                         close(fd);
465                                         fd_list[list_idx].memseg_list_fd = -1;
466                                 }
467                         }
468                 }
469         } while (again);
470         return 0;
471 }
472
473 static int
474 alloc_seg(struct rte_memseg *ms, void *addr, int socket_id,
475                 struct hugepage_info *hi, unsigned int list_idx,
476                 unsigned int seg_idx)
477 {
478 #ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
479         int cur_socket_id = 0;
480 #endif
481         uint64_t map_offset;
482         rte_iova_t iova;
483         void *va;
484         char path[PATH_MAX];
485         int ret = 0;
486         int fd;
487         size_t alloc_sz;
488         int flags;
489         void *new_addr;
490
491         alloc_sz = hi->hugepage_sz;
492         if (!internal_config.single_file_segments &&
493                         internal_config.in_memory &&
494                         anonymous_hugepages_supported) {
495                 int log2, flags;
496
497                 log2 = rte_log2_u32(alloc_sz);
498                 /* as per mmap() manpage, all page sizes are log2 of page size
499                  * shifted by MAP_HUGE_SHIFT
500                  */
501                 flags = (log2 << RTE_MAP_HUGE_SHIFT) | MAP_HUGETLB | MAP_FIXED |
502                                 MAP_PRIVATE | MAP_ANONYMOUS;
503                 fd = -1;
504                 va = mmap(addr, alloc_sz, PROT_READ | PROT_WRITE, flags, -1, 0);
505
506                 /* single-file segments codepath will never be active because
507                  * in-memory mode is incompatible with it and it's stopped at
508                  * EAL initialization stage, however the compiler doesn't know
509                  * that and complains about map_offset being used uninitialized
510                  * on failure codepaths while having in-memory mode enabled. so,
511                  * assign a value here.
512                  */
513                 map_offset = 0;
514         } else {
515                 /* takes out a read lock on segment or segment list */
516                 fd = get_seg_fd(path, sizeof(path), hi, list_idx, seg_idx);
517                 if (fd < 0) {
518                         RTE_LOG(ERR, EAL, "Couldn't get fd on hugepage file\n");
519                         return -1;
520                 }
521
522                 if (internal_config.single_file_segments) {
523                         map_offset = seg_idx * alloc_sz;
524                         ret = resize_hugefile(fd, path, list_idx, seg_idx,
525                                         map_offset, alloc_sz, true);
526                         if (ret < 0)
527                                 goto resized;
528                 } else {
529                         map_offset = 0;
530                         if (ftruncate(fd, alloc_sz) < 0) {
531                                 RTE_LOG(DEBUG, EAL, "%s(): ftruncate() failed: %s\n",
532                                         __func__, strerror(errno));
533                                 goto resized;
534                         }
535                         if (internal_config.hugepage_unlink) {
536                                 if (unlink(path)) {
537                                         RTE_LOG(DEBUG, EAL, "%s(): unlink() failed: %s\n",
538                                                 __func__, strerror(errno));
539                                         goto resized;
540                                 }
541                         }
542                 }
543
544                 /*
545                  * map the segment, and populate page tables, the kernel fills
546                  * this segment with zeros if it's a new page.
547                  */
548                 va = mmap(addr, alloc_sz, PROT_READ | PROT_WRITE,
549                                 MAP_SHARED | MAP_POPULATE | MAP_FIXED, fd,
550                                 map_offset);
551         }
552
553         if (va == MAP_FAILED) {
554                 RTE_LOG(DEBUG, EAL, "%s(): mmap() failed: %s\n", __func__,
555                         strerror(errno));
556                 /* mmap failed, but the previous region might have been
557                  * unmapped anyway. try to remap it
558                  */
559                 goto unmapped;
560         }
561         if (va != addr) {
562                 RTE_LOG(DEBUG, EAL, "%s(): wrong mmap() address\n", __func__);
563                 munmap(va, alloc_sz);
564                 goto resized;
565         }
566
567         /* In linux, hugetlb limitations, like cgroup, are
568          * enforced at fault time instead of mmap(), even
569          * with the option of MAP_POPULATE. Kernel will send
570          * a SIGBUS signal. To avoid to be killed, save stack
571          * environment here, if SIGBUS happens, we can jump
572          * back here.
573          */
574         if (huge_wrap_sigsetjmp()) {
575                 RTE_LOG(DEBUG, EAL, "SIGBUS: Cannot mmap more hugepages of size %uMB\n",
576                         (unsigned int)(alloc_sz >> 20));
577                 goto mapped;
578         }
579
580         /* we need to trigger a write to the page to enforce page fault and
581          * ensure that page is accessible to us, but we can't overwrite value
582          * that is already there, so read the old value, and write itback.
583          * kernel populates the page with zeroes initially.
584          */
585         *(volatile int *)addr = *(volatile int *)addr;
586
587         iova = rte_mem_virt2iova(addr);
588         if (iova == RTE_BAD_PHYS_ADDR) {
589                 RTE_LOG(DEBUG, EAL, "%s(): can't get IOVA addr\n",
590                         __func__);
591                 goto mapped;
592         }
593
594 #ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
595         move_pages(getpid(), 1, &addr, NULL, &cur_socket_id, 0);
596
597         if (cur_socket_id != socket_id) {
598                 RTE_LOG(DEBUG, EAL,
599                                 "%s(): allocation happened on wrong socket (wanted %d, got %d)\n",
600                         __func__, socket_id, cur_socket_id);
601                 goto mapped;
602         }
603 #endif
604         /* for non-single file segments that aren't in-memory, we can close fd
605          * here */
606         if (!internal_config.single_file_segments && !internal_config.in_memory)
607                 close(fd);
608
609         ms->addr = addr;
610         ms->hugepage_sz = alloc_sz;
611         ms->len = alloc_sz;
612         ms->nchannel = rte_memory_get_nchannel();
613         ms->nrank = rte_memory_get_nrank();
614         ms->iova = iova;
615         ms->socket_id = socket_id;
616
617         return 0;
618
619 mapped:
620         munmap(addr, alloc_sz);
621 unmapped:
622         flags = MAP_FIXED;
623 #ifdef RTE_ARCH_PPC_64
624         flags |= MAP_HUGETLB;
625 #endif
626         new_addr = eal_get_virtual_area(addr, &alloc_sz, alloc_sz, 0, flags);
627         if (new_addr != addr) {
628                 if (new_addr != NULL)
629                         munmap(new_addr, alloc_sz);
630                 /* we're leaving a hole in our virtual address space. if
631                  * somebody else maps this hole now, we could accidentally
632                  * override it in the future.
633                  */
634                 RTE_LOG(CRIT, EAL, "Can't mmap holes in our virtual address space\n");
635         }
636 resized:
637         /* in-memory mode will never be single-file-segments mode */
638         if (internal_config.single_file_segments) {
639                 resize_hugefile(fd, path, list_idx, seg_idx, map_offset,
640                                 alloc_sz, false);
641                 /* ignore failure, can't make it any worse */
642         } else {
643                 /* only remove file if we can take out a write lock */
644                 if (internal_config.hugepage_unlink == 0 &&
645                                 internal_config.in_memory == 0 &&
646                                 lock(fd, LOCK_EX) == 1)
647                         unlink(path);
648                 close(fd);
649         }
650         return -1;
651 }
652
653 static int
654 free_seg(struct rte_memseg *ms, struct hugepage_info *hi,
655                 unsigned int list_idx, unsigned int seg_idx)
656 {
657         uint64_t map_offset;
658         char path[PATH_MAX];
659         int fd, ret;
660
661         /* erase page data */
662         memset(ms->addr, 0, ms->len);
663
664         if (mmap(ms->addr, ms->len, PROT_READ,
665                         MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0) ==
666                                 MAP_FAILED) {
667                 RTE_LOG(DEBUG, EAL, "couldn't unmap page\n");
668                 return -1;
669         }
670
671         /* if we've already unlinked the page, nothing needs to be done */
672         if (internal_config.hugepage_unlink) {
673                 memset(ms, 0, sizeof(*ms));
674                 return 0;
675         }
676
677         /* if we are not in single file segments mode, we're going to unmap the
678          * segment and thus drop the lock on original fd, but hugepage dir is
679          * now locked so we can take out another one without races.
680          */
681         fd = get_seg_fd(path, sizeof(path), hi, list_idx, seg_idx);
682         if (fd < 0)
683                 return -1;
684
685         if (internal_config.single_file_segments) {
686                 map_offset = seg_idx * ms->len;
687                 if (resize_hugefile(fd, path, list_idx, seg_idx, map_offset,
688                                 ms->len, false))
689                         return -1;
690                 ret = 0;
691         } else {
692                 /* if we're able to take out a write lock, we're the last one
693                  * holding onto this page.
694                  */
695                 ret = lock(fd, LOCK_EX);
696                 if (ret >= 0) {
697                         /* no one else is using this page */
698                         if (ret == 1)
699                                 unlink(path);
700                 }
701                 /* closing fd will drop the lock */
702                 close(fd);
703         }
704
705         memset(ms, 0, sizeof(*ms));
706
707         return ret < 0 ? -1 : 0;
708 }
709
710 struct alloc_walk_param {
711         struct hugepage_info *hi;
712         struct rte_memseg **ms;
713         size_t page_sz;
714         unsigned int segs_allocated;
715         unsigned int n_segs;
716         int socket;
717         bool exact;
718 };
719 static int
720 alloc_seg_walk(const struct rte_memseg_list *msl, void *arg)
721 {
722         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
723         struct alloc_walk_param *wa = arg;
724         struct rte_memseg_list *cur_msl;
725         size_t page_sz;
726         int cur_idx, start_idx, j, dir_fd = -1;
727         unsigned int msl_idx, need, i;
728
729         if (msl->page_sz != wa->page_sz)
730                 return 0;
731         if (msl->socket_id != wa->socket)
732                 return 0;
733
734         page_sz = (size_t)msl->page_sz;
735
736         msl_idx = msl - mcfg->memsegs;
737         cur_msl = &mcfg->memsegs[msl_idx];
738
739         need = wa->n_segs;
740
741         /* try finding space in memseg list */
742         cur_idx = rte_fbarray_find_next_n_free(&cur_msl->memseg_arr, 0, need);
743         if (cur_idx < 0)
744                 return 0;
745         start_idx = cur_idx;
746
747         /* do not allow any page allocations during the time we're allocating,
748          * because file creation and locking operations are not atomic,
749          * and we might be the first or the last ones to use a particular page,
750          * so we need to ensure atomicity of every operation.
751          *
752          * during init, we already hold a write lock, so don't try to take out
753          * another one.
754          */
755         if (wa->hi->lock_descriptor == -1 && !internal_config.in_memory) {
756                 dir_fd = open(wa->hi->hugedir, O_RDONLY);
757                 if (dir_fd < 0) {
758                         RTE_LOG(ERR, EAL, "%s(): Cannot open '%s': %s\n",
759                                 __func__, wa->hi->hugedir, strerror(errno));
760                         return -1;
761                 }
762                 /* blocking writelock */
763                 if (flock(dir_fd, LOCK_EX)) {
764                         RTE_LOG(ERR, EAL, "%s(): Cannot lock '%s': %s\n",
765                                 __func__, wa->hi->hugedir, strerror(errno));
766                         close(dir_fd);
767                         return -1;
768                 }
769         }
770
771         for (i = 0; i < need; i++, cur_idx++) {
772                 struct rte_memseg *cur;
773                 void *map_addr;
774
775                 cur = rte_fbarray_get(&cur_msl->memseg_arr, cur_idx);
776                 map_addr = RTE_PTR_ADD(cur_msl->base_va,
777                                 cur_idx * page_sz);
778
779                 if (alloc_seg(cur, map_addr, wa->socket, wa->hi,
780                                 msl_idx, cur_idx)) {
781                         RTE_LOG(DEBUG, EAL, "attempted to allocate %i segments, but only %i were allocated\n",
782                                 need, i);
783
784                         /* if exact number wasn't requested, stop */
785                         if (!wa->exact)
786                                 goto out;
787
788                         /* clean up */
789                         for (j = start_idx; j < cur_idx; j++) {
790                                 struct rte_memseg *tmp;
791                                 struct rte_fbarray *arr =
792                                                 &cur_msl->memseg_arr;
793
794                                 tmp = rte_fbarray_get(arr, j);
795                                 rte_fbarray_set_free(arr, j);
796
797                                 /* free_seg may attempt to create a file, which
798                                  * may fail.
799                                  */
800                                 if (free_seg(tmp, wa->hi, msl_idx, j))
801                                         RTE_LOG(DEBUG, EAL, "Cannot free page\n");
802                         }
803                         /* clear the list */
804                         if (wa->ms)
805                                 memset(wa->ms, 0, sizeof(*wa->ms) * wa->n_segs);
806
807                         if (dir_fd >= 0)
808                                 close(dir_fd);
809                         return -1;
810                 }
811                 if (wa->ms)
812                         wa->ms[i] = cur;
813
814                 rte_fbarray_set_used(&cur_msl->memseg_arr, cur_idx);
815         }
816 out:
817         wa->segs_allocated = i;
818         if (i > 0)
819                 cur_msl->version++;
820         if (dir_fd >= 0)
821                 close(dir_fd);
822         return 1;
823 }
824
825 struct free_walk_param {
826         struct hugepage_info *hi;
827         struct rte_memseg *ms;
828 };
829 static int
830 free_seg_walk(const struct rte_memseg_list *msl, void *arg)
831 {
832         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
833         struct rte_memseg_list *found_msl;
834         struct free_walk_param *wa = arg;
835         uintptr_t start_addr, end_addr;
836         int msl_idx, seg_idx, ret, dir_fd = -1;
837
838         start_addr = (uintptr_t) msl->base_va;
839         end_addr = start_addr + msl->memseg_arr.len * (size_t)msl->page_sz;
840
841         if ((uintptr_t)wa->ms->addr < start_addr ||
842                         (uintptr_t)wa->ms->addr >= end_addr)
843                 return 0;
844
845         msl_idx = msl - mcfg->memsegs;
846         seg_idx = RTE_PTR_DIFF(wa->ms->addr, start_addr) / msl->page_sz;
847
848         /* msl is const */
849         found_msl = &mcfg->memsegs[msl_idx];
850
851         /* do not allow any page allocations during the time we're freeing,
852          * because file creation and locking operations are not atomic,
853          * and we might be the first or the last ones to use a particular page,
854          * so we need to ensure atomicity of every operation.
855          *
856          * during init, we already hold a write lock, so don't try to take out
857          * another one.
858          */
859         if (wa->hi->lock_descriptor == -1 && !internal_config.in_memory) {
860                 dir_fd = open(wa->hi->hugedir, O_RDONLY);
861                 if (dir_fd < 0) {
862                         RTE_LOG(ERR, EAL, "%s(): Cannot open '%s': %s\n",
863                                 __func__, wa->hi->hugedir, strerror(errno));
864                         return -1;
865                 }
866                 /* blocking writelock */
867                 if (flock(dir_fd, LOCK_EX)) {
868                         RTE_LOG(ERR, EAL, "%s(): Cannot lock '%s': %s\n",
869                                 __func__, wa->hi->hugedir, strerror(errno));
870                         close(dir_fd);
871                         return -1;
872                 }
873         }
874
875         found_msl->version++;
876
877         rte_fbarray_set_free(&found_msl->memseg_arr, seg_idx);
878
879         ret = free_seg(wa->ms, wa->hi, msl_idx, seg_idx);
880
881         if (dir_fd >= 0)
882                 close(dir_fd);
883
884         if (ret < 0)
885                 return -1;
886
887         return 1;
888 }
889
890 int
891 eal_memalloc_alloc_seg_bulk(struct rte_memseg **ms, int n_segs, size_t page_sz,
892                 int socket, bool exact)
893 {
894         int i, ret = -1;
895 #ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
896         bool have_numa = false;
897         int oldpolicy;
898         struct bitmask *oldmask;
899 #endif
900         struct alloc_walk_param wa;
901         struct hugepage_info *hi = NULL;
902
903         memset(&wa, 0, sizeof(wa));
904
905         /* dynamic allocation not supported in legacy mode */
906         if (internal_config.legacy_mem)
907                 return -1;
908
909         for (i = 0; i < (int) RTE_DIM(internal_config.hugepage_info); i++) {
910                 if (page_sz ==
911                                 internal_config.hugepage_info[i].hugepage_sz) {
912                         hi = &internal_config.hugepage_info[i];
913                         break;
914                 }
915         }
916         if (!hi) {
917                 RTE_LOG(ERR, EAL, "%s(): can't find relevant hugepage_info entry\n",
918                         __func__);
919                 return -1;
920         }
921
922 #ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
923         if (check_numa()) {
924                 oldmask = numa_allocate_nodemask();
925                 prepare_numa(&oldpolicy, oldmask, socket);
926                 have_numa = true;
927         }
928 #endif
929
930         wa.exact = exact;
931         wa.hi = hi;
932         wa.ms = ms;
933         wa.n_segs = n_segs;
934         wa.page_sz = page_sz;
935         wa.socket = socket;
936         wa.segs_allocated = 0;
937
938         /* memalloc is locked, so it's safe to use thread-unsafe version */
939         ret = rte_memseg_list_walk_thread_unsafe(alloc_seg_walk, &wa);
940         if (ret == 0) {
941                 RTE_LOG(ERR, EAL, "%s(): couldn't find suitable memseg_list\n",
942                         __func__);
943                 ret = -1;
944         } else if (ret > 0) {
945                 ret = (int)wa.segs_allocated;
946         }
947
948 #ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
949         if (have_numa)
950                 restore_numa(&oldpolicy, oldmask);
951 #endif
952         return ret;
953 }
954
955 struct rte_memseg *
956 eal_memalloc_alloc_seg(size_t page_sz, int socket)
957 {
958         struct rte_memseg *ms;
959         if (eal_memalloc_alloc_seg_bulk(&ms, 1, page_sz, socket, true) < 0)
960                 return NULL;
961         /* return pointer to newly allocated memseg */
962         return ms;
963 }
964
965 int
966 eal_memalloc_free_seg_bulk(struct rte_memseg **ms, int n_segs)
967 {
968         int seg, ret = 0;
969
970         /* dynamic free not supported in legacy mode */
971         if (internal_config.legacy_mem)
972                 return -1;
973
974         for (seg = 0; seg < n_segs; seg++) {
975                 struct rte_memseg *cur = ms[seg];
976                 struct hugepage_info *hi = NULL;
977                 struct free_walk_param wa;
978                 int i, walk_res;
979
980                 /* if this page is marked as unfreeable, fail */
981                 if (cur->flags & RTE_MEMSEG_FLAG_DO_NOT_FREE) {
982                         RTE_LOG(DEBUG, EAL, "Page is not allowed to be freed\n");
983                         ret = -1;
984                         continue;
985                 }
986
987                 memset(&wa, 0, sizeof(wa));
988
989                 for (i = 0; i < (int)RTE_DIM(internal_config.hugepage_info);
990                                 i++) {
991                         hi = &internal_config.hugepage_info[i];
992                         if (cur->hugepage_sz == hi->hugepage_sz)
993                                 break;
994                 }
995                 if (i == (int)RTE_DIM(internal_config.hugepage_info)) {
996                         RTE_LOG(ERR, EAL, "Can't find relevant hugepage_info entry\n");
997                         ret = -1;
998                         continue;
999                 }
1000
1001                 wa.ms = cur;
1002                 wa.hi = hi;
1003
1004                 /* memalloc is locked, so it's safe to use thread-unsafe version
1005                  */
1006                 walk_res = rte_memseg_list_walk_thread_unsafe(free_seg_walk,
1007                                 &wa);
1008                 if (walk_res == 1)
1009                         continue;
1010                 if (walk_res == 0)
1011                         RTE_LOG(ERR, EAL, "Couldn't find memseg list\n");
1012                 ret = -1;
1013         }
1014         return ret;
1015 }
1016
1017 int
1018 eal_memalloc_free_seg(struct rte_memseg *ms)
1019 {
1020         /* dynamic free not supported in legacy mode */
1021         if (internal_config.legacy_mem)
1022                 return -1;
1023
1024         return eal_memalloc_free_seg_bulk(&ms, 1);
1025 }
1026
1027 static int
1028 sync_chunk(struct rte_memseg_list *primary_msl,
1029                 struct rte_memseg_list *local_msl, struct hugepage_info *hi,
1030                 unsigned int msl_idx, bool used, int start, int end)
1031 {
1032         struct rte_fbarray *l_arr, *p_arr;
1033         int i, ret, chunk_len, diff_len;
1034
1035         l_arr = &local_msl->memseg_arr;
1036         p_arr = &primary_msl->memseg_arr;
1037
1038         /* we need to aggregate allocations/deallocations into bigger chunks,
1039          * as we don't want to spam the user with per-page callbacks.
1040          *
1041          * to avoid any potential issues, we also want to trigger
1042          * deallocation callbacks *before* we actually deallocate
1043          * memory, so that the user application could wrap up its use
1044          * before it goes away.
1045          */
1046
1047         chunk_len = end - start;
1048
1049         /* find how many contiguous pages we can map/unmap for this chunk */
1050         diff_len = used ?
1051                         rte_fbarray_find_contig_free(l_arr, start) :
1052                         rte_fbarray_find_contig_used(l_arr, start);
1053
1054         /* has to be at least one page */
1055         if (diff_len < 1)
1056                 return -1;
1057
1058         diff_len = RTE_MIN(chunk_len, diff_len);
1059
1060         /* if we are freeing memory, notify the application */
1061         if (!used) {
1062                 struct rte_memseg *ms;
1063                 void *start_va;
1064                 size_t len, page_sz;
1065
1066                 ms = rte_fbarray_get(l_arr, start);
1067                 start_va = ms->addr;
1068                 page_sz = (size_t)primary_msl->page_sz;
1069                 len = page_sz * diff_len;
1070
1071                 eal_memalloc_mem_event_notify(RTE_MEM_EVENT_FREE,
1072                                 start_va, len);
1073         }
1074
1075         for (i = 0; i < diff_len; i++) {
1076                 struct rte_memseg *p_ms, *l_ms;
1077                 int seg_idx = start + i;
1078
1079                 l_ms = rte_fbarray_get(l_arr, seg_idx);
1080                 p_ms = rte_fbarray_get(p_arr, seg_idx);
1081
1082                 if (l_ms == NULL || p_ms == NULL)
1083                         return -1;
1084
1085                 if (used) {
1086                         ret = alloc_seg(l_ms, p_ms->addr,
1087                                         p_ms->socket_id, hi,
1088                                         msl_idx, seg_idx);
1089                         if (ret < 0)
1090                                 return -1;
1091                         rte_fbarray_set_used(l_arr, seg_idx);
1092                 } else {
1093                         ret = free_seg(l_ms, hi, msl_idx, seg_idx);
1094                         rte_fbarray_set_free(l_arr, seg_idx);
1095                         if (ret < 0)
1096                                 return -1;
1097                 }
1098         }
1099
1100         /* if we just allocated memory, notify the application */
1101         if (used) {
1102                 struct rte_memseg *ms;
1103                 void *start_va;
1104                 size_t len, page_sz;
1105
1106                 ms = rte_fbarray_get(l_arr, start);
1107                 start_va = ms->addr;
1108                 page_sz = (size_t)primary_msl->page_sz;
1109                 len = page_sz * diff_len;
1110
1111                 eal_memalloc_mem_event_notify(RTE_MEM_EVENT_ALLOC,
1112                                 start_va, len);
1113         }
1114
1115         /* calculate how much we can advance until next chunk */
1116         diff_len = used ?
1117                         rte_fbarray_find_contig_used(l_arr, start) :
1118                         rte_fbarray_find_contig_free(l_arr, start);
1119         ret = RTE_MIN(chunk_len, diff_len);
1120
1121         return ret;
1122 }
1123
1124 static int
1125 sync_status(struct rte_memseg_list *primary_msl,
1126                 struct rte_memseg_list *local_msl, struct hugepage_info *hi,
1127                 unsigned int msl_idx, bool used)
1128 {
1129         struct rte_fbarray *l_arr, *p_arr;
1130         int p_idx, l_chunk_len, p_chunk_len, ret;
1131         int start, end;
1132
1133         /* this is a little bit tricky, but the basic idea is - walk both lists
1134          * and spot any places where there are discrepancies. walking both lists
1135          * and noting discrepancies in a single go is a hard problem, so we do
1136          * it in two passes - first we spot any places where allocated segments
1137          * mismatch (i.e. ensure that everything that's allocated in the primary
1138          * is also allocated in the secondary), and then we do it by looking at
1139          * free segments instead.
1140          *
1141          * we also need to aggregate changes into chunks, as we have to call
1142          * callbacks per allocation, not per page.
1143          */
1144         l_arr = &local_msl->memseg_arr;
1145         p_arr = &primary_msl->memseg_arr;
1146
1147         if (used)
1148                 p_idx = rte_fbarray_find_next_used(p_arr, 0);
1149         else
1150                 p_idx = rte_fbarray_find_next_free(p_arr, 0);
1151
1152         while (p_idx >= 0) {
1153                 int next_chunk_search_idx;
1154
1155                 if (used) {
1156                         p_chunk_len = rte_fbarray_find_contig_used(p_arr,
1157                                         p_idx);
1158                         l_chunk_len = rte_fbarray_find_contig_used(l_arr,
1159                                         p_idx);
1160                 } else {
1161                         p_chunk_len = rte_fbarray_find_contig_free(p_arr,
1162                                         p_idx);
1163                         l_chunk_len = rte_fbarray_find_contig_free(l_arr,
1164                                         p_idx);
1165                 }
1166                 /* best case scenario - no differences (or bigger, which will be
1167                  * fixed during next iteration), look for next chunk
1168                  */
1169                 if (l_chunk_len >= p_chunk_len) {
1170                         next_chunk_search_idx = p_idx + p_chunk_len;
1171                         goto next_chunk;
1172                 }
1173
1174                 /* if both chunks start at the same point, skip parts we know
1175                  * are identical, and sync the rest. each call to sync_chunk
1176                  * will only sync contiguous segments, so we need to call this
1177                  * until we are sure there are no more differences in this
1178                  * chunk.
1179                  */
1180                 start = p_idx + l_chunk_len;
1181                 end = p_idx + p_chunk_len;
1182                 do {
1183                         ret = sync_chunk(primary_msl, local_msl, hi, msl_idx,
1184                                         used, start, end);
1185                         start += ret;
1186                 } while (start < end && ret >= 0);
1187                 /* if ret is negative, something went wrong */
1188                 if (ret < 0)
1189                         return -1;
1190
1191                 next_chunk_search_idx = p_idx + p_chunk_len;
1192 next_chunk:
1193                 /* skip to end of this chunk */
1194                 if (used) {
1195                         p_idx = rte_fbarray_find_next_used(p_arr,
1196                                         next_chunk_search_idx);
1197                 } else {
1198                         p_idx = rte_fbarray_find_next_free(p_arr,
1199                                         next_chunk_search_idx);
1200                 }
1201         }
1202         return 0;
1203 }
1204
1205 static int
1206 sync_existing(struct rte_memseg_list *primary_msl,
1207                 struct rte_memseg_list *local_msl, struct hugepage_info *hi,
1208                 unsigned int msl_idx)
1209 {
1210         int ret, dir_fd;
1211
1212         /* do not allow any page allocations during the time we're allocating,
1213          * because file creation and locking operations are not atomic,
1214          * and we might be the first or the last ones to use a particular page,
1215          * so we need to ensure atomicity of every operation.
1216          */
1217         dir_fd = open(hi->hugedir, O_RDONLY);
1218         if (dir_fd < 0) {
1219                 RTE_LOG(ERR, EAL, "%s(): Cannot open '%s': %s\n", __func__,
1220                         hi->hugedir, strerror(errno));
1221                 return -1;
1222         }
1223         /* blocking writelock */
1224         if (flock(dir_fd, LOCK_EX)) {
1225                 RTE_LOG(ERR, EAL, "%s(): Cannot lock '%s': %s\n", __func__,
1226                         hi->hugedir, strerror(errno));
1227                 close(dir_fd);
1228                 return -1;
1229         }
1230
1231         /* ensure all allocated space is the same in both lists */
1232         ret = sync_status(primary_msl, local_msl, hi, msl_idx, true);
1233         if (ret < 0)
1234                 goto fail;
1235
1236         /* ensure all unallocated space is the same in both lists */
1237         ret = sync_status(primary_msl, local_msl, hi, msl_idx, false);
1238         if (ret < 0)
1239                 goto fail;
1240
1241         /* update version number */
1242         local_msl->version = primary_msl->version;
1243
1244         close(dir_fd);
1245
1246         return 0;
1247 fail:
1248         close(dir_fd);
1249         return -1;
1250 }
1251
1252 static int
1253 sync_walk(const struct rte_memseg_list *msl, void *arg __rte_unused)
1254 {
1255         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
1256         struct rte_memseg_list *primary_msl, *local_msl;
1257         struct hugepage_info *hi = NULL;
1258         unsigned int i;
1259         int msl_idx;
1260
1261         msl_idx = msl - mcfg->memsegs;
1262         primary_msl = &mcfg->memsegs[msl_idx];
1263         local_msl = &local_memsegs[msl_idx];
1264
1265         for (i = 0; i < RTE_DIM(internal_config.hugepage_info); i++) {
1266                 uint64_t cur_sz =
1267                         internal_config.hugepage_info[i].hugepage_sz;
1268                 uint64_t msl_sz = primary_msl->page_sz;
1269                 if (msl_sz == cur_sz) {
1270                         hi = &internal_config.hugepage_info[i];
1271                         break;
1272                 }
1273         }
1274         if (!hi) {
1275                 RTE_LOG(ERR, EAL, "Can't find relevant hugepage_info entry\n");
1276                 return -1;
1277         }
1278
1279         /* if versions don't match, synchronize everything */
1280         if (local_msl->version != primary_msl->version &&
1281                         sync_existing(primary_msl, local_msl, hi, msl_idx))
1282                 return -1;
1283         return 0;
1284 }
1285
1286
1287 int
1288 eal_memalloc_sync_with_primary(void)
1289 {
1290         /* nothing to be done in primary */
1291         if (rte_eal_process_type() == RTE_PROC_PRIMARY)
1292                 return 0;
1293
1294         /* memalloc is locked, so it's safe to call thread-unsafe version */
1295         if (rte_memseg_list_walk_thread_unsafe(sync_walk, NULL))
1296                 return -1;
1297         return 0;
1298 }
1299
1300 static int
1301 secondary_msl_create_walk(const struct rte_memseg_list *msl,
1302                 void *arg __rte_unused)
1303 {
1304         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
1305         struct rte_memseg_list *primary_msl, *local_msl;
1306         char name[PATH_MAX];
1307         int msl_idx, ret;
1308
1309         msl_idx = msl - mcfg->memsegs;
1310         primary_msl = &mcfg->memsegs[msl_idx];
1311         local_msl = &local_memsegs[msl_idx];
1312
1313         /* create distinct fbarrays for each secondary */
1314         snprintf(name, RTE_FBARRAY_NAME_LEN, "%s_%i",
1315                 primary_msl->memseg_arr.name, getpid());
1316
1317         ret = rte_fbarray_init(&local_msl->memseg_arr, name,
1318                 primary_msl->memseg_arr.len,
1319                 primary_msl->memseg_arr.elt_sz);
1320         if (ret < 0) {
1321                 RTE_LOG(ERR, EAL, "Cannot initialize local memory map\n");
1322                 return -1;
1323         }
1324         local_msl->base_va = primary_msl->base_va;
1325
1326         return 0;
1327 }
1328
1329 static int
1330 fd_list_create_walk(const struct rte_memseg_list *msl,
1331                 void *arg __rte_unused)
1332 {
1333         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
1334         unsigned int i, len;
1335         int msl_idx;
1336         int *data;
1337
1338         msl_idx = msl - mcfg->memsegs;
1339         len = msl->memseg_arr.len;
1340
1341         /* ensure we have space to store fd per each possible segment */
1342         data = malloc(sizeof(int) * len);
1343         if (data == NULL) {
1344                 RTE_LOG(ERR, EAL, "Unable to allocate space for file descriptors\n");
1345                 return -1;
1346         }
1347         /* set all fd's as invalid */
1348         for (i = 0; i < len; i++)
1349                 data[i] = -1;
1350
1351         fd_list[msl_idx].fds = data;
1352         fd_list[msl_idx].len = len;
1353         fd_list[msl_idx].count = 0;
1354         fd_list[msl_idx].memseg_list_fd = -1;
1355
1356         return 0;
1357 }
1358
1359 int
1360 eal_memalloc_init(void)
1361 {
1362         if (rte_eal_process_type() == RTE_PROC_SECONDARY)
1363                 if (rte_memseg_list_walk(secondary_msl_create_walk, NULL) < 0)
1364                         return -1;
1365
1366         /* initialize all of the fd lists */
1367         if (internal_config.single_file_segments)
1368                 if (rte_memseg_list_walk(fd_list_create_walk, NULL))
1369                         return -1;
1370         return 0;
1371 }