blob: c88240a685ec81ca6f0078641a7961563069ce60 [file] [log] [blame]
Paul E. McKenney22e40922019-01-17 10:23:39 -08001// SPDX-License-Identifier: GPL-2.0+
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002/*
SeongJae Park65bb0dc2020-01-06 21:08:02 +01003 * Read-Copy Update mechanism for mutual exclusion (tree-based version)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01004 *
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01005 * Copyright IBM Corporation, 2008
6 *
7 * Authors: Dipankar Sarma <dipankar@in.ibm.com>
8 * Manfred Spraul <manfred@colorfullife.com>
SeongJae Park65bb0dc2020-01-06 21:08:02 +01009 * Paul E. McKenney <paulmck@linux.ibm.com>
Paul E. McKenney64db4cf2008-12-18 21:55:32 +010010 *
Paul E. McKenney22e40922019-01-17 10:23:39 -080011 * Based on the original work by Paul McKenney <paulmck@linux.ibm.com>
Paul E. McKenney64db4cf2008-12-18 21:55:32 +010012 * and inputs from Rusty Russell, Andrea Arcangeli and Andi Kleen.
13 *
14 * For detailed explanation of Read-Copy Update mechanism see -
Paul E. McKenneya71fca52009-09-18 10:28:19 -070015 * Documentation/RCU
Paul E. McKenney64db4cf2008-12-18 21:55:32 +010016 */
Joe Perchesa7538352018-05-14 13:27:33 -070017
18#define pr_fmt(fmt) "rcu: " fmt
19
Paul E. McKenney64db4cf2008-12-18 21:55:32 +010020#include <linux/types.h>
21#include <linux/kernel.h>
22#include <linux/init.h>
23#include <linux/spinlock.h>
24#include <linux/smp.h>
Ingo Molnarf9411eb2017-02-06 09:50:49 +010025#include <linux/rcupdate_wait.h>
Paul E. McKenney64db4cf2008-12-18 21:55:32 +010026#include <linux/interrupt.h>
27#include <linux/sched.h>
Ingo Molnarb17b0152017-02-08 18:51:35 +010028#include <linux/sched/debug.h>
Ingo Molnarc1dc0b92009-08-02 11:28:21 +020029#include <linux/nmi.h>
Paul E. McKenney8826f3b2011-05-11 05:41:41 -070030#include <linux/atomic.h>
Paul E. McKenney64db4cf2008-12-18 21:55:32 +010031#include <linux/bitops.h>
Paul Gortmaker9984de12011-05-23 14:51:41 -040032#include <linux/export.h>
Paul E. McKenney64db4cf2008-12-18 21:55:32 +010033#include <linux/completion.h>
34#include <linux/moduleparam.h>
35#include <linux/percpu.h>
36#include <linux/notifier.h>
37#include <linux/cpu.h>
38#include <linux/mutex.h>
39#include <linux/time.h>
Paul E. McKenneybbad9372010-04-02 16:17:17 -070040#include <linux/kernel_stat.h>
Paul E. McKenneya26ac242011-01-12 14:10:23 -080041#include <linux/wait.h>
42#include <linux/kthread.h>
Ingo Molnarae7e81c2017-02-01 18:07:51 +010043#include <uapi/linux/sched/types.h>
Linus Torvalds268bb0c2011-05-20 12:50:29 -070044#include <linux/prefetch.h>
Paul E. McKenney3d3b7db2012-01-23 17:05:46 -080045#include <linux/delay.h>
Paul E. McKenney661a85d2012-07-07 05:57:03 -070046#include <linux/random.h>
Steven Rostedt (Red Hat)af658dc2015-04-29 14:36:05 -040047#include <linux/trace_events.h>
Borislav Petkovd1d74d12013-04-22 00:12:42 +020048#include <linux/suspend.h>
Paul E. McKenneya278d472017-04-05 09:05:18 -070049#include <linux/ftrace.h>
Paul E. McKenneyd3052102018-07-25 11:49:47 -070050#include <linux/tick.h>
Paul E. McKenney2ccaff12018-12-12 12:32:06 -080051#include <linux/sysrq.h>
Masami Hiramatsuc13324a2019-02-13 01:12:15 +090052#include <linux/kprobes.h>
Sebastian Andrzej Siewior48d07c02019-03-20 22:13:33 +010053#include <linux/gfp.h>
54#include <linux/oom.h>
55#include <linux/smpboot.h>
56#include <linux/jiffies.h>
Joel Fernandes (Google)77a40f92019-08-30 12:36:32 -040057#include <linux/slab.h>
Sebastian Andrzej Siewior48d07c02019-03-20 22:13:33 +010058#include <linux/sched/isolation.h>
Eric Dumazetcfcdef52019-07-24 18:07:52 -070059#include <linux/sched/clock.h>
Sebastian Andrzej Siewior48d07c02019-03-20 22:13:33 +010060#include "../time/tick-internal.h"
Paul E. McKenney64db4cf2008-12-18 21:55:32 +010061
Paul E. McKenney4102ada2013-10-08 20:23:47 -070062#include "tree.h"
Paul E. McKenney29c00b42011-06-17 15:53:19 -070063#include "rcu.h"
Paul E. McKenney9f77da92009-08-22 13:56:45 -070064
Paul E. McKenney4102ada2013-10-08 20:23:47 -070065#ifdef MODULE_PARAM_PREFIX
66#undef MODULE_PARAM_PREFIX
67#endif
68#define MODULE_PARAM_PREFIX "rcutree."
69
Paul E. McKenney35315932020-04-13 16:36:29 -070070#ifndef data_race
71#define data_race(expr) \
72 ({ \
73 expr; \
74 })
75#endif
76#ifndef ASSERT_EXCLUSIVE_WRITER
77#define ASSERT_EXCLUSIVE_WRITER(var) do { } while (0)
78#endif
79#ifndef ASSERT_EXCLUSIVE_ACCESS
80#define ASSERT_EXCLUSIVE_ACCESS(var) do { } while (0)
81#endif
82
Paul E. McKenney64db4cf2008-12-18 21:55:32 +010083/* Data structures. */
84
Steven Rostedt (Red Hat)f7f7bac2013-07-12 17:18:47 -040085/*
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -070086 * Steal a bit from the bottom of ->dynticks for idle entry/exit
87 * control. Initially this is for TLB flushing.
Steven Rostedt (Red Hat)f7f7bac2013-07-12 17:18:47 -040088 */
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -070089#define RCU_DYNTICK_CTRL_MASK 0x1
90#define RCU_DYNTICK_CTRL_CTR (RCU_DYNTICK_CTRL_MASK + 1)
91#ifndef rcu_eqs_special_exit
92#define rcu_eqs_special_exit() do { } while (0)
Ard Biesheuvela8a29b32014-07-12 19:01:49 +020093#endif
94
Paul E. McKenney4c5273b2018-08-03 21:00:38 -070095static DEFINE_PER_CPU_SHARED_ALIGNED(struct rcu_data, rcu_data) = {
96 .dynticks_nesting = 1,
97 .dynticks_nmi_nesting = DYNTICK_IRQ_NONIDLE,
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -070098 .dynticks = ATOMIC_INIT(RCU_DYNTICK_CTRL_CTR),
Paul E. McKenney4c5273b2018-08-03 21:00:38 -070099};
Paul E. McKenneyc30fe542019-10-11 21:40:09 -0700100static struct rcu_state rcu_state = {
Paul E. McKenney358be2d2018-07-03 14:15:31 -0700101 .level = { &rcu_state.node[0] },
Paul E. McKenney358be2d2018-07-03 14:15:31 -0700102 .gp_state = RCU_GP_IDLE,
103 .gp_seq = (0UL - 300UL) << RCU_SEQ_CTR_SHIFT,
104 .barrier_mutex = __MUTEX_INITIALIZER(rcu_state.barrier_mutex),
105 .name = RCU_NAME,
106 .abbr = RCU_ABBR,
107 .exp_mutex = __MUTEX_INITIALIZER(rcu_state.exp_mutex),
108 .exp_wake_mutex = __MUTEX_INITIALIZER(rcu_state.exp_wake_mutex),
Mike Galbraith894d45b2018-08-15 09:05:29 -0700109 .ofl_lock = __RAW_SPIN_LOCK_UNLOCKED(rcu_state.ofl_lock),
Paul E. McKenney358be2d2018-07-03 14:15:31 -0700110};
Paul E. McKenney27f4d282011-02-07 12:47:15 -0800111
Paul E. McKenneya3dc2942015-04-20 11:40:50 -0700112/* Dump rcu_node combining tree at boot to verify correct setup. */
113static bool dump_tree;
114module_param(dump_tree, bool, 0444);
Sebastian Andrzej Siewior48d07c02019-03-20 22:13:33 +0100115/* By default, use RCU_SOFTIRQ instead of rcuc kthreads. */
116static bool use_softirq = 1;
117module_param(use_softirq, bool, 0444);
Paul E. McKenney7fa27002015-04-20 10:27:15 -0700118/* Control rcu_node-tree auto-balancing at boot time. */
119static bool rcu_fanout_exact;
120module_param(rcu_fanout_exact, bool, 0444);
Paul E. McKenney47d631a2015-04-21 09:12:13 -0700121/* Increase (but not decrease) the RCU_FANOUT_LEAF at boot time. */
122static int rcu_fanout_leaf = RCU_FANOUT_LEAF;
Paul E. McKenney7e5c2df2012-07-01 15:42:33 -0700123module_param(rcu_fanout_leaf, int, 0444);
Paul E. McKenneyf885b7f2012-04-23 15:52:53 -0700124int rcu_num_lvls __read_mostly = RCU_NUM_LVLS;
Alexander Gordeevcb007102015-06-03 08:18:30 +0200125/* Number of rcu_nodes at specified level. */
Paul E. McKenneye95d68d2017-03-15 13:11:11 -0700126int num_rcu_lvl[] = NUM_RCU_LVL_INIT;
Paul E. McKenneyf885b7f2012-04-23 15:52:53 -0700127int rcu_num_nodes __read_mostly = NUM_RCU_NODES; /* Total # rcu_nodes in use. */
128
Paul E. McKenneyb0d30412011-07-10 15:57:35 -0700129/*
Paul E. McKenney52d7e482017-01-10 02:28:26 -0800130 * The rcu_scheduler_active variable is initialized to the value
131 * RCU_SCHEDULER_INACTIVE and transitions RCU_SCHEDULER_INIT just before the
132 * first task is spawned. So when this variable is RCU_SCHEDULER_INACTIVE,
133 * RCU can assume that there is but one task, allowing RCU to (for example)
Paul E. McKenney0d950922016-04-08 05:00:03 -0700134 * optimize synchronize_rcu() to a simple barrier(). When this variable
Paul E. McKenney52d7e482017-01-10 02:28:26 -0800135 * is RCU_SCHEDULER_INIT, RCU must actually do all the hard work required
136 * to detect real grace periods. This variable is also used to suppress
137 * boot-time false positives from lockdep-RCU error checking. Finally, it
138 * transitions from RCU_SCHEDULER_INIT to RCU_SCHEDULER_RUNNING after RCU
139 * is fully initialized, including all of its kthreads having been spawned.
Paul E. McKenneyb0d30412011-07-10 15:57:35 -0700140 */
Paul E. McKenneybbad9372010-04-02 16:17:17 -0700141int rcu_scheduler_active __read_mostly;
142EXPORT_SYMBOL_GPL(rcu_scheduler_active);
143
Paul E. McKenneyb0d30412011-07-10 15:57:35 -0700144/*
145 * The rcu_scheduler_fully_active variable transitions from zero to one
146 * during the early_initcall() processing, which is after the scheduler
147 * is capable of creating new tasks. So RCU processing (for example,
148 * creating tasks for RCU priority boosting) must be delayed until after
149 * rcu_scheduler_fully_active transitions from zero to one. We also
150 * currently delay invocation of any RCU callbacks until after this point.
151 *
152 * It might later prove better for people registering RCU callbacks during
153 * early boot to take responsibility for these callbacks, but one step at
154 * a time.
155 */
156static int rcu_scheduler_fully_active __read_mostly;
157
Paul E. McKenneyb50912d2018-07-03 17:22:34 -0700158static void rcu_report_qs_rnp(unsigned long mask, struct rcu_node *rnp,
159 unsigned long gps, unsigned long flags);
Paul E. McKenney0aa04b02015-01-23 21:52:37 -0800160static void rcu_init_new_rnp(struct rcu_node *rnp_leaf);
161static void rcu_cleanup_dead_rnp(struct rcu_node *rnp_leaf);
Thomas Gleixner5d01bbd2012-07-16 10:42:35 +0000162static void rcu_boost_kthread_setaffinity(struct rcu_node *rnp, int outgoingcpu);
Paul E. McKenneya46e0892011-06-15 15:47:09 -0700163static void invoke_rcu_core(void);
Paul E. McKenney63d4c8c2018-07-03 17:22:34 -0700164static void rcu_report_exp_rdp(struct rcu_data *rdp);
Paul E. McKenney3549c2b2016-04-15 16:35:29 -0700165static void sync_sched_exp_online_cleanup(int cpu);
Paul E. McKenneyb2b00dd2019-10-30 11:56:10 -0700166static void check_cb_ovld_locked(struct rcu_data *rdp, struct rcu_node *rnp);
Paul E. McKenneya26ac242011-01-12 14:10:23 -0800167
Paul E. McKenneya94844b2014-12-12 07:37:48 -0800168/* rcuc/rcub kthread realtime priority */
Paul E. McKenney26730f52015-04-21 09:22:14 -0700169static int kthread_prio = IS_ENABLED(CONFIG_RCU_BOOST) ? 1 : 0;
Liu Song3ffe3d12019-02-21 22:13:27 +0800170module_param(kthread_prio, int, 0444);
Paul E. McKenneya94844b2014-12-12 07:37:48 -0800171
Paul E. McKenney8d7dc922015-04-14 19:33:59 -0700172/* Delay in jiffies for grace-period initialization delays, debug only. */
Paul E. McKenney0f41c0d2015-03-10 18:33:20 -0700173
Paul E. McKenney90040c92017-05-10 14:36:55 -0700174static int gp_preinit_delay;
175module_param(gp_preinit_delay, int, 0444);
176static int gp_init_delay;
177module_param(gp_init_delay, int, 0444);
178static int gp_cleanup_delay;
179module_param(gp_cleanup_delay, int, 0444);
Paul E. McKenney0f41c0d2015-03-10 18:33:20 -0700180
Paul E. McKenney4cf439a2018-07-02 12:15:25 -0700181/* Retrieve RCU kthreads priority for rcutorture */
Joel Fernandes (Google)4babd852018-06-19 15:14:18 -0700182int rcu_get_gp_kthreads_prio(void)
183{
184 return kthread_prio;
185}
186EXPORT_SYMBOL_GPL(rcu_get_gp_kthreads_prio);
187
Paul E. McKenneyeab128e2015-04-15 12:08:22 -0700188/*
189 * Number of grace periods between delays, normalized by the duration of
Paul E. McKenneybfd090b2017-02-08 14:49:27 -0800190 * the delay. The longer the delay, the more the grace periods between
Paul E. McKenneyeab128e2015-04-15 12:08:22 -0700191 * each delay. The reason for this normalization is that it means that,
192 * for non-zero delays, the overall slowdown of grace periods is constant
193 * regardless of the duration of the delay. This arrangement balances
194 * the need for long delays to increase some race probabilities with the
195 * need for fast grace periods to increase other race probabilities.
196 */
197#define PER_RCU_NODE_PERIOD 3 /* Number of grace periods between delays. */
Paul E. McKenney37745d22015-01-22 18:24:08 -0800198
Paul E. McKenneya26ac242011-01-12 14:10:23 -0800199/*
Paul E. McKenney0aa04b02015-01-23 21:52:37 -0800200 * Compute the mask of online CPUs for the specified rcu_node structure.
201 * This will not be stable unless the rcu_node structure's ->lock is
202 * held, but the bit corresponding to the current CPU will be stable
203 * in most contexts.
204 */
Paul E. McKenneyc30fe542019-10-11 21:40:09 -0700205static unsigned long rcu_rnp_online_cpus(struct rcu_node *rnp)
Paul E. McKenney0aa04b02015-01-23 21:52:37 -0800206{
Paul E. McKenney7d0ae802015-03-03 14:57:58 -0800207 return READ_ONCE(rnp->qsmaskinitnext);
Paul E. McKenney0aa04b02015-01-23 21:52:37 -0800208}
209
210/*
Paul E. McKenney7d0ae802015-03-03 14:57:58 -0800211 * Return true if an RCU grace period is in progress. The READ_ONCE()s
Paul E. McKenneyfc2219d42009-09-23 09:50:41 -0700212 * permit this function to be invoked without holding the root rcu_node
213 * structure's ->lock, but of course results can be subject to change.
214 */
Paul E. McKenneyde8e8732018-07-03 17:22:34 -0700215static int rcu_gp_in_progress(void)
Paul E. McKenneyfc2219d42009-09-23 09:50:41 -0700216{
Paul E. McKenneyde8e8732018-07-03 17:22:34 -0700217 return rcu_seq_state(rcu_seq_current(&rcu_state.gp_seq));
Paul E. McKenneyfc2219d42009-09-23 09:50:41 -0700218}
219
Paul E. McKenney903ee832018-10-02 16:05:46 -0700220/*
221 * Return the number of callbacks queued on the specified CPU.
222 * Handles both the nocbs and normal cases.
223 */
224static long rcu_get_n_cbs_cpu(int cpu)
225{
226 struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
227
Paul E. McKenneyc0352802019-05-21 08:28:41 -0700228 if (rcu_segcblist_is_enabled(&rdp->cblist))
Paul E. McKenney903ee832018-10-02 16:05:46 -0700229 return rcu_segcblist_n_cbs(&rdp->cblist);
Paul E. McKenneyc0352802019-05-21 08:28:41 -0700230 return 0;
Paul E. McKenney903ee832018-10-02 16:05:46 -0700231}
232
Paul E. McKenneyd28139c2018-06-28 14:45:25 -0700233void rcu_softirq_qs(void)
Ingo Molnarb1f77b02009-03-13 03:20:49 +0100234{
Paul E. McKenney45975c72018-07-02 14:30:37 -0700235 rcu_qs();
Paul E. McKenneyd28139c2018-06-28 14:45:25 -0700236 rcu_preempt_deferred_qs(current);
Ingo Molnarb1f77b02009-03-13 03:20:49 +0100237}
238
Paul E. McKenney6563de92016-11-02 13:33:57 -0700239/*
Paul E. McKenney2625d462016-11-02 14:23:30 -0700240 * Record entry into an extended quiescent state. This is only to be
241 * called when not already in an extended quiescent state.
242 */
243static void rcu_dynticks_eqs_enter(void)
244{
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -0700245 struct rcu_data *rdp = this_cpu_ptr(&rcu_data);
Paul E. McKenneyb8c17e62016-11-08 14:25:21 -0800246 int seq;
Paul E. McKenney2625d462016-11-02 14:23:30 -0700247
248 /*
Paul E. McKenneyb8c17e62016-11-08 14:25:21 -0800249 * CPUs seeing atomic_add_return() must see prior RCU read-side
Paul E. McKenney2625d462016-11-02 14:23:30 -0700250 * critical sections, and we also must force ordering with the
251 * next idle sojourn.
252 */
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -0700253 seq = atomic_add_return(RCU_DYNTICK_CTRL_CTR, &rdp->dynticks);
Paul E. McKenneyb8c17e62016-11-08 14:25:21 -0800254 /* Better be in an extended quiescent state! */
255 WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) &&
256 (seq & RCU_DYNTICK_CTRL_CTR));
257 /* Better not have special action (TLB flush) pending! */
258 WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) &&
259 (seq & RCU_DYNTICK_CTRL_MASK));
Paul E. McKenney2625d462016-11-02 14:23:30 -0700260}
261
262/*
263 * Record exit from an extended quiescent state. This is only to be
264 * called from an extended quiescent state.
265 */
266static void rcu_dynticks_eqs_exit(void)
267{
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -0700268 struct rcu_data *rdp = this_cpu_ptr(&rcu_data);
Paul E. McKenneyb8c17e62016-11-08 14:25:21 -0800269 int seq;
Paul E. McKenney2625d462016-11-02 14:23:30 -0700270
271 /*
Paul E. McKenneyb8c17e62016-11-08 14:25:21 -0800272 * CPUs seeing atomic_add_return() must see prior idle sojourns,
Paul E. McKenney2625d462016-11-02 14:23:30 -0700273 * and we also must force ordering with the next RCU read-side
274 * critical section.
275 */
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -0700276 seq = atomic_add_return(RCU_DYNTICK_CTRL_CTR, &rdp->dynticks);
Paul E. McKenneyb8c17e62016-11-08 14:25:21 -0800277 WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) &&
278 !(seq & RCU_DYNTICK_CTRL_CTR));
279 if (seq & RCU_DYNTICK_CTRL_MASK) {
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -0700280 atomic_andnot(RCU_DYNTICK_CTRL_MASK, &rdp->dynticks);
Paul E. McKenneyb8c17e62016-11-08 14:25:21 -0800281 smp_mb__after_atomic(); /* _exit after clearing mask. */
282 /* Prefer duplicate flushes to losing a flush. */
283 rcu_eqs_special_exit();
284 }
Paul E. McKenney2625d462016-11-02 14:23:30 -0700285}
286
287/*
288 * Reset the current CPU's ->dynticks counter to indicate that the
289 * newly onlined CPU is no longer in an extended quiescent state.
290 * This will either leave the counter unchanged, or increment it
291 * to the next non-quiescent value.
292 *
293 * The non-atomic test/increment sequence works because the upper bits
294 * of the ->dynticks counter are manipulated only by the corresponding CPU,
295 * or when the corresponding CPU is offline.
296 */
297static void rcu_dynticks_eqs_online(void)
298{
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -0700299 struct rcu_data *rdp = this_cpu_ptr(&rcu_data);
Paul E. McKenney2625d462016-11-02 14:23:30 -0700300
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -0700301 if (atomic_read(&rdp->dynticks) & RCU_DYNTICK_CTRL_CTR)
Paul E. McKenney2625d462016-11-02 14:23:30 -0700302 return;
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -0700303 atomic_add(RCU_DYNTICK_CTRL_CTR, &rdp->dynticks);
Paul E. McKenney2625d462016-11-02 14:23:30 -0700304}
305
306/*
Paul E. McKenney02a5c5502016-11-02 17:25:06 -0700307 * Is the current CPU in an extended quiescent state?
308 *
309 * No ordering, as we are sampling CPU-local information.
310 */
Paul E. McKenneyc30fe542019-10-11 21:40:09 -0700311static bool rcu_dynticks_curr_cpu_in_eqs(void)
Paul E. McKenney02a5c5502016-11-02 17:25:06 -0700312{
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -0700313 struct rcu_data *rdp = this_cpu_ptr(&rcu_data);
Paul E. McKenney02a5c5502016-11-02 17:25:06 -0700314
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -0700315 return !(atomic_read(&rdp->dynticks) & RCU_DYNTICK_CTRL_CTR);
Paul E. McKenney02a5c5502016-11-02 17:25:06 -0700316}
317
318/*
Paul E. McKenney8b2f63a2016-11-02 14:12:05 -0700319 * Snapshot the ->dynticks counter with full ordering so as to allow
320 * stable comparison of this counter with past and future snapshots.
321 */
Paul E. McKenneyc30fe542019-10-11 21:40:09 -0700322static int rcu_dynticks_snap(struct rcu_data *rdp)
Paul E. McKenney8b2f63a2016-11-02 14:12:05 -0700323{
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -0700324 int snap = atomic_add_return(0, &rdp->dynticks);
Paul E. McKenney8b2f63a2016-11-02 14:12:05 -0700325
Paul E. McKenneyb8c17e62016-11-08 14:25:21 -0800326 return snap & ~RCU_DYNTICK_CTRL_MASK;
Paul E. McKenney8b2f63a2016-11-02 14:12:05 -0700327}
328
329/*
Paul E. McKenney02a5c5502016-11-02 17:25:06 -0700330 * Return true if the snapshot returned from rcu_dynticks_snap()
331 * indicates that RCU is in an extended quiescent state.
332 */
333static bool rcu_dynticks_in_eqs(int snap)
334{
Paul E. McKenneyb8c17e62016-11-08 14:25:21 -0800335 return !(snap & RCU_DYNTICK_CTRL_CTR);
Paul E. McKenney02a5c5502016-11-02 17:25:06 -0700336}
337
338/*
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -0700339 * Return true if the CPU corresponding to the specified rcu_data
Paul E. McKenney02a5c5502016-11-02 17:25:06 -0700340 * structure has spent some time in an extended quiescent state since
341 * rcu_dynticks_snap() returned the specified snapshot.
342 */
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -0700343static bool rcu_dynticks_in_eqs_since(struct rcu_data *rdp, int snap)
Paul E. McKenney02a5c5502016-11-02 17:25:06 -0700344{
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -0700345 return snap != rcu_dynticks_snap(rdp);
Paul E. McKenney02a5c5502016-11-02 17:25:06 -0700346}
347
348/*
Paul E. McKenneyb8c17e62016-11-08 14:25:21 -0800349 * Set the special (bottom) bit of the specified CPU so that it
350 * will take special action (such as flushing its TLB) on the
351 * next exit from an extended quiescent state. Returns true if
352 * the bit was successfully set, or false if the CPU was not in
353 * an extended quiescent state.
354 */
355bool rcu_eqs_special_set(int cpu)
356{
357 int old;
358 int new;
Paul E. McKenneyfaa059c2020-02-03 14:20:00 -0800359 int new_old;
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -0700360 struct rcu_data *rdp = &per_cpu(rcu_data, cpu);
Paul E. McKenneyb8c17e62016-11-08 14:25:21 -0800361
Paul E. McKenneyfaa059c2020-02-03 14:20:00 -0800362 new_old = atomic_read(&rdp->dynticks);
Paul E. McKenneyb8c17e62016-11-08 14:25:21 -0800363 do {
Paul E. McKenneyfaa059c2020-02-03 14:20:00 -0800364 old = new_old;
Paul E. McKenneyb8c17e62016-11-08 14:25:21 -0800365 if (old & RCU_DYNTICK_CTRL_CTR)
366 return false;
367 new = old | RCU_DYNTICK_CTRL_MASK;
Paul E. McKenneyfaa059c2020-02-03 14:20:00 -0800368 new_old = atomic_cmpxchg(&rdp->dynticks, old, new);
369 } while (new_old != old);
Paul E. McKenneyb8c17e62016-11-08 14:25:21 -0800370 return true;
Paul E. McKenney6563de92016-11-02 13:33:57 -0700371}
Paul E. McKenney5cd37192014-12-13 20:32:04 -0800372
Paul E. McKenney4a81e832014-06-20 16:49:01 -0700373/*
374 * Let the RCU core know that this CPU has gone through the scheduler,
375 * which is a quiescent state. This is called when the need for a
376 * quiescent state is urgent, so we burn an atomic operation and full
377 * memory barriers to let the RCU core know about it, regardless of what
378 * this CPU might (or might not) do in the near future.
379 *
Paul E. McKenney0f9be8c2017-01-27 13:17:02 -0800380 * We inform the RCU core by emulating a zero-duration dyntick-idle period.
Paul E. McKenney46a5d162015-10-07 09:10:48 -0700381 *
Paul E. McKenney3b57a392018-05-16 16:01:56 -0700382 * The caller must have disabled interrupts and must not be idle.
Paul E. McKenney4a81e832014-06-20 16:49:01 -0700383 */
Paul E. McKenney366237e2019-07-10 08:01:01 -0700384void rcu_momentary_dyntick_idle(void)
Paul E. McKenney4a81e832014-06-20 16:49:01 -0700385{
Paul E. McKenney3b57a392018-05-16 16:01:56 -0700386 int special;
387
Paul E. McKenney2dba13f2018-08-03 21:00:38 -0700388 raw_cpu_write(rcu_data.rcu_need_heavy_qs, false);
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -0700389 special = atomic_add_return(2 * RCU_DYNTICK_CTRL_CTR,
390 &this_cpu_ptr(&rcu_data)->dynticks);
Paul E. McKenney3b57a392018-05-16 16:01:56 -0700391 /* It is illegal to call this from idle state. */
392 WARN_ON_ONCE(!(special & RCU_DYNTICK_CTRL_CTR));
Paul E. McKenney3e310092018-06-21 12:50:01 -0700393 rcu_preempt_deferred_qs(current);
Paul E. McKenney4a81e832014-06-20 16:49:01 -0700394}
Paul E. McKenney79ba7ff2019-08-04 13:17:35 -0700395EXPORT_SYMBOL_GPL(rcu_momentary_dyntick_idle);
Paul E. McKenney4a81e832014-06-20 16:49:01 -0700396
Paul E. McKenney45975c72018-07-02 14:30:37 -0700397/**
Joel Fernandes (Google)eddded82019-03-26 15:24:09 -0400398 * rcu_is_cpu_rrupt_from_idle - see if interrupted from idle
Boqun Fengbb73c522015-07-30 16:55:38 -0700399 *
Joel Fernandes (Google)eddded82019-03-26 15:24:09 -0400400 * If the current CPU is idle and running at a first-level (not nested)
Paul E. McKenney45975c72018-07-02 14:30:37 -0700401 * interrupt from idle, return true. The caller must have at least
402 * disabled preemption.
Paul E. McKenney5cd37192014-12-13 20:32:04 -0800403 */
Paul E. McKenney45975c72018-07-02 14:30:37 -0700404static int rcu_is_cpu_rrupt_from_idle(void)
Paul E. McKenney5cd37192014-12-13 20:32:04 -0800405{
Joel Fernandes (Google)eddded82019-03-26 15:24:09 -0400406 /* Called only from within the scheduling-clock interrupt */
407 lockdep_assert_in_irq();
408
409 /* Check for counter underflows */
410 RCU_LOCKDEP_WARN(__this_cpu_read(rcu_data.dynticks_nesting) < 0,
411 "RCU dynticks_nesting counter underflow!");
412 RCU_LOCKDEP_WARN(__this_cpu_read(rcu_data.dynticks_nmi_nesting) <= 0,
413 "RCU dynticks_nmi_nesting counter underflow/zero!");
414
415 /* Are we at first interrupt nesting level? */
416 if (__this_cpu_read(rcu_data.dynticks_nmi_nesting) != 1)
417 return false;
418
419 /* Does CPU appear to be idle from an RCU standpoint? */
420 return __this_cpu_read(rcu_data.dynticks_nesting) == 0;
Paul E. McKenney5cd37192014-12-13 20:32:04 -0800421}
Paul E. McKenney5cd37192014-12-13 20:32:04 -0800422
Paul E. McKenneyd5a9a8c2019-04-10 17:01:39 -0700423#define DEFAULT_RCU_BLIMIT 10 /* Maximum callbacks per rcu_do_batch ... */
424#define DEFAULT_MAX_RCU_BLIMIT 10000 /* ... even during callback flood. */
Paul E. McKenney17c77982017-04-28 11:12:34 -0700425static long blimit = DEFAULT_RCU_BLIMIT;
426#define DEFAULT_RCU_QHIMARK 10000 /* If this many pending, ignore blimit. */
427static long qhimark = DEFAULT_RCU_QHIMARK;
428#define DEFAULT_RCU_QLOMARK 100 /* Once only this many pending, use blimit. */
429static long qlowmark = DEFAULT_RCU_QLOMARK;
Paul E. McKenneyb2b00dd2019-10-30 11:56:10 -0700430#define DEFAULT_RCU_QOVLD_MULT 2
431#define DEFAULT_RCU_QOVLD (DEFAULT_RCU_QOVLD_MULT * DEFAULT_RCU_QHIMARK)
432static long qovld = DEFAULT_RCU_QOVLD; /* If this many pending, hammer QS. */
433static long qovld_calc = -1; /* No pre-initialization lock acquisitions! */
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100434
Eric Dumazet878d7432012-10-18 04:55:36 -0700435module_param(blimit, long, 0444);
436module_param(qhimark, long, 0444);
437module_param(qlowmark, long, 0444);
Paul E. McKenneyb2b00dd2019-10-30 11:56:10 -0700438module_param(qovld, long, 0444);
Paul E. McKenney3d76c082009-09-28 07:46:32 -0700439
Paul E. McKenney026ad282013-04-03 22:14:11 -0700440static ulong jiffies_till_first_fqs = ULONG_MAX;
441static ulong jiffies_till_next_fqs = ULONG_MAX;
Paul E. McKenney8c7c4822016-01-03 20:29:57 -0800442static bool rcu_kick_kthreads;
Eric Dumazetcfcdef52019-07-24 18:07:52 -0700443static int rcu_divisor = 7;
444module_param(rcu_divisor, int, 0644);
445
446/* Force an exit from rcu_do_batch() after 3 milliseconds. */
447static long rcu_resched_ns = 3 * NSEC_PER_MSEC;
448module_param(rcu_resched_ns, long, 0644);
Paul E. McKenneyd40011f2012-06-26 20:45:57 -0700449
Paul E. McKenneyc06aed02018-07-25 11:25:23 -0700450/*
451 * How long the grace period must be before we start recruiting
452 * quiescent-state help from rcu_note_context_switch().
453 */
454static ulong jiffies_till_sched_qs = ULONG_MAX;
455module_param(jiffies_till_sched_qs, ulong, 0444);
Paul E. McKenney85f2b602019-03-11 15:45:13 -0700456static ulong jiffies_to_sched_qs; /* See adjust_jiffies_till_sched_qs(). */
Paul E. McKenneyc06aed02018-07-25 11:25:23 -0700457module_param(jiffies_to_sched_qs, ulong, 0444); /* Display only! */
458
459/*
460 * Make sure that we give the grace-period kthread time to detect any
461 * idle CPUs before taking active measures to force quiescent states.
462 * However, don't go below 100 milliseconds, adjusted upwards for really
463 * large systems.
464 */
465static void adjust_jiffies_till_sched_qs(void)
466{
467 unsigned long j;
468
469 /* If jiffies_till_sched_qs was specified, respect the request. */
470 if (jiffies_till_sched_qs != ULONG_MAX) {
471 WRITE_ONCE(jiffies_to_sched_qs, jiffies_till_sched_qs);
472 return;
473 }
Paul E. McKenney85f2b602019-03-11 15:45:13 -0700474 /* Otherwise, set to third fqs scan, but bound below on large system. */
Paul E. McKenneyc06aed02018-07-25 11:25:23 -0700475 j = READ_ONCE(jiffies_till_first_fqs) +
476 2 * READ_ONCE(jiffies_till_next_fqs);
477 if (j < HZ / 10 + nr_cpu_ids / RCU_JIFFIES_FQS_DIV)
478 j = HZ / 10 + nr_cpu_ids / RCU_JIFFIES_FQS_DIV;
479 pr_info("RCU calculated value of scheduler-enlistment delay is %ld jiffies.\n", j);
480 WRITE_ONCE(jiffies_to_sched_qs, j);
481}
482
Byungchul Park67abb962018-06-01 11:03:09 +0900483static int param_set_first_fqs_jiffies(const char *val, const struct kernel_param *kp)
484{
485 ulong j;
486 int ret = kstrtoul(val, 0, &j);
487
Paul E. McKenneyc06aed02018-07-25 11:25:23 -0700488 if (!ret) {
Byungchul Park67abb962018-06-01 11:03:09 +0900489 WRITE_ONCE(*(ulong *)kp->arg, (j > HZ) ? HZ : j);
Paul E. McKenneyc06aed02018-07-25 11:25:23 -0700490 adjust_jiffies_till_sched_qs();
491 }
Byungchul Park67abb962018-06-01 11:03:09 +0900492 return ret;
493}
494
495static int param_set_next_fqs_jiffies(const char *val, const struct kernel_param *kp)
496{
497 ulong j;
498 int ret = kstrtoul(val, 0, &j);
499
Paul E. McKenneyc06aed02018-07-25 11:25:23 -0700500 if (!ret) {
Byungchul Park67abb962018-06-01 11:03:09 +0900501 WRITE_ONCE(*(ulong *)kp->arg, (j > HZ) ? HZ : (j ?: 1));
Paul E. McKenneyc06aed02018-07-25 11:25:23 -0700502 adjust_jiffies_till_sched_qs();
503 }
Byungchul Park67abb962018-06-01 11:03:09 +0900504 return ret;
505}
506
507static struct kernel_param_ops first_fqs_jiffies_ops = {
508 .set = param_set_first_fqs_jiffies,
509 .get = param_get_ulong,
510};
511
512static struct kernel_param_ops next_fqs_jiffies_ops = {
513 .set = param_set_next_fqs_jiffies,
514 .get = param_get_ulong,
515};
516
517module_param_cb(jiffies_till_first_fqs, &first_fqs_jiffies_ops, &jiffies_till_first_fqs, 0644);
518module_param_cb(jiffies_till_next_fqs, &next_fqs_jiffies_ops, &jiffies_till_next_fqs, 0644);
Paul E. McKenney8c7c4822016-01-03 20:29:57 -0800519module_param(rcu_kick_kthreads, bool, 0644);
Paul E. McKenneyd40011f2012-06-26 20:45:57 -0700520
Paul E. McKenney8ff0b902018-07-05 17:55:14 -0700521static void force_qs_rnp(int (*f)(struct rcu_data *rdp));
Paul E. McKenneydd7dafd2019-09-14 03:39:22 -0700522static int rcu_pending(int user);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100523
524/*
Paul E. McKenney17ef2fe2018-04-27 11:39:34 -0700525 * Return the number of RCU GPs completed thus far for debug & stats.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100526 */
Paul E. McKenney17ef2fe2018-04-27 11:39:34 -0700527unsigned long rcu_get_gp_seq(void)
Paul E. McKenney917963d2014-11-21 17:10:16 -0800528{
Paul E. McKenney16fc9c62018-07-03 15:54:39 -0700529 return READ_ONCE(rcu_state.gp_seq);
Paul E. McKenney917963d2014-11-21 17:10:16 -0800530}
Paul E. McKenney17ef2fe2018-04-27 11:39:34 -0700531EXPORT_SYMBOL_GPL(rcu_get_gp_seq);
Paul E. McKenney917963d2014-11-21 17:10:16 -0800532
533/*
Paul E. McKenney291783b2016-01-12 13:43:30 -0800534 * Return the number of RCU expedited batches completed thus far for
535 * debug & stats. Odd numbers mean that a batch is in progress, even
536 * numbers mean idle. The value returned will thus be roughly double
537 * the cumulative batches since boot.
538 */
539unsigned long rcu_exp_batches_completed(void)
540{
Paul E. McKenney16fc9c62018-07-03 15:54:39 -0700541 return rcu_state.expedited_sequence;
Paul E. McKenney291783b2016-01-12 13:43:30 -0800542}
543EXPORT_SYMBOL_GPL(rcu_exp_batches_completed);
544
545/*
Paul E. McKenneyfd897572018-12-10 16:09:49 -0800546 * Return the root node of the rcu_state structure.
547 */
548static struct rcu_node *rcu_get_root(void)
549{
550 return &rcu_state.node[0];
551}
552
553/*
Paul E. McKenneyad0dc7f2014-02-19 10:51:42 -0800554 * Send along grace-period-related data for rcutorture diagnostics.
555 */
556void rcutorture_get_gp_data(enum rcutorture_type test_type, int *flags,
Paul E. McKenneyaebc8262018-05-01 06:42:51 -0700557 unsigned long *gp_seq)
Paul E. McKenneyad0dc7f2014-02-19 10:51:42 -0800558{
Paul E. McKenneyad0dc7f2014-02-19 10:51:42 -0800559 switch (test_type) {
560 case RCU_FLAVOR:
Paul E. McKenneyf7dd7d42018-07-04 15:39:40 -0700561 *flags = READ_ONCE(rcu_state.gp_flags);
562 *gp_seq = rcu_seq_current(&rcu_state.gp_seq);
Paul E. McKenneyad0dc7f2014-02-19 10:51:42 -0800563 break;
564 default:
565 break;
566 }
Paul E. McKenneyad0dc7f2014-02-19 10:51:42 -0800567}
568EXPORT_SYMBOL_GPL(rcutorture_get_gp_data);
569
570/*
Paul E. McKenney215bba92017-10-05 16:37:03 -0700571 * Enter an RCU extended quiescent state, which can be either the
572 * idle loop or adaptive-tickless usermode execution.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100573 *
Paul E. McKenney215bba92017-10-05 16:37:03 -0700574 * We crowbar the ->dynticks_nmi_nesting field to zero to allow for
575 * the possibility of usermode upcalls having messed up our count
576 * of interrupt nesting level during the prior busy period.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100577 */
Paul E. McKenney215bba92017-10-05 16:37:03 -0700578static void rcu_eqs_enter(bool user)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100579{
Paul E. McKenney4c5273b2018-08-03 21:00:38 -0700580 struct rcu_data *rdp = this_cpu_ptr(&rcu_data);
Paul E. McKenney215bba92017-10-05 16:37:03 -0700581
Paul E. McKenney4c5273b2018-08-03 21:00:38 -0700582 WARN_ON_ONCE(rdp->dynticks_nmi_nesting != DYNTICK_IRQ_NONIDLE);
583 WRITE_ONCE(rdp->dynticks_nmi_nesting, 0);
Paul E. McKenney215bba92017-10-05 16:37:03 -0700584 WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) &&
Paul E. McKenney4c5273b2018-08-03 21:00:38 -0700585 rdp->dynticks_nesting == 0);
586 if (rdp->dynticks_nesting != 1) {
587 rdp->dynticks_nesting--;
Paul E. McKenney215bba92017-10-05 16:37:03 -0700588 return;
589 }
Paul E. McKenney96d3fd02013-10-04 14:33:34 -0700590
Frederic Weisbeckerb04db8e2017-11-06 16:01:30 +0100591 lockdep_assert_irqs_disabled();
Marco Elver6cf539a2019-10-09 17:57:43 +0200592 trace_rcu_dyntick(TPS("Start"), rdp->dynticks_nesting, 0, atomic_read(&rdp->dynticks));
Paul E. McKenneye68bbb22017-10-05 19:55:31 -0700593 WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) && !user && !is_idle_task(current));
Paul E. McKenneyb97d23c2018-07-04 15:35:00 -0700594 rdp = this_cpu_ptr(&rcu_data);
595 do_nocb_deferred_wakeup(rdp);
Paul E. McKenney198bbf82014-10-22 15:03:43 -0700596 rcu_prepare_for_idle();
Paul E. McKenney3e310092018-06-21 12:50:01 -0700597 rcu_preempt_deferred_qs(current);
Paul E. McKenney4c5273b2018-08-03 21:00:38 -0700598 WRITE_ONCE(rdp->dynticks_nesting, 0); /* Avoid irq-access tearing. */
Paul E. McKenney844ccdd2017-10-03 16:51:47 -0700599 rcu_dynticks_eqs_enter();
Paul E. McKenney176f8f72014-08-04 17:43:50 -0700600 rcu_dynticks_task_enter();
Frederic Weisbeckeradf50912012-06-28 11:20:21 -0700601}
602
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700603/**
604 * rcu_idle_enter - inform RCU that current CPU is entering idle
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100605 *
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700606 * Enter idle mode, in other words, -leave- the mode in which RCU
607 * read-side critical sections can occur. (Though RCU read-side
608 * critical sections can occur in irq handlers in idle, a possibility
609 * handled by irq_enter() and irq_exit().)
610 *
Paul E. McKenneyc0da3132017-09-22 09:58:47 -0700611 * If you add or remove a call to rcu_idle_enter(), be sure to test with
612 * CONFIG_RCU_EQS_DEBUG=y.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100613 */
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700614void rcu_idle_enter(void)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100615{
Frederic Weisbeckerb04db8e2017-11-06 16:01:30 +0100616 lockdep_assert_irqs_disabled();
Paul E. McKenneycb349ca2012-09-04 17:35:31 -0700617 rcu_eqs_enter(false);
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700618}
619
Paul E. McKenneyd1ec4c32015-05-13 10:41:58 -0700620#ifdef CONFIG_NO_HZ_FULL
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700621/**
Frederic Weisbeckeradf50912012-06-28 11:20:21 -0700622 * rcu_user_enter - inform RCU that we are resuming userspace.
623 *
624 * Enter RCU idle mode right before resuming userspace. No use of RCU
625 * is permitted between this call and rcu_user_exit(). This way the
626 * CPU doesn't need to maintain the tick for RCU maintenance purposes
627 * when the CPU runs in userspace.
Paul E. McKenneyc0da3132017-09-22 09:58:47 -0700628 *
629 * If you add or remove a call to rcu_user_enter(), be sure to test with
630 * CONFIG_RCU_EQS_DEBUG=y.
Frederic Weisbeckeradf50912012-06-28 11:20:21 -0700631 */
632void rcu_user_enter(void)
633{
Frederic Weisbeckerb04db8e2017-11-06 16:01:30 +0100634 lockdep_assert_irqs_disabled();
Paul E. McKenneyd4db30a2017-07-12 09:03:35 -0700635 rcu_eqs_enter(true);
Frederic Weisbeckeradf50912012-06-28 11:20:21 -0700636}
Paul E. McKenneyd1ec4c32015-05-13 10:41:58 -0700637#endif /* CONFIG_NO_HZ_FULL */
Frederic Weisbecker19dd15912012-06-04 16:42:35 -0700638
Byungchul Parkcf7614e2018-06-22 15:12:06 +0900639/*
Paul E. McKenneyfd581a92017-10-02 21:56:20 -0700640 * If we are returning from the outermost NMI handler that interrupted an
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -0700641 * RCU-idle period, update rdp->dynticks and rdp->dynticks_nmi_nesting
Paul E. McKenneyfd581a92017-10-02 21:56:20 -0700642 * to let the RCU grace-period handling know that the CPU is back to
643 * being RCU-idle.
644 *
Byungchul Parkcf7614e2018-06-22 15:12:06 +0900645 * If you add or remove a call to rcu_nmi_exit_common(), be sure to test
Paul E. McKenneyfd581a92017-10-02 21:56:20 -0700646 * with CONFIG_RCU_EQS_DEBUG=y.
647 */
Byungchul Parkcf7614e2018-06-22 15:12:06 +0900648static __always_inline void rcu_nmi_exit_common(bool irq)
Paul E. McKenneyfd581a92017-10-02 21:56:20 -0700649{
Paul E. McKenney4c5273b2018-08-03 21:00:38 -0700650 struct rcu_data *rdp = this_cpu_ptr(&rcu_data);
Paul E. McKenneyfd581a92017-10-02 21:56:20 -0700651
652 /*
653 * Check for ->dynticks_nmi_nesting underflow and bad ->dynticks.
654 * (We are exiting an NMI handler, so RCU better be paying attention
655 * to us!)
656 */
Paul E. McKenney4c5273b2018-08-03 21:00:38 -0700657 WARN_ON_ONCE(rdp->dynticks_nmi_nesting <= 0);
Paul E. McKenneyfd581a92017-10-02 21:56:20 -0700658 WARN_ON_ONCE(rcu_dynticks_curr_cpu_in_eqs());
659
660 /*
661 * If the nesting level is not 1, the CPU wasn't RCU-idle, so
662 * leave it in non-RCU-idle state.
663 */
Paul E. McKenney4c5273b2018-08-03 21:00:38 -0700664 if (rdp->dynticks_nmi_nesting != 1) {
Marco Elver6cf539a2019-10-09 17:57:43 +0200665 trace_rcu_dyntick(TPS("--="), rdp->dynticks_nmi_nesting, rdp->dynticks_nmi_nesting - 2,
666 atomic_read(&rdp->dynticks));
Paul E. McKenney4c5273b2018-08-03 21:00:38 -0700667 WRITE_ONCE(rdp->dynticks_nmi_nesting, /* No store tearing. */
668 rdp->dynticks_nmi_nesting - 2);
Paul E. McKenneyfd581a92017-10-02 21:56:20 -0700669 return;
670 }
671
672 /* This NMI interrupted an RCU-idle CPU, restore RCU-idleness. */
Marco Elver6cf539a2019-10-09 17:57:43 +0200673 trace_rcu_dyntick(TPS("Startirq"), rdp->dynticks_nmi_nesting, 0, atomic_read(&rdp->dynticks));
Paul E. McKenney4c5273b2018-08-03 21:00:38 -0700674 WRITE_ONCE(rdp->dynticks_nmi_nesting, 0); /* Avoid store tearing. */
Byungchul Parkcf7614e2018-06-22 15:12:06 +0900675
676 if (irq)
677 rcu_prepare_for_idle();
678
Paul E. McKenneyfd581a92017-10-02 21:56:20 -0700679 rcu_dynticks_eqs_enter();
Byungchul Parkcf7614e2018-06-22 15:12:06 +0900680
681 if (irq)
682 rcu_dynticks_task_enter();
683}
684
685/**
686 * rcu_nmi_exit - inform RCU of exit from NMI context
Byungchul Parkcf7614e2018-06-22 15:12:06 +0900687 *
688 * If you add or remove a call to rcu_nmi_exit(), be sure to test
689 * with CONFIG_RCU_EQS_DEBUG=y.
690 */
691void rcu_nmi_exit(void)
692{
693 rcu_nmi_exit_common(false);
Paul E. McKenneyfd581a92017-10-02 21:56:20 -0700694}
695
696/**
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700697 * rcu_irq_exit - inform RCU that current CPU is exiting irq towards idle
698 *
699 * Exit from an interrupt handler, which might possibly result in entering
700 * idle mode, in other words, leaving the mode in which read-side critical
Paul E. McKenney7c9906c2015-10-31 00:59:01 -0700701 * sections can occur. The caller must have disabled interrupts.
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700702 *
703 * This code assumes that the idle loop never does anything that might
704 * result in unbalanced calls to irq_enter() and irq_exit(). If your
Paul E. McKenney58721f52017-10-03 10:42:22 -0700705 * architecture's idle loop violates this assumption, RCU will give you what
706 * you deserve, good and hard. But very infrequently and irreproducibly.
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700707 *
708 * Use things like work queues to work around this limitation.
709 *
710 * You have been warned.
Paul E. McKenneyc0da3132017-09-22 09:58:47 -0700711 *
712 * If you add or remove a call to rcu_irq_exit(), be sure to test with
713 * CONFIG_RCU_EQS_DEBUG=y.
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700714 */
715void rcu_irq_exit(void)
716{
Frederic Weisbeckerb04db8e2017-11-06 16:01:30 +0100717 lockdep_assert_irqs_disabled();
Byungchul Parkcf7614e2018-06-22 15:12:06 +0900718 rcu_nmi_exit_common(true);
Paul E. McKenney7c9906c2015-10-31 00:59:01 -0700719}
720
721/*
722 * Wrapper for rcu_irq_exit() where interrupts are enabled.
Paul E. McKenneyc0da3132017-09-22 09:58:47 -0700723 *
724 * If you add or remove a call to rcu_irq_exit_irqson(), be sure to test
725 * with CONFIG_RCU_EQS_DEBUG=y.
Paul E. McKenney7c9906c2015-10-31 00:59:01 -0700726 */
727void rcu_irq_exit_irqson(void)
728{
729 unsigned long flags;
730
731 local_irq_save(flags);
732 rcu_irq_exit();
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700733 local_irq_restore(flags);
734}
735
736/*
Frederic Weisbeckeradf50912012-06-28 11:20:21 -0700737 * Exit an RCU extended quiescent state, which can be either the
738 * idle loop or adaptive-tickless usermode execution.
Paul E. McKenney51a1fd32017-10-03 14:43:40 -0700739 *
740 * We crowbar the ->dynticks_nmi_nesting field to DYNTICK_IRQ_NONIDLE to
741 * allow for the possibility of usermode upcalls messing up our count of
742 * interrupt nesting level during the busy period that is just now starting.
Frederic Weisbeckeradf50912012-06-28 11:20:21 -0700743 */
744static void rcu_eqs_exit(bool user)
745{
Paul E. McKenney4c5273b2018-08-03 21:00:38 -0700746 struct rcu_data *rdp;
Paul E. McKenney84585aa2017-10-04 15:55:16 -0700747 long oldval;
Frederic Weisbeckeradf50912012-06-28 11:20:21 -0700748
Frederic Weisbeckerb04db8e2017-11-06 16:01:30 +0100749 lockdep_assert_irqs_disabled();
Paul E. McKenney4c5273b2018-08-03 21:00:38 -0700750 rdp = this_cpu_ptr(&rcu_data);
751 oldval = rdp->dynticks_nesting;
Paul E. McKenney1ce46ee2015-05-05 23:04:22 -0700752 WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) && oldval < 0);
Paul E. McKenney51a1fd32017-10-03 14:43:40 -0700753 if (oldval) {
Paul E. McKenney4c5273b2018-08-03 21:00:38 -0700754 rdp->dynticks_nesting++;
Paul E. McKenney9dd238e2017-10-05 16:56:26 -0700755 return;
Paul E. McKenney3a5924052013-10-04 18:48:55 -0700756 }
Paul E. McKenney9dd238e2017-10-05 16:56:26 -0700757 rcu_dynticks_task_exit();
758 rcu_dynticks_eqs_exit();
759 rcu_cleanup_after_idle();
Marco Elver6cf539a2019-10-09 17:57:43 +0200760 trace_rcu_dyntick(TPS("End"), rdp->dynticks_nesting, 1, atomic_read(&rdp->dynticks));
Paul E. McKenneye68bbb22017-10-05 19:55:31 -0700761 WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) && !user && !is_idle_task(current));
Paul E. McKenney4c5273b2018-08-03 21:00:38 -0700762 WRITE_ONCE(rdp->dynticks_nesting, 1);
763 WARN_ON_ONCE(rdp->dynticks_nmi_nesting);
764 WRITE_ONCE(rdp->dynticks_nmi_nesting, DYNTICK_IRQ_NONIDLE);
Frederic Weisbeckeradf50912012-06-28 11:20:21 -0700765}
766
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700767/**
768 * rcu_idle_exit - inform RCU that current CPU is leaving idle
769 *
770 * Exit idle mode, in other words, -enter- the mode in which RCU
771 * read-side critical sections can occur.
772 *
Paul E. McKenneyc0da3132017-09-22 09:58:47 -0700773 * If you add or remove a call to rcu_idle_exit(), be sure to test with
774 * CONFIG_RCU_EQS_DEBUG=y.
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700775 */
776void rcu_idle_exit(void)
777{
Frederic Weisbeckerc5d900b2012-07-11 20:26:31 +0200778 unsigned long flags;
779
780 local_irq_save(flags);
Paul E. McKenneycb349ca2012-09-04 17:35:31 -0700781 rcu_eqs_exit(false);
Frederic Weisbeckerc5d900b2012-07-11 20:26:31 +0200782 local_irq_restore(flags);
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700783}
784
Paul E. McKenneyd1ec4c32015-05-13 10:41:58 -0700785#ifdef CONFIG_NO_HZ_FULL
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700786/**
Frederic Weisbeckeradf50912012-06-28 11:20:21 -0700787 * rcu_user_exit - inform RCU that we are exiting userspace.
788 *
789 * Exit RCU idle mode while entering the kernel because it can
790 * run a RCU read side critical section anytime.
Paul E. McKenneyc0da3132017-09-22 09:58:47 -0700791 *
792 * If you add or remove a call to rcu_user_exit(), be sure to test with
793 * CONFIG_RCU_EQS_DEBUG=y.
Frederic Weisbeckeradf50912012-06-28 11:20:21 -0700794 */
795void rcu_user_exit(void)
796{
Frederic Weisbecker91d1aa432012-11-27 19:33:25 +0100797 rcu_eqs_exit(1);
Frederic Weisbeckeradf50912012-06-28 11:20:21 -0700798}
Paul E. McKenneyd1ec4c32015-05-13 10:41:58 -0700799#endif /* CONFIG_NO_HZ_FULL */
Frederic Weisbecker19dd15912012-06-04 16:42:35 -0700800
801/**
Byungchul Parkcf7614e2018-06-22 15:12:06 +0900802 * rcu_nmi_enter_common - inform RCU of entry to NMI context
803 * @irq: Is this call from rcu_irq_enter?
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100804 *
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -0700805 * If the CPU was idle from RCU's viewpoint, update rdp->dynticks and
Paul E. McKenney4c5273b2018-08-03 21:00:38 -0700806 * rdp->dynticks_nmi_nesting to let the RCU grace-period handling know
Paul E. McKenney734d1682014-11-21 14:45:12 -0800807 * that the CPU is active. This implementation permits nested NMIs, as
808 * long as the nesting level does not overflow an int. (You will probably
809 * run out of stack space first.)
Paul E. McKenneyc0da3132017-09-22 09:58:47 -0700810 *
Byungchul Parkcf7614e2018-06-22 15:12:06 +0900811 * If you add or remove a call to rcu_nmi_enter_common(), be sure to test
Paul E. McKenneyc0da3132017-09-22 09:58:47 -0700812 * with CONFIG_RCU_EQS_DEBUG=y.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100813 */
Byungchul Parkcf7614e2018-06-22 15:12:06 +0900814static __always_inline void rcu_nmi_enter_common(bool irq)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100815{
Paul E. McKenney84585aa2017-10-04 15:55:16 -0700816 long incby = 2;
Paul E. McKenney5b145572019-11-26 18:05:45 -0800817 struct rcu_data *rdp = this_cpu_ptr(&rcu_data);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100818
Paul E. McKenney734d1682014-11-21 14:45:12 -0800819 /* Complain about underflow. */
Paul E. McKenney4c5273b2018-08-03 21:00:38 -0700820 WARN_ON_ONCE(rdp->dynticks_nmi_nesting < 0);
Paul E. McKenney734d1682014-11-21 14:45:12 -0800821
822 /*
823 * If idle from RCU viewpoint, atomically increment ->dynticks
824 * to mark non-idle and increment ->dynticks_nmi_nesting by one.
825 * Otherwise, increment ->dynticks_nmi_nesting by two. This means
826 * if ->dynticks_nmi_nesting is equal to one, we are guaranteed
827 * to be in the outermost NMI handler that interrupted an RCU-idle
828 * period (observation due to Andy Lutomirski).
829 */
Paul E. McKenney02a5c5502016-11-02 17:25:06 -0700830 if (rcu_dynticks_curr_cpu_in_eqs()) {
Byungchul Parkcf7614e2018-06-22 15:12:06 +0900831
832 if (irq)
833 rcu_dynticks_task_exit();
834
Paul E. McKenney2625d462016-11-02 14:23:30 -0700835 rcu_dynticks_eqs_exit();
Byungchul Parkcf7614e2018-06-22 15:12:06 +0900836
837 if (irq)
838 rcu_cleanup_after_idle();
839
Paul E. McKenney734d1682014-11-21 14:45:12 -0800840 incby = 1;
Paul E. McKenneybf37da92020-03-12 16:55:07 -0700841 } else if (irq && tick_nohz_full_cpu(rdp->cpu) &&
Paul E. McKenneyb200a042019-08-15 13:24:49 -0700842 rdp->dynticks_nmi_nesting == DYNTICK_IRQ_NONIDLE &&
Paul E. McKenney2a2ae872020-01-08 20:06:25 -0800843 READ_ONCE(rdp->rcu_urgent_qs) &&
844 !READ_ONCE(rdp->rcu_forced_tick)) {
Paul E. McKenney5b145572019-11-26 18:05:45 -0800845 raw_spin_lock_rcu_node(rdp->mynode);
846 // Recheck under lock.
847 if (rdp->rcu_urgent_qs && !rdp->rcu_forced_tick) {
Paul E. McKenney2a2ae872020-01-08 20:06:25 -0800848 WRITE_ONCE(rdp->rcu_forced_tick, true);
Paul E. McKenney5b145572019-11-26 18:05:45 -0800849 tick_dep_set_cpu(rdp->cpu, TICK_DEP_BIT_RCU);
850 }
851 raw_spin_unlock_rcu_node(rdp->mynode);
Paul E. McKenney734d1682014-11-21 14:45:12 -0800852 }
Paul E. McKenneybd2b8792017-10-04 12:29:01 -0700853 trace_rcu_dyntick(incby == 1 ? TPS("Endirq") : TPS("++="),
Paul E. McKenney4c5273b2018-08-03 21:00:38 -0700854 rdp->dynticks_nmi_nesting,
Marco Elver6cf539a2019-10-09 17:57:43 +0200855 rdp->dynticks_nmi_nesting + incby, atomic_read(&rdp->dynticks));
Paul E. McKenney4c5273b2018-08-03 21:00:38 -0700856 WRITE_ONCE(rdp->dynticks_nmi_nesting, /* Prevent store tearing. */
857 rdp->dynticks_nmi_nesting + incby);
Paul E. McKenney734d1682014-11-21 14:45:12 -0800858 barrier();
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100859}
860
861/**
Byungchul Parkcf7614e2018-06-22 15:12:06 +0900862 * rcu_nmi_enter - inform RCU of entry to NMI context
863 */
864void rcu_nmi_enter(void)
865{
866 rcu_nmi_enter_common(false);
867}
Masami Hiramatsuc13324a2019-02-13 01:12:15 +0900868NOKPROBE_SYMBOL(rcu_nmi_enter);
Byungchul Parkcf7614e2018-06-22 15:12:06 +0900869
870/**
Paul E. McKenney34240692011-10-03 11:38:52 -0700871 * rcu_irq_enter - inform RCU that current CPU is entering irq away from idle
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100872 *
Paul E. McKenney34240692011-10-03 11:38:52 -0700873 * Enter an interrupt handler, which might possibly result in exiting
874 * idle mode, in other words, entering the mode in which read-side critical
875 * sections can occur. The caller must have disabled interrupts.
Paul E. McKenneyc0da3132017-09-22 09:58:47 -0700876 *
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700877 * Note that the Linux kernel is fully capable of entering an interrupt
Paul E. McKenney58721f52017-10-03 10:42:22 -0700878 * handler that it never exits, for example when doing upcalls to user mode!
879 * This code assumes that the idle loop never does upcalls to user mode.
880 * If your architecture's idle loop does do upcalls to user mode (or does
881 * anything else that results in unbalanced calls to the irq_enter() and
882 * irq_exit() functions), RCU will give you what you deserve, good and hard.
883 * But very infrequently and irreproducibly.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100884 *
885 * Use things like work queues to work around this limitation.
886 *
887 * You have been warned.
888 *
889 * If you add or remove a call to rcu_irq_enter(), be sure to test with
890 * CONFIG_RCU_EQS_DEBUG=y.
Paul E. McKenney23b5c8f2010-09-07 10:38:22 -0700891 */
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100892void rcu_irq_enter(void)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100893{
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100894 lockdep_assert_irqs_disabled();
Byungchul Parkcf7614e2018-06-22 15:12:06 +0900895 rcu_nmi_enter_common(true);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100896}
Paul E. McKenney734d1682014-11-21 14:45:12 -0800897
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100898/*
899 * Wrapper for rcu_irq_enter() where interrupts are enabled.
900 *
901 * If you add or remove a call to rcu_irq_enter_irqson(), be sure to test
902 * with CONFIG_RCU_EQS_DEBUG=y.
903 */
904void rcu_irq_enter_irqson(void)
905{
906 unsigned long flags;
Paul E. McKenney734d1682014-11-21 14:45:12 -0800907
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100908 local_irq_save(flags);
909 rcu_irq_enter();
910 local_irq_restore(flags);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100911}
912
Paul E. McKenney66e4c332019-08-12 16:14:00 -0700913/*
Joel Fernandes (Google)516e5ae2019-09-05 10:26:41 -0700914 * If any sort of urgency was applied to the current CPU (for example,
915 * the scheduler-clock interrupt was enabled on a nohz_full CPU) in order
916 * to get to a quiescent state, disable it.
Paul E. McKenney66e4c332019-08-12 16:14:00 -0700917 */
Joel Fernandes (Google)516e5ae2019-09-05 10:26:41 -0700918static void rcu_disable_urgency_upon_qs(struct rcu_data *rdp)
Paul E. McKenney66e4c332019-08-12 16:14:00 -0700919{
Paul E. McKenney5b145572019-11-26 18:05:45 -0800920 raw_lockdep_assert_held_rcu_node(rdp->mynode);
Joel Fernandes (Google)516e5ae2019-09-05 10:26:41 -0700921 WRITE_ONCE(rdp->rcu_urgent_qs, false);
922 WRITE_ONCE(rdp->rcu_need_heavy_qs, false);
Paul E. McKenney66e4c332019-08-12 16:14:00 -0700923 if (tick_nohz_full_cpu(rdp->cpu) && rdp->rcu_forced_tick) {
924 tick_dep_clear_cpu(rdp->cpu, TICK_DEP_BIT_RCU);
Paul E. McKenney2a2ae872020-01-08 20:06:25 -0800925 WRITE_ONCE(rdp->rcu_forced_tick, false);
Paul E. McKenney66e4c332019-08-12 16:14:00 -0700926 }
927}
928
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100929/**
Zhouyi Zhou2320bda2018-10-08 06:50:41 +0000930 * rcu_is_watching - see if RCU thinks that the current CPU is not idle
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100931 *
Paul E. McKenney791875d2017-05-03 11:06:05 -0700932 * Return true if RCU is watching the running CPU, which means that this
933 * CPU can safely enter RCU read-side critical sections. In other words,
Zhouyi Zhou2320bda2018-10-08 06:50:41 +0000934 * if the current CPU is not in its idle loop or is in an interrupt or
935 * NMI handler, return true.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100936 */
Linus Torvaldsb29c8302013-11-16 12:23:18 -0800937bool notrace rcu_is_watching(void)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100938{
Pranith Kumarf534ed12014-07-08 18:26:11 -0400939 bool ret;
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700940
Alexei Starovoitov46f00d12015-06-16 10:35:18 -0700941 preempt_disable_notrace();
Paul E. McKenney791875d2017-05-03 11:06:05 -0700942 ret = !rcu_dynticks_curr_cpu_in_eqs();
Alexei Starovoitov46f00d12015-06-16 10:35:18 -0700943 preempt_enable_notrace();
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700944 return ret;
945}
Paul E. McKenney5c173eb2013-09-13 17:20:11 -0700946EXPORT_SYMBOL_GPL(rcu_is_watching);
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700947
Paul E. McKenneybcbfdd02017-04-11 15:50:41 -0700948/*
949 * If a holdout task is actually running, request an urgent quiescent
950 * state from its CPU. This is unsynchronized, so migrations can cause
951 * the request to go to the wrong CPU. Which is OK, all that will happen
952 * is that the CPU's next context switch will be a bit slower and next
953 * time around this task will generate another request.
954 */
955void rcu_request_urgent_qs_task(struct task_struct *t)
956{
957 int cpu;
958
959 barrier();
960 cpu = task_cpu(t);
961 if (!task_curr(t))
962 return; /* This task is not running on that CPU. */
Paul E. McKenney2dba13f2018-08-03 21:00:38 -0700963 smp_store_release(per_cpu_ptr(&rcu_data.rcu_urgent_qs, cpu), true);
Paul E. McKenneybcbfdd02017-04-11 15:50:41 -0700964}
965
Paul E. McKenney62fde6e2012-05-22 22:10:24 -0700966#if defined(CONFIG_PROVE_RCU) && defined(CONFIG_HOTPLUG_CPU)
Paul E. McKenneyc0d6d012012-01-23 12:41:26 -0800967
968/*
Paul E. McKenney55547882018-05-15 10:14:34 -0700969 * Is the current CPU online as far as RCU is concerned?
Paul E. McKenney2036d942012-01-30 17:02:47 -0800970 *
Paul E. McKenney55547882018-05-15 10:14:34 -0700971 * Disable preemption to avoid false positives that could otherwise
972 * happen due to the current CPU number being sampled, this task being
973 * preempted, its old CPU being taken offline, resuming on some other CPU,
Paul E. McKenney49918a52018-07-07 18:12:26 -0700974 * then determining that its old CPU is now offline.
Paul E. McKenneyc0d6d012012-01-23 12:41:26 -0800975 *
Paul E. McKenney55547882018-05-15 10:14:34 -0700976 * Disable checking if in an NMI handler because we cannot safely
977 * report errors from NMI handlers anyway. In addition, it is OK to use
978 * RCU on an offline processor during initial boot, hence the check for
979 * rcu_scheduler_fully_active.
Paul E. McKenneyc0d6d012012-01-23 12:41:26 -0800980 */
981bool rcu_lockdep_current_cpu_online(void)
982{
Paul E. McKenney2036d942012-01-30 17:02:47 -0800983 struct rcu_data *rdp;
984 struct rcu_node *rnp;
Paul E. McKenneyb97d23c2018-07-04 15:35:00 -0700985 bool ret = false;
Paul E. McKenneyc0d6d012012-01-23 12:41:26 -0800986
Paul E. McKenney55547882018-05-15 10:14:34 -0700987 if (in_nmi() || !rcu_scheduler_fully_active)
Fengguang Wuf6f7ee92013-10-10 11:08:33 -0700988 return true;
Paul E. McKenneyc0d6d012012-01-23 12:41:26 -0800989 preempt_disable();
Paul E. McKenneyb97d23c2018-07-04 15:35:00 -0700990 rdp = this_cpu_ptr(&rcu_data);
991 rnp = rdp->mynode;
992 if (rdp->grpmask & rcu_rnp_online_cpus(rnp))
993 ret = true;
Paul E. McKenneyc0d6d012012-01-23 12:41:26 -0800994 preempt_enable();
Paul E. McKenneyb97d23c2018-07-04 15:35:00 -0700995 return ret;
Paul E. McKenneyc0d6d012012-01-23 12:41:26 -0800996}
997EXPORT_SYMBOL_GPL(rcu_lockdep_current_cpu_online);
998
Paul E. McKenney62fde6e2012-05-22 22:10:24 -0700999#endif /* #if defined(CONFIG_PROVE_RCU) && defined(CONFIG_HOTPLUG_CPU) */
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -07001000
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001001/*
Paul E. McKenney9b9500d2017-08-17 17:05:59 -07001002 * We are reporting a quiescent state on behalf of some other CPU, so
1003 * it is our responsibility to check for and handle potential overflow
Paul E. McKenneya66ae8a2018-04-27 18:06:08 -07001004 * of the rcu_node ->gp_seq counter with respect to the rcu_data counters.
Paul E. McKenney9b9500d2017-08-17 17:05:59 -07001005 * After all, the CPU might be in deep idle state, and thus executing no
1006 * code whatsoever.
1007 */
1008static void rcu_gpnum_ovf(struct rcu_node *rnp, struct rcu_data *rdp)
1009{
Matthew Wilcoxa32e01e2018-01-17 06:24:30 -08001010 raw_lockdep_assert_held_rcu_node(rnp);
Paul E. McKenneya66ae8a2018-04-27 18:06:08 -07001011 if (ULONG_CMP_LT(rcu_seq_current(&rdp->gp_seq) + ULONG_MAX / 4,
1012 rnp->gp_seq))
Paul E. McKenney9b9500d2017-08-17 17:05:59 -07001013 WRITE_ONCE(rdp->gpwrap, true);
Paul E. McKenney8aa670c2018-04-28 14:15:40 -07001014 if (ULONG_CMP_LT(rdp->rcu_iw_gp_seq + ULONG_MAX / 4, rnp->gp_seq))
1015 rdp->rcu_iw_gp_seq = rnp->gp_seq + ULONG_MAX / 4;
Paul E. McKenney9b9500d2017-08-17 17:05:59 -07001016}
1017
1018/*
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001019 * Snapshot the specified CPU's dynticks counter so that we can later
1020 * credit them with an implicit quiescent state. Return 1 if this CPU
Paul E. McKenney1eba8f82009-09-23 09:50:42 -07001021 * is in dynticks idle mode, which is an extended quiescent state.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001022 */
Paul E. McKenneyfe5ac722017-05-11 11:26:22 -07001023static int dyntick_save_progress_counter(struct rcu_data *rdp)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001024{
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -07001025 rdp->dynticks_snap = rcu_dynticks_snap(rdp);
Paul E. McKenney02a5c5502016-11-02 17:25:06 -07001026 if (rcu_dynticks_in_eqs(rdp->dynticks_snap)) {
Paul E. McKenney88d1bea2018-07-04 14:45:00 -07001027 trace_rcu_fqs(rcu_state.name, rdp->gp_seq, rdp->cpu, TPS("dti"));
Paul E. McKenney9b9500d2017-08-17 17:05:59 -07001028 rcu_gpnum_ovf(rdp->mynode, rdp);
Paul E. McKenney23a9bac2015-12-13 08:57:10 -08001029 return 1;
Andreea-Cristina Bernat7941dbd2014-03-17 18:33:28 +02001030 }
Paul E. McKenney23a9bac2015-12-13 08:57:10 -08001031 return 0;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001032}
1033
1034/*
1035 * Return true if the specified CPU has passed through a quiescent
1036 * state by virtue of being in or having passed through an dynticks
1037 * idle state since the last call to dyntick_save_progress_counter()
Paul E. McKenneya82dcc72012-08-01 14:29:20 -07001038 * for this same CPU, or by virtue of having been offline.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001039 */
Paul E. McKenneyfe5ac722017-05-11 11:26:22 -07001040static int rcu_implicit_dynticks_qs(struct rcu_data *rdp)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001041{
Paul E. McKenney3a19b462016-11-30 11:21:21 -08001042 unsigned long jtsq;
Paul E. McKenney0f9be8c2017-01-27 13:17:02 -08001043 bool *rnhqp;
Paul E. McKenney9226b102017-01-27 14:17:50 -08001044 bool *ruqp;
Paul E. McKenney9b9500d2017-08-17 17:05:59 -07001045 struct rcu_node *rnp = rdp->mynode;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001046
1047 /*
1048 * If the CPU passed through or entered a dynticks idle phase with
1049 * no active irq/NMI handlers, then we can safely pretend that the CPU
1050 * already acknowledged the request to pass through a quiescent
1051 * state. Either way, that CPU cannot possibly be in an RCU
1052 * read-side critical section that started before the beginning
1053 * of the current RCU grace period.
1054 */
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -07001055 if (rcu_dynticks_in_eqs_since(rdp, rdp->dynticks_snap)) {
Paul E. McKenney88d1bea2018-07-04 14:45:00 -07001056 trace_rcu_fqs(rcu_state.name, rdp->gp_seq, rdp->cpu, TPS("dti"));
Paul E. McKenney9b9500d2017-08-17 17:05:59 -07001057 rcu_gpnum_ovf(rnp, rdp);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001058 return 1;
1059 }
1060
Paul E. McKenneyf2e2df52018-05-15 16:23:23 -07001061 /* If waiting too long on an offline CPU, complain. */
1062 if (!(rdp->grpmask & rcu_rnp_online_cpus(rnp)) &&
Paul E. McKenney88d1bea2018-07-04 14:45:00 -07001063 time_after(jiffies, rcu_state.gp_start + HZ)) {
Paul E. McKenneyf2e2df52018-05-15 16:23:23 -07001064 bool onl;
1065 struct rcu_node *rnp1;
1066
1067 WARN_ON(1); /* Offline CPUs are supposed to report QS! */
1068 pr_info("%s: grp: %d-%d level: %d ->gp_seq %ld ->completedqs %ld\n",
1069 __func__, rnp->grplo, rnp->grphi, rnp->level,
1070 (long)rnp->gp_seq, (long)rnp->completedqs);
1071 for (rnp1 = rnp; rnp1; rnp1 = rnp1->parent)
1072 pr_info("%s: %d:%d ->qsmask %#lx ->qsmaskinit %#lx ->qsmaskinitnext %#lx ->rcu_gp_init_mask %#lx\n",
1073 __func__, rnp1->grplo, rnp1->grphi, rnp1->qsmask, rnp1->qsmaskinit, rnp1->qsmaskinitnext, rnp1->rcu_gp_init_mask);
1074 onl = !!(rdp->grpmask & rcu_rnp_online_cpus(rnp));
1075 pr_info("%s %d: %c online: %ld(%d) offline: %ld(%d)\n",
1076 __func__, rdp->cpu, ".o"[onl],
1077 (long)rdp->rcu_onl_gp_seq, rdp->rcu_onl_gp_flags,
1078 (long)rdp->rcu_ofl_gp_seq, rdp->rcu_ofl_gp_flags);
1079 return 1; /* Break things loose after complaining. */
1080 }
1081
Paul E. McKenney65d798f2013-04-12 16:19:10 -07001082 /*
Paul E. McKenney4a81e832014-06-20 16:49:01 -07001083 * A CPU running for an extended time within the kernel can
Paul E. McKenneyc06aed02018-07-25 11:25:23 -07001084 * delay RCU grace periods: (1) At age jiffies_to_sched_qs,
1085 * set .rcu_urgent_qs, (2) At age 2*jiffies_to_sched_qs, set
Paul E. McKenney7e28c5a2018-07-11 08:09:28 -07001086 * both .rcu_need_heavy_qs and .rcu_urgent_qs. Note that the
1087 * unsynchronized assignments to the per-CPU rcu_need_heavy_qs
1088 * variable are safe because the assignments are repeated if this
1089 * CPU failed to pass through a quiescent state. This code
Paul E. McKenneyc06aed02018-07-25 11:25:23 -07001090 * also checks .jiffies_resched in case jiffies_to_sched_qs
Paul E. McKenney7e28c5a2018-07-11 08:09:28 -07001091 * is set way high.
Paul E. McKenney65d798f2013-04-12 16:19:10 -07001092 */
Paul E. McKenneyc06aed02018-07-25 11:25:23 -07001093 jtsq = READ_ONCE(jiffies_to_sched_qs);
Paul E. McKenney2dba13f2018-08-03 21:00:38 -07001094 ruqp = per_cpu_ptr(&rcu_data.rcu_urgent_qs, rdp->cpu);
1095 rnhqp = &per_cpu(rcu_data.rcu_need_heavy_qs, rdp->cpu);
Paul E. McKenney0f9be8c2017-01-27 13:17:02 -08001096 if (!READ_ONCE(*rnhqp) &&
Paul E. McKenney7e28c5a2018-07-11 08:09:28 -07001097 (time_after(jiffies, rcu_state.gp_start + jtsq * 2) ||
Paul E. McKenneyb2b00dd2019-10-30 11:56:10 -07001098 time_after(jiffies, rcu_state.jiffies_resched) ||
1099 rcu_state.cbovld)) {
Paul E. McKenney0f9be8c2017-01-27 13:17:02 -08001100 WRITE_ONCE(*rnhqp, true);
Paul E. McKenney9226b102017-01-27 14:17:50 -08001101 /* Store rcu_need_heavy_qs before rcu_urgent_qs. */
1102 smp_store_release(ruqp, true);
Paul E. McKenney7e28c5a2018-07-11 08:09:28 -07001103 } else if (time_after(jiffies, rcu_state.gp_start + jtsq)) {
1104 WRITE_ONCE(*ruqp, true);
Paul E. McKenney6193c762013-09-23 13:57:18 -07001105 }
1106
Paul E. McKenney28053bc2016-12-01 11:31:31 -08001107 /*
Paul E. McKenneyc98cac62018-11-21 11:35:03 -08001108 * NO_HZ_FULL CPUs can run in-kernel without rcu_sched_clock_irq!
Paul E. McKenneyd3052102018-07-25 11:49:47 -07001109 * The above code handles this, but only for straight cond_resched().
1110 * And some in-kernel loops check need_resched() before calling
1111 * cond_resched(), which defeats the above code for CPUs that are
1112 * running in-kernel with scheduling-clock interrupts disabled.
1113 * So hit them over the head with the resched_cpu() hammer!
Paul E. McKenney28053bc2016-12-01 11:31:31 -08001114 */
Paul E. McKenneyd3052102018-07-25 11:49:47 -07001115 if (tick_nohz_full_cpu(rdp->cpu) &&
Paul E. McKenneyb2b00dd2019-10-30 11:56:10 -07001116 (time_after(jiffies, READ_ONCE(rdp->last_fqs_resched) + jtsq * 3) ||
1117 rcu_state.cbovld)) {
Joel Fernandes (Google)05ef9e92019-08-15 22:59:14 -04001118 WRITE_ONCE(*ruqp, true);
Paul E. McKenney28053bc2016-12-01 11:31:31 -08001119 resched_cpu(rdp->cpu);
Paul E. McKenneyd3052102018-07-25 11:49:47 -07001120 WRITE_ONCE(rdp->last_fqs_resched, jiffies);
1121 }
1122
1123 /*
1124 * If more than halfway to RCU CPU stall-warning time, invoke
1125 * resched_cpu() more frequently to try to loosen things up a bit.
1126 * Also check to see if the CPU is getting hammered with interrupts,
1127 * but only once per grace period, just to keep the IPIs down to
1128 * a dull roar.
Paul E. McKenney49149502015-12-11 13:48:43 -08001129 */
Paul E. McKenney7e28c5a2018-07-11 08:09:28 -07001130 if (time_after(jiffies, rcu_state.jiffies_resched)) {
Paul E. McKenneyd3052102018-07-25 11:49:47 -07001131 if (time_after(jiffies,
1132 READ_ONCE(rdp->last_fqs_resched) + jtsq)) {
1133 resched_cpu(rdp->cpu);
1134 WRITE_ONCE(rdp->last_fqs_resched, jiffies);
1135 }
Paul E. McKenney9b9500d2017-08-17 17:05:59 -07001136 if (IS_ENABLED(CONFIG_IRQ_WORK) &&
Paul E. McKenney8aa670c2018-04-28 14:15:40 -07001137 !rdp->rcu_iw_pending && rdp->rcu_iw_gp_seq != rnp->gp_seq &&
Paul E. McKenney9b9500d2017-08-17 17:05:59 -07001138 (rnp->ffmask & rdp->grpmask)) {
1139 init_irq_work(&rdp->rcu_iw, rcu_iw_handler);
Sebastian Andrzej Siewior49915ac2020-03-21 12:26:03 +01001140 atomic_set(&rdp->rcu_iw.flags, IRQ_WORK_HARD_IRQ);
Paul E. McKenney9b9500d2017-08-17 17:05:59 -07001141 rdp->rcu_iw_pending = true;
Paul E. McKenney8aa670c2018-04-28 14:15:40 -07001142 rdp->rcu_iw_gp_seq = rnp->gp_seq;
Paul E. McKenney9b9500d2017-08-17 17:05:59 -07001143 irq_work_queue_on(&rdp->rcu_iw, rdp->cpu);
1144 }
1145 }
Paul E. McKenney49149502015-12-11 13:48:43 -08001146
Paul E. McKenneya82dcc72012-08-01 14:29:20 -07001147 return 0;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001148}
1149
Paul E. McKenney41e80592018-04-12 11:24:09 -07001150/* Trace-event wrapper function for trace_rcu_future_grace_period. */
1151static void trace_rcu_this_gp(struct rcu_node *rnp, struct rcu_data *rdp,
Joel Fernandesb73de912018-05-20 21:42:18 -07001152 unsigned long gp_seq_req, const char *s)
Paul E. McKenney0446be42012-12-30 15:21:01 -08001153{
Paul E. McKenney0937d042020-01-03 14:53:31 -08001154 trace_rcu_future_grace_period(rcu_state.name, READ_ONCE(rnp->gp_seq),
1155 gp_seq_req, rnp->level,
1156 rnp->grplo, rnp->grphi, s);
Paul E. McKenney0446be42012-12-30 15:21:01 -08001157}
1158
1159/*
Joel Fernandesb73de912018-05-20 21:42:18 -07001160 * rcu_start_this_gp - Request the start of a particular grace period
Joel Fernandes (Google)df2bf8f2018-05-22 23:38:14 -07001161 * @rnp_start: The leaf node of the CPU from which to start.
Joel Fernandesb73de912018-05-20 21:42:18 -07001162 * @rdp: The rcu_data corresponding to the CPU from which to start.
1163 * @gp_seq_req: The gp_seq of the grace period to start.
1164 *
Paul E. McKenney41e80592018-04-12 11:24:09 -07001165 * Start the specified grace period, as needed to handle newly arrived
Paul E. McKenney0446be42012-12-30 15:21:01 -08001166 * callbacks. The required future grace periods are recorded in each
Paul E. McKenney7a1d0f22018-05-01 10:26:57 -07001167 * rcu_node structure's ->gp_seq_needed field. Returns true if there
Paul E. McKenney48a76392014-03-11 13:02:16 -07001168 * is reason to awaken the grace-period kthread.
Paul E. McKenney0446be42012-12-30 15:21:01 -08001169 *
Paul E. McKenneyd5cd9682018-04-12 10:45:06 -07001170 * The caller must hold the specified rcu_node structure's ->lock, which
1171 * is why the caller is responsible for waking the grace-period kthread.
Joel Fernandesb73de912018-05-20 21:42:18 -07001172 *
1173 * Returns true if the GP thread needs to be awakened else false.
Paul E. McKenney0446be42012-12-30 15:21:01 -08001174 */
Joel Fernandes (Google)df2bf8f2018-05-22 23:38:14 -07001175static bool rcu_start_this_gp(struct rcu_node *rnp_start, struct rcu_data *rdp,
Joel Fernandesb73de912018-05-20 21:42:18 -07001176 unsigned long gp_seq_req)
Paul E. McKenney0446be42012-12-30 15:21:01 -08001177{
Paul E. McKenney48a76392014-03-11 13:02:16 -07001178 bool ret = false;
Joel Fernandes (Google)df2bf8f2018-05-22 23:38:14 -07001179 struct rcu_node *rnp;
Paul E. McKenney0446be42012-12-30 15:21:01 -08001180
Paul E. McKenney360e0da2018-04-12 11:50:41 -07001181 /*
1182 * Use funnel locking to either acquire the root rcu_node
1183 * structure's lock or bail out if the need for this grace period
Joel Fernandes (Google)df2bf8f2018-05-22 23:38:14 -07001184 * has already been recorded -- or if that grace period has in
1185 * fact already started. If there is already a grace period in
1186 * progress in a non-leaf node, no recording is needed because the
1187 * end of the grace period will scan the leaf rcu_node structures.
1188 * Note that rnp_start->lock must not be released.
Paul E. McKenney360e0da2018-04-12 11:50:41 -07001189 */
Joel Fernandes (Google)df2bf8f2018-05-22 23:38:14 -07001190 raw_lockdep_assert_held_rcu_node(rnp_start);
1191 trace_rcu_this_gp(rnp_start, rdp, gp_seq_req, TPS("Startleaf"));
1192 for (rnp = rnp_start; 1; rnp = rnp->parent) {
1193 if (rnp != rnp_start)
1194 raw_spin_lock_rcu_node(rnp);
1195 if (ULONG_CMP_GE(rnp->gp_seq_needed, gp_seq_req) ||
1196 rcu_seq_started(&rnp->gp_seq, gp_seq_req) ||
1197 (rnp != rnp_start &&
1198 rcu_seq_state(rcu_seq_current(&rnp->gp_seq)))) {
1199 trace_rcu_this_gp(rnp, rdp, gp_seq_req,
Joel Fernandesb73de912018-05-20 21:42:18 -07001200 TPS("Prestarted"));
Paul E. McKenney360e0da2018-04-12 11:50:41 -07001201 goto unlock_out;
1202 }
Paul E. McKenney8ff372902020-01-04 11:33:17 -08001203 WRITE_ONCE(rnp->gp_seq_needed, gp_seq_req);
Joel Fernandes (Google)226ca5e2018-05-22 23:38:15 -07001204 if (rcu_seq_state(rcu_seq_current(&rnp->gp_seq))) {
Paul E. McKenneya2165e42018-05-12 07:42:20 -07001205 /*
Joel Fernandes (Google)226ca5e2018-05-22 23:38:15 -07001206 * We just marked the leaf or internal node, and a
1207 * grace period is in progress, which means that
1208 * rcu_gp_cleanup() will see the marking. Bail to
1209 * reduce contention.
Paul E. McKenneya2165e42018-05-12 07:42:20 -07001210 */
Joel Fernandes (Google)df2bf8f2018-05-22 23:38:14 -07001211 trace_rcu_this_gp(rnp_start, rdp, gp_seq_req,
Joel Fernandesb73de912018-05-20 21:42:18 -07001212 TPS("Startedleaf"));
Paul E. McKenneya2165e42018-05-12 07:42:20 -07001213 goto unlock_out;
1214 }
Joel Fernandes (Google)df2bf8f2018-05-22 23:38:14 -07001215 if (rnp != rnp_start && rnp->parent != NULL)
1216 raw_spin_unlock_rcu_node(rnp);
1217 if (!rnp->parent)
Paul E. McKenney360e0da2018-04-12 11:50:41 -07001218 break; /* At root, and perhaps also leaf. */
Paul E. McKenney0446be42012-12-30 15:21:01 -08001219 }
1220
Paul E. McKenney360e0da2018-04-12 11:50:41 -07001221 /* If GP already in progress, just leave, otherwise start one. */
Paul E. McKenneyde8e8732018-07-03 17:22:34 -07001222 if (rcu_gp_in_progress()) {
Joel Fernandes (Google)df2bf8f2018-05-22 23:38:14 -07001223 trace_rcu_this_gp(rnp, rdp, gp_seq_req, TPS("Startedleafroot"));
Paul E. McKenney0446be42012-12-30 15:21:01 -08001224 goto unlock_out;
1225 }
Joel Fernandes (Google)df2bf8f2018-05-22 23:38:14 -07001226 trace_rcu_this_gp(rnp, rdp, gp_seq_req, TPS("Startedroot"));
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001227 WRITE_ONCE(rcu_state.gp_flags, rcu_state.gp_flags | RCU_GP_FLAG_INIT);
Paul E. McKenney2906d212020-01-03 15:17:12 -08001228 WRITE_ONCE(rcu_state.gp_req_activity, jiffies);
Paul E. McKenney5648d652020-01-21 12:30:22 -08001229 if (!READ_ONCE(rcu_state.gp_kthread)) {
Joel Fernandes (Google)df2bf8f2018-05-22 23:38:14 -07001230 trace_rcu_this_gp(rnp, rdp, gp_seq_req, TPS("NoGPkthread"));
Paul E. McKenney360e0da2018-04-12 11:50:41 -07001231 goto unlock_out;
Paul E. McKenney0446be42012-12-30 15:21:01 -08001232 }
Paul E. McKenney0f11ad32020-02-10 09:58:37 -08001233 trace_rcu_grace_period(rcu_state.name, rcu_state.gp_seq, TPS("newreq"));
Paul E. McKenney360e0da2018-04-12 11:50:41 -07001234 ret = true; /* Caller must wake GP kthread. */
Paul E. McKenney0446be42012-12-30 15:21:01 -08001235unlock_out:
Paul E. McKenneyab5e8692018-05-01 11:07:23 -07001236 /* Push furthest requested GP to leaf node and rcu_data structure. */
Joel Fernandes (Google)df2bf8f2018-05-22 23:38:14 -07001237 if (ULONG_CMP_LT(gp_seq_req, rnp->gp_seq_needed)) {
Paul E. McKenney8ff372902020-01-04 11:33:17 -08001238 WRITE_ONCE(rnp_start->gp_seq_needed, rnp->gp_seq_needed);
1239 WRITE_ONCE(rdp->gp_seq_needed, rnp->gp_seq_needed);
Paul E. McKenneyab5e8692018-05-01 11:07:23 -07001240 }
Joel Fernandes (Google)df2bf8f2018-05-22 23:38:14 -07001241 if (rnp != rnp_start)
1242 raw_spin_unlock_rcu_node(rnp);
Paul E. McKenney48a76392014-03-11 13:02:16 -07001243 return ret;
Paul E. McKenney0446be42012-12-30 15:21:01 -08001244}
1245
1246/*
1247 * Clean up any old requests for the just-ended grace period. Also return
Paul E. McKenneyd1e4f012017-02-08 14:58:41 -08001248 * whether any additional grace periods have been requested.
Paul E. McKenney0446be42012-12-30 15:21:01 -08001249 */
Paul E. McKenney3481f2e2018-07-03 17:22:34 -07001250static bool rcu_future_gp_cleanup(struct rcu_node *rnp)
Paul E. McKenney0446be42012-12-30 15:21:01 -08001251{
Paul E. McKenneyfb313402018-04-12 07:20:30 -07001252 bool needmore;
Paul E. McKenneyda1df502018-07-03 15:37:16 -07001253 struct rcu_data *rdp = this_cpu_ptr(&rcu_data);
Paul E. McKenney0446be42012-12-30 15:21:01 -08001254
Paul E. McKenney7a1d0f22018-05-01 10:26:57 -07001255 needmore = ULONG_CMP_LT(rnp->gp_seq, rnp->gp_seq_needed);
1256 if (!needmore)
1257 rnp->gp_seq_needed = rnp->gp_seq; /* Avoid counter wrap. */
Joel Fernandesb73de912018-05-20 21:42:18 -07001258 trace_rcu_this_gp(rnp, rdp, rnp->gp_seq,
Paul E. McKenney41e80592018-04-12 11:24:09 -07001259 needmore ? TPS("CleanupMore") : TPS("Cleanup"));
Paul E. McKenney0446be42012-12-30 15:21:01 -08001260 return needmore;
1261}
1262
1263/*
Paul E. McKenney5648d652020-01-21 12:30:22 -08001264 * Awaken the grace-period kthread. Don't do a self-awaken (unless in an
1265 * interrupt or softirq handler, in which case we just might immediately
1266 * sleep upon return, resulting in a grace-period hang), and don't bother
1267 * awakening when there is nothing for the grace-period kthread to do
1268 * (as in several CPUs raced to awaken, we lost), and finally don't try
1269 * to awaken a kthread that has not yet been created. If all those checks
1270 * are passed, track some debug information and awaken.
Zhang, Jun1d1f8982018-12-18 06:55:01 -08001271 *
1272 * So why do the self-wakeup when in an interrupt or softirq handler
1273 * in the grace-period kthread's context? Because the kthread might have
1274 * been interrupted just as it was going to sleep, and just after the final
1275 * pre-sleep check of the awaken condition. In this case, a wakeup really
1276 * is required, and is therefore supplied.
Paul E. McKenney48a76392014-03-11 13:02:16 -07001277 */
Paul E. McKenney532c00c2018-07-03 17:22:34 -07001278static void rcu_gp_kthread_wake(void)
Paul E. McKenney48a76392014-03-11 13:02:16 -07001279{
Paul E. McKenney5648d652020-01-21 12:30:22 -08001280 struct task_struct *t = READ_ONCE(rcu_state.gp_kthread);
1281
1282 if ((current == t && !in_irq() && !in_serving_softirq()) ||
1283 !READ_ONCE(rcu_state.gp_flags) || !t)
Paul E. McKenney48a76392014-03-11 13:02:16 -07001284 return;
Paul E. McKenneyfd897572018-12-10 16:09:49 -08001285 WRITE_ONCE(rcu_state.gp_wake_time, jiffies);
1286 WRITE_ONCE(rcu_state.gp_wake_seq, READ_ONCE(rcu_state.gp_seq));
Paul E. McKenney532c00c2018-07-03 17:22:34 -07001287 swake_up_one(&rcu_state.gp_wq);
Paul E. McKenney48a76392014-03-11 13:02:16 -07001288}
1289
1290/*
Paul E. McKenney29365e52018-04-30 10:57:36 -07001291 * If there is room, assign a ->gp_seq number to any callbacks on this
1292 * CPU that have not already been assigned. Also accelerate any callbacks
1293 * that were previously assigned a ->gp_seq number that has since proven
1294 * to be too conservative, which can happen if callbacks get assigned a
1295 * ->gp_seq number while RCU is idle, but with reference to a non-root
1296 * rcu_node structure. This function is idempotent, so it does not hurt
1297 * to call it repeatedly. Returns an flag saying that we should awaken
1298 * the RCU grace-period kthread.
Paul E. McKenneydc35c892012-12-03 13:52:00 -08001299 *
1300 * The caller must hold rnp->lock with interrupts disabled.
1301 */
Paul E. McKenney02f50142018-07-03 17:22:34 -07001302static bool rcu_accelerate_cbs(struct rcu_node *rnp, struct rcu_data *rdp)
Paul E. McKenneydc35c892012-12-03 13:52:00 -08001303{
Joel Fernandesb73de912018-05-20 21:42:18 -07001304 unsigned long gp_seq_req;
Paul E. McKenney15fecf82017-02-08 12:36:42 -08001305 bool ret = false;
Paul E. McKenneydc35c892012-12-03 13:52:00 -08001306
Paul E. McKenneyd1b222c2019-07-02 16:03:33 -07001307 rcu_lockdep_assert_cblist_protected(rdp);
Matthew Wilcoxa32e01e2018-01-17 06:24:30 -08001308 raw_lockdep_assert_held_rcu_node(rnp);
Paul E. McKenneyc0b334c2017-04-28 12:32:15 -07001309
Paul E. McKenney15fecf82017-02-08 12:36:42 -08001310 /* If no pending (not yet ready to invoke) callbacks, nothing to do. */
1311 if (!rcu_segcblist_pend_cbs(&rdp->cblist))
Paul E. McKenney48a76392014-03-11 13:02:16 -07001312 return false;
Paul E. McKenneydc35c892012-12-03 13:52:00 -08001313
1314 /*
Paul E. McKenney15fecf82017-02-08 12:36:42 -08001315 * Callbacks are often registered with incomplete grace-period
1316 * information. Something about the fact that getting exact
1317 * information requires acquiring a global lock... RCU therefore
1318 * makes a conservative estimate of the grace period number at which
1319 * a given callback will become ready to invoke. The following
1320 * code checks this estimate and improves it when possible, thus
1321 * accelerating callback invocation to an earlier grace-period
1322 * number.
Paul E. McKenneydc35c892012-12-03 13:52:00 -08001323 */
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001324 gp_seq_req = rcu_seq_snap(&rcu_state.gp_seq);
Joel Fernandesb73de912018-05-20 21:42:18 -07001325 if (rcu_segcblist_accelerate(&rdp->cblist, gp_seq_req))
1326 ret = rcu_start_this_gp(rnp, rdp, gp_seq_req);
Paul E. McKenney6d4b4182012-11-27 16:55:44 -08001327
1328 /* Trace depending on how much we were able to accelerate. */
Paul E. McKenney15fecf82017-02-08 12:36:42 -08001329 if (rcu_segcblist_restempty(&rdp->cblist, RCU_WAIT_TAIL))
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001330 trace_rcu_grace_period(rcu_state.name, rdp->gp_seq, TPS("AccWaitCB"));
Paul E. McKenney6d4b4182012-11-27 16:55:44 -08001331 else
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001332 trace_rcu_grace_period(rcu_state.name, rdp->gp_seq, TPS("AccReadyCB"));
Paul E. McKenney48a76392014-03-11 13:02:16 -07001333 return ret;
Paul E. McKenneydc35c892012-12-03 13:52:00 -08001334}
1335
1336/*
Paul E. McKenneye44e73c2018-05-01 16:29:47 -07001337 * Similar to rcu_accelerate_cbs(), but does not require that the leaf
1338 * rcu_node structure's ->lock be held. It consults the cached value
1339 * of ->gp_seq_needed in the rcu_data structure, and if that indicates
1340 * that a new grace-period request be made, invokes rcu_accelerate_cbs()
1341 * while holding the leaf rcu_node structure's ->lock.
1342 */
Paul E. McKenneyc6e09b92018-07-03 17:22:34 -07001343static void rcu_accelerate_cbs_unlocked(struct rcu_node *rnp,
Paul E. McKenneye44e73c2018-05-01 16:29:47 -07001344 struct rcu_data *rdp)
1345{
1346 unsigned long c;
1347 bool needwake;
1348
Paul E. McKenneyd1b222c2019-07-02 16:03:33 -07001349 rcu_lockdep_assert_cblist_protected(rdp);
Paul E. McKenneyc6e09b92018-07-03 17:22:34 -07001350 c = rcu_seq_snap(&rcu_state.gp_seq);
Paul E. McKenneya5b89502020-01-07 15:48:39 -08001351 if (!READ_ONCE(rdp->gpwrap) && ULONG_CMP_GE(rdp->gp_seq_needed, c)) {
Paul E. McKenneye44e73c2018-05-01 16:29:47 -07001352 /* Old request still live, so mark recent callbacks. */
1353 (void)rcu_segcblist_accelerate(&rdp->cblist, c);
1354 return;
1355 }
1356 raw_spin_lock_rcu_node(rnp); /* irqs already disabled. */
Paul E. McKenney02f50142018-07-03 17:22:34 -07001357 needwake = rcu_accelerate_cbs(rnp, rdp);
Paul E. McKenneye44e73c2018-05-01 16:29:47 -07001358 raw_spin_unlock_rcu_node(rnp); /* irqs remain disabled. */
1359 if (needwake)
Paul E. McKenney532c00c2018-07-03 17:22:34 -07001360 rcu_gp_kthread_wake();
Paul E. McKenneye44e73c2018-05-01 16:29:47 -07001361}
1362
1363/*
Paul E. McKenneydc35c892012-12-03 13:52:00 -08001364 * Move any callbacks whose grace period has completed to the
1365 * RCU_DONE_TAIL sublist, then compact the remaining sublists and
Paul E. McKenney29365e52018-04-30 10:57:36 -07001366 * assign ->gp_seq numbers to any callbacks in the RCU_NEXT_TAIL
Paul E. McKenneydc35c892012-12-03 13:52:00 -08001367 * sublist. This function is idempotent, so it does not hurt to
1368 * invoke it repeatedly. As long as it is not invoked -too- often...
Paul E. McKenney48a76392014-03-11 13:02:16 -07001369 * Returns true if the RCU grace-period kthread needs to be awakened.
Paul E. McKenneydc35c892012-12-03 13:52:00 -08001370 *
1371 * The caller must hold rnp->lock with interrupts disabled.
1372 */
Paul E. McKenney834f56b2018-07-03 17:22:34 -07001373static bool rcu_advance_cbs(struct rcu_node *rnp, struct rcu_data *rdp)
Paul E. McKenneydc35c892012-12-03 13:52:00 -08001374{
Paul E. McKenneyd1b222c2019-07-02 16:03:33 -07001375 rcu_lockdep_assert_cblist_protected(rdp);
Matthew Wilcoxa32e01e2018-01-17 06:24:30 -08001376 raw_lockdep_assert_held_rcu_node(rnp);
Paul E. McKenneyc0b334c2017-04-28 12:32:15 -07001377
Paul E. McKenney15fecf82017-02-08 12:36:42 -08001378 /* If no pending (not yet ready to invoke) callbacks, nothing to do. */
1379 if (!rcu_segcblist_pend_cbs(&rdp->cblist))
Paul E. McKenney48a76392014-03-11 13:02:16 -07001380 return false;
Paul E. McKenneydc35c892012-12-03 13:52:00 -08001381
1382 /*
Paul E. McKenney29365e52018-04-30 10:57:36 -07001383 * Find all callbacks whose ->gp_seq numbers indicate that they
Paul E. McKenneydc35c892012-12-03 13:52:00 -08001384 * are ready to invoke, and put them into the RCU_DONE_TAIL sublist.
1385 */
Paul E. McKenney29365e52018-04-30 10:57:36 -07001386 rcu_segcblist_advance(&rdp->cblist, rnp->gp_seq);
Paul E. McKenneydc35c892012-12-03 13:52:00 -08001387
1388 /* Classify any remaining callbacks. */
Paul E. McKenney02f50142018-07-03 17:22:34 -07001389 return rcu_accelerate_cbs(rnp, rdp);
Paul E. McKenneydc35c892012-12-03 13:52:00 -08001390}
1391
1392/*
Paul E. McKenney7f36ef82019-05-28 05:54:26 -07001393 * Move and classify callbacks, but only if doing so won't require
1394 * that the RCU grace-period kthread be awakened.
1395 */
1396static void __maybe_unused rcu_advance_cbs_nowake(struct rcu_node *rnp,
1397 struct rcu_data *rdp)
1398{
Paul E. McKenneyd1b222c2019-07-02 16:03:33 -07001399 rcu_lockdep_assert_cblist_protected(rdp);
Paul E. McKenney6608c3a2019-06-01 06:16:38 -07001400 if (!rcu_seq_state(rcu_seq_current(&rnp->gp_seq)) ||
1401 !raw_spin_trylock_rcu_node(rnp))
Paul E. McKenney7f36ef82019-05-28 05:54:26 -07001402 return;
1403 WARN_ON_ONCE(rcu_advance_cbs(rnp, rdp));
Paul E. McKenney6608c3a2019-06-01 06:16:38 -07001404 raw_spin_unlock_rcu_node(rnp);
Paul E. McKenney7f36ef82019-05-28 05:54:26 -07001405}
1406
1407/*
Paul E. McKenneyba9fbe92013-03-19 11:53:31 -07001408 * Update CPU-local rcu_data state to record the beginnings and ends of
1409 * grace periods. The caller must hold the ->lock of the leaf rcu_node
1410 * structure corresponding to the current CPU, and must have irqs disabled.
Paul E. McKenney48a76392014-03-11 13:02:16 -07001411 * Returns true if the grace-period kthread needs to be awakened.
Paul E. McKenneyd09b62d2009-11-02 13:52:28 -08001412 */
Paul E. McKenneyc7e48f72018-07-03 17:22:34 -07001413static bool __note_gp_changes(struct rcu_node *rnp, struct rcu_data *rdp)
Paul E. McKenneyd09b62d2009-11-02 13:52:28 -08001414{
Paul E. McKenney5d6742b2019-05-15 09:56:40 -07001415 bool ret = false;
Paul E. McKenneyb5ea0372019-12-09 15:19:45 -08001416 bool need_qs;
Paul E. McKenney5d6742b2019-05-15 09:56:40 -07001417 const bool offloaded = IS_ENABLED(CONFIG_RCU_NOCB_CPU) &&
1418 rcu_segcblist_is_offloaded(&rdp->cblist);
Paul E. McKenney48a76392014-03-11 13:02:16 -07001419
Matthew Wilcoxa32e01e2018-01-17 06:24:30 -08001420 raw_lockdep_assert_held_rcu_node(rnp);
Paul E. McKenneyc0b334c2017-04-28 12:32:15 -07001421
Paul E. McKenney67e14c12018-04-27 16:01:46 -07001422 if (rdp->gp_seq == rnp->gp_seq)
1423 return false; /* Nothing to do. */
1424
Paul E. McKenneyba9fbe92013-03-19 11:53:31 -07001425 /* Handle the ends of any preceding grace periods first. */
Paul E. McKenney67e14c12018-04-27 16:01:46 -07001426 if (rcu_seq_completed_gp(rdp->gp_seq, rnp->gp_seq) ||
1427 unlikely(READ_ONCE(rdp->gpwrap))) {
Paul E. McKenney5d6742b2019-05-15 09:56:40 -07001428 if (!offloaded)
1429 ret = rcu_advance_cbs(rnp, rdp); /* Advance CBs. */
Paul E. McKenneyb5ea0372019-12-09 15:19:45 -08001430 rdp->core_needs_qs = false;
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001431 trace_rcu_grace_period(rcu_state.name, rdp->gp_seq, TPS("cpuend"));
Paul E. McKenney67e14c12018-04-27 16:01:46 -07001432 } else {
Paul E. McKenney5d6742b2019-05-15 09:56:40 -07001433 if (!offloaded)
1434 ret = rcu_accelerate_cbs(rnp, rdp); /* Recent CBs. */
Paul E. McKenneyb5ea0372019-12-09 15:19:45 -08001435 if (rdp->core_needs_qs)
1436 rdp->core_needs_qs = !!(rnp->qsmask & rdp->grpmask);
Paul E. McKenneyd09b62d2009-11-02 13:52:28 -08001437 }
Paul E. McKenney398ebe62013-03-19 10:53:14 -07001438
Paul E. McKenney67e14c12018-04-27 16:01:46 -07001439 /* Now handle the beginnings of any new-to-this-CPU grace periods. */
1440 if (rcu_seq_new_gp(rdp->gp_seq, rnp->gp_seq) ||
1441 unlikely(READ_ONCE(rdp->gpwrap))) {
Paul E. McKenney6eaef632013-03-19 10:08:37 -07001442 /*
1443 * If the current grace period is waiting for this CPU,
1444 * set up to detect a quiescent state, otherwise don't
1445 * go looking for one.
1446 */
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001447 trace_rcu_grace_period(rcu_state.name, rnp->gp_seq, TPS("cpustart"));
Paul E. McKenneyb5ea0372019-12-09 15:19:45 -08001448 need_qs = !!(rnp->qsmask & rdp->grpmask);
1449 rdp->cpu_no_qs.b.norm = need_qs;
1450 rdp->core_needs_qs = need_qs;
Paul E. McKenney6eaef632013-03-19 10:08:37 -07001451 zero_cpu_stall_ticks(rdp);
1452 }
Paul E. McKenney67e14c12018-04-27 16:01:46 -07001453 rdp->gp_seq = rnp->gp_seq; /* Remember new grace-period state. */
Zhang, Jun13dc7d02018-12-19 10:37:34 -08001454 if (ULONG_CMP_LT(rdp->gp_seq_needed, rnp->gp_seq_needed) || rdp->gpwrap)
Paul E. McKenney8ff372902020-01-04 11:33:17 -08001455 WRITE_ONCE(rdp->gp_seq_needed, rnp->gp_seq_needed);
Paul E. McKenney3d184692018-05-15 16:47:30 -07001456 WRITE_ONCE(rdp->gpwrap, false);
1457 rcu_gpnum_ovf(rnp, rdp);
Paul E. McKenney48a76392014-03-11 13:02:16 -07001458 return ret;
Paul E. McKenney6eaef632013-03-19 10:08:37 -07001459}
1460
Paul E. McKenney15cabdf2018-07-03 17:22:34 -07001461static void note_gp_changes(struct rcu_data *rdp)
Paul E. McKenney6eaef632013-03-19 10:08:37 -07001462{
1463 unsigned long flags;
Paul E. McKenney48a76392014-03-11 13:02:16 -07001464 bool needwake;
Paul E. McKenney6eaef632013-03-19 10:08:37 -07001465 struct rcu_node *rnp;
1466
1467 local_irq_save(flags);
1468 rnp = rdp->mynode;
Paul E. McKenney67e14c12018-04-27 16:01:46 -07001469 if ((rdp->gp_seq == rcu_seq_current(&rnp->gp_seq) &&
Paul E. McKenney7d0ae802015-03-03 14:57:58 -08001470 !unlikely(READ_ONCE(rdp->gpwrap))) || /* w/out lock. */
Peter Zijlstra2a67e742015-10-08 12:24:23 +02001471 !raw_spin_trylock_rcu_node(rnp)) { /* irqs already off, so later. */
Paul E. McKenney6eaef632013-03-19 10:08:37 -07001472 local_irq_restore(flags);
1473 return;
1474 }
Paul E. McKenneyc7e48f72018-07-03 17:22:34 -07001475 needwake = __note_gp_changes(rnp, rdp);
Boqun Feng67c583a72015-12-29 12:18:47 +08001476 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
Paul E. McKenney48a76392014-03-11 13:02:16 -07001477 if (needwake)
Paul E. McKenney532c00c2018-07-03 17:22:34 -07001478 rcu_gp_kthread_wake();
Paul E. McKenney6eaef632013-03-19 10:08:37 -07001479}
1480
Paul E. McKenney22212332018-07-03 17:22:34 -07001481static void rcu_gp_slow(int delay)
Paul E. McKenney0f41c0d2015-03-10 18:33:20 -07001482{
1483 if (delay > 0 &&
Paul E. McKenney22212332018-07-03 17:22:34 -07001484 !(rcu_seq_ctr(rcu_state.gp_seq) %
Paul E. McKenneydee4f422018-04-26 15:30:28 -07001485 (rcu_num_nodes * PER_RCU_NODE_PERIOD * delay)))
Paul E. McKenney0f41c0d2015-03-10 18:33:20 -07001486 schedule_timeout_uninterruptible(delay);
1487}
1488
Paul E. McKenney6eaef632013-03-19 10:08:37 -07001489/*
Paul E. McKenney45fed3e2015-11-07 23:35:00 -08001490 * Initialize a new grace period. Return false if no grace period required.
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001491 */
Paul E. McKenney0854a05c2018-07-03 17:22:34 -07001492static bool rcu_gp_init(void)
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001493{
Paul E. McKenneyec2c2972018-05-07 09:34:17 -07001494 unsigned long flags;
Paul E. McKenney0aa04b02015-01-23 21:52:37 -08001495 unsigned long oldmask;
Paul E. McKenneyec2c2972018-05-07 09:34:17 -07001496 unsigned long mask;
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001497 struct rcu_data *rdp;
Paul E. McKenney336a4f62018-07-03 17:22:34 -07001498 struct rcu_node *rnp = rcu_get_root();
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001499
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001500 WRITE_ONCE(rcu_state.gp_activity, jiffies);
Peter Zijlstra2a67e742015-10-08 12:24:23 +02001501 raw_spin_lock_irq_rcu_node(rnp);
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001502 if (!READ_ONCE(rcu_state.gp_flags)) {
Paul E. McKenneyf7be8202013-08-08 18:27:52 -07001503 /* Spurious wakeup, tell caller to go back to sleep. */
Boqun Feng67c583a72015-12-29 12:18:47 +08001504 raw_spin_unlock_irq_rcu_node(rnp);
Paul E. McKenney45fed3e2015-11-07 23:35:00 -08001505 return false;
Paul E. McKenneyf7be8202013-08-08 18:27:52 -07001506 }
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001507 WRITE_ONCE(rcu_state.gp_flags, 0); /* Clear all flags: New GP. */
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001508
Paul E. McKenneyde8e8732018-07-03 17:22:34 -07001509 if (WARN_ON_ONCE(rcu_gp_in_progress())) {
Paul E. McKenneyf7be8202013-08-08 18:27:52 -07001510 /*
1511 * Grace period already in progress, don't start another.
1512 * Not supposed to be able to happen.
1513 */
Boqun Feng67c583a72015-12-29 12:18:47 +08001514 raw_spin_unlock_irq_rcu_node(rnp);
Paul E. McKenney45fed3e2015-11-07 23:35:00 -08001515 return false;
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001516 }
1517
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001518 /* Advance to a new grace period and initialize state. */
Paul E. McKenneyad3832e2018-07-03 17:22:34 -07001519 record_gp_stall_check_time();
Paul E. McKenneyff3bb6f2018-05-01 14:34:08 -07001520 /* Record GP times before starting GP, hence rcu_seq_start(). */
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001521 rcu_seq_start(&rcu_state.gp_seq);
1522 trace_rcu_grace_period(rcu_state.name, rcu_state.gp_seq, TPS("start"));
Boqun Feng67c583a72015-12-29 12:18:47 +08001523 raw_spin_unlock_irq_rcu_node(rnp);
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001524
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001525 /*
Paul E. McKenney0aa04b02015-01-23 21:52:37 -08001526 * Apply per-leaf buffered online and offline operations to the
1527 * rcu_node tree. Note that this new grace period need not wait
1528 * for subsequent online CPUs, and that quiescent-state forcing
1529 * will handle subsequent offline CPUs.
1530 */
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001531 rcu_state.gp_state = RCU_GP_ONOFF;
Paul E. McKenneyaedf4ba2018-07-04 14:33:59 -07001532 rcu_for_each_leaf_node(rnp) {
Mike Galbraith894d45b2018-08-15 09:05:29 -07001533 raw_spin_lock(&rcu_state.ofl_lock);
Peter Zijlstra2a67e742015-10-08 12:24:23 +02001534 raw_spin_lock_irq_rcu_node(rnp);
Paul E. McKenney0aa04b02015-01-23 21:52:37 -08001535 if (rnp->qsmaskinit == rnp->qsmaskinitnext &&
1536 !rnp->wait_blkd_tasks) {
1537 /* Nothing to do on this leaf rcu_node structure. */
Boqun Feng67c583a72015-12-29 12:18:47 +08001538 raw_spin_unlock_irq_rcu_node(rnp);
Mike Galbraith894d45b2018-08-15 09:05:29 -07001539 raw_spin_unlock(&rcu_state.ofl_lock);
Paul E. McKenney0aa04b02015-01-23 21:52:37 -08001540 continue;
1541 }
1542
1543 /* Record old state, apply changes to ->qsmaskinit field. */
1544 oldmask = rnp->qsmaskinit;
1545 rnp->qsmaskinit = rnp->qsmaskinitnext;
1546
1547 /* If zero-ness of ->qsmaskinit changed, propagate up tree. */
1548 if (!oldmask != !rnp->qsmaskinit) {
Paul E. McKenney962aff02018-05-02 12:49:21 -07001549 if (!oldmask) { /* First online CPU for rcu_node. */
1550 if (!rnp->wait_blkd_tasks) /* Ever offline? */
1551 rcu_init_new_rnp(rnp);
1552 } else if (rcu_preempt_has_tasks(rnp)) {
1553 rnp->wait_blkd_tasks = true; /* blocked tasks */
1554 } else { /* Last offline CPU and can propagate. */
Paul E. McKenney0aa04b02015-01-23 21:52:37 -08001555 rcu_cleanup_dead_rnp(rnp);
Paul E. McKenney962aff02018-05-02 12:49:21 -07001556 }
Paul E. McKenney0aa04b02015-01-23 21:52:37 -08001557 }
1558
1559 /*
1560 * If all waited-on tasks from prior grace period are
1561 * done, and if all this rcu_node structure's CPUs are
1562 * still offline, propagate up the rcu_node tree and
1563 * clear ->wait_blkd_tasks. Otherwise, if one of this
1564 * rcu_node structure's CPUs has since come back online,
Paul E. McKenney962aff02018-05-02 12:49:21 -07001565 * simply clear ->wait_blkd_tasks.
Paul E. McKenney0aa04b02015-01-23 21:52:37 -08001566 */
1567 if (rnp->wait_blkd_tasks &&
Paul E. McKenney962aff02018-05-02 12:49:21 -07001568 (!rcu_preempt_has_tasks(rnp) || rnp->qsmaskinit)) {
Paul E. McKenney0aa04b02015-01-23 21:52:37 -08001569 rnp->wait_blkd_tasks = false;
Paul E. McKenney962aff02018-05-02 12:49:21 -07001570 if (!rnp->qsmaskinit)
1571 rcu_cleanup_dead_rnp(rnp);
Paul E. McKenney0aa04b02015-01-23 21:52:37 -08001572 }
1573
Boqun Feng67c583a72015-12-29 12:18:47 +08001574 raw_spin_unlock_irq_rcu_node(rnp);
Mike Galbraith894d45b2018-08-15 09:05:29 -07001575 raw_spin_unlock(&rcu_state.ofl_lock);
Paul E. McKenney0aa04b02015-01-23 21:52:37 -08001576 }
Paul E. McKenney22212332018-07-03 17:22:34 -07001577 rcu_gp_slow(gp_preinit_delay); /* Races with CPU hotplug. */
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001578
1579 /*
1580 * Set the quiescent-state-needed bits in all the rcu_node
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001581 * structures for all currently online CPUs in breadth-first
1582 * order, starting from the root rcu_node structure, relying on the
1583 * layout of the tree within the rcu_state.node[] array. Note that
1584 * other CPUs will access only the leaves of the hierarchy, thus
1585 * seeing that no grace period is in progress, at least until the
1586 * corresponding leaf node has been initialized.
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001587 *
1588 * The grace period cannot complete until the initialization
1589 * process finishes, because this kthread handles both.
1590 */
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001591 rcu_state.gp_state = RCU_GP_INIT;
Paul E. McKenneyaedf4ba2018-07-04 14:33:59 -07001592 rcu_for_each_node_breadth_first(rnp) {
Paul E. McKenney22212332018-07-03 17:22:34 -07001593 rcu_gp_slow(gp_init_delay);
Paul E. McKenneyec2c2972018-05-07 09:34:17 -07001594 raw_spin_lock_irqsave_rcu_node(rnp, flags);
Paul E. McKenneyda1df502018-07-03 15:37:16 -07001595 rdp = this_cpu_ptr(&rcu_data);
Paul E. McKenney81ab59a2018-07-03 17:22:34 -07001596 rcu_preempt_check_blocked_tasks(rnp);
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001597 rnp->qsmask = rnp->qsmaskinit;
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001598 WRITE_ONCE(rnp->gp_seq, rcu_state.gp_seq);
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001599 if (rnp == rdp->mynode)
Paul E. McKenneyc7e48f72018-07-03 17:22:34 -07001600 (void)__note_gp_changes(rnp, rdp);
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001601 rcu_preempt_boost_start_gp(rnp);
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001602 trace_rcu_grace_period_init(rcu_state.name, rnp->gp_seq,
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001603 rnp->level, rnp->grplo,
1604 rnp->grphi, rnp->qsmask);
Paul E. McKenneyec2c2972018-05-07 09:34:17 -07001605 /* Quiescent states for tasks on any now-offline CPUs. */
1606 mask = rnp->qsmask & ~rnp->qsmaskinitnext;
Paul E. McKenneyf2e2df52018-05-15 16:23:23 -07001607 rnp->rcu_gp_init_mask = mask;
Paul E. McKenneyec2c2972018-05-07 09:34:17 -07001608 if ((mask || rnp->wait_blkd_tasks) && rcu_is_leaf_node(rnp))
Paul E. McKenneyb50912d2018-07-03 17:22:34 -07001609 rcu_report_qs_rnp(mask, rnp, rnp->gp_seq, flags);
Paul E. McKenneyec2c2972018-05-07 09:34:17 -07001610 else
1611 raw_spin_unlock_irq_rcu_node(rnp);
Paul E. McKenneycee43932018-03-02 16:35:27 -08001612 cond_resched_tasks_rcu_qs();
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001613 WRITE_ONCE(rcu_state.gp_activity, jiffies);
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001614 }
1615
Paul E. McKenney45fed3e2015-11-07 23:35:00 -08001616 return true;
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001617}
1618
1619/*
Peter Zijlstrab3dae102018-06-12 10:34:52 +02001620 * Helper function for swait_event_idle_exclusive() wakeup at force-quiescent-state
Luis R. Rodriguezd5374222017-06-20 14:45:47 -07001621 * time.
Paul E. McKenneyb9a425c2015-07-01 13:50:28 -07001622 */
Paul E. McKenney0854a05c2018-07-03 17:22:34 -07001623static bool rcu_gp_fqs_check_wake(int *gfp)
Paul E. McKenneyb9a425c2015-07-01 13:50:28 -07001624{
Paul E. McKenney336a4f62018-07-03 17:22:34 -07001625 struct rcu_node *rnp = rcu_get_root();
Paul E. McKenneyb9a425c2015-07-01 13:50:28 -07001626
1627 /* Someone like call_rcu() requested a force-quiescent-state scan. */
Paul E. McKenney0854a05c2018-07-03 17:22:34 -07001628 *gfp = READ_ONCE(rcu_state.gp_flags);
Paul E. McKenneyb9a425c2015-07-01 13:50:28 -07001629 if (*gfp & RCU_GP_FLAG_FQS)
1630 return true;
1631
1632 /* The current grace period has completed. */
1633 if (!READ_ONCE(rnp->qsmask) && !rcu_preempt_blocked_readers_cgp(rnp))
1634 return true;
1635
1636 return false;
1637}
1638
1639/*
Paul E. McKenney4cdfc1752012-06-22 17:06:26 -07001640 * Do one round of quiescent-state forcing.
1641 */
Paul E. McKenney0854a05c2018-07-03 17:22:34 -07001642static void rcu_gp_fqs(bool first_time)
Paul E. McKenney4cdfc1752012-06-22 17:06:26 -07001643{
Paul E. McKenney336a4f62018-07-03 17:22:34 -07001644 struct rcu_node *rnp = rcu_get_root();
Paul E. McKenney4cdfc1752012-06-22 17:06:26 -07001645
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001646 WRITE_ONCE(rcu_state.gp_activity, jiffies);
1647 rcu_state.n_force_qs++;
Petr Mladek77f81fe2015-09-09 12:09:49 -07001648 if (first_time) {
Paul E. McKenney4cdfc1752012-06-22 17:06:26 -07001649 /* Collect dyntick-idle snapshots. */
Paul E. McKenneye9ecb782018-07-03 17:22:34 -07001650 force_qs_rnp(dyntick_save_progress_counter);
Paul E. McKenney4cdfc1752012-06-22 17:06:26 -07001651 } else {
1652 /* Handle dyntick-idle and offline CPUs. */
Paul E. McKenneye9ecb782018-07-03 17:22:34 -07001653 force_qs_rnp(rcu_implicit_dynticks_qs);
Paul E. McKenney4cdfc1752012-06-22 17:06:26 -07001654 }
1655 /* Clear flag to prevent immediate re-entry. */
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001656 if (READ_ONCE(rcu_state.gp_flags) & RCU_GP_FLAG_FQS) {
Peter Zijlstra2a67e742015-10-08 12:24:23 +02001657 raw_spin_lock_irq_rcu_node(rnp);
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001658 WRITE_ONCE(rcu_state.gp_flags,
1659 READ_ONCE(rcu_state.gp_flags) & ~RCU_GP_FLAG_FQS);
Boqun Feng67c583a72015-12-29 12:18:47 +08001660 raw_spin_unlock_irq_rcu_node(rnp);
Paul E. McKenney4cdfc1752012-06-22 17:06:26 -07001661 }
Paul E. McKenney4cdfc1752012-06-22 17:06:26 -07001662}
1663
1664/*
Paul E. McKenneyc3854a02018-07-05 18:23:23 -07001665 * Loop doing repeated quiescent-state forcing until the grace period ends.
1666 */
1667static void rcu_gp_fqs_loop(void)
1668{
1669 bool first_gp_fqs;
1670 int gf;
1671 unsigned long j;
1672 int ret;
1673 struct rcu_node *rnp = rcu_get_root();
1674
1675 first_gp_fqs = true;
Paul E. McKenneyc06aed02018-07-25 11:25:23 -07001676 j = READ_ONCE(jiffies_till_first_fqs);
Paul E. McKenneyc3854a02018-07-05 18:23:23 -07001677 ret = 0;
1678 for (;;) {
1679 if (!ret) {
1680 rcu_state.jiffies_force_qs = jiffies + j;
1681 WRITE_ONCE(rcu_state.jiffies_kick_kthreads,
Paul E. McKenney9cf422a2018-11-20 10:43:34 -08001682 jiffies + (j ? 3 * j : 2));
Paul E. McKenneyc3854a02018-07-05 18:23:23 -07001683 }
Paul E. McKenney0f11ad32020-02-10 09:58:37 -08001684 trace_rcu_grace_period(rcu_state.name, rcu_state.gp_seq,
Paul E. McKenneyc3854a02018-07-05 18:23:23 -07001685 TPS("fqswait"));
1686 rcu_state.gp_state = RCU_GP_WAIT_FQS;
1687 ret = swait_event_idle_timeout_exclusive(
1688 rcu_state.gp_wq, rcu_gp_fqs_check_wake(&gf), j);
1689 rcu_state.gp_state = RCU_GP_DOING_FQS;
1690 /* Locking provides needed memory barriers. */
1691 /* If grace period done, leave loop. */
1692 if (!READ_ONCE(rnp->qsmask) &&
1693 !rcu_preempt_blocked_readers_cgp(rnp))
1694 break;
1695 /* If time for quiescent-state forcing, do it. */
1696 if (ULONG_CMP_GE(jiffies, rcu_state.jiffies_force_qs) ||
1697 (gf & RCU_GP_FLAG_FQS)) {
Paul E. McKenney0f11ad32020-02-10 09:58:37 -08001698 trace_rcu_grace_period(rcu_state.name, rcu_state.gp_seq,
Paul E. McKenneyc3854a02018-07-05 18:23:23 -07001699 TPS("fqsstart"));
1700 rcu_gp_fqs(first_gp_fqs);
1701 first_gp_fqs = false;
Paul E. McKenney0f11ad32020-02-10 09:58:37 -08001702 trace_rcu_grace_period(rcu_state.name, rcu_state.gp_seq,
Paul E. McKenneyc3854a02018-07-05 18:23:23 -07001703 TPS("fqsend"));
1704 cond_resched_tasks_rcu_qs();
1705 WRITE_ONCE(rcu_state.gp_activity, jiffies);
1706 ret = 0; /* Force full wait till next FQS. */
Paul E. McKenneyc06aed02018-07-25 11:25:23 -07001707 j = READ_ONCE(jiffies_till_next_fqs);
Paul E. McKenneyc3854a02018-07-05 18:23:23 -07001708 } else {
1709 /* Deal with stray signal. */
1710 cond_resched_tasks_rcu_qs();
1711 WRITE_ONCE(rcu_state.gp_activity, jiffies);
1712 WARN_ON(signal_pending(current));
Paul E. McKenney0f11ad32020-02-10 09:58:37 -08001713 trace_rcu_grace_period(rcu_state.name, rcu_state.gp_seq,
Paul E. McKenneyc3854a02018-07-05 18:23:23 -07001714 TPS("fqswaitsig"));
1715 ret = 1; /* Keep old FQS timing. */
1716 j = jiffies;
1717 if (time_after(jiffies, rcu_state.jiffies_force_qs))
1718 j = 1;
1719 else
1720 j = rcu_state.jiffies_force_qs - j;
1721 }
1722 }
1723}
1724
1725/*
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001726 * Clean up after the old grace period.
1727 */
Paul E. McKenney0854a05c2018-07-03 17:22:34 -07001728static void rcu_gp_cleanup(void)
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001729{
Paul E. McKenneyb2b00dd2019-10-30 11:56:10 -07001730 int cpu;
Paul E. McKenney48a76392014-03-11 13:02:16 -07001731 bool needgp = false;
Paul E. McKenneyb2b00dd2019-10-30 11:56:10 -07001732 unsigned long gp_duration;
Paul E. McKenneyde30ad52018-04-26 11:52:09 -07001733 unsigned long new_gp_seq;
Paul E. McKenney5d6742b2019-05-15 09:56:40 -07001734 bool offloaded;
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001735 struct rcu_data *rdp;
Paul E. McKenney336a4f62018-07-03 17:22:34 -07001736 struct rcu_node *rnp = rcu_get_root();
Paul Gortmakerabedf8e2016-02-19 09:46:41 +01001737 struct swait_queue_head *sq;
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001738
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001739 WRITE_ONCE(rcu_state.gp_activity, jiffies);
Peter Zijlstra2a67e742015-10-08 12:24:23 +02001740 raw_spin_lock_irq_rcu_node(rnp);
Paul E. McKenneyc51d7b52018-10-03 17:25:33 -07001741 rcu_state.gp_end = jiffies;
1742 gp_duration = rcu_state.gp_end - rcu_state.gp_start;
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001743 if (gp_duration > rcu_state.gp_max)
1744 rcu_state.gp_max = gp_duration;
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001745
1746 /*
1747 * We know the grace period is complete, but to everyone else
1748 * it appears to still be ongoing. But it is also the case
1749 * that to everyone else it looks like there is nothing that
1750 * they can do to advance the grace period. It is therefore
1751 * safe for us to drop the lock in order to mark the grace
1752 * period as completed in all of the rcu_node structures.
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001753 */
Boqun Feng67c583a72015-12-29 12:18:47 +08001754 raw_spin_unlock_irq_rcu_node(rnp);
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001755
Paul E. McKenney5d4b8652012-07-07 07:56:57 -07001756 /*
Paul E. McKenneyff3bb6f2018-05-01 14:34:08 -07001757 * Propagate new ->gp_seq value to rcu_node structures so that
1758 * other CPUs don't have to wait until the start of the next grace
1759 * period to process their callbacks. This also avoids some nasty
1760 * RCU grace-period initialization races by forcing the end of
1761 * the current grace period to be completely recorded in all of
1762 * the rcu_node structures before the beginning of the next grace
1763 * period is recorded in any of the rcu_node structures.
Paul E. McKenney5d4b8652012-07-07 07:56:57 -07001764 */
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001765 new_gp_seq = rcu_state.gp_seq;
Paul E. McKenneyde30ad52018-04-26 11:52:09 -07001766 rcu_seq_end(&new_gp_seq);
Paul E. McKenneyaedf4ba2018-07-04 14:33:59 -07001767 rcu_for_each_node_breadth_first(rnp) {
Peter Zijlstra2a67e742015-10-08 12:24:23 +02001768 raw_spin_lock_irq_rcu_node(rnp);
Paul E. McKenney4bc8d552017-11-27 15:13:56 -08001769 if (WARN_ON_ONCE(rcu_preempt_blocked_readers_cgp(rnp)))
Paul E. McKenney81ab59a2018-07-03 17:22:34 -07001770 dump_blkd_tasks(rnp, 10);
Paul E. McKenney5c60d252015-02-09 05:37:47 -08001771 WARN_ON_ONCE(rnp->qsmask);
Paul E. McKenneyde30ad52018-04-26 11:52:09 -07001772 WRITE_ONCE(rnp->gp_seq, new_gp_seq);
Paul E. McKenneyda1df502018-07-03 15:37:16 -07001773 rdp = this_cpu_ptr(&rcu_data);
Paul E. McKenneyb11cc572012-12-17 14:21:14 -08001774 if (rnp == rdp->mynode)
Paul E. McKenneyc7e48f72018-07-03 17:22:34 -07001775 needgp = __note_gp_changes(rnp, rdp) || needgp;
Paul E. McKenney78e4bc32013-09-24 15:04:06 -07001776 /* smp_mb() provided by prior unlock-lock pair. */
Paul E. McKenney3481f2e2018-07-03 17:22:34 -07001777 needgp = rcu_future_gp_cleanup(rnp) || needgp;
Paul E. McKenneyb2b00dd2019-10-30 11:56:10 -07001778 // Reset overload indication for CPUs no longer overloaded
1779 if (rcu_is_leaf_node(rnp))
1780 for_each_leaf_node_cpu_mask(rnp, cpu, rnp->cbovldmask) {
1781 rdp = per_cpu_ptr(&rcu_data, cpu);
1782 check_cb_ovld_locked(rdp, rnp);
1783 }
Daniel Wagner065bb782016-02-19 09:46:40 +01001784 sq = rcu_nocb_gp_get(rnp);
Boqun Feng67c583a72015-12-29 12:18:47 +08001785 raw_spin_unlock_irq_rcu_node(rnp);
Daniel Wagner065bb782016-02-19 09:46:40 +01001786 rcu_nocb_gp_cleanup(sq);
Paul E. McKenneycee43932018-03-02 16:35:27 -08001787 cond_resched_tasks_rcu_qs();
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001788 WRITE_ONCE(rcu_state.gp_activity, jiffies);
Paul E. McKenney22212332018-07-03 17:22:34 -07001789 rcu_gp_slow(gp_cleanup_delay);
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001790 }
Paul E. McKenney336a4f62018-07-03 17:22:34 -07001791 rnp = rcu_get_root();
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001792 raw_spin_lock_irq_rcu_node(rnp); /* GP before ->gp_seq update. */
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001793
Paul E. McKenney0a89e5a2018-10-15 10:00:58 -07001794 /* Declare grace period done, trace first to use old GP number. */
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001795 trace_rcu_grace_period(rcu_state.name, rcu_state.gp_seq, TPS("end"));
Paul E. McKenney0a89e5a2018-10-15 10:00:58 -07001796 rcu_seq_end(&rcu_state.gp_seq);
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001797 rcu_state.gp_state = RCU_GP_IDLE;
Paul E. McKenneyfb313402018-04-12 07:20:30 -07001798 /* Check for GP requests since above loop. */
Paul E. McKenneyda1df502018-07-03 15:37:16 -07001799 rdp = this_cpu_ptr(&rcu_data);
Joel Fernandes (Google)5b550722018-05-13 20:15:40 -07001800 if (!needgp && ULONG_CMP_LT(rnp->gp_seq, rnp->gp_seq_needed)) {
Paul E. McKenneyabd13fd2018-05-01 13:08:46 -07001801 trace_rcu_this_gp(rnp, rdp, rnp->gp_seq_needed,
Paul E. McKenney41e80592018-04-12 11:24:09 -07001802 TPS("CleanupMore"));
Paul E. McKenneyfb313402018-04-12 07:20:30 -07001803 needgp = true;
1804 }
Paul E. McKenney48a76392014-03-11 13:02:16 -07001805 /* Advance CBs to reduce false positives below. */
Paul E. McKenney5d6742b2019-05-15 09:56:40 -07001806 offloaded = IS_ENABLED(CONFIG_RCU_NOCB_CPU) &&
1807 rcu_segcblist_is_offloaded(&rdp->cblist);
1808 if ((offloaded || !rcu_accelerate_cbs(rnp, rdp)) && needgp) {
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001809 WRITE_ONCE(rcu_state.gp_flags, RCU_GP_FLAG_INIT);
Paul E. McKenney2906d212020-01-03 15:17:12 -08001810 WRITE_ONCE(rcu_state.gp_req_activity, jiffies);
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001811 trace_rcu_grace_period(rcu_state.name,
Paul E. McKenney0f11ad32020-02-10 09:58:37 -08001812 rcu_state.gp_seq,
Paul E. McKenneybb311ec2013-08-09 16:02:09 -07001813 TPS("newreq"));
Paul E. McKenney18390ae2018-04-22 15:06:05 -07001814 } else {
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001815 WRITE_ONCE(rcu_state.gp_flags,
1816 rcu_state.gp_flags & RCU_GP_FLAG_INIT);
Paul E. McKenneybb311ec2013-08-09 16:02:09 -07001817 }
Boqun Feng67c583a72015-12-29 12:18:47 +08001818 raw_spin_unlock_irq_rcu_node(rnp);
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001819}
1820
1821/*
Paul E. McKenneyb3dbec72012-06-18 18:36:08 -07001822 * Body of kthread that handles grace periods.
1823 */
Paul E. McKenney0854a05c2018-07-03 17:22:34 -07001824static int __noreturn rcu_gp_kthread(void *unused)
Paul E. McKenneyb3dbec72012-06-18 18:36:08 -07001825{
Paul E. McKenney58719682015-02-24 11:05:36 -08001826 rcu_bind_gp_kthread();
Paul E. McKenneyb3dbec72012-06-18 18:36:08 -07001827 for (;;) {
1828
1829 /* Handle grace-period start. */
Paul E. McKenneyb3dbec72012-06-18 18:36:08 -07001830 for (;;) {
Paul E. McKenney0f11ad32020-02-10 09:58:37 -08001831 trace_rcu_grace_period(rcu_state.name, rcu_state.gp_seq,
Paul E. McKenney63c4db72013-08-09 12:19:29 -07001832 TPS("reqwait"));
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001833 rcu_state.gp_state = RCU_GP_WAIT_GPS;
1834 swait_event_idle_exclusive(rcu_state.gp_wq,
1835 READ_ONCE(rcu_state.gp_flags) &
1836 RCU_GP_FLAG_INIT);
1837 rcu_state.gp_state = RCU_GP_DONE_GPS;
Paul E. McKenney78e4bc32013-09-24 15:04:06 -07001838 /* Locking provides needed memory barrier. */
Paul E. McKenney0854a05c2018-07-03 17:22:34 -07001839 if (rcu_gp_init())
Paul E. McKenneyb3dbec72012-06-18 18:36:08 -07001840 break;
Paul E. McKenneycee43932018-03-02 16:35:27 -08001841 cond_resched_tasks_rcu_qs();
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001842 WRITE_ONCE(rcu_state.gp_activity, jiffies);
Paul E. McKenney73a860c2014-08-14 10:28:23 -07001843 WARN_ON(signal_pending(current));
Paul E. McKenney0f11ad32020-02-10 09:58:37 -08001844 trace_rcu_grace_period(rcu_state.name, rcu_state.gp_seq,
Paul E. McKenney63c4db72013-08-09 12:19:29 -07001845 TPS("reqwaitsig"));
Paul E. McKenneyb3dbec72012-06-18 18:36:08 -07001846 }
Paul E. McKenneycabc49c2012-06-20 17:07:14 -07001847
Paul E. McKenney4cdfc1752012-06-22 17:06:26 -07001848 /* Handle quiescent-state forcing. */
Paul E. McKenneyc3854a02018-07-05 18:23:23 -07001849 rcu_gp_fqs_loop();
Paul E. McKenney4cdfc1752012-06-22 17:06:26 -07001850
1851 /* Handle grace-period end. */
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001852 rcu_state.gp_state = RCU_GP_CLEANUP;
Paul E. McKenney0854a05c2018-07-03 17:22:34 -07001853 rcu_gp_cleanup();
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001854 rcu_state.gp_state = RCU_GP_CLEANED;
Paul E. McKenneyb3dbec72012-06-18 18:36:08 -07001855 }
Paul E. McKenneyb3dbec72012-06-18 18:36:08 -07001856}
1857
1858/*
Paul E. McKenney49918a52018-07-07 18:12:26 -07001859 * Report a full set of quiescent states to the rcu_state data structure.
1860 * Invoke rcu_gp_kthread_wake() to awaken the grace-period kthread if
1861 * another grace period is required. Whether we wake the grace-period
1862 * kthread or it awakens itself for the next round of quiescent-state
1863 * forcing, that kthread will clean up after the just-completed grace
1864 * period. Note that the caller must hold rnp->lock, which is released
1865 * before return.
Paul E. McKenneyf41d9112009-08-22 13:56:52 -07001866 */
Paul E. McKenneyaff4e9e2018-07-03 17:22:34 -07001867static void rcu_report_qs_rsp(unsigned long flags)
Paul E. McKenney336a4f62018-07-03 17:22:34 -07001868 __releases(rcu_get_root()->lock)
Paul E. McKenneyf41d9112009-08-22 13:56:52 -07001869{
Paul E. McKenney336a4f62018-07-03 17:22:34 -07001870 raw_lockdep_assert_held_rcu_node(rcu_get_root());
Paul E. McKenneyde8e8732018-07-03 17:22:34 -07001871 WARN_ON_ONCE(!rcu_gp_in_progress());
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001872 WRITE_ONCE(rcu_state.gp_flags,
1873 READ_ONCE(rcu_state.gp_flags) | RCU_GP_FLAG_FQS);
Paul E. McKenney336a4f62018-07-03 17:22:34 -07001874 raw_spin_unlock_irqrestore_rcu_node(rcu_get_root(), flags);
Paul E. McKenney532c00c2018-07-03 17:22:34 -07001875 rcu_gp_kthread_wake();
Paul E. McKenneyf41d9112009-08-22 13:56:52 -07001876}
1877
1878/*
Paul E. McKenneyd3f6bad2009-12-02 12:10:13 -08001879 * Similar to rcu_report_qs_rdp(), for which it is a helper function.
1880 * Allows quiescent states for a group of CPUs to be reported at one go
1881 * to the specified rcu_node structure, though all the CPUs in the group
Paul E. McKenney654e9532015-03-15 09:19:35 -07001882 * must be represented by the same rcu_node structure (which need not be a
1883 * leaf rcu_node structure, though it often will be). The gps parameter
1884 * is the grace-period snapshot, which means that the quiescent states
Paul E. McKenneyc9a24e22018-04-27 14:54:46 -07001885 * are valid only if rnp->gp_seq is equal to gps. That structure's lock
Paul E. McKenney654e9532015-03-15 09:19:35 -07001886 * must be held upon entry, and it is released before return.
Paul E. McKenneyec2c2972018-05-07 09:34:17 -07001887 *
1888 * As a special case, if mask is zero, the bit-already-cleared check is
1889 * disabled. This allows propagating quiescent state due to resumed tasks
1890 * during grace-period initialization.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001891 */
Paul E. McKenneyb50912d2018-07-03 17:22:34 -07001892static void rcu_report_qs_rnp(unsigned long mask, struct rcu_node *rnp,
1893 unsigned long gps, unsigned long flags)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001894 __releases(rnp->lock)
1895{
Paul E. McKenney654e9532015-03-15 09:19:35 -07001896 unsigned long oldmask = 0;
Paul E. McKenney28ecd582009-09-18 09:50:17 -07001897 struct rcu_node *rnp_c;
1898
Matthew Wilcoxa32e01e2018-01-17 06:24:30 -08001899 raw_lockdep_assert_held_rcu_node(rnp);
Paul E. McKenneyc0b334c2017-04-28 12:32:15 -07001900
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001901 /* Walk up the rcu_node hierarchy. */
1902 for (;;) {
Paul E. McKenneyec2c2972018-05-07 09:34:17 -07001903 if ((!(rnp->qsmask & mask) && mask) || rnp->gp_seq != gps) {
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001904
Paul E. McKenney654e9532015-03-15 09:19:35 -07001905 /*
1906 * Our bit has already been cleared, or the
1907 * relevant grace period is already over, so done.
1908 */
Boqun Feng67c583a72015-12-29 12:18:47 +08001909 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001910 return;
1911 }
Paul E. McKenney654e9532015-03-15 09:19:35 -07001912 WARN_ON_ONCE(oldmask); /* Any child must be all zeroed! */
Paul E. McKenney5b4c11d2018-04-13 17:11:44 -07001913 WARN_ON_ONCE(!rcu_is_leaf_node(rnp) &&
Paul E. McKenney2dee9402017-07-11 21:52:31 -07001914 rcu_preempt_blocked_readers_cgp(rnp));
Paul E. McKenney7672d642020-01-03 11:38:51 -08001915 WRITE_ONCE(rnp->qsmask, rnp->qsmask & ~mask);
Paul E. McKenney67a0edb2018-07-05 16:15:38 -07001916 trace_rcu_quiescent_state_report(rcu_state.name, rnp->gp_seq,
Paul E. McKenneyd4c08f22011-06-25 06:36:56 -07001917 mask, rnp->qsmask, rnp->level,
1918 rnp->grplo, rnp->grphi,
1919 !!rnp->gp_tasks);
Paul E. McKenney27f4d282011-02-07 12:47:15 -08001920 if (rnp->qsmask != 0 || rcu_preempt_blocked_readers_cgp(rnp)) {
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001921
1922 /* Other bits still set at this level, so done. */
Boqun Feng67c583a72015-12-29 12:18:47 +08001923 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001924 return;
1925 }
Paul E. McKenneyd43a5d32018-04-28 18:50:06 -07001926 rnp->completedqs = rnp->gp_seq;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001927 mask = rnp->grpmask;
1928 if (rnp->parent == NULL) {
1929
1930 /* No more levels. Exit loop holding root lock. */
1931
1932 break;
1933 }
Boqun Feng67c583a72015-12-29 12:18:47 +08001934 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
Paul E. McKenney28ecd582009-09-18 09:50:17 -07001935 rnp_c = rnp;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001936 rnp = rnp->parent;
Peter Zijlstra2a67e742015-10-08 12:24:23 +02001937 raw_spin_lock_irqsave_rcu_node(rnp, flags);
Paul E. McKenney0937d042020-01-03 14:53:31 -08001938 oldmask = READ_ONCE(rnp_c->qsmask);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001939 }
1940
1941 /*
1942 * Get here if we are the last CPU to pass through a quiescent
Paul E. McKenneyd3f6bad2009-12-02 12:10:13 -08001943 * state for this grace period. Invoke rcu_report_qs_rsp()
Paul E. McKenneyf41d9112009-08-22 13:56:52 -07001944 * to clean up and start the next grace period if one is needed.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001945 */
Paul E. McKenneyaff4e9e2018-07-03 17:22:34 -07001946 rcu_report_qs_rsp(flags); /* releases rnp->lock. */
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001947}
1948
1949/*
Paul E. McKenneycc99a312015-02-23 08:59:29 -08001950 * Record a quiescent state for all tasks that were previously queued
1951 * on the specified rcu_node structure and that were blocking the current
Paul E. McKenney49918a52018-07-07 18:12:26 -07001952 * RCU grace period. The caller must hold the corresponding rnp->lock with
Paul E. McKenneycc99a312015-02-23 08:59:29 -08001953 * irqs disabled, and this lock is released upon return, but irqs remain
1954 * disabled.
1955 */
Paul E. McKenney17a82122018-05-03 14:30:02 -07001956static void __maybe_unused
Paul E. McKenney139ad4d2018-07-03 17:22:34 -07001957rcu_report_unblock_qs_rnp(struct rcu_node *rnp, unsigned long flags)
Paul E. McKenneycc99a312015-02-23 08:59:29 -08001958 __releases(rnp->lock)
1959{
Paul E. McKenney654e9532015-03-15 09:19:35 -07001960 unsigned long gps;
Paul E. McKenneycc99a312015-02-23 08:59:29 -08001961 unsigned long mask;
1962 struct rcu_node *rnp_p;
1963
Matthew Wilcoxa32e01e2018-01-17 06:24:30 -08001964 raw_lockdep_assert_held_rcu_node(rnp);
Lai Jiangshanc130d2d2019-10-15 10:28:48 +00001965 if (WARN_ON_ONCE(!IS_ENABLED(CONFIG_PREEMPT_RCU)) ||
Paul E. McKenneyc74859d2018-04-27 14:05:27 -07001966 WARN_ON_ONCE(rcu_preempt_blocked_readers_cgp(rnp)) ||
1967 rnp->qsmask != 0) {
Boqun Feng67c583a72015-12-29 12:18:47 +08001968 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
Paul E. McKenneycc99a312015-02-23 08:59:29 -08001969 return; /* Still need more quiescent states! */
1970 }
1971
Paul E. McKenney77cfc7b2018-05-01 15:00:10 -07001972 rnp->completedqs = rnp->gp_seq;
Paul E. McKenneycc99a312015-02-23 08:59:29 -08001973 rnp_p = rnp->parent;
1974 if (rnp_p == NULL) {
1975 /*
Paul E. McKenneya77da142015-03-08 14:52:27 -07001976 * Only one rcu_node structure in the tree, so don't
1977 * try to report up to its nonexistent parent!
Paul E. McKenneycc99a312015-02-23 08:59:29 -08001978 */
Paul E. McKenneyaff4e9e2018-07-03 17:22:34 -07001979 rcu_report_qs_rsp(flags);
Paul E. McKenneycc99a312015-02-23 08:59:29 -08001980 return;
1981 }
1982
Paul E. McKenneyc9a24e22018-04-27 14:54:46 -07001983 /* Report up the rest of the hierarchy, tracking current ->gp_seq. */
1984 gps = rnp->gp_seq;
Paul E. McKenneycc99a312015-02-23 08:59:29 -08001985 mask = rnp->grpmask;
Boqun Feng67c583a72015-12-29 12:18:47 +08001986 raw_spin_unlock_rcu_node(rnp); /* irqs remain disabled. */
Peter Zijlstra2a67e742015-10-08 12:24:23 +02001987 raw_spin_lock_rcu_node(rnp_p); /* irqs already disabled. */
Paul E. McKenneyb50912d2018-07-03 17:22:34 -07001988 rcu_report_qs_rnp(mask, rnp_p, gps, flags);
Paul E. McKenneycc99a312015-02-23 08:59:29 -08001989}
1990
1991/*
Paul E. McKenneyd3f6bad2009-12-02 12:10:13 -08001992 * Record a quiescent state for the specified CPU to that CPU's rcu_data
Paul E. McKenney4b455dc2016-01-27 22:44:45 -08001993 * structure. This must be called from the specified CPU.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001994 */
1995static void
Paul E. McKenney33085c42018-07-03 17:22:34 -07001996rcu_report_qs_rdp(int cpu, struct rcu_data *rdp)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001997{
1998 unsigned long flags;
1999 unsigned long mask;
Paul E. McKenney5d6742b2019-05-15 09:56:40 -07002000 bool needwake = false;
2001 const bool offloaded = IS_ENABLED(CONFIG_RCU_NOCB_CPU) &&
2002 rcu_segcblist_is_offloaded(&rdp->cblist);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002003 struct rcu_node *rnp;
2004
2005 rnp = rdp->mynode;
Peter Zijlstra2a67e742015-10-08 12:24:23 +02002006 raw_spin_lock_irqsave_rcu_node(rnp, flags);
Paul E. McKenneyc9a24e22018-04-27 14:54:46 -07002007 if (rdp->cpu_no_qs.b.norm || rdp->gp_seq != rnp->gp_seq ||
2008 rdp->gpwrap) {
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002009
2010 /*
Paul E. McKenneye4cc1f22011-06-27 00:17:43 -07002011 * The grace period in which this quiescent state was
2012 * recorded has ended, so don't report it upwards.
2013 * We will instead need a new quiescent state that lies
2014 * within the current grace period.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002015 */
Paul E. McKenney5b74c452015-08-06 15:16:57 -07002016 rdp->cpu_no_qs.b.norm = true; /* need qs for new gp. */
Boqun Feng67c583a72015-12-29 12:18:47 +08002017 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002018 return;
2019 }
2020 mask = rdp->grpmask;
Paul E. McKenneyb5ea0372019-12-09 15:19:45 -08002021 if (rdp->cpu == smp_processor_id())
2022 rdp->core_needs_qs = false;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002023 if ((rnp->qsmask & mask) == 0) {
Boqun Feng67c583a72015-12-29 12:18:47 +08002024 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002025 } else {
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002026 /*
2027 * This GP can't end until cpu checks in, so all of our
2028 * callbacks can be processed during the next GP.
2029 */
Paul E. McKenney5d6742b2019-05-15 09:56:40 -07002030 if (!offloaded)
2031 needwake = rcu_accelerate_cbs(rnp, rdp);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002032
Joel Fernandes (Google)516e5ae2019-09-05 10:26:41 -07002033 rcu_disable_urgency_upon_qs(rdp);
Paul E. McKenneyb50912d2018-07-03 17:22:34 -07002034 rcu_report_qs_rnp(mask, rnp, rnp->gp_seq, flags);
Paul E. McKenney654e9532015-03-15 09:19:35 -07002035 /* ^^^ Released rnp->lock */
Paul E. McKenney48a76392014-03-11 13:02:16 -07002036 if (needwake)
Paul E. McKenney532c00c2018-07-03 17:22:34 -07002037 rcu_gp_kthread_wake();
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002038 }
2039}
2040
2041/*
2042 * Check to see if there is a new grace period of which this CPU
2043 * is not yet aware, and if so, set up local rcu_data state for it.
2044 * Otherwise, see if this CPU has just passed through its first
2045 * quiescent state for this grace period, and record that fact if so.
2046 */
2047static void
Paul E. McKenney8087d3e2018-07-03 17:22:34 -07002048rcu_check_quiescent_state(struct rcu_data *rdp)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002049{
Paul E. McKenney05eb5522013-03-19 12:38:24 -07002050 /* Check for grace-period ends and beginnings. */
Paul E. McKenney15cabdf2018-07-03 17:22:34 -07002051 note_gp_changes(rdp);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002052
2053 /*
2054 * Does this CPU still need to do its part for current grace period?
2055 * If no, return and let the other CPUs do their part as well.
2056 */
Paul E. McKenney97c668b2015-08-06 11:31:51 -07002057 if (!rdp->core_needs_qs)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002058 return;
2059
2060 /*
2061 * Was there a quiescent state since the beginning of the grace
2062 * period? If no, then exit and wait for the next call.
2063 */
Paul E. McKenney3a19b462016-11-30 11:21:21 -08002064 if (rdp->cpu_no_qs.b.norm)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002065 return;
2066
Paul E. McKenneyd3f6bad2009-12-02 12:10:13 -08002067 /*
2068 * Tell RCU we are done (but rcu_report_qs_rdp() will be the
2069 * judge of that).
2070 */
Paul E. McKenney33085c42018-07-03 17:22:34 -07002071 rcu_report_qs_rdp(rdp->cpu, rdp);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002072}
2073
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002074/*
Paul E. McKenney780cd592018-07-03 17:22:34 -07002075 * Near the end of the offline process. Trace the fact that this CPU
2076 * is going offline.
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08002077 */
Paul E. McKenney780cd592018-07-03 17:22:34 -07002078int rcutree_dying_cpu(unsigned int cpu)
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08002079{
Yafang Shao4f5fbd72019-03-26 20:13:11 +08002080 bool blkd;
2081 struct rcu_data *rdp = this_cpu_ptr(&rcu_data);
2082 struct rcu_node *rnp = rdp->mynode;
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08002083
Paul E. McKenneyea463512015-03-03 14:05:26 -08002084 if (!IS_ENABLED(CONFIG_HOTPLUG_CPU))
Paul E. McKenney780cd592018-07-03 17:22:34 -07002085 return 0;
Paul E. McKenneyea463512015-03-03 14:05:26 -08002086
Yafang Shao4f5fbd72019-03-26 20:13:11 +08002087 blkd = !!(rnp->qsmask & rdp->grpmask);
Paul E. McKenney0937d042020-01-03 14:53:31 -08002088 trace_rcu_grace_period(rcu_state.name, READ_ONCE(rnp->gp_seq),
Paul E. McKenney477351f2018-05-01 12:54:11 -07002089 blkd ? TPS("cpuofl") : TPS("cpuofl-bgp"));
Paul E. McKenney780cd592018-07-03 17:22:34 -07002090 return 0;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002091}
2092
2093/*
Paul E. McKenney8af3a5e2014-10-31 11:22:37 -07002094 * All CPUs for the specified rcu_node structure have gone offline,
2095 * and all tasks that were preempted within an RCU read-side critical
2096 * section while running on one of those CPUs have since exited their RCU
2097 * read-side critical section. Some other CPU is reporting this fact with
2098 * the specified rcu_node structure's ->lock held and interrupts disabled.
2099 * This function therefore goes up the tree of rcu_node structures,
2100 * clearing the corresponding bits in the ->qsmaskinit fields. Note that
2101 * the leaf rcu_node structure's ->qsmaskinit field has already been
Paul E. McKenneyc50cbe52018-05-02 13:51:57 -07002102 * updated.
Paul E. McKenney8af3a5e2014-10-31 11:22:37 -07002103 *
2104 * This function does check that the specified rcu_node structure has
2105 * all CPUs offline and no blocked tasks, so it is OK to invoke it
2106 * prematurely. That said, invoking it after the fact will cost you
2107 * a needless lock acquisition. So once it has done its work, don't
2108 * invoke it again.
2109 */
2110static void rcu_cleanup_dead_rnp(struct rcu_node *rnp_leaf)
2111{
2112 long mask;
2113 struct rcu_node *rnp = rnp_leaf;
2114
Paul E. McKenney962aff02018-05-02 12:49:21 -07002115 raw_lockdep_assert_held_rcu_node(rnp_leaf);
Paul E. McKenneyea463512015-03-03 14:05:26 -08002116 if (!IS_ENABLED(CONFIG_HOTPLUG_CPU) ||
Paul E. McKenney962aff02018-05-02 12:49:21 -07002117 WARN_ON_ONCE(rnp_leaf->qsmaskinit) ||
2118 WARN_ON_ONCE(rcu_preempt_has_tasks(rnp_leaf)))
Paul E. McKenney8af3a5e2014-10-31 11:22:37 -07002119 return;
2120 for (;;) {
2121 mask = rnp->grpmask;
2122 rnp = rnp->parent;
2123 if (!rnp)
2124 break;
Peter Zijlstra2a67e742015-10-08 12:24:23 +02002125 raw_spin_lock_rcu_node(rnp); /* irqs already disabled. */
Paul E. McKenney8af3a5e2014-10-31 11:22:37 -07002126 rnp->qsmaskinit &= ~mask;
Paul E. McKenney962aff02018-05-02 12:49:21 -07002127 /* Between grace periods, so better already be zero! */
2128 WARN_ON_ONCE(rnp->qsmask);
Paul E. McKenney8af3a5e2014-10-31 11:22:37 -07002129 if (rnp->qsmaskinit) {
Boqun Feng67c583a72015-12-29 12:18:47 +08002130 raw_spin_unlock_rcu_node(rnp);
2131 /* irqs remain disabled. */
Paul E. McKenney8af3a5e2014-10-31 11:22:37 -07002132 return;
2133 }
Boqun Feng67c583a72015-12-29 12:18:47 +08002134 raw_spin_unlock_rcu_node(rnp); /* irqs remain disabled. */
Paul E. McKenney8af3a5e2014-10-31 11:22:37 -07002135 }
2136}
2137
2138/*
Paul E. McKenneye5601402012-01-07 11:03:57 -08002139 * The CPU has been completely removed, and some other CPU is reporting
Paul E. McKenneya58163d2017-06-20 12:11:34 -07002140 * this fact from process context. Do the remainder of the cleanup.
2141 * There can only be one CPU hotplug operation at a time, so no need for
2142 * explicit locking.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002143 */
Paul E. McKenney780cd592018-07-03 17:22:34 -07002144int rcutree_dead_cpu(unsigned int cpu)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002145{
Paul E. McKenneyda1df502018-07-03 15:37:16 -07002146 struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08002147 struct rcu_node *rnp = rdp->mynode; /* Outgoing CPU's rdp & rnp. */
Paul E. McKenneye5601402012-01-07 11:03:57 -08002148
Paul E. McKenneyea463512015-03-03 14:05:26 -08002149 if (!IS_ENABLED(CONFIG_HOTPLUG_CPU))
Paul E. McKenney780cd592018-07-03 17:22:34 -07002150 return 0;
Paul E. McKenneyea463512015-03-03 14:05:26 -08002151
Paul E. McKenney2036d942012-01-30 17:02:47 -08002152 /* Adjust any no-longer-needed kthreads. */
Thomas Gleixner5d01bbd2012-07-16 10:42:35 +00002153 rcu_boost_kthread_setaffinity(rnp, -1);
Paul E. McKenney780cd592018-07-03 17:22:34 -07002154 /* Do any needed no-CB deferred wakeups from this CPU. */
2155 do_nocb_deferred_wakeup(per_cpu_ptr(&rcu_data, cpu));
Paul E. McKenney96926682019-08-02 15:12:47 -07002156
2157 // Stop-machine done, so allow nohz_full to disable tick.
2158 tick_dep_clear(TICK_DEP_BIT_RCU);
Paul E. McKenney780cd592018-07-03 17:22:34 -07002159 return 0;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002160}
2161
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002162/*
2163 * Invoke any RCU callbacks that have made it to the end of their grace
2164 * period. Thottle as specified by rdp->blimit.
2165 */
Paul E. McKenney5bb5d092018-07-03 17:22:34 -07002166static void rcu_do_batch(struct rcu_data *rdp)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002167{
2168 unsigned long flags;
Paul E. McKenneyec5ef872019-05-21 13:03:49 -07002169 const bool offloaded = IS_ENABLED(CONFIG_RCU_NOCB_CPU) &&
2170 rcu_segcblist_is_offloaded(&rdp->cblist);
Paul E. McKenney15fecf82017-02-08 12:36:42 -08002171 struct rcu_head *rhp;
2172 struct rcu_cblist rcl = RCU_CBLIST_INITIALIZER(rcl);
2173 long bl, count;
Eric Dumazetcfcdef52019-07-24 18:07:52 -07002174 long pending, tlimit = 0;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002175
Paul E. McKenneydc35c892012-12-03 13:52:00 -08002176 /* If no callbacks are ready, just return. */
Paul E. McKenney15fecf82017-02-08 12:36:42 -08002177 if (!rcu_segcblist_ready_cbs(&rdp->cblist)) {
Paul E. McKenney3c779df2018-07-05 15:54:02 -07002178 trace_rcu_batch_start(rcu_state.name,
Paul E. McKenney15fecf82017-02-08 12:36:42 -08002179 rcu_segcblist_n_cbs(&rdp->cblist), 0);
Paul E. McKenney3c779df2018-07-05 15:54:02 -07002180 trace_rcu_batch_end(rcu_state.name, 0,
Paul E. McKenney15fecf82017-02-08 12:36:42 -08002181 !rcu_segcblist_empty(&rdp->cblist),
Paul E. McKenney4968c302011-12-07 16:32:40 -08002182 need_resched(), is_idle_task(current),
2183 rcu_is_callbacks_kthread());
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002184 return;
Paul E. McKenney29c00b42011-06-17 15:53:19 -07002185 }
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002186
2187 /*
2188 * Extract the list of ready callbacks, disabling to prevent
Paul E. McKenney15fecf82017-02-08 12:36:42 -08002189 * races with call_rcu() from interrupt handlers. Leave the
2190 * callback counts, as rcu_barrier() needs to be conservative.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002191 */
2192 local_irq_save(flags);
Paul E. McKenney5d6742b2019-05-15 09:56:40 -07002193 rcu_nocb_lock(rdp);
Paul E. McKenney8146c4e22012-01-10 14:23:29 -08002194 WARN_ON_ONCE(cpu_is_offline(smp_processor_id()));
Eric Dumazetcfcdef52019-07-24 18:07:52 -07002195 pending = rcu_segcblist_n_cbs(&rdp->cblist);
2196 bl = max(rdp->blimit, pending >> rcu_divisor);
2197 if (unlikely(bl > 100))
2198 tlimit = local_clock() + rcu_resched_ns;
Paul E. McKenney3c779df2018-07-05 15:54:02 -07002199 trace_rcu_batch_start(rcu_state.name,
Paul E. McKenney15fecf82017-02-08 12:36:42 -08002200 rcu_segcblist_n_cbs(&rdp->cblist), bl);
2201 rcu_segcblist_extract_done_cbs(&rdp->cblist, &rcl);
Paul E. McKenney7f36ef82019-05-28 05:54:26 -07002202 if (offloaded)
2203 rdp->qlen_last_fqs_check = rcu_segcblist_n_cbs(&rdp->cblist);
Paul E. McKenney5d6742b2019-05-15 09:56:40 -07002204 rcu_nocb_unlock_irqrestore(rdp, flags);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002205
2206 /* Invoke callbacks. */
Paul E. McKenney6a949b72019-07-28 11:50:56 -07002207 tick_dep_set_task(current, TICK_DEP_BIT_RCU);
Paul E. McKenney15fecf82017-02-08 12:36:42 -08002208 rhp = rcu_cblist_dequeue(&rcl);
2209 for (; rhp; rhp = rcu_cblist_dequeue(&rcl)) {
Joel Fernandes (Google)77a40f92019-08-30 12:36:32 -04002210 rcu_callback_t f;
2211
Paul E. McKenney15fecf82017-02-08 12:36:42 -08002212 debug_rcu_head_unqueue(rhp);
Joel Fernandes (Google)77a40f92019-08-30 12:36:32 -04002213
2214 rcu_lock_acquire(&rcu_callback_map);
2215 trace_rcu_invoke_callback(rcu_state.name, rhp);
2216
2217 f = rhp->func;
2218 WRITE_ONCE(rhp->func, (rcu_callback_t)0L);
2219 f(rhp);
2220
2221 rcu_lock_release(&rcu_callback_map);
2222
Paul E. McKenney15fecf82017-02-08 12:36:42 -08002223 /*
2224 * Stop only if limit reached and CPU has something to do.
2225 * Note: The rcl structure counts down from zero.
2226 */
Paul E. McKenneyec5ef872019-05-21 13:03:49 -07002227 if (-rcl.len >= bl && !offloaded &&
Paul E. McKenneydff16722011-11-29 15:57:13 -08002228 (need_resched() ||
2229 (!is_idle_task(current) && !rcu_is_callbacks_kthread())))
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002230 break;
Eric Dumazetcfcdef52019-07-24 18:07:52 -07002231 if (unlikely(tlimit)) {
2232 /* only call local_clock() every 32 callbacks */
2233 if (likely((-rcl.len & 31) || local_clock() < tlimit))
2234 continue;
2235 /* Exceeded the time limit, so leave. */
2236 break;
2237 }
Paul E. McKenneyec5ef872019-05-21 13:03:49 -07002238 if (offloaded) {
Paul E. McKenney5d6742b2019-05-15 09:56:40 -07002239 WARN_ON_ONCE(in_serving_softirq());
2240 local_bh_enable();
2241 lockdep_assert_irqs_enabled();
2242 cond_resched_tasks_rcu_qs();
2243 lockdep_assert_irqs_enabled();
2244 local_bh_disable();
2245 }
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002246 }
2247
2248 local_irq_save(flags);
Paul E. McKenney5d6742b2019-05-15 09:56:40 -07002249 rcu_nocb_lock(rdp);
Paul E. McKenney4b27f202017-05-02 08:45:25 -07002250 count = -rcl.len;
Paul E. McKenney3c779df2018-07-05 15:54:02 -07002251 trace_rcu_batch_end(rcu_state.name, count, !!rcl.head, need_resched(),
Paul E. McKenney8ef0f372017-05-02 08:18:40 -07002252 is_idle_task(current), rcu_is_callbacks_kthread());
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002253
Paul E. McKenney15fecf82017-02-08 12:36:42 -08002254 /* Update counts and requeue any remaining callbacks. */
2255 rcu_segcblist_insert_done_cbs(&rdp->cblist, &rcl);
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08002256 smp_mb(); /* List handling before counting for rcu_barrier(). */
Paul E. McKenney15fecf82017-02-08 12:36:42 -08002257 rcu_segcblist_insert_count(&rdp->cblist, &rcl);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002258
2259 /* Reinstate batch limit if we have worked down the excess. */
Paul E. McKenney15fecf82017-02-08 12:36:42 -08002260 count = rcu_segcblist_n_cbs(&rdp->cblist);
Paul E. McKenneyd5a9a8c2019-04-10 17:01:39 -07002261 if (rdp->blimit >= DEFAULT_MAX_RCU_BLIMIT && count <= qlowmark)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002262 rdp->blimit = blimit;
2263
Paul E. McKenney37c72e52009-10-14 10:15:55 -07002264 /* Reset ->qlen_last_fqs_check trigger if enough CBs have drained. */
Paul E. McKenney15fecf82017-02-08 12:36:42 -08002265 if (count == 0 && rdp->qlen_last_fqs_check != 0) {
Paul E. McKenney37c72e52009-10-14 10:15:55 -07002266 rdp->qlen_last_fqs_check = 0;
Paul E. McKenney3c779df2018-07-05 15:54:02 -07002267 rdp->n_force_qs_snap = rcu_state.n_force_qs;
Paul E. McKenney15fecf82017-02-08 12:36:42 -08002268 } else if (count < rdp->qlen_last_fqs_check - qhimark)
2269 rdp->qlen_last_fqs_check = count;
Paul E. McKenneyefd88b02017-10-19 14:52:41 -07002270
2271 /*
2272 * The following usually indicates a double call_rcu(). To track
2273 * this down, try building with CONFIG_DEBUG_OBJECTS_RCU_HEAD=y.
2274 */
Paul E. McKenneyd1b222c2019-07-02 16:03:33 -07002275 WARN_ON_ONCE(count == 0 && !rcu_segcblist_empty(&rdp->cblist));
2276 WARN_ON_ONCE(!IS_ENABLED(CONFIG_RCU_NOCB_CPU) &&
2277 count != 0 && rcu_segcblist_empty(&rdp->cblist));
Paul E. McKenney37c72e52009-10-14 10:15:55 -07002278
Paul E. McKenney5d6742b2019-05-15 09:56:40 -07002279 rcu_nocb_unlock_irqrestore(rdp, flags);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002280
Paul E. McKenneye0f23062011-06-21 01:29:39 -07002281 /* Re-invoke RCU core processing if there are callbacks remaining. */
Paul E. McKenneyec5ef872019-05-21 13:03:49 -07002282 if (!offloaded && rcu_segcblist_ready_cbs(&rdp->cblist))
Paul E. McKenneya46e0892011-06-15 15:47:09 -07002283 invoke_rcu_core();
Paul E. McKenney6a949b72019-07-28 11:50:56 -07002284 tick_dep_clear_task(current, TICK_DEP_BIT_RCU);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002285}
2286
2287/*
Paul E. McKenneyc98cac62018-11-21 11:35:03 -08002288 * This function is invoked from each scheduling-clock interrupt,
2289 * and checks to see if this CPU is in a non-context-switch quiescent
2290 * state, for example, user mode or idle loop. It also schedules RCU
2291 * core processing. If the current grace period has gone on too long,
2292 * it will ask the scheduler to manufacture a context switch for the sole
2293 * purpose of providing a providing the needed quiescent state.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002294 */
Paul E. McKenneyc98cac62018-11-21 11:35:03 -08002295void rcu_sched_clock_irq(int user)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002296{
Steven Rostedt (Red Hat)f7f7bac2013-07-12 17:18:47 -04002297 trace_rcu_utilization(TPS("Start scheduler-tick"));
Paul E. McKenney4e950202018-07-05 17:59:36 -07002298 raw_cpu_inc(rcu_data.ticks_this_gp);
Paul E. McKenney92aa39e2018-07-09 13:47:30 -07002299 /* The load-acquire pairs with the store-release setting to true. */
Paul E. McKenney2dba13f2018-08-03 21:00:38 -07002300 if (smp_load_acquire(this_cpu_ptr(&rcu_data.rcu_urgent_qs))) {
Paul E. McKenney92aa39e2018-07-09 13:47:30 -07002301 /* Idle and userspace execution already are quiescent states. */
Paul E. McKenneya0ef9ec2018-07-09 15:50:16 -07002302 if (!rcu_is_cpu_rrupt_from_idle() && !user) {
Paul E. McKenney92aa39e2018-07-09 13:47:30 -07002303 set_tsk_need_resched(current);
2304 set_preempt_need_resched();
2305 }
Paul E. McKenney2dba13f2018-08-03 21:00:38 -07002306 __this_cpu_write(rcu_data.rcu_urgent_qs, false);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002307 }
Paul E. McKenneyc98cac62018-11-21 11:35:03 -08002308 rcu_flavor_sched_clock_irq(user);
Paul E. McKenneydd7dafd2019-09-14 03:39:22 -07002309 if (rcu_pending(user))
Paul E. McKenneya46e0892011-06-15 15:47:09 -07002310 invoke_rcu_core();
Byungchul Park07f27572018-05-11 17:30:34 +09002311
Steven Rostedt (Red Hat)f7f7bac2013-07-12 17:18:47 -04002312 trace_rcu_utilization(TPS("End scheduler-tick"));
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002313}
2314
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002315/*
Zhouyi Zhou5d8a7522019-03-20 03:33:00 +00002316 * Scan the leaf rcu_node structures. For each structure on which all
2317 * CPUs have reported a quiescent state and on which there are tasks
2318 * blocking the current grace period, initiate RCU priority boosting.
2319 * Otherwise, invoke the specified function to check dyntick state for
2320 * each CPU that has not yet reported a quiescent state.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002321 */
Paul E. McKenney8ff0b902018-07-05 17:55:14 -07002322static void force_qs_rnp(int (*f)(struct rcu_data *rdp))
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002323{
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002324 int cpu;
2325 unsigned long flags;
2326 unsigned long mask;
Paul E. McKenney66e4c332019-08-12 16:14:00 -07002327 struct rcu_data *rdp;
Paul E. McKenneya0b6c9a2009-09-28 07:46:33 -07002328 struct rcu_node *rnp;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002329
Paul E. McKenneyb2b00dd2019-10-30 11:56:10 -07002330 rcu_state.cbovld = rcu_state.cbovldnext;
2331 rcu_state.cbovldnext = false;
Paul E. McKenneyaedf4ba2018-07-04 14:33:59 -07002332 rcu_for_each_leaf_node(rnp) {
Paul E. McKenneycee43932018-03-02 16:35:27 -08002333 cond_resched_tasks_rcu_qs();
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002334 mask = 0;
Peter Zijlstra2a67e742015-10-08 12:24:23 +02002335 raw_spin_lock_irqsave_rcu_node(rnp, flags);
Paul E. McKenneyb2b00dd2019-10-30 11:56:10 -07002336 rcu_state.cbovldnext |= !!rnp->cbovldmask;
Paul E. McKenneya0b6c9a2009-09-28 07:46:33 -07002337 if (rnp->qsmask == 0) {
Lai Jiangshanc130d2d2019-10-15 10:28:48 +00002338 if (!IS_ENABLED(CONFIG_PREEMPT_RCU) ||
Paul E. McKenneya77da142015-03-08 14:52:27 -07002339 rcu_preempt_blocked_readers_cgp(rnp)) {
2340 /*
2341 * No point in scanning bits because they
2342 * are all zero. But we might need to
2343 * priority-boost blocked readers.
2344 */
2345 rcu_initiate_boost(rnp, flags);
2346 /* rcu_initiate_boost() releases rnp->lock */
2347 continue;
2348 }
Paul E. McKenney92816432018-05-02 11:07:02 -07002349 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
2350 continue;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002351 }
Paul E. McKenney7441e762019-10-30 09:37:11 -07002352 for_each_leaf_node_cpu_mask(rnp, cpu, rnp->qsmask) {
2353 rdp = per_cpu_ptr(&rcu_data, cpu);
2354 if (f(rdp)) {
2355 mask |= rdp->grpmask;
2356 rcu_disable_urgency_upon_qs(rdp);
Paul E. McKenney0edd1b12013-06-21 16:37:22 -07002357 }
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002358 }
Paul E. McKenney45f014c52e2010-01-04 15:09:08 -08002359 if (mask != 0) {
Paul E. McKenneyc9a24e22018-04-27 14:54:46 -07002360 /* Idle/offline CPUs, report (releases rnp->lock). */
Paul E. McKenneyb50912d2018-07-03 17:22:34 -07002361 rcu_report_qs_rnp(mask, rnp, rnp->gp_seq, flags);
Paul E. McKenney0aa04b02015-01-23 21:52:37 -08002362 } else {
2363 /* Nothing to do here, so just drop the lock. */
Boqun Feng67c583a72015-12-29 12:18:47 +08002364 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002365 }
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002366 }
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002367}
2368
2369/*
2370 * Force quiescent states on reluctant CPUs, and also detect which
2371 * CPUs are in dyntick-idle mode.
2372 */
Paul E. McKenneycd920e52018-11-28 16:57:54 -08002373void rcu_force_quiescent_state(void)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002374{
2375 unsigned long flags;
Paul E. McKenney394f2762012-06-26 17:00:35 -07002376 bool ret;
2377 struct rcu_node *rnp;
2378 struct rcu_node *rnp_old = NULL;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002379
Paul E. McKenney394f2762012-06-26 17:00:35 -07002380 /* Funnel through hierarchy to reduce memory contention. */
Paul E. McKenneyda1df502018-07-03 15:37:16 -07002381 rnp = __this_cpu_read(rcu_data.mynode);
Paul E. McKenney394f2762012-06-26 17:00:35 -07002382 for (; rnp != NULL; rnp = rnp->parent) {
Paul E. McKenney67a0edb2018-07-05 16:15:38 -07002383 ret = (READ_ONCE(rcu_state.gp_flags) & RCU_GP_FLAG_FQS) ||
Paul E. McKenney66e4c332019-08-12 16:14:00 -07002384 !raw_spin_trylock(&rnp->fqslock);
Paul E. McKenney394f2762012-06-26 17:00:35 -07002385 if (rnp_old != NULL)
2386 raw_spin_unlock(&rnp_old->fqslock);
Paul E. McKenneyd62df572018-01-10 13:10:49 -08002387 if (ret)
Paul E. McKenney394f2762012-06-26 17:00:35 -07002388 return;
Paul E. McKenney394f2762012-06-26 17:00:35 -07002389 rnp_old = rnp;
2390 }
Paul E. McKenney336a4f62018-07-03 17:22:34 -07002391 /* rnp_old == rcu_get_root(), rnp == NULL. */
Paul E. McKenney394f2762012-06-26 17:00:35 -07002392
2393 /* Reached the root of the rcu_node tree, acquire lock. */
Peter Zijlstra2a67e742015-10-08 12:24:23 +02002394 raw_spin_lock_irqsave_rcu_node(rnp_old, flags);
Paul E. McKenney394f2762012-06-26 17:00:35 -07002395 raw_spin_unlock(&rnp_old->fqslock);
Paul E. McKenney67a0edb2018-07-05 16:15:38 -07002396 if (READ_ONCE(rcu_state.gp_flags) & RCU_GP_FLAG_FQS) {
Boqun Feng67c583a72015-12-29 12:18:47 +08002397 raw_spin_unlock_irqrestore_rcu_node(rnp_old, flags);
Paul E. McKenney4cdfc1752012-06-22 17:06:26 -07002398 return; /* Someone beat us to it. */
Paul E. McKenney46a1e342010-01-04 15:09:09 -08002399 }
Paul E. McKenney67a0edb2018-07-05 16:15:38 -07002400 WRITE_ONCE(rcu_state.gp_flags,
2401 READ_ONCE(rcu_state.gp_flags) | RCU_GP_FLAG_FQS);
Boqun Feng67c583a72015-12-29 12:18:47 +08002402 raw_spin_unlock_irqrestore_rcu_node(rnp_old, flags);
Paul E. McKenney532c00c2018-07-03 17:22:34 -07002403 rcu_gp_kthread_wake();
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002404}
Paul E. McKenneycd920e52018-11-28 16:57:54 -08002405EXPORT_SYMBOL_GPL(rcu_force_quiescent_state);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002406
Paul E. McKenneyfb60e532018-11-21 12:42:12 -08002407/* Perform RCU core processing work for the current CPU. */
Sebastian Andrzej Siewior48d07c02019-03-20 22:13:33 +01002408static __latent_entropy void rcu_core(void)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002409{
Shaohua Li09223372011-06-14 13:26:25 +08002410 unsigned long flags;
Paul E. McKenneyda1df502018-07-03 15:37:16 -07002411 struct rcu_data *rdp = raw_cpu_ptr(&rcu_data);
Paul E. McKenneyb0f74032013-02-04 12:14:24 -08002412 struct rcu_node *rnp = rdp->mynode;
Paul E. McKenneyc1ab99d2019-05-21 13:39:15 -07002413 const bool offloaded = IS_ENABLED(CONFIG_RCU_NOCB_CPU) &&
2414 rcu_segcblist_is_offloaded(&rdp->cblist);
Paul E. McKenneya26ac242011-01-12 14:10:23 -08002415
2416 if (cpu_is_offline(smp_processor_id()))
Peter Zijlstra08bca602011-05-20 16:06:29 -07002417 return;
Paul E. McKenneya26ac242011-01-12 14:10:23 -08002418 trace_rcu_utilization(TPS("Start RCU core"));
Shaohua Li09223372011-06-14 13:26:25 +08002419 WARN_ON_ONCE(!rdp->beenonline);
Paul E. McKenney29154c52012-05-30 03:21:48 -07002420
Paul E. McKenney3e310092018-06-21 12:50:01 -07002421 /* Report any deferred quiescent states if preemption enabled. */
Paul E. McKenneyfced9c82018-07-26 13:44:00 -07002422 if (!(preempt_count() & PREEMPT_MASK)) {
Paul E. McKenney3e310092018-06-21 12:50:01 -07002423 rcu_preempt_deferred_qs(current);
Paul E. McKenneyfced9c82018-07-26 13:44:00 -07002424 } else if (rcu_preempt_need_deferred_qs(current)) {
2425 set_tsk_need_resched(current);
2426 set_preempt_need_resched();
2427 }
Paul E. McKenney3e310092018-06-21 12:50:01 -07002428
Paul E. McKenney29154c52012-05-30 03:21:48 -07002429 /* Update RCU state based on any recent quiescent states. */
Paul E. McKenney8087d3e2018-07-03 17:22:34 -07002430 rcu_check_quiescent_state(rdp);
Paul E. McKenney29154c52012-05-30 03:21:48 -07002431
2432 /* No grace period and unregistered callbacks? */
Paul E. McKenneyde8e8732018-07-03 17:22:34 -07002433 if (!rcu_gp_in_progress() &&
Paul E. McKenneyc1ab99d2019-05-21 13:39:15 -07002434 rcu_segcblist_is_enabled(&rdp->cblist) && !offloaded) {
Paul E. McKenney48a76392014-03-11 13:02:16 -07002435 local_irq_save(flags);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002436 if (!rcu_segcblist_restempty(&rdp->cblist, RCU_NEXT_READY_TAIL))
Paul E. McKenneyc6e09b92018-07-03 17:22:34 -07002437 rcu_accelerate_cbs_unlocked(rnp, rdp);
Paul E. McKenney29154c52012-05-30 03:21:48 -07002438 local_irq_restore(flags);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002439 }
Yao Dongdong9910aff2015-02-25 17:09:46 +08002440
Paul E. McKenney791416c2018-10-01 15:42:44 -07002441 rcu_check_gp_start_stall(rnp, rdp, rcu_jiffies_till_stall_check());
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002442
Paul E. McKenney29154c52012-05-30 03:21:48 -07002443 /* If there are callbacks ready, invoke them. */
Paul E. McKenneyc1ab99d2019-05-21 13:39:15 -07002444 if (!offloaded && rcu_segcblist_ready_cbs(&rdp->cblist) &&
Paul E. McKenney43e903a2019-03-25 08:36:03 -07002445 likely(READ_ONCE(rcu_scheduler_fully_active)))
2446 rcu_do_batch(rdp);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002447
2448 /* Do any needed deferred wakeups of rcuo kthreads. */
2449 do_nocb_deferred_wakeup(rdp);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002450 trace_rcu_utilization(TPS("End RCU core"));
2451}
2452
Sebastian Andrzej Siewior48d07c02019-03-20 22:13:33 +01002453static void rcu_core_si(struct softirq_action *h)
2454{
2455 rcu_core();
2456}
2457
2458static void rcu_wake_cond(struct task_struct *t, int status)
2459{
2460 /*
2461 * If the thread is yielding, only wake it when this
2462 * is invoked from idle
2463 */
2464 if (t && (status != RCU_KTHREAD_YIELDING || is_idle_task(current)))
2465 wake_up_process(t);
2466}
2467
2468static void invoke_rcu_core_kthread(void)
2469{
2470 struct task_struct *t;
2471 unsigned long flags;
2472
2473 local_irq_save(flags);
2474 __this_cpu_write(rcu_data.rcu_cpu_has_work, 1);
2475 t = __this_cpu_read(rcu_data.rcu_cpu_kthread_task);
2476 if (t != NULL && t != current)
2477 rcu_wake_cond(t, __this_cpu_read(rcu_data.rcu_cpu_kthread_status));
2478 local_irq_restore(flags);
2479}
2480
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002481/*
Sebastian Andrzej Siewior48d07c02019-03-20 22:13:33 +01002482 * Wake up this CPU's rcuc kthread to do RCU core processing.
2483 */
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002484static void invoke_rcu_core(void)
2485{
Sebastian Andrzej Siewior48d07c02019-03-20 22:13:33 +01002486 if (!cpu_online(smp_processor_id()))
2487 return;
2488 if (use_softirq)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002489 raise_softirq(RCU_SOFTIRQ);
Sebastian Andrzej Siewior48d07c02019-03-20 22:13:33 +01002490 else
2491 invoke_rcu_core_kthread();
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002492}
2493
Sebastian Andrzej Siewior48d07c02019-03-20 22:13:33 +01002494static void rcu_cpu_kthread_park(unsigned int cpu)
2495{
2496 per_cpu(rcu_data.rcu_cpu_kthread_status, cpu) = RCU_KTHREAD_OFFCPU;
2497}
2498
2499static int rcu_cpu_kthread_should_run(unsigned int cpu)
2500{
2501 return __this_cpu_read(rcu_data.rcu_cpu_has_work);
2502}
2503
2504/*
2505 * Per-CPU kernel thread that invokes RCU callbacks. This replaces
2506 * the RCU softirq used in configurations of RCU that do not support RCU
2507 * priority boosting.
2508 */
2509static void rcu_cpu_kthread(unsigned int cpu)
2510{
2511 unsigned int *statusp = this_cpu_ptr(&rcu_data.rcu_cpu_kthread_status);
2512 char work, *workp = this_cpu_ptr(&rcu_data.rcu_cpu_has_work);
2513 int spincnt;
2514
Lai Jiangshan2488a5e2019-10-15 10:23:57 +00002515 trace_rcu_utilization(TPS("Start CPU kthread@rcu_run"));
Sebastian Andrzej Siewior48d07c02019-03-20 22:13:33 +01002516 for (spincnt = 0; spincnt < 10; spincnt++) {
Sebastian Andrzej Siewior48d07c02019-03-20 22:13:33 +01002517 local_bh_disable();
2518 *statusp = RCU_KTHREAD_RUNNING;
2519 local_irq_disable();
2520 work = *workp;
2521 *workp = 0;
2522 local_irq_enable();
2523 if (work)
2524 rcu_core();
2525 local_bh_enable();
2526 if (*workp == 0) {
2527 trace_rcu_utilization(TPS("End CPU kthread@rcu_wait"));
2528 *statusp = RCU_KTHREAD_WAITING;
2529 return;
2530 }
2531 }
2532 *statusp = RCU_KTHREAD_YIELDING;
2533 trace_rcu_utilization(TPS("Start CPU kthread@rcu_yield"));
2534 schedule_timeout_interruptible(2);
2535 trace_rcu_utilization(TPS("End CPU kthread@rcu_yield"));
2536 *statusp = RCU_KTHREAD_WAITING;
2537}
2538
2539static struct smp_hotplug_thread rcu_cpu_thread_spec = {
2540 .store = &rcu_data.rcu_cpu_kthread_task,
2541 .thread_should_run = rcu_cpu_kthread_should_run,
2542 .thread_fn = rcu_cpu_kthread,
2543 .thread_comm = "rcuc/%u",
2544 .setup = rcu_cpu_kthread_setup,
2545 .park = rcu_cpu_kthread_park,
2546};
2547
2548/*
2549 * Spawn per-CPU RCU core processing kthreads.
2550 */
2551static int __init rcu_spawn_core_kthreads(void)
2552{
2553 int cpu;
2554
2555 for_each_possible_cpu(cpu)
2556 per_cpu(rcu_data.rcu_cpu_has_work, cpu) = 0;
2557 if (!IS_ENABLED(CONFIG_RCU_BOOST) && use_softirq)
2558 return 0;
2559 WARN_ONCE(smpboot_register_percpu_thread(&rcu_cpu_thread_spec),
2560 "%s: Could not start rcuc kthread, OOM is now expected behavior\n", __func__);
2561 return 0;
2562}
2563early_initcall(rcu_spawn_core_kthreads);
2564
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002565/*
2566 * Handle any core-RCU processing required by a call_rcu() invocation.
Paul E. McKenney29154c52012-05-30 03:21:48 -07002567 */
Paul E. McKenney5c7d8962018-07-03 17:22:34 -07002568static void __call_rcu_core(struct rcu_data *rdp, struct rcu_head *head,
2569 unsigned long flags)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002570{
Paul E. McKenney37c72e52009-10-14 10:15:55 -07002571 /*
2572 * If called from an extended quiescent state, invoke the RCU
2573 * core in order to force a re-evaluation of RCU's idleness.
2574 */
2575 if (!rcu_is_watching())
Paul E. McKenney2655d572011-04-07 22:47:23 -07002576 invoke_rcu_core();
Paul E. McKenneyb52573d2010-12-14 17:36:02 -08002577
2578 /* If interrupts were disabled or CPU offline, don't invoke RCU core. */
Paul E. McKenney470716f2013-03-19 11:32:11 -07002579 if (irqs_disabled_flags(flags) || cpu_is_offline(smp_processor_id()))
Paul E. McKenneyb52573d2010-12-14 17:36:02 -08002580 return;
2581
2582 /*
Paul E. McKenneyb52573d2010-12-14 17:36:02 -08002583 * Force the grace period if too many callbacks or too long waiting.
Paul E. McKenneycd920e52018-11-28 16:57:54 -08002584 * Enforce hysteresis, and don't invoke rcu_force_quiescent_state()
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002585 * if some other CPU has recently done so. Also, don't bother
Paul E. McKenneycd920e52018-11-28 16:57:54 -08002586 * invoking rcu_force_quiescent_state() if the newly enqueued callback
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002587 * is the only one waiting for a grace period to complete.
2588 */
Paul E. McKenney15fecf82017-02-08 12:36:42 -08002589 if (unlikely(rcu_segcblist_n_cbs(&rdp->cblist) >
2590 rdp->qlen_last_fqs_check + qhimark)) {
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002591
2592 /* Are we ignoring a completed grace period? */
Paul E. McKenney15cabdf2018-07-03 17:22:34 -07002593 note_gp_changes(rdp);
Paul E. McKenneyb52573d2010-12-14 17:36:02 -08002594
2595 /* Start a new grace period if one not already started. */
Paul E. McKenneyde8e8732018-07-03 17:22:34 -07002596 if (!rcu_gp_in_progress()) {
Paul E. McKenneyc6e09b92018-07-03 17:22:34 -07002597 rcu_accelerate_cbs_unlocked(rdp->mynode, rdp);
Paul E. McKenneyb52573d2010-12-14 17:36:02 -08002598 } else {
2599 /* Give the grace period a kick. */
Paul E. McKenneyd5a9a8c2019-04-10 17:01:39 -07002600 rdp->blimit = DEFAULT_MAX_RCU_BLIMIT;
Paul E. McKenney5c7d8962018-07-03 17:22:34 -07002601 if (rcu_state.n_force_qs == rdp->n_force_qs_snap &&
Paul E. McKenney15fecf82017-02-08 12:36:42 -08002602 rcu_segcblist_first_pend_cb(&rdp->cblist) != head)
Paul E. McKenneycd920e52018-11-28 16:57:54 -08002603 rcu_force_quiescent_state();
Paul E. McKenney5c7d8962018-07-03 17:22:34 -07002604 rdp->n_force_qs_snap = rcu_state.n_force_qs;
Paul E. McKenney15fecf82017-02-08 12:36:42 -08002605 rdp->qlen_last_fqs_check = rcu_segcblist_n_cbs(&rdp->cblist);
Paul E. McKenneyb52573d2010-12-14 17:36:02 -08002606 }
Paul E. McKenney4cdfc1752012-06-22 17:06:26 -07002607 }
Paul E. McKenney29154c52012-05-30 03:21:48 -07002608}
2609
Paul E. McKenney3fbfbf72012-08-19 21:35:53 -07002610/*
Paul E. McKenneyae150182013-04-23 13:20:57 -07002611 * RCU callback function to leak a callback.
2612 */
2613static void rcu_leak_callback(struct rcu_head *rhp)
2614{
2615}
2616
2617/*
Paul E. McKenneyb2b00dd2019-10-30 11:56:10 -07002618 * Check and if necessary update the leaf rcu_node structure's
2619 * ->cbovldmask bit corresponding to the current CPU based on that CPU's
2620 * number of queued RCU callbacks. The caller must hold the leaf rcu_node
2621 * structure's ->lock.
Paul E. McKenney3fbfbf72012-08-19 21:35:53 -07002622 */
Paul E. McKenneyb2b00dd2019-10-30 11:56:10 -07002623static void check_cb_ovld_locked(struct rcu_data *rdp, struct rcu_node *rnp)
2624{
2625 raw_lockdep_assert_held_rcu_node(rnp);
2626 if (qovld_calc <= 0)
2627 return; // Early boot and wildcard value set.
2628 if (rcu_segcblist_n_cbs(&rdp->cblist) >= qovld_calc)
2629 WRITE_ONCE(rnp->cbovldmask, rnp->cbovldmask | rdp->grpmask);
2630 else
2631 WRITE_ONCE(rnp->cbovldmask, rnp->cbovldmask & ~rdp->grpmask);
2632}
2633
2634/*
2635 * Check and if necessary update the leaf rcu_node structure's
2636 * ->cbovldmask bit corresponding to the current CPU based on that CPU's
2637 * number of queued RCU callbacks. No locks need be held, but the
2638 * caller must have disabled interrupts.
2639 *
2640 * Note that this function ignores the possibility that there are a lot
2641 * of callbacks all of which have already seen the end of their respective
2642 * grace periods. This omission is due to the need for no-CBs CPUs to
2643 * be holding ->nocb_lock to do this check, which is too heavy for a
2644 * common-case operation.
2645 */
2646static void check_cb_ovld(struct rcu_data *rdp)
2647{
2648 struct rcu_node *const rnp = rdp->mynode;
2649
2650 if (qovld_calc <= 0 ||
2651 ((rcu_segcblist_n_cbs(&rdp->cblist) >= qovld_calc) ==
2652 !!(READ_ONCE(rnp->cbovldmask) & rdp->grpmask)))
2653 return; // Early boot wildcard value or already set correctly.
2654 raw_spin_lock_rcu_node(rnp);
2655 check_cb_ovld_locked(rdp, rnp);
2656 raw_spin_unlock_rcu_node(rnp);
2657}
2658
Paul E. McKenneyb692dc42020-02-11 07:29:02 -08002659/* Helper function for call_rcu() and friends. */
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002660static void
Joel Fernandes (Google)77a40f92019-08-30 12:36:32 -04002661__call_rcu(struct rcu_head *head, rcu_callback_t func)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002662{
2663 unsigned long flags;
2664 struct rcu_data *rdp;
Paul E. McKenney5d6742b2019-05-15 09:56:40 -07002665 bool was_alldone;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002666
Paul E. McKenneyb8f2ed52016-08-23 06:51:47 -07002667 /* Misaligned rcu_head! */
2668 WARN_ON_ONCE((unsigned long)head & (sizeof(void *) - 1));
2669
Paul E. McKenneyae150182013-04-23 13:20:57 -07002670 if (debug_rcu_head_queue(head)) {
Paul E. McKenneyfa3c6642017-05-03 11:38:55 -07002671 /*
2672 * Probable double call_rcu(), so leak the callback.
2673 * Use rcu:rcu_callback trace event to find the previous
2674 * time callback was passed to __call_rcu().
2675 */
Sakari Ailusd75f7732019-03-25 21:32:28 +02002676 WARN_ONCE(1, "__call_rcu(): Double-freed CB %p->%pS()!!!\n",
Paul E. McKenneyfa3c6642017-05-03 11:38:55 -07002677 head, head->func);
Paul E. McKenney7d0ae802015-03-03 14:57:58 -08002678 WRITE_ONCE(head->func, rcu_leak_callback);
Paul E. McKenneyae150182013-04-23 13:20:57 -07002679 return;
2680 }
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002681 head->func = func;
2682 head->next = NULL;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002683 local_irq_save(flags);
Paul E. McKenneyda1df502018-07-03 15:37:16 -07002684 rdp = this_cpu_ptr(&rcu_data);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002685
2686 /* Add the callback to our list. */
Paul E. McKenney5d6742b2019-05-15 09:56:40 -07002687 if (unlikely(!rcu_segcblist_is_enabled(&rdp->cblist))) {
2688 // This can trigger due to call_rcu() from offline CPU:
2689 WARN_ON_ONCE(rcu_scheduler_active != RCU_SCHEDULER_INACTIVE);
Paul E. McKenney34404ca2015-01-19 20:39:20 -08002690 WARN_ON_ONCE(!rcu_is_watching());
Paul E. McKenney5d6742b2019-05-15 09:56:40 -07002691 // Very early boot, before rcu_init(). Initialize if needed
2692 // and then drop through to queue the callback.
Paul E. McKenney15fecf82017-02-08 12:36:42 -08002693 if (rcu_segcblist_empty(&rdp->cblist))
2694 rcu_segcblist_init(&rdp->cblist);
Paul E. McKenney0d8ee372012-08-03 13:16:15 -07002695 }
Joel Fernandes (Google)77a40f92019-08-30 12:36:32 -04002696
Paul E. McKenneyb2b00dd2019-10-30 11:56:10 -07002697 check_cb_ovld(rdp);
Paul E. McKenneyd1b222c2019-07-02 16:03:33 -07002698 if (rcu_nocb_try_bypass(rdp, head, &was_alldone, flags))
2699 return; // Enqueued onto ->nocb_bypass, so just leave.
Paul E. McKenneyb692dc42020-02-11 07:29:02 -08002700 // If no-CBs CPU gets here, rcu_nocb_try_bypass() acquired ->nocb_lock.
Joel Fernandes (Google)77a40f92019-08-30 12:36:32 -04002701 rcu_segcblist_enqueue(&rdp->cblist, head);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002702 if (__is_kfree_rcu_offset((unsigned long)func))
Paul E. McKenney3c779df2018-07-05 15:54:02 -07002703 trace_rcu_kfree_callback(rcu_state.name, head,
2704 (unsigned long)func,
Paul E. McKenney15fecf82017-02-08 12:36:42 -08002705 rcu_segcblist_n_cbs(&rdp->cblist));
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002706 else
Paul E. McKenney3c779df2018-07-05 15:54:02 -07002707 trace_rcu_callback(rcu_state.name, head,
Paul E. McKenney15fecf82017-02-08 12:36:42 -08002708 rcu_segcblist_n_cbs(&rdp->cblist));
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002709
Paul E. McKenney29154c52012-05-30 03:21:48 -07002710 /* Go handle any RCU core processing required. */
Paul E. McKenney5d6742b2019-05-15 09:56:40 -07002711 if (IS_ENABLED(CONFIG_RCU_NOCB_CPU) &&
2712 unlikely(rcu_segcblist_is_offloaded(&rdp->cblist))) {
2713 __call_rcu_nocb_wake(rdp, was_alldone, flags); /* unlocks */
2714 } else {
2715 __call_rcu_core(rdp, head, flags);
2716 local_irq_restore(flags);
2717 }
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002718}
2719
Paul E. McKenneya68a2bb2017-05-03 08:34:57 -07002720/**
Paul E. McKenney45975c72018-07-02 14:30:37 -07002721 * call_rcu() - Queue an RCU callback for invocation after a grace period.
Paul E. McKenneya68a2bb2017-05-03 08:34:57 -07002722 * @head: structure to be used for queueing the RCU updates.
2723 * @func: actual callback function to be invoked after the grace period
2724 *
2725 * The callback function will be invoked some time after a full grace
Paul E. McKenney45975c72018-07-02 14:30:37 -07002726 * period elapses, in other words after all pre-existing RCU read-side
2727 * critical sections have completed. However, the callback function
2728 * might well execute concurrently with RCU read-side critical sections
2729 * that started after call_rcu() was invoked. RCU read-side critical
2730 * sections are delimited by rcu_read_lock() and rcu_read_unlock(), and
2731 * may be nested. In addition, regions of code across which interrupts,
2732 * preemption, or softirqs have been disabled also serve as RCU read-side
2733 * critical sections. This includes hardware interrupt handlers, softirq
2734 * handlers, and NMI handlers.
Paul E. McKenney27fdb352017-10-19 14:26:21 -07002735 *
Paul E. McKenney45975c72018-07-02 14:30:37 -07002736 * Note that all CPUs must agree that the grace period extended beyond
2737 * all pre-existing RCU read-side critical section. On systems with more
2738 * than one CPU, this means that when "func()" is invoked, each CPU is
2739 * guaranteed to have executed a full memory barrier since the end of its
2740 * last RCU read-side critical section whose beginning preceded the call
2741 * to call_rcu(). It also means that each CPU executing an RCU read-side
2742 * critical section that continues beyond the start of "func()" must have
2743 * executed a memory barrier after the call_rcu() but before the beginning
2744 * of that RCU read-side critical section. Note that these guarantees
2745 * include CPUs that are offline, idle, or executing in user mode, as
2746 * well as CPUs that are executing in the kernel.
Paul E. McKenneya68a2bb2017-05-03 08:34:57 -07002747 *
Paul E. McKenney45975c72018-07-02 14:30:37 -07002748 * Furthermore, if CPU A invoked call_rcu() and CPU B invoked the
2749 * resulting RCU callback function "func()", then both CPU A and CPU B are
2750 * guaranteed to execute a full memory barrier during the time interval
2751 * between the call to call_rcu() and the invocation of "func()" -- even
2752 * if CPU A and CPU B are the same CPU (but again only if the system has
2753 * more than one CPU).
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002754 */
Paul E. McKenney45975c72018-07-02 14:30:37 -07002755void call_rcu(struct rcu_head *head, rcu_callback_t func)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002756{
Joel Fernandes (Google)77a40f92019-08-30 12:36:32 -04002757 __call_rcu(head, func);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002758}
Paul E. McKenney45975c72018-07-02 14:30:37 -07002759EXPORT_SYMBOL_GPL(call_rcu);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002760
Byungchul Parka35d1692019-08-05 18:22:27 -04002761
2762/* Maximum number of jiffies to wait before draining a batch. */
2763#define KFREE_DRAIN_JIFFIES (HZ / 50)
Joel Fernandes (Google)0392beb2019-09-19 14:58:26 -07002764#define KFREE_N_BATCHES 2
2765
Uladzislau Rezki (Sony)34c88172020-01-20 15:42:25 +01002766/*
2767 * This macro defines how many entries the "records" array
2768 * will contain. It is based on the fact that the size of
2769 * kfree_rcu_bulk_data structure becomes exactly one page.
2770 */
2771#define KFREE_BULK_MAX_ENTR ((PAGE_SIZE / sizeof(void *)) - 3)
2772
2773/**
2774 * struct kfree_rcu_bulk_data - single block to store kfree_rcu() pointers
2775 * @nr_records: Number of active pointers in the array
2776 * @records: Array of the kfree_rcu() pointers
2777 * @next: Next bulk object in the block chain
2778 * @head_free_debug: For debug, when CONFIG_DEBUG_OBJECTS_RCU_HEAD is set
2779 */
2780struct kfree_rcu_bulk_data {
2781 unsigned long nr_records;
2782 void *records[KFREE_BULK_MAX_ENTR];
2783 struct kfree_rcu_bulk_data *next;
2784 struct rcu_head *head_free_debug;
2785};
2786
Joel Fernandes (Google)0392beb2019-09-19 14:58:26 -07002787/**
2788 * struct kfree_rcu_cpu_work - single batch of kfree_rcu() requests
2789 * @rcu_work: Let queue_rcu_work() invoke workqueue handler after grace period
2790 * @head_free: List of kfree_rcu() objects waiting for a grace period
Uladzislau Rezki (Sony)34c88172020-01-20 15:42:25 +01002791 * @bhead_free: Bulk-List of kfree_rcu() objects waiting for a grace period
Joel Fernandes (Google)0392beb2019-09-19 14:58:26 -07002792 * @krcp: Pointer to @kfree_rcu_cpu structure
2793 */
2794
2795struct kfree_rcu_cpu_work {
2796 struct rcu_work rcu_work;
2797 struct rcu_head *head_free;
Uladzislau Rezki (Sony)34c88172020-01-20 15:42:25 +01002798 struct kfree_rcu_bulk_data *bhead_free;
Joel Fernandes (Google)0392beb2019-09-19 14:58:26 -07002799 struct kfree_rcu_cpu *krcp;
2800};
Byungchul Parka35d1692019-08-05 18:22:27 -04002801
2802/**
2803 * struct kfree_rcu_cpu - batch up kfree_rcu() requests for RCU grace period
Byungchul Parka35d1692019-08-05 18:22:27 -04002804 * @head: List of kfree_rcu() objects not yet waiting for a grace period
Uladzislau Rezki (Sony)34c88172020-01-20 15:42:25 +01002805 * @bhead: Bulk-List of kfree_rcu() objects not yet waiting for a grace period
2806 * @bcached: Keeps at most one object for later reuse when build chain blocks
Joel Fernandes (Google)0392beb2019-09-19 14:58:26 -07002807 * @krw_arr: Array of batches of kfree_rcu() objects waiting for a grace period
Byungchul Parka35d1692019-08-05 18:22:27 -04002808 * @lock: Synchronize access to this structure
2809 * @monitor_work: Promote @head to @head_free after KFREE_DRAIN_JIFFIES
2810 * @monitor_todo: Tracks whether a @monitor_work delayed work is pending
2811 * @initialized: The @lock and @rcu_work fields have been initialized
2812 *
2813 * This is a per-CPU structure. The reason that it is not included in
2814 * the rcu_data structure is to permit this code to be extracted from
2815 * the RCU files. Such extraction could allow further optimization of
2816 * the interactions with the slab allocators.
Andreea-Cristina Bernat495aa962014-03-18 20:48:48 +02002817 */
Byungchul Parka35d1692019-08-05 18:22:27 -04002818struct kfree_rcu_cpu {
Byungchul Parka35d1692019-08-05 18:22:27 -04002819 struct rcu_head *head;
Uladzislau Rezki (Sony)34c88172020-01-20 15:42:25 +01002820 struct kfree_rcu_bulk_data *bhead;
2821 struct kfree_rcu_bulk_data *bcached;
Joel Fernandes (Google)0392beb2019-09-19 14:58:26 -07002822 struct kfree_rcu_cpu_work krw_arr[KFREE_N_BATCHES];
Byungchul Parka35d1692019-08-05 18:22:27 -04002823 spinlock_t lock;
2824 struct delayed_work monitor_work;
Joel Fernandes569d7672019-09-22 10:49:57 -07002825 bool monitor_todo;
Byungchul Parka35d1692019-08-05 18:22:27 -04002826 bool initialized;
2827};
2828
2829static DEFINE_PER_CPU(struct kfree_rcu_cpu, krc);
2830
Uladzislau Rezki (Sony)34c88172020-01-20 15:42:25 +01002831static __always_inline void
2832debug_rcu_head_unqueue_bulk(struct rcu_head *head)
2833{
2834#ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD
2835 for (; head; head = head->next)
2836 debug_rcu_head_unqueue(head);
2837#endif
2838}
2839
Paul E. McKenney6d813392012-02-23 13:30:16 -08002840/*
Byungchul Parka35d1692019-08-05 18:22:27 -04002841 * This function is invoked in workqueue context after a grace period.
Uladzislau Rezki (Sony)34c88172020-01-20 15:42:25 +01002842 * It frees all the objects queued on ->bhead_free or ->head_free.
Byungchul Parka35d1692019-08-05 18:22:27 -04002843 */
2844static void kfree_rcu_work(struct work_struct *work)
2845{
2846 unsigned long flags;
2847 struct rcu_head *head, *next;
Uladzislau Rezki (Sony)34c88172020-01-20 15:42:25 +01002848 struct kfree_rcu_bulk_data *bhead, *bnext;
Byungchul Parka35d1692019-08-05 18:22:27 -04002849 struct kfree_rcu_cpu *krcp;
Joel Fernandes (Google)0392beb2019-09-19 14:58:26 -07002850 struct kfree_rcu_cpu_work *krwp;
Byungchul Parka35d1692019-08-05 18:22:27 -04002851
Joel Fernandes (Google)0392beb2019-09-19 14:58:26 -07002852 krwp = container_of(to_rcu_work(work),
2853 struct kfree_rcu_cpu_work, rcu_work);
2854 krcp = krwp->krcp;
Byungchul Parka35d1692019-08-05 18:22:27 -04002855 spin_lock_irqsave(&krcp->lock, flags);
Joel Fernandes (Google)0392beb2019-09-19 14:58:26 -07002856 head = krwp->head_free;
2857 krwp->head_free = NULL;
Uladzislau Rezki (Sony)34c88172020-01-20 15:42:25 +01002858 bhead = krwp->bhead_free;
2859 krwp->bhead_free = NULL;
Byungchul Parka35d1692019-08-05 18:22:27 -04002860 spin_unlock_irqrestore(&krcp->lock, flags);
2861
Uladzislau Rezki (Sony)34c88172020-01-20 15:42:25 +01002862 /* "bhead" is now private, so traverse locklessly. */
2863 for (; bhead; bhead = bnext) {
2864 bnext = bhead->next;
2865
2866 debug_rcu_head_unqueue_bulk(bhead->head_free_debug);
2867
2868 rcu_lock_acquire(&rcu_callback_map);
Uladzislau Rezki (Sony)61370792020-01-20 15:42:26 +01002869 trace_rcu_invoke_kfree_bulk_callback(rcu_state.name,
2870 bhead->nr_records, bhead->records);
2871
Uladzislau Rezki (Sony)34c88172020-01-20 15:42:25 +01002872 kfree_bulk(bhead->nr_records, bhead->records);
2873 rcu_lock_release(&rcu_callback_map);
2874
2875 if (cmpxchg(&krcp->bcached, NULL, bhead))
2876 free_page((unsigned long) bhead);
2877
2878 cond_resched_tasks_rcu_qs();
2879 }
2880
2881 /*
2882 * Emergency case only. It can happen under low memory
2883 * condition when an allocation gets failed, so the "bulk"
2884 * path can not be temporary maintained.
2885 */
Byungchul Parka35d1692019-08-05 18:22:27 -04002886 for (; head; head = next) {
Joel Fernandes (Google)77a40f92019-08-30 12:36:32 -04002887 unsigned long offset = (unsigned long)head->func;
2888
Byungchul Parka35d1692019-08-05 18:22:27 -04002889 next = head->next;
Joel Fernandes (Google)e99637b2019-09-22 13:03:17 -07002890 debug_rcu_head_unqueue(head);
Joel Fernandes (Google)77a40f92019-08-30 12:36:32 -04002891 rcu_lock_acquire(&rcu_callback_map);
2892 trace_rcu_invoke_kfree_callback(rcu_state.name, head, offset);
2893
Uladzislau Rezki (Sony)34c88172020-01-20 15:42:25 +01002894 if (!WARN_ON_ONCE(!__is_kfree_rcu_offset(offset)))
Joel Fernandes (Google)189a6882019-08-30 12:36:33 -04002895 kfree((void *)head - offset);
Joel Fernandes (Google)77a40f92019-08-30 12:36:32 -04002896
2897 rcu_lock_release(&rcu_callback_map);
Byungchul Parka35d1692019-08-05 18:22:27 -04002898 cond_resched_tasks_rcu_qs();
2899 }
2900}
2901
2902/*
2903 * Schedule the kfree batch RCU work to run in workqueue context after a GP.
2904 *
2905 * This function is invoked by kfree_rcu_monitor() when the KFREE_DRAIN_JIFFIES
2906 * timeout has been reached.
2907 */
2908static inline bool queue_kfree_rcu_work(struct kfree_rcu_cpu *krcp)
2909{
Uladzislau Rezki (Sony)34c88172020-01-20 15:42:25 +01002910 struct kfree_rcu_cpu_work *krwp;
2911 bool queued = false;
Joel Fernandes (Google)0392beb2019-09-19 14:58:26 -07002912 int i;
Joel Fernandes (Google)0392beb2019-09-19 14:58:26 -07002913
Byungchul Parka35d1692019-08-05 18:22:27 -04002914 lockdep_assert_held(&krcp->lock);
Uladzislau Rezki (Sony)34c88172020-01-20 15:42:25 +01002915
2916 for (i = 0; i < KFREE_N_BATCHES; i++) {
2917 krwp = &(krcp->krw_arr[i]);
2918
2919 /*
2920 * Try to detach bhead or head and attach it over any
2921 * available corresponding free channel. It can be that
2922 * a previous RCU batch is in progress, it means that
2923 * immediately to queue another one is not possible so
2924 * return false to tell caller to retry.
2925 */
2926 if ((krcp->bhead && !krwp->bhead_free) ||
2927 (krcp->head && !krwp->head_free)) {
2928 /* Channel 1. */
2929 if (!krwp->bhead_free) {
2930 krwp->bhead_free = krcp->bhead;
2931 krcp->bhead = NULL;
2932 }
2933
2934 /* Channel 2. */
2935 if (!krwp->head_free) {
2936 krwp->head_free = krcp->head;
2937 krcp->head = NULL;
2938 }
2939
2940 /*
2941 * One work is per one batch, so there are two "free channels",
2942 * "bhead_free" and "head_free" the batch can handle. It can be
2943 * that the work is in the pending state when two channels have
2944 * been detached following each other, one by one.
2945 */
2946 queue_rcu_work(system_wq, &krwp->rcu_work);
2947 queued = true;
Joel Fernandes (Google)0392beb2019-09-19 14:58:26 -07002948 }
Uladzislau Rezki (Sony)34c88172020-01-20 15:42:25 +01002949 }
Byungchul Parka35d1692019-08-05 18:22:27 -04002950
Uladzislau Rezki (Sony)34c88172020-01-20 15:42:25 +01002951 return queued;
Byungchul Parka35d1692019-08-05 18:22:27 -04002952}
2953
2954static inline void kfree_rcu_drain_unlock(struct kfree_rcu_cpu *krcp,
2955 unsigned long flags)
2956{
2957 // Attempt to start a new batch.
Joel Fernandes569d7672019-09-22 10:49:57 -07002958 krcp->monitor_todo = false;
Byungchul Parka35d1692019-08-05 18:22:27 -04002959 if (queue_kfree_rcu_work(krcp)) {
2960 // Success! Our job is done here.
2961 spin_unlock_irqrestore(&krcp->lock, flags);
2962 return;
2963 }
2964
2965 // Previous RCU batch still in progress, try again later.
Joel Fernandes569d7672019-09-22 10:49:57 -07002966 krcp->monitor_todo = true;
2967 schedule_delayed_work(&krcp->monitor_work, KFREE_DRAIN_JIFFIES);
Byungchul Parka35d1692019-08-05 18:22:27 -04002968 spin_unlock_irqrestore(&krcp->lock, flags);
2969}
2970
2971/*
2972 * This function is invoked after the KFREE_DRAIN_JIFFIES timeout.
2973 * It invokes kfree_rcu_drain_unlock() to attempt to start another batch.
2974 */
2975static void kfree_rcu_monitor(struct work_struct *work)
2976{
2977 unsigned long flags;
2978 struct kfree_rcu_cpu *krcp = container_of(work, struct kfree_rcu_cpu,
2979 monitor_work.work);
2980
2981 spin_lock_irqsave(&krcp->lock, flags);
Joel Fernandes569d7672019-09-22 10:49:57 -07002982 if (krcp->monitor_todo)
Byungchul Parka35d1692019-08-05 18:22:27 -04002983 kfree_rcu_drain_unlock(krcp, flags);
2984 else
2985 spin_unlock_irqrestore(&krcp->lock, flags);
2986}
2987
Uladzislau Rezki (Sony)34c88172020-01-20 15:42:25 +01002988static inline bool
2989kfree_call_rcu_add_ptr_to_bulk(struct kfree_rcu_cpu *krcp,
2990 struct rcu_head *head, rcu_callback_t func)
2991{
2992 struct kfree_rcu_bulk_data *bnode;
2993
2994 if (unlikely(!krcp->initialized))
2995 return false;
2996
2997 lockdep_assert_held(&krcp->lock);
2998
2999 /* Check if a new block is required. */
3000 if (!krcp->bhead ||
3001 krcp->bhead->nr_records == KFREE_BULK_MAX_ENTR) {
3002 bnode = xchg(&krcp->bcached, NULL);
3003 if (!bnode) {
3004 WARN_ON_ONCE(sizeof(struct kfree_rcu_bulk_data) > PAGE_SIZE);
3005
3006 bnode = (struct kfree_rcu_bulk_data *)
3007 __get_free_page(GFP_NOWAIT | __GFP_NOWARN);
3008 }
3009
3010 /* Switch to emergency path. */
3011 if (unlikely(!bnode))
3012 return false;
3013
3014 /* Initialize the new block. */
3015 bnode->nr_records = 0;
3016 bnode->next = krcp->bhead;
3017 bnode->head_free_debug = NULL;
3018
3019 /* Attach it to the head. */
3020 krcp->bhead = bnode;
3021 }
3022
3023#ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD
3024 head->func = func;
3025 head->next = krcp->bhead->head_free_debug;
3026 krcp->bhead->head_free_debug = head;
3027#endif
3028
3029 /* Finally insert. */
3030 krcp->bhead->records[krcp->bhead->nr_records++] =
3031 (void *) head - (unsigned long) func;
3032
3033 return true;
3034}
3035
Byungchul Parka35d1692019-08-05 18:22:27 -04003036/*
Uladzislau Rezki (Sony)34c88172020-01-20 15:42:25 +01003037 * Queue a request for lazy invocation of kfree_bulk()/kfree() after a grace
3038 * period. Please note there are two paths are maintained, one is the main one
3039 * that uses kfree_bulk() interface and second one is emergency one, that is
3040 * used only when the main path can not be maintained temporary, due to memory
3041 * pressure.
Byungchul Parka35d1692019-08-05 18:22:27 -04003042 *
3043 * Each kfree_call_rcu() request is added to a batch. The batch will be drained
Uladzislau Rezki (Sony)34c88172020-01-20 15:42:25 +01003044 * every KFREE_DRAIN_JIFFIES number of jiffies. All the objects in the batch will
3045 * be free'd in workqueue context. This allows us to: batch requests together to
3046 * reduce the number of grace periods during heavy kfree_rcu() load.
Andreea-Cristina Bernat495aa962014-03-18 20:48:48 +02003047 */
Paul E. McKenney98ece502018-07-03 17:22:34 -07003048void kfree_call_rcu(struct rcu_head *head, rcu_callback_t func)
Andreea-Cristina Bernat495aa962014-03-18 20:48:48 +02003049{
Byungchul Parka35d1692019-08-05 18:22:27 -04003050 unsigned long flags;
3051 struct kfree_rcu_cpu *krcp;
3052
Byungchul Parka35d1692019-08-05 18:22:27 -04003053 local_irq_save(flags); // For safely calling this_cpu_ptr().
3054 krcp = this_cpu_ptr(&krc);
3055 if (krcp->initialized)
3056 spin_lock(&krcp->lock);
3057
3058 // Queue the object but don't yet schedule the batch.
Joel Fernandes (Google)e99637b2019-09-22 13:03:17 -07003059 if (debug_rcu_head_queue(head)) {
3060 // Probable double kfree_rcu(), just leak.
3061 WARN_ONCE(1, "%s(): Double-freed call. rcu_head %p\n",
3062 __func__, head);
3063 goto unlock_return;
3064 }
Uladzislau Rezki (Sony)34c88172020-01-20 15:42:25 +01003065
3066 /*
3067 * Under high memory pressure GFP_NOWAIT can fail,
3068 * in that case the emergency path is maintained.
3069 */
3070 if (unlikely(!kfree_call_rcu_add_ptr_to_bulk(krcp, head, func))) {
3071 head->func = func;
3072 head->next = krcp->head;
3073 krcp->head = head;
3074 }
Byungchul Parka35d1692019-08-05 18:22:27 -04003075
3076 // Set timer to drain after KFREE_DRAIN_JIFFIES.
3077 if (rcu_scheduler_active == RCU_SCHEDULER_RUNNING &&
Joel Fernandes569d7672019-09-22 10:49:57 -07003078 !krcp->monitor_todo) {
3079 krcp->monitor_todo = true;
Byungchul Parka35d1692019-08-05 18:22:27 -04003080 schedule_delayed_work(&krcp->monitor_work, KFREE_DRAIN_JIFFIES);
Joel Fernandes569d7672019-09-22 10:49:57 -07003081 }
Byungchul Parka35d1692019-08-05 18:22:27 -04003082
Joel Fernandes (Google)e99637b2019-09-22 13:03:17 -07003083unlock_return:
Byungchul Parka35d1692019-08-05 18:22:27 -04003084 if (krcp->initialized)
3085 spin_unlock(&krcp->lock);
3086 local_irq_restore(flags);
Andreea-Cristina Bernat495aa962014-03-18 20:48:48 +02003087}
3088EXPORT_SYMBOL_GPL(kfree_call_rcu);
3089
Byungchul Parka35d1692019-08-05 18:22:27 -04003090void __init kfree_rcu_scheduler_running(void)
3091{
3092 int cpu;
3093 unsigned long flags;
3094
3095 for_each_online_cpu(cpu) {
3096 struct kfree_rcu_cpu *krcp = per_cpu_ptr(&krc, cpu);
3097
3098 spin_lock_irqsave(&krcp->lock, flags);
Joel Fernandes569d7672019-09-22 10:49:57 -07003099 if (!krcp->head || krcp->monitor_todo) {
Byungchul Parka35d1692019-08-05 18:22:27 -04003100 spin_unlock_irqrestore(&krcp->lock, flags);
3101 continue;
3102 }
Joel Fernandes569d7672019-09-22 10:49:57 -07003103 krcp->monitor_todo = true;
Joel Fernandes (Google)0392beb2019-09-19 14:58:26 -07003104 schedule_delayed_work_on(cpu, &krcp->monitor_work,
3105 KFREE_DRAIN_JIFFIES);
Byungchul Parka35d1692019-08-05 18:22:27 -04003106 spin_unlock_irqrestore(&krcp->lock, flags);
3107 }
3108}
3109
Paul E. McKenneye5bc3af2018-11-29 10:42:06 -08003110/*
3111 * During early boot, any blocking grace-period wait automatically
Sebastian Andrzej Siewior90326f02019-10-15 21:18:14 +02003112 * implies a grace period. Later on, this is never the case for PREEMPTION.
Paul E. McKenneye5bc3af2018-11-29 10:42:06 -08003113 *
Sebastian Andrzej Siewior90326f02019-10-15 21:18:14 +02003114 * Howevr, because a context switch is a grace period for !PREEMPTION, any
Paul E. McKenneye5bc3af2018-11-29 10:42:06 -08003115 * blocking grace-period wait automatically implies a grace period if
3116 * there is only one CPU online at any point time during execution of
3117 * either synchronize_rcu() or synchronize_rcu_expedited(). It is OK to
3118 * occasionally incorrectly indicate that there are multiple CPUs online
3119 * when there was in fact only one the whole time, as this just adds some
3120 * overhead: RCU still operates correctly.
3121 */
3122static int rcu_blocking_is_gp(void)
3123{
3124 int ret;
3125
Thomas Gleixner01b1d882019-07-26 23:19:38 +02003126 if (IS_ENABLED(CONFIG_PREEMPTION))
Paul E. McKenneye5bc3af2018-11-29 10:42:06 -08003127 return rcu_scheduler_active == RCU_SCHEDULER_INACTIVE;
3128 might_sleep(); /* Check for RCU read-side critical section. */
3129 preempt_disable();
3130 ret = num_online_cpus() <= 1;
3131 preempt_enable();
3132 return ret;
3133}
3134
3135/**
3136 * synchronize_rcu - wait until a grace period has elapsed.
3137 *
3138 * Control will return to the caller some time after a full grace
3139 * period has elapsed, in other words after all currently executing RCU
3140 * read-side critical sections have completed. Note, however, that
3141 * upon return from synchronize_rcu(), the caller might well be executing
3142 * concurrently with new RCU read-side critical sections that began while
3143 * synchronize_rcu() was waiting. RCU read-side critical sections are
3144 * delimited by rcu_read_lock() and rcu_read_unlock(), and may be nested.
3145 * In addition, regions of code across which interrupts, preemption, or
3146 * softirqs have been disabled also serve as RCU read-side critical
3147 * sections. This includes hardware interrupt handlers, softirq handlers,
3148 * and NMI handlers.
3149 *
3150 * Note that this guarantee implies further memory-ordering guarantees.
3151 * On systems with more than one CPU, when synchronize_rcu() returns,
3152 * each CPU is guaranteed to have executed a full memory barrier since
3153 * the end of its last RCU read-side critical section whose beginning
3154 * preceded the call to synchronize_rcu(). In addition, each CPU having
3155 * an RCU read-side critical section that extends beyond the return from
3156 * synchronize_rcu() is guaranteed to have executed a full memory barrier
3157 * after the beginning of synchronize_rcu() and before the beginning of
3158 * that RCU read-side critical section. Note that these guarantees include
3159 * CPUs that are offline, idle, or executing in user mode, as well as CPUs
3160 * that are executing in the kernel.
3161 *
3162 * Furthermore, if CPU A invoked synchronize_rcu(), which returned
3163 * to its caller on CPU B, then both CPU A and CPU B are guaranteed
3164 * to have executed a full memory barrier during the execution of
3165 * synchronize_rcu() -- even if CPU A and CPU B are the same CPU (but
3166 * again only if the system has more than one CPU).
3167 */
3168void synchronize_rcu(void)
3169{
3170 RCU_LOCKDEP_WARN(lock_is_held(&rcu_bh_lock_map) ||
3171 lock_is_held(&rcu_lock_map) ||
3172 lock_is_held(&rcu_sched_lock_map),
3173 "Illegal synchronize_rcu() in RCU read-side critical section");
3174 if (rcu_blocking_is_gp())
3175 return;
3176 if (rcu_gp_is_expedited())
3177 synchronize_rcu_expedited();
3178 else
3179 wait_rcu_gp(call_rcu);
3180}
3181EXPORT_SYMBOL_GPL(synchronize_rcu);
3182
Paul E. McKenney765a3f42014-03-14 16:37:08 -07003183/**
3184 * get_state_synchronize_rcu - Snapshot current RCU state
3185 *
3186 * Returns a cookie that is used by a later call to cond_synchronize_rcu()
3187 * to determine whether or not a full grace period has elapsed in the
3188 * meantime.
3189 */
3190unsigned long get_state_synchronize_rcu(void)
3191{
3192 /*
3193 * Any prior manipulation of RCU-protected data must happen
Paul E. McKenneye4be81a2018-04-27 15:16:50 -07003194 * before the load from ->gp_seq.
Paul E. McKenney765a3f42014-03-14 16:37:08 -07003195 */
3196 smp_mb(); /* ^^^ */
Paul E. McKenney16fc9c62018-07-03 15:54:39 -07003197 return rcu_seq_snap(&rcu_state.gp_seq);
Paul E. McKenney765a3f42014-03-14 16:37:08 -07003198}
3199EXPORT_SYMBOL_GPL(get_state_synchronize_rcu);
3200
3201/**
3202 * cond_synchronize_rcu - Conditionally wait for an RCU grace period
3203 *
3204 * @oldstate: return value from earlier call to get_state_synchronize_rcu()
3205 *
3206 * If a full RCU grace period has elapsed since the earlier call to
3207 * get_state_synchronize_rcu(), just return. Otherwise, invoke
3208 * synchronize_rcu() to wait for a full grace period.
3209 *
3210 * Yes, this function does not take counter wrap into account. But
3211 * counter wrap is harmless. If the counter wraps, we have waited for
3212 * more than 2 billion grace periods (and way more on a 64-bit system!),
3213 * so waiting for one additional grace period should be just fine.
3214 */
3215void cond_synchronize_rcu(unsigned long oldstate)
3216{
Paul E. McKenney16fc9c62018-07-03 15:54:39 -07003217 if (!rcu_seq_done(&rcu_state.gp_seq, oldstate))
Paul E. McKenney765a3f42014-03-14 16:37:08 -07003218 synchronize_rcu();
Paul E. McKenneye4be81a2018-04-27 15:16:50 -07003219 else
3220 smp_mb(); /* Ensure GP ends before subsequent accesses. */
Paul E. McKenney765a3f42014-03-14 16:37:08 -07003221}
3222EXPORT_SYMBOL_GPL(cond_synchronize_rcu);
3223
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003224/*
Paul E. McKenney98ece502018-07-03 17:22:34 -07003225 * Check to see if there is any immediate RCU-related work to be done by
Paul E. McKenney49918a52018-07-07 18:12:26 -07003226 * the current CPU, returning 1 if so and zero otherwise. The checks are
3227 * in order of increasing expense: checks that can be carried out against
3228 * CPU-local state are performed first. However, we must check for CPU
3229 * stalls first, else we might not get a chance.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003230 */
Paul E. McKenneydd7dafd2019-09-14 03:39:22 -07003231static int rcu_pending(int user)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003232{
Paul E. McKenneyed93dfc2019-09-13 14:09:56 -07003233 bool gp_in_progress;
Paul E. McKenney98ece502018-07-03 17:22:34 -07003234 struct rcu_data *rdp = this_cpu_ptr(&rcu_data);
Paul E. McKenney2f51f982009-11-13 19:51:39 -08003235 struct rcu_node *rnp = rdp->mynode;
3236
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003237 /* Check for CPU stalls, if enabled. */
Paul E. McKenneyea12ff22018-07-03 17:22:34 -07003238 check_cpu_stall(rdp);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003239
Paul E. McKenney85f69b32019-04-16 14:48:28 -07003240 /* Does this CPU need a deferred NOCB wakeup? */
3241 if (rcu_nocb_need_deferred_wakeup(rdp))
3242 return 1;
3243
Paul E. McKenneydd7dafd2019-09-14 03:39:22 -07003244 /* Is this a nohz_full CPU in userspace or idle? (Ignore RCU if so.) */
3245 if ((user || rcu_is_cpu_rrupt_from_idle()) && rcu_nohz_full_cpu())
Paul E. McKenneya0969322013-11-08 09:03:10 -08003246 return 0;
3247
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003248 /* Is the RCU core waiting for a quiescent state from this CPU? */
Paul E. McKenneyed93dfc2019-09-13 14:09:56 -07003249 gp_in_progress = rcu_gp_in_progress();
3250 if (rdp->core_needs_qs && !rdp->cpu_no_qs.b.norm && gp_in_progress)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003251 return 1;
3252
3253 /* Does this CPU have callbacks ready to invoke? */
Paul E. McKenney01c495f2018-01-10 12:36:00 -08003254 if (rcu_segcblist_ready_cbs(&rdp->cblist))
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003255 return 1;
3256
3257 /* Has RCU gone idle with this CPU needing another grace period? */
Paul E. McKenneyed93dfc2019-09-13 14:09:56 -07003258 if (!gp_in_progress && rcu_segcblist_is_enabled(&rdp->cblist) &&
Paul E. McKenney921bb5f2019-05-21 13:53:28 -07003259 (!IS_ENABLED(CONFIG_RCU_NOCB_CPU) ||
3260 !rcu_segcblist_is_offloaded(&rdp->cblist)) &&
Paul E. McKenneyc1935202018-04-12 16:29:13 -07003261 !rcu_segcblist_restempty(&rdp->cblist, RCU_NEXT_READY_TAIL))
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003262 return 1;
3263
Paul E. McKenney67e14c12018-04-27 16:01:46 -07003264 /* Have RCU grace period completed or started? */
3265 if (rcu_seq_current(&rnp->gp_seq) != rdp->gp_seq ||
Paul E. McKenney01c495f2018-01-10 12:36:00 -08003266 unlikely(READ_ONCE(rdp->gpwrap))) /* outside lock */
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003267 return 1;
3268
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003269 /* nothing to do */
3270 return 0;
3271}
3272
3273/*
Paul E. McKenneydd46a782018-07-10 18:37:30 -07003274 * Helper function for rcu_barrier() tracing. If tracing is disabled,
Paul E. McKenneya83eff02012-05-23 18:47:05 -07003275 * the compiler is expected to optimize this away.
3276 */
Paul E. McKenneydd46a782018-07-10 18:37:30 -07003277static void rcu_barrier_trace(const char *s, int cpu, unsigned long done)
Paul E. McKenneya83eff02012-05-23 18:47:05 -07003278{
Paul E. McKenney8344b872018-07-03 17:22:34 -07003279 trace_rcu_barrier(rcu_state.name, s, cpu,
3280 atomic_read(&rcu_state.barrier_cpu_count), done);
Paul E. McKenneya83eff02012-05-23 18:47:05 -07003281}
3282
3283/*
Paul E. McKenneydd46a782018-07-10 18:37:30 -07003284 * RCU callback function for rcu_barrier(). If we are last, wake
3285 * up the task executing rcu_barrier().
Paul E. McKenneyaa24f932020-01-20 15:43:45 -08003286 *
3287 * Note that the value of rcu_state.barrier_sequence must be captured
3288 * before the atomic_dec_and_test(). Otherwise, if this CPU is not last,
3289 * other CPUs might count the value down to zero before this CPU gets
3290 * around to invoking rcu_barrier_trace(), which might result in bogus
3291 * data from the next instance of rcu_barrier().
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08003292 */
Paul E. McKenney24ebbca2012-05-29 00:34:56 -07003293static void rcu_barrier_callback(struct rcu_head *rhp)
Paul E. McKenneyd0ec7742009-10-06 21:48:16 -07003294{
Paul E. McKenneyaa24f932020-01-20 15:43:45 -08003295 unsigned long __maybe_unused s = rcu_state.barrier_sequence;
3296
Paul E. McKenneyec9f5832018-07-05 16:26:12 -07003297 if (atomic_dec_and_test(&rcu_state.barrier_cpu_count)) {
Paul E. McKenneyaa24f932020-01-20 15:43:45 -08003298 rcu_barrier_trace(TPS("LastCB"), -1, s);
Paul E. McKenneyec9f5832018-07-05 16:26:12 -07003299 complete(&rcu_state.barrier_completion);
Paul E. McKenneya83eff02012-05-23 18:47:05 -07003300 } else {
Paul E. McKenneyaa24f932020-01-20 15:43:45 -08003301 rcu_barrier_trace(TPS("CB"), -1, s);
Paul E. McKenneya83eff02012-05-23 18:47:05 -07003302 }
Paul E. McKenneyd0ec7742009-10-06 21:48:16 -07003303}
3304
3305/*
3306 * Called with preemption disabled, and from cross-cpu IRQ context.
3307 */
Paul E. McKenney127e2982020-02-11 06:17:33 -08003308static void rcu_barrier_func(void *cpu_in)
Paul E. McKenneyd0ec7742009-10-06 21:48:16 -07003309{
Paul E. McKenney127e2982020-02-11 06:17:33 -08003310 uintptr_t cpu = (uintptr_t)cpu_in;
3311 struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
Paul E. McKenneyd0ec7742009-10-06 21:48:16 -07003312
Paul E. McKenneydd46a782018-07-10 18:37:30 -07003313 rcu_barrier_trace(TPS("IRQ"), -1, rcu_state.barrier_sequence);
Paul E. McKenneyf92c7342017-04-10 15:40:35 -07003314 rdp->barrier_head.func = rcu_barrier_callback;
3315 debug_rcu_head_queue(&rdp->barrier_head);
Paul E. McKenney5d6742b2019-05-15 09:56:40 -07003316 rcu_nocb_lock(rdp);
Paul E. McKenneyd1b222c2019-07-02 16:03:33 -07003317 WARN_ON_ONCE(!rcu_nocb_flush_bypass(rdp, NULL, jiffies));
Joel Fernandes (Google)77a40f92019-08-30 12:36:32 -04003318 if (rcu_segcblist_entrain(&rdp->cblist, &rdp->barrier_head)) {
Paul E. McKenneyec9f5832018-07-05 16:26:12 -07003319 atomic_inc(&rcu_state.barrier_cpu_count);
Paul E. McKenneyf92c7342017-04-10 15:40:35 -07003320 } else {
3321 debug_rcu_head_unqueue(&rdp->barrier_head);
Paul E. McKenneydd46a782018-07-10 18:37:30 -07003322 rcu_barrier_trace(TPS("IRQNQ"), -1,
Paul E. McKenney66e4c332019-08-12 16:14:00 -07003323 rcu_state.barrier_sequence);
Paul E. McKenneyf92c7342017-04-10 15:40:35 -07003324 }
Paul E. McKenney5d6742b2019-05-15 09:56:40 -07003325 rcu_nocb_unlock(rdp);
Paul E. McKenneyd0ec7742009-10-06 21:48:16 -07003326}
3327
Paul E. McKenneydd46a782018-07-10 18:37:30 -07003328/**
3329 * rcu_barrier - Wait until all in-flight call_rcu() callbacks complete.
3330 *
3331 * Note that this primitive does not necessarily wait for an RCU grace period
3332 * to complete. For example, if there are no RCU callbacks queued anywhere
3333 * in the system, then rcu_barrier() is within its rights to return
3334 * immediately, without waiting for anything, much less an RCU grace period.
Paul E. McKenneyd0ec7742009-10-06 21:48:16 -07003335 */
Paul E. McKenneydd46a782018-07-10 18:37:30 -07003336void rcu_barrier(void)
Paul E. McKenneyd0ec7742009-10-06 21:48:16 -07003337{
Paul E. McKenney127e2982020-02-11 06:17:33 -08003338 uintptr_t cpu;
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08003339 struct rcu_data *rdp;
Paul E. McKenneyec9f5832018-07-05 16:26:12 -07003340 unsigned long s = rcu_seq_snap(&rcu_state.barrier_sequence);
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08003341
Paul E. McKenneydd46a782018-07-10 18:37:30 -07003342 rcu_barrier_trace(TPS("Begin"), -1, s);
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08003343
Paul E. McKenneye74f4c42009-10-06 21:48:17 -07003344 /* Take mutex to serialize concurrent rcu_barrier() requests. */
Paul E. McKenneyec9f5832018-07-05 16:26:12 -07003345 mutex_lock(&rcu_state.barrier_mutex);
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08003346
Paul E. McKenney4f525a52015-06-26 11:20:00 -07003347 /* Did someone else do our work for us? */
Paul E. McKenneyec9f5832018-07-05 16:26:12 -07003348 if (rcu_seq_done(&rcu_state.barrier_sequence, s)) {
Paul E. McKenneydd46a782018-07-10 18:37:30 -07003349 rcu_barrier_trace(TPS("EarlyExit"), -1,
Paul E. McKenney66e4c332019-08-12 16:14:00 -07003350 rcu_state.barrier_sequence);
Paul E. McKenneycf3a9c42012-05-29 14:56:46 -07003351 smp_mb(); /* caller's subsequent code after above check. */
Paul E. McKenneyec9f5832018-07-05 16:26:12 -07003352 mutex_unlock(&rcu_state.barrier_mutex);
Paul E. McKenneycf3a9c42012-05-29 14:56:46 -07003353 return;
3354 }
3355
Paul E. McKenney4f525a52015-06-26 11:20:00 -07003356 /* Mark the start of the barrier operation. */
Paul E. McKenneyec9f5832018-07-05 16:26:12 -07003357 rcu_seq_start(&rcu_state.barrier_sequence);
Paul E. McKenneydd46a782018-07-10 18:37:30 -07003358 rcu_barrier_trace(TPS("Inc1"), -1, rcu_state.barrier_sequence);
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08003359
Paul E. McKenneyd0ec7742009-10-06 21:48:16 -07003360 /*
Paul E. McKenney127e2982020-02-11 06:17:33 -08003361 * Initialize the count to two rather than to zero in order
3362 * to avoid a too-soon return to zero in case of an immediate
3363 * invocation of the just-enqueued callback (or preemption of
3364 * this task). Exclude CPU-hotplug operations to ensure that no
3365 * offline non-offloaded CPU has callbacks queued.
Paul E. McKenneyd0ec7742009-10-06 21:48:16 -07003366 */
Paul E. McKenneyec9f5832018-07-05 16:26:12 -07003367 init_completion(&rcu_state.barrier_completion);
Paul E. McKenney127e2982020-02-11 06:17:33 -08003368 atomic_set(&rcu_state.barrier_cpu_count, 2);
Paul E. McKenney1331e7a2012-08-02 17:43:50 -07003369 get_online_cpus();
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08003370
3371 /*
Paul E. McKenney1331e7a2012-08-02 17:43:50 -07003372 * Force each CPU with callbacks to register a new callback.
3373 * When that callback is invoked, we will know that all of the
3374 * corresponding CPU's preceding callbacks have been invoked.
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08003375 */
Paul E. McKenney3fbfbf72012-08-19 21:35:53 -07003376 for_each_possible_cpu(cpu) {
Paul E. McKenneyda1df502018-07-03 15:37:16 -07003377 rdp = per_cpu_ptr(&rcu_data, cpu);
Paul E. McKenney127e2982020-02-11 06:17:33 -08003378 if (cpu_is_offline(cpu) &&
Paul E. McKenneyce5215c2019-04-12 15:58:34 -07003379 !rcu_segcblist_is_offloaded(&rdp->cblist))
3380 continue;
Paul E. McKenney127e2982020-02-11 06:17:33 -08003381 if (rcu_segcblist_n_cbs(&rdp->cblist) && cpu_online(cpu)) {
Paul E. McKenneydd46a782018-07-10 18:37:30 -07003382 rcu_barrier_trace(TPS("OnlineQ"), cpu,
Paul E. McKenney66e4c332019-08-12 16:14:00 -07003383 rcu_state.barrier_sequence);
Paul E. McKenney127e2982020-02-11 06:17:33 -08003384 smp_call_function_single(cpu, rcu_barrier_func, (void *)cpu, 1);
3385 } else if (rcu_segcblist_n_cbs(&rdp->cblist) &&
3386 cpu_is_offline(cpu)) {
3387 rcu_barrier_trace(TPS("OfflineNoCBQ"), cpu,
3388 rcu_state.barrier_sequence);
3389 local_irq_disable();
3390 rcu_barrier_func((void *)cpu);
3391 local_irq_enable();
3392 } else if (cpu_is_offline(cpu)) {
3393 rcu_barrier_trace(TPS("OfflineNoCBNoQ"), cpu,
3394 rcu_state.barrier_sequence);
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08003395 } else {
Paul E. McKenneydd46a782018-07-10 18:37:30 -07003396 rcu_barrier_trace(TPS("OnlineNQ"), cpu,
Paul E. McKenney66e4c332019-08-12 16:14:00 -07003397 rcu_state.barrier_sequence);
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08003398 }
3399 }
Paul E. McKenney1331e7a2012-08-02 17:43:50 -07003400 put_online_cpus();
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08003401
3402 /*
3403 * Now that we have an rcu_barrier_callback() callback on each
3404 * CPU, and thus each counted, remove the initial count.
3405 */
Paul E. McKenney127e2982020-02-11 06:17:33 -08003406 if (atomic_sub_and_test(2, &rcu_state.barrier_cpu_count))
Paul E. McKenneyec9f5832018-07-05 16:26:12 -07003407 complete(&rcu_state.barrier_completion);
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08003408
3409 /* Wait for all rcu_barrier_callback() callbacks to be invoked. */
Paul E. McKenneyec9f5832018-07-05 16:26:12 -07003410 wait_for_completion(&rcu_state.barrier_completion);
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08003411
Paul E. McKenney4f525a52015-06-26 11:20:00 -07003412 /* Mark the end of the barrier operation. */
Paul E. McKenneydd46a782018-07-10 18:37:30 -07003413 rcu_barrier_trace(TPS("Inc2"), -1, rcu_state.barrier_sequence);
Paul E. McKenneyec9f5832018-07-05 16:26:12 -07003414 rcu_seq_end(&rcu_state.barrier_sequence);
Paul E. McKenney4f525a52015-06-26 11:20:00 -07003415
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08003416 /* Other rcu_barrier() invocations can now safely proceed. */
Paul E. McKenneyec9f5832018-07-05 16:26:12 -07003417 mutex_unlock(&rcu_state.barrier_mutex);
Paul E. McKenneyd0ec7742009-10-06 21:48:16 -07003418}
Paul E. McKenney45975c72018-07-02 14:30:37 -07003419EXPORT_SYMBOL_GPL(rcu_barrier);
Paul E. McKenneyd0ec7742009-10-06 21:48:16 -07003420
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003421/*
Paul E. McKenney0aa04b02015-01-23 21:52:37 -08003422 * Propagate ->qsinitmask bits up the rcu_node tree to account for the
3423 * first CPU in a given leaf rcu_node structure coming online. The caller
3424 * must hold the corresponding leaf rcu_node ->lock with interrrupts
3425 * disabled.
3426 */
3427static void rcu_init_new_rnp(struct rcu_node *rnp_leaf)
3428{
3429 long mask;
Paul E. McKenney8d672fa2018-05-02 14:46:43 -07003430 long oldmask;
Paul E. McKenney0aa04b02015-01-23 21:52:37 -08003431 struct rcu_node *rnp = rnp_leaf;
3432
Paul E. McKenney8d672fa2018-05-02 14:46:43 -07003433 raw_lockdep_assert_held_rcu_node(rnp_leaf);
Paul E. McKenney962aff02018-05-02 12:49:21 -07003434 WARN_ON_ONCE(rnp->wait_blkd_tasks);
Paul E. McKenney0aa04b02015-01-23 21:52:37 -08003435 for (;;) {
3436 mask = rnp->grpmask;
3437 rnp = rnp->parent;
3438 if (rnp == NULL)
3439 return;
Paul E. McKenney6cf10082015-10-08 15:36:54 -07003440 raw_spin_lock_rcu_node(rnp); /* Interrupts already disabled. */
Paul E. McKenney8d672fa2018-05-02 14:46:43 -07003441 oldmask = rnp->qsmaskinit;
Paul E. McKenney0aa04b02015-01-23 21:52:37 -08003442 rnp->qsmaskinit |= mask;
Boqun Feng67c583a72015-12-29 12:18:47 +08003443 raw_spin_unlock_rcu_node(rnp); /* Interrupts remain disabled. */
Paul E. McKenney8d672fa2018-05-02 14:46:43 -07003444 if (oldmask)
3445 return;
Paul E. McKenney0aa04b02015-01-23 21:52:37 -08003446 }
3447}
3448
3449/*
Paul E. McKenney27569622009-08-15 09:53:46 -07003450 * Do boot-time initialization of a CPU's per-CPU RCU data.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003451 */
Paul E. McKenney27569622009-08-15 09:53:46 -07003452static void __init
Paul E. McKenney53b46302018-07-03 17:22:34 -07003453rcu_boot_init_percpu_data(int cpu)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003454{
Paul E. McKenneyda1df502018-07-03 15:37:16 -07003455 struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
Paul E. McKenney27569622009-08-15 09:53:46 -07003456
3457 /* Set up local state, ensuring consistent view of global state. */
Mark Rutlandbc75e992016-06-03 15:20:04 +01003458 rdp->grpmask = leaf_node_cpu_bit(rdp->mynode, cpu);
Paul E. McKenney4c5273b2018-08-03 21:00:38 -07003459 WARN_ON_ONCE(rdp->dynticks_nesting != 1);
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -07003460 WARN_ON_ONCE(rcu_dynticks_in_eqs(rcu_dynticks_snap(rdp)));
Paul E. McKenney53b46302018-07-03 17:22:34 -07003461 rdp->rcu_ofl_gp_seq = rcu_state.gp_seq;
Paul E. McKenney57738942018-05-08 14:18:57 -07003462 rdp->rcu_ofl_gp_flags = RCU_GP_CLEANED;
Paul E. McKenney53b46302018-07-03 17:22:34 -07003463 rdp->rcu_onl_gp_seq = rcu_state.gp_seq;
Paul E. McKenney57738942018-05-08 14:18:57 -07003464 rdp->rcu_onl_gp_flags = RCU_GP_CLEANED;
Paul E. McKenney27569622009-08-15 09:53:46 -07003465 rdp->cpu = cpu;
Paul E. McKenney3fbfbf72012-08-19 21:35:53 -07003466 rcu_boot_init_nocb_percpu_data(rdp);
Paul E. McKenney27569622009-08-15 09:53:46 -07003467}
3468
3469/*
Paul E. McKenney53b46302018-07-03 17:22:34 -07003470 * Invoked early in the CPU-online process, when pretty much all services
3471 * are available. The incoming CPU is not present.
3472 *
3473 * Initializes a CPU's per-CPU RCU data. Note that only one online or
Paul E. McKenneyff3bb6f2018-05-01 14:34:08 -07003474 * offline event can be happening at a given time. Note also that we can
3475 * accept some slop in the rsp->gp_seq access due to the fact that this
Paul E. McKenneye83e73f2019-05-14 09:50:49 -07003476 * CPU cannot possibly have any non-offloaded RCU callbacks in flight yet.
3477 * And any offloaded callbacks are being numbered elsewhere.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003478 */
Paul E. McKenney53b46302018-07-03 17:22:34 -07003479int rcutree_prepare_cpu(unsigned int cpu)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003480{
3481 unsigned long flags;
Paul E. McKenneyda1df502018-07-03 15:37:16 -07003482 struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
Paul E. McKenney336a4f62018-07-03 17:22:34 -07003483 struct rcu_node *rnp = rcu_get_root();
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003484
3485 /* Set up local state, ensuring consistent view of global state. */
Paul E. McKenney6cf10082015-10-08 15:36:54 -07003486 raw_spin_lock_irqsave_rcu_node(rnp, flags);
Paul E. McKenney37c72e52009-10-14 10:15:55 -07003487 rdp->qlen_last_fqs_check = 0;
Paul E. McKenney53b46302018-07-03 17:22:34 -07003488 rdp->n_force_qs_snap = rcu_state.n_force_qs;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003489 rdp->blimit = blimit;
Paul E. McKenney15fecf82017-02-08 12:36:42 -08003490 if (rcu_segcblist_empty(&rdp->cblist) && /* No early-boot CBs? */
Paul E. McKenneye83e73f2019-05-14 09:50:49 -07003491 !rcu_segcblist_is_offloaded(&rdp->cblist))
Paul E. McKenney15fecf82017-02-08 12:36:42 -08003492 rcu_segcblist_init(&rdp->cblist); /* Re-enable callbacks. */
Paul E. McKenney4c5273b2018-08-03 21:00:38 -07003493 rdp->dynticks_nesting = 1; /* CPU not up, no tearing. */
Paul E. McKenney2625d462016-11-02 14:23:30 -07003494 rcu_dynticks_eqs_online();
Boqun Feng67c583a72015-12-29 12:18:47 +08003495 raw_spin_unlock_rcu_node(rnp); /* irqs remain disabled. */
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003496
Paul E. McKenney0aa04b02015-01-23 21:52:37 -08003497 /*
3498 * Add CPU to leaf rcu_node pending-online bitmask. Any needed
3499 * propagation up the rcu_node tree will happen at the beginning
3500 * of the next grace period.
3501 */
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003502 rnp = rdp->mynode;
Peter Zijlstra2a67e742015-10-08 12:24:23 +02003503 raw_spin_lock_rcu_node(rnp); /* irqs already disabled. */
Paul E. McKenneyb9585e92015-07-31 16:04:45 -07003504 rdp->beenonline = true; /* We have now been online. */
Paul E. McKenney8ff372902020-01-04 11:33:17 -08003505 rdp->gp_seq = READ_ONCE(rnp->gp_seq);
3506 rdp->gp_seq_needed = rdp->gp_seq;
Paul E. McKenney5b74c452015-08-06 15:16:57 -07003507 rdp->cpu_no_qs.b.norm = true;
Paul E. McKenney97c668b2015-08-06 11:31:51 -07003508 rdp->core_needs_qs = false;
Paul E. McKenney9b9500d2017-08-17 17:05:59 -07003509 rdp->rcu_iw_pending = false;
Paul E. McKenney8ff372902020-01-04 11:33:17 -08003510 rdp->rcu_iw_gp_seq = rdp->gp_seq - 1;
Paul E. McKenney53b46302018-07-03 17:22:34 -07003511 trace_rcu_grace_period(rcu_state.name, rdp->gp_seq, TPS("cpuonl"));
Boqun Feng67c583a72015-12-29 12:18:47 +08003512 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
Thomas Gleixner4df83742016-07-13 17:17:03 +00003513 rcu_prepare_kthreads(cpu);
Paul E. McKenneyad368d12018-11-27 13:55:53 -08003514 rcu_spawn_cpu_nocb_kthread(cpu);
Thomas Gleixner4df83742016-07-13 17:17:03 +00003515
3516 return 0;
3517}
3518
Paul E. McKenneydeb34f32017-03-23 13:21:30 -07003519/*
3520 * Update RCU priority boot kthread affinity for CPU-hotplug changes.
3521 */
Thomas Gleixner4df83742016-07-13 17:17:03 +00003522static void rcutree_affinity_setting(unsigned int cpu, int outgoing)
3523{
Paul E. McKenneyda1df502018-07-03 15:37:16 -07003524 struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
Thomas Gleixner4df83742016-07-13 17:17:03 +00003525
3526 rcu_boost_kthread_setaffinity(rdp->mynode, outgoing);
3527}
3528
Paul E. McKenneydeb34f32017-03-23 13:21:30 -07003529/*
3530 * Near the end of the CPU-online process. Pretty much all services
3531 * enabled, and the CPU is now very much alive.
3532 */
Thomas Gleixner4df83742016-07-13 17:17:03 +00003533int rcutree_online_cpu(unsigned int cpu)
3534{
Paul E. McKenney9b9500d2017-08-17 17:05:59 -07003535 unsigned long flags;
3536 struct rcu_data *rdp;
3537 struct rcu_node *rnp;
Paul E. McKenney9b9500d2017-08-17 17:05:59 -07003538
Paul E. McKenneyb97d23c2018-07-04 15:35:00 -07003539 rdp = per_cpu_ptr(&rcu_data, cpu);
3540 rnp = rdp->mynode;
3541 raw_spin_lock_irqsave_rcu_node(rnp, flags);
3542 rnp->ffmask |= rdp->grpmask;
3543 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
Paul E. McKenney9b9500d2017-08-17 17:05:59 -07003544 if (rcu_scheduler_active == RCU_SCHEDULER_INACTIVE)
3545 return 0; /* Too early in boot for scheduler work. */
3546 sync_sched_exp_online_cleanup(cpu);
3547 rcutree_affinity_setting(cpu, -1);
Paul E. McKenney96926682019-08-02 15:12:47 -07003548
3549 // Stop-machine done, so allow nohz_full to disable tick.
3550 tick_dep_clear(TICK_DEP_BIT_RCU);
Thomas Gleixner4df83742016-07-13 17:17:03 +00003551 return 0;
3552}
3553
Paul E. McKenneydeb34f32017-03-23 13:21:30 -07003554/*
3555 * Near the beginning of the process. The CPU is still very much alive
3556 * with pretty much all services enabled.
3557 */
Thomas Gleixner4df83742016-07-13 17:17:03 +00003558int rcutree_offline_cpu(unsigned int cpu)
3559{
Paul E. McKenney9b9500d2017-08-17 17:05:59 -07003560 unsigned long flags;
3561 struct rcu_data *rdp;
3562 struct rcu_node *rnp;
Paul E. McKenney9b9500d2017-08-17 17:05:59 -07003563
Paul E. McKenneyb97d23c2018-07-04 15:35:00 -07003564 rdp = per_cpu_ptr(&rcu_data, cpu);
3565 rnp = rdp->mynode;
3566 raw_spin_lock_irqsave_rcu_node(rnp, flags);
3567 rnp->ffmask &= ~rdp->grpmask;
3568 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
Paul E. McKenney9b9500d2017-08-17 17:05:59 -07003569
Thomas Gleixner4df83742016-07-13 17:17:03 +00003570 rcutree_affinity_setting(cpu, cpu);
Paul E. McKenney96926682019-08-02 15:12:47 -07003571
3572 // nohz_full CPUs need the tick for stop-machine to work quickly
3573 tick_dep_set(TICK_DEP_BIT_RCU);
Thomas Gleixner4df83742016-07-13 17:17:03 +00003574 return 0;
3575}
3576
Peter Zijlstraf64c6012018-05-22 09:50:53 -07003577static DEFINE_PER_CPU(int, rcu_cpu_started);
3578
Paul E. McKenney7ec99de2016-06-30 13:58:26 -07003579/*
3580 * Mark the specified CPU as being online so that subsequent grace periods
3581 * (both expedited and normal) will wait on it. Note that this means that
3582 * incoming CPUs are not allowed to use RCU read-side critical sections
3583 * until this function is called. Failing to observe this restriction
3584 * will result in lockdep splats.
Paul E. McKenneydeb34f32017-03-23 13:21:30 -07003585 *
3586 * Note that this function is special in that it is invoked directly
3587 * from the incoming CPU rather than from the cpuhp_step mechanism.
3588 * This is because this function must be invoked at a precise location.
Paul E. McKenney7ec99de2016-06-30 13:58:26 -07003589 */
3590void rcu_cpu_starting(unsigned int cpu)
3591{
3592 unsigned long flags;
3593 unsigned long mask;
Paul E. McKenney313517fc2017-06-08 16:55:40 -07003594 int nbits;
3595 unsigned long oldmask;
Paul E. McKenney7ec99de2016-06-30 13:58:26 -07003596 struct rcu_data *rdp;
3597 struct rcu_node *rnp;
Paul E. McKenney7ec99de2016-06-30 13:58:26 -07003598
Peter Zijlstraf64c6012018-05-22 09:50:53 -07003599 if (per_cpu(rcu_cpu_started, cpu))
3600 return;
3601
3602 per_cpu(rcu_cpu_started, cpu) = 1;
3603
Paul E. McKenneyb97d23c2018-07-04 15:35:00 -07003604 rdp = per_cpu_ptr(&rcu_data, cpu);
3605 rnp = rdp->mynode;
3606 mask = rdp->grpmask;
3607 raw_spin_lock_irqsave_rcu_node(rnp, flags);
Paul E. McKenney105abf82020-01-03 15:44:23 -08003608 WRITE_ONCE(rnp->qsmaskinitnext, rnp->qsmaskinitnext | mask);
Paul E. McKenneyb97d23c2018-07-04 15:35:00 -07003609 oldmask = rnp->expmaskinitnext;
3610 rnp->expmaskinitnext |= mask;
3611 oldmask ^= rnp->expmaskinitnext;
3612 nbits = bitmap_weight(&oldmask, BITS_PER_LONG);
3613 /* Allow lockless access for expedited grace periods. */
Paul E. McKenneyeb7a6652018-07-05 17:47:45 -07003614 smp_store_release(&rcu_state.ncpus, rcu_state.ncpus + nbits); /* ^^^ */
Paul E. McKenney2f084692020-02-10 05:29:58 -08003615 ASSERT_EXCLUSIVE_WRITER(rcu_state.ncpus);
Paul E. McKenneyb97d23c2018-07-04 15:35:00 -07003616 rcu_gpnum_ovf(rnp, rdp); /* Offline-induced counter wrap? */
Paul E. McKenneyeb7a6652018-07-05 17:47:45 -07003617 rdp->rcu_onl_gp_seq = READ_ONCE(rcu_state.gp_seq);
3618 rdp->rcu_onl_gp_flags = READ_ONCE(rcu_state.gp_flags);
Paul E. McKenneyb97d23c2018-07-04 15:35:00 -07003619 if (rnp->qsmask & mask) { /* RCU waiting on incoming CPU? */
Joel Fernandes (Google)516e5ae2019-09-05 10:26:41 -07003620 rcu_disable_urgency_upon_qs(rdp);
Paul E. McKenneyb97d23c2018-07-04 15:35:00 -07003621 /* Report QS -after- changing ->qsmaskinitnext! */
3622 rcu_report_qs_rnp(mask, rnp, rnp->gp_seq, flags);
3623 } else {
3624 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
Paul E. McKenney7ec99de2016-06-30 13:58:26 -07003625 }
Paul E. McKenney313517fc2017-06-08 16:55:40 -07003626 smp_mb(); /* Ensure RCU read-side usage follows above initialization. */
Paul E. McKenney7ec99de2016-06-30 13:58:26 -07003627}
3628
Thomas Gleixner27d50c72016-02-26 18:43:44 +00003629#ifdef CONFIG_HOTPLUG_CPU
3630/*
Paul E. McKenneydeb34f32017-03-23 13:21:30 -07003631 * The outgoing function has no further need of RCU, so remove it from
Paul E. McKenney53b46302018-07-03 17:22:34 -07003632 * the rcu_node tree's ->qsmaskinitnext bit masks.
Paul E. McKenneydeb34f32017-03-23 13:21:30 -07003633 *
3634 * Note that this function is special in that it is invoked directly
3635 * from the outgoing CPU rather than from the cpuhp_step mechanism.
3636 * This is because this function must be invoked at a precise location.
3637 */
Thomas Gleixner27d50c72016-02-26 18:43:44 +00003638void rcu_report_dead(unsigned int cpu)
3639{
Paul E. McKenney53b46302018-07-03 17:22:34 -07003640 unsigned long flags;
3641 unsigned long mask;
3642 struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
3643 struct rcu_node *rnp = rdp->mynode; /* Outgoing CPU's rdp & rnp. */
Thomas Gleixner27d50c72016-02-26 18:43:44 +00003644
Paul E. McKenney49918a52018-07-07 18:12:26 -07003645 /* QS for any half-done expedited grace period. */
Thomas Gleixner27d50c72016-02-26 18:43:44 +00003646 preempt_disable();
Paul E. McKenney63d4c8c2018-07-03 17:22:34 -07003647 rcu_report_exp_rdp(this_cpu_ptr(&rcu_data));
Thomas Gleixner27d50c72016-02-26 18:43:44 +00003648 preempt_enable();
Paul E. McKenney3e310092018-06-21 12:50:01 -07003649 rcu_preempt_deferred_qs(current);
Paul E. McKenney53b46302018-07-03 17:22:34 -07003650
3651 /* Remove outgoing CPU from mask in the leaf rcu_node structure. */
3652 mask = rdp->grpmask;
Mike Galbraith894d45b2018-08-15 09:05:29 -07003653 raw_spin_lock(&rcu_state.ofl_lock);
Paul E. McKenney53b46302018-07-03 17:22:34 -07003654 raw_spin_lock_irqsave_rcu_node(rnp, flags); /* Enforce GP memory-order guarantee. */
3655 rdp->rcu_ofl_gp_seq = READ_ONCE(rcu_state.gp_seq);
3656 rdp->rcu_ofl_gp_flags = READ_ONCE(rcu_state.gp_flags);
3657 if (rnp->qsmask & mask) { /* RCU waiting on outgoing CPU? */
3658 /* Report quiescent state -before- changing ->qsmaskinitnext! */
3659 rcu_report_qs_rnp(mask, rnp, rnp->gp_seq, flags);
3660 raw_spin_lock_irqsave_rcu_node(rnp, flags);
3661 }
Paul E. McKenney105abf82020-01-03 15:44:23 -08003662 WRITE_ONCE(rnp->qsmaskinitnext, rnp->qsmaskinitnext & ~mask);
Paul E. McKenney53b46302018-07-03 17:22:34 -07003663 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
Mike Galbraith894d45b2018-08-15 09:05:29 -07003664 raw_spin_unlock(&rcu_state.ofl_lock);
Peter Zijlstraf64c6012018-05-22 09:50:53 -07003665
3666 per_cpu(rcu_cpu_started, cpu) = 0;
Thomas Gleixner27d50c72016-02-26 18:43:44 +00003667}
Paul E. McKenneya58163d2017-06-20 12:11:34 -07003668
Paul E. McKenney53b46302018-07-03 17:22:34 -07003669/*
3670 * The outgoing CPU has just passed through the dying-idle state, and we
3671 * are being invoked from the CPU that was IPIed to continue the offline
3672 * operation. Migrate the outgoing CPU's callbacks to the current CPU.
3673 */
3674void rcutree_migrate_callbacks(int cpu)
Paul E. McKenneya58163d2017-06-20 12:11:34 -07003675{
3676 unsigned long flags;
Paul E. McKenneyb1a2d792017-06-26 12:23:46 -07003677 struct rcu_data *my_rdp;
Paul E. McKenneyc00045be2019-04-16 14:09:15 -07003678 struct rcu_node *my_rnp;
Paul E. McKenneyda1df502018-07-03 15:37:16 -07003679 struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
Paul E. McKenneyec4eacc2018-04-22 08:49:24 -07003680 bool needwake;
Paul E. McKenneya58163d2017-06-20 12:11:34 -07003681
Paul E. McKenneyce5215c2019-04-12 15:58:34 -07003682 if (rcu_segcblist_is_offloaded(&rdp->cblist) ||
3683 rcu_segcblist_empty(&rdp->cblist))
Paul E. McKenney95335c02017-06-26 10:49:50 -07003684 return; /* No callbacks to migrate. */
3685
Paul E. McKenneyb1a2d792017-06-26 12:23:46 -07003686 local_irq_save(flags);
Paul E. McKenneyda1df502018-07-03 15:37:16 -07003687 my_rdp = this_cpu_ptr(&rcu_data);
Paul E. McKenneyc00045be2019-04-16 14:09:15 -07003688 my_rnp = my_rdp->mynode;
Paul E. McKenney5d6742b2019-05-15 09:56:40 -07003689 rcu_nocb_lock(my_rdp); /* irqs already disabled. */
Paul E. McKenneyd1b222c2019-07-02 16:03:33 -07003690 WARN_ON_ONCE(!rcu_nocb_flush_bypass(my_rdp, NULL, jiffies));
Paul E. McKenneyc00045be2019-04-16 14:09:15 -07003691 raw_spin_lock_rcu_node(my_rnp); /* irqs already disabled. */
Paul E. McKenneyec4eacc2018-04-22 08:49:24 -07003692 /* Leverage recent GPs and set GP for new callbacks. */
Paul E. McKenneyc00045be2019-04-16 14:09:15 -07003693 needwake = rcu_advance_cbs(my_rnp, rdp) ||
3694 rcu_advance_cbs(my_rnp, my_rdp);
Paul E. McKenneyf2dbe4a2017-06-27 07:44:06 -07003695 rcu_segcblist_merge(&my_rdp->cblist, &rdp->cblist);
Paul E. McKenney23651d92019-07-10 12:54:56 -07003696 needwake = needwake || rcu_advance_cbs(my_rnp, my_rdp);
Paul E. McKenneyc0352802019-05-21 08:28:41 -07003697 rcu_segcblist_disable(&rdp->cblist);
Paul E. McKenney09efeee2017-07-19 10:56:46 -07003698 WARN_ON_ONCE(rcu_segcblist_empty(&my_rdp->cblist) !=
3699 !rcu_segcblist_n_cbs(&my_rdp->cblist));
Paul E. McKenney5d6742b2019-05-15 09:56:40 -07003700 if (rcu_segcblist_is_offloaded(&my_rdp->cblist)) {
3701 raw_spin_unlock_rcu_node(my_rnp); /* irqs remain disabled. */
3702 __call_rcu_nocb_wake(my_rdp, true, flags);
3703 } else {
3704 rcu_nocb_unlock(my_rdp); /* irqs remain disabled. */
3705 raw_spin_unlock_irqrestore_rcu_node(my_rnp, flags);
3706 }
Paul E. McKenneyec4eacc2018-04-22 08:49:24 -07003707 if (needwake)
Paul E. McKenney532c00c2018-07-03 17:22:34 -07003708 rcu_gp_kthread_wake();
Paul E. McKenney5d6742b2019-05-15 09:56:40 -07003709 lockdep_assert_irqs_enabled();
Paul E. McKenneya58163d2017-06-20 12:11:34 -07003710 WARN_ONCE(rcu_segcblist_n_cbs(&rdp->cblist) != 0 ||
3711 !rcu_segcblist_empty(&rdp->cblist),
3712 "rcu_cleanup_dead_cpu: Callbacks on offline CPU %d: qlen=%lu, 1stCB=%p\n",
3713 cpu, rcu_segcblist_n_cbs(&rdp->cblist),
3714 rcu_segcblist_first_cb(&rdp->cblist));
3715}
Thomas Gleixner27d50c72016-02-26 18:43:44 +00003716#endif
3717
Paul E. McKenneydeb34f32017-03-23 13:21:30 -07003718/*
3719 * On non-huge systems, use expedited RCU grace periods to make suspend
3720 * and hibernation run faster.
3721 */
Borislav Petkovd1d74d12013-04-22 00:12:42 +02003722static int rcu_pm_notify(struct notifier_block *self,
3723 unsigned long action, void *hcpu)
3724{
3725 switch (action) {
3726 case PM_HIBERNATION_PREPARE:
3727 case PM_SUSPEND_PREPARE:
Paul E. McKenneye85e6a22019-01-10 15:30:15 -08003728 rcu_expedite_gp();
Borislav Petkovd1d74d12013-04-22 00:12:42 +02003729 break;
3730 case PM_POST_HIBERNATION:
3731 case PM_POST_SUSPEND:
Paul E. McKenneye85e6a22019-01-10 15:30:15 -08003732 rcu_unexpedite_gp();
Borislav Petkovd1d74d12013-04-22 00:12:42 +02003733 break;
3734 default:
3735 break;
3736 }
3737 return NOTIFY_OK;
3738}
3739
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003740/*
Paul E. McKenney49918a52018-07-07 18:12:26 -07003741 * Spawn the kthreads that handle RCU's grace periods.
Paul E. McKenneyb3dbec72012-06-18 18:36:08 -07003742 */
3743static int __init rcu_spawn_gp_kthread(void)
3744{
3745 unsigned long flags;
Paul E. McKenneya94844b2014-12-12 07:37:48 -08003746 int kthread_prio_in = kthread_prio;
Paul E. McKenneyb3dbec72012-06-18 18:36:08 -07003747 struct rcu_node *rnp;
Paul E. McKenneya94844b2014-12-12 07:37:48 -08003748 struct sched_param sp;
Paul E. McKenneyb3dbec72012-06-18 18:36:08 -07003749 struct task_struct *t;
3750
Paul E. McKenneya94844b2014-12-12 07:37:48 -08003751 /* Force priority into range. */
Joel Fernandes (Google)c7cd1612018-06-19 15:14:17 -07003752 if (IS_ENABLED(CONFIG_RCU_BOOST) && kthread_prio < 2
3753 && IS_BUILTIN(CONFIG_RCU_TORTURE_TEST))
3754 kthread_prio = 2;
3755 else if (IS_ENABLED(CONFIG_RCU_BOOST) && kthread_prio < 1)
Paul E. McKenneya94844b2014-12-12 07:37:48 -08003756 kthread_prio = 1;
3757 else if (kthread_prio < 0)
3758 kthread_prio = 0;
3759 else if (kthread_prio > 99)
3760 kthread_prio = 99;
Joel Fernandes (Google)c7cd1612018-06-19 15:14:17 -07003761
Paul E. McKenneya94844b2014-12-12 07:37:48 -08003762 if (kthread_prio != kthread_prio_in)
3763 pr_alert("rcu_spawn_gp_kthread(): Limited prio to %d from %d\n",
3764 kthread_prio, kthread_prio_in);
3765
Paul E. McKenney9386c0b2014-07-13 12:00:53 -07003766 rcu_scheduler_fully_active = 1;
Paul E. McKenneyb97d23c2018-07-04 15:35:00 -07003767 t = kthread_create(rcu_gp_kthread, NULL, "%s", rcu_state.name);
Paul E. McKenney08543bd2018-10-22 08:04:03 -07003768 if (WARN_ONCE(IS_ERR(t), "%s: Could not start grace-period kthread, OOM is now expected behavior\n", __func__))
3769 return 0;
Paul E. McKenneyb97d23c2018-07-04 15:35:00 -07003770 if (kthread_prio) {
3771 sp.sched_priority = kthread_prio;
3772 sched_setscheduler_nocheck(t, SCHED_FIFO, &sp);
Paul E. McKenneyb3dbec72012-06-18 18:36:08 -07003773 }
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003774 rnp = rcu_get_root();
Lai Jiangshan0c340292010-03-28 11:12:30 +08003775 raw_spin_lock_irqsave_rcu_node(rnp, flags);
Paul E. McKenney5648d652020-01-21 12:30:22 -08003776 WRITE_ONCE(rcu_state.gp_activity, jiffies);
3777 WRITE_ONCE(rcu_state.gp_req_activity, jiffies);
3778 // Reset .gp_activity and .gp_req_activity before setting .gp_kthread.
3779 smp_store_release(&rcu_state.gp_kthread, t); /* ^^^ */
Paul E. McKenneyb97d23c2018-07-04 15:35:00 -07003780 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
3781 wake_up_process(t);
Lai Jiangshan0c340292010-03-28 11:12:30 +08003782 rcu_spawn_nocb_kthreads();
Paul E. McKenney4a90a062010-04-14 16:48:11 -07003783 rcu_spawn_boost_kthreads();
Lai Jiangshan0c340292010-03-28 11:12:30 +08003784 return 0;
Lai Jiangshan394f99a2010-06-28 16:25:04 +08003785}
Lai Jiangshan0c340292010-03-28 11:12:30 +08003786early_initcall(rcu_spawn_gp_kthread);
3787
Paul E. McKenney6ce75a22012-06-12 11:01:13 -07003788/*
Paul E. McKenney52d7e482017-01-10 02:28:26 -08003789 * This function is invoked towards the end of the scheduler's
3790 * initialization process. Before this is called, the idle task might
3791 * contain synchronous grace-period primitives (during which time, this idle
3792 * task is booting the system, and such primitives are no-ops). After this
3793 * function is called, any synchronous grace-period primitives are run as
3794 * expedited, with the requesting task driving the grace period forward.
Paul E. McKenney900b1022017-02-10 14:32:54 -08003795 * A later core_initcall() rcu_set_runtime_mode() will switch to full
Paul E. McKenney52d7e482017-01-10 02:28:26 -08003796 * runtime RCU functionality.
Paul E. McKenneyf885b7f2012-04-23 15:52:53 -07003797 */
3798void rcu_scheduler_starting(void)
3799{
3800 WARN_ON(num_online_cpus() != 1);
3801 WARN_ON(nr_context_switches() > 0);
Paul E. McKenney52d7e482017-01-10 02:28:26 -08003802 rcu_test_sync_prims();
3803 rcu_scheduler_active = RCU_SCHEDULER_INIT;
3804 rcu_test_sync_prims();
Paul E. McKenneyf885b7f2012-04-23 15:52:53 -07003805}
3806
3807/*
Paul E. McKenney49918a52018-07-07 18:12:26 -07003808 * Helper function for rcu_init() that initializes the rcu_state structure.
Paul E. McKenney4102ada2013-10-08 20:23:47 -07003809 */
Paul E. McKenneyb8bb1f62018-07-03 17:22:34 -07003810static void __init rcu_init_one(void)
Paul E. McKenneyf885b7f2012-04-23 15:52:53 -07003811{
Alexander Gordeevcb007102015-06-03 08:18:30 +02003812 static const char * const buf[] = RCU_NODE_NAME_INIT;
3813 static const char * const fqs[] = RCU_FQS_NAME_INIT;
Paul E. McKenney3dc5dbe2015-09-26 14:51:24 -07003814 static struct lock_class_key rcu_node_class[RCU_NUM_LVLS];
3815 static struct lock_class_key rcu_fqs_class[RCU_NUM_LVLS];
Alexander Gordeev199977b2015-06-03 08:18:29 +02003816
Alexander Gordeev199977b2015-06-03 08:18:29 +02003817 int levelspread[RCU_NUM_LVLS]; /* kids/node in each level. */
Paul E. McKenney026ad282013-04-03 22:14:11 -07003818 int cpustride = 1;
3819 int i;
3820 int j;
3821 struct rcu_node *rnp;
3822
Alexander Gordeev05b84ae2015-06-03 08:18:28 +02003823 BUILD_BUG_ON(RCU_NUM_LVLS > ARRAY_SIZE(buf)); /* Fix buf[] init! */
Paul E. McKenney026ad282013-04-03 22:14:11 -07003824
Paul E. McKenney3eaaaf6c2015-03-09 16:51:17 -07003825 /* Silence gcc 4.8 false positive about array index out of range. */
3826 if (rcu_num_lvls <= 0 || rcu_num_lvls > RCU_NUM_LVLS)
3827 panic("rcu_init_one: rcu_num_lvls out of range");
Paul E. McKenney026ad282013-04-03 22:14:11 -07003828
Paul E. McKenneyf885b7f2012-04-23 15:52:53 -07003829 /* Initialize the level-tracking arrays. */
Paul E. McKenneyb17c7032012-09-06 15:38:02 -07003830
Paul E. McKenney39479092013-10-09 15:20:33 -07003831 for (i = 1; i < rcu_num_lvls; i++)
Paul E. McKenneyeb7a6652018-07-05 17:47:45 -07003832 rcu_state.level[i] =
3833 rcu_state.level[i - 1] + num_rcu_lvl[i - 1];
Paul E. McKenney41f5c632017-03-15 12:59:17 -07003834 rcu_init_levelspread(levelspread, num_rcu_lvl);
Paul E. McKenneyf885b7f2012-04-23 15:52:53 -07003835
Paul E. McKenney9f680ab2009-11-22 08:53:49 -08003836 /* Initialize the elements themselves, starting from the leaves. */
Paul E. McKenneyf41d9112009-08-22 13:56:52 -07003837
Paul E. McKenney017c4262010-01-14 16:10:58 -08003838 for (i = rcu_num_lvls - 1; i >= 0; i--) {
Alexander Gordeev199977b2015-06-03 08:18:29 +02003839 cpustride *= levelspread[i];
Paul E. McKenneyeb7a6652018-07-05 17:47:45 -07003840 rnp = rcu_state.level[i];
Paul E. McKenney41f5c632017-03-15 12:59:17 -07003841 for (j = 0; j < num_rcu_lvl[i]; j++, rnp++) {
Boqun Feng67c583a72015-12-29 12:18:47 +08003842 raw_spin_lock_init(&ACCESS_PRIVATE(rnp, lock));
3843 lockdep_set_class_and_name(&ACCESS_PRIVATE(rnp, lock),
Paul E. McKenneyf41d9112009-08-22 13:56:52 -07003844 &rcu_node_class[i], buf[i]);
Jiang Fangb5b39362013-02-02 14:13:42 -08003845 raw_spin_lock_init(&rnp->fqslock);
Paul E. McKenney9f680ab2009-11-22 08:53:49 -08003846 lockdep_set_class_and_name(&rnp->fqslock,
3847 &rcu_fqs_class[i], fqs[i]);
Paul E. McKenneyeb7a6652018-07-05 17:47:45 -07003848 rnp->gp_seq = rcu_state.gp_seq;
3849 rnp->gp_seq_needed = rcu_state.gp_seq;
3850 rnp->completedqs = rcu_state.gp_seq;
Paul E. McKenney9f680ab2009-11-22 08:53:49 -08003851 rnp->qsmask = 0;
3852 rnp->qsmaskinit = 0;
3853 rnp->grplo = j * cpustride;
Borislav Petkovd1d74d12013-04-22 00:12:42 +02003854 rnp->grphi = (j + 1) * cpustride - 1;
Paul E. McKenney017c4262010-01-14 16:10:58 -08003855 if (rnp->grphi >= nr_cpu_ids)
3856 rnp->grphi = nr_cpu_ids - 1;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003857 if (i == 0) {
3858 rnp->grpnum = 0;
Paul E. McKenney4102ada2013-10-08 20:23:47 -07003859 rnp->grpmask = 0;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003860 rnp->parent = NULL;
3861 } else {
Alexander Gordeev199977b2015-06-03 08:18:29 +02003862 rnp->grpnum = j % levelspread[i - 1];
Paul E. McKenneydf63fa5b2018-07-31 09:49:20 -07003863 rnp->grpmask = BIT(rnp->grpnum);
Paul E. McKenneyeb7a6652018-07-05 17:47:45 -07003864 rnp->parent = rcu_state.level[i - 1] +
Alexander Gordeev199977b2015-06-03 08:18:29 +02003865 j / levelspread[i - 1];
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003866 }
3867 rnp->level = i;
3868 INIT_LIST_HEAD(&rnp->blkd_tasks);
3869 rcu_init_one_nocb(rnp);
Paul E. McKenneyf6a12f32016-01-30 17:57:35 -08003870 init_waitqueue_head(&rnp->exp_wq[0]);
3871 init_waitqueue_head(&rnp->exp_wq[1]);
Paul E. McKenney3b5f6682016-03-16 16:47:55 -07003872 init_waitqueue_head(&rnp->exp_wq[2]);
3873 init_waitqueue_head(&rnp->exp_wq[3]);
Paul E. McKenneyf6a12f32016-01-30 17:57:35 -08003874 spin_lock_init(&rnp->exp_lock);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003875 }
3876 }
3877
Paul E. McKenneyeb7a6652018-07-05 17:47:45 -07003878 init_swait_queue_head(&rcu_state.gp_wq);
3879 init_swait_queue_head(&rcu_state.expedited_wq);
Paul E. McKenneyaedf4ba2018-07-04 14:33:59 -07003880 rnp = rcu_first_leaf_node();
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003881 for_each_possible_cpu(i) {
3882 while (i > rnp->grphi)
3883 rnp++;
Paul E. McKenneyda1df502018-07-03 15:37:16 -07003884 per_cpu_ptr(&rcu_data, i)->mynode = rnp;
Paul E. McKenney53b46302018-07-03 17:22:34 -07003885 rcu_boot_init_percpu_data(i);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003886 }
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003887}
3888
3889/*
3890 * Compute the rcu_node tree geometry from kernel parameters. This cannot
3891 * replace the definitions in tree.h because those are needed to size
3892 * the ->node array in the rcu_state structure.
3893 */
3894static void __init rcu_init_geometry(void)
3895{
3896 ulong d;
3897 int i;
Alexander Gordeev05b84ae2015-06-03 08:18:28 +02003898 int rcu_capacity[RCU_NUM_LVLS];
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003899
3900 /*
3901 * Initialize any unspecified boot parameters.
3902 * The default values of jiffies_till_first_fqs and
3903 * jiffies_till_next_fqs are set to the RCU_JIFFIES_TILL_FORCE_QS
3904 * value, which is a function of HZ, then adding one for each
3905 * RCU_JIFFIES_FQS_DIV CPUs that might be on the system.
3906 */
3907 d = RCU_JIFFIES_TILL_FORCE_QS + nr_cpu_ids / RCU_JIFFIES_FQS_DIV;
3908 if (jiffies_till_first_fqs == ULONG_MAX)
3909 jiffies_till_first_fqs = d;
3910 if (jiffies_till_next_fqs == ULONG_MAX)
3911 jiffies_till_next_fqs = d;
Neeraj Upadhyay69730322019-03-11 15:16:11 +05303912 adjust_jiffies_till_sched_qs();
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003913
3914 /* If the compile-time values are accurate, just leave. */
Paul E. McKenney47d631a2015-04-21 09:12:13 -07003915 if (rcu_fanout_leaf == RCU_FANOUT_LEAF &&
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003916 nr_cpu_ids == NR_CPUS)
3917 return;
Joe Perchesa7538352018-05-14 13:27:33 -07003918 pr_info("Adjusting geometry for rcu_fanout_leaf=%d, nr_cpu_ids=%u\n",
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003919 rcu_fanout_leaf, nr_cpu_ids);
3920
3921 /*
Paul E. McKenneyee968ac2015-07-31 08:28:35 -07003922 * The boot-time rcu_fanout_leaf parameter must be at least two
3923 * and cannot exceed the number of bits in the rcu_node masks.
3924 * Complain and fall back to the compile-time values if this
3925 * limit is exceeded.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003926 */
Paul E. McKenneyee968ac2015-07-31 08:28:35 -07003927 if (rcu_fanout_leaf < 2 ||
Alexander Gordeev75cf15a2015-06-03 08:18:23 +02003928 rcu_fanout_leaf > sizeof(unsigned long) * 8) {
Paul E. McKenney13bd64942015-06-04 10:06:01 -07003929 rcu_fanout_leaf = RCU_FANOUT_LEAF;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003930 WARN_ON(1);
3931 return;
3932 }
3933
Alexander Gordeev75cf15a2015-06-03 08:18:23 +02003934 /*
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003935 * Compute number of nodes that can be handled an rcu_node tree
Alexander Gordeev96181382015-06-03 08:18:26 +02003936 * with the given number of levels.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003937 */
Alexander Gordeev96181382015-06-03 08:18:26 +02003938 rcu_capacity[0] = rcu_fanout_leaf;
Alexander Gordeev05b84ae2015-06-03 08:18:28 +02003939 for (i = 1; i < RCU_NUM_LVLS; i++)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003940 rcu_capacity[i] = rcu_capacity[i - 1] * RCU_FANOUT;
3941
3942 /*
Alexander Gordeev75cf15a2015-06-03 08:18:23 +02003943 * The tree must be able to accommodate the configured number of CPUs.
Paul E. McKenneyee968ac2015-07-31 08:28:35 -07003944 * If this limit is exceeded, fall back to the compile-time values.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003945 */
Paul E. McKenneyee968ac2015-07-31 08:28:35 -07003946 if (nr_cpu_ids > rcu_capacity[RCU_NUM_LVLS - 1]) {
3947 rcu_fanout_leaf = RCU_FANOUT_LEAF;
3948 WARN_ON(1);
3949 return;
3950 }
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003951
Alexander Gordeev679f9852015-06-03 08:18:25 +02003952 /* Calculate the number of levels in the tree. */
Alexander Gordeev96181382015-06-03 08:18:26 +02003953 for (i = 0; nr_cpu_ids > rcu_capacity[i]; i++) {
Alexander Gordeev679f9852015-06-03 08:18:25 +02003954 }
Alexander Gordeev96181382015-06-03 08:18:26 +02003955 rcu_num_lvls = i + 1;
Alexander Gordeev679f9852015-06-03 08:18:25 +02003956
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003957 /* Calculate the number of rcu_nodes at each level of the tree. */
Alexander Gordeev679f9852015-06-03 08:18:25 +02003958 for (i = 0; i < rcu_num_lvls; i++) {
Alexander Gordeev96181382015-06-03 08:18:26 +02003959 int cap = rcu_capacity[(rcu_num_lvls - 1) - i];
Alexander Gordeev679f9852015-06-03 08:18:25 +02003960 num_rcu_lvl[i] = DIV_ROUND_UP(nr_cpu_ids, cap);
3961 }
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003962
3963 /* Calculate the total number of rcu_node structures. */
3964 rcu_num_nodes = 0;
Alexander Gordeev679f9852015-06-03 08:18:25 +02003965 for (i = 0; i < rcu_num_lvls; i++)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003966 rcu_num_nodes += num_rcu_lvl[i];
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003967}
3968
Paul E. McKenneya3dc2942015-04-20 11:40:50 -07003969/*
3970 * Dump out the structure of the rcu_node combining tree associated
Paul E. McKenney49918a52018-07-07 18:12:26 -07003971 * with the rcu_state structure.
Paul E. McKenneya3dc2942015-04-20 11:40:50 -07003972 */
Paul E. McKenneyb8bb1f62018-07-03 17:22:34 -07003973static void __init rcu_dump_rcu_node_tree(void)
Paul E. McKenneya3dc2942015-04-20 11:40:50 -07003974{
3975 int level = 0;
3976 struct rcu_node *rnp;
3977
3978 pr_info("rcu_node tree layout dump\n");
3979 pr_info(" ");
Paul E. McKenneyaedf4ba2018-07-04 14:33:59 -07003980 rcu_for_each_node_breadth_first(rnp) {
Paul E. McKenneya3dc2942015-04-20 11:40:50 -07003981 if (rnp->level != level) {
3982 pr_cont("\n");
3983 pr_info(" ");
3984 level = rnp->level;
3985 }
3986 pr_cont("%d:%d ^%d ", rnp->grplo, rnp->grphi, rnp->grpnum);
3987 }
3988 pr_cont("\n");
3989}
3990
Paul E. McKenneyad7c9462018-01-08 14:35:52 -08003991struct workqueue_struct *rcu_gp_wq;
Paul E. McKenney25f3d7e2018-02-01 22:05:38 -08003992struct workqueue_struct *rcu_par_gp_wq;
Paul E. McKenneyad7c9462018-01-08 14:35:52 -08003993
Byungchul Parka35d1692019-08-05 18:22:27 -04003994static void __init kfree_rcu_batch_init(void)
3995{
3996 int cpu;
Joel Fernandes (Google)0392beb2019-09-19 14:58:26 -07003997 int i;
Byungchul Parka35d1692019-08-05 18:22:27 -04003998
3999 for_each_possible_cpu(cpu) {
4000 struct kfree_rcu_cpu *krcp = per_cpu_ptr(&krc, cpu);
4001
4002 spin_lock_init(&krcp->lock);
Uladzislau Rezki (Sony)34c88172020-01-20 15:42:25 +01004003 for (i = 0; i < KFREE_N_BATCHES; i++) {
4004 INIT_RCU_WORK(&krcp->krw_arr[i].rcu_work, kfree_rcu_work);
Joel Fernandes (Google)0392beb2019-09-19 14:58:26 -07004005 krcp->krw_arr[i].krcp = krcp;
Uladzislau Rezki (Sony)34c88172020-01-20 15:42:25 +01004006 }
4007
Byungchul Parka35d1692019-08-05 18:22:27 -04004008 INIT_DELAYED_WORK(&krcp->monitor_work, kfree_rcu_monitor);
4009 krcp->initialized = true;
4010 }
4011}
4012
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01004013void __init rcu_init(void)
4014{
4015 int cpu;
4016
Paul E. McKenney47627672015-01-19 21:10:21 -08004017 rcu_early_boot_tests();
4018
Byungchul Parka35d1692019-08-05 18:22:27 -04004019 kfree_rcu_batch_init();
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01004020 rcu_bootup_announce();
4021 rcu_init_geometry();
Paul E. McKenneyb8bb1f62018-07-03 17:22:34 -07004022 rcu_init_one();
Paul E. McKenneya3dc2942015-04-20 11:40:50 -07004023 if (dump_tree)
Paul E. McKenneyb8bb1f62018-07-03 17:22:34 -07004024 rcu_dump_rcu_node_tree();
Sebastian Andrzej Siewior48d07c02019-03-20 22:13:33 +01004025 if (use_softirq)
4026 open_softirq(RCU_SOFTIRQ, rcu_core_si);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01004027
4028 /*
4029 * We don't need protection against CPU-hotplug here because
4030 * this is called early in boot, before either interrupts
4031 * or the scheduler are operational.
4032 */
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01004033 pm_notifier(rcu_pm_notify, 0);
Paul E. McKenney7ec99de2016-06-30 13:58:26 -07004034 for_each_online_cpu(cpu) {
Thomas Gleixner4df83742016-07-13 17:17:03 +00004035 rcutree_prepare_cpu(cpu);
Paul E. McKenney7ec99de2016-06-30 13:58:26 -07004036 rcu_cpu_starting(cpu);
Paul E. McKenney9b9500d2017-08-17 17:05:59 -07004037 rcutree_online_cpu(cpu);
Paul E. McKenney7ec99de2016-06-30 13:58:26 -07004038 }
Paul E. McKenneyad7c9462018-01-08 14:35:52 -08004039
4040 /* Create workqueue for expedited GPs and for Tree SRCU. */
4041 rcu_gp_wq = alloc_workqueue("rcu_gp", WQ_MEM_RECLAIM, 0);
4042 WARN_ON(!rcu_gp_wq);
Paul E. McKenney25f3d7e2018-02-01 22:05:38 -08004043 rcu_par_gp_wq = alloc_workqueue("rcu_par_gp", WQ_MEM_RECLAIM, 0);
4044 WARN_ON(!rcu_par_gp_wq);
Paul E. McKenneye0fcba92018-08-14 08:45:54 -07004045 srcu_init();
Paul E. McKenneyb2b00dd2019-10-30 11:56:10 -07004046
4047 /* Fill in default value for rcutree.qovld boot parameter. */
4048 /* -After- the rcu_node ->lock fields are initialized! */
4049 if (qovld < 0)
4050 qovld_calc = DEFAULT_RCU_QOVLD_MULT * qhimark;
4051 else
4052 qovld_calc = qovld;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01004053}
4054
Paul E. McKenney10462d62019-01-11 16:10:57 -08004055#include "tree_stall.h"
Paul E. McKenney3549c2b2016-04-15 16:35:29 -07004056#include "tree_exp.h"
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01004057#include "tree_plugin.h"