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