← Documents Documentation/RCU/rcu_dereference.rst GitHub 원문 ↗

Linux 6.18.37 · RCU

rcu_dereference() 반환값을 올바르게 다루는 법

Address/data dependency를 보존하는 pointer 연산, 비교, accessor 선택과 sparse __rcu 검사를 설명합니다.

Source pathDocumentation/RCU/rcu_dereference.rst
Source versionLinux v6.18.37
TranslationDUJINLABS 전문 번역 + 해설

요약·해설과 원문, 전문 번역을 서로 분리했습니다. API 이름, symbol, source path는 원문 표기를 사용합니다.

1. 요약·해설

원문의 핵심 논리와 kernel programming 관점의 보충 설명입니다. 아래의 전문 번역과는 별도로 작성했습니다.

요약·해설

rcu_dereference.rst:1-502

Address/data dependency를 보존하는 pointer 연산, 비교, accessor 선택과 sparse __rcu 검사를 설명합니다.

2. 영어 원문 전체

번역 기준이 된 Linux v6.18.37 원문입니다. 줄 번호는 이 버전의 파일 좌표입니다.

원문 전체 펼치기
1 .. _rcu_dereference_doc:
2
3 PROPER CARE AND FEEDING OF RETURN VALUES FROM rcu_dereference()
4 ===============================================================
5
6 Proper care and feeding of address and data dependencies is critically
7 important to correct use of things like RCU. To this end, the pointers
8 returned from the rcu_dereference() family of primitives carry address and
9 data dependencies. These dependencies extend from the rcu_dereference()
10 macro's load of the pointer to the later use of that pointer to compute
11 either the address of a later memory access (representing an address
12 dependency) or the value written by a later memory access (representing
13 a data dependency).
14
15 Most of the time, these dependencies are preserved, permitting you to
16 freely use values from rcu_dereference(). For example, dereferencing
17 (prefix "*"), field selection ("->"), assignment ("="), address-of
18 ("&"), casts, and addition or subtraction of constants all work quite
19 naturally and safely. However, because current compilers do not take
20 either address or data dependencies into account it is still possible
21 to get into trouble.
22
23 Follow these rules to preserve the address and data dependencies emanating
24 from your calls to rcu_dereference() and friends, thus keeping your RCU
25 readers working properly:
26
27 - You must use one of the rcu_dereference() family of primitives
28 to load an RCU-protected pointer, otherwise CONFIG_PROVE_RCU
29 will complain. Worse yet, your code can see random memory-corruption
30 bugs due to games that compilers and DEC Alpha can play.
31 Without one of the rcu_dereference() primitives, compilers
32 can reload the value, and won't your code have fun with two
33 different values for a single pointer! Without rcu_dereference(),
34 DEC Alpha can load a pointer, dereference that pointer, and
35 return data preceding initialization that preceded the store
36 of the pointer. (As noted later, in recent kernels READ_ONCE()
37 also prevents DEC Alpha from playing these tricks.)
38
39 In addition, the volatile cast in rcu_dereference() prevents the
40 compiler from deducing the resulting pointer value. Please see
41 the section entitled "EXAMPLE WHERE THE COMPILER KNOWS TOO MUCH"
42 for an example where the compiler can in fact deduce the exact
43 value of the pointer, and thus cause misordering.
44
45 - In the special case where data is added but is never removed
46 while readers are accessing the structure, READ_ONCE() may be used
47 instead of rcu_dereference(). In this case, use of READ_ONCE()
48 takes on the role of the lockless_dereference() primitive that
49 was removed in v4.15.
50
51 - You are only permitted to use rcu_dereference() on pointer values.
52 The compiler simply knows too much about integral values to
53 trust it to carry dependencies through integer operations.
54 There are a very few exceptions, namely that you can temporarily
55 cast the pointer to uintptr_t in order to:
56
57 - Set bits and clear bits down in the must-be-zero low-order
58 bits of that pointer. This clearly means that the pointer
59 must have alignment constraints, for example, this does
60 *not* work in general for char* pointers.
61
62 - XOR bits to translate pointers, as is done in some
63 classic buddy-allocator algorithms.
64
65 It is important to cast the value back to pointer before
66 doing much of anything else with it.
67
68 - Avoid cancellation when using the "+" and "-" infix arithmetic
69 operators. For example, for a given variable "x", avoid
70 "(x-(uintptr_t)x)" for char* pointers. The compiler is within its
71 rights to substitute zero for this sort of expression, so that
72 subsequent accesses no longer depend on the rcu_dereference(),
73 again possibly resulting in bugs due to misordering.
74
75 Of course, if "p" is a pointer from rcu_dereference(), and "a"
76 and "b" are integers that happen to be equal, the expression
77 "p+a-b" is safe because its value still necessarily depends on
78 the rcu_dereference(), thus maintaining proper ordering.
79
80 - If you are using RCU to protect JITed functions, so that the
81 "()" function-invocation operator is applied to a value obtained
82 (directly or indirectly) from rcu_dereference(), you may need to
83 interact directly with the hardware to flush instruction caches.
84 This issue arises on some systems when a newly JITed function is
85 using the same memory that was used by an earlier JITed function.
86
87 - Do not use the results from relational operators ("==", "!=",
88 ">", ">=", "<", or "<=") when dereferencing. For example,
89 the following (quite strange) code is buggy::
90
91 int *p;
92 int *q;
93
94 ...
95
96 p = rcu_dereference(gp)
97 q = &global_q;
98 q += p > &oom_p;
99 r1 = *q; /* BUGGY!!! */
100
101 As before, the reason this is buggy is that relational operators
102 are often compiled using branches. And as before, although
103 weak-memory machines such as ARM or PowerPC do order stores
104 after such branches, but can speculate loads, which can again
105 result in misordering bugs.
106
107 - Be very careful about comparing pointers obtained from
108 rcu_dereference() against non-NULL values. As Linus Torvalds
109 explained, if the two pointers are equal, the compiler could
110 substitute the pointer you are comparing against for the pointer
111 obtained from rcu_dereference(). For example::
112
113 p = rcu_dereference(gp);
114 if (p == &default_struct)
115 do_default(p->a);
116
117 Because the compiler now knows that the value of "p" is exactly
118 the address of the variable "default_struct", it is free to
119 transform this code into the following::
120
121 p = rcu_dereference(gp);
122 if (p == &default_struct)
123 do_default(default_struct.a);
124
125 On ARM and Power hardware, the load from "default_struct.a"
126 can now be speculated, such that it might happen before the
127 rcu_dereference(). This could result in bugs due to misordering.
128
129 However, comparisons are OK in the following cases:
130
131 - The comparison was against the NULL pointer. If the
132 compiler knows that the pointer is NULL, you had better
133 not be dereferencing it anyway. If the comparison is
134 non-equal, the compiler is none the wiser. Therefore,
135 it is safe to compare pointers from rcu_dereference()
136 against NULL pointers.
137
138 - The pointer is never dereferenced after being compared.
139 Since there are no subsequent dereferences, the compiler
140 cannot use anything it learned from the comparison
141 to reorder the non-existent subsequent dereferences.
142 This sort of comparison occurs frequently when scanning
143 RCU-protected circular linked lists.
144
145 Note that if the pointer comparison is done outside
146 of an RCU read-side critical section, and the pointer
147 is never dereferenced, rcu_access_pointer() should be
148 used in place of rcu_dereference(). In most cases,
149 it is best to avoid accidental dereferences by testing
150 the rcu_access_pointer() return value directly, without
151 assigning it to a variable.
152
153 Within an RCU read-side critical section, there is little
154 reason to use rcu_access_pointer().
155
156 - The comparison is against a pointer that references memory
157 that was initialized "a long time ago." The reason
158 this is safe is that even if misordering occurs, the
159 misordering will not affect the accesses that follow
160 the comparison. So exactly how long ago is "a long
161 time ago"? Here are some possibilities:
162
163 - Compile time.
164
165 - Boot time.
166
167 - Module-init time for module code.
168
169 - Prior to kthread creation for kthread code.
170
171 - During some prior acquisition of the lock that
172 we now hold.
173
174 - Before mod_timer() time for a timer handler.
175
176 There are many other possibilities involving the Linux
177 kernel's wide array of primitives that cause code to
178 be invoked at a later time.
179
180 - The pointer being compared against also came from
181 rcu_dereference(). In this case, both pointers depend
182 on one rcu_dereference() or another, so you get proper
183 ordering either way.
184
185 That said, this situation can make certain RCU usage
186 bugs more likely to happen. Which can be a good thing,
187 at least if they happen during testing. An example
188 of such an RCU usage bug is shown in the section titled
189 "EXAMPLE OF AMPLIFIED RCU-USAGE BUG".
190
191 - All of the accesses following the comparison are stores,
192 so that a control dependency preserves the needed ordering.
193 That said, it is easy to get control dependencies wrong.
194 Please see the "CONTROL DEPENDENCIES" section of
195 Documentation/memory-barriers.txt for more details.
196
197 - The pointers are not equal *and* the compiler does
198 not have enough information to deduce the value of the
199 pointer. Note that the volatile cast in rcu_dereference()
200 will normally prevent the compiler from knowing too much.
201
202 However, please note that if the compiler knows that the
203 pointer takes on only one of two values, a not-equal
204 comparison will provide exactly the information that the
205 compiler needs to deduce the value of the pointer.
206
207 - Disable any value-speculation optimizations that your compiler
208 might provide, especially if you are making use of feedback-based
209 optimizations that take data collected from prior runs. Such
210 value-speculation optimizations reorder operations by design.
211
212 There is one exception to this rule: Value-speculation
213 optimizations that leverage the branch-prediction hardware are
214 safe on strongly ordered systems (such as x86), but not on weakly
215 ordered systems (such as ARM or Power). Choose your compiler
216 command-line options wisely!
217
218
219 EXAMPLE OF AMPLIFIED RCU-USAGE BUG
220 ----------------------------------
221
222 Because updaters can run concurrently with RCU readers, RCU readers can
223 see stale and/or inconsistent values. If RCU readers need fresh or
224 consistent values, which they sometimes do, they need to take proper
225 precautions. To see this, consider the following code fragment::
226
227 struct foo {
228 int a;
229 int b;
230 int c;
231 };
232 struct foo *gp1;
233 struct foo *gp2;
234
235 void updater(void)
236 {
237 struct foo *p;
238
239 p = kmalloc(...);
240 if (p == NULL)
241 deal_with_it();
242 p->a = 42; /* Each field in its own cache line. */
243 p->b = 43;
244 p->c = 44;
245 rcu_assign_pointer(gp1, p);
246 p->b = 143;
247 p->c = 144;
248 rcu_assign_pointer(gp2, p);
249 }
250
251 void reader(void)
252 {
253 struct foo *p;
254 struct foo *q;
255 int r1, r2;
256
257 rcu_read_lock();
258 p = rcu_dereference(gp2);
259 if (p == NULL)
260 return;
261 r1 = p->b; /* Guaranteed to get 143. */
262 q = rcu_dereference(gp1); /* Guaranteed non-NULL. */
263 if (p == q) {
264 /* The compiler decides that q->c is same as p->c. */
265 r2 = p->c; /* Could get 44 on weakly order system. */
266 } else {
267 r2 = p->c - r1; /* Unconditional access to p->c. */
268 }
269 rcu_read_unlock();
270 do_something_with(r1, r2);
271 }
272
273 You might be surprised that the outcome (r1 == 143 && r2 == 44) is possible,
274 but you should not be. After all, the updater might have been invoked
275 a second time between the time reader() loaded into "r1" and the time
276 that it loaded into "r2". The fact that this same result can occur due
277 to some reordering from the compiler and CPUs is beside the point.
278
279 But suppose that the reader needs a consistent view?
280
281 Then one approach is to use locking, for example, as follows::
282
283 struct foo {
284 int a;
285 int b;
286 int c;
287 spinlock_t lock;
288 };
289 struct foo *gp1;
290 struct foo *gp2;
291
292 void updater(void)
293 {
294 struct foo *p;
295
296 p = kmalloc(...);
297 if (p == NULL)
298 deal_with_it();
299 spin_lock(&p->lock);
300 p->a = 42; /* Each field in its own cache line. */
301 p->b = 43;
302 p->c = 44;
303 spin_unlock(&p->lock);
304 rcu_assign_pointer(gp1, p);
305 spin_lock(&p->lock);
306 p->b = 143;
307 p->c = 144;
308 spin_unlock(&p->lock);
309 rcu_assign_pointer(gp2, p);
310 }
311
312 void reader(void)
313 {
314 struct foo *p;
315 struct foo *q;
316 int r1, r2;
317
318 rcu_read_lock();
319 p = rcu_dereference(gp2);
320 if (p == NULL)
321 return;
322 spin_lock(&p->lock);
323 r1 = p->b; /* Guaranteed to get 143. */
324 q = rcu_dereference(gp1); /* Guaranteed non-NULL. */
325 if (p == q) {
326 /* The compiler decides that q->c is same as p->c. */
327 r2 = p->c; /* Locking guarantees r2 == 144. */
328 } else {
329 spin_lock(&q->lock);
330 r2 = q->c - r1;
331 spin_unlock(&q->lock);
332 }
333 rcu_read_unlock();
334 spin_unlock(&p->lock);
335 do_something_with(r1, r2);
336 }
337
338 As always, use the right tool for the job!
339
340
341 EXAMPLE WHERE THE COMPILER KNOWS TOO MUCH
342 -----------------------------------------
343
344 If a pointer obtained from rcu_dereference() compares not-equal to some
345 other pointer, the compiler normally has no clue what the value of the
346 first pointer might be. This lack of knowledge prevents the compiler
347 from carrying out optimizations that otherwise might destroy the ordering
348 guarantees that RCU depends on. And the volatile cast in rcu_dereference()
349 should prevent the compiler from guessing the value.
350
351 But without rcu_dereference(), the compiler knows more than you might
352 expect. Consider the following code fragment::
353
354 struct foo {
355 int a;
356 int b;
357 };
358 static struct foo variable1;
359 static struct foo variable2;
360 static struct foo *gp = &variable1;
361
362 void updater(void)
363 {
364 initialize_foo(&variable2);
365 rcu_assign_pointer(gp, &variable2);
366 /*
367 * The above is the only store to gp in this translation unit,
368 * and the address of gp is not exported in any way.
369 */
370 }
371
372 int reader(void)
373 {
374 struct foo *p;
375
376 p = gp;
377 barrier();
378 if (p == &variable1)
379 return p->a; /* Must be variable1.a. */
380 else
381 return p->b; /* Must be variable2.b. */
382 }
383
384 Because the compiler can see all stores to "gp", it knows that the only
385 possible values of "gp" are "variable1" on the one hand and "variable2"
386 on the other. The comparison in reader() therefore tells the compiler
387 the exact value of "p" even in the not-equals case. This allows the
388 compiler to make the return values independent of the load from "gp",
389 in turn destroying the ordering between this load and the loads of the
390 return values. This can result in "p->b" returning pre-initialization
391 garbage values on weakly ordered systems.
392
393 In short, rcu_dereference() is *not* optional when you are going to
394 dereference the resulting pointer.
395
396
397 WHICH MEMBER OF THE rcu_dereference() FAMILY SHOULD YOU USE?
398 ------------------------------------------------------------
399
400 First, please avoid using rcu_dereference_raw() and also please avoid
401 using rcu_dereference_check() and rcu_dereference_protected() with a
402 second argument with a constant value of 1 (or true, for that matter).
403 With that caution out of the way, here is some guidance for which
404 member of the rcu_dereference() to use in various situations:
405
406 1. If the access needs to be within an RCU read-side critical
407 section, use rcu_dereference(). With the new consolidated
408 RCU flavors, an RCU read-side critical section is entered
409 using rcu_read_lock(), anything that disables bottom halves,
410 anything that disables interrupts, or anything that disables
411 preemption. Please note that spinlock critical sections
412 are also implied RCU read-side critical sections, even when
413 they are preemptible, as they are in kernels built with
414 CONFIG_PREEMPT_RT=y.
415
416 2. If the access might be within an RCU read-side critical section
417 on the one hand, or protected by (say) my_lock on the other,
418 use rcu_dereference_check(), for example::
419
420 p1 = rcu_dereference_check(p->rcu_protected_pointer,
421 lockdep_is_held(&my_lock));
422
423
424 3. If the access might be within an RCU read-side critical section
425 on the one hand, or protected by either my_lock or your_lock on
426 the other, again use rcu_dereference_check(), for example::
427
428 p1 = rcu_dereference_check(p->rcu_protected_pointer,
429 lockdep_is_held(&my_lock) ||
430 lockdep_is_held(&your_lock));
431
432 4. If the access is on the update side, so that it is always protected
433 by my_lock, use rcu_dereference_protected()::
434
435 p1 = rcu_dereference_protected(p->rcu_protected_pointer,
436 lockdep_is_held(&my_lock));
437
438 This can be extended to handle multiple locks as in #3 above,
439 and both can be extended to check other conditions as well.
440
441 5. If the protection is supplied by the caller, and is thus unknown
442 to this code, that is the rare case when rcu_dereference_raw()
443 is appropriate. In addition, rcu_dereference_raw() might be
444 appropriate when the lockdep expression would be excessively
445 complex, except that a better approach in that case might be to
446 take a long hard look at your synchronization design. Still,
447 there are data-locking cases where any one of a very large number
448 of locks or reference counters suffices to protect the pointer,
449 so rcu_dereference_raw() does have its place.
450
451 However, its place is probably quite a bit smaller than one
452 might expect given the number of uses in the current kernel.
453 Ditto for its synonym, rcu_dereference_check( ... , 1), and
454 its close relative, rcu_dereference_protected(... , 1).
455
456
457 SPARSE CHECKING OF RCU-PROTECTED POINTERS
458 -----------------------------------------
459
460 The sparse static-analysis tool checks for non-RCU access to RCU-protected
461 pointers, which can result in "interesting" bugs due to compiler
462 optimizations involving invented loads and perhaps also load tearing.
463 For example, suppose someone mistakenly does something like this::
464
465 p = q->rcu_protected_pointer;
466 do_something_with(p->a);
467 do_something_else_with(p->b);
468
469 If register pressure is high, the compiler might optimize "p" out
470 of existence, transforming the code to something like this::
471
472 do_something_with(q->rcu_protected_pointer->a);
473 do_something_else_with(q->rcu_protected_pointer->b);
474
475 This could fatally disappoint your code if q->rcu_protected_pointer
476 changed in the meantime. Nor is this a theoretical problem: Exactly
477 this sort of bug cost Paul E. McKenney (and several of his innocent
478 colleagues) a three-day weekend back in the early 1990s.
479
480 Load tearing could of course result in dereferencing a mashup of a pair
481 of pointers, which also might fatally disappoint your code.
482
483 These problems could have been avoided simply by making the code instead
484 read as follows::
485
486 p = rcu_dereference(q->rcu_protected_pointer);
487 do_something_with(p->a);
488 do_something_else_with(p->b);
489
490 Unfortunately, these sorts of bugs can be extremely hard to spot during
491 review. This is where the sparse tool comes into play, along with the
492 "__rcu" marker. If you mark a pointer declaration, whether in a structure
493 or as a formal parameter, with "__rcu", which tells sparse to complain if
494 this pointer is accessed directly. It will also cause sparse to complain
495 if a pointer not marked with "__rcu" is accessed using rcu_dereference()
496 and friends. For example, ->rcu_protected_pointer might be declared as
497 follows::
498
499 struct foo __rcu *rcu_protected_pointer;
500
501 Use of "__rcu" is opt-in. If you choose not to use it, then you should
502 ignore the sparse warnings.
503

