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