malloc: enable memory hotplug support
[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         struct rte_mem_config *mcfg;
32         struct rte_fbarray *arr;
33         const struct rte_memzone *mz;
34         int i = 0;
35
36         /* get pointer to global configuration */
37         mcfg = rte_eal_get_configuration()->mem_config;
38         arr = &mcfg->memzones;
39
40         /*
41          * the algorithm is not optimal (linear), but there are few
42          * zones and this function should be called at init only
43          */
44         i = rte_fbarray_find_next_used(arr, 0);
45         while (i >= 0) {
46                 mz = rte_fbarray_get(arr, i);
47                 if (mz->addr != NULL &&
48                                 !strncmp(name, mz->name, RTE_MEMZONE_NAMESIZE))
49                         return mz;
50                 i = rte_fbarray_find_next_used(arr, i + 1);
51         }
52         return NULL;
53 }
54
55
56 /* This function will return the greatest free block if a heap has been
57  * specified. If no heap has been specified, it will return the heap and
58  * length of the greatest free block available in all heaps */
59 static size_t
60 find_heap_max_free_elem(int *s, unsigned align)
61 {
62         struct rte_mem_config *mcfg;
63         struct rte_malloc_socket_stats stats;
64         int i, socket = *s;
65         size_t len = 0;
66
67         /* get pointer to global configuration */
68         mcfg = rte_eal_get_configuration()->mem_config;
69
70         for (i = 0; i < RTE_MAX_NUMA_NODES; i++) {
71                 if ((socket != SOCKET_ID_ANY) && (socket != i))
72                         continue;
73
74                 malloc_heap_get_stats(&mcfg->malloc_heaps[i], &stats);
75                 if (stats.greatest_free_size > len) {
76                         len = stats.greatest_free_size;
77                         *s = i;
78                 }
79         }
80
81         if (len < MALLOC_ELEM_OVERHEAD + align)
82                 return 0;
83
84         return len - MALLOC_ELEM_OVERHEAD - align;
85 }
86
87 static const struct rte_memzone *
88 memzone_reserve_aligned_thread_unsafe(const char *name, size_t len,
89                 int socket_id, unsigned int flags, unsigned int align,
90                 unsigned int bound)
91 {
92         struct rte_memzone *mz;
93         struct rte_mem_config *mcfg;
94         struct rte_fbarray *arr;
95         size_t requested_len;
96         int mz_idx;
97         bool contig;
98
99         /* get pointer to global configuration */
100         mcfg = rte_eal_get_configuration()->mem_config;
101         arr = &mcfg->memzones;
102
103         /* no more room in config */
104         if (arr->count >= arr->len) {
105                 RTE_LOG(ERR, EAL, "%s(): No more room in config\n", __func__);
106                 rte_errno = ENOSPC;
107                 return NULL;
108         }
109
110         if (strlen(name) > sizeof(mz->name) - 1) {
111                 RTE_LOG(DEBUG, EAL, "%s(): memzone <%s>: name too long\n",
112                         __func__, name);
113                 rte_errno = ENAMETOOLONG;
114                 return NULL;
115         }
116
117         /* zone already exist */
118         if ((memzone_lookup_thread_unsafe(name)) != NULL) {
119                 RTE_LOG(DEBUG, EAL, "%s(): memzone <%s> already exists\n",
120                         __func__, name);
121                 rte_errno = EEXIST;
122                 return NULL;
123         }
124
125         /* if alignment is not a power of two */
126         if (align && !rte_is_power_of_2(align)) {
127                 RTE_LOG(ERR, EAL, "%s(): Invalid alignment: %u\n", __func__,
128                                 align);
129                 rte_errno = EINVAL;
130                 return NULL;
131         }
132
133         /* alignment less than cache size is not allowed */
134         if (align < RTE_CACHE_LINE_SIZE)
135                 align = RTE_CACHE_LINE_SIZE;
136
137         /* align length on cache boundary. Check for overflow before doing so */
138         if (len > SIZE_MAX - RTE_CACHE_LINE_MASK) {
139                 rte_errno = EINVAL; /* requested size too big */
140                 return NULL;
141         }
142
143         len += RTE_CACHE_LINE_MASK;
144         len &= ~((size_t) RTE_CACHE_LINE_MASK);
145
146         /* save minimal requested  length */
147         requested_len = RTE_MAX((size_t)RTE_CACHE_LINE_SIZE,  len);
148
149         /* check that boundary condition is valid */
150         if (bound != 0 && (requested_len > bound || !rte_is_power_of_2(bound))) {
151                 rte_errno = EINVAL;
152                 return NULL;
153         }
154
155         if ((socket_id != SOCKET_ID_ANY) &&
156             (socket_id >= RTE_MAX_NUMA_NODES || socket_id < 0)) {
157                 rte_errno = EINVAL;
158                 return NULL;
159         }
160
161         if (!rte_eal_has_hugepages())
162                 socket_id = SOCKET_ID_ANY;
163
164         contig = (flags & RTE_MEMZONE_IOVA_CONTIG) != 0;
165         /* malloc only cares about size flags, remove contig flag from flags */
166         flags &= ~RTE_MEMZONE_IOVA_CONTIG;
167
168         if (len == 0) {
169                 /* len == 0 is only allowed for non-contiguous zones */
170                 if (contig) {
171                         RTE_LOG(DEBUG, EAL, "Reserving zero-length contiguous memzones is not supported\n");
172                         rte_errno = EINVAL;
173                         return NULL;
174                 }
175                 if (bound != 0)
176                         requested_len = bound;
177                 else {
178                         requested_len = find_heap_max_free_elem(&socket_id, align);
179                         if (requested_len == 0) {
180                                 rte_errno = ENOMEM;
181                                 return NULL;
182                         }
183                 }
184         }
185
186         /* allocate memory on heap */
187         void *mz_addr = malloc_heap_alloc(NULL, requested_len, socket_id, flags,
188                         align, bound, contig);
189         if (mz_addr == NULL) {
190                 rte_errno = ENOMEM;
191                 return NULL;
192         }
193
194         struct malloc_elem *elem = malloc_elem_from_data(mz_addr);
195
196         /* fill the zone in config */
197         mz_idx = rte_fbarray_find_next_free(arr, 0);
198
199         if (mz_idx < 0) {
200                 mz = NULL;
201         } else {
202                 rte_fbarray_set_used(arr, mz_idx);
203                 mz = rte_fbarray_get(arr, mz_idx);
204         }
205
206         if (mz == NULL) {
207                 RTE_LOG(ERR, EAL, "%s(): Cannot find free memzone\n", __func__);
208                 malloc_elem_free(elem);
209                 rte_errno = ENOSPC;
210                 return NULL;
211         }
212
213         snprintf(mz->name, sizeof(mz->name), "%s", name);
214         mz->iova = rte_malloc_virt2iova(mz_addr);
215         mz->addr = mz_addr;
216         mz->len = (requested_len == 0 ? elem->size : requested_len);
217         mz->hugepage_sz = elem->msl->page_sz;
218         mz->socket_id = elem->msl->socket_id;
219         mz->flags = 0;
220
221         return mz;
222 }
223
224 static const struct rte_memzone *
225 rte_memzone_reserve_thread_safe(const char *name, size_t len, int socket_id,
226                 unsigned int flags, unsigned int align, unsigned int bound)
227 {
228         struct rte_mem_config *mcfg;
229         const struct rte_memzone *mz = NULL;
230
231         /* get pointer to global configuration */
232         mcfg = rte_eal_get_configuration()->mem_config;
233
234         rte_rwlock_write_lock(&mcfg->mlock);
235
236         mz = memzone_reserve_aligned_thread_unsafe(
237                 name, len, socket_id, flags, align, bound);
238
239         rte_rwlock_write_unlock(&mcfg->mlock);
240
241         return mz;
242 }
243
244 /*
245  * Return a pointer to a correctly filled memzone descriptor (with a
246  * specified alignment and boundary). If the allocation cannot be done,
247  * return NULL.
248  */
249 const struct rte_memzone *
250 rte_memzone_reserve_bounded(const char *name, size_t len, int socket_id,
251                             unsigned flags, unsigned align, unsigned bound)
252 {
253         return rte_memzone_reserve_thread_safe(name, len, socket_id, flags,
254                                                align, bound);
255 }
256
257 /*
258  * Return a pointer to a correctly filled memzone descriptor (with a
259  * specified alignment). If the allocation cannot be done, return NULL.
260  */
261 const struct rte_memzone *
262 rte_memzone_reserve_aligned(const char *name, size_t len, int socket_id,
263                             unsigned flags, unsigned align)
264 {
265         return rte_memzone_reserve_thread_safe(name, len, socket_id, flags,
266                                                align, 0);
267 }
268
269 /*
270  * Return a pointer to a correctly filled memzone descriptor. If the
271  * allocation cannot be done, return NULL.
272  */
273 const struct rte_memzone *
274 rte_memzone_reserve(const char *name, size_t len, int socket_id,
275                     unsigned flags)
276 {
277         return rte_memzone_reserve_thread_safe(name, len, socket_id,
278                                                flags, RTE_CACHE_LINE_SIZE, 0);
279 }
280
281 int
282 rte_memzone_free(const struct rte_memzone *mz)
283 {
284         struct rte_mem_config *mcfg;
285         struct rte_fbarray *arr;
286         struct rte_memzone *found_mz;
287         int ret = 0;
288         void *addr = NULL;
289         unsigned idx;
290
291         if (mz == NULL)
292                 return -EINVAL;
293
294         mcfg = rte_eal_get_configuration()->mem_config;
295         arr = &mcfg->memzones;
296
297         rte_rwlock_write_lock(&mcfg->mlock);
298
299         idx = rte_fbarray_find_idx(arr, mz);
300         found_mz = rte_fbarray_get(arr, idx);
301
302         if (found_mz == NULL) {
303                 ret = -EINVAL;
304         } else if (found_mz->addr == NULL) {
305                 RTE_LOG(ERR, EAL, "Memzone is not allocated\n");
306                 ret = -EINVAL;
307         } else {
308                 addr = found_mz->addr;
309                 memset(found_mz, 0, sizeof(*found_mz));
310                 rte_fbarray_set_free(arr, idx);
311         }
312
313         rte_rwlock_write_unlock(&mcfg->mlock);
314
315         if (addr != NULL)
316                 rte_free(addr);
317
318         return ret;
319 }
320
321 /*
322  * Lookup for the memzone identified by the given name
323  */
324 const struct rte_memzone *
325 rte_memzone_lookup(const char *name)
326 {
327         struct rte_mem_config *mcfg;
328         const struct rte_memzone *memzone = NULL;
329
330         mcfg = rte_eal_get_configuration()->mem_config;
331
332         rte_rwlock_read_lock(&mcfg->mlock);
333
334         memzone = memzone_lookup_thread_unsafe(name);
335
336         rte_rwlock_read_unlock(&mcfg->mlock);
337
338         return memzone;
339 }
340
341 static void
342 dump_memzone(const struct rte_memzone *mz, void *arg)
343 {
344         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
345         struct rte_memseg_list *msl = NULL;
346         void *cur_addr, *mz_end;
347         struct rte_memseg *ms;
348         int mz_idx, ms_idx;
349         size_t page_sz;
350         FILE *f = arg;
351
352         mz_idx = rte_fbarray_find_idx(&mcfg->memzones, mz);
353
354         fprintf(f, "Zone %u: name:<%s>, len:0x%zx, virt:%p, "
355                                 "socket_id:%"PRId32", flags:%"PRIx32"\n",
356                         mz_idx,
357                         mz->name,
358                         mz->len,
359                         mz->addr,
360                         mz->socket_id,
361                         mz->flags);
362
363         /* go through each page occupied by this memzone */
364         msl = rte_mem_virt2memseg_list(mz->addr);
365         if (!msl) {
366                 RTE_LOG(DEBUG, EAL, "Skipping bad memzone\n");
367                 return;
368         }
369         page_sz = (size_t)mz->hugepage_sz;
370         cur_addr = RTE_PTR_ALIGN_FLOOR(mz->addr, page_sz);
371         mz_end = RTE_PTR_ADD(cur_addr, mz->len);
372
373         fprintf(f, "physical segments used:\n");
374         ms_idx = RTE_PTR_DIFF(mz->addr, msl->base_va) / page_sz;
375         ms = rte_fbarray_get(&msl->memseg_arr, ms_idx);
376
377         do {
378                 fprintf(f, "  addr: %p iova: 0x%" PRIx64 " "
379                                 "len: 0x%zx "
380                                 "pagesz: 0x%zx\n",
381                         cur_addr, ms->iova, ms->len, page_sz);
382
383                 /* advance VA to next page */
384                 cur_addr = RTE_PTR_ADD(cur_addr, page_sz);
385
386                 /* memzones occupy contiguous segments */
387                 ++ms;
388         } while (cur_addr < mz_end);
389 }
390
391 /* Dump all reserved memory zones on console */
392 void
393 rte_memzone_dump(FILE *f)
394 {
395         rte_memzone_walk(dump_memzone, f);
396 }
397
398 /*
399  * Init the memzone subsystem
400  */
401 int
402 rte_eal_memzone_init(void)
403 {
404         struct rte_mem_config *mcfg;
405
406         /* get pointer to global configuration */
407         mcfg = rte_eal_get_configuration()->mem_config;
408
409         rte_rwlock_write_lock(&mcfg->mlock);
410
411         if (rte_eal_process_type() == RTE_PROC_PRIMARY &&
412                         rte_fbarray_init(&mcfg->memzones, "memzone",
413                         RTE_MAX_MEMZONE, sizeof(struct rte_memzone))) {
414                 RTE_LOG(ERR, EAL, "Cannot allocate memzone list\n");
415                 return -1;
416         } else if (rte_eal_process_type() == RTE_PROC_SECONDARY &&
417                         rte_fbarray_attach(&mcfg->memzones)) {
418                 RTE_LOG(ERR, EAL, "Cannot attach to memzone list\n");
419                 rte_rwlock_write_unlock(&mcfg->mlock);
420                 return -1;
421         }
422
423         rte_rwlock_write_unlock(&mcfg->mlock);
424
425         return 0;
426 }
427
428 /* Walk all reserved memory zones */
429 void rte_memzone_walk(void (*func)(const struct rte_memzone *, void *),
430                       void *arg)
431 {
432         struct rte_mem_config *mcfg;
433         struct rte_fbarray *arr;
434         int i;
435
436         mcfg = rte_eal_get_configuration()->mem_config;
437         arr = &mcfg->memzones;
438
439         rte_rwlock_read_lock(&mcfg->mlock);
440         i = rte_fbarray_find_next_used(arr, 0);
441         while (i >= 0) {
442                 struct rte_memzone *mz = rte_fbarray_get(arr, i);
443                 (*func)(mz, arg);
444                 i = rte_fbarray_find_next_used(arr, i + 1);
445         }
446         rte_rwlock_read_unlock(&mcfg->mlock);
447 }