call_rcu_sched() series
Subsystems (2)
cover call_rcu_sched() series
This series adds call_rcu_sched(), applies some fixes to RCU Classic, adds rcu_barrier_sched(), updates rcutorture, and updates the RCU documentation.
PATCH 1/5 Add call_rcu_sched()
Fourth cut of patch to provide the call_rcu_sched(). This is again to synchronize_sched() as call_rcu() is to synchronize_rcu().
Should be fine for experimental and -rt use, but not ready for inclusion. With some luck, I will be able to tell Andrew to come out of hiding on the next round.
Passes multi-day rcutorture sessions with concurrent CPU hotplugging.
Fixes since the first version include a bug that could result in indefinite blocking (spotted by Gautham Shenoy), better resiliency against CPU-hotplug operations, and other minor fixes.
Fixes since the second version include reworking grace-period detection to avoid deadlocks that could happen when running concurrently with CPU hotplug, adding Mathieu's fix to avoid the softlockup messages, as well as Mathieu's fix to allow use earlier in boot.
Fixes since the third version include a wrong-CPU bug spotted by Andrew, getting rid of the obsolete synchronize_kernel API that somehow snuck back in, merging spin_unlock() and local_irq_restore() in a few places, commenting the code that checks for quiescent states based on interrupting from user-mode execution or the idle loop, removing some inline attributes, and some code-style changes.
Known/suspected shortcomings:
o I still do not entirely trust the sleep/wakeup logic. Next step
will be to use a private snapshot of the CPU online mask in rcu_sched_grace_period() -- if the CPU wasn't there at the start of the grace period, we don't need to hear from it. And the bit about accounting for changes in online CPUs inside of rcu_sched_grace_period() is ugly anyway.
o It might be good for rcu_sched_grace_period() to invoke
resched_cpu() when a given CPU wasn't responding quickly, but resched_cpu() is declared static...
This patch also fixes a long-standing bug in the earlier preemptable-RCU implementation of synchronize_rcu() that could result in loss of concurrent external changes to a task's CPU affinity mask. I still cannot remember who reported this...
---
include/linux/rcuclassic.h | 3 include/linux/rcupdate.h | 22 ++ include/linux/rcupreempt.h | 42 +++- init/main.c | 1 kernel/rcupdate.c | 20 -- kernel/rcupreempt.c | 414 ++++++++++++++++++++++++++++++++++++++++----- 6 files changed, 434 insertions(+), 68 deletions(-)
include/linux/rcuclassic.h +3 -0
@@ -153,7 +153,10 @@ extern struct lockdep_map rcu_lock_map;#define __synchronize_sched () synchronize_rcu() +#define call_rcu_sched (head, func) call_rcu(head, func) +extern void __rcu_init (void ); +#define rcu_init_sched () do { } while (0)extern void rcu_check_callbacks (int cpu,int user);extern void rcu_restart_cpu (int cpu);
include/linux/rcupdate.h +22 -0
@@ -42,6 +42,7 @@#include <linux/cpumask.h> #include <linux/seqlock.h> #include <linux/lockdep.h> +#include <linux/completion.h> /** * struct rcu_head - callback structure for use with RCU
@@ -182,6 +183,27 @@ struct rcu_head { (p) = (v); \ }) +/* Infrastructure to implement the synchronize_() primitives. */ + +struct rcu_synchronize { +struct rcu_head head; +struct completion completion; +}; + +extern void wakeme_after_rcu (struct rcu_head *head); + +#define synchronize_rcu_xxx (name, func) \ +void name(void) \ +{ \ + struct rcu_synchronize rcu; \ + \ + init_completion(&rcu.completion); \ +/* Will wake me after RCU finished. */ \ +func (&rcu.head, wakeme_after_rcu); \ +/* Wait for it. */ \ +wait_for_completion (&rcu.completion); \ +} +/** * synchronize_sched - block until all CPUs have exited any non-preemptive * kernel code sequences.
include/linux/rcupreempt.h +36 -6
72 lines not on this page · raw message
init/main.c +1 -0
@@ -730,6 +730,7 @@ static void __init do_initcalls(void)*/ static void __init do_basic_setup (void ) { +rcu_init_sched ();/* needed by module_init stage. */ /* drivers will send hotplug events */ init_workqueues ();usermodehelper_init ();
kernel/rcupdate.c +2 -18
@@ -39,18 +39,12 @@#include <linux/sched.h> #include <asm/atomic.h> #include <linux/bitops.h> -#include <linux/completion.h> #include <linux/percpu.h> #include <linux/notifier.h> #include <linux/cpu.h> #include <linux/mutex.h> #include <linux/module.h> -struct rcu_synchronize { -struct rcu_head head; -struct completion completion; -}; - static DEFINE_PER_CPU(struct rcu_head, rcu_barrier_head) = {NULL};static atomic_t rcu_barrier_cpu_count;static DEFINE_MUTEX (rcu_barrier_mutex);
@@ -60,7 +54,7 @@ static struct completion rcu_barrier_com* Awaken the corresponding synchronize_rcu() instance now that a * grace period has elapsed. */ -static void wakeme_after_rcu (struct rcu_head *head) +void wakeme_after_rcu (struct rcu_head *head) {struct rcu_synchronize *rcu;
@@ -77,17 +71,7 @@ static void wakeme_after_rcu(struct rcu_* sections are delimited by rcu_read_lock() and rcu_read_unlock(), * and may be nested. */ -void synchronize_rcu (void ) -{ -struct rcu_synchronize rcu; - -init_completion (&rcu.completion); -/* Will wake me after RCU finished */ -call_rcu (&rcu.head, wakeme_after_rcu); - -/* Wait for it */ -wait_for_completion (&rcu.completion); -} +synchronize_rcu_xxx(synchronize_rcu, call_rcu)EXPORT_SYMBOL_GPL (synchronize_rcu);static void rcu_barrier_callback (struct rcu_head *notused)
kernel/rcupreempt.c +370 -44
649 lines not on this page · raw message
PATCH 2/5 Add memory barriers and comments to rcu_check_callbacks()
In reply to Paul E. McKenneyAdd comments to the logic that infers quiescent states when interrupting from either user mode or the idle loop. Also add a memory barrier: it appears that James Huang was in fact onto something, as the scheduler is much less synchronization happy than it once was, so we can no longer rely on its memory barriers in all cases.
---
rcuclassic.c | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-)
kernel/rcuclassic.c +29 -1
@@ -502,10 +502,38 @@ void rcu_check_callbacks(int cpu, int usif (user || (idle_cpu (cpu) && !in_softirq () &&hardirq_count () <= (1 <<HARDIRQ_SHIFT ))) { + +/* +* Get here if this CPU took its interrupt from user +* mode or from the idle loop, and if this is not a +* nested interrupt. In this case, the CPU is in +* a quiescent state, so count it. +* +* Also do a memory barrier. This is needed to handle +* the case where writes from a preempt-disable section +* of code get reordered into schedule() by this CPU's +* write buffer. The memory barrier makes sure that +* the rcu_qsctr_inc() and rcu_bh_qsctr_inc() are see +* by other CPUs to happen after any such write. +*/ + +smp_mb ();/* See above block comment. */ rcu_qsctr_inc (cpu);rcu_bh_qsctr_inc (cpu); - }else if (!in_softirq ()) + + }else if (!in_softirq ()) { + +/* +* Get here if this CPU did not take its interrupt from +* softirq, in other words, if it is not interrupting +* a rcu_bh read-side critical section. This is an _bh +* critical section, so count it. The memory barrier +* is needed for the same reason as is the above one. +*/ + +smp_mb ();/* See above block comment. */ rcu_bh_qsctr_inc (cpu); + }raise_rcu_softirq (); }
PATCH 3/5 Add rcu_barrier_sched() and rcu_barrier_bh()
In reply to Paul E. McKenneyAdd rcu_barrier_sched() and rcu_barrier_bh(). With these in place, rcutorture no longer gives the occasional oops when repeatedly starting and stopping torturing rcu_bh. Also adds the API needed to flush out pre-existing call_rcu_sched() callbacks.
---
include/linux/rcupdate.h | 2 + kernel/rcupdate.c | 55 +++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 51 insertions(+), 6 deletions(-)
include/linux/rcupdate.h +2 -0
@@ -260,6 +260,8 @@ extern void call_rcu_bh(struct rcu_head/* Exported common interfaces */ extern void synchronize_rcu (void );extern void rcu_barrier (void ); +extern void rcu_barrier_bh (void ); +extern void rcu_barrier_sched (void );extern long rcu_batches_completed (void );extern long rcu_batches_completed_bh (void );
kernel/rcupdate.c +49 -6
87 lines not on this page · raw message
PATCH 4/5 Add call_rcu_sched() and friends to rcutorture
In reply to Paul E. McKenneyAdd entry to rcu_torture_ops allowing the correct barrier function to be used upon exit from rcutorture. Also add torture options for the new call_rcu_sched() API.
---
rcutorture.c | 34 +++++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-)
kernel/rcutorture.c +31 -3
@@ -191,6 +191,7 @@ struct rcu_torture_ops {int (*completed)(void );void (*deferredfree)(struct rcu_torture *p);void (*sync)(void ); +void (*cb_barrier)(void );int (*stats)(char *page);char *name; };
@@ -264,6 +265,7 @@ static struct rcu_torture_ops rcu_ops = .completed = rcu_torture_completed, .deferredfree = rcu_torture_deferred_free, .sync = synchronize_rcu, + .cb_barrier = rcu_barrier, .stats = NULL, .name = "rcu" };
@@ -303,6 +305,7 @@ static struct rcu_torture_ops rcu_sync_o .completed = rcu_torture_completed, .deferredfree = rcu_sync_torture_deferred_free, .sync = synchronize_rcu, + .cb_barrier = NULL, .stats = NULL, .name = "rcu_sync" };
@@ -363,6 +366,7 @@ static struct rcu_torture_ops rcu_bh_ops .completed = rcu_bh_torture_completed, .deferredfree = rcu_bh_torture_deferred_free, .sync = rcu_bh_torture_synchronize, + .cb_barrier = rcu_barrier_bh, .stats = NULL, .name = "rcu_bh" };
@@ -376,6 +380,7 @@ static struct rcu_torture_ops rcu_bh_syn .completed = rcu_bh_torture_completed, .deferredfree = rcu_sync_torture_deferred_free, .sync = rcu_bh_torture_synchronize, + .cb_barrier = NULL, .stats = NULL, .name = "rcu_bh_sync" };
@@ -457,6 +462,7 @@ static struct rcu_torture_ops srcu_ops = .completed = srcu_torture_completed, .deferredfree = rcu_sync_torture_deferred_free, .sync = srcu_torture_synchronize, + .cb_barrier = NULL, .stats = srcu_torture_stats, .name = "srcu" };
@@ -481,6 +487,11 @@ static int sched_torture_completed(void)return 0 ; } +static void rcu_sched_torture_deferred_free (struct rcu_torture *p) +{ +call_rcu_sched (&p->rtort_rcu, rcu_torture_cb); +} +static void sched_torture_synchronize (void ) {synchronize_sched ();
@@ -493,12 +504,27 @@ static struct rcu_torture_ops sched_ops .readdelay = rcu_read_delay, /* just reuse rcu's version. */ .readunlock = sched_torture_read_unlock, .completed = sched_torture_completed, - .deferredfree = rcu_sync_torture_deferred_free, + .deferredfree = rcu_sched_torture_deferred_free, .sync = sched_torture_synchronize, + .cb_barrier = rcu_barrier_sched, .stats = NULL, .name = "sched" }; +static struct rcu_torture_ops sched_ops_sync = { + .init = rcu_sync_torture_init, + .cleanup =NULL , + .readlock = sched_torture_read_lock, + .readdelay = rcu_read_delay,/* just reuse rcu's version. */ + .readunlock = sched_torture_read_unlock, + .completed = sched_torture_completed, + .deferredfree = rcu_sync_torture_deferred_free, + .sync = sched_torture_synchronize, + .cb_barrier =NULL , + .stats =NULL , + .name ="sched_sync" +}; +/* * RCU torture writer kthread. Repeatedly substitutes a new structure * for that pointed to by rcu_torture_current, freeing the old structure
@@ -844,7 +870,9 @@ rcu_torture_cleanup(void) stats_task =NULL ;/* Wait for all RCU callbacks to fire. */ -rcu_barrier (); + +if (cur_ops->cb_barrier !=NULL ) + cur_ops->cb_barrier ();rcu_torture_stats_print ();/* -After- the stats thread is stopped! */
@@ -864,7 +892,7 @@ rcu_torture_init(void)int firsterr =0 ;static struct rcu_torture_ops *torture_ops[] = { &rcu_ops, &rcu_sync_ops, &rcu_bh_ops, &rcu_bh_sync_ops, - &srcu_ops, &sched_ops, }; + &srcu_ops, &sched_ops, &sched_ops_sync, };/* Process args and tell the world that the torturer is on the job. */ for (i =0 ; i <ARRAY_SIZE (torture_ops); i++) {
PATCH 5/5 1Q08 RCU doc update, add call_rcu_sched()
In reply to Paul E. McKenneyLong-delayed update to the RCU documentation, including adding the new call_rcu_sched() and rcu_barrier_sched() APIs.
---
NMI-RCU.txt | 3 + RTFP.txt | 108 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ checklist.txt | 89 ++++++++++++++++++++++++++++++++--------------- whatisRCU.txt | 58 ++++++++++++++++++++----------- 4 files changed, 210 insertions(+), 48 deletions(-)
Documentation/RCU/checklist.txt +60 -29
@@ -13,10 +13,13 @@ over a rather long period of time, but i detailed performance measurements show that RCU is nonetheless the right tool for the job. - The other exception would be where performance is not an issue, - and RCU provides a simpler implementation. An example of this - situation is the dynamic NMI code in the Linux 2.6 kernel, - at least on architectures where NMIs are rare. + Another exception is where performance is not an issue, and RCU + provides a simpler implementation. An example of this situation + is the dynamic NMI code in the Linux 2.6 kernel, at least on + architectures where NMIs are rare. + + Yet another exception is where the low real-time latency of RCU's + read-side primitives is critically important. 1. Does the update code have proper mutual exclusion?
@@ -39,9 +42,10 @@ over a rather long period of time, but i 2. Do the RCU read-side critical sections make proper use of rcu_read_lock() and friends? These primitives are needed - to suppress preemption (or bottom halves, in the case of - rcu_read_lock_bh()) in the read-side critical sections, - and are also an excellent aid to readability. + to prevent grace periods from ending prematurely, which + could result in data being unceremoniously freed out from + under your read-side code, which can greatly increase the + actuarial risk of your kernel. As a rough rule of thumb, any dereference of an RCU-protected pointer must be covered by rcu_read_lock() or rcu_read_lock_bh()
@@ -54,15 +58,30 @@ over a rather long period of time, but i be running while updates are in progress. There are a number of ways to handle this concurrency, depending on the situation: - a. Make updates appear atomic to readers. For example, + a. Use the RCU variants of the list and hlist update + primitives to add, remove, and replace elements on an + RCU-protected list. Alternatively, use the RCU-protected + trees that have been added to the Linux kernel. + + This is almost always the best approach. + + b. Proceed as in (a) above, but also maintain per-element + locks (that are acquired by both readers and writers) + that guard per-element state. Of course, fields that + the readers refrain from accessing can be guarded by the + update-side lock. + + This works quite well, also. + + c. Make updates appear atomic to readers. For example, pointer updates to properly aligned fields will appear atomic, as will individual atomic primitives. Operations performed under a lock and sequences of multiple atomic primitives will -not- appear to be atomic. - This is almost always the best approach. + This can work, but is starting to get a bit tricky. - b. Carefully order the updates and the reads so that + d. Carefully order the updates and the reads so that readers see valid data at all phases of the update. This is often more difficult than it sounds, especially given modern CPUs' tendency to reorder memory references.
@@ -123,18 +142,22 @@ over a rather long period of time, but i when publicizing a pointer to a structure that can be traversed by an RCU read-side critical section. -5. If call_rcu(), or a related primitive such as call_rcu_bh(), - is used, the callback function must be written to be called - from softirq context. In particular, it cannot block. +5. If call_rcu(), or a related primitive such as call_rcu_bh() or + call_rcu_sched(), is used, the callback function must be + written to be called from softirq context. In particular, + it cannot block. 6. Since synchronize_rcu() can block, it cannot be called from - any sort of irq context. + any sort of irq context. Ditto for synchronize_sched() and + synchronize_srcu(). 7. If the updater uses call_rcu(), then the corresponding readers must use rcu_read_lock() and rcu_read_unlock(). If the updater uses call_rcu_bh(), then the corresponding readers must use - rcu_read_lock_bh() and rcu_read_unlock_bh(). Mixing things up - will result in confusion and broken kernels. + rcu_read_lock_bh() and rcu_read_unlock_bh(). If the updater + uses call_rcu_sched(), then the corresponding readers must + disable preemption. Mixing things up will result in confusion + and broken kernels. One exception to this rule: rcu_read_lock() and rcu_read_unlock() may be substituted for rcu_read_lock_bh() and rcu_read_unlock_bh()
@@ -143,9 +166,9 @@ over a rather long period of time, but i such cases is a must, of course! And the jury is still out on whether the increased speed is worth it. -8. Although synchronize_rcu() is a bit slower than is call_rcu(), - it usually results in simpler code. So, unless update - performance is critically important or the updaters cannot block, +8. Although synchronize_rcu() is slower than is call_rcu(), it + usually results in simpler code. So, unless update performance + is critically important or the updaters cannot block, synchronize_rcu() should be used in preference to call_rcu(). An especially important property of the synchronize_rcu()
@@ -187,23 +210,23 @@ over a rather long period of time, but i number of updates per grace period. 9. All RCU list-traversal primitives, which include - list_for_each_rcu(), list_for_each_entry_rcu(), + rcu_dereference(), list_for_each_rcu(), list_for_each_entry_rcu(), list_for_each_continue_rcu(), and list_for_each_safe_rcu(), - must be within an RCU read-side critical section. RCU + must be either within an RCU read-side critical section or + must be protected by appropriate update-side locks. RCU read-side critical sections are delimited by rcu_read_lock() and rcu_read_unlock(), or by similar primitives such as rcu_read_lock_bh() and rcu_read_unlock_bh(). - Use of the _rcu() list-traversal primitives outside of an - RCU read-side critical section causes no harm other than - a slight performance degradation on Alpha CPUs. It can - also be quite helpful in reducing code bloat when common - code is shared between readers and updaters. + The reason that it is permissible to use RCU list-traversal + primitives when the update-side lock is held is that doing so + can be quite helpful in reducing code bloat when common code is + shared between readers and updaters. 10. Conversely, if you are in an RCU read-side critical section, - you -must- use the "_rcu()" variants of the list macros. - Failing to do so will break Alpha and confuse people reading - your code. + and you don't hold the appropriate update-side lock, you -must- + use the "_rcu()" variants of the list macros. Failing to do so + will break Alpha and confuse people reading your code. 11. Note that synchronize_rcu() -only- guarantees to wait until all currently executing rcu_read_lock()-protected RCU read-side
@@ -230,6 +253,14 @@ over a rather long period of time, but i must use whatever locking or other synchronization is required to safely access and/or modify that data structure. + RCU callbacks are -usually- executed on the same CPU that executed + the corresponding call_rcu(), call_rcu_bh(), or call_rcu_sched(), + but are by -no- means guaranteed to be. For example, if a given + CPU goes offline while having an RCU callback pending, then that + RCU callback will execute on some surviving CPU. (If this was + not the case, a self-spawning RCU callback would prevent the + victim CPU from ever going offline.) + 14. SRCU (srcu_read_lock(), srcu_read_unlock(), and synchronize_srcu()) may only be invoked from process context. Unlike other forms of RCU, it -is- permissible to block in an SRCU read-side critical
Documentation/RCU/NMI-RCU.txt +3 -0
@@ -93,6 +93,9 @@ Since NMI handlers disable preemption, s not to return until all ongoing NMI handlers exit. It is therefore safe to free up the handler's data as soon as synchronize_sched() returns. +Important note: for this to work, the architecture in question must +invoke irq_enter() and irq_exit() on NMI entry and exit, respectively. + Answer to Quick Quiz
Documentation/RCU/RTFP.txt +108 -0
129 lines not on this page · raw message
Documentation/RCU/whatisRCU.txt +39 -19
84 lines not on this page · raw message
Quoted 3 lines · On Sun, 13 Apr 2008 20:58:48 -0700 "Paul E. McKenney"…
This series adds
call_rcu_sched(), applies some fixes to RCU Classic, addsrcu_barrier_sched(), updates rcutorture, and updates the RCU documentation.
Some massaging was needed here to make these apply against the RCU changes in git-sched.
All fairly simple, except I don't know if these:
static inline void rcu_enter_nohz(void)
{
smp_mb(); /* CPUs seeing ++ must see prior RCU read-side crit sects */
__get_cpu_var(rcu_dyntick_sched).dynticks++;
WARN_ON(__get_cpu_var(rcu_dyntick_sched).dynticks & 0x1);
}static inline void rcu_exit_nohz(void)
{
__get_cpu_var(rcu_dyntick_sched).dynticks++;
smp_mb(); /* CPUs seeing ++ must see later RCU read-side crit sects */
WARN_ON(!(__get_cpu_var(rcu_dyntick_sched).dynticks & 0x1));
}in include/linux/rcupreempt.h ended up the way we want them?
Quoted 18 lines · On Mon, 14 Apr 2008 18:33:12 -0700 Andrew Morton <[email protected]>…
Quoted 3 lines · On Sun, 13 Apr 2008 20:58:48 -0700 "Paul E. McKenney"…
This series adds
call_rcu_sched(), applies some fixes to RCU Classic, addsrcu_barrier_sched(), updates rcutorture, and updates the RCU documentation.Some massaging was needed here to make these apply against the RCU changes in git-sched.
All fairly simple, except I don't know if these:
static inline void
rcu_enter_nohz(void){ smp_mb(); /* CPUs seeing ++ must see prior RCU read-side crit sects */ __get_cpu_var(rcu_dyntick_sched).dynticks++; WARN_ON(__get_cpu_var(rcu_dyntick_sched).dynticks & 0x1); }
Quoted 8 lines
static inline void
rcu_exit_nohz(void){ __get_cpu_var(rcu_dyntick_sched).dynticks++; smp_mb(); /* CPUs seeing ++ must see later RCU read-side crit sects */ WARN_ON(!(__get_cpu_var(rcu_dyntick_sched).dynticks & 0x1)); }in
include/linux/rcupreempt.hended up the way we want them?
argh, I see. It's all tangled up with the WARN_ON_SECS stuff.
Let me try to straighten it all out.
Quoted 27 lines · On Mon, 14 Apr 2008 18:45:44 -0700 Andrew Morton <[email protected]>…
Quoted 25 lines · On Mon, 14 Apr 2008 18:33:12 -0700 Andrew Morton <[email protected]>…
Quoted 3 lines · On Sun, 13 Apr 2008 20:58:48 -0700 "Paul E. McKenney"…
This series adds
call_rcu_sched(), applies some fixes to RCU Classic, addsrcu_barrier_sched(), updates rcutorture, and updates the RCU documentation.Some massaging was needed here to make these apply against the RCU changes in git-sched.
All fairly simple, except I don't know if these:
static inline void
rcu_enter_nohz(void){ smp_mb(); /* CPUs seeing ++ must see prior RCU read-side crit sects */ __get_cpu_var(rcu_dyntick_sched).dynticks++; WARN_ON(__get_cpu_var(rcu_dyntick_sched).dynticks & 0x1); }static inline void
rcu_exit_nohz(void){ __get_cpu_var(rcu_dyntick_sched).dynticks++; smp_mb(); /* CPUs seeing ++ must see later RCU read-side crit sects */ WARN_ON(!(__get_cpu_var(rcu_dyntick_sched).dynticks & 0x1)); }
Quoted 3 lines
Quoted 1 line
in
include/linux/rcupreempt.hended up the way we want them?argh, I see. It's all tangled up with the
WARN_ON_SECSstuff.
No it isn't. Don't mind me.