xref: /linux/include/linux/rcupdate.h (revision 28483203f7d7fe4f123ed08266c381fac96b0701)
1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3  * Read-Copy Update mechanism for mutual exclusion
4  *
5  * Copyright IBM Corporation, 2001
6  *
7  * Author: Dipankar Sarma <dipankar@in.ibm.com>
8  *
9  * Based on the original work by Paul McKenney <paulmck@vnet.ibm.com>
10  * and inputs from Rusty Russell, Andrea Arcangeli and Andi Kleen.
11  * Papers:
12  * http://www.rdrop.com/users/paulmck/paper/rclockpdcsproof.pdf
13  * http://lse.sourceforge.net/locking/rclock_OLS.2001.05.01c.sc.pdf (OLS2001)
14  *
15  * For detailed explanation of Read-Copy Update mechanism see -
16  *		http://lse.sourceforge.net/locking/rcupdate.html
17  *
18  */
19 
20 #ifndef __LINUX_RCUPDATE_H
21 #define __LINUX_RCUPDATE_H
22 
23 #include <linux/types.h>
24 #include <linux/compiler.h>
25 #include <linux/atomic.h>
26 #include <linux/irqflags.h>
27 #include <linux/sched.h>
28 #include <linux/bottom_half.h>
29 #include <linux/lockdep.h>
30 #include <linux/cleanup.h>
31 #include <asm/processor.h>
32 #include <linux/context_tracking_irq.h>
33 
34 token_context_lock(RCU, __reentrant_ctx_lock);
35 token_context_lock_instance(RCU, RCU_SCHED);
36 token_context_lock_instance(RCU, RCU_BH);
37 
38 /*
39  * A convenience macro that can be used for RCU-protected globals or struct
40  * members; adds type qualifier __rcu, and also enforces __guarded_by(RCU).
41  */
42 #define __rcu_guarded __rcu __guarded_by(RCU)
43 
44 #define ULONG_CMP_GE(a, b)	(ULONG_MAX / 2 >= (a) - (b))
45 #define ULONG_CMP_LT(a, b)	(ULONG_MAX / 2 < (a) - (b))
46 
47 #define RCU_SEQ_CTR_SHIFT    2
48 #define RCU_SEQ_STATE_MASK   ((1 << RCU_SEQ_CTR_SHIFT) - 1)
49 
50 /* Exported common interfaces */
51 void call_rcu(struct rcu_head *head, rcu_callback_t func);
52 void rcu_barrier_tasks(void);
53 void synchronize_rcu(void);
54 
55 struct rcu_gp_oldstate;
56 unsigned long get_completed_synchronize_rcu(void);
57 void get_completed_synchronize_rcu_full(struct rcu_gp_oldstate *rgosp);
58 
59 // Maximum number of unsigned long values corresponding to
60 // not-yet-completed RCU grace periods.
61 #define NUM_ACTIVE_RCU_POLL_OLDSTATE 2
62 
63 /**
64  * same_state_synchronize_rcu - Are two old-state values identical?
65  * @oldstate1: First old-state value.
66  * @oldstate2: Second old-state value.
67  *
68  * The two old-state values must have been obtained from either
69  * get_state_synchronize_rcu(), start_poll_synchronize_rcu(), or
70  * get_completed_synchronize_rcu().  Returns @true if the two values are
71  * identical and @false otherwise.  This allows structures whose lifetimes
72  * are tracked by old-state values to push these values to a list header,
73  * allowing those structures to be slightly smaller.
74  */
same_state_synchronize_rcu(unsigned long oldstate1,unsigned long oldstate2)75 static inline bool same_state_synchronize_rcu(unsigned long oldstate1, unsigned long oldstate2)
76 {
77 	return oldstate1 == oldstate2;
78 }
79 
80 #ifdef CONFIG_PREEMPT_RCU
81 
82 void __rcu_read_lock(void);
83 void __rcu_read_unlock(void);
84 
85 /*
86  * Defined as a macro as it is a very low level header included from
87  * areas that don't even know about current.  This gives the rcu_read_lock()
88  * nesting depth, but makes sense only if CONFIG_PREEMPT_RCU -- in other
89  * types of kernel builds, the rcu_read_lock() nesting depth is unknowable.
90  */
91 #define rcu_preempt_depth() READ_ONCE(current->rcu_read_lock_nesting)
92 
93 #else /* #ifdef CONFIG_PREEMPT_RCU */
94 
95 #ifdef CONFIG_TINY_RCU
96 #define rcu_read_unlock_strict() do { } while (0)
97 #else
98 void rcu_read_unlock_strict(void);
99 #endif
100 
__rcu_read_lock(void)101 static inline void __rcu_read_lock(void)
102 {
103 	preempt_disable();
104 }
105 
__rcu_read_unlock(void)106 static inline void __rcu_read_unlock(void)
107 {
108 	if (IS_ENABLED(CONFIG_RCU_STRICT_GRACE_PERIOD))
109 		rcu_read_unlock_strict();
110 	preempt_enable();
111 }
112 
rcu_preempt_depth(void)113 static inline int rcu_preempt_depth(void)
114 {
115 	return 0;
116 }
117 
118 #endif /* #else #ifdef CONFIG_PREEMPT_RCU */
119 
120 #ifdef CONFIG_RCU_LAZY
121 void call_rcu_hurry(struct rcu_head *head, rcu_callback_t func);
122 #else
call_rcu_hurry(struct rcu_head * head,rcu_callback_t func)123 static inline void call_rcu_hurry(struct rcu_head *head, rcu_callback_t func)
124 {
125 	call_rcu(head, func);
126 }
127 #endif
128 
129 /* Internal to kernel */
130 void rcu_init(void);
131 extern int rcu_scheduler_active;
132 void rcu_sched_clock_irq(int user);
133 
134 #ifdef CONFIG_RCU_STALL_COMMON
135 void rcu_sysrq_start(void);
136 void rcu_sysrq_end(void);
137 #else /* #ifdef CONFIG_RCU_STALL_COMMON */
rcu_sysrq_start(void)138 static inline void rcu_sysrq_start(void) { }
rcu_sysrq_end(void)139 static inline void rcu_sysrq_end(void) { }
140 #endif /* #else #ifdef CONFIG_RCU_STALL_COMMON */
141 
142 #if defined(CONFIG_NO_HZ_FULL) && (!defined(CONFIG_GENERIC_ENTRY) || !defined(CONFIG_VIRT_XFER_TO_GUEST_WORK))
143 void rcu_irq_work_resched(void);
144 #else
rcu_irq_work_resched(void)145 static __always_inline void rcu_irq_work_resched(void) { }
146 #endif
147 
148 #ifdef CONFIG_RCU_NOCB_CPU
149 void rcu_init_nohz(void);
150 int rcu_nocb_cpu_offload(int cpu);
151 int rcu_nocb_cpu_deoffload(int cpu);
152 void rcu_nocb_flush_deferred_wakeup(void);
153 
154 #define RCU_NOCB_LOCKDEP_WARN(c, s) RCU_LOCKDEP_WARN(c, s)
155 
156 #else /* #ifdef CONFIG_RCU_NOCB_CPU */
157 
rcu_init_nohz(void)158 static inline void rcu_init_nohz(void) { }
rcu_nocb_cpu_offload(int cpu)159 static inline int rcu_nocb_cpu_offload(int cpu) { return -EINVAL; }
rcu_nocb_cpu_deoffload(int cpu)160 static inline int rcu_nocb_cpu_deoffload(int cpu) { return 0; }
rcu_nocb_flush_deferred_wakeup(void)161 static inline void rcu_nocb_flush_deferred_wakeup(void) { }
162 
163 #define RCU_NOCB_LOCKDEP_WARN(c, s)
164 
165 #endif /* #else #ifdef CONFIG_RCU_NOCB_CPU */
166 
167 /*
168  * Note a quasi-voluntary context switch for RCU-tasks's benefit.
169  * This is a macro rather than an inline function to avoid #include hell.
170  */
171 #ifdef CONFIG_TASKS_RCU_GENERIC
172 
173 # ifdef CONFIG_TASKS_RCU
174 # define rcu_tasks_classic_qs(t, preempt)				\
175 	do {								\
176 		if (!(preempt) && READ_ONCE((t)->rcu_tasks_holdout))	\
177 			WRITE_ONCE((t)->rcu_tasks_holdout, false);	\
178 	} while (0)
179 void call_rcu_tasks(struct rcu_head *head, rcu_callback_t func);
180 void synchronize_rcu_tasks(void);
181 void rcu_tasks_torture_stats_print(char *tt, char *tf);
182 # else
183 # define rcu_tasks_classic_qs(t, preempt) do { } while (0)
184 # define call_rcu_tasks call_rcu
185 # define synchronize_rcu_tasks synchronize_rcu
186 # endif
187 
188 #define rcu_tasks_qs(t, preempt) rcu_tasks_classic_qs((t), (preempt))
189 
190 # ifdef CONFIG_TASKS_RUDE_RCU
191 void synchronize_rcu_tasks_rude(void);
192 void rcu_tasks_rude_torture_stats_print(char *tt, char *tf);
193 # endif
194 
195 #define rcu_note_voluntary_context_switch(t) rcu_tasks_qs(t, false)
196 void exit_tasks_rcu_start(void);
197 void exit_tasks_rcu_finish(void);
198 #else /* #ifdef CONFIG_TASKS_RCU_GENERIC */
199 #define rcu_tasks_classic_qs(t, preempt) do { } while (0)
200 #define rcu_tasks_qs(t, preempt) do { } while (0)
201 #define rcu_note_voluntary_context_switch(t) do { } while (0)
202 #define call_rcu_tasks call_rcu
203 #define synchronize_rcu_tasks synchronize_rcu
exit_tasks_rcu_start(void)204 static inline void exit_tasks_rcu_start(void) { }
exit_tasks_rcu_finish(void)205 static inline void exit_tasks_rcu_finish(void) { }
206 #endif /* #else #ifdef CONFIG_TASKS_RCU_GENERIC */
207 
208 /**
209  * rcu_trace_implies_rcu_gp - does an RCU Tasks Trace grace period imply an RCU grace period?
210  *
211  * Now that RCU Tasks Trace is implemented in terms of SRCU-fast, a
212  * call to synchronize_rcu_tasks_trace() is guaranteed to imply at least
213  * one call to synchronize_rcu().
214  */
rcu_trace_implies_rcu_gp(void)215 static inline bool rcu_trace_implies_rcu_gp(void) { return true; }
216 
217 /**
218  * cond_resched_tasks_rcu_qs - Report potential quiescent states to RCU
219  *
220  * This macro resembles cond_resched(), except that it is defined to
221  * report potential quiescent states to RCU-tasks even if the cond_resched()
222  * machinery were to be shut off, as some advocate for PREEMPTION kernels.
223  */
224 #define cond_resched_tasks_rcu_qs() \
225 do { \
226 	rcu_tasks_qs(current, false); \
227 	cond_resched(); \
228 } while (0)
229 
230 /**
231  * rcu_softirq_qs_periodic - Report RCU and RCU-Tasks quiescent states
232  * @old_ts: jiffies at start of processing.
233  *
234  * This helper is for long-running softirq handlers, such as NAPI threads in
235  * networking. The caller should initialize the variable passed in as @old_ts
236  * at the beginning of the softirq handler. When invoked frequently, this macro
237  * will invoke rcu_softirq_qs() every 100 milliseconds thereafter, which will
238  * provide both RCU and RCU-Tasks quiescent states. Note that this macro
239  * modifies its old_ts argument.
240  *
241  * Because regions of code that have disabled softirq act as RCU read-side
242  * critical sections, this macro should be invoked with softirq (and
243  * preemption) enabled.
244  *
245  * The macro is not needed when CONFIG_PREEMPT_RT is defined. RT kernels would
246  * have more chance to invoke schedule() calls and provide necessary quiescent
247  * states. As a contrast, calling cond_resched() only won't achieve the same
248  * effect because cond_resched() does not provide RCU-Tasks quiescent states.
249  */
250 #define rcu_softirq_qs_periodic(old_ts) \
251 do { \
252 	if (!IS_ENABLED(CONFIG_PREEMPT_RT) && \
253 	    time_after(jiffies, (old_ts) + HZ / 10)) { \
254 		preempt_disable(); \
255 		rcu_softirq_qs(); \
256 		preempt_enable(); \
257 		(old_ts) = jiffies; \
258 	} \
259 } while (0)
260 
261 /*
262  * Infrastructure to implement the synchronize_() primitives in
263  * TREE_RCU and rcu_barrier_() primitives in TINY_RCU.
264  */
265 
266 #if defined(CONFIG_TREE_RCU)
267 #include <linux/rcutree.h>
268 #elif defined(CONFIG_TINY_RCU)
269 #include <linux/rcutiny.h>
270 #else
271 #error "Unknown RCU implementation specified to kernel configuration"
272 #endif
273 
274 /*
275  * The init_rcu_head_on_stack() and destroy_rcu_head_on_stack() calls
276  * are needed for dynamic initialization and destruction of rcu_head
277  * on the stack, and init_rcu_head()/destroy_rcu_head() are needed for
278  * dynamic initialization and destruction of statically allocated rcu_head
279  * structures.  However, rcu_head structures allocated dynamically in the
280  * heap don't need any initialization.
281  */
282 #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD
283 void init_rcu_head(struct rcu_head *head);
284 void destroy_rcu_head(struct rcu_head *head);
285 void init_rcu_head_on_stack(struct rcu_head *head);
286 void destroy_rcu_head_on_stack(struct rcu_head *head);
287 #else /* !CONFIG_DEBUG_OBJECTS_RCU_HEAD */
init_rcu_head(struct rcu_head * head)288 static inline void init_rcu_head(struct rcu_head *head) { }
destroy_rcu_head(struct rcu_head * head)289 static inline void destroy_rcu_head(struct rcu_head *head) { }
init_rcu_head_on_stack(struct rcu_head * head)290 static inline void init_rcu_head_on_stack(struct rcu_head *head) { }
destroy_rcu_head_on_stack(struct rcu_head * head)291 static inline void destroy_rcu_head_on_stack(struct rcu_head *head) { }
292 #endif	/* #else !CONFIG_DEBUG_OBJECTS_RCU_HEAD */
293 
294 #if defined(CONFIG_HOTPLUG_CPU) && defined(CONFIG_PROVE_RCU)
295 bool rcu_lockdep_current_cpu_online(void);
296 #else /* #if defined(CONFIG_HOTPLUG_CPU) && defined(CONFIG_PROVE_RCU) */
rcu_lockdep_current_cpu_online(void)297 static inline bool rcu_lockdep_current_cpu_online(void) { return true; }
298 #endif /* #else #if defined(CONFIG_HOTPLUG_CPU) && defined(CONFIG_PROVE_RCU) */
299 
300 extern struct lockdep_map rcu_lock_map;
301 extern struct lockdep_map rcu_bh_lock_map;
302 extern struct lockdep_map rcu_sched_lock_map;
303 extern struct lockdep_map rcu_callback_map;
304 
305 #ifdef CONFIG_DEBUG_LOCK_ALLOC
306 
rcu_lock_acquire(struct lockdep_map * map)307 static inline void rcu_lock_acquire(struct lockdep_map *map)
308 {
309 	lock_acquire(map, 0, 0, 2, 0, NULL, _THIS_IP_);
310 }
311 
rcu_try_lock_acquire(struct lockdep_map * map)312 static inline void rcu_try_lock_acquire(struct lockdep_map *map)
313 {
314 	lock_acquire(map, 0, 1, 2, 0, NULL, _THIS_IP_);
315 }
316 
rcu_lock_release(struct lockdep_map * map)317 static inline void rcu_lock_release(struct lockdep_map *map)
318 {
319 	lock_release(map, _THIS_IP_);
320 }
321 
322 int debug_lockdep_rcu_enabled(void);
323 int rcu_read_lock_held(void);
324 int rcu_read_lock_bh_held(void);
325 int rcu_read_lock_sched_held(void);
326 int rcu_read_lock_any_held(void);
327 
328 #else /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
329 
330 # define rcu_lock_acquire(a)		do { } while (0)
331 # define rcu_try_lock_acquire(a)	do { } while (0)
332 # define rcu_lock_release(a)		do { } while (0)
333 
rcu_read_lock_held(void)334 static inline int rcu_read_lock_held(void)
335 {
336 	return 1;
337 }
338 
rcu_read_lock_bh_held(void)339 static inline int rcu_read_lock_bh_held(void)
340 {
341 	return 1;
342 }
343 
rcu_read_lock_sched_held(void)344 static inline int rcu_read_lock_sched_held(void)
345 {
346 	return !preemptible();
347 }
348 
rcu_read_lock_any_held(void)349 static inline int rcu_read_lock_any_held(void)
350 {
351 	return !preemptible();
352 }
353 
debug_lockdep_rcu_enabled(void)354 static inline int debug_lockdep_rcu_enabled(void)
355 {
356 	return 0;
357 }
358 
359 #endif /* #else #ifdef CONFIG_DEBUG_LOCK_ALLOC */
360 
361 #ifdef CONFIG_PROVE_RCU
362 
363 /**
364  * RCU_LOCKDEP_WARN - emit lockdep splat if specified condition is met
365  * @c: condition to check
366  * @s: informative message
367  *
368  * This checks debug_lockdep_rcu_enabled() before checking (c) to
369  * prevent early boot splats due to lockdep not yet being initialized,
370  * and rechecks it after checking (c) to prevent false-positive splats
371  * due to races with lockdep being disabled.  See commit 3066820034b5dd
372  * ("rcu: Reject RCU_LOCKDEP_WARN() false positives") for more detail.
373  */
374 #define RCU_LOCKDEP_WARN(c, s)						\
375 	do {								\
376 		static bool __section(".data..unlikely") __warned;	\
377 		if (debug_lockdep_rcu_enabled() && (c) &&		\
378 		    debug_lockdep_rcu_enabled() && !__warned) {		\
379 			__warned = true;				\
380 			lockdep_rcu_suspicious(__FILE__, __LINE__, s);	\
381 		}							\
382 	} while (0)
383 
384 #ifndef CONFIG_PREEMPT_RCU
rcu_preempt_sleep_check(void)385 static inline void rcu_preempt_sleep_check(void)
386 {
387 	RCU_LOCKDEP_WARN(lock_is_held(&rcu_lock_map),
388 			 "Illegal context switch in RCU read-side critical section");
389 }
390 #else // #ifndef CONFIG_PREEMPT_RCU
rcu_preempt_sleep_check(void)391 static inline void rcu_preempt_sleep_check(void) { }
392 #endif // #else // #ifndef CONFIG_PREEMPT_RCU
393 
394 #define rcu_sleep_check()						\
395 	do {								\
396 		rcu_preempt_sleep_check();				\
397 		if (!IS_ENABLED(CONFIG_PREEMPT_RT))			\
398 		    RCU_LOCKDEP_WARN(lock_is_held(&rcu_bh_lock_map),	\
399 				 "Illegal context switch in RCU-bh read-side critical section"); \
400 		RCU_LOCKDEP_WARN(lock_is_held(&rcu_sched_lock_map),	\
401 				 "Illegal context switch in RCU-sched read-side critical section"); \
402 	} while (0)
403 
404 // See RCU_LOCKDEP_WARN() for an explanation of the double call to
405 // debug_lockdep_rcu_enabled().
lockdep_assert_rcu_helper(bool c,const struct __ctx_lock_RCU * ctx)406 static __always_inline bool lockdep_assert_rcu_helper(bool c, const struct __ctx_lock_RCU *ctx)
407 	__assumes_shared_ctx_lock(RCU) __assumes_shared_ctx_lock(ctx)
408 {
409 	return debug_lockdep_rcu_enabled() &&
410 	       (c || !rcu_is_watching() || !rcu_lockdep_current_cpu_online()) &&
411 	       debug_lockdep_rcu_enabled();
412 }
413 
414 /**
415  * lockdep_assert_in_rcu_read_lock - WARN if not protected by rcu_read_lock()
416  *
417  * Splats if lockdep is enabled and there is no rcu_read_lock() in effect.
418  */
419 #define lockdep_assert_in_rcu_read_lock() \
420 	WARN_ON_ONCE(lockdep_assert_rcu_helper(!lock_is_held(&rcu_lock_map), RCU))
421 
422 /**
423  * lockdep_assert_in_rcu_read_lock_bh - WARN if not protected by rcu_read_lock_bh()
424  *
425  * Splats if lockdep is enabled and there is no rcu_read_lock_bh() in effect.
426  * Note that local_bh_disable() and friends do not suffice here, instead an
427  * actual rcu_read_lock_bh() is required.
428  */
429 #define lockdep_assert_in_rcu_read_lock_bh() \
430 	WARN_ON_ONCE(lockdep_assert_rcu_helper(!lock_is_held(&rcu_bh_lock_map), RCU_BH))
431 
432 /**
433  * lockdep_assert_in_rcu_read_lock_sched - WARN if not protected by rcu_read_lock_sched()
434  *
435  * Splats if lockdep is enabled and there is no rcu_read_lock_sched()
436  * in effect.  Note that preempt_disable() and friends do not suffice here,
437  * instead an actual rcu_read_lock_sched() is required.
438  */
439 #define lockdep_assert_in_rcu_read_lock_sched() \
440 	WARN_ON_ONCE(lockdep_assert_rcu_helper(!lock_is_held(&rcu_sched_lock_map), RCU_SCHED))
441 
442 /**
443  * lockdep_assert_in_rcu_reader - WARN if not within some type of RCU reader
444  *
445  * Splats if lockdep is enabled and there is no RCU reader of any
446  * type in effect.  Note that regions of code protected by things like
447  * preempt_disable, local_bh_disable(), and local_irq_disable() all qualify
448  * as RCU readers.
449  *
450  * Note that this will never trigger in PREEMPT_NONE or PREEMPT_VOLUNTARY
451  * kernels that are not also built with PREEMPT_COUNT.  But if you have
452  * lockdep enabled, you might as well also enable PREEMPT_COUNT.
453  */
454 #define lockdep_assert_in_rcu_reader()								\
455 	WARN_ON_ONCE(lockdep_assert_rcu_helper(!lock_is_held(&rcu_lock_map) &&			\
456 					       !lock_is_held(&rcu_bh_lock_map) &&		\
457 					       !lock_is_held(&rcu_sched_lock_map) &&		\
458 					       preemptible(), RCU))
459 
460 #else /* #ifdef CONFIG_PROVE_RCU */
461 
462 #define RCU_LOCKDEP_WARN(c, s) do { } while (0 && (c))
463 #define rcu_sleep_check() do { } while (0)
464 
465 #define lockdep_assert_in_rcu_read_lock() __assume_shared_ctx_lock(RCU)
466 #define lockdep_assert_in_rcu_read_lock_bh() __assume_shared_ctx_lock(RCU_BH)
467 #define lockdep_assert_in_rcu_read_lock_sched() __assume_shared_ctx_lock(RCU_SCHED)
468 #define lockdep_assert_in_rcu_reader() __assume_shared_ctx_lock(RCU)
469 
470 #endif /* #else #ifdef CONFIG_PROVE_RCU */
471 
472 /*
473  * Helper functions for rcu_dereference_check(), rcu_dereference_protected()
474  * and rcu_assign_pointer().  Some of these could be folded into their
475  * callers, but they are left separate in order to ease introduction of
476  * multiple pointers markings to match different RCU implementations
477  * (e.g., __srcu), should this make sense in the future.
478  */
479 
480 #ifdef __CHECKER__
481 #define rcu_check_sparse(p, space) \
482 	((void)(((typeof(*p) space *)p) == p))
483 #else /* #ifdef __CHECKER__ */
484 #define rcu_check_sparse(p, space)
485 #endif /* #else #ifdef __CHECKER__ */
486 
487 #define __unrcu_pointer(p, local)					\
488 context_unsafe(								\
489 	typeof(*p) *local = (typeof(*p) *__force)(p);			\
490 	rcu_check_sparse(p, __rcu);					\
491 	((typeof(*p) __force __kernel *)(local))			\
492 )
493 /**
494  * unrcu_pointer - mark a pointer as not being RCU protected
495  * @p: pointer needing to lose its __rcu property
496  *
497  * Converts @p from an __rcu pointer to a __kernel pointer.
498  * This allows an __rcu pointer to be used with xchg() and friends.
499  */
500 #define unrcu_pointer(p) __unrcu_pointer(p, __UNIQUE_ID(rcu))
501 
502 #define __rcu_access_pointer(p, local, space) \
503 ({ \
504 	typeof(*p) *local = (typeof(*p) *__force)READ_ONCE(p); \
505 	rcu_check_sparse(p, space); \
506 	((typeof(*p) __force __kernel *)(local)); \
507 })
508 #define __rcu_dereference_check(p, local, c, space) \
509 ({ \
510 	/* Dependency order vs. p above. */ \
511 	typeof(*p) *local = (typeof(*p) *__force)READ_ONCE(p); \
512 	RCU_LOCKDEP_WARN(!(c), "suspicious rcu_dereference_check() usage"); \
513 	rcu_check_sparse(p, space); \
514 	((typeof(*p) __force __kernel *)(local)); \
515 })
516 #define __rcu_dereference_protected(p, local, c, space) \
517 ({ \
518 	RCU_LOCKDEP_WARN(!(c), "suspicious rcu_dereference_protected() usage"); \
519 	rcu_check_sparse(p, space); \
520 	((typeof(*p) __force __kernel *)(p)); \
521 })
522 #define __rcu_dereference_raw(p, local) \
523 ({ \
524 	/* Dependency order vs. p above. */ \
525 	typeof(p) local = READ_ONCE(p); \
526 	((typeof(*p) __force __kernel *)(local)); \
527 })
528 #define rcu_dereference_raw(p) __rcu_dereference_raw(p, __UNIQUE_ID(rcu))
529 
530 /**
531  * RCU_INITIALIZER() - statically initialize an RCU-protected global variable
532  * @v: The value to statically initialize with.
533  */
534 #define RCU_INITIALIZER(v) (typeof(*(v)) __force __rcu *)(v)
535 
536 /**
537  * rcu_assign_pointer() - assign to RCU-protected pointer
538  * @p: pointer to assign to
539  * @v: value to assign (publish)
540  *
541  * Assigns the specified value to the specified RCU-protected
542  * pointer, ensuring that any concurrent RCU readers will see
543  * any prior initialization.
544  *
545  * Inserts memory barriers on architectures that require them
546  * (which is most of them), and also prevents the compiler from
547  * reordering the code that initializes the structure after the pointer
548  * assignment.  More importantly, this call documents which pointers
549  * will be dereferenced by RCU read-side code.
550  *
551  * In some special cases, you may use RCU_INIT_POINTER() instead
552  * of rcu_assign_pointer().  RCU_INIT_POINTER() is a bit faster due
553  * to the fact that it does not constrain either the CPU or the compiler.
554  * That said, using RCU_INIT_POINTER() when you should have used
555  * rcu_assign_pointer() is a very bad thing that results in
556  * impossible-to-diagnose memory corruption.  So please be careful.
557  * See the RCU_INIT_POINTER() comment header for details.
558  *
559  * Note that rcu_assign_pointer() evaluates each of its arguments only
560  * once, appearances notwithstanding.  One of the "extra" evaluations
561  * is in typeof() and the other visible only to sparse (__CHECKER__),
562  * neither of which actually execute the argument.  As with most cpp
563  * macros, this execute-arguments-only-once property is important, so
564  * please be careful when making changes to rcu_assign_pointer() and the
565  * other macros that it invokes.
566  */
567 #define rcu_assign_pointer(p, v)					      \
568 context_unsafe(							      \
569 	uintptr_t _r_a_p__v = (uintptr_t)(v);				      \
570 	rcu_check_sparse(p, __rcu);					      \
571 									      \
572 	if (__builtin_constant_p(v) && (_r_a_p__v) == (uintptr_t)NULL)	      \
573 		WRITE_ONCE((p), (typeof(p))(_r_a_p__v));		      \
574 	else								      \
575 		smp_store_release(&p, RCU_INITIALIZER((typeof(p))_r_a_p__v)); \
576 )
577 
578 /**
579  * rcu_replace_pointer() - replace an RCU pointer, returning its old value
580  * @rcu_ptr: RCU pointer, whose old value is returned
581  * @ptr: regular pointer
582  * @c: the lockdep conditions under which the dereference will take place
583  *
584  * Perform a replacement, where @rcu_ptr is an RCU-annotated
585  * pointer and @c is the lockdep argument that is passed to the
586  * rcu_dereference_protected() call used to read that pointer.  The old
587  * value of @rcu_ptr is returned, and @rcu_ptr is set to @ptr.
588  */
589 #define rcu_replace_pointer(rcu_ptr, ptr, c)				\
590 ({									\
591 	typeof(ptr) __tmp = rcu_dereference_protected((rcu_ptr), (c));	\
592 	rcu_assign_pointer((rcu_ptr), (ptr));				\
593 	__tmp;								\
594 })
595 
596 /**
597  * rcu_access_pointer() - fetch RCU pointer with no dereferencing
598  * @p: The pointer to read
599  *
600  * Return the value of the specified RCU-protected pointer, but omit the
601  * lockdep checks for being in an RCU read-side critical section.  This is
602  * useful when the value of this pointer is accessed, but the pointer is
603  * not dereferenced, for example, when testing an RCU-protected pointer
604  * against NULL.  Although rcu_access_pointer() may also be used in cases
605  * where update-side locks prevent the value of the pointer from changing,
606  * you should instead use rcu_dereference_protected() for this use case.
607  * Within an RCU read-side critical section, there is little reason to
608  * use rcu_access_pointer().
609  *
610  * It is usually best to test the rcu_access_pointer() return value
611  * directly in order to avoid accidental dereferences being introduced
612  * by later inattentive changes.  In other words, assigning the
613  * rcu_access_pointer() return value to a local variable results in an
614  * accident waiting to happen.
615  *
616  * It is also permissible to use rcu_access_pointer() when read-side
617  * access to the pointer was removed at least one grace period ago, as is
618  * the case in the context of the RCU callback that is freeing up the data,
619  * or after a synchronize_rcu() returns.  This can be useful when tearing
620  * down multi-linked structures after a grace period has elapsed.  However,
621  * rcu_dereference_protected() is normally preferred for this use case.
622  */
623 #define rcu_access_pointer(p) __rcu_access_pointer((p), __UNIQUE_ID(rcu), __rcu)
624 
625 /**
626  * rcu_dereference_check() - rcu_dereference with debug checking
627  * @p: The pointer to read, prior to dereferencing
628  * @c: The conditions under which the dereference will take place
629  *
630  * Do an rcu_dereference(), but check that the conditions under which the
631  * dereference will take place are correct.  Typically the conditions
632  * indicate the various locking conditions that should be held at that
633  * point.  The check should return true if the conditions are satisfied.
634  * An implicit check for being in an RCU read-side critical section
635  * (rcu_read_lock()) is included.
636  *
637  * For example:
638  *
639  *	bar = rcu_dereference_check(foo->bar, lockdep_is_held(&foo->lock));
640  *
641  * could be used to indicate to lockdep that foo->bar may only be dereferenced
642  * if either rcu_read_lock() is held, or that the lock required to replace
643  * the bar struct at foo->bar is held.
644  *
645  * Note that the list of conditions may also include indications of when a lock
646  * need not be held, for example during initialisation or destruction of the
647  * target struct:
648  *
649  *	bar = rcu_dereference_check(foo->bar, lockdep_is_held(&foo->lock) ||
650  *					      atomic_read(&foo->usage) == 0);
651  *
652  * Inserts memory barriers on architectures that require them
653  * (currently only the Alpha), prevents the compiler from refetching
654  * (and from merging fetches), and, more importantly, documents exactly
655  * which pointers are protected by RCU and checks that the pointer is
656  * annotated as __rcu.
657  */
658 #define rcu_dereference_check(p, c) \
659 	__rcu_dereference_check((p), __UNIQUE_ID(rcu), \
660 				(c) || rcu_read_lock_held(), __rcu)
661 
662 /**
663  * rcu_dereference_bh_check() - rcu_dereference_bh with debug checking
664  * @p: The pointer to read, prior to dereferencing
665  * @c: The conditions under which the dereference will take place
666  *
667  * This is the RCU-bh counterpart to rcu_dereference_check().  However,
668  * please note that starting in v5.0 kernels, vanilla RCU grace periods
669  * wait for local_bh_disable() regions of code in addition to regions of
670  * code demarked by rcu_read_lock() and rcu_read_unlock().  This means
671  * that synchronize_rcu(), call_rcu, and friends all take not only
672  * rcu_read_lock() but also rcu_read_lock_bh() into account.
673  */
674 #define rcu_dereference_bh_check(p, c) \
675 	__rcu_dereference_check((p), __UNIQUE_ID(rcu), \
676 				(c) || rcu_read_lock_bh_held(), __rcu)
677 
678 /**
679  * rcu_dereference_sched_check() - rcu_dereference_sched with debug checking
680  * @p: The pointer to read, prior to dereferencing
681  * @c: The conditions under which the dereference will take place
682  *
683  * This is the RCU-sched counterpart to rcu_dereference_check().
684  * However, please note that starting in v5.0 kernels, vanilla RCU grace
685  * periods wait for preempt_disable() regions of code in addition to
686  * regions of code demarked by rcu_read_lock() and rcu_read_unlock().
687  * This means that synchronize_rcu(), call_rcu, and friends all take not
688  * only rcu_read_lock() but also rcu_read_lock_sched() into account.
689  */
690 #define rcu_dereference_sched_check(p, c) \
691 	__rcu_dereference_check((p), __UNIQUE_ID(rcu), \
692 				(c) || rcu_read_lock_sched_held(), \
693 				__rcu)
694 
695 /**
696  * rcu_dereference_all_check() - rcu_dereference_all with debug checking
697  * @p: The pointer to read, prior to dereferencing
698  * @c: The conditions under which the dereference will take place
699  *
700  * This is similar to rcu_dereference_check(), but allows protection
701  * by all forms of vanilla RCU readers, including preemption disabled,
702  * bh-disabled, and interrupt-disabled regions of code.  Note that "vanilla
703  * RCU" excludes SRCU and the various Tasks RCU flavors.  Please note
704  * that this macro should not be backported to any Linux-kernel version
705  * preceding v5.0 due to changes in synchronize_rcu() semantics prior
706  * to that version.
707  */
708 #define rcu_dereference_all_check(p, c) \
709 	__rcu_dereference_check((p), __UNIQUE_ID(rcu), \
710 				(c) || rcu_read_lock_any_held(), \
711 				__rcu)
712 
713 /*
714  * The tracing infrastructure traces RCU (we want that), but unfortunately
715  * some of the RCU checks causes tracing to lock up the system.
716  *
717  * The no-tracing version of rcu_dereference_raw() must not call
718  * rcu_read_lock_held().
719  */
720 #define rcu_dereference_raw_check(p) \
721 	__rcu_dereference_check((p), __UNIQUE_ID(rcu), 1, __rcu)
722 
723 /**
724  * rcu_dereference_protected() - fetch RCU pointer when updates prevented
725  * @p: The pointer to read, prior to dereferencing
726  * @c: The conditions under which the dereference will take place
727  *
728  * Return the value of the specified RCU-protected pointer, but omit
729  * the READ_ONCE().  This is useful in cases where update-side locks
730  * prevent the value of the pointer from changing.  Please note that this
731  * primitive does *not* prevent the compiler from repeating this reference
732  * or combining it with other references, so it should not be used without
733  * protection of appropriate locks.
734  *
735  * This function is only for update-side use.  Using this function
736  * when protected only by rcu_read_lock() will result in infrequent
737  * but very ugly failures.
738  */
739 #define rcu_dereference_protected(p, c) \
740 	__rcu_dereference_protected((p), __UNIQUE_ID(rcu), (c), __rcu)
741 
742 
743 /**
744  * rcu_dereference() - fetch RCU-protected pointer for dereferencing
745  * @p: The pointer to read, prior to dereferencing
746  *
747  * This is a simple wrapper around rcu_dereference_check().
748  */
749 #define rcu_dereference(p) rcu_dereference_check(p, 0)
750 
751 /**
752  * rcu_dereference_bh() - fetch an RCU-bh-protected pointer for dereferencing
753  * @p: The pointer to read, prior to dereferencing
754  *
755  * Makes rcu_dereference_check() do the dirty work.
756  */
757 #define rcu_dereference_bh(p) rcu_dereference_bh_check(p, 0)
758 
759 /**
760  * rcu_dereference_sched() - fetch RCU-sched-protected pointer for dereferencing
761  * @p: The pointer to read, prior to dereferencing
762  *
763  * Makes rcu_dereference_check() do the dirty work.
764  */
765 #define rcu_dereference_sched(p) rcu_dereference_sched_check(p, 0)
766 
767 /**
768  * rcu_dereference_all() - fetch RCU-all-protected pointer for dereferencing
769  * @p: The pointer to read, prior to dereferencing
770  *
771  * Makes rcu_dereference_check() do the dirty work.
772  */
773 #define rcu_dereference_all(p) rcu_dereference_all_check(p, 0)
774 
775 /**
776  * rcu_pointer_handoff() - Hand off a pointer from RCU to other mechanism
777  * @p: The pointer to hand off
778  *
779  * This is simply an identity function, but it documents where a pointer
780  * is handed off from RCU to some other synchronization mechanism, for
781  * example, reference counting or locking.  In C11, it would map to
782  * kill_dependency().  It could be used as follows::
783  *
784  *	rcu_read_lock();
785  *	p = rcu_dereference(gp);
786  *	long_lived = is_long_lived(p);
787  *	if (long_lived) {
788  *		if (!atomic_inc_not_zero(p->refcnt))
789  *			long_lived = false;
790  *		else
791  *			p = rcu_pointer_handoff(p);
792  *	}
793  *	rcu_read_unlock();
794  */
795 #define rcu_pointer_handoff(p) (p)
796 
797 /**
798  * rcu_read_lock() - mark the beginning of an RCU read-side critical section
799  *
800  * When synchronize_rcu() is invoked on one CPU while other CPUs
801  * are within RCU read-side critical sections, then the
802  * synchronize_rcu() is guaranteed to block until after all the other
803  * CPUs exit their critical sections.  Similarly, if call_rcu() is invoked
804  * on one CPU while other CPUs are within RCU read-side critical
805  * sections, invocation of the corresponding RCU callback is deferred
806  * until after the all the other CPUs exit their critical sections.
807  *
808  * Both synchronize_rcu() and call_rcu() also wait for regions of code
809  * with preemption disabled, including regions of code with interrupts or
810  * softirqs disabled.
811  *
812  * Note, however, that RCU callbacks are permitted to run concurrently
813  * with new RCU read-side critical sections.  One way that this can happen
814  * is via the following sequence of events: (1) CPU 0 enters an RCU
815  * read-side critical section, (2) CPU 1 invokes call_rcu() to register
816  * an RCU callback, (3) CPU 0 exits the RCU read-side critical section,
817  * (4) CPU 2 enters a RCU read-side critical section, (5) the RCU
818  * callback is invoked.  This is legal, because the RCU read-side critical
819  * section that was running concurrently with the call_rcu() (and which
820  * therefore might be referencing something that the corresponding RCU
821  * callback would free up) has completed before the corresponding
822  * RCU callback is invoked.
823  *
824  * RCU read-side critical sections may be nested.  Any deferred actions
825  * will be deferred until the outermost RCU read-side critical section
826  * completes.
827  *
828  * You can avoid reading and understanding the next paragraph by
829  * following this rule: don't put anything in an rcu_read_lock() RCU
830  * read-side critical section that would block in a !PREEMPTION kernel.
831  * But if you want the full story, read on!
832  *
833  * In non-preemptible RCU implementations (pure TREE_RCU and TINY_RCU),
834  * it is illegal to block while in an RCU read-side critical section.
835  * In preemptible RCU implementations (PREEMPT_RCU) in CONFIG_PREEMPTION
836  * kernel builds, RCU read-side critical sections may be preempted,
837  * but explicit blocking is illegal.  Finally, in preemptible RCU
838  * implementations in real-time (with -rt patchset) kernel builds, RCU
839  * read-side critical sections may be preempted and they may also block, but
840  * only when acquiring spinlocks that are subject to priority inheritance.
841  */
rcu_read_lock(void)842 static __always_inline void rcu_read_lock(void)
843 	__acquires_shared(RCU)
844 {
845 	__rcu_read_lock();
846 	__acquire_shared(RCU);
847 	rcu_lock_acquire(&rcu_lock_map);
848 	RCU_LOCKDEP_WARN(!rcu_is_watching(),
849 			 "rcu_read_lock() used illegally while idle");
850 }
851 
852 /*
853  * So where is rcu_write_lock()?  It does not exist, as there is no
854  * way for writers to lock out RCU readers.  This is a feature, not
855  * a bug -- this property is what provides RCU's performance benefits.
856  * Of course, writers must coordinate with each other.  The normal
857  * spinlock primitives work well for this, but any other technique may be
858  * used as well.  RCU does not care how the writers keep out of each
859  * others' way, as long as they do so.
860  */
861 
862 /**
863  * rcu_read_unlock() - marks the end of an RCU read-side critical section.
864  *
865  * In almost all situations, rcu_read_unlock() is immune from deadlock.
866  * This deadlock immunity also extends to the scheduler's runqueue
867  * and priority-inheritance spinlocks, courtesy of the quiescent-state
868  * deferral that is carried out when rcu_read_unlock() is invoked with
869  * interrupts disabled.
870  *
871  * See rcu_read_lock() for more information.
872  */
rcu_read_unlock(void)873 static inline void rcu_read_unlock(void)
874 	__releases_shared(RCU)
875 {
876 	RCU_LOCKDEP_WARN(!rcu_is_watching(),
877 			 "rcu_read_unlock() used illegally while idle");
878 	rcu_lock_release(&rcu_lock_map); /* Keep acq info for rls diags. */
879 	__release_shared(RCU);
880 	__rcu_read_unlock();
881 }
882 
883 /**
884  * rcu_read_lock_bh() - mark the beginning of an RCU-bh critical section
885  *
886  * This is equivalent to rcu_read_lock(), but also disables softirqs.
887  * Note that anything else that disables softirqs can also serve as an RCU
888  * read-side critical section.  However, please note that this equivalence
889  * applies only to v5.0 and later.  Before v5.0, rcu_read_lock() and
890  * rcu_read_lock_bh() were unrelated.
891  *
892  * Note that rcu_read_lock_bh() and the matching rcu_read_unlock_bh()
893  * must occur in the same context, for example, it is illegal to invoke
894  * rcu_read_unlock_bh() from one task if the matching rcu_read_lock_bh()
895  * was invoked from some other task.
896  */
rcu_read_lock_bh(void)897 static inline void rcu_read_lock_bh(void)
898 	__acquires_shared(RCU) __acquires_shared(RCU_BH)
899 {
900 	local_bh_disable();
901 	__acquire_shared(RCU);
902 	__acquire_shared(RCU_BH);
903 	rcu_lock_acquire(&rcu_bh_lock_map);
904 	RCU_LOCKDEP_WARN(!rcu_is_watching(),
905 			 "rcu_read_lock_bh() used illegally while idle");
906 }
907 
908 /**
909  * rcu_read_unlock_bh() - marks the end of a softirq-only RCU critical section
910  *
911  * See rcu_read_lock_bh() for more information.
912  */
rcu_read_unlock_bh(void)913 static inline void rcu_read_unlock_bh(void)
914 	__releases_shared(RCU) __releases_shared(RCU_BH)
915 {
916 	RCU_LOCKDEP_WARN(!rcu_is_watching(),
917 			 "rcu_read_unlock_bh() used illegally while idle");
918 	rcu_lock_release(&rcu_bh_lock_map);
919 	__release_shared(RCU_BH);
920 	__release_shared(RCU);
921 	local_bh_enable();
922 }
923 
924 /**
925  * rcu_read_lock_sched() - mark the beginning of a RCU-sched critical section
926  *
927  * This is equivalent to rcu_read_lock(), but also disables preemption.
928  * Read-side critical sections can also be introduced by anything else that
929  * disables preemption, including local_irq_disable() and friends.  However,
930  * please note that the equivalence to rcu_read_lock() applies only to
931  * v5.0 and later.  Before v5.0, rcu_read_lock() and rcu_read_lock_sched()
932  * were unrelated.
933  *
934  * Note that rcu_read_lock_sched() and the matching rcu_read_unlock_sched()
935  * must occur in the same context, for example, it is illegal to invoke
936  * rcu_read_unlock_sched() from process context if the matching
937  * rcu_read_lock_sched() was invoked from an NMI handler.
938  */
rcu_read_lock_sched(void)939 static inline void rcu_read_lock_sched(void)
940 	__acquires_shared(RCU) __acquires_shared(RCU_SCHED)
941 {
942 	preempt_disable();
943 	__acquire_shared(RCU);
944 	__acquire_shared(RCU_SCHED);
945 	rcu_lock_acquire(&rcu_sched_lock_map);
946 	RCU_LOCKDEP_WARN(!rcu_is_watching(),
947 			 "rcu_read_lock_sched() used illegally while idle");
948 }
949 
950 /* Used by lockdep and tracing: cannot be traced, cannot call lockdep. */
rcu_read_lock_sched_notrace(void)951 static inline notrace void rcu_read_lock_sched_notrace(void)
952 	__acquires_shared(RCU) __acquires_shared(RCU_SCHED)
953 {
954 	preempt_disable_notrace();
955 	__acquire_shared(RCU);
956 	__acquire_shared(RCU_SCHED);
957 }
958 
959 /**
960  * rcu_read_unlock_sched() - marks the end of a RCU-classic critical section
961  *
962  * See rcu_read_lock_sched() for more information.
963  */
rcu_read_unlock_sched(void)964 static inline void rcu_read_unlock_sched(void)
965 	__releases_shared(RCU) __releases_shared(RCU_SCHED)
966 {
967 	RCU_LOCKDEP_WARN(!rcu_is_watching(),
968 			 "rcu_read_unlock_sched() used illegally while idle");
969 	rcu_lock_release(&rcu_sched_lock_map);
970 	__release_shared(RCU_SCHED);
971 	__release_shared(RCU);
972 	preempt_enable();
973 }
974 
975 /* Used by lockdep and tracing: cannot be traced, cannot call lockdep. */
rcu_read_unlock_sched_notrace(void)976 static inline notrace void rcu_read_unlock_sched_notrace(void)
977 	__releases_shared(RCU) __releases_shared(RCU_SCHED)
978 {
979 	__release_shared(RCU_SCHED);
980 	__release_shared(RCU);
981 	preempt_enable_notrace();
982 }
983 
rcu_read_lock_dont_migrate(void)984 static __always_inline void rcu_read_lock_dont_migrate(void)
985 	__acquires_shared(RCU)
986 {
987 	if (IS_ENABLED(CONFIG_PREEMPT_RCU))
988 		migrate_disable();
989 	rcu_read_lock();
990 }
991 
rcu_read_unlock_migrate(void)992 static inline void rcu_read_unlock_migrate(void)
993 	__releases_shared(RCU)
994 {
995 	rcu_read_unlock();
996 	if (IS_ENABLED(CONFIG_PREEMPT_RCU))
997 		migrate_enable();
998 }
999 
1000 /**
1001  * RCU_INIT_POINTER() - initialize an RCU protected pointer
1002  * @p: The pointer to be initialized.
1003  * @v: The value to initialized the pointer to.
1004  *
1005  * Initialize an RCU-protected pointer in special cases where readers
1006  * do not need ordering constraints on the CPU or the compiler.  These
1007  * special cases are:
1008  *
1009  * 1.	This use of RCU_INIT_POINTER() is NULLing out the pointer *or*
1010  * 2.	The caller has taken whatever steps are required to prevent
1011  *	RCU readers from concurrently accessing this pointer *or*
1012  * 3.	The referenced data structure has already been exposed to
1013  *	readers either at compile time or via rcu_assign_pointer() *and*
1014  *
1015  *	a.	You have not made *any* reader-visible changes to
1016  *		this structure since then *or*
1017  *	b.	It is OK for readers accessing this structure from its
1018  *		new location to see the old state of the structure.  (For
1019  *		example, the changes were to statistical counters or to
1020  *		other state where exact synchronization is not required.)
1021  *
1022  * Failure to follow these rules governing use of RCU_INIT_POINTER() will
1023  * result in impossible-to-diagnose memory corruption.  As in the structures
1024  * will look OK in crash dumps, but any concurrent RCU readers might
1025  * see pre-initialized values of the referenced data structure.  So
1026  * please be very careful how you use RCU_INIT_POINTER()!!!
1027  *
1028  * If you are creating an RCU-protected linked structure that is accessed
1029  * by a single external-to-structure RCU-protected pointer, then you may
1030  * use RCU_INIT_POINTER() to initialize the internal RCU-protected
1031  * pointers, but you must use rcu_assign_pointer() to initialize the
1032  * external-to-structure pointer *after* you have completely initialized
1033  * the reader-accessible portions of the linked structure.
1034  *
1035  * Note that unlike rcu_assign_pointer(), RCU_INIT_POINTER() provides no
1036  * ordering guarantees for either the CPU or the compiler.
1037  */
1038 #define RCU_INIT_POINTER(p, v) \
1039 	context_unsafe( \
1040 		rcu_check_sparse(p, __rcu); \
1041 		WRITE_ONCE(p, RCU_INITIALIZER(v)); \
1042 	)
1043 
1044 /**
1045  * RCU_POINTER_INITIALIZER() - statically initialize an RCU protected pointer
1046  * @p: The pointer to be initialized.
1047  * @v: The value to initialized the pointer to.
1048  *
1049  * GCC-style initialization for an RCU-protected pointer in a structure field.
1050  */
1051 #define RCU_POINTER_INITIALIZER(p, v) \
1052 		.p = RCU_INITIALIZER(v)
1053 
1054 /**
1055  * kfree_rcu() - kfree an object after a grace period.
1056  * @ptr: pointer to kfree for double-argument invocations.
1057  * @rhf: the name of the struct rcu_head within the type of @ptr.
1058  *
1059  * Many rcu callbacks functions just call kfree() on the base structure.
1060  * These functions are trivial, but their size adds up, and furthermore
1061  * when they are used in a kernel module, that module must invoke the
1062  * high-latency rcu_barrier() function at module-unload time.
1063  *
1064  * The kfree_rcu() function handles this issue. In order to have a universal
1065  * callback function handling different offsets of rcu_head, the callback needs
1066  * to determine the starting address of the freed object, which can be a large
1067  * kmalloc or vmalloc allocation. To allow simply aligning the pointer down to
1068  * page boundary for those, only offsets up to 4095 bytes can be accommodated.
1069  * If the offset is larger than 4095 bytes, a compile-time error will
1070  * be generated in kvfree_rcu_arg_2(). If this error is triggered, you can
1071  * either fall back to use of call_rcu() or rearrange the structure to
1072  * position the rcu_head structure into the first 4096 bytes.
1073  *
1074  * The object to be freed can be allocated either by kmalloc(),
1075  * kmalloc_nolock(), or kmem_cache_alloc().
1076  *
1077  * Note that the allowable offset might decrease in the future.
1078  *
1079  * The BUILD_BUG_ON check must not involve any function calls, hence the
1080  * checks are done in macros here.
1081  */
1082 #define kfree_rcu(ptr, rhf) kvfree_rcu_arg_2(ptr, rhf)
1083 #define kvfree_rcu(ptr, rhf) kvfree_rcu_arg_2(ptr, rhf)
1084 
1085 /**
1086  * kfree_rcu_mightsleep() - kfree an object after a grace period.
1087  * @ptr: pointer to kfree for single-argument invocations.
1088  *
1089  * When it comes to head-less variant, only one argument
1090  * is passed and that is just a pointer which has to be
1091  * freed after a grace period. Therefore the semantic is
1092  *
1093  *     kfree_rcu_mightsleep(ptr);
1094  *
1095  * where @ptr is the pointer to be freed by kvfree().
1096  *
1097  * Please note, head-less way of freeing is permitted to
1098  * use from a context that has to follow might_sleep()
1099  * annotation. Otherwise, please switch and embed the
1100  * rcu_head structure within the type of @ptr.
1101  */
1102 #define kfree_rcu_mightsleep(ptr) kvfree_rcu_arg_1(ptr)
1103 #define kvfree_rcu_mightsleep(ptr) kvfree_rcu_arg_1(ptr)
1104 
1105 /*
1106  * In mm/slab_common.c, no suitable header to include here.
1107  */
1108 void kvfree_call_rcu(struct rcu_head *head, void *ptr);
1109 
1110 /*
1111  * The BUILD_BUG_ON() makes sure the rcu_head offset can be handled. See the
1112  * comment of kfree_rcu() for details.
1113  */
1114 #define kvfree_rcu_arg_2(ptr, rhf)					\
1115 do {									\
1116 	typeof (ptr) ___p = (ptr);					\
1117 									\
1118 	if (___p) {							\
1119 		BUILD_BUG_ON(offsetof(typeof(*(ptr)), rhf) >= 4096);	\
1120 		kvfree_call_rcu(&((___p)->rhf), (void *) (___p));	\
1121 	}								\
1122 } while (0)
1123 
1124 #define kvfree_rcu_arg_1(ptr)					\
1125 do {								\
1126 	typeof(ptr) ___p = (ptr);				\
1127 								\
1128 	if (___p)						\
1129 		kvfree_call_rcu(NULL, (void *) (___p));		\
1130 } while (0)
1131 
1132 /*
1133  * Place this after a lock-acquisition primitive to guarantee that
1134  * an UNLOCK+LOCK pair acts as a full barrier.  This guarantee applies
1135  * if the UNLOCK and LOCK are executed by the same CPU or if the
1136  * UNLOCK and LOCK operate on the same lock variable.
1137  */
1138 #ifdef CONFIG_ARCH_WEAK_RELEASE_ACQUIRE
1139 #define smp_mb__after_unlock_lock()	smp_mb()  /* Full ordering for lock. */
1140 #else /* #ifdef CONFIG_ARCH_WEAK_RELEASE_ACQUIRE */
1141 #define smp_mb__after_unlock_lock()	do { } while (0)
1142 #endif /* #else #ifdef CONFIG_ARCH_WEAK_RELEASE_ACQUIRE */
1143 
1144 
1145 /* Has the specified rcu_head structure been handed to call_rcu()? */
1146 
1147 /**
1148  * rcu_head_init - Initialize rcu_head for rcu_head_after_call_rcu()
1149  * @rhp: The rcu_head structure to initialize.
1150  *
1151  * If you intend to invoke rcu_head_after_call_rcu() to test whether a
1152  * given rcu_head structure has already been passed to call_rcu(), then
1153  * you must also invoke this rcu_head_init() function on it just after
1154  * allocating that structure.  Calls to this function must not race with
1155  * calls to call_rcu(), rcu_head_after_call_rcu(), or callback invocation.
1156  */
rcu_head_init(struct rcu_head * rhp)1157 static inline void rcu_head_init(struct rcu_head *rhp)
1158 {
1159 	rhp->func = (rcu_callback_t)~0L;
1160 }
1161 
1162 /**
1163  * rcu_head_after_call_rcu() - Has this rcu_head been passed to call_rcu()?
1164  * @rhp: The rcu_head structure to test.
1165  * @f: The function passed to call_rcu() along with @rhp.
1166  *
1167  * Returns @true if the @rhp has been passed to call_rcu() with @func,
1168  * and @false otherwise.  Emits a warning in any other case, including
1169  * the case where @rhp has already been invoked after a grace period.
1170  * Calls to this function must not race with callback invocation.  One way
1171  * to avoid such races is to enclose the call to rcu_head_after_call_rcu()
1172  * in an RCU read-side critical section that includes a read-side fetch
1173  * of the pointer to the structure containing @rhp.
1174  */
1175 static inline bool
rcu_head_after_call_rcu(struct rcu_head * rhp,rcu_callback_t f)1176 rcu_head_after_call_rcu(struct rcu_head *rhp, rcu_callback_t f)
1177 {
1178 	rcu_callback_t func = READ_ONCE(rhp->func);
1179 
1180 	if (func == f)
1181 		return true;
1182 	WARN_ON_ONCE(func != (rcu_callback_t)~0L);
1183 	return false;
1184 }
1185 
1186 /* kernel/ksysfs.c definitions */
1187 extern int rcu_expedited;
1188 extern int rcu_normal;
1189 
1190 DEFINE_LOCK_GUARD_0(rcu, rcu_read_lock(), rcu_read_unlock())
1191 DECLARE_LOCK_GUARD_0_ATTRS(rcu, __acquires_shared(RCU), __releases_shared(RCU))
1192 
1193 #endif /* __LINUX_RCUPDATE_H */
1194