Documentation/scheduler/sched-arch.rst
Nick Piggin, 2005
To request the scheduler call switch_to with the runqueue unlocked,
you must #define __ARCH_WANT_UNLOCKED_CTXSW in a header file
(typically the one where switch_to is defined).
Unlocked context switches introduce only a very minor performance penalty to the core scheduler implementation in the CONFIG_SMP case.
Your cpu_idle routines need to obey the following rules:
Preempt should now disabled over idle routines. Should only be enabled to call schedule() then disabled again.
need_resched/TIF_NEED_RESCHED is only ever set, and will never be cleared until the running task has called schedule(). Idle threads need only ever query need_resched, and may never set or clear it.
When cpu_idle finds (need_resched() == 'true'), it should call schedule(). It should not call schedule() otherwise.
The only time interrupts need to be disabled when checking need_resched is if we are about to sleep the processor until the next interrupt (this doesn't provide any protection of need_resched, it prevents losing an interrupt):
4a. Common problem with this type of sleep appears to be::
local_irq_disable();
if (!need_resched()) {
local_irq_enable();
*** resched interrupt arrives here ***
__asm__("sleep until next interrupt");
}
TIF_POLLING_NRFLAG can be set by idle routines that do not need an interrupt to wake them up when need_resched goes high. In other words, they must be periodically polling need_resched, although it may be reasonable to do some background work or enter a low CPU priority.
arch/x86/kernel/process.c has examples of both polling and sleeping idle functions.
Possible arch problems I found (and either tried to fix or didn't):
ia64 - is safe_halt call racy vs interrupts? (does it sleep?) (See #4a)
sparc - IRQs on at this point(?), change local_irq_save to _disable. - TODO: needs secondary CPUs to disable preempt (See #1)