Skip to content

Commit ac91071

Browse files
lyakhkv2019i
authored andcommitted
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 <guennadi.liakhovetski@linux.intel.com>
1 parent 1ba94cf commit ac91071

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

src/include/sof/coherent.h

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,13 @@ static inline void coherent_release(struct coherent __sparse_cache *c,
156156

157157
static inline void *__coherent_init(size_t offset, const size_t size)
158158
{
159-
void *object = rzalloc(SOF_MEM_ZONE_RUNTIME_SHARED, 0, SOF_MEM_CAPS_RAM, size);
159+
/*
160+
* Allocate an object with an uncached alias but align size on a cache-
161+
* line boundary to avoid sharing a cache line with the adjacent
162+
* allocation
163+
*/
164+
void *object = rzalloc(SOF_MEM_ZONE_RUNTIME_SHARED, 0, SOF_MEM_CAPS_RAM,
165+
ALIGN_UP(size, PLATFORM_DCACHE_ALIGN));
160166
struct coherent *c;
161167

162168
if (!object)
@@ -245,7 +251,9 @@ static inline void coherent_release_thread(struct coherent __sparse_cache *c,
245251

246252
static inline void *__coherent_init_thread(size_t offset, const size_t size)
247253
{
248-
void *object = rzalloc(SOF_MEM_ZONE_RUNTIME_SHARED, 0, SOF_MEM_CAPS_RAM, size);
254+
/* As above - prevent cache line sharing */
255+
void *object = rzalloc(SOF_MEM_ZONE_RUNTIME_SHARED, 0, SOF_MEM_CAPS_RAM,
256+
ALIGN_UP(size, PLATFORM_DCACHE_ALIGN));
249257
struct coherent *c;
250258

251259
if (!object)
@@ -331,7 +339,9 @@ static inline void coherent_release(struct coherent __sparse_cache *c,
331339

332340
static inline void *__coherent_init(size_t offset, const size_t size)
333341
{
334-
void *object = rzalloc(SOF_MEM_ZONE_RUNTIME_SHARED, 0, SOF_MEM_CAPS_RAM, size);
342+
/* As in CONFIG_INCOHERENT case - prevent cache line sharing */
343+
void *object = rzalloc(SOF_MEM_ZONE_RUNTIME_SHARED, 0, SOF_MEM_CAPS_RAM,
344+
ALIGN_UP(size, PLATFORM_DCACHE_ALIGN));
335345
struct coherent *c;
336346

337347
if (!object)
@@ -394,11 +404,7 @@ static inline void coherent_release_thread(struct coherent __sparse_cache *c,
394404

395405
static inline void *__coherent_init_thread(size_t offset, const size_t size)
396406
{
397-
/*
398-
* Allocate an object with an uncached alias but align size on a cache-
399-
* line boundary to avoid sharing a cache line with the adjacent
400-
* allocation
401-
*/
407+
/* As above - prevent cache line sharing */
402408
void *object = rzalloc(SOF_MEM_ZONE_RUNTIME_SHARED, 0, SOF_MEM_CAPS_RAM,
403409
ALIGN_UP(size, PLATFORM_DCACHE_ALIGN));
404410
struct coherent *c;

0 commit comments

Comments
 (0)