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 const struct rte_mem_config *mcfg;
32 const struct rte_memzone *mz;
35 /* get pointer to global configuration */
36 mcfg = rte_eal_get_configuration()->mem_config;
39 * the algorithm is not optimal (linear), but there are few
40 * zones and this function should be called at init only
42 for (i = 0; i < RTE_MAX_MEMZONE; i++) {
43 mz = &mcfg->memzone[i];
44 if (mz->addr != NULL && !strncmp(name, mz->name, RTE_MEMZONE_NAMESIZE))
45 return &mcfg->memzone[i];
51 static inline struct rte_memzone *
52 get_next_free_memzone(void)
54 struct rte_mem_config *mcfg;
57 /* get pointer to global configuration */
58 mcfg = rte_eal_get_configuration()->mem_config;
60 for (i = 0; i < RTE_MAX_MEMZONE; i++) {
61 if (mcfg->memzone[i].addr == NULL)
62 return &mcfg->memzone[i];
68 /* This function will return the greatest free block if a heap has been
69 * specified. If no heap has been specified, it will return the heap and
70 * length of the greatest free block available in all heaps */
72 find_heap_max_free_elem(int *s, unsigned align)
74 struct rte_mem_config *mcfg;
75 struct rte_malloc_socket_stats stats;
79 /* get pointer to global configuration */
80 mcfg = rte_eal_get_configuration()->mem_config;
82 for (i = 0; i < RTE_MAX_NUMA_NODES; i++) {
83 if ((socket != SOCKET_ID_ANY) && (socket != i))
86 malloc_heap_get_stats(&mcfg->malloc_heaps[i], &stats);
87 if (stats.greatest_free_size > len) {
88 len = stats.greatest_free_size;
93 if (len < MALLOC_ELEM_OVERHEAD + align)
96 return len - MALLOC_ELEM_OVERHEAD - align;
99 static const struct rte_memzone *
100 memzone_reserve_aligned_thread_unsafe(const char *name, size_t len,
101 int socket_id, unsigned flags, unsigned align, unsigned bound)
103 struct rte_memzone *mz;
104 struct rte_mem_config *mcfg;
105 size_t requested_len;
108 /* get pointer to global configuration */
109 mcfg = rte_eal_get_configuration()->mem_config;
111 /* no more room in config */
112 if (mcfg->memzone_cnt >= RTE_MAX_MEMZONE) {
113 RTE_LOG(ERR, EAL, "%s(): No more room in config\n", __func__);
118 if (strlen(name) > sizeof(mz->name) - 1) {
119 RTE_LOG(DEBUG, EAL, "%s(): memzone <%s>: name too long\n",
121 rte_errno = ENAMETOOLONG;
125 /* zone already exist */
126 if ((memzone_lookup_thread_unsafe(name)) != NULL) {
127 RTE_LOG(DEBUG, EAL, "%s(): memzone <%s> already exists\n",
133 /* if alignment is not a power of two */
134 if (align && !rte_is_power_of_2(align)) {
135 RTE_LOG(ERR, EAL, "%s(): Invalid alignment: %u\n", __func__,
141 /* alignment less than cache size is not allowed */
142 if (align < RTE_CACHE_LINE_SIZE)
143 align = RTE_CACHE_LINE_SIZE;
145 /* align length on cache boundary. Check for overflow before doing so */
146 if (len > SIZE_MAX - RTE_CACHE_LINE_MASK) {
147 rte_errno = EINVAL; /* requested size too big */
151 len += RTE_CACHE_LINE_MASK;
152 len &= ~((size_t) RTE_CACHE_LINE_MASK);
154 /* save minimal requested length */
155 requested_len = RTE_MAX((size_t)RTE_CACHE_LINE_SIZE, len);
157 /* check that boundary condition is valid */
158 if (bound != 0 && (requested_len > bound || !rte_is_power_of_2(bound))) {
163 if ((socket_id != SOCKET_ID_ANY) &&
164 (socket_id >= RTE_MAX_NUMA_NODES || socket_id < 0)) {
169 if (!rte_eal_has_hugepages())
170 socket_id = SOCKET_ID_ANY;
174 requested_len = bound;
176 requested_len = find_heap_max_free_elem(&socket_id, align);
177 if (requested_len == 0) {
184 if (socket_id == SOCKET_ID_ANY)
185 socket = malloc_get_numa_socket();
189 /* allocate memory on heap */
190 void *mz_addr = malloc_heap_alloc(&mcfg->malloc_heaps[socket], NULL,
191 requested_len, flags, align, bound);
193 if ((mz_addr == NULL) && (socket_id == SOCKET_ID_ANY)) {
194 /* try other heaps */
195 for (i = 0; i < RTE_MAX_NUMA_NODES; i++) {
199 mz_addr = malloc_heap_alloc(&mcfg->malloc_heaps[i],
200 NULL, requested_len, flags, align, bound);
206 if (mz_addr == NULL) {
211 struct malloc_elem *elem = malloc_elem_from_data(mz_addr);
213 /* fill the zone in config */
214 mz = get_next_free_memzone();
217 RTE_LOG(ERR, EAL, "%s(): Cannot find free memzone but there is room "
218 "in config!\n", __func__);
219 malloc_elem_free(elem);
225 snprintf(mz->name, sizeof(mz->name), "%s", name);
226 mz->iova = rte_malloc_virt2iova(mz_addr);
228 mz->len = (requested_len == 0 ? elem->size : requested_len);
229 mz->hugepage_sz = elem->ms->hugepage_sz;
230 mz->socket_id = elem->ms->socket_id;
232 mz->memseg_id = elem->ms - rte_eal_get_configuration()->mem_config->memseg;
237 static const struct rte_memzone *
238 rte_memzone_reserve_thread_safe(const char *name, size_t len,
239 int socket_id, unsigned flags, unsigned align,
242 struct rte_mem_config *mcfg;
243 const struct rte_memzone *mz = NULL;
245 /* get pointer to global configuration */
246 mcfg = rte_eal_get_configuration()->mem_config;
248 rte_rwlock_write_lock(&mcfg->mlock);
250 mz = memzone_reserve_aligned_thread_unsafe(
251 name, len, socket_id, flags, align, bound);
253 rte_rwlock_write_unlock(&mcfg->mlock);
259 * Return a pointer to a correctly filled memzone descriptor (with a
260 * specified alignment and boundary). If the allocation cannot be done,
263 const struct rte_memzone *
264 rte_memzone_reserve_bounded(const char *name, size_t len, int socket_id,
265 unsigned flags, unsigned align, unsigned bound)
267 return rte_memzone_reserve_thread_safe(name, len, socket_id, flags,
272 * Return a pointer to a correctly filled memzone descriptor (with a
273 * specified alignment). If the allocation cannot be done, return NULL.
275 const struct rte_memzone *
276 rte_memzone_reserve_aligned(const char *name, size_t len, int socket_id,
277 unsigned flags, unsigned align)
279 return rte_memzone_reserve_thread_safe(name, len, socket_id, flags,
284 * Return a pointer to a correctly filled memzone descriptor. If the
285 * allocation cannot be done, return NULL.
287 const struct rte_memzone *
288 rte_memzone_reserve(const char *name, size_t len, int socket_id,
291 return rte_memzone_reserve_thread_safe(name, len, socket_id,
292 flags, RTE_CACHE_LINE_SIZE, 0);
296 rte_memzone_free(const struct rte_memzone *mz)
298 struct rte_mem_config *mcfg;
306 mcfg = rte_eal_get_configuration()->mem_config;
308 rte_rwlock_write_lock(&mcfg->mlock);
310 idx = ((uintptr_t)mz - (uintptr_t)mcfg->memzone);
311 idx = idx / sizeof(struct rte_memzone);
313 addr = mcfg->memzone[idx].addr;
316 else if (mcfg->memzone_cnt == 0) {
317 rte_panic("%s(): memzone address not NULL but memzone_cnt is 0!\n",
320 memset(&mcfg->memzone[idx], 0, sizeof(mcfg->memzone[idx]));
324 rte_rwlock_write_unlock(&mcfg->mlock);
332 * Lookup for the memzone identified by the given name
334 const struct rte_memzone *
335 rte_memzone_lookup(const char *name)
337 struct rte_mem_config *mcfg;
338 const struct rte_memzone *memzone = NULL;
340 mcfg = rte_eal_get_configuration()->mem_config;
342 rte_rwlock_read_lock(&mcfg->mlock);
344 memzone = memzone_lookup_thread_unsafe(name);
346 rte_rwlock_read_unlock(&mcfg->mlock);
351 /* Dump all reserved memory zones on console */
353 rte_memzone_dump(FILE *f)
355 struct rte_mem_config *mcfg;
358 /* get pointer to global configuration */
359 mcfg = rte_eal_get_configuration()->mem_config;
361 rte_rwlock_read_lock(&mcfg->mlock);
363 for (i=0; i<RTE_MAX_MEMZONE; i++) {
364 if (mcfg->memzone[i].addr == NULL)
366 fprintf(f, "Zone %u: name:<%s>, IO:0x%"PRIx64", len:0x%zx"
367 ", virt:%p, socket_id:%"PRId32", flags:%"PRIx32"\n", i,
368 mcfg->memzone[i].name,
369 mcfg->memzone[i].iova,
370 mcfg->memzone[i].len,
371 mcfg->memzone[i].addr,
372 mcfg->memzone[i].socket_id,
373 mcfg->memzone[i].flags);
375 rte_rwlock_read_unlock(&mcfg->mlock);
379 * Init the memzone subsystem
382 rte_eal_memzone_init(void)
384 struct rte_mem_config *mcfg;
385 const struct rte_memseg *memseg;
387 /* get pointer to global configuration */
388 mcfg = rte_eal_get_configuration()->mem_config;
390 /* secondary processes don't need to initialise anything */
391 if (rte_eal_process_type() == RTE_PROC_SECONDARY)
394 memseg = rte_eal_get_physmem_layout();
395 if (memseg == NULL) {
396 RTE_LOG(ERR, EAL, "%s(): Cannot get physical layout\n", __func__);
400 rte_rwlock_write_lock(&mcfg->mlock);
402 /* delete all zones */
403 mcfg->memzone_cnt = 0;
404 memset(mcfg->memzone, 0, sizeof(mcfg->memzone));
406 rte_rwlock_write_unlock(&mcfg->mlock);
408 return rte_eal_malloc_heap_init();
411 /* Walk all reserved memory zones */
412 void rte_memzone_walk(void (*func)(const struct rte_memzone *, void *),
415 struct rte_mem_config *mcfg;
418 mcfg = rte_eal_get_configuration()->mem_config;
420 rte_rwlock_read_lock(&mcfg->mlock);
421 for (i=0; i<RTE_MAX_MEMZONE; i++) {
422 if (mcfg->memzone[i].addr != NULL)
423 (*func)(&mcfg->memzone[i], arg);
425 rte_rwlock_read_unlock(&mcfg->mlock);