blob: 874054b30fe6815d4ac9caaf209d2a577cdfd8e6 [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 */
Joe Perchesa7538352018-05-14 13:27:33 -070030
31#define pr_fmt(fmt) "rcu: " fmt
32
Paul E. McKenney64db4cf2008-12-18 21:55:32 +010033#include <linux/types.h>
34#include <linux/kernel.h>
35#include <linux/init.h>
36#include <linux/spinlock.h>
37#include <linux/smp.h>
Ingo Molnarf9411eb2017-02-06 09:50:49 +010038#include <linux/rcupdate_wait.h>
Paul E. McKenney64db4cf2008-12-18 21:55:32 +010039#include <linux/interrupt.h>
40#include <linux/sched.h>
Ingo Molnarb17b0152017-02-08 18:51:35 +010041#include <linux/sched/debug.h>
Ingo Molnarc1dc0b92009-08-02 11:28:21 +020042#include <linux/nmi.h>
Paul E. McKenney8826f3b2011-05-11 05:41:41 -070043#include <linux/atomic.h>
Paul E. McKenney64db4cf2008-12-18 21:55:32 +010044#include <linux/bitops.h>
Paul Gortmaker9984de12011-05-23 14:51:41 -040045#include <linux/export.h>
Paul E. McKenney64db4cf2008-12-18 21:55:32 +010046#include <linux/completion.h>
47#include <linux/moduleparam.h>
48#include <linux/percpu.h>
49#include <linux/notifier.h>
50#include <linux/cpu.h>
51#include <linux/mutex.h>
52#include <linux/time.h>
Paul E. McKenneybbad9372010-04-02 16:17:17 -070053#include <linux/kernel_stat.h>
Paul E. McKenneya26ac242011-01-12 14:10:23 -080054#include <linux/wait.h>
55#include <linux/kthread.h>
Ingo Molnarae7e81c2017-02-01 18:07:51 +010056#include <uapi/linux/sched/types.h>
Linus Torvalds268bb0c2011-05-20 12:50:29 -070057#include <linux/prefetch.h>
Paul E. McKenney3d3b7db2012-01-23 17:05:46 -080058#include <linux/delay.h>
59#include <linux/stop_machine.h>
Paul E. McKenney661a85d2012-07-07 05:57:03 -070060#include <linux/random.h>
Steven Rostedt (Red Hat)af658dc2015-04-29 14:36:05 -040061#include <linux/trace_events.h>
Borislav Petkovd1d74d12013-04-22 00:12:42 +020062#include <linux/suspend.h>
Paul E. McKenneya278d472017-04-05 09:05:18 -070063#include <linux/ftrace.h>
Paul E. McKenneyd3052102018-07-25 11:49:47 -070064#include <linux/tick.h>
Paul E. McKenney2ccaff12018-12-12 12:32:06 -080065#include <linux/sysrq.h>
Paul E. McKenney64db4cf2008-12-18 21:55:32 +010066
Paul E. McKenney4102ada2013-10-08 20:23:47 -070067#include "tree.h"
Paul E. McKenney29c00b42011-06-17 15:53:19 -070068#include "rcu.h"
Paul E. McKenney9f77da92009-08-22 13:56:45 -070069
Paul E. McKenney4102ada2013-10-08 20:23:47 -070070#ifdef MODULE_PARAM_PREFIX
71#undef MODULE_PARAM_PREFIX
72#endif
73#define MODULE_PARAM_PREFIX "rcutree."
74
Paul E. McKenney64db4cf2008-12-18 21:55:32 +010075/* Data structures. */
76
Steven Rostedt (Red Hat)f7f7bac2013-07-12 17:18:47 -040077/*
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -070078 * Steal a bit from the bottom of ->dynticks for idle entry/exit
79 * control. Initially this is for TLB flushing.
Steven Rostedt (Red Hat)f7f7bac2013-07-12 17:18:47 -040080 */
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -070081#define RCU_DYNTICK_CTRL_MASK 0x1
82#define RCU_DYNTICK_CTRL_CTR (RCU_DYNTICK_CTRL_MASK + 1)
83#ifndef rcu_eqs_special_exit
84#define rcu_eqs_special_exit() do { } while (0)
Ard Biesheuvela8a29b32014-07-12 19:01:49 +020085#endif
86
Paul E. McKenney4c5273b2018-08-03 21:00:38 -070087static DEFINE_PER_CPU_SHARED_ALIGNED(struct rcu_data, rcu_data) = {
88 .dynticks_nesting = 1,
89 .dynticks_nmi_nesting = DYNTICK_IRQ_NONIDLE,
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -070090 .dynticks = ATOMIC_INIT(RCU_DYNTICK_CTRL_CTR),
Paul E. McKenney4c5273b2018-08-03 21:00:38 -070091};
Paul E. McKenney358be2d2018-07-03 14:15:31 -070092struct rcu_state rcu_state = {
93 .level = { &rcu_state.node[0] },
Paul E. McKenney358be2d2018-07-03 14:15:31 -070094 .gp_state = RCU_GP_IDLE,
95 .gp_seq = (0UL - 300UL) << RCU_SEQ_CTR_SHIFT,
96 .barrier_mutex = __MUTEX_INITIALIZER(rcu_state.barrier_mutex),
97 .name = RCU_NAME,
98 .abbr = RCU_ABBR,
99 .exp_mutex = __MUTEX_INITIALIZER(rcu_state.exp_mutex),
100 .exp_wake_mutex = __MUTEX_INITIALIZER(rcu_state.exp_wake_mutex),
Mike Galbraith894d45b2018-08-15 09:05:29 -0700101 .ofl_lock = __RAW_SPIN_LOCK_UNLOCKED(rcu_state.ofl_lock),
Paul E. McKenney358be2d2018-07-03 14:15:31 -0700102};
Paul E. McKenney27f4d282011-02-07 12:47:15 -0800103
Paul E. McKenneya3dc2942015-04-20 11:40:50 -0700104/* Dump rcu_node combining tree at boot to verify correct setup. */
105static bool dump_tree;
106module_param(dump_tree, bool, 0444);
Paul E. McKenney7fa27002015-04-20 10:27:15 -0700107/* Control rcu_node-tree auto-balancing at boot time. */
108static bool rcu_fanout_exact;
109module_param(rcu_fanout_exact, bool, 0444);
Paul E. McKenney47d631a2015-04-21 09:12:13 -0700110/* Increase (but not decrease) the RCU_FANOUT_LEAF at boot time. */
111static int rcu_fanout_leaf = RCU_FANOUT_LEAF;
Paul E. McKenney7e5c2df2012-07-01 15:42:33 -0700112module_param(rcu_fanout_leaf, int, 0444);
Paul E. McKenneyf885b7f2012-04-23 15:52:53 -0700113int rcu_num_lvls __read_mostly = RCU_NUM_LVLS;
Alexander Gordeevcb007102015-06-03 08:18:30 +0200114/* Number of rcu_nodes at specified level. */
Paul E. McKenneye95d68d2017-03-15 13:11:11 -0700115int num_rcu_lvl[] = NUM_RCU_LVL_INIT;
Paul E. McKenneyf885b7f2012-04-23 15:52:53 -0700116int rcu_num_nodes __read_mostly = NUM_RCU_NODES; /* Total # rcu_nodes in use. */
Daniel Bristot de Oliveira088e9d22016-06-02 13:51:41 -0300117/* panic() on RCU Stall sysctl. */
118int sysctl_panic_on_rcu_stall __read_mostly;
Paul E. McKenney2ccaff12018-12-12 12:32:06 -0800119/* Commandeer a sysrq key to dump RCU's tree. */
120static bool sysrq_rcu;
121module_param(sysrq_rcu, bool, 0444);
Paul E. McKenneyf885b7f2012-04-23 15:52:53 -0700122
Paul E. McKenneyb0d30412011-07-10 15:57:35 -0700123/*
Paul E. McKenney52d7e482017-01-10 02:28:26 -0800124 * The rcu_scheduler_active variable is initialized to the value
125 * RCU_SCHEDULER_INACTIVE and transitions RCU_SCHEDULER_INIT just before the
126 * first task is spawned. So when this variable is RCU_SCHEDULER_INACTIVE,
127 * RCU can assume that there is but one task, allowing RCU to (for example)
Paul E. McKenney0d950922016-04-08 05:00:03 -0700128 * optimize synchronize_rcu() to a simple barrier(). When this variable
Paul E. McKenney52d7e482017-01-10 02:28:26 -0800129 * is RCU_SCHEDULER_INIT, RCU must actually do all the hard work required
130 * to detect real grace periods. This variable is also used to suppress
131 * boot-time false positives from lockdep-RCU error checking. Finally, it
132 * transitions from RCU_SCHEDULER_INIT to RCU_SCHEDULER_RUNNING after RCU
133 * is fully initialized, including all of its kthreads having been spawned.
Paul E. McKenneyb0d30412011-07-10 15:57:35 -0700134 */
Paul E. McKenneybbad9372010-04-02 16:17:17 -0700135int rcu_scheduler_active __read_mostly;
136EXPORT_SYMBOL_GPL(rcu_scheduler_active);
137
Paul E. McKenneyb0d30412011-07-10 15:57:35 -0700138/*
139 * The rcu_scheduler_fully_active variable transitions from zero to one
140 * during the early_initcall() processing, which is after the scheduler
141 * is capable of creating new tasks. So RCU processing (for example,
142 * creating tasks for RCU priority boosting) must be delayed until after
143 * rcu_scheduler_fully_active transitions from zero to one. We also
144 * currently delay invocation of any RCU callbacks until after this point.
145 *
146 * It might later prove better for people registering RCU callbacks during
147 * early boot to take responsibility for these callbacks, but one step at
148 * a time.
149 */
150static int rcu_scheduler_fully_active __read_mostly;
151
Paul E. McKenneyb50912d2018-07-03 17:22:34 -0700152static void rcu_report_qs_rnp(unsigned long mask, struct rcu_node *rnp,
153 unsigned long gps, unsigned long flags);
Paul E. McKenney0aa04b02015-01-23 21:52:37 -0800154static void rcu_init_new_rnp(struct rcu_node *rnp_leaf);
155static void rcu_cleanup_dead_rnp(struct rcu_node *rnp_leaf);
Thomas Gleixner5d01bbd2012-07-16 10:42:35 +0000156static void rcu_boost_kthread_setaffinity(struct rcu_node *rnp, int outgoingcpu);
Paul E. McKenneya46e0892011-06-15 15:47:09 -0700157static void invoke_rcu_core(void);
Paul E. McKenneyaff4e9e2018-07-03 17:22:34 -0700158static void invoke_rcu_callbacks(struct rcu_data *rdp);
Paul E. McKenney63d4c8c2018-07-03 17:22:34 -0700159static void rcu_report_exp_rdp(struct rcu_data *rdp);
Paul E. McKenney3549c2b2016-04-15 16:35:29 -0700160static void sync_sched_exp_online_cleanup(int cpu);
Paul E. McKenneya26ac242011-01-12 14:10:23 -0800161
Paul E. McKenneya94844b2014-12-12 07:37:48 -0800162/* rcuc/rcub kthread realtime priority */
Paul E. McKenney26730f52015-04-21 09:22:14 -0700163static int kthread_prio = IS_ENABLED(CONFIG_RCU_BOOST) ? 1 : 0;
Paul E. McKenneya94844b2014-12-12 07:37:48 -0800164module_param(kthread_prio, int, 0644);
165
Paul E. McKenney8d7dc922015-04-14 19:33:59 -0700166/* Delay in jiffies for grace-period initialization delays, debug only. */
Paul E. McKenney0f41c0d2015-03-10 18:33:20 -0700167
Paul E. McKenney90040c92017-05-10 14:36:55 -0700168static int gp_preinit_delay;
169module_param(gp_preinit_delay, int, 0444);
170static int gp_init_delay;
171module_param(gp_init_delay, int, 0444);
172static int gp_cleanup_delay;
173module_param(gp_cleanup_delay, int, 0444);
Paul E. McKenney0f41c0d2015-03-10 18:33:20 -0700174
Paul E. McKenney4cf439a2018-07-02 12:15:25 -0700175/* Retrieve RCU kthreads priority for rcutorture */
Joel Fernandes (Google)4babd852018-06-19 15:14:18 -0700176int rcu_get_gp_kthreads_prio(void)
177{
178 return kthread_prio;
179}
180EXPORT_SYMBOL_GPL(rcu_get_gp_kthreads_prio);
181
Paul E. McKenneyeab128e2015-04-15 12:08:22 -0700182/*
183 * Number of grace periods between delays, normalized by the duration of
Paul E. McKenneybfd090b2017-02-08 14:49:27 -0800184 * the delay. The longer the delay, the more the grace periods between
Paul E. McKenneyeab128e2015-04-15 12:08:22 -0700185 * each delay. The reason for this normalization is that it means that,
186 * for non-zero delays, the overall slowdown of grace periods is constant
187 * regardless of the duration of the delay. This arrangement balances
188 * the need for long delays to increase some race probabilities with the
189 * need for fast grace periods to increase other race probabilities.
190 */
191#define PER_RCU_NODE_PERIOD 3 /* Number of grace periods between delays. */
Paul E. McKenney37745d22015-01-22 18:24:08 -0800192
Paul E. McKenneya26ac242011-01-12 14:10:23 -0800193/*
Paul E. McKenney0aa04b02015-01-23 21:52:37 -0800194 * Compute the mask of online CPUs for the specified rcu_node structure.
195 * This will not be stable unless the rcu_node structure's ->lock is
196 * held, but the bit corresponding to the current CPU will be stable
197 * in most contexts.
198 */
199unsigned long rcu_rnp_online_cpus(struct rcu_node *rnp)
200{
Paul E. McKenney7d0ae802015-03-03 14:57:58 -0800201 return READ_ONCE(rnp->qsmaskinitnext);
Paul E. McKenney0aa04b02015-01-23 21:52:37 -0800202}
203
204/*
Paul E. McKenney7d0ae802015-03-03 14:57:58 -0800205 * Return true if an RCU grace period is in progress. The READ_ONCE()s
Paul E. McKenneyfc2219d42009-09-23 09:50:41 -0700206 * permit this function to be invoked without holding the root rcu_node
207 * structure's ->lock, but of course results can be subject to change.
208 */
Paul E. McKenneyde8e8732018-07-03 17:22:34 -0700209static int rcu_gp_in_progress(void)
Paul E. McKenneyfc2219d42009-09-23 09:50:41 -0700210{
Paul E. McKenneyde8e8732018-07-03 17:22:34 -0700211 return rcu_seq_state(rcu_seq_current(&rcu_state.gp_seq));
Paul E. McKenneyfc2219d42009-09-23 09:50:41 -0700212}
213
Paul E. McKenney903ee832018-10-02 16:05:46 -0700214/*
215 * Return the number of callbacks queued on the specified CPU.
216 * Handles both the nocbs and normal cases.
217 */
218static long rcu_get_n_cbs_cpu(int cpu)
219{
220 struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
221
222 if (rcu_segcblist_is_enabled(&rdp->cblist)) /* Online normal CPU? */
223 return rcu_segcblist_n_cbs(&rdp->cblist);
224 return rcu_get_n_cbs_nocb_cpu(rdp); /* Works for offline, too. */
225}
226
Paul E. McKenneyd28139c2018-06-28 14:45:25 -0700227void rcu_softirq_qs(void)
Ingo Molnarb1f77b02009-03-13 03:20:49 +0100228{
Paul E. McKenney45975c72018-07-02 14:30:37 -0700229 rcu_qs();
Paul E. McKenneyd28139c2018-06-28 14:45:25 -0700230 rcu_preempt_deferred_qs(current);
Ingo Molnarb1f77b02009-03-13 03:20:49 +0100231}
232
Paul E. McKenney6563de92016-11-02 13:33:57 -0700233/*
Paul E. McKenney2625d462016-11-02 14:23:30 -0700234 * Record entry into an extended quiescent state. This is only to be
235 * called when not already in an extended quiescent state.
236 */
237static void rcu_dynticks_eqs_enter(void)
238{
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -0700239 struct rcu_data *rdp = this_cpu_ptr(&rcu_data);
Paul E. McKenneyb8c17e62016-11-08 14:25:21 -0800240 int seq;
Paul E. McKenney2625d462016-11-02 14:23:30 -0700241
242 /*
Paul E. McKenneyb8c17e62016-11-08 14:25:21 -0800243 * CPUs seeing atomic_add_return() must see prior RCU read-side
Paul E. McKenney2625d462016-11-02 14:23:30 -0700244 * critical sections, and we also must force ordering with the
245 * next idle sojourn.
246 */
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -0700247 seq = atomic_add_return(RCU_DYNTICK_CTRL_CTR, &rdp->dynticks);
Paul E. McKenneyb8c17e62016-11-08 14:25:21 -0800248 /* Better be in an extended quiescent state! */
249 WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) &&
250 (seq & RCU_DYNTICK_CTRL_CTR));
251 /* Better not have special action (TLB flush) pending! */
252 WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) &&
253 (seq & RCU_DYNTICK_CTRL_MASK));
Paul E. McKenney2625d462016-11-02 14:23:30 -0700254}
255
256/*
257 * Record exit from an extended quiescent state. This is only to be
258 * called from an extended quiescent state.
259 */
260static void rcu_dynticks_eqs_exit(void)
261{
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -0700262 struct rcu_data *rdp = this_cpu_ptr(&rcu_data);
Paul E. McKenneyb8c17e62016-11-08 14:25:21 -0800263 int seq;
Paul E. McKenney2625d462016-11-02 14:23:30 -0700264
265 /*
Paul E. McKenneyb8c17e62016-11-08 14:25:21 -0800266 * CPUs seeing atomic_add_return() must see prior idle sojourns,
Paul E. McKenney2625d462016-11-02 14:23:30 -0700267 * and we also must force ordering with the next RCU read-side
268 * critical section.
269 */
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -0700270 seq = atomic_add_return(RCU_DYNTICK_CTRL_CTR, &rdp->dynticks);
Paul E. McKenneyb8c17e62016-11-08 14:25:21 -0800271 WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) &&
272 !(seq & RCU_DYNTICK_CTRL_CTR));
273 if (seq & RCU_DYNTICK_CTRL_MASK) {
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -0700274 atomic_andnot(RCU_DYNTICK_CTRL_MASK, &rdp->dynticks);
Paul E. McKenneyb8c17e62016-11-08 14:25:21 -0800275 smp_mb__after_atomic(); /* _exit after clearing mask. */
276 /* Prefer duplicate flushes to losing a flush. */
277 rcu_eqs_special_exit();
278 }
Paul E. McKenney2625d462016-11-02 14:23:30 -0700279}
280
281/*
282 * Reset the current CPU's ->dynticks counter to indicate that the
283 * newly onlined CPU is no longer in an extended quiescent state.
284 * This will either leave the counter unchanged, or increment it
285 * to the next non-quiescent value.
286 *
287 * The non-atomic test/increment sequence works because the upper bits
288 * of the ->dynticks counter are manipulated only by the corresponding CPU,
289 * or when the corresponding CPU is offline.
290 */
291static void rcu_dynticks_eqs_online(void)
292{
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -0700293 struct rcu_data *rdp = this_cpu_ptr(&rcu_data);
Paul E. McKenney2625d462016-11-02 14:23:30 -0700294
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -0700295 if (atomic_read(&rdp->dynticks) & RCU_DYNTICK_CTRL_CTR)
Paul E. McKenney2625d462016-11-02 14:23:30 -0700296 return;
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -0700297 atomic_add(RCU_DYNTICK_CTRL_CTR, &rdp->dynticks);
Paul E. McKenney2625d462016-11-02 14:23:30 -0700298}
299
300/*
Paul E. McKenney02a5c5502016-11-02 17:25:06 -0700301 * Is the current CPU in an extended quiescent state?
302 *
303 * No ordering, as we are sampling CPU-local information.
304 */
305bool rcu_dynticks_curr_cpu_in_eqs(void)
306{
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -0700307 struct rcu_data *rdp = this_cpu_ptr(&rcu_data);
Paul E. McKenney02a5c5502016-11-02 17:25:06 -0700308
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -0700309 return !(atomic_read(&rdp->dynticks) & RCU_DYNTICK_CTRL_CTR);
Paul E. McKenney02a5c5502016-11-02 17:25:06 -0700310}
311
312/*
Paul E. McKenney8b2f63a2016-11-02 14:12:05 -0700313 * Snapshot the ->dynticks counter with full ordering so as to allow
314 * stable comparison of this counter with past and future snapshots.
315 */
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -0700316int rcu_dynticks_snap(struct rcu_data *rdp)
Paul E. McKenney8b2f63a2016-11-02 14:12:05 -0700317{
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -0700318 int snap = atomic_add_return(0, &rdp->dynticks);
Paul E. McKenney8b2f63a2016-11-02 14:12:05 -0700319
Paul E. McKenneyb8c17e62016-11-08 14:25:21 -0800320 return snap & ~RCU_DYNTICK_CTRL_MASK;
Paul E. McKenney8b2f63a2016-11-02 14:12:05 -0700321}
322
323/*
Paul E. McKenney02a5c5502016-11-02 17:25:06 -0700324 * Return true if the snapshot returned from rcu_dynticks_snap()
325 * indicates that RCU is in an extended quiescent state.
326 */
327static bool rcu_dynticks_in_eqs(int snap)
328{
Paul E. McKenneyb8c17e62016-11-08 14:25:21 -0800329 return !(snap & RCU_DYNTICK_CTRL_CTR);
Paul E. McKenney02a5c5502016-11-02 17:25:06 -0700330}
331
332/*
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -0700333 * Return true if the CPU corresponding to the specified rcu_data
Paul E. McKenney02a5c5502016-11-02 17:25:06 -0700334 * structure has spent some time in an extended quiescent state since
335 * rcu_dynticks_snap() returned the specified snapshot.
336 */
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -0700337static bool rcu_dynticks_in_eqs_since(struct rcu_data *rdp, int snap)
Paul E. McKenney02a5c5502016-11-02 17:25:06 -0700338{
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -0700339 return snap != rcu_dynticks_snap(rdp);
Paul E. McKenney02a5c5502016-11-02 17:25:06 -0700340}
341
342/*
Paul E. McKenneyb8c17e62016-11-08 14:25:21 -0800343 * Set the special (bottom) bit of the specified CPU so that it
344 * will take special action (such as flushing its TLB) on the
345 * next exit from an extended quiescent state. Returns true if
346 * the bit was successfully set, or false if the CPU was not in
347 * an extended quiescent state.
348 */
349bool rcu_eqs_special_set(int cpu)
350{
351 int old;
352 int new;
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -0700353 struct rcu_data *rdp = &per_cpu(rcu_data, cpu);
Paul E. McKenneyb8c17e62016-11-08 14:25:21 -0800354
355 do {
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -0700356 old = atomic_read(&rdp->dynticks);
Paul E. McKenneyb8c17e62016-11-08 14:25:21 -0800357 if (old & RCU_DYNTICK_CTRL_CTR)
358 return false;
359 new = old | RCU_DYNTICK_CTRL_MASK;
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -0700360 } while (atomic_cmpxchg(&rdp->dynticks, old, new) != old);
Paul E. McKenneyb8c17e62016-11-08 14:25:21 -0800361 return true;
Paul E. McKenney6563de92016-11-02 13:33:57 -0700362}
Paul E. McKenney5cd37192014-12-13 20:32:04 -0800363
Paul E. McKenney4a81e832014-06-20 16:49:01 -0700364/*
365 * Let the RCU core know that this CPU has gone through the scheduler,
366 * which is a quiescent state. This is called when the need for a
367 * quiescent state is urgent, so we burn an atomic operation and full
368 * memory barriers to let the RCU core know about it, regardless of what
369 * this CPU might (or might not) do in the near future.
370 *
Paul E. McKenney0f9be8c2017-01-27 13:17:02 -0800371 * We inform the RCU core by emulating a zero-duration dyntick-idle period.
Paul E. McKenney46a5d162015-10-07 09:10:48 -0700372 *
Paul E. McKenney3b57a392018-05-16 16:01:56 -0700373 * The caller must have disabled interrupts and must not be idle.
Paul E. McKenney4a81e832014-06-20 16:49:01 -0700374 */
Paul E. McKenney395a2f02018-07-10 14:00:14 -0700375static void __maybe_unused rcu_momentary_dyntick_idle(void)
Paul E. McKenney4a81e832014-06-20 16:49:01 -0700376{
Paul E. McKenney3b57a392018-05-16 16:01:56 -0700377 int special;
378
Paul E. McKenney2dba13f2018-08-03 21:00:38 -0700379 raw_cpu_write(rcu_data.rcu_need_heavy_qs, false);
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -0700380 special = atomic_add_return(2 * RCU_DYNTICK_CTRL_CTR,
381 &this_cpu_ptr(&rcu_data)->dynticks);
Paul E. McKenney3b57a392018-05-16 16:01:56 -0700382 /* It is illegal to call this from idle state. */
383 WARN_ON_ONCE(!(special & RCU_DYNTICK_CTRL_CTR));
Paul E. McKenney3e310092018-06-21 12:50:01 -0700384 rcu_preempt_deferred_qs(current);
Paul E. McKenney4a81e832014-06-20 16:49:01 -0700385}
386
Paul E. McKenney45975c72018-07-02 14:30:37 -0700387/**
388 * rcu_is_cpu_rrupt_from_idle - see if idle or immediately interrupted from idle
Boqun Fengbb73c522015-07-30 16:55:38 -0700389 *
Paul E. McKenney45975c72018-07-02 14:30:37 -0700390 * If the current CPU is idle or running at a first-level (not nested)
391 * interrupt from idle, return true. The caller must have at least
392 * disabled preemption.
Paul E. McKenney5cd37192014-12-13 20:32:04 -0800393 */
Paul E. McKenney45975c72018-07-02 14:30:37 -0700394static int rcu_is_cpu_rrupt_from_idle(void)
Paul E. McKenney5cd37192014-12-13 20:32:04 -0800395{
Paul E. McKenney4c5273b2018-08-03 21:00:38 -0700396 return __this_cpu_read(rcu_data.dynticks_nesting) <= 0 &&
397 __this_cpu_read(rcu_data.dynticks_nmi_nesting) <= 1;
Paul E. McKenney5cd37192014-12-13 20:32:04 -0800398}
Paul E. McKenney5cd37192014-12-13 20:32:04 -0800399
Paul E. McKenney17c77982017-04-28 11:12:34 -0700400#define DEFAULT_RCU_BLIMIT 10 /* Maximum callbacks per rcu_do_batch. */
401static long blimit = DEFAULT_RCU_BLIMIT;
402#define DEFAULT_RCU_QHIMARK 10000 /* If this many pending, ignore blimit. */
403static long qhimark = DEFAULT_RCU_QHIMARK;
404#define DEFAULT_RCU_QLOMARK 100 /* Once only this many pending, use blimit. */
405static long qlowmark = DEFAULT_RCU_QLOMARK;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100406
Eric Dumazet878d7432012-10-18 04:55:36 -0700407module_param(blimit, long, 0444);
408module_param(qhimark, long, 0444);
409module_param(qlowmark, long, 0444);
Paul E. McKenney3d76c082009-09-28 07:46:32 -0700410
Paul E. McKenney026ad282013-04-03 22:14:11 -0700411static ulong jiffies_till_first_fqs = ULONG_MAX;
412static ulong jiffies_till_next_fqs = ULONG_MAX;
Paul E. McKenney8c7c4822016-01-03 20:29:57 -0800413static bool rcu_kick_kthreads;
Paul E. McKenneyd40011f2012-06-26 20:45:57 -0700414
Paul E. McKenneyc06aed02018-07-25 11:25:23 -0700415/*
416 * How long the grace period must be before we start recruiting
417 * quiescent-state help from rcu_note_context_switch().
418 */
419static ulong jiffies_till_sched_qs = ULONG_MAX;
420module_param(jiffies_till_sched_qs, ulong, 0444);
421static ulong jiffies_to_sched_qs; /* Adjusted version of above if not default */
422module_param(jiffies_to_sched_qs, ulong, 0444); /* Display only! */
423
424/*
425 * Make sure that we give the grace-period kthread time to detect any
426 * idle CPUs before taking active measures to force quiescent states.
427 * However, don't go below 100 milliseconds, adjusted upwards for really
428 * large systems.
429 */
430static void adjust_jiffies_till_sched_qs(void)
431{
432 unsigned long j;
433
434 /* If jiffies_till_sched_qs was specified, respect the request. */
435 if (jiffies_till_sched_qs != ULONG_MAX) {
436 WRITE_ONCE(jiffies_to_sched_qs, jiffies_till_sched_qs);
437 return;
438 }
439 j = READ_ONCE(jiffies_till_first_fqs) +
440 2 * READ_ONCE(jiffies_till_next_fqs);
441 if (j < HZ / 10 + nr_cpu_ids / RCU_JIFFIES_FQS_DIV)
442 j = HZ / 10 + nr_cpu_ids / RCU_JIFFIES_FQS_DIV;
443 pr_info("RCU calculated value of scheduler-enlistment delay is %ld jiffies.\n", j);
444 WRITE_ONCE(jiffies_to_sched_qs, j);
445}
446
Byungchul Park67abb962018-06-01 11:03:09 +0900447static int param_set_first_fqs_jiffies(const char *val, const struct kernel_param *kp)
448{
449 ulong j;
450 int ret = kstrtoul(val, 0, &j);
451
Paul E. McKenneyc06aed02018-07-25 11:25:23 -0700452 if (!ret) {
Byungchul Park67abb962018-06-01 11:03:09 +0900453 WRITE_ONCE(*(ulong *)kp->arg, (j > HZ) ? HZ : j);
Paul E. McKenneyc06aed02018-07-25 11:25:23 -0700454 adjust_jiffies_till_sched_qs();
455 }
Byungchul Park67abb962018-06-01 11:03:09 +0900456 return ret;
457}
458
459static int param_set_next_fqs_jiffies(const char *val, const struct kernel_param *kp)
460{
461 ulong j;
462 int ret = kstrtoul(val, 0, &j);
463
Paul E. McKenneyc06aed02018-07-25 11:25:23 -0700464 if (!ret) {
Byungchul Park67abb962018-06-01 11:03:09 +0900465 WRITE_ONCE(*(ulong *)kp->arg, (j > HZ) ? HZ : (j ?: 1));
Paul E. McKenneyc06aed02018-07-25 11:25:23 -0700466 adjust_jiffies_till_sched_qs();
467 }
Byungchul Park67abb962018-06-01 11:03:09 +0900468 return ret;
469}
470
471static struct kernel_param_ops first_fqs_jiffies_ops = {
472 .set = param_set_first_fqs_jiffies,
473 .get = param_get_ulong,
474};
475
476static struct kernel_param_ops next_fqs_jiffies_ops = {
477 .set = param_set_next_fqs_jiffies,
478 .get = param_get_ulong,
479};
480
481module_param_cb(jiffies_till_first_fqs, &first_fqs_jiffies_ops, &jiffies_till_first_fqs, 0644);
482module_param_cb(jiffies_till_next_fqs, &next_fqs_jiffies_ops, &jiffies_till_next_fqs, 0644);
Paul E. McKenney8c7c4822016-01-03 20:29:57 -0800483module_param(rcu_kick_kthreads, bool, 0644);
Paul E. McKenneyd40011f2012-06-26 20:45:57 -0700484
Paul E. McKenney8ff0b902018-07-05 17:55:14 -0700485static void force_qs_rnp(int (*f)(struct rcu_data *rdp));
Paul E. McKenneye3950ec2014-10-21 08:03:57 -0700486static int rcu_pending(void);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100487
488/*
Paul E. McKenney17ef2fe2018-04-27 11:39:34 -0700489 * Return the number of RCU GPs completed thus far for debug & stats.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100490 */
Paul E. McKenney17ef2fe2018-04-27 11:39:34 -0700491unsigned long rcu_get_gp_seq(void)
Paul E. McKenney917963d2014-11-21 17:10:16 -0800492{
Paul E. McKenney16fc9c62018-07-03 15:54:39 -0700493 return READ_ONCE(rcu_state.gp_seq);
Paul E. McKenney917963d2014-11-21 17:10:16 -0800494}
Paul E. McKenney17ef2fe2018-04-27 11:39:34 -0700495EXPORT_SYMBOL_GPL(rcu_get_gp_seq);
Paul E. McKenney917963d2014-11-21 17:10:16 -0800496
497/*
Paul E. McKenney291783b2016-01-12 13:43:30 -0800498 * Return the number of RCU expedited batches completed thus far for
499 * debug & stats. Odd numbers mean that a batch is in progress, even
500 * numbers mean idle. The value returned will thus be roughly double
501 * the cumulative batches since boot.
502 */
503unsigned long rcu_exp_batches_completed(void)
504{
Paul E. McKenney16fc9c62018-07-03 15:54:39 -0700505 return rcu_state.expedited_sequence;
Paul E. McKenney291783b2016-01-12 13:43:30 -0800506}
507EXPORT_SYMBOL_GPL(rcu_exp_batches_completed);
508
509/*
Paul E. McKenneyfd897572018-12-10 16:09:49 -0800510 * Return the root node of the rcu_state structure.
511 */
512static struct rcu_node *rcu_get_root(void)
513{
514 return &rcu_state.node[0];
515}
516
517/*
Paul E. McKenney69196012018-10-02 11:24:08 -0700518 * Convert a ->gp_state value to a character string.
519 */
520static const char *gp_state_getname(short gs)
521{
522 if (gs < 0 || gs >= ARRAY_SIZE(gp_state_names))
523 return "???";
524 return gp_state_names[gs];
525}
526
527/*
Paul E. McKenneyafea2272014-03-12 07:10:41 -0700528 * Show the state of the grace-period kthreads.
529 */
530void show_rcu_gp_kthreads(void)
531{
Paul E. McKenney47199a02018-05-28 10:33:08 -0700532 int cpu;
Paul E. McKenneyc669c012018-10-02 12:42:21 -0700533 unsigned long j;
Paul E. McKenneyfd897572018-12-10 16:09:49 -0800534 unsigned long ja;
535 unsigned long jr;
536 unsigned long jw;
Paul E. McKenney47199a02018-05-28 10:33:08 -0700537 struct rcu_data *rdp;
538 struct rcu_node *rnp;
Paul E. McKenneyafea2272014-03-12 07:10:41 -0700539
Paul E. McKenneyfd897572018-12-10 16:09:49 -0800540 j = jiffies;
541 ja = j - READ_ONCE(rcu_state.gp_activity);
542 jr = j - READ_ONCE(rcu_state.gp_req_activity);
543 jw = j - READ_ONCE(rcu_state.gp_wake_time);
544 pr_info("%s: wait state: %s(%d) ->state: %#lx delta ->gp_activity %lu ->gp_req_activity %lu ->gp_wake_time %lu ->gp_wake_seq %ld ->gp_seq %ld ->gp_seq_needed %ld ->gp_flags %#x\n",
Paul E. McKenneyc669c012018-10-02 12:42:21 -0700545 rcu_state.name, gp_state_getname(rcu_state.gp_state),
Paul E. McKenneyfd897572018-12-10 16:09:49 -0800546 rcu_state.gp_state,
547 rcu_state.gp_kthread ? rcu_state.gp_kthread->state : 0x1ffffL,
548 ja, jr, jw, (long)READ_ONCE(rcu_state.gp_wake_seq),
549 (long)READ_ONCE(rcu_state.gp_seq),
550 (long)READ_ONCE(rcu_get_root()->gp_seq_needed),
551 READ_ONCE(rcu_state.gp_flags));
Paul E. McKenneyb97d23c2018-07-04 15:35:00 -0700552 rcu_for_each_node_breadth_first(rnp) {
553 if (ULONG_CMP_GE(rcu_state.gp_seq, rnp->gp_seq_needed))
554 continue;
Paul E. McKenneyfd897572018-12-10 16:09:49 -0800555 pr_info("\trcu_node %d:%d ->gp_seq %ld ->gp_seq_needed %ld\n",
556 rnp->grplo, rnp->grphi, (long)rnp->gp_seq,
557 (long)rnp->gp_seq_needed);
Paul E. McKenneyb97d23c2018-07-04 15:35:00 -0700558 if (!rcu_is_leaf_node(rnp))
559 continue;
560 for_each_leaf_node_possible_cpu(rnp, cpu) {
561 rdp = per_cpu_ptr(&rcu_data, cpu);
562 if (rdp->gpwrap ||
563 ULONG_CMP_GE(rcu_state.gp_seq,
564 rdp->gp_seq_needed))
Paul E. McKenney47199a02018-05-28 10:33:08 -0700565 continue;
Paul E. McKenneyfd897572018-12-10 16:09:49 -0800566 pr_info("\tcpu %d ->gp_seq_needed %ld\n",
567 cpu, (long)rdp->gp_seq_needed);
Paul E. McKenney47199a02018-05-28 10:33:08 -0700568 }
Paul E. McKenneyafea2272014-03-12 07:10:41 -0700569 }
Paul E. McKenneyb97d23c2018-07-04 15:35:00 -0700570 /* sched_show_task(rcu_state.gp_kthread); */
Paul E. McKenneyafea2272014-03-12 07:10:41 -0700571}
572EXPORT_SYMBOL_GPL(show_rcu_gp_kthreads);
573
Paul E. McKenney2ccaff12018-12-12 12:32:06 -0800574/* Dump grace-period-request information due to commandeered sysrq. */
575static void sysrq_show_rcu(int key)
576{
577 show_rcu_gp_kthreads();
578}
579
580static struct sysrq_key_op sysrq_rcudump_op = {
581 .handler = sysrq_show_rcu,
582 .help_msg = "show-rcu(y)",
583 .action_msg = "Show RCU tree",
584 .enable_mask = SYSRQ_ENABLE_DUMP,
585};
586
587static int __init rcu_sysrq_init(void)
588{
589 if (sysrq_rcu)
590 return register_sysrq_key('y', &sysrq_rcudump_op);
591 return 0;
592}
593early_initcall(rcu_sysrq_init);
594
Paul E. McKenneyafea2272014-03-12 07:10:41 -0700595/*
Paul E. McKenneyad0dc7f2014-02-19 10:51:42 -0800596 * Send along grace-period-related data for rcutorture diagnostics.
597 */
598void rcutorture_get_gp_data(enum rcutorture_type test_type, int *flags,
Paul E. McKenneyaebc8262018-05-01 06:42:51 -0700599 unsigned long *gp_seq)
Paul E. McKenneyad0dc7f2014-02-19 10:51:42 -0800600{
Paul E. McKenneyad0dc7f2014-02-19 10:51:42 -0800601 switch (test_type) {
602 case RCU_FLAVOR:
Paul E. McKenneyf7dd7d42018-07-04 15:39:40 -0700603 *flags = READ_ONCE(rcu_state.gp_flags);
604 *gp_seq = rcu_seq_current(&rcu_state.gp_seq);
Paul E. McKenneyad0dc7f2014-02-19 10:51:42 -0800605 break;
606 default:
607 break;
608 }
Paul E. McKenneyad0dc7f2014-02-19 10:51:42 -0800609}
610EXPORT_SYMBOL_GPL(rcutorture_get_gp_data);
611
612/*
Paul E. McKenney215bba92017-10-05 16:37:03 -0700613 * Enter an RCU extended quiescent state, which can be either the
614 * idle loop or adaptive-tickless usermode execution.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100615 *
Paul E. McKenney215bba92017-10-05 16:37:03 -0700616 * We crowbar the ->dynticks_nmi_nesting field to zero to allow for
617 * the possibility of usermode upcalls having messed up our count
618 * of interrupt nesting level during the prior busy period.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100619 */
Paul E. McKenney215bba92017-10-05 16:37:03 -0700620static void rcu_eqs_enter(bool user)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100621{
Paul E. McKenney4c5273b2018-08-03 21:00:38 -0700622 struct rcu_data *rdp = this_cpu_ptr(&rcu_data);
Paul E. McKenney215bba92017-10-05 16:37:03 -0700623
Paul E. McKenney4c5273b2018-08-03 21:00:38 -0700624 WARN_ON_ONCE(rdp->dynticks_nmi_nesting != DYNTICK_IRQ_NONIDLE);
625 WRITE_ONCE(rdp->dynticks_nmi_nesting, 0);
Paul E. McKenney215bba92017-10-05 16:37:03 -0700626 WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) &&
Paul E. McKenney4c5273b2018-08-03 21:00:38 -0700627 rdp->dynticks_nesting == 0);
628 if (rdp->dynticks_nesting != 1) {
629 rdp->dynticks_nesting--;
Paul E. McKenney215bba92017-10-05 16:37:03 -0700630 return;
631 }
Paul E. McKenney96d3fd02013-10-04 14:33:34 -0700632
Frederic Weisbeckerb04db8e2017-11-06 16:01:30 +0100633 lockdep_assert_irqs_disabled();
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -0700634 trace_rcu_dyntick(TPS("Start"), rdp->dynticks_nesting, 0, rdp->dynticks);
Paul E. McKenneye68bbb22017-10-05 19:55:31 -0700635 WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) && !user && !is_idle_task(current));
Paul E. McKenneyb97d23c2018-07-04 15:35:00 -0700636 rdp = this_cpu_ptr(&rcu_data);
637 do_nocb_deferred_wakeup(rdp);
Paul E. McKenney198bbf82014-10-22 15:03:43 -0700638 rcu_prepare_for_idle();
Paul E. McKenney3e310092018-06-21 12:50:01 -0700639 rcu_preempt_deferred_qs(current);
Paul E. McKenney4c5273b2018-08-03 21:00:38 -0700640 WRITE_ONCE(rdp->dynticks_nesting, 0); /* Avoid irq-access tearing. */
Paul E. McKenney844ccdd2017-10-03 16:51:47 -0700641 rcu_dynticks_eqs_enter();
Paul E. McKenney176f8f72014-08-04 17:43:50 -0700642 rcu_dynticks_task_enter();
Frederic Weisbeckeradf50912012-06-28 11:20:21 -0700643}
644
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700645/**
646 * rcu_idle_enter - inform RCU that current CPU is entering idle
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100647 *
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700648 * Enter idle mode, in other words, -leave- the mode in which RCU
649 * read-side critical sections can occur. (Though RCU read-side
650 * critical sections can occur in irq handlers in idle, a possibility
651 * handled by irq_enter() and irq_exit().)
652 *
Paul E. McKenneyc0da3132017-09-22 09:58:47 -0700653 * If you add or remove a call to rcu_idle_enter(), be sure to test with
654 * CONFIG_RCU_EQS_DEBUG=y.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100655 */
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700656void rcu_idle_enter(void)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100657{
Frederic Weisbeckerb04db8e2017-11-06 16:01:30 +0100658 lockdep_assert_irqs_disabled();
Paul E. McKenneycb349ca2012-09-04 17:35:31 -0700659 rcu_eqs_enter(false);
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700660}
661
Paul E. McKenneyd1ec4c32015-05-13 10:41:58 -0700662#ifdef CONFIG_NO_HZ_FULL
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700663/**
Frederic Weisbeckeradf50912012-06-28 11:20:21 -0700664 * rcu_user_enter - inform RCU that we are resuming userspace.
665 *
666 * Enter RCU idle mode right before resuming userspace. No use of RCU
667 * is permitted between this call and rcu_user_exit(). This way the
668 * CPU doesn't need to maintain the tick for RCU maintenance purposes
669 * when the CPU runs in userspace.
Paul E. McKenneyc0da3132017-09-22 09:58:47 -0700670 *
671 * If you add or remove a call to rcu_user_enter(), be sure to test with
672 * CONFIG_RCU_EQS_DEBUG=y.
Frederic Weisbeckeradf50912012-06-28 11:20:21 -0700673 */
674void rcu_user_enter(void)
675{
Frederic Weisbeckerb04db8e2017-11-06 16:01:30 +0100676 lockdep_assert_irqs_disabled();
Paul E. McKenneyd4db30a2017-07-12 09:03:35 -0700677 rcu_eqs_enter(true);
Frederic Weisbeckeradf50912012-06-28 11:20:21 -0700678}
Paul E. McKenneyd1ec4c32015-05-13 10:41:58 -0700679#endif /* CONFIG_NO_HZ_FULL */
Frederic Weisbecker19dd15912012-06-04 16:42:35 -0700680
Byungchul Parkcf7614e2018-06-22 15:12:06 +0900681/*
Paul E. McKenneyfd581a92017-10-02 21:56:20 -0700682 * If we are returning from the outermost NMI handler that interrupted an
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -0700683 * RCU-idle period, update rdp->dynticks and rdp->dynticks_nmi_nesting
Paul E. McKenneyfd581a92017-10-02 21:56:20 -0700684 * to let the RCU grace-period handling know that the CPU is back to
685 * being RCU-idle.
686 *
Byungchul Parkcf7614e2018-06-22 15:12:06 +0900687 * If you add or remove a call to rcu_nmi_exit_common(), be sure to test
Paul E. McKenneyfd581a92017-10-02 21:56:20 -0700688 * with CONFIG_RCU_EQS_DEBUG=y.
689 */
Byungchul Parkcf7614e2018-06-22 15:12:06 +0900690static __always_inline void rcu_nmi_exit_common(bool irq)
Paul E. McKenneyfd581a92017-10-02 21:56:20 -0700691{
Paul E. McKenney4c5273b2018-08-03 21:00:38 -0700692 struct rcu_data *rdp = this_cpu_ptr(&rcu_data);
Paul E. McKenneyfd581a92017-10-02 21:56:20 -0700693
694 /*
695 * Check for ->dynticks_nmi_nesting underflow and bad ->dynticks.
696 * (We are exiting an NMI handler, so RCU better be paying attention
697 * to us!)
698 */
Paul E. McKenney4c5273b2018-08-03 21:00:38 -0700699 WARN_ON_ONCE(rdp->dynticks_nmi_nesting <= 0);
Paul E. McKenneyfd581a92017-10-02 21:56:20 -0700700 WARN_ON_ONCE(rcu_dynticks_curr_cpu_in_eqs());
701
702 /*
703 * If the nesting level is not 1, the CPU wasn't RCU-idle, so
704 * leave it in non-RCU-idle state.
705 */
Paul E. McKenney4c5273b2018-08-03 21:00:38 -0700706 if (rdp->dynticks_nmi_nesting != 1) {
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -0700707 trace_rcu_dyntick(TPS("--="), rdp->dynticks_nmi_nesting, rdp->dynticks_nmi_nesting - 2, rdp->dynticks);
Paul E. McKenney4c5273b2018-08-03 21:00:38 -0700708 WRITE_ONCE(rdp->dynticks_nmi_nesting, /* No store tearing. */
709 rdp->dynticks_nmi_nesting - 2);
Paul E. McKenneyfd581a92017-10-02 21:56:20 -0700710 return;
711 }
712
713 /* This NMI interrupted an RCU-idle CPU, restore RCU-idleness. */
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -0700714 trace_rcu_dyntick(TPS("Startirq"), rdp->dynticks_nmi_nesting, 0, rdp->dynticks);
Paul E. McKenney4c5273b2018-08-03 21:00:38 -0700715 WRITE_ONCE(rdp->dynticks_nmi_nesting, 0); /* Avoid store tearing. */
Byungchul Parkcf7614e2018-06-22 15:12:06 +0900716
717 if (irq)
718 rcu_prepare_for_idle();
719
Paul E. McKenneyfd581a92017-10-02 21:56:20 -0700720 rcu_dynticks_eqs_enter();
Byungchul Parkcf7614e2018-06-22 15:12:06 +0900721
722 if (irq)
723 rcu_dynticks_task_enter();
724}
725
726/**
727 * rcu_nmi_exit - inform RCU of exit from NMI context
728 * @irq: Is this call from rcu_irq_exit?
729 *
730 * If you add or remove a call to rcu_nmi_exit(), be sure to test
731 * with CONFIG_RCU_EQS_DEBUG=y.
732 */
733void rcu_nmi_exit(void)
734{
735 rcu_nmi_exit_common(false);
Paul E. McKenneyfd581a92017-10-02 21:56:20 -0700736}
737
738/**
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700739 * rcu_irq_exit - inform RCU that current CPU is exiting irq towards idle
740 *
741 * Exit from an interrupt handler, which might possibly result in entering
742 * idle mode, in other words, leaving the mode in which read-side critical
Paul E. McKenney7c9906c2015-10-31 00:59:01 -0700743 * sections can occur. The caller must have disabled interrupts.
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700744 *
745 * This code assumes that the idle loop never does anything that might
746 * result in unbalanced calls to irq_enter() and irq_exit(). If your
Paul E. McKenney58721f52017-10-03 10:42:22 -0700747 * architecture's idle loop violates this assumption, RCU will give you what
748 * you deserve, good and hard. But very infrequently and irreproducibly.
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700749 *
750 * Use things like work queues to work around this limitation.
751 *
752 * You have been warned.
Paul E. McKenneyc0da3132017-09-22 09:58:47 -0700753 *
754 * If you add or remove a call to rcu_irq_exit(), be sure to test with
755 * CONFIG_RCU_EQS_DEBUG=y.
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700756 */
757void rcu_irq_exit(void)
758{
Frederic Weisbeckerb04db8e2017-11-06 16:01:30 +0100759 lockdep_assert_irqs_disabled();
Byungchul Parkcf7614e2018-06-22 15:12:06 +0900760 rcu_nmi_exit_common(true);
Paul E. McKenney7c9906c2015-10-31 00:59:01 -0700761}
762
763/*
764 * Wrapper for rcu_irq_exit() where interrupts are enabled.
Paul E. McKenneyc0da3132017-09-22 09:58:47 -0700765 *
766 * If you add or remove a call to rcu_irq_exit_irqson(), be sure to test
767 * with CONFIG_RCU_EQS_DEBUG=y.
Paul E. McKenney7c9906c2015-10-31 00:59:01 -0700768 */
769void rcu_irq_exit_irqson(void)
770{
771 unsigned long flags;
772
773 local_irq_save(flags);
774 rcu_irq_exit();
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700775 local_irq_restore(flags);
776}
777
778/*
Frederic Weisbeckeradf50912012-06-28 11:20:21 -0700779 * Exit an RCU extended quiescent state, which can be either the
780 * idle loop or adaptive-tickless usermode execution.
Paul E. McKenney51a1fd32017-10-03 14:43:40 -0700781 *
782 * We crowbar the ->dynticks_nmi_nesting field to DYNTICK_IRQ_NONIDLE to
783 * allow for the possibility of usermode upcalls messing up our count of
784 * interrupt nesting level during the busy period that is just now starting.
Frederic Weisbeckeradf50912012-06-28 11:20:21 -0700785 */
786static void rcu_eqs_exit(bool user)
787{
Paul E. McKenney4c5273b2018-08-03 21:00:38 -0700788 struct rcu_data *rdp;
Paul E. McKenney84585aa2017-10-04 15:55:16 -0700789 long oldval;
Frederic Weisbeckeradf50912012-06-28 11:20:21 -0700790
Frederic Weisbeckerb04db8e2017-11-06 16:01:30 +0100791 lockdep_assert_irqs_disabled();
Paul E. McKenney4c5273b2018-08-03 21:00:38 -0700792 rdp = this_cpu_ptr(&rcu_data);
793 oldval = rdp->dynticks_nesting;
Paul E. McKenney1ce46ee2015-05-05 23:04:22 -0700794 WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) && oldval < 0);
Paul E. McKenney51a1fd32017-10-03 14:43:40 -0700795 if (oldval) {
Paul E. McKenney4c5273b2018-08-03 21:00:38 -0700796 rdp->dynticks_nesting++;
Paul E. McKenney9dd238e2017-10-05 16:56:26 -0700797 return;
Paul E. McKenney3a5924052013-10-04 18:48:55 -0700798 }
Paul E. McKenney9dd238e2017-10-05 16:56:26 -0700799 rcu_dynticks_task_exit();
800 rcu_dynticks_eqs_exit();
801 rcu_cleanup_after_idle();
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -0700802 trace_rcu_dyntick(TPS("End"), rdp->dynticks_nesting, 1, rdp->dynticks);
Paul E. McKenneye68bbb22017-10-05 19:55:31 -0700803 WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) && !user && !is_idle_task(current));
Paul E. McKenney4c5273b2018-08-03 21:00:38 -0700804 WRITE_ONCE(rdp->dynticks_nesting, 1);
805 WARN_ON_ONCE(rdp->dynticks_nmi_nesting);
806 WRITE_ONCE(rdp->dynticks_nmi_nesting, DYNTICK_IRQ_NONIDLE);
Frederic Weisbeckeradf50912012-06-28 11:20:21 -0700807}
808
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700809/**
810 * rcu_idle_exit - inform RCU that current CPU is leaving idle
811 *
812 * Exit idle mode, in other words, -enter- the mode in which RCU
813 * read-side critical sections can occur.
814 *
Paul E. McKenneyc0da3132017-09-22 09:58:47 -0700815 * If you add or remove a call to rcu_idle_exit(), be sure to test with
816 * CONFIG_RCU_EQS_DEBUG=y.
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700817 */
818void rcu_idle_exit(void)
819{
Frederic Weisbeckerc5d900b2012-07-11 20:26:31 +0200820 unsigned long flags;
821
822 local_irq_save(flags);
Paul E. McKenneycb349ca2012-09-04 17:35:31 -0700823 rcu_eqs_exit(false);
Frederic Weisbeckerc5d900b2012-07-11 20:26:31 +0200824 local_irq_restore(flags);
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700825}
826
Paul E. McKenneyd1ec4c32015-05-13 10:41:58 -0700827#ifdef CONFIG_NO_HZ_FULL
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700828/**
Frederic Weisbeckeradf50912012-06-28 11:20:21 -0700829 * rcu_user_exit - inform RCU that we are exiting userspace.
830 *
831 * Exit RCU idle mode while entering the kernel because it can
832 * run a RCU read side critical section anytime.
Paul E. McKenneyc0da3132017-09-22 09:58:47 -0700833 *
834 * If you add or remove a call to rcu_user_exit(), be sure to test with
835 * CONFIG_RCU_EQS_DEBUG=y.
Frederic Weisbeckeradf50912012-06-28 11:20:21 -0700836 */
837void rcu_user_exit(void)
838{
Frederic Weisbecker91d1aa432012-11-27 19:33:25 +0100839 rcu_eqs_exit(1);
Frederic Weisbeckeradf50912012-06-28 11:20:21 -0700840}
Paul E. McKenneyd1ec4c32015-05-13 10:41:58 -0700841#endif /* CONFIG_NO_HZ_FULL */
Frederic Weisbecker19dd15912012-06-04 16:42:35 -0700842
843/**
Byungchul Parkcf7614e2018-06-22 15:12:06 +0900844 * rcu_nmi_enter_common - inform RCU of entry to NMI context
845 * @irq: Is this call from rcu_irq_enter?
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100846 *
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -0700847 * If the CPU was idle from RCU's viewpoint, update rdp->dynticks and
Paul E. McKenney4c5273b2018-08-03 21:00:38 -0700848 * rdp->dynticks_nmi_nesting to let the RCU grace-period handling know
Paul E. McKenney734d1682014-11-21 14:45:12 -0800849 * that the CPU is active. This implementation permits nested NMIs, as
850 * long as the nesting level does not overflow an int. (You will probably
851 * run out of stack space first.)
Paul E. McKenneyc0da3132017-09-22 09:58:47 -0700852 *
Byungchul Parkcf7614e2018-06-22 15:12:06 +0900853 * If you add or remove a call to rcu_nmi_enter_common(), be sure to test
Paul E. McKenneyc0da3132017-09-22 09:58:47 -0700854 * with CONFIG_RCU_EQS_DEBUG=y.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100855 */
Byungchul Parkcf7614e2018-06-22 15:12:06 +0900856static __always_inline void rcu_nmi_enter_common(bool irq)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100857{
Paul E. McKenney4c5273b2018-08-03 21:00:38 -0700858 struct rcu_data *rdp = this_cpu_ptr(&rcu_data);
Paul E. McKenney84585aa2017-10-04 15:55:16 -0700859 long incby = 2;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100860
Paul E. McKenney734d1682014-11-21 14:45:12 -0800861 /* Complain about underflow. */
Paul E. McKenney4c5273b2018-08-03 21:00:38 -0700862 WARN_ON_ONCE(rdp->dynticks_nmi_nesting < 0);
Paul E. McKenney734d1682014-11-21 14:45:12 -0800863
864 /*
865 * If idle from RCU viewpoint, atomically increment ->dynticks
866 * to mark non-idle and increment ->dynticks_nmi_nesting by one.
867 * Otherwise, increment ->dynticks_nmi_nesting by two. This means
868 * if ->dynticks_nmi_nesting is equal to one, we are guaranteed
869 * to be in the outermost NMI handler that interrupted an RCU-idle
870 * period (observation due to Andy Lutomirski).
871 */
Paul E. McKenney02a5c5502016-11-02 17:25:06 -0700872 if (rcu_dynticks_curr_cpu_in_eqs()) {
Byungchul Parkcf7614e2018-06-22 15:12:06 +0900873
874 if (irq)
875 rcu_dynticks_task_exit();
876
Paul E. McKenney2625d462016-11-02 14:23:30 -0700877 rcu_dynticks_eqs_exit();
Byungchul Parkcf7614e2018-06-22 15:12:06 +0900878
879 if (irq)
880 rcu_cleanup_after_idle();
881
Paul E. McKenney734d1682014-11-21 14:45:12 -0800882 incby = 1;
883 }
Paul E. McKenneybd2b8792017-10-04 12:29:01 -0700884 trace_rcu_dyntick(incby == 1 ? TPS("Endirq") : TPS("++="),
Paul E. McKenney4c5273b2018-08-03 21:00:38 -0700885 rdp->dynticks_nmi_nesting,
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -0700886 rdp->dynticks_nmi_nesting + incby, rdp->dynticks);
Paul E. McKenney4c5273b2018-08-03 21:00:38 -0700887 WRITE_ONCE(rdp->dynticks_nmi_nesting, /* Prevent store tearing. */
888 rdp->dynticks_nmi_nesting + incby);
Paul E. McKenney734d1682014-11-21 14:45:12 -0800889 barrier();
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100890}
891
892/**
Byungchul Parkcf7614e2018-06-22 15:12:06 +0900893 * rcu_nmi_enter - inform RCU of entry to NMI context
894 */
895void rcu_nmi_enter(void)
896{
897 rcu_nmi_enter_common(false);
898}
899
900/**
Paul E. McKenney34240692011-10-03 11:38:52 -0700901 * rcu_irq_enter - inform RCU that current CPU is entering irq away from idle
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100902 *
Paul E. McKenney34240692011-10-03 11:38:52 -0700903 * Enter an interrupt handler, which might possibly result in exiting
904 * idle mode, in other words, entering the mode in which read-side critical
905 * sections can occur. The caller must have disabled interrupts.
Paul E. McKenneyc0da3132017-09-22 09:58:47 -0700906 *
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700907 * Note that the Linux kernel is fully capable of entering an interrupt
Paul E. McKenney58721f52017-10-03 10:42:22 -0700908 * handler that it never exits, for example when doing upcalls to user mode!
909 * This code assumes that the idle loop never does upcalls to user mode.
910 * If your architecture's idle loop does do upcalls to user mode (or does
911 * anything else that results in unbalanced calls to the irq_enter() and
912 * irq_exit() functions), RCU will give you what you deserve, good and hard.
913 * But very infrequently and irreproducibly.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100914 *
915 * Use things like work queues to work around this limitation.
916 *
917 * You have been warned.
918 *
919 * If you add or remove a call to rcu_irq_enter(), be sure to test with
920 * CONFIG_RCU_EQS_DEBUG=y.
Paul E. McKenney23b5c8f2010-09-07 10:38:22 -0700921 */
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100922void rcu_irq_enter(void)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100923{
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100924 lockdep_assert_irqs_disabled();
Byungchul Parkcf7614e2018-06-22 15:12:06 +0900925 rcu_nmi_enter_common(true);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100926}
Paul E. McKenney734d1682014-11-21 14:45:12 -0800927
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100928/*
929 * Wrapper for rcu_irq_enter() where interrupts are enabled.
930 *
931 * If you add or remove a call to rcu_irq_enter_irqson(), be sure to test
932 * with CONFIG_RCU_EQS_DEBUG=y.
933 */
934void rcu_irq_enter_irqson(void)
935{
936 unsigned long flags;
Paul E. McKenney734d1682014-11-21 14:45:12 -0800937
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100938 local_irq_save(flags);
939 rcu_irq_enter();
940 local_irq_restore(flags);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100941}
942
943/**
Zhouyi Zhou2320bda2018-10-08 06:50:41 +0000944 * rcu_is_watching - see if RCU thinks that the current CPU is not idle
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100945 *
Paul E. McKenney791875d2017-05-03 11:06:05 -0700946 * Return true if RCU is watching the running CPU, which means that this
947 * CPU can safely enter RCU read-side critical sections. In other words,
Zhouyi Zhou2320bda2018-10-08 06:50:41 +0000948 * if the current CPU is not in its idle loop or is in an interrupt or
949 * NMI handler, return true.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100950 */
Linus Torvaldsb29c8302013-11-16 12:23:18 -0800951bool notrace rcu_is_watching(void)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100952{
Pranith Kumarf534ed12014-07-08 18:26:11 -0400953 bool ret;
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700954
Alexei Starovoitov46f00d12015-06-16 10:35:18 -0700955 preempt_disable_notrace();
Paul E. McKenney791875d2017-05-03 11:06:05 -0700956 ret = !rcu_dynticks_curr_cpu_in_eqs();
Alexei Starovoitov46f00d12015-06-16 10:35:18 -0700957 preempt_enable_notrace();
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700958 return ret;
959}
Paul E. McKenney5c173eb2013-09-13 17:20:11 -0700960EXPORT_SYMBOL_GPL(rcu_is_watching);
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700961
Paul E. McKenneybcbfdd02017-04-11 15:50:41 -0700962/*
963 * If a holdout task is actually running, request an urgent quiescent
964 * state from its CPU. This is unsynchronized, so migrations can cause
965 * the request to go to the wrong CPU. Which is OK, all that will happen
966 * is that the CPU's next context switch will be a bit slower and next
967 * time around this task will generate another request.
968 */
969void rcu_request_urgent_qs_task(struct task_struct *t)
970{
971 int cpu;
972
973 barrier();
974 cpu = task_cpu(t);
975 if (!task_curr(t))
976 return; /* This task is not running on that CPU. */
Paul E. McKenney2dba13f2018-08-03 21:00:38 -0700977 smp_store_release(per_cpu_ptr(&rcu_data.rcu_urgent_qs, cpu), true);
Paul E. McKenneybcbfdd02017-04-11 15:50:41 -0700978}
979
Paul E. McKenney62fde6e2012-05-22 22:10:24 -0700980#if defined(CONFIG_PROVE_RCU) && defined(CONFIG_HOTPLUG_CPU)
Paul E. McKenneyc0d6d012012-01-23 12:41:26 -0800981
982/*
Paul E. McKenney55547882018-05-15 10:14:34 -0700983 * Is the current CPU online as far as RCU is concerned?
Paul E. McKenney2036d942012-01-30 17:02:47 -0800984 *
Paul E. McKenney55547882018-05-15 10:14:34 -0700985 * Disable preemption to avoid false positives that could otherwise
986 * happen due to the current CPU number being sampled, this task being
987 * preempted, its old CPU being taken offline, resuming on some other CPU,
Paul E. McKenney49918a52018-07-07 18:12:26 -0700988 * then determining that its old CPU is now offline.
Paul E. McKenneyc0d6d012012-01-23 12:41:26 -0800989 *
Paul E. McKenney55547882018-05-15 10:14:34 -0700990 * Disable checking if in an NMI handler because we cannot safely
991 * report errors from NMI handlers anyway. In addition, it is OK to use
992 * RCU on an offline processor during initial boot, hence the check for
993 * rcu_scheduler_fully_active.
Paul E. McKenneyc0d6d012012-01-23 12:41:26 -0800994 */
995bool rcu_lockdep_current_cpu_online(void)
996{
Paul E. McKenney2036d942012-01-30 17:02:47 -0800997 struct rcu_data *rdp;
998 struct rcu_node *rnp;
Paul E. McKenneyb97d23c2018-07-04 15:35:00 -0700999 bool ret = false;
Paul E. McKenneyc0d6d012012-01-23 12:41:26 -08001000
Paul E. McKenney55547882018-05-15 10:14:34 -07001001 if (in_nmi() || !rcu_scheduler_fully_active)
Fengguang Wuf6f7ee92013-10-10 11:08:33 -07001002 return true;
Paul E. McKenneyc0d6d012012-01-23 12:41:26 -08001003 preempt_disable();
Paul E. McKenneyb97d23c2018-07-04 15:35:00 -07001004 rdp = this_cpu_ptr(&rcu_data);
1005 rnp = rdp->mynode;
1006 if (rdp->grpmask & rcu_rnp_online_cpus(rnp))
1007 ret = true;
Paul E. McKenneyc0d6d012012-01-23 12:41:26 -08001008 preempt_enable();
Paul E. McKenneyb97d23c2018-07-04 15:35:00 -07001009 return ret;
Paul E. McKenneyc0d6d012012-01-23 12:41:26 -08001010}
1011EXPORT_SYMBOL_GPL(rcu_lockdep_current_cpu_online);
1012
Paul E. McKenney62fde6e2012-05-22 22:10:24 -07001013#endif /* #if defined(CONFIG_PROVE_RCU) && defined(CONFIG_HOTPLUG_CPU) */
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -07001014
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001015/*
Paul E. McKenney9b9500d2017-08-17 17:05:59 -07001016 * We are reporting a quiescent state on behalf of some other CPU, so
1017 * it is our responsibility to check for and handle potential overflow
Paul E. McKenneya66ae8a2018-04-27 18:06:08 -07001018 * of the rcu_node ->gp_seq counter with respect to the rcu_data counters.
Paul E. McKenney9b9500d2017-08-17 17:05:59 -07001019 * After all, the CPU might be in deep idle state, and thus executing no
1020 * code whatsoever.
1021 */
1022static void rcu_gpnum_ovf(struct rcu_node *rnp, struct rcu_data *rdp)
1023{
Matthew Wilcoxa32e01e2018-01-17 06:24:30 -08001024 raw_lockdep_assert_held_rcu_node(rnp);
Paul E. McKenneya66ae8a2018-04-27 18:06:08 -07001025 if (ULONG_CMP_LT(rcu_seq_current(&rdp->gp_seq) + ULONG_MAX / 4,
1026 rnp->gp_seq))
Paul E. McKenney9b9500d2017-08-17 17:05:59 -07001027 WRITE_ONCE(rdp->gpwrap, true);
Paul E. McKenney8aa670c2018-04-28 14:15:40 -07001028 if (ULONG_CMP_LT(rdp->rcu_iw_gp_seq + ULONG_MAX / 4, rnp->gp_seq))
1029 rdp->rcu_iw_gp_seq = rnp->gp_seq + ULONG_MAX / 4;
Paul E. McKenney9b9500d2017-08-17 17:05:59 -07001030}
1031
1032/*
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001033 * Snapshot the specified CPU's dynticks counter so that we can later
1034 * credit them with an implicit quiescent state. Return 1 if this CPU
Paul E. McKenney1eba8f82009-09-23 09:50:42 -07001035 * is in dynticks idle mode, which is an extended quiescent state.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001036 */
Paul E. McKenneyfe5ac722017-05-11 11:26:22 -07001037static int dyntick_save_progress_counter(struct rcu_data *rdp)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001038{
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -07001039 rdp->dynticks_snap = rcu_dynticks_snap(rdp);
Paul E. McKenney02a5c5502016-11-02 17:25:06 -07001040 if (rcu_dynticks_in_eqs(rdp->dynticks_snap)) {
Paul E. McKenney88d1bea2018-07-04 14:45:00 -07001041 trace_rcu_fqs(rcu_state.name, rdp->gp_seq, rdp->cpu, TPS("dti"));
Paul E. McKenney9b9500d2017-08-17 17:05:59 -07001042 rcu_gpnum_ovf(rdp->mynode, rdp);
Paul E. McKenney23a9bac2015-12-13 08:57:10 -08001043 return 1;
Andreea-Cristina Bernat7941dbd2014-03-17 18:33:28 +02001044 }
Paul E. McKenney23a9bac2015-12-13 08:57:10 -08001045 return 0;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001046}
1047
1048/*
Paul E. McKenney9b9500d2017-08-17 17:05:59 -07001049 * Handler for the irq_work request posted when a grace period has
1050 * gone on for too long, but not yet long enough for an RCU CPU
1051 * stall warning. Set state appropriately, but just complain if
1052 * there is unexpected state on entry.
1053 */
1054static void rcu_iw_handler(struct irq_work *iwp)
1055{
1056 struct rcu_data *rdp;
1057 struct rcu_node *rnp;
1058
1059 rdp = container_of(iwp, struct rcu_data, rcu_iw);
1060 rnp = rdp->mynode;
1061 raw_spin_lock_rcu_node(rnp);
1062 if (!WARN_ON_ONCE(!rdp->rcu_iw_pending)) {
Paul E. McKenney8aa670c2018-04-28 14:15:40 -07001063 rdp->rcu_iw_gp_seq = rnp->gp_seq;
Paul E. McKenney9b9500d2017-08-17 17:05:59 -07001064 rdp->rcu_iw_pending = false;
1065 }
1066 raw_spin_unlock_rcu_node(rnp);
1067}
1068
1069/*
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001070 * Return true if the specified CPU has passed through a quiescent
1071 * state by virtue of being in or having passed through an dynticks
1072 * idle state since the last call to dyntick_save_progress_counter()
Paul E. McKenneya82dcc72012-08-01 14:29:20 -07001073 * for this same CPU, or by virtue of having been offline.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001074 */
Paul E. McKenneyfe5ac722017-05-11 11:26:22 -07001075static int rcu_implicit_dynticks_qs(struct rcu_data *rdp)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001076{
Paul E. McKenney3a19b462016-11-30 11:21:21 -08001077 unsigned long jtsq;
Paul E. McKenney0f9be8c2017-01-27 13:17:02 -08001078 bool *rnhqp;
Paul E. McKenney9226b102017-01-27 14:17:50 -08001079 bool *ruqp;
Paul E. McKenney9b9500d2017-08-17 17:05:59 -07001080 struct rcu_node *rnp = rdp->mynode;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001081
1082 /*
1083 * If the CPU passed through or entered a dynticks idle phase with
1084 * no active irq/NMI handlers, then we can safely pretend that the CPU
1085 * already acknowledged the request to pass through a quiescent
1086 * state. Either way, that CPU cannot possibly be in an RCU
1087 * read-side critical section that started before the beginning
1088 * of the current RCU grace period.
1089 */
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -07001090 if (rcu_dynticks_in_eqs_since(rdp, rdp->dynticks_snap)) {
Paul E. McKenney88d1bea2018-07-04 14:45:00 -07001091 trace_rcu_fqs(rcu_state.name, rdp->gp_seq, rdp->cpu, TPS("dti"));
Paul E. McKenney9b9500d2017-08-17 17:05:59 -07001092 rcu_gpnum_ovf(rnp, rdp);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001093 return 1;
1094 }
1095
Paul E. McKenneyf2e2df52018-05-15 16:23:23 -07001096 /* If waiting too long on an offline CPU, complain. */
1097 if (!(rdp->grpmask & rcu_rnp_online_cpus(rnp)) &&
Paul E. McKenney88d1bea2018-07-04 14:45:00 -07001098 time_after(jiffies, rcu_state.gp_start + HZ)) {
Paul E. McKenneyf2e2df52018-05-15 16:23:23 -07001099 bool onl;
1100 struct rcu_node *rnp1;
1101
1102 WARN_ON(1); /* Offline CPUs are supposed to report QS! */
1103 pr_info("%s: grp: %d-%d level: %d ->gp_seq %ld ->completedqs %ld\n",
1104 __func__, rnp->grplo, rnp->grphi, rnp->level,
1105 (long)rnp->gp_seq, (long)rnp->completedqs);
1106 for (rnp1 = rnp; rnp1; rnp1 = rnp1->parent)
1107 pr_info("%s: %d:%d ->qsmask %#lx ->qsmaskinit %#lx ->qsmaskinitnext %#lx ->rcu_gp_init_mask %#lx\n",
1108 __func__, rnp1->grplo, rnp1->grphi, rnp1->qsmask, rnp1->qsmaskinit, rnp1->qsmaskinitnext, rnp1->rcu_gp_init_mask);
1109 onl = !!(rdp->grpmask & rcu_rnp_online_cpus(rnp));
1110 pr_info("%s %d: %c online: %ld(%d) offline: %ld(%d)\n",
1111 __func__, rdp->cpu, ".o"[onl],
1112 (long)rdp->rcu_onl_gp_seq, rdp->rcu_onl_gp_flags,
1113 (long)rdp->rcu_ofl_gp_seq, rdp->rcu_ofl_gp_flags);
1114 return 1; /* Break things loose after complaining. */
1115 }
1116
Paul E. McKenney65d798f2013-04-12 16:19:10 -07001117 /*
Paul E. McKenney4a81e832014-06-20 16:49:01 -07001118 * A CPU running for an extended time within the kernel can
Paul E. McKenneyc06aed02018-07-25 11:25:23 -07001119 * delay RCU grace periods: (1) At age jiffies_to_sched_qs,
1120 * set .rcu_urgent_qs, (2) At age 2*jiffies_to_sched_qs, set
Paul E. McKenney7e28c5a2018-07-11 08:09:28 -07001121 * both .rcu_need_heavy_qs and .rcu_urgent_qs. Note that the
1122 * unsynchronized assignments to the per-CPU rcu_need_heavy_qs
1123 * variable are safe because the assignments are repeated if this
1124 * CPU failed to pass through a quiescent state. This code
Paul E. McKenneyc06aed02018-07-25 11:25:23 -07001125 * also checks .jiffies_resched in case jiffies_to_sched_qs
Paul E. McKenney7e28c5a2018-07-11 08:09:28 -07001126 * is set way high.
Paul E. McKenney65d798f2013-04-12 16:19:10 -07001127 */
Paul E. McKenneyc06aed02018-07-25 11:25:23 -07001128 jtsq = READ_ONCE(jiffies_to_sched_qs);
Paul E. McKenney2dba13f2018-08-03 21:00:38 -07001129 ruqp = per_cpu_ptr(&rcu_data.rcu_urgent_qs, rdp->cpu);
1130 rnhqp = &per_cpu(rcu_data.rcu_need_heavy_qs, rdp->cpu);
Paul E. McKenney0f9be8c2017-01-27 13:17:02 -08001131 if (!READ_ONCE(*rnhqp) &&
Paul E. McKenney7e28c5a2018-07-11 08:09:28 -07001132 (time_after(jiffies, rcu_state.gp_start + jtsq * 2) ||
Paul E. McKenney88d1bea2018-07-04 14:45:00 -07001133 time_after(jiffies, rcu_state.jiffies_resched))) {
Paul E. McKenney0f9be8c2017-01-27 13:17:02 -08001134 WRITE_ONCE(*rnhqp, true);
Paul E. McKenney9226b102017-01-27 14:17:50 -08001135 /* Store rcu_need_heavy_qs before rcu_urgent_qs. */
1136 smp_store_release(ruqp, true);
Paul E. McKenney7e28c5a2018-07-11 08:09:28 -07001137 } else if (time_after(jiffies, rcu_state.gp_start + jtsq)) {
1138 WRITE_ONCE(*ruqp, true);
Paul E. McKenney6193c762013-09-23 13:57:18 -07001139 }
1140
Paul E. McKenney28053bc2016-12-01 11:31:31 -08001141 /*
Paul E. McKenneyc98cac62018-11-21 11:35:03 -08001142 * NO_HZ_FULL CPUs can run in-kernel without rcu_sched_clock_irq!
Paul E. McKenneyd3052102018-07-25 11:49:47 -07001143 * The above code handles this, but only for straight cond_resched().
1144 * And some in-kernel loops check need_resched() before calling
1145 * cond_resched(), which defeats the above code for CPUs that are
1146 * running in-kernel with scheduling-clock interrupts disabled.
1147 * So hit them over the head with the resched_cpu() hammer!
Paul E. McKenney28053bc2016-12-01 11:31:31 -08001148 */
Paul E. McKenneyd3052102018-07-25 11:49:47 -07001149 if (tick_nohz_full_cpu(rdp->cpu) &&
1150 time_after(jiffies,
1151 READ_ONCE(rdp->last_fqs_resched) + jtsq * 3)) {
Paul E. McKenney28053bc2016-12-01 11:31:31 -08001152 resched_cpu(rdp->cpu);
Paul E. McKenneyd3052102018-07-25 11:49:47 -07001153 WRITE_ONCE(rdp->last_fqs_resched, jiffies);
1154 }
1155
1156 /*
1157 * If more than halfway to RCU CPU stall-warning time, invoke
1158 * resched_cpu() more frequently to try to loosen things up a bit.
1159 * Also check to see if the CPU is getting hammered with interrupts,
1160 * but only once per grace period, just to keep the IPIs down to
1161 * a dull roar.
Paul E. McKenney49149502015-12-11 13:48:43 -08001162 */
Paul E. McKenney7e28c5a2018-07-11 08:09:28 -07001163 if (time_after(jiffies, rcu_state.jiffies_resched)) {
Paul E. McKenneyd3052102018-07-25 11:49:47 -07001164 if (time_after(jiffies,
1165 READ_ONCE(rdp->last_fqs_resched) + jtsq)) {
1166 resched_cpu(rdp->cpu);
1167 WRITE_ONCE(rdp->last_fqs_resched, jiffies);
1168 }
Paul E. McKenney9b9500d2017-08-17 17:05:59 -07001169 if (IS_ENABLED(CONFIG_IRQ_WORK) &&
Paul E. McKenney8aa670c2018-04-28 14:15:40 -07001170 !rdp->rcu_iw_pending && rdp->rcu_iw_gp_seq != rnp->gp_seq &&
Paul E. McKenney9b9500d2017-08-17 17:05:59 -07001171 (rnp->ffmask & rdp->grpmask)) {
1172 init_irq_work(&rdp->rcu_iw, rcu_iw_handler);
1173 rdp->rcu_iw_pending = true;
Paul E. McKenney8aa670c2018-04-28 14:15:40 -07001174 rdp->rcu_iw_gp_seq = rnp->gp_seq;
Paul E. McKenney9b9500d2017-08-17 17:05:59 -07001175 irq_work_queue_on(&rdp->rcu_iw, rdp->cpu);
1176 }
1177 }
Paul E. McKenney49149502015-12-11 13:48:43 -08001178
Paul E. McKenneya82dcc72012-08-01 14:29:20 -07001179 return 0;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001180}
1181
Paul E. McKenneyad3832e2018-07-03 17:22:34 -07001182static void record_gp_stall_check_time(void)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001183{
Paul E. McKenneycb1e78c2013-12-04 18:42:03 -08001184 unsigned long j = jiffies;
Paul E. McKenney6193c762013-09-23 13:57:18 -07001185 unsigned long j1;
Paul E. McKenney26cdfed2013-09-04 10:51:13 -07001186
Paul E. McKenneyad3832e2018-07-03 17:22:34 -07001187 rcu_state.gp_start = j;
Paul E. McKenney6193c762013-09-23 13:57:18 -07001188 j1 = rcu_jiffies_till_stall_check();
Paul E. McKenney91f63ce2018-05-01 15:05:45 -07001189 /* Record ->gp_start before ->jiffies_stall. */
Paul E. McKenneyad3832e2018-07-03 17:22:34 -07001190 smp_store_release(&rcu_state.jiffies_stall, j + j1); /* ^^^ */
1191 rcu_state.jiffies_resched = j + j1 / 2;
1192 rcu_state.n_force_qs_gpstart = READ_ONCE(rcu_state.n_force_qs);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001193}
1194
Paul E. McKenneyb637a322012-09-19 16:58:38 -07001195/*
Paul E. McKenneyfb81a442014-12-17 08:35:02 -08001196 * Complain about starvation of grace-period kthread.
1197 */
Paul E. McKenney8fd119b2018-07-03 17:22:34 -07001198static void rcu_check_gp_kthread_starvation(void)
Paul E. McKenneyfb81a442014-12-17 08:35:02 -08001199{
Paul E. McKenney7cba4772018-07-04 18:25:59 -07001200 struct task_struct *gpk = rcu_state.gp_kthread;
Paul E. McKenneyfb81a442014-12-17 08:35:02 -08001201 unsigned long j;
1202
Paul E. McKenney7cba4772018-07-04 18:25:59 -07001203 j = jiffies - READ_ONCE(rcu_state.gp_activity);
1204 if (j > 2 * HZ) {
Paul E. McKenney78c5a672018-04-27 13:32:28 -07001205 pr_err("%s kthread starved for %ld jiffies! g%ld f%#x %s(%d) ->state=%#lx ->cpu=%d\n",
Paul E. McKenney7cba4772018-07-04 18:25:59 -07001206 rcu_state.name, j,
1207 (long)rcu_seq_current(&rcu_state.gp_seq),
Paul E. McKenney3b6505f2018-12-12 07:20:07 -08001208 READ_ONCE(rcu_state.gp_flags),
Paul E. McKenney7cba4772018-07-04 18:25:59 -07001209 gp_state_getname(rcu_state.gp_state), rcu_state.gp_state,
1210 gpk ? gpk->state : ~0, gpk ? task_cpu(gpk) : -1);
1211 if (gpk) {
Paul E. McKenneyd07aee22018-01-11 12:08:20 -08001212 pr_err("RCU grace-period kthread stack dump:\n");
Paul E. McKenney7cba4772018-07-04 18:25:59 -07001213 sched_show_task(gpk);
1214 wake_up_process(gpk);
Paul E. McKenney86057b82015-12-31 08:48:36 -08001215 }
Paul E. McKenneyb1adb3e2015-10-01 10:38:16 -07001216 }
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001217}
1218
Paul E. McKenneyb637a322012-09-19 16:58:38 -07001219/*
Paul E. McKenney7aa92232016-11-29 05:49:06 -08001220 * Dump stacks of all tasks running on stalled CPUs. First try using
1221 * NMIs, but fall back to manual remote stack tracing on architectures
1222 * that don't support NMI-based stack dumps. The NMI-triggered stack
1223 * traces are more accurate because they are printed by the target CPU.
Paul E. McKenneyb637a322012-09-19 16:58:38 -07001224 */
Paul E. McKenney33dbdbf2018-07-03 17:22:34 -07001225static void rcu_dump_cpu_stacks(void)
Paul E. McKenneyb637a322012-09-19 16:58:38 -07001226{
1227 int cpu;
1228 unsigned long flags;
1229 struct rcu_node *rnp;
1230
Paul E. McKenneyaedf4ba2018-07-04 14:33:59 -07001231 rcu_for_each_leaf_node(rnp) {
Paul E. McKenney6cf10082015-10-08 15:36:54 -07001232 raw_spin_lock_irqsave_rcu_node(rnp, flags);
Paul E. McKenney7aa92232016-11-29 05:49:06 -08001233 for_each_leaf_node_possible_cpu(rnp, cpu)
1234 if (rnp->qsmask & leaf_node_cpu_bit(rnp, cpu))
1235 if (!trigger_single_cpu_backtrace(cpu))
Mark Rutlandbc75e992016-06-03 15:20:04 +01001236 dump_cpu_task(cpu);
Boqun Feng67c583a72015-12-29 12:18:47 +08001237 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
Paul E. McKenneyb637a322012-09-19 16:58:38 -07001238 }
1239}
1240
Paul E. McKenney8c7c4822016-01-03 20:29:57 -08001241/*
1242 * If too much time has passed in the current grace period, and if
1243 * so configured, go kick the relevant kthreads.
1244 */
Paul E. McKenneye1741c62018-07-03 17:22:34 -07001245static void rcu_stall_kick_kthreads(void)
Paul E. McKenney8c7c4822016-01-03 20:29:57 -08001246{
1247 unsigned long j;
1248
1249 if (!rcu_kick_kthreads)
1250 return;
Paul E. McKenney4c6ed432018-07-05 00:02:29 -07001251 j = READ_ONCE(rcu_state.jiffies_kick_kthreads);
1252 if (time_after(jiffies, j) && rcu_state.gp_kthread &&
1253 (rcu_gp_in_progress() || READ_ONCE(rcu_state.gp_flags))) {
1254 WARN_ONCE(1, "Kicking %s grace-period kthread\n",
1255 rcu_state.name);
Paul E. McKenney5dffed12016-02-17 11:54:28 -08001256 rcu_ftrace_dump(DUMP_ALL);
Paul E. McKenney4c6ed432018-07-05 00:02:29 -07001257 wake_up_process(rcu_state.gp_kthread);
1258 WRITE_ONCE(rcu_state.jiffies_kick_kthreads, j + HZ);
Paul E. McKenney8c7c4822016-01-03 20:29:57 -08001259 }
1260}
1261
Paul E. McKenney95394e62018-05-17 11:33:17 -07001262static void panic_on_rcu_stall(void)
Daniel Bristot de Oliveira088e9d22016-06-02 13:51:41 -03001263{
1264 if (sysctl_panic_on_rcu_stall)
1265 panic("RCU Stall\n");
1266}
1267
Paul E. McKenneya91e7e52018-07-03 17:22:34 -07001268static void print_other_cpu_stall(unsigned long gp_seq)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001269{
1270 int cpu;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001271 unsigned long flags;
Paul E. McKenney6ccd2ec2014-12-11 10:20:59 -08001272 unsigned long gpa;
1273 unsigned long j;
Paul E. McKenney285fe292012-05-09 08:45:12 -07001274 int ndetected = 0;
Paul E. McKenney336a4f62018-07-03 17:22:34 -07001275 struct rcu_node *rnp = rcu_get_root();
Paul E. McKenney53bb8572012-09-21 16:35:25 -07001276 long totqlen = 0;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001277
Paul E. McKenney8c7c4822016-01-03 20:29:57 -08001278 /* Kick and suppress, if so configured. */
Paul E. McKenneye1741c62018-07-03 17:22:34 -07001279 rcu_stall_kick_kthreads();
Paul E. McKenney8c7c4822016-01-03 20:29:57 -08001280 if (rcu_cpu_stall_suppress)
1281 return;
1282
Paul E. McKenney8cdd32a2010-08-09 14:23:03 -07001283 /*
1284 * OK, time to rat on our buddy...
1285 * See Documentation/RCU/stallwarn.txt for info on how to debug
1286 * RCU CPU stall warnings.
1287 */
Paul E. McKenney4c6ed432018-07-05 00:02:29 -07001288 pr_err("INFO: %s detected stalls on CPUs/tasks:", rcu_state.name);
Paul E. McKenneya858af22012-01-16 13:29:10 -08001289 print_cpu_stall_info_begin();
Paul E. McKenneyaedf4ba2018-07-04 14:33:59 -07001290 rcu_for_each_leaf_node(rnp) {
Paul E. McKenney6cf10082015-10-08 15:36:54 -07001291 raw_spin_lock_irqsave_rcu_node(rnp, flags);
Paul E. McKenney9bc8b552011-08-13 13:31:47 -07001292 ndetected += rcu_print_task_stall(rnp);
Paul E. McKenneyc8020a62012-08-10 16:55:59 -07001293 if (rnp->qsmask != 0) {
Mark Rutlandbc75e992016-06-03 15:20:04 +01001294 for_each_leaf_node_possible_cpu(rnp, cpu)
1295 if (rnp->qsmask & leaf_node_cpu_bit(rnp, cpu)) {
Paul E. McKenneyb21ebed2018-07-03 17:22:34 -07001296 print_cpu_stall_info(cpu);
Paul E. McKenneyc8020a62012-08-10 16:55:59 -07001297 ndetected++;
1298 }
1299 }
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. McKenneya858af22012-01-16 13:29:10 -08001302
Paul E. McKenneya858af22012-01-16 13:29:10 -08001303 print_cpu_stall_info_end();
Paul E. McKenney53bb8572012-09-21 16:35:25 -07001304 for_each_possible_cpu(cpu)
Paul E. McKenney903ee832018-10-02 16:05:46 -07001305 totqlen += rcu_get_n_cbs_cpu(cpu);
Paul E. McKenney471f87c32018-04-30 13:09:17 -07001306 pr_cont("(detected by %d, t=%ld jiffies, g=%ld, q=%lu)\n",
Paul E. McKenney4c6ed432018-07-05 00:02:29 -07001307 smp_processor_id(), (long)(jiffies - rcu_state.gp_start),
1308 (long)rcu_seq_current(&rcu_state.gp_seq), totqlen);
Paul E. McKenney6ccd2ec2014-12-11 10:20:59 -08001309 if (ndetected) {
Paul E. McKenney33dbdbf2018-07-03 17:22:34 -07001310 rcu_dump_cpu_stacks();
Byungchul Parkc4402b22016-11-09 17:57:13 +09001311
1312 /* Complain about tasks blocking the grace period. */
Paul E. McKenneya2887cd2018-07-03 17:22:34 -07001313 rcu_print_detail_task_stall();
Paul E. McKenney6ccd2ec2014-12-11 10:20:59 -08001314 } else {
Paul E. McKenney4c6ed432018-07-05 00:02:29 -07001315 if (rcu_seq_current(&rcu_state.gp_seq) != gp_seq) {
Paul E. McKenney6ccd2ec2014-12-11 10:20:59 -08001316 pr_err("INFO: Stall ended before state dump start\n");
1317 } else {
1318 j = jiffies;
Paul E. McKenney4c6ed432018-07-05 00:02:29 -07001319 gpa = READ_ONCE(rcu_state.gp_activity);
Paul E. McKenney237a0f22015-01-22 14:32:06 -08001320 pr_err("All QSes seen, last %s kthread activity %ld (%ld-%ld), jiffies_till_next_fqs=%ld, root ->qsmask %#lx\n",
Paul E. McKenney4c6ed432018-07-05 00:02:29 -07001321 rcu_state.name, j - gpa, j, gpa,
Paul E. McKenneyc06aed02018-07-25 11:25:23 -07001322 READ_ONCE(jiffies_till_next_fqs),
Paul E. McKenney336a4f62018-07-03 17:22:34 -07001323 rcu_get_root()->qsmask);
Paul E. McKenney6ccd2ec2014-12-11 10:20:59 -08001324 /* In this case, the current CPU might be at fault. */
1325 sched_show_task(current);
1326 }
1327 }
Paul E. McKenney8c42b1f2018-04-09 11:04:46 -07001328 /* Rewrite if needed in case of slow consoles. */
Paul E. McKenney4c6ed432018-07-05 00:02:29 -07001329 if (ULONG_CMP_GE(jiffies, READ_ONCE(rcu_state.jiffies_stall)))
1330 WRITE_ONCE(rcu_state.jiffies_stall,
Paul E. McKenney8c42b1f2018-04-09 11:04:46 -07001331 jiffies + 3 * rcu_jiffies_till_stall_check() + 3);
Ingo Molnarc1dc0b92009-08-02 11:28:21 +02001332
Paul E. McKenney8fd119b2018-07-03 17:22:34 -07001333 rcu_check_gp_kthread_starvation();
Paul E. McKenneyfb81a442014-12-17 08:35:02 -08001334
Daniel Bristot de Oliveira088e9d22016-06-02 13:51:41 -03001335 panic_on_rcu_stall();
1336
Paul E. McKenneycd920e52018-11-28 16:57:54 -08001337 rcu_force_quiescent_state(); /* Kick them all. */
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001338}
1339
Paul E. McKenney4e8b8e02018-07-03 17:22:34 -07001340static void print_cpu_stall(void)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001341{
Paul E. McKenney53bb8572012-09-21 16:35:25 -07001342 int cpu;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001343 unsigned long flags;
Paul E. McKenneyda1df502018-07-03 15:37:16 -07001344 struct rcu_data *rdp = this_cpu_ptr(&rcu_data);
Paul E. McKenney336a4f62018-07-03 17:22:34 -07001345 struct rcu_node *rnp = rcu_get_root();
Paul E. McKenney53bb8572012-09-21 16:35:25 -07001346 long totqlen = 0;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001347
Paul E. McKenney8c7c4822016-01-03 20:29:57 -08001348 /* Kick and suppress, if so configured. */
Paul E. McKenneye1741c62018-07-03 17:22:34 -07001349 rcu_stall_kick_kthreads();
Paul E. McKenney8c7c4822016-01-03 20:29:57 -08001350 if (rcu_cpu_stall_suppress)
1351 return;
1352
Paul E. McKenney8cdd32a2010-08-09 14:23:03 -07001353 /*
1354 * OK, time to rat on ourselves...
1355 * See Documentation/RCU/stallwarn.txt for info on how to debug
1356 * RCU CPU stall warnings.
1357 */
Paul E. McKenney4c6ed432018-07-05 00:02:29 -07001358 pr_err("INFO: %s self-detected stall on CPU", rcu_state.name);
Paul E. McKenneya858af22012-01-16 13:29:10 -08001359 print_cpu_stall_info_begin();
Paul E. McKenney9b9500d2017-08-17 17:05:59 -07001360 raw_spin_lock_irqsave_rcu_node(rdp->mynode, flags);
Paul E. McKenneyb21ebed2018-07-03 17:22:34 -07001361 print_cpu_stall_info(smp_processor_id());
Paul E. McKenney9b9500d2017-08-17 17:05:59 -07001362 raw_spin_unlock_irqrestore_rcu_node(rdp->mynode, flags);
Paul E. McKenneya858af22012-01-16 13:29:10 -08001363 print_cpu_stall_info_end();
Paul E. McKenney53bb8572012-09-21 16:35:25 -07001364 for_each_possible_cpu(cpu)
Paul E. McKenney903ee832018-10-02 16:05:46 -07001365 totqlen += rcu_get_n_cbs_cpu(cpu);
Paul E. McKenney471f87c32018-04-30 13:09:17 -07001366 pr_cont(" (t=%lu jiffies g=%ld q=%lu)\n",
Paul E. McKenney4c6ed432018-07-05 00:02:29 -07001367 jiffies - rcu_state.gp_start,
1368 (long)rcu_seq_current(&rcu_state.gp_seq), totqlen);
Paul E. McKenneyfb81a442014-12-17 08:35:02 -08001369
Paul E. McKenney8fd119b2018-07-03 17:22:34 -07001370 rcu_check_gp_kthread_starvation();
Paul E. McKenneyfb81a442014-12-17 08:35:02 -08001371
Paul E. McKenney33dbdbf2018-07-03 17:22:34 -07001372 rcu_dump_cpu_stacks();
Ingo Molnarc1dc0b92009-08-02 11:28:21 +02001373
Paul E. McKenney6cf10082015-10-08 15:36:54 -07001374 raw_spin_lock_irqsave_rcu_node(rnp, flags);
Paul E. McKenney8c42b1f2018-04-09 11:04:46 -07001375 /* Rewrite if needed in case of slow consoles. */
Paul E. McKenney4c6ed432018-07-05 00:02:29 -07001376 if (ULONG_CMP_GE(jiffies, READ_ONCE(rcu_state.jiffies_stall)))
1377 WRITE_ONCE(rcu_state.jiffies_stall,
Paul E. McKenney7d0ae802015-03-03 14:57:58 -08001378 jiffies + 3 * rcu_jiffies_till_stall_check() + 3);
Boqun Feng67c583a72015-12-29 12:18:47 +08001379 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
Ingo Molnarc1dc0b92009-08-02 11:28:21 +02001380
Daniel Bristot de Oliveira088e9d22016-06-02 13:51:41 -03001381 panic_on_rcu_stall();
1382
Peter Zijlstrab021fe32013-09-17 09:30:55 +02001383 /*
1384 * Attempt to revive the RCU machinery by forcing a context switch.
1385 *
1386 * A context switch would normally allow the RCU state machine to make
1387 * progress and it could be we're stuck in kernel space without context
1388 * switches for an entirely unreasonable amount of time.
1389 */
Paul E. McKenneyfced9c82018-07-26 13:44:00 -07001390 set_tsk_need_resched(current);
1391 set_preempt_need_resched();
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001392}
1393
Paul E. McKenneyea12ff22018-07-03 17:22:34 -07001394static void check_cpu_stall(struct rcu_data *rdp)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001395{
Paul E. McKenney471f87c32018-04-30 13:09:17 -07001396 unsigned long gs1;
1397 unsigned long gs2;
Paul E. McKenney26cdfed2013-09-04 10:51:13 -07001398 unsigned long gps;
Paul E. McKenneybad6e132011-05-02 23:40:04 -07001399 unsigned long j;
Paul E. McKenney8c42b1f2018-04-09 11:04:46 -07001400 unsigned long jn;
Paul E. McKenneybad6e132011-05-02 23:40:04 -07001401 unsigned long js;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001402 struct rcu_node *rnp;
1403
Paul E. McKenney8c7c4822016-01-03 20:29:57 -08001404 if ((rcu_cpu_stall_suppress && !rcu_kick_kthreads) ||
Paul E. McKenneyde8e8732018-07-03 17:22:34 -07001405 !rcu_gp_in_progress())
Paul E. McKenneyc68de202010-04-15 10:12:40 -07001406 return;
Paul E. McKenneye1741c62018-07-03 17:22:34 -07001407 rcu_stall_kick_kthreads();
Paul E. McKenneycb1e78c2013-12-04 18:42:03 -08001408 j = jiffies;
Paul E. McKenney26cdfed2013-09-04 10:51:13 -07001409
1410 /*
1411 * Lots of memory barriers to reject false positives.
1412 *
Paul E. McKenney4c6ed432018-07-05 00:02:29 -07001413 * The idea is to pick up rcu_state.gp_seq, then
1414 * rcu_state.jiffies_stall, then rcu_state.gp_start, and finally
1415 * another copy of rcu_state.gp_seq. These values are updated in
1416 * the opposite order with memory barriers (or equivalent) during
1417 * grace-period initialization and cleanup. Now, a false positive
1418 * can occur if we get an new value of rcu_state.gp_start and a old
1419 * value of rcu_state.jiffies_stall. But given the memory barriers,
1420 * the only way that this can happen is if one grace period ends
1421 * and another starts between these two fetches. This is detected
1422 * by comparing the second fetch of rcu_state.gp_seq with the
1423 * previous fetch from rcu_state.gp_seq.
Paul E. McKenney26cdfed2013-09-04 10:51:13 -07001424 *
Paul E. McKenney4c6ed432018-07-05 00:02:29 -07001425 * Given this check, comparisons of jiffies, rcu_state.jiffies_stall,
1426 * and rcu_state.gp_start suffice to forestall false positives.
Paul E. McKenney26cdfed2013-09-04 10:51:13 -07001427 */
Paul E. McKenney4c6ed432018-07-05 00:02:29 -07001428 gs1 = READ_ONCE(rcu_state.gp_seq);
Paul E. McKenney471f87c32018-04-30 13:09:17 -07001429 smp_rmb(); /* Pick up ->gp_seq first... */
Paul E. McKenney4c6ed432018-07-05 00:02:29 -07001430 js = READ_ONCE(rcu_state.jiffies_stall);
Paul E. McKenney26cdfed2013-09-04 10:51:13 -07001431 smp_rmb(); /* ...then ->jiffies_stall before the rest... */
Paul E. McKenney4c6ed432018-07-05 00:02:29 -07001432 gps = READ_ONCE(rcu_state.gp_start);
Paul E. McKenney471f87c32018-04-30 13:09:17 -07001433 smp_rmb(); /* ...and finally ->gp_start before ->gp_seq again. */
Paul E. McKenney4c6ed432018-07-05 00:02:29 -07001434 gs2 = READ_ONCE(rcu_state.gp_seq);
Paul E. McKenney471f87c32018-04-30 13:09:17 -07001435 if (gs1 != gs2 ||
Paul E. McKenney26cdfed2013-09-04 10:51:13 -07001436 ULONG_CMP_LT(j, js) ||
1437 ULONG_CMP_GE(gps, js))
1438 return; /* No stall or GP completed since entering function. */
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001439 rnp = rdp->mynode;
Paul E. McKenney8c42b1f2018-04-09 11:04:46 -07001440 jn = jiffies + 3 * rcu_jiffies_till_stall_check() + 3;
Paul E. McKenneyde8e8732018-07-03 17:22:34 -07001441 if (rcu_gp_in_progress() &&
Paul E. McKenney8c42b1f2018-04-09 11:04:46 -07001442 (READ_ONCE(rnp->qsmask) & rdp->grpmask) &&
Paul E. McKenney4c6ed432018-07-05 00:02:29 -07001443 cmpxchg(&rcu_state.jiffies_stall, js, jn) == js) {
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001444
1445 /* We haven't checked in, so go dump stack. */
Paul E. McKenney4e8b8e02018-07-03 17:22:34 -07001446 print_cpu_stall();
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001447
Paul E. McKenneyde8e8732018-07-03 17:22:34 -07001448 } else if (rcu_gp_in_progress() &&
Paul E. McKenney8c42b1f2018-04-09 11:04:46 -07001449 ULONG_CMP_GE(j, js + RCU_STALL_RAT_DELAY) &&
Paul E. McKenney4c6ed432018-07-05 00:02:29 -07001450 cmpxchg(&rcu_state.jiffies_stall, js, jn) == js) {
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001451
Paul E. McKenneybad6e132011-05-02 23:40:04 -07001452 /* They had a few time units to dump stack, so complain. */
Paul E. McKenneya91e7e52018-07-03 17:22:34 -07001453 print_other_cpu_stall(gs2);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001454 }
1455}
1456
Paul E. McKenney53d84e02010-08-10 14:28:53 -07001457/**
1458 * rcu_cpu_stall_reset - prevent further stall warnings in current grace period
1459 *
1460 * Set the stall-warning timeout way off into the future, thus preventing
1461 * any RCU CPU stall-warning messages from appearing in the current set of
1462 * RCU grace periods.
1463 *
1464 * The caller must disable hard irqs.
1465 */
1466void rcu_cpu_stall_reset(void)
1467{
Paul E. McKenneyb97d23c2018-07-04 15:35:00 -07001468 WRITE_ONCE(rcu_state.jiffies_stall, jiffies + ULONG_MAX / 2);
Paul E. McKenney53d84e02010-08-10 14:28:53 -07001469}
1470
Paul E. McKenney41e80592018-04-12 11:24:09 -07001471/* Trace-event wrapper function for trace_rcu_future_grace_period. */
1472static void trace_rcu_this_gp(struct rcu_node *rnp, struct rcu_data *rdp,
Joel Fernandesb73de912018-05-20 21:42:18 -07001473 unsigned long gp_seq_req, const char *s)
Paul E. McKenney0446be42012-12-30 15:21:01 -08001474{
Paul E. McKenney88d1bea2018-07-04 14:45:00 -07001475 trace_rcu_future_grace_period(rcu_state.name, rnp->gp_seq, gp_seq_req,
Paul E. McKenneyabd13fd2018-05-01 13:08:46 -07001476 rnp->level, rnp->grplo, rnp->grphi, s);
Paul E. McKenney0446be42012-12-30 15:21:01 -08001477}
1478
1479/*
Joel Fernandesb73de912018-05-20 21:42:18 -07001480 * rcu_start_this_gp - Request the start of a particular grace period
Joel Fernandes (Google)df2bf8f2018-05-22 23:38:14 -07001481 * @rnp_start: The leaf node of the CPU from which to start.
Joel Fernandesb73de912018-05-20 21:42:18 -07001482 * @rdp: The rcu_data corresponding to the CPU from which to start.
1483 * @gp_seq_req: The gp_seq of the grace period to start.
1484 *
Paul E. McKenney41e80592018-04-12 11:24:09 -07001485 * Start the specified grace period, as needed to handle newly arrived
Paul E. McKenney0446be42012-12-30 15:21:01 -08001486 * callbacks. The required future grace periods are recorded in each
Paul E. McKenney7a1d0f22018-05-01 10:26:57 -07001487 * rcu_node structure's ->gp_seq_needed field. Returns true if there
Paul E. McKenney48a76392014-03-11 13:02:16 -07001488 * is reason to awaken the grace-period kthread.
Paul E. McKenney0446be42012-12-30 15:21:01 -08001489 *
Paul E. McKenneyd5cd9682018-04-12 10:45:06 -07001490 * The caller must hold the specified rcu_node structure's ->lock, which
1491 * is why the caller is responsible for waking the grace-period kthread.
Joel Fernandesb73de912018-05-20 21:42:18 -07001492 *
1493 * Returns true if the GP thread needs to be awakened else false.
Paul E. McKenney0446be42012-12-30 15:21:01 -08001494 */
Joel Fernandes (Google)df2bf8f2018-05-22 23:38:14 -07001495static bool rcu_start_this_gp(struct rcu_node *rnp_start, struct rcu_data *rdp,
Joel Fernandesb73de912018-05-20 21:42:18 -07001496 unsigned long gp_seq_req)
Paul E. McKenney0446be42012-12-30 15:21:01 -08001497{
Paul E. McKenney48a76392014-03-11 13:02:16 -07001498 bool ret = false;
Joel Fernandes (Google)df2bf8f2018-05-22 23:38:14 -07001499 struct rcu_node *rnp;
Paul E. McKenney0446be42012-12-30 15:21:01 -08001500
Paul E. McKenney360e0da2018-04-12 11:50:41 -07001501 /*
1502 * Use funnel locking to either acquire the root rcu_node
1503 * structure's lock or bail out if the need for this grace period
Joel Fernandes (Google)df2bf8f2018-05-22 23:38:14 -07001504 * has already been recorded -- or if that grace period has in
1505 * fact already started. If there is already a grace period in
1506 * progress in a non-leaf node, no recording is needed because the
1507 * end of the grace period will scan the leaf rcu_node structures.
1508 * Note that rnp_start->lock must not be released.
Paul E. McKenney360e0da2018-04-12 11:50:41 -07001509 */
Joel Fernandes (Google)df2bf8f2018-05-22 23:38:14 -07001510 raw_lockdep_assert_held_rcu_node(rnp_start);
1511 trace_rcu_this_gp(rnp_start, rdp, gp_seq_req, TPS("Startleaf"));
1512 for (rnp = rnp_start; 1; rnp = rnp->parent) {
1513 if (rnp != rnp_start)
1514 raw_spin_lock_rcu_node(rnp);
1515 if (ULONG_CMP_GE(rnp->gp_seq_needed, gp_seq_req) ||
1516 rcu_seq_started(&rnp->gp_seq, gp_seq_req) ||
1517 (rnp != rnp_start &&
1518 rcu_seq_state(rcu_seq_current(&rnp->gp_seq)))) {
1519 trace_rcu_this_gp(rnp, rdp, gp_seq_req,
Joel Fernandesb73de912018-05-20 21:42:18 -07001520 TPS("Prestarted"));
Paul E. McKenney360e0da2018-04-12 11:50:41 -07001521 goto unlock_out;
1522 }
Joel Fernandes (Google)df2bf8f2018-05-22 23:38:14 -07001523 rnp->gp_seq_needed = gp_seq_req;
Joel Fernandes (Google)226ca5e2018-05-22 23:38:15 -07001524 if (rcu_seq_state(rcu_seq_current(&rnp->gp_seq))) {
Paul E. McKenneya2165e42018-05-12 07:42:20 -07001525 /*
Joel Fernandes (Google)226ca5e2018-05-22 23:38:15 -07001526 * We just marked the leaf or internal node, and a
1527 * grace period is in progress, which means that
1528 * rcu_gp_cleanup() will see the marking. Bail to
1529 * reduce contention.
Paul E. McKenneya2165e42018-05-12 07:42:20 -07001530 */
Joel Fernandes (Google)df2bf8f2018-05-22 23:38:14 -07001531 trace_rcu_this_gp(rnp_start, rdp, gp_seq_req,
Joel Fernandesb73de912018-05-20 21:42:18 -07001532 TPS("Startedleaf"));
Paul E. McKenneya2165e42018-05-12 07:42:20 -07001533 goto unlock_out;
1534 }
Joel Fernandes (Google)df2bf8f2018-05-22 23:38:14 -07001535 if (rnp != rnp_start && rnp->parent != NULL)
1536 raw_spin_unlock_rcu_node(rnp);
1537 if (!rnp->parent)
Paul E. McKenney360e0da2018-04-12 11:50:41 -07001538 break; /* At root, and perhaps also leaf. */
Paul E. McKenney0446be42012-12-30 15:21:01 -08001539 }
1540
Paul E. McKenney360e0da2018-04-12 11:50:41 -07001541 /* If GP already in progress, just leave, otherwise start one. */
Paul E. McKenneyde8e8732018-07-03 17:22:34 -07001542 if (rcu_gp_in_progress()) {
Joel Fernandes (Google)df2bf8f2018-05-22 23:38:14 -07001543 trace_rcu_this_gp(rnp, rdp, gp_seq_req, TPS("Startedleafroot"));
Paul E. McKenney0446be42012-12-30 15:21:01 -08001544 goto unlock_out;
1545 }
Joel Fernandes (Google)df2bf8f2018-05-22 23:38:14 -07001546 trace_rcu_this_gp(rnp, rdp, gp_seq_req, TPS("Startedroot"));
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001547 WRITE_ONCE(rcu_state.gp_flags, rcu_state.gp_flags | RCU_GP_FLAG_INIT);
1548 rcu_state.gp_req_activity = jiffies;
1549 if (!rcu_state.gp_kthread) {
Joel Fernandes (Google)df2bf8f2018-05-22 23:38:14 -07001550 trace_rcu_this_gp(rnp, rdp, gp_seq_req, TPS("NoGPkthread"));
Paul E. McKenney360e0da2018-04-12 11:50:41 -07001551 goto unlock_out;
Paul E. McKenney0446be42012-12-30 15:21:01 -08001552 }
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001553 trace_rcu_grace_period(rcu_state.name, READ_ONCE(rcu_state.gp_seq), TPS("newreq"));
Paul E. McKenney360e0da2018-04-12 11:50:41 -07001554 ret = true; /* Caller must wake GP kthread. */
Paul E. McKenney0446be42012-12-30 15:21:01 -08001555unlock_out:
Paul E. McKenneyab5e8692018-05-01 11:07:23 -07001556 /* Push furthest requested GP to leaf node and rcu_data structure. */
Joel Fernandes (Google)df2bf8f2018-05-22 23:38:14 -07001557 if (ULONG_CMP_LT(gp_seq_req, rnp->gp_seq_needed)) {
1558 rnp_start->gp_seq_needed = rnp->gp_seq_needed;
1559 rdp->gp_seq_needed = rnp->gp_seq_needed;
Paul E. McKenneyab5e8692018-05-01 11:07:23 -07001560 }
Joel Fernandes (Google)df2bf8f2018-05-22 23:38:14 -07001561 if (rnp != rnp_start)
1562 raw_spin_unlock_rcu_node(rnp);
Paul E. McKenney48a76392014-03-11 13:02:16 -07001563 return ret;
Paul E. McKenney0446be42012-12-30 15:21:01 -08001564}
1565
1566/*
1567 * Clean up any old requests for the just-ended grace period. Also return
Paul E. McKenneyd1e4f012017-02-08 14:58:41 -08001568 * whether any additional grace periods have been requested.
Paul E. McKenney0446be42012-12-30 15:21:01 -08001569 */
Paul E. McKenney3481f2e2018-07-03 17:22:34 -07001570static bool rcu_future_gp_cleanup(struct rcu_node *rnp)
Paul E. McKenney0446be42012-12-30 15:21:01 -08001571{
Paul E. McKenneyfb313402018-04-12 07:20:30 -07001572 bool needmore;
Paul E. McKenneyda1df502018-07-03 15:37:16 -07001573 struct rcu_data *rdp = this_cpu_ptr(&rcu_data);
Paul E. McKenney0446be42012-12-30 15:21:01 -08001574
Paul E. McKenney7a1d0f22018-05-01 10:26:57 -07001575 needmore = ULONG_CMP_LT(rnp->gp_seq, rnp->gp_seq_needed);
1576 if (!needmore)
1577 rnp->gp_seq_needed = rnp->gp_seq; /* Avoid counter wrap. */
Joel Fernandesb73de912018-05-20 21:42:18 -07001578 trace_rcu_this_gp(rnp, rdp, rnp->gp_seq,
Paul E. McKenney41e80592018-04-12 11:24:09 -07001579 needmore ? TPS("CleanupMore") : TPS("Cleanup"));
Paul E. McKenney0446be42012-12-30 15:21:01 -08001580 return needmore;
1581}
1582
1583/*
Zhang, Jun1d1f8982018-12-18 06:55:01 -08001584 * Awaken the grace-period kthread. Don't do a self-awaken (unless in
1585 * an interrupt or softirq handler), and don't bother awakening when there
1586 * is nothing for the grace-period kthread to do (as in several CPUs raced
1587 * to awaken, and we lost), and finally don't try to awaken a kthread that
1588 * has not yet been created. If all those checks are passed, track some
1589 * debug information and awaken.
1590 *
1591 * So why do the self-wakeup when in an interrupt or softirq handler
1592 * in the grace-period kthread's context? Because the kthread might have
1593 * been interrupted just as it was going to sleep, and just after the final
1594 * pre-sleep check of the awaken condition. In this case, a wakeup really
1595 * is required, and is therefore supplied.
Paul E. McKenney48a76392014-03-11 13:02:16 -07001596 */
Paul E. McKenney532c00c2018-07-03 17:22:34 -07001597static void rcu_gp_kthread_wake(void)
Paul E. McKenney48a76392014-03-11 13:02:16 -07001598{
Zhang, Jun1d1f8982018-12-18 06:55:01 -08001599 if ((current == rcu_state.gp_kthread &&
1600 !in_interrupt() && !in_serving_softirq()) ||
Paul E. McKenney532c00c2018-07-03 17:22:34 -07001601 !READ_ONCE(rcu_state.gp_flags) ||
1602 !rcu_state.gp_kthread)
Paul E. McKenney48a76392014-03-11 13:02:16 -07001603 return;
Paul E. McKenneyfd897572018-12-10 16:09:49 -08001604 WRITE_ONCE(rcu_state.gp_wake_time, jiffies);
1605 WRITE_ONCE(rcu_state.gp_wake_seq, READ_ONCE(rcu_state.gp_seq));
Paul E. McKenney532c00c2018-07-03 17:22:34 -07001606 swake_up_one(&rcu_state.gp_wq);
Paul E. McKenney48a76392014-03-11 13:02:16 -07001607}
1608
1609/*
Paul E. McKenney29365e52018-04-30 10:57:36 -07001610 * If there is room, assign a ->gp_seq number to any callbacks on this
1611 * CPU that have not already been assigned. Also accelerate any callbacks
1612 * that were previously assigned a ->gp_seq number that has since proven
1613 * to be too conservative, which can happen if callbacks get assigned a
1614 * ->gp_seq number while RCU is idle, but with reference to a non-root
1615 * rcu_node structure. This function is idempotent, so it does not hurt
1616 * to call it repeatedly. Returns an flag saying that we should awaken
1617 * the RCU grace-period kthread.
Paul E. McKenneydc35c892012-12-03 13:52:00 -08001618 *
1619 * The caller must hold rnp->lock with interrupts disabled.
1620 */
Paul E. McKenney02f50142018-07-03 17:22:34 -07001621static bool rcu_accelerate_cbs(struct rcu_node *rnp, struct rcu_data *rdp)
Paul E. McKenneydc35c892012-12-03 13:52:00 -08001622{
Joel Fernandesb73de912018-05-20 21:42:18 -07001623 unsigned long gp_seq_req;
Paul E. McKenney15fecf82017-02-08 12:36:42 -08001624 bool ret = false;
Paul E. McKenneydc35c892012-12-03 13:52:00 -08001625
Matthew Wilcoxa32e01e2018-01-17 06:24:30 -08001626 raw_lockdep_assert_held_rcu_node(rnp);
Paul E. McKenneyc0b334c2017-04-28 12:32:15 -07001627
Paul E. McKenney15fecf82017-02-08 12:36:42 -08001628 /* If no pending (not yet ready to invoke) callbacks, nothing to do. */
1629 if (!rcu_segcblist_pend_cbs(&rdp->cblist))
Paul E. McKenney48a76392014-03-11 13:02:16 -07001630 return false;
Paul E. McKenneydc35c892012-12-03 13:52:00 -08001631
1632 /*
Paul E. McKenney15fecf82017-02-08 12:36:42 -08001633 * Callbacks are often registered with incomplete grace-period
1634 * information. Something about the fact that getting exact
1635 * information requires acquiring a global lock... RCU therefore
1636 * makes a conservative estimate of the grace period number at which
1637 * a given callback will become ready to invoke. The following
1638 * code checks this estimate and improves it when possible, thus
1639 * accelerating callback invocation to an earlier grace-period
1640 * number.
Paul E. McKenneydc35c892012-12-03 13:52:00 -08001641 */
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001642 gp_seq_req = rcu_seq_snap(&rcu_state.gp_seq);
Joel Fernandesb73de912018-05-20 21:42:18 -07001643 if (rcu_segcblist_accelerate(&rdp->cblist, gp_seq_req))
1644 ret = rcu_start_this_gp(rnp, rdp, gp_seq_req);
Paul E. McKenney6d4b4182012-11-27 16:55:44 -08001645
1646 /* Trace depending on how much we were able to accelerate. */
Paul E. McKenney15fecf82017-02-08 12:36:42 -08001647 if (rcu_segcblist_restempty(&rdp->cblist, RCU_WAIT_TAIL))
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001648 trace_rcu_grace_period(rcu_state.name, rdp->gp_seq, TPS("AccWaitCB"));
Paul E. McKenney6d4b4182012-11-27 16:55:44 -08001649 else
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001650 trace_rcu_grace_period(rcu_state.name, rdp->gp_seq, TPS("AccReadyCB"));
Paul E. McKenney48a76392014-03-11 13:02:16 -07001651 return ret;
Paul E. McKenneydc35c892012-12-03 13:52:00 -08001652}
1653
1654/*
Paul E. McKenneye44e73c2018-05-01 16:29:47 -07001655 * Similar to rcu_accelerate_cbs(), but does not require that the leaf
1656 * rcu_node structure's ->lock be held. It consults the cached value
1657 * of ->gp_seq_needed in the rcu_data structure, and if that indicates
1658 * that a new grace-period request be made, invokes rcu_accelerate_cbs()
1659 * while holding the leaf rcu_node structure's ->lock.
1660 */
Paul E. McKenneyc6e09b92018-07-03 17:22:34 -07001661static void rcu_accelerate_cbs_unlocked(struct rcu_node *rnp,
Paul E. McKenneye44e73c2018-05-01 16:29:47 -07001662 struct rcu_data *rdp)
1663{
1664 unsigned long c;
1665 bool needwake;
1666
1667 lockdep_assert_irqs_disabled();
Paul E. McKenneyc6e09b92018-07-03 17:22:34 -07001668 c = rcu_seq_snap(&rcu_state.gp_seq);
Paul E. McKenneye44e73c2018-05-01 16:29:47 -07001669 if (!rdp->gpwrap && ULONG_CMP_GE(rdp->gp_seq_needed, c)) {
1670 /* Old request still live, so mark recent callbacks. */
1671 (void)rcu_segcblist_accelerate(&rdp->cblist, c);
1672 return;
1673 }
1674 raw_spin_lock_rcu_node(rnp); /* irqs already disabled. */
Paul E. McKenney02f50142018-07-03 17:22:34 -07001675 needwake = rcu_accelerate_cbs(rnp, rdp);
Paul E. McKenneye44e73c2018-05-01 16:29:47 -07001676 raw_spin_unlock_rcu_node(rnp); /* irqs remain disabled. */
1677 if (needwake)
Paul E. McKenney532c00c2018-07-03 17:22:34 -07001678 rcu_gp_kthread_wake();
Paul E. McKenneye44e73c2018-05-01 16:29:47 -07001679}
1680
1681/*
Paul E. McKenneydc35c892012-12-03 13:52:00 -08001682 * Move any callbacks whose grace period has completed to the
1683 * RCU_DONE_TAIL sublist, then compact the remaining sublists and
Paul E. McKenney29365e52018-04-30 10:57:36 -07001684 * assign ->gp_seq numbers to any callbacks in the RCU_NEXT_TAIL
Paul E. McKenneydc35c892012-12-03 13:52:00 -08001685 * sublist. This function is idempotent, so it does not hurt to
1686 * invoke it repeatedly. As long as it is not invoked -too- often...
Paul E. McKenney48a76392014-03-11 13:02:16 -07001687 * Returns true if the RCU grace-period kthread needs to be awakened.
Paul E. McKenneydc35c892012-12-03 13:52:00 -08001688 *
1689 * The caller must hold rnp->lock with interrupts disabled.
1690 */
Paul E. McKenney834f56b2018-07-03 17:22:34 -07001691static bool rcu_advance_cbs(struct rcu_node *rnp, struct rcu_data *rdp)
Paul E. McKenneydc35c892012-12-03 13:52:00 -08001692{
Matthew Wilcoxa32e01e2018-01-17 06:24:30 -08001693 raw_lockdep_assert_held_rcu_node(rnp);
Paul E. McKenneyc0b334c2017-04-28 12:32:15 -07001694
Paul E. McKenney15fecf82017-02-08 12:36:42 -08001695 /* If no pending (not yet ready to invoke) callbacks, nothing to do. */
1696 if (!rcu_segcblist_pend_cbs(&rdp->cblist))
Paul E. McKenney48a76392014-03-11 13:02:16 -07001697 return false;
Paul E. McKenneydc35c892012-12-03 13:52:00 -08001698
1699 /*
Paul E. McKenney29365e52018-04-30 10:57:36 -07001700 * Find all callbacks whose ->gp_seq numbers indicate that they
Paul E. McKenneydc35c892012-12-03 13:52:00 -08001701 * are ready to invoke, and put them into the RCU_DONE_TAIL sublist.
1702 */
Paul E. McKenney29365e52018-04-30 10:57:36 -07001703 rcu_segcblist_advance(&rdp->cblist, rnp->gp_seq);
Paul E. McKenneydc35c892012-12-03 13:52:00 -08001704
1705 /* Classify any remaining callbacks. */
Paul E. McKenney02f50142018-07-03 17:22:34 -07001706 return rcu_accelerate_cbs(rnp, rdp);
Paul E. McKenneydc35c892012-12-03 13:52:00 -08001707}
1708
1709/*
Paul E. McKenneyba9fbe92013-03-19 11:53:31 -07001710 * Update CPU-local rcu_data state to record the beginnings and ends of
1711 * grace periods. The caller must hold the ->lock of the leaf rcu_node
1712 * structure corresponding to the current CPU, and must have irqs disabled.
Paul E. McKenney48a76392014-03-11 13:02:16 -07001713 * Returns true if the grace-period kthread needs to be awakened.
Paul E. McKenneyd09b62d2009-11-02 13:52:28 -08001714 */
Paul E. McKenneyc7e48f72018-07-03 17:22:34 -07001715static bool __note_gp_changes(struct rcu_node *rnp, struct rcu_data *rdp)
Paul E. McKenneyd09b62d2009-11-02 13:52:28 -08001716{
Paul E. McKenney48a76392014-03-11 13:02:16 -07001717 bool ret;
Paul E. McKenney3563a432016-07-28 09:39:11 -07001718 bool need_gp;
Paul E. McKenney48a76392014-03-11 13:02:16 -07001719
Matthew Wilcoxa32e01e2018-01-17 06:24:30 -08001720 raw_lockdep_assert_held_rcu_node(rnp);
Paul E. McKenneyc0b334c2017-04-28 12:32:15 -07001721
Paul E. McKenney67e14c12018-04-27 16:01:46 -07001722 if (rdp->gp_seq == rnp->gp_seq)
1723 return false; /* Nothing to do. */
1724
Paul E. McKenneyba9fbe92013-03-19 11:53:31 -07001725 /* Handle the ends of any preceding grace periods first. */
Paul E. McKenney67e14c12018-04-27 16:01:46 -07001726 if (rcu_seq_completed_gp(rdp->gp_seq, rnp->gp_seq) ||
1727 unlikely(READ_ONCE(rdp->gpwrap))) {
Paul E. McKenney834f56b2018-07-03 17:22:34 -07001728 ret = rcu_advance_cbs(rnp, rdp); /* Advance callbacks. */
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001729 trace_rcu_grace_period(rcu_state.name, rdp->gp_seq, TPS("cpuend"));
Paul E. McKenney67e14c12018-04-27 16:01:46 -07001730 } else {
Paul E. McKenney02f50142018-07-03 17:22:34 -07001731 ret = rcu_accelerate_cbs(rnp, rdp); /* Recent callbacks. */
Paul E. McKenneyd09b62d2009-11-02 13:52:28 -08001732 }
Paul E. McKenney398ebe62013-03-19 10:53:14 -07001733
Paul E. McKenney67e14c12018-04-27 16:01:46 -07001734 /* Now handle the beginnings of any new-to-this-CPU grace periods. */
1735 if (rcu_seq_new_gp(rdp->gp_seq, rnp->gp_seq) ||
1736 unlikely(READ_ONCE(rdp->gpwrap))) {
Paul E. McKenney6eaef632013-03-19 10:08:37 -07001737 /*
1738 * If the current grace period is waiting for this CPU,
1739 * set up to detect a quiescent state, otherwise don't
1740 * go looking for one.
1741 */
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001742 trace_rcu_grace_period(rcu_state.name, rnp->gp_seq, TPS("cpustart"));
Paul E. McKenney3563a432016-07-28 09:39:11 -07001743 need_gp = !!(rnp->qsmask & rdp->grpmask);
1744 rdp->cpu_no_qs.b.norm = need_gp;
Paul E. McKenney3563a432016-07-28 09:39:11 -07001745 rdp->core_needs_qs = need_gp;
Paul E. McKenney6eaef632013-03-19 10:08:37 -07001746 zero_cpu_stall_ticks(rdp);
1747 }
Paul E. McKenney67e14c12018-04-27 16:01:46 -07001748 rdp->gp_seq = rnp->gp_seq; /* Remember new grace-period state. */
Zhang, Jun13dc7d02018-12-19 10:37:34 -08001749 if (ULONG_CMP_LT(rdp->gp_seq_needed, rnp->gp_seq_needed) || rdp->gpwrap)
Paul E. McKenney3d184692018-05-15 16:47:30 -07001750 rdp->gp_seq_needed = rnp->gp_seq_needed;
1751 WRITE_ONCE(rdp->gpwrap, false);
1752 rcu_gpnum_ovf(rnp, rdp);
Paul E. McKenney48a76392014-03-11 13:02:16 -07001753 return ret;
Paul E. McKenney6eaef632013-03-19 10:08:37 -07001754}
1755
Paul E. McKenney15cabdf2018-07-03 17:22:34 -07001756static void note_gp_changes(struct rcu_data *rdp)
Paul E. McKenney6eaef632013-03-19 10:08:37 -07001757{
1758 unsigned long flags;
Paul E. McKenney48a76392014-03-11 13:02:16 -07001759 bool needwake;
Paul E. McKenney6eaef632013-03-19 10:08:37 -07001760 struct rcu_node *rnp;
1761
1762 local_irq_save(flags);
1763 rnp = rdp->mynode;
Paul E. McKenney67e14c12018-04-27 16:01:46 -07001764 if ((rdp->gp_seq == rcu_seq_current(&rnp->gp_seq) &&
Paul E. McKenney7d0ae802015-03-03 14:57:58 -08001765 !unlikely(READ_ONCE(rdp->gpwrap))) || /* w/out lock. */
Peter Zijlstra2a67e742015-10-08 12:24:23 +02001766 !raw_spin_trylock_rcu_node(rnp)) { /* irqs already off, so later. */
Paul E. McKenney6eaef632013-03-19 10:08:37 -07001767 local_irq_restore(flags);
1768 return;
1769 }
Paul E. McKenneyc7e48f72018-07-03 17:22:34 -07001770 needwake = __note_gp_changes(rnp, rdp);
Boqun Feng67c583a72015-12-29 12:18:47 +08001771 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
Paul E. McKenney48a76392014-03-11 13:02:16 -07001772 if (needwake)
Paul E. McKenney532c00c2018-07-03 17:22:34 -07001773 rcu_gp_kthread_wake();
Paul E. McKenney6eaef632013-03-19 10:08:37 -07001774}
1775
Paul E. McKenney22212332018-07-03 17:22:34 -07001776static void rcu_gp_slow(int delay)
Paul E. McKenney0f41c0d2015-03-10 18:33:20 -07001777{
1778 if (delay > 0 &&
Paul E. McKenney22212332018-07-03 17:22:34 -07001779 !(rcu_seq_ctr(rcu_state.gp_seq) %
Paul E. McKenneydee4f422018-04-26 15:30:28 -07001780 (rcu_num_nodes * PER_RCU_NODE_PERIOD * delay)))
Paul E. McKenney0f41c0d2015-03-10 18:33:20 -07001781 schedule_timeout_uninterruptible(delay);
1782}
1783
Paul E. McKenney6eaef632013-03-19 10:08:37 -07001784/*
Paul E. McKenney45fed3e2015-11-07 23:35:00 -08001785 * Initialize a new grace period. Return false if no grace period required.
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001786 */
Paul E. McKenney0854a05c2018-07-03 17:22:34 -07001787static bool rcu_gp_init(void)
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001788{
Paul E. McKenneyec2c2972018-05-07 09:34:17 -07001789 unsigned long flags;
Paul E. McKenney0aa04b02015-01-23 21:52:37 -08001790 unsigned long oldmask;
Paul E. McKenneyec2c2972018-05-07 09:34:17 -07001791 unsigned long mask;
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001792 struct rcu_data *rdp;
Paul E. McKenney336a4f62018-07-03 17:22:34 -07001793 struct rcu_node *rnp = rcu_get_root();
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001794
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001795 WRITE_ONCE(rcu_state.gp_activity, jiffies);
Peter Zijlstra2a67e742015-10-08 12:24:23 +02001796 raw_spin_lock_irq_rcu_node(rnp);
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001797 if (!READ_ONCE(rcu_state.gp_flags)) {
Paul E. McKenneyf7be8202013-08-08 18:27:52 -07001798 /* Spurious wakeup, tell caller to go back to sleep. */
Boqun Feng67c583a72015-12-29 12:18:47 +08001799 raw_spin_unlock_irq_rcu_node(rnp);
Paul E. McKenney45fed3e2015-11-07 23:35:00 -08001800 return false;
Paul E. McKenneyf7be8202013-08-08 18:27:52 -07001801 }
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001802 WRITE_ONCE(rcu_state.gp_flags, 0); /* Clear all flags: New GP. */
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001803
Paul E. McKenneyde8e8732018-07-03 17:22:34 -07001804 if (WARN_ON_ONCE(rcu_gp_in_progress())) {
Paul E. McKenneyf7be8202013-08-08 18:27:52 -07001805 /*
1806 * Grace period already in progress, don't start another.
1807 * Not supposed to be able to happen.
1808 */
Boqun Feng67c583a72015-12-29 12:18:47 +08001809 raw_spin_unlock_irq_rcu_node(rnp);
Paul E. McKenney45fed3e2015-11-07 23:35:00 -08001810 return false;
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001811 }
1812
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001813 /* Advance to a new grace period and initialize state. */
Paul E. McKenneyad3832e2018-07-03 17:22:34 -07001814 record_gp_stall_check_time();
Paul E. McKenneyff3bb6f2018-05-01 14:34:08 -07001815 /* Record GP times before starting GP, hence rcu_seq_start(). */
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001816 rcu_seq_start(&rcu_state.gp_seq);
1817 trace_rcu_grace_period(rcu_state.name, rcu_state.gp_seq, TPS("start"));
Boqun Feng67c583a72015-12-29 12:18:47 +08001818 raw_spin_unlock_irq_rcu_node(rnp);
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001819
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001820 /*
Paul E. McKenney0aa04b02015-01-23 21:52:37 -08001821 * Apply per-leaf buffered online and offline operations to the
1822 * rcu_node tree. Note that this new grace period need not wait
1823 * for subsequent online CPUs, and that quiescent-state forcing
1824 * will handle subsequent offline CPUs.
1825 */
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001826 rcu_state.gp_state = RCU_GP_ONOFF;
Paul E. McKenneyaedf4ba2018-07-04 14:33:59 -07001827 rcu_for_each_leaf_node(rnp) {
Mike Galbraith894d45b2018-08-15 09:05:29 -07001828 raw_spin_lock(&rcu_state.ofl_lock);
Peter Zijlstra2a67e742015-10-08 12:24:23 +02001829 raw_spin_lock_irq_rcu_node(rnp);
Paul E. McKenney0aa04b02015-01-23 21:52:37 -08001830 if (rnp->qsmaskinit == rnp->qsmaskinitnext &&
1831 !rnp->wait_blkd_tasks) {
1832 /* Nothing to do on this leaf rcu_node structure. */
Boqun Feng67c583a72015-12-29 12:18:47 +08001833 raw_spin_unlock_irq_rcu_node(rnp);
Mike Galbraith894d45b2018-08-15 09:05:29 -07001834 raw_spin_unlock(&rcu_state.ofl_lock);
Paul E. McKenney0aa04b02015-01-23 21:52:37 -08001835 continue;
1836 }
1837
1838 /* Record old state, apply changes to ->qsmaskinit field. */
1839 oldmask = rnp->qsmaskinit;
1840 rnp->qsmaskinit = rnp->qsmaskinitnext;
1841
1842 /* If zero-ness of ->qsmaskinit changed, propagate up tree. */
1843 if (!oldmask != !rnp->qsmaskinit) {
Paul E. McKenney962aff02018-05-02 12:49:21 -07001844 if (!oldmask) { /* First online CPU for rcu_node. */
1845 if (!rnp->wait_blkd_tasks) /* Ever offline? */
1846 rcu_init_new_rnp(rnp);
1847 } else if (rcu_preempt_has_tasks(rnp)) {
1848 rnp->wait_blkd_tasks = true; /* blocked tasks */
1849 } else { /* Last offline CPU and can propagate. */
Paul E. McKenney0aa04b02015-01-23 21:52:37 -08001850 rcu_cleanup_dead_rnp(rnp);
Paul E. McKenney962aff02018-05-02 12:49:21 -07001851 }
Paul E. McKenney0aa04b02015-01-23 21:52:37 -08001852 }
1853
1854 /*
1855 * If all waited-on tasks from prior grace period are
1856 * done, and if all this rcu_node structure's CPUs are
1857 * still offline, propagate up the rcu_node tree and
1858 * clear ->wait_blkd_tasks. Otherwise, if one of this
1859 * rcu_node structure's CPUs has since come back online,
Paul E. McKenney962aff02018-05-02 12:49:21 -07001860 * simply clear ->wait_blkd_tasks.
Paul E. McKenney0aa04b02015-01-23 21:52:37 -08001861 */
1862 if (rnp->wait_blkd_tasks &&
Paul E. McKenney962aff02018-05-02 12:49:21 -07001863 (!rcu_preempt_has_tasks(rnp) || rnp->qsmaskinit)) {
Paul E. McKenney0aa04b02015-01-23 21:52:37 -08001864 rnp->wait_blkd_tasks = false;
Paul E. McKenney962aff02018-05-02 12:49:21 -07001865 if (!rnp->qsmaskinit)
1866 rcu_cleanup_dead_rnp(rnp);
Paul E. McKenney0aa04b02015-01-23 21:52:37 -08001867 }
1868
Boqun Feng67c583a72015-12-29 12:18:47 +08001869 raw_spin_unlock_irq_rcu_node(rnp);
Mike Galbraith894d45b2018-08-15 09:05:29 -07001870 raw_spin_unlock(&rcu_state.ofl_lock);
Paul E. McKenney0aa04b02015-01-23 21:52:37 -08001871 }
Paul E. McKenney22212332018-07-03 17:22:34 -07001872 rcu_gp_slow(gp_preinit_delay); /* Races with CPU hotplug. */
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001873
1874 /*
1875 * Set the quiescent-state-needed bits in all the rcu_node
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001876 * structures for all currently online CPUs in breadth-first
1877 * order, starting from the root rcu_node structure, relying on the
1878 * layout of the tree within the rcu_state.node[] array. Note that
1879 * other CPUs will access only the leaves of the hierarchy, thus
1880 * seeing that no grace period is in progress, at least until the
1881 * corresponding leaf node has been initialized.
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001882 *
1883 * The grace period cannot complete until the initialization
1884 * process finishes, because this kthread handles both.
1885 */
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001886 rcu_state.gp_state = RCU_GP_INIT;
Paul E. McKenneyaedf4ba2018-07-04 14:33:59 -07001887 rcu_for_each_node_breadth_first(rnp) {
Paul E. McKenney22212332018-07-03 17:22:34 -07001888 rcu_gp_slow(gp_init_delay);
Paul E. McKenneyec2c2972018-05-07 09:34:17 -07001889 raw_spin_lock_irqsave_rcu_node(rnp, flags);
Paul E. McKenneyda1df502018-07-03 15:37:16 -07001890 rdp = this_cpu_ptr(&rcu_data);
Paul E. McKenney81ab59a2018-07-03 17:22:34 -07001891 rcu_preempt_check_blocked_tasks(rnp);
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001892 rnp->qsmask = rnp->qsmaskinit;
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001893 WRITE_ONCE(rnp->gp_seq, rcu_state.gp_seq);
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001894 if (rnp == rdp->mynode)
Paul E. McKenneyc7e48f72018-07-03 17:22:34 -07001895 (void)__note_gp_changes(rnp, rdp);
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001896 rcu_preempt_boost_start_gp(rnp);
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001897 trace_rcu_grace_period_init(rcu_state.name, rnp->gp_seq,
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001898 rnp->level, rnp->grplo,
1899 rnp->grphi, rnp->qsmask);
Paul E. McKenneyec2c2972018-05-07 09:34:17 -07001900 /* Quiescent states for tasks on any now-offline CPUs. */
1901 mask = rnp->qsmask & ~rnp->qsmaskinitnext;
Paul E. McKenneyf2e2df52018-05-15 16:23:23 -07001902 rnp->rcu_gp_init_mask = mask;
Paul E. McKenneyec2c2972018-05-07 09:34:17 -07001903 if ((mask || rnp->wait_blkd_tasks) && rcu_is_leaf_node(rnp))
Paul E. McKenneyb50912d2018-07-03 17:22:34 -07001904 rcu_report_qs_rnp(mask, rnp, rnp->gp_seq, flags);
Paul E. McKenneyec2c2972018-05-07 09:34:17 -07001905 else
1906 raw_spin_unlock_irq_rcu_node(rnp);
Paul E. McKenneycee43932018-03-02 16:35:27 -08001907 cond_resched_tasks_rcu_qs();
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001908 WRITE_ONCE(rcu_state.gp_activity, jiffies);
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001909 }
1910
Paul E. McKenney45fed3e2015-11-07 23:35:00 -08001911 return true;
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001912}
1913
1914/*
Peter Zijlstrab3dae102018-06-12 10:34:52 +02001915 * Helper function for swait_event_idle_exclusive() wakeup at force-quiescent-state
Luis R. Rodriguezd5374222017-06-20 14:45:47 -07001916 * time.
Paul E. McKenneyb9a425c2015-07-01 13:50:28 -07001917 */
Paul E. McKenney0854a05c2018-07-03 17:22:34 -07001918static bool rcu_gp_fqs_check_wake(int *gfp)
Paul E. McKenneyb9a425c2015-07-01 13:50:28 -07001919{
Paul E. McKenney336a4f62018-07-03 17:22:34 -07001920 struct rcu_node *rnp = rcu_get_root();
Paul E. McKenneyb9a425c2015-07-01 13:50:28 -07001921
1922 /* Someone like call_rcu() requested a force-quiescent-state scan. */
Paul E. McKenney0854a05c2018-07-03 17:22:34 -07001923 *gfp = READ_ONCE(rcu_state.gp_flags);
Paul E. McKenneyb9a425c2015-07-01 13:50:28 -07001924 if (*gfp & RCU_GP_FLAG_FQS)
1925 return true;
1926
1927 /* The current grace period has completed. */
1928 if (!READ_ONCE(rnp->qsmask) && !rcu_preempt_blocked_readers_cgp(rnp))
1929 return true;
1930
1931 return false;
1932}
1933
1934/*
Paul E. McKenney4cdfc1752012-06-22 17:06:26 -07001935 * Do one round of quiescent-state forcing.
1936 */
Paul E. McKenney0854a05c2018-07-03 17:22:34 -07001937static void rcu_gp_fqs(bool first_time)
Paul E. McKenney4cdfc1752012-06-22 17:06:26 -07001938{
Paul E. McKenney336a4f62018-07-03 17:22:34 -07001939 struct rcu_node *rnp = rcu_get_root();
Paul E. McKenney4cdfc1752012-06-22 17:06:26 -07001940
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001941 WRITE_ONCE(rcu_state.gp_activity, jiffies);
1942 rcu_state.n_force_qs++;
Petr Mladek77f81fe2015-09-09 12:09:49 -07001943 if (first_time) {
Paul E. McKenney4cdfc1752012-06-22 17:06:26 -07001944 /* Collect dyntick-idle snapshots. */
Paul E. McKenneye9ecb782018-07-03 17:22:34 -07001945 force_qs_rnp(dyntick_save_progress_counter);
Paul E. McKenney4cdfc1752012-06-22 17:06:26 -07001946 } else {
1947 /* Handle dyntick-idle and offline CPUs. */
Paul E. McKenneye9ecb782018-07-03 17:22:34 -07001948 force_qs_rnp(rcu_implicit_dynticks_qs);
Paul E. McKenney4cdfc1752012-06-22 17:06:26 -07001949 }
1950 /* Clear flag to prevent immediate re-entry. */
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001951 if (READ_ONCE(rcu_state.gp_flags) & RCU_GP_FLAG_FQS) {
Peter Zijlstra2a67e742015-10-08 12:24:23 +02001952 raw_spin_lock_irq_rcu_node(rnp);
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001953 WRITE_ONCE(rcu_state.gp_flags,
1954 READ_ONCE(rcu_state.gp_flags) & ~RCU_GP_FLAG_FQS);
Boqun Feng67c583a72015-12-29 12:18:47 +08001955 raw_spin_unlock_irq_rcu_node(rnp);
Paul E. McKenney4cdfc1752012-06-22 17:06:26 -07001956 }
Paul E. McKenney4cdfc1752012-06-22 17:06:26 -07001957}
1958
1959/*
Paul E. McKenneyc3854a02018-07-05 18:23:23 -07001960 * Loop doing repeated quiescent-state forcing until the grace period ends.
1961 */
1962static void rcu_gp_fqs_loop(void)
1963{
1964 bool first_gp_fqs;
1965 int gf;
1966 unsigned long j;
1967 int ret;
1968 struct rcu_node *rnp = rcu_get_root();
1969
1970 first_gp_fqs = true;
Paul E. McKenneyc06aed02018-07-25 11:25:23 -07001971 j = READ_ONCE(jiffies_till_first_fqs);
Paul E. McKenneyc3854a02018-07-05 18:23:23 -07001972 ret = 0;
1973 for (;;) {
1974 if (!ret) {
1975 rcu_state.jiffies_force_qs = jiffies + j;
1976 WRITE_ONCE(rcu_state.jiffies_kick_kthreads,
Paul E. McKenney9cf422a2018-11-20 10:43:34 -08001977 jiffies + (j ? 3 * j : 2));
Paul E. McKenneyc3854a02018-07-05 18:23:23 -07001978 }
1979 trace_rcu_grace_period(rcu_state.name,
1980 READ_ONCE(rcu_state.gp_seq),
1981 TPS("fqswait"));
1982 rcu_state.gp_state = RCU_GP_WAIT_FQS;
1983 ret = swait_event_idle_timeout_exclusive(
1984 rcu_state.gp_wq, rcu_gp_fqs_check_wake(&gf), j);
1985 rcu_state.gp_state = RCU_GP_DOING_FQS;
1986 /* Locking provides needed memory barriers. */
1987 /* If grace period done, leave loop. */
1988 if (!READ_ONCE(rnp->qsmask) &&
1989 !rcu_preempt_blocked_readers_cgp(rnp))
1990 break;
1991 /* If time for quiescent-state forcing, do it. */
1992 if (ULONG_CMP_GE(jiffies, rcu_state.jiffies_force_qs) ||
1993 (gf & RCU_GP_FLAG_FQS)) {
1994 trace_rcu_grace_period(rcu_state.name,
1995 READ_ONCE(rcu_state.gp_seq),
1996 TPS("fqsstart"));
1997 rcu_gp_fqs(first_gp_fqs);
1998 first_gp_fqs = false;
1999 trace_rcu_grace_period(rcu_state.name,
2000 READ_ONCE(rcu_state.gp_seq),
2001 TPS("fqsend"));
2002 cond_resched_tasks_rcu_qs();
2003 WRITE_ONCE(rcu_state.gp_activity, jiffies);
2004 ret = 0; /* Force full wait till next FQS. */
Paul E. McKenneyc06aed02018-07-25 11:25:23 -07002005 j = READ_ONCE(jiffies_till_next_fqs);
Paul E. McKenneyc3854a02018-07-05 18:23:23 -07002006 } else {
2007 /* Deal with stray signal. */
2008 cond_resched_tasks_rcu_qs();
2009 WRITE_ONCE(rcu_state.gp_activity, jiffies);
2010 WARN_ON(signal_pending(current));
2011 trace_rcu_grace_period(rcu_state.name,
2012 READ_ONCE(rcu_state.gp_seq),
2013 TPS("fqswaitsig"));
2014 ret = 1; /* Keep old FQS timing. */
2015 j = jiffies;
2016 if (time_after(jiffies, rcu_state.jiffies_force_qs))
2017 j = 1;
2018 else
2019 j = rcu_state.jiffies_force_qs - j;
2020 }
2021 }
2022}
2023
2024/*
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07002025 * Clean up after the old grace period.
2026 */
Paul E. McKenney0854a05c2018-07-03 17:22:34 -07002027static void rcu_gp_cleanup(void)
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07002028{
2029 unsigned long gp_duration;
Paul E. McKenney48a76392014-03-11 13:02:16 -07002030 bool needgp = false;
Paul E. McKenneyde30ad52018-04-26 11:52:09 -07002031 unsigned long new_gp_seq;
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07002032 struct rcu_data *rdp;
Paul E. McKenney336a4f62018-07-03 17:22:34 -07002033 struct rcu_node *rnp = rcu_get_root();
Paul Gortmakerabedf8e2016-02-19 09:46:41 +01002034 struct swait_queue_head *sq;
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07002035
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07002036 WRITE_ONCE(rcu_state.gp_activity, jiffies);
Peter Zijlstra2a67e742015-10-08 12:24:23 +02002037 raw_spin_lock_irq_rcu_node(rnp);
Paul E. McKenneyc51d7b52018-10-03 17:25:33 -07002038 rcu_state.gp_end = jiffies;
2039 gp_duration = rcu_state.gp_end - rcu_state.gp_start;
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07002040 if (gp_duration > rcu_state.gp_max)
2041 rcu_state.gp_max = gp_duration;
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07002042
2043 /*
2044 * We know the grace period is complete, but to everyone else
2045 * it appears to still be ongoing. But it is also the case
2046 * that to everyone else it looks like there is nothing that
2047 * they can do to advance the grace period. It is therefore
2048 * safe for us to drop the lock in order to mark the grace
2049 * period as completed in all of the rcu_node structures.
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07002050 */
Boqun Feng67c583a72015-12-29 12:18:47 +08002051 raw_spin_unlock_irq_rcu_node(rnp);
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07002052
Paul E. McKenney5d4b8652012-07-07 07:56:57 -07002053 /*
Paul E. McKenneyff3bb6f2018-05-01 14:34:08 -07002054 * Propagate new ->gp_seq value to rcu_node structures so that
2055 * other CPUs don't have to wait until the start of the next grace
2056 * period to process their callbacks. This also avoids some nasty
2057 * RCU grace-period initialization races by forcing the end of
2058 * the current grace period to be completely recorded in all of
2059 * the rcu_node structures before the beginning of the next grace
2060 * period is recorded in any of the rcu_node structures.
Paul E. McKenney5d4b8652012-07-07 07:56:57 -07002061 */
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07002062 new_gp_seq = rcu_state.gp_seq;
Paul E. McKenneyde30ad52018-04-26 11:52:09 -07002063 rcu_seq_end(&new_gp_seq);
Paul E. McKenneyaedf4ba2018-07-04 14:33:59 -07002064 rcu_for_each_node_breadth_first(rnp) {
Peter Zijlstra2a67e742015-10-08 12:24:23 +02002065 raw_spin_lock_irq_rcu_node(rnp);
Paul E. McKenney4bc8d552017-11-27 15:13:56 -08002066 if (WARN_ON_ONCE(rcu_preempt_blocked_readers_cgp(rnp)))
Paul E. McKenney81ab59a2018-07-03 17:22:34 -07002067 dump_blkd_tasks(rnp, 10);
Paul E. McKenney5c60d252015-02-09 05:37:47 -08002068 WARN_ON_ONCE(rnp->qsmask);
Paul E. McKenneyde30ad52018-04-26 11:52:09 -07002069 WRITE_ONCE(rnp->gp_seq, new_gp_seq);
Paul E. McKenneyda1df502018-07-03 15:37:16 -07002070 rdp = this_cpu_ptr(&rcu_data);
Paul E. McKenneyb11cc572012-12-17 14:21:14 -08002071 if (rnp == rdp->mynode)
Paul E. McKenneyc7e48f72018-07-03 17:22:34 -07002072 needgp = __note_gp_changes(rnp, rdp) || needgp;
Paul E. McKenney78e4bc32013-09-24 15:04:06 -07002073 /* smp_mb() provided by prior unlock-lock pair. */
Paul E. McKenney3481f2e2018-07-03 17:22:34 -07002074 needgp = rcu_future_gp_cleanup(rnp) || needgp;
Daniel Wagner065bb782016-02-19 09:46:40 +01002075 sq = rcu_nocb_gp_get(rnp);
Boqun Feng67c583a72015-12-29 12:18:47 +08002076 raw_spin_unlock_irq_rcu_node(rnp);
Daniel Wagner065bb782016-02-19 09:46:40 +01002077 rcu_nocb_gp_cleanup(sq);
Paul E. McKenneycee43932018-03-02 16:35:27 -08002078 cond_resched_tasks_rcu_qs();
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07002079 WRITE_ONCE(rcu_state.gp_activity, jiffies);
Paul E. McKenney22212332018-07-03 17:22:34 -07002080 rcu_gp_slow(gp_cleanup_delay);
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07002081 }
Paul E. McKenney336a4f62018-07-03 17:22:34 -07002082 rnp = rcu_get_root();
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07002083 raw_spin_lock_irq_rcu_node(rnp); /* GP before ->gp_seq update. */
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07002084
Paul E. McKenney0a89e5a2018-10-15 10:00:58 -07002085 /* Declare grace period done, trace first to use old GP number. */
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07002086 trace_rcu_grace_period(rcu_state.name, rcu_state.gp_seq, TPS("end"));
Paul E. McKenney0a89e5a2018-10-15 10:00:58 -07002087 rcu_seq_end(&rcu_state.gp_seq);
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07002088 rcu_state.gp_state = RCU_GP_IDLE;
Paul E. McKenneyfb313402018-04-12 07:20:30 -07002089 /* Check for GP requests since above loop. */
Paul E. McKenneyda1df502018-07-03 15:37:16 -07002090 rdp = this_cpu_ptr(&rcu_data);
Joel Fernandes (Google)5b550722018-05-13 20:15:40 -07002091 if (!needgp && ULONG_CMP_LT(rnp->gp_seq, rnp->gp_seq_needed)) {
Paul E. McKenneyabd13fd2018-05-01 13:08:46 -07002092 trace_rcu_this_gp(rnp, rdp, rnp->gp_seq_needed,
Paul E. McKenney41e80592018-04-12 11:24:09 -07002093 TPS("CleanupMore"));
Paul E. McKenneyfb313402018-04-12 07:20:30 -07002094 needgp = true;
2095 }
Paul E. McKenney48a76392014-03-11 13:02:16 -07002096 /* Advance CBs to reduce false positives below. */
Paul E. McKenney02f50142018-07-03 17:22:34 -07002097 if (!rcu_accelerate_cbs(rnp, rdp) && needgp) {
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07002098 WRITE_ONCE(rcu_state.gp_flags, RCU_GP_FLAG_INIT);
2099 rcu_state.gp_req_activity = jiffies;
2100 trace_rcu_grace_period(rcu_state.name,
2101 READ_ONCE(rcu_state.gp_seq),
Paul E. McKenneybb311ec2013-08-09 16:02:09 -07002102 TPS("newreq"));
Paul E. McKenney18390ae2018-04-22 15:06:05 -07002103 } else {
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07002104 WRITE_ONCE(rcu_state.gp_flags,
2105 rcu_state.gp_flags & RCU_GP_FLAG_INIT);
Paul E. McKenneybb311ec2013-08-09 16:02:09 -07002106 }
Boqun Feng67c583a72015-12-29 12:18:47 +08002107 raw_spin_unlock_irq_rcu_node(rnp);
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07002108}
2109
2110/*
Paul E. McKenneyb3dbec72012-06-18 18:36:08 -07002111 * Body of kthread that handles grace periods.
2112 */
Paul E. McKenney0854a05c2018-07-03 17:22:34 -07002113static int __noreturn rcu_gp_kthread(void *unused)
Paul E. McKenneyb3dbec72012-06-18 18:36:08 -07002114{
Paul E. McKenney58719682015-02-24 11:05:36 -08002115 rcu_bind_gp_kthread();
Paul E. McKenneyb3dbec72012-06-18 18:36:08 -07002116 for (;;) {
2117
2118 /* Handle grace-period start. */
Paul E. McKenneyb3dbec72012-06-18 18:36:08 -07002119 for (;;) {
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07002120 trace_rcu_grace_period(rcu_state.name,
2121 READ_ONCE(rcu_state.gp_seq),
Paul E. McKenney63c4db72013-08-09 12:19:29 -07002122 TPS("reqwait"));
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07002123 rcu_state.gp_state = RCU_GP_WAIT_GPS;
2124 swait_event_idle_exclusive(rcu_state.gp_wq,
2125 READ_ONCE(rcu_state.gp_flags) &
2126 RCU_GP_FLAG_INIT);
2127 rcu_state.gp_state = RCU_GP_DONE_GPS;
Paul E. McKenney78e4bc32013-09-24 15:04:06 -07002128 /* Locking provides needed memory barrier. */
Paul E. McKenney0854a05c2018-07-03 17:22:34 -07002129 if (rcu_gp_init())
Paul E. McKenneyb3dbec72012-06-18 18:36:08 -07002130 break;
Paul E. McKenneycee43932018-03-02 16:35:27 -08002131 cond_resched_tasks_rcu_qs();
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07002132 WRITE_ONCE(rcu_state.gp_activity, jiffies);
Paul E. McKenney73a860c2014-08-14 10:28:23 -07002133 WARN_ON(signal_pending(current));
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07002134 trace_rcu_grace_period(rcu_state.name,
2135 READ_ONCE(rcu_state.gp_seq),
Paul E. McKenney63c4db72013-08-09 12:19:29 -07002136 TPS("reqwaitsig"));
Paul E. McKenneyb3dbec72012-06-18 18:36:08 -07002137 }
Paul E. McKenneycabc49c2012-06-20 17:07:14 -07002138
Paul E. McKenney4cdfc1752012-06-22 17:06:26 -07002139 /* Handle quiescent-state forcing. */
Paul E. McKenneyc3854a02018-07-05 18:23:23 -07002140 rcu_gp_fqs_loop();
Paul E. McKenney4cdfc1752012-06-22 17:06:26 -07002141
2142 /* Handle grace-period end. */
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07002143 rcu_state.gp_state = RCU_GP_CLEANUP;
Paul E. McKenney0854a05c2018-07-03 17:22:34 -07002144 rcu_gp_cleanup();
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07002145 rcu_state.gp_state = RCU_GP_CLEANED;
Paul E. McKenneyb3dbec72012-06-18 18:36:08 -07002146 }
Paul E. McKenneyb3dbec72012-06-18 18:36:08 -07002147}
2148
2149/*
Paul E. McKenney49918a52018-07-07 18:12:26 -07002150 * Report a full set of quiescent states to the rcu_state data structure.
2151 * Invoke rcu_gp_kthread_wake() to awaken the grace-period kthread if
2152 * another grace period is required. Whether we wake the grace-period
2153 * kthread or it awakens itself for the next round of quiescent-state
2154 * forcing, that kthread will clean up after the just-completed grace
2155 * period. Note that the caller must hold rnp->lock, which is released
2156 * before return.
Paul E. McKenneyf41d9112009-08-22 13:56:52 -07002157 */
Paul E. McKenneyaff4e9e2018-07-03 17:22:34 -07002158static void rcu_report_qs_rsp(unsigned long flags)
Paul E. McKenney336a4f62018-07-03 17:22:34 -07002159 __releases(rcu_get_root()->lock)
Paul E. McKenneyf41d9112009-08-22 13:56:52 -07002160{
Paul E. McKenney336a4f62018-07-03 17:22:34 -07002161 raw_lockdep_assert_held_rcu_node(rcu_get_root());
Paul E. McKenneyde8e8732018-07-03 17:22:34 -07002162 WARN_ON_ONCE(!rcu_gp_in_progress());
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07002163 WRITE_ONCE(rcu_state.gp_flags,
2164 READ_ONCE(rcu_state.gp_flags) | RCU_GP_FLAG_FQS);
Paul E. McKenney336a4f62018-07-03 17:22:34 -07002165 raw_spin_unlock_irqrestore_rcu_node(rcu_get_root(), flags);
Paul E. McKenney532c00c2018-07-03 17:22:34 -07002166 rcu_gp_kthread_wake();
Paul E. McKenneyf41d9112009-08-22 13:56:52 -07002167}
2168
2169/*
Paul E. McKenneyd3f6bad2009-12-02 12:10:13 -08002170 * Similar to rcu_report_qs_rdp(), for which it is a helper function.
2171 * Allows quiescent states for a group of CPUs to be reported at one go
2172 * to the specified rcu_node structure, though all the CPUs in the group
Paul E. McKenney654e9532015-03-15 09:19:35 -07002173 * must be represented by the same rcu_node structure (which need not be a
2174 * leaf rcu_node structure, though it often will be). The gps parameter
2175 * is the grace-period snapshot, which means that the quiescent states
Paul E. McKenneyc9a24e22018-04-27 14:54:46 -07002176 * are valid only if rnp->gp_seq is equal to gps. That structure's lock
Paul E. McKenney654e9532015-03-15 09:19:35 -07002177 * must be held upon entry, and it is released before return.
Paul E. McKenneyec2c2972018-05-07 09:34:17 -07002178 *
2179 * As a special case, if mask is zero, the bit-already-cleared check is
2180 * disabled. This allows propagating quiescent state due to resumed tasks
2181 * during grace-period initialization.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002182 */
Paul E. McKenneyb50912d2018-07-03 17:22:34 -07002183static void rcu_report_qs_rnp(unsigned long mask, struct rcu_node *rnp,
2184 unsigned long gps, unsigned long flags)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002185 __releases(rnp->lock)
2186{
Paul E. McKenney654e9532015-03-15 09:19:35 -07002187 unsigned long oldmask = 0;
Paul E. McKenney28ecd582009-09-18 09:50:17 -07002188 struct rcu_node *rnp_c;
2189
Matthew Wilcoxa32e01e2018-01-17 06:24:30 -08002190 raw_lockdep_assert_held_rcu_node(rnp);
Paul E. McKenneyc0b334c2017-04-28 12:32:15 -07002191
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002192 /* Walk up the rcu_node hierarchy. */
2193 for (;;) {
Paul E. McKenneyec2c2972018-05-07 09:34:17 -07002194 if ((!(rnp->qsmask & mask) && mask) || rnp->gp_seq != gps) {
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002195
Paul E. McKenney654e9532015-03-15 09:19:35 -07002196 /*
2197 * Our bit has already been cleared, or the
2198 * relevant grace period is already over, so done.
2199 */
Boqun Feng67c583a72015-12-29 12:18:47 +08002200 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002201 return;
2202 }
Paul E. McKenney654e9532015-03-15 09:19:35 -07002203 WARN_ON_ONCE(oldmask); /* Any child must be all zeroed! */
Paul E. McKenney5b4c11d2018-04-13 17:11:44 -07002204 WARN_ON_ONCE(!rcu_is_leaf_node(rnp) &&
Paul E. McKenney2dee9402017-07-11 21:52:31 -07002205 rcu_preempt_blocked_readers_cgp(rnp));
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002206 rnp->qsmask &= ~mask;
Paul E. McKenney67a0edb2018-07-05 16:15:38 -07002207 trace_rcu_quiescent_state_report(rcu_state.name, rnp->gp_seq,
Paul E. McKenneyd4c08f22011-06-25 06:36:56 -07002208 mask, rnp->qsmask, rnp->level,
2209 rnp->grplo, rnp->grphi,
2210 !!rnp->gp_tasks);
Paul E. McKenney27f4d282011-02-07 12:47:15 -08002211 if (rnp->qsmask != 0 || rcu_preempt_blocked_readers_cgp(rnp)) {
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002212
2213 /* Other bits still set at this level, so done. */
Boqun Feng67c583a72015-12-29 12:18:47 +08002214 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002215 return;
2216 }
Paul E. McKenneyd43a5d32018-04-28 18:50:06 -07002217 rnp->completedqs = rnp->gp_seq;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002218 mask = rnp->grpmask;
2219 if (rnp->parent == NULL) {
2220
2221 /* No more levels. Exit loop holding root lock. */
2222
2223 break;
2224 }
Boqun Feng67c583a72015-12-29 12:18:47 +08002225 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
Paul E. McKenney28ecd582009-09-18 09:50:17 -07002226 rnp_c = rnp;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002227 rnp = rnp->parent;
Peter Zijlstra2a67e742015-10-08 12:24:23 +02002228 raw_spin_lock_irqsave_rcu_node(rnp, flags);
Paul E. McKenney654e9532015-03-15 09:19:35 -07002229 oldmask = rnp_c->qsmask;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002230 }
2231
2232 /*
2233 * Get here if we are the last CPU to pass through a quiescent
Paul E. McKenneyd3f6bad2009-12-02 12:10:13 -08002234 * state for this grace period. Invoke rcu_report_qs_rsp()
Paul E. McKenneyf41d9112009-08-22 13:56:52 -07002235 * to clean up and start the next grace period if one is needed.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002236 */
Paul E. McKenneyaff4e9e2018-07-03 17:22:34 -07002237 rcu_report_qs_rsp(flags); /* releases rnp->lock. */
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002238}
2239
2240/*
Paul E. McKenneycc99a312015-02-23 08:59:29 -08002241 * Record a quiescent state for all tasks that were previously queued
2242 * on the specified rcu_node structure and that were blocking the current
Paul E. McKenney49918a52018-07-07 18:12:26 -07002243 * RCU grace period. The caller must hold the corresponding rnp->lock with
Paul E. McKenneycc99a312015-02-23 08:59:29 -08002244 * irqs disabled, and this lock is released upon return, but irqs remain
2245 * disabled.
2246 */
Paul E. McKenney17a82122018-05-03 14:30:02 -07002247static void __maybe_unused
Paul E. McKenney139ad4d2018-07-03 17:22:34 -07002248rcu_report_unblock_qs_rnp(struct rcu_node *rnp, unsigned long flags)
Paul E. McKenneycc99a312015-02-23 08:59:29 -08002249 __releases(rnp->lock)
2250{
Paul E. McKenney654e9532015-03-15 09:19:35 -07002251 unsigned long gps;
Paul E. McKenneycc99a312015-02-23 08:59:29 -08002252 unsigned long mask;
2253 struct rcu_node *rnp_p;
2254
Matthew Wilcoxa32e01e2018-01-17 06:24:30 -08002255 raw_lockdep_assert_held_rcu_node(rnp);
Paul E. McKenney45975c72018-07-02 14:30:37 -07002256 if (WARN_ON_ONCE(!IS_ENABLED(CONFIG_PREEMPT)) ||
Paul E. McKenneyc74859d2018-04-27 14:05:27 -07002257 WARN_ON_ONCE(rcu_preempt_blocked_readers_cgp(rnp)) ||
2258 rnp->qsmask != 0) {
Boqun Feng67c583a72015-12-29 12:18:47 +08002259 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
Paul E. McKenneycc99a312015-02-23 08:59:29 -08002260 return; /* Still need more quiescent states! */
2261 }
2262
Paul E. McKenney77cfc7b2018-05-01 15:00:10 -07002263 rnp->completedqs = rnp->gp_seq;
Paul E. McKenneycc99a312015-02-23 08:59:29 -08002264 rnp_p = rnp->parent;
2265 if (rnp_p == NULL) {
2266 /*
Paul E. McKenneya77da142015-03-08 14:52:27 -07002267 * Only one rcu_node structure in the tree, so don't
2268 * try to report up to its nonexistent parent!
Paul E. McKenneycc99a312015-02-23 08:59:29 -08002269 */
Paul E. McKenneyaff4e9e2018-07-03 17:22:34 -07002270 rcu_report_qs_rsp(flags);
Paul E. McKenneycc99a312015-02-23 08:59:29 -08002271 return;
2272 }
2273
Paul E. McKenneyc9a24e22018-04-27 14:54:46 -07002274 /* Report up the rest of the hierarchy, tracking current ->gp_seq. */
2275 gps = rnp->gp_seq;
Paul E. McKenneycc99a312015-02-23 08:59:29 -08002276 mask = rnp->grpmask;
Boqun Feng67c583a72015-12-29 12:18:47 +08002277 raw_spin_unlock_rcu_node(rnp); /* irqs remain disabled. */
Peter Zijlstra2a67e742015-10-08 12:24:23 +02002278 raw_spin_lock_rcu_node(rnp_p); /* irqs already disabled. */
Paul E. McKenneyb50912d2018-07-03 17:22:34 -07002279 rcu_report_qs_rnp(mask, rnp_p, gps, flags);
Paul E. McKenneycc99a312015-02-23 08:59:29 -08002280}
2281
2282/*
Paul E. McKenneyd3f6bad2009-12-02 12:10:13 -08002283 * Record a quiescent state for the specified CPU to that CPU's rcu_data
Paul E. McKenney4b455dc2016-01-27 22:44:45 -08002284 * structure. This must be called from the specified CPU.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002285 */
2286static void
Paul E. McKenney33085c42018-07-03 17:22:34 -07002287rcu_report_qs_rdp(int cpu, struct rcu_data *rdp)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002288{
2289 unsigned long flags;
2290 unsigned long mask;
Paul E. McKenney48a76392014-03-11 13:02:16 -07002291 bool needwake;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002292 struct rcu_node *rnp;
2293
2294 rnp = rdp->mynode;
Peter Zijlstra2a67e742015-10-08 12:24:23 +02002295 raw_spin_lock_irqsave_rcu_node(rnp, flags);
Paul E. McKenneyc9a24e22018-04-27 14:54:46 -07002296 if (rdp->cpu_no_qs.b.norm || rdp->gp_seq != rnp->gp_seq ||
2297 rdp->gpwrap) {
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002298
2299 /*
Paul E. McKenneye4cc1f22011-06-27 00:17:43 -07002300 * The grace period in which this quiescent state was
2301 * recorded has ended, so don't report it upwards.
2302 * We will instead need a new quiescent state that lies
2303 * within the current grace period.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002304 */
Paul E. McKenney5b74c452015-08-06 15:16:57 -07002305 rdp->cpu_no_qs.b.norm = true; /* need qs for new gp. */
Boqun Feng67c583a72015-12-29 12:18:47 +08002306 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002307 return;
2308 }
2309 mask = rdp->grpmask;
2310 if ((rnp->qsmask & mask) == 0) {
Boqun Feng67c583a72015-12-29 12:18:47 +08002311 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002312 } else {
Paul E. McKenneybb53e412015-12-10 09:30:12 -08002313 rdp->core_needs_qs = false;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002314
2315 /*
2316 * This GP can't end until cpu checks in, so all of our
2317 * callbacks can be processed during the next GP.
2318 */
Paul E. McKenney02f50142018-07-03 17:22:34 -07002319 needwake = rcu_accelerate_cbs(rnp, rdp);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002320
Paul E. McKenneyb50912d2018-07-03 17:22:34 -07002321 rcu_report_qs_rnp(mask, rnp, rnp->gp_seq, flags);
Paul E. McKenney654e9532015-03-15 09:19:35 -07002322 /* ^^^ Released rnp->lock */
Paul E. McKenney48a76392014-03-11 13:02:16 -07002323 if (needwake)
Paul E. McKenney532c00c2018-07-03 17:22:34 -07002324 rcu_gp_kthread_wake();
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002325 }
2326}
2327
2328/*
2329 * Check to see if there is a new grace period of which this CPU
2330 * is not yet aware, and if so, set up local rcu_data state for it.
2331 * Otherwise, see if this CPU has just passed through its first
2332 * quiescent state for this grace period, and record that fact if so.
2333 */
2334static void
Paul E. McKenney8087d3e2018-07-03 17:22:34 -07002335rcu_check_quiescent_state(struct rcu_data *rdp)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002336{
Paul E. McKenney05eb5522013-03-19 12:38:24 -07002337 /* Check for grace-period ends and beginnings. */
Paul E. McKenney15cabdf2018-07-03 17:22:34 -07002338 note_gp_changes(rdp);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002339
2340 /*
2341 * Does this CPU still need to do its part for current grace period?
2342 * If no, return and let the other CPUs do their part as well.
2343 */
Paul E. McKenney97c668b2015-08-06 11:31:51 -07002344 if (!rdp->core_needs_qs)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002345 return;
2346
2347 /*
2348 * Was there a quiescent state since the beginning of the grace
2349 * period? If no, then exit and wait for the next call.
2350 */
Paul E. McKenney3a19b462016-11-30 11:21:21 -08002351 if (rdp->cpu_no_qs.b.norm)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002352 return;
2353
Paul E. McKenneyd3f6bad2009-12-02 12:10:13 -08002354 /*
2355 * Tell RCU we are done (but rcu_report_qs_rdp() will be the
2356 * judge of that).
2357 */
Paul E. McKenney33085c42018-07-03 17:22:34 -07002358 rcu_report_qs_rdp(rdp->cpu, rdp);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002359}
2360
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002361/*
Paul E. McKenney780cd592018-07-03 17:22:34 -07002362 * Near the end of the offline process. Trace the fact that this CPU
2363 * is going offline.
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08002364 */
Paul E. McKenney780cd592018-07-03 17:22:34 -07002365int rcutree_dying_cpu(unsigned int cpu)
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08002366{
Paul E. McKenney477351f2018-05-01 12:54:11 -07002367 RCU_TRACE(bool blkd;)
Paul E. McKenneyda1df502018-07-03 15:37:16 -07002368 RCU_TRACE(struct rcu_data *rdp = this_cpu_ptr(&rcu_data);)
Paul E. McKenney88a49762017-01-23 12:04:46 -08002369 RCU_TRACE(struct rcu_node *rnp = rdp->mynode;)
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08002370
Paul E. McKenneyea463512015-03-03 14:05:26 -08002371 if (!IS_ENABLED(CONFIG_HOTPLUG_CPU))
Paul E. McKenney780cd592018-07-03 17:22:34 -07002372 return 0;
Paul E. McKenneyea463512015-03-03 14:05:26 -08002373
Paul E. McKenney477351f2018-05-01 12:54:11 -07002374 RCU_TRACE(blkd = !!(rnp->qsmask & rdp->grpmask);)
Paul E. McKenney780cd592018-07-03 17:22:34 -07002375 trace_rcu_grace_period(rcu_state.name, rnp->gp_seq,
Paul E. McKenney477351f2018-05-01 12:54:11 -07002376 blkd ? TPS("cpuofl") : TPS("cpuofl-bgp"));
Paul E. McKenney780cd592018-07-03 17:22:34 -07002377 return 0;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002378}
2379
2380/*
Paul E. McKenney8af3a5e2014-10-31 11:22:37 -07002381 * All CPUs for the specified rcu_node structure have gone offline,
2382 * and all tasks that were preempted within an RCU read-side critical
2383 * section while running on one of those CPUs have since exited their RCU
2384 * read-side critical section. Some other CPU is reporting this fact with
2385 * the specified rcu_node structure's ->lock held and interrupts disabled.
2386 * This function therefore goes up the tree of rcu_node structures,
2387 * clearing the corresponding bits in the ->qsmaskinit fields. Note that
2388 * the leaf rcu_node structure's ->qsmaskinit field has already been
Paul E. McKenneyc50cbe52018-05-02 13:51:57 -07002389 * updated.
Paul E. McKenney8af3a5e2014-10-31 11:22:37 -07002390 *
2391 * This function does check that the specified rcu_node structure has
2392 * all CPUs offline and no blocked tasks, so it is OK to invoke it
2393 * prematurely. That said, invoking it after the fact will cost you
2394 * a needless lock acquisition. So once it has done its work, don't
2395 * invoke it again.
2396 */
2397static void rcu_cleanup_dead_rnp(struct rcu_node *rnp_leaf)
2398{
2399 long mask;
2400 struct rcu_node *rnp = rnp_leaf;
2401
Paul E. McKenney962aff02018-05-02 12:49:21 -07002402 raw_lockdep_assert_held_rcu_node(rnp_leaf);
Paul E. McKenneyea463512015-03-03 14:05:26 -08002403 if (!IS_ENABLED(CONFIG_HOTPLUG_CPU) ||
Paul E. McKenney962aff02018-05-02 12:49:21 -07002404 WARN_ON_ONCE(rnp_leaf->qsmaskinit) ||
2405 WARN_ON_ONCE(rcu_preempt_has_tasks(rnp_leaf)))
Paul E. McKenney8af3a5e2014-10-31 11:22:37 -07002406 return;
2407 for (;;) {
2408 mask = rnp->grpmask;
2409 rnp = rnp->parent;
2410 if (!rnp)
2411 break;
Peter Zijlstra2a67e742015-10-08 12:24:23 +02002412 raw_spin_lock_rcu_node(rnp); /* irqs already disabled. */
Paul E. McKenney8af3a5e2014-10-31 11:22:37 -07002413 rnp->qsmaskinit &= ~mask;
Paul E. McKenney962aff02018-05-02 12:49:21 -07002414 /* Between grace periods, so better already be zero! */
2415 WARN_ON_ONCE(rnp->qsmask);
Paul E. McKenney8af3a5e2014-10-31 11:22:37 -07002416 if (rnp->qsmaskinit) {
Boqun Feng67c583a72015-12-29 12:18:47 +08002417 raw_spin_unlock_rcu_node(rnp);
2418 /* irqs remain disabled. */
Paul E. McKenney8af3a5e2014-10-31 11:22:37 -07002419 return;
2420 }
Boqun Feng67c583a72015-12-29 12:18:47 +08002421 raw_spin_unlock_rcu_node(rnp); /* irqs remain disabled. */
Paul E. McKenney8af3a5e2014-10-31 11:22:37 -07002422 }
2423}
2424
2425/*
Paul E. McKenneye5601402012-01-07 11:03:57 -08002426 * The CPU has been completely removed, and some other CPU is reporting
Paul E. McKenneya58163d2017-06-20 12:11:34 -07002427 * this fact from process context. Do the remainder of the cleanup.
2428 * There can only be one CPU hotplug operation at a time, so no need for
2429 * explicit locking.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002430 */
Paul E. McKenney780cd592018-07-03 17:22:34 -07002431int rcutree_dead_cpu(unsigned int cpu)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002432{
Paul E. McKenneyda1df502018-07-03 15:37:16 -07002433 struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08002434 struct rcu_node *rnp = rdp->mynode; /* Outgoing CPU's rdp & rnp. */
Paul E. McKenneye5601402012-01-07 11:03:57 -08002435
Paul E. McKenneyea463512015-03-03 14:05:26 -08002436 if (!IS_ENABLED(CONFIG_HOTPLUG_CPU))
Paul E. McKenney780cd592018-07-03 17:22:34 -07002437 return 0;
Paul E. McKenneyea463512015-03-03 14:05:26 -08002438
Paul E. McKenney2036d942012-01-30 17:02:47 -08002439 /* Adjust any no-longer-needed kthreads. */
Thomas Gleixner5d01bbd2012-07-16 10:42:35 +00002440 rcu_boost_kthread_setaffinity(rnp, -1);
Paul E. McKenney780cd592018-07-03 17:22:34 -07002441 /* Do any needed no-CB deferred wakeups from this CPU. */
2442 do_nocb_deferred_wakeup(per_cpu_ptr(&rcu_data, cpu));
2443 return 0;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002444}
2445
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002446/*
2447 * Invoke any RCU callbacks that have made it to the end of their grace
2448 * period. Thottle as specified by rdp->blimit.
2449 */
Paul E. McKenney5bb5d092018-07-03 17:22:34 -07002450static void rcu_do_batch(struct rcu_data *rdp)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002451{
2452 unsigned long flags;
Paul E. McKenney15fecf82017-02-08 12:36:42 -08002453 struct rcu_head *rhp;
2454 struct rcu_cblist rcl = RCU_CBLIST_INITIALIZER(rcl);
2455 long bl, count;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002456
Paul E. McKenneydc35c892012-12-03 13:52:00 -08002457 /* If no callbacks are ready, just return. */
Paul E. McKenney15fecf82017-02-08 12:36:42 -08002458 if (!rcu_segcblist_ready_cbs(&rdp->cblist)) {
Paul E. McKenney3c779df2018-07-05 15:54:02 -07002459 trace_rcu_batch_start(rcu_state.name,
Paul E. McKenney15fecf82017-02-08 12:36:42 -08002460 rcu_segcblist_n_lazy_cbs(&rdp->cblist),
2461 rcu_segcblist_n_cbs(&rdp->cblist), 0);
Paul E. McKenney3c779df2018-07-05 15:54:02 -07002462 trace_rcu_batch_end(rcu_state.name, 0,
Paul E. McKenney15fecf82017-02-08 12:36:42 -08002463 !rcu_segcblist_empty(&rdp->cblist),
Paul E. McKenney4968c302011-12-07 16:32:40 -08002464 need_resched(), is_idle_task(current),
2465 rcu_is_callbacks_kthread());
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002466 return;
Paul E. McKenney29c00b42011-06-17 15:53:19 -07002467 }
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002468
2469 /*
2470 * Extract the list of ready callbacks, disabling to prevent
Paul E. McKenney15fecf82017-02-08 12:36:42 -08002471 * races with call_rcu() from interrupt handlers. Leave the
2472 * callback counts, as rcu_barrier() needs to be conservative.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002473 */
2474 local_irq_save(flags);
Paul E. McKenney8146c4e22012-01-10 14:23:29 -08002475 WARN_ON_ONCE(cpu_is_offline(smp_processor_id()));
Paul E. McKenney29c00b42011-06-17 15:53:19 -07002476 bl = rdp->blimit;
Paul E. McKenney3c779df2018-07-05 15:54:02 -07002477 trace_rcu_batch_start(rcu_state.name,
2478 rcu_segcblist_n_lazy_cbs(&rdp->cblist),
Paul E. McKenney15fecf82017-02-08 12:36:42 -08002479 rcu_segcblist_n_cbs(&rdp->cblist), bl);
2480 rcu_segcblist_extract_done_cbs(&rdp->cblist, &rcl);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002481 local_irq_restore(flags);
2482
2483 /* Invoke callbacks. */
Paul E. McKenney15fecf82017-02-08 12:36:42 -08002484 rhp = rcu_cblist_dequeue(&rcl);
2485 for (; rhp; rhp = rcu_cblist_dequeue(&rcl)) {
2486 debug_rcu_head_unqueue(rhp);
Paul E. McKenney3c779df2018-07-05 15:54:02 -07002487 if (__rcu_reclaim(rcu_state.name, rhp))
Paul E. McKenney15fecf82017-02-08 12:36:42 -08002488 rcu_cblist_dequeued_lazy(&rcl);
2489 /*
2490 * Stop only if limit reached and CPU has something to do.
2491 * Note: The rcl structure counts down from zero.
2492 */
Paul E. McKenney4b27f202017-05-02 08:45:25 -07002493 if (-rcl.len >= bl &&
Paul E. McKenneydff16722011-11-29 15:57:13 -08002494 (need_resched() ||
2495 (!is_idle_task(current) && !rcu_is_callbacks_kthread())))
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002496 break;
2497 }
2498
2499 local_irq_save(flags);
Paul E. McKenney4b27f202017-05-02 08:45:25 -07002500 count = -rcl.len;
Paul E. McKenney3c779df2018-07-05 15:54:02 -07002501 trace_rcu_batch_end(rcu_state.name, count, !!rcl.head, need_resched(),
Paul E. McKenney8ef0f372017-05-02 08:18:40 -07002502 is_idle_task(current), rcu_is_callbacks_kthread());
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002503
Paul E. McKenney15fecf82017-02-08 12:36:42 -08002504 /* Update counts and requeue any remaining callbacks. */
2505 rcu_segcblist_insert_done_cbs(&rdp->cblist, &rcl);
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08002506 smp_mb(); /* List handling before counting for rcu_barrier(). */
Paul E. McKenney15fecf82017-02-08 12:36:42 -08002507 rcu_segcblist_insert_count(&rdp->cblist, &rcl);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002508
2509 /* Reinstate batch limit if we have worked down the excess. */
Paul E. McKenney15fecf82017-02-08 12:36:42 -08002510 count = rcu_segcblist_n_cbs(&rdp->cblist);
2511 if (rdp->blimit == LONG_MAX && count <= qlowmark)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002512 rdp->blimit = blimit;
2513
Paul E. McKenney37c72e52009-10-14 10:15:55 -07002514 /* Reset ->qlen_last_fqs_check trigger if enough CBs have drained. */
Paul E. McKenney15fecf82017-02-08 12:36:42 -08002515 if (count == 0 && rdp->qlen_last_fqs_check != 0) {
Paul E. McKenney37c72e52009-10-14 10:15:55 -07002516 rdp->qlen_last_fqs_check = 0;
Paul E. McKenney3c779df2018-07-05 15:54:02 -07002517 rdp->n_force_qs_snap = rcu_state.n_force_qs;
Paul E. McKenney15fecf82017-02-08 12:36:42 -08002518 } else if (count < rdp->qlen_last_fqs_check - qhimark)
2519 rdp->qlen_last_fqs_check = count;
Paul E. McKenneyefd88b02017-10-19 14:52:41 -07002520
2521 /*
2522 * The following usually indicates a double call_rcu(). To track
2523 * this down, try building with CONFIG_DEBUG_OBJECTS_RCU_HEAD=y.
2524 */
Paul E. McKenney15fecf82017-02-08 12:36:42 -08002525 WARN_ON_ONCE(rcu_segcblist_empty(&rdp->cblist) != (count == 0));
Paul E. McKenney37c72e52009-10-14 10:15:55 -07002526
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002527 local_irq_restore(flags);
2528
Paul E. McKenneye0f23062011-06-21 01:29:39 -07002529 /* Re-invoke RCU core processing if there are callbacks remaining. */
Paul E. McKenney15fecf82017-02-08 12:36:42 -08002530 if (rcu_segcblist_ready_cbs(&rdp->cblist))
Paul E. McKenneya46e0892011-06-15 15:47:09 -07002531 invoke_rcu_core();
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002532}
2533
2534/*
Paul E. McKenneyc98cac62018-11-21 11:35:03 -08002535 * This function is invoked from each scheduling-clock interrupt,
2536 * and checks to see if this CPU is in a non-context-switch quiescent
2537 * state, for example, user mode or idle loop. It also schedules RCU
2538 * core processing. If the current grace period has gone on too long,
2539 * it will ask the scheduler to manufacture a context switch for the sole
2540 * purpose of providing a providing the needed quiescent state.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002541 */
Paul E. McKenneyc98cac62018-11-21 11:35:03 -08002542void rcu_sched_clock_irq(int user)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002543{
Steven Rostedt (Red Hat)f7f7bac2013-07-12 17:18:47 -04002544 trace_rcu_utilization(TPS("Start scheduler-tick"));
Paul E. McKenney4e950202018-07-05 17:59:36 -07002545 raw_cpu_inc(rcu_data.ticks_this_gp);
Paul E. McKenney92aa39e2018-07-09 13:47:30 -07002546 /* The load-acquire pairs with the store-release setting to true. */
Paul E. McKenney2dba13f2018-08-03 21:00:38 -07002547 if (smp_load_acquire(this_cpu_ptr(&rcu_data.rcu_urgent_qs))) {
Paul E. McKenney92aa39e2018-07-09 13:47:30 -07002548 /* Idle and userspace execution already are quiescent states. */
Paul E. McKenneya0ef9ec2018-07-09 15:50:16 -07002549 if (!rcu_is_cpu_rrupt_from_idle() && !user) {
Paul E. McKenney92aa39e2018-07-09 13:47:30 -07002550 set_tsk_need_resched(current);
2551 set_preempt_need_resched();
2552 }
Paul E. McKenney2dba13f2018-08-03 21:00:38 -07002553 __this_cpu_write(rcu_data.rcu_urgent_qs, false);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002554 }
Paul E. McKenneyc98cac62018-11-21 11:35:03 -08002555 rcu_flavor_sched_clock_irq(user);
Paul E. McKenneye3950ec2014-10-21 08:03:57 -07002556 if (rcu_pending())
Paul E. McKenneya46e0892011-06-15 15:47:09 -07002557 invoke_rcu_core();
Byungchul Park07f27572018-05-11 17:30:34 +09002558
Steven Rostedt (Red Hat)f7f7bac2013-07-12 17:18:47 -04002559 trace_rcu_utilization(TPS("End scheduler-tick"));
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002560}
2561
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002562/*
2563 * Scan the leaf rcu_node structures, processing dyntick state for any that
2564 * have not yet encountered a quiescent state, using the function specified.
Paul E. McKenney27f4d282011-02-07 12:47:15 -08002565 * Also initiate boosting for any threads blocked on the root rcu_node.
2566 *
Paul E. McKenneyee47eb92010-01-04 15:09:07 -08002567 * The caller must have suppressed start of new grace periods.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002568 */
Paul E. McKenney8ff0b902018-07-05 17:55:14 -07002569static void force_qs_rnp(int (*f)(struct rcu_data *rdp))
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002570{
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002571 int cpu;
2572 unsigned long flags;
2573 unsigned long mask;
Paul E. McKenneya0b6c9a2009-09-28 07:46:33 -07002574 struct rcu_node *rnp;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002575
Paul E. McKenneyaedf4ba2018-07-04 14:33:59 -07002576 rcu_for_each_leaf_node(rnp) {
Paul E. McKenneycee43932018-03-02 16:35:27 -08002577 cond_resched_tasks_rcu_qs();
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002578 mask = 0;
Peter Zijlstra2a67e742015-10-08 12:24:23 +02002579 raw_spin_lock_irqsave_rcu_node(rnp, flags);
Paul E. McKenneya0b6c9a2009-09-28 07:46:33 -07002580 if (rnp->qsmask == 0) {
Paul E. McKenney45975c72018-07-02 14:30:37 -07002581 if (!IS_ENABLED(CONFIG_PREEMPT) ||
Paul E. McKenneya77da142015-03-08 14:52:27 -07002582 rcu_preempt_blocked_readers_cgp(rnp)) {
2583 /*
2584 * No point in scanning bits because they
2585 * are all zero. But we might need to
2586 * priority-boost blocked readers.
2587 */
2588 rcu_initiate_boost(rnp, flags);
2589 /* rcu_initiate_boost() releases rnp->lock */
2590 continue;
2591 }
Paul E. McKenney92816432018-05-02 11:07:02 -07002592 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
2593 continue;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002594 }
Mark Rutlandbc75e992016-06-03 15:20:04 +01002595 for_each_leaf_node_possible_cpu(rnp, cpu) {
2596 unsigned long bit = leaf_node_cpu_bit(rnp, cpu);
Paul E. McKenney0edd1b12013-06-21 16:37:22 -07002597 if ((rnp->qsmask & bit) != 0) {
Paul E. McKenneyda1df502018-07-03 15:37:16 -07002598 if (f(per_cpu_ptr(&rcu_data, cpu)))
Paul E. McKenney0edd1b12013-06-21 16:37:22 -07002599 mask |= bit;
2600 }
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002601 }
Paul E. McKenney45f014c52e2010-01-04 15:09:08 -08002602 if (mask != 0) {
Paul E. McKenneyc9a24e22018-04-27 14:54:46 -07002603 /* Idle/offline CPUs, report (releases rnp->lock). */
Paul E. McKenneyb50912d2018-07-03 17:22:34 -07002604 rcu_report_qs_rnp(mask, rnp, rnp->gp_seq, flags);
Paul E. McKenney0aa04b02015-01-23 21:52:37 -08002605 } else {
2606 /* Nothing to do here, so just drop the lock. */
Boqun Feng67c583a72015-12-29 12:18:47 +08002607 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002608 }
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002609 }
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002610}
2611
2612/*
2613 * Force quiescent states on reluctant CPUs, and also detect which
2614 * CPUs are in dyntick-idle mode.
2615 */
Paul E. McKenneycd920e52018-11-28 16:57:54 -08002616void rcu_force_quiescent_state(void)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002617{
2618 unsigned long flags;
Paul E. McKenney394f2762012-06-26 17:00:35 -07002619 bool ret;
2620 struct rcu_node *rnp;
2621 struct rcu_node *rnp_old = NULL;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002622
Paul E. McKenney394f2762012-06-26 17:00:35 -07002623 /* Funnel through hierarchy to reduce memory contention. */
Paul E. McKenneyda1df502018-07-03 15:37:16 -07002624 rnp = __this_cpu_read(rcu_data.mynode);
Paul E. McKenney394f2762012-06-26 17:00:35 -07002625 for (; rnp != NULL; rnp = rnp->parent) {
Paul E. McKenney67a0edb2018-07-05 16:15:38 -07002626 ret = (READ_ONCE(rcu_state.gp_flags) & RCU_GP_FLAG_FQS) ||
Paul E. McKenney394f2762012-06-26 17:00:35 -07002627 !raw_spin_trylock(&rnp->fqslock);
2628 if (rnp_old != NULL)
2629 raw_spin_unlock(&rnp_old->fqslock);
Paul E. McKenneyd62df572018-01-10 13:10:49 -08002630 if (ret)
Paul E. McKenney394f2762012-06-26 17:00:35 -07002631 return;
Paul E. McKenney394f2762012-06-26 17:00:35 -07002632 rnp_old = rnp;
2633 }
Paul E. McKenney336a4f62018-07-03 17:22:34 -07002634 /* rnp_old == rcu_get_root(), rnp == NULL. */
Paul E. McKenney394f2762012-06-26 17:00:35 -07002635
2636 /* Reached the root of the rcu_node tree, acquire lock. */
Peter Zijlstra2a67e742015-10-08 12:24:23 +02002637 raw_spin_lock_irqsave_rcu_node(rnp_old, flags);
Paul E. McKenney394f2762012-06-26 17:00:35 -07002638 raw_spin_unlock(&rnp_old->fqslock);
Paul E. McKenney67a0edb2018-07-05 16:15:38 -07002639 if (READ_ONCE(rcu_state.gp_flags) & RCU_GP_FLAG_FQS) {
Boqun Feng67c583a72015-12-29 12:18:47 +08002640 raw_spin_unlock_irqrestore_rcu_node(rnp_old, flags);
Paul E. McKenney4cdfc1752012-06-22 17:06:26 -07002641 return; /* Someone beat us to it. */
Paul E. McKenney46a1e342010-01-04 15:09:09 -08002642 }
Paul E. McKenney67a0edb2018-07-05 16:15:38 -07002643 WRITE_ONCE(rcu_state.gp_flags,
2644 READ_ONCE(rcu_state.gp_flags) | RCU_GP_FLAG_FQS);
Boqun Feng67c583a72015-12-29 12:18:47 +08002645 raw_spin_unlock_irqrestore_rcu_node(rnp_old, flags);
Paul E. McKenney532c00c2018-07-03 17:22:34 -07002646 rcu_gp_kthread_wake();
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002647}
Paul E. McKenneycd920e52018-11-28 16:57:54 -08002648EXPORT_SYMBOL_GPL(rcu_force_quiescent_state);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002649
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002650/*
Paul E. McKenney26d950a2018-04-21 20:44:11 -07002651 * This function checks for grace-period requests that fail to motivate
2652 * RCU to come out of its idle mode.
2653 */
Paul E. McKenney791416c2018-10-01 15:42:44 -07002654void
2655rcu_check_gp_start_stall(struct rcu_node *rnp, struct rcu_data *rdp,
2656 const unsigned long gpssdelay)
Paul E. McKenney26d950a2018-04-21 20:44:11 -07002657{
2658 unsigned long flags;
2659 unsigned long j;
Paul E. McKenney336a4f62018-07-03 17:22:34 -07002660 struct rcu_node *rnp_root = rcu_get_root();
Paul E. McKenney26d950a2018-04-21 20:44:11 -07002661 static atomic_t warned = ATOMIC_INIT(0);
2662
Paul E. McKenneyde8e8732018-07-03 17:22:34 -07002663 if (!IS_ENABLED(CONFIG_PROVE_RCU) || rcu_gp_in_progress() ||
Paul E. McKenney7a1d0f22018-05-01 10:26:57 -07002664 ULONG_CMP_GE(rnp_root->gp_seq, rnp_root->gp_seq_needed))
Paul E. McKenney26d950a2018-04-21 20:44:11 -07002665 return;
2666 j = jiffies; /* Expensive access, and in common case don't get here. */
Paul E. McKenney67a0edb2018-07-05 16:15:38 -07002667 if (time_before(j, READ_ONCE(rcu_state.gp_req_activity) + gpssdelay) ||
2668 time_before(j, READ_ONCE(rcu_state.gp_activity) + gpssdelay) ||
Paul E. McKenney26d950a2018-04-21 20:44:11 -07002669 atomic_read(&warned))
2670 return;
2671
2672 raw_spin_lock_irqsave_rcu_node(rnp, flags);
2673 j = jiffies;
Paul E. McKenneyde8e8732018-07-03 17:22:34 -07002674 if (rcu_gp_in_progress() ||
Paul E. McKenney7a1d0f22018-05-01 10:26:57 -07002675 ULONG_CMP_GE(rnp_root->gp_seq, rnp_root->gp_seq_needed) ||
Paul E. McKenney67a0edb2018-07-05 16:15:38 -07002676 time_before(j, READ_ONCE(rcu_state.gp_req_activity) + gpssdelay) ||
2677 time_before(j, READ_ONCE(rcu_state.gp_activity) + gpssdelay) ||
Paul E. McKenney26d950a2018-04-21 20:44:11 -07002678 atomic_read(&warned)) {
2679 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
2680 return;
2681 }
2682 /* Hold onto the leaf lock to make others see warned==1. */
2683
2684 if (rnp_root != rnp)
2685 raw_spin_lock_rcu_node(rnp_root); /* irqs already disabled. */
2686 j = jiffies;
Paul E. McKenneyde8e8732018-07-03 17:22:34 -07002687 if (rcu_gp_in_progress() ||
Paul E. McKenney7a1d0f22018-05-01 10:26:57 -07002688 ULONG_CMP_GE(rnp_root->gp_seq, rnp_root->gp_seq_needed) ||
Paul E. McKenney67a0edb2018-07-05 16:15:38 -07002689 time_before(j, rcu_state.gp_req_activity + gpssdelay) ||
2690 time_before(j, rcu_state.gp_activity + gpssdelay) ||
Paul E. McKenney26d950a2018-04-21 20:44:11 -07002691 atomic_xchg(&warned, 1)) {
2692 raw_spin_unlock_rcu_node(rnp_root); /* irqs remain disabled. */
2693 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
2694 return;
2695 }
Paul E. McKenney26d950a2018-04-21 20:44:11 -07002696 WARN_ON(1);
2697 if (rnp_root != rnp)
2698 raw_spin_unlock_rcu_node(rnp_root);
2699 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
Paul E. McKenneyfd897572018-12-10 16:09:49 -08002700 show_rcu_gp_kthreads();
Paul E. McKenney26d950a2018-04-21 20:44:11 -07002701}
2702
2703/*
Paul E. McKenneye0aff972018-10-01 17:40:54 -07002704 * Do a forward-progress check for rcutorture. This is normally invoked
2705 * due to an OOM event. The argument "j" gives the time period during
2706 * which rcutorture would like progress to have been made.
2707 */
2708void rcu_fwd_progress_check(unsigned long j)
2709{
Paul E. McKenneybfcfcff2018-10-02 17:09:56 -07002710 unsigned long cbs;
2711 int cpu;
2712 unsigned long max_cbs = 0;
2713 int max_cpu = -1;
Paul E. McKenneye0aff972018-10-01 17:40:54 -07002714 struct rcu_data *rdp;
2715
2716 if (rcu_gp_in_progress()) {
Paul E. McKenney8dd3b542018-10-03 11:02:05 -07002717 pr_info("%s: GP age %lu jiffies\n",
2718 __func__, jiffies - rcu_state.gp_start);
Paul E. McKenneye0aff972018-10-01 17:40:54 -07002719 show_rcu_gp_kthreads();
2720 } else {
Paul E. McKenneyc51d7b52018-10-03 17:25:33 -07002721 pr_info("%s: Last GP end %lu jiffies ago\n",
2722 __func__, jiffies - rcu_state.gp_end);
Paul E. McKenneye0aff972018-10-01 17:40:54 -07002723 preempt_disable();
2724 rdp = this_cpu_ptr(&rcu_data);
2725 rcu_check_gp_start_stall(rdp->mynode, rdp, j);
2726 preempt_enable();
2727 }
Paul E. McKenneybfcfcff2018-10-02 17:09:56 -07002728 for_each_possible_cpu(cpu) {
2729 cbs = rcu_get_n_cbs_cpu(cpu);
2730 if (!cbs)
2731 continue;
2732 if (max_cpu < 0)
2733 pr_info("%s: callbacks", __func__);
2734 pr_cont(" %d: %lu", cpu, cbs);
2735 if (cbs <= max_cbs)
2736 continue;
2737 max_cbs = cbs;
2738 max_cpu = cpu;
2739 }
2740 if (max_cpu >= 0)
2741 pr_cont("\n");
Paul E. McKenneye0aff972018-10-01 17:40:54 -07002742}
2743EXPORT_SYMBOL_GPL(rcu_fwd_progress_check);
2744
2745/*
Paul E. McKenneyb049fdf2018-07-03 17:22:34 -07002746 * This does the RCU core processing work for the specified rcu_data
2747 * structures. This may be called only from the CPU to whom the rdp
2748 * belongs.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002749 */
Emese Revfy0766f782016-06-20 20:42:34 +02002750static __latent_entropy void rcu_process_callbacks(struct softirq_action *unused)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002751{
Shaohua Li09223372011-06-14 13:26:25 +08002752 unsigned long flags;
Paul E. McKenneyda1df502018-07-03 15:37:16 -07002753 struct rcu_data *rdp = raw_cpu_ptr(&rcu_data);
Paul E. McKenneyb0f74032013-02-04 12:14:24 -08002754 struct rcu_node *rnp = rdp->mynode;
Paul E. McKenneya26ac242011-01-12 14:10:23 -08002755
2756 if (cpu_is_offline(smp_processor_id()))
Peter Zijlstra08bca602011-05-20 16:06:29 -07002757 return;
Paul E. McKenneya26ac242011-01-12 14:10:23 -08002758 trace_rcu_utilization(TPS("Start RCU core"));
Shaohua Li09223372011-06-14 13:26:25 +08002759 WARN_ON_ONCE(!rdp->beenonline);
Paul E. McKenney29154c52012-05-30 03:21:48 -07002760
Paul E. McKenney3e310092018-06-21 12:50:01 -07002761 /* Report any deferred quiescent states if preemption enabled. */
Paul E. McKenneyfced9c82018-07-26 13:44:00 -07002762 if (!(preempt_count() & PREEMPT_MASK)) {
Paul E. McKenney3e310092018-06-21 12:50:01 -07002763 rcu_preempt_deferred_qs(current);
Paul E. McKenneyfced9c82018-07-26 13:44:00 -07002764 } else if (rcu_preempt_need_deferred_qs(current)) {
2765 set_tsk_need_resched(current);
2766 set_preempt_need_resched();
2767 }
Paul E. McKenney3e310092018-06-21 12:50:01 -07002768
Paul E. McKenney29154c52012-05-30 03:21:48 -07002769 /* Update RCU state based on any recent quiescent states. */
Paul E. McKenney8087d3e2018-07-03 17:22:34 -07002770 rcu_check_quiescent_state(rdp);
Paul E. McKenney29154c52012-05-30 03:21:48 -07002771
2772 /* No grace period and unregistered callbacks? */
Paul E. McKenneyde8e8732018-07-03 17:22:34 -07002773 if (!rcu_gp_in_progress() &&
Paul E. McKenney48a76392014-03-11 13:02:16 -07002774 rcu_segcblist_is_enabled(&rdp->cblist)) {
2775 local_irq_save(flags);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002776 if (!rcu_segcblist_restempty(&rdp->cblist, RCU_NEXT_READY_TAIL))
Paul E. McKenneyc6e09b92018-07-03 17:22:34 -07002777 rcu_accelerate_cbs_unlocked(rnp, rdp);
Paul E. McKenney29154c52012-05-30 03:21:48 -07002778 local_irq_restore(flags);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002779 }
Yao Dongdong9910aff2015-02-25 17:09:46 +08002780
Paul E. McKenney791416c2018-10-01 15:42:44 -07002781 rcu_check_gp_start_stall(rnp, rdp, rcu_jiffies_till_stall_check());
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002782
Paul E. McKenney29154c52012-05-30 03:21:48 -07002783 /* If there are callbacks ready, invoke them. */
2784 if (rcu_segcblist_ready_cbs(&rdp->cblist))
Paul E. McKenneyaff4e9e2018-07-03 17:22:34 -07002785 invoke_rcu_callbacks(rdp);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002786
2787 /* Do any needed deferred wakeups of rcuo kthreads. */
2788 do_nocb_deferred_wakeup(rdp);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002789 trace_rcu_utilization(TPS("End RCU core"));
2790}
2791
2792/*
Paul E. McKenney49918a52018-07-07 18:12:26 -07002793 * Schedule RCU callback invocation. If the running implementation of RCU
2794 * does not support RCU priority boosting, just do a direct call, otherwise
2795 * wake up the per-CPU kernel kthread. Note that because we are running
2796 * on the current CPU with softirqs disabled, the rcu_cpu_kthread_task
2797 * cannot disappear out from under us.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002798 */
Paul E. McKenneyaff4e9e2018-07-03 17:22:34 -07002799static void invoke_rcu_callbacks(struct rcu_data *rdp)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002800{
2801 if (unlikely(!READ_ONCE(rcu_scheduler_fully_active)))
Paul E. McKenney2655d572011-04-07 22:47:23 -07002802 return;
Paul E. McKenney3c779df2018-07-05 15:54:02 -07002803 if (likely(!rcu_state.boost)) {
Paul E. McKenney5bb5d092018-07-03 17:22:34 -07002804 rcu_do_batch(rdp);
Paul E. McKenneyc57afe82012-02-28 11:02:21 -08002805 return;
2806 }
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08002807 invoke_rcu_callbacks_kthread();
2808}
2809
Paul E. McKenney2655d572011-04-07 22:47:23 -07002810static void invoke_rcu_core(void)
Paul E. McKenneyd4c08f22011-06-25 06:36:56 -07002811{
2812 if (cpu_online(smp_processor_id()))
Paul E. McKenney486e2592012-01-06 14:11:30 -08002813 raise_softirq(RCU_SOFTIRQ);
Paul E. McKenneyd4c08f22011-06-25 06:36:56 -07002814}
Paul E. McKenney486e2592012-01-06 14:11:30 -08002815
Paul E. McKenneyd4c08f22011-06-25 06:36:56 -07002816/*
Paul E. McKenney2655d572011-04-07 22:47:23 -07002817 * Handle any core-RCU processing required by a call_rcu() invocation.
2818 */
Paul E. McKenney5c7d8962018-07-03 17:22:34 -07002819static void __call_rcu_core(struct rcu_data *rdp, struct rcu_head *head,
2820 unsigned long flags)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002821{
Paul E. McKenney37c72e52009-10-14 10:15:55 -07002822 /*
2823 * If called from an extended quiescent state, invoke the RCU
2824 * core in order to force a re-evaluation of RCU's idleness.
2825 */
2826 if (!rcu_is_watching())
Paul E. McKenney2655d572011-04-07 22:47:23 -07002827 invoke_rcu_core();
Paul E. McKenneyb52573d2010-12-14 17:36:02 -08002828
2829 /* If interrupts were disabled or CPU offline, don't invoke RCU core. */
Paul E. McKenney470716f2013-03-19 11:32:11 -07002830 if (irqs_disabled_flags(flags) || cpu_is_offline(smp_processor_id()))
Paul E. McKenneyb52573d2010-12-14 17:36:02 -08002831 return;
2832
2833 /*
Paul E. McKenneyb52573d2010-12-14 17:36:02 -08002834 * Force the grace period if too many callbacks or too long waiting.
Paul E. McKenneycd920e52018-11-28 16:57:54 -08002835 * Enforce hysteresis, and don't invoke rcu_force_quiescent_state()
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002836 * if some other CPU has recently done so. Also, don't bother
Paul E. McKenneycd920e52018-11-28 16:57:54 -08002837 * invoking rcu_force_quiescent_state() if the newly enqueued callback
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002838 * is the only one waiting for a grace period to complete.
2839 */
Paul E. McKenney15fecf82017-02-08 12:36:42 -08002840 if (unlikely(rcu_segcblist_n_cbs(&rdp->cblist) >
2841 rdp->qlen_last_fqs_check + qhimark)) {
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002842
2843 /* Are we ignoring a completed grace period? */
Paul E. McKenney15cabdf2018-07-03 17:22:34 -07002844 note_gp_changes(rdp);
Paul E. McKenneyb52573d2010-12-14 17:36:02 -08002845
2846 /* Start a new grace period if one not already started. */
Paul E. McKenneyde8e8732018-07-03 17:22:34 -07002847 if (!rcu_gp_in_progress()) {
Paul E. McKenneyc6e09b92018-07-03 17:22:34 -07002848 rcu_accelerate_cbs_unlocked(rdp->mynode, rdp);
Paul E. McKenneyb52573d2010-12-14 17:36:02 -08002849 } else {
2850 /* Give the grace period a kick. */
2851 rdp->blimit = LONG_MAX;
Paul E. McKenney5c7d8962018-07-03 17:22:34 -07002852 if (rcu_state.n_force_qs == rdp->n_force_qs_snap &&
Paul E. McKenney15fecf82017-02-08 12:36:42 -08002853 rcu_segcblist_first_pend_cb(&rdp->cblist) != head)
Paul E. McKenneycd920e52018-11-28 16:57:54 -08002854 rcu_force_quiescent_state();
Paul E. McKenney5c7d8962018-07-03 17:22:34 -07002855 rdp->n_force_qs_snap = rcu_state.n_force_qs;
Paul E. McKenney15fecf82017-02-08 12:36:42 -08002856 rdp->qlen_last_fqs_check = rcu_segcblist_n_cbs(&rdp->cblist);
Paul E. McKenneyb52573d2010-12-14 17:36:02 -08002857 }
Paul E. McKenney4cdfc1752012-06-22 17:06:26 -07002858 }
Paul E. McKenney29154c52012-05-30 03:21:48 -07002859}
2860
Paul E. McKenney3fbfbf72012-08-19 21:35:53 -07002861/*
Paul E. McKenneyae150182013-04-23 13:20:57 -07002862 * RCU callback function to leak a callback.
2863 */
2864static void rcu_leak_callback(struct rcu_head *rhp)
2865{
2866}
2867
2868/*
Paul E. McKenney3fbfbf72012-08-19 21:35:53 -07002869 * Helper function for call_rcu() and friends. The cpu argument will
2870 * normally be -1, indicating "currently running CPU". It may specify
Paul E. McKenneydd46a782018-07-10 18:37:30 -07002871 * a CPU only if that CPU is a no-CBs CPU. Currently, only rcu_barrier()
Paul E. McKenney3fbfbf72012-08-19 21:35:53 -07002872 * is expected to specify a CPU.
2873 */
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002874static void
Paul E. McKenney5c7d8962018-07-03 17:22:34 -07002875__call_rcu(struct rcu_head *head, rcu_callback_t func, int cpu, bool lazy)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002876{
2877 unsigned long flags;
2878 struct rcu_data *rdp;
2879
Paul E. McKenneyb8f2ed52016-08-23 06:51:47 -07002880 /* Misaligned rcu_head! */
2881 WARN_ON_ONCE((unsigned long)head & (sizeof(void *) - 1));
2882
Paul E. McKenneyae150182013-04-23 13:20:57 -07002883 if (debug_rcu_head_queue(head)) {
Paul E. McKenneyfa3c6642017-05-03 11:38:55 -07002884 /*
2885 * Probable double call_rcu(), so leak the callback.
2886 * Use rcu:rcu_callback trace event to find the previous
2887 * time callback was passed to __call_rcu().
2888 */
2889 WARN_ONCE(1, "__call_rcu(): Double-freed CB %p->%pF()!!!\n",
2890 head, head->func);
Paul E. McKenney7d0ae802015-03-03 14:57:58 -08002891 WRITE_ONCE(head->func, rcu_leak_callback);
Paul E. McKenneyae150182013-04-23 13:20:57 -07002892 return;
2893 }
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002894 head->func = func;
2895 head->next = NULL;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002896 local_irq_save(flags);
Paul E. McKenneyda1df502018-07-03 15:37:16 -07002897 rdp = this_cpu_ptr(&rcu_data);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002898
2899 /* Add the callback to our list. */
Paul E. McKenney15fecf82017-02-08 12:36:42 -08002900 if (unlikely(!rcu_segcblist_is_enabled(&rdp->cblist)) || cpu != -1) {
Paul E. McKenney3fbfbf72012-08-19 21:35:53 -07002901 int offline;
2902
2903 if (cpu != -1)
Paul E. McKenneyda1df502018-07-03 15:37:16 -07002904 rdp = per_cpu_ptr(&rcu_data, cpu);
Paul E. McKenney143da9c2015-01-19 19:57:32 -08002905 if (likely(rdp->mynode)) {
2906 /* Post-boot, so this should be for a no-CBs CPU. */
2907 offline = !__call_rcu_nocb(rdp, head, lazy, flags);
2908 WARN_ON_ONCE(offline);
2909 /* Offline CPU, _call_rcu() illegal, leak callback. */
2910 local_irq_restore(flags);
2911 return;
2912 }
2913 /*
2914 * Very early boot, before rcu_init(). Initialize if needed
2915 * and then drop through to queue the callback.
2916 */
Paul E. McKenney08543bd2018-10-22 08:04:03 -07002917 WARN_ON_ONCE(cpu != -1);
Paul E. McKenney34404ca2015-01-19 20:39:20 -08002918 WARN_ON_ONCE(!rcu_is_watching());
Paul E. McKenney15fecf82017-02-08 12:36:42 -08002919 if (rcu_segcblist_empty(&rdp->cblist))
2920 rcu_segcblist_init(&rdp->cblist);
Paul E. McKenney0d8ee372012-08-03 13:16:15 -07002921 }
Paul E. McKenney15fecf82017-02-08 12:36:42 -08002922 rcu_segcblist_enqueue(&rdp->cblist, head, lazy);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002923 if (__is_kfree_rcu_offset((unsigned long)func))
Paul E. McKenney3c779df2018-07-05 15:54:02 -07002924 trace_rcu_kfree_callback(rcu_state.name, head,
2925 (unsigned long)func,
Paul E. McKenney15fecf82017-02-08 12:36:42 -08002926 rcu_segcblist_n_lazy_cbs(&rdp->cblist),
2927 rcu_segcblist_n_cbs(&rdp->cblist));
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002928 else
Paul E. McKenney3c779df2018-07-05 15:54:02 -07002929 trace_rcu_callback(rcu_state.name, head,
Paul E. McKenney15fecf82017-02-08 12:36:42 -08002930 rcu_segcblist_n_lazy_cbs(&rdp->cblist),
2931 rcu_segcblist_n_cbs(&rdp->cblist));
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002932
Paul E. McKenney29154c52012-05-30 03:21:48 -07002933 /* Go handle any RCU core processing required. */
Paul E. McKenney5c7d8962018-07-03 17:22:34 -07002934 __call_rcu_core(rdp, head, flags);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002935 local_irq_restore(flags);
2936}
2937
Paul E. McKenneya68a2bb2017-05-03 08:34:57 -07002938/**
Paul E. McKenney45975c72018-07-02 14:30:37 -07002939 * call_rcu() - Queue an RCU callback for invocation after a grace period.
Paul E. McKenneya68a2bb2017-05-03 08:34:57 -07002940 * @head: structure to be used for queueing the RCU updates.
2941 * @func: actual callback function to be invoked after the grace period
2942 *
2943 * The callback function will be invoked some time after a full grace
Paul E. McKenney45975c72018-07-02 14:30:37 -07002944 * period elapses, in other words after all pre-existing RCU read-side
2945 * critical sections have completed. However, the callback function
2946 * might well execute concurrently with RCU read-side critical sections
2947 * that started after call_rcu() was invoked. RCU read-side critical
2948 * sections are delimited by rcu_read_lock() and rcu_read_unlock(), and
2949 * may be nested. In addition, regions of code across which interrupts,
2950 * preemption, or softirqs have been disabled also serve as RCU read-side
2951 * critical sections. This includes hardware interrupt handlers, softirq
2952 * handlers, and NMI handlers.
Paul E. McKenney27fdb352017-10-19 14:26:21 -07002953 *
Paul E. McKenney45975c72018-07-02 14:30:37 -07002954 * Note that all CPUs must agree that the grace period extended beyond
2955 * all pre-existing RCU read-side critical section. On systems with more
2956 * than one CPU, this means that when "func()" is invoked, each CPU is
2957 * guaranteed to have executed a full memory barrier since the end of its
2958 * last RCU read-side critical section whose beginning preceded the call
2959 * to call_rcu(). It also means that each CPU executing an RCU read-side
2960 * critical section that continues beyond the start of "func()" must have
2961 * executed a memory barrier after the call_rcu() but before the beginning
2962 * of that RCU read-side critical section. Note that these guarantees
2963 * include CPUs that are offline, idle, or executing in user mode, as
2964 * well as CPUs that are executing in the kernel.
Paul E. McKenneya68a2bb2017-05-03 08:34:57 -07002965 *
Paul E. McKenney45975c72018-07-02 14:30:37 -07002966 * Furthermore, if CPU A invoked call_rcu() and CPU B invoked the
2967 * resulting RCU callback function "func()", then both CPU A and CPU B are
2968 * guaranteed to execute a full memory barrier during the time interval
2969 * between the call to call_rcu() and the invocation of "func()" -- even
2970 * if CPU A and CPU B are the same CPU (but again only if the system has
2971 * more than one CPU).
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002972 */
Paul E. McKenney45975c72018-07-02 14:30:37 -07002973void call_rcu(struct rcu_head *head, rcu_callback_t func)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002974{
Paul E. McKenney5c7d8962018-07-03 17:22:34 -07002975 __call_rcu(head, func, -1, 0);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002976}
Paul E. McKenney45975c72018-07-02 14:30:37 -07002977EXPORT_SYMBOL_GPL(call_rcu);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002978
Paul E. McKenney6d813392012-02-23 13:30:16 -08002979/*
Andreea-Cristina Bernat495aa962014-03-18 20:48:48 +02002980 * Queue an RCU callback for lazy invocation after a grace period.
2981 * This will likely be later named something like "call_rcu_lazy()",
2982 * but this change will require some way of tagging the lazy RCU
2983 * callbacks in the list of pending callbacks. Until then, this
2984 * function may only be called from __kfree_rcu().
2985 */
Paul E. McKenney98ece502018-07-03 17:22:34 -07002986void kfree_call_rcu(struct rcu_head *head, rcu_callback_t func)
Andreea-Cristina Bernat495aa962014-03-18 20:48:48 +02002987{
Paul E. McKenney5c7d8962018-07-03 17:22:34 -07002988 __call_rcu(head, func, -1, 1);
Andreea-Cristina Bernat495aa962014-03-18 20:48:48 +02002989}
2990EXPORT_SYMBOL_GPL(kfree_call_rcu);
2991
Paul E. McKenneye5bc3af2018-11-29 10:42:06 -08002992/*
2993 * During early boot, any blocking grace-period wait automatically
2994 * implies a grace period. Later on, this is never the case for PREEMPT.
2995 *
2996 * Howevr, because a context switch is a grace period for !PREEMPT, any
2997 * blocking grace-period wait automatically implies a grace period if
2998 * there is only one CPU online at any point time during execution of
2999 * either synchronize_rcu() or synchronize_rcu_expedited(). It is OK to
3000 * occasionally incorrectly indicate that there are multiple CPUs online
3001 * when there was in fact only one the whole time, as this just adds some
3002 * overhead: RCU still operates correctly.
3003 */
3004static int rcu_blocking_is_gp(void)
3005{
3006 int ret;
3007
3008 if (IS_ENABLED(CONFIG_PREEMPT))
3009 return rcu_scheduler_active == RCU_SCHEDULER_INACTIVE;
3010 might_sleep(); /* Check for RCU read-side critical section. */
3011 preempt_disable();
3012 ret = num_online_cpus() <= 1;
3013 preempt_enable();
3014 return ret;
3015}
3016
3017/**
3018 * synchronize_rcu - wait until a grace period has elapsed.
3019 *
3020 * Control will return to the caller some time after a full grace
3021 * period has elapsed, in other words after all currently executing RCU
3022 * read-side critical sections have completed. Note, however, that
3023 * upon return from synchronize_rcu(), the caller might well be executing
3024 * concurrently with new RCU read-side critical sections that began while
3025 * synchronize_rcu() was waiting. RCU read-side critical sections are
3026 * delimited by rcu_read_lock() and rcu_read_unlock(), and may be nested.
3027 * In addition, regions of code across which interrupts, preemption, or
3028 * softirqs have been disabled also serve as RCU read-side critical
3029 * sections. This includes hardware interrupt handlers, softirq handlers,
3030 * and NMI handlers.
3031 *
3032 * Note that this guarantee implies further memory-ordering guarantees.
3033 * On systems with more than one CPU, when synchronize_rcu() returns,
3034 * each CPU is guaranteed to have executed a full memory barrier since
3035 * the end of its last RCU read-side critical section whose beginning
3036 * preceded the call to synchronize_rcu(). In addition, each CPU having
3037 * an RCU read-side critical section that extends beyond the return from
3038 * synchronize_rcu() is guaranteed to have executed a full memory barrier
3039 * after the beginning of synchronize_rcu() and before the beginning of
3040 * that RCU read-side critical section. Note that these guarantees include
3041 * CPUs that are offline, idle, or executing in user mode, as well as CPUs
3042 * that are executing in the kernel.
3043 *
3044 * Furthermore, if CPU A invoked synchronize_rcu(), which returned
3045 * to its caller on CPU B, then both CPU A and CPU B are guaranteed
3046 * to have executed a full memory barrier during the execution of
3047 * synchronize_rcu() -- even if CPU A and CPU B are the same CPU (but
3048 * again only if the system has more than one CPU).
3049 */
3050void synchronize_rcu(void)
3051{
3052 RCU_LOCKDEP_WARN(lock_is_held(&rcu_bh_lock_map) ||
3053 lock_is_held(&rcu_lock_map) ||
3054 lock_is_held(&rcu_sched_lock_map),
3055 "Illegal synchronize_rcu() in RCU read-side critical section");
3056 if (rcu_blocking_is_gp())
3057 return;
3058 if (rcu_gp_is_expedited())
3059 synchronize_rcu_expedited();
3060 else
3061 wait_rcu_gp(call_rcu);
3062}
3063EXPORT_SYMBOL_GPL(synchronize_rcu);
3064
Paul E. McKenney765a3f42014-03-14 16:37:08 -07003065/**
3066 * get_state_synchronize_rcu - Snapshot current RCU state
3067 *
3068 * Returns a cookie that is used by a later call to cond_synchronize_rcu()
3069 * to determine whether or not a full grace period has elapsed in the
3070 * meantime.
3071 */
3072unsigned long get_state_synchronize_rcu(void)
3073{
3074 /*
3075 * Any prior manipulation of RCU-protected data must happen
Paul E. McKenneye4be81a2018-04-27 15:16:50 -07003076 * before the load from ->gp_seq.
Paul E. McKenney765a3f42014-03-14 16:37:08 -07003077 */
3078 smp_mb(); /* ^^^ */
Paul E. McKenney16fc9c62018-07-03 15:54:39 -07003079 return rcu_seq_snap(&rcu_state.gp_seq);
Paul E. McKenney765a3f42014-03-14 16:37:08 -07003080}
3081EXPORT_SYMBOL_GPL(get_state_synchronize_rcu);
3082
3083/**
3084 * cond_synchronize_rcu - Conditionally wait for an RCU grace period
3085 *
3086 * @oldstate: return value from earlier call to get_state_synchronize_rcu()
3087 *
3088 * If a full RCU grace period has elapsed since the earlier call to
3089 * get_state_synchronize_rcu(), just return. Otherwise, invoke
3090 * synchronize_rcu() to wait for a full grace period.
3091 *
3092 * Yes, this function does not take counter wrap into account. But
3093 * counter wrap is harmless. If the counter wraps, we have waited for
3094 * more than 2 billion grace periods (and way more on a 64-bit system!),
3095 * so waiting for one additional grace period should be just fine.
3096 */
3097void cond_synchronize_rcu(unsigned long oldstate)
3098{
Paul E. McKenney16fc9c62018-07-03 15:54:39 -07003099 if (!rcu_seq_done(&rcu_state.gp_seq, oldstate))
Paul E. McKenney765a3f42014-03-14 16:37:08 -07003100 synchronize_rcu();
Paul E. McKenneye4be81a2018-04-27 15:16:50 -07003101 else
3102 smp_mb(); /* Ensure GP ends before subsequent accesses. */
Paul E. McKenney765a3f42014-03-14 16:37:08 -07003103}
3104EXPORT_SYMBOL_GPL(cond_synchronize_rcu);
3105
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003106/*
Paul E. McKenney98ece502018-07-03 17:22:34 -07003107 * Check to see if there is any immediate RCU-related work to be done by
Paul E. McKenney49918a52018-07-07 18:12:26 -07003108 * the current CPU, returning 1 if so and zero otherwise. The checks are
3109 * in order of increasing expense: checks that can be carried out against
3110 * CPU-local state are performed first. However, we must check for CPU
3111 * stalls first, else we might not get a chance.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003112 */
Paul E. McKenney98ece502018-07-03 17:22:34 -07003113static int rcu_pending(void)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003114{
Paul E. McKenney98ece502018-07-03 17:22:34 -07003115 struct rcu_data *rdp = this_cpu_ptr(&rcu_data);
Paul E. McKenney2f51f982009-11-13 19:51:39 -08003116 struct rcu_node *rnp = rdp->mynode;
3117
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003118 /* Check for CPU stalls, if enabled. */
Paul E. McKenneyea12ff22018-07-03 17:22:34 -07003119 check_cpu_stall(rdp);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003120
Paul E. McKenneya0969322013-11-08 09:03:10 -08003121 /* Is this CPU a NO_HZ_FULL CPU that should ignore RCU? */
Paul E. McKenney4580b052018-07-03 17:22:34 -07003122 if (rcu_nohz_full_cpu())
Paul E. McKenneya0969322013-11-08 09:03:10 -08003123 return 0;
3124
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003125 /* Is the RCU core waiting for a quiescent state from this CPU? */
Paul E. McKenney01c495f2018-01-10 12:36:00 -08003126 if (rdp->core_needs_qs && !rdp->cpu_no_qs.b.norm)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003127 return 1;
3128
3129 /* Does this CPU have callbacks ready to invoke? */
Paul E. McKenney01c495f2018-01-10 12:36:00 -08003130 if (rcu_segcblist_ready_cbs(&rdp->cblist))
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003131 return 1;
3132
3133 /* Has RCU gone idle with this CPU needing another grace period? */
Paul E. McKenneyde8e8732018-07-03 17:22:34 -07003134 if (!rcu_gp_in_progress() &&
Paul E. McKenneyc1935202018-04-12 16:29:13 -07003135 rcu_segcblist_is_enabled(&rdp->cblist) &&
3136 !rcu_segcblist_restempty(&rdp->cblist, RCU_NEXT_READY_TAIL))
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003137 return 1;
3138
Paul E. McKenney67e14c12018-04-27 16:01:46 -07003139 /* Have RCU grace period completed or started? */
3140 if (rcu_seq_current(&rnp->gp_seq) != rdp->gp_seq ||
Paul E. McKenney01c495f2018-01-10 12:36:00 -08003141 unlikely(READ_ONCE(rdp->gpwrap))) /* outside lock */
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003142 return 1;
3143
Paul E. McKenney96d3fd02013-10-04 14:33:34 -07003144 /* Does this CPU need a deferred NOCB wakeup? */
Paul E. McKenney01c495f2018-01-10 12:36:00 -08003145 if (rcu_nocb_need_deferred_wakeup(rdp))
Paul E. McKenney96d3fd02013-10-04 14:33:34 -07003146 return 1;
Paul E. McKenney96d3fd02013-10-04 14:33:34 -07003147
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003148 /* nothing to do */
3149 return 0;
3150}
3151
3152/*
Paul E. McKenneydd46a782018-07-10 18:37:30 -07003153 * Helper function for rcu_barrier() tracing. If tracing is disabled,
Paul E. McKenneya83eff02012-05-23 18:47:05 -07003154 * the compiler is expected to optimize this away.
3155 */
Paul E. McKenneydd46a782018-07-10 18:37:30 -07003156static void rcu_barrier_trace(const char *s, int cpu, unsigned long done)
Paul E. McKenneya83eff02012-05-23 18:47:05 -07003157{
Paul E. McKenney8344b872018-07-03 17:22:34 -07003158 trace_rcu_barrier(rcu_state.name, s, cpu,
3159 atomic_read(&rcu_state.barrier_cpu_count), done);
Paul E. McKenneya83eff02012-05-23 18:47:05 -07003160}
3161
3162/*
Paul E. McKenneydd46a782018-07-10 18:37:30 -07003163 * RCU callback function for rcu_barrier(). If we are last, wake
3164 * up the task executing rcu_barrier().
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08003165 */
Paul E. McKenney24ebbca2012-05-29 00:34:56 -07003166static void rcu_barrier_callback(struct rcu_head *rhp)
Paul E. McKenneyd0ec7742009-10-06 21:48:16 -07003167{
Paul E. McKenneyec9f5832018-07-05 16:26:12 -07003168 if (atomic_dec_and_test(&rcu_state.barrier_cpu_count)) {
Paul E. McKenneydd46a782018-07-10 18:37:30 -07003169 rcu_barrier_trace(TPS("LastCB"), -1,
Paul E. McKenneyec9f5832018-07-05 16:26:12 -07003170 rcu_state.barrier_sequence);
3171 complete(&rcu_state.barrier_completion);
Paul E. McKenneya83eff02012-05-23 18:47:05 -07003172 } else {
Paul E. McKenneydd46a782018-07-10 18:37:30 -07003173 rcu_barrier_trace(TPS("CB"), -1, rcu_state.barrier_sequence);
Paul E. McKenneya83eff02012-05-23 18:47:05 -07003174 }
Paul E. McKenneyd0ec7742009-10-06 21:48:16 -07003175}
3176
3177/*
3178 * Called with preemption disabled, and from cross-cpu IRQ context.
3179 */
Paul E. McKenneyec9f5832018-07-05 16:26:12 -07003180static void rcu_barrier_func(void *unused)
Paul E. McKenneyd0ec7742009-10-06 21:48:16 -07003181{
Paul E. McKenneyda1df502018-07-03 15:37:16 -07003182 struct rcu_data *rdp = raw_cpu_ptr(&rcu_data);
Paul E. McKenneyd0ec7742009-10-06 21:48:16 -07003183
Paul E. McKenneydd46a782018-07-10 18:37:30 -07003184 rcu_barrier_trace(TPS("IRQ"), -1, rcu_state.barrier_sequence);
Paul E. McKenneyf92c7342017-04-10 15:40:35 -07003185 rdp->barrier_head.func = rcu_barrier_callback;
3186 debug_rcu_head_queue(&rdp->barrier_head);
3187 if (rcu_segcblist_entrain(&rdp->cblist, &rdp->barrier_head, 0)) {
Paul E. McKenneyec9f5832018-07-05 16:26:12 -07003188 atomic_inc(&rcu_state.barrier_cpu_count);
Paul E. McKenneyf92c7342017-04-10 15:40:35 -07003189 } else {
3190 debug_rcu_head_unqueue(&rdp->barrier_head);
Paul E. McKenneydd46a782018-07-10 18:37:30 -07003191 rcu_barrier_trace(TPS("IRQNQ"), -1,
Paul E. McKenneyec9f5832018-07-05 16:26:12 -07003192 rcu_state.barrier_sequence);
Paul E. McKenneyf92c7342017-04-10 15:40:35 -07003193 }
Paul E. McKenneyd0ec7742009-10-06 21:48:16 -07003194}
3195
Paul E. McKenneydd46a782018-07-10 18:37:30 -07003196/**
3197 * rcu_barrier - Wait until all in-flight call_rcu() callbacks complete.
3198 *
3199 * Note that this primitive does not necessarily wait for an RCU grace period
3200 * to complete. For example, if there are no RCU callbacks queued anywhere
3201 * in the system, then rcu_barrier() is within its rights to return
3202 * immediately, without waiting for anything, much less an RCU grace period.
Paul E. McKenneyd0ec7742009-10-06 21:48:16 -07003203 */
Paul E. McKenneydd46a782018-07-10 18:37:30 -07003204void rcu_barrier(void)
Paul E. McKenneyd0ec7742009-10-06 21:48:16 -07003205{
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08003206 int cpu;
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08003207 struct rcu_data *rdp;
Paul E. McKenneyec9f5832018-07-05 16:26:12 -07003208 unsigned long s = rcu_seq_snap(&rcu_state.barrier_sequence);
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08003209
Paul E. McKenneydd46a782018-07-10 18:37:30 -07003210 rcu_barrier_trace(TPS("Begin"), -1, s);
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08003211
Paul E. McKenneye74f4c42009-10-06 21:48:17 -07003212 /* Take mutex to serialize concurrent rcu_barrier() requests. */
Paul E. McKenneyec9f5832018-07-05 16:26:12 -07003213 mutex_lock(&rcu_state.barrier_mutex);
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08003214
Paul E. McKenney4f525a52015-06-26 11:20:00 -07003215 /* Did someone else do our work for us? */
Paul E. McKenneyec9f5832018-07-05 16:26:12 -07003216 if (rcu_seq_done(&rcu_state.barrier_sequence, s)) {
Paul E. McKenneydd46a782018-07-10 18:37:30 -07003217 rcu_barrier_trace(TPS("EarlyExit"), -1,
Paul E. McKenneyec9f5832018-07-05 16:26:12 -07003218 rcu_state.barrier_sequence);
Paul E. McKenneycf3a9c42012-05-29 14:56:46 -07003219 smp_mb(); /* caller's subsequent code after above check. */
Paul E. McKenneyec9f5832018-07-05 16:26:12 -07003220 mutex_unlock(&rcu_state.barrier_mutex);
Paul E. McKenneycf3a9c42012-05-29 14:56:46 -07003221 return;
3222 }
3223
Paul E. McKenney4f525a52015-06-26 11:20:00 -07003224 /* Mark the start of the barrier operation. */
Paul E. McKenneyec9f5832018-07-05 16:26:12 -07003225 rcu_seq_start(&rcu_state.barrier_sequence);
Paul E. McKenneydd46a782018-07-10 18:37:30 -07003226 rcu_barrier_trace(TPS("Inc1"), -1, rcu_state.barrier_sequence);
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08003227
Paul E. McKenneyd0ec7742009-10-06 21:48:16 -07003228 /*
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08003229 * Initialize the count to one rather than to zero in order to
3230 * avoid a too-soon return to zero in case of a short grace period
Paul E. McKenney1331e7a2012-08-02 17:43:50 -07003231 * (or preemption of this task). Exclude CPU-hotplug operations
3232 * to ensure that no offline CPU has callbacks queued.
Paul E. McKenneyd0ec7742009-10-06 21:48:16 -07003233 */
Paul E. McKenneyec9f5832018-07-05 16:26:12 -07003234 init_completion(&rcu_state.barrier_completion);
3235 atomic_set(&rcu_state.barrier_cpu_count, 1);
Paul E. McKenney1331e7a2012-08-02 17:43:50 -07003236 get_online_cpus();
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08003237
3238 /*
Paul E. McKenney1331e7a2012-08-02 17:43:50 -07003239 * Force each CPU with callbacks to register a new callback.
3240 * When that callback is invoked, we will know that all of the
3241 * corresponding CPU's preceding callbacks have been invoked.
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08003242 */
Paul E. McKenney3fbfbf72012-08-19 21:35:53 -07003243 for_each_possible_cpu(cpu) {
Frederic Weisbeckerd1e43fa2013-03-26 23:47:24 +01003244 if (!cpu_online(cpu) && !rcu_is_nocb_cpu(cpu))
Paul E. McKenney3fbfbf72012-08-19 21:35:53 -07003245 continue;
Paul E. McKenneyda1df502018-07-03 15:37:16 -07003246 rdp = per_cpu_ptr(&rcu_data, cpu);
Frederic Weisbeckerd1e43fa2013-03-26 23:47:24 +01003247 if (rcu_is_nocb_cpu(cpu)) {
Paul E. McKenney4580b052018-07-03 17:22:34 -07003248 if (!rcu_nocb_cpu_needs_barrier(cpu)) {
Paul E. McKenneydd46a782018-07-10 18:37:30 -07003249 rcu_barrier_trace(TPS("OfflineNoCB"), cpu,
Paul E. McKenneyec9f5832018-07-05 16:26:12 -07003250 rcu_state.barrier_sequence);
Paul E. McKenneyd7e29932014-10-27 09:15:54 -07003251 } else {
Paul E. McKenneydd46a782018-07-10 18:37:30 -07003252 rcu_barrier_trace(TPS("OnlineNoCB"), cpu,
Paul E. McKenneyec9f5832018-07-05 16:26:12 -07003253 rcu_state.barrier_sequence);
Paul E. McKenney41050a02014-12-18 12:31:27 -08003254 smp_mb__before_atomic();
Paul E. McKenneyec9f5832018-07-05 16:26:12 -07003255 atomic_inc(&rcu_state.barrier_cpu_count);
Paul E. McKenneyd7e29932014-10-27 09:15:54 -07003256 __call_rcu(&rdp->barrier_head,
Paul E. McKenney5c7d8962018-07-03 17:22:34 -07003257 rcu_barrier_callback, cpu, 0);
Paul E. McKenneyd7e29932014-10-27 09:15:54 -07003258 }
Paul E. McKenney15fecf82017-02-08 12:36:42 -08003259 } else if (rcu_segcblist_n_cbs(&rdp->cblist)) {
Paul E. McKenneydd46a782018-07-10 18:37:30 -07003260 rcu_barrier_trace(TPS("OnlineQ"), cpu,
Paul E. McKenneyec9f5832018-07-05 16:26:12 -07003261 rcu_state.barrier_sequence);
3262 smp_call_function_single(cpu, rcu_barrier_func, NULL, 1);
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08003263 } else {
Paul E. McKenneydd46a782018-07-10 18:37:30 -07003264 rcu_barrier_trace(TPS("OnlineNQ"), cpu,
Paul E. McKenneyec9f5832018-07-05 16:26:12 -07003265 rcu_state.barrier_sequence);
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08003266 }
3267 }
Paul E. McKenney1331e7a2012-08-02 17:43:50 -07003268 put_online_cpus();
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08003269
3270 /*
3271 * Now that we have an rcu_barrier_callback() callback on each
3272 * CPU, and thus each counted, remove the initial count.
3273 */
Paul E. McKenneyec9f5832018-07-05 16:26:12 -07003274 if (atomic_dec_and_test(&rcu_state.barrier_cpu_count))
3275 complete(&rcu_state.barrier_completion);
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08003276
3277 /* Wait for all rcu_barrier_callback() callbacks to be invoked. */
Paul E. McKenneyec9f5832018-07-05 16:26:12 -07003278 wait_for_completion(&rcu_state.barrier_completion);
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08003279
Paul E. McKenney4f525a52015-06-26 11:20:00 -07003280 /* Mark the end of the barrier operation. */
Paul E. McKenneydd46a782018-07-10 18:37:30 -07003281 rcu_barrier_trace(TPS("Inc2"), -1, rcu_state.barrier_sequence);
Paul E. McKenneyec9f5832018-07-05 16:26:12 -07003282 rcu_seq_end(&rcu_state.barrier_sequence);
Paul E. McKenney4f525a52015-06-26 11:20:00 -07003283
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08003284 /* Other rcu_barrier() invocations can now safely proceed. */
Paul E. McKenneyec9f5832018-07-05 16:26:12 -07003285 mutex_unlock(&rcu_state.barrier_mutex);
Paul E. McKenneyd0ec7742009-10-06 21:48:16 -07003286}
Paul E. McKenney45975c72018-07-02 14:30:37 -07003287EXPORT_SYMBOL_GPL(rcu_barrier);
Paul E. McKenneyd0ec7742009-10-06 21:48:16 -07003288
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003289/*
Paul E. McKenney0aa04b02015-01-23 21:52:37 -08003290 * Propagate ->qsinitmask bits up the rcu_node tree to account for the
3291 * first CPU in a given leaf rcu_node structure coming online. The caller
3292 * must hold the corresponding leaf rcu_node ->lock with interrrupts
3293 * disabled.
3294 */
3295static void rcu_init_new_rnp(struct rcu_node *rnp_leaf)
3296{
3297 long mask;
Paul E. McKenney8d672fa2018-05-02 14:46:43 -07003298 long oldmask;
Paul E. McKenney0aa04b02015-01-23 21:52:37 -08003299 struct rcu_node *rnp = rnp_leaf;
3300
Paul E. McKenney8d672fa2018-05-02 14:46:43 -07003301 raw_lockdep_assert_held_rcu_node(rnp_leaf);
Paul E. McKenney962aff02018-05-02 12:49:21 -07003302 WARN_ON_ONCE(rnp->wait_blkd_tasks);
Paul E. McKenney0aa04b02015-01-23 21:52:37 -08003303 for (;;) {
3304 mask = rnp->grpmask;
3305 rnp = rnp->parent;
3306 if (rnp == NULL)
3307 return;
Paul E. McKenney6cf10082015-10-08 15:36:54 -07003308 raw_spin_lock_rcu_node(rnp); /* Interrupts already disabled. */
Paul E. McKenney8d672fa2018-05-02 14:46:43 -07003309 oldmask = rnp->qsmaskinit;
Paul E. McKenney0aa04b02015-01-23 21:52:37 -08003310 rnp->qsmaskinit |= mask;
Boqun Feng67c583a72015-12-29 12:18:47 +08003311 raw_spin_unlock_rcu_node(rnp); /* Interrupts remain disabled. */
Paul E. McKenney8d672fa2018-05-02 14:46:43 -07003312 if (oldmask)
3313 return;
Paul E. McKenney0aa04b02015-01-23 21:52:37 -08003314 }
3315}
3316
3317/*
Paul E. McKenney27569622009-08-15 09:53:46 -07003318 * Do boot-time initialization of a CPU's per-CPU RCU data.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003319 */
Paul E. McKenney27569622009-08-15 09:53:46 -07003320static void __init
Paul E. McKenney53b46302018-07-03 17:22:34 -07003321rcu_boot_init_percpu_data(int cpu)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003322{
Paul E. McKenneyda1df502018-07-03 15:37:16 -07003323 struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
Paul E. McKenney27569622009-08-15 09:53:46 -07003324
3325 /* Set up local state, ensuring consistent view of global state. */
Mark Rutlandbc75e992016-06-03 15:20:04 +01003326 rdp->grpmask = leaf_node_cpu_bit(rdp->mynode, cpu);
Paul E. McKenney4c5273b2018-08-03 21:00:38 -07003327 WARN_ON_ONCE(rdp->dynticks_nesting != 1);
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -07003328 WARN_ON_ONCE(rcu_dynticks_in_eqs(rcu_dynticks_snap(rdp)));
Paul E. McKenney53b46302018-07-03 17:22:34 -07003329 rdp->rcu_ofl_gp_seq = rcu_state.gp_seq;
Paul E. McKenney57738942018-05-08 14:18:57 -07003330 rdp->rcu_ofl_gp_flags = RCU_GP_CLEANED;
Paul E. McKenney53b46302018-07-03 17:22:34 -07003331 rdp->rcu_onl_gp_seq = rcu_state.gp_seq;
Paul E. McKenney57738942018-05-08 14:18:57 -07003332 rdp->rcu_onl_gp_flags = RCU_GP_CLEANED;
Paul E. McKenney27569622009-08-15 09:53:46 -07003333 rdp->cpu = cpu;
Paul E. McKenney3fbfbf72012-08-19 21:35:53 -07003334 rcu_boot_init_nocb_percpu_data(rdp);
Paul E. McKenney27569622009-08-15 09:53:46 -07003335}
3336
3337/*
Paul E. McKenney53b46302018-07-03 17:22:34 -07003338 * Invoked early in the CPU-online process, when pretty much all services
3339 * are available. The incoming CPU is not present.
3340 *
3341 * Initializes a CPU's per-CPU RCU data. Note that only one online or
Paul E. McKenneyff3bb6f2018-05-01 14:34:08 -07003342 * offline event can be happening at a given time. Note also that we can
3343 * accept some slop in the rsp->gp_seq access due to the fact that this
3344 * CPU cannot possibly have any RCU callbacks in flight yet.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003345 */
Paul E. McKenney53b46302018-07-03 17:22:34 -07003346int rcutree_prepare_cpu(unsigned int cpu)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003347{
3348 unsigned long flags;
Paul E. McKenneyda1df502018-07-03 15:37:16 -07003349 struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
Paul E. McKenney336a4f62018-07-03 17:22:34 -07003350 struct rcu_node *rnp = rcu_get_root();
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003351
3352 /* Set up local state, ensuring consistent view of global state. */
Paul E. McKenney6cf10082015-10-08 15:36:54 -07003353 raw_spin_lock_irqsave_rcu_node(rnp, flags);
Paul E. McKenney37c72e52009-10-14 10:15:55 -07003354 rdp->qlen_last_fqs_check = 0;
Paul E. McKenney53b46302018-07-03 17:22:34 -07003355 rdp->n_force_qs_snap = rcu_state.n_force_qs;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003356 rdp->blimit = blimit;
Paul E. McKenney15fecf82017-02-08 12:36:42 -08003357 if (rcu_segcblist_empty(&rdp->cblist) && /* No early-boot CBs? */
3358 !init_nocb_callback_list(rdp))
3359 rcu_segcblist_init(&rdp->cblist); /* Re-enable callbacks. */
Paul E. McKenney4c5273b2018-08-03 21:00:38 -07003360 rdp->dynticks_nesting = 1; /* CPU not up, no tearing. */
Paul E. McKenney2625d462016-11-02 14:23:30 -07003361 rcu_dynticks_eqs_online();
Boqun Feng67c583a72015-12-29 12:18:47 +08003362 raw_spin_unlock_rcu_node(rnp); /* irqs remain disabled. */
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003363
Paul E. McKenney0aa04b02015-01-23 21:52:37 -08003364 /*
3365 * Add CPU to leaf rcu_node pending-online bitmask. Any needed
3366 * propagation up the rcu_node tree will happen at the beginning
3367 * of the next grace period.
3368 */
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003369 rnp = rdp->mynode;
Peter Zijlstra2a67e742015-10-08 12:24:23 +02003370 raw_spin_lock_rcu_node(rnp); /* irqs already disabled. */
Paul E. McKenneyb9585e92015-07-31 16:04:45 -07003371 rdp->beenonline = true; /* We have now been online. */
Paul E. McKenneyde30ad52018-04-26 11:52:09 -07003372 rdp->gp_seq = rnp->gp_seq;
Paul E. McKenney7a1d0f22018-05-01 10:26:57 -07003373 rdp->gp_seq_needed = rnp->gp_seq;
Paul E. McKenney5b74c452015-08-06 15:16:57 -07003374 rdp->cpu_no_qs.b.norm = true;
Paul E. McKenney97c668b2015-08-06 11:31:51 -07003375 rdp->core_needs_qs = false;
Paul E. McKenney9b9500d2017-08-17 17:05:59 -07003376 rdp->rcu_iw_pending = false;
Paul E. McKenney8aa670c2018-04-28 14:15:40 -07003377 rdp->rcu_iw_gp_seq = rnp->gp_seq - 1;
Paul E. McKenney53b46302018-07-03 17:22:34 -07003378 trace_rcu_grace_period(rcu_state.name, rdp->gp_seq, TPS("cpuonl"));
Boqun Feng67c583a72015-12-29 12:18:47 +08003379 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
Thomas Gleixner4df83742016-07-13 17:17:03 +00003380 rcu_prepare_kthreads(cpu);
Paul E. McKenneyad368d12018-11-27 13:55:53 -08003381 rcu_spawn_cpu_nocb_kthread(cpu);
Thomas Gleixner4df83742016-07-13 17:17:03 +00003382
3383 return 0;
3384}
3385
Paul E. McKenneydeb34f32017-03-23 13:21:30 -07003386/*
3387 * Update RCU priority boot kthread affinity for CPU-hotplug changes.
3388 */
Thomas Gleixner4df83742016-07-13 17:17:03 +00003389static void rcutree_affinity_setting(unsigned int cpu, int outgoing)
3390{
Paul E. McKenneyda1df502018-07-03 15:37:16 -07003391 struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
Thomas Gleixner4df83742016-07-13 17:17:03 +00003392
3393 rcu_boost_kthread_setaffinity(rdp->mynode, outgoing);
3394}
3395
Paul E. McKenneydeb34f32017-03-23 13:21:30 -07003396/*
3397 * Near the end of the CPU-online process. Pretty much all services
3398 * enabled, and the CPU is now very much alive.
3399 */
Thomas Gleixner4df83742016-07-13 17:17:03 +00003400int rcutree_online_cpu(unsigned int cpu)
3401{
Paul E. McKenney9b9500d2017-08-17 17:05:59 -07003402 unsigned long flags;
3403 struct rcu_data *rdp;
3404 struct rcu_node *rnp;
Paul E. McKenney9b9500d2017-08-17 17:05:59 -07003405
Paul E. McKenneyb97d23c2018-07-04 15:35:00 -07003406 rdp = per_cpu_ptr(&rcu_data, cpu);
3407 rnp = rdp->mynode;
3408 raw_spin_lock_irqsave_rcu_node(rnp, flags);
3409 rnp->ffmask |= rdp->grpmask;
3410 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
Paul E. McKenneyda915ad2017-04-05 09:01:53 -07003411 if (IS_ENABLED(CONFIG_TREE_SRCU))
3412 srcu_online_cpu(cpu);
Paul E. McKenney9b9500d2017-08-17 17:05:59 -07003413 if (rcu_scheduler_active == RCU_SCHEDULER_INACTIVE)
3414 return 0; /* Too early in boot for scheduler work. */
3415 sync_sched_exp_online_cleanup(cpu);
3416 rcutree_affinity_setting(cpu, -1);
Thomas Gleixner4df83742016-07-13 17:17:03 +00003417 return 0;
3418}
3419
Paul E. McKenneydeb34f32017-03-23 13:21:30 -07003420/*
3421 * Near the beginning of the process. The CPU is still very much alive
3422 * with pretty much all services enabled.
3423 */
Thomas Gleixner4df83742016-07-13 17:17:03 +00003424int rcutree_offline_cpu(unsigned int cpu)
3425{
Paul E. McKenney9b9500d2017-08-17 17:05:59 -07003426 unsigned long flags;
3427 struct rcu_data *rdp;
3428 struct rcu_node *rnp;
Paul E. McKenney9b9500d2017-08-17 17:05:59 -07003429
Paul E. McKenneyb97d23c2018-07-04 15:35:00 -07003430 rdp = per_cpu_ptr(&rcu_data, cpu);
3431 rnp = rdp->mynode;
3432 raw_spin_lock_irqsave_rcu_node(rnp, flags);
3433 rnp->ffmask &= ~rdp->grpmask;
3434 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
Paul E. McKenney9b9500d2017-08-17 17:05:59 -07003435
Thomas Gleixner4df83742016-07-13 17:17:03 +00003436 rcutree_affinity_setting(cpu, cpu);
Paul E. McKenneyda915ad2017-04-05 09:01:53 -07003437 if (IS_ENABLED(CONFIG_TREE_SRCU))
3438 srcu_offline_cpu(cpu);
Thomas Gleixner4df83742016-07-13 17:17:03 +00003439 return 0;
3440}
3441
Peter Zijlstraf64c6012018-05-22 09:50:53 -07003442static DEFINE_PER_CPU(int, rcu_cpu_started);
3443
Paul E. McKenney7ec99de2016-06-30 13:58:26 -07003444/*
3445 * Mark the specified CPU as being online so that subsequent grace periods
3446 * (both expedited and normal) will wait on it. Note that this means that
3447 * incoming CPUs are not allowed to use RCU read-side critical sections
3448 * until this function is called. Failing to observe this restriction
3449 * will result in lockdep splats.
Paul E. McKenneydeb34f32017-03-23 13:21:30 -07003450 *
3451 * Note that this function is special in that it is invoked directly
3452 * from the incoming CPU rather than from the cpuhp_step mechanism.
3453 * This is because this function must be invoked at a precise location.
Paul E. McKenney7ec99de2016-06-30 13:58:26 -07003454 */
3455void rcu_cpu_starting(unsigned int cpu)
3456{
3457 unsigned long flags;
3458 unsigned long mask;
Paul E. McKenney313517fc2017-06-08 16:55:40 -07003459 int nbits;
3460 unsigned long oldmask;
Paul E. McKenney7ec99de2016-06-30 13:58:26 -07003461 struct rcu_data *rdp;
3462 struct rcu_node *rnp;
Paul E. McKenney7ec99de2016-06-30 13:58:26 -07003463
Peter Zijlstraf64c6012018-05-22 09:50:53 -07003464 if (per_cpu(rcu_cpu_started, cpu))
3465 return;
3466
3467 per_cpu(rcu_cpu_started, cpu) = 1;
3468
Paul E. McKenneyb97d23c2018-07-04 15:35:00 -07003469 rdp = per_cpu_ptr(&rcu_data, cpu);
3470 rnp = rdp->mynode;
3471 mask = rdp->grpmask;
3472 raw_spin_lock_irqsave_rcu_node(rnp, flags);
3473 rnp->qsmaskinitnext |= mask;
3474 oldmask = rnp->expmaskinitnext;
3475 rnp->expmaskinitnext |= mask;
3476 oldmask ^= rnp->expmaskinitnext;
3477 nbits = bitmap_weight(&oldmask, BITS_PER_LONG);
3478 /* Allow lockless access for expedited grace periods. */
Paul E. McKenneyeb7a6652018-07-05 17:47:45 -07003479 smp_store_release(&rcu_state.ncpus, rcu_state.ncpus + nbits); /* ^^^ */
Paul E. McKenneyb97d23c2018-07-04 15:35:00 -07003480 rcu_gpnum_ovf(rnp, rdp); /* Offline-induced counter wrap? */
Paul E. McKenneyeb7a6652018-07-05 17:47:45 -07003481 rdp->rcu_onl_gp_seq = READ_ONCE(rcu_state.gp_seq);
3482 rdp->rcu_onl_gp_flags = READ_ONCE(rcu_state.gp_flags);
Paul E. McKenneyb97d23c2018-07-04 15:35:00 -07003483 if (rnp->qsmask & mask) { /* RCU waiting on incoming CPU? */
3484 /* Report QS -after- changing ->qsmaskinitnext! */
3485 rcu_report_qs_rnp(mask, rnp, rnp->gp_seq, flags);
3486 } else {
3487 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
Paul E. McKenney7ec99de2016-06-30 13:58:26 -07003488 }
Paul E. McKenney313517fc2017-06-08 16:55:40 -07003489 smp_mb(); /* Ensure RCU read-side usage follows above initialization. */
Paul E. McKenney7ec99de2016-06-30 13:58:26 -07003490}
3491
Thomas Gleixner27d50c72016-02-26 18:43:44 +00003492#ifdef CONFIG_HOTPLUG_CPU
3493/*
Paul E. McKenneydeb34f32017-03-23 13:21:30 -07003494 * The outgoing function has no further need of RCU, so remove it from
Paul E. McKenney53b46302018-07-03 17:22:34 -07003495 * the rcu_node tree's ->qsmaskinitnext bit masks.
Paul E. McKenneydeb34f32017-03-23 13:21:30 -07003496 *
3497 * Note that this function is special in that it is invoked directly
3498 * from the outgoing CPU rather than from the cpuhp_step mechanism.
3499 * This is because this function must be invoked at a precise location.
3500 */
Thomas Gleixner27d50c72016-02-26 18:43:44 +00003501void rcu_report_dead(unsigned int cpu)
3502{
Paul E. McKenney53b46302018-07-03 17:22:34 -07003503 unsigned long flags;
3504 unsigned long mask;
3505 struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
3506 struct rcu_node *rnp = rdp->mynode; /* Outgoing CPU's rdp & rnp. */
Thomas Gleixner27d50c72016-02-26 18:43:44 +00003507
Paul E. McKenney49918a52018-07-07 18:12:26 -07003508 /* QS for any half-done expedited grace period. */
Thomas Gleixner27d50c72016-02-26 18:43:44 +00003509 preempt_disable();
Paul E. McKenney63d4c8c2018-07-03 17:22:34 -07003510 rcu_report_exp_rdp(this_cpu_ptr(&rcu_data));
Thomas Gleixner27d50c72016-02-26 18:43:44 +00003511 preempt_enable();
Paul E. McKenney3e310092018-06-21 12:50:01 -07003512 rcu_preempt_deferred_qs(current);
Paul E. McKenney53b46302018-07-03 17:22:34 -07003513
3514 /* Remove outgoing CPU from mask in the leaf rcu_node structure. */
3515 mask = rdp->grpmask;
Mike Galbraith894d45b2018-08-15 09:05:29 -07003516 raw_spin_lock(&rcu_state.ofl_lock);
Paul E. McKenney53b46302018-07-03 17:22:34 -07003517 raw_spin_lock_irqsave_rcu_node(rnp, flags); /* Enforce GP memory-order guarantee. */
3518 rdp->rcu_ofl_gp_seq = READ_ONCE(rcu_state.gp_seq);
3519 rdp->rcu_ofl_gp_flags = READ_ONCE(rcu_state.gp_flags);
3520 if (rnp->qsmask & mask) { /* RCU waiting on outgoing CPU? */
3521 /* Report quiescent state -before- changing ->qsmaskinitnext! */
3522 rcu_report_qs_rnp(mask, rnp, rnp->gp_seq, flags);
3523 raw_spin_lock_irqsave_rcu_node(rnp, flags);
3524 }
3525 rnp->qsmaskinitnext &= ~mask;
3526 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
Mike Galbraith894d45b2018-08-15 09:05:29 -07003527 raw_spin_unlock(&rcu_state.ofl_lock);
Peter Zijlstraf64c6012018-05-22 09:50:53 -07003528
3529 per_cpu(rcu_cpu_started, cpu) = 0;
Thomas Gleixner27d50c72016-02-26 18:43:44 +00003530}
Paul E. McKenneya58163d2017-06-20 12:11:34 -07003531
Paul E. McKenney53b46302018-07-03 17:22:34 -07003532/*
3533 * The outgoing CPU has just passed through the dying-idle state, and we
3534 * are being invoked from the CPU that was IPIed to continue the offline
3535 * operation. Migrate the outgoing CPU's callbacks to the current CPU.
3536 */
3537void rcutree_migrate_callbacks(int cpu)
Paul E. McKenneya58163d2017-06-20 12:11:34 -07003538{
3539 unsigned long flags;
Paul E. McKenneyb1a2d792017-06-26 12:23:46 -07003540 struct rcu_data *my_rdp;
Paul E. McKenneyda1df502018-07-03 15:37:16 -07003541 struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
Paul E. McKenney336a4f62018-07-03 17:22:34 -07003542 struct rcu_node *rnp_root = rcu_get_root();
Paul E. McKenneyec4eacc2018-04-22 08:49:24 -07003543 bool needwake;
Paul E. McKenneya58163d2017-06-20 12:11:34 -07003544
Paul E. McKenney95335c02017-06-26 10:49:50 -07003545 if (rcu_is_nocb_cpu(cpu) || rcu_segcblist_empty(&rdp->cblist))
3546 return; /* No callbacks to migrate. */
3547
Paul E. McKenneyb1a2d792017-06-26 12:23:46 -07003548 local_irq_save(flags);
Paul E. McKenneyda1df502018-07-03 15:37:16 -07003549 my_rdp = this_cpu_ptr(&rcu_data);
Paul E. McKenneyb1a2d792017-06-26 12:23:46 -07003550 if (rcu_nocb_adopt_orphan_cbs(my_rdp, rdp, flags)) {
3551 local_irq_restore(flags);
3552 return;
3553 }
Paul E. McKenney9fa46fb2017-06-26 15:43:27 -07003554 raw_spin_lock_rcu_node(rnp_root); /* irqs already disabled. */
Paul E. McKenneyec4eacc2018-04-22 08:49:24 -07003555 /* Leverage recent GPs and set GP for new callbacks. */
Paul E. McKenney834f56b2018-07-03 17:22:34 -07003556 needwake = rcu_advance_cbs(rnp_root, rdp) ||
3557 rcu_advance_cbs(rnp_root, my_rdp);
Paul E. McKenneyf2dbe4a2017-06-27 07:44:06 -07003558 rcu_segcblist_merge(&my_rdp->cblist, &rdp->cblist);
Paul E. McKenney09efeee2017-07-19 10:56:46 -07003559 WARN_ON_ONCE(rcu_segcblist_empty(&my_rdp->cblist) !=
3560 !rcu_segcblist_n_cbs(&my_rdp->cblist));
Paul E. McKenney537b85c2017-06-26 17:59:02 -07003561 raw_spin_unlock_irqrestore_rcu_node(rnp_root, flags);
Paul E. McKenneyec4eacc2018-04-22 08:49:24 -07003562 if (needwake)
Paul E. McKenney532c00c2018-07-03 17:22:34 -07003563 rcu_gp_kthread_wake();
Paul E. McKenneya58163d2017-06-20 12:11:34 -07003564 WARN_ONCE(rcu_segcblist_n_cbs(&rdp->cblist) != 0 ||
3565 !rcu_segcblist_empty(&rdp->cblist),
3566 "rcu_cleanup_dead_cpu: Callbacks on offline CPU %d: qlen=%lu, 1stCB=%p\n",
3567 cpu, rcu_segcblist_n_cbs(&rdp->cblist),
3568 rcu_segcblist_first_cb(&rdp->cblist));
3569}
Thomas Gleixner27d50c72016-02-26 18:43:44 +00003570#endif
3571
Paul E. McKenneydeb34f32017-03-23 13:21:30 -07003572/*
3573 * On non-huge systems, use expedited RCU grace periods to make suspend
3574 * and hibernation run faster.
3575 */
Borislav Petkovd1d74d12013-04-22 00:12:42 +02003576static int rcu_pm_notify(struct notifier_block *self,
3577 unsigned long action, void *hcpu)
3578{
3579 switch (action) {
3580 case PM_HIBERNATION_PREPARE:
3581 case PM_SUSPEND_PREPARE:
3582 if (nr_cpu_ids <= 256) /* Expediting bad for large systems. */
Paul E. McKenney5afff482015-02-18 16:39:09 -08003583 rcu_expedite_gp();
Borislav Petkovd1d74d12013-04-22 00:12:42 +02003584 break;
3585 case PM_POST_HIBERNATION:
3586 case PM_POST_SUSPEND:
Paul E. McKenney5afff482015-02-18 16:39:09 -08003587 if (nr_cpu_ids <= 256) /* Expediting bad for large systems. */
3588 rcu_unexpedite_gp();
Borislav Petkovd1d74d12013-04-22 00:12:42 +02003589 break;
3590 default:
3591 break;
3592 }
3593 return NOTIFY_OK;
3594}
3595
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003596/*
Paul E. McKenney49918a52018-07-07 18:12:26 -07003597 * Spawn the kthreads that handle RCU's grace periods.
Paul E. McKenneyb3dbec72012-06-18 18:36:08 -07003598 */
3599static int __init rcu_spawn_gp_kthread(void)
3600{
3601 unsigned long flags;
Paul E. McKenneya94844b2014-12-12 07:37:48 -08003602 int kthread_prio_in = kthread_prio;
Paul E. McKenneyb3dbec72012-06-18 18:36:08 -07003603 struct rcu_node *rnp;
Paul E. McKenneya94844b2014-12-12 07:37:48 -08003604 struct sched_param sp;
Paul E. McKenneyb3dbec72012-06-18 18:36:08 -07003605 struct task_struct *t;
3606
Paul E. McKenneya94844b2014-12-12 07:37:48 -08003607 /* Force priority into range. */
Joel Fernandes (Google)c7cd1612018-06-19 15:14:17 -07003608 if (IS_ENABLED(CONFIG_RCU_BOOST) && kthread_prio < 2
3609 && IS_BUILTIN(CONFIG_RCU_TORTURE_TEST))
3610 kthread_prio = 2;
3611 else if (IS_ENABLED(CONFIG_RCU_BOOST) && kthread_prio < 1)
Paul E. McKenneya94844b2014-12-12 07:37:48 -08003612 kthread_prio = 1;
3613 else if (kthread_prio < 0)
3614 kthread_prio = 0;
3615 else if (kthread_prio > 99)
3616 kthread_prio = 99;
Joel Fernandes (Google)c7cd1612018-06-19 15:14:17 -07003617
Paul E. McKenneya94844b2014-12-12 07:37:48 -08003618 if (kthread_prio != kthread_prio_in)
3619 pr_alert("rcu_spawn_gp_kthread(): Limited prio to %d from %d\n",
3620 kthread_prio, kthread_prio_in);
3621
Paul E. McKenney9386c0b2014-07-13 12:00:53 -07003622 rcu_scheduler_fully_active = 1;
Paul E. McKenneyb97d23c2018-07-04 15:35:00 -07003623 t = kthread_create(rcu_gp_kthread, NULL, "%s", rcu_state.name);
Paul E. McKenney08543bd2018-10-22 08:04:03 -07003624 if (WARN_ONCE(IS_ERR(t), "%s: Could not start grace-period kthread, OOM is now expected behavior\n", __func__))
3625 return 0;
Paul E. McKenneyb97d23c2018-07-04 15:35:00 -07003626 rnp = rcu_get_root();
3627 raw_spin_lock_irqsave_rcu_node(rnp, flags);
3628 rcu_state.gp_kthread = t;
3629 if (kthread_prio) {
3630 sp.sched_priority = kthread_prio;
3631 sched_setscheduler_nocheck(t, SCHED_FIFO, &sp);
Paul E. McKenneyb3dbec72012-06-18 18:36:08 -07003632 }
Paul E. McKenneyb97d23c2018-07-04 15:35:00 -07003633 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
3634 wake_up_process(t);
Paul E. McKenney35ce7f22014-07-11 11:30:24 -07003635 rcu_spawn_nocb_kthreads();
Paul E. McKenney9386c0b2014-07-13 12:00:53 -07003636 rcu_spawn_boost_kthreads();
Paul E. McKenneyb3dbec72012-06-18 18:36:08 -07003637 return 0;
3638}
3639early_initcall(rcu_spawn_gp_kthread);
3640
3641/*
Paul E. McKenney52d7e482017-01-10 02:28:26 -08003642 * This function is invoked towards the end of the scheduler's
3643 * initialization process. Before this is called, the idle task might
3644 * contain synchronous grace-period primitives (during which time, this idle
3645 * task is booting the system, and such primitives are no-ops). After this
3646 * function is called, any synchronous grace-period primitives are run as
3647 * expedited, with the requesting task driving the grace period forward.
Paul E. McKenney900b1022017-02-10 14:32:54 -08003648 * A later core_initcall() rcu_set_runtime_mode() will switch to full
Paul E. McKenney52d7e482017-01-10 02:28:26 -08003649 * runtime RCU functionality.
Paul E. McKenneybbad9372010-04-02 16:17:17 -07003650 */
3651void rcu_scheduler_starting(void)
3652{
3653 WARN_ON(num_online_cpus() != 1);
3654 WARN_ON(nr_context_switches() > 0);
Paul E. McKenney52d7e482017-01-10 02:28:26 -08003655 rcu_test_sync_prims();
3656 rcu_scheduler_active = RCU_SCHEDULER_INIT;
3657 rcu_test_sync_prims();
Paul E. McKenneybbad9372010-04-02 16:17:17 -07003658}
3659
3660/*
Paul E. McKenney49918a52018-07-07 18:12:26 -07003661 * Helper function for rcu_init() that initializes the rcu_state structure.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003662 */
Paul E. McKenneyb8bb1f62018-07-03 17:22:34 -07003663static void __init rcu_init_one(void)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003664{
Alexander Gordeevcb007102015-06-03 08:18:30 +02003665 static const char * const buf[] = RCU_NODE_NAME_INIT;
3666 static const char * const fqs[] = RCU_FQS_NAME_INIT;
Paul E. McKenney3dc5dbe2015-09-26 14:51:24 -07003667 static struct lock_class_key rcu_node_class[RCU_NUM_LVLS];
3668 static struct lock_class_key rcu_fqs_class[RCU_NUM_LVLS];
Alexander Gordeev199977b2015-06-03 08:18:29 +02003669
Alexander Gordeev199977b2015-06-03 08:18:29 +02003670 int levelspread[RCU_NUM_LVLS]; /* kids/node in each level. */
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003671 int cpustride = 1;
3672 int i;
3673 int j;
3674 struct rcu_node *rnp;
3675
Alexander Gordeev05b84ae2015-06-03 08:18:28 +02003676 BUILD_BUG_ON(RCU_NUM_LVLS > ARRAY_SIZE(buf)); /* Fix buf[] init! */
Paul E. McKenneyb6407e82010-01-04 16:04:02 -08003677
Paul E. McKenney3eaaaf6c2015-03-09 16:51:17 -07003678 /* Silence gcc 4.8 false positive about array index out of range. */
3679 if (rcu_num_lvls <= 0 || rcu_num_lvls > RCU_NUM_LVLS)
3680 panic("rcu_init_one: rcu_num_lvls out of range");
Paul E. McKenney49305212012-11-29 13:49:00 -08003681
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003682 /* Initialize the level-tracking arrays. */
3683
Paul E. McKenneyf885b7f2012-04-23 15:52:53 -07003684 for (i = 1; i < rcu_num_lvls; i++)
Paul E. McKenneyeb7a6652018-07-05 17:47:45 -07003685 rcu_state.level[i] =
3686 rcu_state.level[i - 1] + num_rcu_lvl[i - 1];
Paul E. McKenney41f5c632017-03-15 12:59:17 -07003687 rcu_init_levelspread(levelspread, num_rcu_lvl);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003688
3689 /* Initialize the elements themselves, starting from the leaves. */
3690
Paul E. McKenneyf885b7f2012-04-23 15:52:53 -07003691 for (i = rcu_num_lvls - 1; i >= 0; i--) {
Alexander Gordeev199977b2015-06-03 08:18:29 +02003692 cpustride *= levelspread[i];
Paul E. McKenneyeb7a6652018-07-05 17:47:45 -07003693 rnp = rcu_state.level[i];
Paul E. McKenney41f5c632017-03-15 12:59:17 -07003694 for (j = 0; j < num_rcu_lvl[i]; j++, rnp++) {
Boqun Feng67c583a72015-12-29 12:18:47 +08003695 raw_spin_lock_init(&ACCESS_PRIVATE(rnp, lock));
3696 lockdep_set_class_and_name(&ACCESS_PRIVATE(rnp, lock),
Paul E. McKenneyb6407e82010-01-04 16:04:02 -08003697 &rcu_node_class[i], buf[i]);
Paul E. McKenney394f2762012-06-26 17:00:35 -07003698 raw_spin_lock_init(&rnp->fqslock);
3699 lockdep_set_class_and_name(&rnp->fqslock,
3700 &rcu_fqs_class[i], fqs[i]);
Paul E. McKenneyeb7a6652018-07-05 17:47:45 -07003701 rnp->gp_seq = rcu_state.gp_seq;
3702 rnp->gp_seq_needed = rcu_state.gp_seq;
3703 rnp->completedqs = rcu_state.gp_seq;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003704 rnp->qsmask = 0;
3705 rnp->qsmaskinit = 0;
3706 rnp->grplo = j * cpustride;
3707 rnp->grphi = (j + 1) * cpustride - 1;
Himangi Saraogi595f3902014-03-18 22:52:26 +05303708 if (rnp->grphi >= nr_cpu_ids)
3709 rnp->grphi = nr_cpu_ids - 1;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003710 if (i == 0) {
3711 rnp->grpnum = 0;
3712 rnp->grpmask = 0;
3713 rnp->parent = NULL;
3714 } else {
Alexander Gordeev199977b2015-06-03 08:18:29 +02003715 rnp->grpnum = j % levelspread[i - 1];
Paul E. McKenneydf63fa5b2018-07-31 09:49:20 -07003716 rnp->grpmask = BIT(rnp->grpnum);
Paul E. McKenneyeb7a6652018-07-05 17:47:45 -07003717 rnp->parent = rcu_state.level[i - 1] +
Alexander Gordeev199977b2015-06-03 08:18:29 +02003718 j / levelspread[i - 1];
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003719 }
3720 rnp->level = i;
Paul E. McKenney12f5f522010-11-29 21:56:39 -08003721 INIT_LIST_HEAD(&rnp->blkd_tasks);
Paul E. McKenneydae6e642013-02-10 20:48:58 -08003722 rcu_init_one_nocb(rnp);
Paul E. McKenneyf6a12f32016-01-30 17:57:35 -08003723 init_waitqueue_head(&rnp->exp_wq[0]);
3724 init_waitqueue_head(&rnp->exp_wq[1]);
Paul E. McKenney3b5f6682016-03-16 16:47:55 -07003725 init_waitqueue_head(&rnp->exp_wq[2]);
3726 init_waitqueue_head(&rnp->exp_wq[3]);
Paul E. McKenneyf6a12f32016-01-30 17:57:35 -08003727 spin_lock_init(&rnp->exp_lock);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003728 }
3729 }
Lai Jiangshan0c340292010-03-28 11:12:30 +08003730
Paul E. McKenneyeb7a6652018-07-05 17:47:45 -07003731 init_swait_queue_head(&rcu_state.gp_wq);
3732 init_swait_queue_head(&rcu_state.expedited_wq);
Paul E. McKenneyaedf4ba2018-07-04 14:33:59 -07003733 rnp = rcu_first_leaf_node();
Lai Jiangshan0c340292010-03-28 11:12:30 +08003734 for_each_possible_cpu(i) {
Paul E. McKenney4a90a062010-04-14 16:48:11 -07003735 while (i > rnp->grphi)
Lai Jiangshan0c340292010-03-28 11:12:30 +08003736 rnp++;
Paul E. McKenneyda1df502018-07-03 15:37:16 -07003737 per_cpu_ptr(&rcu_data, i)->mynode = rnp;
Paul E. McKenney53b46302018-07-03 17:22:34 -07003738 rcu_boot_init_percpu_data(i);
Lai Jiangshan0c340292010-03-28 11:12:30 +08003739 }
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003740}
3741
Paul E. McKenneyf885b7f2012-04-23 15:52:53 -07003742/*
3743 * Compute the rcu_node tree geometry from kernel parameters. This cannot
Paul E. McKenney4102ada2013-10-08 20:23:47 -07003744 * replace the definitions in tree.h because those are needed to size
Paul E. McKenneyf885b7f2012-04-23 15:52:53 -07003745 * the ->node array in the rcu_state structure.
3746 */
3747static void __init rcu_init_geometry(void)
3748{
Paul E. McKenney026ad282013-04-03 22:14:11 -07003749 ulong d;
Paul E. McKenneyf885b7f2012-04-23 15:52:53 -07003750 int i;
Alexander Gordeev05b84ae2015-06-03 08:18:28 +02003751 int rcu_capacity[RCU_NUM_LVLS];
Paul E. McKenneyf885b7f2012-04-23 15:52:53 -07003752
Paul E. McKenney026ad282013-04-03 22:14:11 -07003753 /*
3754 * Initialize any unspecified boot parameters.
3755 * The default values of jiffies_till_first_fqs and
3756 * jiffies_till_next_fqs are set to the RCU_JIFFIES_TILL_FORCE_QS
3757 * value, which is a function of HZ, then adding one for each
3758 * RCU_JIFFIES_FQS_DIV CPUs that might be on the system.
3759 */
3760 d = RCU_JIFFIES_TILL_FORCE_QS + nr_cpu_ids / RCU_JIFFIES_FQS_DIV;
3761 if (jiffies_till_first_fqs == ULONG_MAX)
3762 jiffies_till_first_fqs = d;
3763 if (jiffies_till_next_fqs == ULONG_MAX)
3764 jiffies_till_next_fqs = d;
Paul E. McKenneyc06aed02018-07-25 11:25:23 -07003765 if (jiffies_till_sched_qs == ULONG_MAX)
3766 adjust_jiffies_till_sched_qs();
Paul E. McKenney026ad282013-04-03 22:14:11 -07003767
Paul E. McKenneyf885b7f2012-04-23 15:52:53 -07003768 /* If the compile-time values are accurate, just leave. */
Paul E. McKenney47d631a2015-04-21 09:12:13 -07003769 if (rcu_fanout_leaf == RCU_FANOUT_LEAF &&
Paul E. McKenneyb17c7032012-09-06 15:38:02 -07003770 nr_cpu_ids == NR_CPUS)
Paul E. McKenneyf885b7f2012-04-23 15:52:53 -07003771 return;
Joe Perchesa7538352018-05-14 13:27:33 -07003772 pr_info("Adjusting geometry for rcu_fanout_leaf=%d, nr_cpu_ids=%u\n",
Paul E. McKenney39479092013-10-09 15:20:33 -07003773 rcu_fanout_leaf, nr_cpu_ids);
Paul E. McKenneyf885b7f2012-04-23 15:52:53 -07003774
3775 /*
Paul E. McKenneyee968ac2015-07-31 08:28:35 -07003776 * The boot-time rcu_fanout_leaf parameter must be at least two
3777 * and cannot exceed the number of bits in the rcu_node masks.
3778 * Complain and fall back to the compile-time values if this
3779 * limit is exceeded.
Paul E. McKenneyf885b7f2012-04-23 15:52:53 -07003780 */
Paul E. McKenneyee968ac2015-07-31 08:28:35 -07003781 if (rcu_fanout_leaf < 2 ||
Alexander Gordeev75cf15a2015-06-03 08:18:23 +02003782 rcu_fanout_leaf > sizeof(unsigned long) * 8) {
Paul E. McKenney13bd64942015-06-04 10:06:01 -07003783 rcu_fanout_leaf = RCU_FANOUT_LEAF;
Paul E. McKenneyf885b7f2012-04-23 15:52:53 -07003784 WARN_ON(1);
3785 return;
3786 }
3787
Alexander Gordeev75cf15a2015-06-03 08:18:23 +02003788 /*
Paul E. McKenneyf885b7f2012-04-23 15:52:53 -07003789 * Compute number of nodes that can be handled an rcu_node tree
Alexander Gordeev96181382015-06-03 08:18:26 +02003790 * with the given number of levels.
Paul E. McKenneyf885b7f2012-04-23 15:52:53 -07003791 */
Alexander Gordeev96181382015-06-03 08:18:26 +02003792 rcu_capacity[0] = rcu_fanout_leaf;
Alexander Gordeev05b84ae2015-06-03 08:18:28 +02003793 for (i = 1; i < RCU_NUM_LVLS; i++)
Paul E. McKenneyf885b7f2012-04-23 15:52:53 -07003794 rcu_capacity[i] = rcu_capacity[i - 1] * RCU_FANOUT;
3795
3796 /*
Alexander Gordeev75cf15a2015-06-03 08:18:23 +02003797 * The tree must be able to accommodate the configured number of CPUs.
Paul E. McKenneyee968ac2015-07-31 08:28:35 -07003798 * If this limit is exceeded, fall back to the compile-time values.
Paul E. McKenneyf885b7f2012-04-23 15:52:53 -07003799 */
Paul E. McKenneyee968ac2015-07-31 08:28:35 -07003800 if (nr_cpu_ids > rcu_capacity[RCU_NUM_LVLS - 1]) {
3801 rcu_fanout_leaf = RCU_FANOUT_LEAF;
3802 WARN_ON(1);
3803 return;
3804 }
Paul E. McKenneyf885b7f2012-04-23 15:52:53 -07003805
Alexander Gordeev679f9852015-06-03 08:18:25 +02003806 /* Calculate the number of levels in the tree. */
Alexander Gordeev96181382015-06-03 08:18:26 +02003807 for (i = 0; nr_cpu_ids > rcu_capacity[i]; i++) {
Alexander Gordeev679f9852015-06-03 08:18:25 +02003808 }
Alexander Gordeev96181382015-06-03 08:18:26 +02003809 rcu_num_lvls = i + 1;
Alexander Gordeev679f9852015-06-03 08:18:25 +02003810
Paul E. McKenneyf885b7f2012-04-23 15:52:53 -07003811 /* Calculate the number of rcu_nodes at each level of the tree. */
Alexander Gordeev679f9852015-06-03 08:18:25 +02003812 for (i = 0; i < rcu_num_lvls; i++) {
Alexander Gordeev96181382015-06-03 08:18:26 +02003813 int cap = rcu_capacity[(rcu_num_lvls - 1) - i];
Alexander Gordeev679f9852015-06-03 08:18:25 +02003814 num_rcu_lvl[i] = DIV_ROUND_UP(nr_cpu_ids, cap);
3815 }
Paul E. McKenneyf885b7f2012-04-23 15:52:53 -07003816
3817 /* Calculate the total number of rcu_node structures. */
3818 rcu_num_nodes = 0;
Alexander Gordeev679f9852015-06-03 08:18:25 +02003819 for (i = 0; i < rcu_num_lvls; i++)
Paul E. McKenneyf885b7f2012-04-23 15:52:53 -07003820 rcu_num_nodes += num_rcu_lvl[i];
Paul E. McKenneyf885b7f2012-04-23 15:52:53 -07003821}
3822
Paul E. McKenneya3dc2942015-04-20 11:40:50 -07003823/*
3824 * Dump out the structure of the rcu_node combining tree associated
Paul E. McKenney49918a52018-07-07 18:12:26 -07003825 * with the rcu_state structure.
Paul E. McKenneya3dc2942015-04-20 11:40:50 -07003826 */
Paul E. McKenneyb8bb1f62018-07-03 17:22:34 -07003827static void __init rcu_dump_rcu_node_tree(void)
Paul E. McKenneya3dc2942015-04-20 11:40:50 -07003828{
3829 int level = 0;
3830 struct rcu_node *rnp;
3831
3832 pr_info("rcu_node tree layout dump\n");
3833 pr_info(" ");
Paul E. McKenneyaedf4ba2018-07-04 14:33:59 -07003834 rcu_for_each_node_breadth_first(rnp) {
Paul E. McKenneya3dc2942015-04-20 11:40:50 -07003835 if (rnp->level != level) {
3836 pr_cont("\n");
3837 pr_info(" ");
3838 level = rnp->level;
3839 }
3840 pr_cont("%d:%d ^%d ", rnp->grplo, rnp->grphi, rnp->grpnum);
3841 }
3842 pr_cont("\n");
3843}
3844
Paul E. McKenneyad7c9462018-01-08 14:35:52 -08003845struct workqueue_struct *rcu_gp_wq;
Paul E. McKenney25f3d7e2018-02-01 22:05:38 -08003846struct workqueue_struct *rcu_par_gp_wq;
Paul E. McKenneyad7c9462018-01-08 14:35:52 -08003847
Paul E. McKenney9f680ab2009-11-22 08:53:49 -08003848void __init rcu_init(void)
Paul E. McKenneyf41d9112009-08-22 13:56:52 -07003849{
Paul E. McKenney017c4262010-01-14 16:10:58 -08003850 int cpu;
Paul E. McKenney9f680ab2009-11-22 08:53:49 -08003851
Paul E. McKenney47627672015-01-19 21:10:21 -08003852 rcu_early_boot_tests();
3853
Paul E. McKenneyf41d9112009-08-22 13:56:52 -07003854 rcu_bootup_announce();
Paul E. McKenneyf885b7f2012-04-23 15:52:53 -07003855 rcu_init_geometry();
Paul E. McKenneyb8bb1f62018-07-03 17:22:34 -07003856 rcu_init_one();
Paul E. McKenneya3dc2942015-04-20 11:40:50 -07003857 if (dump_tree)
Paul E. McKenneyb8bb1f62018-07-03 17:22:34 -07003858 rcu_dump_rcu_node_tree();
Jiang Fangb5b39362013-02-02 14:13:42 -08003859 open_softirq(RCU_SOFTIRQ, rcu_process_callbacks);
Paul E. McKenney9f680ab2009-11-22 08:53:49 -08003860
3861 /*
3862 * We don't need protection against CPU-hotplug here because
3863 * this is called early in boot, before either interrupts
3864 * or the scheduler are operational.
3865 */
Borislav Petkovd1d74d12013-04-22 00:12:42 +02003866 pm_notifier(rcu_pm_notify, 0);
Paul E. McKenney7ec99de2016-06-30 13:58:26 -07003867 for_each_online_cpu(cpu) {
Thomas Gleixner4df83742016-07-13 17:17:03 +00003868 rcutree_prepare_cpu(cpu);
Paul E. McKenney7ec99de2016-06-30 13:58:26 -07003869 rcu_cpu_starting(cpu);
Paul E. McKenney9b9500d2017-08-17 17:05:59 -07003870 rcutree_online_cpu(cpu);
Paul E. McKenney7ec99de2016-06-30 13:58:26 -07003871 }
Paul E. McKenneyad7c9462018-01-08 14:35:52 -08003872
3873 /* Create workqueue for expedited GPs and for Tree SRCU. */
3874 rcu_gp_wq = alloc_workqueue("rcu_gp", WQ_MEM_RECLAIM, 0);
3875 WARN_ON(!rcu_gp_wq);
Paul E. McKenney25f3d7e2018-02-01 22:05:38 -08003876 rcu_par_gp_wq = alloc_workqueue("rcu_par_gp", WQ_MEM_RECLAIM, 0);
3877 WARN_ON(!rcu_par_gp_wq);
Paul E. McKenneye0fcba92018-08-14 08:45:54 -07003878 srcu_init();
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003879}
3880
Paul E. McKenney3549c2b2016-04-15 16:35:29 -07003881#include "tree_exp.h"
Paul E. McKenney4102ada2013-10-08 20:23:47 -07003882#include "tree_plugin.h"