blob: 195bf1fb59ea46b7af1826b63243e6ce8ada9cbe [file] [log] [blame]
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001/*
2 * Read-Copy Update mechanism for mutual exclusion
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 *
18 * Copyright IBM Corporation, 2008
19 *
20 * Authors: Dipankar Sarma <dipankar@in.ibm.com>
21 * Manfred Spraul <manfred@colorfullife.com>
22 * Paul E. McKenney <paulmck@linux.vnet.ibm.com> Hierarchical version
23 *
24 * Based on the original work by Paul McKenney <paulmck@us.ibm.com>
25 * and inputs from Rusty Russell, Andrea Arcangeli and Andi Kleen.
26 *
27 * For detailed explanation of Read-Copy Update mechanism see -
Paul E. McKenneya71fca52009-09-18 10:28:19 -070028 * Documentation/RCU
Paul E. McKenney64db4cf2008-12-18 21:55:32 +010029 */
30#include <linux/types.h>
31#include <linux/kernel.h>
32#include <linux/init.h>
33#include <linux/spinlock.h>
34#include <linux/smp.h>
35#include <linux/rcupdate.h>
36#include <linux/interrupt.h>
37#include <linux/sched.h>
Ingo Molnarc1dc0b92009-08-02 11:28:21 +020038#include <linux/nmi.h>
Paul E. McKenney8826f3b2011-05-11 05:41:41 -070039#include <linux/atomic.h>
Paul E. McKenney64db4cf2008-12-18 21:55:32 +010040#include <linux/bitops.h>
Paul Gortmaker9984de12011-05-23 14:51:41 -040041#include <linux/export.h>
Paul E. McKenney64db4cf2008-12-18 21:55:32 +010042#include <linux/completion.h>
43#include <linux/moduleparam.h>
44#include <linux/percpu.h>
45#include <linux/notifier.h>
46#include <linux/cpu.h>
47#include <linux/mutex.h>
48#include <linux/time.h>
Paul E. McKenneybbad9372010-04-02 16:17:17 -070049#include <linux/kernel_stat.h>
Paul E. McKenneya26ac242011-01-12 14:10:23 -080050#include <linux/wait.h>
51#include <linux/kthread.h>
Linus Torvalds268bb0c2011-05-20 12:50:29 -070052#include <linux/prefetch.h>
Paul E. McKenney64db4cf2008-12-18 21:55:32 +010053
Paul E. McKenney9f77da92009-08-22 13:56:45 -070054#include "rcutree.h"
Paul E. McKenney29c00b42011-06-17 15:53:19 -070055#include <trace/events/rcu.h>
56
57#include "rcu.h"
Paul E. McKenney9f77da92009-08-22 13:56:45 -070058
Paul E. McKenney64db4cf2008-12-18 21:55:32 +010059/* Data structures. */
60
Paul E. McKenneyb668c9c2009-11-22 08:53:48 -080061static struct lock_class_key rcu_node_class[NUM_RCU_LVLS];
Peter Zijlstra88b91c72009-10-26 10:24:31 -070062
Paul E. McKenney4300aa62010-04-13 16:18:22 -070063#define RCU_STATE_INITIALIZER(structname) { \
Paul E. McKenneye99033c2011-06-21 00:13:44 -070064 .level = { &structname##_state.node[0] }, \
Paul E. McKenney64db4cf2008-12-18 21:55:32 +010065 .levelcnt = { \
66 NUM_RCU_LVL_0, /* root of hierarchy. */ \
67 NUM_RCU_LVL_1, \
68 NUM_RCU_LVL_2, \
Paul E. McKenneycf244dc2009-12-02 12:10:14 -080069 NUM_RCU_LVL_3, \
70 NUM_RCU_LVL_4, /* == MAX_RCU_LVLS */ \
Paul E. McKenney64db4cf2008-12-18 21:55:32 +010071 }, \
Paul E. McKenneyaf446b72011-09-10 21:54:08 -070072 .fqs_state = RCU_GP_IDLE, \
Paul E. McKenney64db4cf2008-12-18 21:55:32 +010073 .gpnum = -300, \
74 .completed = -300, \
Paul E. McKenneye99033c2011-06-21 00:13:44 -070075 .onofflock = __RAW_SPIN_LOCK_UNLOCKED(&structname##_state.onofflock), \
76 .fqslock = __RAW_SPIN_LOCK_UNLOCKED(&structname##_state.fqslock), \
Paul E. McKenney64db4cf2008-12-18 21:55:32 +010077 .n_force_qs = 0, \
78 .n_force_qs_ngp = 0, \
Paul E. McKenney4300aa62010-04-13 16:18:22 -070079 .name = #structname, \
Paul E. McKenney64db4cf2008-12-18 21:55:32 +010080}
81
Paul E. McKenneye99033c2011-06-21 00:13:44 -070082struct rcu_state rcu_sched_state = RCU_STATE_INITIALIZER(rcu_sched);
Paul E. McKenneyd6714c22009-08-22 13:56:46 -070083DEFINE_PER_CPU(struct rcu_data, rcu_sched_data);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +010084
Paul E. McKenneye99033c2011-06-21 00:13:44 -070085struct rcu_state rcu_bh_state = RCU_STATE_INITIALIZER(rcu_bh);
Ingo Molnar6258c4f2009-03-25 16:42:24 +010086DEFINE_PER_CPU(struct rcu_data, rcu_bh_data);
Ingo Molnarb1f77b02009-03-13 03:20:49 +010087
Paul E. McKenney27f4d282011-02-07 12:47:15 -080088static struct rcu_state *rcu_state;
89
Paul E. McKenneyb0d30412011-07-10 15:57:35 -070090/*
91 * The rcu_scheduler_active variable transitions from zero to one just
92 * before the first task is spawned. So when this variable is zero, RCU
93 * can assume that there is but one task, allowing RCU to (for example)
94 * optimized synchronize_sched() to a simple barrier(). When this variable
95 * is one, RCU must actually do all the hard work required to detect real
96 * grace periods. This variable is also used to suppress boot-time false
97 * positives from lockdep-RCU error checking.
98 */
Paul E. McKenneybbad9372010-04-02 16:17:17 -070099int rcu_scheduler_active __read_mostly;
100EXPORT_SYMBOL_GPL(rcu_scheduler_active);
101
Paul E. McKenneyb0d30412011-07-10 15:57:35 -0700102/*
103 * The rcu_scheduler_fully_active variable transitions from zero to one
104 * during the early_initcall() processing, which is after the scheduler
105 * is capable of creating new tasks. So RCU processing (for example,
106 * creating tasks for RCU priority boosting) must be delayed until after
107 * rcu_scheduler_fully_active transitions from zero to one. We also
108 * currently delay invocation of any RCU callbacks until after this point.
109 *
110 * It might later prove better for people registering RCU callbacks during
111 * early boot to take responsibility for these callbacks, but one step at
112 * a time.
113 */
114static int rcu_scheduler_fully_active __read_mostly;
115
Paul E. McKenneya46e0892011-06-15 15:47:09 -0700116#ifdef CONFIG_RCU_BOOST
117
Ingo Molnarb1f77b02009-03-13 03:20:49 +0100118/*
Paul E. McKenneya26ac242011-01-12 14:10:23 -0800119 * Control variables for per-CPU and per-rcu_node kthreads. These
120 * handle all flavors of RCU.
121 */
122static DEFINE_PER_CPU(struct task_struct *, rcu_cpu_kthread_task);
Paul E. McKenneyd71df902011-03-29 17:48:28 -0700123DEFINE_PER_CPU(unsigned int, rcu_cpu_kthread_status);
Paul E. McKenney15ba0ba2011-04-06 16:01:16 -0700124DEFINE_PER_CPU(int, rcu_cpu_kthread_cpu);
Paul E. McKenney5ece5ba2011-04-22 18:08:51 -0700125DEFINE_PER_CPU(unsigned int, rcu_cpu_kthread_loops);
Paul E. McKenneyd71df902011-03-29 17:48:28 -0700126DEFINE_PER_CPU(char, rcu_cpu_has_work);
Paul E. McKenneya26ac242011-01-12 14:10:23 -0800127
Paul E. McKenneya46e0892011-06-15 15:47:09 -0700128#endif /* #ifdef CONFIG_RCU_BOOST */
129
Paul E. McKenney0f962a52011-04-14 12:13:53 -0700130static void rcu_node_kthread_setaffinity(struct rcu_node *rnp, int outgoingcpu);
Paul E. McKenneya46e0892011-06-15 15:47:09 -0700131static void invoke_rcu_core(void);
132static void invoke_rcu_callbacks(struct rcu_state *rsp, struct rcu_data *rdp);
Paul E. McKenneya26ac242011-01-12 14:10:23 -0800133
Paul E. McKenneya26ac242011-01-12 14:10:23 -0800134/*
Paul E. McKenney4a298652011-04-03 21:33:51 -0700135 * Track the rcutorture test sequence number and the update version
136 * number within a given test. The rcutorture_testseq is incremented
137 * on every rcutorture module load and unload, so has an odd value
138 * when a test is running. The rcutorture_vernum is set to zero
139 * when rcutorture starts and is incremented on each rcutorture update.
140 * These variables enable correlating rcutorture output with the
141 * RCU tracing information.
142 */
143unsigned long rcutorture_testseq;
144unsigned long rcutorture_vernum;
145
146/*
Paul E. McKenneyfc2219d2009-09-23 09:50:41 -0700147 * Return true if an RCU grace period is in progress. The ACCESS_ONCE()s
148 * permit this function to be invoked without holding the root rcu_node
149 * structure's ->lock, but of course results can be subject to change.
150 */
151static int rcu_gp_in_progress(struct rcu_state *rsp)
152{
153 return ACCESS_ONCE(rsp->completed) != ACCESS_ONCE(rsp->gpnum);
154}
155
156/*
Paul E. McKenneyd6714c22009-08-22 13:56:46 -0700157 * Note a quiescent state. Because we do not need to know
Ingo Molnarb1f77b02009-03-13 03:20:49 +0100158 * how many quiescent states passed, just if there was at least
Paul E. McKenneyd6714c22009-08-22 13:56:46 -0700159 * one since the start of the grace period, this just sets a flag.
Paul E. McKenneye4cc1f22011-06-27 00:17:43 -0700160 * The caller must have disabled preemption.
Ingo Molnarb1f77b02009-03-13 03:20:49 +0100161 */
Paul E. McKenneyd6714c22009-08-22 13:56:46 -0700162void rcu_sched_qs(int cpu)
Ingo Molnarb1f77b02009-03-13 03:20:49 +0100163{
Paul E. McKenney25502a62010-04-01 17:37:01 -0700164 struct rcu_data *rdp = &per_cpu(rcu_sched_data, cpu);
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700165
Paul E. McKenneye4cc1f22011-06-27 00:17:43 -0700166 rdp->passed_quiesce_gpnum = rdp->gpnum;
Paul E. McKenneyc3422be2009-09-13 09:15:10 -0700167 barrier();
Paul E. McKenneye4cc1f22011-06-27 00:17:43 -0700168 if (rdp->passed_quiesce == 0)
Paul E. McKenneyd4c08f22011-06-25 06:36:56 -0700169 trace_rcu_grace_period("rcu_sched", rdp->gpnum, "cpuqs");
Paul E. McKenneye4cc1f22011-06-27 00:17:43 -0700170 rdp->passed_quiesce = 1;
Ingo Molnarb1f77b02009-03-13 03:20:49 +0100171}
172
Paul E. McKenneyd6714c22009-08-22 13:56:46 -0700173void rcu_bh_qs(int cpu)
Ingo Molnarb1f77b02009-03-13 03:20:49 +0100174{
Paul E. McKenney25502a62010-04-01 17:37:01 -0700175 struct rcu_data *rdp = &per_cpu(rcu_bh_data, cpu);
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700176
Paul E. McKenneye4cc1f22011-06-27 00:17:43 -0700177 rdp->passed_quiesce_gpnum = rdp->gpnum;
Paul E. McKenneyc3422be2009-09-13 09:15:10 -0700178 barrier();
Paul E. McKenneye4cc1f22011-06-27 00:17:43 -0700179 if (rdp->passed_quiesce == 0)
Paul E. McKenneyd4c08f22011-06-25 06:36:56 -0700180 trace_rcu_grace_period("rcu_bh", rdp->gpnum, "cpuqs");
Paul E. McKenneye4cc1f22011-06-27 00:17:43 -0700181 rdp->passed_quiesce = 1;
Ingo Molnarb1f77b02009-03-13 03:20:49 +0100182}
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100183
Paul E. McKenney25502a62010-04-01 17:37:01 -0700184/*
185 * Note a context switch. This is a quiescent state for RCU-sched,
186 * and requires special handling for preemptible RCU.
Paul E. McKenneye4cc1f22011-06-27 00:17:43 -0700187 * The caller must have disabled preemption.
Paul E. McKenney25502a62010-04-01 17:37:01 -0700188 */
189void rcu_note_context_switch(int cpu)
190{
Paul E. McKenney300df912011-06-18 22:26:31 -0700191 trace_rcu_utilization("Start context switch");
Paul E. McKenney25502a62010-04-01 17:37:01 -0700192 rcu_sched_qs(cpu);
193 rcu_preempt_note_context_switch(cpu);
Paul E. McKenney300df912011-06-18 22:26:31 -0700194 trace_rcu_utilization("End context switch");
Paul E. McKenney25502a62010-04-01 17:37:01 -0700195}
Gleb Natapov29ce8312011-05-04 16:31:03 +0300196EXPORT_SYMBOL_GPL(rcu_note_context_switch);
Paul E. McKenney25502a62010-04-01 17:37:01 -0700197
Paul E. McKenney90a4d2c2009-01-04 11:41:11 -0800198DEFINE_PER_CPU(struct rcu_dynticks, rcu_dynticks) = {
Paul E. McKenney4145fa72011-10-31 15:01:54 -0700199 .dynticks_nesting = DYNTICK_TASK_NESTING,
Paul E. McKenney23b5c8f2010-09-07 10:38:22 -0700200 .dynticks = ATOMIC_INIT(1),
Paul E. McKenney90a4d2c2009-01-04 11:41:11 -0800201};
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100202
Paul E. McKenneye0f23062011-06-21 01:29:39 -0700203static int blimit = 10; /* Maximum callbacks per rcu_do_batch. */
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100204static int qhimark = 10000; /* If this many pending, ignore blimit. */
205static int qlowmark = 100; /* Once only this many pending, use blimit. */
206
Paul E. McKenney3d76c082009-09-28 07:46:32 -0700207module_param(blimit, int, 0);
208module_param(qhimark, int, 0);
209module_param(qlowmark, int, 0);
210
Paul E. McKenneya00e0d712011-02-08 17:14:39 -0800211int rcu_cpu_stall_suppress __read_mostly;
Paul E. McKenneyf2e0dd72010-07-14 14:38:30 -0700212module_param(rcu_cpu_stall_suppress, int, 0644);
Paul E. McKenney742734e2010-06-30 11:43:52 -0700213
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100214static void force_quiescent_state(struct rcu_state *rsp, int relaxed);
Paul E. McKenneya1572292009-08-22 13:56:51 -0700215static int rcu_pending(int cpu);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100216
217/*
Paul E. McKenneyd6714c22009-08-22 13:56:46 -0700218 * Return the number of RCU-sched batches processed thus far for debug & stats.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100219 */
Paul E. McKenneyd6714c22009-08-22 13:56:46 -0700220long rcu_batches_completed_sched(void)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100221{
Paul E. McKenneyd6714c22009-08-22 13:56:46 -0700222 return rcu_sched_state.completed;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100223}
Paul E. McKenneyd6714c22009-08-22 13:56:46 -0700224EXPORT_SYMBOL_GPL(rcu_batches_completed_sched);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100225
226/*
227 * Return the number of RCU BH batches processed thus far for debug & stats.
228 */
229long rcu_batches_completed_bh(void)
230{
231 return rcu_bh_state.completed;
232}
233EXPORT_SYMBOL_GPL(rcu_batches_completed_bh);
234
235/*
Paul E. McKenneybf66f182010-01-04 15:09:10 -0800236 * Force a quiescent state for RCU BH.
237 */
238void rcu_bh_force_quiescent_state(void)
239{
240 force_quiescent_state(&rcu_bh_state, 0);
241}
242EXPORT_SYMBOL_GPL(rcu_bh_force_quiescent_state);
243
244/*
Paul E. McKenney4a298652011-04-03 21:33:51 -0700245 * Record the number of times rcutorture tests have been initiated and
246 * terminated. This information allows the debugfs tracing stats to be
247 * correlated to the rcutorture messages, even when the rcutorture module
248 * is being repeatedly loaded and unloaded. In other words, we cannot
249 * store this state in rcutorture itself.
250 */
251void rcutorture_record_test_transition(void)
252{
253 rcutorture_testseq++;
254 rcutorture_vernum = 0;
255}
256EXPORT_SYMBOL_GPL(rcutorture_record_test_transition);
257
258/*
259 * Record the number of writer passes through the current rcutorture test.
260 * This is also used to correlate debugfs tracing stats with the rcutorture
261 * messages.
262 */
263void rcutorture_record_progress(unsigned long vernum)
264{
265 rcutorture_vernum++;
266}
267EXPORT_SYMBOL_GPL(rcutorture_record_progress);
268
269/*
Paul E. McKenneybf66f182010-01-04 15:09:10 -0800270 * Force a quiescent state for RCU-sched.
271 */
272void rcu_sched_force_quiescent_state(void)
273{
274 force_quiescent_state(&rcu_sched_state, 0);
275}
276EXPORT_SYMBOL_GPL(rcu_sched_force_quiescent_state);
277
278/*
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100279 * Does the CPU have callbacks ready to be invoked?
280 */
281static int
282cpu_has_callbacks_ready_to_invoke(struct rcu_data *rdp)
283{
284 return &rdp->nxtlist != rdp->nxttail[RCU_DONE_TAIL];
285}
286
287/*
288 * Does the current CPU require a yet-as-unscheduled grace period?
289 */
290static int
291cpu_needs_another_gp(struct rcu_state *rsp, struct rcu_data *rdp)
292{
Paul E. McKenneyfc2219d2009-09-23 09:50:41 -0700293 return *rdp->nxttail[RCU_DONE_TAIL] && !rcu_gp_in_progress(rsp);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100294}
295
296/*
297 * Return the root node of the specified rcu_state structure.
298 */
299static struct rcu_node *rcu_get_root(struct rcu_state *rsp)
300{
301 return &rsp->node[0];
302}
303
304#ifdef CONFIG_SMP
305
306/*
307 * If the specified CPU is offline, tell the caller that it is in
308 * a quiescent state. Otherwise, whack it with a reschedule IPI.
309 * Grace periods can end up waiting on an offline CPU when that
310 * CPU is in the process of coming online -- it will be added to the
311 * rcu_node bitmasks before it actually makes it online. The same thing
312 * can happen while a CPU is in the process of coming online. Because this
313 * race is quite rare, we check for it after detecting that the grace
314 * period has been delayed rather than checking each and every CPU
315 * each and every time we start a new grace period.
316 */
317static int rcu_implicit_offline_qs(struct rcu_data *rdp)
318{
319 /*
320 * If the CPU is offline, it is in a quiescent state. We can
321 * trust its state not to change because interrupts are disabled.
322 */
323 if (cpu_is_offline(rdp->cpu)) {
Paul E. McKenneyd4c08f22011-06-25 06:36:56 -0700324 trace_rcu_fqs(rdp->rsp->name, rdp->gpnum, rdp->cpu, "ofl");
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100325 rdp->offline_fqs++;
326 return 1;
327 }
328
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700329 /*
330 * The CPU is online, so send it a reschedule IPI. This forces
331 * it through the scheduler, and (inefficiently) also handles cases
332 * where idle loops fail to inform RCU about the CPU being idle.
333 */
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100334 if (rdp->cpu != smp_processor_id())
335 smp_send_reschedule(rdp->cpu);
336 else
337 set_need_resched();
338 rdp->resched_ipi++;
339 return 0;
340}
341
342#endif /* #ifdef CONFIG_SMP */
343
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700344/*
345 * rcu_idle_enter_common - inform RCU that current CPU is moving towards idle
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100346 *
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700347 * If the new value of the ->dynticks_nesting counter now is zero,
348 * we really have entered idle, and must do the appropriate accounting.
349 * The caller must have disabled interrupts.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100350 */
Paul E. McKenney4145fa72011-10-31 15:01:54 -0700351static void rcu_idle_enter_common(struct rcu_dynticks *rdtp, long long oldval)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100352{
Frederic Weisbeckerfacc4e12011-11-28 16:26:56 -0800353 trace_rcu_dyntick("Start", oldval, 0);
Paul E. McKenney99745b62011-11-10 15:48:45 -0800354 if (!is_idle_task(current)) {
Paul E. McKenney0989cb42011-11-01 08:57:21 -0700355 struct task_struct *idle = idle_task(smp_processor_id());
356
Frederic Weisbeckerfacc4e12011-11-28 16:26:56 -0800357 trace_rcu_dyntick("Error on entry: not idle task", oldval, 0);
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700358 ftrace_dump(DUMP_ALL);
Paul E. McKenney0989cb42011-11-01 08:57:21 -0700359 WARN_ONCE(1, "Current pid: %d comm: %s / Idle pid: %d comm: %s",
360 current->pid, current->comm,
361 idle->pid, idle->comm); /* must be idle task! */
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700362 }
Paul E. McKenneyaea1b352011-11-02 06:54:54 -0700363 rcu_prepare_for_idle(smp_processor_id());
Paul E. McKenney23b5c8f2010-09-07 10:38:22 -0700364 /* CPUs seeing atomic_inc() must see prior RCU read-side crit sects */
365 smp_mb__before_atomic_inc(); /* See above. */
366 atomic_inc(&rdtp->dynticks);
367 smp_mb__after_atomic_inc(); /* Force ordering with next sojourn. */
368 WARN_ON_ONCE(atomic_read(&rdtp->dynticks) & 0x1);
Paul E. McKenneyc44e2cd2012-01-12 13:08:18 -0800369
370 /*
371 * The idle task is not permitted to enter the idle loop while
372 * in an RCU read-side critical section.
373 */
374 rcu_lockdep_assert(!lock_is_held(&rcu_lock_map),
375 "Illegal idle entry in RCU read-side critical section.");
376 rcu_lockdep_assert(!lock_is_held(&rcu_bh_lock_map),
377 "Illegal idle entry in RCU-bh read-side critical section.");
378 rcu_lockdep_assert(!lock_is_held(&rcu_sched_lock_map),
379 "Illegal idle entry in RCU-sched read-side critical section.");
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100380}
381
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700382/**
383 * rcu_idle_enter - inform RCU that current CPU is entering idle
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100384 *
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700385 * Enter idle mode, in other words, -leave- the mode in which RCU
386 * read-side critical sections can occur. (Though RCU read-side
387 * critical sections can occur in irq handlers in idle, a possibility
388 * handled by irq_enter() and irq_exit().)
389 *
390 * We crowbar the ->dynticks_nesting field to zero to allow for
391 * the possibility of usermode upcalls having messed up our count
392 * of interrupt nesting level during the prior busy period.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100393 */
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700394void rcu_idle_enter(void)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100395{
396 unsigned long flags;
Paul E. McKenney4145fa72011-10-31 15:01:54 -0700397 long long oldval;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100398 struct rcu_dynticks *rdtp;
399
400 local_irq_save(flags);
401 rdtp = &__get_cpu_var(rcu_dynticks);
Paul E. McKenney4145fa72011-10-31 15:01:54 -0700402 oldval = rdtp->dynticks_nesting;
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700403 rdtp->dynticks_nesting = 0;
Paul E. McKenney4145fa72011-10-31 15:01:54 -0700404 rcu_idle_enter_common(rdtp, oldval);
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700405 local_irq_restore(flags);
406}
407
408/**
409 * rcu_irq_exit - inform RCU that current CPU is exiting irq towards idle
410 *
411 * Exit from an interrupt handler, which might possibly result in entering
412 * idle mode, in other words, leaving the mode in which read-side critical
413 * sections can occur.
414 *
415 * This code assumes that the idle loop never does anything that might
416 * result in unbalanced calls to irq_enter() and irq_exit(). If your
417 * architecture violates this assumption, RCU will give you what you
418 * deserve, good and hard. But very infrequently and irreproducibly.
419 *
420 * Use things like work queues to work around this limitation.
421 *
422 * You have been warned.
423 */
424void rcu_irq_exit(void)
425{
426 unsigned long flags;
Paul E. McKenney4145fa72011-10-31 15:01:54 -0700427 long long oldval;
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700428 struct rcu_dynticks *rdtp;
429
430 local_irq_save(flags);
431 rdtp = &__get_cpu_var(rcu_dynticks);
Paul E. McKenney4145fa72011-10-31 15:01:54 -0700432 oldval = rdtp->dynticks_nesting;
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700433 rdtp->dynticks_nesting--;
434 WARN_ON_ONCE(rdtp->dynticks_nesting < 0);
Frederic Weisbeckerb6fc6022011-11-28 16:18:56 -0800435 if (rdtp->dynticks_nesting)
436 trace_rcu_dyntick("--=", oldval, rdtp->dynticks_nesting);
437 else
438 rcu_idle_enter_common(rdtp, oldval);
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700439 local_irq_restore(flags);
440}
441
442/*
443 * rcu_idle_exit_common - inform RCU that current CPU is moving away from idle
444 *
445 * If the new value of the ->dynticks_nesting counter was previously zero,
446 * we really have exited idle, and must do the appropriate accounting.
447 * The caller must have disabled interrupts.
448 */
449static void rcu_idle_exit_common(struct rcu_dynticks *rdtp, long long oldval)
450{
Paul E. McKenney23b5c8f2010-09-07 10:38:22 -0700451 smp_mb__before_atomic_inc(); /* Force ordering w/previous sojourn. */
452 atomic_inc(&rdtp->dynticks);
453 /* CPUs seeing atomic_inc() must see later RCU read-side crit sects */
454 smp_mb__after_atomic_inc(); /* See above. */
455 WARN_ON_ONCE(!(atomic_read(&rdtp->dynticks) & 0x1));
Paul E. McKenney7cb92492011-11-28 12:28:34 -0800456 rcu_cleanup_after_idle(smp_processor_id());
Paul E. McKenney4145fa72011-10-31 15:01:54 -0700457 trace_rcu_dyntick("End", oldval, rdtp->dynticks_nesting);
Paul E. McKenney99745b62011-11-10 15:48:45 -0800458 if (!is_idle_task(current)) {
Paul E. McKenney0989cb42011-11-01 08:57:21 -0700459 struct task_struct *idle = idle_task(smp_processor_id());
460
Paul E. McKenney4145fa72011-10-31 15:01:54 -0700461 trace_rcu_dyntick("Error on exit: not idle task",
462 oldval, rdtp->dynticks_nesting);
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700463 ftrace_dump(DUMP_ALL);
Paul E. McKenney0989cb42011-11-01 08:57:21 -0700464 WARN_ONCE(1, "Current pid: %d comm: %s / Idle pid: %d comm: %s",
465 current->pid, current->comm,
466 idle->pid, idle->comm); /* must be idle task! */
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700467 }
468}
469
470/**
471 * rcu_idle_exit - inform RCU that current CPU is leaving idle
472 *
473 * Exit idle mode, in other words, -enter- the mode in which RCU
474 * read-side critical sections can occur.
475 *
Paul E. McKenney4145fa72011-10-31 15:01:54 -0700476 * We crowbar the ->dynticks_nesting field to DYNTICK_TASK_NESTING to
477 * allow for the possibility of usermode upcalls messing up our count
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700478 * of interrupt nesting level during the busy period that is just
479 * now starting.
480 */
481void rcu_idle_exit(void)
482{
483 unsigned long flags;
484 struct rcu_dynticks *rdtp;
485 long long oldval;
486
487 local_irq_save(flags);
488 rdtp = &__get_cpu_var(rcu_dynticks);
489 oldval = rdtp->dynticks_nesting;
490 WARN_ON_ONCE(oldval != 0);
Paul E. McKenney4145fa72011-10-31 15:01:54 -0700491 rdtp->dynticks_nesting = DYNTICK_TASK_NESTING;
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700492 rcu_idle_exit_common(rdtp, oldval);
493 local_irq_restore(flags);
494}
495
496/**
497 * rcu_irq_enter - inform RCU that current CPU is entering irq away from idle
498 *
499 * Enter an interrupt handler, which might possibly result in exiting
500 * idle mode, in other words, entering the mode in which read-side critical
501 * sections can occur.
502 *
503 * Note that the Linux kernel is fully capable of entering an interrupt
504 * handler that it never exits, for example when doing upcalls to
505 * user mode! This code assumes that the idle loop never does upcalls to
506 * user mode. If your architecture does do upcalls from the idle loop (or
507 * does anything else that results in unbalanced calls to the irq_enter()
508 * and irq_exit() functions), RCU will give you what you deserve, good
509 * and hard. But very infrequently and irreproducibly.
510 *
511 * Use things like work queues to work around this limitation.
512 *
513 * You have been warned.
514 */
515void rcu_irq_enter(void)
516{
517 unsigned long flags;
518 struct rcu_dynticks *rdtp;
519 long long oldval;
520
521 local_irq_save(flags);
522 rdtp = &__get_cpu_var(rcu_dynticks);
523 oldval = rdtp->dynticks_nesting;
524 rdtp->dynticks_nesting++;
525 WARN_ON_ONCE(rdtp->dynticks_nesting == 0);
Frederic Weisbeckerb6fc6022011-11-28 16:18:56 -0800526 if (oldval)
527 trace_rcu_dyntick("++=", oldval, rdtp->dynticks_nesting);
528 else
529 rcu_idle_exit_common(rdtp, oldval);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100530 local_irq_restore(flags);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100531}
532
533/**
534 * rcu_nmi_enter - inform RCU of entry to NMI context
535 *
536 * If the CPU was idle with dynamic ticks active, and there is no
537 * irq handler running, this updates rdtp->dynticks_nmi to let the
538 * RCU grace-period handling know that the CPU is active.
539 */
540void rcu_nmi_enter(void)
541{
542 struct rcu_dynticks *rdtp = &__get_cpu_var(rcu_dynticks);
543
Paul E. McKenney23b5c8f2010-09-07 10:38:22 -0700544 if (rdtp->dynticks_nmi_nesting == 0 &&
545 (atomic_read(&rdtp->dynticks) & 0x1))
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100546 return;
Paul E. McKenney23b5c8f2010-09-07 10:38:22 -0700547 rdtp->dynticks_nmi_nesting++;
548 smp_mb__before_atomic_inc(); /* Force delay from prior write. */
549 atomic_inc(&rdtp->dynticks);
550 /* CPUs seeing atomic_inc() must see later RCU read-side crit sects */
551 smp_mb__after_atomic_inc(); /* See above. */
552 WARN_ON_ONCE(!(atomic_read(&rdtp->dynticks) & 0x1));
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100553}
554
555/**
556 * rcu_nmi_exit - inform RCU of exit from NMI context
557 *
558 * If the CPU was idle with dynamic ticks active, and there is no
559 * irq handler running, this updates rdtp->dynticks_nmi to let the
560 * RCU grace-period handling know that the CPU is no longer active.
561 */
562void rcu_nmi_exit(void)
563{
564 struct rcu_dynticks *rdtp = &__get_cpu_var(rcu_dynticks);
565
Paul E. McKenney23b5c8f2010-09-07 10:38:22 -0700566 if (rdtp->dynticks_nmi_nesting == 0 ||
567 --rdtp->dynticks_nmi_nesting != 0)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100568 return;
Paul E. McKenney23b5c8f2010-09-07 10:38:22 -0700569 /* CPUs seeing atomic_inc() must see prior RCU read-side crit sects */
570 smp_mb__before_atomic_inc(); /* See above. */
571 atomic_inc(&rdtp->dynticks);
572 smp_mb__after_atomic_inc(); /* Force delay to next write. */
573 WARN_ON_ONCE(atomic_read(&rdtp->dynticks) & 0x1);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100574}
575
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700576#ifdef CONFIG_PROVE_RCU
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100577
578/**
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700579 * rcu_is_cpu_idle - see if RCU thinks that the current CPU is idle
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100580 *
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700581 * If the current CPU is in its idle loop and is neither in an interrupt
Paul E. McKenney34240692011-10-03 11:38:52 -0700582 * or NMI handler, return true.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100583 */
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700584int rcu_is_cpu_idle(void)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100585{
Paul E. McKenney34240692011-10-03 11:38:52 -0700586 int ret;
587
588 preempt_disable();
589 ret = (atomic_read(&__get_cpu_var(rcu_dynticks).dynticks) & 0x1) == 0;
590 preempt_enable();
591 return ret;
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700592}
Frederic Weisbeckere6b80a32011-10-07 16:25:18 -0700593EXPORT_SYMBOL(rcu_is_cpu_idle);
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700594
595#endif /* #ifdef CONFIG_PROVE_RCU */
596
597/**
598 * rcu_is_cpu_rrupt_from_idle - see if idle or immediately interrupted from idle
599 *
600 * If the current CPU is idle or running at a first-level (not nested)
601 * interrupt from idle, return true. The caller must have at least
602 * disabled preemption.
603 */
604int rcu_is_cpu_rrupt_from_idle(void)
605{
606 return __get_cpu_var(rcu_dynticks).dynticks_nesting <= 1;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100607}
608
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100609#ifdef CONFIG_SMP
610
611/*
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100612 * Snapshot the specified CPU's dynticks counter so that we can later
613 * credit them with an implicit quiescent state. Return 1 if this CPU
Paul E. McKenney1eba8f82009-09-23 09:50:42 -0700614 * is in dynticks idle mode, which is an extended quiescent state.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100615 */
616static int dyntick_save_progress_counter(struct rcu_data *rdp)
617{
Paul E. McKenney23b5c8f2010-09-07 10:38:22 -0700618 rdp->dynticks_snap = atomic_add_return(0, &rdp->dynticks->dynticks);
Paul E. McKenneyf0e7c192011-11-23 15:02:05 -0800619 return (rdp->dynticks_snap & 0x1) == 0;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100620}
621
622/*
623 * Return true if the specified CPU has passed through a quiescent
624 * state by virtue of being in or having passed through an dynticks
625 * idle state since the last call to dyntick_save_progress_counter()
626 * for this same CPU.
627 */
628static int rcu_implicit_dynticks_qs(struct rcu_data *rdp)
629{
Paul E. McKenney7eb4f452011-07-30 07:32:48 -0700630 unsigned int curr;
631 unsigned int snap;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100632
Paul E. McKenney7eb4f452011-07-30 07:32:48 -0700633 curr = (unsigned int)atomic_add_return(0, &rdp->dynticks->dynticks);
634 snap = (unsigned int)rdp->dynticks_snap;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100635
636 /*
637 * If the CPU passed through or entered a dynticks idle phase with
638 * no active irq/NMI handlers, then we can safely pretend that the CPU
639 * already acknowledged the request to pass through a quiescent
640 * state. Either way, that CPU cannot possibly be in an RCU
641 * read-side critical section that started before the beginning
642 * of the current RCU grace period.
643 */
Paul E. McKenney7eb4f452011-07-30 07:32:48 -0700644 if ((curr & 0x1) == 0 || UINT_CMP_GE(curr, snap + 2)) {
Paul E. McKenneyd4c08f22011-06-25 06:36:56 -0700645 trace_rcu_fqs(rdp->rsp->name, rdp->gpnum, rdp->cpu, "dti");
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100646 rdp->dynticks_fqs++;
647 return 1;
648 }
649
650 /* Go check for the CPU being offline. */
651 return rcu_implicit_offline_qs(rdp);
652}
653
654#endif /* #ifdef CONFIG_SMP */
655
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100656static void record_gp_stall_check_time(struct rcu_state *rsp)
657{
658 rsp->gp_start = jiffies;
659 rsp->jiffies_stall = jiffies + RCU_SECONDS_TILL_STALL_CHECK;
660}
661
662static void print_other_cpu_stall(struct rcu_state *rsp)
663{
664 int cpu;
665 long delta;
666 unsigned long flags;
Paul E. McKenney9bc8b552011-08-13 13:31:47 -0700667 int ndetected;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100668 struct rcu_node *rnp = rcu_get_root(rsp);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100669
670 /* Only let one CPU complain about others per time interval. */
671
Paul E. McKenney1304afb2010-02-22 17:05:02 -0800672 raw_spin_lock_irqsave(&rnp->lock, flags);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100673 delta = jiffies - rsp->jiffies_stall;
Paul E. McKenneyfc2219d2009-09-23 09:50:41 -0700674 if (delta < RCU_STALL_RAT_DELAY || !rcu_gp_in_progress(rsp)) {
Paul E. McKenney1304afb2010-02-22 17:05:02 -0800675 raw_spin_unlock_irqrestore(&rnp->lock, flags);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100676 return;
677 }
678 rsp->jiffies_stall = jiffies + RCU_SECONDS_TILL_STALL_RECHECK;
Paul E. McKenneya0b6c9a2009-09-28 07:46:33 -0700679
680 /*
681 * Now rat on any tasks that got kicked up to the root rcu_node
682 * due to CPU offlining.
683 */
Paul E. McKenney9bc8b552011-08-13 13:31:47 -0700684 ndetected = rcu_print_task_stall(rnp);
Paul E. McKenney1304afb2010-02-22 17:05:02 -0800685 raw_spin_unlock_irqrestore(&rnp->lock, flags);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100686
Paul E. McKenney8cdd32a2010-08-09 14:23:03 -0700687 /*
688 * OK, time to rat on our buddy...
689 * See Documentation/RCU/stallwarn.txt for info on how to debug
690 * RCU CPU stall warnings.
691 */
Paul E. McKenney4300aa62010-04-13 16:18:22 -0700692 printk(KERN_ERR "INFO: %s detected stalls on CPUs/tasks: {",
693 rsp->name);
Paul E. McKenneya0b6c9a2009-09-28 07:46:33 -0700694 rcu_for_each_leaf_node(rsp, rnp) {
Paul E. McKenney3acd9eb2010-02-22 17:05:03 -0800695 raw_spin_lock_irqsave(&rnp->lock, flags);
Paul E. McKenney9bc8b552011-08-13 13:31:47 -0700696 ndetected += rcu_print_task_stall(rnp);
Paul E. McKenney3acd9eb2010-02-22 17:05:03 -0800697 raw_spin_unlock_irqrestore(&rnp->lock, flags);
Paul E. McKenneya0b6c9a2009-09-28 07:46:33 -0700698 if (rnp->qsmask == 0)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100699 continue;
Paul E. McKenneya0b6c9a2009-09-28 07:46:33 -0700700 for (cpu = 0; cpu <= rnp->grphi - rnp->grplo; cpu++)
Paul E. McKenney9bc8b552011-08-13 13:31:47 -0700701 if (rnp->qsmask & (1UL << cpu)) {
Paul E. McKenneya0b6c9a2009-09-28 07:46:33 -0700702 printk(" %d", rnp->grplo + cpu);
Paul E. McKenney9bc8b552011-08-13 13:31:47 -0700703 ndetected++;
704 }
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100705 }
Paul E. McKenney4300aa62010-04-13 16:18:22 -0700706 printk("} (detected by %d, t=%ld jiffies)\n",
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100707 smp_processor_id(), (long)(jiffies - rsp->gp_start));
Paul E. McKenney9bc8b552011-08-13 13:31:47 -0700708 if (ndetected == 0)
709 printk(KERN_ERR "INFO: Stall ended before state dump start\n");
710 else if (!trigger_all_cpu_backtrace())
Paul E. McKenney4627e242011-08-03 03:34:24 -0700711 dump_stack();
Ingo Molnarc1dc0b92009-08-02 11:28:21 +0200712
Paul E. McKenney1ed509a2010-02-22 17:05:05 -0800713 /* If so configured, complain about tasks blocking the grace period. */
714
715 rcu_print_detail_task_stall(rsp);
716
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100717 force_quiescent_state(rsp, 0); /* Kick them all. */
718}
719
720static void print_cpu_stall(struct rcu_state *rsp)
721{
722 unsigned long flags;
723 struct rcu_node *rnp = rcu_get_root(rsp);
724
Paul E. McKenney8cdd32a2010-08-09 14:23:03 -0700725 /*
726 * OK, time to rat on ourselves...
727 * See Documentation/RCU/stallwarn.txt for info on how to debug
728 * RCU CPU stall warnings.
729 */
Paul E. McKenney4300aa62010-04-13 16:18:22 -0700730 printk(KERN_ERR "INFO: %s detected stall on CPU %d (t=%lu jiffies)\n",
731 rsp->name, smp_processor_id(), jiffies - rsp->gp_start);
Paul E. McKenney4627e242011-08-03 03:34:24 -0700732 if (!trigger_all_cpu_backtrace())
733 dump_stack();
Ingo Molnarc1dc0b92009-08-02 11:28:21 +0200734
Paul E. McKenney1304afb2010-02-22 17:05:02 -0800735 raw_spin_lock_irqsave(&rnp->lock, flags);
Paul E. McKenney20133cf2010-02-22 17:05:01 -0800736 if (ULONG_CMP_GE(jiffies, rsp->jiffies_stall))
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100737 rsp->jiffies_stall =
738 jiffies + RCU_SECONDS_TILL_STALL_RECHECK;
Paul E. McKenney1304afb2010-02-22 17:05:02 -0800739 raw_spin_unlock_irqrestore(&rnp->lock, flags);
Ingo Molnarc1dc0b92009-08-02 11:28:21 +0200740
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100741 set_need_resched(); /* kick ourselves to get things going. */
742}
743
744static void check_cpu_stall(struct rcu_state *rsp, struct rcu_data *rdp)
745{
Paul E. McKenneybad6e132011-05-02 23:40:04 -0700746 unsigned long j;
747 unsigned long js;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100748 struct rcu_node *rnp;
749
Paul E. McKenney742734e2010-06-30 11:43:52 -0700750 if (rcu_cpu_stall_suppress)
Paul E. McKenneyc68de202010-04-15 10:12:40 -0700751 return;
Paul E. McKenneybad6e132011-05-02 23:40:04 -0700752 j = ACCESS_ONCE(jiffies);
753 js = ACCESS_ONCE(rsp->jiffies_stall);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100754 rnp = rdp->mynode;
Paul E. McKenneybad6e132011-05-02 23:40:04 -0700755 if ((ACCESS_ONCE(rnp->qsmask) & rdp->grpmask) && ULONG_CMP_GE(j, js)) {
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100756
757 /* We haven't checked in, so go dump stack. */
758 print_cpu_stall(rsp);
759
Paul E. McKenneybad6e132011-05-02 23:40:04 -0700760 } else if (rcu_gp_in_progress(rsp) &&
761 ULONG_CMP_GE(j, js + RCU_STALL_RAT_DELAY)) {
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100762
Paul E. McKenneybad6e132011-05-02 23:40:04 -0700763 /* They had a few time units to dump stack, so complain. */
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100764 print_other_cpu_stall(rsp);
765 }
766}
767
Paul E. McKenneyc68de202010-04-15 10:12:40 -0700768static int rcu_panic(struct notifier_block *this, unsigned long ev, void *ptr)
769{
Paul E. McKenney742734e2010-06-30 11:43:52 -0700770 rcu_cpu_stall_suppress = 1;
Paul E. McKenneyc68de202010-04-15 10:12:40 -0700771 return NOTIFY_DONE;
772}
773
Paul E. McKenney53d84e02010-08-10 14:28:53 -0700774/**
775 * rcu_cpu_stall_reset - prevent further stall warnings in current grace period
776 *
777 * Set the stall-warning timeout way off into the future, thus preventing
778 * any RCU CPU stall-warning messages from appearing in the current set of
779 * RCU grace periods.
780 *
781 * The caller must disable hard irqs.
782 */
783void rcu_cpu_stall_reset(void)
784{
785 rcu_sched_state.jiffies_stall = jiffies + ULONG_MAX / 2;
786 rcu_bh_state.jiffies_stall = jiffies + ULONG_MAX / 2;
787 rcu_preempt_stall_reset();
788}
789
Paul E. McKenneyc68de202010-04-15 10:12:40 -0700790static struct notifier_block rcu_panic_block = {
791 .notifier_call = rcu_panic,
792};
793
794static void __init check_cpu_stall_init(void)
795{
796 atomic_notifier_chain_register(&panic_notifier_list, &rcu_panic_block);
797}
798
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100799/*
800 * Update CPU-local rcu_data state to record the newly noticed grace period.
801 * This is used both when we started the grace period and when we notice
Paul E. McKenney91603062009-11-02 13:52:29 -0800802 * that someone else started the grace period. The caller must hold the
803 * ->lock of the leaf rcu_node structure corresponding to the current CPU,
804 * and must have irqs disabled.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100805 */
Paul E. McKenney91603062009-11-02 13:52:29 -0800806static void __note_new_gpnum(struct rcu_state *rsp, struct rcu_node *rnp, struct rcu_data *rdp)
807{
808 if (rdp->gpnum != rnp->gpnum) {
Paul E. McKenney121dfc42010-12-10 15:02:47 -0800809 /*
810 * If the current grace period is waiting for this CPU,
811 * set up to detect a quiescent state, otherwise don't
812 * go looking for one.
813 */
Paul E. McKenney91603062009-11-02 13:52:29 -0800814 rdp->gpnum = rnp->gpnum;
Paul E. McKenneyd4c08f22011-06-25 06:36:56 -0700815 trace_rcu_grace_period(rsp->name, rdp->gpnum, "cpustart");
Paul E. McKenney121dfc42010-12-10 15:02:47 -0800816 if (rnp->qsmask & rdp->grpmask) {
817 rdp->qs_pending = 1;
Paul E. McKenneye4cc1f22011-06-27 00:17:43 -0700818 rdp->passed_quiesce = 0;
Paul E. McKenney121dfc42010-12-10 15:02:47 -0800819 } else
820 rdp->qs_pending = 0;
Paul E. McKenney91603062009-11-02 13:52:29 -0800821 }
822}
823
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100824static void note_new_gpnum(struct rcu_state *rsp, struct rcu_data *rdp)
825{
Paul E. McKenney91603062009-11-02 13:52:29 -0800826 unsigned long flags;
827 struct rcu_node *rnp;
828
829 local_irq_save(flags);
830 rnp = rdp->mynode;
831 if (rdp->gpnum == ACCESS_ONCE(rnp->gpnum) || /* outside lock. */
Paul E. McKenney1304afb2010-02-22 17:05:02 -0800832 !raw_spin_trylock(&rnp->lock)) { /* irqs already off, so later. */
Paul E. McKenney91603062009-11-02 13:52:29 -0800833 local_irq_restore(flags);
834 return;
835 }
836 __note_new_gpnum(rsp, rnp, rdp);
Paul E. McKenney1304afb2010-02-22 17:05:02 -0800837 raw_spin_unlock_irqrestore(&rnp->lock, flags);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100838}
839
840/*
841 * Did someone else start a new RCU grace period start since we last
842 * checked? Update local state appropriately if so. Must be called
843 * on the CPU corresponding to rdp.
844 */
845static int
846check_for_new_grace_period(struct rcu_state *rsp, struct rcu_data *rdp)
847{
848 unsigned long flags;
849 int ret = 0;
850
851 local_irq_save(flags);
852 if (rdp->gpnum != rsp->gpnum) {
853 note_new_gpnum(rsp, rdp);
854 ret = 1;
855 }
856 local_irq_restore(flags);
857 return ret;
858}
859
860/*
Paul E. McKenneyd09b62d2009-11-02 13:52:28 -0800861 * Advance this CPU's callbacks, but only if the current grace period
862 * has ended. This may be called only from the CPU to whom the rdp
863 * belongs. In addition, the corresponding leaf rcu_node structure's
864 * ->lock must be held by the caller, with irqs disabled.
865 */
866static void
867__rcu_process_gp_end(struct rcu_state *rsp, struct rcu_node *rnp, struct rcu_data *rdp)
868{
869 /* Did another grace period end? */
870 if (rdp->completed != rnp->completed) {
871
872 /* Advance callbacks. No harm if list empty. */
873 rdp->nxttail[RCU_DONE_TAIL] = rdp->nxttail[RCU_WAIT_TAIL];
874 rdp->nxttail[RCU_WAIT_TAIL] = rdp->nxttail[RCU_NEXT_READY_TAIL];
875 rdp->nxttail[RCU_NEXT_READY_TAIL] = rdp->nxttail[RCU_NEXT_TAIL];
876
877 /* Remember that we saw this grace-period completion. */
878 rdp->completed = rnp->completed;
Paul E. McKenneyd4c08f22011-06-25 06:36:56 -0700879 trace_rcu_grace_period(rsp->name, rdp->gpnum, "cpuend");
Frederic Weisbecker20377f32010-12-10 22:11:10 +0100880
881 /*
Frederic Weisbecker5ff8e6f2010-12-10 22:11:11 +0100882 * If we were in an extended quiescent state, we may have
Paul E. McKenney121dfc42010-12-10 15:02:47 -0800883 * missed some grace periods that others CPUs handled on
Frederic Weisbecker5ff8e6f2010-12-10 22:11:11 +0100884 * our behalf. Catch up with this state to avoid noting
Paul E. McKenney121dfc42010-12-10 15:02:47 -0800885 * spurious new grace periods. If another grace period
886 * has started, then rnp->gpnum will have advanced, so
887 * we will detect this later on.
Frederic Weisbecker5ff8e6f2010-12-10 22:11:11 +0100888 */
Paul E. McKenney121dfc42010-12-10 15:02:47 -0800889 if (ULONG_CMP_LT(rdp->gpnum, rdp->completed))
Frederic Weisbecker5ff8e6f2010-12-10 22:11:11 +0100890 rdp->gpnum = rdp->completed;
891
892 /*
Paul E. McKenney121dfc42010-12-10 15:02:47 -0800893 * If RCU does not need a quiescent state from this CPU,
894 * then make sure that this CPU doesn't go looking for one.
Frederic Weisbecker20377f32010-12-10 22:11:10 +0100895 */
Paul E. McKenney121dfc42010-12-10 15:02:47 -0800896 if ((rnp->qsmask & rdp->grpmask) == 0)
Frederic Weisbecker20377f32010-12-10 22:11:10 +0100897 rdp->qs_pending = 0;
Paul E. McKenneyd09b62d2009-11-02 13:52:28 -0800898 }
899}
900
901/*
902 * Advance this CPU's callbacks, but only if the current grace period
903 * has ended. This may be called only from the CPU to whom the rdp
904 * belongs.
905 */
906static void
907rcu_process_gp_end(struct rcu_state *rsp, struct rcu_data *rdp)
908{
909 unsigned long flags;
910 struct rcu_node *rnp;
911
912 local_irq_save(flags);
913 rnp = rdp->mynode;
914 if (rdp->completed == ACCESS_ONCE(rnp->completed) || /* outside lock. */
Paul E. McKenney1304afb2010-02-22 17:05:02 -0800915 !raw_spin_trylock(&rnp->lock)) { /* irqs already off, so later. */
Paul E. McKenneyd09b62d2009-11-02 13:52:28 -0800916 local_irq_restore(flags);
917 return;
918 }
919 __rcu_process_gp_end(rsp, rnp, rdp);
Paul E. McKenney1304afb2010-02-22 17:05:02 -0800920 raw_spin_unlock_irqrestore(&rnp->lock, flags);
Paul E. McKenneyd09b62d2009-11-02 13:52:28 -0800921}
922
923/*
924 * Do per-CPU grace-period initialization for running CPU. The caller
925 * must hold the lock of the leaf rcu_node structure corresponding to
926 * this CPU.
927 */
928static void
929rcu_start_gp_per_cpu(struct rcu_state *rsp, struct rcu_node *rnp, struct rcu_data *rdp)
930{
931 /* Prior grace period ended, so advance callbacks for current CPU. */
932 __rcu_process_gp_end(rsp, rnp, rdp);
933
934 /*
935 * Because this CPU just now started the new grace period, we know
936 * that all of its callbacks will be covered by this upcoming grace
937 * period, even the ones that were registered arbitrarily recently.
938 * Therefore, advance all outstanding callbacks to RCU_WAIT_TAIL.
939 *
940 * Other CPUs cannot be sure exactly when the grace period started.
941 * Therefore, their recently registered callbacks must pass through
942 * an additional RCU_NEXT_READY stage, so that they will be handled
943 * by the next RCU grace period.
944 */
945 rdp->nxttail[RCU_NEXT_READY_TAIL] = rdp->nxttail[RCU_NEXT_TAIL];
946 rdp->nxttail[RCU_WAIT_TAIL] = rdp->nxttail[RCU_NEXT_TAIL];
Paul E. McKenney91603062009-11-02 13:52:29 -0800947
948 /* Set state so that this CPU will detect the next quiescent state. */
949 __note_new_gpnum(rsp, rnp, rdp);
Paul E. McKenneyd09b62d2009-11-02 13:52:28 -0800950}
951
952/*
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100953 * Start a new RCU grace period if warranted, re-initializing the hierarchy
954 * in preparation for detecting the next grace period. The caller must hold
955 * the root node's ->lock, which is released before return. Hard irqs must
956 * be disabled.
Paul E. McKenneye5601402012-01-07 11:03:57 -0800957 *
958 * Note that it is legal for a dying CPU (which is marked as offline) to
959 * invoke this function. This can happen when the dying CPU reports its
960 * quiescent state.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100961 */
962static void
963rcu_start_gp(struct rcu_state *rsp, unsigned long flags)
964 __releases(rcu_get_root(rsp)->lock)
965{
Lai Jiangshan394f99a2010-06-28 16:25:04 +0800966 struct rcu_data *rdp = this_cpu_ptr(rsp->rda);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100967 struct rcu_node *rnp = rcu_get_root(rsp);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100968
Paul E. McKenney037067a2011-08-07 20:26:31 -0700969 if (!rcu_scheduler_fully_active ||
Paul E. McKenneyafe24b12011-08-24 16:52:09 -0700970 !cpu_needs_another_gp(rsp, rdp)) {
Paul E. McKenneyb32e9eb2009-11-12 22:35:03 -0800971 /*
Paul E. McKenneyafe24b12011-08-24 16:52:09 -0700972 * Either the scheduler hasn't yet spawned the first
973 * non-idle task or this CPU does not need another
974 * grace period. Either way, don't start a new grace
975 * period.
Paul E. McKenneyb32e9eb2009-11-12 22:35:03 -0800976 */
Paul E. McKenneyafe24b12011-08-24 16:52:09 -0700977 raw_spin_unlock_irqrestore(&rnp->lock, flags);
978 return;
979 }
980
981 if (rsp->fqs_active) {
982 /*
983 * This CPU needs a grace period, but force_quiescent_state()
984 * is running. Tell it to start one on this CPU's behalf.
985 */
986 rsp->fqs_need_gp = 1;
987 raw_spin_unlock_irqrestore(&rnp->lock, flags);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100988 return;
989 }
990
991 /* Advance to a new grace period and initialize state. */
992 rsp->gpnum++;
Paul E. McKenneyd4c08f22011-06-25 06:36:56 -0700993 trace_rcu_grace_period(rsp->name, rsp->gpnum, "start");
Paul E. McKenneyaf446b72011-09-10 21:54:08 -0700994 WARN_ON_ONCE(rsp->fqs_state == RCU_GP_INIT);
995 rsp->fqs_state = RCU_GP_INIT; /* Hold off force_quiescent_state. */
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100996 rsp->jiffies_force_qs = jiffies + RCU_JIFFIES_TILL_FORCE_QS;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100997 record_gp_stall_check_time(rsp);
Paul E. McKenney1304afb2010-02-22 17:05:02 -0800998 raw_spin_unlock(&rnp->lock); /* leave irqs disabled. */
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100999
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001000 /* Exclude any concurrent CPU-hotplug operations. */
Paul E. McKenney1304afb2010-02-22 17:05:02 -08001001 raw_spin_lock(&rsp->onofflock); /* irqs already disabled. */
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001002
1003 /*
Paul E. McKenneyb835db12009-09-08 15:54:37 -07001004 * Set the quiescent-state-needed bits in all the rcu_node
1005 * structures for all currently online CPUs in breadth-first
1006 * order, starting from the root rcu_node structure. This
1007 * operation relies on the layout of the hierarchy within the
1008 * rsp->node[] array. Note that other CPUs will access only
1009 * the leaves of the hierarchy, which still indicate that no
1010 * grace period is in progress, at least until the corresponding
1011 * leaf node has been initialized. In addition, we have excluded
1012 * CPU-hotplug operations.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001013 *
1014 * Note that the grace period cannot complete until we finish
1015 * the initialization process, as there will be at least one
1016 * qsmask bit set in the root node until that time, namely the
Paul E. McKenneyb835db12009-09-08 15:54:37 -07001017 * one corresponding to this CPU, due to the fact that we have
1018 * irqs disabled.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001019 */
Paul E. McKenneya0b6c9a2009-09-28 07:46:33 -07001020 rcu_for_each_node_breadth_first(rsp, rnp) {
Paul E. McKenney1304afb2010-02-22 17:05:02 -08001021 raw_spin_lock(&rnp->lock); /* irqs already disabled. */
Paul E. McKenneyb0e165c2009-09-13 09:15:09 -07001022 rcu_preempt_check_blocked_tasks(rnp);
Paul E. McKenney49e29122009-09-18 09:50:19 -07001023 rnp->qsmask = rnp->qsmaskinit;
Paul E. McKenneyde078d82009-09-08 15:54:36 -07001024 rnp->gpnum = rsp->gpnum;
Paul E. McKenneyd09b62d2009-11-02 13:52:28 -08001025 rnp->completed = rsp->completed;
1026 if (rnp == rdp->mynode)
1027 rcu_start_gp_per_cpu(rsp, rnp, rdp);
Paul E. McKenney27f4d282011-02-07 12:47:15 -08001028 rcu_preempt_boost_start_gp(rnp);
Paul E. McKenneyd4c08f22011-06-25 06:36:56 -07001029 trace_rcu_grace_period_init(rsp->name, rnp->gpnum,
1030 rnp->level, rnp->grplo,
1031 rnp->grphi, rnp->qsmask);
Paul E. McKenney1304afb2010-02-22 17:05:02 -08001032 raw_spin_unlock(&rnp->lock); /* irqs remain disabled. */
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001033 }
1034
Paul E. McKenney83f5b012009-10-28 08:14:49 -07001035 rnp = rcu_get_root(rsp);
Paul E. McKenney1304afb2010-02-22 17:05:02 -08001036 raw_spin_lock(&rnp->lock); /* irqs already disabled. */
Paul E. McKenneyaf446b72011-09-10 21:54:08 -07001037 rsp->fqs_state = RCU_SIGNAL_INIT; /* force_quiescent_state now OK. */
Paul E. McKenney1304afb2010-02-22 17:05:02 -08001038 raw_spin_unlock(&rnp->lock); /* irqs remain disabled. */
1039 raw_spin_unlock_irqrestore(&rsp->onofflock, flags);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001040}
1041
1042/*
Paul E. McKenneyd3f6bad2009-12-02 12:10:13 -08001043 * Report a full set of quiescent states to the specified rcu_state
1044 * data structure. This involves cleaning up after the prior grace
1045 * period and letting rcu_start_gp() start up the next grace period
1046 * if one is needed. Note that the caller must hold rnp->lock, as
1047 * required by rcu_start_gp(), which will release it.
Paul E. McKenneyf41d9112009-08-22 13:56:52 -07001048 */
Paul E. McKenneyd3f6bad2009-12-02 12:10:13 -08001049static void rcu_report_qs_rsp(struct rcu_state *rsp, unsigned long flags)
Paul E. McKenneyfc2219d2009-09-23 09:50:41 -07001050 __releases(rcu_get_root(rsp)->lock)
Paul E. McKenneyf41d9112009-08-22 13:56:52 -07001051{
Paul E. McKenney15ba0ba2011-04-06 16:01:16 -07001052 unsigned long gp_duration;
Paul E. McKenneyafe24b12011-08-24 16:52:09 -07001053 struct rcu_node *rnp = rcu_get_root(rsp);
1054 struct rcu_data *rdp = this_cpu_ptr(rsp->rda);
Paul E. McKenney15ba0ba2011-04-06 16:01:16 -07001055
Paul E. McKenneyfc2219d2009-09-23 09:50:41 -07001056 WARN_ON_ONCE(!rcu_gp_in_progress(rsp));
Paul E. McKenney0bbcc522011-05-16 02:24:04 -07001057
1058 /*
1059 * Ensure that all grace-period and pre-grace-period activity
1060 * is seen before the assignment to rsp->completed.
1061 */
1062 smp_mb(); /* See above block comment. */
Paul E. McKenney15ba0ba2011-04-06 16:01:16 -07001063 gp_duration = jiffies - rsp->gp_start;
1064 if (gp_duration > rsp->gp_max)
1065 rsp->gp_max = gp_duration;
Paul E. McKenneyafe24b12011-08-24 16:52:09 -07001066
1067 /*
1068 * We know the grace period is complete, but to everyone else
1069 * it appears to still be ongoing. But it is also the case
1070 * that to everyone else it looks like there is nothing that
1071 * they can do to advance the grace period. It is therefore
1072 * safe for us to drop the lock in order to mark the grace
1073 * period as completed in all of the rcu_node structures.
1074 *
1075 * But if this CPU needs another grace period, it will take
1076 * care of this while initializing the next grace period.
1077 * We use RCU_WAIT_TAIL instead of the usual RCU_DONE_TAIL
1078 * because the callbacks have not yet been advanced: Those
1079 * callbacks are waiting on the grace period that just now
1080 * completed.
1081 */
1082 if (*rdp->nxttail[RCU_WAIT_TAIL] == NULL) {
1083 raw_spin_unlock(&rnp->lock); /* irqs remain disabled. */
1084
1085 /*
1086 * Propagate new ->completed value to rcu_node structures
1087 * so that other CPUs don't have to wait until the start
1088 * of the next grace period to process their callbacks.
1089 */
1090 rcu_for_each_node_breadth_first(rsp, rnp) {
1091 raw_spin_lock(&rnp->lock); /* irqs already disabled. */
1092 rnp->completed = rsp->gpnum;
1093 raw_spin_unlock(&rnp->lock); /* irqs remain disabled. */
1094 }
1095 rnp = rcu_get_root(rsp);
1096 raw_spin_lock(&rnp->lock); /* irqs already disabled. */
1097 }
1098
1099 rsp->completed = rsp->gpnum; /* Declare the grace period complete. */
Paul E. McKenneyd4c08f22011-06-25 06:36:56 -07001100 trace_rcu_grace_period(rsp->name, rsp->completed, "end");
Paul E. McKenneyaf446b72011-09-10 21:54:08 -07001101 rsp->fqs_state = RCU_GP_IDLE;
Paul E. McKenneyf41d9112009-08-22 13:56:52 -07001102 rcu_start_gp(rsp, flags); /* releases root node's rnp->lock. */
1103}
1104
1105/*
Paul E. McKenneyd3f6bad2009-12-02 12:10:13 -08001106 * Similar to rcu_report_qs_rdp(), for which it is a helper function.
1107 * Allows quiescent states for a group of CPUs to be reported at one go
1108 * to the specified rcu_node structure, though all the CPUs in the group
1109 * must be represented by the same rcu_node structure (which need not be
1110 * a leaf rcu_node structure, though it often will be). That structure's
1111 * lock must be held upon entry, and it is released before return.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001112 */
1113static void
Paul E. McKenneyd3f6bad2009-12-02 12:10:13 -08001114rcu_report_qs_rnp(unsigned long mask, struct rcu_state *rsp,
1115 struct rcu_node *rnp, unsigned long flags)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001116 __releases(rnp->lock)
1117{
Paul E. McKenney28ecd582009-09-18 09:50:17 -07001118 struct rcu_node *rnp_c;
1119
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001120 /* Walk up the rcu_node hierarchy. */
1121 for (;;) {
1122 if (!(rnp->qsmask & mask)) {
1123
1124 /* Our bit has already been cleared, so done. */
Paul E. McKenney1304afb2010-02-22 17:05:02 -08001125 raw_spin_unlock_irqrestore(&rnp->lock, flags);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001126 return;
1127 }
1128 rnp->qsmask &= ~mask;
Paul E. McKenneyd4c08f22011-06-25 06:36:56 -07001129 trace_rcu_quiescent_state_report(rsp->name, rnp->gpnum,
1130 mask, rnp->qsmask, rnp->level,
1131 rnp->grplo, rnp->grphi,
1132 !!rnp->gp_tasks);
Paul E. McKenney27f4d282011-02-07 12:47:15 -08001133 if (rnp->qsmask != 0 || rcu_preempt_blocked_readers_cgp(rnp)) {
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001134
1135 /* Other bits still set at this level, so done. */
Paul E. McKenney1304afb2010-02-22 17:05:02 -08001136 raw_spin_unlock_irqrestore(&rnp->lock, flags);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001137 return;
1138 }
1139 mask = rnp->grpmask;
1140 if (rnp->parent == NULL) {
1141
1142 /* No more levels. Exit loop holding root lock. */
1143
1144 break;
1145 }
Paul E. McKenney1304afb2010-02-22 17:05:02 -08001146 raw_spin_unlock_irqrestore(&rnp->lock, flags);
Paul E. McKenney28ecd582009-09-18 09:50:17 -07001147 rnp_c = rnp;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001148 rnp = rnp->parent;
Paul E. McKenney1304afb2010-02-22 17:05:02 -08001149 raw_spin_lock_irqsave(&rnp->lock, flags);
Paul E. McKenney28ecd582009-09-18 09:50:17 -07001150 WARN_ON_ONCE(rnp_c->qsmask);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001151 }
1152
1153 /*
1154 * Get here if we are the last CPU to pass through a quiescent
Paul E. McKenneyd3f6bad2009-12-02 12:10:13 -08001155 * state for this grace period. Invoke rcu_report_qs_rsp()
Paul E. McKenneyf41d9112009-08-22 13:56:52 -07001156 * to clean up and start the next grace period if one is needed.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001157 */
Paul E. McKenneyd3f6bad2009-12-02 12:10:13 -08001158 rcu_report_qs_rsp(rsp, flags); /* releases rnp->lock. */
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001159}
1160
1161/*
Paul E. McKenneyd3f6bad2009-12-02 12:10:13 -08001162 * Record a quiescent state for the specified CPU to that CPU's rcu_data
1163 * structure. This must be either called from the specified CPU, or
1164 * called when the specified CPU is known to be offline (and when it is
1165 * also known that no other CPU is concurrently trying to help the offline
1166 * CPU). The lastcomp argument is used to make sure we are still in the
1167 * grace period of interest. We don't want to end the current grace period
1168 * based on quiescent states detected in an earlier grace period!
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001169 */
1170static void
Paul E. McKenneye4cc1f22011-06-27 00:17:43 -07001171rcu_report_qs_rdp(int cpu, struct rcu_state *rsp, struct rcu_data *rdp, long lastgp)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001172{
1173 unsigned long flags;
1174 unsigned long mask;
1175 struct rcu_node *rnp;
1176
1177 rnp = rdp->mynode;
Paul E. McKenney1304afb2010-02-22 17:05:02 -08001178 raw_spin_lock_irqsave(&rnp->lock, flags);
Paul E. McKenneye4cc1f22011-06-27 00:17:43 -07001179 if (lastgp != rnp->gpnum || rnp->completed == rnp->gpnum) {
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001180
1181 /*
Paul E. McKenneye4cc1f22011-06-27 00:17:43 -07001182 * The grace period in which this quiescent state was
1183 * recorded has ended, so don't report it upwards.
1184 * We will instead need a new quiescent state that lies
1185 * within the current grace period.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001186 */
Paul E. McKenneye4cc1f22011-06-27 00:17:43 -07001187 rdp->passed_quiesce = 0; /* need qs for new gp. */
Paul E. McKenney1304afb2010-02-22 17:05:02 -08001188 raw_spin_unlock_irqrestore(&rnp->lock, flags);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001189 return;
1190 }
1191 mask = rdp->grpmask;
1192 if ((rnp->qsmask & mask) == 0) {
Paul E. McKenney1304afb2010-02-22 17:05:02 -08001193 raw_spin_unlock_irqrestore(&rnp->lock, flags);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001194 } else {
1195 rdp->qs_pending = 0;
1196
1197 /*
1198 * This GP can't end until cpu checks in, so all of our
1199 * callbacks can be processed during the next GP.
1200 */
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001201 rdp->nxttail[RCU_NEXT_READY_TAIL] = rdp->nxttail[RCU_NEXT_TAIL];
1202
Paul E. McKenneyd3f6bad2009-12-02 12:10:13 -08001203 rcu_report_qs_rnp(mask, rsp, rnp, flags); /* rlses rnp->lock */
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001204 }
1205}
1206
1207/*
1208 * Check to see if there is a new grace period of which this CPU
1209 * is not yet aware, and if so, set up local rcu_data state for it.
1210 * Otherwise, see if this CPU has just passed through its first
1211 * quiescent state for this grace period, and record that fact if so.
1212 */
1213static void
1214rcu_check_quiescent_state(struct rcu_state *rsp, struct rcu_data *rdp)
1215{
1216 /* If there is now a new grace period, record and return. */
1217 if (check_for_new_grace_period(rsp, rdp))
1218 return;
1219
1220 /*
1221 * Does this CPU still need to do its part for current grace period?
1222 * If no, return and let the other CPUs do their part as well.
1223 */
1224 if (!rdp->qs_pending)
1225 return;
1226
1227 /*
1228 * Was there a quiescent state since the beginning of the grace
1229 * period? If no, then exit and wait for the next call.
1230 */
Paul E. McKenneye4cc1f22011-06-27 00:17:43 -07001231 if (!rdp->passed_quiesce)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001232 return;
1233
Paul E. McKenneyd3f6bad2009-12-02 12:10:13 -08001234 /*
1235 * Tell RCU we are done (but rcu_report_qs_rdp() will be the
1236 * judge of that).
1237 */
Paul E. McKenneye4cc1f22011-06-27 00:17:43 -07001238 rcu_report_qs_rdp(rdp->cpu, rsp, rdp, rdp->passed_quiesce_gpnum);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001239}
1240
1241#ifdef CONFIG_HOTPLUG_CPU
1242
1243/*
Lai Jiangshan29494be2010-10-20 14:13:06 +08001244 * Move a dying CPU's RCU callbacks to online CPU's callback list.
Paul E. McKenneye5601402012-01-07 11:03:57 -08001245 * Also record a quiescent state for this CPU for the current grace period.
1246 * Synchronization and interrupt disabling are not required because
1247 * this function executes in stop_machine() context. Therefore, cleanup
1248 * operations that might block must be done later from the CPU_DEAD
1249 * notifier.
1250 *
1251 * Note that the outgoing CPU's bit has already been cleared in the
1252 * cpu_online_mask. This allows us to randomly pick a callback
1253 * destination from the bits set in that mask.
Paul E. McKenneye74f4c42009-10-06 21:48:17 -07001254 */
Paul E. McKenneye5601402012-01-07 11:03:57 -08001255static void rcu_cleanup_dying_cpu(struct rcu_state *rsp)
Paul E. McKenneye74f4c42009-10-06 21:48:17 -07001256{
Paul E. McKenneye5601402012-01-07 11:03:57 -08001257 unsigned long flags;
Paul E. McKenneye74f4c42009-10-06 21:48:17 -07001258 int i;
Paul E. McKenneye5601402012-01-07 11:03:57 -08001259 unsigned long mask;
1260 int need_report;
Lai Jiangshan29494be2010-10-20 14:13:06 +08001261 int receive_cpu = cpumask_any(cpu_online_mask);
Lai Jiangshan394f99a2010-06-28 16:25:04 +08001262 struct rcu_data *rdp = this_cpu_ptr(rsp->rda);
Lai Jiangshan29494be2010-10-20 14:13:06 +08001263 struct rcu_data *receive_rdp = per_cpu_ptr(rsp->rda, receive_cpu);
Paul E. McKenneye5601402012-01-07 11:03:57 -08001264 struct rcu_node *rnp = rdp->mynode; /* For dying CPU. */
Paul E. McKenneye74f4c42009-10-06 21:48:17 -07001265
Paul E. McKenneya50c3af2012-01-10 17:52:31 -08001266 /* First, adjust the counts. */
1267 if (rdp->nxtlist != NULL) {
1268 receive_rdp->qlen_lazy += rdp->qlen_lazy;
1269 receive_rdp->qlen += rdp->qlen;
1270 rdp->qlen_lazy = 0;
1271 rdp->qlen = 0;
1272 }
1273
1274 /*
1275 * Next, move ready-to-invoke callbacks to be invoked on some
1276 * other CPU. These will not be required to pass through another
1277 * grace period: They are done, regardless of CPU.
1278 */
1279 if (rdp->nxtlist != NULL &&
1280 rdp->nxttail[RCU_DONE_TAIL] != &rdp->nxtlist) {
1281 struct rcu_head *oldhead;
1282 struct rcu_head **oldtail;
1283 struct rcu_head **newtail;
1284
1285 oldhead = rdp->nxtlist;
1286 oldtail = receive_rdp->nxttail[RCU_DONE_TAIL];
1287 rdp->nxtlist = *rdp->nxttail[RCU_DONE_TAIL];
1288 *rdp->nxttail[RCU_DONE_TAIL] = *oldtail;
1289 *receive_rdp->nxttail[RCU_DONE_TAIL] = oldhead;
1290 newtail = rdp->nxttail[RCU_DONE_TAIL];
1291 for (i = RCU_DONE_TAIL; i < RCU_NEXT_SIZE; i++) {
1292 if (receive_rdp->nxttail[i] == oldtail)
1293 receive_rdp->nxttail[i] = newtail;
1294 if (rdp->nxttail[i] == newtail)
1295 rdp->nxttail[i] = &rdp->nxtlist;
1296 }
1297 }
1298
1299 /*
1300 * Finally, put the rest of the callbacks at the end of the list.
1301 * The ones that made it partway through get to start over: We
1302 * cannot assume that grace periods are synchronized across CPUs.
1303 * (We could splice RCU_WAIT_TAIL into RCU_NEXT_READY_TAIL, but
1304 * this does not seem compelling. Not yet, anyway.)
1305 */
Paul E. McKenneye5601402012-01-07 11:03:57 -08001306 if (rdp->nxtlist != NULL) {
1307 *receive_rdp->nxttail[RCU_NEXT_TAIL] = rdp->nxtlist;
1308 receive_rdp->nxttail[RCU_NEXT_TAIL] =
1309 rdp->nxttail[RCU_NEXT_TAIL];
Paul E. McKenneye5601402012-01-07 11:03:57 -08001310 receive_rdp->n_cbs_adopted += rdp->qlen;
1311 rdp->n_cbs_orphaned += rdp->qlen;
Lai Jiangshan29494be2010-10-20 14:13:06 +08001312
Paul E. McKenneye5601402012-01-07 11:03:57 -08001313 rdp->nxtlist = NULL;
1314 for (i = 0; i < RCU_NEXT_SIZE; i++)
1315 rdp->nxttail[i] = &rdp->nxtlist;
Paul E. McKenneye5601402012-01-07 11:03:57 -08001316 }
Lai Jiangshan29494be2010-10-20 14:13:06 +08001317
Paul E. McKenneya50c3af2012-01-10 17:52:31 -08001318 /*
1319 * Record a quiescent state for the dying CPU. This is safe
1320 * only because we have already cleared out the callbacks.
1321 * (Otherwise, the RCU core might try to schedule the invocation
1322 * of callbacks on this now-offline CPU, which would be bad.)
1323 */
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001324 mask = rdp->grpmask; /* rnp->grplo is constant. */
Paul E. McKenneye5601402012-01-07 11:03:57 -08001325 trace_rcu_grace_period(rsp->name,
1326 rnp->gpnum + 1 - !!(rnp->qsmask & mask),
1327 "cpuofl");
1328 rcu_report_qs_rdp(smp_processor_id(), rsp, rdp, rsp->gpnum);
1329 /* Note that rcu_report_qs_rdp() might call trace_rcu_grace_period(). */
1330
1331 /*
1332 * Remove the dying CPU from the bitmasks in the rcu_node
1333 * hierarchy. Because we are in stop_machine() context, we
1334 * automatically exclude ->onofflock critical sections.
1335 */
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001336 do {
Paul E. McKenneye5601402012-01-07 11:03:57 -08001337 raw_spin_lock_irqsave(&rnp->lock, flags);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001338 rnp->qsmaskinit &= ~mask;
1339 if (rnp->qsmaskinit != 0) {
Paul E. McKenneye5601402012-01-07 11:03:57 -08001340 raw_spin_unlock_irqrestore(&rnp->lock, flags);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001341 break;
1342 }
Paul E. McKenneyd4c08f22011-06-25 06:36:56 -07001343 if (rnp == rdp->mynode) {
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -08001344 need_report = rcu_preempt_offline_tasks(rsp, rnp, rdp);
Paul E. McKenneye5601402012-01-07 11:03:57 -08001345 if (need_report & RCU_OFL_TASKS_NORM_GP)
1346 rcu_report_unblock_qs_rnp(rnp, flags);
1347 else
1348 raw_spin_unlock_irqrestore(&rnp->lock, flags);
1349 if (need_report & RCU_OFL_TASKS_EXP_GP)
1350 rcu_report_exp_rnp(rsp, rnp, true);
Paul E. McKenneyd4c08f22011-06-25 06:36:56 -07001351 } else
Paul E. McKenneye5601402012-01-07 11:03:57 -08001352 raw_spin_unlock_irqrestore(&rnp->lock, flags);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001353 mask = rnp->grpmask;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001354 rnp = rnp->parent;
1355 } while (rnp != NULL);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001356}
1357
1358/*
Paul E. McKenneye5601402012-01-07 11:03:57 -08001359 * The CPU has been completely removed, and some other CPU is reporting
1360 * this fact from process context. Do the remainder of the cleanup.
1361 * There can only be one CPU hotplug operation at a time, so no other
1362 * CPU can be attempting to update rcu_cpu_kthread_task.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001363 */
Paul E. McKenneye5601402012-01-07 11:03:57 -08001364static void rcu_cleanup_dead_cpu(int cpu, struct rcu_state *rsp)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001365{
Paul E. McKenneye5601402012-01-07 11:03:57 -08001366 struct rcu_data *rdp = per_cpu_ptr(rsp->rda, cpu);
1367 struct rcu_node *rnp = rdp->mynode;
1368
1369 rcu_stop_cpu_kthread(cpu);
1370 rcu_node_kthread_setaffinity(rnp, -1);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001371}
1372
1373#else /* #ifdef CONFIG_HOTPLUG_CPU */
1374
Paul E. McKenneye5601402012-01-07 11:03:57 -08001375static void rcu_cleanup_dying_cpu(struct rcu_state *rsp)
Paul E. McKenneye74f4c42009-10-06 21:48:17 -07001376{
1377}
1378
Paul E. McKenneye5601402012-01-07 11:03:57 -08001379static void rcu_cleanup_dead_cpu(int cpu, struct rcu_state *rsp)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001380{
1381}
1382
1383#endif /* #else #ifdef CONFIG_HOTPLUG_CPU */
1384
1385/*
1386 * Invoke any RCU callbacks that have made it to the end of their grace
1387 * period. Thottle as specified by rdp->blimit.
1388 */
Paul E. McKenney37c72e52009-10-14 10:15:55 -07001389static void rcu_do_batch(struct rcu_state *rsp, struct rcu_data *rdp)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001390{
1391 unsigned long flags;
1392 struct rcu_head *next, *list, **tail;
Paul E. McKenney486e2592012-01-06 14:11:30 -08001393 int bl, count, count_lazy;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001394
1395 /* If no callbacks are ready, just return.*/
Paul E. McKenney29c00b42011-06-17 15:53:19 -07001396 if (!cpu_has_callbacks_ready_to_invoke(rdp)) {
Paul E. McKenney486e2592012-01-06 14:11:30 -08001397 trace_rcu_batch_start(rsp->name, rdp->qlen_lazy, rdp->qlen, 0);
Paul E. McKenney4968c302011-12-07 16:32:40 -08001398 trace_rcu_batch_end(rsp->name, 0, !!ACCESS_ONCE(rdp->nxtlist),
1399 need_resched(), is_idle_task(current),
1400 rcu_is_callbacks_kthread());
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001401 return;
Paul E. McKenney29c00b42011-06-17 15:53:19 -07001402 }
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001403
1404 /*
1405 * Extract the list of ready callbacks, disabling to prevent
1406 * races with call_rcu() from interrupt handlers.
1407 */
1408 local_irq_save(flags);
Paul E. McKenney8146c4e22012-01-10 14:23:29 -08001409 WARN_ON_ONCE(cpu_is_offline(smp_processor_id()));
Paul E. McKenney29c00b42011-06-17 15:53:19 -07001410 bl = rdp->blimit;
Paul E. McKenney486e2592012-01-06 14:11:30 -08001411 trace_rcu_batch_start(rsp->name, rdp->qlen_lazy, rdp->qlen, bl);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001412 list = rdp->nxtlist;
1413 rdp->nxtlist = *rdp->nxttail[RCU_DONE_TAIL];
1414 *rdp->nxttail[RCU_DONE_TAIL] = NULL;
1415 tail = rdp->nxttail[RCU_DONE_TAIL];
1416 for (count = RCU_NEXT_SIZE - 1; count >= 0; count--)
1417 if (rdp->nxttail[count] == rdp->nxttail[RCU_DONE_TAIL])
1418 rdp->nxttail[count] = &rdp->nxtlist;
1419 local_irq_restore(flags);
1420
1421 /* Invoke callbacks. */
Paul E. McKenney486e2592012-01-06 14:11:30 -08001422 count = count_lazy = 0;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001423 while (list) {
1424 next = list->next;
1425 prefetch(next);
Mathieu Desnoyers551d55a2010-04-17 08:48:42 -04001426 debug_rcu_head_unqueue(list);
Paul E. McKenney486e2592012-01-06 14:11:30 -08001427 if (__rcu_reclaim(rsp->name, list))
1428 count_lazy++;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001429 list = next;
Paul E. McKenneydff16722011-11-29 15:57:13 -08001430 /* Stop only if limit reached and CPU has something to do. */
1431 if (++count >= bl &&
1432 (need_resched() ||
1433 (!is_idle_task(current) && !rcu_is_callbacks_kthread())))
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001434 break;
1435 }
1436
1437 local_irq_save(flags);
Paul E. McKenney4968c302011-12-07 16:32:40 -08001438 trace_rcu_batch_end(rsp->name, count, !!list, need_resched(),
1439 is_idle_task(current),
1440 rcu_is_callbacks_kthread());
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001441
1442 /* Update count, and requeue any remaining callbacks. */
Paul E. McKenney486e2592012-01-06 14:11:30 -08001443 rdp->qlen_lazy -= count_lazy;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001444 rdp->qlen -= count;
Paul E. McKenney269dcc12010-09-07 14:23:09 -07001445 rdp->n_cbs_invoked += count;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001446 if (list != NULL) {
1447 *tail = rdp->nxtlist;
1448 rdp->nxtlist = list;
1449 for (count = 0; count < RCU_NEXT_SIZE; count++)
1450 if (&rdp->nxtlist == rdp->nxttail[count])
1451 rdp->nxttail[count] = tail;
1452 else
1453 break;
1454 }
1455
1456 /* Reinstate batch limit if we have worked down the excess. */
1457 if (rdp->blimit == LONG_MAX && rdp->qlen <= qlowmark)
1458 rdp->blimit = blimit;
1459
Paul E. McKenney37c72e52009-10-14 10:15:55 -07001460 /* Reset ->qlen_last_fqs_check trigger if enough CBs have drained. */
1461 if (rdp->qlen == 0 && rdp->qlen_last_fqs_check != 0) {
1462 rdp->qlen_last_fqs_check = 0;
1463 rdp->n_force_qs_snap = rsp->n_force_qs;
1464 } else if (rdp->qlen < rdp->qlen_last_fqs_check - qhimark)
1465 rdp->qlen_last_fqs_check = rdp->qlen;
1466
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001467 local_irq_restore(flags);
1468
Paul E. McKenneye0f23062011-06-21 01:29:39 -07001469 /* Re-invoke RCU core processing if there are callbacks remaining. */
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001470 if (cpu_has_callbacks_ready_to_invoke(rdp))
Paul E. McKenneya46e0892011-06-15 15:47:09 -07001471 invoke_rcu_core();
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001472}
1473
1474/*
1475 * Check to see if this CPU is in a non-context-switch quiescent state
1476 * (user mode or idle loop for rcu, non-softirq execution for rcu_bh).
Paul E. McKenneye0f23062011-06-21 01:29:39 -07001477 * Also schedule RCU core processing.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001478 *
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -07001479 * This function must be called from hardirq context. It is normally
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001480 * invoked from the scheduling-clock interrupt. If rcu_pending returns
1481 * false, there is no point in invoking rcu_check_callbacks().
1482 */
1483void rcu_check_callbacks(int cpu, int user)
1484{
Paul E. McKenney300df912011-06-18 22:26:31 -07001485 trace_rcu_utilization("Start scheduler-tick");
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -07001486 if (user || rcu_is_cpu_rrupt_from_idle()) {
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001487
1488 /*
1489 * Get here if this CPU took its interrupt from user
1490 * mode or from the idle loop, and if this is not a
1491 * nested interrupt. In this case, the CPU is in
Paul E. McKenneyd6714c22009-08-22 13:56:46 -07001492 * a quiescent state, so note it.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001493 *
1494 * No memory barrier is required here because both
Paul E. McKenneyd6714c22009-08-22 13:56:46 -07001495 * rcu_sched_qs() and rcu_bh_qs() reference only CPU-local
1496 * variables that other CPUs neither access nor modify,
1497 * at least not while the corresponding CPU is online.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001498 */
1499
Paul E. McKenneyd6714c22009-08-22 13:56:46 -07001500 rcu_sched_qs(cpu);
1501 rcu_bh_qs(cpu);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001502
1503 } else if (!in_softirq()) {
1504
1505 /*
1506 * Get here if this CPU did not take its interrupt from
1507 * softirq, in other words, if it is not interrupting
1508 * a rcu_bh read-side critical section. This is an _bh
Paul E. McKenneyd6714c22009-08-22 13:56:46 -07001509 * critical section, so note it.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001510 */
1511
Paul E. McKenneyd6714c22009-08-22 13:56:46 -07001512 rcu_bh_qs(cpu);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001513 }
Paul E. McKenneyf41d9112009-08-22 13:56:52 -07001514 rcu_preempt_check_callbacks(cpu);
Paul E. McKenneyd21670a2010-04-14 17:39:26 -07001515 if (rcu_pending(cpu))
Paul E. McKenneya46e0892011-06-15 15:47:09 -07001516 invoke_rcu_core();
Paul E. McKenney300df912011-06-18 22:26:31 -07001517 trace_rcu_utilization("End scheduler-tick");
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001518}
1519
1520#ifdef CONFIG_SMP
1521
1522/*
1523 * Scan the leaf rcu_node structures, processing dyntick state for any that
1524 * have not yet encountered a quiescent state, using the function specified.
Paul E. McKenney27f4d282011-02-07 12:47:15 -08001525 * Also initiate boosting for any threads blocked on the root rcu_node.
1526 *
Paul E. McKenneyee47eb92010-01-04 15:09:07 -08001527 * The caller must have suppressed start of new grace periods.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001528 */
Paul E. McKenney45f014c2010-01-04 15:09:08 -08001529static void force_qs_rnp(struct rcu_state *rsp, int (*f)(struct rcu_data *))
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001530{
1531 unsigned long bit;
1532 int cpu;
1533 unsigned long flags;
1534 unsigned long mask;
Paul E. McKenneya0b6c9a2009-09-28 07:46:33 -07001535 struct rcu_node *rnp;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001536
Paul E. McKenneya0b6c9a2009-09-28 07:46:33 -07001537 rcu_for_each_leaf_node(rsp, rnp) {
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001538 mask = 0;
Paul E. McKenney1304afb2010-02-22 17:05:02 -08001539 raw_spin_lock_irqsave(&rnp->lock, flags);
Paul E. McKenneyee47eb92010-01-04 15:09:07 -08001540 if (!rcu_gp_in_progress(rsp)) {
Paul E. McKenney1304afb2010-02-22 17:05:02 -08001541 raw_spin_unlock_irqrestore(&rnp->lock, flags);
Paul E. McKenney0f10dc822010-01-04 15:09:06 -08001542 return;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001543 }
Paul E. McKenneya0b6c9a2009-09-28 07:46:33 -07001544 if (rnp->qsmask == 0) {
Paul E. McKenney1217ed12011-05-04 21:43:49 -07001545 rcu_initiate_boost(rnp, flags); /* releases rnp->lock */
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001546 continue;
1547 }
Paul E. McKenneya0b6c9a2009-09-28 07:46:33 -07001548 cpu = rnp->grplo;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001549 bit = 1;
Paul E. McKenneya0b6c9a2009-09-28 07:46:33 -07001550 for (; cpu <= rnp->grphi; cpu++, bit <<= 1) {
Lai Jiangshan394f99a2010-06-28 16:25:04 +08001551 if ((rnp->qsmask & bit) != 0 &&
1552 f(per_cpu_ptr(rsp->rda, cpu)))
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001553 mask |= bit;
1554 }
Paul E. McKenney45f014c2010-01-04 15:09:08 -08001555 if (mask != 0) {
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001556
Paul E. McKenneyd3f6bad2009-12-02 12:10:13 -08001557 /* rcu_report_qs_rnp() releases rnp->lock. */
1558 rcu_report_qs_rnp(mask, rsp, rnp, flags);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001559 continue;
1560 }
Paul E. McKenney1304afb2010-02-22 17:05:02 -08001561 raw_spin_unlock_irqrestore(&rnp->lock, flags);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001562 }
Paul E. McKenney27f4d282011-02-07 12:47:15 -08001563 rnp = rcu_get_root(rsp);
Paul E. McKenney1217ed12011-05-04 21:43:49 -07001564 if (rnp->qsmask == 0) {
1565 raw_spin_lock_irqsave(&rnp->lock, flags);
1566 rcu_initiate_boost(rnp, flags); /* releases rnp->lock. */
1567 }
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001568}
1569
1570/*
1571 * Force quiescent states on reluctant CPUs, and also detect which
1572 * CPUs are in dyntick-idle mode.
1573 */
1574static void force_quiescent_state(struct rcu_state *rsp, int relaxed)
1575{
1576 unsigned long flags;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001577 struct rcu_node *rnp = rcu_get_root(rsp);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001578
Paul E. McKenney300df912011-06-18 22:26:31 -07001579 trace_rcu_utilization("Start fqs");
1580 if (!rcu_gp_in_progress(rsp)) {
1581 trace_rcu_utilization("End fqs");
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001582 return; /* No grace period in progress, nothing to force. */
Paul E. McKenney300df912011-06-18 22:26:31 -07001583 }
Paul E. McKenney1304afb2010-02-22 17:05:02 -08001584 if (!raw_spin_trylock_irqsave(&rsp->fqslock, flags)) {
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001585 rsp->n_force_qs_lh++; /* Inexact, can lose counts. Tough! */
Paul E. McKenney300df912011-06-18 22:26:31 -07001586 trace_rcu_utilization("End fqs");
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001587 return; /* Someone else is already on the job. */
1588 }
Paul E. McKenney20133cf2010-02-22 17:05:01 -08001589 if (relaxed && ULONG_CMP_GE(rsp->jiffies_force_qs, jiffies))
Paul E. McKenneyf96e9232010-01-04 15:09:00 -08001590 goto unlock_fqs_ret; /* no emergency and done recently. */
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001591 rsp->n_force_qs++;
Paul E. McKenney1304afb2010-02-22 17:05:02 -08001592 raw_spin_lock(&rnp->lock); /* irqs already disabled */
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001593 rsp->jiffies_force_qs = jiffies + RCU_JIFFIES_TILL_FORCE_QS;
Paul E. McKenney560d4bc2009-11-13 19:51:38 -08001594 if(!rcu_gp_in_progress(rsp)) {
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001595 rsp->n_force_qs_ngp++;
Paul E. McKenney1304afb2010-02-22 17:05:02 -08001596 raw_spin_unlock(&rnp->lock); /* irqs remain disabled */
Paul E. McKenneyf96e9232010-01-04 15:09:00 -08001597 goto unlock_fqs_ret; /* no GP in progress, time updated. */
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001598 }
Paul E. McKenney07079d52010-01-04 15:09:02 -08001599 rsp->fqs_active = 1;
Paul E. McKenneyaf446b72011-09-10 21:54:08 -07001600 switch (rsp->fqs_state) {
Paul E. McKenney83f5b012009-10-28 08:14:49 -07001601 case RCU_GP_IDLE:
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001602 case RCU_GP_INIT:
1603
Paul E. McKenney83f5b012009-10-28 08:14:49 -07001604 break; /* grace period idle or initializing, ignore. */
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001605
1606 case RCU_SAVE_DYNTICK:
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001607 if (RCU_SIGNAL_INIT != RCU_SAVE_DYNTICK)
1608 break; /* So gcc recognizes the dead code. */
1609
Lai Jiangshanf2614142010-03-28 11:15:20 +08001610 raw_spin_unlock(&rnp->lock); /* irqs remain disabled */
1611
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001612 /* Record dyntick-idle state. */
Paul E. McKenney45f014c2010-01-04 15:09:08 -08001613 force_qs_rnp(rsp, dyntick_save_progress_counter);
Paul E. McKenney1304afb2010-02-22 17:05:02 -08001614 raw_spin_lock(&rnp->lock); /* irqs already disabled */
Paul E. McKenneyee47eb92010-01-04 15:09:07 -08001615 if (rcu_gp_in_progress(rsp))
Paul E. McKenneyaf446b72011-09-10 21:54:08 -07001616 rsp->fqs_state = RCU_FORCE_QS;
Paul E. McKenneyee47eb92010-01-04 15:09:07 -08001617 break;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001618
1619 case RCU_FORCE_QS:
1620
1621 /* Check dyntick-idle state, send IPI to laggarts. */
Paul E. McKenney1304afb2010-02-22 17:05:02 -08001622 raw_spin_unlock(&rnp->lock); /* irqs remain disabled */
Paul E. McKenney45f014c2010-01-04 15:09:08 -08001623 force_qs_rnp(rsp, rcu_implicit_dynticks_qs);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001624
1625 /* Leave state in case more forcing is required. */
1626
Paul E. McKenney1304afb2010-02-22 17:05:02 -08001627 raw_spin_lock(&rnp->lock); /* irqs already disabled */
Paul E. McKenneyf96e9232010-01-04 15:09:00 -08001628 break;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001629 }
Paul E. McKenney07079d52010-01-04 15:09:02 -08001630 rsp->fqs_active = 0;
Paul E. McKenney46a1e342010-01-04 15:09:09 -08001631 if (rsp->fqs_need_gp) {
Paul E. McKenney1304afb2010-02-22 17:05:02 -08001632 raw_spin_unlock(&rsp->fqslock); /* irqs remain disabled */
Paul E. McKenney46a1e342010-01-04 15:09:09 -08001633 rsp->fqs_need_gp = 0;
1634 rcu_start_gp(rsp, flags); /* releases rnp->lock */
Paul E. McKenney300df912011-06-18 22:26:31 -07001635 trace_rcu_utilization("End fqs");
Paul E. McKenney46a1e342010-01-04 15:09:09 -08001636 return;
1637 }
Paul E. McKenney1304afb2010-02-22 17:05:02 -08001638 raw_spin_unlock(&rnp->lock); /* irqs remain disabled */
Paul E. McKenneyf96e9232010-01-04 15:09:00 -08001639unlock_fqs_ret:
Paul E. McKenney1304afb2010-02-22 17:05:02 -08001640 raw_spin_unlock_irqrestore(&rsp->fqslock, flags);
Paul E. McKenney300df912011-06-18 22:26:31 -07001641 trace_rcu_utilization("End fqs");
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001642}
1643
1644#else /* #ifdef CONFIG_SMP */
1645
1646static void force_quiescent_state(struct rcu_state *rsp, int relaxed)
1647{
1648 set_need_resched();
1649}
1650
1651#endif /* #else #ifdef CONFIG_SMP */
1652
1653/*
Paul E. McKenneye0f23062011-06-21 01:29:39 -07001654 * This does the RCU core processing work for the specified rcu_state
1655 * and rcu_data structures. This may be called only from the CPU to
1656 * whom the rdp belongs.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001657 */
1658static void
1659__rcu_process_callbacks(struct rcu_state *rsp, struct rcu_data *rdp)
1660{
1661 unsigned long flags;
1662
Paul E. McKenney2e597552009-08-15 09:53:48 -07001663 WARN_ON_ONCE(rdp->beenonline == 0);
1664
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001665 /*
1666 * If an RCU GP has gone long enough, go check for dyntick
1667 * idle CPUs and, if needed, send resched IPIs.
1668 */
Paul E. McKenney20133cf2010-02-22 17:05:01 -08001669 if (ULONG_CMP_LT(ACCESS_ONCE(rsp->jiffies_force_qs), jiffies))
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001670 force_quiescent_state(rsp, 1);
1671
1672 /*
1673 * Advance callbacks in response to end of earlier grace
1674 * period that some other CPU ended.
1675 */
1676 rcu_process_gp_end(rsp, rdp);
1677
1678 /* Update RCU state based on any recent quiescent states. */
1679 rcu_check_quiescent_state(rsp, rdp);
1680
1681 /* Does this CPU require a not-yet-started grace period? */
1682 if (cpu_needs_another_gp(rsp, rdp)) {
Paul E. McKenney1304afb2010-02-22 17:05:02 -08001683 raw_spin_lock_irqsave(&rcu_get_root(rsp)->lock, flags);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001684 rcu_start_gp(rsp, flags); /* releases above lock */
1685 }
1686
1687 /* If there are callbacks ready, invoke them. */
Shaohua Li09223372011-06-14 13:26:25 +08001688 if (cpu_has_callbacks_ready_to_invoke(rdp))
Paul E. McKenneya46e0892011-06-15 15:47:09 -07001689 invoke_rcu_callbacks(rsp, rdp);
Shaohua Li09223372011-06-14 13:26:25 +08001690}
1691
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001692/*
Paul E. McKenneye0f23062011-06-21 01:29:39 -07001693 * Do RCU core processing for the current CPU.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001694 */
Shaohua Li09223372011-06-14 13:26:25 +08001695static void rcu_process_callbacks(struct softirq_action *unused)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001696{
Paul E. McKenney300df912011-06-18 22:26:31 -07001697 trace_rcu_utilization("Start RCU core");
Paul E. McKenneyd6714c22009-08-22 13:56:46 -07001698 __rcu_process_callbacks(&rcu_sched_state,
1699 &__get_cpu_var(rcu_sched_data));
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001700 __rcu_process_callbacks(&rcu_bh_state, &__get_cpu_var(rcu_bh_data));
Paul E. McKenneyf41d9112009-08-22 13:56:52 -07001701 rcu_preempt_process_callbacks();
Paul E. McKenney300df912011-06-18 22:26:31 -07001702 trace_rcu_utilization("End RCU core");
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001703}
1704
Paul E. McKenneya26ac242011-01-12 14:10:23 -08001705/*
Paul E. McKenneye0f23062011-06-21 01:29:39 -07001706 * Schedule RCU callback invocation. If the specified type of RCU
1707 * does not support RCU priority boosting, just do a direct call,
1708 * otherwise wake up the per-CPU kernel kthread. Note that because we
1709 * are running on the current CPU with interrupts disabled, the
1710 * rcu_cpu_kthread_task cannot disappear out from under us.
Paul E. McKenneya26ac242011-01-12 14:10:23 -08001711 */
Paul E. McKenneya46e0892011-06-15 15:47:09 -07001712static void invoke_rcu_callbacks(struct rcu_state *rsp, struct rcu_data *rdp)
Paul E. McKenneya26ac242011-01-12 14:10:23 -08001713{
Paul E. McKenneyb0d30412011-07-10 15:57:35 -07001714 if (unlikely(!ACCESS_ONCE(rcu_scheduler_fully_active)))
1715 return;
Paul E. McKenneya46e0892011-06-15 15:47:09 -07001716 if (likely(!rsp->boost)) {
1717 rcu_do_batch(rsp, rdp);
Paul E. McKenneya26ac242011-01-12 14:10:23 -08001718 return;
1719 }
Paul E. McKenneya46e0892011-06-15 15:47:09 -07001720 invoke_rcu_callbacks_kthread();
Paul E. McKenneya26ac242011-01-12 14:10:23 -08001721}
1722
Paul E. McKenneya46e0892011-06-15 15:47:09 -07001723static void invoke_rcu_core(void)
Shaohua Li09223372011-06-14 13:26:25 +08001724{
1725 raise_softirq(RCU_SOFTIRQ);
1726}
1727
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001728static void
1729__call_rcu(struct rcu_head *head, void (*func)(struct rcu_head *rcu),
Paul E. McKenney486e2592012-01-06 14:11:30 -08001730 struct rcu_state *rsp, bool lazy)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001731{
1732 unsigned long flags;
1733 struct rcu_data *rdp;
1734
Paul E. McKenney0bb7b592012-01-05 14:44:39 -08001735 WARN_ON_ONCE((unsigned long)head & 0x3); /* Misaligned rcu_head! */
Mathieu Desnoyers551d55a2010-04-17 08:48:42 -04001736 debug_rcu_head_queue(head);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001737 head->func = func;
1738 head->next = NULL;
1739
1740 smp_mb(); /* Ensure RCU update seen before callback registry. */
1741
1742 /*
1743 * Opportunistically note grace-period endings and beginnings.
1744 * Note that we might see a beginning right after we see an
1745 * end, but never vice versa, since this CPU has to pass through
1746 * a quiescent state betweentimes.
1747 */
1748 local_irq_save(flags);
Paul E. McKenneye5601402012-01-07 11:03:57 -08001749 WARN_ON_ONCE(cpu_is_offline(smp_processor_id()));
Lai Jiangshan394f99a2010-06-28 16:25:04 +08001750 rdp = this_cpu_ptr(rsp->rda);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001751
1752 /* Add the callback to our list. */
1753 *rdp->nxttail[RCU_NEXT_TAIL] = head;
1754 rdp->nxttail[RCU_NEXT_TAIL] = &head->next;
Paul E. McKenney2655d572011-04-07 22:47:23 -07001755 rdp->qlen++;
Paul E. McKenney486e2592012-01-06 14:11:30 -08001756 if (lazy)
1757 rdp->qlen_lazy++;
Paul E. McKenney2655d572011-04-07 22:47:23 -07001758
Paul E. McKenneyd4c08f22011-06-25 06:36:56 -07001759 if (__is_kfree_rcu_offset((unsigned long)func))
1760 trace_rcu_kfree_callback(rsp->name, head, (unsigned long)func,
Paul E. McKenney486e2592012-01-06 14:11:30 -08001761 rdp->qlen_lazy, rdp->qlen);
Paul E. McKenneyd4c08f22011-06-25 06:36:56 -07001762 else
Paul E. McKenney486e2592012-01-06 14:11:30 -08001763 trace_rcu_callback(rsp->name, head, rdp->qlen_lazy, rdp->qlen);
Paul E. McKenneyd4c08f22011-06-25 06:36:56 -07001764
Paul E. McKenney2655d572011-04-07 22:47:23 -07001765 /* If interrupts were disabled, don't dive into RCU core. */
1766 if (irqs_disabled_flags(flags)) {
1767 local_irq_restore(flags);
1768 return;
1769 }
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001770
Paul E. McKenney37c72e52009-10-14 10:15:55 -07001771 /*
1772 * Force the grace period if too many callbacks or too long waiting.
1773 * Enforce hysteresis, and don't invoke force_quiescent_state()
1774 * if some other CPU has recently done so. Also, don't bother
1775 * invoking force_quiescent_state() if the newly enqueued callback
1776 * is the only one waiting for a grace period to complete.
1777 */
Paul E. McKenney2655d572011-04-07 22:47:23 -07001778 if (unlikely(rdp->qlen > rdp->qlen_last_fqs_check + qhimark)) {
Paul E. McKenneyb52573d2010-12-14 17:36:02 -08001779
1780 /* Are we ignoring a completed grace period? */
1781 rcu_process_gp_end(rsp, rdp);
1782 check_for_new_grace_period(rsp, rdp);
1783
1784 /* Start a new grace period if one not already started. */
1785 if (!rcu_gp_in_progress(rsp)) {
1786 unsigned long nestflag;
1787 struct rcu_node *rnp_root = rcu_get_root(rsp);
1788
1789 raw_spin_lock_irqsave(&rnp_root->lock, nestflag);
1790 rcu_start_gp(rsp, nestflag); /* rlses rnp_root->lock */
1791 } else {
1792 /* Give the grace period a kick. */
1793 rdp->blimit = LONG_MAX;
1794 if (rsp->n_force_qs == rdp->n_force_qs_snap &&
1795 *rdp->nxttail[RCU_DONE_TAIL] != head)
1796 force_quiescent_state(rsp, 0);
1797 rdp->n_force_qs_snap = rsp->n_force_qs;
1798 rdp->qlen_last_fqs_check = rdp->qlen;
1799 }
Paul E. McKenney20133cf2010-02-22 17:05:01 -08001800 } else if (ULONG_CMP_LT(ACCESS_ONCE(rsp->jiffies_force_qs), jiffies))
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001801 force_quiescent_state(rsp, 1);
1802 local_irq_restore(flags);
1803}
1804
1805/*
Paul E. McKenneyd6714c22009-08-22 13:56:46 -07001806 * Queue an RCU-sched callback for invocation after a grace period.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001807 */
Paul E. McKenneyd6714c22009-08-22 13:56:46 -07001808void call_rcu_sched(struct rcu_head *head, void (*func)(struct rcu_head *rcu))
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001809{
Paul E. McKenney486e2592012-01-06 14:11:30 -08001810 __call_rcu(head, func, &rcu_sched_state, 0);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001811}
Paul E. McKenneyd6714c22009-08-22 13:56:46 -07001812EXPORT_SYMBOL_GPL(call_rcu_sched);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001813
1814/*
Paul E. McKenney486e2592012-01-06 14:11:30 -08001815 * Queue an RCU callback for invocation after a quicker grace period.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001816 */
1817void call_rcu_bh(struct rcu_head *head, void (*func)(struct rcu_head *rcu))
1818{
Paul E. McKenney486e2592012-01-06 14:11:30 -08001819 __call_rcu(head, func, &rcu_bh_state, 0);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001820}
1821EXPORT_SYMBOL_GPL(call_rcu_bh);
1822
Paul E. McKenney6ebb2372009-11-22 08:53:50 -08001823/**
1824 * synchronize_sched - wait until an rcu-sched grace period has elapsed.
1825 *
1826 * Control will return to the caller some time after a full rcu-sched
1827 * grace period has elapsed, in other words after all currently executing
1828 * rcu-sched read-side critical sections have completed. These read-side
1829 * critical sections are delimited by rcu_read_lock_sched() and
1830 * rcu_read_unlock_sched(), and may be nested. Note that preempt_disable(),
1831 * local_irq_disable(), and so on may be used in place of
1832 * rcu_read_lock_sched().
1833 *
1834 * This means that all preempt_disable code sequences, including NMI and
1835 * hardware-interrupt handlers, in progress on entry will have completed
1836 * before this primitive returns. However, this does not guarantee that
1837 * softirq handlers will have completed, since in some kernels, these
1838 * handlers can run in process context, and can block.
1839 *
1840 * This primitive provides the guarantees made by the (now removed)
1841 * synchronize_kernel() API. In contrast, synchronize_rcu() only
1842 * guarantees that rcu_read_lock() sections will have completed.
1843 * In "classic RCU", these two guarantees happen to be one and
1844 * the same, but can differ in realtime RCU implementations.
1845 */
1846void synchronize_sched(void)
1847{
Paul E. McKenneyfe15d702012-01-04 13:30:33 -08001848 rcu_lockdep_assert(!lock_is_held(&rcu_bh_lock_map) &&
1849 !lock_is_held(&rcu_lock_map) &&
1850 !lock_is_held(&rcu_sched_lock_map),
1851 "Illegal synchronize_sched() in RCU-sched read-side critical section");
Paul E. McKenney6ebb2372009-11-22 08:53:50 -08001852 if (rcu_blocking_is_gp())
1853 return;
Paul E. McKenney2c428182011-05-26 22:14:36 -07001854 wait_rcu_gp(call_rcu_sched);
Paul E. McKenney6ebb2372009-11-22 08:53:50 -08001855}
1856EXPORT_SYMBOL_GPL(synchronize_sched);
1857
1858/**
1859 * synchronize_rcu_bh - wait until an rcu_bh grace period has elapsed.
1860 *
1861 * Control will return to the caller some time after a full rcu_bh grace
1862 * period has elapsed, in other words after all currently executing rcu_bh
1863 * read-side critical sections have completed. RCU read-side critical
1864 * sections are delimited by rcu_read_lock_bh() and rcu_read_unlock_bh(),
1865 * and may be nested.
1866 */
1867void synchronize_rcu_bh(void)
1868{
Paul E. McKenneyfe15d702012-01-04 13:30:33 -08001869 rcu_lockdep_assert(!lock_is_held(&rcu_bh_lock_map) &&
1870 !lock_is_held(&rcu_lock_map) &&
1871 !lock_is_held(&rcu_sched_lock_map),
1872 "Illegal synchronize_rcu_bh() in RCU-bh read-side critical section");
Paul E. McKenney6ebb2372009-11-22 08:53:50 -08001873 if (rcu_blocking_is_gp())
1874 return;
Paul E. McKenney2c428182011-05-26 22:14:36 -07001875 wait_rcu_gp(call_rcu_bh);
Paul E. McKenney6ebb2372009-11-22 08:53:50 -08001876}
1877EXPORT_SYMBOL_GPL(synchronize_rcu_bh);
1878
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001879/*
1880 * Check to see if there is any immediate RCU-related work to be done
1881 * by the current CPU, for the specified type of RCU, returning 1 if so.
1882 * The checks are in order of increasing expense: checks that can be
1883 * carried out against CPU-local state are performed first. However,
1884 * we must check for CPU stalls first, else we might not get a chance.
1885 */
1886static int __rcu_pending(struct rcu_state *rsp, struct rcu_data *rdp)
1887{
Paul E. McKenney2f51f982009-11-13 19:51:39 -08001888 struct rcu_node *rnp = rdp->mynode;
1889
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001890 rdp->n_rcu_pending++;
1891
1892 /* Check for CPU stalls, if enabled. */
1893 check_cpu_stall(rsp, rdp);
1894
1895 /* Is the RCU core waiting for a quiescent state from this CPU? */
Paul E. McKenney5c51dd72011-08-04 06:59:03 -07001896 if (rcu_scheduler_fully_active &&
1897 rdp->qs_pending && !rdp->passed_quiesce) {
Paul E. McKenneyd25eb942010-03-18 21:36:51 -07001898
1899 /*
1900 * If force_quiescent_state() coming soon and this CPU
1901 * needs a quiescent state, and this is either RCU-sched
1902 * or RCU-bh, force a local reschedule.
1903 */
Paul E. McKenneyd21670a2010-04-14 17:39:26 -07001904 rdp->n_rp_qs_pending++;
Paul E. McKenney6cc68792011-03-02 13:15:15 -08001905 if (!rdp->preemptible &&
Paul E. McKenneyd25eb942010-03-18 21:36:51 -07001906 ULONG_CMP_LT(ACCESS_ONCE(rsp->jiffies_force_qs) - 1,
1907 jiffies))
1908 set_need_resched();
Paul E. McKenneye4cc1f22011-06-27 00:17:43 -07001909 } else if (rdp->qs_pending && rdp->passed_quiesce) {
Paul E. McKenneyd21670a2010-04-14 17:39:26 -07001910 rdp->n_rp_report_qs++;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001911 return 1;
Paul E. McKenney7ba5c842009-04-13 21:31:17 -07001912 }
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001913
1914 /* Does this CPU have callbacks ready to invoke? */
Paul E. McKenney7ba5c842009-04-13 21:31:17 -07001915 if (cpu_has_callbacks_ready_to_invoke(rdp)) {
1916 rdp->n_rp_cb_ready++;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001917 return 1;
Paul E. McKenney7ba5c842009-04-13 21:31:17 -07001918 }
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001919
1920 /* Has RCU gone idle with this CPU needing another grace period? */
Paul E. McKenney7ba5c842009-04-13 21:31:17 -07001921 if (cpu_needs_another_gp(rsp, rdp)) {
1922 rdp->n_rp_cpu_needs_gp++;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001923 return 1;
Paul E. McKenney7ba5c842009-04-13 21:31:17 -07001924 }
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001925
1926 /* Has another RCU grace period completed? */
Paul E. McKenney2f51f982009-11-13 19:51:39 -08001927 if (ACCESS_ONCE(rnp->completed) != rdp->completed) { /* outside lock */
Paul E. McKenney7ba5c842009-04-13 21:31:17 -07001928 rdp->n_rp_gp_completed++;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001929 return 1;
Paul E. McKenney7ba5c842009-04-13 21:31:17 -07001930 }
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001931
1932 /* Has a new RCU grace period started? */
Paul E. McKenney2f51f982009-11-13 19:51:39 -08001933 if (ACCESS_ONCE(rnp->gpnum) != rdp->gpnum) { /* outside lock */
Paul E. McKenney7ba5c842009-04-13 21:31:17 -07001934 rdp->n_rp_gp_started++;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001935 return 1;
Paul E. McKenney7ba5c842009-04-13 21:31:17 -07001936 }
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001937
1938 /* Has an RCU GP gone long enough to send resched IPIs &c? */
Paul E. McKenneyfc2219d2009-09-23 09:50:41 -07001939 if (rcu_gp_in_progress(rsp) &&
Paul E. McKenney20133cf2010-02-22 17:05:01 -08001940 ULONG_CMP_LT(ACCESS_ONCE(rsp->jiffies_force_qs), jiffies)) {
Paul E. McKenney7ba5c842009-04-13 21:31:17 -07001941 rdp->n_rp_need_fqs++;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001942 return 1;
Paul E. McKenney7ba5c842009-04-13 21:31:17 -07001943 }
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001944
1945 /* nothing to do */
Paul E. McKenney7ba5c842009-04-13 21:31:17 -07001946 rdp->n_rp_need_nothing++;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001947 return 0;
1948}
1949
1950/*
1951 * Check to see if there is any immediate RCU-related work to be done
1952 * by the current CPU, returning 1 if so. This function is part of the
1953 * RCU implementation; it is -not- an exported member of the RCU API.
1954 */
Paul E. McKenneya1572292009-08-22 13:56:51 -07001955static int rcu_pending(int cpu)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001956{
Paul E. McKenneyd6714c22009-08-22 13:56:46 -07001957 return __rcu_pending(&rcu_sched_state, &per_cpu(rcu_sched_data, cpu)) ||
Paul E. McKenneyf41d9112009-08-22 13:56:52 -07001958 __rcu_pending(&rcu_bh_state, &per_cpu(rcu_bh_data, cpu)) ||
1959 rcu_preempt_pending(cpu);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001960}
1961
1962/*
1963 * Check to see if any future RCU-related work will need to be done
1964 * by the current CPU, even if none need be done immediately, returning
Paul E. McKenney8bd93a22010-02-22 17:04:59 -08001965 * 1 if so.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001966 */
Paul E. McKenneyaea1b352011-11-02 06:54:54 -07001967static int rcu_cpu_has_callbacks(int cpu)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001968{
1969 /* RCU callbacks either ready or pending? */
Paul E. McKenneyd6714c22009-08-22 13:56:46 -07001970 return per_cpu(rcu_sched_data, cpu).nxtlist ||
Paul E. McKenneyf41d9112009-08-22 13:56:52 -07001971 per_cpu(rcu_bh_data, cpu).nxtlist ||
Paul E. McKenney30fbcc92012-01-12 11:01:14 -08001972 rcu_preempt_cpu_has_callbacks(cpu);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001973}
1974
Paul E. McKenneyd0ec7742009-10-06 21:48:16 -07001975static DEFINE_PER_CPU(struct rcu_head, rcu_barrier_head) = {NULL};
1976static atomic_t rcu_barrier_cpu_count;
1977static DEFINE_MUTEX(rcu_barrier_mutex);
1978static struct completion rcu_barrier_completion;
Paul E. McKenneyd0ec7742009-10-06 21:48:16 -07001979
1980static void rcu_barrier_callback(struct rcu_head *notused)
1981{
1982 if (atomic_dec_and_test(&rcu_barrier_cpu_count))
1983 complete(&rcu_barrier_completion);
1984}
1985
1986/*
1987 * Called with preemption disabled, and from cross-cpu IRQ context.
1988 */
1989static void rcu_barrier_func(void *type)
1990{
1991 int cpu = smp_processor_id();
1992 struct rcu_head *head = &per_cpu(rcu_barrier_head, cpu);
1993 void (*call_rcu_func)(struct rcu_head *head,
1994 void (*func)(struct rcu_head *head));
1995
1996 atomic_inc(&rcu_barrier_cpu_count);
1997 call_rcu_func = type;
1998 call_rcu_func(head, rcu_barrier_callback);
1999}
2000
Paul E. McKenneyd0ec7742009-10-06 21:48:16 -07002001/*
2002 * Orchestrate the specified type of RCU barrier, waiting for all
2003 * RCU callbacks of the specified type to complete.
2004 */
Paul E. McKenneye74f4c42009-10-06 21:48:17 -07002005static void _rcu_barrier(struct rcu_state *rsp,
2006 void (*call_rcu_func)(struct rcu_head *head,
Paul E. McKenneyd0ec7742009-10-06 21:48:16 -07002007 void (*func)(struct rcu_head *head)))
2008{
2009 BUG_ON(in_interrupt());
Paul E. McKenneye74f4c42009-10-06 21:48:17 -07002010 /* Take mutex to serialize concurrent rcu_barrier() requests. */
Paul E. McKenneyd0ec7742009-10-06 21:48:16 -07002011 mutex_lock(&rcu_barrier_mutex);
2012 init_completion(&rcu_barrier_completion);
2013 /*
2014 * Initialize rcu_barrier_cpu_count to 1, then invoke
2015 * rcu_barrier_func() on each CPU, so that each CPU also has
2016 * incremented rcu_barrier_cpu_count. Only then is it safe to
2017 * decrement rcu_barrier_cpu_count -- otherwise the first CPU
2018 * might complete its grace period before all of the other CPUs
2019 * did their increment, causing this function to return too
Paul E. McKenney2d999e02010-10-20 12:06:18 -07002020 * early. Note that on_each_cpu() disables irqs, which prevents
2021 * any CPUs from coming online or going offline until each online
2022 * CPU has queued its RCU-barrier callback.
Paul E. McKenneyd0ec7742009-10-06 21:48:16 -07002023 */
2024 atomic_set(&rcu_barrier_cpu_count, 1);
2025 on_each_cpu(rcu_barrier_func, (void *)call_rcu_func, 1);
2026 if (atomic_dec_and_test(&rcu_barrier_cpu_count))
2027 complete(&rcu_barrier_completion);
2028 wait_for_completion(&rcu_barrier_completion);
2029 mutex_unlock(&rcu_barrier_mutex);
Paul E. McKenneyd0ec7742009-10-06 21:48:16 -07002030}
2031
2032/**
Paul E. McKenneyd0ec7742009-10-06 21:48:16 -07002033 * rcu_barrier_bh - Wait until all in-flight call_rcu_bh() callbacks complete.
2034 */
2035void rcu_barrier_bh(void)
2036{
Paul E. McKenneye74f4c42009-10-06 21:48:17 -07002037 _rcu_barrier(&rcu_bh_state, call_rcu_bh);
Paul E. McKenneyd0ec7742009-10-06 21:48:16 -07002038}
2039EXPORT_SYMBOL_GPL(rcu_barrier_bh);
2040
2041/**
2042 * rcu_barrier_sched - Wait for in-flight call_rcu_sched() callbacks.
2043 */
2044void rcu_barrier_sched(void)
2045{
Paul E. McKenneye74f4c42009-10-06 21:48:17 -07002046 _rcu_barrier(&rcu_sched_state, call_rcu_sched);
Paul E. McKenneyd0ec7742009-10-06 21:48:16 -07002047}
2048EXPORT_SYMBOL_GPL(rcu_barrier_sched);
2049
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002050/*
Paul E. McKenney27569622009-08-15 09:53:46 -07002051 * Do boot-time initialization of a CPU's per-CPU RCU data.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002052 */
Paul E. McKenney27569622009-08-15 09:53:46 -07002053static void __init
2054rcu_boot_init_percpu_data(int cpu, struct rcu_state *rsp)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002055{
2056 unsigned long flags;
2057 int i;
Lai Jiangshan394f99a2010-06-28 16:25:04 +08002058 struct rcu_data *rdp = per_cpu_ptr(rsp->rda, cpu);
Paul E. McKenney27569622009-08-15 09:53:46 -07002059 struct rcu_node *rnp = rcu_get_root(rsp);
2060
2061 /* Set up local state, ensuring consistent view of global state. */
Paul E. McKenney1304afb2010-02-22 17:05:02 -08002062 raw_spin_lock_irqsave(&rnp->lock, flags);
Paul E. McKenney27569622009-08-15 09:53:46 -07002063 rdp->grpmask = 1UL << (cpu - rdp->mynode->grplo);
2064 rdp->nxtlist = NULL;
2065 for (i = 0; i < RCU_NEXT_SIZE; i++)
2066 rdp->nxttail[i] = &rdp->nxtlist;
Paul E. McKenney486e2592012-01-06 14:11:30 -08002067 rdp->qlen_lazy = 0;
Paul E. McKenney27569622009-08-15 09:53:46 -07002068 rdp->qlen = 0;
Paul E. McKenney27569622009-08-15 09:53:46 -07002069 rdp->dynticks = &per_cpu(rcu_dynticks, cpu);
Paul E. McKenney4145fa72011-10-31 15:01:54 -07002070 WARN_ON_ONCE(rdp->dynticks->dynticks_nesting != DYNTICK_TASK_NESTING);
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -07002071 WARN_ON_ONCE(atomic_read(&rdp->dynticks->dynticks) != 1);
Paul E. McKenney27569622009-08-15 09:53:46 -07002072 rdp->cpu = cpu;
Paul E. McKenneyd4c08f22011-06-25 06:36:56 -07002073 rdp->rsp = rsp;
Paul E. McKenney1304afb2010-02-22 17:05:02 -08002074 raw_spin_unlock_irqrestore(&rnp->lock, flags);
Paul E. McKenney27569622009-08-15 09:53:46 -07002075}
2076
2077/*
2078 * Initialize a CPU's per-CPU RCU data. Note that only one online or
2079 * offline event can be happening at a given time. Note also that we
2080 * can accept some slop in the rsp->completed access due to the fact
2081 * that this CPU cannot possibly have any RCU callbacks in flight yet.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002082 */
2083static void __cpuinit
Paul E. McKenney6cc68792011-03-02 13:15:15 -08002084rcu_init_percpu_data(int cpu, struct rcu_state *rsp, int preemptible)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002085{
2086 unsigned long flags;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002087 unsigned long mask;
Lai Jiangshan394f99a2010-06-28 16:25:04 +08002088 struct rcu_data *rdp = per_cpu_ptr(rsp->rda, cpu);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002089 struct rcu_node *rnp = rcu_get_root(rsp);
2090
2091 /* Set up local state, ensuring consistent view of global state. */
Paul E. McKenney1304afb2010-02-22 17:05:02 -08002092 raw_spin_lock_irqsave(&rnp->lock, flags);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002093 rdp->beenonline = 1; /* We have now been online. */
Paul E. McKenney6cc68792011-03-02 13:15:15 -08002094 rdp->preemptible = preemptible;
Paul E. McKenney37c72e52009-10-14 10:15:55 -07002095 rdp->qlen_last_fqs_check = 0;
2096 rdp->n_force_qs_snap = rsp->n_force_qs;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002097 rdp->blimit = blimit;
Paul E. McKenneyc92b1312011-11-23 13:38:58 -08002098 rdp->dynticks->dynticks_nesting = DYNTICK_TASK_NESTING;
2099 atomic_set(&rdp->dynticks->dynticks,
2100 (atomic_read(&rdp->dynticks->dynticks) & ~0x1) + 1);
Paul E. McKenney7cb92492011-11-28 12:28:34 -08002101 rcu_prepare_for_idle_init(cpu);
Paul E. McKenney1304afb2010-02-22 17:05:02 -08002102 raw_spin_unlock(&rnp->lock); /* irqs remain disabled. */
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002103
2104 /*
2105 * A new grace period might start here. If so, we won't be part
2106 * of it, but that is OK, as we are currently in a quiescent state.
2107 */
2108
2109 /* Exclude any attempts to start a new GP on large systems. */
Paul E. McKenney1304afb2010-02-22 17:05:02 -08002110 raw_spin_lock(&rsp->onofflock); /* irqs already disabled. */
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002111
2112 /* Add CPU to rcu_node bitmasks. */
2113 rnp = rdp->mynode;
2114 mask = rdp->grpmask;
2115 do {
2116 /* Exclude any attempts to start a new GP on small systems. */
Paul E. McKenney1304afb2010-02-22 17:05:02 -08002117 raw_spin_lock(&rnp->lock); /* irqs already disabled. */
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002118 rnp->qsmaskinit |= mask;
2119 mask = rnp->grpmask;
Paul E. McKenneyd09b62d2009-11-02 13:52:28 -08002120 if (rnp == rdp->mynode) {
Paul E. McKenney06ae1152011-08-14 15:56:54 -07002121 /*
2122 * If there is a grace period in progress, we will
2123 * set up to wait for it next time we run the
2124 * RCU core code.
2125 */
2126 rdp->gpnum = rnp->completed;
Paul E. McKenneyd09b62d2009-11-02 13:52:28 -08002127 rdp->completed = rnp->completed;
Paul E. McKenney06ae1152011-08-14 15:56:54 -07002128 rdp->passed_quiesce = 0;
2129 rdp->qs_pending = 0;
Paul E. McKenneye4cc1f22011-06-27 00:17:43 -07002130 rdp->passed_quiesce_gpnum = rnp->gpnum - 1;
Paul E. McKenneyd4c08f22011-06-25 06:36:56 -07002131 trace_rcu_grace_period(rsp->name, rdp->gpnum, "cpuonl");
Paul E. McKenneyd09b62d2009-11-02 13:52:28 -08002132 }
Paul E. McKenney1304afb2010-02-22 17:05:02 -08002133 raw_spin_unlock(&rnp->lock); /* irqs already disabled. */
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002134 rnp = rnp->parent;
2135 } while (rnp != NULL && !(rnp->qsmaskinit & mask));
2136
Paul E. McKenney1304afb2010-02-22 17:05:02 -08002137 raw_spin_unlock_irqrestore(&rsp->onofflock, flags);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002138}
2139
Peter Zijlstrad72bce02011-05-30 13:34:51 +02002140static void __cpuinit rcu_prepare_cpu(int cpu)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002141{
Paul E. McKenneyf41d9112009-08-22 13:56:52 -07002142 rcu_init_percpu_data(cpu, &rcu_sched_state, 0);
2143 rcu_init_percpu_data(cpu, &rcu_bh_state, 0);
2144 rcu_preempt_init_percpu_data(cpu);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002145}
2146
2147/*
Paul E. McKenneyf41d9112009-08-22 13:56:52 -07002148 * Handle CPU online/offline notification events.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002149 */
Paul E. McKenney9f680ab2009-11-22 08:53:49 -08002150static int __cpuinit rcu_cpu_notify(struct notifier_block *self,
2151 unsigned long action, void *hcpu)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002152{
2153 long cpu = (long)hcpu;
Paul E. McKenney27f4d282011-02-07 12:47:15 -08002154 struct rcu_data *rdp = per_cpu_ptr(rcu_state->rda, cpu);
Paul E. McKenneya26ac242011-01-12 14:10:23 -08002155 struct rcu_node *rnp = rdp->mynode;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002156
Paul E. McKenney300df912011-06-18 22:26:31 -07002157 trace_rcu_utilization("Start CPU hotplug");
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002158 switch (action) {
2159 case CPU_UP_PREPARE:
2160 case CPU_UP_PREPARE_FROZEN:
Peter Zijlstrad72bce02011-05-30 13:34:51 +02002161 rcu_prepare_cpu(cpu);
2162 rcu_prepare_kthreads(cpu);
Paul E. McKenneya26ac242011-01-12 14:10:23 -08002163 break;
2164 case CPU_ONLINE:
Paul E. McKenney0f962a52011-04-14 12:13:53 -07002165 case CPU_DOWN_FAILED:
2166 rcu_node_kthread_setaffinity(rnp, -1);
Paul E. McKenneye3995a22011-04-18 15:31:26 -07002167 rcu_cpu_kthread_setrt(cpu, 1);
Paul E. McKenney0f962a52011-04-14 12:13:53 -07002168 break;
2169 case CPU_DOWN_PREPARE:
2170 rcu_node_kthread_setaffinity(rnp, cpu);
Paul E. McKenneye3995a22011-04-18 15:31:26 -07002171 rcu_cpu_kthread_setrt(cpu, 0);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002172 break;
Paul E. McKenneyd0ec7742009-10-06 21:48:16 -07002173 case CPU_DYING:
2174 case CPU_DYING_FROZEN:
2175 /*
Paul E. McKenney2d999e02010-10-20 12:06:18 -07002176 * The whole machine is "stopped" except this CPU, so we can
2177 * touch any data without introducing corruption. We send the
2178 * dying CPU's callbacks to an arbitrarily chosen online CPU.
Paul E. McKenneyd0ec7742009-10-06 21:48:16 -07002179 */
Paul E. McKenneye5601402012-01-07 11:03:57 -08002180 rcu_cleanup_dying_cpu(&rcu_bh_state);
2181 rcu_cleanup_dying_cpu(&rcu_sched_state);
2182 rcu_preempt_cleanup_dying_cpu();
Paul E. McKenney7cb92492011-11-28 12:28:34 -08002183 rcu_cleanup_after_idle(cpu);
Paul E. McKenneyd0ec7742009-10-06 21:48:16 -07002184 break;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002185 case CPU_DEAD:
2186 case CPU_DEAD_FROZEN:
2187 case CPU_UP_CANCELED:
2188 case CPU_UP_CANCELED_FROZEN:
Paul E. McKenneye5601402012-01-07 11:03:57 -08002189 rcu_cleanup_dead_cpu(cpu, &rcu_bh_state);
2190 rcu_cleanup_dead_cpu(cpu, &rcu_sched_state);
2191 rcu_preempt_cleanup_dead_cpu(cpu);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002192 break;
2193 default:
2194 break;
2195 }
Paul E. McKenney300df912011-06-18 22:26:31 -07002196 trace_rcu_utilization("End CPU hotplug");
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002197 return NOTIFY_OK;
2198}
2199
2200/*
Paul E. McKenneybbad9372010-04-02 16:17:17 -07002201 * This function is invoked towards the end of the scheduler's initialization
2202 * process. Before this is called, the idle task might contain
2203 * RCU read-side critical sections (during which time, this idle
2204 * task is booting the system). After this function is called, the
2205 * idle tasks are prohibited from containing RCU read-side critical
2206 * sections. This function also enables RCU lockdep checking.
2207 */
2208void rcu_scheduler_starting(void)
2209{
2210 WARN_ON(num_online_cpus() != 1);
2211 WARN_ON(nr_context_switches() > 0);
2212 rcu_scheduler_active = 1;
2213}
2214
2215/*
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002216 * Compute the per-level fanout, either using the exact fanout specified
2217 * or balancing the tree, depending on CONFIG_RCU_FANOUT_EXACT.
2218 */
2219#ifdef CONFIG_RCU_FANOUT_EXACT
2220static void __init rcu_init_levelspread(struct rcu_state *rsp)
2221{
2222 int i;
2223
Paul E. McKenney0209f642010-12-14 16:07:52 -08002224 for (i = NUM_RCU_LVLS - 1; i > 0; i--)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002225 rsp->levelspread[i] = CONFIG_RCU_FANOUT;
Paul E. McKenney0209f642010-12-14 16:07:52 -08002226 rsp->levelspread[0] = RCU_FANOUT_LEAF;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002227}
2228#else /* #ifdef CONFIG_RCU_FANOUT_EXACT */
2229static void __init rcu_init_levelspread(struct rcu_state *rsp)
2230{
2231 int ccur;
2232 int cprv;
2233 int i;
2234
2235 cprv = NR_CPUS;
2236 for (i = NUM_RCU_LVLS - 1; i >= 0; i--) {
2237 ccur = rsp->levelcnt[i];
2238 rsp->levelspread[i] = (cprv + ccur - 1) / ccur;
2239 cprv = ccur;
2240 }
2241}
2242#endif /* #else #ifdef CONFIG_RCU_FANOUT_EXACT */
2243
2244/*
2245 * Helper function for rcu_init() that initializes one rcu_state structure.
2246 */
Lai Jiangshan394f99a2010-06-28 16:25:04 +08002247static void __init rcu_init_one(struct rcu_state *rsp,
2248 struct rcu_data __percpu *rda)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002249{
Paul E. McKenneyb6407e82010-01-04 16:04:02 -08002250 static char *buf[] = { "rcu_node_level_0",
2251 "rcu_node_level_1",
2252 "rcu_node_level_2",
2253 "rcu_node_level_3" }; /* Match MAX_RCU_LVLS */
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002254 int cpustride = 1;
2255 int i;
2256 int j;
2257 struct rcu_node *rnp;
2258
Paul E. McKenneyb6407e82010-01-04 16:04:02 -08002259 BUILD_BUG_ON(MAX_RCU_LVLS > ARRAY_SIZE(buf)); /* Fix buf[] init! */
2260
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002261 /* Initialize the level-tracking arrays. */
2262
2263 for (i = 1; i < NUM_RCU_LVLS; i++)
2264 rsp->level[i] = rsp->level[i - 1] + rsp->levelcnt[i - 1];
2265 rcu_init_levelspread(rsp);
2266
2267 /* Initialize the elements themselves, starting from the leaves. */
2268
2269 for (i = NUM_RCU_LVLS - 1; i >= 0; i--) {
2270 cpustride *= rsp->levelspread[i];
2271 rnp = rsp->level[i];
2272 for (j = 0; j < rsp->levelcnt[i]; j++, rnp++) {
Paul E. McKenney1304afb2010-02-22 17:05:02 -08002273 raw_spin_lock_init(&rnp->lock);
Paul E. McKenneyb6407e82010-01-04 16:04:02 -08002274 lockdep_set_class_and_name(&rnp->lock,
2275 &rcu_node_class[i], buf[i]);
Paul E. McKenneyf41d9112009-08-22 13:56:52 -07002276 rnp->gpnum = 0;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002277 rnp->qsmask = 0;
2278 rnp->qsmaskinit = 0;
2279 rnp->grplo = j * cpustride;
2280 rnp->grphi = (j + 1) * cpustride - 1;
2281 if (rnp->grphi >= NR_CPUS)
2282 rnp->grphi = NR_CPUS - 1;
2283 if (i == 0) {
2284 rnp->grpnum = 0;
2285 rnp->grpmask = 0;
2286 rnp->parent = NULL;
2287 } else {
2288 rnp->grpnum = j % rsp->levelspread[i - 1];
2289 rnp->grpmask = 1UL << rnp->grpnum;
2290 rnp->parent = rsp->level[i - 1] +
2291 j / rsp->levelspread[i - 1];
2292 }
2293 rnp->level = i;
Paul E. McKenney12f5f522010-11-29 21:56:39 -08002294 INIT_LIST_HEAD(&rnp->blkd_tasks);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002295 }
2296 }
Lai Jiangshan0c340292010-03-28 11:12:30 +08002297
Lai Jiangshan394f99a2010-06-28 16:25:04 +08002298 rsp->rda = rda;
Lai Jiangshan0c340292010-03-28 11:12:30 +08002299 rnp = rsp->level[NUM_RCU_LVLS - 1];
2300 for_each_possible_cpu(i) {
Paul E. McKenney4a90a062010-04-14 16:48:11 -07002301 while (i > rnp->grphi)
Lai Jiangshan0c340292010-03-28 11:12:30 +08002302 rnp++;
Lai Jiangshan394f99a2010-06-28 16:25:04 +08002303 per_cpu_ptr(rsp->rda, i)->mynode = rnp;
Lai Jiangshan0c340292010-03-28 11:12:30 +08002304 rcu_boot_init_percpu_data(i, rsp);
2305 }
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002306}
2307
Paul E. McKenney9f680ab2009-11-22 08:53:49 -08002308void __init rcu_init(void)
Paul E. McKenneyf41d9112009-08-22 13:56:52 -07002309{
Paul E. McKenney017c4262010-01-14 16:10:58 -08002310 int cpu;
Paul E. McKenney9f680ab2009-11-22 08:53:49 -08002311
Paul E. McKenneyf41d9112009-08-22 13:56:52 -07002312 rcu_bootup_announce();
Lai Jiangshan394f99a2010-06-28 16:25:04 +08002313 rcu_init_one(&rcu_sched_state, &rcu_sched_data);
2314 rcu_init_one(&rcu_bh_state, &rcu_bh_data);
Paul E. McKenneyf41d9112009-08-22 13:56:52 -07002315 __rcu_init_preempt();
Shaohua Li09223372011-06-14 13:26:25 +08002316 open_softirq(RCU_SOFTIRQ, rcu_process_callbacks);
Paul E. McKenney9f680ab2009-11-22 08:53:49 -08002317
2318 /*
2319 * We don't need protection against CPU-hotplug here because
2320 * this is called early in boot, before either interrupts
2321 * or the scheduler are operational.
2322 */
2323 cpu_notifier(rcu_cpu_notify, 0);
Paul E. McKenney017c4262010-01-14 16:10:58 -08002324 for_each_online_cpu(cpu)
2325 rcu_cpu_notify(NULL, CPU_UP_PREPARE, (void *)(long)cpu);
Paul E. McKenneyc68de202010-04-15 10:12:40 -07002326 check_cpu_stall_init();
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002327}
2328
Paul E. McKenney1eba8f82009-09-23 09:50:42 -07002329#include "rcutree_plugin.h"