Skip to content

Commit 85b8ae9

Browse files
peter-mitsishenrikbrixandersen
authored andcommitted
arch: xtensa: Fix arch_is_in_isr() race condition
Fixes a flaw in the the xtensa implementation of arch_is_in_isr() that could manifest on SMP systems. If the reading of the current CPU's nested interrupt count is not fully atomic on an SMP system, then an ill-timed context switch could occur leaving the caller reading the nested interrupt of a different CPU. Signed-off-by: Peter Mitsis <[email protected]>
1 parent 4e2b0cd commit 85b8ae9

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

arch/xtensa/include/kernel_arch_func.h

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,25 @@ static ALWAYS_INLINE void arch_cohere_stacks(struct k_thread *old_thread,
278278

279279
static inline bool arch_is_in_isr(void)
280280
{
281-
return arch_curr_cpu()->nested != 0U;
281+
uint32_t nested;
282+
283+
#if defined(CONFIG_SMP)
284+
/*
285+
* Lock interrupts on SMP to ensure that the caller does not migrate
286+
* to another CPU before we get to read the nested field.
287+
*/
288+
unsigned int key;
289+
290+
key = arch_irq_lock();
291+
#endif
292+
293+
nested = arch_curr_cpu()->nested;
294+
295+
#if defined(CONFIG_SMP)
296+
arch_irq_unlock(key);
297+
#endif
298+
299+
return nested != 0U;
282300
}
283301

284302
#ifdef __cplusplus

0 commit comments

Comments
 (0)