Skip to content

audio: dai-zephyr: enable user-space use of DAI, part 1#10801

Open
kv2019i wants to merge 4 commits into
thesofproject:mainfrom
kv2019i:202605-host-dai-user
Open

audio: dai-zephyr: enable user-space use of DAI, part 1#10801
kv2019i wants to merge 4 commits into
thesofproject:mainfrom
kv2019i:202605-host-dai-user

Conversation

@kv2019i
Copy link
Copy Markdown
Collaborator

@kv2019i kv2019i commented May 21, 2026

A series of of patches to enable dai-zephyr component to be used in user-space. This includes:

  • addition of new sof_umutex.h rtos interface that is used to implement DAI property locking (instead of spinlocks)
  • fixes to sof_dma.h (ability to use SG lists with loops)
  • make dai_get() work in user-space
  • fix use of dai_get_properties() to be safe to be used from user-space
  • modify heap allocations in dai-zephyr so that they will work also when building for user-space LL

Similar changes to host-zephyr are in #10799

kv2019i added 2 commits May 21, 2026 21:14
Allow a non-null pointer at the end of the DMA transfer block list,
if and only if it points to the first entry in the block list.

The SOF DAI module sets the DMA transfers blocks like this and
this change is required to use DAI module from user-space.

Signed-off-by: Kai Vehmanen <[email protected]>
The dai_get()/dai_put() provide a helper to access DAI devices.
When used in user-space, the wrapper struct should be created in
user-space memory.

Signed-off-by: Kai Vehmanen <[email protected]>
Copilot AI review requested due to automatic review settings May 21, 2026 18:22
@kv2019i kv2019i requested a review from jsarha May 21, 2026 18:22
@kv2019i
Copy link
Copy Markdown
Collaborator Author

kv2019i commented May 21, 2026

For context, part of #10558

Comment thread zephyr/Kconfig Outdated
Comment thread zephyr/test/CMakeLists.txt Outdated
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 enables the dai-zephyr component (and related infrastructure) to be usable from Zephyr user-space threads by introducing a user-space mutex abstraction, updating syscall verification, and adjusting allocations to work with user-space heaps/memory domains.

Changes:

  • Add sof_umutex API (Zephyr + POSIX) with syscall plumbing and a user-space test.
  • Update dai-zephyr to use sof_umutex for property protection and to use heap-aware allocation/free paths.
  • Update the SOF DMA syscall deep-copy logic to support cyclic (looping) SG block lists.

Reviewed changes

Copilot reviewed 16 out of 16 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
zephyr/test/userspace/test_umutex.c Adds a user-space thread test for sof_umutex init/lock/unlock/free.
zephyr/test/CMakeLists.txt Wires new user-space tests into the Zephyr test build.
zephyr/syscall/umutex.c Adds syscall verification handlers for sof_umutex_*.
zephyr/syscall/sof_dma.c Extends DMA block list deep-copy to allow looping SG lists.
zephyr/lib/umutex.c Implements kernel-side sof_umutex using dynamically allocated k_mutex.
zephyr/Kconfig Introduces Kconfig switches for userspace alloc + mutex interfaces and selects mutex for userspace LL.
zephyr/include/rtos/umutex.h Declares the sof_umutex API and syscall wrappers.
zephyr/include/rtos/mutex.h Exposes sof_umutex via the RTOS mutex umbrella header.
zephyr/CMakeLists.txt Adds umutex implementation and syscall sources/headers to the Zephyr build.
src/lib/dai.c Updates dai_get()/dai_put() allocation/free to use sof_heap_alloc/free (userspace LL aware).
src/ipc/ipc4/dai.c Uses heap-aware allocation/free for dd->dai_spec_config.
src/include/sof/lib/dai-zephyr.h Switches DAI property lock to sof_umutex and adds mod_alloc_ctx to dai_data.
src/include/sof/audio/component.h Moves mod_alloc_ctx definition earlier to satisfy DAI header needs.
src/audio/dai-zephyr.c Replaces spinlock-based property protection with sof_umutex and updates multiple allocations to use alloc_ctx.heap.
posix/include/rtos/umutex.h Adds POSIX header wrapper for sof_umutex.
posix/include/rtos/mutex.h Adds a POSIX stub implementation of sof_umutex.

Comment thread zephyr/test/CMakeLists.txt Outdated
Comment thread zephyr/syscall/umutex.c Outdated
Comment on lines +23 to +41
static inline int z_vrfy_sof_umutex_lock(struct sof_umutex *umutex,
k_timeout_t timeout)
{
K_OOPS(K_SYSCALL_MEMORY_WRITE(umutex, sizeof(struct sof_umutex)));
return z_impl_sof_umutex_lock(umutex, timeout);
}
#include <zephyr/syscalls/sof_umutex_lock_mrsh.c>

static inline int z_vrfy_sof_umutex_unlock(struct sof_umutex *umutex)
{
K_OOPS(K_SYSCALL_MEMORY_WRITE(umutex, sizeof(struct sof_umutex)));
return z_impl_sof_umutex_unlock(umutex);
}
#include <zephyr/syscalls/sof_umutex_unlock_mrsh.c>

