Skip to content

schedule: zephyr_ll_user: improve heap init and make it usable from user-space#10807

Draft
kv2019i wants to merge 4 commits into
thesofproject:mainfrom
kv2019i:202605-ll-sched-part1
Draft

schedule: zephyr_ll_user: improve heap init and make it usable from user-space#10807
kv2019i wants to merge 4 commits into
thesofproject:mainfrom
kv2019i:202605-ll-sched-part1

Conversation

@kv2019i
Copy link
Copy Markdown
Collaborator

@kv2019i kv2019i commented May 25, 2026

Series that expands the LL/system-user heap implementation and prepares for use from user-space.

Separate the state for LL scheduler memory into kernel and user
accessible resources. The pointer to the LL heap must be accessible
from user-space, so that user space can allocate memory and pass
the heap pointer as argument.

Signed-off-by: Kai Vehmanen <kai.vehmanen@linux.intel.com>
@kv2019i
Copy link
Copy Markdown
Collaborator Author

kv2019i commented May 25, 2026

For context, part of #10558

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR extends the Zephyr userspace support to provide a dedicated, shared low-latency (LL) userspace heap with embedded k_heap metadata, enabling safer use from userspace syscall verification paths and making the LL heap pointer accessible from userspace.

Changes:

  • Add sys_user_heap_init() / sys_user_heap_remove() to allocate/free a single page-aligned region containing both struct k_heap and its backing buffer.
  • Introduce CONFIG_SOF_ZEPHYR_SYS_USER_HEAP_SIZE to configure the shared LL userspace heap size.
  • Update the LL userspace scheduler resources to use the new heap and expose the heap pointer via a sysuser app-memory section, plus attach the sysuser partition to the LL domain.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 5 comments.

File Description
zephyr/lib/userspace_helper.c Adds shared LL userspace heap init/free with embedded k_heap metadata.
zephyr/Kconfig Adds configurable size option for the shared LL userspace heap.
zephyr/include/rtos/userspace_helper.h Exposes the new heap APIs and documents behavior/usage.
src/schedule/zephyr_ll_user.c Switches LL heap init to the new API and exposes a userspace-accessible heap pointer/partition.

Comment thread zephyr/include/rtos/userspace_helper.h Outdated
struct k_heap *module_driver_heap_init(void);

/**
* Initialize private processing module heap with embedded metadata.
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Fixed in V2.

Comment thread zephyr/include/rtos/userspace_helper.h Outdated
Comment on lines +68 to +70
* Free private processing module heap allocated by
* sys_user_heap_init().
* @param mod_drv_heap pointer to the k_heap structure.
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Fixed in V2.

Comment on lines +74 to +76
const size_t alloc_size = CONFIG_SOF_ZEPHYR_SYS_USER_HEAP_SIZE;
const size_t prefix_size = ALIGN_UP(sizeof(struct k_heap), 4);
const size_t kheap_size = alloc_size - prefix_size;
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Fixed in V2.

Comment thread zephyr/lib/userspace_helper.c Outdated
Comment on lines +111 to +114
void sys_user_heap_remove(struct k_heap *mod_drv_heap)
{
if (mod_drv_heap)
rfree(mod_drv_heap);
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Fixed in V2.

Comment on lines +73 to +78
/* store a user-accessible pointer */
zephyr_ll_heap = ll_mem_resources.heap;

/* attach common partition to LL domain */
user_memory_attach_common_partition(zephyr_ll_mem_domain());
user_memory_attach_system_user_partition(zephyr_ll_mem_domain());
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

The existing user_memory_attach() calls don't check the errors, so I prefer to leave them out here as well. We get decent errors for access permissions nowadays, so if this fails, we get a good error anyways.

kv2019i added 3 commits May 26, 2026 11:06
For user-space LL scheduler to implement full life-cycle
for modules, including calls to mod_free(). This requires that
heap metadata is also part of the memory partition mapped to
the user-thread. Implement a new sys_user_heap_init() that
allocates both the heap buffer and its metadata, into single
contiguous allocation, and returns the resulting object.
This then allows caller to map both metadata and the heap
to a user-space thread.

Signed-off-by: Kai Vehmanen <kai.vehmanen@linux.intel.com>
Use the new sys_user_heap_init() interface to allocate the system
user heap for LL scheduler.

Signed-off-by: Kai Vehmanen <kai.vehmanen@linux.intel.com>
Only double-map the LL resources if
CONFIG_CACHE_HAS_MIRRORED_MEMORY_REGIONS is set.

Signed-off-by: Kai Vehmanen <kai.vehmanen@linux.intel.com>
@kv2019i kv2019i force-pushed the 202605-ll-sched-part1 branch from 60a5e56 to 105633d Compare May 26, 2026 08:07
@kv2019i
Copy link
Copy Markdown
Collaborator Author

kv2019i commented May 26, 2026

V2 pushed:

  • address copilot comments, no other changes

@softwarecki
Copy link
Copy Markdown
Collaborator

I do not understand why you allocate the k_heap structure in user-accessible memory. It contains wait queue and k_spinlock, which are kernel synchronization primitives. Exposing them to user space allows unintended or malicious modification. This should be kept in kernel space.

@kv2019i
Copy link
Copy Markdown
Collaborator Author

kv2019i commented May 26, 2026

@softwarecki wrote:

I do not understand why you allocate the k_heap structure in user-accessible memory. It contains wait queue and k_spinlock, which are kernel synchronization primitives. Exposing them to user space allows unintended or malicious modification. This should be kept in kernel space.

You are completely right. I've been today working on upstream ready version of b11d04d , and adding the z_vrfy logic for sof_heap_alloc, and well, this approach doesn't work in the end. I was planning to verify the kernel objects before entering kernel mode, but alas, not working.

@kv2019i kv2019i marked this pull request as draft May 26, 2026 12:02
@softwarecki
Copy link
Copy Markdown
Collaborator

If you plan to place k_heap in cached memory, you need to add it to a memory domain so the kernel can access it as I handled it here: #10643 (comment)

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.

4 participants