xref: /openbmc/linux/kernel/rcu/rcu.h (revision e0aff97355575ac6a28a48a4217533a3953095c5)
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 {
1612ee5aca5SPaul E. McKenney 	unsigned long rnd_diff;
1622ee5aca5SPaul E. McKenney 
1632ee5aca5SPaul E. McKenney 	if (old == new)
1642ee5aca5SPaul E. McKenney 		return 0;
1652ee5aca5SPaul E. McKenney 	/*
1662ee5aca5SPaul E. McKenney 	 * Compute the number of grace periods (still shifted up), plus
1672ee5aca5SPaul E. McKenney 	 * one if either of new and old is not an exact grace period.
1682ee5aca5SPaul E. McKenney 	 */
1692ee5aca5SPaul E. McKenney 	rnd_diff = (new & ~RCU_SEQ_STATE_MASK) -
1702ee5aca5SPaul E. McKenney 		   ((old + RCU_SEQ_STATE_MASK) & ~RCU_SEQ_STATE_MASK) +
1712ee5aca5SPaul E. McKenney 		   ((new & RCU_SEQ_STATE_MASK) || (old & RCU_SEQ_STATE_MASK));
1722ee5aca5SPaul E. McKenney 	if (ULONG_CMP_GE(RCU_SEQ_STATE_MASK, rnd_diff))
1732ee5aca5SPaul E. McKenney 		return 1; /* Definitely no grace period has elapsed. */
1742ee5aca5SPaul 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
1797f87c036SPaul E. McKenney  * by call_rcu() and rcu callback execution, and are therefore not part
1807f87c036SPaul E. McKenney  * of the RCU API. These are in rcupdate.h because they are used by all
1817f87c036SPaul E. McKenney  * RCU implementations.
1824102adabSPaul E. McKenney  */
1834102adabSPaul E. McKenney 
1844102adabSPaul E. McKenney #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD
1854102adabSPaul E. McKenney # define STATE_RCU_HEAD_READY	0
1864102adabSPaul E. McKenney # define STATE_RCU_HEAD_QUEUED	1
1874102adabSPaul E. McKenney 
1884102adabSPaul E. McKenney extern struct debug_obj_descr rcuhead_debug_descr;
1894102adabSPaul E. McKenney 
1904102adabSPaul E. McKenney static inline int debug_rcu_head_queue(struct rcu_head *head)
1914102adabSPaul E. McKenney {
1924102adabSPaul E. McKenney 	int r1;
1934102adabSPaul E. McKenney 
1944102adabSPaul E. McKenney 	r1 = debug_object_activate(head, &rcuhead_debug_descr);
1954102adabSPaul E. McKenney 	debug_object_active_state(head, &rcuhead_debug_descr,
1964102adabSPaul E. McKenney 				  STATE_RCU_HEAD_READY,
1974102adabSPaul E. McKenney 				  STATE_RCU_HEAD_QUEUED);
1984102adabSPaul E. McKenney 	return r1;
1994102adabSPaul E. McKenney }
2004102adabSPaul E. McKenney 
2014102adabSPaul E. McKenney static inline void debug_rcu_head_unqueue(struct rcu_head *head)
2024102adabSPaul E. McKenney {
2034102adabSPaul E. McKenney 	debug_object_active_state(head, &rcuhead_debug_descr,
2044102adabSPaul E. McKenney 				  STATE_RCU_HEAD_QUEUED,
2054102adabSPaul E. McKenney 				  STATE_RCU_HEAD_READY);
2064102adabSPaul E. McKenney 	debug_object_deactivate(head, &rcuhead_debug_descr);
2074102adabSPaul E. McKenney }
2084102adabSPaul E. McKenney #else	/* !CONFIG_DEBUG_OBJECTS_RCU_HEAD */
2094102adabSPaul E. McKenney static inline int debug_rcu_head_queue(struct rcu_head *head)
2104102adabSPaul E. McKenney {
2114102adabSPaul E. McKenney 	return 0;
2124102adabSPaul E. McKenney }
2134102adabSPaul E. McKenney 
2144102adabSPaul E. McKenney static inline void debug_rcu_head_unqueue(struct rcu_head *head)
2154102adabSPaul E. McKenney {
2164102adabSPaul E. McKenney }
2174102adabSPaul E. McKenney #endif	/* #else !CONFIG_DEBUG_OBJECTS_RCU_HEAD */
2184102adabSPaul E. McKenney 
219bd73a7f5STeodora Baluta void kfree(const void *);
2204102adabSPaul E. McKenney 
221406e3e53SPaul E. McKenney /*
222406e3e53SPaul E. McKenney  * Reclaim the specified callback, either by invoking it (non-lazy case)
223406e3e53SPaul E. McKenney  * or freeing it directly (lazy case).  Return true if lazy, false otherwise.
224406e3e53SPaul E. McKenney  */
2254102adabSPaul E. McKenney static inline bool __rcu_reclaim(const char *rn, struct rcu_head *head)
2264102adabSPaul E. McKenney {
22774de6960SPaul E. McKenney 	rcu_callback_t f;
2284102adabSPaul E. McKenney 	unsigned long offset = (unsigned long)head->func;
2294102adabSPaul E. McKenney 
23024ef659aSPaul E. McKenney 	rcu_lock_acquire(&rcu_callback_map);
2314102adabSPaul E. McKenney 	if (__is_kfree_rcu_offset(offset)) {
232dffd06a7SPaul E. McKenney 		RCU_TRACE(trace_rcu_invoke_kfree_callback(rn, head, offset);)
2334102adabSPaul E. McKenney 		kfree((void *)head - offset);
23424ef659aSPaul E. McKenney 		rcu_lock_release(&rcu_callback_map);
235406e3e53SPaul E. McKenney 		return true;
2364102adabSPaul E. McKenney 	} else {
237dffd06a7SPaul E. McKenney 		RCU_TRACE(trace_rcu_invoke_callback(rn, head);)
23874de6960SPaul E. McKenney 		f = head->func;
23974de6960SPaul E. McKenney 		WRITE_ONCE(head->func, (rcu_callback_t)0L);
24074de6960SPaul E. McKenney 		f(head);
24124ef659aSPaul E. McKenney 		rcu_lock_release(&rcu_callback_map);
242406e3e53SPaul E. McKenney 		return false;
2434102adabSPaul E. McKenney 	}
2444102adabSPaul E. McKenney }
2454102adabSPaul E. McKenney 
2464102adabSPaul E. McKenney #ifdef CONFIG_RCU_STALL_COMMON
2474102adabSPaul E. McKenney 
2484102adabSPaul E. McKenney extern int rcu_cpu_stall_suppress;
2494102adabSPaul E. McKenney int rcu_jiffies_till_stall_check(void);
2504102adabSPaul E. McKenney 
251f22ce091SPaul E. McKenney #define rcu_ftrace_dump_stall_suppress() \
252f22ce091SPaul E. McKenney do { \
253f22ce091SPaul E. McKenney 	if (!rcu_cpu_stall_suppress) \
254f22ce091SPaul E. McKenney 		rcu_cpu_stall_suppress = 3; \
255f22ce091SPaul E. McKenney } while (0)
256f22ce091SPaul E. McKenney 
257f22ce091SPaul E. McKenney #define rcu_ftrace_dump_stall_unsuppress() \
258f22ce091SPaul E. McKenney do { \
259f22ce091SPaul E. McKenney 	if (rcu_cpu_stall_suppress == 3) \
260f22ce091SPaul E. McKenney 		rcu_cpu_stall_suppress = 0; \
261f22ce091SPaul E. McKenney } while (0)
262f22ce091SPaul E. McKenney 
263f22ce091SPaul E. McKenney #else /* #endif #ifdef CONFIG_RCU_STALL_COMMON */
264f22ce091SPaul E. McKenney #define rcu_ftrace_dump_stall_suppress()
265f22ce091SPaul E. McKenney #define rcu_ftrace_dump_stall_unsuppress()
2664102adabSPaul E. McKenney #endif /* #ifdef CONFIG_RCU_STALL_COMMON */
2674102adabSPaul E. McKenney 
2684102adabSPaul E. McKenney /*
2694102adabSPaul E. McKenney  * Strings used in tracepoints need to be exported via the
2704102adabSPaul E. McKenney  * tracing system such that tools like perf and trace-cmd can
2714102adabSPaul E. McKenney  * translate the string address pointers to actual text.
2724102adabSPaul E. McKenney  */
2734102adabSPaul E. McKenney #define TPS(x)  tracepoint_string(x)
2744102adabSPaul E. McKenney 
275b8989b76SPaul E. McKenney /*
276b8989b76SPaul E. McKenney  * Dump the ftrace buffer, but only one time per callsite per boot.
277b8989b76SPaul E. McKenney  */
278b8989b76SPaul E. McKenney #define rcu_ftrace_dump(oops_dump_mode) \
279b8989b76SPaul E. McKenney do { \
280b8989b76SPaul E. McKenney 	static atomic_t ___rfd_beenhere = ATOMIC_INIT(0); \
281b8989b76SPaul E. McKenney 	\
282b8989b76SPaul E. McKenney 	if (!atomic_read(&___rfd_beenhere) && \
28383b6ca1fSPaul E. McKenney 	    !atomic_xchg(&___rfd_beenhere, 1)) { \
28483b6ca1fSPaul E. McKenney 		tracing_off(); \
285f22ce091SPaul E. McKenney 		rcu_ftrace_dump_stall_suppress(); \
286b8989b76SPaul E. McKenney 		ftrace_dump(oops_dump_mode); \
287f22ce091SPaul E. McKenney 		rcu_ftrace_dump_stall_unsuppress(); \
28883b6ca1fSPaul E. McKenney 	} \
289b8989b76SPaul E. McKenney } while (0)
290b8989b76SPaul E. McKenney 
291aa23c6fbSPranith Kumar void rcu_early_boot_tests(void);
29252d7e48bSPaul E. McKenney void rcu_test_sync_prims(void);
293aa23c6fbSPranith Kumar 
2945f6130faSLai Jiangshan /*
2955f6130faSLai Jiangshan  * This function really isn't for public consumption, but RCU is special in
2965f6130faSLai Jiangshan  * that context switches can allow the state machine to make progress.
2975f6130faSLai Jiangshan  */
2985f6130faSLai Jiangshan extern void resched_cpu(int cpu);
2995f6130faSLai Jiangshan 
3002b34c43cSPaul E. McKenney #if defined(SRCU) || !defined(TINY_RCU)
3012b34c43cSPaul E. McKenney 
3022b34c43cSPaul E. McKenney #include <linux/rcu_node_tree.h>
3032b34c43cSPaul E. McKenney 
3042b34c43cSPaul E. McKenney extern int rcu_num_lvls;
305e95d68d2SPaul E. McKenney extern int num_rcu_lvl[];
3062b34c43cSPaul E. McKenney extern int rcu_num_nodes;
3072b34c43cSPaul E. McKenney static bool rcu_fanout_exact;
3082b34c43cSPaul E. McKenney static int rcu_fanout_leaf;
3092b34c43cSPaul E. McKenney 
3102b34c43cSPaul E. McKenney /*
3112b34c43cSPaul E. McKenney  * Compute the per-level fanout, either using the exact fanout specified
3122b34c43cSPaul E. McKenney  * or balancing the tree, depending on the rcu_fanout_exact boot parameter.
3132b34c43cSPaul E. McKenney  */
3142b34c43cSPaul E. McKenney static inline void rcu_init_levelspread(int *levelspread, const int *levelcnt)
3152b34c43cSPaul E. McKenney {
3162b34c43cSPaul E. McKenney 	int i;
3172b34c43cSPaul E. McKenney 
3182b34c43cSPaul E. McKenney 	if (rcu_fanout_exact) {
3192b34c43cSPaul E. McKenney 		levelspread[rcu_num_lvls - 1] = rcu_fanout_leaf;
3202b34c43cSPaul E. McKenney 		for (i = rcu_num_lvls - 2; i >= 0; i--)
3212b34c43cSPaul E. McKenney 			levelspread[i] = RCU_FANOUT;
3222b34c43cSPaul E. McKenney 	} else {
3232b34c43cSPaul E. McKenney 		int ccur;
3242b34c43cSPaul E. McKenney 		int cprv;
3252b34c43cSPaul E. McKenney 
3262b34c43cSPaul E. McKenney 		cprv = nr_cpu_ids;
3272b34c43cSPaul E. McKenney 		for (i = rcu_num_lvls - 1; i >= 0; i--) {
3282b34c43cSPaul E. McKenney 			ccur = levelcnt[i];
3292b34c43cSPaul E. McKenney 			levelspread[i] = (cprv + ccur - 1) / ccur;
3302b34c43cSPaul E. McKenney 			cprv = ccur;
3312b34c43cSPaul E. McKenney 		}
3322b34c43cSPaul E. McKenney 	}
3332b34c43cSPaul E. McKenney }
3342b34c43cSPaul E. McKenney 
3357f87c036SPaul E. McKenney /* Returns a pointer to the first leaf rcu_node structure. */
336aedf4ba9SPaul E. McKenney #define rcu_first_leaf_node() (rcu_state.level[rcu_num_lvls - 1])
3375b4c11d5SPaul E. McKenney 
3385b4c11d5SPaul E. McKenney /* Is this rcu_node a leaf? */
3395b4c11d5SPaul E. McKenney #define rcu_is_leaf_node(rnp) ((rnp)->level == rcu_num_lvls - 1)
3405b4c11d5SPaul E. McKenney 
3415257514dSPaul E. McKenney /* Is this rcu_node the last leaf? */
342aedf4ba9SPaul E. McKenney #define rcu_is_last_leaf_node(rnp) ((rnp) == &rcu_state.node[rcu_num_nodes - 1])
3435257514dSPaul E. McKenney 
344efbe451dSPaul E. McKenney /*
345aedf4ba9SPaul E. McKenney  * Do a full breadth-first scan of the {s,}rcu_node structures for the
3467f87c036SPaul E. McKenney  * specified state structure (for SRCU) or the only rcu_state structure
3477f87c036SPaul E. McKenney  * (for RCU).
348efbe451dSPaul E. McKenney  */
349aedf4ba9SPaul E. McKenney #define srcu_for_each_node_breadth_first(sp, rnp) \
350aedf4ba9SPaul E. McKenney 	for ((rnp) = &(sp)->node[0]; \
351aedf4ba9SPaul E. McKenney 	     (rnp) < &(sp)->node[rcu_num_nodes]; (rnp)++)
352aedf4ba9SPaul E. McKenney #define rcu_for_each_node_breadth_first(rnp) \
353aedf4ba9SPaul E. McKenney 	srcu_for_each_node_breadth_first(&rcu_state, rnp)
354efbe451dSPaul E. McKenney 
355efbe451dSPaul E. McKenney /*
3567f87c036SPaul E. McKenney  * Scan the leaves of the rcu_node hierarchy for the rcu_state structure.
3577f87c036SPaul E. McKenney  * Note that if there is a singleton rcu_node tree with but one rcu_node
3587f87c036SPaul E. McKenney  * structure, this loop -will- visit the rcu_node structure.  It is still
3597f87c036SPaul E. McKenney  * a leaf node, even if it is also the root node.
360efbe451dSPaul E. McKenney  */
361aedf4ba9SPaul E. McKenney #define rcu_for_each_leaf_node(rnp) \
362aedf4ba9SPaul E. McKenney 	for ((rnp) = rcu_first_leaf_node(); \
363aedf4ba9SPaul E. McKenney 	     (rnp) < &rcu_state.node[rcu_num_nodes]; (rnp)++)
364efbe451dSPaul E. McKenney 
365efbe451dSPaul E. McKenney /*
366efbe451dSPaul E. McKenney  * Iterate over all possible CPUs in a leaf RCU node.
367efbe451dSPaul E. McKenney  */
368efbe451dSPaul E. McKenney #define for_each_leaf_node_possible_cpu(rnp, cpu) \
36965963d24SPaul E. McKenney 	for ((cpu) = cpumask_next((rnp)->grplo - 1, cpu_possible_mask); \
37065963d24SPaul E. McKenney 	     (cpu) <= rnp->grphi; \
37165963d24SPaul E. McKenney 	     (cpu) = cpumask_next((cpu), cpu_possible_mask))
37265963d24SPaul E. McKenney 
37365963d24SPaul E. McKenney /*
37465963d24SPaul E. McKenney  * Iterate over all CPUs in a leaf RCU node's specified mask.
37565963d24SPaul E. McKenney  */
37665963d24SPaul E. McKenney #define rcu_find_next_bit(rnp, cpu, mask) \
37765963d24SPaul E. McKenney 	((rnp)->grplo + find_next_bit(&(mask), BITS_PER_LONG, (cpu)))
37865963d24SPaul E. McKenney #define for_each_leaf_node_cpu_mask(rnp, cpu, mask) \
37965963d24SPaul E. McKenney 	for ((cpu) = rcu_find_next_bit((rnp), 0, (mask)); \
38065963d24SPaul E. McKenney 	     (cpu) <= rnp->grphi; \
38165963d24SPaul E. McKenney 	     (cpu) = rcu_find_next_bit((rnp), (cpu) + 1 - (rnp->grplo), (mask)))
382efbe451dSPaul E. McKenney 
38383d40bd3SPaul E. McKenney /*
38483d40bd3SPaul E. McKenney  * Wrappers for the rcu_node::lock acquire and release.
38583d40bd3SPaul E. McKenney  *
38683d40bd3SPaul E. McKenney  * Because the rcu_nodes form a tree, the tree traversal locking will observe
38783d40bd3SPaul E. McKenney  * different lock values, this in turn means that an UNLOCK of one level
38883d40bd3SPaul E. McKenney  * followed by a LOCK of another level does not imply a full memory barrier;
38983d40bd3SPaul E. McKenney  * and most importantly transitivity is lost.
39083d40bd3SPaul E. McKenney  *
39183d40bd3SPaul E. McKenney  * In order to restore full ordering between tree levels, augment the regular
39283d40bd3SPaul E. McKenney  * lock acquire functions with smp_mb__after_unlock_lock().
39383d40bd3SPaul E. McKenney  *
39483d40bd3SPaul E. McKenney  * As ->lock of struct rcu_node is a __private field, therefore one should use
39583d40bd3SPaul E. McKenney  * these wrappers rather than directly call raw_spin_{lock,unlock}* on ->lock.
39683d40bd3SPaul E. McKenney  */
39783d40bd3SPaul E. McKenney #define raw_spin_lock_rcu_node(p)					\
39883d40bd3SPaul E. McKenney do {									\
39983d40bd3SPaul E. McKenney 	raw_spin_lock(&ACCESS_PRIVATE(p, lock));			\
40083d40bd3SPaul E. McKenney 	smp_mb__after_unlock_lock();					\
40183d40bd3SPaul E. McKenney } while (0)
40283d40bd3SPaul E. McKenney 
40383d40bd3SPaul E. McKenney #define raw_spin_unlock_rcu_node(p) raw_spin_unlock(&ACCESS_PRIVATE(p, lock))
40483d40bd3SPaul E. McKenney 
40583d40bd3SPaul E. McKenney #define raw_spin_lock_irq_rcu_node(p)					\
40683d40bd3SPaul E. McKenney do {									\
40783d40bd3SPaul E. McKenney 	raw_spin_lock_irq(&ACCESS_PRIVATE(p, lock));			\
40883d40bd3SPaul E. McKenney 	smp_mb__after_unlock_lock();					\
40983d40bd3SPaul E. McKenney } while (0)
41083d40bd3SPaul E. McKenney 
41183d40bd3SPaul E. McKenney #define raw_spin_unlock_irq_rcu_node(p)					\
41283d40bd3SPaul E. McKenney 	raw_spin_unlock_irq(&ACCESS_PRIVATE(p, lock))
41383d40bd3SPaul E. McKenney 
4144e4bea74SPaul E. McKenney #define raw_spin_lock_irqsave_rcu_node(p, flags)			\
41583d40bd3SPaul E. McKenney do {									\
4164e4bea74SPaul E. McKenney 	raw_spin_lock_irqsave(&ACCESS_PRIVATE(p, lock), flags);	\
41783d40bd3SPaul E. McKenney 	smp_mb__after_unlock_lock();					\
41883d40bd3SPaul E. McKenney } while (0)
41983d40bd3SPaul E. McKenney 
4204e4bea74SPaul E. McKenney #define raw_spin_unlock_irqrestore_rcu_node(p, flags)			\
421a32e01eeSMatthew Wilcox 	raw_spin_unlock_irqrestore(&ACCESS_PRIVATE(p, lock), flags)
42283d40bd3SPaul E. McKenney 
42383d40bd3SPaul E. McKenney #define raw_spin_trylock_rcu_node(p)					\
42483d40bd3SPaul E. McKenney ({									\
42583d40bd3SPaul E. McKenney 	bool ___locked = raw_spin_trylock(&ACCESS_PRIVATE(p, lock));	\
42683d40bd3SPaul E. McKenney 									\
42783d40bd3SPaul E. McKenney 	if (___locked)							\
42883d40bd3SPaul E. McKenney 		smp_mb__after_unlock_lock();				\
42983d40bd3SPaul E. McKenney 	___locked;							\
43083d40bd3SPaul E. McKenney })
43183d40bd3SPaul E. McKenney 
432a32e01eeSMatthew Wilcox #define raw_lockdep_assert_held_rcu_node(p)				\
433a32e01eeSMatthew Wilcox 	lockdep_assert_held(&ACCESS_PRIVATE(p, lock))
434a32e01eeSMatthew Wilcox 
4352b34c43cSPaul E. McKenney #endif /* #if defined(SRCU) || !defined(TINY_RCU) */
4362b34c43cSPaul E. McKenney 
437e0fcba9aSPaul E. McKenney #ifdef CONFIG_SRCU
438e0fcba9aSPaul E. McKenney void srcu_init(void);
439e0fcba9aSPaul E. McKenney #else /* #ifdef CONFIG_SRCU */
440e0fcba9aSPaul E. McKenney static inline void srcu_init(void) { }
441e0fcba9aSPaul E. McKenney #endif /* #else #ifdef CONFIG_SRCU */
442e0fcba9aSPaul E. McKenney 
44325c36329SPaul E. McKenney #ifdef CONFIG_TINY_RCU
44425c36329SPaul E. McKenney /* Tiny RCU doesn't expedite, as its purpose in life is instead to be tiny. */
4457414fac0SPaul E. McKenney static inline bool rcu_gp_is_normal(void) { return true; }
4467414fac0SPaul E. McKenney static inline bool rcu_gp_is_expedited(void) { return false; }
4477414fac0SPaul E. McKenney static inline void rcu_expedite_gp(void) { }
4487414fac0SPaul E. McKenney static inline void rcu_unexpedite_gp(void) { }
449bfbd767dSPaul E. McKenney static inline void rcu_request_urgent_qs_task(struct task_struct *t) { }
45025c36329SPaul E. McKenney #else /* #ifdef CONFIG_TINY_RCU */
45125c36329SPaul E. McKenney bool rcu_gp_is_normal(void);     /* Internal RCU use. */
45225c36329SPaul E. McKenney bool rcu_gp_is_expedited(void);  /* Internal RCU use. */
45325c36329SPaul E. McKenney void rcu_expedite_gp(void);
45425c36329SPaul E. McKenney void rcu_unexpedite_gp(void);
45525c36329SPaul E. McKenney void rcupdate_announce_bootup_oddness(void);
456bfbd767dSPaul E. McKenney void rcu_request_urgent_qs_task(struct task_struct *t);
45725c36329SPaul E. McKenney #endif /* #else #ifdef CONFIG_TINY_RCU */
45825c36329SPaul E. McKenney 
45982118249SPaul E. McKenney #define RCU_SCHEDULER_INACTIVE	0
46082118249SPaul E. McKenney #define RCU_SCHEDULER_INIT	1
46182118249SPaul E. McKenney #define RCU_SCHEDULER_RUNNING	2
46282118249SPaul E. McKenney 
463cad7b389SPaul E. McKenney enum rcutorture_type {
464cad7b389SPaul E. McKenney 	RCU_FLAVOR,
465cad7b389SPaul E. McKenney 	RCU_BH_FLAVOR,
466cad7b389SPaul E. McKenney 	RCU_SCHED_FLAVOR,
467cad7b389SPaul E. McKenney 	RCU_TASKS_FLAVOR,
468cad7b389SPaul E. McKenney 	SRCU_FLAVOR,
469cad7b389SPaul E. McKenney 	INVALID_RCU_FLAVOR
470cad7b389SPaul E. McKenney };
471cad7b389SPaul E. McKenney 
472cad7b389SPaul E. McKenney #if defined(CONFIG_TREE_RCU) || defined(CONFIG_PREEMPT_RCU)
473cad7b389SPaul E. McKenney void rcutorture_get_gp_data(enum rcutorture_type test_type, int *flags,
474aebc8264SPaul E. McKenney 			    unsigned long *gp_seq);
475cad7b389SPaul E. McKenney void rcutorture_record_progress(unsigned long vernum);
476cad7b389SPaul E. McKenney void do_trace_rcu_torture_read(const char *rcutorturename,
477cad7b389SPaul E. McKenney 			       struct rcu_head *rhp,
478cad7b389SPaul E. McKenney 			       unsigned long secs,
479cad7b389SPaul E. McKenney 			       unsigned long c_old,
480cad7b389SPaul E. McKenney 			       unsigned long c);
481cad7b389SPaul E. McKenney #else
482cad7b389SPaul E. McKenney static inline void rcutorture_get_gp_data(enum rcutorture_type test_type,
483aebc8264SPaul E. McKenney 					  int *flags, unsigned long *gp_seq)
484cad7b389SPaul E. McKenney {
485cad7b389SPaul E. McKenney 	*flags = 0;
486aebc8264SPaul E. McKenney 	*gp_seq = 0;
487cad7b389SPaul E. McKenney }
4887414fac0SPaul E. McKenney static inline void rcutorture_record_progress(unsigned long vernum) { }
489cad7b389SPaul E. McKenney #ifdef CONFIG_RCU_TRACE
490cad7b389SPaul E. McKenney void do_trace_rcu_torture_read(const char *rcutorturename,
491cad7b389SPaul E. McKenney 			       struct rcu_head *rhp,
492cad7b389SPaul E. McKenney 			       unsigned long secs,
493cad7b389SPaul E. McKenney 			       unsigned long c_old,
494cad7b389SPaul E. McKenney 			       unsigned long c);
495cad7b389SPaul E. McKenney #else
496cad7b389SPaul E. McKenney #define do_trace_rcu_torture_read(rcutorturename, rhp, secs, c_old, c) \
497cad7b389SPaul E. McKenney 	do { } while (0)
498cad7b389SPaul E. McKenney #endif
499cad7b389SPaul E. McKenney #endif
500cad7b389SPaul E. McKenney 
501cad7b389SPaul E. McKenney #ifdef CONFIG_TINY_SRCU
502cad7b389SPaul E. McKenney 
503cad7b389SPaul E. McKenney static inline void srcutorture_get_gp_data(enum rcutorture_type test_type,
504cad7b389SPaul E. McKenney 					   struct srcu_struct *sp, int *flags,
505aebc8264SPaul E. McKenney 					   unsigned long *gp_seq)
506cad7b389SPaul E. McKenney {
507cad7b389SPaul E. McKenney 	if (test_type != SRCU_FLAVOR)
508cad7b389SPaul E. McKenney 		return;
509cad7b389SPaul E. McKenney 	*flags = 0;
510aebc8264SPaul E. McKenney 	*gp_seq = sp->srcu_idx;
511cad7b389SPaul E. McKenney }
512cad7b389SPaul E. McKenney 
513cad7b389SPaul E. McKenney #elif defined(CONFIG_TREE_SRCU)
514cad7b389SPaul E. McKenney 
515cad7b389SPaul E. McKenney void srcutorture_get_gp_data(enum rcutorture_type test_type,
516cad7b389SPaul E. McKenney 			     struct srcu_struct *sp, int *flags,
517aebc8264SPaul E. McKenney 			     unsigned long *gp_seq);
518cad7b389SPaul E. McKenney 
519cad7b389SPaul E. McKenney #endif
520cad7b389SPaul E. McKenney 
521e3c8d51eSPaul E. McKenney #ifdef CONFIG_TINY_RCU
52217ef2fe9SPaul E. McKenney static inline unsigned long rcu_get_gp_seq(void) { return 0; }
5237414fac0SPaul E. McKenney static inline unsigned long rcu_exp_batches_completed(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 show_rcu_gp_kthreads(void) { }
5284babd855SJoel Fernandes (Google) static inline int rcu_get_gp_kthreads_prio(void) { return 0; }
529*e0aff973SPaul E. McKenney static inline void rcu_fwd_progress_check(unsigned long j) { }
530e3c8d51eSPaul E. McKenney #else /* #ifdef CONFIG_TINY_RCU */
53117ef2fe9SPaul E. McKenney unsigned long rcu_get_gp_seq(void);
532e3c8d51eSPaul E. McKenney unsigned long rcu_exp_batches_completed(void);
5335a0465e1SPaul E. McKenney unsigned long srcu_batches_completed(struct srcu_struct *sp);
534e3c8d51eSPaul E. McKenney void show_rcu_gp_kthreads(void);
5354babd855SJoel Fernandes (Google) int rcu_get_gp_kthreads_prio(void);
536*e0aff973SPaul E. McKenney void rcu_fwd_progress_check(unsigned long j);
537e3c8d51eSPaul E. McKenney void rcu_force_quiescent_state(void);
538ad7c946bSPaul E. McKenney extern struct workqueue_struct *rcu_gp_wq;
53925f3d7efSPaul E. McKenney extern struct workqueue_struct *rcu_par_gp_wq;
540e3c8d51eSPaul E. McKenney #endif /* #else #ifdef CONFIG_TINY_RCU */
541e3c8d51eSPaul E. McKenney 
54244c65ff2SPaul E. McKenney #ifdef CONFIG_RCU_NOCB_CPU
5433d54f798SPaul E. McKenney bool rcu_is_nocb_cpu(int cpu);
5445ab7ab83SPaul E. McKenney void rcu_bind_current_to_nocb(void);
5453d54f798SPaul E. McKenney #else
5463d54f798SPaul E. McKenney static inline bool rcu_is_nocb_cpu(int cpu) { return false; }
5475ab7ab83SPaul E. McKenney static inline void rcu_bind_current_to_nocb(void) { }
5483d54f798SPaul E. McKenney #endif
5493d54f798SPaul E. McKenney 
5504102adabSPaul E. McKenney #endif /* __LINUX_RCU_H */
551