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