Get physical address of any rte_malloc allocated buffer using function rte_malloc_virt2phy(addr). The rte_memzone pointer is now stored in each allocated memory block header to allow simple computation of physical address of a block using the memzone it comes from. Declaration of memzone in malloc_elem structure adds a dependency between rte_malloc.h and rte_memory.h; test source code are modified to include both files in correct order.
Signed-off-by: Didier Pallard <didier.pallard at 6wind.com> --- app/test/test_hash.c | 2 +- app/test/test_hash_perf.c | 2 +- app/test/test_memcpy.c | 1 + app/test/test_memcpy_perf.c | 1 + lib/librte_malloc/malloc_elem.c | 7 ++++--- lib/librte_malloc/malloc_elem.h | 2 ++ lib/librte_malloc/malloc_heap.c | 2 +- lib/librte_malloc/rte_malloc.c | 12 ++++++++++++ lib/librte_malloc/rte_malloc.h | 13 +++++++++++++ 9 files changed, 36 insertions(+), 6 deletions(-) diff --git a/app/test/test_hash.c b/app/test/test_hash.c index 5437e05..4de8cb1 100644 --- a/app/test/test_hash.c +++ b/app/test/test_hash.c @@ -41,10 +41,10 @@ #include <sys/queue.h> #include <rte_common.h> -#include <rte_malloc.h> #include <rte_cycles.h> #include <rte_random.h> #include <rte_memory.h> +#include <rte_malloc.h> #include <rte_memzone.h> #include <rte_tailq.h> #include <rte_eal.h> diff --git a/app/test/test_hash_perf.c b/app/test/test_hash_perf.c index 8a0feb3..311c2bd 100644 --- a/app/test/test_hash_perf.c +++ b/app/test/test_hash_perf.c @@ -42,10 +42,10 @@ #include <rte_common.h> #include <rte_lcore.h> -#include <rte_malloc.h> #include <rte_cycles.h> #include <rte_random.h> #include <rte_memory.h> +#include <rte_malloc.h> #include <rte_memzone.h> #include <rte_tailq.h> #include <rte_eal.h> diff --git a/app/test/test_memcpy.c b/app/test/test_memcpy.c index d7777a8..729a2ff 100644 --- a/app/test/test_memcpy.c +++ b/app/test/test_memcpy.c @@ -41,6 +41,7 @@ #include <cmdline_parse.h> #include <rte_cycles.h> #include <rte_random.h> +#include <rte_memory.h> #include <rte_malloc.h> #include <rte_memcpy.h> diff --git a/app/test/test_memcpy_perf.c b/app/test/test_memcpy_perf.c index 236b295..1e75f53 100644 --- a/app/test/test_memcpy_perf.c +++ b/app/test/test_memcpy_perf.c @@ -41,6 +41,7 @@ #include <cmdline_parse.h> #include <rte_cycles.h> #include <rte_random.h> +#include <rte_memory.h> #include <rte_malloc.h> #include <rte_memcpy.h> diff --git a/lib/librte_malloc/malloc_elem.c b/lib/librte_malloc/malloc_elem.c index 919d474..8da517f 100644 --- a/lib/librte_malloc/malloc_elem.c +++ b/lib/librte_malloc/malloc_elem.c @@ -57,9 +57,10 @@ */ void malloc_elem_init(struct malloc_elem *elem, - struct malloc_heap *heap, size_t size) + struct malloc_heap *heap, const struct rte_memzone *mz, size_t size) { elem->heap = heap; + elem->mz = mz; elem->prev = elem->next_free = NULL; elem->state = ELEM_FREE; elem->size = size; @@ -74,7 +75,7 @@ malloc_elem_init(struct malloc_elem *elem, void malloc_elem_mkend(struct malloc_elem *elem, struct malloc_elem *prev) { - malloc_elem_init(elem, prev->heap, 0); + malloc_elem_init(elem, prev->heap, prev->mz, 0); elem->prev = prev; elem->state = ELEM_BUSY; /* mark busy so its never merged */ } @@ -117,7 +118,7 @@ split_elem(struct malloc_elem *elem, struct malloc_elem *split_pt) const unsigned old_elem_size = (uintptr_t)split_pt - (uintptr_t)elem; const unsigned new_elem_size = elem->size - old_elem_size; - malloc_elem_init(split_pt, elem->heap, new_elem_size); + malloc_elem_init(split_pt, elem->heap, elem->mz, new_elem_size); split_pt->prev = elem; next_elem->prev = split_pt; elem->size = old_elem_size; diff --git a/lib/librte_malloc/malloc_elem.h b/lib/librte_malloc/malloc_elem.h index 8a75e0c..f1710cf 100644 --- a/lib/librte_malloc/malloc_elem.h +++ b/lib/librte_malloc/malloc_elem.h @@ -48,6 +48,7 @@ struct malloc_elem { struct malloc_heap *heap; struct malloc_elem *volatile prev; /* points to prev elem in memzone */ struct malloc_elem *volatile next_free; /* to make list of free elements */ + const struct rte_memzone *mz; volatile enum elem_state state; uint32_t pad; size_t size; @@ -134,6 +135,7 @@ malloc_elem_from_data(void *data) void malloc_elem_init(struct malloc_elem *elem, struct malloc_heap *heap, + const struct rte_memzone *mz, size_t size); /* diff --git a/lib/librte_malloc/malloc_heap.c b/lib/librte_malloc/malloc_heap.c index b6d83a4..bbf6bb8 100644 --- a/lib/librte_malloc/malloc_heap.c +++ b/lib/librte_malloc/malloc_heap.c @@ -100,7 +100,7 @@ malloc_heap_add_memzone(struct malloc_heap *heap, size_t size, unsigned align) end_elem = RTE_PTR_ALIGN_FLOOR(end_elem, CACHE_LINE_SIZE); const unsigned elem_size = (uintptr_t)end_elem - (uintptr_t)start_elem; - malloc_elem_init(start_elem, heap, elem_size); + malloc_elem_init(start_elem, heap, mz, elem_size); malloc_elem_mkend(end_elem, start_elem); start_elem->next_free = heap->free_head; diff --git a/lib/librte_malloc/rte_malloc.c b/lib/librte_malloc/rte_malloc.c index 03db51f..3c76c43 100644 --- a/lib/librte_malloc/rte_malloc.c +++ b/lib/librte_malloc/rte_malloc.c @@ -228,3 +228,15 @@ rte_malloc_set_limit(__rte_unused const char *type, { return 0; } + +/* + * Return the physical address of a virtual address obtained through rte_malloc + */ +phys_addr_t +rte_malloc_virt2phy(const void *addr) +{ + const struct malloc_elem *elem = malloc_elem_from_data(addr); + if (elem == NULL) + return 0; + return elem->mz->phys_addr + ((uintptr_t)addr - (uintptr_t)elem->mz->addr); +} diff --git a/lib/librte_malloc/rte_malloc.h b/lib/librte_malloc/rte_malloc.h index 64ffaf0..1451d9c 100644 --- a/lib/librte_malloc/rte_malloc.h +++ b/lib/librte_malloc/rte_malloc.h @@ -319,6 +319,19 @@ rte_malloc_dump_stats(const char *type); int rte_malloc_set_limit(const char *type, size_t max); +/** + * Return the physical address of a virtual address obtained through + * rte_malloc + * + * @param addr + * Adress obtained from a previous rte_malloc call + * @return + * NULL on error + * otherwise return physical address of the buffer + */ +phys_addr_t +rte_malloc_virt2phy(const void *addr); + #ifdef __cplusplus } #endif -- 1.7.10.4