audio: comp_buffer: restore virtual heap routing for mem allocation - #11182
audio: comp_buffer: restore virtual heap routing for mem allocation#11182wjablon1 wants to merge 1 commit into
Conversation
For SOF_USERSPACE_LL=n, CONFIG_USERSPACE=y configuration, the component buffers are allocated out of sof_heap instead of virtual_heap (alloc_ctx->heap == NULL + lack of SOF_MEM_FLAG_LARGE_BUFFER fall straight to sof_heap) which leads to out-of-memory errors for certain scenarios. This commit fixes that by using sof_ctx_alloc and sof_ctx_free for all the configurations as it turns out these already have proper fallbacks in place when used with SOF_MEM_FLAG_LARGE_BUFFER flag. Signed-off-by: Wojciech Jablonski <wojciech.jablonski@intel.com>
There was a problem hiding this comment.
🟡 Changes recommended
buffer_set_size_range() can incorrectly reinitialize the stream to size 0 (and potentially return success) when all allocation attempts fail, which is a functional correctness issue in OOM scenarios.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Pull request overview
This PR fixes an out-of-memory scenario in Zephyr userspace builds by ensuring component buffer backing memory is allocated via sof_ctx_alloc() with the SOF_MEM_FLAG_LARGE_BUFFER flag, so the allocator can correctly route large allocations to the virtual heap (when applicable) even when alloc_ctx->heap == NULL.
Changes:
- Simplifies buffer data allocation/free paths to consistently use
sof_ctx_alloc()/sof_ctx_free(). - Forces
SOF_MEM_FLAG_LARGE_BUFFERon buffer stream allocations/resizes to restore virtual-heap routing in the affected configuration. - Removes direct
sof_heap_alloc()/sof_heap_free()fallbacks incomp_buffer.cfor buffer stream memory.
File summaries
| File | Description |
|---|---|
| src/audio/buffers/comp_buffer.c | Routes stream allocations/frees through sof_ctx_alloc/free with SOF_MEM_FLAG_LARGE_BUFFER to leverage allocator fallbacks (incl. virtual heap). |
Review details
Suppressed comments (2)
src/audio/buffers/comp_buffer.c:317
buffer_alloc_range()logs/returns errors using the originalflags, but the allocation now always addsSOF_MEM_FLAG_LARGE_BUFFER. Logging the effective flags (and only logging "allocated" after success) avoids confusing traces when allocations fail.
for (size = preferred_size; size >= minimum_size; size -= minimum_size) {
stream_addr = sof_ctx_alloc(alloc, flags | SOF_MEM_FLAG_LARGE_BUFFER,
size, align);
if (stream_addr)
break;
}
src/audio/buffers/comp_buffer.c:372
- On resize failure, the error log reports the current buffer size and the original flags, but the attempted allocation is for the requested
sizewithSOF_MEM_FLAG_LARGE_BUFFERadded. Logging the requested size and effective flags will make OOM debugging much clearer.
new_ptr = sof_ctx_alloc(alloc, buffer->flags | SOF_MEM_FLAG_LARGE_BUFFER, size, alignment);
/* we couldn't allocate bigger chunk */
if (!new_ptr && size > audio_stream_get_size(&buffer->stream)) {
buf_err(buffer, "resize can't alloc %u bytes of flags 0x%x",
audio_stream_get_size(&buffer->stream), buffer->flags);
return -ENOMEM;
- Files reviewed: 1/1 changed files
- Comments generated: 2
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| for (new_size = preferred_size; new_size >= minimum_size; | ||
| new_size -= minimum_size) { | ||
| if (alloc) | ||
| new_ptr = sof_ctx_alloc(alloc, buffer->flags, new_size, alignment); | ||
| else | ||
| new_ptr = sof_heap_alloc(sof_sys_user_heap_get(), | ||
| buffer->flags | SOF_MEM_FLAG_LARGE_BUFFER, | ||
| new_size, alignment); | ||
|
|
||
| new_ptr = sof_ctx_alloc(alloc, buffer->flags | SOF_MEM_FLAG_LARGE_BUFFER, | ||
| new_size, alignment); | ||
| if (new_ptr) | ||
| break; | ||
| } |
| stream_addr = sof_ctx_alloc(alloc, flags | SOF_MEM_FLAG_LARGE_BUFFER, size, align); | ||
| if (!stream_addr) { | ||
| tr_err(&buffer_tr, "could not alloc size = %zu bytes of flags = 0x%x", | ||
| size, flags); |
PR 11182: test resultsRun date: 2026-09-09 12:21 UTC Tested commit: 168eb4f204eaf33003d31e3c116d702a03ee21d8 |
| sof_ctx_free(alloc, buffer->stream.addr); | ||
| else | ||
| sof_heap_free(sof_sys_user_heap_get(), buffer->stream.addr); | ||
| sof_ctx_free(alloc, buffer->stream.addr); |
There was a problem hiding this comment.
what are actually cases when alloc == NULL? This appears to interfere with be10674 @serhiy-katsyuba-intel ?
For SOF_USERSPACE_LL=n, CONFIG_USERSPACE=y configuration, the component buffers are allocated out of sof_heap instead of virtual_heap (alloc_ctx->heap == NULL + lack of SOF_MEM_FLAG_LARGE_BUFFER fall straight to sof_heap) which leads to out-of-memory errors for certain scenarios.
This commit fixes that by using sof_ctx_alloc and sof_ctx_free for all the configurations as it turns out these already have proper fallbacks in place when used with SOF_MEM_FLAG_LARGE_BUFFER flag.