mem: replace memseg with memseg lists
[dpdk.git] / lib / librte_eal / linuxapp / eal / eal_memory.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation.
3  * Copyright(c) 2013 6WIND S.A.
4  */
5
6 #define _FILE_OFFSET_BITS 64
7 #include <errno.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 <unistd.h>
21 #include <limits.h>
22 #include <sys/ioctl.h>
23 #include <sys/time.h>
24 #include <signal.h>
25 #include <setjmp.h>
26 #ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
27 #include <numa.h>
28 #include <numaif.h>
29 #endif
30
31 #include <rte_errno.h>
32 #include <rte_log.h>
33 #include <rte_memory.h>
34 #include <rte_launch.h>
35 #include <rte_eal.h>
36 #include <rte_eal_memconfig.h>
37 #include <rte_per_lcore.h>
38 #include <rte_lcore.h>
39 #include <rte_common.h>
40 #include <rte_string_fns.h>
41
42 #include "eal_private.h"
43 #include "eal_internal_cfg.h"
44 #include "eal_filesystem.h"
45 #include "eal_hugepages.h"
46
47 #define PFN_MASK_SIZE   8
48
49 /**
50  * @file
51  * Huge page mapping under linux
52  *
53  * To reserve a big contiguous amount of memory, we use the hugepage
54  * feature of linux. For that, we need to have hugetlbfs mounted. This
55  * code will create many files in this directory (one per page) and
56  * map them in virtual memory. For each page, we will retrieve its
57  * physical address and remap it in order to have a virtual contiguous
58  * zone as well as a physical contiguous zone.
59  */
60
61 static bool phys_addrs_available = true;
62
63 #define RANDOMIZE_VA_SPACE_FILE "/proc/sys/kernel/randomize_va_space"
64
65 static void
66 test_phys_addrs_available(void)
67 {
68         uint64_t tmp;
69         phys_addr_t physaddr;
70
71         if (!rte_eal_has_hugepages()) {
72                 RTE_LOG(ERR, EAL,
73                         "Started without hugepages support, physical addresses not available\n");
74                 phys_addrs_available = false;
75                 return;
76         }
77
78         physaddr = rte_mem_virt2phy(&tmp);
79         if (physaddr == RTE_BAD_PHYS_ADDR) {
80                 if (rte_eal_iova_mode() == RTE_IOVA_PA)
81                         RTE_LOG(ERR, EAL,
82                                 "Cannot obtain physical addresses: %s. "
83                                 "Only vfio will function.\n",
84                                 strerror(errno));
85                 phys_addrs_available = false;
86         }
87 }
88
89 /*
90  * Get physical address of any mapped virtual address in the current process.
91  */
92 phys_addr_t
93 rte_mem_virt2phy(const void *virtaddr)
94 {
95         int fd, retval;
96         uint64_t page, physaddr;
97         unsigned long virt_pfn;
98         int page_size;
99         off_t offset;
100
101         /* Cannot parse /proc/self/pagemap, no need to log errors everywhere */
102         if (!phys_addrs_available)
103                 return RTE_BAD_IOVA;
104
105         /* standard page size */
106         page_size = getpagesize();
107
108         fd = open("/proc/self/pagemap", O_RDONLY);
109         if (fd < 0) {
110                 RTE_LOG(ERR, EAL, "%s(): cannot open /proc/self/pagemap: %s\n",
111                         __func__, strerror(errno));
112                 return RTE_BAD_IOVA;
113         }
114
115         virt_pfn = (unsigned long)virtaddr / page_size;
116         offset = sizeof(uint64_t) * virt_pfn;
117         if (lseek(fd, offset, SEEK_SET) == (off_t) -1) {
118                 RTE_LOG(ERR, EAL, "%s(): seek error in /proc/self/pagemap: %s\n",
119                                 __func__, strerror(errno));
120                 close(fd);
121                 return RTE_BAD_IOVA;
122         }
123
124         retval = read(fd, &page, PFN_MASK_SIZE);
125         close(fd);
126         if (retval < 0) {
127                 RTE_LOG(ERR, EAL, "%s(): cannot read /proc/self/pagemap: %s\n",
128                                 __func__, strerror(errno));
129                 return RTE_BAD_IOVA;
130         } else if (retval != PFN_MASK_SIZE) {
131                 RTE_LOG(ERR, EAL, "%s(): read %d bytes from /proc/self/pagemap "
132                                 "but expected %d:\n",
133                                 __func__, retval, PFN_MASK_SIZE);
134                 return RTE_BAD_IOVA;
135         }
136
137         /*
138          * the pfn (page frame number) are bits 0-54 (see
139          * pagemap.txt in linux Documentation)
140          */
141         if ((page & 0x7fffffffffffffULL) == 0)
142                 return RTE_BAD_IOVA;
143
144         physaddr = ((page & 0x7fffffffffffffULL) * page_size)
145                 + ((unsigned long)virtaddr % page_size);
146
147         return physaddr;
148 }
149
150 rte_iova_t
151 rte_mem_virt2iova(const void *virtaddr)
152 {
153         if (rte_eal_iova_mode() == RTE_IOVA_VA)
154                 return (uintptr_t)virtaddr;
155         return rte_mem_virt2phy(virtaddr);
156 }
157
158 /*
159  * For each hugepage in hugepg_tbl, fill the physaddr value. We find
160  * it by browsing the /proc/self/pagemap special file.
161  */
162 static int
163 find_physaddrs(struct hugepage_file *hugepg_tbl, struct hugepage_info *hpi)
164 {
165         unsigned int i;
166         phys_addr_t addr;
167
168         for (i = 0; i < hpi->num_pages[0]; i++) {
169                 addr = rte_mem_virt2phy(hugepg_tbl[i].orig_va);
170                 if (addr == RTE_BAD_PHYS_ADDR)
171                         return -1;
172                 hugepg_tbl[i].physaddr = addr;
173         }
174         return 0;
175 }
176
177 /*
178  * For each hugepage in hugepg_tbl, fill the physaddr value sequentially.
179  */
180 static int
181 set_physaddrs(struct hugepage_file *hugepg_tbl, struct hugepage_info *hpi)
182 {
183         unsigned int i;
184         static phys_addr_t addr;
185
186         for (i = 0; i < hpi->num_pages[0]; i++) {
187                 hugepg_tbl[i].physaddr = addr;
188                 addr += hugepg_tbl[i].size;
189         }
190         return 0;
191 }
192
193 /*
194  * Check whether address-space layout randomization is enabled in
195  * the kernel. This is important for multi-process as it can prevent
196  * two processes mapping data to the same virtual address
197  * Returns:
198  *    0 - address space randomization disabled
199  *    1/2 - address space randomization enabled
200  *    negative error code on error
201  */
202 static int
203 aslr_enabled(void)
204 {
205         char c;
206         int retval, fd = open(RANDOMIZE_VA_SPACE_FILE, O_RDONLY);
207         if (fd < 0)
208                 return -errno;
209         retval = read(fd, &c, 1);
210         close(fd);
211         if (retval < 0)
212                 return -errno;
213         if (retval == 0)
214                 return -EIO;
215         switch (c) {
216                 case '0' : return 0;
217                 case '1' : return 1;
218                 case '2' : return 2;
219                 default: return -EINVAL;
220         }
221 }
222
223 static sigjmp_buf huge_jmpenv;
224
225 static void huge_sigbus_handler(int signo __rte_unused)
226 {
227         siglongjmp(huge_jmpenv, 1);
228 }
229
230 /* Put setjmp into a wrap method to avoid compiling error. Any non-volatile,
231  * non-static local variable in the stack frame calling sigsetjmp might be
232  * clobbered by a call to longjmp.
233  */
234 static int huge_wrap_sigsetjmp(void)
235 {
236         return sigsetjmp(huge_jmpenv, 1);
237 }
238
239 #ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
240 /* Callback for numa library. */
241 void numa_error(char *where)
242 {
243         RTE_LOG(ERR, EAL, "%s failed: %s\n", where, strerror(errno));
244 }
245 #endif
246
247 /*
248  * Mmap all hugepages of hugepage table: it first open a file in
249  * hugetlbfs, then mmap() hugepage_sz data in it. If orig is set, the
250  * virtual address is stored in hugepg_tbl[i].orig_va, else it is stored
251  * in hugepg_tbl[i].final_va. The second mapping (when orig is 0) tries to
252  * map contiguous physical blocks in contiguous virtual blocks.
253  */
254 static unsigned
255 map_all_hugepages(struct hugepage_file *hugepg_tbl, struct hugepage_info *hpi,
256                   uint64_t *essential_memory __rte_unused)
257 {
258         int fd;
259         unsigned i;
260         void *virtaddr;
261         struct flock lck = {0};
262 #ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
263         int node_id = -1;
264         int essential_prev = 0;
265         int oldpolicy;
266         struct bitmask *oldmask = numa_allocate_nodemask();
267         bool have_numa = true;
268         unsigned long maxnode = 0;
269
270         /* Check if kernel supports NUMA. */
271         if (numa_available() != 0) {
272                 RTE_LOG(DEBUG, EAL, "NUMA is not supported.\n");
273                 have_numa = false;
274         }
275
276         if (have_numa) {
277                 RTE_LOG(DEBUG, EAL, "Trying to obtain current memory policy.\n");
278                 if (get_mempolicy(&oldpolicy, oldmask->maskp,
279                                   oldmask->size + 1, 0, 0) < 0) {
280                         RTE_LOG(ERR, EAL,
281                                 "Failed to get current mempolicy: %s. "
282                                 "Assuming MPOL_DEFAULT.\n", strerror(errno));
283                         oldpolicy = MPOL_DEFAULT;
284                 }
285                 for (i = 0; i < RTE_MAX_NUMA_NODES; i++)
286                         if (internal_config.socket_mem[i])
287                                 maxnode = i + 1;
288         }
289 #endif
290
291         for (i = 0; i < hpi->num_pages[0]; i++) {
292                 struct hugepage_file *hf = &hugepg_tbl[i];
293                 uint64_t hugepage_sz = hpi->hugepage_sz;
294
295 #ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
296                 if (maxnode) {
297                         unsigned int j;
298
299                         for (j = 0; j < maxnode; j++)
300                                 if (essential_memory[j])
301                                         break;
302
303                         if (j == maxnode) {
304                                 node_id = (node_id + 1) % maxnode;
305                                 while (!internal_config.socket_mem[node_id]) {
306                                         node_id++;
307                                         node_id %= maxnode;
308                                 }
309                                 essential_prev = 0;
310                         } else {
311                                 node_id = j;
312                                 essential_prev = essential_memory[j];
313
314                                 if (essential_memory[j] < hugepage_sz)
315                                         essential_memory[j] = 0;
316                                 else
317                                         essential_memory[j] -= hugepage_sz;
318                         }
319
320                         RTE_LOG(DEBUG, EAL,
321                                 "Setting policy MPOL_PREFERRED for socket %d\n",
322                                 node_id);
323                         numa_set_preferred(node_id);
324                 }
325 #endif
326
327                 hf->file_id = i;
328                 hf->size = hugepage_sz;
329                 eal_get_hugefile_path(hf->filepath, sizeof(hf->filepath),
330                                 hpi->hugedir, hf->file_id);
331                 hf->filepath[sizeof(hf->filepath) - 1] = '\0';
332
333                 /* try to create hugepage file */
334                 fd = open(hf->filepath, O_CREAT | O_RDWR, 0600);
335                 if (fd < 0) {
336                         RTE_LOG(DEBUG, EAL, "%s(): open failed: %s\n", __func__,
337                                         strerror(errno));
338                         goto out;
339                 }
340
341                 /* map the segment, and populate page tables,
342                  * the kernel fills this segment with zeros. we don't care where
343                  * this gets mapped - we already have contiguous memory areas
344                  * ready for us to map into.
345                  */
346                 virtaddr = mmap(NULL, hugepage_sz, PROT_READ | PROT_WRITE,
347                                 MAP_SHARED | MAP_POPULATE, fd, 0);
348                 if (virtaddr == MAP_FAILED) {
349                         RTE_LOG(DEBUG, EAL, "%s(): mmap failed: %s\n", __func__,
350                                         strerror(errno));
351                         close(fd);
352                         goto out;
353                 }
354
355                 hf->orig_va = virtaddr;
356
357                 /* In linux, hugetlb limitations, like cgroup, are
358                  * enforced at fault time instead of mmap(), even
359                  * with the option of MAP_POPULATE. Kernel will send
360                  * a SIGBUS signal. To avoid to be killed, save stack
361                  * environment here, if SIGBUS happens, we can jump
362                  * back here.
363                  */
364                 if (huge_wrap_sigsetjmp()) {
365                         RTE_LOG(DEBUG, EAL, "SIGBUS: Cannot mmap more "
366                                 "hugepages of size %u MB\n",
367                                 (unsigned int)(hugepage_sz / 0x100000));
368                         munmap(virtaddr, hugepage_sz);
369                         close(fd);
370                         unlink(hugepg_tbl[i].filepath);
371 #ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
372                         if (maxnode)
373                                 essential_memory[node_id] =
374                                         essential_prev;
375 #endif
376                         goto out;
377                 }
378                 *(int *)virtaddr = 0;
379
380
381                 /* set shared lock on the file. */
382                 lck.l_type = F_RDLCK;
383                 lck.l_whence = SEEK_SET;
384                 lck.l_start = 0;
385                 lck.l_len = hugepage_sz;
386                 if (fcntl(fd, F_SETLK, &lck) == -1) {
387                         RTE_LOG(DEBUG, EAL, "%s(): Locking file failed:%s \n",
388                                 __func__, strerror(errno));
389                         close(fd);
390                         goto out;
391                 }
392
393                 close(fd);
394         }
395
396 out:
397 #ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
398         if (maxnode) {
399                 RTE_LOG(DEBUG, EAL,
400                         "Restoring previous memory policy: %d\n", oldpolicy);
401                 if (oldpolicy == MPOL_DEFAULT) {
402                         numa_set_localalloc();
403                 } else if (set_mempolicy(oldpolicy, oldmask->maskp,
404                                          oldmask->size + 1) < 0) {
405                         RTE_LOG(ERR, EAL, "Failed to restore mempolicy: %s\n",
406                                 strerror(errno));
407                         numa_set_localalloc();
408                 }
409         }
410         numa_free_cpumask(oldmask);
411 #endif
412         return i;
413 }
414
415 /*
416  * Parse /proc/self/numa_maps to get the NUMA socket ID for each huge
417  * page.
418  */
419 static int
420 find_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, internal_config.hugefile_prefix);
439
440         /* parse numa map */
441         while (fgets(buf, sizeof(buf), f) != NULL) {
442
443                 /* ignore non huge page */
444                 if (strstr(buf, " huge ") == NULL &&
445                                 strstr(buf, hugedir_str) == NULL)
446                         continue;
447
448                 /* get zone addr */
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                 /* get node id (socket id) */
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                 /* if we find this page in our mappings, set socket_id */
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
497 error:
498         fclose(f);
499         return -1;
500 }
501
502 static int
503 cmp_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         /* PowerPC needs memory sorted in reverse order from x86 */
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  * Uses mmap to create a shared memory area for storage of data
523  * Used in this file to store the hugepage file map on disk
524  */
525 static void *
526 create_shared_memory(const char *filename, const size_t mem_size)
527 {
528         void *retval;
529         int fd = open(filename, O_CREAT | O_RDWR, 0666);
530         if (fd < 0)
531                 return NULL;
532         if (ftruncate(fd, mem_size) < 0) {
533                 close(fd);
534                 return NULL;
535         }
536         retval = mmap(NULL, mem_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
537         close(fd);
538         if (retval == MAP_FAILED)
539                 return NULL;
540         return retval;
541 }
542
543 /*
544  * this copies *active* hugepages from one hugepage table to another.
545  * destination is typically the shared memory.
546  */
547 static int
548 copy_hugepages_to_shared_mem(struct hugepage_file * dst, int dest_size,
549                 const struct hugepage_file * src, int src_size)
550 {
551         int src_pos, dst_pos = 0;
552
553         for (src_pos = 0; src_pos < src_size; src_pos++) {
554                 if (src[src_pos].orig_va != NULL) {
555                         /* error on overflow attempt */
556                         if (dst_pos == dest_size)
557                                 return -1;
558                         memcpy(&dst[dst_pos], &src[src_pos], sizeof(struct hugepage_file));
559                         dst_pos++;
560                 }
561         }
562         return 0;
563 }
564
565 static int
566 unlink_hugepage_files(struct hugepage_file *hugepg_tbl,
567                 unsigned num_hp_info)
568 {
569         unsigned socket, size;
570         int page, nrpages = 0;
571
572         /* get total number of hugepages */
573         for (size = 0; size < num_hp_info; size++)
574                 for (socket = 0; socket < RTE_MAX_NUMA_NODES; socket++)
575                         nrpages +=
576                         internal_config.hugepage_info[size].num_pages[socket];
577
578         for (page = 0; page < nrpages; page++) {
579                 struct hugepage_file *hp = &hugepg_tbl[page];
580
581                 if (hp->final_va != NULL && unlink(hp->filepath)) {
582                         RTE_LOG(WARNING, EAL, "%s(): Removing %s failed: %s\n",
583                                 __func__, hp->filepath, strerror(errno));
584                 }
585         }
586         return 0;
587 }
588
589 /*
590  * unmaps hugepages that are not going to be used. since we originally allocate
591  * ALL hugepages (not just those we need), additional unmapping needs to be done.
592  */
593 static int
594 unmap_unneeded_hugepages(struct hugepage_file *hugepg_tbl,
595                 struct hugepage_info *hpi,
596                 unsigned num_hp_info)
597 {
598         unsigned socket, size;
599         int page, nrpages = 0;
600
601         /* get total number of hugepages */
602         for (size = 0; size < num_hp_info; size++)
603                 for (socket = 0; socket < RTE_MAX_NUMA_NODES; socket++)
604                         nrpages += internal_config.hugepage_info[size].num_pages[socket];
605
606         for (size = 0; size < num_hp_info; size++) {
607                 for (socket = 0; socket < RTE_MAX_NUMA_NODES; socket++) {
608                         unsigned pages_found = 0;
609
610                         /* traverse until we have unmapped all the unused pages */
611                         for (page = 0; page < nrpages; page++) {
612                                 struct hugepage_file *hp = &hugepg_tbl[page];
613
614                                 /* find a page that matches the criteria */
615                                 if ((hp->size == hpi[size].hugepage_sz) &&
616                                                 (hp->socket_id == (int) socket)) {
617
618                                         /* if we skipped enough pages, unmap the rest */
619                                         if (pages_found == hpi[size].num_pages[socket]) {
620                                                 uint64_t unmap_len;
621
622                                                 unmap_len = hp->size;
623
624                                                 /* get start addr and len of the remaining segment */
625                                                 munmap(hp->orig_va,
626                                                         (size_t)unmap_len);
627
628                                                 hp->orig_va = NULL;
629                                                 if (unlink(hp->filepath) == -1) {
630                                                         RTE_LOG(ERR, EAL, "%s(): Removing %s failed: %s\n",
631                                                                         __func__, hp->filepath, strerror(errno));
632                                                         return -1;
633                                                 }
634                                         } else {
635                                                 /* lock the page and skip */
636                                                 pages_found++;
637                                         }
638
639                                 } /* match page */
640                         } /* foreach page */
641                 } /* foreach socket */
642         } /* foreach pagesize */
643
644         return 0;
645 }
646
647 static int
648 remap_segment(struct hugepage_file *hugepages, int seg_start, int seg_end)
649 {
650         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
651         struct rte_memseg_list *msl;
652         struct rte_fbarray *arr;
653         int cur_page, seg_len;
654         unsigned int msl_idx;
655         int ms_idx;
656         uint64_t page_sz;
657         size_t memseg_len;
658         int socket_id;
659
660         page_sz = hugepages[seg_start].size;
661         socket_id = hugepages[seg_start].socket_id;
662         seg_len = seg_end - seg_start;
663
664         RTE_LOG(DEBUG, EAL, "Attempting to map %" PRIu64 "M on socket %i\n",
665                         (seg_len * page_sz) >> 20ULL, socket_id);
666
667         /* find free space in memseg lists */
668         for (msl_idx = 0; msl_idx < RTE_MAX_MEMSEG_LISTS; msl_idx++) {
669                 bool empty;
670                 msl = &mcfg->memsegs[msl_idx];
671                 arr = &msl->memseg_arr;
672
673                 if (msl->page_sz != page_sz)
674                         continue;
675                 if (msl->socket_id != socket_id)
676                         continue;
677
678                 /* leave space for a hole if array is not empty */
679                 empty = arr->count == 0;
680                 ms_idx = rte_fbarray_find_next_n_free(arr, 0,
681                                 seg_len + (empty ? 0 : 1));
682
683                 /* memseg list is full? */
684                 if (ms_idx < 0)
685                         continue;
686
687                 /* leave some space between memsegs, they are not IOVA
688                  * contiguous, so they shouldn't be VA contiguous either.
689                  */
690                 if (!empty)
691                         ms_idx++;
692                 break;
693         }
694         if (msl_idx == RTE_MAX_MEMSEG_LISTS) {
695                 RTE_LOG(ERR, EAL, "Could not find space for memseg. Please increase %s and/or %s in configuration.\n",
696                                 RTE_STR(CONFIG_RTE_MAX_MEMSEG_PER_TYPE),
697                                 RTE_STR(CONFIG_RTE_MAX_MEM_PER_TYPE));
698                 return -1;
699         }
700
701 #ifdef RTE_ARCH_PPC64
702         /* for PPC64 we go through the list backwards */
703         for (cur_page = seg_end - 1; cur_page >= seg_start;
704                         cur_page--, ms_idx++) {
705 #else
706         for (cur_page = seg_start; cur_page < seg_end; cur_page++, ms_idx++) {
707 #endif
708                 struct hugepage_file *hfile = &hugepages[cur_page];
709                 struct rte_memseg *ms = rte_fbarray_get(arr, ms_idx);
710                 struct flock lck;
711                 void *addr;
712                 int fd;
713
714                 fd = open(hfile->filepath, O_RDWR);
715                 if (fd < 0) {
716                         RTE_LOG(ERR, EAL, "Could not open '%s': %s\n",
717                                         hfile->filepath, strerror(errno));
718                         return -1;
719                 }
720                 /* set shared lock on the file. */
721                 lck.l_type = F_RDLCK;
722                 lck.l_whence = SEEK_SET;
723                 lck.l_start = 0;
724                 lck.l_len = page_sz;
725                 if (fcntl(fd, F_SETLK, &lck) == -1) {
726                         RTE_LOG(DEBUG, EAL, "Could not lock '%s': %s\n",
727                                         hfile->filepath, strerror(errno));
728                         close(fd);
729                         return -1;
730                 }
731                 memseg_len = (size_t)page_sz;
732                 addr = RTE_PTR_ADD(msl->base_va, ms_idx * memseg_len);
733
734                 /* we know this address is already mmapped by memseg list, so
735                  * using MAP_FIXED here is safe
736                  */
737                 addr = mmap(addr, page_sz, PROT_READ | PROT_WRITE,
738                                 MAP_SHARED | MAP_POPULATE | MAP_FIXED, fd, 0);
739                 if (addr == MAP_FAILED) {
740                         RTE_LOG(ERR, EAL, "Couldn't remap '%s': %s\n",
741                                         hfile->filepath, strerror(errno));
742                         close(fd);
743                         return -1;
744                 }
745
746                 /* we have a new address, so unmap previous one */
747 #ifndef RTE_ARCH_64
748                 /* in 32-bit legacy mode, we have already unmapped the page */
749                 if (!internal_config.legacy_mem)
750                         munmap(hfile->orig_va, page_sz);
751 #else
752                 munmap(hfile->orig_va, page_sz);
753 #endif
754
755                 hfile->orig_va = NULL;
756                 hfile->final_va = addr;
757
758                 /* rewrite physical addresses in IOVA as VA mode */
759                 if (rte_eal_iova_mode() == RTE_IOVA_VA)
760                         hfile->physaddr = (uintptr_t)addr;
761
762                 /* set up memseg data */
763                 ms->addr = addr;
764                 ms->hugepage_sz = page_sz;
765                 ms->len = memseg_len;
766                 ms->iova = hfile->physaddr;
767                 ms->socket_id = hfile->socket_id;
768                 ms->nchannel = rte_memory_get_nchannel();
769                 ms->nrank = rte_memory_get_nrank();
770
771                 rte_fbarray_set_used(arr, ms_idx);
772
773                 close(fd);
774         }
775         RTE_LOG(DEBUG, EAL, "Allocated %" PRIu64 "M on socket %i\n",
776                         (seg_len * page_sz) >> 20, socket_id);
777         return 0;
778 }
779
780 #define MEMSEG_LIST_FMT "memseg-%" PRIu64 "k-%i-%i"
781 static int
782 alloc_memseg_list(struct rte_memseg_list *msl, uint64_t page_sz,
783                 int n_segs, int socket_id, int type_msl_idx)
784 {
785         char name[RTE_FBARRAY_NAME_LEN];
786
787         snprintf(name, sizeof(name), MEMSEG_LIST_FMT, page_sz >> 10, socket_id,
788                  type_msl_idx);
789         if (rte_fbarray_init(&msl->memseg_arr, name, n_segs,
790                         sizeof(struct rte_memseg))) {
791                 RTE_LOG(ERR, EAL, "Cannot allocate memseg list: %s\n",
792                         rte_strerror(rte_errno));
793                 return -1;
794         }
795
796         msl->page_sz = page_sz;
797         msl->socket_id = socket_id;
798         msl->base_va = NULL;
799
800         RTE_LOG(DEBUG, EAL, "Memseg list allocated: 0x%zxkB at socket %i\n",
801                         (size_t)page_sz >> 10, socket_id);
802
803         return 0;
804 }
805
806 static int
807 alloc_va_space(struct rte_memseg_list *msl)
808 {
809         uint64_t page_sz;
810         size_t mem_sz;
811         void *addr;
812         int flags = 0;
813
814 #ifdef RTE_ARCH_PPC_64
815         flags |= MAP_HUGETLB;
816 #endif
817
818         page_sz = msl->page_sz;
819         mem_sz = page_sz * msl->memseg_arr.len;
820
821         addr = eal_get_virtual_area(msl->base_va, &mem_sz, page_sz, 0, flags);
822         if (addr == NULL) {
823                 if (rte_errno == EADDRNOTAVAIL)
824                         RTE_LOG(ERR, EAL, "Could not mmap %llu bytes at [%p] - please use '--base-virtaddr' option\n",
825                                 (unsigned long long)mem_sz, msl->base_va);
826                 else
827                         RTE_LOG(ERR, EAL, "Cannot reserve memory\n");
828                 return -1;
829         }
830         msl->base_va = addr;
831
832         return 0;
833 }
834
835 /*
836  * Our VA space is not preallocated yet, so preallocate it here. We need to know
837  * how many segments there are in order to map all pages into one address space,
838  * and leave appropriate holes between segments so that rte_malloc does not
839  * concatenate them into one big segment.
840  *
841  * we also need to unmap original pages to free up address space.
842  */
843 static int __rte_unused
844 prealloc_segments(struct hugepage_file *hugepages, int n_pages)
845 {
846         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
847         int cur_page, seg_start_page, end_seg, new_memseg;
848         unsigned int hpi_idx, socket, i;
849         int n_contig_segs, n_segs;
850         int msl_idx;
851
852         /* before we preallocate segments, we need to free up our VA space.
853          * we're not removing files, and we already have information about
854          * PA-contiguousness, so it is safe to unmap everything.
855          */
856         for (cur_page = 0; cur_page < n_pages; cur_page++) {
857                 struct hugepage_file *hpi = &hugepages[cur_page];
858                 munmap(hpi->orig_va, hpi->size);
859                 hpi->orig_va = NULL;
860         }
861
862         /* we cannot know how many page sizes and sockets we have discovered, so
863          * loop over all of them
864          */
865         for (hpi_idx = 0; hpi_idx < internal_config.num_hugepage_sizes;
866                         hpi_idx++) {
867                 uint64_t page_sz =
868                         internal_config.hugepage_info[hpi_idx].hugepage_sz;
869
870                 for (i = 0; i < rte_socket_count(); i++) {
871                         struct rte_memseg_list *msl;
872
873                         socket = rte_socket_id_by_idx(i);
874                         n_contig_segs = 0;
875                         n_segs = 0;
876                         seg_start_page = -1;
877
878                         for (cur_page = 0; cur_page < n_pages; cur_page++) {
879                                 struct hugepage_file *prev, *cur;
880                                 int prev_seg_start_page = -1;
881
882                                 cur = &hugepages[cur_page];
883                                 prev = cur_page == 0 ? NULL :
884                                                 &hugepages[cur_page - 1];
885
886                                 new_memseg = 0;
887                                 end_seg = 0;
888
889                                 if (cur->size == 0)
890                                         end_seg = 1;
891                                 else if (cur->socket_id != (int) socket)
892                                         end_seg = 1;
893                                 else if (cur->size != page_sz)
894                                         end_seg = 1;
895                                 else if (cur_page == 0)
896                                         new_memseg = 1;
897 #ifdef RTE_ARCH_PPC_64
898                                 /* On PPC64 architecture, the mmap always start
899                                  * from higher address to lower address. Here,
900                                  * physical addresses are in descending order.
901                                  */
902                                 else if ((prev->physaddr - cur->physaddr) !=
903                                                 cur->size)
904                                         new_memseg = 1;
905 #else
906                                 else if ((cur->physaddr - prev->physaddr) !=
907                                                 cur->size)
908                                         new_memseg = 1;
909 #endif
910                                 if (new_memseg) {
911                                         /* if we're already inside a segment,
912                                          * new segment means end of current one
913                                          */
914                                         if (seg_start_page != -1) {
915                                                 end_seg = 1;
916                                                 prev_seg_start_page =
917                                                                 seg_start_page;
918                                         }
919                                         seg_start_page = cur_page;
920                                 }
921
922                                 if (end_seg) {
923                                         if (prev_seg_start_page != -1) {
924                                                 /* we've found a new segment */
925                                                 n_contig_segs++;
926                                                 n_segs += cur_page -
927                                                         prev_seg_start_page;
928                                         } else if (seg_start_page != -1) {
929                                                 /* we didn't find new segment,
930                                                  * but did end current one
931                                                  */
932                                                 n_contig_segs++;
933                                                 n_segs += cur_page -
934                                                                 seg_start_page;
935                                                 seg_start_page = -1;
936                                                 continue;
937                                         } else {
938                                                 /* we're skipping this page */
939                                                 continue;
940                                         }
941                                 }
942                                 /* segment continues */
943                         }
944                         /* check if we missed last segment */
945                         if (seg_start_page != -1) {
946                                 n_contig_segs++;
947                                 n_segs += cur_page - seg_start_page;
948                         }
949
950                         /* if no segments were found, do not preallocate */
951                         if (n_segs == 0)
952                                 continue;
953
954                         /* we now have total number of pages that we will
955                          * allocate for this segment list. add separator pages
956                          * to the total count, and preallocate VA space.
957                          */
958                         n_segs += n_contig_segs - 1;
959
960                         /* now, preallocate VA space for these segments */
961
962                         /* first, find suitable memseg list for this */
963                         for (msl_idx = 0; msl_idx < RTE_MAX_MEMSEG_LISTS;
964                                         msl_idx++) {
965                                 msl = &mcfg->memsegs[msl_idx];
966
967                                 if (msl->base_va != NULL)
968                                         continue;
969                                 break;
970                         }
971                         if (msl_idx == RTE_MAX_MEMSEG_LISTS) {
972                                 RTE_LOG(ERR, EAL, "Not enough space in memseg lists, please increase %s\n",
973                                         RTE_STR(CONFIG_RTE_MAX_MEMSEG_LISTS));
974                                 return -1;
975                         }
976
977                         /* now, allocate fbarray itself */
978                         if (alloc_memseg_list(msl, page_sz, n_segs, socket,
979                                                 msl_idx) < 0)
980                                 return -1;
981
982                         /* finally, allocate VA space */
983                         if (alloc_va_space(msl) < 0)
984                                 return -1;
985                 }
986         }
987         return 0;
988 }
989
990 /*
991  * We cannot reallocate memseg lists on the fly because PPC64 stores pages
992  * backwards, therefore we have to process the entire memseg first before
993  * remapping it into memseg list VA space.
994  */
995 static int
996 remap_needed_hugepages(struct hugepage_file *hugepages, int n_pages)
997 {
998         int cur_page, seg_start_page, new_memseg, ret;
999
1000         seg_start_page = 0;
1001         for (cur_page = 0; cur_page < n_pages; cur_page++) {
1002                 struct hugepage_file *prev, *cur;
1003
1004                 new_memseg = 0;
1005
1006                 cur = &hugepages[cur_page];
1007                 prev = cur_page == 0 ? NULL : &hugepages[cur_page - 1];
1008
1009                 /* if size is zero, no more pages left */
1010                 if (cur->size == 0)
1011                         break;
1012
1013                 if (cur_page == 0)
1014                         new_memseg = 1;
1015                 else if (cur->socket_id != prev->socket_id)
1016                         new_memseg = 1;
1017                 else if (cur->size != prev->size)
1018                         new_memseg = 1;
1019 #ifdef RTE_ARCH_PPC_64
1020                 /* On PPC64 architecture, the mmap always start from higher
1021                  * address to lower address. Here, physical addresses are in
1022                  * descending order.
1023                  */
1024                 else if ((prev->physaddr - cur->physaddr) != cur->size)
1025                         new_memseg = 1;
1026 #else
1027                 else if ((cur->physaddr - prev->physaddr) != cur->size)
1028                         new_memseg = 1;
1029 #endif
1030
1031                 if (new_memseg) {
1032                         /* if this isn't the first time, remap segment */
1033                         if (cur_page != 0) {
1034                                 ret = remap_segment(hugepages, seg_start_page,
1035                                                 cur_page);
1036                                 if (ret != 0)
1037                                         return -1;
1038                         }
1039                         /* remember where we started */
1040                         seg_start_page = cur_page;
1041                 }
1042                 /* continuation of previous memseg */
1043         }
1044         /* we were stopped, but we didn't remap the last segment, do it now */
1045         if (cur_page != 0) {
1046                 ret = remap_segment(hugepages, seg_start_page,
1047                                 cur_page);
1048                 if (ret != 0)
1049                         return -1;
1050         }
1051         return 0;
1052 }
1053
1054 static inline uint64_t
1055 get_socket_mem_size(int socket)
1056 {
1057         uint64_t size = 0;
1058         unsigned i;
1059
1060         for (i = 0; i < internal_config.num_hugepage_sizes; i++){
1061                 struct hugepage_info *hpi = &internal_config.hugepage_info[i];
1062                 if (hpi->hugedir != NULL)
1063                         size += hpi->hugepage_sz * hpi->num_pages[socket];
1064         }
1065
1066         return size;
1067 }
1068
1069 /*
1070  * This function is a NUMA-aware equivalent of calc_num_pages.
1071  * It takes in the list of hugepage sizes and the
1072  * number of pages thereof, and calculates the best number of
1073  * pages of each size to fulfill the request for <memory> ram
1074  */
1075 static int
1076 calc_num_pages_per_socket(uint64_t * memory,
1077                 struct hugepage_info *hp_info,
1078                 struct hugepage_info *hp_used,
1079                 unsigned num_hp_info)
1080 {
1081         unsigned socket, j, i = 0;
1082         unsigned requested, available;
1083         int total_num_pages = 0;
1084         uint64_t remaining_mem, cur_mem;
1085         uint64_t total_mem = internal_config.memory;
1086
1087         if (num_hp_info == 0)
1088                 return -1;
1089
1090         /* if specific memory amounts per socket weren't requested */
1091         if (internal_config.force_sockets == 0) {
1092                 size_t total_size;
1093 #ifdef RTE_ARCH_64
1094                 int cpu_per_socket[RTE_MAX_NUMA_NODES];
1095                 size_t default_size;
1096                 unsigned lcore_id;
1097
1098                 /* Compute number of cores per socket */
1099                 memset(cpu_per_socket, 0, sizeof(cpu_per_socket));
1100                 RTE_LCORE_FOREACH(lcore_id) {
1101                         cpu_per_socket[rte_lcore_to_socket_id(lcore_id)]++;
1102                 }
1103
1104                 /*
1105                  * Automatically spread requested memory amongst detected sockets according
1106                  * to number of cores from cpu mask present on each socket
1107                  */
1108                 total_size = internal_config.memory;
1109                 for (socket = 0; socket < RTE_MAX_NUMA_NODES && total_size != 0; socket++) {
1110
1111                         /* Set memory amount per socket */
1112                         default_size = (internal_config.memory * cpu_per_socket[socket])
1113                                         / rte_lcore_count();
1114
1115                         /* Limit to maximum available memory on socket */
1116                         default_size = RTE_MIN(default_size, get_socket_mem_size(socket));
1117
1118                         /* Update sizes */
1119                         memory[socket] = default_size;
1120                         total_size -= default_size;
1121                 }
1122
1123                 /*
1124                  * If some memory is remaining, try to allocate it by getting all
1125                  * available memory from sockets, one after the other
1126                  */
1127                 for (socket = 0; socket < RTE_MAX_NUMA_NODES && total_size != 0; socket++) {
1128                         /* take whatever is available */
1129                         default_size = RTE_MIN(get_socket_mem_size(socket) - memory[socket],
1130                                                total_size);
1131
1132                         /* Update sizes */
1133                         memory[socket] += default_size;
1134                         total_size -= default_size;
1135                 }
1136 #else
1137                 /* in 32-bit mode, allocate all of the memory only on master
1138                  * lcore socket
1139                  */
1140                 total_size = internal_config.memory;
1141                 for (socket = 0; socket < RTE_MAX_NUMA_NODES && total_size != 0;
1142                                 socket++) {
1143                         struct rte_config *cfg = rte_eal_get_configuration();
1144                         unsigned int master_lcore_socket;
1145
1146                         master_lcore_socket =
1147                                 rte_lcore_to_socket_id(cfg->master_lcore);
1148
1149                         if (master_lcore_socket != socket)
1150                                 continue;
1151
1152                         /* Update sizes */
1153                         memory[socket] = total_size;
1154                         break;
1155                 }
1156 #endif
1157         }
1158
1159         for (socket = 0; socket < RTE_MAX_NUMA_NODES && total_mem != 0; socket++) {
1160                 /* skips if the memory on specific socket wasn't requested */
1161                 for (i = 0; i < num_hp_info && memory[socket] != 0; i++){
1162                         hp_used[i].hugedir = hp_info[i].hugedir;
1163                         hp_used[i].num_pages[socket] = RTE_MIN(
1164                                         memory[socket] / hp_info[i].hugepage_sz,
1165                                         hp_info[i].num_pages[socket]);
1166
1167                         cur_mem = hp_used[i].num_pages[socket] *
1168                                         hp_used[i].hugepage_sz;
1169
1170                         memory[socket] -= cur_mem;
1171                         total_mem -= cur_mem;
1172
1173                         total_num_pages += hp_used[i].num_pages[socket];
1174
1175                         /* check if we have met all memory requests */
1176                         if (memory[socket] == 0)
1177                                 break;
1178
1179                         /* check if we have any more pages left at this size, if so
1180                          * move on to next size */
1181                         if (hp_used[i].num_pages[socket] == hp_info[i].num_pages[socket])
1182                                 continue;
1183                         /* At this point we know that there are more pages available that are
1184                          * bigger than the memory we want, so lets see if we can get enough
1185                          * from other page sizes.
1186                          */
1187                         remaining_mem = 0;
1188                         for (j = i+1; j < num_hp_info; j++)
1189                                 remaining_mem += hp_info[j].hugepage_sz *
1190                                 hp_info[j].num_pages[socket];
1191
1192                         /* is there enough other memory, if not allocate another page and quit */
1193                         if (remaining_mem < memory[socket]){
1194                                 cur_mem = RTE_MIN(memory[socket],
1195                                                 hp_info[i].hugepage_sz);
1196                                 memory[socket] -= cur_mem;
1197                                 total_mem -= cur_mem;
1198                                 hp_used[i].num_pages[socket]++;
1199                                 total_num_pages++;
1200                                 break; /* we are done with this socket*/
1201                         }
1202                 }
1203                 /* if we didn't satisfy all memory requirements per socket */
1204                 if (memory[socket] > 0 &&
1205                                 internal_config.socket_mem[socket] != 0) {
1206                         /* to prevent icc errors */
1207                         requested = (unsigned) (internal_config.socket_mem[socket] /
1208                                         0x100000);
1209                         available = requested -
1210                                         ((unsigned) (memory[socket] / 0x100000));
1211                         RTE_LOG(ERR, EAL, "Not enough memory available on socket %u! "
1212                                         "Requested: %uMB, available: %uMB\n", socket,
1213                                         requested, available);
1214                         return -1;
1215                 }
1216         }
1217
1218         /* if we didn't satisfy total memory requirements */
1219         if (total_mem > 0) {
1220                 requested = (unsigned) (internal_config.memory / 0x100000);
1221                 available = requested - (unsigned) (total_mem / 0x100000);
1222                 RTE_LOG(ERR, EAL, "Not enough memory available! Requested: %uMB,"
1223                                 " available: %uMB\n", requested, available);
1224                 return -1;
1225         }
1226         return total_num_pages;
1227 }
1228
1229 static inline size_t
1230 eal_get_hugepage_mem_size(void)
1231 {
1232         uint64_t size = 0;
1233         unsigned i, j;
1234
1235         for (i = 0; i < internal_config.num_hugepage_sizes; i++) {
1236                 struct hugepage_info *hpi = &internal_config.hugepage_info[i];
1237                 if (hpi->hugedir != NULL) {
1238                         for (j = 0; j < RTE_MAX_NUMA_NODES; j++) {
1239                                 size += hpi->hugepage_sz * hpi->num_pages[j];
1240                         }
1241                 }
1242         }
1243
1244         return (size < SIZE_MAX) ? (size_t)(size) : SIZE_MAX;
1245 }
1246
1247 static struct sigaction huge_action_old;
1248 static int huge_need_recover;
1249
1250 static void
1251 huge_register_sigbus(void)
1252 {
1253         sigset_t mask;
1254         struct sigaction action;
1255
1256         sigemptyset(&mask);
1257         sigaddset(&mask, SIGBUS);
1258         action.sa_flags = 0;
1259         action.sa_mask = mask;
1260         action.sa_handler = huge_sigbus_handler;
1261
1262         huge_need_recover = !sigaction(SIGBUS, &action, &huge_action_old);
1263 }
1264
1265 static void
1266 huge_recover_sigbus(void)
1267 {
1268         if (huge_need_recover) {
1269                 sigaction(SIGBUS, &huge_action_old, NULL);
1270                 huge_need_recover = 0;
1271         }
1272 }
1273
1274 /*
1275  * Prepare physical memory mapping: fill configuration structure with
1276  * these infos, return 0 on success.
1277  *  1. map N huge pages in separate files in hugetlbfs
1278  *  2. find associated physical addr
1279  *  3. find associated NUMA socket ID
1280  *  4. sort all huge pages by physical address
1281  *  5. remap these N huge pages in the correct order
1282  *  6. unmap the first mapping
1283  *  7. fill memsegs in configuration with contiguous zones
1284  */
1285 static int
1286 eal_legacy_hugepage_init(void)
1287 {
1288         struct rte_mem_config *mcfg;
1289         struct hugepage_file *hugepage = NULL, *tmp_hp = NULL;
1290         struct hugepage_info used_hp[MAX_HUGEPAGE_SIZES];
1291         struct rte_fbarray *arr;
1292         struct rte_memseg *ms;
1293
1294         uint64_t memory[RTE_MAX_NUMA_NODES];
1295
1296         unsigned hp_offset;
1297         int i, j;
1298         int nr_hugefiles, nr_hugepages = 0;
1299         void *addr;
1300
1301         test_phys_addrs_available();
1302
1303         memset(used_hp, 0, sizeof(used_hp));
1304
1305         /* get pointer to global configuration */
1306         mcfg = rte_eal_get_configuration()->mem_config;
1307
1308         /* hugetlbfs can be disabled */
1309         if (internal_config.no_hugetlbfs) {
1310                 struct rte_memseg_list *msl;
1311                 uint64_t page_sz;
1312                 int n_segs, cur_seg;
1313
1314                 /* nohuge mode is legacy mode */
1315                 internal_config.legacy_mem = 1;
1316
1317                 /* create a memseg list */
1318                 msl = &mcfg->memsegs[0];
1319
1320                 page_sz = RTE_PGSIZE_4K;
1321                 n_segs = internal_config.memory / page_sz;
1322
1323                 if (rte_fbarray_init(&msl->memseg_arr, "nohugemem", n_segs,
1324                                         sizeof(struct rte_memseg))) {
1325                         RTE_LOG(ERR, EAL, "Cannot allocate memseg list\n");
1326                         return -1;
1327                 }
1328
1329                 addr = mmap(NULL, internal_config.memory, PROT_READ | PROT_WRITE,
1330                                 MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
1331                 if (addr == MAP_FAILED) {
1332                         RTE_LOG(ERR, EAL, "%s: mmap() failed: %s\n", __func__,
1333                                         strerror(errno));
1334                         return -1;
1335                 }
1336                 msl->base_va = addr;
1337                 msl->page_sz = page_sz;
1338                 msl->socket_id = 0;
1339
1340                 /* populate memsegs. each memseg is one page long */
1341                 for (cur_seg = 0; cur_seg < n_segs; cur_seg++) {
1342                         arr = &msl->memseg_arr;
1343
1344                         ms = rte_fbarray_get(arr, cur_seg);
1345                         if (rte_eal_iova_mode() == RTE_IOVA_VA)
1346                                 ms->iova = (uintptr_t)addr;
1347                         else
1348                                 ms->iova = RTE_BAD_IOVA;
1349                         ms->addr = addr;
1350                         ms->hugepage_sz = page_sz;
1351                         ms->socket_id = 0;
1352                         ms->len = page_sz;
1353
1354                         rte_fbarray_set_used(arr, cur_seg);
1355
1356                         addr = RTE_PTR_ADD(addr, (size_t)page_sz);
1357                 }
1358                 return 0;
1359         }
1360
1361         /* calculate total number of hugepages available. at this point we haven't
1362          * yet started sorting them so they all are on socket 0 */
1363         for (i = 0; i < (int) internal_config.num_hugepage_sizes; i++) {
1364                 /* meanwhile, also initialize used_hp hugepage sizes in used_hp */
1365                 used_hp[i].hugepage_sz = internal_config.hugepage_info[i].hugepage_sz;
1366
1367                 nr_hugepages += internal_config.hugepage_info[i].num_pages[0];
1368         }
1369
1370         /*
1371          * allocate a memory area for hugepage table.
1372          * this isn't shared memory yet. due to the fact that we need some
1373          * processing done on these pages, shared memory will be created
1374          * at a later stage.
1375          */
1376         tmp_hp = malloc(nr_hugepages * sizeof(struct hugepage_file));
1377         if (tmp_hp == NULL)
1378                 goto fail;
1379
1380         memset(tmp_hp, 0, nr_hugepages * sizeof(struct hugepage_file));
1381
1382         hp_offset = 0; /* where we start the current page size entries */
1383
1384         huge_register_sigbus();
1385
1386         /* make a copy of socket_mem, needed for balanced allocation. */
1387         for (i = 0; i < RTE_MAX_NUMA_NODES; i++)
1388                 memory[i] = internal_config.socket_mem[i];
1389
1390         /* map all hugepages and sort them */
1391         for (i = 0; i < (int)internal_config.num_hugepage_sizes; i ++){
1392                 unsigned pages_old, pages_new;
1393                 struct hugepage_info *hpi;
1394
1395                 /*
1396                  * we don't yet mark hugepages as used at this stage, so
1397                  * we just map all hugepages available to the system
1398                  * all hugepages are still located on socket 0
1399                  */
1400                 hpi = &internal_config.hugepage_info[i];
1401
1402                 if (hpi->num_pages[0] == 0)
1403                         continue;
1404
1405                 /* map all hugepages available */
1406                 pages_old = hpi->num_pages[0];
1407                 pages_new = map_all_hugepages(&tmp_hp[hp_offset], hpi, memory);
1408                 if (pages_new < pages_old) {
1409                         RTE_LOG(DEBUG, EAL,
1410                                 "%d not %d hugepages of size %u MB allocated\n",
1411                                 pages_new, pages_old,
1412                                 (unsigned)(hpi->hugepage_sz / 0x100000));
1413
1414                         int pages = pages_old - pages_new;
1415
1416                         nr_hugepages -= pages;
1417                         hpi->num_pages[0] = pages_new;
1418                         if (pages_new == 0)
1419                                 continue;
1420                 }
1421
1422                 if (phys_addrs_available &&
1423                                 rte_eal_iova_mode() != RTE_IOVA_VA) {
1424                         /* find physical addresses for each hugepage */
1425                         if (find_physaddrs(&tmp_hp[hp_offset], hpi) < 0) {
1426                                 RTE_LOG(DEBUG, EAL, "Failed to find phys addr "
1427                                         "for %u MB pages\n",
1428                                         (unsigned int)(hpi->hugepage_sz / 0x100000));
1429                                 goto fail;
1430                         }
1431                 } else {
1432                         /* set physical addresses for each hugepage */
1433                         if (set_physaddrs(&tmp_hp[hp_offset], hpi) < 0) {
1434                                 RTE_LOG(DEBUG, EAL, "Failed to set phys addr "
1435                                         "for %u MB pages\n",
1436                                         (unsigned int)(hpi->hugepage_sz / 0x100000));
1437                                 goto fail;
1438                         }
1439                 }
1440
1441                 if (find_numasocket(&tmp_hp[hp_offset], hpi) < 0){
1442                         RTE_LOG(DEBUG, EAL, "Failed to find NUMA socket for %u MB pages\n",
1443                                         (unsigned)(hpi->hugepage_sz / 0x100000));
1444                         goto fail;
1445                 }
1446
1447                 qsort(&tmp_hp[hp_offset], hpi->num_pages[0],
1448                       sizeof(struct hugepage_file), cmp_physaddr);
1449
1450                 /* we have processed a num of hugepages of this size, so inc offset */
1451                 hp_offset += hpi->num_pages[0];
1452         }
1453
1454         huge_recover_sigbus();
1455
1456         if (internal_config.memory == 0 && internal_config.force_sockets == 0)
1457                 internal_config.memory = eal_get_hugepage_mem_size();
1458
1459         nr_hugefiles = nr_hugepages;
1460
1461
1462         /* clean out the numbers of pages */
1463         for (i = 0; i < (int) internal_config.num_hugepage_sizes; i++)
1464                 for (j = 0; j < RTE_MAX_NUMA_NODES; j++)
1465                         internal_config.hugepage_info[i].num_pages[j] = 0;
1466
1467         /* get hugepages for each socket */
1468         for (i = 0; i < nr_hugefiles; i++) {
1469                 int socket = tmp_hp[i].socket_id;
1470
1471                 /* find a hugepage info with right size and increment num_pages */
1472                 const int nb_hpsizes = RTE_MIN(MAX_HUGEPAGE_SIZES,
1473                                 (int)internal_config.num_hugepage_sizes);
1474                 for (j = 0; j < nb_hpsizes; j++) {
1475                         if (tmp_hp[i].size ==
1476                                         internal_config.hugepage_info[j].hugepage_sz) {
1477                                 internal_config.hugepage_info[j].num_pages[socket]++;
1478                         }
1479                 }
1480         }
1481
1482         /* make a copy of socket_mem, needed for number of pages calculation */
1483         for (i = 0; i < RTE_MAX_NUMA_NODES; i++)
1484                 memory[i] = internal_config.socket_mem[i];
1485
1486         /* calculate final number of pages */
1487         nr_hugepages = calc_num_pages_per_socket(memory,
1488                         internal_config.hugepage_info, used_hp,
1489                         internal_config.num_hugepage_sizes);
1490
1491         /* error if not enough memory available */
1492         if (nr_hugepages < 0)
1493                 goto fail;
1494
1495         /* reporting in! */
1496         for (i = 0; i < (int) internal_config.num_hugepage_sizes; i++) {
1497                 for (j = 0; j < RTE_MAX_NUMA_NODES; j++) {
1498                         if (used_hp[i].num_pages[j] > 0) {
1499                                 RTE_LOG(DEBUG, EAL,
1500                                         "Requesting %u pages of size %uMB"
1501                                         " from socket %i\n",
1502                                         used_hp[i].num_pages[j],
1503                                         (unsigned)
1504                                         (used_hp[i].hugepage_sz / 0x100000),
1505                                         j);
1506                         }
1507                 }
1508         }
1509
1510         /* create shared memory */
1511         hugepage = create_shared_memory(eal_hugepage_info_path(),
1512                         nr_hugefiles * sizeof(struct hugepage_file));
1513
1514         if (hugepage == NULL) {
1515                 RTE_LOG(ERR, EAL, "Failed to create shared memory!\n");
1516                 goto fail;
1517         }
1518         memset(hugepage, 0, nr_hugefiles * sizeof(struct hugepage_file));
1519
1520         /*
1521          * unmap pages that we won't need (looks at used_hp).
1522          * also, sets final_va to NULL on pages that were unmapped.
1523          */
1524         if (unmap_unneeded_hugepages(tmp_hp, used_hp,
1525                         internal_config.num_hugepage_sizes) < 0) {
1526                 RTE_LOG(ERR, EAL, "Unmapping and locking hugepages failed!\n");
1527                 goto fail;
1528         }
1529
1530         /*
1531          * copy stuff from malloc'd hugepage* to the actual shared memory.
1532          * this procedure only copies those hugepages that have orig_va
1533          * not NULL. has overflow protection.
1534          */
1535         if (copy_hugepages_to_shared_mem(hugepage, nr_hugefiles,
1536                         tmp_hp, nr_hugefiles) < 0) {
1537                 RTE_LOG(ERR, EAL, "Copying tables to shared memory failed!\n");
1538                 goto fail;
1539         }
1540
1541 #ifndef RTE_ARCH_64
1542         /* for legacy 32-bit mode, we did not preallocate VA space, so do it */
1543         if (internal_config.legacy_mem &&
1544                         prealloc_segments(hugepage, nr_hugefiles)) {
1545                 RTE_LOG(ERR, EAL, "Could not preallocate VA space for hugepages\n");
1546                 goto fail;
1547         }
1548 #endif
1549
1550         /* remap all pages we do need into memseg list VA space, so that those
1551          * pages become first-class citizens in DPDK memory subsystem
1552          */
1553         if (remap_needed_hugepages(hugepage, nr_hugefiles)) {
1554                 RTE_LOG(ERR, EAL, "Couldn't remap hugepage files into memseg lists\n");
1555                 goto fail;
1556         }
1557
1558         /* free the hugepage backing files */
1559         if (internal_config.hugepage_unlink &&
1560                 unlink_hugepage_files(tmp_hp, internal_config.num_hugepage_sizes) < 0) {
1561                 RTE_LOG(ERR, EAL, "Unlinking hugepage files failed!\n");
1562                 goto fail;
1563         }
1564
1565         /* free the temporary hugepage table */
1566         free(tmp_hp);
1567         tmp_hp = NULL;
1568
1569         munmap(hugepage, nr_hugefiles * sizeof(struct hugepage_file));
1570
1571         /* we're not going to allocate more pages, so release VA space for
1572          * unused memseg lists
1573          */
1574         for (i = 0; i < RTE_MAX_MEMSEG_LISTS; i++) {
1575                 struct rte_memseg_list *msl = &mcfg->memsegs[i];
1576                 size_t mem_sz;
1577
1578                 /* skip inactive lists */
1579                 if (msl->base_va == NULL)
1580                         continue;
1581                 /* skip lists where there is at least one page allocated */
1582                 if (msl->memseg_arr.count > 0)
1583                         continue;
1584                 /* this is an unused list, deallocate it */
1585                 mem_sz = (size_t)msl->page_sz * msl->memseg_arr.len;
1586                 munmap(msl->base_va, mem_sz);
1587                 msl->base_va = NULL;
1588
1589                 /* destroy backing fbarray */
1590                 rte_fbarray_destroy(&msl->memseg_arr);
1591         }
1592
1593         return 0;
1594
1595 fail:
1596         huge_recover_sigbus();
1597         free(tmp_hp);
1598         if (hugepage != NULL)
1599                 munmap(hugepage, nr_hugefiles * sizeof(struct hugepage_file));
1600
1601         return -1;
1602 }
1603
1604 /*
1605  * uses fstat to report the size of a file on disk
1606  */
1607 static off_t
1608 getFileSize(int fd)
1609 {
1610         struct stat st;
1611         if (fstat(fd, &st) < 0)
1612                 return 0;
1613         return st.st_size;
1614 }
1615
1616 /*
1617  * This creates the memory mappings in the secondary process to match that of
1618  * the server process. It goes through each memory segment in the DPDK runtime
1619  * configuration and finds the hugepages which form that segment, mapping them
1620  * in order to form a contiguous block in the virtual memory space
1621  */
1622 static int
1623 eal_legacy_hugepage_attach(void)
1624 {
1625         struct hugepage_file *hp = NULL;
1626         unsigned int num_hp = 0;
1627         unsigned int i = 0;
1628         unsigned int cur_seg;
1629         off_t size = 0;
1630         int fd, fd_hugepage = -1;
1631
1632         if (aslr_enabled() > 0) {
1633                 RTE_LOG(WARNING, EAL, "WARNING: Address Space Layout Randomization "
1634                                 "(ASLR) is enabled in the kernel.\n");
1635                 RTE_LOG(WARNING, EAL, "   This may cause issues with mapping memory "
1636                                 "into secondary processes\n");
1637         }
1638
1639         test_phys_addrs_available();
1640
1641         fd_hugepage = open(eal_hugepage_info_path(), O_RDONLY);
1642         if (fd_hugepage < 0) {
1643                 RTE_LOG(ERR, EAL, "Could not open %s\n", eal_hugepage_info_path());
1644                 goto error;
1645         }
1646
1647         size = getFileSize(fd_hugepage);
1648         hp = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd_hugepage, 0);
1649         if (hp == MAP_FAILED) {
1650                 RTE_LOG(ERR, EAL, "Could not mmap %s\n", eal_hugepage_info_path());
1651                 goto error;
1652         }
1653
1654         num_hp = size / sizeof(struct hugepage_file);
1655         RTE_LOG(DEBUG, EAL, "Analysing %u files\n", num_hp);
1656
1657         /* map all segments into memory to make sure we get the addrs. the
1658          * segments themselves are already in memseg list (which is shared and
1659          * has its VA space already preallocated), so we just need to map
1660          * everything into correct addresses.
1661          */
1662         for (i = 0; i < num_hp; i++) {
1663                 struct hugepage_file *hf = &hp[i];
1664                 size_t map_sz = hf->size;
1665                 void *map_addr = hf->final_va;
1666                 struct flock lck;
1667
1668                 /* if size is zero, no more pages left */
1669                 if (map_sz == 0)
1670                         break;
1671
1672                 fd = open(hf->filepath, O_RDWR);
1673                 if (fd < 0) {
1674                         RTE_LOG(ERR, EAL, "Could not open %s: %s\n",
1675                                 hf->filepath, strerror(errno));
1676                         goto error;
1677                 }
1678
1679                 map_addr = mmap(map_addr, map_sz, PROT_READ | PROT_WRITE,
1680                                 MAP_SHARED | MAP_FIXED, fd, 0);
1681                 if (map_addr == MAP_FAILED) {
1682                         RTE_LOG(ERR, EAL, "Could not map %s: %s\n",
1683                                 hf->filepath, strerror(errno));
1684                         goto error;
1685                 }
1686
1687                 /* set shared lock on the file. */
1688                 lck.l_type = F_RDLCK;
1689                 lck.l_whence = SEEK_SET;
1690                 lck.l_start = 0;
1691                 lck.l_len = map_sz;
1692                 if (fcntl(fd, F_SETLK, &lck) == -1) {
1693                         RTE_LOG(DEBUG, EAL, "%s(): Locking file failed: %s\n",
1694                                 __func__, strerror(errno));
1695                         close(fd);
1696                         goto error;
1697                 }
1698
1699                 close(fd);
1700         }
1701         /* unmap the hugepage config file, since we are done using it */
1702         munmap(hp, size);
1703         close(fd_hugepage);
1704         return 0;
1705
1706 error:
1707         /* map all segments into memory to make sure we get the addrs */
1708         cur_seg = 0;
1709         for (cur_seg = 0; cur_seg < i; cur_seg++) {
1710                 struct hugepage_file *hf = &hp[i];
1711                 size_t map_sz = hf->size;
1712                 void *map_addr = hf->final_va;
1713
1714                 munmap(map_addr, map_sz);
1715         }
1716         if (hp != NULL && hp != MAP_FAILED)
1717                 munmap(hp, size);
1718         if (fd_hugepage >= 0)
1719                 close(fd_hugepage);
1720         return -1;
1721 }
1722
1723 int
1724 rte_eal_hugepage_init(void)
1725 {
1726         if (internal_config.legacy_mem)
1727                 return eal_legacy_hugepage_init();
1728         return -1;
1729 }
1730
1731 int
1732 rte_eal_hugepage_attach(void)
1733 {
1734         if (internal_config.legacy_mem)
1735                 return eal_legacy_hugepage_attach();
1736         return -1;
1737 }
1738
1739 int
1740 rte_eal_using_phys_addrs(void)
1741 {
1742         return phys_addrs_available;
1743 }