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