Merge branch 'core-speculation-for-linus' of git://git.kernel.org/pub/scm/linux/kerne...
[sfrench/cifs-2.6.git] / Documentation / RCU / UP.txt
1 RCU on Uniprocessor Systems
2
3
4 A common misconception is that, on UP systems, the call_rcu() primitive
5 may immediately invoke its function.  The basis of this misconception
6 is that since there is only one CPU, it should not be necessary to
7 wait for anything else to get done, since there are no other CPUs for
8 anything else to be happening on.  Although this approach will -sort- -of-
9 work a surprising amount of the time, it is a very bad idea in general.
10 This document presents three examples that demonstrate exactly how bad
11 an idea this is.
12
13
14 Example 1: softirq Suicide
15
16 Suppose that an RCU-based algorithm scans a linked list containing
17 elements A, B, and C in process context, and can delete elements from
18 this same list in softirq context.  Suppose that the process-context scan
19 is referencing element B when it is interrupted by softirq processing,
20 which deletes element B, and then invokes call_rcu() to free element B
21 after a grace period.
22
23 Now, if call_rcu() were to directly invoke its arguments, then upon return
24 from softirq, the list scan would find itself referencing a newly freed
25 element B.  This situation can greatly decrease the life expectancy of
26 your kernel.
27
28 This same problem can occur if call_rcu() is invoked from a hardware
29 interrupt handler.
30
31
32 Example 2: Function-Call Fatality
33
34 Of course, one could avert the suicide described in the preceding example
35 by having call_rcu() directly invoke its arguments only if it was called
36 from process context.  However, this can fail in a similar manner.
37
38 Suppose that an RCU-based algorithm again scans a linked list containing
39 elements A, B, and C in process contexts, but that it invokes a function
40 on each element as it is scanned.  Suppose further that this function
41 deletes element B from the list, then passes it to call_rcu() for deferred
42 freeing.  This may be a bit unconventional, but it is perfectly legal
43 RCU usage, since call_rcu() must wait for a grace period to elapse.
44 Therefore, in this case, allowing call_rcu() to immediately invoke
45 its arguments would cause it to fail to make the fundamental guarantee
46 underlying RCU, namely that call_rcu() defers invoking its arguments until
47 all RCU read-side critical sections currently executing have completed.
48
49 Quick Quiz #1: why is it -not- legal to invoke synchronize_rcu() in
50         this case?
51
52
53 Example 3: Death by Deadlock
54
55 Suppose that call_rcu() is invoked while holding a lock, and that the
56 callback function must acquire this same lock.  In this case, if
57 call_rcu() were to directly invoke the callback, the result would
58 be self-deadlock.
59
60 In some cases, it would possible to restructure to code so that
61 the call_rcu() is delayed until after the lock is released.  However,
62 there are cases where this can be quite ugly:
63
64 1.      If a number of items need to be passed to call_rcu() within
65         the same critical section, then the code would need to create
66         a list of them, then traverse the list once the lock was
67         released.
68
69 2.      In some cases, the lock will be held across some kernel API,
70         so that delaying the call_rcu() until the lock is released
71         requires that the data item be passed up via a common API.
72         It is far better to guarantee that callbacks are invoked
73         with no locks held than to have to modify such APIs to allow
74         arbitrary data items to be passed back up through them.
75
76 If call_rcu() directly invokes the callback, painful locking restrictions
77 or API changes would be required.
78
79 Quick Quiz #2: What locking restriction must RCU callbacks respect?
80
81
82 Summary
83
84 Permitting call_rcu() to immediately invoke its arguments breaks RCU,
85 even on a UP system.  So do not do it!  Even on a UP system, the RCU
86 infrastructure -must- respect grace periods, and -must- invoke callbacks
87 from a known environment in which no locks are held.
88
89 Note that it -is- safe for synchronize_rcu() to return immediately on
90 UP systems, including !PREEMPT SMP builds running on UP systems.
91
92 Quick Quiz #3: Why can't synchronize_rcu() return immediately on
93         UP systems running preemptable RCU?
94
95
96 Answer to Quick Quiz #1:
97         Why is it -not- legal to invoke synchronize_rcu() in this case?
98
99         Because the calling function is scanning an RCU-protected linked
100         list, and is therefore within an RCU read-side critical section.
101         Therefore, the called function has been invoked within an RCU
102         read-side critical section, and is not permitted to block.
103
104 Answer to Quick Quiz #2:
105         What locking restriction must RCU callbacks respect?
106
107         Any lock that is acquired within an RCU callback must be
108         acquired elsewhere using an _irq variant of the spinlock
109         primitive.  For example, if "mylock" is acquired by an
110         RCU callback, then a process-context acquisition of this
111         lock must use something like spin_lock_irqsave() to
112         acquire the lock.
113
114         If the process-context code were to simply use spin_lock(),
115         then, since RCU callbacks can be invoked from softirq context,
116         the callback might be called from a softirq that interrupted
117         the process-context critical section.  This would result in
118         self-deadlock.
119
120         This restriction might seem gratuitous, since very few RCU
121         callbacks acquire locks directly.  However, a great many RCU
122         callbacks do acquire locks -indirectly-, for example, via
123         the kfree() primitive.
124
125 Answer to Quick Quiz #3:
126         Why can't synchronize_rcu() return immediately on UP systems
127         running preemptable RCU?
128
129         Because some other task might have been preempted in the middle
130         of an RCU read-side critical section.  If synchronize_rcu()
131         simply immediately returned, it would prematurely signal the
132         end of the grace period, which would come as a nasty shock to
133         that other thread when it started running again.