3. 한국어 전문 번역

영어 원문의 문단 순서와 의미를 유지한 전체 번역입니다. 코드, 함수명, symbol과 URL은 원문 표기를 유지합니다.

Address/data dependency의 출발점

1-50

RCU의 정확성에는 address dependency와 data dependency를 보존하는 일이 매우 중요하다. `rcu_dereference()` 계열이 반환한 pointer는 해당 macro가 pointer를 load한 시점부터, 그 pointer로 나중 memory access의 주소를 계산하거나 store할 값을 계산하는 시점까지 dependency를 운반한다.

일반적인 역참조 `*`, field 선택 `->`, 대입 `=`, 주소 연산 `&`, cast, 상수 덧셈과 뺄셈은 dependency를 자연스럽게 보존한다. 그러나 compiler는 dependency를 언어 수준의 ordering으로 완전히 이해하지 않으므로 특정 변환이 dependency를 없앨 수 있다.

RCU-protected pointer는 반드시 `rcu_dereference()` 계열로 load해야 한다. 그렇지 않으면 `CONFIG_PROVE_RCU` 경고뿐 아니라 compiler의 재로딩으로 같은 논리 pointer가 두 값으로 사용되거나, DEC Alpha에서 pointer 게시 전 초기화보다 오래된 데이터를 읽는 memory corruption이 생길 수 있다. 최근 kernel에서는 `READ_ONCE()`도 Alpha의 관련 재정렬을 막는다.

