Skip to content

Conversation

@softwarecki
Copy link
Collaborator

This PR fixes a memory allocation bug for the ring buffer introduced by the hastily merged ##10379 . That change modified the way the ring buffer is allocated, switching to the sof_heap_alloc function. However, it did not properly handle the SOF_MEM_FLAG_USER_SHARED_BUFFER flag, which broke userspace support.

Additionally, the error handling for ring buffer creation was improved in the ipc_comp_connect function, and error code propagation was fixed in the comp_trigger_local function.

Update comp_trigger_local to return the error code returned
by the schedule_task function.

Signed-off-by: Adrian Warecki <adrian.warecki@intel.com>
Handle ring buffer allocation failure by jumping to the new free_unlocked
label that skips ll_unblock. Do not release the lock at this point because
it is acquired later.

Signed-off-by: Adrian Warecki <adrian.warecki@intel.com>
…lloc

Add missing support for the SOF_MEM_FLAG_USER_SHARED_BUFFER flag in the
sof_heap_alloc function.

Add a generic function is_heap_pointer that allows checking whether
a given pointer belongs to a given heap. It is used in the sof_heap_free
function to ensure that the freed pointer belongs to the specified heap,
and if not, to free it using rfree.

Signed-off-by: Adrian Warecki <adrian.warecki@intel.com>
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR addresses a memory allocation bug in the ring buffer handling introduced by PR #10379. The core issue was that the sof_heap_alloc function did not properly handle the SOF_MEM_FLAG_USER_SHARED_BUFFER flag, breaking userspace support. The fix ensures that buffers with this flag are allocated using rballoc_align instead of the standard heap allocator.

Key changes:

  • Added proper handling of SOF_MEM_FLAG_USER_SHARED_BUFFER flag in allocation/deallocation logic
  • Implemented heap pointer validation to ensure correct memory freeing
  • Fixed error handling and propagation in component connection and trigger functions

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
zephyr/lib/alloc.c Added is_heap_pointer helper function and updated sof_heap_alloc/sof_heap_free to properly handle shared buffer flags and validate heap ownership
src/ipc/ipc4/helper.c Fixed error handling path for ring buffer creation failure to avoid unlocking when lock wasn't acquired
src/include/sof/audio/component_ext.h Added early return on trigger error and propagated schedule_task return value
src/audio/module_adapter/module_adapter.c Initialized heap metadata fields required by new is_heap_pointer validation

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

case COMP_TRIGGER_START:
case COMP_TRIGGER_RELEASE:
schedule_task(dev->task, 0, dev->period);
ret = schedule_task(dev->task, 0, dev->period);
Copy link

Copilot AI Nov 29, 2025

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_task is captured but never checked or returned. If schedule_task fails after a successful trigger operation, the error is silently ignored. Consider checking ret and handling the error appropriately.

Copilot uses AI. Check for mistakes.
Copy link
Collaborator

@kv2019i kv2019i left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Btw, if you want to point to a specific commit you are fixing, you can use the "Fixes: commit-id ("desription")" notation. This is optional on SOF side, but can be useful to augment the commit text.

void sof_heap_free(struct k_heap *heap, void *addr)
{
if (heap && addr)
if (heap && addr && is_heap_pointer(heap, addr))
Copy link
Collaborator

Choose a reason for hiding this comment

The 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 .

Copy link
Member

@lgirdwood lgirdwood left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor comment.

* 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.
Copy link
Member

Choose a reason for hiding this comment

The 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.

@lgirdwood lgirdwood merged commit 9ff0a7a into thesofproject:main Dec 1, 2025
26 of 42 checks passed
@kv2019i
Copy link
Collaborator

kv2019i commented Dec 2, 2025

@softwarecki @lgirdwood This broke testbench and other non-zephyr builds https://github.com/thesofproject/sof/actions/runs/19784196486/job/56688397772

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.

6 participants