From c638e98b209dc4ad2ecc451640a0af99393b6c5f Mon Sep 17 00:00:00 2001 From: Marcin Szkudlinski Date: Wed, 22 Mar 2023 08:59:23 +0100 Subject: [PATCH] zephyr: all allocations from heap must be aligned to cacheline There was a possible problems witch cache coherency when allocating memory from "uncached" zone - i.e. with SOF_MEM_FLAG_COHERENT flag set. In that case heap_alloc_aligned was called directly and alignment was not set. if (zone_is_cached(zone) && !(flags & SOF_MEM_FLAG_COHERENT)) { ptr = (__sparse_force void *)heap_alloc_aligned_cached(heap, 0, bytes); } else { ptr = heap_alloc_aligned(heap, PLATFORM_DCACHE_ALIGN, bytes); } Any following cache invalidation on secondary cores may lead to issues. Signed-off-by: Marcin Szkudlinski --- zephyr/lib/alloc.c | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/zephyr/lib/alloc.c b/zephyr/lib/alloc.c index 881f3d936a1d..0a1e2e20ef25 100644 --- a/zephyr/lib/alloc.c +++ b/zephyr/lib/alloc.c @@ -154,6 +154,20 @@ static void *heap_alloc_aligned(struct k_heap *h, size_t min_align, size_t bytes struct sys_memory_stats stats; #endif + /* + * Zephyr sys_heap stores metadata at start of each + * heap allocation. To ensure no allocated cached buffer + * overlaps the same cacheline with the metadata chunk, + * align both allocation start and size of allocation + * to cacheline. As cached and non-cached allocations are + * mixed, same rules need to be followed for both type of + * allocations. + */ +#ifdef CONFIG_SOF_ZEPHYR_HEAP_CACHED + min_align = MAX(PLATFORM_DCACHE_ALIGN, min_align); + bytes = ALIGN_UP(bytes, min_align); +#endif + key = k_spin_lock(&h->lock); ret = sys_heap_aligned_alloc(&h->heap, min_align, bytes); k_spin_unlock(&h->lock, key); @@ -172,20 +186,6 @@ static void __sparse_cache *heap_alloc_aligned_cached(struct k_heap *h, { void __sparse_cache *ptr; - /* - * Zephyr sys_heap stores metadata at start of each - * heap allocation. To ensure no allocated cached buffer - * overlaps the same cacheline with the metadata chunk, - * align both allocation start and size of allocation - * to cacheline. As cached and non-cached allocations are - * mixed, same rules need to be followed for both type of - * allocations. - */ -#ifdef CONFIG_SOF_ZEPHYR_HEAP_CACHED - min_align = MAX(PLATFORM_DCACHE_ALIGN, min_align); - bytes = ALIGN_UP(bytes, min_align); -#endif - ptr = (__sparse_force void __sparse_cache *)heap_alloc_aligned(h, min_align, bytes); #ifdef CONFIG_SOF_ZEPHYR_HEAP_CACHED