malloc: index heaps using heap ID rather than NUMA node
[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_memcpy.h>
12 #include <rte_memory.h>
13 #include <rte_eal.h>
14 #include <rte_eal_memconfig.h>
15 #include <rte_branch_prediction.h>
16 #include <rte_debug.h>
17 #include <rte_launch.h>
18 #include <rte_per_lcore.h>
19 #include <rte_lcore.h>
20 #include <rte_common.h>
21 #include <rte_spinlock.h>
22
23 #include <rte_malloc.h>
24 #include "malloc_elem.h"
25 #include "malloc_heap.h"
26
27
28 /* Free the memory space back to heap */
29 void rte_free(void *addr)
30 {
31         if (addr == NULL) return;
32         if (malloc_heap_free(malloc_elem_from_data(addr)) < 0)
33                 RTE_LOG(ERR, EAL, "Error: Invalid memory\n");
34 }
35
36 /*
37  * Allocate memory on specified heap.
38  */
39 void *
40 rte_malloc_socket(const char *type, size_t size, unsigned int align,
41                 int socket_arg)
42 {
43         /* return NULL if size is 0 or alignment is not power-of-2 */
44         if (size == 0 || (align && !rte_is_power_of_2(align)))
45                 return NULL;
46
47         if (!rte_eal_has_hugepages())
48                 socket_arg = SOCKET_ID_ANY;
49
50         /* Check socket parameter */
51         if (socket_arg >= RTE_MAX_NUMA_NODES)
52                 return NULL;
53
54         return malloc_heap_alloc(type, size, socket_arg, 0,
55                         align == 0 ? 1 : align, 0, false);
56 }
57
58 /*
59  * Allocate memory on default heap.
60  */
61 void *
62 rte_malloc(const char *type, size_t size, unsigned align)
63 {
64         return rte_malloc_socket(type, size, align, SOCKET_ID_ANY);
65 }
66
67 /*
68  * Allocate zero'd memory on specified heap.
69  */
70 void *
71 rte_zmalloc_socket(const char *type, size_t size, unsigned align, int socket)
72 {
73         return rte_malloc_socket(type, size, align, socket);
74 }
75
76 /*
77  * Allocate zero'd memory on default heap.
78  */
79 void *
80 rte_zmalloc(const char *type, size_t size, unsigned align)
81 {
82         return rte_zmalloc_socket(type, size, align, SOCKET_ID_ANY);
83 }
84
85 /*
86  * Allocate zero'd memory on specified heap.
87  */
88 void *
89 rte_calloc_socket(const char *type, size_t num, size_t size, unsigned align, int socket)
90 {
91         return rte_zmalloc_socket(type, num * size, align, socket);
92 }
93
94 /*
95  * Allocate zero'd memory on default heap.
96  */
97 void *
98 rte_calloc(const char *type, size_t num, size_t size, unsigned align)
99 {
100         return rte_zmalloc(type, num * size, align);
101 }
102
103 /*
104  * Resize allocated memory.
105  */
106 void *
107 rte_realloc(void *ptr, size_t size, unsigned align)
108 {
109         if (ptr == NULL)
110                 return rte_malloc(NULL, size, align);
111
112         struct malloc_elem *elem = malloc_elem_from_data(ptr);
113         if (elem == NULL) {
114                 RTE_LOG(ERR, EAL, "Error: memory corruption detected\n");
115                 return NULL;
116         }
117
118         size = RTE_CACHE_LINE_ROUNDUP(size), align = RTE_CACHE_LINE_ROUNDUP(align);
119         /* check alignment matches first, and if ok, see if we can resize block */
120         if (RTE_PTR_ALIGN(ptr,align) == ptr &&
121                         malloc_heap_resize(elem, size) == 0)
122                 return ptr;
123
124         /* either alignment is off, or we have no room to expand,
125          * so move data. */
126         void *new_ptr = rte_malloc(NULL, size, align);
127         if (new_ptr == NULL)
128                 return NULL;
129         const unsigned old_size = elem->size - MALLOC_ELEM_OVERHEAD;
130         rte_memcpy(new_ptr, ptr, old_size < size ? old_size : size);
131         rte_free(ptr);
132
133         return new_ptr;
134 }
135
136 int
137 rte_malloc_validate(const void *ptr, size_t *size)
138 {
139         const struct malloc_elem *elem = malloc_elem_from_data(ptr);
140         if (!malloc_elem_cookies_ok(elem))
141                 return -1;
142         if (size != NULL)
143                 *size = elem->size - elem->pad - MALLOC_ELEM_OVERHEAD;
144         return 0;
145 }
146
147 /*
148  * Function to retrieve data for heap on given socket
149  */
150 int
151 rte_malloc_get_socket_stats(int socket,
152                 struct rte_malloc_socket_stats *socket_stats)
153 {
154         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
155         int heap_idx, ret = -1;
156
157         rte_rwlock_read_lock(&mcfg->memory_hotplug_lock);
158
159         heap_idx = malloc_socket_to_heap_id(socket);
160         if (heap_idx < 0)
161                 goto unlock;
162
163         ret = malloc_heap_get_stats(&mcfg->malloc_heaps[heap_idx],
164                         socket_stats);
165 unlock:
166         rte_rwlock_read_unlock(&mcfg->memory_hotplug_lock);
167
168         return ret;
169 }
170
171 /*
172  * Function to dump contents of all heaps
173  */
174 void __rte_experimental
175 rte_malloc_dump_heaps(FILE *f)
176 {
177         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
178         unsigned int idx;
179
180         rte_rwlock_read_lock(&mcfg->memory_hotplug_lock);
181
182         for (idx = 0; idx < RTE_MAX_HEAPS; idx++) {
183                 fprintf(f, "Heap id: %u\n", idx);
184                 malloc_heap_dump(&mcfg->malloc_heaps[idx], f);
185         }
186
187         rte_rwlock_read_unlock(&mcfg->memory_hotplug_lock);
188 }
189
190 /*
191  * Print stats on memory type. If type is NULL, info on all types is printed
192  */
193 void
194 rte_malloc_dump_stats(FILE *f, __rte_unused const char *type)
195 {
196         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
197         unsigned int heap_id;
198         struct rte_malloc_socket_stats sock_stats;
199
200         rte_rwlock_read_lock(&mcfg->memory_hotplug_lock);
201
202         /* Iterate through all initialised heaps */
203         for (heap_id = 0; heap_id < RTE_MAX_HEAPS; heap_id++) {
204                 struct malloc_heap *heap = &mcfg->malloc_heaps[heap_id];
205
206                 malloc_heap_get_stats(heap, &sock_stats);
207
208                 fprintf(f, "Heap id:%u\n", heap_id);
209                 fprintf(f, "\tHeap_size:%zu,\n", sock_stats.heap_totalsz_bytes);
210                 fprintf(f, "\tFree_size:%zu,\n", sock_stats.heap_freesz_bytes);
211                 fprintf(f, "\tAlloc_size:%zu,\n", sock_stats.heap_allocsz_bytes);
212                 fprintf(f, "\tGreatest_free_size:%zu,\n",
213                                 sock_stats.greatest_free_size);
214                 fprintf(f, "\tAlloc_count:%u,\n",sock_stats.alloc_count);
215                 fprintf(f, "\tFree_count:%u,\n", sock_stats.free_count);
216         }
217         rte_rwlock_read_unlock(&mcfg->memory_hotplug_lock);
218         return;
219 }
220
221 /*
222  * TODO: Set limit to memory that can be allocated to memory type
223  */
224 int
225 rte_malloc_set_limit(__rte_unused const char *type,
226                 __rte_unused size_t max)
227 {
228         return 0;
229 }
230
231 /*
232  * Return the IO address of a virtual address obtained through rte_malloc
233  */
234 rte_iova_t
235 rte_malloc_virt2iova(const void *addr)
236 {
237         const struct rte_memseg *ms;
238         struct malloc_elem *elem = malloc_elem_from_data(addr);
239
240         if (elem == NULL)
241                 return RTE_BAD_IOVA;
242
243         if (!elem->msl->external && rte_eal_iova_mode() == RTE_IOVA_VA)
244                 return (uintptr_t) addr;
245
246         ms = rte_mem_virt2memseg(addr, elem->msl);
247         if (ms == NULL)
248                 return RTE_BAD_IOVA;
249
250         if (ms->iova == RTE_BAD_IOVA)
251                 return RTE_BAD_IOVA;
252
253         return ms->iova + RTE_PTR_DIFF(addr, ms->addr);
254 }