From f69675fc028eac6db094de155a77e82bdcf52b18 Mon Sep 17 00:00:00 2001 From: Kai Vehmanen Date: Fri, 27 Nov 2026 16:33:09 +0200 Subject: [PATCH] vregion: create the interim heap on the allocating core vregion_set_interim() is called from pipeline_complete() on the pipeline core, also for the vregions of DP modules on other cores. It created the interim k_heap right away, so the heap metadata was written to cached memory on the pipeline core. The first interim allocation on the module's own core then read stale metadata and failed, although the heap was empty: [core 0] vregion.interim_heap_init: creating interim heap: lifetime used 6784, interim available 13696 at 0xa0212a80 [core 1] src_prepare: comp:0 0x1013 entry [core 1] vregion.interim_alloc: interim alloc failed for 80 bytes align 64 [core 1] module_adapter.module_prepare: comp:0 0x1013 error -12: module specific prepare failed Make vregion_set_interim() only mark the switch, and create the interim heap on the next allocation, as the comments already describe. The allocations after the switch are made on the module's own core, so the heap metadata is only accessed on that core. Fixes: 10c8e6b1a913 ("lib: vregion: lazy interim heap creation from remaining lifetime space") Signed-off-by: Kai Vehmanen --- zephyr/lib/vregion.c | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/zephyr/lib/vregion.c b/zephyr/lib/vregion.c index 7239c7644a68..2562a0c9c28d 100644 --- a/zephyr/lib/vregion.c +++ b/zephyr/lib/vregion.c @@ -101,6 +101,7 @@ struct vregion { /* current allocation mode */ enum vregion_mem_type type; /* LIFETIME at creation, switch to INTERIM */ + bool interim_pending; /* vregion_set_interim() called, heap not yet created */ /* interim heap - created lazily on first interim allocation */ struct interim_heap interim; /* interim heap */ @@ -259,6 +260,7 @@ __cold struct vregion *vregion_create(size_t memsize) /* start in lifetime allocation mode */ vr->type = VREGION_MEM_TYPE_LIFETIME; + vr->interim_pending = false; k_mutex_init(&vr->lock); /* The creator is the first user */ @@ -495,8 +497,17 @@ void z_impl_vregion_set_interim(struct vregion *vr) k_mutex_lock(&vr->lock, K_FOREVER); - interim_heap_init(vr); - vregion_tel_sync_locked(vr); + /* + * Only mark the switch here, the interim heap is created on the + * next allocation. The heap metadata is in cached memory, so it + * must be initialized on the core that allocates from it, but this + * is called on the pipeline core, which can differ from the core of + * a DP module in the pipeline. + */ + if (vr->type == VREGION_MEM_TYPE_LIFETIME && !vr->interim_pending) + vr->interim_pending = true; + else + LOG_WRN("vregion %p already in interim mode", (void *)vr); k_mutex_unlock(&vr->lock); } @@ -645,6 +656,11 @@ void *z_impl_vregion_alloc_align(struct vregion *vr, k_mutex_lock(&vr->lock, K_FOREVER); + if (vr->interim_pending) { + vr->interim_pending = false; + interim_heap_init(vr); + } + switch (vr->type) { case VREGION_MEM_TYPE_INTERIM: p = interim_alloc(&vr->interim, size, alignment);