From 62c5684ed6ac66ad829398a3a9ac32ca81224b91 Mon Sep 17 00:00:00 2001 From: Liam Girdwood Date: Mon, 29 Oct 2018 16:24:52 +0000 Subject: [PATCH] allocator: Improve rfree() debug and trace System heap is one time allocate only meant for init and not for freeing. Panic if someone attempts to free system heap. Provide pointers and cpu core ID for any failures in trace . Signed-off-by: Liam Girdwood --- src/lib/alloc.c | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/src/lib/alloc.c b/src/lib/alloc.c index e2150baef73d..be093cab23d7 100644 --- a/src/lib/alloc.c +++ b/src/lib/alloc.c @@ -327,13 +327,12 @@ static void free_block(void *ptr) int i; int block; - /* sanity check */ - if (ptr == NULL) - return; - heap = get_heap_from_ptr(ptr); - if (heap == NULL) + if (!heap) { + trace_error(TRACE_CLASS_MEM, "invalid heap %p cpu %d", + (uintptr_t)ptr, cpu_get_id()); return; + } /* find block that ptr belongs to */ for (i = 0; i < heap->blocks; i++) { @@ -346,7 +345,8 @@ static void free_block(void *ptr) } /* not found */ - trace_mem_error("eMF"); + trace_error(TRACE_CLASS_MEM, "invalid ptr %p cpu %d", + (uintptr_t)ptr, cpu_get_id()); return; found: @@ -540,8 +540,25 @@ void *rballoc(int zone, uint32_t caps, size_t bytes) void rfree(void *ptr) { + struct mm_heap *cpu_heap; uint32_t flags; + /* sanity check - NULL ptrs are fine */ + if (!ptr) + return; + + /* use the heap dedicated for the selected core */ + cpu_heap = cache_to_uncache(memmap.system + cpu_get_id()); + + /* panic if pointer is from system heap */ + if (ptr >= (void *)cpu_heap->heap && + ptr <= (void *)cpu_heap->heap + cpu_heap->size) { + trace_error(TRACE_CLASS_MEM, "attempt to free system heap %p cpu %d", + (uintptr_t)ptr, cpu_get_id()); + panic(SOF_IPC_PANIC_MEM); + } + + /* free the block */ spin_lock_irq(&memmap.lock, flags); free_block(ptr); spin_unlock_irq(&memmap.lock, flags);