Repository navigation
Conversation
The LL user heap is a k_heap in kernel memory, but its sys_heap metadata is in the heap memory, which LL and IPC user threads can write. The heap system calls run the allocator in supervisor mode on that metadata, so a user thread that corrupts it gets a write anywhere in kernel memory. Add an allocator that can run in user mode: the sys_heap object, a sys_mutex and usage counters in the system user partition, which is mapped only into the LL memory domain. The kernel initialises the heap at boot and does not touch it after that. Allocations keep the cache line rules of the kernel heap allocator, and frees of cached pointers write back and invalidate the memory first. The k_heap returned by zephyr_ll_user_heap() is no longer initialised. It stays a kernel-only handle whose address selects the new allocator in sof_heap_alloc() and sof_heap_free(). These are still system calls, so for now the allocator runs in the system call, and the free validator checks the pointer against a kernel copy of the heap bounds. Telemetry reports the counters instead of parsing the heap. Signed-off-by: Kai Vehmanen <kai.vehmanen@linux.intel.com>
There was a problem hiding this comment.
🟡 Changes recommended
The new vregion pin requirement causes every user-mode DP resource call to fault in the still-supported SOF_USERSPACE_LL=y, SOF_VREGIONS=n configuration, where vregion_verify_pin() always returns false.
4 open issues
What changed in this PR
Moves the low-latency (LL) user heap allocator into user mode so kernel code no longer processes user-writable heap metadata.
Changes:
- Replaces heap syscalls with direct user-mode allocation and kernel-side refusal.
- Separates LL module resource handling from DP syscalls and moves large shared copies to page allocation.
- Adds heap isolation tests and updates configuration, interfaces, and documentation.
| File | Description |
|---|---|
zephyr/test/userspace/test_ll_user_heap.c |
Adds allocation, isolation, and forged-context tests. |
zephyr/test/userspace/test_ll_task.c |
Runs pipeline allocation checks in user mode. |
zephyr/test/userspace/test_heap_alloc.c |
Removes obsolete heap syscall tests. |
zephyr/test/userspace/README.md |
Documents the new heap test suite. |
zephyr/test/CMakeLists.txt |
Registers the replacement tests. |
zephyr/syscall/alloc.c |
Removes heap syscall validators. |
zephyr/lib/fast-get.c |
Uses page allocation for large shared copies and adjusts locking. |
zephyr/lib/alloc.c |
Dispatches user allocations, refuses kernel use, and updates telemetry. |
zephyr/Kconfig |
Removes obsolete heap syscall and tracing options. |
zephyr/include/rtos/alloc.h |
Declares heap APIs as ordinary functions. |
zephyr/CMakeLists.txt |
Removes heap syscall build integration. |
src/schedule/zephyr_ll_user_heap.c |
Implements the user-mode heap allocator and counters. |
src/schedule/CMakeLists.txt |
Builds the new allocator for LL userspace. |
src/kernel/schedule/zephyr_ll_user.c |
Initializes the allocator and retains an opaque heap handle. |
src/include/sof/userspace/README.md |
Updates the memory-boundary and syscall documentation. |
src/include/sof/schedule/ll_schedule.h |
Declares allocator, statistics, and test interfaces. |
src/include/sof/schedule/ll_schedule_domain.h |
Replaces heap verification with a size accessor. |
src/include/sof/audio/module_adapter/module/generic.h |
Separates public resource functions from DP syscalls. |
src/audio/module_adapter/module/generic.c |
Adds context-specific resource handling and DP validation. |
doc/sof.doxygen.in |
Removes the retired heap syscall configuration define. |
Review effort: Balanced
| vreg = alloc->vreg; | ||
| K_OOPS(!vregion_verify_pin(vreg)); |
There was a problem hiding this comment.
Thanks, valid. Fixed in V2. With CONFIG_SOF_USERSPACE_LL, a DP module without its own vregion (vregions disabled, the userspace proxy or the driver heap) would take its resources from the LL user heap. The kernel can no longer allocate from that heap safely, so there is no safe supervisor fallback. These configurations are now excluded in Kconfig.
| #if CONFIG_SOF_USERSPACE_LL | ||
| if (mod_res_user(mod)) | ||
| return mod_fast_get_copy(mod, dram_ptr, size); |
There was a problem hiding this comment.
Thanks, valid, but it will be handled in the next series. The private copy is a stopgap until LL modules get a user-mode fast_get interface.
| zassert_not_null(ptr); | ||
| zassert_equal((uintptr_t)ptr % MAX(align, PLATFORM_DCACHE_ALIGN), 0, "%p misaligned", | ||
| ptr); | ||
| zassert_equal(sys_cache_is_ptr_cached(ptr), !(flags & SOF_MEM_FLAG_COHERENT)); |
There was a problem hiding this comment.
Thanks, valid. Fixed in V2: the alias check now runs only with CONFIG_CACHE_MANAGEMENT and CONFIG_CACHE_HAS_MIRRORED_MEMORY_REGIONS, where sys_cache_is_ptr_cached() can tell the aliases apart.
| ko = k_object_find(lock); | ||
| zassert_not_null(ko); | ||
| k_mutex_init(ko->data.mutex); |
There was a problem hiding this comment.
Thanks, valid. Fixed in V2: the backing k_mutex is reset only with CONFIG_SYS_MUTEX_IMPL_K_MUTEX. With the futex backend the test resets the lock with sys_mutex_init().
PR 11524: test resultsRun date: 2026-11-23 17:26 UTC Tested commit: c3f4d5894476c38f51c2162072728bc62123bb2a |
With CONFIG_SOF_USERSPACE_LL, the mod_*() resource functions are system calls, so for LL modules the kernel runs the LL user heap allocator and the resource pool code on memory that the LL and IPC user threads can write. The validators also never check the allocation context of an LL module, so a user thread can make the kernel allocate from a heap it made up. Make the module resource functions plain functions. In the LL and IPC user threads they run the implementation in user mode for LL modules, under a sys_mutex in LL memory instead of the kernel mutex. DP modules, and kernel code, still go through the system calls, which are renamed to mod_dp_*(). Their validators now reject LL modules and DP modules whose allocation context has no live vregion, and pin the vregion for the call. The context is still read from user memory, so this does not stop a DP thread that changes it during the call. DP modules without a vregion of their own, with vregions disabled, the userspace proxy or the driver heap, would take their resources from the LL user heap, which the kernel must not allocate from. With the DP scheduler, SOF_USERSPACE_LL therefore now requires DP modules to run as userspace applications without the driver heap. fast_get() keeps its state in kernel memory, so LL modules in user mode get a private copy of the data in their own memory instead of a shared copy, until fast_get() has a user-mode interface. Signed-off-by: Kai Vehmanen <kai.vehmanen@linux.intel.com>
…heap With CONFIG_SOF_USERSPACE_LL, large fast_get() copies for DP modules are allocated in supervisor mode from the heap of the module allocation context, which is the LL user heap. The kernel must not run that allocator, since user threads can write its metadata. Take large copies from vpage_alloc() instead when vregions are enabled. They are page aligned already and are mapped read-only into the memory domain of each user, as before. vpage_alloc() takes a mutex, so with CONFIG_SOF_USERSPACE_LL the fast_get lock is a mutex too: fast_get() and fast_put() are only called from threads. Free the copy before the partition of the last user is removed, like vregions do: vpage_free() flushes the cache through the mapping of the current domain. Signed-off-by: Kai Vehmanen <kai.vehmanen@linux.intel.com>
The LL and IPC user threads allocate from the LL user heap through the sof_heap_alloc() and sof_heap_free() system calls, so the allocator runs in supervisor mode on metadata that user threads can write. With CONFIG_SOF_USERSPACE_LL, make sof_heap_alloc() and sof_heap_free() plain functions again: in user mode they call the user-mode allocator, and kill the thread for any other heap, as the system call validators did. In kernel mode an allocation from or a free to the LL user heap is refused and logged with the caller. A refused free leaks the memory. LL module resources and DP fast_get() copies no longer use this heap in kernel mode, and a trace build showed no kernel thread using it, so the LL heap trace option has no use left and is removed. SOF_USERSPACE_LL no longer selects SOF_USERSPACE_INTERFACE_ALLOC. Run the pipeline check of the LL standalone test in a user thread, since it allocates from the LL user heap. Signed-off-by: Kai Vehmanen <kai.vehmanen@linux.intel.com>
The LL and IPC user threads now allocate from the LL user heap in user mode, and no configuration enables SOF_USERSPACE_INTERFACE_ALLOC any more. Remove the option, the system calls and their validators, the test of the validators, which was only built without the LL user threads, and the kernel helpers that only the validators used. Signed-off-by: Kai Vehmanen <kai.vehmanen@linux.intel.com>
With CONFIG_SYS_HEAP_RUNTIME_STATS and IPC4, every allocation from a kernel heap logs the heap usage. The LL user heap lost this when its allocator moved to user mode. Log the sys_heap statistics in the user-mode allocator too, under the heap lock. Kernel code only reads the usage counters of the LL user heap, for telemetry. Signed-off-by: Kai Vehmanen <kai.vehmanen@linux.intel.com>
Add a standalone suite for the LL user heap. A user thread allocates coherent and cached memory with different alignments and the usage counters follow. Kernel code gets no memory from the heap and frees nothing to it. A user thread can only allocate from the LL user heap. A user thread then overwrites the heap header so that every chunk index points at an unmapped address, and another one allocates from the heap, which faults that thread only. The kernel paths that used to run the allocator, sof_heap_alloc(), sof_heap_free() and the usage counters, still work. The test then restores the header and the heap lock, which the faulting thread may hold, and the heap works again. The DP module resource system call rejects an LL module, a forged heap and a forged vregion, and the user-mode module allocator rejects a forged heap in the user thread. Signed-off-by: Kai Vehmanen <kai.vehmanen@linux.intel.com>
Describe where the LL user heap state is, that its allocator runs in user mode only and that kernel code refuses to use it, how the module resource functions pick between user mode and the mod_dp_*() system calls, and where large fast_get() copies for DP modules come from. Remove the heap system calls from the syscall map and the Kconfig graph, and list the new test. Signed-off-by: Kai Vehmanen <kai.vehmanen@linux.intel.com>
16892d1 to
c3f4d58
Compare
|
V2 pushed:
|


With CONFIG_SOF_USERSPACE_LL, the LL user heap's sys_heap metadata is in memory that the LL and IPC user threads can write. The sof_heap_alloc()/sof_heap_free() and mod_*() system calls ran the allocator in supervisor mode on that metadata. A user thread that corrupted the heap could therefore get an arbitrary kernel write. Earlier pull requests this month moved every kernel user of this heap into kernel-only RW/RO areas. This PR makes the heap a user-mode-only allocator and makes the kernel refuse to use it.
Known limitation: the DP module context is still read from DP-writable memory during a mod_dp_*() call. A kernel-side module registry is a follow-up.
Tested on Intel PTL: chain DMA, HDA, nocodec ALSA with DP, multiple-pipeline, DP long run and create/free cycles (no heap leak), D3 cycles, and all standalone userspace suites. Every commit builds, and the mtl/lnl/ptl/imx8m/testbench builds are OK.