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