Skip to content

Commit 60f40a6

Browse files
author
Jyri Sarha
committed
modules: Add safeguard to mod_alloc() and friends
Add safeguard to mod_alloc() and friends that checks that they are always called from the same thread (e.g. no locking needed). Signed-off-by: Jyri Sarha <jyri.sarha@linux.intel.com>
1 parent 61bafa9 commit 60f40a6

3 files changed

Lines changed: 30 additions & 1 deletion

File tree

src/audio/module_adapter/Kconfig

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,17 @@ menu "Processing modules"
1313
containers to allocate at once is selected by this
1414
config option.
1515

16+
config MODULE_MEMORY_API_DEBUG
17+
bool "Turn on memory API thread safety checks"
18+
default y if DEBUG
19+
help
20+
The Module Memory API structures are not protected
21+
by locks. This is because the initialization,
22+
allocation, and freeing of resources should always
23+
be done in the same thread. This option adds an
24+
assert to make sure no other thread makes such
25+
operations.
26+
1627
config CADENCE_CODEC
1728
bool "Cadence codec"
1829
default n

src/audio/module_adapter/module/generic.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,16 @@
1717
#include <sof/audio/data_blob.h>
1818
#include <sof/lib/fast-get.h>
1919

20+
#if CONFIG_MODULE_MEMORY_API_DEBUG
21+
#define MEM_API_CHECK_THREAD(res) __ASSERT((res)->rsrc_mngr == k_current_get(), \
22+
"Module memory API opration from wrong thread")
23+
#else
24+
#define MEM_API_CHECK_THREAD(res)
25+
#endif
26+
2027
LOG_MODULE_DECLARE(module_adapter, CONFIG_SOF_LOG_LEVEL);
2128

29+
2230
int module_load_config(struct comp_dev *dev, const void *cfg, size_t size)
2331
{
2432
int ret;
@@ -99,7 +107,9 @@ int module_init(struct processing_module *mod)
99107
list_init(&md->resources.cont_chunk_list);
100108
md->resources.heap_usage = 0;
101109
md->resources.heap_high_water_mark = 0;
102-
110+
#if CONFIG_MODULE_MEMORY_API_DEBUG
111+
md->resources.rsrc_mngr = k_current_get();
112+
#endif
103113
/* Now we can proceed with module specific initialization */
104114
ret = interface->init(mod);
105115
if (ret) {
@@ -167,6 +177,7 @@ void *mod_alloc_align(struct processing_module *mod, uint32_t size, uint32_t ali
167177
struct module_resources *res = &mod->priv.resources;
168178
void *ptr;
169179

180+
MEM_API_CHECK_THREAD(res);
170181
if (!container)
171182
return NULL;
172183

@@ -251,6 +262,7 @@ mod_data_blob_handler_new(struct processing_module *mod)
251262
struct module_memory *container = container_get(mod);
252263
struct comp_data_blob_handler *dbh;
253264

265+
MEM_API_CHECK_THREAD(res);
254266
if (!container)
255267
return NULL;
256268

@@ -284,6 +296,7 @@ const void *mod_fast_get(struct processing_module *mod, const void * const dram_
284296
struct module_memory *container = container_get(mod);
285297
const void *ptr;
286298

299+
MEM_API_CHECK_THREAD(res);
287300
if (!container)
288301
return NULL;
289302

@@ -315,6 +328,7 @@ int mod_free(struct processing_module *mod, void *ptr)
315328
struct list_item *mem_list;
316329
struct list_item *_mem_list;
317330

331+
MEM_API_CHECK_THREAD(res);
318332
if (!ptr)
319333
return 0;
320334

@@ -539,6 +553,7 @@ void mod_free_all(struct processing_module *mod)
539553
struct list_item *list;
540554
struct list_item *_list;
541555

556+
MEM_API_CHECK_THREAD(res);
542557
/* Find which container keeps this memory */
543558
list_for_item_safe(list, _list, &res->mem_list) {
544559
struct module_memory *mem = container_of(list, struct module_memory, mem_list);

src/include/sof/audio/module_adapter/module/generic.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,9 @@ struct module_resources {
127127
struct list_item cont_chunk_list; /**< Memory container chunks */
128128
size_t heap_usage;
129129
size_t heap_high_water_mark;
130+
#if CONFIG_MODULE_MEMORY_API_DEBUG
131+
k_tid_t rsrc_mngr;
132+
#endif
130133
};
131134

132135
/**

0 commit comments

Comments
 (0)