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