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