31bf6d8d665f0223606c9ec8e212dec437a1493f
[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 /* This function will return the greatest free block if a heap has been
81  * specified. If no heap has been specified, it will return the heap and
82  * length of the greatest free block available in all heaps */
83 static size_t
84 find_heap_max_free_elem(int *s, unsigned align)
85 {
86         struct rte_mem_config *mcfg;
87         struct rte_malloc_socket_stats stats;
88         int i, socket = *s;
89         size_t len = 0;
90
91         /* get pointer to global configuration */
92         mcfg = rte_eal_get_configuration()->mem_config;
93
94         for (i = 0; i < RTE_MAX_NUMA_NODES; i++) {
95                 if ((socket != SOCKET_ID_ANY) && (socket != i))
96                         continue;
97
98                 malloc_heap_get_stats(&mcfg->malloc_heaps[i], &stats);
99                 if (stats.greatest_free_size > len) {
100                         len = stats.greatest_free_size;
101                         *s = i;
102                 }
103         }
104
105         return (len - MALLOC_ELEM_OVERHEAD - align);
106 }
107
108 static const struct rte_memzone *
109 memzone_reserve_aligned_thread_unsafe(const char *name, size_t len,
110                 int socket_id, unsigned flags, unsigned align, unsigned bound)
111 {
112         struct rte_mem_config *mcfg;
113         size_t requested_len;
114         int socket, i;
115
116         /* get pointer to global configuration */
117         mcfg = rte_eal_get_configuration()->mem_config;
118
119         /* no more room in config */
120         if (mcfg->memzone_idx >= RTE_MAX_MEMZONE) {
121                 RTE_LOG(ERR, EAL, "%s(): No more room in config\n", __func__);
122                 rte_errno = ENOSPC;
123                 return NULL;
124         }
125
126         /* zone already exist */
127         if ((memzone_lookup_thread_unsafe(name)) != NULL) {
128                 RTE_LOG(DEBUG, EAL, "%s(): memzone <%s> already exists\n",
129                         __func__, name);
130                 rte_errno = EEXIST;
131                 return NULL;
132         }
133
134         /* if alignment is not a power of two */
135         if (align && !rte_is_power_of_2(align)) {
136                 RTE_LOG(ERR, EAL, "%s(): Invalid alignment: %u\n", __func__,
137                                 align);
138                 rte_errno = EINVAL;
139                 return NULL;
140         }
141
142         /* alignment less than cache size is not allowed */
143         if (align < RTE_CACHE_LINE_SIZE)
144                 align = RTE_CACHE_LINE_SIZE;
145
146         /* align length on cache boundary. Check for overflow before doing so */
147         if (len > SIZE_MAX - RTE_CACHE_LINE_MASK) {
148                 rte_errno = EINVAL; /* requested size too big */
149                 return NULL;
150         }
151
152         len += RTE_CACHE_LINE_MASK;
153         len &= ~((size_t) RTE_CACHE_LINE_MASK);
154
155         /* save minimal requested  length */
156         requested_len = RTE_MAX((size_t)RTE_CACHE_LINE_SIZE,  len);
157
158         /* check that boundary condition is valid */
159         if (bound != 0 && (requested_len > bound || !rte_is_power_of_2(bound))) {
160                 rte_errno = EINVAL;
161                 return NULL;
162         }
163
164         if ((socket_id != SOCKET_ID_ANY) && (socket_id >= RTE_MAX_NUMA_NODES)) {
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         }
178
179         if (socket_id == SOCKET_ID_ANY)
180                 socket = malloc_get_numa_socket();
181         else
182                 socket = socket_id;
183
184         /* allocate memory on heap */
185         void *mz_addr = malloc_heap_alloc(&mcfg->malloc_heaps[socket], NULL,
186                         requested_len, flags, align, bound);
187
188         if ((mz_addr == NULL) && (socket_id == SOCKET_ID_ANY)) {
189                 /* try other heaps */
190                 for (i = 0; i < RTE_MAX_NUMA_NODES; i++) {
191                         if (socket == i)
192                                 continue;
193
194                         mz_addr = malloc_heap_alloc(&mcfg->malloc_heaps[i],
195                                         NULL, requested_len, flags, align, bound);
196                         if (mz_addr != NULL)
197                                 break;
198                 }
199         }
200
201         if (mz_addr == NULL) {
202                 rte_errno = ENOMEM;
203                 return NULL;
204         }
205
206         const struct malloc_elem *elem = malloc_elem_from_data(mz_addr);
207
208         /* fill the zone in config */
209         struct rte_memzone *mz = &mcfg->memzone[mcfg->memzone_idx++];
210         snprintf(mz->name, sizeof(mz->name), "%s", name);
211         mz->phys_addr = rte_malloc_virt2phy(mz_addr);
212         mz->addr = mz_addr;
213         mz->len = (requested_len == 0 ? elem->size : requested_len);
214         mz->hugepage_sz = elem->ms->hugepage_sz;
215         mz->socket_id = elem->ms->socket_id;
216         mz->flags = 0;
217         mz->memseg_id = elem->ms - rte_eal_get_configuration()->mem_config->memseg;
218
219         return mz;
220 }
221
222 static const struct rte_memzone *
223 rte_memzone_reserve_thread_safe(const char *name, size_t len,
224                                 int socket_id, unsigned flags, unsigned align,
225                                 unsigned bound)
226 {
227         struct rte_mem_config *mcfg;
228         const struct rte_memzone *mz = NULL;
229
230         /* get pointer to global configuration */
231         mcfg = rte_eal_get_configuration()->mem_config;
232
233         rte_rwlock_write_lock(&mcfg->mlock);
234
235         mz = memzone_reserve_aligned_thread_unsafe(
236                 name, len, socket_id, flags, align, bound);
237
238         rte_rwlock_write_unlock(&mcfg->mlock);
239
240         return mz;
241 }
242
243 /*
244  * Return a pointer to a correctly filled memzone descriptor (with a
245  * specified alignment and boundary). If the allocation cannot be done,
246  * return NULL.
247  */
248 const struct rte_memzone *
249 rte_memzone_reserve_bounded(const char *name, size_t len, int socket_id,
250                             unsigned flags, unsigned align, unsigned bound)
251 {
252         return rte_memzone_reserve_thread_safe(name, len, socket_id, flags,
253                                                align, bound);
254 }
255
256 /*
257  * Return a pointer to a correctly filled memzone descriptor (with a
258  * specified alignment). If the allocation cannot be done, return NULL.
259  */
260 const struct rte_memzone *
261 rte_memzone_reserve_aligned(const char *name, size_t len, int socket_id,
262                             unsigned flags, unsigned align)
263 {
264         return rte_memzone_reserve_thread_safe(name, len, socket_id, flags,
265                                                align, 0);
266 }
267
268 /*
269  * Return a pointer to a correctly filled memzone descriptor. If the
270  * allocation cannot be done, return NULL.
271  */
272 const struct rte_memzone *
273 rte_memzone_reserve(const char *name, size_t len, int socket_id,
274                     unsigned flags)
275 {
276         return rte_memzone_reserve_thread_safe(name, len, socket_id,
277                                                flags, RTE_CACHE_LINE_SIZE, 0);
278 }
279
280 /*
281  * Lookup for the memzone identified by the given name
282  */
283 const struct rte_memzone *
284 rte_memzone_lookup(const char *name)
285 {
286         struct rte_mem_config *mcfg;
287         const struct rte_memzone *memzone = NULL;
288
289         mcfg = rte_eal_get_configuration()->mem_config;
290
291         rte_rwlock_read_lock(&mcfg->mlock);
292
293         memzone = memzone_lookup_thread_unsafe(name);
294
295         rte_rwlock_read_unlock(&mcfg->mlock);
296
297         return memzone;
298 }
299
300 /* Dump all reserved memory zones on console */
301 void
302 rte_memzone_dump(FILE *f)
303 {
304         struct rte_mem_config *mcfg;
305         unsigned i = 0;
306
307         /* get pointer to global configuration */
308         mcfg = rte_eal_get_configuration()->mem_config;
309
310         rte_rwlock_read_lock(&mcfg->mlock);
311         /* dump all zones */
312         for (i=0; i<RTE_MAX_MEMZONE; i++) {
313                 if (mcfg->memzone[i].addr == NULL)
314                         break;
315                 fprintf(f, "Zone %u: name:<%s>, phys:0x%"PRIx64", len:0x%zx"
316                        ", virt:%p, socket_id:%"PRId32", flags:%"PRIx32"\n", i,
317                        mcfg->memzone[i].name,
318                        mcfg->memzone[i].phys_addr,
319                        mcfg->memzone[i].len,
320                        mcfg->memzone[i].addr,
321                        mcfg->memzone[i].socket_id,
322                        mcfg->memzone[i].flags);
323         }
324         rte_rwlock_read_unlock(&mcfg->mlock);
325 }
326
327 /*
328  * Init the memzone subsystem
329  */
330 int
331 rte_eal_memzone_init(void)
332 {
333         struct rte_mem_config *mcfg;
334         const struct rte_memseg *memseg;
335
336         /* get pointer to global configuration */
337         mcfg = rte_eal_get_configuration()->mem_config;
338
339         /* secondary processes don't need to initialise anything */
340         if (rte_eal_process_type() == RTE_PROC_SECONDARY)
341                 return 0;
342
343         memseg = rte_eal_get_physmem_layout();
344         if (memseg == NULL) {
345                 RTE_LOG(ERR, EAL, "%s(): Cannot get physical layout\n", __func__);
346                 return -1;
347         }
348
349         rte_rwlock_write_lock(&mcfg->mlock);
350
351         /* delete all zones */
352         mcfg->memzone_idx = 0;
353         memset(mcfg->memzone, 0, sizeof(mcfg->memzone));
354
355         rte_rwlock_write_unlock(&mcfg->mlock);
356
357         return rte_eal_malloc_heap_init();
358 }
359
360 /* Walk all reserved memory zones */
361 void rte_memzone_walk(void (*func)(const struct rte_memzone *, void *),
362                       void *arg)
363 {
364         struct rte_mem_config *mcfg;
365         unsigned i;
366
367         mcfg = rte_eal_get_configuration()->mem_config;
368
369         rte_rwlock_read_lock(&mcfg->mlock);
370         for (i=0; i<RTE_MAX_MEMZONE; i++) {
371                 if (mcfg->memzone[i].addr != NULL)
372                         (*func)(&mcfg->memzone[i], arg);
373         }
374         rte_rwlock_read_unlock(&mcfg->mlock);
375 }