Skip to content

Commit c8a8626

Browse files
Jyri Sarhakv2019i
authored andcommitted
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). The checking code has to be also behind defined(__ZEPHYR__) to keep cmocka tests working. Signed-off-by: Jyri Sarha <jyri.sarha@linux.intel.com>
1 parent 80ee3da commit c8a8626

3 files changed

Lines changed: 35 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,6 +17,14 @@
1717
#include <sof/audio/data_blob.h>
1818
#include <sof/lib/fast-get.h>
1919

20+
/* The __ZEPHYR__ condition is to keep cmocka tests working */
21+
#if CONFIG_MODULE_MEMORY_API_DEBUG && defined(__ZEPHYR__)
22+
#define MEM_API_CHECK_THREAD(res) __ASSERT((res)->rsrc_mngr == k_current_get(), \
23+
"Module memory API operation from wrong thread")
24+
#else
25+
#define MEM_API_CHECK_THREAD(res)
26+
#endif
27+
2028
LOG_MODULE_DECLARE(module_adapter, CONFIG_SOF_LOG_LEVEL);
2129

2230
int module_load_config(struct comp_dev *dev, const void *cfg, size_t size)
@@ -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 && defined(__ZEPHYR__)
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_resource *container = container_get(mod);
252263
struct comp_data_blob_handler *bhp;
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_resource *container = container_get(mod);
285297
const void *ptr;
286298

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

@@ -340,6 +353,7 @@ int mod_free(struct processing_module *mod, const void *ptr)
340353
struct list_item *res_list;
341354
struct list_item *_res_list;
342355

356+
MEM_API_CHECK_THREAD(res);
343357
if (!ptr)
344358
return 0;
345359

@@ -560,6 +574,7 @@ void mod_free_all(struct processing_module *mod)
560574
struct list_item *list;
561575
struct list_item *_list;
562576

577+
MEM_API_CHECK_THREAD(res);
563578
/* Find which container keeps this memory */
564579
list_for_item_safe(list, _list, &res->res_list) {
565580
struct module_resource *container =

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@
1919
#include <sof/audio/source_api.h>
2020
#include "module_interface.h"
2121

22+
/* The __ZEPHYR__ condition is to keep cmocka tests working */
23+
#if CONFIG_MODULE_MEMORY_API_DEBUG && defined(__ZEPHYR__)
24+
#include <zephyr/kernel/thread.h>
25+
#endif
26+
2227
/*
2328
* helpers to determine processing type
2429
* Needed till all the modules use PROCESSING_MODE_SINK_SOURCE
@@ -127,6 +132,9 @@ struct module_resources {
127132
struct list_item cont_chunk_list; /**< Memory container chunks */
128133
size_t heap_usage;
129134
size_t heap_high_water_mark;
135+
#if CONFIG_MODULE_MEMORY_API_DEBUG && defined(__ZEPHYR__)
136+
k_tid_t rsrc_mngr;
137+
#endif
130138
};
131139

132140
enum mod_resource_type {

0 commit comments

Comments
 (0)