Skip to content

Commit 6d09634

Browse files
committed
zephyr: lib: alloc: fine-tune logic in z_impl_sof_heap_alloc()
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>
1 parent 59a3f0d commit 6d09634

2 files changed

Lines changed: 19 additions & 3 deletions

File tree

zephyr/lib/alloc.c

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -631,11 +631,26 @@ EXPORT_SYMBOL(rfree);
631631
void *z_impl_sof_heap_alloc(struct k_heap *heap, uint32_t flags, size_t bytes,
632632
size_t alignment)
633633
{
634-
if (flags & (SOF_MEM_FLAG_LARGE_BUFFER | SOF_MEM_FLAG_USER_SHARED_BUFFER))
634+
#if CONFIG_SOF_USERSPACE_USE_SHARED_HEAP
635+
/*
636+
* FLAG_USER_SHARED_BUFFER maps to a specific heap, so
637+
* the passed 'heap' can be ignored
638+
*/
639+
if (flags & SOF_MEM_FLAG_USER_SHARED_BUFFER)
635640
return rballoc_align(flags, bytes, alignment);
641+
#endif
642+
643+
if (!heap) {
644+
/*
645+
* Ensure virtual heap is utilized for large buffers
646+
* if no heap is explicitly passed. rballoc_align()
647+
* has this logic.
648+
*/
649+
if (flags & SOF_MEM_FLAG_LARGE_BUFFER)
650+
return rballoc_align(flags, bytes, alignment);
636651

637-
if (!heap)
638652
heap = &sof_heap;
653+
}
639654

640655
if (flags & SOF_MEM_FLAG_COHERENT)
641656
return heap_alloc_aligned(heap, alignment, bytes);

zephyr/syscall/alloc.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ static inline void *z_vrfy_sof_heap_alloc(struct k_heap *heap, uint32_t flags,
1212
{
1313
/* only allow flags that are safe for user-space heap isolation */
1414
static const uint32_t allowed_flags =
15-
SOF_MEM_FLAG_USER | SOF_MEM_FLAG_COHERENT | SOF_MEM_FLAG_DMA;
15+
SOF_MEM_FLAG_USER | SOF_MEM_FLAG_COHERENT | SOF_MEM_FLAG_DMA |
16+
SOF_MEM_FLAG_LARGE_BUFFER;
1617

1718
K_OOPS(flags & ~allowed_flags);
1819

0 commit comments

Comments
 (0)