Skip to content

Commit b00aedf

Browse files
frextritepaulmckrcu
authored andcommitted
doc: Convert to rcu_dereference.txt to rcu_dereference.rst
This patch converts rcu_dereference.txt to rcu_dereference.rst and adds it to index.rst Signed-off-by: Amol Grover <[email protected]> Signed-off-by: Paul E. McKenney <[email protected]>
1 parent 5e1bc93 commit b00aedf

File tree

2 files changed

+42
-34
lines changed

2 files changed

+42
-34
lines changed

Documentation/RCU/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ RCU concepts
88
:maxdepth: 3
99

1010
arrayRCU
11+
rcu_dereference
1112
whatisRCU
1213
rcu
1314
listRCU

Documentation/RCU/rcu_dereference.txt renamed to Documentation/RCU/rcu_dereference.rst

Lines changed: 41 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
.. _rcu_dereference_doc:
2+
13
PROPER CARE AND FEEDING OF RETURN VALUES FROM rcu_dereference()
4+
===============================================================
25

36
Most of the time, you can use values from rcu_dereference() or one of
47
the similar primitives without worries. Dereferencing (prefix "*"),
@@ -8,7 +11,7 @@ subtraction of constants, and casts all work quite naturally and safely.
811
It is nevertheless possible to get into trouble with other operations.
912
Follow these rules to keep your RCU code working properly:
1013

11-
o You must use one of the rcu_dereference() family of primitives
14+
- You must use one of the rcu_dereference() family of primitives
1215
to load an RCU-protected pointer, otherwise CONFIG_PROVE_RCU
1316
will complain. Worse yet, your code can see random memory-corruption
1417
bugs due to games that compilers and DEC Alpha can play.
@@ -25,24 +28,24 @@ o You must use one of the rcu_dereference() family of primitives
2528
for an example where the compiler can in fact deduce the exact
2629
value of the pointer, and thus cause misordering.
2730

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

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

39-
o XOR bits to translate pointers, as is done in some
42+
- XOR bits to translate pointers, as is done in some
4043
classic buddy-allocator algorithms.
4144

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

45-
o Avoid cancellation when using the "+" and "-" infix arithmetic
48+
- Avoid cancellation when using the "+" and "-" infix arithmetic
4649
operators. For example, for a given variable "x", avoid
4750
"(x-(uintptr_t)x)" for char* pointers. The compiler is within its
4851
rights to substitute zero for this sort of expression, so that
@@ -54,16 +57,16 @@ o Avoid cancellation when using the "+" and "-" infix arithmetic
5457
"p+a-b" is safe because its value still necessarily depends on
5558
the rcu_dereference(), thus maintaining proper ordering.
5659

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

