checklist.txt 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. Review Checklist for RCU Patches
  2. This document contains a checklist for producing and reviewing patches
  3. that make use of RCU. Violating any of the rules listed below will
  4. result in the same sorts of problems that leaving out a locking primitive
  5. would cause. This list is based on experiences reviewing such patches
  6. over a rather long period of time, but improvements are always welcome!
  7. 0. Is RCU being applied to a read-mostly situation? If the data
  8. structure is updated more than about 10% of the time, then
  9. you should strongly consider some other approach, unless
  10. detailed performance measurements show that RCU is nonetheless
  11. the right tool for the job.
  12. The other exception would be where performance is not an issue,
  13. and RCU provides a simpler implementation. An example of this
  14. situation is the dynamic NMI code in the Linux 2.6 kernel,
  15. at least on architectures where NMIs are rare.
  16. 1. Does the update code have proper mutual exclusion?
  17. RCU does allow -readers- to run (almost) naked, but -writers- must
  18. still use some sort of mutual exclusion, such as:
  19. a. locking,
  20. b. atomic operations, or
  21. c. restricting updates to a single task.
  22. If you choose #b, be prepared to describe how you have handled
  23. memory barriers on weakly ordered machines (pretty much all of
  24. them -- even x86 allows reads to be reordered), and be prepared
  25. to explain why this added complexity is worthwhile. If you
  26. choose #c, be prepared to explain how this single task does not
  27. become a major bottleneck on big multiprocessor machines (for
  28. example, if the task is updating information relating to itself
  29. that other tasks can read, there by definition can be no
  30. bottleneck).
  31. 2. Do the RCU read-side critical sections make proper use of
  32. rcu_read_lock() and friends? These primitives are needed
  33. to suppress preemption (or bottom halves, in the case of
  34. rcu_read_lock_bh()) in the read-side critical sections,
  35. and are also an excellent aid to readability.
  36. 3. Does the update code tolerate concurrent accesses?
  37. The whole point of RCU is to permit readers to run without
  38. any locks or atomic operations. This means that readers will
  39. be running while updates are in progress. There are a number
  40. of ways to handle this concurrency, depending on the situation:
  41. a. Make updates appear atomic to readers. For example,
  42. pointer updates to properly aligned fields will appear
  43. atomic, as will individual atomic primitives. Operations
  44. performed under a lock and sequences of multiple atomic
  45. primitives will -not- appear to be atomic.
  46. This is almost always the best approach.
  47. b. Carefully order the updates and the reads so that
  48. readers see valid data at all phases of the update.
  49. This is often more difficult than it sounds, especially
  50. given modern CPUs' tendency to reorder memory references.
  51. One must usually liberally sprinkle memory barriers
  52. (smp_wmb(), smp_rmb(), smp_mb()) through the code,
  53. making it difficult to understand and to test.
  54. It is usually better to group the changing data into
  55. a separate structure, so that the change may be made
  56. to appear atomic by updating a pointer to reference
  57. a new structure containing updated values.
  58. 4. Weakly ordered CPUs pose special challenges. Almost all CPUs
  59. are weakly ordered -- even i386 CPUs allow reads to be reordered.
  60. RCU code must take all of the following measures to prevent
  61. memory-corruption problems:
  62. a. Readers must maintain proper ordering of their memory
  63. accesses. The rcu_dereference() primitive ensures that
  64. the CPU picks up the pointer before it picks up the data
  65. that the pointer points to. This really is necessary
  66. on Alpha CPUs. If you don't believe me, see:
  67. http://www.openvms.compaq.com/wizard/wiz_2637.html
  68. The rcu_dereference() primitive is also an excellent
  69. documentation aid, letting the person reading the code
  70. know exactly which pointers are protected by RCU.
  71. The rcu_dereference() primitive is used by the various
  72. "_rcu()" list-traversal primitives, such as the
  73. list_for_each_entry_rcu().
  74. b. If the list macros are being used, the list_add_tail_rcu()
  75. and list_add_rcu() primitives must be used in order
  76. to prevent weakly ordered machines from misordering
  77. structure initialization and pointer planting.
  78. Similarly, if the hlist macros are being used, the
  79. hlist_add_head_rcu() primitive is required.
  80. c. If the list macros are being used, the list_del_rcu()
  81. primitive must be used to keep list_del()'s pointer
  82. poisoning from inflicting toxic effects on concurrent
  83. readers. Similarly, if the hlist macros are being used,
  84. the hlist_del_rcu() primitive is required.
  85. The list_replace_rcu() primitive may be used to
  86. replace an old structure with a new one in an
  87. RCU-protected list.
  88. d. Updates must ensure that initialization of a given
  89. structure happens before pointers to that structure are
  90. publicized. Use the rcu_assign_pointer() primitive
  91. when publicizing a pointer to a structure that can
  92. be traversed by an RCU read-side critical section.
  93. 5. If call_rcu(), or a related primitive such as call_rcu_bh(),
  94. is used, the callback function must be written to be called
  95. from softirq context. In particular, it cannot block.
  96. 6. Since synchronize_rcu() can block, it cannot be called from
  97. any sort of irq context.
  98. 7. If the updater uses call_rcu(), then the corresponding readers
  99. must use rcu_read_lock() and rcu_read_unlock(). If the updater
  100. uses call_rcu_bh(), then the corresponding readers must use
  101. rcu_read_lock_bh() and rcu_read_unlock_bh(). Mixing things up
  102. will result in confusion and broken kernels.
  103. One exception to this rule: rcu_read_lock() and rcu_read_unlock()
  104. may be substituted for rcu_read_lock_bh() and rcu_read_unlock_bh()
  105. in cases where local bottom halves are already known to be
  106. disabled, for example, in irq or softirq context. Commenting
  107. such cases is a must, of course! And the jury is still out on
  108. whether the increased speed is worth it.
  109. 8. Although synchronize_rcu() is a bit slower than is call_rcu(),
  110. it usually results in simpler code. So, unless update performance
  111. is important or the updaters cannot block, synchronize_rcu()
  112. should be used in preference to call_rcu().
  113. 9. All RCU list-traversal primitives, which include
  114. list_for_each_rcu(), list_for_each_entry_rcu(),
  115. list_for_each_continue_rcu(), and list_for_each_safe_rcu(),
  116. must be within an RCU read-side critical section. RCU
  117. read-side critical sections are delimited by rcu_read_lock()
  118. and rcu_read_unlock(), or by similar primitives such as
  119. rcu_read_lock_bh() and rcu_read_unlock_bh().
  120. Use of the _rcu() list-traversal primitives outside of an
  121. RCU read-side critical section causes no harm other than
  122. a slight performance degradation on Alpha CPUs and some
  123. confusion on the part of people trying to read the code.
  124. Another way of thinking of this is "If you are holding the
  125. lock that prevents the data structure from changing, why do
  126. you also need RCU-based protection?" That said, there may
  127. well be situations where use of the _rcu() list-traversal
  128. primitives while the update-side lock is held results in
  129. simpler and more maintainable code. The jury is still out
  130. on this question.
  131. 10. Conversely, if you are in an RCU read-side critical section,
  132. you -must- use the "_rcu()" variants of the list macros.
  133. Failing to do so will break Alpha and confuse people reading
  134. your code.
  135. 11. Note that synchronize_rcu() -only- guarantees to wait until
  136. all currently executing rcu_read_lock()-protected RCU read-side
  137. critical sections complete. It does -not- necessarily guarantee
  138. that all currently running interrupts, NMIs, preempt_disable()
  139. code, or idle loops will complete. Therefore, if you do not have
  140. rcu_read_lock()-protected read-side critical sections, do -not-
  141. use synchronize_rcu().
  142. If you want to wait for some of these other things, you might
  143. instead need to use synchronize_irq() or synchronize_sched().