From 6788ebc53bb837936eabb0292e3a6f9428251cec Mon Sep 17 00:00:00 2001 From: Marcin Szkudlinski Date: Mon, 4 Sep 2023 10:35:49 +0200 Subject: [PATCH] sparse: add a debug core check for shared structures to verify that shared structures are used in a proper way a debug check is introduced If compiled with COHERENT_CHECK_NONSHARED_CORES flag each usage of shared structures will be verified against proper memory alias usage Signed-off-by: Marcin Szkudlinski --- src/audio/buffer.c | 14 ++++++++++++++ src/include/sof/audio/buffer.h | 2 ++ src/include/sof/coherent.h | 14 ++++++++++++++ 3 files changed, 30 insertions(+) diff --git a/src/audio/buffer.c b/src/audio/buffer.c index 161d72632717..78b36158e50e 100644 --- a/src/audio/buffer.c +++ b/src/audio/buffer.c @@ -53,6 +53,8 @@ struct comp_buffer *buffer_alloc(uint32_t size, uint32_t caps, uint32_t flags, u return NULL; } + CORE_CHECK_STRUCT_INIT(buffer, is_shared); + stream_addr = rballoc_align(0, caps, size, align); if (!stream_addr) { rfree(buffer); @@ -80,6 +82,7 @@ struct comp_buffer *buffer_alloc(uint32_t size, uint32_t caps, uint32_t flags, u void buffer_zero(struct comp_buffer __sparse_cache *buffer) { buf_dbg(buffer, "stream_zero()"); + CORE_CHECK_STRUCT(buffer); bzero(audio_stream_get_addr(&buffer->stream), audio_stream_get_size(&buffer->stream)); if (buffer->caps & SOF_MEM_CAPS_DMA) @@ -92,6 +95,8 @@ int buffer_set_size(struct comp_buffer __sparse_cache *buffer, uint32_t size, ui { void *new_ptr = NULL; + CORE_CHECK_STRUCT(buffer); + /* validate request */ if (size == 0) { buf_err(buffer, "resize size = %u is invalid", size); @@ -130,6 +135,8 @@ int buffer_set_params(struct comp_buffer __sparse_cache *buffer, int ret; int i; + CORE_CHECK_STRUCT(buffer); + if (!params) { buf_err(buffer, "buffer_set_params(): !params"); return -EINVAL; @@ -157,6 +164,7 @@ bool buffer_params_match(struct comp_buffer __sparse_cache *buffer, struct sof_ipc_stream_params *params, uint32_t flag) { assert(params); + CORE_CHECK_STRUCT(buffer); if ((flag & BUFF_PARAMS_FRAME_FMT) && audio_stream_get_frm_fmt(&buffer->stream) != params->frame_fmt) @@ -180,6 +188,8 @@ void buffer_free(struct comp_buffer *buffer) .buffer = buffer, }; + CORE_CHECK_STRUCT(buffer); + if (!buffer) return; @@ -254,6 +264,8 @@ void comp_update_buffer_consume(struct comp_buffer __sparse_cache *buffer, uint3 .transaction_begin_address = audio_stream_get_rptr(&buffer->stream), }; + CORE_CHECK_STRUCT(buffer); + /* return if no bytes */ if (!bytes) { #if CONFIG_SOF_LOG_DBG_BUFFER @@ -292,6 +304,7 @@ void comp_update_buffer_consume(struct comp_buffer __sparse_cache *buffer, uint3 void buffer_attach(struct comp_buffer *buffer, struct list_item *head, int dir) { struct list_item *list = buffer_comp_list(buffer, dir); + CORE_CHECK_STRUCT(buffer); list_item_prepend(list, head); } @@ -302,5 +315,6 @@ void buffer_attach(struct comp_buffer *buffer, struct list_item *head, int dir) void buffer_detach(struct comp_buffer *buffer, struct list_item *head, int dir) { struct list_item *buf_list = buffer_comp_list(buffer, dir); + CORE_CHECK_STRUCT(buffer); list_item_del(buf_list); } diff --git a/src/include/sof/audio/buffer.h b/src/include/sof/audio/buffer.h index 4238c902dfdf..51a505b209cf 100644 --- a/src/include/sof/audio/buffer.h +++ b/src/include/sof/audio/buffer.h @@ -134,6 +134,8 @@ extern struct tr_ctx buffer_tr; */ struct comp_buffer { /* data buffer */ + CORE_CHECK_STRUCT_FIELD; + struct audio_stream stream; /* configuration */ diff --git a/src/include/sof/coherent.h b/src/include/sof/coherent.h index 4ca5120cfee9..8e06fb466a66 100644 --- a/src/include/sof/coherent.h +++ b/src/include/sof/coherent.h @@ -80,9 +80,23 @@ struct coherent { /* debug sharing amongst cores */ #ifdef COHERENT_CHECK_NONSHARED_CORES + +#define CORE_CHECK_STRUCT_FIELD uint32_t __core; bool __is_shared +#define CORE_CHECK_STRUCT_INIT(_c, is_shared) { (_c)->__core = cpu_get_id(); \ + (_c)->__is_shared = is_shared; } +#define CORE_CHECK_STRUCT(_c) { assert(!!(_c)->__is_shared == !!is_uncached(_c)); \ + assert(cpu_get_id() == (_c)->__core || (_c)->__is_shared); } + #define CHECK_COHERENT_CORE(_c) assert((_c)->core == cpu_get_id()) + #else + +#define CORE_CHECK_STRUCT_FIELD +#define CORE_CHECK_STRUCT_INIT(_c, is_shared) +#define CORE_CHECK_STRUCT(_c) + #define CHECK_COHERENT_CORE(_c) + #endif #ifdef __ZEPHYR__