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