Skip to content

Conversation

@lyakh
Copy link
Collaborator

@lyakh lyakh commented Nov 19, 2025

Prepare further parts for the userspace conversion: the DP thread, fast-get and the ring-buffer

Copy link

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 prepares the userspace conversion by refactoring DP thread initialization, fast-get/fast-put APIs, and ring buffer allocation to support user-mode execution with proper heap management.

  • Moved DP thread creation from scheduler_dp_task_shedule to scheduler_dp_task_init to initialize threads earlier
  • Converted fast_get and fast_put to syscalls that accept a k_heap parameter for userspace compatibility
  • Updated ring buffer allocation to use heap-based allocation instead of global memory functions

Reviewed Changes

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

Show a summary per file
File Description
zephyr/lib/fast-get.c Converted fast_get/fast_put to syscalls with heap parameter and added verification wrappers
zephyr/CMakeLists.txt Added syscall header generation for fast-get.h
src/schedule/zephyr_dp_schedule.c Moved thread creation from schedule to init, introduced undeclared variable bug
src/library_manager/llext_manager.c Added memory domain partition management for loadable extensions
src/include/sof/llext_manager.h Added llext_manager_add_domain function declaration
src/include/sof/lib/fast-get.h Updated fast_get/fast_put signatures to include heap parameter as syscalls
src/include/sof/audio/component_ext.h Removed schedule_task_cancel from comp_trigger_local (moved to module_adapter_free)
src/include/sof/audio/audio_buffer.h Added heap pointer to sof_audio_buffer struct
src/audio/src/src_lite.c Removed unused set_configuration and get_configuration interface assignments
src/audio/src/src_common.h Removed declarations for unused src_set_config and src_get_config
src/audio/src/src_common.c Removed unused src_set_config and src_get_config implementations
src/audio/src/src.c Removed unused set_configuration and get_configuration interface assignments
src/audio/module_adapter/module_adapter.c Moved DP task initialization earlier and added schedule_task_cancel to free function
src/audio/module_adapter/module/generic.c Updated fast_get/fast_put calls to pass heap parameter
src/audio/buffers/ring_buffer.c Converted to heap-based allocation with proper memory management

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +622 to +632
pdata->thread_id = k_thread_create(pdata->thread, (__sparse_force void *)p_stack,
stack_size, dp_thread_fn, *task, NULL, NULL,
CONFIG_DP_THREAD_PRIORITY, (*task)->flags, K_FOREVER);

k_thread_access_grant(pdata->thread_id, pdata->sem);
scheduler_dp_grant(pdata->thread_id, cpu_get_id());

/* pin the thread to specific core */
ret = k_thread_cpu_pin(pdata->thread_id, core);
Copy link

Copilot AI Nov 19, 2025

Choose a reason for hiding this comment

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

The variable pdata is used but never declared in this function. It should either be declared as struct task_dp_pdata *pdata = &task_memory->pdata; before line 621, or all occurrences of pdata should be changed to task_memory->pdata.

Copilot uses AI. Check for mistakes.