static inline void z_vrfy_sof_umutex_free(struct sof_umutex *umutex)
{
K_OOPS(K_SYSCALL_MEMORY_WRITE(umutex, sizeof(struct sof_umutex)));
z_impl_sof_umutex_free(umutex);
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.

This is a valid concern. The verification is similar to zephyr sys_mutex() but it has additional step to look up the k_object() that is not here. Let me mark as draft to ensure this is addressed before merge.

Comment thread zephyr/syscall/umutex.c Outdated
Comment thread zephyr/lib/umutex.c Outdated
Comment on lines +18 to +35
int z_impl_sof_umutex_init(struct sof_umutex *umutex)
{
struct k_mutex *m;
int ret;

m = k_object_alloc(K_OBJ_MUTEX);
if (m == NULL) {
return -ENOMEM;
}

ret = k_mutex_init(m);
if (ret) {
k_object_free(m);
return ret;
}

umutex->mutex = m;
return 0;
Comment thread src/audio/dai-zephyr.c Outdated
Comment thread src/audio/dai-zephyr.c
dd->dma_buffer = NULL;
dma_sg_free(NULL, &config->elem_array);
rfree(dd->z_config);
dma_sg_free(dd->alloc_ctx.heap, &config->elem_array);
Copy link
Copy Markdown
Collaborator Author

@kv2019i kv2019i May 22, 2026

Choose a reason for hiding this comment

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

This is a valid problem, but problem exists in existing code, so I prefer to handle this case in a follow-up PR. If I need to revise this series, I will add a new commit to address this.
UPDATE: actually, this is not a real leak. Only error case where dd->z_config is allocated and non-zero, is the case where dai_set_dma_config() fails. But if that fails, nothing is allocated to dd->z_config->head_block either. So current code in main is safe.

@kv2019i kv2019i marked this pull request as draft May 21, 2026 18:44
Copy link
Copy Markdown
Collaborator

@lyakh lyakh left a comment

Choose a reason for hiding this comment

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

Yes, if possible, the kobject should be checked

Comment thread src/audio/dai-zephyr.c
Comment thread zephyr/test/userspace/test_umutex.c Outdated
Comment thread src/audio/dai-zephyr.c Outdated
Comment thread src/audio/dai-zephyr.c
dev->ipc_config = *config;

dd = rzalloc(SOF_MEM_FLAG_USER | SOF_MEM_FLAG_COHERENT, sizeof(*dd));
dd = sof_heap_alloc(heap, SOF_MEM_FLAG_USER | SOF_MEM_FLAG_COHERENT, sizeof(*dd), 0);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

just NULL

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.

I went back and forth between "heap" variable and just putting NULL, but in the end I think this is more readable and makes it explicit same value needs to be passed to alloc and free in this function. Compilation output should be the same in both cases.

kv2019i added 2 commits May 22, 2026 14:00
Reorder redefinitions to ensure "struct mod_alloc_ctx" is defined
before lib/dai.h is included.

Signed-off-by: Kai Vehmanen <[email protected]>
Convert all memory allocations to use the sof_heap_alloc() interface
and pass the dai_data specific heap object. This makes dai-zephyr
code compatible with use from user-space, but does not affect
kernel space use.

Signed-off-by: Kai Vehmanen <[email protected]>
@kv2019i kv2019i force-pushed the 202605-host-dai-user branch from 9fdefeb to 37702da Compare May 22, 2026 11:26
@kv2019i
Copy link
Copy Markdown
Collaborator Author

kv2019i commented May 22, 2026

V2 pushed:

  • drop the changes to DAI properties locking, this requires some more thought'n'work (now reviewing the origina commit in copier: fix allocation and configuration size #6930 where this locking was dded)
  • also dropped umutex.h as it was needed for the properties
  • keep the remaining patches

@kv2019i kv2019i changed the title audio: dai-zephyr: enable user-space use of DAI audio: dai-zephyr: enable user-space use of DAI, part 1 May 22, 2026
@kv2019i kv2019i marked this pull request as ready for review May 22, 2026 11:43
Copy link
Copy Markdown
Collaborator Author

@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.

Replied to code review comments. V2 should be ready.

Comment thread src/audio/dai-zephyr.c
dev->ipc_config = *config;

dd = rzalloc(SOF_MEM_FLAG_USER | SOF_MEM_FLAG_COHERENT, sizeof(*dd));
dd = sof_heap_alloc(heap, SOF_MEM_FLAG_USER | SOF_MEM_FLAG_COHERENT, sizeof(*dd), 0);
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.

I went back and forth between "heap" variable and just putting NULL, but in the end I think this is more readable and makes it explicit same value needs to be passed to alloc and free in this function. Compilation output should be the same in both cases.

Comment thread src/audio/dai-zephyr.c
dd->dma_buffer = NULL;
dma_sg_free(NULL, &config->elem_array);
rfree(dd->z_config);
dma_sg_free(dd->alloc_ctx.heap, &config->elem_array);
Copy link
Copy Markdown
Collaborator Author

@kv2019i kv2019i May 22, 2026

Choose a reason for hiding this comment

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

This is a valid problem, but problem exists in existing code, so I prefer to handle this case in a follow-up PR. If I need to revise this series, I will add a new commit to address this.
UPDATE: actually, this is not a real leak. Only error case where dd->z_config is allocated and non-zero, is the case where dai_set_dma_config() fails. But if that fails, nothing is allocated to dd->z_config->head_block either. So current code in main is safe.

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.

3 participants