mem: introduce memzone freeing
[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         return (len - MALLOC_ELEM_OVERHEAD - align);
123 }
124
125 static const struct rte_memzone *
126 memzone_reserve_aligned_thread_unsafe(const char *name, size_t len,
127                 int socket_id, unsigned flags, unsigned align, unsigned bound)
128 {
129         struct rte_mem_config *mcfg;
130         size_t requested_len;
131         int socket, i;
132
133         /* get pointer to global configuration */
134         mcfg = rte_eal_get_configuration()->mem_config;
135
136         /* no more room in config */
137         if (mcfg->memzone_cnt >= RTE_MAX_MEMZONE) {
138                 RTE_LOG(ERR, EAL, "%s(): No more room in config\n", __func__);
139                 rte_errno = ENOSPC;
140                 return NULL;
141         }
142
143         /* zone already exist */
144         if ((memzone_lookup_thread_unsafe(name)) != NULL) {
145                 RTE_LOG(DEBUG, EAL, "%s(): memzone <%s> already exists\n",
146                         __func__, name);
147                 rte_errno = EEXIST;
148                 return NULL;
149         }
150
151         /* if alignment is not a power of two */
152         if (align && !rte_is_power_of_2(align)) {
153                 RTE_LOG(ERR, EAL, "%s(): Invalid alignment: %u\n", __func__,
154                                 align);
155                 rte_errno = EINVAL;
156                 return NULL;
157         }
158
159         /* alignment less than cache size is not allowed */
160         if (align < RTE_CACHE_LINE_SIZE)
161                 align = RTE_CACHE_LINE_SIZE;
162
163         /* align length on cache boundary. Check for overflow before doing so */
164         if (len > SIZE_MAX - RTE_CACHE_LINE_MASK) {
165                 rte_errno = EINVAL; /* requested size too big */
166                 return NULL;
167         }
168
169         len += RTE_CACHE_LINE_MASK;
170         len &= ~((size_t) RTE_CACHE_LINE_MASK);
171
172         /* save minimal requested  length */
173         requested_len = RTE_MAX((size_t)RTE_CACHE_LINE_SIZE,  len);
174
175         /* check that boundary condition is valid */
176         if (bound != 0 && (requested_len > bound || !rte_is_power_of_2(bound))) {
177                 rte_errno = EINVAL;
178                 return NULL;
179         }
180
181         if ((socket_id != SOCKET_ID_ANY) && (socket_id >= RTE_MAX_NUMA_NODES)) {
182                 rte_errno = EINVAL;
183                 return NULL;
184         }
185
186         if (!rte_eal_has_hugepages())
187                 socket_id = SOCKET_ID_ANY;
188
189         if (len == 0) {
190                 if (bound != 0)
191                         requested_len = bound;
192                 else
193                         requested_len = find_heap_max_free_elem(&socket_id, align);
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);
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, bound);
213                         if (mz_addr != NULL)
214                                 break;
215                 }
216         }
217
218         if (mz_addr == NULL) {
219                 rte_errno = ENOMEM;
220                 return NULL;
221         }
222
223         const struct malloc_elem *elem = malloc_elem_from_data(mz_addr);
224
225         /* fill the zone in config */
226         struct rte_memzone *mz = get_next_free_memzone();
227
228         if (mz == NULL) {
229                 RTE_LOG(ERR, EAL, "%s(): Cannot find free memzone but there is room "
230                                 "in config!\n", __func__);
231                 rte_errno = ENOSPC;
232                 return NULL;
233         }
234
235         mcfg->memzone_cnt++;
236         snprintf(mz->name, sizeof(mz->name), "%s", name);
237         mz->phys_addr = rte_malloc_virt2phy(mz_addr);
238         mz->addr = mz_addr;
239         mz->len = (requested_len == 0 ? elem->size : requested_len);
240         mz->hugepage_sz = elem->ms->hugepage_sz;
241         mz->socket_id = elem->ms->socket_id;
242         mz->flags = 0;
243         mz->memseg_id = elem->ms - rte_eal_get_configuration()->mem_config->memseg;
244
245         return mz;
246 }
247
248 static const struct rte_memzone *
249 rte_memzone_reserve_thread_safe(const char *name, size_t len,
250                                 int socket_id, unsigned flags, unsigned align,
251                                 unsigned 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 /* Dump all reserved memory zones on console */
363 void
364 rte_memzone_dump(FILE *f)
365 {
366         struct rte_mem_config *mcfg;
367         unsigned i = 0;
368
369         /* get pointer to global configuration */
370         mcfg = rte_eal_get_configuration()->mem_config;
371
372         rte_rwlock_read_lock(&mcfg->mlock);
373         /* dump all zones */
374         for (i=0; i<RTE_MAX_MEMZONE; i++) {
375                 if (mcfg->memzone[i].addr == NULL)
376                         break;
377                 fprintf(f, "Zone %u: name:<%s>, phys:0x%"PRIx64", len:0x%zx"
378                        ", virt:%p, socket_id:%"PRId32", flags:%"PRIx32"\n", i,
379                        mcfg->memzone[i].name,
380                        mcfg->memzone[i].phys_addr,
381                        mcfg->memzone[i].len,
382                        mcfg->memzone[i].addr,
383                        mcfg->memzone[i].socket_id,
384                        mcfg->memzone[i].flags);
385         }
386         rte_rwlock_read_unlock(&mcfg->mlock);
387 }
388
389 /*
390  * Init the memzone subsystem
391  */
392 int
393 rte_eal_memzone_init(void)
394 {
395         struct rte_mem_config *mcfg;
396         const struct rte_memseg *memseg;
397
398         /* get pointer to global configuration */
399         mcfg = rte_eal_get_configuration()->mem_config;
400
401         /* secondary processes don't need to initialise anything */
402         if (rte_eal_process_type() == RTE_PROC_SECONDARY)
403                 return 0;
404
405         memseg = rte_eal_get_physmem_layout();
406         if (memseg == NULL) {
407                 RTE_LOG(ERR, EAL, "%s(): Cannot get physical layout\n", __func__);
408                 return -1;
409         }
410
411         rte_rwlock_write_lock(&mcfg->mlock);
412
413         /* delete all zones */
414         mcfg->memzone_cnt = 0;
415         memset(mcfg->memzone, 0, sizeof(mcfg->memzone));
416
417         rte_rwlock_write_unlock(&mcfg->mlock);
418
419         return rte_eal_malloc_heap_init();
420 }
421
422 /* Walk all reserved memory zones */
423 void rte_memzone_walk(void (*func)(const struct rte_memzone *, void *),
424                       void *arg)
425 {
426         struct rte_mem_config *mcfg;
427         unsigned i;
428
429         mcfg = rte_eal_get_configuration()->mem_config;
430
431         rte_rwlock_read_lock(&mcfg->mlock);
432         for (i=0; i<RTE_MAX_MEMZONE; i++) {
433                 if (mcfg->memzone[i].addr != NULL)
434                         (*func)(&mcfg->memzone[i], arg);
435         }
436         rte_rwlock_read_unlock(&mcfg->mlock);
437 }