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