Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 40 additions & 15 deletions src/audio/module_adapter/module_adapter.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,28 @@ struct comp_dev *module_adapter_new(const struct comp_driver *drv,
#endif

static struct dp_heap_user *module_adapter_dp_heap_new(const struct comp_ipc_config *config,
const struct module_ext_init_data *ext_init,
size_t *heap_size)
{
/* src-lite with 8 channels has been seen allocating 14k in one go */
/* FIXME: the size will be derived from configuration */
const size_t buf_size = 20 * 1024;
size_t buf_size = 20 * 1024;

#if CONFIG_IPC_MAJOR_4
if (config->ipc_extended_init && ext_init && ext_init->dp_data &&
(ext_init->dp_data->lifetime_heap_bytes > 0 ||
ext_init->dp_data->interim_heap_bytes)) {
Copy link

Copilot AI Apr 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The condition mixes an explicit comparison with an implicit non-zero test: ... || ext_init->dp_data->interim_heap_bytes). For consistency/readability (and to avoid surprises if the type changes), compare interim_heap_bytes > 0 like the lifetime field.

Suggested change
ext_init->dp_data->interim_heap_bytes)) {
ext_init->dp_data->interim_heap_bytes > 0)) {

Copilot uses AI. Check for mistakes.
/*
* For the moment there is only one heap so sum up
* lifetime and interim values. It is also a conscious
* decision here to count the size of struct
* dp_heap_user to be included into required heap
* size.
*/
buf_size = ext_init->dp_data->lifetime_heap_bytes +
ext_init->dp_data->interim_heap_bytes;
}
Comment on lines +68 to +81
Copy link

Copilot AI Apr 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DP heap size calculation appears to under-allocate and can underflow. buf_size is set to lifetime_heap_bytes + interim_heap_bytes, but later the usable heap is computed as buf_size - heap_prefix_size; if the IPC values are meant to be the required heap size (per IPC4 comments), you likely need to allocate required + heap_prefix_size so the heap has the requested capacity. Also, if the sum is smaller than heap_prefix_size, buf_size - heap_prefix_size will wrap (size_t underflow) and k_heap_init() will be called with an invalid huge size.

Copilot uses AI. Check for mistakes.
Comment on lines +79 to +81
Copy link

Copilot AI Apr 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Potential integer overflow/wrap: lifetime_heap_bytes and interim_heap_bytes are uint32_t, so lifetime + interim is computed in 32-bit and can wrap before being assigned to size_t. Cast to size_t before adding and consider bounding the requested size to avoid pathological allocations from IPC input.

Copilot uses AI. Check for mistakes.
#endif
/* Keep uncached to match the default SOF heap! */
uint8_t *mod_heap_mem = rballoc_align(SOF_MEM_FLAG_USER | SOF_MEM_FLAG_COHERENT,
buf_size, PAGE_SZ);
Expand All @@ -86,8 +102,10 @@ static struct dp_heap_user *module_adapter_dp_heap_new(const struct comp_ipc_con
return mod_heap_user;
}

