Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / kernel / rcupreempt.c
blobd3bf38ffa7a532d5d8c921980d178300716cb91e
1 /*
2 * Read-Copy Update mechanism for mutual exclusion, realtime implementation
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.
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.
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.
18 * Copyright IBM Corporation, 2006
20 * Authors: Paul E. McKenney <paulmck@us.ibm.com>
21 * With thanks to Esben Nielsen, Bill Huey, and Ingo Molnar
22 * for pushing me away from locks and towards counters, and
23 * to Suparna Bhattacharya for pushing me completely away
24 * from atomic instructions on the read side.
26 <<<<<<< HEAD:kernel/rcupreempt.c
27 =======
28 * - Added handling of Dynamic Ticks
29 * Copyright 2007 - Paul E. Mckenney <paulmck@us.ibm.com>
30 * - Steven Rostedt <srostedt@redhat.com>
32 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:kernel/rcupreempt.c
33 * Papers: http://www.rdrop.com/users/paulmck/RCU
35 * Design Document: http://lwn.net/Articles/253651/
37 * For detailed explanation of Read-Copy Update mechanism see -
38 * Documentation/RCU/ *.txt
41 #include <linux/types.h>
42 #include <linux/kernel.h>
43 #include <linux/init.h>
44 #include <linux/spinlock.h>
45 #include <linux/smp.h>
46 #include <linux/rcupdate.h>
47 #include <linux/interrupt.h>
48 #include <linux/sched.h>
49 #include <asm/atomic.h>
50 #include <linux/bitops.h>
51 #include <linux/module.h>
52 #include <linux/completion.h>
53 #include <linux/moduleparam.h>
54 #include <linux/percpu.h>
55 #include <linux/notifier.h>
56 #include <linux/rcupdate.h>
57 #include <linux/cpu.h>
58 #include <linux/random.h>
59 #include <linux/delay.h>
60 #include <linux/byteorder/swabb.h>
61 #include <linux/cpumask.h>
62 #include <linux/rcupreempt_trace.h>
65 * Macro that prevents the compiler from reordering accesses, but does
66 * absolutely -nothing- to prevent CPUs from reordering. This is used
67 * only to mediate communication between mainline code and hardware
68 * interrupt and NMI handlers.
70 #define ACCESS_ONCE(x) (*(volatile typeof(x) *)&(x))
73 * PREEMPT_RCU data structures.
77 * GP_STAGES specifies the number of times the state machine has
78 * to go through the all the rcu_try_flip_states (see below)
79 * in a single Grace Period.
81 * GP in GP_STAGES stands for Grace Period ;)
83 #define GP_STAGES 2
84 struct rcu_data {
85 spinlock_t lock; /* Protect rcu_data fields. */
86 long completed; /* Number of last completed batch. */
87 int waitlistcount;
88 struct tasklet_struct rcu_tasklet;
89 struct rcu_head *nextlist;
90 struct rcu_head **nexttail;
91 struct rcu_head *waitlist[GP_STAGES];
92 struct rcu_head **waittail[GP_STAGES];
93 struct rcu_head *donelist;
94 struct rcu_head **donetail;
95 long rcu_flipctr[2];
96 #ifdef CONFIG_RCU_TRACE
97 struct rcupreempt_trace trace;
98 #endif /* #ifdef CONFIG_RCU_TRACE */
102 * States for rcu_try_flip() and friends.
105 enum rcu_try_flip_states {
108 * Stay here if nothing is happening. Flip the counter if somthing
109 * starts happening. Denoted by "I"
111 rcu_try_flip_idle_state,
114 * Wait here for all CPUs to notice that the counter has flipped. This
115 * prevents the old set of counters from ever being incremented once
116 * we leave this state, which in turn is necessary because we cannot
117 * test any individual counter for zero -- we can only check the sum.
118 * Denoted by "A".
120 rcu_try_flip_waitack_state,
123 * Wait here for the sum of the old per-CPU counters to reach zero.
124 * Denoted by "Z".
126 rcu_try_flip_waitzero_state,
129 * Wait here for each of the other CPUs to execute a memory barrier.
130 * This is necessary to ensure that these other CPUs really have
131 * completed executing their RCU read-side critical sections, despite
132 * their CPUs wildly reordering memory. Denoted by "M".
134 rcu_try_flip_waitmb_state,
137 struct rcu_ctrlblk {
138 spinlock_t fliplock; /* Protect state-machine transitions. */
139 long completed; /* Number of last completed batch. */
140 enum rcu_try_flip_states rcu_try_flip_state; /* The current state of
141 the rcu state machine */
144 static DEFINE_PER_CPU(struct rcu_data, rcu_data);
145 static struct rcu_ctrlblk rcu_ctrlblk = {
146 .fliplock = __SPIN_LOCK_UNLOCKED(rcu_ctrlblk.fliplock),
147 .completed = 0,
148 .rcu_try_flip_state = rcu_try_flip_idle_state,
152 #ifdef CONFIG_RCU_TRACE
153 static char *rcu_try_flip_state_names[] =
154 { "idle", "waitack", "waitzero", "waitmb" };
155 #endif /* #ifdef CONFIG_RCU_TRACE */
157 static cpumask_t rcu_cpu_online_map __read_mostly = CPU_MASK_NONE;
160 * Enum and per-CPU flag to determine when each CPU has seen
161 * the most recent counter flip.
164 enum rcu_flip_flag_values {
165 rcu_flip_seen, /* Steady/initial state, last flip seen. */
166 /* Only GP detector can update. */
167 rcu_flipped /* Flip just completed, need confirmation. */
168 /* Only corresponding CPU can update. */
170 static DEFINE_PER_CPU_SHARED_ALIGNED(enum rcu_flip_flag_values, rcu_flip_flag)
171 = rcu_flip_seen;
174 * Enum and per-CPU flag to determine when each CPU has executed the
175 * needed memory barrier to fence in memory references from its last RCU
176 * read-side critical section in the just-completed grace period.
179 enum rcu_mb_flag_values {
180 rcu_mb_done, /* Steady/initial state, no mb()s required. */
181 /* Only GP detector can update. */
182 rcu_mb_needed /* Flip just completed, need an mb(). */
183 /* Only corresponding CPU can update. */
185 static DEFINE_PER_CPU_SHARED_ALIGNED(enum rcu_mb_flag_values, rcu_mb_flag)
186 = rcu_mb_done;
189 * RCU_DATA_ME: find the current CPU's rcu_data structure.
190 * RCU_DATA_CPU: find the specified CPU's rcu_data structure.
192 #define RCU_DATA_ME() (&__get_cpu_var(rcu_data))
193 #define RCU_DATA_CPU(cpu) (&per_cpu(rcu_data, cpu))
196 * Helper macro for tracing when the appropriate rcu_data is not
197 * cached in a local variable, but where the CPU number is so cached.
199 #define RCU_TRACE_CPU(f, cpu) RCU_TRACE(f, &(RCU_DATA_CPU(cpu)->trace));
202 * Helper macro for tracing when the appropriate rcu_data is not
203 * cached in a local variable.
205 #define RCU_TRACE_ME(f) RCU_TRACE(f, &(RCU_DATA_ME()->trace));
208 * Helper macro for tracing when the appropriate rcu_data is pointed
209 * to by a local variable.
211 #define RCU_TRACE_RDP(f, rdp) RCU_TRACE(f, &((rdp)->trace));
214 * Return the number of RCU batches processed thus far. Useful
215 * for debug and statistics.
217 long rcu_batches_completed(void)
219 return rcu_ctrlblk.completed;
221 EXPORT_SYMBOL_GPL(rcu_batches_completed);
223 EXPORT_SYMBOL_GPL(rcu_batches_completed_bh);
225 void __rcu_read_lock(void)
227 int idx;
228 struct task_struct *t = current;
229 int nesting;
231 nesting = ACCESS_ONCE(t->rcu_read_lock_nesting);
232 if (nesting != 0) {
234 /* An earlier rcu_read_lock() covers us, just count it. */
236 t->rcu_read_lock_nesting = nesting + 1;
238 } else {
239 unsigned long flags;
242 * We disable interrupts for the following reasons:
243 * - If we get scheduling clock interrupt here, and we
244 * end up acking the counter flip, it's like a promise
245 * that we will never increment the old counter again.
246 * Thus we will break that promise if that
247 * scheduling clock interrupt happens between the time
248 * we pick the .completed field and the time that we
249 * increment our counter.
251 * - We don't want to be preempted out here.
253 * NMIs can still occur, of course, and might themselves
254 * contain rcu_read_lock().
257 local_irq_save(flags);
260 * Outermost nesting of rcu_read_lock(), so increment
261 * the current counter for the current CPU. Use volatile
262 * casts to prevent the compiler from reordering.
265 idx = ACCESS_ONCE(rcu_ctrlblk.completed) & 0x1;
266 ACCESS_ONCE(RCU_DATA_ME()->rcu_flipctr[idx])++;
269 * Now that the per-CPU counter has been incremented, we
270 * are protected from races with rcu_read_lock() invoked
271 * from NMI handlers on this CPU. We can therefore safely
272 * increment the nesting counter, relieving further NMIs
273 * of the need to increment the per-CPU counter.
276 ACCESS_ONCE(t->rcu_read_lock_nesting) = nesting + 1;
279 * Now that we have preventing any NMIs from storing
280 * to the ->rcu_flipctr_idx, we can safely use it to
281 * remember which counter to decrement in the matching
282 * rcu_read_unlock().
285 ACCESS_ONCE(t->rcu_flipctr_idx) = idx;
286 local_irq_restore(flags);
289 EXPORT_SYMBOL_GPL(__rcu_read_lock);
291 void __rcu_read_unlock(void)
293 int idx;
294 struct task_struct *t = current;
295 int nesting;
297 nesting = ACCESS_ONCE(t->rcu_read_lock_nesting);
298 if (nesting > 1) {
301 * We are still protected by the enclosing rcu_read_lock(),
302 * so simply decrement the counter.
305 t->rcu_read_lock_nesting = nesting - 1;
307 } else {
308 unsigned long flags;
311 * Disable local interrupts to prevent the grace-period
312 * detection state machine from seeing us half-done.
313 * NMIs can still occur, of course, and might themselves
314 * contain rcu_read_lock() and rcu_read_unlock().
317 local_irq_save(flags);
320 * Outermost nesting of rcu_read_unlock(), so we must
321 * decrement the current counter for the current CPU.
322 * This must be done carefully, because NMIs can
323 * occur at any point in this code, and any rcu_read_lock()
324 * and rcu_read_unlock() pairs in the NMI handlers
325 * must interact non-destructively with this code.
326 * Lots of volatile casts, and -very- careful ordering.
328 * Changes to this code, including this one, must be
329 * inspected, validated, and tested extremely carefully!!!
333 * First, pick up the index.
336 idx = ACCESS_ONCE(t->rcu_flipctr_idx);
339 * Now that we have fetched the counter index, it is
340 * safe to decrement the per-task RCU nesting counter.
341 * After this, any interrupts or NMIs will increment and
342 * decrement the per-CPU counters.
344 ACCESS_ONCE(t->rcu_read_lock_nesting) = nesting - 1;
347 * It is now safe to decrement this task's nesting count.
348 * NMIs that occur after this statement will route their
349 * rcu_read_lock() calls through this "else" clause, and
350 * will thus start incrementing the per-CPU counter on
351 * their own. They will also clobber ->rcu_flipctr_idx,
352 * but that is OK, since we have already fetched it.
355 ACCESS_ONCE(RCU_DATA_ME()->rcu_flipctr[idx])--;
356 local_irq_restore(flags);
359 EXPORT_SYMBOL_GPL(__rcu_read_unlock);
362 * If a global counter flip has occurred since the last time that we
363 * advanced callbacks, advance them. Hardware interrupts must be
364 * disabled when calling this function.
366 static void __rcu_advance_callbacks(struct rcu_data *rdp)
368 int cpu;
369 int i;
370 int wlc = 0;
372 if (rdp->completed != rcu_ctrlblk.completed) {
373 if (rdp->waitlist[GP_STAGES - 1] != NULL) {
374 *rdp->donetail = rdp->waitlist[GP_STAGES - 1];
375 rdp->donetail = rdp->waittail[GP_STAGES - 1];
376 RCU_TRACE_RDP(rcupreempt_trace_move2done, rdp);
378 for (i = GP_STAGES - 2; i >= 0; i--) {
379 if (rdp->waitlist[i] != NULL) {
380 rdp->waitlist[i + 1] = rdp->waitlist[i];
381 rdp->waittail[i + 1] = rdp->waittail[i];
382 wlc++;
383 } else {
384 rdp->waitlist[i + 1] = NULL;
385 rdp->waittail[i + 1] =
386 &rdp->waitlist[i + 1];
389 if (rdp->nextlist != NULL) {
390 rdp->waitlist[0] = rdp->nextlist;
391 rdp->waittail[0] = rdp->nexttail;
392 wlc++;
393 rdp->nextlist = NULL;
394 rdp->nexttail = &rdp->nextlist;
395 RCU_TRACE_RDP(rcupreempt_trace_move2wait, rdp);
396 } else {
397 rdp->waitlist[0] = NULL;
398 rdp->waittail[0] = &rdp->waitlist[0];
400 rdp->waitlistcount = wlc;
401 rdp->completed = rcu_ctrlblk.completed;
405 * Check to see if this CPU needs to report that it has seen
406 * the most recent counter flip, thereby declaring that all
407 * subsequent rcu_read_lock() invocations will respect this flip.
410 cpu = raw_smp_processor_id();
411 if (per_cpu(rcu_flip_flag, cpu) == rcu_flipped) {
412 smp_mb(); /* Subsequent counter accesses must see new value */
413 per_cpu(rcu_flip_flag, cpu) = rcu_flip_seen;
414 smp_mb(); /* Subsequent RCU read-side critical sections */
415 /* seen -after- acknowledgement. */
419 <<<<<<< HEAD:kernel/rcupreempt.c
420 =======
421 #ifdef CONFIG_NO_HZ
423 DEFINE_PER_CPU(long, dynticks_progress_counter) = 1;
424 static DEFINE_PER_CPU(long, rcu_dyntick_snapshot);
425 static DEFINE_PER_CPU(int, rcu_update_flag);
428 * rcu_irq_enter - Called from Hard irq handlers and NMI/SMI.
430 * If the CPU was idle with dynamic ticks active, this updates the
431 * dynticks_progress_counter to let the RCU handling know that the
432 * CPU is active.
434 void rcu_irq_enter(void)
436 int cpu = smp_processor_id();
438 if (per_cpu(rcu_update_flag, cpu))
439 per_cpu(rcu_update_flag, cpu)++;
442 * Only update if we are coming from a stopped ticks mode
443 * (dynticks_progress_counter is even).
445 if (!in_interrupt() &&
446 (per_cpu(dynticks_progress_counter, cpu) & 0x1) == 0) {
448 * The following might seem like we could have a race
449 * with NMI/SMIs. But this really isn't a problem.
450 * Here we do a read/modify/write, and the race happens
451 * when an NMI/SMI comes in after the read and before
452 * the write. But NMI/SMIs will increment this counter
453 * twice before returning, so the zero bit will not
454 * be corrupted by the NMI/SMI which is the most important
455 * part.
457 * The only thing is that we would bring back the counter
458 * to a postion that it was in during the NMI/SMI.
459 * But the zero bit would be set, so the rest of the
460 * counter would again be ignored.
462 * On return from the IRQ, the counter may have the zero
463 * bit be 0 and the counter the same as the return from
464 * the NMI/SMI. If the state machine was so unlucky to
465 * see that, it still doesn't matter, since all
466 * RCU read-side critical sections on this CPU would
467 * have already completed.
469 per_cpu(dynticks_progress_counter, cpu)++;
471 * The following memory barrier ensures that any
472 * rcu_read_lock() primitives in the irq handler
473 * are seen by other CPUs to follow the above
474 * increment to dynticks_progress_counter. This is
475 * required in order for other CPUs to correctly
476 * determine when it is safe to advance the RCU
477 * grace-period state machine.
479 smp_mb(); /* see above block comment. */
481 * Since we can't determine the dynamic tick mode from
482 * the dynticks_progress_counter after this routine,
483 * we use a second flag to acknowledge that we came
484 * from an idle state with ticks stopped.
486 per_cpu(rcu_update_flag, cpu)++;
488 * If we take an NMI/SMI now, they will also increment
489 * the rcu_update_flag, and will not update the
490 * dynticks_progress_counter on exit. That is for
491 * this IRQ to do.
497 * rcu_irq_exit - Called from exiting Hard irq context.
499 * If the CPU was idle with dynamic ticks active, update the
500 * dynticks_progress_counter to put let the RCU handling be
501 * aware that the CPU is going back to idle with no ticks.
503 void rcu_irq_exit(void)
505 int cpu = smp_processor_id();
508 * rcu_update_flag is set if we interrupted the CPU
509 * when it was idle with ticks stopped.
510 * Once this occurs, we keep track of interrupt nesting
511 * because a NMI/SMI could also come in, and we still
512 * only want the IRQ that started the increment of the
513 * dynticks_progress_counter to be the one that modifies
514 * it on exit.
516 if (per_cpu(rcu_update_flag, cpu)) {
517 if (--per_cpu(rcu_update_flag, cpu))
518 return;
520 /* This must match the interrupt nesting */
521 WARN_ON(in_interrupt());
524 * If an NMI/SMI happens now we are still
525 * protected by the dynticks_progress_counter being odd.
529 * The following memory barrier ensures that any
530 * rcu_read_unlock() primitives in the irq handler
531 * are seen by other CPUs to preceed the following
532 * increment to dynticks_progress_counter. This
533 * is required in order for other CPUs to determine
534 * when it is safe to advance the RCU grace-period
535 * state machine.
537 smp_mb(); /* see above block comment. */
538 per_cpu(dynticks_progress_counter, cpu)++;
539 WARN_ON(per_cpu(dynticks_progress_counter, cpu) & 0x1);
543 static void dyntick_save_progress_counter(int cpu)
545 per_cpu(rcu_dyntick_snapshot, cpu) =
546 per_cpu(dynticks_progress_counter, cpu);
549 static inline int
550 rcu_try_flip_waitack_needed(int cpu)
552 long curr;
553 long snap;
555 curr = per_cpu(dynticks_progress_counter, cpu);
556 snap = per_cpu(rcu_dyntick_snapshot, cpu);
557 smp_mb(); /* force ordering with cpu entering/leaving dynticks. */
560 * If the CPU remained in dynticks mode for the entire time
561 * and didn't take any interrupts, NMIs, SMIs, or whatever,
562 * then it cannot be in the middle of an rcu_read_lock(), so
563 * the next rcu_read_lock() it executes must use the new value
564 * of the counter. So we can safely pretend that this CPU
565 * already acknowledged the counter.
568 if ((curr == snap) && ((curr & 0x1) == 0))
569 return 0;
572 * If the CPU passed through or entered a dynticks idle phase with
573 * no active irq handlers, then, as above, we can safely pretend
574 * that this CPU already acknowledged the counter.
577 if ((curr - snap) > 2 || (snap & 0x1) == 0)
578 return 0;
580 /* We need this CPU to explicitly acknowledge the counter flip. */
582 return 1;
585 static inline int
586 rcu_try_flip_waitmb_needed(int cpu)
588 long curr;
589 long snap;
591 curr = per_cpu(dynticks_progress_counter, cpu);
592 snap = per_cpu(rcu_dyntick_snapshot, cpu);
593 smp_mb(); /* force ordering with cpu entering/leaving dynticks. */
596 * If the CPU remained in dynticks mode for the entire time
597 * and didn't take any interrupts, NMIs, SMIs, or whatever,
598 * then it cannot have executed an RCU read-side critical section
599 * during that time, so there is no need for it to execute a
600 * memory barrier.
603 if ((curr == snap) && ((curr & 0x1) == 0))
604 return 0;
607 * If the CPU either entered or exited an outermost interrupt,
608 * SMI, NMI, or whatever handler, then we know that it executed
609 * a memory barrier when doing so. So we don't need another one.
611 if (curr != snap)
612 return 0;
614 /* We need the CPU to execute a memory barrier. */
616 return 1;
619 #else /* !CONFIG_NO_HZ */
621 # define dyntick_save_progress_counter(cpu) do { } while (0)
622 # define rcu_try_flip_waitack_needed(cpu) (1)
623 # define rcu_try_flip_waitmb_needed(cpu) (1)
625 #endif /* CONFIG_NO_HZ */
627 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:kernel/rcupreempt.c
629 * Get here when RCU is idle. Decide whether we need to
630 * move out of idle state, and return non-zero if so.
631 * "Straightforward" approach for the moment, might later
632 * use callback-list lengths, grace-period duration, or
633 * some such to determine when to exit idle state.
634 * Might also need a pre-idle test that does not acquire
635 * the lock, but let's get the simple case working first...
638 static int
639 rcu_try_flip_idle(void)
641 int cpu;
643 RCU_TRACE_ME(rcupreempt_trace_try_flip_i1);
644 if (!rcu_pending(smp_processor_id())) {
645 RCU_TRACE_ME(rcupreempt_trace_try_flip_ie1);
646 return 0;
650 * Do the flip.
653 RCU_TRACE_ME(rcupreempt_trace_try_flip_g1);
654 rcu_ctrlblk.completed++; /* stands in for rcu_try_flip_g2 */
657 * Need a memory barrier so that other CPUs see the new
658 * counter value before they see the subsequent change of all
659 * the rcu_flip_flag instances to rcu_flipped.
662 smp_mb(); /* see above block comment. */
664 /* Now ask each CPU for acknowledgement of the flip. */
666 <<<<<<< HEAD:kernel/rcupreempt.c
667 for_each_cpu_mask(cpu, rcu_cpu_online_map)
668 =======
669 for_each_cpu_mask(cpu, rcu_cpu_online_map) {
670 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:kernel/rcupreempt.c
671 per_cpu(rcu_flip_flag, cpu) = rcu_flipped;
672 <<<<<<< HEAD:kernel/rcupreempt.c
673 =======
674 dyntick_save_progress_counter(cpu);
676 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:kernel/rcupreempt.c
678 return 1;
682 * Wait for CPUs to acknowledge the flip.
685 static int
686 rcu_try_flip_waitack(void)
688 int cpu;
690 RCU_TRACE_ME(rcupreempt_trace_try_flip_a1);
691 for_each_cpu_mask(cpu, rcu_cpu_online_map)
692 <<<<<<< HEAD:kernel/rcupreempt.c
693 if (per_cpu(rcu_flip_flag, cpu) != rcu_flip_seen) {
694 =======
695 if (rcu_try_flip_waitack_needed(cpu) &&
696 per_cpu(rcu_flip_flag, cpu) != rcu_flip_seen) {
697 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:kernel/rcupreempt.c
698 RCU_TRACE_ME(rcupreempt_trace_try_flip_ae1);
699 return 0;
703 * Make sure our checks above don't bleed into subsequent
704 * waiting for the sum of the counters to reach zero.
707 smp_mb(); /* see above block comment. */
708 RCU_TRACE_ME(rcupreempt_trace_try_flip_a2);
709 return 1;
713 * Wait for collective ``last'' counter to reach zero,
714 * then tell all CPUs to do an end-of-grace-period memory barrier.
717 static int
718 rcu_try_flip_waitzero(void)
720 int cpu;
721 int lastidx = !(rcu_ctrlblk.completed & 0x1);
722 int sum = 0;
724 /* Check to see if the sum of the "last" counters is zero. */
726 RCU_TRACE_ME(rcupreempt_trace_try_flip_z1);
727 for_each_cpu_mask(cpu, rcu_cpu_online_map)
728 sum += RCU_DATA_CPU(cpu)->rcu_flipctr[lastidx];
729 if (sum != 0) {
730 RCU_TRACE_ME(rcupreempt_trace_try_flip_ze1);
731 return 0;
735 * This ensures that the other CPUs see the call for
736 * memory barriers -after- the sum to zero has been
737 * detected here
739 smp_mb(); /* ^^^^^^^^^^^^ */
741 /* Call for a memory barrier from each CPU. */
742 <<<<<<< HEAD:kernel/rcupreempt.c
743 for_each_cpu_mask(cpu, rcu_cpu_online_map)
744 =======
745 for_each_cpu_mask(cpu, rcu_cpu_online_map) {
746 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:kernel/rcupreempt.c
747 per_cpu(rcu_mb_flag, cpu) = rcu_mb_needed;
748 <<<<<<< HEAD:kernel/rcupreempt.c
749 =======
750 dyntick_save_progress_counter(cpu);
752 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:kernel/rcupreempt.c
754 RCU_TRACE_ME(rcupreempt_trace_try_flip_z2);
755 return 1;
759 * Wait for all CPUs to do their end-of-grace-period memory barrier.
760 * Return 0 once all CPUs have done so.
763 static int
764 rcu_try_flip_waitmb(void)
766 int cpu;
768 RCU_TRACE_ME(rcupreempt_trace_try_flip_m1);
769 for_each_cpu_mask(cpu, rcu_cpu_online_map)
770 <<<<<<< HEAD:kernel/rcupreempt.c
771 if (per_cpu(rcu_mb_flag, cpu) != rcu_mb_done) {
772 =======
773 if (rcu_try_flip_waitmb_needed(cpu) &&
774 per_cpu(rcu_mb_flag, cpu) != rcu_mb_done) {
775 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:kernel/rcupreempt.c
776 RCU_TRACE_ME(rcupreempt_trace_try_flip_me1);
777 return 0;
780 smp_mb(); /* Ensure that the above checks precede any following flip. */
781 RCU_TRACE_ME(rcupreempt_trace_try_flip_m2);
782 return 1;
786 * Attempt a single flip of the counters. Remember, a single flip does
787 * -not- constitute a grace period. Instead, the interval between
788 * at least GP_STAGES consecutive flips is a grace period.
790 * If anyone is nuts enough to run this CONFIG_PREEMPT_RCU implementation
791 * on a large SMP, they might want to use a hierarchical organization of
792 * the per-CPU-counter pairs.
794 static void rcu_try_flip(void)
796 unsigned long flags;
798 RCU_TRACE_ME(rcupreempt_trace_try_flip_1);
799 if (unlikely(!spin_trylock_irqsave(&rcu_ctrlblk.fliplock, flags))) {
800 RCU_TRACE_ME(rcupreempt_trace_try_flip_e1);
801 return;
805 * Take the next transition(s) through the RCU grace-period
806 * flip-counter state machine.
809 switch (rcu_ctrlblk.rcu_try_flip_state) {
810 case rcu_try_flip_idle_state:
811 if (rcu_try_flip_idle())
812 rcu_ctrlblk.rcu_try_flip_state =
813 rcu_try_flip_waitack_state;
814 break;
815 case rcu_try_flip_waitack_state:
816 if (rcu_try_flip_waitack())
817 rcu_ctrlblk.rcu_try_flip_state =
818 rcu_try_flip_waitzero_state;
819 break;
820 case rcu_try_flip_waitzero_state:
821 if (rcu_try_flip_waitzero())
822 rcu_ctrlblk.rcu_try_flip_state =
823 rcu_try_flip_waitmb_state;
824 break;
825 case rcu_try_flip_waitmb_state:
826 if (rcu_try_flip_waitmb())
827 rcu_ctrlblk.rcu_try_flip_state =
828 rcu_try_flip_idle_state;
830 spin_unlock_irqrestore(&rcu_ctrlblk.fliplock, flags);
834 * Check to see if this CPU needs to do a memory barrier in order to
835 * ensure that any prior RCU read-side critical sections have committed
836 * their counter manipulations and critical-section memory references
837 * before declaring the grace period to be completed.
839 static void rcu_check_mb(int cpu)
841 if (per_cpu(rcu_mb_flag, cpu) == rcu_mb_needed) {
842 smp_mb(); /* Ensure RCU read-side accesses are visible. */
843 per_cpu(rcu_mb_flag, cpu) = rcu_mb_done;
847 void rcu_check_callbacks(int cpu, int user)
849 unsigned long flags;
850 struct rcu_data *rdp = RCU_DATA_CPU(cpu);
852 rcu_check_mb(cpu);
853 if (rcu_ctrlblk.completed == rdp->completed)
854 rcu_try_flip();
855 spin_lock_irqsave(&rdp->lock, flags);
856 RCU_TRACE_RDP(rcupreempt_trace_check_callbacks, rdp);
857 __rcu_advance_callbacks(rdp);
858 if (rdp->donelist == NULL) {
859 spin_unlock_irqrestore(&rdp->lock, flags);
860 } else {
861 spin_unlock_irqrestore(&rdp->lock, flags);
862 raise_softirq(RCU_SOFTIRQ);
867 * Needed by dynticks, to make sure all RCU processing has finished
868 * when we go idle:
870 void rcu_advance_callbacks(int cpu, int user)
872 unsigned long flags;
873 struct rcu_data *rdp = RCU_DATA_CPU(cpu);
875 if (rcu_ctrlblk.completed == rdp->completed) {
876 rcu_try_flip();
877 if (rcu_ctrlblk.completed == rdp->completed)
878 return;
880 spin_lock_irqsave(&rdp->lock, flags);
881 RCU_TRACE_RDP(rcupreempt_trace_check_callbacks, rdp);
882 __rcu_advance_callbacks(rdp);
883 spin_unlock_irqrestore(&rdp->lock, flags);
886 #ifdef CONFIG_HOTPLUG_CPU
887 #define rcu_offline_cpu_enqueue(srclist, srctail, dstlist, dsttail) do { \
888 *dsttail = srclist; \
889 if (srclist != NULL) { \
890 dsttail = srctail; \
891 srclist = NULL; \
892 srctail = &srclist;\
894 } while (0)
896 void rcu_offline_cpu(int cpu)
898 int i;
899 struct rcu_head *list = NULL;
900 unsigned long flags;
901 struct rcu_data *rdp = RCU_DATA_CPU(cpu);
902 struct rcu_head **tail = &list;
905 * Remove all callbacks from the newly dead CPU, retaining order.
906 * Otherwise rcu_barrier() will fail
909 spin_lock_irqsave(&rdp->lock, flags);
910 rcu_offline_cpu_enqueue(rdp->donelist, rdp->donetail, list, tail);
911 for (i = GP_STAGES - 1; i >= 0; i--)
912 rcu_offline_cpu_enqueue(rdp->waitlist[i], rdp->waittail[i],
913 list, tail);
914 rcu_offline_cpu_enqueue(rdp->nextlist, rdp->nexttail, list, tail);
915 spin_unlock_irqrestore(&rdp->lock, flags);
916 rdp->waitlistcount = 0;
918 /* Disengage the newly dead CPU from the grace-period computation. */
920 spin_lock_irqsave(&rcu_ctrlblk.fliplock, flags);
921 rcu_check_mb(cpu);
922 if (per_cpu(rcu_flip_flag, cpu) == rcu_flipped) {
923 smp_mb(); /* Subsequent counter accesses must see new value */
924 per_cpu(rcu_flip_flag, cpu) = rcu_flip_seen;
925 smp_mb(); /* Subsequent RCU read-side critical sections */
926 /* seen -after- acknowledgement. */
929 RCU_DATA_ME()->rcu_flipctr[0] += RCU_DATA_CPU(cpu)->rcu_flipctr[0];
930 RCU_DATA_ME()->rcu_flipctr[1] += RCU_DATA_CPU(cpu)->rcu_flipctr[1];
932 RCU_DATA_CPU(cpu)->rcu_flipctr[0] = 0;
933 RCU_DATA_CPU(cpu)->rcu_flipctr[1] = 0;
935 cpu_clear(cpu, rcu_cpu_online_map);
937 spin_unlock_irqrestore(&rcu_ctrlblk.fliplock, flags);
940 * Place the removed callbacks on the current CPU's queue.
941 * Make them all start a new grace period: simple approach,
942 * in theory could starve a given set of callbacks, but
943 * you would need to be doing some serious CPU hotplugging
944 * to make this happen. If this becomes a problem, adding
945 * a synchronize_rcu() to the hotplug path would be a simple
946 * fix.
949 <<<<<<< HEAD:kernel/rcupreempt.c
950 =======
951 local_irq_save(flags);
952 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:kernel/rcupreempt.c
953 rdp = RCU_DATA_ME();
954 <<<<<<< HEAD:kernel/rcupreempt.c
955 spin_lock_irqsave(&rdp->lock, flags);
956 =======
957 spin_lock(&rdp->lock);
958 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:kernel/rcupreempt.c
959 *rdp->nexttail = list;
960 if (list)
961 rdp->nexttail = tail;
962 spin_unlock_irqrestore(&rdp->lock, flags);
965 void __devinit rcu_online_cpu(int cpu)
967 unsigned long flags;
969 spin_lock_irqsave(&rcu_ctrlblk.fliplock, flags);
970 cpu_set(cpu, rcu_cpu_online_map);
971 spin_unlock_irqrestore(&rcu_ctrlblk.fliplock, flags);
974 #else /* #ifdef CONFIG_HOTPLUG_CPU */
976 void rcu_offline_cpu(int cpu)
980 void __devinit rcu_online_cpu(int cpu)
984 #endif /* #else #ifdef CONFIG_HOTPLUG_CPU */
986 static void rcu_process_callbacks(struct softirq_action *unused)
988 unsigned long flags;
989 struct rcu_head *next, *list;
990 <<<<<<< HEAD:kernel/rcupreempt.c
991 struct rcu_data *rdp = RCU_DATA_ME();
992 =======
993 struct rcu_data *rdp;
994 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:kernel/rcupreempt.c
996 <<<<<<< HEAD:kernel/rcupreempt.c
997 spin_lock_irqsave(&rdp->lock, flags);
998 =======
999 local_irq_save(flags);
1000 rdp = RCU_DATA_ME();
1001 spin_lock(&rdp->lock);
1002 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:kernel/rcupreempt.c
1003 list = rdp->donelist;
1004 if (list == NULL) {
1005 spin_unlock_irqrestore(&rdp->lock, flags);
1006 return;
1008 rdp->donelist = NULL;
1009 rdp->donetail = &rdp->donelist;
1010 RCU_TRACE_RDP(rcupreempt_trace_done_remove, rdp);
1011 spin_unlock_irqrestore(&rdp->lock, flags);
1012 while (list) {
1013 next = list->next;
1014 list->func(list);
1015 list = next;
1016 RCU_TRACE_ME(rcupreempt_trace_invoke);
1020 void call_rcu(struct rcu_head *head, void (*func)(struct rcu_head *rcu))
1022 unsigned long flags;
1023 struct rcu_data *rdp;
1025 head->func = func;
1026 head->next = NULL;
1027 local_irq_save(flags);
1028 rdp = RCU_DATA_ME();
1029 spin_lock(&rdp->lock);
1030 __rcu_advance_callbacks(rdp);
1031 *rdp->nexttail = head;
1032 rdp->nexttail = &head->next;
1033 RCU_TRACE_RDP(rcupreempt_trace_next_add, rdp);
1034 spin_unlock(&rdp->lock);
1035 local_irq_restore(flags);
1037 EXPORT_SYMBOL_GPL(call_rcu);
1040 * Wait until all currently running preempt_disable() code segments
1041 * (including hardware-irq-disable segments) complete. Note that
1042 * in -rt this does -not- necessarily result in all currently executing
1043 * interrupt -handlers- having completed.
1045 void __synchronize_sched(void)
1047 cpumask_t oldmask;
1048 int cpu;
1050 if (sched_getaffinity(0, &oldmask) < 0)
1051 oldmask = cpu_possible_map;
1052 for_each_online_cpu(cpu) {
1053 sched_setaffinity(0, cpumask_of_cpu(cpu));
1054 schedule();
1056 sched_setaffinity(0, oldmask);
1058 EXPORT_SYMBOL_GPL(__synchronize_sched);
1061 * Check to see if any future RCU-related work will need to be done
1062 * by the current CPU, even if none need be done immediately, returning
1063 * 1 if so. Assumes that notifiers would take care of handling any
1064 * outstanding requests from the RCU core.
1066 * This function is part of the RCU implementation; it is -not-
1067 * an exported member of the RCU API.
1069 int rcu_needs_cpu(int cpu)
1071 struct rcu_data *rdp = RCU_DATA_CPU(cpu);
1073 return (rdp->donelist != NULL ||
1074 !!rdp->waitlistcount ||
1075 rdp->nextlist != NULL);
1078 int rcu_pending(int cpu)
1080 struct rcu_data *rdp = RCU_DATA_CPU(cpu);
1082 /* The CPU has at least one callback queued somewhere. */
1084 if (rdp->donelist != NULL ||
1085 !!rdp->waitlistcount ||
1086 rdp->nextlist != NULL)
1087 return 1;
1089 /* The RCU core needs an acknowledgement from this CPU. */
1091 if ((per_cpu(rcu_flip_flag, cpu) == rcu_flipped) ||
1092 (per_cpu(rcu_mb_flag, cpu) == rcu_mb_needed))
1093 return 1;
1095 /* This CPU has fallen behind the global grace-period number. */
1097 if (rdp->completed != rcu_ctrlblk.completed)
1098 return 1;
1100 /* Nothing needed from this CPU. */
1102 return 0;
1105 static int __cpuinit rcu_cpu_notify(struct notifier_block *self,
1106 unsigned long action, void *hcpu)
1108 long cpu = (long)hcpu;
1110 switch (action) {
1111 case CPU_UP_PREPARE:
1112 case CPU_UP_PREPARE_FROZEN:
1113 rcu_online_cpu(cpu);
1114 break;
1115 case CPU_UP_CANCELED:
1116 case CPU_UP_CANCELED_FROZEN:
1117 case CPU_DEAD:
1118 case CPU_DEAD_FROZEN:
1119 rcu_offline_cpu(cpu);
1120 break;
1121 default:
1122 break;
1124 return NOTIFY_OK;
1127 static struct notifier_block __cpuinitdata rcu_nb = {
1128 .notifier_call = rcu_cpu_notify,
1131 void __init __rcu_init(void)
1133 int cpu;
1134 int i;
1135 struct rcu_data *rdp;
1137 printk(KERN_NOTICE "Preemptible RCU implementation.\n");
1138 for_each_possible_cpu(cpu) {
1139 rdp = RCU_DATA_CPU(cpu);
1140 spin_lock_init(&rdp->lock);
1141 rdp->completed = 0;
1142 rdp->waitlistcount = 0;
1143 rdp->nextlist = NULL;
1144 rdp->nexttail = &rdp->nextlist;
1145 for (i = 0; i < GP_STAGES; i++) {
1146 rdp->waitlist[i] = NULL;
1147 rdp->waittail[i] = &rdp->waitlist[i];
1149 rdp->donelist = NULL;
1150 rdp->donetail = &rdp->donelist;
1151 rdp->rcu_flipctr[0] = 0;
1152 rdp->rcu_flipctr[1] = 0;
1154 register_cpu_notifier(&rcu_nb);
1157 * We don't need protection against CPU-Hotplug here
1158 * since
1159 * a) If a CPU comes online while we are iterating over the
1160 * cpu_online_map below, we would only end up making a
1161 * duplicate call to rcu_online_cpu() which sets the corresponding
1162 * CPU's mask in the rcu_cpu_online_map.
1164 * b) A CPU cannot go offline at this point in time since the user
1165 * does not have access to the sysfs interface, nor do we
1166 * suspend the system.
1168 for_each_online_cpu(cpu)
1169 rcu_cpu_notify(&rcu_nb, CPU_UP_PREPARE, (void *)(long) cpu);
1171 open_softirq(RCU_SOFTIRQ, rcu_process_callbacks, NULL);
1175 * Deprecated, use synchronize_rcu() or synchronize_sched() instead.
1177 void synchronize_kernel(void)
1179 synchronize_rcu();
1182 #ifdef CONFIG_RCU_TRACE
1183 long *rcupreempt_flipctr(int cpu)
1185 return &RCU_DATA_CPU(cpu)->rcu_flipctr[0];
1187 EXPORT_SYMBOL_GPL(rcupreempt_flipctr);
1189 int rcupreempt_flip_flag(int cpu)
1191 return per_cpu(rcu_flip_flag, cpu);
1193 EXPORT_SYMBOL_GPL(rcupreempt_flip_flag);
1195 int rcupreempt_mb_flag(int cpu)
1197 return per_cpu(rcu_mb_flag, cpu);
1199 EXPORT_SYMBOL_GPL(rcupreempt_mb_flag);
1201 char *rcupreempt_try_flip_state_name(void)
1203 return rcu_try_flip_state_names[rcu_ctrlblk.rcu_try_flip_state];
1205 EXPORT_SYMBOL_GPL(rcupreempt_try_flip_state_name);
1207 struct rcupreempt_trace *rcupreempt_trace_cpu(int cpu)
1209 struct rcu_data *rdp = RCU_DATA_CPU(cpu);
1211 return &rdp->trace;
1213 EXPORT_SYMBOL_GPL(rcupreempt_trace_cpu);
1215 #endif /* #ifdef RCU_TRACE */