Skip to content

Commit d6ca1a3

Browse files
zephyr: Fix Zephyr heap metadata corruption
Zephyr stores heap metadata just before each allocated chunk. So not only must the chunk start be aligned to cache line size, but the chunk size must also be aligned up to cache line size. This is to prevent metadata corruption in case an invalidate is called for a chunk -- it may corrupt metadata for the next chunk if it is located in the same cache line. Signed-off-by: Serhiy Katsyuba <serhiy.katsyuba@intel.com>
1 parent 8d44f26 commit d6ca1a3

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

zephyr/lib/alloc.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -471,10 +471,16 @@ void *rmalloc_align(uint32_t flags, size_t bytes, uint32_t alignment)
471471
ptr = (__sparse_force void *)heap_alloc_aligned_cached(heap, alignment, bytes);
472472
} else {
473473
/*
474-
* XTOS alloc implementation has used dcache alignment,
475-
* so SOF application code is expecting this behaviour.
474+
* Zephyr sys_heap stores metadata at start of each
475+
* heap allocation. To ensure no allocated cached buffer
476+
* overlaps the same cacheline with the metadata chunk,
477+
* align both allocation start and size of allocation
478+
* to cacheline. As cached and non-cached allocations are
479+
* mixed, same rules need to be followed for both type of
480+
* allocations.
476481
*/
477482
alignment = MAX(PLATFORM_DCACHE_ALIGN, alignment);
483+
bytes = ALIGN_UP(bytes, alignment);
478484
ptr = heap_alloc_aligned(heap, alignment, bytes);
479485
}
480486

0 commit comments

Comments
 (0)