eal: ignore failure of naming a control thread
[dpdk.git] / lib / librte_eal / common / eal_common_memory.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4
5 #include <errno.h>
6 #include <stdio.h>
7 #include <stdint.h>
8 #include <stdlib.h>
9 #include <stdarg.h>
10 #include <string.h>
11 #include <unistd.h>
12 #include <inttypes.h>
13 #include <sys/mman.h>
14 #include <sys/queue.h>
15
16 #include <rte_fbarray.h>
17 #include <rte_memory.h>
18 #include <rte_eal.h>
19 #include <rte_eal_memconfig.h>
20 #include <rte_errno.h>
21 #include <rte_log.h>
22
23 #include "eal_memalloc.h"
24 #include "eal_private.h"
25 #include "eal_internal_cfg.h"
26
27 /*
28  * Try to mmap *size bytes in /dev/zero. If it is successful, return the
29  * pointer to the mmap'd area and keep *size unmodified. Else, retry
30  * with a smaller zone: decrease *size by hugepage_sz until it reaches
31  * 0. In this case, return NULL. Note: this function returns an address
32  * which is a multiple of hugepage size.
33  */
34
35 #define MEMSEG_LIST_FMT "memseg-%" PRIu64 "k-%i-%i"
36
37 static void *next_baseaddr;
38 static uint64_t system_page_sz;
39
40 void *
41 eal_get_virtual_area(void *requested_addr, size_t *size,
42                 size_t page_sz, int flags, int mmap_flags)
43 {
44         bool addr_is_hint, allow_shrink, unmap, no_align;
45         uint64_t map_sz;
46         void *mapped_addr, *aligned_addr;
47
48         if (system_page_sz == 0)
49                 system_page_sz = sysconf(_SC_PAGESIZE);
50
51         mmap_flags |= MAP_PRIVATE | MAP_ANONYMOUS;
52
53         RTE_LOG(DEBUG, EAL, "Ask a virtual area of 0x%zx bytes\n", *size);
54
55         addr_is_hint = (flags & EAL_VIRTUAL_AREA_ADDR_IS_HINT) > 0;
56         allow_shrink = (flags & EAL_VIRTUAL_AREA_ALLOW_SHRINK) > 0;
57         unmap = (flags & EAL_VIRTUAL_AREA_UNMAP) > 0;
58
59         if (next_baseaddr == NULL && internal_config.base_virtaddr != 0 &&
60                         rte_eal_process_type() == RTE_PROC_PRIMARY)
61                 next_baseaddr = (void *) internal_config.base_virtaddr;
62
63         if (requested_addr == NULL && next_baseaddr != NULL) {
64                 requested_addr = next_baseaddr;
65                 requested_addr = RTE_PTR_ALIGN(requested_addr, page_sz);
66                 addr_is_hint = true;
67         }
68
69         /* if requested address is not aligned by page size, or if requested
70          * address is NULL, add page size to requested length as we may get an
71          * address that's aligned by system page size, which can be smaller than
72          * our requested page size. additionally, we shouldn't try to align if
73          * system page size is the same as requested page size.
74          */
75         no_align = (requested_addr != NULL &&
76                 ((uintptr_t)requested_addr & (page_sz - 1))) ||
77                 page_sz == system_page_sz;
78
79         do {
80                 map_sz = no_align ? *size : *size + page_sz;
81                 if (map_sz > SIZE_MAX) {
82                         RTE_LOG(ERR, EAL, "Map size too big\n");
83                         rte_errno = E2BIG;
84                         return NULL;
85                 }
86
87                 mapped_addr = mmap(requested_addr, (size_t)map_sz, PROT_READ,
88                                 mmap_flags, -1, 0);
89                 if (mapped_addr == MAP_FAILED && allow_shrink)
90                         *size -= page_sz;
91         } while (allow_shrink && mapped_addr == MAP_FAILED && *size > 0);
92
93         /* align resulting address - if map failed, we will ignore the value
94          * anyway, so no need to add additional checks.
95          */
96         aligned_addr = no_align ? mapped_addr :
97                         RTE_PTR_ALIGN(mapped_addr, page_sz);
98
99         if (*size == 0) {
100                 RTE_LOG(ERR, EAL, "Cannot get a virtual area of any size: %s\n",
101                         strerror(errno));
102                 rte_errno = errno;
103                 return NULL;
104         } else if (mapped_addr == MAP_FAILED) {
105                 RTE_LOG(ERR, EAL, "Cannot get a virtual area: %s\n",
106                         strerror(errno));
107                 /* pass errno up the call chain */
108                 rte_errno = errno;
109                 return NULL;
110         } else if (requested_addr != NULL && !addr_is_hint &&
111                         aligned_addr != requested_addr) {
112                 RTE_LOG(ERR, EAL, "Cannot get a virtual area at requested address: %p (got %p)\n",
113                         requested_addr, aligned_addr);
114                 munmap(mapped_addr, map_sz);
115                 rte_errno = EADDRNOTAVAIL;
116                 return NULL;
117         } else if (requested_addr != NULL && addr_is_hint &&
118                         aligned_addr != requested_addr) {
119                 RTE_LOG(WARNING, EAL, "WARNING! Base virtual address hint (%p != %p) not respected!\n",
120                         requested_addr, aligned_addr);
121                 RTE_LOG(WARNING, EAL, "   This may cause issues with mapping memory into secondary processes\n");
122         } else if (next_baseaddr != NULL) {
123                 next_baseaddr = RTE_PTR_ADD(aligned_addr, *size);
124         }
125
126         RTE_LOG(DEBUG, EAL, "Virtual area found at %p (size = 0x%zx)\n",
127                 aligned_addr, *size);
128
129         if (unmap) {
130                 munmap(mapped_addr, map_sz);
131         } else if (!no_align) {
132                 void *map_end, *aligned_end;
133                 size_t before_len, after_len;
134
135                 /* when we reserve space with alignment, we add alignment to
136                  * mapping size. On 32-bit, if 1GB alignment was requested, this
137                  * would waste 1GB of address space, which is a luxury we cannot
138                  * afford. so, if alignment was performed, check if any unneeded
139                  * address space can be unmapped back.
140                  */
141
142                 map_end = RTE_PTR_ADD(mapped_addr, (size_t)map_sz);
143                 aligned_end = RTE_PTR_ADD(aligned_addr, *size);
144
145                 /* unmap space before aligned mmap address */
146                 before_len = RTE_PTR_DIFF(aligned_addr, mapped_addr);
147                 if (before_len > 0)
148                         munmap(mapped_addr, before_len);
149
150                 /* unmap space after aligned end mmap address */
151                 after_len = RTE_PTR_DIFF(map_end, aligned_end);
152                 if (after_len > 0)
153                         munmap(aligned_end, after_len);
154         }
155
156         return aligned_addr;
157 }
158
159 static uint64_t
160 get_mem_amount(uint64_t page_sz, uint64_t max_mem)
161 {
162         uint64_t area_sz, max_pages;
163
164         /* limit to RTE_MAX_MEMSEG_PER_LIST pages or RTE_MAX_MEM_MB_PER_LIST */
165         max_pages = RTE_MAX_MEMSEG_PER_LIST;
166         max_mem = RTE_MIN((uint64_t)RTE_MAX_MEM_MB_PER_LIST << 20, max_mem);
167
168         area_sz = RTE_MIN(page_sz * max_pages, max_mem);
169
170         /* make sure the list isn't smaller than the page size */
171         area_sz = RTE_MAX(area_sz, page_sz);
172
173         return RTE_ALIGN(area_sz, page_sz);
174 }
175
176 static int
177 free_memseg_list(struct rte_memseg_list *msl)
178 {
179         if (rte_fbarray_destroy(&msl->memseg_arr)) {
180                 RTE_LOG(ERR, EAL, "Cannot destroy memseg list\n");
181                 return -1;
182         }
183         memset(msl, 0, sizeof(*msl));
184         return 0;
185 }
186
187 static int
188 alloc_memseg_list(struct rte_memseg_list *msl, uint64_t page_sz,
189                 uint64_t max_mem, int socket_id, int type_msl_idx)
190 {
191         char name[RTE_FBARRAY_NAME_LEN];
192         uint64_t mem_amount;
193         int max_segs;
194
195         mem_amount = get_mem_amount(page_sz, max_mem);
196         max_segs = mem_amount / page_sz;
197
198         snprintf(name, sizeof(name), MEMSEG_LIST_FMT, page_sz >> 10, socket_id,
199                  type_msl_idx);
200         if (rte_fbarray_init(&msl->memseg_arr, name, max_segs,
201                         sizeof(struct rte_memseg))) {
202                 RTE_LOG(ERR, EAL, "Cannot allocate memseg list: %s\n",
203                         rte_strerror(rte_errno));
204                 return -1;
205         }
206
207         msl->page_sz = page_sz;
208         msl->socket_id = socket_id;
209         msl->base_va = NULL;
210
211         RTE_LOG(DEBUG, EAL, "Memseg list allocated: 0x%zxkB at socket %i\n",
212                         (size_t)page_sz >> 10, socket_id);
213
214         return 0;
215 }
216
217 static int
218 alloc_va_space(struct rte_memseg_list *msl)
219 {
220         uint64_t page_sz;
221         size_t mem_sz;
222         void *addr;
223         int flags = 0;
224
225 #ifdef RTE_ARCH_PPC_64
226         flags |= MAP_HUGETLB;
227 #endif
228
229         page_sz = msl->page_sz;
230         mem_sz = page_sz * msl->memseg_arr.len;
231
232         addr = eal_get_virtual_area(msl->base_va, &mem_sz, page_sz, 0, flags);
233         if (addr == NULL) {
234                 if (rte_errno == EADDRNOTAVAIL)
235                         RTE_LOG(ERR, EAL, "Could not mmap %llu bytes at [%p] - please use '--base-virtaddr' option\n",
236                                 (unsigned long long)mem_sz, msl->base_va);
237                 else
238                         RTE_LOG(ERR, EAL, "Cannot reserve memory\n");
239                 return -1;
240         }
241         msl->base_va = addr;
242
243         return 0;
244 }
245
246 static int __rte_unused
247 memseg_primary_init_32(void)
248 {
249         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
250         int active_sockets, hpi_idx, msl_idx = 0;
251         unsigned int socket_id, i;
252         struct rte_memseg_list *msl;
253         uint64_t extra_mem_per_socket, total_extra_mem, total_requested_mem;
254         uint64_t max_mem;
255
256         /* no-huge does not need this at all */
257         if (internal_config.no_hugetlbfs)
258                 return 0;
259
260         /* this is a giant hack, but desperate times call for desperate
261          * measures. in legacy 32-bit mode, we cannot preallocate VA space,
262          * because having upwards of 2 gigabytes of VA space already mapped will
263          * interfere with our ability to map and sort hugepages.
264          *
265          * therefore, in legacy 32-bit mode, we will be initializing memseg
266          * lists much later - in eal_memory.c, right after we unmap all the
267          * unneeded pages. this will not affect secondary processes, as those
268          * should be able to mmap the space without (too many) problems.
269          */
270         if (internal_config.legacy_mem)
271                 return 0;
272
273         /* 32-bit mode is a very special case. we cannot know in advance where
274          * the user will want to allocate their memory, so we have to do some
275          * heuristics.
276          */
277         active_sockets = 0;
278         total_requested_mem = 0;
279         if (internal_config.force_sockets)
280                 for (i = 0; i < rte_socket_count(); i++) {
281                         uint64_t mem;
282
283                         socket_id = rte_socket_id_by_idx(i);
284                         mem = internal_config.socket_mem[socket_id];
285
286                         if (mem == 0)
287                                 continue;
288
289                         active_sockets++;
290                         total_requested_mem += mem;
291                 }
292         else
293                 total_requested_mem = internal_config.memory;
294
295         max_mem = (uint64_t)RTE_MAX_MEM_MB << 20;
296         if (total_requested_mem > max_mem) {
297                 RTE_LOG(ERR, EAL, "Invalid parameters: 32-bit process can at most use %uM of memory\n",
298                                 (unsigned int)(max_mem >> 20));
299                 return -1;
300         }
301         total_extra_mem = max_mem - total_requested_mem;
302         extra_mem_per_socket = active_sockets == 0 ? total_extra_mem :
303                         total_extra_mem / active_sockets;
304
305         /* the allocation logic is a little bit convoluted, but here's how it
306          * works, in a nutshell:
307          *  - if user hasn't specified on which sockets to allocate memory via
308          *    --socket-mem, we allocate all of our memory on master core socket.
309          *  - if user has specified sockets to allocate memory on, there may be
310          *    some "unused" memory left (e.g. if user has specified --socket-mem
311          *    such that not all memory adds up to 2 gigabytes), so add it to all
312          *    sockets that are in use equally.
313          *
314          * page sizes are sorted by size in descending order, so we can safely
315          * assume that we dispense with bigger page sizes first.
316          */
317
318         /* create memseg lists */
319         for (i = 0; i < rte_socket_count(); i++) {
320                 int hp_sizes = (int) internal_config.num_hugepage_sizes;
321                 uint64_t max_socket_mem, cur_socket_mem;
322                 unsigned int master_lcore_socket;
323                 struct rte_config *cfg = rte_eal_get_configuration();
324                 bool skip;
325
326                 socket_id = rte_socket_id_by_idx(i);
327
328 #ifndef RTE_EAL_NUMA_AWARE_HUGEPAGES
329                 if (socket_id > 0)
330                         break;
331 #endif
332
333                 /* if we didn't specifically request memory on this socket */
334                 skip = active_sockets != 0 &&
335                                 internal_config.socket_mem[socket_id] == 0;
336                 /* ...or if we didn't specifically request memory on *any*
337                  * socket, and this is not master lcore
338                  */
339                 master_lcore_socket = rte_lcore_to_socket_id(cfg->master_lcore);
340                 skip |= active_sockets == 0 && socket_id != master_lcore_socket;
341
342                 if (skip) {
343                         RTE_LOG(DEBUG, EAL, "Will not preallocate memory on socket %u\n",
344                                         socket_id);
345                         continue;
346                 }
347
348                 /* max amount of memory on this socket */
349                 max_socket_mem = (active_sockets != 0 ?
350                                         internal_config.socket_mem[socket_id] :
351                                         internal_config.memory) +
352                                         extra_mem_per_socket;
353                 cur_socket_mem = 0;
354
355                 for (hpi_idx = 0; hpi_idx < hp_sizes; hpi_idx++) {
356                         uint64_t max_pagesz_mem, cur_pagesz_mem = 0;
357                         uint64_t hugepage_sz;
358                         struct hugepage_info *hpi;
359                         int type_msl_idx, max_segs, total_segs = 0;
360
361                         hpi = &internal_config.hugepage_info[hpi_idx];
362                         hugepage_sz = hpi->hugepage_sz;
363
364                         /* check if pages are actually available */
365                         if (hpi->num_pages[socket_id] == 0)
366                                 continue;
367
368                         max_segs = RTE_MAX_MEMSEG_PER_TYPE;
369                         max_pagesz_mem = max_socket_mem - cur_socket_mem;
370
371                         /* make it multiple of page size */
372                         max_pagesz_mem = RTE_ALIGN_FLOOR(max_pagesz_mem,
373                                         hugepage_sz);
374
375                         RTE_LOG(DEBUG, EAL, "Attempting to preallocate "
376                                         "%" PRIu64 "M on socket %i\n",
377                                         max_pagesz_mem >> 20, socket_id);
378
379                         type_msl_idx = 0;
380                         while (cur_pagesz_mem < max_pagesz_mem &&
381                                         total_segs < max_segs) {
382                                 if (msl_idx >= RTE_MAX_MEMSEG_LISTS) {
383                                         RTE_LOG(ERR, EAL,
384                                                 "No more space in memseg lists, please increase %s\n",
385                                                 RTE_STR(CONFIG_RTE_MAX_MEMSEG_LISTS));
386                                         return -1;
387                                 }
388
389                                 msl = &mcfg->memsegs[msl_idx];
390
391                                 if (alloc_memseg_list(msl, hugepage_sz,
392                                                 max_pagesz_mem, socket_id,
393                                                 type_msl_idx)) {
394                                         /* failing to allocate a memseg list is
395                                          * a serious error.
396                                          */
397                                         RTE_LOG(ERR, EAL, "Cannot allocate memseg list\n");
398                                         return -1;
399                                 }
400
401                                 if (alloc_va_space(msl)) {
402                                         /* if we couldn't allocate VA space, we
403                                          * can try with smaller page sizes.
404                                          */
405                                         RTE_LOG(ERR, EAL, "Cannot allocate VA space for memseg list, retrying with different page size\n");
406                                         /* deallocate memseg list */
407                                         if (free_memseg_list(msl))
408                                                 return -1;
409                                         break;
410                                 }
411
412                                 total_segs += msl->memseg_arr.len;
413                                 cur_pagesz_mem = total_segs * hugepage_sz;
414                                 type_msl_idx++;
415                                 msl_idx++;
416                         }
417                         cur_socket_mem += cur_pagesz_mem;
418                 }
419                 if (cur_socket_mem == 0) {
420                         RTE_LOG(ERR, EAL, "Cannot allocate VA space on socket %u\n",
421                                 socket_id);
422                         return -1;
423                 }
424         }
425
426         return 0;
427 }
428
429 static int __rte_unused
430 memseg_primary_init(void)
431 {
432         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
433         int i, socket_id, hpi_idx, msl_idx = 0;
434         struct rte_memseg_list *msl;
435         uint64_t max_mem, total_mem;
436
437         /* no-huge does not need this at all */
438         if (internal_config.no_hugetlbfs)
439                 return 0;
440
441         max_mem = (uint64_t)RTE_MAX_MEM_MB << 20;
442         total_mem = 0;
443
444         /* create memseg lists */
445         for (hpi_idx = 0; hpi_idx < (int) internal_config.num_hugepage_sizes;
446                         hpi_idx++) {
447                 struct hugepage_info *hpi;
448                 uint64_t hugepage_sz;
449
450                 hpi = &internal_config.hugepage_info[hpi_idx];
451                 hugepage_sz = hpi->hugepage_sz;
452
453                 for (i = 0; i < (int) rte_socket_count(); i++) {
454                         uint64_t max_type_mem, total_type_mem = 0;
455                         int type_msl_idx, max_segs, total_segs = 0;
456
457                         socket_id = rte_socket_id_by_idx(i);
458
459 #ifndef RTE_EAL_NUMA_AWARE_HUGEPAGES
460                         if (socket_id > 0)
461                                 break;
462 #endif
463
464                         if (total_mem >= max_mem)
465                                 break;
466
467                         max_type_mem = RTE_MIN(max_mem - total_mem,
468                                 (uint64_t)RTE_MAX_MEM_MB_PER_TYPE << 20);
469                         max_segs = RTE_MAX_MEMSEG_PER_TYPE;
470
471                         type_msl_idx = 0;
472                         while (total_type_mem < max_type_mem &&
473                                         total_segs < max_segs) {
474                                 uint64_t cur_max_mem;
475                                 if (msl_idx >= RTE_MAX_MEMSEG_LISTS) {
476                                         RTE_LOG(ERR, EAL,
477                                                 "No more space in memseg lists, please increase %s\n",
478                                                 RTE_STR(CONFIG_RTE_MAX_MEMSEG_LISTS));
479                                         return -1;
480                                 }
481
482                                 msl = &mcfg->memsegs[msl_idx++];
483
484                                 cur_max_mem = max_type_mem - total_type_mem;
485                                 if (alloc_memseg_list(msl, hugepage_sz,
486                                                 cur_max_mem, socket_id,
487                                                 type_msl_idx))
488                                         return -1;
489
490                                 total_segs += msl->memseg_arr.len;
491                                 total_type_mem = total_segs * hugepage_sz;
492                                 type_msl_idx++;
493
494                                 if (alloc_va_space(msl)) {
495                                         RTE_LOG(ERR, EAL, "Cannot allocate VA space for memseg list\n");
496                                         return -1;
497                                 }
498                         }
499                         total_mem += total_type_mem;
500                 }
501         }
502         return 0;
503 }
504
505 static int
506 memseg_secondary_init(void)
507 {
508         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
509         int msl_idx = 0;
510         struct rte_memseg_list *msl;
511
512         for (msl_idx = 0; msl_idx < RTE_MAX_MEMSEG_LISTS; msl_idx++) {
513
514                 msl = &mcfg->memsegs[msl_idx];
515
516                 /* skip empty memseg lists */
517                 if (msl->memseg_arr.len == 0)
518                         continue;
519
520                 if (rte_fbarray_attach(&msl->memseg_arr)) {
521                         RTE_LOG(ERR, EAL, "Cannot attach to primary process memseg lists\n");
522                         return -1;
523                 }
524
525                 /* preallocate VA space */
526                 if (alloc_va_space(msl)) {
527                         RTE_LOG(ERR, EAL, "Cannot preallocate VA space for hugepage memory\n");
528                         return -1;
529                 }
530         }
531
532         return 0;
533 }
534
535 static struct rte_memseg *
536 virt2memseg(const void *addr, const struct rte_memseg_list *msl)
537 {
538         const struct rte_fbarray *arr;
539         void *start, *end;
540         int ms_idx;
541
542         if (msl == NULL)
543                 return NULL;
544
545         /* a memseg list was specified, check if it's the right one */
546         start = msl->base_va;
547         end = RTE_PTR_ADD(start, (size_t)msl->page_sz * msl->memseg_arr.len);
548
549         if (addr < start || addr >= end)
550                 return NULL;
551
552         /* now, calculate index */
553         arr = &msl->memseg_arr;
554         ms_idx = RTE_PTR_DIFF(addr, msl->base_va) / msl->page_sz;
555         return rte_fbarray_get(arr, ms_idx);
556 }
557
558 static struct rte_memseg_list *
559 virt2memseg_list(const void *addr)
560 {
561         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
562         struct rte_memseg_list *msl;
563         int msl_idx;
564
565         for (msl_idx = 0; msl_idx < RTE_MAX_MEMSEG_LISTS; msl_idx++) {
566                 void *start, *end;
567                 msl = &mcfg->memsegs[msl_idx];
568
569                 start = msl->base_va;
570                 end = RTE_PTR_ADD(start,
571                                 (size_t)msl->page_sz * msl->memseg_arr.len);
572                 if (addr >= start && addr < end)
573                         break;
574         }
575         /* if we didn't find our memseg list */
576         if (msl_idx == RTE_MAX_MEMSEG_LISTS)
577                 return NULL;
578         return msl;
579 }
580
581 __rte_experimental struct rte_memseg_list *
582 rte_mem_virt2memseg_list(const void *addr)
583 {
584         return virt2memseg_list(addr);
585 }
586
587 struct virtiova {
588         rte_iova_t iova;
589         void *virt;
590 };
591 static int
592 find_virt(const struct rte_memseg_list *msl __rte_unused,
593                 const struct rte_memseg *ms, void *arg)
594 {
595         struct virtiova *vi = arg;
596         if (vi->iova >= ms->iova && vi->iova < (ms->iova + ms->len)) {
597                 size_t offset = vi->iova - ms->iova;
598                 vi->virt = RTE_PTR_ADD(ms->addr, offset);
599                 /* stop the walk */
600                 return 1;
601         }
602         return 0;
603 }
604 static int
605 find_virt_legacy(const struct rte_memseg_list *msl __rte_unused,
606                 const struct rte_memseg *ms, size_t len, void *arg)
607 {
608         struct virtiova *vi = arg;
609         if (vi->iova >= ms->iova && vi->iova < (ms->iova + len)) {
610                 size_t offset = vi->iova - ms->iova;
611                 vi->virt = RTE_PTR_ADD(ms->addr, offset);
612                 /* stop the walk */
613                 return 1;
614         }
615         return 0;
616 }
617
618 __rte_experimental void *
619 rte_mem_iova2virt(rte_iova_t iova)
620 {
621         struct virtiova vi;
622
623         memset(&vi, 0, sizeof(vi));
624
625         vi.iova = iova;
626         /* for legacy mem, we can get away with scanning VA-contiguous segments,
627          * as we know they are PA-contiguous as well
628          */
629         if (internal_config.legacy_mem)
630                 rte_memseg_contig_walk(find_virt_legacy, &vi);
631         else
632                 rte_memseg_walk(find_virt, &vi);
633
634         return vi.virt;
635 }
636
637 __rte_experimental struct rte_memseg *
638 rte_mem_virt2memseg(const void *addr, const struct rte_memseg_list *msl)
639 {
640         return virt2memseg(addr, msl != NULL ? msl :
641                         rte_mem_virt2memseg_list(addr));
642 }
643
644 static int
645 physmem_size(const struct rte_memseg_list *msl, void *arg)
646 {
647         uint64_t *total_len = arg;
648
649         *total_len += msl->memseg_arr.count * msl->page_sz;
650
651         return 0;
652 }
653
654 /* get the total size of memory */
655 uint64_t
656 rte_eal_get_physmem_size(void)
657 {
658         uint64_t total_len = 0;
659
660         rte_memseg_list_walk(physmem_size, &total_len);
661
662         return total_len;
663 }
664
665 static int
666 dump_memseg(const struct rte_memseg_list *msl, const struct rte_memseg *ms,
667                 void *arg)
668 {
669         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
670         int msl_idx, ms_idx;
671         FILE *f = arg;
672
673         msl_idx = msl - mcfg->memsegs;
674         if (msl_idx < 0 || msl_idx >= RTE_MAX_MEMSEG_LISTS)
675                 return -1;
676
677         ms_idx = rte_fbarray_find_idx(&msl->memseg_arr, ms);
678         if (ms_idx < 0)
679                 return -1;
680
681         fprintf(f, "Segment %i-%i: IOVA:0x%"PRIx64", len:%zu, "
682                         "virt:%p, socket_id:%"PRId32", "
683                         "hugepage_sz:%"PRIu64", nchannel:%"PRIx32", "
684                         "nrank:%"PRIx32"\n",
685                         msl_idx, ms_idx,
686                         ms->iova,
687                         ms->len,
688                         ms->addr,
689                         ms->socket_id,
690                         ms->hugepage_sz,
691                         ms->nchannel,
692                         ms->nrank);
693
694         return 0;
695 }
696
697 /*
698  * Defining here because declared in rte_memory.h, but the actual implementation
699  * is in eal_common_memalloc.c, like all other memalloc internals.
700  */
701 int __rte_experimental
702 rte_mem_event_callback_register(const char *name, rte_mem_event_callback_t clb,
703                 void *arg)
704 {
705         /* FreeBSD boots with legacy mem enabled by default */
706         if (internal_config.legacy_mem) {
707                 RTE_LOG(DEBUG, EAL, "Registering mem event callbacks not supported\n");
708                 rte_errno = ENOTSUP;
709                 return -1;
710         }
711         return eal_memalloc_mem_event_callback_register(name, clb, arg);
712 }
713
714 int __rte_experimental
715 rte_mem_event_callback_unregister(const char *name, void *arg)
716 {
717         /* FreeBSD boots with legacy mem enabled by default */
718         if (internal_config.legacy_mem) {
719                 RTE_LOG(DEBUG, EAL, "Registering mem event callbacks not supported\n");
720                 rte_errno = ENOTSUP;
721                 return -1;
722         }
723         return eal_memalloc_mem_event_callback_unregister(name, arg);
724 }
725
726 int __rte_experimental
727 rte_mem_alloc_validator_register(const char *name,
728                 rte_mem_alloc_validator_t clb, int socket_id, size_t limit)
729 {
730         /* FreeBSD boots with legacy mem enabled by default */
731         if (internal_config.legacy_mem) {
732                 RTE_LOG(DEBUG, EAL, "Registering mem alloc validators not supported\n");
733                 rte_errno = ENOTSUP;
734                 return -1;
735         }
736         return eal_memalloc_mem_alloc_validator_register(name, clb, socket_id,
737                         limit);
738 }
739
740 int __rte_experimental
741 rte_mem_alloc_validator_unregister(const char *name, int socket_id)
742 {
743         /* FreeBSD boots with legacy mem enabled by default */
744         if (internal_config.legacy_mem) {
745                 RTE_LOG(DEBUG, EAL, "Registering mem alloc validators not supported\n");
746                 rte_errno = ENOTSUP;
747                 return -1;
748         }
749         return eal_memalloc_mem_alloc_validator_unregister(name, socket_id);
750 }
751
752 /* Dump the physical memory layout on console */
753 void
754 rte_dump_physmem_layout(FILE *f)
755 {
756         rte_memseg_walk(dump_memseg, f);
757 }
758
759 /* return the number of memory channels */
760 unsigned rte_memory_get_nchannel(void)
761 {
762         return rte_eal_get_configuration()->mem_config->nchannel;
763 }
764
765 /* return the number of memory rank */
766 unsigned rte_memory_get_nrank(void)
767 {
768         return rte_eal_get_configuration()->mem_config->nrank;
769 }
770
771 static int
772 rte_eal_memdevice_init(void)
773 {
774         struct rte_config *config;
775
776         if (rte_eal_process_type() == RTE_PROC_SECONDARY)
777                 return 0;
778
779         config = rte_eal_get_configuration();
780         config->mem_config->nchannel = internal_config.force_nchannel;
781         config->mem_config->nrank = internal_config.force_nrank;
782
783         return 0;
784 }
785
786 /* Lock page in physical memory and prevent from swapping. */
787 int
788 rte_mem_lock_page(const void *virt)
789 {
790         unsigned long virtual = (unsigned long)virt;
791         int page_size = getpagesize();
792         unsigned long aligned = (virtual & ~(page_size - 1));
793         return mlock((void *)aligned, page_size);
794 }
795
796 int __rte_experimental
797 rte_memseg_contig_walk(rte_memseg_contig_walk_t func, void *arg)
798 {
799         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
800         int i, ms_idx, ret = 0;
801
802         /* do not allow allocations/frees/init while we iterate */
803         rte_rwlock_read_lock(&mcfg->memory_hotplug_lock);
804
805         for (i = 0; i < RTE_MAX_MEMSEG_LISTS; i++) {
806                 struct rte_memseg_list *msl = &mcfg->memsegs[i];
807                 const struct rte_memseg *ms;
808                 struct rte_fbarray *arr;
809
810                 if (msl->memseg_arr.count == 0)
811                         continue;
812
813                 arr = &msl->memseg_arr;
814
815                 ms_idx = rte_fbarray_find_next_used(arr, 0);
816                 while (ms_idx >= 0) {
817                         int n_segs;
818                         size_t len;
819
820                         ms = rte_fbarray_get(arr, ms_idx);
821
822                         /* find how many more segments there are, starting with
823                          * this one.
824                          */
825                         n_segs = rte_fbarray_find_contig_used(arr, ms_idx);
826                         len = n_segs * msl->page_sz;
827
828                         ret = func(msl, ms, len, arg);
829                         if (ret < 0) {
830                                 ret = -1;
831                                 goto out;
832                         } else if (ret > 0) {
833                                 ret = 1;
834                                 goto out;
835                         }
836                         ms_idx = rte_fbarray_find_next_used(arr,
837                                         ms_idx + n_segs);
838                 }
839         }
840 out:
841         rte_rwlock_read_unlock(&mcfg->memory_hotplug_lock);
842         return ret;
843 }
844
845 int __rte_experimental
846 rte_memseg_walk(rte_memseg_walk_t func, void *arg)
847 {
848         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
849         int i, ms_idx, ret = 0;
850
851         /* do not allow allocations/frees/init while we iterate */
852         rte_rwlock_read_lock(&mcfg->memory_hotplug_lock);
853
854         for (i = 0; i < RTE_MAX_MEMSEG_LISTS; i++) {
855                 struct rte_memseg_list *msl = &mcfg->memsegs[i];
856                 const struct rte_memseg *ms;
857                 struct rte_fbarray *arr;
858
859                 if (msl->memseg_arr.count == 0)
860                         continue;
861
862                 arr = &msl->memseg_arr;
863
864                 ms_idx = rte_fbarray_find_next_used(arr, 0);
865                 while (ms_idx >= 0) {
866                         ms = rte_fbarray_get(arr, ms_idx);
867                         ret = func(msl, ms, arg);
868                         if (ret < 0) {
869                                 ret = -1;
870                                 goto out;
871                         } else if (ret > 0) {
872                                 ret = 1;
873                                 goto out;
874                         }
875                         ms_idx = rte_fbarray_find_next_used(arr, ms_idx + 1);
876                 }
877         }
878 out:
879         rte_rwlock_read_unlock(&mcfg->memory_hotplug_lock);
880         return ret;
881 }
882
883 int __rte_experimental
884 rte_memseg_list_walk(rte_memseg_list_walk_t func, void *arg)
885 {
886         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
887         int i, ret = 0;
888
889         /* do not allow allocations/frees/init while we iterate */
890         rte_rwlock_read_lock(&mcfg->memory_hotplug_lock);
891
892         for (i = 0; i < RTE_MAX_MEMSEG_LISTS; i++) {
893                 struct rte_memseg_list *msl = &mcfg->memsegs[i];
894
895                 if (msl->base_va == NULL)
896                         continue;
897
898                 ret = func(msl, arg);
899                 if (ret < 0) {
900                         ret = -1;
901                         goto out;
902                 }
903                 if (ret > 0) {
904                         ret = 1;
905                         goto out;
906                 }
907         }
908 out:
909         rte_rwlock_read_unlock(&mcfg->memory_hotplug_lock);
910         return ret;
911 }
912
913 /* init memory subsystem */
914 int
915 rte_eal_memory_init(void)
916 {
917         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
918         int retval;
919         RTE_LOG(DEBUG, EAL, "Setting up physically contiguous memory...\n");
920
921         if (!mcfg)
922                 return -1;
923
924         /* lock mem hotplug here, to prevent races while we init */
925         rte_rwlock_read_lock(&mcfg->memory_hotplug_lock);
926
927         retval = rte_eal_process_type() == RTE_PROC_PRIMARY ?
928 #ifndef RTE_ARCH_64
929                         memseg_primary_init_32() :
930 #else
931                         memseg_primary_init() :
932 #endif
933                         memseg_secondary_init();
934
935         if (retval < 0)
936                 goto fail;
937
938         if (eal_memalloc_init() < 0)
939                 goto fail;
940
941         retval = rte_eal_process_type() == RTE_PROC_PRIMARY ?
942                         rte_eal_hugepage_init() :
943                         rte_eal_hugepage_attach();
944         if (retval < 0)
945                 goto fail;
946
947         if (internal_config.no_shconf == 0 && rte_eal_memdevice_init() < 0)
948                 goto fail;
949
950         return 0;
951 fail:
952         rte_rwlock_read_unlock(&mcfg->memory_hotplug_lock);
953         return -1;
954 }