From 2aeaf92fd44f35f5cf77c37e830a35866d4fe0c1 Mon Sep 17 00:00:00 2001 From: Guennadi Liakhovetski Date: Wed, 22 Mar 2023 12:29:34 +0100 Subject: [PATCH] coherent: fix allocation size When adding object allocation to the coherent API the size alignment was misplaced, re-opening a possibility for cache corruption when sharing cache lines with adjacent allocations. Add alignment to all coherent object allocations. Signed-off-by: Guennadi Liakhovetski --- src/include/sof/coherent.h | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/include/sof/coherent.h b/src/include/sof/coherent.h index 82840d933739..05eb67e482ad 100644 --- a/src/include/sof/coherent.h +++ b/src/include/sof/coherent.h @@ -156,7 +156,13 @@ static inline void coherent_release(struct coherent __sparse_cache *c, static inline void *__coherent_init(size_t offset, const size_t size) { - void *object = rzalloc(SOF_MEM_ZONE_RUNTIME_SHARED, 0, SOF_MEM_CAPS_RAM, size); + /* + * Allocate an object with an uncached alias but align size on a cache- + * line boundary to avoid sharing a cache line with the adjacent + * allocation + */ + void *object = rzalloc(SOF_MEM_ZONE_RUNTIME_SHARED, 0, SOF_MEM_CAPS_RAM, + ALIGN_UP(size, PLATFORM_DCACHE_ALIGN)); struct coherent *c; if (!object) @@ -245,7 +251,9 @@ static inline void coherent_release_thread(struct coherent __sparse_cache *c, static inline void *__coherent_init_thread(size_t offset, const size_t size) { - void *object = rzalloc(SOF_MEM_ZONE_RUNTIME_SHARED, 0, SOF_MEM_CAPS_RAM, size); + /* As above - prevent cache line sharing */ + void *object = rzalloc(SOF_MEM_ZONE_RUNTIME_SHARED, 0, SOF_MEM_CAPS_RAM, + ALIGN_UP(size, PLATFORM_DCACHE_ALIGN)); struct coherent *c; if (!object) @@ -331,7 +339,9 @@ static inline void coherent_release(struct coherent __sparse_cache *c, static inline void *__coherent_init(size_t offset, const size_t size) { - void *object = rzalloc(SOF_MEM_ZONE_RUNTIME_SHARED, 0, SOF_MEM_CAPS_RAM, size); + /* As in CONFIG_INCOHERENT case - prevent cache line sharing */ + void *object = rzalloc(SOF_MEM_ZONE_RUNTIME_SHARED, 0, SOF_MEM_CAPS_RAM, + ALIGN_UP(size, PLATFORM_DCACHE_ALIGN)); struct coherent *c; if (!object) @@ -394,11 +404,7 @@ static inline void coherent_release_thread(struct coherent __sparse_cache *c, static inline void *__coherent_init_thread(size_t offset, const size_t size) { - /* - * Allocate an object with an uncached alias but align size on a cache- - * line boundary to avoid sharing a cache line with the adjacent - * allocation - */ + /* As above - prevent cache line sharing */ void *object = rzalloc(SOF_MEM_ZONE_RUNTIME_SHARED, 0, SOF_MEM_CAPS_RAM, ALIGN_UP(size, PLATFORM_DCACHE_ALIGN)); struct coherent *c;