xref: /openbmc/linux/kernel/rcu/rcu.h (revision 36b5dae64513b7ce3a0e0f6cb469e0f74bacad45)
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 
201bd73a7f5STeodora Baluta void kfree(const void *);
2024102adabSPaul E. McKenney 
203406e3e53SPaul E. McKenney /*
204406e3e53SPaul E. McKenney  * Reclaim the specified callback, either by invoking it (non-lazy case)
205406e3e53SPaul E. McKenney  * or freeing it directly (lazy case).  Return true if lazy, false otherwise.
206406e3e53SPaul E. McKenney  */
2074102adabSPaul E. McKenney static inline bool __rcu_reclaim(const char *rn, struct rcu_head *head)
2084102adabSPaul E. McKenney {
20974de6960SPaul E. McKenney 	rcu_callback_t f;
2104102adabSPaul E. McKenney 	unsigned long offset = (unsigned long)head->func;
2114102adabSPaul E. McKenney 
21224ef659aSPaul E. McKenney 	rcu_lock_acquire(&rcu_callback_map);
2134102adabSPaul E. McKenney 	if (__is_kfree_rcu_offset(offset)) {
2144f5fbd78SYafang Shao 		trace_rcu_invoke_kfree_callback(rn, head, offset);
2154102adabSPaul E. McKenney 		kfree((void *)head - offset);
21624ef659aSPaul E. McKenney 		rcu_lock_release(&rcu_callback_map);
217406e3e53SPaul E. McKenney 		return true;
2184102adabSPaul E. McKenney 	} else {
2194f5fbd78SYafang Shao 		trace_rcu_invoke_callback(rn, head);
22074de6960SPaul E. McKenney 		f = head->func;
22174de6960SPaul E. McKenney 		WRITE_ONCE(head->func, (rcu_callback_t)0L);
22274de6960SPaul E. McKenney 		f(head);
22324ef659aSPaul E. McKenney 		rcu_lock_release(&rcu_callback_map);
224406e3e53SPaul E. McKenney 		return false;
2254102adabSPaul E. McKenney 	}
2264102adabSPaul E. McKenney }
2274102adabSPaul E. McKenney 
2284102adabSPaul E. McKenney #ifdef CONFIG_RCU_STALL_COMMON
2294102adabSPaul E. McKenney 
230cdc694b2SPaul E. McKenney extern int rcu_cpu_stall_ftrace_dump;
2314102adabSPaul E. McKenney extern int rcu_cpu_stall_suppress;
23210462d6fSPaul E. McKenney extern int rcu_cpu_stall_timeout;
2334102adabSPaul E. McKenney int rcu_jiffies_till_stall_check(void);
2344102adabSPaul E. McKenney 
235f22ce091SPaul E. McKenney #define rcu_ftrace_dump_stall_suppress() \
236f22ce091SPaul E. McKenney do { \
237f22ce091SPaul E. McKenney 	if (!rcu_cpu_stall_suppress) \
238f22ce091SPaul E. McKenney 		rcu_cpu_stall_suppress = 3; \
239f22ce091SPaul E. McKenney } while (0)
240f22ce091SPaul E. McKenney 
241f22ce091SPaul E. McKenney #define rcu_ftrace_dump_stall_unsuppress() \
242f22ce091SPaul E. McKenney do { \
243f22ce091SPaul E. McKenney 	if (rcu_cpu_stall_suppress == 3) \
244f22ce091SPaul E. McKenney 		rcu_cpu_stall_suppress = 0; \
245f22ce091SPaul E. McKenney } while (0)
246f22ce091SPaul E. McKenney 
247f22ce091SPaul E. McKenney #else /* #endif #ifdef CONFIG_RCU_STALL_COMMON */
248f22ce091SPaul E. McKenney #define rcu_ftrace_dump_stall_suppress()
249f22ce091SPaul E. McKenney #define rcu_ftrace_dump_stall_unsuppress()
2504102adabSPaul E. McKenney #endif /* #ifdef CONFIG_RCU_STALL_COMMON */
2514102adabSPaul E. McKenney 
2524102adabSPaul E. McKenney /*
2534102adabSPaul E. McKenney  * Strings used in tracepoints need to be exported via the
2544102adabSPaul E. McKenney  * tracing system such that tools like perf and trace-cmd can
2554102adabSPaul E. McKenney  * translate the string address pointers to actual text.
2564102adabSPaul E. McKenney  */
2574102adabSPaul E. McKenney #define TPS(x)  tracepoint_string(x)
2584102adabSPaul E. McKenney 
259b8989b76SPaul E. McKenney /*
260b8989b76SPaul E. McKenney  * Dump the ftrace buffer, but only one time per callsite per boot.
261b8989b76SPaul E. McKenney  */
262b8989b76SPaul E. McKenney #define rcu_ftrace_dump(oops_dump_mode) \
263b8989b76SPaul E. McKenney do { \
264b8989b76SPaul E. McKenney 	static atomic_t ___rfd_beenhere = ATOMIC_INIT(0); \
265b8989b76SPaul E. McKenney 	\
266b8989b76SPaul E. McKenney 	if (!atomic_read(&___rfd_beenhere) && \
26783b6ca1fSPaul E. McKenney 	    !atomic_xchg(&___rfd_beenhere, 1)) { \
26883b6ca1fSPaul E. McKenney 		tracing_off(); \
269f22ce091SPaul E. McKenney 		rcu_ftrace_dump_stall_suppress(); \
270b8989b76SPaul E. McKenney 		ftrace_dump(oops_dump_mode); \
271f22ce091SPaul E. McKenney 		rcu_ftrace_dump_stall_unsuppress(); \
27283b6ca1fSPaul E. McKenney 	} \
273b8989b76SPaul E. McKenney } while (0)
274b8989b76SPaul E. McKenney 
275aa23c6fbSPranith Kumar void rcu_early_boot_tests(void);
27652d7e48bSPaul E. McKenney void rcu_test_sync_prims(void);
277aa23c6fbSPranith Kumar 
2785f6130faSLai Jiangshan /*
2795f6130faSLai Jiangshan  * This function really isn't for public consumption, but RCU is special in
2805f6130faSLai Jiangshan  * that context switches can allow the state machine to make progress.
2815f6130faSLai Jiangshan  */
2825f6130faSLai Jiangshan extern void resched_cpu(int cpu);
2835f6130faSLai Jiangshan 
2842b34c43cSPaul E. McKenney #if defined(SRCU) || !defined(TINY_RCU)
2852b34c43cSPaul E. McKenney 
2862b34c43cSPaul E. McKenney #include <linux/rcu_node_tree.h>
2872b34c43cSPaul E. McKenney 
2882b34c43cSPaul E. McKenney extern int rcu_num_lvls;
289e95d68d2SPaul E. McKenney extern int num_rcu_lvl[];
2902b34c43cSPaul E. McKenney extern int rcu_num_nodes;
2912b34c43cSPaul E. McKenney static bool rcu_fanout_exact;
2922b34c43cSPaul E. McKenney static int rcu_fanout_leaf;
2932b34c43cSPaul E. McKenney 
2942b34c43cSPaul E. McKenney /*
2952b34c43cSPaul E. McKenney  * Compute the per-level fanout, either using the exact fanout specified
2962b34c43cSPaul E. McKenney  * or balancing the tree, depending on the rcu_fanout_exact boot parameter.
2972b34c43cSPaul E. McKenney  */
2982b34c43cSPaul E. McKenney static inline void rcu_init_levelspread(int *levelspread, const int *levelcnt)
2992b34c43cSPaul E. McKenney {
3002b34c43cSPaul E. McKenney 	int i;
3012b34c43cSPaul E. McKenney 
302*36b5dae6SPaul E. McKenney 	for (i = 0; i < RCU_NUM_LVLS; i++)
303*36b5dae6SPaul E. McKenney 		levelspread[i] = INT_MIN;
3042b34c43cSPaul E. McKenney 	if (rcu_fanout_exact) {
3052b34c43cSPaul E. McKenney 		levelspread[rcu_num_lvls - 1] = rcu_fanout_leaf;
3062b34c43cSPaul E. McKenney 		for (i = rcu_num_lvls - 2; i >= 0; i--)
3072b34c43cSPaul E. McKenney 			levelspread[i] = RCU_FANOUT;
3082b34c43cSPaul E. McKenney 	} else {
3092b34c43cSPaul E. McKenney 		int ccur;
3102b34c43cSPaul E. McKenney 		int cprv;
3112b34c43cSPaul E. McKenney 
3122b34c43cSPaul E. McKenney 		cprv = nr_cpu_ids;
3132b34c43cSPaul E. McKenney 		for (i = rcu_num_lvls - 1; i >= 0; i--) {
3142b34c43cSPaul E. McKenney 			ccur = levelcnt[i];
3152b34c43cSPaul E. McKenney 			levelspread[i] = (cprv + ccur - 1) / ccur;
3162b34c43cSPaul E. McKenney 			cprv = ccur;
3172b34c43cSPaul E. McKenney 		}
3182b34c43cSPaul E. McKenney 	}
3192b34c43cSPaul E. McKenney }
3202b34c43cSPaul E. McKenney 
3217f87c036SPaul E. McKenney /* Returns a pointer to the first leaf rcu_node structure. */
322aedf4ba9SPaul E. McKenney #define rcu_first_leaf_node() (rcu_state.level[rcu_num_lvls - 1])
3235b4c11d5SPaul E. McKenney 
3245b4c11d5SPaul E. McKenney /* Is this rcu_node a leaf? */
3255b4c11d5SPaul E. McKenney #define rcu_is_leaf_node(rnp) ((rnp)->level == rcu_num_lvls - 1)
3265b4c11d5SPaul E. McKenney 
3275257514dSPaul E. McKenney /* Is this rcu_node the last leaf? */
328aedf4ba9SPaul E. McKenney #define rcu_is_last_leaf_node(rnp) ((rnp) == &rcu_state.node[rcu_num_nodes - 1])
3295257514dSPaul E. McKenney 
330efbe451dSPaul E. McKenney /*
331aedf4ba9SPaul E. McKenney  * Do a full breadth-first scan of the {s,}rcu_node structures for the
3327f87c036SPaul E. McKenney  * specified state structure (for SRCU) or the only rcu_state structure
3337f87c036SPaul E. McKenney  * (for RCU).
334efbe451dSPaul E. McKenney  */
335aedf4ba9SPaul E. McKenney #define srcu_for_each_node_breadth_first(sp, rnp) \
336aedf4ba9SPaul E. McKenney 	for ((rnp) = &(sp)->node[0]; \
337aedf4ba9SPaul E. McKenney 	     (rnp) < &(sp)->node[rcu_num_nodes]; (rnp)++)
338aedf4ba9SPaul E. McKenney #define rcu_for_each_node_breadth_first(rnp) \
339aedf4ba9SPaul E. McKenney 	srcu_for_each_node_breadth_first(&rcu_state, rnp)
340efbe451dSPaul E. McKenney 
341efbe451dSPaul E. McKenney /*
3427f87c036SPaul E. McKenney  * Scan the leaves of the rcu_node hierarchy for the rcu_state structure.
3437f87c036SPaul E. McKenney  * Note that if there is a singleton rcu_node tree with but one rcu_node
3447f87c036SPaul E. McKenney  * structure, this loop -will- visit the rcu_node structure.  It is still
3457f87c036SPaul E. McKenney  * a leaf node, even if it is also the root node.
346efbe451dSPaul E. McKenney  */
347aedf4ba9SPaul E. McKenney #define rcu_for_each_leaf_node(rnp) \
348aedf4ba9SPaul E. McKenney 	for ((rnp) = rcu_first_leaf_node(); \
349aedf4ba9SPaul E. McKenney 	     (rnp) < &rcu_state.node[rcu_num_nodes]; (rnp)++)
350efbe451dSPaul E. McKenney 
351efbe451dSPaul E. McKenney /*
352efbe451dSPaul E. McKenney  * Iterate over all possible CPUs in a leaf RCU node.
353efbe451dSPaul E. McKenney  */
354efbe451dSPaul E. McKenney #define for_each_leaf_node_possible_cpu(rnp, cpu) \
35565963d24SPaul E. McKenney 	for ((cpu) = cpumask_next((rnp)->grplo - 1, cpu_possible_mask); \
35665963d24SPaul E. McKenney 	     (cpu) <= rnp->grphi; \
35765963d24SPaul E. McKenney 	     (cpu) = cpumask_next((cpu), cpu_possible_mask))
35865963d24SPaul E. McKenney 
35965963d24SPaul E. McKenney /*
36065963d24SPaul E. McKenney  * Iterate over all CPUs in a leaf RCU node's specified mask.
36165963d24SPaul E. McKenney  */
36265963d24SPaul E. McKenney #define rcu_find_next_bit(rnp, cpu, mask) \
36365963d24SPaul E. McKenney 	((rnp)->grplo + find_next_bit(&(mask), BITS_PER_LONG, (cpu)))
36465963d24SPaul E. McKenney #define for_each_leaf_node_cpu_mask(rnp, cpu, mask) \
36565963d24SPaul E. McKenney 	for ((cpu) = rcu_find_next_bit((rnp), 0, (mask)); \
36665963d24SPaul E. McKenney 	     (cpu) <= rnp->grphi; \
36765963d24SPaul E. McKenney 	     (cpu) = rcu_find_next_bit((rnp), (cpu) + 1 - (rnp->grplo), (mask)))
368efbe451dSPaul E. McKenney 
36983d40bd3SPaul E. McKenney /*
37083d40bd3SPaul E. McKenney  * Wrappers for the rcu_node::lock acquire and release.
37183d40bd3SPaul E. McKenney  *
37283d40bd3SPaul E. McKenney  * Because the rcu_nodes form a tree, the tree traversal locking will observe
37383d40bd3SPaul E. McKenney  * different lock values, this in turn means that an UNLOCK of one level
37483d40bd3SPaul E. McKenney  * followed by a LOCK of another level does not imply a full memory barrier;
37583d40bd3SPaul E. McKenney  * and most importantly transitivity is lost.
37683d40bd3SPaul E. McKenney  *
37783d40bd3SPaul E. McKenney  * In order to restore full ordering between tree levels, augment the regular
37883d40bd3SPaul E. McKenney  * lock acquire functions with smp_mb__after_unlock_lock().
37983d40bd3SPaul E. McKenney  *
38083d40bd3SPaul E. McKenney  * As ->lock of struct rcu_node is a __private field, therefore one should use
38183d40bd3SPaul E. McKenney  * these wrappers rather than directly call raw_spin_{lock,unlock}* on ->lock.
38283d40bd3SPaul E. McKenney  */
38383d40bd3SPaul E. McKenney #define raw_spin_lock_rcu_node(p)					\
38483d40bd3SPaul E. McKenney do {									\
38583d40bd3SPaul E. McKenney 	raw_spin_lock(&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_rcu_node(p) raw_spin_unlock(&ACCESS_PRIVATE(p, lock))
39083d40bd3SPaul E. McKenney 
39183d40bd3SPaul E. McKenney #define raw_spin_lock_irq_rcu_node(p)					\
39283d40bd3SPaul E. McKenney do {									\
39383d40bd3SPaul E. McKenney 	raw_spin_lock_irq(&ACCESS_PRIVATE(p, lock));			\
39483d40bd3SPaul E. McKenney 	smp_mb__after_unlock_lock();					\
39583d40bd3SPaul E. McKenney } while (0)
39683d40bd3SPaul E. McKenney 
39783d40bd3SPaul E. McKenney #define raw_spin_unlock_irq_rcu_node(p)					\
39883d40bd3SPaul E. McKenney 	raw_spin_unlock_irq(&ACCESS_PRIVATE(p, lock))
39983d40bd3SPaul E. McKenney 
4004e4bea74SPaul E. McKenney #define raw_spin_lock_irqsave_rcu_node(p, flags)			\
40183d40bd3SPaul E. McKenney do {									\
4024e4bea74SPaul E. McKenney 	raw_spin_lock_irqsave(&ACCESS_PRIVATE(p, lock), flags);	\
40383d40bd3SPaul E. McKenney 	smp_mb__after_unlock_lock();					\
40483d40bd3SPaul E. McKenney } while (0)
40583d40bd3SPaul E. McKenney 
4064e4bea74SPaul E. McKenney #define raw_spin_unlock_irqrestore_rcu_node(p, flags)			\
407a32e01eeSMatthew Wilcox 	raw_spin_unlock_irqrestore(&ACCESS_PRIVATE(p, lock), flags)
40883d40bd3SPaul E. McKenney 
40983d40bd3SPaul E. McKenney #define raw_spin_trylock_rcu_node(p)					\
41083d40bd3SPaul E. McKenney ({									\
41183d40bd3SPaul E. McKenney 	bool ___locked = raw_spin_trylock(&ACCESS_PRIVATE(p, lock));	\
41283d40bd3SPaul E. McKenney 									\
41383d40bd3SPaul E. McKenney 	if (___locked)							\
41483d40bd3SPaul E. McKenney 		smp_mb__after_unlock_lock();				\
41583d40bd3SPaul E. McKenney 	___locked;							\
41683d40bd3SPaul E. McKenney })
41783d40bd3SPaul E. McKenney 
418a32e01eeSMatthew Wilcox #define raw_lockdep_assert_held_rcu_node(p)				\
419a32e01eeSMatthew Wilcox 	lockdep_assert_held(&ACCESS_PRIVATE(p, lock))
420a32e01eeSMatthew Wilcox 
4212b34c43cSPaul E. McKenney #endif /* #if defined(SRCU) || !defined(TINY_RCU) */
4222b34c43cSPaul E. McKenney 
423e0fcba9aSPaul E. McKenney #ifdef CONFIG_SRCU
424e0fcba9aSPaul E. McKenney void srcu_init(void);
425e0fcba9aSPaul E. McKenney #else /* #ifdef CONFIG_SRCU */
426e0fcba9aSPaul E. McKenney static inline void srcu_init(void) { }
427e0fcba9aSPaul E. McKenney #endif /* #else #ifdef CONFIG_SRCU */
428e0fcba9aSPaul E. McKenney 
42925c36329SPaul E. McKenney #ifdef CONFIG_TINY_RCU
43025c36329SPaul E. McKenney /* Tiny RCU doesn't expedite, as its purpose in life is instead to be tiny. */
4317414fac0SPaul E. McKenney static inline bool rcu_gp_is_normal(void) { return true; }
4327414fac0SPaul E. McKenney static inline bool rcu_gp_is_expedited(void) { return false; }
4337414fac0SPaul E. McKenney static inline void rcu_expedite_gp(void) { }
4347414fac0SPaul E. McKenney static inline void rcu_unexpedite_gp(void) { }
435bfbd767dSPaul E. McKenney static inline void rcu_request_urgent_qs_task(struct task_struct *t) { }
43625c36329SPaul E. McKenney #else /* #ifdef CONFIG_TINY_RCU */
43725c36329SPaul E. McKenney bool rcu_gp_is_normal(void);     /* Internal RCU use. */
43825c36329SPaul E. McKenney bool rcu_gp_is_expedited(void);  /* Internal RCU use. */
43925c36329SPaul E. McKenney void rcu_expedite_gp(void);
44025c36329SPaul E. McKenney void rcu_unexpedite_gp(void);
44125c36329SPaul E. McKenney void rcupdate_announce_bootup_oddness(void);
442bfbd767dSPaul E. McKenney void rcu_request_urgent_qs_task(struct task_struct *t);
44325c36329SPaul E. McKenney #endif /* #else #ifdef CONFIG_TINY_RCU */
44425c36329SPaul E. McKenney 
44582118249SPaul E. McKenney #define RCU_SCHEDULER_INACTIVE	0
44682118249SPaul E. McKenney #define RCU_SCHEDULER_INIT	1
44782118249SPaul E. McKenney #define RCU_SCHEDULER_RUNNING	2
44882118249SPaul E. McKenney 
449cad7b389SPaul E. McKenney enum rcutorture_type {
450cad7b389SPaul E. McKenney 	RCU_FLAVOR,
451cad7b389SPaul E. McKenney 	RCU_TASKS_FLAVOR,
452c682db55SPaul E. McKenney 	RCU_TRIVIAL_FLAVOR,
453cad7b389SPaul E. McKenney 	SRCU_FLAVOR,
454cad7b389SPaul E. McKenney 	INVALID_RCU_FLAVOR
455cad7b389SPaul E. McKenney };
456cad7b389SPaul E. McKenney 
457cad7b389SPaul E. McKenney #if defined(CONFIG_TREE_RCU) || defined(CONFIG_PREEMPT_RCU)
458cad7b389SPaul E. McKenney void rcutorture_get_gp_data(enum rcutorture_type test_type, int *flags,
459aebc8264SPaul E. McKenney 			    unsigned long *gp_seq);
460cad7b389SPaul E. McKenney void rcutorture_record_progress(unsigned long vernum);
461cad7b389SPaul E. McKenney void do_trace_rcu_torture_read(const char *rcutorturename,
462cad7b389SPaul E. McKenney 			       struct rcu_head *rhp,
463cad7b389SPaul E. McKenney 			       unsigned long secs,
464cad7b389SPaul E. McKenney 			       unsigned long c_old,
465cad7b389SPaul E. McKenney 			       unsigned long c);
466cad7b389SPaul E. McKenney #else
467cad7b389SPaul E. McKenney static inline void rcutorture_get_gp_data(enum rcutorture_type test_type,
468aebc8264SPaul E. McKenney 					  int *flags, unsigned long *gp_seq)
469cad7b389SPaul E. McKenney {
470cad7b389SPaul E. McKenney 	*flags = 0;
471aebc8264SPaul E. McKenney 	*gp_seq = 0;
472cad7b389SPaul E. McKenney }
4737414fac0SPaul E. McKenney static inline void rcutorture_record_progress(unsigned long vernum) { }
474cad7b389SPaul E. McKenney #ifdef CONFIG_RCU_TRACE
475cad7b389SPaul E. McKenney void do_trace_rcu_torture_read(const char *rcutorturename,
476cad7b389SPaul E. McKenney 			       struct rcu_head *rhp,
477cad7b389SPaul E. McKenney 			       unsigned long secs,
478cad7b389SPaul E. McKenney 			       unsigned long c_old,
479cad7b389SPaul E. McKenney 			       unsigned long c);
480cad7b389SPaul E. McKenney #else
481cad7b389SPaul E. McKenney #define do_trace_rcu_torture_read(rcutorturename, rhp, secs, c_old, c) \
482cad7b389SPaul E. McKenney 	do { } while (0)
483cad7b389SPaul E. McKenney #endif
484cad7b389SPaul E. McKenney #endif
485cad7b389SPaul E. McKenney 
486c682db55SPaul E. McKenney #if IS_ENABLED(CONFIG_RCU_TORTURE_TEST) || IS_MODULE(CONFIG_RCU_TORTURE_TEST)
487c682db55SPaul E. McKenney long rcutorture_sched_setaffinity(pid_t pid, const struct cpumask *in_mask);
488c682db55SPaul E. McKenney #endif
489c682db55SPaul E. McKenney 
490cad7b389SPaul E. McKenney #ifdef CONFIG_TINY_SRCU
491cad7b389SPaul E. McKenney 
492cad7b389SPaul E. McKenney static inline void srcutorture_get_gp_data(enum rcutorture_type test_type,
493cad7b389SPaul E. McKenney 					   struct srcu_struct *sp, int *flags,
494aebc8264SPaul E. McKenney 					   unsigned long *gp_seq)
495cad7b389SPaul E. McKenney {
496cad7b389SPaul E. McKenney 	if (test_type != SRCU_FLAVOR)
497cad7b389SPaul E. McKenney 		return;
498cad7b389SPaul E. McKenney 	*flags = 0;
499aebc8264SPaul E. McKenney 	*gp_seq = sp->srcu_idx;
500cad7b389SPaul E. McKenney }
501cad7b389SPaul E. McKenney 
502cad7b389SPaul E. McKenney #elif defined(CONFIG_TREE_SRCU)
503cad7b389SPaul E. McKenney 
504cad7b389SPaul E. McKenney void srcutorture_get_gp_data(enum rcutorture_type test_type,
505cad7b389SPaul E. McKenney 			     struct srcu_struct *sp, int *flags,
506aebc8264SPaul E. McKenney 			     unsigned long *gp_seq);
507cad7b389SPaul E. McKenney 
508cad7b389SPaul E. McKenney #endif
509cad7b389SPaul E. McKenney 
510e3c8d51eSPaul E. McKenney #ifdef CONFIG_TINY_RCU
51117ef2fe9SPaul E. McKenney static inline unsigned long rcu_get_gp_seq(void) { return 0; }
5127414fac0SPaul E. McKenney static inline unsigned long rcu_exp_batches_completed(void) { return 0; }
5137414fac0SPaul E. McKenney static inline unsigned long
5147414fac0SPaul E. McKenney srcu_batches_completed(struct srcu_struct *sp) { return 0; }
5157414fac0SPaul E. McKenney static inline void rcu_force_quiescent_state(void) { }
5167414fac0SPaul E. McKenney static inline void show_rcu_gp_kthreads(void) { }
5174babd855SJoel Fernandes (Google) static inline int rcu_get_gp_kthreads_prio(void) { return 0; }
518e0aff973SPaul E. McKenney static inline void rcu_fwd_progress_check(unsigned long j) { }
519e3c8d51eSPaul E. McKenney #else /* #ifdef CONFIG_TINY_RCU */
52017ef2fe9SPaul E. McKenney unsigned long rcu_get_gp_seq(void);
521e3c8d51eSPaul E. McKenney unsigned long rcu_exp_batches_completed(void);
5225a0465e1SPaul E. McKenney unsigned long srcu_batches_completed(struct srcu_struct *sp);
523e3c8d51eSPaul E. McKenney void show_rcu_gp_kthreads(void);
5244babd855SJoel Fernandes (Google) int rcu_get_gp_kthreads_prio(void);
525e0aff973SPaul E. McKenney void rcu_fwd_progress_check(unsigned long j);
526e3c8d51eSPaul E. McKenney void rcu_force_quiescent_state(void);
527ad7c946bSPaul E. McKenney extern struct workqueue_struct *rcu_gp_wq;
52825f3d7efSPaul E. McKenney extern struct workqueue_struct *rcu_par_gp_wq;
529e3c8d51eSPaul E. McKenney #endif /* #else #ifdef CONFIG_TINY_RCU */
530e3c8d51eSPaul E. McKenney 
53144c65ff2SPaul E. McKenney #ifdef CONFIG_RCU_NOCB_CPU
5323d54f798SPaul E. McKenney bool rcu_is_nocb_cpu(int cpu);
5335ab7ab83SPaul E. McKenney void rcu_bind_current_to_nocb(void);
5343d54f798SPaul E. McKenney #else
5353d54f798SPaul E. McKenney static inline bool rcu_is_nocb_cpu(int cpu) { return false; }
5365ab7ab83SPaul E. McKenney static inline void rcu_bind_current_to_nocb(void) { }
5373d54f798SPaul E. McKenney #endif
5383d54f798SPaul E. McKenney 
5394102adabSPaul E. McKenney #endif /* __LINUX_RCU_H */
540