xref: /openbmc/linux/kernel/rcu/rcu.h (revision 2ee5aca54622aacc196106c623fea4116f1043a6)
14102adabSPaul E. McKenney /*
24102adabSPaul E. McKenney  * Read-Copy Update definitions shared among RCU implementations.
34102adabSPaul E. McKenney  *
44102adabSPaul E. McKenney  * This program is free software; you can redistribute it and/or modify
54102adabSPaul E. McKenney  * it under the terms of the GNU General Public License as published by
64102adabSPaul E. McKenney  * the Free Software Foundation; either version 2 of the License, or
74102adabSPaul E. McKenney  * (at your option) any later version.
84102adabSPaul E. McKenney  *
94102adabSPaul E. McKenney  * This program is distributed in the hope that it will be useful,
104102adabSPaul E. McKenney  * but WITHOUT ANY WARRANTY; without even the implied warranty of
114102adabSPaul E. McKenney  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
124102adabSPaul E. McKenney  * GNU General Public License for more details.
134102adabSPaul E. McKenney  *
144102adabSPaul E. McKenney  * You should have received a copy of the GNU General Public License
1587de1cfdSPaul E. McKenney  * along with this program; if not, you can access it online at
1687de1cfdSPaul E. McKenney  * http://www.gnu.org/licenses/gpl-2.0.html.
174102adabSPaul E. McKenney  *
184102adabSPaul E. McKenney  * Copyright IBM Corporation, 2011
194102adabSPaul E. McKenney  *
204102adabSPaul E. McKenney  * Author: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
214102adabSPaul E. McKenney  */
224102adabSPaul E. McKenney 
234102adabSPaul E. McKenney #ifndef __LINUX_RCU_H
244102adabSPaul E. McKenney #define __LINUX_RCU_H
254102adabSPaul E. McKenney 
265cb5c6e1SPaul Gortmaker #include <trace/events/rcu.h>
274102adabSPaul E. McKenney #ifdef CONFIG_RCU_TRACE
284102adabSPaul E. McKenney #define RCU_TRACE(stmt) stmt
294102adabSPaul E. McKenney #else /* #ifdef CONFIG_RCU_TRACE */
304102adabSPaul E. McKenney #define RCU_TRACE(stmt)
314102adabSPaul E. McKenney #endif /* #else #ifdef CONFIG_RCU_TRACE */
324102adabSPaul E. McKenney 
3351a1fd30SPaul E. McKenney /* Offset to allow for unmatched rcu_irq_{enter,exit}(). */
3484585aa8SPaul E. McKenney #define DYNTICK_IRQ_NONIDLE	((LONG_MAX / 2) + 1)
356136d6e4SPaul E. McKenney 
362e8c28c2SPaul E. McKenney 
372e8c28c2SPaul E. McKenney /*
382e8c28c2SPaul E. McKenney  * Grace-period counter management.
392e8c28c2SPaul E. McKenney  */
402e8c28c2SPaul E. McKenney 
41f1ec57a4SPaul E. McKenney #define RCU_SEQ_CTR_SHIFT	2
42031aeee0SPaul E. McKenney #define RCU_SEQ_STATE_MASK	((1 << RCU_SEQ_CTR_SHIFT) - 1)
43031aeee0SPaul E. McKenney 
44031aeee0SPaul E. McKenney /*
45031aeee0SPaul E. McKenney  * Return the counter portion of a sequence number previously returned
46031aeee0SPaul E. McKenney  * by rcu_seq_snap() or rcu_seq_current().
47031aeee0SPaul E. McKenney  */
48031aeee0SPaul E. McKenney static inline unsigned long rcu_seq_ctr(unsigned long s)
49031aeee0SPaul E. McKenney {
50031aeee0SPaul E. McKenney 	return s >> RCU_SEQ_CTR_SHIFT;
51031aeee0SPaul E. McKenney }
52031aeee0SPaul E. McKenney 
53031aeee0SPaul E. McKenney /*
54031aeee0SPaul E. McKenney  * Return the state portion of a sequence number previously returned
55031aeee0SPaul E. McKenney  * by rcu_seq_snap() or rcu_seq_current().
56031aeee0SPaul E. McKenney  */
57031aeee0SPaul E. McKenney static inline int rcu_seq_state(unsigned long s)
58031aeee0SPaul E. McKenney {
59031aeee0SPaul E. McKenney 	return s & RCU_SEQ_STATE_MASK;
60031aeee0SPaul E. McKenney }
61031aeee0SPaul E. McKenney 
6280a7956fSPaul E. McKenney /*
6380a7956fSPaul E. McKenney  * Set the state portion of the pointed-to sequence number.
6480a7956fSPaul E. McKenney  * The caller is responsible for preventing conflicting updates.
6580a7956fSPaul E. McKenney  */
6680a7956fSPaul E. McKenney static inline void rcu_seq_set_state(unsigned long *sp, int newstate)
6780a7956fSPaul E. McKenney {
6880a7956fSPaul E. McKenney 	WARN_ON_ONCE(newstate & ~RCU_SEQ_STATE_MASK);
6980a7956fSPaul E. McKenney 	WRITE_ONCE(*sp, (*sp & ~RCU_SEQ_STATE_MASK) + newstate);
7080a7956fSPaul E. McKenney }
7180a7956fSPaul E. McKenney 
722e8c28c2SPaul E. McKenney /* Adjust sequence number for start of update-side operation. */
732e8c28c2SPaul E. McKenney static inline void rcu_seq_start(unsigned long *sp)
742e8c28c2SPaul E. McKenney {
752e8c28c2SPaul E. McKenney 	WRITE_ONCE(*sp, *sp + 1);
762e8c28c2SPaul E. McKenney 	smp_mb(); /* Ensure update-side operation after counter increment. */
77031aeee0SPaul E. McKenney 	WARN_ON_ONCE(rcu_seq_state(*sp) != 1);
782e8c28c2SPaul E. McKenney }
792e8c28c2SPaul E. McKenney 
809a414201SPaul E. McKenney /* Compute the end-of-grace-period value for the specified sequence number. */
819a414201SPaul E. McKenney static inline unsigned long rcu_seq_endval(unsigned long *sp)
829a414201SPaul E. McKenney {
839a414201SPaul E. McKenney 	return (*sp | RCU_SEQ_STATE_MASK) + 1;
849a414201SPaul E. McKenney }
859a414201SPaul E. McKenney 
862e8c28c2SPaul E. McKenney /* Adjust sequence number for end of update-side operation. */
872e8c28c2SPaul E. McKenney static inline void rcu_seq_end(unsigned long *sp)
882e8c28c2SPaul E. McKenney {
892e8c28c2SPaul E. McKenney 	smp_mb(); /* Ensure update-side operation before counter increment. */
90031aeee0SPaul E. McKenney 	WARN_ON_ONCE(!rcu_seq_state(*sp));
919a414201SPaul E. McKenney 	WRITE_ONCE(*sp, rcu_seq_endval(sp));
922e8c28c2SPaul E. McKenney }
932e8c28c2SPaul E. McKenney 
940d805a70SJoel Fernandes (Google) /*
950d805a70SJoel Fernandes (Google)  * rcu_seq_snap - Take a snapshot of the update side's sequence number.
960d805a70SJoel Fernandes (Google)  *
970d805a70SJoel Fernandes (Google)  * This function returns the earliest value of the grace-period sequence number
980d805a70SJoel Fernandes (Google)  * that will indicate that a full grace period has elapsed since the current
990d805a70SJoel Fernandes (Google)  * time.  Once the grace-period sequence number has reached this value, it will
1000d805a70SJoel Fernandes (Google)  * be safe to invoke all callbacks that have been registered prior to the
1010d805a70SJoel Fernandes (Google)  * current time. This value is the current grace-period number plus two to the
1020d805a70SJoel Fernandes (Google)  * power of the number of low-order bits reserved for state, then rounded up to
1030d805a70SJoel Fernandes (Google)  * the next value in which the state bits are all zero.
1040d805a70SJoel Fernandes (Google)  */
1052e8c28c2SPaul E. McKenney static inline unsigned long rcu_seq_snap(unsigned long *sp)
1062e8c28c2SPaul E. McKenney {
1072e8c28c2SPaul E. McKenney 	unsigned long s;
1082e8c28c2SPaul E. McKenney 
109031aeee0SPaul E. McKenney 	s = (READ_ONCE(*sp) + 2 * RCU_SEQ_STATE_MASK + 1) & ~RCU_SEQ_STATE_MASK;
1102e8c28c2SPaul E. McKenney 	smp_mb(); /* Above access must not bleed into critical section. */
1112e8c28c2SPaul E. McKenney 	return s;
1122e8c28c2SPaul E. McKenney }
1132e8c28c2SPaul E. McKenney 
1148660b7d8SPaul E. McKenney /* Return the current value the update side's sequence number, no ordering. */
1158660b7d8SPaul E. McKenney static inline unsigned long rcu_seq_current(unsigned long *sp)
1168660b7d8SPaul E. McKenney {
1178660b7d8SPaul E. McKenney 	return READ_ONCE(*sp);
1188660b7d8SPaul E. McKenney }
1198660b7d8SPaul E. McKenney 
1202e8c28c2SPaul E. McKenney /*
1212e3e5e55SPaul E. McKenney  * Given a snapshot from rcu_seq_snap(), determine whether or not the
1222e3e5e55SPaul E. McKenney  * corresponding update-side operation has started.
1232e3e5e55SPaul E. McKenney  */
1242e3e5e55SPaul E. McKenney static inline bool rcu_seq_started(unsigned long *sp, unsigned long s)
1252e3e5e55SPaul E. McKenney {
1262e3e5e55SPaul E. McKenney 	return ULONG_CMP_LT((s - 1) & ~RCU_SEQ_STATE_MASK, READ_ONCE(*sp));
1272e3e5e55SPaul E. McKenney }
1282e3e5e55SPaul E. McKenney 
1292e3e5e55SPaul E. McKenney /*
1302e8c28c2SPaul E. McKenney  * Given a snapshot from rcu_seq_snap(), determine whether or not a
1312e8c28c2SPaul E. McKenney  * full update-side operation has occurred.
1322e8c28c2SPaul E. McKenney  */
1332e8c28c2SPaul E. McKenney static inline bool rcu_seq_done(unsigned long *sp, unsigned long s)
1342e8c28c2SPaul E. McKenney {
1352e8c28c2SPaul E. McKenney 	return ULONG_CMP_GE(READ_ONCE(*sp), s);
1362e8c28c2SPaul E. McKenney }
1372e8c28c2SPaul E. McKenney 
1384102adabSPaul E. McKenney /*
13967e14c1eSPaul E. McKenney  * Has a grace period completed since the time the old gp_seq was collected?
14067e14c1eSPaul E. McKenney  */
14167e14c1eSPaul E. McKenney static inline bool rcu_seq_completed_gp(unsigned long old, unsigned long new)
14267e14c1eSPaul E. McKenney {
14367e14c1eSPaul E. McKenney 	return ULONG_CMP_LT(old, new & ~RCU_SEQ_STATE_MASK);
14467e14c1eSPaul E. McKenney }
14567e14c1eSPaul E. McKenney 
14667e14c1eSPaul E. McKenney /*
14767e14c1eSPaul E. McKenney  * Has a grace period started since the time the old gp_seq was collected?
14867e14c1eSPaul E. McKenney  */
14967e14c1eSPaul E. McKenney static inline bool rcu_seq_new_gp(unsigned long old, unsigned long new)
15067e14c1eSPaul E. McKenney {
15167e14c1eSPaul E. McKenney 	return ULONG_CMP_LT((old + RCU_SEQ_STATE_MASK) & ~RCU_SEQ_STATE_MASK,
15267e14c1eSPaul E. McKenney 			    new);
15367e14c1eSPaul E. McKenney }
15467e14c1eSPaul E. McKenney 
15567e14c1eSPaul E. McKenney /*
156d7219312SPaul E. McKenney  * Roughly how many full grace periods have elapsed between the collection
157d7219312SPaul E. McKenney  * of the two specified grace periods?
158d7219312SPaul E. McKenney  */
159d7219312SPaul E. McKenney static inline unsigned long rcu_seq_diff(unsigned long new, unsigned long old)
160d7219312SPaul E. McKenney {
161*2ee5aca5SPaul E. McKenney 	unsigned long rnd_diff;
162*2ee5aca5SPaul E. McKenney 
163*2ee5aca5SPaul E. McKenney 	if (old == new)
164*2ee5aca5SPaul E. McKenney 		return 0;
165*2ee5aca5SPaul E. McKenney 	/*
166*2ee5aca5SPaul E. McKenney 	 * Compute the number of grace periods (still shifted up), plus
167*2ee5aca5SPaul E. McKenney 	 * one if either of new and old is not an exact grace period.
168*2ee5aca5SPaul E. McKenney 	 */
169*2ee5aca5SPaul E. McKenney 	rnd_diff = (new & ~RCU_SEQ_STATE_MASK) -
170*2ee5aca5SPaul E. McKenney 		   ((old + RCU_SEQ_STATE_MASK) & ~RCU_SEQ_STATE_MASK) +
171*2ee5aca5SPaul E. McKenney 		   ((new & RCU_SEQ_STATE_MASK) || (old & RCU_SEQ_STATE_MASK));
172*2ee5aca5SPaul E. McKenney 	if (ULONG_CMP_GE(RCU_SEQ_STATE_MASK, rnd_diff))
173*2ee5aca5SPaul E. McKenney 		return 1; /* Definitely no grace period has elapsed. */
174*2ee5aca5SPaul E. McKenney 	return ((rnd_diff - RCU_SEQ_STATE_MASK - 1) >> RCU_SEQ_CTR_SHIFT) + 2;
175d7219312SPaul E. McKenney }
176d7219312SPaul E. McKenney 
177d7219312SPaul E. McKenney /*
1784102adabSPaul E. McKenney  * debug_rcu_head_queue()/debug_rcu_head_unqueue() are used internally
1794102adabSPaul E. McKenney  * by call_rcu() and rcu callback execution, and are therefore not part of the
1804102adabSPaul E. McKenney  * RCU API. Leaving in rcupdate.h because they are used by all RCU flavors.
1814102adabSPaul E. McKenney  */
1824102adabSPaul E. McKenney 
1834102adabSPaul E. McKenney #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD
1844102adabSPaul E. McKenney # define STATE_RCU_HEAD_READY	0
1854102adabSPaul E. McKenney # define STATE_RCU_HEAD_QUEUED	1
1864102adabSPaul E. McKenney 
1874102adabSPaul E. McKenney extern struct debug_obj_descr rcuhead_debug_descr;
1884102adabSPaul E. McKenney 
1894102adabSPaul E. McKenney static inline int debug_rcu_head_queue(struct rcu_head *head)
1904102adabSPaul E. McKenney {
1914102adabSPaul E. McKenney 	int r1;
1924102adabSPaul E. McKenney 
1934102adabSPaul E. McKenney 	r1 = debug_object_activate(head, &rcuhead_debug_descr);
1944102adabSPaul E. McKenney 	debug_object_active_state(head, &rcuhead_debug_descr,
1954102adabSPaul E. McKenney 				  STATE_RCU_HEAD_READY,
1964102adabSPaul E. McKenney 				  STATE_RCU_HEAD_QUEUED);
1974102adabSPaul E. McKenney 	return r1;
1984102adabSPaul E. McKenney }
1994102adabSPaul E. McKenney 
2004102adabSPaul E. McKenney static inline void debug_rcu_head_unqueue(struct rcu_head *head)
2014102adabSPaul E. McKenney {
2024102adabSPaul E. McKenney 	debug_object_active_state(head, &rcuhead_debug_descr,
2034102adabSPaul E. McKenney 				  STATE_RCU_HEAD_QUEUED,
2044102adabSPaul E. McKenney 				  STATE_RCU_HEAD_READY);
2054102adabSPaul E. McKenney 	debug_object_deactivate(head, &rcuhead_debug_descr);
2064102adabSPaul E. McKenney }
2074102adabSPaul E. McKenney #else	/* !CONFIG_DEBUG_OBJECTS_RCU_HEAD */
2084102adabSPaul E. McKenney static inline int debug_rcu_head_queue(struct rcu_head *head)
2094102adabSPaul E. McKenney {
2104102adabSPaul E. McKenney 	return 0;
2114102adabSPaul E. McKenney }
2124102adabSPaul E. McKenney 
2134102adabSPaul E. McKenney static inline void debug_rcu_head_unqueue(struct rcu_head *head)
2144102adabSPaul E. McKenney {
2154102adabSPaul E. McKenney }
2164102adabSPaul E. McKenney #endif	/* #else !CONFIG_DEBUG_OBJECTS_RCU_HEAD */
2174102adabSPaul E. McKenney 
218bd73a7f5STeodora Baluta void kfree(const void *);
2194102adabSPaul E. McKenney 
220406e3e53SPaul E. McKenney /*
221406e3e53SPaul E. McKenney  * Reclaim the specified callback, either by invoking it (non-lazy case)
222406e3e53SPaul E. McKenney  * or freeing it directly (lazy case).  Return true if lazy, false otherwise.
223406e3e53SPaul E. McKenney  */
2244102adabSPaul E. McKenney static inline bool __rcu_reclaim(const char *rn, struct rcu_head *head)
2254102adabSPaul E. McKenney {
2264102adabSPaul E. McKenney 	unsigned long offset = (unsigned long)head->func;
2274102adabSPaul E. McKenney 
22824ef659aSPaul E. McKenney 	rcu_lock_acquire(&rcu_callback_map);
2294102adabSPaul E. McKenney 	if (__is_kfree_rcu_offset(offset)) {
230dffd06a7SPaul E. McKenney 		RCU_TRACE(trace_rcu_invoke_kfree_callback(rn, head, offset);)
2314102adabSPaul E. McKenney 		kfree((void *)head - offset);
23224ef659aSPaul E. McKenney 		rcu_lock_release(&rcu_callback_map);
233406e3e53SPaul E. McKenney 		return true;
2344102adabSPaul E. McKenney 	} else {
235dffd06a7SPaul E. McKenney 		RCU_TRACE(trace_rcu_invoke_callback(rn, head);)
2364102adabSPaul E. McKenney 		head->func(head);
23724ef659aSPaul E. McKenney 		rcu_lock_release(&rcu_callback_map);
238406e3e53SPaul E. McKenney 		return false;
2394102adabSPaul E. McKenney 	}
2404102adabSPaul E. McKenney }
2414102adabSPaul E. McKenney 
2424102adabSPaul E. McKenney #ifdef CONFIG_RCU_STALL_COMMON
2434102adabSPaul E. McKenney 
2444102adabSPaul E. McKenney extern int rcu_cpu_stall_suppress;
2454102adabSPaul E. McKenney int rcu_jiffies_till_stall_check(void);
2464102adabSPaul E. McKenney 
247f22ce091SPaul E. McKenney #define rcu_ftrace_dump_stall_suppress() \
248f22ce091SPaul E. McKenney do { \
249f22ce091SPaul E. McKenney 	if (!rcu_cpu_stall_suppress) \
250f22ce091SPaul E. McKenney 		rcu_cpu_stall_suppress = 3; \
251f22ce091SPaul E. McKenney } while (0)
252f22ce091SPaul E. McKenney 
253f22ce091SPaul E. McKenney #define rcu_ftrace_dump_stall_unsuppress() \
254f22ce091SPaul E. McKenney do { \
255f22ce091SPaul E. McKenney 	if (rcu_cpu_stall_suppress == 3) \
256f22ce091SPaul E. McKenney 		rcu_cpu_stall_suppress = 0; \
257f22ce091SPaul E. McKenney } while (0)
258f22ce091SPaul E. McKenney 
259f22ce091SPaul E. McKenney #else /* #endif #ifdef CONFIG_RCU_STALL_COMMON */
260f22ce091SPaul E. McKenney #define rcu_ftrace_dump_stall_suppress()
261f22ce091SPaul E. McKenney #define rcu_ftrace_dump_stall_unsuppress()
2624102adabSPaul E. McKenney #endif /* #ifdef CONFIG_RCU_STALL_COMMON */
2634102adabSPaul E. McKenney 
2644102adabSPaul E. McKenney /*
2654102adabSPaul E. McKenney  * Strings used in tracepoints need to be exported via the
2664102adabSPaul E. McKenney  * tracing system such that tools like perf and trace-cmd can
2674102adabSPaul E. McKenney  * translate the string address pointers to actual text.
2684102adabSPaul E. McKenney  */
2694102adabSPaul E. McKenney #define TPS(x)  tracepoint_string(x)
2704102adabSPaul E. McKenney 
271b8989b76SPaul E. McKenney /*
272b8989b76SPaul E. McKenney  * Dump the ftrace buffer, but only one time per callsite per boot.
273b8989b76SPaul E. McKenney  */
274b8989b76SPaul E. McKenney #define rcu_ftrace_dump(oops_dump_mode) \
275b8989b76SPaul E. McKenney do { \
276b8989b76SPaul E. McKenney 	static atomic_t ___rfd_beenhere = ATOMIC_INIT(0); \
277b8989b76SPaul E. McKenney 	\
278b8989b76SPaul E. McKenney 	if (!atomic_read(&___rfd_beenhere) && \
27983b6ca1fSPaul E. McKenney 	    !atomic_xchg(&___rfd_beenhere, 1)) { \
28083b6ca1fSPaul E. McKenney 		tracing_off(); \
281f22ce091SPaul E. McKenney 		rcu_ftrace_dump_stall_suppress(); \
282b8989b76SPaul E. McKenney 		ftrace_dump(oops_dump_mode); \
283f22ce091SPaul E. McKenney 		rcu_ftrace_dump_stall_unsuppress(); \
28483b6ca1fSPaul E. McKenney 	} \
285b8989b76SPaul E. McKenney } while (0)
286b8989b76SPaul E. McKenney 
287aa23c6fbSPranith Kumar void rcu_early_boot_tests(void);
28852d7e48bSPaul E. McKenney void rcu_test_sync_prims(void);
289aa23c6fbSPranith Kumar 
2905f6130faSLai Jiangshan /*
2915f6130faSLai Jiangshan  * This function really isn't for public consumption, but RCU is special in
2925f6130faSLai Jiangshan  * that context switches can allow the state machine to make progress.
2935f6130faSLai Jiangshan  */
2945f6130faSLai Jiangshan extern void resched_cpu(int cpu);
2955f6130faSLai Jiangshan 
2962b34c43cSPaul E. McKenney #if defined(SRCU) || !defined(TINY_RCU)
2972b34c43cSPaul E. McKenney 
2982b34c43cSPaul E. McKenney #include <linux/rcu_node_tree.h>
2992b34c43cSPaul E. McKenney 
3002b34c43cSPaul E. McKenney extern int rcu_num_lvls;
301e95d68d2SPaul E. McKenney extern int num_rcu_lvl[];
3022b34c43cSPaul E. McKenney extern int rcu_num_nodes;
3032b34c43cSPaul E. McKenney static bool rcu_fanout_exact;
3042b34c43cSPaul E. McKenney static int rcu_fanout_leaf;
3052b34c43cSPaul E. McKenney 
3062b34c43cSPaul E. McKenney /*
3072b34c43cSPaul E. McKenney  * Compute the per-level fanout, either using the exact fanout specified
3082b34c43cSPaul E. McKenney  * or balancing the tree, depending on the rcu_fanout_exact boot parameter.
3092b34c43cSPaul E. McKenney  */
3102b34c43cSPaul E. McKenney static inline void rcu_init_levelspread(int *levelspread, const int *levelcnt)
3112b34c43cSPaul E. McKenney {
3122b34c43cSPaul E. McKenney 	int i;
3132b34c43cSPaul E. McKenney 
3142b34c43cSPaul E. McKenney 	if (rcu_fanout_exact) {
3152b34c43cSPaul E. McKenney 		levelspread[rcu_num_lvls - 1] = rcu_fanout_leaf;
3162b34c43cSPaul E. McKenney 		for (i = rcu_num_lvls - 2; i >= 0; i--)
3172b34c43cSPaul E. McKenney 			levelspread[i] = RCU_FANOUT;
3182b34c43cSPaul E. McKenney 	} else {
3192b34c43cSPaul E. McKenney 		int ccur;
3202b34c43cSPaul E. McKenney 		int cprv;
3212b34c43cSPaul E. McKenney 
3222b34c43cSPaul E. McKenney 		cprv = nr_cpu_ids;
3232b34c43cSPaul E. McKenney 		for (i = rcu_num_lvls - 1; i >= 0; i--) {
3242b34c43cSPaul E. McKenney 			ccur = levelcnt[i];
3252b34c43cSPaul E. McKenney 			levelspread[i] = (cprv + ccur - 1) / ccur;
3262b34c43cSPaul E. McKenney 			cprv = ccur;
3272b34c43cSPaul E. McKenney 		}
3282b34c43cSPaul E. McKenney 	}
3292b34c43cSPaul E. McKenney }
3302b34c43cSPaul E. McKenney 
3315b4c11d5SPaul E. McKenney /* Returns first leaf rcu_node of the specified RCU flavor. */
3325b4c11d5SPaul E. McKenney #define rcu_first_leaf_node(rsp) ((rsp)->level[rcu_num_lvls - 1])
3335b4c11d5SPaul E. McKenney 
3345b4c11d5SPaul E. McKenney /* Is this rcu_node a leaf? */
3355b4c11d5SPaul E. McKenney #define rcu_is_leaf_node(rnp) ((rnp)->level == rcu_num_lvls - 1)
3365b4c11d5SPaul E. McKenney 
3375257514dSPaul E. McKenney /* Is this rcu_node the last leaf? */
3385257514dSPaul E. McKenney #define rcu_is_last_leaf_node(rsp, rnp) ((rnp) == &(rsp)->node[rcu_num_nodes - 1])
3395257514dSPaul E. McKenney 
340efbe451dSPaul E. McKenney /*
341efbe451dSPaul E. McKenney  * Do a full breadth-first scan of the rcu_node structures for the
342efbe451dSPaul E. McKenney  * specified rcu_state structure.
343efbe451dSPaul E. McKenney  */
344efbe451dSPaul E. McKenney #define rcu_for_each_node_breadth_first(rsp, rnp) \
345efbe451dSPaul E. McKenney 	for ((rnp) = &(rsp)->node[0]; \
346efbe451dSPaul E. McKenney 	     (rnp) < &(rsp)->node[rcu_num_nodes]; (rnp)++)
347efbe451dSPaul E. McKenney 
348efbe451dSPaul E. McKenney /*
349efbe451dSPaul E. McKenney  * Do a breadth-first scan of the non-leaf rcu_node structures for the
350efbe451dSPaul E. McKenney  * specified rcu_state structure.  Note that if there is a singleton
351efbe451dSPaul E. McKenney  * rcu_node tree with but one rcu_node structure, this loop is a no-op.
352efbe451dSPaul E. McKenney  */
353efbe451dSPaul E. McKenney #define rcu_for_each_nonleaf_node_breadth_first(rsp, rnp) \
3545b4c11d5SPaul E. McKenney 	for ((rnp) = &(rsp)->node[0]; !rcu_is_leaf_node(rsp, rnp); (rnp)++)
355efbe451dSPaul E. McKenney 
356efbe451dSPaul E. McKenney /*
357efbe451dSPaul E. McKenney  * Scan the leaves of the rcu_node hierarchy for the specified rcu_state
358efbe451dSPaul E. McKenney  * structure.  Note that if there is a singleton rcu_node tree with but
359efbe451dSPaul E. McKenney  * one rcu_node structure, this loop -will- visit the rcu_node structure.
360efbe451dSPaul E. McKenney  * It is still a leaf node, even if it is also the root node.
361efbe451dSPaul E. McKenney  */
362efbe451dSPaul E. McKenney #define rcu_for_each_leaf_node(rsp, rnp) \
3635b4c11d5SPaul E. McKenney 	for ((rnp) = rcu_first_leaf_node(rsp); \
364efbe451dSPaul E. McKenney 	     (rnp) < &(rsp)->node[rcu_num_nodes]; (rnp)++)
365efbe451dSPaul E. McKenney 
366efbe451dSPaul E. McKenney /*
367efbe451dSPaul E. McKenney  * Iterate over all possible CPUs in a leaf RCU node.
368efbe451dSPaul E. McKenney  */
369efbe451dSPaul E. McKenney #define for_each_leaf_node_possible_cpu(rnp, cpu) \
37065963d24SPaul E. McKenney 	for ((cpu) = cpumask_next((rnp)->grplo - 1, cpu_possible_mask); \
37165963d24SPaul E. McKenney 	     (cpu) <= rnp->grphi; \
37265963d24SPaul E. McKenney 	     (cpu) = cpumask_next((cpu), cpu_possible_mask))
37365963d24SPaul E. McKenney 
37465963d24SPaul E. McKenney /*
37565963d24SPaul E. McKenney  * Iterate over all CPUs in a leaf RCU node's specified mask.
37665963d24SPaul E. McKenney  */
37765963d24SPaul E. McKenney #define rcu_find_next_bit(rnp, cpu, mask) \
37865963d24SPaul E. McKenney 	((rnp)->grplo + find_next_bit(&(mask), BITS_PER_LONG, (cpu)))
37965963d24SPaul E. McKenney #define for_each_leaf_node_cpu_mask(rnp, cpu, mask) \
38065963d24SPaul E. McKenney 	for ((cpu) = rcu_find_next_bit((rnp), 0, (mask)); \
38165963d24SPaul E. McKenney 	     (cpu) <= rnp->grphi; \
38265963d24SPaul E. McKenney 	     (cpu) = rcu_find_next_bit((rnp), (cpu) + 1 - (rnp->grplo), (mask)))
383efbe451dSPaul E. McKenney 
38483d40bd3SPaul E. McKenney /*
38583d40bd3SPaul E. McKenney  * Wrappers for the rcu_node::lock acquire and release.
38683d40bd3SPaul E. McKenney  *
38783d40bd3SPaul E. McKenney  * Because the rcu_nodes form a tree, the tree traversal locking will observe
38883d40bd3SPaul E. McKenney  * different lock values, this in turn means that an UNLOCK of one level
38983d40bd3SPaul E. McKenney  * followed by a LOCK of another level does not imply a full memory barrier;
39083d40bd3SPaul E. McKenney  * and most importantly transitivity is lost.
39183d40bd3SPaul E. McKenney  *
39283d40bd3SPaul E. McKenney  * In order to restore full ordering between tree levels, augment the regular
39383d40bd3SPaul E. McKenney  * lock acquire functions with smp_mb__after_unlock_lock().
39483d40bd3SPaul E. McKenney  *
39583d40bd3SPaul E. McKenney  * As ->lock of struct rcu_node is a __private field, therefore one should use
39683d40bd3SPaul E. McKenney  * these wrappers rather than directly call raw_spin_{lock,unlock}* on ->lock.
39783d40bd3SPaul E. McKenney  */
39883d40bd3SPaul E. McKenney #define raw_spin_lock_rcu_node(p)					\
39983d40bd3SPaul E. McKenney do {									\
40083d40bd3SPaul E. McKenney 	raw_spin_lock(&ACCESS_PRIVATE(p, lock));			\
40183d40bd3SPaul E. McKenney 	smp_mb__after_unlock_lock();					\
40283d40bd3SPaul E. McKenney } while (0)
40383d40bd3SPaul E. McKenney 
40483d40bd3SPaul E. McKenney #define raw_spin_unlock_rcu_node(p) raw_spin_unlock(&ACCESS_PRIVATE(p, lock))
40583d40bd3SPaul E. McKenney 
40683d40bd3SPaul E. McKenney #define raw_spin_lock_irq_rcu_node(p)					\
40783d40bd3SPaul E. McKenney do {									\
40883d40bd3SPaul E. McKenney 	raw_spin_lock_irq(&ACCESS_PRIVATE(p, lock));			\
40983d40bd3SPaul E. McKenney 	smp_mb__after_unlock_lock();					\
41083d40bd3SPaul E. McKenney } while (0)
41183d40bd3SPaul E. McKenney 
41283d40bd3SPaul E. McKenney #define raw_spin_unlock_irq_rcu_node(p)					\
41383d40bd3SPaul E. McKenney 	raw_spin_unlock_irq(&ACCESS_PRIVATE(p, lock))
41483d40bd3SPaul E. McKenney 
4154e4bea74SPaul E. McKenney #define raw_spin_lock_irqsave_rcu_node(p, flags)			\
41683d40bd3SPaul E. McKenney do {									\
4174e4bea74SPaul E. McKenney 	raw_spin_lock_irqsave(&ACCESS_PRIVATE(p, lock), flags);	\
41883d40bd3SPaul E. McKenney 	smp_mb__after_unlock_lock();					\
41983d40bd3SPaul E. McKenney } while (0)
42083d40bd3SPaul E. McKenney 
4214e4bea74SPaul E. McKenney #define raw_spin_unlock_irqrestore_rcu_node(p, flags)			\
422a32e01eeSMatthew Wilcox 	raw_spin_unlock_irqrestore(&ACCESS_PRIVATE(p, lock), flags)
42383d40bd3SPaul E. McKenney 
42483d40bd3SPaul E. McKenney #define raw_spin_trylock_rcu_node(p)					\
42583d40bd3SPaul E. McKenney ({									\
42683d40bd3SPaul E. McKenney 	bool ___locked = raw_spin_trylock(&ACCESS_PRIVATE(p, lock));	\
42783d40bd3SPaul E. McKenney 									\
42883d40bd3SPaul E. McKenney 	if (___locked)							\
42983d40bd3SPaul E. McKenney 		smp_mb__after_unlock_lock();				\
43083d40bd3SPaul E. McKenney 	___locked;							\
43183d40bd3SPaul E. McKenney })
43283d40bd3SPaul E. McKenney 
433a32e01eeSMatthew Wilcox #define raw_lockdep_assert_held_rcu_node(p)				\
434a32e01eeSMatthew Wilcox 	lockdep_assert_held(&ACCESS_PRIVATE(p, lock))
435a32e01eeSMatthew Wilcox 
4362b34c43cSPaul E. McKenney #endif /* #if defined(SRCU) || !defined(TINY_RCU) */
4372b34c43cSPaul E. McKenney 
43825c36329SPaul E. McKenney #ifdef CONFIG_TINY_RCU
43925c36329SPaul E. McKenney /* Tiny RCU doesn't expedite, as its purpose in life is instead to be tiny. */
4407414fac0SPaul E. McKenney static inline bool rcu_gp_is_normal(void) { return true; }
4417414fac0SPaul E. McKenney static inline bool rcu_gp_is_expedited(void) { return false; }
4427414fac0SPaul E. McKenney static inline void rcu_expedite_gp(void) { }
4437414fac0SPaul E. McKenney static inline void rcu_unexpedite_gp(void) { }
444bfbd767dSPaul E. McKenney static inline void rcu_request_urgent_qs_task(struct task_struct *t) { }
44525c36329SPaul E. McKenney #else /* #ifdef CONFIG_TINY_RCU */
44625c36329SPaul E. McKenney bool rcu_gp_is_normal(void);     /* Internal RCU use. */
44725c36329SPaul E. McKenney bool rcu_gp_is_expedited(void);  /* Internal RCU use. */
44825c36329SPaul E. McKenney void rcu_expedite_gp(void);
44925c36329SPaul E. McKenney void rcu_unexpedite_gp(void);
45025c36329SPaul E. McKenney void rcupdate_announce_bootup_oddness(void);
451bfbd767dSPaul E. McKenney void rcu_request_urgent_qs_task(struct task_struct *t);
45225c36329SPaul E. McKenney #endif /* #else #ifdef CONFIG_TINY_RCU */
45325c36329SPaul E. McKenney 
45482118249SPaul E. McKenney #define RCU_SCHEDULER_INACTIVE	0
45582118249SPaul E. McKenney #define RCU_SCHEDULER_INIT	1
45682118249SPaul E. McKenney #define RCU_SCHEDULER_RUNNING	2
45782118249SPaul E. McKenney 
458cad7b389SPaul E. McKenney enum rcutorture_type {
459cad7b389SPaul E. McKenney 	RCU_FLAVOR,
460cad7b389SPaul E. McKenney 	RCU_BH_FLAVOR,
461cad7b389SPaul E. McKenney 	RCU_SCHED_FLAVOR,
462cad7b389SPaul E. McKenney 	RCU_TASKS_FLAVOR,
463cad7b389SPaul E. McKenney 	SRCU_FLAVOR,
464cad7b389SPaul E. McKenney 	INVALID_RCU_FLAVOR
465cad7b389SPaul E. McKenney };
466cad7b389SPaul E. McKenney 
467cad7b389SPaul E. McKenney #if defined(CONFIG_TREE_RCU) || defined(CONFIG_PREEMPT_RCU)
468cad7b389SPaul E. McKenney void rcutorture_get_gp_data(enum rcutorture_type test_type, int *flags,
469aebc8264SPaul E. McKenney 			    unsigned long *gp_seq);
470cad7b389SPaul E. McKenney void rcutorture_record_test_transition(void);
471cad7b389SPaul E. McKenney void rcutorture_record_progress(unsigned long vernum);
472cad7b389SPaul E. McKenney void do_trace_rcu_torture_read(const char *rcutorturename,
473cad7b389SPaul E. McKenney 			       struct rcu_head *rhp,
474cad7b389SPaul E. McKenney 			       unsigned long secs,
475cad7b389SPaul E. McKenney 			       unsigned long c_old,
476cad7b389SPaul E. McKenney 			       unsigned long c);
477cad7b389SPaul E. McKenney #else
478cad7b389SPaul E. McKenney static inline void rcutorture_get_gp_data(enum rcutorture_type test_type,
479aebc8264SPaul E. McKenney 					  int *flags, unsigned long *gp_seq)
480cad7b389SPaul E. McKenney {
481cad7b389SPaul E. McKenney 	*flags = 0;
482aebc8264SPaul E. McKenney 	*gp_seq = 0;
483cad7b389SPaul E. McKenney }
4847414fac0SPaul E. McKenney static inline void rcutorture_record_test_transition(void) { }
4857414fac0SPaul E. McKenney static inline void rcutorture_record_progress(unsigned long vernum) { }
486cad7b389SPaul E. McKenney #ifdef CONFIG_RCU_TRACE
487cad7b389SPaul E. McKenney void do_trace_rcu_torture_read(const char *rcutorturename,
488cad7b389SPaul E. McKenney 			       struct rcu_head *rhp,
489cad7b389SPaul E. McKenney 			       unsigned long secs,
490cad7b389SPaul E. McKenney 			       unsigned long c_old,
491cad7b389SPaul E. McKenney 			       unsigned long c);
492cad7b389SPaul E. McKenney #else
493cad7b389SPaul E. McKenney #define do_trace_rcu_torture_read(rcutorturename, rhp, secs, c_old, c) \
494cad7b389SPaul E. McKenney 	do { } while (0)
495cad7b389SPaul E. McKenney #endif
496cad7b389SPaul E. McKenney #endif
497cad7b389SPaul E. McKenney 
498cad7b389SPaul E. McKenney #ifdef CONFIG_TINY_SRCU
499cad7b389SPaul E. McKenney 
500cad7b389SPaul E. McKenney static inline void srcutorture_get_gp_data(enum rcutorture_type test_type,
501cad7b389SPaul E. McKenney 					   struct srcu_struct *sp, int *flags,
502aebc8264SPaul E. McKenney 					   unsigned long *gp_seq)
503cad7b389SPaul E. McKenney {
504cad7b389SPaul E. McKenney 	if (test_type != SRCU_FLAVOR)
505cad7b389SPaul E. McKenney 		return;
506cad7b389SPaul E. McKenney 	*flags = 0;
507aebc8264SPaul E. McKenney 	*gp_seq = sp->srcu_idx;
508cad7b389SPaul E. McKenney }
509cad7b389SPaul E. McKenney 
510cad7b389SPaul E. McKenney #elif defined(CONFIG_TREE_SRCU)
511cad7b389SPaul E. McKenney 
512cad7b389SPaul E. McKenney void srcutorture_get_gp_data(enum rcutorture_type test_type,
513cad7b389SPaul E. McKenney 			     struct srcu_struct *sp, int *flags,
514aebc8264SPaul E. McKenney 			     unsigned long *gp_seq);
515cad7b389SPaul E. McKenney 
516cad7b389SPaul E. McKenney #endif
517cad7b389SPaul E. McKenney 
518e3c8d51eSPaul E. McKenney #ifdef CONFIG_TINY_RCU
51917ef2fe9SPaul E. McKenney static inline unsigned long rcu_get_gp_seq(void) { return 0; }
52017ef2fe9SPaul E. McKenney static inline unsigned long rcu_bh_get_gp_seq(void) { return 0; }
52117ef2fe9SPaul E. McKenney static inline unsigned long rcu_sched_get_gp_seq(void) { return 0; }
5227414fac0SPaul E. McKenney static inline unsigned long rcu_exp_batches_completed(void) { return 0; }
5237414fac0SPaul E. McKenney static inline unsigned long rcu_exp_batches_completed_sched(void) { return 0; }
5247414fac0SPaul E. McKenney static inline unsigned long
5257414fac0SPaul E. McKenney srcu_batches_completed(struct srcu_struct *sp) { return 0; }
5267414fac0SPaul E. McKenney static inline void rcu_force_quiescent_state(void) { }
5277414fac0SPaul E. McKenney static inline void rcu_bh_force_quiescent_state(void) { }
5287414fac0SPaul E. McKenney static inline void rcu_sched_force_quiescent_state(void) { }
5297414fac0SPaul E. McKenney static inline void show_rcu_gp_kthreads(void) { }
530e3c8d51eSPaul E. McKenney #else /* #ifdef CONFIG_TINY_RCU */
531e3c8d51eSPaul E. McKenney extern unsigned long rcutorture_testseq;
532e3c8d51eSPaul E. McKenney extern unsigned long rcutorture_vernum;
53317ef2fe9SPaul E. McKenney unsigned long rcu_get_gp_seq(void);
53417ef2fe9SPaul E. McKenney unsigned long rcu_bh_get_gp_seq(void);
53517ef2fe9SPaul E. McKenney unsigned long rcu_sched_get_gp_seq(void);
536e3c8d51eSPaul E. McKenney unsigned long rcu_exp_batches_completed(void);
537e3c8d51eSPaul E. McKenney unsigned long rcu_exp_batches_completed_sched(void);
5385a0465e1SPaul E. McKenney unsigned long srcu_batches_completed(struct srcu_struct *sp);
539e3c8d51eSPaul E. McKenney void show_rcu_gp_kthreads(void);
540e3c8d51eSPaul E. McKenney void rcu_force_quiescent_state(void);
541e3c8d51eSPaul E. McKenney void rcu_bh_force_quiescent_state(void);
542e3c8d51eSPaul E. McKenney void rcu_sched_force_quiescent_state(void);
543ad7c946bSPaul E. McKenney extern struct workqueue_struct *rcu_gp_wq;
54425f3d7efSPaul E. McKenney extern struct workqueue_struct *rcu_par_gp_wq;
545e3c8d51eSPaul E. McKenney #endif /* #else #ifdef CONFIG_TINY_RCU */
546e3c8d51eSPaul E. McKenney 
54744c65ff2SPaul E. McKenney #ifdef CONFIG_RCU_NOCB_CPU
5483d54f798SPaul E. McKenney bool rcu_is_nocb_cpu(int cpu);
5493d54f798SPaul E. McKenney #else
5503d54f798SPaul E. McKenney static inline bool rcu_is_nocb_cpu(int cpu) { return false; }
5513d54f798SPaul E. McKenney #endif
5523d54f798SPaul E. McKenney 
5534102adabSPaul E. McKenney #endif /* __LINUX_RCU_H */
554