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