Skip to content

audio: module-adapter: make generic code compatible with user-space#10950

Open
kv2019i wants to merge 3 commits into
thesofproject:mainfrom
kv2019i:202606-module-adapt-userspace
Open

audio: module-adapter: make generic code compatible with user-space#10950
kv2019i wants to merge 3 commits into
thesofproject:mainfrom
kv2019i:202606-module-adapt-userspace

Conversation

@kv2019i

@kv2019i kv2019i commented Jun 22, 2026

Copy link
Copy Markdown
Collaborator

Series of patches to make generic module code safe to use from user-space.

Copilot AI review requested due to automatic review settings June 22, 2026 12:53
@kv2019i

kv2019i commented Jun 22, 2026

Copy link
Copy Markdown
Collaborator Author

For context, part of #10558

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.

Pull request overview

This PR aims to make generic module-adapter and data blob handling code safe/usable from user-space contexts by switching allocations to user-safe heap APIs and threading a heap choice through the data blob handler creation path.

Changes:

  • Extend comp_data_blob_handler_new_ext() with an optional struct k_heap *heap parameter and update the default wrapper.
  • Update generic module configuration/runtime allocations to use sof_heap_alloc()/sof_heap_free() (with sof_sys_user_heap_get()).
  • Pass the module allocator heap into comp_data_blob_handler_new_ext() from mod_data_blob_handler_new().

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
src/include/sof/audio/data_blob.h Extends blob handler constructor API with an optional heap parameter and updates the inline wrapper.
src/audio/module_adapter/module/generic.c Replaces config/runtime rballoc/rfree usage with sof_heap_alloc/sof_heap_free and passes allocator heap into blob handler creation.
src/audio/data_blob.c Stores heap in the handler, allocates/frees handler via heap when provided, and adds “default heap” alloc/free helpers.

Comment thread src/audio/data_blob.c Outdated
Comment thread src/include/sof/audio/data_blob.h Outdated
@kv2019i kv2019i force-pushed the 202606-module-adapt-userspace branch from 82045ae to dfc1660 Compare June 22, 2026 16:29
@kv2019i

kv2019i commented Jun 22, 2026

Copy link
Copy Markdown
Collaborator Author

V2 pushed:

  • simplify the changes to comp_data_blob_handler_new_ext(), the 3rd commit can be dropped

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.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.

