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