From ddee1ab4cf39c058ea00b755ee9bfe488437657c Mon Sep 17 00:00:00 2001 From: Andy Ross Date: Wed, 19 Feb 2025 14:57:05 -0800 Subject: [PATCH] kernel/sched: Correct locking in essential thread panic Calling a (handled/ignored) panic with the scheduler lock held produces spinlock errors in some circumstances, depending on whether or not the swap gets reached before another context switch. Release the lock around the call, we don't touch any scheduler state on the path to z_swap(), so this is safe. Signed-off-by: Andy Ross --- kernel/sched.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/kernel/sched.c b/kernel/sched.c index 6a7e89e8b8d..d5fe15ede44 100644 --- a/kernel/sched.c +++ b/kernel/sched.c @@ -434,7 +434,9 @@ static ALWAYS_INLINE void z_thread_halt(struct k_thread *thread, k_spinlock_key_ halt_thread(thread, terminate ? _THREAD_DEAD : _THREAD_SUSPENDED); if ((thread == _current) && !arch_is_in_isr()) { if (z_is_thread_essential(thread)) { + k_spin_unlock(&_sched_spinlock, key); k_panic(); + key = k_spin_lock(&_sched_spinlock); } z_swap(&_sched_spinlock, key); __ASSERT(!terminate, "aborted _current back from dead");