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