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