Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
at v5.2-rc2 133 lines 5.6 kB view raw
1RCU on Uniprocessor Systems 2 3 4A common misconception is that, on UP systems, the call_rcu() primitive 5may immediately invoke its function. The basis of this misconception 6is that since there is only one CPU, it should not be necessary to 7wait for anything else to get done, since there are no other CPUs for 8anything else to be happening on. Although this approach will -sort- -of- 9work a surprising amount of the time, it is a very bad idea in general. 10This document presents three examples that demonstrate exactly how bad 11an idea this is. 12 13 14Example 1: softirq Suicide 15 16Suppose that an RCU-based algorithm scans a linked list containing 17elements A, B, and C in process context, and can delete elements from 18this same list in softirq context. Suppose that the process-context scan 19is referencing element B when it is interrupted by softirq processing, 20which deletes element B, and then invokes call_rcu() to free element B 21after a grace period. 22 23Now, if call_rcu() were to directly invoke its arguments, then upon return 24from softirq, the list scan would find itself referencing a newly freed 25element B. This situation can greatly decrease the life expectancy of 26your kernel. 27 28This same problem can occur if call_rcu() is invoked from a hardware 29interrupt handler. 30 31 32Example 2: Function-Call Fatality 33 34Of course, one could avert the suicide described in the preceding example 35by having call_rcu() directly invoke its arguments only if it was called 36from process context. However, this can fail in a similar manner. 37 38Suppose that an RCU-based algorithm again scans a linked list containing 39elements A, B, and C in process contexts, but that it invokes a function 40on each element as it is scanned. Suppose further that this function 41deletes element B from the list, then passes it to call_rcu() for deferred 42freeing. This may be a bit unconventional, but it is perfectly legal 43RCU usage, since call_rcu() must wait for a grace period to elapse. 44Therefore, in this case, allowing call_rcu() to immediately invoke 45its arguments would cause it to fail to make the fundamental guarantee 46underlying RCU, namely that call_rcu() defers invoking its arguments until 47all RCU read-side critical sections currently executing have completed. 48 49Quick Quiz #1: why is it -not- legal to invoke synchronize_rcu() in 50 this case? 51 52 53Example 3: Death by Deadlock 54 55Suppose that call_rcu() is invoked while holding a lock, and that the 56callback function must acquire this same lock. In this case, if 57call_rcu() were to directly invoke the callback, the result would 58be self-deadlock. 59 60In some cases, it would possible to restructure to code so that 61the call_rcu() is delayed until after the lock is released. However, 62there are cases where this can be quite ugly: 63 641. 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 692. 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 76If call_rcu() directly invokes the callback, painful locking restrictions 77or API changes would be required. 78 79Quick Quiz #2: What locking restriction must RCU callbacks respect? 80 81 82Summary 83 84Permitting call_rcu() to immediately invoke its arguments breaks RCU, 85even on a UP system. So do not do it! Even on a UP system, the RCU 86infrastructure -must- respect grace periods, and -must- invoke callbacks 87from a known environment in which no locks are held. 88 89Note that it -is- safe for synchronize_rcu() to return immediately on 90UP systems, including !PREEMPT SMP builds running on UP systems. 91 92Quick Quiz #3: Why can't synchronize_rcu() return immediately on 93 UP systems running preemptable RCU? 94 95 96Answer 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 104Answer 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 125Answer 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.