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_eal_paging.h>
17 #include <rte_errno.h>
18 #include <rte_malloc.h>
20 #include <rte_string_fns.h>
24 #define EXTERNAL_MEM_SZ (RTE_PGSIZE_4K << 10) /* 4M of data */
27 check_mem(void *addr, rte_iova_t *iova, size_t pgsz, int n_pages)
31 /* check that we can get this memory from EAL now */
32 for (i = 0; i < n_pages; i++) {
33 const struct rte_memseg_list *msl;
34 const struct rte_memseg *ms;
35 void *cur = RTE_PTR_ADD(addr, pgsz * i);
36 rte_iova_t expected_iova;
38 msl = rte_mem_virt2memseg_list(cur);
40 printf("%s():%i: Memseg list is not marked as external\n",
45 ms = rte_mem_virt2memseg(cur, msl);
47 printf("%s():%i: Failed to retrieve memseg for external mem\n",
51 if (ms->addr != cur) {
52 printf("%s():%i: VA mismatch\n", __func__, __LINE__);
55 expected_iova = (iova == NULL) ? RTE_BAD_IOVA : iova[i];
56 if (ms->iova != expected_iova) {
57 printf("%s():%i: IOVA mismatch\n", __func__, __LINE__);
65 test_malloc_invalid_param(void *addr, size_t len, size_t pgsz, rte_iova_t *iova,
68 static const char * const names[] = {
71 "this heap name is definitely way too long to be valid"
73 const char *valid_name = "valid heap name";
76 /* check invalid name handling */
77 for (i = 0; i < RTE_DIM(names); i++) {
78 const char *name = names[i];
80 /* these calls may fail for other reasons, so check errno */
81 if (rte_malloc_heap_create(name) >= 0 || rte_errno != EINVAL) {
82 printf("%s():%i: Created heap with invalid name\n",
87 if (rte_malloc_heap_destroy(name) >= 0 || rte_errno != EINVAL) {
88 printf("%s():%i: Destroyed heap with invalid name\n",
93 if (rte_malloc_heap_get_socket(name) >= 0 ||
94 rte_errno != EINVAL) {
95 printf("%s():%i: Found socket for heap with invalid name\n",
100 if (rte_malloc_heap_memory_add(name, addr, len,
101 NULL, 0, pgsz) >= 0 || rte_errno != EINVAL) {
102 printf("%s():%i: Added memory to heap with invalid name\n",
106 if (rte_malloc_heap_memory_remove(name, addr, len) >= 0 ||
107 rte_errno != EINVAL) {
108 printf("%s():%i: Removed memory from heap with invalid name\n",
113 if (rte_malloc_heap_memory_attach(name, addr, len) >= 0 ||
114 rte_errno != EINVAL) {
115 printf("%s():%i: Attached memory to heap with invalid name\n",
119 if (rte_malloc_heap_memory_detach(name, addr, len) >= 0 ||
120 rte_errno != EINVAL) {
121 printf("%s():%i: Detached memory from heap with invalid name\n",
127 /* do same as above, but with a valid heap name */
129 /* skip create call */
130 if (rte_malloc_heap_destroy(valid_name) >= 0 || rte_errno != ENOENT) {
131 printf("%s():%i: Destroyed heap with invalid name\n",
135 if (rte_malloc_heap_get_socket(valid_name) >= 0 ||
136 rte_errno != ENOENT) {
137 printf("%s():%i: Found socket for heap with invalid name\n",
142 /* these calls may fail for other reasons, so check errno */
143 if (rte_malloc_heap_memory_add(valid_name, addr, len,
144 NULL, 0, pgsz) >= 0 || rte_errno != ENOENT) {
145 printf("%s():%i: Added memory to non-existent heap\n",
149 if (rte_malloc_heap_memory_remove(valid_name, addr, len) >= 0 ||
150 rte_errno != ENOENT) {
151 printf("%s():%i: Removed memory from non-existent heap\n",
156 if (rte_malloc_heap_memory_attach(valid_name, addr, len) >= 0 ||
157 rte_errno != ENOENT) {
158 printf("%s():%i: Attached memory to non-existent heap\n",
162 if (rte_malloc_heap_memory_detach(valid_name, addr, len) >= 0 ||
163 rte_errno != ENOENT) {
164 printf("%s():%i: Detached memory from non-existent heap\n",
169 /* create a valid heap but test other invalid parameters */
170 if (rte_malloc_heap_create(valid_name) != 0) {
171 printf("%s():%i: Failed to create valid heap\n",
177 if (rte_malloc_heap_memory_add(valid_name, addr, 0,
178 NULL, 0, pgsz) >= 0 || rte_errno != EINVAL) {
179 printf("%s():%i: Added memory with invalid parameters\n",
184 if (rte_malloc_heap_memory_remove(valid_name, addr, 0) >= 0 ||
185 rte_errno != EINVAL) {
186 printf("%s():%i: Removed memory with invalid parameters\n",
191 if (rte_malloc_heap_memory_attach(valid_name, addr, 0) >= 0 ||
192 rte_errno != EINVAL) {
193 printf("%s():%i: Attached memory with invalid parameters\n",
197 if (rte_malloc_heap_memory_detach(valid_name, addr, 0) >= 0 ||
198 rte_errno != EINVAL) {
199 printf("%s():%i: Detached memory with invalid parameters\n",
205 if (rte_malloc_heap_memory_add(valid_name, NULL, len,
206 NULL, 0, pgsz) >= 0 || rte_errno != EINVAL) {
207 printf("%s():%i: Added memory with invalid parameters\n",
212 if (rte_malloc_heap_memory_remove(valid_name, NULL, len) >= 0 ||
213 rte_errno != EINVAL) {
214 printf("%s():%i: Removed memory with invalid parameters\n",
219 if (rte_malloc_heap_memory_attach(valid_name, NULL, len) >= 0 ||
220 rte_errno != EINVAL) {
221 printf("%s():%i: Attached memory with invalid parameters\n",
225 if (rte_malloc_heap_memory_detach(valid_name, NULL, len) >= 0 ||
226 rte_errno != EINVAL) {
227 printf("%s():%i: Detached memory with invalid parameters\n",
232 /* the following tests are only valid if IOVA table is not NULL */
234 /* wrong page count */
235 if (rte_malloc_heap_memory_add(valid_name, addr, len,
236 iova, 0, pgsz) >= 0 || rte_errno != EINVAL) {
237 printf("%s():%i: Added memory with invalid parameters\n",
241 if (rte_malloc_heap_memory_add(valid_name, addr, len,
242 iova, n_pages - 1, pgsz) >= 0 ||
243 rte_errno != EINVAL) {
244 printf("%s():%i: Added memory with invalid parameters\n",
248 if (rte_malloc_heap_memory_add(valid_name, addr, len,
249 iova, n_pages + 1, pgsz) >= 0 ||
250 rte_errno != EINVAL) {
251 printf("%s():%i: Added memory with invalid parameters\n",
257 /* tests passed, destroy heap */
258 if (rte_malloc_heap_destroy(valid_name) != 0) {
259 printf("%s():%i: Failed to destroy valid heap\n",
265 rte_malloc_heap_destroy(valid_name);
270 test_malloc_basic(void *addr, size_t len, size_t pgsz, rte_iova_t *iova,
273 const char *heap_name = "heap";
276 const struct rte_memzone *mz = NULL, *contig_mz = NULL;
279 if (rte_malloc_heap_create(heap_name) != 0) {
280 printf("%s():%i: Failed to create malloc heap\n",
285 /* get socket ID corresponding to this heap */
286 socket_id = rte_malloc_heap_get_socket(heap_name);
288 printf("%s():%i: cannot find socket for external heap\n",
293 /* heap is empty, so any allocation should fail */
294 ptr = rte_malloc_socket("EXTMEM", 64, 0, socket_id);
296 printf("%s():%i: Allocated from empty heap\n", __func__,
301 /* add memory to heap */
302 if (rte_malloc_heap_memory_add(heap_name, addr, len,
303 iova, n_pages, pgsz) != 0) {
304 printf("%s():%i: Failed to add memory to heap\n",
309 /* check if memory is accessible from EAL */
310 if (check_mem(addr, iova, pgsz, n_pages) < 0)
313 /* allocate - this now should succeed */
314 ptr = rte_malloc_socket("EXTMEM", 64, 0, socket_id);
316 printf("%s():%i: Failed to allocate from external heap\n",
321 /* check if address is in expected range */
322 if (ptr < addr || ptr >= RTE_PTR_ADD(addr, len)) {
323 printf("%s():%i: Allocated from unexpected address space\n",
328 /* we've allocated something - removing memory should fail */
329 if (rte_malloc_heap_memory_remove(heap_name, addr, len) >= 0 ||
330 rte_errno != EBUSY) {
331 printf("%s():%i: Removing memory succeeded when memory is not free\n",
335 if (rte_malloc_heap_destroy(heap_name) >= 0 || rte_errno != EBUSY) {
336 printf("%s():%i: Destroying heap succeeded when memory is not free\n",
341 /* try allocating a memzone */
342 mz = rte_memzone_reserve("heap_test", pgsz * 2, socket_id, 0);
344 printf("%s():%i: Failed to reserve memzone\n",
348 /* try allocating an IOVA-contiguous memzone - this should succeed
349 * if we've set up a contiguous IOVA table, and fail if we haven't.
351 contig_mz = rte_memzone_reserve("heap_test_contig", pgsz * 2, socket_id,
352 RTE_MEMZONE_IOVA_CONTIG);
353 if ((iova == NULL) != (contig_mz == NULL)) {
354 printf("%s():%i: Failed to reserve memzone\n",
359 rte_malloc_dump_stats(stdout, NULL);
360 rte_malloc_dump_heaps(stdout);
362 /* free memory - removing it should now succeed */
366 rte_memzone_free(mz);
368 rte_memzone_free(contig_mz);
371 if (rte_malloc_heap_memory_remove(heap_name, addr, len) != 0) {
372 printf("%s():%i: Removing memory from heap failed\n",
376 if (rte_malloc_heap_destroy(heap_name) != 0) {
377 printf("%s():%i: Destroying heap failed\n",
384 rte_memzone_free(contig_mz);
385 rte_memzone_free(mz);
387 /* even if something failed, attempt to clean up */
388 rte_malloc_heap_memory_remove(heap_name, addr, len);
389 rte_malloc_heap_destroy(heap_name);
395 test_extmem_invalid_param(void *addr, size_t len, size_t pgsz, rte_iova_t *iova,
398 /* these calls may fail for other reasons, so check errno */
399 if (rte_extmem_unregister(addr, len) >= 0 ||
400 rte_errno != ENOENT) {
401 printf("%s():%i: Unregistered non-existent memory\n",
406 if (rte_extmem_attach(addr, len) >= 0 ||
407 rte_errno != ENOENT) {
408 printf("%s():%i: Attached to non-existent memory\n",
412 if (rte_extmem_attach(addr, len) >= 0 ||
413 rte_errno != ENOENT) {
414 printf("%s():%i: Detached from non-existent memory\n",
420 if (rte_extmem_register(addr, 0, NULL, 0, pgsz) >= 0 ||
421 rte_errno != EINVAL) {
422 printf("%s():%i: Registered memory with invalid parameters\n",
427 if (rte_extmem_unregister(addr, 0) >= 0 ||
428 rte_errno != EINVAL) {
429 printf("%s():%i: Unregistered memory with invalid parameters\n",
434 if (rte_extmem_attach(addr, 0) >= 0 ||
435 rte_errno != EINVAL) {
436 printf("%s():%i: Attached memory with invalid parameters\n",
440 if (rte_extmem_attach(addr, 0) >= 0 ||
441 rte_errno != EINVAL) {
442 printf("%s():%i: Detached memory with invalid parameters\n",
448 if (rte_extmem_register(NULL, len, NULL, 0, pgsz) >= 0 ||
449 rte_errno != EINVAL) {
450 printf("%s():%i: Registered memory with invalid parameters\n",
455 if (rte_extmem_unregister(NULL, len) >= 0 ||
456 rte_errno != EINVAL) {
457 printf("%s():%i: Unregistered memory with invalid parameters\n",
462 if (rte_extmem_attach(NULL, len) >= 0 ||
463 rte_errno != EINVAL) {
464 printf("%s():%i: Attached memory with invalid parameters\n",
468 if (rte_extmem_attach(NULL, len) >= 0 ||
469 rte_errno != EINVAL) {
470 printf("%s():%i: Detached memory with invalid parameters\n",
475 /* the following tests are only valid if IOVA table is not NULL */
477 /* wrong page count */
478 if (rte_extmem_register(addr, len,
479 iova, 0, pgsz) >= 0 || rte_errno != EINVAL) {
480 printf("%s():%i: Registered memory with invalid parameters\n",
484 if (rte_extmem_register(addr, len,
485 iova, n_pages - 1, pgsz) >= 0 ||
486 rte_errno != EINVAL) {
487 printf("%s():%i: Registered memory with invalid parameters\n",
491 if (rte_extmem_register(addr, len,
492 iova, n_pages + 1, pgsz) >= 0 ||
493 rte_errno != EINVAL) {
494 printf("%s():%i: Registered memory with invalid parameters\n",
504 test_extmem_basic(void *addr, size_t len, size_t pgsz, rte_iova_t *iova,
507 /* register memory */
508 if (rte_extmem_register(addr, len, iova, n_pages, pgsz) != 0) {
509 printf("%s():%i: Failed to register memory\n",
514 /* check if memory is accessible from EAL */
515 if (check_mem(addr, iova, pgsz, n_pages) < 0)
518 if (rte_extmem_unregister(addr, len) != 0) {
519 printf("%s():%i: Removing memory from heap failed\n",
526 /* even if something failed, attempt to clean up */
527 rte_extmem_unregister(addr, len);
532 /* we need to test attach/detach in secondary processes. */
534 test_external_mem(void)
536 size_t pgsz = rte_mem_page_size();
537 size_t len = EXTERNAL_MEM_SZ;
538 rte_iova_t iova[len / pgsz];
543 /* create external memory area */
544 n_pages = RTE_DIM(iova);
545 addr = mmap(NULL, len, PROT_WRITE | PROT_READ,
546 MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
547 if (addr == MAP_FAILED) {
548 printf("%s():%i: Failed to create dummy memory area\n",
552 for (i = 0; i < n_pages; i++) {
554 rte_iova_t tmp = 0x100000000 + i * pgsz;
558 /* test external heap memory */
559 ret = test_malloc_invalid_param(addr, len, pgsz, iova, n_pages);
560 ret |= test_malloc_basic(addr, len, pgsz, iova, n_pages);
561 /* when iova table is NULL, everything should still work */
562 ret |= test_malloc_invalid_param(addr, len, pgsz, NULL, n_pages);
563 ret |= test_malloc_basic(addr, len, pgsz, NULL, n_pages);
565 /* test non-heap memory */
566 ret |= test_extmem_invalid_param(addr, len, pgsz, iova, n_pages);
567 ret |= test_extmem_basic(addr, len, pgsz, iova, n_pages);
568 /* when iova table is NULL, everything should still work */
569 ret |= test_extmem_invalid_param(addr, len, pgsz, NULL, n_pages);
570 ret |= test_extmem_basic(addr, len, pgsz, NULL, n_pages);
577 REGISTER_TEST_COMMAND(external_mem_autotest, test_external_mem);