64-
o Do not use the results from relational operators ("==", "!=",
67+
- Do not use the results from relational operators ("==", "!=",
6568
">", ">=", "<", or "<=") when dereferencing. For example,
66-
the following (quite strange) code is buggy:
69+
the following (quite strange) code is buggy::
6770

6871
int *p;
6972
int *q;
@@ -81,19 +84,19 @@ o Do not use the results from relational operators ("==", "!=",
8184
after such branches, but can speculate loads, which can again
8285
result in misordering bugs.
8386

84-
o Be very careful about comparing pointers obtained from
87+
- Be very careful about comparing pointers obtained from
8588
rcu_dereference() against non-NULL values. As Linus Torvalds
8689
explained, if the two pointers are equal, the compiler could
8790
substitute the pointer you are comparing against for the pointer
88-
obtained from rcu_dereference(). For example:
91+
obtained from rcu_dereference(). For example::
8992

9093
p = rcu_dereference(gp);
9194
if (p == &default_struct)
9295
do_default(p->a);
9396

9497
Because the compiler now knows that the value of "p" is exactly
9598
the address of the variable "default_struct", it is free to
96-
transform this code into the following:
99+
transform this code into the following::
97100

98101
p = rcu_dereference(gp);
99102
if (p == &default_struct)
@@ -105,14 +108,14 @@ o Be very careful about comparing pointers obtained from
105108

106109
However, comparisons are OK in the following cases:
107110

108-
o The comparison was against the NULL pointer. If the
111+
- The comparison was against the NULL pointer. If the
109112
compiler knows that the pointer is NULL, you had better
110113
not be dereferencing it anyway. If the comparison is
111114
non-equal, the compiler is none the wiser. Therefore,
112115
it is safe to compare pointers from rcu_dereference()
113116
against NULL pointers.
114117

115-
o The pointer is never dereferenced after being compared.
118+
- The pointer is never dereferenced after being compared.
116119
Since there are no subsequent dereferences, the compiler
117120
cannot use anything it learned from the comparison
118121
to reorder the non-existent subsequent dereferences.
@@ -124,31 +127,31 @@ o Be very careful about comparing pointers obtained from
124127
dereferenced, rcu_access_pointer() should be used in place
125128
of rcu_dereference().
126129

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

134-
o Compile time.
137+
- Compile time.
135138

136-
o Boot time.
139+
- Boot time.
137140

138-
o Module-init time for module code.
141+
- Module-init time for module code.
139142

140-
o Prior to kthread creation for kthread code.
143+
- Prior to kthread creation for kthread code.
141144

142-
o During some prior acquisition of the lock that
145+
- During some prior acquisition of the lock that
143146
we now hold.
144147

145-
o Before mod_timer() time for a timer handler.
148+
- Before mod_timer() time for a timer handler.
146149

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

151-
o The pointer being compared against also came from
154+
- The pointer being compared against also came from
152155
rcu_dereference(). In this case, both pointers depend
153156
on one rcu_dereference() or another, so you get proper
154157
ordering either way.
@@ -159,13 +162,13 @@ o Be very careful about comparing pointers obtained from
159162
of such an RCU usage bug is shown in the section titled
160163
"EXAMPLE OF AMPLIFIED RCU-USAGE BUG".
161164

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

168-
o The pointers are not equal -and- the compiler does
171+
- The pointers are not equal -and- the compiler does
169172
not have enough information to deduce the value of the
170173
pointer. Note that the volatile cast in rcu_dereference()
171174
will normally prevent the compiler from knowing too much.
@@ -175,7 +178,7 @@ o Be very careful about comparing pointers obtained from
175178
comparison will provide exactly the information that the
176179
compiler needs to deduce the value of the pointer.
177180

178-
o Disable any value-speculation optimizations that your compiler
181+
- Disable any value-speculation optimizations that your compiler
179182
might provide, especially if you are making use of feedback-based
180183
optimizations that take data collected from prior runs. Such
181184
value-speculation optimizations reorder operations by design.
@@ -188,11 +191,12 @@ o Disable any value-speculation optimizations that your compiler
188191

189192

190193
EXAMPLE OF AMPLIFIED RCU-USAGE BUG
194+
----------------------------------
191195

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

197201
struct foo {
198202
int a;
@@ -244,7 +248,7 @@ to some reordering from the compiler and CPUs is beside the point.
244248

245249
But suppose that the reader needs a consistent view?
246250

247-
Then one approach is to use locking, for example, as follows:
251+
Then one approach is to use locking, for example, as follows::
248252

249253
struct foo {
250254
int a;
@@ -299,6 +303,7 @@ As always, use the right tool for the job!
299303

300304

301305
EXAMPLE WHERE THE COMPILER KNOWS TOO MUCH
306+
-----------------------------------------
302307

303308
If a pointer obtained from rcu_dereference() compares not-equal to some
304309
other pointer, the compiler normally has no clue what the value of the
@@ -308,7 +313,7 @@ guarantees that RCU depends on. And the volatile cast in rcu_dereference()
308313
should prevent the compiler from guessing the value.
309314

310315
But without rcu_dereference(), the compiler knows more than you might
311-
expect. Consider the following code fragment:
316+
expect. Consider the following code fragment::
312317

313318
struct foo {
314319
int a;
@@ -354,6 +359,7 @@ dereference the resulting pointer.
354359

355360

356361
WHICH MEMBER OF THE rcu_dereference() FAMILY SHOULD YOU USE?
362+
------------------------------------------------------------
357363

358364
First, please avoid using rcu_dereference_raw() and also please avoid
359365
using rcu_dereference_check() and rcu_dereference_protected() with a
@@ -370,22 +376,22 @@ member of the rcu_dereference() to use in various situations:
370376

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

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

378384

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

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

387393
4. If the access is on the update side, so that it is always protected
388-
by my_lock, use rcu_dereference_protected():
394+
by my_lock, use rcu_dereference_protected()::
389395

390396
p1 = rcu_dereference_protected(p->rcu_protected_pointer,
391397
lockdep_is_held(&my_lock));
@@ -410,18 +416,19 @@ member of the rcu_dereference() to use in various situations:
410416

411417

412418
SPARSE CHECKING OF RCU-PROTECTED POINTERS
419+
-----------------------------------------
413420

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

419426
p = q->rcu_protected_pointer;
420427
do_something_with(p->a);
421428
do_something_else_with(p->b);
422429

423430
If register pressure is high, the compiler might optimize "p" out
424-
of existence, transforming the code to something like this:
431+
of existence, transforming the code to something like this::
425432

426433
do_something_with(q->rcu_protected_pointer->a);
427434
do_something_else_with(q->rcu_protected_pointer->b);
@@ -435,7 +442,7 @@ Load tearing could of course result in dereferencing a mashup of a pair
435442
of pointers, which also might fatally disappoint your code.
436443

437444
These problems could have been avoided simply by making the code instead
438-
read as follows:
445+
read as follows::
439446

440447
p = rcu_dereference(q->rcu_protected_pointer);
441448
do_something_with(p->a);
@@ -448,7 +455,7 @@ or as a formal parameter, with "__rcu", which tells sparse to complain if
448455
this pointer is accessed directly. It will also cause sparse to complain
449456
if a pointer not marked with "__rcu" is accessed using rcu_dereference()
450457
and friends. For example, ->rcu_protected_pointer might be declared as
451-
follows:
458+
follows::
452459

453460
struct foo __rcu *rcu_protected_pointer;
454461

0 commit comments

Comments
 (0)