0001 =================================================================
0002 CPU Scheduler implementation hints for architecture specific code
0003 =================================================================
0004
0005 Nick Piggin, 2005
0006
0007 Context switch
0008 ==============
0009 1. Runqueue locking
0010 By default, the switch_to arch function is called with the runqueue
0011 locked. This is usually not a problem unless switch_to may need to
0012 take the runqueue lock. This is usually due to a wake up operation in
0013 the context switch. See arch/ia64/include/asm/switch_to.h for an example.
0014
0015 To request the scheduler call switch_to with the runqueue unlocked,
0016 you must `#define __ARCH_WANT_UNLOCKED_CTXSW` in a header file
0017 (typically the one where switch_to is defined).
0018
0019 Unlocked context switches introduce only a very minor performance
0020 penalty to the core scheduler implementation in the CONFIG_SMP case.
0021
0022 CPU idle
0023 ========
0024 Your cpu_idle routines need to obey the following rules:
0025
0026 1. Preempt should now disabled over idle routines. Should only
0027 be enabled to call schedule() then disabled again.
0028
0029 2. need_resched/TIF_NEED_RESCHED is only ever set, and will never
0030 be cleared until the running task has called schedule(). Idle
0031 threads need only ever query need_resched, and may never set or
0032 clear it.
0033
0034 3. When cpu_idle finds (need_resched() == 'true'), it should call
0035 schedule(). It should not call schedule() otherwise.
0036
0037 4. The only time interrupts need to be disabled when checking
0038 need_resched is if we are about to sleep the processor until
0039 the next interrupt (this doesn't provide any protection of
0040 need_resched, it prevents losing an interrupt):
0041
0042 4a. Common problem with this type of sleep appears to be::
0043
0044 local_irq_disable();
0045 if (!need_resched()) {
0046 local_irq_enable();
0047 *** resched interrupt arrives here ***
0048 __asm__("sleep until next interrupt");
0049 }
0050
0051 5. TIF_POLLING_NRFLAG can be set by idle routines that do not
0052 need an interrupt to wake them up when need_resched goes high.
0053 In other words, they must be periodically polling need_resched,
0054 although it may be reasonable to do some background work or enter
0055 a low CPU priority.
0056
0057 - 5a. If TIF_POLLING_NRFLAG is set, and we do decide to enter
0058 an interrupt sleep, it needs to be cleared then a memory
0059 barrier issued (followed by a test of need_resched with
0060 interrupts disabled, as explained in 3).
0061
0062 arch/x86/kernel/process.c has examples of both polling and
0063 sleeping idle functions.
0064
0065
0066 Possible arch/ problems
0067 =======================
0068
0069 Possible arch problems I found (and either tried to fix or didn't):
0070
0071 ia64 - is safe_halt call racy vs interrupts? (does it sleep?) (See #4a)
0072
0073 sh64 - Is sleeping racy vs interrupts? (See #4a)
0074
0075 sparc - IRQs on at this point(?), change local_irq_save to _disable.
0076 - TODO: needs secondary CPUs to disable preempt (See #1)