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