Skip to content

user-dp: keep DP module regions and their owner kernel-side (on top of PR11563) - #11610

Draft
kv2019i wants to merge 12 commits into
thesofproject:mainfrom
kv2019i:202611-dp-heap-own
Draft

kv2019i wants to merge 12 commits into
thesofproject:mainfrom
kv2019i:202611-dp-heap-own

Conversation

@kv2019i

@kv2019i kv2019i commented Dec 3, 2026

Copy link
Copy Markdown
Collaborator

Depends on #11563 (user-mode interim heap of the pipeline regions)

This is the first step towards letting the DP thread of a module run the interim heap of its own region. It moves the trust decisions about DP module regions out of module memory that user threads can write, and into kernel-only state.

  • DP region from the kernel: scheduler_dp_task_init() now takes the region of the DP module from the kernel and keeps a reference in the kernel-only task record. It builds the DP memory partitions from the kernel-only struct vregion, not from the allocation context in module memory. Before, LL and IPC user threads could get the kernel to map other memory into the DP domain.
  • Region owner: the DP thread is recorded as the owner of its module region (vregion_owner_set()). The vregion system calls accept the owner, but only for the lifetime allocator of its own region (vregion_owner_check()), and kill any other DP thread. The kernel never runs the interim heap for a DP thread.
  • LL module allowlist: LL modules created in user mode are registered in LL memory. The LL and IPC user threads use the user-mode resource path only for registered modules; any other module goes through the system calls. Before, a DP thread could mark its module as an LL module, or pass a made-up module, and an IPC user thread would then walk DP-controlled resource state with access to all LL memory.
  • Tests and docs: standalone test cases for the owner and the allowlist, and an update to the userspace README.

Testing: on PTL, the standalone userspace suites pass 12 of 12, and the full LL regression passes. The regression covered chain DMA, HDA, multiple-pipeline, DP streams, D3 and LL SRC, plus 50 DP create/free cycles with no page leak. Every commit builds, and the mtl, lnl, ptl, imx8m and testbench builds pass.

🤖 Generated with Claude Code

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(). It
does so right away, not on the next allocation like for the k_heap, as
the kernel writes no heap metadata and the users set up their heap
through the uncached alias. 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. As vregion_create_map()
maps those into the LL memory domain too, the system call can not
require the flag before the DP thread runs the interim heap of its
region.

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. As
the allocations no longer enter the kernel, it reads them each time it
collects the region data.

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, but
telemetry reports the usage, 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>
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 <kai.vehmanen@linux.intel.com>
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 <kai.vehmanen@linux.intel.com>
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.
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 <kai.vehmanen@linux.intel.com>
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 <kai.vehmanen@linux.intel.com>
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 <kai.vehmanen@linux.intel.com>
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 <kai.vehmanen@linux.intel.com>
@kv2019i
kv2019i requested a balanced review from Copilot December 3, 2026 15:56
@kv2019i

kv2019i commented Dec 3, 2026

Copy link
Copy Markdown
Collaborator Author

Keeping as draft until #11563 is merged. Submitting for early review and CI dry run.

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

Critical lifetime-enforcement gaps still allow kernel operations on DP-writable interim heaps, while DP allocation/free wrappers can fault before reaching permitted syscalls.

4 open issues
What changed in this PR

Building on #11563, this PR moves DP region trust decisions into kernel state and tightens userspace allocation boundaries.

Changes:

  • Track DP regions and their owners in kernel-only state.
  • Add user-mode pipeline heaps and an LL module resource allowlist.
  • Extend userspace tests and documentation.
File Description
zephyr/​test/​userspace/​test_ll_user_heap.c Adds heap, ownership, and allowlist tests.
zephyr/​test/​userspace/​README.md Documents interim-heap tests.
zephyr/​syscall/​vregion.c Adds owner-aware allocation validation.
zephyr/​lib/​vregion.c Implements region ownership and heap modes.
zephyr/​Kconfig Configures user-mode region heaps.
zephyr/​include/​rtos/​alloc.h Routes context allocations through user-heap helpers.
src/​lib/​vregion_user.c Implements user-mode interim heaps.
src/​lib/​objpool.c Uses user-aware region allocation helpers.
src/​lib/​CMakeLists.txt Conditionally builds user-heap support.
src/​kernel/​schedule/​zephyr_dp_schedule_thread-kernel.c Updates the scheduler initialization signature.
src/​kernel/​schedule/​zephyr_dp_schedule_application-kernel.c Retains trusted regions and manages ownership.
src/​include/​sof/​userspace/​README.md Documents allocation boundaries and ownership.
src/​include/​sof/​schedule/​dp_schedule.h Adds the trusted-region scheduler parameter.
src/​include/​sof/​lib/​vregion.h Declares ownership and user-heap APIs.
src/​include/​sof/​audio/​pipeline.h Updates DP task initialization API.
src/​include/​sof/​audio/​module_adapter/​module/​generic.h Declares LL resource registration helpers.
src/​audio/​pipeline/​pipeline-schedule.c Passes trusted regions to the scheduler.
src/​audio/​pipeline/​pipeline-graph.c Manages pipeline user-heap lifecycle.
src/​audio/​module_adapter/​module/​generic.c Adds the LL module resource allowlist.
src/​audio/​module_adapter/​module_adapter.c Registers LL modules and passes DP regions directly.

Review effort: Balanced

Comment thread src/lib/vregion_user.c
Comment on lines +77 to +78
for (i = 0; i < ARRAY_SIZE(vregion_user_heaps); i++)
if (vregion_user_heaps[i].vr == vr)
Comment thread zephyr/lib/vregion.c
Comment on lines +795 to +799
if (lifetime_only) {
/* the kernel does not run a heap for the owner thread */
LOG_ERR("error: interim alloc of %zu from %p by its owner refused",
size, (void *)vr);
p = NULL;
Comment thread zephyr/syscall/vregion.c
Comment on lines +31 to +34
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);
Comment on lines +507 to +509
/* like a region too small for a kernel interim heap, not fatal here */
if (vregion_user_heap_add(p->alloc->vreg) < 0)
pipe_warn(p, "no interim heap");
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