New upstream version 17.11.1
[deb_dpdk.git] / lib / librte_eal / common / eal_common_memzone.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <stdlib.h>
35 #include <stdio.h>
36 #include <stdint.h>
37 #include <stdarg.h>
38 #include <inttypes.h>
39 #include <string.h>
40 #include <errno.h>
41 #include <sys/queue.h>
42
43 #include <rte_log.h>
44 #include <rte_memory.h>
45 #include <rte_memzone.h>
46 #include <rte_eal.h>
47 #include <rte_eal_memconfig.h>
48 #include <rte_per_lcore.h>
49 #include <rte_errno.h>
50 #include <rte_string_fns.h>
51 #include <rte_common.h>
52
53 #include "malloc_heap.h"
54 #include "malloc_elem.h"
55 #include "eal_private.h"
56
57 static inline const struct rte_memzone *
58 memzone_lookup_thread_unsafe(const char *name)
59 {
60         const struct rte_mem_config *mcfg;
61         const struct rte_memzone *mz;
62         unsigned i = 0;
63
64         /* get pointer to global configuration */
65         mcfg = rte_eal_get_configuration()->mem_config;
66
67         /*
68          * the algorithm is not optimal (linear), but there are few
69          * zones and this function should be called at init only
70          */
71         for (i = 0; i < RTE_MAX_MEMZONE; i++) {
72                 mz = &mcfg->memzone[i];
73                 if (mz->addr != NULL && !strncmp(name, mz->name, RTE_MEMZONE_NAMESIZE))
74                         return &mcfg->memzone[i];
75         }
76
77         return NULL;
78 }
79
80 static inline struct rte_memzone *
81 get_next_free_memzone(void)
82 {
83         struct rte_mem_config *mcfg;
84         unsigned i = 0;
85
86         /* get pointer to global configuration */
87         mcfg = rte_eal_get_configuration()->mem_config;
88
89         for (i = 0; i < RTE_MAX_MEMZONE; i++) {
90                 if (mcfg->memzone[i].addr == NULL)
91                         return &mcfg->memzone[i];
92         }
93
94         return NULL;
95 }
96
97 /* This function will return the greatest free block if a heap has been
98  * specified. If no heap has been specified, it will return the heap and
99  * length of the greatest free block available in all heaps */
100 static size_t
101 find_heap_max_free_elem(int *s, unsigned align)
102 {
103         struct rte_mem_config *mcfg;
104         struct rte_malloc_socket_stats stats;
105         int i, socket = *s;
106         size_t len = 0;
107
108         /* get pointer to global configuration */
109         mcfg = rte_eal_get_configuration()->mem_config;
110
111         for (i = 0; i < RTE_MAX_NUMA_NODES; i++) {
112                 if ((socket != SOCKET_ID_ANY) && (socket != i))
113                         continue;
114
115                 malloc_heap_get_stats(&mcfg->malloc_heaps[i], &stats);
116                 if (stats.greatest_free_size > len) {
117                         len = stats.greatest_free_size;
118                         *s = i;
119                 }
120         }
121
122         if (len < MALLOC_ELEM_OVERHEAD + align)
123                 return 0;
124
125         return len - MALLOC_ELEM_OVERHEAD - align;
126 }
127
128 static const struct rte_memzone *
129 memzone_reserve_aligned_thread_unsafe(const char *name, size_t len,
130                 int socket_id, unsigned flags, unsigned align, unsigned bound)
131 {
132         struct rte_memzone *mz;
133         struct rte_mem_config *mcfg;
134         size_t requested_len;
135         int socket, i;
136
137         /* get pointer to global configuration */
138         mcfg = rte_eal_get_configuration()->mem_config;
139
140         /* no more room in config */
141         if (mcfg->memzone_cnt >= RTE_MAX_MEMZONE) {
142                 RTE_LOG(ERR, EAL, "%s(): No more room in config\n", __func__);
143                 rte_errno = ENOSPC;
144                 return NULL;
145         }
146
147         if (strlen(name) > sizeof(mz->name) - 1) {
148                 RTE_LOG(DEBUG, EAL, "%s(): memzone <%s>: name too long\n",
149                         __func__, name);
150                 rte_errno = ENAMETOOLONG;
151                 return NULL;
152         }
153
154         /* zone already exist */
155         if ((memzone_lookup_thread_unsafe(name)) != NULL) {
156                 RTE_LOG(DEBUG, EAL, "%s(): memzone <%s> already exists\n",
157                         __func__, name);
158                 rte_errno = EEXIST;
159                 return NULL;
160         }
161
162         /* if alignment is not a power of two */
163         if (align && !rte_is_power_of_2(align)) {
164                 RTE_LOG(ERR, EAL, "%s(): Invalid alignment: %u\n", __func__,
165                                 align);
166                 rte_errno = EINVAL;
167                 return NULL;
168         }
169
170         /* alignment less than cache size is not allowed */
171         if (align < RTE_CACHE_LINE_SIZE)
172                 align = RTE_CACHE_LINE_SIZE;
173
174         /* align length on cache boundary. Check for overflow before doing so */
175         if (len > SIZE_MAX - RTE_CACHE_LINE_MASK) {
176                 rte_errno = EINVAL; /* requested size too big */
177                 return NULL;
178         }
179
180         len += RTE_CACHE_LINE_MASK;
181         len &= ~((size_t) RTE_CACHE_LINE_MASK);
182
183         /* save minimal requested  length */
184         requested_len = RTE_MAX((size_t)RTE_CACHE_LINE_SIZE,  len);
185
186         /* check that boundary condition is valid */
187         if (bound != 0 && (requested_len > bound || !rte_is_power_of_2(bound))) {
188                 rte_errno = EINVAL;
189                 return NULL;
190         }
191
192         if ((socket_id != SOCKET_ID_ANY) &&
193             (socket_id >= RTE_MAX_NUMA_NODES || socket_id < 0)) {
194                 rte_errno = EINVAL;
195                 return NULL;
196         }
197
198         if (!rte_eal_has_hugepages())
199                 socket_id = SOCKET_ID_ANY;
200
201         if (len == 0) {
202                 if (bound != 0)
203                         requested_len = bound;
204                 else {
205                         requested_len = find_heap_max_free_elem(&socket_id, align);
206                         if (requested_len == 0) {
207                                 rte_errno = ENOMEM;
208                                 return NULL;
209                         }
210                 }
211         }
212
213         if (socket_id == SOCKET_ID_ANY)
214                 socket = malloc_get_numa_socket();
215         else
216                 socket = socket_id;
217
218         /* allocate memory on heap */
219         void *mz_addr = malloc_heap_alloc(&mcfg->malloc_heaps[socket], NULL,
220                         requested_len, flags, align, bound);
221
222         if ((mz_addr == NULL) && (socket_id == SOCKET_ID_ANY)) {
223                 /* try other heaps */
224                 for (i = 0; i < RTE_MAX_NUMA_NODES; i++) {
225                         if (socket == i)
226                                 continue;
227
228                         mz_addr = malloc_heap_alloc(&mcfg->malloc_heaps[i],
229                                         NULL, requested_len, flags, align, bound);
230                         if (mz_addr != NULL)
231                                 break;
232                 }
233         }
234
235         if (mz_addr == NULL) {
236                 rte_errno = ENOMEM;
237                 return NULL;
238         }
239
240         struct malloc_elem *elem = malloc_elem_from_data(mz_addr);
241
242         /* fill the zone in config */
243         mz = get_next_free_memzone();
244
245         if (mz == NULL) {
246                 RTE_LOG(ERR, EAL, "%s(): Cannot find free memzone but there is room "
247                                 "in config!\n", __func__);
248                 malloc_elem_free(elem);
249                 rte_errno = ENOSPC;
250                 return NULL;
251         }
252
253         mcfg->memzone_cnt++;
254         snprintf(mz->name, sizeof(mz->name), "%s", name);
255         mz->iova = rte_malloc_virt2iova(mz_addr);
256         mz->addr = mz_addr;
257         mz->len = (requested_len == 0 ? elem->size : requested_len);
258         mz->hugepage_sz = elem->ms->hugepage_sz;
259         mz->socket_id = elem->ms->socket_id;
260         mz->flags = 0;
261         mz->memseg_id = elem->ms - rte_eal_get_configuration()->mem_config->memseg;
262
263         return mz;
264 }
265
266 static const struct rte_memzone *
267 rte_memzone_reserve_thread_safe(const char *name, size_t len,
268                                 int socket_id, unsigned flags, unsigned align,
269                                 unsigned bound)
270 {
271         struct rte_mem_config *mcfg;
272         const struct rte_memzone *mz = NULL;
273
274         /* get pointer to global configuration */
275         mcfg = rte_eal_get_configuration()->mem_config;
276
277         rte_rwlock_write_lock(&mcfg->mlock);
278
279         mz = memzone_reserve_aligned_thread_unsafe(
280                 name, len, socket_id, flags, align, bound);
281
282         rte_rwlock_write_unlock(&mcfg->mlock);
283
284         return mz;
285 }
286
287 /*
288  * Return a pointer to a correctly filled memzone descriptor (with a
289  * specified alignment and boundary). If the allocation cannot be done,
290  * return NULL.
291  */
292 const struct rte_memzone *
293 rte_memzone_reserve_bounded(const char *name, size_t len, int socket_id,
294                             unsigned flags, unsigned align, unsigned bound)
295 {
296         return rte_memzone_reserve_thread_safe(name, len, socket_id, flags,
297                                                align, bound);
298 }
299
300 /*
301  * Return a pointer to a correctly filled memzone descriptor (with a
302  * specified alignment). If the allocation cannot be done, return NULL.
303  */
304 const struct rte_memzone *
305 rte_memzone_reserve_aligned(const char *name, size_t len, int socket_id,
306                             unsigned flags, unsigned align)
307 {
308         return rte_memzone_reserve_thread_safe(name, len, socket_id, flags,
309                                                align, 0);
310 }
311
312 /*
313  * Return a pointer to a correctly filled memzone descriptor. If the
314  * allocation cannot be done, return NULL.
315  */
316 const struct rte_memzone *
317 rte_memzone_reserve(const char *name, size_t len, int socket_id,
318                     unsigned flags)
319 {
320         return rte_memzone_reserve_thread_safe(name, len, socket_id,
321                                                flags, RTE_CACHE_LINE_SIZE, 0);
322 }
323
324 int
325 rte_memzone_free(const struct rte_memzone *mz)
326 {
327         struct rte_mem_config *mcfg;
328         int ret = 0;
329         void *addr;
330         unsigned idx;
331
332         if (mz == NULL)
333                 return -EINVAL;
334
335         mcfg = rte_eal_get_configuration()->mem_config;
336
337         rte_rwlock_write_lock(&mcfg->mlock);
338
339         idx = ((uintptr_t)mz - (uintptr_t)mcfg->memzone);
340         idx = idx / sizeof(struct rte_memzone);
341
342         addr = mcfg->memzone[idx].addr;
343         if (addr == NULL)
344                 ret = -EINVAL;
345         else if (mcfg->memzone_cnt == 0) {
346                 rte_panic("%s(): memzone address not NULL but memzone_cnt is 0!\n",
347                                 __func__);
348         } else {
349                 memset(&mcfg->memzone[idx], 0, sizeof(mcfg->memzone[idx]));
350                 mcfg->memzone_cnt--;
351         }
352
353         rte_rwlock_write_unlock(&mcfg->mlock);
354
355         rte_free(addr);
356
357         return ret;
358 }
359
360 /*
361  * Lookup for the memzone identified by the given name
362  */
363 const struct rte_memzone *
364 rte_memzone_lookup(const char *name)
365 {
366         struct rte_mem_config *mcfg;
367         const struct rte_memzone *memzone = NULL;
368
369         mcfg = rte_eal_get_configuration()->mem_config;
370
371         rte_rwlock_read_lock(&mcfg->mlock);
372
373         memzone = memzone_lookup_thread_unsafe(name);
374
375         rte_rwlock_read_unlock(&mcfg->mlock);
376
377         return memzone;
378 }
379
380 /* Dump all reserved memory zones on console */
381 void
382 rte_memzone_dump(FILE *f)
383 {
384         struct rte_mem_config *mcfg;
385         unsigned i = 0;
386
387         /* get pointer to global configuration */
388         mcfg = rte_eal_get_configuration()->mem_config;
389
390         rte_rwlock_read_lock(&mcfg->mlock);
391         /* dump all zones */
392         for (i=0; i<RTE_MAX_MEMZONE; i++) {
393                 if (mcfg->memzone[i].addr == NULL)
394                         break;
395                 fprintf(f, "Zone %u: name:<%s>, IO:0x%"PRIx64", len:0x%zx"
396                        ", virt:%p, socket_id:%"PRId32", flags:%"PRIx32"\n", i,
397                        mcfg->memzone[i].name,
398                        mcfg->memzone[i].iova,
399                        mcfg->memzone[i].len,
400                        mcfg->memzone[i].addr,
401                        mcfg->memzone[i].socket_id,
402                        mcfg->memzone[i].flags);
403         }
404         rte_rwlock_read_unlock(&mcfg->mlock);
405 }
406
407 /*
408  * Init the memzone subsystem
409  */
410 int
411 rte_eal_memzone_init(void)
412 {
413         struct rte_mem_config *mcfg;
414         const struct rte_memseg *memseg;
415
416         /* get pointer to global configuration */
417         mcfg = rte_eal_get_configuration()->mem_config;
418
419         /* secondary processes don't need to initialise anything */
420         if (rte_eal_process_type() == RTE_PROC_SECONDARY)
421                 return 0;
422
423         memseg = rte_eal_get_physmem_layout();
424         if (memseg == NULL) {
425                 RTE_LOG(ERR, EAL, "%s(): Cannot get physical layout\n", __func__);
426                 return -1;
427         }
428
429         rte_rwlock_write_lock(&mcfg->mlock);
430
431         /* delete all zones */
432         mcfg->memzone_cnt = 0;
433         memset(mcfg->memzone, 0, sizeof(mcfg->memzone));
434
435         rte_rwlock_write_unlock(&mcfg->mlock);
436
437         return rte_eal_malloc_heap_init();
438 }
439
440 /* Walk all reserved memory zones */
441 void rte_memzone_walk(void (*func)(const struct rte_memzone *, void *),
442                       void *arg)
443 {
444         struct rte_mem_config *mcfg;
445         unsigned i;
446
447         mcfg = rte_eal_get_configuration()->mem_config;
448
449         rte_rwlock_read_lock(&mcfg->mlock);
450         for (i=0; i<RTE_MAX_MEMZONE; i++) {
451                 if (mcfg->memzone[i].addr != NULL)
452                         (*func)(&mcfg->memzone[i], arg);
453         }
454         rte_rwlock_read_unlock(&mcfg->mlock);
455 }