From 5183fc5693832b6aa1fe1cab06e628681300d0fa Mon Sep 17 00:00:00 2001 From: Mathieu Choplain Date: Mon, 21 Jul 2025 14:09:47 +0200 Subject: [PATCH] kernel: assert no spinlock is held on swap when !USE_SWITCH The do_swap() routine used when CONFIG_USE_SWITCH=y asserts that caller thread does not hold any spinlock when CONFIG_SPIN_VALIDATE is enabled. However, there is no similar check in place when CONFIG_USE_SWITCH=n. Copy this assertion in the USE_SWITCH=n implementation of z_swap_irqlock(). Signed-off-by: Mathieu Choplain --- kernel/include/kswap.h | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/kernel/include/kswap.h b/kernel/include/kswap.h index 0d790ed3a17..cff3efab6e9 100644 --- a/kernel/include/kswap.h +++ b/kernel/include/kswap.h @@ -203,6 +203,16 @@ static inline int z_swap_irqlock(unsigned int key) { int ret; z_check_stack_sentinel(); + +#ifdef CONFIG_SPIN_VALIDATE + /* Refer to comment in do_swap() above for details */ +# ifndef CONFIG_ARM64 + __ASSERT(arch_irq_unlocked(key) || + _current->base.thread_state & (_THREAD_DUMMY | _THREAD_DEAD), + "Context switching while holding lock!"); +# endif /* CONFIG_ARM64 */ +#endif /* CONFIG_SPIN_VALIDATE */ + ret = arch_swap(key); return ret; }