1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
15 #include <sys/queue.h>
17 #include <rte_fbarray.h>
18 #include <rte_memory.h>
20 #include <rte_eal_memconfig.h>
21 #include <rte_errno.h>
24 #include "eal_memalloc.h"
25 #include "eal_private.h"
26 #include "eal_internal_cfg.h"
27 #include "eal_memcfg.h"
28 #include "malloc_heap.h"
31 * Try to mmap *size bytes in /dev/zero. If it is successful, return the
32 * pointer to the mmap'd area and keep *size unmodified. Else, retry
33 * with a smaller zone: decrease *size by hugepage_sz until it reaches
34 * 0. In this case, return NULL. Note: this function returns an address
35 * which is a multiple of hugepage size.
38 #define MEMSEG_LIST_FMT "memseg-%" PRIu64 "k-%i-%i"
40 static void *next_baseaddr;
41 static uint64_t system_page_sz;
43 #define MAX_MMAP_WITH_DEFINED_ADDR_TRIES 5
45 eal_get_virtual_area(void *requested_addr, size_t *size,
46 size_t page_sz, int flags, int mmap_flags)
48 bool addr_is_hint, allow_shrink, unmap, no_align;
50 void *mapped_addr, *aligned_addr;
53 if (system_page_sz == 0)
54 system_page_sz = sysconf(_SC_PAGESIZE);
56 mmap_flags |= MAP_PRIVATE | MAP_ANONYMOUS;
58 RTE_LOG(DEBUG, EAL, "Ask a virtual area of 0x%zx bytes\n", *size);
60 addr_is_hint = (flags & EAL_VIRTUAL_AREA_ADDR_IS_HINT) > 0;
61 allow_shrink = (flags & EAL_VIRTUAL_AREA_ALLOW_SHRINK) > 0;
62 unmap = (flags & EAL_VIRTUAL_AREA_UNMAP) > 0;
64 if (next_baseaddr == NULL && internal_config.base_virtaddr != 0 &&
65 rte_eal_process_type() == RTE_PROC_PRIMARY)
66 next_baseaddr = (void *) internal_config.base_virtaddr;
69 if (next_baseaddr == NULL && internal_config.base_virtaddr == 0 &&
70 rte_eal_process_type() == RTE_PROC_PRIMARY)
71 next_baseaddr = (void *) eal_get_baseaddr();
73 if (requested_addr == NULL && next_baseaddr != NULL) {
74 requested_addr = next_baseaddr;
75 requested_addr = RTE_PTR_ALIGN(requested_addr, page_sz);
79 /* we don't need alignment of resulting pointer in the following cases:
81 * 1. page size is equal to system size
82 * 2. we have a requested address, and it is page-aligned, and we will
83 * be discarding the address if we get a different one.
85 * for all other cases, alignment is potentially necessary.
87 no_align = (requested_addr != NULL &&
88 requested_addr == RTE_PTR_ALIGN(requested_addr, page_sz) &&
90 page_sz == system_page_sz;
93 map_sz = no_align ? *size : *size + page_sz;
94 if (map_sz > SIZE_MAX) {
95 RTE_LOG(ERR, EAL, "Map size too big\n");
100 mapped_addr = mmap(requested_addr, (size_t)map_sz, PROT_READ,
102 if (mapped_addr == MAP_FAILED && allow_shrink)
105 if (mapped_addr != MAP_FAILED && addr_is_hint &&
106 mapped_addr != requested_addr) {
108 next_baseaddr = RTE_PTR_ADD(next_baseaddr, page_sz);
109 if (try <= MAX_MMAP_WITH_DEFINED_ADDR_TRIES) {
110 /* hint was not used. Try with another offset */
111 munmap(mapped_addr, map_sz);
112 mapped_addr = MAP_FAILED;
113 requested_addr = next_baseaddr;
116 } while ((allow_shrink || addr_is_hint) &&
117 mapped_addr == MAP_FAILED && *size > 0);
119 /* align resulting address - if map failed, we will ignore the value
120 * anyway, so no need to add additional checks.
122 aligned_addr = no_align ? mapped_addr :
123 RTE_PTR_ALIGN(mapped_addr, page_sz);
126 RTE_LOG(ERR, EAL, "Cannot get a virtual area of any size: %s\n",
130 } else if (mapped_addr == MAP_FAILED) {
131 RTE_LOG(ERR, EAL, "Cannot get a virtual area: %s\n",
133 /* pass errno up the call chain */
136 } else if (requested_addr != NULL && !addr_is_hint &&
137 aligned_addr != requested_addr) {
138 RTE_LOG(ERR, EAL, "Cannot get a virtual area at requested address: %p (got %p)\n",
139 requested_addr, aligned_addr);
140 munmap(mapped_addr, map_sz);
141 rte_errno = EADDRNOTAVAIL;
143 } else if (requested_addr != NULL && addr_is_hint &&
144 aligned_addr != requested_addr) {
145 RTE_LOG(WARNING, EAL, "WARNING! Base virtual address hint (%p != %p) not respected!\n",
146 requested_addr, aligned_addr);
147 RTE_LOG(WARNING, EAL, " This may cause issues with mapping memory into secondary processes\n");
148 } else if (next_baseaddr != NULL) {
149 next_baseaddr = RTE_PTR_ADD(aligned_addr, *size);
152 RTE_LOG(DEBUG, EAL, "Virtual area found at %p (size = 0x%zx)\n",
153 aligned_addr, *size);
156 munmap(mapped_addr, map_sz);
157 } else if (!no_align) {
158 void *map_end, *aligned_end;
159 size_t before_len, after_len;
161 /* when we reserve space with alignment, we add alignment to
162 * mapping size. On 32-bit, if 1GB alignment was requested, this
163 * would waste 1GB of address space, which is a luxury we cannot
164 * afford. so, if alignment was performed, check if any unneeded
165 * address space can be unmapped back.
168 map_end = RTE_PTR_ADD(mapped_addr, (size_t)map_sz);
169 aligned_end = RTE_PTR_ADD(aligned_addr, *size);
171 /* unmap space before aligned mmap address */
172 before_len = RTE_PTR_DIFF(aligned_addr, mapped_addr);
174 munmap(mapped_addr, before_len);
176 /* unmap space after aligned end mmap address */
177 after_len = RTE_PTR_DIFF(map_end, aligned_end);
179 munmap(aligned_end, after_len);
185 static struct rte_memseg *
186 virt2memseg(const void *addr, const struct rte_memseg_list *msl)
188 const struct rte_fbarray *arr;
195 /* a memseg list was specified, check if it's the right one */
196 start = msl->base_va;
197 end = RTE_PTR_ADD(start, msl->len);
199 if (addr < start || addr >= end)
202 /* now, calculate index */
203 arr = &msl->memseg_arr;
204 ms_idx = RTE_PTR_DIFF(addr, msl->base_va) / msl->page_sz;
205 return rte_fbarray_get(arr, ms_idx);
208 static struct rte_memseg_list *
209 virt2memseg_list(const void *addr)
211 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
212 struct rte_memseg_list *msl;
215 for (msl_idx = 0; msl_idx < RTE_MAX_MEMSEG_LISTS; msl_idx++) {
217 msl = &mcfg->memsegs[msl_idx];
219 start = msl->base_va;
220 end = RTE_PTR_ADD(start, msl->len);
221 if (addr >= start && addr < end)
224 /* if we didn't find our memseg list */
225 if (msl_idx == RTE_MAX_MEMSEG_LISTS)
230 struct rte_memseg_list *
231 rte_mem_virt2memseg_list(const void *addr)
233 return virt2memseg_list(addr);
241 find_virt(const struct rte_memseg_list *msl __rte_unused,
242 const struct rte_memseg *ms, void *arg)
244 struct virtiova *vi = arg;
245 if (vi->iova >= ms->iova && vi->iova < (ms->iova + ms->len)) {
246 size_t offset = vi->iova - ms->iova;
247 vi->virt = RTE_PTR_ADD(ms->addr, offset);
254 find_virt_legacy(const struct rte_memseg_list *msl __rte_unused,
255 const struct rte_memseg *ms, size_t len, void *arg)
257 struct virtiova *vi = arg;
258 if (vi->iova >= ms->iova && vi->iova < (ms->iova + len)) {
259 size_t offset = vi->iova - ms->iova;
260 vi->virt = RTE_PTR_ADD(ms->addr, offset);
268 rte_mem_iova2virt(rte_iova_t iova)
272 memset(&vi, 0, sizeof(vi));
275 /* for legacy mem, we can get away with scanning VA-contiguous segments,
276 * as we know they are PA-contiguous as well
278 if (internal_config.legacy_mem)
279 rte_memseg_contig_walk(find_virt_legacy, &vi);
281 rte_memseg_walk(find_virt, &vi);
287 rte_mem_virt2memseg(const void *addr, const struct rte_memseg_list *msl)
289 return virt2memseg(addr, msl != NULL ? msl :
290 rte_mem_virt2memseg_list(addr));
294 physmem_size(const struct rte_memseg_list *msl, void *arg)
296 uint64_t *total_len = arg;
301 *total_len += msl->memseg_arr.count * msl->page_sz;
306 /* get the total size of memory */
308 rte_eal_get_physmem_size(void)
310 uint64_t total_len = 0;
312 rte_memseg_list_walk(physmem_size, &total_len);
318 dump_memseg(const struct rte_memseg_list *msl, const struct rte_memseg *ms,
321 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
322 int msl_idx, ms_idx, fd;
325 msl_idx = msl - mcfg->memsegs;
326 if (msl_idx < 0 || msl_idx >= RTE_MAX_MEMSEG_LISTS)
329 ms_idx = rte_fbarray_find_idx(&msl->memseg_arr, ms);
333 fd = eal_memalloc_get_seg_fd(msl_idx, ms_idx);
334 fprintf(f, "Segment %i-%i: IOVA:0x%"PRIx64", len:%zu, "
335 "virt:%p, socket_id:%"PRId32", "
336 "hugepage_sz:%"PRIu64", nchannel:%"PRIx32", "
337 "nrank:%"PRIx32" fd:%i\n",
352 * Defining here because declared in rte_memory.h, but the actual implementation
353 * is in eal_common_memalloc.c, like all other memalloc internals.
356 rte_mem_event_callback_register(const char *name, rte_mem_event_callback_t clb,
359 /* FreeBSD boots with legacy mem enabled by default */
360 if (internal_config.legacy_mem) {
361 RTE_LOG(DEBUG, EAL, "Registering mem event callbacks not supported\n");
365 return eal_memalloc_mem_event_callback_register(name, clb, arg);
369 rte_mem_event_callback_unregister(const char *name, void *arg)
371 /* FreeBSD boots with legacy mem enabled by default */
372 if (internal_config.legacy_mem) {
373 RTE_LOG(DEBUG, EAL, "Registering mem event callbacks not supported\n");
377 return eal_memalloc_mem_event_callback_unregister(name, arg);
381 rte_mem_alloc_validator_register(const char *name,
382 rte_mem_alloc_validator_t clb, int socket_id, size_t limit)
384 /* FreeBSD boots with legacy mem enabled by default */
385 if (internal_config.legacy_mem) {
386 RTE_LOG(DEBUG, EAL, "Registering mem alloc validators not supported\n");
390 return eal_memalloc_mem_alloc_validator_register(name, clb, socket_id,
395 rte_mem_alloc_validator_unregister(const char *name, int socket_id)
397 /* FreeBSD boots with legacy mem enabled by default */
398 if (internal_config.legacy_mem) {
399 RTE_LOG(DEBUG, EAL, "Registering mem alloc validators not supported\n");
403 return eal_memalloc_mem_alloc_validator_unregister(name, socket_id);
406 /* Dump the physical memory layout on console */
408 rte_dump_physmem_layout(FILE *f)
410 rte_memseg_walk(dump_memseg, f);
414 check_iova(const struct rte_memseg_list *msl __rte_unused,
415 const struct rte_memseg *ms, void *arg)
417 uint64_t *mask = arg;
420 /* higher address within segment */
421 iova = (ms->iova + ms->len) - 1;
425 RTE_LOG(DEBUG, EAL, "memseg iova %"PRIx64", len %zx, out of range\n",
428 RTE_LOG(DEBUG, EAL, "\tusing dma mask %"PRIx64"\n", *mask);
432 #define MAX_DMA_MASK_BITS 63
434 /* check memseg iovas are within the required range based on dma mask */
436 check_dma_mask(uint8_t maskbits, bool thread_unsafe)
438 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
442 /* Sanity check. We only check width can be managed with 64 bits
443 * variables. Indeed any higher value is likely wrong. */
444 if (maskbits > MAX_DMA_MASK_BITS) {
445 RTE_LOG(ERR, EAL, "wrong dma mask size %u (Max: %u)\n",
446 maskbits, MAX_DMA_MASK_BITS);
450 /* create dma mask */
451 mask = ~((1ULL << maskbits) - 1);
454 ret = rte_memseg_walk_thread_unsafe(check_iova, &mask);
456 ret = rte_memseg_walk(check_iova, &mask);
460 * Dma mask precludes hugepage usage.
461 * This device can not be used and we do not need to keep
467 * we need to keep the more restricted maskbit for checking
468 * potential dynamic memory allocation in the future.
470 mcfg->dma_maskbits = mcfg->dma_maskbits == 0 ? maskbits :
471 RTE_MIN(mcfg->dma_maskbits, maskbits);
477 rte_mem_check_dma_mask(uint8_t maskbits)
479 return check_dma_mask(maskbits, false);
483 rte_mem_check_dma_mask_thread_unsafe(uint8_t maskbits)
485 return check_dma_mask(maskbits, true);
489 * Set dma mask to use when memory initialization is done.
491 * This function should ONLY be used by code executed before the memory
492 * initialization. PMDs should use rte_mem_check_dma_mask if addressing
493 * limitations by the device.
496 rte_mem_set_dma_mask(uint8_t maskbits)
498 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
500 mcfg->dma_maskbits = mcfg->dma_maskbits == 0 ? maskbits :
501 RTE_MIN(mcfg->dma_maskbits, maskbits);
504 /* return the number of memory channels */
505 unsigned rte_memory_get_nchannel(void)
507 return rte_eal_get_configuration()->mem_config->nchannel;
510 /* return the number of memory rank */
511 unsigned rte_memory_get_nrank(void)
513 return rte_eal_get_configuration()->mem_config->nrank;
517 rte_eal_memdevice_init(void)
519 struct rte_config *config;
521 if (rte_eal_process_type() == RTE_PROC_SECONDARY)
524 config = rte_eal_get_configuration();
525 config->mem_config->nchannel = internal_config.force_nchannel;
526 config->mem_config->nrank = internal_config.force_nrank;
531 /* Lock page in physical memory and prevent from swapping. */
533 rte_mem_lock_page(const void *virt)
535 unsigned long virtual = (unsigned long)virt;
536 int page_size = getpagesize();
537 unsigned long aligned = (virtual & ~(page_size - 1));
538 return mlock((void *)aligned, page_size);
542 rte_memseg_contig_walk_thread_unsafe(rte_memseg_contig_walk_t func, void *arg)
544 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
545 int i, ms_idx, ret = 0;
547 for (i = 0; i < RTE_MAX_MEMSEG_LISTS; i++) {
548 struct rte_memseg_list *msl = &mcfg->memsegs[i];
549 const struct rte_memseg *ms;
550 struct rte_fbarray *arr;
552 if (msl->memseg_arr.count == 0)
555 arr = &msl->memseg_arr;
557 ms_idx = rte_fbarray_find_next_used(arr, 0);
558 while (ms_idx >= 0) {
562 ms = rte_fbarray_get(arr, ms_idx);
564 /* find how many more segments there are, starting with
567 n_segs = rte_fbarray_find_contig_used(arr, ms_idx);
568 len = n_segs * msl->page_sz;
570 ret = func(msl, ms, len, arg);
573 ms_idx = rte_fbarray_find_next_used(arr,
581 rte_memseg_contig_walk(rte_memseg_contig_walk_t func, void *arg)
585 /* do not allow allocations/frees/init while we iterate */
586 rte_mcfg_mem_read_lock();
587 ret = rte_memseg_contig_walk_thread_unsafe(func, arg);
588 rte_mcfg_mem_read_unlock();
594 rte_memseg_walk_thread_unsafe(rte_memseg_walk_t func, void *arg)
596 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
597 int i, ms_idx, ret = 0;
599 for (i = 0; i < RTE_MAX_MEMSEG_LISTS; i++) {
600 struct rte_memseg_list *msl = &mcfg->memsegs[i];
601 const struct rte_memseg *ms;
602 struct rte_fbarray *arr;
604 if (msl->memseg_arr.count == 0)
607 arr = &msl->memseg_arr;
609 ms_idx = rte_fbarray_find_next_used(arr, 0);
610 while (ms_idx >= 0) {
611 ms = rte_fbarray_get(arr, ms_idx);
612 ret = func(msl, ms, arg);
615 ms_idx = rte_fbarray_find_next_used(arr, ms_idx + 1);
622 rte_memseg_walk(rte_memseg_walk_t func, void *arg)
626 /* do not allow allocations/frees/init while we iterate */
627 rte_mcfg_mem_read_lock();
628 ret = rte_memseg_walk_thread_unsafe(func, arg);
629 rte_mcfg_mem_read_unlock();
635 rte_memseg_list_walk_thread_unsafe(rte_memseg_list_walk_t func, void *arg)
637 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
640 for (i = 0; i < RTE_MAX_MEMSEG_LISTS; i++) {
641 struct rte_memseg_list *msl = &mcfg->memsegs[i];
643 if (msl->base_va == NULL)
646 ret = func(msl, arg);
654 rte_memseg_list_walk(rte_memseg_list_walk_t func, void *arg)
658 /* do not allow allocations/frees/init while we iterate */
659 rte_mcfg_mem_read_lock();
660 ret = rte_memseg_list_walk_thread_unsafe(func, arg);
661 rte_mcfg_mem_read_unlock();
667 rte_memseg_get_fd_thread_unsafe(const struct rte_memseg *ms)
669 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
670 struct rte_memseg_list *msl;
671 struct rte_fbarray *arr;
672 int msl_idx, seg_idx, ret;
679 msl = rte_mem_virt2memseg_list(ms->addr);
684 arr = &msl->memseg_arr;
686 msl_idx = msl - mcfg->memsegs;
687 seg_idx = rte_fbarray_find_idx(arr, ms);
689 if (!rte_fbarray_is_used(arr, seg_idx)) {
694 /* segment fd API is not supported for external segments */
700 ret = eal_memalloc_get_seg_fd(msl_idx, seg_idx);
709 rte_memseg_get_fd(const struct rte_memseg *ms)
713 rte_mcfg_mem_read_lock();
714 ret = rte_memseg_get_fd_thread_unsafe(ms);
715 rte_mcfg_mem_read_unlock();
721 rte_memseg_get_fd_offset_thread_unsafe(const struct rte_memseg *ms,
724 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
725 struct rte_memseg_list *msl;
726 struct rte_fbarray *arr;
727 int msl_idx, seg_idx, ret;
729 if (ms == NULL || offset == NULL) {
734 msl = rte_mem_virt2memseg_list(ms->addr);
739 arr = &msl->memseg_arr;
741 msl_idx = msl - mcfg->memsegs;
742 seg_idx = rte_fbarray_find_idx(arr, ms);
744 if (!rte_fbarray_is_used(arr, seg_idx)) {
749 /* segment fd API is not supported for external segments */
755 ret = eal_memalloc_get_seg_fd_offset(msl_idx, seg_idx, offset);
764 rte_memseg_get_fd_offset(const struct rte_memseg *ms, size_t *offset)
768 rte_mcfg_mem_read_lock();
769 ret = rte_memseg_get_fd_offset_thread_unsafe(ms, offset);
770 rte_mcfg_mem_read_unlock();
776 rte_extmem_register(void *va_addr, size_t len, rte_iova_t iova_addrs[],
777 unsigned int n_pages, size_t page_sz)
779 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
780 unsigned int socket_id, n;
783 if (va_addr == NULL || page_sz == 0 || len == 0 ||
784 !rte_is_power_of_2(page_sz) ||
785 RTE_ALIGN(len, page_sz) != len ||
786 ((len / page_sz) != n_pages && iova_addrs != NULL) ||
787 !rte_is_aligned(va_addr, page_sz)) {
791 rte_mcfg_mem_write_lock();
793 /* make sure the segment doesn't already exist */
794 if (malloc_heap_find_external_seg(va_addr, len) != NULL) {
800 /* get next available socket ID */
801 socket_id = mcfg->next_socket_id;
802 if (socket_id > INT32_MAX) {
803 RTE_LOG(ERR, EAL, "Cannot assign new socket ID's\n");
809 /* we can create a new memseg */
811 if (malloc_heap_create_external_seg(va_addr, iova_addrs, n,
812 page_sz, "extmem", socket_id) == NULL) {
817 /* memseg list successfully created - increment next socket ID */
818 mcfg->next_socket_id++;
820 rte_mcfg_mem_write_unlock();
825 rte_extmem_unregister(void *va_addr, size_t len)
827 struct rte_memseg_list *msl;
830 if (va_addr == NULL || len == 0) {
834 rte_mcfg_mem_write_lock();
836 /* find our segment */
837 msl = malloc_heap_find_external_seg(va_addr, len);
844 ret = malloc_heap_destroy_external_seg(msl);
846 rte_mcfg_mem_write_unlock();
851 sync_memory(void *va_addr, size_t len, bool attach)
853 struct rte_memseg_list *msl;
856 if (va_addr == NULL || len == 0) {
860 rte_mcfg_mem_write_lock();
862 /* find our segment */
863 msl = malloc_heap_find_external_seg(va_addr, len);
870 ret = rte_fbarray_attach(&msl->memseg_arr);
872 ret = rte_fbarray_detach(&msl->memseg_arr);
875 rte_mcfg_mem_write_unlock();
880 rte_extmem_attach(void *va_addr, size_t len)
882 return sync_memory(va_addr, len, true);
886 rte_extmem_detach(void *va_addr, size_t len)
888 return sync_memory(va_addr, len, false);
891 /* init memory subsystem */
893 rte_eal_memory_init(void)
895 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
897 RTE_LOG(DEBUG, EAL, "Setting up physically contiguous memory...\n");
902 /* lock mem hotplug here, to prevent races while we init */
903 rte_mcfg_mem_read_lock();
905 if (rte_eal_memseg_init() < 0)
908 if (eal_memalloc_init() < 0)
911 retval = rte_eal_process_type() == RTE_PROC_PRIMARY ?
912 rte_eal_hugepage_init() :
913 rte_eal_hugepage_attach();
917 if (internal_config.no_shconf == 0 && rte_eal_memdevice_init() < 0)
922 rte_mcfg_mem_read_unlock();