1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
14 #include <sys/queue.h>
16 #include <rte_fbarray.h>
17 #include <rte_memory.h>
19 #include <rte_eal_memconfig.h>
20 #include <rte_errno.h>
23 #include "eal_memalloc.h"
24 #include "eal_private.h"
25 #include "eal_internal_cfg.h"
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.
35 #define MEMSEG_LIST_FMT "memseg-%" PRIu64 "k-%i-%i"
37 static void *next_baseaddr;
38 static uint64_t system_page_sz;
41 eal_get_virtual_area(void *requested_addr, size_t *size,
42 size_t page_sz, int flags, int mmap_flags)
44 bool addr_is_hint, allow_shrink, unmap, no_align;
46 void *mapped_addr, *aligned_addr;
48 if (system_page_sz == 0)
49 system_page_sz = sysconf(_SC_PAGESIZE);
51 mmap_flags |= MAP_PRIVATE | MAP_ANONYMOUS;
53 RTE_LOG(DEBUG, EAL, "Ask a virtual area of 0x%zx bytes\n", *size);
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;
59 if (next_baseaddr == NULL && internal_config.base_virtaddr != 0 &&
60 rte_eal_process_type() == RTE_PROC_PRIMARY)
61 next_baseaddr = (void *) internal_config.base_virtaddr;
63 if (requested_addr == NULL && next_baseaddr != NULL) {
64 requested_addr = next_baseaddr;
65 requested_addr = RTE_PTR_ALIGN(requested_addr, page_sz);
69 /* we don't need alignment of resulting pointer in the following cases:
71 * 1. page size is equal to system size
72 * 2. we have a requested address, and it is page-aligned, and we will
73 * be discarding the address if we get a different one.
75 * for all other cases, alignment is potentially necessary.
77 no_align = (requested_addr != NULL &&
78 requested_addr == RTE_PTR_ALIGN(requested_addr, page_sz) &&
80 page_sz == system_page_sz;
83 map_sz = no_align ? *size : *size + page_sz;
84 if (map_sz > SIZE_MAX) {
85 RTE_LOG(ERR, EAL, "Map size too big\n");
90 mapped_addr = mmap(requested_addr, (size_t)map_sz, PROT_READ,
92 if (mapped_addr == MAP_FAILED && allow_shrink)
94 } while (allow_shrink && mapped_addr == MAP_FAILED && *size > 0);
96 /* align resulting address - if map failed, we will ignore the value
97 * anyway, so no need to add additional checks.
99 aligned_addr = no_align ? mapped_addr :
100 RTE_PTR_ALIGN(mapped_addr, page_sz);
103 RTE_LOG(ERR, EAL, "Cannot get a virtual area of any size: %s\n",
107 } else if (mapped_addr == MAP_FAILED) {
108 RTE_LOG(ERR, EAL, "Cannot get a virtual area: %s\n",
110 /* pass errno up the call chain */
113 } else if (requested_addr != NULL && !addr_is_hint &&
114 aligned_addr != requested_addr) {
115 RTE_LOG(ERR, EAL, "Cannot get a virtual area at requested address: %p (got %p)\n",
116 requested_addr, aligned_addr);
117 munmap(mapped_addr, map_sz);
118 rte_errno = EADDRNOTAVAIL;
120 } else if (requested_addr != NULL && addr_is_hint &&
121 aligned_addr != requested_addr) {
122 RTE_LOG(WARNING, EAL, "WARNING! Base virtual address hint (%p != %p) not respected!\n",
123 requested_addr, aligned_addr);
124 RTE_LOG(WARNING, EAL, " This may cause issues with mapping memory into secondary processes\n");
125 } else if (next_baseaddr != NULL) {
126 next_baseaddr = RTE_PTR_ADD(aligned_addr, *size);
129 RTE_LOG(DEBUG, EAL, "Virtual area found at %p (size = 0x%zx)\n",
130 aligned_addr, *size);
133 munmap(mapped_addr, map_sz);
134 } else if (!no_align) {
135 void *map_end, *aligned_end;
136 size_t before_len, after_len;
138 /* when we reserve space with alignment, we add alignment to
139 * mapping size. On 32-bit, if 1GB alignment was requested, this
140 * would waste 1GB of address space, which is a luxury we cannot
141 * afford. so, if alignment was performed, check if any unneeded
142 * address space can be unmapped back.
145 map_end = RTE_PTR_ADD(mapped_addr, (size_t)map_sz);
146 aligned_end = RTE_PTR_ADD(aligned_addr, *size);
148 /* unmap space before aligned mmap address */
149 before_len = RTE_PTR_DIFF(aligned_addr, mapped_addr);
151 munmap(mapped_addr, before_len);
153 /* unmap space after aligned end mmap address */
154 after_len = RTE_PTR_DIFF(map_end, aligned_end);
156 munmap(aligned_end, after_len);
162 static struct rte_memseg *
163 virt2memseg(const void *addr, const struct rte_memseg_list *msl)
165 const struct rte_fbarray *arr;
172 /* a memseg list was specified, check if it's the right one */
173 start = msl->base_va;
174 end = RTE_PTR_ADD(start, msl->len);
176 if (addr < start || addr >= end)
179 /* now, calculate index */
180 arr = &msl->memseg_arr;
181 ms_idx = RTE_PTR_DIFF(addr, msl->base_va) / msl->page_sz;
182 return rte_fbarray_get(arr, ms_idx);
185 static struct rte_memseg_list *
186 virt2memseg_list(const void *addr)
188 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
189 struct rte_memseg_list *msl;
192 for (msl_idx = 0; msl_idx < RTE_MAX_MEMSEG_LISTS; msl_idx++) {
194 msl = &mcfg->memsegs[msl_idx];
196 start = msl->base_va;
197 end = RTE_PTR_ADD(start, msl->len);
198 if (addr >= start && addr < end)
201 /* if we didn't find our memseg list */
202 if (msl_idx == RTE_MAX_MEMSEG_LISTS)
207 __rte_experimental struct rte_memseg_list *
208 rte_mem_virt2memseg_list(const void *addr)
210 return virt2memseg_list(addr);
218 find_virt(const struct rte_memseg_list *msl __rte_unused,
219 const struct rte_memseg *ms, void *arg)
221 struct virtiova *vi = arg;
222 if (vi->iova >= ms->iova && vi->iova < (ms->iova + ms->len)) {
223 size_t offset = vi->iova - ms->iova;
224 vi->virt = RTE_PTR_ADD(ms->addr, offset);
231 find_virt_legacy(const struct rte_memseg_list *msl __rte_unused,
232 const struct rte_memseg *ms, size_t len, void *arg)
234 struct virtiova *vi = arg;
235 if (vi->iova >= ms->iova && vi->iova < (ms->iova + len)) {
236 size_t offset = vi->iova - ms->iova;
237 vi->virt = RTE_PTR_ADD(ms->addr, offset);
244 __rte_experimental void *
245 rte_mem_iova2virt(rte_iova_t iova)
249 memset(&vi, 0, sizeof(vi));
252 /* for legacy mem, we can get away with scanning VA-contiguous segments,
253 * as we know they are PA-contiguous as well
255 if (internal_config.legacy_mem)
256 rte_memseg_contig_walk(find_virt_legacy, &vi);
258 rte_memseg_walk(find_virt, &vi);
263 __rte_experimental struct rte_memseg *
264 rte_mem_virt2memseg(const void *addr, const struct rte_memseg_list *msl)
266 return virt2memseg(addr, msl != NULL ? msl :
267 rte_mem_virt2memseg_list(addr));
271 physmem_size(const struct rte_memseg_list *msl, void *arg)
273 uint64_t *total_len = arg;
278 *total_len += msl->memseg_arr.count * msl->page_sz;
283 /* get the total size of memory */
285 rte_eal_get_physmem_size(void)
287 uint64_t total_len = 0;
289 rte_memseg_list_walk(physmem_size, &total_len);
295 dump_memseg(const struct rte_memseg_list *msl, const struct rte_memseg *ms,
298 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
299 int msl_idx, ms_idx, fd;
302 msl_idx = msl - mcfg->memsegs;
303 if (msl_idx < 0 || msl_idx >= RTE_MAX_MEMSEG_LISTS)
306 ms_idx = rte_fbarray_find_idx(&msl->memseg_arr, ms);
310 fd = eal_memalloc_get_seg_fd(msl_idx, ms_idx);
311 fprintf(f, "Segment %i-%i: IOVA:0x%"PRIx64", len:%zu, "
312 "virt:%p, socket_id:%"PRId32", "
313 "hugepage_sz:%"PRIu64", nchannel:%"PRIx32", "
314 "nrank:%"PRIx32" fd:%i\n",
329 * Defining here because declared in rte_memory.h, but the actual implementation
330 * is in eal_common_memalloc.c, like all other memalloc internals.
332 int __rte_experimental
333 rte_mem_event_callback_register(const char *name, rte_mem_event_callback_t clb,
336 /* FreeBSD boots with legacy mem enabled by default */
337 if (internal_config.legacy_mem) {
338 RTE_LOG(DEBUG, EAL, "Registering mem event callbacks not supported\n");
342 return eal_memalloc_mem_event_callback_register(name, clb, arg);
345 int __rte_experimental
346 rte_mem_event_callback_unregister(const char *name, void *arg)
348 /* FreeBSD boots with legacy mem enabled by default */
349 if (internal_config.legacy_mem) {
350 RTE_LOG(DEBUG, EAL, "Registering mem event callbacks not supported\n");
354 return eal_memalloc_mem_event_callback_unregister(name, arg);
357 int __rte_experimental
358 rte_mem_alloc_validator_register(const char *name,
359 rte_mem_alloc_validator_t clb, int socket_id, size_t limit)
361 /* FreeBSD boots with legacy mem enabled by default */
362 if (internal_config.legacy_mem) {
363 RTE_LOG(DEBUG, EAL, "Registering mem alloc validators not supported\n");
367 return eal_memalloc_mem_alloc_validator_register(name, clb, socket_id,
371 int __rte_experimental
372 rte_mem_alloc_validator_unregister(const char *name, int socket_id)
374 /* FreeBSD boots with legacy mem enabled by default */
375 if (internal_config.legacy_mem) {
376 RTE_LOG(DEBUG, EAL, "Registering mem alloc validators not supported\n");
380 return eal_memalloc_mem_alloc_validator_unregister(name, socket_id);
383 /* Dump the physical memory layout on console */
385 rte_dump_physmem_layout(FILE *f)
387 rte_memseg_walk(dump_memseg, f);
390 /* return the number of memory channels */
391 unsigned rte_memory_get_nchannel(void)
393 return rte_eal_get_configuration()->mem_config->nchannel;
396 /* return the number of memory rank */
397 unsigned rte_memory_get_nrank(void)
399 return rte_eal_get_configuration()->mem_config->nrank;
403 rte_eal_memdevice_init(void)
405 struct rte_config *config;
407 if (rte_eal_process_type() == RTE_PROC_SECONDARY)
410 config = rte_eal_get_configuration();
411 config->mem_config->nchannel = internal_config.force_nchannel;
412 config->mem_config->nrank = internal_config.force_nrank;
417 /* Lock page in physical memory and prevent from swapping. */
419 rte_mem_lock_page(const void *virt)
421 unsigned long virtual = (unsigned long)virt;
422 int page_size = getpagesize();
423 unsigned long aligned = (virtual & ~(page_size - 1));
424 return mlock((void *)aligned, page_size);
427 int __rte_experimental
428 rte_memseg_contig_walk_thread_unsafe(rte_memseg_contig_walk_t func, void *arg)
430 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
431 int i, ms_idx, ret = 0;
433 for (i = 0; i < RTE_MAX_MEMSEG_LISTS; i++) {
434 struct rte_memseg_list *msl = &mcfg->memsegs[i];
435 const struct rte_memseg *ms;
436 struct rte_fbarray *arr;
438 if (msl->memseg_arr.count == 0)
441 arr = &msl->memseg_arr;
443 ms_idx = rte_fbarray_find_next_used(arr, 0);
444 while (ms_idx >= 0) {
448 ms = rte_fbarray_get(arr, ms_idx);
450 /* find how many more segments there are, starting with
453 n_segs = rte_fbarray_find_contig_used(arr, ms_idx);
454 len = n_segs * msl->page_sz;
456 ret = func(msl, ms, len, arg);
459 ms_idx = rte_fbarray_find_next_used(arr,
466 int __rte_experimental
467 rte_memseg_contig_walk(rte_memseg_contig_walk_t func, void *arg)
469 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
472 /* do not allow allocations/frees/init while we iterate */
473 rte_rwlock_read_lock(&mcfg->memory_hotplug_lock);
474 ret = rte_memseg_contig_walk_thread_unsafe(func, arg);
475 rte_rwlock_read_unlock(&mcfg->memory_hotplug_lock);
480 int __rte_experimental
481 rte_memseg_walk_thread_unsafe(rte_memseg_walk_t func, void *arg)
483 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
484 int i, ms_idx, ret = 0;
486 for (i = 0; i < RTE_MAX_MEMSEG_LISTS; i++) {
487 struct rte_memseg_list *msl = &mcfg->memsegs[i];
488 const struct rte_memseg *ms;
489 struct rte_fbarray *arr;
491 if (msl->memseg_arr.count == 0)
494 arr = &msl->memseg_arr;
496 ms_idx = rte_fbarray_find_next_used(arr, 0);
497 while (ms_idx >= 0) {
498 ms = rte_fbarray_get(arr, ms_idx);
499 ret = func(msl, ms, arg);
502 ms_idx = rte_fbarray_find_next_used(arr, ms_idx + 1);
508 int __rte_experimental
509 rte_memseg_walk(rte_memseg_walk_t func, void *arg)
511 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
514 /* do not allow allocations/frees/init while we iterate */
515 rte_rwlock_read_lock(&mcfg->memory_hotplug_lock);
516 ret = rte_memseg_walk_thread_unsafe(func, arg);
517 rte_rwlock_read_unlock(&mcfg->memory_hotplug_lock);
522 int __rte_experimental
523 rte_memseg_list_walk_thread_unsafe(rte_memseg_list_walk_t func, void *arg)
525 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
528 for (i = 0; i < RTE_MAX_MEMSEG_LISTS; i++) {
529 struct rte_memseg_list *msl = &mcfg->memsegs[i];
531 if (msl->base_va == NULL)
534 ret = func(msl, arg);
541 int __rte_experimental
542 rte_memseg_list_walk(rte_memseg_list_walk_t func, void *arg)
544 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
547 /* do not allow allocations/frees/init while we iterate */
548 rte_rwlock_read_lock(&mcfg->memory_hotplug_lock);
549 ret = rte_memseg_list_walk_thread_unsafe(func, arg);
550 rte_rwlock_read_unlock(&mcfg->memory_hotplug_lock);
555 int __rte_experimental
556 rte_memseg_get_fd_thread_unsafe(const struct rte_memseg *ms)
558 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
559 struct rte_memseg_list *msl;
560 struct rte_fbarray *arr;
561 int msl_idx, seg_idx, ret;
568 msl = rte_mem_virt2memseg_list(ms->addr);
573 arr = &msl->memseg_arr;
575 msl_idx = msl - mcfg->memsegs;
576 seg_idx = rte_fbarray_find_idx(arr, ms);
578 if (!rte_fbarray_is_used(arr, seg_idx)) {
583 ret = eal_memalloc_get_seg_fd(msl_idx, seg_idx);
591 int __rte_experimental
592 rte_memseg_get_fd(const struct rte_memseg *ms)
594 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
597 rte_rwlock_read_lock(&mcfg->memory_hotplug_lock);
598 ret = rte_memseg_get_fd_thread_unsafe(ms);
599 rte_rwlock_read_unlock(&mcfg->memory_hotplug_lock);
604 int __rte_experimental
605 rte_memseg_get_fd_offset_thread_unsafe(const struct rte_memseg *ms,
608 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
609 struct rte_memseg_list *msl;
610 struct rte_fbarray *arr;
611 int msl_idx, seg_idx, ret;
613 if (ms == NULL || offset == NULL) {
618 msl = rte_mem_virt2memseg_list(ms->addr);
623 arr = &msl->memseg_arr;
625 msl_idx = msl - mcfg->memsegs;
626 seg_idx = rte_fbarray_find_idx(arr, ms);
628 if (!rte_fbarray_is_used(arr, seg_idx)) {
633 ret = eal_memalloc_get_seg_fd_offset(msl_idx, seg_idx, offset);
641 int __rte_experimental
642 rte_memseg_get_fd_offset(const struct rte_memseg *ms, size_t *offset)
644 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
647 rte_rwlock_read_lock(&mcfg->memory_hotplug_lock);
648 ret = rte_memseg_get_fd_offset_thread_unsafe(ms, offset);
649 rte_rwlock_read_unlock(&mcfg->memory_hotplug_lock);
654 /* init memory subsystem */
656 rte_eal_memory_init(void)
658 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
660 RTE_LOG(DEBUG, EAL, "Setting up physically contiguous memory...\n");
665 /* lock mem hotplug here, to prevent races while we init */
666 rte_rwlock_read_lock(&mcfg->memory_hotplug_lock);
668 if (rte_eal_memseg_init() < 0)
671 if (eal_memalloc_init() < 0)
674 retval = rte_eal_process_type() == RTE_PROC_PRIMARY ?
675 rte_eal_hugepage_init() :
676 rte_eal_hugepage_attach();
680 if (internal_config.no_shconf == 0 && rte_eal_memdevice_init() < 0)
685 rte_rwlock_read_unlock(&mcfg->memory_hotplug_lock);