sched-arch.txt 3.3 KB

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