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