12ddd42d97e826d21a3d871e208db3dc7a75687f
[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 socket, i, 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         if (socket_id == SOCKET_ID_ANY)
187                 socket = malloc_get_numa_socket();
188         else
189                 socket = socket_id;
190
191         /* allocate memory on heap */
192         void *mz_addr = malloc_heap_alloc(&mcfg->malloc_heaps[socket], NULL,
193                         requested_len, flags, align, bound, contig);
194
195         if ((mz_addr == NULL) && (socket_id == SOCKET_ID_ANY)) {
196                 /* try other heaps */
197                 for (i = 0; i < RTE_MAX_NUMA_NODES; i++) {
198                         if (socket == i)
199                                 continue;
200
201                         mz_addr = malloc_heap_alloc(&mcfg->malloc_heaps[i],
202                                         NULL, requested_len, flags, align,
203                                         bound, contig);
204                         if (mz_addr != NULL)
205                                 break;
206                 }
207         }
208
209         if (mz_addr == NULL) {
210                 rte_errno = ENOMEM;
211                 return NULL;
212         }
213
214         struct malloc_elem *elem = malloc_elem_from_data(mz_addr);
215
216         /* fill the zone in config */
217         mz_idx = rte_fbarray_find_next_free(arr, 0);
218
219         if (mz_idx < 0) {
220                 mz = NULL;
221         } else {
222                 rte_fbarray_set_used(arr, mz_idx);
223                 mz = rte_fbarray_get(arr, mz_idx);
224         }
225
226         if (mz == NULL) {
227                 RTE_LOG(ERR, EAL, "%s(): Cannot find free memzone\n", __func__);
228                 malloc_elem_free(elem);
229                 rte_errno = ENOSPC;
230                 return NULL;
231         }
232
233         snprintf(mz->name, sizeof(mz->name), "%s", name);
234         mz->iova = rte_malloc_virt2iova(mz_addr);
235         mz->addr = mz_addr;
236         mz->len = (requested_len == 0 ? elem->size : requested_len);
237         mz->hugepage_sz = elem->msl->page_sz;
238         mz->socket_id = elem->msl->socket_id;
239         mz->flags = 0;
240
241         return mz;
242 }
243
244 static const struct rte_memzone *
245 rte_memzone_reserve_thread_safe(const char *name, size_t len, int socket_id,
246                 unsigned int flags, unsigned int align, unsigned int bound)
247 {
248         struct rte_mem_config *mcfg;
249         const struct rte_memzone *mz = NULL;
250
251         /* get pointer to global configuration */
252         mcfg = rte_eal_get_configuration()->mem_config;
253
254         rte_rwlock_write_lock(&mcfg->mlock);
255
256         mz = memzone_reserve_aligned_thread_unsafe(
257                 name, len, socket_id, flags, align, bound);
258
259         rte_rwlock_write_unlock(&mcfg->mlock);
260
261         return mz;
262 }
263
264 /*
265  * Return a pointer to a correctly filled memzone descriptor (with a
266  * specified alignment and boundary). If the allocation cannot be done,
267  * return NULL.
268  */
269 const struct rte_memzone *
270 rte_memzone_reserve_bounded(const char *name, size_t len, int socket_id,
271                             unsigned flags, unsigned align, unsigned bound)
272 {
273         return rte_memzone_reserve_thread_safe(name, len, socket_id, flags,
274                                                align, bound);
275 }
276
277 /*
278  * Return a pointer to a correctly filled memzone descriptor (with a
279  * specified alignment). If the allocation cannot be done, return NULL.
280  */
281 const struct rte_memzone *
282 rte_memzone_reserve_aligned(const char *name, size_t len, int socket_id,
283                             unsigned flags, unsigned align)
284 {
285         return rte_memzone_reserve_thread_safe(name, len, socket_id, flags,
286                                                align, 0);
287 }
288
289 /*
290  * Return a pointer to a correctly filled memzone descriptor. If the
291  * allocation cannot be done, return NULL.
292  */
293 const struct rte_memzone *
294 rte_memzone_reserve(const char *name, size_t len, int socket_id,
295                     unsigned flags)
296 {
297         return rte_memzone_reserve_thread_safe(name, len, socket_id,
298                                                flags, RTE_CACHE_LINE_SIZE, 0);
299 }
300
301 int
302 rte_memzone_free(const struct rte_memzone *mz)
303 {
304         struct rte_mem_config *mcfg;
305         struct rte_fbarray *arr;
306         struct rte_memzone *found_mz;
307         int ret = 0;
308         void *addr = NULL;
309         unsigned idx;
310
311         if (mz == NULL)
312                 return -EINVAL;
313
314         mcfg = rte_eal_get_configuration()->mem_config;
315         arr = &mcfg->memzones;
316
317         rte_rwlock_write_lock(&mcfg->mlock);
318
319         idx = rte_fbarray_find_idx(arr, mz);
320         found_mz = rte_fbarray_get(arr, idx);
321
322         if (found_mz == NULL) {
323                 ret = -EINVAL;
324         } else if (found_mz->addr == NULL) {
325                 RTE_LOG(ERR, EAL, "Memzone is not allocated\n");
326                 ret = -EINVAL;
327         } else {
328                 addr = found_mz->addr;
329                 memset(found_mz, 0, sizeof(*found_mz));
330                 rte_fbarray_set_free(arr, idx);
331         }
332
333         rte_rwlock_write_unlock(&mcfg->mlock);
334
335         if (addr != NULL)
336                 rte_free(addr);
337
338         return ret;
339 }
340
341 /*
342  * Lookup for the memzone identified by the given name
343  */
344 const struct rte_memzone *
345 rte_memzone_lookup(const char *name)
346 {
347         struct rte_mem_config *mcfg;
348         const struct rte_memzone *memzone = NULL;
349
350         mcfg = rte_eal_get_configuration()->mem_config;
351
352         rte_rwlock_read_lock(&mcfg->mlock);
353
354         memzone = memzone_lookup_thread_unsafe(name);
355
356         rte_rwlock_read_unlock(&mcfg->mlock);
357
358         return memzone;
359 }
360
361 static void
362 dump_memzone(const struct rte_memzone *mz, void *arg)
363 {
364         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
365         struct rte_memseg_list *msl = NULL;
366         void *cur_addr, *mz_end;
367         struct rte_memseg *ms;
368         int mz_idx, ms_idx;
369         size_t page_sz;
370         FILE *f = arg;
371
372         mz_idx = rte_fbarray_find_idx(&mcfg->memzones, mz);
373
374         fprintf(f, "Zone %u: name:<%s>, len:0x%zx, virt:%p, "
375                                 "socket_id:%"PRId32", flags:%"PRIx32"\n",
376                         mz_idx,
377                         mz->name,
378                         mz->len,
379                         mz->addr,
380                         mz->socket_id,
381                         mz->flags);
382
383         /* go through each page occupied by this memzone */
384         msl = rte_mem_virt2memseg_list(mz->addr);
385         if (!msl) {
386                 RTE_LOG(DEBUG, EAL, "Skipping bad memzone\n");
387                 return;
388         }
389         page_sz = (size_t)mz->hugepage_sz;
390         cur_addr = RTE_PTR_ALIGN_FLOOR(mz->addr, page_sz);
391         mz_end = RTE_PTR_ADD(cur_addr, mz->len);
392
393         fprintf(f, "physical segments used:\n");
394         ms_idx = RTE_PTR_DIFF(mz->addr, msl->base_va) / page_sz;
395         ms = rte_fbarray_get(&msl->memseg_arr, ms_idx);
396
397         do {
398                 fprintf(f, "  addr: %p iova: 0x%" PRIx64 " "
399                                 "len: 0x%zx "
400                                 "pagesz: 0x%zx\n",
401                         cur_addr, ms->iova, ms->len, page_sz);
402
403                 /* advance VA to next page */
404                 cur_addr = RTE_PTR_ADD(cur_addr, page_sz);
405
406                 /* memzones occupy contiguous segments */
407                 ++ms;
408         } while (cur_addr < mz_end);
409 }
410
411 /* Dump all reserved memory zones on console */
412 void
413 rte_memzone_dump(FILE *f)
414 {
415         rte_memzone_walk(dump_memzone, f);
416 }
417
418 /*
419  * Init the memzone subsystem
420  */
421 int
422 rte_eal_memzone_init(void)
423 {
424         struct rte_mem_config *mcfg;
425
426         /* get pointer to global configuration */
427         mcfg = rte_eal_get_configuration()->mem_config;
428
429         rte_rwlock_write_lock(&mcfg->mlock);
430
431         if (rte_eal_process_type() == RTE_PROC_PRIMARY &&
432                         rte_fbarray_init(&mcfg->memzones, "memzone",
433                         RTE_MAX_MEMZONE, sizeof(struct rte_memzone))) {
434                 RTE_LOG(ERR, EAL, "Cannot allocate memzone list\n");
435                 return -1;
436         } else if (rte_eal_process_type() == RTE_PROC_SECONDARY &&
437                         rte_fbarray_attach(&mcfg->memzones)) {
438                 RTE_LOG(ERR, EAL, "Cannot attach to memzone list\n");
439                 rte_rwlock_write_unlock(&mcfg->mlock);
440                 return -1;
441         }
442
443         rte_rwlock_write_unlock(&mcfg->mlock);
444
445         return 0;
446 }
447
448 /* Walk all reserved memory zones */
449 void rte_memzone_walk(void (*func)(const struct rte_memzone *, void *),
450                       void *arg)
451 {
452         struct rte_mem_config *mcfg;
453         struct rte_fbarray *arr;
454         int i;
455
456         mcfg = rte_eal_get_configuration()->mem_config;
457         arr = &mcfg->memzones;
458
459         rte_rwlock_read_lock(&mcfg->mlock);
460         i = rte_fbarray_find_next_used(arr, 0);
461         while (i >= 0) {
462                 struct rte_memzone *mz = rte_fbarray_get(arr, i);
463                 (*func)(mz, arg);
464                 i = rte_fbarray_find_next_used(arr, i + 1);
465         }
466         rte_rwlock_read_unlock(&mcfg->mlock);
467 }