Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 17 | * |
| 18 | * Copyright (C) IBM Corporation, 2001 |
| 19 | * |
| 20 | * Authors: Dipankar Sarma <dipankar@in.ibm.com> |
| 21 | * Manfred Spraul <manfred@colorfullife.com> |
| 22 | * |
| 23 | * Based on the original work by Paul McKenney <paulmck@us.ibm.com> |
| 24 | * and inputs from Rusty Russell, Andrea Arcangeli and Andi Kleen. |
| 25 | * Papers: |
| 26 | * http://www.rdrop.com/users/paulmck/paper/rclockpdcsproof.pdf |
| 27 | * http://lse.sourceforge.net/locking/rclock_OLS.2001.05.01c.sc.pdf (OLS2001) |
| 28 | * |
| 29 | * For detailed explanation of Read-Copy Update mechanism see - |
| 30 | * http://lse.sourceforge.net/locking/rcupdate.html |
| 31 | * |
| 32 | */ |
| 33 | #include <linux/types.h> |
| 34 | #include <linux/kernel.h> |
| 35 | #include <linux/init.h> |
| 36 | #include <linux/spinlock.h> |
| 37 | #include <linux/smp.h> |
Ingo Molnar | e56d090 | 2006-01-08 01:01:37 -0800 | [diff] [blame] | 38 | #include <linux/rcupdate.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | #include <linux/interrupt.h> |
| 40 | #include <linux/sched.h> |
| 41 | #include <asm/atomic.h> |
| 42 | #include <linux/bitops.h> |
| 43 | #include <linux/module.h> |
| 44 | #include <linux/completion.h> |
| 45 | #include <linux/moduleparam.h> |
| 46 | #include <linux/percpu.h> |
| 47 | #include <linux/notifier.h> |
| 48 | #include <linux/rcupdate.h> |
| 49 | #include <linux/cpu.h> |
Ingo Molnar | 9331b31 | 2006-03-23 03:00:19 -0800 | [diff] [blame] | 50 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | |
Peter Zijlstra | 851a67b | 2007-10-11 22:11:12 +0200 | [diff] [blame^] | 52 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
| 53 | static struct lock_class_key rcu_lock_key; |
| 54 | struct lockdep_map rcu_lock_map = |
| 55 | STATIC_LOCKDEP_MAP_INIT("rcu_read_lock", &rcu_lock_key); |
| 56 | |
| 57 | EXPORT_SYMBOL_GPL(rcu_lock_map); |
| 58 | #endif |
| 59 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | /* Definition for rcupdate control block. */ |
Adrian Bunk | 2178426 | 2006-03-23 03:01:00 -0800 | [diff] [blame] | 61 | static struct rcu_ctrlblk rcu_ctrlblk = { |
Oleg Nesterov | 69a0b31 | 2006-01-10 16:48:02 +0300 | [diff] [blame] | 62 | .cur = -300, |
| 63 | .completed = -300, |
Ingo Molnar | e4d9191 | 2006-07-03 00:24:34 -0700 | [diff] [blame] | 64 | .lock = __SPIN_LOCK_UNLOCKED(&rcu_ctrlblk.lock), |
Oleg Nesterov | 69a0b31 | 2006-01-10 16:48:02 +0300 | [diff] [blame] | 65 | .cpumask = CPU_MASK_NONE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | }; |
Adrian Bunk | 2178426 | 2006-03-23 03:01:00 -0800 | [diff] [blame] | 67 | static struct rcu_ctrlblk rcu_bh_ctrlblk = { |
Oleg Nesterov | 69a0b31 | 2006-01-10 16:48:02 +0300 | [diff] [blame] | 68 | .cur = -300, |
| 69 | .completed = -300, |
Ingo Molnar | e4d9191 | 2006-07-03 00:24:34 -0700 | [diff] [blame] | 70 | .lock = __SPIN_LOCK_UNLOCKED(&rcu_bh_ctrlblk.lock), |
Oleg Nesterov | 69a0b31 | 2006-01-10 16:48:02 +0300 | [diff] [blame] | 71 | .cpumask = CPU_MASK_NONE, |
| 72 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | |
| 74 | DEFINE_PER_CPU(struct rcu_data, rcu_data) = { 0L }; |
| 75 | DEFINE_PER_CPU(struct rcu_data, rcu_bh_data) = { 0L }; |
| 76 | |
| 77 | /* Fake initialization required by compiler */ |
| 78 | static DEFINE_PER_CPU(struct tasklet_struct, rcu_tasklet) = {NULL}; |
Dipankar Sarma | 21a1ea9 | 2006-03-07 21:55:33 -0800 | [diff] [blame] | 79 | static int blimit = 10; |
| 80 | static int qhimark = 10000; |
| 81 | static int qlowmark = 100; |
Dipankar Sarma | 21a1ea9 | 2006-03-07 21:55:33 -0800 | [diff] [blame] | 82 | |
| 83 | static atomic_t rcu_barrier_cpu_count; |
Ingo Molnar | 9331b31 | 2006-03-23 03:00:19 -0800 | [diff] [blame] | 84 | static DEFINE_MUTEX(rcu_barrier_mutex); |
Dipankar Sarma | 21a1ea9 | 2006-03-07 21:55:33 -0800 | [diff] [blame] | 85 | static struct completion rcu_barrier_completion; |
| 86 | |
| 87 | #ifdef CONFIG_SMP |
| 88 | static void force_quiescent_state(struct rcu_data *rdp, |
| 89 | struct rcu_ctrlblk *rcp) |
| 90 | { |
| 91 | int cpu; |
| 92 | cpumask_t cpumask; |
| 93 | set_need_resched(); |
Oleg Nesterov | 20e9751 | 2006-10-04 02:17:17 -0700 | [diff] [blame] | 94 | if (unlikely(!rcp->signaled)) { |
| 95 | rcp->signaled = 1; |
Dipankar Sarma | 21a1ea9 | 2006-03-07 21:55:33 -0800 | [diff] [blame] | 96 | /* |
| 97 | * Don't send IPI to itself. With irqs disabled, |
| 98 | * rdp->cpu is the current cpu. |
| 99 | */ |
| 100 | cpumask = rcp->cpumask; |
| 101 | cpu_clear(rdp->cpu, cpumask); |
| 102 | for_each_cpu_mask(cpu, cpumask) |
| 103 | smp_send_reschedule(cpu); |
| 104 | } |
| 105 | } |
| 106 | #else |
| 107 | static inline void force_quiescent_state(struct rcu_data *rdp, |
| 108 | struct rcu_ctrlblk *rcp) |
| 109 | { |
| 110 | set_need_resched(); |
| 111 | } |
| 112 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | |
| 114 | /** |
| 115 | * call_rcu - Queue an RCU callback for invocation after a grace period. |
| 116 | * @head: structure to be used for queueing the RCU updates. |
| 117 | * @func: actual update function to be invoked after the grace period |
| 118 | * |
| 119 | * The update function will be invoked some time after a full grace |
| 120 | * period elapses, in other words after all currently executing RCU |
| 121 | * read-side critical sections have completed. RCU read-side critical |
| 122 | * sections are delimited by rcu_read_lock() and rcu_read_unlock(), |
| 123 | * and may be nested. |
| 124 | */ |
| 125 | void fastcall call_rcu(struct rcu_head *head, |
| 126 | void (*func)(struct rcu_head *rcu)) |
| 127 | { |
| 128 | unsigned long flags; |
| 129 | struct rcu_data *rdp; |
| 130 | |
| 131 | head->func = func; |
| 132 | head->next = NULL; |
| 133 | local_irq_save(flags); |
| 134 | rdp = &__get_cpu_var(rcu_data); |
| 135 | *rdp->nxttail = head; |
| 136 | rdp->nxttail = &head->next; |
Dipankar Sarma | 21a1ea9 | 2006-03-07 21:55:33 -0800 | [diff] [blame] | 137 | if (unlikely(++rdp->qlen > qhimark)) { |
| 138 | rdp->blimit = INT_MAX; |
| 139 | force_quiescent_state(rdp, &rcu_ctrlblk); |
| 140 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | local_irq_restore(flags); |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * call_rcu_bh - Queue an RCU for invocation after a quicker grace period. |
| 146 | * @head: structure to be used for queueing the RCU updates. |
| 147 | * @func: actual update function to be invoked after the grace period |
| 148 | * |
| 149 | * The update function will be invoked some time after a full grace |
| 150 | * period elapses, in other words after all currently executing RCU |
| 151 | * read-side critical sections have completed. call_rcu_bh() assumes |
| 152 | * that the read-side critical sections end on completion of a softirq |
| 153 | * handler. This means that read-side critical sections in process |
| 154 | * context must not be interrupted by softirqs. This interface is to be |
| 155 | * used when most of the read-side critical sections are in softirq context. |
| 156 | * RCU read-side critical sections are delimited by rcu_read_lock() and |
| 157 | * rcu_read_unlock(), * if in interrupt context or rcu_read_lock_bh() |
| 158 | * and rcu_read_unlock_bh(), if in process context. These may be nested. |
| 159 | */ |
| 160 | void fastcall call_rcu_bh(struct rcu_head *head, |
| 161 | void (*func)(struct rcu_head *rcu)) |
| 162 | { |
| 163 | unsigned long flags; |
| 164 | struct rcu_data *rdp; |
| 165 | |
| 166 | head->func = func; |
| 167 | head->next = NULL; |
| 168 | local_irq_save(flags); |
| 169 | rdp = &__get_cpu_var(rcu_bh_data); |
| 170 | *rdp->nxttail = head; |
| 171 | rdp->nxttail = &head->next; |
Dipankar Sarma | 21a1ea9 | 2006-03-07 21:55:33 -0800 | [diff] [blame] | 172 | |
| 173 | if (unlikely(++rdp->qlen > qhimark)) { |
| 174 | rdp->blimit = INT_MAX; |
| 175 | force_quiescent_state(rdp, &rcu_bh_ctrlblk); |
| 176 | } |
| 177 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | local_irq_restore(flags); |
| 179 | } |
| 180 | |
| 181 | /* |
Paul E. McKenney | a241ec6 | 2005-10-30 15:03:12 -0800 | [diff] [blame] | 182 | * Return the number of RCU batches processed thus far. Useful |
| 183 | * for debug and statistics. |
| 184 | */ |
| 185 | long rcu_batches_completed(void) |
| 186 | { |
| 187 | return rcu_ctrlblk.completed; |
| 188 | } |
| 189 | |
Paul E. McKenney | c32e066 | 2006-06-27 02:54:04 -0700 | [diff] [blame] | 190 | /* |
| 191 | * Return the number of RCU batches processed thus far. Useful |
| 192 | * for debug and statistics. |
| 193 | */ |
| 194 | long rcu_batches_completed_bh(void) |
| 195 | { |
| 196 | return rcu_bh_ctrlblk.completed; |
| 197 | } |
| 198 | |
Dipankar Sarma | ab4720e | 2005-12-12 00:37:05 -0800 | [diff] [blame] | 199 | static void rcu_barrier_callback(struct rcu_head *notused) |
| 200 | { |
| 201 | if (atomic_dec_and_test(&rcu_barrier_cpu_count)) |
| 202 | complete(&rcu_barrier_completion); |
| 203 | } |
| 204 | |
| 205 | /* |
| 206 | * Called with preemption disabled, and from cross-cpu IRQ context. |
| 207 | */ |
| 208 | static void rcu_barrier_func(void *notused) |
| 209 | { |
| 210 | int cpu = smp_processor_id(); |
| 211 | struct rcu_data *rdp = &per_cpu(rcu_data, cpu); |
| 212 | struct rcu_head *head; |
| 213 | |
| 214 | head = &rdp->barrier; |
| 215 | atomic_inc(&rcu_barrier_cpu_count); |
| 216 | call_rcu(head, rcu_barrier_callback); |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * rcu_barrier - Wait until all the in-flight RCUs are complete. |
| 221 | */ |
| 222 | void rcu_barrier(void) |
| 223 | { |
| 224 | BUG_ON(in_interrupt()); |
Ingo Molnar | 9331b31 | 2006-03-23 03:00:19 -0800 | [diff] [blame] | 225 | /* Take cpucontrol mutex to protect against CPU hotplug */ |
| 226 | mutex_lock(&rcu_barrier_mutex); |
Dipankar Sarma | ab4720e | 2005-12-12 00:37:05 -0800 | [diff] [blame] | 227 | init_completion(&rcu_barrier_completion); |
| 228 | atomic_set(&rcu_barrier_cpu_count, 0); |
| 229 | on_each_cpu(rcu_barrier_func, NULL, 0, 1); |
| 230 | wait_for_completion(&rcu_barrier_completion); |
Ingo Molnar | 9331b31 | 2006-03-23 03:00:19 -0800 | [diff] [blame] | 231 | mutex_unlock(&rcu_barrier_mutex); |
Dipankar Sarma | ab4720e | 2005-12-12 00:37:05 -0800 | [diff] [blame] | 232 | } |
| 233 | EXPORT_SYMBOL_GPL(rcu_barrier); |
| 234 | |
Paul E. McKenney | a241ec6 | 2005-10-30 15:03:12 -0800 | [diff] [blame] | 235 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | * Invoke the completed RCU callbacks. They are expected to be in |
| 237 | * a per-cpu list. |
| 238 | */ |
| 239 | static void rcu_do_batch(struct rcu_data *rdp) |
| 240 | { |
| 241 | struct rcu_head *next, *list; |
| 242 | int count = 0; |
| 243 | |
| 244 | list = rdp->donelist; |
| 245 | while (list) { |
Eric Dumazet | 1c69d92 | 2006-12-06 20:38:44 -0800 | [diff] [blame] | 246 | next = list->next; |
| 247 | prefetch(next); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | list->func(list); |
| 249 | list = next; |
Dipankar Sarma | 21a1ea9 | 2006-03-07 21:55:33 -0800 | [diff] [blame] | 250 | if (++count >= rdp->blimit) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | break; |
| 252 | } |
Eric Dumazet | 1c69d92 | 2006-12-06 20:38:44 -0800 | [diff] [blame] | 253 | rdp->donelist = list; |
Oleg Nesterov | dd9daa2 | 2006-09-12 20:35:55 -0700 | [diff] [blame] | 254 | |
| 255 | local_irq_disable(); |
| 256 | rdp->qlen -= count; |
| 257 | local_irq_enable(); |
Dipankar Sarma | 21a1ea9 | 2006-03-07 21:55:33 -0800 | [diff] [blame] | 258 | if (rdp->blimit == INT_MAX && rdp->qlen <= qlowmark) |
| 259 | rdp->blimit = blimit; |
Oleg Nesterov | dd9daa2 | 2006-09-12 20:35:55 -0700 | [diff] [blame] | 260 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | if (!rdp->donelist) |
| 262 | rdp->donetail = &rdp->donelist; |
| 263 | else |
| 264 | tasklet_schedule(&per_cpu(rcu_tasklet, rdp->cpu)); |
| 265 | } |
| 266 | |
| 267 | /* |
| 268 | * Grace period handling: |
| 269 | * The grace period handling consists out of two steps: |
| 270 | * - A new grace period is started. |
| 271 | * This is done by rcu_start_batch. The start is not broadcasted to |
| 272 | * all cpus, they must pick this up by comparing rcp->cur with |
| 273 | * rdp->quiescbatch. All cpus are recorded in the |
Oleg Nesterov | 69a0b31 | 2006-01-10 16:48:02 +0300 | [diff] [blame] | 274 | * rcu_ctrlblk.cpumask bitmap. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | * - All cpus must go through a quiescent state. |
| 276 | * Since the start of the grace period is not broadcasted, at least two |
| 277 | * calls to rcu_check_quiescent_state are required: |
| 278 | * The first call just notices that a new grace period is running. The |
| 279 | * following calls check if there was a quiescent state since the beginning |
Oleg Nesterov | 69a0b31 | 2006-01-10 16:48:02 +0300 | [diff] [blame] | 280 | * of the grace period. If so, it updates rcu_ctrlblk.cpumask. If |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | * the bitmap is empty, then the grace period is completed. |
| 282 | * rcu_check_quiescent_state calls rcu_start_batch(0) to start the next grace |
| 283 | * period (if necessary). |
| 284 | */ |
| 285 | /* |
| 286 | * Register a new batch of callbacks, and start it up if there is currently no |
| 287 | * active batch and the batch to be registered has not already occurred. |
Oleg Nesterov | 69a0b31 | 2006-01-10 16:48:02 +0300 | [diff] [blame] | 288 | * Caller must hold rcu_ctrlblk.lock. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | */ |
Oleg Nesterov | 69a0b31 | 2006-01-10 16:48:02 +0300 | [diff] [blame] | 290 | static void rcu_start_batch(struct rcu_ctrlblk *rcp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | if (rcp->next_pending && |
| 293 | rcp->completed == rcp->cur) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | rcp->next_pending = 0; |
Srivatsa Vaddagiri | c3f5902 | 2005-12-12 00:37:07 -0800 | [diff] [blame] | 295 | /* |
| 296 | * next_pending == 0 must be visible in |
| 297 | * __rcu_process_callbacks() before it can see new value of cur. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | */ |
| 299 | smp_wmb(); |
| 300 | rcp->cur++; |
Srivatsa Vaddagiri | c3f5902 | 2005-12-12 00:37:07 -0800 | [diff] [blame] | 301 | |
| 302 | /* |
| 303 | * Accessing nohz_cpu_mask before incrementing rcp->cur needs a |
| 304 | * Barrier Otherwise it can cause tickless idle CPUs to be |
Oleg Nesterov | 69a0b31 | 2006-01-10 16:48:02 +0300 | [diff] [blame] | 305 | * included in rcp->cpumask, which will extend graceperiods |
Srivatsa Vaddagiri | c3f5902 | 2005-12-12 00:37:07 -0800 | [diff] [blame] | 306 | * unnecessarily. |
| 307 | */ |
| 308 | smp_mb(); |
Oleg Nesterov | 69a0b31 | 2006-01-10 16:48:02 +0300 | [diff] [blame] | 309 | cpus_andnot(rcp->cpumask, cpu_online_map, nohz_cpu_mask); |
Srivatsa Vaddagiri | c3f5902 | 2005-12-12 00:37:07 -0800 | [diff] [blame] | 310 | |
Oleg Nesterov | 20e9751 | 2006-10-04 02:17:17 -0700 | [diff] [blame] | 311 | rcp->signaled = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | } |
| 313 | } |
| 314 | |
| 315 | /* |
| 316 | * cpu went through a quiescent state since the beginning of the grace period. |
| 317 | * Clear it from the cpu mask and complete the grace period if it was the last |
| 318 | * cpu. Start another grace period if someone has further entries pending |
| 319 | */ |
Oleg Nesterov | 69a0b31 | 2006-01-10 16:48:02 +0300 | [diff] [blame] | 320 | static void cpu_quiet(int cpu, struct rcu_ctrlblk *rcp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | { |
Oleg Nesterov | 69a0b31 | 2006-01-10 16:48:02 +0300 | [diff] [blame] | 322 | cpu_clear(cpu, rcp->cpumask); |
| 323 | if (cpus_empty(rcp->cpumask)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | /* batch completed ! */ |
| 325 | rcp->completed = rcp->cur; |
Oleg Nesterov | 69a0b31 | 2006-01-10 16:48:02 +0300 | [diff] [blame] | 326 | rcu_start_batch(rcp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | } |
| 328 | } |
| 329 | |
| 330 | /* |
| 331 | * Check if the cpu has gone through a quiescent state (say context |
| 332 | * switch). If so and if it already hasn't done so in this RCU |
| 333 | * quiescent cycle, then indicate that it has done so. |
| 334 | */ |
| 335 | static void rcu_check_quiescent_state(struct rcu_ctrlblk *rcp, |
Oleg Nesterov | 69a0b31 | 2006-01-10 16:48:02 +0300 | [diff] [blame] | 336 | struct rcu_data *rdp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | { |
| 338 | if (rdp->quiescbatch != rcp->cur) { |
| 339 | /* start new grace period: */ |
| 340 | rdp->qs_pending = 1; |
| 341 | rdp->passed_quiesc = 0; |
| 342 | rdp->quiescbatch = rcp->cur; |
| 343 | return; |
| 344 | } |
| 345 | |
| 346 | /* Grace period already completed for this cpu? |
| 347 | * qs_pending is checked instead of the actual bitmap to avoid |
| 348 | * cacheline trashing. |
| 349 | */ |
| 350 | if (!rdp->qs_pending) |
| 351 | return; |
| 352 | |
| 353 | /* |
| 354 | * Was there a quiescent state since the beginning of the grace |
| 355 | * period? If no, then exit and wait for the next call. |
| 356 | */ |
| 357 | if (!rdp->passed_quiesc) |
| 358 | return; |
| 359 | rdp->qs_pending = 0; |
| 360 | |
Oleg Nesterov | 69a0b31 | 2006-01-10 16:48:02 +0300 | [diff] [blame] | 361 | spin_lock(&rcp->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | /* |
| 363 | * rdp->quiescbatch/rcp->cur and the cpu bitmap can come out of sync |
| 364 | * during cpu startup. Ignore the quiescent state. |
| 365 | */ |
| 366 | if (likely(rdp->quiescbatch == rcp->cur)) |
Oleg Nesterov | 69a0b31 | 2006-01-10 16:48:02 +0300 | [diff] [blame] | 367 | cpu_quiet(rdp->cpu, rcp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | |
Oleg Nesterov | 69a0b31 | 2006-01-10 16:48:02 +0300 | [diff] [blame] | 369 | spin_unlock(&rcp->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | |
| 373 | #ifdef CONFIG_HOTPLUG_CPU |
| 374 | |
| 375 | /* warning! helper for rcu_offline_cpu. do not use elsewhere without reviewing |
| 376 | * locking requirements, the list it's pulling from has to belong to a cpu |
| 377 | * which is dead and hence not processing interrupts. |
| 378 | */ |
| 379 | static void rcu_move_batch(struct rcu_data *this_rdp, struct rcu_head *list, |
| 380 | struct rcu_head **tail) |
| 381 | { |
| 382 | local_irq_disable(); |
| 383 | *this_rdp->nxttail = list; |
| 384 | if (list) |
| 385 | this_rdp->nxttail = tail; |
| 386 | local_irq_enable(); |
| 387 | } |
| 388 | |
| 389 | static void __rcu_offline_cpu(struct rcu_data *this_rdp, |
Oleg Nesterov | 69a0b31 | 2006-01-10 16:48:02 +0300 | [diff] [blame] | 390 | struct rcu_ctrlblk *rcp, struct rcu_data *rdp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | { |
| 392 | /* if the cpu going offline owns the grace period |
| 393 | * we can block indefinitely waiting for it, so flush |
| 394 | * it here |
| 395 | */ |
Oleg Nesterov | 69a0b31 | 2006-01-10 16:48:02 +0300 | [diff] [blame] | 396 | spin_lock_bh(&rcp->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | if (rcp->cur != rcp->completed) |
Oleg Nesterov | 69a0b31 | 2006-01-10 16:48:02 +0300 | [diff] [blame] | 398 | cpu_quiet(rdp->cpu, rcp); |
| 399 | spin_unlock_bh(&rcp->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | rcu_move_batch(this_rdp, rdp->curlist, rdp->curtail); |
| 401 | rcu_move_batch(this_rdp, rdp->nxtlist, rdp->nxttail); |
Oleg Nesterov | a9c8281 | 2006-01-10 17:24:53 +0300 | [diff] [blame] | 402 | rcu_move_batch(this_rdp, rdp->donelist, rdp->donetail); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | } |
Oleg Nesterov | a9c8281 | 2006-01-10 17:24:53 +0300 | [diff] [blame] | 404 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | static void rcu_offline_cpu(int cpu) |
| 406 | { |
| 407 | struct rcu_data *this_rdp = &get_cpu_var(rcu_data); |
| 408 | struct rcu_data *this_bh_rdp = &get_cpu_var(rcu_bh_data); |
| 409 | |
Oleg Nesterov | 69a0b31 | 2006-01-10 16:48:02 +0300 | [diff] [blame] | 410 | __rcu_offline_cpu(this_rdp, &rcu_ctrlblk, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | &per_cpu(rcu_data, cpu)); |
Oleg Nesterov | 69a0b31 | 2006-01-10 16:48:02 +0300 | [diff] [blame] | 412 | __rcu_offline_cpu(this_bh_rdp, &rcu_bh_ctrlblk, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | &per_cpu(rcu_bh_data, cpu)); |
| 414 | put_cpu_var(rcu_data); |
| 415 | put_cpu_var(rcu_bh_data); |
| 416 | tasklet_kill_immediate(&per_cpu(rcu_tasklet, cpu), cpu); |
| 417 | } |
| 418 | |
| 419 | #else |
| 420 | |
| 421 | static void rcu_offline_cpu(int cpu) |
| 422 | { |
| 423 | } |
| 424 | |
| 425 | #endif |
| 426 | |
| 427 | /* |
| 428 | * This does the RCU processing work from tasklet context. |
| 429 | */ |
| 430 | static void __rcu_process_callbacks(struct rcu_ctrlblk *rcp, |
Oleg Nesterov | 69a0b31 | 2006-01-10 16:48:02 +0300 | [diff] [blame] | 431 | struct rcu_data *rdp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | { |
| 433 | if (rdp->curlist && !rcu_batch_before(rcp->completed, rdp->batch)) { |
| 434 | *rdp->donetail = rdp->curlist; |
| 435 | rdp->donetail = rdp->curtail; |
| 436 | rdp->curlist = NULL; |
| 437 | rdp->curtail = &rdp->curlist; |
| 438 | } |
| 439 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | if (rdp->nxtlist && !rdp->curlist) { |
Oleg Nesterov | caa9ee7 | 2006-03-24 03:15:50 -0800 | [diff] [blame] | 441 | local_irq_disable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | rdp->curlist = rdp->nxtlist; |
| 443 | rdp->curtail = rdp->nxttail; |
| 444 | rdp->nxtlist = NULL; |
| 445 | rdp->nxttail = &rdp->nxtlist; |
| 446 | local_irq_enable(); |
| 447 | |
| 448 | /* |
| 449 | * start the next batch of callbacks |
| 450 | */ |
| 451 | |
| 452 | /* determine batch number */ |
| 453 | rdp->batch = rcp->cur + 1; |
| 454 | /* see the comment and corresponding wmb() in |
| 455 | * the rcu_start_batch() |
| 456 | */ |
| 457 | smp_rmb(); |
| 458 | |
| 459 | if (!rcp->next_pending) { |
| 460 | /* and start it/schedule start if it's a new batch */ |
Oleg Nesterov | 69a0b31 | 2006-01-10 16:48:02 +0300 | [diff] [blame] | 461 | spin_lock(&rcp->lock); |
Oleg Nesterov | dbc1651 | 2006-01-08 22:19:33 +0300 | [diff] [blame] | 462 | rcp->next_pending = 1; |
Oleg Nesterov | 69a0b31 | 2006-01-10 16:48:02 +0300 | [diff] [blame] | 463 | rcu_start_batch(rcp); |
| 464 | spin_unlock(&rcp->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | } |
Oleg Nesterov | caa9ee7 | 2006-03-24 03:15:50 -0800 | [diff] [blame] | 467 | |
Oleg Nesterov | 69a0b31 | 2006-01-10 16:48:02 +0300 | [diff] [blame] | 468 | rcu_check_quiescent_state(rcp, rdp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | if (rdp->donelist) |
| 470 | rcu_do_batch(rdp); |
| 471 | } |
| 472 | |
| 473 | static void rcu_process_callbacks(unsigned long unused) |
| 474 | { |
Oleg Nesterov | 69a0b31 | 2006-01-10 16:48:02 +0300 | [diff] [blame] | 475 | __rcu_process_callbacks(&rcu_ctrlblk, &__get_cpu_var(rcu_data)); |
| 476 | __rcu_process_callbacks(&rcu_bh_ctrlblk, &__get_cpu_var(rcu_bh_data)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | } |
| 478 | |
Oleg Nesterov | 6775177 | 2006-01-08 22:19:16 +0300 | [diff] [blame] | 479 | static int __rcu_pending(struct rcu_ctrlblk *rcp, struct rcu_data *rdp) |
| 480 | { |
| 481 | /* This cpu has pending rcu entries and the grace period |
| 482 | * for them has completed. |
| 483 | */ |
| 484 | if (rdp->curlist && !rcu_batch_before(rcp->completed, rdp->batch)) |
| 485 | return 1; |
| 486 | |
| 487 | /* This cpu has no pending entries, but there are new entries */ |
| 488 | if (!rdp->curlist && rdp->nxtlist) |
| 489 | return 1; |
| 490 | |
| 491 | /* This cpu has finished callbacks to invoke */ |
| 492 | if (rdp->donelist) |
| 493 | return 1; |
| 494 | |
| 495 | /* The rcu core waits for a quiescent state from the cpu */ |
| 496 | if (rdp->quiescbatch != rcp->cur || rdp->qs_pending) |
| 497 | return 1; |
| 498 | |
| 499 | /* nothing to do */ |
| 500 | return 0; |
| 501 | } |
| 502 | |
Heiko Carstens | 986733e | 2006-05-15 09:43:58 -0700 | [diff] [blame] | 503 | /* |
| 504 | * Check to see if there is any immediate RCU-related work to be done |
| 505 | * by the current CPU, returning 1 if so. This function is part of the |
| 506 | * RCU implementation; it is -not- an exported member of the RCU API. |
| 507 | */ |
Oleg Nesterov | 6775177 | 2006-01-08 22:19:16 +0300 | [diff] [blame] | 508 | int rcu_pending(int cpu) |
| 509 | { |
| 510 | return __rcu_pending(&rcu_ctrlblk, &per_cpu(rcu_data, cpu)) || |
| 511 | __rcu_pending(&rcu_bh_ctrlblk, &per_cpu(rcu_bh_data, cpu)); |
| 512 | } |
| 513 | |
Heiko Carstens | 986733e | 2006-05-15 09:43:58 -0700 | [diff] [blame] | 514 | /* |
| 515 | * Check to see if any future RCU-related work will need to be done |
| 516 | * by the current CPU, even if none need be done immediately, returning |
| 517 | * 1 if so. This function is part of the RCU implementation; it is -not- |
| 518 | * an exported member of the RCU API. |
| 519 | */ |
| 520 | int rcu_needs_cpu(int cpu) |
| 521 | { |
| 522 | struct rcu_data *rdp = &per_cpu(rcu_data, cpu); |
| 523 | struct rcu_data *rdp_bh = &per_cpu(rcu_bh_data, cpu); |
| 524 | |
| 525 | return (!!rdp->curlist || !!rdp_bh->curlist || rcu_pending(cpu)); |
| 526 | } |
| 527 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | void rcu_check_callbacks(int cpu, int user) |
| 529 | { |
| 530 | if (user || |
| 531 | (idle_cpu(cpu) && !in_softirq() && |
| 532 | hardirq_count() <= (1 << HARDIRQ_SHIFT))) { |
| 533 | rcu_qsctr_inc(cpu); |
| 534 | rcu_bh_qsctr_inc(cpu); |
| 535 | } else if (!in_softirq()) |
| 536 | rcu_bh_qsctr_inc(cpu); |
| 537 | tasklet_schedule(&per_cpu(rcu_tasklet, cpu)); |
| 538 | } |
| 539 | |
| 540 | static void rcu_init_percpu_data(int cpu, struct rcu_ctrlblk *rcp, |
| 541 | struct rcu_data *rdp) |
| 542 | { |
| 543 | memset(rdp, 0, sizeof(*rdp)); |
| 544 | rdp->curtail = &rdp->curlist; |
| 545 | rdp->nxttail = &rdp->nxtlist; |
| 546 | rdp->donetail = &rdp->donelist; |
| 547 | rdp->quiescbatch = rcp->completed; |
| 548 | rdp->qs_pending = 0; |
| 549 | rdp->cpu = cpu; |
Dipankar Sarma | 21a1ea9 | 2006-03-07 21:55:33 -0800 | [diff] [blame] | 550 | rdp->blimit = blimit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | } |
| 552 | |
| 553 | static void __devinit rcu_online_cpu(int cpu) |
| 554 | { |
| 555 | struct rcu_data *rdp = &per_cpu(rcu_data, cpu); |
| 556 | struct rcu_data *bh_rdp = &per_cpu(rcu_bh_data, cpu); |
| 557 | |
| 558 | rcu_init_percpu_data(cpu, &rcu_ctrlblk, rdp); |
| 559 | rcu_init_percpu_data(cpu, &rcu_bh_ctrlblk, bh_rdp); |
| 560 | tasklet_init(&per_cpu(rcu_tasklet, cpu), rcu_process_callbacks, 0UL); |
| 561 | } |
| 562 | |
Chandra Seetharaman | 8c78f30 | 2006-07-30 03:03:35 -0700 | [diff] [blame] | 563 | static int __cpuinit rcu_cpu_notify(struct notifier_block *self, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | unsigned long action, void *hcpu) |
| 565 | { |
| 566 | long cpu = (long)hcpu; |
| 567 | switch (action) { |
| 568 | case CPU_UP_PREPARE: |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 569 | case CPU_UP_PREPARE_FROZEN: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | rcu_online_cpu(cpu); |
| 571 | break; |
| 572 | case CPU_DEAD: |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 573 | case CPU_DEAD_FROZEN: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | rcu_offline_cpu(cpu); |
| 575 | break; |
| 576 | default: |
| 577 | break; |
| 578 | } |
| 579 | return NOTIFY_OK; |
| 580 | } |
| 581 | |
Chandra Seetharaman | 8c78f30 | 2006-07-30 03:03:35 -0700 | [diff] [blame] | 582 | static struct notifier_block __cpuinitdata rcu_nb = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 583 | .notifier_call = rcu_cpu_notify, |
| 584 | }; |
| 585 | |
| 586 | /* |
| 587 | * Initializes rcu mechanism. Assumed to be called early. |
| 588 | * That is before local timer(SMP) or jiffie timer (uniproc) is setup. |
| 589 | * Note that rcu_qsctr and friends are implicitly |
| 590 | * initialized due to the choice of ``0'' for RCU_CTR_INVALID. |
| 591 | */ |
| 592 | void __init rcu_init(void) |
| 593 | { |
| 594 | rcu_cpu_notify(&rcu_nb, CPU_UP_PREPARE, |
| 595 | (void *)(long)smp_processor_id()); |
| 596 | /* Register notifier for non-boot CPUs */ |
| 597 | register_cpu_notifier(&rcu_nb); |
| 598 | } |
| 599 | |
| 600 | struct rcu_synchronize { |
| 601 | struct rcu_head head; |
| 602 | struct completion completion; |
| 603 | }; |
| 604 | |
| 605 | /* Because of FASTCALL declaration of complete, we use this wrapper */ |
| 606 | static void wakeme_after_rcu(struct rcu_head *head) |
| 607 | { |
| 608 | struct rcu_synchronize *rcu; |
| 609 | |
| 610 | rcu = container_of(head, struct rcu_synchronize, head); |
| 611 | complete(&rcu->completion); |
| 612 | } |
| 613 | |
| 614 | /** |
Paul E. McKenney | 9b06e81 | 2005-05-01 08:59:04 -0700 | [diff] [blame] | 615 | * synchronize_rcu - wait until a grace period has elapsed. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 | * |
| 617 | * Control will return to the caller some time after a full grace |
| 618 | * period has elapsed, in other words after all currently executing RCU |
| 619 | * read-side critical sections have completed. RCU read-side critical |
| 620 | * sections are delimited by rcu_read_lock() and rcu_read_unlock(), |
| 621 | * and may be nested. |
Paul E. McKenney | 9b06e81 | 2005-05-01 08:59:04 -0700 | [diff] [blame] | 622 | * |
| 623 | * If your read-side code is not protected by rcu_read_lock(), do -not- |
| 624 | * use synchronize_rcu(). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | */ |
Paul E. McKenney | 9b06e81 | 2005-05-01 08:59:04 -0700 | [diff] [blame] | 626 | void synchronize_rcu(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | { |
| 628 | struct rcu_synchronize rcu; |
| 629 | |
| 630 | init_completion(&rcu.completion); |
| 631 | /* Will wake me after RCU finished */ |
| 632 | call_rcu(&rcu.head, wakeme_after_rcu); |
| 633 | |
| 634 | /* Wait for it */ |
| 635 | wait_for_completion(&rcu.completion); |
| 636 | } |
| 637 | |
Dipankar Sarma | 21a1ea9 | 2006-03-07 21:55:33 -0800 | [diff] [blame] | 638 | module_param(blimit, int, 0); |
| 639 | module_param(qhimark, int, 0); |
| 640 | module_param(qlowmark, int, 0); |
Paul E. McKenney | a241ec6 | 2005-10-30 15:03:12 -0800 | [diff] [blame] | 641 | EXPORT_SYMBOL_GPL(rcu_batches_completed); |
Paul E. McKenney | c32e066 | 2006-06-27 02:54:04 -0700 | [diff] [blame] | 642 | EXPORT_SYMBOL_GPL(rcu_batches_completed_bh); |
Paul E. McKenney | d83015b | 2006-06-23 02:05:51 -0700 | [diff] [blame] | 643 | EXPORT_SYMBOL_GPL(call_rcu); |
| 644 | EXPORT_SYMBOL_GPL(call_rcu_bh); |
Paul E. McKenney | 9b06e81 | 2005-05-01 08:59:04 -0700 | [diff] [blame] | 645 | EXPORT_SYMBOL_GPL(synchronize_rcu); |