€• Œsphinx.addnodes”Œdocument”“”)�”}”(Œ rawsource”Œ”Œchildren”]”(Œ translations”Œ LanguagesNode”“”)�”}”(hhh]”(hŒ pending_xref”“”)�”}”(hhh]”Œdocutils.nodes”ŒText”“”ŒChinese (Simplified)”…”�”}”Œparent”hsbaŒ attributes”}”(Œids”]”Œclasses”]”Œnames”]”Œdupnames”]”Œbackrefs”]”Œ refdomain”Œstd”Œreftype”Œdoc”Œ reftarget”Œ'/translations/zh_CN/RCU/rcu_dereference”Œmodname”NŒ classname”NŒ refexplicit”ˆuŒtagname”hhh ubh)�”}”(hhh]”hŒChinese (Traditional)”…”�”}”hh2sbah}”(h]”h ]”h"]”h$]”h&]”Œ refdomain”h)Œreftype”h+Œ reftarget”Œ'/translations/zh_TW/RCU/rcu_dereference”Œmodname”NŒ classname”NŒ refexplicit”ˆuh1hhh ubh)�”}”(hhh]”hŒItalian”…”�”}”hhFsbah}”(h]”h ]”h"]”h$]”h&]”Œ refdomain”h)Œreftype”h+Œ reftarget”Œ'/translations/it_IT/RCU/rcu_dereference”Œmodname”NŒ classname”NŒ refexplicit”ˆuh1hhh ubh)�”}”(hhh]”hŒJapanese”…”�”}”hhZsbah}”(h]”h ]”h"]”h$]”h&]”Œ refdomain”h)Œreftype”h+Œ reftarget”Œ'/translations/ja_JP/RCU/rcu_dereference”Œmodname”NŒ classname”NŒ refexplicit”ˆuh1hhh ubh)�”}”(hhh]”hŒKorean”…”�”}”hhnsbah}”(h]”h ]”h"]”h$]”h&]”Œ refdomain”h)Œreftype”h+Œ reftarget”Œ'/translations/ko_KR/RCU/rcu_dereference”Œmodname”NŒ classname”NŒ refexplicit”ˆuh1hhh ubh)�”}”(hhh]”hŒPortuguese (Brazilian)”…”�”}”hh‚sbah}”(h]”h ]”h"]”h$]”h&]”Œ refdomain”h)Œreftype”h+Œ reftarget”Œ'/translations/pt_BR/RCU/rcu_dereference”Œmodname”NŒ classname”NŒ refexplicit”ˆuh1hhh ubh)�”}”(hhh]”hŒSpanish”…”�”}”hh–sbah}”(h]”h ]”h"]”h$]”h&]”Œ refdomain”h)Œreftype”h+Œ reftarget”Œ'/translations/sp_SP/RCU/rcu_dereference”Œmodname”NŒ classname”NŒ refexplicit”ˆuh1hhh ubeh}”(h]”h ]”h"]”h$]”h&]”Œcurrent_language”ŒEnglish”uh1h hhŒ _document”hŒsource”NŒline”NubhŒtarget”“”)�”}”(hŒ.. _rcu_dereference_doc:”h]”h}”(h]”h ]”h"]”h$]”h&]”Œrefid”Œrcu-dereference-doc”uh1hµh´Khhh²hh³ŒA/var/lib/git/docbuild/linux/Documentation/RCU/rcu_dereference.rst”ubhŒsection”“”)�”}”(hhh]”(hŒtitle”“”)�”}”(hŒ?PROPER CARE AND FEEDING OF RETURN VALUES FROM rcu_dereference()”h]”hŒ?PROPER CARE AND FEEDING OF RETURN VALUES FROM rcu_dereference()”…”�”}”(hhËh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÉhhÆh²hh³hÃh´KubhŒ paragraph”“”)�”}”(hX 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).”h]”hX 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).”…”�”}”(hhÛh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´KhhÆh²hubhÚ)�”}”(hXµ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.”h]”hXÅ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.”…”�”}”(hhéh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´KhhÆh²hubhÚ)�”}”(hŒ¬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:”h]”hŒ¬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:”…”�”}”(hh÷h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´KhhÆh²hubhŒ bullet_list”“”)�”}”(hhh]”(hŒ list_item”“”)�”}”(hXÖ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. ”h]”(hÚ)�”}”(hX¡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.)”h]”hX£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.)”…”�”}”(hjh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´Khj ubhÚ)�”}”(hX2In 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.”h]”hX6In 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.”…”�”}”(hjh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´K'hj ubeh}”(h]”h ]”h"]”h$]”h&]”uh1j hjh²hh³hÃh´Nubj )�”}”(hXIn 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. ”h]”hÚ)�”}”(hXIn 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.”h]”hXIn 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.”…”�”}”(hj6h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´K-hj2ubah}”(h]”h ]”h"]”h$]”h&]”uh1j hjh²hh³hÃh´Nubj )�”}”(hXæ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. ”h]”(hÚ)�”}”(hX%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:”h]”hX%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:”…”�”}”(hjNh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´K3hjJubj)�”}”(hhh]”(j )�”}”(hŒØ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. ”h]”hÚ)�”}”(hŒ×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.”h]”(hŒ®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 ”…”�”}”(hjch²hh³Nh´NubhŒemphasis”“”)�”}”(hŒ*not*”h]”hŒnot”…”�”}”(hjmh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jkhjcubhŒ$ work in general for char* pointers.”…”�”}”(hjch²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´K9hj_ubah}”(h]”h ]”h"]”h$]”h&]”uh1j hj\ubj )�”}”(hŒWXOR bits to translate pointers, as is done in some classic buddy-allocator algorithms. ”h]”hÚ)�”}”(hŒVXOR bits to translate pointers, as is done in some classic buddy-allocator algorithms.”h]”hŒVXOR bits to translate pointers, as is done in some classic buddy-allocator algorithms.”…”�”}”(hj�h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´K>hj‹ubah}”(h]”h ]”h"]”h$]”h&]”uh1j hj\ubeh}”(h]”h ]”h"]”h$]”h&]”Œbullet”Œ-”uh1jh³hÃh´K9hjJubhÚ)�”}”(hŒ]It is important to cast the value back to pointer before doing much of anything else with it.”h]”hŒ]It is important to cast the value back to pointer before doing much of anything else with it.”…”�”}”(hj«h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´KAhjJubeh}”(h]”h ]”h"]”h$]”h&]”uh1j hjh²hh³hÃh´Nubj )�”}”(hXcAvoid 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. ”h]”(hÚ)�”}”(hXmAvoid 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.”h]”hX}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.”…”�”}”(hjÃh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´KDhj¿ubhÚ)�”}”(hŒó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.”h]”hXOf 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.”…”�”}”(hjÑh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´KKhj¿ubeh}”(h]”h ]”h"]”h$]”h&]”uh1j hjh²hh³hÃh´Nubj )�”}”(hXƒ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. ”h]”hÚ)�”}”(hX‚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.”h]”hX†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.”…”�”}”(hjéh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´KPhjåubah}”(h]”h ]”h"]”h$]”h&]”uh1j hjh²hh³hÃh´Nubj )�”}”(hX[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 after such branches, but can speculate loads, which can again result in misordering bugs. ”h]”(hÚ)�”}”(hŒ¦Do not use the results from relational operators ("==", "!=", ">", ">=", "<", or "<=") when dereferencing. For example, the following (quite strange) code is buggy::”h]”hŒ½Do not use the results from relational operators (“==â€�, “!=â€�, “>â€�, “>=â€�, “<â€�, or “<=â€�) when dereferencing. For example, the following (quite strange) code is buggy:”…”�”}”(hjh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´KWhjýubhŒ literal_block”“”)�”}”(hŒfint *p; int *q; ... p = rcu_dereference(gp) q = &global_q; q += p > &oom_p; r1 = *q; /* BUGGY!!! */”h]”hŒfint *p; int *q; ... p = rcu_dereference(gp) q = &global_q; q += p > &oom_p; r1 = *q; /* BUGGY!!! */”…”�”}”hjsbah}”(h]”h ]”h"]”h$]”h&]”Œ xml:space”Œpreserve”uh1jh³hÃh´K[hjýubhÚ)�”}”(hXAs 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 after such branches, but can speculate loads, which can again result in misordering bugs.”h]”hXAs 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 after such branches, but can speculate loads, which can again result in misordering bugs.”…”�”}”(hj!h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´Kehjýubeh}”(h]”h ]”h"]”h$]”h&]”uh1j hjh²hh³hÃh´Nubj )�”}”(hXÚ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. ”h]”(hÚ)�”}”(hX"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::”h]”hX!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:”…”�”}”(hj9h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´Kkhj5ubj)�”}”(hŒLp = rcu_dereference(gp); if (p == &default_struct) do_default(p->a);”h]”hŒLp = rcu_dereference(gp); if (p == &default_struct) do_default(p->a);”…”�”}”hjGsbah}”(h]”h ]”h"]”h$]”h&]”jj uh1jh³hÃh´Kqhj5ubhÚ)�”}”(hŒ¤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::”h]”hŒ«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:”…”�”}”(hjUh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´Kuhj5ubj)�”}”(hŒXp = rcu_dereference(gp); if (p == &default_struct) do_default(default_struct.a);”h]”hŒXp = rcu_dereference(gp); if (p == &default_struct) do_default(default_struct.a);”…”�”}”hjcsbah}”(h]”h ]”h"]”h$]”h&]”jj uh1jh³hÃh´Kyhj5ubhÚ)�”}”(hŒ¹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.”h]”hŒ½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.”…”�”}”(hjqh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´K}hj5ubhÚ)�”}”(hŒ3However, comparisons are OK in the following cases:”h]”hŒ3However, comparisons are OK in the following cases:”…”�”}”(hjh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´K�hj5ubj)�”}”(hhh]”(j )�”}”(hX'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. ”h]”hÚ)�”}”(hX&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.”h]”hX&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.”…”�”}”(hj”h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´Kƒhj�ubah}”(h]”h ]”h"]”h$]”h&]”uh1j hj�ubj )�”}”(hXõ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(). ”h]”(hÚ)�”}”(hX5The 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.”h]”hX5The 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.”…”�”}”(hj¬h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´KŠhj¨ubhÚ)�”}”(hX^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.”h]”hX^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.”…”�”}”(hjºh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´K‘hj¨ubhÚ)�”}”(hŒ]Within an RCU read-side critical section, there is little reason to use rcu_access_pointer().”h]”hŒ]Within an RCU read-side critical section, there is little reason to use rcu_access_pointer().”…”�”}”(hjÈh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´K™hj¨ubeh}”(h]”h ]”h"]”h$]”h&]”uh1j hj�ubj )�”}”(hXÍ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. ”h]”(hÚ)�”}”(hX5The 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:”h]”hX=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:”…”�”}”(hjàh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´KœhjÜubj)�”}”(hhh]”(j )�”}”(hŒCompile time. ”h]”hÚ)�”}”(hŒ Compile time.”h]”hŒ Compile time.”…”�”}”(hjõh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´K£hjñubah}”(h]”h ]”h"]”h$]”h&]”uh1j hjîubj )�”}”(hŒ Boot time. ”h]”hÚ)�”}”(hŒ Boot time.”h]”hŒ Boot time.”…”�”}”(hj h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´K¥hj ubah}”(h]”h ]”h"]”h$]”h&]”uh1j hjîubj )�”}”(hŒ"Module-init time for module code. ”h]”hÚ)�”}”(hŒ!Module-init time for module code.”h]”hŒ!Module-init time for module code.”…”�”}”(hj%h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´K§hj!ubah}”(h]”h ]”h"]”h$]”h&]”uh1j hjîubj )�”}”(hŒ,Prior to kthread creation for kthread code. ”h]”hÚ)�”}”(hŒ+Prior to kthread creation for kthread code.”h]”hŒ+Prior to kthread creation for kthread code.”…”�”}”(hj=h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´K©hj9ubah}”(h]”h ]”h"]”h$]”h&]”uh1j hjîubj )�”}”(hŒ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); }”h]”hX&struct foo { int a; int b; int c; }; struct foo *gp1; struct foo *gp2; void updater(void) { struct foo *p; p = kmalloc_obj(*p); 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); }”…”�”}”hjhsbah}”(h]”h ]”h"]”h$]”h&]”jj uh1jh³hÃh´KãhjIh²hubhÚ)�”}”(hXdYou 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.”h]”hXlYou 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.”…”�”}”(hjvh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´MhjIh²hubhÚ)�”}”(hŒ4But suppose that the reader needs a consistent view?”h]”hŒ4But suppose that the reader needs a consistent view?”…”�”}”(hj„h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´MhjIh²hubhÚ)�”}”(hŒ>Then one approach is to use locking, for example, as follows::”h]”hŒ=Then one approach is to use locking, for example, as follows:”…”�”}”(hj’h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´MhjIh²hubj)�”}”(hXstruct foo { int a; int b; int c; spinlock_t lock; }; struct foo *gp1; struct foo *gp2; void updater(void) { struct foo *p; p = kmalloc_obj(*p); 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); }”h]”hXstruct foo { int a; int b; int c; spinlock_t lock; }; struct foo *gp1; struct foo *gp2; void updater(void) { struct foo *p; p = kmalloc_obj(*p); 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); }”…”�”}”hj sbah}”(h]”h ]”h"]”h$]”h&]”jj uh1jh³hÃh´MhjIh²hubhÚ)�”}”(hŒ*As always, use the right tool for the job!”h]”hŒ*As always, use the right tool for the job!”…”�”}”(hj®h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´MRhjIh²hubeh}”(h]”Œ"example-of-amplified-rcu-usage-bug”ah ]”h"]”Œ"example of amplified rcu-usage bug”ah$]”h&]”uh1hÄhhÆh²hh³hÃh´KÜubhÅ)�”}”(hhh]”(hÊ)�”}”(hŒ)EXAMPLE WHERE THE COMPILER KNOWS TOO MUCH”h]”hŒ)EXAMPLE WHERE THE COMPILER KNOWS TOO MUCH”…”�”}”(hjÇh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÉhjÄh²hh³hÃh´MVubhÚ)�”}”(hXŸ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.”h]”hXŸ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.”…”�”}”(hjÕh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´MXhjÄh²hubhÚ)�”}”(hŒuBut without rcu_dereference(), the compiler knows more than you might expect. Consider the following code fragment::”h]”hŒtBut without rcu_dereference(), the compiler knows more than you might expect. Consider the following code fragment:”…”�”}”(hjãh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´M_hjÄh²hubj)�”}”(hXystruct 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. */ }”h]”hXystruct 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. */ }”…”�”}”hjñsbah}”(h]”h ]”h"]”h$]”h&]”jj uh1jh³hÃh´MbhjÄh²hubhÚ)�”}”(hXBecause 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.”h]”hX5Because 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.”…”�”}”(hjÿh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´M€hjÄh²hubhÚ)�”}”(hŒfIn short, rcu_dereference() is *not* optional when you are going to dereference the resulting pointer.”h]”(hŒIn short, rcu_dereference() is ”…”�”}”(hj h²hh³Nh´Nubjl)�”}”(hŒ*not*”h]”hŒnot”…”�”}”(hjh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jkhj ubhŒB optional when you are going to dereference the resulting pointer.”…”�”}”(hj h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´M‰hjÄh²hubeh}”(h]”Œ)example-where-the-compiler-knows-too-much”ah ]”h"]”Œ)example where the compiler knows too much”ah$]”h&]”uh1hÄhhÆh²hh³hÃh´MVubhÅ)�”}”(hhh]”(hÊ)�”}”(hŒrcu_protected_pointer, lockdep_is_held(&my_lock)); ”h]”(hÚ)�”}”(hŒ§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::”h]”hŒ¦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:”…”�”}”(hjuh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´M hjqubj)�”}”(hŒkp1 = rcu_dereference_check(p->rcu_protected_pointer, lockdep_is_held(&my_lock));”h]”hŒkp1 = rcu_dereference_check(p->rcu_protected_pointer, lockdep_is_held(&my_lock));”…”�”}”hjƒsbah}”(h]”h ]”h"]”h$]”h&]”jj uh1jh³hÃh´M¤hjqubeh}”(h]”h ]”h"]”h$]”h&]”uh1j hjVh²hh³hÃh´Nubj )�”}”(hX{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)); ”h]”(hÚ)�”}”(hŒ»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::”h]”hŒº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:”…”�”}”(hj›h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´M¨hj—ubj)�”}”(hŒ¥p1 = rcu_dereference_check(p->rcu_protected_pointer, lockdep_is_held(&my_lock) || lockdep_is_held(&your_lock));”h]”hŒ¥p1 = rcu_dereference_check(p->rcu_protected_pointer, lockdep_is_held(&my_lock) || lockdep_is_held(&your_lock));”…”�”}”hj©sbah}”(h]”h ]”h"]”h$]”h&]”jj uh1jh³hÃh´M¬hj—ubeh}”(h]”h ]”h"]”h$]”h&]”uh1j hjVh²hh³hÃh´Nubj )�”}”(hXrIf 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. ”h]”(hÚ)�”}”(hŒqIf the access is on the update side, so that it is always protected by my_lock, use rcu_dereference_protected()::”h]”hŒpIf the access is on the update side, so that it is always protected by my_lock, use rcu_dereference_protected():”…”�”}”(hjÁh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´M°hj½ubj)�”}”(hŒsp1 = rcu_dereference_protected(p->rcu_protected_pointer, lockdep_is_held(&my_lock));”h]”hŒsp1 = rcu_dereference_protected(p->rcu_protected_pointer, lockdep_is_held(&my_lock));”…”�”}”hjÏsbah}”(h]”h ]”h"]”h$]”h&]”jj uh1jh³hÃh´M³hj½ubhÚ)�”}”(hŒyThis can be extended to handle multiple locks as in #3 above, and both can be extended to check other conditions as well.”h]”hŒyThis can be extended to handle multiple locks as in #3 above, and both can be extended to check other conditions as well.”…”�”}”(hjÝh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´M¶hj½ubeh}”(h]”h ]”h"]”h$]”h&]”uh1j hjVh²hh³hÃh´Nubj )�”}”(hXIf 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). ”h]”(hÚ)�”}”(hX'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.”h]”hX'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.”…”�”}”(hjõh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´M¹hjñubhÚ)�”}”(hŒì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).”h]”hŒì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).”…”�”}”(hjh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´MÃhjñubeh}”(h]”h ]”h"]”h$]”h&]”uh1j hjVh²hh³hÃh´Nubeh}”(h]”h ]”h"]”h$]”h&]”Œenumtype”Œarabic”Œprefix”hŒsuffix”Œ.”uh1jThj5h²hh³hÃh´M–ubeh}”(h]”Œ9which-member-of-the-rcu-dereference-family-should-you-use”ah ]”h"]”Œrcu_protected_pointer; do_something_with(p->a); do_something_else_with(p->b);”h]”hŒTp = q->rcu_protected_pointer; do_something_with(p->a); do_something_else_with(p->b);”…”�”}”hjIsbah}”(h]”h ]”h"]”h$]”h&]”jj uh1jh³hÃh´MÑhj*h²hubhÚ)�”}”(hŒ~If register pressure is high, the compiler might optimize "p" out of existence, transforming the code to something like this::”h]”hŒ�If register pressure is high, the compiler might optimize “pâ€� out of existence, transforming the code to something like this:”…”�”}”(hjWh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´MÕhj*h²hubj)�”}”(hŒddo_something_with(q->rcu_protected_pointer->a); do_something_else_with(q->rcu_protected_pointer->b);”h]”hŒddo_something_with(q->rcu_protected_pointer->a); do_something_else_with(q->rcu_protected_pointer->b);”…”�”}”hjesbah}”(h]”h ]”h"]”h$]”h&]”jj uh1jh³hÃh´MØhj*h²hubhÚ)�”}”(hXThis 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.”h]”hXThis 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.”…”�”}”(hjsh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´MÛhj*h²hubhÚ)�”}”(hŒƒLoad tearing could of course result in dereferencing a mashup of a pair of pointers, which also might fatally disappoint your code.”h]”hŒƒLoad tearing could of course result in dereferencing a mashup of a pair of pointers, which also might fatally disappoint your code.”…”�”}”(hj�h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´Màhj*h²hubhÚ)�”}”(hŒZThese problems could have been avoided simply by making the code instead read as follows::”h]”hŒYThese problems could have been avoided simply by making the code instead read as follows:”…”�”}”(hj�h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´Mãhj*h²hubj)�”}”(hŒep = rcu_dereference(q->rcu_protected_pointer); do_something_with(p->a); do_something_else_with(p->b);”h]”hŒep = rcu_dereference(q->rcu_protected_pointer); do_something_with(p->a); do_something_else_with(p->b);”…”�”}”hj�sbah}”(h]”h ]”h"]”h$]”h&]”jj uh1jh³hÃh´Mæhj*h²hubhÚ)�”}”(hXUnfortunately, 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::”h]”hXUnfortunately, 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:”…”�”}”(hj«h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´Mêhj*h²hubj)�”}”(hŒ(struct foo __rcu *rcu_protected_pointer;”h]”hŒ(struct foo __rcu *rcu_protected_pointer;”…”�”}”hj¹sbah}”(h]”h ]”h"]”h$]”h&]”jj uh1jh³hÃh´Móhj*h²hubhÚ)�”}”(hŒcUse of "__rcu" is opt-in. If you choose not to use it, then you should ignore the sparse warnings.”h]”hŒgUse of “__rcuâ€� is opt-in. If you choose not to use it, then you should ignore the sparse warnings.”…”�”}”(hjÇh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´Mõhj*h²hubeh}”(h]”Œ)sparse-checking-of-rcu-protected-pointers”ah ]”h"]”Œ)sparse checking of rcu-protected pointers”ah$]”h&]”uh1hÄhhÆh²hh³hÃh´MÊubeh}”(h]”(Œ=proper-care-and-feeding-of-return-values-from-rcu-dereference”hÂeh ]”h"]”(Œ?proper care and feeding of return values from rcu_dereference()”Œrcu_dereference_doc”eh$]”h&]”uh1hÄhhh²hh³hÃh´KŒexpect_referenced_by_name”}”jãh·sŒexpect_referenced_by_id”}”hÂh·subeh}”(h]”h ]”h"]”h$]”h&]”Œsource”hÃuh1hŒcurrent_source”NŒ current_line”NŒsettings”Œdocutils.frontend”ŒValues”“”)�”}”(hÉNŒ generator”NŒ datestamp”NŒ source_link”NŒ source_url”NŒ toc_backlinks”Œentry”Œfootnote_backlinks”KŒ sectnum_xform”KŒstrip_comments”NŒstrip_elements_with_classes”NŒ strip_classes”NŒ report_level”KŒ halt_level”KŒexit_status_level”KŒdebug”NŒwarning_stream”NŒ traceback”ˆŒinput_encoding”Œ utf-8-sig”Œinput_encoding_error_handler”Œstrict”Œoutput_encoding”•BŒutf-8”Œoutput_encoding_error_handler”j Œerror_encoding”Œutf-8”Œerror_encoding_error_handler”Œbackslashreplace”Œ language_code”Œen”Œrecord_dependencies”NŒconfig”NŒ id_prefix”hŒauto_id_prefix”Œid”Œ dump_settings”NŒdump_internals”NŒdump_transforms”NŒdump_pseudo_xml”NŒexpose_internals”NŒstrict_visitor”NŒ_disable_config”NŒ_source”hÃŒ _destination”NŒ _config_files”]”Œ7/var/lib/git/docbuild/linux/Documentation/docutils.conf”aŒfile_insertion_enabled”ˆŒ raw_enabled”KŒline_length_limit”M'Œpep_references”NŒ pep_base_url”Œhttps://peps.python.org/”Œpep_file_url_template”Œpep-%04d”Œrfc_references”NŒ rfc_base_url”Œ&https://datatracker.ietf.org/doc/html/”Œ tab_width”KŒtrim_footnote_reference_space”‰Œsyntax_highlight”Œlong”Œ smart_quotes”ˆŒsmartquotes_locales”]”Œcharacter_level_inline_markup”‰Œdoctitle_xform”‰Œ docinfo_xform”KŒsectsubtitle_xform”‰Œ image_loading”Œlink”Œembed_stylesheet”‰Œcloak_email_addresses”ˆŒsection_self_link”‰Œenv”NubŒreporter”NŒindirect_targets”]”Œsubstitution_defs”}”Œsubstitution_names”}”Œrefnames”}”Œrefids”}”hÂ]”h·asŒnameids”}”(jãhÂjâjßjÁj¾j2j/j'j$jÚj×uŒ nametypes”}”(jãˆjâ‰jÁ‰j2‰j'‰jÚ‰uh}”(hÂhÆjßhÆj¾jIj/jÄj$j5j×j*uŒ footnote_refs”}”Œ citation_refs”}”Œ autofootnotes”]”Œautofootnote_refs”]”Œsymbol_footnotes”]”Œsymbol_footnote_refs”]”Œ footnotes”]”Œ citations”]”Œautofootnote_start”KŒsymbol_footnote_start”KŒ id_counter”Œ collections”ŒCounter”“”}”…”R”Œparse_messages”]”Œtransform_messages”]”hŒsystem_message”“”)�”}”(hhh]”hÚ)�”}”(hhh]”hŒ9Hyperlink target "rcu-dereference-doc" is not referenced.”…”�”}”hjwsbah}”(h]”h ]”h"]”h$]”h&]”uh1hÙhjtubah}”(h]”h ]”h"]”h$]”h&]”Œlevel”KŒtype”ŒINFO”Œsource”hÃŒline”Kuh1jrubaŒ transformer”NŒ include_log”]”Œ decoration”Nh²hub.