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