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