`rcu_dereference()`의 volatile cast는 compiler가 반환 pointer의 정확한 값을 추론하기 어렵게 한다. 이후 절처럼 compiler가 가능한 pointer 값을 모두 알게 되면 비교 결과로 주소를 상수화해 dependency를 제거할 수 있기 때문이다.

Dependency chain
rcu_dereference() pointer load반환 pointer 보존주소/값 계산후속 memory access게시 전 초기화 관찰

Pointer load가 후속 데이터 access의 주소 또는 값 계산으로 이어져야 한다.

.. _rcu_dereference_doc:

PROPER CARE AND FEEDING OF RETURN VALUES FROM rcu_dereference()
===============================================================

Proper care and feeding of address and data dependencies is critically
important to correct use of things like RCU.  To this end, the pointers
returned from the rcu_dereference() family of primitives carry address and
data dependencies.  These dependencies extend from the rcu_dereference()
macro's load of the pointer to the later use of that pointer to compute
either the address of a later memory access (representing an address
dependency) or the value written by a later memory access (representing
a data dependency).

Most of the time, these dependencies are preserved, permitting you to
freely use values from rcu_dereference().  For example, dereferencing
(prefix "*"), field selection ("->"), assignment ("="), address-of
("&"), casts, and addition or subtraction of constants all work quite
naturally and safely.  However, because current compilers do not take
either address or data dependencies into account it is still possible
to get into trouble.

