1 CPU Scheduler implementation hints for architecture specific code 2 3 Nick Piggin, 2005 4 5Context switch 6============== 71. Runqueue locking 8By default, the switch_to arch function is called with the runqueue 9locked. This is usually not a problem unless switch_to may need to 10take the runqueue lock. This is usually due to a wake up operation in 11the context switch. See arch/ia64/include/asm/switch_to.h for an example. 12 13To request the scheduler call switch_to with the runqueue unlocked, 14you must `#define __ARCH_WANT_UNLOCKED_CTXSW` in a header file 15(typically the one where switch_to is defined). 16 17Unlocked context switches introduce only a very minor performance 18penalty to the core scheduler implementation in the CONFIG_SMP case. 19 20CPU idle 21======== 22Your cpu_idle routines need to obey the following rules: 23 241. Preempt should now disabled over idle routines. Should only 25 be enabled to call schedule() then disabled again. 26 272. need_resched/TIF_NEED_RESCHED is only ever set, and will never 28 be cleared until the running task has called schedule(). Idle 29 threads need only ever query need_resched, and may never set or 30 clear it. 31 323. When cpu_idle finds (need_resched() == 'true'), it should call 33 schedule(). It should not call schedule() otherwise. 34 354. The only time interrupts need to be disabled when checking 36 need_resched is if we are about to sleep the processor until 37 the next interrupt (this doesn't provide any protection of 38 need_resched, it prevents losing an interrupt). 39 40 4a. Common problem with this type of sleep appears to be: 41 local_irq_disable(); 42 if (!need_resched()) { 43 local_irq_enable(); 44 *** resched interrupt arrives here *** 45 __asm__("sleep until next interrupt"); 46 } 47 485. TIF_POLLING_NRFLAG can be set by idle routines that do not 49 need an interrupt to wake them up when need_resched goes high. 50 In other words, they must be periodically polling need_resched, 51 although it may be reasonable to do some background work or enter 52 a low CPU priority. 53 54 5a. If TIF_POLLING_NRFLAG is set, and we do decide to enter 55 an interrupt sleep, it needs to be cleared then a memory 56 barrier issued (followed by a test of need_resched with 57 interrupts disabled, as explained in 3). 58 59arch/x86/kernel/process.c has examples of both polling and 60sleeping idle functions. 61 62 63Possible arch/ problems 64======================= 65 66Possible arch problems I found (and either tried to fix or didn't): 67 68ia64 - is safe_halt call racy vs interrupts? (does it sleep?) (See #4a) 69 70sh64 - Is sleeping racy vs interrupts? (See #4a) 71 72sparc - IRQs on at this point(?), change local_irq_save to _disable. 73 - TODO: needs secondary CPUs to disable preempt (See #1) 74 75