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