Follow these rules to preserve the address and data dependencies emanating
from your calls to rcu_dereference() and friends, thus keeping your RCU
readers working properly:

-        You must use one of the rcu_dereference() family of primitives
        to load an RCU-protected pointer, otherwise CONFIG_PROVE_RCU
        will complain.  Worse yet, your code can see random memory-corruption
        bugs due to games that compilers and DEC Alpha can play.
        Without one of the rcu_dereference() primitives, compilers
        can reload the value, and won't your code have fun with two
        different values for a single pointer!  Without rcu_dereference(),
        DEC Alpha can load a pointer, dereference that pointer, and
        return data preceding initialization that preceded the store
        of the pointer.  (As noted later, in recent kernels READ_ONCE()
        also prevents DEC Alpha from playing these tricks.)

        In addition, the volatile cast in rcu_dereference() prevents the
        compiler from deducing the resulting pointer value.  Please see
        the section entitled "EXAMPLE WHERE THE COMPILER KNOWS TOO MUCH"
        for an example where the compiler can in fact deduce the exact
        value of the pointer, and thus cause misordering.

-        In the special case where data is added but is never removed
        while readers are accessing the structure, READ_ONCE() may be used
        instead of rcu_dereference().  In this case, use of READ_ONCE()
        takes on the role of the lockless_dereference() primitive that
        was removed in v4.15.

READ_ONCE, integer cast와 산술 규칙

51-103

Reader가 접근하는 동안 자료가 추가되기만 하고 제거되지 않는 특별한 구조에서는 `rcu_dereference()` 대신 `READ_ONCE()`를 사용할 수 있다. 이 용법은 v4.15에서 제거된 `lockless_dereference()`의 역할을 이어받는다.

`rcu_dereference()`는 pointer 값에만 사용해야 한다. Compiler가 integer 연산을 지나며 값을 추론하기 너무 쉽기 때문이다. 제한된 예외로 pointer를 잠시 `uintptr_t`로 cast해 정렬로 반드시 0인 low-order bit를 set/clear하거나 classic buddy allocator처럼 XOR로 pointer를 변환할 수 있다. 곧바로 다시 pointer로 cast해야 하며 `char *`처럼 정렬 보장이 약한 pointer에는 일반적으로 쓸 수 없다.

산술이 원래 값을 상쇄하면 dependency도 사라진다. 예를 들어 `x - (uintptr_t)x`는 compiler가 0으로 치환할 수 있다. 반면 `p+a-b`에서 우연히 `a==b`여도 식의 표현이 여전히 `p`에 의존하면 dependency가 유지된다.

