Skip to content

audio: comp_buffer: restore virtual heap routing for mem allocation - #11182

Open
wjablon1 wants to merge 1 commit into
thesofproject:mainfrom
wjablon1:com_buff_alloc_fix
Open

audio: comp_buffer: restore virtual heap routing for mem allocation#11182
wjablon1 wants to merge 1 commit into
thesofproject:mainfrom
wjablon1:com_buff_alloc_fix

Conversation

@wjablon1

@wjablon1 wjablon1 commented Sep 9, 2026

Copy link
Copy Markdown
Contributor

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.

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>

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.

🟡 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_BUFFER on buffer stream allocations/resizes to restore virtual-heap routing in the affected configuration.
  • Removes direct sof_heap_alloc()/sof_heap_free() fallbacks in comp_buffer.c for 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 original flags, but the allocation now always adds SOF_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 size with SOF_MEM_FLAG_LARGE_BUFFER added. 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.

Comment on lines 412 to 418
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;
}
Comment on lines +273 to 276
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);
@intel-sofci

Copy link
Copy Markdown

PR 11182: test results

Run date: 2026-09-09 12:21 UTC

Tested commit: 168eb4f204eaf33003d31e3c116d702a03ee21d8

mtl pass rate lnl pass rate ptl pass rate wcl pass rate nvl pass rate

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);

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.

what are actually cases when alloc == NULL? This appears to interfere with be10674 @serhiy-katsyuba-intel ?

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.

4 participants