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