1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018 Intel Corporation
13 #include <rte_common.h>
14 #include <rte_debug.h>
16 #include <rte_errno.h>
17 #include <rte_malloc.h>
19 #include <rte_string_fns.h>
23 #define EXTERNAL_MEM_SZ (RTE_PGSIZE_4K << 10) /* 4M of data */
26 check_mem(void *addr, rte_iova_t *iova, size_t pgsz, int n_pages)
30 /* check that we can get this memory from EAL now */
31 for (i = 0; i < n_pages; i++) {
32 const struct rte_memseg *ms;
33 void *cur = RTE_PTR_ADD(addr, pgsz * i);
35 ms = rte_mem_virt2memseg(cur, NULL);
37 printf("%s():%i: Failed to retrieve memseg for external mem\n",
41 if (ms->addr != cur) {
42 printf("%s():%i: VA mismatch\n", __func__, __LINE__);
45 if (ms->iova != iova[i]) {
46 printf("%s():%i: IOVA mismatch\n", __func__, __LINE__);
54 test_malloc_invalid_param(void *addr, size_t len, size_t pgsz, rte_iova_t *iova,
57 static const char * const names[] = {
60 "this heap name is definitely way too long to be valid"
62 const char *valid_name = "valid heap name";
65 /* check invalid name handling */
66 for (i = 0; i < RTE_DIM(names); i++) {
67 const char *name = names[i];
69 /* these calls may fail for other reasons, so check errno */
70 if (rte_malloc_heap_create(name) >= 0 || rte_errno != EINVAL) {
71 printf("%s():%i: Created heap with invalid name\n",
76 if (rte_malloc_heap_destroy(name) >= 0 || rte_errno != EINVAL) {
77 printf("%s():%i: Destroyed heap with invalid name\n",
82 if (rte_malloc_heap_get_socket(name) >= 0 ||
83 rte_errno != EINVAL) {
84 printf("%s():%i: Found socket for heap with invalid name\n",
89 if (rte_malloc_heap_memory_add(name, addr, len,
90 NULL, 0, pgsz) >= 0 || rte_errno != EINVAL) {
91 printf("%s():%i: Added memory to heap with invalid name\n",
95 if (rte_malloc_heap_memory_remove(name, addr, len) >= 0 ||
96 rte_errno != EINVAL) {
97 printf("%s():%i: Removed memory from heap with invalid name\n",
102 if (rte_malloc_heap_memory_attach(name, addr, len) >= 0 ||
103 rte_errno != EINVAL) {
104 printf("%s():%i: Attached memory to heap with invalid name\n",
108 if (rte_malloc_heap_memory_detach(name, addr, len) >= 0 ||
109 rte_errno != EINVAL) {
110 printf("%s():%i: Detached memory from heap with invalid name\n",
116 /* do same as above, but with a valid heap name */
118 /* skip create call */
119 if (rte_malloc_heap_destroy(valid_name) >= 0 || rte_errno != ENOENT) {
120 printf("%s():%i: Destroyed heap with invalid name\n",
124 if (rte_malloc_heap_get_socket(valid_name) >= 0 ||
125 rte_errno != ENOENT) {
126 printf("%s():%i: Found socket for heap with invalid name\n",
131 /* these calls may fail for other reasons, so check errno */
132 if (rte_malloc_heap_memory_add(valid_name, addr, len,
133 NULL, 0, pgsz) >= 0 || rte_errno != ENOENT) {
134 printf("%s():%i: Added memory to non-existent heap\n",
138 if (rte_malloc_heap_memory_remove(valid_name, addr, len) >= 0 ||
139 rte_errno != ENOENT) {
140 printf("%s():%i: Removed memory from non-existent heap\n",
145 if (rte_malloc_heap_memory_attach(valid_name, addr, len) >= 0 ||
146 rte_errno != ENOENT) {
147 printf("%s():%i: Attached memory to non-existent heap\n",
151 if (rte_malloc_heap_memory_detach(valid_name, addr, len) >= 0 ||
152 rte_errno != ENOENT) {
153 printf("%s():%i: Detached memory from non-existent heap\n",
158 /* create a valid heap but test other invalid parameters */
159 if (rte_malloc_heap_create(valid_name) != 0) {
160 printf("%s():%i: Failed to create valid heap\n",
166 if (rte_malloc_heap_memory_add(valid_name, addr, 0,
167 NULL, 0, pgsz) >= 0 || rte_errno != EINVAL) {
168 printf("%s():%i: Added memory with invalid parameters\n",
173 if (rte_malloc_heap_memory_remove(valid_name, addr, 0) >= 0 ||
174 rte_errno != EINVAL) {
175 printf("%s():%i: Removed memory with invalid parameters\n",
180 if (rte_malloc_heap_memory_attach(valid_name, addr, 0) >= 0 ||
181 rte_errno != EINVAL) {
182 printf("%s():%i: Attached memory with invalid parameters\n",
186 if (rte_malloc_heap_memory_detach(valid_name, addr, 0) >= 0 ||
187 rte_errno != EINVAL) {
188 printf("%s():%i: Detached memory with invalid parameters\n",
194 if (rte_malloc_heap_memory_add(valid_name, NULL, len,
195 NULL, 0, pgsz) >= 0 || rte_errno != EINVAL) {
196 printf("%s():%i: Added memory with invalid parameters\n",
201 if (rte_malloc_heap_memory_remove(valid_name, NULL, len) >= 0 ||
202 rte_errno != EINVAL) {
203 printf("%s():%i: Removed memory with invalid parameters\n",
208 if (rte_malloc_heap_memory_attach(valid_name, NULL, len) >= 0 ||
209 rte_errno != EINVAL) {
210 printf("%s():%i: Attached memory with invalid parameters\n",
214 if (rte_malloc_heap_memory_detach(valid_name, NULL, len) >= 0 ||
215 rte_errno != EINVAL) {
216 printf("%s():%i: Detached memory with invalid parameters\n",
221 /* wrong page count */
222 if (rte_malloc_heap_memory_add(valid_name, addr, len,
223 iova, 0, pgsz) >= 0 || rte_errno != EINVAL) {
224 printf("%s():%i: Added memory with invalid parameters\n",
228 if (rte_malloc_heap_memory_add(valid_name, addr, len,
229 iova, n_pages - 1, pgsz) >= 0 || rte_errno != EINVAL) {
230 printf("%s():%i: Added memory with invalid parameters\n",
234 if (rte_malloc_heap_memory_add(valid_name, addr, len,
235 iova, n_pages + 1, pgsz) >= 0 || rte_errno != EINVAL) {
236 printf("%s():%i: Added memory with invalid parameters\n",
241 /* tests passed, destroy heap */
242 if (rte_malloc_heap_destroy(valid_name) != 0) {
243 printf("%s():%i: Failed to destroy valid heap\n",
249 rte_malloc_heap_destroy(valid_name);
254 test_malloc_basic(void *addr, size_t len, size_t pgsz, rte_iova_t *iova,
257 const char *heap_name = "heap";
260 const struct rte_memzone *mz = NULL;
263 if (rte_malloc_heap_create(heap_name) != 0) {
264 printf("%s():%i: Failed to create malloc heap\n",
269 /* get socket ID corresponding to this heap */
270 socket_id = rte_malloc_heap_get_socket(heap_name);
272 printf("%s():%i: cannot find socket for external heap\n",
277 /* heap is empty, so any allocation should fail */
278 ptr = rte_malloc_socket("EXTMEM", 64, 0, socket_id);
280 printf("%s():%i: Allocated from empty heap\n", __func__,
285 /* add memory to heap */
286 if (rte_malloc_heap_memory_add(heap_name, addr, len,
287 iova, n_pages, pgsz) != 0) {
288 printf("%s():%i: Failed to add memory to heap\n",
293 /* check if memory is accessible from EAL */
294 if (check_mem(addr, iova, pgsz, n_pages) < 0)
297 /* allocate - this now should succeed */
298 ptr = rte_malloc_socket("EXTMEM", 64, 0, socket_id);
300 printf("%s():%i: Failed to allocate from external heap\n",
305 /* check if address is in expected range */
306 if (ptr < addr || ptr >= RTE_PTR_ADD(addr, len)) {
307 printf("%s():%i: Allocated from unexpected address space\n",
312 /* we've allocated something - removing memory should fail */
313 if (rte_malloc_heap_memory_remove(heap_name, addr, len) >= 0 ||
314 rte_errno != EBUSY) {
315 printf("%s():%i: Removing memory succeeded when memory is not free\n",
319 if (rte_malloc_heap_destroy(heap_name) >= 0 || rte_errno != EBUSY) {
320 printf("%s():%i: Destroying heap succeeded when memory is not free\n",
325 /* try allocating an IOVA-contiguous memzone - this should succeed
326 * because we've set up a contiguous IOVA table.
328 mz = rte_memzone_reserve("heap_test", pgsz * 2, socket_id,
329 RTE_MEMZONE_IOVA_CONTIG);
331 printf("%s():%i: Failed to reserve memzone\n",
336 rte_malloc_dump_stats(stdout, NULL);
337 rte_malloc_dump_heaps(stdout);
339 /* free memory - removing it should now succeed */
343 rte_memzone_free(mz);
346 if (rte_malloc_heap_memory_remove(heap_name, addr, len) != 0) {
347 printf("%s():%i: Removing memory from heap failed\n",
351 if (rte_malloc_heap_destroy(heap_name) != 0) {
352 printf("%s():%i: Destroying heap failed\n",
359 rte_memzone_free(mz);
361 /* even if something failed, attempt to clean up */
362 rte_malloc_heap_memory_remove(heap_name, addr, len);
363 rte_malloc_heap_destroy(heap_name);
368 /* we need to test attach/detach in secondary processes. */
370 test_external_mem(void)
372 size_t len = EXTERNAL_MEM_SZ;
373 size_t pgsz = RTE_PGSIZE_4K;
374 rte_iova_t iova[len / pgsz];
379 /* create external memory area */
380 n_pages = RTE_DIM(iova);
381 addr = mmap(NULL, len, PROT_WRITE | PROT_READ,
382 MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
383 if (addr == MAP_FAILED) {
384 printf("%s():%i: Failed to create dummy memory area\n",
388 for (i = 0; i < n_pages; i++) {
390 rte_iova_t tmp = 0x100000000 + i * pgsz;
394 ret = test_malloc_invalid_param(addr, len, pgsz, iova, n_pages);
395 ret |= test_malloc_basic(addr, len, pgsz, iova, n_pages);
402 REGISTER_TEST_COMMAND(external_mem_autotest, test_external_mem);