#ifdef CONFIG_USERSPACE
if ((*task)->flags & K_USER) {
ret = user_memory_init_shared(pdata->thread_id, pdata->mod);
Copy link

Copilot AI Nov 19, 2025

Choose a reason for hiding this comment

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

The variable pdata is undeclared. Should use task_memory->pdata instead.

Copilot uses AI. Check for mistakes.
Comment on lines +647 to +650
k_sem_init(pdata->sem, 0, 1);
k_thread_start(pdata->thread_id);
Copy link

Copilot AI Nov 19, 2025

Choose a reason for hiding this comment

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

The variable pdata is undeclared. Should use task_memory->pdata instead.

Copilot uses AI. Check for mistakes.
return 0;

e_thread:
k_thread_abort(pdata->thread_id);
Copy link

Copilot AI Nov 19, 2025

Choose a reason for hiding this comment

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

The variable pdata is undeclared. Should use task_memory->pdata instead.

Copilot uses AI. Check for mistakes.
We need to move IPC processing for DP scheduled components into their
thread context. For that the thread has to be started early. Create
it immediately when creating DP task context. Note, that
k_thread_create() never fails, so no need to check for error.

Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
We need to run all module callbacks in DP thread context, for this
the thread has to be started early - before the first module callback
is called.

Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
DP processing threads should have as long as life time as possible to
process all the relevant IPCs in the thread context. Move thread
termination to be called immediately before freeing module data.

Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
src_set_config() and src_get_config() aren't used, they would return
an error if ever called. It's easier to just remove them.

Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
Add functions for adding LLEXT partitions to a memory domain for
user-space modules.

Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
Allocate the ring-buffer object on module heap too for DP access.

Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
The ring-buffer data buffer has to be accessible to user-space DP
modules, allocate it on module heap.

Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
@lyakh lyakh force-pushed the user-p2 branch 5 times, most recently from b38c2f7 to 0bb8ad5 Compare November 19, 2025 16:19
Copy link
Collaborator

@kv2019i kv2019i left a comment

Choose a reason for hiding this comment

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

Series looks good, question on last patch adding new syscalls.

#ifdef CONFIG_USERSPACE
void z_vrfy_fast_put(struct k_heap *heap, const void *sram_ptr)
{
z_impl_fast_put(heap, sram_ptr);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Shouldn't we have checks the user thread has access to 'heap' and 'sram_ptr'?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@kv2019i we should, yes, unfortunately it isn't very easy to check that perfectly... I've added some minimal checking, we need something better eventually

Copy link
Collaborator

Choose a reason for hiding this comment

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

Thanks for adding the checks. We should add TODO/FIXMEs if there are known gaps.

Pass a "heap" argument to fast_get() and fast_put() for user-space
DP allocations.

Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
Convert fast_get() and fast_put() to syscalls.

Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
#ifdef CONFIG_USERSPACE
void z_vrfy_fast_put(struct k_heap *heap, const void *sram_ptr)
{
K_OOPS(K_SYSCALL_MEMORY_WRITE(heap, sizeof(*heap)));
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is a bit surprising, I didn't realize k_heap object is fully visible to kernel space. But at least this is safe, it thread has write access, then this is ok.

#ifdef CONFIG_USERSPACE
void z_vrfy_fast_put(struct k_heap *heap, const void *sram_ptr)
{
z_impl_fast_put(heap, sram_ptr);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Thanks for adding the checks. We should add TODO/FIXMEs if there are known gaps.

@lgirdwood lgirdwood merged commit 4c4ca6a into thesofproject:main Nov 20, 2025
34 of 42 checks passed
@lyakh lyakh deleted the user-p2 branch November 20, 2025 18:23
@softwarecki
Copy link
Collaborator

Wow, that was merged really fast without giving a chance for review. Great job.

@softwarecki
Copy link
Collaborator

Additionally, this breaks building with CONFIG_USERSPACE enabled. Brilliant work!

sof/zephyr/lib/fast-get.c:176:2: warning: implicit declaration of function 'K_OOPS' is invalid in C99 [-Wimplicit-function-declaration]
        K_OOPS(K_SYSCALL_MEMORY_WRITE(heap, sizeof(*heap)));
        ^
sof/zephyr/lib/fast-get.c:176:9: warning: implicit declaration of function 'K_SYSCALL_MEMORY_WRITE' is invalid in C99 [-Wimplicit-function-declaration]
        K_OOPS(K_SYSCALL_MEMORY_WRITE(heap, sizeof(*heap)));
               ^
sof/zephyr/lib/fast-get.c:177:55: error: invalid application of 'sizeof' to an incomplete type 'struct z_heap'
        K_OOPS(K_SYSCALL_MEMORY_WRITE(heap->heap.heap, sizeof(*heap->heap.heap)));
                                                             ^~~~~~~~~~~~~~~~~~
zephyr/include/zephyr/sys/sys_heap.h:58:9: note: forward declaration of 'struct z_heap'
        struct z_heap *heap;
               ^
sof/zephyr/lib/fast-get.c:185:10: fatal error: 'zephyr/syscalls/mod_fast_put_mrsh.c' file not found
#include <zephyr/syscalls/mod_fast_put_mrsh.c>
         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2 warnings and 2 errors generated.

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