New upstream version 18.05
[deb_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         struct rte_mem_config *mcfg;
32         struct rte_fbarray *arr;
33         const struct rte_memzone *mz;
34         int i = 0;
35
36         /* get pointer to global configuration */
37         mcfg = rte_eal_get_configuration()->mem_config;
38         arr = &mcfg->memzones;
39
40         /*
41          * the algorithm is not optimal (linear), but there are few
42          * zones and this function should be called at init only
43          */
44         i = rte_fbarray_find_next_used(arr, 0);
45         while (i >= 0) {
46                 mz = rte_fbarray_get(arr, i);
47                 if (mz->addr != NULL &&
48                                 !strncmp(name, mz->name, RTE_MEMZONE_NAMESIZE))
49                         return mz;
50                 i = rte_fbarray_find_next_used(arr, i + 1);
51         }
52         return NULL;
53 }
54
55
56 /* This function will return the greatest free block if a heap has been
57  * specified. If no heap has been specified, it will return the heap and
58  * length of the greatest free block available in all heaps */
59 static size_t
60 find_heap_max_free_elem(int *s, unsigned align)
61 {
62         struct rte_mem_config *mcfg;
63         struct rte_malloc_socket_stats stats;
64         int i, socket = *s;
65         size_t len = 0;
66
67         /* get pointer to global configuration */
68         mcfg = rte_eal_get_configuration()->mem_config;
69
70         for (i = 0; i < RTE_MAX_NUMA_NODES; i++) {
71                 if ((socket != SOCKET_ID_ANY) && (socket != i))
72                         continue;
73
74                 malloc_heap_get_stats(&mcfg->malloc_heaps[i], &stats);
75                 if (stats.greatest_free_size > len) {
76                         len = stats.greatest_free_size;
77                         *s = i;
78                 }
79         }
80
81         if (len < MALLOC_ELEM_OVERHEAD + align)
82                 return 0;
83
84         return len - MALLOC_ELEM_OVERHEAD - align;
85 }
86
87 static const struct rte_memzone *
88 memzone_reserve_aligned_thread_unsafe(const char *name, size_t len,
89                 int socket_id, unsigned int flags, unsigned int align,
90                 unsigned int bound)
91 {
92         struct rte_memzone *mz;
93         struct rte_mem_config *mcfg;
94         struct rte_fbarray *arr;
95         size_t requested_len;
96         int mz_idx;
97         bool contig;
98
99         /* get pointer to global configuration */
100         mcfg = rte_eal_get_configuration()->mem_config;
101         arr = &mcfg->memzones;
102
103         /* no more room in config */
104         if (arr->count >= arr->len) {
105                 RTE_LOG(ERR, EAL, "%s(): No more room in config\n", __func__);
106                 rte_errno = ENOSPC;
107                 return NULL;
108         }
109
110         if (strlen(name) > sizeof(mz->name) - 1) {
111                 RTE_LOG(DEBUG, EAL, "%s(): memzone <%s>: name too long\n",
112                         __func__, name);
113                 rte_errno = ENAMETOOLONG;
114                 return NULL;
115         }
116
117         /* zone already exist */
118         if ((memzone_lookup_thread_unsafe(name)) != NULL) {
119                 RTE_LOG(DEBUG, EAL, "%s(): memzone <%s> already exists\n",
120                         __func__, name);
121                 rte_errno = EEXIST;
122                 return NULL;
123         }
124
125         /* if alignment is not a power of two */
126         if (align && !rte_is_power_of_2(align)) {
127                 RTE_LOG(ERR, EAL, "%s(): Invalid alignment: %u\n", __func__,
128                                 align);
129                 rte_errno = EINVAL;
130                 return NULL;
131         }
132
133         /* alignment less than cache size is not allowed */
134         if (align < RTE_CACHE_LINE_SIZE)
135                 align = RTE_CACHE_LINE_SIZE;
136
137         /* align length on cache boundary. Check for overflow before doing so */
138         if (len > SIZE_MAX - RTE_CACHE_LINE_MASK) {
139                 rte_errno = EINVAL; /* requested size too big */
140                 return NULL;
141         }
142
143         len += RTE_CACHE_LINE_MASK;
144         len &= ~((size_t) RTE_CACHE_LINE_MASK);
145
146         /* save minimal requested  length */
147         requested_len = RTE_MAX((size_t)RTE_CACHE_LINE_SIZE,  len);
148
149         /* check that boundary condition is valid */
150         if (bound != 0 && (requested_len > bound || !rte_is_power_of_2(bound))) {
151                 rte_errno = EINVAL;
152                 return NULL;
153         }
154
155         if ((socket_id != SOCKET_ID_ANY) &&
156             (socket_id >= RTE_MAX_NUMA_NODES || socket_id < 0)) {
157                 rte_errno = EINVAL;
158                 return NULL;
159         }
160
161         if (!rte_eal_has_hugepages())
162                 socket_id = SOCKET_ID_ANY;
163
164         contig = (flags & RTE_MEMZONE_IOVA_CONTIG) != 0;
165         /* malloc only cares about size flags, remove contig flag from flags */
166         flags &= ~RTE_MEMZONE_IOVA_CONTIG;
167
168         if (len == 0) {
169                 /* len == 0 is only allowed for non-contiguous zones */
170                 if (contig) {
171                         RTE_LOG(DEBUG, EAL, "Reserving zero-length contiguous memzones is not supported\n");
172                         rte_errno = EINVAL;
173                         return NULL;
174                 }
175                 if (bound != 0)
176                         requested_len = bound;
177                 else {
178                         requested_len = find_heap_max_free_elem(&socket_id, align);
179                         if (requested_len == 0) {
180                                 rte_errno = ENOMEM;
181                                 return NULL;
182                         }
183                 }
184         }
185
186         /* allocate memory on heap */
187         void *mz_addr = malloc_heap_alloc(NULL, requested_len, socket_id, flags,
188                         align, bound, contig);
189         if (mz_addr == NULL) {
190                 rte_errno = ENOMEM;
191                 return NULL;
192         }
193
194         struct malloc_elem *elem = malloc_elem_from_data(mz_addr);
195
196         /* fill the zone in config */
197         mz_idx = rte_fbarray_find_next_free(arr, 0);
198
199         if (mz_idx < 0) {
200                 mz = NULL;
201         } else {
202                 rte_fbarray_set_used(arr, mz_idx);
203                 mz = rte_fbarray_get(arr, mz_idx);
204         }
205
206         if (mz == NULL) {
207                 RTE_LOG(ERR, EAL, "%s(): Cannot find free memzone\n", __func__);
208                 malloc_heap_free(elem);
209                 rte_errno = ENOSPC;
210                 return NULL;
211         }
212
213         snprintf(mz->name, sizeof(mz->name), "%s", name);
214         mz->iova = rte_malloc_virt2iova(mz_addr);
215         mz->addr = mz_addr;
216         mz->len = (requested_len == 0 ?
217                         (elem->size - MALLOC_ELEM_OVERHEAD) : requested_len);
218         mz->hugepage_sz = elem->msl->page_sz;
219         mz->socket_id = elem->msl->socket_id;
220         mz->flags = 0;
221
222         return mz;
223 }
224
225 static const struct rte_memzone *
226 rte_memzone_reserve_thread_safe(const char *name, size_t len, int socket_id,
227                 unsigned int flags, unsigned int align, unsigned int bound)
228 {
229         struct rte_mem_config *mcfg;
230         const struct rte_memzone *mz = NULL;
231
232         /* get pointer to global configuration */
233         mcfg = rte_eal_get_configuration()->mem_config;
234
235         rte_rwlock_write_lock(&mcfg->mlock);
236
237         mz = memzone_reserve_aligned_thread_unsafe(
238                 name, len, socket_id, flags, align, bound);
239
240         rte_rwlock_write_unlock(&mcfg->mlock);
241
242         return mz;
243 }
244
245 /*
246  * Return a pointer to a correctly filled memzone descriptor (with a
247  * specified alignment and boundary). If the allocation cannot be done,
248  * return NULL.
249  */
250 const struct rte_memzone *
251 rte_memzone_reserve_bounded(const char *name, size_t len, int socket_id,
252                             unsigned flags, unsigned align, unsigned bound)
253 {
254         return rte_memzone_reserve_thread_safe(name, len, socket_id, flags,
255                                                align, bound);
256 }
257
258 /*
259  * Return a pointer to a correctly filled memzone descriptor (with a
260  * specified alignment). If the allocation cannot be done, return NULL.
261  */
262 const struct rte_memzone *
263 rte_memzone_reserve_aligned(const char *name, size_t len, int socket_id,
264                             unsigned flags, unsigned align)
265 {
266         return rte_memzone_reserve_thread_safe(name, len, socket_id, flags,
267                                                align, 0);
268 }
269
270 /*
271  * Return a pointer to a correctly filled memzone descriptor. If the
272  * allocation cannot be done, return NULL.
273  */
274 const struct rte_memzone *
275 rte_memzone_reserve(const char *name, size_t len, int socket_id,
276                     unsigned flags)
277 {
278         return rte_memzone_reserve_thread_safe(name, len, socket_id,
279                                                flags, RTE_CACHE_LINE_SIZE, 0);
280 }
281
282 int
283 rte_memzone_free(const struct rte_memzone *mz)
284 {
285         struct rte_mem_config *mcfg;
286         struct rte_fbarray *arr;
287         struct rte_memzone *found_mz;
288         int ret = 0;
289         void *addr = NULL;
290         unsigned idx;
291
292         if (mz == NULL)
293                 return -EINVAL;
294
295         mcfg = rte_eal_get_configuration()->mem_config;
296         arr = &mcfg->memzones;
297
298         rte_rwlock_write_lock(&mcfg->mlock);
299
300         idx = rte_fbarray_find_idx(arr, mz);
301         found_mz = rte_fbarray_get(arr, idx);
302
303         if (found_mz == NULL) {
304                 ret = -EINVAL;
305         } else if (found_mz->addr == NULL) {
306                 RTE_LOG(ERR, EAL, "Memzone is not allocated\n");
307                 ret = -EINVAL;
308         } else {
309                 addr = found_mz->addr;
310                 memset(found_mz, 0, sizeof(*found_mz));
311                 rte_fbarray_set_free(arr, idx);
312         }
313
314         rte_rwlock_write_unlock(&mcfg->mlock);
315
316         if (addr != NULL)
317                 rte_free(addr);
318
319         return ret;
320 }
321
322 /*
323  * Lookup for the memzone identified by the given name
324  */
325 const struct rte_memzone *
326 rte_memzone_lookup(const char *name)
327 {
328         struct rte_mem_config *mcfg;
329         const struct rte_memzone *memzone = NULL;
330
331         mcfg = rte_eal_get_configuration()->mem_config;
332
333         rte_rwlock_read_lock(&mcfg->mlock);
334
335         memzone = memzone_lookup_thread_unsafe(name);
336
337         rte_rwlock_read_unlock(&mcfg->mlock);
338
339         return memzone;
340 }
341
342 static void
343 dump_memzone(const struct rte_memzone *mz, void *arg)
344 {
345         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
346         struct rte_memseg_list *msl = NULL;
347         void *cur_addr, *mz_end;
348         struct rte_memseg *ms;
349         int mz_idx, ms_idx;
350         size_t page_sz;
351         FILE *f = arg;
352
353         mz_idx = rte_fbarray_find_idx(&mcfg->memzones, mz);
354
355         fprintf(f, "Zone %u: name:<%s>, len:0x%zx, virt:%p, "
356                                 "socket_id:%"PRId32", flags:%"PRIx32"\n",
357                         mz_idx,
358                         mz->name,
359                         mz->len,
360                         mz->addr,
361                         mz->socket_id,
362                         mz->flags);
363
364         /* go through each page occupied by this memzone */
365         msl = rte_mem_virt2memseg_list(mz->addr);
366         if (!msl) {
367                 RTE_LOG(DEBUG, EAL, "Skipping bad memzone\n");
368                 return;
369         }
370         page_sz = (size_t)mz->hugepage_sz;
371         cur_addr = RTE_PTR_ALIGN_FLOOR(mz->addr, page_sz);
372         mz_end = RTE_PTR_ADD(cur_addr, mz->len);
373
374         fprintf(f, "physical segments used:\n");
375         ms_idx = RTE_PTR_DIFF(mz->addr, msl->base_va) / page_sz;
376         ms = rte_fbarray_get(&msl->memseg_arr, ms_idx);
377
378         do {
379                 fprintf(f, "  addr: %p iova: 0x%" PRIx64 " "
380                                 "len: 0x%zx "
381                                 "pagesz: 0x%zx\n",
382                         cur_addr, ms->iova, ms->len, page_sz);
383
384                 /* advance VA to next page */
385                 cur_addr = RTE_PTR_ADD(cur_addr, page_sz);
386
387                 /* memzones occupy contiguous segments */
388                 ++ms;
389         } while (cur_addr < mz_end);
390 }
391
392 /* Dump all reserved memory zones on console */
393 void
394 rte_memzone_dump(FILE *f)
395 {
396         rte_memzone_walk(dump_memzone, f);
397 }
398
399 /*
400  * Init the memzone subsystem
401  */
402 int
403 rte_eal_memzone_init(void)
404 {
405         struct rte_mem_config *mcfg;
406
407         /* get pointer to global configuration */
408         mcfg = rte_eal_get_configuration()->mem_config;
409
410         rte_rwlock_write_lock(&mcfg->mlock);
411
412         if (rte_eal_process_type() == RTE_PROC_PRIMARY &&
413                         rte_fbarray_init(&mcfg->memzones, "memzone",
414                         RTE_MAX_MEMZONE, sizeof(struct rte_memzone))) {
415                 RTE_LOG(ERR, EAL, "Cannot allocate memzone list\n");
416                 return -1;
417         } else if (rte_eal_process_type() == RTE_PROC_SECONDARY &&
418                         rte_fbarray_attach(&mcfg->memzones)) {
419                 RTE_LOG(ERR, EAL, "Cannot attach to memzone list\n");
420                 rte_rwlock_write_unlock(&mcfg->mlock);
421                 return -1;
422         }
423
424         rte_rwlock_write_unlock(&mcfg->mlock);
425
426         return 0;
427 }
428
429 /* Walk all reserved memory zones */
430 void rte_memzone_walk(void (*func)(const struct rte_memzone *, void *),
431                       void *arg)
432 {
433         struct rte_mem_config *mcfg;
434         struct rte_fbarray *arr;
435         int i;
436
437         mcfg = rte_eal_get_configuration()->mem_config;
438         arr = &mcfg->memzones;
439
440         rte_rwlock_read_lock(&mcfg->mlock);
441         i = rte_fbarray_find_next_used(arr, 0);
442         while (i >= 0) {
443                 struct rte_memzone *mz = rte_fbarray_get(arr, i);
444                 (*func)(mz, arg);
445                 i = rte_fbarray_find_next_used(arr, i + 1);
446         }
447         rte_rwlock_read_unlock(&mcfg->mlock);
448 }