1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
11 #include <rte_errno.h>
12 #include <rte_memcpy.h>
13 #include <rte_memory.h>
15 #include <rte_eal_memconfig.h>
16 #include <rte_branch_prediction.h>
17 #include <rte_debug.h>
18 #include <rte_launch.h>
19 #include <rte_per_lcore.h>
20 #include <rte_lcore.h>
21 #include <rte_common.h>
22 #include <rte_spinlock.h>
24 #include <rte_malloc.h>
25 #include "malloc_elem.h"
26 #include "malloc_heap.h"
29 /* Free the memory space back to heap */
30 void rte_free(void *addr)
32 if (addr == NULL) return;
33 if (malloc_heap_free(malloc_elem_from_data(addr)) < 0)
34 RTE_LOG(ERR, EAL, "Error: Invalid memory\n");
38 * Allocate memory on specified heap.
41 rte_malloc_socket(const char *type, size_t size, unsigned int align,
44 /* return NULL if size is 0 or alignment is not power-of-2 */
45 if (size == 0 || (align && !rte_is_power_of_2(align)))
48 if (!rte_eal_has_hugepages())
49 socket_arg = SOCKET_ID_ANY;
51 return malloc_heap_alloc(type, size, socket_arg, 0,
52 align == 0 ? 1 : align, 0, false);
56 * Allocate memory on default heap.
59 rte_malloc(const char *type, size_t size, unsigned align)
61 return rte_malloc_socket(type, size, align, SOCKET_ID_ANY);
65 * Allocate zero'd memory on specified heap.
68 rte_zmalloc_socket(const char *type, size_t size, unsigned align, int socket)
70 return rte_malloc_socket(type, size, align, socket);
74 * Allocate zero'd memory on default heap.
77 rte_zmalloc(const char *type, size_t size, unsigned align)
79 return rte_zmalloc_socket(type, size, align, SOCKET_ID_ANY);
83 * Allocate zero'd memory on specified heap.
86 rte_calloc_socket(const char *type, size_t num, size_t size, unsigned align, int socket)
88 return rte_zmalloc_socket(type, num * size, align, socket);
92 * Allocate zero'd memory on default heap.
95 rte_calloc(const char *type, size_t num, size_t size, unsigned align)
97 return rte_zmalloc(type, num * size, align);
101 * Resize allocated memory.
104 rte_realloc(void *ptr, size_t size, unsigned align)
107 return rte_malloc(NULL, size, align);
109 struct malloc_elem *elem = malloc_elem_from_data(ptr);
111 RTE_LOG(ERR, EAL, "Error: memory corruption detected\n");
115 size = RTE_CACHE_LINE_ROUNDUP(size), align = RTE_CACHE_LINE_ROUNDUP(align);
116 /* check alignment matches first, and if ok, see if we can resize block */
117 if (RTE_PTR_ALIGN(ptr,align) == ptr &&
118 malloc_heap_resize(elem, size) == 0)
121 /* either alignment is off, or we have no room to expand,
123 void *new_ptr = rte_malloc(NULL, size, align);
126 const unsigned old_size = elem->size - MALLOC_ELEM_OVERHEAD;
127 rte_memcpy(new_ptr, ptr, old_size < size ? old_size : size);
134 rte_malloc_validate(const void *ptr, size_t *size)
136 const struct malloc_elem *elem = malloc_elem_from_data(ptr);
137 if (!malloc_elem_cookies_ok(elem))
140 *size = elem->size - elem->pad - MALLOC_ELEM_OVERHEAD;
145 * Function to retrieve data for heap on given socket
148 rte_malloc_get_socket_stats(int socket,
149 struct rte_malloc_socket_stats *socket_stats)
151 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
152 int heap_idx, ret = -1;
154 rte_rwlock_read_lock(&mcfg->memory_hotplug_lock);
156 heap_idx = malloc_socket_to_heap_id(socket);
160 ret = malloc_heap_get_stats(&mcfg->malloc_heaps[heap_idx],
163 rte_rwlock_read_unlock(&mcfg->memory_hotplug_lock);
169 * Function to dump contents of all heaps
171 void __rte_experimental
172 rte_malloc_dump_heaps(FILE *f)
174 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
177 rte_rwlock_read_lock(&mcfg->memory_hotplug_lock);
179 for (idx = 0; idx < RTE_MAX_HEAPS; idx++) {
180 fprintf(f, "Heap id: %u\n", idx);
181 malloc_heap_dump(&mcfg->malloc_heaps[idx], f);
184 rte_rwlock_read_unlock(&mcfg->memory_hotplug_lock);
188 rte_malloc_heap_get_socket(const char *name)
190 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
191 struct malloc_heap *heap = NULL;
196 strnlen(name, RTE_HEAP_NAME_MAX_LEN) == 0 ||
197 strnlen(name, RTE_HEAP_NAME_MAX_LEN) ==
198 RTE_HEAP_NAME_MAX_LEN) {
202 rte_rwlock_read_lock(&mcfg->memory_hotplug_lock);
203 for (idx = 0; idx < RTE_MAX_HEAPS; idx++) {
204 struct malloc_heap *tmp = &mcfg->malloc_heaps[idx];
206 if (!strncmp(name, tmp->name, RTE_HEAP_NAME_MAX_LEN)) {
213 ret = heap->socket_id;
218 rte_rwlock_read_unlock(&mcfg->memory_hotplug_lock);
224 rte_malloc_heap_socket_is_external(int socket_id)
226 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
230 if (socket_id == SOCKET_ID_ANY)
233 rte_rwlock_read_lock(&mcfg->memory_hotplug_lock);
234 for (idx = 0; idx < RTE_MAX_HEAPS; idx++) {
235 struct malloc_heap *tmp = &mcfg->malloc_heaps[idx];
237 if ((int)tmp->socket_id == socket_id) {
238 /* external memory always has large socket ID's */
239 ret = tmp->socket_id >= RTE_MAX_NUMA_NODES;
243 rte_rwlock_read_unlock(&mcfg->memory_hotplug_lock);
249 * Print stats on memory type. If type is NULL, info on all types is printed
252 rte_malloc_dump_stats(FILE *f, __rte_unused const char *type)
254 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
255 unsigned int heap_id;
256 struct rte_malloc_socket_stats sock_stats;
258 rte_rwlock_read_lock(&mcfg->memory_hotplug_lock);
260 /* Iterate through all initialised heaps */
261 for (heap_id = 0; heap_id < RTE_MAX_HEAPS; heap_id++) {
262 struct malloc_heap *heap = &mcfg->malloc_heaps[heap_id];
264 malloc_heap_get_stats(heap, &sock_stats);
266 fprintf(f, "Heap id:%u\n", heap_id);
267 fprintf(f, "\tHeap name:%s\n", heap->name);
268 fprintf(f, "\tHeap_size:%zu,\n", sock_stats.heap_totalsz_bytes);
269 fprintf(f, "\tFree_size:%zu,\n", sock_stats.heap_freesz_bytes);
270 fprintf(f, "\tAlloc_size:%zu,\n", sock_stats.heap_allocsz_bytes);
271 fprintf(f, "\tGreatest_free_size:%zu,\n",
272 sock_stats.greatest_free_size);
273 fprintf(f, "\tAlloc_count:%u,\n",sock_stats.alloc_count);
274 fprintf(f, "\tFree_count:%u,\n", sock_stats.free_count);
276 rte_rwlock_read_unlock(&mcfg->memory_hotplug_lock);
281 * TODO: Set limit to memory that can be allocated to memory type
284 rte_malloc_set_limit(__rte_unused const char *type,
285 __rte_unused size_t max)
291 * Return the IO address of a virtual address obtained through rte_malloc
294 rte_malloc_virt2iova(const void *addr)
296 const struct rte_memseg *ms;
297 struct malloc_elem *elem = malloc_elem_from_data(addr);
302 if (!elem->msl->external && rte_eal_iova_mode() == RTE_IOVA_VA)
303 return (uintptr_t) addr;
305 ms = rte_mem_virt2memseg(addr, elem->msl);
309 if (ms->iova == RTE_BAD_IOVA)
312 return ms->iova + RTE_PTR_DIFF(addr, ms->addr);
315 static struct malloc_heap *
316 find_named_heap(const char *name)
318 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
321 for (i = 0; i < RTE_MAX_HEAPS; i++) {
322 struct malloc_heap *heap = &mcfg->malloc_heaps[i];
324 if (!strncmp(name, heap->name, RTE_HEAP_NAME_MAX_LEN))
331 rte_malloc_heap_memory_add(const char *heap_name, void *va_addr, size_t len,
332 rte_iova_t iova_addrs[], unsigned int n_pages, size_t page_sz)
334 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
335 struct malloc_heap *heap = NULL;
339 if (heap_name == NULL || va_addr == NULL ||
340 page_sz == 0 || !rte_is_power_of_2(page_sz) ||
341 strnlen(heap_name, RTE_HEAP_NAME_MAX_LEN) == 0 ||
342 strnlen(heap_name, RTE_HEAP_NAME_MAX_LEN) ==
343 RTE_HEAP_NAME_MAX_LEN) {
348 rte_rwlock_write_lock(&mcfg->memory_hotplug_lock);
351 heap = find_named_heap(heap_name);
357 if (heap->socket_id < RTE_MAX_NUMA_NODES) {
358 /* cannot add memory to internal heaps */
364 if (n != n_pages && iova_addrs != NULL) {
370 rte_spinlock_lock(&heap->lock);
371 ret = malloc_heap_add_external_memory(heap, va_addr, iova_addrs, n,
373 rte_spinlock_unlock(&heap->lock);
376 rte_rwlock_write_unlock(&mcfg->memory_hotplug_lock);
382 rte_malloc_heap_memory_remove(const char *heap_name, void *va_addr, size_t len)
384 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
385 struct malloc_heap *heap = NULL;
388 if (heap_name == NULL || va_addr == NULL || len == 0 ||
389 strnlen(heap_name, RTE_HEAP_NAME_MAX_LEN) == 0 ||
390 strnlen(heap_name, RTE_HEAP_NAME_MAX_LEN) ==
391 RTE_HEAP_NAME_MAX_LEN) {
395 rte_rwlock_write_lock(&mcfg->memory_hotplug_lock);
397 heap = find_named_heap(heap_name);
403 if (heap->socket_id < RTE_MAX_NUMA_NODES) {
404 /* cannot remove memory from internal heaps */
410 rte_spinlock_lock(&heap->lock);
411 ret = malloc_heap_remove_external_memory(heap, va_addr, len);
412 rte_spinlock_unlock(&heap->lock);
415 rte_rwlock_write_unlock(&mcfg->memory_hotplug_lock);
421 rte_malloc_heap_create(const char *heap_name)
423 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
424 struct malloc_heap *heap = NULL;
427 if (heap_name == NULL ||
428 strnlen(heap_name, RTE_HEAP_NAME_MAX_LEN) == 0 ||
429 strnlen(heap_name, RTE_HEAP_NAME_MAX_LEN) ==
430 RTE_HEAP_NAME_MAX_LEN) {
434 /* check if there is space in the heap list, or if heap with this name
437 rte_rwlock_write_lock(&mcfg->memory_hotplug_lock);
439 for (i = 0; i < RTE_MAX_HEAPS; i++) {
440 struct malloc_heap *tmp = &mcfg->malloc_heaps[i];
442 if (strncmp(heap_name, tmp->name,
443 RTE_HEAP_NAME_MAX_LEN) == 0) {
444 RTE_LOG(ERR, EAL, "Heap %s already exists\n",
451 if (strnlen(tmp->name, RTE_HEAP_NAME_MAX_LEN) == 0) {
457 RTE_LOG(ERR, EAL, "Cannot create new heap: no space\n");
463 /* we're sure that we can create a new heap, so do it */
464 ret = malloc_heap_create(heap, heap_name);
466 rte_rwlock_write_unlock(&mcfg->memory_hotplug_lock);
472 rte_malloc_heap_destroy(const char *heap_name)
474 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
475 struct malloc_heap *heap = NULL;
478 if (heap_name == NULL ||
479 strnlen(heap_name, RTE_HEAP_NAME_MAX_LEN) == 0 ||
480 strnlen(heap_name, RTE_HEAP_NAME_MAX_LEN) ==
481 RTE_HEAP_NAME_MAX_LEN) {
485 rte_rwlock_write_lock(&mcfg->memory_hotplug_lock);
487 /* start from non-socket heaps */
488 heap = find_named_heap(heap_name);
490 RTE_LOG(ERR, EAL, "Heap %s not found\n", heap_name);
495 /* we shouldn't be able to destroy internal heaps */
496 if (heap->socket_id < RTE_MAX_NUMA_NODES) {
501 /* sanity checks done, now we can destroy the heap */
502 rte_spinlock_lock(&heap->lock);
503 ret = malloc_heap_destroy(heap);
505 /* if we failed, lock is still active */
507 rte_spinlock_unlock(&heap->lock);
509 rte_rwlock_write_unlock(&mcfg->memory_hotplug_lock);