zephyr: Meta-IRQ does not preempt cooperative threads when woken from IRQ (cortex_m)

Describe the bug I believe commit 42036cd broke the ability of Meta-IRQs to preempt all other threads even if those are cooperative. This was working as expected in Zephyr 3.5.0

Z_GENERIC_SECTION(.text._HandlerModeExit) void z_arm_exc_exit(void)
{
#ifdef CONFIG_PREEMPT_ENABLED
	/* If thread is preemptible */
	if (_kernel.cpus->current->base.prio >= 0) {
		/* and cached thread is not current thread */
		if (_kernel.ready_q.cache != _kernel.cpus->current) {
			/* trigger a context switch */
			SCB->ICSR |= SCB_ICSR_PENDSVSET_Msk;
		}
	}
#endif /* CONFIG_PREEMPT_ENABLED */

if (_kernel.cpus->current->base.prio >= 0) prevents the meta-IRQ from being woken when returning from an interrupt unblocking it with e.g sem_give()

The “promise” as stated in the documentation is broken: The thread, once woken, will be guaranteed to run before the current CPU returns into application code.

To Reproduce Have a cooperative thread doing some processing and have a meta-IRQ thread waiting for a semaphore. Unblock the meta-IRQ by calling a sem_give() from IRQ context. The meta-irq will not be woken immediately but I believe at the first possible reschedule point.

Expected behavior The meta-IRQ should preempt the cooperative thread and after processing return to that cooperative thread.

Impact (Severe impact) In our case, we are collected data from a front-end in a time-critical manner (10ms interval) while also running Bluetooth host. Since BT_RX_WQ is a cooperative thread the meta-IRQ cannot be woken within the time frame especially when connection is being established.

Environment :

  • Zephyr 3.7.0

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 15 (9 by maintainers)

Commits related to this issue

Most upvoted comments

Would appreciate if you could do it, especially if any (unit) testing is required as I’m not familiar with the test framework.

@ithinuel, I replacing the check with the snippet and it doesn’t work as it assumes the current thread is a meta-irq thread? Replacing it with the following snippet does work as it checks the cached thread but I am wondering if this check is supposed to be here anyway. I believe the administration is done in update_cache()

((_kernel.cpus->current->base.prio >= 0) || thread_is_metairq(_kernel.ready_q.cache))

Looking at https://github.com/zephyrproject-rtos/zephyr/issues/61761, it doesn’t take in account meta-irq threads which according to the documentation (below) can preempt all threads even if they have taken a scheduler lock.

These are scheduled according to their normal priority, but also have the special ability to preempt all other threads (and other meta-IRQ threads) at lower priorities, even if those threads are cooperative and/or have taken a scheduler lock.