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;
60 struct internal_config *internal_conf =
61 eal_get_internal_configuration();
63 /* get pointer to global configuration */
64 mcfg = rte_eal_get_configuration()->mem_config;
66 /* for debug purposes, hugetlbfs can be disabled */
67 if (internal_conf->no_hugetlbfs) {
68 struct rte_memseg_list *msl;
69 uint64_t mem_sz, page_sz;
72 /* create a memseg list */
73 msl = &mcfg->memsegs[0];
75 mem_sz = internal_conf->memory;
76 page_sz = RTE_PGSIZE_4K;
77 n_segs = mem_sz / page_sz;
79 if (eal_memseg_list_init_named(
80 msl, "nohugemem", page_sz, n_segs, 0, true)) {
84 addr = mmap(NULL, mem_sz, PROT_READ | PROT_WRITE,
85 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
86 if (addr == MAP_FAILED) {
87 RTE_LOG(ERR, EAL, "%s: mmap() failed: %s\n", __func__,
95 eal_memseg_list_populate(msl, addr, n_segs);
100 /* map all hugepages and sort them */
101 for (i = 0; i < internal_conf->num_hugepage_sizes; i++) {
102 struct hugepage_info *hpi;
103 rte_iova_t prev_end = 0;
104 int prev_ms_idx = -1;
105 uint64_t page_sz, mem_needed;
106 unsigned int n_pages, max_pages;
108 hpi = &internal_conf->hugepage_info[i];
109 page_sz = hpi->hugepage_sz;
110 max_pages = hpi->num_pages[0];
111 mem_needed = RTE_ALIGN_CEIL(internal_conf->memory - total_mem,
114 n_pages = RTE_MIN(mem_needed / page_sz, max_pages);
116 for (j = 0; j < n_pages; j++) {
117 struct rte_memseg_list *msl;
118 struct rte_fbarray *arr;
119 struct rte_memseg *seg;
123 size_t sysctl_size = sizeof(physaddr);
124 char physaddr_str[64];
127 /* first, check if this segment is IOVA-adjacent to
130 snprintf(physaddr_str, sizeof(physaddr_str),
131 "hw.contigmem.physaddr.%d", j);
132 error = sysctlbyname(physaddr_str, &physaddr,
133 &sysctl_size, NULL, 0);
135 RTE_LOG(ERR, EAL, "Failed to get physical addr for buffer %u "
136 "from %s\n", j, hpi->hugedir);
140 is_adjacent = prev_end != 0 && physaddr == prev_end;
141 prev_end = physaddr + hpi->hugepage_sz;
143 for (msl_idx = 0; msl_idx < RTE_MAX_MEMSEG_LISTS;
145 bool empty, need_hole;
146 msl = &mcfg->memsegs[msl_idx];
147 arr = &msl->memseg_arr;
149 if (msl->page_sz != page_sz)
152 empty = arr->count == 0;
154 /* we need a hole if this isn't an empty memseg
155 * list, and if previous segment was not
156 * adjacent to current one.
158 need_hole = !empty && !is_adjacent;
160 /* we need 1, plus hole if not adjacent */
161 ms_idx = rte_fbarray_find_next_n_free(arr,
162 0, 1 + (need_hole ? 1 : 0));
164 /* memseg list is full? */
168 if (need_hole && prev_ms_idx == ms_idx - 1)
170 prev_ms_idx = ms_idx;
174 if (msl_idx == RTE_MAX_MEMSEG_LISTS) {
175 RTE_LOG(ERR, EAL, "Could not find space for memseg. Please increase %s and/or %s in configuration.\n",
176 RTE_STR(RTE_MAX_MEMSEG_PER_TYPE),
177 RTE_STR(RTE_MAX_MEM_MB_PER_TYPE));
180 arr = &msl->memseg_arr;
181 seg = rte_fbarray_get(arr, ms_idx);
183 addr = RTE_PTR_ADD(msl->base_va,
184 (size_t)msl->page_sz * ms_idx);
186 /* address is already mapped in memseg list, so using
187 * MAP_FIXED here is safe.
189 addr = mmap(addr, page_sz, PROT_READ|PROT_WRITE,
190 MAP_SHARED | MAP_FIXED,
191 hpi->lock_descriptor,
193 if (addr == MAP_FAILED) {
194 RTE_LOG(ERR, EAL, "Failed to mmap buffer %u from %s\n",
200 seg->iova = physaddr;
201 seg->hugepage_sz = page_sz;
203 seg->nchannel = mcfg->nchannel;
204 seg->nrank = mcfg->nrank;
207 rte_fbarray_set_used(arr, ms_idx);
209 RTE_LOG(INFO, EAL, "Mapped memory segment %u @ %p: physaddr:0x%"
211 seg_idx++, addr, physaddr, page_sz);
213 total_mem += seg->len;
215 if (total_mem >= internal_conf->memory)
218 if (total_mem < internal_conf->memory) {
219 RTE_LOG(ERR, EAL, "Couldn't reserve requested memory, "
220 "requested: %" PRIu64 "M "
221 "available: %" PRIu64 "M\n",
222 internal_conf->memory >> 20, total_mem >> 20);
228 struct attach_walk_args {
233 attach_segment(const struct rte_memseg_list *msl, const struct rte_memseg *ms,
236 struct attach_walk_args *wa = arg;
242 addr = mmap(ms->addr, ms->len, PROT_READ | PROT_WRITE,
243 MAP_SHARED | MAP_FIXED, wa->fd_hugepage,
244 wa->seg_idx * EAL_PAGE_SIZE);
245 if (addr == MAP_FAILED || addr != ms->addr)
253 rte_eal_hugepage_attach(void)
255 struct hugepage_info *hpi;
256 int fd_hugepage = -1;
258 struct internal_config *internal_conf =
259 eal_get_internal_configuration();
261 hpi = &internal_conf->hugepage_info[0];
263 for (i = 0; i < internal_conf->num_hugepage_sizes; i++) {
264 const struct hugepage_info *cur_hpi = &hpi[i];
265 struct attach_walk_args wa;
267 memset(&wa, 0, sizeof(wa));
269 /* Obtain a file descriptor for contiguous memory */
270 fd_hugepage = open(cur_hpi->hugedir, O_RDWR);
271 if (fd_hugepage < 0) {
272 RTE_LOG(ERR, EAL, "Could not open %s\n",
276 wa.fd_hugepage = fd_hugepage;
279 /* Map the contiguous memory into each memory segment */
280 if (rte_memseg_walk(attach_segment, &wa) < 0) {
281 RTE_LOG(ERR, EAL, "Failed to mmap buffer %u from %s\n",
282 wa.seg_idx, cur_hpi->hugedir);
290 /* hugepage_info is no longer required */
294 if (fd_hugepage >= 0)
300 rte_eal_using_phys_addrs(void)
306 get_mem_amount(uint64_t page_sz, uint64_t max_mem)
308 uint64_t area_sz, max_pages;
310 /* limit to RTE_MAX_MEMSEG_PER_LIST pages or RTE_MAX_MEM_MB_PER_LIST */
311 max_pages = RTE_MAX_MEMSEG_PER_LIST;
312 max_mem = RTE_MIN((uint64_t)RTE_MAX_MEM_MB_PER_LIST << 20, max_mem);
314 area_sz = RTE_MIN(page_sz * max_pages, max_mem);
316 /* make sure the list isn't smaller than the page size */
317 area_sz = RTE_MAX(area_sz, page_sz);
319 return RTE_ALIGN(area_sz, page_sz);
323 memseg_list_alloc(struct rte_memseg_list *msl)
327 #ifdef RTE_ARCH_PPC_64
328 flags |= EAL_RESERVE_HUGEPAGES;
330 return eal_memseg_list_alloc(msl, flags);
334 memseg_primary_init(void)
336 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
337 int hpi_idx, msl_idx = 0;
338 struct rte_memseg_list *msl;
339 uint64_t max_mem, total_mem;
340 struct internal_config *internal_conf =
341 eal_get_internal_configuration();
343 /* no-huge does not need this at all */
344 if (internal_conf->no_hugetlbfs)
347 /* FreeBSD has an issue where core dump will dump the entire memory
348 * contents, including anonymous zero-page memory. Therefore, while we
349 * will be limiting total amount of memory to RTE_MAX_MEM_MB, we will
350 * also be further limiting total memory amount to whatever memory is
351 * available to us through contigmem driver (plus spacing blocks).
353 * so, at each stage, we will be checking how much memory we are
354 * preallocating, and adjust all the values accordingly.
357 max_mem = (uint64_t)RTE_MAX_MEM_MB << 20;
360 /* create memseg lists */
361 for (hpi_idx = 0; hpi_idx < (int) internal_conf->num_hugepage_sizes;
363 uint64_t max_type_mem, total_type_mem = 0;
365 int type_msl_idx, max_segs, avail_segs, total_segs = 0;
366 struct hugepage_info *hpi;
367 uint64_t hugepage_sz;
369 hpi = &internal_conf->hugepage_info[hpi_idx];
370 hugepage_sz = hpi->hugepage_sz;
372 /* no NUMA support on FreeBSD */
374 /* check if we've already exceeded total memory amount */
375 if (total_mem >= max_mem)
378 /* first, calculate theoretical limits according to config */
379 max_type_mem = RTE_MIN(max_mem - total_mem,
380 (uint64_t)RTE_MAX_MEM_MB_PER_TYPE << 20);
381 max_segs = RTE_MAX_MEMSEG_PER_TYPE;
383 /* now, limit all of that to whatever will actually be
384 * available to us, because without dynamic allocation support,
385 * all of that extra memory will be sitting there being useless
386 * and slowing down core dumps in case of a crash.
388 * we need (N*2)-1 segments because we cannot guarantee that
389 * each segment will be IOVA-contiguous with the previous one,
390 * so we will allocate more and put spaces between segments
391 * that are non-contiguous.
393 avail_segs = (hpi->num_pages[0] * 2) - 1;
394 avail_mem = avail_segs * hugepage_sz;
396 max_type_mem = RTE_MIN(avail_mem, max_type_mem);
397 max_segs = RTE_MIN(avail_segs, max_segs);
400 while (total_type_mem < max_type_mem &&
401 total_segs < max_segs) {
402 uint64_t cur_max_mem, cur_mem;
405 if (msl_idx >= RTE_MAX_MEMSEG_LISTS) {
407 "No more space in memseg lists, please increase %s\n",
408 RTE_STR(RTE_MAX_MEMSEG_LISTS));
412 msl = &mcfg->memsegs[msl_idx++];
414 cur_max_mem = max_type_mem - total_type_mem;
416 cur_mem = get_mem_amount(hugepage_sz,
418 n_segs = cur_mem / hugepage_sz;
420 if (eal_memseg_list_init(msl, hugepage_sz, n_segs,
421 0, type_msl_idx, false))
424 total_segs += msl->memseg_arr.len;
425 total_type_mem = total_segs * hugepage_sz;
428 if (memseg_list_alloc(msl)) {
429 RTE_LOG(ERR, EAL, "Cannot allocate VA space for memseg list\n");
433 total_mem += total_type_mem;
439 memseg_secondary_init(void)
441 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
443 struct rte_memseg_list *msl;
445 for (msl_idx = 0; msl_idx < RTE_MAX_MEMSEG_LISTS; msl_idx++) {
447 msl = &mcfg->memsegs[msl_idx];
449 /* skip empty memseg lists */
450 if (msl->memseg_arr.len == 0)
453 if (rte_fbarray_attach(&msl->memseg_arr)) {
454 RTE_LOG(ERR, EAL, "Cannot attach to primary process memseg lists\n");
458 /* preallocate VA space */
459 if (memseg_list_alloc(msl)) {
460 RTE_LOG(ERR, EAL, "Cannot preallocate VA space for hugepage memory\n");
469 rte_eal_memseg_init(void)
471 return rte_eal_process_type() == RTE_PROC_PRIMARY ?
472 memseg_primary_init() :
473 memseg_secondary_init();