From 3be0b6326ca17fced2313374a3d80e95f7d40017 Mon Sep 17 00:00:00 2001 From: Adrian Bonislawski Date: Wed, 25 Nov 2026 17:48:19 +0100 Subject: [PATCH 1/2] userspace_proxy: allocate the memory domain in coherent memory The Xtensa MMU code in Zephyr links the memory domains into a list that is walked on whichever core maps or unmaps memory. The domain of a userspace module was allocated through the cached alias, so another core could see stale contents. Allocate it from coherent memory, as the application variant of the DP scheduler already does. Signed-off-by: Adrian Bonislawski --- src/kernel/audio/module_adapter/library/userspace_proxy.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/kernel/audio/module_adapter/library/userspace_proxy.c b/src/kernel/audio/module_adapter/library/userspace_proxy.c index 37ef6ab7dcbc..aa922c3ed9dd 100644 --- a/src/kernel/audio/module_adapter/library/userspace_proxy.c +++ b/src/kernel/audio/module_adapter/library/userspace_proxy.c @@ -435,8 +435,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; From 91dbd73ac98a4386b6b589aad8bd3cde5dfd1cee Mon Sep 17 00:00:00 2001 From: Adrian Bonislawski Date: Wed, 25 Nov 2026 17:48:34 +0100 Subject: [PATCH 2/2] userspace_proxy: deinitialize the memory domain before freeing it userspace_proxy_destroy() freed the memory domain of a module without k_mem_domain_deinit(). The domain stayed on the Xtensa MMU domain list in Zephyr, so the DSP panicked once the freed memory was reused. Deinitialize the domain before freeing it. This fails while the domain has member threads, so destroy the proxy in the library manager, after module_adapter_free(), and move a surviving IPC worker thread back to the default domain first. Signed-off-by: Adrian Bonislawski --- src/audio/module_adapter/library/README.md | 8 +++ .../module_adapter/library/userspace_proxy.h | 2 + .../module_adapter/library/userspace_proxy.c | 67 ++++++++++++++----- src/kernel/library_manager/lib_manager.c | 10 +++ 4 files changed, 72 insertions(+), 15 deletions(-) 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 aa922c3ed9dd..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, @@ -487,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; @@ -497,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); } @@ -624,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)