xref: /linux/kernel/rcu/tree.h (revision b8f7aca3f0e0e6223094ba2662bac90353674b04)
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>
139621fbeeSKalesh Singh #include <linux/kthread.h>
144102adabSPaul E. McKenney #include <linux/spinlock.h>
15037741a6SIngo Molnar #include <linux/rtmutex.h>
164102adabSPaul E. McKenney #include <linux/threads.h>
174102adabSPaul E. McKenney #include <linux/cpumask.h>
184102adabSPaul E. McKenney #include <linux/seqlock.h>
19abedf8e2SPaul Gortmaker #include <linux/swait.h>
20f2425b4eSPaul E. McKenney #include <linux/rcu_node_tree.h>
214102adabSPaul E. McKenney 
2245753c5fSIngo Molnar #include "rcu_segcblist.h"
2345753c5fSIngo Molnar 
2425f3d7efSPaul E. McKenney /* Communicate arguments to a workqueue handler. */
2525f3d7efSPaul E. McKenney struct rcu_exp_work {
2625f3d7efSPaul E. McKenney 	unsigned long rew_s;
279621fbeeSKalesh Singh #ifdef CONFIG_RCU_EXP_KTHREAD
289621fbeeSKalesh Singh 	struct kthread_work rew_work;
299621fbeeSKalesh Singh #else
3025f3d7efSPaul E. McKenney 	struct work_struct rew_work;
319621fbeeSKalesh Singh #endif /* CONFIG_RCU_EXP_KTHREAD */
3225f3d7efSPaul E. McKenney };
3325f3d7efSPaul E. McKenney 
344102adabSPaul E. McKenney /* RCU's kthread states for tracing. */
354102adabSPaul E. McKenney #define RCU_KTHREAD_STOPPED  0
364102adabSPaul E. McKenney #define RCU_KTHREAD_RUNNING  1
374102adabSPaul E. McKenney #define RCU_KTHREAD_WAITING  2
384102adabSPaul E. McKenney #define RCU_KTHREAD_OFFCPU   3
394102adabSPaul E. McKenney #define RCU_KTHREAD_YIELDING 4
404102adabSPaul E. McKenney #define RCU_KTHREAD_MAX      4
414102adabSPaul E. McKenney 
424102adabSPaul E. McKenney /*
434102adabSPaul E. McKenney  * Definition for node within the RCU grace-period-detection hierarchy.
444102adabSPaul E. McKenney  */
454102adabSPaul E. McKenney struct rcu_node {
4667c583a7SBoqun Feng 	raw_spinlock_t __private lock;	/* Root rcu_node's lock protects */
4767c583a7SBoqun Feng 					/*  some rcu_state fields as well as */
4867c583a7SBoqun Feng 					/*  following. */
49360fbbb4SLihao Liang 	unsigned long gp_seq;	/* Track rsp->gp_seq. */
50adbccddbSJoel Fernandes (Google) 	unsigned long gp_seq_needed; /* Track furthest future GP request. */
514bc8d555SPaul E. McKenney 	unsigned long completedqs; /* All QSes done for this node. */
524102adabSPaul E. McKenney 	unsigned long qsmask;	/* CPUs or groups that need to switch in */
534102adabSPaul E. McKenney 				/*  order for current grace period to proceed.*/
544102adabSPaul E. McKenney 				/*  In leaf rcu_node, each bit corresponds to */
554102adabSPaul E. McKenney 				/*  an rcu_data structure, otherwise, each */
564102adabSPaul E. McKenney 				/*  bit corresponds to a child rcu_node */
574102adabSPaul E. McKenney 				/*  structure. */
58f2e2df59SPaul E. McKenney 	unsigned long rcu_gp_init_mask;	/* Mask of offline CPUs at GP init. */
594102adabSPaul E. McKenney 	unsigned long qsmaskinit;
60b9585e94SPaul E. McKenney 				/* Per-GP initial value for qsmask. */
610aa04b05SPaul E. McKenney 				/*  Initialized from ->qsmaskinitnext at the */
620aa04b05SPaul E. McKenney 				/*  beginning of each grace period. */
630aa04b05SPaul E. McKenney 	unsigned long qsmaskinitnext;
64b9585e94SPaul E. McKenney 	unsigned long expmask;	/* CPUs or groups that need to check in */
65b9585e94SPaul E. McKenney 				/*  to allow the current expedited GP */
66b9585e94SPaul E. McKenney 				/*  to complete. */
67b9585e94SPaul E. McKenney 	unsigned long expmaskinit;
68b9585e94SPaul E. McKenney 				/* Per-GP initial values for expmask. */
69b9585e94SPaul E. McKenney 				/*  Initialized from ->expmaskinitnext at the */
70b9585e94SPaul E. McKenney 				/*  beginning of each expedited GP. */
71b9585e94SPaul E. McKenney 	unsigned long expmaskinitnext;
72b9585e94SPaul E. McKenney 				/* Online CPUs for next expedited GP. */
731de6e56dSPaul E. McKenney 				/*  Any CPU that has ever been online will */
741de6e56dSPaul E. McKenney 				/*  have its bit set. */
75b2b00ddfSPaul E. McKenney 	unsigned long cbovldmask;
76b2b00ddfSPaul E. McKenney 				/* CPUs experiencing callback overload. */
779b9500daSPaul E. McKenney 	unsigned long ffmask;	/* Fully functional CPUs. */
784102adabSPaul E. McKenney 	unsigned long grpmask;	/* Mask to apply to parent qsmask. */
794102adabSPaul E. McKenney 				/*  Only one bit will be set in this mask. */
80a2dae430SWei Yang 	int	grplo;		/* lowest-numbered CPU here. */
81a2dae430SWei Yang 	int	grphi;		/* highest-numbered CPU here. */
827a0c2b09SWei Yang 	u8	grpnum;		/* group number for next level up. */
834102adabSPaul E. McKenney 	u8	level;		/* root is at level 0. */
840aa04b05SPaul E. McKenney 	bool	wait_blkd_tasks;/* Necessary to wait for blocked tasks to */
850aa04b05SPaul E. McKenney 				/*  exit RCU read-side critical sections */
860aa04b05SPaul E. McKenney 				/*  before propagating offline up the */
870aa04b05SPaul E. McKenney 				/*  rcu_node tree? */
884102adabSPaul E. McKenney 	struct rcu_node *parent;
894102adabSPaul E. McKenney 	struct list_head blkd_tasks;
904102adabSPaul E. McKenney 				/* Tasks blocked in RCU read-side critical */
914102adabSPaul E. McKenney 				/*  section.  Tasks are placed at the head */
924102adabSPaul E. McKenney 				/*  of this list and age towards the tail. */
934102adabSPaul E. McKenney 	struct list_head *gp_tasks;
944102adabSPaul E. McKenney 				/* Pointer to the first task blocking the */
954102adabSPaul E. McKenney 				/*  current grace period, or NULL if there */
964102adabSPaul E. McKenney 				/*  is no such task. */
974102adabSPaul E. McKenney 	struct list_head *exp_tasks;
984102adabSPaul E. McKenney 				/* Pointer to the first task blocking the */
994102adabSPaul E. McKenney 				/*  current expedited grace period, or NULL */
1004102adabSPaul E. McKenney 				/*  if there is no such task.  If there */
1014102adabSPaul E. McKenney 				/*  is no current expedited grace period, */
1024102adabSPaul E. McKenney 				/*  then there can cannot be any such task. */
1034102adabSPaul E. McKenney 	struct list_head *boost_tasks;
1044102adabSPaul E. McKenney 				/* Pointer to first task that needs to be */
1054102adabSPaul E. McKenney 				/*  priority boosted, or NULL if no priority */
1064102adabSPaul E. McKenney 				/*  boosting is needed for this rcu_node */
1074102adabSPaul E. McKenney 				/*  structure.  If there are no tasks */
1084102adabSPaul E. McKenney 				/*  queued on this rcu_node structure that */
1094102adabSPaul E. McKenney 				/*  are blocking the current grace period, */
1104102adabSPaul E. McKenney 				/*  there can be no such task. */
111abaa93d9SPaul E. McKenney 	struct rt_mutex boost_mtx;
112abaa93d9SPaul E. McKenney 				/* Used only for the priority-boosting */
113abaa93d9SPaul E. McKenney 				/*  side effect, not as a lock. */
1144102adabSPaul E. McKenney 	unsigned long boost_time;
1154102adabSPaul E. McKenney 				/* When to start boosting (jiffies). */
116218b957aSDavid Woodhouse 	struct mutex boost_kthread_mutex;
117218b957aSDavid Woodhouse 				/* Exclusion for thread spawning and affinity */
118218b957aSDavid Woodhouse 				/*  manipulation. */
1194102adabSPaul E. McKenney 	struct task_struct *boost_kthread_task;
1204102adabSPaul E. McKenney 				/* kthread that takes care of priority */
1214102adabSPaul E. McKenney 				/*  boosting for this rcu_node structure. */
1224102adabSPaul E. McKenney 	unsigned int boost_kthread_status;
1234102adabSPaul E. McKenney 				/* State of boost_kthread_task for tracing. */
124396eba65SPaul E. McKenney 	unsigned long n_boosts;	/* Number of boosts for this rcu_node structure. */
1254102adabSPaul E. McKenney #ifdef CONFIG_RCU_NOCB_CPU
126abedf8e2SPaul Gortmaker 	struct swait_queue_head nocb_gp_wq[2];
1274102adabSPaul E. McKenney 				/* Place for rcu_nocb_kthread() to wait GP. */
1284102adabSPaul E. McKenney #endif /* #ifdef CONFIG_RCU_NOCB_CPU */
1294102adabSPaul E. McKenney 	raw_spinlock_t fqslock ____cacheline_internodealigned_in_smp;
130385b73c0SPaul E. McKenney 
131f6a12f34SPaul E. McKenney 	spinlock_t exp_lock ____cacheline_internodealigned_in_smp;
132f6a12f34SPaul E. McKenney 	unsigned long exp_seq_rq;
1333b5f668eSPaul E. McKenney 	wait_queue_head_t exp_wq[4];
13425f3d7efSPaul E. McKenney 	struct rcu_exp_work rew;
13525f3d7efSPaul E. McKenney 	bool exp_need_flush;	/* Need to flush workitem? */
136d96c52feSPaul E. McKenney 	raw_spinlock_t exp_poll_lock;
137d96c52feSPaul E. McKenney 				/* Lock and data for polled expedited grace periods. */
138d96c52feSPaul E. McKenney 	unsigned long exp_seq_poll_rq;
139d96c52feSPaul E. McKenney 	struct work_struct exp_poll_wq;
1404102adabSPaul E. McKenney } ____cacheline_internodealigned_in_smp;
1414102adabSPaul E. McKenney 
1424102adabSPaul E. McKenney /*
143bc75e999SMark Rutland  * Bitmasks in an rcu_node cover the interval [grplo, grphi] of CPU IDs, and
144bc75e999SMark Rutland  * are indexed relative to this interval rather than the global CPU ID space.
145bc75e999SMark Rutland  * This generates the bit for a CPU in node-local masks.
146bc75e999SMark Rutland  */
147df63fa5bSPaul E. McKenney #define leaf_node_cpu_bit(rnp, cpu) (BIT((cpu) - (rnp)->grplo))
148bc75e999SMark Rutland 
149bc75e999SMark Rutland /*
1505b74c458SPaul E. McKenney  * Union to allow "aggregate OR" operation on the need for a quiescent
1515b74c458SPaul E. McKenney  * state by the normal and expedited grace periods.
1525b74c458SPaul E. McKenney  */
1535b74c458SPaul E. McKenney union rcu_noqs {
1545b74c458SPaul E. McKenney 	struct {
1555b74c458SPaul E. McKenney 		u8 norm;
1565b74c458SPaul E. McKenney 		u8 exp;
1575b74c458SPaul E. McKenney 	} b; /* Bits. */
1585b74c458SPaul E. McKenney 	u16 s; /* Set of bits, aggregate OR here. */
1595b74c458SPaul E. McKenney };
1605b74c458SPaul E. McKenney 
1614102adabSPaul E. McKenney /* Per-CPU data for read-copy update. */
1624102adabSPaul E. McKenney struct rcu_data {
1634102adabSPaul E. McKenney 	/* 1) quiescent-state and grace-period handling : */
164360fbbb4SLihao Liang 	unsigned long	gp_seq;		/* Track rsp->gp_seq counter. */
165adbccddbSJoel Fernandes (Google) 	unsigned long	gp_seq_needed;	/* Track furthest future GP request. */
1665b74c458SPaul E. McKenney 	union rcu_noqs	cpu_no_qs;	/* No QSes yet for this CPU. */
167a616aec9SIngo Molnar 	bool		core_needs_qs;	/* Core waits for quiescent state. */
1684102adabSPaul E. McKenney 	bool		beenonline;	/* CPU online at least once. */
169ff3bb6f4SPaul E. McKenney 	bool		gpwrap;		/* Possible ->gp_seq wrap. */
170c0f97f20SPaul E. McKenney 	bool		cpu_started;	/* RCU watching this onlining CPU. */
1714102adabSPaul E. McKenney 	struct rcu_node *mynode;	/* This CPU's leaf of hierarchy */
1724102adabSPaul E. McKenney 	unsigned long grpmask;		/* Mask to apply to leaf qsmask. */
1734102adabSPaul E. McKenney 	unsigned long	ticks_this_gp;	/* The number of scheduling-clock */
1744102adabSPaul E. McKenney 					/*  ticks this CPU has handled */
1754102adabSPaul E. McKenney 					/*  during and after the last grace */
1764102adabSPaul E. McKenney 					/* period it is aware of. */
1770864f057SPaul E. McKenney 	struct irq_work defer_qs_iw;	/* Obtain later scheduler attention. */
1780864f057SPaul E. McKenney 	bool defer_qs_iw_pending;	/* Scheduler attention pending? */
179a657f261SPaul E. McKenney 	struct work_struct strict_work;	/* Schedule readers for strict GPs. */
1804102adabSPaul E. McKenney 
1814102adabSPaul E. McKenney 	/* 2) batch handling */
18215fecf89SPaul E. McKenney 	struct rcu_segcblist cblist;	/* Segmented callback list, with */
18315fecf89SPaul E. McKenney 					/* different callbacks waiting for */
18415fecf89SPaul E. McKenney 					/* different grace periods. */
1854102adabSPaul E. McKenney 	long		qlen_last_fqs_check;
1864102adabSPaul E. McKenney 					/* qlen at last check for QS forcing */
187e816d56fSPaul E. McKenney 	unsigned long	n_cbs_invoked;	/* # callbacks invoked since boot. */
1884102adabSPaul E. McKenney 	unsigned long	n_force_qs_snap;
1894102adabSPaul E. McKenney 					/* did other CPU force QS recently? */
1904102adabSPaul E. McKenney 	long		blimit;		/* Upper limit on a processed batch */
1914102adabSPaul E. McKenney 
1924102adabSPaul E. McKenney 	/* 3) dynticks interface. */
1934102adabSPaul E. McKenney 	int dynticks_snap;		/* Per-GP tracking for dynticks. */
194dc5a4f29SPaul E. McKenney 	bool rcu_need_heavy_qs;		/* GP old, so heavy quiescent state! */
195cc72046cSPaul E. McKenney 	bool rcu_urgent_qs;		/* GP old need light quiescent state. */
19666e4c33bSPaul E. McKenney 	bool rcu_forced_tick;		/* Forced tick to provide QS. */
197df1e849aSPaul E. McKenney 	bool rcu_forced_tick_exp;	/*   ... provide QS to expedited GP. */
1984102adabSPaul E. McKenney 
1998d8a9d0eSPaul E. McKenney 	/* 4) rcu_barrier(), OOM callbacks, and expediting. */
200a16578ddSPaul E. McKenney 	unsigned long barrier_seq_snap;	/* Snap of rcu_state.barrier_sequence. */
2014102adabSPaul E. McKenney 	struct rcu_head barrier_head;
2020742ac3eSPaul E. McKenney 	int exp_dynticks_snap;		/* Double-check need for IPI. */
2034102adabSPaul E. McKenney 
2048d8a9d0eSPaul E. McKenney 	/* 5) Callback offloading. */
2054102adabSPaul E. McKenney #ifdef CONFIG_RCU_NOCB_CPU
20612f54c3aSPaul E. McKenney 	struct swait_queue_head nocb_cb_wq; /* For nocb kthreads to sleep on. */
207d97b0781SFrederic Weisbecker 	struct swait_queue_head nocb_state_wq; /* For offloading state changes */
20812f54c3aSPaul E. McKenney 	struct task_struct *nocb_gp_kthread;
2098be6e1b1SPaul E. McKenney 	raw_spinlock_t nocb_lock;	/* Guard following pair of fields. */
21081c0b3d7SPaul E. McKenney 	atomic_t nocb_lock_contended;	/* Contention experienced. */
2119fdd3bc9SPaul E. McKenney 	int nocb_defer_wakeup;		/* Defer wakeup of nocb_kthread. */
2128be6e1b1SPaul E. McKenney 	struct timer_list nocb_timer;	/* Enforce finite deferral. */
213d1b222c6SPaul E. McKenney 	unsigned long nocb_gp_adv_time;	/* Last call_rcu() CB adv (jiffies). */
21402e30241SNeeraj Upadhyay 	struct mutex nocb_gp_kthread_mutex; /* Exclusion for nocb gp kthread */
21502e30241SNeeraj Upadhyay 					    /* spawning */
216d1b222c6SPaul E. McKenney 
217d1b222c6SPaul E. McKenney 	/* The following fields are used by call_rcu, hence own cacheline. */
218d1b222c6SPaul E. McKenney 	raw_spinlock_t nocb_bypass_lock ____cacheline_internodealigned_in_smp;
219d1b222c6SPaul E. McKenney 	struct rcu_cblist nocb_bypass;	/* Lock-contention-bypass CB list. */
220d1b222c6SPaul E. McKenney 	unsigned long nocb_bypass_first; /* Time (jiffies) of first enqueue. */
221d1b222c6SPaul E. McKenney 	unsigned long nocb_nobypass_last; /* Last ->cblist enqueue (jiffies). */
222d1b222c6SPaul E. McKenney 	int nocb_nobypass_count;	/* # ->cblist enqueues at ^^^ time. */
223fbce7497SPaul E. McKenney 
2246484fe54SPaul E. McKenney 	/* The following fields are used by GP kthread, hence own cacheline. */
2254fd8c5f1SPaul E. McKenney 	raw_spinlock_t nocb_gp_lock ____cacheline_internodealigned_in_smp;
226f7a81b12SPaul E. McKenney 	u8 nocb_gp_sleep;		/* Is the nocb GP thread asleep? */
227f7a81b12SPaul E. McKenney 	u8 nocb_gp_bypass;		/* Found a bypass on last scan? */
228f7a81b12SPaul E. McKenney 	u8 nocb_gp_gp;			/* GP to wait for on last scan? */
229f7a81b12SPaul E. McKenney 	unsigned long nocb_gp_seq;	/*  If so, ->gp_seq to wait for. */
230f7a81b12SPaul E. McKenney 	unsigned long nocb_gp_loops;	/* # passes through wait code. */
23112f54c3aSPaul E. McKenney 	struct swait_queue_head nocb_gp_wq; /* For nocb kthreads to sleep on. */
2325d6742b3SPaul E. McKenney 	bool nocb_cb_sleep;		/* Is the nocb CB thread asleep? */
23312f54c3aSPaul E. McKenney 	struct task_struct *nocb_cb_kthread;
2342ebc45c4SFrederic Weisbecker 	struct list_head nocb_head_rdp; /*
2352ebc45c4SFrederic Weisbecker 					 * Head of rcu_data list in wakeup chain,
2362ebc45c4SFrederic Weisbecker 					 * if rdp_gp.
2372ebc45c4SFrederic Weisbecker 					 */
2382ebc45c4SFrederic Weisbecker 	struct list_head nocb_entry_rdp; /* rcu_data node in wakeup chain. */
2391598f4a4SFrederic Weisbecker 	struct rcu_data *nocb_toggling_rdp; /* rdp queued for (de-)offloading */
240fbce7497SPaul E. McKenney 
241d1b222c6SPaul E. McKenney 	/* The following fields are used by CB kthread, hence new cacheline. */
24258bf6f77SPaul E. McKenney 	struct rcu_data *nocb_gp_rdp ____cacheline_internodealigned_in_smp;
2436484fe54SPaul E. McKenney 					/* GP rdp takes GP-end wakeups. */
2444102adabSPaul E. McKenney #endif /* #ifdef CONFIG_RCU_NOCB_CPU */
2454102adabSPaul E. McKenney 
24637f62d7cSPaul E. McKenney 	/* 6) RCU priority boosting. */
24737f62d7cSPaul E. McKenney 	struct task_struct *rcu_cpu_kthread_task;
24837f62d7cSPaul E. McKenney 					/* rcuc per-CPU kthread or NULL. */
2496ffdde28SPaul E. McKenney 	unsigned int rcu_cpu_kthread_status;
250f7e972eeSPaul E. McKenney 	char rcu_cpu_has_work;
251c9515875SZqiang 	unsigned long rcuc_activity;
25237f62d7cSPaul E. McKenney 
25337f62d7cSPaul E. McKenney 	/* 7) Diagnostic data, including RCU CPU stall warnings. */
2544102adabSPaul E. McKenney 	unsigned int softirq_snap;	/* Snapshot of softirq activity. */
2559b9500daSPaul E. McKenney 	/* ->rcu_iw* fields protected by leaf rcu_node ->lock. */
2569b9500daSPaul E. McKenney 	struct irq_work rcu_iw;		/* Check for non-irq activity. */
2579b9500daSPaul E. McKenney 	bool rcu_iw_pending;		/* Is ->rcu_iw pending? */
2588aa670cdSPaul E. McKenney 	unsigned long rcu_iw_gp_seq;	/* ->gp_seq associated with ->rcu_iw. */
25957738942SPaul E. McKenney 	unsigned long rcu_ofl_gp_seq;	/* ->gp_seq at last offline. */
26057738942SPaul E. McKenney 	short rcu_ofl_gp_flags;		/* ->gp_flags at last offline. */
26157738942SPaul E. McKenney 	unsigned long rcu_onl_gp_seq;	/* ->gp_seq at last online. */
26257738942SPaul E. McKenney 	short rcu_onl_gp_flags;		/* ->gp_flags at last online. */
263d3052109SPaul E. McKenney 	unsigned long last_fqs_resched;	/* Time of last rcu_resched(). */
264c708b08cSPaul E. McKenney 	unsigned long last_sched_clock;	/* Jiffies of last rcu_sched_clock_irq(). */
2654102adabSPaul E. McKenney 
2664102adabSPaul E. McKenney 	int cpu;
2674102adabSPaul E. McKenney };
2684102adabSPaul E. McKenney 
2699fdd3bc9SPaul E. McKenney /* Values for nocb_defer_wakeup field in struct rcu_data. */
270511324e4SPaul E. McKenney #define RCU_NOCB_WAKE_NOT	0
271e75bcd48SFrederic Weisbecker #define RCU_NOCB_WAKE_BYPASS	1
272e75bcd48SFrederic Weisbecker #define RCU_NOCB_WAKE		2
273e75bcd48SFrederic Weisbecker #define RCU_NOCB_WAKE_FORCE	3
2749fdd3bc9SPaul E. McKenney 
2754102adabSPaul E. McKenney #define RCU_JIFFIES_TILL_FORCE_QS (1 + (HZ > 250) + (HZ > 500))
2764102adabSPaul E. McKenney 					/* For jiffies_till_first_fqs and */
2774102adabSPaul E. McKenney 					/*  and jiffies_till_next_fqs. */
2784102adabSPaul E. McKenney 
2794102adabSPaul E. McKenney #define RCU_JIFFIES_FQS_DIV	256	/* Very large systems need more */
2804102adabSPaul E. McKenney 					/*  delay between bouts of */
2814102adabSPaul E. McKenney 					/*  quiescent-state forcing. */
2824102adabSPaul E. McKenney 
2834102adabSPaul E. McKenney #define RCU_STALL_RAT_DELAY	2	/* Allow other CPUs time to take */
2844102adabSPaul E. McKenney 					/*  at least one scheduling clock */
2854102adabSPaul E. McKenney 					/*  irq before ratting on them. */
2864102adabSPaul E. McKenney 
2874102adabSPaul E. McKenney #define rcu_wait(cond)							\
2884102adabSPaul E. McKenney do {									\
2894102adabSPaul E. McKenney 	for (;;) {							\
2904102adabSPaul E. McKenney 		set_current_state(TASK_INTERRUPTIBLE);			\
2914102adabSPaul E. McKenney 		if (cond)						\
2924102adabSPaul E. McKenney 			break;						\
2934102adabSPaul E. McKenney 		schedule();						\
2944102adabSPaul E. McKenney 	}								\
2954102adabSPaul E. McKenney 	__set_current_state(TASK_RUNNING);				\
2964102adabSPaul E. McKenney } while (0)
2974102adabSPaul E. McKenney 
2984102adabSPaul E. McKenney /*
2994102adabSPaul E. McKenney  * RCU global state, including node hierarchy.  This hierarchy is
3004102adabSPaul E. McKenney  * represented in "heap" form in a dense array.  The root (first level)
3014102adabSPaul E. McKenney  * of the hierarchy is in ->node[0] (referenced by ->level[0]), the second
3024102adabSPaul E. McKenney  * level in ->node[1] through ->node[m] (->node[1] referenced by ->level[1]),
3034102adabSPaul E. McKenney  * and the third level in ->node[m+1] and following (->node[m+1] referenced
3044102adabSPaul E. McKenney  * by ->level[2]).  The number of levels is determined by the number of
3054102adabSPaul E. McKenney  * CPUs and by CONFIG_RCU_FANOUT.  Small systems will have a "hierarchy"
3064102adabSPaul E. McKenney  * consisting of a single rcu_node.
3074102adabSPaul E. McKenney  */
3084102adabSPaul E. McKenney struct rcu_state {
3094102adabSPaul E. McKenney 	struct rcu_node node[NUM_RCU_NODES];	/* Hierarchy. */
310032dfc87SAlexander Gordeev 	struct rcu_node *level[RCU_NUM_LVLS + 1];
311032dfc87SAlexander Gordeev 						/* Hierarchy levels (+1 to */
312032dfc87SAlexander Gordeev 						/*  shut bogus gcc warning) */
313b9585e94SPaul E. McKenney 	int ncpus;				/* # CPUs seen so far. */
314ed73860cSNeeraj Upadhyay 	int n_online_cpus;			/* # CPUs online for RCU. */
3154102adabSPaul E. McKenney 
3164102adabSPaul E. McKenney 	/* The following fields are guarded by the root rcu_node's lock. */
3174102adabSPaul E. McKenney 
318eae9f147SNeeraj Upadhyay 	unsigned long gp_seq ____cacheline_internodealigned_in_smp;
319eae9f147SNeeraj Upadhyay 						/* Grace-period sequence #. */
32000943a60SWei Yang 	unsigned long gp_max;			/* Maximum GP duration in */
32100943a60SWei Yang 						/*  jiffies. */
3224102adabSPaul E. McKenney 	struct task_struct *gp_kthread;		/* Task for grace periods. */
323abedf8e2SPaul Gortmaker 	struct swait_queue_head gp_wq;		/* Where GP task waits. */
324afea227fSPaul E. McKenney 	short gp_flags;				/* Commands for GP task. */
325afea227fSPaul E. McKenney 	short gp_state;				/* GP kthread sleep state. */
326fd897573SPaul E. McKenney 	unsigned long gp_wake_time;		/* Last GP kthread wake. */
327fd897573SPaul E. McKenney 	unsigned long gp_wake_seq;		/* ->gp_seq at ^^^. */
328bf95b2bcSPaul E. McKenney 	unsigned long gp_seq_polled;		/* GP seq for polled API. */
329bf95b2bcSPaul E. McKenney 	unsigned long gp_seq_polled_snap;	/* ->gp_seq_polled at normal GP start. */
330dd041405SPaul E. McKenney 	unsigned long gp_seq_polled_exp_snap;	/* ->gp_seq_polled at expedited GP start. */
3314102adabSPaul E. McKenney 
3324102adabSPaul E. McKenney 	/* End of fields guarded by root rcu_node's lock. */
3334102adabSPaul E. McKenney 
3344102adabSPaul E. McKenney 	struct mutex barrier_mutex;		/* Guards barrier fields. */
3354102adabSPaul E. McKenney 	atomic_t barrier_cpu_count;		/* # CPUs waiting on. */
3364102adabSPaul E. McKenney 	struct completion barrier_completion;	/* Wake at barrier end. */
3374f525a52SPaul E. McKenney 	unsigned long barrier_sequence;		/* ++ at start and end of */
338dd46a788SPaul E. McKenney 						/*  rcu_barrier(). */
3394102adabSPaul E. McKenney 	/* End of fields guarded by barrier_mutex. */
3404102adabSPaul E. McKenney 
34180b3fd47SPaul E. McKenney 	raw_spinlock_t barrier_lock;		/* Protects ->barrier_seq_snap. */
34280b3fd47SPaul E. McKenney 
343f6a12f34SPaul E. McKenney 	struct mutex exp_mutex;			/* Serialize expedited GP. */
3443b5f668eSPaul E. McKenney 	struct mutex exp_wake_mutex;		/* Serialize wakeup. */
345d6ada2cfSPaul E. McKenney 	unsigned long expedited_sequence;	/* Take a ticket. */
3463a6d7c64SPeter Zijlstra 	atomic_t expedited_need_qs;		/* # CPUs left to check in. */
347abedf8e2SPaul Gortmaker 	struct swait_queue_head expedited_wq;	/* Wait for check-ins. */
348b9585e94SPaul E. McKenney 	int ncpus_snap;				/* # CPUs seen last time. */
349b2b00ddfSPaul E. McKenney 	u8 cbovld;				/* Callback overload now? */
350b2b00ddfSPaul E. McKenney 	u8 cbovldnext;				/* ^        ^  next time? */
3514102adabSPaul E. McKenney 
3524102adabSPaul E. McKenney 	unsigned long jiffies_force_qs;		/* Time at which to invoke */
3534102adabSPaul E. McKenney 						/*  force_quiescent_state(). */
3548c7c4829SPaul E. McKenney 	unsigned long jiffies_kick_kthreads;	/* Time at which to kick */
3558c7c4829SPaul E. McKenney 						/*  kthreads, if configured. */
3564102adabSPaul E. McKenney 	unsigned long n_force_qs;		/* Number of calls to */
3574102adabSPaul E. McKenney 						/*  force_quiescent_state(). */
3584102adabSPaul E. McKenney 	unsigned long gp_start;			/* Time at which GP started, */
3594102adabSPaul E. McKenney 						/*  but in jiffies. */
360c51d7b5eSPaul E. McKenney 	unsigned long gp_end;			/* Time last GP ended, again */
361c51d7b5eSPaul E. McKenney 						/*  in jiffies. */
3626ccd2ecdSPaul E. McKenney 	unsigned long gp_activity;		/* Time of last GP kthread */
3636ccd2ecdSPaul E. McKenney 						/*  activity in jiffies. */
36426d950a9SPaul E. McKenney 	unsigned long gp_req_activity;		/* Time of last GP request */
36526d950a9SPaul E. McKenney 						/*  in jiffies. */
3664102adabSPaul E. McKenney 	unsigned long jiffies_stall;		/* Time at which to check */
3674102adabSPaul E. McKenney 						/*  for CPU stalls. */
3686193c76aSPaul E. McKenney 	unsigned long jiffies_resched;		/* Time at which to resched */
3696193c76aSPaul E. McKenney 						/*  a reluctant CPU. */
370fc908ed3SPaul E. McKenney 	unsigned long n_force_qs_gpstart;	/* Snapshot of n_force_qs at */
371fc908ed3SPaul E. McKenney 						/*  GP start. */
3724102adabSPaul E. McKenney 	const char *name;			/* Name of structure. */
3734102adabSPaul E. McKenney 	char abbr;				/* Abbreviated name. */
3741e64b15aSPaul E. McKenney 
37582980b16SDavid Woodhouse 	arch_spinlock_t ofl_lock ____cacheline_internodealigned_in_smp;
3761e64b15aSPaul E. McKenney 						/* Synchronize offline with */
3771e64b15aSPaul E. McKenney 						/*  GP pre-initialization. */
3788d2aaa9bSFrederic Weisbecker 	int nocb_is_setup;			/* nocb is setup from boot */
3794102adabSPaul E. McKenney };
3804102adabSPaul E. McKenney 
3814102adabSPaul E. McKenney /* Values for rcu_state structure's gp_flags field. */
3824102adabSPaul E. McKenney #define RCU_GP_FLAG_INIT 0x1	/* Need grace-period initialization. */
3834102adabSPaul E. McKenney #define RCU_GP_FLAG_FQS  0x2	/* Need grace-period quiescent-state forcing. */
3841fca4d12SPaul E. McKenney #define RCU_GP_FLAG_OVLD 0x4	/* Experiencing callback overload. */
3854102adabSPaul E. McKenney 
386c34d2f41SPaul E. McKenney /* Values for rcu_state structure's gp_state field. */
38777f81fe0SPetr Mladek #define RCU_GP_IDLE	 0	/* Initial state and no GP in progress. */
388afea227fSPaul E. McKenney #define RCU_GP_WAIT_GPS  1	/* Wait for grace-period start. */
389319362c9SPaul E. McKenney #define RCU_GP_DONE_GPS  2	/* Wait done for grace-period start. */
390fea3f222SPaul E. McKenney #define RCU_GP_ONOFF     3	/* Grace-period initialization hotplug. */
391fea3f222SPaul E. McKenney #define RCU_GP_INIT      4	/* Grace-period initialization. */
392fea3f222SPaul E. McKenney #define RCU_GP_WAIT_FQS  5	/* Wait for force-quiescent-state time. */
393fea3f222SPaul E. McKenney #define RCU_GP_DOING_FQS 6	/* Wait done for force-quiescent-state time. */
394fea3f222SPaul E. McKenney #define RCU_GP_CLEANUP   7	/* Grace-period cleanup started. */
395fea3f222SPaul E. McKenney #define RCU_GP_CLEANED   8	/* Grace-period cleanup complete. */
396afea227fSPaul E. McKenney 
397358be2d3SPaul E. McKenney /*
398358be2d3SPaul E. McKenney  * In order to export the rcu_state name to the tracing tools, it
399358be2d3SPaul E. McKenney  * needs to be added in the __tracepoint_string section.
400358be2d3SPaul E. McKenney  * This requires defining a separate variable tp_<sname>_varname
401358be2d3SPaul E. McKenney  * that points to the string being used, and this will allow
402358be2d3SPaul E. McKenney  * the tracing userspace tools to be able to decipher the string
403358be2d3SPaul E. McKenney  * address to the matching string.
404358be2d3SPaul E. McKenney  */
405358be2d3SPaul E. McKenney #ifdef CONFIG_PREEMPT_RCU
406358be2d3SPaul E. McKenney #define RCU_ABBR 'p'
407358be2d3SPaul E. McKenney #define RCU_NAME_RAW "rcu_preempt"
408358be2d3SPaul E. McKenney #else /* #ifdef CONFIG_PREEMPT_RCU */
409358be2d3SPaul E. McKenney #define RCU_ABBR 's'
410358be2d3SPaul E. McKenney #define RCU_NAME_RAW "rcu_sched"
411358be2d3SPaul E. McKenney #endif /* #else #ifdef CONFIG_PREEMPT_RCU */
412358be2d3SPaul E. McKenney #ifndef CONFIG_TRACING
413358be2d3SPaul E. McKenney #define RCU_NAME RCU_NAME_RAW
414358be2d3SPaul E. McKenney #else /* #ifdef CONFIG_TRACING */
415358be2d3SPaul E. McKenney static char rcu_name[] = RCU_NAME_RAW;
416358be2d3SPaul E. McKenney static const char *tp_rcu_varname __used __tracepoint_string = rcu_name;
417358be2d3SPaul E. McKenney #define RCU_NAME rcu_name
418358be2d3SPaul E. McKenney #endif /* #else #ifdef CONFIG_TRACING */
4196b50e119SPaul E. McKenney 
42032255d51SPaul E. McKenney /* Forward declarations for tree_plugin.h */
4214102adabSPaul E. McKenney static void rcu_bootup_announce(void);
42245975c7dSPaul E. McKenney static void rcu_qs(void);
4234102adabSPaul E. McKenney static int rcu_preempt_blocked_readers_cgp(struct rcu_node *rnp);
4244102adabSPaul E. McKenney #ifdef CONFIG_HOTPLUG_CPU
4258af3a5e7SPaul E. McKenney static bool rcu_preempt_has_tasks(struct rcu_node *rnp);
4264102adabSPaul E. McKenney #endif /* #ifdef CONFIG_HOTPLUG_CPU */
42774611ecbSPaul E. McKenney static int rcu_print_task_exp_stall(struct rcu_node *rnp);
42881ab59a3SPaul E. McKenney static void rcu_preempt_check_blocked_tasks(struct rcu_node *rnp);
429c98cac60SPaul E. McKenney static void rcu_flavor_sched_clock_irq(int user);
43081ab59a3SPaul E. McKenney static void dump_blkd_tasks(struct rcu_node *rnp, int ncheck);
4314102adabSPaul E. McKenney static void rcu_initiate_boost(struct rcu_node *rnp, unsigned long flags);
4324102adabSPaul E. McKenney static void rcu_preempt_boost_start_gp(struct rcu_node *rnp);
43351038506SZqiang static bool rcu_is_callbacks_kthread(struct rcu_data *rdp);
43448d07c04SSebastian Andrzej Siewior static void rcu_cpu_kthread_setup(unsigned int cpu);
4353ef5a1c3SPaul E. McKenney static void rcu_spawn_one_boost_kthread(struct rcu_node *rnp);
4360aa04b05SPaul E. McKenney static bool rcu_preempt_has_tasks(struct rcu_node *rnp);
4373e310098SPaul E. McKenney static bool rcu_preempt_need_deferred_qs(struct task_struct *t);
4384102adabSPaul E. McKenney static void zero_cpu_stall_ticks(struct rcu_data *rdp);
439abedf8e2SPaul Gortmaker static struct swait_queue_head *rcu_nocb_gp_get(struct rcu_node *rnp);
440abedf8e2SPaul Gortmaker static void rcu_nocb_gp_cleanup(struct swait_queue_head *sq);
4414102adabSPaul E. McKenney static void rcu_init_one_nocb(struct rcu_node *rnp);
442*b8f7aca3SFrederic Weisbecker static bool wake_nocb_gp(struct rcu_data *rdp, bool force);
443d1b222c6SPaul E. McKenney static bool rcu_nocb_flush_bypass(struct rcu_data *rdp, struct rcu_head *rhp,
444d1b222c6SPaul E. McKenney 				  unsigned long j);
445d1b222c6SPaul E. McKenney static bool rcu_nocb_try_bypass(struct rcu_data *rdp, struct rcu_head *rhp,
446d1b222c6SPaul E. McKenney 				bool *was_alldone, unsigned long flags);
4475d6742b3SPaul E. McKenney static void __call_rcu_nocb_wake(struct rcu_data *rdp, bool was_empty,
44896d3fd0dSPaul E. McKenney 				 unsigned long flags);
44987090516SFrederic Weisbecker static int rcu_nocb_need_deferred_wakeup(struct rcu_data *rdp, int level);
450f8bb5caeSFrederic Weisbecker static bool do_nocb_deferred_wakeup(struct rcu_data *rdp);
4514102adabSPaul E. McKenney static void rcu_boot_init_nocb_percpu_data(struct rcu_data *rdp);
452ad368d15SPaul E. McKenney static void rcu_spawn_cpu_nocb_kthread(int cpu);
453f7a81b12SPaul E. McKenney static void show_rcu_nocb_state(struct rcu_data *rdp);
4545d6742b3SPaul E. McKenney static void rcu_nocb_lock(struct rcu_data *rdp);
4555d6742b3SPaul E. McKenney static void rcu_nocb_unlock(struct rcu_data *rdp);
4565d6742b3SPaul E. McKenney static void rcu_nocb_unlock_irqrestore(struct rcu_data *rdp,
4575d6742b3SPaul E. McKenney 				       unsigned long flags);
458d1b222c6SPaul E. McKenney static void rcu_lockdep_assert_cblist_protected(struct rcu_data *rdp);
45935ce7f29SPaul E. McKenney #ifdef CONFIG_RCU_NOCB_CPU
4604580b054SPaul E. McKenney static void __init rcu_organize_nocb_kthreads(void);
461118e0d4aSFrederic Weisbecker 
462118e0d4aSFrederic Weisbecker /*
463118e0d4aSFrederic Weisbecker  * Disable IRQs before checking offloaded state so that local
464118e0d4aSFrederic Weisbecker  * locking is safe against concurrent de-offloading.
465118e0d4aSFrederic Weisbecker  */
46681c0b3d7SPaul E. McKenney #define rcu_nocb_lock_irqsave(rdp, flags)			\
46781c0b3d7SPaul E. McKenney do {								\
46881c0b3d7SPaul E. McKenney 	local_irq_save(flags);					\
469118e0d4aSFrederic Weisbecker 	if (rcu_segcblist_is_offloaded(&(rdp)->cblist))	\
470118e0d4aSFrederic Weisbecker 		raw_spin_lock(&(rdp)->nocb_lock);		\
47181c0b3d7SPaul E. McKenney } while (0)
47281c0b3d7SPaul E. McKenney #else /* #ifdef CONFIG_RCU_NOCB_CPU */
47381c0b3d7SPaul E. McKenney #define rcu_nocb_lock_irqsave(rdp, flags) local_irq_save(flags)
47481c0b3d7SPaul E. McKenney #endif /* #else #ifdef CONFIG_RCU_NOCB_CPU */
47581c0b3d7SPaul E. McKenney 
4764102adabSPaul E. McKenney static void rcu_bind_gp_kthread(void);
4774580b054SPaul E. McKenney static bool rcu_nohz_full_cpu(void);
47832255d51SPaul E. McKenney 
47932255d51SPaul E. McKenney /* Forward declarations for tree_stall.h */
48032255d51SPaul E. McKenney static void record_gp_stall_check_time(void);
4817ac1907cSPaul E. McKenney static void rcu_iw_handler(struct irq_work *iwp);
48232255d51SPaul E. McKenney static void check_cpu_stall(struct rcu_data *rdp);
483b51bcbbfSPaul E. McKenney static void rcu_check_gp_start_stall(struct rcu_node *rnp, struct rcu_data *rdp,
484b51bcbbfSPaul E. McKenney 				     const unsigned long gpssdelay);
485d96c52feSPaul E. McKenney 
486d96c52feSPaul E. McKenney /* Forward declarations for tree_exp.h. */
487d96c52feSPaul E. McKenney static void sync_rcu_do_polled_gp(struct work_struct *wp);
488