blob: 9fbc54a2585d42cb9276adf2c2d168f53e883f63 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Paul E. McKenneya71fca52009-09-18 10:28:19 -07002 * Read-Copy Update mechanism for mutual exclusion
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
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, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 *
Paul E. McKenney01c1c662008-01-25 21:08:24 +010018 * Copyright IBM Corporation, 2001
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 *
20 * Author: Dipankar Sarma <dipankar@in.ibm.com>
Paul E. McKenneya71fca52009-09-18 10:28:19 -070021 *
Josh Triplett595182b2006-10-04 02:17:21 -070022 * Based on the original work by Paul McKenney <paulmck@us.ibm.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 * 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 -
Paul E. McKenneya71fca52009-09-18 10:28:19 -070029 * http://lse.sourceforge.net/locking/rcupdate.html
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 *
31 */
32
33#ifndef __LINUX_RCUPDATE_H
34#define __LINUX_RCUPDATE_H
35
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/cache.h>
37#include <linux/spinlock.h>
38#include <linux/threads.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/cpumask.h>
40#include <linux/seqlock.h>
Peter Zijlstra851a67b2007-10-11 22:11:12 +020041#include <linux/lockdep.h>
Paul E. McKenney4446a362008-05-12 21:21:05 +020042#include <linux/completion.h>
Mathieu Desnoyers551d55a2010-04-17 08:48:42 -040043#include <linux/debugobjects.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Dave Younge5ab6772010-03-10 15:24:05 -080045#ifdef CONFIG_RCU_TORTURE_TEST
46extern int rcutorture_runnable; /* for sysctl */
47#endif /* #ifdef CONFIG_RCU_TORTURE_TEST */
48
Linus Torvalds1da177e2005-04-16 15:20:36 -070049/**
50 * struct rcu_head - callback structure for use with RCU
51 * @next: next update requests in a list
52 * @func: actual update function to call after the grace period.
53 */
54struct rcu_head {
55 struct rcu_head *next;
56 void (*func)(struct rcu_head *head);
57};
58
Paul E. McKenney03b042b2009-06-25 09:08:16 -070059/* Exported common interfaces */
Paul E. McKenney03b042b2009-06-25 09:08:16 -070060extern void rcu_barrier(void);
61extern void rcu_barrier_bh(void);
62extern void rcu_barrier_sched(void);
63extern void synchronize_sched_expedited(void);
64extern int sched_expedited_torture_stats(char *page);
65
66/* Internal to kernel */
67extern void rcu_init(void);
Paul E. McKenneya6826042009-02-25 18:03:42 -080068
Paul E. McKenneyf41d9112009-08-22 13:56:52 -070069#if defined(CONFIG_TREE_RCU) || defined(CONFIG_TREE_PREEMPT_RCU)
Paul E. McKenney64db4cf2008-12-18 21:55:32 +010070#include <linux/rcutree.h>
Paul E. McKenney2c28e242009-10-26 13:57:44 -070071#elif defined(CONFIG_TINY_RCU)
Paul E. McKenney9b1d82f2009-10-25 19:03:50 -070072#include <linux/rcutiny.h>
Paul E. McKenney64db4cf2008-12-18 21:55:32 +010073#else
74#error "Unknown RCU implementation specified to kernel configuration"
Paul E. McKenney6b3ef482009-08-22 13:56:53 -070075#endif
Paul E. McKenney01c1c662008-01-25 21:08:24 +010076
Paul E. McKenney3d76c082009-09-28 07:46:32 -070077#define RCU_HEAD_INIT { .next = NULL, .func = NULL }
Dipankar Sarma8b6490e2005-09-09 13:04:07 -070078#define RCU_HEAD(head) struct rcu_head head = RCU_HEAD_INIT
Linus Torvalds1da177e2005-04-16 15:20:36 -070079#define INIT_RCU_HEAD(ptr) do { \
80 (ptr)->next = NULL; (ptr)->func = NULL; \
81} while (0)
82
Mathieu Desnoyers551d55a2010-04-17 08:48:42 -040083/*
84 * init_rcu_head_on_stack()/destroy_rcu_head_on_stack() are needed for dynamic
85 * initialization and destruction of rcu_head on the stack. rcu_head structures
86 * allocated dynamically in the heap or defined statically don't need any
87 * initialization.
88 */
89#ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD
90extern void init_rcu_head_on_stack(struct rcu_head *head);
91extern void destroy_rcu_head_on_stack(struct rcu_head *head);
92#else /* !CONFIG_DEBUG_OBJECTS_RCU_HEAD */
Mathieu Desnoyers43760302010-04-17 08:48:39 -040093static inline void init_rcu_head_on_stack(struct rcu_head *head)
94{
95}
96
97static inline void destroy_rcu_head_on_stack(struct rcu_head *head)
98{
99}
Mathieu Desnoyers551d55a2010-04-17 08:48:42 -0400100#endif /* #else !CONFIG_DEBUG_OBJECTS_RCU_HEAD */
Mathieu Desnoyers43760302010-04-17 08:48:39 -0400101
Paul E. McKenneybc33f242009-08-22 13:56:47 -0700102#ifdef CONFIG_DEBUG_LOCK_ALLOC
Paul E. McKenney632ee202010-02-22 17:04:45 -0800103
Paul E. McKenneybc33f242009-08-22 13:56:47 -0700104extern struct lockdep_map rcu_lock_map;
Paul E. McKenney632ee202010-02-22 17:04:45 -0800105# define rcu_read_acquire() \
106 lock_acquire(&rcu_lock_map, 0, 0, 2, 1, NULL, _THIS_IP_)
Paul E. McKenneybc33f242009-08-22 13:56:47 -0700107# define rcu_read_release() lock_release(&rcu_lock_map, 1, _THIS_IP_)
Paul E. McKenney632ee202010-02-22 17:04:45 -0800108
109extern struct lockdep_map rcu_bh_lock_map;
110# define rcu_read_acquire_bh() \
111 lock_acquire(&rcu_bh_lock_map, 0, 0, 2, 1, NULL, _THIS_IP_)
112# define rcu_read_release_bh() lock_release(&rcu_bh_lock_map, 1, _THIS_IP_)
113
114extern struct lockdep_map rcu_sched_lock_map;
115# define rcu_read_acquire_sched() \
116 lock_acquire(&rcu_sched_lock_map, 0, 0, 2, 1, NULL, _THIS_IP_)
117# define rcu_read_release_sched() \
118 lock_release(&rcu_sched_lock_map, 1, _THIS_IP_)
119
Paul E. McKenneybc293d62010-04-15 12:50:39 -0700120extern int debug_lockdep_rcu_enabled(void);
Paul E. McKenney54dbf962010-03-03 07:46:57 -0800121
Paul E. McKenney632ee202010-02-22 17:04:45 -0800122/**
123 * rcu_read_lock_held - might we be in RCU read-side critical section?
124 *
Paul E. McKenneyd20200b2010-03-30 10:52:21 -0700125 * If CONFIG_DEBUG_LOCK_ALLOC is selected, returns nonzero iff in an RCU
126 * read-side critical section. In absence of CONFIG_DEBUG_LOCK_ALLOC,
Paul E. McKenney632ee202010-02-22 17:04:45 -0800127 * this assumes we are in an RCU read-side critical section unless it can
128 * prove otherwise.
Paul E. McKenney54dbf962010-03-03 07:46:57 -0800129 *
Paul E. McKenney32c141a2010-03-30 10:59:28 -0700130 * Check debug_lockdep_rcu_enabled() to prevent false positives during boot
131 * and while lockdep is disabled.
Paul E. McKenney632ee202010-02-22 17:04:45 -0800132 */
133static inline int rcu_read_lock_held(void)
134{
Paul E. McKenney54dbf962010-03-03 07:46:57 -0800135 if (!debug_lockdep_rcu_enabled())
136 return 1;
137 return lock_is_held(&rcu_lock_map);
Paul E. McKenney632ee202010-02-22 17:04:45 -0800138}
139
Paul E. McKenneye3818b82010-03-15 17:03:43 -0700140/*
141 * rcu_read_lock_bh_held() is defined out of line to avoid #include-file
142 * hell.
Paul E. McKenney632ee202010-02-22 17:04:45 -0800143 */
Paul E. McKenneye3818b82010-03-15 17:03:43 -0700144extern int rcu_read_lock_bh_held(void);
Paul E. McKenney632ee202010-02-22 17:04:45 -0800145
146/**
147 * rcu_read_lock_sched_held - might we be in RCU-sched read-side critical section?
148 *
Paul E. McKenneyd20200b2010-03-30 10:52:21 -0700149 * If CONFIG_DEBUG_LOCK_ALLOC is selected, returns nonzero iff in an
150 * RCU-sched read-side critical section. In absence of
151 * CONFIG_DEBUG_LOCK_ALLOC, this assumes we are in an RCU-sched read-side
152 * critical section unless it can prove otherwise. Note that disabling
153 * of preemption (including disabling irqs) counts as an RCU-sched
154 * read-side critical section.
Paul E. McKenney54dbf962010-03-03 07:46:57 -0800155 *
Paul E. McKenney32c141a2010-03-30 10:59:28 -0700156 * Check debug_lockdep_rcu_enabled() to prevent false positives during boot
157 * and while lockdep is disabled.
Paul E. McKenney632ee202010-02-22 17:04:45 -0800158 */
Paul E. McKenneye6033e32010-03-03 17:50:16 -0800159#ifdef CONFIG_PREEMPT
Paul E. McKenney632ee202010-02-22 17:04:45 -0800160static inline int rcu_read_lock_sched_held(void)
161{
162 int lockdep_opinion = 0;
163
Paul E. McKenney54dbf962010-03-03 07:46:57 -0800164 if (!debug_lockdep_rcu_enabled())
165 return 1;
Paul E. McKenney632ee202010-02-22 17:04:45 -0800166 if (debug_locks)
167 lockdep_opinion = lock_is_held(&rcu_sched_lock_map);
Lai Jiangshan0cff8102010-03-18 12:25:33 -0700168 return lockdep_opinion || preempt_count() != 0 || irqs_disabled();
Paul E. McKenney632ee202010-02-22 17:04:45 -0800169}
Paul E. McKenneye6033e32010-03-03 17:50:16 -0800170#else /* #ifdef CONFIG_PREEMPT */
171static inline int rcu_read_lock_sched_held(void)
172{
173 return 1;
174}
175#endif /* #else #ifdef CONFIG_PREEMPT */
Paul E. McKenney632ee202010-02-22 17:04:45 -0800176
177#else /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
178
179# define rcu_read_acquire() do { } while (0)
180# define rcu_read_release() do { } while (0)
181# define rcu_read_acquire_bh() do { } while (0)
182# define rcu_read_release_bh() do { } while (0)
183# define rcu_read_acquire_sched() do { } while (0)
184# define rcu_read_release_sched() do { } while (0)
185
186static inline int rcu_read_lock_held(void)
187{
188 return 1;
189}
190
191static inline int rcu_read_lock_bh_held(void)
192{
193 return 1;
194}
195
Paul E. McKenneye6033e32010-03-03 17:50:16 -0800196#ifdef CONFIG_PREEMPT
Paul E. McKenney632ee202010-02-22 17:04:45 -0800197static inline int rcu_read_lock_sched_held(void)
198{
Paul E. McKenneybbad9372010-04-02 16:17:17 -0700199 return preempt_count() != 0 || irqs_disabled();
Paul E. McKenney632ee202010-02-22 17:04:45 -0800200}
Paul E. McKenneye6033e32010-03-03 17:50:16 -0800201#else /* #ifdef CONFIG_PREEMPT */
202static inline int rcu_read_lock_sched_held(void)
203{
204 return 1;
205}
206#endif /* #else #ifdef CONFIG_PREEMPT */
Paul E. McKenney632ee202010-02-22 17:04:45 -0800207
208#endif /* #else #ifdef CONFIG_DEBUG_LOCK_ALLOC */
209
210#ifdef CONFIG_PROVE_RCU
211
Paul E. McKenneyee84b822010-05-06 09:28:41 -0700212extern int rcu_my_thread_group_empty(void);
213
Lai Jiangshan2b3fc352010-04-20 16:23:07 +0800214#define __do_rcu_dereference_check(c) \
215 do { \
216 static bool __warned; \
217 if (debug_lockdep_rcu_enabled() && !__warned && !(c)) { \
218 __warned = true; \
219 lockdep_rcu_dereference(__FILE__, __LINE__); \
220 } \
221 } while (0)
222
Paul E. McKenney632ee202010-02-22 17:04:45 -0800223/**
224 * rcu_dereference_check - rcu_dereference with debug checking
David Howellsc08c68d2010-04-09 15:39:11 -0700225 * @p: The pointer to read, prior to dereferencing
226 * @c: The conditions under which the dereference will take place
Paul E. McKenney632ee202010-02-22 17:04:45 -0800227 *
David Howellsc08c68d2010-04-09 15:39:11 -0700228 * Do an rcu_dereference(), but check that the conditions under which the
229 * dereference will take place are correct. Typically the conditions indicate
230 * the various locking conditions that should be held at that point. The check
231 * should return true if the conditions are satisfied.
232 *
233 * For example:
234 *
235 * bar = rcu_dereference_check(foo->bar, rcu_read_lock_held() ||
236 * lockdep_is_held(&foo->lock));
237 *
238 * could be used to indicate to lockdep that foo->bar may only be dereferenced
239 * if either the RCU read lock is held, or that the lock required to replace
240 * the bar struct at foo->bar is held.
241 *
242 * Note that the list of conditions may also include indications of when a lock
243 * need not be held, for example during initialisation or destruction of the
244 * target struct:
245 *
246 * bar = rcu_dereference_check(foo->bar, rcu_read_lock_held() ||
247 * lockdep_is_held(&foo->lock) ||
248 * atomic_read(&foo->usage) == 0);
Paul E. McKenney632ee202010-02-22 17:04:45 -0800249 */
250#define rcu_dereference_check(p, c) \
251 ({ \
Lai Jiangshan2b3fc352010-04-20 16:23:07 +0800252 __do_rcu_dereference_check(c); \
Paul E. McKenneyc26d34a2010-02-22 17:04:46 -0800253 rcu_dereference_raw(p); \
Paul E. McKenney632ee202010-02-22 17:04:45 -0800254 })
255
Paul E. McKenneyb62730b2010-04-09 15:39:10 -0700256/**
257 * rcu_dereference_protected - fetch RCU pointer when updates prevented
258 *
259 * Return the value of the specified RCU-protected pointer, but omit
260 * both the smp_read_barrier_depends() and the ACCESS_ONCE(). This
261 * is useful in cases where update-side locks prevent the value of the
262 * pointer from changing. Please note that this primitive does -not-
263 * prevent the compiler from repeating this reference or combining it
264 * with other references, so it should not be used without protection
265 * of appropriate locks.
266 */
267#define rcu_dereference_protected(p, c) \
268 ({ \
Lai Jiangshan2b3fc352010-04-20 16:23:07 +0800269 __do_rcu_dereference_check(c); \
Paul E. McKenneyb62730b2010-04-09 15:39:10 -0700270 (p); \
271 })
272
Paul E. McKenney632ee202010-02-22 17:04:45 -0800273#else /* #ifdef CONFIG_PROVE_RCU */
274
Paul E. McKenneyc26d34a2010-02-22 17:04:46 -0800275#define rcu_dereference_check(p, c) rcu_dereference_raw(p)
Paul E. McKenneyb62730b2010-04-09 15:39:10 -0700276#define rcu_dereference_protected(p, c) (p)
Paul E. McKenney632ee202010-02-22 17:04:45 -0800277
278#endif /* #else #ifdef CONFIG_PROVE_RCU */
Paul E. McKenneybc33f242009-08-22 13:56:47 -0700279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280/**
Paul E. McKenneyb62730b2010-04-09 15:39:10 -0700281 * rcu_access_pointer - fetch RCU pointer with no dereferencing
282 *
283 * Return the value of the specified RCU-protected pointer, but omit the
284 * smp_read_barrier_depends() and keep the ACCESS_ONCE(). This is useful
285 * when the value of this pointer is accessed, but the pointer is not
286 * dereferenced, for example, when testing an RCU-protected pointer against
287 * NULL. This may also be used in cases where update-side locks prevent
288 * the value of the pointer from changing, but rcu_dereference_protected()
289 * is a lighter-weight primitive for this use case.
290 */
291#define rcu_access_pointer(p) ACCESS_ONCE(p)
292
293/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 * rcu_read_lock - mark the beginning of an RCU read-side critical section.
295 *
Paul E. McKenney9b06e812005-05-01 08:59:04 -0700296 * When synchronize_rcu() is invoked on one CPU while other CPUs
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 * are within RCU read-side critical sections, then the
Paul E. McKenney9b06e812005-05-01 08:59:04 -0700298 * synchronize_rcu() is guaranteed to block until after all the other
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 * CPUs exit their critical sections. Similarly, if call_rcu() is invoked
300 * on one CPU while other CPUs are within RCU read-side critical
301 * sections, invocation of the corresponding RCU callback is deferred
302 * until after the all the other CPUs exit their critical sections.
303 *
304 * Note, however, that RCU callbacks are permitted to run concurrently
305 * with RCU read-side critical sections. One way that this can happen
306 * is via the following sequence of events: (1) CPU 0 enters an RCU
307 * read-side critical section, (2) CPU 1 invokes call_rcu() to register
308 * an RCU callback, (3) CPU 0 exits the RCU read-side critical section,
309 * (4) CPU 2 enters a RCU read-side critical section, (5) the RCU
310 * callback is invoked. This is legal, because the RCU read-side critical
311 * section that was running concurrently with the call_rcu() (and which
312 * therefore might be referencing something that the corresponding RCU
313 * callback would free up) has completed before the corresponding
314 * RCU callback is invoked.
315 *
316 * RCU read-side critical sections may be nested. Any deferred actions
317 * will be deferred until the outermost RCU read-side critical section
318 * completes.
319 *
320 * It is illegal to block while in an RCU read-side critical section.
321 */
Paul E. McKenneybc33f242009-08-22 13:56:47 -0700322static inline void rcu_read_lock(void)
323{
324 __rcu_read_lock();
325 __acquire(RCU);
326 rcu_read_acquire();
327}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329/*
330 * So where is rcu_write_lock()? It does not exist, as there is no
331 * way for writers to lock out RCU readers. This is a feature, not
332 * a bug -- this property is what provides RCU's performance benefits.
333 * Of course, writers must coordinate with each other. The normal
334 * spinlock primitives work well for this, but any other technique may be
335 * used as well. RCU does not care how the writers keep out of each
336 * others' way, as long as they do so.
337 */
Paul E. McKenney3d76c082009-09-28 07:46:32 -0700338
339/**
340 * rcu_read_unlock - marks the end of an RCU read-side critical section.
341 *
342 * See rcu_read_lock() for more information.
343 */
Paul E. McKenneybc33f242009-08-22 13:56:47 -0700344static inline void rcu_read_unlock(void)
345{
346 rcu_read_release();
347 __release(RCU);
348 __rcu_read_unlock();
349}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
351/**
352 * rcu_read_lock_bh - mark the beginning of a softirq-only RCU critical section
353 *
354 * This is equivalent of rcu_read_lock(), but to be used when updates
355 * are being done using call_rcu_bh(). Since call_rcu_bh() callbacks
356 * consider completion of a softirq handler to be a quiescent state,
357 * a process in RCU read-side critical section must be protected by
358 * disabling softirqs. Read-side critical sections in interrupt context
359 * can use just rcu_read_lock().
360 *
361 */
Paul E. McKenneybc33f242009-08-22 13:56:47 -0700362static inline void rcu_read_lock_bh(void)
363{
364 __rcu_read_lock_bh();
365 __acquire(RCU_BH);
Paul E. McKenney632ee202010-02-22 17:04:45 -0800366 rcu_read_acquire_bh();
Paul E. McKenneybc33f242009-08-22 13:56:47 -0700367}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
369/*
370 * rcu_read_unlock_bh - marks the end of a softirq-only RCU critical section
371 *
372 * See rcu_read_lock_bh() for more information.
373 */
Paul E. McKenneybc33f242009-08-22 13:56:47 -0700374static inline void rcu_read_unlock_bh(void)
375{
Paul E. McKenney632ee202010-02-22 17:04:45 -0800376 rcu_read_release_bh();
Paul E. McKenneybc33f242009-08-22 13:56:47 -0700377 __release(RCU_BH);
378 __rcu_read_unlock_bh();
379}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
381/**
Mathieu Desnoyers1c50b722008-09-29 11:06:46 -0400382 * rcu_read_lock_sched - mark the beginning of a RCU-classic critical section
383 *
384 * Should be used with either
385 * - synchronize_sched()
386 * or
387 * - call_rcu_sched() and rcu_barrier_sched()
388 * on the write-side to insure proper synchronization.
389 */
Paul E. McKenneyd6714c22009-08-22 13:56:46 -0700390static inline void rcu_read_lock_sched(void)
391{
392 preempt_disable();
Paul E. McKenneybc33f242009-08-22 13:56:47 -0700393 __acquire(RCU_SCHED);
Paul E. McKenney632ee202010-02-22 17:04:45 -0800394 rcu_read_acquire_sched();
Paul E. McKenneyd6714c22009-08-22 13:56:46 -0700395}
Paul E. McKenney1eba8f82009-09-23 09:50:42 -0700396
397/* Used by lockdep and tracing: cannot be traced, cannot call lockdep. */
Paul E. McKenney7c614d62009-08-24 09:42:00 -0700398static inline notrace void rcu_read_lock_sched_notrace(void)
Paul E. McKenneyd6714c22009-08-22 13:56:46 -0700399{
400 preempt_disable_notrace();
Paul E. McKenneybc33f242009-08-22 13:56:47 -0700401 __acquire(RCU_SCHED);
Paul E. McKenneyd6714c22009-08-22 13:56:46 -0700402}
Mathieu Desnoyers1c50b722008-09-29 11:06:46 -0400403
404/*
405 * rcu_read_unlock_sched - marks the end of a RCU-classic critical section
406 *
407 * See rcu_read_lock_sched for more information.
408 */
Paul E. McKenneyd6714c22009-08-22 13:56:46 -0700409static inline void rcu_read_unlock_sched(void)
410{
Paul E. McKenney632ee202010-02-22 17:04:45 -0800411 rcu_read_release_sched();
Paul E. McKenneybc33f242009-08-22 13:56:47 -0700412 __release(RCU_SCHED);
Paul E. McKenneyd6714c22009-08-22 13:56:46 -0700413 preempt_enable();
414}
Paul E. McKenney1eba8f82009-09-23 09:50:42 -0700415
416/* Used by lockdep and tracing: cannot be traced, cannot call lockdep. */
Paul E. McKenney7c614d62009-08-24 09:42:00 -0700417static inline notrace void rcu_read_unlock_sched_notrace(void)
Paul E. McKenneyd6714c22009-08-22 13:56:46 -0700418{
Paul E. McKenneybc33f242009-08-22 13:56:47 -0700419 __release(RCU_SCHED);
Paul E. McKenneyd6714c22009-08-22 13:56:46 -0700420 preempt_enable_notrace();
421}
Mathieu Desnoyers1c50b722008-09-29 11:06:46 -0400422
423
424/**
Paul E. McKenneyc26d34a2010-02-22 17:04:46 -0800425 * rcu_dereference_raw - fetch an RCU-protected pointer
426 *
427 * The caller must be within some flavor of RCU read-side critical
428 * section, or must be otherwise preventing the pointer from changing,
429 * for example, by holding an appropriate lock. This pointer may later
430 * be safely dereferenced. It is the caller's responsibility to have
431 * done the right thing, as this primitive does no checking of any kind.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 *
433 * Inserts memory barriers on architectures that require them
434 * (currently only the Alpha), and, more importantly, documents
435 * exactly which pointers are protected by RCU.
436 */
Paul E. McKenneyc26d34a2010-02-22 17:04:46 -0800437#define rcu_dereference_raw(p) ({ \
Paul E. McKenney97b43032007-10-16 23:26:04 -0700438 typeof(p) _________p1 = ACCESS_ONCE(p); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 smp_read_barrier_depends(); \
440 (_________p1); \
441 })
442
443/**
Paul E. McKenneyc26d34a2010-02-22 17:04:46 -0800444 * rcu_dereference - fetch an RCU-protected pointer, checking for RCU
445 *
446 * Makes rcu_dereference_check() do the dirty work.
447 */
448#define rcu_dereference(p) \
449 rcu_dereference_check(p, rcu_read_lock_held())
450
451/**
452 * rcu_dereference_bh - fetch an RCU-protected pointer, checking for RCU-bh
453 *
454 * Makes rcu_dereference_check() do the dirty work.
455 */
456#define rcu_dereference_bh(p) \
457 rcu_dereference_check(p, rcu_read_lock_bh_held())
458
459/**
460 * rcu_dereference_sched - fetch RCU-protected pointer, checking for RCU-sched
461 *
462 * Makes rcu_dereference_check() do the dirty work.
463 */
464#define rcu_dereference_sched(p) \
465 rcu_dereference_check(p, rcu_read_lock_sched_held())
466
467/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 * rcu_assign_pointer - assign (publicize) a pointer to a newly
469 * initialized structure that will be dereferenced by RCU read-side
470 * critical sections. Returns the value assigned.
471 *
472 * Inserts memory barriers on architectures that require them
473 * (pretty much all of them other than x86), and also prevents
474 * the compiler from reordering the code that initializes the
475 * structure after the pointer assignment. More importantly, this
476 * call documents which pointers will be dereferenced by RCU read-side
477 * code.
478 */
479
Paul E. McKenneyd99c4f62008-02-06 01:37:25 -0800480#define rcu_assign_pointer(p, v) \
481 ({ \
482 if (!__builtin_constant_p(v) || \
483 ((v) != NULL)) \
484 smp_wmb(); \
485 (p) = (v); \
486 })
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Paul E. McKenney4446a362008-05-12 21:21:05 +0200488/* Infrastructure to implement the synchronize_() primitives. */
489
490struct rcu_synchronize {
491 struct rcu_head head;
492 struct completion completion;
493};
494
495extern void wakeme_after_rcu(struct rcu_head *head);
496
Paul E. McKenney9b06e812005-05-01 08:59:04 -0700497/**
Paul E. McKenney01c1c662008-01-25 21:08:24 +0100498 * call_rcu - Queue an RCU callback for invocation after a grace period.
499 * @head: structure to be used for queueing the RCU updates.
500 * @func: actual update function to be invoked after the grace period
501 *
502 * The update function will be invoked some time after a full grace
503 * period elapses, in other words after all currently executing RCU
504 * read-side critical sections have completed. RCU read-side critical
505 * sections are delimited by rcu_read_lock() and rcu_read_unlock(),
506 * and may be nested.
507 */
508extern void call_rcu(struct rcu_head *head,
509 void (*func)(struct rcu_head *head));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
Paul E. McKenney01c1c662008-01-25 21:08:24 +0100511/**
512 * call_rcu_bh - Queue an RCU for invocation after a quicker grace period.
513 * @head: structure to be used for queueing the RCU updates.
514 * @func: actual update function to be invoked after the grace period
515 *
516 * The update function will be invoked some time after a full grace
517 * period elapses, in other words after all currently executing RCU
518 * read-side critical sections have completed. call_rcu_bh() assumes
519 * that the read-side critical sections end on completion of a softirq
520 * handler. This means that read-side critical sections in process
521 * context must not be interrupted by softirqs. This interface is to be
522 * used when most of the read-side critical sections are in softirq context.
523 * RCU read-side critical sections are delimited by :
524 * - rcu_read_lock() and rcu_read_unlock(), if in interrupt context.
525 * OR
526 * - rcu_read_lock_bh() and rcu_read_unlock_bh(), if in process context.
527 * These may be nested.
528 */
529extern void call_rcu_bh(struct rcu_head *head,
530 void (*func)(struct rcu_head *head));
531
Mathieu Desnoyers551d55a2010-04-17 08:48:42 -0400532/*
533 * debug_rcu_head_queue()/debug_rcu_head_unqueue() are used internally
534 * by call_rcu() and rcu callback execution, and are therefore not part of the
535 * RCU API. Leaving in rcupdate.h because they are used by all RCU flavors.
536 */
537
538#ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD
539# define STATE_RCU_HEAD_READY 0
540# define STATE_RCU_HEAD_QUEUED 1
541
542extern struct debug_obj_descr rcuhead_debug_descr;
543
544static inline void debug_rcu_head_queue(struct rcu_head *head)
545{
546 debug_object_activate(head, &rcuhead_debug_descr);
547 debug_object_active_state(head, &rcuhead_debug_descr,
548 STATE_RCU_HEAD_READY,
549 STATE_RCU_HEAD_QUEUED);
550}
551
552static inline void debug_rcu_head_unqueue(struct rcu_head *head)
553{
554 debug_object_active_state(head, &rcuhead_debug_descr,
555 STATE_RCU_HEAD_QUEUED,
556 STATE_RCU_HEAD_READY);
557 debug_object_deactivate(head, &rcuhead_debug_descr);
558}
559#else /* !CONFIG_DEBUG_OBJECTS_RCU_HEAD */
560static inline void debug_rcu_head_queue(struct rcu_head *head)
561{
562}
563
564static inline void debug_rcu_head_unqueue(struct rcu_head *head)
565{
566}
567#endif /* #else !CONFIG_DEBUG_OBJECTS_RCU_HEAD */
568
Paul E. McKenneyf5155b32010-04-30 06:42:01 -0700569#ifndef CONFIG_PROVE_RCU
570#define __do_rcu_dereference_check(c) do { } while (0)
571#endif /* #ifdef CONFIG_PROVE_RCU */
572
573#define __rcu_dereference_index_check(p, c) \
574 ({ \
575 typeof(p) _________p1 = ACCESS_ONCE(p); \
576 __do_rcu_dereference_check(c); \
577 smp_read_barrier_depends(); \
578 (_________p1); \
579 })
580
581/**
582 * rcu_dereference_index_check() - rcu_dereference for indices with debug checking
583 * @p: The pointer to read, prior to dereferencing
584 * @c: The conditions under which the dereference will take place
585 *
586 * Similar to rcu_dereference_check(), but omits the sparse checking.
587 * This allows rcu_dereference_index_check() to be used on integers,
588 * which can then be used as array indices. Attempting to use
589 * rcu_dereference_check() on an integer will give compiler warnings
590 * because the sparse address-space mechanism relies on dereferencing
591 * the RCU-protected pointer. Dereferencing integers is not something
592 * that even gcc will put up with.
593 *
594 * Note that this function does not implicitly check for RCU read-side
595 * critical sections. If this function gains lots of uses, it might
596 * make sense to provide versions for each flavor of RCU, but it does
597 * not make sense as of early 2010.
598 */
599#define rcu_dereference_index_check(p, c) \
600 __rcu_dereference_index_check((p), (c))
601
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602#endif /* __LINUX_RCUPDATE_H */