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; } } 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 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. 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);