Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2010-2014 Intel Corporation.
3 : : * Copyright(c) 2013 6WIND S.A.
4 : : */
5 : :
6 : : #include <errno.h>
7 : : #include <fcntl.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/stat.h>
16 : : #include <sys/file.h>
17 : : #include <sys/resource.h>
18 : : #include <sys/personality.h>
19 : : #include <unistd.h>
20 : : #include <limits.h>
21 : : #include <signal.h>
22 : : #include <setjmp.h>
23 : : #ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
24 : : #include <numa.h>
25 : : #include <numaif.h>
26 : : #endif
27 : :
28 : : #include <rte_errno.h>
29 : : #include <rte_log.h>
30 : : #include <rte_memory.h>
31 : : #include <rte_eal.h>
32 : : #include <rte_lcore.h>
33 : : #include <rte_common.h>
34 : :
35 : : #include <eal_export.h>
36 : : #include "eal_private.h"
37 : : #include "eal_memalloc.h"
38 : : #include "eal_memcfg.h"
39 : : #include "eal_internal_cfg.h"
40 : : #include "eal_filesystem.h"
41 : : #include "eal_hugepages.h"
42 : : #include "eal_options.h"
43 : :
44 : : #define PFN_MASK_SIZE 8
45 : :
46 : : /**
47 : : * @file
48 : : * Huge page mapping under linux
49 : : *
50 : : * To reserve a big contiguous amount of memory, we use the hugepage
51 : : * feature of linux. For that, we need to have hugetlbfs mounted. This
52 : : * code will create many files in this directory (one per page) and
53 : : * map them in virtual memory. For each page, we will retrieve its
54 : : * physical address and remap it in order to have a virtual contiguous
55 : : * zone as well as a physical contiguous zone.
56 : : */
57 : :
58 : : static int phys_addrs_available = -1;
59 : :
60 : : #define RANDOMIZE_VA_SPACE_FILE "/proc/sys/kernel/randomize_va_space"
61 : :
62 : 196 : uint64_t eal_get_baseaddr(void)
63 : : {
64 : : /*
65 : : * Linux kernel uses a really high address as starting address for
66 : : * serving mmaps calls. If there exists addressing limitations and IOVA
67 : : * mode is VA, this starting address is likely too high for those
68 : : * devices. However, it is possible to use a lower address in the
69 : : * process virtual address space as with 64 bits there is a lot of
70 : : * available space.
71 : : *
72 : : * Current known limitations are 39 or 40 bits. Setting the starting
73 : : * address at 4GB implies there are 508GB or 1020GB for mapping the
74 : : * available hugepages. This is likely enough for most systems, although
75 : : * a device with addressing limitations should call
76 : : * rte_mem_check_dma_mask for ensuring all memory is within supported
77 : : * range.
78 : : */
79 : : #if defined(RTE_ARCH_LOONGARCH)
80 : : return 0x7000000000ULL;
81 : : #else
82 : 196 : return 0x100000000ULL;
83 : : #endif
84 : : }
85 : :
86 : : /*
87 : : * Get physical address of any mapped virtual address in the current process.
88 : : */
89 : : RTE_EXPORT_SYMBOL(rte_mem_virt2phy)
90 : : phys_addr_t
91 : 4768 : rte_mem_virt2phy(const void *virtaddr)
92 : : {
93 : : int fd, retval;
94 : : uint64_t page, physaddr;
95 : : unsigned long virt_pfn;
96 : : int page_size;
97 : : off_t offset;
98 : :
99 [ + - ]: 4768 : if (phys_addrs_available == 0)
100 : : return RTE_BAD_IOVA;
101 : :
102 : : /* standard page size */
103 : 4768 : page_size = getpagesize();
104 : :
105 : : fd = open("/proc/self/pagemap", O_RDONLY);
106 [ - + ]: 4768 : if (fd < 0) {
107 : 0 : EAL_LOG(INFO, "%s(): cannot open /proc/self/pagemap: %s",
108 : : __func__, strerror(errno));
109 : 0 : return RTE_BAD_IOVA;
110 : : }
111 : :
112 : 4768 : virt_pfn = (unsigned long)virtaddr / page_size;
113 : 4768 : offset = sizeof(uint64_t) * virt_pfn;
114 [ - + ]: 4768 : if (lseek(fd, offset, SEEK_SET) == (off_t) -1) {
115 : 0 : EAL_LOG(INFO, "%s(): seek error in /proc/self/pagemap: %s",
116 : : __func__, strerror(errno));
117 : 0 : close(fd);
118 : 0 : return RTE_BAD_IOVA;
119 : : }
120 : :
121 : 4768 : retval = read(fd, &page, PFN_MASK_SIZE);
122 : 4768 : close(fd);
123 [ - + ]: 4768 : if (retval < 0) {
124 : 0 : EAL_LOG(INFO, "%s(): cannot read /proc/self/pagemap: %s",
125 : : __func__, strerror(errno));
126 : 0 : return RTE_BAD_IOVA;
127 [ - + ]: 4768 : } else if (retval != PFN_MASK_SIZE) {
128 : 0 : EAL_LOG(INFO, "%s(): read %d bytes from /proc/self/pagemap "
129 : : "but expected %d:",
130 : : __func__, retval, PFN_MASK_SIZE);
131 : 0 : return RTE_BAD_IOVA;
132 : : }
133 : :
134 : : /*
135 : : * the pfn (page frame number) are bits 0-54 (see
136 : : * pagemap.txt in linux Documentation)
137 : : */
138 [ + - ]: 4768 : if ((page & 0x7fffffffffffffULL) == 0)
139 : : return RTE_BAD_IOVA;
140 : :
141 : 4768 : physaddr = ((page & 0x7fffffffffffffULL) * page_size)
142 : 4768 : + ((unsigned long)virtaddr % page_size);
143 : :
144 : 4768 : return physaddr;
145 : : }
146 : :
147 : : RTE_EXPORT_SYMBOL(rte_mem_virt2iova)
148 : : rte_iova_t
149 : 2623 : rte_mem_virt2iova(const void *virtaddr)
150 : : {
151 [ - + ]: 2623 : if (rte_eal_iova_mode() == RTE_IOVA_VA)
152 : 0 : return (uintptr_t)virtaddr;
153 : 2623 : return rte_mem_virt2phy(virtaddr);
154 : : }
155 : :
156 : : /*
157 : : * For each hugepage in hugepg_tbl, fill the physaddr value. We find
158 : : * it by browsing the /proc/self/pagemap special file.
159 : : */
160 : : static int
161 : 2 : find_physaddrs(struct hugepage_file *hugepg_tbl, struct hugepage_info *hpi)
162 : : {
163 : : unsigned int i;
164 : : phys_addr_t addr;
165 : :
166 [ + + ]: 2048 : for (i = 0; i < hpi->num_pages[0]; i++) {
167 : 2046 : addr = rte_mem_virt2phy(hugepg_tbl[i].orig_va);
168 [ + - ]: 2046 : if (addr == RTE_BAD_PHYS_ADDR)
169 : : return -1;
170 : 2046 : hugepg_tbl[i].physaddr = addr;
171 : : }
172 : : return 0;
173 : : }
174 : :
175 : : /*
176 : : * For each hugepage in hugepg_tbl, fill the physaddr value sequentially.
177 : : */
178 : : static int
179 : : set_physaddrs(struct hugepage_file *hugepg_tbl, struct hugepage_info *hpi)
180 : : {
181 : : unsigned int i;
182 : : static phys_addr_t addr;
183 : :
184 [ # # ]: 0 : for (i = 0; i < hpi->num_pages[0]; i++) {
185 : 0 : hugepg_tbl[i].physaddr = addr;
186 : 0 : addr += hugepg_tbl[i].size;
187 : : }
188 : : return 0;
189 : : }
190 : :
191 : : /*
192 : : * Check whether address-space layout randomization is enabled in
193 : : * the kernel. This is important for multi-process as it can prevent
194 : : * two processes mapping data to the same virtual address
195 : : * Returns:
196 : : * 0 - address space randomization disabled
197 : : * 1/2 - address space randomization enabled
198 : : * negative error code on error
199 : : */
200 : : static int
201 : 1 : aslr_enabled(void)
202 : : {
203 : : char c;
204 : :
205 : : /*
206 : : * Check whether the current process is executed with the command line
207 : : * "setarch ... --addr-no-randomize ..." or "setarch ... -R ..."
208 : : * This complements the sysfs check to ensure comprehensive ASLR status detection.
209 : : * This check is necessary to support the functionality of the "setarch" command,
210 : : * which can disable ASLR by setting the ADDR_NO_RANDOMIZE personality flag.
211 : : */
212 [ + - ]: 1 : if ((personality(0xffffffff) & ADDR_NO_RANDOMIZE) == ADDR_NO_RANDOMIZE)
213 : : return 0;
214 : :
215 : : int retval, fd = open(RANDOMIZE_VA_SPACE_FILE, O_RDONLY);
216 [ - + ]: 1 : if (fd < 0)
217 : 0 : return -errno;
218 : 1 : retval = read(fd, &c, 1);
219 : 1 : close(fd);
220 [ - + ]: 1 : if (retval < 0)
221 : 0 : return -errno;
222 [ + - ]: 1 : if (retval == 0)
223 : : return -EIO;
224 [ - + ]: 1 : switch (c) {
225 : : case '0' : return 0;
226 : : case '1' : return 1;
227 : : case '2' : return 2;
228 : : default: return -EINVAL;
229 : : }
230 : : }
231 : :
232 : : static sigjmp_buf huge_jmpenv;
233 : :
234 : 0 : static void huge_sigbus_handler(int signo __rte_unused)
235 : : {
236 : 0 : siglongjmp(huge_jmpenv, 1);
237 : : }
238 : :
239 : : /* Put setjmp into a wrap method to avoid compiling error. Any non-volatile,
240 : : * non-static local variable in the stack frame calling sigsetjmp might be
241 : : * clobbered by a call to longjmp.
242 : : */
243 : 2046 : static int huge_wrap_sigsetjmp(void)
244 : : {
245 : 2046 : return sigsetjmp(huge_jmpenv, 1);
246 : : }
247 : :
248 : : #ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
249 : : /* Callback for numa library. */
250 : : void numa_error(char *where)
251 : : {
252 : 0 : EAL_LOG(ERR, "%s failed: %s", where, strerror(errno));
253 : 0 : }
254 : : #endif
255 : :
256 : : /*
257 : : * Mmap all hugepages of hugepage table: it first open a file in
258 : : * hugetlbfs, then mmap() hugepage_sz data in it. If orig is set, the
259 : : * virtual address is stored in hugepg_tbl[i].orig_va, else it is stored
260 : : * in hugepg_tbl[i].final_va. The second mapping (when orig is 0) tries to
261 : : * map contiguous physical blocks in contiguous virtual blocks.
262 : : */
263 : : static unsigned
264 : 2 : map_all_hugepages(struct hugepage_file *hugepg_tbl, struct hugepage_info *hpi,
265 : : uint64_t *essential_memory __rte_unused)
266 : : {
267 : : int fd;
268 : : unsigned i;
269 : : void *virtaddr;
270 : : #ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
271 : : int node_id = -1;
272 : : int essential_prev = 0;
273 : : int oldpolicy;
274 : : struct bitmask *oldmask = NULL;
275 : : bool have_numa = true;
276 : : unsigned long maxnode = 0;
277 : : const struct internal_config *internal_conf =
278 : 2 : eal_get_internal_configuration();
279 : :
280 : : /* Check if kernel supports NUMA. */
281 [ + - ]: 2 : if (numa_available() != 0) {
282 : 0 : EAL_LOG(DEBUG, "NUMA is not supported.");
283 : : have_numa = false;
284 : : }
285 : :
286 : : if (have_numa) {
287 : 2 : EAL_LOG(DEBUG, "Trying to obtain current memory policy.");
288 : 2 : oldmask = numa_allocate_nodemask();
289 [ - + ]: 2 : if (get_mempolicy(&oldpolicy, oldmask->maskp,
290 : 2 : oldmask->size + 1, 0, 0) < 0) {
291 : 0 : EAL_LOG(ERR,
292 : : "Failed to get current mempolicy: %s. "
293 : : "Assuming MPOL_DEFAULT.", strerror(errno));
294 : 0 : oldpolicy = MPOL_DEFAULT;
295 : : }
296 [ + + ]: 66 : for (i = 0; i < RTE_MAX_NUMA_NODES; i++)
297 [ - + ]: 64 : if (internal_conf->numa_mem[i])
298 : 0 : maxnode = i + 1;
299 : : }
300 : : #endif
301 : :
302 [ + + ]: 2048 : for (i = 0; i < hpi->num_pages[0]; i++) {
303 : 2046 : struct hugepage_file *hf = &hugepg_tbl[i];
304 : 2046 : uint64_t hugepage_sz = hpi->hugepage_sz;
305 : :
306 : : #ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
307 [ - + ]: 2046 : if (maxnode) {
308 : : unsigned int j;
309 : :
310 [ # # ]: 0 : for (j = 0; j < maxnode; j++)
311 [ # # ]: 0 : if (essential_memory[j])
312 : : break;
313 : :
314 [ # # ]: 0 : if (j == maxnode) {
315 : 0 : node_id = (node_id + 1) % maxnode;
316 [ # # ]: 0 : while (!internal_conf->numa_mem[node_id]) {
317 : 0 : node_id++;
318 : 0 : node_id %= maxnode;
319 : : }
320 : : essential_prev = 0;
321 : : } else {
322 : 0 : node_id = j;
323 : 0 : essential_prev = essential_memory[j];
324 : :
325 [ # # ]: 0 : if (essential_memory[j] < hugepage_sz)
326 : 0 : essential_memory[j] = 0;
327 : : else
328 : 0 : essential_memory[j] -= hugepage_sz;
329 : : }
330 : :
331 : 0 : EAL_LOG(DEBUG,
332 : : "Setting policy MPOL_PREFERRED for socket %d",
333 : : node_id);
334 : 0 : numa_set_preferred(node_id);
335 : : }
336 : : #endif
337 : :
338 : 2046 : hf->file_id = i;
339 : 2046 : hf->size = hugepage_sz;
340 [ - + ]: 2046 : if (eal_get_hugefile_path(hf->filepath, sizeof(hf->filepath), hpi->hugedir,
341 : : hf->file_id) == NULL) {
342 : 0 : EAL_LOG(DEBUG, "%s(): huge file path '%s' truncated",
343 : : __func__, hf->filepath);
344 : 0 : goto out;
345 : : }
346 : :
347 : 2046 : hf->filepath[sizeof(hf->filepath) - 1] = '\0';
348 : :
349 : : /* try to create hugepage file */
350 : : fd = open(hf->filepath, O_CREAT | O_RDWR, 0600);
351 [ - + ]: 2046 : if (fd < 0) {
352 : 0 : EAL_LOG(DEBUG, "%s(): open failed: %s", __func__,
353 : : strerror(errno));
354 : 0 : goto out;
355 : : }
356 : :
357 : : /* map the segment, and populate page tables,
358 : : * the kernel fills this segment with zeros. we don't care where
359 : : * this gets mapped - we already have contiguous memory areas
360 : : * ready for us to map into.
361 : : */
362 : 2046 : virtaddr = mmap(NULL, hugepage_sz, PROT_READ | PROT_WRITE,
363 : : MAP_SHARED | MAP_POPULATE, fd, 0);
364 [ - + ]: 2046 : if (virtaddr == MAP_FAILED) {
365 : 0 : EAL_LOG(DEBUG, "%s(): mmap failed: %s", __func__,
366 : : strerror(errno));
367 : 0 : close(fd);
368 : 0 : goto out;
369 : : }
370 : :
371 : 2046 : hf->orig_va = virtaddr;
372 : :
373 : : /* In linux, hugetlb limitations, like cgroup, are
374 : : * enforced at fault time instead of mmap(), even
375 : : * with the option of MAP_POPULATE. Kernel will send
376 : : * a SIGBUS signal. To avoid to be killed, save stack
377 : : * environment here, if SIGBUS happens, we can jump
378 : : * back here.
379 : : */
380 [ - + ]: 2046 : if (huge_wrap_sigsetjmp()) {
381 : 0 : EAL_LOG(DEBUG, "SIGBUS: Cannot mmap more "
382 : : "hugepages of size %u MB",
383 : : (unsigned int)(hugepage_sz / 0x100000));
384 : 0 : munmap(virtaddr, hugepage_sz);
385 : 0 : close(fd);
386 : 0 : unlink(hugepg_tbl[i].filepath);
387 : : #ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
388 [ # # ]: 0 : if (maxnode)
389 : 0 : essential_memory[node_id] =
390 : : essential_prev;
391 : : #endif
392 : 0 : goto out;
393 : : }
394 : 2046 : *(int *)virtaddr = 0;
395 : :
396 : : /* set shared lock on the file. */
397 [ - + ]: 2046 : if (flock(fd, LOCK_SH) < 0) {
398 : 0 : EAL_LOG(DEBUG, "%s(): Locking file failed:%s ",
399 : : __func__, strerror(errno));
400 : 0 : close(fd);
401 : 0 : goto out;
402 : : }
403 : :
404 : 2046 : close(fd);
405 : : }
406 : :
407 : 2 : out:
408 : : #ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
409 [ - + ]: 2 : if (maxnode) {
410 : 0 : EAL_LOG(DEBUG,
411 : : "Restoring previous memory policy: %d", oldpolicy);
412 [ # # ]: 0 : if (oldpolicy == MPOL_DEFAULT) {
413 : 0 : numa_set_localalloc();
414 [ # # ]: 0 : } else if (set_mempolicy(oldpolicy, oldmask->maskp,
415 : 0 : oldmask->size + 1) < 0) {
416 : 0 : EAL_LOG(ERR, "Failed to restore mempolicy: %s",
417 : : strerror(errno));
418 : 0 : numa_set_localalloc();
419 : : }
420 : : }
421 [ + - ]: 2 : if (oldmask != NULL)
422 : : numa_free_cpumask(oldmask);
423 : : #endif
424 : 2 : return i;
425 : : }
426 : :
427 : : /*
428 : : * Parse /proc/self/numa_maps to get the NUMA socket ID for each huge
429 : : * page.
430 : : */
431 : : static int
432 : 2 : find_numasocket(struct hugepage_file *hugepg_tbl, struct hugepage_info *hpi)
433 : : {
434 : : int socket_id;
435 : : char *end, *nodestr;
436 : : unsigned i, hp_count = 0;
437 : : uint64_t virt_addr;
438 : : char buf[BUFSIZ];
439 : : char *hugedir_str;
440 : : FILE *f;
441 : : int ret;
442 : :
443 : 2 : f = fopen("/proc/self/numa_maps", "r");
444 [ - + ]: 2 : if (f == NULL) {
445 : 0 : EAL_LOG(NOTICE, "NUMA support not available"
446 : : " consider that all memory is in socket_id 0");
447 : 0 : return 0;
448 : : }
449 : :
450 : 2 : ret = asprintf(&hugedir_str, "%s/%s",
451 : 2 : hpi->hugedir, eal_get_hugefile_prefix());
452 [ - + ]: 2 : if (ret < 0) {
453 : 0 : EAL_LOG(ERR, "%s(): failed to store hugepage path", __func__);
454 : 0 : hugedir_str = NULL;
455 : 0 : goto error;
456 : : }
457 : :
458 : : ret = -1;
459 : :
460 : : /* parse numa map */
461 [ + + ]: 2366 : while (fgets(buf, sizeof(buf), f) != NULL) {
462 : :
463 : : /* ignore non huge page */
464 [ + + ]: 2364 : if (strstr(buf, " huge ") == NULL &&
465 [ + - ]: 318 : strstr(buf, hugedir_str) == NULL)
466 : 318 : continue;
467 : :
468 : : /* get zone addr */
469 : 2046 : virt_addr = strtoull(buf, &end, 16);
470 [ + - - + ]: 2046 : if (virt_addr == 0 || end == buf) {
471 : 0 : EAL_LOG(ERR, "%s(): error in numa_maps parsing", __func__);
472 : 0 : goto error;
473 : : }
474 : :
475 : : /* get node id (socket id) */
476 : 2046 : nodestr = strstr(buf, " N");
477 [ - + ]: 2046 : if (nodestr == NULL) {
478 : 0 : EAL_LOG(ERR, "%s(): error in numa_maps parsing", __func__);
479 : 0 : goto error;
480 : : }
481 : 2046 : nodestr += 2;
482 : 2046 : end = strstr(nodestr, "=");
483 [ - + ]: 2046 : if (end == NULL) {
484 : 0 : EAL_LOG(ERR, "%s(): error in numa_maps parsing", __func__);
485 : 0 : goto error;
486 : : }
487 : 2046 : end[0] = '\0';
488 : 2046 : end = NULL;
489 : :
490 : 2046 : socket_id = strtoul(nodestr, &end, 0);
491 [ + - + - : 2046 : if ((nodestr[0] == '\0') || (end == NULL) || (*end != '\0')) {
- + ]
492 : 0 : EAL_LOG(ERR, "%s(): error in numa_maps parsing", __func__);
493 : 0 : goto error;
494 : : }
495 : :
496 : : /* if we find this page in our mappings, set socket_id */
497 [ + + ]: 2095104 : for (i = 0; i < hpi->num_pages[0]; i++) {
498 : 2093058 : void *va = (void *)(unsigned long)virt_addr;
499 [ + + ]: 2093058 : if (hugepg_tbl[i].orig_va == va) {
500 : 2046 : hugepg_tbl[i].socket_id = socket_id;
501 : 2046 : hp_count++;
502 : : #ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
503 : 2046 : EAL_LOG(DEBUG,
504 : : "Hugepage %s is on socket %d",
505 : : hugepg_tbl[i].filepath, socket_id);
506 : : #endif
507 : : }
508 : : }
509 : : }
510 : :
511 [ - + ]: 2 : if (hp_count < hpi->num_pages[0])
512 : 0 : goto error;
513 : :
514 : : ret = 0;
515 : 2 : error:
516 : 2 : free(hugedir_str);
517 : 2 : fclose(f);
518 : 2 : return ret;
519 : : }
520 : :
521 : : static int
522 : 10498 : cmp_physaddr(const void *a, const void *b)
523 : : {
524 : : #ifndef RTE_ARCH_PPC_64
525 : : const struct hugepage_file *p1 = a;
526 : : const struct hugepage_file *p2 = b;
527 : : #else
528 : : /* PowerPC needs memory sorted in reverse order from x86 */
529 : : const struct hugepage_file *p1 = b;
530 : : const struct hugepage_file *p2 = a;
531 : : #endif
532 [ + + ]: 10498 : if (p1->physaddr < p2->physaddr)
533 : : return -1;
534 [ - + ]: 9174 : else if (p1->physaddr > p2->physaddr)
535 : : return 1;
536 : : else
537 : 0 : return 0;
538 : : }
539 : :
540 : : /*
541 : : * Uses mmap to create a shared memory area for storage of data
542 : : * Used in this file to store the hugepage file map on disk
543 : : */
544 : : static void *
545 : 2 : create_shared_memory(const char *filename, const size_t mem_size)
546 : : {
547 : : void *retval;
548 : : int fd;
549 : : const struct internal_config *internal_conf =
550 : 2 : eal_get_internal_configuration();
551 : :
552 : : /* if no shared files mode is used, create anonymous memory instead */
553 [ - + ]: 2 : if (internal_conf->no_shconf) {
554 : 0 : retval = mmap(NULL, mem_size, PROT_READ | PROT_WRITE,
555 : : MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
556 [ # # ]: 0 : if (retval == MAP_FAILED)
557 : : return NULL;
558 : 0 : return retval;
559 : : }
560 : :
561 : : fd = open(filename, O_CREAT | O_RDWR, 0600);
562 [ + - ]: 2 : if (fd < 0)
563 : : return NULL;
564 [ - + ]: 2 : if (ftruncate(fd, mem_size) < 0) {
565 : 0 : close(fd);
566 : 0 : return NULL;
567 : : }
568 : 2 : retval = mmap(NULL, mem_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
569 : 2 : close(fd);
570 [ - + ]: 2 : if (retval == MAP_FAILED)
571 : 0 : return NULL;
572 : : return retval;
573 : : }
574 : :
575 : : /*
576 : : * this copies *active* hugepages from one hugepage table to another.
577 : : * destination is typically the shared memory.
578 : : */
579 : : static int
580 : 2 : copy_hugepages_to_shared_mem(struct hugepage_file * dst, int dest_size,
581 : : const struct hugepage_file * src, int src_size)
582 : : {
583 : : int src_pos, dst_pos = 0;
584 : :
585 [ + + ]: 2048 : for (src_pos = 0; src_pos < src_size; src_pos++) {
586 [ + + ]: 2046 : if (src[src_pos].orig_va != NULL) {
587 : : /* error on overflow attempt */
588 [ + - ]: 18 : if (dst_pos == dest_size)
589 : : return -1;
590 : 18 : memcpy(&dst[dst_pos], &src[src_pos], sizeof(struct hugepage_file));
591 : 18 : dst_pos++;
592 : : }
593 : : }
594 : : return 0;
595 : : }
596 : :
597 : : static int
598 : 0 : unlink_hugepage_files(struct hugepage_file *hugepg_tbl,
599 : : unsigned num_hp_info)
600 : : {
601 : : unsigned socket, size;
602 : : int page, nrpages = 0;
603 : : const struct internal_config *internal_conf =
604 : 0 : eal_get_internal_configuration();
605 : :
606 : : /* get total number of hugepages */
607 [ # # ]: 0 : for (size = 0; size < num_hp_info; size++)
608 [ # # ]: 0 : for (socket = 0; socket < RTE_MAX_NUMA_NODES; socket++)
609 : 0 : nrpages +=
610 : 0 : internal_conf->hugepage_info[size].num_pages[socket];
611 : :
612 [ # # ]: 0 : for (page = 0; page < nrpages; page++) {
613 : 0 : struct hugepage_file *hp = &hugepg_tbl[page];
614 : :
615 [ # # # # ]: 0 : if (hp->orig_va != NULL && unlink(hp->filepath)) {
616 : 0 : EAL_LOG(WARNING, "%s(): Removing %s failed: %s",
617 : : __func__, hp->filepath, strerror(errno));
618 : : }
619 : : }
620 : 0 : return 0;
621 : : }
622 : :
623 : : /*
624 : : * unmaps hugepages that are not going to be used. since we originally allocate
625 : : * ALL hugepages (not just those we need), additional unmapping needs to be done.
626 : : */
627 : : static int
628 : 2 : unmap_unneeded_hugepages(struct hugepage_file *hugepg_tbl,
629 : : struct hugepage_info *hpi,
630 : : unsigned num_hp_info)
631 : : {
632 : : unsigned socket, size;
633 : : int page, nrpages = 0;
634 : : const struct internal_config *internal_conf =
635 : 2 : eal_get_internal_configuration();
636 : :
637 : : /* get total number of hugepages */
638 [ + + ]: 4 : for (size = 0; size < num_hp_info; size++)
639 [ + + ]: 66 : for (socket = 0; socket < RTE_MAX_NUMA_NODES; socket++)
640 : 64 : nrpages += internal_conf->hugepage_info[size].num_pages[socket];
641 : :
642 [ + + ]: 4 : for (size = 0; size < num_hp_info; size++) {
643 [ + + ]: 66 : for (socket = 0; socket < RTE_MAX_NUMA_NODES; socket++) {
644 : : unsigned pages_found = 0;
645 : :
646 : : /* traverse until we have unmapped all the unused pages */
647 [ + + ]: 65536 : for (page = 0; page < nrpages; page++) {
648 : 65472 : struct hugepage_file *hp = &hugepg_tbl[page];
649 : :
650 : : /* find a page that matches the criteria */
651 [ + - ]: 65472 : if ((hp->size == hpi[size].hugepage_sz) &&
652 [ + + ]: 65472 : (hp->socket_id == (int) socket)) {
653 : :
654 : : /* if we skipped enough pages, unmap the rest */
655 [ + + ]: 2046 : if (pages_found == hpi[size].num_pages[socket]) {
656 : : uint64_t unmap_len;
657 : :
658 : : unmap_len = hp->size;
659 : :
660 : : /* get start addr and len of the remaining segment */
661 : 2028 : munmap(hp->orig_va,
662 : : (size_t)unmap_len);
663 : :
664 : 2028 : hp->orig_va = NULL;
665 [ - + ]: 2028 : if (unlink(hp->filepath) == -1) {
666 : 0 : EAL_LOG(ERR, "%s(): Removing %s failed: %s",
667 : : __func__, hp->filepath, strerror(errno));
668 : 0 : return -1;
669 : : }
670 : : } else {
671 : : /* lock the page and skip */
672 : 18 : pages_found++;
673 : : }
674 : :
675 : : } /* match page */
676 : : } /* foreach page */
677 : : } /* foreach socket */
678 : : } /* foreach pagesize */
679 : :
680 : : return 0;
681 : : }
682 : :
683 : : static int
684 : 6 : remap_segment(struct hugepage_file *hugepages, int seg_start, int seg_end)
685 : : {
686 : 6 : struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
687 : : struct rte_memseg_list *msl;
688 : : struct rte_fbarray *arr;
689 : : int cur_page, seg_len;
690 : : unsigned int msl_idx;
691 : : int ms_idx;
692 : : uint64_t page_sz;
693 : : size_t memseg_len;
694 : : int socket_id;
695 : : #ifndef RTE_ARCH_64
696 : : const struct internal_config *internal_conf =
697 : : eal_get_internal_configuration();
698 : : #endif
699 : 6 : page_sz = hugepages[seg_start].size;
700 : 6 : socket_id = hugepages[seg_start].socket_id;
701 : 6 : seg_len = seg_end - seg_start;
702 : :
703 : 6 : EAL_LOG(DEBUG, "Attempting to map %" PRIu64 "M on socket %i",
704 : : (seg_len * page_sz) >> 20ULL, socket_id);
705 : :
706 : : /* find free space in memseg lists */
707 [ + - ]: 6 : for (msl_idx = 0; msl_idx < RTE_MAX_MEMSEG_LISTS; msl_idx++) {
708 : : int free_len;
709 : : bool empty;
710 : 6 : msl = &mcfg->memsegs[msl_idx];
711 : 6 : arr = &msl->memseg_arr;
712 : :
713 [ - + ]: 6 : if (msl->page_sz != page_sz)
714 : 0 : continue;
715 [ - + ]: 6 : if (msl->socket_id != socket_id)
716 : 0 : continue;
717 : :
718 : : /* leave space for a hole if array is not empty */
719 : 6 : empty = arr->count == 0;
720 : : /* find start of the biggest contiguous block and its size */
721 : 6 : ms_idx = rte_fbarray_find_biggest_free(arr, 0);
722 [ - + ]: 6 : if (ms_idx < 0)
723 : 0 : continue;
724 : : /* hole is 1 segment long, so at least two segments long. */
725 : 6 : free_len = rte_fbarray_find_contig_free(arr, ms_idx);
726 [ - + ]: 6 : if (free_len < 2)
727 : 0 : continue;
728 : : /* leave some space between memsegs, they are not IOVA
729 : : * contiguous, so they shouldn't be VA contiguous either.
730 : : */
731 [ + + ]: 6 : if (!empty) {
732 : 4 : ms_idx++;
733 : 4 : free_len--;
734 : : }
735 : :
736 : : /* we might not get all of the space we wanted */
737 : 6 : free_len = RTE_MIN(seg_len, free_len);
738 : 6 : seg_end = seg_start + free_len;
739 : : seg_len = seg_end - seg_start;
740 : 6 : break;
741 : : }
742 [ - + ]: 6 : if (msl_idx == RTE_MAX_MEMSEG_LISTS) {
743 : 0 : EAL_LOG(ERR,
744 : : "Could not find suitable space for memseg in existing memseg lists");
745 : 0 : return -1;
746 : : }
747 : :
748 : : #ifdef RTE_ARCH_PPC_64
749 : : /* for PPC64 we go through the list backwards */
750 : : for (cur_page = seg_end - 1; cur_page >= seg_start;
751 : : cur_page--, ms_idx++) {
752 : : #else
753 [ + + ]: 24 : for (cur_page = seg_start; cur_page < seg_end; cur_page++, ms_idx++) {
754 : : #endif
755 : 18 : struct hugepage_file *hfile = &hugepages[cur_page];
756 : 18 : struct rte_memseg *ms = rte_fbarray_get(arr, ms_idx);
757 : : void *addr;
758 : : int fd;
759 : :
760 : 18 : fd = open(hfile->filepath, O_RDWR);
761 [ - + ]: 18 : if (fd < 0) {
762 : 0 : EAL_LOG(ERR, "Could not open '%s': %s",
763 : : hfile->filepath, strerror(errno));
764 : 0 : return -1;
765 : : }
766 : : /* set shared lock on the file. */
767 [ - + ]: 18 : if (flock(fd, LOCK_SH) < 0) {
768 : 0 : EAL_LOG(DEBUG, "Could not lock '%s': %s",
769 : : hfile->filepath, strerror(errno));
770 : 0 : close(fd);
771 : 0 : return -1;
772 : : }
773 : : memseg_len = (size_t)page_sz;
774 : :
775 [ - + ]: 18 : if (msl->base_va == NULL) {
776 : 0 : EAL_LOG(ERR, "Base VA is NULL for memseg list");
777 : 0 : close(fd);
778 : 0 : return -1;
779 : : }
780 : :
781 : 18 : addr = RTE_PTR_ADD(msl->base_va, ms_idx * memseg_len);
782 : :
783 : : /* we know this address is already mmapped by memseg list, so
784 : : * using MAP_FIXED here is safe
785 : : */
786 : 18 : addr = mmap(addr, page_sz, PROT_READ | PROT_WRITE,
787 : : MAP_SHARED | MAP_POPULATE | MAP_FIXED, fd, 0);
788 [ - + ]: 18 : if (addr == MAP_FAILED) {
789 : 0 : EAL_LOG(ERR, "Couldn't remap '%s': %s",
790 : : hfile->filepath, strerror(errno));
791 : 0 : close(fd);
792 : 0 : return -1;
793 : : }
794 : :
795 : : /* we have a new address, so unmap previous one */
796 : : #ifndef RTE_ARCH_64
797 : : /* in 32-bit legacy mode, we have already unmapped the page */
798 : : if (!internal_conf->legacy_mem)
799 : : munmap(hfile->orig_va, page_sz);
800 : : #else
801 : 18 : munmap(hfile->orig_va, page_sz);
802 : : #endif
803 : :
804 : 18 : hfile->orig_va = NULL;
805 : 18 : hfile->final_va = addr;
806 : :
807 : : /* rewrite physical addresses in IOVA as VA mode */
808 [ - + ]: 18 : if (rte_eal_iova_mode() == RTE_IOVA_VA)
809 : 0 : hfile->physaddr = (uintptr_t)addr;
810 : :
811 : : /* set up memseg data */
812 : 18 : ms->addr = addr;
813 : 18 : ms->hugepage_sz = page_sz;
814 : 18 : ms->len = memseg_len;
815 : 18 : ms->iova = hfile->physaddr;
816 : 18 : ms->socket_id = hfile->socket_id;
817 : 18 : ms->nchannel = rte_memory_get_nchannel();
818 : 18 : ms->nrank = rte_memory_get_nrank();
819 : :
820 : 18 : rte_fbarray_set_used(arr, ms_idx);
821 : :
822 : : /* store segment fd internally */
823 [ - + ]: 18 : if (eal_memalloc_set_seg_fd(msl_idx, ms_idx, fd) < 0)
824 : 0 : EAL_LOG(ERR, "Could not store segment fd: %s",
825 : : rte_strerror(rte_errno));
826 : : }
827 : 6 : EAL_LOG(DEBUG, "Allocated %" PRIu64 "M on socket %i",
828 : : (seg_len * page_sz) >> 20, socket_id);
829 : 6 : return seg_len;
830 : : }
831 : :
832 : : static int
833 : : memseg_list_free(struct rte_memseg_list *msl)
834 : : {
835 : : if (rte_fbarray_destroy(&msl->memseg_arr)) {
836 : : EAL_LOG(ERR, "Cannot destroy memseg list");
837 : : return -1;
838 : : }
839 : : memset(msl, 0, sizeof(*msl));
840 : : return 0;
841 : : }
842 : :
843 : : /*
844 : : * Our VA space is not preallocated yet, so preallocate it here. We need to know
845 : : * how many segments there are in order to map all pages into one address space,
846 : : * and leave appropriate holes between segments so that rte_malloc does not
847 : : * concatenate them into one big segment.
848 : : *
849 : : * we also need to unmap original pages to free up address space.
850 : : */
851 : : static int __rte_unused
852 : : prealloc_segments(struct hugepage_file *hugepages, int n_pages)
853 : : {
854 : : struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
855 : : int cur_page, seg_start_page, end_seg, new_memseg;
856 : : unsigned int hpi_idx, socket, i;
857 : : int n_contig_segs, n_segs;
858 : : int msl_idx;
859 : : const struct internal_config *internal_conf =
860 : : eal_get_internal_configuration();
861 : :
862 : : /* before we preallocate segments, we need to free up our VA space.
863 : : * we're not removing files, and we already have information about
864 : : * PA-contiguousness, so it is safe to unmap everything.
865 : : */
866 : : for (cur_page = 0; cur_page < n_pages; cur_page++) {
867 : : struct hugepage_file *hpi = &hugepages[cur_page];
868 : : munmap(hpi->orig_va, hpi->size);
869 : : hpi->orig_va = NULL;
870 : : }
871 : :
872 : : /* we cannot know how many page sizes and sockets we have discovered, so
873 : : * loop over all of them
874 : : */
875 : : for (hpi_idx = 0; hpi_idx < internal_conf->num_hugepage_sizes;
876 : : hpi_idx++) {
877 : : uint64_t page_sz =
878 : : internal_conf->hugepage_info[hpi_idx].hugepage_sz;
879 : :
880 : : for (i = 0; i < rte_socket_count(); i++) {
881 : : struct rte_memseg_list *msl;
882 : :
883 : : socket = rte_socket_id_by_idx(i);
884 : : n_contig_segs = 0;
885 : : n_segs = 0;
886 : : seg_start_page = -1;
887 : :
888 : : for (cur_page = 0; cur_page < n_pages; cur_page++) {
889 : : struct hugepage_file *prev, *cur;
890 : : int prev_seg_start_page = -1;
891 : :
892 : : cur = &hugepages[cur_page];
893 : : prev = cur_page == 0 ? NULL :
894 : : &hugepages[cur_page - 1];
895 : :
896 : : new_memseg = 0;
897 : : end_seg = 0;
898 : :
899 : : if (cur->size == 0)
900 : : end_seg = 1;
901 : : else if (cur->socket_id != (int) socket)
902 : : end_seg = 1;
903 : : else if (cur->size != page_sz)
904 : : end_seg = 1;
905 : : else if (cur_page == 0)
906 : : new_memseg = 1;
907 : : #ifdef RTE_ARCH_PPC_64
908 : : /* On PPC64 architecture, the mmap always start
909 : : * from higher address to lower address. Here,
910 : : * physical addresses are in descending order.
911 : : */
912 : : else if ((prev->physaddr - cur->physaddr) !=
913 : : cur->size)
914 : : new_memseg = 1;
915 : : #else
916 : : else if ((cur->physaddr - prev->physaddr) !=
917 : : cur->size)
918 : : new_memseg = 1;
919 : : #endif
920 : : if (new_memseg) {
921 : : /* if we're already inside a segment,
922 : : * new segment means end of current one
923 : : */
924 : : if (seg_start_page != -1) {
925 : : end_seg = 1;
926 : : prev_seg_start_page =
927 : : seg_start_page;
928 : : }
929 : : seg_start_page = cur_page;
930 : : }
931 : :
932 : : if (end_seg) {
933 : : if (prev_seg_start_page != -1) {
934 : : /* we've found a new segment */
935 : : n_contig_segs++;
936 : : n_segs += cur_page -
937 : : prev_seg_start_page;
938 : : } else if (seg_start_page != -1) {
939 : : /* we didn't find new segment,
940 : : * but did end current one
941 : : */
942 : : n_contig_segs++;
943 : : n_segs += cur_page -
944 : : seg_start_page;
945 : : seg_start_page = -1;
946 : : continue;
947 : : } else {
948 : : /* we're skipping this page */
949 : : continue;
950 : : }
951 : : }
952 : : /* segment continues */
953 : : }
954 : : /* check if we missed last segment */
955 : : if (seg_start_page != -1) {
956 : : n_contig_segs++;
957 : : n_segs += cur_page - seg_start_page;
958 : : }
959 : :
960 : : /* if no segments were found, do not preallocate */
961 : : if (n_segs == 0)
962 : : continue;
963 : :
964 : : /* we now have total number of pages that we will
965 : : * allocate for this segment list. add separator pages
966 : : * to the total count, and preallocate VA space.
967 : : */
968 : : n_segs += n_contig_segs - 1;
969 : :
970 : : /* now, preallocate VA space for these segments */
971 : :
972 : : /* first, find suitable memseg list for this */
973 : : for (msl_idx = 0; msl_idx < RTE_MAX_MEMSEG_LISTS;
974 : : msl_idx++) {
975 : : msl = &mcfg->memsegs[msl_idx];
976 : :
977 : : if (msl->base_va != NULL)
978 : : continue;
979 : : break;
980 : : }
981 : : if (msl_idx == RTE_MAX_MEMSEG_LISTS) {
982 : : EAL_LOG(ERR, "Not enough space in memseg lists, please increase RTE_MAX_MEMSEG_LISTS");
983 : : return -1;
984 : : }
985 : :
986 : : /* now, allocate fbarray itself */
987 : : if (eal_memseg_list_init(msl, page_sz, n_segs,
988 : : socket, msl_idx, true) < 0)
989 : : return -1;
990 : :
991 : : /* finally, allocate VA space */
992 : : if (eal_memseg_list_alloc(msl, 0) < 0) {
993 : : EAL_LOG(ERR, "Cannot preallocate 0x%"PRIx64"kB hugepages",
994 : : page_sz >> 10);
995 : : return -1;
996 : : }
997 : : }
998 : : }
999 : : return 0;
1000 : : }
1001 : :
1002 : : /*
1003 : : * We cannot reallocate memseg lists on the fly because PPC64 stores pages
1004 : : * backwards, therefore we have to process the entire memseg first before
1005 : : * remapping it into memseg list VA space.
1006 : : */
1007 : : static int
1008 : 2 : remap_needed_hugepages(struct hugepage_file *hugepages, int n_pages)
1009 : : {
1010 : : int cur_page, seg_start_page, new_memseg, ret;
1011 : :
1012 : : seg_start_page = 0;
1013 [ + - ]: 20 : for (cur_page = 0; cur_page < n_pages; cur_page++) {
1014 : : struct hugepage_file *prev, *cur;
1015 : :
1016 : : new_memseg = 0;
1017 : :
1018 : 20 : cur = &hugepages[cur_page];
1019 [ + + ]: 20 : prev = cur_page == 0 ? NULL : &hugepages[cur_page - 1];
1020 : :
1021 : : /* if size is zero, no more pages left */
1022 [ + + ]: 20 : if (cur->size == 0)
1023 : : break;
1024 : :
1025 [ + + ]: 18 : if (cur_page == 0)
1026 : : new_memseg = 1;
1027 [ + - ]: 16 : else if (cur->socket_id != prev->socket_id)
1028 : : new_memseg = 1;
1029 [ + - ]: 16 : else if (cur->size != prev->size)
1030 : : new_memseg = 1;
1031 : : #ifdef RTE_ARCH_PPC_64
1032 : : /* On PPC64 architecture, the mmap always start from higher
1033 : : * address to lower address. Here, physical addresses are in
1034 : : * descending order.
1035 : : */
1036 : : else if ((prev->physaddr - cur->physaddr) != cur->size)
1037 : : new_memseg = 1;
1038 : : #else
1039 [ + + ]: 16 : else if ((cur->physaddr - prev->physaddr) != cur->size)
1040 : : new_memseg = 1;
1041 : : #endif
1042 : :
1043 : : if (new_memseg) {
1044 : : /* if this isn't the first time, remap segment */
1045 [ + + ]: 6 : if (cur_page != 0) {
1046 : : int n_remapped = 0;
1047 : 4 : int n_needed = cur_page - seg_start_page;
1048 [ + + ]: 8 : while (n_remapped < n_needed) {
1049 : 4 : ret = remap_segment(hugepages, seg_start_page,
1050 : : cur_page);
1051 [ + - ]: 4 : if (ret < 0)
1052 : : return -1;
1053 : 4 : n_remapped += ret;
1054 : 4 : seg_start_page += ret;
1055 : : }
1056 : : }
1057 : : /* remember where we started */
1058 : : seg_start_page = cur_page;
1059 : : }
1060 : : /* continuation of previous memseg */
1061 : : }
1062 : : /* we were stopped, but we didn't remap the last segment, do it now */
1063 [ + - ]: 2 : if (cur_page != 0) {
1064 : : int n_remapped = 0;
1065 : 2 : int n_needed = cur_page - seg_start_page;
1066 [ + + ]: 4 : while (n_remapped < n_needed) {
1067 : 2 : ret = remap_segment(hugepages, seg_start_page,
1068 : : cur_page);
1069 [ + - ]: 2 : if (ret < 0)
1070 : : return -1;
1071 : 2 : n_remapped += ret;
1072 : 2 : seg_start_page += ret;
1073 : : }
1074 : : }
1075 : : return 0;
1076 : : }
1077 : :
1078 : : static inline size_t
1079 : 0 : eal_get_hugepage_mem_size(void)
1080 : : {
1081 : : uint64_t size = 0;
1082 : : unsigned i, j;
1083 : : struct internal_config *internal_conf =
1084 : 0 : eal_get_internal_configuration();
1085 : :
1086 [ # # ]: 0 : for (i = 0; i < internal_conf->num_hugepage_sizes; i++) {
1087 : : struct hugepage_info *hpi = &internal_conf->hugepage_info[i];
1088 [ # # ]: 0 : if (strnlen(hpi->hugedir, sizeof(hpi->hugedir)) != 0) {
1089 [ # # ]: 0 : for (j = 0; j < RTE_MAX_NUMA_NODES; j++) {
1090 : 0 : size += hpi->hugepage_sz * hpi->num_pages[j];
1091 : : }
1092 : : }
1093 : : }
1094 : :
1095 : 0 : return (size < SIZE_MAX) ? (size_t)(size) : SIZE_MAX;
1096 : : }
1097 : :
1098 : : static struct sigaction huge_action_old;
1099 : : static int huge_need_recover;
1100 : :
1101 : : static void
1102 : 2 : huge_register_sigbus(void)
1103 : : {
1104 : : sigset_t mask;
1105 : : struct sigaction action;
1106 : :
1107 : 2 : sigemptyset(&mask);
1108 : 2 : sigaddset(&mask, SIGBUS);
1109 : 2 : action.sa_flags = 0;
1110 : 2 : action.sa_mask = mask;
1111 : 2 : action.sa_handler = huge_sigbus_handler;
1112 : :
1113 : 2 : huge_need_recover = !sigaction(SIGBUS, &action, &huge_action_old);
1114 : 2 : }
1115 : :
1116 : : static void
1117 : : huge_recover_sigbus(void)
1118 : : {
1119 [ + - ]: 2 : if (huge_need_recover) {
1120 : 2 : sigaction(SIGBUS, &huge_action_old, NULL);
1121 : 2 : huge_need_recover = 0;
1122 : : }
1123 : : }
1124 : :
1125 : : /*
1126 : : * Prepare physical memory mapping: fill configuration structure with
1127 : : * these infos, return 0 on success.
1128 : : * 1. map N huge pages in separate files in hugetlbfs
1129 : : * 2. find associated physical addr
1130 : : * 3. find associated NUMA socket ID
1131 : : * 4. sort all huge pages by physical address
1132 : : * 5. remap these N huge pages in the correct order
1133 : : * 6. unmap the first mapping
1134 : : * 7. fill memsegs in configuration with contiguous zones
1135 : : */
1136 : : static int
1137 : 133 : eal_legacy_hugepage_init(void)
1138 : : {
1139 : : struct rte_mem_config *mcfg;
1140 : : struct hugepage_file *hugepage = NULL, *tmp_hp = NULL;
1141 : : struct hugepage_info used_hp[MAX_HUGEPAGE_SIZES];
1142 : : struct internal_config *internal_conf =
1143 : 133 : eal_get_internal_configuration();
1144 : :
1145 : : uint64_t memory[RTE_MAX_NUMA_NODES];
1146 : :
1147 : : unsigned hp_offset;
1148 : : int i, j;
1149 : : int nr_hugefiles, nr_hugepages = 0;
1150 : : void *addr;
1151 : :
1152 : : memset(used_hp, 0, sizeof(used_hp));
1153 : :
1154 : : /* get pointer to global configuration */
1155 : 133 : mcfg = rte_eal_get_configuration()->mem_config;
1156 : :
1157 : : /* hugetlbfs can be disabled */
1158 [ + + ]: 133 : if (internal_conf->no_hugetlbfs) {
1159 : : void *prealloc_addr;
1160 : : size_t mem_sz;
1161 : : struct rte_memseg_list *msl;
1162 : : int n_segs, fd, flags;
1163 : : int memfd;
1164 : : uint64_t page_sz;
1165 : :
1166 : : /* nohuge mode is legacy mode */
1167 : 131 : internal_conf->legacy_mem = 1;
1168 : :
1169 : : /* nohuge mode is single-file segments mode */
1170 : 131 : internal_conf->single_file_segments = 1;
1171 : :
1172 : : /* create a memseg list */
1173 : 131 : msl = &mcfg->memsegs[0];
1174 : :
1175 : 131 : mem_sz = internal_conf->memory;
1176 : : page_sz = RTE_PGSIZE_4K;
1177 : 131 : n_segs = mem_sz / page_sz;
1178 : :
1179 [ + - ]: 131 : if (eal_memseg_list_init_named(
1180 : : msl, "nohugemem", page_sz, n_segs, 0, true)) {
1181 : : return -1;
1182 : : }
1183 : :
1184 : : /* set up parameters for anonymous mmap */
1185 : : fd = -1;
1186 : : flags = MAP_PRIVATE | MAP_ANONYMOUS;
1187 : :
1188 : : /* create a memfd and store it in the segment fd table */
1189 : 131 : memfd = memfd_create("nohuge", 0);
1190 [ - + ]: 131 : if (memfd < 0) {
1191 : 0 : EAL_LOG(DEBUG, "Cannot create memfd: %s",
1192 : : strerror(errno));
1193 : 0 : EAL_LOG(DEBUG, "Falling back to anonymous map");
1194 : : } else {
1195 : : /* we got an fd - now resize it */
1196 [ - + ]: 131 : if (ftruncate(memfd, internal_conf->memory) < 0) {
1197 : 0 : EAL_LOG(ERR, "Cannot resize memfd: %s",
1198 : : strerror(errno));
1199 : 0 : EAL_LOG(ERR, "Falling back to anonymous map");
1200 : 0 : close(memfd);
1201 : : } else {
1202 : : /* creating memfd-backed file was successful.
1203 : : * we want changes to memfd to be visible to
1204 : : * other processes (such as vhost backend), so
1205 : : * map it as shared memory.
1206 : : */
1207 : 131 : EAL_LOG(DEBUG, "Using memfd for anonymous memory");
1208 : : fd = memfd;
1209 : : flags = MAP_SHARED;
1210 : : }
1211 : : }
1212 : : /* preallocate address space for the memory, so that it can be
1213 : : * fit into the DMA mask.
1214 : : */
1215 [ - + ]: 131 : if (eal_memseg_list_alloc(msl, 0)) {
1216 : 0 : EAL_LOG(ERR, "Cannot preallocate VA space for hugepage memory");
1217 : 0 : return -1;
1218 : : }
1219 : :
1220 : 131 : prealloc_addr = msl->base_va;
1221 : 131 : addr = mmap(prealloc_addr, mem_sz, PROT_READ | PROT_WRITE,
1222 : : flags | MAP_FIXED, fd, 0);
1223 [ - + ]: 131 : if (addr == MAP_FAILED || addr != prealloc_addr) {
1224 : 0 : EAL_LOG(ERR, "%s: mmap() failed: %s", __func__,
1225 : : strerror(errno));
1226 : 0 : munmap(prealloc_addr, mem_sz);
1227 : 0 : return -1;
1228 : : }
1229 : :
1230 : : /* we're in single-file segments mode, so only the segment list
1231 : : * fd needs to be set up.
1232 : : */
1233 [ + - ]: 131 : if (fd != -1) {
1234 [ - + ]: 131 : if (eal_memalloc_set_seg_list_fd(0, fd) < 0) {
1235 : 0 : EAL_LOG(ERR, "Cannot set up segment list fd");
1236 : : /* not a serious error, proceed */
1237 : : }
1238 : : }
1239 : :
1240 : 131 : eal_memseg_list_populate(msl, addr, n_segs);
1241 : :
1242 [ - + - - ]: 131 : if (mcfg->dma_maskbits &&
1243 : 0 : rte_mem_check_dma_mask_thread_unsafe(mcfg->dma_maskbits)) {
1244 : 0 : EAL_LOG(ERR,
1245 : : "%s(): couldn't allocate memory due to IOVA exceeding limits of current DMA mask.",
1246 : : __func__);
1247 [ # # # # ]: 0 : if (rte_eal_iova_mode() == RTE_IOVA_VA &&
1248 : 0 : rte_eal_using_phys_addrs())
1249 : 0 : EAL_LOG(ERR,
1250 : : "%s(): Please try initializing EAL with --iova-mode=pa parameter.",
1251 : : __func__);
1252 : 0 : goto fail;
1253 : : }
1254 : 131 : return 0;
1255 : : }
1256 : :
1257 : : /* calculate total number of hugepages available. at this point we haven't
1258 : : * yet started sorting them so they all are on socket 0 */
1259 [ + + ]: 4 : for (i = 0; i < (int) internal_conf->num_hugepage_sizes; i++) {
1260 : : /* meanwhile, also initialize used_hp hugepage sizes in used_hp */
1261 : 2 : used_hp[i].hugepage_sz = internal_conf->hugepage_info[i].hugepage_sz;
1262 : :
1263 : 2 : nr_hugepages += internal_conf->hugepage_info[i].num_pages[0];
1264 : : }
1265 : :
1266 : : /*
1267 : : * allocate a memory area for hugepage table.
1268 : : * this isn't shared memory yet. due to the fact that we need some
1269 : : * processing done on these pages, shared memory will be created
1270 : : * at a later stage.
1271 : : */
1272 : 2 : tmp_hp = malloc(nr_hugepages * sizeof(struct hugepage_file));
1273 [ - + ]: 2 : if (tmp_hp == NULL)
1274 : 0 : goto fail;
1275 : :
1276 : : memset(tmp_hp, 0, nr_hugepages * sizeof(struct hugepage_file));
1277 : :
1278 : : hp_offset = 0; /* where we start the current page size entries */
1279 : :
1280 : 2 : huge_register_sigbus();
1281 : :
1282 : : /* make a copy of numa_mem, needed for balanced allocation. */
1283 [ + + ]: 66 : for (i = 0; i < RTE_MAX_NUMA_NODES; i++)
1284 : 64 : memory[i] = internal_conf->numa_mem[i];
1285 : :
1286 : : /* map all hugepages and sort them */
1287 [ + + ]: 4 : for (i = 0; i < (int)internal_conf->num_hugepage_sizes; i++) {
1288 : : unsigned pages_old, pages_new;
1289 : : struct hugepage_info *hpi;
1290 : :
1291 : : /*
1292 : : * we don't yet mark hugepages as used at this stage, so
1293 : : * we just map all hugepages available to the system
1294 : : * all hugepages are still located on socket 0
1295 : : */
1296 : 2 : hpi = &internal_conf->hugepage_info[i];
1297 : :
1298 [ - + ]: 2 : if (hpi->num_pages[0] == 0)
1299 : 0 : continue;
1300 : :
1301 : : /* map all hugepages available */
1302 : : pages_old = hpi->num_pages[0];
1303 : 2 : pages_new = map_all_hugepages(&tmp_hp[hp_offset], hpi, memory);
1304 [ - + ]: 2 : if (pages_new < pages_old) {
1305 : 0 : EAL_LOG(DEBUG,
1306 : : "%d not %d hugepages of size %u MB allocated",
1307 : : pages_new, pages_old,
1308 : : (unsigned)(hpi->hugepage_sz / 0x100000));
1309 : :
1310 : 0 : int pages = pages_old - pages_new;
1311 : :
1312 : 0 : nr_hugepages -= pages;
1313 : 0 : hpi->num_pages[0] = pages_new;
1314 [ # # ]: 0 : if (pages_new == 0)
1315 : 0 : continue;
1316 : : }
1317 : :
1318 [ + - - + ]: 4 : if (rte_eal_using_phys_addrs() &&
1319 : 2 : rte_eal_iova_mode() != RTE_IOVA_VA) {
1320 : : /* find physical addresses for each hugepage */
1321 [ - + ]: 2 : if (find_physaddrs(&tmp_hp[hp_offset], hpi) < 0) {
1322 : 0 : EAL_LOG(DEBUG, "Failed to find phys addr "
1323 : : "for %u MB pages",
1324 : : (unsigned int)(hpi->hugepage_sz / 0x100000));
1325 : 0 : goto fail;
1326 : : }
1327 : : } else {
1328 : : /* set physical addresses for each hugepage */
1329 : : if (set_physaddrs(&tmp_hp[hp_offset], hpi) < 0) {
1330 : : EAL_LOG(DEBUG, "Failed to set phys addr "
1331 : : "for %u MB pages",
1332 : : (unsigned int)(hpi->hugepage_sz / 0x100000));
1333 : : goto fail;
1334 : : }
1335 : : }
1336 : :
1337 [ - + ]: 2 : if (find_numasocket(&tmp_hp[hp_offset], hpi) < 0){
1338 : 0 : EAL_LOG(DEBUG, "Failed to find NUMA socket for %u MB pages",
1339 : : (unsigned)(hpi->hugepage_sz / 0x100000));
1340 : 0 : goto fail;
1341 : : }
1342 : :
1343 : 2 : qsort(&tmp_hp[hp_offset], hpi->num_pages[0],
1344 : : sizeof(struct hugepage_file), cmp_physaddr);
1345 : :
1346 : : /* we have processed a num of hugepages of this size, so inc offset */
1347 : 2 : hp_offset += hpi->num_pages[0];
1348 : : }
1349 : :
1350 : : huge_recover_sigbus();
1351 : :
1352 [ - + - - ]: 2 : if (internal_conf->memory == 0 && internal_conf->force_numa == 0)
1353 : 0 : internal_conf->memory = eal_get_hugepage_mem_size();
1354 : :
1355 : : nr_hugefiles = nr_hugepages;
1356 : :
1357 : :
1358 : : /* clean out the numbers of pages */
1359 [ + + ]: 4 : for (i = 0; i < (int) internal_conf->num_hugepage_sizes; i++)
1360 [ + + ]: 66 : for (j = 0; j < RTE_MAX_NUMA_NODES; j++)
1361 : 64 : internal_conf->hugepage_info[i].num_pages[j] = 0;
1362 : :
1363 : : /* get hugepages for each socket */
1364 [ + + ]: 2048 : for (i = 0; i < nr_hugefiles; i++) {
1365 : 2046 : int socket = tmp_hp[i].socket_id;
1366 : :
1367 : : /* find a hugepage info with right size and increment num_pages */
1368 : 2046 : const int nb_hpsizes = RTE_MIN(MAX_HUGEPAGE_SIZES,
1369 : : (int)internal_conf->num_hugepage_sizes);
1370 [ + + ]: 4092 : for (j = 0; j < nb_hpsizes; j++) {
1371 : 2046 : if (tmp_hp[i].size ==
1372 [ + - ]: 2046 : internal_conf->hugepage_info[j].hugepage_sz) {
1373 : 2046 : internal_conf->hugepage_info[j].num_pages[socket]++;
1374 : : }
1375 : : }
1376 : : }
1377 : :
1378 : : /* make a copy of numa_mem, needed for number of pages calculation */
1379 [ + + ]: 66 : for (i = 0; i < RTE_MAX_NUMA_NODES; i++)
1380 : 64 : memory[i] = internal_conf->numa_mem[i];
1381 : :
1382 : : /* calculate final number of pages */
1383 : 2 : nr_hugepages = eal_dynmem_calc_num_pages_per_socket(memory,
1384 : 2 : internal_conf->hugepage_info, used_hp,
1385 : : internal_conf->num_hugepage_sizes);
1386 : :
1387 : : /* error if not enough memory available */
1388 [ - + ]: 2 : if (nr_hugepages < 0)
1389 : 0 : goto fail;
1390 : :
1391 : : /* reporting in! */
1392 [ + + ]: 4 : for (i = 0; i < (int) internal_conf->num_hugepage_sizes; i++) {
1393 [ + + ]: 66 : for (j = 0; j < RTE_MAX_NUMA_NODES; j++) {
1394 [ + + ]: 64 : if (used_hp[i].num_pages[j] > 0) {
1395 : 2 : EAL_LOG(DEBUG,
1396 : : "Requesting %u pages of size %uMB"
1397 : : " from socket %i",
1398 : : used_hp[i].num_pages[j],
1399 : : (unsigned)
1400 : : (used_hp[i].hugepage_sz / 0x100000),
1401 : : j);
1402 : : }
1403 : : }
1404 : : }
1405 : :
1406 : : /* create shared memory */
1407 : 2 : hugepage = create_shared_memory(eal_hugepage_data_path(),
1408 : : nr_hugefiles * sizeof(struct hugepage_file));
1409 : :
1410 [ - + ]: 2 : if (hugepage == NULL) {
1411 : 0 : EAL_LOG(ERR, "Failed to create shared memory!");
1412 : 0 : goto fail;
1413 : : }
1414 : : memset(hugepage, 0, nr_hugefiles * sizeof(struct hugepage_file));
1415 : :
1416 : : /*
1417 : : * unmap pages that we won't need (looks at used_hp).
1418 : : * also, sets final_va to NULL on pages that were unmapped.
1419 : : */
1420 [ - + ]: 2 : if (unmap_unneeded_hugepages(tmp_hp, used_hp,
1421 : : internal_conf->num_hugepage_sizes) < 0) {
1422 : 0 : EAL_LOG(ERR, "Unmapping and locking hugepages failed!");
1423 : 0 : goto fail;
1424 : : }
1425 : :
1426 : : /*
1427 : : * copy stuff from malloc'd hugepage* to the actual shared memory.
1428 : : * this procedure only copies those hugepages that have orig_va
1429 : : * not NULL. has overflow protection.
1430 : : */
1431 [ - + ]: 2 : if (copy_hugepages_to_shared_mem(hugepage, nr_hugefiles,
1432 : : tmp_hp, nr_hugefiles) < 0) {
1433 : 0 : EAL_LOG(ERR, "Copying tables to shared memory failed!");
1434 : 0 : goto fail;
1435 : : }
1436 : :
1437 : : #ifndef RTE_ARCH_64
1438 : : /* for legacy 32-bit mode, we did not preallocate VA space, so do it */
1439 : : if (internal_conf->legacy_mem &&
1440 : : prealloc_segments(hugepage, nr_hugefiles)) {
1441 : : EAL_LOG(ERR, "Could not preallocate VA space for hugepages");
1442 : : goto fail;
1443 : : }
1444 : : #endif
1445 : :
1446 : : /* remap all pages we do need into memseg list VA space, so that those
1447 : : * pages become first-class citizens in DPDK memory subsystem
1448 : : */
1449 [ - + ]: 2 : if (remap_needed_hugepages(hugepage, nr_hugefiles)) {
1450 : 0 : EAL_LOG(ERR, "Couldn't remap hugepage files into memseg lists");
1451 : 0 : goto fail;
1452 : : }
1453 : :
1454 : : /* free the hugepage backing files */
1455 [ - + - - ]: 2 : if (internal_conf->hugepage_file.unlink_before_mapping &&
1456 : 0 : unlink_hugepage_files(tmp_hp, internal_conf->num_hugepage_sizes) < 0) {
1457 : 0 : EAL_LOG(ERR, "Unlinking hugepage files failed!");
1458 : 0 : goto fail;
1459 : : }
1460 : :
1461 : : /* free the temporary hugepage table */
1462 : 2 : free(tmp_hp);
1463 : : tmp_hp = NULL;
1464 : :
1465 : 2 : munmap(hugepage, nr_hugefiles * sizeof(struct hugepage_file));
1466 : : hugepage = NULL;
1467 : :
1468 : : /* we're not going to allocate more pages, so release VA space for
1469 : : * unused memseg lists
1470 : : */
1471 [ + + ]: 258 : for (i = 0; i < RTE_MAX_MEMSEG_LISTS; i++) {
1472 : : struct rte_memseg_list *msl = &mcfg->memsegs[i];
1473 : : size_t mem_sz;
1474 : :
1475 : : /* skip inactive lists */
1476 [ + + ]: 256 : if (msl->base_va == NULL)
1477 : 252 : continue;
1478 : : /* skip lists where there is at least one page allocated */
1479 [ + + ]: 4 : if (msl->memseg_arr.count > 0)
1480 : 2 : continue;
1481 : : /* this is an unused list, deallocate it */
1482 : 2 : mem_sz = msl->len;
1483 : 2 : munmap(msl->base_va, mem_sz);
1484 : 2 : msl->base_va = NULL;
1485 : 2 : msl->len = 0;
1486 : 2 : msl->heap = 0;
1487 : :
1488 : : /* destroy backing fbarray */
1489 : 2 : rte_fbarray_destroy(&msl->memseg_arr);
1490 : : }
1491 : :
1492 [ - + - - ]: 2 : if (mcfg->dma_maskbits &&
1493 : 0 : rte_mem_check_dma_mask_thread_unsafe(mcfg->dma_maskbits)) {
1494 : 0 : EAL_LOG(ERR,
1495 : : "%s(): couldn't allocate memory due to IOVA exceeding limits of current DMA mask.",
1496 : : __func__);
1497 : 0 : goto fail;
1498 : : }
1499 : :
1500 : : return 0;
1501 : :
1502 [ # # ]: 0 : fail:
1503 : : huge_recover_sigbus();
1504 : 0 : free(tmp_hp);
1505 [ # # ]: 0 : if (hugepage != NULL)
1506 : 0 : munmap(hugepage, nr_hugefiles * sizeof(struct hugepage_file));
1507 : :
1508 : : return -1;
1509 : : }
1510 : :
1511 : : /*
1512 : : * uses fstat to report the size of a file on disk
1513 : : */
1514 : : static off_t
1515 : : getFileSize(int fd)
1516 : : {
1517 : : struct stat st;
1518 [ # # ]: 0 : if (fstat(fd, &st) < 0)
1519 : : return 0;
1520 : 0 : return st.st_size;
1521 : : }
1522 : :
1523 : : /*
1524 : : * This creates the memory mappings in the secondary process to match that of
1525 : : * the server process. It goes through each memory segment in the DPDK runtime
1526 : : * configuration and finds the hugepages which form that segment, mapping them
1527 : : * in order to form a contiguous block in the virtual memory space
1528 : : */
1529 : : static int
1530 : 0 : eal_legacy_hugepage_attach(void)
1531 : : {
1532 : 0 : struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
1533 : : struct hugepage_file *hp = NULL;
1534 : : unsigned int num_hp = 0;
1535 : : unsigned int i = 0;
1536 : : unsigned int cur_seg;
1537 : : off_t size = 0;
1538 : : int fd, fd_hugepage = -1;
1539 : :
1540 [ # # ]: 0 : if (aslr_enabled() > 0) {
1541 : 0 : EAL_LOG(WARNING, "WARNING: Address Space Layout Randomization "
1542 : : "(ASLR) is enabled in the kernel.");
1543 : 0 : EAL_LOG(WARNING, " This may cause issues with mapping memory "
1544 : : "into secondary processes");
1545 : : }
1546 : :
1547 : 0 : fd_hugepage = open(eal_hugepage_data_path(), O_RDONLY);
1548 [ # # ]: 0 : if (fd_hugepage < 0) {
1549 : 0 : EAL_LOG(ERR, "Could not open %s",
1550 : : eal_hugepage_data_path());
1551 : 0 : goto error;
1552 : : }
1553 : :
1554 : : size = getFileSize(fd_hugepage);
1555 : 0 : hp = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd_hugepage, 0);
1556 [ # # ]: 0 : if (hp == MAP_FAILED) {
1557 : 0 : EAL_LOG(ERR, "Could not mmap %s",
1558 : : eal_hugepage_data_path());
1559 : 0 : goto error;
1560 : : }
1561 : :
1562 : 0 : num_hp = size / sizeof(struct hugepage_file);
1563 : 0 : EAL_LOG(DEBUG, "Analysing %u files", num_hp);
1564 : :
1565 : : /* map all segments into memory to make sure we get the addrs. the
1566 : : * segments themselves are already in memseg list (which is shared and
1567 : : * has its VA space already preallocated), so we just need to map
1568 : : * everything into correct addresses.
1569 : : */
1570 [ # # ]: 0 : for (i = 0; i < num_hp; i++) {
1571 : 0 : struct hugepage_file *hf = &hp[i];
1572 : 0 : size_t map_sz = hf->size;
1573 : 0 : void *map_addr = hf->final_va;
1574 : : int msl_idx, ms_idx;
1575 : : struct rte_memseg_list *msl;
1576 : : struct rte_memseg *ms;
1577 : :
1578 : : /* if size is zero, no more pages left */
1579 [ # # ]: 0 : if (map_sz == 0)
1580 : : break;
1581 : :
1582 : 0 : fd = open(hf->filepath, O_RDWR);
1583 [ # # ]: 0 : if (fd < 0) {
1584 : 0 : EAL_LOG(ERR, "Could not open %s: %s",
1585 : : hf->filepath, strerror(errno));
1586 : 0 : goto error;
1587 : : }
1588 : :
1589 : 0 : map_addr = mmap(map_addr, map_sz, PROT_READ | PROT_WRITE,
1590 : : MAP_SHARED | MAP_FIXED, fd, 0);
1591 [ # # ]: 0 : if (map_addr == MAP_FAILED) {
1592 : 0 : EAL_LOG(ERR, "Could not map %s: %s",
1593 : : hf->filepath, strerror(errno));
1594 : 0 : goto fd_error;
1595 : : }
1596 : :
1597 : : /* set shared lock on the file. */
1598 [ # # ]: 0 : if (flock(fd, LOCK_SH) < 0) {
1599 : 0 : EAL_LOG(DEBUG, "%s(): Locking file failed: %s",
1600 : : __func__, strerror(errno));
1601 : 0 : goto mmap_error;
1602 : : }
1603 : :
1604 : : /* find segment data */
1605 : 0 : msl = rte_mem_virt2memseg_list(map_addr);
1606 [ # # ]: 0 : if (msl == NULL) {
1607 : 0 : EAL_LOG(DEBUG, "%s(): Cannot find memseg list",
1608 : : __func__);
1609 : 0 : goto mmap_error;
1610 : : }
1611 : 0 : ms = rte_mem_virt2memseg(map_addr, msl);
1612 [ # # ]: 0 : if (ms == NULL) {
1613 : 0 : EAL_LOG(DEBUG, "%s(): Cannot find memseg",
1614 : : __func__);
1615 : 0 : goto mmap_error;
1616 : : }
1617 : :
1618 : 0 : msl_idx = msl - mcfg->memsegs;
1619 : 0 : ms_idx = rte_fbarray_find_idx(&msl->memseg_arr, ms);
1620 [ # # ]: 0 : if (ms_idx < 0) {
1621 : 0 : EAL_LOG(DEBUG, "%s(): Cannot find memseg idx",
1622 : : __func__);
1623 : 0 : goto mmap_error;
1624 : : }
1625 : :
1626 : : /* store segment fd internally */
1627 [ # # ]: 0 : if (eal_memalloc_set_seg_fd(msl_idx, ms_idx, fd) < 0)
1628 : 0 : EAL_LOG(ERR, "Could not store segment fd: %s",
1629 : : rte_strerror(rte_errno));
1630 : : }
1631 : : /* unmap the hugepage config file, since we are done using it */
1632 : 0 : munmap(hp, size);
1633 : 0 : close(fd_hugepage);
1634 : 0 : return 0;
1635 : :
1636 : 0 : mmap_error:
1637 : 0 : munmap(hp[i].final_va, hp[i].size);
1638 : 0 : fd_error:
1639 : 0 : close(fd);
1640 : 0 : error:
1641 : : /* unwind mmap's done so far */
1642 [ # # ]: 0 : for (cur_seg = 0; cur_seg < i; cur_seg++)
1643 : 0 : munmap(hp[cur_seg].final_va, hp[cur_seg].size);
1644 : :
1645 [ # # ]: 0 : if (hp != NULL && hp != MAP_FAILED)
1646 : 0 : munmap(hp, size);
1647 [ # # ]: 0 : if (fd_hugepage >= 0)
1648 : 0 : close(fd_hugepage);
1649 : : return -1;
1650 : : }
1651 : :
1652 : : static int
1653 : 31 : eal_hugepage_attach(void)
1654 : : {
1655 [ + + ]: 31 : if (eal_memalloc_sync_with_primary()) {
1656 : 1 : EAL_LOG(ERR, "Could not map memory from primary process");
1657 [ + - ]: 1 : if (aslr_enabled() > 0)
1658 : 1 : EAL_LOG(ERR, "It is recommended to disable ASLR in the kernel and retry running both primary and secondary processes");
1659 : 1 : return -1;
1660 : : }
1661 : : return 0;
1662 : : }
1663 : :
1664 : : int
1665 : 195 : rte_eal_hugepage_init(void)
1666 : : {
1667 : : const struct internal_config *internal_conf =
1668 : 195 : eal_get_internal_configuration();
1669 : :
1670 : 195 : return internal_conf->legacy_mem ?
1671 [ + + ]: 195 : eal_legacy_hugepage_init() :
1672 : 62 : eal_dynmem_hugepage_init();
1673 : : }
1674 : :
1675 : : int
1676 : 31 : rte_eal_hugepage_attach(void)
1677 : : {
1678 : : const struct internal_config *internal_conf =
1679 : 31 : eal_get_internal_configuration();
1680 : :
1681 : 31 : return internal_conf->legacy_mem ?
1682 [ - + ]: 31 : eal_legacy_hugepage_attach() :
1683 : 31 : eal_hugepage_attach();
1684 : : }
1685 : :
1686 : : RTE_EXPORT_SYMBOL(rte_eal_using_phys_addrs)
1687 : : int
1688 : 231 : rte_eal_using_phys_addrs(void)
1689 : : {
1690 [ + + ]: 231 : if (phys_addrs_available == -1) {
1691 : 229 : uint64_t tmp = 0;
1692 : :
1693 [ + + + - ]: 327 : if (rte_eal_has_hugepages() != 0 &&
1694 : 98 : rte_mem_virt2phy(&tmp) != RTE_BAD_PHYS_ADDR)
1695 : 98 : phys_addrs_available = 1;
1696 : : else
1697 : 131 : phys_addrs_available = 0;
1698 : : }
1699 : 231 : return phys_addrs_available;
1700 : : }
1701 : :
1702 : : static int __rte_unused
1703 : : memseg_primary_init_32(void)
1704 : : {
1705 : : /* limit total amount of memory on 32-bit */
1706 : : const uint64_t mem32_max_mem = 2ULL << 30;
1707 : : struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
1708 : : int active_sockets, hpi_idx, msl_idx = 0;
1709 : : unsigned int socket_id, i;
1710 : : struct rte_memseg_list *msl;
1711 : : uint64_t extra_mem_per_socket, total_extra_mem, total_requested_mem;
1712 : : struct internal_config *internal_conf =
1713 : : eal_get_internal_configuration();
1714 : :
1715 : : /* no-huge does not need this at all */
1716 : : if (internal_conf->no_hugetlbfs)
1717 : : return 0;
1718 : :
1719 : : /* this is a giant hack, but desperate times call for desperate
1720 : : * measures. in legacy 32-bit mode, we cannot preallocate VA space,
1721 : : * because having upwards of 2 gigabytes of VA space already mapped will
1722 : : * interfere with our ability to map and sort hugepages.
1723 : : *
1724 : : * therefore, in legacy 32-bit mode, we will be initializing memseg
1725 : : * lists much later - in eal_memory.c, right after we unmap all the
1726 : : * unneeded pages. this will not affect secondary processes, as those
1727 : : * should be able to mmap the space without (too many) problems.
1728 : : */
1729 : : if (internal_conf->legacy_mem)
1730 : : return 0;
1731 : :
1732 : : /* 32-bit mode is a very special case. we cannot know in advance where
1733 : : * the user will want to allocate their memory, so we have to do some
1734 : : * heuristics.
1735 : : */
1736 : : active_sockets = 0;
1737 : : total_requested_mem = 0;
1738 : : if (internal_conf->force_numa)
1739 : : for (i = 0; i < rte_socket_count(); i++) {
1740 : : uint64_t mem;
1741 : :
1742 : : socket_id = rte_socket_id_by_idx(i);
1743 : : mem = internal_conf->numa_mem[socket_id];
1744 : :
1745 : : if (mem == 0)
1746 : : continue;
1747 : :
1748 : : active_sockets++;
1749 : : total_requested_mem += mem;
1750 : : }
1751 : : else
1752 : : total_requested_mem = internal_conf->memory;
1753 : :
1754 : : if (total_requested_mem > mem32_max_mem) {
1755 : : EAL_LOG(ERR, "Invalid parameters: 32-bit process can at most use %uM of memory",
1756 : : (unsigned int)(mem32_max_mem >> 20));
1757 : : return -1;
1758 : : }
1759 : : total_extra_mem = mem32_max_mem - total_requested_mem;
1760 : : extra_mem_per_socket = active_sockets == 0 ? total_extra_mem :
1761 : : total_extra_mem / active_sockets;
1762 : :
1763 : : /* the allocation logic is a little bit convoluted, but here's how it
1764 : : * works, in a nutshell:
1765 : : * - if user hasn't specified on which sockets to allocate memory via
1766 : : * --socket-mem, we allocate all of our memory on main core socket.
1767 : : * - if user has specified sockets to allocate memory on, there may be
1768 : : * some "unused" memory left (e.g. if user has specified --socket-mem
1769 : : * such that not all memory adds up to 2 gigabytes), so add it to all
1770 : : * sockets that are in use equally.
1771 : : *
1772 : : * page sizes are sorted by size in descending order, so we can safely
1773 : : * assume that we dispense with bigger page sizes first.
1774 : : */
1775 : :
1776 : : /* create memseg lists */
1777 : : for (i = 0; i < rte_socket_count(); i++) {
1778 : : int hp_sizes = (int) internal_conf->num_hugepage_sizes;
1779 : : uint64_t max_socket_mem, cur_socket_mem;
1780 : : unsigned int main_lcore_socket;
1781 : : struct rte_config *cfg = rte_eal_get_configuration();
1782 : : bool skip;
1783 : : int ret;
1784 : :
1785 : : ret = rte_socket_id_by_idx(i);
1786 : : if (ret == -1) {
1787 : : EAL_LOG(ERR, "Cannot get socket ID for socket index %u", i);
1788 : : return -1;
1789 : : }
1790 : : socket_id = (unsigned int)ret;
1791 : :
1792 : : #ifndef RTE_EAL_NUMA_AWARE_HUGEPAGES
1793 : : /* we can still sort pages by socket in legacy mode */
1794 : : if (!internal_conf->legacy_mem && socket_id > 0)
1795 : : break;
1796 : : #endif
1797 : :
1798 : : /* if we didn't specifically request memory on this socket */
1799 : : skip = active_sockets != 0 &&
1800 : : internal_conf->numa_mem[socket_id] == 0;
1801 : : /* ...or if we didn't specifically request memory on *any*
1802 : : * socket, and this is not main lcore
1803 : : */
1804 : : main_lcore_socket = rte_lcore_to_socket_id(cfg->main_lcore);
1805 : : skip |= active_sockets == 0 && socket_id != main_lcore_socket;
1806 : :
1807 : : if (skip) {
1808 : : EAL_LOG(DEBUG, "Will not preallocate memory on socket %u",
1809 : : socket_id);
1810 : : continue;
1811 : : }
1812 : :
1813 : : /* max amount of memory on this socket */
1814 : : max_socket_mem = (active_sockets != 0 ?
1815 : : internal_conf->numa_mem[socket_id] :
1816 : : internal_conf->memory) +
1817 : : extra_mem_per_socket;
1818 : : cur_socket_mem = 0;
1819 : :
1820 : : for (hpi_idx = 0; hpi_idx < hp_sizes; hpi_idx++) {
1821 : : uint64_t max_pagesz_mem, cur_pagesz_mem = 0;
1822 : : uint64_t hugepage_sz;
1823 : : uint64_t pagesz_mem_limit;
1824 : : struct hugepage_info *hpi;
1825 : :
1826 : : hpi = &internal_conf->hugepage_info[hpi_idx];
1827 : : hugepage_sz = hpi->hugepage_sz;
1828 : :
1829 : : /* check if pages are actually available */
1830 : : if (hpi->num_pages[socket_id] == 0)
1831 : : continue;
1832 : :
1833 : : max_pagesz_mem = max_socket_mem - cur_socket_mem;
1834 : : pagesz_mem_limit = internal_conf->hugepage_mem_sz_limits[hpi_idx];
1835 : : max_pagesz_mem = RTE_MIN(max_pagesz_mem, pagesz_mem_limit);
1836 : :
1837 : : /* make it multiple of page size */
1838 : : max_pagesz_mem = RTE_ALIGN_FLOOR(max_pagesz_mem,
1839 : : hugepage_sz);
1840 : :
1841 : : if (max_pagesz_mem == 0)
1842 : : continue;
1843 : :
1844 : : EAL_LOG(DEBUG, "Attempting to preallocate "
1845 : : "%" PRIu64 "M on socket %i",
1846 : : max_pagesz_mem >> 20, socket_id);
1847 : :
1848 : : while (cur_pagesz_mem < max_pagesz_mem) {
1849 : : uint64_t rem_mem;
1850 : : unsigned int n_segs;
1851 : :
1852 : : rem_mem = max_pagesz_mem - cur_pagesz_mem;
1853 : : n_segs = rem_mem / hugepage_sz;
1854 : :
1855 : : while (n_segs > 0) {
1856 : : if (msl_idx >= RTE_MAX_MEMSEG_LISTS) {
1857 : : EAL_LOG(ERR,
1858 : : "No more space in memseg lists, please increase RTE_MAX_MEMSEG_LISTS");
1859 : : return -1;
1860 : : }
1861 : :
1862 : : msl = &mcfg->memsegs[msl_idx];
1863 : :
1864 : : if (eal_memseg_list_init(msl, hugepage_sz,
1865 : : n_segs, socket_id, msl_idx, true) < 0) {
1866 : : /* failing to allocate a memseg list is a serious error. */
1867 : : EAL_LOG(ERR, "Cannot allocate memseg list");
1868 : : return -1;
1869 : : }
1870 : :
1871 : : if (eal_memseg_list_alloc(msl, 0) == 0)
1872 : : break;
1873 : :
1874 : : if (memseg_list_free(msl))
1875 : : return -1;
1876 : :
1877 : : EAL_LOG(DEBUG,
1878 : : "Cannot allocate VA space for memseg list, retrying with smaller chunk");
1879 : : n_segs /= 2;
1880 : : }
1881 : :
1882 : : if (n_segs == 0)
1883 : : break;
1884 : :
1885 : : cur_pagesz_mem += (uint64_t)n_segs * hugepage_sz;
1886 : : cur_socket_mem += (uint64_t)n_segs * hugepage_sz;
1887 : : msl_idx++;
1888 : : }
1889 : : }
1890 : : if (cur_socket_mem == 0) {
1891 : : EAL_LOG(ERR, "Cannot allocate VA space on socket %u",
1892 : : socket_id);
1893 : : return -1;
1894 : : }
1895 : : }
1896 : :
1897 : : return 0;
1898 : : }
1899 : :
1900 : : static int __rte_unused
1901 : : memseg_primary_init(void)
1902 : : {
1903 : 195 : return eal_dynmem_memseg_lists_init();
1904 : : }
1905 : :
1906 : : static int __rte_unused
1907 : 31 : memseg_secondary_init_dynmem(void)
1908 : : {
1909 : 31 : struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
1910 : : int msl_idx = 0;
1911 : : struct rte_memseg_list *msl;
1912 : : void *mem_va_addr;
1913 : : size_t mem_va_len;
1914 : :
1915 [ + - + - ]: 31 : if (mcfg->mem_va_addr == 0 || mcfg->mem_va_len == 0 ||
1916 [ - + ]: 31 : mcfg->mem_va_page_sz == 0) {
1917 : 0 : EAL_LOG(ERR, "Missing shared dynamic memory VA range from primary process");
1918 : 0 : return -1;
1919 : : }
1920 : :
1921 : 31 : mem_va_addr = (void *)(uintptr_t)mcfg->mem_va_addr;
1922 : 31 : mem_va_len = mcfg->mem_va_len;
1923 : :
1924 [ - + ]: 31 : if (eal_get_virtual_area(mem_va_addr, &mem_va_len,
1925 : : mcfg->mem_va_page_sz, 0, 0) == NULL) {
1926 : 0 : EAL_LOG(ERR, "Cannot reserve VA space for hugepage memory");
1927 : 0 : return -1;
1928 : : }
1929 : :
1930 [ + + ]: 3999 : for (msl_idx = 0; msl_idx < RTE_MAX_MEMSEG_LISTS; msl_idx++) {
1931 : :
1932 : 3968 : msl = &mcfg->memsegs[msl_idx];
1933 : :
1934 : : /* skip empty and external memseg lists */
1935 [ + + - + ]: 3968 : if (msl->memseg_arr.len == 0 || msl->external)
1936 : 3906 : continue;
1937 : :
1938 [ - + ]: 62 : if (rte_fbarray_attach(&msl->memseg_arr)) {
1939 : 0 : EAL_LOG(ERR, "Cannot attach to primary process memseg lists");
1940 : 0 : eal_mem_free(mem_va_addr, mem_va_len);
1941 : 0 : return -1;
1942 : : }
1943 : :
1944 [ - + ]: 62 : if (eal_memseg_list_assign(msl, msl->base_va)) {
1945 : 0 : EAL_LOG(ERR, "Cannot assign VA space for hugepage memory");
1946 : 0 : eal_mem_free(mem_va_addr, mem_va_len);
1947 : 0 : return -1;
1948 : : }
1949 : :
1950 : 62 : EAL_LOG(DEBUG, "Attaching segment list: n_segs:%u socket_id:%d hugepage_sz:%" PRIu64,
1951 : : msl->memseg_arr.len, msl->socket_id, msl->page_sz);
1952 : : }
1953 : :
1954 : : return 0;
1955 : : }
1956 : :
1957 : : static int
1958 : 0 : memseg_secondary_init_legacy(void)
1959 : : {
1960 : 0 : struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
1961 : : int msl_idx = 0;
1962 : : struct rte_memseg_list *msl;
1963 : :
1964 [ # # ]: 0 : for (msl_idx = 0; msl_idx < RTE_MAX_MEMSEG_LISTS; msl_idx++) {
1965 : :
1966 : 0 : msl = &mcfg->memsegs[msl_idx];
1967 : :
1968 : : /* skip empty and external memseg lists */
1969 [ # # # # ]: 0 : if (msl->memseg_arr.len == 0 || msl->external)
1970 : 0 : continue;
1971 : :
1972 [ # # ]: 0 : if (rte_fbarray_attach(&msl->memseg_arr)) {
1973 : 0 : EAL_LOG(ERR, "Cannot attach to primary process memseg lists");
1974 : 0 : return -1;
1975 : : }
1976 : :
1977 : : /* preallocate VA space */
1978 [ # # ]: 0 : if (eal_memseg_list_alloc(msl, 0)) {
1979 : 0 : EAL_LOG(ERR, "Cannot preallocate VA space for hugepage memory");
1980 : 0 : return -1;
1981 : : }
1982 : : }
1983 : :
1984 : : return 0;
1985 : : }
1986 : :
1987 : : static int
1988 : 31 : memseg_secondary_init(void)
1989 : : {
1990 : : #ifdef RTE_ARCH_64
1991 : : const struct internal_config *internal_conf =
1992 : 31 : eal_get_internal_configuration();
1993 : :
1994 : : /* for 32-bit dynmem init is same as legacy */
1995 [ + - ]: 31 : if (!internal_conf->legacy_mem)
1996 : 31 : return memseg_secondary_init_dynmem();
1997 : : #endif
1998 : :
1999 : 0 : return memseg_secondary_init_legacy();
2000 : : }
2001 : :
2002 : : int
2003 : 226 : rte_eal_memseg_init(void)
2004 : : {
2005 : : /* increase rlimit to maximum */
2006 : : struct rlimit lim;
2007 : :
2008 : : #ifndef RTE_EAL_NUMA_AWARE_HUGEPAGES
2009 : : const struct internal_config *internal_conf =
2010 : : eal_get_internal_configuration();
2011 : : #endif
2012 [ + - ]: 226 : if (getrlimit(RLIMIT_NOFILE, &lim) == 0) {
2013 : : /* set limit to maximum */
2014 : 226 : lim.rlim_cur = lim.rlim_max;
2015 : :
2016 [ - + ]: 226 : if (setrlimit(RLIMIT_NOFILE, &lim) < 0) {
2017 : 0 : EAL_LOG(DEBUG, "Setting maximum number of open files failed: %s",
2018 : : strerror(errno));
2019 : : } else {
2020 : 226 : EAL_LOG(DEBUG, "Setting maximum number of open files to %"
2021 : : PRIu64,
2022 : : (uint64_t)lim.rlim_cur);
2023 : : }
2024 : : } else {
2025 : 0 : EAL_LOG(ERR, "Cannot get current resource limits");
2026 : : }
2027 : : #ifndef RTE_EAL_NUMA_AWARE_HUGEPAGES
2028 : : if (!internal_conf->legacy_mem && rte_socket_count() > 1) {
2029 : : EAL_LOG(WARNING, "DPDK is running on a NUMA system, but is compiled without NUMA support.");
2030 : : EAL_LOG(WARNING, "This will have adverse consequences for performance and usability.");
2031 : : EAL_LOG(WARNING, "Please use --legacy-mem option, or recompile with NUMA support.");
2032 : : }
2033 : : #endif
2034 : :
2035 : 226 : return rte_eal_process_type() == RTE_PROC_PRIMARY ?
2036 : : #ifndef RTE_ARCH_64
2037 : : memseg_primary_init_32() :
2038 : : #else
2039 [ + + ]: 226 : memseg_primary_init() :
2040 : : #endif
2041 : 31 : memseg_secondary_init();
2042 : : }
|