-
Notifications
You must be signed in to change notification settings - Fork 358
schedule: zephyr_ll_user: improve heap init and make it usable from user-space #10807
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
9681d96
a363950
c2e7fe6
a416a76
924f439
a33d385
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -99,9 +99,26 @@ void rfree(void *ptr); | |
| */ | ||
| void l3_heap_save(void); | ||
|
|
||
| void *z_impl_sof_heap_alloc(struct k_heap *heap, uint32_t flags, size_t bytes, | ||
| size_t alignment); | ||
|
|
||
| void z_impl_sof_heap_free(struct k_heap *heap, void *addr); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hm, just occurred to me - I think these declarations are already present in that zephyr/syscalls/alloc.h inccluded below? So maybe they're only needed here under the
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And not even under else, removed in V5. |
||
|
|
||
| /* | ||
| * This is ugly to define the signatures twice, but this | ||
| * is required to support userspace builds that do not export alloc. | ||
| */ | ||
| #ifdef CONFIG_SOF_USERSPACE_INTERFACE_ALLOC | ||
| __syscall void *sof_heap_alloc(struct k_heap *heap, uint32_t flags, size_t bytes, | ||
| size_t alignment); | ||
| __syscall void sof_heap_free(struct k_heap *heap, void *addr); | ||
| #include <zephyr/syscalls/alloc.h> | ||
| #else | ||
| void *sof_heap_alloc(struct k_heap *heap, uint32_t flags, size_t bytes, | ||
| size_t alignment); | ||
| void sof_heap_free(struct k_heap *heap, void *addr); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. in other such cases we did
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right, that would have been clean, but I feel really uneasy to have a define for something as generic as "sof_heap_alloc" in a very widely included header like rtos/alloc.h. I kept the uglier (but more conservative) code now in V5. |
||
| #endif | ||
|
|
||
| #if CONFIG_SOF_FULL_ZEPHYR_APPLICATION | ||
| struct k_heap *sof_sys_heap_get(void); | ||
| #else | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| // SPDX-License-Identifier: BSD-3-Clause | ||
| // | ||
| // Copyright(c) 2026 Intel Corporation. | ||
|
|
||
| #include <rtos/alloc.h> | ||
| #include <sof/schedule/ll_schedule_domain.h> | ||
| #include <zephyr/kernel.h> | ||
| #include <zephyr/internal/syscall_handler.h> | ||
|
|
||
| static inline void *z_vrfy_sof_heap_alloc(struct k_heap *heap, uint32_t flags, | ||
| size_t bytes, size_t alignment) | ||
| { | ||
| /* only allow flags that are safe for user-space heap isolation */ | ||
| static const uint32_t allowed_flags = | ||
| SOF_MEM_FLAG_USER | SOF_MEM_FLAG_COHERENT | SOF_MEM_FLAG_DMA; | ||
|
|
||
| K_OOPS(flags & ~allowed_flags); | ||
|
|
||
| /* user-space use of sof_heap_alloc() limited to this single heap */ | ||
| K_OOPS(!zephyr_ll_user_heap_verify(heap)); | ||
|
|
||
| return z_impl_sof_heap_alloc(heap, flags, bytes, alignment); | ||
| } | ||
| #include <zephyr/syscalls/sof_heap_alloc_mrsh.c> | ||
|
|
||
| static inline void z_vrfy_sof_heap_free(struct k_heap *heap, void *addr) | ||
| { | ||
| /* user-space use of sof_heap_alloc() limited to this single heap */ | ||
| K_OOPS(!zephyr_ll_user_heap_verify(heap)); | ||
|
|
||
| if (addr) { | ||
| uintptr_t start = (uintptr_t)heap->heap.init_mem; | ||
| uintptr_t addr_uc = (uintptr_t)sys_cache_uncached_ptr_get(addr); | ||
| K_OOPS(addr_uc < start || addr_uc >= start + heap->heap.init_bytes); | ||
| K_OOPS(K_SYSCALL_MEMORY_WRITE(addr, 1)); | ||
| } | ||
| z_impl_sof_heap_free(heap, addr); | ||
| } | ||
| #include <zephyr/syscalls/sof_heap_free_mrsh.c> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could this be
? Advantage -
const,.rodata, potential disadvantage - you'd need to make sure not to try to use it before the heap is initialised. But that should be easy enough to check too, e.g. by checking the.heap.init_mempointer. Can be a follow-upThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, possibly yes. I'd like to keep the current code open still. We might be changing this many times still as we move to use vregion/pages more.