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