xref: /linux/kernel/rcu/tree.h (revision 02e3024175274ed4bf7912e7a1281b300cec76b5)
122e40925SPaul E. McKenney /* SPDX-License-Identifier: GPL-2.0+ */
24102adabSPaul E. McKenney /*
34102adabSPaul E. McKenney  * Read-Copy Update mechanism for mutual exclusion (tree-based version)
44102adabSPaul E. McKenney  * Internal non-public definitions.
54102adabSPaul E. McKenney  *
64102adabSPaul E. McKenney  * Copyright IBM Corporation, 2008
74102adabSPaul E. McKenney  *
84102adabSPaul E. McKenney  * Author: Ingo Molnar <mingo@elte.hu>
922e40925SPaul E. McKenney  *	   Paul E. McKenney <paulmck@linux.ibm.com>
104102adabSPaul E. McKenney  */
114102adabSPaul E. McKenney 
124102adabSPaul E. McKenney #include <linux/cache.h>
134102adabSPaul E. McKenney #include <linux/spinlock.h>
14037741a6SIngo Molnar #include <linux/rtmutex.h>
154102adabSPaul E. McKenney #include <linux/threads.h>
164102adabSPaul E. McKenney #include <linux/cpumask.h>
174102adabSPaul E. McKenney #include <linux/seqlock.h>
18abedf8e2SPaul Gortmaker #include <linux/swait.h>
19f2425b4eSPaul E. McKenney #include <linux/rcu_node_tree.h>
204102adabSPaul E. McKenney 
2145753c5fSIngo Molnar #include "rcu_segcblist.h"
2245753c5fSIngo Molnar 
2325f3d7efSPaul E. McKenney /* Communicate arguments to a workqueue handler. */
2425f3d7efSPaul E. McKenney struct rcu_exp_work {
2525f3d7efSPaul E. McKenney 	unsigned long rew_s;
2625f3d7efSPaul E. McKenney 	struct work_struct rew_work;
2725f3d7efSPaul E. McKenney };
2825f3d7efSPaul E. McKenney 
294102adabSPaul E. McKenney /* RCU's kthread states for tracing. */
304102adabSPaul E. McKenney #define RCU_KTHREAD_STOPPED  0
314102adabSPaul E. McKenney #define RCU_KTHREAD_RUNNING  1
324102adabSPaul E. McKenney #define RCU_KTHREAD_WAITING  2
334102adabSPaul E. McKenney #define RCU_KTHREAD_OFFCPU   3
344102adabSPaul E. McKenney #define RCU_KTHREAD_YIELDING 4
354102adabSPaul E. McKenney #define RCU_KTHREAD_MAX      4
364102adabSPaul E. McKenney 
374102adabSPaul E. McKenney /*
384102adabSPaul E. McKenney  * Definition for node within the RCU grace-period-detection hierarchy.
394102adabSPaul E. McKenney  */
404102adabSPaul E. McKenney struct rcu_node {
4167c583a7SBoqun Feng 	raw_spinlock_t __private lock;	/* Root rcu_node's lock protects */
4267c583a7SBoqun Feng 					/*  some rcu_state fields as well as */
4367c583a7SBoqun Feng 					/*  following. */
44360fbbb4SLihao Liang 	unsigned long gp_seq;	/* Track rsp->gp_seq. */
45adbccddbSJoel Fernandes (Google) 	unsigned long gp_seq_needed; /* Track furthest future GP request. */
464bc8d555SPaul E. McKenney 	unsigned long completedqs; /* All QSes done for this node. */
474102adabSPaul E. McKenney 	unsigned long qsmask;	/* CPUs or groups that need to switch in */
484102adabSPaul E. McKenney 				/*  order for current grace period to proceed.*/
494102adabSPaul E. McKenney 				/*  In leaf rcu_node, each bit corresponds to */
504102adabSPaul E. McKenney 				/*  an rcu_data structure, otherwise, each */
514102adabSPaul E. McKenney 				/*  bit corresponds to a child rcu_node */
524102adabSPaul E. McKenney 				/*  structure. */
53f2e2df59SPaul E. McKenney 	unsigned long rcu_gp_init_mask;	/* Mask of offline CPUs at GP init. */
544102adabSPaul E. McKenney 	unsigned long qsmaskinit;
55b9585e94SPaul E. McKenney 				/* Per-GP initial value for qsmask. */
560aa04b05SPaul E. McKenney 				/*  Initialized from ->qsmaskinitnext at the */
570aa04b05SPaul E. McKenney 				/*  beginning of each grace period. */
580aa04b05SPaul E. McKenney 	unsigned long qsmaskinitnext;
594d60b475SPaul E. McKenney 	unsigned long ofl_seq;	/* CPU-hotplug operation sequence count. */
600aa04b05SPaul E. McKenney 				/* Online CPUs for next grace period. */
61b9585e94SPaul E. McKenney 	unsigned long expmask;	/* CPUs or groups that need to check in */
62b9585e94SPaul E. McKenney 				/*  to allow the current expedited GP */
63b9585e94SPaul E. McKenney 				/*  to complete. */
64b9585e94SPaul E. McKenney 	unsigned long expmaskinit;
65b9585e94SPaul E. McKenney 				/* Per-GP initial values for expmask. */
66b9585e94SPaul E. McKenney 				/*  Initialized from ->expmaskinitnext at the */
67b9585e94SPaul E. McKenney 				/*  beginning of each expedited GP. */
68b9585e94SPaul E. McKenney 	unsigned long expmaskinitnext;
69b9585e94SPaul E. McKenney 				/* Online CPUs for next expedited GP. */
701de6e56dSPaul E. McKenney 				/*  Any CPU that has ever been online will */
711de6e56dSPaul E. McKenney 				/*  have its bit set. */
72b2b00ddfSPaul E. McKenney 	unsigned long cbovldmask;
73b2b00ddfSPaul E. McKenney 				/* CPUs experiencing callback overload. */
749b9500daSPaul E. McKenney 	unsigned long ffmask;	/* Fully functional CPUs. */
754102adabSPaul E. McKenney 	unsigned long grpmask;	/* Mask to apply to parent qsmask. */
764102adabSPaul E. McKenney 				/*  Only one bit will be set in this mask. */
77a2dae430SWei Yang 	int	grplo;		/* lowest-numbered CPU here. */
78a2dae430SWei Yang 	int	grphi;		/* highest-numbered CPU here. */
797a0c2b09SWei Yang 	u8	grpnum;		/* group number for next level up. */
804102adabSPaul E. McKenney 	u8	level;		/* root is at level 0. */
810aa04b05SPaul E. McKenney 	bool	wait_blkd_tasks;/* Necessary to wait for blocked tasks to */
820aa04b05SPaul E. McKenney 				/*  exit RCU read-side critical sections */
830aa04b05SPaul E. McKenney 				/*  before propagating offline up the */
840aa04b05SPaul E. McKenney 				/*  rcu_node tree? */
854102adabSPaul E. McKenney 	struct rcu_node *parent;
864102adabSPaul E. McKenney 	struct list_head blkd_tasks;
874102adabSPaul E. McKenney 				/* Tasks blocked in RCU read-side critical */
884102adabSPaul E. McKenney 				/*  section.  Tasks are placed at the head */
894102adabSPaul E. McKenney 				/*  of this list and age towards the tail. */
904102adabSPaul E. McKenney 	struct list_head *gp_tasks;
914102adabSPaul E. McKenney 				/* Pointer to the first task blocking the */
924102adabSPaul E. McKenney 				/*  current grace period, or NULL if there */
934102adabSPaul E. McKenney 				/*  is no such task. */
944102adabSPaul E. McKenney 	struct list_head *exp_tasks;
954102adabSPaul E. McKenney 				/* Pointer to the first task blocking the */
964102adabSPaul E. McKenney 				/*  current expedited grace period, or NULL */
974102adabSPaul E. McKenney 				/*  if there is no such task.  If there */
984102adabSPaul E. McKenney 				/*  is no current expedited grace period, */
994102adabSPaul E. McKenney 				/*  then there can cannot be any such task. */
1004102adabSPaul E. McKenney 	struct list_head *boost_tasks;
1014102adabSPaul E. McKenney 				/* Pointer to first task that needs to be */
1024102adabSPaul E. McKenney 				/*  priority boosted, or NULL if no priority */
1034102adabSPaul E. McKenney 				/*  boosting is needed for this rcu_node */
1044102adabSPaul E. McKenney 				/*  structure.  If there are no tasks */
1054102adabSPaul E. McKenney 				/*  queued on this rcu_node structure that */
1064102adabSPaul E. McKenney 				/*  are blocking the current grace period, */
1074102adabSPaul E. McKenney 				/*  there can be no such task. */
108abaa93d9SPaul E. McKenney 	struct rt_mutex boost_mtx;
109abaa93d9SPaul E. McKenney 				/* Used only for the priority-boosting */
110abaa93d9SPaul E. McKenney 				/*  side effect, not as a lock. */
1114102adabSPaul E. McKenney 	unsigned long boost_time;
1124102adabSPaul E. McKenney 				/* When to start boosting (jiffies). */
1134102adabSPaul E. McKenney 	struct task_struct *boost_kthread_task;
1144102adabSPaul E. McKenney 				/* kthread that takes care of priority */
1154102adabSPaul E. McKenney 				/*  boosting for this rcu_node structure. */
1164102adabSPaul E. McKenney 	unsigned int boost_kthread_status;
1174102adabSPaul E. McKenney 				/* State of boost_kthread_task for tracing. */
118396eba65SPaul E. McKenney 	unsigned long n_boosts;	/* Number of boosts for this rcu_node structure. */
1194102adabSPaul E. McKenney #ifdef CONFIG_RCU_NOCB_CPU
120abedf8e2SPaul Gortmaker 	struct swait_queue_head nocb_gp_wq[2];
1214102adabSPaul E. McKenney 				/* Place for rcu_nocb_kthread() to wait GP. */
1224102adabSPaul E. McKenney #endif /* #ifdef CONFIG_RCU_NOCB_CPU */
1234102adabSPaul E. McKenney 	raw_spinlock_t fqslock ____cacheline_internodealigned_in_smp;
124385b73c0SPaul E. McKenney 
125f6a12f34SPaul E. McKenney 	spinlock_t exp_lock ____cacheline_internodealigned_in_smp;
126f6a12f34SPaul E. McKenney 	unsigned long exp_seq_rq;
1273b5f668eSPaul E. McKenney 	wait_queue_head_t exp_wq[4];
12825f3d7efSPaul E. McKenney 	struct rcu_exp_work rew;
12925f3d7efSPaul E. McKenney 	bool exp_need_flush;	/* Need to flush workitem? */
1304102adabSPaul E. McKenney } ____cacheline_internodealigned_in_smp;
1314102adabSPaul E. McKenney 
1324102adabSPaul E. McKenney /*
133bc75e999SMark Rutland  * Bitmasks in an rcu_node cover the interval [grplo, grphi] of CPU IDs, and
134bc75e999SMark Rutland  * are indexed relative to this interval rather than the global CPU ID space.
135bc75e999SMark Rutland  * This generates the bit for a CPU in node-local masks.
136bc75e999SMark Rutland  */
137df63fa5bSPaul E. McKenney #define leaf_node_cpu_bit(rnp, cpu) (BIT((cpu) - (rnp)->grplo))
138bc75e999SMark Rutland 
139bc75e999SMark Rutland /*
1405b74c458SPaul E. McKenney  * Union to allow "aggregate OR" operation on the need for a quiescent
1415b74c458SPaul E. McKenney  * state by the normal and expedited grace periods.
1425b74c458SPaul E. McKenney  */
1435b74c458SPaul E. McKenney union rcu_noqs {
1445b74c458SPaul E. McKenney 	struct {
1455b74c458SPaul E. McKenney 		u8 norm;
1465b74c458SPaul E. McKenney 		u8 exp;
1475b74c458SPaul E. McKenney 	} b; /* Bits. */
1485b74c458SPaul E. McKenney 	u16 s; /* Set of bits, aggregate OR here. */
1495b74c458SPaul E. McKenney };
1505b74c458SPaul E. McKenney 
1514102adabSPaul E. McKenney /* Per-CPU data for read-copy update. */
1524102adabSPaul E. McKenney struct rcu_data {
1534102adabSPaul E. McKenney 	/* 1) quiescent-state and grace-period handling : */
154360fbbb4SLihao Liang 	unsigned long	gp_seq;		/* Track rsp->gp_seq counter. */
155adbccddbSJoel Fernandes (Google) 	unsigned long	gp_seq_needed;	/* Track furthest future GP request. */
1565b74c458SPaul E. McKenney 	union rcu_noqs	cpu_no_qs;	/* No QSes yet for this CPU. */
157a616aec9SIngo Molnar 	bool		core_needs_qs;	/* Core waits for quiescent state. */
1584102adabSPaul E. McKenney 	bool		beenonline;	/* CPU online at least once. */
159ff3bb6f4SPaul E. McKenney 	bool		gpwrap;		/* Possible ->gp_seq wrap. */
160c0f97f20SPaul E. McKenney 	bool		cpu_started;	/* RCU watching this onlining CPU. */
1614102adabSPaul E. McKenney 	struct rcu_node *mynode;	/* This CPU's leaf of hierarchy */
1624102adabSPaul E. McKenney 	unsigned long grpmask;		/* Mask to apply to leaf qsmask. */
1634102adabSPaul E. McKenney 	unsigned long	ticks_this_gp;	/* The number of scheduling-clock */
1644102adabSPaul E. McKenney 					/*  ticks this CPU has handled */
1654102adabSPaul E. McKenney 					/*  during and after the last grace */
1664102adabSPaul E. McKenney 					/* period it is aware of. */
1670864f057SPaul E. McKenney 	struct irq_work defer_qs_iw;	/* Obtain later scheduler attention. */
1680864f057SPaul E. McKenney 	bool defer_qs_iw_pending;	/* Scheduler attention pending? */
169a657f261SPaul E. McKenney 	struct work_struct strict_work;	/* Schedule readers for strict GPs. */
1704102adabSPaul E. McKenney 
1714102adabSPaul E. McKenney 	/* 2) batch handling */
17215fecf89SPaul E. McKenney 	struct rcu_segcblist cblist;	/* Segmented callback list, with */
17315fecf89SPaul E. McKenney 					/* different callbacks waiting for */
17415fecf89SPaul E. McKenney 					/* different grace periods. */
1754102adabSPaul E. McKenney 	long		qlen_last_fqs_check;
1764102adabSPaul E. McKenney 					/* qlen at last check for QS forcing */
177e816d56fSPaul E. McKenney 	unsigned long	n_cbs_invoked;	/* # callbacks invoked since boot. */
1784102adabSPaul E. McKenney 	unsigned long	n_force_qs_snap;
1794102adabSPaul E. McKenney 					/* did other CPU force QS recently? */
1804102adabSPaul E. McKenney 	long		blimit;		/* Upper limit on a processed batch */
1814102adabSPaul E. McKenney 
1824102adabSPaul E. McKenney 	/* 3) dynticks interface. */
1834102adabSPaul E. McKenney 	int dynticks_snap;		/* Per-GP tracking for dynticks. */
184cc72046cSPaul E. McKenney 	long dynticks_nesting;		/* Track process nesting level. */
185cc72046cSPaul E. McKenney 	long dynticks_nmi_nesting;	/* Track irq/NMI nesting level. */
186dc5a4f29SPaul E. McKenney 	atomic_t dynticks;		/* Even value for idle, else odd. */
187dc5a4f29SPaul E. McKenney 	bool rcu_need_heavy_qs;		/* GP old, so heavy quiescent state! */
188cc72046cSPaul E. McKenney 	bool rcu_urgent_qs;		/* GP old need light quiescent state. */
18966e4c33bSPaul E. McKenney 	bool rcu_forced_tick;		/* Forced tick to provide QS. */
190df1e849aSPaul E. McKenney 	bool rcu_forced_tick_exp;	/*   ... provide QS to expedited GP. */
1914102adabSPaul E. McKenney 
1928d8a9d0eSPaul E. McKenney 	/* 4) rcu_barrier(), OOM callbacks, and expediting. */
1934102adabSPaul E. McKenney 	struct rcu_head barrier_head;
1940742ac3eSPaul E. McKenney 	int exp_dynticks_snap;		/* Double-check need for IPI. */
1954102adabSPaul E. McKenney 
1968d8a9d0eSPaul E. McKenney 	/* 5) Callback offloading. */
1974102adabSPaul E. McKenney #ifdef CONFIG_RCU_NOCB_CPU
19812f54c3aSPaul E. McKenney 	struct swait_queue_head nocb_cb_wq; /* For nocb kthreads to sleep on. */
199d97b0781SFrederic Weisbecker 	struct swait_queue_head nocb_state_wq; /* For offloading state changes */
20012f54c3aSPaul E. McKenney 	struct task_struct *nocb_gp_kthread;
2018be6e1b1SPaul E. McKenney 	raw_spinlock_t nocb_lock;	/* Guard following pair of fields. */
20281c0b3d7SPaul E. McKenney 	atomic_t nocb_lock_contended;	/* Contention experienced. */
2039fdd3bc9SPaul E. McKenney 	int nocb_defer_wakeup;		/* Defer wakeup of nocb_kthread. */
2048be6e1b1SPaul E. McKenney 	struct timer_list nocb_timer;	/* Enforce finite deferral. */
205d1b222c6SPaul E. McKenney 	unsigned long nocb_gp_adv_time;	/* Last call_rcu() CB adv (jiffies). */
206*02e30241SNeeraj Upadhyay 	struct mutex nocb_gp_kthread_mutex; /* Exclusion for nocb gp kthread */
207*02e30241SNeeraj Upadhyay 					    /* spawning */
208d1b222c6SPaul E. McKenney 
209d1b222c6SPaul E. McKenney 	/* The following fields are used by call_rcu, hence own cacheline. */
210d1b222c6SPaul E. McKenney 	raw_spinlock_t nocb_bypass_lock ____cacheline_internodealigned_in_smp;
211d1b222c6SPaul E. McKenney 	struct rcu_cblist nocb_bypass;	/* Lock-contention-bypass CB list. */
212d1b222c6SPaul E. McKenney 	unsigned long nocb_bypass_first; /* Time (jiffies) of first enqueue. */
213d1b222c6SPaul E. McKenney 	unsigned long nocb_nobypass_last; /* Last ->cblist enqueue (jiffies). */
214d1b222c6SPaul E. McKenney 	int nocb_nobypass_count;	/* # ->cblist enqueues at ^^^ time. */
215fbce7497SPaul E. McKenney 
2166484fe54SPaul E. McKenney 	/* The following fields are used by GP kthread, hence own cacheline. */
2174fd8c5f1SPaul E. McKenney 	raw_spinlock_t nocb_gp_lock ____cacheline_internodealigned_in_smp;
218f7a81b12SPaul E. McKenney 	u8 nocb_gp_sleep;		/* Is the nocb GP thread asleep? */
219f7a81b12SPaul E. McKenney 	u8 nocb_gp_bypass;		/* Found a bypass on last scan? */
220f7a81b12SPaul E. McKenney 	u8 nocb_gp_gp;			/* GP to wait for on last scan? */
221f7a81b12SPaul E. McKenney 	unsigned long nocb_gp_seq;	/*  If so, ->gp_seq to wait for. */
222f7a81b12SPaul E. McKenney 	unsigned long nocb_gp_loops;	/* # passes through wait code. */
22312f54c3aSPaul E. McKenney 	struct swait_queue_head nocb_gp_wq; /* For nocb kthreads to sleep on. */
2245d6742b3SPaul E. McKenney 	bool nocb_cb_sleep;		/* Is the nocb CB thread asleep? */
22512f54c3aSPaul E. McKenney 	struct task_struct *nocb_cb_kthread;
2262ebc45c4SFrederic Weisbecker 	struct list_head nocb_head_rdp; /*
2272ebc45c4SFrederic Weisbecker 					 * Head of rcu_data list in wakeup chain,
2282ebc45c4SFrederic Weisbecker 					 * if rdp_gp.
2292ebc45c4SFrederic Weisbecker 					 */
2302ebc45c4SFrederic Weisbecker 	struct list_head nocb_entry_rdp; /* rcu_data node in wakeup chain. */
231fbce7497SPaul E. McKenney 
232d1b222c6SPaul E. McKenney 	/* The following fields are used by CB kthread, hence new cacheline. */
23358bf6f77SPaul E. McKenney 	struct rcu_data *nocb_gp_rdp ____cacheline_internodealigned_in_smp;
2346484fe54SPaul E. McKenney 					/* GP rdp takes GP-end wakeups. */
2354102adabSPaul E. McKenney #endif /* #ifdef CONFIG_RCU_NOCB_CPU */
2364102adabSPaul E. McKenney 
23737f62d7cSPaul E. McKenney 	/* 6) RCU priority boosting. */
23837f62d7cSPaul E. McKenney 	struct task_struct *rcu_cpu_kthread_task;
23937f62d7cSPaul E. McKenney 					/* rcuc per-CPU kthread or NULL. */
2406ffdde28SPaul E. McKenney 	unsigned int rcu_cpu_kthread_status;
241f7e972eeSPaul E. McKenney 	char rcu_cpu_has_work;
24237f62d7cSPaul E. McKenney 
24337f62d7cSPaul E. McKenney 	/* 7) Diagnostic data, including RCU CPU stall warnings. */
2444102adabSPaul E. McKenney 	unsigned int softirq_snap;	/* Snapshot of softirq activity. */
2459b9500daSPaul E. McKenney 	/* ->rcu_iw* fields protected by leaf rcu_node ->lock. */
2469b9500daSPaul E. McKenney 	struct irq_work rcu_iw;		/* Check for non-irq activity. */
2479b9500daSPaul E. McKenney 	bool rcu_iw_pending;		/* Is ->rcu_iw pending? */
2488aa670cdSPaul E. McKenney 	unsigned long rcu_iw_gp_seq;	/* ->gp_seq associated with ->rcu_iw. */
24957738942SPaul E. McKenney 	unsigned long rcu_ofl_gp_seq;	/* ->gp_seq at last offline. */
25057738942SPaul E. McKenney 	short rcu_ofl_gp_flags;		/* ->gp_flags at last offline. */
25157738942SPaul E. McKenney 	unsigned long rcu_onl_gp_seq;	/* ->gp_seq at last online. */
25257738942SPaul E. McKenney 	short rcu_onl_gp_flags;		/* ->gp_flags at last online. */
253d3052109SPaul E. McKenney 	unsigned long last_fqs_resched;	/* Time of last rcu_resched(). */
2544102adabSPaul E. McKenney 
2554102adabSPaul E. McKenney 	int cpu;
2564102adabSPaul E. McKenney };
2574102adabSPaul E. McKenney 
2589fdd3bc9SPaul E. McKenney /* Values for nocb_defer_wakeup field in struct rcu_data. */
259511324e4SPaul E. McKenney #define RCU_NOCB_WAKE_NOT	0
260e75bcd48SFrederic Weisbecker #define RCU_NOCB_WAKE_BYPASS	1
261e75bcd48SFrederic Weisbecker #define RCU_NOCB_WAKE		2
262e75bcd48SFrederic Weisbecker #define RCU_NOCB_WAKE_FORCE	3
2639fdd3bc9SPaul E. McKenney 
2644102adabSPaul E. McKenney #define RCU_JIFFIES_TILL_FORCE_QS (1 + (HZ > 250) + (HZ > 500))
2654102adabSPaul E. McKenney 					/* For jiffies_till_first_fqs and */
2664102adabSPaul E. McKenney 					/*  and jiffies_till_next_fqs. */
2674102adabSPaul E. McKenney 
2684102adabSPaul E. McKenney #define RCU_JIFFIES_FQS_DIV	256	/* Very large systems need more */
2694102adabSPaul E. McKenney 					/*  delay between bouts of */
2704102adabSPaul E. McKenney 					/*  quiescent-state forcing. */
2714102adabSPaul E. McKenney 
2724102adabSPaul E. McKenney #define RCU_STALL_RAT_DELAY	2	/* Allow other CPUs time to take */
2734102adabSPaul E. McKenney 					/*  at least one scheduling clock */
2744102adabSPaul E. McKenney 					/*  irq before ratting on them. */
2754102adabSPaul E. McKenney 
2764102adabSPaul E. McKenney #define rcu_wait(cond)							\
2774102adabSPaul E. McKenney do {									\
2784102adabSPaul E. McKenney 	for (;;) {							\
2794102adabSPaul E. McKenney 		set_current_state(TASK_INTERRUPTIBLE);			\
2804102adabSPaul E. McKenney 		if (cond)						\
2814102adabSPaul E. McKenney 			break;						\
2824102adabSPaul E. McKenney 		schedule();						\
2834102adabSPaul E. McKenney 	}								\
2844102adabSPaul E. McKenney 	__set_current_state(TASK_RUNNING);				\
2854102adabSPaul E. McKenney } while (0)
2864102adabSPaul E. McKenney 
2874102adabSPaul E. McKenney /*
2884102adabSPaul E. McKenney  * RCU global state, including node hierarchy.  This hierarchy is
2894102adabSPaul E. McKenney  * represented in "heap" form in a dense array.  The root (first level)
2904102adabSPaul E. McKenney  * of the hierarchy is in ->node[0] (referenced by ->level[0]), the second
2914102adabSPaul E. McKenney  * level in ->node[1] through ->node[m] (->node[1] referenced by ->level[1]),
2924102adabSPaul E. McKenney  * and the third level in ->node[m+1] and following (->node[m+1] referenced
2934102adabSPaul E. McKenney  * by ->level[2]).  The number of levels is determined by the number of
2944102adabSPaul E. McKenney  * CPUs and by CONFIG_RCU_FANOUT.  Small systems will have a "hierarchy"
2954102adabSPaul E. McKenney  * consisting of a single rcu_node.
2964102adabSPaul E. McKenney  */
2974102adabSPaul E. McKenney struct rcu_state {
2984102adabSPaul E. McKenney 	struct rcu_node node[NUM_RCU_NODES];	/* Hierarchy. */
299032dfc87SAlexander Gordeev 	struct rcu_node *level[RCU_NUM_LVLS + 1];
300032dfc87SAlexander Gordeev 						/* Hierarchy levels (+1 to */
301032dfc87SAlexander Gordeev 						/*  shut bogus gcc warning) */
302b9585e94SPaul E. McKenney 	int ncpus;				/* # CPUs seen so far. */
303ed73860cSNeeraj Upadhyay 	int n_online_cpus;			/* # CPUs online for RCU. */
3044102adabSPaul E. McKenney 
3054102adabSPaul E. McKenney 	/* The following fields are guarded by the root rcu_node's lock. */
3064102adabSPaul E. McKenney 
30777f81fe0SPetr Mladek 	u8	boost ____cacheline_internodealigned_in_smp;
30877f81fe0SPetr Mladek 						/* Subject to priority boost. */
309de30ad51SPaul E. McKenney 	unsigned long gp_seq;			/* Grace-period sequence #. */
31000943a60SWei Yang 	unsigned long gp_max;			/* Maximum GP duration in */
31100943a60SWei Yang 						/*  jiffies. */
3124102adabSPaul E. McKenney 	struct task_struct *gp_kthread;		/* Task for grace periods. */
313abedf8e2SPaul Gortmaker 	struct swait_queue_head gp_wq;		/* Where GP task waits. */
314afea227fSPaul E. McKenney 	short gp_flags;				/* Commands for GP task. */
315afea227fSPaul E. McKenney 	short gp_state;				/* GP kthread sleep state. */
316fd897573SPaul E. McKenney 	unsigned long gp_wake_time;		/* Last GP kthread wake. */
317fd897573SPaul E. McKenney 	unsigned long gp_wake_seq;		/* ->gp_seq at ^^^. */
3184102adabSPaul E. McKenney 
3194102adabSPaul E. McKenney 	/* End of fields guarded by root rcu_node's lock. */
3204102adabSPaul E. McKenney 
3214102adabSPaul E. McKenney 	struct mutex barrier_mutex;		/* Guards barrier fields. */
3224102adabSPaul E. McKenney 	atomic_t barrier_cpu_count;		/* # CPUs waiting on. */
3234102adabSPaul E. McKenney 	struct completion barrier_completion;	/* Wake at barrier end. */
3244f525a52SPaul E. McKenney 	unsigned long barrier_sequence;		/* ++ at start and end of */
325dd46a788SPaul E. McKenney 						/*  rcu_barrier(). */
3264102adabSPaul E. McKenney 	/* End of fields guarded by barrier_mutex. */
3274102adabSPaul E. McKenney 
328f6a12f34SPaul E. McKenney 	struct mutex exp_mutex;			/* Serialize expedited GP. */
3293b5f668eSPaul E. McKenney 	struct mutex exp_wake_mutex;		/* Serialize wakeup. */
330d6ada2cfSPaul E. McKenney 	unsigned long expedited_sequence;	/* Take a ticket. */
3313a6d7c64SPeter Zijlstra 	atomic_t expedited_need_qs;		/* # CPUs left to check in. */
332abedf8e2SPaul Gortmaker 	struct swait_queue_head expedited_wq;	/* Wait for check-ins. */
333b9585e94SPaul E. McKenney 	int ncpus_snap;				/* # CPUs seen last time. */
334b2b00ddfSPaul E. McKenney 	u8 cbovld;				/* Callback overload now? */
335b2b00ddfSPaul E. McKenney 	u8 cbovldnext;				/* ^        ^  next time? */
3364102adabSPaul E. McKenney 
3374102adabSPaul E. McKenney 	unsigned long jiffies_force_qs;		/* Time at which to invoke */
3384102adabSPaul E. McKenney 						/*  force_quiescent_state(). */
3398c7c4829SPaul E. McKenney 	unsigned long jiffies_kick_kthreads;	/* Time at which to kick */
3408c7c4829SPaul E. McKenney 						/*  kthreads, if configured. */
3414102adabSPaul E. McKenney 	unsigned long n_force_qs;		/* Number of calls to */
3424102adabSPaul E. McKenney 						/*  force_quiescent_state(). */
3434102adabSPaul E. McKenney 	unsigned long gp_start;			/* Time at which GP started, */
3444102adabSPaul E. McKenney 						/*  but in jiffies. */
345c51d7b5eSPaul E. McKenney 	unsigned long gp_end;			/* Time last GP ended, again */
346c51d7b5eSPaul E. McKenney 						/*  in jiffies. */
3476ccd2ecdSPaul E. McKenney 	unsigned long gp_activity;		/* Time of last GP kthread */
3486ccd2ecdSPaul E. McKenney 						/*  activity in jiffies. */
34926d950a9SPaul E. McKenney 	unsigned long gp_req_activity;		/* Time of last GP request */
35026d950a9SPaul E. McKenney 						/*  in jiffies. */
3514102adabSPaul E. McKenney 	unsigned long jiffies_stall;		/* Time at which to check */
3524102adabSPaul E. McKenney 						/*  for CPU stalls. */
3536193c76aSPaul E. McKenney 	unsigned long jiffies_resched;		/* Time at which to resched */
3546193c76aSPaul E. McKenney 						/*  a reluctant CPU. */
355fc908ed3SPaul E. McKenney 	unsigned long n_force_qs_gpstart;	/* Snapshot of n_force_qs at */
356fc908ed3SPaul E. McKenney 						/*  GP start. */
3574102adabSPaul E. McKenney 	const char *name;			/* Name of structure. */
3584102adabSPaul E. McKenney 	char abbr;				/* Abbreviated name. */
3591e64b15aSPaul E. McKenney 
360894d45bbSMike Galbraith 	raw_spinlock_t ofl_lock ____cacheline_internodealigned_in_smp;
3611e64b15aSPaul E. McKenney 						/* Synchronize offline with */
3621e64b15aSPaul E. McKenney 						/*  GP pre-initialization. */
3634102adabSPaul E. McKenney };
3644102adabSPaul E. McKenney 
3654102adabSPaul E. McKenney /* Values for rcu_state structure's gp_flags field. */
3664102adabSPaul E. McKenney #define RCU_GP_FLAG_INIT 0x1	/* Need grace-period initialization. */
3674102adabSPaul E. McKenney #define RCU_GP_FLAG_FQS  0x2	/* Need grace-period quiescent-state forcing. */
3681fca4d12SPaul E. McKenney #define RCU_GP_FLAG_OVLD 0x4	/* Experiencing callback overload. */
3694102adabSPaul E. McKenney 
370c34d2f41SPaul E. McKenney /* Values for rcu_state structure's gp_state field. */
37177f81fe0SPetr Mladek #define RCU_GP_IDLE	 0	/* Initial state and no GP in progress. */
372afea227fSPaul E. McKenney #define RCU_GP_WAIT_GPS  1	/* Wait for grace-period start. */
373319362c9SPaul E. McKenney #define RCU_GP_DONE_GPS  2	/* Wait done for grace-period start. */
374fea3f222SPaul E. McKenney #define RCU_GP_ONOFF     3	/* Grace-period initialization hotplug. */
375fea3f222SPaul E. McKenney #define RCU_GP_INIT      4	/* Grace-period initialization. */
376fea3f222SPaul E. McKenney #define RCU_GP_WAIT_FQS  5	/* Wait for force-quiescent-state time. */
377fea3f222SPaul E. McKenney #define RCU_GP_DOING_FQS 6	/* Wait done for force-quiescent-state time. */
378fea3f222SPaul E. McKenney #define RCU_GP_CLEANUP   7	/* Grace-period cleanup started. */
379fea3f222SPaul E. McKenney #define RCU_GP_CLEANED   8	/* Grace-period cleanup complete. */
380afea227fSPaul E. McKenney 
381358be2d3SPaul E. McKenney /*
382358be2d3SPaul E. McKenney  * In order to export the rcu_state name to the tracing tools, it
383358be2d3SPaul E. McKenney  * needs to be added in the __tracepoint_string section.
384358be2d3SPaul E. McKenney  * This requires defining a separate variable tp_<sname>_varname
385358be2d3SPaul E. McKenney  * that points to the string being used, and this will allow
386358be2d3SPaul E. McKenney  * the tracing userspace tools to be able to decipher the string
387358be2d3SPaul E. McKenney  * address to the matching string.
388358be2d3SPaul E. McKenney  */
389358be2d3SPaul E. McKenney #ifdef CONFIG_PREEMPT_RCU
390358be2d3SPaul E. McKenney #define RCU_ABBR 'p'
391358be2d3SPaul E. McKenney #define RCU_NAME_RAW "rcu_preempt"
392358be2d3SPaul E. McKenney #else /* #ifdef CONFIG_PREEMPT_RCU */
393358be2d3SPaul E. McKenney #define RCU_ABBR 's'
394358be2d3SPaul E. McKenney #define RCU_NAME_RAW "rcu_sched"
395358be2d3SPaul E. McKenney #endif /* #else #ifdef CONFIG_PREEMPT_RCU */
396358be2d3SPaul E. McKenney #ifndef CONFIG_TRACING
397358be2d3SPaul E. McKenney #define RCU_NAME RCU_NAME_RAW
398358be2d3SPaul E. McKenney #else /* #ifdef CONFIG_TRACING */
399358be2d3SPaul E. McKenney static char rcu_name[] = RCU_NAME_RAW;
400358be2d3SPaul E. McKenney static const char *tp_rcu_varname __used __tracepoint_string = rcu_name;
401358be2d3SPaul E. McKenney #define RCU_NAME rcu_name
402358be2d3SPaul E. McKenney #endif /* #else #ifdef CONFIG_TRACING */
4036b50e119SPaul E. McKenney 
40432255d51SPaul E. McKenney /* Forward declarations for tree_plugin.h */
4054102adabSPaul E. McKenney static void rcu_bootup_announce(void);
40645975c7dSPaul E. McKenney static void rcu_qs(void);
4074102adabSPaul E. McKenney static int rcu_preempt_blocked_readers_cgp(struct rcu_node *rnp);
4084102adabSPaul E. McKenney #ifdef CONFIG_HOTPLUG_CPU
4098af3a5e7SPaul E. McKenney static bool rcu_preempt_has_tasks(struct rcu_node *rnp);
4104102adabSPaul E. McKenney #endif /* #ifdef CONFIG_HOTPLUG_CPU */
41174611ecbSPaul E. McKenney static int rcu_print_task_exp_stall(struct rcu_node *rnp);
41281ab59a3SPaul E. McKenney static void rcu_preempt_check_blocked_tasks(struct rcu_node *rnp);
413c98cac60SPaul E. McKenney static void rcu_flavor_sched_clock_irq(int user);
41481ab59a3SPaul E. McKenney static void dump_blkd_tasks(struct rcu_node *rnp, int ncheck);
4154102adabSPaul E. McKenney static void rcu_initiate_boost(struct rcu_node *rnp, unsigned long flags);
4164102adabSPaul E. McKenney static void rcu_preempt_boost_start_gp(struct rcu_node *rnp);
4174102adabSPaul E. McKenney static bool rcu_is_callbacks_kthread(void);
41848d07c04SSebastian Andrzej Siewior static void rcu_cpu_kthread_setup(unsigned int cpu);
4193ef5a1c3SPaul E. McKenney static void rcu_spawn_one_boost_kthread(struct rcu_node *rnp);
4209386c0b7SPaul E. McKenney static void __init rcu_spawn_boost_kthreads(void);
4210aa04b05SPaul E. McKenney static bool rcu_preempt_has_tasks(struct rcu_node *rnp);
4223e310098SPaul E. McKenney static bool rcu_preempt_need_deferred_qs(struct task_struct *t);
4233e310098SPaul E. McKenney static void rcu_preempt_deferred_qs(struct task_struct *t);
4244102adabSPaul E. McKenney static void zero_cpu_stall_ticks(struct rcu_data *rdp);
425abedf8e2SPaul Gortmaker static struct swait_queue_head *rcu_nocb_gp_get(struct rcu_node *rnp);
426abedf8e2SPaul Gortmaker static void rcu_nocb_gp_cleanup(struct swait_queue_head *sq);
4274102adabSPaul E. McKenney static void rcu_init_one_nocb(struct rcu_node *rnp);
428d1b222c6SPaul E. McKenney static bool rcu_nocb_flush_bypass(struct rcu_data *rdp, struct rcu_head *rhp,
429d1b222c6SPaul E. McKenney 				  unsigned long j);
430d1b222c6SPaul E. McKenney static bool rcu_nocb_try_bypass(struct rcu_data *rdp, struct rcu_head *rhp,
431d1b222c6SPaul E. McKenney 				bool *was_alldone, unsigned long flags);
4325d6742b3SPaul E. McKenney static void __call_rcu_nocb_wake(struct rcu_data *rdp, bool was_empty,
43396d3fd0dSPaul E. McKenney 				 unsigned long flags);
43487090516SFrederic Weisbecker static int rcu_nocb_need_deferred_wakeup(struct rcu_data *rdp, int level);
435f8bb5caeSFrederic Weisbecker static bool do_nocb_deferred_wakeup(struct rcu_data *rdp);
4364102adabSPaul E. McKenney static void rcu_boot_init_nocb_percpu_data(struct rcu_data *rdp);
437ad368d15SPaul E. McKenney static void rcu_spawn_cpu_nocb_kthread(int cpu);
43835ce7f29SPaul E. McKenney static void __init rcu_spawn_nocb_kthreads(void);
439f7a81b12SPaul E. McKenney static void show_rcu_nocb_state(struct rcu_data *rdp);
4405d6742b3SPaul E. McKenney static void rcu_nocb_lock(struct rcu_data *rdp);
4415d6742b3SPaul E. McKenney static void rcu_nocb_unlock(struct rcu_data *rdp);
4425d6742b3SPaul E. McKenney static void rcu_nocb_unlock_irqrestore(struct rcu_data *rdp,
4435d6742b3SPaul E. McKenney 				       unsigned long flags);
444d1b222c6SPaul E. McKenney static void rcu_lockdep_assert_cblist_protected(struct rcu_data *rdp);
44535ce7f29SPaul E. McKenney #ifdef CONFIG_RCU_NOCB_CPU
4464580b054SPaul E. McKenney static void __init rcu_organize_nocb_kthreads(void);
447118e0d4aSFrederic Weisbecker 
448118e0d4aSFrederic Weisbecker /*
449118e0d4aSFrederic Weisbecker  * Disable IRQs before checking offloaded state so that local
450118e0d4aSFrederic Weisbecker  * locking is safe against concurrent de-offloading.
451118e0d4aSFrederic Weisbecker  */
45281c0b3d7SPaul E. McKenney #define rcu_nocb_lock_irqsave(rdp, flags)			\
45381c0b3d7SPaul E. McKenney do {								\
45481c0b3d7SPaul E. McKenney 	local_irq_save(flags);					\
455118e0d4aSFrederic Weisbecker 	if (rcu_segcblist_is_offloaded(&(rdp)->cblist))	\
456118e0d4aSFrederic Weisbecker 		raw_spin_lock(&(rdp)->nocb_lock);		\
45781c0b3d7SPaul E. McKenney } while (0)
45881c0b3d7SPaul E. McKenney #else /* #ifdef CONFIG_RCU_NOCB_CPU */
45981c0b3d7SPaul E. McKenney #define rcu_nocb_lock_irqsave(rdp, flags) local_irq_save(flags)
46081c0b3d7SPaul E. McKenney #endif /* #else #ifdef CONFIG_RCU_NOCB_CPU */
46181c0b3d7SPaul E. McKenney 
4624102adabSPaul E. McKenney static void rcu_bind_gp_kthread(void);
4634580b054SPaul E. McKenney static bool rcu_nohz_full_cpu(void);
464176f8f7aSPaul E. McKenney static void rcu_dynticks_task_enter(void);
465176f8f7aSPaul E. McKenney static void rcu_dynticks_task_exit(void);
4667d0c9c50SPaul E. McKenney static void rcu_dynticks_task_trace_enter(void);
4677d0c9c50SPaul E. McKenney static void rcu_dynticks_task_trace_exit(void);
46832255d51SPaul E. McKenney 
46932255d51SPaul E. McKenney /* Forward declarations for tree_stall.h */
47032255d51SPaul E. McKenney static void record_gp_stall_check_time(void);
4717ac1907cSPaul E. McKenney static void rcu_iw_handler(struct irq_work *iwp);
47232255d51SPaul E. McKenney static void check_cpu_stall(struct rcu_data *rdp);
473b51bcbbfSPaul E. McKenney static void rcu_check_gp_start_stall(struct rcu_node *rnp, struct rcu_data *rdp,
474b51bcbbfSPaul E. McKenney 				     const unsigned long gpssdelay);
475