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_eal_memconfig.h>
19 #include <rte_per_lcore.h>
20 #include <rte_errno.h>
21 #include <rte_string_fns.h>
22 #include <rte_common.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) {
75 RTE_LOG(ERR, EAL, "%s(): No more room in config\n", __func__);
80 if (strlen(name) > sizeof(mz->name) - 1) {
81 RTE_LOG(DEBUG, EAL, "%s(): memzone <%s>: name too long\n",
83 rte_errno = ENAMETOOLONG;
87 /* zone already exist */
88 if ((memzone_lookup_thread_unsafe(name)) != NULL) {
89 RTE_LOG(DEBUG, EAL, "%s(): memzone <%s> already exists\n",
95 /* if alignment is not a power of two */
96 if (align && !rte_is_power_of_2(align)) {
97 RTE_LOG(ERR, EAL, "%s(): Invalid alignment: %u\n", __func__,
103 /* alignment less than cache size is not allowed */
104 if (align < RTE_CACHE_LINE_SIZE)
105 align = RTE_CACHE_LINE_SIZE;
107 /* align length on cache boundary. Check for overflow before doing so */
108 if (len > SIZE_MAX - RTE_CACHE_LINE_MASK) {
109 rte_errno = EINVAL; /* requested size too big */
113 len = RTE_ALIGN_CEIL(len, RTE_CACHE_LINE_SIZE);
115 /* save minimal requested length */
116 requested_len = RTE_MAX((size_t)RTE_CACHE_LINE_SIZE, len);
118 /* check that boundary condition is valid */
119 if (bound != 0 && (requested_len > bound || !rte_is_power_of_2(bound))) {
124 if ((socket_id != SOCKET_ID_ANY) && socket_id < 0) {
129 /* only set socket to SOCKET_ID_ANY if we aren't allocating for an
132 if (!rte_eal_has_hugepages() && socket_id < RTE_MAX_NUMA_NODES)
133 socket_id = SOCKET_ID_ANY;
135 contig = (flags & RTE_MEMZONE_IOVA_CONTIG) != 0;
136 /* malloc only cares about size flags, remove contig flag from flags */
137 flags &= ~RTE_MEMZONE_IOVA_CONTIG;
139 if (len == 0 && bound == 0) {
140 /* no size constraints were placed, so use malloc elem len */
142 mz_addr = malloc_heap_alloc_biggest(NULL, socket_id, flags,
146 requested_len = bound;
147 /* allocate memory on heap */
148 mz_addr = malloc_heap_alloc(NULL, requested_len, socket_id,
149 flags, align, bound, contig);
151 if (mz_addr == NULL) {
156 struct malloc_elem *elem = malloc_elem_from_data(mz_addr);
158 /* fill the zone in config */
159 mz_idx = rte_fbarray_find_next_free(arr, 0);
164 rte_fbarray_set_used(arr, mz_idx);
165 mz = rte_fbarray_get(arr, mz_idx);
169 RTE_LOG(ERR, EAL, "%s(): Cannot find free memzone\n", __func__);
170 malloc_heap_free(elem);
175 strlcpy(mz->name, name, sizeof(mz->name));
176 mz->iova = rte_malloc_virt2iova(mz_addr);
178 mz->len = requested_len == 0 ?
179 elem->size - elem->pad - MALLOC_ELEM_OVERHEAD :
181 mz->hugepage_sz = elem->msl->page_sz;
182 mz->socket_id = elem->msl->socket_id;
188 static const struct rte_memzone *
189 rte_memzone_reserve_thread_safe(const char *name, size_t len, int socket_id,
190 unsigned int flags, unsigned int align, unsigned int bound)
192 struct rte_mem_config *mcfg;
193 const struct rte_memzone *mz = NULL;
195 /* get pointer to global configuration */
196 mcfg = rte_eal_get_configuration()->mem_config;
198 rte_rwlock_write_lock(&mcfg->mlock);
200 mz = memzone_reserve_aligned_thread_unsafe(
201 name, len, socket_id, flags, align, bound);
203 rte_rwlock_write_unlock(&mcfg->mlock);
209 * Return a pointer to a correctly filled memzone descriptor (with a
210 * specified alignment and boundary). If the allocation cannot be done,
213 const struct rte_memzone *
214 rte_memzone_reserve_bounded(const char *name, size_t len, int socket_id,
215 unsigned flags, unsigned align, unsigned bound)
217 return rte_memzone_reserve_thread_safe(name, len, socket_id, flags,
222 * Return a pointer to a correctly filled memzone descriptor (with a
223 * specified alignment). If the allocation cannot be done, return NULL.
225 const struct rte_memzone *
226 rte_memzone_reserve_aligned(const char *name, size_t len, int socket_id,
227 unsigned flags, unsigned align)
229 return rte_memzone_reserve_thread_safe(name, len, socket_id, flags,
234 * Return a pointer to a correctly filled memzone descriptor. If the
235 * allocation cannot be done, return NULL.
237 const struct rte_memzone *
238 rte_memzone_reserve(const char *name, size_t len, int socket_id,
241 return rte_memzone_reserve_thread_safe(name, len, socket_id,
242 flags, RTE_CACHE_LINE_SIZE, 0);
246 rte_memzone_free(const struct rte_memzone *mz)
248 struct rte_mem_config *mcfg;
249 struct rte_fbarray *arr;
250 struct rte_memzone *found_mz;
258 mcfg = rte_eal_get_configuration()->mem_config;
259 arr = &mcfg->memzones;
261 rte_rwlock_write_lock(&mcfg->mlock);
263 idx = rte_fbarray_find_idx(arr, mz);
264 found_mz = rte_fbarray_get(arr, idx);
266 if (found_mz == NULL) {
268 } else if (found_mz->addr == NULL) {
269 RTE_LOG(ERR, EAL, "Memzone is not allocated\n");
272 addr = found_mz->addr;
273 memset(found_mz, 0, sizeof(*found_mz));
274 rte_fbarray_set_free(arr, idx);
277 rte_rwlock_write_unlock(&mcfg->mlock);
286 * Lookup for the memzone identified by the given name
288 const struct rte_memzone *
289 rte_memzone_lookup(const char *name)
291 struct rte_mem_config *mcfg;
292 const struct rte_memzone *memzone = NULL;
294 mcfg = rte_eal_get_configuration()->mem_config;
296 rte_rwlock_read_lock(&mcfg->mlock);
298 memzone = memzone_lookup_thread_unsafe(name);
300 rte_rwlock_read_unlock(&mcfg->mlock);
306 dump_memzone(const struct rte_memzone *mz, void *arg)
308 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
309 struct rte_memseg_list *msl = NULL;
310 void *cur_addr, *mz_end;
311 struct rte_memseg *ms;
316 mz_idx = rte_fbarray_find_idx(&mcfg->memzones, mz);
318 fprintf(f, "Zone %u: name:<%s>, len:0x%zx, virt:%p, "
319 "socket_id:%"PRId32", flags:%"PRIx32"\n",
327 /* go through each page occupied by this memzone */
328 msl = rte_mem_virt2memseg_list(mz->addr);
330 RTE_LOG(DEBUG, EAL, "Skipping bad memzone\n");
333 page_sz = (size_t)mz->hugepage_sz;
334 cur_addr = RTE_PTR_ALIGN_FLOOR(mz->addr, page_sz);
335 mz_end = RTE_PTR_ADD(cur_addr, mz->len);
337 fprintf(f, "physical segments used:\n");
338 ms_idx = RTE_PTR_DIFF(mz->addr, msl->base_va) / page_sz;
339 ms = rte_fbarray_get(&msl->memseg_arr, ms_idx);
342 fprintf(f, " addr: %p iova: 0x%" PRIx64 " "
345 cur_addr, ms->iova, ms->len, page_sz);
347 /* advance VA to next page */
348 cur_addr = RTE_PTR_ADD(cur_addr, page_sz);
350 /* memzones occupy contiguous segments */
352 } while (cur_addr < mz_end);
355 /* Dump all reserved memory zones on console */
357 rte_memzone_dump(FILE *f)
359 rte_memzone_walk(dump_memzone, f);
363 * Init the memzone subsystem
366 rte_eal_memzone_init(void)
368 struct rte_mem_config *mcfg;
371 /* get pointer to global configuration */
372 mcfg = rte_eal_get_configuration()->mem_config;
374 rte_rwlock_write_lock(&mcfg->mlock);
376 if (rte_eal_process_type() == RTE_PROC_PRIMARY &&
377 rte_fbarray_init(&mcfg->memzones, "memzone",
378 RTE_MAX_MEMZONE, sizeof(struct rte_memzone))) {
379 RTE_LOG(ERR, EAL, "Cannot allocate memzone list\n");
381 } else if (rte_eal_process_type() == RTE_PROC_SECONDARY &&
382 rte_fbarray_attach(&mcfg->memzones)) {
383 RTE_LOG(ERR, EAL, "Cannot attach to memzone list\n");
387 rte_rwlock_write_unlock(&mcfg->mlock);
392 /* Walk all reserved memory zones */
393 void rte_memzone_walk(void (*func)(const struct rte_memzone *, void *),
396 struct rte_mem_config *mcfg;
397 struct rte_fbarray *arr;
400 mcfg = rte_eal_get_configuration()->mem_config;
401 arr = &mcfg->memzones;
403 rte_rwlock_read_lock(&mcfg->mlock);
404 i = rte_fbarray_find_next_used(arr, 0);
406 struct rte_memzone *mz = rte_fbarray_get(arr, i);
408 i = rte_fbarray_find_next_used(arr, i + 1);
410 rte_rwlock_read_unlock(&mcfg->mlock);