···3232 for lockless updates. This does result in the mildly3333 counter-intuitive situation where rcu_read_lock() and3434 rcu_read_unlock() are used to protect updates, however, this3535- approach provides the same potential simplifications that garbage3636- collectors do.3535+ approach can provide the same simplifications to certain types3636+ of lockless algorithms that garbage collectors do.373738381. Does the update code have proper mutual exclusion?3939···4949 them -- even x86 allows later loads to be reordered to precede5050 earlier stores), and be prepared to explain why this added5151 complexity is worthwhile. If you choose #c, be prepared to5252- explain how this single task does not become a major bottleneck on5353- big multiprocessor machines (for example, if the task is updating5454- information relating to itself that other tasks can read, there5555- by definition can be no bottleneck). Note that the definition5656- of "large" has changed significantly: Eight CPUs was "large"5757- in the year 2000, but a hundred CPUs was unremarkable in 2017.5252+ explain how this single task does not become a major bottleneck5353+ on large systems (for example, if the task is updating information5454+ relating to itself that other tasks can read, there by definition5555+ can be no bottleneck). Note that the definition of "large" has5656+ changed significantly: Eight CPUs was "large" in the year 2000,5757+ but a hundred CPUs was unremarkable in 2017.585859592. Do the RCU read-side critical sections make proper use of6060 rcu_read_lock() and friends? These primitives are needed···97979898 b. Proceed as in (a) above, but also maintain per-element9999 locks (that are acquired by both readers and writers)100100- that guard per-element state. Of course, fields that101101- the readers refrain from accessing can be guarded by102102- some other lock acquired only by updaters, if desired.100100+ that guard per-element state. Fields that the readers101101+ refrain from accessing can be guarded by some other lock102102+ acquired only by updaters, if desired.103103104104- This works quite well, also.104104+ This also works quite well.105105106106 c. Make updates appear atomic to readers. For example,107107 pointer updates to properly aligned fields will108108 appear atomic, as will individual atomic primitives.109109 Sequences of operations performed under a lock will *not*110110 appear to be atomic to RCU readers, nor will sequences111111- of multiple atomic primitives.111111+ of multiple atomic primitives. One alternative is to112112+ move multiple individual fields to a separate structure,113113+ thus solving the multiple-field problem by imposing an114114+ additional level of indirection.112115113116 This can work, but is starting to get a bit tricky.114117115115- d. Carefully order the updates and the reads so that116116- readers see valid data at all phases of the update.117117- This is often more difficult than it sounds, especially118118- given modern CPUs' tendency to reorder memory references.119119- One must usually liberally sprinkle memory barriers120120- (smp_wmb(), smp_rmb(), smp_mb()) through the code,121121- making it difficult to understand and to test.118118+ d. Carefully order the updates and the reads so that readers119119+ see valid data at all phases of the update. This is often120120+ more difficult than it sounds, especially given modern121121+ CPUs' tendency to reorder memory references. One must122122+ usually liberally sprinkle memory-ordering operations123123+ through the code, making it difficult to understand and124124+ to test. Where it works, it is better to use things125125+ like smp_store_release() and smp_load_acquire(), but in126126+ some cases the smp_mb() full memory barrier is required.122127123123- It is usually better to group the changing data into124124- a separate structure, so that the change may be made125125- to appear atomic by updating a pointer to reference126126- a new structure containing updated values.128128+ As noted earlier, it is usually better to group the129129+ changing data into a separate structure, so that the130130+ change may be made to appear atomic by updating a pointer131131+ to reference a new structure containing updated values.1271321281334. Weakly ordered CPUs pose special challenges. Almost all CPUs129134 are weakly ordered -- even x86 CPUs allow later loads to be···193188 when publicizing a pointer to a structure that can194189 be traversed by an RCU read-side critical section.195190196196-5. If call_rcu() or call_srcu() is used, the callback function will197197- be called from softirq context. In particular, it cannot block.198198- If you need the callback to block, run that code in a workqueue199199- handler scheduled from the callback. The queue_rcu_work()200200- function does this for you in the case of call_rcu().191191+5. If any of call_rcu(), call_srcu(), call_rcu_tasks(),192192+ call_rcu_tasks_rude(), or call_rcu_tasks_trace() is used,193193+ the callback function may be invoked from softirq context,194194+ and in any case with bottom halves disabled. In particular,195195+ this callback function cannot block. If you need the callback196196+ to block, run that code in a workqueue handler scheduled from197197+ the callback. The queue_rcu_work() function does this for you198198+ in the case of call_rcu().2011992022006. Since synchronize_rcu() can block, it cannot be called203201 from any sort of irq context. The same rule applies204204- for synchronize_srcu(), synchronize_rcu_expedited(), and205205- synchronize_srcu_expedited().202202+ for synchronize_srcu(), synchronize_rcu_expedited(),203203+ synchronize_srcu_expedited(), synchronize_rcu_tasks(),204204+ synchronize_rcu_tasks_rude(), and synchronize_rcu_tasks_trace().206205207206 The expedited forms of these primitives have the same semantics208208- as the non-expedited forms, but expediting is both expensive and209209- (with the exception of synchronize_srcu_expedited()) unfriendly210210- to real-time workloads. Use of the expedited primitives should211211- be restricted to rare configuration-change operations that would212212- not normally be undertaken while a real-time workload is running.213213- However, real-time workloads can use rcupdate.rcu_normal kernel214214- boot parameter to completely disable expedited grace periods,215215- though this might have performance implications.207207+ as the non-expedited forms, but expediting is more CPU intensive.208208+ Use of the expedited primitives should be restricted to rare209209+ configuration-change operations that would not normally be210210+ undertaken while a real-time workload is running. Note that211211+ IPI-sensitive real-time workloads can use the rcupdate.rcu_normal212212+ kernel boot parameter to completely disable expedited grace213213+ periods, though this might have performance implications.216214217215 In particular, if you find yourself invoking one of the expedited218216 primitives repeatedly in a loop, please do everyone a favor:···223215 a single non-expedited primitive to cover the entire batch.224216 This will very likely be faster than the loop containing the225217 expedited primitive, and will be much much easier on the rest226226- of the system, especially to real-time workloads running on227227- the rest of the system.218218+ of the system, especially to real-time workloads running on the219219+ rest of the system. Alternatively, instead use asynchronous220220+ primitives such as call_rcu().2282212292227. As of v4.20, a given kernel implements only one RCU flavor, which230223 is RCU-sched for PREEMPTION=n and RCU-preempt for PREEMPTION=y.···248239 the corresponding readers must use rcu_read_lock_trace() and249240 rcu_read_unlock_trace(). If an updater uses call_rcu_tasks_rude()250241 or synchronize_rcu_tasks_rude(), then the corresponding readers251251- must use anything that disables interrupts.242242+ must use anything that disables preemption, for example,243243+ preempt_disable() and preempt_enable().252244253245 Mixing things up will result in confusion and broken kernels, and254246 has even resulted in an exploitable security issue. Therefore,···263253 that this usage is safe is that readers can use anything that264254 disables BH when updaters use call_rcu() or synchronize_rcu().265255266266-8. Although synchronize_rcu() is slower than is call_rcu(), it267267- usually results in simpler code. So, unless update performance is268268- critically important, the updaters cannot block, or the latency of269269- synchronize_rcu() is visible from userspace, synchronize_rcu()270270- should be used in preference to call_rcu(). Furthermore,271271- kfree_rcu() usually results in even simpler code than does272272- synchronize_rcu() without synchronize_rcu()'s multi-millisecond273273- latency. So please take advantage of kfree_rcu()'s "fire and274274- forget" memory-freeing capabilities where it applies.256256+8. Although synchronize_rcu() is slower than is call_rcu(),257257+ it usually results in simpler code. So, unless update258258+ performance is critically important, the updaters cannot block,259259+ or the latency of synchronize_rcu() is visible from userspace,260260+ synchronize_rcu() should be used in preference to call_rcu().261261+ Furthermore, kfree_rcu() and kvfree_rcu() usually result262262+ in even simpler code than does synchronize_rcu() without263263+ synchronize_rcu()'s multi-millisecond latency. So please take264264+ advantage of kfree_rcu()'s and kvfree_rcu()'s "fire and forget"265265+ memory-freeing capabilities where it applies.275266276267 An especially important property of the synchronize_rcu()277268 primitive is that it automatically self-limits: if grace periods···282271 cases where grace periods are delayed, as failing to do so can283272 result in excessive realtime latencies or even OOM conditions.284273285285- Ways of gaining this self-limiting property when using call_rcu()286286- include:274274+ Ways of gaining this self-limiting property when using call_rcu(),275275+ kfree_rcu(), or kvfree_rcu() include:287276288277 a. Keeping a count of the number of data-structure elements289278 used by the RCU-protected data structure, including···315304 here is that superuser already has lots of ways to crash316305 the machine.317306318318- d. Periodically invoke synchronize_rcu(), permitting a limited319319- number of updates per grace period. Better yet, periodically320320- invoke rcu_barrier() to wait for all outstanding callbacks.307307+ d. Periodically invoke rcu_barrier(), permitting a limited308308+ number of updates per grace period.321309322322- The same cautions apply to call_srcu() and kfree_rcu().310310+ The same cautions apply to call_srcu(), call_rcu_tasks(),311311+ call_rcu_tasks_rude(), and call_rcu_tasks_trace(). This is312312+ why there is an srcu_barrier(), rcu_barrier_tasks(),313313+ rcu_barrier_tasks_rude(), and rcu_barrier_tasks_rude(),314314+ respectively.323315324324- Note that although these primitives do take action to avoid memory325325- exhaustion when any given CPU has too many callbacks, a determined326326- user could still exhaust memory. This is especially the case327327- if a system with a large number of CPUs has been configured to328328- offload all of its RCU callbacks onto a single CPU, or if the329329- system has relatively little free memory.316316+ Note that although these primitives do take action to avoid317317+ memory exhaustion when any given CPU has too many callbacks,318318+ a determined user or administrator can still exhaust memory.319319+ This is especially the case if a system with a large number of320320+ CPUs has been configured to offload all of its RCU callbacks onto321321+ a single CPU, or if the system has relatively little free memory.3303223313239. All RCU list-traversal primitives, which include332324 rcu_dereference(), list_for_each_entry_rcu(), and···358344 and you don't hold the appropriate update-side lock, you *must*359345 use the "_rcu()" variants of the list macros. Failing to do so360346 will break Alpha, cause aggressive compilers to generate bad code,361361- and confuse people trying to read your code.347347+ and confuse people trying to understand your code.36234836334911. Any lock acquired by an RCU callback must be acquired elsewhere364364- with softirq disabled, e.g., via spin_lock_irqsave(),365365- spin_lock_bh(), etc. Failing to disable softirq on a given366366- acquisition of that lock will result in deadlock as soon as367367- the RCU softirq handler happens to run your RCU callback while368368- interrupting that acquisition's critical section.350350+ with softirq disabled, e.g., via spin_lock_bh(). Failing to351351+ disable softirq on a given acquisition of that lock will result352352+ in deadlock as soon as the RCU softirq handler happens to run353353+ your RCU callback while interrupting that acquisition's critical354354+ section.36935537035612. RCU callbacks can be and are executed in parallel. In many cases,371357 the callback code simply wrappers around kfree(), so that this···386372 for some real-time workloads, this is the whole point of using387373 the rcu_nocbs= kernel boot parameter.388374389389-13. Unlike other forms of RCU, it *is* permissible to block in an375375+ In addition, do not assume that callbacks queued in a given order376376+ will be invoked in that order, even if they all are queued on the377377+ same CPU. Furthermore, do not assume that same-CPU callbacks will378378+ be invoked serially. For example, in recent kernels, CPUs can be379379+ switched between offloaded and de-offloaded callback invocation,380380+ and while a given CPU is undergoing such a switch, its callbacks381381+ might be concurrently invoked by that CPU's softirq handler and382382+ that CPU's rcuo kthread. At such times, that CPU's callbacks383383+ might be executed both concurrently and out of order.384384+385385+13. Unlike most flavors of RCU, it *is* permissible to block in an390386 SRCU read-side critical section (demarked by srcu_read_lock()391387 and srcu_read_unlock()), hence the "SRCU": "sleepable RCU".392388 Please note that if you don't need to sleep in read-side critical···436412 never sends IPIs to other CPUs, so it is easier on437413 real-time workloads than is synchronize_rcu_expedited().438414415415+ It is also permissible to sleep in RCU Tasks Trace read-side416416+ critical, which are delimited by rcu_read_lock_trace() and417417+ rcu_read_unlock_trace(). However, this is a specialized flavor418418+ of RCU, and you should not use it without first checking with419419+ its current users. In most cases, you should instead use SRCU.420420+439421 Note that rcu_assign_pointer() relates to SRCU just as it does to440422 other forms of RCU, but instead of rcu_dereference() you should441423 use srcu_dereference() in order to avoid lockdep splats.···472442 find problems as follows:473443474444 CONFIG_PROVE_LOCKING:475475- check that accesses to RCU-protected data476476- structures are carried out under the proper RCU477477- read-side critical section, while holding the right478478- combination of locks, or whatever other conditions479479- are appropriate.445445+ check that accesses to RCU-protected data structures446446+ are carried out under the proper RCU read-side critical447447+ section, while holding the right combination of locks,448448+ or whatever other conditions are appropriate.480449481450 CONFIG_DEBUG_OBJECTS_RCU_HEAD:482482- check that you don't pass the483483- same object to call_rcu() (or friends) before an RCU484484- grace period has elapsed since the last time that you485485- passed that same object to call_rcu() (or friends).451451+ check that you don't pass the same object to call_rcu()452452+ (or friends) before an RCU grace period has elapsed453453+ since the last time that you passed that same object to454454+ call_rcu() (or friends).486455487456 __rcu sparse checks:488488- tag the pointer to the RCU-protected data489489- structure with __rcu, and sparse will warn you if you490490- access that pointer without the services of one of the491491- variants of rcu_dereference().457457+ tag the pointer to the RCU-protected data structure458458+ with __rcu, and sparse will warn you if you access that459459+ pointer without the services of one of the variants460460+ of rcu_dereference().492461493462 These debugging aids can help you find problems that are494463 otherwise extremely difficult to spot.495464496496-17. If you register a callback using call_rcu() or call_srcu(), and497497- pass in a function defined within a loadable module, then it in498498- necessary to wait for all pending callbacks to be invoked after499499- the last invocation and before unloading that module. Note that500500- it is absolutely *not* sufficient to wait for a grace period!501501- The current (say) synchronize_rcu() implementation is *not*502502- guaranteed to wait for callbacks registered on other CPUs.503503- Or even on the current CPU if that CPU recently went offline504504- and came back online.465465+17. If you pass a callback function defined within a module to one of466466+ call_rcu(), call_srcu(), call_rcu_tasks(), call_rcu_tasks_rude(),467467+ or call_rcu_tasks_trace(), then it is necessary to wait for all468468+ pending callbacks to be invoked before unloading that module.469469+ Note that it is absolutely *not* sufficient to wait for a grace470470+ period! For example, synchronize_rcu() implementation is *not*471471+ guaranteed to wait for callbacks registered on other CPUs via472472+ call_rcu(). Or even on the current CPU if that CPU recently473473+ went offline and came back online.505474506475 You instead need to use one of the barrier functions:507476508477 - call_rcu() -> rcu_barrier()509478 - call_srcu() -> srcu_barrier()479479+ - call_rcu_tasks() -> rcu_barrier_tasks()480480+ - call_rcu_tasks_rude() -> rcu_barrier_tasks_rude()481481+ - call_rcu_tasks_trace() -> rcu_barrier_tasks_trace()510482511483 However, these barrier functions are absolutely *not* guaranteed512512- to wait for a grace period. In fact, if there are no call_rcu()513513- callbacks waiting anywhere in the system, rcu_barrier() is within514514- its rights to return immediately.484484+ to wait for a grace period. For example, if there are no485485+ call_rcu() callbacks queued anywhere in the system, rcu_barrier()486486+ can and will return immediately.515487516516- So if you need to wait for both an RCU grace period and for517517- all pre-existing call_rcu() callbacks, you will need to execute518518- both rcu_barrier() and synchronize_rcu(), if necessary, using519519- something like workqueues to execute them concurrently.488488+ So if you need to wait for both a grace period and for all489489+ pre-existing callbacks, you will need to invoke both functions,490490+ with the pair depending on the flavor of RCU:491491+492492+ - Either synchronize_rcu() or synchronize_rcu_expedited(),493493+ together with rcu_barrier()494494+ - Either synchronize_srcu() or synchronize_srcu_expedited(),495495+ together with and srcu_barrier()496496+ - synchronize_rcu_tasks() and rcu_barrier_tasks()497497+ - synchronize_tasks_rude() and rcu_barrier_tasks_rude()498498+ - synchronize_tasks_trace() and rcu_barrier_tasks_trace()499499+500500+ If necessary, you can use something like workqueues to execute501501+ the requisite pair of functions concurrently.520502521503 See rcubarrier.rst for more information.