3382 lines · 107.9 KB · C
RawDownload
// SPDX-License-Identifier: GPL-2.0+/* * Read-Copy Update mechanism for mutual exclusion * * Copyright IBM Corporation, 2008 * * Authors: Dipankar Sarma <dipankar@in.ibm.com> *	    Manfred Spraul <manfred@colorfullife.com> *	    Paul E. McKenney <paulmck@linux.ibm.com> Hierarchical version * * Based on the original work by Paul McKenney <paulmck@linux.ibm.com> * and inputs from Rusty Russell, Andrea Arcangeli and Andi Kleen. * * For detailed explanation of Read-Copy Update mechanism see - *	Documentation/RCU */ #define pr_fmt(fmt) "rcu: " fmt #include <linux/types.h>#include <linux/kernel.h>#include <linux/init.h>#include <linux/spinlock.h>#include <linux/smp.h>#include <linux/rcupdate_wait.h>#include <linux/interrupt.h>#include <linux/sched.h>#include <linux/sched/debug.h>#include <linux/nmi.h>#include <linux/atomic.h>#include <linux/bitops.h>#include <linux/export.h>#include <linux/completion.h>#include <linux/moduleparam.h>#include <linux/percpu.h>#include <linux/notifier.h>#include <linux/cpu.h>#include <linux/mutex.h>#include <linux/time.h>#include <linux/kernel_stat.h>#include <linux/wait.h>#include <linux/kthread.h>#include <uapi/linux/sched/types.h>#include <linux/prefetch.h>#include <linux/delay.h>#include <linux/stop_machine.h>#include <linux/random.h>#include <linux/trace_events.h>#include <linux/suspend.h>#include <linux/ftrace.h>#include <linux/tick.h>#include <linux/sysrq.h>#include <linux/kprobes.h> #include "tree.h"#include "rcu.h" #ifdef MODULE_PARAM_PREFIX#undef MODULE_PARAM_PREFIX#endif#define MODULE_PARAM_PREFIX "rcutree." /* Data structures. */ /* * Steal a bit from the bottom of ->dynticks for idle entry/exit * control.  Initially this is for TLB flushing. */#define RCU_DYNTICK_CTRL_MASK 0x1#define RCU_DYNTICK_CTRL_CTR  (RCU_DYNTICK_CTRL_MASK + 1)#ifndef rcu_eqs_special_exit#define rcu_eqs_special_exit() do { } while (0)#endif static DEFINE_PER_CPU_SHARED_ALIGNED(struct rcu_data, rcu_data) = {	.dynticks_nesting = 1,	.dynticks_nmi_nesting = DYNTICK_IRQ_NONIDLE,	.dynticks = ATOMIC_INIT(RCU_DYNTICK_CTRL_CTR),};struct rcu_state rcu_state = {	.level = { &rcu_state.node[0] },	.gp_state = RCU_GP_IDLE,	.gp_seq = (0UL - 300UL) << RCU_SEQ_CTR_SHIFT,	.barrier_mutex = __MUTEX_INITIALIZER(rcu_state.barrier_mutex),	.name = RCU_NAME,	.abbr = RCU_ABBR,	.exp_mutex = __MUTEX_INITIALIZER(rcu_state.exp_mutex),	.exp_wake_mutex = __MUTEX_INITIALIZER(rcu_state.exp_wake_mutex),	.ofl_lock = __RAW_SPIN_LOCK_UNLOCKED(rcu_state.ofl_lock),}; /* Dump rcu_node combining tree at boot to verify correct setup. */static bool dump_tree;module_param(dump_tree, bool, 0444);/* Control rcu_node-tree auto-balancing at boot time. */static bool rcu_fanout_exact;module_param(rcu_fanout_exact, bool, 0444);/* Increase (but not decrease) the RCU_FANOUT_LEAF at boot time. */static int rcu_fanout_leaf = RCU_FANOUT_LEAF;module_param(rcu_fanout_leaf, int, 0444);int rcu_num_lvls __read_mostly = RCU_NUM_LVLS;/* Number of rcu_nodes at specified level. */int num_rcu_lvl[] = NUM_RCU_LVL_INIT;int rcu_num_nodes __read_mostly = NUM_RCU_NODES; /* Total # rcu_nodes in use. */ /* * The rcu_scheduler_active variable is initialized to the value * RCU_SCHEDULER_INACTIVE and transitions RCU_SCHEDULER_INIT just before the * first task is spawned.  So when this variable is RCU_SCHEDULER_INACTIVE, * RCU can assume that there is but one task, allowing RCU to (for example) * optimize synchronize_rcu() to a simple barrier().  When this variable * is RCU_SCHEDULER_INIT, RCU must actually do all the hard work required * to detect real grace periods.  This variable is also used to suppress * boot-time false positives from lockdep-RCU error checking.  Finally, it * transitions from RCU_SCHEDULER_INIT to RCU_SCHEDULER_RUNNING after RCU * is fully initialized, including all of its kthreads having been spawned. */int rcu_scheduler_active __read_mostly;EXPORT_SYMBOL_GPL(rcu_scheduler_active); /* * The rcu_scheduler_fully_active variable transitions from zero to one * during the early_initcall() processing, which is after the scheduler * is capable of creating new tasks.  So RCU processing (for example, * creating tasks for RCU priority boosting) must be delayed until after * rcu_scheduler_fully_active transitions from zero to one.  We also * currently delay invocation of any RCU callbacks until after this point. * * It might later prove better for people registering RCU callbacks during * early boot to take responsibility for these callbacks, but one step at * a time. */static int rcu_scheduler_fully_active __read_mostly; static void rcu_report_qs_rnp(unsigned long mask, struct rcu_node *rnp,			      unsigned long gps, unsigned long flags);static void rcu_init_new_rnp(struct rcu_node *rnp_leaf);static void rcu_cleanup_dead_rnp(struct rcu_node *rnp_leaf);static void rcu_boost_kthread_setaffinity(struct rcu_node *rnp, int outgoingcpu);static void invoke_rcu_core(void);static void invoke_rcu_callbacks(struct rcu_data *rdp);static void rcu_report_exp_rdp(struct rcu_data *rdp);static void sync_sched_exp_online_cleanup(int cpu); /* rcuc/rcub kthread realtime priority */static int kthread_prio = IS_ENABLED(CONFIG_RCU_BOOST) ? 1 : 0;module_param(kthread_prio, int, 0444); /* Delay in jiffies for grace-period initialization delays, debug only. */ static int gp_preinit_delay;module_param(gp_preinit_delay, int, 0444);static int gp_init_delay;module_param(gp_init_delay, int, 0444);static int gp_cleanup_delay;module_param(gp_cleanup_delay, int, 0444); /* Retrieve RCU kthreads priority for rcutorture */int rcu_get_gp_kthreads_prio(void){	return kthread_prio;}EXPORT_SYMBOL_GPL(rcu_get_gp_kthreads_prio); /* * Number of grace periods between delays, normalized by the duration of * the delay.  The longer the delay, the more the grace periods between * each delay.  The reason for this normalization is that it means that, * for non-zero delays, the overall slowdown of grace periods is constant * regardless of the duration of the delay.  This arrangement balances * the need for long delays to increase some race probabilities with the * need for fast grace periods to increase other race probabilities. */#define PER_RCU_NODE_PERIOD 3	/* Number of grace periods between delays. */ /* * Compute the mask of online CPUs for the specified rcu_node structure. * This will not be stable unless the rcu_node structure's ->lock is * held, but the bit corresponding to the current CPU will be stable * in most contexts. */unsigned long rcu_rnp_online_cpus(struct rcu_node *rnp){	return READ_ONCE(rnp->qsmaskinitnext);} /* * Return true if an RCU grace period is in progress.  The READ_ONCE()s * permit this function to be invoked without holding the root rcu_node * structure's ->lock, but of course results can be subject to change. */static int rcu_gp_in_progress(void){	return rcu_seq_state(rcu_seq_current(&rcu_state.gp_seq));} /* * Return the number of callbacks queued on the specified CPU. * Handles both the nocbs and normal cases. */static long rcu_get_n_cbs_cpu(int cpu){	struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu); 	if (rcu_segcblist_is_enabled(&rdp->cblist)) /* Online normal CPU? */		return rcu_segcblist_n_cbs(&rdp->cblist);	return rcu_get_n_cbs_nocb_cpu(rdp); /* Works for offline, too. */} void rcu_softirq_qs(void){	rcu_qs();	rcu_preempt_deferred_qs(current);} /* * Record entry into an extended quiescent state.  This is only to be * called when not already in an extended quiescent state. */static void rcu_dynticks_eqs_enter(void){	struct rcu_data *rdp = this_cpu_ptr(&rcu_data);	int seq; 	/*	 * CPUs seeing atomic_add_return() must see prior RCU read-side	 * critical sections, and we also must force ordering with the	 * next idle sojourn.	 */	seq = atomic_add_return(RCU_DYNTICK_CTRL_CTR, &rdp->dynticks);	/* Better be in an extended quiescent state! */	WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) &&		     (seq & RCU_DYNTICK_CTRL_CTR));	/* Better not have special action (TLB flush) pending! */	WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) &&		     (seq & RCU_DYNTICK_CTRL_MASK));} /* * Record exit from an extended quiescent state.  This is only to be * called from an extended quiescent state. */static void rcu_dynticks_eqs_exit(void){	struct rcu_data *rdp = this_cpu_ptr(&rcu_data);	int seq; 	/*	 * CPUs seeing atomic_add_return() must see prior idle sojourns,	 * and we also must force ordering with the next RCU read-side	 * critical section.	 */	seq = atomic_add_return(RCU_DYNTICK_CTRL_CTR, &rdp->dynticks);	WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) &&		     !(seq & RCU_DYNTICK_CTRL_CTR));	if (seq & RCU_DYNTICK_CTRL_MASK) {		atomic_andnot(RCU_DYNTICK_CTRL_MASK, &rdp->dynticks);		smp_mb__after_atomic(); /* _exit after clearing mask. */		/* Prefer duplicate flushes to losing a flush. */		rcu_eqs_special_exit();	}} /* * Reset the current CPU's ->dynticks counter to indicate that the * newly onlined CPU is no longer in an extended quiescent state. * This will either leave the counter unchanged, or increment it * to the next non-quiescent value. * * The non-atomic test/increment sequence works because the upper bits * of the ->dynticks counter are manipulated only by the corresponding CPU, * or when the corresponding CPU is offline. */static void rcu_dynticks_eqs_online(void){	struct rcu_data *rdp = this_cpu_ptr(&rcu_data); 	if (atomic_read(&rdp->dynticks) & RCU_DYNTICK_CTRL_CTR)		return;	atomic_add(RCU_DYNTICK_CTRL_CTR, &rdp->dynticks);} /* * Is the current CPU in an extended quiescent state? * * No ordering, as we are sampling CPU-local information. */bool rcu_dynticks_curr_cpu_in_eqs(void){	struct rcu_data *rdp = this_cpu_ptr(&rcu_data); 	return !(atomic_read(&rdp->dynticks) & RCU_DYNTICK_CTRL_CTR);} /* * Snapshot the ->dynticks counter with full ordering so as to allow * stable comparison of this counter with past and future snapshots. */int rcu_dynticks_snap(struct rcu_data *rdp){	int snap = atomic_add_return(0, &rdp->dynticks); 	return snap & ~RCU_DYNTICK_CTRL_MASK;} /* * Return true if the snapshot returned from rcu_dynticks_snap() * indicates that RCU is in an extended quiescent state. */static bool rcu_dynticks_in_eqs(int snap){	return !(snap & RCU_DYNTICK_CTRL_CTR);} /* * Return true if the CPU corresponding to the specified rcu_data * structure has spent some time in an extended quiescent state since * rcu_dynticks_snap() returned the specified snapshot. */static bool rcu_dynticks_in_eqs_since(struct rcu_data *rdp, int snap){	return snap != rcu_dynticks_snap(rdp);} /* * Set the special (bottom) bit of the specified CPU so that it * will take special action (such as flushing its TLB) on the * next exit from an extended quiescent state.  Returns true if * the bit was successfully set, or false if the CPU was not in * an extended quiescent state. */bool rcu_eqs_special_set(int cpu){	int old;	int new;	struct rcu_data *rdp = &per_cpu(rcu_data, cpu); 	do {		old = atomic_read(&rdp->dynticks);		if (old & RCU_DYNTICK_CTRL_CTR)			return false;		new = old | RCU_DYNTICK_CTRL_MASK;	} while (atomic_cmpxchg(&rdp->dynticks, old, new) != old);	return true;} /* * Let the RCU core know that this CPU has gone through the scheduler, * which is a quiescent state.  This is called when the need for a * quiescent state is urgent, so we burn an atomic operation and full * memory barriers to let the RCU core know about it, regardless of what * this CPU might (or might not) do in the near future. * * We inform the RCU core by emulating a zero-duration dyntick-idle period. * * The caller must have disabled interrupts and must not be idle. */static void __maybe_unused rcu_momentary_dyntick_idle(void){	int special; 	raw_cpu_write(rcu_data.rcu_need_heavy_qs, false);	special = atomic_add_return(2 * RCU_DYNTICK_CTRL_CTR,				    &this_cpu_ptr(&rcu_data)->dynticks);	/* It is illegal to call this from idle state. */	WARN_ON_ONCE(!(special & RCU_DYNTICK_CTRL_CTR));	rcu_preempt_deferred_qs(current);} /** * rcu_is_cpu_rrupt_from_idle - see if idle or immediately interrupted from idle * * If the current CPU is idle or running at a first-level (not nested) * interrupt from idle, return true.  The caller must have at least * disabled preemption. */static int rcu_is_cpu_rrupt_from_idle(void){	return __this_cpu_read(rcu_data.dynticks_nesting) <= 0 &&	       __this_cpu_read(rcu_data.dynticks_nmi_nesting) <= 1;} #define DEFAULT_RCU_BLIMIT 10     /* Maximum callbacks per rcu_do_batch. */static long blimit = DEFAULT_RCU_BLIMIT;#define DEFAULT_RCU_QHIMARK 10000 /* If this many pending, ignore blimit. */static long qhimark = DEFAULT_RCU_QHIMARK;#define DEFAULT_RCU_QLOMARK 100   /* Once only this many pending, use blimit. */static long qlowmark = DEFAULT_RCU_QLOMARK; module_param(blimit, long, 0444);module_param(qhimark, long, 0444);module_param(qlowmark, long, 0444); static ulong jiffies_till_first_fqs = ULONG_MAX;static ulong jiffies_till_next_fqs = ULONG_MAX;static bool rcu_kick_kthreads; /* * How long the grace period must be before we start recruiting * quiescent-state help from rcu_note_context_switch(). */static ulong jiffies_till_sched_qs = ULONG_MAX;module_param(jiffies_till_sched_qs, ulong, 0444);static ulong jiffies_to_sched_qs; /* See adjust_jiffies_till_sched_qs(). */module_param(jiffies_to_sched_qs, ulong, 0444); /* Display only! */ /* * Make sure that we give the grace-period kthread time to detect any * idle CPUs before taking active measures to force quiescent states. * However, don't go below 100 milliseconds, adjusted upwards for really * large systems. */static void adjust_jiffies_till_sched_qs(void){	unsigned long j; 	/* If jiffies_till_sched_qs was specified, respect the request. */	if (jiffies_till_sched_qs != ULONG_MAX) {		WRITE_ONCE(jiffies_to_sched_qs, jiffies_till_sched_qs);		return;	}	/* Otherwise, set to third fqs scan, but bound below on large system. */	j = READ_ONCE(jiffies_till_first_fqs) +		      2 * READ_ONCE(jiffies_till_next_fqs);	if (j < HZ / 10 + nr_cpu_ids / RCU_JIFFIES_FQS_DIV)		j = HZ / 10 + nr_cpu_ids / RCU_JIFFIES_FQS_DIV;	pr_info("RCU calculated value of scheduler-enlistment delay is %ld jiffies.\n", j);	WRITE_ONCE(jiffies_to_sched_qs, j);} static int param_set_first_fqs_jiffies(const char *val, const struct kernel_param *kp){	ulong j;	int ret = kstrtoul(val, 0, &j); 	if (!ret) {		WRITE_ONCE(*(ulong *)kp->arg, (j > HZ) ? HZ : j);		adjust_jiffies_till_sched_qs();	}	return ret;} static int param_set_next_fqs_jiffies(const char *val, const struct kernel_param *kp){	ulong j;	int ret = kstrtoul(val, 0, &j); 	if (!ret) {		WRITE_ONCE(*(ulong *)kp->arg, (j > HZ) ? HZ : (j ?: 1));		adjust_jiffies_till_sched_qs();	}	return ret;} static struct kernel_param_ops first_fqs_jiffies_ops = {	.set = param_set_first_fqs_jiffies,	.get = param_get_ulong,}; static struct kernel_param_ops next_fqs_jiffies_ops = {	.set = param_set_next_fqs_jiffies,	.get = param_get_ulong,}; module_param_cb(jiffies_till_first_fqs, &first_fqs_jiffies_ops, &jiffies_till_first_fqs, 0644);module_param_cb(jiffies_till_next_fqs, &next_fqs_jiffies_ops, &jiffies_till_next_fqs, 0644);module_param(rcu_kick_kthreads, bool, 0644); static void force_qs_rnp(int (*f)(struct rcu_data *rdp));static int rcu_pending(void); /* * Return the number of RCU GPs completed thus far for debug & stats. */unsigned long rcu_get_gp_seq(void){	return READ_ONCE(rcu_state.gp_seq);}EXPORT_SYMBOL_GPL(rcu_get_gp_seq); /* * Return the number of RCU expedited batches completed thus far for * debug & stats.  Odd numbers mean that a batch is in progress, even * numbers mean idle.  The value returned will thus be roughly double * the cumulative batches since boot. */unsigned long rcu_exp_batches_completed(void){	return rcu_state.expedited_sequence;}EXPORT_SYMBOL_GPL(rcu_exp_batches_completed); /* * Return the root node of the rcu_state structure. */static struct rcu_node *rcu_get_root(void){	return &rcu_state.node[0];} 

Lines 1–500 of 3382.