RCU로 JIT 함수 수명을 보호하고 `rcu_dereference()`에서 얻은 주소를 호출한다면, old JIT 코드와 같은 memory를 재사용하는 시스템에서 instruction cache를 hardware 수준으로 flush해야 할 수 있다. RCU의 data ordering이 instruction cache coherence를 자동으로 대신하지는 않는다.

허용되는 pointer 변환
연산판정
역참조와 ->안전
상수 더하기/빼기안전
정렬된 low bit set/clear제한적으로 안전
Pointer XOR 변환제한적으로 안전
자기 자신을 빼는 cancellation금지
일반 integer 계산금지

Dependency를 보존하는 범위에서만 integer 표현을 잠시 사용한다.

-        You are only permitted to use rcu_dereference() on pointer values.
        The compiler simply knows too much about integral values to
        trust it to carry dependencies through integer operations.
        There are a very few exceptions, namely that you can temporarily
        cast the pointer to uintptr_t in order to:

        -        Set bits and clear bits down in the must-be-zero low-order
                bits of that pointer.  This clearly means that the pointer
                must have alignment constraints, for example, this does
                *not* work in general for char* pointers.

        -        XOR bits to translate pointers, as is done in some
                classic buddy-allocator algorithms.

        It is important to cast the value back to pointer before
        doing much of anything else with it.

-        Avoid cancellation when using the "+" and "-" infix arithmetic
        operators.  For example, for a given variable "x", avoid
        "(x-(uintptr_t)x)" for char* pointers.        The compiler is within its
        rights to substitute zero for this sort of expression, so that
        subsequent accesses no longer depend on the rcu_dereference(),
        again possibly resulting in bugs due to misordering.

        Of course, if "p" is a pointer from rcu_dereference(), and "a"
        and "b" are integers that happen to be equal, the expression
        "p+a-b" is safe because its value still necessarily depends on
        the rcu_dereference(), thus maintaining proper ordering.

-        If you are using RCU to protect JITed functions, so that the
        "()" function-invocation operator is applied to a value obtained
        (directly or indirectly) from rcu_dereference(), you may need to
        interact directly with the hardware to flush instruction caches.
        This issue arises on some systems when a newly JITed function is
        using the same memory that was used by an earlier JITed function.

-        Do not use the results from relational operators ("==", "!=",
        ">", ">=", "<", or "<=") when dereferencing.  For example,
        the following (quite strange) code is buggy::

                int *p;
                int *q;

                ...

                p = rcu_dereference(gp)
                q = &global_q;
                q += p > &oom_p;
                r1 = *q;  /* BUGGY!!! */

        As before, the reason this is buggy is that relational operators
        are often compiled using branches.  And as before, although
        weak-memory machines such as ARM or PowerPC do order stores

관계 연산과 non-NULL pointer 비교

104-218

관계 연산 `==`, `!=`, `>`, `>=`, `<`, `<=`의 결과로 역참조 주소를 선택하면 안 된다. 관계 연산은 branch로 compile되는 경우가 많고 ARM이나 PowerPC는 branch 뒤 store는 ordering할 수 있어도 load를 speculate할 수 있다. 예제의 `q += p > &oom_p; r1 = *q`는 따라서 잘못이다.

특히 `rcu_dereference()` pointer를 non-NULL 상수 주소와 비교한 뒤 역참조하면 compiler가 비교가 참인 branch에서 pointer를 그 상수 주소로 바꿀 수 있다. `p == &default_struct` 뒤 `p->a`는 `default_struct.a`로 변환되고 load가 accessor보다 앞서 speculate될 수 있다.

NULL 비교는 안전하다. NULL이면 역참조하면 안 되고 non-NULL branch에서는 compiler가 실제 주소를 알 수 없기 때문이다. 비교 뒤 pointer를 전혀 역참조하지 않는 경우도 안전하며, reader 밖에서 값만 비교한다면 `rcu_access_pointer()`를 변수에 저장하지 않고 직접 검사하는 편이 낫다.

비교 대상 memory가 compile time, boot time, module init, kthread 생성 전, 이전 lock 획득 전, `mod_timer()` 전처럼 충분히 오래전에 초기화되어 이후 ordering이 중요하지 않다면 안전할 수 있다. 두 pointer가 모두 `rcu_dereference()`에서 왔거나, 비교 뒤 access가 모두 store여서 올바른 control dependency가 있는 경우도 가능하다.

두 pointer가 같지 않고 compiler가 첫 pointer 값을 추론할 정보가 부족하면 보통 안전하다. 하지만 가능한 값이 둘뿐이라면 not-equal 결과가 나머지 하나를 정확히 알려 주므로 안전하지 않다. Profile feedback 기반 value speculation은 dependency를 설계상 깨므로 꺼야 한다. Branch-prediction hardware를 활용한 speculation은 x86 같은 strong-order 시스템에서는 가능하지만 ARM/Power에는 안전하지 않다.

Pointer 비교 뒤 역참조
비교 상황후속 역참조
NULL과 비교안전
비교 뒤 역참조 없음안전
오래전 초기화된 고정 객체조건부 안전
다른 rcu_dereference pointerordering은 유지
일반 non-NULL 상수 pointer위험
가능한 값이 둘뿐인 !=위험

비교가 compiler에 실제 주소를 알려 주는지가 핵심이다.

        after such branches, but can speculate loads, which can again
        result in misordering bugs.

