Skip to content

Commit 60d4df8

Browse files
schedule: zephyr_ll: release LL lock held by a faulting user thread
In user-space LL builds zephyr_ll_run() keeps the per-core LL scheduler mutex locked while running the tasks. When a module faults in the LL thread, k_sys_fatal_error_handler() returns and z_fatal_error() aborts only that thread, but Zephyr does not release mutexes of aborted threads. The lock stays owned by the dead thread and the next user_ll_lock_sched() on that core pends forever. E.g. the IPC user thread triggering a pipeline stop blocks, and all further IPCs for that core time out with -EAGAIN in ipc_user_forward_cmd(). Add zephyr_ll_release_locks_of_current(), called from the fatal error handler while _current is still the faulting thread and owner of the lock. It looks the locks up in the kernel-only zephyr_ll_klocks[] copy, resets the shadow owner tracking and unlocks every recursion level, handing the lock over to the first waiter. Rescheduling is deferred until the exception returns. This only unblocks the waiters. The scheduler state is not repaired: the tasks run in the interrupted tick stay on the dead thread's temporary list and no further LL ticks happen on that core. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Ganesh, Aditya <aditya.ganesh@intel.com>
1 parent c681105 commit 60d4df8

4 files changed

Lines changed: 97 additions & 3 deletions

File tree

‎src/include/sof/schedule/zephyr_ll.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ int zephyr_ll_task_cancel(void *data, struct task *task);
9494
extern struct k_mutex *zephyr_ll_locks[CONFIG_CORE_COUNT];
9595

9696
struct k_thread *zephyr_ll_init_context(void *data, struct task *task);
97+
98+
/* fatal error handling, see zephyr_ll.c and zephyr_ll-kernel.c */
99+
void zephyr_ll_lock_shadow_reset(int core);
100+
int zephyr_ll_release_locks_of_current(void);
97101
#endif
98102

99103
struct zephyr_domain_thread {

‎src/kernel/schedule/zephyr_ll-kernel.c‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,55 @@ void user_ll_grant_access(struct k_thread *thread, int core)
219219

220220
k_thread_access_grant(thread, zephyr_ll_klocks[core]);
221221
}
222+
223+
/**
224+
* @brief Release all LL scheduler locks held by the faulting thread.
225+
*
226+
* Zephyr does not release mutexes when a thread is aborted. If a user-space
227+
* LL thread faults while running a module, it still holds its core's LL
228+
* scheduler lock (zephyr_ll_run() keeps it locked while running tasks), and
229+
* every later user_ll_lock_sched() on that core, e.g. from the IPC user
230+
* thread triggering a pipeline, pends forever.
231+
*
232+
* Must only be called from k_sys_fatal_error_handler(), before
233+
* z_fatal_error() aborts _current. The current thread is the mutex owner
234+
* there, so the regular unlock path is used to hand the lock over to the
235+
* first waiter. Rescheduling is deferred until the exception returns.
236+
*
237+
* Uses the kernel-only zephyr_ll_klocks[] copy: zephyr_ll_locks[] is
238+
* user-writable and must not be trusted here.
239+
*
240+
* @note The scheduler state protected by the lock is not repaired: tasks
241+
* run in the interrupted tick stay on the dead thread's temporary list
242+
* and no further LL ticks happen on that core.
243+
*
244+
* @return Number of LL scheduler locks released.
245+
*/
246+
int zephyr_ll_release_locks_of_current(void)
247+
{
248+
struct k_thread *thread = k_current_get();
249+
int released = 0;
250+
int core;
251+
252+
for (core = 0; core < CONFIG_CORE_COUNT; core++) {
253+
struct k_mutex *m = zephyr_ll_klocks[core];
254+
255+
if (!m || m->owner != thread)
256+
continue;
257+
258+
/* before unlocking: the new owner counts depth from 0 */
259+
zephyr_ll_lock_shadow_reset(core);
260+
261+
/* the mutex is recursive, drop every level */
262+
while (m->owner == thread)
263+
if (z_impl_k_mutex_unlock(m))
264+
break;
265+
266+
released++;
267+
}
268+
269+
return released;
270+
}
222271
#endif /* CONFIG_SOF_USERSPACE_LL */
223272

