mem: hide internal heap header
[dpdk.git] / lib / librte_eal / common / rte_malloc.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2019 Intel Corporation
3  */
4
5 #include <stdint.h>
6 #include <stddef.h>
7 #include <stdio.h>
8 #include <string.h>
9 #include <sys/queue.h>
10
11 #include <rte_errno.h>
12 #include <rte_memcpy.h>
13 #include <rte_memory.h>
14 #include <rte_eal.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>
23
24 #include <rte_malloc.h>
25 #include "malloc_elem.h"
26 #include "malloc_heap.h"
27 #include "eal_memalloc.h"
28 #include "eal_memcfg.h"
29
30
31 /* Free the memory space back to heap */
32 void rte_free(void *addr)
33 {
34         if (addr == NULL) return;
35         if (malloc_heap_free(malloc_elem_from_data(addr)) < 0)
36                 RTE_LOG(ERR, EAL, "Error: Invalid memory\n");
37 }
38
39 /*
40  * Allocate memory on specified heap.
41  */
42 void *
43 rte_malloc_socket(const char *type, size_t size, unsigned int align,
44                 int socket_arg)
45 {
46         /* return NULL if size is 0 or alignment is not power-of-2 */
47         if (size == 0 || (align && !rte_is_power_of_2(align)))
48                 return NULL;
49
50         /* if there are no hugepages and if we are not allocating from an
51          * external heap, use memory from any socket available. checking for
52          * socket being external may return -1 in case of invalid socket, but
53          * that's OK - if there are no hugepages, it doesn't matter.
54          */
55         if (rte_malloc_heap_socket_is_external(socket_arg) != 1 &&
56                                 !rte_eal_has_hugepages())
57                 socket_arg = SOCKET_ID_ANY;
58
59         return malloc_heap_alloc(type, size, socket_arg, 0,
60                         align == 0 ? 1 : align, 0, false);
61 }
62
63 /*
64  * Allocate memory on default heap.
65  */
66 void *
67 rte_malloc(const char *type, size_t size, unsigned align)
68 {
69         return rte_malloc_socket(type, size, align, SOCKET_ID_ANY);
70 }
71
72 /*
73  * Allocate zero'd memory on specified heap.
74  */
75 void *
76 rte_zmalloc_socket(const char *type, size_t size, unsigned align, int socket)
77 {
78         void *ptr = rte_malloc_socket(type, size, align, socket);
79
80 #ifdef RTE_MALLOC_DEBUG
81         /*
82          * If DEBUG is enabled, then freed memory is marked with poison
83          * value and set to zero on allocation.
84          * If DEBUG is not enabled then  memory is already zeroed.
85          */
86         if (ptr != NULL)
87                 memset(ptr, 0, size);
88 #endif
89         return ptr;
90 }
91
92 /*
93  * Allocate zero'd memory on default heap.
94  */
95 void *
96 rte_zmalloc(const char *type, size_t size, unsigned align)
97 {
98         return rte_zmalloc_socket(type, size, align, SOCKET_ID_ANY);
99 }
100
101 /*
102  * Allocate zero'd memory on specified heap.
103  */
104 void *
105 rte_calloc_socket(const char *type, size_t num, size_t size, unsigned align, int socket)
106 {
107         return rte_zmalloc_socket(type, num * size, align, socket);
108 }
109
110 /*
111  * Allocate zero'd memory on default heap.
112  */
113 void *
114 rte_calloc(const char *type, size_t num, size_t size, unsigned align)
115 {
116         return rte_zmalloc(type, num * size, align);
117 }
118
119 /*
120  * Resize allocated memory on specified heap.
121  */
122 void *
123 rte_realloc_socket(void *ptr, size_t size, unsigned int align, int socket)
124 {
125         if (ptr == NULL)
126                 return rte_malloc_socket(NULL, size, align, socket);
127
128         struct malloc_elem *elem = malloc_elem_from_data(ptr);
129         if (elem == NULL) {
130                 RTE_LOG(ERR, EAL, "Error: memory corruption detected\n");
131                 return NULL;
132         }
133
134         size = RTE_CACHE_LINE_ROUNDUP(size), align = RTE_CACHE_LINE_ROUNDUP(align);
135
136         /* check requested socket id and alignment matches first, and if ok,
137          * see if we can resize block
138          */
139         if ((socket == SOCKET_ID_ANY ||
140              (unsigned int)socket == elem->heap->socket_id) &&
141                         RTE_PTR_ALIGN(ptr, align) == ptr &&
142                         malloc_heap_resize(elem, size) == 0)
143                 return ptr;
144
145         /* either requested socket id doesn't match, alignment is off
146          * or we have no room to expand,
147          * so move the data.
148          */
149         void *new_ptr = rte_malloc_socket(NULL, size, align, socket);
150         if (new_ptr == NULL)
151                 return NULL;
152         const unsigned old_size = elem->size - MALLOC_ELEM_OVERHEAD;
153         rte_memcpy(new_ptr, ptr, old_size < size ? old_size : size);
154         rte_free(ptr);
155
156         return new_ptr;
157 }
158
159 /*
160  * Resize allocated memory.
161  */
162 void *
163 rte_realloc(void *ptr, size_t size, unsigned int align)
164 {
165         return rte_realloc_socket(ptr, size, align, SOCKET_ID_ANY);
166 }
167
168 int
169 rte_malloc_validate(const void *ptr, size_t *size)
170 {
171         const struct malloc_elem *elem = malloc_elem_from_data(ptr);
172         if (!malloc_elem_cookies_ok(elem))
173                 return -1;
174         if (size != NULL)
175                 *size = elem->size - elem->pad - MALLOC_ELEM_OVERHEAD;
176         return 0;
177 }
178
179 /*
180  * Function to retrieve data for heap on given socket
181  */
182 int
183 rte_malloc_get_socket_stats(int socket,
184                 struct rte_malloc_socket_stats *socket_stats)
185 {
186         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
187         int heap_idx;
188
189         heap_idx = malloc_socket_to_heap_id(socket);
190         if (heap_idx < 0)
191                 return -1;
192
193         return malloc_heap_get_stats(&mcfg->malloc_heaps[heap_idx],
194                         socket_stats);
195 }
196
197 /*
198  * Function to dump contents of all heaps
199  */
200 void
201 rte_malloc_dump_heaps(FILE *f)
202 {
203         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
204         unsigned int idx;
205
206         for (idx = 0; idx < RTE_MAX_HEAPS; idx++) {
207                 fprintf(f, "Heap id: %u\n", idx);
208                 malloc_heap_dump(&mcfg->malloc_heaps[idx], f);
209         }
210 }
211
212 int
213 rte_malloc_heap_get_socket(const char *name)
214 {
215         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
216         struct malloc_heap *heap = NULL;
217         unsigned int idx;
218         int ret;
219
220         if (name == NULL ||
221                         strnlen(name, RTE_HEAP_NAME_MAX_LEN) == 0 ||
222                         strnlen(name, RTE_HEAP_NAME_MAX_LEN) ==
223                                 RTE_HEAP_NAME_MAX_LEN) {
224                 rte_errno = EINVAL;
225                 return -1;
226         }
227         rte_mcfg_mem_read_lock();
228         for (idx = 0; idx < RTE_MAX_HEAPS; idx++) {
229                 struct malloc_heap *tmp = &mcfg->malloc_heaps[idx];
230
231                 if (!strncmp(name, tmp->name, RTE_HEAP_NAME_MAX_LEN)) {
232                         heap = tmp;
233                         break;
234                 }
235         }
236
237         if (heap != NULL) {
238                 ret = heap->socket_id;
239         } else {
240                 rte_errno = ENOENT;
241                 ret = -1;
242         }
243         rte_mcfg_mem_read_unlock();
244
245         return ret;
246 }
247
248 int
249 rte_malloc_heap_socket_is_external(int socket_id)
250 {
251         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
252         unsigned int idx;
253         int ret = -1;
254
255         if (socket_id == SOCKET_ID_ANY)
256                 return 0;
257
258         rte_mcfg_mem_read_lock();
259         for (idx = 0; idx < RTE_MAX_HEAPS; idx++) {
260                 struct malloc_heap *tmp = &mcfg->malloc_heaps[idx];
261
262                 if ((int)tmp->socket_id == socket_id) {
263                         /* external memory always has large socket ID's */
264                         ret = tmp->socket_id >= RTE_MAX_NUMA_NODES;
265                         break;
266                 }
267         }
268         rte_mcfg_mem_read_unlock();
269
270         return ret;
271 }
272
273 /*
274  * Print stats on memory type. If type is NULL, info on all types is printed
275  */
276 void
277 rte_malloc_dump_stats(FILE *f, __rte_unused const char *type)
278 {
279         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
280         unsigned int heap_id;
281         struct rte_malloc_socket_stats sock_stats;
282
283         /* Iterate through all initialised heaps */
284         for (heap_id = 0; heap_id < RTE_MAX_HEAPS; heap_id++) {
285                 struct malloc_heap *heap = &mcfg->malloc_heaps[heap_id];
286
287                 malloc_heap_get_stats(heap, &sock_stats);
288
289                 fprintf(f, "Heap id:%u\n", heap_id);
290                 fprintf(f, "\tHeap name:%s\n", heap->name);
291                 fprintf(f, "\tHeap_size:%zu,\n", sock_stats.heap_totalsz_bytes);
292                 fprintf(f, "\tFree_size:%zu,\n", sock_stats.heap_freesz_bytes);
293                 fprintf(f, "\tAlloc_size:%zu,\n", sock_stats.heap_allocsz_bytes);
294                 fprintf(f, "\tGreatest_free_size:%zu,\n",
295                                 sock_stats.greatest_free_size);
296                 fprintf(f, "\tAlloc_count:%u,\n",sock_stats.alloc_count);
297                 fprintf(f, "\tFree_count:%u,\n", sock_stats.free_count);
298         }
299         return;
300 }
301
302 /*
303  * TODO: Set limit to memory that can be allocated to memory type
304  */
305 int
306 rte_malloc_set_limit(__rte_unused const char *type,
307                 __rte_unused size_t max)
308 {
309         return 0;
310 }
311
312 /*
313  * Return the IO address of a virtual address obtained through rte_malloc
314  */
315 rte_iova_t
316 rte_malloc_virt2iova(const void *addr)
317 {
318         const struct rte_memseg *ms;
319         struct malloc_elem *elem = malloc_elem_from_data(addr);
320
321         if (elem == NULL)
322                 return RTE_BAD_IOVA;
323
324         if (!elem->msl->external && rte_eal_iova_mode() == RTE_IOVA_VA)
325                 return (uintptr_t) addr;
326
327         ms = rte_mem_virt2memseg(addr, elem->msl);
328         if (ms == NULL)
329                 return RTE_BAD_IOVA;
330
331         if (ms->iova == RTE_BAD_IOVA)
332                 return RTE_BAD_IOVA;
333
334         return ms->iova + RTE_PTR_DIFF(addr, ms->addr);
335 }
336
337 static struct malloc_heap *
338 find_named_heap(const char *name)
339 {
340         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
341         unsigned int i;
342
343         for (i = 0; i < RTE_MAX_HEAPS; i++) {
344                 struct malloc_heap *heap = &mcfg->malloc_heaps[i];
345
346                 if (!strncmp(name, heap->name, RTE_HEAP_NAME_MAX_LEN))
347                         return heap;
348         }
349         return NULL;
350 }
351
352 int
353 rte_malloc_heap_memory_add(const char *heap_name, void *va_addr, size_t len,
354                 rte_iova_t iova_addrs[], unsigned int n_pages, size_t page_sz)
355 {
356         struct malloc_heap *heap = NULL;
357         struct rte_memseg_list *msl;
358         unsigned int n;
359         int ret;
360
361         if (heap_name == NULL || va_addr == NULL ||
362                         page_sz == 0 || !rte_is_power_of_2(page_sz) ||
363                         RTE_ALIGN(len, page_sz) != len ||
364                         !rte_is_aligned(va_addr, page_sz) ||
365                         ((len / page_sz) != n_pages && iova_addrs != NULL) ||
366                         strnlen(heap_name, RTE_HEAP_NAME_MAX_LEN) == 0 ||
367                         strnlen(heap_name, RTE_HEAP_NAME_MAX_LEN) ==
368                                 RTE_HEAP_NAME_MAX_LEN) {
369                 rte_errno = EINVAL;
370                 return -1;
371         }
372         rte_mcfg_mem_write_lock();
373
374         /* find our heap */
375         heap = find_named_heap(heap_name);
376         if (heap == NULL) {
377                 rte_errno = ENOENT;
378                 ret = -1;
379                 goto unlock;
380         }
381         if (heap->socket_id < RTE_MAX_NUMA_NODES) {
382                 /* cannot add memory to internal heaps */
383                 rte_errno = EPERM;
384                 ret = -1;
385                 goto unlock;
386         }
387         n = len / page_sz;
388
389         msl = malloc_heap_create_external_seg(va_addr, iova_addrs, n, page_sz,
390                         heap_name, heap->socket_id);
391         if (msl == NULL) {
392                 ret = -1;
393                 goto unlock;
394         }
395
396         rte_spinlock_lock(&heap->lock);
397         ret = malloc_heap_add_external_memory(heap, msl);
398         rte_spinlock_unlock(&heap->lock);
399
400 unlock:
401         rte_mcfg_mem_write_unlock();
402
403         return ret;
404 }
405
406 int
407 rte_malloc_heap_memory_remove(const char *heap_name, void *va_addr, size_t len)
408 {
409         struct malloc_heap *heap = NULL;
410         struct rte_memseg_list *msl;
411         int ret;
412
413         if (heap_name == NULL || va_addr == NULL || len == 0 ||
414                         strnlen(heap_name, RTE_HEAP_NAME_MAX_LEN) == 0 ||
415                         strnlen(heap_name, RTE_HEAP_NAME_MAX_LEN) ==
416                                 RTE_HEAP_NAME_MAX_LEN) {
417                 rte_errno = EINVAL;
418                 return -1;
419         }
420         rte_mcfg_mem_write_lock();
421         /* find our heap */
422         heap = find_named_heap(heap_name);
423         if (heap == NULL) {
424                 rte_errno = ENOENT;
425                 ret = -1;
426                 goto unlock;
427         }
428         if (heap->socket_id < RTE_MAX_NUMA_NODES) {
429                 /* cannot remove memory from internal heaps */
430                 rte_errno = EPERM;
431                 ret = -1;
432                 goto unlock;
433         }
434
435         msl = malloc_heap_find_external_seg(va_addr, len);
436         if (msl == NULL) {
437                 ret = -1;
438                 goto unlock;
439         }
440
441         rte_spinlock_lock(&heap->lock);
442         ret = malloc_heap_remove_external_memory(heap, va_addr, len);
443         rte_spinlock_unlock(&heap->lock);
444         if (ret != 0)
445                 goto unlock;
446
447         ret = malloc_heap_destroy_external_seg(msl);
448
449 unlock:
450         rte_mcfg_mem_write_unlock();
451
452         return ret;
453 }
454
455 static int
456 sync_memory(const char *heap_name, void *va_addr, size_t len, bool attach)
457 {
458         struct malloc_heap *heap = NULL;
459         struct rte_memseg_list *msl;
460         int ret;
461
462         if (heap_name == NULL || va_addr == NULL || len == 0 ||
463                         strnlen(heap_name, RTE_HEAP_NAME_MAX_LEN) == 0 ||
464                         strnlen(heap_name, RTE_HEAP_NAME_MAX_LEN) ==
465                                 RTE_HEAP_NAME_MAX_LEN) {
466                 rte_errno = EINVAL;
467                 return -1;
468         }
469         rte_mcfg_mem_read_lock();
470
471         /* find our heap */
472         heap = find_named_heap(heap_name);
473         if (heap == NULL) {
474                 rte_errno = ENOENT;
475                 ret = -1;
476                 goto unlock;
477         }
478         /* we shouldn't be able to sync to internal heaps */
479         if (heap->socket_id < RTE_MAX_NUMA_NODES) {
480                 rte_errno = EPERM;
481                 ret = -1;
482                 goto unlock;
483         }
484
485         /* find corresponding memseg list to sync to */
486         msl = malloc_heap_find_external_seg(va_addr, len);
487         if (msl == NULL) {
488                 ret = -1;
489                 goto unlock;
490         }
491
492         if (attach) {
493                 ret = rte_fbarray_attach(&msl->memseg_arr);
494                 if (ret == 0) {
495                         /* notify all subscribers that a new memory area was
496                          * added.
497                          */
498                         eal_memalloc_mem_event_notify(RTE_MEM_EVENT_ALLOC,
499                                         va_addr, len);
500                 } else {
501                         ret = -1;
502                         goto unlock;
503                 }
504         } else {
505                 /* notify all subscribers that a memory area is about to
506                  * be removed.
507                  */
508                 eal_memalloc_mem_event_notify(RTE_MEM_EVENT_FREE,
509                                 msl->base_va, msl->len);
510                 ret = rte_fbarray_detach(&msl->memseg_arr);
511                 if (ret < 0) {
512                         ret = -1;
513                         goto unlock;
514                 }
515         }
516 unlock:
517         rte_mcfg_mem_read_unlock();
518         return ret;
519 }
520
521 int
522 rte_malloc_heap_memory_attach(const char *heap_name, void *va_addr, size_t len)
523 {
524         return sync_memory(heap_name, va_addr, len, true);
525 }
526
527 int
528 rte_malloc_heap_memory_detach(const char *heap_name, void *va_addr, size_t len)
529 {
530         return sync_memory(heap_name, va_addr, len, false);
531 }
532
533 int
534 rte_malloc_heap_create(const char *heap_name)
535 {
536         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
537         struct malloc_heap *heap = NULL;
538         int i, ret;
539
540         if (heap_name == NULL ||
541                         strnlen(heap_name, RTE_HEAP_NAME_MAX_LEN) == 0 ||
542                         strnlen(heap_name, RTE_HEAP_NAME_MAX_LEN) ==
543                                 RTE_HEAP_NAME_MAX_LEN) {
544                 rte_errno = EINVAL;
545                 return -1;
546         }
547         /* check if there is space in the heap list, or if heap with this name
548          * already exists.
549          */
550         rte_mcfg_mem_write_lock();
551
552         for (i = 0; i < RTE_MAX_HEAPS; i++) {
553                 struct malloc_heap *tmp = &mcfg->malloc_heaps[i];
554                 /* existing heap */
555                 if (strncmp(heap_name, tmp->name,
556                                 RTE_HEAP_NAME_MAX_LEN) == 0) {
557                         RTE_LOG(ERR, EAL, "Heap %s already exists\n",
558                                 heap_name);
559                         rte_errno = EEXIST;
560                         ret = -1;
561                         goto unlock;
562                 }
563                 /* empty heap */
564                 if (strnlen(tmp->name, RTE_HEAP_NAME_MAX_LEN) == 0) {
565                         heap = tmp;
566                         break;
567                 }
568         }
569         if (heap == NULL) {
570                 RTE_LOG(ERR, EAL, "Cannot create new heap: no space\n");
571                 rte_errno = ENOSPC;
572                 ret = -1;
573                 goto unlock;
574         }
575
576         /* we're sure that we can create a new heap, so do it */
577         ret = malloc_heap_create(heap, heap_name);
578 unlock:
579         rte_mcfg_mem_write_unlock();
580
581         return ret;
582 }
583
584 int
585 rte_malloc_heap_destroy(const char *heap_name)
586 {
587         struct malloc_heap *heap = NULL;
588         int ret;
589
590         if (heap_name == NULL ||
591                         strnlen(heap_name, RTE_HEAP_NAME_MAX_LEN) == 0 ||
592                         strnlen(heap_name, RTE_HEAP_NAME_MAX_LEN) ==
593                                 RTE_HEAP_NAME_MAX_LEN) {
594                 rte_errno = EINVAL;
595                 return -1;
596         }
597         rte_mcfg_mem_write_lock();
598
599         /* start from non-socket heaps */
600         heap = find_named_heap(heap_name);
601         if (heap == NULL) {
602                 RTE_LOG(ERR, EAL, "Heap %s not found\n", heap_name);
603                 rte_errno = ENOENT;
604                 ret = -1;
605                 goto unlock;
606         }
607         /* we shouldn't be able to destroy internal heaps */
608         if (heap->socket_id < RTE_MAX_NUMA_NODES) {
609                 rte_errno = EPERM;
610                 ret = -1;
611                 goto unlock;
612         }
613         /* sanity checks done, now we can destroy the heap */
614         rte_spinlock_lock(&heap->lock);
615         ret = malloc_heap_destroy(heap);
616
617         /* if we failed, lock is still active */
618         if (ret < 0)
619                 rte_spinlock_unlock(&heap->lock);
620 unlock:
621         rte_mcfg_mem_write_unlock();
622
623         return ret;
624 }