-        Be very careful about comparing pointers obtained from
        rcu_dereference() against non-NULL values.  As Linus Torvalds
        explained, if the two pointers are equal, the compiler could
        substitute the pointer you are comparing against for the pointer
        obtained from rcu_dereference().  For example::

                p = rcu_dereference(gp);
                if (p == &default_struct)
                        do_default(p->a);

        Because the compiler now knows that the value of "p" is exactly
        the address of the variable "default_struct", it is free to
        transform this code into the following::

                p = rcu_dereference(gp);
                if (p == &default_struct)
                        do_default(default_struct.a);

        On ARM and Power hardware, the load from "default_struct.a"
        can now be speculated, such that it might happen before the
        rcu_dereference().  This could result in bugs due to misordering.

        However, comparisons are OK in the following cases:

        -        The comparison was against the NULL pointer.  If the
                compiler knows that the pointer is NULL, you had better
                not be dereferencing it anyway.  If the comparison is
                non-equal, the compiler is none the wiser.  Therefore,
                it is safe to compare pointers from rcu_dereference()
                against NULL pointers.

        -        The pointer is never dereferenced after being compared.
                Since there are no subsequent dereferences, the compiler
                cannot use anything it learned from the comparison
                to reorder the non-existent subsequent dereferences.
                This sort of comparison occurs frequently when scanning
                RCU-protected circular linked lists.

                Note that if the pointer comparison is done outside
                of an RCU read-side critical section, and the pointer
                is never dereferenced, rcu_access_pointer() should be
                used in place of rcu_dereference().  In most cases,
                it is best to avoid accidental dereferences by testing
                the rcu_access_pointer() return value directly, without
                assigning it to a variable.

                Within an RCU read-side critical section, there is little
                reason to use rcu_access_pointer().

        -        The comparison is against a pointer that references memory
                that was initialized "a long time ago."  The reason
                this is safe is that even if misordering occurs, the
                misordering will not affect the accesses that follow
                the comparison.  So exactly how long ago is "a long
                time ago"?  Here are some possibilities:

                -        Compile time.

                -        Boot time.

                -        Module-init time for module code.

                -        Prior to kthread creation for kthread code.

                -        During some prior acquisition of the lock that
                        we now hold.

                -        Before mod_timer() time for a timer handler.

                There are many other possibilities involving the Linux
                kernel's wide array of primitives that cause code to
                be invoked at a later time.

        -        The pointer being compared against also came from
                rcu_dereference().  In this case, both pointers depend
                on one rcu_dereference() or another, so you get proper
                ordering either way.

                That said, this situation can make certain RCU usage
                bugs more likely to happen.  Which can be a good thing,
                at least if they happen during testing.  An example
                of such an RCU usage bug is shown in the section titled
                "EXAMPLE OF AMPLIFIED RCU-USAGE BUG".

        -        All of the accesses following the comparison are stores,
                so that a control dependency preserves the needed ordering.
                That said, it is easy to get control dependencies wrong.
                Please see the "CONTROL DEPENDENCIES" section of
                Documentation/memory-barriers.txt for more details.

        -        The pointers are not equal *and* the compiler does
                not have enough information to deduce the value of the
                pointer.  Note that the volatile cast in rcu_dereference()
                will normally prevent the compiler from knowing too much.

                However, please note that if the compiler knows that the
                pointer takes on only one of two values, a not-equal
                comparison will provide exactly the information that the
                compiler needs to deduce the value of the pointer.

-        Disable any value-speculation optimizations that your compiler
        might provide, especially if you are making use of feedback-based
        optimizations that take data collected from prior runs.  Such
        value-speculation optimizations reorder operations by design.

        There is one exception to this rule:  Value-speculation
        optimizations that leverage the branch-prediction hardware are
        safe on strongly ordered systems (such as x86), but not on weakly
        ordered systems (such as ARM or Power).  Choose your compiler
        command-line options wisely!

비일관 reader bug가 증폭되는 예

219-340

RCU updater는 reader와 동시에 실행하므로 reader가 stale하거나 서로 일관되지 않은 필드 값을 보는 것은 기본적으로 허용된다. 예제의 updater는 새 `struct foo`에 `b=43`, `c=44`를 쓰고 `gp1`으로 게시한 뒤 같은 객체를 `b=143`, `c=144`로 바꾸고 `gp2`로 게시한다.

Reader가 먼저 `gp2`를 읽어 `r1=p->b`에서 143을 얻고, 이어 `gp1`을 읽어 `p==q`를 확인한 뒤 `p->c`를 읽어도 weak-order 시스템에서는 44가 나올 수 있다. Compiler가 `q->c`와 `p->c`를 같다고 판단하는 변환뿐 아니라, 두 load 사이 updater가 다시 실행되는 정상적인 동시성만으로도 같은 결과가 가능하다.

일관된 view가 필요하면 별도 lock을 사용한다. 예제는 `struct foo`에 spinlock을 넣고 updater가 필드 묶음을 lock 아래 변경하며, reader도 `p->lock` 아래 `b`와 `c`를 읽는다. `p!=q`이면 `q->lock`도 잡아 해당 객체의 `c`를 안정시킨다.

RCU는 객체의 존재와 publish ordering을 제공하지만 같은 객체의 여러 시점 필드를 atomic snapshot으로 만들지 않는다. Freshness와 consistency가 필요하면 그 요구에 맞는 lock, sequence counter 또는 copy-replace 설계를 함께 사용해야 한다.

Amplified inconsistency
초기 b=43,c=44gp1으로 게시b=143,c=144 갱신gp2로 게시reader가 gp2와 gp1 비교r1=143,r2=44 가능

같은 객체가 두 번 게시되고 중간에 필드가 바뀌면 reader는 시점을 섞어 볼 수 있다.

RCU와 일관성
필요 속성수단
객체 수명RCU grace period
안전한 pointer 공개rcu_assign_pointer/rcu_dereference
다중 필드 일관성spinlock 또는 seqcount
불변 snapshotcopy-update-replace

필요한 속성마다 별도 도구가 있다.

EXAMPLE OF AMPLIFIED RCU-USAGE BUG
----------------------------------

Because updaters can run concurrently with RCU readers, RCU readers can
see stale and/or inconsistent values.  If RCU readers need fresh or
consistent values, which they sometimes do, they need to take proper
precautions.  To see this, consider the following code fragment::

        struct foo {
                int a;
                int b;
                int c;
        };
        struct foo *gp1;
        struct foo *gp2;

        void updater(void)
        {
                struct foo *p;

                p = kmalloc(...);
                if (p == NULL)
                        deal_with_it();
                p->a = 42;  /* Each field in its own cache line. */
                p->b = 43;
                p->c = 44;
                rcu_assign_pointer(gp1, p);
                p->b = 143;
                p->c = 144;
                rcu_assign_pointer(gp2, p);
        }

        void reader(void)
        {
                struct foo *p;
                struct foo *q;
                int r1, r2;

                rcu_read_lock();
                p = rcu_dereference(gp2);
                if (p == NULL)
                        return;
                r1 = p->b;  /* Guaranteed to get 143. */
                q = rcu_dereference(gp1);  /* Guaranteed non-NULL. */
                if (p == q) {
                        /* The compiler decides that q->c is same as p->c. */
                        r2 = p->c; /* Could get 44 on weakly order system. */
                } else {
                        r2 = p->c - r1; /* Unconditional access to p->c. */
                }
                rcu_read_unlock();
                do_something_with(r1, r2);
        }

