1f5f7534652c438a04d49f23eb06e464a83bd97f
[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->msl->page_sz;
243         mz->socket_id = elem->msl->socket_id;
244         mz->flags = 0;
245
246         return mz;
247 }
248
249 static const struct rte_memzone *
250 rte_memzone_reserve_thread_safe(const char *name, size_t len, int socket_id,
251                 unsigned int flags, unsigned int align, unsigned int bound)
252 {
253         struct rte_mem_config *mcfg;
254         const struct rte_memzone *mz = NULL;
255
256         /* get pointer to global configuration */
257         mcfg = rte_eal_get_configuration()->mem_config;
258
259         rte_rwlock_write_lock(&mcfg->mlock);
260
261         mz = memzone_reserve_aligned_thread_unsafe(
262                 name, len, socket_id, flags, align, bound);
263
264         rte_rwlock_write_unlock(&mcfg->mlock);
265
266         return mz;
267 }
268
269 /*
270  * Return a pointer to a correctly filled memzone descriptor (with a
271  * specified alignment and boundary). If the allocation cannot be done,
272  * return NULL.
273  */
274 const struct rte_memzone *
275 rte_memzone_reserve_bounded(const char *name, size_t len, int socket_id,
276                             unsigned flags, unsigned align, unsigned bound)
277 {
278         return rte_memzone_reserve_thread_safe(name, len, socket_id, flags,
279                                                align, bound);
280 }
281
282 /*
283  * Return a pointer to a correctly filled memzone descriptor (with a
284  * specified alignment). If the allocation cannot be done, return NULL.
285  */
286 const struct rte_memzone *
287 rte_memzone_reserve_aligned(const char *name, size_t len, int socket_id,
288                             unsigned flags, unsigned align)
289 {
290         return rte_memzone_reserve_thread_safe(name, len, socket_id, flags,
291                                                align, 0);
292 }
293
294 /*
295  * Return a pointer to a correctly filled memzone descriptor. If the
296  * allocation cannot be done, return NULL.
297  */
298 const struct rte_memzone *
299 rte_memzone_reserve(const char *name, size_t len, int socket_id,
300                     unsigned flags)
301 {
302         return rte_memzone_reserve_thread_safe(name, len, socket_id,
303                                                flags, RTE_CACHE_LINE_SIZE, 0);
304 }
305
306 int
307 rte_memzone_free(const struct rte_memzone *mz)
308 {
309         struct rte_mem_config *mcfg;
310         int ret = 0;
311         void *addr;
312         unsigned idx;
313
314         if (mz == NULL)
315                 return -EINVAL;
316
317         mcfg = rte_eal_get_configuration()->mem_config;
318
319         rte_rwlock_write_lock(&mcfg->mlock);
320
321         idx = ((uintptr_t)mz - (uintptr_t)mcfg->memzone);
322         idx = idx / sizeof(struct rte_memzone);
323
324         addr = mcfg->memzone[idx].addr;
325         if (addr == NULL)
326                 ret = -EINVAL;
327         else if (mcfg->memzone_cnt == 0) {
328                 rte_panic("%s(): memzone address not NULL but memzone_cnt is 0!\n",
329                                 __func__);
330         } else {
331                 memset(&mcfg->memzone[idx], 0, sizeof(mcfg->memzone[idx]));
332                 mcfg->memzone_cnt--;
333         }
334
335         rte_rwlock_write_unlock(&mcfg->mlock);
336
337         rte_free(addr);
338
339         return ret;
340 }
341
342 /*
343  * Lookup for the memzone identified by the given name
344  */
345 const struct rte_memzone *
346 rte_memzone_lookup(const char *name)
347 {
348         struct rte_mem_config *mcfg;
349         const struct rte_memzone *memzone = NULL;
350
351         mcfg = rte_eal_get_configuration()->mem_config;
352
353         rte_rwlock_read_lock(&mcfg->mlock);
354
355         memzone = memzone_lookup_thread_unsafe(name);
356
357         rte_rwlock_read_unlock(&mcfg->mlock);
358
359         return memzone;
360 }
361
362 static void
363 dump_memzone(const struct rte_memzone *mz, void *arg)
364 {
365         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
366         struct rte_memseg_list *msl = NULL;
367         void *cur_addr, *mz_end;
368         struct rte_memseg *ms;
369         int mz_idx, ms_idx;
370         size_t page_sz;
371         FILE *f = arg;
372
373         mz_idx = mz - mcfg->memzone;
374
375         fprintf(f, "Zone %u: name:<%s>, len:0x%zx, virt:%p, "
376                                 "socket_id:%"PRId32", flags:%"PRIx32"\n",
377                         mz_idx,
378                         mz->name,
379                         mz->len,
380                         mz->addr,
381                         mz->socket_id,
382                         mz->flags);
383
384         /* go through each page occupied by this memzone */
385         msl = rte_mem_virt2memseg_list(mz->addr);
386         if (!msl) {
387                 RTE_LOG(DEBUG, EAL, "Skipping bad memzone\n");
388                 return;
389         }
390         page_sz = (size_t)mz->hugepage_sz;
391         cur_addr = RTE_PTR_ALIGN_FLOOR(mz->addr, page_sz);
392         mz_end = RTE_PTR_ADD(cur_addr, mz->len);
393
394         fprintf(f, "physical segments used:\n");
395         ms_idx = RTE_PTR_DIFF(mz->addr, msl->base_va) / page_sz;
396         ms = rte_fbarray_get(&msl->memseg_arr, ms_idx);
397
398         do {
399                 fprintf(f, "  addr: %p iova: 0x%" PRIx64 " "
400                                 "len: 0x%zx "
401                                 "pagesz: 0x%zx\n",
402                         cur_addr, ms->iova, ms->len, page_sz);
403
404                 /* advance VA to next page */
405                 cur_addr = RTE_PTR_ADD(cur_addr, page_sz);
406
407                 /* memzones occupy contiguous segments */
408                 ++ms;
409         } while (cur_addr < mz_end);
410 }
411
412 /* Dump all reserved memory zones on console */
413 void
414 rte_memzone_dump(FILE *f)
415 {
416         rte_memzone_walk(dump_memzone, f);
417 }
418
419 /*
420  * Init the memzone subsystem
421  */
422 int
423 rte_eal_memzone_init(void)
424 {
425         struct rte_mem_config *mcfg;
426
427         /* get pointer to global configuration */
428         mcfg = rte_eal_get_configuration()->mem_config;
429
430         /* secondary processes don't need to initialise anything */
431         if (rte_eal_process_type() == RTE_PROC_SECONDARY)
432                 return 0;
433
434         rte_rwlock_write_lock(&mcfg->mlock);
435
436         /* delete all zones */
437         mcfg->memzone_cnt = 0;
438         memset(mcfg->memzone, 0, sizeof(mcfg->memzone));
439
440         rte_rwlock_write_unlock(&mcfg->mlock);
441
442         return rte_eal_malloc_heap_init();
443 }
444
445 /* Walk all reserved memory zones */
446 void rte_memzone_walk(void (*func)(const struct rte_memzone *, void *),
447                       void *arg)
448 {
449         struct rte_mem_config *mcfg;
450         unsigned i;
451
452         mcfg = rte_eal_get_configuration()->mem_config;
453
454         rte_rwlock_read_lock(&mcfg->mlock);
455         for (i=0; i<RTE_MAX_MEMZONE; i++) {
456                 if (mcfg->memzone[i].addr != NULL)
457                         (*func)(&mcfg->memzone[i], arg);
458         }
459         rte_rwlock_read_unlock(&mcfg->mlock);
460 }