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
6 changes: 6 additions & 0 deletions src/audio/copier/copier.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
24 changes: 16 additions & 8 deletions src/audio/copier/copier_host.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
//
// Author: Baofeng Tian <baofeng.tian@intel.com>

#include <rtos/alloc.h>
#include <sof/lib/cold.h>
#include <sof/tlv.h>
#include <sof/trace/trace.h>
Expand Down Expand Up @@ -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();

Expand All @@ -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);
Comment on lines +70 to +72

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Right, both of those paths skip host teardown and now that the group is outside the module pool the membership just stays. Fixed in two commits:

copier_init() now calls copier_host_free() before taking the shared error label when mic_privacy_configure() fails, so the gateway and the group reference go away.

module_adapter_new_ext() tracks whether module_init() succeeded and runs module_free() on the err path when it did, before schedule_task_free() and the allocation context release, same order as module_adapter_free(). That covers module_adapter_params() and anything else added after module_init() there, not just the copier.

Re-ran the init/delete model with both failure points added, including re-joining the same group id with a different fpi_update_period_usec afterwards: no stale member and the list ends empty.

if (!group) {
comp_err(parent_dev, "Failed to alloc memory for new group");
return -ENOMEM;
Expand All @@ -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();

Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/audio/copier/host_copier.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions src/audio/module_adapter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
11 changes: 11 additions & 0 deletions src/audio/module_adapter/module_adapter.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Loading