1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
7 #include <sys/sysctl.h>
14 #include <rte_errno.h>
16 #include <rte_string_fns.h>
18 #include "eal_private.h"
19 #include "eal_internal_cfg.h"
20 #include "eal_filesystem.h"
21 #include "eal_memcfg.h"
22 #include "eal_options.h"
24 #define EAL_PAGE_SIZE (sysconf(_SC_PAGESIZE))
26 uint64_t eal_get_baseaddr(void)
29 * FreeBSD may allocate something in the space we will be mapping things
30 * before we get a chance to do that, so use a base address that's far
31 * away from where malloc() et al usually map things.
33 return 0x1000000000ULL;
37 * Get physical address of any mapped virtual address in the current process.
40 rte_mem_virt2phy(const void *virtaddr)
42 /* XXX not implemented. This function is only used by
43 * rte_mempool_virt2iova() when hugepages are disabled. */
48 rte_mem_virt2iova(const void *virtaddr)
50 return rte_mem_virt2phy(virtaddr);
54 rte_eal_hugepage_init(void)
56 struct rte_mem_config *mcfg;
57 uint64_t total_mem = 0;
59 unsigned int i, j, seg_idx = 0;
61 /* get pointer to global configuration */
62 mcfg = rte_eal_get_configuration()->mem_config;
64 /* for debug purposes, hugetlbfs can be disabled */
65 if (internal_config.no_hugetlbfs) {
66 struct rte_memseg_list *msl;
67 struct rte_fbarray *arr;
68 struct rte_memseg *ms;
72 /* create a memseg list */
73 msl = &mcfg->memsegs[0];
75 page_sz = RTE_PGSIZE_4K;
76 n_segs = internal_config.memory / page_sz;
78 if (rte_fbarray_init(&msl->memseg_arr, "nohugemem", n_segs,
79 sizeof(struct rte_memseg))) {
80 RTE_LOG(ERR, EAL, "Cannot allocate memseg list\n");
84 addr = mmap(NULL, internal_config.memory,
85 PROT_READ | PROT_WRITE,
86 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
87 if (addr == MAP_FAILED) {
88 RTE_LOG(ERR, EAL, "%s: mmap() failed: %s\n", __func__,
93 msl->page_sz = page_sz;
94 msl->len = internal_config.memory;
98 /* populate memsegs. each memseg is 1 page long */
99 for (cur_seg = 0; cur_seg < n_segs; cur_seg++) {
100 arr = &msl->memseg_arr;
102 ms = rte_fbarray_get(arr, cur_seg);
103 if (rte_eal_iova_mode() == RTE_IOVA_VA)
104 ms->iova = (uintptr_t)addr;
106 ms->iova = RTE_BAD_IOVA;
108 ms->hugepage_sz = page_sz;
112 rte_fbarray_set_used(arr, cur_seg);
114 addr = RTE_PTR_ADD(addr, page_sz);
119 /* map all hugepages and sort them */
120 for (i = 0; i < internal_config.num_hugepage_sizes; i ++){
121 struct hugepage_info *hpi;
122 rte_iova_t prev_end = 0;
123 int prev_ms_idx = -1;
124 uint64_t page_sz, mem_needed;
125 unsigned int n_pages, max_pages;
127 hpi = &internal_config.hugepage_info[i];
128 page_sz = hpi->hugepage_sz;
129 max_pages = hpi->num_pages[0];
130 mem_needed = RTE_ALIGN_CEIL(internal_config.memory - total_mem,
133 n_pages = RTE_MIN(mem_needed / page_sz, max_pages);
135 for (j = 0; j < n_pages; j++) {
136 struct rte_memseg_list *msl;
137 struct rte_fbarray *arr;
138 struct rte_memseg *seg;
142 size_t sysctl_size = sizeof(physaddr);
143 char physaddr_str[64];
146 /* first, check if this segment is IOVA-adjacent to
149 snprintf(physaddr_str, sizeof(physaddr_str),
150 "hw.contigmem.physaddr.%d", j);
151 error = sysctlbyname(physaddr_str, &physaddr,
152 &sysctl_size, NULL, 0);
154 RTE_LOG(ERR, EAL, "Failed to get physical addr for buffer %u "
155 "from %s\n", j, hpi->hugedir);
159 is_adjacent = prev_end != 0 && physaddr == prev_end;
160 prev_end = physaddr + hpi->hugepage_sz;
162 for (msl_idx = 0; msl_idx < RTE_MAX_MEMSEG_LISTS;
164 bool empty, need_hole;
165 msl = &mcfg->memsegs[msl_idx];
166 arr = &msl->memseg_arr;
168 if (msl->page_sz != page_sz)
171 empty = arr->count == 0;
173 /* we need a hole if this isn't an empty memseg
174 * list, and if previous segment was not
175 * adjacent to current one.
177 need_hole = !empty && !is_adjacent;
179 /* we need 1, plus hole if not adjacent */
180 ms_idx = rte_fbarray_find_next_n_free(arr,
181 0, 1 + (need_hole ? 1 : 0));
183 /* memseg list is full? */
187 if (need_hole && prev_ms_idx == ms_idx - 1)
189 prev_ms_idx = ms_idx;
193 if (msl_idx == RTE_MAX_MEMSEG_LISTS) {
194 RTE_LOG(ERR, EAL, "Could not find space for memseg. Please increase %s and/or %s in configuration.\n",
195 RTE_STR(CONFIG_RTE_MAX_MEMSEG_PER_TYPE),
196 RTE_STR(CONFIG_RTE_MAX_MEM_PER_TYPE));
199 arr = &msl->memseg_arr;
200 seg = rte_fbarray_get(arr, ms_idx);
202 addr = RTE_PTR_ADD(msl->base_va,
203 (size_t)msl->page_sz * ms_idx);
205 /* address is already mapped in memseg list, so using
206 * MAP_FIXED here is safe.
208 addr = mmap(addr, page_sz, PROT_READ|PROT_WRITE,
209 MAP_SHARED | MAP_FIXED,
210 hpi->lock_descriptor,
212 if (addr == MAP_FAILED) {
213 RTE_LOG(ERR, EAL, "Failed to mmap buffer %u from %s\n",
219 seg->iova = physaddr;
220 seg->hugepage_sz = page_sz;
222 seg->nchannel = mcfg->nchannel;
223 seg->nrank = mcfg->nrank;
226 rte_fbarray_set_used(arr, ms_idx);
228 RTE_LOG(INFO, EAL, "Mapped memory segment %u @ %p: physaddr:0x%"
230 seg_idx++, addr, physaddr, page_sz);
232 total_mem += seg->len;
234 if (total_mem >= internal_config.memory)
237 if (total_mem < internal_config.memory) {
238 RTE_LOG(ERR, EAL, "Couldn't reserve requested memory, "
239 "requested: %" PRIu64 "M "
240 "available: %" PRIu64 "M\n",
241 internal_config.memory >> 20, total_mem >> 20);
247 struct attach_walk_args {
252 attach_segment(const struct rte_memseg_list *msl, const struct rte_memseg *ms,
255 struct attach_walk_args *wa = arg;
261 addr = mmap(ms->addr, ms->len, PROT_READ | PROT_WRITE,
262 MAP_SHARED | MAP_FIXED, wa->fd_hugepage,
263 wa->seg_idx * EAL_PAGE_SIZE);
264 if (addr == MAP_FAILED || addr != ms->addr)
272 rte_eal_hugepage_attach(void)
274 const struct hugepage_info *hpi;
275 int fd_hugepage = -1;
278 hpi = &internal_config.hugepage_info[0];
280 for (i = 0; i < internal_config.num_hugepage_sizes; i++) {
281 const struct hugepage_info *cur_hpi = &hpi[i];
282 struct attach_walk_args wa;
284 memset(&wa, 0, sizeof(wa));
286 /* Obtain a file descriptor for contiguous memory */
287 fd_hugepage = open(cur_hpi->hugedir, O_RDWR);
288 if (fd_hugepage < 0) {
289 RTE_LOG(ERR, EAL, "Could not open %s\n",
293 wa.fd_hugepage = fd_hugepage;
296 /* Map the contiguous memory into each memory segment */
297 if (rte_memseg_walk(attach_segment, &wa) < 0) {
298 RTE_LOG(ERR, EAL, "Failed to mmap buffer %u from %s\n",
299 wa.seg_idx, cur_hpi->hugedir);
307 /* hugepage_info is no longer required */
311 if (fd_hugepage >= 0)
317 rte_eal_using_phys_addrs(void)
323 get_mem_amount(uint64_t page_sz, uint64_t max_mem)
325 uint64_t area_sz, max_pages;
327 /* limit to RTE_MAX_MEMSEG_PER_LIST pages or RTE_MAX_MEM_MB_PER_LIST */
328 max_pages = RTE_MAX_MEMSEG_PER_LIST;
329 max_mem = RTE_MIN((uint64_t)RTE_MAX_MEM_MB_PER_LIST << 20, max_mem);
331 area_sz = RTE_MIN(page_sz * max_pages, max_mem);
333 /* make sure the list isn't smaller than the page size */
334 area_sz = RTE_MAX(area_sz, page_sz);
336 return RTE_ALIGN(area_sz, page_sz);
339 #define MEMSEG_LIST_FMT "memseg-%" PRIu64 "k-%i-%i"
341 alloc_memseg_list(struct rte_memseg_list *msl, uint64_t page_sz,
342 int n_segs, int socket_id, int type_msl_idx)
344 char name[RTE_FBARRAY_NAME_LEN];
346 snprintf(name, sizeof(name), MEMSEG_LIST_FMT, page_sz >> 10, socket_id,
348 if (rte_fbarray_init(&msl->memseg_arr, name, n_segs,
349 sizeof(struct rte_memseg))) {
350 RTE_LOG(ERR, EAL, "Cannot allocate memseg list: %s\n",
351 rte_strerror(rte_errno));
355 msl->page_sz = page_sz;
356 msl->socket_id = socket_id;
359 RTE_LOG(DEBUG, EAL, "Memseg list allocated: 0x%zxkB at socket %i\n",
360 (size_t)page_sz >> 10, socket_id);
366 alloc_va_space(struct rte_memseg_list *msl)
373 #ifdef RTE_ARCH_PPC_64
374 flags |= MAP_HUGETLB;
377 page_sz = msl->page_sz;
378 mem_sz = page_sz * msl->memseg_arr.len;
380 addr = eal_get_virtual_area(msl->base_va, &mem_sz, page_sz, 0, flags);
382 if (rte_errno == EADDRNOTAVAIL)
383 RTE_LOG(ERR, EAL, "Could not mmap %llu bytes at [%p] - "
384 "please use '--" OPT_BASE_VIRTADDR "' option\n",
385 (unsigned long long)mem_sz, msl->base_va);
387 RTE_LOG(ERR, EAL, "Cannot reserve memory\n");
398 memseg_primary_init(void)
400 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
401 int hpi_idx, msl_idx = 0;
402 struct rte_memseg_list *msl;
403 uint64_t max_mem, total_mem;
405 /* no-huge does not need this at all */
406 if (internal_config.no_hugetlbfs)
409 /* FreeBSD has an issue where core dump will dump the entire memory
410 * contents, including anonymous zero-page memory. Therefore, while we
411 * will be limiting total amount of memory to RTE_MAX_MEM_MB, we will
412 * also be further limiting total memory amount to whatever memory is
413 * available to us through contigmem driver (plus spacing blocks).
415 * so, at each stage, we will be checking how much memory we are
416 * preallocating, and adjust all the values accordingly.
419 max_mem = (uint64_t)RTE_MAX_MEM_MB << 20;
422 /* create memseg lists */
423 for (hpi_idx = 0; hpi_idx < (int) internal_config.num_hugepage_sizes;
425 uint64_t max_type_mem, total_type_mem = 0;
427 int type_msl_idx, max_segs, avail_segs, total_segs = 0;
428 struct hugepage_info *hpi;
429 uint64_t hugepage_sz;
431 hpi = &internal_config.hugepage_info[hpi_idx];
432 hugepage_sz = hpi->hugepage_sz;
434 /* no NUMA support on FreeBSD */
436 /* check if we've already exceeded total memory amount */
437 if (total_mem >= max_mem)
440 /* first, calculate theoretical limits according to config */
441 max_type_mem = RTE_MIN(max_mem - total_mem,
442 (uint64_t)RTE_MAX_MEM_MB_PER_TYPE << 20);
443 max_segs = RTE_MAX_MEMSEG_PER_TYPE;
445 /* now, limit all of that to whatever will actually be
446 * available to us, because without dynamic allocation support,
447 * all of that extra memory will be sitting there being useless
448 * and slowing down core dumps in case of a crash.
450 * we need (N*2)-1 segments because we cannot guarantee that
451 * each segment will be IOVA-contiguous with the previous one,
452 * so we will allocate more and put spaces inbetween segments
453 * that are non-contiguous.
455 avail_segs = (hpi->num_pages[0] * 2) - 1;
456 avail_mem = avail_segs * hugepage_sz;
458 max_type_mem = RTE_MIN(avail_mem, max_type_mem);
459 max_segs = RTE_MIN(avail_segs, max_segs);
462 while (total_type_mem < max_type_mem &&
463 total_segs < max_segs) {
464 uint64_t cur_max_mem, cur_mem;
467 if (msl_idx >= RTE_MAX_MEMSEG_LISTS) {
469 "No more space in memseg lists, please increase %s\n",
470 RTE_STR(CONFIG_RTE_MAX_MEMSEG_LISTS));
474 msl = &mcfg->memsegs[msl_idx++];
476 cur_max_mem = max_type_mem - total_type_mem;
478 cur_mem = get_mem_amount(hugepage_sz,
480 n_segs = cur_mem / hugepage_sz;
482 if (alloc_memseg_list(msl, hugepage_sz, n_segs,
486 total_segs += msl->memseg_arr.len;
487 total_type_mem = total_segs * hugepage_sz;
490 if (alloc_va_space(msl)) {
491 RTE_LOG(ERR, EAL, "Cannot allocate VA space for memseg list\n");
495 total_mem += total_type_mem;
501 memseg_secondary_init(void)
503 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
505 struct rte_memseg_list *msl;
507 for (msl_idx = 0; msl_idx < RTE_MAX_MEMSEG_LISTS; msl_idx++) {
509 msl = &mcfg->memsegs[msl_idx];
511 /* skip empty memseg lists */
512 if (msl->memseg_arr.len == 0)
515 if (rte_fbarray_attach(&msl->memseg_arr)) {
516 RTE_LOG(ERR, EAL, "Cannot attach to primary process memseg lists\n");
520 /* preallocate VA space */
521 if (alloc_va_space(msl)) {
522 RTE_LOG(ERR, EAL, "Cannot preallocate VA space for hugepage memory\n");
531 rte_eal_memseg_init(void)
533 return rte_eal_process_type() == RTE_PROC_PRIMARY ?
534 memseg_primary_init() :
535 memseg_secondary_init();