Comment thread src/audio/data_blob.c
handler = rzalloc(SOF_MEM_FLAG_USER,
sizeof(struct comp_data_blob_handler));
handler = sof_heap_alloc(heap, SOF_MEM_FLAG_USER | SOF_MEM_FLAG_COHERENT,
sizeof(struct comp_data_blob_handler), 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.

could just use sof_sys_user_heap_get() directly as in other cases. But this one also adds COHERENT - that's on purpose, right? Maybe mention in the commit message

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.

Ok, fixing both. Earlier version of the patch stored a heap instance, but no need for that. And COHERENT is a mistake, we should align flags. Will fix.

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.

Solved in V3

/* No space for config available yet, allocate now */
dst->data = rballoc(SOF_MEM_FLAG_USER, size);
dst->data = sof_heap_alloc(sof_sys_user_heap_get(),
SOF_MEM_FLAG_USER, size, 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.

so, we're releasing some of VMH memory and using the standard heap here. Have you checked how big these allocations are typically and how many we switch? Do we need to reallocate some memory from VMH to the userspace 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.

Hmm, right. I don't think we should touch the virtual/direct heap mix in the same PRs as we add support for user-space LL use. And yeah, that's what I'm doing here and some of earlier rballoc(). I could use SOF_MEM_FLAG_LARGE_BUFFER to mark places where rballoc() was used, but that is hardcoded to kernel in current zephyr/alloc.c, so won't work either. But alas, some cleaner solution is needed. Sizing the VMH split is a much more complicated problem and I don't want to tackle that in these PRs...

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.

New attempt in V3 to preserve current behaviour (and vmh split) in kernel only builds.

@kv2019i kv2019i left a comment

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.

Inline responses... this will need more work.

Comment thread src/audio/data_blob.c
handler = rzalloc(SOF_MEM_FLAG_USER,
sizeof(struct comp_data_blob_handler));
handler = sof_heap_alloc(heap, SOF_MEM_FLAG_USER | SOF_MEM_FLAG_COHERENT,
sizeof(struct comp_data_blob_handler), 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.

Ok, fixing both. Earlier version of the patch stored a heap instance, but no need for that. And COHERENT is a mistake, we should align flags. Will fix.

/* No space for config available yet, allocate now */
dst->data = rballoc(SOF_MEM_FLAG_USER, size);
dst->data = sof_heap_alloc(sof_sys_user_heap_get(),
SOF_MEM_FLAG_USER, size, 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.

Hmm, right. I don't think we should touch the virtual/direct heap mix in the same PRs as we add support for user-space LL use. And yeah, that's what I'm doing here and some of earlier rballoc(). I could use SOF_MEM_FLAG_LARGE_BUFFER to mark places where rballoc() was used, but that is hardcoded to kernel in current zephyr/alloc.c, so won't work either. But alas, some cleaner solution is needed. Sizing the VMH split is a much more complicated problem and I don't want to tackle that in these PRs...

@kv2019i kv2019i marked this pull request as draft June 23, 2026 13:20
kv2019i added 3 commits June 23, 2026 19:46
Modify the special handling of SOF_MEM_FLAG_USER_SHARED_BUFFER and
SOF_MEM_FLAG_LARGE_BUFFER in z_impl_sof_heap_alloc().

First make the shared user heap special handling conditional on
CONFIG_SOF_USERSPACE_USE_SHARED_HEAP. This flag is a special case as it
is not a hint to the allocator but rather an explicit selector for a
specific heap (similar to SOF_MEM_FLAG_L3). If defined in build, and the
flag is passed to sof_heap_alloc(), the heap argument is ignored, and
instead the shared user heap instance is always used. No functional
change to current code.

Unlike the shared heap flag, SOF_MEM_FLAG_LARGE_BUFFER is a more
traditional flag, which serves as a hint to the allocator. Modify the
implementation to ignore this flag if the caller has passed a non-NULL
heap. Assumption here is that the heap user has passed is more suitable
for large buffers than use of default rballoc_align(), so the hint can
be ignored. This is especially important for cases where e.g. the caller
is allocating memory to a different memory domain and using default
rballoc_align() would result in access errors. If no heap is passed, the
existing logic to use rballoc_align() is used. Functionality is modified
only for cases where a custom heap is passed to alloc.

Signed-off-by: Kai Vehmanen <kai.vehmanen@linux.intel.com>
Use user-space friendy sof_heap_alloc() for dynamic allocations
in data_blob handler.

Signed-off-by: Kai Vehmanen <kai.vehmanen@linux.intel.com>
Replace direct rballoc() and rfree() calls with sof_heap_alloc()
and sof_heap_free(), and use sof_sys_user_heap_get() as the heap.
This makes the code ready to be used in user-space, including
module prepare and free stages.

Signed-off-by: Kai Vehmanen <kai.vehmanen@linux.intel.com>
@kv2019i kv2019i force-pushed the 202606-module-adapt-userspace branch from dfc1660 to 5431f77 Compare June 23, 2026 16:46
@kv2019i

kv2019i commented Jun 23, 2026

Copy link
Copy Markdown
Collaborator Author

V3 pushed:

  • modified zephyr/lib/alloc.h to allow passing FLAG_LARGE_BUFFER to sof_heap_alloc() (allows to replace rballoc() with sof_heap_alloc() and preserve current semantics and behaviour in kernel-only builds with virtual heap)
  • addressing other code review feedback
  • marking again ready for review

@kv2019i kv2019i marked this pull request as ready for review June 23, 2026 16:48
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