From 79775e921bcfe9fd2a7e56ada0bd08c07671b224 Mon Sep 17 00:00:00 2001 From: Shrushti P K Date: Wed, 2 Dec 2026 12:19:31 +0530 Subject: [PATCH 1/4] audio: copier_host: free the FPI sync group with its last member add_to_fpi_sync_group() allocates the struct fpi_sync_group that every host gateway copier joining the same group_id shares with mod_alloc_ext(), so the group belongs to the resource pool of the copier that happens to create it. mod_free_all() releases that pool when the module is freed or when its init fails, while delete_from_fpi_sync_group() keeps the group linked in group_list_head as long as another member still references it. Deleting the creating copier while a second member is alive therefore leaves a freed node in the global list. The next host copier init reads it in find_group_by_id() and list_item_append() writes to it, and when the remaining member is deleted it decrements ref_count and runs list_item_del() on the freed block before mod_free() fails to find it. A single playback copier whose init fails after joining the group, for example with a node_id v_index of IPC4_MAX_PIPELINE_REG_SLOTS or more, leaves the same dangling node because the e_conv path does not leave the group before mod_free_all() frees it. Allocate the group from the system user heap with sof_heap_alloc(), as data_blob.c does for memory that is not owned by one module, and free it there when the last member leaves. Leave the group on the copier_host_create() error path. hd->group_id was a uint8_t copy of the 32-bit IPC group id, so delete_from_fpi_sync_group() could release a different group than the one joined; store the full id. Fixes: ca850768bfc9 ("audio: copier: Use module API also for allocating non cached memory") Signed-off-by: Shrushti P K --- src/audio/copier/copier_host.c | 24 ++++++++++++++++-------- src/audio/copier/host_copier.h | 2 +- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/audio/copier/copier_host.c b/src/audio/copier/copier_host.c index c3ab0208a5d0..61f8b9a40ae9 100644 --- a/src/audio/copier/copier_host.c +++ b/src/audio/copier/copier_host.c @@ -4,6 +4,7 @@ // // Author: Baofeng Tian +#include #include #include #include @@ -50,7 +51,6 @@ __cold static int add_to_fpi_sync_group(struct comp_dev *parent_dev, struct ipc4_copier_sync_group *sync_group) { struct fpi_sync_group *group = find_group_by_id(sync_group->group_id); - struct processing_module *mod = comp_mod(parent_dev); assert_can_be_cold(); @@ -63,8 +63,13 @@ __cold static int add_to_fpi_sync_group(struct comp_dev *parent_dev, group->ref_count++; } else { - group = mod_alloc_ext(mod, SOF_MEM_FLAG_USER | SOF_MEM_FLAG_COHERENT, - sizeof(*group), 0); + /* The group is shared by every gateway that joins it, so it is not a + * resource of the module that creates it: mod_free_all() would release + * it with that module while the other members are still linked to it. + */ + group = sof_heap_alloc(sof_sys_user_heap_get(), + SOF_MEM_FLAG_USER | SOF_MEM_FLAG_COHERENT, + sizeof(*group), 0); if (!group) { comp_err(parent_dev, "Failed to alloc memory for new group"); return -ENOMEM; @@ -84,10 +89,9 @@ __cold static int add_to_fpi_sync_group(struct comp_dev *parent_dev, return 0; } -__cold static void delete_from_fpi_sync_group(struct processing_module *mod) +__cold static void delete_from_fpi_sync_group(struct host_data *hd) { - struct copier_data *cd = module_get_private_data(mod); - struct fpi_sync_group *group = find_group_by_id(cd->hd->group_id); + struct fpi_sync_group *group = find_group_by_id(hd->group_id); assert_can_be_cold(); @@ -97,7 +101,7 @@ __cold static void delete_from_fpi_sync_group(struct processing_module *mod) group->ref_count--; if (group->ref_count == 0) { list_item_del(&group->item); - mod_free(mod, group); + sof_heap_free(sof_sys_user_heap_get(), group); } } #endif @@ -254,6 +258,10 @@ __cold int copier_host_create(struct processing_module *mod, return 0; e_conv: +#if CONFIG_HOST_DMA_STREAM_SYNCHRONIZATION + if (hd->is_grouped) + delete_from_fpi_sync_group(hd); +#endif host_common_free(hd); e_data: mod_free(mod, hd); @@ -269,7 +277,7 @@ __cold void copier_host_free(struct processing_module *mod) #if CONFIG_HOST_DMA_STREAM_SYNCHRONIZATION if (cd->hd->is_grouped) - delete_from_fpi_sync_group(mod); + delete_from_fpi_sync_group(cd->hd); #endif host_common_free(cd->hd); mod_free(mod, cd->hd); diff --git a/src/audio/copier/host_copier.h b/src/audio/copier/host_copier.h index 309ddaeae24b..56ac6bcf30e4 100644 --- a/src/audio/copier/host_copier.h +++ b/src/audio/copier/host_copier.h @@ -115,7 +115,7 @@ struct host_data { uint32_t dma_buffer_size; /* dma buffer size */ #if CONFIG_HOST_DMA_STREAM_SYNCHRONIZATION bool is_grouped; - uint8_t group_id; + uint32_t group_id; uint64_t next_sync; uint64_t period_in_cycles; #endif From ecb139744317ff4d2985732bc3111585b4115eb8 Mon Sep 17 00:00:00 2001 From: Shrushti P K Date: Fri, 4 Dec 2026 22:01:04 +0530 Subject: [PATCH 2/4] audio: module_adapter: free the module when creation fails after init module_adapter_new_ext() keeps going after module_init() has succeeded: it calls module_adapter_reset_data() and, on IPC4, module_adapter_params(). If anything there fails the err path only frees the task, scrubs the pipeline back-pointers and releases the module allocation context, so the module's own .free() never runs. That is fine for state that lives in the module allocation context, which is released wholesale, but not for anything the .init() method took ownership of outside it. A host copier joins an FPI sync group in copier_host_create(); the group lives on the system user heap and is released by its last member, so a failed create leaves a member that never leaves, and the id cannot be reused later with a different period. Track whether module_init() succeeded and run module_free() on the err path when it did, before the task is freed and the allocation context goes away, matching the order in module_adapter_free(). Signed-off-by: Shrushti P K --- src/audio/module_adapter/module_adapter.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/audio/module_adapter/module_adapter.c b/src/audio/module_adapter/module_adapter.c index 1257d15978b9..3193022ffc37 100644 --- a/src/audio/module_adapter/module_adapter.c +++ b/src/audio/module_adapter/module_adapter.c @@ -255,6 +255,7 @@ __cold struct comp_dev *module_adapter_new_ext(const struct comp_driver *drv, struct userspace_context *user_ctx) { int ret; + bool module_initialised = false; struct module_config *dst; struct ipc_config_process spec = *((const struct ipc_config_process *) const_spec); @@ -364,6 +365,8 @@ __cold struct comp_dev *module_adapter_new_ext(const struct comp_driver *drv, goto err; } + module_initialised = true; + module_adapter_reset_data(dst); dev->state = COMP_STATE_READY; @@ -392,6 +395,14 @@ __cold struct comp_dev *module_adapter_new_ext(const struct comp_driver *drv, return dev; err: + /* A module's .init() can take ownership of state that outlives the module + * allocation context released below, for example the FPI sync group a host + * copier joins in copier_host_create(). Once module_init() has succeeded the + * only thing that undoes that is the module's own .free(), so run it before + * tearing the rest down, and while the DP thread is still alive. + */ + if (module_initialised) + module_free(mod); #if CONFIG_ZEPHYR_DP_SCHEDULER if (dev->task) schedule_task_free(dev->task); From f7112935008f9be6a3b5d15a6b11e074ff4a1c5e Mon Sep 17 00:00:00 2001 From: Shrushti P K Date: Fri, 4 Dec 2026 22:01:10 +0530 Subject: [PATCH 3/4] audio: copier: free the host gateway when mic privacy setup fails copier_init() calls mic_privacy_configure() after copier_host_create() has already set the host gateway up, and its error path jumps to the shared error label, which only frees cd. The host data is reclaimed with the module allocation context, but the FPI sync group the gateway may have joined is not: it lives on the system user heap and is released by its last member, so the failed copier stays a member for good and the group id cannot be reused with a different fpi_update_period_usec. Call copier_host_free() before taking the error path so the gateway, and with it the group membership, is released. Signed-off-by: Shrushti P K --- src/audio/copier/copier.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/audio/copier/copier.c b/src/audio/copier/copier.c index 79f19cec416a..47df2cabb4a7 100644 --- a/src/audio/copier/copier.c +++ b/src/audio/copier/copier.c @@ -231,6 +231,12 @@ __cold static int copier_init(struct processing_module *mod) ret = mic_privacy_configure(mod, cd); if (ret < 0) { comp_err(dev, "unable to configure mic privacy"); + /* the host gateway is already set up and may have + * joined an FPI sync group, which outlives this + * module, so release it here: the shared error path + * below only frees cd. + */ + copier_host_free(mod); goto error; } } From cb69b185026e1613220b586c0b02f613d8fbd013 Mon Sep 17 00:00:00 2001 From: Shrushti P K Date: Fri, 4 Dec 2026 22:01:36 +0530 Subject: [PATCH 4/4] audio: module_adapter: document teardown on the creation error path The creation and teardown flow only describes module_init() returning OK, so note that the steps after it can fail and that the error path runs the same teardown as comp_free(), and why module_free() has to be part of it. Signed-off-by: Shrushti P K --- src/audio/module_adapter/README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/audio/module_adapter/README.md b/src/audio/module_adapter/README.md index f59ad2b7a41b..adceb9b2033e 100644 --- a/src/audio/module_adapter/README.md +++ b/src/audio/module_adapter/README.md @@ -47,6 +47,13 @@ sequenceDiagram deactivate ModAd ``` +`module_adapter_new()` does not stop at `module_init()`: it still resets the module data and, +on IPC4, retrieves the stream params. A failure in any of those steps runs the same teardown as +`comp_free()`, `module_free()` included, because `ops->init()` may have taken ownership of state +that does not live in the module allocation context and so is not reclaimed by releasing it. A +host copier joining an FPI sync group is one such case: the group sits on the system user heap +and is freed by its last member, so `ops->free()` has to run for the membership to be dropped. + ## Configuration and Binding Flow Connection establishes buffer relationships (`comp_buffer`) connecting the component sources and sinks to the rest of the generic pipeline graph.