xref: /openbmc/linux/kernel/rcu/rcu.h (revision 7d0c9c50c5a109acd7a5cf589fc5563f9ef7149a)
1b5b11890SPaul E. McKenney /* SPDX-License-Identifier: GPL-2.0+ */
24102adabSPaul E. McKenney /*
34102adabSPaul E. McKenney  * Read-Copy Update definitions shared among RCU implementations.
44102adabSPaul E. McKenney  *
54102adabSPaul E. McKenney  * Copyright IBM Corporation, 2011
64102adabSPaul E. McKenney  *
7b5b11890SPaul E. McKenney  * Author: Paul E. McKenney <paulmck@linux.ibm.com>
84102adabSPaul E. McKenney  */
94102adabSPaul E. McKenney 
104102adabSPaul E. McKenney #ifndef __LINUX_RCU_H
114102adabSPaul E. McKenney #define __LINUX_RCU_H
124102adabSPaul E. McKenney 
135cb5c6e1SPaul Gortmaker #include <trace/events/rcu.h>
144102adabSPaul E. McKenney 
15c2d8089dSPaul E. McKenney /* Offset to allow distinguishing irq vs. task-based idle entry/exit. */
1684585aa8SPaul E. McKenney #define DYNTICK_IRQ_NONIDLE	((LONG_MAX / 2) + 1)
176136d6e4SPaul E. McKenney 
182e8c28c2SPaul E. McKenney 
192e8c28c2SPaul E. McKenney /*
202e8c28c2SPaul E. McKenney  * Grace-period counter management.
212e8c28c2SPaul E. McKenney  */
222e8c28c2SPaul E. McKenney 
23f1ec57a4SPaul E. McKenney #define RCU_SEQ_CTR_SHIFT	2
24031aeee0SPaul E. McKenney #define RCU_SEQ_STATE_MASK	((1 << RCU_SEQ_CTR_SHIFT) - 1)
25031aeee0SPaul E. McKenney 
26031aeee0SPaul E. McKenney /*
27031aeee0SPaul E. McKenney  * Return the counter portion of a sequence number previously returned
28031aeee0SPaul E. McKenney  * by rcu_seq_snap() or rcu_seq_current().
29031aeee0SPaul E. McKenney  */
30031aeee0SPaul E. McKenney static inline unsigned long rcu_seq_ctr(unsigned long s)
31031aeee0SPaul E. McKenney {
32031aeee0SPaul E. McKenney 	return s >> RCU_SEQ_CTR_SHIFT;
33031aeee0SPaul E. McKenney }
34031aeee0SPaul E. McKenney 
35031aeee0SPaul E. McKenney /*
36031aeee0SPaul E. McKenney  * Return the state portion of a sequence number previously returned
37031aeee0SPaul E. McKenney  * by rcu_seq_snap() or rcu_seq_current().
38031aeee0SPaul E. McKenney  */
39031aeee0SPaul E. McKenney static inline int rcu_seq_state(unsigned long s)
40031aeee0SPaul E. McKenney {
41031aeee0SPaul E. McKenney 	return s & RCU_SEQ_STATE_MASK;
42031aeee0SPaul E. McKenney }
43031aeee0SPaul E. McKenney 
4480a7956fSPaul E. McKenney /*
4580a7956fSPaul E. McKenney  * Set the state portion of the pointed-to sequence number.
4680a7956fSPaul E. McKenney  * The caller is responsible for preventing conflicting updates.
4780a7956fSPaul E. McKenney  */
4880a7956fSPaul E. McKenney static inline void rcu_seq_set_state(unsigned long *sp, int newstate)
4980a7956fSPaul E. McKenney {
5080a7956fSPaul E. McKenney 	WARN_ON_ONCE(newstate & ~RCU_SEQ_STATE_MASK);
5180a7956fSPaul E. McKenney 	WRITE_ONCE(*sp, (*sp & ~RCU_SEQ_STATE_MASK) + newstate);
5280a7956fSPaul E. McKenney }
5380a7956fSPaul E. McKenney 
542e8c28c2SPaul E. McKenney /* Adjust sequence number for start of update-side operation. */
552e8c28c2SPaul E. McKenney static inline void rcu_seq_start(unsigned long *sp)
562e8c28c2SPaul E. McKenney {
572e8c28c2SPaul E. McKenney 	WRITE_ONCE(*sp, *sp + 1);
582e8c28c2SPaul E. McKenney 	smp_mb(); /* Ensure update-side operation after counter increment. */
59031aeee0SPaul E. McKenney 	WARN_ON_ONCE(rcu_seq_state(*sp) != 1);
602e8c28c2SPaul E. McKenney }
612e8c28c2SPaul E. McKenney 
629a414201SPaul E. McKenney /* Compute the end-of-grace-period value for the specified sequence number. */
639a414201SPaul E. McKenney static inline unsigned long rcu_seq_endval(unsigned long *sp)
649a414201SPaul E. McKenney {
659a414201SPaul E. McKenney 	return (*sp | RCU_SEQ_STATE_MASK) + 1;
669a414201SPaul E. McKenney }
679a414201SPaul E. McKenney 
682e8c28c2SPaul E. McKenney /* Adjust sequence number for end of update-side operation. */
692e8c28c2SPaul E. McKenney static inline void rcu_seq_end(unsigned long *sp)
702e8c28c2SPaul E. McKenney {
712e8c28c2SPaul E. McKenney 	smp_mb(); /* Ensure update-side operation before counter increment. */
72031aeee0SPaul E. McKenney 	WARN_ON_ONCE(!rcu_seq_state(*sp));
739a414201SPaul E. McKenney 	WRITE_ONCE(*sp, rcu_seq_endval(sp));
742e8c28c2SPaul E. McKenney }
752e8c28c2SPaul E. McKenney 
760d805a70SJoel Fernandes (Google) /*
770d805a70SJoel Fernandes (Google)  * rcu_seq_snap - Take a snapshot of the update side's sequence number.
780d805a70SJoel Fernandes (Google)  *
790d805a70SJoel Fernandes (Google)  * This function returns the earliest value of the grace-period sequence number
800d805a70SJoel Fernandes (Google)  * that will indicate that a full grace period has elapsed since the current
810d805a70SJoel Fernandes (Google)  * time.  Once the grace-period sequence number has reached this value, it will
820d805a70SJoel Fernandes (Google)  * be safe to invoke all callbacks that have been registered prior to the
830d805a70SJoel Fernandes (Google)  * current time. This value is the current grace-period number plus two to the
840d805a70SJoel Fernandes (Google)  * power of the number of low-order bits reserved for state, then rounded up to
850d805a70SJoel Fernandes (Google)  * the next value in which the state bits are all zero.
860d805a70SJoel Fernandes (Google)  */
872e8c28c2SPaul E. McKenney static inline unsigned long rcu_seq_snap(unsigned long *sp)
882e8c28c2SPaul E. McKenney {
892e8c28c2SPaul E. McKenney 	unsigned long s;
902e8c28c2SPaul E. McKenney 
91031aeee0SPaul E. McKenney 	s = (READ_ONCE(*sp) + 2 * RCU_SEQ_STATE_MASK + 1) & ~RCU_SEQ_STATE_MASK;
922e8c28c2SPaul E. McKenney 	smp_mb(); /* Above access must not bleed into critical section. */
932e8c28c2SPaul E. McKenney 	return s;
942e8c28c2SPaul E. McKenney }
952e8c28c2SPaul E. McKenney 
968660b7d8SPaul E. McKenney /* Return the current value the update side's sequence number, no ordering. */
978660b7d8SPaul E. McKenney static inline unsigned long rcu_seq_current(unsigned long *sp)
988660b7d8SPaul E. McKenney {
998660b7d8SPaul E. McKenney 	return READ_ONCE(*sp);
1008660b7d8SPaul E. McKenney }
1018660b7d8SPaul E. McKenney 
1022e8c28c2SPaul E. McKenney /*
1032e3e5e55SPaul E. McKenney  * Given a snapshot from rcu_seq_snap(), determine whether or not the
1042e3e5e55SPaul E. McKenney  * corresponding update-side operation has started.
1052e3e5e55SPaul E. McKenney  */
1062e3e5e55SPaul E. McKenney static inline bool rcu_seq_started(unsigned long *sp, unsigned long s)
1072e3e5e55SPaul E. McKenney {
1082e3e5e55SPaul E. McKenney 	return ULONG_CMP_LT((s - 1) & ~RCU_SEQ_STATE_MASK, READ_ONCE(*sp));
1092e3e5e55SPaul E. McKenney }
1102e3e5e55SPaul E. McKenney 
1112e3e5e55SPaul E. McKenney /*
1122e8c28c2SPaul E. McKenney  * Given a snapshot from rcu_seq_snap(), determine whether or not a
1132e8c28c2SPaul E. McKenney  * full update-side operation has occurred.
1142e8c28c2SPaul E. McKenney  */
1152e8c28c2SPaul E. McKenney static inline bool rcu_seq_done(unsigned long *sp, unsigned long s)
1162e8c28c2SPaul E. McKenney {
1172e8c28c2SPaul E. McKenney 	return ULONG_CMP_GE(READ_ONCE(*sp), s);
1182e8c28c2SPaul E. McKenney }
1192e8c28c2SPaul E. McKenney 
1204102adabSPaul E. McKenney /*
12167e14c1eSPaul E. McKenney  * Has a grace period completed since the time the old gp_seq was collected?
12267e14c1eSPaul E. McKenney  */
12367e14c1eSPaul E. McKenney static inline bool rcu_seq_completed_gp(unsigned long old, unsigned long new)
12467e14c1eSPaul E. McKenney {
12567e14c1eSPaul E. McKenney 	return ULONG_CMP_LT(old, new & ~RCU_SEQ_STATE_MASK);
12667e14c1eSPaul E. McKenney }
12767e14c1eSPaul E. McKenney 
12867e14c1eSPaul E. McKenney /*
12967e14c1eSPaul E. McKenney  * Has a grace period started since the time the old gp_seq was collected?
13067e14c1eSPaul E. McKenney  */
13167e14c1eSPaul E. McKenney static inline bool rcu_seq_new_gp(unsigned long old, unsigned long new)
13267e14c1eSPaul E. McKenney {
13367e14c1eSPaul E. McKenney 	return ULONG_CMP_LT((old + RCU_SEQ_STATE_MASK) & ~RCU_SEQ_STATE_MASK,
13467e14c1eSPaul E. McKenney 			    new);
13567e14c1eSPaul E. McKenney }
13667e14c1eSPaul E. McKenney 
13767e14c1eSPaul E. McKenney /*
138d7219312SPaul E. McKenney  * Roughly how many full grace periods have elapsed between the collection
139d7219312SPaul E. McKenney  * of the two specified grace periods?
140d7219312SPaul E. McKenney  */
141d7219312SPaul E. McKenney static inline unsigned long rcu_seq_diff(unsigned long new, unsigned long old)
142d7219312SPaul E. McKenney {
1432ee5aca5SPaul E. McKenney 	unsigned long rnd_diff;
1442ee5aca5SPaul E. McKenney 
1452ee5aca5SPaul E. McKenney 	if (old == new)
1462ee5aca5SPaul E. McKenney 		return 0;
1472ee5aca5SPaul E. McKenney 	/*
1482ee5aca5SPaul E. McKenney 	 * Compute the number of grace periods (still shifted up), plus
1492ee5aca5SPaul E. McKenney 	 * one if either of new and old is not an exact grace period.
1502ee5aca5SPaul E. McKenney 	 */
1512ee5aca5SPaul E. McKenney 	rnd_diff = (new & ~RCU_SEQ_STATE_MASK) -
1522ee5aca5SPaul E. McKenney 		   ((old + RCU_SEQ_STATE_MASK) & ~RCU_SEQ_STATE_MASK) +
1532ee5aca5SPaul E. McKenney 		   ((new & RCU_SEQ_STATE_MASK) || (old & RCU_SEQ_STATE_MASK));
1542ee5aca5SPaul E. McKenney 	if (ULONG_CMP_GE(RCU_SEQ_STATE_MASK, rnd_diff))
1552ee5aca5SPaul E. McKenney 		return 1; /* Definitely no grace period has elapsed. */
1562ee5aca5SPaul E. McKenney 	return ((rnd_diff - RCU_SEQ_STATE_MASK - 1) >> RCU_SEQ_CTR_SHIFT) + 2;
157d7219312SPaul E. McKenney }
158d7219312SPaul E. McKenney 
159d7219312SPaul E. McKenney /*
1604102adabSPaul E. McKenney  * debug_rcu_head_queue()/debug_rcu_head_unqueue() are used internally
1617f87c036SPaul E. McKenney  * by call_rcu() and rcu callback execution, and are therefore not part
1627f87c036SPaul E. McKenney  * of the RCU API. These are in rcupdate.h because they are used by all
1637f87c036SPaul E. McKenney  * RCU implementations.
1644102adabSPaul E. McKenney  */
1654102adabSPaul E. McKenney 
1664102adabSPaul E. McKenney #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD
1674102adabSPaul E. McKenney # define STATE_RCU_HEAD_READY	0
1684102adabSPaul E. McKenney # define STATE_RCU_HEAD_QUEUED	1
1694102adabSPaul E. McKenney 
1704102adabSPaul E. McKenney extern struct debug_obj_descr rcuhead_debug_descr;
1714102adabSPaul E. McKenney 
1724102adabSPaul E. McKenney static inline int debug_rcu_head_queue(struct rcu_head *head)
1734102adabSPaul E. McKenney {
1744102adabSPaul E. McKenney 	int r1;
1754102adabSPaul E. McKenney 
1764102adabSPaul E. McKenney 	r1 = debug_object_activate(head, &rcuhead_debug_descr);
1774102adabSPaul E. McKenney 	debug_object_active_state(head, &rcuhead_debug_descr,
1784102adabSPaul E. McKenney 				  STATE_RCU_HEAD_READY,
1794102adabSPaul E. McKenney 				  STATE_RCU_HEAD_QUEUED);
1804102adabSPaul E. McKenney 	return r1;
1814102adabSPaul E. McKenney }
1824102adabSPaul E. McKenney 
1834102adabSPaul E. McKenney static inline void debug_rcu_head_unqueue(struct rcu_head *head)
1844102adabSPaul E. McKenney {
1854102adabSPaul E. McKenney 	debug_object_active_state(head, &rcuhead_debug_descr,
1864102adabSPaul E. McKenney 				  STATE_RCU_HEAD_QUEUED,
1874102adabSPaul E. McKenney 				  STATE_RCU_HEAD_READY);
1884102adabSPaul E. McKenney 	debug_object_deactivate(head, &rcuhead_debug_descr);
1894102adabSPaul E. McKenney }
1904102adabSPaul E. McKenney #else	/* !CONFIG_DEBUG_OBJECTS_RCU_HEAD */
1914102adabSPaul E. McKenney static inline int debug_rcu_head_queue(struct rcu_head *head)
1924102adabSPaul E. McKenney {
1934102adabSPaul E. McKenney 	return 0;
1944102adabSPaul E. McKenney }
1954102adabSPaul E. McKenney 
1964102adabSPaul E. McKenney static inline void debug_rcu_head_unqueue(struct rcu_head *head)
1974102adabSPaul E. McKenney {
1984102adabSPaul E. McKenney }
1994102adabSPaul E. McKenney #endif	/* #else !CONFIG_DEBUG_OBJECTS_RCU_HEAD */
2004102adabSPaul E. McKenney 
20158c53360SPaul E. McKenney extern int rcu_cpu_stall_suppress_at_boot;
20258c53360SPaul E. McKenney 
20358c53360SPaul E. McKenney static inline bool rcu_stall_is_suppressed_at_boot(void)
20458c53360SPaul E. McKenney {
20558c53360SPaul E. McKenney 	return rcu_cpu_stall_suppress_at_boot && !rcu_inkernel_boot_has_ended();
20658c53360SPaul E. McKenney }
20758c53360SPaul E. McKenney 
2084102adabSPaul E. McKenney #ifdef CONFIG_RCU_STALL_COMMON
2094102adabSPaul E. McKenney 
210cdc694b2SPaul E. McKenney extern int rcu_cpu_stall_ftrace_dump;
2114102adabSPaul E. McKenney extern int rcu_cpu_stall_suppress;
21210462d6fSPaul E. McKenney extern int rcu_cpu_stall_timeout;
2134102adabSPaul E. McKenney int rcu_jiffies_till_stall_check(void);
2144102adabSPaul E. McKenney 
21558c53360SPaul E. McKenney static inline bool rcu_stall_is_suppressed(void)
21658c53360SPaul E. McKenney {
21758c53360SPaul E. McKenney 	return rcu_stall_is_suppressed_at_boot() || rcu_cpu_stall_suppress;
21858c53360SPaul E. McKenney }
21958c53360SPaul E. McKenney 
220f22ce091SPaul E. McKenney #define rcu_ftrace_dump_stall_suppress() \
221f22ce091SPaul E. McKenney do { \
222f22ce091SPaul E. McKenney 	if (!rcu_cpu_stall_suppress) \
223f22ce091SPaul E. McKenney 		rcu_cpu_stall_suppress = 3; \
224f22ce091SPaul E. McKenney } while (0)
225f22ce091SPaul E. McKenney 
226f22ce091SPaul E. McKenney #define rcu_ftrace_dump_stall_unsuppress() \
227f22ce091SPaul E. McKenney do { \
228f22ce091SPaul E. McKenney 	if (rcu_cpu_stall_suppress == 3) \
229f22ce091SPaul E. McKenney 		rcu_cpu_stall_suppress = 0; \
230f22ce091SPaul E. McKenney } while (0)
231f22ce091SPaul E. McKenney 
232f22ce091SPaul E. McKenney #else /* #endif #ifdef CONFIG_RCU_STALL_COMMON */
23358c53360SPaul E. McKenney 
23458c53360SPaul E. McKenney static inline bool rcu_stall_is_suppressed(void)
23558c53360SPaul E. McKenney {
23658c53360SPaul E. McKenney 	return rcu_stall_is_suppressed_at_boot();
23758c53360SPaul E. McKenney }
238f22ce091SPaul E. McKenney #define rcu_ftrace_dump_stall_suppress()
239f22ce091SPaul E. McKenney #define rcu_ftrace_dump_stall_unsuppress()
2404102adabSPaul E. McKenney #endif /* #ifdef CONFIG_RCU_STALL_COMMON */
2414102adabSPaul E. McKenney 
2424102adabSPaul E. McKenney /*
2434102adabSPaul E. McKenney  * Strings used in tracepoints need to be exported via the
2444102adabSPaul E. McKenney  * tracing system such that tools like perf and trace-cmd can
2454102adabSPaul E. McKenney  * translate the string address pointers to actual text.
2464102adabSPaul E. McKenney  */
2474102adabSPaul E. McKenney #define TPS(x)  tracepoint_string(x)
2484102adabSPaul E. McKenney 
249b8989b76SPaul E. McKenney /*
250b8989b76SPaul E. McKenney  * Dump the ftrace buffer, but only one time per callsite per boot.
251b8989b76SPaul E. McKenney  */
252b8989b76SPaul E. McKenney #define rcu_ftrace_dump(oops_dump_mode) \
253b8989b76SPaul E. McKenney do { \
254b8989b76SPaul E. McKenney 	static atomic_t ___rfd_beenhere = ATOMIC_INIT(0); \
255b8989b76SPaul E. McKenney 	\
256b8989b76SPaul E. McKenney 	if (!atomic_read(&___rfd_beenhere) && \
25783b6ca1fSPaul E. McKenney 	    !atomic_xchg(&___rfd_beenhere, 1)) { \
25883b6ca1fSPaul E. McKenney 		tracing_off(); \
259f22ce091SPaul E. McKenney 		rcu_ftrace_dump_stall_suppress(); \
260b8989b76SPaul E. McKenney 		ftrace_dump(oops_dump_mode); \
261f22ce091SPaul E. McKenney 		rcu_ftrace_dump_stall_unsuppress(); \
26283b6ca1fSPaul E. McKenney 	} \
263b8989b76SPaul E. McKenney } while (0)
264b8989b76SPaul E. McKenney 
265aa23c6fbSPranith Kumar void rcu_early_boot_tests(void);
26652d7e48bSPaul E. McKenney void rcu_test_sync_prims(void);
267aa23c6fbSPranith Kumar 
2685f6130faSLai Jiangshan /*
2695f6130faSLai Jiangshan  * This function really isn't for public consumption, but RCU is special in
2705f6130faSLai Jiangshan  * that context switches can allow the state machine to make progress.
2715f6130faSLai Jiangshan  */
2725f6130faSLai Jiangshan extern void resched_cpu(int cpu);
2735f6130faSLai Jiangshan 
274822175e7SLai Jiangshan #if defined(CONFIG_SRCU) || !defined(CONFIG_TINY_RCU)
2752b34c43cSPaul E. McKenney 
2762b34c43cSPaul E. McKenney #include <linux/rcu_node_tree.h>
2772b34c43cSPaul E. McKenney 
2782b34c43cSPaul E. McKenney extern int rcu_num_lvls;
279e95d68d2SPaul E. McKenney extern int num_rcu_lvl[];
2802b34c43cSPaul E. McKenney extern int rcu_num_nodes;
2812b34c43cSPaul E. McKenney static bool rcu_fanout_exact;
2822b34c43cSPaul E. McKenney static int rcu_fanout_leaf;
2832b34c43cSPaul E. McKenney 
2842b34c43cSPaul E. McKenney /*
2852b34c43cSPaul E. McKenney  * Compute the per-level fanout, either using the exact fanout specified
2862b34c43cSPaul E. McKenney  * or balancing the tree, depending on the rcu_fanout_exact boot parameter.
2872b34c43cSPaul E. McKenney  */
2882b34c43cSPaul E. McKenney static inline void rcu_init_levelspread(int *levelspread, const int *levelcnt)
2892b34c43cSPaul E. McKenney {
2902b34c43cSPaul E. McKenney 	int i;
2912b34c43cSPaul E. McKenney 
29236b5dae6SPaul E. McKenney 	for (i = 0; i < RCU_NUM_LVLS; i++)
29336b5dae6SPaul E. McKenney 		levelspread[i] = INT_MIN;
2942b34c43cSPaul E. McKenney 	if (rcu_fanout_exact) {
2952b34c43cSPaul E. McKenney 		levelspread[rcu_num_lvls - 1] = rcu_fanout_leaf;
2962b34c43cSPaul E. McKenney 		for (i = rcu_num_lvls - 2; i >= 0; i--)
2972b34c43cSPaul E. McKenney 			levelspread[i] = RCU_FANOUT;
2982b34c43cSPaul E. McKenney 	} else {
2992b34c43cSPaul E. McKenney 		int ccur;
3002b34c43cSPaul E. McKenney 		int cprv;
3012b34c43cSPaul E. McKenney 
3022b34c43cSPaul E. McKenney 		cprv = nr_cpu_ids;
3032b34c43cSPaul E. McKenney 		for (i = rcu_num_lvls - 1; i >= 0; i--) {
3042b34c43cSPaul E. McKenney 			ccur = levelcnt[i];
3052b34c43cSPaul E. McKenney 			levelspread[i] = (cprv + ccur - 1) / ccur;
3062b34c43cSPaul E. McKenney 			cprv = ccur;
3072b34c43cSPaul E. McKenney 		}
3082b34c43cSPaul E. McKenney 	}
3092b34c43cSPaul E. McKenney }
3102b34c43cSPaul E. McKenney 
3117f87c036SPaul E. McKenney /* Returns a pointer to the first leaf rcu_node structure. */
312aedf4ba9SPaul E. McKenney #define rcu_first_leaf_node() (rcu_state.level[rcu_num_lvls - 1])
3135b4c11d5SPaul E. McKenney 
3145b4c11d5SPaul E. McKenney /* Is this rcu_node a leaf? */
3155b4c11d5SPaul E. McKenney #define rcu_is_leaf_node(rnp) ((rnp)->level == rcu_num_lvls - 1)
3165b4c11d5SPaul E. McKenney 
3175257514dSPaul E. McKenney /* Is this rcu_node the last leaf? */
318aedf4ba9SPaul E. McKenney #define rcu_is_last_leaf_node(rnp) ((rnp) == &rcu_state.node[rcu_num_nodes - 1])
3195257514dSPaul E. McKenney 
320efbe451dSPaul E. McKenney /*
321aedf4ba9SPaul E. McKenney  * Do a full breadth-first scan of the {s,}rcu_node structures for the
3227f87c036SPaul E. McKenney  * specified state structure (for SRCU) or the only rcu_state structure
3237f87c036SPaul E. McKenney  * (for RCU).
324efbe451dSPaul E. McKenney  */
325aedf4ba9SPaul E. McKenney #define srcu_for_each_node_breadth_first(sp, rnp) \
326aedf4ba9SPaul E. McKenney 	for ((rnp) = &(sp)->node[0]; \
327aedf4ba9SPaul E. McKenney 	     (rnp) < &(sp)->node[rcu_num_nodes]; (rnp)++)
328aedf4ba9SPaul E. McKenney #define rcu_for_each_node_breadth_first(rnp) \
329aedf4ba9SPaul E. McKenney 	srcu_for_each_node_breadth_first(&rcu_state, rnp)
330efbe451dSPaul E. McKenney 
331efbe451dSPaul E. McKenney /*
3327f87c036SPaul E. McKenney  * Scan the leaves of the rcu_node hierarchy for the rcu_state structure.
3337f87c036SPaul E. McKenney  * Note that if there is a singleton rcu_node tree with but one rcu_node
3347f87c036SPaul E. McKenney  * structure, this loop -will- visit the rcu_node structure.  It is still
3357f87c036SPaul E. McKenney  * a leaf node, even if it is also the root node.
336efbe451dSPaul E. McKenney  */
337aedf4ba9SPaul E. McKenney #define rcu_for_each_leaf_node(rnp) \
338aedf4ba9SPaul E. McKenney 	for ((rnp) = rcu_first_leaf_node(); \
339aedf4ba9SPaul E. McKenney 	     (rnp) < &rcu_state.node[rcu_num_nodes]; (rnp)++)
340efbe451dSPaul E. McKenney 
341efbe451dSPaul E. McKenney /*
342efbe451dSPaul E. McKenney  * Iterate over all possible CPUs in a leaf RCU node.
343efbe451dSPaul E. McKenney  */
344efbe451dSPaul E. McKenney #define for_each_leaf_node_possible_cpu(rnp, cpu) \
34582dd8419SPaul E. McKenney 	for (WARN_ON_ONCE(!rcu_is_leaf_node(rnp)), \
34682dd8419SPaul E. McKenney 	     (cpu) = cpumask_next((rnp)->grplo - 1, cpu_possible_mask); \
34765963d24SPaul E. McKenney 	     (cpu) <= rnp->grphi; \
34865963d24SPaul E. McKenney 	     (cpu) = cpumask_next((cpu), cpu_possible_mask))
34965963d24SPaul E. McKenney 
35065963d24SPaul E. McKenney /*
35165963d24SPaul E. McKenney  * Iterate over all CPUs in a leaf RCU node's specified mask.
35265963d24SPaul E. McKenney  */
35365963d24SPaul E. McKenney #define rcu_find_next_bit(rnp, cpu, mask) \
35465963d24SPaul E. McKenney 	((rnp)->grplo + find_next_bit(&(mask), BITS_PER_LONG, (cpu)))
35565963d24SPaul E. McKenney #define for_each_leaf_node_cpu_mask(rnp, cpu, mask) \
35682dd8419SPaul E. McKenney 	for (WARN_ON_ONCE(!rcu_is_leaf_node(rnp)), \
35782dd8419SPaul E. McKenney 	     (cpu) = rcu_find_next_bit((rnp), 0, (mask)); \
35865963d24SPaul E. McKenney 	     (cpu) <= rnp->grphi; \
35965963d24SPaul E. McKenney 	     (cpu) = rcu_find_next_bit((rnp), (cpu) + 1 - (rnp->grplo), (mask)))
360efbe451dSPaul E. McKenney 
36183d40bd3SPaul E. McKenney /*
36283d40bd3SPaul E. McKenney  * Wrappers for the rcu_node::lock acquire and release.
36383d40bd3SPaul E. McKenney  *
36483d40bd3SPaul E. McKenney  * Because the rcu_nodes form a tree, the tree traversal locking will observe
36583d40bd3SPaul E. McKenney  * different lock values, this in turn means that an UNLOCK of one level
36683d40bd3SPaul E. McKenney  * followed by a LOCK of another level does not imply a full memory barrier;
36783d40bd3SPaul E. McKenney  * and most importantly transitivity is lost.
36883d40bd3SPaul E. McKenney  *
36983d40bd3SPaul E. McKenney  * In order to restore full ordering between tree levels, augment the regular
37083d40bd3SPaul E. McKenney  * lock acquire functions with smp_mb__after_unlock_lock().
37183d40bd3SPaul E. McKenney  *
37283d40bd3SPaul E. McKenney  * As ->lock of struct rcu_node is a __private field, therefore one should use
37383d40bd3SPaul E. McKenney  * these wrappers rather than directly call raw_spin_{lock,unlock}* on ->lock.
37483d40bd3SPaul E. McKenney  */
37583d40bd3SPaul E. McKenney #define raw_spin_lock_rcu_node(p)					\
37683d40bd3SPaul E. McKenney do {									\
37783d40bd3SPaul E. McKenney 	raw_spin_lock(&ACCESS_PRIVATE(p, lock));			\
37883d40bd3SPaul E. McKenney 	smp_mb__after_unlock_lock();					\
37983d40bd3SPaul E. McKenney } while (0)
38083d40bd3SPaul E. McKenney 
38183d40bd3SPaul E. McKenney #define raw_spin_unlock_rcu_node(p) raw_spin_unlock(&ACCESS_PRIVATE(p, lock))
38283d40bd3SPaul E. McKenney 
38383d40bd3SPaul E. McKenney #define raw_spin_lock_irq_rcu_node(p)					\
38483d40bd3SPaul E. McKenney do {									\
38583d40bd3SPaul E. McKenney 	raw_spin_lock_irq(&ACCESS_PRIVATE(p, lock));			\
38683d40bd3SPaul E. McKenney 	smp_mb__after_unlock_lock();					\
38783d40bd3SPaul E. McKenney } while (0)
38883d40bd3SPaul E. McKenney 
38983d40bd3SPaul E. McKenney #define raw_spin_unlock_irq_rcu_node(p)					\
39083d40bd3SPaul E. McKenney 	raw_spin_unlock_irq(&ACCESS_PRIVATE(p, lock))
39183d40bd3SPaul E. McKenney 
3924e4bea74SPaul E. McKenney #define raw_spin_lock_irqsave_rcu_node(p, flags)			\
39383d40bd3SPaul E. McKenney do {									\
3944e4bea74SPaul E. McKenney 	raw_spin_lock_irqsave(&ACCESS_PRIVATE(p, lock), flags);	\
39583d40bd3SPaul E. McKenney 	smp_mb__after_unlock_lock();					\
39683d40bd3SPaul E. McKenney } while (0)
39783d40bd3SPaul E. McKenney 
3984e4bea74SPaul E. McKenney #define raw_spin_unlock_irqrestore_rcu_node(p, flags)			\
399a32e01eeSMatthew Wilcox 	raw_spin_unlock_irqrestore(&ACCESS_PRIVATE(p, lock), flags)
40083d40bd3SPaul E. McKenney 
40183d40bd3SPaul E. McKenney #define raw_spin_trylock_rcu_node(p)					\
40283d40bd3SPaul E. McKenney ({									\
40383d40bd3SPaul E. McKenney 	bool ___locked = raw_spin_trylock(&ACCESS_PRIVATE(p, lock));	\
40483d40bd3SPaul E. McKenney 									\
40583d40bd3SPaul E. McKenney 	if (___locked)							\
40683d40bd3SPaul E. McKenney 		smp_mb__after_unlock_lock();				\
40783d40bd3SPaul E. McKenney 	___locked;							\
40883d40bd3SPaul E. McKenney })
40983d40bd3SPaul E. McKenney 
410a32e01eeSMatthew Wilcox #define raw_lockdep_assert_held_rcu_node(p)				\
411a32e01eeSMatthew Wilcox 	lockdep_assert_held(&ACCESS_PRIVATE(p, lock))
412a32e01eeSMatthew Wilcox 
413822175e7SLai Jiangshan #endif /* #if defined(CONFIG_SRCU) || !defined(CONFIG_TINY_RCU) */
4142b34c43cSPaul E. McKenney 
415e0fcba9aSPaul E. McKenney #ifdef CONFIG_SRCU
416e0fcba9aSPaul E. McKenney void srcu_init(void);
417e0fcba9aSPaul E. McKenney #else /* #ifdef CONFIG_SRCU */
418e0fcba9aSPaul E. McKenney static inline void srcu_init(void) { }
419e0fcba9aSPaul E. McKenney #endif /* #else #ifdef CONFIG_SRCU */
420e0fcba9aSPaul E. McKenney 
42125c36329SPaul E. McKenney #ifdef CONFIG_TINY_RCU
42225c36329SPaul E. McKenney /* Tiny RCU doesn't expedite, as its purpose in life is instead to be tiny. */
4237414fac0SPaul E. McKenney static inline bool rcu_gp_is_normal(void) { return true; }
4247414fac0SPaul E. McKenney static inline bool rcu_gp_is_expedited(void) { return false; }
4257414fac0SPaul E. McKenney static inline void rcu_expedite_gp(void) { }
4267414fac0SPaul E. McKenney static inline void rcu_unexpedite_gp(void) { }
427bfbd767dSPaul E. McKenney static inline void rcu_request_urgent_qs_task(struct task_struct *t) { }
42825c36329SPaul E. McKenney #else /* #ifdef CONFIG_TINY_RCU */
42925c36329SPaul E. McKenney bool rcu_gp_is_normal(void);     /* Internal RCU use. */
43025c36329SPaul E. McKenney bool rcu_gp_is_expedited(void);  /* Internal RCU use. */
43125c36329SPaul E. McKenney void rcu_expedite_gp(void);
43225c36329SPaul E. McKenney void rcu_unexpedite_gp(void);
43325c36329SPaul E. McKenney void rcupdate_announce_bootup_oddness(void);
434e21408ceSPaul E. McKenney void show_rcu_tasks_gp_kthreads(void);
435bfbd767dSPaul E. McKenney void rcu_request_urgent_qs_task(struct task_struct *t);
43625c36329SPaul E. McKenney #endif /* #else #ifdef CONFIG_TINY_RCU */
43725c36329SPaul E. McKenney 
43882118249SPaul E. McKenney #define RCU_SCHEDULER_INACTIVE	0
43982118249SPaul E. McKenney #define RCU_SCHEDULER_INIT	1
44082118249SPaul E. McKenney #define RCU_SCHEDULER_RUNNING	2
44182118249SPaul E. McKenney 
442cad7b389SPaul E. McKenney enum rcutorture_type {
443cad7b389SPaul E. McKenney 	RCU_FLAVOR,
444cad7b389SPaul E. McKenney 	RCU_TASKS_FLAVOR,
4453d6e43c7SPaul E. McKenney 	RCU_TASKS_RUDE_FLAVOR,
446c1a76c0bSPaul E. McKenney 	RCU_TASKS_TRACING_FLAVOR,
447c682db55SPaul E. McKenney 	RCU_TRIVIAL_FLAVOR,
448cad7b389SPaul E. McKenney 	SRCU_FLAVOR,
449cad7b389SPaul E. McKenney 	INVALID_RCU_FLAVOR
450cad7b389SPaul E. McKenney };
451cad7b389SPaul E. McKenney 
452b3e627d3SLai Jiangshan #if defined(CONFIG_TREE_RCU)
453cad7b389SPaul E. McKenney void rcutorture_get_gp_data(enum rcutorture_type test_type, int *flags,
454aebc8264SPaul E. McKenney 			    unsigned long *gp_seq);
455cad7b389SPaul E. McKenney void do_trace_rcu_torture_read(const char *rcutorturename,
456cad7b389SPaul E. McKenney 			       struct rcu_head *rhp,
457cad7b389SPaul E. McKenney 			       unsigned long secs,
458cad7b389SPaul E. McKenney 			       unsigned long c_old,
459cad7b389SPaul E. McKenney 			       unsigned long c);
460cad7b389SPaul E. McKenney #else
461cad7b389SPaul E. McKenney static inline void rcutorture_get_gp_data(enum rcutorture_type test_type,
462aebc8264SPaul E. McKenney 					  int *flags, unsigned long *gp_seq)
463cad7b389SPaul E. McKenney {
464cad7b389SPaul E. McKenney 	*flags = 0;
465aebc8264SPaul E. McKenney 	*gp_seq = 0;
466cad7b389SPaul E. McKenney }
467cad7b389SPaul E. McKenney #ifdef CONFIG_RCU_TRACE
468cad7b389SPaul E. McKenney void do_trace_rcu_torture_read(const char *rcutorturename,
469cad7b389SPaul E. McKenney 			       struct rcu_head *rhp,
470cad7b389SPaul E. McKenney 			       unsigned long secs,
471cad7b389SPaul E. McKenney 			       unsigned long c_old,
472cad7b389SPaul E. McKenney 			       unsigned long c);
473cad7b389SPaul E. McKenney #else
474cad7b389SPaul E. McKenney #define do_trace_rcu_torture_read(rcutorturename, rhp, secs, c_old, c) \
475cad7b389SPaul E. McKenney 	do { } while (0)
476cad7b389SPaul E. McKenney #endif
477cad7b389SPaul E. McKenney #endif
478cad7b389SPaul E. McKenney 
479c682db55SPaul E. McKenney #if IS_ENABLED(CONFIG_RCU_TORTURE_TEST) || IS_MODULE(CONFIG_RCU_TORTURE_TEST)
480c682db55SPaul E. McKenney long rcutorture_sched_setaffinity(pid_t pid, const struct cpumask *in_mask);
481c682db55SPaul E. McKenney #endif
482c682db55SPaul E. McKenney 
483cad7b389SPaul E. McKenney #ifdef CONFIG_TINY_SRCU
484cad7b389SPaul E. McKenney 
485cad7b389SPaul E. McKenney static inline void srcutorture_get_gp_data(enum rcutorture_type test_type,
486cad7b389SPaul E. McKenney 					   struct srcu_struct *sp, int *flags,
487aebc8264SPaul E. McKenney 					   unsigned long *gp_seq)
488cad7b389SPaul E. McKenney {
489cad7b389SPaul E. McKenney 	if (test_type != SRCU_FLAVOR)
490cad7b389SPaul E. McKenney 		return;
491cad7b389SPaul E. McKenney 	*flags = 0;
492aebc8264SPaul E. McKenney 	*gp_seq = sp->srcu_idx;
493cad7b389SPaul E. McKenney }
494cad7b389SPaul E. McKenney 
495cad7b389SPaul E. McKenney #elif defined(CONFIG_TREE_SRCU)
496cad7b389SPaul E. McKenney 
497cad7b389SPaul E. McKenney void srcutorture_get_gp_data(enum rcutorture_type test_type,
498cad7b389SPaul E. McKenney 			     struct srcu_struct *sp, int *flags,
499aebc8264SPaul E. McKenney 			     unsigned long *gp_seq);
500cad7b389SPaul E. McKenney 
501cad7b389SPaul E. McKenney #endif
502cad7b389SPaul E. McKenney 
503e3c8d51eSPaul E. McKenney #ifdef CONFIG_TINY_RCU
504*7d0c9c50SPaul E. McKenney static inline bool rcu_dynticks_zero_in_eqs(int cpu, int *vp) { return false; }
50517ef2fe9SPaul E. McKenney static inline unsigned long rcu_get_gp_seq(void) { return 0; }
5067414fac0SPaul E. McKenney static inline unsigned long rcu_exp_batches_completed(void) { return 0; }
5077414fac0SPaul E. McKenney static inline unsigned long
5087414fac0SPaul E. McKenney srcu_batches_completed(struct srcu_struct *sp) { return 0; }
5097414fac0SPaul E. McKenney static inline void rcu_force_quiescent_state(void) { }
5107414fac0SPaul E. McKenney static inline void show_rcu_gp_kthreads(void) { }
5114babd855SJoel Fernandes (Google) static inline int rcu_get_gp_kthreads_prio(void) { return 0; }
512e0aff973SPaul E. McKenney static inline void rcu_fwd_progress_check(unsigned long j) { }
513e3c8d51eSPaul E. McKenney #else /* #ifdef CONFIG_TINY_RCU */
514*7d0c9c50SPaul E. McKenney bool rcu_dynticks_zero_in_eqs(int cpu, int *vp);
51517ef2fe9SPaul E. McKenney unsigned long rcu_get_gp_seq(void);
516e3c8d51eSPaul E. McKenney unsigned long rcu_exp_batches_completed(void);
5175a0465e1SPaul E. McKenney unsigned long srcu_batches_completed(struct srcu_struct *sp);
518e3c8d51eSPaul E. McKenney void show_rcu_gp_kthreads(void);
5194babd855SJoel Fernandes (Google) int rcu_get_gp_kthreads_prio(void);
520e0aff973SPaul E. McKenney void rcu_fwd_progress_check(unsigned long j);
521e3c8d51eSPaul E. McKenney void rcu_force_quiescent_state(void);
522ad7c946bSPaul E. McKenney extern struct workqueue_struct *rcu_gp_wq;
52325f3d7efSPaul E. McKenney extern struct workqueue_struct *rcu_par_gp_wq;
524e3c8d51eSPaul E. McKenney #endif /* #else #ifdef CONFIG_TINY_RCU */
525e3c8d51eSPaul E. McKenney 
52644c65ff2SPaul E. McKenney #ifdef CONFIG_RCU_NOCB_CPU
5273d54f798SPaul E. McKenney bool rcu_is_nocb_cpu(int cpu);
5285ab7ab83SPaul E. McKenney void rcu_bind_current_to_nocb(void);
5293d54f798SPaul E. McKenney #else
5303d54f798SPaul E. McKenney static inline bool rcu_is_nocb_cpu(int cpu) { return false; }
5315ab7ab83SPaul E. McKenney static inline void rcu_bind_current_to_nocb(void) { }
5323d54f798SPaul E. McKenney #endif
5333d54f798SPaul E. McKenney 
5344102adabSPaul E. McKenney #endif /* __LINUX_RCU_H */
535