static struct processing_module *module_adapter_mem_alloc(const struct comp_driver *drv,
const struct comp_ipc_config *config)
static struct processing_module *
module_adapter_mem_alloc(const struct comp_driver *drv,
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this needed? Would the added third argument otherwise exceed 100 characters?

const struct comp_ipc_config *config,
const struct module_ext_init_data *ext_init)
{
struct k_heap *mod_heap;
/*
Expand All @@ -104,7 +122,7 @@ static struct processing_module *module_adapter_mem_alloc(const struct comp_driv

if (config->proc_domain == COMP_PROCESSING_DOMAIN_DP && IS_ENABLED(CONFIG_USERSPACE) &&
!IS_ENABLED(CONFIG_SOF_USERSPACE_USE_DRIVER_HEAP)) {
mod_heap_user = module_adapter_dp_heap_new(config, &heap_size);
mod_heap_user = module_adapter_dp_heap_new(config, ext_init, &heap_size);
if (!mod_heap_user) {
comp_cl_err(drv, "Failed to allocate DP module heap");
return NULL;
Expand Down Expand Up @@ -220,8 +238,14 @@ struct comp_dev *module_adapter_new_ext(const struct comp_driver *drv,
return NULL;
}
#endif
const struct module_ext_init_data *ext_init =
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor: You could probably pass only dp_data to module_adapter_mem_alloc instead of the entire structure ext_data.
Since the ext_data is 0-initialized, the dp_data pointer is already set to NULL so you probably wouldn't need the "#if":

#if CONFIG_IPC_MAJOR_4
&ext_data;
#else
NULL;
#endif

struct processing_module *mod = module_adapter_mem_alloc(drv, config);
struct processing_module *mod = module_adapter_mem_alloc(drv, config, ext_init);

if (!mod)
return NULL;
Expand All @@ -234,6 +258,17 @@ struct comp_dev *module_adapter_new_ext(const struct comp_driver *drv,

struct comp_dev *dev = mod->dev;

dst = &mod->priv.cfg;
/*
* NOTE: dst->ext_data points to stack variable and contains
* pointers to IPC payload mailbox, so its only valid in
* functions that called from this function. This why
* the pointer is set NULL before this function exits.
*/
#if CONFIG_IPC_MAJOR_4
dst->ext_data = &ext_data;
#endif

#if CONFIG_ZEPHYR_DP_SCHEDULER
/* create a task for DP processing */
if (config->proc_domain == COMP_PROCESSING_DOMAIN_DP) {
Expand All @@ -246,16 +281,6 @@ struct comp_dev *module_adapter_new_ext(const struct comp_driver *drv,
}
#endif /* CONFIG_ZEPHYR_DP_SCHEDULER */

dst = &mod->priv.cfg;
/*
* NOTE: dst->ext_data points to stack variable and contains
* pointers to IPC payload mailbox, so its only valid in
* functions that called from this function. This why
* the pointer is set NULL before this function exits.
*/
#if CONFIG_IPC_MAJOR_4
dst->ext_data = &ext_data;
#endif
ret = module_adapter_init_data(dev, dst, config, &spec);
if (ret) {
comp_err(dev, "%d: module init data failed",
Expand Down
7 changes: 6 additions & 1 deletion src/audio/pipeline/pipeline-schedule.c
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,7 @@ int pipeline_comp_dp_task_init(struct comp_dev *comp)
{
/* DP tasks are guaranteed to have a module_adapter */
struct processing_module *mod = comp_mod(comp);
size_t stack_size = TASK_DP_STACK_SIZE;
struct task_ops ops = {
.run = dp_task_run,
.get_deadline = NULL,
Expand All @@ -403,8 +404,12 @@ int pipeline_comp_dp_task_init(struct comp_dev *comp)
unsigned int flags = IS_ENABLED(CONFIG_USERSPACE) ? K_USER : 0;
#endif

if (mod->priv.cfg.ext_data && mod->priv.cfg.ext_data->dp_data &&
mod->priv.cfg.ext_data->dp_data->stack_bytes > 0)
stack_size = mod->priv.cfg.ext_data->dp_data->stack_bytes;

Comment on lines +408 to +410
Copy link

Copilot AI Apr 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DP task stack sizing should enforce a minimum (and potentially alignment) rather than blindly accepting any positive dp_data->stack_bytes. If the IPC value is smaller than TASK_DP_STACK_SIZE (or smaller than what Zephyr requires), this can create an undersized stack and lead to runtime crashes; consider clamping with max(TASK_DP_STACK_SIZE, requested) (and/or rejecting too-small values).

Suggested change
mod->priv.cfg.ext_data->dp_data->stack_bytes > 0)
stack_size = mod->priv.cfg.ext_data->dp_data->stack_bytes;
mod->priv.cfg.ext_data->dp_data->stack_bytes > 0) {
size_t requested_stack_size = mod->priv.cfg.ext_data->dp_data->stack_bytes;
if (requested_stack_size > TASK_DP_STACK_SIZE)
stack_size = requested_stack_size;
}

Copilot uses AI. Check for mistakes.
return scheduler_dp_task_init(&comp->task, SOF_UUID(dp_task_uuid), &ops, mod,
comp->ipc_config.core, TASK_DP_STACK_SIZE, flags);
comp->ipc_config.core, stack_size, flags);
}
#endif /* CONFIG_ZEPHYR_DP_SCHEDULER */

Expand Down
Loading