blob: a739292be6058aa2f256517a457a5ca8f625eb48 [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
Paul E. McKenney87de1cf2013-12-03 10:02:52 -080015 * along with this program; if not, you can access it online at
16 * http://www.gnu.org/licenses/gpl-2.0.html.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +010017 *
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>
Paul E. McKenney4102ada2013-10-08 20:23:47 -070044#include <linux/module.h>
Paul E. McKenney64db4cf2008-12-18 21:55:32 +010045#include <linux/percpu.h>
46#include <linux/notifier.h>
47#include <linux/cpu.h>
48#include <linux/mutex.h>
49#include <linux/time.h>
Paul E. McKenneybbad9372010-04-02 16:17:17 -070050#include <linux/kernel_stat.h>
Paul E. McKenneya26ac242011-01-12 14:10:23 -080051#include <linux/wait.h>
52#include <linux/kthread.h>
Linus Torvalds268bb0c2011-05-20 12:50:29 -070053#include <linux/prefetch.h>
Paul E. McKenney3d3b7db2012-01-23 17:05:46 -080054#include <linux/delay.h>
55#include <linux/stop_machine.h>
Paul E. McKenney661a85d2012-07-07 05:57:03 -070056#include <linux/random.h>
Steven Rostedt (Red Hat)af658dc2015-04-29 14:36:05 -040057#include <linux/trace_events.h>
Borislav Petkovd1d74d12013-04-22 00:12:42 +020058#include <linux/suspend.h>
Paul E. McKenney64db4cf2008-12-18 21:55:32 +010059
Paul E. McKenney4102ada2013-10-08 20:23:47 -070060#include "tree.h"
Paul E. McKenney29c00b42011-06-17 15:53:19 -070061#include "rcu.h"
Paul E. McKenney9f77da92009-08-22 13:56:45 -070062
Paul E. McKenney4102ada2013-10-08 20:23:47 -070063MODULE_ALIAS("rcutree");
64#ifdef MODULE_PARAM_PREFIX
65#undef MODULE_PARAM_PREFIX
66#endif
67#define MODULE_PARAM_PREFIX "rcutree."
68
Paul E. McKenney64db4cf2008-12-18 21:55:32 +010069/* Data structures. */
70
Steven Rostedt (Red Hat)f7f7bac2013-07-12 17:18:47 -040071/*
72 * In order to export the rcu_state name to the tracing tools, it
73 * needs to be added in the __tracepoint_string section.
74 * This requires defining a separate variable tp_<sname>_varname
75 * that points to the string being used, and this will allow
76 * the tracing userspace tools to be able to decipher the string
77 * address to the matching string.
78 */
Ard Biesheuvela8a29b32014-07-12 19:01:49 +020079#ifdef CONFIG_TRACING
80# define DEFINE_RCU_TPS(sname) \
Steven Rostedt (Red Hat)f7f7bac2013-07-12 17:18:47 -040081static char sname##_varname[] = #sname; \
Ard Biesheuvela8a29b32014-07-12 19:01:49 +020082static const char *tp_##sname##_varname __used __tracepoint_string = sname##_varname;
83# define RCU_STATE_NAME(sname) sname##_varname
84#else
85# define DEFINE_RCU_TPS(sname)
86# define RCU_STATE_NAME(sname) __stringify(sname)
87#endif
88
89#define RCU_STATE_INITIALIZER(sname, sabbr, cr) \
90DEFINE_RCU_TPS(sname) \
Nicolas Ioossc92fb052015-05-05 21:57:06 +080091static DEFINE_PER_CPU_SHARED_ALIGNED(struct rcu_data, sname##_data); \
Steven Rostedt (Red Hat)a41bfeb2013-07-12 17:00:28 -040092struct rcu_state sname##_state = { \
Paul E. McKenney6c90cc72012-05-28 19:41:41 -070093 .level = { &sname##_state.node[0] }, \
Paul E. McKenney27232492015-01-20 22:44:13 -080094 .rda = &sname##_data, \
Paul E. McKenney037b64e2012-05-28 23:26:01 -070095 .call = cr, \
Petr Mladek77f81fe2015-09-09 12:09:49 -070096 .gp_state = RCU_GP_IDLE, \
Paul E. McKenney42c35332012-09-28 10:49:58 -070097 .gpnum = 0UL - 300UL, \
98 .completed = 0UL - 300UL, \
Paul E. McKenney7b2e6012012-10-08 10:54:03 -070099 .orphan_lock = __RAW_SPIN_LOCK_UNLOCKED(&sname##_state.orphan_lock), \
Paul E. McKenney6c90cc72012-05-28 19:41:41 -0700100 .orphan_nxttail = &sname##_state.orphan_nxtlist, \
101 .orphan_donetail = &sname##_state.orphan_donelist, \
Paul E. McKenney7be7f0b2012-05-29 05:18:53 -0700102 .barrier_mutex = __MUTEX_INITIALIZER(sname##_state.barrier_mutex), \
Ard Biesheuvela8a29b32014-07-12 19:01:49 +0200103 .name = RCU_STATE_NAME(sname), \
Paul E. McKenneya4889852012-12-03 08:16:28 -0800104 .abbr = sabbr, \
Paul E. McKenney27232492015-01-20 22:44:13 -0800105}
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100106
Steven Rostedt (Red Hat)a41bfeb2013-07-12 17:00:28 -0400107RCU_STATE_INITIALIZER(rcu_sched, 's', call_rcu_sched);
108RCU_STATE_INITIALIZER(rcu_bh, 'b', call_rcu_bh);
Ingo Molnarb1f77b02009-03-13 03:20:49 +0100109
Paul E. McKenneyb28a7c02015-03-04 07:39:27 -0800110static struct rcu_state *const rcu_state_p;
Paul E. McKenney6ce75a22012-06-12 11:01:13 -0700111LIST_HEAD(rcu_struct_flavors);
Paul E. McKenney27f4d282011-02-07 12:47:15 -0800112
Paul E. McKenneya3dc2942015-04-20 11:40:50 -0700113/* Dump rcu_node combining tree at boot to verify correct setup. */
114static bool dump_tree;
115module_param(dump_tree, bool, 0444);
Paul E. McKenney7fa27002015-04-20 10:27:15 -0700116/* Control rcu_node-tree auto-balancing at boot time. */
117static bool rcu_fanout_exact;
118module_param(rcu_fanout_exact, bool, 0444);
Paul E. McKenney47d631a2015-04-21 09:12:13 -0700119/* Increase (but not decrease) the RCU_FANOUT_LEAF at boot time. */
120static int rcu_fanout_leaf = RCU_FANOUT_LEAF;
Paul E. McKenney7e5c2df2012-07-01 15:42:33 -0700121module_param(rcu_fanout_leaf, int, 0444);
Paul E. McKenneyf885b7f2012-04-23 15:52:53 -0700122int rcu_num_lvls __read_mostly = RCU_NUM_LVLS;
Alexander Gordeevcb007102015-06-03 08:18:30 +0200123/* Number of rcu_nodes at specified level. */
124static int num_rcu_lvl[] = NUM_RCU_LVL_INIT;
Paul E. McKenneyf885b7f2012-04-23 15:52:53 -0700125int rcu_num_nodes __read_mostly = NUM_RCU_NODES; /* Total # rcu_nodes in use. */
126
Paul E. McKenneyb0d30412011-07-10 15:57:35 -0700127/*
128 * The rcu_scheduler_active variable transitions from zero to one just
129 * before the first task is spawned. So when this variable is zero, RCU
130 * can assume that there is but one task, allowing RCU to (for example)
Cody P Schaferb44f6652013-01-04 12:59:40 -0500131 * optimize synchronize_sched() to a simple barrier(). When this variable
Paul E. McKenneyb0d30412011-07-10 15:57:35 -0700132 * is one, RCU must actually do all the hard work required to detect real
133 * grace periods. This variable is also used to suppress boot-time false
134 * positives from lockdep-RCU error checking.
135 */
Paul E. McKenneybbad9372010-04-02 16:17:17 -0700136int rcu_scheduler_active __read_mostly;
137EXPORT_SYMBOL_GPL(rcu_scheduler_active);
138
Paul E. McKenneyb0d30412011-07-10 15:57:35 -0700139/*
140 * The rcu_scheduler_fully_active variable transitions from zero to one
141 * during the early_initcall() processing, which is after the scheduler
142 * is capable of creating new tasks. So RCU processing (for example,
143 * creating tasks for RCU priority boosting) must be delayed until after
144 * rcu_scheduler_fully_active transitions from zero to one. We also
145 * currently delay invocation of any RCU callbacks until after this point.
146 *
147 * It might later prove better for people registering RCU callbacks during
148 * early boot to take responsibility for these callbacks, but one step at
149 * a time.
150 */
151static int rcu_scheduler_fully_active __read_mostly;
152
Paul E. McKenney0aa04b02015-01-23 21:52:37 -0800153static void rcu_init_new_rnp(struct rcu_node *rnp_leaf);
154static void rcu_cleanup_dead_rnp(struct rcu_node *rnp_leaf);
Thomas Gleixner5d01bbd2012-07-16 10:42:35 +0000155static void rcu_boost_kthread_setaffinity(struct rcu_node *rnp, int outgoingcpu);
Paul E. McKenneya46e0892011-06-15 15:47:09 -0700156static void invoke_rcu_core(void);
157static void invoke_rcu_callbacks(struct rcu_state *rsp, struct rcu_data *rdp);
Paul E. McKenney6587a232015-08-06 16:50:39 -0700158static void rcu_report_exp_rdp(struct rcu_state *rsp,
159 struct rcu_data *rdp, bool wake);
Paul E. McKenneya26ac242011-01-12 14:10:23 -0800160
Paul E. McKenneya94844b2014-12-12 07:37:48 -0800161/* rcuc/rcub kthread realtime priority */
Paul E. McKenney26730f52015-04-21 09:22:14 -0700162#ifdef CONFIG_RCU_KTHREAD_PRIO
Paul E. McKenneya94844b2014-12-12 07:37:48 -0800163static int kthread_prio = CONFIG_RCU_KTHREAD_PRIO;
Paul E. McKenney26730f52015-04-21 09:22:14 -0700164#else /* #ifdef CONFIG_RCU_KTHREAD_PRIO */
165static int kthread_prio = IS_ENABLED(CONFIG_RCU_BOOST) ? 1 : 0;
166#endif /* #else #ifdef CONFIG_RCU_KTHREAD_PRIO */
Paul E. McKenneya94844b2014-12-12 07:37:48 -0800167module_param(kthread_prio, int, 0644);
168
Paul E. McKenney8d7dc922015-04-14 19:33:59 -0700169/* Delay in jiffies for grace-period initialization delays, debug only. */
Paul E. McKenney0f41c0d2015-03-10 18:33:20 -0700170
171#ifdef CONFIG_RCU_TORTURE_TEST_SLOW_PREINIT
172static int gp_preinit_delay = CONFIG_RCU_TORTURE_TEST_SLOW_PREINIT_DELAY;
173module_param(gp_preinit_delay, int, 0644);
174#else /* #ifdef CONFIG_RCU_TORTURE_TEST_SLOW_PREINIT */
175static const int gp_preinit_delay;
176#endif /* #else #ifdef CONFIG_RCU_TORTURE_TEST_SLOW_PREINIT */
177
Paul E. McKenney8d7dc922015-04-14 19:33:59 -0700178#ifdef CONFIG_RCU_TORTURE_TEST_SLOW_INIT
179static int gp_init_delay = CONFIG_RCU_TORTURE_TEST_SLOW_INIT_DELAY;
Paul E. McKenney37745d22015-01-22 18:24:08 -0800180module_param(gp_init_delay, int, 0644);
Paul E. McKenney8d7dc922015-04-14 19:33:59 -0700181#else /* #ifdef CONFIG_RCU_TORTURE_TEST_SLOW_INIT */
182static const int gp_init_delay;
183#endif /* #else #ifdef CONFIG_RCU_TORTURE_TEST_SLOW_INIT */
Paul E. McKenneyeab128e2015-04-15 12:08:22 -0700184
Paul E. McKenney0f41c0d2015-03-10 18:33:20 -0700185#ifdef CONFIG_RCU_TORTURE_TEST_SLOW_CLEANUP
186static int gp_cleanup_delay = CONFIG_RCU_TORTURE_TEST_SLOW_CLEANUP_DELAY;
187module_param(gp_cleanup_delay, int, 0644);
188#else /* #ifdef CONFIG_RCU_TORTURE_TEST_SLOW_CLEANUP */
189static const int gp_cleanup_delay;
190#endif /* #else #ifdef CONFIG_RCU_TORTURE_TEST_SLOW_CLEANUP */
191
Paul E. McKenneyeab128e2015-04-15 12:08:22 -0700192/*
193 * Number of grace periods between delays, normalized by the duration of
194 * the delay. The longer the the delay, the more the grace periods between
195 * each delay. The reason for this normalization is that it means that,
196 * for non-zero delays, the overall slowdown of grace periods is constant
197 * regardless of the duration of the delay. This arrangement balances
198 * the need for long delays to increase some race probabilities with the
199 * need for fast grace periods to increase other race probabilities.
200 */
201#define PER_RCU_NODE_PERIOD 3 /* Number of grace periods between delays. */
Paul E. McKenney37745d22015-01-22 18:24:08 -0800202
Paul E. McKenneya26ac242011-01-12 14:10:23 -0800203/*
Paul E. McKenney4a298652011-04-03 21:33:51 -0700204 * Track the rcutorture test sequence number and the update version
205 * number within a given test. The rcutorture_testseq is incremented
206 * on every rcutorture module load and unload, so has an odd value
207 * when a test is running. The rcutorture_vernum is set to zero
208 * when rcutorture starts and is incremented on each rcutorture update.
209 * These variables enable correlating rcutorture output with the
210 * RCU tracing information.
211 */
212unsigned long rcutorture_testseq;
213unsigned long rcutorture_vernum;
214
215/*
Paul E. McKenney0aa04b02015-01-23 21:52:37 -0800216 * Compute the mask of online CPUs for the specified rcu_node structure.
217 * This will not be stable unless the rcu_node structure's ->lock is
218 * held, but the bit corresponding to the current CPU will be stable
219 * in most contexts.
220 */
221unsigned long rcu_rnp_online_cpus(struct rcu_node *rnp)
222{
Paul E. McKenney7d0ae802015-03-03 14:57:58 -0800223 return READ_ONCE(rnp->qsmaskinitnext);
Paul E. McKenney0aa04b02015-01-23 21:52:37 -0800224}
225
226/*
Paul E. McKenney7d0ae802015-03-03 14:57:58 -0800227 * Return true if an RCU grace period is in progress. The READ_ONCE()s
Paul E. McKenneyfc2219d42009-09-23 09:50:41 -0700228 * permit this function to be invoked without holding the root rcu_node
229 * structure's ->lock, but of course results can be subject to change.
230 */
231static int rcu_gp_in_progress(struct rcu_state *rsp)
232{
Paul E. McKenney7d0ae802015-03-03 14:57:58 -0800233 return READ_ONCE(rsp->completed) != READ_ONCE(rsp->gpnum);
Paul E. McKenneyfc2219d42009-09-23 09:50:41 -0700234}
235
236/*
Paul E. McKenneyd6714c22009-08-22 13:56:46 -0700237 * Note a quiescent state. Because we do not need to know
Ingo Molnarb1f77b02009-03-13 03:20:49 +0100238 * how many quiescent states passed, just if there was at least
Paul E. McKenneyd6714c22009-08-22 13:56:46 -0700239 * one since the start of the grace period, this just sets a flag.
Paul E. McKenneye4cc1f22011-06-27 00:17:43 -0700240 * The caller must have disabled preemption.
Ingo Molnarb1f77b02009-03-13 03:20:49 +0100241 */
Paul E. McKenney284a8c92014-08-14 16:38:46 -0700242void rcu_sched_qs(void)
Ingo Molnarb1f77b02009-03-13 03:20:49 +0100243{
Paul E. McKenneyfecbf6f2015-09-28 18:19:24 -0700244 if (!__this_cpu_read(rcu_sched_data.cpu_no_qs.s))
245 return;
246 trace_rcu_grace_period(TPS("rcu_sched"),
247 __this_cpu_read(rcu_sched_data.gpnum),
248 TPS("cpuqs"));
249 __this_cpu_write(rcu_sched_data.cpu_no_qs.b.norm, false);
250 if (!__this_cpu_read(rcu_sched_data.cpu_no_qs.b.exp))
251 return;
Paul E. McKenney46a5d162015-10-07 09:10:48 -0700252 __this_cpu_write(rcu_sched_data.cpu_no_qs.b.exp, false);
253 rcu_report_exp_rdp(&rcu_sched_state,
254 this_cpu_ptr(&rcu_sched_data), true);
Ingo Molnarb1f77b02009-03-13 03:20:49 +0100255}
256
Paul E. McKenney284a8c92014-08-14 16:38:46 -0700257void rcu_bh_qs(void)
Ingo Molnarb1f77b02009-03-13 03:20:49 +0100258{
Paul E. McKenney5b74c452015-08-06 15:16:57 -0700259 if (__this_cpu_read(rcu_bh_data.cpu_no_qs.s)) {
Paul E. McKenney284a8c92014-08-14 16:38:46 -0700260 trace_rcu_grace_period(TPS("rcu_bh"),
261 __this_cpu_read(rcu_bh_data.gpnum),
262 TPS("cpuqs"));
Paul E. McKenney5b74c452015-08-06 15:16:57 -0700263 __this_cpu_write(rcu_bh_data.cpu_no_qs.b.norm, false);
Paul E. McKenney284a8c92014-08-14 16:38:46 -0700264 }
Ingo Molnarb1f77b02009-03-13 03:20:49 +0100265}
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100266
Paul E. McKenney4a81e832014-06-20 16:49:01 -0700267static DEFINE_PER_CPU(int, rcu_sched_qs_mask);
268
269static DEFINE_PER_CPU(struct rcu_dynticks, rcu_dynticks) = {
270 .dynticks_nesting = DYNTICK_TASK_EXIT_IDLE,
271 .dynticks = ATOMIC_INIT(1),
272#ifdef CONFIG_NO_HZ_FULL_SYSIDLE
273 .dynticks_idle_nesting = DYNTICK_TASK_NEST_VALUE,
274 .dynticks_idle = ATOMIC_INIT(1),
275#endif /* #ifdef CONFIG_NO_HZ_FULL_SYSIDLE */
276};
277
Paul E. McKenney5cd37192014-12-13 20:32:04 -0800278DEFINE_PER_CPU_SHARED_ALIGNED(unsigned long, rcu_qs_ctr);
279EXPORT_PER_CPU_SYMBOL_GPL(rcu_qs_ctr);
280
Paul E. McKenney4a81e832014-06-20 16:49:01 -0700281/*
282 * Let the RCU core know that this CPU has gone through the scheduler,
283 * which is a quiescent state. This is called when the need for a
284 * quiescent state is urgent, so we burn an atomic operation and full
285 * memory barriers to let the RCU core know about it, regardless of what
286 * this CPU might (or might not) do in the near future.
287 *
288 * We inform the RCU core by emulating a zero-duration dyntick-idle
289 * period, which we in turn do by incrementing the ->dynticks counter
290 * by two.
Paul E. McKenney46a5d162015-10-07 09:10:48 -0700291 *
292 * The caller must have disabled interrupts.
Paul E. McKenney4a81e832014-06-20 16:49:01 -0700293 */
294static void rcu_momentary_dyntick_idle(void)
295{
Paul E. McKenney4a81e832014-06-20 16:49:01 -0700296 struct rcu_data *rdp;
297 struct rcu_dynticks *rdtp;
298 int resched_mask;
299 struct rcu_state *rsp;
300
Paul E. McKenney4a81e832014-06-20 16:49:01 -0700301 /*
302 * Yes, we can lose flag-setting operations. This is OK, because
303 * the flag will be set again after some delay.
304 */
305 resched_mask = raw_cpu_read(rcu_sched_qs_mask);
306 raw_cpu_write(rcu_sched_qs_mask, 0);
307
308 /* Find the flavor that needs a quiescent state. */
309 for_each_rcu_flavor(rsp) {
310 rdp = raw_cpu_ptr(rsp->rda);
311 if (!(resched_mask & rsp->flavor_mask))
312 continue;
313 smp_mb(); /* rcu_sched_qs_mask before cond_resched_completed. */
Paul E. McKenney7d0ae802015-03-03 14:57:58 -0800314 if (READ_ONCE(rdp->mynode->completed) !=
315 READ_ONCE(rdp->cond_resched_completed))
Paul E. McKenney4a81e832014-06-20 16:49:01 -0700316 continue;
317
318 /*
319 * Pretend to be momentarily idle for the quiescent state.
320 * This allows the grace-period kthread to record the
321 * quiescent state, with no need for this CPU to do anything
322 * further.
323 */
324 rdtp = this_cpu_ptr(&rcu_dynticks);
325 smp_mb__before_atomic(); /* Earlier stuff before QS. */
326 atomic_add(2, &rdtp->dynticks); /* QS. */
327 smp_mb__after_atomic(); /* Later stuff after QS. */
328 break;
329 }
Paul E. McKenney4a81e832014-06-20 16:49:01 -0700330}
331
Paul E. McKenney25502a62010-04-01 17:37:01 -0700332/*
333 * Note a context switch. This is a quiescent state for RCU-sched,
334 * and requires special handling for preemptible RCU.
Paul E. McKenney46a5d162015-10-07 09:10:48 -0700335 * The caller must have disabled interrupts.
Paul E. McKenney25502a62010-04-01 17:37:01 -0700336 */
Paul E. McKenney38200cf2014-10-21 12:50:04 -0700337void rcu_note_context_switch(void)
Paul E. McKenney25502a62010-04-01 17:37:01 -0700338{
Boqun Fengbb73c522015-07-30 16:55:38 -0700339 barrier(); /* Avoid RCU read-side critical sections leaking down. */
Steven Rostedt (Red Hat)f7f7bac2013-07-12 17:18:47 -0400340 trace_rcu_utilization(TPS("Start context switch"));
Paul E. McKenney284a8c92014-08-14 16:38:46 -0700341 rcu_sched_qs();
Paul E. McKenney38200cf2014-10-21 12:50:04 -0700342 rcu_preempt_note_context_switch();
Paul E. McKenney4a81e832014-06-20 16:49:01 -0700343 if (unlikely(raw_cpu_read(rcu_sched_qs_mask)))
344 rcu_momentary_dyntick_idle();
Steven Rostedt (Red Hat)f7f7bac2013-07-12 17:18:47 -0400345 trace_rcu_utilization(TPS("End context switch"));
Boqun Fengbb73c522015-07-30 16:55:38 -0700346 barrier(); /* Avoid RCU read-side critical sections leaking up. */
Paul E. McKenney25502a62010-04-01 17:37:01 -0700347}
Gleb Natapov29ce8312011-05-04 16:31:03 +0300348EXPORT_SYMBOL_GPL(rcu_note_context_switch);
Paul E. McKenney25502a62010-04-01 17:37:01 -0700349
Paul E. McKenney5cd37192014-12-13 20:32:04 -0800350/*
Paul E. McKenney1925d192015-01-18 17:45:05 -0800351 * Register a quiescent state for all RCU flavors. If there is an
Paul E. McKenney5cd37192014-12-13 20:32:04 -0800352 * emergency, invoke rcu_momentary_dyntick_idle() to do a heavy-weight
353 * dyntick-idle quiescent state visible to other CPUs (but only for those
Paul E. McKenney1925d192015-01-18 17:45:05 -0800354 * RCU flavors in desperate need of a quiescent state, which will normally
Paul E. McKenney5cd37192014-12-13 20:32:04 -0800355 * be none of them). Either way, do a lightweight quiescent state for
356 * all RCU flavors.
Boqun Fengbb73c522015-07-30 16:55:38 -0700357 *
358 * The barrier() calls are redundant in the common case when this is
359 * called externally, but just in case this is called from within this
360 * file.
361 *
Paul E. McKenney5cd37192014-12-13 20:32:04 -0800362 */
363void rcu_all_qs(void)
364{
Paul E. McKenney46a5d162015-10-07 09:10:48 -0700365 unsigned long flags;
366
Boqun Fengbb73c522015-07-30 16:55:38 -0700367 barrier(); /* Avoid RCU read-side critical sections leaking down. */
Paul E. McKenney46a5d162015-10-07 09:10:48 -0700368 if (unlikely(raw_cpu_read(rcu_sched_qs_mask))) {
369 local_irq_save(flags);
Paul E. McKenney5cd37192014-12-13 20:32:04 -0800370 rcu_momentary_dyntick_idle();
Paul E. McKenney46a5d162015-10-07 09:10:48 -0700371 local_irq_restore(flags);
372 }
Paul E. McKenney5cd37192014-12-13 20:32:04 -0800373 this_cpu_inc(rcu_qs_ctr);
Boqun Fengbb73c522015-07-30 16:55:38 -0700374 barrier(); /* Avoid RCU read-side critical sections leaking up. */
Paul E. McKenney5cd37192014-12-13 20:32:04 -0800375}
376EXPORT_SYMBOL_GPL(rcu_all_qs);
377
Eric Dumazet878d7432012-10-18 04:55:36 -0700378static long blimit = 10; /* Maximum callbacks per rcu_do_batch. */
379static long qhimark = 10000; /* If this many pending, ignore blimit. */
380static long qlowmark = 100; /* Once only this many pending, use blimit. */
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100381
Eric Dumazet878d7432012-10-18 04:55:36 -0700382module_param(blimit, long, 0444);
383module_param(qhimark, long, 0444);
384module_param(qlowmark, long, 0444);
Paul E. McKenney3d76c082009-09-28 07:46:32 -0700385
Paul E. McKenney026ad282013-04-03 22:14:11 -0700386static ulong jiffies_till_first_fqs = ULONG_MAX;
387static ulong jiffies_till_next_fqs = ULONG_MAX;
Paul E. McKenney8c7c4822016-01-03 20:29:57 -0800388static bool rcu_kick_kthreads;
Paul E. McKenneyd40011f2012-06-26 20:45:57 -0700389
390module_param(jiffies_till_first_fqs, ulong, 0644);
391module_param(jiffies_till_next_fqs, ulong, 0644);
Paul E. McKenney8c7c4822016-01-03 20:29:57 -0800392module_param(rcu_kick_kthreads, bool, 0644);
Paul E. McKenneyd40011f2012-06-26 20:45:57 -0700393
Paul E. McKenney4a81e832014-06-20 16:49:01 -0700394/*
395 * How long the grace period must be before we start recruiting
396 * quiescent-state help from rcu_note_context_switch().
397 */
398static ulong jiffies_till_sched_qs = HZ / 20;
399module_param(jiffies_till_sched_qs, ulong, 0644);
400
Paul E. McKenney48a76392014-03-11 13:02:16 -0700401static bool rcu_start_gp_advanced(struct rcu_state *rsp, struct rcu_node *rnp,
Paul E. McKenney910ee452012-12-31 02:24:21 -0800402 struct rcu_data *rdp);
Paul E. McKenney217af2a2013-06-21 15:39:06 -0700403static void force_qs_rnp(struct rcu_state *rsp,
404 int (*f)(struct rcu_data *rsp, bool *isidle,
405 unsigned long *maxj),
406 bool *isidle, unsigned long *maxj);
Paul E. McKenney4cdfc1752012-06-22 17:06:26 -0700407static void force_quiescent_state(struct rcu_state *rsp);
Paul E. McKenneye3950ec2014-10-21 08:03:57 -0700408static int rcu_pending(void);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100409
410/*
Paul E. McKenney917963d2014-11-21 17:10:16 -0800411 * Return the number of RCU batches started thus far for debug & stats.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100412 */
Paul E. McKenney917963d2014-11-21 17:10:16 -0800413unsigned long rcu_batches_started(void)
414{
415 return rcu_state_p->gpnum;
416}
417EXPORT_SYMBOL_GPL(rcu_batches_started);
418
419/*
420 * Return the number of RCU-sched batches started thus far for debug & stats.
421 */
422unsigned long rcu_batches_started_sched(void)
423{
424 return rcu_sched_state.gpnum;
425}
426EXPORT_SYMBOL_GPL(rcu_batches_started_sched);
427
428/*
429 * Return the number of RCU BH batches started thus far for debug & stats.
430 */
431unsigned long rcu_batches_started_bh(void)
432{
433 return rcu_bh_state.gpnum;
434}
435EXPORT_SYMBOL_GPL(rcu_batches_started_bh);
436
437/*
438 * Return the number of RCU batches completed thus far for debug & stats.
439 */
440unsigned long rcu_batches_completed(void)
441{
442 return rcu_state_p->completed;
443}
444EXPORT_SYMBOL_GPL(rcu_batches_completed);
445
446/*
447 * Return the number of RCU-sched batches completed thus far for debug & stats.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100448 */
Paul E. McKenney9733e4f2014-11-21 12:49:13 -0800449unsigned long rcu_batches_completed_sched(void)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100450{
Paul E. McKenneyd6714c22009-08-22 13:56:46 -0700451 return rcu_sched_state.completed;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100452}
Paul E. McKenneyd6714c22009-08-22 13:56:46 -0700453EXPORT_SYMBOL_GPL(rcu_batches_completed_sched);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100454
455/*
Paul E. McKenney917963d2014-11-21 17:10:16 -0800456 * Return the number of RCU BH batches completed thus far for debug & stats.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100457 */
Paul E. McKenney9733e4f2014-11-21 12:49:13 -0800458unsigned long rcu_batches_completed_bh(void)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100459{
460 return rcu_bh_state.completed;
461}
462EXPORT_SYMBOL_GPL(rcu_batches_completed_bh);
463
464/*
Andreea-Cristina Bernata381d752014-03-19 11:18:31 +0200465 * Force a quiescent state.
466 */
467void rcu_force_quiescent_state(void)
468{
Uma Sharmae5341652014-03-23 22:32:09 -0700469 force_quiescent_state(rcu_state_p);
Andreea-Cristina Bernata381d752014-03-19 11:18:31 +0200470}
471EXPORT_SYMBOL_GPL(rcu_force_quiescent_state);
472
473/*
Paul E. McKenneybf66f182010-01-04 15:09:10 -0800474 * Force a quiescent state for RCU BH.
475 */
476void rcu_bh_force_quiescent_state(void)
477{
Paul E. McKenney4cdfc1752012-06-22 17:06:26 -0700478 force_quiescent_state(&rcu_bh_state);
Paul E. McKenneybf66f182010-01-04 15:09:10 -0800479}
480EXPORT_SYMBOL_GPL(rcu_bh_force_quiescent_state);
481
482/*
Paul E. McKenneye7580f32015-02-24 14:23:39 -0800483 * Force a quiescent state for RCU-sched.
484 */
485void rcu_sched_force_quiescent_state(void)
486{
487 force_quiescent_state(&rcu_sched_state);
488}
489EXPORT_SYMBOL_GPL(rcu_sched_force_quiescent_state);
490
491/*
Paul E. McKenneyafea2272014-03-12 07:10:41 -0700492 * Show the state of the grace-period kthreads.
493 */
494void show_rcu_gp_kthreads(void)
495{
496 struct rcu_state *rsp;
497
498 for_each_rcu_flavor(rsp) {
499 pr_info("%s: wait state: %d ->state: %#lx\n",
500 rsp->name, rsp->gp_state, rsp->gp_kthread->state);
501 /* sched_show_task(rsp->gp_kthread); */
502 }
503}
504EXPORT_SYMBOL_GPL(show_rcu_gp_kthreads);
505
506/*
Paul E. McKenney4a298652011-04-03 21:33:51 -0700507 * Record the number of times rcutorture tests have been initiated and
508 * terminated. This information allows the debugfs tracing stats to be
509 * correlated to the rcutorture messages, even when the rcutorture module
510 * is being repeatedly loaded and unloaded. In other words, we cannot
511 * store this state in rcutorture itself.
512 */
513void rcutorture_record_test_transition(void)
514{
515 rcutorture_testseq++;
516 rcutorture_vernum = 0;
517}
518EXPORT_SYMBOL_GPL(rcutorture_record_test_transition);
519
520/*
Paul E. McKenneyad0dc7f2014-02-19 10:51:42 -0800521 * Send along grace-period-related data for rcutorture diagnostics.
522 */
523void rcutorture_get_gp_data(enum rcutorture_type test_type, int *flags,
524 unsigned long *gpnum, unsigned long *completed)
525{
526 struct rcu_state *rsp = NULL;
527
528 switch (test_type) {
529 case RCU_FLAVOR:
Uma Sharmae5341652014-03-23 22:32:09 -0700530 rsp = rcu_state_p;
Paul E. McKenneyad0dc7f2014-02-19 10:51:42 -0800531 break;
532 case RCU_BH_FLAVOR:
533 rsp = &rcu_bh_state;
534 break;
535 case RCU_SCHED_FLAVOR:
536 rsp = &rcu_sched_state;
537 break;
538 default:
539 break;
540 }
541 if (rsp != NULL) {
Paul E. McKenney7d0ae802015-03-03 14:57:58 -0800542 *flags = READ_ONCE(rsp->gp_flags);
543 *gpnum = READ_ONCE(rsp->gpnum);
544 *completed = READ_ONCE(rsp->completed);
Paul E. McKenneyad0dc7f2014-02-19 10:51:42 -0800545 return;
546 }
547 *flags = 0;
548 *gpnum = 0;
549 *completed = 0;
550}
551EXPORT_SYMBOL_GPL(rcutorture_get_gp_data);
552
553/*
Paul E. McKenney4a298652011-04-03 21:33:51 -0700554 * Record the number of writer passes through the current rcutorture test.
555 * This is also used to correlate debugfs tracing stats with the rcutorture
556 * messages.
557 */
558void rcutorture_record_progress(unsigned long vernum)
559{
560 rcutorture_vernum++;
561}
562EXPORT_SYMBOL_GPL(rcutorture_record_progress);
563
564/*
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100565 * Does the CPU have callbacks ready to be invoked?
566 */
567static int
568cpu_has_callbacks_ready_to_invoke(struct rcu_data *rdp)
569{
Paul E. McKenney3fbfbf72012-08-19 21:35:53 -0700570 return &rdp->nxtlist != rdp->nxttail[RCU_DONE_TAIL] &&
571 rdp->nxttail[RCU_DONE_TAIL] != NULL;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100572}
573
574/*
Paul E. McKenney365187f2014-03-10 10:55:52 -0700575 * Return the root node of the specified rcu_state structure.
576 */
577static struct rcu_node *rcu_get_root(struct rcu_state *rsp)
578{
579 return &rsp->node[0];
580}
581
582/*
583 * Is there any need for future grace periods?
584 * Interrupts must be disabled. If the caller does not hold the root
585 * rnp_node structure's ->lock, the results are advisory only.
586 */
587static int rcu_future_needs_gp(struct rcu_state *rsp)
588{
589 struct rcu_node *rnp = rcu_get_root(rsp);
Paul E. McKenney7d0ae802015-03-03 14:57:58 -0800590 int idx = (READ_ONCE(rnp->completed) + 1) & 0x1;
Paul E. McKenney365187f2014-03-10 10:55:52 -0700591 int *fp = &rnp->need_future_gp[idx];
592
Paul E. McKenney7d0ae802015-03-03 14:57:58 -0800593 return READ_ONCE(*fp);
Paul E. McKenney365187f2014-03-10 10:55:52 -0700594}
595
596/*
Paul E. McKenneydc35c892012-12-03 13:52:00 -0800597 * Does the current CPU require a not-yet-started grace period?
598 * The caller must have disabled interrupts to prevent races with
599 * normal callback registry.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100600 */
Paul E. McKenneyd117c8a2015-10-31 00:01:18 -0700601static bool
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100602cpu_needs_another_gp(struct rcu_state *rsp, struct rcu_data *rdp)
603{
Paul E. McKenneydc35c892012-12-03 13:52:00 -0800604 int i;
Paul E. McKenney3fbfbf72012-08-19 21:35:53 -0700605
Paul E. McKenneydc35c892012-12-03 13:52:00 -0800606 if (rcu_gp_in_progress(rsp))
Paul E. McKenneyd117c8a2015-10-31 00:01:18 -0700607 return false; /* No, a grace period is already in progress. */
Paul E. McKenney365187f2014-03-10 10:55:52 -0700608 if (rcu_future_needs_gp(rsp))
Paul E. McKenneyd117c8a2015-10-31 00:01:18 -0700609 return true; /* Yes, a no-CBs CPU needs one. */
Paul E. McKenneydc35c892012-12-03 13:52:00 -0800610 if (!rdp->nxttail[RCU_NEXT_TAIL])
Paul E. McKenneyd117c8a2015-10-31 00:01:18 -0700611 return false; /* No, this is a no-CBs (or offline) CPU. */
Paul E. McKenneydc35c892012-12-03 13:52:00 -0800612 if (*rdp->nxttail[RCU_NEXT_READY_TAIL])
Paul E. McKenneyd117c8a2015-10-31 00:01:18 -0700613 return true; /* Yes, CPU has newly registered callbacks. */
Paul E. McKenneydc35c892012-12-03 13:52:00 -0800614 for (i = RCU_WAIT_TAIL; i < RCU_NEXT_TAIL; i++)
615 if (rdp->nxttail[i - 1] != rdp->nxttail[i] &&
Paul E. McKenney7d0ae802015-03-03 14:57:58 -0800616 ULONG_CMP_LT(READ_ONCE(rsp->completed),
Paul E. McKenneydc35c892012-12-03 13:52:00 -0800617 rdp->nxtcompleted[i]))
Paul E. McKenneyd117c8a2015-10-31 00:01:18 -0700618 return true; /* Yes, CBs for future grace period. */
619 return false; /* No grace period needed. */
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100620}
621
622/*
Frederic Weisbeckeradf50912012-06-28 11:20:21 -0700623 * rcu_eqs_enter_common - current CPU is moving towards extended quiescent state
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100624 *
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700625 * If the new value of the ->dynticks_nesting counter now is zero,
626 * we really have entered idle, and must do the appropriate accounting.
627 * The caller must have disabled interrupts.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100628 */
Christoph Lameter28ced792014-09-02 14:13:44 -0700629static void rcu_eqs_enter_common(long long oldval, bool user)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100630{
Paul E. McKenney96d3fd02013-10-04 14:33:34 -0700631 struct rcu_state *rsp;
632 struct rcu_data *rdp;
Christoph Lameter28ced792014-09-02 14:13:44 -0700633 struct rcu_dynticks *rdtp = this_cpu_ptr(&rcu_dynticks);
Paul E. McKenney96d3fd02013-10-04 14:33:34 -0700634
Steven Rostedt (Red Hat)f7f7bac2013-07-12 17:18:47 -0400635 trace_rcu_dyntick(TPS("Start"), oldval, rdtp->dynticks_nesting);
Paul E. McKenney1ce46ee2015-05-05 23:04:22 -0700636 if (IS_ENABLED(CONFIG_RCU_EQS_DEBUG) &&
637 !user && !is_idle_task(current)) {
Paul E. McKenney289828e2013-08-31 19:23:29 -0700638 struct task_struct *idle __maybe_unused =
639 idle_task(smp_processor_id());
Paul E. McKenney0989cb42011-11-01 08:57:21 -0700640
Steven Rostedt (Red Hat)f7f7bac2013-07-12 17:18:47 -0400641 trace_rcu_dyntick(TPS("Error on entry: not idle task"), oldval, 0);
Paul E. McKenney274529b2016-03-21 19:46:04 -0700642 rcu_ftrace_dump(DUMP_ORIG);
Paul E. McKenney0989cb42011-11-01 08:57:21 -0700643 WARN_ONCE(1, "Current pid: %d comm: %s / Idle pid: %d comm: %s",
644 current->pid, current->comm,
645 idle->pid, idle->comm); /* must be idle task! */
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700646 }
Paul E. McKenney96d3fd02013-10-04 14:33:34 -0700647 for_each_rcu_flavor(rsp) {
648 rdp = this_cpu_ptr(rsp->rda);
649 do_nocb_deferred_wakeup(rdp);
650 }
Paul E. McKenney198bbf82014-10-22 15:03:43 -0700651 rcu_prepare_for_idle();
Paul E. McKenney23b5c8f2010-09-07 10:38:22 -0700652 /* CPUs seeing atomic_inc() must see prior RCU read-side crit sects */
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100653 smp_mb__before_atomic(); /* See above. */
Paul E. McKenney23b5c8f2010-09-07 10:38:22 -0700654 atomic_inc(&rdtp->dynticks);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100655 smp_mb__after_atomic(); /* Force ordering with next sojourn. */
Paul E. McKenney1ce46ee2015-05-05 23:04:22 -0700656 WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) &&
657 atomic_read(&rdtp->dynticks) & 0x1);
Paul E. McKenney176f8f72014-08-04 17:43:50 -0700658 rcu_dynticks_task_enter();
Paul E. McKenneyc44e2cd2012-01-12 13:08:18 -0800659
660 /*
Frederic Weisbeckeradf50912012-06-28 11:20:21 -0700661 * It is illegal to enter an extended quiescent state while
Paul E. McKenneyc44e2cd2012-01-12 13:08:18 -0800662 * in an RCU read-side critical section.
663 */
Paul E. McKenneyf78f5b92015-06-18 15:50:02 -0700664 RCU_LOCKDEP_WARN(lock_is_held(&rcu_lock_map),
665 "Illegal idle entry in RCU read-side critical section.");
666 RCU_LOCKDEP_WARN(lock_is_held(&rcu_bh_lock_map),
667 "Illegal idle entry in RCU-bh read-side critical section.");
668 RCU_LOCKDEP_WARN(lock_is_held(&rcu_sched_lock_map),
669 "Illegal idle entry in RCU-sched read-side critical section.");
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100670}
671
Frederic Weisbeckeradf50912012-06-28 11:20:21 -0700672/*
673 * Enter an RCU extended quiescent state, which can be either the
674 * idle loop or adaptive-tickless usermode execution.
675 */
676static void rcu_eqs_enter(bool user)
677{
Frederic Weisbeckeradf50912012-06-28 11:20:21 -0700678 long long oldval;
679 struct rcu_dynticks *rdtp;
680
Christoph Lameterc9d4b0a2013-08-31 13:34:10 -0700681 rdtp = this_cpu_ptr(&rcu_dynticks);
Frederic Weisbeckeradf50912012-06-28 11:20:21 -0700682 oldval = rdtp->dynticks_nesting;
Paul E. McKenney1ce46ee2015-05-05 23:04:22 -0700683 WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) &&
684 (oldval & DYNTICK_TASK_NEST_MASK) == 0);
Paul E. McKenney3a5924052013-10-04 18:48:55 -0700685 if ((oldval & DYNTICK_TASK_NEST_MASK) == DYNTICK_TASK_NEST_VALUE) {
Frederic Weisbeckeradf50912012-06-28 11:20:21 -0700686 rdtp->dynticks_nesting = 0;
Christoph Lameter28ced792014-09-02 14:13:44 -0700687 rcu_eqs_enter_common(oldval, user);
Paul E. McKenney3a5924052013-10-04 18:48:55 -0700688 } else {
Frederic Weisbeckeradf50912012-06-28 11:20:21 -0700689 rdtp->dynticks_nesting -= DYNTICK_TASK_NEST_VALUE;
Paul E. McKenney3a5924052013-10-04 18:48:55 -0700690 }
Frederic Weisbeckeradf50912012-06-28 11:20:21 -0700691}
692
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700693/**
694 * rcu_idle_enter - inform RCU that current CPU is entering idle
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100695 *
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700696 * Enter idle mode, in other words, -leave- the mode in which RCU
697 * read-side critical sections can occur. (Though RCU read-side
698 * critical sections can occur in irq handlers in idle, a possibility
699 * handled by irq_enter() and irq_exit().)
700 *
701 * We crowbar the ->dynticks_nesting field to zero to allow for
702 * the possibility of usermode upcalls having messed up our count
703 * of interrupt nesting level during the prior busy period.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100704 */
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700705void rcu_idle_enter(void)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100706{
Frederic Weisbeckerc5d900b2012-07-11 20:26:31 +0200707 unsigned long flags;
708
709 local_irq_save(flags);
Paul E. McKenneycb349ca2012-09-04 17:35:31 -0700710 rcu_eqs_enter(false);
Christoph Lameter28ced792014-09-02 14:13:44 -0700711 rcu_sysidle_enter(0);
Frederic Weisbeckerc5d900b2012-07-11 20:26:31 +0200712 local_irq_restore(flags);
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700713}
Paul E. McKenney8a2ecf42012-02-02 15:42:04 -0800714EXPORT_SYMBOL_GPL(rcu_idle_enter);
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700715
Paul E. McKenneyd1ec4c32015-05-13 10:41:58 -0700716#ifdef CONFIG_NO_HZ_FULL
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700717/**
Frederic Weisbeckeradf50912012-06-28 11:20:21 -0700718 * rcu_user_enter - inform RCU that we are resuming userspace.
719 *
720 * Enter RCU idle mode right before resuming userspace. No use of RCU
721 * is permitted between this call and rcu_user_exit(). This way the
722 * CPU doesn't need to maintain the tick for RCU maintenance purposes
723 * when the CPU runs in userspace.
724 */
725void rcu_user_enter(void)
726{
Frederic Weisbecker91d1aa432012-11-27 19:33:25 +0100727 rcu_eqs_enter(1);
Frederic Weisbeckeradf50912012-06-28 11:20:21 -0700728}
Paul E. McKenneyd1ec4c32015-05-13 10:41:58 -0700729#endif /* CONFIG_NO_HZ_FULL */
Frederic Weisbecker19dd15912012-06-04 16:42:35 -0700730
731/**
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700732 * rcu_irq_exit - inform RCU that current CPU is exiting irq towards idle
733 *
734 * Exit from an interrupt handler, which might possibly result in entering
735 * idle mode, in other words, leaving the mode in which read-side critical
Paul E. McKenney7c9906c2015-10-31 00:59:01 -0700736 * sections can occur. The caller must have disabled interrupts.
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700737 *
738 * This code assumes that the idle loop never does anything that might
739 * result in unbalanced calls to irq_enter() and irq_exit(). If your
740 * architecture violates this assumption, RCU will give you what you
741 * deserve, good and hard. But very infrequently and irreproducibly.
742 *
743 * Use things like work queues to work around this limitation.
744 *
745 * You have been warned.
746 */
747void rcu_irq_exit(void)
748{
Paul E. McKenney4145fa72011-10-31 15:01:54 -0700749 long long oldval;
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700750 struct rcu_dynticks *rdtp;
751
Paul E. McKenney7c9906c2015-10-31 00:59:01 -0700752 RCU_LOCKDEP_WARN(!irqs_disabled(), "rcu_irq_exit() invoked with irqs enabled!!!");
Christoph Lameterc9d4b0a2013-08-31 13:34:10 -0700753 rdtp = this_cpu_ptr(&rcu_dynticks);
Paul E. McKenney4145fa72011-10-31 15:01:54 -0700754 oldval = rdtp->dynticks_nesting;
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700755 rdtp->dynticks_nesting--;
Paul E. McKenney1ce46ee2015-05-05 23:04:22 -0700756 WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) &&
757 rdtp->dynticks_nesting < 0);
Frederic Weisbeckerb6fc6022011-11-28 16:18:56 -0800758 if (rdtp->dynticks_nesting)
Steven Rostedt (Red Hat)f7f7bac2013-07-12 17:18:47 -0400759 trace_rcu_dyntick(TPS("--="), oldval, rdtp->dynticks_nesting);
Frederic Weisbeckerb6fc6022011-11-28 16:18:56 -0800760 else
Christoph Lameter28ced792014-09-02 14:13:44 -0700761 rcu_eqs_enter_common(oldval, true);
762 rcu_sysidle_enter(1);
Paul E. McKenney7c9906c2015-10-31 00:59:01 -0700763}
764
765/*
766 * Wrapper for rcu_irq_exit() where interrupts are enabled.
767 */
768void rcu_irq_exit_irqson(void)
769{
770 unsigned long flags;
771
772 local_irq_save(flags);
773 rcu_irq_exit();
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700774 local_irq_restore(flags);
775}
776
777/*
Frederic Weisbeckeradf50912012-06-28 11:20:21 -0700778 * rcu_eqs_exit_common - current CPU moving away from extended quiescent state
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700779 *
780 * If the new value of the ->dynticks_nesting counter was previously zero,
781 * we really have exited idle, and must do the appropriate accounting.
782 * The caller must have disabled interrupts.
783 */
Christoph Lameter28ced792014-09-02 14:13:44 -0700784static void rcu_eqs_exit_common(long long oldval, int user)
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700785{
Christoph Lameter28ced792014-09-02 14:13:44 -0700786 struct rcu_dynticks *rdtp = this_cpu_ptr(&rcu_dynticks);
787
Paul E. McKenney176f8f72014-08-04 17:43:50 -0700788 rcu_dynticks_task_exit();
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100789 smp_mb__before_atomic(); /* Force ordering w/previous sojourn. */
Paul E. McKenney23b5c8f2010-09-07 10:38:22 -0700790 atomic_inc(&rdtp->dynticks);
791 /* CPUs seeing atomic_inc() must see later RCU read-side crit sects */
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100792 smp_mb__after_atomic(); /* See above. */
Paul E. McKenney1ce46ee2015-05-05 23:04:22 -0700793 WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) &&
794 !(atomic_read(&rdtp->dynticks) & 0x1));
Paul E. McKenney8fa78452014-10-22 15:07:37 -0700795 rcu_cleanup_after_idle();
Steven Rostedt (Red Hat)f7f7bac2013-07-12 17:18:47 -0400796 trace_rcu_dyntick(TPS("End"), oldval, rdtp->dynticks_nesting);
Paul E. McKenney1ce46ee2015-05-05 23:04:22 -0700797 if (IS_ENABLED(CONFIG_RCU_EQS_DEBUG) &&
798 !user && !is_idle_task(current)) {
Paul E. McKenney289828e2013-08-31 19:23:29 -0700799 struct task_struct *idle __maybe_unused =
800 idle_task(smp_processor_id());
Paul E. McKenney0989cb42011-11-01 08:57:21 -0700801
Steven Rostedt (Red Hat)f7f7bac2013-07-12 17:18:47 -0400802 trace_rcu_dyntick(TPS("Error on exit: not idle task"),
Paul E. McKenney4145fa72011-10-31 15:01:54 -0700803 oldval, rdtp->dynticks_nesting);
Paul E. McKenney274529b2016-03-21 19:46:04 -0700804 rcu_ftrace_dump(DUMP_ORIG);
Paul E. McKenney0989cb42011-11-01 08:57:21 -0700805 WARN_ONCE(1, "Current pid: %d comm: %s / Idle pid: %d comm: %s",
806 current->pid, current->comm,
807 idle->pid, idle->comm); /* must be idle task! */
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700808 }
809}
810
Frederic Weisbeckeradf50912012-06-28 11:20:21 -0700811/*
812 * Exit an RCU extended quiescent state, which can be either the
813 * idle loop or adaptive-tickless usermode execution.
814 */
815static void rcu_eqs_exit(bool user)
816{
Frederic Weisbeckeradf50912012-06-28 11:20:21 -0700817 struct rcu_dynticks *rdtp;
818 long long oldval;
819
Christoph Lameterc9d4b0a2013-08-31 13:34:10 -0700820 rdtp = this_cpu_ptr(&rcu_dynticks);
Frederic Weisbeckeradf50912012-06-28 11:20:21 -0700821 oldval = rdtp->dynticks_nesting;
Paul E. McKenney1ce46ee2015-05-05 23:04:22 -0700822 WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) && oldval < 0);
Paul E. McKenney3a5924052013-10-04 18:48:55 -0700823 if (oldval & DYNTICK_TASK_NEST_MASK) {
Frederic Weisbeckeradf50912012-06-28 11:20:21 -0700824 rdtp->dynticks_nesting += DYNTICK_TASK_NEST_VALUE;
Paul E. McKenney3a5924052013-10-04 18:48:55 -0700825 } else {
Frederic Weisbeckeradf50912012-06-28 11:20:21 -0700826 rdtp->dynticks_nesting = DYNTICK_TASK_EXIT_IDLE;
Christoph Lameter28ced792014-09-02 14:13:44 -0700827 rcu_eqs_exit_common(oldval, user);
Paul E. McKenney3a5924052013-10-04 18:48:55 -0700828 }
Frederic Weisbeckeradf50912012-06-28 11:20:21 -0700829}
830
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700831/**
832 * rcu_idle_exit - inform RCU that current CPU is leaving idle
833 *
834 * Exit idle mode, in other words, -enter- the mode in which RCU
835 * read-side critical sections can occur.
836 *
Paul E. McKenney29e37d82011-11-17 16:55:56 -0800837 * We crowbar the ->dynticks_nesting field to DYNTICK_TASK_NEST to
Paul E. McKenney4145fa72011-10-31 15:01:54 -0700838 * allow for the possibility of usermode upcalls messing up our count
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700839 * of interrupt nesting level during the busy period that is just
840 * now starting.
841 */
842void rcu_idle_exit(void)
843{
Frederic Weisbeckerc5d900b2012-07-11 20:26:31 +0200844 unsigned long flags;
845
846 local_irq_save(flags);
Paul E. McKenneycb349ca2012-09-04 17:35:31 -0700847 rcu_eqs_exit(false);
Christoph Lameter28ced792014-09-02 14:13:44 -0700848 rcu_sysidle_exit(0);
Frederic Weisbeckerc5d900b2012-07-11 20:26:31 +0200849 local_irq_restore(flags);
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700850}
Paul E. McKenney8a2ecf42012-02-02 15:42:04 -0800851EXPORT_SYMBOL_GPL(rcu_idle_exit);
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700852
Paul E. McKenneyd1ec4c32015-05-13 10:41:58 -0700853#ifdef CONFIG_NO_HZ_FULL
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700854/**
Frederic Weisbeckeradf50912012-06-28 11:20:21 -0700855 * rcu_user_exit - inform RCU that we are exiting userspace.
856 *
857 * Exit RCU idle mode while entering the kernel because it can
858 * run a RCU read side critical section anytime.
859 */
860void rcu_user_exit(void)
861{
Frederic Weisbecker91d1aa432012-11-27 19:33:25 +0100862 rcu_eqs_exit(1);
Frederic Weisbeckeradf50912012-06-28 11:20:21 -0700863}
Paul E. McKenneyd1ec4c32015-05-13 10:41:58 -0700864#endif /* CONFIG_NO_HZ_FULL */
Frederic Weisbecker19dd15912012-06-04 16:42:35 -0700865
866/**
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700867 * rcu_irq_enter - inform RCU that current CPU is entering irq away from idle
868 *
869 * Enter an interrupt handler, which might possibly result in exiting
870 * idle mode, in other words, entering the mode in which read-side critical
Paul E. McKenney7c9906c2015-10-31 00:59:01 -0700871 * sections can occur. The caller must have disabled interrupts.
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700872 *
873 * Note that the Linux kernel is fully capable of entering an interrupt
874 * handler that it never exits, for example when doing upcalls to
875 * user mode! This code assumes that the idle loop never does upcalls to
876 * user mode. If your architecture does do upcalls from the idle loop (or
877 * does anything else that results in unbalanced calls to the irq_enter()
878 * and irq_exit() functions), RCU will give you what you deserve, good
879 * and hard. But very infrequently and irreproducibly.
880 *
881 * Use things like work queues to work around this limitation.
882 *
883 * You have been warned.
884 */
885void rcu_irq_enter(void)
886{
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700887 struct rcu_dynticks *rdtp;
888 long long oldval;
889
Paul E. McKenney7c9906c2015-10-31 00:59:01 -0700890 RCU_LOCKDEP_WARN(!irqs_disabled(), "rcu_irq_enter() invoked with irqs enabled!!!");
Christoph Lameterc9d4b0a2013-08-31 13:34:10 -0700891 rdtp = this_cpu_ptr(&rcu_dynticks);
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700892 oldval = rdtp->dynticks_nesting;
893 rdtp->dynticks_nesting++;
Paul E. McKenney1ce46ee2015-05-05 23:04:22 -0700894 WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) &&
895 rdtp->dynticks_nesting == 0);
Frederic Weisbeckerb6fc6022011-11-28 16:18:56 -0800896 if (oldval)
Steven Rostedt (Red Hat)f7f7bac2013-07-12 17:18:47 -0400897 trace_rcu_dyntick(TPS("++="), oldval, rdtp->dynticks_nesting);
Frederic Weisbeckerb6fc6022011-11-28 16:18:56 -0800898 else
Christoph Lameter28ced792014-09-02 14:13:44 -0700899 rcu_eqs_exit_common(oldval, true);
900 rcu_sysidle_exit(1);
Paul E. McKenney7c9906c2015-10-31 00:59:01 -0700901}
902
903/*
904 * Wrapper for rcu_irq_enter() where interrupts are enabled.
905 */
906void rcu_irq_enter_irqson(void)
907{
908 unsigned long flags;
909
910 local_irq_save(flags);
911 rcu_irq_enter();
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100912 local_irq_restore(flags);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100913}
914
915/**
916 * rcu_nmi_enter - inform RCU of entry to NMI context
917 *
Paul E. McKenney734d1682014-11-21 14:45:12 -0800918 * If the CPU was idle from RCU's viewpoint, update rdtp->dynticks and
919 * rdtp->dynticks_nmi_nesting to let the RCU grace-period handling know
920 * that the CPU is active. This implementation permits nested NMIs, as
921 * long as the nesting level does not overflow an int. (You will probably
922 * run out of stack space first.)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100923 */
924void rcu_nmi_enter(void)
925{
Christoph Lameterc9d4b0a2013-08-31 13:34:10 -0700926 struct rcu_dynticks *rdtp = this_cpu_ptr(&rcu_dynticks);
Paul E. McKenney734d1682014-11-21 14:45:12 -0800927 int incby = 2;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100928
Paul E. McKenney734d1682014-11-21 14:45:12 -0800929 /* Complain about underflow. */
930 WARN_ON_ONCE(rdtp->dynticks_nmi_nesting < 0);
931
932 /*
933 * If idle from RCU viewpoint, atomically increment ->dynticks
934 * to mark non-idle and increment ->dynticks_nmi_nesting by one.
935 * Otherwise, increment ->dynticks_nmi_nesting by two. This means
936 * if ->dynticks_nmi_nesting is equal to one, we are guaranteed
937 * to be in the outermost NMI handler that interrupted an RCU-idle
938 * period (observation due to Andy Lutomirski).
939 */
940 if (!(atomic_read(&rdtp->dynticks) & 0x1)) {
941 smp_mb__before_atomic(); /* Force delay from prior write. */
942 atomic_inc(&rdtp->dynticks);
943 /* atomic_inc() before later RCU read-side crit sects */
944 smp_mb__after_atomic(); /* See above. */
945 WARN_ON_ONCE(!(atomic_read(&rdtp->dynticks) & 0x1));
946 incby = 1;
947 }
948 rdtp->dynticks_nmi_nesting += incby;
949 barrier();
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100950}
951
952/**
953 * rcu_nmi_exit - inform RCU of exit from NMI context
954 *
Paul E. McKenney734d1682014-11-21 14:45:12 -0800955 * If we are returning from the outermost NMI handler that interrupted an
956 * RCU-idle period, update rdtp->dynticks and rdtp->dynticks_nmi_nesting
957 * to let the RCU grace-period handling know that the CPU is back to
958 * being RCU-idle.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100959 */
960void rcu_nmi_exit(void)
961{
Christoph Lameterc9d4b0a2013-08-31 13:34:10 -0700962 struct rcu_dynticks *rdtp = this_cpu_ptr(&rcu_dynticks);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100963
Paul E. McKenney734d1682014-11-21 14:45:12 -0800964 /*
965 * Check for ->dynticks_nmi_nesting underflow and bad ->dynticks.
966 * (We are exiting an NMI handler, so RCU better be paying attention
967 * to us!)
968 */
969 WARN_ON_ONCE(rdtp->dynticks_nmi_nesting <= 0);
970 WARN_ON_ONCE(!(atomic_read(&rdtp->dynticks) & 0x1));
971
972 /*
973 * If the nesting level is not 1, the CPU wasn't RCU-idle, so
974 * leave it in non-RCU-idle state.
975 */
976 if (rdtp->dynticks_nmi_nesting != 1) {
977 rdtp->dynticks_nmi_nesting -= 2;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100978 return;
Paul E. McKenney734d1682014-11-21 14:45:12 -0800979 }
980
981 /* This NMI interrupted an RCU-idle CPU, restore RCU-idleness. */
982 rdtp->dynticks_nmi_nesting = 0;
Paul E. McKenney23b5c8f2010-09-07 10:38:22 -0700983 /* CPUs seeing atomic_inc() must see prior RCU read-side crit sects */
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100984 smp_mb__before_atomic(); /* See above. */
Paul E. McKenney23b5c8f2010-09-07 10:38:22 -0700985 atomic_inc(&rdtp->dynticks);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100986 smp_mb__after_atomic(); /* Force delay to next write. */
Paul E. McKenney23b5c8f2010-09-07 10:38:22 -0700987 WARN_ON_ONCE(atomic_read(&rdtp->dynticks) & 0x1);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100988}
989
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100990/**
Paul E. McKenneycc6783f2013-09-06 17:39:49 -0700991 * __rcu_is_watching - are RCU read-side critical sections safe?
992 *
993 * Return true if RCU is watching the running CPU, which means that
994 * this CPU can safely enter RCU read-side critical sections. Unlike
Paul E. McKenney5c173eb2013-09-13 17:20:11 -0700995 * rcu_is_watching(), the caller of __rcu_is_watching() must have at
Paul E. McKenneycc6783f2013-09-06 17:39:49 -0700996 * least disabled preemption.
997 */
Linus Torvaldsb29c8302013-11-16 12:23:18 -0800998bool notrace __rcu_is_watching(void)
Paul E. McKenneycc6783f2013-09-06 17:39:49 -0700999{
Paul E. McKenney5c173eb2013-09-13 17:20:11 -07001000 return atomic_read(this_cpu_ptr(&rcu_dynticks.dynticks)) & 0x1;
Paul E. McKenneycc6783f2013-09-06 17:39:49 -07001001}
1002
Paul E. McKenney5c173eb2013-09-13 17:20:11 -07001003/**
1004 * rcu_is_watching - see if RCU thinks that the current CPU is idle
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001005 *
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -07001006 * If the current CPU is in its idle loop and is neither in an interrupt
Paul E. McKenney34240692011-10-03 11:38:52 -07001007 * or NMI handler, return true.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001008 */
Linus Torvaldsb29c8302013-11-16 12:23:18 -08001009bool notrace rcu_is_watching(void)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001010{
Pranith Kumarf534ed12014-07-08 18:26:11 -04001011 bool ret;
Paul E. McKenney34240692011-10-03 11:38:52 -07001012
Alexei Starovoitov46f00d12015-06-16 10:35:18 -07001013 preempt_disable_notrace();
Paul E. McKenney5c173eb2013-09-13 17:20:11 -07001014 ret = __rcu_is_watching();
Alexei Starovoitov46f00d12015-06-16 10:35:18 -07001015 preempt_enable_notrace();
Paul E. McKenney34240692011-10-03 11:38:52 -07001016 return ret;
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -07001017}
Paul E. McKenney5c173eb2013-09-13 17:20:11 -07001018EXPORT_SYMBOL_GPL(rcu_is_watching);
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -07001019
Paul E. McKenney62fde6e2012-05-22 22:10:24 -07001020#if defined(CONFIG_PROVE_RCU) && defined(CONFIG_HOTPLUG_CPU)
Paul E. McKenneyc0d6d012012-01-23 12:41:26 -08001021
1022/*
1023 * Is the current CPU online? Disable preemption to avoid false positives
1024 * that could otherwise happen due to the current CPU number being sampled,
1025 * this task being preempted, its old CPU being taken offline, resuming
1026 * on some other CPU, then determining that its old CPU is now offline.
1027 * It is OK to use RCU on an offline processor during initial boot, hence
Paul E. McKenney2036d942012-01-30 17:02:47 -08001028 * the check for rcu_scheduler_fully_active. Note also that it is OK
1029 * for a CPU coming online to use RCU for one jiffy prior to marking itself
1030 * online in the cpu_online_mask. Similarly, it is OK for a CPU going
1031 * offline to continue to use RCU for one jiffy after marking itself
1032 * offline in the cpu_online_mask. This leniency is necessary given the
1033 * non-atomic nature of the online and offline processing, for example,
1034 * the fact that a CPU enters the scheduler after completing the CPU_DYING
1035 * notifiers.
1036 *
1037 * This is also why RCU internally marks CPUs online during the
1038 * CPU_UP_PREPARE phase and offline during the CPU_DEAD phase.
Paul E. McKenneyc0d6d012012-01-23 12:41:26 -08001039 *
1040 * Disable checking if in an NMI handler because we cannot safely report
1041 * errors from NMI handlers anyway.
1042 */
1043bool rcu_lockdep_current_cpu_online(void)
1044{
Paul E. McKenney2036d942012-01-30 17:02:47 -08001045 struct rcu_data *rdp;
1046 struct rcu_node *rnp;
Paul E. McKenneyc0d6d012012-01-23 12:41:26 -08001047 bool ret;
1048
1049 if (in_nmi())
Fengguang Wuf6f7ee92013-10-10 11:08:33 -07001050 return true;
Paul E. McKenneyc0d6d012012-01-23 12:41:26 -08001051 preempt_disable();
Christoph Lameterc9d4b0a2013-08-31 13:34:10 -07001052 rdp = this_cpu_ptr(&rcu_sched_data);
Paul E. McKenney2036d942012-01-30 17:02:47 -08001053 rnp = rdp->mynode;
Paul E. McKenney0aa04b02015-01-23 21:52:37 -08001054 ret = (rdp->grpmask & rcu_rnp_online_cpus(rnp)) ||
Paul E. McKenneyc0d6d012012-01-23 12:41:26 -08001055 !rcu_scheduler_fully_active;
1056 preempt_enable();
1057 return ret;
1058}
1059EXPORT_SYMBOL_GPL(rcu_lockdep_current_cpu_online);
1060
Paul E. McKenney62fde6e2012-05-22 22:10:24 -07001061#endif /* #if defined(CONFIG_PROVE_RCU) && defined(CONFIG_HOTPLUG_CPU) */
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -07001062
1063/**
1064 * rcu_is_cpu_rrupt_from_idle - see if idle or immediately interrupted from idle
1065 *
1066 * If the current CPU is idle or running at a first-level (not nested)
1067 * interrupt from idle, return true. The caller must have at least
1068 * disabled preemption.
1069 */
Josh Triplett62e3cb12012-11-20 09:55:26 -08001070static int rcu_is_cpu_rrupt_from_idle(void)
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -07001071{
Christoph Lameterc9d4b0a2013-08-31 13:34:10 -07001072 return __this_cpu_read(rcu_dynticks.dynticks_nesting) <= 1;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001073}
1074
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001075/*
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001076 * Snapshot the specified CPU's dynticks counter so that we can later
1077 * credit them with an implicit quiescent state. Return 1 if this CPU
Paul E. McKenney1eba8f82009-09-23 09:50:42 -07001078 * is in dynticks idle mode, which is an extended quiescent state.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001079 */
Paul E. McKenney217af2a2013-06-21 15:39:06 -07001080static int dyntick_save_progress_counter(struct rcu_data *rdp,
1081 bool *isidle, unsigned long *maxj)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001082{
Paul E. McKenney23b5c8f2010-09-07 10:38:22 -07001083 rdp->dynticks_snap = atomic_add_return(0, &rdp->dynticks->dynticks);
Paul E. McKenney0edd1b12013-06-21 16:37:22 -07001084 rcu_sysidle_check_cpu(rdp, isidle, maxj);
Andreea-Cristina Bernat7941dbd2014-03-17 18:33:28 +02001085 if ((rdp->dynticks_snap & 0x1) == 0) {
1086 trace_rcu_fqs(rdp->rsp->name, rdp->gpnum, rdp->cpu, TPS("dti"));
Paul E. McKenney7d0ae802015-03-03 14:57:58 -08001087 if (ULONG_CMP_LT(READ_ONCE(rdp->gpnum) + ULONG_MAX / 4,
Paul E. McKenneye3663b12014-12-08 20:26:55 -08001088 rdp->mynode->gpnum))
Paul E. McKenney7d0ae802015-03-03 14:57:58 -08001089 WRITE_ONCE(rdp->gpwrap, true);
Paul E. McKenney23a9bac2015-12-13 08:57:10 -08001090 return 1;
Andreea-Cristina Bernat7941dbd2014-03-17 18:33:28 +02001091 }
Paul E. McKenney23a9bac2015-12-13 08:57:10 -08001092 return 0;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001093}
1094
1095/*
1096 * Return true if the specified CPU has passed through a quiescent
1097 * state by virtue of being in or having passed through an dynticks
1098 * idle state since the last call to dyntick_save_progress_counter()
Paul E. McKenneya82dcc72012-08-01 14:29:20 -07001099 * for this same CPU, or by virtue of having been offline.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001100 */
Paul E. McKenney217af2a2013-06-21 15:39:06 -07001101static int rcu_implicit_dynticks_qs(struct rcu_data *rdp,
1102 bool *isidle, unsigned long *maxj)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001103{
Paul E. McKenney7eb4f452011-07-30 07:32:48 -07001104 unsigned int curr;
Paul E. McKenney4a81e832014-06-20 16:49:01 -07001105 int *rcrmp;
Paul E. McKenney7eb4f452011-07-30 07:32:48 -07001106 unsigned int snap;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001107
Paul E. McKenney7eb4f452011-07-30 07:32:48 -07001108 curr = (unsigned int)atomic_add_return(0, &rdp->dynticks->dynticks);
1109 snap = (unsigned int)rdp->dynticks_snap;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001110
1111 /*
1112 * If the CPU passed through or entered a dynticks idle phase with
1113 * no active irq/NMI handlers, then we can safely pretend that the CPU
1114 * already acknowledged the request to pass through a quiescent
1115 * state. Either way, that CPU cannot possibly be in an RCU
1116 * read-side critical section that started before the beginning
1117 * of the current RCU grace period.
1118 */
Paul E. McKenney7eb4f452011-07-30 07:32:48 -07001119 if ((curr & 0x1) == 0 || UINT_CMP_GE(curr, snap + 2)) {
Steven Rostedt (Red Hat)f7f7bac2013-07-12 17:18:47 -04001120 trace_rcu_fqs(rdp->rsp->name, rdp->gpnum, rdp->cpu, TPS("dti"));
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001121 rdp->dynticks_fqs++;
1122 return 1;
1123 }
1124
Paul E. McKenneya82dcc72012-08-01 14:29:20 -07001125 /*
1126 * Check for the CPU being offline, but only if the grace period
1127 * is old enough. We don't need to worry about the CPU changing
1128 * state: If we see it offline even once, it has been through a
1129 * quiescent state.
1130 *
1131 * The reason for insisting that the grace period be at least
1132 * one jiffy old is that CPUs that are not quite online and that
1133 * have just gone offline can still execute RCU read-side critical
1134 * sections.
1135 */
1136 if (ULONG_CMP_GE(rdp->rsp->gp_start + 2, jiffies))
1137 return 0; /* Grace period is not old enough. */
1138 barrier();
1139 if (cpu_is_offline(rdp->cpu)) {
Steven Rostedt (Red Hat)f7f7bac2013-07-12 17:18:47 -04001140 trace_rcu_fqs(rdp->rsp->name, rdp->gpnum, rdp->cpu, TPS("ofl"));
Paul E. McKenneya82dcc72012-08-01 14:29:20 -07001141 rdp->offline_fqs++;
1142 return 1;
1143 }
Paul E. McKenney65d798f2013-04-12 16:19:10 -07001144
1145 /*
Paul E. McKenney4a81e832014-06-20 16:49:01 -07001146 * A CPU running for an extended time within the kernel can
1147 * delay RCU grace periods. When the CPU is in NO_HZ_FULL mode,
1148 * even context-switching back and forth between a pair of
1149 * in-kernel CPU-bound tasks cannot advance grace periods.
1150 * So if the grace period is old enough, make the CPU pay attention.
1151 * Note that the unsynchronized assignments to the per-CPU
1152 * rcu_sched_qs_mask variable are safe. Yes, setting of
1153 * bits can be lost, but they will be set again on the next
1154 * force-quiescent-state pass. So lost bit sets do not result
1155 * in incorrect behavior, merely in a grace period lasting
1156 * a few jiffies longer than it might otherwise. Because
1157 * there are at most four threads involved, and because the
1158 * updates are only once every few jiffies, the probability of
1159 * lossage (and thus of slight grace-period extension) is
1160 * quite low.
1161 *
1162 * Note that if the jiffies_till_sched_qs boot/sysfs parameter
1163 * is set too high, we override with half of the RCU CPU stall
1164 * warning delay.
Paul E. McKenney65d798f2013-04-12 16:19:10 -07001165 */
Paul E. McKenney4a81e832014-06-20 16:49:01 -07001166 rcrmp = &per_cpu(rcu_sched_qs_mask, rdp->cpu);
1167 if (ULONG_CMP_GE(jiffies,
1168 rdp->rsp->gp_start + jiffies_till_sched_qs) ||
Paul E. McKenneycb1e78c2013-12-04 18:42:03 -08001169 ULONG_CMP_GE(jiffies, rdp->rsp->jiffies_resched)) {
Paul E. McKenney7d0ae802015-03-03 14:57:58 -08001170 if (!(READ_ONCE(*rcrmp) & rdp->rsp->flavor_mask)) {
1171 WRITE_ONCE(rdp->cond_resched_completed,
1172 READ_ONCE(rdp->mynode->completed));
Paul E. McKenney4a81e832014-06-20 16:49:01 -07001173 smp_mb(); /* ->cond_resched_completed before *rcrmp. */
Paul E. McKenney7d0ae802015-03-03 14:57:58 -08001174 WRITE_ONCE(*rcrmp,
1175 READ_ONCE(*rcrmp) + rdp->rsp->flavor_mask);
Paul E. McKenney4a81e832014-06-20 16:49:01 -07001176 }
Paul E. McKenney49149502015-12-11 13:48:43 -08001177 rdp->rsp->jiffies_resched += 5; /* Re-enable beating. */
Paul E. McKenney6193c762013-09-23 13:57:18 -07001178 }
1179
Paul E. McKenney49149502015-12-11 13:48:43 -08001180 /* And if it has been a really long time, kick the CPU as well. */
1181 if (ULONG_CMP_GE(jiffies,
1182 rdp->rsp->gp_start + 2 * jiffies_till_sched_qs) ||
1183 ULONG_CMP_GE(jiffies, rdp->rsp->gp_start + jiffies_till_sched_qs))
1184 resched_cpu(rdp->cpu); /* Force CPU into scheduler. */
1185
Paul E. McKenneya82dcc72012-08-01 14:29:20 -07001186 return 0;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001187}
1188
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001189static void record_gp_stall_check_time(struct rcu_state *rsp)
1190{
Paul E. McKenneycb1e78c2013-12-04 18:42:03 -08001191 unsigned long j = jiffies;
Paul E. McKenney6193c762013-09-23 13:57:18 -07001192 unsigned long j1;
Paul E. McKenney26cdfed2013-09-04 10:51:13 -07001193
1194 rsp->gp_start = j;
1195 smp_wmb(); /* Record start time before stall time. */
Paul E. McKenney6193c762013-09-23 13:57:18 -07001196 j1 = rcu_jiffies_till_stall_check();
Paul E. McKenney7d0ae802015-03-03 14:57:58 -08001197 WRITE_ONCE(rsp->jiffies_stall, j + j1);
Paul E. McKenney6193c762013-09-23 13:57:18 -07001198 rsp->jiffies_resched = j + j1 / 2;
Paul E. McKenney7d0ae802015-03-03 14:57:58 -08001199 rsp->n_force_qs_gpstart = READ_ONCE(rsp->n_force_qs);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001200}
1201
Paul E. McKenneyb637a322012-09-19 16:58:38 -07001202/*
Paul E. McKenney6b50e112015-11-17 14:39:26 -08001203 * Convert a ->gp_state value to a character string.
1204 */
1205static const char *gp_state_getname(short gs)
1206{
1207 if (gs < 0 || gs >= ARRAY_SIZE(gp_state_names))
1208 return "???";
1209 return gp_state_names[gs];
1210}
1211
1212/*
Paul E. McKenneyfb81a442014-12-17 08:35:02 -08001213 * Complain about starvation of grace-period kthread.
1214 */
1215static void rcu_check_gp_kthread_starvation(struct rcu_state *rsp)
1216{
1217 unsigned long gpa;
1218 unsigned long j;
1219
1220 j = jiffies;
Paul E. McKenney7d0ae802015-03-03 14:57:58 -08001221 gpa = READ_ONCE(rsp->gp_activity);
Paul E. McKenneyb1adb3e2015-10-01 10:38:16 -07001222 if (j - gpa > 2 * HZ) {
Paul E. McKenney6b50e112015-11-17 14:39:26 -08001223 pr_err("%s kthread starved for %ld jiffies! g%lu c%lu f%#x %s(%d) ->state=%#lx\n",
Paul E. McKenney81e701e432015-04-16 11:02:25 -07001224 rsp->name, j - gpa,
Paul E. McKenney319362c2015-05-19 14:16:52 -07001225 rsp->gpnum, rsp->completed,
Paul E. McKenney6b50e112015-11-17 14:39:26 -08001226 rsp->gp_flags,
1227 gp_state_getname(rsp->gp_state), rsp->gp_state,
Paul E. McKenneya0e3a3a2015-12-05 17:34:10 -08001228 rsp->gp_kthread ? rsp->gp_kthread->state : ~0);
Paul E. McKenney86057b82015-12-31 08:48:36 -08001229 if (rsp->gp_kthread) {
Paul E. McKenneyb1adb3e2015-10-01 10:38:16 -07001230 sched_show_task(rsp->gp_kthread);
Paul E. McKenney86057b82015-12-31 08:48:36 -08001231 wake_up_process(rsp->gp_kthread);
1232 }
Paul E. McKenneyb1adb3e2015-10-01 10:38:16 -07001233 }
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001234}
1235
Paul E. McKenneyb637a322012-09-19 16:58:38 -07001236/*
Paul E. McKenneybc1dce52014-06-18 09:18:31 -07001237 * Dump stacks of all tasks running on stalled CPUs.
Paul E. McKenneyb637a322012-09-19 16:58:38 -07001238 */
1239static void rcu_dump_cpu_stacks(struct rcu_state *rsp)
1240{
1241 int cpu;
1242 unsigned long flags;
1243 struct rcu_node *rnp;
1244
1245 rcu_for_each_leaf_node(rsp, rnp) {
Paul E. McKenney6cf10082015-10-08 15:36:54 -07001246 raw_spin_lock_irqsave_rcu_node(rnp, flags);
Paul E. McKenneyb637a322012-09-19 16:58:38 -07001247 if (rnp->qsmask != 0) {
1248 for (cpu = 0; cpu <= rnp->grphi - rnp->grplo; cpu++)
1249 if (rnp->qsmask & (1UL << cpu))
1250 dump_cpu_task(rnp->grplo + cpu);
1251 }
Boqun Feng67c583a72015-12-29 12:18:47 +08001252 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
Paul E. McKenneyb637a322012-09-19 16:58:38 -07001253 }
1254}
1255
Paul E. McKenney8c7c4822016-01-03 20:29:57 -08001256/*
1257 * If too much time has passed in the current grace period, and if
1258 * so configured, go kick the relevant kthreads.
1259 */
1260static void rcu_stall_kick_kthreads(struct rcu_state *rsp)
1261{
1262 unsigned long j;
1263
1264 if (!rcu_kick_kthreads)
1265 return;
1266 j = READ_ONCE(rsp->jiffies_kick_kthreads);
1267 if (time_after(jiffies, j) && rsp->gp_kthread) {
1268 WARN_ONCE(1, "Kicking %s grace-period kthread\n", rsp->name);
1269 wake_up_process(rsp->gp_kthread);
1270 WRITE_ONCE(rsp->jiffies_kick_kthreads, j + HZ);
1271 }
1272}
1273
Paul E. McKenney6ccd2ec2014-12-11 10:20:59 -08001274static void print_other_cpu_stall(struct rcu_state *rsp, unsigned long gpnum)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001275{
1276 int cpu;
1277 long delta;
1278 unsigned long flags;
Paul E. McKenney6ccd2ec2014-12-11 10:20:59 -08001279 unsigned long gpa;
1280 unsigned long j;
Paul E. McKenney285fe292012-05-09 08:45:12 -07001281 int ndetected = 0;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001282 struct rcu_node *rnp = rcu_get_root(rsp);
Paul E. McKenney53bb8572012-09-21 16:35:25 -07001283 long totqlen = 0;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001284
Paul E. McKenney8c7c4822016-01-03 20:29:57 -08001285 /* Kick and suppress, if so configured. */
1286 rcu_stall_kick_kthreads(rsp);
1287 if (rcu_cpu_stall_suppress)
1288 return;
1289
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001290 /* Only let one CPU complain about others per time interval. */
1291
Paul E. McKenney6cf10082015-10-08 15:36:54 -07001292 raw_spin_lock_irqsave_rcu_node(rnp, flags);
Paul E. McKenney7d0ae802015-03-03 14:57:58 -08001293 delta = jiffies - READ_ONCE(rsp->jiffies_stall);
Paul E. McKenneyfc2219d42009-09-23 09:50:41 -07001294 if (delta < RCU_STALL_RAT_DELAY || !rcu_gp_in_progress(rsp)) {
Boqun Feng67c583a72015-12-29 12:18:47 +08001295 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001296 return;
1297 }
Paul E. McKenney7d0ae802015-03-03 14:57:58 -08001298 WRITE_ONCE(rsp->jiffies_stall,
1299 jiffies + 3 * rcu_jiffies_till_stall_check() + 3);
Boqun Feng67c583a72015-12-29 12:18:47 +08001300 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001301
Paul E. McKenney8cdd32a2010-08-09 14:23:03 -07001302 /*
1303 * OK, time to rat on our buddy...
1304 * See Documentation/RCU/stallwarn.txt for info on how to debug
1305 * RCU CPU stall warnings.
1306 */
Paul E. McKenneyd7f3e202013-03-18 16:19:52 -07001307 pr_err("INFO: %s detected stalls on CPUs/tasks:",
Paul E. McKenney4300aa62010-04-13 16:18:22 -07001308 rsp->name);
Paul E. McKenneya858af22012-01-16 13:29:10 -08001309 print_cpu_stall_info_begin();
Paul E. McKenneya0b6c9a2009-09-28 07:46:33 -07001310 rcu_for_each_leaf_node(rsp, rnp) {
Paul E. McKenney6cf10082015-10-08 15:36:54 -07001311 raw_spin_lock_irqsave_rcu_node(rnp, flags);
Paul E. McKenney9bc8b552011-08-13 13:31:47 -07001312 ndetected += rcu_print_task_stall(rnp);
Paul E. McKenneyc8020a62012-08-10 16:55:59 -07001313 if (rnp->qsmask != 0) {
1314 for (cpu = 0; cpu <= rnp->grphi - rnp->grplo; cpu++)
1315 if (rnp->qsmask & (1UL << cpu)) {
1316 print_cpu_stall_info(rsp,
1317 rnp->grplo + cpu);
1318 ndetected++;
1319 }
1320 }
Boqun Feng67c583a72015-12-29 12:18:47 +08001321 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001322 }
Paul E. McKenneya858af22012-01-16 13:29:10 -08001323
Paul E. McKenneya858af22012-01-16 13:29:10 -08001324 print_cpu_stall_info_end();
Paul E. McKenney53bb8572012-09-21 16:35:25 -07001325 for_each_possible_cpu(cpu)
1326 totqlen += per_cpu_ptr(rsp->rda, cpu)->qlen;
Paul E. McKenney83ebe632014-03-06 11:09:10 -08001327 pr_cont("(detected by %d, t=%ld jiffies, g=%ld, c=%ld, q=%lu)\n",
Paul E. McKenneyeee05882012-09-21 14:15:05 -07001328 smp_processor_id(), (long)(jiffies - rsp->gp_start),
Paul E. McKenney83ebe632014-03-06 11:09:10 -08001329 (long)rsp->gpnum, (long)rsp->completed, totqlen);
Paul E. McKenney6ccd2ec2014-12-11 10:20:59 -08001330 if (ndetected) {
Paul E. McKenneyb637a322012-09-19 16:58:38 -07001331 rcu_dump_cpu_stacks(rsp);
Paul E. McKenney6ccd2ec2014-12-11 10:20:59 -08001332 } else {
Paul E. McKenney7d0ae802015-03-03 14:57:58 -08001333 if (READ_ONCE(rsp->gpnum) != gpnum ||
1334 READ_ONCE(rsp->completed) == gpnum) {
Paul E. McKenney6ccd2ec2014-12-11 10:20:59 -08001335 pr_err("INFO: Stall ended before state dump start\n");
1336 } else {
1337 j = jiffies;
Paul E. McKenney7d0ae802015-03-03 14:57:58 -08001338 gpa = READ_ONCE(rsp->gp_activity);
Paul E. McKenney237a0f22015-01-22 14:32:06 -08001339 pr_err("All QSes seen, last %s kthread activity %ld (%ld-%ld), jiffies_till_next_fqs=%ld, root ->qsmask %#lx\n",
Paul E. McKenney6ccd2ec2014-12-11 10:20:59 -08001340 rsp->name, j - gpa, j, gpa,
Paul E. McKenney237a0f22015-01-22 14:32:06 -08001341 jiffies_till_next_fqs,
1342 rcu_get_root(rsp)->qsmask);
Paul E. McKenney6ccd2ec2014-12-11 10:20:59 -08001343 /* In this case, the current CPU might be at fault. */
1344 sched_show_task(current);
1345 }
1346 }
Ingo Molnarc1dc0b92009-08-02 11:28:21 +02001347
Paul E. McKenney4cdfc1752012-06-22 17:06:26 -07001348 /* Complain about tasks blocking the grace period. */
Paul E. McKenney1ed509a2010-02-22 17:05:05 -08001349 rcu_print_detail_task_stall(rsp);
1350
Paul E. McKenneyfb81a442014-12-17 08:35:02 -08001351 rcu_check_gp_kthread_starvation(rsp);
1352
Paul E. McKenney4cdfc1752012-06-22 17:06:26 -07001353 force_quiescent_state(rsp); /* Kick them all. */
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001354}
1355
1356static void print_cpu_stall(struct rcu_state *rsp)
1357{
Paul E. McKenney53bb8572012-09-21 16:35:25 -07001358 int cpu;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001359 unsigned long flags;
1360 struct rcu_node *rnp = rcu_get_root(rsp);
Paul E. McKenney53bb8572012-09-21 16:35:25 -07001361 long totqlen = 0;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001362
Paul E. McKenney8c7c4822016-01-03 20:29:57 -08001363 /* Kick and suppress, if so configured. */
1364 rcu_stall_kick_kthreads(rsp);
1365 if (rcu_cpu_stall_suppress)
1366 return;
1367
Paul E. McKenney8cdd32a2010-08-09 14:23:03 -07001368 /*
1369 * OK, time to rat on ourselves...
1370 * See Documentation/RCU/stallwarn.txt for info on how to debug
1371 * RCU CPU stall warnings.
1372 */
Paul E. McKenneyd7f3e202013-03-18 16:19:52 -07001373 pr_err("INFO: %s self-detected stall on CPU", rsp->name);
Paul E. McKenneya858af22012-01-16 13:29:10 -08001374 print_cpu_stall_info_begin();
1375 print_cpu_stall_info(rsp, smp_processor_id());
1376 print_cpu_stall_info_end();
Paul E. McKenney53bb8572012-09-21 16:35:25 -07001377 for_each_possible_cpu(cpu)
1378 totqlen += per_cpu_ptr(rsp->rda, cpu)->qlen;
Paul E. McKenney83ebe632014-03-06 11:09:10 -08001379 pr_cont(" (t=%lu jiffies g=%ld c=%ld q=%lu)\n",
1380 jiffies - rsp->gp_start,
1381 (long)rsp->gpnum, (long)rsp->completed, totqlen);
Paul E. McKenneyfb81a442014-12-17 08:35:02 -08001382
1383 rcu_check_gp_kthread_starvation(rsp);
1384
Paul E. McKenneybc1dce52014-06-18 09:18:31 -07001385 rcu_dump_cpu_stacks(rsp);
Ingo Molnarc1dc0b92009-08-02 11:28:21 +02001386
Paul E. McKenney6cf10082015-10-08 15:36:54 -07001387 raw_spin_lock_irqsave_rcu_node(rnp, flags);
Paul E. McKenney7d0ae802015-03-03 14:57:58 -08001388 if (ULONG_CMP_GE(jiffies, READ_ONCE(rsp->jiffies_stall)))
1389 WRITE_ONCE(rsp->jiffies_stall,
1390 jiffies + 3 * rcu_jiffies_till_stall_check() + 3);
Boqun Feng67c583a72015-12-29 12:18:47 +08001391 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
Ingo Molnarc1dc0b92009-08-02 11:28:21 +02001392
Peter Zijlstrab021fe32013-09-17 09:30:55 +02001393 /*
1394 * Attempt to revive the RCU machinery by forcing a context switch.
1395 *
1396 * A context switch would normally allow the RCU state machine to make
1397 * progress and it could be we're stuck in kernel space without context
1398 * switches for an entirely unreasonable amount of time.
1399 */
1400 resched_cpu(smp_processor_id());
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001401}
1402
1403static void check_cpu_stall(struct rcu_state *rsp, struct rcu_data *rdp)
1404{
Paul E. McKenney26cdfed2013-09-04 10:51:13 -07001405 unsigned long completed;
1406 unsigned long gpnum;
1407 unsigned long gps;
Paul E. McKenneybad6e132011-05-02 23:40:04 -07001408 unsigned long j;
1409 unsigned long js;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001410 struct rcu_node *rnp;
1411
Paul E. McKenney8c7c4822016-01-03 20:29:57 -08001412 if ((rcu_cpu_stall_suppress && !rcu_kick_kthreads) ||
1413 !rcu_gp_in_progress(rsp))
Paul E. McKenneyc68de202010-04-15 10:12:40 -07001414 return;
Paul E. McKenney8c7c4822016-01-03 20:29:57 -08001415 rcu_stall_kick_kthreads(rsp);
Paul E. McKenneycb1e78c2013-12-04 18:42:03 -08001416 j = jiffies;
Paul E. McKenney26cdfed2013-09-04 10:51:13 -07001417
1418 /*
1419 * Lots of memory barriers to reject false positives.
1420 *
1421 * The idea is to pick up rsp->gpnum, then rsp->jiffies_stall,
1422 * then rsp->gp_start, and finally rsp->completed. These values
1423 * are updated in the opposite order with memory barriers (or
1424 * equivalent) during grace-period initialization and cleanup.
1425 * Now, a false positive can occur if we get an new value of
1426 * rsp->gp_start and a old value of rsp->jiffies_stall. But given
1427 * the memory barriers, the only way that this can happen is if one
1428 * grace period ends and another starts between these two fetches.
1429 * Detect this by comparing rsp->completed with the previous fetch
1430 * from rsp->gpnum.
1431 *
1432 * Given this check, comparisons of jiffies, rsp->jiffies_stall,
1433 * and rsp->gp_start suffice to forestall false positives.
1434 */
Paul E. McKenney7d0ae802015-03-03 14:57:58 -08001435 gpnum = READ_ONCE(rsp->gpnum);
Paul E. McKenney26cdfed2013-09-04 10:51:13 -07001436 smp_rmb(); /* Pick up ->gpnum first... */
Paul E. McKenney7d0ae802015-03-03 14:57:58 -08001437 js = READ_ONCE(rsp->jiffies_stall);
Paul E. McKenney26cdfed2013-09-04 10:51:13 -07001438 smp_rmb(); /* ...then ->jiffies_stall before the rest... */
Paul E. McKenney7d0ae802015-03-03 14:57:58 -08001439 gps = READ_ONCE(rsp->gp_start);
Paul E. McKenney26cdfed2013-09-04 10:51:13 -07001440 smp_rmb(); /* ...and finally ->gp_start before ->completed. */
Paul E. McKenney7d0ae802015-03-03 14:57:58 -08001441 completed = READ_ONCE(rsp->completed);
Paul E. McKenney26cdfed2013-09-04 10:51:13 -07001442 if (ULONG_CMP_GE(completed, gpnum) ||
1443 ULONG_CMP_LT(j, js) ||
1444 ULONG_CMP_GE(gps, js))
1445 return; /* No stall or GP completed since entering function. */
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001446 rnp = rdp->mynode;
Paul E. McKenneyc96ea7c2012-08-13 11:17:06 -07001447 if (rcu_gp_in_progress(rsp) &&
Paul E. McKenney7d0ae802015-03-03 14:57:58 -08001448 (READ_ONCE(rnp->qsmask) & rdp->grpmask)) {
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001449
1450 /* We haven't checked in, so go dump stack. */
1451 print_cpu_stall(rsp);
1452
Paul E. McKenneybad6e132011-05-02 23:40:04 -07001453 } else if (rcu_gp_in_progress(rsp) &&
1454 ULONG_CMP_GE(j, js + RCU_STALL_RAT_DELAY)) {
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001455
Paul E. McKenneybad6e132011-05-02 23:40:04 -07001456 /* They had a few time units to dump stack, so complain. */
Paul E. McKenney6ccd2ec2014-12-11 10:20:59 -08001457 print_other_cpu_stall(rsp, gpnum);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001458 }
1459}
1460
Paul E. McKenney53d84e02010-08-10 14:28:53 -07001461/**
1462 * rcu_cpu_stall_reset - prevent further stall warnings in current grace period
1463 *
1464 * Set the stall-warning timeout way off into the future, thus preventing
1465 * any RCU CPU stall-warning messages from appearing in the current set of
1466 * RCU grace periods.
1467 *
1468 * The caller must disable hard irqs.
1469 */
1470void rcu_cpu_stall_reset(void)
1471{
Paul E. McKenney6ce75a22012-06-12 11:01:13 -07001472 struct rcu_state *rsp;
1473
1474 for_each_rcu_flavor(rsp)
Paul E. McKenney7d0ae802015-03-03 14:57:58 -08001475 WRITE_ONCE(rsp->jiffies_stall, jiffies + ULONG_MAX / 2);
Paul E. McKenney53d84e02010-08-10 14:28:53 -07001476}
1477
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001478/*
Paul E. McKenneyd3f3f3f2015-01-18 18:21:09 -08001479 * Initialize the specified rcu_data structure's default callback list
1480 * to empty. The default callback list is the one that is not used by
1481 * no-callbacks CPUs.
1482 */
1483static void init_default_callback_list(struct rcu_data *rdp)
1484{
1485 int i;
1486
1487 rdp->nxtlist = NULL;
1488 for (i = 0; i < RCU_NEXT_SIZE; i++)
1489 rdp->nxttail[i] = &rdp->nxtlist;
1490}
1491
1492/*
Paul E. McKenney3f5d3ea2012-05-09 15:39:56 -07001493 * Initialize the specified rcu_data structure's callback list to empty.
1494 */
1495static void init_callback_list(struct rcu_data *rdp)
1496{
Paul E. McKenney34ed62462013-01-07 13:37:42 -08001497 if (init_nocb_callback_list(rdp))
1498 return;
Paul E. McKenneyd3f3f3f2015-01-18 18:21:09 -08001499 init_default_callback_list(rdp);
Paul E. McKenney3f5d3ea2012-05-09 15:39:56 -07001500}
1501
1502/*
Paul E. McKenneydc35c892012-12-03 13:52:00 -08001503 * Determine the value that ->completed will have at the end of the
1504 * next subsequent grace period. This is used to tag callbacks so that
1505 * a CPU can invoke callbacks in a timely fashion even if that CPU has
1506 * been dyntick-idle for an extended period with callbacks under the
1507 * influence of RCU_FAST_NO_HZ.
1508 *
1509 * The caller must hold rnp->lock with interrupts disabled.
1510 */
1511static unsigned long rcu_cbs_completed(struct rcu_state *rsp,
1512 struct rcu_node *rnp)
1513{
1514 /*
1515 * If RCU is idle, we just wait for the next grace period.
1516 * But we can only be sure that RCU is idle if we are looking
1517 * at the root rcu_node structure -- otherwise, a new grace
1518 * period might have started, but just not yet gotten around
1519 * to initializing the current non-root rcu_node structure.
1520 */
1521 if (rcu_get_root(rsp) == rnp && rnp->gpnum == rnp->completed)
1522 return rnp->completed + 1;
1523
1524 /*
1525 * Otherwise, wait for a possible partial grace period and
1526 * then the subsequent full grace period.
1527 */
1528 return rnp->completed + 2;
1529}
1530
1531/*
Paul E. McKenney0446be42012-12-30 15:21:01 -08001532 * Trace-event helper function for rcu_start_future_gp() and
1533 * rcu_nocb_wait_gp().
1534 */
1535static void trace_rcu_future_gp(struct rcu_node *rnp, struct rcu_data *rdp,
Steven Rostedt (Red Hat)e66c33d2013-07-12 16:50:28 -04001536 unsigned long c, const char *s)
Paul E. McKenney0446be42012-12-30 15:21:01 -08001537{
1538 trace_rcu_future_grace_period(rdp->rsp->name, rnp->gpnum,
1539 rnp->completed, c, rnp->level,
1540 rnp->grplo, rnp->grphi, s);
1541}
1542
1543/*
1544 * Start some future grace period, as needed to handle newly arrived
1545 * callbacks. The required future grace periods are recorded in each
Paul E. McKenney48a76392014-03-11 13:02:16 -07001546 * rcu_node structure's ->need_future_gp field. Returns true if there
1547 * is reason to awaken the grace-period kthread.
Paul E. McKenney0446be42012-12-30 15:21:01 -08001548 *
1549 * The caller must hold the specified rcu_node structure's ->lock.
1550 */
Paul E. McKenney48a76392014-03-11 13:02:16 -07001551static bool __maybe_unused
1552rcu_start_future_gp(struct rcu_node *rnp, struct rcu_data *rdp,
1553 unsigned long *c_out)
Paul E. McKenney0446be42012-12-30 15:21:01 -08001554{
1555 unsigned long c;
1556 int i;
Paul E. McKenney48a76392014-03-11 13:02:16 -07001557 bool ret = false;
Paul E. McKenney0446be42012-12-30 15:21:01 -08001558 struct rcu_node *rnp_root = rcu_get_root(rdp->rsp);
1559
1560 /*
1561 * Pick up grace-period number for new callbacks. If this
1562 * grace period is already marked as needed, return to the caller.
1563 */
1564 c = rcu_cbs_completed(rdp->rsp, rnp);
Steven Rostedt (Red Hat)f7f7bac2013-07-12 17:18:47 -04001565 trace_rcu_future_gp(rnp, rdp, c, TPS("Startleaf"));
Paul E. McKenney0446be42012-12-30 15:21:01 -08001566 if (rnp->need_future_gp[c & 0x1]) {
Steven Rostedt (Red Hat)f7f7bac2013-07-12 17:18:47 -04001567 trace_rcu_future_gp(rnp, rdp, c, TPS("Prestartleaf"));
Paul E. McKenney48a76392014-03-11 13:02:16 -07001568 goto out;
Paul E. McKenney0446be42012-12-30 15:21:01 -08001569 }
1570
1571 /*
1572 * If either this rcu_node structure or the root rcu_node structure
1573 * believe that a grace period is in progress, then we must wait
1574 * for the one following, which is in "c". Because our request
1575 * will be noticed at the end of the current grace period, we don't
Pranith Kumar48bd8e92014-06-11 10:32:47 -07001576 * need to explicitly start one. We only do the lockless check
1577 * of rnp_root's fields if the current rcu_node structure thinks
1578 * there is no grace period in flight, and because we hold rnp->lock,
1579 * the only possible change is when rnp_root's two fields are
1580 * equal, in which case rnp_root->gpnum might be concurrently
1581 * incremented. But that is OK, as it will just result in our
1582 * doing some extra useless work.
Paul E. McKenney0446be42012-12-30 15:21:01 -08001583 */
1584 if (rnp->gpnum != rnp->completed ||
Paul E. McKenney7d0ae802015-03-03 14:57:58 -08001585 READ_ONCE(rnp_root->gpnum) != READ_ONCE(rnp_root->completed)) {
Paul E. McKenney0446be42012-12-30 15:21:01 -08001586 rnp->need_future_gp[c & 0x1]++;
Steven Rostedt (Red Hat)f7f7bac2013-07-12 17:18:47 -04001587 trace_rcu_future_gp(rnp, rdp, c, TPS("Startedleaf"));
Paul E. McKenney48a76392014-03-11 13:02:16 -07001588 goto out;
Paul E. McKenney0446be42012-12-30 15:21:01 -08001589 }
1590
1591 /*
1592 * There might be no grace period in progress. If we don't already
1593 * hold it, acquire the root rcu_node structure's lock in order to
1594 * start one (if needed).
1595 */
Peter Zijlstra2a67e742015-10-08 12:24:23 +02001596 if (rnp != rnp_root)
1597 raw_spin_lock_rcu_node(rnp_root);
Paul E. McKenney0446be42012-12-30 15:21:01 -08001598
1599 /*
1600 * Get a new grace-period number. If there really is no grace
1601 * period in progress, it will be smaller than the one we obtained
1602 * earlier. Adjust callbacks as needed. Note that even no-CBs
1603 * CPUs have a ->nxtcompleted[] array, so no no-CBs checks needed.
1604 */
1605 c = rcu_cbs_completed(rdp->rsp, rnp_root);
1606 for (i = RCU_DONE_TAIL; i < RCU_NEXT_TAIL; i++)
1607 if (ULONG_CMP_LT(c, rdp->nxtcompleted[i]))
1608 rdp->nxtcompleted[i] = c;
1609
1610 /*
1611 * If the needed for the required grace period is already
1612 * recorded, trace and leave.
1613 */
1614 if (rnp_root->need_future_gp[c & 0x1]) {
Steven Rostedt (Red Hat)f7f7bac2013-07-12 17:18:47 -04001615 trace_rcu_future_gp(rnp, rdp, c, TPS("Prestartedroot"));
Paul E. McKenney0446be42012-12-30 15:21:01 -08001616 goto unlock_out;
1617 }
1618
1619 /* Record the need for the future grace period. */
1620 rnp_root->need_future_gp[c & 0x1]++;
1621
1622 /* If a grace period is not already in progress, start one. */
1623 if (rnp_root->gpnum != rnp_root->completed) {
Steven Rostedt (Red Hat)f7f7bac2013-07-12 17:18:47 -04001624 trace_rcu_future_gp(rnp, rdp, c, TPS("Startedleafroot"));
Paul E. McKenney0446be42012-12-30 15:21:01 -08001625 } else {
Steven Rostedt (Red Hat)f7f7bac2013-07-12 17:18:47 -04001626 trace_rcu_future_gp(rnp, rdp, c, TPS("Startedroot"));
Paul E. McKenney48a76392014-03-11 13:02:16 -07001627 ret = rcu_start_gp_advanced(rdp->rsp, rnp_root, rdp);
Paul E. McKenney0446be42012-12-30 15:21:01 -08001628 }
1629unlock_out:
1630 if (rnp != rnp_root)
Boqun Feng67c583a72015-12-29 12:18:47 +08001631 raw_spin_unlock_rcu_node(rnp_root);
Paul E. McKenney48a76392014-03-11 13:02:16 -07001632out:
1633 if (c_out != NULL)
1634 *c_out = c;
1635 return ret;
Paul E. McKenney0446be42012-12-30 15:21:01 -08001636}
1637
1638/*
1639 * Clean up any old requests for the just-ended grace period. Also return
1640 * whether any additional grace periods have been requested. Also invoke
1641 * rcu_nocb_gp_cleanup() in order to wake up any no-callbacks kthreads
1642 * waiting for this grace period to complete.
1643 */
1644static int rcu_future_gp_cleanup(struct rcu_state *rsp, struct rcu_node *rnp)
1645{
1646 int c = rnp->completed;
1647 int needmore;
1648 struct rcu_data *rdp = this_cpu_ptr(rsp->rda);
1649
Paul E. McKenney0446be42012-12-30 15:21:01 -08001650 rnp->need_future_gp[c & 0x1] = 0;
1651 needmore = rnp->need_future_gp[(c + 1) & 0x1];
Steven Rostedt (Red Hat)f7f7bac2013-07-12 17:18:47 -04001652 trace_rcu_future_gp(rnp, rdp, c,
1653 needmore ? TPS("CleanupMore") : TPS("Cleanup"));
Paul E. McKenney0446be42012-12-30 15:21:01 -08001654 return needmore;
1655}
1656
1657/*
Paul E. McKenney48a76392014-03-11 13:02:16 -07001658 * Awaken the grace-period kthread for the specified flavor of RCU.
1659 * Don't do a self-awaken, and don't bother awakening when there is
1660 * nothing for the grace-period kthread to do (as in several CPUs
1661 * raced to awaken, and we lost), and finally don't try to awaken
1662 * a kthread that has not yet been created.
1663 */
1664static void rcu_gp_kthread_wake(struct rcu_state *rsp)
1665{
1666 if (current == rsp->gp_kthread ||
Paul E. McKenney7d0ae802015-03-03 14:57:58 -08001667 !READ_ONCE(rsp->gp_flags) ||
Paul E. McKenney48a76392014-03-11 13:02:16 -07001668 !rsp->gp_kthread)
1669 return;
Paul Gortmakerabedf8e2016-02-19 09:46:41 +01001670 swake_up(&rsp->gp_wq);
Paul E. McKenney48a76392014-03-11 13:02:16 -07001671}
1672
1673/*
Paul E. McKenneydc35c892012-12-03 13:52:00 -08001674 * If there is room, assign a ->completed number to any callbacks on
1675 * this CPU that have not already been assigned. Also accelerate any
1676 * callbacks that were previously assigned a ->completed number that has
1677 * since proven to be too conservative, which can happen if callbacks get
1678 * assigned a ->completed number while RCU is idle, but with reference to
1679 * a non-root rcu_node structure. This function is idempotent, so it does
Paul E. McKenney48a76392014-03-11 13:02:16 -07001680 * not hurt to call it repeatedly. Returns an flag saying that we should
1681 * awaken the RCU grace-period kthread.
Paul E. McKenneydc35c892012-12-03 13:52:00 -08001682 *
1683 * The caller must hold rnp->lock with interrupts disabled.
1684 */
Paul E. McKenney48a76392014-03-11 13:02:16 -07001685static bool rcu_accelerate_cbs(struct rcu_state *rsp, struct rcu_node *rnp,
Paul E. McKenneydc35c892012-12-03 13:52:00 -08001686 struct rcu_data *rdp)
1687{
1688 unsigned long c;
1689 int i;
Paul E. McKenney48a76392014-03-11 13:02:16 -07001690 bool ret;
Paul E. McKenneydc35c892012-12-03 13:52:00 -08001691
1692 /* If the CPU has no callbacks, nothing to do. */
1693 if (!rdp->nxttail[RCU_NEXT_TAIL] || !*rdp->nxttail[RCU_DONE_TAIL])
Paul E. McKenney48a76392014-03-11 13:02:16 -07001694 return false;
Paul E. McKenneydc35c892012-12-03 13:52:00 -08001695
1696 /*
1697 * Starting from the sublist containing the callbacks most
1698 * recently assigned a ->completed number and working down, find the
1699 * first sublist that is not assignable to an upcoming grace period.
1700 * Such a sublist has something in it (first two tests) and has
1701 * a ->completed number assigned that will complete sooner than
1702 * the ->completed number for newly arrived callbacks (last test).
1703 *
1704 * The key point is that any later sublist can be assigned the
1705 * same ->completed number as the newly arrived callbacks, which
1706 * means that the callbacks in any of these later sublist can be
1707 * grouped into a single sublist, whether or not they have already
1708 * been assigned a ->completed number.
1709 */
1710 c = rcu_cbs_completed(rsp, rnp);
1711 for (i = RCU_NEXT_TAIL - 1; i > RCU_DONE_TAIL; i--)
1712 if (rdp->nxttail[i] != rdp->nxttail[i - 1] &&
1713 !ULONG_CMP_GE(rdp->nxtcompleted[i], c))
1714 break;
1715
1716 /*
1717 * If there are no sublist for unassigned callbacks, leave.
1718 * At the same time, advance "i" one sublist, so that "i" will
1719 * index into the sublist where all the remaining callbacks should
1720 * be grouped into.
1721 */
1722 if (++i >= RCU_NEXT_TAIL)
Paul E. McKenney48a76392014-03-11 13:02:16 -07001723 return false;
Paul E. McKenneydc35c892012-12-03 13:52:00 -08001724
1725 /*
1726 * Assign all subsequent callbacks' ->completed number to the next
1727 * full grace period and group them all in the sublist initially
1728 * indexed by "i".
1729 */
1730 for (; i <= RCU_NEXT_TAIL; i++) {
1731 rdp->nxttail[i] = rdp->nxttail[RCU_NEXT_TAIL];
1732 rdp->nxtcompleted[i] = c;
1733 }
Paul E. McKenney910ee452012-12-31 02:24:21 -08001734 /* Record any needed additional grace periods. */
Paul E. McKenney48a76392014-03-11 13:02:16 -07001735 ret = rcu_start_future_gp(rnp, rdp, NULL);
Paul E. McKenney6d4b4182012-11-27 16:55:44 -08001736
1737 /* Trace depending on how much we were able to accelerate. */
1738 if (!*rdp->nxttail[RCU_WAIT_TAIL])
Steven Rostedt (Red Hat)f7f7bac2013-07-12 17:18:47 -04001739 trace_rcu_grace_period(rsp->name, rdp->gpnum, TPS("AccWaitCB"));
Paul E. McKenney6d4b4182012-11-27 16:55:44 -08001740 else
Steven Rostedt (Red Hat)f7f7bac2013-07-12 17:18:47 -04001741 trace_rcu_grace_period(rsp->name, rdp->gpnum, TPS("AccReadyCB"));
Paul E. McKenney48a76392014-03-11 13:02:16 -07001742 return ret;
Paul E. McKenneydc35c892012-12-03 13:52:00 -08001743}
1744
1745/*
1746 * Move any callbacks whose grace period has completed to the
1747 * RCU_DONE_TAIL sublist, then compact the remaining sublists and
1748 * assign ->completed numbers to any callbacks in the RCU_NEXT_TAIL
1749 * sublist. This function is idempotent, so it does not hurt to
1750 * invoke it repeatedly. As long as it is not invoked -too- often...
Paul E. McKenney48a76392014-03-11 13:02:16 -07001751 * Returns true if the RCU grace-period kthread needs to be awakened.
Paul E. McKenneydc35c892012-12-03 13:52:00 -08001752 *
1753 * The caller must hold rnp->lock with interrupts disabled.
1754 */
Paul E. McKenney48a76392014-03-11 13:02:16 -07001755static bool rcu_advance_cbs(struct rcu_state *rsp, struct rcu_node *rnp,
Paul E. McKenneydc35c892012-12-03 13:52:00 -08001756 struct rcu_data *rdp)
1757{
1758 int i, j;
1759
1760 /* If the CPU has no callbacks, nothing to do. */
1761 if (!rdp->nxttail[RCU_NEXT_TAIL] || !*rdp->nxttail[RCU_DONE_TAIL])
Paul E. McKenney48a76392014-03-11 13:02:16 -07001762 return false;
Paul E. McKenneydc35c892012-12-03 13:52:00 -08001763
1764 /*
1765 * Find all callbacks whose ->completed numbers indicate that they
1766 * are ready to invoke, and put them into the RCU_DONE_TAIL sublist.
1767 */
1768 for (i = RCU_WAIT_TAIL; i < RCU_NEXT_TAIL; i++) {
1769 if (ULONG_CMP_LT(rnp->completed, rdp->nxtcompleted[i]))
1770 break;
1771 rdp->nxttail[RCU_DONE_TAIL] = rdp->nxttail[i];
1772 }
1773 /* Clean up any sublist tail pointers that were misordered above. */
1774 for (j = RCU_WAIT_TAIL; j < i; j++)
1775 rdp->nxttail[j] = rdp->nxttail[RCU_DONE_TAIL];
1776
1777 /* Copy down callbacks to fill in empty sublists. */
1778 for (j = RCU_WAIT_TAIL; i < RCU_NEXT_TAIL; i++, j++) {
1779 if (rdp->nxttail[j] == rdp->nxttail[RCU_NEXT_TAIL])
1780 break;
1781 rdp->nxttail[j] = rdp->nxttail[i];
1782 rdp->nxtcompleted[j] = rdp->nxtcompleted[i];
1783 }
1784
1785 /* Classify any remaining callbacks. */
Paul E. McKenney48a76392014-03-11 13:02:16 -07001786 return rcu_accelerate_cbs(rsp, rnp, rdp);
Paul E. McKenneydc35c892012-12-03 13:52:00 -08001787}
1788
1789/*
Paul E. McKenneyba9fbe92013-03-19 11:53:31 -07001790 * Update CPU-local rcu_data state to record the beginnings and ends of
1791 * grace periods. The caller must hold the ->lock of the leaf rcu_node
1792 * structure corresponding to the current CPU, and must have irqs disabled.
Paul E. McKenney48a76392014-03-11 13:02:16 -07001793 * Returns true if the grace-period kthread needs to be awakened.
Paul E. McKenneyd09b62d2009-11-02 13:52:28 -08001794 */
Paul E. McKenney48a76392014-03-11 13:02:16 -07001795static bool __note_gp_changes(struct rcu_state *rsp, struct rcu_node *rnp,
1796 struct rcu_data *rdp)
Paul E. McKenneyd09b62d2009-11-02 13:52:28 -08001797{
Paul E. McKenney48a76392014-03-11 13:02:16 -07001798 bool ret;
1799
Paul E. McKenneyba9fbe92013-03-19 11:53:31 -07001800 /* Handle the ends of any preceding grace periods first. */
Paul E. McKenneye3663b12014-12-08 20:26:55 -08001801 if (rdp->completed == rnp->completed &&
Paul E. McKenney7d0ae802015-03-03 14:57:58 -08001802 !unlikely(READ_ONCE(rdp->gpwrap))) {
Paul E. McKenneyd09b62d2009-11-02 13:52:28 -08001803
Paul E. McKenneyba9fbe92013-03-19 11:53:31 -07001804 /* No grace period end, so just accelerate recent callbacks. */
Paul E. McKenney48a76392014-03-11 13:02:16 -07001805 ret = rcu_accelerate_cbs(rsp, rnp, rdp);
Paul E. McKenneydc35c892012-12-03 13:52:00 -08001806
1807 } else {
1808
1809 /* Advance callbacks. */
Paul E. McKenney48a76392014-03-11 13:02:16 -07001810 ret = rcu_advance_cbs(rsp, rnp, rdp);
Paul E. McKenneyd09b62d2009-11-02 13:52:28 -08001811
1812 /* Remember that we saw this grace-period completion. */
1813 rdp->completed = rnp->completed;
Steven Rostedt (Red Hat)f7f7bac2013-07-12 17:18:47 -04001814 trace_rcu_grace_period(rsp->name, rdp->gpnum, TPS("cpuend"));
Paul E. McKenneyd09b62d2009-11-02 13:52:28 -08001815 }
Paul E. McKenney398ebe62013-03-19 10:53:14 -07001816
Paul E. McKenney7d0ae802015-03-03 14:57:58 -08001817 if (rdp->gpnum != rnp->gpnum || unlikely(READ_ONCE(rdp->gpwrap))) {
Paul E. McKenney6eaef632013-03-19 10:08:37 -07001818 /*
1819 * If the current grace period is waiting for this CPU,
1820 * set up to detect a quiescent state, otherwise don't
1821 * go looking for one.
1822 */
1823 rdp->gpnum = rnp->gpnum;
Steven Rostedt (Red Hat)f7f7bac2013-07-12 17:18:47 -04001824 trace_rcu_grace_period(rsp->name, rdp->gpnum, TPS("cpustart"));
Paul E. McKenney5b74c452015-08-06 15:16:57 -07001825 rdp->cpu_no_qs.b.norm = true;
Paul E. McKenney5cd37192014-12-13 20:32:04 -08001826 rdp->rcu_qs_ctr_snap = __this_cpu_read(rcu_qs_ctr);
Paul E. McKenney97c668b2015-08-06 11:31:51 -07001827 rdp->core_needs_qs = !!(rnp->qsmask & rdp->grpmask);
Paul E. McKenney6eaef632013-03-19 10:08:37 -07001828 zero_cpu_stall_ticks(rdp);
Paul E. McKenney7d0ae802015-03-03 14:57:58 -08001829 WRITE_ONCE(rdp->gpwrap, false);
Paul E. McKenney6eaef632013-03-19 10:08:37 -07001830 }
Paul E. McKenney48a76392014-03-11 13:02:16 -07001831 return ret;
Paul E. McKenney6eaef632013-03-19 10:08:37 -07001832}
1833
Paul E. McKenneyd34ea322013-03-19 11:10:43 -07001834static void note_gp_changes(struct rcu_state *rsp, struct rcu_data *rdp)
Paul E. McKenney6eaef632013-03-19 10:08:37 -07001835{
1836 unsigned long flags;
Paul E. McKenney48a76392014-03-11 13:02:16 -07001837 bool needwake;
Paul E. McKenney6eaef632013-03-19 10:08:37 -07001838 struct rcu_node *rnp;
1839
1840 local_irq_save(flags);
1841 rnp = rdp->mynode;
Paul E. McKenney7d0ae802015-03-03 14:57:58 -08001842 if ((rdp->gpnum == READ_ONCE(rnp->gpnum) &&
1843 rdp->completed == READ_ONCE(rnp->completed) &&
1844 !unlikely(READ_ONCE(rdp->gpwrap))) || /* w/out lock. */
Peter Zijlstra2a67e742015-10-08 12:24:23 +02001845 !raw_spin_trylock_rcu_node(rnp)) { /* irqs already off, so later. */
Paul E. McKenney6eaef632013-03-19 10:08:37 -07001846 local_irq_restore(flags);
1847 return;
1848 }
Paul E. McKenney48a76392014-03-11 13:02:16 -07001849 needwake = __note_gp_changes(rsp, rnp, rdp);
Boqun Feng67c583a72015-12-29 12:18:47 +08001850 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
Paul E. McKenney48a76392014-03-11 13:02:16 -07001851 if (needwake)
1852 rcu_gp_kthread_wake(rsp);
Paul E. McKenney6eaef632013-03-19 10:08:37 -07001853}
1854
Paul E. McKenney0f41c0d2015-03-10 18:33:20 -07001855static void rcu_gp_slow(struct rcu_state *rsp, int delay)
1856{
1857 if (delay > 0 &&
1858 !(rsp->gpnum % (rcu_num_nodes * PER_RCU_NODE_PERIOD * delay)))
1859 schedule_timeout_uninterruptible(delay);
1860}
1861
Paul E. McKenney6eaef632013-03-19 10:08:37 -07001862/*
Paul E. McKenney45fed3e2015-11-07 23:35:00 -08001863 * Initialize a new grace period. Return false if no grace period required.
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001864 */
Paul E. McKenney45fed3e2015-11-07 23:35:00 -08001865static bool rcu_gp_init(struct rcu_state *rsp)
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001866{
Paul E. McKenney0aa04b02015-01-23 21:52:37 -08001867 unsigned long oldmask;
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001868 struct rcu_data *rdp;
1869 struct rcu_node *rnp = rcu_get_root(rsp);
1870
Paul E. McKenney7d0ae802015-03-03 14:57:58 -08001871 WRITE_ONCE(rsp->gp_activity, jiffies);
Peter Zijlstra2a67e742015-10-08 12:24:23 +02001872 raw_spin_lock_irq_rcu_node(rnp);
Paul E. McKenney7d0ae802015-03-03 14:57:58 -08001873 if (!READ_ONCE(rsp->gp_flags)) {
Paul E. McKenneyf7be8202013-08-08 18:27:52 -07001874 /* Spurious wakeup, tell caller to go back to sleep. */
Boqun Feng67c583a72015-12-29 12:18:47 +08001875 raw_spin_unlock_irq_rcu_node(rnp);
Paul E. McKenney45fed3e2015-11-07 23:35:00 -08001876 return false;
Paul E. McKenneyf7be8202013-08-08 18:27:52 -07001877 }
Paul E. McKenney7d0ae802015-03-03 14:57:58 -08001878 WRITE_ONCE(rsp->gp_flags, 0); /* Clear all flags: New grace period. */
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001879
Paul E. McKenneyf7be8202013-08-08 18:27:52 -07001880 if (WARN_ON_ONCE(rcu_gp_in_progress(rsp))) {
1881 /*
1882 * Grace period already in progress, don't start another.
1883 * Not supposed to be able to happen.
1884 */
Boqun Feng67c583a72015-12-29 12:18:47 +08001885 raw_spin_unlock_irq_rcu_node(rnp);
Paul E. McKenney45fed3e2015-11-07 23:35:00 -08001886 return false;
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001887 }
1888
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001889 /* Advance to a new grace period and initialize state. */
Paul E. McKenney26cdfed2013-09-04 10:51:13 -07001890 record_gp_stall_check_time(rsp);
Paul E. McKenney765a3f42014-03-14 16:37:08 -07001891 /* Record GP times before starting GP, hence smp_store_release(). */
1892 smp_store_release(&rsp->gpnum, rsp->gpnum + 1);
Steven Rostedt (Red Hat)f7f7bac2013-07-12 17:18:47 -04001893 trace_rcu_grace_period(rsp->name, rsp->gpnum, TPS("start"));
Boqun Feng67c583a72015-12-29 12:18:47 +08001894 raw_spin_unlock_irq_rcu_node(rnp);
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001895
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001896 /*
Paul E. McKenney0aa04b02015-01-23 21:52:37 -08001897 * Apply per-leaf buffered online and offline operations to the
1898 * rcu_node tree. Note that this new grace period need not wait
1899 * for subsequent online CPUs, and that quiescent-state forcing
1900 * will handle subsequent offline CPUs.
1901 */
1902 rcu_for_each_leaf_node(rsp, rnp) {
Paul E. McKenney0f41c0d2015-03-10 18:33:20 -07001903 rcu_gp_slow(rsp, gp_preinit_delay);
Peter Zijlstra2a67e742015-10-08 12:24:23 +02001904 raw_spin_lock_irq_rcu_node(rnp);
Paul E. McKenney0aa04b02015-01-23 21:52:37 -08001905 if (rnp->qsmaskinit == rnp->qsmaskinitnext &&
1906 !rnp->wait_blkd_tasks) {
1907 /* Nothing to do on this leaf rcu_node structure. */
Boqun Feng67c583a72015-12-29 12:18:47 +08001908 raw_spin_unlock_irq_rcu_node(rnp);
Paul E. McKenney0aa04b02015-01-23 21:52:37 -08001909 continue;
1910 }
1911
1912 /* Record old state, apply changes to ->qsmaskinit field. */
1913 oldmask = rnp->qsmaskinit;
1914 rnp->qsmaskinit = rnp->qsmaskinitnext;
1915
1916 /* If zero-ness of ->qsmaskinit changed, propagate up tree. */
1917 if (!oldmask != !rnp->qsmaskinit) {
1918 if (!oldmask) /* First online CPU for this rcu_node. */
1919 rcu_init_new_rnp(rnp);
1920 else if (rcu_preempt_has_tasks(rnp)) /* blocked tasks */
1921 rnp->wait_blkd_tasks = true;
1922 else /* Last offline CPU and can propagate. */
1923 rcu_cleanup_dead_rnp(rnp);
1924 }
1925
1926 /*
1927 * If all waited-on tasks from prior grace period are
1928 * done, and if all this rcu_node structure's CPUs are
1929 * still offline, propagate up the rcu_node tree and
1930 * clear ->wait_blkd_tasks. Otherwise, if one of this
1931 * rcu_node structure's CPUs has since come back online,
1932 * simply clear ->wait_blkd_tasks (but rcu_cleanup_dead_rnp()
1933 * checks for this, so just call it unconditionally).
1934 */
1935 if (rnp->wait_blkd_tasks &&
1936 (!rcu_preempt_has_tasks(rnp) ||
1937 rnp->qsmaskinit)) {
1938 rnp->wait_blkd_tasks = false;
1939 rcu_cleanup_dead_rnp(rnp);
1940 }
1941
Boqun Feng67c583a72015-12-29 12:18:47 +08001942 raw_spin_unlock_irq_rcu_node(rnp);
Paul E. McKenney0aa04b02015-01-23 21:52:37 -08001943 }
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001944
1945 /*
1946 * Set the quiescent-state-needed bits in all the rcu_node
1947 * structures for all currently online CPUs in breadth-first order,
1948 * starting from the root rcu_node structure, relying on the layout
1949 * of the tree within the rsp->node[] array. Note that other CPUs
1950 * will access only the leaves of the hierarchy, thus seeing that no
1951 * grace period is in progress, at least until the corresponding
1952 * leaf node has been initialized. In addition, we have excluded
1953 * CPU-hotplug operations.
1954 *
1955 * The grace period cannot complete until the initialization
1956 * process finishes, because this kthread handles both.
1957 */
1958 rcu_for_each_node_breadth_first(rsp, rnp) {
Paul E. McKenney0f41c0d2015-03-10 18:33:20 -07001959 rcu_gp_slow(rsp, gp_init_delay);
Peter Zijlstra2a67e742015-10-08 12:24:23 +02001960 raw_spin_lock_irq_rcu_node(rnp);
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001961 rdp = this_cpu_ptr(rsp->rda);
1962 rcu_preempt_check_blocked_tasks(rnp);
1963 rnp->qsmask = rnp->qsmaskinit;
Paul E. McKenney7d0ae802015-03-03 14:57:58 -08001964 WRITE_ONCE(rnp->gpnum, rsp->gpnum);
Lai Jiangshan3f47da02015-01-13 15:30:34 +08001965 if (WARN_ON_ONCE(rnp->completed != rsp->completed))
Paul E. McKenney7d0ae802015-03-03 14:57:58 -08001966 WRITE_ONCE(rnp->completed, rsp->completed);
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001967 if (rnp == rdp->mynode)
Paul E. McKenney48a76392014-03-11 13:02:16 -07001968 (void)__note_gp_changes(rsp, rnp, rdp);
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001969 rcu_preempt_boost_start_gp(rnp);
1970 trace_rcu_grace_period_init(rsp->name, rnp->gpnum,
1971 rnp->level, rnp->grplo,
1972 rnp->grphi, rnp->qsmask);
Boqun Feng67c583a72015-12-29 12:18:47 +08001973 raw_spin_unlock_irq_rcu_node(rnp);
Paul E. McKenneybde6c3a2014-07-01 11:26:57 -07001974 cond_resched_rcu_qs();
Paul E. McKenney7d0ae802015-03-03 14:57:58 -08001975 WRITE_ONCE(rsp->gp_activity, jiffies);
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001976 }
1977
Paul E. McKenney45fed3e2015-11-07 23:35:00 -08001978 return true;
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001979}
1980
1981/*
Paul E. McKenneyb9a425c2015-07-01 13:50:28 -07001982 * Helper function for wait_event_interruptible_timeout() wakeup
1983 * at force-quiescent-state time.
1984 */
1985static bool rcu_gp_fqs_check_wake(struct rcu_state *rsp, int *gfp)
1986{
1987 struct rcu_node *rnp = rcu_get_root(rsp);
1988
1989 /* Someone like call_rcu() requested a force-quiescent-state scan. */
1990 *gfp = READ_ONCE(rsp->gp_flags);
1991 if (*gfp & RCU_GP_FLAG_FQS)
1992 return true;
1993
1994 /* The current grace period has completed. */
1995 if (!READ_ONCE(rnp->qsmask) && !rcu_preempt_blocked_readers_cgp(rnp))
1996 return true;
1997
1998 return false;
1999}
2000
2001/*
Paul E. McKenney4cdfc1752012-06-22 17:06:26 -07002002 * Do one round of quiescent-state forcing.
2003 */
Petr Mladek77f81fe2015-09-09 12:09:49 -07002004static void rcu_gp_fqs(struct rcu_state *rsp, bool first_time)
Paul E. McKenney4cdfc1752012-06-22 17:06:26 -07002005{
Paul E. McKenney217af2a2013-06-21 15:39:06 -07002006 bool isidle = false;
2007 unsigned long maxj;
Paul E. McKenney4cdfc1752012-06-22 17:06:26 -07002008 struct rcu_node *rnp = rcu_get_root(rsp);
2009
Paul E. McKenney7d0ae802015-03-03 14:57:58 -08002010 WRITE_ONCE(rsp->gp_activity, jiffies);
Paul E. McKenney4cdfc1752012-06-22 17:06:26 -07002011 rsp->n_force_qs++;
Petr Mladek77f81fe2015-09-09 12:09:49 -07002012 if (first_time) {
Paul E. McKenney4cdfc1752012-06-22 17:06:26 -07002013 /* Collect dyntick-idle snapshots. */
Paul E. McKenney0edd1b12013-06-21 16:37:22 -07002014 if (is_sysidle_rcu_state(rsp)) {
Pranith Kumare02b2ed2014-07-09 00:08:17 -04002015 isidle = true;
Paul E. McKenney0edd1b12013-06-21 16:37:22 -07002016 maxj = jiffies - ULONG_MAX / 4;
2017 }
Paul E. McKenney217af2a2013-06-21 15:39:06 -07002018 force_qs_rnp(rsp, dyntick_save_progress_counter,
2019 &isidle, &maxj);
Paul E. McKenney0edd1b12013-06-21 16:37:22 -07002020 rcu_sysidle_report_gp(rsp, isidle, maxj);
Paul E. McKenney4cdfc1752012-06-22 17:06:26 -07002021 } else {
2022 /* Handle dyntick-idle and offline CPUs. */
Paul E. McKenney675da672015-02-23 15:57:07 -08002023 isidle = true;
Paul E. McKenney217af2a2013-06-21 15:39:06 -07002024 force_qs_rnp(rsp, rcu_implicit_dynticks_qs, &isidle, &maxj);
Paul E. McKenney4cdfc1752012-06-22 17:06:26 -07002025 }
2026 /* Clear flag to prevent immediate re-entry. */
Paul E. McKenney7d0ae802015-03-03 14:57:58 -08002027 if (READ_ONCE(rsp->gp_flags) & RCU_GP_FLAG_FQS) {
Peter Zijlstra2a67e742015-10-08 12:24:23 +02002028 raw_spin_lock_irq_rcu_node(rnp);
Paul E. McKenney7d0ae802015-03-03 14:57:58 -08002029 WRITE_ONCE(rsp->gp_flags,
2030 READ_ONCE(rsp->gp_flags) & ~RCU_GP_FLAG_FQS);
Boqun Feng67c583a72015-12-29 12:18:47 +08002031 raw_spin_unlock_irq_rcu_node(rnp);
Paul E. McKenney4cdfc1752012-06-22 17:06:26 -07002032 }
Paul E. McKenney4cdfc1752012-06-22 17:06:26 -07002033}
2034
2035/*
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07002036 * Clean up after the old grace period.
2037 */
Paul E. McKenney4cdfc1752012-06-22 17:06:26 -07002038static void rcu_gp_cleanup(struct rcu_state *rsp)
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07002039{
2040 unsigned long gp_duration;
Paul E. McKenney48a76392014-03-11 13:02:16 -07002041 bool needgp = false;
Paul E. McKenneydae6e642013-02-10 20:48:58 -08002042 int nocb = 0;
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07002043 struct rcu_data *rdp;
2044 struct rcu_node *rnp = rcu_get_root(rsp);
Paul Gortmakerabedf8e2016-02-19 09:46:41 +01002045 struct swait_queue_head *sq;
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07002046
Paul E. McKenney7d0ae802015-03-03 14:57:58 -08002047 WRITE_ONCE(rsp->gp_activity, jiffies);
Peter Zijlstra2a67e742015-10-08 12:24:23 +02002048 raw_spin_lock_irq_rcu_node(rnp);
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07002049 gp_duration = jiffies - rsp->gp_start;
2050 if (gp_duration > rsp->gp_max)
2051 rsp->gp_max = gp_duration;
2052
2053 /*
2054 * We know the grace period is complete, but to everyone else
2055 * it appears to still be ongoing. But it is also the case
2056 * that to everyone else it looks like there is nothing that
2057 * they can do to advance the grace period. It is therefore
2058 * safe for us to drop the lock in order to mark the grace
2059 * period as completed in all of the rcu_node structures.
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07002060 */
Boqun Feng67c583a72015-12-29 12:18:47 +08002061 raw_spin_unlock_irq_rcu_node(rnp);
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07002062
Paul E. McKenney5d4b8652012-07-07 07:56:57 -07002063 /*
2064 * Propagate new ->completed value to rcu_node structures so
2065 * that other CPUs don't have to wait until the start of the next
2066 * grace period to process their callbacks. This also avoids
2067 * some nasty RCU grace-period initialization races by forcing
2068 * the end of the current grace period to be completely recorded in
2069 * all of the rcu_node structures before the beginning of the next
2070 * grace period is recorded in any of the rcu_node structures.
2071 */
2072 rcu_for_each_node_breadth_first(rsp, rnp) {
Peter Zijlstra2a67e742015-10-08 12:24:23 +02002073 raw_spin_lock_irq_rcu_node(rnp);
Paul E. McKenney5c60d252015-02-09 05:37:47 -08002074 WARN_ON_ONCE(rcu_preempt_blocked_readers_cgp(rnp));
2075 WARN_ON_ONCE(rnp->qsmask);
Paul E. McKenney7d0ae802015-03-03 14:57:58 -08002076 WRITE_ONCE(rnp->completed, rsp->gpnum);
Paul E. McKenneyb11cc572012-12-17 14:21:14 -08002077 rdp = this_cpu_ptr(rsp->rda);
2078 if (rnp == rdp->mynode)
Paul E. McKenney48a76392014-03-11 13:02:16 -07002079 needgp = __note_gp_changes(rsp, rnp, rdp) || needgp;
Paul E. McKenney78e4bc32013-09-24 15:04:06 -07002080 /* smp_mb() provided by prior unlock-lock pair. */
Paul E. McKenney0446be42012-12-30 15:21:01 -08002081 nocb += rcu_future_gp_cleanup(rsp, rnp);
Daniel Wagner065bb782016-02-19 09:46:40 +01002082 sq = rcu_nocb_gp_get(rnp);
Boqun Feng67c583a72015-12-29 12:18:47 +08002083 raw_spin_unlock_irq_rcu_node(rnp);
Daniel Wagner065bb782016-02-19 09:46:40 +01002084 rcu_nocb_gp_cleanup(sq);
Paul E. McKenneybde6c3a2014-07-01 11:26:57 -07002085 cond_resched_rcu_qs();
Paul E. McKenney7d0ae802015-03-03 14:57:58 -08002086 WRITE_ONCE(rsp->gp_activity, jiffies);
Paul E. McKenney0f41c0d2015-03-10 18:33:20 -07002087 rcu_gp_slow(rsp, gp_cleanup_delay);
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07002088 }
Paul E. McKenney5d4b8652012-07-07 07:56:57 -07002089 rnp = rcu_get_root(rsp);
Peter Zijlstra2a67e742015-10-08 12:24:23 +02002090 raw_spin_lock_irq_rcu_node(rnp); /* Order GP before ->completed update. */
Paul E. McKenneydae6e642013-02-10 20:48:58 -08002091 rcu_nocb_gp_set(rnp, nocb);
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07002092
Paul E. McKenney765a3f42014-03-14 16:37:08 -07002093 /* Declare grace period done. */
Paul E. McKenney7d0ae802015-03-03 14:57:58 -08002094 WRITE_ONCE(rsp->completed, rsp->gpnum);
Steven Rostedt (Red Hat)f7f7bac2013-07-12 17:18:47 -04002095 trace_rcu_grace_period(rsp->name, rsp->completed, TPS("end"));
Petr Mladek77f81fe2015-09-09 12:09:49 -07002096 rsp->gp_state = RCU_GP_IDLE;
Paul E. McKenney5d4b8652012-07-07 07:56:57 -07002097 rdp = this_cpu_ptr(rsp->rda);
Paul E. McKenney48a76392014-03-11 13:02:16 -07002098 /* Advance CBs to reduce false positives below. */
2099 needgp = rcu_advance_cbs(rsp, rnp, rdp) || needgp;
2100 if (needgp || cpu_needs_another_gp(rsp, rdp)) {
Paul E. McKenney7d0ae802015-03-03 14:57:58 -08002101 WRITE_ONCE(rsp->gp_flags, RCU_GP_FLAG_INIT);
Paul E. McKenneybb311ec2013-08-09 16:02:09 -07002102 trace_rcu_grace_period(rsp->name,
Paul E. McKenney7d0ae802015-03-03 14:57:58 -08002103 READ_ONCE(rsp->gpnum),
Paul E. McKenneybb311ec2013-08-09 16:02:09 -07002104 TPS("newreq"));
2105 }
Boqun Feng67c583a72015-12-29 12:18:47 +08002106 raw_spin_unlock_irq_rcu_node(rnp);
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07002107}
2108
2109/*
Paul E. McKenneyb3dbec72012-06-18 18:36:08 -07002110 * Body of kthread that handles grace periods.
2111 */
Paul E. McKenney755609a2012-06-19 17:18:20 -07002112static int __noreturn rcu_gp_kthread(void *arg)
Paul E. McKenneyb3dbec72012-06-18 18:36:08 -07002113{
Petr Mladek77f81fe2015-09-09 12:09:49 -07002114 bool first_gp_fqs;
Paul E. McKenney88d6df62013-08-08 21:44:31 -07002115 int gf;
Paul E. McKenneyd40011f2012-06-26 20:45:57 -07002116 unsigned long j;
Paul E. McKenney4cdfc1752012-06-22 17:06:26 -07002117 int ret;
Paul E. McKenneyb3dbec72012-06-18 18:36:08 -07002118 struct rcu_state *rsp = arg;
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07002119 struct rcu_node *rnp = rcu_get_root(rsp);
Paul E. McKenneyb3dbec72012-06-18 18:36:08 -07002120
Paul E. McKenney58719682015-02-24 11:05:36 -08002121 rcu_bind_gp_kthread();
Paul E. McKenneyb3dbec72012-06-18 18:36:08 -07002122 for (;;) {
2123
2124 /* Handle grace-period start. */
Paul E. McKenneyb3dbec72012-06-18 18:36:08 -07002125 for (;;) {
Paul E. McKenney63c4db72013-08-09 12:19:29 -07002126 trace_rcu_grace_period(rsp->name,
Paul E. McKenney7d0ae802015-03-03 14:57:58 -08002127 READ_ONCE(rsp->gpnum),
Paul E. McKenney63c4db72013-08-09 12:19:29 -07002128 TPS("reqwait"));
Paul E. McKenneyafea2272014-03-12 07:10:41 -07002129 rsp->gp_state = RCU_GP_WAIT_GPS;
Paul Gortmakerabedf8e2016-02-19 09:46:41 +01002130 swait_event_interruptible(rsp->gp_wq,
Paul E. McKenney7d0ae802015-03-03 14:57:58 -08002131 READ_ONCE(rsp->gp_flags) &
Paul E. McKenney4cdfc1752012-06-22 17:06:26 -07002132 RCU_GP_FLAG_INIT);
Paul E. McKenney319362c2015-05-19 14:16:52 -07002133 rsp->gp_state = RCU_GP_DONE_GPS;
Paul E. McKenney78e4bc32013-09-24 15:04:06 -07002134 /* Locking provides needed memory barrier. */
Paul E. McKenneyf7be8202013-08-08 18:27:52 -07002135 if (rcu_gp_init(rsp))
Paul E. McKenneyb3dbec72012-06-18 18:36:08 -07002136 break;
Paul E. McKenneybde6c3a2014-07-01 11:26:57 -07002137 cond_resched_rcu_qs();
Paul E. McKenney7d0ae802015-03-03 14:57:58 -08002138 WRITE_ONCE(rsp->gp_activity, jiffies);
Paul E. McKenney73a860c2014-08-14 10:28:23 -07002139 WARN_ON(signal_pending(current));
Paul E. McKenney63c4db72013-08-09 12:19:29 -07002140 trace_rcu_grace_period(rsp->name,
Paul E. McKenney7d0ae802015-03-03 14:57:58 -08002141 READ_ONCE(rsp->gpnum),
Paul E. McKenney63c4db72013-08-09 12:19:29 -07002142 TPS("reqwaitsig"));
Paul E. McKenneyb3dbec72012-06-18 18:36:08 -07002143 }
Paul E. McKenneycabc49c2012-06-20 17:07:14 -07002144
Paul E. McKenney4cdfc1752012-06-22 17:06:26 -07002145 /* Handle quiescent-state forcing. */
Petr Mladek77f81fe2015-09-09 12:09:49 -07002146 first_gp_fqs = true;
Paul E. McKenneyd40011f2012-06-26 20:45:57 -07002147 j = jiffies_till_first_fqs;
2148 if (j > HZ) {
2149 j = HZ;
2150 jiffies_till_first_fqs = HZ;
2151 }
Paul E. McKenney88d6df62013-08-08 21:44:31 -07002152 ret = 0;
Paul E. McKenneycabc49c2012-06-20 17:07:14 -07002153 for (;;) {
Paul E. McKenney8c7c4822016-01-03 20:29:57 -08002154 if (!ret) {
Paul E. McKenney88d6df62013-08-08 21:44:31 -07002155 rsp->jiffies_force_qs = jiffies + j;
Paul E. McKenney8c7c4822016-01-03 20:29:57 -08002156 WRITE_ONCE(rsp->jiffies_kick_kthreads,
2157 jiffies + 3 * j);
2158 }
Paul E. McKenney63c4db72013-08-09 12:19:29 -07002159 trace_rcu_grace_period(rsp->name,
Paul E. McKenney7d0ae802015-03-03 14:57:58 -08002160 READ_ONCE(rsp->gpnum),
Paul E. McKenney63c4db72013-08-09 12:19:29 -07002161 TPS("fqswait"));
Paul E. McKenneyafea2272014-03-12 07:10:41 -07002162 rsp->gp_state = RCU_GP_WAIT_FQS;
Paul Gortmakerabedf8e2016-02-19 09:46:41 +01002163 ret = swait_event_interruptible_timeout(rsp->gp_wq,
Paul E. McKenneyb9a425c2015-07-01 13:50:28 -07002164 rcu_gp_fqs_check_wake(rsp, &gf), j);
Paul E. McKenney32bb1c72015-07-02 12:27:31 -07002165 rsp->gp_state = RCU_GP_DOING_FQS;
Paul E. McKenney78e4bc32013-09-24 15:04:06 -07002166 /* Locking provides needed memory barriers. */
Paul E. McKenney4cdfc1752012-06-22 17:06:26 -07002167 /* If grace period done, leave loop. */
Paul E. McKenney7d0ae802015-03-03 14:57:58 -08002168 if (!READ_ONCE(rnp->qsmask) &&
Paul E. McKenney4cdfc1752012-06-22 17:06:26 -07002169 !rcu_preempt_blocked_readers_cgp(rnp))
Paul E. McKenneycabc49c2012-06-20 17:07:14 -07002170 break;
Paul E. McKenney4cdfc1752012-06-22 17:06:26 -07002171 /* If time for quiescent-state forcing, do it. */
Paul E. McKenney88d6df62013-08-08 21:44:31 -07002172 if (ULONG_CMP_GE(jiffies, rsp->jiffies_force_qs) ||
2173 (gf & RCU_GP_FLAG_FQS)) {
Paul E. McKenney63c4db72013-08-09 12:19:29 -07002174 trace_rcu_grace_period(rsp->name,
Paul E. McKenney7d0ae802015-03-03 14:57:58 -08002175 READ_ONCE(rsp->gpnum),
Paul E. McKenney63c4db72013-08-09 12:19:29 -07002176 TPS("fqsstart"));
Petr Mladek77f81fe2015-09-09 12:09:49 -07002177 rcu_gp_fqs(rsp, first_gp_fqs);
2178 first_gp_fqs = false;
Paul E. McKenney63c4db72013-08-09 12:19:29 -07002179 trace_rcu_grace_period(rsp->name,
Paul E. McKenney7d0ae802015-03-03 14:57:58 -08002180 READ_ONCE(rsp->gpnum),
Paul E. McKenney63c4db72013-08-09 12:19:29 -07002181 TPS("fqsend"));
Paul E. McKenneybde6c3a2014-07-01 11:26:57 -07002182 cond_resched_rcu_qs();
Paul E. McKenney7d0ae802015-03-03 14:57:58 -08002183 WRITE_ONCE(rsp->gp_activity, jiffies);
Paul E. McKenneyfcfd0a22016-01-03 16:42:18 -08002184 ret = 0; /* Force full wait till next FQS. */
2185 j = jiffies_till_next_fqs;
2186 if (j > HZ) {
2187 j = HZ;
2188 jiffies_till_next_fqs = HZ;
2189 } else if (j < 1) {
2190 j = 1;
2191 jiffies_till_next_fqs = 1;
2192 }
Paul E. McKenney4cdfc1752012-06-22 17:06:26 -07002193 } else {
2194 /* Deal with stray signal. */
Paul E. McKenneybde6c3a2014-07-01 11:26:57 -07002195 cond_resched_rcu_qs();
Paul E. McKenney7d0ae802015-03-03 14:57:58 -08002196 WRITE_ONCE(rsp->gp_activity, jiffies);
Paul E. McKenney73a860c2014-08-14 10:28:23 -07002197 WARN_ON(signal_pending(current));
Paul E. McKenney63c4db72013-08-09 12:19:29 -07002198 trace_rcu_grace_period(rsp->name,
Paul E. McKenney7d0ae802015-03-03 14:57:58 -08002199 READ_ONCE(rsp->gpnum),
Paul E. McKenney63c4db72013-08-09 12:19:29 -07002200 TPS("fqswaitsig"));
Paul E. McKenneyfcfd0a22016-01-03 16:42:18 -08002201 ret = 1; /* Keep old FQS timing. */
2202 j = jiffies;
2203 if (time_after(jiffies, rsp->jiffies_force_qs))
2204 j = 1;
2205 else
2206 j = rsp->jiffies_force_qs - j;
Paul E. McKenneyd40011f2012-06-26 20:45:57 -07002207 }
Paul E. McKenneycabc49c2012-06-20 17:07:14 -07002208 }
Paul E. McKenney4cdfc1752012-06-22 17:06:26 -07002209
2210 /* Handle grace-period end. */
Paul E. McKenney319362c2015-05-19 14:16:52 -07002211 rsp->gp_state = RCU_GP_CLEANUP;
Paul E. McKenney4cdfc1752012-06-22 17:06:26 -07002212 rcu_gp_cleanup(rsp);
Paul E. McKenney319362c2015-05-19 14:16:52 -07002213 rsp->gp_state = RCU_GP_CLEANED;
Paul E. McKenneyb3dbec72012-06-18 18:36:08 -07002214 }
Paul E. McKenneyb3dbec72012-06-18 18:36:08 -07002215}
2216
2217/*
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002218 * Start a new RCU grace period if warranted, re-initializing the hierarchy
2219 * in preparation for detecting the next grace period. The caller must hold
Paul E. McKenneyb8462082012-12-29 22:04:18 -08002220 * the root node's ->lock and hard irqs must be disabled.
Paul E. McKenneye5601402012-01-07 11:03:57 -08002221 *
2222 * Note that it is legal for a dying CPU (which is marked as offline) to
2223 * invoke this function. This can happen when the dying CPU reports its
2224 * quiescent state.
Paul E. McKenney48a76392014-03-11 13:02:16 -07002225 *
2226 * Returns true if the grace-period kthread must be awakened.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002227 */
Paul E. McKenney48a76392014-03-11 13:02:16 -07002228static bool
Paul E. McKenney910ee452012-12-31 02:24:21 -08002229rcu_start_gp_advanced(struct rcu_state *rsp, struct rcu_node *rnp,
2230 struct rcu_data *rdp)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002231{
Paul E. McKenneyb8462082012-12-29 22:04:18 -08002232 if (!rsp->gp_kthread || !cpu_needs_another_gp(rsp, rdp)) {
Paul E. McKenneyb32e9eb2009-11-12 22:35:03 -08002233 /*
Paul E. McKenneyb3dbec72012-06-18 18:36:08 -07002234 * Either we have not yet spawned the grace-period
Paul E. McKenney62da1922012-09-20 16:02:49 -07002235 * task, this CPU does not need another grace period,
2236 * or a grace period is already in progress.
Paul E. McKenneyb3dbec72012-06-18 18:36:08 -07002237 * Either way, don't start a new grace period.
Paul E. McKenneyb32e9eb2009-11-12 22:35:03 -08002238 */
Paul E. McKenney48a76392014-03-11 13:02:16 -07002239 return false;
Paul E. McKenneyafe24b12011-08-24 16:52:09 -07002240 }
Paul E. McKenney7d0ae802015-03-03 14:57:58 -08002241 WRITE_ONCE(rsp->gp_flags, RCU_GP_FLAG_INIT);
2242 trace_rcu_grace_period(rsp->name, READ_ONCE(rsp->gpnum),
Paul E. McKenneybb311ec2013-08-09 16:02:09 -07002243 TPS("newreq"));
Paul E. McKenney62da1922012-09-20 16:02:49 -07002244
Steven Rostedt016a8d52013-05-28 17:32:53 -04002245 /*
2246 * We can't do wakeups while holding the rnp->lock, as that
Paul E. McKenney1eafd312013-06-20 13:50:40 -07002247 * could cause possible deadlocks with the rq->lock. Defer
Paul E. McKenney48a76392014-03-11 13:02:16 -07002248 * the wakeup to our caller.
Steven Rostedt016a8d52013-05-28 17:32:53 -04002249 */
Paul E. McKenney48a76392014-03-11 13:02:16 -07002250 return true;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002251}
2252
2253/*
Paul E. McKenney910ee452012-12-31 02:24:21 -08002254 * Similar to rcu_start_gp_advanced(), but also advance the calling CPU's
2255 * callbacks. Note that rcu_start_gp_advanced() cannot do this because it
2256 * is invoked indirectly from rcu_advance_cbs(), which would result in
2257 * endless recursion -- or would do so if it wasn't for the self-deadlock
2258 * that is encountered beforehand.
Paul E. McKenney48a76392014-03-11 13:02:16 -07002259 *
2260 * Returns true if the grace-period kthread needs to be awakened.
Paul E. McKenney910ee452012-12-31 02:24:21 -08002261 */
Paul E. McKenney48a76392014-03-11 13:02:16 -07002262static bool rcu_start_gp(struct rcu_state *rsp)
Paul E. McKenney910ee452012-12-31 02:24:21 -08002263{
2264 struct rcu_data *rdp = this_cpu_ptr(rsp->rda);
2265 struct rcu_node *rnp = rcu_get_root(rsp);
Paul E. McKenney48a76392014-03-11 13:02:16 -07002266 bool ret = false;
Paul E. McKenney910ee452012-12-31 02:24:21 -08002267
2268 /*
2269 * If there is no grace period in progress right now, any
2270 * callbacks we have up to this point will be satisfied by the
2271 * next grace period. Also, advancing the callbacks reduces the
2272 * probability of false positives from cpu_needs_another_gp()
2273 * resulting in pointless grace periods. So, advance callbacks
2274 * then start the grace period!
2275 */
Paul E. McKenney48a76392014-03-11 13:02:16 -07002276 ret = rcu_advance_cbs(rsp, rnp, rdp) || ret;
2277 ret = rcu_start_gp_advanced(rsp, rnp, rdp) || ret;
2278 return ret;
Paul E. McKenney910ee452012-12-31 02:24:21 -08002279}
2280
2281/*
Paul E. McKenney89945152015-12-10 09:59:43 -08002282 * Report a full set of quiescent states to the specified rcu_state data
2283 * structure. Invoke rcu_gp_kthread_wake() to awaken the grace-period
2284 * kthread if another grace period is required. Whether we wake
2285 * the grace-period kthread or it awakens itself for the next round
2286 * of quiescent-state forcing, that kthread will clean up after the
2287 * just-completed grace period. Note that the caller must hold rnp->lock,
2288 * which is released before return.
Paul E. McKenneyf41d9112009-08-22 13:56:52 -07002289 */
Paul E. McKenneyd3f6bad2009-12-02 12:10:13 -08002290static void rcu_report_qs_rsp(struct rcu_state *rsp, unsigned long flags)
Paul E. McKenneyfc2219d42009-09-23 09:50:41 -07002291 __releases(rcu_get_root(rsp)->lock)
Paul E. McKenneyf41d9112009-08-22 13:56:52 -07002292{
Paul E. McKenneyfc2219d42009-09-23 09:50:41 -07002293 WARN_ON_ONCE(!rcu_gp_in_progress(rsp));
Paul E. McKenneycd73ca22015-03-16 11:53:52 -07002294 WRITE_ONCE(rsp->gp_flags, READ_ONCE(rsp->gp_flags) | RCU_GP_FLAG_FQS);
Boqun Feng67c583a72015-12-29 12:18:47 +08002295 raw_spin_unlock_irqrestore_rcu_node(rcu_get_root(rsp), flags);
Paul Gortmakerabedf8e2016-02-19 09:46:41 +01002296 swake_up(&rsp->gp_wq); /* Memory barrier implied by swake_up() path. */
Paul E. McKenneyf41d9112009-08-22 13:56:52 -07002297}
2298
2299/*
Paul E. McKenneyd3f6bad2009-12-02 12:10:13 -08002300 * Similar to rcu_report_qs_rdp(), for which it is a helper function.
2301 * Allows quiescent states for a group of CPUs to be reported at one go
2302 * to the specified rcu_node structure, though all the CPUs in the group
Paul E. McKenney654e9532015-03-15 09:19:35 -07002303 * must be represented by the same rcu_node structure (which need not be a
2304 * leaf rcu_node structure, though it often will be). The gps parameter
2305 * is the grace-period snapshot, which means that the quiescent states
2306 * are valid only if rnp->gpnum is equal to gps. That structure's lock
2307 * must be held upon entry, and it is released before return.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002308 */
2309static void
Paul E. McKenneyd3f6bad2009-12-02 12:10:13 -08002310rcu_report_qs_rnp(unsigned long mask, struct rcu_state *rsp,
Paul E. McKenney654e9532015-03-15 09:19:35 -07002311 struct rcu_node *rnp, unsigned long gps, unsigned long flags)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002312 __releases(rnp->lock)
2313{
Paul E. McKenney654e9532015-03-15 09:19:35 -07002314 unsigned long oldmask = 0;
Paul E. McKenney28ecd582009-09-18 09:50:17 -07002315 struct rcu_node *rnp_c;
2316
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002317 /* Walk up the rcu_node hierarchy. */
2318 for (;;) {
Paul E. McKenney654e9532015-03-15 09:19:35 -07002319 if (!(rnp->qsmask & mask) || rnp->gpnum != gps) {
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002320
Paul E. McKenney654e9532015-03-15 09:19:35 -07002321 /*
2322 * Our bit has already been cleared, or the
2323 * relevant grace period is already over, so done.
2324 */
Boqun Feng67c583a72015-12-29 12:18:47 +08002325 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002326 return;
2327 }
Paul E. McKenney654e9532015-03-15 09:19:35 -07002328 WARN_ON_ONCE(oldmask); /* Any child must be all zeroed! */
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002329 rnp->qsmask &= ~mask;
Paul E. McKenneyd4c08f22011-06-25 06:36:56 -07002330 trace_rcu_quiescent_state_report(rsp->name, rnp->gpnum,
2331 mask, rnp->qsmask, rnp->level,
2332 rnp->grplo, rnp->grphi,
2333 !!rnp->gp_tasks);
Paul E. McKenney27f4d282011-02-07 12:47:15 -08002334 if (rnp->qsmask != 0 || rcu_preempt_blocked_readers_cgp(rnp)) {
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002335
2336 /* Other bits still set at this level, so done. */
Boqun Feng67c583a72015-12-29 12:18:47 +08002337 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002338 return;
2339 }
2340 mask = rnp->grpmask;
2341 if (rnp->parent == NULL) {
2342
2343 /* No more levels. Exit loop holding root lock. */
2344
2345 break;
2346 }
Boqun Feng67c583a72015-12-29 12:18:47 +08002347 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
Paul E. McKenney28ecd582009-09-18 09:50:17 -07002348 rnp_c = rnp;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002349 rnp = rnp->parent;
Peter Zijlstra2a67e742015-10-08 12:24:23 +02002350 raw_spin_lock_irqsave_rcu_node(rnp, flags);
Paul E. McKenney654e9532015-03-15 09:19:35 -07002351 oldmask = rnp_c->qsmask;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002352 }
2353
2354 /*
2355 * Get here if we are the last CPU to pass through a quiescent
Paul E. McKenneyd3f6bad2009-12-02 12:10:13 -08002356 * state for this grace period. Invoke rcu_report_qs_rsp()
Paul E. McKenneyf41d9112009-08-22 13:56:52 -07002357 * to clean up and start the next grace period if one is needed.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002358 */
Paul E. McKenneyd3f6bad2009-12-02 12:10:13 -08002359 rcu_report_qs_rsp(rsp, flags); /* releases rnp->lock. */
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002360}
2361
2362/*
Paul E. McKenneycc99a312015-02-23 08:59:29 -08002363 * Record a quiescent state for all tasks that were previously queued
2364 * on the specified rcu_node structure and that were blocking the current
2365 * RCU grace period. The caller must hold the specified rnp->lock with
2366 * irqs disabled, and this lock is released upon return, but irqs remain
2367 * disabled.
2368 */
Paul E. McKenney0aa04b02015-01-23 21:52:37 -08002369static void rcu_report_unblock_qs_rnp(struct rcu_state *rsp,
Paul E. McKenneycc99a312015-02-23 08:59:29 -08002370 struct rcu_node *rnp, unsigned long flags)
2371 __releases(rnp->lock)
2372{
Paul E. McKenney654e9532015-03-15 09:19:35 -07002373 unsigned long gps;
Paul E. McKenneycc99a312015-02-23 08:59:29 -08002374 unsigned long mask;
2375 struct rcu_node *rnp_p;
2376
Paul E. McKenneya77da142015-03-08 14:52:27 -07002377 if (rcu_state_p == &rcu_sched_state || rsp != rcu_state_p ||
2378 rnp->qsmask != 0 || rcu_preempt_blocked_readers_cgp(rnp)) {
Boqun Feng67c583a72015-12-29 12:18:47 +08002379 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
Paul E. McKenneycc99a312015-02-23 08:59:29 -08002380 return; /* Still need more quiescent states! */
2381 }
2382
2383 rnp_p = rnp->parent;
2384 if (rnp_p == NULL) {
2385 /*
Paul E. McKenneya77da142015-03-08 14:52:27 -07002386 * Only one rcu_node structure in the tree, so don't
2387 * try to report up to its nonexistent parent!
Paul E. McKenneycc99a312015-02-23 08:59:29 -08002388 */
2389 rcu_report_qs_rsp(rsp, flags);
2390 return;
2391 }
2392
Paul E. McKenney654e9532015-03-15 09:19:35 -07002393 /* Report up the rest of the hierarchy, tracking current ->gpnum. */
2394 gps = rnp->gpnum;
Paul E. McKenneycc99a312015-02-23 08:59:29 -08002395 mask = rnp->grpmask;
Boqun Feng67c583a72015-12-29 12:18:47 +08002396 raw_spin_unlock_rcu_node(rnp); /* irqs remain disabled. */
Peter Zijlstra2a67e742015-10-08 12:24:23 +02002397 raw_spin_lock_rcu_node(rnp_p); /* irqs already disabled. */
Paul E. McKenney654e9532015-03-15 09:19:35 -07002398 rcu_report_qs_rnp(mask, rsp, rnp_p, gps, flags);
Paul E. McKenneycc99a312015-02-23 08:59:29 -08002399}
2400
2401/*
Paul E. McKenneyd3f6bad2009-12-02 12:10:13 -08002402 * Record a quiescent state for the specified CPU to that CPU's rcu_data
Paul E. McKenney4b455dc2016-01-27 22:44:45 -08002403 * structure. This must be called from the specified CPU.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002404 */
2405static void
Paul E. McKenneyd7d6a112012-08-21 15:00:05 -07002406rcu_report_qs_rdp(int cpu, struct rcu_state *rsp, struct rcu_data *rdp)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002407{
2408 unsigned long flags;
2409 unsigned long mask;
Paul E. McKenney48a76392014-03-11 13:02:16 -07002410 bool needwake;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002411 struct rcu_node *rnp;
2412
2413 rnp = rdp->mynode;
Peter Zijlstra2a67e742015-10-08 12:24:23 +02002414 raw_spin_lock_irqsave_rcu_node(rnp, flags);
Paul E. McKenney5b74c452015-08-06 15:16:57 -07002415 if ((rdp->cpu_no_qs.b.norm &&
Paul E. McKenney5cd37192014-12-13 20:32:04 -08002416 rdp->rcu_qs_ctr_snap == __this_cpu_read(rcu_qs_ctr)) ||
2417 rdp->gpnum != rnp->gpnum || rnp->completed == rnp->gpnum ||
2418 rdp->gpwrap) {
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002419
2420 /*
Paul E. McKenneye4cc1f22011-06-27 00:17:43 -07002421 * The grace period in which this quiescent state was
2422 * recorded has ended, so don't report it upwards.
2423 * We will instead need a new quiescent state that lies
2424 * within the current grace period.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002425 */
Paul E. McKenney5b74c452015-08-06 15:16:57 -07002426 rdp->cpu_no_qs.b.norm = true; /* need qs for new gp. */
Paul E. McKenney5cd37192014-12-13 20:32:04 -08002427 rdp->rcu_qs_ctr_snap = __this_cpu_read(rcu_qs_ctr);
Boqun Feng67c583a72015-12-29 12:18:47 +08002428 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002429 return;
2430 }
2431 mask = rdp->grpmask;
2432 if ((rnp->qsmask & mask) == 0) {
Boqun Feng67c583a72015-12-29 12:18:47 +08002433 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002434 } else {
Paul E. McKenneybb53e412015-12-10 09:30:12 -08002435 rdp->core_needs_qs = false;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002436
2437 /*
2438 * This GP can't end until cpu checks in, so all of our
2439 * callbacks can be processed during the next GP.
2440 */
Paul E. McKenney48a76392014-03-11 13:02:16 -07002441 needwake = rcu_accelerate_cbs(rsp, rnp, rdp);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002442
Paul E. McKenney654e9532015-03-15 09:19:35 -07002443 rcu_report_qs_rnp(mask, rsp, rnp, rnp->gpnum, flags);
2444 /* ^^^ Released rnp->lock */
Paul E. McKenney48a76392014-03-11 13:02:16 -07002445 if (needwake)
2446 rcu_gp_kthread_wake(rsp);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002447 }
2448}
2449
2450/*
2451 * Check to see if there is a new grace period of which this CPU
2452 * is not yet aware, and if so, set up local rcu_data state for it.
2453 * Otherwise, see if this CPU has just passed through its first
2454 * quiescent state for this grace period, and record that fact if so.
2455 */
2456static void
2457rcu_check_quiescent_state(struct rcu_state *rsp, struct rcu_data *rdp)
2458{
Paul E. McKenney05eb5522013-03-19 12:38:24 -07002459 /* Check for grace-period ends and beginnings. */
2460 note_gp_changes(rsp, rdp);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002461
2462 /*
2463 * Does this CPU still need to do its part for current grace period?
2464 * If no, return and let the other CPUs do their part as well.
2465 */
Paul E. McKenney97c668b2015-08-06 11:31:51 -07002466 if (!rdp->core_needs_qs)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002467 return;
2468
2469 /*
2470 * Was there a quiescent state since the beginning of the grace
2471 * period? If no, then exit and wait for the next call.
2472 */
Paul E. McKenney5b74c452015-08-06 15:16:57 -07002473 if (rdp->cpu_no_qs.b.norm &&
Paul E. McKenney5cd37192014-12-13 20:32:04 -08002474 rdp->rcu_qs_ctr_snap == __this_cpu_read(rcu_qs_ctr))
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002475 return;
2476
Paul E. McKenneyd3f6bad2009-12-02 12:10:13 -08002477 /*
2478 * Tell RCU we are done (but rcu_report_qs_rdp() will be the
2479 * judge of that).
2480 */
Paul E. McKenneyd7d6a112012-08-21 15:00:05 -07002481 rcu_report_qs_rdp(rdp->cpu, rsp, rdp);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002482}
2483
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002484/*
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08002485 * Send the specified CPU's RCU callbacks to the orphanage. The
2486 * specified CPU must be offline, and the caller must hold the
Paul E. McKenney7b2e6012012-10-08 10:54:03 -07002487 * ->orphan_lock.
Paul E. McKenneye74f4c42009-10-06 21:48:17 -07002488 */
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08002489static void
2490rcu_send_cbs_to_orphanage(int cpu, struct rcu_state *rsp,
2491 struct rcu_node *rnp, struct rcu_data *rdp)
Paul E. McKenneye74f4c42009-10-06 21:48:17 -07002492{
Paul E. McKenney3fbfbf72012-08-19 21:35:53 -07002493 /* No-CBs CPUs do not have orphanable callbacks. */
Paul E. McKenneyea463512015-03-03 14:05:26 -08002494 if (!IS_ENABLED(CONFIG_HOTPLUG_CPU) || rcu_is_nocb_cpu(rdp->cpu))
Paul E. McKenney3fbfbf72012-08-19 21:35:53 -07002495 return;
2496
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08002497 /*
2498 * Orphan the callbacks. First adjust the counts. This is safe
Paul E. McKenneyabfd6e52012-09-20 16:59:47 -07002499 * because _rcu_barrier() excludes CPU-hotplug operations, so it
2500 * cannot be running now. Thus no memory barrier is required.
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08002501 */
Paul E. McKenneya50c3af2012-01-10 17:52:31 -08002502 if (rdp->nxtlist != NULL) {
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08002503 rsp->qlen_lazy += rdp->qlen_lazy;
2504 rsp->qlen += rdp->qlen;
2505 rdp->n_cbs_orphaned += rdp->qlen;
Paul E. McKenneya50c3af2012-01-10 17:52:31 -08002506 rdp->qlen_lazy = 0;
Paul E. McKenney7d0ae802015-03-03 14:57:58 -08002507 WRITE_ONCE(rdp->qlen, 0);
Paul E. McKenneya50c3af2012-01-10 17:52:31 -08002508 }
2509
2510 /*
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08002511 * Next, move those callbacks still needing a grace period to
2512 * the orphanage, where some other CPU will pick them up.
2513 * Some of the callbacks might have gone partway through a grace
2514 * period, but that is too bad. They get to start over because we
2515 * cannot assume that grace periods are synchronized across CPUs.
2516 * We don't bother updating the ->nxttail[] array yet, instead
2517 * we just reset the whole thing later on.
Paul E. McKenneya50c3af2012-01-10 17:52:31 -08002518 */
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08002519 if (*rdp->nxttail[RCU_DONE_TAIL] != NULL) {
2520 *rsp->orphan_nxttail = *rdp->nxttail[RCU_DONE_TAIL];
2521 rsp->orphan_nxttail = rdp->nxttail[RCU_NEXT_TAIL];
2522 *rdp->nxttail[RCU_DONE_TAIL] = NULL;
Paul E. McKenneya50c3af2012-01-10 17:52:31 -08002523 }
2524
2525 /*
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08002526 * Then move the ready-to-invoke callbacks to the orphanage,
2527 * where some other CPU will pick them up. These will not be
2528 * required to pass though another grace period: They are done.
Paul E. McKenneya50c3af2012-01-10 17:52:31 -08002529 */
Paul E. McKenneye5601402012-01-07 11:03:57 -08002530 if (rdp->nxtlist != NULL) {
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08002531 *rsp->orphan_donetail = rdp->nxtlist;
2532 rsp->orphan_donetail = rdp->nxttail[RCU_DONE_TAIL];
Paul E. McKenneye5601402012-01-07 11:03:57 -08002533 }
Lai Jiangshan29494be2010-10-20 14:13:06 +08002534
Paul E. McKenneyb33078b2015-01-16 14:01:21 -08002535 /*
2536 * Finally, initialize the rcu_data structure's list to empty and
2537 * disallow further callbacks on this CPU.
2538 */
Paul E. McKenney3f5d3ea2012-05-09 15:39:56 -07002539 init_callback_list(rdp);
Paul E. McKenneyb33078b2015-01-16 14:01:21 -08002540 rdp->nxttail[RCU_NEXT_TAIL] = NULL;
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08002541}
2542
2543/*
2544 * Adopt the RCU callbacks from the specified rcu_state structure's
Paul E. McKenney7b2e6012012-10-08 10:54:03 -07002545 * orphanage. The caller must hold the ->orphan_lock.
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08002546 */
Paul E. McKenney96d3fd02013-10-04 14:33:34 -07002547static void rcu_adopt_orphan_cbs(struct rcu_state *rsp, unsigned long flags)
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08002548{
2549 int i;
Christoph Lameterfa07a582014-04-15 12:20:12 -07002550 struct rcu_data *rdp = raw_cpu_ptr(rsp->rda);
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08002551
Paul E. McKenney3fbfbf72012-08-19 21:35:53 -07002552 /* No-CBs CPUs are handled specially. */
Paul E. McKenneyea463512015-03-03 14:05:26 -08002553 if (!IS_ENABLED(CONFIG_HOTPLUG_CPU) ||
2554 rcu_nocb_adopt_orphan_cbs(rsp, rdp, flags))
Paul E. McKenney3fbfbf72012-08-19 21:35:53 -07002555 return;
2556
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08002557 /* Do the accounting first. */
2558 rdp->qlen_lazy += rsp->qlen_lazy;
2559 rdp->qlen += rsp->qlen;
2560 rdp->n_cbs_adopted += rsp->qlen;
Paul E. McKenney8f5af6f2012-05-04 08:31:53 -07002561 if (rsp->qlen_lazy != rsp->qlen)
2562 rcu_idle_count_callbacks_posted();
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08002563 rsp->qlen_lazy = 0;
2564 rsp->qlen = 0;
2565
2566 /*
2567 * We do not need a memory barrier here because the only way we
2568 * can get here if there is an rcu_barrier() in flight is if
2569 * we are the task doing the rcu_barrier().
2570 */
2571
2572 /* First adopt the ready-to-invoke callbacks. */
2573 if (rsp->orphan_donelist != NULL) {
2574 *rsp->orphan_donetail = *rdp->nxttail[RCU_DONE_TAIL];
2575 *rdp->nxttail[RCU_DONE_TAIL] = rsp->orphan_donelist;
2576 for (i = RCU_NEXT_SIZE - 1; i >= RCU_DONE_TAIL; i--)
2577 if (rdp->nxttail[i] == rdp->nxttail[RCU_DONE_TAIL])
2578 rdp->nxttail[i] = rsp->orphan_donetail;
2579 rsp->orphan_donelist = NULL;
2580 rsp->orphan_donetail = &rsp->orphan_donelist;
2581 }
2582
2583 /* And then adopt the callbacks that still need a grace period. */
2584 if (rsp->orphan_nxtlist != NULL) {
2585 *rdp->nxttail[RCU_NEXT_TAIL] = rsp->orphan_nxtlist;
2586 rdp->nxttail[RCU_NEXT_TAIL] = rsp->orphan_nxttail;
2587 rsp->orphan_nxtlist = NULL;
2588 rsp->orphan_nxttail = &rsp->orphan_nxtlist;
2589 }
2590}
2591
2592/*
2593 * Trace the fact that this CPU is going offline.
2594 */
2595static void rcu_cleanup_dying_cpu(struct rcu_state *rsp)
2596{
2597 RCU_TRACE(unsigned long mask);
2598 RCU_TRACE(struct rcu_data *rdp = this_cpu_ptr(rsp->rda));
2599 RCU_TRACE(struct rcu_node *rnp = rdp->mynode);
2600
Paul E. McKenneyea463512015-03-03 14:05:26 -08002601 if (!IS_ENABLED(CONFIG_HOTPLUG_CPU))
2602 return;
2603
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08002604 RCU_TRACE(mask = rdp->grpmask);
Paul E. McKenneye5601402012-01-07 11:03:57 -08002605 trace_rcu_grace_period(rsp->name,
2606 rnp->gpnum + 1 - !!(rnp->qsmask & mask),
Steven Rostedt (Red Hat)f7f7bac2013-07-12 17:18:47 -04002607 TPS("cpuofl"));
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002608}
2609
2610/*
Paul E. McKenney8af3a5e2014-10-31 11:22:37 -07002611 * All CPUs for the specified rcu_node structure have gone offline,
2612 * and all tasks that were preempted within an RCU read-side critical
2613 * section while running on one of those CPUs have since exited their RCU
2614 * read-side critical section. Some other CPU is reporting this fact with
2615 * the specified rcu_node structure's ->lock held and interrupts disabled.
2616 * This function therefore goes up the tree of rcu_node structures,
2617 * clearing the corresponding bits in the ->qsmaskinit fields. Note that
2618 * the leaf rcu_node structure's ->qsmaskinit field has already been
2619 * updated
2620 *
2621 * This function does check that the specified rcu_node structure has
2622 * all CPUs offline and no blocked tasks, so it is OK to invoke it
2623 * prematurely. That said, invoking it after the fact will cost you
2624 * a needless lock acquisition. So once it has done its work, don't
2625 * invoke it again.
2626 */
2627static void rcu_cleanup_dead_rnp(struct rcu_node *rnp_leaf)
2628{
2629 long mask;
2630 struct rcu_node *rnp = rnp_leaf;
2631
Paul E. McKenneyea463512015-03-03 14:05:26 -08002632 if (!IS_ENABLED(CONFIG_HOTPLUG_CPU) ||
2633 rnp->qsmaskinit || rcu_preempt_has_tasks(rnp))
Paul E. McKenney8af3a5e2014-10-31 11:22:37 -07002634 return;
2635 for (;;) {
2636 mask = rnp->grpmask;
2637 rnp = rnp->parent;
2638 if (!rnp)
2639 break;
Peter Zijlstra2a67e742015-10-08 12:24:23 +02002640 raw_spin_lock_rcu_node(rnp); /* irqs already disabled. */
Paul E. McKenney8af3a5e2014-10-31 11:22:37 -07002641 rnp->qsmaskinit &= ~mask;
Paul E. McKenney0aa04b02015-01-23 21:52:37 -08002642 rnp->qsmask &= ~mask;
Paul E. McKenney8af3a5e2014-10-31 11:22:37 -07002643 if (rnp->qsmaskinit) {
Boqun Feng67c583a72015-12-29 12:18:47 +08002644 raw_spin_unlock_rcu_node(rnp);
2645 /* irqs remain disabled. */
Paul E. McKenney8af3a5e2014-10-31 11:22:37 -07002646 return;
2647 }
Boqun Feng67c583a72015-12-29 12:18:47 +08002648 raw_spin_unlock_rcu_node(rnp); /* irqs remain disabled. */
Paul E. McKenney8af3a5e2014-10-31 11:22:37 -07002649 }
2650}
2651
2652/*
Paul E. McKenneye5601402012-01-07 11:03:57 -08002653 * The CPU has been completely removed, and some other CPU is reporting
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08002654 * this fact from process context. Do the remainder of the cleanup,
2655 * including orphaning the outgoing CPU's RCU callbacks, and also
Paul E. McKenney1331e7a2012-08-02 17:43:50 -07002656 * adopting them. There can only be one CPU hotplug operation at a time,
2657 * so no other CPU can be attempting to update rcu_cpu_kthread_task.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002658 */
Paul E. McKenneye5601402012-01-07 11:03:57 -08002659static void rcu_cleanup_dead_cpu(int cpu, struct rcu_state *rsp)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002660{
Paul E. McKenney2036d942012-01-30 17:02:47 -08002661 unsigned long flags;
Paul E. McKenneye5601402012-01-07 11:03:57 -08002662 struct rcu_data *rdp = per_cpu_ptr(rsp->rda, cpu);
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08002663 struct rcu_node *rnp = rdp->mynode; /* Outgoing CPU's rdp & rnp. */
Paul E. McKenneye5601402012-01-07 11:03:57 -08002664
Paul E. McKenneyea463512015-03-03 14:05:26 -08002665 if (!IS_ENABLED(CONFIG_HOTPLUG_CPU))
2666 return;
2667
Paul E. McKenney2036d942012-01-30 17:02:47 -08002668 /* Adjust any no-longer-needed kthreads. */
Thomas Gleixner5d01bbd2012-07-16 10:42:35 +00002669 rcu_boost_kthread_setaffinity(rnp, -1);
Paul E. McKenney2036d942012-01-30 17:02:47 -08002670
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08002671 /* Orphan the dead CPU's callbacks, and adopt them if appropriate. */
Paul E. McKenney78043c42015-01-18 17:46:24 -08002672 raw_spin_lock_irqsave(&rsp->orphan_lock, flags);
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08002673 rcu_send_cbs_to_orphanage(cpu, rsp, rnp, rdp);
Paul E. McKenney96d3fd02013-10-04 14:33:34 -07002674 rcu_adopt_orphan_cbs(rsp, flags);
Paul E. McKenneya8f4cba2014-10-31 13:48:41 -07002675 raw_spin_unlock_irqrestore(&rsp->orphan_lock, flags);
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08002676
Paul E. McKenneycf015372012-06-21 11:26:42 -07002677 WARN_ONCE(rdp->qlen != 0 || rdp->nxtlist != NULL,
2678 "rcu_cleanup_dead_cpu: Callbacks on offline CPU %d: qlen=%lu, nxtlist=%p\n",
2679 cpu, rdp->qlen, rdp->nxtlist);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002680}
2681
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002682/*
2683 * Invoke any RCU callbacks that have made it to the end of their grace
2684 * period. Thottle as specified by rdp->blimit.
2685 */
Paul E. McKenney37c72e52009-10-14 10:15:55 -07002686static void rcu_do_batch(struct rcu_state *rsp, struct rcu_data *rdp)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002687{
2688 unsigned long flags;
2689 struct rcu_head *next, *list, **tail;
Eric Dumazet878d7432012-10-18 04:55:36 -07002690 long bl, count, count_lazy;
2691 int i;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002692
Paul E. McKenneydc35c892012-12-03 13:52:00 -08002693 /* If no callbacks are ready, just return. */
Paul E. McKenney29c00b42011-06-17 15:53:19 -07002694 if (!cpu_has_callbacks_ready_to_invoke(rdp)) {
Paul E. McKenney486e2592012-01-06 14:11:30 -08002695 trace_rcu_batch_start(rsp->name, rdp->qlen_lazy, rdp->qlen, 0);
Paul E. McKenney7d0ae802015-03-03 14:57:58 -08002696 trace_rcu_batch_end(rsp->name, 0, !!READ_ONCE(rdp->nxtlist),
Paul E. McKenney4968c302011-12-07 16:32:40 -08002697 need_resched(), is_idle_task(current),
2698 rcu_is_callbacks_kthread());
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002699 return;
Paul E. McKenney29c00b42011-06-17 15:53:19 -07002700 }
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002701
2702 /*
2703 * Extract the list of ready callbacks, disabling to prevent
2704 * races with call_rcu() from interrupt handlers.
2705 */
2706 local_irq_save(flags);
Paul E. McKenney8146c4e22012-01-10 14:23:29 -08002707 WARN_ON_ONCE(cpu_is_offline(smp_processor_id()));
Paul E. McKenney29c00b42011-06-17 15:53:19 -07002708 bl = rdp->blimit;
Paul E. McKenney486e2592012-01-06 14:11:30 -08002709 trace_rcu_batch_start(rsp->name, rdp->qlen_lazy, rdp->qlen, bl);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002710 list = rdp->nxtlist;
2711 rdp->nxtlist = *rdp->nxttail[RCU_DONE_TAIL];
2712 *rdp->nxttail[RCU_DONE_TAIL] = NULL;
2713 tail = rdp->nxttail[RCU_DONE_TAIL];
Paul E. McKenneyb41772a2012-06-21 20:50:42 -07002714 for (i = RCU_NEXT_SIZE - 1; i >= 0; i--)
2715 if (rdp->nxttail[i] == rdp->nxttail[RCU_DONE_TAIL])
2716 rdp->nxttail[i] = &rdp->nxtlist;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002717 local_irq_restore(flags);
2718
2719 /* Invoke callbacks. */
Paul E. McKenney486e2592012-01-06 14:11:30 -08002720 count = count_lazy = 0;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002721 while (list) {
2722 next = list->next;
2723 prefetch(next);
Mathieu Desnoyers551d55a2010-04-17 08:48:42 -04002724 debug_rcu_head_unqueue(list);
Paul E. McKenney486e2592012-01-06 14:11:30 -08002725 if (__rcu_reclaim(rsp->name, list))
2726 count_lazy++;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002727 list = next;
Paul E. McKenneydff16722011-11-29 15:57:13 -08002728 /* Stop only if limit reached and CPU has something to do. */
2729 if (++count >= bl &&
2730 (need_resched() ||
2731 (!is_idle_task(current) && !rcu_is_callbacks_kthread())))
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002732 break;
2733 }
2734
2735 local_irq_save(flags);
Paul E. McKenney4968c302011-12-07 16:32:40 -08002736 trace_rcu_batch_end(rsp->name, count, !!list, need_resched(),
2737 is_idle_task(current),
2738 rcu_is_callbacks_kthread());
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002739
2740 /* Update count, and requeue any remaining callbacks. */
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002741 if (list != NULL) {
2742 *tail = rdp->nxtlist;
2743 rdp->nxtlist = list;
Paul E. McKenneyb41772a2012-06-21 20:50:42 -07002744 for (i = 0; i < RCU_NEXT_SIZE; i++)
2745 if (&rdp->nxtlist == rdp->nxttail[i])
2746 rdp->nxttail[i] = tail;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002747 else
2748 break;
2749 }
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08002750 smp_mb(); /* List handling before counting for rcu_barrier(). */
2751 rdp->qlen_lazy -= count_lazy;
Paul E. McKenney7d0ae802015-03-03 14:57:58 -08002752 WRITE_ONCE(rdp->qlen, rdp->qlen - count);
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08002753 rdp->n_cbs_invoked += count;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002754
2755 /* Reinstate batch limit if we have worked down the excess. */
2756 if (rdp->blimit == LONG_MAX && rdp->qlen <= qlowmark)
2757 rdp->blimit = blimit;
2758
Paul E. McKenney37c72e52009-10-14 10:15:55 -07002759 /* Reset ->qlen_last_fqs_check trigger if enough CBs have drained. */
2760 if (rdp->qlen == 0 && rdp->qlen_last_fqs_check != 0) {
2761 rdp->qlen_last_fqs_check = 0;
2762 rdp->n_force_qs_snap = rsp->n_force_qs;
2763 } else if (rdp->qlen < rdp->qlen_last_fqs_check - qhimark)
2764 rdp->qlen_last_fqs_check = rdp->qlen;
Paul E. McKenneycfca9272012-06-25 12:54:17 -07002765 WARN_ON_ONCE((rdp->nxtlist == NULL) != (rdp->qlen == 0));
Paul E. McKenney37c72e52009-10-14 10:15:55 -07002766
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002767 local_irq_restore(flags);
2768
Paul E. McKenneye0f23062011-06-21 01:29:39 -07002769 /* Re-invoke RCU core processing if there are callbacks remaining. */
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002770 if (cpu_has_callbacks_ready_to_invoke(rdp))
Paul E. McKenneya46e0892011-06-15 15:47:09 -07002771 invoke_rcu_core();
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002772}
2773
2774/*
2775 * Check to see if this CPU is in a non-context-switch quiescent state
2776 * (user mode or idle loop for rcu, non-softirq execution for rcu_bh).
Paul E. McKenneye0f23062011-06-21 01:29:39 -07002777 * Also schedule RCU core processing.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002778 *
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -07002779 * This function must be called from hardirq context. It is normally
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002780 * invoked from the scheduling-clock interrupt. If rcu_pending returns
2781 * false, there is no point in invoking rcu_check_callbacks().
2782 */
Paul E. McKenneyc3377c2d2014-10-21 07:53:02 -07002783void rcu_check_callbacks(int user)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002784{
Steven Rostedt (Red Hat)f7f7bac2013-07-12 17:18:47 -04002785 trace_rcu_utilization(TPS("Start scheduler-tick"));
Paul E. McKenneya858af22012-01-16 13:29:10 -08002786 increment_cpu_stall_ticks();
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -07002787 if (user || rcu_is_cpu_rrupt_from_idle()) {
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002788
2789 /*
2790 * Get here if this CPU took its interrupt from user
2791 * mode or from the idle loop, and if this is not a
2792 * nested interrupt. In this case, the CPU is in
Paul E. McKenneyd6714c22009-08-22 13:56:46 -07002793 * a quiescent state, so note it.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002794 *
2795 * No memory barrier is required here because both
Paul E. McKenneyd6714c22009-08-22 13:56:46 -07002796 * rcu_sched_qs() and rcu_bh_qs() reference only CPU-local
2797 * variables that other CPUs neither access nor modify,
2798 * at least not while the corresponding CPU is online.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002799 */
2800
Paul E. McKenney284a8c92014-08-14 16:38:46 -07002801 rcu_sched_qs();
2802 rcu_bh_qs();
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002803
2804 } else if (!in_softirq()) {
2805
2806 /*
2807 * Get here if this CPU did not take its interrupt from
2808 * softirq, in other words, if it is not interrupting
2809 * a rcu_bh read-side critical section. This is an _bh
Paul E. McKenneyd6714c22009-08-22 13:56:46 -07002810 * critical section, so note it.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002811 */
2812
Paul E. McKenney284a8c92014-08-14 16:38:46 -07002813 rcu_bh_qs();
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002814 }
Paul E. McKenney86aea0e2014-10-21 08:12:00 -07002815 rcu_preempt_check_callbacks();
Paul E. McKenneye3950ec2014-10-21 08:03:57 -07002816 if (rcu_pending())
Paul E. McKenneya46e0892011-06-15 15:47:09 -07002817 invoke_rcu_core();
Paul E. McKenney8315f422014-06-27 13:42:20 -07002818 if (user)
2819 rcu_note_voluntary_context_switch(current);
Steven Rostedt (Red Hat)f7f7bac2013-07-12 17:18:47 -04002820 trace_rcu_utilization(TPS("End scheduler-tick"));
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002821}
2822
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002823/*
2824 * Scan the leaf rcu_node structures, processing dyntick state for any that
2825 * have not yet encountered a quiescent state, using the function specified.
Paul E. McKenney27f4d282011-02-07 12:47:15 -08002826 * Also initiate boosting for any threads blocked on the root rcu_node.
2827 *
Paul E. McKenneyee47eb92010-01-04 15:09:07 -08002828 * The caller must have suppressed start of new grace periods.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002829 */
Paul E. McKenney217af2a2013-06-21 15:39:06 -07002830static void force_qs_rnp(struct rcu_state *rsp,
2831 int (*f)(struct rcu_data *rsp, bool *isidle,
2832 unsigned long *maxj),
2833 bool *isidle, unsigned long *maxj)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002834{
2835 unsigned long bit;
2836 int cpu;
2837 unsigned long flags;
2838 unsigned long mask;
Paul E. McKenneya0b6c9a2009-09-28 07:46:33 -07002839 struct rcu_node *rnp;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002840
Paul E. McKenneya0b6c9a2009-09-28 07:46:33 -07002841 rcu_for_each_leaf_node(rsp, rnp) {
Paul E. McKenneybde6c3a2014-07-01 11:26:57 -07002842 cond_resched_rcu_qs();
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002843 mask = 0;
Peter Zijlstra2a67e742015-10-08 12:24:23 +02002844 raw_spin_lock_irqsave_rcu_node(rnp, flags);
Paul E. McKenneya0b6c9a2009-09-28 07:46:33 -07002845 if (rnp->qsmask == 0) {
Paul E. McKenneya77da142015-03-08 14:52:27 -07002846 if (rcu_state_p == &rcu_sched_state ||
2847 rsp != rcu_state_p ||
2848 rcu_preempt_blocked_readers_cgp(rnp)) {
2849 /*
2850 * No point in scanning bits because they
2851 * are all zero. But we might need to
2852 * priority-boost blocked readers.
2853 */
2854 rcu_initiate_boost(rnp, flags);
2855 /* rcu_initiate_boost() releases rnp->lock */
2856 continue;
2857 }
2858 if (rnp->parent &&
2859 (rnp->parent->qsmask & rnp->grpmask)) {
2860 /*
2861 * Race between grace-period
2862 * initialization and task exiting RCU
2863 * read-side critical section: Report.
2864 */
2865 rcu_report_unblock_qs_rnp(rsp, rnp, flags);
2866 /* rcu_report_unblock_qs_rnp() rlses ->lock */
2867 continue;
2868 }
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002869 }
Paul E. McKenneya0b6c9a2009-09-28 07:46:33 -07002870 cpu = rnp->grplo;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002871 bit = 1;
Paul E. McKenneya0b6c9a2009-09-28 07:46:33 -07002872 for (; cpu <= rnp->grphi; cpu++, bit <<= 1) {
Paul E. McKenney0edd1b12013-06-21 16:37:22 -07002873 if ((rnp->qsmask & bit) != 0) {
Paul E. McKenney0edd1b12013-06-21 16:37:22 -07002874 if (f(per_cpu_ptr(rsp->rda, cpu), isidle, maxj))
2875 mask |= bit;
2876 }
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002877 }
Paul E. McKenney45f014c52e2010-01-04 15:09:08 -08002878 if (mask != 0) {
Paul E. McKenney654e9532015-03-15 09:19:35 -07002879 /* Idle/offline CPUs, report (releases rnp->lock. */
2880 rcu_report_qs_rnp(mask, rsp, rnp, rnp->gpnum, flags);
Paul E. McKenney0aa04b02015-01-23 21:52:37 -08002881 } else {
2882 /* Nothing to do here, so just drop the lock. */
Boqun Feng67c583a72015-12-29 12:18:47 +08002883 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002884 }
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002885 }
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002886}
2887
2888/*
2889 * Force quiescent states on reluctant CPUs, and also detect which
2890 * CPUs are in dyntick-idle mode.
2891 */
Paul E. McKenney4cdfc1752012-06-22 17:06:26 -07002892static void force_quiescent_state(struct rcu_state *rsp)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002893{
2894 unsigned long flags;
Paul E. McKenney394f2762012-06-26 17:00:35 -07002895 bool ret;
2896 struct rcu_node *rnp;
2897 struct rcu_node *rnp_old = NULL;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002898
Paul E. McKenney394f2762012-06-26 17:00:35 -07002899 /* Funnel through hierarchy to reduce memory contention. */
Shan Weid860d402014-06-19 14:12:44 -07002900 rnp = __this_cpu_read(rsp->rda->mynode);
Paul E. McKenney394f2762012-06-26 17:00:35 -07002901 for (; rnp != NULL; rnp = rnp->parent) {
Paul E. McKenney7d0ae802015-03-03 14:57:58 -08002902 ret = (READ_ONCE(rsp->gp_flags) & RCU_GP_FLAG_FQS) ||
Paul E. McKenney394f2762012-06-26 17:00:35 -07002903 !raw_spin_trylock(&rnp->fqslock);
2904 if (rnp_old != NULL)
2905 raw_spin_unlock(&rnp_old->fqslock);
2906 if (ret) {
Paul E. McKenneya7925632014-06-02 14:54:34 -07002907 rsp->n_force_qs_lh++;
Paul E. McKenney394f2762012-06-26 17:00:35 -07002908 return;
2909 }
2910 rnp_old = rnp;
2911 }
2912 /* rnp_old == rcu_get_root(rsp), rnp == NULL. */
2913
2914 /* Reached the root of the rcu_node tree, acquire lock. */
Peter Zijlstra2a67e742015-10-08 12:24:23 +02002915 raw_spin_lock_irqsave_rcu_node(rnp_old, flags);
Paul E. McKenney394f2762012-06-26 17:00:35 -07002916 raw_spin_unlock(&rnp_old->fqslock);
Paul E. McKenney7d0ae802015-03-03 14:57:58 -08002917 if (READ_ONCE(rsp->gp_flags) & RCU_GP_FLAG_FQS) {
Paul E. McKenneya7925632014-06-02 14:54:34 -07002918 rsp->n_force_qs_lh++;
Boqun Feng67c583a72015-12-29 12:18:47 +08002919 raw_spin_unlock_irqrestore_rcu_node(rnp_old, flags);
Paul E. McKenney4cdfc1752012-06-22 17:06:26 -07002920 return; /* Someone beat us to it. */
Paul E. McKenney46a1e342010-01-04 15:09:09 -08002921 }
Paul E. McKenney7d0ae802015-03-03 14:57:58 -08002922 WRITE_ONCE(rsp->gp_flags, READ_ONCE(rsp->gp_flags) | RCU_GP_FLAG_FQS);
Boqun Feng67c583a72015-12-29 12:18:47 +08002923 raw_spin_unlock_irqrestore_rcu_node(rnp_old, flags);
Paul Gortmakerabedf8e2016-02-19 09:46:41 +01002924 swake_up(&rsp->gp_wq); /* Memory barrier implied by swake_up() path. */
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002925}
2926
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002927/*
Paul E. McKenneye0f23062011-06-21 01:29:39 -07002928 * This does the RCU core processing work for the specified rcu_state
2929 * and rcu_data structures. This may be called only from the CPU to
2930 * whom the rdp belongs.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002931 */
2932static void
Paul E. McKenney1bca8cf2012-06-12 09:40:38 -07002933__rcu_process_callbacks(struct rcu_state *rsp)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002934{
2935 unsigned long flags;
Paul E. McKenney48a76392014-03-11 13:02:16 -07002936 bool needwake;
Christoph Lameterfa07a582014-04-15 12:20:12 -07002937 struct rcu_data *rdp = raw_cpu_ptr(rsp->rda);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002938
Paul E. McKenney2e597552009-08-15 09:53:48 -07002939 WARN_ON_ONCE(rdp->beenonline == 0);
2940
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002941 /* Update RCU state based on any recent quiescent states. */
2942 rcu_check_quiescent_state(rsp, rdp);
2943
2944 /* Does this CPU require a not-yet-started grace period? */
Paul E. McKenneydc35c892012-12-03 13:52:00 -08002945 local_irq_save(flags);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002946 if (cpu_needs_another_gp(rsp, rdp)) {
Paul E. McKenney6cf10082015-10-08 15:36:54 -07002947 raw_spin_lock_rcu_node(rcu_get_root(rsp)); /* irqs disabled. */
Paul E. McKenney48a76392014-03-11 13:02:16 -07002948 needwake = rcu_start_gp(rsp);
Boqun Feng67c583a72015-12-29 12:18:47 +08002949 raw_spin_unlock_irqrestore_rcu_node(rcu_get_root(rsp), flags);
Paul E. McKenney48a76392014-03-11 13:02:16 -07002950 if (needwake)
2951 rcu_gp_kthread_wake(rsp);
Paul E. McKenneydc35c892012-12-03 13:52:00 -08002952 } else {
2953 local_irq_restore(flags);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002954 }
2955
2956 /* If there are callbacks ready, invoke them. */
Shaohua Li09223372011-06-14 13:26:25 +08002957 if (cpu_has_callbacks_ready_to_invoke(rdp))
Paul E. McKenneya46e0892011-06-15 15:47:09 -07002958 invoke_rcu_callbacks(rsp, rdp);
Paul E. McKenney96d3fd02013-10-04 14:33:34 -07002959
2960 /* Do any needed deferred wakeups of rcuo kthreads. */
2961 do_nocb_deferred_wakeup(rdp);
Shaohua Li09223372011-06-14 13:26:25 +08002962}
2963
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002964/*
Paul E. McKenneye0f23062011-06-21 01:29:39 -07002965 * Do RCU core processing for the current CPU.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002966 */
Shaohua Li09223372011-06-14 13:26:25 +08002967static void rcu_process_callbacks(struct softirq_action *unused)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002968{
Paul E. McKenney6ce75a22012-06-12 11:01:13 -07002969 struct rcu_state *rsp;
2970
Paul E. McKenneybfa00b42012-06-21 09:54:10 -07002971 if (cpu_is_offline(smp_processor_id()))
2972 return;
Steven Rostedt (Red Hat)f7f7bac2013-07-12 17:18:47 -04002973 trace_rcu_utilization(TPS("Start RCU core"));
Paul E. McKenney6ce75a22012-06-12 11:01:13 -07002974 for_each_rcu_flavor(rsp)
2975 __rcu_process_callbacks(rsp);
Steven Rostedt (Red Hat)f7f7bac2013-07-12 17:18:47 -04002976 trace_rcu_utilization(TPS("End RCU core"));
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002977}
2978
Paul E. McKenneya26ac242011-01-12 14:10:23 -08002979/*
Paul E. McKenneye0f23062011-06-21 01:29:39 -07002980 * Schedule RCU callback invocation. If the specified type of RCU
2981 * does not support RCU priority boosting, just do a direct call,
2982 * otherwise wake up the per-CPU kernel kthread. Note that because we
Paul E. McKenney924df8a2014-10-29 15:37:58 -07002983 * are running on the current CPU with softirqs disabled, the
Paul E. McKenneye0f23062011-06-21 01:29:39 -07002984 * rcu_cpu_kthread_task cannot disappear out from under us.
Paul E. McKenneya26ac242011-01-12 14:10:23 -08002985 */
Paul E. McKenneya46e0892011-06-15 15:47:09 -07002986static void invoke_rcu_callbacks(struct rcu_state *rsp, struct rcu_data *rdp)
Paul E. McKenneya26ac242011-01-12 14:10:23 -08002987{
Paul E. McKenney7d0ae802015-03-03 14:57:58 -08002988 if (unlikely(!READ_ONCE(rcu_scheduler_fully_active)))
Paul E. McKenneyb0d30412011-07-10 15:57:35 -07002989 return;
Paul E. McKenneya46e0892011-06-15 15:47:09 -07002990 if (likely(!rsp->boost)) {
2991 rcu_do_batch(rsp, rdp);
Paul E. McKenneya26ac242011-01-12 14:10:23 -08002992 return;
2993 }
Paul E. McKenneya46e0892011-06-15 15:47:09 -07002994 invoke_rcu_callbacks_kthread();
Paul E. McKenneya26ac242011-01-12 14:10:23 -08002995}
2996
Paul E. McKenneya46e0892011-06-15 15:47:09 -07002997static void invoke_rcu_core(void)
Shaohua Li09223372011-06-14 13:26:25 +08002998{
Paul E. McKenneyb0f74032013-02-04 12:14:24 -08002999 if (cpu_online(smp_processor_id()))
3000 raise_softirq(RCU_SOFTIRQ);
Shaohua Li09223372011-06-14 13:26:25 +08003001}
3002
Paul E. McKenney29154c52012-05-30 03:21:48 -07003003/*
3004 * Handle any core-RCU processing required by a call_rcu() invocation.
3005 */
3006static void __call_rcu_core(struct rcu_state *rsp, struct rcu_data *rdp,
3007 struct rcu_head *head, unsigned long flags)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003008{
Paul E. McKenney48a76392014-03-11 13:02:16 -07003009 bool needwake;
3010
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003011 /*
Paul E. McKenney29154c52012-05-30 03:21:48 -07003012 * If called from an extended quiescent state, invoke the RCU
3013 * core in order to force a re-evaluation of RCU's idleness.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003014 */
Yao Dongdong9910aff2015-02-25 17:09:46 +08003015 if (!rcu_is_watching())
Paul E. McKenney29154c52012-05-30 03:21:48 -07003016 invoke_rcu_core();
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003017
Paul E. McKenney29154c52012-05-30 03:21:48 -07003018 /* If interrupts were disabled or CPU offline, don't invoke RCU core. */
3019 if (irqs_disabled_flags(flags) || cpu_is_offline(smp_processor_id()))
Paul E. McKenney2655d572011-04-07 22:47:23 -07003020 return;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003021
Paul E. McKenney37c72e52009-10-14 10:15:55 -07003022 /*
3023 * Force the grace period if too many callbacks or too long waiting.
3024 * Enforce hysteresis, and don't invoke force_quiescent_state()
3025 * if some other CPU has recently done so. Also, don't bother
3026 * invoking force_quiescent_state() if the newly enqueued callback
3027 * is the only one waiting for a grace period to complete.
3028 */
Paul E. McKenney2655d572011-04-07 22:47:23 -07003029 if (unlikely(rdp->qlen > rdp->qlen_last_fqs_check + qhimark)) {
Paul E. McKenneyb52573d2010-12-14 17:36:02 -08003030
3031 /* Are we ignoring a completed grace period? */
Paul E. McKenney470716f2013-03-19 11:32:11 -07003032 note_gp_changes(rsp, rdp);
Paul E. McKenneyb52573d2010-12-14 17:36:02 -08003033
3034 /* Start a new grace period if one not already started. */
3035 if (!rcu_gp_in_progress(rsp)) {
Paul E. McKenneyb52573d2010-12-14 17:36:02 -08003036 struct rcu_node *rnp_root = rcu_get_root(rsp);
3037
Peter Zijlstra2a67e742015-10-08 12:24:23 +02003038 raw_spin_lock_rcu_node(rnp_root);
Paul E. McKenney48a76392014-03-11 13:02:16 -07003039 needwake = rcu_start_gp(rsp);
Boqun Feng67c583a72015-12-29 12:18:47 +08003040 raw_spin_unlock_rcu_node(rnp_root);
Paul E. McKenney48a76392014-03-11 13:02:16 -07003041 if (needwake)
3042 rcu_gp_kthread_wake(rsp);
Paul E. McKenneyb52573d2010-12-14 17:36:02 -08003043 } else {
3044 /* Give the grace period a kick. */
3045 rdp->blimit = LONG_MAX;
3046 if (rsp->n_force_qs == rdp->n_force_qs_snap &&
3047 *rdp->nxttail[RCU_DONE_TAIL] != head)
Paul E. McKenney4cdfc1752012-06-22 17:06:26 -07003048 force_quiescent_state(rsp);
Paul E. McKenneyb52573d2010-12-14 17:36:02 -08003049 rdp->n_force_qs_snap = rsp->n_force_qs;
3050 rdp->qlen_last_fqs_check = rdp->qlen;
3051 }
Paul E. McKenney4cdfc1752012-06-22 17:06:26 -07003052 }
Paul E. McKenney29154c52012-05-30 03:21:48 -07003053}
3054
Paul E. McKenney3fbfbf72012-08-19 21:35:53 -07003055/*
Paul E. McKenneyae150182013-04-23 13:20:57 -07003056 * RCU callback function to leak a callback.
3057 */
3058static void rcu_leak_callback(struct rcu_head *rhp)
3059{
3060}
3061
3062/*
Paul E. McKenney3fbfbf72012-08-19 21:35:53 -07003063 * Helper function for call_rcu() and friends. The cpu argument will
3064 * normally be -1, indicating "currently running CPU". It may specify
3065 * a CPU only if that CPU is a no-CBs CPU. Currently, only _rcu_barrier()
3066 * is expected to specify a CPU.
3067 */
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003068static void
Boqun Fengb6a4ae72015-07-29 13:29:38 +08003069__call_rcu(struct rcu_head *head, rcu_callback_t func,
Paul E. McKenney3fbfbf72012-08-19 21:35:53 -07003070 struct rcu_state *rsp, int cpu, bool lazy)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003071{
3072 unsigned long flags;
3073 struct rcu_data *rdp;
3074
Paul E. McKenney1146edc2014-06-09 08:24:17 -07003075 WARN_ON_ONCE((unsigned long)head & 0x1); /* Misaligned rcu_head! */
Paul E. McKenneyae150182013-04-23 13:20:57 -07003076 if (debug_rcu_head_queue(head)) {
3077 /* Probable double call_rcu(), so leak the callback. */
Paul E. McKenney7d0ae802015-03-03 14:57:58 -08003078 WRITE_ONCE(head->func, rcu_leak_callback);
Paul E. McKenneyae150182013-04-23 13:20:57 -07003079 WARN_ONCE(1, "__call_rcu(): Leaked duplicate callback\n");
3080 return;
3081 }
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003082 head->func = func;
3083 head->next = NULL;
3084
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003085 /*
3086 * Opportunistically note grace-period endings and beginnings.
3087 * Note that we might see a beginning right after we see an
3088 * end, but never vice versa, since this CPU has to pass through
3089 * a quiescent state betweentimes.
3090 */
3091 local_irq_save(flags);
3092 rdp = this_cpu_ptr(rsp->rda);
3093
3094 /* Add the callback to our list. */
Paul E. McKenney3fbfbf72012-08-19 21:35:53 -07003095 if (unlikely(rdp->nxttail[RCU_NEXT_TAIL] == NULL) || cpu != -1) {
3096 int offline;
3097
3098 if (cpu != -1)
3099 rdp = per_cpu_ptr(rsp->rda, cpu);
Paul E. McKenney143da9c2015-01-19 19:57:32 -08003100 if (likely(rdp->mynode)) {
3101 /* Post-boot, so this should be for a no-CBs CPU. */
3102 offline = !__call_rcu_nocb(rdp, head, lazy, flags);
3103 WARN_ON_ONCE(offline);
3104 /* Offline CPU, _call_rcu() illegal, leak callback. */
3105 local_irq_restore(flags);
3106 return;
3107 }
3108 /*
3109 * Very early boot, before rcu_init(). Initialize if needed
3110 * and then drop through to queue the callback.
3111 */
3112 BUG_ON(cpu != -1);
Paul E. McKenney34404ca2015-01-19 20:39:20 -08003113 WARN_ON_ONCE(!rcu_is_watching());
Paul E. McKenney143da9c2015-01-19 19:57:32 -08003114 if (!likely(rdp->nxtlist))
3115 init_default_callback_list(rdp);
Paul E. McKenney0d8ee372012-08-03 13:16:15 -07003116 }
Paul E. McKenney7d0ae802015-03-03 14:57:58 -08003117 WRITE_ONCE(rdp->qlen, rdp->qlen + 1);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003118 if (lazy)
3119 rdp->qlen_lazy++;
3120 else
3121 rcu_idle_count_callbacks_posted();
3122 smp_mb(); /* Count before adding callback for rcu_barrier(). */
3123 *rdp->nxttail[RCU_NEXT_TAIL] = head;
3124 rdp->nxttail[RCU_NEXT_TAIL] = &head->next;
3125
3126 if (__is_kfree_rcu_offset((unsigned long)func))
3127 trace_rcu_kfree_callback(rsp->name, head, (unsigned long)func,
3128 rdp->qlen_lazy, rdp->qlen);
3129 else
3130 trace_rcu_callback(rsp->name, head, rdp->qlen_lazy, rdp->qlen);
3131
Paul E. McKenney29154c52012-05-30 03:21:48 -07003132 /* Go handle any RCU core processing required. */
3133 __call_rcu_core(rsp, rdp, head, flags);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003134 local_irq_restore(flags);
3135}
3136
3137/*
Paul E. McKenneyd6714c22009-08-22 13:56:46 -07003138 * Queue an RCU-sched callback for invocation after a grace period.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003139 */
Boqun Fengb6a4ae72015-07-29 13:29:38 +08003140void call_rcu_sched(struct rcu_head *head, rcu_callback_t func)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003141{
Paul E. McKenney3fbfbf72012-08-19 21:35:53 -07003142 __call_rcu(head, func, &rcu_sched_state, -1, 0);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003143}
Paul E. McKenneyd6714c22009-08-22 13:56:46 -07003144EXPORT_SYMBOL_GPL(call_rcu_sched);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003145
3146/*
Paul E. McKenney486e2592012-01-06 14:11:30 -08003147 * Queue an RCU callback for invocation after a quicker grace period.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003148 */
Boqun Fengb6a4ae72015-07-29 13:29:38 +08003149void call_rcu_bh(struct rcu_head *head, rcu_callback_t func)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003150{
Paul E. McKenney3fbfbf72012-08-19 21:35:53 -07003151 __call_rcu(head, func, &rcu_bh_state, -1, 0);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003152}
3153EXPORT_SYMBOL_GPL(call_rcu_bh);
3154
Paul E. McKenney6d813392012-02-23 13:30:16 -08003155/*
Andreea-Cristina Bernat495aa962014-03-18 20:48:48 +02003156 * Queue an RCU callback for lazy invocation after a grace period.
3157 * This will likely be later named something like "call_rcu_lazy()",
3158 * but this change will require some way of tagging the lazy RCU
3159 * callbacks in the list of pending callbacks. Until then, this
3160 * function may only be called from __kfree_rcu().
3161 */
3162void kfree_call_rcu(struct rcu_head *head,
Boqun Fengb6a4ae72015-07-29 13:29:38 +08003163 rcu_callback_t func)
Andreea-Cristina Bernat495aa962014-03-18 20:48:48 +02003164{
Uma Sharmae5341652014-03-23 22:32:09 -07003165 __call_rcu(head, func, rcu_state_p, -1, 1);
Andreea-Cristina Bernat495aa962014-03-18 20:48:48 +02003166}
3167EXPORT_SYMBOL_GPL(kfree_call_rcu);
3168
3169/*
Paul E. McKenney6d813392012-02-23 13:30:16 -08003170 * Because a context switch is a grace period for RCU-sched and RCU-bh,
3171 * any blocking grace-period wait automatically implies a grace period
3172 * if there is only one CPU online at any point time during execution
3173 * of either synchronize_sched() or synchronize_rcu_bh(). It is OK to
3174 * occasionally incorrectly indicate that there are multiple CPUs online
3175 * when there was in fact only one the whole time, as this just adds
3176 * some overhead: RCU still operates correctly.
Paul E. McKenney6d813392012-02-23 13:30:16 -08003177 */
3178static inline int rcu_blocking_is_gp(void)
3179{
Paul E. McKenney95f0c1d2012-06-19 11:58:27 -07003180 int ret;
3181
Paul E. McKenney6d813392012-02-23 13:30:16 -08003182 might_sleep(); /* Check for RCU read-side critical section. */
Paul E. McKenney95f0c1d2012-06-19 11:58:27 -07003183 preempt_disable();
3184 ret = num_online_cpus() <= 1;
3185 preempt_enable();
3186 return ret;
Paul E. McKenney6d813392012-02-23 13:30:16 -08003187}
3188
Paul E. McKenney6ebb2372009-11-22 08:53:50 -08003189/**
3190 * synchronize_sched - wait until an rcu-sched grace period has elapsed.
3191 *
3192 * Control will return to the caller some time after a full rcu-sched
3193 * grace period has elapsed, in other words after all currently executing
3194 * rcu-sched read-side critical sections have completed. These read-side
3195 * critical sections are delimited by rcu_read_lock_sched() and
3196 * rcu_read_unlock_sched(), and may be nested. Note that preempt_disable(),
3197 * local_irq_disable(), and so on may be used in place of
3198 * rcu_read_lock_sched().
3199 *
3200 * This means that all preempt_disable code sequences, including NMI and
Paul E. McKenneyf0a0e6f2012-10-23 13:47:01 -07003201 * non-threaded hardware-interrupt handlers, in progress on entry will
3202 * have completed before this primitive returns. However, this does not
3203 * guarantee that softirq handlers will have completed, since in some
3204 * kernels, these handlers can run in process context, and can block.
3205 *
3206 * Note that this guarantee implies further memory-ordering guarantees.
3207 * On systems with more than one CPU, when synchronize_sched() returns,
3208 * each CPU is guaranteed to have executed a full memory barrier since the
3209 * end of its last RCU-sched read-side critical section whose beginning
3210 * preceded the call to synchronize_sched(). In addition, each CPU having
3211 * an RCU read-side critical section that extends beyond the return from
3212 * synchronize_sched() is guaranteed to have executed a full memory barrier
3213 * after the beginning of synchronize_sched() and before the beginning of
3214 * that RCU read-side critical section. Note that these guarantees include
3215 * CPUs that are offline, idle, or executing in user mode, as well as CPUs
3216 * that are executing in the kernel.
3217 *
3218 * Furthermore, if CPU A invoked synchronize_sched(), which returned
3219 * to its caller on CPU B, then both CPU A and CPU B are guaranteed
3220 * to have executed a full memory barrier during the execution of
3221 * synchronize_sched() -- even if CPU A and CPU B are the same CPU (but
3222 * again only if the system has more than one CPU).
Paul E. McKenney6ebb2372009-11-22 08:53:50 -08003223 *
3224 * This primitive provides the guarantees made by the (now removed)
3225 * synchronize_kernel() API. In contrast, synchronize_rcu() only
3226 * guarantees that rcu_read_lock() sections will have completed.
3227 * In "classic RCU", these two guarantees happen to be one and
3228 * the same, but can differ in realtime RCU implementations.
3229 */
3230void synchronize_sched(void)
3231{
Paul E. McKenneyf78f5b92015-06-18 15:50:02 -07003232 RCU_LOCKDEP_WARN(lock_is_held(&rcu_bh_lock_map) ||
3233 lock_is_held(&rcu_lock_map) ||
3234 lock_is_held(&rcu_sched_lock_map),
3235 "Illegal synchronize_sched() in RCU-sched read-side critical section");
Paul E. McKenney6ebb2372009-11-22 08:53:50 -08003236 if (rcu_blocking_is_gp())
3237 return;
Paul E. McKenney5afff482015-02-18 16:39:09 -08003238 if (rcu_gp_is_expedited())
Antti P Miettinen3705b882012-10-05 09:59:15 +03003239 synchronize_sched_expedited();
3240 else
3241 wait_rcu_gp(call_rcu_sched);
Paul E. McKenney6ebb2372009-11-22 08:53:50 -08003242}
3243EXPORT_SYMBOL_GPL(synchronize_sched);
3244
3245/**
3246 * synchronize_rcu_bh - wait until an rcu_bh grace period has elapsed.
3247 *
3248 * Control will return to the caller some time after a full rcu_bh grace
3249 * period has elapsed, in other words after all currently executing rcu_bh
3250 * read-side critical sections have completed. RCU read-side critical
3251 * sections are delimited by rcu_read_lock_bh() and rcu_read_unlock_bh(),
3252 * and may be nested.
Paul E. McKenneyf0a0e6f2012-10-23 13:47:01 -07003253 *
3254 * See the description of synchronize_sched() for more detailed information
3255 * on memory ordering guarantees.
Paul E. McKenney6ebb2372009-11-22 08:53:50 -08003256 */
3257void synchronize_rcu_bh(void)
3258{
Paul E. McKenneyf78f5b92015-06-18 15:50:02 -07003259 RCU_LOCKDEP_WARN(lock_is_held(&rcu_bh_lock_map) ||
3260 lock_is_held(&rcu_lock_map) ||
3261 lock_is_held(&rcu_sched_lock_map),
3262 "Illegal synchronize_rcu_bh() in RCU-bh read-side critical section");
Paul E. McKenney6ebb2372009-11-22 08:53:50 -08003263 if (rcu_blocking_is_gp())
3264 return;
Paul E. McKenney5afff482015-02-18 16:39:09 -08003265 if (rcu_gp_is_expedited())
Antti P Miettinen3705b882012-10-05 09:59:15 +03003266 synchronize_rcu_bh_expedited();
3267 else
3268 wait_rcu_gp(call_rcu_bh);
Paul E. McKenney6ebb2372009-11-22 08:53:50 -08003269}
3270EXPORT_SYMBOL_GPL(synchronize_rcu_bh);
3271
Paul E. McKenney765a3f42014-03-14 16:37:08 -07003272/**
3273 * get_state_synchronize_rcu - Snapshot current RCU state
3274 *
3275 * Returns a cookie that is used by a later call to cond_synchronize_rcu()
3276 * to determine whether or not a full grace period has elapsed in the
3277 * meantime.
3278 */
3279unsigned long get_state_synchronize_rcu(void)
3280{
3281 /*
3282 * Any prior manipulation of RCU-protected data must happen
3283 * before the load from ->gpnum.
3284 */
3285 smp_mb(); /* ^^^ */
3286
3287 /*
3288 * Make sure this load happens before the purportedly
3289 * time-consuming work between get_state_synchronize_rcu()
3290 * and cond_synchronize_rcu().
3291 */
Uma Sharmae5341652014-03-23 22:32:09 -07003292 return smp_load_acquire(&rcu_state_p->gpnum);
Paul E. McKenney765a3f42014-03-14 16:37:08 -07003293}
3294EXPORT_SYMBOL_GPL(get_state_synchronize_rcu);
3295
3296/**
3297 * cond_synchronize_rcu - Conditionally wait for an RCU grace period
3298 *
3299 * @oldstate: return value from earlier call to get_state_synchronize_rcu()
3300 *
3301 * If a full RCU grace period has elapsed since the earlier call to
3302 * get_state_synchronize_rcu(), just return. Otherwise, invoke
3303 * synchronize_rcu() to wait for a full grace period.
3304 *
3305 * Yes, this function does not take counter wrap into account. But
3306 * counter wrap is harmless. If the counter wraps, we have waited for
3307 * more than 2 billion grace periods (and way more on a 64-bit system!),
3308 * so waiting for one additional grace period should be just fine.
3309 */
3310void cond_synchronize_rcu(unsigned long oldstate)
3311{
3312 unsigned long newstate;
3313
3314 /*
3315 * Ensure that this load happens before any RCU-destructive
3316 * actions the caller might carry out after we return.
3317 */
Uma Sharmae5341652014-03-23 22:32:09 -07003318 newstate = smp_load_acquire(&rcu_state_p->completed);
Paul E. McKenney765a3f42014-03-14 16:37:08 -07003319 if (ULONG_CMP_GE(oldstate, newstate))
3320 synchronize_rcu();
3321}
3322EXPORT_SYMBOL_GPL(cond_synchronize_rcu);
3323
Paul E. McKenney24560052015-05-30 10:11:24 -07003324/**
3325 * get_state_synchronize_sched - Snapshot current RCU-sched state
3326 *
3327 * Returns a cookie that is used by a later call to cond_synchronize_sched()
3328 * to determine whether or not a full grace period has elapsed in the
3329 * meantime.
3330 */
3331unsigned long get_state_synchronize_sched(void)
3332{
3333 /*
3334 * Any prior manipulation of RCU-protected data must happen
3335 * before the load from ->gpnum.
3336 */
3337 smp_mb(); /* ^^^ */
3338
3339 /*
3340 * Make sure this load happens before the purportedly
3341 * time-consuming work between get_state_synchronize_sched()
3342 * and cond_synchronize_sched().
3343 */
3344 return smp_load_acquire(&rcu_sched_state.gpnum);
3345}
3346EXPORT_SYMBOL_GPL(get_state_synchronize_sched);
3347
3348/**
3349 * cond_synchronize_sched - Conditionally wait for an RCU-sched grace period
3350 *
3351 * @oldstate: return value from earlier call to get_state_synchronize_sched()
3352 *
3353 * If a full RCU-sched grace period has elapsed since the earlier call to
3354 * get_state_synchronize_sched(), just return. Otherwise, invoke
3355 * synchronize_sched() to wait for a full grace period.
3356 *
3357 * Yes, this function does not take counter wrap into account. But
3358 * counter wrap is harmless. If the counter wraps, we have waited for
3359 * more than 2 billion grace periods (and way more on a 64-bit system!),
3360 * so waiting for one additional grace period should be just fine.
3361 */
3362void cond_synchronize_sched(unsigned long oldstate)
3363{
3364 unsigned long newstate;
3365
3366 /*
3367 * Ensure that this load happens before any RCU-destructive
3368 * actions the caller might carry out after we return.
3369 */
3370 newstate = smp_load_acquire(&rcu_sched_state.completed);
3371 if (ULONG_CMP_GE(oldstate, newstate))
3372 synchronize_sched();
3373}
3374EXPORT_SYMBOL_GPL(cond_synchronize_sched);
3375
Paul E. McKenney28f00762015-06-25 15:00:58 -07003376/* Adjust sequence number for start of update-side operation. */
3377static void rcu_seq_start(unsigned long *sp)
3378{
3379 WRITE_ONCE(*sp, *sp + 1);
3380 smp_mb(); /* Ensure update-side operation after counter increment. */
3381 WARN_ON_ONCE(!(*sp & 0x1));
3382}
3383
3384/* Adjust sequence number for end of update-side operation. */
3385static void rcu_seq_end(unsigned long *sp)
3386{
3387 smp_mb(); /* Ensure update-side operation before counter increment. */
3388 WRITE_ONCE(*sp, *sp + 1);
3389 WARN_ON_ONCE(*sp & 0x1);
3390}
3391
3392/* Take a snapshot of the update side's sequence number. */
3393static unsigned long rcu_seq_snap(unsigned long *sp)
3394{
3395 unsigned long s;
3396
Paul E. McKenney28f00762015-06-25 15:00:58 -07003397 s = (READ_ONCE(*sp) + 3) & ~0x1;
3398 smp_mb(); /* Above access must not bleed into critical section. */
3399 return s;
3400}
3401
3402/*
3403 * Given a snapshot from rcu_seq_snap(), determine whether or not a
3404 * full update-side operation has occurred.
3405 */
3406static bool rcu_seq_done(unsigned long *sp, unsigned long s)
3407{
3408 return ULONG_CMP_GE(READ_ONCE(*sp), s);
3409}
3410
3411/* Wrapper functions for expedited grace periods. */
3412static void rcu_exp_gp_seq_start(struct rcu_state *rsp)
3413{
3414 rcu_seq_start(&rsp->expedited_sequence);
3415}
3416static void rcu_exp_gp_seq_end(struct rcu_state *rsp)
3417{
3418 rcu_seq_end(&rsp->expedited_sequence);
Paul E. McKenney704dd432015-06-27 09:36:29 -07003419 smp_mb(); /* Ensure that consecutive grace periods serialize. */
Paul E. McKenney28f00762015-06-25 15:00:58 -07003420}
3421static unsigned long rcu_exp_gp_seq_snap(struct rcu_state *rsp)
3422{
Paul E. McKenney886ef5a2015-09-29 12:34:40 -07003423 smp_mb(); /* Caller's modifications seen first by other CPUs. */
Paul E. McKenney28f00762015-06-25 15:00:58 -07003424 return rcu_seq_snap(&rsp->expedited_sequence);
3425}
3426static bool rcu_exp_gp_seq_done(struct rcu_state *rsp, unsigned long s)
3427{
3428 return rcu_seq_done(&rsp->expedited_sequence, s);
3429}
3430
Paul E. McKenney7922cd02015-07-31 13:34:32 -07003431/*
Paul E. McKenneyb9585e92015-07-31 16:04:45 -07003432 * Reset the ->expmaskinit values in the rcu_node tree to reflect any
3433 * recent CPU-online activity. Note that these masks are not cleared
3434 * when CPUs go offline, so they reflect the union of all CPUs that have
3435 * ever been online. This means that this function normally takes its
3436 * no-work-to-do fastpath.
3437 */
3438static void sync_exp_reset_tree_hotplug(struct rcu_state *rsp)
3439{
3440 bool done;
3441 unsigned long flags;
3442 unsigned long mask;
3443 unsigned long oldmask;
3444 int ncpus = READ_ONCE(rsp->ncpus);
3445 struct rcu_node *rnp;
3446 struct rcu_node *rnp_up;
3447
3448 /* If no new CPUs onlined since last time, nothing to do. */
3449 if (likely(ncpus == rsp->ncpus_snap))
3450 return;
3451 rsp->ncpus_snap = ncpus;
3452
3453 /*
3454 * Each pass through the following loop propagates newly onlined
3455 * CPUs for the current rcu_node structure up the rcu_node tree.
3456 */
3457 rcu_for_each_leaf_node(rsp, rnp) {
Peter Zijlstra2a67e742015-10-08 12:24:23 +02003458 raw_spin_lock_irqsave_rcu_node(rnp, flags);
Paul E. McKenneyb9585e92015-07-31 16:04:45 -07003459 if (rnp->expmaskinit == rnp->expmaskinitnext) {
Boqun Feng67c583a72015-12-29 12:18:47 +08003460 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
Paul E. McKenneyb9585e92015-07-31 16:04:45 -07003461 continue; /* No new CPUs, nothing to do. */
3462 }
3463
3464 /* Update this node's mask, track old value for propagation. */
3465 oldmask = rnp->expmaskinit;
3466 rnp->expmaskinit = rnp->expmaskinitnext;
Boqun Feng67c583a72015-12-29 12:18:47 +08003467 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
Paul E. McKenneyb9585e92015-07-31 16:04:45 -07003468
3469 /* If was already nonzero, nothing to propagate. */
3470 if (oldmask)
3471 continue;
3472
3473 /* Propagate the new CPU up the tree. */
3474 mask = rnp->grpmask;
3475 rnp_up = rnp->parent;
3476 done = false;
3477 while (rnp_up) {
Peter Zijlstra2a67e742015-10-08 12:24:23 +02003478 raw_spin_lock_irqsave_rcu_node(rnp_up, flags);
Paul E. McKenneyb9585e92015-07-31 16:04:45 -07003479 if (rnp_up->expmaskinit)
3480 done = true;
3481 rnp_up->expmaskinit |= mask;
Boqun Feng67c583a72015-12-29 12:18:47 +08003482 raw_spin_unlock_irqrestore_rcu_node(rnp_up, flags);
Paul E. McKenneyb9585e92015-07-31 16:04:45 -07003483 if (done)
3484 break;
3485 mask = rnp_up->grpmask;
3486 rnp_up = rnp_up->parent;
3487 }
3488 }
3489}
3490
3491/*
3492 * Reset the ->expmask values in the rcu_node tree in preparation for
3493 * a new expedited grace period.
3494 */
3495static void __maybe_unused sync_exp_reset_tree(struct rcu_state *rsp)
3496{
3497 unsigned long flags;
3498 struct rcu_node *rnp;
3499
3500 sync_exp_reset_tree_hotplug(rsp);
3501 rcu_for_each_node_breadth_first(rsp, rnp) {
Peter Zijlstra2a67e742015-10-08 12:24:23 +02003502 raw_spin_lock_irqsave_rcu_node(rnp, flags);
Paul E. McKenneyb9585e92015-07-31 16:04:45 -07003503 WARN_ON_ONCE(rnp->expmask);
3504 rnp->expmask = rnp->expmaskinit;
Boqun Feng67c583a72015-12-29 12:18:47 +08003505 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
Paul E. McKenneyb9585e92015-07-31 16:04:45 -07003506 }
3507}
3508
3509/*
Paul E. McKenney8203d6d2015-08-02 13:53:17 -07003510 * Return non-zero if there is no RCU expedited grace period in progress
Paul E. McKenney7922cd02015-07-31 13:34:32 -07003511 * for the specified rcu_node structure, in other words, if all CPUs and
3512 * tasks covered by the specified rcu_node structure have done their bit
3513 * for the current expedited grace period. Works only for preemptible
3514 * RCU -- other RCU implementation use other means.
3515 *
3516 * Caller must hold the root rcu_node's exp_funnel_mutex.
3517 */
3518static int sync_rcu_preempt_exp_done(struct rcu_node *rnp)
3519{
Paul E. McKenney8203d6d2015-08-02 13:53:17 -07003520 return rnp->exp_tasks == NULL &&
Paul E. McKenney7922cd02015-07-31 13:34:32 -07003521 READ_ONCE(rnp->expmask) == 0;
3522}
3523
3524/*
3525 * Report the exit from RCU read-side critical section for the last task
3526 * that queued itself during or before the current expedited preemptible-RCU
3527 * grace period. This event is reported either to the rcu_node structure on
3528 * which the task was queued or to one of that rcu_node structure's ancestors,
3529 * recursively up the tree. (Calm down, calm down, we do the recursion
3530 * iteratively!)
3531 *
Paul E. McKenney8203d6d2015-08-02 13:53:17 -07003532 * Caller must hold the root rcu_node's exp_funnel_mutex and the
3533 * specified rcu_node structure's ->lock.
Paul E. McKenney7922cd02015-07-31 13:34:32 -07003534 */
Paul E. McKenney8203d6d2015-08-02 13:53:17 -07003535static void __rcu_report_exp_rnp(struct rcu_state *rsp, struct rcu_node *rnp,
3536 bool wake, unsigned long flags)
3537 __releases(rnp->lock)
Paul E. McKenney7922cd02015-07-31 13:34:32 -07003538{
Paul E. McKenney7922cd02015-07-31 13:34:32 -07003539 unsigned long mask;
3540
Paul E. McKenney7922cd02015-07-31 13:34:32 -07003541 for (;;) {
3542 if (!sync_rcu_preempt_exp_done(rnp)) {
Paul E. McKenney8203d6d2015-08-02 13:53:17 -07003543 if (!rnp->expmask)
3544 rcu_initiate_boost(rnp, flags);
3545 else
Boqun Feng67c583a72015-12-29 12:18:47 +08003546 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
Paul E. McKenney7922cd02015-07-31 13:34:32 -07003547 break;
3548 }
3549 if (rnp->parent == NULL) {
Boqun Feng67c583a72015-12-29 12:18:47 +08003550 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
Paul E. McKenney7922cd02015-07-31 13:34:32 -07003551 if (wake) {
3552 smp_mb(); /* EGP done before wake_up(). */
Paul Gortmakerabedf8e2016-02-19 09:46:41 +01003553 swake_up(&rsp->expedited_wq);
Paul E. McKenney7922cd02015-07-31 13:34:32 -07003554 }
3555 break;
3556 }
3557 mask = rnp->grpmask;
Boqun Feng67c583a72015-12-29 12:18:47 +08003558 raw_spin_unlock_rcu_node(rnp); /* irqs remain disabled */
Paul E. McKenney7922cd02015-07-31 13:34:32 -07003559 rnp = rnp->parent;
Peter Zijlstra2a67e742015-10-08 12:24:23 +02003560 raw_spin_lock_rcu_node(rnp); /* irqs already disabled */
Paul E. McKenney8203d6d2015-08-02 13:53:17 -07003561 WARN_ON_ONCE(!(rnp->expmask & mask));
Paul E. McKenney7922cd02015-07-31 13:34:32 -07003562 rnp->expmask &= ~mask;
3563 }
3564}
3565
Paul E. McKenney8203d6d2015-08-02 13:53:17 -07003566/*
3567 * Report expedited quiescent state for specified node. This is a
3568 * lock-acquisition wrapper function for __rcu_report_exp_rnp().
3569 *
3570 * Caller must hold the root rcu_node's exp_funnel_mutex.
3571 */
3572static void __maybe_unused rcu_report_exp_rnp(struct rcu_state *rsp,
3573 struct rcu_node *rnp, bool wake)
3574{
3575 unsigned long flags;
3576
Peter Zijlstra2a67e742015-10-08 12:24:23 +02003577 raw_spin_lock_irqsave_rcu_node(rnp, flags);
Paul E. McKenney8203d6d2015-08-02 13:53:17 -07003578 __rcu_report_exp_rnp(rsp, rnp, wake, flags);
3579}
3580
3581/*
3582 * Report expedited quiescent state for multiple CPUs, all covered by the
3583 * specified leaf rcu_node structure. Caller must hold the root
3584 * rcu_node's exp_funnel_mutex.
3585 */
3586static void rcu_report_exp_cpu_mult(struct rcu_state *rsp, struct rcu_node *rnp,
3587 unsigned long mask, bool wake)
3588{
3589 unsigned long flags;
3590
Peter Zijlstra2a67e742015-10-08 12:24:23 +02003591 raw_spin_lock_irqsave_rcu_node(rnp, flags);
Paul E. McKenney338b0f72015-09-03 00:45:02 -07003592 if (!(rnp->expmask & mask)) {
Boqun Feng67c583a72015-12-29 12:18:47 +08003593 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
Paul E. McKenney338b0f72015-09-03 00:45:02 -07003594 return;
3595 }
Paul E. McKenney8203d6d2015-08-02 13:53:17 -07003596 rnp->expmask &= ~mask;
3597 __rcu_report_exp_rnp(rsp, rnp, wake, flags); /* Releases rnp->lock. */
3598}
3599
3600/*
3601 * Report expedited quiescent state for specified rcu_data (CPU).
3602 * Caller must hold the root rcu_node's exp_funnel_mutex.
3603 */
Paul E. McKenney6587a232015-08-06 16:50:39 -07003604static void rcu_report_exp_rdp(struct rcu_state *rsp, struct rcu_data *rdp,
3605 bool wake)
Paul E. McKenney8203d6d2015-08-02 13:53:17 -07003606{
3607 rcu_report_exp_cpu_mult(rsp, rdp->mynode, rdp->grpmask, wake);
3608}
3609
Paul E. McKenney29fd9302015-06-25 19:03:16 -07003610/* Common code for synchronize_{rcu,sched}_expedited() work-done checking. */
3611static bool sync_exp_work_done(struct rcu_state *rsp, struct rcu_node *rnp,
Paul E. McKenney2cd6ffa2015-06-29 17:06:39 -07003612 struct rcu_data *rdp,
Paul E. McKenney29fd9302015-06-25 19:03:16 -07003613 atomic_long_t *stat, unsigned long s)
Paul E. McKenney385b73c2015-06-24 14:20:08 -07003614{
Paul E. McKenney28f00762015-06-25 15:00:58 -07003615 if (rcu_exp_gp_seq_done(rsp, s)) {
Paul E. McKenney385b73c2015-06-24 14:20:08 -07003616 if (rnp)
3617 mutex_unlock(&rnp->exp_funnel_mutex);
Paul E. McKenney2cd6ffa2015-06-29 17:06:39 -07003618 else if (rdp)
3619 mutex_unlock(&rdp->exp_funnel_mutex);
Paul E. McKenney385b73c2015-06-24 14:20:08 -07003620 /* Ensure test happens before caller kfree(). */
3621 smp_mb__before_atomic(); /* ^^^ */
3622 atomic_long_inc(stat);
Paul E. McKenney385b73c2015-06-24 14:20:08 -07003623 return true;
3624 }
3625 return false;
3626}
3627
Paul E. McKenneyb09e5f82015-06-25 16:30:54 -07003628/*
3629 * Funnel-lock acquisition for expedited grace periods. Returns a
3630 * pointer to the root rcu_node structure, or NULL if some other
3631 * task did the expedited grace period for us.
3632 */
3633static struct rcu_node *exp_funnel_lock(struct rcu_state *rsp, unsigned long s)
3634{
Paul E. McKenneydf5bd512015-10-01 10:26:24 -07003635 struct rcu_data *rdp = per_cpu_ptr(rsp->rda, raw_smp_processor_id());
Paul E. McKenneyb09e5f82015-06-25 16:30:54 -07003636 struct rcu_node *rnp0;
3637 struct rcu_node *rnp1 = NULL;
3638
3639 /*
Paul E. McKenneycdacbe12015-07-11 16:24:45 -07003640 * First try directly acquiring the root lock in order to reduce
3641 * latency in the common case where expedited grace periods are
3642 * rare. We check mutex_is_locked() to avoid pathological levels of
3643 * memory contention on ->exp_funnel_mutex in the heavy-load case.
3644 */
3645 rnp0 = rcu_get_root(rsp);
3646 if (!mutex_is_locked(&rnp0->exp_funnel_mutex)) {
3647 if (mutex_trylock(&rnp0->exp_funnel_mutex)) {
3648 if (sync_exp_work_done(rsp, rnp0, NULL,
Paul E. McKenneydf5bd512015-10-01 10:26:24 -07003649 &rdp->expedited_workdone0, s))
Paul E. McKenneycdacbe12015-07-11 16:24:45 -07003650 return NULL;
3651 return rnp0;
3652 }
3653 }
3654
3655 /*
Paul E. McKenneyb09e5f82015-06-25 16:30:54 -07003656 * Each pass through the following loop works its way
3657 * up the rcu_node tree, returning if others have done the
3658 * work or otherwise falls through holding the root rnp's
3659 * ->exp_funnel_mutex. The mapping from CPU to rcu_node structure
3660 * can be inexact, as it is just promoting locality and is not
3661 * strictly needed for correctness.
3662 */
Paul E. McKenneydf5bd512015-10-01 10:26:24 -07003663 if (sync_exp_work_done(rsp, NULL, NULL, &rdp->expedited_workdone1, s))
Paul E. McKenney2cd6ffa2015-06-29 17:06:39 -07003664 return NULL;
3665 mutex_lock(&rdp->exp_funnel_mutex);
3666 rnp0 = rdp->mynode;
Paul E. McKenneyb09e5f82015-06-25 16:30:54 -07003667 for (; rnp0 != NULL; rnp0 = rnp0->parent) {
Paul E. McKenney2cd6ffa2015-06-29 17:06:39 -07003668 if (sync_exp_work_done(rsp, rnp1, rdp,
Paul E. McKenneydf5bd512015-10-01 10:26:24 -07003669 &rdp->expedited_workdone2, s))
Paul E. McKenneyb09e5f82015-06-25 16:30:54 -07003670 return NULL;
3671 mutex_lock(&rnp0->exp_funnel_mutex);
3672 if (rnp1)
3673 mutex_unlock(&rnp1->exp_funnel_mutex);
Paul E. McKenney2cd6ffa2015-06-29 17:06:39 -07003674 else
3675 mutex_unlock(&rdp->exp_funnel_mutex);
Paul E. McKenneyb09e5f82015-06-25 16:30:54 -07003676 rnp1 = rnp0;
3677 }
Paul E. McKenney2cd6ffa2015-06-29 17:06:39 -07003678 if (sync_exp_work_done(rsp, rnp1, rdp,
Paul E. McKenneydf5bd512015-10-01 10:26:24 -07003679 &rdp->expedited_workdone3, s))
Paul E. McKenneyb09e5f82015-06-25 16:30:54 -07003680 return NULL;
3681 return rnp1;
3682}
3683
Paul E. McKenneycf3620a2015-06-30 11:14:32 -07003684/* Invoked on each online non-idle CPU for expedited quiescent state. */
Paul E. McKenney338b0f72015-09-03 00:45:02 -07003685static void sync_sched_exp_handler(void *data)
Paul E. McKenney3d3b7db2012-01-23 17:05:46 -08003686{
Paul E. McKenney338b0f72015-09-03 00:45:02 -07003687 struct rcu_data *rdp;
3688 struct rcu_node *rnp;
3689 struct rcu_state *rsp = data;
Paul E. McKenneyb09e5f82015-06-25 16:30:54 -07003690
Paul E. McKenney338b0f72015-09-03 00:45:02 -07003691 rdp = this_cpu_ptr(rsp->rda);
3692 rnp = rdp->mynode;
3693 if (!(READ_ONCE(rnp->expmask) & rdp->grpmask) ||
3694 __this_cpu_read(rcu_sched_data.cpu_no_qs.b.exp))
3695 return;
Paul E. McKenney6587a232015-08-06 16:50:39 -07003696 __this_cpu_write(rcu_sched_data.cpu_no_qs.b.exp, true);
3697 resched_cpu(smp_processor_id());
Paul E. McKenney3d3b7db2012-01-23 17:05:46 -08003698}
3699
Paul E. McKenney338b0f72015-09-03 00:45:02 -07003700/* Send IPI for expedited cleanup if needed at end of CPU-hotplug operation. */
3701static void sync_sched_exp_online_cleanup(int cpu)
3702{
3703 struct rcu_data *rdp;
3704 int ret;
3705 struct rcu_node *rnp;
3706 struct rcu_state *rsp = &rcu_sched_state;
3707
3708 rdp = per_cpu_ptr(rsp->rda, cpu);
3709 rnp = rdp->mynode;
3710 if (!(READ_ONCE(rnp->expmask) & rdp->grpmask))
3711 return;
3712 ret = smp_call_function_single(cpu, sync_sched_exp_handler, rsp, 0);
3713 WARN_ON_ONCE(ret);
3714}
3715
Paul E. McKenneybce5fa12015-08-05 16:03:54 -07003716/*
3717 * Select the nodes that the upcoming expedited grace period needs
3718 * to wait for.
3719 */
Paul E. McKenneydcdb8802015-08-15 19:00:31 -07003720static void sync_rcu_exp_select_cpus(struct rcu_state *rsp,
3721 smp_call_func_t func)
Paul E. McKenneybce5fa12015-08-05 16:03:54 -07003722{
3723 int cpu;
3724 unsigned long flags;
3725 unsigned long mask;
3726 unsigned long mask_ofl_test;
3727 unsigned long mask_ofl_ipi;
Paul E. McKenney6587a232015-08-06 16:50:39 -07003728 int ret;
Paul E. McKenneybce5fa12015-08-05 16:03:54 -07003729 struct rcu_node *rnp;
3730
3731 sync_exp_reset_tree(rsp);
3732 rcu_for_each_leaf_node(rsp, rnp) {
Peter Zijlstra2a67e742015-10-08 12:24:23 +02003733 raw_spin_lock_irqsave_rcu_node(rnp, flags);
Paul E. McKenneybce5fa12015-08-05 16:03:54 -07003734
3735 /* Each pass checks a CPU for identity, offline, and idle. */
3736 mask_ofl_test = 0;
3737 for (cpu = rnp->grplo; cpu <= rnp->grphi; cpu++) {
3738 struct rcu_data *rdp = per_cpu_ptr(rsp->rda, cpu);
3739 struct rcu_dynticks *rdtp = &per_cpu(rcu_dynticks, cpu);
3740
3741 if (raw_smp_processor_id() == cpu ||
Paul E. McKenneybce5fa12015-08-05 16:03:54 -07003742 !(atomic_add_return(0, &rdtp->dynticks) & 0x1))
3743 mask_ofl_test |= rdp->grpmask;
3744 }
3745 mask_ofl_ipi = rnp->expmask & ~mask_ofl_test;
3746
3747 /*
3748 * Need to wait for any blocked tasks as well. Note that
3749 * additional blocking tasks will also block the expedited
3750 * GP until such time as the ->expmask bits are cleared.
3751 */
3752 if (rcu_preempt_has_tasks(rnp))
3753 rnp->exp_tasks = rnp->blkd_tasks.next;
Boqun Feng67c583a72015-12-29 12:18:47 +08003754 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
Paul E. McKenneybce5fa12015-08-05 16:03:54 -07003755
3756 /* IPI the remaining CPUs for expedited quiescent state. */
3757 mask = 1;
3758 for (cpu = rnp->grplo; cpu <= rnp->grphi; cpu++, mask <<= 1) {
3759 if (!(mask_ofl_ipi & mask))
3760 continue;
Paul E. McKenney338b0f72015-09-03 00:45:02 -07003761retry_ipi:
Paul E. McKenneydcdb8802015-08-15 19:00:31 -07003762 ret = smp_call_function_single(cpu, func, rsp, 0);
Paul E. McKenney338b0f72015-09-03 00:45:02 -07003763 if (!ret) {
Paul E. McKenney6587a232015-08-06 16:50:39 -07003764 mask_ofl_ipi &= ~mask;
Paul E. McKenney1307f212015-09-29 15:29:21 -07003765 continue;
Paul E. McKenney338b0f72015-09-03 00:45:02 -07003766 }
Paul E. McKenney1307f212015-09-29 15:29:21 -07003767 /* Failed, raced with offline. */
3768 raw_spin_lock_irqsave_rcu_node(rnp, flags);
3769 if (cpu_online(cpu) &&
3770 (rnp->expmask & mask)) {
Boqun Feng67c583a72015-12-29 12:18:47 +08003771 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
Paul E. McKenney1307f212015-09-29 15:29:21 -07003772 schedule_timeout_uninterruptible(1);
3773 if (cpu_online(cpu) &&
3774 (rnp->expmask & mask))
3775 goto retry_ipi;
3776 raw_spin_lock_irqsave_rcu_node(rnp, flags);
3777 }
3778 if (!(rnp->expmask & mask))
3779 mask_ofl_ipi &= ~mask;
Boqun Feng67c583a72015-12-29 12:18:47 +08003780 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
Paul E. McKenneybce5fa12015-08-05 16:03:54 -07003781 }
3782 /* Report quiescent states for those that went offline. */
3783 mask_ofl_test |= mask_ofl_ipi;
3784 if (mask_ofl_test)
3785 rcu_report_exp_cpu_mult(rsp, rnp, mask_ofl_test, false);
3786 }
Paul E. McKenney3d3b7db2012-01-23 17:05:46 -08003787}
3788
Paul E. McKenneycf3620a2015-06-30 11:14:32 -07003789static void synchronize_sched_expedited_wait(struct rcu_state *rsp)
3790{
3791 int cpu;
3792 unsigned long jiffies_stall;
3793 unsigned long jiffies_start;
Paul E. McKenneybce5fa12015-08-05 16:03:54 -07003794 unsigned long mask;
Paul E. McKenney72611ab2015-11-17 13:25:21 -08003795 int ndetected;
Paul E. McKenneybce5fa12015-08-05 16:03:54 -07003796 struct rcu_node *rnp;
3797 struct rcu_node *rnp_root = rcu_get_root(rsp);
Paul E. McKenneycf3620a2015-06-30 11:14:32 -07003798 int ret;
3799
3800 jiffies_stall = rcu_jiffies_till_stall_check();
3801 jiffies_start = jiffies;
3802
3803 for (;;) {
Paul Gortmakerabedf8e2016-02-19 09:46:41 +01003804 ret = swait_event_timeout(
Paul E. McKenneycf3620a2015-06-30 11:14:32 -07003805 rsp->expedited_wq,
Paul E. McKenneybce5fa12015-08-05 16:03:54 -07003806 sync_rcu_preempt_exp_done(rnp_root),
Paul E. McKenneycf3620a2015-06-30 11:14:32 -07003807 jiffies_stall);
Paul E. McKenney73f36f92015-11-17 10:56:55 -08003808 if (ret > 0 || sync_rcu_preempt_exp_done(rnp_root))
Paul E. McKenneycf3620a2015-06-30 11:14:32 -07003809 return;
3810 if (ret < 0) {
3811 /* Hit a signal, disable CPU stall warnings. */
Paul Gortmakerabedf8e2016-02-19 09:46:41 +01003812 swait_event(rsp->expedited_wq,
Paul E. McKenneybce5fa12015-08-05 16:03:54 -07003813 sync_rcu_preempt_exp_done(rnp_root));
Paul E. McKenneycf3620a2015-06-30 11:14:32 -07003814 return;
3815 }
Paul E. McKenneyc5865632015-08-18 11:25:48 -07003816 pr_err("INFO: %s detected expedited stalls on CPUs/tasks: {",
Paul E. McKenneycf3620a2015-06-30 11:14:32 -07003817 rsp->name);
Paul E. McKenney72611ab2015-11-17 13:25:21 -08003818 ndetected = 0;
Paul E. McKenneybce5fa12015-08-05 16:03:54 -07003819 rcu_for_each_leaf_node(rsp, rnp) {
Paul E. McKenney72611ab2015-11-17 13:25:21 -08003820 ndetected = rcu_print_task_exp_stall(rnp);
Paul E. McKenneybce5fa12015-08-05 16:03:54 -07003821 mask = 1;
3822 for (cpu = rnp->grplo; cpu <= rnp->grphi; cpu++, mask <<= 1) {
Paul E. McKenney74611ec2015-08-18 10:20:43 -07003823 struct rcu_data *rdp;
Paul E. McKenneycf3620a2015-06-30 11:14:32 -07003824
Paul E. McKenneybce5fa12015-08-05 16:03:54 -07003825 if (!(rnp->expmask & mask))
3826 continue;
Paul E. McKenney72611ab2015-11-17 13:25:21 -08003827 ndetected++;
Paul E. McKenney74611ec2015-08-18 10:20:43 -07003828 rdp = per_cpu_ptr(rsp->rda, cpu);
3829 pr_cont(" %d-%c%c%c", cpu,
3830 "O."[cpu_online(cpu)],
3831 "o."[!!(rdp->grpmask & rnp->expmaskinit)],
3832 "N."[!!(rdp->grpmask & rnp->expmaskinitnext)]);
Paul E. McKenneybce5fa12015-08-05 16:03:54 -07003833 }
3834 mask <<= 1;
Paul E. McKenneycf3620a2015-06-30 11:14:32 -07003835 }
Paul E. McKenney72611ab2015-11-17 13:25:21 -08003836 pr_cont(" } %lu jiffies s: %lu root: %#lx/%c\n",
3837 jiffies - jiffies_start, rsp->expedited_sequence,
3838 rnp_root->expmask, ".T"[!!rnp_root->exp_tasks]);
3839 if (!ndetected) {
3840 pr_err("blocking rcu_node structures:");
3841 rcu_for_each_node_breadth_first(rsp, rnp) {
3842 if (rnp == rnp_root)
3843 continue; /* printed unconditionally */
3844 if (sync_rcu_preempt_exp_done(rnp))
3845 continue;
3846 pr_cont(" l=%u:%d-%d:%#lx/%c",
3847 rnp->level, rnp->grplo, rnp->grphi,
3848 rnp->expmask,
3849 ".T"[!!rnp->exp_tasks]);
3850 }
3851 pr_cont("\n");
3852 }
Paul E. McKenneybce5fa12015-08-05 16:03:54 -07003853 rcu_for_each_leaf_node(rsp, rnp) {
3854 mask = 1;
3855 for (cpu = rnp->grplo; cpu <= rnp->grphi; cpu++, mask <<= 1) {
3856 if (!(rnp->expmask & mask))
3857 continue;
3858 dump_cpu_task(cpu);
3859 }
Paul E. McKenneycf3620a2015-06-30 11:14:32 -07003860 }
3861 jiffies_stall = 3 * rcu_jiffies_till_stall_check() + 3;
3862 }
3863}
3864
Paul E. McKenney236fefa2012-01-31 14:00:41 -08003865/**
3866 * synchronize_sched_expedited - Brute-force RCU-sched grace period
Paul E. McKenney3d3b7db2012-01-23 17:05:46 -08003867 *
Paul E. McKenney236fefa2012-01-31 14:00:41 -08003868 * Wait for an RCU-sched grace period to elapse, but use a "big hammer"
3869 * approach to force the grace period to end quickly. This consumes
3870 * significant time on all CPUs and is unfriendly to real-time workloads,
3871 * so is thus not recommended for any sort of common-case code. In fact,
3872 * if you are using synchronize_sched_expedited() in a loop, please
3873 * restructure your code to batch your updates, and then use a single
3874 * synchronize_sched() instead.
3875 *
Paul E. McKenneyd6ada2c2015-06-24 10:46:30 -07003876 * This implementation can be thought of as an application of sequence
3877 * locking to expedited grace periods, but using the sequence counter to
3878 * determine when someone else has already done the work instead of for
Paul E. McKenney385b73c2015-06-24 14:20:08 -07003879 * retrying readers.
Paul E. McKenney3d3b7db2012-01-23 17:05:46 -08003880 */
3881void synchronize_sched_expedited(void)
3882{
Paul E. McKenney7fd0ddc2015-06-25 16:35:03 -07003883 unsigned long s;
Paul E. McKenneyb09e5f82015-06-25 16:30:54 -07003884 struct rcu_node *rnp;
Paul E. McKenney40694d62012-10-11 15:24:03 -07003885 struct rcu_state *rsp = &rcu_sched_state;
Paul E. McKenney3d3b7db2012-01-23 17:05:46 -08003886
Paul E. McKenney06f60de2015-09-29 08:15:52 -07003887 /* If only one CPU, this is automatically a grace period. */
3888 if (rcu_blocking_is_gp())
3889 return;
3890
Paul E. McKenney5a9be7c2015-11-24 15:44:06 -08003891 /* If expedited grace periods are prohibited, fall back to normal. */
3892 if (rcu_gp_is_normal()) {
3893 wait_rcu_gp(call_rcu_sched);
3894 return;
3895 }
3896
Paul E. McKenneyd6ada2c2015-06-24 10:46:30 -07003897 /* Take a snapshot of the sequence number. */
Paul E. McKenney28f00762015-06-25 15:00:58 -07003898 s = rcu_exp_gp_seq_snap(rsp);
Paul E. McKenney1924bcb2012-10-11 12:30:37 -07003899
Paul E. McKenneyb09e5f82015-06-25 16:30:54 -07003900 rnp = exp_funnel_lock(rsp, s);
Paul E. McKenney807226e2015-08-07 12:03:45 -07003901 if (rnp == NULL)
Paul E. McKenneyb09e5f82015-06-25 16:30:54 -07003902 return; /* Someone else did our work for us. */
Paul E. McKenney3d3b7db2012-01-23 17:05:46 -08003903
Paul E. McKenney28f00762015-06-25 15:00:58 -07003904 rcu_exp_gp_seq_start(rsp);
Paul E. McKenney338b0f72015-09-03 00:45:02 -07003905 sync_rcu_exp_select_cpus(rsp, sync_sched_exp_handler);
Paul E. McKenneybce5fa12015-08-05 16:03:54 -07003906 synchronize_sched_expedited_wait(rsp);
Peter Zijlstra3a6d7c62015-06-25 11:27:10 -07003907
Paul E. McKenney28f00762015-06-25 15:00:58 -07003908 rcu_exp_gp_seq_end(rsp);
Paul E. McKenneyb09e5f82015-06-25 16:30:54 -07003909 mutex_unlock(&rnp->exp_funnel_mutex);
Paul E. McKenney3d3b7db2012-01-23 17:05:46 -08003910}
3911EXPORT_SYMBOL_GPL(synchronize_sched_expedited);
3912
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003913/*
3914 * Check to see if there is any immediate RCU-related work to be done
3915 * by the current CPU, for the specified type of RCU, returning 1 if so.
3916 * The checks are in order of increasing expense: checks that can be
3917 * carried out against CPU-local state are performed first. However,
3918 * we must check for CPU stalls first, else we might not get a chance.
3919 */
3920static int __rcu_pending(struct rcu_state *rsp, struct rcu_data *rdp)
3921{
Paul E. McKenney2f51f982009-11-13 19:51:39 -08003922 struct rcu_node *rnp = rdp->mynode;
3923
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003924 rdp->n_rcu_pending++;
3925
3926 /* Check for CPU stalls, if enabled. */
3927 check_cpu_stall(rsp, rdp);
3928
Paul E. McKenneya0969322013-11-08 09:03:10 -08003929 /* Is this CPU a NO_HZ_FULL CPU that should ignore RCU? */
3930 if (rcu_nohz_full_cpu(rsp))
3931 return 0;
3932
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003933 /* Is the RCU core waiting for a quiescent state from this CPU? */
Paul E. McKenney5c51dd72011-08-04 06:59:03 -07003934 if (rcu_scheduler_fully_active &&
Paul E. McKenney5b74c452015-08-06 15:16:57 -07003935 rdp->core_needs_qs && rdp->cpu_no_qs.b.norm &&
Paul E. McKenney5cd37192014-12-13 20:32:04 -08003936 rdp->rcu_qs_ctr_snap == __this_cpu_read(rcu_qs_ctr)) {
Paul E. McKenney97c668b2015-08-06 11:31:51 -07003937 rdp->n_rp_core_needs_qs++;
3938 } else if (rdp->core_needs_qs &&
Paul E. McKenney5b74c452015-08-06 15:16:57 -07003939 (!rdp->cpu_no_qs.b.norm ||
Paul E. McKenney5cd37192014-12-13 20:32:04 -08003940 rdp->rcu_qs_ctr_snap != __this_cpu_read(rcu_qs_ctr))) {
Paul E. McKenneyd21670a2010-04-14 17:39:26 -07003941 rdp->n_rp_report_qs++;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003942 return 1;
Paul E. McKenney7ba5c842009-04-13 21:31:17 -07003943 }
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003944
3945 /* Does this CPU have callbacks ready to invoke? */
Paul E. McKenney7ba5c842009-04-13 21:31:17 -07003946 if (cpu_has_callbacks_ready_to_invoke(rdp)) {
3947 rdp->n_rp_cb_ready++;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003948 return 1;
Paul E. McKenney7ba5c842009-04-13 21:31:17 -07003949 }
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003950
3951 /* Has RCU gone idle with this CPU needing another grace period? */
Paul E. McKenney7ba5c842009-04-13 21:31:17 -07003952 if (cpu_needs_another_gp(rsp, rdp)) {
3953 rdp->n_rp_cpu_needs_gp++;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003954 return 1;
Paul E. McKenney7ba5c842009-04-13 21:31:17 -07003955 }
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003956
3957 /* Has another RCU grace period completed? */
Paul E. McKenney7d0ae802015-03-03 14:57:58 -08003958 if (READ_ONCE(rnp->completed) != rdp->completed) { /* outside lock */
Paul E. McKenney7ba5c842009-04-13 21:31:17 -07003959 rdp->n_rp_gp_completed++;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003960 return 1;
Paul E. McKenney7ba5c842009-04-13 21:31:17 -07003961 }
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003962
3963 /* Has a new RCU grace period started? */
Paul E. McKenney7d0ae802015-03-03 14:57:58 -08003964 if (READ_ONCE(rnp->gpnum) != rdp->gpnum ||
3965 unlikely(READ_ONCE(rdp->gpwrap))) { /* outside lock */
Paul E. McKenney7ba5c842009-04-13 21:31:17 -07003966 rdp->n_rp_gp_started++;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003967 return 1;
Paul E. McKenney7ba5c842009-04-13 21:31:17 -07003968 }
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003969
Paul E. McKenney96d3fd02013-10-04 14:33:34 -07003970 /* Does this CPU need a deferred NOCB wakeup? */
3971 if (rcu_nocb_need_deferred_wakeup(rdp)) {
3972 rdp->n_rp_nocb_defer_wakeup++;
3973 return 1;
3974 }
3975
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003976 /* nothing to do */
Paul E. McKenney7ba5c842009-04-13 21:31:17 -07003977 rdp->n_rp_need_nothing++;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003978 return 0;
3979}
3980
3981/*
3982 * Check to see if there is any immediate RCU-related work to be done
3983 * by the current CPU, returning 1 if so. This function is part of the
3984 * RCU implementation; it is -not- an exported member of the RCU API.
3985 */
Paul E. McKenneye3950ec2014-10-21 08:03:57 -07003986static int rcu_pending(void)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003987{
Paul E. McKenney6ce75a22012-06-12 11:01:13 -07003988 struct rcu_state *rsp;
3989
3990 for_each_rcu_flavor(rsp)
Paul E. McKenneye3950ec2014-10-21 08:03:57 -07003991 if (__rcu_pending(rsp, this_cpu_ptr(rsp->rda)))
Paul E. McKenney6ce75a22012-06-12 11:01:13 -07003992 return 1;
3993 return 0;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003994}
3995
3996/*
Paul E. McKenneyc0f4dfd2012-12-28 11:30:36 -08003997 * Return true if the specified CPU has any callback. If all_lazy is
3998 * non-NULL, store an indication of whether all callbacks are lazy.
3999 * (If there are no callbacks, all of them are deemed to be lazy.)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01004000 */
Nicholas Mc Guire82072c42015-05-11 18:12:27 +02004001static bool __maybe_unused rcu_cpu_has_callbacks(bool *all_lazy)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01004002{
Paul E. McKenneyc0f4dfd2012-12-28 11:30:36 -08004003 bool al = true;
4004 bool hc = false;
4005 struct rcu_data *rdp;
Paul E. McKenney6ce75a22012-06-12 11:01:13 -07004006 struct rcu_state *rsp;
4007
Paul E. McKenneyc0f4dfd2012-12-28 11:30:36 -08004008 for_each_rcu_flavor(rsp) {
Paul E. McKenneyaa6da512014-10-21 13:23:08 -07004009 rdp = this_cpu_ptr(rsp->rda);
Paul E. McKenney69c8d282013-09-03 09:52:20 -07004010 if (!rdp->nxtlist)
4011 continue;
4012 hc = true;
4013 if (rdp->qlen != rdp->qlen_lazy || !all_lazy) {
Paul E. McKenneyc0f4dfd2012-12-28 11:30:36 -08004014 al = false;
Paul E. McKenney69c8d282013-09-03 09:52:20 -07004015 break;
4016 }
Paul E. McKenneyc0f4dfd2012-12-28 11:30:36 -08004017 }
4018 if (all_lazy)
4019 *all_lazy = al;
4020 return hc;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01004021}
4022
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08004023/*
Paul E. McKenneya83eff02012-05-23 18:47:05 -07004024 * Helper function for _rcu_barrier() tracing. If tracing is disabled,
4025 * the compiler is expected to optimize this away.
4026 */
Steven Rostedt (Red Hat)e66c33d2013-07-12 16:50:28 -04004027static void _rcu_barrier_trace(struct rcu_state *rsp, const char *s,
Paul E. McKenneya83eff02012-05-23 18:47:05 -07004028 int cpu, unsigned long done)
4029{
4030 trace_rcu_barrier(rsp->name, s, cpu,
4031 atomic_read(&rsp->barrier_cpu_count), done);
4032}
4033
4034/*
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08004035 * RCU callback function for _rcu_barrier(). If we are last, wake
4036 * up the task executing _rcu_barrier().
4037 */
Paul E. McKenney24ebbca2012-05-29 00:34:56 -07004038static void rcu_barrier_callback(struct rcu_head *rhp)
Paul E. McKenneyd0ec7742009-10-06 21:48:16 -07004039{
Paul E. McKenney24ebbca2012-05-29 00:34:56 -07004040 struct rcu_data *rdp = container_of(rhp, struct rcu_data, barrier_head);
4041 struct rcu_state *rsp = rdp->rsp;
4042
Paul E. McKenneya83eff02012-05-23 18:47:05 -07004043 if (atomic_dec_and_test(&rsp->barrier_cpu_count)) {
Paul E. McKenney4f525a52015-06-26 11:20:00 -07004044 _rcu_barrier_trace(rsp, "LastCB", -1, rsp->barrier_sequence);
Paul E. McKenney7db74df2012-05-29 03:03:37 -07004045 complete(&rsp->barrier_completion);
Paul E. McKenneya83eff02012-05-23 18:47:05 -07004046 } else {
Paul E. McKenney4f525a52015-06-26 11:20:00 -07004047 _rcu_barrier_trace(rsp, "CB", -1, rsp->barrier_sequence);
Paul E. McKenneya83eff02012-05-23 18:47:05 -07004048 }
Paul E. McKenneyd0ec7742009-10-06 21:48:16 -07004049}
4050
4051/*
4052 * Called with preemption disabled, and from cross-cpu IRQ context.
4053 */
4054static void rcu_barrier_func(void *type)
4055{
Paul E. McKenney037b64e2012-05-28 23:26:01 -07004056 struct rcu_state *rsp = type;
Christoph Lameterfa07a582014-04-15 12:20:12 -07004057 struct rcu_data *rdp = raw_cpu_ptr(rsp->rda);
Paul E. McKenneyd0ec7742009-10-06 21:48:16 -07004058
Paul E. McKenney4f525a52015-06-26 11:20:00 -07004059 _rcu_barrier_trace(rsp, "IRQ", -1, rsp->barrier_sequence);
Paul E. McKenney24ebbca2012-05-29 00:34:56 -07004060 atomic_inc(&rsp->barrier_cpu_count);
Paul E. McKenney06668ef2012-05-28 23:57:46 -07004061 rsp->call(&rdp->barrier_head, rcu_barrier_callback);
Paul E. McKenneyd0ec7742009-10-06 21:48:16 -07004062}
4063
Paul E. McKenneyd0ec7742009-10-06 21:48:16 -07004064/*
4065 * Orchestrate the specified type of RCU barrier, waiting for all
4066 * RCU callbacks of the specified type to complete.
4067 */
Paul E. McKenney037b64e2012-05-28 23:26:01 -07004068static void _rcu_barrier(struct rcu_state *rsp)
Paul E. McKenneyd0ec7742009-10-06 21:48:16 -07004069{
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08004070 int cpu;
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08004071 struct rcu_data *rdp;
Paul E. McKenney4f525a52015-06-26 11:20:00 -07004072 unsigned long s = rcu_seq_snap(&rsp->barrier_sequence);
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08004073
Paul E. McKenney4f525a52015-06-26 11:20:00 -07004074 _rcu_barrier_trace(rsp, "Begin", -1, s);
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08004075
Paul E. McKenneye74f4c42009-10-06 21:48:17 -07004076 /* Take mutex to serialize concurrent rcu_barrier() requests. */
Paul E. McKenney7be7f0b2012-05-29 05:18:53 -07004077 mutex_lock(&rsp->barrier_mutex);
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08004078
Paul E. McKenney4f525a52015-06-26 11:20:00 -07004079 /* Did someone else do our work for us? */
4080 if (rcu_seq_done(&rsp->barrier_sequence, s)) {
4081 _rcu_barrier_trace(rsp, "EarlyExit", -1, rsp->barrier_sequence);
Paul E. McKenneycf3a9c42012-05-29 14:56:46 -07004082 smp_mb(); /* caller's subsequent code after above check. */
4083 mutex_unlock(&rsp->barrier_mutex);
4084 return;
4085 }
4086
Paul E. McKenney4f525a52015-06-26 11:20:00 -07004087 /* Mark the start of the barrier operation. */
4088 rcu_seq_start(&rsp->barrier_sequence);
4089 _rcu_barrier_trace(rsp, "Inc1", -1, rsp->barrier_sequence);
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08004090
Paul E. McKenneyd0ec7742009-10-06 21:48:16 -07004091 /*
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08004092 * Initialize the count to one rather than to zero in order to
4093 * avoid a too-soon return to zero in case of a short grace period
Paul E. McKenney1331e7a2012-08-02 17:43:50 -07004094 * (or preemption of this task). Exclude CPU-hotplug operations
4095 * to ensure that no offline CPU has callbacks queued.
Paul E. McKenneyd0ec7742009-10-06 21:48:16 -07004096 */
Paul E. McKenney7db74df2012-05-29 03:03:37 -07004097 init_completion(&rsp->barrier_completion);
Paul E. McKenney24ebbca2012-05-29 00:34:56 -07004098 atomic_set(&rsp->barrier_cpu_count, 1);
Paul E. McKenney1331e7a2012-08-02 17:43:50 -07004099 get_online_cpus();
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08004100
4101 /*
Paul E. McKenney1331e7a2012-08-02 17:43:50 -07004102 * Force each CPU with callbacks to register a new callback.
4103 * When that callback is invoked, we will know that all of the
4104 * corresponding CPU's preceding callbacks have been invoked.
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08004105 */
Paul E. McKenney3fbfbf72012-08-19 21:35:53 -07004106 for_each_possible_cpu(cpu) {
Frederic Weisbeckerd1e43fa2013-03-26 23:47:24 +01004107 if (!cpu_online(cpu) && !rcu_is_nocb_cpu(cpu))
Paul E. McKenney3fbfbf72012-08-19 21:35:53 -07004108 continue;
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08004109 rdp = per_cpu_ptr(rsp->rda, cpu);
Frederic Weisbeckerd1e43fa2013-03-26 23:47:24 +01004110 if (rcu_is_nocb_cpu(cpu)) {
Paul E. McKenneyd7e29932014-10-27 09:15:54 -07004111 if (!rcu_nocb_cpu_needs_barrier(rsp, cpu)) {
4112 _rcu_barrier_trace(rsp, "OfflineNoCB", cpu,
Paul E. McKenney4f525a52015-06-26 11:20:00 -07004113 rsp->barrier_sequence);
Paul E. McKenneyd7e29932014-10-27 09:15:54 -07004114 } else {
4115 _rcu_barrier_trace(rsp, "OnlineNoCB", cpu,
Paul E. McKenney4f525a52015-06-26 11:20:00 -07004116 rsp->barrier_sequence);
Paul E. McKenney41050a02014-12-18 12:31:27 -08004117 smp_mb__before_atomic();
Paul E. McKenneyd7e29932014-10-27 09:15:54 -07004118 atomic_inc(&rsp->barrier_cpu_count);
4119 __call_rcu(&rdp->barrier_head,
4120 rcu_barrier_callback, rsp, cpu, 0);
4121 }
Paul E. McKenney7d0ae802015-03-03 14:57:58 -08004122 } else if (READ_ONCE(rdp->qlen)) {
Paul E. McKenneya83eff02012-05-23 18:47:05 -07004123 _rcu_barrier_trace(rsp, "OnlineQ", cpu,
Paul E. McKenney4f525a52015-06-26 11:20:00 -07004124 rsp->barrier_sequence);
Paul E. McKenney037b64e2012-05-28 23:26:01 -07004125 smp_call_function_single(cpu, rcu_barrier_func, rsp, 1);
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08004126 } else {
Paul E. McKenneya83eff02012-05-23 18:47:05 -07004127 _rcu_barrier_trace(rsp, "OnlineNQ", cpu,
Paul E. McKenney4f525a52015-06-26 11:20:00 -07004128 rsp->barrier_sequence);
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08004129 }
4130 }
Paul E. McKenney1331e7a2012-08-02 17:43:50 -07004131 put_online_cpus();
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08004132
4133 /*
4134 * Now that we have an rcu_barrier_callback() callback on each
4135 * CPU, and thus each counted, remove the initial count.
4136 */
Paul E. McKenney24ebbca2012-05-29 00:34:56 -07004137 if (atomic_dec_and_test(&rsp->barrier_cpu_count))
Paul E. McKenney7db74df2012-05-29 03:03:37 -07004138 complete(&rsp->barrier_completion);
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08004139
4140 /* Wait for all rcu_barrier_callback() callbacks to be invoked. */
Paul E. McKenney7db74df2012-05-29 03:03:37 -07004141 wait_for_completion(&rsp->barrier_completion);
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08004142
Paul E. McKenney4f525a52015-06-26 11:20:00 -07004143 /* Mark the end of the barrier operation. */
4144 _rcu_barrier_trace(rsp, "Inc2", -1, rsp->barrier_sequence);
4145 rcu_seq_end(&rsp->barrier_sequence);
4146
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08004147 /* Other rcu_barrier() invocations can now safely proceed. */
Paul E. McKenney7be7f0b2012-05-29 05:18:53 -07004148 mutex_unlock(&rsp->barrier_mutex);
Paul E. McKenneyd0ec7742009-10-06 21:48:16 -07004149}
4150
4151/**
Paul E. McKenneyd0ec7742009-10-06 21:48:16 -07004152 * rcu_barrier_bh - Wait until all in-flight call_rcu_bh() callbacks complete.
4153 */
4154void rcu_barrier_bh(void)
4155{
Paul E. McKenney037b64e2012-05-28 23:26:01 -07004156 _rcu_barrier(&rcu_bh_state);
Paul E. McKenneyd0ec7742009-10-06 21:48:16 -07004157}
4158EXPORT_SYMBOL_GPL(rcu_barrier_bh);
4159
4160/**
4161 * rcu_barrier_sched - Wait for in-flight call_rcu_sched() callbacks.
4162 */
4163void rcu_barrier_sched(void)
4164{
Paul E. McKenney037b64e2012-05-28 23:26:01 -07004165 _rcu_barrier(&rcu_sched_state);
Paul E. McKenneyd0ec7742009-10-06 21:48:16 -07004166}
4167EXPORT_SYMBOL_GPL(rcu_barrier_sched);
4168
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01004169/*
Paul E. McKenney0aa04b02015-01-23 21:52:37 -08004170 * Propagate ->qsinitmask bits up the rcu_node tree to account for the
4171 * first CPU in a given leaf rcu_node structure coming online. The caller
4172 * must hold the corresponding leaf rcu_node ->lock with interrrupts
4173 * disabled.
4174 */
4175static void rcu_init_new_rnp(struct rcu_node *rnp_leaf)
4176{
4177 long mask;
4178 struct rcu_node *rnp = rnp_leaf;
4179
4180 for (;;) {
4181 mask = rnp->grpmask;
4182 rnp = rnp->parent;
4183 if (rnp == NULL)
4184 return;
Paul E. McKenney6cf10082015-10-08 15:36:54 -07004185 raw_spin_lock_rcu_node(rnp); /* Interrupts already disabled. */
Paul E. McKenney0aa04b02015-01-23 21:52:37 -08004186 rnp->qsmaskinit |= mask;
Boqun Feng67c583a72015-12-29 12:18:47 +08004187 raw_spin_unlock_rcu_node(rnp); /* Interrupts remain disabled. */
Paul E. McKenney0aa04b02015-01-23 21:52:37 -08004188 }
4189}
4190
4191/*
Paul E. McKenney27569622009-08-15 09:53:46 -07004192 * Do boot-time initialization of a CPU's per-CPU RCU data.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01004193 */
Paul E. McKenney27569622009-08-15 09:53:46 -07004194static void __init
4195rcu_boot_init_percpu_data(int cpu, struct rcu_state *rsp)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01004196{
4197 unsigned long flags;
Lai Jiangshan394f99a2010-06-28 16:25:04 +08004198 struct rcu_data *rdp = per_cpu_ptr(rsp->rda, cpu);
Paul E. McKenney27569622009-08-15 09:53:46 -07004199 struct rcu_node *rnp = rcu_get_root(rsp);
4200
4201 /* Set up local state, ensuring consistent view of global state. */
Paul E. McKenney6cf10082015-10-08 15:36:54 -07004202 raw_spin_lock_irqsave_rcu_node(rnp, flags);
Paul E. McKenney27569622009-08-15 09:53:46 -07004203 rdp->grpmask = 1UL << (cpu - rdp->mynode->grplo);
Paul E. McKenney27569622009-08-15 09:53:46 -07004204 rdp->dynticks = &per_cpu(rcu_dynticks, cpu);
Paul E. McKenney29e37d82011-11-17 16:55:56 -08004205 WARN_ON_ONCE(rdp->dynticks->dynticks_nesting != DYNTICK_TASK_EXIT_IDLE);
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -07004206 WARN_ON_ONCE(atomic_read(&rdp->dynticks->dynticks) != 1);
Paul E. McKenney27569622009-08-15 09:53:46 -07004207 rdp->cpu = cpu;
Paul E. McKenneyd4c08f22011-06-25 06:36:56 -07004208 rdp->rsp = rsp;
Paul E. McKenney2cd6ffa2015-06-29 17:06:39 -07004209 mutex_init(&rdp->exp_funnel_mutex);
Paul E. McKenney3fbfbf72012-08-19 21:35:53 -07004210 rcu_boot_init_nocb_percpu_data(rdp);
Boqun Feng67c583a72015-12-29 12:18:47 +08004211 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
Paul E. McKenney27569622009-08-15 09:53:46 -07004212}
4213
4214/*
4215 * Initialize a CPU's per-CPU RCU data. Note that only one online or
4216 * offline event can be happening at a given time. Note also that we
4217 * can accept some slop in the rsp->completed access due to the fact
4218 * that this CPU cannot possibly have any RCU callbacks in flight yet.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01004219 */
Paul Gortmaker49fb4c62013-06-19 14:52:21 -04004220static void
Iulia Manda9b671222014-03-11 13:18:22 +02004221rcu_init_percpu_data(int cpu, struct rcu_state *rsp)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01004222{
4223 unsigned long flags;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01004224 unsigned long mask;
Lai Jiangshan394f99a2010-06-28 16:25:04 +08004225 struct rcu_data *rdp = per_cpu_ptr(rsp->rda, cpu);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01004226 struct rcu_node *rnp = rcu_get_root(rsp);
4227
4228 /* Set up local state, ensuring consistent view of global state. */
Paul E. McKenney6cf10082015-10-08 15:36:54 -07004229 raw_spin_lock_irqsave_rcu_node(rnp, flags);
Paul E. McKenney37c72e52009-10-14 10:15:55 -07004230 rdp->qlen_last_fqs_check = 0;
4231 rdp->n_force_qs_snap = rsp->n_force_qs;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01004232 rdp->blimit = blimit;
Paul E. McKenney39c8d312015-01-20 23:42:38 -08004233 if (!rdp->nxtlist)
4234 init_callback_list(rdp); /* Re-enable callbacks on this CPU. */
Paul E. McKenney29e37d82011-11-17 16:55:56 -08004235 rdp->dynticks->dynticks_nesting = DYNTICK_TASK_EXIT_IDLE;
Paul E. McKenney23332102013-06-21 12:34:33 -07004236 rcu_sysidle_init_percpu_data(rdp->dynticks);
Paul E. McKenneyc92b1312011-11-23 13:38:58 -08004237 atomic_set(&rdp->dynticks->dynticks,
4238 (atomic_read(&rdp->dynticks->dynticks) & ~0x1) + 1);
Boqun Feng67c583a72015-12-29 12:18:47 +08004239 raw_spin_unlock_rcu_node(rnp); /* irqs remain disabled. */
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01004240
Paul E. McKenney0aa04b02015-01-23 21:52:37 -08004241 /*
4242 * Add CPU to leaf rcu_node pending-online bitmask. Any needed
4243 * propagation up the rcu_node tree will happen at the beginning
4244 * of the next grace period.
4245 */
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01004246 rnp = rdp->mynode;
4247 mask = rdp->grpmask;
Peter Zijlstra2a67e742015-10-08 12:24:23 +02004248 raw_spin_lock_rcu_node(rnp); /* irqs already disabled. */
Paul E. McKenney0aa04b02015-01-23 21:52:37 -08004249 rnp->qsmaskinitnext |= mask;
Paul E. McKenneyb9585e92015-07-31 16:04:45 -07004250 rnp->expmaskinitnext |= mask;
4251 if (!rdp->beenonline)
4252 WRITE_ONCE(rsp->ncpus, READ_ONCE(rsp->ncpus) + 1);
4253 rdp->beenonline = true; /* We have now been online. */
Paul E. McKenney0aa04b02015-01-23 21:52:37 -08004254 rdp->gpnum = rnp->completed; /* Make CPU later note any new GP. */
4255 rdp->completed = rnp->completed;
Paul E. McKenney5b74c452015-08-06 15:16:57 -07004256 rdp->cpu_no_qs.b.norm = true;
Paul E. McKenneya738eec2015-03-10 14:53:29 -07004257 rdp->rcu_qs_ctr_snap = per_cpu(rcu_qs_ctr, cpu);
Paul E. McKenney97c668b2015-08-06 11:31:51 -07004258 rdp->core_needs_qs = false;
Paul E. McKenney0aa04b02015-01-23 21:52:37 -08004259 trace_rcu_grace_period(rsp->name, rdp->gpnum, TPS("cpuonl"));
Boqun Feng67c583a72015-12-29 12:18:47 +08004260 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01004261}
4262
Paul Gortmaker49fb4c62013-06-19 14:52:21 -04004263static void rcu_prepare_cpu(int cpu)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01004264{
Paul E. McKenney6ce75a22012-06-12 11:01:13 -07004265 struct rcu_state *rsp;
4266
4267 for_each_rcu_flavor(rsp)
Iulia Manda9b671222014-03-11 13:18:22 +02004268 rcu_init_percpu_data(cpu, rsp);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01004269}
4270
Thomas Gleixner27d50c72016-02-26 18:43:44 +00004271#ifdef CONFIG_HOTPLUG_CPU
4272/*
4273 * The CPU is exiting the idle loop into the arch_cpu_idle_dead()
4274 * function. We now remove it from the rcu_node tree's ->qsmaskinit
4275 * bit masks.
Linus Torvalds710d60c2016-03-15 13:50:29 -07004276 * The CPU is exiting the idle loop into the arch_cpu_idle_dead()
4277 * function. We now remove it from the rcu_node tree's ->qsmaskinit
4278 * bit masks.
Thomas Gleixner27d50c72016-02-26 18:43:44 +00004279 */
4280static void rcu_cleanup_dying_idle_cpu(int cpu, struct rcu_state *rsp)
4281{
4282 unsigned long flags;
4283 unsigned long mask;
4284 struct rcu_data *rdp = per_cpu_ptr(rsp->rda, cpu);
4285 struct rcu_node *rnp = rdp->mynode; /* Outgoing CPU's rdp & rnp. */
4286
4287 if (!IS_ENABLED(CONFIG_HOTPLUG_CPU))
4288 return;
4289
4290 /* Remove outgoing CPU from mask in the leaf rcu_node structure. */
4291 mask = rdp->grpmask;
4292 raw_spin_lock_irqsave_rcu_node(rnp, flags); /* Enforce GP memory-order guarantee. */
4293 rnp->qsmaskinitnext &= ~mask;
Linus Torvalds710d60c2016-03-15 13:50:29 -07004294 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
Thomas Gleixner27d50c72016-02-26 18:43:44 +00004295}
4296
4297void rcu_report_dead(unsigned int cpu)
4298{
4299 struct rcu_state *rsp;
4300
4301 /* QS for any half-done expedited RCU-sched GP. */
4302 preempt_disable();
4303 rcu_report_exp_rdp(&rcu_sched_state,
4304 this_cpu_ptr(rcu_sched_state.rda), true);
4305 preempt_enable();
4306 for_each_rcu_flavor(rsp)
4307 rcu_cleanup_dying_idle_cpu(cpu, rsp);
4308}
4309#endif
4310
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01004311/*
Paul E. McKenneyf41d9112009-08-22 13:56:52 -07004312 * Handle CPU online/offline notification events.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01004313 */
Paul E. McKenney88428cc52015-01-28 14:42:09 -08004314int rcu_cpu_notify(struct notifier_block *self,
4315 unsigned long action, void *hcpu)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01004316{
4317 long cpu = (long)hcpu;
Uma Sharmae5341652014-03-23 22:32:09 -07004318 struct rcu_data *rdp = per_cpu_ptr(rcu_state_p->rda, cpu);
Paul E. McKenneya26ac242011-01-12 14:10:23 -08004319 struct rcu_node *rnp = rdp->mynode;
Paul E. McKenney6ce75a22012-06-12 11:01:13 -07004320 struct rcu_state *rsp;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01004321
4322 switch (action) {
4323 case CPU_UP_PREPARE:
4324 case CPU_UP_PREPARE_FROZEN:
Peter Zijlstrad72bce02011-05-30 13:34:51 +02004325 rcu_prepare_cpu(cpu);
4326 rcu_prepare_kthreads(cpu);
Paul E. McKenney35ce7f22014-07-11 11:30:24 -07004327 rcu_spawn_all_nocb_kthreads(cpu);
Paul E. McKenneya26ac242011-01-12 14:10:23 -08004328 break;
4329 case CPU_ONLINE:
Paul E. McKenney0f962a52011-04-14 12:13:53 -07004330 case CPU_DOWN_FAILED:
Paul E. McKenney338b0f72015-09-03 00:45:02 -07004331 sync_sched_exp_online_cleanup(cpu);
Thomas Gleixner5d01bbd2012-07-16 10:42:35 +00004332 rcu_boost_kthread_setaffinity(rnp, -1);
Paul E. McKenney0f962a52011-04-14 12:13:53 -07004333 break;
4334 case CPU_DOWN_PREPARE:
Paul E. McKenney34ed62462013-01-07 13:37:42 -08004335 rcu_boost_kthread_setaffinity(rnp, cpu);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01004336 break;
Paul E. McKenneyd0ec7742009-10-06 21:48:16 -07004337 case CPU_DYING:
4338 case CPU_DYING_FROZEN:
Paul E. McKenney6ce75a22012-06-12 11:01:13 -07004339 for_each_rcu_flavor(rsp)
4340 rcu_cleanup_dying_cpu(rsp);
Paul E. McKenneyd0ec7742009-10-06 21:48:16 -07004341 break;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01004342 case CPU_DEAD:
4343 case CPU_DEAD_FROZEN:
4344 case CPU_UP_CANCELED:
4345 case CPU_UP_CANCELED_FROZEN:
Paul E. McKenney776d6802014-10-23 10:50:41 -07004346 for_each_rcu_flavor(rsp) {
Paul E. McKenney6ce75a22012-06-12 11:01:13 -07004347 rcu_cleanup_dead_cpu(cpu, rsp);
Paul E. McKenney776d6802014-10-23 10:50:41 -07004348 do_nocb_deferred_wakeup(per_cpu_ptr(rsp->rda, cpu));
4349 }
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01004350 break;
4351 default:
4352 break;
4353 }
Paul E. McKenney34ed62462013-01-07 13:37:42 -08004354 return NOTIFY_OK;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01004355}
4356
Borislav Petkovd1d74d12013-04-22 00:12:42 +02004357static int rcu_pm_notify(struct notifier_block *self,
4358 unsigned long action, void *hcpu)
4359{
4360 switch (action) {
4361 case PM_HIBERNATION_PREPARE:
4362 case PM_SUSPEND_PREPARE:
4363 if (nr_cpu_ids <= 256) /* Expediting bad for large systems. */
Paul E. McKenney5afff482015-02-18 16:39:09 -08004364 rcu_expedite_gp();
Borislav Petkovd1d74d12013-04-22 00:12:42 +02004365 break;
4366 case PM_POST_HIBERNATION:
4367 case PM_POST_SUSPEND:
Paul E. McKenney5afff482015-02-18 16:39:09 -08004368 if (nr_cpu_ids <= 256) /* Expediting bad for large systems. */
4369 rcu_unexpedite_gp();
Borislav Petkovd1d74d12013-04-22 00:12:42 +02004370 break;
4371 default:
4372 break;
4373 }
4374 return NOTIFY_OK;
4375}
4376
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01004377/*
Paul E. McKenney9386c0b2014-07-13 12:00:53 -07004378 * Spawn the kthreads that handle each RCU flavor's grace periods.
Paul E. McKenneyb3dbec72012-06-18 18:36:08 -07004379 */
4380static int __init rcu_spawn_gp_kthread(void)
4381{
4382 unsigned long flags;
Paul E. McKenneya94844b2014-12-12 07:37:48 -08004383 int kthread_prio_in = kthread_prio;
Paul E. McKenneyb3dbec72012-06-18 18:36:08 -07004384 struct rcu_node *rnp;
4385 struct rcu_state *rsp;
Paul E. McKenneya94844b2014-12-12 07:37:48 -08004386 struct sched_param sp;
Paul E. McKenneyb3dbec72012-06-18 18:36:08 -07004387 struct task_struct *t;
4388
Paul E. McKenneya94844b2014-12-12 07:37:48 -08004389 /* Force priority into range. */
4390 if (IS_ENABLED(CONFIG_RCU_BOOST) && kthread_prio < 1)
4391 kthread_prio = 1;
4392 else if (kthread_prio < 0)
4393 kthread_prio = 0;
4394 else if (kthread_prio > 99)
4395 kthread_prio = 99;
4396 if (kthread_prio != kthread_prio_in)
4397 pr_alert("rcu_spawn_gp_kthread(): Limited prio to %d from %d\n",
4398 kthread_prio, kthread_prio_in);
4399
Paul E. McKenney9386c0b2014-07-13 12:00:53 -07004400 rcu_scheduler_fully_active = 1;
Paul E. McKenneyb3dbec72012-06-18 18:36:08 -07004401 for_each_rcu_flavor(rsp) {
Paul E. McKenneya94844b2014-12-12 07:37:48 -08004402 t = kthread_create(rcu_gp_kthread, rsp, "%s", rsp->name);
Paul E. McKenneyb3dbec72012-06-18 18:36:08 -07004403 BUG_ON(IS_ERR(t));
4404 rnp = rcu_get_root(rsp);
Paul E. McKenney6cf10082015-10-08 15:36:54 -07004405 raw_spin_lock_irqsave_rcu_node(rnp, flags);
Paul E. McKenneyb3dbec72012-06-18 18:36:08 -07004406 rsp->gp_kthread = t;
Paul E. McKenneya94844b2014-12-12 07:37:48 -08004407 if (kthread_prio) {
4408 sp.sched_priority = kthread_prio;
4409 sched_setscheduler_nocheck(t, SCHED_FIFO, &sp);
4410 }
Boqun Feng67c583a72015-12-29 12:18:47 +08004411 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
Peter Zijlstrae11f1332015-11-04 08:22:05 -08004412 wake_up_process(t);
Paul E. McKenneyb3dbec72012-06-18 18:36:08 -07004413 }
Paul E. McKenney35ce7f22014-07-11 11:30:24 -07004414 rcu_spawn_nocb_kthreads();
Paul E. McKenney9386c0b2014-07-13 12:00:53 -07004415 rcu_spawn_boost_kthreads();
Paul E. McKenneyb3dbec72012-06-18 18:36:08 -07004416 return 0;
4417}
4418early_initcall(rcu_spawn_gp_kthread);
4419
4420/*
Paul E. McKenneybbad9372010-04-02 16:17:17 -07004421 * This function is invoked towards the end of the scheduler's initialization
4422 * process. Before this is called, the idle task might contain
4423 * RCU read-side critical sections (during which time, this idle
4424 * task is booting the system). After this function is called, the
4425 * idle tasks are prohibited from containing RCU read-side critical
4426 * sections. This function also enables RCU lockdep checking.
4427 */
4428void rcu_scheduler_starting(void)
4429{
4430 WARN_ON(num_online_cpus() != 1);
4431 WARN_ON(nr_context_switches() > 0);
4432 rcu_scheduler_active = 1;
4433}
4434
4435/*
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01004436 * Compute the per-level fanout, either using the exact fanout specified
Paul E. McKenney7fa27002015-04-20 10:27:15 -07004437 * or balancing the tree, depending on the rcu_fanout_exact boot parameter.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01004438 */
Alexander Gordeev199977b2015-06-03 08:18:29 +02004439static void __init rcu_init_levelspread(int *levelspread, const int *levelcnt)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01004440{
4441 int i;
4442
Paul E. McKenney7fa27002015-04-20 10:27:15 -07004443 if (rcu_fanout_exact) {
Alexander Gordeev199977b2015-06-03 08:18:29 +02004444 levelspread[rcu_num_lvls - 1] = rcu_fanout_leaf;
Paul E. McKenney66292402015-01-19 19:16:38 -08004445 for (i = rcu_num_lvls - 2; i >= 0; i--)
Alexander Gordeev199977b2015-06-03 08:18:29 +02004446 levelspread[i] = RCU_FANOUT;
Paul E. McKenney66292402015-01-19 19:16:38 -08004447 } else {
4448 int ccur;
4449 int cprv;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01004450
Paul E. McKenney66292402015-01-19 19:16:38 -08004451 cprv = nr_cpu_ids;
4452 for (i = rcu_num_lvls - 1; i >= 0; i--) {
Alexander Gordeev199977b2015-06-03 08:18:29 +02004453 ccur = levelcnt[i];
4454 levelspread[i] = (cprv + ccur - 1) / ccur;
Paul E. McKenney66292402015-01-19 19:16:38 -08004455 cprv = ccur;
4456 }
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01004457 }
4458}
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01004459
4460/*
4461 * Helper function for rcu_init() that initializes one rcu_state structure.
4462 */
Paul E. McKenneya87f2032015-10-20 12:38:49 -07004463static void __init rcu_init_one(struct rcu_state *rsp)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01004464{
Alexander Gordeevcb007102015-06-03 08:18:30 +02004465 static const char * const buf[] = RCU_NODE_NAME_INIT;
4466 static const char * const fqs[] = RCU_FQS_NAME_INIT;
Paul E. McKenney385b73c2015-06-24 14:20:08 -07004467 static const char * const exp[] = RCU_EXP_NAME_INIT;
Paul E. McKenney3dc5dbe2015-09-26 14:51:24 -07004468 static struct lock_class_key rcu_node_class[RCU_NUM_LVLS];
4469 static struct lock_class_key rcu_fqs_class[RCU_NUM_LVLS];
4470 static struct lock_class_key rcu_exp_class[RCU_NUM_LVLS];
Paul E. McKenney4a81e832014-06-20 16:49:01 -07004471 static u8 fl_mask = 0x1;
Alexander Gordeev199977b2015-06-03 08:18:29 +02004472
4473 int levelcnt[RCU_NUM_LVLS]; /* # nodes in each level. */
4474 int levelspread[RCU_NUM_LVLS]; /* kids/node in each level. */
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01004475 int cpustride = 1;
4476 int i;
4477 int j;
4478 struct rcu_node *rnp;
4479
Alexander Gordeev05b84ae2015-06-03 08:18:28 +02004480 BUILD_BUG_ON(RCU_NUM_LVLS > ARRAY_SIZE(buf)); /* Fix buf[] init! */
Paul E. McKenneyb6407e82010-01-04 16:04:02 -08004481
Paul E. McKenney3eaaaf6c2015-03-09 16:51:17 -07004482 /* Silence gcc 4.8 false positive about array index out of range. */
4483 if (rcu_num_lvls <= 0 || rcu_num_lvls > RCU_NUM_LVLS)
4484 panic("rcu_init_one: rcu_num_lvls out of range");
Paul E. McKenney49305212012-11-29 13:49:00 -08004485
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01004486 /* Initialize the level-tracking arrays. */
4487
Paul E. McKenneyf885b7f2012-04-23 15:52:53 -07004488 for (i = 0; i < rcu_num_lvls; i++)
Alexander Gordeev199977b2015-06-03 08:18:29 +02004489 levelcnt[i] = num_rcu_lvl[i];
Paul E. McKenneyf885b7f2012-04-23 15:52:53 -07004490 for (i = 1; i < rcu_num_lvls; i++)
Alexander Gordeev199977b2015-06-03 08:18:29 +02004491 rsp->level[i] = rsp->level[i - 1] + levelcnt[i - 1];
4492 rcu_init_levelspread(levelspread, levelcnt);
Paul E. McKenney4a81e832014-06-20 16:49:01 -07004493 rsp->flavor_mask = fl_mask;
4494 fl_mask <<= 1;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01004495
4496 /* Initialize the elements themselves, starting from the leaves. */
4497
Paul E. McKenneyf885b7f2012-04-23 15:52:53 -07004498 for (i = rcu_num_lvls - 1; i >= 0; i--) {
Alexander Gordeev199977b2015-06-03 08:18:29 +02004499 cpustride *= levelspread[i];
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01004500 rnp = rsp->level[i];
Alexander Gordeev199977b2015-06-03 08:18:29 +02004501 for (j = 0; j < levelcnt[i]; j++, rnp++) {
Boqun Feng67c583a72015-12-29 12:18:47 +08004502 raw_spin_lock_init(&ACCESS_PRIVATE(rnp, lock));
4503 lockdep_set_class_and_name(&ACCESS_PRIVATE(rnp, lock),
Paul E. McKenneyb6407e82010-01-04 16:04:02 -08004504 &rcu_node_class[i], buf[i]);
Paul E. McKenney394f2762012-06-26 17:00:35 -07004505 raw_spin_lock_init(&rnp->fqslock);
4506 lockdep_set_class_and_name(&rnp->fqslock,
4507 &rcu_fqs_class[i], fqs[i]);
Paul E. McKenney25d30cf2012-07-11 05:23:18 -07004508 rnp->gpnum = rsp->gpnum;
4509 rnp->completed = rsp->completed;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01004510 rnp->qsmask = 0;
4511 rnp->qsmaskinit = 0;
4512 rnp->grplo = j * cpustride;
4513 rnp->grphi = (j + 1) * cpustride - 1;
Himangi Saraogi595f3902014-03-18 22:52:26 +05304514 if (rnp->grphi >= nr_cpu_ids)
4515 rnp->grphi = nr_cpu_ids - 1;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01004516 if (i == 0) {
4517 rnp->grpnum = 0;
4518 rnp->grpmask = 0;
4519 rnp->parent = NULL;
4520 } else {
Alexander Gordeev199977b2015-06-03 08:18:29 +02004521 rnp->grpnum = j % levelspread[i - 1];
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01004522 rnp->grpmask = 1UL << rnp->grpnum;
4523 rnp->parent = rsp->level[i - 1] +
Alexander Gordeev199977b2015-06-03 08:18:29 +02004524 j / levelspread[i - 1];
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01004525 }
4526 rnp->level = i;
Paul E. McKenney12f5f522010-11-29 21:56:39 -08004527 INIT_LIST_HEAD(&rnp->blkd_tasks);
Paul E. McKenneydae6e642013-02-10 20:48:58 -08004528 rcu_init_one_nocb(rnp);
Paul E. McKenney385b73c2015-06-24 14:20:08 -07004529 mutex_init(&rnp->exp_funnel_mutex);
Paul E. McKenney83c2c732015-08-06 20:43:02 -07004530 lockdep_set_class_and_name(&rnp->exp_funnel_mutex,
4531 &rcu_exp_class[i], exp[i]);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01004532 }
4533 }
Lai Jiangshan0c340292010-03-28 11:12:30 +08004534
Paul Gortmakerabedf8e2016-02-19 09:46:41 +01004535 init_swait_queue_head(&rsp->gp_wq);
4536 init_swait_queue_head(&rsp->expedited_wq);
Paul E. McKenneyf885b7f2012-04-23 15:52:53 -07004537 rnp = rsp->level[rcu_num_lvls - 1];
Lai Jiangshan0c340292010-03-28 11:12:30 +08004538 for_each_possible_cpu(i) {
Paul E. McKenney4a90a062010-04-14 16:48:11 -07004539 while (i > rnp->grphi)
Lai Jiangshan0c340292010-03-28 11:12:30 +08004540 rnp++;
Lai Jiangshan394f99a2010-06-28 16:25:04 +08004541 per_cpu_ptr(rsp->rda, i)->mynode = rnp;
Lai Jiangshan0c340292010-03-28 11:12:30 +08004542 rcu_boot_init_percpu_data(i, rsp);
4543 }
Paul E. McKenney6ce75a22012-06-12 11:01:13 -07004544 list_add(&rsp->flavors, &rcu_struct_flavors);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01004545}
4546
Paul E. McKenneyf885b7f2012-04-23 15:52:53 -07004547/*
4548 * Compute the rcu_node tree geometry from kernel parameters. This cannot
Paul E. McKenney4102ada2013-10-08 20:23:47 -07004549 * replace the definitions in tree.h because those are needed to size
Paul E. McKenneyf885b7f2012-04-23 15:52:53 -07004550 * the ->node array in the rcu_state structure.
4551 */
4552static void __init rcu_init_geometry(void)
4553{
Paul E. McKenney026ad282013-04-03 22:14:11 -07004554 ulong d;
Paul E. McKenneyf885b7f2012-04-23 15:52:53 -07004555 int i;
Alexander Gordeev05b84ae2015-06-03 08:18:28 +02004556 int rcu_capacity[RCU_NUM_LVLS];
Paul E. McKenneyf885b7f2012-04-23 15:52:53 -07004557
Paul E. McKenney026ad282013-04-03 22:14:11 -07004558 /*
4559 * Initialize any unspecified boot parameters.
4560 * The default values of jiffies_till_first_fqs and
4561 * jiffies_till_next_fqs are set to the RCU_JIFFIES_TILL_FORCE_QS
4562 * value, which is a function of HZ, then adding one for each
4563 * RCU_JIFFIES_FQS_DIV CPUs that might be on the system.
4564 */
4565 d = RCU_JIFFIES_TILL_FORCE_QS + nr_cpu_ids / RCU_JIFFIES_FQS_DIV;
4566 if (jiffies_till_first_fqs == ULONG_MAX)
4567 jiffies_till_first_fqs = d;
4568 if (jiffies_till_next_fqs == ULONG_MAX)
4569 jiffies_till_next_fqs = d;
4570
Paul E. McKenneyf885b7f2012-04-23 15:52:53 -07004571 /* If the compile-time values are accurate, just leave. */
Paul E. McKenney47d631a2015-04-21 09:12:13 -07004572 if (rcu_fanout_leaf == RCU_FANOUT_LEAF &&
Paul E. McKenneyb17c7032012-09-06 15:38:02 -07004573 nr_cpu_ids == NR_CPUS)
Paul E. McKenneyf885b7f2012-04-23 15:52:53 -07004574 return;
Paul E. McKenney39479092013-10-09 15:20:33 -07004575 pr_info("RCU: Adjusting geometry for rcu_fanout_leaf=%d, nr_cpu_ids=%d\n",
4576 rcu_fanout_leaf, nr_cpu_ids);
Paul E. McKenneyf885b7f2012-04-23 15:52:53 -07004577
4578 /*
Paul E. McKenneyee968ac2015-07-31 08:28:35 -07004579 * The boot-time rcu_fanout_leaf parameter must be at least two
4580 * and cannot exceed the number of bits in the rcu_node masks.
4581 * Complain and fall back to the compile-time values if this
4582 * limit is exceeded.
Paul E. McKenneyf885b7f2012-04-23 15:52:53 -07004583 */
Paul E. McKenneyee968ac2015-07-31 08:28:35 -07004584 if (rcu_fanout_leaf < 2 ||
Alexander Gordeev75cf15a2015-06-03 08:18:23 +02004585 rcu_fanout_leaf > sizeof(unsigned long) * 8) {
Paul E. McKenney13bd64942015-06-04 10:06:01 -07004586 rcu_fanout_leaf = RCU_FANOUT_LEAF;
Paul E. McKenneyf885b7f2012-04-23 15:52:53 -07004587 WARN_ON(1);
4588 return;
4589 }
4590
Alexander Gordeev75cf15a2015-06-03 08:18:23 +02004591 /*
Paul E. McKenneyf885b7f2012-04-23 15:52:53 -07004592 * Compute number of nodes that can be handled an rcu_node tree
Alexander Gordeev96181382015-06-03 08:18:26 +02004593 * with the given number of levels.
Paul E. McKenneyf885b7f2012-04-23 15:52:53 -07004594 */
Alexander Gordeev96181382015-06-03 08:18:26 +02004595 rcu_capacity[0] = rcu_fanout_leaf;
Alexander Gordeev05b84ae2015-06-03 08:18:28 +02004596 for (i = 1; i < RCU_NUM_LVLS; i++)
Paul E. McKenneyf885b7f2012-04-23 15:52:53 -07004597 rcu_capacity[i] = rcu_capacity[i - 1] * RCU_FANOUT;
4598
4599 /*
Alexander Gordeev75cf15a2015-06-03 08:18:23 +02004600 * The tree must be able to accommodate the configured number of CPUs.
Paul E. McKenneyee968ac2015-07-31 08:28:35 -07004601 * If this limit is exceeded, fall back to the compile-time values.
Paul E. McKenneyf885b7f2012-04-23 15:52:53 -07004602 */
Paul E. McKenneyee968ac2015-07-31 08:28:35 -07004603 if (nr_cpu_ids > rcu_capacity[RCU_NUM_LVLS - 1]) {
4604 rcu_fanout_leaf = RCU_FANOUT_LEAF;
4605 WARN_ON(1);
4606 return;
4607 }
Paul E. McKenneyf885b7f2012-04-23 15:52:53 -07004608
Alexander Gordeev679f9852015-06-03 08:18:25 +02004609 /* Calculate the number of levels in the tree. */
Alexander Gordeev96181382015-06-03 08:18:26 +02004610 for (i = 0; nr_cpu_ids > rcu_capacity[i]; i++) {
Alexander Gordeev679f9852015-06-03 08:18:25 +02004611 }
Alexander Gordeev96181382015-06-03 08:18:26 +02004612 rcu_num_lvls = i + 1;
Alexander Gordeev679f9852015-06-03 08:18:25 +02004613
Paul E. McKenneyf885b7f2012-04-23 15:52:53 -07004614 /* Calculate the number of rcu_nodes at each level of the tree. */
Alexander Gordeev679f9852015-06-03 08:18:25 +02004615 for (i = 0; i < rcu_num_lvls; i++) {
Alexander Gordeev96181382015-06-03 08:18:26 +02004616 int cap = rcu_capacity[(rcu_num_lvls - 1) - i];
Alexander Gordeev679f9852015-06-03 08:18:25 +02004617 num_rcu_lvl[i] = DIV_ROUND_UP(nr_cpu_ids, cap);
4618 }
Paul E. McKenneyf885b7f2012-04-23 15:52:53 -07004619
4620 /* Calculate the total number of rcu_node structures. */
4621 rcu_num_nodes = 0;
Alexander Gordeev679f9852015-06-03 08:18:25 +02004622 for (i = 0; i < rcu_num_lvls; i++)
Paul E. McKenneyf885b7f2012-04-23 15:52:53 -07004623 rcu_num_nodes += num_rcu_lvl[i];
Paul E. McKenneyf885b7f2012-04-23 15:52:53 -07004624}
4625
Paul E. McKenneya3dc2942015-04-20 11:40:50 -07004626/*
4627 * Dump out the structure of the rcu_node combining tree associated
4628 * with the rcu_state structure referenced by rsp.
4629 */
4630static void __init rcu_dump_rcu_node_tree(struct rcu_state *rsp)
4631{
4632 int level = 0;
4633 struct rcu_node *rnp;
4634
4635 pr_info("rcu_node tree layout dump\n");
4636 pr_info(" ");
4637 rcu_for_each_node_breadth_first(rsp, rnp) {
4638 if (rnp->level != level) {
4639 pr_cont("\n");
4640 pr_info(" ");
4641 level = rnp->level;
4642 }
4643 pr_cont("%d:%d ^%d ", rnp->grplo, rnp->grphi, rnp->grpnum);
4644 }
4645 pr_cont("\n");
4646}
4647
Paul E. McKenney9f680ab2009-11-22 08:53:49 -08004648void __init rcu_init(void)
Paul E. McKenneyf41d9112009-08-22 13:56:52 -07004649{
Paul E. McKenney017c4262010-01-14 16:10:58 -08004650 int cpu;
Paul E. McKenney9f680ab2009-11-22 08:53:49 -08004651
Paul E. McKenney47627672015-01-19 21:10:21 -08004652 rcu_early_boot_tests();
4653
Paul E. McKenneyf41d9112009-08-22 13:56:52 -07004654 rcu_bootup_announce();
Paul E. McKenneyf885b7f2012-04-23 15:52:53 -07004655 rcu_init_geometry();
Paul E. McKenneya87f2032015-10-20 12:38:49 -07004656 rcu_init_one(&rcu_bh_state);
4657 rcu_init_one(&rcu_sched_state);
Paul E. McKenneya3dc2942015-04-20 11:40:50 -07004658 if (dump_tree)
4659 rcu_dump_rcu_node_tree(&rcu_sched_state);
Paul E. McKenneyf41d9112009-08-22 13:56:52 -07004660 __rcu_init_preempt();
Jiang Fangb5b39362013-02-02 14:13:42 -08004661 open_softirq(RCU_SOFTIRQ, rcu_process_callbacks);
Paul E. McKenney9f680ab2009-11-22 08:53:49 -08004662
4663 /*
4664 * We don't need protection against CPU-hotplug here because
4665 * this is called early in boot, before either interrupts
4666 * or the scheduler are operational.
4667 */
4668 cpu_notifier(rcu_cpu_notify, 0);
Borislav Petkovd1d74d12013-04-22 00:12:42 +02004669 pm_notifier(rcu_pm_notify, 0);
Paul E. McKenney017c4262010-01-14 16:10:58 -08004670 for_each_online_cpu(cpu)
4671 rcu_cpu_notify(NULL, CPU_UP_PREPARE, (void *)(long)cpu);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01004672}
4673
Paul E. McKenney4102ada2013-10-08 20:23:47 -07004674#include "tree_plugin.h"