1 /*
2 * Read-Copy Update mechanism for mutual exclusion, realtime implementation
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, 2006
19 *
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.
25 *
26 * - Added handling of Dynamic Ticks
27 * Copyright 2007 - Paul E. Mckenney <paulmck@us.ibm.com>
28 * - Steven Rostedt <srostedt@redhat.com>
29 *
30 * Papers: http://www.rdrop.com/users/paulmck/RCU
31 *
32 * Design Document: http://lwn.net/Articles/253651/
33 *
34 * For detailed explanation of Read-Copy Update mechanism see -
35 * Documentation/RCU/ *.txt
36 *
37 */
38 #include <linux/types.h>
39 #include <linux/kernel.h>
40 #include <linux/init.h>
41 #include <linux/spinlock.h>
42 #include <linux/smp.h>
43 #include <linux/rcupdate.h>
44 #include <linux/interrupt.h>
45 #include <linux/sched.h>
46 #include <asm/atomic.h>
47 #include <linux/bitops.h>
48 #include <linux/module.h>
49 #include <linux/completion.h>
50 #include <linux/moduleparam.h>
51 #include <linux/percpu.h>
52 #include <linux/notifier.h>
53 #include <linux/rcupdate.h>
54 #include <linux/cpu.h>
55 #include <linux/random.h>
56 #include <linux/delay.h>
57 #include <linux/byteorder/swabb.h>
58 #include <linux/cpumask.h>
59 #include <linux/rcupreempt_trace.h>
60
61 /*
62 * Macro that prevents the compiler from reordering accesses, but does
63 * absolutely -nothing- to prevent CPUs from reordering. This is used
64 * only to mediate communication between mainline code and hardware
65 * interrupt and NMI handlers.
66 */
67 #define ACCESS_ONCE(x) (*(volatile typeof(x) *)&(x))
68
69 /*
70 * PREEMPT_RCU data structures.
71 */
72
73 /*
74 * GP_STAGES specifies the number of times the state machine has
75 * to go through the all the rcu_try_flip_states (see below)
76 * in a single Grace Period.
77 *
78 * GP in GP_STAGES stands for Grace Period ;)
79 */
80 #define GP_STAGES 2
81 struct rcu_data {
82 raw_spinlock_t lock; /* Protect rcu_data fields. */
83 long completed; /* Number of last completed batch. */
84 int waitlistcount;
85 struct tasklet_struct rcu_tasklet;
86 struct rcu_head *nextlist;
87 struct rcu_head **nexttail;
88 struct rcu_head *waitlist[GP_STAGES];
89 struct rcu_head **waittail[GP_STAGES];
90 struct rcu_head *donelist;
91 struct rcu_head **donetail;
92 long rcu_flipctr[2];
93 #ifdef CONFIG_RCU_TRACE
94 struct rcupreempt_trace trace;
95 #endif /* #ifdef CONFIG_RCU_TRACE */
96 };
97
98 /*
99 * States for rcu_try_flip() and friends.
100 */
101
102 enum rcu_try_flip_states {
103
104 /*
105 * Stay here if nothing is happening. Flip the counter if somthing
106 * starts happening. Denoted by "I"
107 */
108 rcu_try_flip_idle_state,
109
110 /*
111 * Wait here for all CPUs to notice that the counter has flipped. This
112 * prevents the old set of counters from ever being incremented once
113 * we leave this state, which in turn is necessary because we cannot
114 * test any individual counter for zero -- we can only check the sum.
115 * Denoted by "A".
116 */
117 rcu_try_flip_waitack_state,
118
119 /*
120 * Wait here for the sum of the old per-CPU counters to reach zero.
121 * Denoted by "Z".
122 */
123 rcu_try_flip_waitzero_state,
124
125 /*
126 * Wait here for each of the other CPUs to execute a memory barrier.
127 * This is necessary to ensure that these other CPUs really have
128 * completed executing their RCU read-side critical sections, despite
129 * their CPUs wildly reordering memory. Denoted by "M".
130 */
131 rcu_try_flip_waitmb_state,
132 };
133
134 struct rcu_ctrlblk {
135 raw_spinlock_t fliplock; /* Protect state-machine transitions. */
136 long completed; /* Number of last completed batch. */
137 enum rcu_try_flip_states rcu_try_flip_state; /* The current state of
138 the rcu state machine */
139 };
140
141 static DEFINE_PER_CPU(struct rcu_data, rcu_data);
142 static struct rcu_ctrlblk rcu_ctrlblk = {
143 .fliplock = RAW_SPIN_LOCK_UNLOCKED(rcu_ctrlblk.fliplock),
144 .completed = 0,
145 .rcu_try_flip_state = rcu_try_flip_idle_state,
146 };
147
148
149 #ifdef CONFIG_RCU_TRACE
150 static char *rcu_try_flip_state_names[] =
151 { "idle", "waitack", "waitzero", "waitmb" };
152 #endif /* #ifdef CONFIG_RCU_TRACE */
153
154 static cpumask_t rcu_cpu_online_map __read_mostly = CPU_MASK_NONE;
155
156 /*
157 * Enum and per-CPU flag to determine when each CPU has seen
158 * the most recent counter flip.
159 */
160
161 enum rcu_flip_flag_values {
162 rcu_flip_seen, /* Steady/initial state, last flip seen. */
163 /* Only GP detector can update. */
164 rcu_flipped /* Flip just completed, need confirmation. */
165 /* Only corresponding CPU can update. */
166 };
167 static DEFINE_PER_CPU_SHARED_ALIGNED(enum rcu_flip_flag_values, rcu_flip_flag)
168 = rcu_flip_seen;
169
170 /*
171 * Enum and per-CPU flag to determine when each CPU has executed the
172 * needed memory barrier to fence in memory references from its last RCU
173 * read-side critical section in the just-completed grace period.
174 */
175
176 enum rcu_mb_flag_values {
177 rcu_mb_done, /* Steady/initial state, no mb()s required. */
178 /* Only GP detector can update. */
179 rcu_mb_needed /* Flip just completed, need an mb(). */
180 /* Only corresponding CPU can update. */
181 };
182 static DEFINE_PER_CPU_SHARED_ALIGNED(enum rcu_mb_flag_values, rcu_mb_flag)
183 = rcu_mb_done;
184
185 /*
186 * RCU_DATA_ME: find the current CPU's rcu_data structure.
187 * RCU_DATA_CPU: find the specified CPU's rcu_data structure.
188 */
189 #define RCU_DATA_ME() (&__get_cpu_var(rcu_data))
190 #define RCU_DATA_CPU(cpu) (&per_cpu(rcu_data, cpu))
191
192 /*
193 * Helper macro for tracing when the appropriate rcu_data is not
194 * cached in a local variable, but where the CPU number is so cached.
195 */
196 #define RCU_TRACE_CPU(f, cpu) RCU_TRACE(f, &(RCU_DATA_CPU(cpu)->trace));
197
198 /*
199 * Helper macro for tracing when the appropriate rcu_data is not
200 * cached in a local variable.
201 */
202 #define RCU_TRACE_ME(f) RCU_TRACE(f, &(RCU_DATA_ME()->trace));
203
204 /*
205 * Helper macro for tracing when the appropriate rcu_data is pointed
206 * to by a local variable.
207 */
208 #define RCU_TRACE_RDP(f, rdp) RCU_TRACE(f, &((rdp)->trace));
209
210 /*
211 * Return the number of RCU batches processed thus far. Useful
212 * for debug and statistics.
213 */
214 long rcu_batches_completed(void)
215 {
216 return rcu_ctrlblk.completed;
217 }
218 EXPORT_SYMBOL_GPL(rcu_batches_completed);
219
220 EXPORT_SYMBOL_GPL(rcu_batches_completed_bh);
221
222 void __rcu_read_lock(void)
223 {
224 int idx;
225 struct task_struct *t = current;
226 int nesting;
227
228 nesting = ACCESS_ONCE(t->rcu_read_lock_nesting);
229 if (nesting != 0) {
230
231 /* An earlier rcu_read_lock() covers us, just count it. */
232
233 t->rcu_read_lock_nesting = nesting + 1;
234
235 } else {
236 unsigned long flags;
237
238 /*
239 * We disable interrupts for the following reasons:
240 * - If we get scheduling clock interrupt here, and we
241 * end up acking the counter flip, it's like a promise
242 * that we will never increment the old counter again.
243 * Thus we will break that promise if that
244 * scheduling clock interrupt happens between the time
245 * we pick the .completed field and the time that we
246 * increment our counter.
247 *
248 * - We don't want to be preempted out here.
249 *
250 * NMIs can still occur, of course, and might themselves
251 * contain rcu_read_lock().
252 */
253
254 local_irq_save(flags);
255
256 /*
257 * Outermost nesting of rcu_read_lock(), so increment
258 * the current counter for the current CPU. Use volatile
259 * casts to prevent the compiler from reordering.
260 */
261
262 idx = ACCESS_ONCE(rcu_ctrlblk.completed) & 0x1;
263 ACCESS_ONCE(RCU_DATA_ME()->rcu_flipctr[idx])++;
264
265 /*
266 * Now that the per-CPU counter has been incremented, we
267 * are protected from races with rcu_read_lock() invoked
268 * from NMI handlers on this CPU. We can therefore safely
269 * increment the nesting counter, relieving further NMIs
270 * of the need to increment the per-CPU counter.
271 */
272
273 ACCESS_ONCE(t->rcu_read_lock_nesting) = nesting + 1;
274
275 /*
276 * Now that we have preventing any NMIs from storing
277 * to the ->rcu_flipctr_idx, we can safely use it to
278 * remember which counter to decrement in the matching
279 * rcu_read_unlock().
280 */
281
282 ACCESS_ONCE(t->rcu_flipctr_idx) = idx;
283 local_irq_restore(flags);
284 }
285 }
286 EXPORT_SYMBOL_GPL(__rcu_read_lock);
287
288 void __rcu_read_unlock(void)
289 {
290 int idx;
291 struct task_struct *t = current;
292 int nesting;
293
294 nesting = ACCESS_ONCE(t->rcu_read_lock_nesting);
295 if (nesting > 1) {
296
297 /*
298 * We are still protected by the enclosing rcu_read_lock(),
299 * so simply decrement the counter.
300 */
301
302 t->rcu_read_lock_nesting = nesting - 1;
303
304 } else {
305 unsigned long flags;
306
307 /*
308 * Disable local interrupts to prevent the grace-period
309 * detection state machine from seeing us half-done.
310 * NMIs can still occur, of course, and might themselves
311 * contain rcu_read_lock() and rcu_read_unlock().
312 */
313
314 local_irq_save(flags);
315
316 /*
317 * Outermost nesting of rcu_read_unlock(), so we must
318 * decrement the current counter for the current CPU.
319 * This must be done carefully, because NMIs can
320 * occur at any point in this code, and any rcu_read_lock()
321 * and rcu_read_unlock() pairs in the NMI handlers
322 * must interact non-destructively with this code.
323 * Lots of volatile casts, and -very- careful ordering.
324 *
325 * Changes to this code, including this one, must be
326 * inspected, validated, and tested extremely carefully!!!
327 */
328
329 /*
330 * First, pick up the index.
331 */
332
333 idx = ACCESS_ONCE(t->rcu_flipctr_idx);
334
335 /*
336 * Now that we have fetched the counter index, it is
337 * safe to decrement the per-task RCU nesting counter.
338 * After this, any interrupts or NMIs will increment and
339 * decrement the per-CPU counters.
340 */
341 ACCESS_ONCE(t->rcu_read_lock_nesting) = nesting - 1;
342
343 /*
344 * It is now safe to decrement this task's nesting count.
345 * NMIs that occur after this statement will route their
346 * rcu_read_lock() calls through this "else" clause, and
347 * will thus start incrementing the per-CPU counter on
348 * their own. They will also clobber ->rcu_flipctr_idx,
349 * but that is OK, since we have already fetched it.
350 */
351
352 ACCESS_ONCE(RCU_DATA_ME()->rcu_flipctr[idx])--;
353 local_irq_restore(flags);
354
355 __rcu_preempt_unboost();
356 }
357 }
358 EXPORT_SYMBOL_GPL(__rcu_read_unlock);
359
360 /*
361 * If a global counter flip has occurred since the last time that we
362 * advanced callbacks, advance them. Hardware interrupts must be
363 * disabled when calling this function.
364 */
365 static void __rcu_advance_callbacks(struct rcu_data *rdp)
366 {
367 int cpu;
368 int i;
369 int wlc = 0;
370
371 if (rdp->completed != rcu_ctrlblk.completed) {
372 if (rdp->waitlist[GP_STAGES - 1] != NULL) {
373 *rdp->donetail = rdp->waitlist[GP_STAGES - 1];
374 rdp->donetail = rdp->waittail[GP_STAGES - 1];
375 RCU_TRACE_RDP(rcupreempt_trace_move2done, rdp);
376 }
377 for (i = GP_STAGES - 2; i >= 0; i--) {
378 if (rdp->waitlist[i] != NULL) {
379 rdp->waitlist[i + 1] = rdp->waitlist[i];
380 rdp->waittail[i + 1] = rdp->waittail[i];
381 wlc++;
382 } else {
383 rdp->waitlist[i + 1] = NULL;
384 rdp->waittail[i + 1] =
385 &rdp->waitlist[i + 1];
386 }
387 }
388 if (rdp->nextlist != NULL) {
389 rdp->waitlist[0] = rdp->nextlist;
390 rdp->waittail[0] = rdp->nexttail;
391 wlc++;
392 rdp->nextlist = NULL;
393 rdp->nexttail = &rdp->nextlist;
394 RCU_TRACE_RDP(rcupreempt_trace_move2wait, rdp);
395 } else {
396 rdp->waitlist[0] = NULL;
397 rdp->waittail[0] = &rdp->waitlist[0];
398 }
399 rdp->waitlistcount = wlc;
400 rdp->completed = rcu_ctrlblk.completed;
401 }
402
403 /*
404 * Check to see if this CPU needs to report that it has seen
405 * the most recent counter flip, thereby declaring that all
406 * subsequent rcu_read_lock() invocations will respect this flip.
407 */
408
409 cpu = raw_smp_processor_id();
410 if (per_cpu(rcu_flip_flag, cpu) == rcu_flipped) {
411 smp_mb(); /* Subsequent counter accesses must see new value */
412 per_cpu(rcu_flip_flag, cpu) = rcu_flip_seen;
413 smp_mb(); /* Subsequent RCU read-side critical sections */
414 /* seen -after- acknowledgement. */
415 }
416 }
417
418 #ifdef CONFIG_NO_HZ
419
420 DEFINE_PER_CPU(long, dynticks_progress_counter) = 1;
421 static DEFINE_PER_CPU(long, rcu_dyntick_snapshot);
422 static DEFINE_PER_CPU(int, rcu_update_flag);
423
424 /**
425 * rcu_irq_enter - Called from Hard irq handlers and NMI/SMI.
426 *
427 * If the CPU was idle with dynamic ticks active, this updates the
428 * dynticks_progress_counter to let the RCU handling know that the
429 * CPU is active.
430 */
431 void rcu_irq_enter(void)
432 {
433 int cpu = smp_processor_id();
434
435 if (per_cpu(rcu_update_flag, cpu))
436 per_cpu(rcu_update_flag, cpu)++;
437
438 /*
439 * Only update if we are coming from a stopped ticks mode
440 * (dynticks_progress_counter is even).
441 */
442 if (!in_interrupt() &&
443 (per_cpu(dynticks_progress_counter, cpu) & 0x1) == 0) {
444 /*
445 * The following might seem like we could have a race
446 * with NMI/SMIs. But this really isn't a problem.
447 * Here we do a read/modify/write, and the race happens
448 * when an NMI/SMI comes in after the read and before
449 * the write. But NMI/SMIs will increment this counter
450 * twice before returning, so the zero bit will not
451 * be corrupted by the NMI/SMI which is the most important
452 * part.
453 *
454 * The only thing is that we would bring back the counter
455 * to a postion that it was in during the NMI/SMI.
456 * But the zero bit would be set, so the rest of the
457 * counter would again be ignored.
458 *
459 * On return from the IRQ, the counter may have the zero
460 * bit be 0 and the counter the same as the return from
461 * the NMI/SMI. If the state machine was so unlucky to
462 * see that, it still doesn't matter, since all
463 * RCU read-side critical sections on this CPU would
464 * have already completed.
465 */
466 per_cpu(dynticks_progress_counter, cpu)++;
467 /*
468 * The following memory barrier ensures that any
469 * rcu_read_lock() primitives in the irq handler
470 * are seen by other CPUs to follow the above
471 * increment to dynticks_progress_counter. This is
472 * required in order for other CPUs to correctly
473 * determine when it is safe to advance the RCU
474 * grace-period state machine.
475 */
476 smp_mb(); /* see above block comment. */
477 /*
478 * Since we can't determine the dynamic tick mode from
479 * the dynticks_progress_counter after this routine,
480 * we use a second flag to acknowledge that we came
481 * from an idle state with ticks stopped.
482 */
483 per_cpu(rcu_update_flag, cpu)++;
484 /*
485 * If we take an NMI/SMI now, they will also increment
486 * the rcu_update_flag, and will not update the
487 * dynticks_progress_counter on exit. That is for
488 * this IRQ to do.
489 */
490 }
491 }
492
493 /**
494 * rcu_irq_exit - Called from exiting Hard irq context.
495 *
496 * If the CPU was idle with dynamic ticks active, update the
497 * dynticks_progress_counter to put let the RCU handling be
498 * aware that the CPU is going back to idle with no ticks.
499 */
500 void rcu_irq_exit(void)
501 {
502 int cpu = smp_processor_id();
503
504 /*
505 * rcu_update_flag is set if we interrupted the CPU
506 * when it was idle with ticks stopped.
507 * Once this occurs, we keep track of interrupt nesting
508 * because a NMI/SMI could also come in, and we still
509 * only want the IRQ that started the increment of the
510 * dynticks_progress_counter to be the one that modifies
511 * it on exit.
512 */
513 if (per_cpu(rcu_update_flag, cpu)) {
514 if (--per_cpu(rcu_update_flag, cpu))
515 return;
516
517 /* This must match the interrupt nesting */
518 WARN_ON(in_interrupt());
519
520 /*
521 * If an NMI/SMI happens now we are still
522 * protected by the dynticks_progress_counter being odd.
523 */
524
525 /*
526 * The following memory barrier ensures that any
527 * rcu_read_unlock() primitives in the irq handler
528 * are seen by other CPUs to preceed the following
529 * increment to dynticks_progress_counter. This
530 * is required in order for other CPUs to determine
531 * when it is safe to advance the RCU grace-period
532 * state machine.
533 */
534 smp_mb(); /* see above block comment. */
535 per_cpu(dynticks_progress_counter, cpu)++;
536 WARN_ON(per_cpu(dynticks_progress_counter, cpu) & 0x1);
537 }
538 }
539
540 static void dyntick_save_progress_counter(int cpu)
541 {
542 per_cpu(rcu_dyntick_snapshot, cpu) =
543 per_cpu(dynticks_progress_counter, cpu);
544 }
545
546 static inline int
547 rcu_try_flip_waitack_needed(int cpu)
548 {
549 long curr;
550 long snap;
551
552 curr = per_cpu(dynticks_progress_counter, cpu);
553 snap = per_cpu(rcu_dyntick_snapshot, cpu);
554 smp_mb(); /* force ordering with cpu entering/leaving dynticks. */
555
556 /*
557 * If the CPU remained in dynticks mode for the entire time
558 * and didn't take any interrupts, NMIs, SMIs, or whatever,
559 * then it cannot be in the middle of an rcu_read_lock(), so
560 * the next rcu_read_lock() it executes must use the new value
561 * of the counter. So we can safely pretend that this CPU
562 * already acknowledged the counter.
563 */
564
565 if ((curr == snap) && ((curr & 0x1) == 0))
566 return 0;
567
568 /*
569 * If the CPU passed through or entered a dynticks idle phase with
570 * no active irq handlers, then, as above, we can safely pretend
571 * that this CPU already acknowledged the counter.
572 */
573
574 if ((curr - snap) > 2 || (snap & 0x1) == 0)
575 return 0;
576
577 /* We need this CPU to explicitly acknowledge the counter flip. */
578
579 return 1;
580 }
581
582 static inline int
583 rcu_try_flip_waitmb_needed(int cpu)
584 {
585 long curr;
586 long snap;
587
588 curr = per_cpu(dynticks_progress_counter, cpu);
589 snap = per_cpu(rcu_dyntick_snapshot, cpu);
590 smp_mb(); /* force ordering with cpu entering/leaving dynticks. */
591
592 /*
593 * If the CPU remained in dynticks mode for the entire time
594 * and didn't take any interrupts, NMIs, SMIs, or whatever,
595 * then it cannot have executed an RCU read-side critical section
596 * during that time, so there is no need for it to execute a
597 * memory barrier.
598 */
599
600 if ((curr == snap) && ((curr & 0x1) == 0))
601 return 0;
602
603 /*
604 * If the CPU either entered or exited an outermost interrupt,
605 * SMI, NMI, or whatever handler, then we know that it executed
606 * a memory barrier when doing so. So we don't need another one.
607 */
608 if (curr != snap)
609 return 0;
610
611 /* We need the CPU to execute a memory barrier. */
612
613 return 1;
614 }
615
616 #else /* !CONFIG_NO_HZ */
617
618 # define dyntick_save_progress_counter(cpu) do { } while (0)
619 # define rcu_try_flip_waitack_needed(cpu) (1)
620 # define rcu_try_flip_waitmb_needed(cpu) (1)
621
622 #endif /* CONFIG_NO_HZ */
623
624 /*
625 * Get here when RCU is idle. Decide whether we need to
626 * move out of idle state, and return non-zero if so.
627 * "Straightforward" approach for the moment, might later
628 * use callback-list lengths, grace-period duration, or
629 * some such to determine when to exit idle state.
630 * Might also need a pre-idle test that does not acquire
631 * the lock, but let's get the simple case working first...
632 */
633
634 static int
635 rcu_try_flip_idle(void)
636 {
637 int cpu;
638
639 RCU_TRACE_ME(rcupreempt_trace_try_flip_i1);
640 if (!rcu_pending(smp_processor_id())) {
641 RCU_TRACE_ME(rcupreempt_trace_try_flip_ie1);
642 return 0;
643 }
644
645 /*
646 * Do the flip.
647 */
648
649 RCU_TRACE_ME(rcupreempt_trace_try_flip_g1);
650 rcu_ctrlblk.completed++; /* stands in for rcu_try_flip_g2 */
651
652 /*
653 * Need a memory barrier so that other CPUs see the new
654 * counter value before they see the subsequent change of all
655 * the rcu_flip_flag instances to rcu_flipped.
656 */
657
658 smp_mb(); /* see above block comment. */
659
660 /* Now ask each CPU for acknowledgement of the flip. */
661
662 for_each_cpu_mask(cpu, rcu_cpu_online_map) {
663 per_cpu(rcu_flip_flag, cpu) = rcu_flipped;
664 dyntick_save_progress_counter(cpu);
665 }
666
667 return 1;
668 }
669
670 /*
671 * Wait for CPUs to acknowledge the flip.
672 */
673
674 static int
675 rcu_try_flip_waitack(void)
676 {
677 int cpu;
678
679 RCU_TRACE_ME(rcupreempt_trace_try_flip_a1);
680 for_each_cpu_mask(cpu, rcu_cpu_online_map)
681 if (rcu_try_flip_waitack_needed(cpu) &&
682 per_cpu(rcu_flip_flag, cpu) != rcu_flip_seen) {
683 RCU_TRACE_ME(rcupreempt_trace_try_flip_ae1);
684 return 0;
685 }
686
687 /*
688 * Make sure our checks above don't bleed into subsequent
689 * waiting for the sum of the counters to reach zero.
690 */
691
692 smp_mb(); /* see above block comment. */
693 RCU_TRACE_ME(rcupreempt_trace_try_flip_a2);
694 return 1;
695 }
696
697 /*
698 * Wait for collective ``last'' counter to reach zero,
699 * then tell all CPUs to do an end-of-grace-period memory barrier.
700 */
701
702 static int
703 rcu_try_flip_waitzero(void)
704 {
705 int cpu;
706 int lastidx = !(rcu_ctrlblk.completed & 0x1);
707 int sum = 0;
708
709 /* Check to see if the sum of the "last" counters is zero. */
710
711 RCU_TRACE_ME(rcupreempt_trace_try_flip_z1);
712 for_each_cpu_mask(cpu, rcu_cpu_online_map)
713 sum += RCU_DATA_CPU(cpu)->rcu_flipctr[lastidx];
714 if (sum != 0) {
715 RCU_TRACE_ME(rcupreempt_trace_try_flip_ze1);
716 return 0;
717 }
718
719 /*
720 * This ensures that the other CPUs see the call for
721 * memory barriers -after- the sum to zero has been
722 * detected here
723 */
724 smp_mb(); /* ^^^^^^^^^^^^ */
725
726 /* Call for a memory barrier from each CPU. */
727 for_each_cpu_mask(cpu, rcu_cpu_online_map) {
728 per_cpu(rcu_mb_flag, cpu) = rcu_mb_needed;
729 dyntick_save_progress_counter(cpu);
730 }
731
732 RCU_TRACE_ME(rcupreempt_trace_try_flip_z2);
733 return 1;
734 }
735
736 /*
737 * Wait for all CPUs to do their end-of-grace-period memory barrier.
738 * Return 0 once all CPUs have done so.
739 */
740
741 static int
742 rcu_try_flip_waitmb(void)
743 {
744 int cpu;
745
746 RCU_TRACE_ME(rcupreempt_trace_try_flip_m1);
747 for_each_cpu_mask(cpu, rcu_cpu_online_map)
748 if (rcu_try_flip_waitmb_needed(cpu) &&
749 per_cpu(rcu_mb_flag, cpu) != rcu_mb_done) {
750 RCU_TRACE_ME(rcupreempt_trace_try_flip_me1);
751 return 0;
752 }
753
754 smp_mb(); /* Ensure that the above checks precede any following flip. */
755 RCU_TRACE_ME(rcupreempt_trace_try_flip_m2);
756 return 1;
757 }
758
759 /*
760 * Attempt a single flip of the counters. Remember, a single flip does
761 * -not- constitute a grace period. Instead, the interval between
762 * at least GP_STAGES consecutive flips is a grace period.
763 *
764 * If anyone is nuts enough to run this CONFIG_PREEMPT_RCU implementation
765 * on a large SMP, they might want to use a hierarchical organization of
766 * the per-CPU-counter pairs.
767 */
768 static void rcu_try_flip(void)
769 {
770 unsigned long flags;
771
772 RCU_TRACE_ME(rcupreempt_trace_try_flip_1);
773 if (unlikely(!spin_trylock_irqsave(&rcu_ctrlblk.fliplock, flags))) {
774 RCU_TRACE_ME(rcupreempt_trace_try_flip_e1);
775 return;
776 }
777
778 /*
779 * Take the next transition(s) through the RCU grace-period
780 * flip-counter state machine.
781 */
782
783 switch (rcu_ctrlblk.rcu_try_flip_state) {
784 case rcu_try_flip_idle_state:
785 if (rcu_try_flip_idle())
786 rcu_ctrlblk.rcu_try_flip_state =
787 rcu_try_flip_waitack_state;
788 break;
789 case rcu_try_flip_waitack_state:
790 if (rcu_try_flip_waitack())
791 rcu_ctrlblk.rcu_try_flip_state =
792 rcu_try_flip_waitzero_state;
793 break;
794 case rcu_try_flip_waitzero_state:
795 if (rcu_try_flip_waitzero())
796 rcu_ctrlblk.rcu_try_flip_state =
797 rcu_try_flip_waitmb_state;
798 break;
799 case rcu_try_flip_waitmb_state:
800 if (rcu_try_flip_waitmb())
801 rcu_ctrlblk.rcu_try_flip_state =
802 rcu_try_flip_idle_state;
803 }
804 spin_unlock_irqrestore(&rcu_ctrlblk.fliplock, flags);
805 }
806
807 /*
808 * Check to see if this CPU needs to do a memory barrier in order to
809 * ensure that any prior RCU read-side critical sections have committed
810 * their counter manipulations and critical-section memory references
811 * before declaring the grace period to be completed.
812 */
813 static void rcu_check_mb(int cpu)
814 {
815 if (per_cpu(rcu_mb_flag, cpu) == rcu_mb_needed) {
816 smp_mb(); /* Ensure RCU read-side accesses are visible. */
817 per_cpu(rcu_mb_flag, cpu) = rcu_mb_done;
818 }
819 }
820
821 void rcu_check_callbacks(int cpu, int user)
822 {
823 unsigned long flags;
824 struct rcu_data *rdp = RCU_DATA_CPU(cpu);
825
826 rcu_check_mb(cpu);
827 if (rcu_ctrlblk.completed == rdp->completed)
828 rcu_try_flip();
829 spin_lock_irqsave(&rdp->lock, flags);
830 RCU_TRACE_RDP(rcupreempt_trace_check_callbacks, rdp);
831 __rcu_advance_callbacks(rdp);
832 if (rdp->donelist == NULL) {
833 spin_unlock_irqrestore(&rdp->lock, flags);
834 } else {
835 spin_unlock_irqrestore(&rdp->lock, flags);
836 raise_softirq(RCU_SOFTIRQ);
837 }
838 }
839
840 /*
841 * Needed by dynticks, to make sure all RCU processing has finished
842 * when we go idle:
843 */
844 void rcu_advance_callbacks(int cpu, int user)
845 {
846 unsigned long flags;
847 struct rcu_data *rdp = RCU_DATA_CPU(cpu);
848
849 if (rcu_ctrlblk.completed == rdp->completed) {
850 rcu_try_flip();
851 if (rcu_ctrlblk.completed == rdp->completed)
852 return;
853 }
854 spin_lock_irqsave(&rdp->lock, flags);
855 RCU_TRACE_RDP(rcupreempt_trace_check_callbacks, rdp);
856 __rcu_advance_callbacks(rdp);
857 spin_unlock_irqrestore(&rdp->lock, flags);
858 }
859
860 #ifdef CONFIG_HOTPLUG_CPU
861 #define rcu_offline_cpu_enqueue(srclist, srctail, dstlist, dsttail) do { \
862 *dsttail = srclist; \
863 if (srclist != NULL) { \
864 dsttail = srctail; \
865 srclist = NULL; \
866 srctail = &srclist;\
867 } \
868 } while (0)
869
870 void rcu_offline_cpu(int cpu)
871 {
872 int i;
873 struct rcu_head *list = NULL;
874 unsigned long flags;
875 struct rcu_data *rdp = RCU_DATA_CPU(cpu);
876 struct rcu_head **tail = &list;
877
878 /*
879 * Remove all callbacks from the newly dead CPU, retaining order.
880 * Otherwise rcu_barrier() will fail
881 */
882
883 spin_lock_irqsave(&rdp->lock, flags);
884 rcu_offline_cpu_enqueue(rdp->donelist, rdp->donetail, list, tail);
885 for (i = GP_STAGES - 1; i >= 0; i--)
886 rcu_offline_cpu_enqueue(rdp->waitlist[i], rdp->waittail[i],
887 list, tail);
888 rcu_offline_cpu_enqueue(rdp->nextlist, rdp->nexttail, list, tail);
889 spin_unlock_irqrestore(&rdp->lock, flags);
890 rdp->waitlistcount = 0;
891
892 /* Disengage the newly dead CPU from the grace-period computation. */
893
894 spin_lock_irqsave(&rcu_ctrlblk.fliplock, flags);
895 rcu_check_mb(cpu);
896 if (per_cpu(rcu_flip_flag, cpu) == rcu_flipped) {
897 smp_mb(); /* Subsequent counter accesses must see new value */
898 per_cpu(rcu_flip_flag, cpu) = rcu_flip_seen;
899 smp_mb(); /* Subsequent RCU read-side critical sections */
900 /* seen -after- acknowledgement. */
901 }
902
903 RCU_DATA_ME()->rcu_flipctr[0] += RCU_DATA_CPU(cpu)->rcu_flipctr[0];
904 RCU_DATA_ME()->rcu_flipctr[1] += RCU_DATA_CPU(cpu)->rcu_flipctr[1];
905
906 RCU_DATA_CPU(cpu)->rcu_flipctr[0] = 0;
907 RCU_DATA_CPU(cpu)->rcu_flipctr[1] = 0;
908
909 cpu_clear(cpu, rcu_cpu_online_map);
910
911 spin_unlock_irqrestore(&rcu_ctrlblk.fliplock, flags);
912
913 /*
914 * Place the removed callbacks on the current CPU's queue.
915 * Make them all start a new grace period: simple approach,
916 * in theory could starve a given set of callbacks, but
917 * you would need to be doing some serious CPU hotplugging
918 * to make this happen. If this becomes a problem, adding
919 * a synchronize_rcu() to the hotplug path would be a simple
920 * fix.
921 */
922
923 local_irq_save(flags);
924 rdp = RCU_DATA_ME();
925 spin_lock(&rdp->lock);
926 *rdp->nexttail = list;
927 if (list)
928 rdp->nexttail = tail;
929 spin_unlock_irqrestore(&rdp->lock, flags);
930 }
931
932 void __devinit rcu_online_cpu(int cpu)
933 {
934 unsigned long flags;
935
936 spin_lock_irqsave(&rcu_ctrlblk.fliplock, flags);
937 cpu_set(cpu, rcu_cpu_online_map);
938 spin_unlock_irqrestore(&rcu_ctrlblk.fliplock, flags);
939 }
940
941 #else /* #ifdef CONFIG_HOTPLUG_CPU */
942
943 void rcu_offline_cpu(int cpu)
944 {
945 }
946
947 void __devinit rcu_online_cpu(int cpu)
948 {
949 }
950
951 #endif /* #else #ifdef CONFIG_HOTPLUG_CPU */
952
953 void rcu_process_callbacks(struct softirq_action *unused)
954 {
955 unsigned long flags;
956 struct rcu_head *next, *list;
957 struct rcu_data *rdp;
958
959 local_irq_save(flags);
960 rdp = RCU_DATA_ME();
961 spin_lock(&rdp->lock);
962 list = rdp->donelist;
963 if (list == NULL) {
964 spin_unlock_irqrestore(&rdp->lock, flags);
965 return;
966 }
967 rdp->donelist = NULL;
968 rdp->donetail = &rdp->donelist;
969 RCU_TRACE_RDP(rcupreempt_trace_done_remove, rdp);
970 spin_unlock_irqrestore(&rdp->lock, flags);
971 while (list) {
972 next = list->next;
973 list->func(list);
974 list = next;
975 RCU_TRACE_ME(rcupreempt_trace_invoke);
976 }
977 }
978
979 void call_rcu(struct rcu_head *head, void (*func)(struct rcu_head *rcu))
980 {
981 unsigned long flags;
982 struct rcu_data *rdp;
983
984 head->func = func;
985 head->next = NULL;
986 local_irq_save(flags);
987 rdp = RCU_DATA_ME();
988 spin_lock(&rdp->lock);
989 __rcu_advance_callbacks(rdp);
990 *rdp->nexttail = head;
991 rdp->nexttail = &head->next;
992 RCU_TRACE_RDP(rcupreempt_trace_next_add, rdp);
993 spin_unlock(&rdp->lock);
994 local_irq_restore(flags);
995 }
996 EXPORT_SYMBOL_GPL(call_rcu);
997
998 /*
999 * Wait until all currently running preempt_disable() code segments
1000 * (including hardware-irq-disable segments) complete. Note that
1001 * in -rt this does -not- necessarily result in all currently executing
1002 * interrupt -handlers- having completed.
1003 */
1004 void __synchronize_sched(void)
1005 {
1006 cpumask_t oldmask;
1007 int cpu;
1008
1009 if (sched_getaffinity(0, &oldmask) < 0)
1010 oldmask = cpu_possible_map;
1011 for_each_online_cpu(cpu) {
1012 sched_setaffinity(0, cpumask_of_cpu(cpu));
1013 schedule();
1014 }
1015 sched_setaffinity(0, oldmask);
1016 }
1017 EXPORT_SYMBOL_GPL(__synchronize_sched);
1018
1019 /*
1020 * Check to see if any future RCU-related work will need to be done
1021 * by the current CPU, even if none need be done immediately, returning
1022 * 1 if so. Assumes that notifiers would take care of handling any
1023 * outstanding requests from the RCU core.
1024 *
1025 * This function is part of the RCU implementation; it is -not-
1026 * an exported member of the RCU API.
1027 */
1028 int rcu_needs_cpu(int cpu)
1029 {
1030 struct rcu_data *rdp = RCU_DATA_CPU(cpu);
1031
1032 return (rdp->donelist != NULL ||
1033 !!rdp->waitlistcount ||
1034 rdp->nextlist != NULL);
1035 }
1036
1037 int rcu_pending(int cpu)
1038 {
1039 struct rcu_data *rdp = RCU_DATA_CPU(cpu);
1040
1041 /* The CPU has at least one callback queued somewhere. */
1042
1043 if (rdp->donelist != NULL ||
1044 !!rdp->waitlistcount ||
1045 rdp->nextlist != NULL)
1046 return 1;
1047
1048 /* The RCU core needs an acknowledgement from this CPU. */
1049
1050 if ((per_cpu(rcu_flip_flag, cpu) == rcu_flipped) ||
1051 (per_cpu(rcu_mb_flag, cpu) == rcu_mb_needed))
1052 return 1;
1053
1054 /* This CPU has fallen behind the global grace-period number. */
1055
1056 if (rdp->completed != rcu_ctrlblk.completed)
1057 return 1;
1058
1059 /* Nothing needed from this CPU. */
1060
1061 return 0;
1062 }
1063
1064 static int __cpuinit rcu_cpu_notify(struct notifier_block *self,
1065 unsigned long action, void *hcpu)
1066 {
1067 long cpu = (long)hcpu;
1068
1069 switch (action) {
1070 case CPU_UP_PREPARE:
1071 case CPU_UP_PREPARE_FROZEN:
1072 rcu_online_cpu(cpu);
1073 break;
1074 case CPU_UP_CANCELED:
1075 case CPU_UP_CANCELED_FROZEN:
1076 case CPU_DEAD:
1077 case CPU_DEAD_FROZEN:
1078 rcu_offline_cpu(cpu);
1079 break;
1080 default:
1081 break;
1082 }
1083 return NOTIFY_OK;
1084 }
1085
1086 static struct notifier_block __cpuinitdata rcu_nb = {
1087 .notifier_call = rcu_cpu_notify,
1088 };
1089
1090 void __init __rcu_init(void)
1091 {
1092 int cpu;
1093 int i;
1094 struct rcu_data *rdp;
1095
1096 printk(KERN_NOTICE "Preemptible RCU implementation.\n");
1097 for_each_possible_cpu(cpu) {
1098 rdp = RCU_DATA_CPU(cpu);
1099 spin_lock_init(&rdp->lock);
1100 rdp->completed = 0;
1101 rdp->waitlistcount = 0;
1102 rdp->nextlist = NULL;
1103 rdp->nexttail = &rdp->nextlist;
1104 for (i = 0; i < GP_STAGES; i++) {
1105 rdp->waitlist[i] = NULL;
1106 rdp->waittail[i] = &rdp->waitlist[i];
1107 }
1108 rdp->donelist = NULL;
1109 rdp->donetail = &rdp->donelist;
1110 rdp->rcu_flipctr[0] = 0;
1111 rdp->rcu_flipctr[1] = 0;
1112 }
1113 register_cpu_notifier(&rcu_nb);
1114
1115 /*
1116 * We don't need protection against CPU-Hotplug here
1117 * since
1118 * a) If a CPU comes online while we are iterating over the
1119 * cpu_online_map below, we would only end up making a
1120 * duplicate call to rcu_online_cpu() which sets the corresponding
1121 * CPU's mask in the rcu_cpu_online_map.
1122 *
1123 * b) A CPU cannot go offline at this point in time since the user
1124 * does not have access to the sysfs interface, nor do we
1125 * suspend the system.
1126 */
1127 for_each_online_cpu(cpu)
1128 rcu_cpu_notify(&rcu_nb, CPU_UP_PREPARE, (void *)(long) cpu);
1129
1130 open_softirq(RCU_SOFTIRQ, rcu_process_callbacks, NULL);
1131
1132 rcu_preempt_boost_init();
1133 }
1134
1135 /*
1136 * Deprecated, use synchronize_rcu() or synchronize_sched() instead.
1137 */
1138 void synchronize_kernel(void)
1139 {
1140 synchronize_rcu();
1141 }
1142
1143 #ifdef CONFIG_RCU_TRACE
1144 long *rcupreempt_flipctr(int cpu)
1145 {
1146 return &RCU_DATA_CPU(cpu)->rcu_flipctr[0];
1147 }
1148 EXPORT_SYMBOL_GPL(rcupreempt_flipctr);
1149
1150 int rcupreempt_flip_flag(int cpu)
1151 {
1152 return per_cpu(rcu_flip_flag, cpu);
1153 }
1154 EXPORT_SYMBOL_GPL(rcupreempt_flip_flag);
1155
1156 int rcupreempt_mb_flag(int cpu)
1157 {
1158 return per_cpu(rcu_mb_flag, cpu);
1159 }
1160 EXPORT_SYMBOL_GPL(rcupreempt_mb_flag);
1161
1162 char *rcupreempt_try_flip_state_name(void)
1163 {
1164 return rcu_try_flip_state_names[rcu_ctrlblk.rcu_try_flip_state];
1165 }
1166 EXPORT_SYMBOL_GPL(rcupreempt_try_flip_state_name);
1167
1168 struct rcupreempt_trace *rcupreempt_trace_cpu(int cpu)
1169 {
1170 struct rcu_data *rdp = RCU_DATA_CPU(cpu);
1171
1172 return &rdp->trace;
1173 }
1174 EXPORT_SYMBOL_GPL(rcupreempt_trace_cpu);
1175
1176 #endif /* #ifdef RCU_TRACE */
1177
|
This page was automatically generated by the
LXR engine.
|