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