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