malloc: allow attaching to external memory chunks
[dpdk.git] / lib / librte_eal / common / rte_malloc.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 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
28
29 /* Free the memory space back to heap */
30 void rte_free(void *addr)
31 {
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");
35 }
36
37 /*
38  * Allocate memory on specified heap.
39  */
40 void *
41 rte_malloc_socket(const char *type, size_t size, unsigned int align,
42                 int socket_arg)
43 {
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)))
46                 return NULL;
47
48         if (!rte_eal_has_hugepages())
49                 socket_arg = SOCKET_ID_ANY;
50
51         return malloc_heap_alloc(type, size, socket_arg, 0,
52                         align == 0 ? 1 : align, 0, false);
53 }
54
55 /*
56  * Allocate memory on default heap.
57  */
58 void *
59 rte_malloc(const char *type, size_t size, unsigned align)
60 {
61         return rte_malloc_socket(type, size, align, SOCKET_ID_ANY);
62 }
63
64 /*
65  * Allocate zero'd memory on specified heap.
66  */
67 void *
68 rte_zmalloc_socket(const char *type, size_t size, unsigned align, int socket)
69 {
70         return rte_malloc_socket(type, size, align, socket);
71 }
72
73 /*
74  * Allocate zero'd memory on default heap.
75  */
76 void *
77 rte_zmalloc(const char *type, size_t size, unsigned align)
78 {
79         return rte_zmalloc_socket(type, size, align, SOCKET_ID_ANY);
80 }
81
82 /*
83  * Allocate zero'd memory on specified heap.
84  */
85 void *
86 rte_calloc_socket(const char *type, size_t num, size_t size, unsigned align, int socket)
87 {
88         return rte_zmalloc_socket(type, num * size, align, socket);
89 }
90
91 /*
92  * Allocate zero'd memory on default heap.
93  */
94 void *
95 rte_calloc(const char *type, size_t num, size_t size, unsigned align)
96 {
97         return rte_zmalloc(type, num * size, align);
98 }
99
100 /*
101  * Resize allocated memory.
102  */
103 void *
104 rte_realloc(void *ptr, size_t size, unsigned align)
105 {
106         if (ptr == NULL)
107                 return rte_malloc(NULL, size, align);
108
109         struct malloc_elem *elem = malloc_elem_from_data(ptr);
110         if (elem == NULL) {
111                 RTE_LOG(ERR, EAL, "Error: memory corruption detected\n");
112                 return NULL;
113         }
114
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)
119                 return ptr;
120
121         /* either alignment is off, or we have no room to expand,
122          * so move data. */
123         void *new_ptr = rte_malloc(NULL, size, align);
124         if (new_ptr == NULL)
125                 return NULL;
126         const unsigned old_size = elem->size - MALLOC_ELEM_OVERHEAD;
127         rte_memcpy(new_ptr, ptr, old_size < size ? old_size : size);
128         rte_free(ptr);
129
130         return new_ptr;
131 }
132
133 int
134 rte_malloc_validate(const void *ptr, size_t *size)
135 {
136         const struct malloc_elem *elem = malloc_elem_from_data(ptr);
137         if (!malloc_elem_cookies_ok(elem))
138                 return -1;
139         if (size != NULL)
140                 *size = elem->size - elem->pad - MALLOC_ELEM_OVERHEAD;
141         return 0;
142 }
143
144 /*
145  * Function to retrieve data for heap on given socket
146  */
147 int
148 rte_malloc_get_socket_stats(int socket,
149                 struct rte_malloc_socket_stats *socket_stats)
150 {
151         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
152         int heap_idx, ret = -1;
153
154         rte_rwlock_read_lock(&mcfg->memory_hotplug_lock);
155
156         heap_idx = malloc_socket_to_heap_id(socket);
157         if (heap_idx < 0)
158                 goto unlock;
159
160         ret = malloc_heap_get_stats(&mcfg->malloc_heaps[heap_idx],
161                         socket_stats);
162 unlock:
163         rte_rwlock_read_unlock(&mcfg->memory_hotplug_lock);
164
165         return ret;
166 }
167
168 /*
169  * Function to dump contents of all heaps
170  */
171 void __rte_experimental
172 rte_malloc_dump_heaps(FILE *f)
173 {
174         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
175         unsigned int idx;
176
177         rte_rwlock_read_lock(&mcfg->memory_hotplug_lock);
178
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);
182         }
183
184         rte_rwlock_read_unlock(&mcfg->memory_hotplug_lock);
185 }
186
187 int
188 rte_malloc_heap_get_socket(const char *name)
189 {
190         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
191         struct malloc_heap *heap = NULL;
192         unsigned int idx;
193         int ret;
194
195         if (name == NULL ||
196                         strnlen(name, RTE_HEAP_NAME_MAX_LEN) == 0 ||
197                         strnlen(name, RTE_HEAP_NAME_MAX_LEN) ==
198                                 RTE_HEAP_NAME_MAX_LEN) {
199                 rte_errno = EINVAL;
200                 return -1;
201         }
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];
205
206                 if (!strncmp(name, tmp->name, RTE_HEAP_NAME_MAX_LEN)) {
207                         heap = tmp;
208                         break;
209                 }
210         }
211
212         if (heap != NULL) {
213                 ret = heap->socket_id;
214         } else {
215                 rte_errno = ENOENT;
216                 ret = -1;
217         }
218         rte_rwlock_read_unlock(&mcfg->memory_hotplug_lock);
219
220         return ret;
221 }
222
223 int
224 rte_malloc_heap_socket_is_external(int socket_id)
225 {
226         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
227         unsigned int idx;
228         int ret = -1;
229
230         if (socket_id == SOCKET_ID_ANY)
231                 return 0;
232
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];
236
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;
240                         break;
241                 }
242         }
243         rte_rwlock_read_unlock(&mcfg->memory_hotplug_lock);
244
245         return ret;
246 }
247
248 /*
249  * Print stats on memory type. If type is NULL, info on all types is printed
250  */
251 void
252 rte_malloc_dump_stats(FILE *f, __rte_unused const char *type)
253 {
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;
257
258         rte_rwlock_read_lock(&mcfg->memory_hotplug_lock);
259
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];
263
264                 malloc_heap_get_stats(heap, &sock_stats);
265
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);
275         }
276         rte_rwlock_read_unlock(&mcfg->memory_hotplug_lock);
277         return;
278 }
279
280 /*
281  * TODO: Set limit to memory that can be allocated to memory type
282  */
283 int
284 rte_malloc_set_limit(__rte_unused const char *type,
285                 __rte_unused size_t max)
286 {
287         return 0;
288 }
289
290 /*
291  * Return the IO address of a virtual address obtained through rte_malloc
292  */
293 rte_iova_t
294 rte_malloc_virt2iova(const void *addr)
295 {
296         const struct rte_memseg *ms;
297         struct malloc_elem *elem = malloc_elem_from_data(addr);
298
299         if (elem == NULL)
300                 return RTE_BAD_IOVA;
301
302         if (!elem->msl->external && rte_eal_iova_mode() == RTE_IOVA_VA)
303                 return (uintptr_t) addr;
304
305         ms = rte_mem_virt2memseg(addr, elem->msl);
306         if (ms == NULL)
307                 return RTE_BAD_IOVA;
308
309         if (ms->iova == RTE_BAD_IOVA)
310                 return RTE_BAD_IOVA;
311
312         return ms->iova + RTE_PTR_DIFF(addr, ms->addr);
313 }
314
315 static struct malloc_heap *
316 find_named_heap(const char *name)
317 {
318         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
319         unsigned int i;
320
321         for (i = 0; i < RTE_MAX_HEAPS; i++) {
322                 struct malloc_heap *heap = &mcfg->malloc_heaps[i];
323
324                 if (!strncmp(name, heap->name, RTE_HEAP_NAME_MAX_LEN))
325                         return heap;
326         }
327         return NULL;
328 }
329
330 int
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)
333 {
334         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
335         struct malloc_heap *heap = NULL;
336         unsigned int n;
337         int ret;
338
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) {
344                 rte_errno = EINVAL;
345                 ret = -1;
346                 goto unlock;
347         }
348         rte_rwlock_write_lock(&mcfg->memory_hotplug_lock);
349
350         /* find our heap */
351         heap = find_named_heap(heap_name);
352         if (heap == NULL) {
353                 rte_errno = ENOENT;
354                 ret = -1;
355                 goto unlock;
356         }
357         if (heap->socket_id < RTE_MAX_NUMA_NODES) {
358                 /* cannot add memory to internal heaps */
359                 rte_errno = EPERM;
360                 ret = -1;
361                 goto unlock;
362         }
363         n = len / page_sz;
364         if (n != n_pages && iova_addrs != NULL) {
365                 rte_errno = EINVAL;
366                 ret = -1;
367                 goto unlock;
368         }
369
370         rte_spinlock_lock(&heap->lock);
371         ret = malloc_heap_add_external_memory(heap, va_addr, iova_addrs, n,
372                         page_sz);
373         rte_spinlock_unlock(&heap->lock);
374
375 unlock:
376         rte_rwlock_write_unlock(&mcfg->memory_hotplug_lock);
377
378         return ret;
379 }
380
381 int
382 rte_malloc_heap_memory_remove(const char *heap_name, void *va_addr, size_t len)
383 {
384         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
385         struct malloc_heap *heap = NULL;
386         int ret;
387
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) {
392                 rte_errno = EINVAL;
393                 return -1;
394         }
395         rte_rwlock_write_lock(&mcfg->memory_hotplug_lock);
396         /* find our heap */
397         heap = find_named_heap(heap_name);
398         if (heap == NULL) {
399                 rte_errno = ENOENT;
400                 ret = -1;
401                 goto unlock;
402         }
403         if (heap->socket_id < RTE_MAX_NUMA_NODES) {
404                 /* cannot remove memory from internal heaps */
405                 rte_errno = EPERM;
406                 ret = -1;
407                 goto unlock;
408         }
409
410         rte_spinlock_lock(&heap->lock);
411         ret = malloc_heap_remove_external_memory(heap, va_addr, len);
412         rte_spinlock_unlock(&heap->lock);
413
414 unlock:
415         rte_rwlock_write_unlock(&mcfg->memory_hotplug_lock);
416
417         return ret;
418 }
419
420 struct sync_mem_walk_arg {
421         void *va_addr;
422         size_t len;
423         int result;
424 };
425
426 static int
427 attach_mem_walk(const struct rte_memseg_list *msl, void *arg)
428 {
429         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
430         struct sync_mem_walk_arg *wa = arg;
431         size_t len = msl->page_sz * msl->memseg_arr.len;
432
433         if (msl->base_va == wa->va_addr &&
434                         len == wa->len) {
435                 struct rte_memseg_list *found_msl;
436                 int msl_idx, ret;
437
438                 /* msl is const */
439                 msl_idx = msl - mcfg->memsegs;
440                 found_msl = &mcfg->memsegs[msl_idx];
441
442                 ret = rte_fbarray_attach(&found_msl->memseg_arr);
443
444                 if (ret < 0)
445                         wa->result = -rte_errno;
446                 else
447                         wa->result = 0;
448                 return 1;
449         }
450         return 0;
451 }
452
453 int
454 rte_malloc_heap_memory_attach(const char *heap_name, void *va_addr, size_t len)
455 {
456         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
457         struct malloc_heap *heap = NULL;
458         struct sync_mem_walk_arg wa;
459         int ret;
460
461         if (heap_name == NULL || va_addr == NULL || len == 0 ||
462                         strnlen(heap_name, RTE_HEAP_NAME_MAX_LEN) == 0 ||
463                         strnlen(heap_name, RTE_HEAP_NAME_MAX_LEN) ==
464                                 RTE_HEAP_NAME_MAX_LEN) {
465                 rte_errno = EINVAL;
466                 return -1;
467         }
468         rte_rwlock_read_lock(&mcfg->memory_hotplug_lock);
469
470         /* find our heap */
471         heap = find_named_heap(heap_name);
472         if (heap == NULL) {
473                 rte_errno = ENOENT;
474                 ret = -1;
475                 goto unlock;
476         }
477         /* we shouldn't be able to attach to internal heaps */
478         if (heap->socket_id < RTE_MAX_NUMA_NODES) {
479                 rte_errno = EPERM;
480                 ret = -1;
481                 goto unlock;
482         }
483
484         /* find corresponding memseg list to attach to */
485         wa.va_addr = va_addr;
486         wa.len = len;
487         wa.result = -ENOENT; /* fail unless explicitly told to succeed */
488
489         /* we're already holding a read lock */
490         rte_memseg_list_walk_thread_unsafe(attach_mem_walk, &wa);
491
492         if (wa.result < 0) {
493                 rte_errno = -wa.result;
494                 ret = -1;
495         } else {
496                 ret = 0;
497         }
498 unlock:
499         rte_rwlock_read_unlock(&mcfg->memory_hotplug_lock);
500         return ret;
501 }
502
503 int
504 rte_malloc_heap_create(const char *heap_name)
505 {
506         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
507         struct malloc_heap *heap = NULL;
508         int i, ret;
509
510         if (heap_name == NULL ||
511                         strnlen(heap_name, RTE_HEAP_NAME_MAX_LEN) == 0 ||
512                         strnlen(heap_name, RTE_HEAP_NAME_MAX_LEN) ==
513                                 RTE_HEAP_NAME_MAX_LEN) {
514                 rte_errno = EINVAL;
515                 return -1;
516         }
517         /* check if there is space in the heap list, or if heap with this name
518          * already exists.
519          */
520         rte_rwlock_write_lock(&mcfg->memory_hotplug_lock);
521
522         for (i = 0; i < RTE_MAX_HEAPS; i++) {
523                 struct malloc_heap *tmp = &mcfg->malloc_heaps[i];
524                 /* existing heap */
525                 if (strncmp(heap_name, tmp->name,
526                                 RTE_HEAP_NAME_MAX_LEN) == 0) {
527                         RTE_LOG(ERR, EAL, "Heap %s already exists\n",
528                                 heap_name);
529                         rte_errno = EEXIST;
530                         ret = -1;
531                         goto unlock;
532                 }
533                 /* empty heap */
534                 if (strnlen(tmp->name, RTE_HEAP_NAME_MAX_LEN) == 0) {
535                         heap = tmp;
536                         break;
537                 }
538         }
539         if (heap == NULL) {
540                 RTE_LOG(ERR, EAL, "Cannot create new heap: no space\n");
541                 rte_errno = ENOSPC;
542                 ret = -1;
543                 goto unlock;
544         }
545
546         /* we're sure that we can create a new heap, so do it */
547         ret = malloc_heap_create(heap, heap_name);
548 unlock:
549         rte_rwlock_write_unlock(&mcfg->memory_hotplug_lock);
550
551         return ret;
552 }
553
554 int
555 rte_malloc_heap_destroy(const char *heap_name)
556 {
557         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
558         struct malloc_heap *heap = NULL;
559         int ret;
560
561         if (heap_name == NULL ||
562                         strnlen(heap_name, RTE_HEAP_NAME_MAX_LEN) == 0 ||
563                         strnlen(heap_name, RTE_HEAP_NAME_MAX_LEN) ==
564                                 RTE_HEAP_NAME_MAX_LEN) {
565                 rte_errno = EINVAL;
566                 return -1;
567         }
568         rte_rwlock_write_lock(&mcfg->memory_hotplug_lock);
569
570         /* start from non-socket heaps */
571         heap = find_named_heap(heap_name);
572         if (heap == NULL) {
573                 RTE_LOG(ERR, EAL, "Heap %s not found\n", heap_name);
574                 rte_errno = ENOENT;
575                 ret = -1;
576                 goto unlock;
577         }
578         /* we shouldn't be able to destroy internal heaps */
579         if (heap->socket_id < RTE_MAX_NUMA_NODES) {
580                 rte_errno = EPERM;
581                 ret = -1;
582                 goto unlock;
583         }
584         /* sanity checks done, now we can destroy the heap */
585         rte_spinlock_lock(&heap->lock);
586         ret = malloc_heap_destroy(heap);
587
588         /* if we failed, lock is still active */
589         if (ret < 0)
590                 rte_spinlock_unlock(&heap->lock);
591 unlock:
592         rte_rwlock_write_unlock(&mcfg->memory_hotplug_lock);
593
594         return ret;
595 }