Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Paul E. McKenney | a71fca5 | 2009-09-18 10:28:19 -0700 | [diff] [blame] | 2 | * Read-Copy Update mechanism for mutual exclusion |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 17 | * |
Paul E. McKenney | 01c1c66 | 2008-01-25 21:08:24 +0100 | [diff] [blame] | 18 | * Copyright IBM Corporation, 2001 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | * |
| 20 | * Author: Dipankar Sarma <dipankar@in.ibm.com> |
Paul E. McKenney | a71fca5 | 2009-09-18 10:28:19 -0700 | [diff] [blame] | 21 | * |
Josh Triplett | 595182b | 2006-10-04 02:17:21 -0700 | [diff] [blame] | 22 | * Based on the original work by Paul McKenney <paulmck@us.ibm.com> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 - |
Paul E. McKenney | a71fca5 | 2009-09-18 10:28:19 -0700 | [diff] [blame] | 29 | * http://lse.sourceforge.net/locking/rcupdate.html |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | * |
| 31 | */ |
| 32 | |
| 33 | #ifndef __LINUX_RCUPDATE_H |
| 34 | #define __LINUX_RCUPDATE_H |
| 35 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | #include <linux/cache.h> |
| 37 | #include <linux/spinlock.h> |
| 38 | #include <linux/threads.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | #include <linux/cpumask.h> |
| 40 | #include <linux/seqlock.h> |
Peter Zijlstra | 851a67b | 2007-10-11 22:11:12 +0200 | [diff] [blame] | 41 | #include <linux/lockdep.h> |
Paul E. McKenney | 4446a36 | 2008-05-12 21:21:05 +0200 | [diff] [blame] | 42 | #include <linux/completion.h> |
Mathieu Desnoyers | 551d55a | 2010-04-17 08:48:42 -0400 | [diff] [blame] | 43 | #include <linux/debugobjects.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | |
Dave Young | e5ab677 | 2010-03-10 15:24:05 -0800 | [diff] [blame] | 45 | #ifdef CONFIG_RCU_TORTURE_TEST |
| 46 | extern int rcutorture_runnable; /* for sysctl */ |
| 47 | #endif /* #ifdef CONFIG_RCU_TORTURE_TEST */ |
| 48 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | /** |
| 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 | */ |
| 54 | struct rcu_head { |
| 55 | struct rcu_head *next; |
| 56 | void (*func)(struct rcu_head *head); |
| 57 | }; |
| 58 | |
Paul E. McKenney | 03b042b | 2009-06-25 09:08:16 -0700 | [diff] [blame] | 59 | /* Exported common interfaces */ |
Paul E. McKenney | 03b042b | 2009-06-25 09:08:16 -0700 | [diff] [blame] | 60 | extern void rcu_barrier(void); |
| 61 | extern void rcu_barrier_bh(void); |
| 62 | extern void rcu_barrier_sched(void); |
| 63 | extern void synchronize_sched_expedited(void); |
| 64 | extern int sched_expedited_torture_stats(char *page); |
| 65 | |
| 66 | /* Internal to kernel */ |
| 67 | extern void rcu_init(void); |
Paul E. McKenney | a682604 | 2009-02-25 18:03:42 -0800 | [diff] [blame] | 68 | |
Paul E. McKenney | f41d911 | 2009-08-22 13:56:52 -0700 | [diff] [blame] | 69 | #if defined(CONFIG_TREE_RCU) || defined(CONFIG_TREE_PREEMPT_RCU) |
Paul E. McKenney | 64db4cf | 2008-12-18 21:55:32 +0100 | [diff] [blame] | 70 | #include <linux/rcutree.h> |
Paul E. McKenney | 2c28e24 | 2009-10-26 13:57:44 -0700 | [diff] [blame] | 71 | #elif defined(CONFIG_TINY_RCU) |
Paul E. McKenney | 9b1d82f | 2009-10-25 19:03:50 -0700 | [diff] [blame] | 72 | #include <linux/rcutiny.h> |
Paul E. McKenney | 64db4cf | 2008-12-18 21:55:32 +0100 | [diff] [blame] | 73 | #else |
| 74 | #error "Unknown RCU implementation specified to kernel configuration" |
Paul E. McKenney | 6b3ef48 | 2009-08-22 13:56:53 -0700 | [diff] [blame] | 75 | #endif |
Paul E. McKenney | 01c1c66 | 2008-01-25 21:08:24 +0100 | [diff] [blame] | 76 | |
Paul E. McKenney | 3d76c08 | 2009-09-28 07:46:32 -0700 | [diff] [blame] | 77 | #define RCU_HEAD_INIT { .next = NULL, .func = NULL } |
Dipankar Sarma | 8b6490e | 2005-09-09 13:04:07 -0700 | [diff] [blame] | 78 | #define RCU_HEAD(head) struct rcu_head head = RCU_HEAD_INIT |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | #define INIT_RCU_HEAD(ptr) do { \ |
| 80 | (ptr)->next = NULL; (ptr)->func = NULL; \ |
| 81 | } while (0) |
| 82 | |
Mathieu Desnoyers | 551d55a | 2010-04-17 08:48:42 -0400 | [diff] [blame] | 83 | /* |
| 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 |
| 90 | extern void init_rcu_head_on_stack(struct rcu_head *head); |
| 91 | extern void destroy_rcu_head_on_stack(struct rcu_head *head); |
| 92 | #else /* !CONFIG_DEBUG_OBJECTS_RCU_HEAD */ |
Mathieu Desnoyers | 4376030 | 2010-04-17 08:48:39 -0400 | [diff] [blame] | 93 | static inline void init_rcu_head_on_stack(struct rcu_head *head) |
| 94 | { |
| 95 | } |
| 96 | |
| 97 | static inline void destroy_rcu_head_on_stack(struct rcu_head *head) |
| 98 | { |
| 99 | } |
Mathieu Desnoyers | 551d55a | 2010-04-17 08:48:42 -0400 | [diff] [blame] | 100 | #endif /* #else !CONFIG_DEBUG_OBJECTS_RCU_HEAD */ |
Mathieu Desnoyers | 4376030 | 2010-04-17 08:48:39 -0400 | [diff] [blame] | 101 | |
Paul E. McKenney | bc33f24 | 2009-08-22 13:56:47 -0700 | [diff] [blame] | 102 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
Paul E. McKenney | 632ee20 | 2010-02-22 17:04:45 -0800 | [diff] [blame] | 103 | |
Paul E. McKenney | bc33f24 | 2009-08-22 13:56:47 -0700 | [diff] [blame] | 104 | extern struct lockdep_map rcu_lock_map; |
Paul E. McKenney | 632ee20 | 2010-02-22 17:04:45 -0800 | [diff] [blame] | 105 | # define rcu_read_acquire() \ |
| 106 | lock_acquire(&rcu_lock_map, 0, 0, 2, 1, NULL, _THIS_IP_) |
Paul E. McKenney | bc33f24 | 2009-08-22 13:56:47 -0700 | [diff] [blame] | 107 | # define rcu_read_release() lock_release(&rcu_lock_map, 1, _THIS_IP_) |
Paul E. McKenney | 632ee20 | 2010-02-22 17:04:45 -0800 | [diff] [blame] | 108 | |
| 109 | extern 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 | |
| 114 | extern 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. McKenney | bc293d6 | 2010-04-15 12:50:39 -0700 | [diff] [blame] | 120 | extern int debug_lockdep_rcu_enabled(void); |
Paul E. McKenney | 54dbf96 | 2010-03-03 07:46:57 -0800 | [diff] [blame] | 121 | |
Paul E. McKenney | 632ee20 | 2010-02-22 17:04:45 -0800 | [diff] [blame] | 122 | /** |
| 123 | * rcu_read_lock_held - might we be in RCU read-side critical section? |
| 124 | * |
Paul E. McKenney | d20200b | 2010-03-30 10:52:21 -0700 | [diff] [blame] | 125 | * 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. McKenney | 632ee20 | 2010-02-22 17:04:45 -0800 | [diff] [blame] | 127 | * this assumes we are in an RCU read-side critical section unless it can |
| 128 | * prove otherwise. |
Paul E. McKenney | 54dbf96 | 2010-03-03 07:46:57 -0800 | [diff] [blame] | 129 | * |
Paul E. McKenney | 32c141a | 2010-03-30 10:59:28 -0700 | [diff] [blame] | 130 | * Check debug_lockdep_rcu_enabled() to prevent false positives during boot |
| 131 | * and while lockdep is disabled. |
Paul E. McKenney | 632ee20 | 2010-02-22 17:04:45 -0800 | [diff] [blame] | 132 | */ |
| 133 | static inline int rcu_read_lock_held(void) |
| 134 | { |
Paul E. McKenney | 54dbf96 | 2010-03-03 07:46:57 -0800 | [diff] [blame] | 135 | if (!debug_lockdep_rcu_enabled()) |
| 136 | return 1; |
| 137 | return lock_is_held(&rcu_lock_map); |
Paul E. McKenney | 632ee20 | 2010-02-22 17:04:45 -0800 | [diff] [blame] | 138 | } |
| 139 | |
Paul E. McKenney | e3818b8 | 2010-03-15 17:03:43 -0700 | [diff] [blame] | 140 | /* |
| 141 | * rcu_read_lock_bh_held() is defined out of line to avoid #include-file |
| 142 | * hell. |
Paul E. McKenney | 632ee20 | 2010-02-22 17:04:45 -0800 | [diff] [blame] | 143 | */ |
Paul E. McKenney | e3818b8 | 2010-03-15 17:03:43 -0700 | [diff] [blame] | 144 | extern int rcu_read_lock_bh_held(void); |
Paul E. McKenney | 632ee20 | 2010-02-22 17:04:45 -0800 | [diff] [blame] | 145 | |
| 146 | /** |
| 147 | * rcu_read_lock_sched_held - might we be in RCU-sched read-side critical section? |
| 148 | * |
Paul E. McKenney | d20200b | 2010-03-30 10:52:21 -0700 | [diff] [blame] | 149 | * 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. McKenney | 54dbf96 | 2010-03-03 07:46:57 -0800 | [diff] [blame] | 155 | * |
Paul E. McKenney | 32c141a | 2010-03-30 10:59:28 -0700 | [diff] [blame] | 156 | * Check debug_lockdep_rcu_enabled() to prevent false positives during boot |
| 157 | * and while lockdep is disabled. |
Paul E. McKenney | 632ee20 | 2010-02-22 17:04:45 -0800 | [diff] [blame] | 158 | */ |
Paul E. McKenney | e6033e3 | 2010-03-03 17:50:16 -0800 | [diff] [blame] | 159 | #ifdef CONFIG_PREEMPT |
Paul E. McKenney | 632ee20 | 2010-02-22 17:04:45 -0800 | [diff] [blame] | 160 | static inline int rcu_read_lock_sched_held(void) |
| 161 | { |
| 162 | int lockdep_opinion = 0; |
| 163 | |
Paul E. McKenney | 54dbf96 | 2010-03-03 07:46:57 -0800 | [diff] [blame] | 164 | if (!debug_lockdep_rcu_enabled()) |
| 165 | return 1; |
Paul E. McKenney | 632ee20 | 2010-02-22 17:04:45 -0800 | [diff] [blame] | 166 | if (debug_locks) |
| 167 | lockdep_opinion = lock_is_held(&rcu_sched_lock_map); |
Lai Jiangshan | 0cff810 | 2010-03-18 12:25:33 -0700 | [diff] [blame] | 168 | return lockdep_opinion || preempt_count() != 0 || irqs_disabled(); |
Paul E. McKenney | 632ee20 | 2010-02-22 17:04:45 -0800 | [diff] [blame] | 169 | } |
Paul E. McKenney | e6033e3 | 2010-03-03 17:50:16 -0800 | [diff] [blame] | 170 | #else /* #ifdef CONFIG_PREEMPT */ |
| 171 | static inline int rcu_read_lock_sched_held(void) |
| 172 | { |
| 173 | return 1; |
| 174 | } |
| 175 | #endif /* #else #ifdef CONFIG_PREEMPT */ |
Paul E. McKenney | 632ee20 | 2010-02-22 17:04:45 -0800 | [diff] [blame] | 176 | |
| 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 | |
| 186 | static inline int rcu_read_lock_held(void) |
| 187 | { |
| 188 | return 1; |
| 189 | } |
| 190 | |
| 191 | static inline int rcu_read_lock_bh_held(void) |
| 192 | { |
| 193 | return 1; |
| 194 | } |
| 195 | |
Paul E. McKenney | e6033e3 | 2010-03-03 17:50:16 -0800 | [diff] [blame] | 196 | #ifdef CONFIG_PREEMPT |
Paul E. McKenney | 632ee20 | 2010-02-22 17:04:45 -0800 | [diff] [blame] | 197 | static inline int rcu_read_lock_sched_held(void) |
| 198 | { |
Paul E. McKenney | bbad937 | 2010-04-02 16:17:17 -0700 | [diff] [blame] | 199 | return preempt_count() != 0 || irqs_disabled(); |
Paul E. McKenney | 632ee20 | 2010-02-22 17:04:45 -0800 | [diff] [blame] | 200 | } |
Paul E. McKenney | e6033e3 | 2010-03-03 17:50:16 -0800 | [diff] [blame] | 201 | #else /* #ifdef CONFIG_PREEMPT */ |
| 202 | static inline int rcu_read_lock_sched_held(void) |
| 203 | { |
| 204 | return 1; |
| 205 | } |
| 206 | #endif /* #else #ifdef CONFIG_PREEMPT */ |
Paul E. McKenney | 632ee20 | 2010-02-22 17:04:45 -0800 | [diff] [blame] | 207 | |
| 208 | #endif /* #else #ifdef CONFIG_DEBUG_LOCK_ALLOC */ |
| 209 | |
| 210 | #ifdef CONFIG_PROVE_RCU |
| 211 | |
Paul E. McKenney | ee84b82 | 2010-05-06 09:28:41 -0700 | [diff] [blame] | 212 | extern int rcu_my_thread_group_empty(void); |
| 213 | |
Lai Jiangshan | 2b3fc35 | 2010-04-20 16:23:07 +0800 | [diff] [blame] | 214 | #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. McKenney | 632ee20 | 2010-02-22 17:04:45 -0800 | [diff] [blame] | 223 | /** |
| 224 | * rcu_dereference_check - rcu_dereference with debug checking |
David Howells | c08c68d | 2010-04-09 15:39:11 -0700 | [diff] [blame] | 225 | * @p: The pointer to read, prior to dereferencing |
| 226 | * @c: The conditions under which the dereference will take place |
Paul E. McKenney | 632ee20 | 2010-02-22 17:04:45 -0800 | [diff] [blame] | 227 | * |
David Howells | c08c68d | 2010-04-09 15:39:11 -0700 | [diff] [blame] | 228 | * 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. McKenney | 632ee20 | 2010-02-22 17:04:45 -0800 | [diff] [blame] | 249 | */ |
| 250 | #define rcu_dereference_check(p, c) \ |
| 251 | ({ \ |
Lai Jiangshan | 2b3fc35 | 2010-04-20 16:23:07 +0800 | [diff] [blame] | 252 | __do_rcu_dereference_check(c); \ |
Paul E. McKenney | c26d34a | 2010-02-22 17:04:46 -0800 | [diff] [blame] | 253 | rcu_dereference_raw(p); \ |
Paul E. McKenney | 632ee20 | 2010-02-22 17:04:45 -0800 | [diff] [blame] | 254 | }) |
| 255 | |
Paul E. McKenney | b62730b | 2010-04-09 15:39:10 -0700 | [diff] [blame] | 256 | /** |
| 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 Jiangshan | 2b3fc35 | 2010-04-20 16:23:07 +0800 | [diff] [blame] | 269 | __do_rcu_dereference_check(c); \ |
Paul E. McKenney | b62730b | 2010-04-09 15:39:10 -0700 | [diff] [blame] | 270 | (p); \ |
| 271 | }) |
| 272 | |
Paul E. McKenney | 632ee20 | 2010-02-22 17:04:45 -0800 | [diff] [blame] | 273 | #else /* #ifdef CONFIG_PROVE_RCU */ |
| 274 | |
Paul E. McKenney | c26d34a | 2010-02-22 17:04:46 -0800 | [diff] [blame] | 275 | #define rcu_dereference_check(p, c) rcu_dereference_raw(p) |
Paul E. McKenney | b62730b | 2010-04-09 15:39:10 -0700 | [diff] [blame] | 276 | #define rcu_dereference_protected(p, c) (p) |
Paul E. McKenney | 632ee20 | 2010-02-22 17:04:45 -0800 | [diff] [blame] | 277 | |
| 278 | #endif /* #else #ifdef CONFIG_PROVE_RCU */ |
Paul E. McKenney | bc33f24 | 2009-08-22 13:56:47 -0700 | [diff] [blame] | 279 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | /** |
Paul E. McKenney | b62730b | 2010-04-09 15:39:10 -0700 | [diff] [blame] | 281 | * 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | * rcu_read_lock - mark the beginning of an RCU read-side critical section. |
| 295 | * |
Paul E. McKenney | 9b06e81 | 2005-05-01 08:59:04 -0700 | [diff] [blame] | 296 | * When synchronize_rcu() is invoked on one CPU while other CPUs |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | * are within RCU read-side critical sections, then the |
Paul E. McKenney | 9b06e81 | 2005-05-01 08:59:04 -0700 | [diff] [blame] | 298 | * synchronize_rcu() is guaranteed to block until after all the other |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | * 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. McKenney | bc33f24 | 2009-08-22 13:56:47 -0700 | [diff] [blame] | 322 | static inline void rcu_read_lock(void) |
| 323 | { |
| 324 | __rcu_read_lock(); |
| 325 | __acquire(RCU); |
| 326 | rcu_read_acquire(); |
| 327 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | /* |
| 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. McKenney | 3d76c08 | 2009-09-28 07:46:32 -0700 | [diff] [blame] | 338 | |
| 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. McKenney | bc33f24 | 2009-08-22 13:56:47 -0700 | [diff] [blame] | 344 | static inline void rcu_read_unlock(void) |
| 345 | { |
| 346 | rcu_read_release(); |
| 347 | __release(RCU); |
| 348 | __rcu_read_unlock(); |
| 349 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | |
| 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. McKenney | bc33f24 | 2009-08-22 13:56:47 -0700 | [diff] [blame] | 362 | static inline void rcu_read_lock_bh(void) |
| 363 | { |
| 364 | __rcu_read_lock_bh(); |
| 365 | __acquire(RCU_BH); |
Paul E. McKenney | 632ee20 | 2010-02-22 17:04:45 -0800 | [diff] [blame] | 366 | rcu_read_acquire_bh(); |
Paul E. McKenney | bc33f24 | 2009-08-22 13:56:47 -0700 | [diff] [blame] | 367 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | |
| 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. McKenney | bc33f24 | 2009-08-22 13:56:47 -0700 | [diff] [blame] | 374 | static inline void rcu_read_unlock_bh(void) |
| 375 | { |
Paul E. McKenney | 632ee20 | 2010-02-22 17:04:45 -0800 | [diff] [blame] | 376 | rcu_read_release_bh(); |
Paul E. McKenney | bc33f24 | 2009-08-22 13:56:47 -0700 | [diff] [blame] | 377 | __release(RCU_BH); |
| 378 | __rcu_read_unlock_bh(); |
| 379 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | |
| 381 | /** |
Mathieu Desnoyers | 1c50b72 | 2008-09-29 11:06:46 -0400 | [diff] [blame] | 382 | * 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. McKenney | d6714c2 | 2009-08-22 13:56:46 -0700 | [diff] [blame] | 390 | static inline void rcu_read_lock_sched(void) |
| 391 | { |
| 392 | preempt_disable(); |
Paul E. McKenney | bc33f24 | 2009-08-22 13:56:47 -0700 | [diff] [blame] | 393 | __acquire(RCU_SCHED); |
Paul E. McKenney | 632ee20 | 2010-02-22 17:04:45 -0800 | [diff] [blame] | 394 | rcu_read_acquire_sched(); |
Paul E. McKenney | d6714c2 | 2009-08-22 13:56:46 -0700 | [diff] [blame] | 395 | } |
Paul E. McKenney | 1eba8f8 | 2009-09-23 09:50:42 -0700 | [diff] [blame] | 396 | |
| 397 | /* Used by lockdep and tracing: cannot be traced, cannot call lockdep. */ |
Paul E. McKenney | 7c614d6 | 2009-08-24 09:42:00 -0700 | [diff] [blame] | 398 | static inline notrace void rcu_read_lock_sched_notrace(void) |
Paul E. McKenney | d6714c2 | 2009-08-22 13:56:46 -0700 | [diff] [blame] | 399 | { |
| 400 | preempt_disable_notrace(); |
Paul E. McKenney | bc33f24 | 2009-08-22 13:56:47 -0700 | [diff] [blame] | 401 | __acquire(RCU_SCHED); |
Paul E. McKenney | d6714c2 | 2009-08-22 13:56:46 -0700 | [diff] [blame] | 402 | } |
Mathieu Desnoyers | 1c50b72 | 2008-09-29 11:06:46 -0400 | [diff] [blame] | 403 | |
| 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. McKenney | d6714c2 | 2009-08-22 13:56:46 -0700 | [diff] [blame] | 409 | static inline void rcu_read_unlock_sched(void) |
| 410 | { |
Paul E. McKenney | 632ee20 | 2010-02-22 17:04:45 -0800 | [diff] [blame] | 411 | rcu_read_release_sched(); |
Paul E. McKenney | bc33f24 | 2009-08-22 13:56:47 -0700 | [diff] [blame] | 412 | __release(RCU_SCHED); |
Paul E. McKenney | d6714c2 | 2009-08-22 13:56:46 -0700 | [diff] [blame] | 413 | preempt_enable(); |
| 414 | } |
Paul E. McKenney | 1eba8f8 | 2009-09-23 09:50:42 -0700 | [diff] [blame] | 415 | |
| 416 | /* Used by lockdep and tracing: cannot be traced, cannot call lockdep. */ |
Paul E. McKenney | 7c614d6 | 2009-08-24 09:42:00 -0700 | [diff] [blame] | 417 | static inline notrace void rcu_read_unlock_sched_notrace(void) |
Paul E. McKenney | d6714c2 | 2009-08-22 13:56:46 -0700 | [diff] [blame] | 418 | { |
Paul E. McKenney | bc33f24 | 2009-08-22 13:56:47 -0700 | [diff] [blame] | 419 | __release(RCU_SCHED); |
Paul E. McKenney | d6714c2 | 2009-08-22 13:56:46 -0700 | [diff] [blame] | 420 | preempt_enable_notrace(); |
| 421 | } |
Mathieu Desnoyers | 1c50b72 | 2008-09-29 11:06:46 -0400 | [diff] [blame] | 422 | |
| 423 | |
| 424 | /** |
Paul E. McKenney | c26d34a | 2010-02-22 17:04:46 -0800 | [diff] [blame] | 425 | * 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | * |
| 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. McKenney | c26d34a | 2010-02-22 17:04:46 -0800 | [diff] [blame] | 437 | #define rcu_dereference_raw(p) ({ \ |
Paul E. McKenney | 97b4303 | 2007-10-16 23:26:04 -0700 | [diff] [blame] | 438 | typeof(p) _________p1 = ACCESS_ONCE(p); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | smp_read_barrier_depends(); \ |
| 440 | (_________p1); \ |
| 441 | }) |
| 442 | |
| 443 | /** |
Paul E. McKenney | c26d34a | 2010-02-22 17:04:46 -0800 | [diff] [blame] | 444 | * 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | * 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. McKenney | d99c4f6 | 2008-02-06 01:37:25 -0800 | [diff] [blame] | 480 | #define rcu_assign_pointer(p, v) \ |
| 481 | ({ \ |
| 482 | if (!__builtin_constant_p(v) || \ |
| 483 | ((v) != NULL)) \ |
| 484 | smp_wmb(); \ |
| 485 | (p) = (v); \ |
| 486 | }) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | |
Paul E. McKenney | 4446a36 | 2008-05-12 21:21:05 +0200 | [diff] [blame] | 488 | /* Infrastructure to implement the synchronize_() primitives. */ |
| 489 | |
| 490 | struct rcu_synchronize { |
| 491 | struct rcu_head head; |
| 492 | struct completion completion; |
| 493 | }; |
| 494 | |
| 495 | extern void wakeme_after_rcu(struct rcu_head *head); |
| 496 | |
Paul E. McKenney | 9b06e81 | 2005-05-01 08:59:04 -0700 | [diff] [blame] | 497 | /** |
Paul E. McKenney | 01c1c66 | 2008-01-25 21:08:24 +0100 | [diff] [blame] | 498 | * 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 | */ |
| 508 | extern void call_rcu(struct rcu_head *head, |
| 509 | void (*func)(struct rcu_head *head)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | |
Paul E. McKenney | 01c1c66 | 2008-01-25 21:08:24 +0100 | [diff] [blame] | 511 | /** |
| 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 | */ |
| 529 | extern void call_rcu_bh(struct rcu_head *head, |
| 530 | void (*func)(struct rcu_head *head)); |
| 531 | |
Mathieu Desnoyers | 551d55a | 2010-04-17 08:48:42 -0400 | [diff] [blame] | 532 | /* |
| 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 | |
| 542 | extern struct debug_obj_descr rcuhead_debug_descr; |
| 543 | |
| 544 | static 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 | |
| 552 | static 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 */ |
| 560 | static inline void debug_rcu_head_queue(struct rcu_head *head) |
| 561 | { |
| 562 | } |
| 563 | |
| 564 | static inline void debug_rcu_head_unqueue(struct rcu_head *head) |
| 565 | { |
| 566 | } |
| 567 | #endif /* #else !CONFIG_DEBUG_OBJECTS_RCU_HEAD */ |
| 568 | |
Paul E. McKenney | f5155b3 | 2010-04-30 06:42:01 -0700 | [diff] [blame] | 569 | #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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | #endif /* __LINUX_RCUPDATE_H */ |