blob: b7f8c494d1d16e3b2ce51bee90bfb45eb792519b [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)
Ard Biesheuvela8a29b32014-07-12 19:01:49 +020091
Paul E. McKenney4c5273b2018-08-03 21:00:38 -070092static DEFINE_PER_CPU_SHARED_ALIGNED(struct rcu_data, rcu_data) = {
93 .dynticks_nesting = 1,
94 .dynticks_nmi_nesting = DYNTICK_IRQ_NONIDLE,
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -070095 .dynticks = ATOMIC_INIT(RCU_DYNTICK_CTRL_CTR),
Paul E. McKenney4c5273b2018-08-03 21:00:38 -070096};
Paul E. McKenneyc30fe542019-10-11 21:40:09 -070097static struct rcu_state rcu_state = {
Paul E. McKenney358be2d2018-07-03 14:15:31 -070098 .level = { &rcu_state.node[0] },
Paul E. McKenney358be2d2018-07-03 14:15:31 -070099 .gp_state = RCU_GP_IDLE,
100 .gp_seq = (0UL - 300UL) << RCU_SEQ_CTR_SHIFT,
101 .barrier_mutex = __MUTEX_INITIALIZER(rcu_state.barrier_mutex),
102 .name = RCU_NAME,
103 .abbr = RCU_ABBR,
104 .exp_mutex = __MUTEX_INITIALIZER(rcu_state.exp_mutex),
105 .exp_wake_mutex = __MUTEX_INITIALIZER(rcu_state.exp_wake_mutex),
Mike Galbraith894d45b2018-08-15 09:05:29 -0700106 .ofl_lock = __RAW_SPIN_LOCK_UNLOCKED(rcu_state.ofl_lock),
Paul E. McKenney358be2d2018-07-03 14:15:31 -0700107};
Paul E. McKenney27f4d282011-02-07 12:47:15 -0800108
Paul E. McKenneya3dc2942015-04-20 11:40:50 -0700109/* Dump rcu_node combining tree at boot to verify correct setup. */
110static bool dump_tree;
111module_param(dump_tree, bool, 0444);
Sebastian Andrzej Siewior48d07c02019-03-20 22:13:33 +0100112/* By default, use RCU_SOFTIRQ instead of rcuc kthreads. */
Jules Irengeda44cd62020-03-30 02:24:48 +0100113static bool use_softirq = true;
Sebastian Andrzej Siewior48d07c02019-03-20 22:13:33 +0100114module_param(use_softirq, bool, 0444);
Paul E. McKenney7fa27002015-04-20 10:27:15 -0700115/* Control rcu_node-tree auto-balancing at boot time. */
116static bool rcu_fanout_exact;
117module_param(rcu_fanout_exact, bool, 0444);
Paul E. McKenney47d631a2015-04-21 09:12:13 -0700118/* Increase (but not decrease) the RCU_FANOUT_LEAF at boot time. */
119static int rcu_fanout_leaf = RCU_FANOUT_LEAF;
Paul E. McKenney7e5c2df2012-07-01 15:42:33 -0700120module_param(rcu_fanout_leaf, int, 0444);
Paul E. McKenneyf885b7f2012-04-23 15:52:53 -0700121int rcu_num_lvls __read_mostly = RCU_NUM_LVLS;
Alexander Gordeevcb007102015-06-03 08:18:30 +0200122/* Number of rcu_nodes at specified level. */
Paul E. McKenneye95d68d2017-03-15 13:11:11 -0700123int num_rcu_lvl[] = NUM_RCU_LVL_INIT;
Paul E. McKenneyf885b7f2012-04-23 15:52:53 -0700124int rcu_num_nodes __read_mostly = NUM_RCU_NODES; /* Total # rcu_nodes in use. */
125
Paul E. McKenneyb0d30412011-07-10 15:57:35 -0700126/*
Paul E. McKenney52d7e482017-01-10 02:28:26 -0800127 * The rcu_scheduler_active variable is initialized to the value
128 * RCU_SCHEDULER_INACTIVE and transitions RCU_SCHEDULER_INIT just before the
129 * first task is spawned. So when this variable is RCU_SCHEDULER_INACTIVE,
130 * RCU can assume that there is but one task, allowing RCU to (for example)
Paul E. McKenney0d950922016-04-08 05:00:03 -0700131 * optimize synchronize_rcu() to a simple barrier(). When this variable
Paul E. McKenney52d7e482017-01-10 02:28:26 -0800132 * is RCU_SCHEDULER_INIT, RCU must actually do all the hard work required
133 * to detect real grace periods. This variable is also used to suppress
134 * boot-time false positives from lockdep-RCU error checking. Finally, it
135 * transitions from RCU_SCHEDULER_INIT to RCU_SCHEDULER_RUNNING after RCU
136 * is fully initialized, including all of its kthreads having been spawned.
Paul E. McKenneyb0d30412011-07-10 15:57:35 -0700137 */
Paul E. McKenneybbad9372010-04-02 16:17:17 -0700138int rcu_scheduler_active __read_mostly;
139EXPORT_SYMBOL_GPL(rcu_scheduler_active);
140
Paul E. McKenneyb0d30412011-07-10 15:57:35 -0700141/*
142 * The rcu_scheduler_fully_active variable transitions from zero to one
143 * during the early_initcall() processing, which is after the scheduler
144 * is capable of creating new tasks. So RCU processing (for example,
145 * creating tasks for RCU priority boosting) must be delayed until after
146 * rcu_scheduler_fully_active transitions from zero to one. We also
147 * currently delay invocation of any RCU callbacks until after this point.
148 *
149 * It might later prove better for people registering RCU callbacks during
150 * early boot to take responsibility for these callbacks, but one step at
151 * a time.
152 */
153static int rcu_scheduler_fully_active __read_mostly;
154
Paul E. McKenneyb50912d2018-07-03 17:22:34 -0700155static void rcu_report_qs_rnp(unsigned long mask, struct rcu_node *rnp,
156 unsigned long gps, unsigned long flags);
Paul E. McKenney0aa04b02015-01-23 21:52:37 -0800157static void rcu_init_new_rnp(struct rcu_node *rnp_leaf);
158static void rcu_cleanup_dead_rnp(struct rcu_node *rnp_leaf);
Thomas Gleixner5d01bbd2012-07-16 10:42:35 +0000159static void rcu_boost_kthread_setaffinity(struct rcu_node *rnp, int outgoingcpu);
Paul E. McKenneya46e0892011-06-15 15:47:09 -0700160static void invoke_rcu_core(void);
Paul E. McKenney63d4c8c2018-07-03 17:22:34 -0700161static void rcu_report_exp_rdp(struct rcu_data *rdp);
Paul E. McKenney3549c2b2016-04-15 16:35:29 -0700162static void sync_sched_exp_online_cleanup(int cpu);
Paul E. McKenneyb2b00dd2019-10-30 11:56:10 -0700163static void check_cb_ovld_locked(struct rcu_data *rdp, struct rcu_node *rnp);
Paul E. McKenneya26ac242011-01-12 14:10:23 -0800164
Paul E. McKenneya94844b2014-12-12 07:37:48 -0800165/* rcuc/rcub kthread realtime priority */
Paul E. McKenney26730f52015-04-21 09:22:14 -0700166static int kthread_prio = IS_ENABLED(CONFIG_RCU_BOOST) ? 1 : 0;
Liu Song3ffe3d12019-02-21 22:13:27 +0800167module_param(kthread_prio, int, 0444);
Paul E. McKenneya94844b2014-12-12 07:37:48 -0800168
Paul E. McKenney8d7dc922015-04-14 19:33:59 -0700169/* Delay in jiffies for grace-period initialization delays, debug only. */
Paul E. McKenney0f41c0d2015-03-10 18:33:20 -0700170
Paul E. McKenney90040c92017-05-10 14:36:55 -0700171static int gp_preinit_delay;
172module_param(gp_preinit_delay, int, 0444);
173static int gp_init_delay;
174module_param(gp_init_delay, int, 0444);
175static int gp_cleanup_delay;
176module_param(gp_cleanup_delay, int, 0444);
Paul E. McKenney0f41c0d2015-03-10 18:33:20 -0700177
Paul E. McKenney4cf439a2018-07-02 12:15:25 -0700178/* Retrieve RCU kthreads priority for rcutorture */
Joel Fernandes (Google)4babd852018-06-19 15:14:18 -0700179int rcu_get_gp_kthreads_prio(void)
180{
181 return kthread_prio;
182}
183EXPORT_SYMBOL_GPL(rcu_get_gp_kthreads_prio);
184
Paul E. McKenneyeab128e2015-04-15 12:08:22 -0700185/*
186 * Number of grace periods between delays, normalized by the duration of
Paul E. McKenneybfd090b2017-02-08 14:49:27 -0800187 * the delay. The longer the delay, the more the grace periods between
Paul E. McKenneyeab128e2015-04-15 12:08:22 -0700188 * each delay. The reason for this normalization is that it means that,
189 * for non-zero delays, the overall slowdown of grace periods is constant
190 * regardless of the duration of the delay. This arrangement balances
191 * the need for long delays to increase some race probabilities with the
192 * need for fast grace periods to increase other race probabilities.
193 */
194#define PER_RCU_NODE_PERIOD 3 /* Number of grace periods between delays. */
Paul E. McKenney37745d22015-01-22 18:24:08 -0800195
Paul E. McKenneya26ac242011-01-12 14:10:23 -0800196/*
Paul E. McKenney0aa04b02015-01-23 21:52:37 -0800197 * Compute the mask of online CPUs for the specified rcu_node structure.
198 * This will not be stable unless the rcu_node structure's ->lock is
199 * held, but the bit corresponding to the current CPU will be stable
200 * in most contexts.
201 */
Paul E. McKenneyc30fe542019-10-11 21:40:09 -0700202static unsigned long rcu_rnp_online_cpus(struct rcu_node *rnp)
Paul E. McKenney0aa04b02015-01-23 21:52:37 -0800203{
Paul E. McKenney7d0ae802015-03-03 14:57:58 -0800204 return READ_ONCE(rnp->qsmaskinitnext);
Paul E. McKenney0aa04b02015-01-23 21:52:37 -0800205}
206
207/*
Paul E. McKenney7d0ae802015-03-03 14:57:58 -0800208 * Return true if an RCU grace period is in progress. The READ_ONCE()s
Paul E. McKenneyfc2219d42009-09-23 09:50:41 -0700209 * permit this function to be invoked without holding the root rcu_node
210 * structure's ->lock, but of course results can be subject to change.
211 */
Paul E. McKenneyde8e8732018-07-03 17:22:34 -0700212static int rcu_gp_in_progress(void)
Paul E. McKenneyfc2219d42009-09-23 09:50:41 -0700213{
Paul E. McKenneyde8e8732018-07-03 17:22:34 -0700214 return rcu_seq_state(rcu_seq_current(&rcu_state.gp_seq));
Paul E. McKenneyfc2219d42009-09-23 09:50:41 -0700215}
216
Paul E. McKenney903ee832018-10-02 16:05:46 -0700217/*
218 * Return the number of callbacks queued on the specified CPU.
219 * Handles both the nocbs and normal cases.
220 */
221static long rcu_get_n_cbs_cpu(int cpu)
222{
223 struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
224
Paul E. McKenneyc0352802019-05-21 08:28:41 -0700225 if (rcu_segcblist_is_enabled(&rdp->cblist))
Paul E. McKenney903ee832018-10-02 16:05:46 -0700226 return rcu_segcblist_n_cbs(&rdp->cblist);
Paul E. McKenneyc0352802019-05-21 08:28:41 -0700227 return 0;
Paul E. McKenney903ee832018-10-02 16:05:46 -0700228}
229
Paul E. McKenneyd28139c2018-06-28 14:45:25 -0700230void rcu_softirq_qs(void)
Ingo Molnarb1f77b02009-03-13 03:20:49 +0100231{
Paul E. McKenney45975c72018-07-02 14:30:37 -0700232 rcu_qs();
Paul E. McKenneyd28139c2018-06-28 14:45:25 -0700233 rcu_preempt_deferred_qs(current);
Ingo Molnarb1f77b02009-03-13 03:20:49 +0100234}
235
Paul E. McKenney6563de92016-11-02 13:33:57 -0700236/*
Paul E. McKenney2625d462016-11-02 14:23:30 -0700237 * Record entry into an extended quiescent state. This is only to be
Paul E. McKenneyac3caf82020-03-12 17:01:57 -0700238 * called when not already in an extended quiescent state, that is,
239 * RCU is watching prior to the call to this function and is no longer
240 * watching upon return.
Paul E. McKenney2625d462016-11-02 14:23:30 -0700241 */
Thomas Gleixnerff5c4f52020-03-13 17:32:17 +0100242static noinstr void rcu_dynticks_eqs_enter(void)
Paul E. McKenney2625d462016-11-02 14:23:30 -0700243{
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -0700244 struct rcu_data *rdp = this_cpu_ptr(&rcu_data);
Paul E. McKenneyb8c17e62016-11-08 14:25:21 -0800245 int seq;
Paul E. McKenney2625d462016-11-02 14:23:30 -0700246
247 /*
Paul E. McKenneyb8c17e62016-11-08 14:25:21 -0800248 * CPUs seeing atomic_add_return() must see prior RCU read-side
Paul E. McKenney2625d462016-11-02 14:23:30 -0700249 * critical sections, and we also must force ordering with the
250 * next idle sojourn.
251 */
Paul E. McKenney7d0c9c52020-03-19 15:33:12 -0700252 rcu_dynticks_task_trace_enter(); // Before ->dynticks update!
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -0700253 seq = atomic_add_return(RCU_DYNTICK_CTRL_CTR, &rdp->dynticks);
Paul E. McKenneyac3caf82020-03-12 17:01:57 -0700254 // RCU is no longer watching. Better be in extended quiescent state!
Paul E. McKenneyb8c17e62016-11-08 14:25:21 -0800255 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
Paul E. McKenneyac3caf82020-03-12 17:01:57 -0700264 * called from an extended quiescent state, that is, RCU is not watching
265 * prior to the call to this function and is watching upon return.
Paul E. McKenney2625d462016-11-02 14:23:30 -0700266 */
Thomas Gleixnerff5c4f52020-03-13 17:32:17 +0100267static noinstr void rcu_dynticks_eqs_exit(void)
Paul E. McKenney2625d462016-11-02 14:23:30 -0700268{
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -0700269 struct rcu_data *rdp = this_cpu_ptr(&rcu_data);
Paul E. McKenneyb8c17e62016-11-08 14:25:21 -0800270 int seq;
Paul E. McKenney2625d462016-11-02 14:23:30 -0700271
272 /*
Paul E. McKenneyb8c17e62016-11-08 14:25:21 -0800273 * CPUs seeing atomic_add_return() must see prior idle sojourns,
Paul E. McKenney2625d462016-11-02 14:23:30 -0700274 * and we also must force ordering with the next RCU read-side
275 * critical section.
276 */
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -0700277 seq = atomic_add_return(RCU_DYNTICK_CTRL_CTR, &rdp->dynticks);
Paul E. McKenneyac3caf82020-03-12 17:01:57 -0700278 // RCU is now watching. Better not be in an extended quiescent state!
Paul E. McKenney7d0c9c52020-03-19 15:33:12 -0700279 rcu_dynticks_task_trace_exit(); // After ->dynticks update!
Paul E. McKenneyb8c17e62016-11-08 14:25:21 -0800280 WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) &&
281 !(seq & RCU_DYNTICK_CTRL_CTR));
282 if (seq & RCU_DYNTICK_CTRL_MASK) {
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -0700283 atomic_andnot(RCU_DYNTICK_CTRL_MASK, &rdp->dynticks);
Paul E. McKenneyb8c17e62016-11-08 14:25:21 -0800284 smp_mb__after_atomic(); /* _exit after clearing mask. */
Paul E. McKenneyb8c17e62016-11-08 14:25:21 -0800285 }
Paul E. McKenney2625d462016-11-02 14:23:30 -0700286}
287
288/*
289 * Reset the current CPU's ->dynticks counter to indicate that the
290 * newly onlined CPU is no longer in an extended quiescent state.
291 * This will either leave the counter unchanged, or increment it
292 * to the next non-quiescent value.
293 *
294 * The non-atomic test/increment sequence works because the upper bits
295 * of the ->dynticks counter are manipulated only by the corresponding CPU,
296 * or when the corresponding CPU is offline.
297 */
298static void rcu_dynticks_eqs_online(void)
299{
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -0700300 struct rcu_data *rdp = this_cpu_ptr(&rcu_data);
Paul E. McKenney2625d462016-11-02 14:23:30 -0700301
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -0700302 if (atomic_read(&rdp->dynticks) & RCU_DYNTICK_CTRL_CTR)
Paul E. McKenney2625d462016-11-02 14:23:30 -0700303 return;
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -0700304 atomic_add(RCU_DYNTICK_CTRL_CTR, &rdp->dynticks);
Paul E. McKenney2625d462016-11-02 14:23:30 -0700305}
306
307/*
Paul E. McKenney02a5c5502016-11-02 17:25:06 -0700308 * Is the current CPU in an extended quiescent state?
309 *
310 * No ordering, as we are sampling CPU-local information.
311 */
Thomas Gleixnerff5c4f52020-03-13 17:32:17 +0100312static __always_inline bool rcu_dynticks_curr_cpu_in_eqs(void)
Paul E. McKenney02a5c5502016-11-02 17:25:06 -0700313{
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -0700314 struct rcu_data *rdp = this_cpu_ptr(&rcu_data);
Paul E. McKenney02a5c5502016-11-02 17:25:06 -0700315
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -0700316 return !(atomic_read(&rdp->dynticks) & RCU_DYNTICK_CTRL_CTR);
Paul E. McKenney02a5c5502016-11-02 17:25:06 -0700317}
318
319/*
Paul E. McKenney8b2f63a2016-11-02 14:12:05 -0700320 * Snapshot the ->dynticks counter with full ordering so as to allow
321 * stable comparison of this counter with past and future snapshots.
322 */
Paul E. McKenneyc30fe542019-10-11 21:40:09 -0700323static int rcu_dynticks_snap(struct rcu_data *rdp)
Paul E. McKenney8b2f63a2016-11-02 14:12:05 -0700324{
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -0700325 int snap = atomic_add_return(0, &rdp->dynticks);
Paul E. McKenney8b2f63a2016-11-02 14:12:05 -0700326
Paul E. McKenneyb8c17e62016-11-08 14:25:21 -0800327 return snap & ~RCU_DYNTICK_CTRL_MASK;
Paul E. McKenney8b2f63a2016-11-02 14:12:05 -0700328}
329
330/*
Paul E. McKenney02a5c5502016-11-02 17:25:06 -0700331 * Return true if the snapshot returned from rcu_dynticks_snap()
332 * indicates that RCU is in an extended quiescent state.
333 */
334static bool rcu_dynticks_in_eqs(int snap)
335{
Paul E. McKenneyb8c17e62016-11-08 14:25:21 -0800336 return !(snap & RCU_DYNTICK_CTRL_CTR);
Paul E. McKenney02a5c5502016-11-02 17:25:06 -0700337}
338
339/*
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -0700340 * Return true if the CPU corresponding to the specified rcu_data
Paul E. McKenney02a5c5502016-11-02 17:25:06 -0700341 * structure has spent some time in an extended quiescent state since
342 * rcu_dynticks_snap() returned the specified snapshot.
343 */
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -0700344static bool rcu_dynticks_in_eqs_since(struct rcu_data *rdp, int snap)
Paul E. McKenney02a5c5502016-11-02 17:25:06 -0700345{
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -0700346 return snap != rcu_dynticks_snap(rdp);
Paul E. McKenney02a5c5502016-11-02 17:25:06 -0700347}
348
349/*
Paul E. McKenney7d0c9c52020-03-19 15:33:12 -0700350 * Return true if the referenced integer is zero while the specified
351 * CPU remains within a single extended quiescent state.
352 */
353bool rcu_dynticks_zero_in_eqs(int cpu, int *vp)
354{
355 struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
356 int snap;
357
358 // If not quiescent, force back to earlier extended quiescent state.
359 snap = atomic_read(&rdp->dynticks) & ~(RCU_DYNTICK_CTRL_MASK |
360 RCU_DYNTICK_CTRL_CTR);
361
362 smp_rmb(); // Order ->dynticks and *vp reads.
363 if (READ_ONCE(*vp))
364 return false; // Non-zero, so report failure;
365 smp_rmb(); // Order *vp read and ->dynticks re-read.
366
367 // If still in the same extended quiescent state, we are good!
368 return snap == (atomic_read(&rdp->dynticks) & ~RCU_DYNTICK_CTRL_MASK);
369}
370
371/*
Paul E. McKenneyb8c17e62016-11-08 14:25:21 -0800372 * Set the special (bottom) bit of the specified CPU so that it
373 * will take special action (such as flushing its TLB) on the
374 * next exit from an extended quiescent state. Returns true if
375 * the bit was successfully set, or false if the CPU was not in
376 * an extended quiescent state.
377 */
378bool rcu_eqs_special_set(int cpu)
379{
380 int old;
381 int new;
Paul E. McKenneyfaa059c2020-02-03 14:20:00 -0800382 int new_old;
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -0700383 struct rcu_data *rdp = &per_cpu(rcu_data, cpu);
Paul E. McKenneyb8c17e62016-11-08 14:25:21 -0800384
Paul E. McKenneyfaa059c2020-02-03 14:20:00 -0800385 new_old = atomic_read(&rdp->dynticks);
Paul E. McKenneyb8c17e62016-11-08 14:25:21 -0800386 do {
Paul E. McKenneyfaa059c2020-02-03 14:20:00 -0800387 old = new_old;
Paul E. McKenneyb8c17e62016-11-08 14:25:21 -0800388 if (old & RCU_DYNTICK_CTRL_CTR)
389 return false;
390 new = old | RCU_DYNTICK_CTRL_MASK;
Paul E. McKenneyfaa059c2020-02-03 14:20:00 -0800391 new_old = atomic_cmpxchg(&rdp->dynticks, old, new);
392 } while (new_old != old);
Paul E. McKenneyb8c17e62016-11-08 14:25:21 -0800393 return true;
Paul E. McKenney6563de92016-11-02 13:33:57 -0700394}
Paul E. McKenney5cd37192014-12-13 20:32:04 -0800395
Paul E. McKenney4a81e832014-06-20 16:49:01 -0700396/*
397 * Let the RCU core know that this CPU has gone through the scheduler,
398 * which is a quiescent state. This is called when the need for a
399 * quiescent state is urgent, so we burn an atomic operation and full
400 * memory barriers to let the RCU core know about it, regardless of what
401 * this CPU might (or might not) do in the near future.
402 *
Paul E. McKenney0f9be8c2017-01-27 13:17:02 -0800403 * We inform the RCU core by emulating a zero-duration dyntick-idle period.
Paul E. McKenney46a5d162015-10-07 09:10:48 -0700404 *
Paul E. McKenney3b57a392018-05-16 16:01:56 -0700405 * The caller must have disabled interrupts and must not be idle.
Paul E. McKenney4a81e832014-06-20 16:49:01 -0700406 */
Paul E. McKenney366237e2019-07-10 08:01:01 -0700407void rcu_momentary_dyntick_idle(void)
Paul E. McKenney4a81e832014-06-20 16:49:01 -0700408{
Paul E. McKenney3b57a392018-05-16 16:01:56 -0700409 int special;
410
Paul E. McKenney2dba13f2018-08-03 21:00:38 -0700411 raw_cpu_write(rcu_data.rcu_need_heavy_qs, false);
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -0700412 special = atomic_add_return(2 * RCU_DYNTICK_CTRL_CTR,
413 &this_cpu_ptr(&rcu_data)->dynticks);
Paul E. McKenney3b57a392018-05-16 16:01:56 -0700414 /* It is illegal to call this from idle state. */
415 WARN_ON_ONCE(!(special & RCU_DYNTICK_CTRL_CTR));
Paul E. McKenney3e310092018-06-21 12:50:01 -0700416 rcu_preempt_deferred_qs(current);
Paul E. McKenney4a81e832014-06-20 16:49:01 -0700417}
Paul E. McKenney79ba7ff2019-08-04 13:17:35 -0700418EXPORT_SYMBOL_GPL(rcu_momentary_dyntick_idle);
Paul E. McKenney4a81e832014-06-20 16:49:01 -0700419
Paul E. McKenney45975c72018-07-02 14:30:37 -0700420/**
Joel Fernandes (Google)eddded82019-03-26 15:24:09 -0400421 * rcu_is_cpu_rrupt_from_idle - see if interrupted from idle
Boqun Fengbb73c522015-07-30 16:55:38 -0700422 *
Joel Fernandes (Google)eddded82019-03-26 15:24:09 -0400423 * If the current CPU is idle and running at a first-level (not nested)
Paul E. McKenney45975c72018-07-02 14:30:37 -0700424 * interrupt from idle, return true. The caller must have at least
425 * disabled preemption.
Paul E. McKenney5cd37192014-12-13 20:32:04 -0800426 */
Paul E. McKenney45975c72018-07-02 14:30:37 -0700427static int rcu_is_cpu_rrupt_from_idle(void)
Paul E. McKenney5cd37192014-12-13 20:32:04 -0800428{
Joel Fernandes (Google)eddded82019-03-26 15:24:09 -0400429 /* Called only from within the scheduling-clock interrupt */
430 lockdep_assert_in_irq();
431
432 /* Check for counter underflows */
433 RCU_LOCKDEP_WARN(__this_cpu_read(rcu_data.dynticks_nesting) < 0,
434 "RCU dynticks_nesting counter underflow!");
435 RCU_LOCKDEP_WARN(__this_cpu_read(rcu_data.dynticks_nmi_nesting) <= 0,
436 "RCU dynticks_nmi_nesting counter underflow/zero!");
437
438 /* Are we at first interrupt nesting level? */
439 if (__this_cpu_read(rcu_data.dynticks_nmi_nesting) != 1)
440 return false;
441
442 /* Does CPU appear to be idle from an RCU standpoint? */
443 return __this_cpu_read(rcu_data.dynticks_nesting) == 0;
Paul E. McKenney5cd37192014-12-13 20:32:04 -0800444}
Paul E. McKenney5cd37192014-12-13 20:32:04 -0800445
Paul E. McKenneyd5a9a8c2019-04-10 17:01:39 -0700446#define DEFAULT_RCU_BLIMIT 10 /* Maximum callbacks per rcu_do_batch ... */
447#define DEFAULT_MAX_RCU_BLIMIT 10000 /* ... even during callback flood. */
Paul E. McKenney17c77982017-04-28 11:12:34 -0700448static long blimit = DEFAULT_RCU_BLIMIT;
449#define DEFAULT_RCU_QHIMARK 10000 /* If this many pending, ignore blimit. */
450static long qhimark = DEFAULT_RCU_QHIMARK;
451#define DEFAULT_RCU_QLOMARK 100 /* Once only this many pending, use blimit. */
452static long qlowmark = DEFAULT_RCU_QLOMARK;
Paul E. McKenneyb2b00dd2019-10-30 11:56:10 -0700453#define DEFAULT_RCU_QOVLD_MULT 2
454#define DEFAULT_RCU_QOVLD (DEFAULT_RCU_QOVLD_MULT * DEFAULT_RCU_QHIMARK)
455static long qovld = DEFAULT_RCU_QOVLD; /* If this many pending, hammer QS. */
456static long qovld_calc = -1; /* No pre-initialization lock acquisitions! */
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100457
Eric Dumazet878d7432012-10-18 04:55:36 -0700458module_param(blimit, long, 0444);
459module_param(qhimark, long, 0444);
460module_param(qlowmark, long, 0444);
Paul E. McKenneyb2b00dd2019-10-30 11:56:10 -0700461module_param(qovld, long, 0444);
Paul E. McKenney3d76c082009-09-28 07:46:32 -0700462
Paul E. McKenney026ad282013-04-03 22:14:11 -0700463static ulong jiffies_till_first_fqs = ULONG_MAX;
464static ulong jiffies_till_next_fqs = ULONG_MAX;
Paul E. McKenney8c7c4822016-01-03 20:29:57 -0800465static bool rcu_kick_kthreads;
Eric Dumazetcfcdef52019-07-24 18:07:52 -0700466static int rcu_divisor = 7;
467module_param(rcu_divisor, int, 0644);
468
469/* Force an exit from rcu_do_batch() after 3 milliseconds. */
470static long rcu_resched_ns = 3 * NSEC_PER_MSEC;
471module_param(rcu_resched_ns, long, 0644);
Paul E. McKenneyd40011f2012-06-26 20:45:57 -0700472
Paul E. McKenneyc06aed02018-07-25 11:25:23 -0700473/*
474 * How long the grace period must be before we start recruiting
475 * quiescent-state help from rcu_note_context_switch().
476 */
477static ulong jiffies_till_sched_qs = ULONG_MAX;
478module_param(jiffies_till_sched_qs, ulong, 0444);
Paul E. McKenney85f2b602019-03-11 15:45:13 -0700479static ulong jiffies_to_sched_qs; /* See adjust_jiffies_till_sched_qs(). */
Paul E. McKenneyc06aed02018-07-25 11:25:23 -0700480module_param(jiffies_to_sched_qs, ulong, 0444); /* Display only! */
481
482/*
483 * Make sure that we give the grace-period kthread time to detect any
484 * idle CPUs before taking active measures to force quiescent states.
485 * However, don't go below 100 milliseconds, adjusted upwards for really
486 * large systems.
487 */
488static void adjust_jiffies_till_sched_qs(void)
489{
490 unsigned long j;
491
492 /* If jiffies_till_sched_qs was specified, respect the request. */
493 if (jiffies_till_sched_qs != ULONG_MAX) {
494 WRITE_ONCE(jiffies_to_sched_qs, jiffies_till_sched_qs);
495 return;
496 }
Paul E. McKenney85f2b602019-03-11 15:45:13 -0700497 /* Otherwise, set to third fqs scan, but bound below on large system. */
Paul E. McKenneyc06aed02018-07-25 11:25:23 -0700498 j = READ_ONCE(jiffies_till_first_fqs) +
499 2 * READ_ONCE(jiffies_till_next_fqs);
500 if (j < HZ / 10 + nr_cpu_ids / RCU_JIFFIES_FQS_DIV)
501 j = HZ / 10 + nr_cpu_ids / RCU_JIFFIES_FQS_DIV;
502 pr_info("RCU calculated value of scheduler-enlistment delay is %ld jiffies.\n", j);
503 WRITE_ONCE(jiffies_to_sched_qs, j);
504}
505
Byungchul Park67abb962018-06-01 11:03:09 +0900506static int param_set_first_fqs_jiffies(const char *val, const struct kernel_param *kp)
507{
508 ulong j;
509 int ret = kstrtoul(val, 0, &j);
510
Paul E. McKenneyc06aed02018-07-25 11:25:23 -0700511 if (!ret) {
Byungchul Park67abb962018-06-01 11:03:09 +0900512 WRITE_ONCE(*(ulong *)kp->arg, (j > HZ) ? HZ : j);
Paul E. McKenneyc06aed02018-07-25 11:25:23 -0700513 adjust_jiffies_till_sched_qs();
514 }
Byungchul Park67abb962018-06-01 11:03:09 +0900515 return ret;
516}
517
518static int param_set_next_fqs_jiffies(const char *val, const struct kernel_param *kp)
519{
520 ulong j;
521 int ret = kstrtoul(val, 0, &j);
522
Paul E. McKenneyc06aed02018-07-25 11:25:23 -0700523 if (!ret) {
Byungchul Park67abb962018-06-01 11:03:09 +0900524 WRITE_ONCE(*(ulong *)kp->arg, (j > HZ) ? HZ : (j ?: 1));
Paul E. McKenneyc06aed02018-07-25 11:25:23 -0700525 adjust_jiffies_till_sched_qs();
526 }
Byungchul Park67abb962018-06-01 11:03:09 +0900527 return ret;
528}
529
530static struct kernel_param_ops first_fqs_jiffies_ops = {
531 .set = param_set_first_fqs_jiffies,
532 .get = param_get_ulong,
533};
534
535static struct kernel_param_ops next_fqs_jiffies_ops = {
536 .set = param_set_next_fqs_jiffies,
537 .get = param_get_ulong,
538};
539
540module_param_cb(jiffies_till_first_fqs, &first_fqs_jiffies_ops, &jiffies_till_first_fqs, 0644);
541module_param_cb(jiffies_till_next_fqs, &next_fqs_jiffies_ops, &jiffies_till_next_fqs, 0644);
Paul E. McKenney8c7c4822016-01-03 20:29:57 -0800542module_param(rcu_kick_kthreads, bool, 0644);
Paul E. McKenneyd40011f2012-06-26 20:45:57 -0700543
Paul E. McKenney8ff0b902018-07-05 17:55:14 -0700544static void force_qs_rnp(int (*f)(struct rcu_data *rdp));
Paul E. McKenneydd7dafd2019-09-14 03:39:22 -0700545static int rcu_pending(int user);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100546
547/*
Paul E. McKenney17ef2fe2018-04-27 11:39:34 -0700548 * Return the number of RCU GPs completed thus far for debug & stats.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100549 */
Paul E. McKenney17ef2fe2018-04-27 11:39:34 -0700550unsigned long rcu_get_gp_seq(void)
Paul E. McKenney917963d2014-11-21 17:10:16 -0800551{
Paul E. McKenney16fc9c62018-07-03 15:54:39 -0700552 return READ_ONCE(rcu_state.gp_seq);
Paul E. McKenney917963d2014-11-21 17:10:16 -0800553}
Paul E. McKenney17ef2fe2018-04-27 11:39:34 -0700554EXPORT_SYMBOL_GPL(rcu_get_gp_seq);
Paul E. McKenney917963d2014-11-21 17:10:16 -0800555
556/*
Paul E. McKenney291783b2016-01-12 13:43:30 -0800557 * Return the number of RCU expedited batches completed thus far for
558 * debug & stats. Odd numbers mean that a batch is in progress, even
559 * numbers mean idle. The value returned will thus be roughly double
560 * the cumulative batches since boot.
561 */
562unsigned long rcu_exp_batches_completed(void)
563{
Paul E. McKenney16fc9c62018-07-03 15:54:39 -0700564 return rcu_state.expedited_sequence;
Paul E. McKenney291783b2016-01-12 13:43:30 -0800565}
566EXPORT_SYMBOL_GPL(rcu_exp_batches_completed);
567
568/*
Paul E. McKenneyfd897572018-12-10 16:09:49 -0800569 * Return the root node of the rcu_state structure.
570 */
571static struct rcu_node *rcu_get_root(void)
572{
573 return &rcu_state.node[0];
574}
575
576/*
Paul E. McKenneyad0dc7f2014-02-19 10:51:42 -0800577 * Send along grace-period-related data for rcutorture diagnostics.
578 */
579void rcutorture_get_gp_data(enum rcutorture_type test_type, int *flags,
Paul E. McKenneyaebc8262018-05-01 06:42:51 -0700580 unsigned long *gp_seq)
Paul E. McKenneyad0dc7f2014-02-19 10:51:42 -0800581{
Paul E. McKenneyad0dc7f2014-02-19 10:51:42 -0800582 switch (test_type) {
583 case RCU_FLAVOR:
Paul E. McKenneyf7dd7d42018-07-04 15:39:40 -0700584 *flags = READ_ONCE(rcu_state.gp_flags);
585 *gp_seq = rcu_seq_current(&rcu_state.gp_seq);
Paul E. McKenneyad0dc7f2014-02-19 10:51:42 -0800586 break;
587 default:
588 break;
589 }
Paul E. McKenneyad0dc7f2014-02-19 10:51:42 -0800590}
591EXPORT_SYMBOL_GPL(rcutorture_get_gp_data);
592
593/*
Paul E. McKenney215bba92017-10-05 16:37:03 -0700594 * Enter an RCU extended quiescent state, which can be either the
595 * idle loop or adaptive-tickless usermode execution.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100596 *
Paul E. McKenney215bba92017-10-05 16:37:03 -0700597 * We crowbar the ->dynticks_nmi_nesting field to zero to allow for
598 * the possibility of usermode upcalls having messed up our count
599 * of interrupt nesting level during the prior busy period.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100600 */
Thomas Gleixnerff5c4f52020-03-13 17:32:17 +0100601static noinstr void rcu_eqs_enter(bool user)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100602{
Paul E. McKenney4c5273b2018-08-03 21:00:38 -0700603 struct rcu_data *rdp = this_cpu_ptr(&rcu_data);
Paul E. McKenney215bba92017-10-05 16:37:03 -0700604
Paul E. McKenney4c5273b2018-08-03 21:00:38 -0700605 WARN_ON_ONCE(rdp->dynticks_nmi_nesting != DYNTICK_IRQ_NONIDLE);
606 WRITE_ONCE(rdp->dynticks_nmi_nesting, 0);
Paul E. McKenney215bba92017-10-05 16:37:03 -0700607 WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) &&
Paul E. McKenney4c5273b2018-08-03 21:00:38 -0700608 rdp->dynticks_nesting == 0);
609 if (rdp->dynticks_nesting != 1) {
Paul E. McKenneyac3caf82020-03-12 17:01:57 -0700610 // RCU will still be watching, so just do accounting and leave.
Paul E. McKenney4c5273b2018-08-03 21:00:38 -0700611 rdp->dynticks_nesting--;
Paul E. McKenney215bba92017-10-05 16:37:03 -0700612 return;
613 }
Paul E. McKenney96d3fd02013-10-04 14:33:34 -0700614
Frederic Weisbeckerb04db8e2017-11-06 16:01:30 +0100615 lockdep_assert_irqs_disabled();
Thomas Gleixnerff5c4f52020-03-13 17:32:17 +0100616 instrumentation_begin();
Marco Elver6cf539a2019-10-09 17:57:43 +0200617 trace_rcu_dyntick(TPS("Start"), rdp->dynticks_nesting, 0, atomic_read(&rdp->dynticks));
Paul E. McKenneye68bbb22017-10-05 19:55:31 -0700618 WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) && !user && !is_idle_task(current));
Paul E. McKenneyb97d23c2018-07-04 15:35:00 -0700619 rdp = this_cpu_ptr(&rcu_data);
620 do_nocb_deferred_wakeup(rdp);
Paul E. McKenney198bbf82014-10-22 15:03:43 -0700621 rcu_prepare_for_idle();
Paul E. McKenney3e310092018-06-21 12:50:01 -0700622 rcu_preempt_deferred_qs(current);
Thomas Gleixnerff5c4f52020-03-13 17:32:17 +0100623 instrumentation_end();
Paul E. McKenney4c5273b2018-08-03 21:00:38 -0700624 WRITE_ONCE(rdp->dynticks_nesting, 0); /* Avoid irq-access tearing. */
Paul E. McKenneyac3caf82020-03-12 17:01:57 -0700625 // RCU is watching here ...
Paul E. McKenney844ccdd2017-10-03 16:51:47 -0700626 rcu_dynticks_eqs_enter();
Paul E. McKenneyac3caf82020-03-12 17:01:57 -0700627 // ... but is no longer watching here.
Paul E. McKenney176f8f72014-08-04 17:43:50 -0700628 rcu_dynticks_task_enter();
Frederic Weisbeckeradf50912012-06-28 11:20:21 -0700629}
630
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700631/**
632 * rcu_idle_enter - inform RCU that current CPU is entering idle
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100633 *
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700634 * Enter idle mode, in other words, -leave- the mode in which RCU
635 * read-side critical sections can occur. (Though RCU read-side
636 * critical sections can occur in irq handlers in idle, a possibility
637 * handled by irq_enter() and irq_exit().)
638 *
Paul E. McKenneyc0da3132017-09-22 09:58:47 -0700639 * If you add or remove a call to rcu_idle_enter(), be sure to test with
640 * CONFIG_RCU_EQS_DEBUG=y.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100641 */
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700642void rcu_idle_enter(void)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100643{
Frederic Weisbeckerb04db8e2017-11-06 16:01:30 +0100644 lockdep_assert_irqs_disabled();
Paul E. McKenneycb349ca2012-09-04 17:35:31 -0700645 rcu_eqs_enter(false);
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700646}
647
Paul E. McKenneyd1ec4c32015-05-13 10:41:58 -0700648#ifdef CONFIG_NO_HZ_FULL
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700649/**
Frederic Weisbeckeradf50912012-06-28 11:20:21 -0700650 * rcu_user_enter - inform RCU that we are resuming userspace.
651 *
652 * Enter RCU idle mode right before resuming userspace. No use of RCU
653 * is permitted between this call and rcu_user_exit(). This way the
654 * CPU doesn't need to maintain the tick for RCU maintenance purposes
655 * when the CPU runs in userspace.
Paul E. McKenneyc0da3132017-09-22 09:58:47 -0700656 *
657 * If you add or remove a call to rcu_user_enter(), be sure to test with
658 * CONFIG_RCU_EQS_DEBUG=y.
Frederic Weisbeckeradf50912012-06-28 11:20:21 -0700659 */
Thomas Gleixnerff5c4f52020-03-13 17:32:17 +0100660noinstr void rcu_user_enter(void)
Frederic Weisbeckeradf50912012-06-28 11:20:21 -0700661{
Frederic Weisbeckerb04db8e2017-11-06 16:01:30 +0100662 lockdep_assert_irqs_disabled();
Paul E. McKenneyd4db30a2017-07-12 09:03:35 -0700663 rcu_eqs_enter(true);
Frederic Weisbeckeradf50912012-06-28 11:20:21 -0700664}
Paul E. McKenneyd1ec4c32015-05-13 10:41:58 -0700665#endif /* CONFIG_NO_HZ_FULL */
Frederic Weisbecker19dd15912012-06-04 16:42:35 -0700666
Paul E. McKenney9ea366f2020-02-13 12:31:16 -0800667/**
668 * rcu_nmi_exit - inform RCU of exit from NMI context
669 *
Paul E. McKenneyfd581a92017-10-02 21:56:20 -0700670 * If we are returning from the outermost NMI handler that interrupted an
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -0700671 * RCU-idle period, update rdp->dynticks and rdp->dynticks_nmi_nesting
Paul E. McKenneyfd581a92017-10-02 21:56:20 -0700672 * to let the RCU grace-period handling know that the CPU is back to
673 * being RCU-idle.
674 *
Paul E. McKenney9ea366f2020-02-13 12:31:16 -0800675 * If you add or remove a call to rcu_nmi_exit(), be sure to test
Paul E. McKenneyfd581a92017-10-02 21:56:20 -0700676 * with CONFIG_RCU_EQS_DEBUG=y.
677 */
Paul E. McKenney9ea366f2020-02-13 12:31:16 -0800678noinstr void rcu_nmi_exit(void)
Paul E. McKenneyfd581a92017-10-02 21:56:20 -0700679{
Paul E. McKenney4c5273b2018-08-03 21:00:38 -0700680 struct rcu_data *rdp = this_cpu_ptr(&rcu_data);
Paul E. McKenneyfd581a92017-10-02 21:56:20 -0700681
682 /*
683 * Check for ->dynticks_nmi_nesting underflow and bad ->dynticks.
684 * (We are exiting an NMI handler, so RCU better be paying attention
685 * to us!)
686 */
Paul E. McKenney4c5273b2018-08-03 21:00:38 -0700687 WARN_ON_ONCE(rdp->dynticks_nmi_nesting <= 0);
Paul E. McKenneyfd581a92017-10-02 21:56:20 -0700688 WARN_ON_ONCE(rcu_dynticks_curr_cpu_in_eqs());
689
690 /*
691 * If the nesting level is not 1, the CPU wasn't RCU-idle, so
692 * leave it in non-RCU-idle state.
693 */
Paul E. McKenney4c5273b2018-08-03 21:00:38 -0700694 if (rdp->dynticks_nmi_nesting != 1) {
Thomas Gleixnerff5c4f52020-03-13 17:32:17 +0100695 instrumentation_begin();
Marco Elver6cf539a2019-10-09 17:57:43 +0200696 trace_rcu_dyntick(TPS("--="), rdp->dynticks_nmi_nesting, rdp->dynticks_nmi_nesting - 2,
697 atomic_read(&rdp->dynticks));
Paul E. McKenney4c5273b2018-08-03 21:00:38 -0700698 WRITE_ONCE(rdp->dynticks_nmi_nesting, /* No store tearing. */
699 rdp->dynticks_nmi_nesting - 2);
Thomas Gleixnerff5c4f52020-03-13 17:32:17 +0100700 instrumentation_end();
Paul E. McKenneyfd581a92017-10-02 21:56:20 -0700701 return;
702 }
703
Thomas Gleixnerff5c4f52020-03-13 17:32:17 +0100704 instrumentation_begin();
Paul E. McKenneyfd581a92017-10-02 21:56:20 -0700705 /* This NMI interrupted an RCU-idle CPU, restore RCU-idleness. */
Marco Elver6cf539a2019-10-09 17:57:43 +0200706 trace_rcu_dyntick(TPS("Startirq"), rdp->dynticks_nmi_nesting, 0, atomic_read(&rdp->dynticks));
Paul E. McKenney4c5273b2018-08-03 21:00:38 -0700707 WRITE_ONCE(rdp->dynticks_nmi_nesting, 0); /* Avoid store tearing. */
Byungchul Parkcf7614e2018-06-22 15:12:06 +0900708
Paul E. McKenney9ea366f2020-02-13 12:31:16 -0800709 if (!in_nmi())
Byungchul Parkcf7614e2018-06-22 15:12:06 +0900710 rcu_prepare_for_idle();
Thomas Gleixnerff5c4f52020-03-13 17:32:17 +0100711 instrumentation_end();
Byungchul Parkcf7614e2018-06-22 15:12:06 +0900712
Paul E. McKenneyac3caf82020-03-12 17:01:57 -0700713 // RCU is watching here ...
Paul E. McKenneyfd581a92017-10-02 21:56:20 -0700714 rcu_dynticks_eqs_enter();
Paul E. McKenneyac3caf82020-03-12 17:01:57 -0700715 // ... but is no longer watching here.
Byungchul Parkcf7614e2018-06-22 15:12:06 +0900716
Paul E. McKenney9ea366f2020-02-13 12:31:16 -0800717 if (!in_nmi())
Byungchul Parkcf7614e2018-06-22 15:12:06 +0900718 rcu_dynticks_task_enter();
719}
720
721/**
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700722 * rcu_irq_exit - inform RCU that current CPU is exiting irq towards idle
723 *
724 * Exit from an interrupt handler, which might possibly result in entering
725 * idle mode, in other words, leaving the mode in which read-side critical
Paul E. McKenney7c9906c2015-10-31 00:59:01 -0700726 * sections can occur. The caller must have disabled interrupts.
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700727 *
728 * This code assumes that the idle loop never does anything that might
729 * result in unbalanced calls to irq_enter() and irq_exit(). If your
Paul E. McKenney58721f52017-10-03 10:42:22 -0700730 * architecture's idle loop violates this assumption, RCU will give you what
731 * you deserve, good and hard. But very infrequently and irreproducibly.
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700732 *
733 * Use things like work queues to work around this limitation.
734 *
735 * You have been warned.
Paul E. McKenneyc0da3132017-09-22 09:58:47 -0700736 *
737 * If you add or remove a call to rcu_irq_exit(), be sure to test with
738 * CONFIG_RCU_EQS_DEBUG=y.
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700739 */
Thomas Gleixnerff5c4f52020-03-13 17:32:17 +0100740void noinstr rcu_irq_exit(void)
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700741{
Frederic Weisbeckerb04db8e2017-11-06 16:01:30 +0100742 lockdep_assert_irqs_disabled();
Paul E. McKenney9ea366f2020-02-13 12:31:16 -0800743 rcu_nmi_exit();
Paul E. McKenney7c9906c2015-10-31 00:59:01 -0700744}
745
Thomas Gleixner8ae0ae62020-05-03 15:08:52 +0200746/**
747 * rcu_irq_exit_preempt - Inform RCU that current CPU is exiting irq
748 * towards in kernel preemption
749 *
750 * Same as rcu_irq_exit() but has a sanity check that scheduling is safe
751 * from RCU point of view. Invoked from return from interrupt before kernel
752 * preemption.
753 */
754void rcu_irq_exit_preempt(void)
755{
756 lockdep_assert_irqs_disabled();
757 rcu_nmi_exit();
758
759 RCU_LOCKDEP_WARN(__this_cpu_read(rcu_data.dynticks_nesting) <= 0,
760 "RCU dynticks_nesting counter underflow/zero!");
761 RCU_LOCKDEP_WARN(__this_cpu_read(rcu_data.dynticks_nmi_nesting) !=
762 DYNTICK_IRQ_NONIDLE,
763 "Bad RCU dynticks_nmi_nesting counter\n");
764 RCU_LOCKDEP_WARN(rcu_dynticks_curr_cpu_in_eqs(),
765 "RCU in extended quiescent state!");
766}
767
Paul E. McKenney7c9906c2015-10-31 00:59:01 -0700768/*
769 * Wrapper for rcu_irq_exit() where interrupts are enabled.
Paul E. McKenneyc0da3132017-09-22 09:58:47 -0700770 *
771 * If you add or remove a call to rcu_irq_exit_irqson(), be sure to test
772 * with CONFIG_RCU_EQS_DEBUG=y.
Paul E. McKenney7c9906c2015-10-31 00:59:01 -0700773 */
774void rcu_irq_exit_irqson(void)
775{
776 unsigned long flags;
777
778 local_irq_save(flags);
779 rcu_irq_exit();
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700780 local_irq_restore(flags);
781}
782
783/*
Frederic Weisbeckeradf50912012-06-28 11:20:21 -0700784 * Exit an RCU extended quiescent state, which can be either the
785 * idle loop or adaptive-tickless usermode execution.
Paul E. McKenney51a1fd32017-10-03 14:43:40 -0700786 *
787 * We crowbar the ->dynticks_nmi_nesting field to DYNTICK_IRQ_NONIDLE to
788 * allow for the possibility of usermode upcalls messing up our count of
789 * interrupt nesting level during the busy period that is just now starting.
Frederic Weisbeckeradf50912012-06-28 11:20:21 -0700790 */
Thomas Gleixnerff5c4f52020-03-13 17:32:17 +0100791static void noinstr rcu_eqs_exit(bool user)
Frederic Weisbeckeradf50912012-06-28 11:20:21 -0700792{
Paul E. McKenney4c5273b2018-08-03 21:00:38 -0700793 struct rcu_data *rdp;
Paul E. McKenney84585aa2017-10-04 15:55:16 -0700794 long oldval;
Frederic Weisbeckeradf50912012-06-28 11:20:21 -0700795
Frederic Weisbeckerb04db8e2017-11-06 16:01:30 +0100796 lockdep_assert_irqs_disabled();
Paul E. McKenney4c5273b2018-08-03 21:00:38 -0700797 rdp = this_cpu_ptr(&rcu_data);
798 oldval = rdp->dynticks_nesting;
Paul E. McKenney1ce46ee2015-05-05 23:04:22 -0700799 WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) && oldval < 0);
Paul E. McKenney51a1fd32017-10-03 14:43:40 -0700800 if (oldval) {
Paul E. McKenneyac3caf82020-03-12 17:01:57 -0700801 // RCU was already watching, so just do accounting and leave.
Paul E. McKenney4c5273b2018-08-03 21:00:38 -0700802 rdp->dynticks_nesting++;
Paul E. McKenney9dd238e2017-10-05 16:56:26 -0700803 return;
Paul E. McKenney3a5924052013-10-04 18:48:55 -0700804 }
Paul E. McKenney9dd238e2017-10-05 16:56:26 -0700805 rcu_dynticks_task_exit();
Paul E. McKenneyac3caf82020-03-12 17:01:57 -0700806 // RCU is not watching here ...
Paul E. McKenney9dd238e2017-10-05 16:56:26 -0700807 rcu_dynticks_eqs_exit();
Paul E. McKenneyac3caf82020-03-12 17:01:57 -0700808 // ... but is watching here.
Thomas Gleixnerff5c4f52020-03-13 17:32:17 +0100809 instrumentation_begin();
Paul E. McKenney9dd238e2017-10-05 16:56:26 -0700810 rcu_cleanup_after_idle();
Marco Elver6cf539a2019-10-09 17:57:43 +0200811 trace_rcu_dyntick(TPS("End"), rdp->dynticks_nesting, 1, atomic_read(&rdp->dynticks));
Paul E. McKenneye68bbb22017-10-05 19:55:31 -0700812 WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) && !user && !is_idle_task(current));
Paul E. McKenney4c5273b2018-08-03 21:00:38 -0700813 WRITE_ONCE(rdp->dynticks_nesting, 1);
814 WARN_ON_ONCE(rdp->dynticks_nmi_nesting);
815 WRITE_ONCE(rdp->dynticks_nmi_nesting, DYNTICK_IRQ_NONIDLE);
Thomas Gleixnerff5c4f52020-03-13 17:32:17 +0100816 instrumentation_end();
Frederic Weisbeckeradf50912012-06-28 11:20:21 -0700817}
818
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700819/**
820 * rcu_idle_exit - inform RCU that current CPU is leaving idle
821 *
822 * Exit idle mode, in other words, -enter- the mode in which RCU
823 * read-side critical sections can occur.
824 *
Paul E. McKenneyc0da3132017-09-22 09:58:47 -0700825 * If you add or remove a call to rcu_idle_exit(), be sure to test with
826 * CONFIG_RCU_EQS_DEBUG=y.
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700827 */
828void rcu_idle_exit(void)
829{
Frederic Weisbeckerc5d900b2012-07-11 20:26:31 +0200830 unsigned long flags;
831
832 local_irq_save(flags);
Paul E. McKenneycb349ca2012-09-04 17:35:31 -0700833 rcu_eqs_exit(false);
Frederic Weisbeckerc5d900b2012-07-11 20:26:31 +0200834 local_irq_restore(flags);
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700835}
836
Paul E. McKenneyd1ec4c32015-05-13 10:41:58 -0700837#ifdef CONFIG_NO_HZ_FULL
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700838/**
Frederic Weisbeckeradf50912012-06-28 11:20:21 -0700839 * rcu_user_exit - inform RCU that we are exiting userspace.
840 *
841 * Exit RCU idle mode while entering the kernel because it can
842 * run a RCU read side critical section anytime.
Paul E. McKenneyc0da3132017-09-22 09:58:47 -0700843 *
844 * If you add or remove a call to rcu_user_exit(), be sure to test with
845 * CONFIG_RCU_EQS_DEBUG=y.
Frederic Weisbeckeradf50912012-06-28 11:20:21 -0700846 */
Thomas Gleixnerff5c4f52020-03-13 17:32:17 +0100847void noinstr rcu_user_exit(void)
Frederic Weisbeckeradf50912012-06-28 11:20:21 -0700848{
Frederic Weisbecker91d1aa432012-11-27 19:33:25 +0100849 rcu_eqs_exit(1);
Frederic Weisbeckeradf50912012-06-28 11:20:21 -0700850}
Paul E. McKenneyaaf2bc52020-05-21 22:05:15 +0200851
852/**
853 * __rcu_irq_enter_check_tick - Enable scheduler tick on CPU if RCU needs it.
854 *
855 * The scheduler tick is not normally enabled when CPUs enter the kernel
856 * from nohz_full userspace execution. After all, nohz_full userspace
857 * execution is an RCU quiescent state and the time executing in the kernel
858 * is quite short. Except of course when it isn't. And it is not hard to
859 * cause a large system to spend tens of seconds or even minutes looping
860 * in the kernel, which can cause a number of problems, include RCU CPU
861 * stall warnings.
862 *
863 * Therefore, if a nohz_full CPU fails to report a quiescent state
864 * in a timely manner, the RCU grace-period kthread sets that CPU's
865 * ->rcu_urgent_qs flag with the expectation that the next interrupt or
866 * exception will invoke this function, which will turn on the scheduler
867 * tick, which will enable RCU to detect that CPU's quiescent states,
868 * for example, due to cond_resched() calls in CONFIG_PREEMPT=n kernels.
869 * The tick will be disabled once a quiescent state is reported for
870 * this CPU.
871 *
872 * Of course, in carefully tuned systems, there might never be an
873 * interrupt or exception. In that case, the RCU grace-period kthread
874 * will eventually cause one to happen. However, in less carefully
875 * controlled environments, this function allows RCU to get what it
876 * needs without creating otherwise useless interruptions.
877 */
878void __rcu_irq_enter_check_tick(void)
879{
880 struct rcu_data *rdp = this_cpu_ptr(&rcu_data);
881
882 // Enabling the tick is unsafe in NMI handlers.
883 if (WARN_ON_ONCE(in_nmi()))
884 return;
885
886 RCU_LOCKDEP_WARN(rcu_dynticks_curr_cpu_in_eqs(),
887 "Illegal rcu_irq_enter_check_tick() from extended quiescent state");
888
889 if (!tick_nohz_full_cpu(rdp->cpu) ||
890 !READ_ONCE(rdp->rcu_urgent_qs) ||
891 READ_ONCE(rdp->rcu_forced_tick)) {
892 // RCU doesn't need nohz_full help from this CPU, or it is
893 // already getting that help.
894 return;
895 }
896
897 // We get here only when not in an extended quiescent state and
898 // from interrupts (as opposed to NMIs). Therefore, (1) RCU is
899 // already watching and (2) The fact that we are in an interrupt
900 // handler and that the rcu_node lock is an irq-disabled lock
901 // prevents self-deadlock. So we can safely recheck under the lock.
902 // Note that the nohz_full state currently cannot change.
903 raw_spin_lock_rcu_node(rdp->mynode);
904 if (rdp->rcu_urgent_qs && !rdp->rcu_forced_tick) {
905 // A nohz_full CPU is in the kernel and RCU needs a
906 // quiescent state. Turn on the tick!
907 WRITE_ONCE(rdp->rcu_forced_tick, true);
908 tick_dep_set_cpu(rdp->cpu, TICK_DEP_BIT_RCU);
909 }
910 raw_spin_unlock_rcu_node(rdp->mynode);
911}
Paul E. McKenneyd1ec4c32015-05-13 10:41:58 -0700912#endif /* CONFIG_NO_HZ_FULL */
Frederic Weisbecker19dd15912012-06-04 16:42:35 -0700913
914/**
Paul E. McKenney9ea366f2020-02-13 12:31:16 -0800915 * rcu_nmi_enter - inform RCU of entry to NMI context
Byungchul Parkcf7614e2018-06-22 15:12:06 +0900916 * @irq: Is this call from rcu_irq_enter?
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100917 *
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -0700918 * If the CPU was idle from RCU's viewpoint, update rdp->dynticks and
Paul E. McKenney4c5273b2018-08-03 21:00:38 -0700919 * rdp->dynticks_nmi_nesting to let the RCU grace-period handling know
Paul E. McKenney734d1682014-11-21 14:45:12 -0800920 * that the CPU is active. This implementation permits nested NMIs, as
921 * long as the nesting level does not overflow an int. (You will probably
922 * run out of stack space first.)
Paul E. McKenneyc0da3132017-09-22 09:58:47 -0700923 *
Paul E. McKenney9ea366f2020-02-13 12:31:16 -0800924 * If you add or remove a call to rcu_nmi_enter(), be sure to test
Paul E. McKenneyc0da3132017-09-22 09:58:47 -0700925 * with CONFIG_RCU_EQS_DEBUG=y.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100926 */
Paul E. McKenney9ea366f2020-02-13 12:31:16 -0800927noinstr void rcu_nmi_enter(void)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100928{
Paul E. McKenney84585aa2017-10-04 15:55:16 -0700929 long incby = 2;
Paul E. McKenney5b145572019-11-26 18:05:45 -0800930 struct rcu_data *rdp = this_cpu_ptr(&rcu_data);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100931
Paul E. McKenney734d1682014-11-21 14:45:12 -0800932 /* Complain about underflow. */
Paul E. McKenney4c5273b2018-08-03 21:00:38 -0700933 WARN_ON_ONCE(rdp->dynticks_nmi_nesting < 0);
Paul E. McKenney734d1682014-11-21 14:45:12 -0800934
935 /*
936 * If idle from RCU viewpoint, atomically increment ->dynticks
937 * to mark non-idle and increment ->dynticks_nmi_nesting by one.
938 * Otherwise, increment ->dynticks_nmi_nesting by two. This means
939 * if ->dynticks_nmi_nesting is equal to one, we are guaranteed
940 * to be in the outermost NMI handler that interrupted an RCU-idle
941 * period (observation due to Andy Lutomirski).
942 */
Paul E. McKenney02a5c5502016-11-02 17:25:06 -0700943 if (rcu_dynticks_curr_cpu_in_eqs()) {
Byungchul Parkcf7614e2018-06-22 15:12:06 +0900944
Paul E. McKenney9ea366f2020-02-13 12:31:16 -0800945 if (!in_nmi())
Byungchul Parkcf7614e2018-06-22 15:12:06 +0900946 rcu_dynticks_task_exit();
947
Paul E. McKenneyac3caf82020-03-12 17:01:57 -0700948 // RCU is not watching here ...
Paul E. McKenney2625d462016-11-02 14:23:30 -0700949 rcu_dynticks_eqs_exit();
Paul E. McKenneyac3caf82020-03-12 17:01:57 -0700950 // ... but is watching here.
Byungchul Parkcf7614e2018-06-22 15:12:06 +0900951
Paul E. McKenney9ea366f2020-02-13 12:31:16 -0800952 if (!in_nmi())
Byungchul Parkcf7614e2018-06-22 15:12:06 +0900953 rcu_cleanup_after_idle();
954
Paul E. McKenney734d1682014-11-21 14:45:12 -0800955 incby = 1;
Paul E. McKenney9ea366f2020-02-13 12:31:16 -0800956 } else if (!in_nmi()) {
Thomas Gleixnerff5c4f52020-03-13 17:32:17 +0100957 instrumentation_begin();
Paul E. McKenneyaaf2bc52020-05-21 22:05:15 +0200958 rcu_irq_enter_check_tick();
Thomas Gleixnerff5c4f52020-03-13 17:32:17 +0100959 instrumentation_end();
Paul E. McKenney734d1682014-11-21 14:45:12 -0800960 }
Thomas Gleixnerff5c4f52020-03-13 17:32:17 +0100961 instrumentation_begin();
Paul E. McKenneybd2b8792017-10-04 12:29:01 -0700962 trace_rcu_dyntick(incby == 1 ? TPS("Endirq") : TPS("++="),
Paul E. McKenney4c5273b2018-08-03 21:00:38 -0700963 rdp->dynticks_nmi_nesting,
Marco Elver6cf539a2019-10-09 17:57:43 +0200964 rdp->dynticks_nmi_nesting + incby, atomic_read(&rdp->dynticks));
Thomas Gleixnerff5c4f52020-03-13 17:32:17 +0100965 instrumentation_end();
Paul E. McKenney4c5273b2018-08-03 21:00:38 -0700966 WRITE_ONCE(rdp->dynticks_nmi_nesting, /* Prevent store tearing. */
967 rdp->dynticks_nmi_nesting + incby);
Paul E. McKenney734d1682014-11-21 14:45:12 -0800968 barrier();
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100969}
970
971/**
Paul E. McKenney34240692011-10-03 11:38:52 -0700972 * rcu_irq_enter - inform RCU that current CPU is entering irq away from idle
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100973 *
Paul E. McKenney34240692011-10-03 11:38:52 -0700974 * Enter an interrupt handler, which might possibly result in exiting
975 * idle mode, in other words, entering the mode in which read-side critical
976 * sections can occur. The caller must have disabled interrupts.
Paul E. McKenneyc0da3132017-09-22 09:58:47 -0700977 *
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -0700978 * Note that the Linux kernel is fully capable of entering an interrupt
Paul E. McKenney58721f52017-10-03 10:42:22 -0700979 * handler that it never exits, for example when doing upcalls to user mode!
980 * This code assumes that the idle loop never does upcalls to user mode.
981 * If your architecture's idle loop does do upcalls to user mode (or does
982 * anything else that results in unbalanced calls to the irq_enter() and
983 * irq_exit() functions), RCU will give you what you deserve, good and hard.
984 * But very infrequently and irreproducibly.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100985 *
986 * Use things like work queues to work around this limitation.
987 *
988 * You have been warned.
989 *
990 * If you add or remove a call to rcu_irq_enter(), be sure to test with
991 * CONFIG_RCU_EQS_DEBUG=y.
Paul E. McKenney23b5c8f2010-09-07 10:38:22 -0700992 */
Thomas Gleixnerff5c4f52020-03-13 17:32:17 +0100993noinstr void rcu_irq_enter(void)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100994{
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100995 lockdep_assert_irqs_disabled();
Paul E. McKenney9ea366f2020-02-13 12:31:16 -0800996 rcu_nmi_enter();
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100997}
Paul E. McKenney734d1682014-11-21 14:45:12 -0800998
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100999/*
1000 * Wrapper for rcu_irq_enter() where interrupts are enabled.
1001 *
1002 * If you add or remove a call to rcu_irq_enter_irqson(), be sure to test
1003 * with CONFIG_RCU_EQS_DEBUG=y.
1004 */
1005void rcu_irq_enter_irqson(void)
1006{
1007 unsigned long flags;
Paul E. McKenney734d1682014-11-21 14:45:12 -08001008
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001009 local_irq_save(flags);
1010 rcu_irq_enter();
1011 local_irq_restore(flags);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001012}
1013
Paul E. McKenney66e4c332019-08-12 16:14:00 -07001014/*
Joel Fernandes (Google)516e5ae2019-09-05 10:26:41 -07001015 * If any sort of urgency was applied to the current CPU (for example,
1016 * the scheduler-clock interrupt was enabled on a nohz_full CPU) in order
1017 * to get to a quiescent state, disable it.
Paul E. McKenney66e4c332019-08-12 16:14:00 -07001018 */
Joel Fernandes (Google)516e5ae2019-09-05 10:26:41 -07001019static void rcu_disable_urgency_upon_qs(struct rcu_data *rdp)
Paul E. McKenney66e4c332019-08-12 16:14:00 -07001020{
Paul E. McKenney5b145572019-11-26 18:05:45 -08001021 raw_lockdep_assert_held_rcu_node(rdp->mynode);
Joel Fernandes (Google)516e5ae2019-09-05 10:26:41 -07001022 WRITE_ONCE(rdp->rcu_urgent_qs, false);
1023 WRITE_ONCE(rdp->rcu_need_heavy_qs, false);
Paul E. McKenney66e4c332019-08-12 16:14:00 -07001024 if (tick_nohz_full_cpu(rdp->cpu) && rdp->rcu_forced_tick) {
1025 tick_dep_clear_cpu(rdp->cpu, TICK_DEP_BIT_RCU);
Paul E. McKenney2a2ae872020-01-08 20:06:25 -08001026 WRITE_ONCE(rdp->rcu_forced_tick, false);
Paul E. McKenney66e4c332019-08-12 16:14:00 -07001027 }
1028}
1029
Thomas Gleixnerb1fcf9b2020-05-12 09:44:43 +02001030noinstr bool __rcu_is_watching(void)
1031{
1032 return !rcu_dynticks_curr_cpu_in_eqs();
1033}
1034
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001035/**
Zhouyi Zhou2320bda2018-10-08 06:50:41 +00001036 * rcu_is_watching - see if RCU thinks that the current CPU is not idle
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001037 *
Paul E. McKenney791875d2017-05-03 11:06:05 -07001038 * Return true if RCU is watching the running CPU, which means that this
1039 * CPU can safely enter RCU read-side critical sections. In other words,
Zhouyi Zhou2320bda2018-10-08 06:50:41 +00001040 * if the current CPU is not in its idle loop or is in an interrupt or
1041 * NMI handler, return true.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001042 */
Thomas Gleixnerff5c4f52020-03-13 17:32:17 +01001043bool rcu_is_watching(void)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001044{
Pranith Kumarf534ed12014-07-08 18:26:11 -04001045 bool ret;
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -07001046
Alexei Starovoitov46f00d12015-06-16 10:35:18 -07001047 preempt_disable_notrace();
Paul E. McKenney791875d2017-05-03 11:06:05 -07001048 ret = !rcu_dynticks_curr_cpu_in_eqs();
Alexei Starovoitov46f00d12015-06-16 10:35:18 -07001049 preempt_enable_notrace();
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -07001050 return ret;
1051}
Paul E. McKenney5c173eb2013-09-13 17:20:11 -07001052EXPORT_SYMBOL_GPL(rcu_is_watching);
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -07001053
Paul E. McKenneybcbfdd02017-04-11 15:50:41 -07001054/*
1055 * If a holdout task is actually running, request an urgent quiescent
1056 * state from its CPU. This is unsynchronized, so migrations can cause
1057 * the request to go to the wrong CPU. Which is OK, all that will happen
1058 * is that the CPU's next context switch will be a bit slower and next
1059 * time around this task will generate another request.
1060 */
1061void rcu_request_urgent_qs_task(struct task_struct *t)
1062{
1063 int cpu;
1064
1065 barrier();
1066 cpu = task_cpu(t);
1067 if (!task_curr(t))
1068 return; /* This task is not running on that CPU. */
Paul E. McKenney2dba13f2018-08-03 21:00:38 -07001069 smp_store_release(per_cpu_ptr(&rcu_data.rcu_urgent_qs, cpu), true);
Paul E. McKenneybcbfdd02017-04-11 15:50:41 -07001070}
1071
Paul E. McKenney62fde6e2012-05-22 22:10:24 -07001072#if defined(CONFIG_PROVE_RCU) && defined(CONFIG_HOTPLUG_CPU)
Paul E. McKenneyc0d6d012012-01-23 12:41:26 -08001073
1074/*
Paul E. McKenney55547882018-05-15 10:14:34 -07001075 * Is the current CPU online as far as RCU is concerned?
Paul E. McKenney2036d942012-01-30 17:02:47 -08001076 *
Paul E. McKenney55547882018-05-15 10:14:34 -07001077 * Disable preemption to avoid false positives that could otherwise
1078 * happen due to the current CPU number being sampled, this task being
1079 * preempted, its old CPU being taken offline, resuming on some other CPU,
Paul E. McKenney49918a52018-07-07 18:12:26 -07001080 * then determining that its old CPU is now offline.
Paul E. McKenneyc0d6d012012-01-23 12:41:26 -08001081 *
Paul E. McKenney55547882018-05-15 10:14:34 -07001082 * Disable checking if in an NMI handler because we cannot safely
1083 * report errors from NMI handlers anyway. In addition, it is OK to use
1084 * RCU on an offline processor during initial boot, hence the check for
1085 * rcu_scheduler_fully_active.
Paul E. McKenneyc0d6d012012-01-23 12:41:26 -08001086 */
1087bool rcu_lockdep_current_cpu_online(void)
1088{
Paul E. McKenney2036d942012-01-30 17:02:47 -08001089 struct rcu_data *rdp;
1090 struct rcu_node *rnp;
Paul E. McKenneyb97d23c2018-07-04 15:35:00 -07001091 bool ret = false;
Paul E. McKenneyc0d6d012012-01-23 12:41:26 -08001092
Paul E. McKenney55547882018-05-15 10:14:34 -07001093 if (in_nmi() || !rcu_scheduler_fully_active)
Fengguang Wuf6f7ee92013-10-10 11:08:33 -07001094 return true;
Thomas Gleixnerff5c4f52020-03-13 17:32:17 +01001095 preempt_disable_notrace();
Paul E. McKenneyb97d23c2018-07-04 15:35:00 -07001096 rdp = this_cpu_ptr(&rcu_data);
1097 rnp = rdp->mynode;
1098 if (rdp->grpmask & rcu_rnp_online_cpus(rnp))
1099 ret = true;
Thomas Gleixnerff5c4f52020-03-13 17:32:17 +01001100 preempt_enable_notrace();
Paul E. McKenneyb97d23c2018-07-04 15:35:00 -07001101 return ret;
Paul E. McKenneyc0d6d012012-01-23 12:41:26 -08001102}
1103EXPORT_SYMBOL_GPL(rcu_lockdep_current_cpu_online);
1104
Paul E. McKenney62fde6e2012-05-22 22:10:24 -07001105#endif /* #if defined(CONFIG_PROVE_RCU) && defined(CONFIG_HOTPLUG_CPU) */
Paul E. McKenney9b2e4f12011-09-30 12:10:22 -07001106
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001107/*
Paul E. McKenney9b9500d2017-08-17 17:05:59 -07001108 * We are reporting a quiescent state on behalf of some other CPU, so
1109 * it is our responsibility to check for and handle potential overflow
Paul E. McKenneya66ae8a2018-04-27 18:06:08 -07001110 * of the rcu_node ->gp_seq counter with respect to the rcu_data counters.
Paul E. McKenney9b9500d2017-08-17 17:05:59 -07001111 * After all, the CPU might be in deep idle state, and thus executing no
1112 * code whatsoever.
1113 */
1114static void rcu_gpnum_ovf(struct rcu_node *rnp, struct rcu_data *rdp)
1115{
Matthew Wilcoxa32e01e2018-01-17 06:24:30 -08001116 raw_lockdep_assert_held_rcu_node(rnp);
Paul E. McKenneya66ae8a2018-04-27 18:06:08 -07001117 if (ULONG_CMP_LT(rcu_seq_current(&rdp->gp_seq) + ULONG_MAX / 4,
1118 rnp->gp_seq))
Paul E. McKenney9b9500d2017-08-17 17:05:59 -07001119 WRITE_ONCE(rdp->gpwrap, true);
Paul E. McKenney8aa670c2018-04-28 14:15:40 -07001120 if (ULONG_CMP_LT(rdp->rcu_iw_gp_seq + ULONG_MAX / 4, rnp->gp_seq))
1121 rdp->rcu_iw_gp_seq = rnp->gp_seq + ULONG_MAX / 4;
Paul E. McKenney9b9500d2017-08-17 17:05:59 -07001122}
1123
1124/*
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001125 * Snapshot the specified CPU's dynticks counter so that we can later
1126 * credit them with an implicit quiescent state. Return 1 if this CPU
Paul E. McKenney1eba8f82009-09-23 09:50:42 -07001127 * is in dynticks idle mode, which is an extended quiescent state.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001128 */
Paul E. McKenneyfe5ac722017-05-11 11:26:22 -07001129static int dyntick_save_progress_counter(struct rcu_data *rdp)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001130{
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -07001131 rdp->dynticks_snap = rcu_dynticks_snap(rdp);
Paul E. McKenney02a5c5502016-11-02 17:25:06 -07001132 if (rcu_dynticks_in_eqs(rdp->dynticks_snap)) {
Paul E. McKenney88d1bea2018-07-04 14:45:00 -07001133 trace_rcu_fqs(rcu_state.name, rdp->gp_seq, rdp->cpu, TPS("dti"));
Paul E. McKenney9b9500d2017-08-17 17:05:59 -07001134 rcu_gpnum_ovf(rdp->mynode, rdp);
Paul E. McKenney23a9bac2015-12-13 08:57:10 -08001135 return 1;
Andreea-Cristina Bernat7941dbd2014-03-17 18:33:28 +02001136 }
Paul E. McKenney23a9bac2015-12-13 08:57:10 -08001137 return 0;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001138}
1139
1140/*
1141 * Return true if the specified CPU has passed through a quiescent
1142 * state by virtue of being in or having passed through an dynticks
1143 * idle state since the last call to dyntick_save_progress_counter()
Paul E. McKenneya82dcc72012-08-01 14:29:20 -07001144 * for this same CPU, or by virtue of having been offline.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001145 */
Paul E. McKenneyfe5ac722017-05-11 11:26:22 -07001146static int rcu_implicit_dynticks_qs(struct rcu_data *rdp)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001147{
Paul E. McKenney3a19b462016-11-30 11:21:21 -08001148 unsigned long jtsq;
Paul E. McKenney0f9be8c2017-01-27 13:17:02 -08001149 bool *rnhqp;
Paul E. McKenney9226b102017-01-27 14:17:50 -08001150 bool *ruqp;
Paul E. McKenney9b9500d2017-08-17 17:05:59 -07001151 struct rcu_node *rnp = rdp->mynode;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001152
1153 /*
1154 * If the CPU passed through or entered a dynticks idle phase with
1155 * no active irq/NMI handlers, then we can safely pretend that the CPU
1156 * already acknowledged the request to pass through a quiescent
1157 * state. Either way, that CPU cannot possibly be in an RCU
1158 * read-side critical section that started before the beginning
1159 * of the current RCU grace period.
1160 */
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -07001161 if (rcu_dynticks_in_eqs_since(rdp, rdp->dynticks_snap)) {
Paul E. McKenney88d1bea2018-07-04 14:45:00 -07001162 trace_rcu_fqs(rcu_state.name, rdp->gp_seq, rdp->cpu, TPS("dti"));
Paul E. McKenney9b9500d2017-08-17 17:05:59 -07001163 rcu_gpnum_ovf(rnp, rdp);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001164 return 1;
1165 }
1166
Paul E. McKenneyf2e2df52018-05-15 16:23:23 -07001167 /* If waiting too long on an offline CPU, complain. */
1168 if (!(rdp->grpmask & rcu_rnp_online_cpus(rnp)) &&
Paul E. McKenney88d1bea2018-07-04 14:45:00 -07001169 time_after(jiffies, rcu_state.gp_start + HZ)) {
Paul E. McKenneyf2e2df52018-05-15 16:23:23 -07001170 bool onl;
1171 struct rcu_node *rnp1;
1172
1173 WARN_ON(1); /* Offline CPUs are supposed to report QS! */
1174 pr_info("%s: grp: %d-%d level: %d ->gp_seq %ld ->completedqs %ld\n",
1175 __func__, rnp->grplo, rnp->grphi, rnp->level,
1176 (long)rnp->gp_seq, (long)rnp->completedqs);
1177 for (rnp1 = rnp; rnp1; rnp1 = rnp1->parent)
1178 pr_info("%s: %d:%d ->qsmask %#lx ->qsmaskinit %#lx ->qsmaskinitnext %#lx ->rcu_gp_init_mask %#lx\n",
1179 __func__, rnp1->grplo, rnp1->grphi, rnp1->qsmask, rnp1->qsmaskinit, rnp1->qsmaskinitnext, rnp1->rcu_gp_init_mask);
1180 onl = !!(rdp->grpmask & rcu_rnp_online_cpus(rnp));
1181 pr_info("%s %d: %c online: %ld(%d) offline: %ld(%d)\n",
1182 __func__, rdp->cpu, ".o"[onl],
1183 (long)rdp->rcu_onl_gp_seq, rdp->rcu_onl_gp_flags,
1184 (long)rdp->rcu_ofl_gp_seq, rdp->rcu_ofl_gp_flags);
1185 return 1; /* Break things loose after complaining. */
1186 }
1187
Paul E. McKenney65d798f2013-04-12 16:19:10 -07001188 /*
Paul E. McKenney4a81e832014-06-20 16:49:01 -07001189 * A CPU running for an extended time within the kernel can
Paul E. McKenneyc06aed02018-07-25 11:25:23 -07001190 * delay RCU grace periods: (1) At age jiffies_to_sched_qs,
1191 * set .rcu_urgent_qs, (2) At age 2*jiffies_to_sched_qs, set
Paul E. McKenney7e28c5a2018-07-11 08:09:28 -07001192 * both .rcu_need_heavy_qs and .rcu_urgent_qs. Note that the
1193 * unsynchronized assignments to the per-CPU rcu_need_heavy_qs
1194 * variable are safe because the assignments are repeated if this
1195 * CPU failed to pass through a quiescent state. This code
Paul E. McKenneyc06aed02018-07-25 11:25:23 -07001196 * also checks .jiffies_resched in case jiffies_to_sched_qs
Paul E. McKenney7e28c5a2018-07-11 08:09:28 -07001197 * is set way high.
Paul E. McKenney65d798f2013-04-12 16:19:10 -07001198 */
Paul E. McKenneyc06aed02018-07-25 11:25:23 -07001199 jtsq = READ_ONCE(jiffies_to_sched_qs);
Paul E. McKenney2dba13f2018-08-03 21:00:38 -07001200 ruqp = per_cpu_ptr(&rcu_data.rcu_urgent_qs, rdp->cpu);
1201 rnhqp = &per_cpu(rcu_data.rcu_need_heavy_qs, rdp->cpu);
Paul E. McKenney0f9be8c2017-01-27 13:17:02 -08001202 if (!READ_ONCE(*rnhqp) &&
Paul E. McKenney7e28c5a2018-07-11 08:09:28 -07001203 (time_after(jiffies, rcu_state.gp_start + jtsq * 2) ||
Paul E. McKenneyb2b00dd2019-10-30 11:56:10 -07001204 time_after(jiffies, rcu_state.jiffies_resched) ||
1205 rcu_state.cbovld)) {
Paul E. McKenney0f9be8c2017-01-27 13:17:02 -08001206 WRITE_ONCE(*rnhqp, true);
Paul E. McKenney9226b102017-01-27 14:17:50 -08001207 /* Store rcu_need_heavy_qs before rcu_urgent_qs. */
1208 smp_store_release(ruqp, true);
Paul E. McKenney7e28c5a2018-07-11 08:09:28 -07001209 } else if (time_after(jiffies, rcu_state.gp_start + jtsq)) {
1210 WRITE_ONCE(*ruqp, true);
Paul E. McKenney6193c762013-09-23 13:57:18 -07001211 }
1212
Paul E. McKenney28053bc2016-12-01 11:31:31 -08001213 /*
Paul E. McKenneyc98cac62018-11-21 11:35:03 -08001214 * NO_HZ_FULL CPUs can run in-kernel without rcu_sched_clock_irq!
Paul E. McKenneyd3052102018-07-25 11:49:47 -07001215 * The above code handles this, but only for straight cond_resched().
1216 * And some in-kernel loops check need_resched() before calling
1217 * cond_resched(), which defeats the above code for CPUs that are
1218 * running in-kernel with scheduling-clock interrupts disabled.
1219 * So hit them over the head with the resched_cpu() hammer!
Paul E. McKenney28053bc2016-12-01 11:31:31 -08001220 */
Paul E. McKenneyd3052102018-07-25 11:49:47 -07001221 if (tick_nohz_full_cpu(rdp->cpu) &&
Paul E. McKenneyb2b00dd2019-10-30 11:56:10 -07001222 (time_after(jiffies, READ_ONCE(rdp->last_fqs_resched) + jtsq * 3) ||
1223 rcu_state.cbovld)) {
Joel Fernandes (Google)05ef9e92019-08-15 22:59:14 -04001224 WRITE_ONCE(*ruqp, true);
Paul E. McKenney28053bc2016-12-01 11:31:31 -08001225 resched_cpu(rdp->cpu);
Paul E. McKenneyd3052102018-07-25 11:49:47 -07001226 WRITE_ONCE(rdp->last_fqs_resched, jiffies);
1227 }
1228
1229 /*
1230 * If more than halfway to RCU CPU stall-warning time, invoke
1231 * resched_cpu() more frequently to try to loosen things up a bit.
1232 * Also check to see if the CPU is getting hammered with interrupts,
1233 * but only once per grace period, just to keep the IPIs down to
1234 * a dull roar.
Paul E. McKenney49149502015-12-11 13:48:43 -08001235 */
Paul E. McKenney7e28c5a2018-07-11 08:09:28 -07001236 if (time_after(jiffies, rcu_state.jiffies_resched)) {
Paul E. McKenneyd3052102018-07-25 11:49:47 -07001237 if (time_after(jiffies,
1238 READ_ONCE(rdp->last_fqs_resched) + jtsq)) {
1239 resched_cpu(rdp->cpu);
1240 WRITE_ONCE(rdp->last_fqs_resched, jiffies);
1241 }
Paul E. McKenney9b9500d2017-08-17 17:05:59 -07001242 if (IS_ENABLED(CONFIG_IRQ_WORK) &&
Paul E. McKenney8aa670c2018-04-28 14:15:40 -07001243 !rdp->rcu_iw_pending && rdp->rcu_iw_gp_seq != rnp->gp_seq &&
Paul E. McKenney9b9500d2017-08-17 17:05:59 -07001244 (rnp->ffmask & rdp->grpmask)) {
1245 init_irq_work(&rdp->rcu_iw, rcu_iw_handler);
Sebastian Andrzej Siewior49915ac2020-03-21 12:26:03 +01001246 atomic_set(&rdp->rcu_iw.flags, IRQ_WORK_HARD_IRQ);
Paul E. McKenney9b9500d2017-08-17 17:05:59 -07001247 rdp->rcu_iw_pending = true;
Paul E. McKenney8aa670c2018-04-28 14:15:40 -07001248 rdp->rcu_iw_gp_seq = rnp->gp_seq;
Paul E. McKenney9b9500d2017-08-17 17:05:59 -07001249 irq_work_queue_on(&rdp->rcu_iw, rdp->cpu);
1250 }
1251 }
Paul E. McKenney49149502015-12-11 13:48:43 -08001252
Paul E. McKenneya82dcc72012-08-01 14:29:20 -07001253 return 0;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01001254}
1255
Paul E. McKenney41e80592018-04-12 11:24:09 -07001256/* Trace-event wrapper function for trace_rcu_future_grace_period. */
1257static void trace_rcu_this_gp(struct rcu_node *rnp, struct rcu_data *rdp,
Joel Fernandesb73de912018-05-20 21:42:18 -07001258 unsigned long gp_seq_req, const char *s)
Paul E. McKenney0446be42012-12-30 15:21:01 -08001259{
Paul E. McKenney0937d042020-01-03 14:53:31 -08001260 trace_rcu_future_grace_period(rcu_state.name, READ_ONCE(rnp->gp_seq),
1261 gp_seq_req, rnp->level,
1262 rnp->grplo, rnp->grphi, s);
Paul E. McKenney0446be42012-12-30 15:21:01 -08001263}
1264
1265/*
Joel Fernandesb73de912018-05-20 21:42:18 -07001266 * rcu_start_this_gp - Request the start of a particular grace period
Joel Fernandes (Google)df2bf8f2018-05-22 23:38:14 -07001267 * @rnp_start: The leaf node of the CPU from which to start.
Joel Fernandesb73de912018-05-20 21:42:18 -07001268 * @rdp: The rcu_data corresponding to the CPU from which to start.
1269 * @gp_seq_req: The gp_seq of the grace period to start.
1270 *
Paul E. McKenney41e80592018-04-12 11:24:09 -07001271 * Start the specified grace period, as needed to handle newly arrived
Paul E. McKenney0446be42012-12-30 15:21:01 -08001272 * callbacks. The required future grace periods are recorded in each
Paul E. McKenney7a1d0f22018-05-01 10:26:57 -07001273 * rcu_node structure's ->gp_seq_needed field. Returns true if there
Paul E. McKenney48a76392014-03-11 13:02:16 -07001274 * is reason to awaken the grace-period kthread.
Paul E. McKenney0446be42012-12-30 15:21:01 -08001275 *
Paul E. McKenneyd5cd9682018-04-12 10:45:06 -07001276 * The caller must hold the specified rcu_node structure's ->lock, which
1277 * is why the caller is responsible for waking the grace-period kthread.
Joel Fernandesb73de912018-05-20 21:42:18 -07001278 *
1279 * Returns true if the GP thread needs to be awakened else false.
Paul E. McKenney0446be42012-12-30 15:21:01 -08001280 */
Joel Fernandes (Google)df2bf8f2018-05-22 23:38:14 -07001281static bool rcu_start_this_gp(struct rcu_node *rnp_start, struct rcu_data *rdp,
Joel Fernandesb73de912018-05-20 21:42:18 -07001282 unsigned long gp_seq_req)
Paul E. McKenney0446be42012-12-30 15:21:01 -08001283{
Paul E. McKenney48a76392014-03-11 13:02:16 -07001284 bool ret = false;
Joel Fernandes (Google)df2bf8f2018-05-22 23:38:14 -07001285 struct rcu_node *rnp;
Paul E. McKenney0446be42012-12-30 15:21:01 -08001286
Paul E. McKenney360e0da2018-04-12 11:50:41 -07001287 /*
1288 * Use funnel locking to either acquire the root rcu_node
1289 * structure's lock or bail out if the need for this grace period
Joel Fernandes (Google)df2bf8f2018-05-22 23:38:14 -07001290 * has already been recorded -- or if that grace period has in
1291 * fact already started. If there is already a grace period in
1292 * progress in a non-leaf node, no recording is needed because the
1293 * end of the grace period will scan the leaf rcu_node structures.
1294 * Note that rnp_start->lock must not be released.
Paul E. McKenney360e0da2018-04-12 11:50:41 -07001295 */
Joel Fernandes (Google)df2bf8f2018-05-22 23:38:14 -07001296 raw_lockdep_assert_held_rcu_node(rnp_start);
1297 trace_rcu_this_gp(rnp_start, rdp, gp_seq_req, TPS("Startleaf"));
1298 for (rnp = rnp_start; 1; rnp = rnp->parent) {
1299 if (rnp != rnp_start)
1300 raw_spin_lock_rcu_node(rnp);
1301 if (ULONG_CMP_GE(rnp->gp_seq_needed, gp_seq_req) ||
1302 rcu_seq_started(&rnp->gp_seq, gp_seq_req) ||
1303 (rnp != rnp_start &&
1304 rcu_seq_state(rcu_seq_current(&rnp->gp_seq)))) {
1305 trace_rcu_this_gp(rnp, rdp, gp_seq_req,
Joel Fernandesb73de912018-05-20 21:42:18 -07001306 TPS("Prestarted"));
Paul E. McKenney360e0da2018-04-12 11:50:41 -07001307 goto unlock_out;
1308 }
Paul E. McKenney8ff372902020-01-04 11:33:17 -08001309 WRITE_ONCE(rnp->gp_seq_needed, gp_seq_req);
Joel Fernandes (Google)226ca5e2018-05-22 23:38:15 -07001310 if (rcu_seq_state(rcu_seq_current(&rnp->gp_seq))) {
Paul E. McKenneya2165e42018-05-12 07:42:20 -07001311 /*
Joel Fernandes (Google)226ca5e2018-05-22 23:38:15 -07001312 * We just marked the leaf or internal node, and a
1313 * grace period is in progress, which means that
1314 * rcu_gp_cleanup() will see the marking. Bail to
1315 * reduce contention.
Paul E. McKenneya2165e42018-05-12 07:42:20 -07001316 */
Joel Fernandes (Google)df2bf8f2018-05-22 23:38:14 -07001317 trace_rcu_this_gp(rnp_start, rdp, gp_seq_req,
Joel Fernandesb73de912018-05-20 21:42:18 -07001318 TPS("Startedleaf"));
Paul E. McKenneya2165e42018-05-12 07:42:20 -07001319 goto unlock_out;
1320 }
Joel Fernandes (Google)df2bf8f2018-05-22 23:38:14 -07001321 if (rnp != rnp_start && rnp->parent != NULL)
1322 raw_spin_unlock_rcu_node(rnp);
1323 if (!rnp->parent)
Paul E. McKenney360e0da2018-04-12 11:50:41 -07001324 break; /* At root, and perhaps also leaf. */
Paul E. McKenney0446be42012-12-30 15:21:01 -08001325 }
1326
Paul E. McKenney360e0da2018-04-12 11:50:41 -07001327 /* If GP already in progress, just leave, otherwise start one. */
Paul E. McKenneyde8e8732018-07-03 17:22:34 -07001328 if (rcu_gp_in_progress()) {
Joel Fernandes (Google)df2bf8f2018-05-22 23:38:14 -07001329 trace_rcu_this_gp(rnp, rdp, gp_seq_req, TPS("Startedleafroot"));
Paul E. McKenney0446be42012-12-30 15:21:01 -08001330 goto unlock_out;
1331 }
Joel Fernandes (Google)df2bf8f2018-05-22 23:38:14 -07001332 trace_rcu_this_gp(rnp, rdp, gp_seq_req, TPS("Startedroot"));
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001333 WRITE_ONCE(rcu_state.gp_flags, rcu_state.gp_flags | RCU_GP_FLAG_INIT);
Paul E. McKenney2906d212020-01-03 15:17:12 -08001334 WRITE_ONCE(rcu_state.gp_req_activity, jiffies);
Paul E. McKenney5648d652020-01-21 12:30:22 -08001335 if (!READ_ONCE(rcu_state.gp_kthread)) {
Joel Fernandes (Google)df2bf8f2018-05-22 23:38:14 -07001336 trace_rcu_this_gp(rnp, rdp, gp_seq_req, TPS("NoGPkthread"));
Paul E. McKenney360e0da2018-04-12 11:50:41 -07001337 goto unlock_out;
Paul E. McKenney0446be42012-12-30 15:21:01 -08001338 }
Paul E. McKenney62ae1952020-03-21 19:52:20 -07001339 trace_rcu_grace_period(rcu_state.name, data_race(rcu_state.gp_seq), TPS("newreq"));
Paul E. McKenney360e0da2018-04-12 11:50:41 -07001340 ret = true; /* Caller must wake GP kthread. */
Paul E. McKenney0446be42012-12-30 15:21:01 -08001341unlock_out:
Paul E. McKenneyab5e8692018-05-01 11:07:23 -07001342 /* Push furthest requested GP to leaf node and rcu_data structure. */
Joel Fernandes (Google)df2bf8f2018-05-22 23:38:14 -07001343 if (ULONG_CMP_LT(gp_seq_req, rnp->gp_seq_needed)) {
Paul E. McKenney8ff372902020-01-04 11:33:17 -08001344 WRITE_ONCE(rnp_start->gp_seq_needed, rnp->gp_seq_needed);
1345 WRITE_ONCE(rdp->gp_seq_needed, rnp->gp_seq_needed);
Paul E. McKenneyab5e8692018-05-01 11:07:23 -07001346 }
Joel Fernandes (Google)df2bf8f2018-05-22 23:38:14 -07001347 if (rnp != rnp_start)
1348 raw_spin_unlock_rcu_node(rnp);
Paul E. McKenney48a76392014-03-11 13:02:16 -07001349 return ret;
Paul E. McKenney0446be42012-12-30 15:21:01 -08001350}
1351
1352/*
1353 * Clean up any old requests for the just-ended grace period. Also return
Paul E. McKenneyd1e4f012017-02-08 14:58:41 -08001354 * whether any additional grace periods have been requested.
Paul E. McKenney0446be42012-12-30 15:21:01 -08001355 */
Paul E. McKenney3481f2e2018-07-03 17:22:34 -07001356static bool rcu_future_gp_cleanup(struct rcu_node *rnp)
Paul E. McKenney0446be42012-12-30 15:21:01 -08001357{
Paul E. McKenneyfb313402018-04-12 07:20:30 -07001358 bool needmore;
Paul E. McKenneyda1df502018-07-03 15:37:16 -07001359 struct rcu_data *rdp = this_cpu_ptr(&rcu_data);
Paul E. McKenney0446be42012-12-30 15:21:01 -08001360
Paul E. McKenney7a1d0f22018-05-01 10:26:57 -07001361 needmore = ULONG_CMP_LT(rnp->gp_seq, rnp->gp_seq_needed);
1362 if (!needmore)
1363 rnp->gp_seq_needed = rnp->gp_seq; /* Avoid counter wrap. */
Joel Fernandesb73de912018-05-20 21:42:18 -07001364 trace_rcu_this_gp(rnp, rdp, rnp->gp_seq,
Paul E. McKenney41e80592018-04-12 11:24:09 -07001365 needmore ? TPS("CleanupMore") : TPS("Cleanup"));
Paul E. McKenney0446be42012-12-30 15:21:01 -08001366 return needmore;
1367}
1368
1369/*
Paul E. McKenney5648d652020-01-21 12:30:22 -08001370 * Awaken the grace-period kthread. Don't do a self-awaken (unless in an
1371 * interrupt or softirq handler, in which case we just might immediately
1372 * sleep upon return, resulting in a grace-period hang), and don't bother
1373 * awakening when there is nothing for the grace-period kthread to do
1374 * (as in several CPUs raced to awaken, we lost), and finally don't try
1375 * to awaken a kthread that has not yet been created. If all those checks
1376 * are passed, track some debug information and awaken.
Zhang, Jun1d1f8982018-12-18 06:55:01 -08001377 *
1378 * So why do the self-wakeup when in an interrupt or softirq handler
1379 * in the grace-period kthread's context? Because the kthread might have
1380 * been interrupted just as it was going to sleep, and just after the final
1381 * pre-sleep check of the awaken condition. In this case, a wakeup really
1382 * is required, and is therefore supplied.
Paul E. McKenney48a76392014-03-11 13:02:16 -07001383 */
Paul E. McKenney532c00c2018-07-03 17:22:34 -07001384static void rcu_gp_kthread_wake(void)
Paul E. McKenney48a76392014-03-11 13:02:16 -07001385{
Paul E. McKenney5648d652020-01-21 12:30:22 -08001386 struct task_struct *t = READ_ONCE(rcu_state.gp_kthread);
1387
1388 if ((current == t && !in_irq() && !in_serving_softirq()) ||
1389 !READ_ONCE(rcu_state.gp_flags) || !t)
Paul E. McKenney48a76392014-03-11 13:02:16 -07001390 return;
Paul E. McKenneyfd897572018-12-10 16:09:49 -08001391 WRITE_ONCE(rcu_state.gp_wake_time, jiffies);
1392 WRITE_ONCE(rcu_state.gp_wake_seq, READ_ONCE(rcu_state.gp_seq));
Paul E. McKenney532c00c2018-07-03 17:22:34 -07001393 swake_up_one(&rcu_state.gp_wq);
Paul E. McKenney48a76392014-03-11 13:02:16 -07001394}
1395
1396/*
Paul E. McKenney29365e52018-04-30 10:57:36 -07001397 * If there is room, assign a ->gp_seq number to any callbacks on this
1398 * CPU that have not already been assigned. Also accelerate any callbacks
1399 * that were previously assigned a ->gp_seq number that has since proven
1400 * to be too conservative, which can happen if callbacks get assigned a
1401 * ->gp_seq number while RCU is idle, but with reference to a non-root
1402 * rcu_node structure. This function is idempotent, so it does not hurt
1403 * to call it repeatedly. Returns an flag saying that we should awaken
1404 * the RCU grace-period kthread.
Paul E. McKenneydc35c892012-12-03 13:52:00 -08001405 *
1406 * The caller must hold rnp->lock with interrupts disabled.
1407 */
Paul E. McKenney02f50142018-07-03 17:22:34 -07001408static bool rcu_accelerate_cbs(struct rcu_node *rnp, struct rcu_data *rdp)
Paul E. McKenneydc35c892012-12-03 13:52:00 -08001409{
Joel Fernandesb73de912018-05-20 21:42:18 -07001410 unsigned long gp_seq_req;
Paul E. McKenney15fecf82017-02-08 12:36:42 -08001411 bool ret = false;
Paul E. McKenneydc35c892012-12-03 13:52:00 -08001412
Paul E. McKenneyd1b222c2019-07-02 16:03:33 -07001413 rcu_lockdep_assert_cblist_protected(rdp);
Matthew Wilcoxa32e01e2018-01-17 06:24:30 -08001414 raw_lockdep_assert_held_rcu_node(rnp);
Paul E. McKenneyc0b334c2017-04-28 12:32:15 -07001415
Paul E. McKenney15fecf82017-02-08 12:36:42 -08001416 /* If no pending (not yet ready to invoke) callbacks, nothing to do. */
1417 if (!rcu_segcblist_pend_cbs(&rdp->cblist))
Paul E. McKenney48a76392014-03-11 13:02:16 -07001418 return false;
Paul E. McKenneydc35c892012-12-03 13:52:00 -08001419
1420 /*
Paul E. McKenney15fecf82017-02-08 12:36:42 -08001421 * Callbacks are often registered with incomplete grace-period
1422 * information. Something about the fact that getting exact
1423 * information requires acquiring a global lock... RCU therefore
1424 * makes a conservative estimate of the grace period number at which
1425 * a given callback will become ready to invoke. The following
1426 * code checks this estimate and improves it when possible, thus
1427 * accelerating callback invocation to an earlier grace-period
1428 * number.
Paul E. McKenneydc35c892012-12-03 13:52:00 -08001429 */
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001430 gp_seq_req = rcu_seq_snap(&rcu_state.gp_seq);
Joel Fernandesb73de912018-05-20 21:42:18 -07001431 if (rcu_segcblist_accelerate(&rdp->cblist, gp_seq_req))
1432 ret = rcu_start_this_gp(rnp, rdp, gp_seq_req);
Paul E. McKenney6d4b4182012-11-27 16:55:44 -08001433
1434 /* Trace depending on how much we were able to accelerate. */
Paul E. McKenney15fecf82017-02-08 12:36:42 -08001435 if (rcu_segcblist_restempty(&rdp->cblist, RCU_WAIT_TAIL))
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001436 trace_rcu_grace_period(rcu_state.name, rdp->gp_seq, TPS("AccWaitCB"));
Paul E. McKenney6d4b4182012-11-27 16:55:44 -08001437 else
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001438 trace_rcu_grace_period(rcu_state.name, rdp->gp_seq, TPS("AccReadyCB"));
Paul E. McKenney48a76392014-03-11 13:02:16 -07001439 return ret;
Paul E. McKenneydc35c892012-12-03 13:52:00 -08001440}
1441
1442/*
Paul E. McKenneye44e73c2018-05-01 16:29:47 -07001443 * Similar to rcu_accelerate_cbs(), but does not require that the leaf
1444 * rcu_node structure's ->lock be held. It consults the cached value
1445 * of ->gp_seq_needed in the rcu_data structure, and if that indicates
1446 * that a new grace-period request be made, invokes rcu_accelerate_cbs()
1447 * while holding the leaf rcu_node structure's ->lock.
1448 */
Paul E. McKenneyc6e09b92018-07-03 17:22:34 -07001449static void rcu_accelerate_cbs_unlocked(struct rcu_node *rnp,
Paul E. McKenneye44e73c2018-05-01 16:29:47 -07001450 struct rcu_data *rdp)
1451{
1452 unsigned long c;
1453 bool needwake;
1454
Paul E. McKenneyd1b222c2019-07-02 16:03:33 -07001455 rcu_lockdep_assert_cblist_protected(rdp);
Paul E. McKenneyc6e09b92018-07-03 17:22:34 -07001456 c = rcu_seq_snap(&rcu_state.gp_seq);
Paul E. McKenneya5b89502020-01-07 15:48:39 -08001457 if (!READ_ONCE(rdp->gpwrap) && ULONG_CMP_GE(rdp->gp_seq_needed, c)) {
Paul E. McKenneye44e73c2018-05-01 16:29:47 -07001458 /* Old request still live, so mark recent callbacks. */
1459 (void)rcu_segcblist_accelerate(&rdp->cblist, c);
1460 return;
1461 }
1462 raw_spin_lock_rcu_node(rnp); /* irqs already disabled. */
Paul E. McKenney02f50142018-07-03 17:22:34 -07001463 needwake = rcu_accelerate_cbs(rnp, rdp);
Paul E. McKenneye44e73c2018-05-01 16:29:47 -07001464 raw_spin_unlock_rcu_node(rnp); /* irqs remain disabled. */
1465 if (needwake)
Paul E. McKenney532c00c2018-07-03 17:22:34 -07001466 rcu_gp_kthread_wake();
Paul E. McKenneye44e73c2018-05-01 16:29:47 -07001467}
1468
1469/*
Paul E. McKenneydc35c892012-12-03 13:52:00 -08001470 * Move any callbacks whose grace period has completed to the
1471 * RCU_DONE_TAIL sublist, then compact the remaining sublists and
Paul E. McKenney29365e52018-04-30 10:57:36 -07001472 * assign ->gp_seq numbers to any callbacks in the RCU_NEXT_TAIL
Paul E. McKenneydc35c892012-12-03 13:52:00 -08001473 * sublist. This function is idempotent, so it does not hurt to
1474 * invoke it repeatedly. As long as it is not invoked -too- often...
Paul E. McKenney48a76392014-03-11 13:02:16 -07001475 * Returns true if the RCU grace-period kthread needs to be awakened.
Paul E. McKenneydc35c892012-12-03 13:52:00 -08001476 *
1477 * The caller must hold rnp->lock with interrupts disabled.
1478 */
Paul E. McKenney834f56b2018-07-03 17:22:34 -07001479static bool rcu_advance_cbs(struct rcu_node *rnp, struct rcu_data *rdp)
Paul E. McKenneydc35c892012-12-03 13:52:00 -08001480{
Paul E. McKenneyd1b222c2019-07-02 16:03:33 -07001481 rcu_lockdep_assert_cblist_protected(rdp);
Matthew Wilcoxa32e01e2018-01-17 06:24:30 -08001482 raw_lockdep_assert_held_rcu_node(rnp);
Paul E. McKenneyc0b334c2017-04-28 12:32:15 -07001483
Paul E. McKenney15fecf82017-02-08 12:36:42 -08001484 /* If no pending (not yet ready to invoke) callbacks, nothing to do. */
1485 if (!rcu_segcblist_pend_cbs(&rdp->cblist))
Paul E. McKenney48a76392014-03-11 13:02:16 -07001486 return false;
Paul E. McKenneydc35c892012-12-03 13:52:00 -08001487
1488 /*
Paul E. McKenney29365e52018-04-30 10:57:36 -07001489 * Find all callbacks whose ->gp_seq numbers indicate that they
Paul E. McKenneydc35c892012-12-03 13:52:00 -08001490 * are ready to invoke, and put them into the RCU_DONE_TAIL sublist.
1491 */
Paul E. McKenney29365e52018-04-30 10:57:36 -07001492 rcu_segcblist_advance(&rdp->cblist, rnp->gp_seq);
Paul E. McKenneydc35c892012-12-03 13:52:00 -08001493
1494 /* Classify any remaining callbacks. */
Paul E. McKenney02f50142018-07-03 17:22:34 -07001495 return rcu_accelerate_cbs(rnp, rdp);
Paul E. McKenneydc35c892012-12-03 13:52:00 -08001496}
1497
1498/*
Paul E. McKenney7f36ef82019-05-28 05:54:26 -07001499 * Move and classify callbacks, but only if doing so won't require
1500 * that the RCU grace-period kthread be awakened.
1501 */
1502static void __maybe_unused rcu_advance_cbs_nowake(struct rcu_node *rnp,
1503 struct rcu_data *rdp)
1504{
Paul E. McKenneyd1b222c2019-07-02 16:03:33 -07001505 rcu_lockdep_assert_cblist_protected(rdp);
Paul E. McKenney6608c3a2019-06-01 06:16:38 -07001506 if (!rcu_seq_state(rcu_seq_current(&rnp->gp_seq)) ||
1507 !raw_spin_trylock_rcu_node(rnp))
Paul E. McKenney7f36ef82019-05-28 05:54:26 -07001508 return;
1509 WARN_ON_ONCE(rcu_advance_cbs(rnp, rdp));
Paul E. McKenney6608c3a2019-06-01 06:16:38 -07001510 raw_spin_unlock_rcu_node(rnp);
Paul E. McKenney7f36ef82019-05-28 05:54:26 -07001511}
1512
1513/*
Paul E. McKenneyba9fbe92013-03-19 11:53:31 -07001514 * Update CPU-local rcu_data state to record the beginnings and ends of
1515 * grace periods. The caller must hold the ->lock of the leaf rcu_node
1516 * structure corresponding to the current CPU, and must have irqs disabled.
Paul E. McKenney48a76392014-03-11 13:02:16 -07001517 * Returns true if the grace-period kthread needs to be awakened.
Paul E. McKenneyd09b62d2009-11-02 13:52:28 -08001518 */
Paul E. McKenneyc7e48f72018-07-03 17:22:34 -07001519static bool __note_gp_changes(struct rcu_node *rnp, struct rcu_data *rdp)
Paul E. McKenneyd09b62d2009-11-02 13:52:28 -08001520{
Paul E. McKenney5d6742b2019-05-15 09:56:40 -07001521 bool ret = false;
Paul E. McKenneyb5ea0372019-12-09 15:19:45 -08001522 bool need_qs;
Paul E. McKenney5d6742b2019-05-15 09:56:40 -07001523 const bool offloaded = IS_ENABLED(CONFIG_RCU_NOCB_CPU) &&
1524 rcu_segcblist_is_offloaded(&rdp->cblist);
Paul E. McKenney48a76392014-03-11 13:02:16 -07001525
Matthew Wilcoxa32e01e2018-01-17 06:24:30 -08001526 raw_lockdep_assert_held_rcu_node(rnp);
Paul E. McKenneyc0b334c2017-04-28 12:32:15 -07001527
Paul E. McKenney67e14c12018-04-27 16:01:46 -07001528 if (rdp->gp_seq == rnp->gp_seq)
1529 return false; /* Nothing to do. */
1530
Paul E. McKenneyba9fbe92013-03-19 11:53:31 -07001531 /* Handle the ends of any preceding grace periods first. */
Paul E. McKenney67e14c12018-04-27 16:01:46 -07001532 if (rcu_seq_completed_gp(rdp->gp_seq, rnp->gp_seq) ||
1533 unlikely(READ_ONCE(rdp->gpwrap))) {
Paul E. McKenney5d6742b2019-05-15 09:56:40 -07001534 if (!offloaded)
1535 ret = rcu_advance_cbs(rnp, rdp); /* Advance CBs. */
Paul E. McKenneyb5ea0372019-12-09 15:19:45 -08001536 rdp->core_needs_qs = false;
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001537 trace_rcu_grace_period(rcu_state.name, rdp->gp_seq, TPS("cpuend"));
Paul E. McKenney67e14c12018-04-27 16:01:46 -07001538 } else {
Paul E. McKenney5d6742b2019-05-15 09:56:40 -07001539 if (!offloaded)
1540 ret = rcu_accelerate_cbs(rnp, rdp); /* Recent CBs. */
Paul E. McKenneyb5ea0372019-12-09 15:19:45 -08001541 if (rdp->core_needs_qs)
1542 rdp->core_needs_qs = !!(rnp->qsmask & rdp->grpmask);
Paul E. McKenneyd09b62d2009-11-02 13:52:28 -08001543 }
Paul E. McKenney398ebe62013-03-19 10:53:14 -07001544
Paul E. McKenney67e14c12018-04-27 16:01:46 -07001545 /* Now handle the beginnings of any new-to-this-CPU grace periods. */
1546 if (rcu_seq_new_gp(rdp->gp_seq, rnp->gp_seq) ||
1547 unlikely(READ_ONCE(rdp->gpwrap))) {
Paul E. McKenney6eaef632013-03-19 10:08:37 -07001548 /*
1549 * If the current grace period is waiting for this CPU,
1550 * set up to detect a quiescent state, otherwise don't
1551 * go looking for one.
1552 */
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001553 trace_rcu_grace_period(rcu_state.name, rnp->gp_seq, TPS("cpustart"));
Paul E. McKenneyb5ea0372019-12-09 15:19:45 -08001554 need_qs = !!(rnp->qsmask & rdp->grpmask);
1555 rdp->cpu_no_qs.b.norm = need_qs;
1556 rdp->core_needs_qs = need_qs;
Paul E. McKenney6eaef632013-03-19 10:08:37 -07001557 zero_cpu_stall_ticks(rdp);
1558 }
Paul E. McKenney67e14c12018-04-27 16:01:46 -07001559 rdp->gp_seq = rnp->gp_seq; /* Remember new grace-period state. */
Zhang, Jun13dc7d02018-12-19 10:37:34 -08001560 if (ULONG_CMP_LT(rdp->gp_seq_needed, rnp->gp_seq_needed) || rdp->gpwrap)
Paul E. McKenney8ff372902020-01-04 11:33:17 -08001561 WRITE_ONCE(rdp->gp_seq_needed, rnp->gp_seq_needed);
Paul E. McKenney3d184692018-05-15 16:47:30 -07001562 WRITE_ONCE(rdp->gpwrap, false);
1563 rcu_gpnum_ovf(rnp, rdp);
Paul E. McKenney48a76392014-03-11 13:02:16 -07001564 return ret;
Paul E. McKenney6eaef632013-03-19 10:08:37 -07001565}
1566
Paul E. McKenney15cabdf2018-07-03 17:22:34 -07001567static void note_gp_changes(struct rcu_data *rdp)
Paul E. McKenney6eaef632013-03-19 10:08:37 -07001568{
1569 unsigned long flags;
Paul E. McKenney48a76392014-03-11 13:02:16 -07001570 bool needwake;
Paul E. McKenney6eaef632013-03-19 10:08:37 -07001571 struct rcu_node *rnp;
1572
1573 local_irq_save(flags);
1574 rnp = rdp->mynode;
Paul E. McKenney67e14c12018-04-27 16:01:46 -07001575 if ((rdp->gp_seq == rcu_seq_current(&rnp->gp_seq) &&
Paul E. McKenney7d0ae802015-03-03 14:57:58 -08001576 !unlikely(READ_ONCE(rdp->gpwrap))) || /* w/out lock. */
Peter Zijlstra2a67e742015-10-08 12:24:23 +02001577 !raw_spin_trylock_rcu_node(rnp)) { /* irqs already off, so later. */
Paul E. McKenney6eaef632013-03-19 10:08:37 -07001578 local_irq_restore(flags);
1579 return;
1580 }
Paul E. McKenneyc7e48f72018-07-03 17:22:34 -07001581 needwake = __note_gp_changes(rnp, rdp);
Boqun Feng67c583a72015-12-29 12:18:47 +08001582 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
Paul E. McKenney48a76392014-03-11 13:02:16 -07001583 if (needwake)
Paul E. McKenney532c00c2018-07-03 17:22:34 -07001584 rcu_gp_kthread_wake();
Paul E. McKenney6eaef632013-03-19 10:08:37 -07001585}
1586
Paul E. McKenney22212332018-07-03 17:22:34 -07001587static void rcu_gp_slow(int delay)
Paul E. McKenney0f41c0d2015-03-10 18:33:20 -07001588{
1589 if (delay > 0 &&
Paul E. McKenney22212332018-07-03 17:22:34 -07001590 !(rcu_seq_ctr(rcu_state.gp_seq) %
Paul E. McKenneydee4f422018-04-26 15:30:28 -07001591 (rcu_num_nodes * PER_RCU_NODE_PERIOD * delay)))
Paul E. McKenney0f41c0d2015-03-10 18:33:20 -07001592 schedule_timeout_uninterruptible(delay);
1593}
1594
Paul E. McKenney55b2dcf2020-04-01 19:57:52 -07001595static unsigned long sleep_duration;
1596
1597/* Allow rcutorture to stall the grace-period kthread. */
1598void rcu_gp_set_torture_wait(int duration)
1599{
1600 if (IS_ENABLED(CONFIG_RCU_TORTURE_TEST) && duration > 0)
1601 WRITE_ONCE(sleep_duration, duration);
1602}
1603EXPORT_SYMBOL_GPL(rcu_gp_set_torture_wait);
1604
1605/* Actually implement the aforementioned wait. */
1606static void rcu_gp_torture_wait(void)
1607{
1608 unsigned long duration;
1609
1610 if (!IS_ENABLED(CONFIG_RCU_TORTURE_TEST))
1611 return;
1612 duration = xchg(&sleep_duration, 0UL);
1613 if (duration > 0) {
1614 pr_alert("%s: Waiting %lu jiffies\n", __func__, duration);
1615 schedule_timeout_uninterruptible(duration);
1616 pr_alert("%s: Wait complete\n", __func__);
1617 }
1618}
1619
Paul E. McKenney6eaef632013-03-19 10:08:37 -07001620/*
Paul E. McKenney45fed3e2015-11-07 23:35:00 -08001621 * Initialize a new grace period. Return false if no grace period required.
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001622 */
Paul E. McKenney0854a05c2018-07-03 17:22:34 -07001623static bool rcu_gp_init(void)
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001624{
Paul E. McKenneyec2c2972018-05-07 09:34:17 -07001625 unsigned long flags;
Paul E. McKenney0aa04b02015-01-23 21:52:37 -08001626 unsigned long oldmask;
Paul E. McKenneyec2c2972018-05-07 09:34:17 -07001627 unsigned long mask;
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001628 struct rcu_data *rdp;
Paul E. McKenney336a4f62018-07-03 17:22:34 -07001629 struct rcu_node *rnp = rcu_get_root();
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001630
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001631 WRITE_ONCE(rcu_state.gp_activity, jiffies);
Peter Zijlstra2a67e742015-10-08 12:24:23 +02001632 raw_spin_lock_irq_rcu_node(rnp);
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001633 if (!READ_ONCE(rcu_state.gp_flags)) {
Paul E. McKenneyf7be8202013-08-08 18:27:52 -07001634 /* Spurious wakeup, tell caller to go back to sleep. */
Boqun Feng67c583a72015-12-29 12:18:47 +08001635 raw_spin_unlock_irq_rcu_node(rnp);
Paul E. McKenney45fed3e2015-11-07 23:35:00 -08001636 return false;
Paul E. McKenneyf7be8202013-08-08 18:27:52 -07001637 }
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001638 WRITE_ONCE(rcu_state.gp_flags, 0); /* Clear all flags: New GP. */
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001639
Paul E. McKenneyde8e8732018-07-03 17:22:34 -07001640 if (WARN_ON_ONCE(rcu_gp_in_progress())) {
Paul E. McKenneyf7be8202013-08-08 18:27:52 -07001641 /*
1642 * Grace period already in progress, don't start another.
1643 * Not supposed to be able to happen.
1644 */
Boqun Feng67c583a72015-12-29 12:18:47 +08001645 raw_spin_unlock_irq_rcu_node(rnp);
Paul E. McKenney45fed3e2015-11-07 23:35:00 -08001646 return false;
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001647 }
1648
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001649 /* Advance to a new grace period and initialize state. */
Paul E. McKenneyad3832e2018-07-03 17:22:34 -07001650 record_gp_stall_check_time();
Paul E. McKenneyff3bb6f2018-05-01 14:34:08 -07001651 /* Record GP times before starting GP, hence rcu_seq_start(). */
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001652 rcu_seq_start(&rcu_state.gp_seq);
Paul E. McKenney62ae1952020-03-21 19:52:20 -07001653 ASSERT_EXCLUSIVE_WRITER(rcu_state.gp_seq);
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001654 trace_rcu_grace_period(rcu_state.name, rcu_state.gp_seq, TPS("start"));
Boqun Feng67c583a72015-12-29 12:18:47 +08001655 raw_spin_unlock_irq_rcu_node(rnp);
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001656
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001657 /*
Paul E. McKenney0aa04b02015-01-23 21:52:37 -08001658 * Apply per-leaf buffered online and offline operations to the
1659 * rcu_node tree. Note that this new grace period need not wait
1660 * for subsequent online CPUs, and that quiescent-state forcing
1661 * will handle subsequent offline CPUs.
1662 */
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001663 rcu_state.gp_state = RCU_GP_ONOFF;
Paul E. McKenneyaedf4ba2018-07-04 14:33:59 -07001664 rcu_for_each_leaf_node(rnp) {
Mike Galbraith894d45b2018-08-15 09:05:29 -07001665 raw_spin_lock(&rcu_state.ofl_lock);
Peter Zijlstra2a67e742015-10-08 12:24:23 +02001666 raw_spin_lock_irq_rcu_node(rnp);
Paul E. McKenney0aa04b02015-01-23 21:52:37 -08001667 if (rnp->qsmaskinit == rnp->qsmaskinitnext &&
1668 !rnp->wait_blkd_tasks) {
1669 /* Nothing to do on this leaf rcu_node structure. */
Boqun Feng67c583a72015-12-29 12:18:47 +08001670 raw_spin_unlock_irq_rcu_node(rnp);
Mike Galbraith894d45b2018-08-15 09:05:29 -07001671 raw_spin_unlock(&rcu_state.ofl_lock);
Paul E. McKenney0aa04b02015-01-23 21:52:37 -08001672 continue;
1673 }
1674
1675 /* Record old state, apply changes to ->qsmaskinit field. */
1676 oldmask = rnp->qsmaskinit;
1677 rnp->qsmaskinit = rnp->qsmaskinitnext;
1678
1679 /* If zero-ness of ->qsmaskinit changed, propagate up tree. */
1680 if (!oldmask != !rnp->qsmaskinit) {
Paul E. McKenney962aff02018-05-02 12:49:21 -07001681 if (!oldmask) { /* First online CPU for rcu_node. */
1682 if (!rnp->wait_blkd_tasks) /* Ever offline? */
1683 rcu_init_new_rnp(rnp);
1684 } else if (rcu_preempt_has_tasks(rnp)) {
1685 rnp->wait_blkd_tasks = true; /* blocked tasks */
1686 } else { /* Last offline CPU and can propagate. */
Paul E. McKenney0aa04b02015-01-23 21:52:37 -08001687 rcu_cleanup_dead_rnp(rnp);
Paul E. McKenney962aff02018-05-02 12:49:21 -07001688 }
Paul E. McKenney0aa04b02015-01-23 21:52:37 -08001689 }
1690
1691 /*
1692 * If all waited-on tasks from prior grace period are
1693 * done, and if all this rcu_node structure's CPUs are
1694 * still offline, propagate up the rcu_node tree and
1695 * clear ->wait_blkd_tasks. Otherwise, if one of this
1696 * rcu_node structure's CPUs has since come back online,
Paul E. McKenney962aff02018-05-02 12:49:21 -07001697 * simply clear ->wait_blkd_tasks.
Paul E. McKenney0aa04b02015-01-23 21:52:37 -08001698 */
1699 if (rnp->wait_blkd_tasks &&
Paul E. McKenney962aff02018-05-02 12:49:21 -07001700 (!rcu_preempt_has_tasks(rnp) || rnp->qsmaskinit)) {
Paul E. McKenney0aa04b02015-01-23 21:52:37 -08001701 rnp->wait_blkd_tasks = false;
Paul E. McKenney962aff02018-05-02 12:49:21 -07001702 if (!rnp->qsmaskinit)
1703 rcu_cleanup_dead_rnp(rnp);
Paul E. McKenney0aa04b02015-01-23 21:52:37 -08001704 }
1705
Boqun Feng67c583a72015-12-29 12:18:47 +08001706 raw_spin_unlock_irq_rcu_node(rnp);
Mike Galbraith894d45b2018-08-15 09:05:29 -07001707 raw_spin_unlock(&rcu_state.ofl_lock);
Paul E. McKenney0aa04b02015-01-23 21:52:37 -08001708 }
Paul E. McKenney22212332018-07-03 17:22:34 -07001709 rcu_gp_slow(gp_preinit_delay); /* Races with CPU hotplug. */
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001710
1711 /*
1712 * Set the quiescent-state-needed bits in all the rcu_node
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001713 * structures for all currently online CPUs in breadth-first
1714 * order, starting from the root rcu_node structure, relying on the
1715 * layout of the tree within the rcu_state.node[] array. Note that
1716 * other CPUs will access only the leaves of the hierarchy, thus
1717 * seeing that no grace period is in progress, at least until the
1718 * corresponding leaf node has been initialized.
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001719 *
1720 * The grace period cannot complete until the initialization
1721 * process finishes, because this kthread handles both.
1722 */
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001723 rcu_state.gp_state = RCU_GP_INIT;
Paul E. McKenneyaedf4ba2018-07-04 14:33:59 -07001724 rcu_for_each_node_breadth_first(rnp) {
Paul E. McKenney22212332018-07-03 17:22:34 -07001725 rcu_gp_slow(gp_init_delay);
Paul E. McKenneyec2c2972018-05-07 09:34:17 -07001726 raw_spin_lock_irqsave_rcu_node(rnp, flags);
Paul E. McKenneyda1df502018-07-03 15:37:16 -07001727 rdp = this_cpu_ptr(&rcu_data);
Paul E. McKenney81ab59a2018-07-03 17:22:34 -07001728 rcu_preempt_check_blocked_tasks(rnp);
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001729 rnp->qsmask = rnp->qsmaskinit;
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001730 WRITE_ONCE(rnp->gp_seq, rcu_state.gp_seq);
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001731 if (rnp == rdp->mynode)
Paul E. McKenneyc7e48f72018-07-03 17:22:34 -07001732 (void)__note_gp_changes(rnp, rdp);
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001733 rcu_preempt_boost_start_gp(rnp);
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001734 trace_rcu_grace_period_init(rcu_state.name, rnp->gp_seq,
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001735 rnp->level, rnp->grplo,
1736 rnp->grphi, rnp->qsmask);
Paul E. McKenneyec2c2972018-05-07 09:34:17 -07001737 /* Quiescent states for tasks on any now-offline CPUs. */
1738 mask = rnp->qsmask & ~rnp->qsmaskinitnext;
Paul E. McKenneyf2e2df52018-05-15 16:23:23 -07001739 rnp->rcu_gp_init_mask = mask;
Paul E. McKenneyec2c2972018-05-07 09:34:17 -07001740 if ((mask || rnp->wait_blkd_tasks) && rcu_is_leaf_node(rnp))
Paul E. McKenneyb50912d2018-07-03 17:22:34 -07001741 rcu_report_qs_rnp(mask, rnp, rnp->gp_seq, flags);
Paul E. McKenneyec2c2972018-05-07 09:34:17 -07001742 else
1743 raw_spin_unlock_irq_rcu_node(rnp);
Paul E. McKenneycee43932018-03-02 16:35:27 -08001744 cond_resched_tasks_rcu_qs();
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001745 WRITE_ONCE(rcu_state.gp_activity, jiffies);
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001746 }
1747
Paul E. McKenney45fed3e2015-11-07 23:35:00 -08001748 return true;
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001749}
1750
1751/*
Peter Zijlstrab3dae102018-06-12 10:34:52 +02001752 * Helper function for swait_event_idle_exclusive() wakeup at force-quiescent-state
Luis R. Rodriguezd5374222017-06-20 14:45:47 -07001753 * time.
Paul E. McKenneyb9a425c2015-07-01 13:50:28 -07001754 */
Paul E. McKenney0854a05c2018-07-03 17:22:34 -07001755static bool rcu_gp_fqs_check_wake(int *gfp)
Paul E. McKenneyb9a425c2015-07-01 13:50:28 -07001756{
Paul E. McKenney336a4f62018-07-03 17:22:34 -07001757 struct rcu_node *rnp = rcu_get_root();
Paul E. McKenneyb9a425c2015-07-01 13:50:28 -07001758
Paul E. McKenney1fca4d12020-02-22 20:07:09 -08001759 // If under overload conditions, force an immediate FQS scan.
1760 if (*gfp & RCU_GP_FLAG_OVLD)
1761 return true;
1762
1763 // Someone like call_rcu() requested a force-quiescent-state scan.
Paul E. McKenney0854a05c2018-07-03 17:22:34 -07001764 *gfp = READ_ONCE(rcu_state.gp_flags);
Paul E. McKenneyb9a425c2015-07-01 13:50:28 -07001765 if (*gfp & RCU_GP_FLAG_FQS)
1766 return true;
1767
Paul E. McKenney1fca4d12020-02-22 20:07:09 -08001768 // The current grace period has completed.
Paul E. McKenneyb9a425c2015-07-01 13:50:28 -07001769 if (!READ_ONCE(rnp->qsmask) && !rcu_preempt_blocked_readers_cgp(rnp))
1770 return true;
1771
1772 return false;
1773}
1774
1775/*
Paul E. McKenney4cdfc1752012-06-22 17:06:26 -07001776 * Do one round of quiescent-state forcing.
1777 */
Paul E. McKenney0854a05c2018-07-03 17:22:34 -07001778static void rcu_gp_fqs(bool first_time)
Paul E. McKenney4cdfc1752012-06-22 17:06:26 -07001779{
Paul E. McKenney336a4f62018-07-03 17:22:34 -07001780 struct rcu_node *rnp = rcu_get_root();
Paul E. McKenney4cdfc1752012-06-22 17:06:26 -07001781
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001782 WRITE_ONCE(rcu_state.gp_activity, jiffies);
1783 rcu_state.n_force_qs++;
Petr Mladek77f81fe2015-09-09 12:09:49 -07001784 if (first_time) {
Paul E. McKenney4cdfc1752012-06-22 17:06:26 -07001785 /* Collect dyntick-idle snapshots. */
Paul E. McKenneye9ecb782018-07-03 17:22:34 -07001786 force_qs_rnp(dyntick_save_progress_counter);
Paul E. McKenney4cdfc1752012-06-22 17:06:26 -07001787 } else {
1788 /* Handle dyntick-idle and offline CPUs. */
Paul E. McKenneye9ecb782018-07-03 17:22:34 -07001789 force_qs_rnp(rcu_implicit_dynticks_qs);
Paul E. McKenney4cdfc1752012-06-22 17:06:26 -07001790 }
1791 /* Clear flag to prevent immediate re-entry. */
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001792 if (READ_ONCE(rcu_state.gp_flags) & RCU_GP_FLAG_FQS) {
Peter Zijlstra2a67e742015-10-08 12:24:23 +02001793 raw_spin_lock_irq_rcu_node(rnp);
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001794 WRITE_ONCE(rcu_state.gp_flags,
1795 READ_ONCE(rcu_state.gp_flags) & ~RCU_GP_FLAG_FQS);
Boqun Feng67c583a72015-12-29 12:18:47 +08001796 raw_spin_unlock_irq_rcu_node(rnp);
Paul E. McKenney4cdfc1752012-06-22 17:06:26 -07001797 }
Paul E. McKenney4cdfc1752012-06-22 17:06:26 -07001798}
1799
1800/*
Paul E. McKenneyc3854a02018-07-05 18:23:23 -07001801 * Loop doing repeated quiescent-state forcing until the grace period ends.
1802 */
1803static void rcu_gp_fqs_loop(void)
1804{
1805 bool first_gp_fqs;
Paul E. McKenney1fca4d12020-02-22 20:07:09 -08001806 int gf = 0;
Paul E. McKenneyc3854a02018-07-05 18:23:23 -07001807 unsigned long j;
1808 int ret;
1809 struct rcu_node *rnp = rcu_get_root();
1810
1811 first_gp_fqs = true;
Paul E. McKenneyc06aed02018-07-25 11:25:23 -07001812 j = READ_ONCE(jiffies_till_first_fqs);
Paul E. McKenney1fca4d12020-02-22 20:07:09 -08001813 if (rcu_state.cbovld)
1814 gf = RCU_GP_FLAG_OVLD;
Paul E. McKenneyc3854a02018-07-05 18:23:23 -07001815 ret = 0;
1816 for (;;) {
1817 if (!ret) {
1818 rcu_state.jiffies_force_qs = jiffies + j;
1819 WRITE_ONCE(rcu_state.jiffies_kick_kthreads,
Paul E. McKenney9cf422a2018-11-20 10:43:34 -08001820 jiffies + (j ? 3 * j : 2));
Paul E. McKenneyc3854a02018-07-05 18:23:23 -07001821 }
Paul E. McKenney0f11ad32020-02-10 09:58:37 -08001822 trace_rcu_grace_period(rcu_state.name, rcu_state.gp_seq,
Paul E. McKenneyc3854a02018-07-05 18:23:23 -07001823 TPS("fqswait"));
1824 rcu_state.gp_state = RCU_GP_WAIT_FQS;
1825 ret = swait_event_idle_timeout_exclusive(
1826 rcu_state.gp_wq, rcu_gp_fqs_check_wake(&gf), j);
Paul E. McKenney55b2dcf2020-04-01 19:57:52 -07001827 rcu_gp_torture_wait();
Paul E. McKenneyc3854a02018-07-05 18:23:23 -07001828 rcu_state.gp_state = RCU_GP_DOING_FQS;
1829 /* Locking provides needed memory barriers. */
1830 /* If grace period done, leave loop. */
1831 if (!READ_ONCE(rnp->qsmask) &&
1832 !rcu_preempt_blocked_readers_cgp(rnp))
1833 break;
1834 /* If time for quiescent-state forcing, do it. */
Paul E. McKenney29ffebc2020-04-10 14:48:20 -07001835 if (!time_after(rcu_state.jiffies_force_qs, jiffies) ||
Paul E. McKenneyc3854a02018-07-05 18:23:23 -07001836 (gf & RCU_GP_FLAG_FQS)) {
Paul E. McKenney0f11ad32020-02-10 09:58:37 -08001837 trace_rcu_grace_period(rcu_state.name, rcu_state.gp_seq,
Paul E. McKenneyc3854a02018-07-05 18:23:23 -07001838 TPS("fqsstart"));
1839 rcu_gp_fqs(first_gp_fqs);
Paul E. McKenney1fca4d12020-02-22 20:07:09 -08001840 gf = 0;
1841 if (first_gp_fqs) {
1842 first_gp_fqs = false;
1843 gf = rcu_state.cbovld ? RCU_GP_FLAG_OVLD : 0;
1844 }
Paul E. McKenney0f11ad32020-02-10 09:58:37 -08001845 trace_rcu_grace_period(rcu_state.name, rcu_state.gp_seq,
Paul E. McKenneyc3854a02018-07-05 18:23:23 -07001846 TPS("fqsend"));
1847 cond_resched_tasks_rcu_qs();
1848 WRITE_ONCE(rcu_state.gp_activity, jiffies);
1849 ret = 0; /* Force full wait till next FQS. */
Paul E. McKenneyc06aed02018-07-25 11:25:23 -07001850 j = READ_ONCE(jiffies_till_next_fqs);
Paul E. McKenneyc3854a02018-07-05 18:23:23 -07001851 } else {
1852 /* Deal with stray signal. */
1853 cond_resched_tasks_rcu_qs();
1854 WRITE_ONCE(rcu_state.gp_activity, jiffies);
1855 WARN_ON(signal_pending(current));
Paul E. McKenney0f11ad32020-02-10 09:58:37 -08001856 trace_rcu_grace_period(rcu_state.name, rcu_state.gp_seq,
Paul E. McKenneyc3854a02018-07-05 18:23:23 -07001857 TPS("fqswaitsig"));
1858 ret = 1; /* Keep old FQS timing. */
1859 j = jiffies;
1860 if (time_after(jiffies, rcu_state.jiffies_force_qs))
1861 j = 1;
1862 else
1863 j = rcu_state.jiffies_force_qs - j;
Paul E. McKenney1fca4d12020-02-22 20:07:09 -08001864 gf = 0;
Paul E. McKenneyc3854a02018-07-05 18:23:23 -07001865 }
1866 }
1867}
1868
1869/*
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001870 * Clean up after the old grace period.
1871 */
Paul E. McKenney0854a05c2018-07-03 17:22:34 -07001872static void rcu_gp_cleanup(void)
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001873{
Paul E. McKenneyb2b00dd2019-10-30 11:56:10 -07001874 int cpu;
Paul E. McKenney48a76392014-03-11 13:02:16 -07001875 bool needgp = false;
Paul E. McKenneyb2b00dd2019-10-30 11:56:10 -07001876 unsigned long gp_duration;
Paul E. McKenneyde30ad52018-04-26 11:52:09 -07001877 unsigned long new_gp_seq;
Paul E. McKenney5d6742b2019-05-15 09:56:40 -07001878 bool offloaded;
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001879 struct rcu_data *rdp;
Paul E. McKenney336a4f62018-07-03 17:22:34 -07001880 struct rcu_node *rnp = rcu_get_root();
Paul Gortmakerabedf8e2016-02-19 09:46:41 +01001881 struct swait_queue_head *sq;
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001882
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001883 WRITE_ONCE(rcu_state.gp_activity, jiffies);
Peter Zijlstra2a67e742015-10-08 12:24:23 +02001884 raw_spin_lock_irq_rcu_node(rnp);
Paul E. McKenneyc51d7b52018-10-03 17:25:33 -07001885 rcu_state.gp_end = jiffies;
1886 gp_duration = rcu_state.gp_end - rcu_state.gp_start;
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001887 if (gp_duration > rcu_state.gp_max)
1888 rcu_state.gp_max = gp_duration;
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001889
1890 /*
1891 * We know the grace period is complete, but to everyone else
1892 * it appears to still be ongoing. But it is also the case
1893 * that to everyone else it looks like there is nothing that
1894 * they can do to advance the grace period. It is therefore
1895 * safe for us to drop the lock in order to mark the grace
1896 * period as completed in all of the rcu_node structures.
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001897 */
Boqun Feng67c583a72015-12-29 12:18:47 +08001898 raw_spin_unlock_irq_rcu_node(rnp);
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001899
Paul E. McKenney5d4b8652012-07-07 07:56:57 -07001900 /*
Paul E. McKenneyff3bb6f2018-05-01 14:34:08 -07001901 * Propagate new ->gp_seq value to rcu_node structures so that
1902 * other CPUs don't have to wait until the start of the next grace
1903 * period to process their callbacks. This also avoids some nasty
1904 * RCU grace-period initialization races by forcing the end of
1905 * the current grace period to be completely recorded in all of
1906 * the rcu_node structures before the beginning of the next grace
1907 * period is recorded in any of the rcu_node structures.
Paul E. McKenney5d4b8652012-07-07 07:56:57 -07001908 */
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001909 new_gp_seq = rcu_state.gp_seq;
Paul E. McKenneyde30ad52018-04-26 11:52:09 -07001910 rcu_seq_end(&new_gp_seq);
Paul E. McKenneyaedf4ba2018-07-04 14:33:59 -07001911 rcu_for_each_node_breadth_first(rnp) {
Peter Zijlstra2a67e742015-10-08 12:24:23 +02001912 raw_spin_lock_irq_rcu_node(rnp);
Paul E. McKenney4bc8d552017-11-27 15:13:56 -08001913 if (WARN_ON_ONCE(rcu_preempt_blocked_readers_cgp(rnp)))
Paul E. McKenney81ab59a2018-07-03 17:22:34 -07001914 dump_blkd_tasks(rnp, 10);
Paul E. McKenney5c60d252015-02-09 05:37:47 -08001915 WARN_ON_ONCE(rnp->qsmask);
Paul E. McKenneyde30ad52018-04-26 11:52:09 -07001916 WRITE_ONCE(rnp->gp_seq, new_gp_seq);
Paul E. McKenneyda1df502018-07-03 15:37:16 -07001917 rdp = this_cpu_ptr(&rcu_data);
Paul E. McKenneyb11cc572012-12-17 14:21:14 -08001918 if (rnp == rdp->mynode)
Paul E. McKenneyc7e48f72018-07-03 17:22:34 -07001919 needgp = __note_gp_changes(rnp, rdp) || needgp;
Paul E. McKenney78e4bc32013-09-24 15:04:06 -07001920 /* smp_mb() provided by prior unlock-lock pair. */
Paul E. McKenney3481f2e2018-07-03 17:22:34 -07001921 needgp = rcu_future_gp_cleanup(rnp) || needgp;
Paul E. McKenneyb2b00dd2019-10-30 11:56:10 -07001922 // Reset overload indication for CPUs no longer overloaded
1923 if (rcu_is_leaf_node(rnp))
1924 for_each_leaf_node_cpu_mask(rnp, cpu, rnp->cbovldmask) {
1925 rdp = per_cpu_ptr(&rcu_data, cpu);
1926 check_cb_ovld_locked(rdp, rnp);
1927 }
Daniel Wagner065bb782016-02-19 09:46:40 +01001928 sq = rcu_nocb_gp_get(rnp);
Boqun Feng67c583a72015-12-29 12:18:47 +08001929 raw_spin_unlock_irq_rcu_node(rnp);
Daniel Wagner065bb782016-02-19 09:46:40 +01001930 rcu_nocb_gp_cleanup(sq);
Paul E. McKenneycee43932018-03-02 16:35:27 -08001931 cond_resched_tasks_rcu_qs();
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001932 WRITE_ONCE(rcu_state.gp_activity, jiffies);
Paul E. McKenney22212332018-07-03 17:22:34 -07001933 rcu_gp_slow(gp_cleanup_delay);
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001934 }
Paul E. McKenney336a4f62018-07-03 17:22:34 -07001935 rnp = rcu_get_root();
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001936 raw_spin_lock_irq_rcu_node(rnp); /* GP before ->gp_seq update. */
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001937
Paul E. McKenney0a89e5a2018-10-15 10:00:58 -07001938 /* Declare grace period done, trace first to use old GP number. */
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001939 trace_rcu_grace_period(rcu_state.name, rcu_state.gp_seq, TPS("end"));
Paul E. McKenney0a89e5a2018-10-15 10:00:58 -07001940 rcu_seq_end(&rcu_state.gp_seq);
Paul E. McKenney62ae1952020-03-21 19:52:20 -07001941 ASSERT_EXCLUSIVE_WRITER(rcu_state.gp_seq);
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001942 rcu_state.gp_state = RCU_GP_IDLE;
Paul E. McKenneyfb313402018-04-12 07:20:30 -07001943 /* Check for GP requests since above loop. */
Paul E. McKenneyda1df502018-07-03 15:37:16 -07001944 rdp = this_cpu_ptr(&rcu_data);
Joel Fernandes (Google)5b550722018-05-13 20:15:40 -07001945 if (!needgp && ULONG_CMP_LT(rnp->gp_seq, rnp->gp_seq_needed)) {
Paul E. McKenneyabd13fd2018-05-01 13:08:46 -07001946 trace_rcu_this_gp(rnp, rdp, rnp->gp_seq_needed,
Paul E. McKenney41e80592018-04-12 11:24:09 -07001947 TPS("CleanupMore"));
Paul E. McKenneyfb313402018-04-12 07:20:30 -07001948 needgp = true;
1949 }
Paul E. McKenney48a76392014-03-11 13:02:16 -07001950 /* Advance CBs to reduce false positives below. */
Paul E. McKenney5d6742b2019-05-15 09:56:40 -07001951 offloaded = IS_ENABLED(CONFIG_RCU_NOCB_CPU) &&
1952 rcu_segcblist_is_offloaded(&rdp->cblist);
1953 if ((offloaded || !rcu_accelerate_cbs(rnp, rdp)) && needgp) {
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001954 WRITE_ONCE(rcu_state.gp_flags, RCU_GP_FLAG_INIT);
Paul E. McKenney2906d212020-01-03 15:17:12 -08001955 WRITE_ONCE(rcu_state.gp_req_activity, jiffies);
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001956 trace_rcu_grace_period(rcu_state.name,
Paul E. McKenney0f11ad32020-02-10 09:58:37 -08001957 rcu_state.gp_seq,
Paul E. McKenneybb311ec2013-08-09 16:02:09 -07001958 TPS("newreq"));
Paul E. McKenney18390ae2018-04-22 15:06:05 -07001959 } else {
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001960 WRITE_ONCE(rcu_state.gp_flags,
1961 rcu_state.gp_flags & RCU_GP_FLAG_INIT);
Paul E. McKenneybb311ec2013-08-09 16:02:09 -07001962 }
Boqun Feng67c583a72015-12-29 12:18:47 +08001963 raw_spin_unlock_irq_rcu_node(rnp);
Paul E. McKenney7fdefc12012-06-22 11:08:41 -07001964}
1965
1966/*
Paul E. McKenneyb3dbec72012-06-18 18:36:08 -07001967 * Body of kthread that handles grace periods.
1968 */
Paul E. McKenney0854a05c2018-07-03 17:22:34 -07001969static int __noreturn rcu_gp_kthread(void *unused)
Paul E. McKenneyb3dbec72012-06-18 18:36:08 -07001970{
Paul E. McKenney58719682015-02-24 11:05:36 -08001971 rcu_bind_gp_kthread();
Paul E. McKenneyb3dbec72012-06-18 18:36:08 -07001972 for (;;) {
1973
1974 /* Handle grace-period start. */
Paul E. McKenneyb3dbec72012-06-18 18:36:08 -07001975 for (;;) {
Paul E. McKenney0f11ad32020-02-10 09:58:37 -08001976 trace_rcu_grace_period(rcu_state.name, rcu_state.gp_seq,
Paul E. McKenney63c4db72013-08-09 12:19:29 -07001977 TPS("reqwait"));
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001978 rcu_state.gp_state = RCU_GP_WAIT_GPS;
1979 swait_event_idle_exclusive(rcu_state.gp_wq,
1980 READ_ONCE(rcu_state.gp_flags) &
1981 RCU_GP_FLAG_INIT);
Paul E. McKenney55b2dcf2020-04-01 19:57:52 -07001982 rcu_gp_torture_wait();
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001983 rcu_state.gp_state = RCU_GP_DONE_GPS;
Paul E. McKenney78e4bc32013-09-24 15:04:06 -07001984 /* Locking provides needed memory barrier. */
Paul E. McKenney0854a05c2018-07-03 17:22:34 -07001985 if (rcu_gp_init())
Paul E. McKenneyb3dbec72012-06-18 18:36:08 -07001986 break;
Paul E. McKenneycee43932018-03-02 16:35:27 -08001987 cond_resched_tasks_rcu_qs();
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001988 WRITE_ONCE(rcu_state.gp_activity, jiffies);
Paul E. McKenney73a860c2014-08-14 10:28:23 -07001989 WARN_ON(signal_pending(current));
Paul E. McKenney0f11ad32020-02-10 09:58:37 -08001990 trace_rcu_grace_period(rcu_state.name, rcu_state.gp_seq,
Paul E. McKenney63c4db72013-08-09 12:19:29 -07001991 TPS("reqwaitsig"));
Paul E. McKenneyb3dbec72012-06-18 18:36:08 -07001992 }
Paul E. McKenneycabc49c2012-06-20 17:07:14 -07001993
Paul E. McKenney4cdfc1752012-06-22 17:06:26 -07001994 /* Handle quiescent-state forcing. */
Paul E. McKenneyc3854a02018-07-05 18:23:23 -07001995 rcu_gp_fqs_loop();
Paul E. McKenney4cdfc1752012-06-22 17:06:26 -07001996
1997 /* Handle grace-period end. */
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07001998 rcu_state.gp_state = RCU_GP_CLEANUP;
Paul E. McKenney0854a05c2018-07-03 17:22:34 -07001999 rcu_gp_cleanup();
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07002000 rcu_state.gp_state = RCU_GP_CLEANED;
Paul E. McKenneyb3dbec72012-06-18 18:36:08 -07002001 }
Paul E. McKenneyb3dbec72012-06-18 18:36:08 -07002002}
2003
2004/*
Paul E. McKenney49918a52018-07-07 18:12:26 -07002005 * Report a full set of quiescent states to the rcu_state data structure.
2006 * Invoke rcu_gp_kthread_wake() to awaken the grace-period kthread if
2007 * another grace period is required. Whether we wake the grace-period
2008 * kthread or it awakens itself for the next round of quiescent-state
2009 * forcing, that kthread will clean up after the just-completed grace
2010 * period. Note that the caller must hold rnp->lock, which is released
2011 * before return.
Paul E. McKenneyf41d9112009-08-22 13:56:52 -07002012 */
Paul E. McKenneyaff4e9e2018-07-03 17:22:34 -07002013static void rcu_report_qs_rsp(unsigned long flags)
Paul E. McKenney336a4f62018-07-03 17:22:34 -07002014 __releases(rcu_get_root()->lock)
Paul E. McKenneyf41d9112009-08-22 13:56:52 -07002015{
Paul E. McKenney336a4f62018-07-03 17:22:34 -07002016 raw_lockdep_assert_held_rcu_node(rcu_get_root());
Paul E. McKenneyde8e8732018-07-03 17:22:34 -07002017 WARN_ON_ONCE(!rcu_gp_in_progress());
Paul E. McKenney9cbc5b92018-07-05 15:47:01 -07002018 WRITE_ONCE(rcu_state.gp_flags,
2019 READ_ONCE(rcu_state.gp_flags) | RCU_GP_FLAG_FQS);
Paul E. McKenney336a4f62018-07-03 17:22:34 -07002020 raw_spin_unlock_irqrestore_rcu_node(rcu_get_root(), flags);
Paul E. McKenney532c00c2018-07-03 17:22:34 -07002021 rcu_gp_kthread_wake();
Paul E. McKenneyf41d9112009-08-22 13:56:52 -07002022}
2023
2024/*
Paul E. McKenneyd3f6bad2009-12-02 12:10:13 -08002025 * Similar to rcu_report_qs_rdp(), for which it is a helper function.
2026 * Allows quiescent states for a group of CPUs to be reported at one go
2027 * to the specified rcu_node structure, though all the CPUs in the group
Paul E. McKenney654e9532015-03-15 09:19:35 -07002028 * must be represented by the same rcu_node structure (which need not be a
2029 * leaf rcu_node structure, though it often will be). The gps parameter
2030 * is the grace-period snapshot, which means that the quiescent states
Paul E. McKenneyc9a24e22018-04-27 14:54:46 -07002031 * are valid only if rnp->gp_seq is equal to gps. That structure's lock
Paul E. McKenney654e9532015-03-15 09:19:35 -07002032 * must be held upon entry, and it is released before return.
Paul E. McKenneyec2c2972018-05-07 09:34:17 -07002033 *
2034 * As a special case, if mask is zero, the bit-already-cleared check is
2035 * disabled. This allows propagating quiescent state due to resumed tasks
2036 * during grace-period initialization.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002037 */
Paul E. McKenneyb50912d2018-07-03 17:22:34 -07002038static void rcu_report_qs_rnp(unsigned long mask, struct rcu_node *rnp,
2039 unsigned long gps, unsigned long flags)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002040 __releases(rnp->lock)
2041{
Paul E. McKenney654e9532015-03-15 09:19:35 -07002042 unsigned long oldmask = 0;
Paul E. McKenney28ecd582009-09-18 09:50:17 -07002043 struct rcu_node *rnp_c;
2044
Matthew Wilcoxa32e01e2018-01-17 06:24:30 -08002045 raw_lockdep_assert_held_rcu_node(rnp);
Paul E. McKenneyc0b334c2017-04-28 12:32:15 -07002046
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002047 /* Walk up the rcu_node hierarchy. */
2048 for (;;) {
Paul E. McKenneyec2c2972018-05-07 09:34:17 -07002049 if ((!(rnp->qsmask & mask) && mask) || rnp->gp_seq != gps) {
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002050
Paul E. McKenney654e9532015-03-15 09:19:35 -07002051 /*
2052 * Our bit has already been cleared, or the
2053 * relevant grace period is already over, so done.
2054 */
Boqun Feng67c583a72015-12-29 12:18:47 +08002055 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002056 return;
2057 }
Paul E. McKenney654e9532015-03-15 09:19:35 -07002058 WARN_ON_ONCE(oldmask); /* Any child must be all zeroed! */
Paul E. McKenney5b4c11d2018-04-13 17:11:44 -07002059 WARN_ON_ONCE(!rcu_is_leaf_node(rnp) &&
Paul E. McKenney2dee9402017-07-11 21:52:31 -07002060 rcu_preempt_blocked_readers_cgp(rnp));
Paul E. McKenney7672d642020-01-03 11:38:51 -08002061 WRITE_ONCE(rnp->qsmask, rnp->qsmask & ~mask);
Paul E. McKenney67a0edb2018-07-05 16:15:38 -07002062 trace_rcu_quiescent_state_report(rcu_state.name, rnp->gp_seq,
Paul E. McKenneyd4c08f22011-06-25 06:36:56 -07002063 mask, rnp->qsmask, rnp->level,
2064 rnp->grplo, rnp->grphi,
2065 !!rnp->gp_tasks);
Paul E. McKenney27f4d282011-02-07 12:47:15 -08002066 if (rnp->qsmask != 0 || rcu_preempt_blocked_readers_cgp(rnp)) {
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002067
2068 /* Other bits still set at this level, so done. */
Boqun Feng67c583a72015-12-29 12:18:47 +08002069 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002070 return;
2071 }
Paul E. McKenneyd43a5d32018-04-28 18:50:06 -07002072 rnp->completedqs = rnp->gp_seq;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002073 mask = rnp->grpmask;
2074 if (rnp->parent == NULL) {
2075
2076 /* No more levels. Exit loop holding root lock. */
2077
2078 break;
2079 }
Boqun Feng67c583a72015-12-29 12:18:47 +08002080 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
Paul E. McKenney28ecd582009-09-18 09:50:17 -07002081 rnp_c = rnp;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002082 rnp = rnp->parent;
Peter Zijlstra2a67e742015-10-08 12:24:23 +02002083 raw_spin_lock_irqsave_rcu_node(rnp, flags);
Paul E. McKenney0937d042020-01-03 14:53:31 -08002084 oldmask = READ_ONCE(rnp_c->qsmask);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002085 }
2086
2087 /*
2088 * Get here if we are the last CPU to pass through a quiescent
Paul E. McKenneyd3f6bad2009-12-02 12:10:13 -08002089 * state for this grace period. Invoke rcu_report_qs_rsp()
Paul E. McKenneyf41d9112009-08-22 13:56:52 -07002090 * to clean up and start the next grace period if one is needed.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002091 */
Paul E. McKenneyaff4e9e2018-07-03 17:22:34 -07002092 rcu_report_qs_rsp(flags); /* releases rnp->lock. */
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002093}
2094
2095/*
Paul E. McKenneycc99a312015-02-23 08:59:29 -08002096 * Record a quiescent state for all tasks that were previously queued
2097 * on the specified rcu_node structure and that were blocking the current
Paul E. McKenney49918a52018-07-07 18:12:26 -07002098 * RCU grace period. The caller must hold the corresponding rnp->lock with
Paul E. McKenneycc99a312015-02-23 08:59:29 -08002099 * irqs disabled, and this lock is released upon return, but irqs remain
2100 * disabled.
2101 */
Paul E. McKenney17a82122018-05-03 14:30:02 -07002102static void __maybe_unused
Paul E. McKenney139ad4d2018-07-03 17:22:34 -07002103rcu_report_unblock_qs_rnp(struct rcu_node *rnp, unsigned long flags)
Paul E. McKenneycc99a312015-02-23 08:59:29 -08002104 __releases(rnp->lock)
2105{
Paul E. McKenney654e9532015-03-15 09:19:35 -07002106 unsigned long gps;
Paul E. McKenneycc99a312015-02-23 08:59:29 -08002107 unsigned long mask;
2108 struct rcu_node *rnp_p;
2109
Matthew Wilcoxa32e01e2018-01-17 06:24:30 -08002110 raw_lockdep_assert_held_rcu_node(rnp);
Lai Jiangshanc130d2d2019-10-15 10:28:48 +00002111 if (WARN_ON_ONCE(!IS_ENABLED(CONFIG_PREEMPT_RCU)) ||
Paul E. McKenneyc74859d2018-04-27 14:05:27 -07002112 WARN_ON_ONCE(rcu_preempt_blocked_readers_cgp(rnp)) ||
2113 rnp->qsmask != 0) {
Boqun Feng67c583a72015-12-29 12:18:47 +08002114 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
Paul E. McKenneycc99a312015-02-23 08:59:29 -08002115 return; /* Still need more quiescent states! */
2116 }
2117
Paul E. McKenney77cfc7b2018-05-01 15:00:10 -07002118 rnp->completedqs = rnp->gp_seq;
Paul E. McKenneycc99a312015-02-23 08:59:29 -08002119 rnp_p = rnp->parent;
2120 if (rnp_p == NULL) {
2121 /*
Paul E. McKenneya77da142015-03-08 14:52:27 -07002122 * Only one rcu_node structure in the tree, so don't
2123 * try to report up to its nonexistent parent!
Paul E. McKenneycc99a312015-02-23 08:59:29 -08002124 */
Paul E. McKenneyaff4e9e2018-07-03 17:22:34 -07002125 rcu_report_qs_rsp(flags);
Paul E. McKenneycc99a312015-02-23 08:59:29 -08002126 return;
2127 }
2128
Paul E. McKenneyc9a24e22018-04-27 14:54:46 -07002129 /* Report up the rest of the hierarchy, tracking current ->gp_seq. */
2130 gps = rnp->gp_seq;
Paul E. McKenneycc99a312015-02-23 08:59:29 -08002131 mask = rnp->grpmask;
Boqun Feng67c583a72015-12-29 12:18:47 +08002132 raw_spin_unlock_rcu_node(rnp); /* irqs remain disabled. */
Peter Zijlstra2a67e742015-10-08 12:24:23 +02002133 raw_spin_lock_rcu_node(rnp_p); /* irqs already disabled. */
Paul E. McKenneyb50912d2018-07-03 17:22:34 -07002134 rcu_report_qs_rnp(mask, rnp_p, gps, flags);
Paul E. McKenneycc99a312015-02-23 08:59:29 -08002135}
2136
2137/*
Paul E. McKenneyd3f6bad2009-12-02 12:10:13 -08002138 * Record a quiescent state for the specified CPU to that CPU's rcu_data
Paul E. McKenney4b455dc2016-01-27 22:44:45 -08002139 * structure. This must be called from the specified CPU.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002140 */
2141static void
Paul E. McKenney33085c42018-07-03 17:22:34 -07002142rcu_report_qs_rdp(int cpu, struct rcu_data *rdp)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002143{
2144 unsigned long flags;
2145 unsigned long mask;
Paul E. McKenney5d6742b2019-05-15 09:56:40 -07002146 bool needwake = false;
2147 const bool offloaded = IS_ENABLED(CONFIG_RCU_NOCB_CPU) &&
2148 rcu_segcblist_is_offloaded(&rdp->cblist);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002149 struct rcu_node *rnp;
2150
2151 rnp = rdp->mynode;
Peter Zijlstra2a67e742015-10-08 12:24:23 +02002152 raw_spin_lock_irqsave_rcu_node(rnp, flags);
Paul E. McKenneyc9a24e22018-04-27 14:54:46 -07002153 if (rdp->cpu_no_qs.b.norm || rdp->gp_seq != rnp->gp_seq ||
2154 rdp->gpwrap) {
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002155
2156 /*
Paul E. McKenneye4cc1f22011-06-27 00:17:43 -07002157 * The grace period in which this quiescent state was
2158 * recorded has ended, so don't report it upwards.
2159 * We will instead need a new quiescent state that lies
2160 * within the current grace period.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002161 */
Paul E. McKenney5b74c452015-08-06 15:16:57 -07002162 rdp->cpu_no_qs.b.norm = true; /* need qs for new gp. */
Boqun Feng67c583a72015-12-29 12:18:47 +08002163 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002164 return;
2165 }
2166 mask = rdp->grpmask;
Paul E. McKenneyb5ea0372019-12-09 15:19:45 -08002167 if (rdp->cpu == smp_processor_id())
2168 rdp->core_needs_qs = false;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002169 if ((rnp->qsmask & mask) == 0) {
Boqun Feng67c583a72015-12-29 12:18:47 +08002170 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002171 } else {
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002172 /*
2173 * This GP can't end until cpu checks in, so all of our
2174 * callbacks can be processed during the next GP.
2175 */
Paul E. McKenney5d6742b2019-05-15 09:56:40 -07002176 if (!offloaded)
2177 needwake = rcu_accelerate_cbs(rnp, rdp);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002178
Joel Fernandes (Google)516e5ae2019-09-05 10:26:41 -07002179 rcu_disable_urgency_upon_qs(rdp);
Paul E. McKenneyb50912d2018-07-03 17:22:34 -07002180 rcu_report_qs_rnp(mask, rnp, rnp->gp_seq, flags);
Paul E. McKenney654e9532015-03-15 09:19:35 -07002181 /* ^^^ Released rnp->lock */
Paul E. McKenney48a76392014-03-11 13:02:16 -07002182 if (needwake)
Paul E. McKenney532c00c2018-07-03 17:22:34 -07002183 rcu_gp_kthread_wake();
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002184 }
2185}
2186
2187/*
2188 * Check to see if there is a new grace period of which this CPU
2189 * is not yet aware, and if so, set up local rcu_data state for it.
2190 * Otherwise, see if this CPU has just passed through its first
2191 * quiescent state for this grace period, and record that fact if so.
2192 */
2193static void
Paul E. McKenney8087d3e2018-07-03 17:22:34 -07002194rcu_check_quiescent_state(struct rcu_data *rdp)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002195{
Paul E. McKenney05eb5522013-03-19 12:38:24 -07002196 /* Check for grace-period ends and beginnings. */
Paul E. McKenney15cabdf2018-07-03 17:22:34 -07002197 note_gp_changes(rdp);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002198
2199 /*
2200 * Does this CPU still need to do its part for current grace period?
2201 * If no, return and let the other CPUs do their part as well.
2202 */
Paul E. McKenney97c668b2015-08-06 11:31:51 -07002203 if (!rdp->core_needs_qs)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002204 return;
2205
2206 /*
2207 * Was there a quiescent state since the beginning of the grace
2208 * period? If no, then exit and wait for the next call.
2209 */
Paul E. McKenney3a19b462016-11-30 11:21:21 -08002210 if (rdp->cpu_no_qs.b.norm)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002211 return;
2212
Paul E. McKenneyd3f6bad2009-12-02 12:10:13 -08002213 /*
2214 * Tell RCU we are done (but rcu_report_qs_rdp() will be the
2215 * judge of that).
2216 */
Paul E. McKenney33085c42018-07-03 17:22:34 -07002217 rcu_report_qs_rdp(rdp->cpu, rdp);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002218}
2219
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002220/*
Paul E. McKenney780cd592018-07-03 17:22:34 -07002221 * Near the end of the offline process. Trace the fact that this CPU
2222 * is going offline.
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08002223 */
Paul E. McKenney780cd592018-07-03 17:22:34 -07002224int rcutree_dying_cpu(unsigned int cpu)
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08002225{
Yafang Shao4f5fbd72019-03-26 20:13:11 +08002226 bool blkd;
2227 struct rcu_data *rdp = this_cpu_ptr(&rcu_data);
2228 struct rcu_node *rnp = rdp->mynode;
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08002229
Paul E. McKenneyea463512015-03-03 14:05:26 -08002230 if (!IS_ENABLED(CONFIG_HOTPLUG_CPU))
Paul E. McKenney780cd592018-07-03 17:22:34 -07002231 return 0;
Paul E. McKenneyea463512015-03-03 14:05:26 -08002232
Yafang Shao4f5fbd72019-03-26 20:13:11 +08002233 blkd = !!(rnp->qsmask & rdp->grpmask);
Paul E. McKenney0937d042020-01-03 14:53:31 -08002234 trace_rcu_grace_period(rcu_state.name, READ_ONCE(rnp->gp_seq),
Paul E. McKenney477351f2018-05-01 12:54:11 -07002235 blkd ? TPS("cpuofl") : TPS("cpuofl-bgp"));
Paul E. McKenney780cd592018-07-03 17:22:34 -07002236 return 0;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002237}
2238
2239/*
Paul E. McKenney8af3a5e2014-10-31 11:22:37 -07002240 * All CPUs for the specified rcu_node structure have gone offline,
2241 * and all tasks that were preempted within an RCU read-side critical
2242 * section while running on one of those CPUs have since exited their RCU
2243 * read-side critical section. Some other CPU is reporting this fact with
2244 * the specified rcu_node structure's ->lock held and interrupts disabled.
2245 * This function therefore goes up the tree of rcu_node structures,
2246 * clearing the corresponding bits in the ->qsmaskinit fields. Note that
2247 * the leaf rcu_node structure's ->qsmaskinit field has already been
Paul E. McKenneyc50cbe52018-05-02 13:51:57 -07002248 * updated.
Paul E. McKenney8af3a5e2014-10-31 11:22:37 -07002249 *
2250 * This function does check that the specified rcu_node structure has
2251 * all CPUs offline and no blocked tasks, so it is OK to invoke it
2252 * prematurely. That said, invoking it after the fact will cost you
2253 * a needless lock acquisition. So once it has done its work, don't
2254 * invoke it again.
2255 */
2256static void rcu_cleanup_dead_rnp(struct rcu_node *rnp_leaf)
2257{
2258 long mask;
2259 struct rcu_node *rnp = rnp_leaf;
2260
Paul E. McKenney962aff02018-05-02 12:49:21 -07002261 raw_lockdep_assert_held_rcu_node(rnp_leaf);
Paul E. McKenneyea463512015-03-03 14:05:26 -08002262 if (!IS_ENABLED(CONFIG_HOTPLUG_CPU) ||
Paul E. McKenney962aff02018-05-02 12:49:21 -07002263 WARN_ON_ONCE(rnp_leaf->qsmaskinit) ||
2264 WARN_ON_ONCE(rcu_preempt_has_tasks(rnp_leaf)))
Paul E. McKenney8af3a5e2014-10-31 11:22:37 -07002265 return;
2266 for (;;) {
2267 mask = rnp->grpmask;
2268 rnp = rnp->parent;
2269 if (!rnp)
2270 break;
Peter Zijlstra2a67e742015-10-08 12:24:23 +02002271 raw_spin_lock_rcu_node(rnp); /* irqs already disabled. */
Paul E. McKenney8af3a5e2014-10-31 11:22:37 -07002272 rnp->qsmaskinit &= ~mask;
Paul E. McKenney962aff02018-05-02 12:49:21 -07002273 /* Between grace periods, so better already be zero! */
2274 WARN_ON_ONCE(rnp->qsmask);
Paul E. McKenney8af3a5e2014-10-31 11:22:37 -07002275 if (rnp->qsmaskinit) {
Boqun Feng67c583a72015-12-29 12:18:47 +08002276 raw_spin_unlock_rcu_node(rnp);
2277 /* irqs remain disabled. */
Paul E. McKenney8af3a5e2014-10-31 11:22:37 -07002278 return;
2279 }
Boqun Feng67c583a72015-12-29 12:18:47 +08002280 raw_spin_unlock_rcu_node(rnp); /* irqs remain disabled. */
Paul E. McKenney8af3a5e2014-10-31 11:22:37 -07002281 }
2282}
2283
2284/*
Paul E. McKenneye5601402012-01-07 11:03:57 -08002285 * The CPU has been completely removed, and some other CPU is reporting
Paul E. McKenneya58163d2017-06-20 12:11:34 -07002286 * this fact from process context. Do the remainder of the cleanup.
2287 * There can only be one CPU hotplug operation at a time, so no need for
2288 * explicit locking.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002289 */
Paul E. McKenney780cd592018-07-03 17:22:34 -07002290int rcutree_dead_cpu(unsigned int cpu)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002291{
Paul E. McKenneyda1df502018-07-03 15:37:16 -07002292 struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08002293 struct rcu_node *rnp = rdp->mynode; /* Outgoing CPU's rdp & rnp. */
Paul E. McKenneye5601402012-01-07 11:03:57 -08002294
Paul E. McKenneyea463512015-03-03 14:05:26 -08002295 if (!IS_ENABLED(CONFIG_HOTPLUG_CPU))
Paul E. McKenney780cd592018-07-03 17:22:34 -07002296 return 0;
Paul E. McKenneyea463512015-03-03 14:05:26 -08002297
Paul E. McKenney2036d942012-01-30 17:02:47 -08002298 /* Adjust any no-longer-needed kthreads. */
Thomas Gleixner5d01bbd2012-07-16 10:42:35 +00002299 rcu_boost_kthread_setaffinity(rnp, -1);
Paul E. McKenney780cd592018-07-03 17:22:34 -07002300 /* Do any needed no-CB deferred wakeups from this CPU. */
2301 do_nocb_deferred_wakeup(per_cpu_ptr(&rcu_data, cpu));
Paul E. McKenney96926682019-08-02 15:12:47 -07002302
2303 // Stop-machine done, so allow nohz_full to disable tick.
2304 tick_dep_clear(TICK_DEP_BIT_RCU);
Paul E. McKenney780cd592018-07-03 17:22:34 -07002305 return 0;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002306}
2307
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002308/*
2309 * Invoke any RCU callbacks that have made it to the end of their grace
2310 * period. Thottle as specified by rdp->blimit.
2311 */
Paul E. McKenney5bb5d092018-07-03 17:22:34 -07002312static void rcu_do_batch(struct rcu_data *rdp)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002313{
2314 unsigned long flags;
Paul E. McKenneyec5ef872019-05-21 13:03:49 -07002315 const bool offloaded = IS_ENABLED(CONFIG_RCU_NOCB_CPU) &&
2316 rcu_segcblist_is_offloaded(&rdp->cblist);
Paul E. McKenney15fecf82017-02-08 12:36:42 -08002317 struct rcu_head *rhp;
2318 struct rcu_cblist rcl = RCU_CBLIST_INITIALIZER(rcl);
2319 long bl, count;
Eric Dumazetcfcdef52019-07-24 18:07:52 -07002320 long pending, tlimit = 0;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002321
Paul E. McKenneydc35c892012-12-03 13:52:00 -08002322 /* If no callbacks are ready, just return. */
Paul E. McKenney15fecf82017-02-08 12:36:42 -08002323 if (!rcu_segcblist_ready_cbs(&rdp->cblist)) {
Paul E. McKenney3c779df2018-07-05 15:54:02 -07002324 trace_rcu_batch_start(rcu_state.name,
Paul E. McKenney15fecf82017-02-08 12:36:42 -08002325 rcu_segcblist_n_cbs(&rdp->cblist), 0);
Paul E. McKenney3c779df2018-07-05 15:54:02 -07002326 trace_rcu_batch_end(rcu_state.name, 0,
Paul E. McKenney15fecf82017-02-08 12:36:42 -08002327 !rcu_segcblist_empty(&rdp->cblist),
Paul E. McKenney4968c302011-12-07 16:32:40 -08002328 need_resched(), is_idle_task(current),
2329 rcu_is_callbacks_kthread());
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002330 return;
Paul E. McKenney29c00b42011-06-17 15:53:19 -07002331 }
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002332
2333 /*
2334 * Extract the list of ready callbacks, disabling to prevent
Paul E. McKenney15fecf82017-02-08 12:36:42 -08002335 * races with call_rcu() from interrupt handlers. Leave the
2336 * callback counts, as rcu_barrier() needs to be conservative.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002337 */
2338 local_irq_save(flags);
Paul E. McKenney5d6742b2019-05-15 09:56:40 -07002339 rcu_nocb_lock(rdp);
Paul E. McKenney8146c4e22012-01-10 14:23:29 -08002340 WARN_ON_ONCE(cpu_is_offline(smp_processor_id()));
Eric Dumazetcfcdef52019-07-24 18:07:52 -07002341 pending = rcu_segcblist_n_cbs(&rdp->cblist);
2342 bl = max(rdp->blimit, pending >> rcu_divisor);
2343 if (unlikely(bl > 100))
2344 tlimit = local_clock() + rcu_resched_ns;
Paul E. McKenney3c779df2018-07-05 15:54:02 -07002345 trace_rcu_batch_start(rcu_state.name,
Paul E. McKenney15fecf82017-02-08 12:36:42 -08002346 rcu_segcblist_n_cbs(&rdp->cblist), bl);
2347 rcu_segcblist_extract_done_cbs(&rdp->cblist, &rcl);
Paul E. McKenney7f36ef82019-05-28 05:54:26 -07002348 if (offloaded)
2349 rdp->qlen_last_fqs_check = rcu_segcblist_n_cbs(&rdp->cblist);
Paul E. McKenney5d6742b2019-05-15 09:56:40 -07002350 rcu_nocb_unlock_irqrestore(rdp, flags);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002351
2352 /* Invoke callbacks. */
Paul E. McKenney6a949b72019-07-28 11:50:56 -07002353 tick_dep_set_task(current, TICK_DEP_BIT_RCU);
Paul E. McKenney15fecf82017-02-08 12:36:42 -08002354 rhp = rcu_cblist_dequeue(&rcl);
2355 for (; rhp; rhp = rcu_cblist_dequeue(&rcl)) {
Joel Fernandes (Google)77a40f92019-08-30 12:36:32 -04002356 rcu_callback_t f;
2357
Paul E. McKenney15fecf82017-02-08 12:36:42 -08002358 debug_rcu_head_unqueue(rhp);
Joel Fernandes (Google)77a40f92019-08-30 12:36:32 -04002359
2360 rcu_lock_acquire(&rcu_callback_map);
2361 trace_rcu_invoke_callback(rcu_state.name, rhp);
2362
2363 f = rhp->func;
2364 WRITE_ONCE(rhp->func, (rcu_callback_t)0L);
2365 f(rhp);
2366
2367 rcu_lock_release(&rcu_callback_map);
2368
Paul E. McKenney15fecf82017-02-08 12:36:42 -08002369 /*
2370 * Stop only if limit reached and CPU has something to do.
2371 * Note: The rcl structure counts down from zero.
2372 */
Paul E. McKenneyec5ef872019-05-21 13:03:49 -07002373 if (-rcl.len >= bl && !offloaded &&
Paul E. McKenneydff16722011-11-29 15:57:13 -08002374 (need_resched() ||
2375 (!is_idle_task(current) && !rcu_is_callbacks_kthread())))
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002376 break;
Eric Dumazetcfcdef52019-07-24 18:07:52 -07002377 if (unlikely(tlimit)) {
2378 /* only call local_clock() every 32 callbacks */
2379 if (likely((-rcl.len & 31) || local_clock() < tlimit))
2380 continue;
2381 /* Exceeded the time limit, so leave. */
2382 break;
2383 }
Paul E. McKenneyec5ef872019-05-21 13:03:49 -07002384 if (offloaded) {
Paul E. McKenney5d6742b2019-05-15 09:56:40 -07002385 WARN_ON_ONCE(in_serving_softirq());
2386 local_bh_enable();
2387 lockdep_assert_irqs_enabled();
2388 cond_resched_tasks_rcu_qs();
2389 lockdep_assert_irqs_enabled();
2390 local_bh_disable();
2391 }
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002392 }
2393
2394 local_irq_save(flags);
Paul E. McKenney5d6742b2019-05-15 09:56:40 -07002395 rcu_nocb_lock(rdp);
Paul E. McKenney4b27f202017-05-02 08:45:25 -07002396 count = -rcl.len;
Paul E. McKenney3c779df2018-07-05 15:54:02 -07002397 trace_rcu_batch_end(rcu_state.name, count, !!rcl.head, need_resched(),
Paul E. McKenney8ef0f372017-05-02 08:18:40 -07002398 is_idle_task(current), rcu_is_callbacks_kthread());
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002399
Paul E. McKenney15fecf82017-02-08 12:36:42 -08002400 /* Update counts and requeue any remaining callbacks. */
2401 rcu_segcblist_insert_done_cbs(&rdp->cblist, &rcl);
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08002402 smp_mb(); /* List handling before counting for rcu_barrier(). */
Paul E. McKenney15fecf82017-02-08 12:36:42 -08002403 rcu_segcblist_insert_count(&rdp->cblist, &rcl);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002404
2405 /* Reinstate batch limit if we have worked down the excess. */
Paul E. McKenney15fecf82017-02-08 12:36:42 -08002406 count = rcu_segcblist_n_cbs(&rdp->cblist);
Paul E. McKenneyd5a9a8c2019-04-10 17:01:39 -07002407 if (rdp->blimit >= DEFAULT_MAX_RCU_BLIMIT && count <= qlowmark)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002408 rdp->blimit = blimit;
2409
Paul E. McKenney37c72e52009-10-14 10:15:55 -07002410 /* Reset ->qlen_last_fqs_check trigger if enough CBs have drained. */
Paul E. McKenney15fecf82017-02-08 12:36:42 -08002411 if (count == 0 && rdp->qlen_last_fqs_check != 0) {
Paul E. McKenney37c72e52009-10-14 10:15:55 -07002412 rdp->qlen_last_fqs_check = 0;
Paul E. McKenney3c779df2018-07-05 15:54:02 -07002413 rdp->n_force_qs_snap = rcu_state.n_force_qs;
Paul E. McKenney15fecf82017-02-08 12:36:42 -08002414 } else if (count < rdp->qlen_last_fqs_check - qhimark)
2415 rdp->qlen_last_fqs_check = count;
Paul E. McKenneyefd88b02017-10-19 14:52:41 -07002416
2417 /*
2418 * The following usually indicates a double call_rcu(). To track
2419 * this down, try building with CONFIG_DEBUG_OBJECTS_RCU_HEAD=y.
2420 */
Paul E. McKenneyd1b222c2019-07-02 16:03:33 -07002421 WARN_ON_ONCE(count == 0 && !rcu_segcblist_empty(&rdp->cblist));
2422 WARN_ON_ONCE(!IS_ENABLED(CONFIG_RCU_NOCB_CPU) &&
2423 count != 0 && rcu_segcblist_empty(&rdp->cblist));
Paul E. McKenney37c72e52009-10-14 10:15:55 -07002424
Paul E. McKenney5d6742b2019-05-15 09:56:40 -07002425 rcu_nocb_unlock_irqrestore(rdp, flags);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002426
Paul E. McKenneye0f23062011-06-21 01:29:39 -07002427 /* Re-invoke RCU core processing if there are callbacks remaining. */
Paul E. McKenneyec5ef872019-05-21 13:03:49 -07002428 if (!offloaded && rcu_segcblist_ready_cbs(&rdp->cblist))
Paul E. McKenneya46e0892011-06-15 15:47:09 -07002429 invoke_rcu_core();
Paul E. McKenney6a949b72019-07-28 11:50:56 -07002430 tick_dep_clear_task(current, TICK_DEP_BIT_RCU);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002431}
2432
2433/*
Paul E. McKenneyc98cac62018-11-21 11:35:03 -08002434 * This function is invoked from each scheduling-clock interrupt,
2435 * and checks to see if this CPU is in a non-context-switch quiescent
2436 * state, for example, user mode or idle loop. It also schedules RCU
2437 * core processing. If the current grace period has gone on too long,
2438 * it will ask the scheduler to manufacture a context switch for the sole
2439 * purpose of providing a providing the needed quiescent state.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002440 */
Paul E. McKenneyc98cac62018-11-21 11:35:03 -08002441void rcu_sched_clock_irq(int user)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002442{
Steven Rostedt (Red Hat)f7f7bac2013-07-12 17:18:47 -04002443 trace_rcu_utilization(TPS("Start scheduler-tick"));
Paul E. McKenney4e950202018-07-05 17:59:36 -07002444 raw_cpu_inc(rcu_data.ticks_this_gp);
Paul E. McKenney92aa39e2018-07-09 13:47:30 -07002445 /* The load-acquire pairs with the store-release setting to true. */
Paul E. McKenney2dba13f2018-08-03 21:00:38 -07002446 if (smp_load_acquire(this_cpu_ptr(&rcu_data.rcu_urgent_qs))) {
Paul E. McKenney92aa39e2018-07-09 13:47:30 -07002447 /* Idle and userspace execution already are quiescent states. */
Paul E. McKenneya0ef9ec2018-07-09 15:50:16 -07002448 if (!rcu_is_cpu_rrupt_from_idle() && !user) {
Paul E. McKenney92aa39e2018-07-09 13:47:30 -07002449 set_tsk_need_resched(current);
2450 set_preempt_need_resched();
2451 }
Paul E. McKenney2dba13f2018-08-03 21:00:38 -07002452 __this_cpu_write(rcu_data.rcu_urgent_qs, false);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002453 }
Paul E. McKenneyc98cac62018-11-21 11:35:03 -08002454 rcu_flavor_sched_clock_irq(user);
Paul E. McKenneydd7dafd2019-09-14 03:39:22 -07002455 if (rcu_pending(user))
Paul E. McKenneya46e0892011-06-15 15:47:09 -07002456 invoke_rcu_core();
Byungchul Park07f27572018-05-11 17:30:34 +09002457
Steven Rostedt (Red Hat)f7f7bac2013-07-12 17:18:47 -04002458 trace_rcu_utilization(TPS("End scheduler-tick"));
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002459}
2460
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002461/*
Zhouyi Zhou5d8a7522019-03-20 03:33:00 +00002462 * Scan the leaf rcu_node structures. For each structure on which all
2463 * CPUs have reported a quiescent state and on which there are tasks
2464 * blocking the current grace period, initiate RCU priority boosting.
2465 * Otherwise, invoke the specified function to check dyntick state for
2466 * each CPU that has not yet reported a quiescent state.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002467 */
Paul E. McKenney8ff0b902018-07-05 17:55:14 -07002468static void force_qs_rnp(int (*f)(struct rcu_data *rdp))
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002469{
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002470 int cpu;
2471 unsigned long flags;
2472 unsigned long mask;
Paul E. McKenney66e4c332019-08-12 16:14:00 -07002473 struct rcu_data *rdp;
Paul E. McKenneya0b6c9a2009-09-28 07:46:33 -07002474 struct rcu_node *rnp;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002475
Paul E. McKenneyb2b00dd2019-10-30 11:56:10 -07002476 rcu_state.cbovld = rcu_state.cbovldnext;
2477 rcu_state.cbovldnext = false;
Paul E. McKenneyaedf4ba2018-07-04 14:33:59 -07002478 rcu_for_each_leaf_node(rnp) {
Paul E. McKenneycee43932018-03-02 16:35:27 -08002479 cond_resched_tasks_rcu_qs();
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002480 mask = 0;
Peter Zijlstra2a67e742015-10-08 12:24:23 +02002481 raw_spin_lock_irqsave_rcu_node(rnp, flags);
Paul E. McKenneyb2b00dd2019-10-30 11:56:10 -07002482 rcu_state.cbovldnext |= !!rnp->cbovldmask;
Paul E. McKenneya0b6c9a2009-09-28 07:46:33 -07002483 if (rnp->qsmask == 0) {
Lai Jiangshanc130d2d2019-10-15 10:28:48 +00002484 if (!IS_ENABLED(CONFIG_PREEMPT_RCU) ||
Paul E. McKenneya77da142015-03-08 14:52:27 -07002485 rcu_preempt_blocked_readers_cgp(rnp)) {
2486 /*
2487 * No point in scanning bits because they
2488 * are all zero. But we might need to
2489 * priority-boost blocked readers.
2490 */
2491 rcu_initiate_boost(rnp, flags);
2492 /* rcu_initiate_boost() releases rnp->lock */
2493 continue;
2494 }
Paul E. McKenney92816432018-05-02 11:07:02 -07002495 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
2496 continue;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002497 }
Paul E. McKenney7441e762019-10-30 09:37:11 -07002498 for_each_leaf_node_cpu_mask(rnp, cpu, rnp->qsmask) {
2499 rdp = per_cpu_ptr(&rcu_data, cpu);
2500 if (f(rdp)) {
2501 mask |= rdp->grpmask;
2502 rcu_disable_urgency_upon_qs(rdp);
Paul E. McKenney0edd1b12013-06-21 16:37:22 -07002503 }
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002504 }
Paul E. McKenney45f014c52e2010-01-04 15:09:08 -08002505 if (mask != 0) {
Paul E. McKenneyc9a24e22018-04-27 14:54:46 -07002506 /* Idle/offline CPUs, report (releases rnp->lock). */
Paul E. McKenneyb50912d2018-07-03 17:22:34 -07002507 rcu_report_qs_rnp(mask, rnp, rnp->gp_seq, flags);
Paul E. McKenney0aa04b02015-01-23 21:52:37 -08002508 } else {
2509 /* Nothing to do here, so just drop the lock. */
Boqun Feng67c583a72015-12-29 12:18:47 +08002510 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002511 }
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002512 }
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002513}
2514
2515/*
2516 * Force quiescent states on reluctant CPUs, and also detect which
2517 * CPUs are in dyntick-idle mode.
2518 */
Paul E. McKenneycd920e52018-11-28 16:57:54 -08002519void rcu_force_quiescent_state(void)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002520{
2521 unsigned long flags;
Paul E. McKenney394f2762012-06-26 17:00:35 -07002522 bool ret;
2523 struct rcu_node *rnp;
2524 struct rcu_node *rnp_old = NULL;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002525
Paul E. McKenney394f2762012-06-26 17:00:35 -07002526 /* Funnel through hierarchy to reduce memory contention. */
Paul E. McKenneyda1df502018-07-03 15:37:16 -07002527 rnp = __this_cpu_read(rcu_data.mynode);
Paul E. McKenney394f2762012-06-26 17:00:35 -07002528 for (; rnp != NULL; rnp = rnp->parent) {
Paul E. McKenney67a0edb2018-07-05 16:15:38 -07002529 ret = (READ_ONCE(rcu_state.gp_flags) & RCU_GP_FLAG_FQS) ||
Paul E. McKenney66e4c332019-08-12 16:14:00 -07002530 !raw_spin_trylock(&rnp->fqslock);
Paul E. McKenney394f2762012-06-26 17:00:35 -07002531 if (rnp_old != NULL)
2532 raw_spin_unlock(&rnp_old->fqslock);
Paul E. McKenneyd62df572018-01-10 13:10:49 -08002533 if (ret)
Paul E. McKenney394f2762012-06-26 17:00:35 -07002534 return;
Paul E. McKenney394f2762012-06-26 17:00:35 -07002535 rnp_old = rnp;
2536 }
Paul E. McKenney336a4f62018-07-03 17:22:34 -07002537 /* rnp_old == rcu_get_root(), rnp == NULL. */
Paul E. McKenney394f2762012-06-26 17:00:35 -07002538
2539 /* Reached the root of the rcu_node tree, acquire lock. */
Peter Zijlstra2a67e742015-10-08 12:24:23 +02002540 raw_spin_lock_irqsave_rcu_node(rnp_old, flags);
Paul E. McKenney394f2762012-06-26 17:00:35 -07002541 raw_spin_unlock(&rnp_old->fqslock);
Paul E. McKenney67a0edb2018-07-05 16:15:38 -07002542 if (READ_ONCE(rcu_state.gp_flags) & RCU_GP_FLAG_FQS) {
Boqun Feng67c583a72015-12-29 12:18:47 +08002543 raw_spin_unlock_irqrestore_rcu_node(rnp_old, flags);
Paul E. McKenney4cdfc1752012-06-22 17:06:26 -07002544 return; /* Someone beat us to it. */
Paul E. McKenney46a1e342010-01-04 15:09:09 -08002545 }
Paul E. McKenney67a0edb2018-07-05 16:15:38 -07002546 WRITE_ONCE(rcu_state.gp_flags,
2547 READ_ONCE(rcu_state.gp_flags) | RCU_GP_FLAG_FQS);
Boqun Feng67c583a72015-12-29 12:18:47 +08002548 raw_spin_unlock_irqrestore_rcu_node(rnp_old, flags);
Paul E. McKenney532c00c2018-07-03 17:22:34 -07002549 rcu_gp_kthread_wake();
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002550}
Paul E. McKenneycd920e52018-11-28 16:57:54 -08002551EXPORT_SYMBOL_GPL(rcu_force_quiescent_state);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002552
Paul E. McKenneyfb60e532018-11-21 12:42:12 -08002553/* Perform RCU core processing work for the current CPU. */
Sebastian Andrzej Siewior48d07c02019-03-20 22:13:33 +01002554static __latent_entropy void rcu_core(void)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002555{
Shaohua Li09223372011-06-14 13:26:25 +08002556 unsigned long flags;
Paul E. McKenneyda1df502018-07-03 15:37:16 -07002557 struct rcu_data *rdp = raw_cpu_ptr(&rcu_data);
Paul E. McKenneyb0f74032013-02-04 12:14:24 -08002558 struct rcu_node *rnp = rdp->mynode;
Paul E. McKenneyc1ab99d2019-05-21 13:39:15 -07002559 const bool offloaded = IS_ENABLED(CONFIG_RCU_NOCB_CPU) &&
2560 rcu_segcblist_is_offloaded(&rdp->cblist);
Paul E. McKenneya26ac242011-01-12 14:10:23 -08002561
2562 if (cpu_is_offline(smp_processor_id()))
Peter Zijlstra08bca602011-05-20 16:06:29 -07002563 return;
Paul E. McKenneya26ac242011-01-12 14:10:23 -08002564 trace_rcu_utilization(TPS("Start RCU core"));
Shaohua Li09223372011-06-14 13:26:25 +08002565 WARN_ON_ONCE(!rdp->beenonline);
Paul E. McKenney29154c52012-05-30 03:21:48 -07002566
Paul E. McKenney3e310092018-06-21 12:50:01 -07002567 /* Report any deferred quiescent states if preemption enabled. */
Paul E. McKenneyfced9c82018-07-26 13:44:00 -07002568 if (!(preempt_count() & PREEMPT_MASK)) {
Paul E. McKenney3e310092018-06-21 12:50:01 -07002569 rcu_preempt_deferred_qs(current);
Paul E. McKenneyfced9c82018-07-26 13:44:00 -07002570 } else if (rcu_preempt_need_deferred_qs(current)) {
2571 set_tsk_need_resched(current);
2572 set_preempt_need_resched();
2573 }
Paul E. McKenney3e310092018-06-21 12:50:01 -07002574
Paul E. McKenney29154c52012-05-30 03:21:48 -07002575 /* Update RCU state based on any recent quiescent states. */
Paul E. McKenney8087d3e2018-07-03 17:22:34 -07002576 rcu_check_quiescent_state(rdp);
Paul E. McKenney29154c52012-05-30 03:21:48 -07002577
2578 /* No grace period and unregistered callbacks? */
Paul E. McKenneyde8e8732018-07-03 17:22:34 -07002579 if (!rcu_gp_in_progress() &&
Paul E. McKenneyc1ab99d2019-05-21 13:39:15 -07002580 rcu_segcblist_is_enabled(&rdp->cblist) && !offloaded) {
Paul E. McKenney48a76392014-03-11 13:02:16 -07002581 local_irq_save(flags);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002582 if (!rcu_segcblist_restempty(&rdp->cblist, RCU_NEXT_READY_TAIL))
Paul E. McKenneyc6e09b92018-07-03 17:22:34 -07002583 rcu_accelerate_cbs_unlocked(rnp, rdp);
Paul E. McKenney29154c52012-05-30 03:21:48 -07002584 local_irq_restore(flags);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002585 }
Yao Dongdong9910aff2015-02-25 17:09:46 +08002586
Paul E. McKenney791416c2018-10-01 15:42:44 -07002587 rcu_check_gp_start_stall(rnp, rdp, rcu_jiffies_till_stall_check());
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002588
Paul E. McKenney29154c52012-05-30 03:21:48 -07002589 /* If there are callbacks ready, invoke them. */
Paul E. McKenneyc1ab99d2019-05-21 13:39:15 -07002590 if (!offloaded && rcu_segcblist_ready_cbs(&rdp->cblist) &&
Paul E. McKenney43e903a2019-03-25 08:36:03 -07002591 likely(READ_ONCE(rcu_scheduler_fully_active)))
2592 rcu_do_batch(rdp);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002593
2594 /* Do any needed deferred wakeups of rcuo kthreads. */
2595 do_nocb_deferred_wakeup(rdp);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002596 trace_rcu_utilization(TPS("End RCU core"));
2597}
2598
Sebastian Andrzej Siewior48d07c02019-03-20 22:13:33 +01002599static void rcu_core_si(struct softirq_action *h)
2600{
2601 rcu_core();
2602}
2603
2604static void rcu_wake_cond(struct task_struct *t, int status)
2605{
2606 /*
2607 * If the thread is yielding, only wake it when this
2608 * is invoked from idle
2609 */
2610 if (t && (status != RCU_KTHREAD_YIELDING || is_idle_task(current)))
2611 wake_up_process(t);
2612}
2613
2614static void invoke_rcu_core_kthread(void)
2615{
2616 struct task_struct *t;
2617 unsigned long flags;
2618
2619 local_irq_save(flags);
2620 __this_cpu_write(rcu_data.rcu_cpu_has_work, 1);
2621 t = __this_cpu_read(rcu_data.rcu_cpu_kthread_task);
2622 if (t != NULL && t != current)
2623 rcu_wake_cond(t, __this_cpu_read(rcu_data.rcu_cpu_kthread_status));
2624 local_irq_restore(flags);
2625}
2626
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002627/*
Sebastian Andrzej Siewior48d07c02019-03-20 22:13:33 +01002628 * Wake up this CPU's rcuc kthread to do RCU core processing.
2629 */
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002630static void invoke_rcu_core(void)
2631{
Sebastian Andrzej Siewior48d07c02019-03-20 22:13:33 +01002632 if (!cpu_online(smp_processor_id()))
2633 return;
2634 if (use_softirq)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002635 raise_softirq(RCU_SOFTIRQ);
Sebastian Andrzej Siewior48d07c02019-03-20 22:13:33 +01002636 else
2637 invoke_rcu_core_kthread();
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002638}
2639
Sebastian Andrzej Siewior48d07c02019-03-20 22:13:33 +01002640static void rcu_cpu_kthread_park(unsigned int cpu)
2641{
2642 per_cpu(rcu_data.rcu_cpu_kthread_status, cpu) = RCU_KTHREAD_OFFCPU;
2643}
2644
2645static int rcu_cpu_kthread_should_run(unsigned int cpu)
2646{
2647 return __this_cpu_read(rcu_data.rcu_cpu_has_work);
2648}
2649
2650/*
2651 * Per-CPU kernel thread that invokes RCU callbacks. This replaces
2652 * the RCU softirq used in configurations of RCU that do not support RCU
2653 * priority boosting.
2654 */
2655static void rcu_cpu_kthread(unsigned int cpu)
2656{
2657 unsigned int *statusp = this_cpu_ptr(&rcu_data.rcu_cpu_kthread_status);
2658 char work, *workp = this_cpu_ptr(&rcu_data.rcu_cpu_has_work);
2659 int spincnt;
2660
Lai Jiangshan2488a5e2019-10-15 10:23:57 +00002661 trace_rcu_utilization(TPS("Start CPU kthread@rcu_run"));
Sebastian Andrzej Siewior48d07c02019-03-20 22:13:33 +01002662 for (spincnt = 0; spincnt < 10; spincnt++) {
Sebastian Andrzej Siewior48d07c02019-03-20 22:13:33 +01002663 local_bh_disable();
2664 *statusp = RCU_KTHREAD_RUNNING;
2665 local_irq_disable();
2666 work = *workp;
2667 *workp = 0;
2668 local_irq_enable();
2669 if (work)
2670 rcu_core();
2671 local_bh_enable();
2672 if (*workp == 0) {
2673 trace_rcu_utilization(TPS("End CPU kthread@rcu_wait"));
2674 *statusp = RCU_KTHREAD_WAITING;
2675 return;
2676 }
2677 }
2678 *statusp = RCU_KTHREAD_YIELDING;
2679 trace_rcu_utilization(TPS("Start CPU kthread@rcu_yield"));
2680 schedule_timeout_interruptible(2);
2681 trace_rcu_utilization(TPS("End CPU kthread@rcu_yield"));
2682 *statusp = RCU_KTHREAD_WAITING;
2683}
2684
2685static struct smp_hotplug_thread rcu_cpu_thread_spec = {
2686 .store = &rcu_data.rcu_cpu_kthread_task,
2687 .thread_should_run = rcu_cpu_kthread_should_run,
2688 .thread_fn = rcu_cpu_kthread,
2689 .thread_comm = "rcuc/%u",
2690 .setup = rcu_cpu_kthread_setup,
2691 .park = rcu_cpu_kthread_park,
2692};
2693
2694/*
2695 * Spawn per-CPU RCU core processing kthreads.
2696 */
2697static int __init rcu_spawn_core_kthreads(void)
2698{
2699 int cpu;
2700
2701 for_each_possible_cpu(cpu)
2702 per_cpu(rcu_data.rcu_cpu_has_work, cpu) = 0;
2703 if (!IS_ENABLED(CONFIG_RCU_BOOST) && use_softirq)
2704 return 0;
2705 WARN_ONCE(smpboot_register_percpu_thread(&rcu_cpu_thread_spec),
2706 "%s: Could not start rcuc kthread, OOM is now expected behavior\n", __func__);
2707 return 0;
2708}
2709early_initcall(rcu_spawn_core_kthreads);
2710
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002711/*
2712 * Handle any core-RCU processing required by a call_rcu() invocation.
Paul E. McKenney29154c52012-05-30 03:21:48 -07002713 */
Paul E. McKenney5c7d8962018-07-03 17:22:34 -07002714static void __call_rcu_core(struct rcu_data *rdp, struct rcu_head *head,
2715 unsigned long flags)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002716{
Paul E. McKenney37c72e52009-10-14 10:15:55 -07002717 /*
2718 * If called from an extended quiescent state, invoke the RCU
2719 * core in order to force a re-evaluation of RCU's idleness.
2720 */
2721 if (!rcu_is_watching())
Paul E. McKenney2655d572011-04-07 22:47:23 -07002722 invoke_rcu_core();
Paul E. McKenneyb52573d2010-12-14 17:36:02 -08002723
2724 /* If interrupts were disabled or CPU offline, don't invoke RCU core. */
Paul E. McKenney470716f2013-03-19 11:32:11 -07002725 if (irqs_disabled_flags(flags) || cpu_is_offline(smp_processor_id()))
Paul E. McKenneyb52573d2010-12-14 17:36:02 -08002726 return;
2727
2728 /*
Paul E. McKenneyb52573d2010-12-14 17:36:02 -08002729 * Force the grace period if too many callbacks or too long waiting.
Paul E. McKenneycd920e52018-11-28 16:57:54 -08002730 * Enforce hysteresis, and don't invoke rcu_force_quiescent_state()
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002731 * if some other CPU has recently done so. Also, don't bother
Paul E. McKenneycd920e52018-11-28 16:57:54 -08002732 * invoking rcu_force_quiescent_state() if the newly enqueued callback
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002733 * is the only one waiting for a grace period to complete.
2734 */
Paul E. McKenney15fecf82017-02-08 12:36:42 -08002735 if (unlikely(rcu_segcblist_n_cbs(&rdp->cblist) >
2736 rdp->qlen_last_fqs_check + qhimark)) {
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002737
2738 /* Are we ignoring a completed grace period? */
Paul E. McKenney15cabdf2018-07-03 17:22:34 -07002739 note_gp_changes(rdp);
Paul E. McKenneyb52573d2010-12-14 17:36:02 -08002740
2741 /* Start a new grace period if one not already started. */
Paul E. McKenneyde8e8732018-07-03 17:22:34 -07002742 if (!rcu_gp_in_progress()) {
Paul E. McKenneyc6e09b92018-07-03 17:22:34 -07002743 rcu_accelerate_cbs_unlocked(rdp->mynode, rdp);
Paul E. McKenneyb52573d2010-12-14 17:36:02 -08002744 } else {
2745 /* Give the grace period a kick. */
Paul E. McKenneyd5a9a8c2019-04-10 17:01:39 -07002746 rdp->blimit = DEFAULT_MAX_RCU_BLIMIT;
Paul E. McKenney5c7d8962018-07-03 17:22:34 -07002747 if (rcu_state.n_force_qs == rdp->n_force_qs_snap &&
Paul E. McKenney15fecf82017-02-08 12:36:42 -08002748 rcu_segcblist_first_pend_cb(&rdp->cblist) != head)
Paul E. McKenneycd920e52018-11-28 16:57:54 -08002749 rcu_force_quiescent_state();
Paul E. McKenney5c7d8962018-07-03 17:22:34 -07002750 rdp->n_force_qs_snap = rcu_state.n_force_qs;
Paul E. McKenney15fecf82017-02-08 12:36:42 -08002751 rdp->qlen_last_fqs_check = rcu_segcblist_n_cbs(&rdp->cblist);
Paul E. McKenneyb52573d2010-12-14 17:36:02 -08002752 }
Paul E. McKenney4cdfc1752012-06-22 17:06:26 -07002753 }
Paul E. McKenney29154c52012-05-30 03:21:48 -07002754}
2755
Paul E. McKenney3fbfbf72012-08-19 21:35:53 -07002756/*
Paul E. McKenneyae150182013-04-23 13:20:57 -07002757 * RCU callback function to leak a callback.
2758 */
2759static void rcu_leak_callback(struct rcu_head *rhp)
2760{
2761}
2762
2763/*
Paul E. McKenneyb2b00dd2019-10-30 11:56:10 -07002764 * Check and if necessary update the leaf rcu_node structure's
2765 * ->cbovldmask bit corresponding to the current CPU based on that CPU's
2766 * number of queued RCU callbacks. The caller must hold the leaf rcu_node
2767 * structure's ->lock.
Paul E. McKenney3fbfbf72012-08-19 21:35:53 -07002768 */
Paul E. McKenneyb2b00dd2019-10-30 11:56:10 -07002769static void check_cb_ovld_locked(struct rcu_data *rdp, struct rcu_node *rnp)
2770{
2771 raw_lockdep_assert_held_rcu_node(rnp);
2772 if (qovld_calc <= 0)
2773 return; // Early boot and wildcard value set.
2774 if (rcu_segcblist_n_cbs(&rdp->cblist) >= qovld_calc)
2775 WRITE_ONCE(rnp->cbovldmask, rnp->cbovldmask | rdp->grpmask);
2776 else
2777 WRITE_ONCE(rnp->cbovldmask, rnp->cbovldmask & ~rdp->grpmask);
2778}
2779
2780/*
2781 * Check and if necessary update the leaf rcu_node structure's
2782 * ->cbovldmask bit corresponding to the current CPU based on that CPU's
2783 * number of queued RCU callbacks. No locks need be held, but the
2784 * caller must have disabled interrupts.
2785 *
2786 * Note that this function ignores the possibility that there are a lot
2787 * of callbacks all of which have already seen the end of their respective
2788 * grace periods. This omission is due to the need for no-CBs CPUs to
2789 * be holding ->nocb_lock to do this check, which is too heavy for a
2790 * common-case operation.
2791 */
2792static void check_cb_ovld(struct rcu_data *rdp)
2793{
2794 struct rcu_node *const rnp = rdp->mynode;
2795
2796 if (qovld_calc <= 0 ||
2797 ((rcu_segcblist_n_cbs(&rdp->cblist) >= qovld_calc) ==
2798 !!(READ_ONCE(rnp->cbovldmask) & rdp->grpmask)))
2799 return; // Early boot wildcard value or already set correctly.
2800 raw_spin_lock_rcu_node(rnp);
2801 check_cb_ovld_locked(rdp, rnp);
2802 raw_spin_unlock_rcu_node(rnp);
2803}
2804
Paul E. McKenneyb692dc42020-02-11 07:29:02 -08002805/* Helper function for call_rcu() and friends. */
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002806static void
Joel Fernandes (Google)77a40f92019-08-30 12:36:32 -04002807__call_rcu(struct rcu_head *head, rcu_callback_t func)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002808{
2809 unsigned long flags;
2810 struct rcu_data *rdp;
Paul E. McKenney5d6742b2019-05-15 09:56:40 -07002811 bool was_alldone;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002812
Paul E. McKenneyb8f2ed52016-08-23 06:51:47 -07002813 /* Misaligned rcu_head! */
2814 WARN_ON_ONCE((unsigned long)head & (sizeof(void *) - 1));
2815
Paul E. McKenneyae150182013-04-23 13:20:57 -07002816 if (debug_rcu_head_queue(head)) {
Paul E. McKenneyfa3c6642017-05-03 11:38:55 -07002817 /*
2818 * Probable double call_rcu(), so leak the callback.
2819 * Use rcu:rcu_callback trace event to find the previous
2820 * time callback was passed to __call_rcu().
2821 */
Sakari Ailusd75f7732019-03-25 21:32:28 +02002822 WARN_ONCE(1, "__call_rcu(): Double-freed CB %p->%pS()!!!\n",
Paul E. McKenneyfa3c6642017-05-03 11:38:55 -07002823 head, head->func);
Paul E. McKenney7d0ae802015-03-03 14:57:58 -08002824 WRITE_ONCE(head->func, rcu_leak_callback);
Paul E. McKenneyae150182013-04-23 13:20:57 -07002825 return;
2826 }
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002827 head->func = func;
2828 head->next = NULL;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002829 local_irq_save(flags);
Paul E. McKenneyda1df502018-07-03 15:37:16 -07002830 rdp = this_cpu_ptr(&rcu_data);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002831
2832 /* Add the callback to our list. */
Paul E. McKenney5d6742b2019-05-15 09:56:40 -07002833 if (unlikely(!rcu_segcblist_is_enabled(&rdp->cblist))) {
2834 // This can trigger due to call_rcu() from offline CPU:
2835 WARN_ON_ONCE(rcu_scheduler_active != RCU_SCHEDULER_INACTIVE);
Paul E. McKenney34404ca2015-01-19 20:39:20 -08002836 WARN_ON_ONCE(!rcu_is_watching());
Paul E. McKenney5d6742b2019-05-15 09:56:40 -07002837 // Very early boot, before rcu_init(). Initialize if needed
2838 // and then drop through to queue the callback.
Paul E. McKenney15fecf82017-02-08 12:36:42 -08002839 if (rcu_segcblist_empty(&rdp->cblist))
2840 rcu_segcblist_init(&rdp->cblist);
Paul E. McKenney0d8ee372012-08-03 13:16:15 -07002841 }
Joel Fernandes (Google)77a40f92019-08-30 12:36:32 -04002842
Paul E. McKenneyb2b00dd2019-10-30 11:56:10 -07002843 check_cb_ovld(rdp);
Paul E. McKenneyd1b222c2019-07-02 16:03:33 -07002844 if (rcu_nocb_try_bypass(rdp, head, &was_alldone, flags))
2845 return; // Enqueued onto ->nocb_bypass, so just leave.
Paul E. McKenneyb692dc42020-02-11 07:29:02 -08002846 // If no-CBs CPU gets here, rcu_nocb_try_bypass() acquired ->nocb_lock.
Joel Fernandes (Google)77a40f92019-08-30 12:36:32 -04002847 rcu_segcblist_enqueue(&rdp->cblist, head);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002848 if (__is_kfree_rcu_offset((unsigned long)func))
Paul E. McKenney3c779df2018-07-05 15:54:02 -07002849 trace_rcu_kfree_callback(rcu_state.name, head,
2850 (unsigned long)func,
Paul E. McKenney15fecf82017-02-08 12:36:42 -08002851 rcu_segcblist_n_cbs(&rdp->cblist));
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002852 else
Paul E. McKenney3c779df2018-07-05 15:54:02 -07002853 trace_rcu_callback(rcu_state.name, head,
Paul E. McKenney15fecf82017-02-08 12:36:42 -08002854 rcu_segcblist_n_cbs(&rdp->cblist));
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002855
Paul E. McKenney29154c52012-05-30 03:21:48 -07002856 /* Go handle any RCU core processing required. */
Paul E. McKenney5d6742b2019-05-15 09:56:40 -07002857 if (IS_ENABLED(CONFIG_RCU_NOCB_CPU) &&
2858 unlikely(rcu_segcblist_is_offloaded(&rdp->cblist))) {
2859 __call_rcu_nocb_wake(rdp, was_alldone, flags); /* unlocks */
2860 } else {
2861 __call_rcu_core(rdp, head, flags);
2862 local_irq_restore(flags);
2863 }
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002864}
2865
Paul E. McKenneya68a2bb2017-05-03 08:34:57 -07002866/**
Paul E. McKenney45975c72018-07-02 14:30:37 -07002867 * call_rcu() - Queue an RCU callback for invocation after a grace period.
Paul E. McKenneya68a2bb2017-05-03 08:34:57 -07002868 * @head: structure to be used for queueing the RCU updates.
2869 * @func: actual callback function to be invoked after the grace period
2870 *
2871 * The callback function will be invoked some time after a full grace
Paul E. McKenney45975c72018-07-02 14:30:37 -07002872 * period elapses, in other words after all pre-existing RCU read-side
2873 * critical sections have completed. However, the callback function
2874 * might well execute concurrently with RCU read-side critical sections
2875 * that started after call_rcu() was invoked. RCU read-side critical
2876 * sections are delimited by rcu_read_lock() and rcu_read_unlock(), and
2877 * may be nested. In addition, regions of code across which interrupts,
2878 * preemption, or softirqs have been disabled also serve as RCU read-side
2879 * critical sections. This includes hardware interrupt handlers, softirq
2880 * handlers, and NMI handlers.
Paul E. McKenney27fdb352017-10-19 14:26:21 -07002881 *
Paul E. McKenney45975c72018-07-02 14:30:37 -07002882 * Note that all CPUs must agree that the grace period extended beyond
2883 * all pre-existing RCU read-side critical section. On systems with more
2884 * than one CPU, this means that when "func()" is invoked, each CPU is
2885 * guaranteed to have executed a full memory barrier since the end of its
2886 * last RCU read-side critical section whose beginning preceded the call
2887 * to call_rcu(). It also means that each CPU executing an RCU read-side
2888 * critical section that continues beyond the start of "func()" must have
2889 * executed a memory barrier after the call_rcu() but before the beginning
2890 * of that RCU read-side critical section. Note that these guarantees
2891 * include CPUs that are offline, idle, or executing in user mode, as
2892 * well as CPUs that are executing in the kernel.
Paul E. McKenneya68a2bb2017-05-03 08:34:57 -07002893 *
Paul E. McKenney45975c72018-07-02 14:30:37 -07002894 * Furthermore, if CPU A invoked call_rcu() and CPU B invoked the
2895 * resulting RCU callback function "func()", then both CPU A and CPU B are
2896 * guaranteed to execute a full memory barrier during the time interval
2897 * between the call to call_rcu() and the invocation of "func()" -- even
2898 * if CPU A and CPU B are the same CPU (but again only if the system has
2899 * more than one CPU).
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002900 */
Paul E. McKenney45975c72018-07-02 14:30:37 -07002901void call_rcu(struct rcu_head *head, rcu_callback_t func)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002902{
Joel Fernandes (Google)77a40f92019-08-30 12:36:32 -04002903 __call_rcu(head, func);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002904}
Paul E. McKenney45975c72018-07-02 14:30:37 -07002905EXPORT_SYMBOL_GPL(call_rcu);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01002906
Byungchul Parka35d1692019-08-05 18:22:27 -04002907
2908/* Maximum number of jiffies to wait before draining a batch. */
2909#define KFREE_DRAIN_JIFFIES (HZ / 50)
Joel Fernandes (Google)0392beb2019-09-19 14:58:26 -07002910#define KFREE_N_BATCHES 2
2911
Uladzislau Rezki (Sony)34c88172020-01-20 15:42:25 +01002912/*
2913 * This macro defines how many entries the "records" array
2914 * will contain. It is based on the fact that the size of
2915 * kfree_rcu_bulk_data structure becomes exactly one page.
2916 */
2917#define KFREE_BULK_MAX_ENTR ((PAGE_SIZE / sizeof(void *)) - 3)
2918
2919/**
2920 * struct kfree_rcu_bulk_data - single block to store kfree_rcu() pointers
2921 * @nr_records: Number of active pointers in the array
2922 * @records: Array of the kfree_rcu() pointers
2923 * @next: Next bulk object in the block chain
2924 * @head_free_debug: For debug, when CONFIG_DEBUG_OBJECTS_RCU_HEAD is set
2925 */
2926struct kfree_rcu_bulk_data {
2927 unsigned long nr_records;
2928 void *records[KFREE_BULK_MAX_ENTR];
2929 struct kfree_rcu_bulk_data *next;
2930 struct rcu_head *head_free_debug;
2931};
2932
Joel Fernandes (Google)0392beb2019-09-19 14:58:26 -07002933/**
2934 * struct kfree_rcu_cpu_work - single batch of kfree_rcu() requests
2935 * @rcu_work: Let queue_rcu_work() invoke workqueue handler after grace period
2936 * @head_free: List of kfree_rcu() objects waiting for a grace period
Uladzislau Rezki (Sony)34c88172020-01-20 15:42:25 +01002937 * @bhead_free: Bulk-List of kfree_rcu() objects waiting for a grace period
Joel Fernandes (Google)0392beb2019-09-19 14:58:26 -07002938 * @krcp: Pointer to @kfree_rcu_cpu structure
2939 */
2940
2941struct kfree_rcu_cpu_work {
2942 struct rcu_work rcu_work;
2943 struct rcu_head *head_free;
Uladzislau Rezki (Sony)34c88172020-01-20 15:42:25 +01002944 struct kfree_rcu_bulk_data *bhead_free;
Joel Fernandes (Google)0392beb2019-09-19 14:58:26 -07002945 struct kfree_rcu_cpu *krcp;
2946};
Byungchul Parka35d1692019-08-05 18:22:27 -04002947
2948/**
2949 * struct kfree_rcu_cpu - batch up kfree_rcu() requests for RCU grace period
Byungchul Parka35d1692019-08-05 18:22:27 -04002950 * @head: List of kfree_rcu() objects not yet waiting for a grace period
Uladzislau Rezki (Sony)34c88172020-01-20 15:42:25 +01002951 * @bhead: Bulk-List of kfree_rcu() objects not yet waiting for a grace period
2952 * @bcached: Keeps at most one object for later reuse when build chain blocks
Joel Fernandes (Google)0392beb2019-09-19 14:58:26 -07002953 * @krw_arr: Array of batches of kfree_rcu() objects waiting for a grace period
Byungchul Parka35d1692019-08-05 18:22:27 -04002954 * @lock: Synchronize access to this structure
2955 * @monitor_work: Promote @head to @head_free after KFREE_DRAIN_JIFFIES
2956 * @monitor_todo: Tracks whether a @monitor_work delayed work is pending
2957 * @initialized: The @lock and @rcu_work fields have been initialized
2958 *
2959 * This is a per-CPU structure. The reason that it is not included in
2960 * the rcu_data structure is to permit this code to be extracted from
2961 * the RCU files. Such extraction could allow further optimization of
2962 * the interactions with the slab allocators.
Andreea-Cristina Bernat495aa962014-03-18 20:48:48 +02002963 */
Byungchul Parka35d1692019-08-05 18:22:27 -04002964struct kfree_rcu_cpu {
Byungchul Parka35d1692019-08-05 18:22:27 -04002965 struct rcu_head *head;
Uladzislau Rezki (Sony)34c88172020-01-20 15:42:25 +01002966 struct kfree_rcu_bulk_data *bhead;
2967 struct kfree_rcu_bulk_data *bcached;
Joel Fernandes (Google)0392beb2019-09-19 14:58:26 -07002968 struct kfree_rcu_cpu_work krw_arr[KFREE_N_BATCHES];
Byungchul Parka35d1692019-08-05 18:22:27 -04002969 spinlock_t lock;
2970 struct delayed_work monitor_work;
Joel Fernandes569d7672019-09-22 10:49:57 -07002971 bool monitor_todo;
Byungchul Parka35d1692019-08-05 18:22:27 -04002972 bool initialized;
Joel Fernandes (Google)91542442020-03-16 12:32:27 -04002973 // Number of objects for which GP not started
2974 int count;
Byungchul Parka35d1692019-08-05 18:22:27 -04002975};
2976
2977static DEFINE_PER_CPU(struct kfree_rcu_cpu, krc);
2978
Uladzislau Rezki (Sony)34c88172020-01-20 15:42:25 +01002979static __always_inline void
2980debug_rcu_head_unqueue_bulk(struct rcu_head *head)
2981{
2982#ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD
2983 for (; head; head = head->next)
2984 debug_rcu_head_unqueue(head);
2985#endif
2986}
2987
Paul E. McKenney6d813392012-02-23 13:30:16 -08002988/*
Byungchul Parka35d1692019-08-05 18:22:27 -04002989 * This function is invoked in workqueue context after a grace period.
Uladzislau Rezki (Sony)34c88172020-01-20 15:42:25 +01002990 * It frees all the objects queued on ->bhead_free or ->head_free.
Byungchul Parka35d1692019-08-05 18:22:27 -04002991 */
2992static void kfree_rcu_work(struct work_struct *work)
2993{
2994 unsigned long flags;
2995 struct rcu_head *head, *next;
Uladzislau Rezki (Sony)34c88172020-01-20 15:42:25 +01002996 struct kfree_rcu_bulk_data *bhead, *bnext;
Byungchul Parka35d1692019-08-05 18:22:27 -04002997 struct kfree_rcu_cpu *krcp;
Joel Fernandes (Google)0392beb2019-09-19 14:58:26 -07002998 struct kfree_rcu_cpu_work *krwp;
Byungchul Parka35d1692019-08-05 18:22:27 -04002999
Joel Fernandes (Google)0392beb2019-09-19 14:58:26 -07003000 krwp = container_of(to_rcu_work(work),
3001 struct kfree_rcu_cpu_work, rcu_work);
3002 krcp = krwp->krcp;
Byungchul Parka35d1692019-08-05 18:22:27 -04003003 spin_lock_irqsave(&krcp->lock, flags);
Joel Fernandes (Google)0392beb2019-09-19 14:58:26 -07003004 head = krwp->head_free;
3005 krwp->head_free = NULL;
Uladzislau Rezki (Sony)34c88172020-01-20 15:42:25 +01003006 bhead = krwp->bhead_free;
3007 krwp->bhead_free = NULL;
Byungchul Parka35d1692019-08-05 18:22:27 -04003008 spin_unlock_irqrestore(&krcp->lock, flags);
3009
Uladzislau Rezki (Sony)34c88172020-01-20 15:42:25 +01003010 /* "bhead" is now private, so traverse locklessly. */
3011 for (; bhead; bhead = bnext) {
3012 bnext = bhead->next;
3013
3014 debug_rcu_head_unqueue_bulk(bhead->head_free_debug);
3015
3016 rcu_lock_acquire(&rcu_callback_map);
Uladzislau Rezki (Sony)61370792020-01-20 15:42:26 +01003017 trace_rcu_invoke_kfree_bulk_callback(rcu_state.name,
3018 bhead->nr_records, bhead->records);
3019
Uladzislau Rezki (Sony)34c88172020-01-20 15:42:25 +01003020 kfree_bulk(bhead->nr_records, bhead->records);
3021 rcu_lock_release(&rcu_callback_map);
3022
3023 if (cmpxchg(&krcp->bcached, NULL, bhead))
3024 free_page((unsigned long) bhead);
3025
3026 cond_resched_tasks_rcu_qs();
3027 }
3028
3029 /*
3030 * Emergency case only. It can happen under low memory
3031 * condition when an allocation gets failed, so the "bulk"
3032 * path can not be temporary maintained.
3033 */
Byungchul Parka35d1692019-08-05 18:22:27 -04003034 for (; head; head = next) {
Joel Fernandes (Google)77a40f92019-08-30 12:36:32 -04003035 unsigned long offset = (unsigned long)head->func;
3036
Byungchul Parka35d1692019-08-05 18:22:27 -04003037 next = head->next;
Joel Fernandes (Google)e99637b2019-09-22 13:03:17 -07003038 debug_rcu_head_unqueue(head);
Joel Fernandes (Google)77a40f92019-08-30 12:36:32 -04003039 rcu_lock_acquire(&rcu_callback_map);
3040 trace_rcu_invoke_kfree_callback(rcu_state.name, head, offset);
3041
Uladzislau Rezki (Sony)34c88172020-01-20 15:42:25 +01003042 if (!WARN_ON_ONCE(!__is_kfree_rcu_offset(offset)))
Joel Fernandes (Google)189a6882019-08-30 12:36:33 -04003043 kfree((void *)head - offset);
Joel Fernandes (Google)77a40f92019-08-30 12:36:32 -04003044
3045 rcu_lock_release(&rcu_callback_map);
Byungchul Parka35d1692019-08-05 18:22:27 -04003046 cond_resched_tasks_rcu_qs();
3047 }
3048}
3049
3050/*
3051 * Schedule the kfree batch RCU work to run in workqueue context after a GP.
3052 *
3053 * This function is invoked by kfree_rcu_monitor() when the KFREE_DRAIN_JIFFIES
3054 * timeout has been reached.
3055 */
3056static inline bool queue_kfree_rcu_work(struct kfree_rcu_cpu *krcp)
3057{
Uladzislau Rezki (Sony)34c88172020-01-20 15:42:25 +01003058 struct kfree_rcu_cpu_work *krwp;
3059 bool queued = false;
Joel Fernandes (Google)0392beb2019-09-19 14:58:26 -07003060 int i;
Joel Fernandes (Google)0392beb2019-09-19 14:58:26 -07003061
Byungchul Parka35d1692019-08-05 18:22:27 -04003062 lockdep_assert_held(&krcp->lock);
Uladzislau Rezki (Sony)34c88172020-01-20 15:42:25 +01003063
3064 for (i = 0; i < KFREE_N_BATCHES; i++) {
3065 krwp = &(krcp->krw_arr[i]);
3066
3067 /*
3068 * Try to detach bhead or head and attach it over any
3069 * available corresponding free channel. It can be that
3070 * a previous RCU batch is in progress, it means that
3071 * immediately to queue another one is not possible so
3072 * return false to tell caller to retry.
3073 */
3074 if ((krcp->bhead && !krwp->bhead_free) ||
3075 (krcp->head && !krwp->head_free)) {
3076 /* Channel 1. */
3077 if (!krwp->bhead_free) {
3078 krwp->bhead_free = krcp->bhead;
3079 krcp->bhead = NULL;
3080 }
3081
3082 /* Channel 2. */
3083 if (!krwp->head_free) {
3084 krwp->head_free = krcp->head;
3085 krcp->head = NULL;
3086 }
3087
Joel Fernandes (Google)a6a82ce2020-03-16 12:32:28 -04003088 WRITE_ONCE(krcp->count, 0);
Joel Fernandes (Google)91542442020-03-16 12:32:27 -04003089
Uladzislau Rezki (Sony)34c88172020-01-20 15:42:25 +01003090 /*
3091 * One work is per one batch, so there are two "free channels",
3092 * "bhead_free" and "head_free" the batch can handle. It can be
3093 * that the work is in the pending state when two channels have
3094 * been detached following each other, one by one.
3095 */
3096 queue_rcu_work(system_wq, &krwp->rcu_work);
3097 queued = true;
Joel Fernandes (Google)0392beb2019-09-19 14:58:26 -07003098 }
Uladzislau Rezki (Sony)34c88172020-01-20 15:42:25 +01003099 }
Byungchul Parka35d1692019-08-05 18:22:27 -04003100
Uladzislau Rezki (Sony)34c88172020-01-20 15:42:25 +01003101 return queued;
Byungchul Parka35d1692019-08-05 18:22:27 -04003102}
3103
3104static inline void kfree_rcu_drain_unlock(struct kfree_rcu_cpu *krcp,
3105 unsigned long flags)
3106{
3107 // Attempt to start a new batch.
Joel Fernandes569d7672019-09-22 10:49:57 -07003108 krcp->monitor_todo = false;
Byungchul Parka35d1692019-08-05 18:22:27 -04003109 if (queue_kfree_rcu_work(krcp)) {
3110 // Success! Our job is done here.
3111 spin_unlock_irqrestore(&krcp->lock, flags);
3112 return;
3113 }
3114
3115 // Previous RCU batch still in progress, try again later.
Joel Fernandes569d7672019-09-22 10:49:57 -07003116 krcp->monitor_todo = true;
3117 schedule_delayed_work(&krcp->monitor_work, KFREE_DRAIN_JIFFIES);
Byungchul Parka35d1692019-08-05 18:22:27 -04003118 spin_unlock_irqrestore(&krcp->lock, flags);
3119}
3120
3121/*
3122 * This function is invoked after the KFREE_DRAIN_JIFFIES timeout.
3123 * It invokes kfree_rcu_drain_unlock() to attempt to start another batch.
3124 */
3125static void kfree_rcu_monitor(struct work_struct *work)
3126{
3127 unsigned long flags;
3128 struct kfree_rcu_cpu *krcp = container_of(work, struct kfree_rcu_cpu,
3129 monitor_work.work);
3130
3131 spin_lock_irqsave(&krcp->lock, flags);
Joel Fernandes569d7672019-09-22 10:49:57 -07003132 if (krcp->monitor_todo)
Byungchul Parka35d1692019-08-05 18:22:27 -04003133 kfree_rcu_drain_unlock(krcp, flags);
3134 else
3135 spin_unlock_irqrestore(&krcp->lock, flags);
3136}
3137
Uladzislau Rezki (Sony)34c88172020-01-20 15:42:25 +01003138static inline bool
3139kfree_call_rcu_add_ptr_to_bulk(struct kfree_rcu_cpu *krcp,
3140 struct rcu_head *head, rcu_callback_t func)
3141{
3142 struct kfree_rcu_bulk_data *bnode;
3143
3144 if (unlikely(!krcp->initialized))
3145 return false;
3146
3147 lockdep_assert_held(&krcp->lock);
3148
3149 /* Check if a new block is required. */
3150 if (!krcp->bhead ||
3151 krcp->bhead->nr_records == KFREE_BULK_MAX_ENTR) {
3152 bnode = xchg(&krcp->bcached, NULL);
3153 if (!bnode) {
3154 WARN_ON_ONCE(sizeof(struct kfree_rcu_bulk_data) > PAGE_SIZE);
3155
3156 bnode = (struct kfree_rcu_bulk_data *)
3157 __get_free_page(GFP_NOWAIT | __GFP_NOWARN);
3158 }
3159
3160 /* Switch to emergency path. */
3161 if (unlikely(!bnode))
3162 return false;
3163
3164 /* Initialize the new block. */
3165 bnode->nr_records = 0;
3166 bnode->next = krcp->bhead;
3167 bnode->head_free_debug = NULL;
3168
3169 /* Attach it to the head. */
3170 krcp->bhead = bnode;
3171 }
3172
3173#ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD
3174 head->func = func;
3175 head->next = krcp->bhead->head_free_debug;
3176 krcp->bhead->head_free_debug = head;
3177#endif
3178
3179 /* Finally insert. */
3180 krcp->bhead->records[krcp->bhead->nr_records++] =
3181 (void *) head - (unsigned long) func;
3182
3183 return true;
3184}
3185
Byungchul Parka35d1692019-08-05 18:22:27 -04003186/*
Uladzislau Rezki (Sony)34c88172020-01-20 15:42:25 +01003187 * Queue a request for lazy invocation of kfree_bulk()/kfree() after a grace
3188 * period. Please note there are two paths are maintained, one is the main one
3189 * that uses kfree_bulk() interface and second one is emergency one, that is
3190 * used only when the main path can not be maintained temporary, due to memory
3191 * pressure.
Byungchul Parka35d1692019-08-05 18:22:27 -04003192 *
3193 * Each kfree_call_rcu() request is added to a batch. The batch will be drained
Uladzislau Rezki (Sony)34c88172020-01-20 15:42:25 +01003194 * every KFREE_DRAIN_JIFFIES number of jiffies. All the objects in the batch will
3195 * be free'd in workqueue context. This allows us to: batch requests together to
3196 * reduce the number of grace periods during heavy kfree_rcu() load.
Andreea-Cristina Bernat495aa962014-03-18 20:48:48 +02003197 */
Paul E. McKenney98ece502018-07-03 17:22:34 -07003198void kfree_call_rcu(struct rcu_head *head, rcu_callback_t func)
Andreea-Cristina Bernat495aa962014-03-18 20:48:48 +02003199{
Byungchul Parka35d1692019-08-05 18:22:27 -04003200 unsigned long flags;
3201 struct kfree_rcu_cpu *krcp;
3202
Byungchul Parka35d1692019-08-05 18:22:27 -04003203 local_irq_save(flags); // For safely calling this_cpu_ptr().
3204 krcp = this_cpu_ptr(&krc);
3205 if (krcp->initialized)
3206 spin_lock(&krcp->lock);
3207
3208 // Queue the object but don't yet schedule the batch.
Joel Fernandes (Google)e99637b2019-09-22 13:03:17 -07003209 if (debug_rcu_head_queue(head)) {
3210 // Probable double kfree_rcu(), just leak.
3211 WARN_ONCE(1, "%s(): Double-freed call. rcu_head %p\n",
3212 __func__, head);
3213 goto unlock_return;
3214 }
Uladzislau Rezki (Sony)34c88172020-01-20 15:42:25 +01003215
3216 /*
3217 * Under high memory pressure GFP_NOWAIT can fail,
3218 * in that case the emergency path is maintained.
3219 */
3220 if (unlikely(!kfree_call_rcu_add_ptr_to_bulk(krcp, head, func))) {
3221 head->func = func;
3222 head->next = krcp->head;
3223 krcp->head = head;
3224 }
Byungchul Parka35d1692019-08-05 18:22:27 -04003225
Joel Fernandes (Google)a6a82ce2020-03-16 12:32:28 -04003226 WRITE_ONCE(krcp->count, krcp->count + 1);
Joel Fernandes (Google)91542442020-03-16 12:32:27 -04003227
Byungchul Parka35d1692019-08-05 18:22:27 -04003228 // Set timer to drain after KFREE_DRAIN_JIFFIES.
3229 if (rcu_scheduler_active == RCU_SCHEDULER_RUNNING &&
Joel Fernandes569d7672019-09-22 10:49:57 -07003230 !krcp->monitor_todo) {
3231 krcp->monitor_todo = true;
Byungchul Parka35d1692019-08-05 18:22:27 -04003232 schedule_delayed_work(&krcp->monitor_work, KFREE_DRAIN_JIFFIES);
Joel Fernandes569d7672019-09-22 10:49:57 -07003233 }
Byungchul Parka35d1692019-08-05 18:22:27 -04003234
Joel Fernandes (Google)e99637b2019-09-22 13:03:17 -07003235unlock_return:
Byungchul Parka35d1692019-08-05 18:22:27 -04003236 if (krcp->initialized)
3237 spin_unlock(&krcp->lock);
3238 local_irq_restore(flags);
Andreea-Cristina Bernat495aa962014-03-18 20:48:48 +02003239}
3240EXPORT_SYMBOL_GPL(kfree_call_rcu);
3241
Joel Fernandes (Google)91542442020-03-16 12:32:27 -04003242static unsigned long
3243kfree_rcu_shrink_count(struct shrinker *shrink, struct shrink_control *sc)
3244{
3245 int cpu;
Joel Fernandes (Google)a6a82ce2020-03-16 12:32:28 -04003246 unsigned long count = 0;
Joel Fernandes (Google)91542442020-03-16 12:32:27 -04003247
3248 /* Snapshot count of all CPUs */
3249 for_each_online_cpu(cpu) {
3250 struct kfree_rcu_cpu *krcp = per_cpu_ptr(&krc, cpu);
3251
Joel Fernandes (Google)a6a82ce2020-03-16 12:32:28 -04003252 count += READ_ONCE(krcp->count);
Joel Fernandes (Google)91542442020-03-16 12:32:27 -04003253 }
3254
3255 return count;
3256}
3257
3258static unsigned long
3259kfree_rcu_shrink_scan(struct shrinker *shrink, struct shrink_control *sc)
3260{
3261 int cpu, freed = 0;
3262 unsigned long flags;
3263
3264 for_each_online_cpu(cpu) {
3265 int count;
3266 struct kfree_rcu_cpu *krcp = per_cpu_ptr(&krc, cpu);
3267
3268 count = krcp->count;
3269 spin_lock_irqsave(&krcp->lock, flags);
3270 if (krcp->monitor_todo)
3271 kfree_rcu_drain_unlock(krcp, flags);
3272 else
3273 spin_unlock_irqrestore(&krcp->lock, flags);
3274
3275 sc->nr_to_scan -= count;
3276 freed += count;
3277
3278 if (sc->nr_to_scan <= 0)
3279 break;
3280 }
3281
3282 return freed;
3283}
3284
3285static struct shrinker kfree_rcu_shrinker = {
3286 .count_objects = kfree_rcu_shrink_count,
3287 .scan_objects = kfree_rcu_shrink_scan,
3288 .batch = 0,
3289 .seeks = DEFAULT_SEEKS,
3290};
3291
Byungchul Parka35d1692019-08-05 18:22:27 -04003292void __init kfree_rcu_scheduler_running(void)
3293{
3294 int cpu;
3295 unsigned long flags;
3296
3297 for_each_online_cpu(cpu) {
3298 struct kfree_rcu_cpu *krcp = per_cpu_ptr(&krc, cpu);
3299
3300 spin_lock_irqsave(&krcp->lock, flags);
Joel Fernandes569d7672019-09-22 10:49:57 -07003301 if (!krcp->head || krcp->monitor_todo) {
Byungchul Parka35d1692019-08-05 18:22:27 -04003302 spin_unlock_irqrestore(&krcp->lock, flags);
3303 continue;
3304 }
Joel Fernandes569d7672019-09-22 10:49:57 -07003305 krcp->monitor_todo = true;
Joel Fernandes (Google)0392beb2019-09-19 14:58:26 -07003306 schedule_delayed_work_on(cpu, &krcp->monitor_work,
3307 KFREE_DRAIN_JIFFIES);
Byungchul Parka35d1692019-08-05 18:22:27 -04003308 spin_unlock_irqrestore(&krcp->lock, flags);
3309 }
3310}
3311
Paul E. McKenneye5bc3af2018-11-29 10:42:06 -08003312/*
3313 * During early boot, any blocking grace-period wait automatically
Sebastian Andrzej Siewior90326f02019-10-15 21:18:14 +02003314 * implies a grace period. Later on, this is never the case for PREEMPTION.
Paul E. McKenneye5bc3af2018-11-29 10:42:06 -08003315 *
Sebastian Andrzej Siewior90326f02019-10-15 21:18:14 +02003316 * Howevr, because a context switch is a grace period for !PREEMPTION, any
Paul E. McKenneye5bc3af2018-11-29 10:42:06 -08003317 * blocking grace-period wait automatically implies a grace period if
3318 * there is only one CPU online at any point time during execution of
3319 * either synchronize_rcu() or synchronize_rcu_expedited(). It is OK to
3320 * occasionally incorrectly indicate that there are multiple CPUs online
3321 * when there was in fact only one the whole time, as this just adds some
3322 * overhead: RCU still operates correctly.
3323 */
3324static int rcu_blocking_is_gp(void)
3325{
3326 int ret;
3327
Thomas Gleixner01b1d882019-07-26 23:19:38 +02003328 if (IS_ENABLED(CONFIG_PREEMPTION))
Paul E. McKenneye5bc3af2018-11-29 10:42:06 -08003329 return rcu_scheduler_active == RCU_SCHEDULER_INACTIVE;
3330 might_sleep(); /* Check for RCU read-side critical section. */
3331 preempt_disable();
3332 ret = num_online_cpus() <= 1;
3333 preempt_enable();
3334 return ret;
3335}
3336
3337/**
3338 * synchronize_rcu - wait until a grace period has elapsed.
3339 *
3340 * Control will return to the caller some time after a full grace
3341 * period has elapsed, in other words after all currently executing RCU
3342 * read-side critical sections have completed. Note, however, that
3343 * upon return from synchronize_rcu(), the caller might well be executing
3344 * concurrently with new RCU read-side critical sections that began while
3345 * synchronize_rcu() was waiting. RCU read-side critical sections are
3346 * delimited by rcu_read_lock() and rcu_read_unlock(), and may be nested.
3347 * In addition, regions of code across which interrupts, preemption, or
3348 * softirqs have been disabled also serve as RCU read-side critical
3349 * sections. This includes hardware interrupt handlers, softirq handlers,
3350 * and NMI handlers.
3351 *
3352 * Note that this guarantee implies further memory-ordering guarantees.
3353 * On systems with more than one CPU, when synchronize_rcu() returns,
3354 * each CPU is guaranteed to have executed a full memory barrier since
3355 * the end of its last RCU read-side critical section whose beginning
3356 * preceded the call to synchronize_rcu(). In addition, each CPU having
3357 * an RCU read-side critical section that extends beyond the return from
3358 * synchronize_rcu() is guaranteed to have executed a full memory barrier
3359 * after the beginning of synchronize_rcu() and before the beginning of
3360 * that RCU read-side critical section. Note that these guarantees include
3361 * CPUs that are offline, idle, or executing in user mode, as well as CPUs
3362 * that are executing in the kernel.
3363 *
3364 * Furthermore, if CPU A invoked synchronize_rcu(), which returned
3365 * to its caller on CPU B, then both CPU A and CPU B are guaranteed
3366 * to have executed a full memory barrier during the execution of
3367 * synchronize_rcu() -- even if CPU A and CPU B are the same CPU (but
3368 * again only if the system has more than one CPU).
3369 */
3370void synchronize_rcu(void)
3371{
3372 RCU_LOCKDEP_WARN(lock_is_held(&rcu_bh_lock_map) ||
3373 lock_is_held(&rcu_lock_map) ||
3374 lock_is_held(&rcu_sched_lock_map),
3375 "Illegal synchronize_rcu() in RCU read-side critical section");
3376 if (rcu_blocking_is_gp())
3377 return;
3378 if (rcu_gp_is_expedited())
3379 synchronize_rcu_expedited();
3380 else
3381 wait_rcu_gp(call_rcu);
3382}
3383EXPORT_SYMBOL_GPL(synchronize_rcu);
3384
Paul E. McKenney765a3f42014-03-14 16:37:08 -07003385/**
3386 * get_state_synchronize_rcu - Snapshot current RCU state
3387 *
3388 * Returns a cookie that is used by a later call to cond_synchronize_rcu()
3389 * to determine whether or not a full grace period has elapsed in the
3390 * meantime.
3391 */
3392unsigned long get_state_synchronize_rcu(void)
3393{
3394 /*
3395 * Any prior manipulation of RCU-protected data must happen
Paul E. McKenneye4be81a2018-04-27 15:16:50 -07003396 * before the load from ->gp_seq.
Paul E. McKenney765a3f42014-03-14 16:37:08 -07003397 */
3398 smp_mb(); /* ^^^ */
Paul E. McKenney16fc9c62018-07-03 15:54:39 -07003399 return rcu_seq_snap(&rcu_state.gp_seq);
Paul E. McKenney765a3f42014-03-14 16:37:08 -07003400}
3401EXPORT_SYMBOL_GPL(get_state_synchronize_rcu);
3402
3403/**
3404 * cond_synchronize_rcu - Conditionally wait for an RCU grace period
3405 *
3406 * @oldstate: return value from earlier call to get_state_synchronize_rcu()
3407 *
3408 * If a full RCU grace period has elapsed since the earlier call to
3409 * get_state_synchronize_rcu(), just return. Otherwise, invoke
3410 * synchronize_rcu() to wait for a full grace period.
3411 *
3412 * Yes, this function does not take counter wrap into account. But
3413 * counter wrap is harmless. If the counter wraps, we have waited for
3414 * more than 2 billion grace periods (and way more on a 64-bit system!),
3415 * so waiting for one additional grace period should be just fine.
3416 */
3417void cond_synchronize_rcu(unsigned long oldstate)
3418{
Paul E. McKenney16fc9c62018-07-03 15:54:39 -07003419 if (!rcu_seq_done(&rcu_state.gp_seq, oldstate))
Paul E. McKenney765a3f42014-03-14 16:37:08 -07003420 synchronize_rcu();
Paul E. McKenneye4be81a2018-04-27 15:16:50 -07003421 else
3422 smp_mb(); /* Ensure GP ends before subsequent accesses. */
Paul E. McKenney765a3f42014-03-14 16:37:08 -07003423}
3424EXPORT_SYMBOL_GPL(cond_synchronize_rcu);
3425
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003426/*
Paul E. McKenney98ece502018-07-03 17:22:34 -07003427 * Check to see if there is any immediate RCU-related work to be done by
Paul E. McKenney49918a52018-07-07 18:12:26 -07003428 * the current CPU, returning 1 if so and zero otherwise. The checks are
3429 * in order of increasing expense: checks that can be carried out against
3430 * CPU-local state are performed first. However, we must check for CPU
3431 * stalls first, else we might not get a chance.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003432 */
Paul E. McKenneydd7dafd2019-09-14 03:39:22 -07003433static int rcu_pending(int user)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003434{
Paul E. McKenneyed93dfc2019-09-13 14:09:56 -07003435 bool gp_in_progress;
Paul E. McKenney98ece502018-07-03 17:22:34 -07003436 struct rcu_data *rdp = this_cpu_ptr(&rcu_data);
Paul E. McKenney2f51f982009-11-13 19:51:39 -08003437 struct rcu_node *rnp = rdp->mynode;
3438
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003439 /* Check for CPU stalls, if enabled. */
Paul E. McKenneyea12ff22018-07-03 17:22:34 -07003440 check_cpu_stall(rdp);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003441
Paul E. McKenney85f69b32019-04-16 14:48:28 -07003442 /* Does this CPU need a deferred NOCB wakeup? */
3443 if (rcu_nocb_need_deferred_wakeup(rdp))
3444 return 1;
3445
Paul E. McKenneydd7dafd2019-09-14 03:39:22 -07003446 /* Is this a nohz_full CPU in userspace or idle? (Ignore RCU if so.) */
3447 if ((user || rcu_is_cpu_rrupt_from_idle()) && rcu_nohz_full_cpu())
Paul E. McKenneya0969322013-11-08 09:03:10 -08003448 return 0;
3449
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003450 /* Is the RCU core waiting for a quiescent state from this CPU? */
Paul E. McKenneyed93dfc2019-09-13 14:09:56 -07003451 gp_in_progress = rcu_gp_in_progress();
3452 if (rdp->core_needs_qs && !rdp->cpu_no_qs.b.norm && gp_in_progress)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003453 return 1;
3454
3455 /* Does this CPU have callbacks ready to invoke? */
Paul E. McKenney01c495f2018-01-10 12:36:00 -08003456 if (rcu_segcblist_ready_cbs(&rdp->cblist))
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003457 return 1;
3458
3459 /* Has RCU gone idle with this CPU needing another grace period? */
Paul E. McKenneyed93dfc2019-09-13 14:09:56 -07003460 if (!gp_in_progress && rcu_segcblist_is_enabled(&rdp->cblist) &&
Paul E. McKenney921bb5f2019-05-21 13:53:28 -07003461 (!IS_ENABLED(CONFIG_RCU_NOCB_CPU) ||
3462 !rcu_segcblist_is_offloaded(&rdp->cblist)) &&
Paul E. McKenneyc1935202018-04-12 16:29:13 -07003463 !rcu_segcblist_restempty(&rdp->cblist, RCU_NEXT_READY_TAIL))
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003464 return 1;
3465
Paul E. McKenney67e14c12018-04-27 16:01:46 -07003466 /* Have RCU grace period completed or started? */
3467 if (rcu_seq_current(&rnp->gp_seq) != rdp->gp_seq ||
Paul E. McKenney01c495f2018-01-10 12:36:00 -08003468 unlikely(READ_ONCE(rdp->gpwrap))) /* outside lock */
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003469 return 1;
3470
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003471 /* nothing to do */
3472 return 0;
3473}
3474
3475/*
Paul E. McKenneydd46a782018-07-10 18:37:30 -07003476 * Helper function for rcu_barrier() tracing. If tracing is disabled,
Paul E. McKenneya83eff02012-05-23 18:47:05 -07003477 * the compiler is expected to optimize this away.
3478 */
Paul E. McKenneydd46a782018-07-10 18:37:30 -07003479static void rcu_barrier_trace(const char *s, int cpu, unsigned long done)
Paul E. McKenneya83eff02012-05-23 18:47:05 -07003480{
Paul E. McKenney8344b872018-07-03 17:22:34 -07003481 trace_rcu_barrier(rcu_state.name, s, cpu,
3482 atomic_read(&rcu_state.barrier_cpu_count), done);
Paul E. McKenneya83eff02012-05-23 18:47:05 -07003483}
3484
3485/*
Paul E. McKenneydd46a782018-07-10 18:37:30 -07003486 * RCU callback function for rcu_barrier(). If we are last, wake
3487 * up the task executing rcu_barrier().
Paul E. McKenneyaa24f932020-01-20 15:43:45 -08003488 *
3489 * Note that the value of rcu_state.barrier_sequence must be captured
3490 * before the atomic_dec_and_test(). Otherwise, if this CPU is not last,
3491 * other CPUs might count the value down to zero before this CPU gets
3492 * around to invoking rcu_barrier_trace(), which might result in bogus
3493 * data from the next instance of rcu_barrier().
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08003494 */
Paul E. McKenney24ebbca2012-05-29 00:34:56 -07003495static void rcu_barrier_callback(struct rcu_head *rhp)
Paul E. McKenneyd0ec7742009-10-06 21:48:16 -07003496{
Paul E. McKenneyaa24f932020-01-20 15:43:45 -08003497 unsigned long __maybe_unused s = rcu_state.barrier_sequence;
3498
Paul E. McKenneyec9f5832018-07-05 16:26:12 -07003499 if (atomic_dec_and_test(&rcu_state.barrier_cpu_count)) {
Paul E. McKenneyaa24f932020-01-20 15:43:45 -08003500 rcu_barrier_trace(TPS("LastCB"), -1, s);
Paul E. McKenneyec9f5832018-07-05 16:26:12 -07003501 complete(&rcu_state.barrier_completion);
Paul E. McKenneya83eff02012-05-23 18:47:05 -07003502 } else {
Paul E. McKenneyaa24f932020-01-20 15:43:45 -08003503 rcu_barrier_trace(TPS("CB"), -1, s);
Paul E. McKenneya83eff02012-05-23 18:47:05 -07003504 }
Paul E. McKenneyd0ec7742009-10-06 21:48:16 -07003505}
3506
3507/*
3508 * Called with preemption disabled, and from cross-cpu IRQ context.
3509 */
Paul E. McKenney127e2982020-02-11 06:17:33 -08003510static void rcu_barrier_func(void *cpu_in)
Paul E. McKenneyd0ec7742009-10-06 21:48:16 -07003511{
Paul E. McKenney127e2982020-02-11 06:17:33 -08003512 uintptr_t cpu = (uintptr_t)cpu_in;
3513 struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
Paul E. McKenneyd0ec7742009-10-06 21:48:16 -07003514
Paul E. McKenneydd46a782018-07-10 18:37:30 -07003515 rcu_barrier_trace(TPS("IRQ"), -1, rcu_state.barrier_sequence);
Paul E. McKenneyf92c7342017-04-10 15:40:35 -07003516 rdp->barrier_head.func = rcu_barrier_callback;
3517 debug_rcu_head_queue(&rdp->barrier_head);
Paul E. McKenney5d6742b2019-05-15 09:56:40 -07003518 rcu_nocb_lock(rdp);
Paul E. McKenneyd1b222c2019-07-02 16:03:33 -07003519 WARN_ON_ONCE(!rcu_nocb_flush_bypass(rdp, NULL, jiffies));
Joel Fernandes (Google)77a40f92019-08-30 12:36:32 -04003520 if (rcu_segcblist_entrain(&rdp->cblist, &rdp->barrier_head)) {
Paul E. McKenneyec9f5832018-07-05 16:26:12 -07003521 atomic_inc(&rcu_state.barrier_cpu_count);
Paul E. McKenneyf92c7342017-04-10 15:40:35 -07003522 } else {
3523 debug_rcu_head_unqueue(&rdp->barrier_head);
Paul E. McKenneydd46a782018-07-10 18:37:30 -07003524 rcu_barrier_trace(TPS("IRQNQ"), -1,
Paul E. McKenney66e4c332019-08-12 16:14:00 -07003525 rcu_state.barrier_sequence);
Paul E. McKenneyf92c7342017-04-10 15:40:35 -07003526 }
Paul E. McKenney5d6742b2019-05-15 09:56:40 -07003527 rcu_nocb_unlock(rdp);
Paul E. McKenneyd0ec7742009-10-06 21:48:16 -07003528}
3529
Paul E. McKenneydd46a782018-07-10 18:37:30 -07003530/**
3531 * rcu_barrier - Wait until all in-flight call_rcu() callbacks complete.
3532 *
3533 * Note that this primitive does not necessarily wait for an RCU grace period
3534 * to complete. For example, if there are no RCU callbacks queued anywhere
3535 * in the system, then rcu_barrier() is within its rights to return
3536 * immediately, without waiting for anything, much less an RCU grace period.
Paul E. McKenneyd0ec7742009-10-06 21:48:16 -07003537 */
Paul E. McKenneydd46a782018-07-10 18:37:30 -07003538void rcu_barrier(void)
Paul E. McKenneyd0ec7742009-10-06 21:48:16 -07003539{
Paul E. McKenney127e2982020-02-11 06:17:33 -08003540 uintptr_t cpu;
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08003541 struct rcu_data *rdp;
Paul E. McKenneyec9f5832018-07-05 16:26:12 -07003542 unsigned long s = rcu_seq_snap(&rcu_state.barrier_sequence);
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08003543
Paul E. McKenneydd46a782018-07-10 18:37:30 -07003544 rcu_barrier_trace(TPS("Begin"), -1, s);
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08003545
Paul E. McKenneye74f4c42009-10-06 21:48:17 -07003546 /* Take mutex to serialize concurrent rcu_barrier() requests. */
Paul E. McKenneyec9f5832018-07-05 16:26:12 -07003547 mutex_lock(&rcu_state.barrier_mutex);
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08003548
Paul E. McKenney4f525a52015-06-26 11:20:00 -07003549 /* Did someone else do our work for us? */
Paul E. McKenneyec9f5832018-07-05 16:26:12 -07003550 if (rcu_seq_done(&rcu_state.barrier_sequence, s)) {
Paul E. McKenneydd46a782018-07-10 18:37:30 -07003551 rcu_barrier_trace(TPS("EarlyExit"), -1,
Paul E. McKenney66e4c332019-08-12 16:14:00 -07003552 rcu_state.barrier_sequence);
Paul E. McKenneycf3a9c42012-05-29 14:56:46 -07003553 smp_mb(); /* caller's subsequent code after above check. */
Paul E. McKenneyec9f5832018-07-05 16:26:12 -07003554 mutex_unlock(&rcu_state.barrier_mutex);
Paul E. McKenneycf3a9c42012-05-29 14:56:46 -07003555 return;
3556 }
3557
Paul E. McKenney4f525a52015-06-26 11:20:00 -07003558 /* Mark the start of the barrier operation. */
Paul E. McKenneyec9f5832018-07-05 16:26:12 -07003559 rcu_seq_start(&rcu_state.barrier_sequence);
Paul E. McKenneydd46a782018-07-10 18:37:30 -07003560 rcu_barrier_trace(TPS("Inc1"), -1, rcu_state.barrier_sequence);
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08003561
Paul E. McKenneyd0ec7742009-10-06 21:48:16 -07003562 /*
Paul E. McKenney127e2982020-02-11 06:17:33 -08003563 * Initialize the count to two rather than to zero in order
3564 * to avoid a too-soon return to zero in case of an immediate
3565 * invocation of the just-enqueued callback (or preemption of
3566 * this task). Exclude CPU-hotplug operations to ensure that no
3567 * offline non-offloaded CPU has callbacks queued.
Paul E. McKenneyd0ec7742009-10-06 21:48:16 -07003568 */
Paul E. McKenneyec9f5832018-07-05 16:26:12 -07003569 init_completion(&rcu_state.barrier_completion);
Paul E. McKenney127e2982020-02-11 06:17:33 -08003570 atomic_set(&rcu_state.barrier_cpu_count, 2);
Paul E. McKenney1331e7a2012-08-02 17:43:50 -07003571 get_online_cpus();
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08003572
3573 /*
Paul E. McKenney1331e7a2012-08-02 17:43:50 -07003574 * Force each CPU with callbacks to register a new callback.
3575 * When that callback is invoked, we will know that all of the
3576 * corresponding CPU's preceding callbacks have been invoked.
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08003577 */
Paul E. McKenney3fbfbf72012-08-19 21:35:53 -07003578 for_each_possible_cpu(cpu) {
Paul E. McKenneyda1df502018-07-03 15:37:16 -07003579 rdp = per_cpu_ptr(&rcu_data, cpu);
Paul E. McKenney127e2982020-02-11 06:17:33 -08003580 if (cpu_is_offline(cpu) &&
Paul E. McKenneyce5215c2019-04-12 15:58:34 -07003581 !rcu_segcblist_is_offloaded(&rdp->cblist))
3582 continue;
Paul E. McKenney127e2982020-02-11 06:17:33 -08003583 if (rcu_segcblist_n_cbs(&rdp->cblist) && cpu_online(cpu)) {
Paul E. McKenneydd46a782018-07-10 18:37:30 -07003584 rcu_barrier_trace(TPS("OnlineQ"), cpu,
Paul E. McKenney66e4c332019-08-12 16:14:00 -07003585 rcu_state.barrier_sequence);
Paul E. McKenney127e2982020-02-11 06:17:33 -08003586 smp_call_function_single(cpu, rcu_barrier_func, (void *)cpu, 1);
3587 } else if (rcu_segcblist_n_cbs(&rdp->cblist) &&
3588 cpu_is_offline(cpu)) {
3589 rcu_barrier_trace(TPS("OfflineNoCBQ"), cpu,
3590 rcu_state.barrier_sequence);
3591 local_irq_disable();
3592 rcu_barrier_func((void *)cpu);
3593 local_irq_enable();
3594 } else if (cpu_is_offline(cpu)) {
3595 rcu_barrier_trace(TPS("OfflineNoCBNoQ"), cpu,
3596 rcu_state.barrier_sequence);
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08003597 } else {
Paul E. McKenneydd46a782018-07-10 18:37:30 -07003598 rcu_barrier_trace(TPS("OnlineNQ"), cpu,
Paul E. McKenney66e4c332019-08-12 16:14:00 -07003599 rcu_state.barrier_sequence);
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08003600 }
3601 }
Paul E. McKenney1331e7a2012-08-02 17:43:50 -07003602 put_online_cpus();
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08003603
3604 /*
3605 * Now that we have an rcu_barrier_callback() callback on each
3606 * CPU, and thus each counted, remove the initial count.
3607 */
Paul E. McKenney127e2982020-02-11 06:17:33 -08003608 if (atomic_sub_and_test(2, &rcu_state.barrier_cpu_count))
Paul E. McKenneyec9f5832018-07-05 16:26:12 -07003609 complete(&rcu_state.barrier_completion);
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08003610
3611 /* Wait for all rcu_barrier_callback() callbacks to be invoked. */
Paul E. McKenneyec9f5832018-07-05 16:26:12 -07003612 wait_for_completion(&rcu_state.barrier_completion);
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08003613
Paul E. McKenney4f525a52015-06-26 11:20:00 -07003614 /* Mark the end of the barrier operation. */
Paul E. McKenneydd46a782018-07-10 18:37:30 -07003615 rcu_barrier_trace(TPS("Inc2"), -1, rcu_state.barrier_sequence);
Paul E. McKenneyec9f5832018-07-05 16:26:12 -07003616 rcu_seq_end(&rcu_state.barrier_sequence);
Paul E. McKenney4f525a52015-06-26 11:20:00 -07003617
Paul E. McKenneyb1420f12012-03-01 13:18:08 -08003618 /* Other rcu_barrier() invocations can now safely proceed. */
Paul E. McKenneyec9f5832018-07-05 16:26:12 -07003619 mutex_unlock(&rcu_state.barrier_mutex);
Paul E. McKenneyd0ec7742009-10-06 21:48:16 -07003620}
Paul E. McKenney45975c72018-07-02 14:30:37 -07003621EXPORT_SYMBOL_GPL(rcu_barrier);
Paul E. McKenneyd0ec7742009-10-06 21:48:16 -07003622
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003623/*
Paul E. McKenney0aa04b02015-01-23 21:52:37 -08003624 * Propagate ->qsinitmask bits up the rcu_node tree to account for the
3625 * first CPU in a given leaf rcu_node structure coming online. The caller
3626 * must hold the corresponding leaf rcu_node ->lock with interrrupts
3627 * disabled.
3628 */
3629static void rcu_init_new_rnp(struct rcu_node *rnp_leaf)
3630{
3631 long mask;
Paul E. McKenney8d672fa2018-05-02 14:46:43 -07003632 long oldmask;
Paul E. McKenney0aa04b02015-01-23 21:52:37 -08003633 struct rcu_node *rnp = rnp_leaf;
3634
Paul E. McKenney8d672fa2018-05-02 14:46:43 -07003635 raw_lockdep_assert_held_rcu_node(rnp_leaf);
Paul E. McKenney962aff02018-05-02 12:49:21 -07003636 WARN_ON_ONCE(rnp->wait_blkd_tasks);
Paul E. McKenney0aa04b02015-01-23 21:52:37 -08003637 for (;;) {
3638 mask = rnp->grpmask;
3639 rnp = rnp->parent;
3640 if (rnp == NULL)
3641 return;
Paul E. McKenney6cf10082015-10-08 15:36:54 -07003642 raw_spin_lock_rcu_node(rnp); /* Interrupts already disabled. */
Paul E. McKenney8d672fa2018-05-02 14:46:43 -07003643 oldmask = rnp->qsmaskinit;
Paul E. McKenney0aa04b02015-01-23 21:52:37 -08003644 rnp->qsmaskinit |= mask;
Boqun Feng67c583a72015-12-29 12:18:47 +08003645 raw_spin_unlock_rcu_node(rnp); /* Interrupts remain disabled. */
Paul E. McKenney8d672fa2018-05-02 14:46:43 -07003646 if (oldmask)
3647 return;
Paul E. McKenney0aa04b02015-01-23 21:52:37 -08003648 }
3649}
3650
3651/*
Paul E. McKenney27569622009-08-15 09:53:46 -07003652 * Do boot-time initialization of a CPU's per-CPU RCU data.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003653 */
Paul E. McKenney27569622009-08-15 09:53:46 -07003654static void __init
Paul E. McKenney53b46302018-07-03 17:22:34 -07003655rcu_boot_init_percpu_data(int cpu)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003656{
Paul E. McKenneyda1df502018-07-03 15:37:16 -07003657 struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
Paul E. McKenney27569622009-08-15 09:53:46 -07003658
3659 /* Set up local state, ensuring consistent view of global state. */
Mark Rutlandbc75e992016-06-03 15:20:04 +01003660 rdp->grpmask = leaf_node_cpu_bit(rdp->mynode, cpu);
Paul E. McKenney4c5273b2018-08-03 21:00:38 -07003661 WARN_ON_ONCE(rdp->dynticks_nesting != 1);
Paul E. McKenneydc5a4f22018-08-03 21:00:38 -07003662 WARN_ON_ONCE(rcu_dynticks_in_eqs(rcu_dynticks_snap(rdp)));
Paul E. McKenney53b46302018-07-03 17:22:34 -07003663 rdp->rcu_ofl_gp_seq = rcu_state.gp_seq;
Paul E. McKenney57738942018-05-08 14:18:57 -07003664 rdp->rcu_ofl_gp_flags = RCU_GP_CLEANED;
Paul E. McKenney53b46302018-07-03 17:22:34 -07003665 rdp->rcu_onl_gp_seq = rcu_state.gp_seq;
Paul E. McKenney57738942018-05-08 14:18:57 -07003666 rdp->rcu_onl_gp_flags = RCU_GP_CLEANED;
Paul E. McKenney27569622009-08-15 09:53:46 -07003667 rdp->cpu = cpu;
Paul E. McKenney3fbfbf72012-08-19 21:35:53 -07003668 rcu_boot_init_nocb_percpu_data(rdp);
Paul E. McKenney27569622009-08-15 09:53:46 -07003669}
3670
3671/*
Paul E. McKenney53b46302018-07-03 17:22:34 -07003672 * Invoked early in the CPU-online process, when pretty much all services
3673 * are available. The incoming CPU is not present.
3674 *
3675 * Initializes a CPU's per-CPU RCU data. Note that only one online or
Paul E. McKenneyff3bb6f2018-05-01 14:34:08 -07003676 * offline event can be happening at a given time. Note also that we can
3677 * accept some slop in the rsp->gp_seq access due to the fact that this
Paul E. McKenneye83e73f2019-05-14 09:50:49 -07003678 * CPU cannot possibly have any non-offloaded RCU callbacks in flight yet.
3679 * And any offloaded callbacks are being numbered elsewhere.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003680 */
Paul E. McKenney53b46302018-07-03 17:22:34 -07003681int rcutree_prepare_cpu(unsigned int cpu)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003682{
3683 unsigned long flags;
Paul E. McKenneyda1df502018-07-03 15:37:16 -07003684 struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
Paul E. McKenney336a4f62018-07-03 17:22:34 -07003685 struct rcu_node *rnp = rcu_get_root();
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003686
3687 /* Set up local state, ensuring consistent view of global state. */
Paul E. McKenney6cf10082015-10-08 15:36:54 -07003688 raw_spin_lock_irqsave_rcu_node(rnp, flags);
Paul E. McKenney37c72e52009-10-14 10:15:55 -07003689 rdp->qlen_last_fqs_check = 0;
Paul E. McKenney53b46302018-07-03 17:22:34 -07003690 rdp->n_force_qs_snap = rcu_state.n_force_qs;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003691 rdp->blimit = blimit;
Paul E. McKenney15fecf82017-02-08 12:36:42 -08003692 if (rcu_segcblist_empty(&rdp->cblist) && /* No early-boot CBs? */
Paul E. McKenneye83e73f2019-05-14 09:50:49 -07003693 !rcu_segcblist_is_offloaded(&rdp->cblist))
Paul E. McKenney15fecf82017-02-08 12:36:42 -08003694 rcu_segcblist_init(&rdp->cblist); /* Re-enable callbacks. */
Paul E. McKenney4c5273b2018-08-03 21:00:38 -07003695 rdp->dynticks_nesting = 1; /* CPU not up, no tearing. */
Paul E. McKenney2625d462016-11-02 14:23:30 -07003696 rcu_dynticks_eqs_online();
Boqun Feng67c583a72015-12-29 12:18:47 +08003697 raw_spin_unlock_rcu_node(rnp); /* irqs remain disabled. */
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003698
Paul E. McKenney0aa04b02015-01-23 21:52:37 -08003699 /*
3700 * Add CPU to leaf rcu_node pending-online bitmask. Any needed
3701 * propagation up the rcu_node tree will happen at the beginning
3702 * of the next grace period.
3703 */
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003704 rnp = rdp->mynode;
Peter Zijlstra2a67e742015-10-08 12:24:23 +02003705 raw_spin_lock_rcu_node(rnp); /* irqs already disabled. */
Paul E. McKenneyb9585e92015-07-31 16:04:45 -07003706 rdp->beenonline = true; /* We have now been online. */
Paul E. McKenney8ff372902020-01-04 11:33:17 -08003707 rdp->gp_seq = READ_ONCE(rnp->gp_seq);
3708 rdp->gp_seq_needed = rdp->gp_seq;
Paul E. McKenney5b74c452015-08-06 15:16:57 -07003709 rdp->cpu_no_qs.b.norm = true;
Paul E. McKenney97c668b2015-08-06 11:31:51 -07003710 rdp->core_needs_qs = false;
Paul E. McKenney9b9500d2017-08-17 17:05:59 -07003711 rdp->rcu_iw_pending = false;
Paul E. McKenney8ff372902020-01-04 11:33:17 -08003712 rdp->rcu_iw_gp_seq = rdp->gp_seq - 1;
Paul E. McKenney53b46302018-07-03 17:22:34 -07003713 trace_rcu_grace_period(rcu_state.name, rdp->gp_seq, TPS("cpuonl"));
Boqun Feng67c583a72015-12-29 12:18:47 +08003714 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
Thomas Gleixner4df83742016-07-13 17:17:03 +00003715 rcu_prepare_kthreads(cpu);
Paul E. McKenneyad368d12018-11-27 13:55:53 -08003716 rcu_spawn_cpu_nocb_kthread(cpu);
Thomas Gleixner4df83742016-07-13 17:17:03 +00003717
3718 return 0;
3719}
3720
Paul E. McKenneydeb34f32017-03-23 13:21:30 -07003721/*
3722 * Update RCU priority boot kthread affinity for CPU-hotplug changes.
3723 */
Thomas Gleixner4df83742016-07-13 17:17:03 +00003724static void rcutree_affinity_setting(unsigned int cpu, int outgoing)
3725{
Paul E. McKenneyda1df502018-07-03 15:37:16 -07003726 struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
Thomas Gleixner4df83742016-07-13 17:17:03 +00003727
3728 rcu_boost_kthread_setaffinity(rdp->mynode, outgoing);
3729}
3730
Paul E. McKenneydeb34f32017-03-23 13:21:30 -07003731/*
3732 * Near the end of the CPU-online process. Pretty much all services
3733 * enabled, and the CPU is now very much alive.
3734 */
Thomas Gleixner4df83742016-07-13 17:17:03 +00003735int rcutree_online_cpu(unsigned int cpu)
3736{
Paul E. McKenney9b9500d2017-08-17 17:05:59 -07003737 unsigned long flags;
3738 struct rcu_data *rdp;
3739 struct rcu_node *rnp;
Paul E. McKenney9b9500d2017-08-17 17:05:59 -07003740
Paul E. McKenneyb97d23c2018-07-04 15:35:00 -07003741 rdp = per_cpu_ptr(&rcu_data, cpu);
3742 rnp = rdp->mynode;
3743 raw_spin_lock_irqsave_rcu_node(rnp, flags);
3744 rnp->ffmask |= rdp->grpmask;
3745 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
Paul E. McKenney9b9500d2017-08-17 17:05:59 -07003746 if (rcu_scheduler_active == RCU_SCHEDULER_INACTIVE)
3747 return 0; /* Too early in boot for scheduler work. */
3748 sync_sched_exp_online_cleanup(cpu);
3749 rcutree_affinity_setting(cpu, -1);
Paul E. McKenney96926682019-08-02 15:12:47 -07003750
3751 // Stop-machine done, so allow nohz_full to disable tick.
3752 tick_dep_clear(TICK_DEP_BIT_RCU);
Thomas Gleixner4df83742016-07-13 17:17:03 +00003753 return 0;
3754}
3755
Paul E. McKenneydeb34f32017-03-23 13:21:30 -07003756/*
3757 * Near the beginning of the process. The CPU is still very much alive
3758 * with pretty much all services enabled.
3759 */
Thomas Gleixner4df83742016-07-13 17:17:03 +00003760int rcutree_offline_cpu(unsigned int cpu)
3761{
Paul E. McKenney9b9500d2017-08-17 17:05:59 -07003762 unsigned long flags;
3763 struct rcu_data *rdp;
3764 struct rcu_node *rnp;
Paul E. McKenney9b9500d2017-08-17 17:05:59 -07003765
Paul E. McKenneyb97d23c2018-07-04 15:35:00 -07003766 rdp = per_cpu_ptr(&rcu_data, cpu);
3767 rnp = rdp->mynode;
3768 raw_spin_lock_irqsave_rcu_node(rnp, flags);
3769 rnp->ffmask &= ~rdp->grpmask;
3770 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
Paul E. McKenney9b9500d2017-08-17 17:05:59 -07003771
Thomas Gleixner4df83742016-07-13 17:17:03 +00003772 rcutree_affinity_setting(cpu, cpu);
Paul E. McKenney96926682019-08-02 15:12:47 -07003773
3774 // nohz_full CPUs need the tick for stop-machine to work quickly
3775 tick_dep_set(TICK_DEP_BIT_RCU);
Thomas Gleixner4df83742016-07-13 17:17:03 +00003776 return 0;
3777}
3778
Peter Zijlstraf64c6012018-05-22 09:50:53 -07003779static DEFINE_PER_CPU(int, rcu_cpu_started);
3780
Paul E. McKenney7ec99de2016-06-30 13:58:26 -07003781/*
3782 * Mark the specified CPU as being online so that subsequent grace periods
3783 * (both expedited and normal) will wait on it. Note that this means that
3784 * incoming CPUs are not allowed to use RCU read-side critical sections
3785 * until this function is called. Failing to observe this restriction
3786 * will result in lockdep splats.
Paul E. McKenneydeb34f32017-03-23 13:21:30 -07003787 *
3788 * Note that this function is special in that it is invoked directly
3789 * from the incoming CPU rather than from the cpuhp_step mechanism.
3790 * This is because this function must be invoked at a precise location.
Paul E. McKenney7ec99de2016-06-30 13:58:26 -07003791 */
3792void rcu_cpu_starting(unsigned int cpu)
3793{
3794 unsigned long flags;
3795 unsigned long mask;
Paul E. McKenney313517fc2017-06-08 16:55:40 -07003796 int nbits;
3797 unsigned long oldmask;
Paul E. McKenney7ec99de2016-06-30 13:58:26 -07003798 struct rcu_data *rdp;
3799 struct rcu_node *rnp;
Paul E. McKenney7ec99de2016-06-30 13:58:26 -07003800
Peter Zijlstraf64c6012018-05-22 09:50:53 -07003801 if (per_cpu(rcu_cpu_started, cpu))
3802 return;
3803
3804 per_cpu(rcu_cpu_started, cpu) = 1;
3805
Paul E. McKenneyb97d23c2018-07-04 15:35:00 -07003806 rdp = per_cpu_ptr(&rcu_data, cpu);
3807 rnp = rdp->mynode;
3808 mask = rdp->grpmask;
3809 raw_spin_lock_irqsave_rcu_node(rnp, flags);
Paul E. McKenney105abf82020-01-03 15:44:23 -08003810 WRITE_ONCE(rnp->qsmaskinitnext, rnp->qsmaskinitnext | mask);
Paul E. McKenneyb97d23c2018-07-04 15:35:00 -07003811 oldmask = rnp->expmaskinitnext;
3812 rnp->expmaskinitnext |= mask;
3813 oldmask ^= rnp->expmaskinitnext;
3814 nbits = bitmap_weight(&oldmask, BITS_PER_LONG);
3815 /* Allow lockless access for expedited grace periods. */
Paul E. McKenneyeb7a6652018-07-05 17:47:45 -07003816 smp_store_release(&rcu_state.ncpus, rcu_state.ncpus + nbits); /* ^^^ */
Paul E. McKenney2f084692020-02-10 05:29:58 -08003817 ASSERT_EXCLUSIVE_WRITER(rcu_state.ncpus);
Paul E. McKenneyb97d23c2018-07-04 15:35:00 -07003818 rcu_gpnum_ovf(rnp, rdp); /* Offline-induced counter wrap? */
Paul E. McKenneyeb7a6652018-07-05 17:47:45 -07003819 rdp->rcu_onl_gp_seq = READ_ONCE(rcu_state.gp_seq);
3820 rdp->rcu_onl_gp_flags = READ_ONCE(rcu_state.gp_flags);
Paul E. McKenneyb97d23c2018-07-04 15:35:00 -07003821 if (rnp->qsmask & mask) { /* RCU waiting on incoming CPU? */
Joel Fernandes (Google)516e5ae2019-09-05 10:26:41 -07003822 rcu_disable_urgency_upon_qs(rdp);
Paul E. McKenneyb97d23c2018-07-04 15:35:00 -07003823 /* Report QS -after- changing ->qsmaskinitnext! */
3824 rcu_report_qs_rnp(mask, rnp, rnp->gp_seq, flags);
3825 } else {
3826 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
Paul E. McKenney7ec99de2016-06-30 13:58:26 -07003827 }
Paul E. McKenney313517fc2017-06-08 16:55:40 -07003828 smp_mb(); /* Ensure RCU read-side usage follows above initialization. */
Paul E. McKenney7ec99de2016-06-30 13:58:26 -07003829}
3830
Thomas Gleixner27d50c72016-02-26 18:43:44 +00003831#ifdef CONFIG_HOTPLUG_CPU
3832/*
Paul E. McKenneydeb34f32017-03-23 13:21:30 -07003833 * The outgoing function has no further need of RCU, so remove it from
Paul E. McKenney53b46302018-07-03 17:22:34 -07003834 * the rcu_node tree's ->qsmaskinitnext bit masks.
Paul E. McKenneydeb34f32017-03-23 13:21:30 -07003835 *
3836 * Note that this function is special in that it is invoked directly
3837 * from the outgoing CPU rather than from the cpuhp_step mechanism.
3838 * This is because this function must be invoked at a precise location.
3839 */
Thomas Gleixner27d50c72016-02-26 18:43:44 +00003840void rcu_report_dead(unsigned int cpu)
3841{
Paul E. McKenney53b46302018-07-03 17:22:34 -07003842 unsigned long flags;
3843 unsigned long mask;
3844 struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
3845 struct rcu_node *rnp = rdp->mynode; /* Outgoing CPU's rdp & rnp. */
Thomas Gleixner27d50c72016-02-26 18:43:44 +00003846
Paul E. McKenney49918a52018-07-07 18:12:26 -07003847 /* QS for any half-done expedited grace period. */
Thomas Gleixner27d50c72016-02-26 18:43:44 +00003848 preempt_disable();
Paul E. McKenney63d4c8c2018-07-03 17:22:34 -07003849 rcu_report_exp_rdp(this_cpu_ptr(&rcu_data));
Thomas Gleixner27d50c72016-02-26 18:43:44 +00003850 preempt_enable();
Paul E. McKenney3e310092018-06-21 12:50:01 -07003851 rcu_preempt_deferred_qs(current);
Paul E. McKenney53b46302018-07-03 17:22:34 -07003852
3853 /* Remove outgoing CPU from mask in the leaf rcu_node structure. */
3854 mask = rdp->grpmask;
Mike Galbraith894d45b2018-08-15 09:05:29 -07003855 raw_spin_lock(&rcu_state.ofl_lock);
Paul E. McKenney53b46302018-07-03 17:22:34 -07003856 raw_spin_lock_irqsave_rcu_node(rnp, flags); /* Enforce GP memory-order guarantee. */
3857 rdp->rcu_ofl_gp_seq = READ_ONCE(rcu_state.gp_seq);
3858 rdp->rcu_ofl_gp_flags = READ_ONCE(rcu_state.gp_flags);
3859 if (rnp->qsmask & mask) { /* RCU waiting on outgoing CPU? */
3860 /* Report quiescent state -before- changing ->qsmaskinitnext! */
3861 rcu_report_qs_rnp(mask, rnp, rnp->gp_seq, flags);
3862 raw_spin_lock_irqsave_rcu_node(rnp, flags);
3863 }
Paul E. McKenney105abf82020-01-03 15:44:23 -08003864 WRITE_ONCE(rnp->qsmaskinitnext, rnp->qsmaskinitnext & ~mask);
Paul E. McKenney53b46302018-07-03 17:22:34 -07003865 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
Mike Galbraith894d45b2018-08-15 09:05:29 -07003866 raw_spin_unlock(&rcu_state.ofl_lock);
Peter Zijlstraf64c6012018-05-22 09:50:53 -07003867
3868 per_cpu(rcu_cpu_started, cpu) = 0;
Thomas Gleixner27d50c72016-02-26 18:43:44 +00003869}
Paul E. McKenneya58163d2017-06-20 12:11:34 -07003870
Paul E. McKenney53b46302018-07-03 17:22:34 -07003871/*
3872 * The outgoing CPU has just passed through the dying-idle state, and we
3873 * are being invoked from the CPU that was IPIed to continue the offline
3874 * operation. Migrate the outgoing CPU's callbacks to the current CPU.
3875 */
3876void rcutree_migrate_callbacks(int cpu)
Paul E. McKenneya58163d2017-06-20 12:11:34 -07003877{
3878 unsigned long flags;
Paul E. McKenneyb1a2d792017-06-26 12:23:46 -07003879 struct rcu_data *my_rdp;
Paul E. McKenneyc00045be2019-04-16 14:09:15 -07003880 struct rcu_node *my_rnp;
Paul E. McKenneyda1df502018-07-03 15:37:16 -07003881 struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
Paul E. McKenneyec4eacc2018-04-22 08:49:24 -07003882 bool needwake;
Paul E. McKenneya58163d2017-06-20 12:11:34 -07003883
Paul E. McKenneyce5215c2019-04-12 15:58:34 -07003884 if (rcu_segcblist_is_offloaded(&rdp->cblist) ||
3885 rcu_segcblist_empty(&rdp->cblist))
Paul E. McKenney95335c02017-06-26 10:49:50 -07003886 return; /* No callbacks to migrate. */
3887
Paul E. McKenneyb1a2d792017-06-26 12:23:46 -07003888 local_irq_save(flags);
Paul E. McKenneyda1df502018-07-03 15:37:16 -07003889 my_rdp = this_cpu_ptr(&rcu_data);
Paul E. McKenneyc00045be2019-04-16 14:09:15 -07003890 my_rnp = my_rdp->mynode;
Paul E. McKenney5d6742b2019-05-15 09:56:40 -07003891 rcu_nocb_lock(my_rdp); /* irqs already disabled. */
Paul E. McKenneyd1b222c2019-07-02 16:03:33 -07003892 WARN_ON_ONCE(!rcu_nocb_flush_bypass(my_rdp, NULL, jiffies));
Paul E. McKenneyc00045be2019-04-16 14:09:15 -07003893 raw_spin_lock_rcu_node(my_rnp); /* irqs already disabled. */
Paul E. McKenneyec4eacc2018-04-22 08:49:24 -07003894 /* Leverage recent GPs and set GP for new callbacks. */
Paul E. McKenneyc00045be2019-04-16 14:09:15 -07003895 needwake = rcu_advance_cbs(my_rnp, rdp) ||
3896 rcu_advance_cbs(my_rnp, my_rdp);
Paul E. McKenneyf2dbe4a2017-06-27 07:44:06 -07003897 rcu_segcblist_merge(&my_rdp->cblist, &rdp->cblist);
Paul E. McKenney23651d92019-07-10 12:54:56 -07003898 needwake = needwake || rcu_advance_cbs(my_rnp, my_rdp);
Paul E. McKenneyc0352802019-05-21 08:28:41 -07003899 rcu_segcblist_disable(&rdp->cblist);
Paul E. McKenney09efeee2017-07-19 10:56:46 -07003900 WARN_ON_ONCE(rcu_segcblist_empty(&my_rdp->cblist) !=
3901 !rcu_segcblist_n_cbs(&my_rdp->cblist));
Paul E. McKenney5d6742b2019-05-15 09:56:40 -07003902 if (rcu_segcblist_is_offloaded(&my_rdp->cblist)) {
3903 raw_spin_unlock_rcu_node(my_rnp); /* irqs remain disabled. */
3904 __call_rcu_nocb_wake(my_rdp, true, flags);
3905 } else {
3906 rcu_nocb_unlock(my_rdp); /* irqs remain disabled. */
3907 raw_spin_unlock_irqrestore_rcu_node(my_rnp, flags);
3908 }
Paul E. McKenneyec4eacc2018-04-22 08:49:24 -07003909 if (needwake)
Paul E. McKenney532c00c2018-07-03 17:22:34 -07003910 rcu_gp_kthread_wake();
Paul E. McKenney5d6742b2019-05-15 09:56:40 -07003911 lockdep_assert_irqs_enabled();
Paul E. McKenneya58163d2017-06-20 12:11:34 -07003912 WARN_ONCE(rcu_segcblist_n_cbs(&rdp->cblist) != 0 ||
3913 !rcu_segcblist_empty(&rdp->cblist),
3914 "rcu_cleanup_dead_cpu: Callbacks on offline CPU %d: qlen=%lu, 1stCB=%p\n",
3915 cpu, rcu_segcblist_n_cbs(&rdp->cblist),
3916 rcu_segcblist_first_cb(&rdp->cblist));
3917}
Thomas Gleixner27d50c72016-02-26 18:43:44 +00003918#endif
3919
Paul E. McKenneydeb34f32017-03-23 13:21:30 -07003920/*
3921 * On non-huge systems, use expedited RCU grace periods to make suspend
3922 * and hibernation run faster.
3923 */
Borislav Petkovd1d74d12013-04-22 00:12:42 +02003924static int rcu_pm_notify(struct notifier_block *self,
3925 unsigned long action, void *hcpu)
3926{
3927 switch (action) {
3928 case PM_HIBERNATION_PREPARE:
3929 case PM_SUSPEND_PREPARE:
Paul E. McKenneye85e6a22019-01-10 15:30:15 -08003930 rcu_expedite_gp();
Borislav Petkovd1d74d12013-04-22 00:12:42 +02003931 break;
3932 case PM_POST_HIBERNATION:
3933 case PM_POST_SUSPEND:
Paul E. McKenneye85e6a22019-01-10 15:30:15 -08003934 rcu_unexpedite_gp();
Borislav Petkovd1d74d12013-04-22 00:12:42 +02003935 break;
3936 default:
3937 break;
3938 }
3939 return NOTIFY_OK;
3940}
3941
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003942/*
Paul E. McKenney49918a52018-07-07 18:12:26 -07003943 * Spawn the kthreads that handle RCU's grace periods.
Paul E. McKenneyb3dbec72012-06-18 18:36:08 -07003944 */
3945static int __init rcu_spawn_gp_kthread(void)
3946{
3947 unsigned long flags;
Paul E. McKenneya94844b2014-12-12 07:37:48 -08003948 int kthread_prio_in = kthread_prio;
Paul E. McKenneyb3dbec72012-06-18 18:36:08 -07003949 struct rcu_node *rnp;
Paul E. McKenneya94844b2014-12-12 07:37:48 -08003950 struct sched_param sp;
Paul E. McKenneyb3dbec72012-06-18 18:36:08 -07003951 struct task_struct *t;
3952
Paul E. McKenneya94844b2014-12-12 07:37:48 -08003953 /* Force priority into range. */
Joel Fernandes (Google)c7cd1612018-06-19 15:14:17 -07003954 if (IS_ENABLED(CONFIG_RCU_BOOST) && kthread_prio < 2
3955 && IS_BUILTIN(CONFIG_RCU_TORTURE_TEST))
3956 kthread_prio = 2;
3957 else if (IS_ENABLED(CONFIG_RCU_BOOST) && kthread_prio < 1)
Paul E. McKenneya94844b2014-12-12 07:37:48 -08003958 kthread_prio = 1;
3959 else if (kthread_prio < 0)
3960 kthread_prio = 0;
3961 else if (kthread_prio > 99)
3962 kthread_prio = 99;
Joel Fernandes (Google)c7cd1612018-06-19 15:14:17 -07003963
Paul E. McKenneya94844b2014-12-12 07:37:48 -08003964 if (kthread_prio != kthread_prio_in)
3965 pr_alert("rcu_spawn_gp_kthread(): Limited prio to %d from %d\n",
3966 kthread_prio, kthread_prio_in);
3967
Paul E. McKenney9386c0b2014-07-13 12:00:53 -07003968 rcu_scheduler_fully_active = 1;
Paul E. McKenneyb97d23c2018-07-04 15:35:00 -07003969 t = kthread_create(rcu_gp_kthread, NULL, "%s", rcu_state.name);
Paul E. McKenney08543bd2018-10-22 08:04:03 -07003970 if (WARN_ONCE(IS_ERR(t), "%s: Could not start grace-period kthread, OOM is now expected behavior\n", __func__))
3971 return 0;
Paul E. McKenneyb97d23c2018-07-04 15:35:00 -07003972 if (kthread_prio) {
3973 sp.sched_priority = kthread_prio;
3974 sched_setscheduler_nocheck(t, SCHED_FIFO, &sp);
Paul E. McKenneyb3dbec72012-06-18 18:36:08 -07003975 }
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01003976 rnp = rcu_get_root();
Lai Jiangshan0c340292010-03-28 11:12:30 +08003977 raw_spin_lock_irqsave_rcu_node(rnp, flags);
Paul E. McKenney5648d652020-01-21 12:30:22 -08003978 WRITE_ONCE(rcu_state.gp_activity, jiffies);
3979 WRITE_ONCE(rcu_state.gp_req_activity, jiffies);
3980 // Reset .gp_activity and .gp_req_activity before setting .gp_kthread.
3981 smp_store_release(&rcu_state.gp_kthread, t); /* ^^^ */
Paul E. McKenneyb97d23c2018-07-04 15:35:00 -07003982 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
3983 wake_up_process(t);
Lai Jiangshan0c340292010-03-28 11:12:30 +08003984 rcu_spawn_nocb_kthreads();
Paul E. McKenney4a90a062010-04-14 16:48:11 -07003985 rcu_spawn_boost_kthreads();
Lai Jiangshan0c340292010-03-28 11:12:30 +08003986 return 0;
Lai Jiangshan394f99a2010-06-28 16:25:04 +08003987}
Lai Jiangshan0c340292010-03-28 11:12:30 +08003988early_initcall(rcu_spawn_gp_kthread);
3989
Paul E. McKenney6ce75a22012-06-12 11:01:13 -07003990/*
Paul E. McKenney52d7e482017-01-10 02:28:26 -08003991 * This function is invoked towards the end of the scheduler's
3992 * initialization process. Before this is called, the idle task might
3993 * contain synchronous grace-period primitives (during which time, this idle
3994 * task is booting the system, and such primitives are no-ops). After this
3995 * function is called, any synchronous grace-period primitives are run as
3996 * expedited, with the requesting task driving the grace period forward.
Paul E. McKenney900b1022017-02-10 14:32:54 -08003997 * A later core_initcall() rcu_set_runtime_mode() will switch to full
Paul E. McKenney52d7e482017-01-10 02:28:26 -08003998 * runtime RCU functionality.
Paul E. McKenneyf885b7f2012-04-23 15:52:53 -07003999 */
4000void rcu_scheduler_starting(void)
4001{
4002 WARN_ON(num_online_cpus() != 1);
4003 WARN_ON(nr_context_switches() > 0);
Paul E. McKenney52d7e482017-01-10 02:28:26 -08004004 rcu_test_sync_prims();
4005 rcu_scheduler_active = RCU_SCHEDULER_INIT;
4006 rcu_test_sync_prims();
Paul E. McKenneyf885b7f2012-04-23 15:52:53 -07004007}
4008
4009/*
Paul E. McKenney49918a52018-07-07 18:12:26 -07004010 * Helper function for rcu_init() that initializes the rcu_state structure.
Paul E. McKenney4102ada2013-10-08 20:23:47 -07004011 */
Paul E. McKenneyb8bb1f62018-07-03 17:22:34 -07004012static void __init rcu_init_one(void)
Paul E. McKenneyf885b7f2012-04-23 15:52:53 -07004013{
Alexander Gordeevcb007102015-06-03 08:18:30 +02004014 static const char * const buf[] = RCU_NODE_NAME_INIT;
4015 static const char * const fqs[] = RCU_FQS_NAME_INIT;
Paul E. McKenney3dc5dbe2015-09-26 14:51:24 -07004016 static struct lock_class_key rcu_node_class[RCU_NUM_LVLS];
4017 static struct lock_class_key rcu_fqs_class[RCU_NUM_LVLS];
Alexander Gordeev199977b2015-06-03 08:18:29 +02004018
Alexander Gordeev199977b2015-06-03 08:18:29 +02004019 int levelspread[RCU_NUM_LVLS]; /* kids/node in each level. */
Paul E. McKenney026ad282013-04-03 22:14:11 -07004020 int cpustride = 1;
4021 int i;
4022 int j;
4023 struct rcu_node *rnp;
4024
Alexander Gordeev05b84ae2015-06-03 08:18:28 +02004025 BUILD_BUG_ON(RCU_NUM_LVLS > ARRAY_SIZE(buf)); /* Fix buf[] init! */
Paul E. McKenney026ad282013-04-03 22:14:11 -07004026
Paul E. McKenney3eaaaf6c2015-03-09 16:51:17 -07004027 /* Silence gcc 4.8 false positive about array index out of range. */
4028 if (rcu_num_lvls <= 0 || rcu_num_lvls > RCU_NUM_LVLS)
4029 panic("rcu_init_one: rcu_num_lvls out of range");
Paul E. McKenney026ad282013-04-03 22:14:11 -07004030
Paul E. McKenneyf885b7f2012-04-23 15:52:53 -07004031 /* Initialize the level-tracking arrays. */
Paul E. McKenneyb17c7032012-09-06 15:38:02 -07004032
Paul E. McKenney39479092013-10-09 15:20:33 -07004033 for (i = 1; i < rcu_num_lvls; i++)
Paul E. McKenneyeb7a6652018-07-05 17:47:45 -07004034 rcu_state.level[i] =
4035 rcu_state.level[i - 1] + num_rcu_lvl[i - 1];
Paul E. McKenney41f5c632017-03-15 12:59:17 -07004036 rcu_init_levelspread(levelspread, num_rcu_lvl);
Paul E. McKenneyf885b7f2012-04-23 15:52:53 -07004037
Paul E. McKenney9f680ab2009-11-22 08:53:49 -08004038 /* Initialize the elements themselves, starting from the leaves. */
Paul E. McKenneyf41d9112009-08-22 13:56:52 -07004039
Paul E. McKenney017c4262010-01-14 16:10:58 -08004040 for (i = rcu_num_lvls - 1; i >= 0; i--) {
Alexander Gordeev199977b2015-06-03 08:18:29 +02004041 cpustride *= levelspread[i];
Paul E. McKenneyeb7a6652018-07-05 17:47:45 -07004042 rnp = rcu_state.level[i];
Paul E. McKenney41f5c632017-03-15 12:59:17 -07004043 for (j = 0; j < num_rcu_lvl[i]; j++, rnp++) {
Boqun Feng67c583a72015-12-29 12:18:47 +08004044 raw_spin_lock_init(&ACCESS_PRIVATE(rnp, lock));
4045 lockdep_set_class_and_name(&ACCESS_PRIVATE(rnp, lock),
Paul E. McKenneyf41d9112009-08-22 13:56:52 -07004046 &rcu_node_class[i], buf[i]);
Jiang Fangb5b39362013-02-02 14:13:42 -08004047 raw_spin_lock_init(&rnp->fqslock);
Paul E. McKenney9f680ab2009-11-22 08:53:49 -08004048 lockdep_set_class_and_name(&rnp->fqslock,
4049 &rcu_fqs_class[i], fqs[i]);
Paul E. McKenneyeb7a6652018-07-05 17:47:45 -07004050 rnp->gp_seq = rcu_state.gp_seq;
4051 rnp->gp_seq_needed = rcu_state.gp_seq;
4052 rnp->completedqs = rcu_state.gp_seq;
Paul E. McKenney9f680ab2009-11-22 08:53:49 -08004053 rnp->qsmask = 0;
4054 rnp->qsmaskinit = 0;
4055 rnp->grplo = j * cpustride;
Borislav Petkovd1d74d12013-04-22 00:12:42 +02004056 rnp->grphi = (j + 1) * cpustride - 1;
Paul E. McKenney017c4262010-01-14 16:10:58 -08004057 if (rnp->grphi >= nr_cpu_ids)
4058 rnp->grphi = nr_cpu_ids - 1;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01004059 if (i == 0) {
4060 rnp->grpnum = 0;
Paul E. McKenney4102ada2013-10-08 20:23:47 -07004061 rnp->grpmask = 0;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01004062 rnp->parent = NULL;
4063 } else {
Alexander Gordeev199977b2015-06-03 08:18:29 +02004064 rnp->grpnum = j % levelspread[i - 1];
Paul E. McKenneydf63fa5b2018-07-31 09:49:20 -07004065 rnp->grpmask = BIT(rnp->grpnum);
Paul E. McKenneyeb7a6652018-07-05 17:47:45 -07004066 rnp->parent = rcu_state.level[i - 1] +
Alexander Gordeev199977b2015-06-03 08:18:29 +02004067 j / levelspread[i - 1];
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01004068 }
4069 rnp->level = i;
4070 INIT_LIST_HEAD(&rnp->blkd_tasks);
4071 rcu_init_one_nocb(rnp);
Paul E. McKenneyf6a12f32016-01-30 17:57:35 -08004072 init_waitqueue_head(&rnp->exp_wq[0]);
4073 init_waitqueue_head(&rnp->exp_wq[1]);
Paul E. McKenney3b5f6682016-03-16 16:47:55 -07004074 init_waitqueue_head(&rnp->exp_wq[2]);
4075 init_waitqueue_head(&rnp->exp_wq[3]);
Paul E. McKenneyf6a12f32016-01-30 17:57:35 -08004076 spin_lock_init(&rnp->exp_lock);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01004077 }
4078 }
4079
Paul E. McKenneyeb7a6652018-07-05 17:47:45 -07004080 init_swait_queue_head(&rcu_state.gp_wq);
4081 init_swait_queue_head(&rcu_state.expedited_wq);
Paul E. McKenneyaedf4ba2018-07-04 14:33:59 -07004082 rnp = rcu_first_leaf_node();
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01004083 for_each_possible_cpu(i) {
4084 while (i > rnp->grphi)
4085 rnp++;
Paul E. McKenneyda1df502018-07-03 15:37:16 -07004086 per_cpu_ptr(&rcu_data, i)->mynode = rnp;
Paul E. McKenney53b46302018-07-03 17:22:34 -07004087 rcu_boot_init_percpu_data(i);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01004088 }
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01004089}
4090
4091/*
4092 * Compute the rcu_node tree geometry from kernel parameters. This cannot
4093 * replace the definitions in tree.h because those are needed to size
4094 * the ->node array in the rcu_state structure.
4095 */
4096static void __init rcu_init_geometry(void)
4097{
4098 ulong d;
4099 int i;
Alexander Gordeev05b84ae2015-06-03 08:18:28 +02004100 int rcu_capacity[RCU_NUM_LVLS];
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01004101
4102 /*
4103 * Initialize any unspecified boot parameters.
4104 * The default values of jiffies_till_first_fqs and
4105 * jiffies_till_next_fqs are set to the RCU_JIFFIES_TILL_FORCE_QS
4106 * value, which is a function of HZ, then adding one for each
4107 * RCU_JIFFIES_FQS_DIV CPUs that might be on the system.
4108 */
4109 d = RCU_JIFFIES_TILL_FORCE_QS + nr_cpu_ids / RCU_JIFFIES_FQS_DIV;
4110 if (jiffies_till_first_fqs == ULONG_MAX)
4111 jiffies_till_first_fqs = d;
4112 if (jiffies_till_next_fqs == ULONG_MAX)
4113 jiffies_till_next_fqs = d;
Neeraj Upadhyay69730322019-03-11 15:16:11 +05304114 adjust_jiffies_till_sched_qs();
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01004115
4116 /* If the compile-time values are accurate, just leave. */
Paul E. McKenney47d631a2015-04-21 09:12:13 -07004117 if (rcu_fanout_leaf == RCU_FANOUT_LEAF &&
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01004118 nr_cpu_ids == NR_CPUS)
4119 return;
Joe Perchesa7538352018-05-14 13:27:33 -07004120 pr_info("Adjusting geometry for rcu_fanout_leaf=%d, nr_cpu_ids=%u\n",
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01004121 rcu_fanout_leaf, nr_cpu_ids);
4122
4123 /*
Paul E. McKenneyee968ac2015-07-31 08:28:35 -07004124 * The boot-time rcu_fanout_leaf parameter must be at least two
4125 * and cannot exceed the number of bits in the rcu_node masks.
4126 * Complain and fall back to the compile-time values if this
4127 * limit is exceeded.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01004128 */
Paul E. McKenneyee968ac2015-07-31 08:28:35 -07004129 if (rcu_fanout_leaf < 2 ||
Alexander Gordeev75cf15a2015-06-03 08:18:23 +02004130 rcu_fanout_leaf > sizeof(unsigned long) * 8) {
Paul E. McKenney13bd64942015-06-04 10:06:01 -07004131 rcu_fanout_leaf = RCU_FANOUT_LEAF;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01004132 WARN_ON(1);
4133 return;
4134 }
4135
Alexander Gordeev75cf15a2015-06-03 08:18:23 +02004136 /*
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01004137 * Compute number of nodes that can be handled an rcu_node tree
Alexander Gordeev96181382015-06-03 08:18:26 +02004138 * with the given number of levels.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01004139 */
Alexander Gordeev96181382015-06-03 08:18:26 +02004140 rcu_capacity[0] = rcu_fanout_leaf;
Alexander Gordeev05b84ae2015-06-03 08:18:28 +02004141 for (i = 1; i < RCU_NUM_LVLS; i++)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01004142 rcu_capacity[i] = rcu_capacity[i - 1] * RCU_FANOUT;
4143
4144 /*
Alexander Gordeev75cf15a2015-06-03 08:18:23 +02004145 * The tree must be able to accommodate the configured number of CPUs.
Paul E. McKenneyee968ac2015-07-31 08:28:35 -07004146 * If this limit is exceeded, fall back to the compile-time values.
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01004147 */
Paul E. McKenneyee968ac2015-07-31 08:28:35 -07004148 if (nr_cpu_ids > rcu_capacity[RCU_NUM_LVLS - 1]) {
4149 rcu_fanout_leaf = RCU_FANOUT_LEAF;
4150 WARN_ON(1);
4151 return;
4152 }
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01004153
Alexander Gordeev679f9852015-06-03 08:18:25 +02004154 /* Calculate the number of levels in the tree. */
Alexander Gordeev96181382015-06-03 08:18:26 +02004155 for (i = 0; nr_cpu_ids > rcu_capacity[i]; i++) {
Alexander Gordeev679f9852015-06-03 08:18:25 +02004156 }
Alexander Gordeev96181382015-06-03 08:18:26 +02004157 rcu_num_lvls = i + 1;
Alexander Gordeev679f9852015-06-03 08:18:25 +02004158
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01004159 /* Calculate the number of rcu_nodes at each level of the tree. */
Alexander Gordeev679f9852015-06-03 08:18:25 +02004160 for (i = 0; i < rcu_num_lvls; i++) {
Alexander Gordeev96181382015-06-03 08:18:26 +02004161 int cap = rcu_capacity[(rcu_num_lvls - 1) - i];
Alexander Gordeev679f9852015-06-03 08:18:25 +02004162 num_rcu_lvl[i] = DIV_ROUND_UP(nr_cpu_ids, cap);
4163 }
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01004164
4165 /* Calculate the total number of rcu_node structures. */
4166 rcu_num_nodes = 0;
Alexander Gordeev679f9852015-06-03 08:18:25 +02004167 for (i = 0; i < rcu_num_lvls; i++)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01004168 rcu_num_nodes += num_rcu_lvl[i];
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01004169}
4170
Paul E. McKenneya3dc2942015-04-20 11:40:50 -07004171/*
4172 * Dump out the structure of the rcu_node combining tree associated
Paul E. McKenney49918a52018-07-07 18:12:26 -07004173 * with the rcu_state structure.
Paul E. McKenneya3dc2942015-04-20 11:40:50 -07004174 */
Paul E. McKenneyb8bb1f62018-07-03 17:22:34 -07004175static void __init rcu_dump_rcu_node_tree(void)
Paul E. McKenneya3dc2942015-04-20 11:40:50 -07004176{
4177 int level = 0;
4178 struct rcu_node *rnp;
4179
4180 pr_info("rcu_node tree layout dump\n");
4181 pr_info(" ");
Paul E. McKenneyaedf4ba2018-07-04 14:33:59 -07004182 rcu_for_each_node_breadth_first(rnp) {
Paul E. McKenneya3dc2942015-04-20 11:40:50 -07004183 if (rnp->level != level) {
4184 pr_cont("\n");
4185 pr_info(" ");
4186 level = rnp->level;
4187 }
4188 pr_cont("%d:%d ^%d ", rnp->grplo, rnp->grphi, rnp->grpnum);
4189 }
4190 pr_cont("\n");
4191}
4192
Paul E. McKenneyad7c9462018-01-08 14:35:52 -08004193struct workqueue_struct *rcu_gp_wq;
Paul E. McKenney25f3d7e2018-02-01 22:05:38 -08004194struct workqueue_struct *rcu_par_gp_wq;
Paul E. McKenneyad7c9462018-01-08 14:35:52 -08004195
Byungchul Parka35d1692019-08-05 18:22:27 -04004196static void __init kfree_rcu_batch_init(void)
4197{
4198 int cpu;
Joel Fernandes (Google)0392beb2019-09-19 14:58:26 -07004199 int i;
Byungchul Parka35d1692019-08-05 18:22:27 -04004200
4201 for_each_possible_cpu(cpu) {
4202 struct kfree_rcu_cpu *krcp = per_cpu_ptr(&krc, cpu);
4203
4204 spin_lock_init(&krcp->lock);
Uladzislau Rezki (Sony)34c88172020-01-20 15:42:25 +01004205 for (i = 0; i < KFREE_N_BATCHES; i++) {
4206 INIT_RCU_WORK(&krcp->krw_arr[i].rcu_work, kfree_rcu_work);
Joel Fernandes (Google)0392beb2019-09-19 14:58:26 -07004207 krcp->krw_arr[i].krcp = krcp;
Uladzislau Rezki (Sony)34c88172020-01-20 15:42:25 +01004208 }
4209
Byungchul Parka35d1692019-08-05 18:22:27 -04004210 INIT_DELAYED_WORK(&krcp->monitor_work, kfree_rcu_monitor);
4211 krcp->initialized = true;
4212 }
Joel Fernandes (Google)91542442020-03-16 12:32:27 -04004213 if (register_shrinker(&kfree_rcu_shrinker))
4214 pr_err("Failed to register kfree_rcu() shrinker!\n");
Byungchul Parka35d1692019-08-05 18:22:27 -04004215}
4216
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01004217void __init rcu_init(void)
4218{
4219 int cpu;
4220
Paul E. McKenney47627672015-01-19 21:10:21 -08004221 rcu_early_boot_tests();
4222
Byungchul Parka35d1692019-08-05 18:22:27 -04004223 kfree_rcu_batch_init();
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01004224 rcu_bootup_announce();
4225 rcu_init_geometry();
Paul E. McKenneyb8bb1f62018-07-03 17:22:34 -07004226 rcu_init_one();
Paul E. McKenneya3dc2942015-04-20 11:40:50 -07004227 if (dump_tree)
Paul E. McKenneyb8bb1f62018-07-03 17:22:34 -07004228 rcu_dump_rcu_node_tree();
Sebastian Andrzej Siewior48d07c02019-03-20 22:13:33 +01004229 if (use_softirq)
4230 open_softirq(RCU_SOFTIRQ, rcu_core_si);
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01004231
4232 /*
4233 * We don't need protection against CPU-hotplug here because
4234 * this is called early in boot, before either interrupts
4235 * or the scheduler are operational.
4236 */
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01004237 pm_notifier(rcu_pm_notify, 0);
Paul E. McKenney7ec99de2016-06-30 13:58:26 -07004238 for_each_online_cpu(cpu) {
Thomas Gleixner4df83742016-07-13 17:17:03 +00004239 rcutree_prepare_cpu(cpu);
Paul E. McKenney7ec99de2016-06-30 13:58:26 -07004240 rcu_cpu_starting(cpu);
Paul E. McKenney9b9500d2017-08-17 17:05:59 -07004241 rcutree_online_cpu(cpu);
Paul E. McKenney7ec99de2016-06-30 13:58:26 -07004242 }
Paul E. McKenneyad7c9462018-01-08 14:35:52 -08004243
4244 /* Create workqueue for expedited GPs and for Tree SRCU. */
4245 rcu_gp_wq = alloc_workqueue("rcu_gp", WQ_MEM_RECLAIM, 0);
4246 WARN_ON(!rcu_gp_wq);
Paul E. McKenney25f3d7e2018-02-01 22:05:38 -08004247 rcu_par_gp_wq = alloc_workqueue("rcu_par_gp", WQ_MEM_RECLAIM, 0);
4248 WARN_ON(!rcu_par_gp_wq);
Paul E. McKenneye0fcba92018-08-14 08:45:54 -07004249 srcu_init();
Paul E. McKenneyb2b00dd2019-10-30 11:56:10 -07004250
4251 /* Fill in default value for rcutree.qovld boot parameter. */
4252 /* -After- the rcu_node ->lock fields are initialized! */
4253 if (qovld < 0)
4254 qovld_calc = DEFAULT_RCU_QOVLD_MULT * qhimark;
4255 else
4256 qovld_calc = qovld;
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01004257}
4258
Paul E. McKenney10462d62019-01-11 16:10:57 -08004259#include "tree_stall.h"
Paul E. McKenney3549c2b2016-04-15 16:35:29 -07004260#include "tree_exp.h"
Paul E. McKenney64db4cf2008-12-18 21:55:32 +01004261#include "tree_plugin.h"