From 5f8950c59b8d51cfe136529a735a27d17a937e33 Mon Sep 17 00:00:00 2001 From: Kai Vehmanen Date: Fri, 4 Dec 2026 10:25:07 +0200 Subject: [PATCH 1/8] vregion: do not read the user heap counters in kernel threads Telemetry reads the usage counters that the user-mode allocator of a pipeline region keeps at the start of the interim range. With telemetry enabled for user-space LL, the telemetry work, a kernel thread, read them in region memory that the LL and IPC user threads use. Kernel threads must not access the memory of user threads: that leaves kernel-ring DTLB entries, on which the user threads of the same core then fault. Read the counters only in system calls of user threads, which map the region, and keep the last values otherwise. Sample all user-mode region heaps where the LL user heap counters are sampled: every 1024 LL ticks, when LL tasks start and stop, and after the IPC commands. The sampling skips the regions whose lock is busy, so the LL thread is not held up. The test makes the IPC user thread copy the counters before it checks what telemetry reports. Signed-off-by: Kai Vehmanen --- src/include/sof/debug/telemetry/telemetry.h | 12 +++++ src/include/sof/userspace/README.md | 15 +++--- src/kernel/debug/telemetry/telemetry.c | 3 ++ zephyr/lib/vregion.c | 56 ++++++++++++++++----- zephyr/test/userspace/test_ll_user_heap.c | 9 +++- 5 files changed, 74 insertions(+), 21 deletions(-) diff --git a/src/include/sof/debug/telemetry/telemetry.h b/src/include/sof/debug/telemetry/telemetry.h index e933c791f273..4c823f3ec274 100644 --- a/src/include/sof/debug/telemetry/telemetry.h +++ b/src/include/sof/debug/telemetry/telemetry.h @@ -431,6 +431,18 @@ void telemetry_sample_ll_user_heap(void); static inline void telemetry_sample_ll_user_heap(void) {} #endif +#if defined(CONFIG_SOF_TELEMETRY_VREGION) && defined(CONFIG_SOF_VREGION_USER_HEAP) +/** + * \brief Copy the counters of the user-mode region heaps for telemetry. + * + * Must be called in a system call of an LL or IPC user thread: kernel threads + * must not access the region memory. + */ +void telemetry_sample_vregion_user_heaps(void); +#else +static inline void telemetry_sample_vregion_user_heaps(void) {} +#endif + #ifdef CONFIG_SOF_TELEMETRY_VREGION void telemetry_collect_vpage_stats(struct sof_tel_vregion_section *vsec); void telemetry_collect_vregion_stats(struct sof_tel_vregion_section *vsec); diff --git a/src/include/sof/userspace/README.md b/src/include/sof/userspace/README.md index 7d2a5cb915a7..5d1f8faeccad 100644 --- a/src/include/sof/userspace/README.md +++ b/src/include/sof/userspace/README.md @@ -250,13 +250,14 @@ the rest of the region for the interim heap, and kernel code, including the `vre syscalls, refuses to allocate from or free to that range and logs the caller. When the pipeline is complete, the IPC user thread gets the range with the `vregion_interim_get()` syscall, which only the LL and IPC user threads may call, and initialises the heap in it. The -heap header at the start of the range holds the usage counters, which telemetry reads as -numbers each time it collects the region data, and the `sys_heap` object. The user threads -find the header of a region in a table in the system user partition, under a `sys_mutex` -there, so `sof_ctx_alloc()`, `sof_ctx_free()` and the object pools allocate from the heap -with no syscall, through `vregion_user_alloc_align()` and `vregion_user_free()`. The heap is -removed when the pipeline is freed, after its modules. The kernel still runs the interim heap -of the DP module regions, which are mapped into the LL memory domain too. +heap header at the start of the range holds the `sys_heap` object and the usage counters. +Telemetry copies the counters as numbers in system calls of the LL and IPC user threads, as +kernel threads must not access the region memory. The user threads find the header of a +region in a table in the system user partition, under a `sys_mutex` there, so +`sof_ctx_alloc()`, `sof_ctx_free()` and the object pools allocate from the heap with no +syscall, through `vregion_user_alloc_align()` and `vregion_user_free()`. The heap is removed +when the pipeline is freed, after its modules. The kernel still runs the interim heap of the +DP module regions, which are mapped into the LL memory domain too. The fast_get area holds the `fast_get()` copies of the LL modules, such as SRC coefficients. LL and IPC user threads get them with the `fast_get_ll()` and `fast_put_ll()` syscalls, and all diff --git a/src/kernel/debug/telemetry/telemetry.c b/src/kernel/debug/telemetry/telemetry.c index 958e77af4155..6bef627854a3 100644 --- a/src/kernel/debug/telemetry/telemetry.c +++ b/src/kernel/debug/telemetry/telemetry.c @@ -1293,6 +1293,7 @@ static void telemetry_update(uint32_t begin_stamp, uint32_t current_stamp) if ((ticks & 0x3ff) == 0) { /* in a system call of the LL user thread with SOF_USERSPACE_LL */ telemetry_sample_ll_user_heap(); + telemetry_sample_vregion_user_heaps(); telemetry_bg_reschedule(K_NO_WAIT); } } @@ -1322,6 +1323,7 @@ void z_impl_telemetry_notify_ll_task_count(int core, unsigned int n_tasks) /* streams allocate and free LL user memory when they start and stop */ telemetry_sample_ll_user_heap(); + telemetry_sample_vregion_user_heaps(); tel_state()->ll_task_count[core] = count; if (telemetry_ext_ready && wnd) { @@ -1353,6 +1355,7 @@ void z_impl_telemetry_notify_ll_task_count(int core, unsigned int n_tasks) void z_impl_telemetry_sync_user(void) { telemetry_sample_ll_user_heap(); + telemetry_sample_vregion_user_heaps(); } #ifdef CONFIG_USERSPACE diff --git a/zephyr/lib/vregion.c b/zephyr/lib/vregion.c index d5a850cfa921..04ad84730bc7 100644 --- a/zephyr/lib/vregion.c +++ b/zephyr/lib/vregion.c @@ -14,6 +14,9 @@ #include #include #include +#ifdef CONFIG_SOF_VREGION_USER_HEAP +#include +#endif #include #include #include @@ -142,7 +145,25 @@ static struct vregion *tel_vregions[SOF_TEL_MAX_VREGIONS]; static struct sof_tel_vregion_info tel_vregion_snap[SOF_TEL_MAX_VREGIONS]; static uint16_t tel_vregions_created; -static void vregion_tel_fill_info(struct vregion *vr, struct sof_tel_vregion_info *info) +/* + * The counters of a user-mode heap are in the region memory, which kernel + * threads such as the telemetry work must not access: that leaves kernel-ring + * DTLB entries, on which the user threads of the same core then fault. So + * read them only in the system calls of the user threads, which map the + * region, and keep the last values otherwise. + */ +static bool vregion_tel_user_readable(void) +{ +#ifdef CONFIG_SOF_VREGION_USER_HEAP + return k_is_in_user_syscall(); +#else + return false; +#endif +} + +/* @prev: the last info of vr, for the values that can not be read now, or NULL */ +static void vregion_tel_fill_info(struct vregion *vr, struct sof_tel_vregion_info *info, + const struct sof_tel_vregion_info *prev) { memset(info, 0, sizeof(*info)); info->base_va = (uint32_t)POINTER_TO_UINT(vr->base); @@ -152,7 +173,8 @@ static void vregion_tel_fill_info(struct vregion *vr, struct sof_tel_vregion_inf info->lifetime_size_bytes = (uint32_t)vr->lifetime.size; info->lifetime_used_bytes = (uint32_t)MIN(vr->lifetime.used, vr->lifetime.size); info->lifetime_free_count = (uint16_t)MIN(vr->lifetime.free_count, UINT16_MAX); - if (vr->type == VREGION_MEM_TYPE_INTERIM && vr->flags & VREGION_FLAG_USER_HEAP) { + if (vr->type == VREGION_MEM_TYPE_INTERIM && vr->flags & VREGION_FLAG_USER_HEAP && + vregion_tel_user_readable()) { /* user-writable counters at the start of the range, only copied as numbers */ const volatile struct vregion_user_heap_stats *stats = sys_cache_uncached_ptr_get((__sparse_force void __sparse_cache *) @@ -161,6 +183,10 @@ static void vregion_tel_fill_info(struct vregion *vr, struct sof_tel_vregion_inf info->interim_alloc_bytes = (uint32_t)allocated; info->interim_free_kb = (uint16_t)((vr->interim_size - allocated) >> 10); + } else if (vr->type == VREGION_MEM_TYPE_INTERIM && vr->flags & VREGION_FLAG_USER_HEAP && + prev) { + info->interim_alloc_bytes = prev->interim_alloc_bytes; + info->interim_free_kb = prev->interim_free_kb; } #if CONFIG_SYS_HEAP_RUNTIME_STATS if (vr->type == VREGION_MEM_TYPE_INTERIM && !(vr->flags & VREGION_FLAG_USER_HEAP) && @@ -181,9 +207,9 @@ static void vregion_tel_sync_locked(struct vregion *vr) { struct sof_tel_vregion_info info; - vregion_tel_fill_info(vr, &info); for (int i = 0; i < SOF_TEL_MAX_VREGIONS; i++) { if (tel_vregions[i] == vr) { + vregion_tel_fill_info(vr, &info, &tel_vregion_snap[i]); tel_vregion_snap[i] = info; break; } @@ -194,7 +220,7 @@ static void vregion_tel_register(struct vregion *vr) { struct sof_tel_vregion_info info; - vregion_tel_fill_info(vr, &info); + vregion_tel_fill_info(vr, &info, NULL); tel_vregions_created++; for (int i = 0; i < SOF_TEL_MAX_VREGIONS; i++) { if (!tel_vregions[i]) { @@ -939,24 +965,31 @@ int vregion_pinned_put(struct vregion *vr) #endif #ifdef CONFIG_SOF_TELEMETRY_VREGION +#ifdef CONFIG_SOF_VREGION_USER_HEAP /* - * The user threads run the user-mode heaps without system calls, so read - * their counters when telemetry is collected. The list lock keeps the - * listed regions, and so their memory, from being freed meanwhile. + * The user threads run the user-mode heaps without system calls, so their + * system calls sample the counters from time to time, see + * vregion_tel_user_readable(). The list lock keeps the listed regions, and so + * their memory, from being freed meanwhile. A busy lock skips the sample, so + * that the LL thread is not held up. */ -static void vregion_tel_sync_user_heaps(void) +void telemetry_sample_vregion_user_heaps(void) { struct vregion *vr; - k_mutex_lock(&vregion_list_lock, K_FOREVER); + if (k_mutex_lock(&vregion_list_lock, K_NO_WAIT)) + return; + SYS_DLIST_FOR_EACH_CONTAINER(&vregion_list, vr, node) { - k_mutex_lock(&vr->lock, K_FOREVER); + if (k_mutex_lock(&vr->lock, K_NO_WAIT)) + continue; if (vr->type == VREGION_MEM_TYPE_INTERIM && vr->flags & VREGION_FLAG_USER_HEAP) vregion_tel_sync_locked(vr); k_mutex_unlock(&vr->lock); } k_mutex_unlock(&vregion_list_lock); } +#endif void telemetry_collect_vregion_stats(struct sof_tel_vregion_section *vsec) { @@ -965,9 +998,6 @@ void telemetry_collect_vregion_stats(struct sof_tel_vregion_section *vsec) if (!vsec) return; - if (IS_ENABLED(CONFIG_SOF_VREGION_USER_HEAP)) - vregion_tel_sync_user_heaps(); - for (int i = 0; i < SOF_TEL_MAX_VREGIONS; i++) { if (!tel_vregions[i]) { memset(&vsec->vregions[i], 0, sizeof(vsec->vregions[i])); diff --git a/zephyr/test/userspace/test_ll_user_heap.c b/zephyr/test/userspace/test_ll_user_heap.c index da0adde0f6a1..b60599138ba5 100644 --- a/zephyr/test/userspace/test_ll_user_heap.c +++ b/zephyr/test/userspace/test_ll_user_heap.c @@ -571,6 +571,8 @@ enum vregion_heap_test_case { VHEAP_ALLOC_CORRUPTED, /* get the interim range */ VHEAP_INTERIM_GET, + /* copy the counters for telemetry */ + VHEAP_TEL_SYNC, }; static void check_vheap_alloc(bool coherent, size_t bytes, size_t align) @@ -652,6 +654,9 @@ static void vheap_user_function(void *p1, void *p2, void *p3) case VHEAP_INTERIM_GET: (void)vregion_interim_get(test_vreg, &start, &size); break; + case VHEAP_TEL_SYNC: + telemetry_sync_user(); + return; default: zassert_unreachable("unknown test case"); } @@ -739,6 +744,8 @@ static uint32_t tel_interim_alloc_bytes(void) { int i; + /* the IPC user thread copies the counters, the kernel does not read them */ + run_vheap_case(VHEAP_TEL_SYNC, true, false); telemetry_collect_vregion_stats(&tel_vsec); for (i = 0; i < SOF_TEL_MAX_VREGIONS; i++) if (tel_vsec.vregions[i].base_va == test_vreg_base) @@ -768,7 +775,7 @@ ZTEST(userspace_vregion_user_heap, kernel_use_refused) allocated = stats->allocated_bytes; zassert_true(allocated >= 64); #if CONFIG_SOF_TELEMETRY_VREGION - /* the allocation made no system call, telemetry reads it anyway */ + /* the allocation made no system call, the user threads copy the counters */ zassert_equal(tel_interim_alloc_bytes(), allocated); #endif From 7503090a017c6e99fe6182c7ecd6799d08085465 Mon Sep 17 00:00:00 2001 From: Kai Vehmanen Date: Fri, 4 Dec 2026 10:27:37 +0200 Subject: [PATCH 2/8] pipeline: fail the completion if no user-mode region heap is left pipeline_complete() only warned when vregion_user_heap_add() failed and marked the pipeline ready. With the table of user-mode region heaps full, -ENOSPC, the region then had no heap at all: the kernel refuses its interim range too, so every allocation after the completion failed, starting with the pipeline task, although the region had free memory. Fail the completion in that case, so that the host gets the error at the state change. A region too small for a heap is still not fatal, as with the kernel-run interim heap. Signed-off-by: Kai Vehmanen --- src/audio/pipeline/pipeline-graph.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/audio/pipeline/pipeline-graph.c b/src/audio/pipeline/pipeline-graph.c index cf6e97b23b8d..b4b84dba13b0 100644 --- a/src/audio/pipeline/pipeline-graph.c +++ b/src/audio/pipeline/pipeline-graph.c @@ -502,10 +502,22 @@ int pipeline_complete(struct pipeline *p, struct comp_dev *source, p->sink_comp = sink; if (p->alloc->vreg) { + int heap_ret; + vregion_set_interim(p->alloc->vreg); - /* like a region too small for a kernel interim heap, not fatal here */ - if (vregion_user_heap_add(p->alloc->vreg) < 0) + /* + * Like a region too small for a kernel interim heap, a too small + * one is not fatal here. But without a free entry in the table of + * the user-mode heaps, the interim range of the region could not + * be used at all, also by the kernel: fail the completion. + */ + heap_ret = vregion_user_heap_add(p->alloc->vreg); + if (heap_ret == -ENOSPC) { + pipe_err(p, "no user-mode heap left for the region"); + return heap_ret; + } + if (heap_ret < 0) pipe_warn(p, "no interim heap"); } From 9f6f6eef2266574244c84b211a2187165d6bddc0 Mon Sep 17 00:00:00 2001 From: Kai Vehmanen Date: Wed, 25 Nov 2026 12:26:43 +0200 Subject: [PATCH 3/8] dp_schedule: take the DP module region from the kernel scheduler_dp_task_init() built the heap partitions of the DP domain from mod_heap_info(), which follows the allocation context pointer in module memory to the region. LL and IPC user threads can write that memory, so they could make the kernel map other memory into the DP domain. The kernel creates the region of a DP module itself, in module_adapter_mem_alloc(). Pass it on through pipeline_comp_dp_task_init() to scheduler_dp_task_init(), which keeps a reference in the kernel-only task record and takes the partition bounds from the kernel-only struct vregion. The reference is dropped when the task is freed. The free system call checks the caller's access to that region too. Modules without a region keep using mod_heap_info(). Signed-off-by: Kai Vehmanen --- src/audio/module_adapter/module_adapter.c | 12 ++++++--- src/audio/pipeline/pipeline-schedule.c | 4 +-- src/include/sof/audio/pipeline.h | 4 ++- src/include/sof/schedule/dp_schedule.h | 5 ++++ .../zephyr_dp_schedule_application-kernel.c | 27 +++++++++++++------ .../zephyr_dp_schedule_thread-kernel.c | 1 + 6 files changed, 39 insertions(+), 14 deletions(-) diff --git a/src/audio/module_adapter/module_adapter.c b/src/audio/module_adapter/module_adapter.c index 94d0f15c7a32..894e86e8a826 100644 --- a/src/audio/module_adapter/module_adapter.c +++ b/src/audio/module_adapter/module_adapter.c @@ -80,7 +80,8 @@ __cold static struct processing_module *module_adapter_mem_alloc(const struct comp_driver *drv, const struct comp_ipc_config *config, const struct module_ext_init_data *ext_init, - struct mod_alloc_ctx *ppl_alloc) + struct mod_alloc_ctx *ppl_alloc, + struct vregion **dp_vreg) { struct k_heap *mod_heap; struct vregion *mod_vreg; @@ -186,6 +187,9 @@ struct processing_module *module_adapter_mem_alloc(const struct comp_driver *drv mod->dev = dev; dev->mod = mod; + /* the region of a DP module, from here and not from module memory */ + *dp_vreg = use_ppl_alloc ? NULL : mod_vreg; + return mod; edev: @@ -296,7 +300,9 @@ __cold struct comp_dev *module_adapter_new_ext(const struct comp_driver *drv, ppl_alloc = ipc_pipe->pipeline->alloc; #endif - struct processing_module *mod = module_adapter_mem_alloc(drv, config, ext_init, ppl_alloc); + struct vregion *dp_vreg; + struct processing_module *mod = module_adapter_mem_alloc(drv, config, ext_init, ppl_alloc, + &dp_vreg); if (!mod) return NULL; @@ -338,7 +344,7 @@ __cold struct comp_dev *module_adapter_new_ext(const struct comp_driver *drv, /* create a task for DP processing */ if (config->proc_domain == COMP_PROCESSING_DOMAIN_DP) { /* All data allocated, create a thread */ - ret = pipeline_comp_dp_task_init(dev); + ret = pipeline_comp_dp_task_init(dev, dp_vreg); if (ret) { comp_cl_err(drv, "DP task creation failed with error %d.", ret); goto err; diff --git a/src/audio/pipeline/pipeline-schedule.c b/src/audio/pipeline/pipeline-schedule.c index 63b9fd979088..82916b9bee83 100644 --- a/src/audio/pipeline/pipeline-schedule.c +++ b/src/audio/pipeline/pipeline-schedule.c @@ -591,7 +591,7 @@ static enum task_state dp_task_run(void *data) return SOF_TASK_STATE_RESCHEDULE; } -int pipeline_comp_dp_task_init(struct comp_dev *comp) +int pipeline_comp_dp_task_init(struct comp_dev *comp, struct vregion *vreg) { /* DP tasks are guaranteed to have a module_adapter */ struct processing_module *mod = comp_mod(comp); @@ -620,7 +620,7 @@ int pipeline_comp_dp_task_init(struct comp_dev *comp) CONFIG_ZEPHYR_DP_SCHEDULER_MIN_STACK_SIZE); } - return scheduler_dp_task_init(&comp->task, SOF_UUID(dp_task_uuid), &ops, mod, + return scheduler_dp_task_init(&comp->task, SOF_UUID(dp_task_uuid), &ops, mod, vreg, comp->ipc_config.core, stack_size, flags); } #endif /* CONFIG_ZEPHYR_DP_SCHEDULER */ diff --git a/src/include/sof/audio/pipeline.h b/src/include/sof/audio/pipeline.h index 9409eaec0236..198c49980d7f 100644 --- a/src/include/sof/audio/pipeline.h +++ b/src/include/sof/audio/pipeline.h @@ -33,6 +33,7 @@ struct comp_buffer; struct comp_dev; struct ipc; +struct vregion; struct ipc_msg; struct k_heap; struct mod_alloc_ctx; @@ -411,9 +412,10 @@ void pipeline_comp_ll_task_free(struct pipeline *p); /** * \brief Initializes a data processing (DP) task for a component. * \param[in,out] comp Component device for which the task is created. + * \param[in] vreg Region of the module created by the kernel, or NULL. * \return 0 on success, or negative error code on failure. */ -int pipeline_comp_dp_task_init(struct comp_dev *comp); +int pipeline_comp_dp_task_init(struct comp_dev *comp, struct vregion *vreg); /** * \brief Schedules a pipeline copy operation to run after a delay. diff --git a/src/include/sof/schedule/dp_schedule.h b/src/include/sof/schedule/dp_schedule.h index ab511e125f65..88b86cc90bab 100644 --- a/src/include/sof/schedule/dp_schedule.h +++ b/src/include/sof/schedule/dp_schedule.h @@ -44,6 +44,7 @@ struct processing_module; struct module_ext_init_data; +struct vregion; /** * @brief Initialize the Data Processing (DP) scheduler on the calling core. @@ -63,6 +64,9 @@ int scheduler_dp_init(void); * @param[in] uid UUID identifying the task/module. * @param[in] ops Task operations callbacks (@ref task_ops). * @param[in] mod Processing module descriptor bound to this task. + * @param[in] vreg Region that the kernel created for the module, or NULL if + * the module has none. Not read back from module memory, which + * user threads can write. * @param[in] core DSP core affinity for the task thread. * @param[in] stack_size Stack byte size for the Zephyr worker thread. * @param[in] options Task configuration flags. @@ -72,6 +76,7 @@ int scheduler_dp_task_init(struct task **task, const struct sof_uuid_entry *uid, const struct task_ops *ops, struct processing_module *mod, + struct vregion *vreg, uint16_t core, size_t stack_size, uint32_t options); diff --git a/src/kernel/schedule/zephyr_dp_schedule_application-kernel.c b/src/kernel/schedule/zephyr_dp_schedule_application-kernel.c index 5355fcf5412f..ba4cc0e16dc8 100644 --- a/src/kernel/schedule/zephyr_dp_schedule_application-kernel.c +++ b/src/kernel/schedule/zephyr_dp_schedule_application-kernel.c @@ -66,6 +66,7 @@ struct scheduler_dp_task_kobj { struct ipc4_flat *flat; /* IPC parameter area in task memory */ void *config_data; /* LARGE_CONFIG_GET reply buffer in task memory */ struct k_mem_domain *mdom; + struct vregion *vreg; /* module region, a reference, or NULL */ struct k_mem_partition mpart[SOF_DP_PART_TYPE_COUNT]; uint32_t comp_id; uint16_t core; @@ -223,6 +224,7 @@ static bool scheduler_dp_internal_free_kobj(struct task *task, scheduler_dp_domain_free(kobj->mod, kobj->mdom, kobj->mpart, kobj->comp_id); mod_free(kobj->mod, container_of(task, struct scheduler_dp_task_memory, task)); + vregion_put(kobj->vreg); rfree(kobj); return true; @@ -251,7 +253,8 @@ static void scheduler_dp_thread_name_set(k_tid_t thread_id, struct processing_mo /* Called only in IPC context in kernel mode (this can change) */ int scheduler_dp_task_init(struct task **task, const struct sof_uuid_entry *uid, const struct task_ops *ops, struct processing_module *mod, - uint16_t core, size_t stack_size, uint32_t options) + struct vregion *vreg, uint16_t core, size_t stack_size, + uint32_t options) { k_thread_stack_t *p_stack; struct scheduler_dp_task_memory *task_memory; @@ -321,6 +324,7 @@ int scheduler_dp_task_init(struct task **task, const struct sof_uuid_entry *uid, kobj->task = ptask; kobj->mod = mod; + kobj->vreg = vregion_get(vreg); kobj->core = core; comp_id = mod->dev->ipc_config.id; kobj->comp_id = comp_id; @@ -418,8 +422,14 @@ int scheduler_dp_task_init(struct task **task, const struct sof_uuid_entry *uid, goto e_thread; } - /* Module heap partition */ - mod_heap_info(mod, &size, &start); + /* + * Module heap partition. Take the region bounds from the kernel-only + * struct vregion, not through the allocation context in module memory. + */ + if (kobj->vreg) + vregion_mem_info(kobj->vreg, &size, &start); + else + mod_heap_info(mod, &size, &start); kobj->mpart[SOF_DP_PART_HEAP] = (struct k_mem_partition){ .start = (uintptr_t)sys_cache_uncached_ptr_get((void *)start), .size = size, @@ -491,6 +501,7 @@ int scheduler_dp_task_init(struct task **task, const struct sof_uuid_entry *uid, e_config: mod_free(mod, ipc_config_data); e_kobj: + vregion_put(kobj->vreg); rfree(kobj); e_stack: user_stack_free(p_stack); @@ -615,7 +626,7 @@ void scheduler_dp_test_record_del(struct task *task) * supervisor mode. They are user-writable, so at least check that they * point to memory the caller can access. */ -static void scheduler_dp_mod_vrfy(struct processing_module *mod) +static void scheduler_dp_mod_vrfy(struct processing_module *mod, struct vregion *vreg) { K_OOPS(K_SYSCALL_MEMORY_WRITE(mod, sizeof(*mod))); K_OOPS(K_SYSCALL_MEMORY_WRITE(mod->dev, sizeof(*mod->dev))); @@ -624,7 +635,7 @@ static void scheduler_dp_mod_vrfy(struct processing_module *mod) struct mod_alloc_ctx *alloc = mod->priv.resources.alloc; K_OOPS(K_SYSCALL_MEMORY_WRITE(alloc, sizeof(*alloc))); - if (alloc->heap) { + if (alloc->heap && !vreg) { size_t h_size = 0; uintptr_t h_start; @@ -632,11 +643,11 @@ static void scheduler_dp_mod_vrfy(struct processing_module *mod) if (h_size) K_OOPS(K_SYSCALL_MEMORY_WRITE(h_start, h_size)); } - if (alloc->vreg) { + if (vreg) { size_t vr_size = 0; uintptr_t vr_start; - vregion_mem_info(alloc->vreg, &vr_size, &vr_start); + vregion_mem_info(vreg, &vr_size, &vr_start); K_OOPS(K_SYSCALL_MEMORY_WRITE(vr_start, vr_size)); } } @@ -666,7 +677,7 @@ void z_vrfy_scheduler_dp_internal_free(struct task *task) */ K_OOPS(K_SYSCALL_OBJ(kobj.event, K_OBJ_EVENT)); K_OOPS(K_SYSCALL_OBJ_INIT(kobj.thread, K_OBJ_THREAD)); - scheduler_dp_mod_vrfy(kobj.mod); + scheduler_dp_mod_vrfy(kobj.mod, kobj.vreg); K_OOPS(!scheduler_dp_internal_free_kobj(task, &kobj)); } diff --git a/src/kernel/schedule/zephyr_dp_schedule_thread-kernel.c b/src/kernel/schedule/zephyr_dp_schedule_thread-kernel.c index 9968b26c9ac6..60b05be13d3d 100644 --- a/src/kernel/schedule/zephyr_dp_schedule_thread-kernel.c +++ b/src/kernel/schedule/zephyr_dp_schedule_thread-kernel.c @@ -38,6 +38,7 @@ int scheduler_dp_task_init(struct task **task, const struct sof_uuid_entry *uid, const struct task_ops *ops, struct processing_module *mod, + struct vregion *vreg, uint16_t core, size_t stack_size, uint32_t options) From 9050d665dabd91b114be0a27212c30fa37a88e03 Mon Sep 17 00:00:00 2001 From: Kai Vehmanen Date: Wed, 25 Nov 2026 12:27:25 +0200 Subject: [PATCH 4/8] vregion: record the DP thread that owns a region Add vregion_owner_set(), which records the owner thread of a region in the kernel-only struct vregion. The DP scheduler sets the DP thread as the owner of its module region right before the thread starts, and clears it when the task is freed, after the thread is aborted. The owner is not used yet. It lets the vregion system calls tell the DP thread of a module from other DP threads. Signed-off-by: Kai Vehmanen --- src/include/sof/lib/vregion.h | 14 ++++++++++++++ .../zephyr_dp_schedule_application-kernel.c | 4 ++++ zephyr/lib/vregion.c | 12 ++++++++++++ 3 files changed, 30 insertions(+) diff --git a/src/include/sof/lib/vregion.h b/src/include/sof/lib/vregion.h index d7d007773b46..5209364a3777 100644 --- a/src/include/sof/lib/vregion.h +++ b/src/include/sof/lib/vregion.h @@ -27,6 +27,7 @@ extern "C" { * @brief Virtual region descriptor managing lifetime and interim heap partitions. */ struct vregion; +struct k_thread; /** * @brief Memory allocation modes within a virtual memory region. @@ -103,6 +104,18 @@ __syscall struct vregion *vregion_create_map(uintptr_t *vreg_start, size_t *vreg */ __syscall void vregion_set_interim(struct vregion *vr); +/** + * @brief Record the user thread that owns a virtual region. + * + * Kernel code only. The owner is the DP thread of the module that the region + * belongs to. It may use the region through the vregion system calls, see + * vregion_owner_check(). The owner is kept in the kernel-only struct vregion. + * + * @param[in] vr Pointer to the virtual region instance, may be NULL. + * @param[in] thread The owner thread, or NULL to clear the owner. + */ +void vregion_owner_set(struct vregion *vr, struct k_thread *thread); + /** * @brief Get the interim range of a virtual region with VREGION_FLAG_USER_HEAP. * @@ -380,6 +393,7 @@ static inline struct vregion *vregion_create_map(uintptr_t *vreg_start, size_t * return NULL; } static inline void vregion_set_interim(struct vregion *vr) {} +static inline void vregion_owner_set(struct vregion *vr, struct k_thread *thread) {} static inline int vregion_interim_get(struct vregion *vr, uintptr_t *start, size_t *size) { return -EPERM; diff --git a/src/kernel/schedule/zephyr_dp_schedule_application-kernel.c b/src/kernel/schedule/zephyr_dp_schedule_application-kernel.c index ba4cc0e16dc8..d8cdbf45bf3a 100644 --- a/src/kernel/schedule/zephyr_dp_schedule_application-kernel.c +++ b/src/kernel/schedule/zephyr_dp_schedule_application-kernel.c @@ -219,6 +219,7 @@ static bool scheduler_dp_internal_free_kobj(struct task *task, /* normally already terminated by scheduler_dp_task_free(), no-op then */ k_thread_abort(kobj->thread); + vregion_owner_set(kobj->vreg, NULL); k_object_free(kobj->event); k_object_free(kobj->thread); scheduler_dp_domain_free(kobj->mod, kobj->mdom, kobj->mpart, kobj->comp_id); @@ -476,6 +477,9 @@ int scheduler_dp_task_init(struct task **task, const struct sof_uuid_entry *uid, goto e_dom; } + /* the DP thread may use its region through the vregion system calls */ + vregion_owner_set(kobj->vreg, thread_id); + /* publish the record before the thread can run */ dp_task_kobj_add(kobj); diff --git a/zephyr/lib/vregion.c b/zephyr/lib/vregion.c index 04ad84730bc7..c937b565e9a7 100644 --- a/zephyr/lib/vregion.c +++ b/zephyr/lib/vregion.c @@ -114,6 +114,7 @@ struct vregion { unsigned int use_count; /* references held by kernel and user code */ unsigned int pins; /* system calls in progress, see vregion_verify_pin() */ struct k_mem_domain *domain; + struct k_thread *owner; /* DP thread of the module, see vregion_owner_set() */ /* current allocation mode */ enum vregion_mem_type type; /* LIFETIME at creation, switch to INTERIM */ @@ -305,6 +306,7 @@ __cold struct vregion *vregion_create(size_t memsize) vr->size = total_size; vr->pages = pages; vr->domain = NULL; + vr->owner = NULL; /* lifetime linear allocator starts at the beginning of the vregion memory */ vr->lifetime.base = vregion_base; @@ -568,6 +570,16 @@ static void interim_heap_init(struct vregion *vr) vr->lifetime.used = (uint8_t *)vr->lifetime.ptr - (uint8_t *)vr->lifetime.base; } +void vregion_owner_set(struct vregion *vr, struct k_thread *thread) +{ + if (!vr) + return; + + k_mutex_lock(&vr->lock, K_FOREVER); + vr->owner = thread; + k_mutex_unlock(&vr->lock); +} + void z_impl_vregion_set_interim(struct vregion *vr) { if (!vr) From bbc1aeb9f0e7d29c60470bc89dce91cdde2743c7 Mon Sep 17 00:00:00 2001 From: Kai Vehmanen Date: Wed, 25 Nov 2026 12:28:38 +0200 Subject: [PATCH 5/8] vregion: let the owner DP thread use its lifetime allocator Add vregion_owner_check() for the vregion system call validators. It passes for the LL and IPC user threads and for the owner thread of the region, and unpins the region and kills any other caller, such as the DP thread of another module. vregion_alloc_align(), vregion_alloc_coherent_align() and vregion_free() now accept the owner DP thread, but only for the lifetime allocator, through the new kernel functions vregion_lifetime_alloc_align() and vregion_lifetime_free(): the kernel never runs the interim heap for it. Its requests are refused as soon as the region switches to interim allocations, before the kernel sets up the heap or reads its statistics. The other vregion system calls stay limited to the LL and IPC user threads. The DP threads do not use the calls yet. They will, once DP modules use their resources in user mode. Without the syscall caller registry, every user thread but the owner is treated as before. Signed-off-by: Kai Vehmanen --- src/include/sof/lib/vregion.h | 46 +++++++++++++++++++ zephyr/lib/vregion.c | 84 ++++++++++++++++++++++++++++++++--- zephyr/syscall/vregion.c | 47 ++++++++++++++------ 3 files changed, 157 insertions(+), 20 deletions(-) diff --git a/src/include/sof/lib/vregion.h b/src/include/sof/lib/vregion.h index 5209364a3777..09938da23bb3 100644 --- a/src/include/sof/lib/vregion.h +++ b/src/include/sof/lib/vregion.h @@ -235,6 +235,45 @@ void vregion_mem_info(struct vregion *vr, size_t *size, uintptr_t *start); */ bool vregion_verify_pin(struct vregion *vr); +/** + * @brief Check the caller of a vregion system call. + * + * For system call validators, on a vregion pinned with vregion_verify_pin(). + * Passes for the LL and IPC user threads and for the owner thread of vr, see + * vregion_owner_set(). Unpins vr and kills any other caller. + * + * @param[in] vr Pinned virtual region instance. + * @return True for the LL and IPC user threads, false for the owner, which + * may only use the lifetime allocator. + */ +bool vregion_owner_check(struct vregion *vr); + +/** + * @brief Allocate from the lifetime allocator of a virtual region only. + * + * Kernel code only, for the owner thread of the region. Never allocates from + * the interim heap, which the kernel may run on the region memory. + * + * @param[in] vr Pointer to the virtual region instance. + * @param[in] size Size of memory to allocate in bytes. + * @param[in] alignment Alignment of memory to allocate in bytes. + * @param[in] coherent Return the uncached alias, as vregion_alloc_coherent_align(). + * @return Pointer to the allocated memory, or NULL on failure. + */ +void *vregion_lifetime_alloc_align(struct vregion *vr, size_t size, size_t alignment, + bool coherent); + +/** + * @brief Free to the lifetime allocator of a virtual region only. + * + * Kernel code only, for the owner thread of the region. Refuses pointers in + * the interim range. + * + * @param[in] vr Pointer to the virtual region instance. + * @param[in] ptr Pointer to the memory to free. + */ +void vregion_lifetime_free(struct vregion *vr, void *ptr); + /** * @brief Release a pin taken with vregion_verify_pin(). * @@ -451,6 +490,13 @@ static inline void *vregion_user_alloc_coherent_align(struct vregion *vr, size_t } static inline void vregion_user_free(struct vregion *vr, void *ptr) {} static inline bool vregion_verify_pin(struct vregion *vr) { return false; } +static inline bool vregion_owner_check(struct vregion *vr) { return false; } +static inline void *vregion_lifetime_alloc_align(struct vregion *vr, size_t size, + size_t alignment, bool coherent) +{ + return NULL; +} +static inline void vregion_lifetime_free(struct vregion *vr, void *ptr) {} static inline void vregion_unpin(struct vregion *vr) {} static inline bool vregion_pinned_get(struct vregion *vr) { return false; } static inline int vregion_pinned_put(struct vregion *vr) { return -EINVAL; } diff --git a/zephyr/lib/vregion.c b/zephyr/lib/vregion.c index c937b565e9a7..cedb200a8109 100644 --- a/zephyr/lib/vregion.c +++ b/zephyr/lib/vregion.c @@ -746,7 +746,8 @@ static void lifetime_free(struct vlinear_heap *heap, void *ptr) * @param vr Pointer to the virtual region instance. * @param ptr Pointer to the memory to free. */ -void z_impl_vregion_free(struct vregion *vr, void *ptr) +/* With @lifetime_only, refuse to free to the interim heap */ +static void vregion_free_mode(struct vregion *vr, void *ptr, bool lifetime_only) { if (!vr || !ptr) return; @@ -757,7 +758,9 @@ void z_impl_vregion_free(struct vregion *vr, void *ptr) ptr = sys_cache_cached_ptr_get(ptr); /* Check if pointer is in interim heap (if initialized) */ - if (vregion_in_interim(vr, ptr) && vr->flags & VREGION_FLAG_USER_HEAP) + if (vregion_in_interim(vr, ptr) && lifetime_only) + LOG_ERR("error: interim free %p from %p by its owner refused", ptr, (void *)vr); + else if (vregion_in_interim(vr, ptr) && vr->flags & VREGION_FLAG_USER_HEAP) /* the users run this heap, see vregion_user_free() */ LOG_ERR("error: interim free %p from user heap of %p refused", ptr, (void *)vr); @@ -773,19 +776,30 @@ void z_impl_vregion_free(struct vregion *vr, void *ptr) vregion_tel_sync_locked(vr); k_mutex_unlock(&vr->lock); } + +void z_impl_vregion_free(struct vregion *vr, void *ptr) +{ + vregion_free_mode(vr, ptr, false); +} EXPORT_SYMBOL(z_impl_vregion_free); +void vregion_lifetime_free(struct vregion *vr, void *ptr) +{ + vregion_free_mode(vr, ptr, true); +} + /** * @brief Allocate memory from the virtual region. * * @param[in] vr Pointer to the virtual region instance. * @param[in] size Size of the allocation. * @param[in] alignment Alignment of the allocation. + * @param[in] lifetime_only Refuse to allocate from the interim heap. * * @return void* Pointer to the allocated memory, or NULL on failure. */ -void *z_impl_vregion_alloc_align(struct vregion *vr, - size_t size, size_t alignment) +static void *vregion_alloc_mode(struct vregion *vr, size_t size, size_t alignment, + bool lifetime_only) { void *p; @@ -797,6 +811,18 @@ void *z_impl_vregion_alloc_align(struct vregion *vr, k_mutex_lock(&vr->lock, K_FOREVER); + /* + * The kernel does not run a heap for the owner thread: once the region + * switches to interim allocations, refuse before the heap is set up or + * its statistics are read. + */ + if (lifetime_only && (vr->interim_pending || vr->type != VREGION_MEM_TYPE_LIFETIME)) { + k_mutex_unlock(&vr->lock); + LOG_ERR("error: interim alloc of %zu from %p by its owner refused", + size, (void *)vr); + return NULL; + } + if (vr->interim_pending) { vr->interim_pending = false; interim_heap_init(vr); @@ -842,15 +868,21 @@ void *z_impl_vregion_alloc_align(struct vregion *vr, return p; } + +void *z_impl_vregion_alloc_align(struct vregion *vr, size_t size, size_t alignment) +{ + return vregion_alloc_mode(vr, size, alignment, false); +} EXPORT_SYMBOL(z_impl_vregion_alloc_align); -void *z_impl_vregion_alloc_coherent_align(struct vregion *vr, size_t size, size_t alignment) +static void *vregion_alloc_coherent_mode(struct vregion *vr, size_t size, size_t alignment, + bool lifetime_only) { if (alignment < CONFIG_DCACHE_LINE_SIZE) alignment = CONFIG_DCACHE_LINE_SIZE; size = ALIGN_UP(size, CONFIG_DCACHE_LINE_SIZE); - void *p = z_impl_vregion_alloc_align(vr, size, alignment); + void *p = vregion_alloc_mode(vr, size, alignment, lifetime_only); if (!p) return NULL; @@ -859,8 +891,22 @@ void *z_impl_vregion_alloc_coherent_align(struct vregion *vr, size_t size, size_ return sys_cache_uncached_ptr_get(p); } + +void *z_impl_vregion_alloc_coherent_align(struct vregion *vr, size_t size, size_t alignment) +{ + return vregion_alloc_coherent_mode(vr, size, alignment, false); +} EXPORT_SYMBOL(z_impl_vregion_alloc_coherent_align); +void *vregion_lifetime_alloc_align(struct vregion *vr, size_t size, size_t alignment, + bool coherent) +{ + if (coherent) + return vregion_alloc_coherent_mode(vr, size, alignment, true); + + return vregion_alloc_mode(vr, size, alignment, true); +} + /** * @brief Log virtual region memory usage. * @@ -888,6 +934,7 @@ void vregion_mem_info(struct vregion *vr, size_t *size, uintptr_t *start) #if CONFIG_SOF_VREGIONS && CONFIG_USERSPACE #include +#include bool vregion_verify_pin(struct vregion *vr) { @@ -932,6 +979,31 @@ bool vregion_verify_pin(struct vregion *vr) return true; } +bool vregion_owner_check(struct vregion *vr) +{ + bool owner; + + k_mutex_lock(&vr->lock, K_FOREVER); + owner = vr->owner && vr->owner == k_current_get(); + k_mutex_unlock(&vr->lock); + +#ifdef CONFIG_SOF_USERSPACE_SYSCALL_CALLER_CHECK + if (sof_syscall_caller_allowed(SOF_SYSUSER_INFRA)) + return true; + + if (!owner) { + /* another DP thread, or a thread that is no SOF infrastructure */ + vregion_unpin(vr); + K_OOPS(true); + } + + return false; +#else + /* no registry of the infrastructure threads, trust all but the owner */ + return !owner; +#endif +} + void vregion_unpin(struct vregion *vr) { bool destroy; diff --git a/zephyr/syscall/vregion.c b/zephyr/syscall/vregion.c index 153bdf8880fa..52c2fdf11964 100644 --- a/zephyr/syscall/vregion.c +++ b/zephyr/syscall/vregion.c @@ -13,18 +13,27 @@ * cannot free the vregion while the implementation uses it. The pin is * counted separately from references, so such a vregion_put() can not * consume it either. + * + * The allocation calls are also open to the owner DP thread of a region, + * for its lifetime allocator only. The other calls are for the LL and IPC + * user threads only. */ static inline void *z_vrfy_vregion_alloc_align(struct vregion *vr, size_t size, size_t alignment) { - void *ptr = NULL; + void *ptr; - SOF_SYSCALL_CALLER_CHECK(SOF_SYSUSER_INFRA); + if (!vregion_verify_pin(vr)) { + SOF_SYSCALL_CALLER_CHECK(SOF_SYSUSER_INFRA); + return NULL; + } - if (vregion_verify_pin(vr)) { + if (vregion_owner_check(vr)) ptr = z_impl_vregion_alloc_align(vr, size, alignment); - vregion_unpin(vr); - } + else + ptr = vregion_lifetime_alloc_align(vr, size, alignment, false); + + vregion_unpin(vr); return ptr; } @@ -33,14 +42,19 @@ static inline void *z_vrfy_vregion_alloc_align(struct vregion *vr, size_t size, static inline void *z_vrfy_vregion_alloc_coherent_align(struct vregion *vr, size_t size, size_t alignment) { - void *ptr = NULL; + void *ptr; - SOF_SYSCALL_CALLER_CHECK(SOF_SYSUSER_INFRA); + if (!vregion_verify_pin(vr)) { + SOF_SYSCALL_CALLER_CHECK(SOF_SYSUSER_INFRA); + return NULL; + } - if (vregion_verify_pin(vr)) { + if (vregion_owner_check(vr)) ptr = z_impl_vregion_alloc_coherent_align(vr, size, alignment); - vregion_unpin(vr); - } + else + ptr = vregion_lifetime_alloc_align(vr, size, alignment, true); + + vregion_unpin(vr); return ptr; } @@ -48,12 +62,17 @@ static inline void *z_vrfy_vregion_alloc_coherent_align(struct vregion *vr, static inline void z_vrfy_vregion_free(struct vregion *vr, void *ptr) { - SOF_SYSCALL_CALLER_CHECK(SOF_SYSUSER_INFRA); + if (!vregion_verify_pin(vr)) { + SOF_SYSCALL_CALLER_CHECK(SOF_SYSUSER_INFRA); + return; + } - if (vregion_verify_pin(vr)) { + if (vregion_owner_check(vr)) z_impl_vregion_free(vr, ptr); - vregion_unpin(vr); - } + else + vregion_lifetime_free(vr, ptr); + + vregion_unpin(vr); } #include From 15f3cc3533ac0843c259c2f9c7948b6718c2540e Mon Sep 17 00:00:00 2001 From: Kai Vehmanen Date: Wed, 25 Nov 2026 12:35:39 +0200 Subject: [PATCH 6/8] module: use LL module resources in user mode only for registered modules The LL and IPC user threads decided from the processing domain in the comp_dev whether to run the module resource functions in user mode or through the mod_dp_*() system calls. The comp_dev and module objects of a DP module are in DP memory, so a DP thread could mark its module as an LL module, or hand over a made-up module through dev->mod, and an IPC user thread would then walk a resource pool and allocation context that the DP thread controls, in user mode, with access to all LL memory. Register the LL modules in LL memory when they are created in user mode, in module_adapter_mem_alloc(), and remove them when their memory is freed. The user-mode path is now taken only for registered modules. Any other module goes to the system calls, whose validators reject LL modules and modules without a live vregion. The processing domain is still checked first, so that DP threads, which cannot access the list, use the system calls for their own module. Signed-off-by: Kai Vehmanen --- src/audio/module_adapter/module/generic.c | 91 ++++++++++++++++++- src/audio/module_adapter/module_adapter.c | 19 ++++ .../sof/audio/module_adapter/module/generic.h | 28 ++++++ 3 files changed, 135 insertions(+), 3 deletions(-) diff --git a/src/audio/module_adapter/module/generic.c b/src/audio/module_adapter/module/generic.c index 219054dbd1de..5a82f1dbb56f 100644 --- a/src/audio/module_adapter/module/generic.c +++ b/src/audio/module_adapter/module/generic.c @@ -605,6 +605,83 @@ int z_impl_mod_free(struct processing_module *mod, const void *ptr) EXPORT_SYMBOL(z_impl_mod_free); #if defined(__ZEPHYR__) && defined(CONFIG_SOF_FULL_ZEPHYR_APPLICATION) +#if CONFIG_SOF_USERSPACE_LL +/* an LL module whose resources the LL and IPC user threads use in user mode */ +struct mod_res_user_entry { + struct list_item list; + struct processing_module *mod; +}; + +/* the LL modules registered with mod_res_user_add(), under mod_res_ulock */ +static APP_SYSUSER_DATA struct list_item mod_res_user_list = LIST_INIT(mod_res_user_list); + +int mod_res_user_add(struct processing_module *mod) +{ + struct mod_res_user_entry *entry; + + if (!k_is_user_context()) + return -EPERM; + + /* the IPC user threads of all cores walk the list */ + entry = sof_heap_alloc(sof_sys_user_heap_get(), SOF_MEM_FLAG_USER | SOF_MEM_FLAG_COHERENT, + sizeof(*entry), 0); + if (!entry) + return -ENOMEM; + + entry->mod = mod; + + sys_mutex_lock(&mod_res_ulock, K_FOREVER); + list_item_append(&entry->list, &mod_res_user_list); + sys_mutex_unlock(&mod_res_ulock); + + return 0; +} + +void mod_res_user_remove(struct processing_module *mod) +{ + struct mod_res_user_entry *entry = NULL; + struct list_item *item; + + if (!k_is_user_context()) + return; + + sys_mutex_lock(&mod_res_ulock, K_FOREVER); + list_for_item(item, &mod_res_user_list) { + entry = container_of(item, struct mod_res_user_entry, list); + if (entry->mod == mod) { + list_item_del(item); + break; + } + entry = NULL; + } + sys_mutex_unlock(&mod_res_ulock); + + sof_heap_free(sof_sys_user_heap_get(), entry); +} + +static bool mod_res_user_listed(const struct processing_module *mod) +{ + struct mod_res_user_entry *entry; + struct list_item *item; + bool found = false; + + /* not an LL or IPC user thread */ + if (sys_mutex_lock(&mod_res_ulock, K_FOREVER)) + return false; + + list_for_item(item, &mod_res_user_list) { + entry = container_of(item, struct mod_res_user_entry, list); + if (entry->mod == mod) { + found = true; + break; + } + } + sys_mutex_unlock(&mod_res_ulock); + + return found; +} +#endif + /* * With CONFIG_SOF_USERSPACE_LL, the LL and IPC user threads use the resources * of LL modules in user mode: the module memory, its resource pool and the @@ -614,14 +691,22 @@ EXPORT_SYMBOL(z_impl_mod_free); * kernel structures behind their vregions. In kernel mode the system call * wrappers call the implementation directly. * - * The processing domain is user-writable, so a user thread can only pick the - * wrong path for itself: the system calls reject LL modules. + * The user-mode path is taken only for the LL modules registered in LL + * memory when they were created, not by the processing domain or other + * module data: the comp_dev and module objects of a DP module are in DP + * memory, so a DP thread could make them look like an LL module, or hand + * over a made-up module, whose resource pool the IPC user thread would then + * walk in user mode. Any other module goes to the system calls, which reject + * LL modules and modules without a live vregion. The processing domain is + * only checked first, so that DP threads, which have no access to the list, + * use the system calls for their own module. */ static bool mod_res_user(struct processing_module *mod) { #if CONFIG_SOF_USERSPACE_LL return k_is_user_context() && - mod->dev->ipc_config.proc_domain != COMP_PROCESSING_DOMAIN_DP; + mod->dev->ipc_config.proc_domain != COMP_PROCESSING_DOMAIN_DP && + mod_res_user_listed(mod); #else return false; #endif diff --git a/src/audio/module_adapter/module_adapter.c b/src/audio/module_adapter/module_adapter.c index 894e86e8a826..8dcd2082695b 100644 --- a/src/audio/module_adapter/module_adapter.c +++ b/src/audio/module_adapter/module_adapter.c @@ -187,11 +187,27 @@ struct processing_module *module_adapter_mem_alloc(const struct comp_driver *drv mod->dev = dev; dev->mod = mod; +#if CONFIG_SOF_USERSPACE_LL + /* an LL module in LL memory, its resources are used in user mode */ + if (k_is_user_context() && config->proc_domain != COMP_PROCESSING_DOMAIN_DP && + mod_res_user_add(mod) < 0) { + comp_cl_err(drv, "failed to register the module resources"); + goto eres; + } +#endif + /* the region of a DP module, from here and not from module memory */ *dp_vreg = use_ppl_alloc ? NULL : mod_vreg; return mod; +#if CONFIG_SOF_USERSPACE_LL +eres: + if (mod_vreg) + vregion_user_free(mod_vreg, dev); + else + sof_heap_free(mod_heap, dev); +#endif edev: if (!use_ppl_alloc && !mod_vreg) sof_heap_free(mod_heap, alloc); @@ -214,6 +230,9 @@ __cold static void module_adapter_mem_free(struct processing_module *mod) assert_can_be_cold(); + /* after the last module resource call */ + mod_res_user_remove(mod); + /* * In principle it shouldn't even be needed to free individual objects * on the module heap since we're freeing the heap itself too diff --git a/src/include/sof/audio/module_adapter/module/generic.h b/src/include/sof/audio/module_adapter/module/generic.h index 4d93ff1daff8..3038d497e61f 100644 --- a/src/include/sof/audio/module_adapter/module/generic.h +++ b/src/include/sof/audio/module_adapter/module/generic.h @@ -216,6 +216,34 @@ void *z_impl_mod_balloc_align(struct processing_module *mod, size_t size, size_t */ void mod_resource_init(struct processing_module *mod); +#if CONFIG_SOF_USERSPACE_LL +/** + * @brief Let the LL and IPC user threads use the resources of an LL module. + * + * In user mode only, for a module whose memory and resource pool are in LL + * memory. The module resource functions then run in user mode for @p mod, + * see mod_res_user() in generic.c, and use the system calls for any other + * module. + * + * @param mod Pointer to processing module. + * @return 0 on success or a negative error code. + */ +int mod_res_user_add(struct processing_module *mod); + +/** + * @brief Undo mod_res_user_add(), before the module memory is freed. + * @param mod Pointer to processing module. + */ +void mod_res_user_remove(struct processing_module *mod); +#else +static inline int mod_res_user_add(struct processing_module *mod) +{ + return 0; +} + +static inline void mod_res_user_remove(struct processing_module *mod) {} +#endif + /** * @brief Query module memory heap boundary and size. * @param mod Pointer to processing module. From 930cc484eda07232182a95fdc12b9a9bcaccabf0 Mon Sep 17 00:00:00 2001 From: Kai Vehmanen Date: Wed, 25 Nov 2026 12:46:58 +0200 Subject: [PATCH 7/8] zephyr: test: userspace: test the region owner and the LL module list Add cases for the owner of a region, the DP thread of a module. It allocates from and frees to the lifetime allocator of its own region with the vregion system calls, coherent memory too, and its requests are refused, without a fault, once the kernel runs the interim heap. The IPC user thread may still use that heap. A thread that is neither the owner nor an LL or IPC user thread is killed, also when the region is mapped for it, and the pins of the killed calls are released. The module resource functions only run in user mode for LL modules that were registered when they were created: a module that claims to be an LL module but is not registered goes to the DP system call, which kills the IPC user thread. The existing cases register their module. Signed-off-by: Kai Vehmanen --- zephyr/test/userspace/test_ll_user_heap.c | 113 ++++++++++++++++++++++ 1 file changed, 113 insertions(+) diff --git a/zephyr/test/userspace/test_ll_user_heap.c b/zephyr/test/userspace/test_ll_user_heap.c index b60599138ba5..990cd96a4bd0 100644 --- a/zephyr/test/userspace/test_ll_user_heap.c +++ b/zephyr/test/userspace/test_ll_user_heap.c @@ -20,6 +20,10 @@ * user thread allocates from it without system calls, kernel code refuses * to use the interim range, only the LL and IPC user threads get the range, * and a user thread that corrupts the heap metadata can only fault itself. + * + * Tests for the owner of a region, the DP thread of a module: it may use the + * lifetime allocator of its own region through the system calls, never the + * kernel interim heap, and other DP threads may not use the region at all. */ #include @@ -54,6 +58,7 @@ K_APPMEM_PARTITION_DEFINE(ll_user_heap_test_part); /* test objects that user threads write, so in a user partition */ K_APP_BMEM(ll_user_heap_test_part) static struct processing_module fake_mod; +K_APP_BMEM(ll_user_heap_test_part) static struct processing_module other_mod; K_APP_BMEM(ll_user_heap_test_part) static struct comp_dev fake_dev; K_APP_BMEM(ll_user_heap_test_part) static struct mod_alloc_ctx fake_alloc; K_APP_BMEM(ll_user_heap_test_part) static void *user_ptr; @@ -82,6 +87,10 @@ enum user_heap_test_case { UHEAP_MOD_DP_SYSCALL, /* call the module allocator in user mode with fake_mod */ UHEAP_MOD_USER_ALLOC, + /* register fake_mod as an LL module for the user-mode resource path */ + UHEAP_MOD_REGISTER, + /* call the module allocator in user mode with other_mod */ + UHEAP_MOD_OTHER_ALLOC, }; static void check_alloc(uint32_t flags, size_t bytes, size_t align) @@ -151,6 +160,12 @@ static void user_function(void *p1, void *p2, void *p3) case UHEAP_MOD_USER_ALLOC: (void)mod_alloc_ext(&fake_mod, SOF_MEM_FLAG_USER, 64, 0); break; + case UHEAP_MOD_REGISTER: + zassert_ok(mod_res_user_add(&fake_mod)); + return; + case UHEAP_MOD_OTHER_ALLOC: + (void)mod_alloc_ext(&other_mod, SOF_MEM_FLAG_USER, 64, 0); + break; default: zassert_unreachable("unknown test case"); } @@ -188,6 +203,20 @@ static void run_case(enum user_heap_test_case test_case, bool fault_allowed) run_user(user_function, test_case, false, fault_allowed); } +/* + * Register fake_mod as an LL module once, as LL modules are when they are + * created. It stays registered: a case may leave the resource lock with a + * faulted thread. + */ +static void fake_mod_register(void) +{ + static bool registered; + + if (!registered) + run_case(UHEAP_MOD_REGISTER, false); + registered = true; +} + static void *ll_user_heap_setup(void) { k_mem_domain_add_partition(zephyr_ll_mem_domain(), &ll_user_heap_test_part); @@ -316,11 +345,32 @@ ZTEST(userspace_ll_user_heap, mod_user_rejects_forged_heap) fake_alloc.heap = (struct k_heap *)kernel_only_words; fake_alloc.vreg = NULL; mod_resource_init(&fake_mod); + fake_mod_register(); /* the allocator runs in user mode and refuses the heap */ run_case(UHEAP_MOD_USER_ALLOC, true); } +ZTEST(userspace_ll_user_heap, mod_user_needs_registration) +{ + /* + * A module that claims to be an LL module, as a DP thread could make + * its own module look, but was not registered when it was created. + */ + memset(&other_mod, 0, sizeof(other_mod)); + memset(&fake_dev, 0, sizeof(fake_dev)); + other_mod.dev = &fake_dev; + fake_dev.mod = &other_mod; + other_mod.priv.resources.alloc = &fake_alloc; + fake_dev.ipc_config.proc_domain = COMP_PROCESSING_DOMAIN_LL; + fake_alloc.heap = zephyr_ll_user_heap(); + fake_alloc.vreg = NULL; + mod_resource_init(&other_mod); + + /* the IPC user thread uses the system call, which rejects LL modules */ + run_user(user_function, UHEAP_MOD_OTHER_ALLOC, true, true); +} + ZTEST_SUITE(userspace_ll_user_heap, sof_boot_test_standalone_predicate, ll_user_heap_setup, NULL, NULL, ll_user_heap_teardown); @@ -506,6 +556,7 @@ ZTEST(userspace_ll_fast_get, mod_fast_get_falls_back) fake_alloc.heap = zephyr_ll_user_heap(); fake_alloc.vreg = NULL; mod_resource_init(&fake_mod); + fake_mod_register(); run_fg_case(FG_MOD_GET, true, false); zassert_true(zephyr_ll_fg_contains(fg_ptr[0], DATA_SIZE)); @@ -573,6 +624,12 @@ enum vregion_heap_test_case { VHEAP_INTERIM_GET, /* copy the counters for telemetry */ VHEAP_TEL_SYNC, + /* allocate and free with the system calls */ + VHEAP_SYSCALL_ALLOC_FREE, + /* allocate with the system calls, expect a refusal */ + VHEAP_SYSCALL_ALLOC_REFUSED, + /* allocate with the system call */ + VHEAP_SYSCALL_ALLOC, }; static void check_vheap_alloc(bool coherent, size_t bytes, size_t align) @@ -657,6 +714,22 @@ static void vheap_user_function(void *p1, void *p2, void *p3) case VHEAP_TEL_SYNC: telemetry_sync_user(); return; + case VHEAP_SYSCALL_ALLOC_FREE: + user_ptr = vregion_alloc(test_vreg, 64); + zassert_not_null(user_ptr); + vregion_free(test_vreg, user_ptr); + user_ptr = vregion_alloc_coherent(test_vreg, 64); + zassert_not_null(user_ptr); + zassert_false(sys_cache_is_ptr_cached(user_ptr)); + vregion_free(test_vreg, user_ptr); + return; + case VHEAP_SYSCALL_ALLOC_REFUSED: + zassert_is_null(vregion_alloc(test_vreg, 64)); + zassert_is_null(vregion_alloc_coherent(test_vreg, 64)); + return; + case VHEAP_SYSCALL_ALLOC: + (void)vregion_alloc(test_vreg, 64); + break; default: zassert_unreachable("unknown test case"); } @@ -852,6 +925,46 @@ ZTEST(userspace_vregion_user_heap, corrupted_metadata_faults_user_only) user_ptr = NULL; } +ZTEST(userspace_vregion_user_heap, owner_lifetime_only) +{ + /* a DP module region, the kernel runs its interim heap */ + test_vreg = vheap_vreg_create(0, false); + + /* the DP thread of the module, not an LL or IPC user thread */ + vregion_owner_set(test_vreg, &user_thread); + + run_vheap_case(VHEAP_SYSCALL_ALLOC_FREE, false, false); + + /* never the kernel interim heap */ + vregion_set_interim(test_vreg); + run_vheap_case(VHEAP_SYSCALL_ALLOC_REFUSED, false, false); + + /* the IPC user thread may */ + vregion_owner_set(test_vreg, NULL); + run_vheap_case(VHEAP_SYSCALL_ALLOC_FREE, true, false); + + vheap_vreg_destroy(test_vreg); +} + +ZTEST(userspace_vregion_user_heap, other_dp_thread_killed) +{ + struct vregion *own_vreg = vheap_vreg_create(0, false); + + /* the region of another module, also mapped for the caller */ + test_vreg = vheap_vreg_create(0, false); + vregion_owner_set(own_vreg, &user_thread); + + run_vheap_case(VHEAP_SYSCALL_ALLOC, false, true); + + /* no owner at all */ + vregion_owner_set(own_vreg, NULL); + run_vheap_case(VHEAP_SYSCALL_ALLOC, false, true); + + /* the pins of the killed calls are released */ + vheap_vreg_destroy(test_vreg); + vheap_vreg_destroy(own_vreg); +} + ZTEST_SUITE(userspace_vregion_user_heap, sof_boot_test_standalone_predicate, ll_user_heap_setup, NULL, NULL, ll_user_heap_teardown); From de52db1d87f500798e6cbf5c6f3785b0f0dc59ab Mon Sep 17 00:00:00 2001 From: Kai Vehmanen Date: Wed, 25 Nov 2026 12:47:17 +0200 Subject: [PATCH 8/8] userspace: doc: describe the DP region owner and the LL module list Describe how the kernel keeps the region of a DP module and records its DP thread as the owner, which vregion system calls the owner may use, and that the LL and IPC user threads use the resources of registered LL modules only in user mode. Signed-off-by: Kai Vehmanen --- src/include/sof/userspace/README.md | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/include/sof/userspace/README.md b/src/include/sof/userspace/README.md index 5d1f8faeccad..747776645717 100644 --- a/src/include/sof/userspace/README.md +++ b/src/include/sof/userspace/README.md @@ -140,6 +140,7 @@ from user-writable data such as `ipc_user->thread[]`. | Any user thread, including DP module threads | `cpu_get_id`, all module management syscalls (`mod_dp_*`), `ipc_msg_send`, `ipc_msg_list_remove`, `send_resource_notif`, `kpb_notifier_schedule` | | IPC user thread only | `ipc_compound_pre_start`, `ipc_compound_post_start`, `ipc_wait_for_compound_msg`, `lib_manager_free_module` | | LL user thread only | `scheduler_dp_ll_tick`, `scheduler_dp_kick` | +| LL or IPC user thread, or the owner DP thread of the region (lifetime allocator only) | `vregion_alloc_align`, `vregion_alloc_coherent_align`, `vregion_free` | | LL or IPC user thread | all other SOF syscalls | With `CONFIG_SOF_USERSPACE_APPLICATION`, the DP task list and task memory are writable by DP @@ -152,12 +153,23 @@ DP modules only get the syscalls needed to implement the module interface. That sending notifications: modules queue messages from `mod_ipc_msg_init()` with `ipc_msg_send()` and free them with `mod_ipc_msg_free()`, and `dp_task_run()` reports processing errors with `send_resource_notif()`. DP modules have no access to the other IPC -syscalls or to vregion, DMA, DAI or scheduler syscalls. +syscalls or to DMA, DAI or scheduler syscalls. + +The kernel creates the vregion of a DP module and passes it to `scheduler_dp_task_init()`, +which keeps a reference in the kernel-only task record and builds the heap partitions of the +DP memory domain from the bounds in the kernel-only `struct vregion`, not from the allocation +context in module memory. It also records the DP thread as the owner of the region +(`vregion_owner_set()`), until the task is freed. The vregion allocation syscalls accept the +owner for the lifetime allocator of its own region only (`vregion_owner_check()`): the kernel +never runs the interim heap for a DP thread, and any other DP thread is killed. Modules call the module resource functions without the `dp` prefix (`mod_alloc_ext()`, `mod_free()`, `mod_fast_get()` and so on), which are plain functions. For an LL module in the LL or IPC user thread they run in user mode, on the module's resource pool and the LL user -heap, under a `sys_mutex` in LL memory. `mod_fast_get()` then gets a shared read-only copy +heap, under a `sys_mutex` in LL memory. That path is only taken for the LL modules that +`module_adapter_mem_alloc()` registered in a list in LL memory when it created them in user +mode (`mod_res_user_add()`), not by the processing domain: the `comp_dev` and module objects +of a DP module are in DP memory, so a DP thread could make its module look like an LL module. `mod_fast_get()` then gets a shared read-only copy with the `fast_get_ll()` syscall, see below, or makes a private copy when the fast_get area is full. For DP modules, and in kernel code, they call the `mod_dp_*` syscalls. Those validators reject LL modules and DP modules whose allocation context has no