blob: 0933d865089068c733e5f2123a2685c26f4b39ae [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. McKenney64db4cf2008-12-18 21:55:32 +010065
Paul E. McKenney4102ada2013-10-08 20:23:47 -070066#include "tree.h"
Paul E. McKenney29c00b42011-06-17 15:53:19 -070067#include "rcu.h"
Paul E. McKenney9f77da92009-08-22 13:56:45 -070068
Paul E. McKenney4102ada2013-10-08 20:23:47 -070069#ifdef MODULE_PARAM_PREFIX
70#undef MODULE_PARAM_PREFIX
71#endif
72#define MODULE_PARAM_PREFIX "rcutree."
73
Paul E. McKenney64db4cf2008-12-18 21:55:32 +010074/* Data structures. */
75
Steven Rostedt (Red Hat)f7f7bac2013-07-12 17:18:47 -040076/*
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -070077 * Steal a bit from the bottom of ->dynticks for idle entry/exit
78 * control. Initially this is for TLB flushing.
Steven Rostedt (Red Hat)f7f7bac2013-07-12 17:18:47 -040079 */
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -070080#define RCU_DYNTICK_CTRL_MASK 0x1
81#define RCU_DYNTICK_CTRL_CTR (RCU_DYNTICK_CTRL_MASK + 1)
82#ifndef rcu_eqs_special_exit
83#define rcu_eqs_special_exit() do { } while (0)
Ard Biesheuvela8a29b32014-07-12 19:01:49 +020084#endif
85
Paul E. McKenney4c5273b2018-08-03 21:00:38 -070086static DEFINE_PER_CPU_SHARED_ALIGNED(struct rcu_data, rcu_data) = {
87 .dynticks_nesting = 1,
88 .dynticks_nmi_nesting = DYNTICK_IRQ_NONIDLE,
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -070089 .dynticks = ATOMIC_INIT(RCU_DYNTICK_CTRL_CTR),
Paul E. McKenney4c5273b2018-08-03 21:00:38 -070090};
Paul E. McKenney358be2d2018-07-03 14:15:31 -070091struct rcu_state rcu_state = {
92 .level = { &rcu_state.node[0] },
Paul E. McKenney358be2d2018-07-03 14:15:31 -070093 .gp_state = RCU_GP_IDLE,
94 .gp_seq = (0UL - 300UL) << RCU_SEQ_CTR_SHIFT,
95 .barrier_mutex = __MUTEX_INITIALIZER(rcu_state.barrier_mutex),
96 .name = RCU_NAME,
97 .abbr = RCU_ABBR,
98 .exp_mutex = __MUTEX_INITIALIZER(rcu_state.exp_mutex),
99 .exp_wake_mutex = __MUTEX_INITIALIZER(rcu_state.exp_wake_mutex),
Mike Galbraith894d45b2018-08-15 09:05:29 -0700100 .ofl_lock = __RAW_SPIN_LOCK_UNLOCKED(rcu_state.ofl_lock),
Paul E. McKenney358be2d2018-07-03 14:15:31 -0700101};
Paul E. McKenney27f4d282011-02-07 12:47:15 -0800102
Paul E. McKenneya3dc2942015-04-20 11:40:50 -0700103/* Dump rcu_node combining tree at boot to verify correct setup. */
104static bool dump_tree;
105module_param(dump_tree, bool, 0444);
Paul E. McKenney7fa27002015-04-20 10:27:15 -0700106/* Control rcu_node-tree auto-balancing at boot time. */
107static bool rcu_fanout_exact;
108module_param(rcu_fanout_exact, bool, 0444);
Paul E. McKenney47d631a2015-04-21 09:12:13 -0700109/* Increase (but not decrease) the RCU_FANOUT_LEAF at boot time. */
110static int rcu_fanout_leaf = RCU_FANOUT_LEAF;
Paul E. McKenney7e5c2df2012-07-01 15:42:33 -0700111module_param(rcu_fanout_leaf, int, 0444);
Paul E. McKenneyf885b7f2012-04-23 15:52:53 -0700112int rcu_num_lvls __read_mostly = RCU_NUM_LVLS;
Alexander Gordeevcb007102015-06-03 08:18:30 +0200113/* Number of rcu_nodes at specified level. */
Paul E. McKenneye95d68d2017-03-15 13:11:11 -0700114int num_rcu_lvl[] = NUM_RCU_LVL_INIT;
Paul E. McKenneyf885b7f2012-04-23 15:52:53 -0700115int rcu_num_nodes __read_mostly = NUM_RCU_NODES; /* Total # rcu_nodes in use. */
Daniel Bristot de Oliveira088e9d22016-06-02 13:51:41 -0300116/* panic() on RCU Stall sysctl. */
117int sysctl_panic_on_rcu_stall __read_mostly;
Paul E. McKenneyf885b7f2012-04-23 15:52:53 -0700118
Paul E. McKenneyb0d30412011-07-10 15:57:35 -0700119/*
Paul E. McKenney52d7e482017-01-10 02:28:26 -0800120 * The rcu_scheduler_active variable is initialized to the value
121 * RCU_SCHEDULER_INACTIVE and transitions RCU_SCHEDULER_INIT just before the
122 * first task is spawned. So when this variable is RCU_SCHEDULER_INACTIVE,
123 * RCU can assume that there is but one task, allowing RCU to (for example)
Paul E. McKenney0d950922016-04-08 05:00:03 -0700124 * optimize synchronize_rcu() to a simple barrier(). When this variable
Paul E. McKenney52d7e482017-01-10 02:28:26 -0800125 * is RCU_SCHEDULER_INIT, RCU must actually do all the hard work required
126 * to detect real grace periods. This variable is also used to suppress
127 * boot-time false positives from lockdep-RCU error checking. Finally, it
128 * transitions from RCU_SCHEDULER_INIT to RCU_SCHEDULER_RUNNING after RCU
129 * is fully initialized, including all of its kthreads having been spawned.
Paul E. McKenneyb0d30412011-07-10 15:57:35 -0700130 */
Paul E. McKenneybbad9372010-04-02 16:17:17 -0700131int rcu_scheduler_active __read_mostly;
132EXPORT_SYMBOL_GPL(rcu_scheduler_active);
133
Paul E. McKenneyb0d30412011-07-10 15:57:35 -0700134/*
135 * The rcu_scheduler_fully_active variable transitions from zero to one
136 * during the early_initcall() processing, which is after the scheduler
137 * is capable of creating new tasks. So RCU processing (for example,
138 * creating tasks for RCU priority boosting) must be delayed until after
139 * rcu_scheduler_fully_active transitions from zero to one. We also
140 * currently delay invocation of any RCU callbacks until after this point.
141 *
142 * It might later prove better for people registering RCU callbacks during
143 * early boot to take responsibility for these callbacks, but one step at
144 * a time.
145 */
146static int rcu_scheduler_fully_active __read_mostly;
147
Paul E. McKenneyb50912d2018-07-03 17:22:34 -0700148static void rcu_report_qs_rnp(unsigned long mask, struct rcu_node *rnp,
149 unsigned long gps, unsigned long flags);
Paul E. McKenney0aa04b02015-01-23 21:52:37 -0800150static void rcu_init_new_rnp(struct rcu_node *rnp_leaf);
151static void rcu_cleanup_dead_rnp(struct rcu_node *rnp_leaf);
Thomas Gleixner5d01bbd2012-07-16 10:42:35 +0000152static void rcu_boost_kthread_setaffinity(struct rcu_node *rnp, int outgoingcpu);
Paul E. McKenneya46e0892011-06-15 15:47:09 -0700153static void invoke_rcu_core(void);
Paul E. McKenneyaff4e9e2018-07-03 17:22:34 -0700154static void invoke_rcu_callbacks(struct rcu_data *rdp);
Paul E. McKenney63d4c8c2018-07-03 17:22:34 -0700155static void rcu_report_exp_rdp(struct rcu_data *rdp);
Paul E. McKenney3549c2b2016-04-15 16:35:29 -0700156static void sync_sched_exp_online_cleanup(int cpu);
Paul E. McKenneya26ac242011-01-12 14:10:23 -0800157
Paul E. McKenneya94844b2014-12-12 07:37:48 -0800158/* rcuc/rcub kthread realtime priority */
Paul E. McKenney26730f52015-04-21 09:22:14 -0700159static int kthread_prio = IS_ENABLED(CONFIG_RCU_BOOST) ? 1 : 0;
Paul E. McKenneya94844b2014-12-12 07:37:48 -0800160module_param(kthread_prio, int, 0644);
161
Paul E. McKenney8d7dc922015-04-14 19:33:59 -0700162/* Delay in jiffies for grace-period initialization delays, debug only. */
Paul E. McKenney0f41c0d2015-03-10 18:33:20 -0700163
Paul E. McKenney90040c92017-05-10 14:36:55 -0700164static int gp_preinit_delay;
165module_param(gp_preinit_delay, int, 0444);
166static int gp_init_delay;
167module_param(gp_init_delay, int, 0444);
168static int gp_cleanup_delay;
169module_param(gp_cleanup_delay, int, 0444);
Paul E. McKenney0f41c0d2015-03-10 18:33:20 -0700170
Paul E. McKenney4cf439a2018-07-02 12:15:25 -0700171/* Retrieve RCU kthreads priority for rcutorture */
Joel Fernandes (Google)4babd852018-06-19 15:14:18 -0700172int rcu_get_gp_kthreads_prio(void)
173{
174 return kthread_prio;
175}
176EXPORT_SYMBOL_GPL(rcu_get_gp_kthreads_prio);
177
Paul E. McKenneyeab128e2015-04-15 12:08:22 -0700178/*
179 * Number of grace periods between delays, normalized by the duration of
Paul E. McKenneybfd090b2017-02-08 14:49:27 -0800180 * the delay. The longer the delay, the more the grace periods between
Paul E. McKenneyeab128e2015-04-15 12:08:22 -0700181 * each delay. The reason for this normalization is that it means that,
182 * for non-zero delays, the overall slowdown of grace periods is constant
183 * regardless of the duration of the delay. This arrangement balances
184 * the need for long delays to increase some race probabilities with the
185 * need for fast grace periods to increase other race probabilities.
186 */
187#define PER_RCU_NODE_PERIOD 3 /* Number of grace periods between delays. */
Paul E. McKenney37745d22015-01-22 18:24:08 -0800188
Paul E. McKenneya26ac242011-01-12 14:10:23 -0800189/*
Paul E. McKenney0aa04b02015-01-23 21:52:37 -0800190 * Compute the mask of online CPUs for the specified rcu_node structure.
191 * This will not be stable unless the rcu_node structure's ->lock is
192 * held, but the bit corresponding to the current CPU will be stable
193 * in most contexts.
194 */
195unsigned long rcu_rnp_online_cpus(struct rcu_node *rnp)
196{
Paul E. McKenney7d0ae802015-03-03 14:57:58 -0800197 return READ_ONCE(rnp->qsmaskinitnext);
Paul E. McKenney0aa04b02015-01-23 21:52:37 -0800198}
199
200/*
Paul E. McKenney7d0ae802015-03-03 14:57:58 -0800201 * Return true if an RCU grace period is in progress. The READ_ONCE()s
Paul E. McKenneyfc2219d42009-09-23 09:50:41 -0700202 * permit this function to be invoked without holding the root rcu_node
203 * structure's ->lock, but of course results can be subject to change.
204 */
Paul E. McKenneyde8e8732018-07-03 17:22:34 -0700205static int rcu_gp_in_progress(void)
Paul E. McKenneyfc2219d42009-09-23 09:50:41 -0700206{
Paul E. McKenneyde8e8732018-07-03 17:22:34 -0700207 return rcu_seq_state(rcu_seq_current(&rcu_state.gp_seq));
Paul E. McKenneyfc2219d42009-09-23 09:50:41 -0700208}
209
Paul E. McKenney903ee832018-10-02 16:05:46 -0700210/*
211 * Return the number of callbacks queued on the specified CPU.
212 * Handles both the nocbs and normal cases.
213 */
214static long rcu_get_n_cbs_cpu(int cpu)
215{
216 struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
217
218 if (rcu_segcblist_is_enabled(&rdp->cblist)) /* Online normal CPU? */
219 return rcu_segcblist_n_cbs(&rdp->cblist);
220 return rcu_get_n_cbs_nocb_cpu(rdp); /* Works for offline, too. */
221}
222
Paul E. McKenneyd28139c2018-06-28 14:45:25 -0700223void rcu_softirq_qs(void)
Ingo Molnarb1f77b02009-03-13 03:20:49 +0100224{
Paul E. McKenney45975c72018-07-02 14:30:37 -0700225 rcu_qs();
Paul E. McKenneyd28139c2018-06-28 14:45:25 -0700226 rcu_preempt_deferred_qs(current);
Ingo Molnarb1f77b02009-03-13 03:20:49 +0100227}
228
Paul E. McKenney6563de92016-11-02 13:33:57 -0700229/*
Paul E. McKenney2625d462016-11-02 14:23:30 -0700230 * Record entry into an extended quiescent state. This is only to be
231 * called when not already in an extended quiescent state.
232 */
233static void rcu_dynticks_eqs_enter(void)
234{
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -0700235 struct rcu_data *rdp = this_cpu_ptr(&rcu_data);
Paul E. McKenneyb8c17e62016-11-08 14:25:21 -0800236 int seq;
Paul E. McKenney2625d462016-11-02 14:23:30 -0700237
238 /*
Paul E. McKenneyb8c17e62016-11-08 14:25:21 -0800239 * CPUs seeing atomic_add_return() must see prior RCU read-side
Paul E. McKenney2625d462016-11-02 14:23:30 -0700240 * critical sections, and we also must force ordering with the
241 * next idle sojourn.
242 */
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -0700243 seq = atomic_add_return(RCU_DYNTICK_CTRL_CTR, &rdp->dynticks);
Paul E. McKenneyb8c17e62016-11-08 14:25:21 -0800244 /* Better be in an extended quiescent state! */
245 WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) &&
246 (seq & RCU_DYNTICK_CTRL_CTR));
247 /* Better not have special action (TLB flush) pending! */
248 WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) &&
249 (seq & RCU_DYNTICK_CTRL_MASK));
Paul E. McKenney2625d462016-11-02 14:23:30 -0700250}
251
252/*
253 * Record exit from an extended quiescent state. This is only to be
254 * called from an extended quiescent state.
255 */
256static void rcu_dynticks_eqs_exit(void)
257{
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -0700258 struct rcu_data *rdp = this_cpu_ptr(&rcu_data);
Paul E. McKenneyb8c17e62016-11-08 14:25:21 -0800259 int seq;
Paul E. McKenney2625d462016-11-02 14:23:30 -0700260
261 /*
Paul E. McKenneyb8c17e62016-11-08 14:25:21 -0800262 * CPUs seeing atomic_add_return() must see prior idle sojourns,
Paul E. McKenney2625d462016-11-02 14:23:30 -0700263 * and we also must force ordering with the next RCU read-side
264 * critical section.
265 */
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -0700266 seq = atomic_add_return(RCU_DYNTICK_CTRL_CTR, &rdp->dynticks);
Paul E. McKenneyb8c17e62016-11-08 14:25:21 -0800267 WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) &&
268 !(seq & RCU_DYNTICK_CTRL_CTR));
269 if (seq & RCU_DYNTICK_CTRL_MASK) {
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -0700270 atomic_andnot(RCU_DYNTICK_CTRL_MASK, &rdp->dynticks);
Paul E. McKenneyb8c17e62016-11-08 14:25:21 -0800271 smp_mb__after_atomic(); /* _exit after clearing mask. */
272 /* Prefer duplicate flushes to losing a flush. */
273 rcu_eqs_special_exit();
274 }
Paul E. McKenney2625d462016-11-02 14:23:30 -0700275}
276
277/*
278 * Reset the current CPU's ->dynticks counter to indicate that the
279 * newly onlined CPU is no longer in an extended quiescent state.
280 * This will either leave the counter unchanged, or increment it
281 * to the next non-quiescent value.
282 *
283 * The non-atomic test/increment sequence works because the upper bits
284 * of the ->dynticks counter are manipulated only by the corresponding CPU,
285 * or when the corresponding CPU is offline.
286 */
287static void rcu_dynticks_eqs_online(void)
288{
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -0700289 struct rcu_data *rdp = this_cpu_ptr(&rcu_data);
Paul E. McKenney2625d462016-11-02 14:23:30 -0700290
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -0700291 if (atomic_read(&rdp->dynticks) & RCU_DYNTICK_CTRL_CTR)
Paul E. McKenney2625d462016-11-02 14:23:30 -0700292 return;
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -0700293 atomic_add(RCU_DYNTICK_CTRL_CTR, &rdp->dynticks);
Paul E. McKenney2625d462016-11-02 14:23:30 -0700294}
295
296/*
Paul E. McKenney02a5c5502016-11-02 17:25:06 -0700297 * Is the current CPU in an extended quiescent state?
298 *
299 * No ordering, as we are sampling CPU-local information.
300 */
301bool rcu_dynticks_curr_cpu_in_eqs(void)
302{
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -0700303 struct rcu_data *rdp = this_cpu_ptr(&rcu_data);
Paul E. McKenney02a5c5502016-11-02 17:25:06 -0700304
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -0700305 return !(atomic_read(&rdp->dynticks) & RCU_DYNTICK_CTRL_CTR);
Paul E. McKenney02a5c5502016-11-02 17:25:06 -0700306}
307
308/*
Paul E. McKenney8b2f63a2016-11-02 14:12:05 -0700309 * Snapshot the ->dynticks counter with full ordering so as to allow
310 * stable comparison of this counter with past and future snapshots.
311 */
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -0700312int rcu_dynticks_snap(struct rcu_data *rdp)
Paul E. McKenney8b2f63a2016-11-02 14:12:05 -0700313{
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -0700314 int snap = atomic_add_return(0, &rdp->dynticks);
Paul E. McKenney8b2f63a2016-11-02 14:12:05 -0700315
Paul E. McKenneyb8c17e62016-11-08 14:25:21 -0800316 return snap & ~RCU_DYNTICK_CTRL_MASK;
Paul E. McKenney8b2f63a2016-11-02 14:12:05 -0700317}
318
319/*
Paul E. McKenney02a5c5502016-11-02 17:25:06 -0700320 * Return true if the snapshot returned from rcu_dynticks_snap()
321 * indicates that RCU is in an extended quiescent state.
322 */
323static bool rcu_dynticks_in_eqs(int snap)
324{
Paul E. McKenneyb8c17e62016-11-08 14:25:21 -0800325 return !(snap & RCU_DYNTICK_CTRL_CTR);
Paul E. McKenney02a5c5502016-11-02 17:25:06 -0700326}
327
328/*
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -0700329 * Return true if the CPU corresponding to the specified rcu_data
Paul E. McKenney02a5c5502016-11-02 17:25:06 -0700330 * structure has spent some time in an extended quiescent state since
331 * rcu_dynticks_snap() returned the specified snapshot.
332 */
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -0700333static bool rcu_dynticks_in_eqs_since(struct rcu_data *rdp, int snap)
Paul E. McKenney02a5c5502016-11-02 17:25:06 -0700334{
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -0700335 return snap != rcu_dynticks_snap(rdp);
Paul E. McKenney02a5c5502016-11-02 17:25:06 -0700336}
337
338/*
Paul E. McKenneyb8c17e62016-11-08 14:25:21 -0800339 * Set the special (bottom) bit of the specified CPU so that it
340 * will take special action (such as flushing its TLB) on the
341 * next exit from an extended quiescent state. Returns true if
342 * the bit was successfully set, or false if the CPU was not in
343 * an extended quiescent state.
344 */
345bool rcu_eqs_special_set(int cpu)
346{
347 int old;
348 int new;
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -0700349 struct rcu_data *rdp = &per_cpu(rcu_data, cpu);
Paul E. McKenneyb8c17e62016-11-08 14:25:21 -0800350
351 do {
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -0700352 old = atomic_read(&rdp->dynticks);
Paul E. McKenneyb8c17e62016-11-08 14:25:21 -0800353 if (old & RCU_DYNTICK_CTRL_CTR)
354 return false;
355 new = old | RCU_DYNTICK_CTRL_MASK;
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -0700356 } while (atomic_cmpxchg(&rdp->dynticks, old, new) != old);
Paul E. McKenneyb8c17e62016-11-08 14:25:21 -0800357 return true;
Paul E. McKenney6563de92016-11-02 13:33:57 -0700358}
Paul E. McKenney5cd37192014-12-13 20:32:04 -0800359
Paul E. McKenney4a81e832014-06-20 16:49:01 -0700360/*
361 * Let the RCU core know that this CPU has gone through the scheduler,
362 * which is a quiescent state. This is called when the need for a
363 * quiescent state is urgent, so we burn an atomic operation and full
364 * memory barriers to let the RCU core know about it, regardless of what
365 * this CPU might (or might not) do in the near future.
366 *
Paul E. McKenney0f9be8c2017-01-27 13:17:02 -0800367 * We inform the RCU core by emulating a zero-duration dyntick-idle period.
Paul E. McKenney46a5d162015-10-07 09:10:48 -0700368 *
Paul E. McKenney3b57a392018-05-16 16:01:56 -0700369 * The caller must have disabled interrupts and must not be idle.
Paul E. McKenney4a81e832014-06-20 16:49:01 -0700370 */
Paul E. McKenney395a2f02018-07-10 14:00:14 -0700371static void __maybe_unused rcu_momentary_dyntick_idle(void)
Paul E. McKenney4a81e832014-06-20 16:49:01 -0700372{
Paul E. McKenney3b57a392018-05-16 16:01:56 -0700373 int special;
374
Paul E. McKenney2dba13f2018-08-03 21:00:38 -0700375 raw_cpu_write(rcu_data.rcu_need_heavy_qs, false);
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -0700376 special = atomic_add_return(2 * RCU_DYNTICK_CTRL_CTR,
377 &this_cpu_ptr(&rcu_data)->dynticks);
Paul E. McKenney3b57a392018-05-16 16:01:56 -0700378 /* It is illegal to call this from idle state. */
379 WARN_ON_ONCE(!(special & RCU_DYNTICK_CTRL_CTR));
Paul E. McKenney3e310092018-06-21 12:50:01 -0700380 rcu_preempt_deferred_qs(current);
Paul E. McKenney4a81e832014-06-20 16:49:01 -0700381}
382
Paul E. McKenney45975c72018-07-02 14:30:37 -0700383/**
384 * rcu_is_cpu_rrupt_from_idle - see if idle or immediately interrupted from idle
Boqun Fengbb73c522015-07-30 16:55:38 -0700385 *
Paul E. McKenney45975c72018-07-02 14:30:37 -0700386 * If the current CPU is idle or running at a first-level (not nested)
387 * interrupt from idle, return true. The caller must have at least
388 * disabled preemption.
Paul E. McKenney5cd37192014-12-13 20:32:04 -0800389 */
Paul E. McKenney45975c72018-07-02 14:30:37 -0700390static int rcu_is_cpu_rrupt_from_idle(void)
Paul E. McKenney5cd37192014-12-13 20:32:04 -0800391{
Paul E. McKenney4c5273b2018-08-03 21:00:38 -0700392 return __this_cpu_read(rcu_data.dynticks_nesting) <= 0 &&
393 __this_cpu_read(rcu_data.dynticks_nmi_nesting) <= 1;
Paul E. McKenney5cd37192014-12-13 20:32:04 -0800394}
Paul E. McKenney5cd37192014-12-13 20:32:04 -0800395
Paul E. McKenney17c77982017-04-28 11:12:34 -0700396#define DEFAULT_RCU_BLIMIT 10 /* Maximum callbacks per rcu_do_batch. */
397static long blimit = DEFAULT_RCU_BLIMIT;
398#define DEFAULT_RCU_QHIMARK 10000 /* If this many pending, ignore blimit. */
399static long qhimark = DEFAULT_RCU_QHIMARK;
400#define DEFAULT_RCU_QLOMARK 100 /* Once only this many pending, use blimit. */
401static long qlowmark = DEFAULT_RCU_QLOMARK;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100402
Eric Dumazet878d7432012-10-18 04:55:36 -0700403module_param(blimit, long, 0444);
404module_param(qhimark, long, 0444);
405module_param(qlowmark, long, 0444);
Paul E. McKenney3d76c082009-09-28 07:46:32 -0700406
Paul E. McKenney026ad282013-04-03 22:14:11 -0700407static ulong jiffies_till_first_fqs = ULONG_MAX;
408static ulong jiffies_till_next_fqs = ULONG_MAX;
Paul E. McKenney8c7c4822016-01-03 20:29:57 -0800409static bool rcu_kick_kthreads;
Paul E. McKenneyd40011f2012-06-26 20:45:57 -0700410
Paul E. McKenneyc06aed02018-07-25 11:25:23 -0700411/*
412 * How long the grace period must be before we start recruiting
413 * quiescent-state help from rcu_note_context_switch().
414 */
415static ulong jiffies_till_sched_qs = ULONG_MAX;
416module_param(jiffies_till_sched_qs, ulong, 0444);
417static ulong jiffies_to_sched_qs; /* Adjusted version of above if not default */
418module_param(jiffies_to_sched_qs, ulong, 0444); /* Display only! */
419
420/*
421 * Make sure that we give the grace-period kthread time to detect any
422 * idle CPUs before taking active measures to force quiescent states.
423 * However, don't go below 100 milliseconds, adjusted upwards for really
424 * large systems.
425 */
426static void adjust_jiffies_till_sched_qs(void)
427{
428 unsigned long j;
429
430 /* If jiffies_till_sched_qs was specified, respect the request. */
431 if (jiffies_till_sched_qs != ULONG_MAX) {
432 WRITE_ONCE(jiffies_to_sched_qs, jiffies_till_sched_qs);
433 return;
434 }
435 j = READ_ONCE(jiffies_till_first_fqs) +
436 2 * READ_ONCE(jiffies_till_next_fqs);
437 if (j < HZ / 10 + nr_cpu_ids / RCU_JIFFIES_FQS_DIV)
438 j = HZ / 10 + nr_cpu_ids / RCU_JIFFIES_FQS_DIV;
439 pr_info("RCU calculated value of scheduler-enlistment delay is %ld jiffies.\n", j);
440 WRITE_ONCE(jiffies_to_sched_qs, j);
441}
442
Byungchul Park67abb962018-06-01 11:03:09 +0900443static int param_set_first_fqs_jiffies(const char *val, const struct kernel_param *kp)
444{
445 ulong j;
446 int ret = kstrtoul(val, 0, &j);
447
Paul E. McKenneyc06aed02018-07-25 11:25:23 -0700448 if (!ret) {
Byungchul Park67abb962018-06-01 11:03:09 +0900449 WRITE_ONCE(*(ulong *)kp->arg, (j > HZ) ? HZ : j);
Paul E. McKenneyc06aed02018-07-25 11:25:23 -0700450 adjust_jiffies_till_sched_qs();
451 }
Byungchul Park67abb962018-06-01 11:03:09 +0900452 return ret;
453}
454
455static int param_set_next_fqs_jiffies(const char *val, const struct kernel_param *kp)
456{
457 ulong j;
458 int ret = kstrtoul(val, 0, &j);
459
Paul E. McKenneyc06aed02018-07-25 11:25:23 -0700460 if (!ret) {
Byungchul Park67abb962018-06-01 11:03:09 +0900461 WRITE_ONCE(*(ulong *)kp->arg, (j > HZ) ? HZ : (j ?: 1));
Paul E. McKenneyc06aed02018-07-25 11:25:23 -0700462 adjust_jiffies_till_sched_qs();
463 }
Byungchul Park67abb962018-06-01 11:03:09 +0900464 return ret;
465}
466
467static struct kernel_param_ops first_fqs_jiffies_ops = {
468 .set = param_set_first_fqs_jiffies,
469 .get = param_get_ulong,
470};
471
472static struct kernel_param_ops next_fqs_jiffies_ops = {
473 .set = param_set_next_fqs_jiffies,
474 .get = param_get_ulong,
475};
476
477module_param_cb(jiffies_till_first_fqs, &first_fqs_jiffies_ops, &jiffies_till_first_fqs, 0644);
478module_param_cb(jiffies_till_next_fqs, &next_fqs_jiffies_ops, &jiffies_till_next_fqs, 0644);
Paul E. McKenney8c7c4822016-01-03 20:29:57 -0800479module_param(rcu_kick_kthreads, bool, 0644);
Paul E. McKenneyd40011f2012-06-26 20:45:57 -0700480
Paul E. McKenney8ff0b902018-07-05 17:55:14 -0700481static void force_qs_rnp(int (*f)(struct rcu_data *rdp));
Paul E. McKenneye9ecb782018-07-03 17:22:34 -0700482static void force_quiescent_state(void);
Paul E. McKenneye3950ec2014-10-21 08:03:57 -0700483static int rcu_pending(void);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100484
485/*
Paul E. McKenney17ef2fe2018-04-27 11:39:34 -0700486 * Return the number of RCU GPs completed thus far for debug & stats.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100487 */
Paul E. McKenney17ef2fe2018-04-27 11:39:34 -0700488unsigned long rcu_get_gp_seq(void)
Paul E. McKenney917963d2014-11-21 17:10:16 -0800489{
Paul E. McKenney16fc9c62018-07-03 15:54:39 -0700490 return READ_ONCE(rcu_state.gp_seq);
Paul E. McKenney917963d2014-11-21 17:10:16 -0800491}
Paul E. McKenney17ef2fe2018-04-27 11:39:34 -0700492EXPORT_SYMBOL_GPL(rcu_get_gp_seq);
Paul E. McKenney917963d2014-11-21 17:10:16 -0800493
494/*
Paul E. McKenney291783b2016-01-12 13:43:30 -0800495 * Return the number of RCU expedited batches completed thus far for
496 * debug & stats. Odd numbers mean that a batch is in progress, even
497 * numbers mean idle. The value returned will thus be roughly double
498 * the cumulative batches since boot.
499 */
500unsigned long rcu_exp_batches_completed(void)
501{
Paul E. McKenney16fc9c62018-07-03 15:54:39 -0700502 return rcu_state.expedited_sequence;
Paul E. McKenney291783b2016-01-12 13:43:30 -0800503}
504EXPORT_SYMBOL_GPL(rcu_exp_batches_completed);
505
506/*
Andreea-Cristina Bernata381d752014-03-19 11:18:31 +0200507 * Force a quiescent state.
508 */
509void rcu_force_quiescent_state(void)
510{
Paul E. McKenneye9ecb782018-07-03 17:22:34 -0700511 force_quiescent_state();
Andreea-Cristina Bernata381d752014-03-19 11:18:31 +0200512}
513EXPORT_SYMBOL_GPL(rcu_force_quiescent_state);
514
515/*
Paul E. McKenney69196012018-10-02 11:24:08 -0700516 * Convert a ->gp_state value to a character string.
517 */
518static const char *gp_state_getname(short gs)
519{
520 if (gs < 0 || gs >= ARRAY_SIZE(gp_state_names))
521 return "???";
522 return gp_state_names[gs];
523}
524
525/*
Paul E. McKenneyafea2272014-03-12 07:10:41 -0700526 * Show the state of the grace-period kthreads.
527 */
528void show_rcu_gp_kthreads(void)
529{
Paul E. McKenney47199a02018-05-28 10:33:08 -0700530 int cpu;
Paul E. McKenneyc669c012018-10-02 12:42:21 -0700531 unsigned long j;
Paul E. McKenney47199a02018-05-28 10:33:08 -0700532 struct rcu_data *rdp;
533 struct rcu_node *rnp;
Paul E. McKenneyafea2272014-03-12 07:10:41 -0700534
Paul E. McKenneyc669c012018-10-02 12:42:21 -0700535 j = jiffies - READ_ONCE(rcu_state.gp_activity);
536 pr_info("%s: wait state: %s(%d) ->state: %#lx delta ->gp_activity %ld\n",
537 rcu_state.name, gp_state_getname(rcu_state.gp_state),
538 rcu_state.gp_state, rcu_state.gp_kthread->state, j);
Paul E. McKenneyb97d23c2018-07-04 15:35:00 -0700539 rcu_for_each_node_breadth_first(rnp) {
540 if (ULONG_CMP_GE(rcu_state.gp_seq, rnp->gp_seq_needed))
541 continue;
542 pr_info("\trcu_node %d:%d ->gp_seq %lu ->gp_seq_needed %lu\n",
543 rnp->grplo, rnp->grphi, rnp->gp_seq,
544 rnp->gp_seq_needed);
545 if (!rcu_is_leaf_node(rnp))
546 continue;
547 for_each_leaf_node_possible_cpu(rnp, cpu) {
548 rdp = per_cpu_ptr(&rcu_data, cpu);
549 if (rdp->gpwrap ||
550 ULONG_CMP_GE(rcu_state.gp_seq,
551 rdp->gp_seq_needed))
Paul E. McKenney47199a02018-05-28 10:33:08 -0700552 continue;
Paul E. McKenneyb97d23c2018-07-04 15:35:00 -0700553 pr_info("\tcpu %d ->gp_seq_needed %lu\n",
554 cpu, rdp->gp_seq_needed);
Paul E. McKenney47199a02018-05-28 10:33:08 -0700555 }
Paul E. McKenneyafea2272014-03-12 07:10:41 -0700556 }
Paul E. McKenneyb97d23c2018-07-04 15:35:00 -0700557 /* sched_show_task(rcu_state.gp_kthread); */
Paul E. McKenneyafea2272014-03-12 07:10:41 -0700558}
559EXPORT_SYMBOL_GPL(show_rcu_gp_kthreads);
560
561/*
Paul E. McKenneyad0dc7f2014-02-19 10:51:42 -0800562 * Send along grace-period-related data for rcutorture diagnostics.
563 */
564void rcutorture_get_gp_data(enum rcutorture_type test_type, int *flags,
Paul E. McKenneyaebc8262018-05-01 06:42:51 -0700565 unsigned long *gp_seq)
Paul E. McKenneyad0dc7f2014-02-19 10:51:42 -0800566{
Paul E. McKenneyad0dc7f2014-02-19 10:51:42 -0800567 switch (test_type) {
568 case RCU_FLAVOR:
Paul E. McKenneyad0dc7f2014-02-19 10:51:42 -0800569 case RCU_BH_FLAVOR:
Paul E. McKenneyad0dc7f2014-02-19 10:51:42 -0800570 case RCU_SCHED_FLAVOR:
Paul E. McKenneyf7dd7d42018-07-04 15:39:40 -0700571 *flags = READ_ONCE(rcu_state.gp_flags);
572 *gp_seq = rcu_seq_current(&rcu_state.gp_seq);
Paul E. McKenneyad0dc7f2014-02-19 10:51:42 -0800573 break;
574 default:
575 break;
576 }
Paul E. McKenneyad0dc7f2014-02-19 10:51:42 -0800577}
578EXPORT_SYMBOL_GPL(rcutorture_get_gp_data);
579
580/*
Paul E. McKenney49918a52018-07-07 18:12:26 -0700581 * Return the root node of the rcu_state structure.
Paul E. McKenney365187f2014-03-10 10:55:52 -0700582 */
Paul E. McKenney336a4f62018-07-03 17:22:34 -0700583static struct rcu_node *rcu_get_root(void)
Paul E. McKenney365187f2014-03-10 10:55:52 -0700584{
Paul E. McKenney336a4f62018-07-03 17:22:34 -0700585 return &rcu_state.node[0];
Paul E. McKenney365187f2014-03-10 10:55:52 -0700586}
587
588/*
Paul E. McKenney215bba92017-10-05 16:37:03 -0700589 * Enter an RCU extended quiescent state, which can be either the
590 * idle loop or adaptive-tickless usermode execution.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100591 *
Paul E. McKenney215bba92017-10-05 16:37:03 -0700592 * We crowbar the ->dynticks_nmi_nesting field to zero to allow for
593 * the possibility of usermode upcalls having messed up our count
594 * of interrupt nesting level during the prior busy period.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100595 */
Paul E. McKenney215bba92017-10-05 16:37:03 -0700596static void rcu_eqs_enter(bool user)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100597{
Paul E. McKenney4c5273b2018-08-03 21:00:38 -0700598 struct rcu_data *rdp = this_cpu_ptr(&rcu_data);
Paul E. McKenney215bba92017-10-05 16:37:03 -0700599
Paul E. McKenney4c5273b2018-08-03 21:00:38 -0700600 WARN_ON_ONCE(rdp->dynticks_nmi_nesting != DYNTICK_IRQ_NONIDLE);
601 WRITE_ONCE(rdp->dynticks_nmi_nesting, 0);
Paul E. McKenney215bba92017-10-05 16:37:03 -0700602 WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) &&
Paul E. McKenney4c5273b2018-08-03 21:00:38 -0700603 rdp->dynticks_nesting == 0);
604 if (rdp->dynticks_nesting != 1) {
605 rdp->dynticks_nesting--;
Paul E. McKenney215bba92017-10-05 16:37:03 -0700606 return;
607 }
Paul E. McKenney96d3fd02013-10-04 14:33:34 -0700608
Frederic Weisbeckerb04db8e2017-11-06 16:01:30 +0100609 lockdep_assert_irqs_disabled();
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -0700610 trace_rcu_dyntick(TPS("Start"), rdp->dynticks_nesting, 0, rdp->dynticks);
Paul E. McKenneye68bbb22017-10-05 19:55:31 -0700611 WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) && !user && !is_idle_task(current));
Paul E. McKenneyb97d23c2018-07-04 15:35:00 -0700612 rdp = this_cpu_ptr(&rcu_data);
613 do_nocb_deferred_wakeup(rdp);
Paul E. McKenney198bbf82014-10-22 15:03:43 -0700614 rcu_prepare_for_idle();
Paul E. McKenney3e310092018-06-21 12:50:01 -0700615 rcu_preempt_deferred_qs(current);
Paul E. McKenney4c5273b2018-08-03 21:00:38 -0700616 WRITE_ONCE(rdp->dynticks_nesting, 0); /* Avoid irq-access tearing. */
Paul E. McKenney844ccdd2017-10-03 16:51:47 -0700617 rcu_dynticks_eqs_enter();
Paul E. McKenney176f8f72014-08-04 17:43:50 -0700618 rcu_dynticks_task_enter();
Frederic Weisbeckeradf50912012-06-28 11:20:21 -0700619}
620
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700621/**
622 * rcu_idle_enter - inform RCU that current CPU is entering idle
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100623 *
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700624 * Enter idle mode, in other words, -leave- the mode in which RCU
625 * read-side critical sections can occur. (Though RCU read-side
626 * critical sections can occur in irq handlers in idle, a possibility
627 * handled by irq_enter() and irq_exit().)
628 *
Paul E. McKenneyc0da3132017-09-22 09:58:47 -0700629 * If you add or remove a call to rcu_idle_enter(), be sure to test with
630 * CONFIG_RCU_EQS_DEBUG=y.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100631 */
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700632void rcu_idle_enter(void)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100633{
Frederic Weisbeckerb04db8e2017-11-06 16:01:30 +0100634 lockdep_assert_irqs_disabled();
Paul E. McKenneycb349ca2012-09-04 17:35:31 -0700635 rcu_eqs_enter(false);
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700636}
637
Paul E. McKenneyd1ec4c32015-05-13 10:41:58 -0700638#ifdef CONFIG_NO_HZ_FULL
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700639/**
Frederic Weisbeckeradf50912012-06-28 11:20:21 -0700640 * rcu_user_enter - inform RCU that we are resuming userspace.
641 *
642 * Enter RCU idle mode right before resuming userspace. No use of RCU
643 * is permitted between this call and rcu_user_exit(). This way the
644 * CPU doesn't need to maintain the tick for RCU maintenance purposes
645 * when the CPU runs in userspace.
Paul E. McKenneyc0da3132017-09-22 09:58:47 -0700646 *
647 * If you add or remove a call to rcu_user_enter(), be sure to test with
648 * CONFIG_RCU_EQS_DEBUG=y.
Frederic Weisbeckeradf50912012-06-28 11:20:21 -0700649 */
650void rcu_user_enter(void)
651{
Frederic Weisbeckerb04db8e2017-11-06 16:01:30 +0100652 lockdep_assert_irqs_disabled();
Paul E. McKenneyd4db30a2017-07-12 09:03:35 -0700653 rcu_eqs_enter(true);
Frederic Weisbeckeradf50912012-06-28 11:20:21 -0700654}
Paul E. McKenneyd1ec4c32015-05-13 10:41:58 -0700655#endif /* CONFIG_NO_HZ_FULL */
Frederic Weisbecker19dd15912012-06-04 16:42:35 -0700656
Byungchul Parkcf7614e2018-06-22 15:12:06 +0900657/*
Paul E. McKenneyfd581a92017-10-02 21:56:20 -0700658 * If we are returning from the outermost NMI handler that interrupted an
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -0700659 * RCU-idle period, update rdp->dynticks and rdp->dynticks_nmi_nesting
Paul E. McKenneyfd581a92017-10-02 21:56:20 -0700660 * to let the RCU grace-period handling know that the CPU is back to
661 * being RCU-idle.
662 *
Byungchul Parkcf7614e2018-06-22 15:12:06 +0900663 * If you add or remove a call to rcu_nmi_exit_common(), be sure to test
Paul E. McKenneyfd581a92017-10-02 21:56:20 -0700664 * with CONFIG_RCU_EQS_DEBUG=y.
665 */
Byungchul Parkcf7614e2018-06-22 15:12:06 +0900666static __always_inline void rcu_nmi_exit_common(bool irq)
Paul E. McKenneyfd581a92017-10-02 21:56:20 -0700667{
Paul E. McKenney4c5273b2018-08-03 21:00:38 -0700668 struct rcu_data *rdp = this_cpu_ptr(&rcu_data);
Paul E. McKenneyfd581a92017-10-02 21:56:20 -0700669
670 /*
671 * Check for ->dynticks_nmi_nesting underflow and bad ->dynticks.
672 * (We are exiting an NMI handler, so RCU better be paying attention
673 * to us!)
674 */
Paul E. McKenney4c5273b2018-08-03 21:00:38 -0700675 WARN_ON_ONCE(rdp->dynticks_nmi_nesting <= 0);
Paul E. McKenneyfd581a92017-10-02 21:56:20 -0700676 WARN_ON_ONCE(rcu_dynticks_curr_cpu_in_eqs());
677
678 /*
679 * If the nesting level is not 1, the CPU wasn't RCU-idle, so
680 * leave it in non-RCU-idle state.
681 */
Paul E. McKenney4c5273b2018-08-03 21:00:38 -0700682 if (rdp->dynticks_nmi_nesting != 1) {
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -0700683 trace_rcu_dyntick(TPS("--="), rdp->dynticks_nmi_nesting, rdp->dynticks_nmi_nesting - 2, rdp->dynticks);
Paul E. McKenney4c5273b2018-08-03 21:00:38 -0700684 WRITE_ONCE(rdp->dynticks_nmi_nesting, /* No store tearing. */
685 rdp->dynticks_nmi_nesting - 2);
Paul E. McKenneyfd581a92017-10-02 21:56:20 -0700686 return;
687 }
688
689 /* This NMI interrupted an RCU-idle CPU, restore RCU-idleness. */
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -0700690 trace_rcu_dyntick(TPS("Startirq"), rdp->dynticks_nmi_nesting, 0, rdp->dynticks);
Paul E. McKenney4c5273b2018-08-03 21:00:38 -0700691 WRITE_ONCE(rdp->dynticks_nmi_nesting, 0); /* Avoid store tearing. */
Byungchul Parkcf7614e2018-06-22 15:12:06 +0900692
693 if (irq)
694 rcu_prepare_for_idle();
695
Paul E. McKenneyfd581a92017-10-02 21:56:20 -0700696 rcu_dynticks_eqs_enter();
Byungchul Parkcf7614e2018-06-22 15:12:06 +0900697
698 if (irq)
699 rcu_dynticks_task_enter();
700}
701
702/**
703 * rcu_nmi_exit - inform RCU of exit from NMI context
704 * @irq: Is this call from rcu_irq_exit?
705 *
706 * If you add or remove a call to rcu_nmi_exit(), be sure to test
707 * with CONFIG_RCU_EQS_DEBUG=y.
708 */
709void rcu_nmi_exit(void)
710{
711 rcu_nmi_exit_common(false);
Paul E. McKenneyfd581a92017-10-02 21:56:20 -0700712}
713
714/**
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700715 * rcu_irq_exit - inform RCU that current CPU is exiting irq towards idle
716 *
717 * Exit from an interrupt handler, which might possibly result in entering
718 * idle mode, in other words, leaving the mode in which read-side critical
Paul E. McKenney7c9906c2015-10-31 00:59:01 -0700719 * sections can occur. The caller must have disabled interrupts.
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700720 *
721 * This code assumes that the idle loop never does anything that might
722 * result in unbalanced calls to irq_enter() and irq_exit(). If your
Paul E. McKenney58721f52017-10-03 10:42:22 -0700723 * architecture's idle loop violates this assumption, RCU will give you what
724 * you deserve, good and hard. But very infrequently and irreproducibly.
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700725 *
726 * Use things like work queues to work around this limitation.
727 *
728 * You have been warned.
Paul E. McKenneyc0da3132017-09-22 09:58:47 -0700729 *
730 * If you add or remove a call to rcu_irq_exit(), be sure to test with
731 * CONFIG_RCU_EQS_DEBUG=y.
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700732 */
733void rcu_irq_exit(void)
734{
Frederic Weisbeckerb04db8e2017-11-06 16:01:30 +0100735 lockdep_assert_irqs_disabled();
Byungchul Parkcf7614e2018-06-22 15:12:06 +0900736 rcu_nmi_exit_common(true);
Paul E. McKenney7c9906c2015-10-31 00:59:01 -0700737}
738
739/*
740 * Wrapper for rcu_irq_exit() where interrupts are enabled.
Paul E. McKenneyc0da3132017-09-22 09:58:47 -0700741 *
742 * If you add or remove a call to rcu_irq_exit_irqson(), be sure to test
743 * with CONFIG_RCU_EQS_DEBUG=y.
Paul E. McKenney7c9906c2015-10-31 00:59:01 -0700744 */
745void rcu_irq_exit_irqson(void)
746{
747 unsigned long flags;
748
749 local_irq_save(flags);
750 rcu_irq_exit();
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700751 local_irq_restore(flags);
752}
753
754/*
Frederic Weisbeckeradf50912012-06-28 11:20:21 -0700755 * Exit an RCU extended quiescent state, which can be either the
756 * idle loop or adaptive-tickless usermode execution.
Paul E. McKenney51a1fd32017-10-03 14:43:40 -0700757 *
758 * We crowbar the ->dynticks_nmi_nesting field to DYNTICK_IRQ_NONIDLE to
759 * allow for the possibility of usermode upcalls messing up our count of
760 * interrupt nesting level during the busy period that is just now starting.
Frederic Weisbeckeradf50912012-06-28 11:20:21 -0700761 */
762static void rcu_eqs_exit(bool user)
763{
Paul E. McKenney4c5273b2018-08-03 21:00:38 -0700764 struct rcu_data *rdp;
Paul E. McKenney84585aa2017-10-04 15:55:16 -0700765 long oldval;
Frederic Weisbeckeradf50912012-06-28 11:20:21 -0700766
Frederic Weisbeckerb04db8e2017-11-06 16:01:30 +0100767 lockdep_assert_irqs_disabled();
Paul E. McKenney4c5273b2018-08-03 21:00:38 -0700768 rdp = this_cpu_ptr(&rcu_data);
769 oldval = rdp->dynticks_nesting;
Paul E. McKenney1ce46ee2015-05-05 23:04:22 -0700770 WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) && oldval < 0);
Paul E. McKenney51a1fd32017-10-03 14:43:40 -0700771 if (oldval) {
Paul E. McKenney4c5273b2018-08-03 21:00:38 -0700772 rdp->dynticks_nesting++;
Paul E. McKenney9dd238e2017-10-05 16:56:26 -0700773 return;
Paul E. McKenney3a5924052013-10-04 18:48:55 -0700774 }
Paul E. McKenney9dd238e2017-10-05 16:56:26 -0700775 rcu_dynticks_task_exit();
776 rcu_dynticks_eqs_exit();
777 rcu_cleanup_after_idle();
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -0700778 trace_rcu_dyntick(TPS("End"), rdp->dynticks_nesting, 1, rdp->dynticks);
Paul E. McKenneye68bbb22017-10-05 19:55:31 -0700779 WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) && !user && !is_idle_task(current));
Paul E. McKenney4c5273b2018-08-03 21:00:38 -0700780 WRITE_ONCE(rdp->dynticks_nesting, 1);
781 WARN_ON_ONCE(rdp->dynticks_nmi_nesting);
782 WRITE_ONCE(rdp->dynticks_nmi_nesting, DYNTICK_IRQ_NONIDLE);
Frederic Weisbeckeradf50912012-06-28 11:20:21 -0700783}
784
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700785/**
786 * rcu_idle_exit - inform RCU that current CPU is leaving idle
787 *
788 * Exit idle mode, in other words, -enter- the mode in which RCU
789 * read-side critical sections can occur.
790 *
Paul E. McKenneyc0da3132017-09-22 09:58:47 -0700791 * If you add or remove a call to rcu_idle_exit(), be sure to test with
792 * CONFIG_RCU_EQS_DEBUG=y.
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700793 */
794void rcu_idle_exit(void)
795{
Frederic Weisbeckerc5d900b2012-07-11 20:26:31 +0200796 unsigned long flags;
797
798 local_irq_save(flags);
Paul E. McKenneycb349ca2012-09-04 17:35:31 -0700799 rcu_eqs_exit(false);
Frederic Weisbeckerc5d900b2012-07-11 20:26:31 +0200800 local_irq_restore(flags);
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700801}
802
Paul E. McKenneyd1ec4c32015-05-13 10:41:58 -0700803#ifdef CONFIG_NO_HZ_FULL
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700804/**
Frederic Weisbeckeradf50912012-06-28 11:20:21 -0700805 * rcu_user_exit - inform RCU that we are exiting userspace.
806 *
807 * Exit RCU idle mode while entering the kernel because it can
808 * run a RCU read side critical section anytime.
Paul E. McKenneyc0da3132017-09-22 09:58:47 -0700809 *
810 * If you add or remove a call to rcu_user_exit(), be sure to test with
811 * CONFIG_RCU_EQS_DEBUG=y.
Frederic Weisbeckeradf50912012-06-28 11:20:21 -0700812 */
813void rcu_user_exit(void)
814{
Frederic Weisbecker91d1aa432012-11-27 19:33:25 +0100815 rcu_eqs_exit(1);
Frederic Weisbeckeradf50912012-06-28 11:20:21 -0700816}
Paul E. McKenneyd1ec4c32015-05-13 10:41:58 -0700817#endif /* CONFIG_NO_HZ_FULL */
Frederic Weisbecker19dd15912012-06-04 16:42:35 -0700818
819/**
Byungchul Parkcf7614e2018-06-22 15:12:06 +0900820 * rcu_nmi_enter_common - inform RCU of entry to NMI context
821 * @irq: Is this call from rcu_irq_enter?
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100822 *
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -0700823 * If the CPU was idle from RCU's viewpoint, update rdp->dynticks and
Paul E. McKenney4c5273b2018-08-03 21:00:38 -0700824 * rdp->dynticks_nmi_nesting to let the RCU grace-period handling know
Paul E. McKenney734d1682014-11-21 14:45:12 -0800825 * that the CPU is active. This implementation permits nested NMIs, as
826 * long as the nesting level does not overflow an int. (You will probably
827 * run out of stack space first.)
Paul E. McKenneyc0da3132017-09-22 09:58:47 -0700828 *
Byungchul Parkcf7614e2018-06-22 15:12:06 +0900829 * If you add or remove a call to rcu_nmi_enter_common(), be sure to test
Paul E. McKenneyc0da3132017-09-22 09:58:47 -0700830 * with CONFIG_RCU_EQS_DEBUG=y.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100831 */
Byungchul Parkcf7614e2018-06-22 15:12:06 +0900832static __always_inline void rcu_nmi_enter_common(bool irq)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100833{
Paul E. McKenney4c5273b2018-08-03 21:00:38 -0700834 struct rcu_data *rdp = this_cpu_ptr(&rcu_data);
Paul E. McKenney84585aa2017-10-04 15:55:16 -0700835 long incby = 2;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100836
Paul E. McKenney734d1682014-11-21 14:45:12 -0800837 /* Complain about underflow. */
Paul E. McKenney4c5273b2018-08-03 21:00:38 -0700838 WARN_ON_ONCE(rdp->dynticks_nmi_nesting < 0);
Paul E. McKenney734d1682014-11-21 14:45:12 -0800839
840 /*
841 * If idle from RCU viewpoint, atomically increment ->dynticks
842 * to mark non-idle and increment ->dynticks_nmi_nesting by one.
843 * Otherwise, increment ->dynticks_nmi_nesting by two. This means
844 * if ->dynticks_nmi_nesting is equal to one, we are guaranteed
845 * to be in the outermost NMI handler that interrupted an RCU-idle
846 * period (observation due to Andy Lutomirski).
847 */
Paul E. McKenney02a5c5502016-11-02 17:25:06 -0700848 if (rcu_dynticks_curr_cpu_in_eqs()) {
Byungchul Parkcf7614e2018-06-22 15:12:06 +0900849
850 if (irq)
851 rcu_dynticks_task_exit();
852
Paul E. McKenney2625d462016-11-02 14:23:30 -0700853 rcu_dynticks_eqs_exit();
Byungchul Parkcf7614e2018-06-22 15:12:06 +0900854
855 if (irq)
856 rcu_cleanup_after_idle();
857
Paul E. McKenney734d1682014-11-21 14:45:12 -0800858 incby = 1;
859 }
Paul E. McKenneybd2b8792017-10-04 12:29:01 -0700860 trace_rcu_dyntick(incby == 1 ? TPS("Endirq") : TPS("++="),
Paul E. McKenney4c5273b2018-08-03 21:00:38 -0700861 rdp->dynticks_nmi_nesting,
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -0700862 rdp->dynticks_nmi_nesting + incby, rdp->dynticks);
Paul E. McKenney4c5273b2018-08-03 21:00:38 -0700863 WRITE_ONCE(rdp->dynticks_nmi_nesting, /* Prevent store tearing. */
864 rdp->dynticks_nmi_nesting + incby);
Paul E. McKenney734d1682014-11-21 14:45:12 -0800865 barrier();
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100866}
867
868/**
Byungchul Parkcf7614e2018-06-22 15:12:06 +0900869 * rcu_nmi_enter - inform RCU of entry to NMI context
870 */
871void rcu_nmi_enter(void)
872{
873 rcu_nmi_enter_common(false);
874}
875
876/**
Paul E. McKenney34240692011-10-03 11:38:52 -0700877 * rcu_irq_enter - inform RCU that current CPU is entering irq away from idle
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100878 *
Paul E. McKenney34240692011-10-03 11:38:52 -0700879 * Enter an interrupt handler, which might possibly result in exiting
880 * idle mode, in other words, entering the mode in which read-side critical
881 * sections can occur. The caller must have disabled interrupts.
Paul E. McKenneyc0da3132017-09-22 09:58:47 -0700882 *
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700883 * Note that the Linux kernel is fully capable of entering an interrupt
Paul E. McKenney58721f52017-10-03 10:42:22 -0700884 * handler that it never exits, for example when doing upcalls to user mode!
885 * This code assumes that the idle loop never does upcalls to user mode.
886 * If your architecture's idle loop does do upcalls to user mode (or does
887 * anything else that results in unbalanced calls to the irq_enter() and
888 * irq_exit() functions), RCU will give you what you deserve, good and hard.
889 * But very infrequently and irreproducibly.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100890 *
891 * Use things like work queues to work around this limitation.
892 *
893 * You have been warned.
894 *
895 * If you add or remove a call to rcu_irq_enter(), be sure to test with
896 * CONFIG_RCU_EQS_DEBUG=y.
Paul E. McKenney23b5c8f2010-09-07 10:38:22 -0700897 */
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100898void rcu_irq_enter(void)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100899{
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100900 lockdep_assert_irqs_disabled();
Byungchul Parkcf7614e2018-06-22 15:12:06 +0900901 rcu_nmi_enter_common(true);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100902}
Paul E. McKenney734d1682014-11-21 14:45:12 -0800903
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100904/*
905 * Wrapper for rcu_irq_enter() where interrupts are enabled.
906 *
907 * If you add or remove a call to rcu_irq_enter_irqson(), be sure to test
908 * with CONFIG_RCU_EQS_DEBUG=y.
909 */
910void rcu_irq_enter_irqson(void)
911{
912 unsigned long flags;
Paul E. McKenney734d1682014-11-21 14:45:12 -0800913
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100914 local_irq_save(flags);
915 rcu_irq_enter();
916 local_irq_restore(flags);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100917}
918
919/**
Zhouyi Zhou2320bda2018-10-08 06:50:41 +0000920 * rcu_is_watching - see if RCU thinks that the current CPU is not idle
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100921 *
Paul E. McKenney791875d2017-05-03 11:06:05 -0700922 * Return true if RCU is watching the running CPU, which means that this
923 * CPU can safely enter RCU read-side critical sections. In other words,
Zhouyi Zhou2320bda2018-10-08 06:50:41 +0000924 * if the current CPU is not in its idle loop or is in an interrupt or
925 * NMI handler, return true.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100926 */
Linus Torvaldsb29c8302013-11-16 12:23:18 -0800927bool notrace rcu_is_watching(void)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100928{
Pranith Kumarf534ed12014-07-08 18:26:11 -0400929 bool ret;
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700930
Alexei Starovoitov46f00d12015-06-16 10:35:18 -0700931 preempt_disable_notrace();
Paul E. McKenney791875d2017-05-03 11:06:05 -0700932 ret = !rcu_dynticks_curr_cpu_in_eqs();
Alexei Starovoitov46f00d12015-06-16 10:35:18 -0700933 preempt_enable_notrace();
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700934 return ret;
935}
Paul E. McKenney5c173eb2013-09-13 17:20:11 -0700936EXPORT_SYMBOL_GPL(rcu_is_watching);
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700937
Paul E. McKenneybcbfdd02017-04-11 15:50:41 -0700938/*
939 * If a holdout task is actually running, request an urgent quiescent
940 * state from its CPU. This is unsynchronized, so migrations can cause
941 * the request to go to the wrong CPU. Which is OK, all that will happen
942 * is that the CPU's next context switch will be a bit slower and next
943 * time around this task will generate another request.
944 */
945void rcu_request_urgent_qs_task(struct task_struct *t)
946{
947 int cpu;
948
949 barrier();
950 cpu = task_cpu(t);
951 if (!task_curr(t))
952 return; /* This task is not running on that CPU. */
Paul E. McKenney2dba13f2018-08-03 21:00:38 -0700953 smp_store_release(per_cpu_ptr(&rcu_data.rcu_urgent_qs, cpu), true);
Paul E. McKenneybcbfdd02017-04-11 15:50:41 -0700954}
955
Paul E. McKenney62fde6e2012-05-22 22:10:24 -0700956#if defined(CONFIG_PROVE_RCU) && defined(CONFIG_HOTPLUG_CPU)
Paul E. McKenneyc0d6d012012-01-23 12:41:26 -0800957
958/*
Paul E. McKenney55547882018-05-15 10:14:34 -0700959 * Is the current CPU online as far as RCU is concerned?
Paul E. McKenney2036d942012-01-30 17:02:47 -0800960 *
Paul E. McKenney55547882018-05-15 10:14:34 -0700961 * Disable preemption to avoid false positives that could otherwise
962 * happen due to the current CPU number being sampled, this task being
963 * preempted, its old CPU being taken offline, resuming on some other CPU,
Paul E. McKenney49918a52018-07-07 18:12:26 -0700964 * then determining that its old CPU is now offline.
Paul E. McKenneyc0d6d012012-01-23 12:41:26 -0800965 *
Paul E. McKenney55547882018-05-15 10:14:34 -0700966 * Disable checking if in an NMI handler because we cannot safely
967 * report errors from NMI handlers anyway. In addition, it is OK to use
968 * RCU on an offline processor during initial boot, hence the check for
969 * rcu_scheduler_fully_active.
Paul E. McKenneyc0d6d012012-01-23 12:41:26 -0800970 */
971bool rcu_lockdep_current_cpu_online(void)
972{
Paul E. McKenney2036d942012-01-30 17:02:47 -0800973 struct rcu_data *rdp;
974 struct rcu_node *rnp;
Paul E. McKenneyb97d23c2018-07-04 15:35:00 -0700975 bool ret = false;
Paul E. McKenneyc0d6d012012-01-23 12:41:26 -0800976
Paul E. McKenney55547882018-05-15 10:14:34 -0700977 if (in_nmi() || !rcu_scheduler_fully_active)
Fengguang Wuf6f7ee92013-10-10 11:08:33 -0700978 return true;
Paul E. McKenneyc0d6d012012-01-23 12:41:26 -0800979 preempt_disable();
Paul E. McKenneyb97d23c2018-07-04 15:35:00 -0700980 rdp = this_cpu_ptr(&rcu_data);
981 rnp = rdp->mynode;
982 if (rdp->grpmask & rcu_rnp_online_cpus(rnp))
983 ret = true;
Paul E. McKenneyc0d6d012012-01-23 12:41:26 -0800984 preempt_enable();
Paul E. McKenneyb97d23c2018-07-04 15:35:00 -0700985 return ret;
Paul E. McKenneyc0d6d012012-01-23 12:41:26 -0800986}
987EXPORT_SYMBOL_GPL(rcu_lockdep_current_cpu_online);
988
Paul E. McKenney62fde6e2012-05-22 22:10:24 -0700989#endif /* #if defined(CONFIG_PROVE_RCU) && defined(CONFIG_HOTPLUG_CPU) */
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700990
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100991/*
Paul E. McKenney9b9500d2017-08-17 17:05:59 -0700992 * We are reporting a quiescent state on behalf of some other CPU, so
993 * it is our responsibility to check for and handle potential overflow
Paul E. McKenneya66ae8a2018-04-27 18:06:08 -0700994 * of the rcu_node ->gp_seq counter with respect to the rcu_data counters.
Paul E. McKenney9b9500d2017-08-17 17:05:59 -0700995 * After all, the CPU might be in deep idle state, and thus executing no
996 * code whatsoever.
997 */
998static void rcu_gpnum_ovf(struct rcu_node *rnp, struct rcu_data *rdp)
999{
Matthew Wilcoxa32e01e2018-01-17 06:24:30 -08001000 raw_lockdep_assert_held_rcu_node(rnp);
Paul E. McKenneya66ae8a2018-04-27 18:06:08 -07001001 if (ULONG_CMP_LT(rcu_seq_current(&rdp->gp_seq) + ULONG_MAX / 4,
1002 rnp->gp_seq))
Paul E. McKenney9b9500d2017-08-17 17:05:59 -07001003 WRITE_ONCE(rdp->gpwrap, true);
Paul E. McKenney8aa670c2018-04-28 14:15:40 -07001004 if (ULONG_CMP_LT(rdp->rcu_iw_gp_seq + ULONG_MAX / 4, rnp->gp_seq))
1005 rdp->rcu_iw_gp_seq = rnp->gp_seq + ULONG_MAX / 4;
Paul E. McKenney9b9500d2017-08-17 17:05:59 -07001006}
1007
1008/*
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001009 * Snapshot the specified CPU's dynticks counter so that we can later
1010 * credit them with an implicit quiescent state. Return 1 if this CPU
Paul E. McKenney1eba8f82009-09-23 09:50:42 -07001011 * is in dynticks idle mode, which is an extended quiescent state.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001012 */
Paul E. McKenneyfe5ac722017-05-11 11:26:22 -07001013static int dyntick_save_progress_counter(struct rcu_data *rdp)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001014{
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -07001015 rdp->dynticks_snap = rcu_dynticks_snap(rdp);
Paul E. McKenney02a5c5502016-11-02 17:25:06 -07001016 if (rcu_dynticks_in_eqs(rdp->dynticks_snap)) {
Paul E. McKenney88d1bea2018-07-04 14:45:00 -07001017 trace_rcu_fqs(rcu_state.name, rdp->gp_seq, rdp->cpu, TPS("dti"));
Paul E. McKenney9b9500d2017-08-17 17:05:59 -07001018 rcu_gpnum_ovf(rdp->mynode, rdp);
Paul E. McKenney23a9bac2015-12-13 08:57:10 -08001019 return 1;
Andreea-Cristina Bernat7941dbd2014-03-17 18:33:28 +02001020 }
Paul E. McKenney23a9bac2015-12-13 08:57:10 -08001021 return 0;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001022}
1023
1024/*
Paul E. McKenney9b9500d2017-08-17 17:05:59 -07001025 * Handler for the irq_work request posted when a grace period has
1026 * gone on for too long, but not yet long enough for an RCU CPU
1027 * stall warning. Set state appropriately, but just complain if
1028 * there is unexpected state on entry.
1029 */
1030static void rcu_iw_handler(struct irq_work *iwp)
1031{
1032 struct rcu_data *rdp;
1033 struct rcu_node *rnp;
1034
1035 rdp = container_of(iwp, struct rcu_data, rcu_iw);
1036 rnp = rdp->mynode;
1037 raw_spin_lock_rcu_node(rnp);
1038 if (!WARN_ON_ONCE(!rdp->rcu_iw_pending)) {
Paul E. McKenney8aa670c2018-04-28 14:15:40 -07001039 rdp->rcu_iw_gp_seq = rnp->gp_seq;
Paul E. McKenney9b9500d2017-08-17 17:05:59 -07001040 rdp->rcu_iw_pending = false;
1041 }
1042 raw_spin_unlock_rcu_node(rnp);
1043}
1044
1045/*
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001046 * Return true if the specified CPU has passed through a quiescent
1047 * state by virtue of being in or having passed through an dynticks
1048 * idle state since the last call to dyntick_save_progress_counter()
Paul E. McKenneya82dcc72012-08-01 14:29:20 -07001049 * for this same CPU, or by virtue of having been offline.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001050 */
Paul E. McKenneyfe5ac722017-05-11 11:26:22 -07001051static int rcu_implicit_dynticks_qs(struct rcu_data *rdp)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001052{
Paul E. McKenney3a19b462016-11-30 11:21:21 -08001053 unsigned long jtsq;
Paul E. McKenney0f9be8c2017-01-27 13:17:02 -08001054 bool *rnhqp;
Paul E. McKenney9226b102017-01-27 14:17:50 -08001055 bool *ruqp;
Paul E. McKenney9b9500d2017-08-17 17:05:59 -07001056 struct rcu_node *rnp = rdp->mynode;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001057
1058 /*
1059 * If the CPU passed through or entered a dynticks idle phase with
1060 * no active irq/NMI handlers, then we can safely pretend that the CPU
1061 * already acknowledged the request to pass through a quiescent
1062 * state. Either way, that CPU cannot possibly be in an RCU
1063 * read-side critical section that started before the beginning
1064 * of the current RCU grace period.
1065 */
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -07001066 if (rcu_dynticks_in_eqs_since(rdp, rdp->dynticks_snap)) {
Paul E. McKenney88d1bea2018-07-04 14:45:00 -07001067 trace_rcu_fqs(rcu_state.name, rdp->gp_seq, rdp->cpu, TPS("dti"));
Paul E. McKenney9b9500d2017-08-17 17:05:59 -07001068 rcu_gpnum_ovf(rnp, rdp);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001069 return 1;
1070 }
1071
Paul E. McKenneyf2e2df52018-05-15 16:23:23 -07001072 /* If waiting too long on an offline CPU, complain. */
1073 if (!(rdp->grpmask & rcu_rnp_online_cpus(rnp)) &&
Paul E. McKenney88d1bea2018-07-04 14:45:00 -07001074 time_after(jiffies, rcu_state.gp_start + HZ)) {
Paul E. McKenneyf2e2df52018-05-15 16:23:23 -07001075 bool onl;
1076 struct rcu_node *rnp1;
1077
1078 WARN_ON(1); /* Offline CPUs are supposed to report QS! */
1079 pr_info("%s: grp: %d-%d level: %d ->gp_seq %ld ->completedqs %ld\n",
1080 __func__, rnp->grplo, rnp->grphi, rnp->level,
1081 (long)rnp->gp_seq, (long)rnp->completedqs);
1082 for (rnp1 = rnp; rnp1; rnp1 = rnp1->parent)
1083 pr_info("%s: %d:%d ->qsmask %#lx ->qsmaskinit %#lx ->qsmaskinitnext %#lx ->rcu_gp_init_mask %#lx\n",
1084 __func__, rnp1->grplo, rnp1->grphi, rnp1->qsmask, rnp1->qsmaskinit, rnp1->qsmaskinitnext, rnp1->rcu_gp_init_mask);
1085 onl = !!(rdp->grpmask & rcu_rnp_online_cpus(rnp));
1086 pr_info("%s %d: %c online: %ld(%d) offline: %ld(%d)\n",
1087 __func__, rdp->cpu, ".o"[onl],
1088 (long)rdp->rcu_onl_gp_seq, rdp->rcu_onl_gp_flags,
1089 (long)rdp->rcu_ofl_gp_seq, rdp->rcu_ofl_gp_flags);
1090 return 1; /* Break things loose after complaining. */
1091 }
1092
Paul E. McKenney65d798f2013-04-12 16:19:10 -07001093 /*
Paul E. McKenney4a81e832014-06-20 16:49:01 -07001094 * A CPU running for an extended time within the kernel can
Paul E. McKenneyc06aed02018-07-25 11:25:23 -07001095 * delay RCU grace periods: (1) At age jiffies_to_sched_qs,
1096 * set .rcu_urgent_qs, (2) At age 2*jiffies_to_sched_qs, set
Paul E. McKenney7e28c5a2018-07-11 08:09:28 -07001097 * both .rcu_need_heavy_qs and .rcu_urgent_qs. Note that the
1098 * unsynchronized assignments to the per-CPU rcu_need_heavy_qs
1099 * variable are safe because the assignments are repeated if this
1100 * CPU failed to pass through a quiescent state. This code
Paul E. McKenneyc06aed02018-07-25 11:25:23 -07001101 * also checks .jiffies_resched in case jiffies_to_sched_qs
Paul E. McKenney7e28c5a2018-07-11 08:09:28 -07001102 * is set way high.
Paul E. McKenney65d798f2013-04-12 16:19:10 -07001103 */
Paul E. McKenneyc06aed02018-07-25 11:25:23 -07001104 jtsq = READ_ONCE(jiffies_to_sched_qs);
Paul E. McKenney2dba13f2018-08-03 21:00:38 -07001105 ruqp = per_cpu_ptr(&rcu_data.rcu_urgent_qs, rdp->cpu);
1106 rnhqp = &per_cpu(rcu_data.rcu_need_heavy_qs, rdp->cpu);
Paul E. McKenney0f9be8c2017-01-27 13:17:02 -08001107 if (!READ_ONCE(*rnhqp) &&
Paul E. McKenney7e28c5a2018-07-11 08:09:28 -07001108 (time_after(jiffies, rcu_state.gp_start + jtsq * 2) ||
Paul E. McKenney88d1bea2018-07-04 14:45:00 -07001109 time_after(jiffies, rcu_state.jiffies_resched))) {
Paul E. McKenney0f9be8c2017-01-27 13:17:02 -08001110 WRITE_ONCE(*rnhqp, true);
Paul E. McKenney9226b102017-01-27 14:17:50 -08001111 /* Store rcu_need_heavy_qs before rcu_urgent_qs. */
1112 smp_store_release(ruqp, true);
Paul E. McKenney7e28c5a2018-07-11 08:09:28 -07001113 } else if (time_after(jiffies, rcu_state.gp_start + jtsq)) {
1114 WRITE_ONCE(*ruqp, true);
Paul E. McKenney6193c762013-09-23 13:57:18 -07001115 }
1116
Paul E. McKenney28053bc2016-12-01 11:31:31 -08001117 /*
Paul E. McKenneyd3052102018-07-25 11:49:47 -07001118 * NO_HZ_FULL CPUs can run in-kernel without rcu_check_callbacks!
1119 * The above code handles this, but only for straight cond_resched().
1120 * And some in-kernel loops check need_resched() before calling
1121 * cond_resched(), which defeats the above code for CPUs that are
1122 * running in-kernel with scheduling-clock interrupts disabled.
1123 * So hit them over the head with the resched_cpu() hammer!
Paul E. McKenney28053bc2016-12-01 11:31:31 -08001124 */
Paul E. McKenneyd3052102018-07-25 11:49:47 -07001125 if (tick_nohz_full_cpu(rdp->cpu) &&
1126 time_after(jiffies,
1127 READ_ONCE(rdp->last_fqs_resched) + jtsq * 3)) {
Paul E. McKenney28053bc2016-12-01 11:31:31 -08001128 resched_cpu(rdp->cpu);
Paul E. McKenneyd3052102018-07-25 11:49:47 -07001129 WRITE_ONCE(rdp->last_fqs_resched, jiffies);
1130 }
1131
1132 /*
1133 * If more than halfway to RCU CPU stall-warning time, invoke
1134 * resched_cpu() more frequently to try to loosen things up a bit.
1135 * Also check to see if the CPU is getting hammered with interrupts,
1136 * but only once per grace period, just to keep the IPIs down to
1137 * a dull roar.
Paul E. McKenney49149502015-12-11 13:48:43 -08001138 */
Paul E. McKenney7e28c5a2018-07-11 08:09:28 -07001139 if (time_after(jiffies, rcu_state.jiffies_resched)) {
Paul E. McKenneyd3052102018-07-25 11:49:47 -07001140 if (time_after(jiffies,
1141 READ_ONCE(rdp->last_fqs_resched) + jtsq)) {
1142 resched_cpu(rdp->cpu);
1143 WRITE_ONCE(rdp->last_fqs_resched, jiffies);
1144 }
Paul E. McKenney9b9500d2017-08-17 17:05:59 -07001145 if (IS_ENABLED(CONFIG_IRQ_WORK) &&
Paul E. McKenney8aa670c2018-04-28 14:15:40 -07001146 !rdp->rcu_iw_pending && rdp->rcu_iw_gp_seq != rnp->gp_seq &&
Paul E. McKenney9b9500d2017-08-17 17:05:59 -07001147 (rnp->ffmask & rdp->grpmask)) {
1148 init_irq_work(&rdp->rcu_iw, rcu_iw_handler);
1149 rdp->rcu_iw_pending = true;
Paul E. McKenney8aa670c2018-04-28 14:15:40 -07001150 rdp->rcu_iw_gp_seq = rnp->gp_seq;
Paul E. McKenney9b9500d2017-08-17 17:05:59 -07001151 irq_work_queue_on(&rdp->rcu_iw, rdp->cpu);
1152 }
1153 }
Paul E. McKenney49149502015-12-11 13:48:43 -08001154
Paul E. McKenneya82dcc72012-08-01 14:29:20 -07001155 return 0;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001156}
1157
Paul E. McKenneyad3832e2018-07-03 17:22:34 -07001158static void record_gp_stall_check_time(void)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001159{
Paul E. McKenneycb1e78c2013-12-04 18:42:03 -08001160 unsigned long j = jiffies;
Paul E. McKenney6193c762013-09-23 13:57:18 -07001161 unsigned long j1;
Paul E. McKenney26cdfed2013-09-04 10:51:13 -07001162
Paul E. McKenneyad3832e2018-07-03 17:22:34 -07001163 rcu_state.gp_start = j;
Paul E. McKenney6193c762013-09-23 13:57:18 -07001164 j1 = rcu_jiffies_till_stall_check();
Paul E. McKenney91f63ce2018-05-01 15:05:45 -07001165 /* Record ->gp_start before ->jiffies_stall. */
Paul E. McKenneyad3832e2018-07-03 17:22:34 -07001166 smp_store_release(&rcu_state.jiffies_stall, j + j1); /* ^^^ */
1167 rcu_state.jiffies_resched = j + j1 / 2;
1168 rcu_state.n_force_qs_gpstart = READ_ONCE(rcu_state.n_force_qs);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001169}
1170
Paul E. McKenneyb637a322012-09-19 16:58:38 -07001171/*
Paul E. McKenneyfb81a442014-12-17 08:35:02 -08001172 * Complain about starvation of grace-period kthread.
1173 */
Paul E. McKenney8fd119b2018-07-03 17:22:34 -07001174static void rcu_check_gp_kthread_starvation(void)
Paul E. McKenneyfb81a442014-12-17 08:35:02 -08001175{
Paul E. McKenney7cba4772018-07-04 18:25:59 -07001176 struct task_struct *gpk = rcu_state.gp_kthread;
Paul E. McKenneyfb81a442014-12-17 08:35:02 -08001177 unsigned long j;
1178
Paul E. McKenney7cba4772018-07-04 18:25:59 -07001179 j = jiffies - READ_ONCE(rcu_state.gp_activity);
1180 if (j > 2 * HZ) {
Paul E. McKenney78c5a672018-04-27 13:32:28 -07001181 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 -07001182 rcu_state.name, j,
1183 (long)rcu_seq_current(&rcu_state.gp_seq),
1184 rcu_state.gp_flags,
1185 gp_state_getname(rcu_state.gp_state), rcu_state.gp_state,
1186 gpk ? gpk->state : ~0, gpk ? task_cpu(gpk) : -1);
1187 if (gpk) {
Paul E. McKenneyd07aee22018-01-11 12:08:20 -08001188 pr_err("RCU grace-period kthread stack dump:\n");
Paul E. McKenney7cba4772018-07-04 18:25:59 -07001189 sched_show_task(gpk);
1190 wake_up_process(gpk);
Paul E. McKenney86057b82015-12-31 08:48:36 -08001191 }
Paul E. McKenneyb1adb3e2015-10-01 10:38:16 -07001192 }
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001193}
1194
Paul E. McKenneyb637a322012-09-19 16:58:38 -07001195/*
Paul E. McKenney7aa92232016-11-29 05:49:06 -08001196 * Dump stacks of all tasks running on stalled CPUs. First try using
1197 * NMIs, but fall back to manual remote stack tracing on architectures
1198 * that don't support NMI-based stack dumps. The NMI-triggered stack
1199 * traces are more accurate because they are printed by the target CPU.
Paul E. McKenneyb637a322012-09-19 16:58:38 -07001200 */
Paul E. McKenney33dbdbf2018-07-03 17:22:34 -07001201static void rcu_dump_cpu_stacks(void)
Paul E. McKenneyb637a322012-09-19 16:58:38 -07001202{
1203 int cpu;
1204 unsigned long flags;
1205 struct rcu_node *rnp;
1206
Paul E. McKenneyaedf4ba2018-07-04 14:33:59 -07001207 rcu_for_each_leaf_node(rnp) {
Paul E. McKenney6cf10082015-10-08 15:36:54 -07001208 raw_spin_lock_irqsave_rcu_node(rnp, flags);
Paul E. McKenney7aa92232016-11-29 05:49:06 -08001209 for_each_leaf_node_possible_cpu(rnp, cpu)
1210 if (rnp->qsmask & leaf_node_cpu_bit(rnp, cpu))
1211 if (!trigger_single_cpu_backtrace(cpu))
Mark Rutlandbc75e992016-06-03 15:20:04 +01001212 dump_cpu_task(cpu);
Boqun Feng67c583a72015-12-29 12:18:47 +08001213 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
Paul E. McKenneyb637a322012-09-19 16:58:38 -07001214 }
1215}
1216
Paul E. McKenney8c7c4822016-01-03 20:29:57 -08001217/*
1218 * If too much time has passed in the current grace period, and if
1219 * so configured, go kick the relevant kthreads.
1220 */
Paul E. McKenneye1741c62018-07-03 17:22:34 -07001221static void rcu_stall_kick_kthreads(void)
Paul E. McKenney8c7c4822016-01-03 20:29:57 -08001222{
1223 unsigned long j;
1224
1225 if (!rcu_kick_kthreads)
1226 return;
Paul E. McKenney4c6ed432018-07-05 00:02:29 -07001227 j = READ_ONCE(rcu_state.jiffies_kick_kthreads);
1228 if (time_after(jiffies, j) && rcu_state.gp_kthread &&
1229 (rcu_gp_in_progress() || READ_ONCE(rcu_state.gp_flags))) {
1230 WARN_ONCE(1, "Kicking %s grace-period kthread\n",
1231 rcu_state.name);
Paul E. McKenney5dffed12016-02-17 11:54:28 -08001232 rcu_ftrace_dump(DUMP_ALL);
Paul E. McKenney4c6ed432018-07-05 00:02:29 -07001233 wake_up_process(rcu_state.gp_kthread);
1234 WRITE_ONCE(rcu_state.jiffies_kick_kthreads, j + HZ);
Paul E. McKenney8c7c4822016-01-03 20:29:57 -08001235 }
1236}
1237
Paul E. McKenney95394e62018-05-17 11:33:17 -07001238static void panic_on_rcu_stall(void)
Daniel Bristot de Oliveira088e9d22016-06-02 13:51:41 -03001239{
1240 if (sysctl_panic_on_rcu_stall)
1241 panic("RCU Stall\n");
1242}
1243
Paul E. McKenneya91e7e52018-07-03 17:22:34 -07001244static void print_other_cpu_stall(unsigned long gp_seq)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001245{
1246 int cpu;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001247 unsigned long flags;
Paul E. McKenney6ccd2ec2014-12-11 10:20:59 -08001248 unsigned long gpa;
1249 unsigned long j;
Paul E. McKenney285fe292012-05-09 08:45:12 -07001250 int ndetected = 0;
Paul E. McKenney336a4f62018-07-03 17:22:34 -07001251 struct rcu_node *rnp = rcu_get_root();
Paul E. McKenney53bb8572012-09-21 16:35:25 -07001252 long totqlen = 0;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001253
Paul E. McKenney8c7c4822016-01-03 20:29:57 -08001254 /* Kick and suppress, if so configured. */
Paul E. McKenneye1741c62018-07-03 17:22:34 -07001255 rcu_stall_kick_kthreads();
Paul E. McKenney8c7c4822016-01-03 20:29:57 -08001256 if (rcu_cpu_stall_suppress)
1257 return;
1258
Paul E. McKenney8cdd32a2010-08-09 14:23:03 -07001259 /*
1260 * OK, time to rat on our buddy...
1261 * See Documentation/RCU/stallwarn.txt for info on how to debug
1262 * RCU CPU stall warnings.
1263 */
Paul E. McKenney4c6ed432018-07-05 00:02:29 -07001264 pr_err("INFO: %s detected stalls on CPUs/tasks:", rcu_state.name);
Paul E. McKenneya858af22012-01-16 13:29:10 -08001265 print_cpu_stall_info_begin();
Paul E. McKenneyaedf4ba2018-07-04 14:33:59 -07001266 rcu_for_each_leaf_node(rnp) {
Paul E. McKenney6cf10082015-10-08 15:36:54 -07001267 raw_spin_lock_irqsave_rcu_node(rnp, flags);
Paul E. McKenney9bc8b552011-08-13 13:31:47 -07001268 ndetected += rcu_print_task_stall(rnp);
Paul E. McKenneyc8020a62012-08-10 16:55:59 -07001269 if (rnp->qsmask != 0) {
Mark Rutlandbc75e992016-06-03 15:20:04 +01001270 for_each_leaf_node_possible_cpu(rnp, cpu)
1271 if (rnp->qsmask & leaf_node_cpu_bit(rnp, cpu)) {
Paul E. McKenneyb21ebed2018-07-03 17:22:34 -07001272 print_cpu_stall_info(cpu);
Paul E. McKenneyc8020a62012-08-10 16:55:59 -07001273 ndetected++;
1274 }
1275 }
Boqun Feng67c583a72015-12-29 12:18:47 +08001276 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001277 }
Paul E. McKenneya858af22012-01-16 13:29:10 -08001278
Paul E. McKenneya858af22012-01-16 13:29:10 -08001279 print_cpu_stall_info_end();
Paul E. McKenney53bb8572012-09-21 16:35:25 -07001280 for_each_possible_cpu(cpu)
Paul E. McKenney903ee832018-10-02 16:05:46 -07001281 totqlen += rcu_get_n_cbs_cpu(cpu);
Paul E. McKenney471f87c32018-04-30 13:09:17 -07001282 pr_cont("(detected by %d, t=%ld jiffies, g=%ld, q=%lu)\n",
Paul E. McKenney4c6ed432018-07-05 00:02:29 -07001283 smp_processor_id(), (long)(jiffies - rcu_state.gp_start),
1284 (long)rcu_seq_current(&rcu_state.gp_seq), totqlen);
Paul E. McKenney6ccd2ec2014-12-11 10:20:59 -08001285 if (ndetected) {
Paul E. McKenney33dbdbf2018-07-03 17:22:34 -07001286 rcu_dump_cpu_stacks();
Byungchul Parkc4402b22016-11-09 17:57:13 +09001287
1288 /* Complain about tasks blocking the grace period. */
Paul E. McKenneya2887cd2018-07-03 17:22:34 -07001289 rcu_print_detail_task_stall();
Paul E. McKenney6ccd2ec2014-12-11 10:20:59 -08001290 } else {
Paul E. McKenney4c6ed432018-07-05 00:02:29 -07001291 if (rcu_seq_current(&rcu_state.gp_seq) != gp_seq) {
Paul E. McKenney6ccd2ec2014-12-11 10:20:59 -08001292 pr_err("INFO: Stall ended before state dump start\n");
1293 } else {
1294 j = jiffies;
Paul E. McKenney4c6ed432018-07-05 00:02:29 -07001295 gpa = READ_ONCE(rcu_state.gp_activity);
Paul E. McKenney237a0f22015-01-22 14:32:06 -08001296 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 -07001297 rcu_state.name, j - gpa, j, gpa,
Paul E. McKenneyc06aed02018-07-25 11:25:23 -07001298 READ_ONCE(jiffies_till_next_fqs),
Paul E. McKenney336a4f62018-07-03 17:22:34 -07001299 rcu_get_root()->qsmask);
Paul E. McKenney6ccd2ec2014-12-11 10:20:59 -08001300 /* In this case, the current CPU might be at fault. */
1301 sched_show_task(current);
1302 }
1303 }
Paul E. McKenney8c42b1f2018-04-09 11:04:46 -07001304 /* Rewrite if needed in case of slow consoles. */
Paul E. McKenney4c6ed432018-07-05 00:02:29 -07001305 if (ULONG_CMP_GE(jiffies, READ_ONCE(rcu_state.jiffies_stall)))
1306 WRITE_ONCE(rcu_state.jiffies_stall,
Paul E. McKenney8c42b1f2018-04-09 11:04:46 -07001307 jiffies + 3 * rcu_jiffies_till_stall_check() + 3);
Ingo Molnarc1dc0b92009-08-02 11:28:21 +02001308
Paul E. McKenney8fd119b2018-07-03 17:22:34 -07001309 rcu_check_gp_kthread_starvation();
Paul E. McKenneyfb81a442014-12-17 08:35:02 -08001310
Daniel Bristot de Oliveira088e9d22016-06-02 13:51:41 -03001311 panic_on_rcu_stall();
1312
Paul E. McKenneye9ecb782018-07-03 17:22:34 -07001313 force_quiescent_state(); /* Kick them all. */
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001314}
1315
Paul E. McKenney4e8b8e02018-07-03 17:22:34 -07001316static void print_cpu_stall(void)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001317{
Paul E. McKenney53bb8572012-09-21 16:35:25 -07001318 int cpu;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001319 unsigned long flags;
Paul E. McKenneyda1df502018-07-03 15:37:16 -07001320 struct rcu_data *rdp = this_cpu_ptr(&rcu_data);
Paul E. McKenney336a4f62018-07-03 17:22:34 -07001321 struct rcu_node *rnp = rcu_get_root();
Paul E. McKenney53bb8572012-09-21 16:35:25 -07001322 long totqlen = 0;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001323
Paul E. McKenney8c7c4822016-01-03 20:29:57 -08001324 /* Kick and suppress, if so configured. */
Paul E. McKenneye1741c62018-07-03 17:22:34 -07001325 rcu_stall_kick_kthreads();
Paul E. McKenney8c7c4822016-01-03 20:29:57 -08001326 if (rcu_cpu_stall_suppress)
1327 return;
1328
Paul E. McKenney8cdd32a2010-08-09 14:23:03 -07001329 /*
1330 * OK, time to rat on ourselves...
1331 * See Documentation/RCU/stallwarn.txt for info on how to debug
1332 * RCU CPU stall warnings.
1333 */
Paul E. McKenney4c6ed432018-07-05 00:02:29 -07001334 pr_err("INFO: %s self-detected stall on CPU", rcu_state.name);
Paul E. McKenneya858af22012-01-16 13:29:10 -08001335 print_cpu_stall_info_begin();
Paul E. McKenney9b9500d2017-08-17 17:05:59 -07001336 raw_spin_lock_irqsave_rcu_node(rdp->mynode, flags);
Paul E. McKenneyb21ebed2018-07-03 17:22:34 -07001337 print_cpu_stall_info(smp_processor_id());
Paul E. McKenney9b9500d2017-08-17 17:05:59 -07001338 raw_spin_unlock_irqrestore_rcu_node(rdp->mynode, flags);
Paul E. McKenneya858af22012-01-16 13:29:10 -08001339 print_cpu_stall_info_end();
Paul E. McKenney53bb8572012-09-21 16:35:25 -07001340 for_each_possible_cpu(cpu)
Paul E. McKenney903ee832018-10-02 16:05:46 -07001341 totqlen += rcu_get_n_cbs_cpu(cpu);
Paul E. McKenney471f87c32018-04-30 13:09:17 -07001342 pr_cont(" (t=%lu jiffies g=%ld q=%lu)\n",
Paul E. McKenney4c6ed432018-07-05 00:02:29 -07001343 jiffies - rcu_state.gp_start,
1344 (long)rcu_seq_current(&rcu_state.gp_seq), totqlen);
Paul E. McKenneyfb81a442014-12-17 08:35:02 -08001345
Paul E. McKenney8fd119b2018-07-03 17:22:34 -07001346 rcu_check_gp_kthread_starvation();
Paul E. McKenneyfb81a442014-12-17 08:35:02 -08001347
Paul E. McKenney33dbdbf2018-07-03 17:22:34 -07001348 rcu_dump_cpu_stacks();
Ingo Molnarc1dc0b92009-08-02 11:28:21 +02001349
Paul E. McKenney6cf10082015-10-08 15:36:54 -07001350 raw_spin_lock_irqsave_rcu_node(rnp, flags);
Paul E. McKenney8c42b1f2018-04-09 11:04:46 -07001351 /* Rewrite if needed in case of slow consoles. */
Paul E. McKenney4c6ed432018-07-05 00:02:29 -07001352 if (ULONG_CMP_GE(jiffies, READ_ONCE(rcu_state.jiffies_stall)))
1353 WRITE_ONCE(rcu_state.jiffies_stall,
Paul E. McKenney7d0ae802015-03-03 14:57:58 -08001354 jiffies + 3 * rcu_jiffies_till_stall_check() + 3);
Boqun Feng67c583a72015-12-29 12:18:47 +08001355 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
Ingo Molnarc1dc0b92009-08-02 11:28:21 +02001356
Daniel Bristot de Oliveira088e9d22016-06-02 13:51:41 -03001357 panic_on_rcu_stall();
1358
Peter Zijlstrab021fe32013-09-17 09:30:55 +02001359 /*
1360 * Attempt to revive the RCU machinery by forcing a context switch.
1361 *
1362 * A context switch would normally allow the RCU state machine to make
1363 * progress and it could be we're stuck in kernel space without context
1364 * switches for an entirely unreasonable amount of time.
1365 */
Paul E. McKenneyfced9c82018-07-26 13:44:00 -07001366 set_tsk_need_resched(current);
1367 set_preempt_need_resched();
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001368}
1369
Paul E. McKenneyea12ff22018-07-03 17:22:34 -07001370static void check_cpu_stall(struct rcu_data *rdp)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001371{
Paul E. McKenney471f87c32018-04-30 13:09:17 -07001372 unsigned long gs1;
1373 unsigned long gs2;
Paul E. McKenney26cdfed2013-09-04 10:51:13 -07001374 unsigned long gps;
Paul E. McKenneybad6e132011-05-02 23:40:04 -07001375 unsigned long j;
Paul E. McKenney8c42b1f2018-04-09 11:04:46 -07001376 unsigned long jn;
Paul E. McKenneybad6e132011-05-02 23:40:04 -07001377 unsigned long js;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001378 struct rcu_node *rnp;
1379
Paul E. McKenney8c7c4822016-01-03 20:29:57 -08001380 if ((rcu_cpu_stall_suppress && !rcu_kick_kthreads) ||
Paul E. McKenneyde8e8732018-07-03 17:22:34 -07001381 !rcu_gp_in_progress())
Paul E. McKenneyc68de202010-04-15 10:12:40 -07001382 return;
Paul E. McKenneye1741c62018-07-03 17:22:34 -07001383 rcu_stall_kick_kthreads();
Paul E. McKenneycb1e78c2013-12-04 18:42:03 -08001384 j = jiffies;
Paul E. McKenney26cdfed2013-09-04 10:51:13 -07001385
1386 /*
1387 * Lots of memory barriers to reject false positives.
1388 *
Paul E. McKenney4c6ed432018-07-05 00:02:29 -07001389 * The idea is to pick up rcu_state.gp_seq, then
1390 * rcu_state.jiffies_stall, then rcu_state.gp_start, and finally
1391 * another copy of rcu_state.gp_seq. These values are updated in
1392 * the opposite order with memory barriers (or equivalent) during
1393 * grace-period initialization and cleanup. Now, a false positive
1394 * can occur if we get an new value of rcu_state.gp_start and a old
1395 * value of rcu_state.jiffies_stall. But given the memory barriers,
1396 * the only way that this can happen is if one grace period ends
1397 * and another starts between these two fetches. This is detected
1398 * by comparing the second fetch of rcu_state.gp_seq with the
1399 * previous fetch from rcu_state.gp_seq.
Paul E. McKenney26cdfed2013-09-04 10:51:13 -07001400 *
Paul E. McKenney4c6ed432018-07-05 00:02:29 -07001401 * Given this check, comparisons of jiffies, rcu_state.jiffies_stall,
1402 * and rcu_state.gp_start suffice to forestall false positives.
Paul E. McKenney26cdfed2013-09-04 10:51:13 -07001403 */
Paul E. McKenney4c6ed432018-07-05 00:02:29 -07001404 gs1 = READ_ONCE(rcu_state.gp_seq);
Paul E. McKenney471f87c32018-04-30 13:09:17 -07001405 smp_rmb(); /* Pick up ->gp_seq first... */
Paul E. McKenney4c6ed432018-07-05 00:02:29 -07001406 js = READ_ONCE(rcu_state.jiffies_stall);
Paul E. McKenney26cdfed2013-09-04 10:51:13 -07001407 smp_rmb(); /* ...then ->jiffies_stall before the rest... */
Paul E. McKenney4c6ed432018-07-05 00:02:29 -07001408 gps = READ_ONCE(rcu_state.gp_start);
Paul E. McKenney471f87c32018-04-30 13:09:17 -07001409 smp_rmb(); /* ...and finally ->gp_start before ->gp_seq again. */
Paul E. McKenney4c6ed432018-07-05 00:02:29 -07001410 gs2 = READ_ONCE(rcu_state.gp_seq);
Paul E. McKenney471f87c32018-04-30 13:09:17 -07001411 if (gs1 != gs2 ||
Paul E. McKenney26cdfed2013-09-04 10:51:13 -07001412 ULONG_CMP_LT(j, js) ||
1413 ULONG_CMP_GE(gps, js))
1414 return; /* No stall or GP completed since entering function. */
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001415 rnp = rdp->mynode;
Paul E. McKenney8c42b1f2018-04-09 11:04:46 -07001416 jn = jiffies + 3 * rcu_jiffies_till_stall_check() + 3;
Paul E. McKenneyde8e8732018-07-03 17:22:34 -07001417 if (rcu_gp_in_progress() &&
Paul E. McKenney8c42b1f2018-04-09 11:04:46 -07001418 (READ_ONCE(rnp->qsmask) & rdp->grpmask) &&
Paul E. McKenney4c6ed432018-07-05 00:02:29 -07001419 cmpxchg(&rcu_state.jiffies_stall, js, jn) == js) {
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001420
1421 /* We haven't checked in, so go dump stack. */
Paul E. McKenney4e8b8e02018-07-03 17:22:34 -07001422 print_cpu_stall();
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001423
Paul E. McKenneyde8e8732018-07-03 17:22:34 -07001424 } else if (rcu_gp_in_progress() &&
Paul E. McKenney8c42b1f2018-04-09 11:04:46 -07001425 ULONG_CMP_GE(j, js + RCU_STALL_RAT_DELAY) &&
Paul E. McKenney4c6ed432018-07-05 00:02:29 -07001426 cmpxchg(&rcu_state.jiffies_stall, js, jn) == js) {
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001427
Paul E. McKenneybad6e132011-05-02 23:40:04 -07001428 /* They had a few time units to dump stack, so complain. */
Paul E. McKenneya91e7e52018-07-03 17:22:34 -07001429 print_other_cpu_stall(gs2);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001430 }
1431}
1432
Paul E. McKenney53d84e02010-08-10 14:28:53 -07001433/**
1434 * rcu_cpu_stall_reset - prevent further stall warnings in current grace period
1435 *
1436 * Set the stall-warning timeout way off into the future, thus preventing
1437 * any RCU CPU stall-warning messages from appearing in the current set of
1438 * RCU grace periods.
1439 *
1440 * The caller must disable hard irqs.
1441 */
1442void rcu_cpu_stall_reset(void)
1443{
Paul E. McKenneyb97d23c2018-07-04 15:35:00 -07001444 WRITE_ONCE(rcu_state.jiffies_stall, jiffies + ULONG_MAX / 2);
Paul E. McKenney53d84e02010-08-10 14:28:53 -07001445}
1446
Paul E. McKenney41e80592018-04-12 11:24:09 -07001447/* Trace-event wrapper function for trace_rcu_future_grace_period. */
1448static void trace_rcu_this_gp(struct rcu_node *rnp, struct rcu_data *rdp,
Joel Fernandesb73de912018-05-20 21:42:18 -07001449 unsigned long gp_seq_req, const char *s)
Paul E. McKenney0446be42012-12-30 15:21:01 -08001450{
Paul E. McKenney88d1bea2018-07-04 14:45:00 -07001451 trace_rcu_future_grace_period(rcu_state.name, rnp->gp_seq, gp_seq_req,
Paul E. McKenneyabd13fd2018-05-01 13:08:46 -07001452 rnp->level, rnp->grplo, rnp->grphi, s);
Paul E. McKenney0446be42012-12-30 15:21:01 -08001453}
1454
1455/*
Joel Fernandesb73de912018-05-20 21:42:18 -07001456 * rcu_start_this_gp - Request the start of a particular grace period
Joel Fernandes (Google)df2bf8f2018-05-22 23:38:14 -07001457 * @rnp_start: The leaf node of the CPU from which to start.
Joel Fernandesb73de912018-05-20 21:42:18 -07001458 * @rdp: The rcu_data corresponding to the CPU from which to start.
1459 * @gp_seq_req: The gp_seq of the grace period to start.
1460 *
Paul E. McKenney41e80592018-04-12 11:24:09 -07001461 * Start the specified grace period, as needed to handle newly arrived
Paul E. McKenney0446be42012-12-30 15:21:01 -08001462 * callbacks. The required future grace periods are recorded in each
Paul E. McKenney7a1d0f22018-05-01 10:26:57 -07001463 * rcu_node structure's ->gp_seq_needed field. Returns true if there
Paul E. McKenney48a76392014-03-11 13:02:16 -07001464 * is reason to awaken the grace-period kthread.
Paul E. McKenney0446be42012-12-30 15:21:01 -08001465 *
Paul E. McKenneyd5cd9682018-04-12 10:45:06 -07001466 * The caller must hold the specified rcu_node structure's ->lock, which
1467 * is why the caller is responsible for waking the grace-period kthread.
Joel Fernandesb73de912018-05-20 21:42:18 -07001468 *
1469 * Returns true if the GP thread needs to be awakened else false.
Paul E. McKenney0446be42012-12-30 15:21:01 -08001470 */
Joel Fernandes (Google)df2bf8f2018-05-22 23:38:14 -07001471static bool rcu_start_this_gp(struct rcu_node *rnp_start, struct rcu_data *rdp,
Joel Fernandesb73de912018-05-20 21:42:18 -07001472 unsigned long gp_seq_req)
Paul E. McKenney0446be42012-12-30 15:21:01 -08001473{
Paul E. McKenney48a76392014-03-11 13:02:16 -07001474 bool ret = false;
Joel Fernandes (Google)df2bf8f2018-05-22 23:38:14 -07001475 struct rcu_node *rnp;
Paul E. McKenney0446be42012-12-30 15:21:01 -08001476
Paul E. McKenney360e0da2018-04-12 11:50:41 -07001477 /*
1478 * Use funnel locking to either acquire the root rcu_node
1479 * structure's lock or bail out if the need for this grace period
Joel Fernandes (Google)df2bf8f2018-05-22 23:38:14 -07001480 * has already been recorded -- or if that grace period has in
1481 * fact already started. If there is already a grace period in
1482 * progress in a non-leaf node, no recording is needed because the
1483 * end of the grace period will scan the leaf rcu_node structures.
1484 * Note that rnp_start->lock must not be released.
Paul E. McKenney360e0da2018-04-12 11:50:41 -07001485 */
Joel Fernandes (Google)df2bf8f2018-05-22 23:38:14 -07001486 raw_lockdep_assert_held_rcu_node(rnp_start);
1487 trace_rcu_this_gp(rnp_start, rdp, gp_seq_req, TPS("Startleaf"));
1488 for (rnp = rnp_start; 1; rnp = rnp->parent) {
1489 if (rnp != rnp_start)
1490 raw_spin_lock_rcu_node(rnp);
1491 if (ULONG_CMP_GE(rnp->gp_seq_needed, gp_seq_req) ||
1492 rcu_seq_started(&rnp->gp_seq, gp_seq_req) ||
1493 (rnp != rnp_start &&
1494 rcu_seq_state(rcu_seq_current(&rnp->gp_seq)))) {
1495 trace_rcu_this_gp(rnp, rdp, gp_seq_req,
Joel Fernandesb73de912018-05-20 21:42:18 -07001496 TPS("Prestarted"));
Paul E. McKenney360e0da2018-04-12 11:50:41 -07001497 goto unlock_out;
1498 }
Joel Fernandes (Google)df2bf8f2018-05-22 23:38:14 -07001499 rnp->gp_seq_needed = gp_seq_req;
Joel Fernandes (Google)226ca5e2018-05-22 23:38:15 -07001500 if (rcu_seq_state(rcu_seq_current(&rnp->gp_seq))) {
Paul E. McKenneya2165e42018-05-12 07:42:20 -07001501 /*
Joel Fernandes (Google)226ca5e2018-05-22 23:38:15 -07001502 * We just marked the leaf or internal node, and a
1503 * grace period is in progress, which means that
1504 * rcu_gp_cleanup() will see the marking. Bail to
1505 * reduce contention.
Paul E. McKenneya2165e42018-05-12 07:42:20 -07001506 */
Joel Fernandes (Google)df2bf8f2018-05-22 23:38:14 -07001507 trace_rcu_this_gp(rnp_start, rdp, gp_seq_req,
Joel Fernandesb73de912018-05-20 21:42:18 -07001508 TPS("Startedleaf"));
Paul E. McKenneya2165e42018-05-12 07:42:20 -07001509 goto unlock_out;
1510 }
Joel Fernandes (Google)df2bf8f2018-05-22 23:38:14 -07001511 if (rnp != rnp_start && rnp->parent != NULL)
1512 raw_spin_unlock_rcu_node(rnp);
1513 if (!rnp->parent)
Paul E. McKenney360e0da2018-04-12 11:50:41 -07001514 break; /* At root, and perhaps also leaf. */
Paul E. McKenney0446be42012-12-30 15:21:01 -08001515 }
1516
Paul E. McKenney360e0da2018-04-12 11:50:41 -07001517 /* If GP already in progress, just leave, otherwise start one. */
Paul E. McKenneyde8e8732018-07-03 17:22:34 -07001518 if (rcu_gp_in_progress()) {
Joel Fernandes (Google)df2bf8f2018-05-22 23:38:14 -07001519 trace_rcu_this_gp(rnp, rdp, gp_seq_req, TPS("Startedleafroot"));
Paul E. McKenney0446be42012-12-30 15:21:01 -08001520 goto unlock_out;
1521 }
Joel Fernandes (Google)df2bf8f2018-05-22 23:38:14 -07001522 trace_rcu_this_gp(rnp, rdp, gp_seq_req, TPS("Startedroot"));
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001523 WRITE_ONCE(rcu_state.gp_flags, rcu_state.gp_flags | RCU_GP_FLAG_INIT);
1524 rcu_state.gp_req_activity = jiffies;
1525 if (!rcu_state.gp_kthread) {
Joel Fernandes (Google)df2bf8f2018-05-22 23:38:14 -07001526 trace_rcu_this_gp(rnp, rdp, gp_seq_req, TPS("NoGPkthread"));
Paul E. McKenney360e0da2018-04-12 11:50:41 -07001527 goto unlock_out;
Paul E. McKenney0446be42012-12-30 15:21:01 -08001528 }
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001529 trace_rcu_grace_period(rcu_state.name, READ_ONCE(rcu_state.gp_seq), TPS("newreq"));
Paul E. McKenney360e0da2018-04-12 11:50:41 -07001530 ret = true; /* Caller must wake GP kthread. */
Paul E. McKenney0446be42012-12-30 15:21:01 -08001531unlock_out:
Paul E. McKenneyab5e8692018-05-01 11:07:23 -07001532 /* Push furthest requested GP to leaf node and rcu_data structure. */
Joel Fernandes (Google)df2bf8f2018-05-22 23:38:14 -07001533 if (ULONG_CMP_LT(gp_seq_req, rnp->gp_seq_needed)) {
1534 rnp_start->gp_seq_needed = rnp->gp_seq_needed;
1535 rdp->gp_seq_needed = rnp->gp_seq_needed;
Paul E. McKenneyab5e8692018-05-01 11:07:23 -07001536 }
Joel Fernandes (Google)df2bf8f2018-05-22 23:38:14 -07001537 if (rnp != rnp_start)
1538 raw_spin_unlock_rcu_node(rnp);
Paul E. McKenney48a76392014-03-11 13:02:16 -07001539 return ret;
Paul E. McKenney0446be42012-12-30 15:21:01 -08001540}
1541
1542/*
1543 * Clean up any old requests for the just-ended grace period. Also return
Paul E. McKenneyd1e4f012017-02-08 14:58:41 -08001544 * whether any additional grace periods have been requested.
Paul E. McKenney0446be42012-12-30 15:21:01 -08001545 */
Paul E. McKenney3481f2e2018-07-03 17:22:34 -07001546static bool rcu_future_gp_cleanup(struct rcu_node *rnp)
Paul E. McKenney0446be42012-12-30 15:21:01 -08001547{
Paul E. McKenneyfb313402018-04-12 07:20:30 -07001548 bool needmore;
Paul E. McKenneyda1df502018-07-03 15:37:16 -07001549 struct rcu_data *rdp = this_cpu_ptr(&rcu_data);
Paul E. McKenney0446be42012-12-30 15:21:01 -08001550
Paul E. McKenney7a1d0f22018-05-01 10:26:57 -07001551 needmore = ULONG_CMP_LT(rnp->gp_seq, rnp->gp_seq_needed);
1552 if (!needmore)
1553 rnp->gp_seq_needed = rnp->gp_seq; /* Avoid counter wrap. */
Joel Fernandesb73de912018-05-20 21:42:18 -07001554 trace_rcu_this_gp(rnp, rdp, rnp->gp_seq,
Paul E. McKenney41e80592018-04-12 11:24:09 -07001555 needmore ? TPS("CleanupMore") : TPS("Cleanup"));
Paul E. McKenney0446be42012-12-30 15:21:01 -08001556 return needmore;
1557}
1558
1559/*
Paul E. McKenney49918a52018-07-07 18:12:26 -07001560 * Awaken the grace-period kthread. Don't do a self-awaken, and don't
1561 * bother awakening when there is nothing for the grace-period kthread
1562 * to do (as in several CPUs raced to awaken, and we lost), and finally
1563 * don't try to awaken a kthread that has not yet been created.
Paul E. McKenney48a76392014-03-11 13:02:16 -07001564 */
Paul E. McKenney532c00c2018-07-03 17:22:34 -07001565static void rcu_gp_kthread_wake(void)
Paul E. McKenney48a76392014-03-11 13:02:16 -07001566{
Paul E. McKenney532c00c2018-07-03 17:22:34 -07001567 if (current == rcu_state.gp_kthread ||
1568 !READ_ONCE(rcu_state.gp_flags) ||
1569 !rcu_state.gp_kthread)
Paul E. McKenney48a76392014-03-11 13:02:16 -07001570 return;
Paul E. McKenney532c00c2018-07-03 17:22:34 -07001571 swake_up_one(&rcu_state.gp_wq);
Paul E. McKenney48a76392014-03-11 13:02:16 -07001572}
1573
1574/*
Paul E. McKenney29365e52018-04-30 10:57:36 -07001575 * If there is room, assign a ->gp_seq number to any callbacks on this
1576 * CPU that have not already been assigned. Also accelerate any callbacks
1577 * that were previously assigned a ->gp_seq number that has since proven
1578 * to be too conservative, which can happen if callbacks get assigned a
1579 * ->gp_seq number while RCU is idle, but with reference to a non-root
1580 * rcu_node structure. This function is idempotent, so it does not hurt
1581 * to call it repeatedly. Returns an flag saying that we should awaken
1582 * the RCU grace-period kthread.
Paul E. McKenneydc35c892012-12-03 13:52:00 -08001583 *
1584 * The caller must hold rnp->lock with interrupts disabled.
1585 */
Paul E. McKenney02f50142018-07-03 17:22:34 -07001586static bool rcu_accelerate_cbs(struct rcu_node *rnp, struct rcu_data *rdp)
Paul E. McKenneydc35c892012-12-03 13:52:00 -08001587{
Joel Fernandesb73de912018-05-20 21:42:18 -07001588 unsigned long gp_seq_req;
Paul E. McKenney15fecf82017-02-08 12:36:42 -08001589 bool ret = false;
Paul E. McKenneydc35c892012-12-03 13:52:00 -08001590
Matthew Wilcoxa32e01e2018-01-17 06:24:30 -08001591 raw_lockdep_assert_held_rcu_node(rnp);
Paul E. McKenneyc0b334c2017-04-28 12:32:15 -07001592
Paul E. McKenney15fecf82017-02-08 12:36:42 -08001593 /* If no pending (not yet ready to invoke) callbacks, nothing to do. */
1594 if (!rcu_segcblist_pend_cbs(&rdp->cblist))
Paul E. McKenney48a76392014-03-11 13:02:16 -07001595 return false;
Paul E. McKenneydc35c892012-12-03 13:52:00 -08001596
1597 /*
Paul E. McKenney15fecf82017-02-08 12:36:42 -08001598 * Callbacks are often registered with incomplete grace-period
1599 * information. Something about the fact that getting exact
1600 * information requires acquiring a global lock... RCU therefore
1601 * makes a conservative estimate of the grace period number at which
1602 * a given callback will become ready to invoke. The following
1603 * code checks this estimate and improves it when possible, thus
1604 * accelerating callback invocation to an earlier grace-period
1605 * number.
Paul E. McKenneydc35c892012-12-03 13:52:00 -08001606 */
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001607 gp_seq_req = rcu_seq_snap(&rcu_state.gp_seq);
Joel Fernandesb73de912018-05-20 21:42:18 -07001608 if (rcu_segcblist_accelerate(&rdp->cblist, gp_seq_req))
1609 ret = rcu_start_this_gp(rnp, rdp, gp_seq_req);
Paul E. McKenney6d4b4182012-11-27 16:55:44 -08001610
1611 /* Trace depending on how much we were able to accelerate. */
Paul E. McKenney15fecf82017-02-08 12:36:42 -08001612 if (rcu_segcblist_restempty(&rdp->cblist, RCU_WAIT_TAIL))
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001613 trace_rcu_grace_period(rcu_state.name, rdp->gp_seq, TPS("AccWaitCB"));
Paul E. McKenney6d4b4182012-11-27 16:55:44 -08001614 else
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001615 trace_rcu_grace_period(rcu_state.name, rdp->gp_seq, TPS("AccReadyCB"));
Paul E. McKenney48a76392014-03-11 13:02:16 -07001616 return ret;
Paul E. McKenneydc35c892012-12-03 13:52:00 -08001617}
1618
1619/*
Paul E. McKenneye44e73c2018-05-01 16:29:47 -07001620 * Similar to rcu_accelerate_cbs(), but does not require that the leaf
1621 * rcu_node structure's ->lock be held. It consults the cached value
1622 * of ->gp_seq_needed in the rcu_data structure, and if that indicates
1623 * that a new grace-period request be made, invokes rcu_accelerate_cbs()
1624 * while holding the leaf rcu_node structure's ->lock.
1625 */
Paul E. McKenneyc6e09b92018-07-03 17:22:34 -07001626static void rcu_accelerate_cbs_unlocked(struct rcu_node *rnp,
Paul E. McKenneye44e73c2018-05-01 16:29:47 -07001627 struct rcu_data *rdp)
1628{
1629 unsigned long c;
1630 bool needwake;
1631
1632 lockdep_assert_irqs_disabled();
Paul E. McKenneyc6e09b92018-07-03 17:22:34 -07001633 c = rcu_seq_snap(&rcu_state.gp_seq);
Paul E. McKenneye44e73c2018-05-01 16:29:47 -07001634 if (!rdp->gpwrap && ULONG_CMP_GE(rdp->gp_seq_needed, c)) {
1635 /* Old request still live, so mark recent callbacks. */
1636 (void)rcu_segcblist_accelerate(&rdp->cblist, c);
1637 return;
1638 }
1639 raw_spin_lock_rcu_node(rnp); /* irqs already disabled. */
Paul E. McKenney02f50142018-07-03 17:22:34 -07001640 needwake = rcu_accelerate_cbs(rnp, rdp);
Paul E. McKenneye44e73c2018-05-01 16:29:47 -07001641 raw_spin_unlock_rcu_node(rnp); /* irqs remain disabled. */
1642 if (needwake)
Paul E. McKenney532c00c2018-07-03 17:22:34 -07001643 rcu_gp_kthread_wake();
Paul E. McKenneye44e73c2018-05-01 16:29:47 -07001644}
1645
1646/*
Paul E. McKenneydc35c892012-12-03 13:52:00 -08001647 * Move any callbacks whose grace period has completed to the
1648 * RCU_DONE_TAIL sublist, then compact the remaining sublists and
Paul E. McKenney29365e52018-04-30 10:57:36 -07001649 * assign ->gp_seq numbers to any callbacks in the RCU_NEXT_TAIL
Paul E. McKenneydc35c892012-12-03 13:52:00 -08001650 * sublist. This function is idempotent, so it does not hurt to
1651 * invoke it repeatedly. As long as it is not invoked -too- often...
Paul E. McKenney48a76392014-03-11 13:02:16 -07001652 * Returns true if the RCU grace-period kthread needs to be awakened.
Paul E. McKenneydc35c892012-12-03 13:52:00 -08001653 *
1654 * The caller must hold rnp->lock with interrupts disabled.
1655 */
Paul E. McKenney834f56b2018-07-03 17:22:34 -07001656static bool rcu_advance_cbs(struct rcu_node *rnp, struct rcu_data *rdp)
Paul E. McKenneydc35c892012-12-03 13:52:00 -08001657{
Matthew Wilcoxa32e01e2018-01-17 06:24:30 -08001658 raw_lockdep_assert_held_rcu_node(rnp);
Paul E. McKenneyc0b334c2017-04-28 12:32:15 -07001659
Paul E. McKenney15fecf82017-02-08 12:36:42 -08001660 /* If no pending (not yet ready to invoke) callbacks, nothing to do. */
1661 if (!rcu_segcblist_pend_cbs(&rdp->cblist))
Paul E. McKenney48a76392014-03-11 13:02:16 -07001662 return false;
Paul E. McKenneydc35c892012-12-03 13:52:00 -08001663
1664 /*
Paul E. McKenney29365e52018-04-30 10:57:36 -07001665 * Find all callbacks whose ->gp_seq numbers indicate that they
Paul E. McKenneydc35c892012-12-03 13:52:00 -08001666 * are ready to invoke, and put them into the RCU_DONE_TAIL sublist.
1667 */
Paul E. McKenney29365e52018-04-30 10:57:36 -07001668 rcu_segcblist_advance(&rdp->cblist, rnp->gp_seq);
Paul E. McKenneydc35c892012-12-03 13:52:00 -08001669
1670 /* Classify any remaining callbacks. */
Paul E. McKenney02f50142018-07-03 17:22:34 -07001671 return rcu_accelerate_cbs(rnp, rdp);
Paul E. McKenneydc35c892012-12-03 13:52:00 -08001672}
1673
1674/*
Paul E. McKenneyba9fbe92013-03-19 11:53:31 -07001675 * Update CPU-local rcu_data state to record the beginnings and ends of
1676 * grace periods. The caller must hold the ->lock of the leaf rcu_node
1677 * structure corresponding to the current CPU, and must have irqs disabled.
Paul E. McKenney48a76392014-03-11 13:02:16 -07001678 * Returns true if the grace-period kthread needs to be awakened.
Paul E. McKenneyd09b62d2009-11-02 13:52:28 -08001679 */
Paul E. McKenneyc7e48f72018-07-03 17:22:34 -07001680static bool __note_gp_changes(struct rcu_node *rnp, struct rcu_data *rdp)
Paul E. McKenneyd09b62d2009-11-02 13:52:28 -08001681{
Paul E. McKenney48a76392014-03-11 13:02:16 -07001682 bool ret;
Paul E. McKenney3563a432016-07-28 09:39:11 -07001683 bool need_gp;
Paul E. McKenney48a76392014-03-11 13:02:16 -07001684
Matthew Wilcoxa32e01e2018-01-17 06:24:30 -08001685 raw_lockdep_assert_held_rcu_node(rnp);
Paul E. McKenneyc0b334c2017-04-28 12:32:15 -07001686
Paul E. McKenney67e14c12018-04-27 16:01:46 -07001687 if (rdp->gp_seq == rnp->gp_seq)
1688 return false; /* Nothing to do. */
1689
Paul E. McKenneyba9fbe92013-03-19 11:53:31 -07001690 /* Handle the ends of any preceding grace periods first. */
Paul E. McKenney67e14c12018-04-27 16:01:46 -07001691 if (rcu_seq_completed_gp(rdp->gp_seq, rnp->gp_seq) ||
1692 unlikely(READ_ONCE(rdp->gpwrap))) {
Paul E. McKenney834f56b2018-07-03 17:22:34 -07001693 ret = rcu_advance_cbs(rnp, rdp); /* Advance callbacks. */
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001694 trace_rcu_grace_period(rcu_state.name, rdp->gp_seq, TPS("cpuend"));
Paul E. McKenney67e14c12018-04-27 16:01:46 -07001695 } else {
Paul E. McKenney02f50142018-07-03 17:22:34 -07001696 ret = rcu_accelerate_cbs(rnp, rdp); /* Recent callbacks. */
Paul E. McKenneyd09b62d2009-11-02 13:52:28 -08001697 }
Paul E. McKenney398ebe62013-03-19 10:53:14 -07001698
Paul E. McKenney67e14c12018-04-27 16:01:46 -07001699 /* Now handle the beginnings of any new-to-this-CPU grace periods. */
1700 if (rcu_seq_new_gp(rdp->gp_seq, rnp->gp_seq) ||
1701 unlikely(READ_ONCE(rdp->gpwrap))) {
Paul E. McKenney6eaef632013-03-19 10:08:37 -07001702 /*
1703 * If the current grace period is waiting for this CPU,
1704 * set up to detect a quiescent state, otherwise don't
1705 * go looking for one.
1706 */
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001707 trace_rcu_grace_period(rcu_state.name, rnp->gp_seq, TPS("cpustart"));
Paul E. McKenney3563a432016-07-28 09:39:11 -07001708 need_gp = !!(rnp->qsmask & rdp->grpmask);
1709 rdp->cpu_no_qs.b.norm = need_gp;
Paul E. McKenney3563a432016-07-28 09:39:11 -07001710 rdp->core_needs_qs = need_gp;
Paul E. McKenney6eaef632013-03-19 10:08:37 -07001711 zero_cpu_stall_ticks(rdp);
1712 }
Paul E. McKenney67e14c12018-04-27 16:01:46 -07001713 rdp->gp_seq = rnp->gp_seq; /* Remember new grace-period state. */
Paul E. McKenney3d184692018-05-15 16:47:30 -07001714 if (ULONG_CMP_GE(rnp->gp_seq_needed, rdp->gp_seq_needed) || rdp->gpwrap)
1715 rdp->gp_seq_needed = rnp->gp_seq_needed;
1716 WRITE_ONCE(rdp->gpwrap, false);
1717 rcu_gpnum_ovf(rnp, rdp);
Paul E. McKenney48a76392014-03-11 13:02:16 -07001718 return ret;
Paul E. McKenney6eaef632013-03-19 10:08:37 -07001719}
1720
Paul E. McKenney15cabdf2018-07-03 17:22:34 -07001721static void note_gp_changes(struct rcu_data *rdp)
Paul E. McKenney6eaef632013-03-19 10:08:37 -07001722{
1723 unsigned long flags;
Paul E. McKenney48a76392014-03-11 13:02:16 -07001724 bool needwake;
Paul E. McKenney6eaef632013-03-19 10:08:37 -07001725 struct rcu_node *rnp;
1726
1727 local_irq_save(flags);
1728 rnp = rdp->mynode;
Paul E. McKenney67e14c12018-04-27 16:01:46 -07001729 if ((rdp->gp_seq == rcu_seq_current(&rnp->gp_seq) &&
Paul E. McKenney7d0ae802015-03-03 14:57:58 -08001730 !unlikely(READ_ONCE(rdp->gpwrap))) || /* w/out lock. */
Peter Zijlstra2a67e742015-10-08 12:24:23 +02001731 !raw_spin_trylock_rcu_node(rnp)) { /* irqs already off, so later. */
Paul E. McKenney6eaef632013-03-19 10:08:37 -07001732 local_irq_restore(flags);
1733 return;
1734 }
Paul E. McKenneyc7e48f72018-07-03 17:22:34 -07001735 needwake = __note_gp_changes(rnp, rdp);
Boqun Feng67c583a72015-12-29 12:18:47 +08001736 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
Paul E. McKenney48a76392014-03-11 13:02:16 -07001737 if (needwake)
Paul E. McKenney532c00c2018-07-03 17:22:34 -07001738 rcu_gp_kthread_wake();
Paul E. McKenney6eaef632013-03-19 10:08:37 -07001739}
1740
Paul E. McKenney22212332018-07-03 17:22:34 -07001741static void rcu_gp_slow(int delay)
Paul E. McKenney0f41c0d2015-03-10 18:33:20 -07001742{
1743 if (delay > 0 &&
Paul E. McKenney22212332018-07-03 17:22:34 -07001744 !(rcu_seq_ctr(rcu_state.gp_seq) %
Paul E. McKenneydee4f422018-04-26 15:30:28 -07001745 (rcu_num_nodes * PER_RCU_NODE_PERIOD * delay)))
Paul E. McKenney0f41c0d2015-03-10 18:33:20 -07001746 schedule_timeout_uninterruptible(delay);
1747}
1748
Paul E. McKenney6eaef632013-03-19 10:08:37 -07001749/*
Paul E. McKenney45fed3e2015-11-07 23:35:00 -08001750 * Initialize a new grace period. Return false if no grace period required.
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001751 */
Paul E. McKenney0854a05c2018-07-03 17:22:34 -07001752static bool rcu_gp_init(void)
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001753{
Paul E. McKenneyec2c2972018-05-07 09:34:17 -07001754 unsigned long flags;
Paul E. McKenney0aa04b02015-01-23 21:52:37 -08001755 unsigned long oldmask;
Paul E. McKenneyec2c2972018-05-07 09:34:17 -07001756 unsigned long mask;
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001757 struct rcu_data *rdp;
Paul E. McKenney336a4f62018-07-03 17:22:34 -07001758 struct rcu_node *rnp = rcu_get_root();
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001759
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001760 WRITE_ONCE(rcu_state.gp_activity, jiffies);
Peter Zijlstra2a67e742015-10-08 12:24:23 +02001761 raw_spin_lock_irq_rcu_node(rnp);
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001762 if (!READ_ONCE(rcu_state.gp_flags)) {
Paul E. McKenneyf7be8202013-08-08 18:27:52 -07001763 /* Spurious wakeup, tell caller to go back to sleep. */
Boqun Feng67c583a72015-12-29 12:18:47 +08001764 raw_spin_unlock_irq_rcu_node(rnp);
Paul E. McKenney45fed3e2015-11-07 23:35:00 -08001765 return false;
Paul E. McKenneyf7be8202013-08-08 18:27:52 -07001766 }
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001767 WRITE_ONCE(rcu_state.gp_flags, 0); /* Clear all flags: New GP. */
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001768
Paul E. McKenneyde8e8732018-07-03 17:22:34 -07001769 if (WARN_ON_ONCE(rcu_gp_in_progress())) {
Paul E. McKenneyf7be8202013-08-08 18:27:52 -07001770 /*
1771 * Grace period already in progress, don't start another.
1772 * Not supposed to be able to happen.
1773 */
Boqun Feng67c583a72015-12-29 12:18:47 +08001774 raw_spin_unlock_irq_rcu_node(rnp);
Paul E. McKenney45fed3e2015-11-07 23:35:00 -08001775 return false;
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001776 }
1777
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001778 /* Advance to a new grace period and initialize state. */
Paul E. McKenneyad3832e2018-07-03 17:22:34 -07001779 record_gp_stall_check_time();
Paul E. McKenneyff3bb6f2018-05-01 14:34:08 -07001780 /* Record GP times before starting GP, hence rcu_seq_start(). */
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001781 rcu_seq_start(&rcu_state.gp_seq);
1782 trace_rcu_grace_period(rcu_state.name, rcu_state.gp_seq, TPS("start"));
Boqun Feng67c583a72015-12-29 12:18:47 +08001783 raw_spin_unlock_irq_rcu_node(rnp);
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001784
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001785 /*
Paul E. McKenney0aa04b02015-01-23 21:52:37 -08001786 * Apply per-leaf buffered online and offline operations to the
1787 * rcu_node tree. Note that this new grace period need not wait
1788 * for subsequent online CPUs, and that quiescent-state forcing
1789 * will handle subsequent offline CPUs.
1790 */
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001791 rcu_state.gp_state = RCU_GP_ONOFF;
Paul E. McKenneyaedf4ba2018-07-04 14:33:59 -07001792 rcu_for_each_leaf_node(rnp) {
Mike Galbraith894d45b2018-08-15 09:05:29 -07001793 raw_spin_lock(&rcu_state.ofl_lock);
Peter Zijlstra2a67e742015-10-08 12:24:23 +02001794 raw_spin_lock_irq_rcu_node(rnp);
Paul E. McKenney0aa04b02015-01-23 21:52:37 -08001795 if (rnp->qsmaskinit == rnp->qsmaskinitnext &&
1796 !rnp->wait_blkd_tasks) {
1797 /* Nothing to do on this leaf rcu_node structure. */
Boqun Feng67c583a72015-12-29 12:18:47 +08001798 raw_spin_unlock_irq_rcu_node(rnp);
Mike Galbraith894d45b2018-08-15 09:05:29 -07001799 raw_spin_unlock(&rcu_state.ofl_lock);
Paul E. McKenney0aa04b02015-01-23 21:52:37 -08001800 continue;
1801 }
1802
1803 /* Record old state, apply changes to ->qsmaskinit field. */
1804 oldmask = rnp->qsmaskinit;
1805 rnp->qsmaskinit = rnp->qsmaskinitnext;
1806
1807 /* If zero-ness of ->qsmaskinit changed, propagate up tree. */
1808 if (!oldmask != !rnp->qsmaskinit) {
Paul E. McKenney962aff02018-05-02 12:49:21 -07001809 if (!oldmask) { /* First online CPU for rcu_node. */
1810 if (!rnp->wait_blkd_tasks) /* Ever offline? */
1811 rcu_init_new_rnp(rnp);
1812 } else if (rcu_preempt_has_tasks(rnp)) {
1813 rnp->wait_blkd_tasks = true; /* blocked tasks */
1814 } else { /* Last offline CPU and can propagate. */
Paul E. McKenney0aa04b02015-01-23 21:52:37 -08001815 rcu_cleanup_dead_rnp(rnp);
Paul E. McKenney962aff02018-05-02 12:49:21 -07001816 }
Paul E. McKenney0aa04b02015-01-23 21:52:37 -08001817 }
1818
1819 /*
1820 * If all waited-on tasks from prior grace period are
1821 * done, and if all this rcu_node structure's CPUs are
1822 * still offline, propagate up the rcu_node tree and
1823 * clear ->wait_blkd_tasks. Otherwise, if one of this
1824 * rcu_node structure's CPUs has since come back online,
Paul E. McKenney962aff02018-05-02 12:49:21 -07001825 * simply clear ->wait_blkd_tasks.
Paul E. McKenney0aa04b02015-01-23 21:52:37 -08001826 */
1827 if (rnp->wait_blkd_tasks &&
Paul E. McKenney962aff02018-05-02 12:49:21 -07001828 (!rcu_preempt_has_tasks(rnp) || rnp->qsmaskinit)) {
Paul E. McKenney0aa04b02015-01-23 21:52:37 -08001829 rnp->wait_blkd_tasks = false;
Paul E. McKenney962aff02018-05-02 12:49:21 -07001830 if (!rnp->qsmaskinit)
1831 rcu_cleanup_dead_rnp(rnp);
Paul E. McKenney0aa04b02015-01-23 21:52:37 -08001832 }
1833
Boqun Feng67c583a72015-12-29 12:18:47 +08001834 raw_spin_unlock_irq_rcu_node(rnp);
Mike Galbraith894d45b2018-08-15 09:05:29 -07001835 raw_spin_unlock(&rcu_state.ofl_lock);
Paul E. McKenney0aa04b02015-01-23 21:52:37 -08001836 }
Paul E. McKenney22212332018-07-03 17:22:34 -07001837 rcu_gp_slow(gp_preinit_delay); /* Races with CPU hotplug. */
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001838
1839 /*
1840 * Set the quiescent-state-needed bits in all the rcu_node
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001841 * structures for all currently online CPUs in breadth-first
1842 * order, starting from the root rcu_node structure, relying on the
1843 * layout of the tree within the rcu_state.node[] array. Note that
1844 * other CPUs will access only the leaves of the hierarchy, thus
1845 * seeing that no grace period is in progress, at least until the
1846 * corresponding leaf node has been initialized.
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001847 *
1848 * The grace period cannot complete until the initialization
1849 * process finishes, because this kthread handles both.
1850 */
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001851 rcu_state.gp_state = RCU_GP_INIT;
Paul E. McKenneyaedf4ba2018-07-04 14:33:59 -07001852 rcu_for_each_node_breadth_first(rnp) {
Paul E. McKenney22212332018-07-03 17:22:34 -07001853 rcu_gp_slow(gp_init_delay);
Paul E. McKenneyec2c2972018-05-07 09:34:17 -07001854 raw_spin_lock_irqsave_rcu_node(rnp, flags);
Paul E. McKenneyda1df502018-07-03 15:37:16 -07001855 rdp = this_cpu_ptr(&rcu_data);
Paul E. McKenney81ab59a2018-07-03 17:22:34 -07001856 rcu_preempt_check_blocked_tasks(rnp);
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001857 rnp->qsmask = rnp->qsmaskinit;
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001858 WRITE_ONCE(rnp->gp_seq, rcu_state.gp_seq);
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001859 if (rnp == rdp->mynode)
Paul E. McKenneyc7e48f72018-07-03 17:22:34 -07001860 (void)__note_gp_changes(rnp, rdp);
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001861 rcu_preempt_boost_start_gp(rnp);
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001862 trace_rcu_grace_period_init(rcu_state.name, rnp->gp_seq,
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001863 rnp->level, rnp->grplo,
1864 rnp->grphi, rnp->qsmask);
Paul E. McKenneyec2c2972018-05-07 09:34:17 -07001865 /* Quiescent states for tasks on any now-offline CPUs. */
1866 mask = rnp->qsmask & ~rnp->qsmaskinitnext;
Paul E. McKenneyf2e2df52018-05-15 16:23:23 -07001867 rnp->rcu_gp_init_mask = mask;
Paul E. McKenneyec2c2972018-05-07 09:34:17 -07001868 if ((mask || rnp->wait_blkd_tasks) && rcu_is_leaf_node(rnp))
Paul E. McKenneyb50912d2018-07-03 17:22:34 -07001869 rcu_report_qs_rnp(mask, rnp, rnp->gp_seq, flags);
Paul E. McKenneyec2c2972018-05-07 09:34:17 -07001870 else
1871 raw_spin_unlock_irq_rcu_node(rnp);
Paul E. McKenneycee43932018-03-02 16:35:27 -08001872 cond_resched_tasks_rcu_qs();
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001873 WRITE_ONCE(rcu_state.gp_activity, jiffies);
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001874 }
1875
Paul E. McKenney45fed3e2015-11-07 23:35:00 -08001876 return true;
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001877}
1878
1879/*
Peter Zijlstrab3dae102018-06-12 10:34:52 +02001880 * Helper function for swait_event_idle_exclusive() wakeup at force-quiescent-state
Luis R. Rodriguezd5374222017-06-20 14:45:47 -07001881 * time.
Paul E. McKenneyb9a425c2015-07-01 13:50:28 -07001882 */
Paul E. McKenney0854a05c2018-07-03 17:22:34 -07001883static bool rcu_gp_fqs_check_wake(int *gfp)
Paul E. McKenneyb9a425c2015-07-01 13:50:28 -07001884{
Paul E. McKenney336a4f62018-07-03 17:22:34 -07001885 struct rcu_node *rnp = rcu_get_root();
Paul E. McKenneyb9a425c2015-07-01 13:50:28 -07001886
1887 /* Someone like call_rcu() requested a force-quiescent-state scan. */
Paul E. McKenney0854a05c2018-07-03 17:22:34 -07001888 *gfp = READ_ONCE(rcu_state.gp_flags);
Paul E. McKenneyb9a425c2015-07-01 13:50:28 -07001889 if (*gfp & RCU_GP_FLAG_FQS)
1890 return true;
1891
1892 /* The current grace period has completed. */
1893 if (!READ_ONCE(rnp->qsmask) && !rcu_preempt_blocked_readers_cgp(rnp))
1894 return true;
1895
1896 return false;
1897}
1898
1899/*
Paul E. McKenney4cdfc1752012-06-22 17:06:26 -07001900 * Do one round of quiescent-state forcing.
1901 */
Paul E. McKenney0854a05c2018-07-03 17:22:34 -07001902static void rcu_gp_fqs(bool first_time)
Paul E. McKenney4cdfc1752012-06-22 17:06:26 -07001903{
Paul E. McKenney336a4f62018-07-03 17:22:34 -07001904 struct rcu_node *rnp = rcu_get_root();
Paul E. McKenney4cdfc1752012-06-22 17:06:26 -07001905
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001906 WRITE_ONCE(rcu_state.gp_activity, jiffies);
1907 rcu_state.n_force_qs++;
Petr Mladek77f81fe2015-09-09 12:09:49 -07001908 if (first_time) {
Paul E. McKenney4cdfc1752012-06-22 17:06:26 -07001909 /* Collect dyntick-idle snapshots. */
Paul E. McKenneye9ecb782018-07-03 17:22:34 -07001910 force_qs_rnp(dyntick_save_progress_counter);
Paul E. McKenney4cdfc1752012-06-22 17:06:26 -07001911 } else {
1912 /* Handle dyntick-idle and offline CPUs. */
Paul E. McKenneye9ecb782018-07-03 17:22:34 -07001913 force_qs_rnp(rcu_implicit_dynticks_qs);
Paul E. McKenney4cdfc1752012-06-22 17:06:26 -07001914 }
1915 /* Clear flag to prevent immediate re-entry. */
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001916 if (READ_ONCE(rcu_state.gp_flags) & RCU_GP_FLAG_FQS) {
Peter Zijlstra2a67e742015-10-08 12:24:23 +02001917 raw_spin_lock_irq_rcu_node(rnp);
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001918 WRITE_ONCE(rcu_state.gp_flags,
1919 READ_ONCE(rcu_state.gp_flags) & ~RCU_GP_FLAG_FQS);
Boqun Feng67c583a72015-12-29 12:18:47 +08001920 raw_spin_unlock_irq_rcu_node(rnp);
Paul E. McKenney4cdfc1752012-06-22 17:06:26 -07001921 }
Paul E. McKenney4cdfc1752012-06-22 17:06:26 -07001922}
1923
1924/*
Paul E. McKenneyc3854a02018-07-05 18:23:23 -07001925 * Loop doing repeated quiescent-state forcing until the grace period ends.
1926 */
1927static void rcu_gp_fqs_loop(void)
1928{
1929 bool first_gp_fqs;
1930 int gf;
1931 unsigned long j;
1932 int ret;
1933 struct rcu_node *rnp = rcu_get_root();
1934
1935 first_gp_fqs = true;
Paul E. McKenneyc06aed02018-07-25 11:25:23 -07001936 j = READ_ONCE(jiffies_till_first_fqs);
Paul E. McKenneyc3854a02018-07-05 18:23:23 -07001937 ret = 0;
1938 for (;;) {
1939 if (!ret) {
1940 rcu_state.jiffies_force_qs = jiffies + j;
1941 WRITE_ONCE(rcu_state.jiffies_kick_kthreads,
1942 jiffies + 3 * j);
1943 }
1944 trace_rcu_grace_period(rcu_state.name,
1945 READ_ONCE(rcu_state.gp_seq),
1946 TPS("fqswait"));
1947 rcu_state.gp_state = RCU_GP_WAIT_FQS;
1948 ret = swait_event_idle_timeout_exclusive(
1949 rcu_state.gp_wq, rcu_gp_fqs_check_wake(&gf), j);
1950 rcu_state.gp_state = RCU_GP_DOING_FQS;
1951 /* Locking provides needed memory barriers. */
1952 /* If grace period done, leave loop. */
1953 if (!READ_ONCE(rnp->qsmask) &&
1954 !rcu_preempt_blocked_readers_cgp(rnp))
1955 break;
1956 /* If time for quiescent-state forcing, do it. */
1957 if (ULONG_CMP_GE(jiffies, rcu_state.jiffies_force_qs) ||
1958 (gf & RCU_GP_FLAG_FQS)) {
1959 trace_rcu_grace_period(rcu_state.name,
1960 READ_ONCE(rcu_state.gp_seq),
1961 TPS("fqsstart"));
1962 rcu_gp_fqs(first_gp_fqs);
1963 first_gp_fqs = false;
1964 trace_rcu_grace_period(rcu_state.name,
1965 READ_ONCE(rcu_state.gp_seq),
1966 TPS("fqsend"));
1967 cond_resched_tasks_rcu_qs();
1968 WRITE_ONCE(rcu_state.gp_activity, jiffies);
1969 ret = 0; /* Force full wait till next FQS. */
Paul E. McKenneyc06aed02018-07-25 11:25:23 -07001970 j = READ_ONCE(jiffies_till_next_fqs);
Paul E. McKenneyc3854a02018-07-05 18:23:23 -07001971 } else {
1972 /* Deal with stray signal. */
1973 cond_resched_tasks_rcu_qs();
1974 WRITE_ONCE(rcu_state.gp_activity, jiffies);
1975 WARN_ON(signal_pending(current));
1976 trace_rcu_grace_period(rcu_state.name,
1977 READ_ONCE(rcu_state.gp_seq),
1978 TPS("fqswaitsig"));
1979 ret = 1; /* Keep old FQS timing. */
1980 j = jiffies;
1981 if (time_after(jiffies, rcu_state.jiffies_force_qs))
1982 j = 1;
1983 else
1984 j = rcu_state.jiffies_force_qs - j;
1985 }
1986 }
1987}
1988
1989/*
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001990 * Clean up after the old grace period.
1991 */
Paul E. McKenney0854a05c2018-07-03 17:22:34 -07001992static void rcu_gp_cleanup(void)
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001993{
1994 unsigned long gp_duration;
Paul E. McKenney48a76392014-03-11 13:02:16 -07001995 bool needgp = false;
Paul E. McKenneyde30ad52018-04-26 11:52:09 -07001996 unsigned long new_gp_seq;
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001997 struct rcu_data *rdp;
Paul E. McKenney336a4f62018-07-03 17:22:34 -07001998 struct rcu_node *rnp = rcu_get_root();
Paul Gortmakerabedf8e2016-02-19 09:46:41 +01001999 struct swait_queue_head *sq;
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07002000
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07002001 WRITE_ONCE(rcu_state.gp_activity, jiffies);
Peter Zijlstra2a67e742015-10-08 12:24:23 +02002002 raw_spin_lock_irq_rcu_node(rnp);
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07002003 gp_duration = jiffies - rcu_state.gp_start;
2004 if (gp_duration > rcu_state.gp_max)
2005 rcu_state.gp_max = gp_duration;
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07002006
2007 /*
2008 * We know the grace period is complete, but to everyone else
2009 * it appears to still be ongoing. But it is also the case
2010 * that to everyone else it looks like there is nothing that
2011 * they can do to advance the grace period. It is therefore
2012 * safe for us to drop the lock in order to mark the grace
2013 * period as completed in all of the rcu_node structures.
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07002014 */
Boqun Feng67c583a72015-12-29 12:18:47 +08002015 raw_spin_unlock_irq_rcu_node(rnp);
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07002016
Paul E. McKenney5d4b8652012-07-07 07:56:57 -07002017 /*
Paul E. McKenneyff3bb6f2018-05-01 14:34:08 -07002018 * Propagate new ->gp_seq value to rcu_node structures so that
2019 * other CPUs don't have to wait until the start of the next grace
2020 * period to process their callbacks. This also avoids some nasty
2021 * RCU grace-period initialization races by forcing the end of
2022 * the current grace period to be completely recorded in all of
2023 * the rcu_node structures before the beginning of the next grace
2024 * period is recorded in any of the rcu_node structures.
Paul E. McKenney5d4b8652012-07-07 07:56:57 -07002025 */
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07002026 new_gp_seq = rcu_state.gp_seq;
Paul E. McKenneyde30ad52018-04-26 11:52:09 -07002027 rcu_seq_end(&new_gp_seq);
Paul E. McKenneyaedf4ba2018-07-04 14:33:59 -07002028 rcu_for_each_node_breadth_first(rnp) {
Peter Zijlstra2a67e742015-10-08 12:24:23 +02002029 raw_spin_lock_irq_rcu_node(rnp);
Paul E. McKenney4bc8d552017-11-27 15:13:56 -08002030 if (WARN_ON_ONCE(rcu_preempt_blocked_readers_cgp(rnp)))
Paul E. McKenney81ab59a2018-07-03 17:22:34 -07002031 dump_blkd_tasks(rnp, 10);
Paul E. McKenney5c60d252015-02-09 05:37:47 -08002032 WARN_ON_ONCE(rnp->qsmask);
Paul E. McKenneyde30ad52018-04-26 11:52:09 -07002033 WRITE_ONCE(rnp->gp_seq, new_gp_seq);
Paul E. McKenneyda1df502018-07-03 15:37:16 -07002034 rdp = this_cpu_ptr(&rcu_data);
Paul E. McKenneyb11cc572012-12-17 14:21:14 -08002035 if (rnp == rdp->mynode)
Paul E. McKenneyc7e48f72018-07-03 17:22:34 -07002036 needgp = __note_gp_changes(rnp, rdp) || needgp;
Paul E. McKenney78e4bc32013-09-24 15:04:06 -07002037 /* smp_mb() provided by prior unlock-lock pair. */
Paul E. McKenney3481f2e2018-07-03 17:22:34 -07002038 needgp = rcu_future_gp_cleanup(rnp) || needgp;
Daniel Wagner065bb782016-02-19 09:46:40 +01002039 sq = rcu_nocb_gp_get(rnp);
Boqun Feng67c583a72015-12-29 12:18:47 +08002040 raw_spin_unlock_irq_rcu_node(rnp);
Daniel Wagner065bb782016-02-19 09:46:40 +01002041 rcu_nocb_gp_cleanup(sq);
Paul E. McKenneycee43932018-03-02 16:35:27 -08002042 cond_resched_tasks_rcu_qs();
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07002043 WRITE_ONCE(rcu_state.gp_activity, jiffies);
Paul E. McKenney22212332018-07-03 17:22:34 -07002044 rcu_gp_slow(gp_cleanup_delay);
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07002045 }
Paul E. McKenney336a4f62018-07-03 17:22:34 -07002046 rnp = rcu_get_root();
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07002047 raw_spin_lock_irq_rcu_node(rnp); /* GP before ->gp_seq update. */
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07002048
Paul E. McKenney0a89e5a2018-10-15 10:00:58 -07002049 /* Declare grace period done, trace first to use old GP number. */
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07002050 trace_rcu_grace_period(rcu_state.name, rcu_state.gp_seq, TPS("end"));
Paul E. McKenney0a89e5a2018-10-15 10:00:58 -07002051 rcu_seq_end(&rcu_state.gp_seq);
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07002052 rcu_state.gp_state = RCU_GP_IDLE;
Paul E. McKenneyfb313402018-04-12 07:20:30 -07002053 /* Check for GP requests since above loop. */
Paul E. McKenneyda1df502018-07-03 15:37:16 -07002054 rdp = this_cpu_ptr(&rcu_data);
Joel Fernandes (Google)5b550722018-05-13 20:15:40 -07002055 if (!needgp && ULONG_CMP_LT(rnp->gp_seq, rnp->gp_seq_needed)) {
Paul E. McKenneyabd13fd2018-05-01 13:08:46 -07002056 trace_rcu_this_gp(rnp, rdp, rnp->gp_seq_needed,
Paul E. McKenney41e80592018-04-12 11:24:09 -07002057 TPS("CleanupMore"));
Paul E. McKenneyfb313402018-04-12 07:20:30 -07002058 needgp = true;
2059 }
Paul E. McKenney48a76392014-03-11 13:02:16 -07002060 /* Advance CBs to reduce false positives below. */
Paul E. McKenney02f50142018-07-03 17:22:34 -07002061 if (!rcu_accelerate_cbs(rnp, rdp) && needgp) {
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07002062 WRITE_ONCE(rcu_state.gp_flags, RCU_GP_FLAG_INIT);
2063 rcu_state.gp_req_activity = jiffies;
2064 trace_rcu_grace_period(rcu_state.name,
2065 READ_ONCE(rcu_state.gp_seq),
Paul E. McKenneybb311ec2013-08-09 16:02:09 -07002066 TPS("newreq"));
Paul E. McKenney18390ae2018-04-22 15:06:05 -07002067 } else {
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07002068 WRITE_ONCE(rcu_state.gp_flags,
2069 rcu_state.gp_flags & RCU_GP_FLAG_INIT);
Paul E. McKenneybb311ec2013-08-09 16:02:09 -07002070 }
Boqun Feng67c583a72015-12-29 12:18:47 +08002071 raw_spin_unlock_irq_rcu_node(rnp);
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07002072}
2073
2074/*
Paul E. McKenneyb3dbec72012-06-18 18:36:08 -07002075 * Body of kthread that handles grace periods.
2076 */
Paul E. McKenney0854a05c2018-07-03 17:22:34 -07002077static int __noreturn rcu_gp_kthread(void *unused)
Paul E. McKenneyb3dbec72012-06-18 18:36:08 -07002078{
Paul E. McKenney58719682015-02-24 11:05:36 -08002079 rcu_bind_gp_kthread();
Paul E. McKenneyb3dbec72012-06-18 18:36:08 -07002080 for (;;) {
2081
2082 /* Handle grace-period start. */
Paul E. McKenneyb3dbec72012-06-18 18:36:08 -07002083 for (;;) {
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07002084 trace_rcu_grace_period(rcu_state.name,
2085 READ_ONCE(rcu_state.gp_seq),
Paul E. McKenney63c4db72013-08-09 12:19:29 -07002086 TPS("reqwait"));
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07002087 rcu_state.gp_state = RCU_GP_WAIT_GPS;
2088 swait_event_idle_exclusive(rcu_state.gp_wq,
2089 READ_ONCE(rcu_state.gp_flags) &
2090 RCU_GP_FLAG_INIT);
2091 rcu_state.gp_state = RCU_GP_DONE_GPS;
Paul E. McKenney78e4bc32013-09-24 15:04:06 -07002092 /* Locking provides needed memory barrier. */
Paul E. McKenney0854a05c2018-07-03 17:22:34 -07002093 if (rcu_gp_init())
Paul E. McKenneyb3dbec72012-06-18 18:36:08 -07002094 break;
Paul E. McKenneycee43932018-03-02 16:35:27 -08002095 cond_resched_tasks_rcu_qs();
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07002096 WRITE_ONCE(rcu_state.gp_activity, jiffies);
Paul E. McKenney73a860c2014-08-14 10:28:23 -07002097 WARN_ON(signal_pending(current));
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07002098 trace_rcu_grace_period(rcu_state.name,
2099 READ_ONCE(rcu_state.gp_seq),
Paul E. McKenney63c4db72013-08-09 12:19:29 -07002100 TPS("reqwaitsig"));
Paul E. McKenneyb3dbec72012-06-18 18:36:08 -07002101 }
Paul E. McKenneycabc49c2012-06-20 17:07:14 -07002102
Paul E. McKenney4cdfc1752012-06-22 17:06:26 -07002103 /* Handle quiescent-state forcing. */
Paul E. McKenneyc3854a02018-07-05 18:23:23 -07002104 rcu_gp_fqs_loop();
Paul E. McKenney4cdfc1752012-06-22 17:06:26 -07002105
2106 /* Handle grace-period end. */
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07002107 rcu_state.gp_state = RCU_GP_CLEANUP;
Paul E. McKenney0854a05c2018-07-03 17:22:34 -07002108 rcu_gp_cleanup();
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07002109 rcu_state.gp_state = RCU_GP_CLEANED;
Paul E. McKenneyb3dbec72012-06-18 18:36:08 -07002110 }
Paul E. McKenneyb3dbec72012-06-18 18:36:08 -07002111}
2112
2113/*
Paul E. McKenney49918a52018-07-07 18:12:26 -07002114 * Report a full set of quiescent states to the rcu_state data structure.
2115 * Invoke rcu_gp_kthread_wake() to awaken the grace-period kthread if
2116 * another grace period is required. Whether we wake the grace-period
2117 * kthread or it awakens itself for the next round of quiescent-state
2118 * forcing, that kthread will clean up after the just-completed grace
2119 * period. Note that the caller must hold rnp->lock, which is released
2120 * before return.
Paul E. McKenneyf41d9112009-08-22 13:56:52 -07002121 */
Paul E. McKenneyaff4e9e2018-07-03 17:22:34 -07002122static void rcu_report_qs_rsp(unsigned long flags)
Paul E. McKenney336a4f62018-07-03 17:22:34 -07002123 __releases(rcu_get_root()->lock)
Paul E. McKenneyf41d9112009-08-22 13:56:52 -07002124{
Paul E. McKenney336a4f62018-07-03 17:22:34 -07002125 raw_lockdep_assert_held_rcu_node(rcu_get_root());
Paul E. McKenneyde8e8732018-07-03 17:22:34 -07002126 WARN_ON_ONCE(!rcu_gp_in_progress());
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07002127 WRITE_ONCE(rcu_state.gp_flags,
2128 READ_ONCE(rcu_state.gp_flags) | RCU_GP_FLAG_FQS);
Paul E. McKenney336a4f62018-07-03 17:22:34 -07002129 raw_spin_unlock_irqrestore_rcu_node(rcu_get_root(), flags);
Paul E. McKenney532c00c2018-07-03 17:22:34 -07002130 rcu_gp_kthread_wake();
Paul E. McKenneyf41d9112009-08-22 13:56:52 -07002131}
2132
2133/*
Paul E. McKenneyd3f6bad2009-12-02 12:10:13 -08002134 * Similar to rcu_report_qs_rdp(), for which it is a helper function.
2135 * Allows quiescent states for a group of CPUs to be reported at one go
2136 * to the specified rcu_node structure, though all the CPUs in the group
Paul E. McKenney654e9532015-03-15 09:19:35 -07002137 * must be represented by the same rcu_node structure (which need not be a
2138 * leaf rcu_node structure, though it often will be). The gps parameter
2139 * is the grace-period snapshot, which means that the quiescent states
Paul E. McKenneyc9a24e22018-04-27 14:54:46 -07002140 * are valid only if rnp->gp_seq is equal to gps. That structure's lock
Paul E. McKenney654e9532015-03-15 09:19:35 -07002141 * must be held upon entry, and it is released before return.
Paul E. McKenneyec2c2972018-05-07 09:34:17 -07002142 *
2143 * As a special case, if mask is zero, the bit-already-cleared check is
2144 * disabled. This allows propagating quiescent state due to resumed tasks
2145 * during grace-period initialization.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002146 */
Paul E. McKenneyb50912d2018-07-03 17:22:34 -07002147static void rcu_report_qs_rnp(unsigned long mask, struct rcu_node *rnp,
2148 unsigned long gps, unsigned long flags)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002149 __releases(rnp->lock)
2150{
Paul E. McKenney654e9532015-03-15 09:19:35 -07002151 unsigned long oldmask = 0;
Paul E. McKenney28ecd582009-09-18 09:50:17 -07002152 struct rcu_node *rnp_c;
2153
Matthew Wilcoxa32e01e2018-01-17 06:24:30 -08002154 raw_lockdep_assert_held_rcu_node(rnp);
Paul E. McKenneyc0b334c2017-04-28 12:32:15 -07002155
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002156 /* Walk up the rcu_node hierarchy. */
2157 for (;;) {
Paul E. McKenneyec2c2972018-05-07 09:34:17 -07002158 if ((!(rnp->qsmask & mask) && mask) || rnp->gp_seq != gps) {
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002159
Paul E. McKenney654e9532015-03-15 09:19:35 -07002160 /*
2161 * Our bit has already been cleared, or the
2162 * relevant grace period is already over, so done.
2163 */
Boqun Feng67c583a72015-12-29 12:18:47 +08002164 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002165 return;
2166 }
Paul E. McKenney654e9532015-03-15 09:19:35 -07002167 WARN_ON_ONCE(oldmask); /* Any child must be all zeroed! */
Paul E. McKenney5b4c11d2018-04-13 17:11:44 -07002168 WARN_ON_ONCE(!rcu_is_leaf_node(rnp) &&
Paul E. McKenney2dee9402017-07-11 21:52:31 -07002169 rcu_preempt_blocked_readers_cgp(rnp));
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002170 rnp->qsmask &= ~mask;
Paul E. McKenney67a0edb2018-07-05 16:15:38 -07002171 trace_rcu_quiescent_state_report(rcu_state.name, rnp->gp_seq,
Paul E. McKenneyd4c08f22011-06-25 06:36:56 -07002172 mask, rnp->qsmask, rnp->level,
2173 rnp->grplo, rnp->grphi,
2174 !!rnp->gp_tasks);
Paul E. McKenney27f4d282011-02-07 12:47:15 -08002175 if (rnp->qsmask != 0 || rcu_preempt_blocked_readers_cgp(rnp)) {
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002176
2177 /* Other bits still set at this level, so done. */
Boqun Feng67c583a72015-12-29 12:18:47 +08002178 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002179 return;
2180 }
Paul E. McKenneyd43a5d32018-04-28 18:50:06 -07002181 rnp->completedqs = rnp->gp_seq;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002182 mask = rnp->grpmask;
2183 if (rnp->parent == NULL) {
2184
2185 /* No more levels. Exit loop holding root lock. */
2186
2187 break;
2188 }
Boqun Feng67c583a72015-12-29 12:18:47 +08002189 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
Paul E. McKenney28ecd582009-09-18 09:50:17 -07002190 rnp_c = rnp;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002191 rnp = rnp->parent;
Peter Zijlstra2a67e742015-10-08 12:24:23 +02002192 raw_spin_lock_irqsave_rcu_node(rnp, flags);
Paul E. McKenney654e9532015-03-15 09:19:35 -07002193 oldmask = rnp_c->qsmask;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002194 }
2195
2196 /*
2197 * Get here if we are the last CPU to pass through a quiescent
Paul E. McKenneyd3f6bad2009-12-02 12:10:13 -08002198 * state for this grace period. Invoke rcu_report_qs_rsp()
Paul E. McKenneyf41d9112009-08-22 13:56:52 -07002199 * to clean up and start the next grace period if one is needed.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002200 */
Paul E. McKenneyaff4e9e2018-07-03 17:22:34 -07002201 rcu_report_qs_rsp(flags); /* releases rnp->lock. */
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002202}
2203
2204/*
Paul E. McKenneycc99a312015-02-23 08:59:29 -08002205 * Record a quiescent state for all tasks that were previously queued
2206 * on the specified rcu_node structure and that were blocking the current
Paul E. McKenney49918a52018-07-07 18:12:26 -07002207 * RCU grace period. The caller must hold the corresponding rnp->lock with
Paul E. McKenneycc99a312015-02-23 08:59:29 -08002208 * irqs disabled, and this lock is released upon return, but irqs remain
2209 * disabled.
2210 */
Paul E. McKenney17a82122018-05-03 14:30:02 -07002211static void __maybe_unused
Paul E. McKenney139ad4d2018-07-03 17:22:34 -07002212rcu_report_unblock_qs_rnp(struct rcu_node *rnp, unsigned long flags)
Paul E. McKenneycc99a312015-02-23 08:59:29 -08002213 __releases(rnp->lock)
2214{
Paul E. McKenney654e9532015-03-15 09:19:35 -07002215 unsigned long gps;
Paul E. McKenneycc99a312015-02-23 08:59:29 -08002216 unsigned long mask;
2217 struct rcu_node *rnp_p;
2218
Matthew Wilcoxa32e01e2018-01-17 06:24:30 -08002219 raw_lockdep_assert_held_rcu_node(rnp);
Paul E. McKenney45975c72018-07-02 14:30:37 -07002220 if (WARN_ON_ONCE(!IS_ENABLED(CONFIG_PREEMPT)) ||
Paul E. McKenneyc74859d2018-04-27 14:05:27 -07002221 WARN_ON_ONCE(rcu_preempt_blocked_readers_cgp(rnp)) ||
2222 rnp->qsmask != 0) {
Boqun Feng67c583a72015-12-29 12:18:47 +08002223 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
Paul E. McKenneycc99a312015-02-23 08:59:29 -08002224 return; /* Still need more quiescent states! */
2225 }
2226
Paul E. McKenney77cfc7b2018-05-01 15:00:10 -07002227 rnp->completedqs = rnp->gp_seq;
Paul E. McKenneycc99a312015-02-23 08:59:29 -08002228 rnp_p = rnp->parent;
2229 if (rnp_p == NULL) {
2230 /*
Paul E. McKenneya77da142015-03-08 14:52:27 -07002231 * Only one rcu_node structure in the tree, so don't
2232 * try to report up to its nonexistent parent!
Paul E. McKenneycc99a312015-02-23 08:59:29 -08002233 */
Paul E. McKenneyaff4e9e2018-07-03 17:22:34 -07002234 rcu_report_qs_rsp(flags);
Paul E. McKenneycc99a312015-02-23 08:59:29 -08002235 return;
2236 }
2237
Paul E. McKenneyc9a24e22018-04-27 14:54:46 -07002238 /* Report up the rest of the hierarchy, tracking current ->gp_seq. */
2239 gps = rnp->gp_seq;
Paul E. McKenneycc99a312015-02-23 08:59:29 -08002240 mask = rnp->grpmask;
Boqun Feng67c583a72015-12-29 12:18:47 +08002241 raw_spin_unlock_rcu_node(rnp); /* irqs remain disabled. */
Peter Zijlstra2a67e742015-10-08 12:24:23 +02002242 raw_spin_lock_rcu_node(rnp_p); /* irqs already disabled. */
Paul E. McKenneyb50912d2018-07-03 17:22:34 -07002243 rcu_report_qs_rnp(mask, rnp_p, gps, flags);
Paul E. McKenneycc99a312015-02-23 08:59:29 -08002244}
2245
2246/*
Paul E. McKenneyd3f6bad2009-12-02 12:10:13 -08002247 * Record a quiescent state for the specified CPU to that CPU's rcu_data
Paul E. McKenney4b455dc2016-01-27 22:44:45 -08002248 * structure. This must be called from the specified CPU.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002249 */
2250static void
Paul E. McKenney33085c42018-07-03 17:22:34 -07002251rcu_report_qs_rdp(int cpu, struct rcu_data *rdp)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002252{
2253 unsigned long flags;
2254 unsigned long mask;
Paul E. McKenney48a76392014-03-11 13:02:16 -07002255 bool needwake;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002256 struct rcu_node *rnp;
2257
2258 rnp = rdp->mynode;
Peter Zijlstra2a67e742015-10-08 12:24:23 +02002259 raw_spin_lock_irqsave_rcu_node(rnp, flags);
Paul E. McKenneyc9a24e22018-04-27 14:54:46 -07002260 if (rdp->cpu_no_qs.b.norm || rdp->gp_seq != rnp->gp_seq ||
2261 rdp->gpwrap) {
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002262
2263 /*
Paul E. McKenneye4cc1f22011-06-27 00:17:43 -07002264 * The grace period in which this quiescent state was
2265 * recorded has ended, so don't report it upwards.
2266 * We will instead need a new quiescent state that lies
2267 * within the current grace period.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002268 */
Paul E. McKenney5b74c452015-08-06 15:16:57 -07002269 rdp->cpu_no_qs.b.norm = true; /* need qs for new gp. */
Boqun Feng67c583a72015-12-29 12:18:47 +08002270 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002271 return;
2272 }
2273 mask = rdp->grpmask;
2274 if ((rnp->qsmask & mask) == 0) {
Boqun Feng67c583a72015-12-29 12:18:47 +08002275 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002276 } else {
Paul E. McKenneybb53e412015-12-10 09:30:12 -08002277 rdp->core_needs_qs = false;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002278
2279 /*
2280 * This GP can't end until cpu checks in, so all of our
2281 * callbacks can be processed during the next GP.
2282 */
Paul E. McKenney02f50142018-07-03 17:22:34 -07002283 needwake = rcu_accelerate_cbs(rnp, rdp);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002284
Paul E. McKenneyb50912d2018-07-03 17:22:34 -07002285 rcu_report_qs_rnp(mask, rnp, rnp->gp_seq, flags);
Paul E. McKenney654e9532015-03-15 09:19:35 -07002286 /* ^^^ Released rnp->lock */
Paul E. McKenney48a76392014-03-11 13:02:16 -07002287 if (needwake)
Paul E. McKenney532c00c2018-07-03 17:22:34 -07002288 rcu_gp_kthread_wake();
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002289 }
2290}
2291
2292/*
2293 * Check to see if there is a new grace period of which this CPU
2294 * is not yet aware, and if so, set up local rcu_data state for it.
2295 * Otherwise, see if this CPU has just passed through its first
2296 * quiescent state for this grace period, and record that fact if so.
2297 */
2298static void
Paul E. McKenney8087d3e2018-07-03 17:22:34 -07002299rcu_check_quiescent_state(struct rcu_data *rdp)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002300{
Paul E. McKenney05eb5522013-03-19 12:38:24 -07002301 /* Check for grace-period ends and beginnings. */
Paul E. McKenney15cabdf2018-07-03 17:22:34 -07002302 note_gp_changes(rdp);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002303
2304 /*
2305 * Does this CPU still need to do its part for current grace period?
2306 * If no, return and let the other CPUs do their part as well.
2307 */
Paul E. McKenney97c668b2015-08-06 11:31:51 -07002308 if (!rdp->core_needs_qs)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002309 return;
2310
2311 /*
2312 * Was there a quiescent state since the beginning of the grace
2313 * period? If no, then exit and wait for the next call.
2314 */
Paul E. McKenney3a19b462016-11-30 11:21:21 -08002315 if (rdp->cpu_no_qs.b.norm)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002316 return;
2317
Paul E. McKenneyd3f6bad2009-12-02 12:10:13 -08002318 /*
2319 * Tell RCU we are done (but rcu_report_qs_rdp() will be the
2320 * judge of that).
2321 */
Paul E. McKenney33085c42018-07-03 17:22:34 -07002322 rcu_report_qs_rdp(rdp->cpu, rdp);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002323}
2324
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002325/*
Paul E. McKenney780cd592018-07-03 17:22:34 -07002326 * Near the end of the offline process. Trace the fact that this CPU
2327 * is going offline.
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08002328 */
Paul E. McKenney780cd592018-07-03 17:22:34 -07002329int rcutree_dying_cpu(unsigned int cpu)
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08002330{
Paul E. McKenney477351f2018-05-01 12:54:11 -07002331 RCU_TRACE(bool blkd;)
Paul E. McKenneyda1df502018-07-03 15:37:16 -07002332 RCU_TRACE(struct rcu_data *rdp = this_cpu_ptr(&rcu_data);)
Paul E. McKenney88a49762017-01-23 12:04:46 -08002333 RCU_TRACE(struct rcu_node *rnp = rdp->mynode;)
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08002334
Paul E. McKenneyea463512015-03-03 14:05:26 -08002335 if (!IS_ENABLED(CONFIG_HOTPLUG_CPU))
Paul E. McKenney780cd592018-07-03 17:22:34 -07002336 return 0;
Paul E. McKenneyea463512015-03-03 14:05:26 -08002337
Paul E. McKenney477351f2018-05-01 12:54:11 -07002338 RCU_TRACE(blkd = !!(rnp->qsmask & rdp->grpmask);)
Paul E. McKenney780cd592018-07-03 17:22:34 -07002339 trace_rcu_grace_period(rcu_state.name, rnp->gp_seq,
Paul E. McKenney477351f2018-05-01 12:54:11 -07002340 blkd ? TPS("cpuofl") : TPS("cpuofl-bgp"));
Paul E. McKenney780cd592018-07-03 17:22:34 -07002341 return 0;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002342}
2343
2344/*
Paul E. McKenney8af3a5e2014-10-31 11:22:37 -07002345 * All CPUs for the specified rcu_node structure have gone offline,
2346 * and all tasks that were preempted within an RCU read-side critical
2347 * section while running on one of those CPUs have since exited their RCU
2348 * read-side critical section. Some other CPU is reporting this fact with
2349 * the specified rcu_node structure's ->lock held and interrupts disabled.
2350 * This function therefore goes up the tree of rcu_node structures,
2351 * clearing the corresponding bits in the ->qsmaskinit fields. Note that
2352 * the leaf rcu_node structure's ->qsmaskinit field has already been
Paul E. McKenneyc50cbe52018-05-02 13:51:57 -07002353 * updated.
Paul E. McKenney8af3a5e2014-10-31 11:22:37 -07002354 *
2355 * This function does check that the specified rcu_node structure has
2356 * all CPUs offline and no blocked tasks, so it is OK to invoke it
2357 * prematurely. That said, invoking it after the fact will cost you
2358 * a needless lock acquisition. So once it has done its work, don't
2359 * invoke it again.
2360 */
2361static void rcu_cleanup_dead_rnp(struct rcu_node *rnp_leaf)
2362{
2363 long mask;
2364 struct rcu_node *rnp = rnp_leaf;
2365
Paul E. McKenney962aff02018-05-02 12:49:21 -07002366 raw_lockdep_assert_held_rcu_node(rnp_leaf);
Paul E. McKenneyea463512015-03-03 14:05:26 -08002367 if (!IS_ENABLED(CONFIG_HOTPLUG_CPU) ||
Paul E. McKenney962aff02018-05-02 12:49:21 -07002368 WARN_ON_ONCE(rnp_leaf->qsmaskinit) ||
2369 WARN_ON_ONCE(rcu_preempt_has_tasks(rnp_leaf)))
Paul E. McKenney8af3a5e2014-10-31 11:22:37 -07002370 return;
2371 for (;;) {
2372 mask = rnp->grpmask;
2373 rnp = rnp->parent;
2374 if (!rnp)
2375 break;
Peter Zijlstra2a67e742015-10-08 12:24:23 +02002376 raw_spin_lock_rcu_node(rnp); /* irqs already disabled. */
Paul E. McKenney8af3a5e2014-10-31 11:22:37 -07002377 rnp->qsmaskinit &= ~mask;
Paul E. McKenney962aff02018-05-02 12:49:21 -07002378 /* Between grace periods, so better already be zero! */
2379 WARN_ON_ONCE(rnp->qsmask);
Paul E. McKenney8af3a5e2014-10-31 11:22:37 -07002380 if (rnp->qsmaskinit) {
Boqun Feng67c583a72015-12-29 12:18:47 +08002381 raw_spin_unlock_rcu_node(rnp);
2382 /* irqs remain disabled. */
Paul E. McKenney8af3a5e2014-10-31 11:22:37 -07002383 return;
2384 }
Boqun Feng67c583a72015-12-29 12:18:47 +08002385 raw_spin_unlock_rcu_node(rnp); /* irqs remain disabled. */
Paul E. McKenney8af3a5e2014-10-31 11:22:37 -07002386 }
2387}
2388
2389/*
Paul E. McKenneye5601402012-01-07 11:03:57 -08002390 * The CPU has been completely removed, and some other CPU is reporting
Paul E. McKenneya58163d2017-06-20 12:11:34 -07002391 * this fact from process context. Do the remainder of the cleanup.
2392 * There can only be one CPU hotplug operation at a time, so no need for
2393 * explicit locking.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002394 */
Paul E. McKenney780cd592018-07-03 17:22:34 -07002395int rcutree_dead_cpu(unsigned int cpu)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002396{
Paul E. McKenneyda1df502018-07-03 15:37:16 -07002397 struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08002398 struct rcu_node *rnp = rdp->mynode; /* Outgoing CPU's rdp & rnp. */
Paul E. McKenneye5601402012-01-07 11:03:57 -08002399
Paul E. McKenneyea463512015-03-03 14:05:26 -08002400 if (!IS_ENABLED(CONFIG_HOTPLUG_CPU))
Paul E. McKenney780cd592018-07-03 17:22:34 -07002401 return 0;
Paul E. McKenneyea463512015-03-03 14:05:26 -08002402
Paul E. McKenney2036d942012-01-30 17:02:47 -08002403 /* Adjust any no-longer-needed kthreads. */
Thomas Gleixner5d01bbd2012-07-16 10:42:35 +00002404 rcu_boost_kthread_setaffinity(rnp, -1);
Paul E. McKenney780cd592018-07-03 17:22:34 -07002405 /* Do any needed no-CB deferred wakeups from this CPU. */
2406 do_nocb_deferred_wakeup(per_cpu_ptr(&rcu_data, cpu));
2407 return 0;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002408}
2409
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002410/*
2411 * Invoke any RCU callbacks that have made it to the end of their grace
2412 * period. Thottle as specified by rdp->blimit.
2413 */
Paul E. McKenney5bb5d092018-07-03 17:22:34 -07002414static void rcu_do_batch(struct rcu_data *rdp)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002415{
2416 unsigned long flags;
Paul E. McKenney15fecf82017-02-08 12:36:42 -08002417 struct rcu_head *rhp;
2418 struct rcu_cblist rcl = RCU_CBLIST_INITIALIZER(rcl);
2419 long bl, count;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002420
Paul E. McKenneydc35c892012-12-03 13:52:00 -08002421 /* If no callbacks are ready, just return. */
Paul E. McKenney15fecf82017-02-08 12:36:42 -08002422 if (!rcu_segcblist_ready_cbs(&rdp->cblist)) {
Paul E. McKenney3c779df2018-07-05 15:54:02 -07002423 trace_rcu_batch_start(rcu_state.name,
Paul E. McKenney15fecf82017-02-08 12:36:42 -08002424 rcu_segcblist_n_lazy_cbs(&rdp->cblist),
2425 rcu_segcblist_n_cbs(&rdp->cblist), 0);
Paul E. McKenney3c779df2018-07-05 15:54:02 -07002426 trace_rcu_batch_end(rcu_state.name, 0,
Paul E. McKenney15fecf82017-02-08 12:36:42 -08002427 !rcu_segcblist_empty(&rdp->cblist),
Paul E. McKenney4968c302011-12-07 16:32:40 -08002428 need_resched(), is_idle_task(current),
2429 rcu_is_callbacks_kthread());
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002430 return;
Paul E. McKenney29c00b42011-06-17 15:53:19 -07002431 }
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002432
2433 /*
2434 * Extract the list of ready callbacks, disabling to prevent
Paul E. McKenney15fecf82017-02-08 12:36:42 -08002435 * races with call_rcu() from interrupt handlers. Leave the
2436 * callback counts, as rcu_barrier() needs to be conservative.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002437 */
2438 local_irq_save(flags);
Paul E. McKenney8146c4e22012-01-10 14:23:29 -08002439 WARN_ON_ONCE(cpu_is_offline(smp_processor_id()));
Paul E. McKenney29c00b42011-06-17 15:53:19 -07002440 bl = rdp->blimit;
Paul E. McKenney3c779df2018-07-05 15:54:02 -07002441 trace_rcu_batch_start(rcu_state.name,
2442 rcu_segcblist_n_lazy_cbs(&rdp->cblist),
Paul E. McKenney15fecf82017-02-08 12:36:42 -08002443 rcu_segcblist_n_cbs(&rdp->cblist), bl);
2444 rcu_segcblist_extract_done_cbs(&rdp->cblist, &rcl);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002445 local_irq_restore(flags);
2446
2447 /* Invoke callbacks. */
Paul E. McKenney15fecf82017-02-08 12:36:42 -08002448 rhp = rcu_cblist_dequeue(&rcl);
2449 for (; rhp; rhp = rcu_cblist_dequeue(&rcl)) {
2450 debug_rcu_head_unqueue(rhp);
Paul E. McKenney3c779df2018-07-05 15:54:02 -07002451 if (__rcu_reclaim(rcu_state.name, rhp))
Paul E. McKenney15fecf82017-02-08 12:36:42 -08002452 rcu_cblist_dequeued_lazy(&rcl);
2453 /*
2454 * Stop only if limit reached and CPU has something to do.
2455 * Note: The rcl structure counts down from zero.
2456 */
Paul E. McKenney4b27f202017-05-02 08:45:25 -07002457 if (-rcl.len >= bl &&
Paul E. McKenneydff16722011-11-29 15:57:13 -08002458 (need_resched() ||
2459 (!is_idle_task(current) && !rcu_is_callbacks_kthread())))
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002460 break;
2461 }
2462
2463 local_irq_save(flags);
Paul E. McKenney4b27f202017-05-02 08:45:25 -07002464 count = -rcl.len;
Paul E. McKenney3c779df2018-07-05 15:54:02 -07002465 trace_rcu_batch_end(rcu_state.name, count, !!rcl.head, need_resched(),
Paul E. McKenney8ef0f372017-05-02 08:18:40 -07002466 is_idle_task(current), rcu_is_callbacks_kthread());
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002467
Paul E. McKenney15fecf82017-02-08 12:36:42 -08002468 /* Update counts and requeue any remaining callbacks. */
2469 rcu_segcblist_insert_done_cbs(&rdp->cblist, &rcl);
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08002470 smp_mb(); /* List handling before counting for rcu_barrier(). */
Paul E. McKenney15fecf82017-02-08 12:36:42 -08002471 rcu_segcblist_insert_count(&rdp->cblist, &rcl);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002472
2473 /* Reinstate batch limit if we have worked down the excess. */
Paul E. McKenney15fecf82017-02-08 12:36:42 -08002474 count = rcu_segcblist_n_cbs(&rdp->cblist);
2475 if (rdp->blimit == LONG_MAX && count <= qlowmark)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002476 rdp->blimit = blimit;
2477
Paul E. McKenney37c72e52009-10-14 10:15:55 -07002478 /* Reset ->qlen_last_fqs_check trigger if enough CBs have drained. */
Paul E. McKenney15fecf82017-02-08 12:36:42 -08002479 if (count == 0 && rdp->qlen_last_fqs_check != 0) {
Paul E. McKenney37c72e52009-10-14 10:15:55 -07002480 rdp->qlen_last_fqs_check = 0;
Paul E. McKenney3c779df2018-07-05 15:54:02 -07002481 rdp->n_force_qs_snap = rcu_state.n_force_qs;
Paul E. McKenney15fecf82017-02-08 12:36:42 -08002482 } else if (count < rdp->qlen_last_fqs_check - qhimark)
2483 rdp->qlen_last_fqs_check = count;
Paul E. McKenneyefd88b02017-10-19 14:52:41 -07002484
2485 /*
2486 * The following usually indicates a double call_rcu(). To track
2487 * this down, try building with CONFIG_DEBUG_OBJECTS_RCU_HEAD=y.
2488 */
Paul E. McKenney15fecf82017-02-08 12:36:42 -08002489 WARN_ON_ONCE(rcu_segcblist_empty(&rdp->cblist) != (count == 0));
Paul E. McKenney37c72e52009-10-14 10:15:55 -07002490
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002491 local_irq_restore(flags);
2492
Paul E. McKenneye0f23062011-06-21 01:29:39 -07002493 /* Re-invoke RCU core processing if there are callbacks remaining. */
Paul E. McKenney15fecf82017-02-08 12:36:42 -08002494 if (rcu_segcblist_ready_cbs(&rdp->cblist))
Paul E. McKenneya46e0892011-06-15 15:47:09 -07002495 invoke_rcu_core();
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002496}
2497
2498/*
2499 * Check to see if this CPU is in a non-context-switch quiescent state
2500 * (user mode or idle loop for rcu, non-softirq execution for rcu_bh).
Paul E. McKenneye0f23062011-06-21 01:29:39 -07002501 * Also schedule RCU core processing.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002502 *
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -07002503 * This function must be called from hardirq context. It is normally
Paul E. McKenney5403d362016-09-01 05:04:24 -07002504 * invoked from the scheduling-clock interrupt.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002505 */
Paul E. McKenneyc3377c2d2014-10-21 07:53:02 -07002506void rcu_check_callbacks(int user)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002507{
Steven Rostedt (Red Hat)f7f7bac2013-07-12 17:18:47 -04002508 trace_rcu_utilization(TPS("Start scheduler-tick"));
Paul E. McKenney4e950202018-07-05 17:59:36 -07002509 raw_cpu_inc(rcu_data.ticks_this_gp);
Paul E. McKenney92aa39e2018-07-09 13:47:30 -07002510 /* The load-acquire pairs with the store-release setting to true. */
Paul E. McKenney2dba13f2018-08-03 21:00:38 -07002511 if (smp_load_acquire(this_cpu_ptr(&rcu_data.rcu_urgent_qs))) {
Paul E. McKenney92aa39e2018-07-09 13:47:30 -07002512 /* Idle and userspace execution already are quiescent states. */
Paul E. McKenneya0ef9ec2018-07-09 15:50:16 -07002513 if (!rcu_is_cpu_rrupt_from_idle() && !user) {
Paul E. McKenney92aa39e2018-07-09 13:47:30 -07002514 set_tsk_need_resched(current);
2515 set_preempt_need_resched();
2516 }
Paul E. McKenney2dba13f2018-08-03 21:00:38 -07002517 __this_cpu_write(rcu_data.rcu_urgent_qs, false);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002518 }
Paul E. McKenney45975c72018-07-02 14:30:37 -07002519 rcu_flavor_check_callbacks(user);
Paul E. McKenneye3950ec2014-10-21 08:03:57 -07002520 if (rcu_pending())
Paul E. McKenneya46e0892011-06-15 15:47:09 -07002521 invoke_rcu_core();
Byungchul Park07f27572018-05-11 17:30:34 +09002522
Steven Rostedt (Red Hat)f7f7bac2013-07-12 17:18:47 -04002523 trace_rcu_utilization(TPS("End scheduler-tick"));
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002524}
2525
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002526/*
2527 * Scan the leaf rcu_node structures, processing dyntick state for any that
2528 * have not yet encountered a quiescent state, using the function specified.
Paul E. McKenney27f4d282011-02-07 12:47:15 -08002529 * Also initiate boosting for any threads blocked on the root rcu_node.
2530 *
Paul E. McKenneyee47eb92010-01-04 15:09:07 -08002531 * The caller must have suppressed start of new grace periods.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002532 */
Paul E. McKenney8ff0b902018-07-05 17:55:14 -07002533static void force_qs_rnp(int (*f)(struct rcu_data *rdp))
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002534{
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002535 int cpu;
2536 unsigned long flags;
2537 unsigned long mask;
Paul E. McKenneya0b6c9a2009-09-28 07:46:33 -07002538 struct rcu_node *rnp;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002539
Paul E. McKenneyaedf4ba2018-07-04 14:33:59 -07002540 rcu_for_each_leaf_node(rnp) {
Paul E. McKenneycee43932018-03-02 16:35:27 -08002541 cond_resched_tasks_rcu_qs();
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002542 mask = 0;
Peter Zijlstra2a67e742015-10-08 12:24:23 +02002543 raw_spin_lock_irqsave_rcu_node(rnp, flags);
Paul E. McKenneya0b6c9a2009-09-28 07:46:33 -07002544 if (rnp->qsmask == 0) {
Paul E. McKenney45975c72018-07-02 14:30:37 -07002545 if (!IS_ENABLED(CONFIG_PREEMPT) ||
Paul E. McKenneya77da142015-03-08 14:52:27 -07002546 rcu_preempt_blocked_readers_cgp(rnp)) {
2547 /*
2548 * No point in scanning bits because they
2549 * are all zero. But we might need to
2550 * priority-boost blocked readers.
2551 */
2552 rcu_initiate_boost(rnp, flags);
2553 /* rcu_initiate_boost() releases rnp->lock */
2554 continue;
2555 }
Paul E. McKenney92816432018-05-02 11:07:02 -07002556 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
2557 continue;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002558 }
Mark Rutlandbc75e992016-06-03 15:20:04 +01002559 for_each_leaf_node_possible_cpu(rnp, cpu) {
2560 unsigned long bit = leaf_node_cpu_bit(rnp, cpu);
Paul E. McKenney0edd1b12013-06-21 16:37:22 -07002561 if ((rnp->qsmask & bit) != 0) {
Paul E. McKenneyda1df502018-07-03 15:37:16 -07002562 if (f(per_cpu_ptr(&rcu_data, cpu)))
Paul E. McKenney0edd1b12013-06-21 16:37:22 -07002563 mask |= bit;
2564 }
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002565 }
Paul E. McKenney45f014c52e2010-01-04 15:09:08 -08002566 if (mask != 0) {
Paul E. McKenneyc9a24e22018-04-27 14:54:46 -07002567 /* Idle/offline CPUs, report (releases rnp->lock). */
Paul E. McKenneyb50912d2018-07-03 17:22:34 -07002568 rcu_report_qs_rnp(mask, rnp, rnp->gp_seq, flags);
Paul E. McKenney0aa04b02015-01-23 21:52:37 -08002569 } else {
2570 /* Nothing to do here, so just drop the lock. */
Boqun Feng67c583a72015-12-29 12:18:47 +08002571 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002572 }
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002573 }
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002574}
2575
2576/*
2577 * Force quiescent states on reluctant CPUs, and also detect which
2578 * CPUs are in dyntick-idle mode.
2579 */
Paul E. McKenneye9ecb782018-07-03 17:22:34 -07002580static void force_quiescent_state(void)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002581{
2582 unsigned long flags;
Paul E. McKenney394f2762012-06-26 17:00:35 -07002583 bool ret;
2584 struct rcu_node *rnp;
2585 struct rcu_node *rnp_old = NULL;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002586
Paul E. McKenney394f2762012-06-26 17:00:35 -07002587 /* Funnel through hierarchy to reduce memory contention. */
Paul E. McKenneyda1df502018-07-03 15:37:16 -07002588 rnp = __this_cpu_read(rcu_data.mynode);
Paul E. McKenney394f2762012-06-26 17:00:35 -07002589 for (; rnp != NULL; rnp = rnp->parent) {
Paul E. McKenney67a0edb2018-07-05 16:15:38 -07002590 ret = (READ_ONCE(rcu_state.gp_flags) & RCU_GP_FLAG_FQS) ||
Paul E. McKenney394f2762012-06-26 17:00:35 -07002591 !raw_spin_trylock(&rnp->fqslock);
2592 if (rnp_old != NULL)
2593 raw_spin_unlock(&rnp_old->fqslock);
Paul E. McKenneyd62df572018-01-10 13:10:49 -08002594 if (ret)
Paul E. McKenney394f2762012-06-26 17:00:35 -07002595 return;
Paul E. McKenney394f2762012-06-26 17:00:35 -07002596 rnp_old = rnp;
2597 }
Paul E. McKenney336a4f62018-07-03 17:22:34 -07002598 /* rnp_old == rcu_get_root(), rnp == NULL. */
Paul E. McKenney394f2762012-06-26 17:00:35 -07002599
2600 /* Reached the root of the rcu_node tree, acquire lock. */
Peter Zijlstra2a67e742015-10-08 12:24:23 +02002601 raw_spin_lock_irqsave_rcu_node(rnp_old, flags);
Paul E. McKenney394f2762012-06-26 17:00:35 -07002602 raw_spin_unlock(&rnp_old->fqslock);
Paul E. McKenney67a0edb2018-07-05 16:15:38 -07002603 if (READ_ONCE(rcu_state.gp_flags) & RCU_GP_FLAG_FQS) {
Boqun Feng67c583a72015-12-29 12:18:47 +08002604 raw_spin_unlock_irqrestore_rcu_node(rnp_old, flags);
Paul E. McKenney4cdfc1752012-06-22 17:06:26 -07002605 return; /* Someone beat us to it. */
Paul E. McKenney46a1e342010-01-04 15:09:09 -08002606 }
Paul E. McKenney67a0edb2018-07-05 16:15:38 -07002607 WRITE_ONCE(rcu_state.gp_flags,
2608 READ_ONCE(rcu_state.gp_flags) | RCU_GP_FLAG_FQS);
Boqun Feng67c583a72015-12-29 12:18:47 +08002609 raw_spin_unlock_irqrestore_rcu_node(rnp_old, flags);
Paul E. McKenney532c00c2018-07-03 17:22:34 -07002610 rcu_gp_kthread_wake();
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002611}
2612
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002613/*
Paul E. McKenney26d950a2018-04-21 20:44:11 -07002614 * This function checks for grace-period requests that fail to motivate
2615 * RCU to come out of its idle mode.
2616 */
Paul E. McKenney791416c2018-10-01 15:42:44 -07002617void
2618rcu_check_gp_start_stall(struct rcu_node *rnp, struct rcu_data *rdp,
2619 const unsigned long gpssdelay)
Paul E. McKenney26d950a2018-04-21 20:44:11 -07002620{
2621 unsigned long flags;
2622 unsigned long j;
Paul E. McKenney336a4f62018-07-03 17:22:34 -07002623 struct rcu_node *rnp_root = rcu_get_root();
Paul E. McKenney26d950a2018-04-21 20:44:11 -07002624 static atomic_t warned = ATOMIC_INIT(0);
2625
Paul E. McKenneyde8e8732018-07-03 17:22:34 -07002626 if (!IS_ENABLED(CONFIG_PROVE_RCU) || rcu_gp_in_progress() ||
Paul E. McKenney7a1d0f22018-05-01 10:26:57 -07002627 ULONG_CMP_GE(rnp_root->gp_seq, rnp_root->gp_seq_needed))
Paul E. McKenney26d950a2018-04-21 20:44:11 -07002628 return;
2629 j = jiffies; /* Expensive access, and in common case don't get here. */
Paul E. McKenney67a0edb2018-07-05 16:15:38 -07002630 if (time_before(j, READ_ONCE(rcu_state.gp_req_activity) + gpssdelay) ||
2631 time_before(j, READ_ONCE(rcu_state.gp_activity) + gpssdelay) ||
Paul E. McKenney26d950a2018-04-21 20:44:11 -07002632 atomic_read(&warned))
2633 return;
2634
2635 raw_spin_lock_irqsave_rcu_node(rnp, flags);
2636 j = jiffies;
Paul E. McKenneyde8e8732018-07-03 17:22:34 -07002637 if (rcu_gp_in_progress() ||
Paul E. McKenney7a1d0f22018-05-01 10:26:57 -07002638 ULONG_CMP_GE(rnp_root->gp_seq, rnp_root->gp_seq_needed) ||
Paul E. McKenney67a0edb2018-07-05 16:15:38 -07002639 time_before(j, READ_ONCE(rcu_state.gp_req_activity) + gpssdelay) ||
2640 time_before(j, READ_ONCE(rcu_state.gp_activity) + gpssdelay) ||
Paul E. McKenney26d950a2018-04-21 20:44:11 -07002641 atomic_read(&warned)) {
2642 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
2643 return;
2644 }
2645 /* Hold onto the leaf lock to make others see warned==1. */
2646
2647 if (rnp_root != rnp)
2648 raw_spin_lock_rcu_node(rnp_root); /* irqs already disabled. */
2649 j = jiffies;
Paul E. McKenneyde8e8732018-07-03 17:22:34 -07002650 if (rcu_gp_in_progress() ||
Paul E. McKenney7a1d0f22018-05-01 10:26:57 -07002651 ULONG_CMP_GE(rnp_root->gp_seq, rnp_root->gp_seq_needed) ||
Paul E. McKenney67a0edb2018-07-05 16:15:38 -07002652 time_before(j, rcu_state.gp_req_activity + gpssdelay) ||
2653 time_before(j, rcu_state.gp_activity + gpssdelay) ||
Paul E. McKenney26d950a2018-04-21 20:44:11 -07002654 atomic_xchg(&warned, 1)) {
2655 raw_spin_unlock_rcu_node(rnp_root); /* irqs remain disabled. */
2656 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
2657 return;
2658 }
Paul E. McKenneyb06ae252018-05-17 13:32:51 -07002659 pr_alert("%s: g%ld->%ld gar:%lu ga:%lu f%#x gs:%d %s->state:%#lx\n",
Paul E. McKenney67a0edb2018-07-05 16:15:38 -07002660 __func__, (long)READ_ONCE(rcu_state.gp_seq),
Paul E. McKenney7a1d0f22018-05-01 10:26:57 -07002661 (long)READ_ONCE(rnp_root->gp_seq_needed),
Paul E. McKenney67a0edb2018-07-05 16:15:38 -07002662 j - rcu_state.gp_req_activity, j - rcu_state.gp_activity,
2663 rcu_state.gp_flags, rcu_state.gp_state, rcu_state.name,
2664 rcu_state.gp_kthread ? rcu_state.gp_kthread->state : 0x1ffffL);
Paul E. McKenney26d950a2018-04-21 20:44:11 -07002665 WARN_ON(1);
2666 if (rnp_root != rnp)
2667 raw_spin_unlock_rcu_node(rnp_root);
2668 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
2669}
2670
2671/*
Paul E. McKenneye0aff972018-10-01 17:40:54 -07002672 * Do a forward-progress check for rcutorture. This is normally invoked
2673 * due to an OOM event. The argument "j" gives the time period during
2674 * which rcutorture would like progress to have been made.
2675 */
2676void rcu_fwd_progress_check(unsigned long j)
2677{
2678 struct rcu_data *rdp;
2679
2680 if (rcu_gp_in_progress()) {
2681 show_rcu_gp_kthreads();
2682 } else {
2683 preempt_disable();
2684 rdp = this_cpu_ptr(&rcu_data);
2685 rcu_check_gp_start_stall(rdp->mynode, rdp, j);
2686 preempt_enable();
2687 }
2688}
2689EXPORT_SYMBOL_GPL(rcu_fwd_progress_check);
2690
2691/*
Paul E. McKenneyb049fdf2018-07-03 17:22:34 -07002692 * This does the RCU core processing work for the specified rcu_data
2693 * structures. This may be called only from the CPU to whom the rdp
2694 * belongs.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002695 */
Emese Revfy0766f782016-06-20 20:42:34 +02002696static __latent_entropy void rcu_process_callbacks(struct softirq_action *unused)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002697{
Shaohua Li09223372011-06-14 13:26:25 +08002698 unsigned long flags;
Paul E. McKenneyda1df502018-07-03 15:37:16 -07002699 struct rcu_data *rdp = raw_cpu_ptr(&rcu_data);
Paul E. McKenneyb0f74032013-02-04 12:14:24 -08002700 struct rcu_node *rnp = rdp->mynode;
Paul E. McKenneya26ac242011-01-12 14:10:23 -08002701
2702 if (cpu_is_offline(smp_processor_id()))
Peter Zijlstra08bca602011-05-20 16:06:29 -07002703 return;
Paul E. McKenneya26ac242011-01-12 14:10:23 -08002704 trace_rcu_utilization(TPS("Start RCU core"));
Shaohua Li09223372011-06-14 13:26:25 +08002705 WARN_ON_ONCE(!rdp->beenonline);
Paul E. McKenney29154c52012-05-30 03:21:48 -07002706
Paul E. McKenney3e310092018-06-21 12:50:01 -07002707 /* Report any deferred quiescent states if preemption enabled. */
Paul E. McKenneyfced9c82018-07-26 13:44:00 -07002708 if (!(preempt_count() & PREEMPT_MASK)) {
Paul E. McKenney3e310092018-06-21 12:50:01 -07002709 rcu_preempt_deferred_qs(current);
Paul E. McKenneyfced9c82018-07-26 13:44:00 -07002710 } else if (rcu_preempt_need_deferred_qs(current)) {
2711 set_tsk_need_resched(current);
2712 set_preempt_need_resched();
2713 }
Paul E. McKenney3e310092018-06-21 12:50:01 -07002714
Paul E. McKenney29154c52012-05-30 03:21:48 -07002715 /* Update RCU state based on any recent quiescent states. */
Paul E. McKenney8087d3e2018-07-03 17:22:34 -07002716 rcu_check_quiescent_state(rdp);
Paul E. McKenney29154c52012-05-30 03:21:48 -07002717
2718 /* No grace period and unregistered callbacks? */
Paul E. McKenneyde8e8732018-07-03 17:22:34 -07002719 if (!rcu_gp_in_progress() &&
Paul E. McKenney48a76392014-03-11 13:02:16 -07002720 rcu_segcblist_is_enabled(&rdp->cblist)) {
2721 local_irq_save(flags);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002722 if (!rcu_segcblist_restempty(&rdp->cblist, RCU_NEXT_READY_TAIL))
Paul E. McKenneyc6e09b92018-07-03 17:22:34 -07002723 rcu_accelerate_cbs_unlocked(rnp, rdp);
Paul E. McKenney29154c52012-05-30 03:21:48 -07002724 local_irq_restore(flags);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002725 }
Yao Dongdong9910aff2015-02-25 17:09:46 +08002726
Paul E. McKenney791416c2018-10-01 15:42:44 -07002727 rcu_check_gp_start_stall(rnp, rdp, rcu_jiffies_till_stall_check());
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002728
Paul E. McKenney29154c52012-05-30 03:21:48 -07002729 /* If there are callbacks ready, invoke them. */
2730 if (rcu_segcblist_ready_cbs(&rdp->cblist))
Paul E. McKenneyaff4e9e2018-07-03 17:22:34 -07002731 invoke_rcu_callbacks(rdp);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002732
2733 /* Do any needed deferred wakeups of rcuo kthreads. */
2734 do_nocb_deferred_wakeup(rdp);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002735 trace_rcu_utilization(TPS("End RCU core"));
2736}
2737
2738/*
Paul E. McKenney49918a52018-07-07 18:12:26 -07002739 * Schedule RCU callback invocation. If the running implementation of RCU
2740 * does not support RCU priority boosting, just do a direct call, otherwise
2741 * wake up the per-CPU kernel kthread. Note that because we are running
2742 * on the current CPU with softirqs disabled, the rcu_cpu_kthread_task
2743 * cannot disappear out from under us.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002744 */
Paul E. McKenneyaff4e9e2018-07-03 17:22:34 -07002745static void invoke_rcu_callbacks(struct rcu_data *rdp)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002746{
2747 if (unlikely(!READ_ONCE(rcu_scheduler_fully_active)))
Paul E. McKenney2655d572011-04-07 22:47:23 -07002748 return;
Paul E. McKenney3c779df2018-07-05 15:54:02 -07002749 if (likely(!rcu_state.boost)) {
Paul E. McKenney5bb5d092018-07-03 17:22:34 -07002750 rcu_do_batch(rdp);
Paul E. McKenneyc57afe82012-02-28 11:02:21 -08002751 return;
2752 }
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08002753 invoke_rcu_callbacks_kthread();
2754}
2755
Paul E. McKenney2655d572011-04-07 22:47:23 -07002756static void invoke_rcu_core(void)
Paul E. McKenneyd4c08f22011-06-25 06:36:56 -07002757{
2758 if (cpu_online(smp_processor_id()))
Paul E. McKenney486e2592012-01-06 14:11:30 -08002759 raise_softirq(RCU_SOFTIRQ);
Paul E. McKenneyd4c08f22011-06-25 06:36:56 -07002760}
Paul E. McKenney486e2592012-01-06 14:11:30 -08002761
Paul E. McKenneyd4c08f22011-06-25 06:36:56 -07002762/*
Paul E. McKenney2655d572011-04-07 22:47:23 -07002763 * Handle any core-RCU processing required by a call_rcu() invocation.
2764 */
Paul E. McKenney5c7d8962018-07-03 17:22:34 -07002765static void __call_rcu_core(struct rcu_data *rdp, struct rcu_head *head,
2766 unsigned long flags)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002767{
Paul E. McKenney37c72e52009-10-14 10:15:55 -07002768 /*
2769 * If called from an extended quiescent state, invoke the RCU
2770 * core in order to force a re-evaluation of RCU's idleness.
2771 */
2772 if (!rcu_is_watching())
Paul E. McKenney2655d572011-04-07 22:47:23 -07002773 invoke_rcu_core();
Paul E. McKenneyb52573d2010-12-14 17:36:02 -08002774
2775 /* If interrupts were disabled or CPU offline, don't invoke RCU core. */
Paul E. McKenney470716f2013-03-19 11:32:11 -07002776 if (irqs_disabled_flags(flags) || cpu_is_offline(smp_processor_id()))
Paul E. McKenneyb52573d2010-12-14 17:36:02 -08002777 return;
2778
2779 /*
Paul E. McKenneyb52573d2010-12-14 17:36:02 -08002780 * Force the grace period if too many callbacks or too long waiting.
2781 * Enforce hysteresis, and don't invoke force_quiescent_state()
Paul E. McKenneyb8462082012-12-29 22:04:18 -08002782 * if some other CPU has recently done so. Also, don't bother
2783 * invoking force_quiescent_state() if the newly enqueued callback
2784 * is the only one waiting for a grace period to complete.
Paul E. McKenneyb52573d2010-12-14 17:36:02 -08002785 */
Paul E. McKenney15fecf82017-02-08 12:36:42 -08002786 if (unlikely(rcu_segcblist_n_cbs(&rdp->cblist) >
2787 rdp->qlen_last_fqs_check + qhimark)) {
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002788
2789 /* Are we ignoring a completed grace period? */
Paul E. McKenney15cabdf2018-07-03 17:22:34 -07002790 note_gp_changes(rdp);
Paul E. McKenneyb52573d2010-12-14 17:36:02 -08002791
2792 /* Start a new grace period if one not already started. */
Paul E. McKenneyde8e8732018-07-03 17:22:34 -07002793 if (!rcu_gp_in_progress()) {
Paul E. McKenneyc6e09b92018-07-03 17:22:34 -07002794 rcu_accelerate_cbs_unlocked(rdp->mynode, rdp);
Paul E. McKenneyb52573d2010-12-14 17:36:02 -08002795 } else {
2796 /* Give the grace period a kick. */
2797 rdp->blimit = LONG_MAX;
Paul E. McKenney5c7d8962018-07-03 17:22:34 -07002798 if (rcu_state.n_force_qs == rdp->n_force_qs_snap &&
Paul E. McKenney15fecf82017-02-08 12:36:42 -08002799 rcu_segcblist_first_pend_cb(&rdp->cblist) != head)
Paul E. McKenneye9ecb782018-07-03 17:22:34 -07002800 force_quiescent_state();
Paul E. McKenney5c7d8962018-07-03 17:22:34 -07002801 rdp->n_force_qs_snap = rcu_state.n_force_qs;
Paul E. McKenney15fecf82017-02-08 12:36:42 -08002802 rdp->qlen_last_fqs_check = rcu_segcblist_n_cbs(&rdp->cblist);
Paul E. McKenneyb52573d2010-12-14 17:36:02 -08002803 }
Paul E. McKenney4cdfc1752012-06-22 17:06:26 -07002804 }
Paul E. McKenney29154c52012-05-30 03:21:48 -07002805}
2806
Paul E. McKenney3fbfbf72012-08-19 21:35:53 -07002807/*
Paul E. McKenneyae150182013-04-23 13:20:57 -07002808 * RCU callback function to leak a callback.
2809 */
2810static void rcu_leak_callback(struct rcu_head *rhp)
2811{
2812}
2813
2814/*
Paul E. McKenney3fbfbf72012-08-19 21:35:53 -07002815 * Helper function for call_rcu() and friends. The cpu argument will
2816 * normally be -1, indicating "currently running CPU". It may specify
Paul E. McKenneydd46a782018-07-10 18:37:30 -07002817 * a CPU only if that CPU is a no-CBs CPU. Currently, only rcu_barrier()
Paul E. McKenney3fbfbf72012-08-19 21:35:53 -07002818 * is expected to specify a CPU.
2819 */
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002820static void
Paul E. McKenney5c7d8962018-07-03 17:22:34 -07002821__call_rcu(struct rcu_head *head, rcu_callback_t func, int cpu, bool lazy)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002822{
2823 unsigned long flags;
2824 struct rcu_data *rdp;
2825
Paul E. McKenneyb8f2ed52016-08-23 06:51:47 -07002826 /* Misaligned rcu_head! */
2827 WARN_ON_ONCE((unsigned long)head & (sizeof(void *) - 1));
2828
Paul E. McKenneyae150182013-04-23 13:20:57 -07002829 if (debug_rcu_head_queue(head)) {
Paul E. McKenneyfa3c6642017-05-03 11:38:55 -07002830 /*
2831 * Probable double call_rcu(), so leak the callback.
2832 * Use rcu:rcu_callback trace event to find the previous
2833 * time callback was passed to __call_rcu().
2834 */
2835 WARN_ONCE(1, "__call_rcu(): Double-freed CB %p->%pF()!!!\n",
2836 head, head->func);
Paul E. McKenney7d0ae802015-03-03 14:57:58 -08002837 WRITE_ONCE(head->func, rcu_leak_callback);
Paul E. McKenneyae150182013-04-23 13:20:57 -07002838 return;
2839 }
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002840 head->func = func;
2841 head->next = NULL;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002842 local_irq_save(flags);
Paul E. McKenneyda1df502018-07-03 15:37:16 -07002843 rdp = this_cpu_ptr(&rcu_data);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002844
2845 /* Add the callback to our list. */
Paul E. McKenney15fecf82017-02-08 12:36:42 -08002846 if (unlikely(!rcu_segcblist_is_enabled(&rdp->cblist)) || cpu != -1) {
Paul E. McKenney3fbfbf72012-08-19 21:35:53 -07002847 int offline;
2848
2849 if (cpu != -1)
Paul E. McKenneyda1df502018-07-03 15:37:16 -07002850 rdp = per_cpu_ptr(&rcu_data, cpu);
Paul E. McKenney143da9c2015-01-19 19:57:32 -08002851 if (likely(rdp->mynode)) {
2852 /* Post-boot, so this should be for a no-CBs CPU. */
2853 offline = !__call_rcu_nocb(rdp, head, lazy, flags);
2854 WARN_ON_ONCE(offline);
2855 /* Offline CPU, _call_rcu() illegal, leak callback. */
2856 local_irq_restore(flags);
2857 return;
2858 }
2859 /*
2860 * Very early boot, before rcu_init(). Initialize if needed
2861 * and then drop through to queue the callback.
2862 */
Paul E. McKenney08543bd2018-10-22 08:04:03 -07002863 WARN_ON_ONCE(cpu != -1);
Paul E. McKenney34404ca2015-01-19 20:39:20 -08002864 WARN_ON_ONCE(!rcu_is_watching());
Paul E. McKenney15fecf82017-02-08 12:36:42 -08002865 if (rcu_segcblist_empty(&rdp->cblist))
2866 rcu_segcblist_init(&rdp->cblist);
Paul E. McKenney0d8ee372012-08-03 13:16:15 -07002867 }
Paul E. McKenney15fecf82017-02-08 12:36:42 -08002868 rcu_segcblist_enqueue(&rdp->cblist, head, lazy);
2869 if (!lazy)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002870 rcu_idle_count_callbacks_posted();
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002871
2872 if (__is_kfree_rcu_offset((unsigned long)func))
Paul E. McKenney3c779df2018-07-05 15:54:02 -07002873 trace_rcu_kfree_callback(rcu_state.name, head,
2874 (unsigned long)func,
Paul E. McKenney15fecf82017-02-08 12:36:42 -08002875 rcu_segcblist_n_lazy_cbs(&rdp->cblist),
2876 rcu_segcblist_n_cbs(&rdp->cblist));
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002877 else
Paul E. McKenney3c779df2018-07-05 15:54:02 -07002878 trace_rcu_callback(rcu_state.name, head,
Paul E. McKenney15fecf82017-02-08 12:36:42 -08002879 rcu_segcblist_n_lazy_cbs(&rdp->cblist),
2880 rcu_segcblist_n_cbs(&rdp->cblist));
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002881
Paul E. McKenney29154c52012-05-30 03:21:48 -07002882 /* Go handle any RCU core processing required. */
Paul E. McKenney5c7d8962018-07-03 17:22:34 -07002883 __call_rcu_core(rdp, head, flags);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002884 local_irq_restore(flags);
2885}
2886
Paul E. McKenneya68a2bb2017-05-03 08:34:57 -07002887/**
Paul E. McKenney45975c72018-07-02 14:30:37 -07002888 * call_rcu() - Queue an RCU callback for invocation after a grace period.
Paul E. McKenneya68a2bb2017-05-03 08:34:57 -07002889 * @head: structure to be used for queueing the RCU updates.
2890 * @func: actual callback function to be invoked after the grace period
2891 *
2892 * The callback function will be invoked some time after a full grace
Paul E. McKenney45975c72018-07-02 14:30:37 -07002893 * period elapses, in other words after all pre-existing RCU read-side
2894 * critical sections have completed. However, the callback function
2895 * might well execute concurrently with RCU read-side critical sections
2896 * that started after call_rcu() was invoked. RCU read-side critical
2897 * sections are delimited by rcu_read_lock() and rcu_read_unlock(), and
2898 * may be nested. In addition, regions of code across which interrupts,
2899 * preemption, or softirqs have been disabled also serve as RCU read-side
2900 * critical sections. This includes hardware interrupt handlers, softirq
2901 * handlers, and NMI handlers.
Paul E. McKenney27fdb352017-10-19 14:26:21 -07002902 *
Paul E. McKenney45975c72018-07-02 14:30:37 -07002903 * Note that all CPUs must agree that the grace period extended beyond
2904 * all pre-existing RCU read-side critical section. On systems with more
2905 * than one CPU, this means that when "func()" is invoked, each CPU is
2906 * guaranteed to have executed a full memory barrier since the end of its
2907 * last RCU read-side critical section whose beginning preceded the call
2908 * to call_rcu(). It also means that each CPU executing an RCU read-side
2909 * critical section that continues beyond the start of "func()" must have
2910 * executed a memory barrier after the call_rcu() but before the beginning
2911 * of that RCU read-side critical section. Note that these guarantees
2912 * include CPUs that are offline, idle, or executing in user mode, as
2913 * well as CPUs that are executing in the kernel.
Paul E. McKenneya68a2bb2017-05-03 08:34:57 -07002914 *
Paul E. McKenney45975c72018-07-02 14:30:37 -07002915 * Furthermore, if CPU A invoked call_rcu() and CPU B invoked the
2916 * resulting RCU callback function "func()", then both CPU A and CPU B are
2917 * guaranteed to execute a full memory barrier during the time interval
2918 * between the call to call_rcu() and the invocation of "func()" -- even
2919 * if CPU A and CPU B are the same CPU (but again only if the system has
2920 * more than one CPU).
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002921 */
Paul E. McKenney45975c72018-07-02 14:30:37 -07002922void call_rcu(struct rcu_head *head, rcu_callback_t func)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002923{
Paul E. McKenney5c7d8962018-07-03 17:22:34 -07002924 __call_rcu(head, func, -1, 0);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002925}
Paul E. McKenney45975c72018-07-02 14:30:37 -07002926EXPORT_SYMBOL_GPL(call_rcu);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002927
Paul E. McKenney6d813392012-02-23 13:30:16 -08002928/*
Andreea-Cristina Bernat495aa962014-03-18 20:48:48 +02002929 * Queue an RCU callback for lazy invocation after a grace period.
2930 * This will likely be later named something like "call_rcu_lazy()",
2931 * but this change will require some way of tagging the lazy RCU
2932 * callbacks in the list of pending callbacks. Until then, this
2933 * function may only be called from __kfree_rcu().
2934 */
Paul E. McKenney98ece502018-07-03 17:22:34 -07002935void kfree_call_rcu(struct rcu_head *head, rcu_callback_t func)
Andreea-Cristina Bernat495aa962014-03-18 20:48:48 +02002936{
Paul E. McKenney5c7d8962018-07-03 17:22:34 -07002937 __call_rcu(head, func, -1, 1);
Andreea-Cristina Bernat495aa962014-03-18 20:48:48 +02002938}
2939EXPORT_SYMBOL_GPL(kfree_call_rcu);
2940
Paul E. McKenney765a3f42014-03-14 16:37:08 -07002941/**
2942 * get_state_synchronize_rcu - Snapshot current RCU state
2943 *
2944 * Returns a cookie that is used by a later call to cond_synchronize_rcu()
2945 * to determine whether or not a full grace period has elapsed in the
2946 * meantime.
2947 */
2948unsigned long get_state_synchronize_rcu(void)
2949{
2950 /*
2951 * Any prior manipulation of RCU-protected data must happen
Paul E. McKenneye4be81a2018-04-27 15:16:50 -07002952 * before the load from ->gp_seq.
Paul E. McKenney765a3f42014-03-14 16:37:08 -07002953 */
2954 smp_mb(); /* ^^^ */
Paul E. McKenney16fc9c62018-07-03 15:54:39 -07002955 return rcu_seq_snap(&rcu_state.gp_seq);
Paul E. McKenney765a3f42014-03-14 16:37:08 -07002956}
2957EXPORT_SYMBOL_GPL(get_state_synchronize_rcu);
2958
2959/**
2960 * cond_synchronize_rcu - Conditionally wait for an RCU grace period
2961 *
2962 * @oldstate: return value from earlier call to get_state_synchronize_rcu()
2963 *
2964 * If a full RCU grace period has elapsed since the earlier call to
2965 * get_state_synchronize_rcu(), just return. Otherwise, invoke
2966 * synchronize_rcu() to wait for a full grace period.
2967 *
2968 * Yes, this function does not take counter wrap into account. But
2969 * counter wrap is harmless. If the counter wraps, we have waited for
2970 * more than 2 billion grace periods (and way more on a 64-bit system!),
2971 * so waiting for one additional grace period should be just fine.
2972 */
2973void cond_synchronize_rcu(unsigned long oldstate)
2974{
Paul E. McKenney16fc9c62018-07-03 15:54:39 -07002975 if (!rcu_seq_done(&rcu_state.gp_seq, oldstate))
Paul E. McKenney765a3f42014-03-14 16:37:08 -07002976 synchronize_rcu();
Paul E. McKenneye4be81a2018-04-27 15:16:50 -07002977 else
2978 smp_mb(); /* Ensure GP ends before subsequent accesses. */
Paul E. McKenney765a3f42014-03-14 16:37:08 -07002979}
2980EXPORT_SYMBOL_GPL(cond_synchronize_rcu);
2981
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002982/*
Paul E. McKenney98ece502018-07-03 17:22:34 -07002983 * Check to see if there is any immediate RCU-related work to be done by
Paul E. McKenney49918a52018-07-07 18:12:26 -07002984 * the current CPU, returning 1 if so and zero otherwise. The checks are
2985 * in order of increasing expense: checks that can be carried out against
2986 * CPU-local state are performed first. However, we must check for CPU
2987 * stalls first, else we might not get a chance.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002988 */
Paul E. McKenney98ece502018-07-03 17:22:34 -07002989static int rcu_pending(void)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002990{
Paul E. McKenney98ece502018-07-03 17:22:34 -07002991 struct rcu_data *rdp = this_cpu_ptr(&rcu_data);
Paul E. McKenney2f51f982009-11-13 19:51:39 -08002992 struct rcu_node *rnp = rdp->mynode;
2993
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002994 /* Check for CPU stalls, if enabled. */
Paul E. McKenneyea12ff22018-07-03 17:22:34 -07002995 check_cpu_stall(rdp);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002996
Paul E. McKenneya0969322013-11-08 09:03:10 -08002997 /* Is this CPU a NO_HZ_FULL CPU that should ignore RCU? */
Paul E. McKenney4580b052018-07-03 17:22:34 -07002998 if (rcu_nohz_full_cpu())
Paul E. McKenneya0969322013-11-08 09:03:10 -08002999 return 0;
3000
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003001 /* Is the RCU core waiting for a quiescent state from this CPU? */
Paul E. McKenney01c495f2018-01-10 12:36:00 -08003002 if (rdp->core_needs_qs && !rdp->cpu_no_qs.b.norm)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003003 return 1;
3004
3005 /* Does this CPU have callbacks ready to invoke? */
Paul E. McKenney01c495f2018-01-10 12:36:00 -08003006 if (rcu_segcblist_ready_cbs(&rdp->cblist))
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003007 return 1;
3008
3009 /* Has RCU gone idle with this CPU needing another grace period? */
Paul E. McKenneyde8e8732018-07-03 17:22:34 -07003010 if (!rcu_gp_in_progress() &&
Paul E. McKenneyc1935202018-04-12 16:29:13 -07003011 rcu_segcblist_is_enabled(&rdp->cblist) &&
3012 !rcu_segcblist_restempty(&rdp->cblist, RCU_NEXT_READY_TAIL))
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003013 return 1;
3014
Paul E. McKenney67e14c12018-04-27 16:01:46 -07003015 /* Have RCU grace period completed or started? */
3016 if (rcu_seq_current(&rnp->gp_seq) != rdp->gp_seq ||
Paul E. McKenney01c495f2018-01-10 12:36:00 -08003017 unlikely(READ_ONCE(rdp->gpwrap))) /* outside lock */
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003018 return 1;
3019
Paul E. McKenney96d3fd02013-10-04 14:33:34 -07003020 /* Does this CPU need a deferred NOCB wakeup? */
Paul E. McKenney01c495f2018-01-10 12:36:00 -08003021 if (rcu_nocb_need_deferred_wakeup(rdp))
Paul E. McKenney96d3fd02013-10-04 14:33:34 -07003022 return 1;
Paul E. McKenney96d3fd02013-10-04 14:33:34 -07003023
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003024 /* nothing to do */
3025 return 0;
3026}
3027
3028/*
Paul E. McKenneyc0f4dfd2012-12-28 11:30:36 -08003029 * Return true if the specified CPU has any callback. If all_lazy is
3030 * non-NULL, store an indication of whether all callbacks are lazy.
3031 * (If there are no callbacks, all of them are deemed to be lazy.)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003032 */
Paul E. McKenney51fbb912018-05-17 13:15:40 -07003033static bool rcu_cpu_has_callbacks(bool *all_lazy)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003034{
Paul E. McKenneyc0f4dfd2012-12-28 11:30:36 -08003035 bool al = true;
3036 bool hc = false;
3037 struct rcu_data *rdp;
Paul E. McKenney6ce75a22012-06-12 11:01:13 -07003038
Paul E. McKenneyb97d23c2018-07-04 15:35:00 -07003039 rdp = this_cpu_ptr(&rcu_data);
3040 if (!rcu_segcblist_empty(&rdp->cblist)) {
Paul E. McKenney69c8d282013-09-03 09:52:20 -07003041 hc = true;
Paul E. McKenneyb97d23c2018-07-04 15:35:00 -07003042 if (rcu_segcblist_n_nonlazy_cbs(&rdp->cblist))
Paul E. McKenneyc0f4dfd2012-12-28 11:30:36 -08003043 al = false;
Paul E. McKenneyc0f4dfd2012-12-28 11:30:36 -08003044 }
3045 if (all_lazy)
3046 *all_lazy = al;
3047 return hc;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003048}
3049
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08003050/*
Paul E. McKenneydd46a782018-07-10 18:37:30 -07003051 * Helper function for rcu_barrier() tracing. If tracing is disabled,
Paul E. McKenneya83eff02012-05-23 18:47:05 -07003052 * the compiler is expected to optimize this away.
3053 */
Paul E. McKenneydd46a782018-07-10 18:37:30 -07003054static void rcu_barrier_trace(const char *s, int cpu, unsigned long done)
Paul E. McKenneya83eff02012-05-23 18:47:05 -07003055{
Paul E. McKenney8344b872018-07-03 17:22:34 -07003056 trace_rcu_barrier(rcu_state.name, s, cpu,
3057 atomic_read(&rcu_state.barrier_cpu_count), done);
Paul E. McKenneya83eff02012-05-23 18:47:05 -07003058}
3059
3060/*
Paul E. McKenneydd46a782018-07-10 18:37:30 -07003061 * RCU callback function for rcu_barrier(). If we are last, wake
3062 * up the task executing rcu_barrier().
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08003063 */
Paul E. McKenney24ebbca2012-05-29 00:34:56 -07003064static void rcu_barrier_callback(struct rcu_head *rhp)
Paul E. McKenneyd0ec7742009-10-06 21:48:16 -07003065{
Paul E. McKenneyec9f5832018-07-05 16:26:12 -07003066 if (atomic_dec_and_test(&rcu_state.barrier_cpu_count)) {
Paul E. McKenneydd46a782018-07-10 18:37:30 -07003067 rcu_barrier_trace(TPS("LastCB"), -1,
Paul E. McKenneyec9f5832018-07-05 16:26:12 -07003068 rcu_state.barrier_sequence);
3069 complete(&rcu_state.barrier_completion);
Paul E. McKenneya83eff02012-05-23 18:47:05 -07003070 } else {
Paul E. McKenneydd46a782018-07-10 18:37:30 -07003071 rcu_barrier_trace(TPS("CB"), -1, rcu_state.barrier_sequence);
Paul E. McKenneya83eff02012-05-23 18:47:05 -07003072 }
Paul E. McKenneyd0ec7742009-10-06 21:48:16 -07003073}
3074
3075/*
3076 * Called with preemption disabled, and from cross-cpu IRQ context.
3077 */
Paul E. McKenneyec9f5832018-07-05 16:26:12 -07003078static void rcu_barrier_func(void *unused)
Paul E. McKenneyd0ec7742009-10-06 21:48:16 -07003079{
Paul E. McKenneyda1df502018-07-03 15:37:16 -07003080 struct rcu_data *rdp = raw_cpu_ptr(&rcu_data);
Paul E. McKenneyd0ec7742009-10-06 21:48:16 -07003081
Paul E. McKenneydd46a782018-07-10 18:37:30 -07003082 rcu_barrier_trace(TPS("IRQ"), -1, rcu_state.barrier_sequence);
Paul E. McKenneyf92c7342017-04-10 15:40:35 -07003083 rdp->barrier_head.func = rcu_barrier_callback;
3084 debug_rcu_head_queue(&rdp->barrier_head);
3085 if (rcu_segcblist_entrain(&rdp->cblist, &rdp->barrier_head, 0)) {
Paul E. McKenneyec9f5832018-07-05 16:26:12 -07003086 atomic_inc(&rcu_state.barrier_cpu_count);
Paul E. McKenneyf92c7342017-04-10 15:40:35 -07003087 } else {
3088 debug_rcu_head_unqueue(&rdp->barrier_head);
Paul E. McKenneydd46a782018-07-10 18:37:30 -07003089 rcu_barrier_trace(TPS("IRQNQ"), -1,
Paul E. McKenneyec9f5832018-07-05 16:26:12 -07003090 rcu_state.barrier_sequence);
Paul E. McKenneyf92c7342017-04-10 15:40:35 -07003091 }
Paul E. McKenneyd0ec7742009-10-06 21:48:16 -07003092}
3093
Paul E. McKenneydd46a782018-07-10 18:37:30 -07003094/**
3095 * rcu_barrier - Wait until all in-flight call_rcu() callbacks complete.
3096 *
3097 * Note that this primitive does not necessarily wait for an RCU grace period
3098 * to complete. For example, if there are no RCU callbacks queued anywhere
3099 * in the system, then rcu_barrier() is within its rights to return
3100 * immediately, without waiting for anything, much less an RCU grace period.
Paul E. McKenneyd0ec7742009-10-06 21:48:16 -07003101 */
Paul E. McKenneydd46a782018-07-10 18:37:30 -07003102void rcu_barrier(void)
Paul E. McKenneyd0ec7742009-10-06 21:48:16 -07003103{
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08003104 int cpu;
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08003105 struct rcu_data *rdp;
Paul E. McKenneyec9f5832018-07-05 16:26:12 -07003106 unsigned long s = rcu_seq_snap(&rcu_state.barrier_sequence);
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08003107
Paul E. McKenneydd46a782018-07-10 18:37:30 -07003108 rcu_barrier_trace(TPS("Begin"), -1, s);
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08003109
Paul E. McKenneye74f4c42009-10-06 21:48:17 -07003110 /* Take mutex to serialize concurrent rcu_barrier() requests. */
Paul E. McKenneyec9f5832018-07-05 16:26:12 -07003111 mutex_lock(&rcu_state.barrier_mutex);
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08003112
Paul E. McKenney4f525a52015-06-26 11:20:00 -07003113 /* Did someone else do our work for us? */
Paul E. McKenneyec9f5832018-07-05 16:26:12 -07003114 if (rcu_seq_done(&rcu_state.barrier_sequence, s)) {
Paul E. McKenneydd46a782018-07-10 18:37:30 -07003115 rcu_barrier_trace(TPS("EarlyExit"), -1,
Paul E. McKenneyec9f5832018-07-05 16:26:12 -07003116 rcu_state.barrier_sequence);
Paul E. McKenneycf3a9c42012-05-29 14:56:46 -07003117 smp_mb(); /* caller's subsequent code after above check. */
Paul E. McKenneyec9f5832018-07-05 16:26:12 -07003118 mutex_unlock(&rcu_state.barrier_mutex);
Paul E. McKenneycf3a9c42012-05-29 14:56:46 -07003119 return;
3120 }
3121
Paul E. McKenney4f525a52015-06-26 11:20:00 -07003122 /* Mark the start of the barrier operation. */
Paul E. McKenneyec9f5832018-07-05 16:26:12 -07003123 rcu_seq_start(&rcu_state.barrier_sequence);
Paul E. McKenneydd46a782018-07-10 18:37:30 -07003124 rcu_barrier_trace(TPS("Inc1"), -1, rcu_state.barrier_sequence);
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08003125
Paul E. McKenneyd0ec7742009-10-06 21:48:16 -07003126 /*
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08003127 * Initialize the count to one rather than to zero in order to
3128 * avoid a too-soon return to zero in case of a short grace period
Paul E. McKenney1331e7a2012-08-02 17:43:50 -07003129 * (or preemption of this task). Exclude CPU-hotplug operations
3130 * to ensure that no offline CPU has callbacks queued.
Paul E. McKenneyd0ec7742009-10-06 21:48:16 -07003131 */
Paul E. McKenneyec9f5832018-07-05 16:26:12 -07003132 init_completion(&rcu_state.barrier_completion);
3133 atomic_set(&rcu_state.barrier_cpu_count, 1);
Paul E. McKenney1331e7a2012-08-02 17:43:50 -07003134 get_online_cpus();
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08003135
3136 /*
Paul E. McKenney1331e7a2012-08-02 17:43:50 -07003137 * Force each CPU with callbacks to register a new callback.
3138 * When that callback is invoked, we will know that all of the
3139 * corresponding CPU's preceding callbacks have been invoked.
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08003140 */
Paul E. McKenney3fbfbf72012-08-19 21:35:53 -07003141 for_each_possible_cpu(cpu) {
Frederic Weisbeckerd1e43fa2013-03-26 23:47:24 +01003142 if (!cpu_online(cpu) && !rcu_is_nocb_cpu(cpu))
Paul E. McKenney3fbfbf72012-08-19 21:35:53 -07003143 continue;
Paul E. McKenneyda1df502018-07-03 15:37:16 -07003144 rdp = per_cpu_ptr(&rcu_data, cpu);
Frederic Weisbeckerd1e43fa2013-03-26 23:47:24 +01003145 if (rcu_is_nocb_cpu(cpu)) {
Paul E. McKenney4580b052018-07-03 17:22:34 -07003146 if (!rcu_nocb_cpu_needs_barrier(cpu)) {
Paul E. McKenneydd46a782018-07-10 18:37:30 -07003147 rcu_barrier_trace(TPS("OfflineNoCB"), cpu,
Paul E. McKenneyec9f5832018-07-05 16:26:12 -07003148 rcu_state.barrier_sequence);
Paul E. McKenneyd7e29932014-10-27 09:15:54 -07003149 } else {
Paul E. McKenneydd46a782018-07-10 18:37:30 -07003150 rcu_barrier_trace(TPS("OnlineNoCB"), cpu,
Paul E. McKenneyec9f5832018-07-05 16:26:12 -07003151 rcu_state.barrier_sequence);
Paul E. McKenney41050a02014-12-18 12:31:27 -08003152 smp_mb__before_atomic();
Paul E. McKenneyec9f5832018-07-05 16:26:12 -07003153 atomic_inc(&rcu_state.barrier_cpu_count);
Paul E. McKenneyd7e29932014-10-27 09:15:54 -07003154 __call_rcu(&rdp->barrier_head,
Paul E. McKenney5c7d8962018-07-03 17:22:34 -07003155 rcu_barrier_callback, cpu, 0);
Paul E. McKenneyd7e29932014-10-27 09:15:54 -07003156 }
Paul E. McKenney15fecf82017-02-08 12:36:42 -08003157 } else if (rcu_segcblist_n_cbs(&rdp->cblist)) {
Paul E. McKenneydd46a782018-07-10 18:37:30 -07003158 rcu_barrier_trace(TPS("OnlineQ"), cpu,
Paul E. McKenneyec9f5832018-07-05 16:26:12 -07003159 rcu_state.barrier_sequence);
3160 smp_call_function_single(cpu, rcu_barrier_func, NULL, 1);
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08003161 } else {
Paul E. McKenneydd46a782018-07-10 18:37:30 -07003162 rcu_barrier_trace(TPS("OnlineNQ"), cpu,
Paul E. McKenneyec9f5832018-07-05 16:26:12 -07003163 rcu_state.barrier_sequence);
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08003164 }
3165 }
Paul E. McKenney1331e7a2012-08-02 17:43:50 -07003166 put_online_cpus();
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08003167
3168 /*
3169 * Now that we have an rcu_barrier_callback() callback on each
3170 * CPU, and thus each counted, remove the initial count.
3171 */
Paul E. McKenneyec9f5832018-07-05 16:26:12 -07003172 if (atomic_dec_and_test(&rcu_state.barrier_cpu_count))
3173 complete(&rcu_state.barrier_completion);
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08003174
3175 /* Wait for all rcu_barrier_callback() callbacks to be invoked. */
Paul E. McKenneyec9f5832018-07-05 16:26:12 -07003176 wait_for_completion(&rcu_state.barrier_completion);
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08003177
Paul E. McKenney4f525a52015-06-26 11:20:00 -07003178 /* Mark the end of the barrier operation. */
Paul E. McKenneydd46a782018-07-10 18:37:30 -07003179 rcu_barrier_trace(TPS("Inc2"), -1, rcu_state.barrier_sequence);
Paul E. McKenneyec9f5832018-07-05 16:26:12 -07003180 rcu_seq_end(&rcu_state.barrier_sequence);
Paul E. McKenney4f525a52015-06-26 11:20:00 -07003181
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08003182 /* Other rcu_barrier() invocations can now safely proceed. */
Paul E. McKenneyec9f5832018-07-05 16:26:12 -07003183 mutex_unlock(&rcu_state.barrier_mutex);
Paul E. McKenneyd0ec7742009-10-06 21:48:16 -07003184}
Paul E. McKenney45975c72018-07-02 14:30:37 -07003185EXPORT_SYMBOL_GPL(rcu_barrier);
Paul E. McKenneyd0ec7742009-10-06 21:48:16 -07003186
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003187/*
Paul E. McKenney0aa04b02015-01-23 21:52:37 -08003188 * Propagate ->qsinitmask bits up the rcu_node tree to account for the
3189 * first CPU in a given leaf rcu_node structure coming online. The caller
3190 * must hold the corresponding leaf rcu_node ->lock with interrrupts
3191 * disabled.
3192 */
3193static void rcu_init_new_rnp(struct rcu_node *rnp_leaf)
3194{
3195 long mask;
Paul E. McKenney8d672fa2018-05-02 14:46:43 -07003196 long oldmask;
Paul E. McKenney0aa04b02015-01-23 21:52:37 -08003197 struct rcu_node *rnp = rnp_leaf;
3198
Paul E. McKenney8d672fa2018-05-02 14:46:43 -07003199 raw_lockdep_assert_held_rcu_node(rnp_leaf);
Paul E. McKenney962aff02018-05-02 12:49:21 -07003200 WARN_ON_ONCE(rnp->wait_blkd_tasks);
Paul E. McKenney0aa04b02015-01-23 21:52:37 -08003201 for (;;) {
3202 mask = rnp->grpmask;
3203 rnp = rnp->parent;
3204 if (rnp == NULL)
3205 return;
Paul E. McKenney6cf10082015-10-08 15:36:54 -07003206 raw_spin_lock_rcu_node(rnp); /* Interrupts already disabled. */
Paul E. McKenney8d672fa2018-05-02 14:46:43 -07003207 oldmask = rnp->qsmaskinit;
Paul E. McKenney0aa04b02015-01-23 21:52:37 -08003208 rnp->qsmaskinit |= mask;
Boqun Feng67c583a72015-12-29 12:18:47 +08003209 raw_spin_unlock_rcu_node(rnp); /* Interrupts remain disabled. */
Paul E. McKenney8d672fa2018-05-02 14:46:43 -07003210 if (oldmask)
3211 return;
Paul E. McKenney0aa04b02015-01-23 21:52:37 -08003212 }
3213}
3214
3215/*
Paul E. McKenney27569622009-08-15 09:53:46 -07003216 * Do boot-time initialization of a CPU's per-CPU RCU data.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003217 */
Paul E. McKenney27569622009-08-15 09:53:46 -07003218static void __init
Paul E. McKenney53b46302018-07-03 17:22:34 -07003219rcu_boot_init_percpu_data(int cpu)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003220{
Paul E. McKenneyda1df502018-07-03 15:37:16 -07003221 struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
Paul E. McKenney27569622009-08-15 09:53:46 -07003222
3223 /* Set up local state, ensuring consistent view of global state. */
Mark Rutlandbc75e992016-06-03 15:20:04 +01003224 rdp->grpmask = leaf_node_cpu_bit(rdp->mynode, cpu);
Paul E. McKenney4c5273b2018-08-03 21:00:38 -07003225 WARN_ON_ONCE(rdp->dynticks_nesting != 1);
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -07003226 WARN_ON_ONCE(rcu_dynticks_in_eqs(rcu_dynticks_snap(rdp)));
Paul E. McKenney53b46302018-07-03 17:22:34 -07003227 rdp->rcu_ofl_gp_seq = rcu_state.gp_seq;
Paul E. McKenney57738942018-05-08 14:18:57 -07003228 rdp->rcu_ofl_gp_flags = RCU_GP_CLEANED;
Paul E. McKenney53b46302018-07-03 17:22:34 -07003229 rdp->rcu_onl_gp_seq = rcu_state.gp_seq;
Paul E. McKenney57738942018-05-08 14:18:57 -07003230 rdp->rcu_onl_gp_flags = RCU_GP_CLEANED;
Paul E. McKenney27569622009-08-15 09:53:46 -07003231 rdp->cpu = cpu;
Paul E. McKenney3fbfbf72012-08-19 21:35:53 -07003232 rcu_boot_init_nocb_percpu_data(rdp);
Paul E. McKenney27569622009-08-15 09:53:46 -07003233}
3234
3235/*
Paul E. McKenney53b46302018-07-03 17:22:34 -07003236 * Invoked early in the CPU-online process, when pretty much all services
3237 * are available. The incoming CPU is not present.
3238 *
3239 * Initializes a CPU's per-CPU RCU data. Note that only one online or
Paul E. McKenneyff3bb6f2018-05-01 14:34:08 -07003240 * offline event can be happening at a given time. Note also that we can
3241 * accept some slop in the rsp->gp_seq access due to the fact that this
3242 * CPU cannot possibly have any RCU callbacks in flight yet.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003243 */
Paul E. McKenney53b46302018-07-03 17:22:34 -07003244int rcutree_prepare_cpu(unsigned int cpu)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003245{
3246 unsigned long flags;
Paul E. McKenneyda1df502018-07-03 15:37:16 -07003247 struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
Paul E. McKenney336a4f62018-07-03 17:22:34 -07003248 struct rcu_node *rnp = rcu_get_root();
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003249
3250 /* Set up local state, ensuring consistent view of global state. */
Paul E. McKenney6cf10082015-10-08 15:36:54 -07003251 raw_spin_lock_irqsave_rcu_node(rnp, flags);
Paul E. McKenney37c72e52009-10-14 10:15:55 -07003252 rdp->qlen_last_fqs_check = 0;
Paul E. McKenney53b46302018-07-03 17:22:34 -07003253 rdp->n_force_qs_snap = rcu_state.n_force_qs;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003254 rdp->blimit = blimit;
Paul E. McKenney15fecf82017-02-08 12:36:42 -08003255 if (rcu_segcblist_empty(&rdp->cblist) && /* No early-boot CBs? */
3256 !init_nocb_callback_list(rdp))
3257 rcu_segcblist_init(&rdp->cblist); /* Re-enable callbacks. */
Paul E. McKenney4c5273b2018-08-03 21:00:38 -07003258 rdp->dynticks_nesting = 1; /* CPU not up, no tearing. */
Paul E. McKenney2625d462016-11-02 14:23:30 -07003259 rcu_dynticks_eqs_online();
Boqun Feng67c583a72015-12-29 12:18:47 +08003260 raw_spin_unlock_rcu_node(rnp); /* irqs remain disabled. */
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003261
Paul E. McKenney0aa04b02015-01-23 21:52:37 -08003262 /*
3263 * Add CPU to leaf rcu_node pending-online bitmask. Any needed
3264 * propagation up the rcu_node tree will happen at the beginning
3265 * of the next grace period.
3266 */
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003267 rnp = rdp->mynode;
Peter Zijlstra2a67e742015-10-08 12:24:23 +02003268 raw_spin_lock_rcu_node(rnp); /* irqs already disabled. */
Paul E. McKenneyb9585e92015-07-31 16:04:45 -07003269 rdp->beenonline = true; /* We have now been online. */
Paul E. McKenneyde30ad52018-04-26 11:52:09 -07003270 rdp->gp_seq = rnp->gp_seq;
Paul E. McKenney7a1d0f22018-05-01 10:26:57 -07003271 rdp->gp_seq_needed = rnp->gp_seq;
Paul E. McKenney5b74c452015-08-06 15:16:57 -07003272 rdp->cpu_no_qs.b.norm = true;
Paul E. McKenney97c668b2015-08-06 11:31:51 -07003273 rdp->core_needs_qs = false;
Paul E. McKenney9b9500d2017-08-17 17:05:59 -07003274 rdp->rcu_iw_pending = false;
Paul E. McKenney8aa670c2018-04-28 14:15:40 -07003275 rdp->rcu_iw_gp_seq = rnp->gp_seq - 1;
Paul E. McKenney53b46302018-07-03 17:22:34 -07003276 trace_rcu_grace_period(rcu_state.name, rdp->gp_seq, TPS("cpuonl"));
Boqun Feng67c583a72015-12-29 12:18:47 +08003277 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
Thomas Gleixner4df83742016-07-13 17:17:03 +00003278 rcu_prepare_kthreads(cpu);
3279 rcu_spawn_all_nocb_kthreads(cpu);
3280
3281 return 0;
3282}
3283
Paul E. McKenneydeb34f32017-03-23 13:21:30 -07003284/*
3285 * Update RCU priority boot kthread affinity for CPU-hotplug changes.
3286 */
Thomas Gleixner4df83742016-07-13 17:17:03 +00003287static void rcutree_affinity_setting(unsigned int cpu, int outgoing)
3288{
Paul E. McKenneyda1df502018-07-03 15:37:16 -07003289 struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
Thomas Gleixner4df83742016-07-13 17:17:03 +00003290
3291 rcu_boost_kthread_setaffinity(rdp->mynode, outgoing);
3292}
3293
Paul E. McKenneydeb34f32017-03-23 13:21:30 -07003294/*
3295 * Near the end of the CPU-online process. Pretty much all services
3296 * enabled, and the CPU is now very much alive.
3297 */
Thomas Gleixner4df83742016-07-13 17:17:03 +00003298int rcutree_online_cpu(unsigned int cpu)
3299{
Paul E. McKenney9b9500d2017-08-17 17:05:59 -07003300 unsigned long flags;
3301 struct rcu_data *rdp;
3302 struct rcu_node *rnp;
Paul E. McKenney9b9500d2017-08-17 17:05:59 -07003303
Paul E. McKenneyb97d23c2018-07-04 15:35:00 -07003304 rdp = per_cpu_ptr(&rcu_data, cpu);
3305 rnp = rdp->mynode;
3306 raw_spin_lock_irqsave_rcu_node(rnp, flags);
3307 rnp->ffmask |= rdp->grpmask;
3308 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
Paul E. McKenneyda915ad2017-04-05 09:01:53 -07003309 if (IS_ENABLED(CONFIG_TREE_SRCU))
3310 srcu_online_cpu(cpu);
Paul E. McKenney9b9500d2017-08-17 17:05:59 -07003311 if (rcu_scheduler_active == RCU_SCHEDULER_INACTIVE)
3312 return 0; /* Too early in boot for scheduler work. */
3313 sync_sched_exp_online_cleanup(cpu);
3314 rcutree_affinity_setting(cpu, -1);
Thomas Gleixner4df83742016-07-13 17:17:03 +00003315 return 0;
3316}
3317
Paul E. McKenneydeb34f32017-03-23 13:21:30 -07003318/*
3319 * Near the beginning of the process. The CPU is still very much alive
3320 * with pretty much all services enabled.
3321 */
Thomas Gleixner4df83742016-07-13 17:17:03 +00003322int rcutree_offline_cpu(unsigned int cpu)
3323{
Paul E. McKenney9b9500d2017-08-17 17:05:59 -07003324 unsigned long flags;
3325 struct rcu_data *rdp;
3326 struct rcu_node *rnp;
Paul E. McKenney9b9500d2017-08-17 17:05:59 -07003327
Paul E. McKenneyb97d23c2018-07-04 15:35:00 -07003328 rdp = per_cpu_ptr(&rcu_data, cpu);
3329 rnp = rdp->mynode;
3330 raw_spin_lock_irqsave_rcu_node(rnp, flags);
3331 rnp->ffmask &= ~rdp->grpmask;
3332 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
Paul E. McKenney9b9500d2017-08-17 17:05:59 -07003333
Thomas Gleixner4df83742016-07-13 17:17:03 +00003334 rcutree_affinity_setting(cpu, cpu);
Paul E. McKenneyda915ad2017-04-05 09:01:53 -07003335 if (IS_ENABLED(CONFIG_TREE_SRCU))
3336 srcu_offline_cpu(cpu);
Thomas Gleixner4df83742016-07-13 17:17:03 +00003337 return 0;
3338}
3339
Peter Zijlstraf64c6012018-05-22 09:50:53 -07003340static DEFINE_PER_CPU(int, rcu_cpu_started);
3341
Paul E. McKenney7ec99de2016-06-30 13:58:26 -07003342/*
3343 * Mark the specified CPU as being online so that subsequent grace periods
3344 * (both expedited and normal) will wait on it. Note that this means that
3345 * incoming CPUs are not allowed to use RCU read-side critical sections
3346 * until this function is called. Failing to observe this restriction
3347 * will result in lockdep splats.
Paul E. McKenneydeb34f32017-03-23 13:21:30 -07003348 *
3349 * Note that this function is special in that it is invoked directly
3350 * from the incoming CPU rather than from the cpuhp_step mechanism.
3351 * This is because this function must be invoked at a precise location.
Paul E. McKenney7ec99de2016-06-30 13:58:26 -07003352 */
3353void rcu_cpu_starting(unsigned int cpu)
3354{
3355 unsigned long flags;
3356 unsigned long mask;
Paul E. McKenney313517fc2017-06-08 16:55:40 -07003357 int nbits;
3358 unsigned long oldmask;
Paul E. McKenney7ec99de2016-06-30 13:58:26 -07003359 struct rcu_data *rdp;
3360 struct rcu_node *rnp;
Paul E. McKenney7ec99de2016-06-30 13:58:26 -07003361
Peter Zijlstraf64c6012018-05-22 09:50:53 -07003362 if (per_cpu(rcu_cpu_started, cpu))
3363 return;
3364
3365 per_cpu(rcu_cpu_started, cpu) = 1;
3366
Paul E. McKenneyb97d23c2018-07-04 15:35:00 -07003367 rdp = per_cpu_ptr(&rcu_data, cpu);
3368 rnp = rdp->mynode;
3369 mask = rdp->grpmask;
3370 raw_spin_lock_irqsave_rcu_node(rnp, flags);
3371 rnp->qsmaskinitnext |= mask;
3372 oldmask = rnp->expmaskinitnext;
3373 rnp->expmaskinitnext |= mask;
3374 oldmask ^= rnp->expmaskinitnext;
3375 nbits = bitmap_weight(&oldmask, BITS_PER_LONG);
3376 /* Allow lockless access for expedited grace periods. */
Paul E. McKenneyeb7a6652018-07-05 17:47:45 -07003377 smp_store_release(&rcu_state.ncpus, rcu_state.ncpus + nbits); /* ^^^ */
Paul E. McKenneyb97d23c2018-07-04 15:35:00 -07003378 rcu_gpnum_ovf(rnp, rdp); /* Offline-induced counter wrap? */
Paul E. McKenneyeb7a6652018-07-05 17:47:45 -07003379 rdp->rcu_onl_gp_seq = READ_ONCE(rcu_state.gp_seq);
3380 rdp->rcu_onl_gp_flags = READ_ONCE(rcu_state.gp_flags);
Paul E. McKenneyb97d23c2018-07-04 15:35:00 -07003381 if (rnp->qsmask & mask) { /* RCU waiting on incoming CPU? */
3382 /* Report QS -after- changing ->qsmaskinitnext! */
3383 rcu_report_qs_rnp(mask, rnp, rnp->gp_seq, flags);
3384 } else {
3385 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
Paul E. McKenney7ec99de2016-06-30 13:58:26 -07003386 }
Paul E. McKenney313517fc2017-06-08 16:55:40 -07003387 smp_mb(); /* Ensure RCU read-side usage follows above initialization. */
Paul E. McKenney7ec99de2016-06-30 13:58:26 -07003388}
3389
Thomas Gleixner27d50c72016-02-26 18:43:44 +00003390#ifdef CONFIG_HOTPLUG_CPU
3391/*
Paul E. McKenneydeb34f32017-03-23 13:21:30 -07003392 * The outgoing function has no further need of RCU, so remove it from
Paul E. McKenney53b46302018-07-03 17:22:34 -07003393 * the rcu_node tree's ->qsmaskinitnext bit masks.
Paul E. McKenneydeb34f32017-03-23 13:21:30 -07003394 *
3395 * Note that this function is special in that it is invoked directly
3396 * from the outgoing CPU rather than from the cpuhp_step mechanism.
3397 * This is because this function must be invoked at a precise location.
3398 */
Thomas Gleixner27d50c72016-02-26 18:43:44 +00003399void rcu_report_dead(unsigned int cpu)
3400{
Paul E. McKenney53b46302018-07-03 17:22:34 -07003401 unsigned long flags;
3402 unsigned long mask;
3403 struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
3404 struct rcu_node *rnp = rdp->mynode; /* Outgoing CPU's rdp & rnp. */
Thomas Gleixner27d50c72016-02-26 18:43:44 +00003405
Paul E. McKenney49918a52018-07-07 18:12:26 -07003406 /* QS for any half-done expedited grace period. */
Thomas Gleixner27d50c72016-02-26 18:43:44 +00003407 preempt_disable();
Paul E. McKenney63d4c8c2018-07-03 17:22:34 -07003408 rcu_report_exp_rdp(this_cpu_ptr(&rcu_data));
Thomas Gleixner27d50c72016-02-26 18:43:44 +00003409 preempt_enable();
Paul E. McKenney3e310092018-06-21 12:50:01 -07003410 rcu_preempt_deferred_qs(current);
Paul E. McKenney53b46302018-07-03 17:22:34 -07003411
3412 /* Remove outgoing CPU from mask in the leaf rcu_node structure. */
3413 mask = rdp->grpmask;
Mike Galbraith894d45b2018-08-15 09:05:29 -07003414 raw_spin_lock(&rcu_state.ofl_lock);
Paul E. McKenney53b46302018-07-03 17:22:34 -07003415 raw_spin_lock_irqsave_rcu_node(rnp, flags); /* Enforce GP memory-order guarantee. */
3416 rdp->rcu_ofl_gp_seq = READ_ONCE(rcu_state.gp_seq);
3417 rdp->rcu_ofl_gp_flags = READ_ONCE(rcu_state.gp_flags);
3418 if (rnp->qsmask & mask) { /* RCU waiting on outgoing CPU? */
3419 /* Report quiescent state -before- changing ->qsmaskinitnext! */
3420 rcu_report_qs_rnp(mask, rnp, rnp->gp_seq, flags);
3421 raw_spin_lock_irqsave_rcu_node(rnp, flags);
3422 }
3423 rnp->qsmaskinitnext &= ~mask;
3424 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
Mike Galbraith894d45b2018-08-15 09:05:29 -07003425 raw_spin_unlock(&rcu_state.ofl_lock);
Peter Zijlstraf64c6012018-05-22 09:50:53 -07003426
3427 per_cpu(rcu_cpu_started, cpu) = 0;
Thomas Gleixner27d50c72016-02-26 18:43:44 +00003428}
Paul E. McKenneya58163d2017-06-20 12:11:34 -07003429
Paul E. McKenney53b46302018-07-03 17:22:34 -07003430/*
3431 * The outgoing CPU has just passed through the dying-idle state, and we
3432 * are being invoked from the CPU that was IPIed to continue the offline
3433 * operation. Migrate the outgoing CPU's callbacks to the current CPU.
3434 */
3435void rcutree_migrate_callbacks(int cpu)
Paul E. McKenneya58163d2017-06-20 12:11:34 -07003436{
3437 unsigned long flags;
Paul E. McKenneyb1a2d792017-06-26 12:23:46 -07003438 struct rcu_data *my_rdp;
Paul E. McKenneyda1df502018-07-03 15:37:16 -07003439 struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
Paul E. McKenney336a4f62018-07-03 17:22:34 -07003440 struct rcu_node *rnp_root = rcu_get_root();
Paul E. McKenneyec4eacc2018-04-22 08:49:24 -07003441 bool needwake;
Paul E. McKenneya58163d2017-06-20 12:11:34 -07003442
Paul E. McKenney95335c02017-06-26 10:49:50 -07003443 if (rcu_is_nocb_cpu(cpu) || rcu_segcblist_empty(&rdp->cblist))
3444 return; /* No callbacks to migrate. */
3445
Paul E. McKenneyb1a2d792017-06-26 12:23:46 -07003446 local_irq_save(flags);
Paul E. McKenneyda1df502018-07-03 15:37:16 -07003447 my_rdp = this_cpu_ptr(&rcu_data);
Paul E. McKenneyb1a2d792017-06-26 12:23:46 -07003448 if (rcu_nocb_adopt_orphan_cbs(my_rdp, rdp, flags)) {
3449 local_irq_restore(flags);
3450 return;
3451 }
Paul E. McKenney9fa46fb2017-06-26 15:43:27 -07003452 raw_spin_lock_rcu_node(rnp_root); /* irqs already disabled. */
Paul E. McKenneyec4eacc2018-04-22 08:49:24 -07003453 /* Leverage recent GPs and set GP for new callbacks. */
Paul E. McKenney834f56b2018-07-03 17:22:34 -07003454 needwake = rcu_advance_cbs(rnp_root, rdp) ||
3455 rcu_advance_cbs(rnp_root, my_rdp);
Paul E. McKenneyf2dbe4a2017-06-27 07:44:06 -07003456 rcu_segcblist_merge(&my_rdp->cblist, &rdp->cblist);
Paul E. McKenney09efeee2017-07-19 10:56:46 -07003457 WARN_ON_ONCE(rcu_segcblist_empty(&my_rdp->cblist) !=
3458 !rcu_segcblist_n_cbs(&my_rdp->cblist));
Paul E. McKenney537b85c2017-06-26 17:59:02 -07003459 raw_spin_unlock_irqrestore_rcu_node(rnp_root, flags);
Paul E. McKenneyec4eacc2018-04-22 08:49:24 -07003460 if (needwake)
Paul E. McKenney532c00c2018-07-03 17:22:34 -07003461 rcu_gp_kthread_wake();
Paul E. McKenneya58163d2017-06-20 12:11:34 -07003462 WARN_ONCE(rcu_segcblist_n_cbs(&rdp->cblist) != 0 ||
3463 !rcu_segcblist_empty(&rdp->cblist),
3464 "rcu_cleanup_dead_cpu: Callbacks on offline CPU %d: qlen=%lu, 1stCB=%p\n",
3465 cpu, rcu_segcblist_n_cbs(&rdp->cblist),
3466 rcu_segcblist_first_cb(&rdp->cblist));
3467}
Thomas Gleixner27d50c72016-02-26 18:43:44 +00003468#endif
3469
Paul E. McKenneydeb34f32017-03-23 13:21:30 -07003470/*
3471 * On non-huge systems, use expedited RCU grace periods to make suspend
3472 * and hibernation run faster.
3473 */
Borislav Petkovd1d74d12013-04-22 00:12:42 +02003474static int rcu_pm_notify(struct notifier_block *self,
3475 unsigned long action, void *hcpu)
3476{
3477 switch (action) {
3478 case PM_HIBERNATION_PREPARE:
3479 case PM_SUSPEND_PREPARE:
3480 if (nr_cpu_ids <= 256) /* Expediting bad for large systems. */
Paul E. McKenney5afff482015-02-18 16:39:09 -08003481 rcu_expedite_gp();
Borislav Petkovd1d74d12013-04-22 00:12:42 +02003482 break;
3483 case PM_POST_HIBERNATION:
3484 case PM_POST_SUSPEND:
Paul E. McKenney5afff482015-02-18 16:39:09 -08003485 if (nr_cpu_ids <= 256) /* Expediting bad for large systems. */
3486 rcu_unexpedite_gp();
Borislav Petkovd1d74d12013-04-22 00:12:42 +02003487 break;
3488 default:
3489 break;
3490 }
3491 return NOTIFY_OK;
3492}
3493
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003494/*
Paul E. McKenney49918a52018-07-07 18:12:26 -07003495 * Spawn the kthreads that handle RCU's grace periods.
Paul E. McKenneyb3dbec72012-06-18 18:36:08 -07003496 */
3497static int __init rcu_spawn_gp_kthread(void)
3498{
3499 unsigned long flags;
Paul E. McKenneya94844b2014-12-12 07:37:48 -08003500 int kthread_prio_in = kthread_prio;
Paul E. McKenneyb3dbec72012-06-18 18:36:08 -07003501 struct rcu_node *rnp;
Paul E. McKenneya94844b2014-12-12 07:37:48 -08003502 struct sched_param sp;
Paul E. McKenneyb3dbec72012-06-18 18:36:08 -07003503 struct task_struct *t;
3504
Paul E. McKenneya94844b2014-12-12 07:37:48 -08003505 /* Force priority into range. */
Joel Fernandes (Google)c7cd1612018-06-19 15:14:17 -07003506 if (IS_ENABLED(CONFIG_RCU_BOOST) && kthread_prio < 2
3507 && IS_BUILTIN(CONFIG_RCU_TORTURE_TEST))
3508 kthread_prio = 2;
3509 else if (IS_ENABLED(CONFIG_RCU_BOOST) && kthread_prio < 1)
Paul E. McKenneya94844b2014-12-12 07:37:48 -08003510 kthread_prio = 1;
3511 else if (kthread_prio < 0)
3512 kthread_prio = 0;
3513 else if (kthread_prio > 99)
3514 kthread_prio = 99;
Joel Fernandes (Google)c7cd1612018-06-19 15:14:17 -07003515
Paul E. McKenneya94844b2014-12-12 07:37:48 -08003516 if (kthread_prio != kthread_prio_in)
3517 pr_alert("rcu_spawn_gp_kthread(): Limited prio to %d from %d\n",
3518 kthread_prio, kthread_prio_in);
3519
Paul E. McKenney9386c0b2014-07-13 12:00:53 -07003520 rcu_scheduler_fully_active = 1;
Paul E. McKenneyb97d23c2018-07-04 15:35:00 -07003521 t = kthread_create(rcu_gp_kthread, NULL, "%s", rcu_state.name);
Paul E. McKenney08543bd2018-10-22 08:04:03 -07003522 if (WARN_ONCE(IS_ERR(t), "%s: Could not start grace-period kthread, OOM is now expected behavior\n", __func__))
3523 return 0;
Paul E. McKenneyb97d23c2018-07-04 15:35:00 -07003524 rnp = rcu_get_root();
3525 raw_spin_lock_irqsave_rcu_node(rnp, flags);
3526 rcu_state.gp_kthread = t;
3527 if (kthread_prio) {
3528 sp.sched_priority = kthread_prio;
3529 sched_setscheduler_nocheck(t, SCHED_FIFO, &sp);
Paul E. McKenneyb3dbec72012-06-18 18:36:08 -07003530 }
Paul E. McKenneyb97d23c2018-07-04 15:35:00 -07003531 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
3532 wake_up_process(t);
Paul E. McKenney35ce7f22014-07-11 11:30:24 -07003533 rcu_spawn_nocb_kthreads();
Paul E. McKenney9386c0b2014-07-13 12:00:53 -07003534 rcu_spawn_boost_kthreads();
Paul E. McKenneyb3dbec72012-06-18 18:36:08 -07003535 return 0;
3536}
3537early_initcall(rcu_spawn_gp_kthread);
3538
3539/*
Paul E. McKenney52d7e482017-01-10 02:28:26 -08003540 * This function is invoked towards the end of the scheduler's
3541 * initialization process. Before this is called, the idle task might
3542 * contain synchronous grace-period primitives (during which time, this idle
3543 * task is booting the system, and such primitives are no-ops). After this
3544 * function is called, any synchronous grace-period primitives are run as
3545 * expedited, with the requesting task driving the grace period forward.
Paul E. McKenney900b1022017-02-10 14:32:54 -08003546 * A later core_initcall() rcu_set_runtime_mode() will switch to full
Paul E. McKenney52d7e482017-01-10 02:28:26 -08003547 * runtime RCU functionality.
Paul E. McKenneybbad9372010-04-02 16:17:17 -07003548 */
3549void rcu_scheduler_starting(void)
3550{
3551 WARN_ON(num_online_cpus() != 1);
3552 WARN_ON(nr_context_switches() > 0);
Paul E. McKenney52d7e482017-01-10 02:28:26 -08003553 rcu_test_sync_prims();
3554 rcu_scheduler_active = RCU_SCHEDULER_INIT;
3555 rcu_test_sync_prims();
Paul E. McKenneybbad9372010-04-02 16:17:17 -07003556}
3557
3558/*
Paul E. McKenney49918a52018-07-07 18:12:26 -07003559 * Helper function for rcu_init() that initializes the rcu_state structure.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003560 */
Paul E. McKenneyb8bb1f62018-07-03 17:22:34 -07003561static void __init rcu_init_one(void)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003562{
Alexander Gordeevcb007102015-06-03 08:18:30 +02003563 static const char * const buf[] = RCU_NODE_NAME_INIT;
3564 static const char * const fqs[] = RCU_FQS_NAME_INIT;
Paul E. McKenney3dc5dbe2015-09-26 14:51:24 -07003565 static struct lock_class_key rcu_node_class[RCU_NUM_LVLS];
3566 static struct lock_class_key rcu_fqs_class[RCU_NUM_LVLS];
Alexander Gordeev199977b2015-06-03 08:18:29 +02003567
Alexander Gordeev199977b2015-06-03 08:18:29 +02003568 int levelspread[RCU_NUM_LVLS]; /* kids/node in each level. */
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003569 int cpustride = 1;
3570 int i;
3571 int j;
3572 struct rcu_node *rnp;
3573
Alexander Gordeev05b84ae2015-06-03 08:18:28 +02003574 BUILD_BUG_ON(RCU_NUM_LVLS > ARRAY_SIZE(buf)); /* Fix buf[] init! */
Paul E. McKenneyb6407e82010-01-04 16:04:02 -08003575
Paul E. McKenney3eaaaf6c2015-03-09 16:51:17 -07003576 /* Silence gcc 4.8 false positive about array index out of range. */
3577 if (rcu_num_lvls <= 0 || rcu_num_lvls > RCU_NUM_LVLS)
3578 panic("rcu_init_one: rcu_num_lvls out of range");
Paul E. McKenney49305212012-11-29 13:49:00 -08003579
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003580 /* Initialize the level-tracking arrays. */
3581
Paul E. McKenneyf885b7f2012-04-23 15:52:53 -07003582 for (i = 1; i < rcu_num_lvls; i++)
Paul E. McKenneyeb7a6652018-07-05 17:47:45 -07003583 rcu_state.level[i] =
3584 rcu_state.level[i - 1] + num_rcu_lvl[i - 1];
Paul E. McKenney41f5c632017-03-15 12:59:17 -07003585 rcu_init_levelspread(levelspread, num_rcu_lvl);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003586
3587 /* Initialize the elements themselves, starting from the leaves. */
3588
Paul E. McKenneyf885b7f2012-04-23 15:52:53 -07003589 for (i = rcu_num_lvls - 1; i >= 0; i--) {
Alexander Gordeev199977b2015-06-03 08:18:29 +02003590 cpustride *= levelspread[i];
Paul E. McKenneyeb7a6652018-07-05 17:47:45 -07003591 rnp = rcu_state.level[i];
Paul E. McKenney41f5c632017-03-15 12:59:17 -07003592 for (j = 0; j < num_rcu_lvl[i]; j++, rnp++) {
Boqun Feng67c583a72015-12-29 12:18:47 +08003593 raw_spin_lock_init(&ACCESS_PRIVATE(rnp, lock));
3594 lockdep_set_class_and_name(&ACCESS_PRIVATE(rnp, lock),
Paul E. McKenneyb6407e82010-01-04 16:04:02 -08003595 &rcu_node_class[i], buf[i]);
Paul E. McKenney394f2762012-06-26 17:00:35 -07003596 raw_spin_lock_init(&rnp->fqslock);
3597 lockdep_set_class_and_name(&rnp->fqslock,
3598 &rcu_fqs_class[i], fqs[i]);
Paul E. McKenneyeb7a6652018-07-05 17:47:45 -07003599 rnp->gp_seq = rcu_state.gp_seq;
3600 rnp->gp_seq_needed = rcu_state.gp_seq;
3601 rnp->completedqs = rcu_state.gp_seq;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003602 rnp->qsmask = 0;
3603 rnp->qsmaskinit = 0;
3604 rnp->grplo = j * cpustride;
3605 rnp->grphi = (j + 1) * cpustride - 1;
Himangi Saraogi595f3902014-03-18 22:52:26 +05303606 if (rnp->grphi >= nr_cpu_ids)
3607 rnp->grphi = nr_cpu_ids - 1;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003608 if (i == 0) {
3609 rnp->grpnum = 0;
3610 rnp->grpmask = 0;
3611 rnp->parent = NULL;
3612 } else {
Alexander Gordeev199977b2015-06-03 08:18:29 +02003613 rnp->grpnum = j % levelspread[i - 1];
Paul E. McKenneydf63fa5b2018-07-31 09:49:20 -07003614 rnp->grpmask = BIT(rnp->grpnum);
Paul E. McKenneyeb7a6652018-07-05 17:47:45 -07003615 rnp->parent = rcu_state.level[i - 1] +
Alexander Gordeev199977b2015-06-03 08:18:29 +02003616 j / levelspread[i - 1];
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003617 }
3618 rnp->level = i;
Paul E. McKenney12f5f522010-11-29 21:56:39 -08003619 INIT_LIST_HEAD(&rnp->blkd_tasks);
Paul E. McKenneydae6e642013-02-10 20:48:58 -08003620 rcu_init_one_nocb(rnp);
Paul E. McKenneyf6a12f32016-01-30 17:57:35 -08003621 init_waitqueue_head(&rnp->exp_wq[0]);
3622 init_waitqueue_head(&rnp->exp_wq[1]);
Paul E. McKenney3b5f6682016-03-16 16:47:55 -07003623 init_waitqueue_head(&rnp->exp_wq[2]);
3624 init_waitqueue_head(&rnp->exp_wq[3]);
Paul E. McKenneyf6a12f32016-01-30 17:57:35 -08003625 spin_lock_init(&rnp->exp_lock);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003626 }
3627 }
Lai Jiangshan0c340292010-03-28 11:12:30 +08003628
Paul E. McKenneyeb7a6652018-07-05 17:47:45 -07003629 init_swait_queue_head(&rcu_state.gp_wq);
3630 init_swait_queue_head(&rcu_state.expedited_wq);
Paul E. McKenneyaedf4ba2018-07-04 14:33:59 -07003631 rnp = rcu_first_leaf_node();
Lai Jiangshan0c340292010-03-28 11:12:30 +08003632 for_each_possible_cpu(i) {
Paul E. McKenney4a90a062010-04-14 16:48:11 -07003633 while (i > rnp->grphi)
Lai Jiangshan0c340292010-03-28 11:12:30 +08003634 rnp++;
Paul E. McKenneyda1df502018-07-03 15:37:16 -07003635 per_cpu_ptr(&rcu_data, i)->mynode = rnp;
Paul E. McKenney53b46302018-07-03 17:22:34 -07003636 rcu_boot_init_percpu_data(i);
Lai Jiangshan0c340292010-03-28 11:12:30 +08003637 }
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003638}
3639
Paul E. McKenneyf885b7f2012-04-23 15:52:53 -07003640/*
3641 * Compute the rcu_node tree geometry from kernel parameters. This cannot
Paul E. McKenney4102ada2013-10-08 20:23:47 -07003642 * replace the definitions in tree.h because those are needed to size
Paul E. McKenneyf885b7f2012-04-23 15:52:53 -07003643 * the ->node array in the rcu_state structure.
3644 */
3645static void __init rcu_init_geometry(void)
3646{
Paul E. McKenney026ad282013-04-03 22:14:11 -07003647 ulong d;
Paul E. McKenneyf885b7f2012-04-23 15:52:53 -07003648 int i;
Alexander Gordeev05b84ae2015-06-03 08:18:28 +02003649 int rcu_capacity[RCU_NUM_LVLS];
Paul E. McKenneyf885b7f2012-04-23 15:52:53 -07003650
Paul E. McKenney026ad282013-04-03 22:14:11 -07003651 /*
3652 * Initialize any unspecified boot parameters.
3653 * The default values of jiffies_till_first_fqs and
3654 * jiffies_till_next_fqs are set to the RCU_JIFFIES_TILL_FORCE_QS
3655 * value, which is a function of HZ, then adding one for each
3656 * RCU_JIFFIES_FQS_DIV CPUs that might be on the system.
3657 */
3658 d = RCU_JIFFIES_TILL_FORCE_QS + nr_cpu_ids / RCU_JIFFIES_FQS_DIV;
3659 if (jiffies_till_first_fqs == ULONG_MAX)
3660 jiffies_till_first_fqs = d;
3661 if (jiffies_till_next_fqs == ULONG_MAX)
3662 jiffies_till_next_fqs = d;
Paul E. McKenneyc06aed02018-07-25 11:25:23 -07003663 if (jiffies_till_sched_qs == ULONG_MAX)
3664 adjust_jiffies_till_sched_qs();
Paul E. McKenney026ad282013-04-03 22:14:11 -07003665
Paul E. McKenneyf885b7f2012-04-23 15:52:53 -07003666 /* If the compile-time values are accurate, just leave. */
Paul E. McKenney47d631a2015-04-21 09:12:13 -07003667 if (rcu_fanout_leaf == RCU_FANOUT_LEAF &&
Paul E. McKenneyb17c7032012-09-06 15:38:02 -07003668 nr_cpu_ids == NR_CPUS)
Paul E. McKenneyf885b7f2012-04-23 15:52:53 -07003669 return;
Joe Perchesa7538352018-05-14 13:27:33 -07003670 pr_info("Adjusting geometry for rcu_fanout_leaf=%d, nr_cpu_ids=%u\n",
Paul E. McKenney39479092013-10-09 15:20:33 -07003671 rcu_fanout_leaf, nr_cpu_ids);
Paul E. McKenneyf885b7f2012-04-23 15:52:53 -07003672
3673 /*
Paul E. McKenneyee968ac2015-07-31 08:28:35 -07003674 * The boot-time rcu_fanout_leaf parameter must be at least two
3675 * and cannot exceed the number of bits in the rcu_node masks.
3676 * Complain and fall back to the compile-time values if this
3677 * limit is exceeded.
Paul E. McKenneyf885b7f2012-04-23 15:52:53 -07003678 */
Paul E. McKenneyee968ac2015-07-31 08:28:35 -07003679 if (rcu_fanout_leaf < 2 ||
Alexander Gordeev75cf15a2015-06-03 08:18:23 +02003680 rcu_fanout_leaf > sizeof(unsigned long) * 8) {
Paul E. McKenney13bd64942015-06-04 10:06:01 -07003681 rcu_fanout_leaf = RCU_FANOUT_LEAF;
Paul E. McKenneyf885b7f2012-04-23 15:52:53 -07003682 WARN_ON(1);
3683 return;
3684 }
3685
Alexander Gordeev75cf15a2015-06-03 08:18:23 +02003686 /*
Paul E. McKenneyf885b7f2012-04-23 15:52:53 -07003687 * Compute number of nodes that can be handled an rcu_node tree
Alexander Gordeev96181382015-06-03 08:18:26 +02003688 * with the given number of levels.
Paul E. McKenneyf885b7f2012-04-23 15:52:53 -07003689 */
Alexander Gordeev96181382015-06-03 08:18:26 +02003690 rcu_capacity[0] = rcu_fanout_leaf;
Alexander Gordeev05b84ae2015-06-03 08:18:28 +02003691 for (i = 1; i < RCU_NUM_LVLS; i++)
Paul E. McKenneyf885b7f2012-04-23 15:52:53 -07003692 rcu_capacity[i] = rcu_capacity[i - 1] * RCU_FANOUT;
3693
3694 /*
Alexander Gordeev75cf15a2015-06-03 08:18:23 +02003695 * The tree must be able to accommodate the configured number of CPUs.
Paul E. McKenneyee968ac2015-07-31 08:28:35 -07003696 * If this limit is exceeded, fall back to the compile-time values.
Paul E. McKenneyf885b7f2012-04-23 15:52:53 -07003697 */
Paul E. McKenneyee968ac2015-07-31 08:28:35 -07003698 if (nr_cpu_ids > rcu_capacity[RCU_NUM_LVLS - 1]) {
3699 rcu_fanout_leaf = RCU_FANOUT_LEAF;
3700 WARN_ON(1);
3701 return;
3702 }
Paul E. McKenneyf885b7f2012-04-23 15:52:53 -07003703
Alexander Gordeev679f9852015-06-03 08:18:25 +02003704 /* Calculate the number of levels in the tree. */
Alexander Gordeev96181382015-06-03 08:18:26 +02003705 for (i = 0; nr_cpu_ids > rcu_capacity[i]; i++) {
Alexander Gordeev679f9852015-06-03 08:18:25 +02003706 }
Alexander Gordeev96181382015-06-03 08:18:26 +02003707 rcu_num_lvls = i + 1;
Alexander Gordeev679f9852015-06-03 08:18:25 +02003708
Paul E. McKenneyf885b7f2012-04-23 15:52:53 -07003709 /* Calculate the number of rcu_nodes at each level of the tree. */
Alexander Gordeev679f9852015-06-03 08:18:25 +02003710 for (i = 0; i < rcu_num_lvls; i++) {
Alexander Gordeev96181382015-06-03 08:18:26 +02003711 int cap = rcu_capacity[(rcu_num_lvls - 1) - i];
Alexander Gordeev679f9852015-06-03 08:18:25 +02003712 num_rcu_lvl[i] = DIV_ROUND_UP(nr_cpu_ids, cap);
3713 }
Paul E. McKenneyf885b7f2012-04-23 15:52:53 -07003714
3715 /* Calculate the total number of rcu_node structures. */
3716 rcu_num_nodes = 0;
Alexander Gordeev679f9852015-06-03 08:18:25 +02003717 for (i = 0; i < rcu_num_lvls; i++)
Paul E. McKenneyf885b7f2012-04-23 15:52:53 -07003718 rcu_num_nodes += num_rcu_lvl[i];
Paul E. McKenneyf885b7f2012-04-23 15:52:53 -07003719}
3720
Paul E. McKenneya3dc2942015-04-20 11:40:50 -07003721/*
3722 * Dump out the structure of the rcu_node combining tree associated
Paul E. McKenney49918a52018-07-07 18:12:26 -07003723 * with the rcu_state structure.
Paul E. McKenneya3dc2942015-04-20 11:40:50 -07003724 */
Paul E. McKenneyb8bb1f62018-07-03 17:22:34 -07003725static void __init rcu_dump_rcu_node_tree(void)
Paul E. McKenneya3dc2942015-04-20 11:40:50 -07003726{
3727 int level = 0;
3728 struct rcu_node *rnp;
3729
3730 pr_info("rcu_node tree layout dump\n");
3731 pr_info(" ");
Paul E. McKenneyaedf4ba2018-07-04 14:33:59 -07003732 rcu_for_each_node_breadth_first(rnp) {
Paul E. McKenneya3dc2942015-04-20 11:40:50 -07003733 if (rnp->level != level) {
3734 pr_cont("\n");
3735 pr_info(" ");
3736 level = rnp->level;
3737 }
3738 pr_cont("%d:%d ^%d ", rnp->grplo, rnp->grphi, rnp->grpnum);
3739 }
3740 pr_cont("\n");
3741}
3742
Paul E. McKenneyad7c9462018-01-08 14:35:52 -08003743struct workqueue_struct *rcu_gp_wq;
Paul E. McKenney25f3d7e2018-02-01 22:05:38 -08003744struct workqueue_struct *rcu_par_gp_wq;
Paul E. McKenneyad7c9462018-01-08 14:35:52 -08003745
Paul E. McKenney9f680ab2009-11-22 08:53:49 -08003746void __init rcu_init(void)
Paul E. McKenneyf41d9112009-08-22 13:56:52 -07003747{
Paul E. McKenney017c4262010-01-14 16:10:58 -08003748 int cpu;
Paul E. McKenney9f680ab2009-11-22 08:53:49 -08003749
Paul E. McKenney47627672015-01-19 21:10:21 -08003750 rcu_early_boot_tests();
3751
Paul E. McKenneyf41d9112009-08-22 13:56:52 -07003752 rcu_bootup_announce();
Paul E. McKenneyf885b7f2012-04-23 15:52:53 -07003753 rcu_init_geometry();
Paul E. McKenneyb8bb1f62018-07-03 17:22:34 -07003754 rcu_init_one();
Paul E. McKenneya3dc2942015-04-20 11:40:50 -07003755 if (dump_tree)
Paul E. McKenneyb8bb1f62018-07-03 17:22:34 -07003756 rcu_dump_rcu_node_tree();
Jiang Fangb5b39362013-02-02 14:13:42 -08003757 open_softirq(RCU_SOFTIRQ, rcu_process_callbacks);
Paul E. McKenney9f680ab2009-11-22 08:53:49 -08003758
3759 /*
3760 * We don't need protection against CPU-hotplug here because
3761 * this is called early in boot, before either interrupts
3762 * or the scheduler are operational.
3763 */
Borislav Petkovd1d74d12013-04-22 00:12:42 +02003764 pm_notifier(rcu_pm_notify, 0);
Paul E. McKenney7ec99de2016-06-30 13:58:26 -07003765 for_each_online_cpu(cpu) {
Thomas Gleixner4df83742016-07-13 17:17:03 +00003766 rcutree_prepare_cpu(cpu);
Paul E. McKenney7ec99de2016-06-30 13:58:26 -07003767 rcu_cpu_starting(cpu);
Paul E. McKenney9b9500d2017-08-17 17:05:59 -07003768 rcutree_online_cpu(cpu);
Paul E. McKenney7ec99de2016-06-30 13:58:26 -07003769 }
Paul E. McKenneyad7c9462018-01-08 14:35:52 -08003770
3771 /* Create workqueue for expedited GPs and for Tree SRCU. */
3772 rcu_gp_wq = alloc_workqueue("rcu_gp", WQ_MEM_RECLAIM, 0);
3773 WARN_ON(!rcu_gp_wq);
Paul E. McKenney25f3d7e2018-02-01 22:05:38 -08003774 rcu_par_gp_wq = alloc_workqueue("rcu_par_gp", WQ_MEM_RECLAIM, 0);
3775 WARN_ON(!rcu_par_gp_wq);
Paul E. McKenneye0fcba92018-08-14 08:45:54 -07003776 srcu_init();
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003777}
3778
Paul E. McKenney3549c2b2016-04-15 16:35:29 -07003779#include "tree_exp.h"
Paul E. McKenney4102ada2013-10-08 20:23:47 -07003780#include "tree_plugin.h"