memzone: enable IOVA-contiguous reserving
[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)
103 {
104         struct rte_memzone *mz;
105         struct rte_mem_config *mcfg;
106         size_t requested_len;
107         int socket, i;
108         bool contig;
109
110         /* get pointer to global configuration */
111         mcfg = rte_eal_get_configuration()->mem_config;
112
113         /* no more room in config */
114         if (mcfg->memzone_cnt >= RTE_MAX_MEMZONE) {
115                 RTE_LOG(ERR, EAL, "%s(): No more room in config\n", __func__);
116                 rte_errno = ENOSPC;
117                 return NULL;
118         }
119
120         if (strlen(name) > sizeof(mz->name) - 1) {
121                 RTE_LOG(DEBUG, EAL, "%s(): memzone <%s>: name too long\n",
122                         __func__, name);
123                 rte_errno = ENAMETOOLONG;
124                 return NULL;
125         }
126
127         /* zone already exist */
128         if ((memzone_lookup_thread_unsafe(name)) != NULL) {
129                 RTE_LOG(DEBUG, EAL, "%s(): memzone <%s> already exists\n",
130                         __func__, name);
131                 rte_errno = EEXIST;
132                 return NULL;
133         }
134
135         /* if alignment is not a power of two */
136         if (align && !rte_is_power_of_2(align)) {
137                 RTE_LOG(ERR, EAL, "%s(): Invalid alignment: %u\n", __func__,
138                                 align);
139                 rte_errno = EINVAL;
140                 return NULL;
141         }
142
143         /* alignment less than cache size is not allowed */
144         if (align < RTE_CACHE_LINE_SIZE)
145                 align = RTE_CACHE_LINE_SIZE;
146
147         /* align length on cache boundary. Check for overflow before doing so */
148         if (len > SIZE_MAX - RTE_CACHE_LINE_MASK) {
149                 rte_errno = EINVAL; /* requested size too big */
150                 return NULL;
151         }
152
153         len += RTE_CACHE_LINE_MASK;
154         len &= ~((size_t) RTE_CACHE_LINE_MASK);
155
156         /* save minimal requested  length */
157         requested_len = RTE_MAX((size_t)RTE_CACHE_LINE_SIZE,  len);
158
159         /* check that boundary condition is valid */
160         if (bound != 0 && (requested_len > bound || !rte_is_power_of_2(bound))) {
161                 rte_errno = EINVAL;
162                 return NULL;
163         }
164
165         if ((socket_id != SOCKET_ID_ANY) &&
166             (socket_id >= RTE_MAX_NUMA_NODES || socket_id < 0)) {
167                 rte_errno = EINVAL;
168                 return NULL;
169         }
170
171         if (!rte_eal_has_hugepages())
172                 socket_id = SOCKET_ID_ANY;
173
174         contig = (flags & RTE_MEMZONE_IOVA_CONTIG) != 0;
175         /* malloc only cares about size flags, remove contig flag from flags */
176         flags &= ~RTE_MEMZONE_IOVA_CONTIG;
177
178         if (len == 0) {
179                 /* len == 0 is only allowed for non-contiguous zones */
180                 if (contig) {
181                         RTE_LOG(DEBUG, EAL, "Reserving zero-length contiguous memzones is not supported\n");
182                         rte_errno = EINVAL;
183                         return NULL;
184                 }
185                 if (bound != 0)
186                         requested_len = bound;
187                 else {
188                         requested_len = find_heap_max_free_elem(&socket_id, align);
189                         if (requested_len == 0) {
190                                 rte_errno = ENOMEM;
191                                 return NULL;
192                         }
193                 }
194         }
195
196         if (socket_id == SOCKET_ID_ANY)
197                 socket = malloc_get_numa_socket();
198         else
199                 socket = socket_id;
200
201         /* allocate memory on heap */
202         void *mz_addr = malloc_heap_alloc(&mcfg->malloc_heaps[socket], NULL,
203                         requested_len, flags, align, bound, contig);
204
205         if ((mz_addr == NULL) && (socket_id == SOCKET_ID_ANY)) {
206                 /* try other heaps */
207                 for (i = 0; i < RTE_MAX_NUMA_NODES; i++) {
208                         if (socket == i)
209                                 continue;
210
211                         mz_addr = malloc_heap_alloc(&mcfg->malloc_heaps[i],
212                                         NULL, requested_len, flags, align,
213                                         bound, contig);
214                         if (mz_addr != NULL)
215                                 break;
216                 }
217         }
218
219         if (mz_addr == NULL) {
220                 rte_errno = ENOMEM;
221                 return NULL;
222         }
223
224         struct malloc_elem *elem = malloc_elem_from_data(mz_addr);
225
226         /* fill the zone in config */
227         mz = get_next_free_memzone();
228
229         if (mz == NULL) {
230                 RTE_LOG(ERR, EAL, "%s(): Cannot find free memzone but there is room "
231                                 "in config!\n", __func__);
232                 malloc_elem_free(elem);
233                 rte_errno = ENOSPC;
234                 return NULL;
235         }
236
237         mcfg->memzone_cnt++;
238         snprintf(mz->name, sizeof(mz->name), "%s", name);
239         mz->iova = rte_malloc_virt2iova(mz_addr);
240         mz->addr = mz_addr;
241         mz->len = (requested_len == 0 ? elem->size : requested_len);
242         mz->hugepage_sz = elem->ms->hugepage_sz;
243         mz->socket_id = elem->ms->socket_id;
244         mz->flags = 0;
245         mz->memseg_id = elem->ms - rte_eal_get_configuration()->mem_config->memseg;
246
247         return mz;
248 }
249
250 static const struct rte_memzone *
251 rte_memzone_reserve_thread_safe(const char *name, size_t len, int socket_id,
252                 unsigned int flags, unsigned int align, unsigned int bound)
253 {
254         struct rte_mem_config *mcfg;
255         const struct rte_memzone *mz = NULL;
256
257         /* get pointer to global configuration */
258         mcfg = rte_eal_get_configuration()->mem_config;
259
260         rte_rwlock_write_lock(&mcfg->mlock);
261
262         mz = memzone_reserve_aligned_thread_unsafe(
263                 name, len, socket_id, flags, align, bound);
264
265         rte_rwlock_write_unlock(&mcfg->mlock);
266
267         return mz;
268 }
269
270 /*
271  * Return a pointer to a correctly filled memzone descriptor (with a
272  * specified alignment and boundary). If the allocation cannot be done,
273  * return NULL.
274  */
275 const struct rte_memzone *
276 rte_memzone_reserve_bounded(const char *name, size_t len, int socket_id,
277                             unsigned flags, unsigned align, unsigned bound)
278 {
279         return rte_memzone_reserve_thread_safe(name, len, socket_id, flags,
280                                                align, bound);
281 }
282
283 /*
284  * Return a pointer to a correctly filled memzone descriptor (with a
285  * specified alignment). If the allocation cannot be done, return NULL.
286  */
287 const struct rte_memzone *
288 rte_memzone_reserve_aligned(const char *name, size_t len, int socket_id,
289                             unsigned flags, unsigned align)
290 {
291         return rte_memzone_reserve_thread_safe(name, len, socket_id, flags,
292                                                align, 0);
293 }
294
295 /*
296  * Return a pointer to a correctly filled memzone descriptor. If the
297  * allocation cannot be done, return NULL.
298  */
299 const struct rte_memzone *
300 rte_memzone_reserve(const char *name, size_t len, int socket_id,
301                     unsigned flags)
302 {
303         return rte_memzone_reserve_thread_safe(name, len, socket_id,
304                                                flags, RTE_CACHE_LINE_SIZE, 0);
305 }
306
307 int
308 rte_memzone_free(const struct rte_memzone *mz)
309 {
310         struct rte_mem_config *mcfg;
311         int ret = 0;
312         void *addr;
313         unsigned idx;
314
315         if (mz == NULL)
316                 return -EINVAL;
317
318         mcfg = rte_eal_get_configuration()->mem_config;
319
320         rte_rwlock_write_lock(&mcfg->mlock);
321
322         idx = ((uintptr_t)mz - (uintptr_t)mcfg->memzone);
323         idx = idx / sizeof(struct rte_memzone);
324
325         addr = mcfg->memzone[idx].addr;
326         if (addr == NULL)
327                 ret = -EINVAL;
328         else if (mcfg->memzone_cnt == 0) {
329                 rte_panic("%s(): memzone address not NULL but memzone_cnt is 0!\n",
330                                 __func__);
331         } else {
332                 memset(&mcfg->memzone[idx], 0, sizeof(mcfg->memzone[idx]));
333                 mcfg->memzone_cnt--;
334         }
335
336         rte_rwlock_write_unlock(&mcfg->mlock);
337
338         rte_free(addr);
339
340         return ret;
341 }
342
343 /*
344  * Lookup for the memzone identified by the given name
345  */
346 const struct rte_memzone *
347 rte_memzone_lookup(const char *name)
348 {
349         struct rte_mem_config *mcfg;
350         const struct rte_memzone *memzone = NULL;
351
352         mcfg = rte_eal_get_configuration()->mem_config;
353
354         rte_rwlock_read_lock(&mcfg->mlock);
355
356         memzone = memzone_lookup_thread_unsafe(name);
357
358         rte_rwlock_read_unlock(&mcfg->mlock);
359
360         return memzone;
361 }
362
363 /* Dump all reserved memory zones on console */
364 void
365 rte_memzone_dump(FILE *f)
366 {
367         struct rte_mem_config *mcfg;
368         unsigned i = 0;
369
370         /* get pointer to global configuration */
371         mcfg = rte_eal_get_configuration()->mem_config;
372
373         rte_rwlock_read_lock(&mcfg->mlock);
374         /* dump all zones */
375         for (i=0; i<RTE_MAX_MEMZONE; i++) {
376                 if (mcfg->memzone[i].addr == NULL)
377                         break;
378                 fprintf(f, "Zone %u: name:<%s>, IO:0x%"PRIx64", len:0x%zx"
379                        ", virt:%p, socket_id:%"PRId32", flags:%"PRIx32"\n", i,
380                        mcfg->memzone[i].name,
381                        mcfg->memzone[i].iova,
382                        mcfg->memzone[i].len,
383                        mcfg->memzone[i].addr,
384                        mcfg->memzone[i].socket_id,
385                        mcfg->memzone[i].flags);
386         }
387         rte_rwlock_read_unlock(&mcfg->mlock);
388 }
389
390 /*
391  * Init the memzone subsystem
392  */
393 int
394 rte_eal_memzone_init(void)
395 {
396         struct rte_mem_config *mcfg;
397         const struct rte_memseg *memseg;
398
399         /* get pointer to global configuration */
400         mcfg = rte_eal_get_configuration()->mem_config;
401
402         /* secondary processes don't need to initialise anything */
403         if (rte_eal_process_type() == RTE_PROC_SECONDARY)
404                 return 0;
405
406         memseg = rte_eal_get_physmem_layout();
407         if (memseg == NULL) {
408                 RTE_LOG(ERR, EAL, "%s(): Cannot get physical layout\n", __func__);
409                 return -1;
410         }
411
412         rte_rwlock_write_lock(&mcfg->mlock);
413
414         /* delete all zones */
415         mcfg->memzone_cnt = 0;
416         memset(mcfg->memzone, 0, sizeof(mcfg->memzone));
417
418         rte_rwlock_write_unlock(&mcfg->mlock);
419
420         return rte_eal_malloc_heap_init();
421 }
422
423 /* Walk all reserved memory zones */
424 void rte_memzone_walk(void (*func)(const struct rte_memzone *, void *),
425                       void *arg)
426 {
427         struct rte_mem_config *mcfg;
428         unsigned i;
429
430         mcfg = rte_eal_get_configuration()->mem_config;
431
432         rte_rwlock_read_lock(&mcfg->mlock);
433         for (i=0; i<RTE_MAX_MEMZONE; i++) {
434                 if (mcfg->memzone[i].addr != NULL)
435                         (*func)(&mcfg->memzone[i], arg);
436         }
437         rte_rwlock_read_unlock(&mcfg->mlock);
438 }