616db5ce3174506f2c44a8fc247eb2234728e88e
[dpdk.git] / lib / eal / common / eal_common_memory.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4
5 #include <fcntl.h>
6 #include <errno.h>
7 #include <stdio.h>
8 #include <stdint.h>
9 #include <stdlib.h>
10 #include <stdarg.h>
11 #include <string.h>
12 #include <unistd.h>
13 #include <inttypes.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_eal_paging.h>
21 #include <rte_errno.h>
22 #include <rte_log.h>
23 #ifndef RTE_EXEC_ENV_WINDOWS
24 #include <rte_telemetry.h>
25 #endif
26
27 #include "eal_memalloc.h"
28 #include "eal_private.h"
29 #include "eal_internal_cfg.h"
30 #include "eal_memcfg.h"
31 #include "eal_options.h"
32 #include "malloc_heap.h"
33
34 /*
35  * Try to mmap *size bytes in /dev/zero. If it is successful, return the
36  * pointer to the mmap'd area and keep *size unmodified. Else, retry
37  * with a smaller zone: decrease *size by hugepage_sz until it reaches
38  * 0. In this case, return NULL. Note: this function returns an address
39  * which is a multiple of hugepage size.
40  */
41
42 #define MEMSEG_LIST_FMT "memseg-%" PRIu64 "k-%i-%i"
43
44 static void *next_baseaddr;
45 static uint64_t system_page_sz;
46
47 #define MAX_MMAP_WITH_DEFINED_ADDR_TRIES 5
48 void *
49 eal_get_virtual_area(void *requested_addr, size_t *size,
50         size_t page_sz, int flags, int reserve_flags)
51 {
52         bool addr_is_hint, allow_shrink, unmap, no_align;
53         uint64_t map_sz;
54         void *mapped_addr, *aligned_addr;
55         uint8_t try = 0;
56         struct internal_config *internal_conf =
57                 eal_get_internal_configuration();
58
59         if (system_page_sz == 0)
60                 system_page_sz = rte_mem_page_size();
61
62         RTE_LOG(DEBUG, EAL, "Ask a virtual area of 0x%zx bytes\n", *size);
63
64         addr_is_hint = (flags & EAL_VIRTUAL_AREA_ADDR_IS_HINT) > 0;
65         allow_shrink = (flags & EAL_VIRTUAL_AREA_ALLOW_SHRINK) > 0;
66         unmap = (flags & EAL_VIRTUAL_AREA_UNMAP) > 0;
67
68         if (next_baseaddr == NULL && internal_conf->base_virtaddr != 0 &&
69                         rte_eal_process_type() == RTE_PROC_PRIMARY)
70                 next_baseaddr = (void *) internal_conf->base_virtaddr;
71
72 #ifdef RTE_ARCH_64
73         if (next_baseaddr == NULL && internal_conf->base_virtaddr == 0 &&
74                         rte_eal_process_type() == RTE_PROC_PRIMARY)
75                 next_baseaddr = (void *) eal_get_baseaddr();
76 #endif
77         if (requested_addr == NULL && next_baseaddr != NULL) {
78                 requested_addr = next_baseaddr;
79                 requested_addr = RTE_PTR_ALIGN(requested_addr, page_sz);
80                 addr_is_hint = true;
81         }
82
83         /* we don't need alignment of resulting pointer in the following cases:
84          *
85          * 1. page size is equal to system size
86          * 2. we have a requested address, and it is page-aligned, and we will
87          *    be discarding the address if we get a different one.
88          *
89          * for all other cases, alignment is potentially necessary.
90          */
91         no_align = (requested_addr != NULL &&
92                 requested_addr == RTE_PTR_ALIGN(requested_addr, page_sz) &&
93                 !addr_is_hint) ||
94                 page_sz == system_page_sz;
95
96         do {
97                 map_sz = no_align ? *size : *size + page_sz;
98                 if (map_sz > SIZE_MAX) {
99                         RTE_LOG(ERR, EAL, "Map size too big\n");
100                         rte_errno = E2BIG;
101                         return NULL;
102                 }
103
104                 mapped_addr = eal_mem_reserve(
105                         requested_addr, (size_t)map_sz, reserve_flags);
106                 if ((mapped_addr == NULL) && allow_shrink)
107                         *size -= page_sz;
108
109                 if ((mapped_addr != NULL) && addr_is_hint &&
110                                 (mapped_addr != requested_addr)) {
111                         try++;
112                         next_baseaddr = RTE_PTR_ADD(next_baseaddr, page_sz);
113                         if (try <= MAX_MMAP_WITH_DEFINED_ADDR_TRIES) {
114                                 /* hint was not used. Try with another offset */
115                                 eal_mem_free(mapped_addr, map_sz);
116                                 mapped_addr = NULL;
117                                 requested_addr = next_baseaddr;
118                         }
119                 }
120         } while ((allow_shrink || addr_is_hint) &&
121                 (mapped_addr == NULL) && (*size > 0));
122
123         /* align resulting address - if map failed, we will ignore the value
124          * anyway, so no need to add additional checks.
125          */
126         aligned_addr = no_align ? mapped_addr :
127                         RTE_PTR_ALIGN(mapped_addr, page_sz);
128
129         if (*size == 0) {
130                 RTE_LOG(ERR, EAL, "Cannot get a virtual area of any size: %s\n",
131                         rte_strerror(rte_errno));
132                 return NULL;
133         } else if (mapped_addr == NULL) {
134                 RTE_LOG(ERR, EAL, "Cannot get a virtual area: %s\n",
135                         rte_strerror(rte_errno));
136                 return NULL;
137         } else if (requested_addr != NULL && !addr_is_hint &&
138                         aligned_addr != requested_addr) {
139                 RTE_LOG(ERR, EAL, "Cannot get a virtual area at requested address: %p (got %p)\n",
140                         requested_addr, aligned_addr);
141                 eal_mem_free(mapped_addr, map_sz);
142                 rte_errno = EADDRNOTAVAIL;
143                 return NULL;
144         } else if (requested_addr != NULL && addr_is_hint &&
145                         aligned_addr != requested_addr) {
146                 RTE_LOG(WARNING, EAL, "WARNING! Base virtual address hint (%p != %p) not respected!\n",
147                         requested_addr, aligned_addr);
148                 RTE_LOG(WARNING, EAL, "   This may cause issues with mapping memory into secondary processes\n");
149         } else if (next_baseaddr != NULL) {
150                 next_baseaddr = RTE_PTR_ADD(aligned_addr, *size);
151         }
152
153         RTE_LOG(DEBUG, EAL, "Virtual area found at %p (size = 0x%zx)\n",
154                 aligned_addr, *size);
155
156         if (unmap) {
157                 eal_mem_free(mapped_addr, map_sz);
158         } else if (!no_align) {
159                 void *map_end, *aligned_end;
160                 size_t before_len, after_len;
161
162                 /* when we reserve space with alignment, we add alignment to
163                  * mapping size. On 32-bit, if 1GB alignment was requested, this
164                  * would waste 1GB of address space, which is a luxury we cannot
165                  * afford. so, if alignment was performed, check if any unneeded
166                  * address space can be unmapped back.
167                  */
168
169                 map_end = RTE_PTR_ADD(mapped_addr, (size_t)map_sz);
170                 aligned_end = RTE_PTR_ADD(aligned_addr, *size);
171
172                 /* unmap space before aligned mmap address */
173                 before_len = RTE_PTR_DIFF(aligned_addr, mapped_addr);
174                 if (before_len > 0)
175                         eal_mem_free(mapped_addr, before_len);
176
177                 /* unmap space after aligned end mmap address */
178                 after_len = RTE_PTR_DIFF(map_end, aligned_end);
179                 if (after_len > 0)
180                         eal_mem_free(aligned_end, after_len);
181         }
182
183         if (!unmap) {
184                 /* Exclude these pages from a core dump. */
185                 eal_mem_set_dump(aligned_addr, *size, false);
186         }
187
188         return aligned_addr;
189 }
190
191 int
192 eal_memseg_list_init_named(struct rte_memseg_list *msl, const char *name,
193                 uint64_t page_sz, int n_segs, int socket_id, bool heap)
194 {
195         if (rte_fbarray_init(&msl->memseg_arr, name, n_segs,
196                         sizeof(struct rte_memseg))) {
197                 RTE_LOG(ERR, EAL, "Cannot allocate memseg list: %s\n",
198                         rte_strerror(rte_errno));
199                 return -1;
200         }
201
202         msl->page_sz = page_sz;
203         msl->socket_id = socket_id;
204         msl->base_va = NULL;
205         msl->heap = heap;
206
207         RTE_LOG(DEBUG, EAL,
208                 "Memseg list allocated at socket %i, page size 0x%"PRIx64"kB\n",
209                 socket_id, page_sz >> 10);
210
211         return 0;
212 }
213
214 int
215 eal_memseg_list_init(struct rte_memseg_list *msl, uint64_t page_sz,
216                 int n_segs, int socket_id, int type_msl_idx, bool heap)
217 {
218         char name[RTE_FBARRAY_NAME_LEN];
219
220         snprintf(name, sizeof(name), MEMSEG_LIST_FMT, page_sz >> 10, socket_id,
221                  type_msl_idx);
222
223         return eal_memseg_list_init_named(
224                 msl, name, page_sz, n_segs, socket_id, heap);
225 }
226
227 int
228 eal_memseg_list_alloc(struct rte_memseg_list *msl, int reserve_flags)
229 {
230         size_t page_sz, mem_sz;
231         void *addr;
232
233         page_sz = msl->page_sz;
234         mem_sz = page_sz * msl->memseg_arr.len;
235
236         addr = eal_get_virtual_area(
237                 msl->base_va, &mem_sz, page_sz, 0, reserve_flags);
238         if (addr == NULL) {
239 #ifndef RTE_EXEC_ENV_WINDOWS
240                 /* The hint would be misleading on Windows, because address
241                  * is by default system-selected (base VA = 0).
242                  * However, this function is called from many places,
243                  * including common code, so don't duplicate the message.
244                  */
245                 if (rte_errno == EADDRNOTAVAIL)
246                         RTE_LOG(ERR, EAL, "Cannot reserve %llu bytes at [%p] - "
247                                 "please use '--" OPT_BASE_VIRTADDR "' option\n",
248                                 (unsigned long long)mem_sz, msl->base_va);
249 #endif
250                 return -1;
251         }
252         msl->base_va = addr;
253         msl->len = mem_sz;
254
255         RTE_LOG(DEBUG, EAL, "VA reserved for memseg list at %p, size %zx\n",
256                         addr, mem_sz);
257
258         return 0;
259 }
260
261 void
262 eal_memseg_list_populate(struct rte_memseg_list *msl, void *addr, int n_segs)
263 {
264         size_t page_sz = msl->page_sz;
265         int i;
266
267         for (i = 0; i < n_segs; i++) {
268                 struct rte_fbarray *arr = &msl->memseg_arr;
269                 struct rte_memseg *ms = rte_fbarray_get(arr, i);
270
271                 if (rte_eal_iova_mode() == RTE_IOVA_VA)
272                         ms->iova = (uintptr_t)addr;
273                 else
274                         ms->iova = RTE_BAD_IOVA;
275                 ms->addr = addr;
276                 ms->hugepage_sz = page_sz;
277                 ms->socket_id = 0;
278                 ms->len = page_sz;
279
280                 rte_fbarray_set_used(arr, i);
281
282                 addr = RTE_PTR_ADD(addr, page_sz);
283         }
284 }
285
286 static struct rte_memseg *
287 virt2memseg(const void *addr, const struct rte_memseg_list *msl)
288 {
289         const struct rte_fbarray *arr;
290         void *start, *end;
291         int ms_idx;
292
293         if (msl == NULL)
294                 return NULL;
295
296         /* a memseg list was specified, check if it's the right one */
297         start = msl->base_va;
298         end = RTE_PTR_ADD(start, msl->len);
299
300         if (addr < start || addr >= end)
301                 return NULL;
302
303         /* now, calculate index */
304         arr = &msl->memseg_arr;
305         ms_idx = RTE_PTR_DIFF(addr, msl->base_va) / msl->page_sz;
306         return rte_fbarray_get(arr, ms_idx);
307 }
308
309 static struct rte_memseg_list *
310 virt2memseg_list(const void *addr)
311 {
312         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
313         struct rte_memseg_list *msl;
314         int msl_idx;
315
316         for (msl_idx = 0; msl_idx < RTE_MAX_MEMSEG_LISTS; msl_idx++) {
317                 void *start, *end;
318                 msl = &mcfg->memsegs[msl_idx];
319
320                 start = msl->base_va;
321                 end = RTE_PTR_ADD(start, msl->len);
322                 if (addr >= start && addr < end)
323                         break;
324         }
325         /* if we didn't find our memseg list */
326         if (msl_idx == RTE_MAX_MEMSEG_LISTS)
327                 return NULL;
328         return msl;
329 }
330
331 struct rte_memseg_list *
332 rte_mem_virt2memseg_list(const void *addr)
333 {
334         return virt2memseg_list(addr);
335 }
336
337 struct virtiova {
338         rte_iova_t iova;
339         void *virt;
340 };
341 static int
342 find_virt(const struct rte_memseg_list *msl __rte_unused,
343                 const struct rte_memseg *ms, void *arg)
344 {
345         struct virtiova *vi = arg;
346         if (vi->iova >= ms->iova && vi->iova < (ms->iova + ms->len)) {
347                 size_t offset = vi->iova - ms->iova;
348                 vi->virt = RTE_PTR_ADD(ms->addr, offset);
349                 /* stop the walk */
350                 return 1;
351         }
352         return 0;
353 }
354 static int
355 find_virt_legacy(const struct rte_memseg_list *msl __rte_unused,
356                 const struct rte_memseg *ms, size_t len, void *arg)
357 {
358         struct virtiova *vi = arg;
359         if (vi->iova >= ms->iova && vi->iova < (ms->iova + len)) {
360                 size_t offset = vi->iova - ms->iova;
361                 vi->virt = RTE_PTR_ADD(ms->addr, offset);
362                 /* stop the walk */
363                 return 1;
364         }
365         return 0;
366 }
367
368 void *
369 rte_mem_iova2virt(rte_iova_t iova)
370 {
371         struct virtiova vi;
372         const struct internal_config *internal_conf =
373                 eal_get_internal_configuration();
374
375         memset(&vi, 0, sizeof(vi));
376
377         vi.iova = iova;
378         /* for legacy mem, we can get away with scanning VA-contiguous segments,
379          * as we know they are PA-contiguous as well
380          */
381         if (internal_conf->legacy_mem)
382                 rte_memseg_contig_walk(find_virt_legacy, &vi);
383         else
384                 rte_memseg_walk(find_virt, &vi);
385
386         return vi.virt;
387 }
388
389 struct rte_memseg *
390 rte_mem_virt2memseg(const void *addr, const struct rte_memseg_list *msl)
391 {
392         return virt2memseg(addr, msl != NULL ? msl :
393                         rte_mem_virt2memseg_list(addr));
394 }
395
396 static int
397 physmem_size(const struct rte_memseg_list *msl, void *arg)
398 {
399         uint64_t *total_len = arg;
400
401         if (msl->external)
402                 return 0;
403
404         *total_len += msl->memseg_arr.count * msl->page_sz;
405
406         return 0;
407 }
408
409 /* get the total size of memory */
410 uint64_t
411 rte_eal_get_physmem_size(void)
412 {
413         uint64_t total_len = 0;
414
415         rte_memseg_list_walk(physmem_size, &total_len);
416
417         return total_len;
418 }
419
420 static int
421 dump_memseg(const struct rte_memseg_list *msl, const struct rte_memseg *ms,
422                 void *arg)
423 {
424         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
425         int msl_idx, ms_idx, fd;
426         FILE *f = arg;
427
428         msl_idx = msl - mcfg->memsegs;
429         if (msl_idx < 0 || msl_idx >= RTE_MAX_MEMSEG_LISTS)
430                 return -1;
431
432         ms_idx = rte_fbarray_find_idx(&msl->memseg_arr, ms);
433         if (ms_idx < 0)
434                 return -1;
435
436         fd = eal_memalloc_get_seg_fd(msl_idx, ms_idx);
437         fprintf(f, "Segment %i-%i: IOVA:0x%"PRIx64", len:%zu, "
438                         "virt:%p, socket_id:%"PRId32", "
439                         "hugepage_sz:%"PRIu64", nchannel:%"PRIx32", "
440                         "nrank:%"PRIx32" fd:%i\n",
441                         msl_idx, ms_idx,
442                         ms->iova,
443                         ms->len,
444                         ms->addr,
445                         ms->socket_id,
446                         ms->hugepage_sz,
447                         ms->nchannel,
448                         ms->nrank,
449                         fd);
450
451         return 0;
452 }
453
454 /*
455  * Defining here because declared in rte_memory.h, but the actual implementation
456  * is in eal_common_memalloc.c, like all other memalloc internals.
457  */
458 int
459 rte_mem_event_callback_register(const char *name, rte_mem_event_callback_t clb,
460                 void *arg)
461 {
462         const struct internal_config *internal_conf =
463                 eal_get_internal_configuration();
464
465         /* FreeBSD boots with legacy mem enabled by default */
466         if (internal_conf->legacy_mem) {
467                 RTE_LOG(DEBUG, EAL, "Registering mem event callbacks not supported\n");
468                 rte_errno = ENOTSUP;
469                 return -1;
470         }
471         return eal_memalloc_mem_event_callback_register(name, clb, arg);
472 }
473
474 int
475 rte_mem_event_callback_unregister(const char *name, void *arg)
476 {
477         const struct internal_config *internal_conf =
478                 eal_get_internal_configuration();
479
480         /* FreeBSD boots with legacy mem enabled by default */
481         if (internal_conf->legacy_mem) {
482                 RTE_LOG(DEBUG, EAL, "Registering mem event callbacks not supported\n");
483                 rte_errno = ENOTSUP;
484                 return -1;
485         }
486         return eal_memalloc_mem_event_callback_unregister(name, arg);
487 }
488
489 int
490 rte_mem_alloc_validator_register(const char *name,
491                 rte_mem_alloc_validator_t clb, int socket_id, size_t limit)
492 {
493         const struct internal_config *internal_conf =
494                 eal_get_internal_configuration();
495
496         /* FreeBSD boots with legacy mem enabled by default */
497         if (internal_conf->legacy_mem) {
498                 RTE_LOG(DEBUG, EAL, "Registering mem alloc validators not supported\n");
499                 rte_errno = ENOTSUP;
500                 return -1;
501         }
502         return eal_memalloc_mem_alloc_validator_register(name, clb, socket_id,
503                         limit);
504 }
505
506 int
507 rte_mem_alloc_validator_unregister(const char *name, int socket_id)
508 {
509         const struct internal_config *internal_conf =
510                 eal_get_internal_configuration();
511
512         /* FreeBSD boots with legacy mem enabled by default */
513         if (internal_conf->legacy_mem) {
514                 RTE_LOG(DEBUG, EAL, "Registering mem alloc validators not supported\n");
515                 rte_errno = ENOTSUP;
516                 return -1;
517         }
518         return eal_memalloc_mem_alloc_validator_unregister(name, socket_id);
519 }
520
521 /* Dump the physical memory layout on console */
522 void
523 rte_dump_physmem_layout(FILE *f)
524 {
525         rte_memseg_walk(dump_memseg, f);
526 }
527
528 static int
529 check_iova(const struct rte_memseg_list *msl __rte_unused,
530                 const struct rte_memseg *ms, void *arg)
531 {
532         uint64_t *mask = arg;
533         rte_iova_t iova;
534
535         /* higher address within segment */
536         iova = (ms->iova + ms->len) - 1;
537         if (!(iova & *mask))
538                 return 0;
539
540         RTE_LOG(DEBUG, EAL, "memseg iova %"PRIx64", len %zx, out of range\n",
541                             ms->iova, ms->len);
542
543         RTE_LOG(DEBUG, EAL, "\tusing dma mask %"PRIx64"\n", *mask);
544         return 1;
545 }
546
547 #define MAX_DMA_MASK_BITS 63
548
549 /* check memseg iovas are within the required range based on dma mask */
550 static int
551 check_dma_mask(uint8_t maskbits, bool thread_unsafe)
552 {
553         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
554         uint64_t mask;
555         int ret;
556
557         /* Sanity check. We only check width can be managed with 64 bits
558          * variables. Indeed any higher value is likely wrong. */
559         if (maskbits > MAX_DMA_MASK_BITS) {
560                 RTE_LOG(ERR, EAL, "wrong dma mask size %u (Max: %u)\n",
561                                    maskbits, MAX_DMA_MASK_BITS);
562                 return -1;
563         }
564
565         /* create dma mask */
566         mask = ~((1ULL << maskbits) - 1);
567
568         if (thread_unsafe)
569                 ret = rte_memseg_walk_thread_unsafe(check_iova, &mask);
570         else
571                 ret = rte_memseg_walk(check_iova, &mask);
572
573         if (ret)
574                 /*
575                  * Dma mask precludes hugepage usage.
576                  * This device can not be used and we do not need to keep
577                  * the dma mask.
578                  */
579                 return 1;
580
581         /*
582          * we need to keep the more restricted maskbit for checking
583          * potential dynamic memory allocation in the future.
584          */
585         mcfg->dma_maskbits = mcfg->dma_maskbits == 0 ? maskbits :
586                              RTE_MIN(mcfg->dma_maskbits, maskbits);
587
588         return 0;
589 }
590
591 int
592 rte_mem_check_dma_mask(uint8_t maskbits)
593 {
594         return check_dma_mask(maskbits, false);
595 }
596
597 int
598 rte_mem_check_dma_mask_thread_unsafe(uint8_t maskbits)
599 {
600         return check_dma_mask(maskbits, true);
601 }
602
603 /*
604  * Set dma mask to use when memory initialization is done.
605  *
606  * This function should ONLY be used by code executed before the memory
607  * initialization. PMDs should use rte_mem_check_dma_mask if addressing
608  * limitations by the device.
609  */
610 void
611 rte_mem_set_dma_mask(uint8_t maskbits)
612 {
613         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
614
615         mcfg->dma_maskbits = mcfg->dma_maskbits == 0 ? maskbits :
616                              RTE_MIN(mcfg->dma_maskbits, maskbits);
617 }
618
619 /* return the number of memory channels */
620 unsigned rte_memory_get_nchannel(void)
621 {
622         return rte_eal_get_configuration()->mem_config->nchannel;
623 }
624
625 /* return the number of memory rank */
626 unsigned rte_memory_get_nrank(void)
627 {
628         return rte_eal_get_configuration()->mem_config->nrank;
629 }
630
631 static int
632 rte_eal_memdevice_init(void)
633 {
634         struct rte_config *config;
635         const struct internal_config *internal_conf;
636
637         if (rte_eal_process_type() == RTE_PROC_SECONDARY)
638                 return 0;
639
640         internal_conf = eal_get_internal_configuration();
641         config = rte_eal_get_configuration();
642         config->mem_config->nchannel = internal_conf->force_nchannel;
643         config->mem_config->nrank = internal_conf->force_nrank;
644
645         return 0;
646 }
647
648 /* Lock page in physical memory and prevent from swapping. */
649 int
650 rte_mem_lock_page(const void *virt)
651 {
652         uintptr_t virtual = (uintptr_t)virt;
653         size_t page_size = rte_mem_page_size();
654         uintptr_t aligned = RTE_PTR_ALIGN_FLOOR(virtual, page_size);
655         return rte_mem_lock((void *)aligned, page_size);
656 }
657
658 int
659 rte_memseg_contig_walk_thread_unsafe(rte_memseg_contig_walk_t func, void *arg)
660 {
661         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
662         int i, ms_idx, ret = 0;
663
664         for (i = 0; i < RTE_MAX_MEMSEG_LISTS; i++) {
665                 struct rte_memseg_list *msl = &mcfg->memsegs[i];
666                 const struct rte_memseg *ms;
667                 struct rte_fbarray *arr;
668
669                 if (msl->memseg_arr.count == 0)
670                         continue;
671
672                 arr = &msl->memseg_arr;
673
674                 ms_idx = rte_fbarray_find_next_used(arr, 0);
675                 while (ms_idx >= 0) {
676                         int n_segs;
677                         size_t len;
678
679                         ms = rte_fbarray_get(arr, ms_idx);
680
681                         /* find how many more segments there are, starting with
682                          * this one.
683                          */
684                         n_segs = rte_fbarray_find_contig_used(arr, ms_idx);
685                         len = n_segs * msl->page_sz;
686
687                         ret = func(msl, ms, len, arg);
688                         if (ret)
689                                 return ret;
690                         ms_idx = rte_fbarray_find_next_used(arr,
691                                         ms_idx + n_segs);
692                 }
693         }
694         return 0;
695 }
696
697 int
698 rte_memseg_contig_walk(rte_memseg_contig_walk_t func, void *arg)
699 {
700         int ret = 0;
701
702         /* do not allow allocations/frees/init while we iterate */
703         rte_mcfg_mem_read_lock();
704         ret = rte_memseg_contig_walk_thread_unsafe(func, arg);
705         rte_mcfg_mem_read_unlock();
706
707         return ret;
708 }
709
710 int
711 rte_memseg_walk_thread_unsafe(rte_memseg_walk_t func, void *arg)
712 {
713         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
714         int i, ms_idx, ret = 0;
715
716         for (i = 0; i < RTE_MAX_MEMSEG_LISTS; i++) {
717                 struct rte_memseg_list *msl = &mcfg->memsegs[i];
718                 const struct rte_memseg *ms;
719                 struct rte_fbarray *arr;
720
721                 if (msl->memseg_arr.count == 0)
722                         continue;
723
724                 arr = &msl->memseg_arr;
725
726                 ms_idx = rte_fbarray_find_next_used(arr, 0);
727                 while (ms_idx >= 0) {
728                         ms = rte_fbarray_get(arr, ms_idx);
729                         ret = func(msl, ms, arg);
730                         if (ret)
731                                 return ret;
732                         ms_idx = rte_fbarray_find_next_used(arr, ms_idx + 1);
733                 }
734         }
735         return 0;
736 }
737
738 int
739 rte_memseg_walk(rte_memseg_walk_t func, void *arg)
740 {
741         int ret = 0;
742
743         /* do not allow allocations/frees/init while we iterate */
744         rte_mcfg_mem_read_lock();
745         ret = rte_memseg_walk_thread_unsafe(func, arg);
746         rte_mcfg_mem_read_unlock();
747
748         return ret;
749 }
750
751 int
752 rte_memseg_list_walk_thread_unsafe(rte_memseg_list_walk_t func, void *arg)
753 {
754         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
755         int i, ret = 0;
756
757         for (i = 0; i < RTE_MAX_MEMSEG_LISTS; i++) {
758                 struct rte_memseg_list *msl = &mcfg->memsegs[i];
759
760                 if (msl->base_va == NULL)
761                         continue;
762
763                 ret = func(msl, arg);
764                 if (ret)
765                         return ret;
766         }
767         return 0;
768 }
769
770 int
771 rte_memseg_list_walk(rte_memseg_list_walk_t func, void *arg)
772 {
773         int ret = 0;
774
775         /* do not allow allocations/frees/init while we iterate */
776         rte_mcfg_mem_read_lock();
777         ret = rte_memseg_list_walk_thread_unsafe(func, arg);
778         rte_mcfg_mem_read_unlock();
779
780         return ret;
781 }
782
783 int
784 rte_memseg_get_fd_thread_unsafe(const struct rte_memseg *ms)
785 {
786         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
787         struct rte_memseg_list *msl;
788         struct rte_fbarray *arr;
789         int msl_idx, seg_idx, ret;
790
791         if (ms == NULL) {
792                 rte_errno = EINVAL;
793                 return -1;
794         }
795
796         msl = rte_mem_virt2memseg_list(ms->addr);
797         if (msl == NULL) {
798                 rte_errno = EINVAL;
799                 return -1;
800         }
801         arr = &msl->memseg_arr;
802
803         msl_idx = msl - mcfg->memsegs;
804         seg_idx = rte_fbarray_find_idx(arr, ms);
805
806         if (!rte_fbarray_is_used(arr, seg_idx)) {
807                 rte_errno = ENOENT;
808                 return -1;
809         }
810
811         /* segment fd API is not supported for external segments */
812         if (msl->external) {
813                 rte_errno = ENOTSUP;
814                 return -1;
815         }
816
817         ret = eal_memalloc_get_seg_fd(msl_idx, seg_idx);
818         if (ret < 0) {
819                 rte_errno = -ret;
820                 ret = -1;
821         }
822         return ret;
823 }
824
825 int
826 rte_memseg_get_fd(const struct rte_memseg *ms)
827 {
828         int ret;
829
830         rte_mcfg_mem_read_lock();
831         ret = rte_memseg_get_fd_thread_unsafe(ms);
832         rte_mcfg_mem_read_unlock();
833
834         return ret;
835 }
836
837 int
838 rte_memseg_get_fd_offset_thread_unsafe(const struct rte_memseg *ms,
839                 size_t *offset)
840 {
841         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
842         struct rte_memseg_list *msl;
843         struct rte_fbarray *arr;
844         int msl_idx, seg_idx, ret;
845
846         if (ms == NULL || offset == NULL) {
847                 rte_errno = EINVAL;
848                 return -1;
849         }
850
851         msl = rte_mem_virt2memseg_list(ms->addr);
852         if (msl == NULL) {
853                 rte_errno = EINVAL;
854                 return -1;
855         }
856         arr = &msl->memseg_arr;
857
858         msl_idx = msl - mcfg->memsegs;
859         seg_idx = rte_fbarray_find_idx(arr, ms);
860
861         if (!rte_fbarray_is_used(arr, seg_idx)) {
862                 rte_errno = ENOENT;
863                 return -1;
864         }
865
866         /* segment fd API is not supported for external segments */
867         if (msl->external) {
868                 rte_errno = ENOTSUP;
869                 return -1;
870         }
871
872         ret = eal_memalloc_get_seg_fd_offset(msl_idx, seg_idx, offset);
873         if (ret < 0) {
874                 rte_errno = -ret;
875                 ret = -1;
876         }
877         return ret;
878 }
879
880 int
881 rte_memseg_get_fd_offset(const struct rte_memseg *ms, size_t *offset)
882 {
883         int ret;
884
885         rte_mcfg_mem_read_lock();
886         ret = rte_memseg_get_fd_offset_thread_unsafe(ms, offset);
887         rte_mcfg_mem_read_unlock();
888
889         return ret;
890 }
891
892 int
893 rte_extmem_register(void *va_addr, size_t len, rte_iova_t iova_addrs[],
894                 unsigned int n_pages, size_t page_sz)
895 {
896         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
897         unsigned int socket_id, n;
898         int ret = 0;
899
900         if (va_addr == NULL || page_sz == 0 || len == 0 ||
901                         !rte_is_power_of_2(page_sz) ||
902                         RTE_ALIGN(len, page_sz) != len ||
903                         ((len / page_sz) != n_pages && iova_addrs != NULL) ||
904                         !rte_is_aligned(va_addr, page_sz)) {
905                 rte_errno = EINVAL;
906                 return -1;
907         }
908         rte_mcfg_mem_write_lock();
909
910         /* make sure the segment doesn't already exist */
911         if (malloc_heap_find_external_seg(va_addr, len) != NULL) {
912                 rte_errno = EEXIST;
913                 ret = -1;
914                 goto unlock;
915         }
916
917         /* get next available socket ID */
918         socket_id = mcfg->next_socket_id;
919         if (socket_id > INT32_MAX) {
920                 RTE_LOG(ERR, EAL, "Cannot assign new socket ID's\n");
921                 rte_errno = ENOSPC;
922                 ret = -1;
923                 goto unlock;
924         }
925
926         /* we can create a new memseg */
927         n = len / page_sz;
928         if (malloc_heap_create_external_seg(va_addr, iova_addrs, n,
929                         page_sz, "extmem", socket_id) == NULL) {
930                 ret = -1;
931                 goto unlock;
932         }
933
934         /* memseg list successfully created - increment next socket ID */
935         mcfg->next_socket_id++;
936 unlock:
937         rte_mcfg_mem_write_unlock();
938         return ret;
939 }
940
941 int
942 rte_extmem_unregister(void *va_addr, size_t len)
943 {
944         struct rte_memseg_list *msl;
945         int ret = 0;
946
947         if (va_addr == NULL || len == 0) {
948                 rte_errno = EINVAL;
949                 return -1;
950         }
951         rte_mcfg_mem_write_lock();
952
953         /* find our segment */
954         msl = malloc_heap_find_external_seg(va_addr, len);
955         if (msl == NULL) {
956                 rte_errno = ENOENT;
957                 ret = -1;
958                 goto unlock;
959         }
960
961         ret = malloc_heap_destroy_external_seg(msl);
962 unlock:
963         rte_mcfg_mem_write_unlock();
964         return ret;
965 }
966
967 static int
968 sync_memory(void *va_addr, size_t len, bool attach)
969 {
970         struct rte_memseg_list *msl;
971         int ret = 0;
972
973         if (va_addr == NULL || len == 0) {
974                 rte_errno = EINVAL;
975                 return -1;
976         }
977         rte_mcfg_mem_write_lock();
978
979         /* find our segment */
980         msl = malloc_heap_find_external_seg(va_addr, len);
981         if (msl == NULL) {
982                 rte_errno = ENOENT;
983                 ret = -1;
984                 goto unlock;
985         }
986         if (attach)
987                 ret = rte_fbarray_attach(&msl->memseg_arr);
988         else
989                 ret = rte_fbarray_detach(&msl->memseg_arr);
990
991 unlock:
992         rte_mcfg_mem_write_unlock();
993         return ret;
994 }
995
996 int
997 rte_extmem_attach(void *va_addr, size_t len)
998 {
999         return sync_memory(va_addr, len, true);
1000 }
1001
1002 int
1003 rte_extmem_detach(void *va_addr, size_t len)
1004 {
1005         return sync_memory(va_addr, len, false);
1006 }
1007
1008 /* detach all EAL memory */
1009 int
1010 rte_eal_memory_detach(void)
1011 {
1012         const struct internal_config *internal_conf =
1013                 eal_get_internal_configuration();
1014         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
1015         size_t page_sz = rte_mem_page_size();
1016         unsigned int i;
1017
1018         if (internal_conf->in_memory == 1)
1019                 return 0;
1020
1021         rte_rwlock_write_lock(&mcfg->memory_hotplug_lock);
1022
1023         /* detach internal memory subsystem data first */
1024         if (eal_memalloc_cleanup())
1025                 RTE_LOG(ERR, EAL, "Could not release memory subsystem data\n");
1026
1027         for (i = 0; i < RTE_DIM(mcfg->memsegs); i++) {
1028                 struct rte_memseg_list *msl = &mcfg->memsegs[i];
1029
1030                 /* skip uninitialized segments */
1031                 if (msl->base_va == NULL)
1032                         continue;
1033                 /*
1034                  * external segments are supposed to be detached at this point,
1035                  * but if they aren't, we can't really do anything about it,
1036                  * because if we skip them here, they'll become invalid after
1037                  * we unmap the memconfig anyway. however, if this is externally
1038                  * referenced memory, we have no business unmapping it.
1039                  */
1040                 if (!msl->external)
1041                         if (rte_mem_unmap(msl->base_va, msl->len) != 0)
1042                                 RTE_LOG(ERR, EAL, "Could not unmap memory: %s\n",
1043                                                 rte_strerror(rte_errno));
1044
1045                 /*
1046                  * we are detaching the fbarray rather than destroying because
1047                  * other processes might still reference this fbarray, and we
1048                  * have no way of knowing if they still do.
1049                  */
1050                 if (rte_fbarray_detach(&msl->memseg_arr))
1051                         RTE_LOG(ERR, EAL, "Could not detach fbarray: %s\n",
1052                                         rte_strerror(rte_errno));
1053         }
1054         rte_rwlock_write_unlock(&mcfg->memory_hotplug_lock);
1055
1056         /*
1057          * we've detached the memseg lists, so we can unmap the shared mem
1058          * config - we can't zero it out because it might still be referenced
1059          * by other processes.
1060          */
1061         if (internal_conf->no_shconf == 0 && mcfg->mem_cfg_addr != 0) {
1062                 if (rte_mem_unmap(mcfg, RTE_ALIGN(sizeof(*mcfg), page_sz)) != 0)
1063                         RTE_LOG(ERR, EAL, "Could not unmap shared memory config: %s\n",
1064                                         rte_strerror(rte_errno));
1065         }
1066         rte_eal_get_configuration()->mem_config = NULL;
1067
1068         return 0;
1069 }
1070
1071 /* init memory subsystem */
1072 int
1073 rte_eal_memory_init(void)
1074 {
1075         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
1076         const struct internal_config *internal_conf =
1077                 eal_get_internal_configuration();
1078
1079         int retval;
1080         RTE_LOG(DEBUG, EAL, "Setting up physically contiguous memory...\n");
1081
1082         if (!mcfg)
1083                 return -1;
1084
1085         /* lock mem hotplug here, to prevent races while we init */
1086         rte_mcfg_mem_read_lock();
1087
1088         if (rte_eal_memseg_init() < 0)
1089                 goto fail;
1090
1091         if (eal_memalloc_init() < 0)
1092                 goto fail;
1093
1094         retval = rte_eal_process_type() == RTE_PROC_PRIMARY ?
1095                         rte_eal_hugepage_init() :
1096                         rte_eal_hugepage_attach();
1097         if (retval < 0)
1098                 goto fail;
1099
1100         if (internal_conf->no_shconf == 0 && rte_eal_memdevice_init() < 0)
1101                 goto fail;
1102
1103         return 0;
1104 fail:
1105         rte_mcfg_mem_read_unlock();
1106         return -1;
1107 }
1108
1109 #ifndef RTE_EXEC_ENV_WINDOWS
1110 #define EAL_MEMZONE_LIST_REQ    "/eal/memzone_list"
1111 #define EAL_MEMZONE_INFO_REQ    "/eal/memzone_info"
1112 #define EAL_HEAP_LIST_REQ       "/eal/heap_list"
1113 #define EAL_HEAP_INFO_REQ       "/eal/heap_info"
1114 #define ADDR_STR                15
1115
1116 /* Telemetry callback handler to return heap stats for requested heap id. */
1117 static int
1118 handle_eal_heap_info_request(const char *cmd __rte_unused, const char *params,
1119                              struct rte_tel_data *d)
1120 {
1121         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
1122         struct rte_malloc_socket_stats sock_stats;
1123         struct malloc_heap *heap;
1124         unsigned int heap_id;
1125
1126         if (params == NULL || strlen(params) == 0)
1127                 return -1;
1128
1129         heap_id = (unsigned int)strtoul(params, NULL, 10);
1130
1131         /* Get the heap stats of user provided heap id */
1132         heap = &mcfg->malloc_heaps[heap_id];
1133         malloc_heap_get_stats(heap, &sock_stats);
1134
1135         rte_tel_data_start_dict(d);
1136         rte_tel_data_add_dict_int(d, "Head id", heap_id);
1137         rte_tel_data_add_dict_string(d, "Name", heap->name);
1138         rte_tel_data_add_dict_u64(d, "Heap_size",
1139                                   sock_stats.heap_totalsz_bytes);
1140         rte_tel_data_add_dict_u64(d, "Free_size", sock_stats.heap_freesz_bytes);
1141         rte_tel_data_add_dict_u64(d, "Alloc_size",
1142                                   sock_stats.heap_allocsz_bytes);
1143         rte_tel_data_add_dict_u64(d, "Greatest_free_size",
1144                                   sock_stats.greatest_free_size);
1145         rte_tel_data_add_dict_u64(d, "Alloc_count", sock_stats.alloc_count);
1146         rte_tel_data_add_dict_u64(d, "Free_count", sock_stats.free_count);
1147
1148         return 0;
1149 }
1150
1151 /* Telemetry callback handler to list the heap ids setup. */
1152 static int
1153 handle_eal_heap_list_request(const char *cmd __rte_unused,
1154                                 const char *params __rte_unused,
1155                                 struct rte_tel_data *d)
1156 {
1157         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
1158         struct rte_malloc_socket_stats sock_stats;
1159         unsigned int heap_id;
1160
1161         rte_tel_data_start_array(d, RTE_TEL_INT_VAL);
1162         /* Iterate through all initialised heaps */
1163         for (heap_id = 0; heap_id < RTE_MAX_HEAPS; heap_id++) {
1164                 struct malloc_heap *heap = &mcfg->malloc_heaps[heap_id];
1165
1166                 malloc_heap_get_stats(heap, &sock_stats);
1167                 if (sock_stats.heap_totalsz_bytes != 0)
1168                         rte_tel_data_add_array_int(d, heap_id);
1169         }
1170
1171         return 0;
1172 }
1173
1174 /* Telemetry callback handler to return memzone info for requested index. */
1175 static int
1176 handle_eal_memzone_info_request(const char *cmd __rte_unused,
1177                                 const char *params, struct rte_tel_data *d)
1178 {
1179         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
1180         struct rte_memseg_list *msl = NULL;
1181         int ms_idx, ms_count = 0;
1182         void *cur_addr, *mz_end;
1183         struct rte_memzone *mz;
1184         struct rte_memseg *ms;
1185         char addr[ADDR_STR];
1186         unsigned int mz_idx;
1187         size_t page_sz;
1188
1189         if (params == NULL || strlen(params) == 0)
1190                 return -1;
1191
1192         mz_idx = strtoul(params, NULL, 10);
1193
1194         /* Get the memzone handle using index */
1195         mz = rte_fbarray_get(&mcfg->memzones, mz_idx);
1196
1197         rte_tel_data_start_dict(d);
1198         rte_tel_data_add_dict_int(d, "Zone", mz_idx);
1199         rte_tel_data_add_dict_string(d, "Name", mz->name);
1200         rte_tel_data_add_dict_int(d, "Length", mz->len);
1201         snprintf(addr, ADDR_STR, "%p", mz->addr);
1202         rte_tel_data_add_dict_string(d, "Address", addr);
1203         rte_tel_data_add_dict_int(d, "Socket", mz->socket_id);
1204         rte_tel_data_add_dict_int(d, "Flags", mz->flags);
1205
1206         /* go through each page occupied by this memzone */
1207         msl = rte_mem_virt2memseg_list(mz->addr);
1208         if (!msl) {
1209                 RTE_LOG(DEBUG, EAL, "Skipping bad memzone\n");
1210                 return -1;
1211         }
1212         page_sz = (size_t)mz->hugepage_sz;
1213         cur_addr = RTE_PTR_ALIGN_FLOOR(mz->addr, page_sz);
1214         mz_end = RTE_PTR_ADD(cur_addr, mz->len);
1215
1216         ms_idx = RTE_PTR_DIFF(mz->addr, msl->base_va) / page_sz;
1217         ms = rte_fbarray_get(&msl->memseg_arr, ms_idx);
1218
1219         rte_tel_data_add_dict_int(d, "Hugepage_size", page_sz);
1220         snprintf(addr, ADDR_STR, "%p", ms->addr);
1221         rte_tel_data_add_dict_string(d, "Hugepage_base", addr);
1222
1223         do {
1224                 /* advance VA to next page */
1225                 cur_addr = RTE_PTR_ADD(cur_addr, page_sz);
1226
1227                 /* memzones occupy contiguous segments */
1228                 ++ms;
1229                 ms_count++;
1230         } while (cur_addr < mz_end);
1231
1232         rte_tel_data_add_dict_int(d, "Hugepage_used", ms_count);
1233
1234         return 0;
1235 }
1236
1237 static void
1238 memzone_list_cb(const struct rte_memzone *mz __rte_unused,
1239                  void *arg __rte_unused)
1240 {
1241         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
1242         struct rte_tel_data *d = arg;
1243         int mz_idx;
1244
1245         mz_idx = rte_fbarray_find_idx(&mcfg->memzones, mz);
1246         rte_tel_data_add_array_int(d, mz_idx);
1247 }
1248
1249
1250 /* Telemetry callback handler to list the memzones reserved. */
1251 static int
1252 handle_eal_memzone_list_request(const char *cmd __rte_unused,
1253                                 const char *params __rte_unused,
1254                                 struct rte_tel_data *d)
1255 {
1256         rte_tel_data_start_array(d, RTE_TEL_INT_VAL);
1257         rte_memzone_walk(memzone_list_cb, d);
1258
1259         return 0;
1260 }
1261
1262 RTE_INIT(memory_telemetry)
1263 {
1264         rte_telemetry_register_cmd(
1265                         EAL_MEMZONE_LIST_REQ, handle_eal_memzone_list_request,
1266                         "List of memzone index reserved. Takes no parameters");
1267         rte_telemetry_register_cmd(
1268                         EAL_MEMZONE_INFO_REQ, handle_eal_memzone_info_request,
1269                         "Returns memzone info. Parameters: int mz_id");
1270         rte_telemetry_register_cmd(
1271                         EAL_HEAP_LIST_REQ, handle_eal_heap_list_request,
1272                         "List of heap index setup. Takes no parameters");
1273         rte_telemetry_register_cmd(
1274                         EAL_HEAP_INFO_REQ, handle_eal_heap_info_request,
1275                         "Returns malloc heap stats. Parameters: int heap_id");
1276 }
1277 #endif