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