Skip to content

Commit 3e3d0cd

Browse files
lyakhkv2019i
authored andcommitted
buffer: prevent cache corruption
Recent research discovered a set of potential issues related with cache prefetch. Specifically it seems like uncached access to memory can cause cache prefetch. This can cause problems in buffer_attach() and buffer_detach() where buffers are added to or removed from lists respectively via uncached addresses, after which they can be used via cached addresses. Add proper cache synchronisation and interrupt locking to protect against such memory corruption. Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
1 parent 4bdbe3b commit 3e3d0cd

2 files changed

Lines changed: 27 additions & 1 deletion

File tree

‎src/audio/buffer.c‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,8 +286,15 @@ void comp_update_buffer_consume(struct comp_buffer __sparse_cache *buffer, uint3
286286
(char *)audio_stream_get_addr(&buffer->stream)));
287287
}
288288

289+
/*
290+
* Locking: must be called with interrupts disabled! Serialized IPCs protect us
291+
* from racing attach / detach calls, but the scheduler can interrupt the IPC
292+
* thread and begin using the buffer for streaming. FIXME: this is still a
293+
* problem with different cores.
294+
*/
289295
void buffer_attach(struct comp_buffer *buffer, struct list_item *head, int dir)
290296
{
297+
struct list_item *list = buffer_comp_list(buffer, dir);
291298
struct list_item __sparse_cache *needs_sync;
292299
bool further_buffers_exist;
293300

@@ -302,11 +309,17 @@ void buffer_attach(struct comp_buffer *buffer, struct list_item *head, int dir)
302309
if (further_buffers_exist)
303310
dcache_writeback_region(needs_sync, sizeof(struct list_item));
304311
/* The cache line can be prefetched here, invalidate it after prepending */
305-
list_item_prepend(buffer_comp_list(buffer, dir), head);
312+
list_item_prepend(list, head);
306313
if (further_buffers_exist)
307314
dcache_invalidate_region(needs_sync, sizeof(struct list_item));
315+
/* no dirty cache lines exist for this buffer yet, no need to write back */
316+
dcache_invalidate_region(uncache_to_cache(list), sizeof(*list));
308317
}
309318

319+
/*
320+
* Locking: must be called with interrupts disabled! See buffer_attach() above
321+
* for details
322+
*/
310323
void buffer_detach(struct comp_buffer *buffer, struct list_item *head, int dir)
311324
{
312325
struct list_item __sparse_cache *needs_sync_prev, *needs_sync_next;
@@ -329,8 +342,10 @@ void buffer_detach(struct comp_buffer *buffer, struct list_item *head, int dir)
329342
dcache_writeback_region(needs_sync_next, sizeof(struct list_item));
330343
if (buffers_before_exist)
331344
dcache_writeback_region(needs_sync_prev, sizeof(struct list_item));
345+
dcache_writeback_region(uncache_to_cache(buf_list), sizeof(*buf_list));
332346
/* buffers before or after can be prefetched here */
333347
list_item_del(buf_list);
348+
dcache_invalidate_region(uncache_to_cache(buf_list), sizeof(*buf_list));
334349
if (buffers_after_exist)
335350
dcache_invalidate_region(needs_sync_next, sizeof(struct list_item));
336351
if (buffers_before_exist)

‎src/audio/module_adapter/module_adapter.c‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <sof/common.h>
1919
#include <sof/platform.h>
2020
#include <sof/ut.h>
21+
#include <rtos/interrupt.h>
2122
#include <limits.h>
2223
#include <stdint.h>
2324

@@ -439,13 +440,17 @@ int module_adapter_prepare(struct comp_dev *dev)
439440
for (i = 0; i < mod->num_output_buffers; i++) {
440441
struct comp_buffer *buffer = buffer_alloc(buff_size, SOF_MEM_CAPS_RAM,
441442
0, PLATFORM_DCACHE_ALIGN);
443+
uint32_t flags;
444+
442445
if (!buffer) {
443446
comp_err(dev, "module_adapter_prepare(): failed to allocate local buffer");
444447
ret = -ENOMEM;
445448
goto free;
446449
}
447450

451+
irq_local_disable(flags);
448452
buffer_attach(buffer, &mod->sink_buffer_list, PPL_DIR_UPSTREAM);
453+
irq_local_enable(flags);
449454

450455
buffer_c = buffer_acquire(buffer);
451456
buffer_set_params(buffer_c, mod->stream_params, BUFFER_UPDATE_FORCE);
@@ -480,8 +485,11 @@ int module_adapter_prepare(struct comp_dev *dev)
480485
list_for_item_safe(blist, _blist, &mod->sink_buffer_list) {
481486
struct comp_buffer *buffer = container_of(blist, struct comp_buffer,
482487
sink_list);
488+
uint32_t flags;
483489

490+
irq_local_disable(flags);
484491
buffer_detach(buffer, &mod->sink_buffer_list, PPL_DIR_UPSTREAM);
492+
irq_local_enable(flags);
485493
buffer_free(buffer);
486494
}
487495

@@ -1404,8 +1412,11 @@ void module_adapter_free(struct comp_dev *dev)
14041412
list_for_item_safe(blist, _blist, &mod->sink_buffer_list) {
14051413
struct comp_buffer *buffer = container_of(blist, struct comp_buffer,
14061414
sink_list);
1415+
uint32_t flags;
14071416

1417+
irq_local_disable(flags);
14081418
buffer_detach(buffer, &mod->sink_buffer_list, PPL_DIR_UPSTREAM);
1419+
irq_local_enable(flags);
14091420
buffer_free(buffer);
14101421
}
14111422

0 commit comments

Comments
 (0)