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