Commit 4af49830 authored by Amol Grover's avatar Amol Grover Committed by Paul E. McKenney

doc: Convert to rcubarrier.txt to ReST

Convert rcubarrier.txt to rcubarrier.rst and add it to index.rst.

Format file according to reST
- Add headings and sub-headings
- Add code segments
- Add cross-references to quizes and answers
Signed-off-by: default avatarAmol Grover <frextrite@gmail.com>
Tested-by: default avatarPhong Tran <tranmanphong@gmail.com>
Signed-off-by: default avatarPaul E. McKenney <paulmck@kernel.org>
parent b00aedf9
...@@ -8,6 +8,7 @@ RCU concepts ...@@ -8,6 +8,7 @@ RCU concepts
:maxdepth: 3 :maxdepth: 3
arrayRCU arrayRCU
rcubarrier
rcu_dereference rcu_dereference
whatisRCU whatisRCU
rcu rcu
......
.. _rcu_barrier:
RCU and Unloadable Modules RCU and Unloadable Modules
==========================
[Originally published in LWN Jan. 14, 2007: http://lwn.net/Articles/217484/] [Originally published in LWN Jan. 14, 2007: http://lwn.net/Articles/217484/]
...@@ -21,7 +24,7 @@ given that readers might well leave absolutely no trace of their ...@@ -21,7 +24,7 @@ given that readers might well leave absolutely no trace of their
presence? There is a synchronize_rcu() primitive that blocks until all presence? There is a synchronize_rcu() primitive that blocks until all
pre-existing readers have completed. An updater wishing to delete an pre-existing readers have completed. An updater wishing to delete an
element p from a linked list might do the following, while holding an element p from a linked list might do the following, while holding an
appropriate lock, of course: appropriate lock, of course::
list_del_rcu(p); list_del_rcu(p);
synchronize_rcu(); synchronize_rcu();
...@@ -32,13 +35,13 @@ primitive must be used instead. This primitive takes a pointer to an ...@@ -32,13 +35,13 @@ primitive must be used instead. This primitive takes a pointer to an
rcu_head struct placed within the RCU-protected data structure and rcu_head struct placed within the RCU-protected data structure and
another pointer to a function that may be invoked later to free that another pointer to a function that may be invoked later to free that
structure. Code to delete an element p from the linked list from IRQ structure. Code to delete an element p from the linked list from IRQ
context might then be as follows: context might then be as follows::
list_del_rcu(p); list_del_rcu(p);
call_rcu(&p->rcu, p_callback); call_rcu(&p->rcu, p_callback);
Since call_rcu() never blocks, this code can safely be used from within Since call_rcu() never blocks, this code can safely be used from within
IRQ context. The function p_callback() might be defined as follows: IRQ context. The function p_callback() might be defined as follows::
static void p_callback(struct rcu_head *rp) static void p_callback(struct rcu_head *rp)
{ {
...@@ -49,6 +52,7 @@ IRQ context. The function p_callback() might be defined as follows: ...@@ -49,6 +52,7 @@ IRQ context. The function p_callback() might be defined as follows:
Unloading Modules That Use call_rcu() Unloading Modules That Use call_rcu()
-------------------------------------
But what if p_callback is defined in an unloadable module? But what if p_callback is defined in an unloadable module?
...@@ -69,10 +73,11 @@ in realtime kernels in order to avoid excessive scheduling latencies. ...@@ -69,10 +73,11 @@ in realtime kernels in order to avoid excessive scheduling latencies.
rcu_barrier() rcu_barrier()
-------------
We instead need the rcu_barrier() primitive. Rather than waiting for We instead need the rcu_barrier() primitive. Rather than waiting for
a grace period to elapse, rcu_barrier() waits for all outstanding RCU a grace period to elapse, rcu_barrier() waits for all outstanding RCU
callbacks to complete. Please note that rcu_barrier() does -not- imply callbacks to complete. Please note that rcu_barrier() does **not** imply
synchronize_rcu(), in particular, if there are no RCU callbacks queued synchronize_rcu(), in particular, if there are no RCU callbacks queued
anywhere, rcu_barrier() is within its rights to return immediately, anywhere, rcu_barrier() is within its rights to return immediately,
without waiting for a grace period to elapse. without waiting for a grace period to elapse.
...@@ -88,15 +93,15 @@ must match the flavor of rcu_barrier() with that of call_rcu(). If your ...@@ -88,15 +93,15 @@ must match the flavor of rcu_barrier() with that of call_rcu(). If your
module uses multiple flavors of call_rcu(), then it must also use multiple module uses multiple flavors of call_rcu(), then it must also use multiple
flavors of rcu_barrier() when unloading that module. For example, if flavors of rcu_barrier() when unloading that module. For example, if
it uses call_rcu(), call_srcu() on srcu_struct_1, and call_srcu() on it uses call_rcu(), call_srcu() on srcu_struct_1, and call_srcu() on
srcu_struct_2(), then the following three lines of code will be required srcu_struct_2, then the following three lines of code will be required
when unloading: when unloading::
1 rcu_barrier(); 1 rcu_barrier();
2 srcu_barrier(&srcu_struct_1); 2 srcu_barrier(&srcu_struct_1);
3 srcu_barrier(&srcu_struct_2); 3 srcu_barrier(&srcu_struct_2);
The rcutorture module makes use of rcu_barrier() in its exit function The rcutorture module makes use of rcu_barrier() in its exit function
as follows: as follows::
1 static void 1 static void
2 rcu_torture_cleanup(void) 2 rcu_torture_cleanup(void)
...@@ -107,60 +112,60 @@ as follows: ...@@ -107,60 +112,60 @@ as follows:
7 if (shuffler_task != NULL) { 7 if (shuffler_task != NULL) {
8 VERBOSE_PRINTK_STRING("Stopping rcu_torture_shuffle task"); 8 VERBOSE_PRINTK_STRING("Stopping rcu_torture_shuffle task");
9 kthread_stop(shuffler_task); 9 kthread_stop(shuffler_task);
10 } 10 }
11 shuffler_task = NULL; 11 shuffler_task = NULL;
12 12
13 if (writer_task != NULL) { 13 if (writer_task != NULL) {
14 VERBOSE_PRINTK_STRING("Stopping rcu_torture_writer task"); 14 VERBOSE_PRINTK_STRING("Stopping rcu_torture_writer task");
15 kthread_stop(writer_task); 15 kthread_stop(writer_task);
16 } 16 }
17 writer_task = NULL; 17 writer_task = NULL;
18 18
19 if (reader_tasks != NULL) { 19 if (reader_tasks != NULL) {
20 for (i = 0; i < nrealreaders; i++) { 20 for (i = 0; i < nrealreaders; i++) {
21 if (reader_tasks[i] != NULL) { 21 if (reader_tasks[i] != NULL) {
22 VERBOSE_PRINTK_STRING( 22 VERBOSE_PRINTK_STRING(
23 "Stopping rcu_torture_reader task"); 23 "Stopping rcu_torture_reader task");
24 kthread_stop(reader_tasks[i]); 24 kthread_stop(reader_tasks[i]);
25 } 25 }
26 reader_tasks[i] = NULL; 26 reader_tasks[i] = NULL;
27 } 27 }
28 kfree(reader_tasks); 28 kfree(reader_tasks);
29 reader_tasks = NULL; 29 reader_tasks = NULL;
30 } 30 }
31 rcu_torture_current = NULL; 31 rcu_torture_current = NULL;
32 32
33 if (fakewriter_tasks != NULL) { 33 if (fakewriter_tasks != NULL) {
34 for (i = 0; i < nfakewriters; i++) { 34 for (i = 0; i < nfakewriters; i++) {
35 if (fakewriter_tasks[i] != NULL) { 35 if (fakewriter_tasks[i] != NULL) {
36 VERBOSE_PRINTK_STRING( 36 VERBOSE_PRINTK_STRING(
37 "Stopping rcu_torture_fakewriter task"); 37 "Stopping rcu_torture_fakewriter task");
38 kthread_stop(fakewriter_tasks[i]); 38 kthread_stop(fakewriter_tasks[i]);
39 } 39 }
40 fakewriter_tasks[i] = NULL; 40 fakewriter_tasks[i] = NULL;
41 } 41 }
42 kfree(fakewriter_tasks); 42 kfree(fakewriter_tasks);
43 fakewriter_tasks = NULL; 43 fakewriter_tasks = NULL;
44 } 44 }
45 45
46 if (stats_task != NULL) { 46 if (stats_task != NULL) {
47 VERBOSE_PRINTK_STRING("Stopping rcu_torture_stats task"); 47 VERBOSE_PRINTK_STRING("Stopping rcu_torture_stats task");
48 kthread_stop(stats_task); 48 kthread_stop(stats_task);
49 } 49 }
50 stats_task = NULL; 50 stats_task = NULL;
51 51
52 /* Wait for all RCU callbacks to fire. */ 52 /* Wait for all RCU callbacks to fire. */
53 rcu_barrier(); 53 rcu_barrier();
54 54
55 rcu_torture_stats_print(); /* -After- the stats thread is stopped! */ 55 rcu_torture_stats_print(); /* -After- the stats thread is stopped! */
56 56
57 if (cur_ops->cleanup != NULL) 57 if (cur_ops->cleanup != NULL)
58 cur_ops->cleanup(); 58 cur_ops->cleanup();
59 if (atomic_read(&n_rcu_torture_error)) 59 if (atomic_read(&n_rcu_torture_error))
60 rcu_torture_print_module_parms("End of test: FAILURE"); 60 rcu_torture_print_module_parms("End of test: FAILURE");
61 else 61 else
62 rcu_torture_print_module_parms("End of test: SUCCESS"); 62 rcu_torture_print_module_parms("End of test: SUCCESS");
63 } 63 }
Line 6 sets a global variable that prevents any RCU callbacks from Line 6 sets a global variable that prevents any RCU callbacks from
re-posting themselves. This will not be necessary in most cases, since re-posting themselves. This will not be necessary in most cases, since
...@@ -176,9 +181,14 @@ for any pre-existing callbacks to complete. ...@@ -176,9 +181,14 @@ for any pre-existing callbacks to complete.
Then lines 55-62 print status and do operation-specific cleanup, and Then lines 55-62 print status and do operation-specific cleanup, and
then return, permitting the module-unload operation to be completed. then return, permitting the module-unload operation to be completed.
Quick Quiz #1: Is there any other situation where rcu_barrier() might .. _rcubarrier_quiz_1:
Quick Quiz #1:
Is there any other situation where rcu_barrier() might
be required? be required?
:ref:`Answer to Quick Quiz #1 <answer_rcubarrier_quiz_1>`
Your module might have additional complications. For example, if your Your module might have additional complications. For example, if your
module invokes call_rcu() from timers, you will need to first cancel all module invokes call_rcu() from timers, you will need to first cancel all
the timers, and only then invoke rcu_barrier() to wait for any remaining the timers, and only then invoke rcu_barrier() to wait for any remaining
...@@ -188,11 +198,12 @@ Of course, if you module uses call_rcu(), you will need to invoke ...@@ -188,11 +198,12 @@ Of course, if you module uses call_rcu(), you will need to invoke
rcu_barrier() before unloading. Similarly, if your module uses rcu_barrier() before unloading. Similarly, if your module uses
call_srcu(), you will need to invoke srcu_barrier() before unloading, call_srcu(), you will need to invoke srcu_barrier() before unloading,
and on the same srcu_struct structure. If your module uses call_rcu() and on the same srcu_struct structure. If your module uses call_rcu()
-and- call_srcu(), then you will need to invoke rcu_barrier() -and- **and** call_srcu(), then you will need to invoke rcu_barrier() **and**
srcu_barrier(). srcu_barrier().
Implementing rcu_barrier() Implementing rcu_barrier()
--------------------------
Dipankar Sarma's implementation of rcu_barrier() makes use of the fact Dipankar Sarma's implementation of rcu_barrier() makes use of the fact
that RCU callbacks are never reordered once queued on one of the per-CPU that RCU callbacks are never reordered once queued on one of the per-CPU
...@@ -200,7 +211,7 @@ queues. His implementation queues an RCU callback on each of the per-CPU ...@@ -200,7 +211,7 @@ queues. His implementation queues an RCU callback on each of the per-CPU
callback queues, and then waits until they have all started executing, at callback queues, and then waits until they have all started executing, at
which point, all earlier RCU callbacks are guaranteed to have completed. which point, all earlier RCU callbacks are guaranteed to have completed.
The original code for rcu_barrier() was as follows: The original code for rcu_barrier() was as follows::
1 void rcu_barrier(void) 1 void rcu_barrier(void)
2 { 2 {
...@@ -211,8 +222,8 @@ The original code for rcu_barrier() was as follows: ...@@ -211,8 +222,8 @@ The original code for rcu_barrier() was as follows:
7 atomic_set(&rcu_barrier_cpu_count, 0); 7 atomic_set(&rcu_barrier_cpu_count, 0);
8 on_each_cpu(rcu_barrier_func, NULL, 0, 1); 8 on_each_cpu(rcu_barrier_func, NULL, 0, 1);
9 wait_for_completion(&rcu_barrier_completion); 9 wait_for_completion(&rcu_barrier_completion);
10 mutex_unlock(&rcu_barrier_mutex); 10 mutex_unlock(&rcu_barrier_mutex);
11 } 11 }
Line 3 verifies that the caller is in process context, and lines 5 and 10 Line 3 verifies that the caller is in process context, and lines 5 and 10
use rcu_barrier_mutex to ensure that only one rcu_barrier() is using the use rcu_barrier_mutex to ensure that only one rcu_barrier() is using the
...@@ -226,7 +237,7 @@ This code was rewritten in 2008 and several times thereafter, but this ...@@ -226,7 +237,7 @@ This code was rewritten in 2008 and several times thereafter, but this
still gives the general idea. still gives the general idea.
The rcu_barrier_func() runs on each CPU, where it invokes call_rcu() The rcu_barrier_func() runs on each CPU, where it invokes call_rcu()
to post an RCU callback, as follows: to post an RCU callback, as follows::
1 static void rcu_barrier_func(void *notused) 1 static void rcu_barrier_func(void *notused)
2 { 2 {
...@@ -237,7 +248,7 @@ to post an RCU callback, as follows: ...@@ -237,7 +248,7 @@ to post an RCU callback, as follows:
7 head = &rdp->barrier; 7 head = &rdp->barrier;
8 atomic_inc(&rcu_barrier_cpu_count); 8 atomic_inc(&rcu_barrier_cpu_count);
9 call_rcu(head, rcu_barrier_callback); 9 call_rcu(head, rcu_barrier_callback);
10 } 10 }
Lines 3 and 4 locate RCU's internal per-CPU rcu_data structure, Lines 3 and 4 locate RCU's internal per-CPU rcu_data structure,
which contains the struct rcu_head that needed for the later call to which contains the struct rcu_head that needed for the later call to
...@@ -248,7 +259,7 @@ the current CPU's queue. ...@@ -248,7 +259,7 @@ the current CPU's queue.
The rcu_barrier_callback() function simply atomically decrements the The rcu_barrier_callback() function simply atomically decrements the
rcu_barrier_cpu_count variable and finalizes the completion when it rcu_barrier_cpu_count variable and finalizes the completion when it
reaches zero, as follows: reaches zero, as follows::
1 static void rcu_barrier_callback(struct rcu_head *notused) 1 static void rcu_barrier_callback(struct rcu_head *notused)
2 { 2 {
...@@ -256,12 +267,17 @@ reaches zero, as follows: ...@@ -256,12 +267,17 @@ reaches zero, as follows:
4 complete(&rcu_barrier_completion); 4 complete(&rcu_barrier_completion);
5 } 5 }
Quick Quiz #2: What happens if CPU 0's rcu_barrier_func() executes .. _rcubarrier_quiz_2:
Quick Quiz #2:
What happens if CPU 0's rcu_barrier_func() executes
immediately (thus incrementing rcu_barrier_cpu_count to the immediately (thus incrementing rcu_barrier_cpu_count to the
value one), but the other CPU's rcu_barrier_func() invocations value one), but the other CPU's rcu_barrier_func() invocations
are delayed for a full grace period? Couldn't this result in are delayed for a full grace period? Couldn't this result in
rcu_barrier() returning prematurely? rcu_barrier() returning prematurely?
:ref:`Answer to Quick Quiz #2 <answer_rcubarrier_quiz_2>`
The current rcu_barrier() implementation is more complex, due to the need The current rcu_barrier() implementation is more complex, due to the need
to avoid disturbing idle CPUs (especially on battery-powered systems) to avoid disturbing idle CPUs (especially on battery-powered systems)
and the need to minimally disturb non-idle CPUs in real-time systems. and the need to minimally disturb non-idle CPUs in real-time systems.
...@@ -269,6 +285,7 @@ However, the code above illustrates the concepts. ...@@ -269,6 +285,7 @@ However, the code above illustrates the concepts.
rcu_barrier() Summary rcu_barrier() Summary
---------------------
The rcu_barrier() primitive has seen relatively little use, since most The rcu_barrier() primitive has seen relatively little use, since most
code using RCU is in the core kernel rather than in modules. However, if code using RCU is in the core kernel rather than in modules. However, if
...@@ -277,8 +294,12 @@ so that your module may be safely unloaded. ...@@ -277,8 +294,12 @@ so that your module may be safely unloaded.
Answers to Quick Quizzes Answers to Quick Quizzes
------------------------
Quick Quiz #1: Is there any other situation where rcu_barrier() might .. _answer_rcubarrier_quiz_1:
Quick Quiz #1:
Is there any other situation where rcu_barrier() might
be required? be required?
Answer: Interestingly enough, rcu_barrier() was not originally Answer: Interestingly enough, rcu_barrier() was not originally
...@@ -292,7 +313,12 @@ Answer: Interestingly enough, rcu_barrier() was not originally ...@@ -292,7 +313,12 @@ Answer: Interestingly enough, rcu_barrier() was not originally
implementing rcutorture, and found that rcu_barrier() solves implementing rcutorture, and found that rcu_barrier() solves
this problem as well. this problem as well.
Quick Quiz #2: What happens if CPU 0's rcu_barrier_func() executes :ref:`Back to Quick Quiz #1 <rcubarrier_quiz_1>`
.. _answer_rcubarrier_quiz_2:
Quick Quiz #2:
What happens if CPU 0's rcu_barrier_func() executes
immediately (thus incrementing rcu_barrier_cpu_count to the immediately (thus incrementing rcu_barrier_cpu_count to the
value one), but the other CPU's rcu_barrier_func() invocations value one), but the other CPU's rcu_barrier_func() invocations
are delayed for a full grace period? Couldn't this result in are delayed for a full grace period? Couldn't this result in
...@@ -323,3 +349,5 @@ Answer: This cannot happen. The reason is that on_each_cpu() has its last ...@@ -323,3 +349,5 @@ Answer: This cannot happen. The reason is that on_each_cpu() has its last
is to add an rcu_read_lock() before line 8 of rcu_barrier() is to add an rcu_read_lock() before line 8 of rcu_barrier()
and an rcu_read_unlock() after line 8 of this same function. If and an rcu_read_unlock() after line 8 of this same function. If
you can think of a better change, please let me know! you can think of a better change, please let me know!
:ref:`Back to Quick Quiz #2 <rcubarrier_quiz_2>`
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment