-
Notifications
You must be signed in to change notification settings - Fork 349
alloc: sof_heap: Fix ring buffer memory allocation #10402
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
Changes from all commits
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 |
|---|---|---|
|
|
@@ -155,6 +155,25 @@ extern char _end[], _heap_sentry[]; | |
|
|
||
| static struct k_heap sof_heap; | ||
|
|
||
| /** | ||
| * Checks whether pointer is from a given heap memory. | ||
| * @param heap Pointer to a heap. | ||
| * @param ptr Pointer to memory being checked. | ||
| * @return True if pointer falls into heap memory region, false otherwise. | ||
|
Member
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. Might be worth to spell out in the comments that this API doesn't tell us if memory ptr is currently allocated or free. |
||
| */ | ||
| static bool is_heap_pointer(const struct k_heap *heap, void *ptr) | ||
| { | ||
| uintptr_t heap_start = | ||
| POINTER_TO_UINT(sys_cache_cached_ptr_get(heap->heap.init_mem)); | ||
| uintptr_t heap_end = heap_start + heap->heap.init_bytes; | ||
|
|
||
| if (!is_cached(ptr)) | ||
| ptr = (__sparse_force void *)sys_cache_cached_ptr_get(ptr); | ||
|
|
||
| return ((POINTER_TO_UINT(ptr) >= heap_start) && | ||
| (POINTER_TO_UINT(ptr) < heap_end)); | ||
| } | ||
|
|
||
| #if CONFIG_SOF_USERSPACE_USE_SHARED_HEAP | ||
| static struct k_heap shared_buffer_heap; | ||
|
|
||
|
|
@@ -628,7 +647,7 @@ EXPORT_SYMBOL(rfree); | |
| void *sof_heap_alloc(struct k_heap *heap, uint32_t flags, size_t bytes, | ||
| size_t alignment) | ||
| { | ||
| if (flags & SOF_MEM_FLAG_LARGE_BUFFER) | ||
| if (flags & (SOF_MEM_FLAG_LARGE_BUFFER | SOF_MEM_FLAG_USER_SHARED_BUFFER)) | ||
| return rballoc_align(flags, bytes, alignment); | ||
|
|
||
| if (!heap) | ||
|
|
@@ -642,7 +661,7 @@ void *sof_heap_alloc(struct k_heap *heap, uint32_t flags, size_t bytes, | |
|
|
||
| void sof_heap_free(struct k_heap *heap, void *addr) | ||
| { | ||
| if (heap && addr) | ||
| if (heap && addr && is_heap_pointer(heap, 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. This seems a bit complex, but this would indeed seem to be a needed check, also for SOF_MEM_FLAG_LARGE_BUFFER . |
||
| heap_free(heap, addr); | ||
| else | ||
| rfree(addr); | ||
|
|
||
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.
The return value of
schedule_taskis captured but never checked or returned. Ifschedule_taskfails after a successful trigger operation, the error is silently ignored. Consider checkingretand handling the error appropriately.