224273
#if CONFIG_SOF_BOOT_TEST_STANDALONE || CONFIG_LIBRARY

‎src/schedule/zephyr_ll.c‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,18 @@ static inline void zephyr_ll_lock_releasing(int core)
6464
zephyr_ll_lock_owner[core] = NULL;
6565
}
6666

67+
/*
68+
* Forget the recorded owner of the core's LL lock. Only for
69+
* zephyr_ll_release_locks_of_current(), when the owner is dying and its lock
70+
* is about to be force-released on its behalf. Must be called before the
71+
* unlock: the new owner's zephyr_ll_lock_acquired() counts depth from 0.
72+
*/
73+
void zephyr_ll_lock_shadow_reset(int core)
74+
{
75+
zephyr_ll_lock_owner[core] = NULL;
76+
zephyr_ll_lock_depth[core] = 0;
77+
}
78+
6779
#ifdef CONFIG_ASSERT
6880
void user_ll_assert_locked(int core)
6981
{

‎zephyr/wrapper.c‎

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
#include <sof/kernel/drivers/interrupt-map.h>
1414
#include <sof/lib/dma.h>
1515
#include <sof/schedule/schedule.h>
16+
#if CONFIG_SOF_USERSPACE_LL
17+
#include <sof/schedule/zephyr_ll.h>
18+
#endif
1619
#include <sof/lib/io.h>
1720
#include <sof/lib/memory.h>
1821
#include <sof/kernel/platform.h>
@@ -356,6 +359,28 @@ static bool sof_boot_test_fault_expected(void)
356359
}
357360
#endif
358361

362+
#if defined(CONFIG_XTENSA) && defined(CONFIG_USERSPACE)
363+
/**
364+
* @brief Release kernel resources the faulting user thread still holds.
365+
*
366+
* Called from k_sys_fatal_error_handler() before returning to
367+
* z_fatal_error(), which then aborts only the faulting user thread.
368+
* Zephyr does not release mutexes of aborted threads, so locks the thread
369+
* holds, e.g. the per-core LL scheduler lock, must be released here or
370+
* their waiters pend forever.
371+
*
372+
* @return Number of locks released.
373+
*/
374+
static int sof_user_fault_release_locks(void)
375+
{
376+
#if CONFIG_SOF_USERSPACE_LL
377+
return zephyr_ll_release_locks_of_current();
378+
#else
379+
return 0;
380+
#endif
381+
}
382+
#endif
383+
359384
void k_sys_fatal_error_handler(unsigned int reason,
360385
const struct arch_esf *esf)
361386
{
@@ -378,14 +403,18 @@ void k_sys_fatal_error_handler(unsigned int reason,
378403
*/
379404
if (reason == K_ERR_KERNEL_OOPS && _current &&
380405
k_thread_is_user_thread(_current)) {
381-
LOG_ERR("ERROR: kernel oops from user thread %p; aborting thread.",
382-
_current);
406+
int n = sof_user_fault_release_locks();
407+
408+
LOG_ERR("ERROR: kernel oops from user thread %p; released %d locks; "
409+
"aborting thread.", _current, n);
383410
return;
384411
}
385412

386413
if (reason != K_ERR_KERNEL_PANIC && xtensa_exception_is_user(esf)) {
414+
int n = sof_user_fault_release_locks();
415+
387416
LOG_ERR("ERROR: Unexpected fatal error %u in Xtensa user mode; "
388-
"aborting thread.\n", reason);
417+
"released %d locks; aborting thread.", reason, n);
389418
return;
390419
}
391420
#endif

0 commit comments

Comments
 (0)