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