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"
27 #include "eal_memalloc.h"
30 /* Free the memory space back to heap */
31 void rte_free(void *addr)
33 if (addr == NULL) return;
34 if (malloc_heap_free(malloc_elem_from_data(addr)) < 0)
35 RTE_LOG(ERR, EAL, "Error: Invalid memory\n");
39 * Allocate memory on specified heap.
42 rte_malloc_socket(const char *type, size_t size, unsigned int align,
45 /* return NULL if size is 0 or alignment is not power-of-2 */
46 if (size == 0 || (align && !rte_is_power_of_2(align)))
49 /* if there are no hugepages and if we are not allocating from an
50 * external heap, use memory from any socket available. checking for
51 * socket being external may return -1 in case of invalid socket, but
52 * that's OK - if there are no hugepages, it doesn't matter.
54 if (rte_malloc_heap_socket_is_external(socket_arg) != 1 &&
55 !rte_eal_has_hugepages())
56 socket_arg = SOCKET_ID_ANY;
58 return malloc_heap_alloc(type, size, socket_arg, 0,
59 align == 0 ? 1 : align, 0, false);
63 * Allocate memory on default heap.
66 rte_malloc(const char *type, size_t size, unsigned align)
68 return rte_malloc_socket(type, size, align, SOCKET_ID_ANY);
72 * Allocate zero'd memory on specified heap.
75 rte_zmalloc_socket(const char *type, size_t size, unsigned align, int socket)
77 return rte_malloc_socket(type, size, align, socket);
81 * Allocate zero'd memory on default heap.
84 rte_zmalloc(const char *type, size_t size, unsigned align)
86 return rte_zmalloc_socket(type, size, align, SOCKET_ID_ANY);
90 * Allocate zero'd memory on specified heap.
93 rte_calloc_socket(const char *type, size_t num, size_t size, unsigned align, int socket)
95 return rte_zmalloc_socket(type, num * size, align, socket);
99 * Allocate zero'd memory on default heap.
102 rte_calloc(const char *type, size_t num, size_t size, unsigned align)
104 return rte_zmalloc(type, num * size, align);
108 * Resize allocated memory.
111 rte_realloc(void *ptr, size_t size, unsigned align)
114 return rte_malloc(NULL, size, align);
116 struct malloc_elem *elem = malloc_elem_from_data(ptr);
118 RTE_LOG(ERR, EAL, "Error: memory corruption detected\n");
122 size = RTE_CACHE_LINE_ROUNDUP(size), align = RTE_CACHE_LINE_ROUNDUP(align);
123 /* check alignment matches first, and if ok, see if we can resize block */
124 if (RTE_PTR_ALIGN(ptr,align) == ptr &&
125 malloc_heap_resize(elem, size) == 0)
128 /* either alignment is off, or we have no room to expand,
130 void *new_ptr = rte_malloc(NULL, size, align);
133 const unsigned old_size = elem->size - MALLOC_ELEM_OVERHEAD;
134 rte_memcpy(new_ptr, ptr, old_size < size ? old_size : size);
141 rte_malloc_validate(const void *ptr, size_t *size)
143 const struct malloc_elem *elem = malloc_elem_from_data(ptr);
144 if (!malloc_elem_cookies_ok(elem))
147 *size = elem->size - elem->pad - MALLOC_ELEM_OVERHEAD;
152 * Function to retrieve data for heap on given socket
155 rte_malloc_get_socket_stats(int socket,
156 struct rte_malloc_socket_stats *socket_stats)
158 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
159 int heap_idx, ret = -1;
161 rte_rwlock_read_lock(&mcfg->memory_hotplug_lock);
163 heap_idx = malloc_socket_to_heap_id(socket);
167 ret = malloc_heap_get_stats(&mcfg->malloc_heaps[heap_idx],
170 rte_rwlock_read_unlock(&mcfg->memory_hotplug_lock);
176 * Function to dump contents of all heaps
178 void __rte_experimental
179 rte_malloc_dump_heaps(FILE *f)
181 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
184 rte_rwlock_read_lock(&mcfg->memory_hotplug_lock);
186 for (idx = 0; idx < RTE_MAX_HEAPS; idx++) {
187 fprintf(f, "Heap id: %u\n", idx);
188 malloc_heap_dump(&mcfg->malloc_heaps[idx], f);
191 rte_rwlock_read_unlock(&mcfg->memory_hotplug_lock);
195 rte_malloc_heap_get_socket(const char *name)
197 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
198 struct malloc_heap *heap = NULL;
203 strnlen(name, RTE_HEAP_NAME_MAX_LEN) == 0 ||
204 strnlen(name, RTE_HEAP_NAME_MAX_LEN) ==
205 RTE_HEAP_NAME_MAX_LEN) {
209 rte_rwlock_read_lock(&mcfg->memory_hotplug_lock);
210 for (idx = 0; idx < RTE_MAX_HEAPS; idx++) {
211 struct malloc_heap *tmp = &mcfg->malloc_heaps[idx];
213 if (!strncmp(name, tmp->name, RTE_HEAP_NAME_MAX_LEN)) {
220 ret = heap->socket_id;
225 rte_rwlock_read_unlock(&mcfg->memory_hotplug_lock);
231 rte_malloc_heap_socket_is_external(int socket_id)
233 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
237 if (socket_id == SOCKET_ID_ANY)
240 rte_rwlock_read_lock(&mcfg->memory_hotplug_lock);
241 for (idx = 0; idx < RTE_MAX_HEAPS; idx++) {
242 struct malloc_heap *tmp = &mcfg->malloc_heaps[idx];
244 if ((int)tmp->socket_id == socket_id) {
245 /* external memory always has large socket ID's */
246 ret = tmp->socket_id >= RTE_MAX_NUMA_NODES;
250 rte_rwlock_read_unlock(&mcfg->memory_hotplug_lock);
256 * Print stats on memory type. If type is NULL, info on all types is printed
259 rte_malloc_dump_stats(FILE *f, __rte_unused const char *type)
261 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
262 unsigned int heap_id;
263 struct rte_malloc_socket_stats sock_stats;
265 rte_rwlock_read_lock(&mcfg->memory_hotplug_lock);
267 /* Iterate through all initialised heaps */
268 for (heap_id = 0; heap_id < RTE_MAX_HEAPS; heap_id++) {
269 struct malloc_heap *heap = &mcfg->malloc_heaps[heap_id];
271 malloc_heap_get_stats(heap, &sock_stats);
273 fprintf(f, "Heap id:%u\n", heap_id);
274 fprintf(f, "\tHeap name:%s\n", heap->name);
275 fprintf(f, "\tHeap_size:%zu,\n", sock_stats.heap_totalsz_bytes);
276 fprintf(f, "\tFree_size:%zu,\n", sock_stats.heap_freesz_bytes);
277 fprintf(f, "\tAlloc_size:%zu,\n", sock_stats.heap_allocsz_bytes);
278 fprintf(f, "\tGreatest_free_size:%zu,\n",
279 sock_stats.greatest_free_size);
280 fprintf(f, "\tAlloc_count:%u,\n",sock_stats.alloc_count);
281 fprintf(f, "\tFree_count:%u,\n", sock_stats.free_count);
283 rte_rwlock_read_unlock(&mcfg->memory_hotplug_lock);
288 * TODO: Set limit to memory that can be allocated to memory type
291 rte_malloc_set_limit(__rte_unused const char *type,
292 __rte_unused size_t max)
298 * Return the IO address of a virtual address obtained through rte_malloc
301 rte_malloc_virt2iova(const void *addr)
303 const struct rte_memseg *ms;
304 struct malloc_elem *elem = malloc_elem_from_data(addr);
309 if (!elem->msl->external && rte_eal_iova_mode() == RTE_IOVA_VA)
310 return (uintptr_t) addr;
312 ms = rte_mem_virt2memseg(addr, elem->msl);
316 if (ms->iova == RTE_BAD_IOVA)
319 return ms->iova + RTE_PTR_DIFF(addr, ms->addr);
322 static struct malloc_heap *
323 find_named_heap(const char *name)
325 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
328 for (i = 0; i < RTE_MAX_HEAPS; i++) {
329 struct malloc_heap *heap = &mcfg->malloc_heaps[i];
331 if (!strncmp(name, heap->name, RTE_HEAP_NAME_MAX_LEN))
338 rte_malloc_heap_memory_add(const char *heap_name, void *va_addr, size_t len,
339 rte_iova_t iova_addrs[], unsigned int n_pages, size_t page_sz)
341 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
342 struct malloc_heap *heap = NULL;
346 if (heap_name == NULL || va_addr == NULL ||
347 page_sz == 0 || !rte_is_power_of_2(page_sz) ||
348 strnlen(heap_name, RTE_HEAP_NAME_MAX_LEN) == 0 ||
349 strnlen(heap_name, RTE_HEAP_NAME_MAX_LEN) ==
350 RTE_HEAP_NAME_MAX_LEN) {
354 rte_rwlock_write_lock(&mcfg->memory_hotplug_lock);
357 heap = find_named_heap(heap_name);
363 if (heap->socket_id < RTE_MAX_NUMA_NODES) {
364 /* cannot add memory to internal heaps */
370 if (n != n_pages && iova_addrs != NULL) {
376 rte_spinlock_lock(&heap->lock);
377 ret = malloc_heap_add_external_memory(heap, va_addr, iova_addrs, n,
379 rte_spinlock_unlock(&heap->lock);
382 rte_rwlock_write_unlock(&mcfg->memory_hotplug_lock);
388 rte_malloc_heap_memory_remove(const char *heap_name, void *va_addr, size_t len)
390 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
391 struct malloc_heap *heap = NULL;
394 if (heap_name == NULL || va_addr == NULL || len == 0 ||
395 strnlen(heap_name, RTE_HEAP_NAME_MAX_LEN) == 0 ||
396 strnlen(heap_name, RTE_HEAP_NAME_MAX_LEN) ==
397 RTE_HEAP_NAME_MAX_LEN) {
401 rte_rwlock_write_lock(&mcfg->memory_hotplug_lock);
403 heap = find_named_heap(heap_name);
409 if (heap->socket_id < RTE_MAX_NUMA_NODES) {
410 /* cannot remove memory from internal heaps */
416 rte_spinlock_lock(&heap->lock);
417 ret = malloc_heap_remove_external_memory(heap, va_addr, len);
418 rte_spinlock_unlock(&heap->lock);
421 rte_rwlock_write_unlock(&mcfg->memory_hotplug_lock);
426 struct sync_mem_walk_arg {
434 sync_mem_walk(const struct rte_memseg_list *msl, void *arg)
436 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
437 struct sync_mem_walk_arg *wa = arg;
438 size_t len = msl->page_sz * msl->memseg_arr.len;
440 if (msl->base_va == wa->va_addr &&
442 struct rte_memseg_list *found_msl;
446 msl_idx = msl - mcfg->memsegs;
447 found_msl = &mcfg->memsegs[msl_idx];
450 ret = rte_fbarray_attach(&found_msl->memseg_arr);
452 /* notify all subscribers that a memory area is about to
455 eal_memalloc_mem_event_notify(RTE_MEM_EVENT_FREE,
456 msl->base_va, msl->len);
457 ret = rte_fbarray_detach(&found_msl->memseg_arr);
461 wa->result = -rte_errno;
463 /* notify all subscribers that a new memory area was
467 eal_memalloc_mem_event_notify(
469 msl->base_va, msl->len);
478 sync_memory(const char *heap_name, void *va_addr, size_t len, bool attach)
480 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
481 struct malloc_heap *heap = NULL;
482 struct sync_mem_walk_arg wa;
485 if (heap_name == NULL || va_addr == NULL || len == 0 ||
486 strnlen(heap_name, RTE_HEAP_NAME_MAX_LEN) == 0 ||
487 strnlen(heap_name, RTE_HEAP_NAME_MAX_LEN) ==
488 RTE_HEAP_NAME_MAX_LEN) {
492 rte_rwlock_read_lock(&mcfg->memory_hotplug_lock);
495 heap = find_named_heap(heap_name);
501 /* we shouldn't be able to sync to internal heaps */
502 if (heap->socket_id < RTE_MAX_NUMA_NODES) {
508 /* find corresponding memseg list to sync to */
509 wa.va_addr = va_addr;
511 wa.result = -ENOENT; /* fail unless explicitly told to succeed */
514 /* we're already holding a read lock */
515 rte_memseg_list_walk_thread_unsafe(sync_mem_walk, &wa);
518 rte_errno = -wa.result;
521 /* notify all subscribers that a new memory area was added */
523 eal_memalloc_mem_event_notify(RTE_MEM_EVENT_ALLOC,
528 rte_rwlock_read_unlock(&mcfg->memory_hotplug_lock);
533 rte_malloc_heap_memory_attach(const char *heap_name, void *va_addr, size_t len)
535 return sync_memory(heap_name, va_addr, len, true);
539 rte_malloc_heap_memory_detach(const char *heap_name, void *va_addr, size_t len)
541 return sync_memory(heap_name, va_addr, len, false);
545 rte_malloc_heap_create(const char *heap_name)
547 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
548 struct malloc_heap *heap = NULL;
551 if (heap_name == NULL ||
552 strnlen(heap_name, RTE_HEAP_NAME_MAX_LEN) == 0 ||
553 strnlen(heap_name, RTE_HEAP_NAME_MAX_LEN) ==
554 RTE_HEAP_NAME_MAX_LEN) {
558 /* check if there is space in the heap list, or if heap with this name
561 rte_rwlock_write_lock(&mcfg->memory_hotplug_lock);
563 for (i = 0; i < RTE_MAX_HEAPS; i++) {
564 struct malloc_heap *tmp = &mcfg->malloc_heaps[i];
566 if (strncmp(heap_name, tmp->name,
567 RTE_HEAP_NAME_MAX_LEN) == 0) {
568 RTE_LOG(ERR, EAL, "Heap %s already exists\n",
575 if (strnlen(tmp->name, RTE_HEAP_NAME_MAX_LEN) == 0) {
581 RTE_LOG(ERR, EAL, "Cannot create new heap: no space\n");
587 /* we're sure that we can create a new heap, so do it */
588 ret = malloc_heap_create(heap, heap_name);
590 rte_rwlock_write_unlock(&mcfg->memory_hotplug_lock);
596 rte_malloc_heap_destroy(const char *heap_name)
598 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
599 struct malloc_heap *heap = NULL;
602 if (heap_name == NULL ||
603 strnlen(heap_name, RTE_HEAP_NAME_MAX_LEN) == 0 ||
604 strnlen(heap_name, RTE_HEAP_NAME_MAX_LEN) ==
605 RTE_HEAP_NAME_MAX_LEN) {
609 rte_rwlock_write_lock(&mcfg->memory_hotplug_lock);
611 /* start from non-socket heaps */
612 heap = find_named_heap(heap_name);
614 RTE_LOG(ERR, EAL, "Heap %s not found\n", heap_name);
619 /* we shouldn't be able to destroy internal heaps */
620 if (heap->socket_id < RTE_MAX_NUMA_NODES) {
625 /* sanity checks done, now we can destroy the heap */
626 rte_spinlock_lock(&heap->lock);
627 ret = malloc_heap_destroy(heap);
629 /* if we failed, lock is still active */
631 rte_spinlock_unlock(&heap->lock);
633 rte_rwlock_write_unlock(&mcfg->memory_hotplug_lock);