···5566[Originally published in LWN Jan. 14, 2007: http://lwn.net/Articles/217484/]7788-RCU (read-copy update) is a synchronization mechanism that can be thought99-of as a replacement for read-writer locking (among other things), but with1010-very low-overhead readers that are immune to deadlock, priority inversion,1111-and unbounded latency. RCU read-side critical sections are delimited1212-by rcu_read_lock() and rcu_read_unlock(), which, in non-CONFIG_PREEMPTION1313-kernels, generate no code whatsoever.1414-1515-This means that RCU writers are unaware of the presence of concurrent1616-readers, so that RCU updates to shared data must be undertaken quite1717-carefully, leaving an old version of the data structure in place until all1818-pre-existing readers have finished. These old versions are needed because1919-such readers might hold a reference to them. RCU updates can therefore be2020-rather expensive, and RCU is thus best suited for read-mostly situations.2121-2222-How can an RCU writer possibly determine when all readers are finished,2323-given that readers might well leave absolutely no trace of their2424-presence? There is a synchronize_rcu() primitive that blocks until all2525-pre-existing readers have completed. An updater wishing to delete an2626-element p from a linked list might do the following, while holding an2727-appropriate lock, of course::2828-2929- list_del_rcu(p);3030- synchronize_rcu();3131- kfree(p);3232-3333-But the above code cannot be used in IRQ context -- the call_rcu()3434-primitive must be used instead. This primitive takes a pointer to an3535-rcu_head struct placed within the RCU-protected data structure and3636-another pointer to a function that may be invoked later to free that3737-structure. Code to delete an element p from the linked list from IRQ3838-context might then be as follows::88+RCU updaters sometimes use call_rcu() to initiate an asynchronous wait for99+a grace period to elapse. This primitive takes a pointer to an rcu_head1010+struct placed within the RCU-protected data structure and another pointer1111+to a function that may be invoked later to free that structure. Code to1212+delete an element p from the linked list from IRQ context might then be1313+as follows::39144015 list_del_rcu(p);4116 call_rcu(&p->rcu, p_callback);···2954Unloading Modules That Use call_rcu()3055-------------------------------------31563232-But what if p_callback is defined in an unloadable module?5757+But what if the p_callback() function is defined in an unloadable module?33583459If we unload the module while some RCU callbacks are pending,3560the CPUs executing these callbacks are going to be severely···42674368One might be tempted to try several back-to-back synchronize_rcu()4469calls, but this is still not guaranteed to work. If there is a very4545-heavy RCU-callback load, then some of the callbacks might be deferred4646-in order to allow other processing to proceed. Such deferral is required4747-in realtime kernels in order to avoid excessive scheduling latencies.7070+heavy RCU-callback load, then some of the callbacks might be deferred in7171+order to allow other processing to proceed. For but one example, such7272+deferral is required in realtime kernels in order to avoid excessive7373+scheduling latencies.487449755076rcu_barrier()5177-------------52785353-We instead need the rcu_barrier() primitive. Rather than waiting for5454-a grace period to elapse, rcu_barrier() waits for all outstanding RCU5555-callbacks to complete. Please note that rcu_barrier() does **not** imply5656-synchronize_rcu(), in particular, if there are no RCU callbacks queued5757-anywhere, rcu_barrier() is within its rights to return immediately,5858-without waiting for a grace period to elapse.7979+This situation can be handled by the rcu_barrier() primitive. Rather8080+than waiting for a grace period to elapse, rcu_barrier() waits for all8181+outstanding RCU callbacks to complete. Please note that rcu_barrier()8282+does **not** imply synchronize_rcu(), in particular, if there are no RCU8383+callbacks queued anywhere, rcu_barrier() is within its rights to return8484+immediately, without waiting for anything, let alone a grace period.59856086Pseudo-code using rcu_barrier() is as follows:6187···6589 3. Allow the module to be unloaded.66906791There is also an srcu_barrier() function for SRCU, and you of course6868-must match the flavor of rcu_barrier() with that of call_rcu(). If your6969-module uses multiple flavors of call_rcu(), then it must also use multiple7070-flavors of rcu_barrier() when unloading that module. For example, if7171-it uses call_rcu(), call_srcu() on srcu_struct_1, and call_srcu() on7272-srcu_struct_2, then the following three lines of code will be required7373-when unloading::9292+must match the flavor of srcu_barrier() with that of call_srcu().9393+If your module uses multiple srcu_struct structures, then it must also9494+use multiple invocations of srcu_barrier() when unloading that module.9595+For example, if it uses call_rcu(), call_srcu() on srcu_struct_1, and9696+call_srcu() on srcu_struct_2, then the following three lines of code9797+will be required when unloading::74987599 1 rcu_barrier();76100 2 srcu_barrier(&srcu_struct_1);77101 3 srcu_barrier(&srcu_struct_2);781027979-The rcutorture module makes use of rcu_barrier() in its exit function8080-as follows::103103+If latency is of the essence, workqueues could be used to run these104104+three functions concurrently.105105+106106+An ancient version of the rcutorture module makes use of rcu_barrier()107107+in its exit function as follows::8110882109 1 static void83110 2 rcu_torture_cleanup(void)···169190:ref:`Answer to Quick Quiz #1 <answer_rcubarrier_quiz_1>`170191171192Your module might have additional complications. For example, if your172172-module invokes call_rcu() from timers, you will need to first cancel all173173-the timers, and only then invoke rcu_barrier() to wait for any remaining193193+module invokes call_rcu() from timers, you will need to first refrain194194+from posting new timers, cancel (or wait for) all the already-posted195195+timers, and only then invoke rcu_barrier() to wait for any remaining174196RCU callbacks to complete.175197176176-Of course, if you module uses call_rcu(), you will need to invoke198198+Of course, if your module uses call_rcu(), you will need to invoke177199rcu_barrier() before unloading. Similarly, if your module uses178200call_srcu(), you will need to invoke srcu_barrier() before unloading,179201and on the same srcu_struct structure. If your module uses call_rcu()180180-**and** call_srcu(), then you will need to invoke rcu_barrier() **and**181181-srcu_barrier().202202+**and** call_srcu(), then (as noted above) you will need to invoke203203+rcu_barrier() **and** srcu_barrier().182204183205184206Implementing rcu_barrier()···191211callback queues, and then waits until they have all started executing, at192212which point, all earlier RCU callbacks are guaranteed to have completed.193213194194-The original code for rcu_barrier() was as follows::214214+The original code for rcu_barrier() was roughly as follows::195215196196- 1 void rcu_barrier(void)197197- 2 {198198- 3 BUG_ON(in_interrupt());199199- 4 /* Take cpucontrol mutex to protect against CPU hotplug */200200- 5 mutex_lock(&rcu_barrier_mutex);201201- 6 init_completion(&rcu_barrier_completion);202202- 7 atomic_set(&rcu_barrier_cpu_count, 0);203203- 8 on_each_cpu(rcu_barrier_func, NULL, 0, 1);204204- 9 wait_for_completion(&rcu_barrier_completion);205205- 10 mutex_unlock(&rcu_barrier_mutex);206206- 11 }216216+ 1 void rcu_barrier(void)217217+ 2 {218218+ 3 BUG_ON(in_interrupt());219219+ 4 /* Take cpucontrol mutex to protect against CPU hotplug */220220+ 5 mutex_lock(&rcu_barrier_mutex);221221+ 6 init_completion(&rcu_barrier_completion);222222+ 7 atomic_set(&rcu_barrier_cpu_count, 1);223223+ 8 on_each_cpu(rcu_barrier_func, NULL, 0, 1);224224+ 9 if (atomic_dec_and_test(&rcu_barrier_cpu_count))225225+ 10 complete(&rcu_barrier_completion);226226+ 11 wait_for_completion(&rcu_barrier_completion);227227+ 12 mutex_unlock(&rcu_barrier_mutex);228228+ 13 }207229208208-Line 3 verifies that the caller is in process context, and lines 5 and 10230230+Line 3 verifies that the caller is in process context, and lines 5 and 12209231use rcu_barrier_mutex to ensure that only one rcu_barrier() is using the210232global completion and counters at a time, which are initialized on lines2112336 and 7. Line 8 causes each CPU to invoke rcu_barrier_func(), which is212234shown below. Note that the final "1" in on_each_cpu()'s argument list213235ensures that all the calls to rcu_barrier_func() will have completed214214-before on_each_cpu() returns. Line 9 then waits for the completion.236236+before on_each_cpu() returns. Line 9 removes the initial count from237237+rcu_barrier_cpu_count, and if this count is now zero, line 10 finalizes238238+the completion, which prevents line 11 from blocking. Either way,239239+line 11 then waits (if needed) for the completion.240240+241241+.. _rcubarrier_quiz_2:242242+243243+Quick Quiz #2:244244+ Why doesn't line 8 initialize rcu_barrier_cpu_count to zero,245245+ thereby avoiding the need for lines 9 and 10?246246+247247+:ref:`Answer to Quick Quiz #2 <answer_rcubarrier_quiz_2>`215248216249This code was rewritten in 2008 and several times thereafter, but this217250still gives the general idea.···246253Lines 3 and 4 locate RCU's internal per-CPU rcu_data structure,247254which contains the struct rcu_head that needed for the later call to248255call_rcu(). Line 7 picks up a pointer to this struct rcu_head, and line249249-8 increments a global counter. This counter will later be decremented256256+8 increments the global counter. This counter will later be decremented250257by the callback. Line 9 then registers the rcu_barrier_callback() on251258the current CPU's queue.252259···260267 4 complete(&rcu_barrier_completion);261268 5 }262269263263-.. _rcubarrier_quiz_2:270270+.. _rcubarrier_quiz_3:264271265265-Quick Quiz #2:272272+Quick Quiz #3:266273 What happens if CPU 0's rcu_barrier_func() executes267274 immediately (thus incrementing rcu_barrier_cpu_count to the268275 value one), but the other CPU's rcu_barrier_func() invocations269276 are delayed for a full grace period? Couldn't this result in270277 rcu_barrier() returning prematurely?271278272272-:ref:`Answer to Quick Quiz #2 <answer_rcubarrier_quiz_2>`279279+:ref:`Answer to Quick Quiz #3 <answer_rcubarrier_quiz_3>`273280274281The current rcu_barrier() implementation is more complex, due to the need275282to avoid disturbing idle CPUs (especially on battery-powered systems)276283and the need to minimally disturb non-idle CPUs in real-time systems.277277-However, the code above illustrates the concepts.284284+In addition, a great many optimizations have been applied. However,285285+the code above illustrates the concepts.278286279287280288rcu_barrier() Summary281289---------------------282290283283-The rcu_barrier() primitive has seen relatively little use, since most291291+The rcu_barrier() primitive is used relatively infrequently, since most284292code using RCU is in the core kernel rather than in modules. However, if285293you are using RCU from an unloadable module, you need to use rcu_barrier()286294so that your module may be safely unloaded.···312318.. _answer_rcubarrier_quiz_2:313319314320Quick Quiz #2:321321+ Why doesn't line 8 initialize rcu_barrier_cpu_count to zero,322322+ thereby avoiding the need for lines 9 and 10?323323+324324+Answer: Suppose that the on_each_cpu() function shown on line 8 was325325+ delayed, so that CPU 0's rcu_barrier_func() executed and326326+ the corresponding grace period elapsed, all before CPU 1's327327+ rcu_barrier_func() started executing. This would result in328328+ rcu_barrier_cpu_count being decremented to zero, so that line329329+ 11's wait_for_completion() would return immediately, failing to330330+ wait for CPU 1's callbacks to be invoked.331331+332332+ Note that this was not a problem when the rcu_barrier() code333333+ was first added back in 2005. This is because on_each_cpu()334334+ disables preemption, which acted as an RCU read-side critical335335+ section, thus preventing CPU 0's grace period from completing336336+ until on_each_cpu() had dealt with all of the CPUs. However,337337+ with the advent of preemptible RCU, rcu_barrier() no longer338338+ waited on nonpreemptible regions of code in preemptible kernels,339339+ that being the job of the new rcu_barrier_sched() function.340340+341341+ However, with the RCU flavor consolidation around v4.20, this342342+ possibility was once again ruled out, because the consolidated343343+ RCU once again waits on nonpreemptible regions of code.344344+345345+ Nevertheless, that extra count might still be a good idea.346346+ Relying on these sort of accidents of implementation can result347347+ in later surprise bugs when the implementation changes.348348+349349+:ref:`Back to Quick Quiz #2 <rcubarrier_quiz_2>`350350+351351+.. _answer_rcubarrier_quiz_3:352352+353353+Quick Quiz #3:315354 What happens if CPU 0's rcu_barrier_func() executes316355 immediately (thus incrementing rcu_barrier_cpu_count to the317356 value one), but the other CPU's rcu_barrier_func() invocations···363336364337 Therefore, on_each_cpu() disables preemption across its call365338 to smp_call_function() and also across the local call to366366- rcu_barrier_func(). This prevents the local CPU from context367367- switching, again preventing grace periods from completing. This339339+ rcu_barrier_func(). Because recent RCU implementations treat340340+ preemption-disabled regions of code as RCU read-side critical341341+ sections, this prevents grace periods from completing. This368342 means that all CPUs have executed rcu_barrier_func() before369343 the first rcu_barrier_callback() can possibly execute, in turn370344 preventing rcu_barrier_cpu_count from prematurely reaching zero.371345372372- Currently, -rt implementations of RCU keep but a single global373373- queue for RCU callbacks, and thus do not suffer from this374374- problem. However, when the -rt RCU eventually does have per-CPU375375- callback queues, things will have to change. One simple change376376- is to add an rcu_read_lock() before line 8 of rcu_barrier()377377- and an rcu_read_unlock() after line 8 of this same function. If378378- you can think of a better change, please let me know!346346+ But if on_each_cpu() ever decides to forgo disabling preemption,347347+ as might well happen due to real-time latency considerations,348348+ initializing rcu_barrier_cpu_count to one will save the day.379349380380-:ref:`Back to Quick Quiz #2 <rcubarrier_quiz_2>`350350+:ref:`Back to Quick Quiz #3 <rcubarrier_quiz_3>`