You might be surprised that the outcome (r1 == 143 && r2 == 44) is possible,
but you should not be.  After all, the updater might have been invoked
a second time between the time reader() loaded into "r1" and the time
that it loaded into "r2".  The fact that this same result can occur due
to some reordering from the compiler and CPUs is beside the point.

But suppose that the reader needs a consistent view?

Then one approach is to use locking, for example, as follows::

        struct foo {
                int a;
                int b;
                int c;
                spinlock_t lock;
        };
        struct foo *gp1;
        struct foo *gp2;

        void updater(void)
        {
                struct foo *p;

                p = kmalloc(...);
                if (p == NULL)
                        deal_with_it();
                spin_lock(&p->lock);
                p->a = 42;  /* Each field in its own cache line. */
                p->b = 43;
                p->c = 44;
                spin_unlock(&p->lock);
                rcu_assign_pointer(gp1, p);
                spin_lock(&p->lock);
                p->b = 143;
                p->c = 144;
                spin_unlock(&p->lock);
                rcu_assign_pointer(gp2, p);
        }

        void reader(void)
        {
                struct foo *p;
                struct foo *q;
                int r1, r2;

                rcu_read_lock();
                p = rcu_dereference(gp2);
                if (p == NULL)
                        return;
                spin_lock(&p->lock);
                r1 = p->b;  /* Guaranteed to get 143. */
                q = rcu_dereference(gp1);  /* Guaranteed non-NULL. */
                if (p == q) {
                        /* The compiler decides that q->c is same as p->c. */
                        r2 = p->c; /* Locking guarantees r2 == 144. */
                } else {
                        spin_lock(&q->lock);
                        r2 = q->c - r1;
                        spin_unlock(&q->lock);
                }
                rcu_read_unlock();
                spin_unlock(&p->lock);
                do_something_with(r1, r2);
        }

As always, use the right tool for the job!

Compiler가 pointer 값을 너무 많이 아는 경우

341-396

평범한 상황에서는 `rcu_dereference()`의 volatile cast가 compiler의 pointer 값 추론을 방해한다. 하지만 accessor를 생략하고 translation unit 안에서 `gp`의 모든 store가 보이면 compiler는 가능한 값을 정확히 알 수 있다.

예제의 static `gp`는 처음 `&variable1`이고 유일한 store가 `rcu_assign_pointer(gp, &variable2)`다. Reader가 `p=gp` 뒤 `p==&variable1`을 검사하면 compiler는 true branch의 `p`가 `variable1`, false branch의 `p`가 `variable2`라고 확정한다.

그 결과 반환 load가 `gp` load와 무관한 상수 객체 access로 바뀌어 ordering이 사라지고, weak-order 시스템에서 `variable2.b`의 초기화 전 쓰레기 값을 읽을 수 있다. 단순 `barrier()`만으로는 RCU subscription dependency를 대신하지 못한다.

반환 pointer를 역참조할 예정이라면 `rcu_dereference()`는 선택 사항이 아니다. Accessor는 hardware ordering뿐 아니라 compiler가 load를 발명하거나 상수화하는 것을 제한한다.

Compiler의 상수화
gp 후보: variable1 또는 variable2평범한 p = gpp == variable1 비교각 branch에서 p 주소 확정후속 field load가 gp load와 분리초기화 전 값 가능

가능한 pointer가 두 개뿐이면 branch가 실제 주소를 노출한다.

EXAMPLE WHERE THE COMPILER KNOWS TOO MUCH
-----------------------------------------

If a pointer obtained from rcu_dereference() compares not-equal to some
other pointer, the compiler normally has no clue what the value of the
first pointer might be.  This lack of knowledge prevents the compiler
from carrying out optimizations that otherwise might destroy the ordering
guarantees that RCU depends on.  And the volatile cast in rcu_dereference()
should prevent the compiler from guessing the value.

But without rcu_dereference(), the compiler knows more than you might
expect.  Consider the following code fragment::

        struct foo {
                int a;
                int b;
        };
        static struct foo variable1;
        static struct foo variable2;
        static struct foo *gp = &variable1;

        void updater(void)
        {
                initialize_foo(&variable2);
                rcu_assign_pointer(gp, &variable2);
                /*
                 * The above is the only store to gp in this translation unit,
                 * and the address of gp is not exported in any way.
                 */
        }

        int reader(void)
        {
                struct foo *p;

                p = gp;
                barrier();
                if (p == &variable1)
                        return p->a; /* Must be variable1.a. */
                else
                        return p->b; /* Must be variable2.b. */
        }

Because the compiler can see all stores to "gp", it knows that the only
possible values of "gp" are "variable1" on the one hand and "variable2"
on the other.  The comparison in reader() therefore tells the compiler
the exact value of "p" even in the not-equals case.  This allows the
compiler to make the return values independent of the load from "gp",
in turn destroying the ordering between this load and the loads of the
return values.  This can result in "p->b" returning pre-initialization
garbage values on weakly ordered systems.

In short, rcu_dereference() is *not* optional when you are going to
dereference the resulting pointer.

어떤 rcu_dereference() 계열을 쓸 것인가

397-456

`rcu_dereference_raw()`와 조건을 상수 1로 둔 `_check()` 또는 `_protected()` 사용은 피해야 한다. 보호 관계를 검증할 기회를 없애기 때문이다.

일반 RCU reader 안이어야 하는 access는 `rcu_dereference()`를 쓴다. 통합된 RCU flavor에서는 `rcu_read_lock()`뿐 아니라 bottom half, interrupt, preemption을 disable하는 구간과 spinlock critical section도 암묵적 RCU reader다. `CONFIG_PREEMPT_RT=y`에서 spinlock이 preemptible이어도 이 의미는 유지된다.

RCU reader 또는 `my_lock` 아래에서 호출될 수 있으면 `rcu_dereference_check(..., lockdep_is_held(&my_lock))`를 쓴다. `my_lock`이나 `your_lock` 중 하나면 조건을 OR로 합친다. Update-side에서 항상 `my_lock`이 보호한다면 `rcu_dereference_protected()`가 맞다.

보호를 caller가 제공해 현재 함수가 표현할 수 없거나 수많은 lock/reference counter 중 하나가 보호하는 드문 data-locking 설계에서는 `rcu_dereference_raw()`를 쓸 수 있다. 조건이 지나치게 복잡하다면 먼저 synchronization 설계를 단순화할 수 있는지 검토해야 한다.

