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"
28 static inline const struct rte_memzone *
29 memzone_lookup_thread_unsafe(const char *name)
31 struct rte_mem_config *mcfg;
32 struct rte_fbarray *arr;
33 const struct rte_memzone *mz;
36 /* get pointer to global configuration */
37 mcfg = rte_eal_get_configuration()->mem_config;
38 arr = &mcfg->memzones;
41 * the algorithm is not optimal (linear), but there are few
42 * zones and this function should be called at init only
44 i = rte_fbarray_find_next_used(arr, 0);
46 mz = rte_fbarray_get(arr, i);
47 if (mz->addr != NULL &&
48 !strncmp(name, mz->name, RTE_MEMZONE_NAMESIZE))
50 i = rte_fbarray_find_next_used(arr, i + 1);
56 /* This function will return the greatest free block if a heap has been
57 * specified. If no heap has been specified, it will return the heap and
58 * length of the greatest free block available in all heaps */
60 find_heap_max_free_elem(int *s, unsigned align)
62 struct rte_mem_config *mcfg;
63 struct rte_malloc_socket_stats stats;
67 /* get pointer to global configuration */
68 mcfg = rte_eal_get_configuration()->mem_config;
70 for (i = 0; i < RTE_MAX_NUMA_NODES; i++) {
71 if ((socket != SOCKET_ID_ANY) && (socket != i))
74 malloc_heap_get_stats(&mcfg->malloc_heaps[i], &stats);
75 if (stats.greatest_free_size > len) {
76 len = stats.greatest_free_size;
81 if (len < MALLOC_ELEM_OVERHEAD + align)
84 return len - MALLOC_ELEM_OVERHEAD - align;
87 static const struct rte_memzone *
88 memzone_reserve_aligned_thread_unsafe(const char *name, size_t len,
89 int socket_id, unsigned int flags, unsigned int align,
92 struct rte_memzone *mz;
93 struct rte_mem_config *mcfg;
94 struct rte_fbarray *arr;
96 int socket, i, mz_idx;
99 /* get pointer to global configuration */
100 mcfg = rte_eal_get_configuration()->mem_config;
101 arr = &mcfg->memzones;
103 /* no more room in config */
104 if (arr->count >= arr->len) {
105 RTE_LOG(ERR, EAL, "%s(): No more room in config\n", __func__);
110 if (strlen(name) > sizeof(mz->name) - 1) {
111 RTE_LOG(DEBUG, EAL, "%s(): memzone <%s>: name too long\n",
113 rte_errno = ENAMETOOLONG;
117 /* zone already exist */
118 if ((memzone_lookup_thread_unsafe(name)) != NULL) {
119 RTE_LOG(DEBUG, EAL, "%s(): memzone <%s> already exists\n",
125 /* if alignment is not a power of two */
126 if (align && !rte_is_power_of_2(align)) {
127 RTE_LOG(ERR, EAL, "%s(): Invalid alignment: %u\n", __func__,
133 /* alignment less than cache size is not allowed */
134 if (align < RTE_CACHE_LINE_SIZE)
135 align = RTE_CACHE_LINE_SIZE;
137 /* align length on cache boundary. Check for overflow before doing so */
138 if (len > SIZE_MAX - RTE_CACHE_LINE_MASK) {
139 rte_errno = EINVAL; /* requested size too big */
143 len += RTE_CACHE_LINE_MASK;
144 len &= ~((size_t) RTE_CACHE_LINE_MASK);
146 /* save minimal requested length */
147 requested_len = RTE_MAX((size_t)RTE_CACHE_LINE_SIZE, len);
149 /* check that boundary condition is valid */
150 if (bound != 0 && (requested_len > bound || !rte_is_power_of_2(bound))) {
155 if ((socket_id != SOCKET_ID_ANY) &&
156 (socket_id >= RTE_MAX_NUMA_NODES || socket_id < 0)) {
161 if (!rte_eal_has_hugepages())
162 socket_id = SOCKET_ID_ANY;
164 contig = (flags & RTE_MEMZONE_IOVA_CONTIG) != 0;
165 /* malloc only cares about size flags, remove contig flag from flags */
166 flags &= ~RTE_MEMZONE_IOVA_CONTIG;
169 /* len == 0 is only allowed for non-contiguous zones */
171 RTE_LOG(DEBUG, EAL, "Reserving zero-length contiguous memzones is not supported\n");
176 requested_len = bound;
178 requested_len = find_heap_max_free_elem(&socket_id, align);
179 if (requested_len == 0) {
186 if (socket_id == SOCKET_ID_ANY)
187 socket = malloc_get_numa_socket();
191 /* allocate memory on heap */
192 void *mz_addr = malloc_heap_alloc(&mcfg->malloc_heaps[socket], NULL,
193 requested_len, flags, align, bound, contig);
195 if ((mz_addr == NULL) && (socket_id == SOCKET_ID_ANY)) {
196 /* try other heaps */
197 for (i = 0; i < RTE_MAX_NUMA_NODES; i++) {
201 mz_addr = malloc_heap_alloc(&mcfg->malloc_heaps[i],
202 NULL, requested_len, flags, align,
209 if (mz_addr == NULL) {
214 struct malloc_elem *elem = malloc_elem_from_data(mz_addr);
216 /* fill the zone in config */
217 mz_idx = rte_fbarray_find_next_free(arr, 0);
222 rte_fbarray_set_used(arr, mz_idx);
223 mz = rte_fbarray_get(arr, mz_idx);
227 RTE_LOG(ERR, EAL, "%s(): Cannot find free memzone\n", __func__);
228 malloc_elem_free(elem);
233 snprintf(mz->name, sizeof(mz->name), "%s", name);
234 mz->iova = rte_malloc_virt2iova(mz_addr);
236 mz->len = (requested_len == 0 ? elem->size : requested_len);
237 mz->hugepage_sz = elem->msl->page_sz;
238 mz->socket_id = elem->msl->socket_id;
244 static const struct rte_memzone *
245 rte_memzone_reserve_thread_safe(const char *name, size_t len, int socket_id,
246 unsigned int flags, unsigned int align, unsigned int bound)
248 struct rte_mem_config *mcfg;
249 const struct rte_memzone *mz = NULL;
251 /* get pointer to global configuration */
252 mcfg = rte_eal_get_configuration()->mem_config;
254 rte_rwlock_write_lock(&mcfg->mlock);
256 mz = memzone_reserve_aligned_thread_unsafe(
257 name, len, socket_id, flags, align, bound);
259 rte_rwlock_write_unlock(&mcfg->mlock);
265 * Return a pointer to a correctly filled memzone descriptor (with a
266 * specified alignment and boundary). If the allocation cannot be done,
269 const struct rte_memzone *
270 rte_memzone_reserve_bounded(const char *name, size_t len, int socket_id,
271 unsigned flags, unsigned align, unsigned bound)
273 return rte_memzone_reserve_thread_safe(name, len, socket_id, flags,
278 * Return a pointer to a correctly filled memzone descriptor (with a
279 * specified alignment). If the allocation cannot be done, return NULL.
281 const struct rte_memzone *
282 rte_memzone_reserve_aligned(const char *name, size_t len, int socket_id,
283 unsigned flags, unsigned align)
285 return rte_memzone_reserve_thread_safe(name, len, socket_id, flags,
290 * Return a pointer to a correctly filled memzone descriptor. If the
291 * allocation cannot be done, return NULL.
293 const struct rte_memzone *
294 rte_memzone_reserve(const char *name, size_t len, int socket_id,
297 return rte_memzone_reserve_thread_safe(name, len, socket_id,
298 flags, RTE_CACHE_LINE_SIZE, 0);
302 rte_memzone_free(const struct rte_memzone *mz)
304 struct rte_mem_config *mcfg;
305 struct rte_fbarray *arr;
306 struct rte_memzone *found_mz;
314 mcfg = rte_eal_get_configuration()->mem_config;
315 arr = &mcfg->memzones;
317 rte_rwlock_write_lock(&mcfg->mlock);
319 idx = rte_fbarray_find_idx(arr, mz);
320 found_mz = rte_fbarray_get(arr, idx);
322 if (found_mz == NULL) {
324 } else if (found_mz->addr == NULL) {
325 RTE_LOG(ERR, EAL, "Memzone is not allocated\n");
328 addr = found_mz->addr;
329 memset(found_mz, 0, sizeof(*found_mz));
330 rte_fbarray_set_free(arr, idx);
333 rte_rwlock_write_unlock(&mcfg->mlock);
342 * Lookup for the memzone identified by the given name
344 const struct rte_memzone *
345 rte_memzone_lookup(const char *name)
347 struct rte_mem_config *mcfg;
348 const struct rte_memzone *memzone = NULL;
350 mcfg = rte_eal_get_configuration()->mem_config;
352 rte_rwlock_read_lock(&mcfg->mlock);
354 memzone = memzone_lookup_thread_unsafe(name);
356 rte_rwlock_read_unlock(&mcfg->mlock);
362 dump_memzone(const struct rte_memzone *mz, void *arg)
364 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
365 struct rte_memseg_list *msl = NULL;
366 void *cur_addr, *mz_end;
367 struct rte_memseg *ms;
372 mz_idx = rte_fbarray_find_idx(&mcfg->memzones, mz);
374 fprintf(f, "Zone %u: name:<%s>, len:0x%zx, virt:%p, "
375 "socket_id:%"PRId32", flags:%"PRIx32"\n",
383 /* go through each page occupied by this memzone */
384 msl = rte_mem_virt2memseg_list(mz->addr);
386 RTE_LOG(DEBUG, EAL, "Skipping bad memzone\n");
389 page_sz = (size_t)mz->hugepage_sz;
390 cur_addr = RTE_PTR_ALIGN_FLOOR(mz->addr, page_sz);
391 mz_end = RTE_PTR_ADD(cur_addr, mz->len);
393 fprintf(f, "physical segments used:\n");
394 ms_idx = RTE_PTR_DIFF(mz->addr, msl->base_va) / page_sz;
395 ms = rte_fbarray_get(&msl->memseg_arr, ms_idx);
398 fprintf(f, " addr: %p iova: 0x%" PRIx64 " "
401 cur_addr, ms->iova, ms->len, page_sz);
403 /* advance VA to next page */
404 cur_addr = RTE_PTR_ADD(cur_addr, page_sz);
406 /* memzones occupy contiguous segments */
408 } while (cur_addr < mz_end);
411 /* Dump all reserved memory zones on console */
413 rte_memzone_dump(FILE *f)
415 rte_memzone_walk(dump_memzone, f);
419 * Init the memzone subsystem
422 rte_eal_memzone_init(void)
424 struct rte_mem_config *mcfg;
426 /* get pointer to global configuration */
427 mcfg = rte_eal_get_configuration()->mem_config;
429 rte_rwlock_write_lock(&mcfg->mlock);
431 if (rte_eal_process_type() == RTE_PROC_PRIMARY &&
432 rte_fbarray_init(&mcfg->memzones, "memzone",
433 RTE_MAX_MEMZONE, sizeof(struct rte_memzone))) {
434 RTE_LOG(ERR, EAL, "Cannot allocate memzone list\n");
436 } else if (rte_eal_process_type() == RTE_PROC_SECONDARY &&
437 rte_fbarray_attach(&mcfg->memzones)) {
438 RTE_LOG(ERR, EAL, "Cannot attach to memzone list\n");
439 rte_rwlock_write_unlock(&mcfg->mlock);
443 rte_rwlock_write_unlock(&mcfg->mlock);
448 /* Walk all reserved memory zones */
449 void rte_memzone_walk(void (*func)(const struct rte_memzone *, void *),
452 struct rte_mem_config *mcfg;
453 struct rte_fbarray *arr;
456 mcfg = rte_eal_get_configuration()->mem_config;
457 arr = &mcfg->memzones;
459 rte_rwlock_read_lock(&mcfg->mlock);
460 i = rte_fbarray_find_next_used(arr, 0);
462 struct rte_memzone *mz = rte_fbarray_get(arr, i);
464 i = rte_fbarray_find_next_used(arr, i + 1);
466 rte_rwlock_read_unlock(&mcfg->mlock);