preempt-locking.rst 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. ===========================================================================
  2. Proper Locking Under a Preemptible Kernel: Keeping Kernel Code Preempt-Safe
  3. ===========================================================================
  4. :Author: Robert Love <rml@tech9.net>
  5. Introduction
  6. ============
  7. A preemptible kernel creates new locking issues. The issues are the same as
  8. those under SMP: concurrency and reentrancy. Thankfully, the Linux preemptible
  9. kernel model leverages existing SMP locking mechanisms. Thus, the kernel
  10. requires explicit additional locking for very few additional situations.
  11. This document is for all kernel hackers. Developing code in the kernel
  12. requires protecting these situations.
  13. RULE #1: Per-CPU data structures need explicit protection
  14. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  15. Two similar problems arise. An example code snippet::
  16. struct this_needs_locking tux[NR_CPUS];
  17. tux[smp_processor_id()] = some_value;
  18. /* task is preempted here... */
  19. something = tux[smp_processor_id()];
  20. First, since the data is per-CPU, it may not have explicit SMP locking, but
  21. require it otherwise. Second, when a preempted task is finally rescheduled,
  22. the previous value of smp_processor_id may not equal the current. You must
  23. protect these situations by disabling preemption around them.
  24. You can also use put_cpu() and get_cpu(), which will disable preemption.
  25. RULE #2: CPU state must be protected.
  26. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  27. Under preemption, the state of the CPU must be protected. This is arch-
  28. dependent, but includes CPU structures and state not preserved over a context
  29. switch. For example, on x86, entering and exiting FPU mode is now a critical
  30. section that must occur while preemption is disabled. Think what would happen
  31. if the kernel is executing a floating-point instruction and is then preempted.
  32. Remember, the kernel does not save FPU state except for user tasks. Therefore,
  33. upon preemption, the FPU registers will be sold to the lowest bidder. Thus,
  34. preemption must be disabled around such regions.
  35. Note, some FPU functions are already explicitly preempt safe. For example,
  36. kernel_fpu_begin and kernel_fpu_end will disable and enable preemption.
  37. RULE #3: Lock acquire and release must be performed by same task
  38. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  39. A lock acquired in one task must be released by the same task. This
  40. means you can't do oddball things like acquire a lock and go off to
  41. play while another task releases it. If you want to do something
  42. like this, acquire and release the task in the same code path and
  43. have the caller wait on an event by the other task.
  44. Solution
  45. ========
  46. Data protection under preemption is achieved by disabling preemption for the
  47. duration of the critical region.
  48. ::
  49. preempt_enable() decrement the preempt counter
  50. preempt_disable() increment the preempt counter
  51. preempt_enable_no_resched() decrement, but do not immediately preempt
  52. preempt_check_resched() if needed, reschedule
  53. preempt_count() return the preempt counter
  54. The functions are nestable. In other words, you can call preempt_disable
  55. n-times in a code path, and preemption will not be reenabled until the n-th
  56. call to preempt_enable. The preempt statements define to nothing if
  57. preemption is not enabled.
  58. Note that you do not need to explicitly prevent preemption if you are holding
  59. any locks or interrupts are disabled, since preemption is implicitly disabled
  60. in those cases.
  61. But keep in mind that 'irqs disabled' is a fundamentally unsafe way of
  62. disabling preemption - any cond_resched() or cond_resched_lock() might trigger
  63. a reschedule if the preempt count is 0. A simple printk() might trigger a
  64. reschedule. So use this implicit preemption-disabling property only if you
  65. know that the affected codepath does not do any of this. Best policy is to use
  66. this only for small, atomic code that you wrote and which calls no complex
  67. functions.
  68. Example::
  69. cpucache_t *cc; /* this is per-CPU */
  70. preempt_disable();
  71. cc = cc_data(searchp);
  72. if (cc && cc->avail) {
  73. __free_block(searchp, cc_entry(cc), cc->avail);
  74. cc->avail = 0;
  75. }
  76. preempt_enable();
  77. return 0;
  78. Notice how the preemption statements must encompass every reference of the
  79. critical variables. Another example::
  80. int buf[NR_CPUS];
  81. set_cpu_val(buf);
  82. if (buf[smp_processor_id()] == -1) printf(KERN_INFO "wee!\n");
  83. spin_lock(&buf_lock);
  84. /* ... */
  85. This code is not preempt-safe, but see how easily we can fix it by simply
  86. moving the spin_lock up two lines.
  87. Preventing preemption using interrupt disabling
  88. ===============================================
  89. It is possible to prevent a preemption event using local_irq_disable and
  90. local_irq_save. Note, when doing so, you must be very careful to not cause
  91. an event that would set need_resched and result in a preemption check. When
  92. in doubt, rely on locking or explicit preemption disabling.
  93. Note in 2.5 interrupt disabling is now only per-CPU (e.g. local).
  94. An additional concern is proper usage of local_irq_disable and local_irq_save.
  95. These may be used to protect from preemption, however, on exit, if preemption
  96. may be enabled, a test to see if preemption is required should be done. If
  97. these are called from the spin_lock and read/write lock macros, the right thing
  98. is done. They may also be called within a spin-lock protected region, however,
  99. if they are ever called outside of this context, a test for preemption should
  100. be made. Do note that calls from interrupt context or bottom half/ tasklets
  101. are also protected by preemption locks and so may use the versions which do
  102. not check preemption.