Accessor 선택표
호출 문맥Accessor
항상 RCU readerrcu_dereference()
Reader 또는 하나 이상의 lockrcu_dereference_check()
항상 update lockrcu_dereference_protected()
보호를 이 함수에서 표현 불가rcu_dereference_raw(), 드물게

실제 호출 문맥을 가장 좁고 검증 가능하게 표현한다.

WHICH MEMBER OF THE rcu_dereference() FAMILY SHOULD YOU USE?
------------------------------------------------------------

First, please avoid using rcu_dereference_raw() and also please avoid
using rcu_dereference_check() and rcu_dereference_protected() with a
second argument with a constant value of 1 (or true, for that matter).
With that caution out of the way, here is some guidance for which
member of the rcu_dereference() to use in various situations:

1.        If the access needs to be within an RCU read-side critical
        section, use rcu_dereference().  With the new consolidated
        RCU flavors, an RCU read-side critical section is entered
        using rcu_read_lock(), anything that disables bottom halves,
        anything that disables interrupts, or anything that disables
        preemption.  Please note that spinlock critical sections
        are also implied RCU read-side critical sections, even when
        they are preemptible, as they are in kernels built with
        CONFIG_PREEMPT_RT=y.

2.        If the access might be within an RCU read-side critical section
        on the one hand, or protected by (say) my_lock on the other,
        use rcu_dereference_check(), for example::

                p1 = rcu_dereference_check(p->rcu_protected_pointer,
                                           lockdep_is_held(&my_lock));


3.        If the access might be within an RCU read-side critical section
        on the one hand, or protected by either my_lock or your_lock on
        the other, again use rcu_dereference_check(), for example::

                p1 = rcu_dereference_check(p->rcu_protected_pointer,
                                           lockdep_is_held(&my_lock) ||
                                           lockdep_is_held(&your_lock));

4.        If the access is on the update side, so that it is always protected
        by my_lock, use rcu_dereference_protected()::

                p1 = rcu_dereference_protected(p->rcu_protected_pointer,
                                               lockdep_is_held(&my_lock));

        This can be extended to handle multiple locks as in #3 above,
        and both can be extended to check other conditions as well.

5.        If the protection is supplied by the caller, and is thus unknown
        to this code, that is the rare case when rcu_dereference_raw()
        is appropriate.  In addition, rcu_dereference_raw() might be
        appropriate when the lockdep expression would be excessively
        complex, except that a better approach in that case might be to
        take a long hard look at your synchronization design.  Still,
        there are data-locking cases where any one of a very large number
        of locks or reference counters suffices to protect the pointer,
        so rcu_dereference_raw() does have its place.

        However, its place is probably quite a bit smaller than one
        might expect given the number of uses in the current kernel.
        Ditto for its synonym, rcu_dereference_check( ... , 1), and
        its close relative, rcu_dereference_protected(... , 1).

Sparse의 __rcu pointer 검사

457-502

Sparse는 RCU pointer를 일반 load로 접근하는 코드를 찾아 compiler의 invented load와 load tearing 위험을 줄인다. `p = q->rcu_protected_pointer` 뒤 `p->a`, `p->b`를 읽는 코드는 register pressure가 높을 때 compiler가 `p`를 없애고 field마다 `q->rcu_protected_pointer`를 다시 읽도록 바꿀 수 있다.

두 load 사이 pointer가 바뀌면 서로 다른 객체의 필드를 사용한다. Load tearing이 있으면 두 pointer bit를 섞은 존재하지 않는 주소를 역참조할 수도 있다. `p = rcu_dereference(q->rcu_protected_pointer)`로 한 번 load하면 이러한 변환과 ordering 문제를 막는다.

Review만으로 찾기 어려우므로 declaration을 `struct foo __rcu *rcu_protected_pointer`처럼 `__rcu`로 표시한다. Sparse는 이 pointer를 직접 접근하면 경고하고, 반대로 `__rcu`가 아닌 pointer를 `rcu_dereference()` 계열로 접근해도 경고한다.

`__rcu`는 opt-in이다. 프로젝트가 이 annotation을 쓰지 않기로 했다면 관련 sparse warning을 무시할 수 있지만, 사용한다면 pointer 선언과 accessor를 일관되게 맞춰야 한다.

Sparse가 막는 재로딩
__rcu pointer 선언rcu_dereference() 한 번p에 안정된 주소 저장p->a 접근p->b 접근직접 load면 sparse 경고

명시적 RCU load가 여러 field access의 공통 pointer 값을 고정한다.

SPARSE CHECKING OF RCU-PROTECTED POINTERS
-----------------------------------------

The sparse static-analysis tool checks for non-RCU access to RCU-protected
pointers, which can result in "interesting" bugs due to compiler
optimizations involving invented loads and perhaps also load tearing.
For example, suppose someone mistakenly does something like this::

        p = q->rcu_protected_pointer;
        do_something_with(p->a);
        do_something_else_with(p->b);

If register pressure is high, the compiler might optimize "p" out
of existence, transforming the code to something like this::

        do_something_with(q->rcu_protected_pointer->a);
        do_something_else_with(q->rcu_protected_pointer->b);

This could fatally disappoint your code if q->rcu_protected_pointer
changed in the meantime.  Nor is this a theoretical problem:  Exactly
this sort of bug cost Paul E. McKenney (and several of his innocent
colleagues) a three-day weekend back in the early 1990s.

Load tearing could of course result in dereferencing a mashup of a pair
of pointers, which also might fatally disappoint your code.

These problems could have been avoided simply by making the code instead
read as follows::

        p = rcu_dereference(q->rcu_protected_pointer);
        do_something_with(p->a);
        do_something_else_with(p->b);

Unfortunately, these sorts of bugs can be extremely hard to spot during
review.  This is where the sparse tool comes into play, along with the
"__rcu" marker.  If you mark a pointer declaration, whether in a structure
or as a formal parameter, with "__rcu", which tells sparse to complain if
this pointer is accessed directly.  It will also cause sparse to complain
if a pointer not marked with "__rcu" is accessed using rcu_dereference()
and friends.  For example, ->rcu_protected_pointer might be declared as
follows::

        struct foo __rcu *rcu_protected_pointer;

Use of "__rcu" is opt-in.  If you choose not to use it, then you should
ignore the sparse warnings.