xref: /OK3568_Linux_fs/kernel/include/linux/rcupdate.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0+ */
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Read-Copy Update mechanism for mutual exclusion
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright IBM Corporation, 2001
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * Author: Dipankar Sarma <dipankar@in.ibm.com>
8*4882a593Smuzhiyun  *
9*4882a593Smuzhiyun  * Based on the original work by Paul McKenney <paulmck@vnet.ibm.com>
10*4882a593Smuzhiyun  * and inputs from Rusty Russell, Andrea Arcangeli and Andi Kleen.
11*4882a593Smuzhiyun  * Papers:
12*4882a593Smuzhiyun  * http://www.rdrop.com/users/paulmck/paper/rclockpdcsproof.pdf
13*4882a593Smuzhiyun  * http://lse.sourceforge.net/locking/rclock_OLS.2001.05.01c.sc.pdf (OLS2001)
14*4882a593Smuzhiyun  *
15*4882a593Smuzhiyun  * For detailed explanation of Read-Copy Update mechanism see -
16*4882a593Smuzhiyun  *		http://lse.sourceforge.net/locking/rcupdate.html
17*4882a593Smuzhiyun  *
18*4882a593Smuzhiyun  */
19*4882a593Smuzhiyun 
20*4882a593Smuzhiyun #ifndef __LINUX_RCUPDATE_H
21*4882a593Smuzhiyun #define __LINUX_RCUPDATE_H
22*4882a593Smuzhiyun 
23*4882a593Smuzhiyun #include <linux/types.h>
24*4882a593Smuzhiyun #include <linux/compiler.h>
25*4882a593Smuzhiyun #include <linux/atomic.h>
26*4882a593Smuzhiyun #include <linux/irqflags.h>
27*4882a593Smuzhiyun #include <linux/preempt.h>
28*4882a593Smuzhiyun #include <linux/bottom_half.h>
29*4882a593Smuzhiyun #include <linux/lockdep.h>
30*4882a593Smuzhiyun #include <asm/processor.h>
31*4882a593Smuzhiyun #include <linux/cpumask.h>
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun #define ULONG_CMP_GE(a, b)	(ULONG_MAX / 2 >= (a) - (b))
34*4882a593Smuzhiyun #define ULONG_CMP_LT(a, b)	(ULONG_MAX / 2 < (a) - (b))
35*4882a593Smuzhiyun #define ulong2long(a)		(*(long *)(&(a)))
36*4882a593Smuzhiyun #define USHORT_CMP_GE(a, b)	(USHRT_MAX / 2 >= (unsigned short)((a) - (b)))
37*4882a593Smuzhiyun #define USHORT_CMP_LT(a, b)	(USHRT_MAX / 2 < (unsigned short)((a) - (b)))
38*4882a593Smuzhiyun 
39*4882a593Smuzhiyun /* Exported common interfaces */
40*4882a593Smuzhiyun void call_rcu(struct rcu_head *head, rcu_callback_t func);
41*4882a593Smuzhiyun void rcu_barrier_tasks(void);
42*4882a593Smuzhiyun void rcu_barrier_tasks_rude(void);
43*4882a593Smuzhiyun void synchronize_rcu(void);
44*4882a593Smuzhiyun 
45*4882a593Smuzhiyun #ifdef CONFIG_PREEMPT_RCU
46*4882a593Smuzhiyun 
47*4882a593Smuzhiyun void __rcu_read_lock(void);
48*4882a593Smuzhiyun void __rcu_read_unlock(void);
49*4882a593Smuzhiyun 
50*4882a593Smuzhiyun /*
51*4882a593Smuzhiyun  * Defined as a macro as it is a very low level header included from
52*4882a593Smuzhiyun  * areas that don't even know about current.  This gives the rcu_read_lock()
53*4882a593Smuzhiyun  * nesting depth, but makes sense only if CONFIG_PREEMPT_RCU -- in other
54*4882a593Smuzhiyun  * types of kernel builds, the rcu_read_lock() nesting depth is unknowable.
55*4882a593Smuzhiyun  */
56*4882a593Smuzhiyun #define rcu_preempt_depth() (current->rcu_read_lock_nesting)
57*4882a593Smuzhiyun 
58*4882a593Smuzhiyun #else /* #ifdef CONFIG_PREEMPT_RCU */
59*4882a593Smuzhiyun 
60*4882a593Smuzhiyun #ifdef CONFIG_TINY_RCU
61*4882a593Smuzhiyun #define rcu_read_unlock_strict() do { } while (0)
62*4882a593Smuzhiyun #else
63*4882a593Smuzhiyun void rcu_read_unlock_strict(void);
64*4882a593Smuzhiyun #endif
65*4882a593Smuzhiyun 
__rcu_read_lock(void)66*4882a593Smuzhiyun static inline void __rcu_read_lock(void)
67*4882a593Smuzhiyun {
68*4882a593Smuzhiyun 	preempt_disable();
69*4882a593Smuzhiyun }
70*4882a593Smuzhiyun 
__rcu_read_unlock(void)71*4882a593Smuzhiyun static inline void __rcu_read_unlock(void)
72*4882a593Smuzhiyun {
73*4882a593Smuzhiyun 	preempt_enable();
74*4882a593Smuzhiyun 	rcu_read_unlock_strict();
75*4882a593Smuzhiyun }
76*4882a593Smuzhiyun 
rcu_preempt_depth(void)77*4882a593Smuzhiyun static inline int rcu_preempt_depth(void)
78*4882a593Smuzhiyun {
79*4882a593Smuzhiyun 	return 0;
80*4882a593Smuzhiyun }
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun #endif /* #else #ifdef CONFIG_PREEMPT_RCU */
83*4882a593Smuzhiyun 
84*4882a593Smuzhiyun /* Internal to kernel */
85*4882a593Smuzhiyun void rcu_init(void);
86*4882a593Smuzhiyun extern int rcu_scheduler_active __read_mostly;
87*4882a593Smuzhiyun void rcu_sched_clock_irq(int user);
88*4882a593Smuzhiyun void rcu_report_dead(unsigned int cpu);
89*4882a593Smuzhiyun void rcutree_migrate_callbacks(int cpu);
90*4882a593Smuzhiyun 
91*4882a593Smuzhiyun #ifdef CONFIG_TASKS_RCU_GENERIC
92*4882a593Smuzhiyun void rcu_init_tasks_generic(void);
93*4882a593Smuzhiyun #else
rcu_init_tasks_generic(void)94*4882a593Smuzhiyun static inline void rcu_init_tasks_generic(void) { }
95*4882a593Smuzhiyun #endif
96*4882a593Smuzhiyun 
97*4882a593Smuzhiyun #ifdef CONFIG_RCU_STALL_COMMON
98*4882a593Smuzhiyun void rcu_sysrq_start(void);
99*4882a593Smuzhiyun void rcu_sysrq_end(void);
100*4882a593Smuzhiyun #else /* #ifdef CONFIG_RCU_STALL_COMMON */
rcu_sysrq_start(void)101*4882a593Smuzhiyun static inline void rcu_sysrq_start(void) { }
rcu_sysrq_end(void)102*4882a593Smuzhiyun static inline void rcu_sysrq_end(void) { }
103*4882a593Smuzhiyun #endif /* #else #ifdef CONFIG_RCU_STALL_COMMON */
104*4882a593Smuzhiyun 
105*4882a593Smuzhiyun #ifdef CONFIG_NO_HZ_FULL
106*4882a593Smuzhiyun void rcu_user_enter(void);
107*4882a593Smuzhiyun void rcu_user_exit(void);
108*4882a593Smuzhiyun #else
rcu_user_enter(void)109*4882a593Smuzhiyun static inline void rcu_user_enter(void) { }
rcu_user_exit(void)110*4882a593Smuzhiyun static inline void rcu_user_exit(void) { }
111*4882a593Smuzhiyun #endif /* CONFIG_NO_HZ_FULL */
112*4882a593Smuzhiyun 
113*4882a593Smuzhiyun #ifdef CONFIG_RCU_NOCB_CPU
114*4882a593Smuzhiyun void rcu_init_nohz(void);
115*4882a593Smuzhiyun void rcu_nocb_flush_deferred_wakeup(void);
116*4882a593Smuzhiyun #else /* #ifdef CONFIG_RCU_NOCB_CPU */
rcu_init_nohz(void)117*4882a593Smuzhiyun static inline void rcu_init_nohz(void) { }
rcu_nocb_flush_deferred_wakeup(void)118*4882a593Smuzhiyun static inline void rcu_nocb_flush_deferred_wakeup(void) { }
119*4882a593Smuzhiyun #endif /* #else #ifdef CONFIG_RCU_NOCB_CPU */
120*4882a593Smuzhiyun 
121*4882a593Smuzhiyun /**
122*4882a593Smuzhiyun  * RCU_NONIDLE - Indicate idle-loop code that needs RCU readers
123*4882a593Smuzhiyun  * @a: Code that RCU needs to pay attention to.
124*4882a593Smuzhiyun  *
125*4882a593Smuzhiyun  * RCU read-side critical sections are forbidden in the inner idle loop,
126*4882a593Smuzhiyun  * that is, between the rcu_idle_enter() and the rcu_idle_exit() -- RCU
127*4882a593Smuzhiyun  * will happily ignore any such read-side critical sections.  However,
128*4882a593Smuzhiyun  * things like powertop need tracepoints in the inner idle loop.
129*4882a593Smuzhiyun  *
130*4882a593Smuzhiyun  * This macro provides the way out:  RCU_NONIDLE(do_something_with_RCU())
131*4882a593Smuzhiyun  * will tell RCU that it needs to pay attention, invoke its argument
132*4882a593Smuzhiyun  * (in this example, calling the do_something_with_RCU() function),
133*4882a593Smuzhiyun  * and then tell RCU to go back to ignoring this CPU.  It is permissible
134*4882a593Smuzhiyun  * to nest RCU_NONIDLE() wrappers, but not indefinitely (but the limit is
135*4882a593Smuzhiyun  * on the order of a million or so, even on 32-bit systems).  It is
136*4882a593Smuzhiyun  * not legal to block within RCU_NONIDLE(), nor is it permissible to
137*4882a593Smuzhiyun  * transfer control either into or out of RCU_NONIDLE()'s statement.
138*4882a593Smuzhiyun  */
139*4882a593Smuzhiyun #define RCU_NONIDLE(a) \
140*4882a593Smuzhiyun 	do { \
141*4882a593Smuzhiyun 		rcu_irq_enter_irqson(); \
142*4882a593Smuzhiyun 		do { a; } while (0); \
143*4882a593Smuzhiyun 		rcu_irq_exit_irqson(); \
144*4882a593Smuzhiyun 	} while (0)
145*4882a593Smuzhiyun 
146*4882a593Smuzhiyun /*
147*4882a593Smuzhiyun  * Note a quasi-voluntary context switch for RCU-tasks's benefit.
148*4882a593Smuzhiyun  * This is a macro rather than an inline function to avoid #include hell.
149*4882a593Smuzhiyun  */
150*4882a593Smuzhiyun #ifdef CONFIG_TASKS_RCU_GENERIC
151*4882a593Smuzhiyun 
152*4882a593Smuzhiyun # ifdef CONFIG_TASKS_RCU
153*4882a593Smuzhiyun # define rcu_tasks_classic_qs(t, preempt)				\
154*4882a593Smuzhiyun 	do {								\
155*4882a593Smuzhiyun 		if (!(preempt) && READ_ONCE((t)->rcu_tasks_holdout))	\
156*4882a593Smuzhiyun 			WRITE_ONCE((t)->rcu_tasks_holdout, false);	\
157*4882a593Smuzhiyun 	} while (0)
158*4882a593Smuzhiyun void call_rcu_tasks(struct rcu_head *head, rcu_callback_t func);
159*4882a593Smuzhiyun void synchronize_rcu_tasks(void);
160*4882a593Smuzhiyun # else
161*4882a593Smuzhiyun # define rcu_tasks_classic_qs(t, preempt) do { } while (0)
162*4882a593Smuzhiyun # define call_rcu_tasks call_rcu
163*4882a593Smuzhiyun # define synchronize_rcu_tasks synchronize_rcu
164*4882a593Smuzhiyun # endif
165*4882a593Smuzhiyun 
166*4882a593Smuzhiyun # ifdef CONFIG_TASKS_TRACE_RCU
167*4882a593Smuzhiyun # define rcu_tasks_trace_qs(t)						\
168*4882a593Smuzhiyun 	do {								\
169*4882a593Smuzhiyun 		if (!likely(READ_ONCE((t)->trc_reader_checked)) &&	\
170*4882a593Smuzhiyun 		    !unlikely(READ_ONCE((t)->trc_reader_nesting))) {	\
171*4882a593Smuzhiyun 			smp_store_release(&(t)->trc_reader_checked, true); \
172*4882a593Smuzhiyun 			smp_mb(); /* Readers partitioned by store. */	\
173*4882a593Smuzhiyun 		}							\
174*4882a593Smuzhiyun 	} while (0)
175*4882a593Smuzhiyun # else
176*4882a593Smuzhiyun # define rcu_tasks_trace_qs(t) do { } while (0)
177*4882a593Smuzhiyun # endif
178*4882a593Smuzhiyun 
179*4882a593Smuzhiyun #define rcu_tasks_qs(t, preempt)					\
180*4882a593Smuzhiyun do {									\
181*4882a593Smuzhiyun 	rcu_tasks_classic_qs((t), (preempt));				\
182*4882a593Smuzhiyun 	rcu_tasks_trace_qs((t));					\
183*4882a593Smuzhiyun } while (0)
184*4882a593Smuzhiyun 
185*4882a593Smuzhiyun # ifdef CONFIG_TASKS_RUDE_RCU
186*4882a593Smuzhiyun void call_rcu_tasks_rude(struct rcu_head *head, rcu_callback_t func);
187*4882a593Smuzhiyun void synchronize_rcu_tasks_rude(void);
188*4882a593Smuzhiyun # endif
189*4882a593Smuzhiyun 
190*4882a593Smuzhiyun #define rcu_note_voluntary_context_switch(t) rcu_tasks_qs(t, false)
191*4882a593Smuzhiyun void exit_tasks_rcu_start(void);
192*4882a593Smuzhiyun void exit_tasks_rcu_finish(void);
193*4882a593Smuzhiyun #else /* #ifdef CONFIG_TASKS_RCU_GENERIC */
194*4882a593Smuzhiyun #define rcu_tasks_qs(t, preempt) do { } while (0)
195*4882a593Smuzhiyun #define rcu_note_voluntary_context_switch(t) do { } while (0)
196*4882a593Smuzhiyun #define call_rcu_tasks call_rcu
197*4882a593Smuzhiyun #define synchronize_rcu_tasks synchronize_rcu
exit_tasks_rcu_start(void)198*4882a593Smuzhiyun static inline void exit_tasks_rcu_start(void) { }
exit_tasks_rcu_finish(void)199*4882a593Smuzhiyun static inline void exit_tasks_rcu_finish(void) { }
200*4882a593Smuzhiyun #endif /* #else #ifdef CONFIG_TASKS_RCU_GENERIC */
201*4882a593Smuzhiyun 
202*4882a593Smuzhiyun /**
203*4882a593Smuzhiyun  * cond_resched_tasks_rcu_qs - Report potential quiescent states to RCU
204*4882a593Smuzhiyun  *
205*4882a593Smuzhiyun  * This macro resembles cond_resched(), except that it is defined to
206*4882a593Smuzhiyun  * report potential quiescent states to RCU-tasks even if the cond_resched()
207*4882a593Smuzhiyun  * machinery were to be shut off, as some advocate for PREEMPTION kernels.
208*4882a593Smuzhiyun  */
209*4882a593Smuzhiyun #define cond_resched_tasks_rcu_qs() \
210*4882a593Smuzhiyun do { \
211*4882a593Smuzhiyun 	rcu_tasks_qs(current, false); \
212*4882a593Smuzhiyun 	cond_resched(); \
213*4882a593Smuzhiyun } while (0)
214*4882a593Smuzhiyun 
215*4882a593Smuzhiyun /*
216*4882a593Smuzhiyun  * Infrastructure to implement the synchronize_() primitives in
217*4882a593Smuzhiyun  * TREE_RCU and rcu_barrier_() primitives in TINY_RCU.
218*4882a593Smuzhiyun  */
219*4882a593Smuzhiyun 
220*4882a593Smuzhiyun #if defined(CONFIG_TREE_RCU)
221*4882a593Smuzhiyun #include <linux/rcutree.h>
222*4882a593Smuzhiyun #elif defined(CONFIG_TINY_RCU)
223*4882a593Smuzhiyun #include <linux/rcutiny.h>
224*4882a593Smuzhiyun #else
225*4882a593Smuzhiyun #error "Unknown RCU implementation specified to kernel configuration"
226*4882a593Smuzhiyun #endif
227*4882a593Smuzhiyun 
228*4882a593Smuzhiyun /*
229*4882a593Smuzhiyun  * The init_rcu_head_on_stack() and destroy_rcu_head_on_stack() calls
230*4882a593Smuzhiyun  * are needed for dynamic initialization and destruction of rcu_head
231*4882a593Smuzhiyun  * on the stack, and init_rcu_head()/destroy_rcu_head() are needed for
232*4882a593Smuzhiyun  * dynamic initialization and destruction of statically allocated rcu_head
233*4882a593Smuzhiyun  * structures.  However, rcu_head structures allocated dynamically in the
234*4882a593Smuzhiyun  * heap don't need any initialization.
235*4882a593Smuzhiyun  */
236*4882a593Smuzhiyun #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD
237*4882a593Smuzhiyun void init_rcu_head(struct rcu_head *head);
238*4882a593Smuzhiyun void destroy_rcu_head(struct rcu_head *head);
239*4882a593Smuzhiyun void init_rcu_head_on_stack(struct rcu_head *head);
240*4882a593Smuzhiyun void destroy_rcu_head_on_stack(struct rcu_head *head);
241*4882a593Smuzhiyun #else /* !CONFIG_DEBUG_OBJECTS_RCU_HEAD */
init_rcu_head(struct rcu_head * head)242*4882a593Smuzhiyun static inline void init_rcu_head(struct rcu_head *head) { }
destroy_rcu_head(struct rcu_head * head)243*4882a593Smuzhiyun static inline void destroy_rcu_head(struct rcu_head *head) { }
init_rcu_head_on_stack(struct rcu_head * head)244*4882a593Smuzhiyun static inline void init_rcu_head_on_stack(struct rcu_head *head) { }
destroy_rcu_head_on_stack(struct rcu_head * head)245*4882a593Smuzhiyun static inline void destroy_rcu_head_on_stack(struct rcu_head *head) { }
246*4882a593Smuzhiyun #endif	/* #else !CONFIG_DEBUG_OBJECTS_RCU_HEAD */
247*4882a593Smuzhiyun 
248*4882a593Smuzhiyun #if defined(CONFIG_HOTPLUG_CPU) && defined(CONFIG_PROVE_RCU)
249*4882a593Smuzhiyun bool rcu_lockdep_current_cpu_online(void);
250*4882a593Smuzhiyun #else /* #if defined(CONFIG_HOTPLUG_CPU) && defined(CONFIG_PROVE_RCU) */
rcu_lockdep_current_cpu_online(void)251*4882a593Smuzhiyun static inline bool rcu_lockdep_current_cpu_online(void) { return true; }
252*4882a593Smuzhiyun #endif /* #else #if defined(CONFIG_HOTPLUG_CPU) && defined(CONFIG_PROVE_RCU) */
253*4882a593Smuzhiyun 
254*4882a593Smuzhiyun #ifdef CONFIG_DEBUG_LOCK_ALLOC
255*4882a593Smuzhiyun 
rcu_lock_acquire(struct lockdep_map * map)256*4882a593Smuzhiyun static inline void rcu_lock_acquire(struct lockdep_map *map)
257*4882a593Smuzhiyun {
258*4882a593Smuzhiyun 	lock_acquire(map, 0, 0, 2, 0, NULL, _THIS_IP_);
259*4882a593Smuzhiyun }
260*4882a593Smuzhiyun 
rcu_lock_release(struct lockdep_map * map)261*4882a593Smuzhiyun static inline void rcu_lock_release(struct lockdep_map *map)
262*4882a593Smuzhiyun {
263*4882a593Smuzhiyun 	lock_release(map, _THIS_IP_);
264*4882a593Smuzhiyun }
265*4882a593Smuzhiyun 
266*4882a593Smuzhiyun extern struct lockdep_map rcu_lock_map;
267*4882a593Smuzhiyun extern struct lockdep_map rcu_bh_lock_map;
268*4882a593Smuzhiyun extern struct lockdep_map rcu_sched_lock_map;
269*4882a593Smuzhiyun extern struct lockdep_map rcu_callback_map;
270*4882a593Smuzhiyun int debug_lockdep_rcu_enabled(void);
271*4882a593Smuzhiyun int rcu_read_lock_held(void);
272*4882a593Smuzhiyun int rcu_read_lock_bh_held(void);
273*4882a593Smuzhiyun int rcu_read_lock_sched_held(void);
274*4882a593Smuzhiyun int rcu_read_lock_any_held(void);
275*4882a593Smuzhiyun 
276*4882a593Smuzhiyun #else /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
277*4882a593Smuzhiyun 
278*4882a593Smuzhiyun # define rcu_lock_acquire(a)		do { } while (0)
279*4882a593Smuzhiyun # define rcu_lock_release(a)		do { } while (0)
280*4882a593Smuzhiyun 
rcu_read_lock_held(void)281*4882a593Smuzhiyun static inline int rcu_read_lock_held(void)
282*4882a593Smuzhiyun {
283*4882a593Smuzhiyun 	return 1;
284*4882a593Smuzhiyun }
285*4882a593Smuzhiyun 
rcu_read_lock_bh_held(void)286*4882a593Smuzhiyun static inline int rcu_read_lock_bh_held(void)
287*4882a593Smuzhiyun {
288*4882a593Smuzhiyun 	return 1;
289*4882a593Smuzhiyun }
290*4882a593Smuzhiyun 
rcu_read_lock_sched_held(void)291*4882a593Smuzhiyun static inline int rcu_read_lock_sched_held(void)
292*4882a593Smuzhiyun {
293*4882a593Smuzhiyun 	return !preemptible();
294*4882a593Smuzhiyun }
295*4882a593Smuzhiyun 
rcu_read_lock_any_held(void)296*4882a593Smuzhiyun static inline int rcu_read_lock_any_held(void)
297*4882a593Smuzhiyun {
298*4882a593Smuzhiyun 	return !preemptible();
299*4882a593Smuzhiyun }
300*4882a593Smuzhiyun 
301*4882a593Smuzhiyun #endif /* #else #ifdef CONFIG_DEBUG_LOCK_ALLOC */
302*4882a593Smuzhiyun 
303*4882a593Smuzhiyun #ifdef CONFIG_PROVE_RCU
304*4882a593Smuzhiyun 
305*4882a593Smuzhiyun /**
306*4882a593Smuzhiyun  * RCU_LOCKDEP_WARN - emit lockdep splat if specified condition is met
307*4882a593Smuzhiyun  * @c: condition to check
308*4882a593Smuzhiyun  * @s: informative message
309*4882a593Smuzhiyun  */
310*4882a593Smuzhiyun #define RCU_LOCKDEP_WARN(c, s)						\
311*4882a593Smuzhiyun 	do {								\
312*4882a593Smuzhiyun 		static bool __section(".data.unlikely") __warned;	\
313*4882a593Smuzhiyun 		if ((c) && debug_lockdep_rcu_enabled() && !__warned) {	\
314*4882a593Smuzhiyun 			__warned = true;				\
315*4882a593Smuzhiyun 			lockdep_rcu_suspicious(__FILE__, __LINE__, s);	\
316*4882a593Smuzhiyun 		}							\
317*4882a593Smuzhiyun 	} while (0)
318*4882a593Smuzhiyun 
319*4882a593Smuzhiyun #if defined(CONFIG_PROVE_RCU) && !defined(CONFIG_PREEMPT_RCU)
rcu_preempt_sleep_check(void)320*4882a593Smuzhiyun static inline void rcu_preempt_sleep_check(void)
321*4882a593Smuzhiyun {
322*4882a593Smuzhiyun 	RCU_LOCKDEP_WARN(lock_is_held(&rcu_lock_map),
323*4882a593Smuzhiyun 			 "Illegal context switch in RCU read-side critical section");
324*4882a593Smuzhiyun }
325*4882a593Smuzhiyun #else /* #ifdef CONFIG_PROVE_RCU */
rcu_preempt_sleep_check(void)326*4882a593Smuzhiyun static inline void rcu_preempt_sleep_check(void) { }
327*4882a593Smuzhiyun #endif /* #else #ifdef CONFIG_PROVE_RCU */
328*4882a593Smuzhiyun 
329*4882a593Smuzhiyun #define rcu_sleep_check()						\
330*4882a593Smuzhiyun 	do {								\
331*4882a593Smuzhiyun 		rcu_preempt_sleep_check();				\
332*4882a593Smuzhiyun 		RCU_LOCKDEP_WARN(lock_is_held(&rcu_bh_lock_map),	\
333*4882a593Smuzhiyun 				 "Illegal context switch in RCU-bh read-side critical section"); \
334*4882a593Smuzhiyun 		RCU_LOCKDEP_WARN(lock_is_held(&rcu_sched_lock_map),	\
335*4882a593Smuzhiyun 				 "Illegal context switch in RCU-sched read-side critical section"); \
336*4882a593Smuzhiyun 	} while (0)
337*4882a593Smuzhiyun 
338*4882a593Smuzhiyun #else /* #ifdef CONFIG_PROVE_RCU */
339*4882a593Smuzhiyun 
340*4882a593Smuzhiyun #define RCU_LOCKDEP_WARN(c, s) do { } while (0)
341*4882a593Smuzhiyun #define rcu_sleep_check() do { } while (0)
342*4882a593Smuzhiyun 
343*4882a593Smuzhiyun #endif /* #else #ifdef CONFIG_PROVE_RCU */
344*4882a593Smuzhiyun 
345*4882a593Smuzhiyun /*
346*4882a593Smuzhiyun  * Helper functions for rcu_dereference_check(), rcu_dereference_protected()
347*4882a593Smuzhiyun  * and rcu_assign_pointer().  Some of these could be folded into their
348*4882a593Smuzhiyun  * callers, but they are left separate in order to ease introduction of
349*4882a593Smuzhiyun  * multiple pointers markings to match different RCU implementations
350*4882a593Smuzhiyun  * (e.g., __srcu), should this make sense in the future.
351*4882a593Smuzhiyun  */
352*4882a593Smuzhiyun 
353*4882a593Smuzhiyun #ifdef __CHECKER__
354*4882a593Smuzhiyun #define rcu_check_sparse(p, space) \
355*4882a593Smuzhiyun 	((void)(((typeof(*p) space *)p) == p))
356*4882a593Smuzhiyun #else /* #ifdef __CHECKER__ */
357*4882a593Smuzhiyun #define rcu_check_sparse(p, space)
358*4882a593Smuzhiyun #endif /* #else #ifdef __CHECKER__ */
359*4882a593Smuzhiyun 
360*4882a593Smuzhiyun #define __rcu_access_pointer(p, space) \
361*4882a593Smuzhiyun ({ \
362*4882a593Smuzhiyun 	typeof(*p) *_________p1 = (typeof(*p) *__force)READ_ONCE(p); \
363*4882a593Smuzhiyun 	rcu_check_sparse(p, space); \
364*4882a593Smuzhiyun 	((typeof(*p) __force __kernel *)(_________p1)); \
365*4882a593Smuzhiyun })
366*4882a593Smuzhiyun #define __rcu_dereference_check(p, c, space) \
367*4882a593Smuzhiyun ({ \
368*4882a593Smuzhiyun 	/* Dependency order vs. p above. */ \
369*4882a593Smuzhiyun 	typeof(*p) *________p1 = (typeof(*p) *__force)READ_ONCE(p); \
370*4882a593Smuzhiyun 	RCU_LOCKDEP_WARN(!(c), "suspicious rcu_dereference_check() usage"); \
371*4882a593Smuzhiyun 	rcu_check_sparse(p, space); \
372*4882a593Smuzhiyun 	((typeof(*p) __force __kernel *)(________p1)); \
373*4882a593Smuzhiyun })
374*4882a593Smuzhiyun #define __rcu_dereference_protected(p, c, space) \
375*4882a593Smuzhiyun ({ \
376*4882a593Smuzhiyun 	RCU_LOCKDEP_WARN(!(c), "suspicious rcu_dereference_protected() usage"); \
377*4882a593Smuzhiyun 	rcu_check_sparse(p, space); \
378*4882a593Smuzhiyun 	((typeof(*p) __force __kernel *)(p)); \
379*4882a593Smuzhiyun })
380*4882a593Smuzhiyun #define rcu_dereference_raw(p) \
381*4882a593Smuzhiyun ({ \
382*4882a593Smuzhiyun 	/* Dependency order vs. p above. */ \
383*4882a593Smuzhiyun 	typeof(p) ________p1 = READ_ONCE(p); \
384*4882a593Smuzhiyun 	((typeof(*p) __force __kernel *)(________p1)); \
385*4882a593Smuzhiyun })
386*4882a593Smuzhiyun 
387*4882a593Smuzhiyun /**
388*4882a593Smuzhiyun  * RCU_INITIALIZER() - statically initialize an RCU-protected global variable
389*4882a593Smuzhiyun  * @v: The value to statically initialize with.
390*4882a593Smuzhiyun  */
391*4882a593Smuzhiyun #define RCU_INITIALIZER(v) (typeof(*(v)) __force __rcu *)(v)
392*4882a593Smuzhiyun 
393*4882a593Smuzhiyun /**
394*4882a593Smuzhiyun  * rcu_assign_pointer() - assign to RCU-protected pointer
395*4882a593Smuzhiyun  * @p: pointer to assign to
396*4882a593Smuzhiyun  * @v: value to assign (publish)
397*4882a593Smuzhiyun  *
398*4882a593Smuzhiyun  * Assigns the specified value to the specified RCU-protected
399*4882a593Smuzhiyun  * pointer, ensuring that any concurrent RCU readers will see
400*4882a593Smuzhiyun  * any prior initialization.
401*4882a593Smuzhiyun  *
402*4882a593Smuzhiyun  * Inserts memory barriers on architectures that require them
403*4882a593Smuzhiyun  * (which is most of them), and also prevents the compiler from
404*4882a593Smuzhiyun  * reordering the code that initializes the structure after the pointer
405*4882a593Smuzhiyun  * assignment.  More importantly, this call documents which pointers
406*4882a593Smuzhiyun  * will be dereferenced by RCU read-side code.
407*4882a593Smuzhiyun  *
408*4882a593Smuzhiyun  * In some special cases, you may use RCU_INIT_POINTER() instead
409*4882a593Smuzhiyun  * of rcu_assign_pointer().  RCU_INIT_POINTER() is a bit faster due
410*4882a593Smuzhiyun  * to the fact that it does not constrain either the CPU or the compiler.
411*4882a593Smuzhiyun  * That said, using RCU_INIT_POINTER() when you should have used
412*4882a593Smuzhiyun  * rcu_assign_pointer() is a very bad thing that results in
413*4882a593Smuzhiyun  * impossible-to-diagnose memory corruption.  So please be careful.
414*4882a593Smuzhiyun  * See the RCU_INIT_POINTER() comment header for details.
415*4882a593Smuzhiyun  *
416*4882a593Smuzhiyun  * Note that rcu_assign_pointer() evaluates each of its arguments only
417*4882a593Smuzhiyun  * once, appearances notwithstanding.  One of the "extra" evaluations
418*4882a593Smuzhiyun  * is in typeof() and the other visible only to sparse (__CHECKER__),
419*4882a593Smuzhiyun  * neither of which actually execute the argument.  As with most cpp
420*4882a593Smuzhiyun  * macros, this execute-arguments-only-once property is important, so
421*4882a593Smuzhiyun  * please be careful when making changes to rcu_assign_pointer() and the
422*4882a593Smuzhiyun  * other macros that it invokes.
423*4882a593Smuzhiyun  */
424*4882a593Smuzhiyun #define rcu_assign_pointer(p, v)					      \
425*4882a593Smuzhiyun do {									      \
426*4882a593Smuzhiyun 	uintptr_t _r_a_p__v = (uintptr_t)(v);				      \
427*4882a593Smuzhiyun 	rcu_check_sparse(p, __rcu);					      \
428*4882a593Smuzhiyun 									      \
429*4882a593Smuzhiyun 	if (__builtin_constant_p(v) && (_r_a_p__v) == (uintptr_t)NULL)	      \
430*4882a593Smuzhiyun 		WRITE_ONCE((p), (typeof(p))(_r_a_p__v));		      \
431*4882a593Smuzhiyun 	else								      \
432*4882a593Smuzhiyun 		smp_store_release(&p, RCU_INITIALIZER((typeof(p))_r_a_p__v)); \
433*4882a593Smuzhiyun } while (0)
434*4882a593Smuzhiyun 
435*4882a593Smuzhiyun /**
436*4882a593Smuzhiyun  * rcu_replace_pointer() - replace an RCU pointer, returning its old value
437*4882a593Smuzhiyun  * @rcu_ptr: RCU pointer, whose old value is returned
438*4882a593Smuzhiyun  * @ptr: regular pointer
439*4882a593Smuzhiyun  * @c: the lockdep conditions under which the dereference will take place
440*4882a593Smuzhiyun  *
441*4882a593Smuzhiyun  * Perform a replacement, where @rcu_ptr is an RCU-annotated
442*4882a593Smuzhiyun  * pointer and @c is the lockdep argument that is passed to the
443*4882a593Smuzhiyun  * rcu_dereference_protected() call used to read that pointer.  The old
444*4882a593Smuzhiyun  * value of @rcu_ptr is returned, and @rcu_ptr is set to @ptr.
445*4882a593Smuzhiyun  */
446*4882a593Smuzhiyun #define rcu_replace_pointer(rcu_ptr, ptr, c)				\
447*4882a593Smuzhiyun ({									\
448*4882a593Smuzhiyun 	typeof(ptr) __tmp = rcu_dereference_protected((rcu_ptr), (c));	\
449*4882a593Smuzhiyun 	rcu_assign_pointer((rcu_ptr), (ptr));				\
450*4882a593Smuzhiyun 	__tmp;								\
451*4882a593Smuzhiyun })
452*4882a593Smuzhiyun 
453*4882a593Smuzhiyun /**
454*4882a593Smuzhiyun  * rcu_access_pointer() - fetch RCU pointer with no dereferencing
455*4882a593Smuzhiyun  * @p: The pointer to read
456*4882a593Smuzhiyun  *
457*4882a593Smuzhiyun  * Return the value of the specified RCU-protected pointer, but omit the
458*4882a593Smuzhiyun  * lockdep checks for being in an RCU read-side critical section.  This is
459*4882a593Smuzhiyun  * useful when the value of this pointer is accessed, but the pointer is
460*4882a593Smuzhiyun  * not dereferenced, for example, when testing an RCU-protected pointer
461*4882a593Smuzhiyun  * against NULL.  Although rcu_access_pointer() may also be used in cases
462*4882a593Smuzhiyun  * where update-side locks prevent the value of the pointer from changing,
463*4882a593Smuzhiyun  * you should instead use rcu_dereference_protected() for this use case.
464*4882a593Smuzhiyun  *
465*4882a593Smuzhiyun  * It is also permissible to use rcu_access_pointer() when read-side
466*4882a593Smuzhiyun  * access to the pointer was removed at least one grace period ago, as
467*4882a593Smuzhiyun  * is the case in the context of the RCU callback that is freeing up
468*4882a593Smuzhiyun  * the data, or after a synchronize_rcu() returns.  This can be useful
469*4882a593Smuzhiyun  * when tearing down multi-linked structures after a grace period
470*4882a593Smuzhiyun  * has elapsed.
471*4882a593Smuzhiyun  */
472*4882a593Smuzhiyun #define rcu_access_pointer(p) __rcu_access_pointer((p), __rcu)
473*4882a593Smuzhiyun 
474*4882a593Smuzhiyun /**
475*4882a593Smuzhiyun  * rcu_dereference_check() - rcu_dereference with debug checking
476*4882a593Smuzhiyun  * @p: The pointer to read, prior to dereferencing
477*4882a593Smuzhiyun  * @c: The conditions under which the dereference will take place
478*4882a593Smuzhiyun  *
479*4882a593Smuzhiyun  * Do an rcu_dereference(), but check that the conditions under which the
480*4882a593Smuzhiyun  * dereference will take place are correct.  Typically the conditions
481*4882a593Smuzhiyun  * indicate the various locking conditions that should be held at that
482*4882a593Smuzhiyun  * point.  The check should return true if the conditions are satisfied.
483*4882a593Smuzhiyun  * An implicit check for being in an RCU read-side critical section
484*4882a593Smuzhiyun  * (rcu_read_lock()) is included.
485*4882a593Smuzhiyun  *
486*4882a593Smuzhiyun  * For example:
487*4882a593Smuzhiyun  *
488*4882a593Smuzhiyun  *	bar = rcu_dereference_check(foo->bar, lockdep_is_held(&foo->lock));
489*4882a593Smuzhiyun  *
490*4882a593Smuzhiyun  * could be used to indicate to lockdep that foo->bar may only be dereferenced
491*4882a593Smuzhiyun  * if either rcu_read_lock() is held, or that the lock required to replace
492*4882a593Smuzhiyun  * the bar struct at foo->bar is held.
493*4882a593Smuzhiyun  *
494*4882a593Smuzhiyun  * Note that the list of conditions may also include indications of when a lock
495*4882a593Smuzhiyun  * need not be held, for example during initialisation or destruction of the
496*4882a593Smuzhiyun  * target struct:
497*4882a593Smuzhiyun  *
498*4882a593Smuzhiyun  *	bar = rcu_dereference_check(foo->bar, lockdep_is_held(&foo->lock) ||
499*4882a593Smuzhiyun  *					      atomic_read(&foo->usage) == 0);
500*4882a593Smuzhiyun  *
501*4882a593Smuzhiyun  * Inserts memory barriers on architectures that require them
502*4882a593Smuzhiyun  * (currently only the Alpha), prevents the compiler from refetching
503*4882a593Smuzhiyun  * (and from merging fetches), and, more importantly, documents exactly
504*4882a593Smuzhiyun  * which pointers are protected by RCU and checks that the pointer is
505*4882a593Smuzhiyun  * annotated as __rcu.
506*4882a593Smuzhiyun  */
507*4882a593Smuzhiyun #define rcu_dereference_check(p, c) \
508*4882a593Smuzhiyun 	__rcu_dereference_check((p), (c) || rcu_read_lock_held(), __rcu)
509*4882a593Smuzhiyun 
510*4882a593Smuzhiyun /**
511*4882a593Smuzhiyun  * rcu_dereference_bh_check() - rcu_dereference_bh with debug checking
512*4882a593Smuzhiyun  * @p: The pointer to read, prior to dereferencing
513*4882a593Smuzhiyun  * @c: The conditions under which the dereference will take place
514*4882a593Smuzhiyun  *
515*4882a593Smuzhiyun  * This is the RCU-bh counterpart to rcu_dereference_check().
516*4882a593Smuzhiyun  */
517*4882a593Smuzhiyun #define rcu_dereference_bh_check(p, c) \
518*4882a593Smuzhiyun 	__rcu_dereference_check((p), (c) || rcu_read_lock_bh_held(), __rcu)
519*4882a593Smuzhiyun 
520*4882a593Smuzhiyun /**
521*4882a593Smuzhiyun  * rcu_dereference_sched_check() - rcu_dereference_sched with debug checking
522*4882a593Smuzhiyun  * @p: The pointer to read, prior to dereferencing
523*4882a593Smuzhiyun  * @c: The conditions under which the dereference will take place
524*4882a593Smuzhiyun  *
525*4882a593Smuzhiyun  * This is the RCU-sched counterpart to rcu_dereference_check().
526*4882a593Smuzhiyun  */
527*4882a593Smuzhiyun #define rcu_dereference_sched_check(p, c) \
528*4882a593Smuzhiyun 	__rcu_dereference_check((p), (c) || rcu_read_lock_sched_held(), \
529*4882a593Smuzhiyun 				__rcu)
530*4882a593Smuzhiyun 
531*4882a593Smuzhiyun /*
532*4882a593Smuzhiyun  * The tracing infrastructure traces RCU (we want that), but unfortunately
533*4882a593Smuzhiyun  * some of the RCU checks causes tracing to lock up the system.
534*4882a593Smuzhiyun  *
535*4882a593Smuzhiyun  * The no-tracing version of rcu_dereference_raw() must not call
536*4882a593Smuzhiyun  * rcu_read_lock_held().
537*4882a593Smuzhiyun  */
538*4882a593Smuzhiyun #define rcu_dereference_raw_check(p) __rcu_dereference_check((p), 1, __rcu)
539*4882a593Smuzhiyun 
540*4882a593Smuzhiyun /**
541*4882a593Smuzhiyun  * rcu_dereference_protected() - fetch RCU pointer when updates prevented
542*4882a593Smuzhiyun  * @p: The pointer to read, prior to dereferencing
543*4882a593Smuzhiyun  * @c: The conditions under which the dereference will take place
544*4882a593Smuzhiyun  *
545*4882a593Smuzhiyun  * Return the value of the specified RCU-protected pointer, but omit
546*4882a593Smuzhiyun  * the READ_ONCE().  This is useful in cases where update-side locks
547*4882a593Smuzhiyun  * prevent the value of the pointer from changing.  Please note that this
548*4882a593Smuzhiyun  * primitive does *not* prevent the compiler from repeating this reference
549*4882a593Smuzhiyun  * or combining it with other references, so it should not be used without
550*4882a593Smuzhiyun  * protection of appropriate locks.
551*4882a593Smuzhiyun  *
552*4882a593Smuzhiyun  * This function is only for update-side use.  Using this function
553*4882a593Smuzhiyun  * when protected only by rcu_read_lock() will result in infrequent
554*4882a593Smuzhiyun  * but very ugly failures.
555*4882a593Smuzhiyun  */
556*4882a593Smuzhiyun #define rcu_dereference_protected(p, c) \
557*4882a593Smuzhiyun 	__rcu_dereference_protected((p), (c), __rcu)
558*4882a593Smuzhiyun 
559*4882a593Smuzhiyun 
560*4882a593Smuzhiyun /**
561*4882a593Smuzhiyun  * rcu_dereference() - fetch RCU-protected pointer for dereferencing
562*4882a593Smuzhiyun  * @p: The pointer to read, prior to dereferencing
563*4882a593Smuzhiyun  *
564*4882a593Smuzhiyun  * This is a simple wrapper around rcu_dereference_check().
565*4882a593Smuzhiyun  */
566*4882a593Smuzhiyun #define rcu_dereference(p) rcu_dereference_check(p, 0)
567*4882a593Smuzhiyun 
568*4882a593Smuzhiyun /**
569*4882a593Smuzhiyun  * rcu_dereference_bh() - fetch an RCU-bh-protected pointer for dereferencing
570*4882a593Smuzhiyun  * @p: The pointer to read, prior to dereferencing
571*4882a593Smuzhiyun  *
572*4882a593Smuzhiyun  * Makes rcu_dereference_check() do the dirty work.
573*4882a593Smuzhiyun  */
574*4882a593Smuzhiyun #define rcu_dereference_bh(p) rcu_dereference_bh_check(p, 0)
575*4882a593Smuzhiyun 
576*4882a593Smuzhiyun /**
577*4882a593Smuzhiyun  * rcu_dereference_sched() - fetch RCU-sched-protected pointer for dereferencing
578*4882a593Smuzhiyun  * @p: The pointer to read, prior to dereferencing
579*4882a593Smuzhiyun  *
580*4882a593Smuzhiyun  * Makes rcu_dereference_check() do the dirty work.
581*4882a593Smuzhiyun  */
582*4882a593Smuzhiyun #define rcu_dereference_sched(p) rcu_dereference_sched_check(p, 0)
583*4882a593Smuzhiyun 
584*4882a593Smuzhiyun /**
585*4882a593Smuzhiyun  * rcu_pointer_handoff() - Hand off a pointer from RCU to other mechanism
586*4882a593Smuzhiyun  * @p: The pointer to hand off
587*4882a593Smuzhiyun  *
588*4882a593Smuzhiyun  * This is simply an identity function, but it documents where a pointer
589*4882a593Smuzhiyun  * is handed off from RCU to some other synchronization mechanism, for
590*4882a593Smuzhiyun  * example, reference counting or locking.  In C11, it would map to
591*4882a593Smuzhiyun  * kill_dependency().  It could be used as follows::
592*4882a593Smuzhiyun  *
593*4882a593Smuzhiyun  *	rcu_read_lock();
594*4882a593Smuzhiyun  *	p = rcu_dereference(gp);
595*4882a593Smuzhiyun  *	long_lived = is_long_lived(p);
596*4882a593Smuzhiyun  *	if (long_lived) {
597*4882a593Smuzhiyun  *		if (!atomic_inc_not_zero(p->refcnt))
598*4882a593Smuzhiyun  *			long_lived = false;
599*4882a593Smuzhiyun  *		else
600*4882a593Smuzhiyun  *			p = rcu_pointer_handoff(p);
601*4882a593Smuzhiyun  *	}
602*4882a593Smuzhiyun  *	rcu_read_unlock();
603*4882a593Smuzhiyun  */
604*4882a593Smuzhiyun #define rcu_pointer_handoff(p) (p)
605*4882a593Smuzhiyun 
606*4882a593Smuzhiyun /**
607*4882a593Smuzhiyun  * rcu_read_lock() - mark the beginning of an RCU read-side critical section
608*4882a593Smuzhiyun  *
609*4882a593Smuzhiyun  * When synchronize_rcu() is invoked on one CPU while other CPUs
610*4882a593Smuzhiyun  * are within RCU read-side critical sections, then the
611*4882a593Smuzhiyun  * synchronize_rcu() is guaranteed to block until after all the other
612*4882a593Smuzhiyun  * CPUs exit their critical sections.  Similarly, if call_rcu() is invoked
613*4882a593Smuzhiyun  * on one CPU while other CPUs are within RCU read-side critical
614*4882a593Smuzhiyun  * sections, invocation of the corresponding RCU callback is deferred
615*4882a593Smuzhiyun  * until after the all the other CPUs exit their critical sections.
616*4882a593Smuzhiyun  *
617*4882a593Smuzhiyun  * Note, however, that RCU callbacks are permitted to run concurrently
618*4882a593Smuzhiyun  * with new RCU read-side critical sections.  One way that this can happen
619*4882a593Smuzhiyun  * is via the following sequence of events: (1) CPU 0 enters an RCU
620*4882a593Smuzhiyun  * read-side critical section, (2) CPU 1 invokes call_rcu() to register
621*4882a593Smuzhiyun  * an RCU callback, (3) CPU 0 exits the RCU read-side critical section,
622*4882a593Smuzhiyun  * (4) CPU 2 enters a RCU read-side critical section, (5) the RCU
623*4882a593Smuzhiyun  * callback is invoked.  This is legal, because the RCU read-side critical
624*4882a593Smuzhiyun  * section that was running concurrently with the call_rcu() (and which
625*4882a593Smuzhiyun  * therefore might be referencing something that the corresponding RCU
626*4882a593Smuzhiyun  * callback would free up) has completed before the corresponding
627*4882a593Smuzhiyun  * RCU callback is invoked.
628*4882a593Smuzhiyun  *
629*4882a593Smuzhiyun  * RCU read-side critical sections may be nested.  Any deferred actions
630*4882a593Smuzhiyun  * will be deferred until the outermost RCU read-side critical section
631*4882a593Smuzhiyun  * completes.
632*4882a593Smuzhiyun  *
633*4882a593Smuzhiyun  * You can avoid reading and understanding the next paragraph by
634*4882a593Smuzhiyun  * following this rule: don't put anything in an rcu_read_lock() RCU
635*4882a593Smuzhiyun  * read-side critical section that would block in a !PREEMPTION kernel.
636*4882a593Smuzhiyun  * But if you want the full story, read on!
637*4882a593Smuzhiyun  *
638*4882a593Smuzhiyun  * In non-preemptible RCU implementations (pure TREE_RCU and TINY_RCU),
639*4882a593Smuzhiyun  * it is illegal to block while in an RCU read-side critical section.
640*4882a593Smuzhiyun  * In preemptible RCU implementations (PREEMPT_RCU) in CONFIG_PREEMPTION
641*4882a593Smuzhiyun  * kernel builds, RCU read-side critical sections may be preempted,
642*4882a593Smuzhiyun  * but explicit blocking is illegal.  Finally, in preemptible RCU
643*4882a593Smuzhiyun  * implementations in real-time (with -rt patchset) kernel builds, RCU
644*4882a593Smuzhiyun  * read-side critical sections may be preempted and they may also block, but
645*4882a593Smuzhiyun  * only when acquiring spinlocks that are subject to priority inheritance.
646*4882a593Smuzhiyun  */
rcu_read_lock(void)647*4882a593Smuzhiyun static __always_inline void rcu_read_lock(void)
648*4882a593Smuzhiyun {
649*4882a593Smuzhiyun 	__rcu_read_lock();
650*4882a593Smuzhiyun 	__acquire(RCU);
651*4882a593Smuzhiyun 	rcu_lock_acquire(&rcu_lock_map);
652*4882a593Smuzhiyun 	RCU_LOCKDEP_WARN(!rcu_is_watching(),
653*4882a593Smuzhiyun 			 "rcu_read_lock() used illegally while idle");
654*4882a593Smuzhiyun }
655*4882a593Smuzhiyun 
656*4882a593Smuzhiyun /*
657*4882a593Smuzhiyun  * So where is rcu_write_lock()?  It does not exist, as there is no
658*4882a593Smuzhiyun  * way for writers to lock out RCU readers.  This is a feature, not
659*4882a593Smuzhiyun  * a bug -- this property is what provides RCU's performance benefits.
660*4882a593Smuzhiyun  * Of course, writers must coordinate with each other.  The normal
661*4882a593Smuzhiyun  * spinlock primitives work well for this, but any other technique may be
662*4882a593Smuzhiyun  * used as well.  RCU does not care how the writers keep out of each
663*4882a593Smuzhiyun  * others' way, as long as they do so.
664*4882a593Smuzhiyun  */
665*4882a593Smuzhiyun 
666*4882a593Smuzhiyun /**
667*4882a593Smuzhiyun  * rcu_read_unlock() - marks the end of an RCU read-side critical section.
668*4882a593Smuzhiyun  *
669*4882a593Smuzhiyun  * In most situations, rcu_read_unlock() is immune from deadlock.
670*4882a593Smuzhiyun  * However, in kernels built with CONFIG_RCU_BOOST, rcu_read_unlock()
671*4882a593Smuzhiyun  * is responsible for deboosting, which it does via rt_mutex_unlock().
672*4882a593Smuzhiyun  * Unfortunately, this function acquires the scheduler's runqueue and
673*4882a593Smuzhiyun  * priority-inheritance spinlocks.  This means that deadlock could result
674*4882a593Smuzhiyun  * if the caller of rcu_read_unlock() already holds one of these locks or
675*4882a593Smuzhiyun  * any lock that is ever acquired while holding them.
676*4882a593Smuzhiyun  *
677*4882a593Smuzhiyun  * That said, RCU readers are never priority boosted unless they were
678*4882a593Smuzhiyun  * preempted.  Therefore, one way to avoid deadlock is to make sure
679*4882a593Smuzhiyun  * that preemption never happens within any RCU read-side critical
680*4882a593Smuzhiyun  * section whose outermost rcu_read_unlock() is called with one of
681*4882a593Smuzhiyun  * rt_mutex_unlock()'s locks held.  Such preemption can be avoided in
682*4882a593Smuzhiyun  * a number of ways, for example, by invoking preempt_disable() before
683*4882a593Smuzhiyun  * critical section's outermost rcu_read_lock().
684*4882a593Smuzhiyun  *
685*4882a593Smuzhiyun  * Given that the set of locks acquired by rt_mutex_unlock() might change
686*4882a593Smuzhiyun  * at any time, a somewhat more future-proofed approach is to make sure
687*4882a593Smuzhiyun  * that that preemption never happens within any RCU read-side critical
688*4882a593Smuzhiyun  * section whose outermost rcu_read_unlock() is called with irqs disabled.
689*4882a593Smuzhiyun  * This approach relies on the fact that rt_mutex_unlock() currently only
690*4882a593Smuzhiyun  * acquires irq-disabled locks.
691*4882a593Smuzhiyun  *
692*4882a593Smuzhiyun  * The second of these two approaches is best in most situations,
693*4882a593Smuzhiyun  * however, the first approach can also be useful, at least to those
694*4882a593Smuzhiyun  * developers willing to keep abreast of the set of locks acquired by
695*4882a593Smuzhiyun  * rt_mutex_unlock().
696*4882a593Smuzhiyun  *
697*4882a593Smuzhiyun  * See rcu_read_lock() for more information.
698*4882a593Smuzhiyun  */
rcu_read_unlock(void)699*4882a593Smuzhiyun static inline void rcu_read_unlock(void)
700*4882a593Smuzhiyun {
701*4882a593Smuzhiyun 	RCU_LOCKDEP_WARN(!rcu_is_watching(),
702*4882a593Smuzhiyun 			 "rcu_read_unlock() used illegally while idle");
703*4882a593Smuzhiyun 	__release(RCU);
704*4882a593Smuzhiyun 	__rcu_read_unlock();
705*4882a593Smuzhiyun 	rcu_lock_release(&rcu_lock_map); /* Keep acq info for rls diags. */
706*4882a593Smuzhiyun }
707*4882a593Smuzhiyun 
708*4882a593Smuzhiyun /**
709*4882a593Smuzhiyun  * rcu_read_lock_bh() - mark the beginning of an RCU-bh critical section
710*4882a593Smuzhiyun  *
711*4882a593Smuzhiyun  * This is equivalent of rcu_read_lock(), but also disables softirqs.
712*4882a593Smuzhiyun  * Note that anything else that disables softirqs can also serve as
713*4882a593Smuzhiyun  * an RCU read-side critical section.
714*4882a593Smuzhiyun  *
715*4882a593Smuzhiyun  * Note that rcu_read_lock_bh() and the matching rcu_read_unlock_bh()
716*4882a593Smuzhiyun  * must occur in the same context, for example, it is illegal to invoke
717*4882a593Smuzhiyun  * rcu_read_unlock_bh() from one task if the matching rcu_read_lock_bh()
718*4882a593Smuzhiyun  * was invoked from some other task.
719*4882a593Smuzhiyun  */
rcu_read_lock_bh(void)720*4882a593Smuzhiyun static inline void rcu_read_lock_bh(void)
721*4882a593Smuzhiyun {
722*4882a593Smuzhiyun 	local_bh_disable();
723*4882a593Smuzhiyun 	__acquire(RCU_BH);
724*4882a593Smuzhiyun 	rcu_lock_acquire(&rcu_bh_lock_map);
725*4882a593Smuzhiyun 	RCU_LOCKDEP_WARN(!rcu_is_watching(),
726*4882a593Smuzhiyun 			 "rcu_read_lock_bh() used illegally while idle");
727*4882a593Smuzhiyun }
728*4882a593Smuzhiyun 
729*4882a593Smuzhiyun /**
730*4882a593Smuzhiyun  * rcu_read_unlock_bh() - marks the end of a softirq-only RCU critical section
731*4882a593Smuzhiyun  *
732*4882a593Smuzhiyun  * See rcu_read_lock_bh() for more information.
733*4882a593Smuzhiyun  */
rcu_read_unlock_bh(void)734*4882a593Smuzhiyun static inline void rcu_read_unlock_bh(void)
735*4882a593Smuzhiyun {
736*4882a593Smuzhiyun 	RCU_LOCKDEP_WARN(!rcu_is_watching(),
737*4882a593Smuzhiyun 			 "rcu_read_unlock_bh() used illegally while idle");
738*4882a593Smuzhiyun 	rcu_lock_release(&rcu_bh_lock_map);
739*4882a593Smuzhiyun 	__release(RCU_BH);
740*4882a593Smuzhiyun 	local_bh_enable();
741*4882a593Smuzhiyun }
742*4882a593Smuzhiyun 
743*4882a593Smuzhiyun /**
744*4882a593Smuzhiyun  * rcu_read_lock_sched() - mark the beginning of a RCU-sched critical section
745*4882a593Smuzhiyun  *
746*4882a593Smuzhiyun  * This is equivalent of rcu_read_lock(), but disables preemption.
747*4882a593Smuzhiyun  * Read-side critical sections can also be introduced by anything else
748*4882a593Smuzhiyun  * that disables preemption, including local_irq_disable() and friends.
749*4882a593Smuzhiyun  *
750*4882a593Smuzhiyun  * Note that rcu_read_lock_sched() and the matching rcu_read_unlock_sched()
751*4882a593Smuzhiyun  * must occur in the same context, for example, it is illegal to invoke
752*4882a593Smuzhiyun  * rcu_read_unlock_sched() from process context if the matching
753*4882a593Smuzhiyun  * rcu_read_lock_sched() was invoked from an NMI handler.
754*4882a593Smuzhiyun  */
rcu_read_lock_sched(void)755*4882a593Smuzhiyun static inline void rcu_read_lock_sched(void)
756*4882a593Smuzhiyun {
757*4882a593Smuzhiyun 	preempt_disable();
758*4882a593Smuzhiyun 	__acquire(RCU_SCHED);
759*4882a593Smuzhiyun 	rcu_lock_acquire(&rcu_sched_lock_map);
760*4882a593Smuzhiyun 	RCU_LOCKDEP_WARN(!rcu_is_watching(),
761*4882a593Smuzhiyun 			 "rcu_read_lock_sched() used illegally while idle");
762*4882a593Smuzhiyun }
763*4882a593Smuzhiyun 
764*4882a593Smuzhiyun /* Used by lockdep and tracing: cannot be traced, cannot call lockdep. */
rcu_read_lock_sched_notrace(void)765*4882a593Smuzhiyun static inline notrace void rcu_read_lock_sched_notrace(void)
766*4882a593Smuzhiyun {
767*4882a593Smuzhiyun 	preempt_disable_notrace();
768*4882a593Smuzhiyun 	__acquire(RCU_SCHED);
769*4882a593Smuzhiyun }
770*4882a593Smuzhiyun 
771*4882a593Smuzhiyun /**
772*4882a593Smuzhiyun  * rcu_read_unlock_sched() - marks the end of a RCU-classic critical section
773*4882a593Smuzhiyun  *
774*4882a593Smuzhiyun  * See rcu_read_lock_sched() for more information.
775*4882a593Smuzhiyun  */
rcu_read_unlock_sched(void)776*4882a593Smuzhiyun static inline void rcu_read_unlock_sched(void)
777*4882a593Smuzhiyun {
778*4882a593Smuzhiyun 	RCU_LOCKDEP_WARN(!rcu_is_watching(),
779*4882a593Smuzhiyun 			 "rcu_read_unlock_sched() used illegally while idle");
780*4882a593Smuzhiyun 	rcu_lock_release(&rcu_sched_lock_map);
781*4882a593Smuzhiyun 	__release(RCU_SCHED);
782*4882a593Smuzhiyun 	preempt_enable();
783*4882a593Smuzhiyun }
784*4882a593Smuzhiyun 
785*4882a593Smuzhiyun /* Used by lockdep and tracing: cannot be traced, cannot call lockdep. */
rcu_read_unlock_sched_notrace(void)786*4882a593Smuzhiyun static inline notrace void rcu_read_unlock_sched_notrace(void)
787*4882a593Smuzhiyun {
788*4882a593Smuzhiyun 	__release(RCU_SCHED);
789*4882a593Smuzhiyun 	preempt_enable_notrace();
790*4882a593Smuzhiyun }
791*4882a593Smuzhiyun 
792*4882a593Smuzhiyun /**
793*4882a593Smuzhiyun  * RCU_INIT_POINTER() - initialize an RCU protected pointer
794*4882a593Smuzhiyun  * @p: The pointer to be initialized.
795*4882a593Smuzhiyun  * @v: The value to initialized the pointer to.
796*4882a593Smuzhiyun  *
797*4882a593Smuzhiyun  * Initialize an RCU-protected pointer in special cases where readers
798*4882a593Smuzhiyun  * do not need ordering constraints on the CPU or the compiler.  These
799*4882a593Smuzhiyun  * special cases are:
800*4882a593Smuzhiyun  *
801*4882a593Smuzhiyun  * 1.	This use of RCU_INIT_POINTER() is NULLing out the pointer *or*
802*4882a593Smuzhiyun  * 2.	The caller has taken whatever steps are required to prevent
803*4882a593Smuzhiyun  *	RCU readers from concurrently accessing this pointer *or*
804*4882a593Smuzhiyun  * 3.	The referenced data structure has already been exposed to
805*4882a593Smuzhiyun  *	readers either at compile time or via rcu_assign_pointer() *and*
806*4882a593Smuzhiyun  *
807*4882a593Smuzhiyun  *	a.	You have not made *any* reader-visible changes to
808*4882a593Smuzhiyun  *		this structure since then *or*
809*4882a593Smuzhiyun  *	b.	It is OK for readers accessing this structure from its
810*4882a593Smuzhiyun  *		new location to see the old state of the structure.  (For
811*4882a593Smuzhiyun  *		example, the changes were to statistical counters or to
812*4882a593Smuzhiyun  *		other state where exact synchronization is not required.)
813*4882a593Smuzhiyun  *
814*4882a593Smuzhiyun  * Failure to follow these rules governing use of RCU_INIT_POINTER() will
815*4882a593Smuzhiyun  * result in impossible-to-diagnose memory corruption.  As in the structures
816*4882a593Smuzhiyun  * will look OK in crash dumps, but any concurrent RCU readers might
817*4882a593Smuzhiyun  * see pre-initialized values of the referenced data structure.  So
818*4882a593Smuzhiyun  * please be very careful how you use RCU_INIT_POINTER()!!!
819*4882a593Smuzhiyun  *
820*4882a593Smuzhiyun  * If you are creating an RCU-protected linked structure that is accessed
821*4882a593Smuzhiyun  * by a single external-to-structure RCU-protected pointer, then you may
822*4882a593Smuzhiyun  * use RCU_INIT_POINTER() to initialize the internal RCU-protected
823*4882a593Smuzhiyun  * pointers, but you must use rcu_assign_pointer() to initialize the
824*4882a593Smuzhiyun  * external-to-structure pointer *after* you have completely initialized
825*4882a593Smuzhiyun  * the reader-accessible portions of the linked structure.
826*4882a593Smuzhiyun  *
827*4882a593Smuzhiyun  * Note that unlike rcu_assign_pointer(), RCU_INIT_POINTER() provides no
828*4882a593Smuzhiyun  * ordering guarantees for either the CPU or the compiler.
829*4882a593Smuzhiyun  */
830*4882a593Smuzhiyun #define RCU_INIT_POINTER(p, v) \
831*4882a593Smuzhiyun 	do { \
832*4882a593Smuzhiyun 		rcu_check_sparse(p, __rcu); \
833*4882a593Smuzhiyun 		WRITE_ONCE(p, RCU_INITIALIZER(v)); \
834*4882a593Smuzhiyun 	} while (0)
835*4882a593Smuzhiyun 
836*4882a593Smuzhiyun /**
837*4882a593Smuzhiyun  * RCU_POINTER_INITIALIZER() - statically initialize an RCU protected pointer
838*4882a593Smuzhiyun  * @p: The pointer to be initialized.
839*4882a593Smuzhiyun  * @v: The value to initialized the pointer to.
840*4882a593Smuzhiyun  *
841*4882a593Smuzhiyun  * GCC-style initialization for an RCU-protected pointer in a structure field.
842*4882a593Smuzhiyun  */
843*4882a593Smuzhiyun #define RCU_POINTER_INITIALIZER(p, v) \
844*4882a593Smuzhiyun 		.p = RCU_INITIALIZER(v)
845*4882a593Smuzhiyun 
846*4882a593Smuzhiyun /*
847*4882a593Smuzhiyun  * Does the specified offset indicate that the corresponding rcu_head
848*4882a593Smuzhiyun  * structure can be handled by kvfree_rcu()?
849*4882a593Smuzhiyun  */
850*4882a593Smuzhiyun #define __is_kvfree_rcu_offset(offset) ((offset) < 4096)
851*4882a593Smuzhiyun 
852*4882a593Smuzhiyun /*
853*4882a593Smuzhiyun  * Helper macro for kfree_rcu() to prevent argument-expansion eyestrain.
854*4882a593Smuzhiyun  */
855*4882a593Smuzhiyun #define __kvfree_rcu(head, offset) \
856*4882a593Smuzhiyun 	do { \
857*4882a593Smuzhiyun 		BUILD_BUG_ON(!__is_kvfree_rcu_offset(offset)); \
858*4882a593Smuzhiyun 		kvfree_call_rcu(head, (rcu_callback_t)(unsigned long)(offset)); \
859*4882a593Smuzhiyun 	} while (0)
860*4882a593Smuzhiyun 
861*4882a593Smuzhiyun /**
862*4882a593Smuzhiyun  * kfree_rcu() - kfree an object after a grace period.
863*4882a593Smuzhiyun  * @ptr:	pointer to kfree
864*4882a593Smuzhiyun  * @rhf:	the name of the struct rcu_head within the type of @ptr.
865*4882a593Smuzhiyun  *
866*4882a593Smuzhiyun  * Many rcu callbacks functions just call kfree() on the base structure.
867*4882a593Smuzhiyun  * These functions are trivial, but their size adds up, and furthermore
868*4882a593Smuzhiyun  * when they are used in a kernel module, that module must invoke the
869*4882a593Smuzhiyun  * high-latency rcu_barrier() function at module-unload time.
870*4882a593Smuzhiyun  *
871*4882a593Smuzhiyun  * The kfree_rcu() function handles this issue.  Rather than encoding a
872*4882a593Smuzhiyun  * function address in the embedded rcu_head structure, kfree_rcu() instead
873*4882a593Smuzhiyun  * encodes the offset of the rcu_head structure within the base structure.
874*4882a593Smuzhiyun  * Because the functions are not allowed in the low-order 4096 bytes of
875*4882a593Smuzhiyun  * kernel virtual memory, offsets up to 4095 bytes can be accommodated.
876*4882a593Smuzhiyun  * If the offset is larger than 4095 bytes, a compile-time error will
877*4882a593Smuzhiyun  * be generated in __kvfree_rcu(). If this error is triggered, you can
878*4882a593Smuzhiyun  * either fall back to use of call_rcu() or rearrange the structure to
879*4882a593Smuzhiyun  * position the rcu_head structure into the first 4096 bytes.
880*4882a593Smuzhiyun  *
881*4882a593Smuzhiyun  * Note that the allowable offset might decrease in the future, for example,
882*4882a593Smuzhiyun  * to allow something like kmem_cache_free_rcu().
883*4882a593Smuzhiyun  *
884*4882a593Smuzhiyun  * The BUILD_BUG_ON check must not involve any function calls, hence the
885*4882a593Smuzhiyun  * checks are done in macros here.
886*4882a593Smuzhiyun  */
887*4882a593Smuzhiyun #define kfree_rcu(ptr, rhf)						\
888*4882a593Smuzhiyun do {									\
889*4882a593Smuzhiyun 	typeof (ptr) ___p = (ptr);					\
890*4882a593Smuzhiyun 									\
891*4882a593Smuzhiyun 	if (___p)							\
892*4882a593Smuzhiyun 		__kvfree_rcu(&((___p)->rhf), offsetof(typeof(*(ptr)), rhf)); \
893*4882a593Smuzhiyun } while (0)
894*4882a593Smuzhiyun 
895*4882a593Smuzhiyun /**
896*4882a593Smuzhiyun  * kvfree_rcu() - kvfree an object after a grace period.
897*4882a593Smuzhiyun  *
898*4882a593Smuzhiyun  * This macro consists of one or two arguments and it is
899*4882a593Smuzhiyun  * based on whether an object is head-less or not. If it
900*4882a593Smuzhiyun  * has a head then a semantic stays the same as it used
901*4882a593Smuzhiyun  * to be before:
902*4882a593Smuzhiyun  *
903*4882a593Smuzhiyun  *     kvfree_rcu(ptr, rhf);
904*4882a593Smuzhiyun  *
905*4882a593Smuzhiyun  * where @ptr is a pointer to kvfree(), @rhf is the name
906*4882a593Smuzhiyun  * of the rcu_head structure within the type of @ptr.
907*4882a593Smuzhiyun  *
908*4882a593Smuzhiyun  * When it comes to head-less variant, only one argument
909*4882a593Smuzhiyun  * is passed and that is just a pointer which has to be
910*4882a593Smuzhiyun  * freed after a grace period. Therefore the semantic is
911*4882a593Smuzhiyun  *
912*4882a593Smuzhiyun  *     kvfree_rcu(ptr);
913*4882a593Smuzhiyun  *
914*4882a593Smuzhiyun  * where @ptr is a pointer to kvfree().
915*4882a593Smuzhiyun  *
916*4882a593Smuzhiyun  * Please note, head-less way of freeing is permitted to
917*4882a593Smuzhiyun  * use from a context that has to follow might_sleep()
918*4882a593Smuzhiyun  * annotation. Otherwise, please switch and embed the
919*4882a593Smuzhiyun  * rcu_head structure within the type of @ptr.
920*4882a593Smuzhiyun  */
921*4882a593Smuzhiyun #define kvfree_rcu(...) KVFREE_GET_MACRO(__VA_ARGS__,		\
922*4882a593Smuzhiyun 	kvfree_rcu_arg_2, kvfree_rcu_arg_1)(__VA_ARGS__)
923*4882a593Smuzhiyun 
924*4882a593Smuzhiyun #define KVFREE_GET_MACRO(_1, _2, NAME, ...) NAME
925*4882a593Smuzhiyun #define kvfree_rcu_arg_2(ptr, rhf) kfree_rcu(ptr, rhf)
926*4882a593Smuzhiyun #define kvfree_rcu_arg_1(ptr)					\
927*4882a593Smuzhiyun do {								\
928*4882a593Smuzhiyun 	typeof(ptr) ___p = (ptr);				\
929*4882a593Smuzhiyun 								\
930*4882a593Smuzhiyun 	if (___p)						\
931*4882a593Smuzhiyun 		kvfree_call_rcu(NULL, (rcu_callback_t) (___p));	\
932*4882a593Smuzhiyun } while (0)
933*4882a593Smuzhiyun 
934*4882a593Smuzhiyun /*
935*4882a593Smuzhiyun  * Place this after a lock-acquisition primitive to guarantee that
936*4882a593Smuzhiyun  * an UNLOCK+LOCK pair acts as a full barrier.  This guarantee applies
937*4882a593Smuzhiyun  * if the UNLOCK and LOCK are executed by the same CPU or if the
938*4882a593Smuzhiyun  * UNLOCK and LOCK operate on the same lock variable.
939*4882a593Smuzhiyun  */
940*4882a593Smuzhiyun #ifdef CONFIG_ARCH_WEAK_RELEASE_ACQUIRE
941*4882a593Smuzhiyun #define smp_mb__after_unlock_lock()	smp_mb()  /* Full ordering for lock. */
942*4882a593Smuzhiyun #else /* #ifdef CONFIG_ARCH_WEAK_RELEASE_ACQUIRE */
943*4882a593Smuzhiyun #define smp_mb__after_unlock_lock()	do { } while (0)
944*4882a593Smuzhiyun #endif /* #else #ifdef CONFIG_ARCH_WEAK_RELEASE_ACQUIRE */
945*4882a593Smuzhiyun 
946*4882a593Smuzhiyun 
947*4882a593Smuzhiyun /* Has the specified rcu_head structure been handed to call_rcu()? */
948*4882a593Smuzhiyun 
949*4882a593Smuzhiyun /**
950*4882a593Smuzhiyun  * rcu_head_init - Initialize rcu_head for rcu_head_after_call_rcu()
951*4882a593Smuzhiyun  * @rhp: The rcu_head structure to initialize.
952*4882a593Smuzhiyun  *
953*4882a593Smuzhiyun  * If you intend to invoke rcu_head_after_call_rcu() to test whether a
954*4882a593Smuzhiyun  * given rcu_head structure has already been passed to call_rcu(), then
955*4882a593Smuzhiyun  * you must also invoke this rcu_head_init() function on it just after
956*4882a593Smuzhiyun  * allocating that structure.  Calls to this function must not race with
957*4882a593Smuzhiyun  * calls to call_rcu(), rcu_head_after_call_rcu(), or callback invocation.
958*4882a593Smuzhiyun  */
rcu_head_init(struct rcu_head * rhp)959*4882a593Smuzhiyun static inline void rcu_head_init(struct rcu_head *rhp)
960*4882a593Smuzhiyun {
961*4882a593Smuzhiyun 	rhp->func = (rcu_callback_t)~0L;
962*4882a593Smuzhiyun }
963*4882a593Smuzhiyun 
964*4882a593Smuzhiyun /**
965*4882a593Smuzhiyun  * rcu_head_after_call_rcu() - Has this rcu_head been passed to call_rcu()?
966*4882a593Smuzhiyun  * @rhp: The rcu_head structure to test.
967*4882a593Smuzhiyun  * @f: The function passed to call_rcu() along with @rhp.
968*4882a593Smuzhiyun  *
969*4882a593Smuzhiyun  * Returns @true if the @rhp has been passed to call_rcu() with @func,
970*4882a593Smuzhiyun  * and @false otherwise.  Emits a warning in any other case, including
971*4882a593Smuzhiyun  * the case where @rhp has already been invoked after a grace period.
972*4882a593Smuzhiyun  * Calls to this function must not race with callback invocation.  One way
973*4882a593Smuzhiyun  * to avoid such races is to enclose the call to rcu_head_after_call_rcu()
974*4882a593Smuzhiyun  * in an RCU read-side critical section that includes a read-side fetch
975*4882a593Smuzhiyun  * of the pointer to the structure containing @rhp.
976*4882a593Smuzhiyun  */
977*4882a593Smuzhiyun static inline bool
rcu_head_after_call_rcu(struct rcu_head * rhp,rcu_callback_t f)978*4882a593Smuzhiyun rcu_head_after_call_rcu(struct rcu_head *rhp, rcu_callback_t f)
979*4882a593Smuzhiyun {
980*4882a593Smuzhiyun 	rcu_callback_t func = READ_ONCE(rhp->func);
981*4882a593Smuzhiyun 
982*4882a593Smuzhiyun 	if (func == f)
983*4882a593Smuzhiyun 		return true;
984*4882a593Smuzhiyun 	WARN_ON_ONCE(func != (rcu_callback_t)~0L);
985*4882a593Smuzhiyun 	return false;
986*4882a593Smuzhiyun }
987*4882a593Smuzhiyun 
988*4882a593Smuzhiyun /* kernel/ksysfs.c definitions */
989*4882a593Smuzhiyun extern int rcu_expedited;
990*4882a593Smuzhiyun extern int rcu_normal;
991*4882a593Smuzhiyun 
992*4882a593Smuzhiyun #endif /* __LINUX_RCUPDATE_H */
993