Skip to content

user-ll: move interim vregion management to user-space - #11563

Open
kv2019i wants to merge 6 commits into
thesofproject:mainfrom
kv2019i:202611-ppl-vregion-heap
Open

kv2019i wants to merge 6 commits into
thesofproject:mainfrom
kv2019i:202611-ppl-vregion-heap

Conversation

@kv2019i

@kv2019i kv2019i commented Nov 30, 2026

Copy link
Copy Markdown
Collaborator

A series to move management of the interim vregions from kernel to user-space. This allows to remove the unsecure use of kheap (user-writable heap memory, heap functions run in kernel) and still continue to reusing the Zephyr heap implementation as before.

The interim heap of a pipeline region is a k_heap whose metadata is in
the region memory. With user-space LL that memory is mapped into the LL
memory domain, and the IPC user threads allocate from the heap through
the vregion system calls, so a user thread that forges the heap
metadata gets the kernel to write anywhere.

Add VREGION_FLAG_USER_HEAP to vregion_create_map(). For such a region
vregion_set_interim() only reserves the interim range and keeps its
bounds in the kernel-only struct vregion, without k_heap_init(). Kernel
code, including the vregion system calls, refuses to allocate from or
free to that range and logs the caller. The new vregion_interim_get()
system call gives the range to the LL and IPC user threads, which run
their own allocator on it in user mode.

The flag is only accepted with CONFIG_SOF_VREGION_USER_HEAP, which is
set with user-space LL. No region uses it yet. Regions without the flag,
such as the DP module regions, keep the k_heap.

Signed-off-by: Kai Vehmanen <kai.vehmanen@linux.intel.com>
Add the allocator that the LL and IPC user threads run on the interim
range of a region with VREGION_FLAG_USER_HEAP, in user mode, like the
LL user heap.

vregion_user_heap_add() gets the range with vregion_interim_get() and
initialises a sys_heap in it. The header at the start of the range
holds the usage counters and the sys_heap object, and the metadata is
only accessed through the uncached alias. The threads find the header
of a region in a table in the system user partition, which DP threads
cannot write. One sys_mutex in that partition serialises the table and
all the heaps. CONFIG_SOF_VREGION_USER_HEAP_COUNT sets the table size,
32 by default, the IPC4 limit of pipelines.

vregion_user_alloc_align(), vregion_user_alloc_coherent_align() and
vregion_user_free() use that heap in user mode when the region has one,
and otherwise call vregion_alloc_align(), vregion_alloc_coherent_align()
and vregion_free(), so they can replace those calls for any region.
Telemetry reads only the counters of a user-mode heap, as numbers.

Signed-off-by: Kai Vehmanen <kai.vehmanen@linux.intel.com>
Allocate and free the region memory of allocation contexts, object pools
and module instances with vregion_user_alloc_align(),
vregion_user_alloc_coherent_align() and vregion_user_free(), so that in
user mode they use the user-mode interim heap of a region that has one.
For the other regions, and in kernel mode, nothing changes: the
functions then call the vregion system calls as before.

Signed-off-by: Kai Vehmanen <kai.vehmanen@linux.intel.com>
With user-space LL, every pipeline has a region, and the IPC user
threads allocate from its interim heap at every pipeline start, for the
pipeline and trigger tasks, and in the prepare of LL modules. The
kernel ran that k_heap on memory that LL and IPC user threads can
write.

Create the pipeline regions with VREGION_FLAG_USER_HEAP. Set up the
user-mode heap when the pipeline is complete, after switching the
region to interim allocations, and remove it when the pipeline is
freed, after its modules. Allocations then do not enter the kernel.

The DP module regions are not changed.

Signed-off-by: Kai Vehmanen <kai.vehmanen@linux.intel.com>
Add a standalone suite for the user-mode interim heap of the regions
with VREGION_FLAG_USER_HEAP. The IPC user thread sets up the heap of a
region mapped into the LL memory domain and allocates coherent and
cached memory of several alignments from the interim range, and the
counters drop back when it is freed. Lifetime allocations still go to
the kernel, and a region without the flag keeps its kernel heap. Kernel
code neither allocates from the interim range nor frees to it, an LL
domain thread that is not the LL or IPC user thread cannot get the
range, and a user thread that corrupts the heap metadata only faults
itself.

The suite shares the user thread of the LL user heap suite, as the
standalone build has no thread ID left for another one.
vregion_user_heap_test_get() gives the test the heap and its lock.

Signed-off-by: Kai Vehmanen <kai.vehmanen@linux.intel.com>
Describe the user-mode interim heap of the pipeline regions next to
the LL user heap, add vregion_interim_get() to the syscall map, and
list the new test cases.

Signed-off-by: Kai Vehmanen <kai.vehmanen@linux.intel.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

A critical validator bypass lets user threads make the kernel process user-controlled allocator metadata, while user-space heap activity also leaves telemetry stale.

2 open issues
What changed in this PR

Moves pipeline interim-heap management into LL/IPC user space, aiming to keep the kernel from processing user-writable allocator metadata while retaining Zephyr’s heap implementation.

Changes:

  • Adds a user-mode region allocator, heap-mode flags, and an interim-range syscall.
  • Updates pipeline lifecycle and allocation callers to use the new allocator.
  • Adds userspace tests and documents the allocation boundary.
File Description
zephyr/​test/​userspace/​test_ll_user_heap.c Tests allocation, access restrictions, and corrupted metadata.
zephyr/​test/​userspace/​README.md Documents the new heap tests.
zephyr/​syscall/​vregion.c Accepts region flags and validates interim-range requests.
zephyr/​lib/​vregion.c Reserves user-managed ranges, rejects kernel allocation, and reads usage counters.
zephyr/​Kconfig Configures user-mode heaps and their maximum count.
zephyr/​include/​rtos/​alloc.h Routes allocation contexts through user-aware helpers.
src/​lib/​vregion_user.c Implements user-mode heap allocation, locking, and statistics.
src/​lib/​objpool.c Uses user-aware allocation for region-backed object pools.
src/​lib/​CMakeLists.txt Conditionally builds the user-mode allocator.
src/​include/​sof/​userspace/​README.md Documents interim-heap ownership and the new syscall.
src/​include/​sof/​lib/​vregion.h Declares flags, allocator APIs, statistics, and fallbacks.
src/​audio/​pipeline/​pipeline-graph.c Creates and removes user heaps with pipeline lifecycle changes.
src/​audio/​module_adapter/​module_adapter.c Updates allocation helpers and preserves kernel-managed DP regions.

Review effort: Balanced

Comment thread zephyr/syscall/vregion.c
K_OOPS(K_SYSCALL_MEMORY_WRITE(vreg_start, sizeof(*vreg_start)));
K_OOPS(K_SYSCALL_MEMORY_WRITE(vreg_size, sizeof(*vreg_size)));
return z_impl_vregion_create_map(vreg_start, vreg_size);
return z_impl_vregion_create_map(vreg_start, vreg_size, flags);
Comment thread zephyr/lib/vregion.c
Comment on lines +154 to +157
size_t allocated = MIN(stats->allocated_bytes, vr->interim_size);

info->interim_alloc_bytes = (uint32_t)allocated;
info->interim_free_kb = (uint16_t)((vr->interim_size - allocated) >> 10);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants