Skip to content

Commit

Permalink
sparse: add a debug core check for shared structures
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
marcinszkudlinski committed Sep 7, 2023
1 parent 7db9ca1 commit ad8f134
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/audio/buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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)
Expand All @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand All @@ -180,6 +188,8 @@ void buffer_free(struct comp_buffer *buffer)
.buffer = buffer,
};

CORE_CHECK_STRUCT(buffer);

if (!buffer)
return;

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
}

Expand All @@ -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);
}
2 changes: 2 additions & 0 deletions src/include/sof/audio/buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ extern struct tr_ctx buffer_tr;
*/
struct comp_buffer {
/* data buffer */
CORE_CHECK_STRUCT_FIELD;

struct audio_stream stream;

/* configuration */
Expand Down
14 changes: 14 additions & 0 deletions src/include/sof/coherent.h
Original file line number Diff line number Diff line change
Expand Up @@ -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__
Expand Down

0 comments on commit ad8f134

Please sign in to comment.