diff --git a/src/audio/module_adapter/library/README.md b/src/audio/module_adapter/library/README.md index 3dd9aee14f7c..c9acfc567aa8 100644 --- a/src/audio/module_adapter/library/README.md +++ b/src/audio/module_adapter/library/README.md @@ -78,3 +78,11 @@ A crucial aspect of the userspace proxy is dynamic memory permission elevation: - Normally, the userspace module can only access its own `heap`, `data`, and `bss` partitions. - When an IPC like `GET_CONF` requests large IPC buffers, the proxy temporarily adds the hardware `MAILBOX_HOSTBOX` into the module's `k_mem_domain` using `k_mem_domain_add_partition()`. - Once the userspace thread returns, that hardware window is immediately removed from the memory domain to minimize the vulnerability window. + +### Memory Domain Lifetime + +The library manager owns the proxy. It creates the proxy, and with it the memory domain of the module, before the module adapter component, and it calls `userspace_proxy_destroy()` only after `module_adapter_free()` has returned. The module's `free()` handler, `userspace_proxy_free()`, only runs the module code. The DP thread of the module runs in the module's memory domain and it can still be alive at that point, so the domain must outlive it. + +`userspace_proxy_destroy()` releases the memory domain with `k_mem_domain_deinit()` before freeing it. The Xtensa MMU code in Zephyr links every initialized domain into a list, which is walked whenever memory is mapped or unmapped, and holds its page tables and ASID until the domain is deinitialized. `k_mem_domain_deinit()` refuses a domain that still has member threads, so the shared IPC worker thread, which stays in the domain of the module it served last, is moved back to the default domain first if another module still uses it. + +The domain object is allocated from coherent memory because that list is walked on every core. diff --git a/src/include/sof/audio/module_adapter/library/userspace_proxy.h b/src/include/sof/audio/module_adapter/library/userspace_proxy.h index 482590caf67f..820c8bcb973b 100644 --- a/src/include/sof/audio/module_adapter/library/userspace_proxy.h +++ b/src/include/sof/audio/module_adapter/library/userspace_proxy.h @@ -59,6 +59,8 @@ int userspace_proxy_create(struct userspace_context **user_ctx, const struct com /** * Destroy userspace module proxy * + * Also destroys the module's memory domain, so its DP thread must be terminated first. + * * @param drv - pointer to component driver * @param user_ctx - pointer to userspace module context */ diff --git a/src/kernel/audio/module_adapter/library/userspace_proxy.c b/src/kernel/audio/module_adapter/library/userspace_proxy.c index 37ef6ab7dcbc..a1c8faeca4dd 100644 --- a/src/kernel/audio/module_adapter/library/userspace_proxy.c +++ b/src/kernel/audio/module_adapter/library/userspace_proxy.c @@ -64,6 +64,8 @@ static inline int user_worker_get(void) static inline void user_worker_put(void) { } +static inline void user_worker_leave_domain(struct k_mem_domain *domain) { } + struct k_work_user *userspace_proxy_register_ipc_handler(struct processing_module *mod, struct k_event *event) { @@ -140,6 +142,23 @@ static void user_worker_put(void) user_stack_free(worker.stack_ptr); } } + +/* Move the worker out of the domain it served last, before that domain is destroyed. */ +static void user_worker_leave_domain(struct k_mem_domain *domain) +{ + int ret; + + if (!worker.reference_count || worker.thread_id->mem_domain_info.mem_domain != domain) + return; + + /* The worker must not run while its memory domain is reconfigured. */ + k_thread_suspend(worker.thread_id); + ret = k_mem_domain_add_thread(&k_mem_domain_default, worker.thread_id); + k_thread_resume(worker.thread_id); + + if (ret < 0) + tr_err(&userspace_proxy_tr, "Failed to move worker out of domain, error: %d", ret); +} #endif static int user_work_item_init(struct userspace_context *user_ctx, struct k_heap *user_heap) @@ -418,6 +437,28 @@ static int userspace_proxy_start_agent(struct userspace_context *user_ctx, return 0; } +/* Free a memory domain. No thread other than the worker may still be a member. */ +static void userspace_proxy_domain_free(struct k_mem_domain *domain) +{ + int ret; + + user_worker_leave_domain(domain); + + /* drop messages still queued by the module, before the domain is reused */ + ipc_user_msg_purge(domain); + + /* Zephyr walks its list of initialized domains on every map and unmap. */ + ret = k_mem_domain_deinit(domain); + if (ret == -EBUSY) { + /* A thread still runs in the domain, so it can only be leaked. */ + tr_err(&userspace_proxy_tr, "Memory domain %p is still in use, not freed", + (void *)domain); + return; + } + + rfree(domain); +} + int userspace_proxy_create(struct userspace_context **user_ctx, const struct comp_driver *drv, const struct sof_man_module *manifest, system_agent_start_fn agent_fn, const struct system_agent_params *agent_params, @@ -435,8 +476,10 @@ int userspace_proxy_create(struct userspace_context **user_ctx, const struct com context->dp_event = NULL; - /* Allocate memory domain struct */ - domain = rzalloc(SOF_MEM_FLAG_KERNEL, sizeof(*domain)); + /* Allocate memory domain struct. It is a kernel object shared between cores: Zephyr + * links it into a list that is walked on any core mapping or unmapping memory. + */ + domain = rzalloc(SOF_MEM_FLAG_KERNEL | SOF_MEM_FLAG_COHERENT, sizeof(*domain)); if (!domain) { ret = -ENOMEM; goto error; @@ -485,7 +528,7 @@ int userspace_proxy_create(struct userspace_context **user_ctx, const struct com error_work_item: user_work_item_free(context, drv->user_heap); error_dom: - rfree(domain); + userspace_proxy_domain_free(domain); error: k_heap_free(drv->user_heap, context); return ret; @@ -495,9 +538,7 @@ void userspace_proxy_destroy(const struct comp_driver *drv, struct userspace_con { tr_dbg(&userspace_proxy_tr, "userspace proxy destroy"); user_work_item_free(user_ctx, drv->user_heap); - /* drop messages still queued by the module, before the domain is reused */ - ipc_user_msg_purge(user_ctx->comp_dom); - rfree(user_ctx->comp_dom); + userspace_proxy_domain_free(user_ctx->comp_dom); k_heap_free(drv->user_heap, user_ctx); } @@ -622,29 +663,27 @@ static int userspace_proxy_reset(struct processing_module *mod) * Queue module free() operation and return its result. * Module free() code is performed in user workqueue. * + * The proxy is destroyed later by the library manager, once the DP thread is terminated. + * * @param mod - pointer to processing module structure. * @return 0 for success, error otherwise. */ static int userspace_proxy_free(struct processing_module *mod) { struct module_params *params = user_work_get_params(mod->user_ctx); - int ret = 0; + int ret; comp_dbg(mod->dev, "start"); - if (mod->user_ctx->interface->free) { - ret = userspace_proxy_invoke(mod->user_ctx, USER_PROXY_MOD_CMD_FREE, false); - if (ret) - return ret; - ret = params->status; - } + if (!mod->user_ctx->interface->free) + return 0; - /* Destroy workqueue if this was last active userspace module */ - userspace_proxy_destroy(mod->dev->drv, mod->user_ctx); - mod->user_ctx = NULL; + ret = userspace_proxy_invoke(mod->user_ctx, USER_PROXY_MOD_CMD_FREE, false); + if (ret) + return ret; /* Return status from module code operation. */ - return ret; + return params->status; } /** diff --git a/src/kernel/library_manager/lib_manager.c b/src/kernel/library_manager/lib_manager.c index 49a004c5ee23..e6ed55dc0150 100644 --- a/src/kernel/library_manager/lib_manager.c +++ b/src/kernel/library_manager/lib_manager.c @@ -856,6 +856,10 @@ __cold static struct comp_dev *lib_manager_module_create(const struct comp_drive __cold static void lib_manager_module_free(struct comp_dev *dev) { uint32_t component_id = dev->ipc_config.id; +#if CONFIG_SOF_USERSPACE_PROXY + const struct comp_driver *drv = dev->drv; + struct userspace_context *userspace = comp_mod(dev)->user_ctx; +#endif /* CONFIG_SOF_USERSPACE_PROXY */ int ret; assert_can_be_cold(); @@ -863,6 +867,12 @@ __cold static void lib_manager_module_free(struct comp_dev *dev) /* This call invalidates dev, mod and config pointers! */ module_adapter_free(dev); +#if CONFIG_SOF_USERSPACE_PROXY + /* The DP thread is terminated now, so the memory domain of the module can go. */ + if (userspace) + userspace_proxy_destroy(drv, userspace); +#endif /* CONFIG_SOF_USERSPACE_PROXY */ + /* Free module resources allocated in L2 memory. */ ret = lib_manager_free_module(component_id); if (ret < 0)