blob: 51525a30e8b4015d9c1ca8c3866b274a6a5874f3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001Review Checklist for RCU Patches
2
3
4This document contains a checklist for producing and reviewing patches
5that make use of RCU. Violating any of the rules listed below will
6result in the same sorts of problems that leaving out a locking primitive
7would cause. This list is based on experiences reviewing such patches
8over a rather long period of time, but improvements are always welcome!
9
100. Is RCU being applied to a read-mostly situation? If the data
11 structure is updated more than about 10% of the time, then
12 you should strongly consider some other approach, unless
13 detailed performance measurements show that RCU is nonetheless
Paul E. McKenney240ebbf2009-06-25 09:08:18 -070014 the right tool for the job. Yes, you might think of RCU
15 as simply cutting overhead off of the readers and imposing it
16 on the writers. That is exactly why normal uses of RCU will
17 do much more reading than updating.
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
Paul E. McKenney32300752008-05-12 21:21:05 +020019 Another exception is where performance is not an issue, and RCU
20 provides a simpler implementation. An example of this situation
21 is the dynamic NMI code in the Linux 2.6 kernel, at least on
22 architectures where NMIs are rare.
23
24 Yet another exception is where the low real-time latency of RCU's
25 read-side primitives is critically important.
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
271. Does the update code have proper mutual exclusion?
28
29 RCU does allow -readers- to run (almost) naked, but -writers- must
30 still use some sort of mutual exclusion, such as:
31
32 a. locking,
33 b. atomic operations, or
34 c. restricting updates to a single task.
35
36 If you choose #b, be prepared to describe how you have handled
37 memory barriers on weakly ordered machines (pretty much all of
38 them -- even x86 allows reads to be reordered), and be prepared
39 to explain why this added complexity is worthwhile. If you
40 choose #c, be prepared to explain how this single task does not
Paul E. McKenneya83f1fe2005-05-01 08:59:05 -070041 become a major bottleneck on big multiprocessor machines (for
42 example, if the task is updating information relating to itself
43 that other tasks can read, there by definition can be no
44 bottleneck).
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
462. Do the RCU read-side critical sections make proper use of
47 rcu_read_lock() and friends? These primitives are needed
Paul E. McKenney32300752008-05-12 21:21:05 +020048 to prevent grace periods from ending prematurely, which
49 could result in data being unceremoniously freed out from
50 under your read-side code, which can greatly increase the
51 actuarial risk of your kernel.
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Paul E. McKenneydd81eca2005-09-10 00:26:24 -070053 As a rough rule of thumb, any dereference of an RCU-protected
54 pointer must be covered by rcu_read_lock() or rcu_read_lock_bh()
55 or by the appropriate update-side lock.
56
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573. Does the update code tolerate concurrent accesses?
58
59 The whole point of RCU is to permit readers to run without
60 any locks or atomic operations. This means that readers will
61 be running while updates are in progress. There are a number
62 of ways to handle this concurrency, depending on the situation:
63
Paul E. McKenney32300752008-05-12 21:21:05 +020064 a. Use the RCU variants of the list and hlist update
65 primitives to add, remove, and replace elements on an
66 RCU-protected list. Alternatively, use the RCU-protected
67 trees that have been added to the Linux kernel.
68
69 This is almost always the best approach.
70
71 b. Proceed as in (a) above, but also maintain per-element
72 locks (that are acquired by both readers and writers)
73 that guard per-element state. Of course, fields that
74 the readers refrain from accessing can be guarded by the
75 update-side lock.
76
77 This works quite well, also.
78
79 c. Make updates appear atomic to readers. For example,
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 pointer updates to properly aligned fields will appear
81 atomic, as will individual atomic primitives. Operations
82 performed under a lock and sequences of multiple atomic
83 primitives will -not- appear to be atomic.
84
Paul E. McKenney32300752008-05-12 21:21:05 +020085 This can work, but is starting to get a bit tricky.
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Paul E. McKenney32300752008-05-12 21:21:05 +020087 d. Carefully order the updates and the reads so that
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 readers see valid data at all phases of the update.
89 This is often more difficult than it sounds, especially
90 given modern CPUs' tendency to reorder memory references.
91 One must usually liberally sprinkle memory barriers
92 (smp_wmb(), smp_rmb(), smp_mb()) through the code,
93 making it difficult to understand and to test.
94
95 It is usually better to group the changing data into
96 a separate structure, so that the change may be made
97 to appear atomic by updating a pointer to reference
98 a new structure containing updated values.
99
1004. Weakly ordered CPUs pose special challenges. Almost all CPUs
101 are weakly ordered -- even i386 CPUs allow reads to be reordered.
102 RCU code must take all of the following measures to prevent
103 memory-corruption problems:
104
105 a. Readers must maintain proper ordering of their memory
106 accesses. The rcu_dereference() primitive ensures that
107 the CPU picks up the pointer before it picks up the data
108 that the pointer points to. This really is necessary
109 on Alpha CPUs. If you don't believe me, see:
110
111 http://www.openvms.compaq.com/wizard/wiz_2637.html
112
113 The rcu_dereference() primitive is also an excellent
114 documentation aid, letting the person reading the code
115 know exactly which pointers are protected by RCU.
116
117 The rcu_dereference() primitive is used by the various
118 "_rcu()" list-traversal primitives, such as the
Paul E. McKenneydd81eca2005-09-10 00:26:24 -0700119 list_for_each_entry_rcu(). Note that it is perfectly
120 legal (if redundant) for update-side code to use
121 rcu_dereference() and the "_rcu()" list-traversal
122 primitives. This is particularly useful in code
123 that is common to readers and updaters.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Paul E. McKenneya83f1fe2005-05-01 08:59:05 -0700125 b. If the list macros are being used, the list_add_tail_rcu()
126 and list_add_rcu() primitives must be used in order
127 to prevent weakly ordered machines from misordering
128 structure initialization and pointer planting.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 Similarly, if the hlist macros are being used, the
Paul E. McKenneya83f1fe2005-05-01 08:59:05 -0700130 hlist_add_head_rcu() primitive is required.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Paul E. McKenneya83f1fe2005-05-01 08:59:05 -0700132 c. If the list macros are being used, the list_del_rcu()
133 primitive must be used to keep list_del()'s pointer
134 poisoning from inflicting toxic effects on concurrent
135 readers. Similarly, if the hlist macros are being used,
136 the hlist_del_rcu() primitive is required.
137
138 The list_replace_rcu() primitive may be used to
139 replace an old structure with a new one in an
140 RCU-protected list.
141
142 d. Updates must ensure that initialization of a given
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 structure happens before pointers to that structure are
144 publicized. Use the rcu_assign_pointer() primitive
145 when publicizing a pointer to a structure that can
146 be traversed by an RCU read-side critical section.
147
Paul E. McKenney32300752008-05-12 21:21:05 +02001485. If call_rcu(), or a related primitive such as call_rcu_bh() or
149 call_rcu_sched(), is used, the callback function must be
150 written to be called from softirq context. In particular,
151 it cannot block.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Paul E. McKenneya83f1fe2005-05-01 08:59:05 -07001536. Since synchronize_rcu() can block, it cannot be called from
Paul E. McKenney32300752008-05-12 21:21:05 +0200154 any sort of irq context. Ditto for synchronize_sched() and
155 synchronize_srcu().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
1577. If the updater uses call_rcu(), then the corresponding readers
158 must use rcu_read_lock() and rcu_read_unlock(). If the updater
159 uses call_rcu_bh(), then the corresponding readers must use
Paul E. McKenney32300752008-05-12 21:21:05 +0200160 rcu_read_lock_bh() and rcu_read_unlock_bh(). If the updater
161 uses call_rcu_sched(), then the corresponding readers must
162 disable preemption. Mixing things up will result in confusion
163 and broken kernels.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
165 One exception to this rule: rcu_read_lock() and rcu_read_unlock()
166 may be substituted for rcu_read_lock_bh() and rcu_read_unlock_bh()
167 in cases where local bottom halves are already known to be
168 disabled, for example, in irq or softirq context. Commenting
169 such cases is a must, of course! And the jury is still out on
170 whether the increased speed is worth it.
171
Paul E. McKenney32300752008-05-12 21:21:05 +02001728. Although synchronize_rcu() is slower than is call_rcu(), it
173 usually results in simpler code. So, unless update performance
174 is critically important or the updaters cannot block,
Paul E. McKenney165d6c72006-06-25 05:48:44 -0700175 synchronize_rcu() should be used in preference to call_rcu().
176
177 An especially important property of the synchronize_rcu()
178 primitive is that it automatically self-limits: if grace periods
179 are delayed for whatever reason, then the synchronize_rcu()
180 primitive will correspondingly delay updates. In contrast,
181 code using call_rcu() should explicitly limit update rate in
182 cases where grace periods are delayed, as failing to do so can
183 result in excessive realtime latencies or even OOM conditions.
184
185 Ways of gaining this self-limiting property when using call_rcu()
186 include:
187
188 a. Keeping a count of the number of data-structure elements
189 used by the RCU-protected data structure, including those
190 waiting for a grace period to elapse. Enforce a limit
191 on this number, stalling updates as needed to allow
192 previously deferred frees to complete.
193
194 Alternatively, limit only the number awaiting deferred
195 free rather than the total number of elements.
196
197 b. Limiting update rate. For example, if updates occur only
198 once per hour, then no explicit rate limiting is required,
199 unless your system is already badly broken. The dcache
200 subsystem takes this approach -- updates are guarded
201 by a global lock, limiting their rate.
202
203 c. Trusted update -- if updates can only be done manually by
204 superuser or some other trusted user, then it might not
205 be necessary to automatically limit them. The theory
206 here is that superuser already has lots of ways to crash
207 the machine.
208
209 d. Use call_rcu_bh() rather than call_rcu(), in order to take
210 advantage of call_rcu_bh()'s faster grace periods.
211
212 e. Periodically invoke synchronize_rcu(), permitting a limited
213 number of updates per grace period.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
2159. All RCU list-traversal primitives, which include
Paul E. McKenney34d7c2b2008-08-01 14:11:05 -0700216 rcu_dereference(), list_for_each_entry_rcu(),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 list_for_each_continue_rcu(), and list_for_each_safe_rcu(),
Paul E. McKenney32300752008-05-12 21:21:05 +0200218 must be either within an RCU read-side critical section or
219 must be protected by appropriate update-side locks. RCU
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 read-side critical sections are delimited by rcu_read_lock()
221 and rcu_read_unlock(), or by similar primitives such as
222 rcu_read_lock_bh() and rcu_read_unlock_bh().
223
Paul E. McKenney32300752008-05-12 21:21:05 +0200224 The reason that it is permissible to use RCU list-traversal
225 primitives when the update-side lock is held is that doing so
226 can be quite helpful in reducing code bloat when common code is
227 shared between readers and updaters.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
22910. Conversely, if you are in an RCU read-side critical section,
Paul E. McKenney32300752008-05-12 21:21:05 +0200230 and you don't hold the appropriate update-side lock, you -must-
231 use the "_rcu()" variants of the list macros. Failing to do so
232 will break Alpha and confuse people reading your code.
Paul E. McKenneya83f1fe2005-05-01 08:59:05 -0700233
23411. Note that synchronize_rcu() -only- guarantees to wait until
235 all currently executing rcu_read_lock()-protected RCU read-side
236 critical sections complete. It does -not- necessarily guarantee
237 that all currently running interrupts, NMIs, preempt_disable()
238 code, or idle loops will complete. Therefore, if you do not have
239 rcu_read_lock()-protected read-side critical sections, do -not-
240 use synchronize_rcu().
241
242 If you want to wait for some of these other things, you might
243 instead need to use synchronize_irq() or synchronize_sched().
Paul E. McKenneyd19720a2006-02-01 03:06:42 -0800244
24512. Any lock acquired by an RCU callback must be acquired elsewhere
Paul E. McKenney240ebbf2009-06-25 09:08:18 -0700246 with softirq disabled, e.g., via spin_lock_irqsave(),
247 spin_lock_bh(), etc. Failing to disable irq on a given
248 acquisition of that lock will result in deadlock as soon as the
249 RCU callback happens to interrupt that acquisition's critical
250 section.
Paul E. McKenney621934e2006-10-04 02:17:02 -0700251
Paul E. McKenneyef48bd22007-07-15 23:41:03 -070025213. RCU callbacks can be and are executed in parallel. In many cases,
253 the callback code simply wrappers around kfree(), so that this
254 is not an issue (or, more accurately, to the extent that it is
255 an issue, the memory-allocator locking handles it). However,
256 if the callbacks do manipulate a shared data structure, they
257 must use whatever locking or other synchronization is required
258 to safely access and/or modify that data structure.
259
Paul E. McKenney32300752008-05-12 21:21:05 +0200260 RCU callbacks are -usually- executed on the same CPU that executed
261 the corresponding call_rcu(), call_rcu_bh(), or call_rcu_sched(),
262 but are by -no- means guaranteed to be. For example, if a given
263 CPU goes offline while having an RCU callback pending, then that
264 RCU callback will execute on some surviving CPU. (If this was
265 not the case, a self-spawning RCU callback would prevent the
266 victim CPU from ever going offline.)
267
Paul E. McKenneyef48bd22007-07-15 23:41:03 -070026814. SRCU (srcu_read_lock(), srcu_read_unlock(), and synchronize_srcu())
Paul E. McKenney621934e2006-10-04 02:17:02 -0700269 may only be invoked from process context. Unlike other forms of
270 RCU, it -is- permissible to block in an SRCU read-side critical
271 section (demarked by srcu_read_lock() and srcu_read_unlock()),
272 hence the "SRCU": "sleepable RCU". Please note that if you
273 don't need to sleep in read-side critical sections, you should
274 be using RCU rather than SRCU, because RCU is almost always
275 faster and easier to use than is SRCU.
276
277 Also unlike other forms of RCU, explicit initialization
278 and cleanup is required via init_srcu_struct() and
279 cleanup_srcu_struct(). These are passed a "struct srcu_struct"
280 that defines the scope of a given SRCU domain. Once initialized,
281 the srcu_struct is passed to srcu_read_lock(), srcu_read_unlock()
282 and synchronize_srcu(). A given synchronize_srcu() waits only
283 for SRCU read-side critical sections governed by srcu_read_lock()
284 and srcu_read_unlock() calls that have been passd the same
285 srcu_struct. This property is what makes sleeping read-side
286 critical sections tolerable -- a given subsystem delays only
287 its own updates, not those of other subsystems using SRCU.
288 Therefore, SRCU is less prone to OOM the system than RCU would
289 be if RCU's read-side critical sections were permitted to
290 sleep.
291
292 The ability to sleep in read-side critical sections does not
293 come for free. First, corresponding srcu_read_lock() and
294 srcu_read_unlock() calls must be passed the same srcu_struct.
295 Second, grace-period-detection overhead is amortized only
296 over those updates sharing a given srcu_struct, rather than
297 being globally amortized as they are for other forms of RCU.
298 Therefore, SRCU should be used in preference to rw_semaphore
299 only in extremely read-intensive situations, or in situations
300 requiring SRCU's read-side deadlock immunity or low read-side
301 realtime latency.
302
303 Note that, rcu_assign_pointer() and rcu_dereference() relate to
304 SRCU just as they do to other forms of RCU.
Paul E. McKenney0612ea02009-03-10 12:55:57 -0700305
30615. The whole point of call_rcu(), synchronize_rcu(), and friends
307 is to wait until all pre-existing readers have finished before
308 carrying out some otherwise-destructive operation. It is
309 therefore critically important to -first- remove any path
310 that readers can follow that could be affected by the
311 destructive operation, and -only- -then- invoke call_rcu(),
312 synchronize_rcu(), or friends.
313
314 Because these primitives only wait for pre-existing readers,
315 it is the caller's responsibility to guarantee safety to
316 any subsequent readers.
Paul E. McKenney240ebbf2009-06-25 09:08:18 -0700317
31816. The various RCU read-side primitives do -not- contain memory
319 barriers. The CPU (and in some cases, the compiler) is free
320 to reorder code into and out of RCU read-side critical sections.
321 It is the responsibility of the RCU update-side primitives to
322 deal with this.