Paul E. McKenney | 01c1c66 | 2008-01-25 21:08:24 +0100 | [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 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 | * Documentation/RCU |
| 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> |
| 38 | #include <linux/rcupdate.h> |
| 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> |
Paul E. McKenney | 01c1c66 | 2008-01-25 21:08:24 +0100 | [diff] [blame] | 48 | #include <linux/cpu.h> |
| 49 | #include <linux/mutex.h> |
Paul E. McKenney | 67182ae | 2008-08-10 18:35:38 -0700 | [diff] [blame^] | 50 | #include <linux/time.h> |
Paul E. McKenney | 01c1c66 | 2008-01-25 21:08:24 +0100 | [diff] [blame] | 51 | |
| 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 | EXPORT_SYMBOL_GPL(rcu_lock_map); |
| 57 | #endif |
| 58 | |
| 59 | |
| 60 | /* Definition for rcupdate control block. */ |
| 61 | static struct rcu_ctrlblk rcu_ctrlblk = { |
| 62 | .cur = -300, |
| 63 | .completed = -300, |
Lai Jiangshan | 3cac97c | 2008-07-06 17:23:55 +0800 | [diff] [blame] | 64 | .pending = -300, |
Paul E. McKenney | 01c1c66 | 2008-01-25 21:08:24 +0100 | [diff] [blame] | 65 | .lock = __SPIN_LOCK_UNLOCKED(&rcu_ctrlblk.lock), |
| 66 | .cpumask = CPU_MASK_NONE, |
| 67 | }; |
| 68 | static struct rcu_ctrlblk rcu_bh_ctrlblk = { |
| 69 | .cur = -300, |
| 70 | .completed = -300, |
Lai Jiangshan | 3cac97c | 2008-07-06 17:23:55 +0800 | [diff] [blame] | 71 | .pending = -300, |
Paul E. McKenney | 01c1c66 | 2008-01-25 21:08:24 +0100 | [diff] [blame] | 72 | .lock = __SPIN_LOCK_UNLOCKED(&rcu_bh_ctrlblk.lock), |
| 73 | .cpumask = CPU_MASK_NONE, |
| 74 | }; |
| 75 | |
| 76 | DEFINE_PER_CPU(struct rcu_data, rcu_data) = { 0L }; |
| 77 | DEFINE_PER_CPU(struct rcu_data, rcu_bh_data) = { 0L }; |
| 78 | |
| 79 | static int blimit = 10; |
| 80 | static int qhimark = 10000; |
| 81 | static int qlowmark = 100; |
| 82 | |
| 83 | #ifdef CONFIG_SMP |
| 84 | static void force_quiescent_state(struct rcu_data *rdp, |
| 85 | struct rcu_ctrlblk *rcp) |
| 86 | { |
| 87 | int cpu; |
| 88 | cpumask_t cpumask; |
| 89 | set_need_resched(); |
| 90 | if (unlikely(!rcp->signaled)) { |
| 91 | rcp->signaled = 1; |
| 92 | /* |
| 93 | * Don't send IPI to itself. With irqs disabled, |
| 94 | * rdp->cpu is the current cpu. |
Gautham R Shenoy | 8558f8f | 2008-06-27 10:17:38 +0530 | [diff] [blame] | 95 | * |
| 96 | * cpu_online_map is updated by the _cpu_down() |
Rusty Russell | 9b1a4d3 | 2008-07-28 12:16:30 -0500 | [diff] [blame] | 97 | * using __stop_machine(). Since we're in irqs disabled |
| 98 | * section, __stop_machine() is not exectuting, hence |
Gautham R Shenoy | 8558f8f | 2008-06-27 10:17:38 +0530 | [diff] [blame] | 99 | * the cpu_online_map is stable. |
| 100 | * |
| 101 | * However, a cpu might have been offlined _just_ before |
| 102 | * we disabled irqs while entering here. |
| 103 | * And rcu subsystem might not yet have handled the CPU_DEAD |
| 104 | * notification, leading to the offlined cpu's bit |
| 105 | * being set in the rcp->cpumask. |
| 106 | * |
| 107 | * Hence cpumask = (rcp->cpumask & cpu_online_map) to prevent |
| 108 | * sending smp_reschedule() to an offlined CPU. |
Paul E. McKenney | 01c1c66 | 2008-01-25 21:08:24 +0100 | [diff] [blame] | 109 | */ |
Gautham R Shenoy | 8558f8f | 2008-06-27 10:17:38 +0530 | [diff] [blame] | 110 | cpus_and(cpumask, rcp->cpumask, cpu_online_map); |
Paul E. McKenney | 01c1c66 | 2008-01-25 21:08:24 +0100 | [diff] [blame] | 111 | cpu_clear(rdp->cpu, cpumask); |
Mike Travis | 363ab6f | 2008-05-12 21:21:13 +0200 | [diff] [blame] | 112 | for_each_cpu_mask_nr(cpu, cpumask) |
Paul E. McKenney | 01c1c66 | 2008-01-25 21:08:24 +0100 | [diff] [blame] | 113 | smp_send_reschedule(cpu); |
| 114 | } |
| 115 | } |
| 116 | #else |
| 117 | static inline void force_quiescent_state(struct rcu_data *rdp, |
| 118 | struct rcu_ctrlblk *rcp) |
| 119 | { |
| 120 | set_need_resched(); |
| 121 | } |
| 122 | #endif |
| 123 | |
Lai Jiangshan | 5127bed | 2008-07-06 17:23:59 +0800 | [diff] [blame] | 124 | static void __call_rcu(struct rcu_head *head, struct rcu_ctrlblk *rcp, |
| 125 | struct rcu_data *rdp) |
| 126 | { |
| 127 | long batch; |
| 128 | smp_mb(); /* reads the most recently updated value of rcu->cur. */ |
| 129 | |
| 130 | /* |
| 131 | * Determine the batch number of this callback. |
| 132 | * |
| 133 | * Using ACCESS_ONCE to avoid the following error when gcc eliminates |
| 134 | * local variable "batch" and emits codes like this: |
| 135 | * 1) rdp->batch = rcp->cur + 1 # gets old value |
| 136 | * ...... |
| 137 | * 2)rcu_batch_after(rcp->cur + 1, rdp->batch) # gets new value |
| 138 | * then [*nxttail[0], *nxttail[1]) may contain callbacks |
| 139 | * that batch# = rdp->batch, see the comment of struct rcu_data. |
| 140 | */ |
| 141 | batch = ACCESS_ONCE(rcp->cur) + 1; |
| 142 | |
| 143 | if (rdp->nxtlist && rcu_batch_after(batch, rdp->batch)) { |
| 144 | /* process callbacks */ |
| 145 | rdp->nxttail[0] = rdp->nxttail[1]; |
| 146 | rdp->nxttail[1] = rdp->nxttail[2]; |
| 147 | if (rcu_batch_after(batch - 1, rdp->batch)) |
| 148 | rdp->nxttail[0] = rdp->nxttail[2]; |
| 149 | } |
| 150 | |
| 151 | rdp->batch = batch; |
| 152 | *rdp->nxttail[2] = head; |
| 153 | rdp->nxttail[2] = &head->next; |
| 154 | |
| 155 | if (unlikely(++rdp->qlen > qhimark)) { |
| 156 | rdp->blimit = INT_MAX; |
| 157 | force_quiescent_state(rdp, &rcu_ctrlblk); |
| 158 | } |
| 159 | } |
| 160 | |
Paul E. McKenney | 01c1c66 | 2008-01-25 21:08:24 +0100 | [diff] [blame] | 161 | /** |
| 162 | * call_rcu - Queue an RCU callback for invocation after a grace period. |
| 163 | * @head: structure to be used for queueing the RCU updates. |
| 164 | * @func: actual update function to be invoked after the grace period |
| 165 | * |
| 166 | * The update function will be invoked some time after a full grace |
| 167 | * period elapses, in other words after all currently executing RCU |
| 168 | * read-side critical sections have completed. RCU read-side critical |
| 169 | * sections are delimited by rcu_read_lock() and rcu_read_unlock(), |
| 170 | * and may be nested. |
| 171 | */ |
| 172 | void call_rcu(struct rcu_head *head, |
| 173 | void (*func)(struct rcu_head *rcu)) |
| 174 | { |
| 175 | unsigned long flags; |
Paul E. McKenney | 01c1c66 | 2008-01-25 21:08:24 +0100 | [diff] [blame] | 176 | |
| 177 | head->func = func; |
| 178 | head->next = NULL; |
| 179 | local_irq_save(flags); |
Lai Jiangshan | 5127bed | 2008-07-06 17:23:59 +0800 | [diff] [blame] | 180 | __call_rcu(head, &rcu_ctrlblk, &__get_cpu_var(rcu_data)); |
Paul E. McKenney | 01c1c66 | 2008-01-25 21:08:24 +0100 | [diff] [blame] | 181 | local_irq_restore(flags); |
| 182 | } |
| 183 | EXPORT_SYMBOL_GPL(call_rcu); |
| 184 | |
| 185 | /** |
| 186 | * call_rcu_bh - Queue an RCU for invocation after a quicker grace period. |
| 187 | * @head: structure to be used for queueing the RCU updates. |
| 188 | * @func: actual update function to be invoked after the grace period |
| 189 | * |
| 190 | * The update function will be invoked some time after a full grace |
| 191 | * period elapses, in other words after all currently executing RCU |
| 192 | * read-side critical sections have completed. call_rcu_bh() assumes |
| 193 | * that the read-side critical sections end on completion of a softirq |
| 194 | * handler. This means that read-side critical sections in process |
| 195 | * context must not be interrupted by softirqs. This interface is to be |
| 196 | * used when most of the read-side critical sections are in softirq context. |
| 197 | * RCU read-side critical sections are delimited by rcu_read_lock() and |
| 198 | * rcu_read_unlock(), * if in interrupt context or rcu_read_lock_bh() |
| 199 | * and rcu_read_unlock_bh(), if in process context. These may be nested. |
| 200 | */ |
| 201 | void call_rcu_bh(struct rcu_head *head, |
| 202 | void (*func)(struct rcu_head *rcu)) |
| 203 | { |
| 204 | unsigned long flags; |
Paul E. McKenney | 01c1c66 | 2008-01-25 21:08:24 +0100 | [diff] [blame] | 205 | |
| 206 | head->func = func; |
| 207 | head->next = NULL; |
| 208 | local_irq_save(flags); |
Lai Jiangshan | 5127bed | 2008-07-06 17:23:59 +0800 | [diff] [blame] | 209 | __call_rcu(head, &rcu_bh_ctrlblk, &__get_cpu_var(rcu_bh_data)); |
Paul E. McKenney | 01c1c66 | 2008-01-25 21:08:24 +0100 | [diff] [blame] | 210 | local_irq_restore(flags); |
| 211 | } |
| 212 | EXPORT_SYMBOL_GPL(call_rcu_bh); |
| 213 | |
| 214 | /* |
| 215 | * Return the number of RCU batches processed thus far. Useful |
| 216 | * for debug and statistics. |
| 217 | */ |
| 218 | long rcu_batches_completed(void) |
| 219 | { |
| 220 | return rcu_ctrlblk.completed; |
| 221 | } |
| 222 | EXPORT_SYMBOL_GPL(rcu_batches_completed); |
| 223 | |
| 224 | /* |
| 225 | * Return the number of RCU batches processed thus far. Useful |
| 226 | * for debug and statistics. |
| 227 | */ |
| 228 | long rcu_batches_completed_bh(void) |
| 229 | { |
| 230 | return rcu_bh_ctrlblk.completed; |
| 231 | } |
| 232 | EXPORT_SYMBOL_GPL(rcu_batches_completed_bh); |
| 233 | |
| 234 | /* Raises the softirq for processing rcu_callbacks. */ |
| 235 | static inline void raise_rcu_softirq(void) |
| 236 | { |
| 237 | raise_softirq(RCU_SOFTIRQ); |
Paul E. McKenney | 01c1c66 | 2008-01-25 21:08:24 +0100 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | /* |
| 241 | * Invoke the completed RCU callbacks. They are expected to be in |
| 242 | * a per-cpu list. |
| 243 | */ |
| 244 | static void rcu_do_batch(struct rcu_data *rdp) |
| 245 | { |
| 246 | struct rcu_head *next, *list; |
| 247 | int count = 0; |
| 248 | |
| 249 | list = rdp->donelist; |
| 250 | while (list) { |
| 251 | next = list->next; |
| 252 | prefetch(next); |
| 253 | list->func(list); |
| 254 | list = next; |
| 255 | if (++count >= rdp->blimit) |
| 256 | break; |
| 257 | } |
| 258 | rdp->donelist = list; |
| 259 | |
| 260 | local_irq_disable(); |
| 261 | rdp->qlen -= count; |
| 262 | local_irq_enable(); |
| 263 | if (rdp->blimit == INT_MAX && rdp->qlen <= qlowmark) |
| 264 | rdp->blimit = blimit; |
| 265 | |
| 266 | if (!rdp->donelist) |
| 267 | rdp->donetail = &rdp->donelist; |
| 268 | else |
| 269 | raise_rcu_softirq(); |
| 270 | } |
| 271 | |
| 272 | /* |
| 273 | * Grace period handling: |
| 274 | * The grace period handling consists out of two steps: |
| 275 | * - A new grace period is started. |
| 276 | * This is done by rcu_start_batch. The start is not broadcasted to |
| 277 | * all cpus, they must pick this up by comparing rcp->cur with |
| 278 | * rdp->quiescbatch. All cpus are recorded in the |
| 279 | * rcu_ctrlblk.cpumask bitmap. |
| 280 | * - All cpus must go through a quiescent state. |
| 281 | * Since the start of the grace period is not broadcasted, at least two |
| 282 | * calls to rcu_check_quiescent_state are required: |
| 283 | * The first call just notices that a new grace period is running. The |
| 284 | * following calls check if there was a quiescent state since the beginning |
| 285 | * of the grace period. If so, it updates rcu_ctrlblk.cpumask. If |
| 286 | * the bitmap is empty, then the grace period is completed. |
| 287 | * rcu_check_quiescent_state calls rcu_start_batch(0) to start the next grace |
| 288 | * period (if necessary). |
| 289 | */ |
Paul E. McKenney | 67182ae | 2008-08-10 18:35:38 -0700 | [diff] [blame^] | 290 | |
| 291 | #ifdef CONFIG_DEBUG_RCU_STALL |
| 292 | |
| 293 | static inline void record_gp_check_time(struct rcu_ctrlblk *rcp) |
| 294 | { |
| 295 | rcp->gp_check = get_seconds() + 3; |
| 296 | } |
| 297 | static void print_other_cpu_stall(struct rcu_ctrlblk *rcp) |
| 298 | { |
| 299 | int cpu; |
| 300 | long delta; |
| 301 | |
| 302 | /* Only let one CPU complain about others per time interval. */ |
| 303 | |
| 304 | spin_lock(&rcp->lock); |
| 305 | delta = get_seconds() - rcp->gp_check; |
| 306 | if (delta < 2L || |
| 307 | cpus_empty(rcp->cpumask)) { |
| 308 | spin_unlock(&rcp->lock); |
| 309 | return; |
| 310 | rcp->gp_check = get_seconds() + 30; |
| 311 | } |
| 312 | spin_unlock(&rcp->lock); |
| 313 | |
| 314 | /* OK, time to rat on our buddy... */ |
| 315 | |
| 316 | printk(KERN_ERR "RCU detected CPU stalls:"); |
| 317 | for_each_cpu_mask(cpu, rcp->cpumask) |
| 318 | printk(" %d", cpu); |
| 319 | printk(" (detected by %d, t=%lu/%lu)\n", |
| 320 | smp_processor_id(), get_seconds(), rcp->gp_check); |
| 321 | } |
| 322 | static void print_cpu_stall(struct rcu_ctrlblk *rcp) |
| 323 | { |
| 324 | printk(KERN_ERR "RCU detected CPU %d stall (t=%lu/%lu)\n", |
| 325 | smp_processor_id(), get_seconds(), rcp->gp_check); |
| 326 | dump_stack(); |
| 327 | spin_lock(&rcp->lock); |
| 328 | if ((long)(get_seconds() - rcp->gp_check) >= 0L) |
| 329 | rcp->gp_check = get_seconds() + 30; |
| 330 | spin_unlock(&rcp->lock); |
| 331 | } |
| 332 | static inline void check_cpu_stall(struct rcu_ctrlblk *rcp, |
| 333 | struct rcu_data *rdp) |
| 334 | { |
| 335 | long delta; |
| 336 | |
| 337 | delta = get_seconds() - rcp->gp_check; |
| 338 | if (cpu_isset(smp_processor_id(), rcp->cpumask) && delta >= 0L) { |
| 339 | |
| 340 | /* We haven't checked in, so go dump stack. */ |
| 341 | |
| 342 | print_cpu_stall(rcp); |
| 343 | |
| 344 | } else if (!cpus_empty(rcp->cpumask) && delta >= 2L) { |
| 345 | |
| 346 | /* They had two seconds to dump stack, so complain. */ |
| 347 | |
| 348 | print_other_cpu_stall(rcp); |
| 349 | |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | #else /* #ifdef CONFIG_DEBUG_RCU_STALL */ |
| 354 | |
| 355 | static inline void record_gp_check_time(struct rcu_ctrlblk *rcp) |
| 356 | { |
| 357 | } |
| 358 | static inline void check_cpu_stall(struct rcu_ctrlblk *rcp, |
| 359 | struct rcu_data *rdp) |
| 360 | { |
| 361 | } |
| 362 | |
| 363 | #endif /* #else #ifdef CONFIG_DEBUG_RCU_STALL */ |
| 364 | |
Paul E. McKenney | 01c1c66 | 2008-01-25 21:08:24 +0100 | [diff] [blame] | 365 | /* |
| 366 | * Register a new batch of callbacks, and start it up if there is currently no |
| 367 | * active batch and the batch to be registered has not already occurred. |
| 368 | * Caller must hold rcu_ctrlblk.lock. |
| 369 | */ |
| 370 | static void rcu_start_batch(struct rcu_ctrlblk *rcp) |
| 371 | { |
Lai Jiangshan | 3cac97c | 2008-07-06 17:23:55 +0800 | [diff] [blame] | 372 | if (rcp->cur != rcp->pending && |
Paul E. McKenney | 01c1c66 | 2008-01-25 21:08:24 +0100 | [diff] [blame] | 373 | rcp->completed == rcp->cur) { |
Paul E. McKenney | 01c1c66 | 2008-01-25 21:08:24 +0100 | [diff] [blame] | 374 | rcp->cur++; |
Paul E. McKenney | 67182ae | 2008-08-10 18:35:38 -0700 | [diff] [blame^] | 375 | record_gp_check_time(rcp); |
Paul E. McKenney | 01c1c66 | 2008-01-25 21:08:24 +0100 | [diff] [blame] | 376 | |
| 377 | /* |
| 378 | * Accessing nohz_cpu_mask before incrementing rcp->cur needs a |
| 379 | * Barrier Otherwise it can cause tickless idle CPUs to be |
| 380 | * included in rcp->cpumask, which will extend graceperiods |
| 381 | * unnecessarily. |
| 382 | */ |
| 383 | smp_mb(); |
| 384 | cpus_andnot(rcp->cpumask, cpu_online_map, nohz_cpu_mask); |
| 385 | |
| 386 | rcp->signaled = 0; |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | /* |
| 391 | * cpu went through a quiescent state since the beginning of the grace period. |
| 392 | * Clear it from the cpu mask and complete the grace period if it was the last |
| 393 | * cpu. Start another grace period if someone has further entries pending |
| 394 | */ |
| 395 | static void cpu_quiet(int cpu, struct rcu_ctrlblk *rcp) |
| 396 | { |
| 397 | cpu_clear(cpu, rcp->cpumask); |
| 398 | if (cpus_empty(rcp->cpumask)) { |
| 399 | /* batch completed ! */ |
| 400 | rcp->completed = rcp->cur; |
| 401 | rcu_start_batch(rcp); |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | /* |
| 406 | * Check if the cpu has gone through a quiescent state (say context |
| 407 | * switch). If so and if it already hasn't done so in this RCU |
| 408 | * quiescent cycle, then indicate that it has done so. |
| 409 | */ |
| 410 | static void rcu_check_quiescent_state(struct rcu_ctrlblk *rcp, |
| 411 | struct rcu_data *rdp) |
| 412 | { |
| 413 | if (rdp->quiescbatch != rcp->cur) { |
| 414 | /* start new grace period: */ |
| 415 | rdp->qs_pending = 1; |
| 416 | rdp->passed_quiesc = 0; |
| 417 | rdp->quiescbatch = rcp->cur; |
| 418 | return; |
| 419 | } |
| 420 | |
| 421 | /* Grace period already completed for this cpu? |
| 422 | * qs_pending is checked instead of the actual bitmap to avoid |
| 423 | * cacheline trashing. |
| 424 | */ |
| 425 | if (!rdp->qs_pending) |
| 426 | return; |
| 427 | |
| 428 | /* |
| 429 | * Was there a quiescent state since the beginning of the grace |
| 430 | * period? If no, then exit and wait for the next call. |
| 431 | */ |
| 432 | if (!rdp->passed_quiesc) |
| 433 | return; |
| 434 | rdp->qs_pending = 0; |
| 435 | |
| 436 | spin_lock(&rcp->lock); |
| 437 | /* |
| 438 | * rdp->quiescbatch/rcp->cur and the cpu bitmap can come out of sync |
| 439 | * during cpu startup. Ignore the quiescent state. |
| 440 | */ |
| 441 | if (likely(rdp->quiescbatch == rcp->cur)) |
| 442 | cpu_quiet(rdp->cpu, rcp); |
| 443 | |
| 444 | spin_unlock(&rcp->lock); |
| 445 | } |
| 446 | |
| 447 | |
| 448 | #ifdef CONFIG_HOTPLUG_CPU |
| 449 | |
| 450 | /* warning! helper for rcu_offline_cpu. do not use elsewhere without reviewing |
| 451 | * locking requirements, the list it's pulling from has to belong to a cpu |
| 452 | * which is dead and hence not processing interrupts. |
| 453 | */ |
| 454 | static void rcu_move_batch(struct rcu_data *this_rdp, struct rcu_head *list, |
Lai Jiangshan | 5127bed | 2008-07-06 17:23:59 +0800 | [diff] [blame] | 455 | struct rcu_head **tail, long batch) |
Paul E. McKenney | 01c1c66 | 2008-01-25 21:08:24 +0100 | [diff] [blame] | 456 | { |
Lai Jiangshan | 5127bed | 2008-07-06 17:23:59 +0800 | [diff] [blame] | 457 | if (list) { |
| 458 | local_irq_disable(); |
| 459 | this_rdp->batch = batch; |
| 460 | *this_rdp->nxttail[2] = list; |
| 461 | this_rdp->nxttail[2] = tail; |
| 462 | local_irq_enable(); |
| 463 | } |
Paul E. McKenney | 01c1c66 | 2008-01-25 21:08:24 +0100 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | static void __rcu_offline_cpu(struct rcu_data *this_rdp, |
| 467 | struct rcu_ctrlblk *rcp, struct rcu_data *rdp) |
| 468 | { |
| 469 | /* if the cpu going offline owns the grace period |
| 470 | * we can block indefinitely waiting for it, so flush |
| 471 | * it here |
| 472 | */ |
| 473 | spin_lock_bh(&rcp->lock); |
| 474 | if (rcp->cur != rcp->completed) |
| 475 | cpu_quiet(rdp->cpu, rcp); |
| 476 | spin_unlock_bh(&rcp->lock); |
Lai Jiangshan | 5127bed | 2008-07-06 17:23:59 +0800 | [diff] [blame] | 477 | /* spin_lock implies smp_mb() */ |
| 478 | rcu_move_batch(this_rdp, rdp->donelist, rdp->donetail, rcp->cur + 1); |
| 479 | rcu_move_batch(this_rdp, rdp->nxtlist, rdp->nxttail[2], rcp->cur + 1); |
Lai Jiangshan | 199a952 | 2008-06-26 10:06:43 +0800 | [diff] [blame] | 480 | |
| 481 | local_irq_disable(); |
| 482 | this_rdp->qlen += rdp->qlen; |
| 483 | local_irq_enable(); |
Paul E. McKenney | 01c1c66 | 2008-01-25 21:08:24 +0100 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | static void rcu_offline_cpu(int cpu) |
| 487 | { |
| 488 | struct rcu_data *this_rdp = &get_cpu_var(rcu_data); |
| 489 | struct rcu_data *this_bh_rdp = &get_cpu_var(rcu_bh_data); |
| 490 | |
| 491 | __rcu_offline_cpu(this_rdp, &rcu_ctrlblk, |
| 492 | &per_cpu(rcu_data, cpu)); |
| 493 | __rcu_offline_cpu(this_bh_rdp, &rcu_bh_ctrlblk, |
| 494 | &per_cpu(rcu_bh_data, cpu)); |
| 495 | put_cpu_var(rcu_data); |
| 496 | put_cpu_var(rcu_bh_data); |
| 497 | } |
| 498 | |
| 499 | #else |
| 500 | |
| 501 | static void rcu_offline_cpu(int cpu) |
| 502 | { |
| 503 | } |
| 504 | |
| 505 | #endif |
| 506 | |
| 507 | /* |
| 508 | * This does the RCU processing work from softirq context. |
| 509 | */ |
| 510 | static void __rcu_process_callbacks(struct rcu_ctrlblk *rcp, |
| 511 | struct rcu_data *rdp) |
| 512 | { |
Lai Jiangshan | 5127bed | 2008-07-06 17:23:59 +0800 | [diff] [blame] | 513 | if (rdp->nxtlist) { |
Paul E. McKenney | 01c1c66 | 2008-01-25 21:08:24 +0100 | [diff] [blame] | 514 | local_irq_disable(); |
Paul E. McKenney | 01c1c66 | 2008-01-25 21:08:24 +0100 | [diff] [blame] | 515 | |
| 516 | /* |
Lai Jiangshan | 5127bed | 2008-07-06 17:23:59 +0800 | [diff] [blame] | 517 | * move the other grace-period-completed entries to |
| 518 | * [rdp->nxtlist, *rdp->nxttail[0]) temporarily |
Paul E. McKenney | 01c1c66 | 2008-01-25 21:08:24 +0100 | [diff] [blame] | 519 | */ |
Lai Jiangshan | 5127bed | 2008-07-06 17:23:59 +0800 | [diff] [blame] | 520 | if (!rcu_batch_before(rcp->completed, rdp->batch)) |
| 521 | rdp->nxttail[0] = rdp->nxttail[1] = rdp->nxttail[2]; |
| 522 | else if (!rcu_batch_before(rcp->completed, rdp->batch - 1)) |
| 523 | rdp->nxttail[0] = rdp->nxttail[1]; |
Paul E. McKenney | 01c1c66 | 2008-01-25 21:08:24 +0100 | [diff] [blame] | 524 | |
Lai Jiangshan | 5127bed | 2008-07-06 17:23:59 +0800 | [diff] [blame] | 525 | /* |
| 526 | * the grace period for entries in |
| 527 | * [rdp->nxtlist, *rdp->nxttail[0]) has completed and |
| 528 | * move these entries to donelist |
| 529 | */ |
| 530 | if (rdp->nxttail[0] != &rdp->nxtlist) { |
| 531 | *rdp->donetail = rdp->nxtlist; |
| 532 | rdp->donetail = rdp->nxttail[0]; |
| 533 | rdp->nxtlist = *rdp->nxttail[0]; |
| 534 | *rdp->donetail = NULL; |
| 535 | |
| 536 | if (rdp->nxttail[1] == rdp->nxttail[0]) |
| 537 | rdp->nxttail[1] = &rdp->nxtlist; |
| 538 | if (rdp->nxttail[2] == rdp->nxttail[0]) |
| 539 | rdp->nxttail[2] = &rdp->nxtlist; |
| 540 | rdp->nxttail[0] = &rdp->nxtlist; |
| 541 | } |
| 542 | |
| 543 | local_irq_enable(); |
Paul E. McKenney | 01c1c66 | 2008-01-25 21:08:24 +0100 | [diff] [blame] | 544 | |
Lai Jiangshan | 3cac97c | 2008-07-06 17:23:55 +0800 | [diff] [blame] | 545 | if (rcu_batch_after(rdp->batch, rcp->pending)) { |
Paul E. McKenney | 01c1c66 | 2008-01-25 21:08:24 +0100 | [diff] [blame] | 546 | /* and start it/schedule start if it's a new batch */ |
| 547 | spin_lock(&rcp->lock); |
Lai Jiangshan | 3cac97c | 2008-07-06 17:23:55 +0800 | [diff] [blame] | 548 | if (rcu_batch_after(rdp->batch, rcp->pending)) { |
| 549 | rcp->pending = rdp->batch; |
| 550 | rcu_start_batch(rcp); |
| 551 | } |
Paul E. McKenney | 01c1c66 | 2008-01-25 21:08:24 +0100 | [diff] [blame] | 552 | spin_unlock(&rcp->lock); |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | rcu_check_quiescent_state(rcp, rdp); |
| 557 | if (rdp->donelist) |
| 558 | rcu_do_batch(rdp); |
| 559 | } |
| 560 | |
| 561 | static void rcu_process_callbacks(struct softirq_action *unused) |
| 562 | { |
| 563 | __rcu_process_callbacks(&rcu_ctrlblk, &__get_cpu_var(rcu_data)); |
| 564 | __rcu_process_callbacks(&rcu_bh_ctrlblk, &__get_cpu_var(rcu_bh_data)); |
| 565 | } |
| 566 | |
| 567 | static int __rcu_pending(struct rcu_ctrlblk *rcp, struct rcu_data *rdp) |
| 568 | { |
Paul E. McKenney | 67182ae | 2008-08-10 18:35:38 -0700 | [diff] [blame^] | 569 | /* Check for CPU stalls, if enabled. */ |
| 570 | check_cpu_stall(rcp, rdp); |
| 571 | |
Lai Jiangshan | 5127bed | 2008-07-06 17:23:59 +0800 | [diff] [blame] | 572 | if (rdp->nxtlist) { |
| 573 | /* |
| 574 | * This cpu has pending rcu entries and the grace period |
| 575 | * for them has completed. |
| 576 | */ |
| 577 | if (!rcu_batch_before(rcp->completed, rdp->batch)) |
| 578 | return 1; |
| 579 | if (!rcu_batch_before(rcp->completed, rdp->batch - 1) && |
| 580 | rdp->nxttail[0] != rdp->nxttail[1]) |
| 581 | return 1; |
| 582 | if (rdp->nxttail[0] != &rdp->nxtlist) |
| 583 | return 1; |
Paul E. McKenney | 01c1c66 | 2008-01-25 21:08:24 +0100 | [diff] [blame] | 584 | |
Lai Jiangshan | 5127bed | 2008-07-06 17:23:59 +0800 | [diff] [blame] | 585 | /* |
| 586 | * This cpu has pending rcu entries and the new batch |
| 587 | * for then hasn't been started nor scheduled start |
| 588 | */ |
| 589 | if (rcu_batch_after(rdp->batch, rcp->pending)) |
| 590 | return 1; |
| 591 | } |
Paul E. McKenney | 01c1c66 | 2008-01-25 21:08:24 +0100 | [diff] [blame] | 592 | |
| 593 | /* This cpu has finished callbacks to invoke */ |
| 594 | if (rdp->donelist) |
| 595 | return 1; |
| 596 | |
| 597 | /* The rcu core waits for a quiescent state from the cpu */ |
| 598 | if (rdp->quiescbatch != rcp->cur || rdp->qs_pending) |
| 599 | return 1; |
| 600 | |
| 601 | /* nothing to do */ |
| 602 | return 0; |
| 603 | } |
| 604 | |
| 605 | /* |
| 606 | * Check to see if there is any immediate RCU-related work to be done |
| 607 | * by the current CPU, returning 1 if so. This function is part of the |
| 608 | * RCU implementation; it is -not- an exported member of the RCU API. |
| 609 | */ |
| 610 | int rcu_pending(int cpu) |
| 611 | { |
| 612 | return __rcu_pending(&rcu_ctrlblk, &per_cpu(rcu_data, cpu)) || |
| 613 | __rcu_pending(&rcu_bh_ctrlblk, &per_cpu(rcu_bh_data, cpu)); |
| 614 | } |
| 615 | |
| 616 | /* |
| 617 | * Check to see if any future RCU-related work will need to be done |
| 618 | * by the current CPU, even if none need be done immediately, returning |
| 619 | * 1 if so. This function is part of the RCU implementation; it is -not- |
| 620 | * an exported member of the RCU API. |
| 621 | */ |
| 622 | int rcu_needs_cpu(int cpu) |
| 623 | { |
| 624 | struct rcu_data *rdp = &per_cpu(rcu_data, cpu); |
| 625 | struct rcu_data *rdp_bh = &per_cpu(rcu_bh_data, cpu); |
| 626 | |
Lai Jiangshan | 5127bed | 2008-07-06 17:23:59 +0800 | [diff] [blame] | 627 | return !!rdp->nxtlist || !!rdp_bh->nxtlist || rcu_pending(cpu); |
Paul E. McKenney | 01c1c66 | 2008-01-25 21:08:24 +0100 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | void rcu_check_callbacks(int cpu, int user) |
| 631 | { |
| 632 | if (user || |
| 633 | (idle_cpu(cpu) && !in_softirq() && |
| 634 | hardirq_count() <= (1 << HARDIRQ_SHIFT))) { |
Paul E. McKenney | 8db559b | 2008-05-12 21:21:05 +0200 | [diff] [blame] | 635 | |
| 636 | /* |
| 637 | * Get here if this CPU took its interrupt from user |
| 638 | * mode or from the idle loop, and if this is not a |
| 639 | * nested interrupt. In this case, the CPU is in |
| 640 | * a quiescent state, so count it. |
| 641 | * |
| 642 | * Also do a memory barrier. This is needed to handle |
| 643 | * the case where writes from a preempt-disable section |
| 644 | * of code get reordered into schedule() by this CPU's |
| 645 | * write buffer. The memory barrier makes sure that |
| 646 | * the rcu_qsctr_inc() and rcu_bh_qsctr_inc() are see |
| 647 | * by other CPUs to happen after any such write. |
| 648 | */ |
| 649 | |
| 650 | smp_mb(); /* See above block comment. */ |
Paul E. McKenney | 01c1c66 | 2008-01-25 21:08:24 +0100 | [diff] [blame] | 651 | rcu_qsctr_inc(cpu); |
| 652 | rcu_bh_qsctr_inc(cpu); |
Paul E. McKenney | 8db559b | 2008-05-12 21:21:05 +0200 | [diff] [blame] | 653 | |
| 654 | } else if (!in_softirq()) { |
| 655 | |
| 656 | /* |
| 657 | * Get here if this CPU did not take its interrupt from |
| 658 | * softirq, in other words, if it is not interrupting |
| 659 | * a rcu_bh read-side critical section. This is an _bh |
| 660 | * critical section, so count it. The memory barrier |
| 661 | * is needed for the same reason as is the above one. |
| 662 | */ |
| 663 | |
| 664 | smp_mb(); /* See above block comment. */ |
Paul E. McKenney | 01c1c66 | 2008-01-25 21:08:24 +0100 | [diff] [blame] | 665 | rcu_bh_qsctr_inc(cpu); |
Paul E. McKenney | 8db559b | 2008-05-12 21:21:05 +0200 | [diff] [blame] | 666 | } |
Paul E. McKenney | 01c1c66 | 2008-01-25 21:08:24 +0100 | [diff] [blame] | 667 | raise_rcu_softirq(); |
| 668 | } |
| 669 | |
| 670 | static void rcu_init_percpu_data(int cpu, struct rcu_ctrlblk *rcp, |
| 671 | struct rcu_data *rdp) |
| 672 | { |
| 673 | memset(rdp, 0, sizeof(*rdp)); |
Lai Jiangshan | 5127bed | 2008-07-06 17:23:59 +0800 | [diff] [blame] | 674 | rdp->nxttail[0] = rdp->nxttail[1] = rdp->nxttail[2] = &rdp->nxtlist; |
Paul E. McKenney | 01c1c66 | 2008-01-25 21:08:24 +0100 | [diff] [blame] | 675 | rdp->donetail = &rdp->donelist; |
| 676 | rdp->quiescbatch = rcp->completed; |
| 677 | rdp->qs_pending = 0; |
| 678 | rdp->cpu = cpu; |
| 679 | rdp->blimit = blimit; |
| 680 | } |
| 681 | |
| 682 | static void __cpuinit rcu_online_cpu(int cpu) |
| 683 | { |
| 684 | struct rcu_data *rdp = &per_cpu(rcu_data, cpu); |
| 685 | struct rcu_data *bh_rdp = &per_cpu(rcu_bh_data, cpu); |
| 686 | |
| 687 | rcu_init_percpu_data(cpu, &rcu_ctrlblk, rdp); |
| 688 | rcu_init_percpu_data(cpu, &rcu_bh_ctrlblk, bh_rdp); |
Carlos R. Mafra | 962cf36 | 2008-05-15 11:15:37 -0300 | [diff] [blame] | 689 | open_softirq(RCU_SOFTIRQ, rcu_process_callbacks); |
Paul E. McKenney | 01c1c66 | 2008-01-25 21:08:24 +0100 | [diff] [blame] | 690 | } |
| 691 | |
| 692 | static int __cpuinit rcu_cpu_notify(struct notifier_block *self, |
| 693 | unsigned long action, void *hcpu) |
| 694 | { |
| 695 | long cpu = (long)hcpu; |
| 696 | |
| 697 | switch (action) { |
| 698 | case CPU_UP_PREPARE: |
| 699 | case CPU_UP_PREPARE_FROZEN: |
| 700 | rcu_online_cpu(cpu); |
| 701 | break; |
| 702 | case CPU_DEAD: |
| 703 | case CPU_DEAD_FROZEN: |
| 704 | rcu_offline_cpu(cpu); |
| 705 | break; |
| 706 | default: |
| 707 | break; |
| 708 | } |
| 709 | return NOTIFY_OK; |
| 710 | } |
| 711 | |
| 712 | static struct notifier_block __cpuinitdata rcu_nb = { |
| 713 | .notifier_call = rcu_cpu_notify, |
| 714 | }; |
| 715 | |
| 716 | /* |
| 717 | * Initializes rcu mechanism. Assumed to be called early. |
| 718 | * That is before local timer(SMP) or jiffie timer (uniproc) is setup. |
| 719 | * Note that rcu_qsctr and friends are implicitly |
| 720 | * initialized due to the choice of ``0'' for RCU_CTR_INVALID. |
| 721 | */ |
| 722 | void __init __rcu_init(void) |
| 723 | { |
| 724 | rcu_cpu_notify(&rcu_nb, CPU_UP_PREPARE, |
| 725 | (void *)(long)smp_processor_id()); |
| 726 | /* Register notifier for non-boot CPUs */ |
| 727 | register_cpu_notifier(&rcu_nb); |
| 728 | } |
| 729 | |
| 730 | module_param(blimit, int, 0); |
| 731 | module_param(qhimark, int, 0); |
| 732 | module_param(qlowmark, int, 0); |