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