1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
12 #include <sys/queue.h>
15 #include <rte_memory.h>
16 #include <rte_memzone.h>
18 #include <rte_per_lcore.h>
19 #include <rte_errno.h>
20 #include <rte_string_fns.h>
21 #include <rte_common.h>
22 #include <rte_eal_trace.h>
24 #include "malloc_heap.h"
25 #include "malloc_elem.h"
26 #include "eal_private.h"
27 #include "eal_memcfg.h"
29 static inline const struct rte_memzone *
30 memzone_lookup_thread_unsafe(const char *name)
32 struct rte_mem_config *mcfg;
33 struct rte_fbarray *arr;
34 const struct rte_memzone *mz;
37 /* get pointer to global configuration */
38 mcfg = rte_eal_get_configuration()->mem_config;
39 arr = &mcfg->memzones;
42 * the algorithm is not optimal (linear), but there are few
43 * zones and this function should be called at init only
45 i = rte_fbarray_find_next_used(arr, 0);
47 mz = rte_fbarray_get(arr, i);
48 if (mz->addr != NULL &&
49 !strncmp(name, mz->name, RTE_MEMZONE_NAMESIZE))
51 i = rte_fbarray_find_next_used(arr, i + 1);
56 static const struct rte_memzone *
57 memzone_reserve_aligned_thread_unsafe(const char *name, size_t len,
58 int socket_id, unsigned int flags, unsigned int align,
61 struct rte_memzone *mz;
62 struct rte_mem_config *mcfg;
63 struct rte_fbarray *arr;
69 /* get pointer to global configuration */
70 mcfg = rte_eal_get_configuration()->mem_config;
71 arr = &mcfg->memzones;
73 /* no more room in config */
74 if (arr->count >= arr->len) {
76 "%s(): Number of requested memzone segments exceeds RTE_MAX_MEMZONE\n",
82 if (strlen(name) > sizeof(mz->name) - 1) {
83 RTE_LOG(DEBUG, EAL, "%s(): memzone <%s>: name too long\n",
85 rte_errno = ENAMETOOLONG;
89 /* zone already exist */
90 if ((memzone_lookup_thread_unsafe(name)) != NULL) {
91 RTE_LOG(DEBUG, EAL, "%s(): memzone <%s> already exists\n",
97 /* if alignment is not a power of two */
98 if (align && !rte_is_power_of_2(align)) {
99 RTE_LOG(ERR, EAL, "%s(): Invalid alignment: %u\n", __func__,
105 /* alignment less than cache size is not allowed */
106 if (align < RTE_CACHE_LINE_SIZE)
107 align = RTE_CACHE_LINE_SIZE;
109 /* align length on cache boundary. Check for overflow before doing so */
110 if (len > SIZE_MAX - RTE_CACHE_LINE_MASK) {
111 rte_errno = EINVAL; /* requested size too big */
115 len = RTE_ALIGN_CEIL(len, RTE_CACHE_LINE_SIZE);
117 /* save minimal requested length */
118 requested_len = RTE_MAX((size_t)RTE_CACHE_LINE_SIZE, len);
120 /* check that boundary condition is valid */
121 if (bound != 0 && (requested_len > bound || !rte_is_power_of_2(bound))) {
126 if ((socket_id != SOCKET_ID_ANY) && socket_id < 0) {
131 /* only set socket to SOCKET_ID_ANY if we aren't allocating for an
134 if (!rte_eal_has_hugepages() && socket_id < RTE_MAX_NUMA_NODES)
135 socket_id = SOCKET_ID_ANY;
137 contig = (flags & RTE_MEMZONE_IOVA_CONTIG) != 0;
138 /* malloc only cares about size flags, remove contig flag from flags */
139 flags &= ~RTE_MEMZONE_IOVA_CONTIG;
141 if (len == 0 && bound == 0) {
142 /* no size constraints were placed, so use malloc elem len */
144 mz_addr = malloc_heap_alloc_biggest(NULL, socket_id, flags,
148 requested_len = bound;
149 /* allocate memory on heap */
150 mz_addr = malloc_heap_alloc(NULL, requested_len, socket_id,
151 flags, align, bound, contig);
153 if (mz_addr == NULL) {
158 struct malloc_elem *elem = malloc_elem_from_data(mz_addr);
160 /* fill the zone in config */
161 mz_idx = rte_fbarray_find_next_free(arr, 0);
166 rte_fbarray_set_used(arr, mz_idx);
167 mz = rte_fbarray_get(arr, mz_idx);
171 RTE_LOG(ERR, EAL, "%s(): Cannot find free memzone\n", __func__);
172 malloc_heap_free(elem);
177 strlcpy(mz->name, name, sizeof(mz->name));
178 mz->iova = rte_malloc_virt2iova(mz_addr);
180 mz->len = requested_len == 0 ?
181 elem->size - elem->pad - MALLOC_ELEM_OVERHEAD :
183 mz->hugepage_sz = elem->msl->page_sz;
184 mz->socket_id = elem->msl->socket_id;
190 static const struct rte_memzone *
191 rte_memzone_reserve_thread_safe(const char *name, size_t len, int socket_id,
192 unsigned int flags, unsigned int align, unsigned int bound)
194 struct rte_mem_config *mcfg;
195 const struct rte_memzone *mz = NULL;
197 /* get pointer to global configuration */
198 mcfg = rte_eal_get_configuration()->mem_config;
200 rte_rwlock_write_lock(&mcfg->mlock);
202 mz = memzone_reserve_aligned_thread_unsafe(
203 name, len, socket_id, flags, align, bound);
205 rte_eal_trace_memzone_reserve(name, len, socket_id, flags, align,
208 rte_rwlock_write_unlock(&mcfg->mlock);
214 * Return a pointer to a correctly filled memzone descriptor (with a
215 * specified alignment and boundary). If the allocation cannot be done,
218 const struct rte_memzone *
219 rte_memzone_reserve_bounded(const char *name, size_t len, int socket_id,
220 unsigned flags, unsigned align, unsigned bound)
222 return rte_memzone_reserve_thread_safe(name, len, socket_id, flags,
227 * Return a pointer to a correctly filled memzone descriptor (with a
228 * specified alignment). If the allocation cannot be done, return NULL.
230 const struct rte_memzone *
231 rte_memzone_reserve_aligned(const char *name, size_t len, int socket_id,
232 unsigned flags, unsigned align)
234 return rte_memzone_reserve_thread_safe(name, len, socket_id, flags,
239 * Return a pointer to a correctly filled memzone descriptor. If the
240 * allocation cannot be done, return NULL.
242 const struct rte_memzone *
243 rte_memzone_reserve(const char *name, size_t len, int socket_id,
246 return rte_memzone_reserve_thread_safe(name, len, socket_id,
247 flags, RTE_CACHE_LINE_SIZE, 0);
251 rte_memzone_free(const struct rte_memzone *mz)
253 char name[RTE_MEMZONE_NAMESIZE];
254 struct rte_mem_config *mcfg;
255 struct rte_fbarray *arr;
256 struct rte_memzone *found_mz;
264 rte_strlcpy(name, mz->name, RTE_MEMZONE_NAMESIZE);
265 mcfg = rte_eal_get_configuration()->mem_config;
266 arr = &mcfg->memzones;
268 rte_rwlock_write_lock(&mcfg->mlock);
270 idx = rte_fbarray_find_idx(arr, mz);
271 found_mz = rte_fbarray_get(arr, idx);
273 if (found_mz == NULL) {
275 } else if (found_mz->addr == NULL) {
276 RTE_LOG(ERR, EAL, "Memzone is not allocated\n");
279 addr = found_mz->addr;
280 memset(found_mz, 0, sizeof(*found_mz));
281 rte_fbarray_set_free(arr, idx);
284 rte_rwlock_write_unlock(&mcfg->mlock);
289 rte_eal_trace_memzone_free(name, addr, ret);
294 * Lookup for the memzone identified by the given name
296 const struct rte_memzone *
297 rte_memzone_lookup(const char *name)
299 struct rte_mem_config *mcfg;
300 const struct rte_memzone *memzone = NULL;
302 mcfg = rte_eal_get_configuration()->mem_config;
304 rte_rwlock_read_lock(&mcfg->mlock);
306 memzone = memzone_lookup_thread_unsafe(name);
308 rte_rwlock_read_unlock(&mcfg->mlock);
310 rte_eal_trace_memzone_lookup(name, memzone);
315 dump_memzone(const struct rte_memzone *mz, void *arg)
317 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
318 struct rte_memseg_list *msl = NULL;
319 void *cur_addr, *mz_end;
320 struct rte_memseg *ms;
325 mz_idx = rte_fbarray_find_idx(&mcfg->memzones, mz);
327 fprintf(f, "Zone %u: name:<%s>, len:0x%zx, virt:%p, "
328 "socket_id:%"PRId32", flags:%"PRIx32"\n",
336 /* go through each page occupied by this memzone */
337 msl = rte_mem_virt2memseg_list(mz->addr);
339 RTE_LOG(DEBUG, EAL, "Skipping bad memzone\n");
342 page_sz = (size_t)mz->hugepage_sz;
343 cur_addr = RTE_PTR_ALIGN_FLOOR(mz->addr, page_sz);
344 mz_end = RTE_PTR_ADD(cur_addr, mz->len);
346 fprintf(f, "physical segments used:\n");
347 ms_idx = RTE_PTR_DIFF(mz->addr, msl->base_va) / page_sz;
348 ms = rte_fbarray_get(&msl->memseg_arr, ms_idx);
351 fprintf(f, " addr: %p iova: 0x%" PRIx64 " "
354 cur_addr, ms->iova, ms->len, page_sz);
356 /* advance VA to next page */
357 cur_addr = RTE_PTR_ADD(cur_addr, page_sz);
359 /* memzones occupy contiguous segments */
361 } while (cur_addr < mz_end);
364 /* Dump all reserved memory zones on console */
366 rte_memzone_dump(FILE *f)
368 rte_memzone_walk(dump_memzone, f);
372 * Init the memzone subsystem
375 rte_eal_memzone_init(void)
377 struct rte_mem_config *mcfg;
380 /* get pointer to global configuration */
381 mcfg = rte_eal_get_configuration()->mem_config;
383 rte_rwlock_write_lock(&mcfg->mlock);
385 if (rte_eal_process_type() == RTE_PROC_PRIMARY &&
386 rte_fbarray_init(&mcfg->memzones, "memzone",
387 RTE_MAX_MEMZONE, sizeof(struct rte_memzone))) {
388 RTE_LOG(ERR, EAL, "Cannot allocate memzone list\n");
390 } else if (rte_eal_process_type() == RTE_PROC_SECONDARY &&
391 rte_fbarray_attach(&mcfg->memzones)) {
392 RTE_LOG(ERR, EAL, "Cannot attach to memzone list\n");
396 rte_rwlock_write_unlock(&mcfg->mlock);
401 /* Walk all reserved memory zones */
402 void rte_memzone_walk(void (*func)(const struct rte_memzone *, void *),
405 struct rte_mem_config *mcfg;
406 struct rte_fbarray *arr;
409 mcfg = rte_eal_get_configuration()->mem_config;
410 arr = &mcfg->memzones;
412 rte_rwlock_read_lock(&mcfg->mlock);
413 i = rte_fbarray_find_next_used(arr, 0);
415 struct rte_memzone *mz = rte_fbarray_get(arr, i);
417 i = rte_fbarray_find_next_used(arr, i + 1);
419 rte_rwlock_read_unlock(&mcfg->mlock);