Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2010-2014 Intel Corporation
3 : : */
4 : :
5 : : #include <stdio.h>
6 : : #include <stdint.h>
7 : : #include <inttypes.h>
8 : : #include <string.h>
9 : : #include <errno.h>
10 : :
11 : : #include <eal_export.h>
12 : : #include <eal_trace_internal.h>
13 : : #include <rte_log.h>
14 : : #include <rte_memory.h>
15 : : #include <rte_memzone.h>
16 : : #include <rte_eal.h>
17 : : #include <rte_errno.h>
18 : : #include <rte_string_fns.h>
19 : : #include <rte_common.h>
20 : :
21 : : #include "malloc_heap.h"
22 : : #include "malloc_elem.h"
23 : : #include "eal_private.h"
24 : : #include "eal_memcfg.h"
25 : :
26 : : /* Default count used until rte_memzone_max_set() is called */
27 : : #define DEFAULT_MAX_MEMZONE_COUNT 2560
28 : :
29 : : RTE_EXPORT_SYMBOL(rte_memzone_max_set)
30 : : int
31 : 0 : rte_memzone_max_set(size_t max)
32 : : {
33 : : struct rte_mem_config *mcfg;
34 : :
35 [ # # ]: 0 : if (eal_get_internal_configuration()->init_complete > 0) {
36 : 0 : EAL_LOG(ERR, "Max memzone cannot be set after EAL init");
37 : 0 : return -1;
38 : : }
39 : :
40 : 0 : mcfg = rte_eal_get_configuration()->mem_config;
41 [ # # ]: 0 : if (mcfg == NULL) {
42 : 0 : EAL_LOG(ERR, "Failed to set max memzone count");
43 : 0 : return -1;
44 : : }
45 : :
46 : 0 : mcfg->max_memzone = max;
47 : :
48 : 0 : return 0;
49 : : }
50 : :
51 : : RTE_EXPORT_SYMBOL(rte_memzone_max_get)
52 : : size_t
53 : 164 : rte_memzone_max_get(void)
54 : : {
55 : : struct rte_mem_config *mcfg;
56 : :
57 : 164 : mcfg = rte_eal_get_configuration()->mem_config;
58 [ + - + - ]: 164 : if (mcfg == NULL || mcfg->max_memzone == 0)
59 : 164 : return DEFAULT_MAX_MEMZONE_COUNT;
60 : :
61 : : return mcfg->max_memzone;
62 : : }
63 : :
64 : : static inline const struct rte_memzone *
65 : 6613 : memzone_lookup_thread_unsafe(const char *name)
66 : : {
67 : : struct rte_mem_config *mcfg;
68 : : struct rte_fbarray *arr;
69 : : const struct rte_memzone *mz;
70 : : int i = 0;
71 : :
72 : : /* get pointer to global configuration */
73 : 6613 : mcfg = rte_eal_get_configuration()->mem_config;
74 : 6613 : arr = &mcfg->memzones;
75 : :
76 : : /*
77 : : * the algorithm is not optimal (linear), but there are few
78 : : * zones and this function should be called at init only
79 : : */
80 : 6613 : i = rte_fbarray_find_next_used(arr, 0);
81 [ + + ]: 601960 : while (i >= 0) {
82 : 595450 : mz = rte_fbarray_get(arr, i);
83 [ + - ]: 595450 : if (mz->addr != NULL &&
84 [ + + ]: 595450 : !strncmp(name, mz->name, RTE_MEMZONE_NAMESIZE))
85 : 103 : return mz;
86 : 595347 : i = rte_fbarray_find_next_used(arr, i + 1);
87 : : }
88 : : return NULL;
89 : : }
90 : :
91 : : #define MEMZONE_KNOWN_FLAGS (RTE_MEMZONE_2MB \
92 : : | RTE_MEMZONE_1GB \
93 : : | RTE_MEMZONE_16MB \
94 : : | RTE_MEMZONE_16GB \
95 : : | RTE_MEMZONE_256KB \
96 : : | RTE_MEMZONE_256MB \
97 : : | RTE_MEMZONE_512MB \
98 : : | RTE_MEMZONE_4GB \
99 : : | RTE_MEMZONE_SIZE_HINT_ONLY \
100 : : | RTE_MEMZONE_IOVA_CONTIG \
101 : : )
102 : :
103 : : static const struct rte_memzone *
104 : 6098 : memzone_reserve_aligned_thread_unsafe(const char *name, size_t len,
105 : : int socket_id, unsigned int flags, unsigned int align,
106 : : unsigned int bound)
107 : : {
108 : : struct rte_memzone *mz;
109 : : struct rte_mem_config *mcfg;
110 : : struct rte_fbarray *arr;
111 : : void *mz_addr;
112 : : size_t requested_len;
113 : : int mz_idx;
114 : : bool contig;
115 : :
116 : : /* get pointer to global configuration */
117 : 6098 : mcfg = rte_eal_get_configuration()->mem_config;
118 : 6098 : arr = &mcfg->memzones;
119 : :
120 : : /* no more room in config */
121 [ - + ]: 6098 : if (arr->count >= arr->len) {
122 : 0 : EAL_LOG(ERR,
123 : : "%s(): Number of requested memzone segments exceeds maximum "
124 : : "%u", __func__, arr->len);
125 : :
126 : 0 : rte_errno = ENOSPC;
127 : 0 : return NULL;
128 : : }
129 : :
130 [ + + ]: 6098 : if (strlen(name) > sizeof(mz->name) - 1) {
131 : 1 : EAL_LOG(DEBUG, "%s(): memzone <%s>: name too long",
132 : : __func__, name);
133 : 1 : rte_errno = ENAMETOOLONG;
134 : 1 : return NULL;
135 : : }
136 : :
137 : : /* zone already exist */
138 [ + + ]: 6097 : if ((memzone_lookup_thread_unsafe(name)) != NULL) {
139 : 36 : EAL_LOG(DEBUG, "%s(): memzone <%s> already exists",
140 : : __func__, name);
141 : 36 : rte_errno = EEXIST;
142 : 36 : return NULL;
143 : : }
144 : :
145 : : /* if alignment is not a power of two */
146 [ + - ]: 6061 : if (align && !rte_is_power_of_2(align)) {
147 : 1 : EAL_LOG(ERR, "%s(): Invalid alignment: %u", __func__,
148 : : align);
149 : 1 : rte_errno = EINVAL;
150 : 1 : return NULL;
151 : : }
152 : :
153 : : /* alignment less than cache size is not allowed */
154 : : if (align < RTE_CACHE_LINE_SIZE)
155 : : align = RTE_CACHE_LINE_SIZE;
156 : :
157 : : /* align length on cache boundary. Check for overflow before doing so */
158 [ - + ]: 6060 : if (len > SIZE_MAX - RTE_CACHE_LINE_MASK) {
159 : 0 : rte_errno = EINVAL; /* requested size too big */
160 : 0 : return NULL;
161 : : }
162 : :
163 : 6060 : len = RTE_ALIGN_CEIL(len, RTE_CACHE_LINE_SIZE);
164 : :
165 : : /* save minimal requested length */
166 : 6060 : requested_len = RTE_MAX((size_t)RTE_CACHE_LINE_SIZE, len);
167 : :
168 : : /* check that boundary condition is valid */
169 [ + + + + ]: 6060 : if (bound != 0 && (requested_len > bound || !rte_is_power_of_2(bound))) {
170 : 2 : rte_errno = EINVAL;
171 : 2 : return NULL;
172 : : }
173 : :
174 [ + + ]: 6058 : if ((socket_id != SOCKET_ID_ANY) && socket_id < 0) {
175 : 4 : rte_errno = EINVAL;
176 : 4 : return NULL;
177 : : }
178 : :
179 [ + + ]: 6054 : if ((flags & ~MEMZONE_KNOWN_FLAGS) != 0) {
180 : 1 : rte_errno = EINVAL;
181 : 1 : return NULL;
182 : : }
183 : :
184 : : /* only set socket to SOCKET_ID_ANY if we aren't allocating for an
185 : : * external heap.
186 : : */
187 [ + + + + ]: 6053 : if (!rte_eal_has_hugepages() && socket_id < RTE_MAX_NUMA_NODES)
188 : : socket_id = SOCKET_ID_ANY;
189 : :
190 : 6053 : contig = (flags & RTE_MEMZONE_IOVA_CONTIG) != 0;
191 : : /* malloc only cares about size flags, remove contig flag from flags */
192 : 6053 : flags &= ~RTE_MEMZONE_IOVA_CONTIG;
193 : :
194 [ + + ]: 6053 : if (len == 0 && bound == 0) {
195 : : /* no size constraints were placed, so use malloc elem len */
196 : : requested_len = 0;
197 : 3 : mz_addr = malloc_heap_alloc_biggest(socket_id, flags, align, contig);
198 : : } else {
199 [ + + ]: 6050 : if (len == 0)
200 : 1 : requested_len = bound;
201 : : /* allocate memory on heap */
202 : 6050 : mz_addr = malloc_heap_alloc(requested_len, socket_id, flags, align, bound, contig);
203 : : }
204 [ + + ]: 6053 : if (mz_addr == NULL) {
205 : 2 : rte_errno = ENOMEM;
206 : 2 : return NULL;
207 : : }
208 : :
209 : : struct malloc_elem *elem = malloc_elem_from_data(mz_addr);
210 : :
211 : : /* fill the zone in config */
212 : 6051 : mz_idx = rte_fbarray_find_next_free(arr, 0);
213 : :
214 [ + - ]: 6051 : if (mz_idx < 0) {
215 : : mz = NULL;
216 : : } else {
217 : 6051 : rte_fbarray_set_used(arr, mz_idx);
218 : 6051 : mz = rte_fbarray_get(arr, mz_idx);
219 : : }
220 : :
221 [ - + ]: 6051 : if (mz == NULL) {
222 : 0 : EAL_LOG(ERR, "%s(): Cannot find free memzone", __func__);
223 : 0 : malloc_heap_free(elem);
224 : 0 : rte_errno = ENOSPC;
225 : 0 : return NULL;
226 : : }
227 : :
228 : 6051 : strlcpy(mz->name, name, sizeof(mz->name));
229 : 6051 : mz->iova = rte_malloc_virt2iova(mz_addr);
230 : 6051 : mz->addr = mz_addr;
231 : 6051 : mz->len = requested_len == 0 ?
232 [ + + ]: 6051 : elem->size - elem->pad - MALLOC_ELEM_OVERHEAD :
233 : : requested_len;
234 : 6051 : mz->hugepage_sz = elem->msl->page_sz;
235 : 6051 : mz->socket_id = elem->msl->socket_id;
236 : 6051 : mz->flags = 0;
237 : :
238 : 6051 : return mz;
239 : : }
240 : :
241 : : static const struct rte_memzone *
242 : 6098 : rte_memzone_reserve_thread_safe(const char *name, size_t len, int socket_id,
243 : : unsigned int flags, unsigned int align, unsigned int bound)
244 : : {
245 : : struct rte_mem_config *mcfg;
246 : : const struct rte_memzone *mz = NULL;
247 : :
248 : : /* get pointer to global configuration */
249 : 6098 : mcfg = rte_eal_get_configuration()->mem_config;
250 : :
251 : 6098 : rte_rwlock_write_lock(&mcfg->mlock);
252 : :
253 : 6098 : mz = memzone_reserve_aligned_thread_unsafe(
254 : : name, len, socket_id, flags, align, bound);
255 : :
256 : 6098 : rte_eal_trace_memzone_reserve(name, len, socket_id, flags, align,
257 : : bound, mz);
258 : :
259 : : rte_rwlock_write_unlock(&mcfg->mlock);
260 : :
261 : 6098 : return mz;
262 : : }
263 : :
264 : : /*
265 : : * Return a pointer to a correctly filled memzone descriptor (with a
266 : : * specified alignment and boundary). If the allocation cannot be done,
267 : : * return NULL.
268 : : */
269 : : RTE_EXPORT_SYMBOL(rte_memzone_reserve_bounded)
270 : : const struct rte_memzone *
271 : 6 : rte_memzone_reserve_bounded(const char *name, size_t len, int socket_id,
272 : : unsigned flags, unsigned align, unsigned bound)
273 : : {
274 : 6 : return rte_memzone_reserve_thread_safe(name, len, socket_id, flags,
275 : : align, bound);
276 : : }
277 : :
278 : : /*
279 : : * Return a pointer to a correctly filled memzone descriptor (with a
280 : : * specified alignment). If the allocation cannot be done, return NULL.
281 : : */
282 : : RTE_EXPORT_SYMBOL(rte_memzone_reserve_aligned)
283 : : const struct rte_memzone *
284 : 4911 : rte_memzone_reserve_aligned(const char *name, size_t len, int socket_id,
285 : : unsigned flags, unsigned align)
286 : : {
287 : 4911 : return rte_memzone_reserve_thread_safe(name, len, socket_id, flags,
288 : : align, 0);
289 : : }
290 : :
291 : : /*
292 : : * Return a pointer to a correctly filled memzone descriptor. If the
293 : : * allocation cannot be done, return NULL.
294 : : */
295 : : RTE_EXPORT_SYMBOL(rte_memzone_reserve)
296 : : const struct rte_memzone *
297 : 1181 : rte_memzone_reserve(const char *name, size_t len, int socket_id,
298 : : unsigned flags)
299 : : {
300 : 1181 : return rte_memzone_reserve_thread_safe(name, len, socket_id,
301 : : flags, RTE_CACHE_LINE_SIZE, 0);
302 : : }
303 : :
304 : : RTE_EXPORT_SYMBOL(rte_memzone_free)
305 : : int
306 : 5983 : rte_memzone_free(const struct rte_memzone *mz)
307 : : {
308 : : char name[RTE_MEMZONE_NAMESIZE];
309 : : struct rte_mem_config *mcfg;
310 : : struct rte_fbarray *arr;
311 : : struct rte_memzone *found_mz;
312 : : int ret = 0;
313 : : void *addr = NULL;
314 : : unsigned idx;
315 : :
316 [ + + ]: 5983 : if (mz == NULL)
317 : : return -EINVAL;
318 : :
319 : 5982 : rte_strlcpy(name, mz->name, RTE_MEMZONE_NAMESIZE);
320 : 5982 : mcfg = rte_eal_get_configuration()->mem_config;
321 : 5982 : arr = &mcfg->memzones;
322 : :
323 : 5982 : rte_rwlock_write_lock(&mcfg->mlock);
324 : :
325 : 5982 : idx = rte_fbarray_find_idx(arr, mz);
326 : 5982 : found_mz = rte_fbarray_get(arr, idx);
327 : :
328 [ + - ]: 5982 : if (found_mz == NULL) {
329 : : ret = -EINVAL;
330 [ - + ]: 5982 : } else if (found_mz->addr == NULL) {
331 : 0 : EAL_LOG(ERR, "Memzone is not allocated");
332 : : ret = -EINVAL;
333 : : } else {
334 : : addr = found_mz->addr;
335 : : memset(found_mz, 0, sizeof(*found_mz));
336 : 5982 : rte_fbarray_set_free(arr, idx);
337 : : }
338 : :
339 : : rte_rwlock_write_unlock(&mcfg->mlock);
340 : :
341 : 5982 : rte_eal_trace_memzone_free(name, addr, ret);
342 : :
343 : 5982 : rte_free(addr);
344 : :
345 : 5982 : return ret;
346 : : }
347 : :
348 : : /*
349 : : * Lookup for the memzone identified by the given name
350 : : */
351 : : RTE_EXPORT_SYMBOL(rte_memzone_lookup)
352 : : const struct rte_memzone *
353 : 516 : rte_memzone_lookup(const char *name)
354 : : {
355 : : struct rte_mem_config *mcfg;
356 : : const struct rte_memzone *memzone = NULL;
357 : :
358 : 516 : mcfg = rte_eal_get_configuration()->mem_config;
359 : :
360 : 516 : rte_rwlock_read_lock(&mcfg->mlock);
361 : :
362 : 516 : memzone = memzone_lookup_thread_unsafe(name);
363 : :
364 : : rte_rwlock_read_unlock(&mcfg->mlock);
365 : :
366 : 516 : rte_eal_trace_memzone_lookup(name, memzone);
367 : 516 : return memzone;
368 : : }
369 : :
370 : : struct memzone_info {
371 : : FILE *f;
372 : : uint64_t total_size;
373 : : };
374 : :
375 : : static void
376 : 5 : dump_memzone(const struct rte_memzone *mz, void *arg)
377 : : {
378 : 5 : struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
379 : : struct rte_memseg_list *msl = NULL;
380 : : struct memzone_info *info = arg;
381 : : void *cur_addr, *mz_end;
382 : : struct rte_memseg *ms;
383 : : int mz_idx, ms_idx;
384 : 5 : FILE *f = info->f;
385 : : size_t page_sz;
386 : :
387 : 5 : mz_idx = rte_fbarray_find_idx(&mcfg->memzones, mz);
388 : 5 : info->total_size += mz->len;
389 : :
390 : : fprintf(f, "Zone %u: name:<%s>, len:0x%zx, virt:%p, "
391 : : "socket_id:%"PRId32", flags:%"PRIx32"\n",
392 : : mz_idx,
393 : 5 : mz->name,
394 : : mz->len,
395 : 5 : mz->addr,
396 : 5 : mz->socket_id,
397 : 5 : mz->flags);
398 : :
399 : : /* go through each page occupied by this memzone */
400 : 5 : msl = rte_mem_virt2memseg_list(mz->addr);
401 [ - + ]: 5 : if (!msl) {
402 : 0 : EAL_LOG(DEBUG, "Skipping bad memzone");
403 : 0 : return;
404 : : }
405 : 5 : page_sz = (size_t)mz->hugepage_sz;
406 : 5 : cur_addr = RTE_PTR_ALIGN_FLOOR(mz->addr, page_sz);
407 : 5 : mz_end = RTE_PTR_ADD(cur_addr, mz->len);
408 : :
409 : : fprintf(f, "physical segments used:\n");
410 : 5 : ms_idx = RTE_PTR_DIFF(mz->addr, msl->base_va) / page_sz;
411 : 5 : ms = rte_fbarray_get(&msl->memseg_arr, ms_idx);
412 : :
413 : : do {
414 : 5 : fprintf(f, " addr: %p iova: 0x%" PRIx64 " "
415 : : "len: 0x%zx "
416 : : "pagesz: 0x%zx\n",
417 : : cur_addr, ms->iova, ms->len, page_sz);
418 : :
419 : : /* advance VA to next page */
420 : 5 : cur_addr = RTE_PTR_ADD(cur_addr, page_sz);
421 : :
422 : : /* memzones occupy contiguous segments */
423 : 5 : ++ms;
424 [ - + ]: 5 : } while (cur_addr < mz_end);
425 : : }
426 : :
427 : : /* Dump all reserved memory zones on console */
428 : : RTE_EXPORT_SYMBOL(rte_memzone_dump)
429 : : void
430 : 1 : rte_memzone_dump(FILE *f)
431 : : {
432 : 1 : struct memzone_info info = { .f = f };
433 : :
434 : 1 : rte_memzone_walk(dump_memzone, &info);
435 : 1 : fprintf(f, "Total Memory Zones size = %"PRIu64"M\n",
436 : 1 : info.total_size / (1024 * 1024));
437 : 1 : }
438 : :
439 : : /*
440 : : * Init the memzone subsystem
441 : : */
442 : : int
443 : 183 : rte_eal_memzone_init(void)
444 : : {
445 : : struct rte_mem_config *mcfg;
446 : : int ret = 0;
447 : :
448 : : /* get pointer to global configuration */
449 : 183 : mcfg = rte_eal_get_configuration()->mem_config;
450 : :
451 : 183 : rte_rwlock_write_lock(&mcfg->mlock);
452 : :
453 [ + + - + ]: 339 : if (rte_eal_process_type() == RTE_PROC_PRIMARY &&
454 : 156 : rte_fbarray_init(&mcfg->memzones, "memzone",
455 : 156 : rte_memzone_max_get(), sizeof(struct rte_memzone))) {
456 : 0 : EAL_LOG(ERR, "Cannot allocate memzone list");
457 : 0 : ret = -1;
458 [ + + - + ]: 210 : } else if (rte_eal_process_type() == RTE_PROC_SECONDARY &&
459 : 27 : rte_fbarray_attach(&mcfg->memzones)) {
460 : 0 : EAL_LOG(ERR, "Cannot attach to memzone list");
461 : : ret = -1;
462 : : }
463 : :
464 : : rte_rwlock_write_unlock(&mcfg->mlock);
465 : :
466 : 183 : return ret;
467 : : }
468 : :
469 : : /* Walk all reserved memory zones */
470 : : RTE_EXPORT_SYMBOL(rte_memzone_walk)
471 : 6 : void rte_memzone_walk(void (*func)(const struct rte_memzone *, void *),
472 : : void *arg)
473 : : {
474 : : struct rte_mem_config *mcfg;
475 : : struct rte_fbarray *arr;
476 : : int i;
477 : :
478 : 6 : mcfg = rte_eal_get_configuration()->mem_config;
479 : 6 : arr = &mcfg->memzones;
480 : :
481 : 6 : rte_rwlock_read_lock(&mcfg->mlock);
482 : 6 : i = rte_fbarray_find_next_used(arr, 0);
483 [ + + ]: 20 : while (i >= 0) {
484 : 14 : struct rte_memzone *mz = rte_fbarray_get(arr, i);
485 : 14 : (*func)(mz, arg);
486 : 14 : i = rte_fbarray_find_next_used(arr, i + 1);
487 : : }
488 : : rte_rwlock_read_unlock(&mcfg->mlock);
489 : 6 : }
|