Skip to content

Commit 95f5201

Browse files
checkupupJun Lai
authored andcommitted
dax: fix race condition on the tuning buffer
While the tuning buffer is being updated, it may also be being used simultaneously in the process thread. Signed-off-by: Jun Lai <jun.lai@dolby.com>
1 parent 9fd06e9 commit 95f5201

2 files changed

Lines changed: 38 additions & 12 deletions

File tree

src/audio/module_adapter/module/dolby/dax.c

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ SOF_DEFINE_REG_UUID(dolby_dax_audio_processing);
2525
#define DAX_CP_MASK 0x8
2626
#define DAX_VOLUME_MASK 0x10
2727
#define DAX_CTC_MASK 0x20
28+
#define DAX_TUNING_FILE_MASK 0x40
2829
#define DAX_PROCESSING_MASK 0x10000
2930
#define DAX_RESET_MASK 0x20000
3031
#define DAX_FREE_MASK 0x40000
@@ -241,24 +242,26 @@ static bool is_enabled(struct processing_module *mod)
241242
return dax_ctx->enable && dax_ctx->p_dax;
242243
}
243244

244-
static int set_tuning_file(struct processing_module *mod, void *value, uint32_t size)
245+
static int set_tuning_file(struct processing_module *mod)
245246
{
246-
int ret = 0;
247+
int ret = -EINVAL;
247248
struct comp_dev *dev = mod->dev;
248249
struct dax_adapter_data *adapter_data = module_get_private_data(mod);
249250
struct sof_dax *dax_ctx = &adapter_data->dax_ctx;
251+
k_spinlock_key_t key;
250252

251-
if (dax_buffer_alloc(mod, &dax_ctx->tuning_file_buffer, size) != 0) {
252-
comp_err(dev, "allocate %u bytes failed for tuning file", size);
253-
ret = -ENOMEM;
254-
} else {
255-
memcpy_s(dax_ctx->tuning_file_buffer.addr,
256-
dax_ctx->tuning_file_buffer.free,
257-
value,
258-
size);
253+
key = k_spin_lock(&adapter_data->lock);
254+
if (adapter_data->tmp_tuning_buf.addr && adapter_data->tmp_tuning_buf.size > 0) {
255+
dax_buffer_release(mod, &dax_ctx->tuning_file_buffer);
256+
// Move the tmp_tuning_buf rather than copying
257+
dax_ctx->tuning_file_buffer = adapter_data->tmp_tuning_buf;
258+
adapter_data->tmp_tuning_buf.addr = NULL;
259+
adapter_data->tmp_tuning_buf.size = 0;
260+
ret = 0;
259261
}
262+
k_spin_unlock(&adapter_data->lock, key);
260263

261-
comp_info(dev, "allocated: tuning %u, ret %d", dax_ctx->tuning_file_buffer.size, ret);
264+
comp_info(dev, "apply tuning %p, ret %d", dax_ctx->tuning_file_buffer.addr, ret);
262265
return ret;
263266
}
264267

@@ -367,7 +370,19 @@ static int dax_set_param_wrapper(struct processing_module *mod,
367370

368371
switch (id) {
369372
case DAX_PARAM_ID_TUNING_FILE:
370-
set_tuning_file(mod, value, size);
373+
if (dax_buffer_alloc(mod, &adapter_data->tmp_tuning_buf, size) != 0) {
374+
comp_err(dev, "allocate %u bytes failed for tuning file", size);
375+
ret = -ENOMEM;
376+
} else {
377+
memcpy_s(adapter_data->tmp_tuning_buf.addr,
378+
adapter_data->tmp_tuning_buf.free,
379+
value,
380+
size);
381+
flag_process(adapter_data, DAX_TUNING_FILE_MASK, DAX_FLAG_SET);
382+
comp_info(dev, "allocated: tuning %p, size %u",
383+
adapter_data->tmp_tuning_buf.addr,
384+
adapter_data->tmp_tuning_buf.size);
385+
}
371386
break;
372387
case DAX_PARAM_ID_ENABLE:
373388
tmp_val = *((int32_t *)value);
@@ -488,6 +503,12 @@ static void check_and_update_settings(struct processing_module *mod)
488503
if (!is_enabled(mod))
489504
return;
490505

506+
if (flag_process(adapter_data, DAX_TUNING_FILE_MASK, DAX_FLAG_READ_AND_CLEAR)) {
507+
set_tuning_file(mod);
508+
flag_process(adapter_data, DAX_DEVICE_MASK, DAX_FLAG_SET);
509+
flag_process(adapter_data, DAX_VOLUME_MASK, DAX_FLAG_SET);
510+
}
511+
491512
if (flag_process(adapter_data, DAX_DEVICE_MASK, DAX_FLAG_READ_AND_CLEAR)) {
492513
set_device(mod, dax_ctx->out_device);
493514
set_tuning_device(mod, dax_ctx->tuning_device);
@@ -547,6 +568,7 @@ static int sof_dax_free(struct processing_module *mod)
547568
dax_buffer_release(mod, &dax_ctx->tuning_file_buffer);
548569
mod_data_blob_handler_free(mod, dax_ctx->blob_handler);
549570
dax_ctx->blob_handler = NULL;
571+
dax_buffer_release(mod, &adapter_data->tmp_tuning_buf);
550572
mod_free(mod, adapter_data);
551573
module_set_private_data(mod, NULL);
552574
}
@@ -584,6 +606,7 @@ static int sof_dax_init(struct processing_module *mod)
584606
adapter_data = module_get_private_data(mod);
585607
adapter_data->comp_id = dev->ipc_config.id;
586608
adapter_data->priority = DAX_USER_PRIORITY_DEFAULT;
609+
k_spinlock_init(&adapter_data->lock);
587610
dax_ctx = &adapter_data->dax_ctx;
588611
dax_ctx->enable = 0;
589612
dax_ctx->profile = 0;

src/audio/module_adapter/module/dolby/dax.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* Author: Jun Lai <jun.lai@dolby.com>
88
*/
99

10+
#include <rtos/spinlock.h>
1011
#include <sof/audio/module_adapter/module/generic.h>
1112
#include <dax_inf.h>
1213

@@ -24,6 +25,8 @@ struct dax_adapter_data {
2425
atomic_t proc_flags;
2526
uint32_t comp_id;
2627
int32_t priority;
28+
struct dax_buffer tmp_tuning_buf;
29+
struct k_spinlock lock;
2730
};
2831

2932
/**

0 commit comments

Comments
 (0)