Skip to content

Commit 62c5684

Browse files
committed
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 <liam.r.girdwood@linux.intel.com>
1 parent e101263 commit 62c5684

1 file changed

Lines changed: 23 additions & 6 deletions

File tree

src/lib/alloc.c

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -327,13 +327,12 @@ static void free_block(void *ptr)
327327
int i;
328328
int block;
329329

330-
/* sanity check */
331-
if (ptr == NULL)
332-
return;
333-
334330
heap = get_heap_from_ptr(ptr);
335-
if (heap == NULL)
331+
if (!heap) {
332+
trace_error(TRACE_CLASS_MEM, "invalid heap %p cpu %d",
333+
(uintptr_t)ptr, cpu_get_id());
336334
return;
335+
}
337336

338337
/* find block that ptr belongs to */
339338
for (i = 0; i < heap->blocks; i++) {
@@ -346,7 +345,8 @@ static void free_block(void *ptr)
346345
}
347346

348347
/* not found */
349-
trace_mem_error("eMF");
348+
trace_error(TRACE_CLASS_MEM, "invalid ptr %p cpu %d",
349+
(uintptr_t)ptr, cpu_get_id());
350350
return;
351351

352352
found:
@@ -540,8 +540,25 @@ void *rballoc(int zone, uint32_t caps, size_t bytes)
540540

541541
void rfree(void *ptr)
542542
{
543+
struct mm_heap *cpu_heap;
543544
uint32_t flags;
544545

546+
/* sanity check - NULL ptrs are fine */
547+
if (!ptr)
548+
return;
549+
550+
/* use the heap dedicated for the selected core */
551+
cpu_heap = cache_to_uncache(memmap.system + cpu_get_id());
552+
553+
/* panic if pointer is from system heap */
554+
if (ptr >= (void *)cpu_heap->heap &&
555+
ptr <= (void *)cpu_heap->heap + cpu_heap->size) {
556+
trace_error(TRACE_CLASS_MEM, "attempt to free system heap %p cpu %d",
557+
(uintptr_t)ptr, cpu_get_id());
558+
panic(SOF_IPC_PANIC_MEM);
559+
}
560+
561+
/* free the block */
545562
spin_lock_irq(&memmap.lock, flags);
546563
free_block(ptr);
547564
spin_unlock_irq(&memmap.lock, flags);

0 commit comments

Comments
 (0)