malloc: support contiguous allocation
[dpdk.git] / lib / librte_eal / common / eal_common_memzone.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4
5 #include <stdlib.h>
6 #include <stdio.h>
7 #include <stdint.h>
8 #include <stdarg.h>
9 #include <inttypes.h>
10 #include <string.h>
11 #include <errno.h>
12 #include <sys/queue.h>
13
14 #include <rte_log.h>
15 #include <rte_memory.h>
16 #include <rte_memzone.h>
17 #include <rte_eal.h>
18 #include <rte_eal_memconfig.h>
19 #include <rte_per_lcore.h>
20 #include <rte_errno.h>
21 #include <rte_string_fns.h>
22 #include <rte_common.h>
23
24 #include "malloc_heap.h"
25 #include "malloc_elem.h"
26 #include "eal_private.h"
27
28 static inline const struct rte_memzone *
29 memzone_lookup_thread_unsafe(const char *name)
30 {
31         const struct rte_mem_config *mcfg;
32         const struct rte_memzone *mz;
33         unsigned i = 0;
34
35         /* get pointer to global configuration */
36         mcfg = rte_eal_get_configuration()->mem_config;
37
38         /*
39          * the algorithm is not optimal (linear), but there are few
40          * zones and this function should be called at init only
41          */
42         for (i = 0; i < RTE_MAX_MEMZONE; i++) {
43                 mz = &mcfg->memzone[i];
44                 if (mz->addr != NULL && !strncmp(name, mz->name, RTE_MEMZONE_NAMESIZE))
45                         return &mcfg->memzone[i];
46         }
47
48         return NULL;
49 }
50
51 static inline struct rte_memzone *
52 get_next_free_memzone(void)
53 {
54         struct rte_mem_config *mcfg;
55         unsigned i = 0;
56
57         /* get pointer to global configuration */
58         mcfg = rte_eal_get_configuration()->mem_config;
59
60         for (i = 0; i < RTE_MAX_MEMZONE; i++) {
61                 if (mcfg->memzone[i].addr == NULL)
62                         return &mcfg->memzone[i];
63         }
64
65         return NULL;
66 }
67
68 /* This function will return the greatest free block if a heap has been
69  * specified. If no heap has been specified, it will return the heap and
70  * length of the greatest free block available in all heaps */
71 static size_t
72 find_heap_max_free_elem(int *s, unsigned align)
73 {
74         struct rte_mem_config *mcfg;
75         struct rte_malloc_socket_stats stats;
76         int i, socket = *s;
77         size_t len = 0;
78
79         /* get pointer to global configuration */
80         mcfg = rte_eal_get_configuration()->mem_config;
81
82         for (i = 0; i < RTE_MAX_NUMA_NODES; i++) {
83                 if ((socket != SOCKET_ID_ANY) && (socket != i))
84                         continue;
85
86                 malloc_heap_get_stats(&mcfg->malloc_heaps[i], &stats);
87                 if (stats.greatest_free_size > len) {
88                         len = stats.greatest_free_size;
89                         *s = i;
90                 }
91         }
92
93         if (len < MALLOC_ELEM_OVERHEAD + align)
94                 return 0;
95
96         return len - MALLOC_ELEM_OVERHEAD - align;
97 }
98
99 static const struct rte_memzone *
100 memzone_reserve_aligned_thread_unsafe(const char *name, size_t len,
101                 int socket_id, unsigned int flags, unsigned int align,
102                 unsigned int bound, bool contig)
103 {
104         struct rte_memzone *mz;
105         struct rte_mem_config *mcfg;
106         size_t requested_len;
107         int socket, i;
108
109         /* get pointer to global configuration */
110         mcfg = rte_eal_get_configuration()->mem_config;
111
112         /* no more room in config */
113         if (mcfg->memzone_cnt >= RTE_MAX_MEMZONE) {
114                 RTE_LOG(ERR, EAL, "%s(): No more room in config\n", __func__);
115                 rte_errno = ENOSPC;
116                 return NULL;
117         }
118
119         if (strlen(name) > sizeof(mz->name) - 1) {
120                 RTE_LOG(DEBUG, EAL, "%s(): memzone <%s>: name too long\n",
121                         __func__, name);
122                 rte_errno = ENAMETOOLONG;
123                 return NULL;
124         }
125
126         /* zone already exist */
127         if ((memzone_lookup_thread_unsafe(name)) != NULL) {
128                 RTE_LOG(DEBUG, EAL, "%s(): memzone <%s> already exists\n",
129                         __func__, name);
130                 rte_errno = EEXIST;
131                 return NULL;
132         }
133
134         /* if alignment is not a power of two */
135         if (align && !rte_is_power_of_2(align)) {
136                 RTE_LOG(ERR, EAL, "%s(): Invalid alignment: %u\n", __func__,
137                                 align);
138                 rte_errno = EINVAL;
139                 return NULL;
140         }
141
142         /* alignment less than cache size is not allowed */
143         if (align < RTE_CACHE_LINE_SIZE)
144                 align = RTE_CACHE_LINE_SIZE;
145
146         /* align length on cache boundary. Check for overflow before doing so */
147         if (len > SIZE_MAX - RTE_CACHE_LINE_MASK) {
148                 rte_errno = EINVAL; /* requested size too big */
149                 return NULL;
150         }
151
152         len += RTE_CACHE_LINE_MASK;
153         len &= ~((size_t) RTE_CACHE_LINE_MASK);
154
155         /* save minimal requested  length */
156         requested_len = RTE_MAX((size_t)RTE_CACHE_LINE_SIZE,  len);
157
158         /* check that boundary condition is valid */
159         if (bound != 0 && (requested_len > bound || !rte_is_power_of_2(bound))) {
160                 rte_errno = EINVAL;
161                 return NULL;
162         }
163
164         if ((socket_id != SOCKET_ID_ANY) &&
165             (socket_id >= RTE_MAX_NUMA_NODES || socket_id < 0)) {
166                 rte_errno = EINVAL;
167                 return NULL;
168         }
169
170         if (!rte_eal_has_hugepages())
171                 socket_id = SOCKET_ID_ANY;
172
173         if (len == 0) {
174                 if (bound != 0)
175                         requested_len = bound;
176                 else {
177                         requested_len = find_heap_max_free_elem(&socket_id, align);
178                         if (requested_len == 0) {
179                                 rte_errno = ENOMEM;
180                                 return NULL;
181                         }
182                 }
183         }
184
185         if (socket_id == SOCKET_ID_ANY)
186                 socket = malloc_get_numa_socket();
187         else
188                 socket = socket_id;
189
190         /* allocate memory on heap */
191         void *mz_addr = malloc_heap_alloc(&mcfg->malloc_heaps[socket], NULL,
192                         requested_len, flags, align, bound, contig);
193
194         if ((mz_addr == NULL) && (socket_id == SOCKET_ID_ANY)) {
195                 /* try other heaps */
196                 for (i = 0; i < RTE_MAX_NUMA_NODES; i++) {
197                         if (socket == i)
198                                 continue;
199
200                         mz_addr = malloc_heap_alloc(&mcfg->malloc_heaps[i],
201                                         NULL, requested_len, flags, align,
202                                         bound, contig);
203                         if (mz_addr != NULL)
204                                 break;
205                 }
206         }
207
208         if (mz_addr == NULL) {
209                 rte_errno = ENOMEM;
210                 return NULL;
211         }
212
213         struct malloc_elem *elem = malloc_elem_from_data(mz_addr);
214
215         /* fill the zone in config */
216         mz = get_next_free_memzone();
217
218         if (mz == NULL) {
219                 RTE_LOG(ERR, EAL, "%s(): Cannot find free memzone but there is room "
220                                 "in config!\n", __func__);
221                 malloc_elem_free(elem);
222                 rte_errno = ENOSPC;
223                 return NULL;
224         }
225
226         mcfg->memzone_cnt++;
227         snprintf(mz->name, sizeof(mz->name), "%s", name);
228         mz->iova = rte_malloc_virt2iova(mz_addr);
229         mz->addr = mz_addr;
230         mz->len = (requested_len == 0 ? elem->size : requested_len);
231         mz->hugepage_sz = elem->ms->hugepage_sz;
232         mz->socket_id = elem->ms->socket_id;
233         mz->flags = 0;
234         mz->memseg_id = elem->ms - rte_eal_get_configuration()->mem_config->memseg;
235
236         return mz;
237 }
238
239 static const struct rte_memzone *
240 rte_memzone_reserve_thread_safe(const char *name, size_t len, int socket_id,
241                 unsigned int flags, unsigned int align, unsigned int bound,
242                 bool contig)
243 {
244         struct rte_mem_config *mcfg;
245         const struct rte_memzone *mz = NULL;
246
247         /* get pointer to global configuration */
248         mcfg = rte_eal_get_configuration()->mem_config;
249
250         rte_rwlock_write_lock(&mcfg->mlock);
251
252         mz = memzone_reserve_aligned_thread_unsafe(
253                 name, len, socket_id, flags, align, bound, contig);
254
255         rte_rwlock_write_unlock(&mcfg->mlock);
256
257         return mz;
258 }
259
260 /*
261  * Return a pointer to a correctly filled memzone descriptor (with a
262  * specified alignment and boundary). If the allocation cannot be done,
263  * return NULL.
264  */
265 const struct rte_memzone *
266 rte_memzone_reserve_bounded(const char *name, size_t len, int socket_id,
267                             unsigned flags, unsigned align, unsigned bound)
268 {
269         return rte_memzone_reserve_thread_safe(name, len, socket_id, flags,
270                                                align, bound, false);
271 }
272
273 /*
274  * Return a pointer to a correctly filled memzone descriptor (with a
275  * specified alignment). If the allocation cannot be done, return NULL.
276  */
277 const struct rte_memzone *
278 rte_memzone_reserve_aligned(const char *name, size_t len, int socket_id,
279                             unsigned flags, unsigned align)
280 {
281         return rte_memzone_reserve_thread_safe(name, len, socket_id, flags,
282                                                align, 0, false);
283 }
284
285 /*
286  * Return a pointer to a correctly filled memzone descriptor. If the
287  * allocation cannot be done, return NULL.
288  */
289 const struct rte_memzone *
290 rte_memzone_reserve(const char *name, size_t len, int socket_id,
291                     unsigned flags)
292 {
293         return rte_memzone_reserve_thread_safe(name, len, socket_id,
294                                                flags, RTE_CACHE_LINE_SIZE, 0,
295                                                false);
296 }
297
298 int
299 rte_memzone_free(const struct rte_memzone *mz)
300 {
301         struct rte_mem_config *mcfg;
302         int ret = 0;
303         void *addr;
304         unsigned idx;
305
306         if (mz == NULL)
307                 return -EINVAL;
308
309         mcfg = rte_eal_get_configuration()->mem_config;
310
311         rte_rwlock_write_lock(&mcfg->mlock);
312
313         idx = ((uintptr_t)mz - (uintptr_t)mcfg->memzone);
314         idx = idx / sizeof(struct rte_memzone);
315
316         addr = mcfg->memzone[idx].addr;
317         if (addr == NULL)
318                 ret = -EINVAL;
319         else if (mcfg->memzone_cnt == 0) {
320                 rte_panic("%s(): memzone address not NULL but memzone_cnt is 0!\n",
321                                 __func__);
322         } else {
323                 memset(&mcfg->memzone[idx], 0, sizeof(mcfg->memzone[idx]));
324                 mcfg->memzone_cnt--;
325         }
326
327         rte_rwlock_write_unlock(&mcfg->mlock);
328
329         rte_free(addr);
330
331         return ret;
332 }
333
334 /*
335  * Lookup for the memzone identified by the given name
336  */
337 const struct rte_memzone *
338 rte_memzone_lookup(const char *name)
339 {
340         struct rte_mem_config *mcfg;
341         const struct rte_memzone *memzone = NULL;
342
343         mcfg = rte_eal_get_configuration()->mem_config;
344
345         rte_rwlock_read_lock(&mcfg->mlock);
346
347         memzone = memzone_lookup_thread_unsafe(name);
348
349         rte_rwlock_read_unlock(&mcfg->mlock);
350
351         return memzone;
352 }
353
354 /* Dump all reserved memory zones on console */
355 void
356 rte_memzone_dump(FILE *f)
357 {
358         struct rte_mem_config *mcfg;
359         unsigned i = 0;
360
361         /* get pointer to global configuration */
362         mcfg = rte_eal_get_configuration()->mem_config;
363
364         rte_rwlock_read_lock(&mcfg->mlock);
365         /* dump all zones */
366         for (i=0; i<RTE_MAX_MEMZONE; i++) {
367                 if (mcfg->memzone[i].addr == NULL)
368                         break;
369                 fprintf(f, "Zone %u: name:<%s>, IO:0x%"PRIx64", len:0x%zx"
370                        ", virt:%p, socket_id:%"PRId32", flags:%"PRIx32"\n", i,
371                        mcfg->memzone[i].name,
372                        mcfg->memzone[i].iova,
373                        mcfg->memzone[i].len,
374                        mcfg->memzone[i].addr,
375                        mcfg->memzone[i].socket_id,
376                        mcfg->memzone[i].flags);
377         }
378         rte_rwlock_read_unlock(&mcfg->mlock);
379 }
380
381 /*
382  * Init the memzone subsystem
383  */
384 int
385 rte_eal_memzone_init(void)
386 {
387         struct rte_mem_config *mcfg;
388         const struct rte_memseg *memseg;
389
390         /* get pointer to global configuration */
391         mcfg = rte_eal_get_configuration()->mem_config;
392
393         /* secondary processes don't need to initialise anything */
394         if (rte_eal_process_type() == RTE_PROC_SECONDARY)
395                 return 0;
396
397         memseg = rte_eal_get_physmem_layout();
398         if (memseg == NULL) {
399                 RTE_LOG(ERR, EAL, "%s(): Cannot get physical layout\n", __func__);
400                 return -1;
401         }
402
403         rte_rwlock_write_lock(&mcfg->mlock);
404
405         /* delete all zones */
406         mcfg->memzone_cnt = 0;
407         memset(mcfg->memzone, 0, sizeof(mcfg->memzone));
408
409         rte_rwlock_write_unlock(&mcfg->mlock);
410
411         return rte_eal_malloc_heap_init();
412 }
413
414 /* Walk all reserved memory zones */
415 void rte_memzone_walk(void (*func)(const struct rte_memzone *, void *),
416                       void *arg)
417 {
418         struct rte_mem_config *mcfg;
419         unsigned i;
420
421         mcfg = rte_eal_get_configuration()->mem_config;
422
423         rte_rwlock_read_lock(&mcfg->mlock);
424         for (i=0; i<RTE_MAX_MEMZONE; i++) {
425                 if (mcfg->memzone[i].addr != NULL)
426                         (*func)(&mcfg->memzone[i], arg);
427         }
428         rte_rwlock_read_unlock(&mcfg->mlock);
429 }