1
2
3
4
5
6#include <errno.h>
7#include <fcntl.h>
8#include <stdarg.h>
9#include <stdbool.h>
10#include <stdlib.h>
11#include <stdio.h>
12#include <stdint.h>
13#include <inttypes.h>
14#include <string.h>
15#include <sys/mman.h>
16#include <sys/types.h>
17#include <sys/stat.h>
18#include <sys/queue.h>
19#include <sys/file.h>
20#include <sys/resource.h>
21#include <unistd.h>
22#include <limits.h>
23#include <sys/ioctl.h>
24#include <sys/time.h>
25#include <signal.h>
26#include <setjmp.h>
27#ifdef F_ADD_SEALS
28#include <linux/memfd.h>
29#define MEMFD_SUPPORTED
30#endif
31#ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
32#include <numa.h>
33#include <numaif.h>
34#endif
35
36#include <rte_errno.h>
37#include <rte_log.h>
38#include <rte_memory.h>
39#include <rte_launch.h>
40#include <rte_eal.h>
41#include <rte_per_lcore.h>
42#include <rte_lcore.h>
43#include <rte_common.h>
44#include <rte_string_fns.h>
45
46#include "eal_private.h"
47#include "eal_memalloc.h"
48#include "eal_memcfg.h"
49#include "eal_internal_cfg.h"
50#include "eal_filesystem.h"
51#include "eal_hugepages.h"
52#include "eal_options.h"
53
54#define PFN_MASK_SIZE 8
55
56
57
58
59
60
61
62
63
64
65
66
67
68static int phys_addrs_available = -1;
69
70#define RANDOMIZE_VA_SPACE_FILE "/proc/sys/kernel/randomize_va_space"
71
72uint64_t eal_get_baseaddr(void)
73{
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89 return 0x100000000ULL;
90}
91
92
93
94
95phys_addr_t
96rte_mem_virt2phy(const void *virtaddr)
97{
98 int fd, retval;
99 uint64_t page, physaddr;
100 unsigned long virt_pfn;
101 int page_size;
102 off_t offset;
103
104 if (phys_addrs_available == 0)
105 return RTE_BAD_IOVA;
106
107
108 page_size = getpagesize();
109
110 fd = open("/proc/self/pagemap", O_RDONLY);
111 if (fd < 0) {
112 RTE_LOG(INFO, EAL, "%s(): cannot open /proc/self/pagemap: %s\n",
113 __func__, strerror(errno));
114 return RTE_BAD_IOVA;
115 }
116
117 virt_pfn = (unsigned long)virtaddr / page_size;
118 offset = sizeof(uint64_t) * virt_pfn;
119 if (lseek(fd, offset, SEEK_SET) == (off_t) -1) {
120 RTE_LOG(INFO, EAL, "%s(): seek error in /proc/self/pagemap: %s\n",
121 __func__, strerror(errno));
122 close(fd);
123 return RTE_BAD_IOVA;
124 }
125
126 retval = read(fd, &page, PFN_MASK_SIZE);
127 close(fd);
128 if (retval < 0) {
129 RTE_LOG(INFO, EAL, "%s(): cannot read /proc/self/pagemap: %s\n",
130 __func__, strerror(errno));
131 return RTE_BAD_IOVA;
132 } else if (retval != PFN_MASK_SIZE) {
133 RTE_LOG(INFO, EAL, "%s(): read %d bytes from /proc/self/pagemap "
134 "but expected %d:\n",
135 __func__, retval, PFN_MASK_SIZE);
136 return RTE_BAD_IOVA;
137 }
138
139
140
141
142
143 if ((page & 0x7fffffffffffffULL) == 0)
144 return RTE_BAD_IOVA;
145
146 physaddr = ((page & 0x7fffffffffffffULL) * page_size)
147 + ((unsigned long)virtaddr % page_size);
148
149 return physaddr;
150}
151
152rte_iova_t
153rte_mem_virt2iova(const void *virtaddr)
154{
155 if (rte_eal_iova_mode() == RTE_IOVA_VA)
156 return (uintptr_t)virtaddr;
157 return rte_mem_virt2phy(virtaddr);
158}
159
160
161
162
163
164static int
165find_physaddrs(struct hugepage_file *hugepg_tbl, struct hugepage_info *hpi)
166{
167 unsigned int i;
168 phys_addr_t addr;
169
170 for (i = 0; i < hpi->num_pages[0]; i++) {
171 addr = rte_mem_virt2phy(hugepg_tbl[i].orig_va);
172 if (addr == RTE_BAD_PHYS_ADDR)
173 return -1;
174 hugepg_tbl[i].physaddr = addr;
175 }
176 return 0;
177}
178
179
180
181
182static int
183set_physaddrs(struct hugepage_file *hugepg_tbl, struct hugepage_info *hpi)
184{
185 unsigned int i;
186 static phys_addr_t addr;
187
188 for (i = 0; i < hpi->num_pages[0]; i++) {
189 hugepg_tbl[i].physaddr = addr;
190 addr += hugepg_tbl[i].size;
191 }
192 return 0;
193}
194
195
196
197
198
199
200
201
202
203
204static int
205aslr_enabled(void)
206{
207 char c;
208 int retval, fd = open(RANDOMIZE_VA_SPACE_FILE, O_RDONLY);
209 if (fd < 0)
210 return -errno;
211 retval = read(fd, &c, 1);
212 close(fd);
213 if (retval < 0)
214 return -errno;
215 if (retval == 0)
216 return -EIO;
217 switch (c) {
218 case '0' : return 0;
219 case '1' : return 1;
220 case '2' : return 2;
221 default: return -EINVAL;
222 }
223}
224
225static sigjmp_buf huge_jmpenv;
226
227static void huge_sigbus_handler(int signo __rte_unused)
228{
229 siglongjmp(huge_jmpenv, 1);
230}
231
232
233
234
235
236static int huge_wrap_sigsetjmp(void)
237{
238 return sigsetjmp(huge_jmpenv, 1);
239}
240
241#ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
242
243void numa_error(char *where)
244{
245 RTE_LOG(ERR, EAL, "%s failed: %s\n", where, strerror(errno));
246}
247#endif
248
249
250
251
252
253
254
255
256static unsigned
257map_all_hugepages(struct hugepage_file *hugepg_tbl, struct hugepage_info *hpi,
258 uint64_t *essential_memory __rte_unused)
259{
260 int fd;
261 unsigned i;
262 void *virtaddr;
263#ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
264 int node_id = -1;
265 int essential_prev = 0;
266 int oldpolicy;
267 struct bitmask *oldmask = NULL;
268 bool have_numa = true;
269 unsigned long maxnode = 0;
270 const struct internal_config *internal_conf =
271 eal_get_internal_configuration();
272
273
274 if (numa_available() != 0) {
275 RTE_LOG(DEBUG, EAL, "NUMA is not supported.\n");
276 have_numa = false;
277 }
278
279 if (have_numa) {
280 RTE_LOG(DEBUG, EAL, "Trying to obtain current memory policy.\n");
281 oldmask = numa_allocate_nodemask();
282 if (get_mempolicy(&oldpolicy, oldmask->maskp,
283 oldmask->size + 1, 0, 0) < 0) {
284 RTE_LOG(ERR, EAL,
285 "Failed to get current mempolicy: %s. "
286 "Assuming MPOL_DEFAULT.\n", strerror(errno));
287 oldpolicy = MPOL_DEFAULT;
288 }
289 for (i = 0; i < RTE_MAX_NUMA_NODES; i++)
290 if (internal_conf->socket_mem[i])
291 maxnode = i + 1;
292 }
293#endif
294
295 for (i = 0; i < hpi->num_pages[0]; i++) {
296 struct hugepage_file *hf = &hugepg_tbl[i];
297 uint64_t hugepage_sz = hpi->hugepage_sz;
298
299#ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
300 if (maxnode) {
301 unsigned int j;
302
303 for (j = 0; j < maxnode; j++)
304 if (essential_memory[j])
305 break;
306
307 if (j == maxnode) {
308 node_id = (node_id + 1) % maxnode;
309 while (!internal_conf->socket_mem[node_id]) {
310 node_id++;
311 node_id %= maxnode;
312 }
313 essential_prev = 0;
314 } else {
315 node_id = j;
316 essential_prev = essential_memory[j];
317
318 if (essential_memory[j] < hugepage_sz)
319 essential_memory[j] = 0;
320 else
321 essential_memory[j] -= hugepage_sz;
322 }
323
324 RTE_LOG(DEBUG, EAL,
325 "Setting policy MPOL_PREFERRED for socket %d\n",
326 node_id);
327 numa_set_preferred(node_id);
328 }
329#endif
330
331 hf->file_id = i;
332 hf->size = hugepage_sz;
333 eal_get_hugefile_path(hf->filepath, sizeof(hf->filepath),
334 hpi->hugedir, hf->file_id);
335 hf->filepath[sizeof(hf->filepath) - 1] = '\0';
336
337
338 fd = open(hf->filepath, O_CREAT | O_RDWR, 0600);
339 if (fd < 0) {
340 RTE_LOG(DEBUG, EAL, "%s(): open failed: %s\n", __func__,
341 strerror(errno));
342 goto out;
343 }
344
345
346
347
348
349
350 virtaddr = mmap(NULL, hugepage_sz, PROT_READ | PROT_WRITE,
351 MAP_SHARED | MAP_POPULATE, fd, 0);
352 if (virtaddr == MAP_FAILED) {
353 RTE_LOG(DEBUG, EAL, "%s(): mmap failed: %s\n", __func__,
354 strerror(errno));
355 close(fd);
356 goto out;
357 }
358
359 hf->orig_va = virtaddr;
360
361
362
363
364
365
366
367
368 if (huge_wrap_sigsetjmp()) {
369 RTE_LOG(DEBUG, EAL, "SIGBUS: Cannot mmap more "
370 "hugepages of size %u MB\n",
371 (unsigned int)(hugepage_sz / 0x100000));
372 munmap(virtaddr, hugepage_sz);
373 close(fd);
374 unlink(hugepg_tbl[i].filepath);
375#ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
376 if (maxnode)
377 essential_memory[node_id] =
378 essential_prev;
379#endif
380 goto out;
381 }
382 *(int *)virtaddr = 0;
383
384
385 if (flock(fd, LOCK_SH) < 0) {
386 RTE_LOG(DEBUG, EAL, "%s(): Locking file failed:%s \n",
387 __func__, strerror(errno));
388 close(fd);
389 goto out;
390 }
391
392 close(fd);
393 }
394
395out:
396#ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
397 if (maxnode) {
398 RTE_LOG(DEBUG, EAL,
399 "Restoring previous memory policy: %d\n", oldpolicy);
400 if (oldpolicy == MPOL_DEFAULT) {
401 numa_set_localalloc();
402 } else if (set_mempolicy(oldpolicy, oldmask->maskp,
403 oldmask->size + 1) < 0) {
404 RTE_LOG(ERR, EAL, "Failed to restore mempolicy: %s\n",
405 strerror(errno));
406 numa_set_localalloc();
407 }
408 }
409 if (oldmask != NULL)
410 numa_free_cpumask(oldmask);
411#endif
412 return i;
413}
414
415
416
417
418
419static int
420find_numasocket(struct hugepage_file *hugepg_tbl, struct hugepage_info *hpi)
421{
422 int socket_id;
423 char *end, *nodestr;
424 unsigned i, hp_count = 0;
425 uint64_t virt_addr;
426 char buf[BUFSIZ];
427 char hugedir_str[PATH_MAX];
428 FILE *f;
429
430 f = fopen("/proc/self/numa_maps", "r");
431 if (f == NULL) {
432 RTE_LOG(NOTICE, EAL, "NUMA support not available"
433 " consider that all memory is in socket_id 0\n");
434 return 0;
435 }
436
437 snprintf(hugedir_str, sizeof(hugedir_str),
438 "%s/%s", hpi->hugedir, eal_get_hugefile_prefix());
439
440
441 while (fgets(buf, sizeof(buf), f) != NULL) {
442
443
444 if (strstr(buf, " huge ") == NULL &&
445 strstr(buf, hugedir_str) == NULL)
446 continue;
447
448
449 virt_addr = strtoull(buf, &end, 16);
450 if (virt_addr == 0 || end == buf) {
451 RTE_LOG(ERR, EAL, "%s(): error in numa_maps parsing\n", __func__);
452 goto error;
453 }
454
455
456 nodestr = strstr(buf, " N");
457 if (nodestr == NULL) {
458 RTE_LOG(ERR, EAL, "%s(): error in numa_maps parsing\n", __func__);
459 goto error;
460 }
461 nodestr += 2;
462 end = strstr(nodestr, "=");
463 if (end == NULL) {
464 RTE_LOG(ERR, EAL, "%s(): error in numa_maps parsing\n", __func__);
465 goto error;
466 }
467 end[0] = '\0';
468 end = NULL;
469
470 socket_id = strtoul(nodestr, &end, 0);
471 if ((nodestr[0] == '\0') || (end == NULL) || (*end != '\0')) {
472 RTE_LOG(ERR, EAL, "%s(): error in numa_maps parsing\n", __func__);
473 goto error;
474 }
475
476
477 for (i = 0; i < hpi->num_pages[0]; i++) {
478 void *va = (void *)(unsigned long)virt_addr;
479 if (hugepg_tbl[i].orig_va == va) {
480 hugepg_tbl[i].socket_id = socket_id;
481 hp_count++;
482#ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
483 RTE_LOG(DEBUG, EAL,
484 "Hugepage %s is on socket %d\n",
485 hugepg_tbl[i].filepath, socket_id);
486#endif
487 }
488 }
489 }
490
491 if (hp_count < hpi->num_pages[0])
492 goto error;
493
494 fclose(f);
495 return 0;
496
497error:
498 fclose(f);
499 return -1;
500}
501
502static int
503cmp_physaddr(const void *a, const void *b)
504{
505#ifndef RTE_ARCH_PPC_64
506 const struct hugepage_file *p1 = a;
507 const struct hugepage_file *p2 = b;
508#else
509
510 const struct hugepage_file *p1 = b;
511 const struct hugepage_file *p2 = a;
512#endif
513 if (p1->physaddr < p2->physaddr)
514 return -1;
515 else if (p1->physaddr > p2->physaddr)
516 return 1;
517 else
518 return 0;
519}
520
521
522
523
524
525static void *
526create_shared_memory(const char *filename, const size_t mem_size)
527{
528 void *retval;
529 int fd;
530 const struct internal_config *internal_conf =
531 eal_get_internal_configuration();
532
533
534 if (internal_conf->no_shconf) {
535 retval = mmap(NULL, mem_size, PROT_READ | PROT_WRITE,
536 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
537 if (retval == MAP_FAILED)
538 return NULL;
539 return retval;
540 }
541
542 fd = open(filename, O_CREAT | O_RDWR, 0600);
543 if (fd < 0)
544 return NULL;
545 if (ftruncate(fd, mem_size) < 0) {
546 close(fd);
547 return NULL;
548 }
549 retval = mmap(NULL, mem_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
550 close(fd);
551 if (retval == MAP_FAILED)
552 return NULL;
553 return retval;
554}
555
556
557
558
559
560static int
561copy_hugepages_to_shared_mem(struct hugepage_file * dst, int dest_size,
562 const struct hugepage_file * src, int src_size)
563{
564 int src_pos, dst_pos = 0;
565
566 for (src_pos = 0; src_pos < src_size; src_pos++) {
567 if (src[src_pos].orig_va != NULL) {
568
569 if (dst_pos == dest_size)
570 return -1;
571 memcpy(&dst[dst_pos], &src[src_pos], sizeof(struct hugepage_file));
572 dst_pos++;
573 }
574 }
575 return 0;
576}
577
578static int
579unlink_hugepage_files(struct hugepage_file *hugepg_tbl,
580 unsigned num_hp_info)
581{
582 unsigned socket, size;
583 int page, nrpages = 0;
584 const struct internal_config *internal_conf =
585 eal_get_internal_configuration();
586
587
588 for (size = 0; size < num_hp_info; size++)
589 for (socket = 0; socket < RTE_MAX_NUMA_NODES; socket++)
590 nrpages +=
591 internal_conf->hugepage_info[size].num_pages[socket];
592
593 for (page = 0; page < nrpages; page++) {
594 struct hugepage_file *hp = &hugepg_tbl[page];
595
596 if (hp->orig_va != NULL && unlink(hp->filepath)) {
597 RTE_LOG(WARNING, EAL, "%s(): Removing %s failed: %s\n",
598 __func__, hp->filepath, strerror(errno));
599 }
600 }
601 return 0;
602}
603
604
605
606
607
608static int
609unmap_unneeded_hugepages(struct hugepage_file *hugepg_tbl,
610 struct hugepage_info *hpi,
611 unsigned num_hp_info)
612{
613 unsigned socket, size;
614 int page, nrpages = 0;
615 const struct internal_config *internal_conf =
616 eal_get_internal_configuration();
617
618
619 for (size = 0; size < num_hp_info; size++)
620 for (socket = 0; socket < RTE_MAX_NUMA_NODES; socket++)
621 nrpages += internal_conf->hugepage_info[size].num_pages[socket];
622
623 for (size = 0; size < num_hp_info; size++) {
624 for (socket = 0; socket < RTE_MAX_NUMA_NODES; socket++) {
625 unsigned pages_found = 0;
626
627
628 for (page = 0; page < nrpages; page++) {
629 struct hugepage_file *hp = &hugepg_tbl[page];
630
631
632 if ((hp->size == hpi[size].hugepage_sz) &&
633 (hp->socket_id == (int) socket)) {
634
635
636 if (pages_found == hpi[size].num_pages[socket]) {
637 uint64_t unmap_len;
638
639 unmap_len = hp->size;
640
641
642 munmap(hp->orig_va,
643 (size_t)unmap_len);
644
645 hp->orig_va = NULL;
646 if (unlink(hp->filepath) == -1) {
647 RTE_LOG(ERR, EAL, "%s(): Removing %s failed: %s\n",
648 __func__, hp->filepath, strerror(errno));
649 return -1;
650 }
651 } else {
652
653 pages_found++;
654 }
655
656 }
657 }
658 }
659 }
660
661 return 0;
662}
663
664static int
665remap_segment(struct hugepage_file *hugepages, int seg_start, int seg_end)
666{
667 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
668 struct rte_memseg_list *msl;
669 struct rte_fbarray *arr;
670 int cur_page, seg_len;
671 unsigned int msl_idx;
672 int ms_idx;
673 uint64_t page_sz;
674 size_t memseg_len;
675 int socket_id;
676#ifndef RTE_ARCH_64
677 const struct internal_config *internal_conf =
678 eal_get_internal_configuration();
679#endif
680 page_sz = hugepages[seg_start].size;
681 socket_id = hugepages[seg_start].socket_id;
682 seg_len = seg_end - seg_start;
683
684 RTE_LOG(DEBUG, EAL, "Attempting to map %" PRIu64 "M on socket %i\n",
685 (seg_len * page_sz) >> 20ULL, socket_id);
686
687
688 for (msl_idx = 0; msl_idx < RTE_MAX_MEMSEG_LISTS; msl_idx++) {
689 bool empty;
690 msl = &mcfg->memsegs[msl_idx];
691 arr = &msl->memseg_arr;
692
693 if (msl->page_sz != page_sz)
694 continue;
695 if (msl->socket_id != socket_id)
696 continue;
697
698
699 empty = arr->count == 0;
700 ms_idx = rte_fbarray_find_next_n_free(arr, 0,
701 seg_len + (empty ? 0 : 1));
702
703
704 if (ms_idx < 0)
705 continue;
706
707
708
709
710 if (!empty)
711 ms_idx++;
712 break;
713 }
714 if (msl_idx == RTE_MAX_MEMSEG_LISTS) {
715 RTE_LOG(ERR, EAL, "Could not find space for memseg. Please increase %s and/or %s in configuration.\n",
716 RTE_STR(RTE_MAX_MEMSEG_PER_TYPE),
717 RTE_STR(RTE_MAX_MEM_MB_PER_TYPE));
718 return -1;
719 }
720
721#ifdef RTE_ARCH_PPC_64
722
723 for (cur_page = seg_end - 1; cur_page >= seg_start;
724 cur_page--, ms_idx++) {
725#else
726 for (cur_page = seg_start; cur_page < seg_end; cur_page++, ms_idx++) {
727#endif
728 struct hugepage_file *hfile = &hugepages[cur_page];
729 struct rte_memseg *ms = rte_fbarray_get(arr, ms_idx);
730 void *addr;
731 int fd;
732
733 fd = open(hfile->filepath, O_RDWR);
734 if (fd < 0) {
735 RTE_LOG(ERR, EAL, "Could not open '%s': %s\n",
736 hfile->filepath, strerror(errno));
737 return -1;
738 }
739
740 if (flock(fd, LOCK_SH) < 0) {
741 RTE_LOG(DEBUG, EAL, "Could not lock '%s': %s\n",
742 hfile->filepath, strerror(errno));
743 close(fd);
744 return -1;
745 }
746 memseg_len = (size_t)page_sz;
747 addr = RTE_PTR_ADD(msl->base_va, ms_idx * memseg_len);
748
749
750
751
752 addr = mmap(addr, page_sz, PROT_READ | PROT_WRITE,
753 MAP_SHARED | MAP_POPULATE | MAP_FIXED, fd, 0);
754 if (addr == MAP_FAILED) {
755 RTE_LOG(ERR, EAL, "Couldn't remap '%s': %s\n",
756 hfile->filepath, strerror(errno));
757 close(fd);
758 return -1;
759 }
760
761
762#ifndef RTE_ARCH_64
763
764 if (!internal_conf->legacy_mem)
765 munmap(hfile->orig_va, page_sz);
766#else
767 munmap(hfile->orig_va, page_sz);
768#endif
769
770 hfile->orig_va = NULL;
771 hfile->final_va = addr;
772
773
774 if (rte_eal_iova_mode() == RTE_IOVA_VA)
775 hfile->physaddr = (uintptr_t)addr;
776
777
778 ms->addr = addr;
779 ms->hugepage_sz = page_sz;
780 ms->len = memseg_len;
781 ms->iova = hfile->physaddr;
782 ms->socket_id = hfile->socket_id;
783 ms->nchannel = rte_memory_get_nchannel();
784 ms->nrank = rte_memory_get_nrank();
785
786 rte_fbarray_set_used(arr, ms_idx);
787
788
789 if (eal_memalloc_set_seg_fd(msl_idx, ms_idx, fd) < 0)
790 RTE_LOG(ERR, EAL, "Could not store segment fd: %s\n",
791 rte_strerror(rte_errno));
792 }
793 RTE_LOG(DEBUG, EAL, "Allocated %" PRIu64 "M on socket %i\n",
794 (seg_len * page_sz) >> 20, socket_id);
795 return 0;
796}
797
798static uint64_t
799get_mem_amount(uint64_t page_sz, uint64_t max_mem)
800{
801 uint64_t area_sz, max_pages;
802
803
804 max_pages = RTE_MAX_MEMSEG_PER_LIST;
805 max_mem = RTE_MIN((uint64_t)RTE_MAX_MEM_MB_PER_LIST << 20, max_mem);
806
807 area_sz = RTE_MIN(page_sz * max_pages, max_mem);
808
809
810 area_sz = RTE_MAX(area_sz, page_sz);
811
812 return RTE_ALIGN(area_sz, page_sz);
813}
814
815static int
816memseg_list_free(struct rte_memseg_list *msl)
817{
818 if (rte_fbarray_destroy(&msl->memseg_arr)) {
819 RTE_LOG(ERR, EAL, "Cannot destroy memseg list\n");
820 return -1;
821 }
822 memset(msl, 0, sizeof(*msl));
823 return 0;
824}
825
826
827
828
829
830
831
832
833
834static int __rte_unused
835prealloc_segments(struct hugepage_file *hugepages, int n_pages)
836{
837 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
838 int cur_page, seg_start_page, end_seg, new_memseg;
839 unsigned int hpi_idx, socket, i;
840 int n_contig_segs, n_segs;
841 int msl_idx;
842 const struct internal_config *internal_conf =
843 eal_get_internal_configuration();
844
845
846
847
848
849 for (cur_page = 0; cur_page < n_pages; cur_page++) {
850 struct hugepage_file *hpi = &hugepages[cur_page];
851 munmap(hpi->orig_va, hpi->size);
852 hpi->orig_va = NULL;
853 }
854
855
856
857
858 for (hpi_idx = 0; hpi_idx < internal_conf->num_hugepage_sizes;
859 hpi_idx++) {
860 uint64_t page_sz =
861 internal_conf->hugepage_info[hpi_idx].hugepage_sz;
862
863 for (i = 0; i < rte_socket_count(); i++) {
864 struct rte_memseg_list *msl;
865
866 socket = rte_socket_id_by_idx(i);
867 n_contig_segs = 0;
868 n_segs = 0;
869 seg_start_page = -1;
870
871 for (cur_page = 0; cur_page < n_pages; cur_page++) {
872 struct hugepage_file *prev, *cur;
873 int prev_seg_start_page = -1;
874
875 cur = &hugepages[cur_page];
876 prev = cur_page == 0 ? NULL :
877 &hugepages[cur_page - 1];
878
879 new_memseg = 0;
880 end_seg = 0;
881
882 if (cur->size == 0)
883 end_seg = 1;
884 else if (cur->socket_id != (int) socket)
885 end_seg = 1;
886 else if (cur->size != page_sz)
887 end_seg = 1;
888 else if (cur_page == 0)
889 new_memseg = 1;
890#ifdef RTE_ARCH_PPC_64
891
892
893
894
895 else if ((prev->physaddr - cur->physaddr) !=
896 cur->size)
897 new_memseg = 1;
898#else
899 else if ((cur->physaddr - prev->physaddr) !=
900 cur->size)
901 new_memseg = 1;
902#endif
903 if (new_memseg) {
904
905
906
907 if (seg_start_page != -1) {
908 end_seg = 1;
909 prev_seg_start_page =
910 seg_start_page;
911 }
912 seg_start_page = cur_page;
913 }
914
915 if (end_seg) {
916 if (prev_seg_start_page != -1) {
917
918 n_contig_segs++;
919 n_segs += cur_page -
920 prev_seg_start_page;
921 } else if (seg_start_page != -1) {
922
923
924
925 n_contig_segs++;
926 n_segs += cur_page -
927 seg_start_page;
928 seg_start_page = -1;
929 continue;
930 } else {
931
932 continue;
933 }
934 }
935
936 }
937
938 if (seg_start_page != -1) {
939 n_contig_segs++;
940 n_segs += cur_page - seg_start_page;
941 }
942
943
944 if (n_segs == 0)
945 continue;
946
947
948
949
950
951 n_segs += n_contig_segs - 1;
952
953
954
955
956 for (msl_idx = 0; msl_idx < RTE_MAX_MEMSEG_LISTS;
957 msl_idx++) {
958 msl = &mcfg->memsegs[msl_idx];
959
960 if (msl->base_va != NULL)
961 continue;
962 break;
963 }
964 if (msl_idx == RTE_MAX_MEMSEG_LISTS) {
965 RTE_LOG(ERR, EAL, "Not enough space in memseg lists, please increase %s\n",
966 RTE_STR(RTE_MAX_MEMSEG_LISTS));
967 return -1;
968 }
969
970
971 if (eal_memseg_list_init(msl, page_sz, n_segs,
972 socket, msl_idx, true) < 0)
973 return -1;
974
975
976 if (eal_memseg_list_alloc(msl, 0) < 0) {
977 RTE_LOG(ERR, EAL, "Cannot preallocate 0x%"PRIx64"kB hugepages\n",
978 page_sz >> 10);
979 return -1;
980 }
981 }
982 }
983 return 0;
984}
985
986
987
988
989
990
991static int
992remap_needed_hugepages(struct hugepage_file *hugepages, int n_pages)
993{
994 int cur_page, seg_start_page, new_memseg, ret;
995
996 seg_start_page = 0;
997 for (cur_page = 0; cur_page < n_pages; cur_page++) {
998 struct hugepage_file *prev, *cur;
999
1000 new_memseg = 0;
1001
1002 cur = &hugepages[cur_page];
1003 prev = cur_page == 0 ? NULL : &hugepages[cur_page - 1];
1004
1005
1006 if (cur->size == 0)
1007 break;
1008
1009 if (cur_page == 0)
1010 new_memseg = 1;
1011 else if (cur->socket_id != prev->socket_id)
1012 new_memseg = 1;
1013 else if (cur->size != prev->size)
1014 new_memseg = 1;
1015#ifdef RTE_ARCH_PPC_64
1016
1017
1018
1019
1020 else if ((prev->physaddr - cur->physaddr) != cur->size)
1021 new_memseg = 1;
1022#else
1023 else if ((cur->physaddr - prev->physaddr) != cur->size)
1024 new_memseg = 1;
1025#endif
1026
1027 if (new_memseg) {
1028
1029 if (cur_page != 0) {
1030 ret = remap_segment(hugepages, seg_start_page,
1031 cur_page);
1032 if (ret != 0)
1033 return -1;
1034 }
1035
1036 seg_start_page = cur_page;
1037 }
1038
1039 }
1040
1041 if (cur_page != 0) {
1042 ret = remap_segment(hugepages, seg_start_page,
1043 cur_page);
1044 if (ret != 0)
1045 return -1;
1046 }
1047 return 0;
1048}
1049
1050static inline size_t
1051eal_get_hugepage_mem_size(void)
1052{
1053 uint64_t size = 0;
1054 unsigned i, j;
1055 struct internal_config *internal_conf =
1056 eal_get_internal_configuration();
1057
1058 for (i = 0; i < internal_conf->num_hugepage_sizes; i++) {
1059 struct hugepage_info *hpi = &internal_conf->hugepage_info[i];
1060 if (strnlen(hpi->hugedir, sizeof(hpi->hugedir)) != 0) {
1061 for (j = 0; j < RTE_MAX_NUMA_NODES; j++) {
1062 size += hpi->hugepage_sz * hpi->num_pages[j];
1063 }
1064 }
1065 }
1066
1067 return (size < SIZE_MAX) ? (size_t)(size) : SIZE_MAX;
1068}
1069
1070static struct sigaction huge_action_old;
1071static int huge_need_recover;
1072
1073static void
1074huge_register_sigbus(void)
1075{
1076 sigset_t mask;
1077 struct sigaction action;
1078
1079 sigemptyset(&mask);
1080 sigaddset(&mask, SIGBUS);
1081 action.sa_flags = 0;
1082 action.sa_mask = mask;
1083 action.sa_handler = huge_sigbus_handler;
1084
1085 huge_need_recover = !sigaction(SIGBUS, &action, &huge_action_old);
1086}
1087
1088static void
1089huge_recover_sigbus(void)
1090{
1091 if (huge_need_recover) {
1092 sigaction(SIGBUS, &huge_action_old, NULL);
1093 huge_need_recover = 0;
1094 }
1095}
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108static int
1109eal_legacy_hugepage_init(void)
1110{
1111 struct rte_mem_config *mcfg;
1112 struct hugepage_file *hugepage = NULL, *tmp_hp = NULL;
1113 struct hugepage_info used_hp[MAX_HUGEPAGE_SIZES];
1114 struct internal_config *internal_conf =
1115 eal_get_internal_configuration();
1116
1117 uint64_t memory[RTE_MAX_NUMA_NODES];
1118
1119 unsigned hp_offset;
1120 int i, j;
1121 int nr_hugefiles, nr_hugepages = 0;
1122 void *addr;
1123
1124 memset(used_hp, 0, sizeof(used_hp));
1125
1126
1127 mcfg = rte_eal_get_configuration()->mem_config;
1128
1129
1130 if (internal_conf->no_hugetlbfs) {
1131 void *prealloc_addr;
1132 size_t mem_sz;
1133 struct rte_memseg_list *msl;
1134 int n_segs, fd, flags;
1135#ifdef MEMFD_SUPPORTED
1136 int memfd;
1137#endif
1138 uint64_t page_sz;
1139
1140
1141 internal_conf->legacy_mem = 1;
1142
1143
1144 internal_conf->single_file_segments = 1;
1145
1146
1147 msl = &mcfg->memsegs[0];
1148
1149 mem_sz = internal_conf->memory;
1150 page_sz = RTE_PGSIZE_4K;
1151 n_segs = mem_sz / page_sz;
1152
1153 if (eal_memseg_list_init_named(
1154 msl, "nohugemem", page_sz, n_segs, 0, true)) {
1155 return -1;
1156 }
1157
1158
1159 fd = -1;
1160 flags = MAP_PRIVATE | MAP_ANONYMOUS;
1161
1162#ifdef MEMFD_SUPPORTED
1163
1164 memfd = memfd_create("nohuge", 0);
1165 if (memfd < 0) {
1166 RTE_LOG(DEBUG, EAL, "Cannot create memfd: %s\n",
1167 strerror(errno));
1168 RTE_LOG(DEBUG, EAL, "Falling back to anonymous map\n");
1169 } else {
1170
1171 if (ftruncate(memfd, internal_conf->memory) < 0) {
1172 RTE_LOG(ERR, EAL, "Cannot resize memfd: %s\n",
1173 strerror(errno));
1174 RTE_LOG(ERR, EAL, "Falling back to anonymous map\n");
1175 close(memfd);
1176 } else {
1177
1178
1179
1180
1181
1182 RTE_LOG(DEBUG, EAL, "Using memfd for anonymous memory\n");
1183 fd = memfd;
1184 flags = MAP_SHARED;
1185 }
1186 }
1187#endif
1188
1189
1190
1191 if (eal_memseg_list_alloc(msl, 0)) {
1192 RTE_LOG(ERR, EAL, "Cannot preallocate VA space for hugepage memory\n");
1193 return -1;
1194 }
1195
1196 prealloc_addr = msl->base_va;
1197 addr = mmap(prealloc_addr, mem_sz, PROT_READ | PROT_WRITE,
1198 flags | MAP_FIXED, fd, 0);
1199 if (addr == MAP_FAILED || addr != prealloc_addr) {
1200 RTE_LOG(ERR, EAL, "%s: mmap() failed: %s\n", __func__,
1201 strerror(errno));
1202 munmap(prealloc_addr, mem_sz);
1203 return -1;
1204 }
1205
1206
1207
1208
1209 if (fd != -1) {
1210 if (eal_memalloc_set_seg_list_fd(0, fd) < 0) {
1211 RTE_LOG(ERR, EAL, "Cannot set up segment list fd\n");
1212
1213 }
1214 }
1215
1216 eal_memseg_list_populate(msl, addr, n_segs);
1217
1218 if (mcfg->dma_maskbits &&
1219 rte_mem_check_dma_mask_thread_unsafe(mcfg->dma_maskbits)) {
1220 RTE_LOG(ERR, EAL,
1221 "%s(): couldn't allocate memory due to IOVA exceeding limits of current DMA mask.\n",
1222 __func__);
1223 if (rte_eal_iova_mode() == RTE_IOVA_VA &&
1224 rte_eal_using_phys_addrs())
1225 RTE_LOG(ERR, EAL,
1226 "%s(): Please try initializing EAL with --iova-mode=pa parameter.\n",
1227 __func__);
1228 goto fail;
1229 }
1230 return 0;
1231 }
1232
1233
1234
1235 for (i = 0; i < (int) internal_conf->num_hugepage_sizes; i++) {
1236
1237 used_hp[i].hugepage_sz = internal_conf->hugepage_info[i].hugepage_sz;
1238
1239 nr_hugepages += internal_conf->hugepage_info[i].num_pages[0];
1240 }
1241
1242
1243
1244
1245
1246
1247
1248 tmp_hp = malloc(nr_hugepages * sizeof(struct hugepage_file));
1249 if (tmp_hp == NULL)
1250 goto fail;
1251
1252 memset(tmp_hp, 0, nr_hugepages * sizeof(struct hugepage_file));
1253
1254 hp_offset = 0;
1255
1256 huge_register_sigbus();
1257
1258
1259 for (i = 0; i < RTE_MAX_NUMA_NODES; i++)
1260 memory[i] = internal_conf->socket_mem[i];
1261
1262
1263 for (i = 0; i < (int)internal_conf->num_hugepage_sizes; i++) {
1264 unsigned pages_old, pages_new;
1265 struct hugepage_info *hpi;
1266
1267
1268
1269
1270
1271
1272 hpi = &internal_conf->hugepage_info[i];
1273
1274 if (hpi->num_pages[0] == 0)
1275 continue;
1276
1277
1278 pages_old = hpi->num_pages[0];
1279 pages_new = map_all_hugepages(&tmp_hp[hp_offset], hpi, memory);
1280 if (pages_new < pages_old) {
1281 RTE_LOG(DEBUG, EAL,
1282 "%d not %d hugepages of size %u MB allocated\n",
1283 pages_new, pages_old,
1284 (unsigned)(hpi->hugepage_sz / 0x100000));
1285
1286 int pages = pages_old - pages_new;
1287
1288 nr_hugepages -= pages;
1289 hpi->num_pages[0] = pages_new;
1290 if (pages_new == 0)
1291 continue;
1292 }
1293
1294 if (rte_eal_using_phys_addrs() &&
1295 rte_eal_iova_mode() != RTE_IOVA_VA) {
1296
1297 if (find_physaddrs(&tmp_hp[hp_offset], hpi) < 0) {
1298 RTE_LOG(DEBUG, EAL, "Failed to find phys addr "
1299 "for %u MB pages\n",
1300 (unsigned int)(hpi->hugepage_sz / 0x100000));
1301 goto fail;
1302 }
1303 } else {
1304
1305 if (set_physaddrs(&tmp_hp[hp_offset], hpi) < 0) {
1306 RTE_LOG(DEBUG, EAL, "Failed to set phys addr "
1307 "for %u MB pages\n",
1308 (unsigned int)(hpi->hugepage_sz / 0x100000));
1309 goto fail;
1310 }
1311 }
1312
1313 if (find_numasocket(&tmp_hp[hp_offset], hpi) < 0){
1314 RTE_LOG(DEBUG, EAL, "Failed to find NUMA socket for %u MB pages\n",
1315 (unsigned)(hpi->hugepage_sz / 0x100000));
1316 goto fail;
1317 }
1318
1319 qsort(&tmp_hp[hp_offset], hpi->num_pages[0],
1320 sizeof(struct hugepage_file), cmp_physaddr);
1321
1322
1323 hp_offset += hpi->num_pages[0];
1324 }
1325
1326 huge_recover_sigbus();
1327
1328 if (internal_conf->memory == 0 && internal_conf->force_sockets == 0)
1329 internal_conf->memory = eal_get_hugepage_mem_size();
1330
1331 nr_hugefiles = nr_hugepages;
1332
1333
1334
1335 for (i = 0; i < (int) internal_conf->num_hugepage_sizes; i++)
1336 for (j = 0; j < RTE_MAX_NUMA_NODES; j++)
1337 internal_conf->hugepage_info[i].num_pages[j] = 0;
1338
1339
1340 for (i = 0; i < nr_hugefiles; i++) {
1341 int socket = tmp_hp[i].socket_id;
1342
1343
1344 const int nb_hpsizes = RTE_MIN(MAX_HUGEPAGE_SIZES,
1345 (int)internal_conf->num_hugepage_sizes);
1346 for (j = 0; j < nb_hpsizes; j++) {
1347 if (tmp_hp[i].size ==
1348 internal_conf->hugepage_info[j].hugepage_sz) {
1349 internal_conf->hugepage_info[j].num_pages[socket]++;
1350 }
1351 }
1352 }
1353
1354
1355 for (i = 0; i < RTE_MAX_NUMA_NODES; i++)
1356 memory[i] = internal_conf->socket_mem[i];
1357
1358
1359 nr_hugepages = eal_dynmem_calc_num_pages_per_socket(memory,
1360 internal_conf->hugepage_info, used_hp,
1361 internal_conf->num_hugepage_sizes);
1362
1363
1364 if (nr_hugepages < 0)
1365 goto fail;
1366
1367
1368 for (i = 0; i < (int) internal_conf->num_hugepage_sizes; i++) {
1369 for (j = 0; j < RTE_MAX_NUMA_NODES; j++) {
1370 if (used_hp[i].num_pages[j] > 0) {
1371 RTE_LOG(DEBUG, EAL,
1372 "Requesting %u pages of size %uMB"
1373 " from socket %i\n",
1374 used_hp[i].num_pages[j],
1375 (unsigned)
1376 (used_hp[i].hugepage_sz / 0x100000),
1377 j);
1378 }
1379 }
1380 }
1381
1382
1383 hugepage = create_shared_memory(eal_hugepage_data_path(),
1384 nr_hugefiles * sizeof(struct hugepage_file));
1385
1386 if (hugepage == NULL) {
1387 RTE_LOG(ERR, EAL, "Failed to create shared memory!\n");
1388 goto fail;
1389 }
1390 memset(hugepage, 0, nr_hugefiles * sizeof(struct hugepage_file));
1391
1392
1393
1394
1395
1396 if (unmap_unneeded_hugepages(tmp_hp, used_hp,
1397 internal_conf->num_hugepage_sizes) < 0) {
1398 RTE_LOG(ERR, EAL, "Unmapping and locking hugepages failed!\n");
1399 goto fail;
1400 }
1401
1402
1403
1404
1405
1406
1407 if (copy_hugepages_to_shared_mem(hugepage, nr_hugefiles,
1408 tmp_hp, nr_hugefiles) < 0) {
1409 RTE_LOG(ERR, EAL, "Copying tables to shared memory failed!\n");
1410 goto fail;
1411 }
1412
1413#ifndef RTE_ARCH_64
1414
1415 if (internal_conf->legacy_mem &&
1416 prealloc_segments(hugepage, nr_hugefiles)) {
1417 RTE_LOG(ERR, EAL, "Could not preallocate VA space for hugepages\n");
1418 goto fail;
1419 }
1420#endif
1421
1422
1423
1424
1425 if (remap_needed_hugepages(hugepage, nr_hugefiles)) {
1426 RTE_LOG(ERR, EAL, "Couldn't remap hugepage files into memseg lists\n");
1427 goto fail;
1428 }
1429
1430
1431 if (internal_conf->hugepage_unlink &&
1432 unlink_hugepage_files(tmp_hp, internal_conf->num_hugepage_sizes) < 0) {
1433 RTE_LOG(ERR, EAL, "Unlinking hugepage files failed!\n");
1434 goto fail;
1435 }
1436
1437
1438 free(tmp_hp);
1439 tmp_hp = NULL;
1440
1441 munmap(hugepage, nr_hugefiles * sizeof(struct hugepage_file));
1442 hugepage = NULL;
1443
1444
1445
1446
1447 for (i = 0; i < RTE_MAX_MEMSEG_LISTS; i++) {
1448 struct rte_memseg_list *msl = &mcfg->memsegs[i];
1449 size_t mem_sz;
1450
1451
1452 if (msl->base_va == NULL)
1453 continue;
1454
1455 if (msl->memseg_arr.count > 0)
1456 continue;
1457
1458 mem_sz = msl->len;
1459 munmap(msl->base_va, mem_sz);
1460 msl->base_va = NULL;
1461 msl->heap = 0;
1462
1463
1464 rte_fbarray_destroy(&msl->memseg_arr);
1465 }
1466
1467 if (mcfg->dma_maskbits &&
1468 rte_mem_check_dma_mask_thread_unsafe(mcfg->dma_maskbits)) {
1469 RTE_LOG(ERR, EAL,
1470 "%s(): couldn't allocate memory due to IOVA exceeding limits of current DMA mask.\n",
1471 __func__);
1472 goto fail;
1473 }
1474
1475 return 0;
1476
1477fail:
1478 huge_recover_sigbus();
1479 free(tmp_hp);
1480 if (hugepage != NULL)
1481 munmap(hugepage, nr_hugefiles * sizeof(struct hugepage_file));
1482
1483 return -1;
1484}
1485
1486
1487
1488
1489static off_t
1490getFileSize(int fd)
1491{
1492 struct stat st;
1493 if (fstat(fd, &st) < 0)
1494 return 0;
1495 return st.st_size;
1496}
1497
1498
1499
1500
1501
1502
1503
1504static int
1505eal_legacy_hugepage_attach(void)
1506{
1507 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
1508 struct hugepage_file *hp = NULL;
1509 unsigned int num_hp = 0;
1510 unsigned int i = 0;
1511 unsigned int cur_seg;
1512 off_t size = 0;
1513 int fd, fd_hugepage = -1;
1514
1515 if (aslr_enabled() > 0) {
1516 RTE_LOG(WARNING, EAL, "WARNING: Address Space Layout Randomization "
1517 "(ASLR) is enabled in the kernel.\n");
1518 RTE_LOG(WARNING, EAL, " This may cause issues with mapping memory "
1519 "into secondary processes\n");
1520 }
1521
1522 fd_hugepage = open(eal_hugepage_data_path(), O_RDONLY);
1523 if (fd_hugepage < 0) {
1524 RTE_LOG(ERR, EAL, "Could not open %s\n",
1525 eal_hugepage_data_path());
1526 goto error;
1527 }
1528
1529 size = getFileSize(fd_hugepage);
1530 hp = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd_hugepage, 0);
1531 if (hp == MAP_FAILED) {
1532 RTE_LOG(ERR, EAL, "Could not mmap %s\n",
1533 eal_hugepage_data_path());
1534 goto error;
1535 }
1536
1537 num_hp = size / sizeof(struct hugepage_file);
1538 RTE_LOG(DEBUG, EAL, "Analysing %u files\n", num_hp);
1539
1540
1541
1542
1543
1544
1545 for (i = 0; i < num_hp; i++) {
1546 struct hugepage_file *hf = &hp[i];
1547 size_t map_sz = hf->size;
1548 void *map_addr = hf->final_va;
1549 int msl_idx, ms_idx;
1550 struct rte_memseg_list *msl;
1551 struct rte_memseg *ms;
1552
1553
1554 if (map_sz == 0)
1555 break;
1556
1557 fd = open(hf->filepath, O_RDWR);
1558 if (fd < 0) {
1559 RTE_LOG(ERR, EAL, "Could not open %s: %s\n",
1560 hf->filepath, strerror(errno));
1561 goto error;
1562 }
1563
1564 map_addr = mmap(map_addr, map_sz, PROT_READ | PROT_WRITE,
1565 MAP_SHARED | MAP_FIXED, fd, 0);
1566 if (map_addr == MAP_FAILED) {
1567 RTE_LOG(ERR, EAL, "Could not map %s: %s\n",
1568 hf->filepath, strerror(errno));
1569 goto fd_error;
1570 }
1571
1572
1573 if (flock(fd, LOCK_SH) < 0) {
1574 RTE_LOG(DEBUG, EAL, "%s(): Locking file failed: %s\n",
1575 __func__, strerror(errno));
1576 goto mmap_error;
1577 }
1578
1579
1580 msl = rte_mem_virt2memseg_list(map_addr);
1581 if (msl == NULL) {
1582 RTE_LOG(DEBUG, EAL, "%s(): Cannot find memseg list\n",
1583 __func__);
1584 goto mmap_error;
1585 }
1586 ms = rte_mem_virt2memseg(map_addr, msl);
1587 if (ms == NULL) {
1588 RTE_LOG(DEBUG, EAL, "%s(): Cannot find memseg\n",
1589 __func__);
1590 goto mmap_error;
1591 }
1592
1593 msl_idx = msl - mcfg->memsegs;
1594 ms_idx = rte_fbarray_find_idx(&msl->memseg_arr, ms);
1595 if (ms_idx < 0) {
1596 RTE_LOG(DEBUG, EAL, "%s(): Cannot find memseg idx\n",
1597 __func__);
1598 goto mmap_error;
1599 }
1600
1601
1602 if (eal_memalloc_set_seg_fd(msl_idx, ms_idx, fd) < 0)
1603 RTE_LOG(ERR, EAL, "Could not store segment fd: %s\n",
1604 rte_strerror(rte_errno));
1605 }
1606
1607 munmap(hp, size);
1608 close(fd_hugepage);
1609 return 0;
1610
1611mmap_error:
1612 munmap(hp[i].final_va, hp[i].size);
1613fd_error:
1614 close(fd);
1615error:
1616
1617 for (cur_seg = 0; cur_seg < i; cur_seg++)
1618 munmap(hp[cur_seg].final_va, hp[cur_seg].size);
1619
1620 if (hp != NULL && hp != MAP_FAILED)
1621 munmap(hp, size);
1622 if (fd_hugepage >= 0)
1623 close(fd_hugepage);
1624 return -1;
1625}
1626
1627static int
1628eal_hugepage_attach(void)
1629{
1630 if (eal_memalloc_sync_with_primary()) {
1631 RTE_LOG(ERR, EAL, "Could not map memory from primary process\n");
1632 if (aslr_enabled() > 0)
1633 RTE_LOG(ERR, EAL, "It is recommended to disable ASLR in the kernel and retry running both primary and secondary processes\n");
1634 return -1;
1635 }
1636 return 0;
1637}
1638
1639int
1640rte_eal_hugepage_init(void)
1641{
1642 const struct internal_config *internal_conf =
1643 eal_get_internal_configuration();
1644
1645 return internal_conf->legacy_mem ?
1646 eal_legacy_hugepage_init() :
1647 eal_dynmem_hugepage_init();
1648}
1649
1650int
1651rte_eal_hugepage_attach(void)
1652{
1653 const struct internal_config *internal_conf =
1654 eal_get_internal_configuration();
1655
1656 return internal_conf->legacy_mem ?
1657 eal_legacy_hugepage_attach() :
1658 eal_hugepage_attach();
1659}
1660
1661int
1662rte_eal_using_phys_addrs(void)
1663{
1664 if (phys_addrs_available == -1) {
1665 uint64_t tmp = 0;
1666
1667 if (rte_eal_has_hugepages() != 0 &&
1668 rte_mem_virt2phy(&tmp) != RTE_BAD_PHYS_ADDR)
1669 phys_addrs_available = 1;
1670 else
1671 phys_addrs_available = 0;
1672 }
1673 return phys_addrs_available;
1674}
1675
1676static int __rte_unused
1677memseg_primary_init_32(void)
1678{
1679 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
1680 int active_sockets, hpi_idx, msl_idx = 0;
1681 unsigned int socket_id, i;
1682 struct rte_memseg_list *msl;
1683 uint64_t extra_mem_per_socket, total_extra_mem, total_requested_mem;
1684 uint64_t max_mem;
1685 struct internal_config *internal_conf =
1686 eal_get_internal_configuration();
1687
1688
1689 if (internal_conf->no_hugetlbfs)
1690 return 0;
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702 if (internal_conf->legacy_mem)
1703 return 0;
1704
1705
1706
1707
1708
1709 active_sockets = 0;
1710 total_requested_mem = 0;
1711 if (internal_conf->force_sockets)
1712 for (i = 0; i < rte_socket_count(); i++) {
1713 uint64_t mem;
1714
1715 socket_id = rte_socket_id_by_idx(i);
1716 mem = internal_conf->socket_mem[socket_id];
1717
1718 if (mem == 0)
1719 continue;
1720
1721 active_sockets++;
1722 total_requested_mem += mem;
1723 }
1724 else
1725 total_requested_mem = internal_conf->memory;
1726
1727 max_mem = (uint64_t)RTE_MAX_MEM_MB << 20;
1728 if (total_requested_mem > max_mem) {
1729 RTE_LOG(ERR, EAL, "Invalid parameters: 32-bit process can at most use %uM of memory\n",
1730 (unsigned int)(max_mem >> 20));
1731 return -1;
1732 }
1733 total_extra_mem = max_mem - total_requested_mem;
1734 extra_mem_per_socket = active_sockets == 0 ? total_extra_mem :
1735 total_extra_mem / active_sockets;
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751 for (i = 0; i < rte_socket_count(); i++) {
1752 int hp_sizes = (int) internal_conf->num_hugepage_sizes;
1753 uint64_t max_socket_mem, cur_socket_mem;
1754 unsigned int main_lcore_socket;
1755 struct rte_config *cfg = rte_eal_get_configuration();
1756 bool skip;
1757
1758 socket_id = rte_socket_id_by_idx(i);
1759
1760#ifndef RTE_EAL_NUMA_AWARE_HUGEPAGES
1761
1762 if (!internal_conf->legacy_mem && socket_id > 0)
1763 break;
1764#endif
1765
1766
1767 skip = active_sockets != 0 &&
1768 internal_conf->socket_mem[socket_id] == 0;
1769
1770
1771
1772 main_lcore_socket = rte_lcore_to_socket_id(cfg->main_lcore);
1773 skip |= active_sockets == 0 && socket_id != main_lcore_socket;
1774
1775 if (skip) {
1776 RTE_LOG(DEBUG, EAL, "Will not preallocate memory on socket %u\n",
1777 socket_id);
1778 continue;
1779 }
1780
1781
1782 max_socket_mem = (active_sockets != 0 ?
1783 internal_conf->socket_mem[socket_id] :
1784 internal_conf->memory) +
1785 extra_mem_per_socket;
1786 cur_socket_mem = 0;
1787
1788 for (hpi_idx = 0; hpi_idx < hp_sizes; hpi_idx++) {
1789 uint64_t max_pagesz_mem, cur_pagesz_mem = 0;
1790 uint64_t hugepage_sz;
1791 struct hugepage_info *hpi;
1792 int type_msl_idx, max_segs, total_segs = 0;
1793
1794 hpi = &internal_conf->hugepage_info[hpi_idx];
1795 hugepage_sz = hpi->hugepage_sz;
1796
1797
1798 if (hpi->num_pages[socket_id] == 0)
1799 continue;
1800
1801 max_segs = RTE_MAX_MEMSEG_PER_TYPE;
1802 max_pagesz_mem = max_socket_mem - cur_socket_mem;
1803
1804
1805 max_pagesz_mem = RTE_ALIGN_FLOOR(max_pagesz_mem,
1806 hugepage_sz);
1807
1808 RTE_LOG(DEBUG, EAL, "Attempting to preallocate "
1809 "%" PRIu64 "M on socket %i\n",
1810 max_pagesz_mem >> 20, socket_id);
1811
1812 type_msl_idx = 0;
1813 while (cur_pagesz_mem < max_pagesz_mem &&
1814 total_segs < max_segs) {
1815 uint64_t cur_mem;
1816 unsigned int n_segs;
1817
1818 if (msl_idx >= RTE_MAX_MEMSEG_LISTS) {
1819 RTE_LOG(ERR, EAL,
1820 "No more space in memseg lists, please increase %s\n",
1821 RTE_STR(RTE_MAX_MEMSEG_LISTS));
1822 return -1;
1823 }
1824
1825 msl = &mcfg->memsegs[msl_idx];
1826
1827 cur_mem = get_mem_amount(hugepage_sz,
1828 max_pagesz_mem);
1829 n_segs = cur_mem / hugepage_sz;
1830
1831 if (eal_memseg_list_init(msl, hugepage_sz,
1832 n_segs, socket_id, type_msl_idx,
1833 true)) {
1834
1835
1836
1837 RTE_LOG(ERR, EAL, "Cannot allocate memseg list\n");
1838 return -1;
1839 }
1840
1841 if (eal_memseg_list_alloc(msl, 0)) {
1842
1843
1844
1845 RTE_LOG(ERR, EAL, "Cannot allocate VA space for memseg list, retrying with different page size\n");
1846
1847 if (memseg_list_free(msl))
1848 return -1;
1849 break;
1850 }
1851
1852 total_segs += msl->memseg_arr.len;
1853 cur_pagesz_mem = total_segs * hugepage_sz;
1854 type_msl_idx++;
1855 msl_idx++;
1856 }
1857 cur_socket_mem += cur_pagesz_mem;
1858 }
1859 if (cur_socket_mem == 0) {
1860 RTE_LOG(ERR, EAL, "Cannot allocate VA space on socket %u\n",
1861 socket_id);
1862 return -1;
1863 }
1864 }
1865
1866 return 0;
1867}
1868
1869static int __rte_unused
1870memseg_primary_init(void)
1871{
1872 return eal_dynmem_memseg_lists_init();
1873}
1874
1875static int
1876memseg_secondary_init(void)
1877{
1878 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
1879 int msl_idx = 0;
1880 struct rte_memseg_list *msl;
1881
1882 for (msl_idx = 0; msl_idx < RTE_MAX_MEMSEG_LISTS; msl_idx++) {
1883
1884 msl = &mcfg->memsegs[msl_idx];
1885
1886
1887 if (msl->memseg_arr.len == 0)
1888 continue;
1889
1890 if (rte_fbarray_attach(&msl->memseg_arr)) {
1891 RTE_LOG(ERR, EAL, "Cannot attach to primary process memseg lists\n");
1892 return -1;
1893 }
1894
1895
1896 if (eal_memseg_list_alloc(msl, 0)) {
1897 RTE_LOG(ERR, EAL, "Cannot preallocate VA space for hugepage memory\n");
1898 return -1;
1899 }
1900 }
1901
1902 return 0;
1903}
1904
1905int
1906rte_eal_memseg_init(void)
1907{
1908
1909 struct rlimit lim;
1910
1911#ifndef RTE_EAL_NUMA_AWARE_HUGEPAGES
1912 const struct internal_config *internal_conf =
1913 eal_get_internal_configuration();
1914#endif
1915 if (getrlimit(RLIMIT_NOFILE, &lim) == 0) {
1916
1917 lim.rlim_cur = lim.rlim_max;
1918
1919 if (setrlimit(RLIMIT_NOFILE, &lim) < 0) {
1920 RTE_LOG(DEBUG, EAL, "Setting maximum number of open files failed: %s\n",
1921 strerror(errno));
1922 } else {
1923 RTE_LOG(DEBUG, EAL, "Setting maximum number of open files to %"
1924 PRIu64 "\n",
1925 (uint64_t)lim.rlim_cur);
1926 }
1927 } else {
1928 RTE_LOG(ERR, EAL, "Cannot get current resource limits\n");
1929 }
1930#ifndef RTE_EAL_NUMA_AWARE_HUGEPAGES
1931 if (!internal_conf->legacy_mem && rte_socket_count() > 1) {
1932 RTE_LOG(WARNING, EAL, "DPDK is running on a NUMA system, but is compiled without NUMA support.\n");
1933 RTE_LOG(WARNING, EAL, "This will have adverse consequences for performance and usability.\n");
1934 RTE_LOG(WARNING, EAL, "Please use --"OPT_LEGACY_MEM" option, or recompile with NUMA support.\n");
1935 }
1936#endif
1937
1938 return rte_eal_process_type() == RTE_PROC_PRIMARY ?
1939#ifndef RTE_ARCH_64
1940 memseg_primary_init_32() :
1941#else
1942 memseg_primary_init() :
1943#endif
1944 memseg_secondary_init();
1945}
1946