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