rcu: Prevent RCU IPI storms in presence of high call_rcu() load
[linux-2.6-block.git] / kernel / rcutree_plugin.h
CommitLineData
f41d911f
PM
1/*
2 * Read-Copy Update mechanism for mutual exclusion (tree-based version)
3 * Internal non-public definitions that provide either classic
4 * or preemptable semantics.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 *
20 * Copyright Red Hat, 2009
21 * Copyright IBM Corporation, 2009
22 *
23 * Author: Ingo Molnar <mingo@elte.hu>
24 * Paul E. McKenney <paulmck@linux.vnet.ibm.com>
25 */
26
27
28#ifdef CONFIG_TREE_PREEMPT_RCU
29
30struct rcu_state rcu_preempt_state = RCU_STATE_INITIALIZER(rcu_preempt_state);
31DEFINE_PER_CPU(struct rcu_data, rcu_preempt_data);
32
33/*
34 * Tell them what RCU they are running.
35 */
36static inline void rcu_bootup_announce(void)
37{
38 printk(KERN_INFO
39 "Experimental preemptable hierarchical RCU implementation.\n");
40}
41
42/*
43 * Return the number of RCU-preempt batches processed thus far
44 * for debug and statistics.
45 */
46long rcu_batches_completed_preempt(void)
47{
48 return rcu_preempt_state.completed;
49}
50EXPORT_SYMBOL_GPL(rcu_batches_completed_preempt);
51
52/*
53 * Return the number of RCU batches processed thus far for debug & stats.
54 */
55long rcu_batches_completed(void)
56{
57 return rcu_batches_completed_preempt();
58}
59EXPORT_SYMBOL_GPL(rcu_batches_completed);
60
61/*
62 * Record a preemptable-RCU quiescent state for the specified CPU. Note
63 * that this just means that the task currently running on the CPU is
64 * not in a quiescent state. There might be any number of tasks blocked
65 * while in an RCU read-side critical section.
66 */
c3422bea 67static void rcu_preempt_qs(int cpu)
f41d911f
PM
68{
69 struct rcu_data *rdp = &per_cpu(rcu_preempt_data, cpu);
f41d911f 70 rdp->passed_quiesc_completed = rdp->completed;
c3422bea
PM
71 barrier();
72 rdp->passed_quiesc = 1;
f41d911f
PM
73}
74
75/*
c3422bea
PM
76 * We have entered the scheduler, and the current task might soon be
77 * context-switched away from. If this task is in an RCU read-side
78 * critical section, we will no longer be able to rely on the CPU to
79 * record that fact, so we enqueue the task on the appropriate entry
80 * of the blocked_tasks[] array. The task will dequeue itself when
81 * it exits the outermost enclosing RCU read-side critical section.
82 * Therefore, the current grace period cannot be permitted to complete
83 * until the blocked_tasks[] entry indexed by the low-order bit of
84 * rnp->gpnum empties.
85 *
86 * Caller must disable preemption.
f41d911f 87 */
c3422bea 88static void rcu_preempt_note_context_switch(int cpu)
f41d911f
PM
89{
90 struct task_struct *t = current;
c3422bea 91 unsigned long flags;
f41d911f
PM
92 int phase;
93 struct rcu_data *rdp;
94 struct rcu_node *rnp;
95
96 if (t->rcu_read_lock_nesting &&
97 (t->rcu_read_unlock_special & RCU_READ_UNLOCK_BLOCKED) == 0) {
98
99 /* Possibly blocking in an RCU read-side critical section. */
100 rdp = rcu_preempt_state.rda[cpu];
101 rnp = rdp->mynode;
c3422bea 102 spin_lock_irqsave(&rnp->lock, flags);
f41d911f 103 t->rcu_read_unlock_special |= RCU_READ_UNLOCK_BLOCKED;
86848966 104 t->rcu_blocked_node = rnp;
f41d911f
PM
105
106 /*
107 * If this CPU has already checked in, then this task
108 * will hold up the next grace period rather than the
109 * current grace period. Queue the task accordingly.
110 * If the task is queued for the current grace period
111 * (i.e., this CPU has not yet passed through a quiescent
112 * state for the current grace period), then as long
113 * as that task remains queued, the current grace period
114 * cannot end.
b0e165c0
PM
115 *
116 * But first, note that the current CPU must still be
117 * on line!
f41d911f 118 */
b0e165c0 119 WARN_ON_ONCE((rdp->grpmask & rnp->qsmaskinit) == 0);
e7d8842e
PM
120 WARN_ON_ONCE(!list_empty(&t->rcu_node_entry));
121 phase = (rnp->gpnum + !(rnp->qsmask & rdp->grpmask)) & 0x1;
f41d911f 122 list_add(&t->rcu_node_entry, &rnp->blocked_tasks[phase]);
c3422bea 123 spin_unlock_irqrestore(&rnp->lock, flags);
f41d911f
PM
124 }
125
126 /*
127 * Either we were not in an RCU read-side critical section to
128 * begin with, or we have now recorded that critical section
129 * globally. Either way, we can now note a quiescent state
130 * for this CPU. Again, if we were in an RCU read-side critical
131 * section, and if that critical section was blocking the current
132 * grace period, then the fact that the task has been enqueued
133 * means that we continue to block the current grace period.
134 */
c3422bea 135 rcu_preempt_qs(cpu);
e7d8842e 136 local_irq_save(flags);
c3422bea 137 t->rcu_read_unlock_special &= ~RCU_READ_UNLOCK_NEED_QS;
e7d8842e 138 local_irq_restore(flags);
f41d911f
PM
139}
140
141/*
142 * Tree-preemptable RCU implementation for rcu_read_lock().
143 * Just increment ->rcu_read_lock_nesting, shared state will be updated
144 * if we block.
145 */
146void __rcu_read_lock(void)
147{
148 ACCESS_ONCE(current->rcu_read_lock_nesting)++;
149 barrier(); /* needed if we ever invoke rcu_read_lock in rcutree.c */
150}
151EXPORT_SYMBOL_GPL(__rcu_read_lock);
152
fc2219d4
PM
153/*
154 * Check for preempted RCU readers blocking the current grace period
155 * for the specified rcu_node structure. If the caller needs a reliable
156 * answer, it must hold the rcu_node's ->lock.
157 */
158static int rcu_preempted_readers(struct rcu_node *rnp)
159{
160 return !list_empty(&rnp->blocked_tasks[rnp->gpnum & 0x1]);
161}
162
f41d911f
PM
163static void rcu_read_unlock_special(struct task_struct *t)
164{
165 int empty;
166 unsigned long flags;
167 unsigned long mask;
168 struct rcu_node *rnp;
169 int special;
170
171 /* NMI handlers cannot block and cannot safely manipulate state. */
172 if (in_nmi())
173 return;
174
175 local_irq_save(flags);
176
177 /*
178 * If RCU core is waiting for this CPU to exit critical section,
179 * let it know that we have done so.
180 */
181 special = t->rcu_read_unlock_special;
182 if (special & RCU_READ_UNLOCK_NEED_QS) {
183 t->rcu_read_unlock_special &= ~RCU_READ_UNLOCK_NEED_QS;
c3422bea 184 rcu_preempt_qs(smp_processor_id());
f41d911f
PM
185 }
186
187 /* Hardware IRQ handlers cannot block. */
188 if (in_irq()) {
189 local_irq_restore(flags);
190 return;
191 }
192
193 /* Clean up if blocked during RCU read-side critical section. */
194 if (special & RCU_READ_UNLOCK_BLOCKED) {
195 t->rcu_read_unlock_special &= ~RCU_READ_UNLOCK_BLOCKED;
196
dd5d19ba
PM
197 /*
198 * Remove this task from the list it blocked on. The
199 * task can migrate while we acquire the lock, but at
200 * most one time. So at most two passes through loop.
201 */
202 for (;;) {
86848966 203 rnp = t->rcu_blocked_node;
e7d8842e 204 spin_lock(&rnp->lock); /* irqs already disabled. */
86848966 205 if (rnp == t->rcu_blocked_node)
dd5d19ba 206 break;
e7d8842e 207 spin_unlock(&rnp->lock); /* irqs remain disabled. */
dd5d19ba 208 }
fc2219d4 209 empty = !rcu_preempted_readers(rnp);
f41d911f 210 list_del_init(&t->rcu_node_entry);
dd5d19ba 211 t->rcu_blocked_node = NULL;
f41d911f
PM
212
213 /*
214 * If this was the last task on the current list, and if
215 * we aren't waiting on any CPUs, report the quiescent state.
216 * Note that both cpu_quiet_msk_finish() and cpu_quiet_msk()
217 * drop rnp->lock and restore irq.
218 */
219 if (!empty && rnp->qsmask == 0 &&
fc2219d4 220 !rcu_preempted_readers(rnp)) {
28ecd580
PM
221 struct rcu_node *rnp_p;
222
f41d911f
PM
223 if (rnp->parent == NULL) {
224 /* Only one rcu_node in the tree. */
225 cpu_quiet_msk_finish(&rcu_preempt_state, flags);
226 return;
227 }
228 /* Report up the rest of the hierarchy. */
229 mask = rnp->grpmask;
230 spin_unlock_irqrestore(&rnp->lock, flags);
28ecd580
PM
231 rnp_p = rnp->parent;
232 spin_lock_irqsave(&rnp_p->lock, flags);
233 WARN_ON_ONCE(rnp->qsmask);
234 cpu_quiet_msk(mask, &rcu_preempt_state, rnp_p, flags);
f41d911f
PM
235 return;
236 }
237 spin_unlock(&rnp->lock);
238 }
239 local_irq_restore(flags);
240}
241
242/*
243 * Tree-preemptable RCU implementation for rcu_read_unlock().
244 * Decrement ->rcu_read_lock_nesting. If the result is zero (outermost
245 * rcu_read_unlock()) and ->rcu_read_unlock_special is non-zero, then
246 * invoke rcu_read_unlock_special() to clean up after a context switch
247 * in an RCU read-side critical section and other special cases.
248 */
249void __rcu_read_unlock(void)
250{
251 struct task_struct *t = current;
252
253 barrier(); /* needed if we ever invoke rcu_read_unlock in rcutree.c */
254 if (--ACCESS_ONCE(t->rcu_read_lock_nesting) == 0 &&
255 unlikely(ACCESS_ONCE(t->rcu_read_unlock_special)))
256 rcu_read_unlock_special(t);
257}
258EXPORT_SYMBOL_GPL(__rcu_read_unlock);
259
260#ifdef CONFIG_RCU_CPU_STALL_DETECTOR
261
262/*
263 * Scan the current list of tasks blocked within RCU read-side critical
264 * sections, printing out the tid of each.
265 */
266static void rcu_print_task_stall(struct rcu_node *rnp)
267{
268 unsigned long flags;
269 struct list_head *lp;
fc2219d4 270 int phase;
f41d911f
PM
271 struct task_struct *t;
272
fc2219d4 273 if (rcu_preempted_readers(rnp)) {
f41d911f 274 spin_lock_irqsave(&rnp->lock, flags);
fc2219d4 275 phase = rnp->gpnum & 0x1;
f41d911f
PM
276 lp = &rnp->blocked_tasks[phase];
277 list_for_each_entry(t, lp, rcu_node_entry)
278 printk(" P%d", t->pid);
279 spin_unlock_irqrestore(&rnp->lock, flags);
280 }
281}
282
283#endif /* #ifdef CONFIG_RCU_CPU_STALL_DETECTOR */
284
b0e165c0
PM
285/*
286 * Check that the list of blocked tasks for the newly completed grace
287 * period is in fact empty. It is a serious bug to complete a grace
288 * period that still has RCU readers blocked! This function must be
289 * invoked -before- updating this rnp's ->gpnum, and the rnp's ->lock
290 * must be held by the caller.
291 */
292static void rcu_preempt_check_blocked_tasks(struct rcu_node *rnp)
293{
fc2219d4 294 WARN_ON_ONCE(rcu_preempted_readers(rnp));
28ecd580 295 WARN_ON_ONCE(rnp->qsmask);
b0e165c0
PM
296}
297
33f76148
PM
298#ifdef CONFIG_HOTPLUG_CPU
299
dd5d19ba
PM
300/*
301 * Handle tasklist migration for case in which all CPUs covered by the
302 * specified rcu_node have gone offline. Move them up to the root
303 * rcu_node. The reason for not just moving them to the immediate
304 * parent is to remove the need for rcu_read_unlock_special() to
305 * make more than two attempts to acquire the target rcu_node's lock.
306 *
307 * The caller must hold rnp->lock with irqs disabled.
308 */
309static void rcu_preempt_offline_tasks(struct rcu_state *rsp,
28ecd580
PM
310 struct rcu_node *rnp,
311 struct rcu_data *rdp)
dd5d19ba
PM
312{
313 int i;
314 struct list_head *lp;
315 struct list_head *lp_root;
316 struct rcu_node *rnp_root = rcu_get_root(rsp);
317 struct task_struct *tp;
318
86848966
PM
319 if (rnp == rnp_root) {
320 WARN_ONCE(1, "Last CPU thought to be offlined?");
dd5d19ba 321 return; /* Shouldn't happen: at least one CPU online. */
86848966 322 }
28ecd580
PM
323 WARN_ON_ONCE(rnp != rdp->mynode &&
324 (!list_empty(&rnp->blocked_tasks[0]) ||
325 !list_empty(&rnp->blocked_tasks[1])));
dd5d19ba
PM
326
327 /*
328 * Move tasks up to root rcu_node. Rely on the fact that the
329 * root rcu_node can be at most one ahead of the rest of the
330 * rcu_nodes in terms of gp_num value. This fact allows us to
331 * move the blocked_tasks[] array directly, element by element.
332 */
333 for (i = 0; i < 2; i++) {
334 lp = &rnp->blocked_tasks[i];
335 lp_root = &rnp_root->blocked_tasks[i];
336 while (!list_empty(lp)) {
337 tp = list_entry(lp->next, typeof(*tp), rcu_node_entry);
338 spin_lock(&rnp_root->lock); /* irqs already disabled */
339 list_del(&tp->rcu_node_entry);
340 tp->rcu_blocked_node = rnp_root;
341 list_add(&tp->rcu_node_entry, lp_root);
342 spin_unlock(&rnp_root->lock); /* irqs remain disabled */
343 }
344 }
345}
346
33f76148
PM
347/*
348 * Do CPU-offline processing for preemptable RCU.
349 */
350static void rcu_preempt_offline_cpu(int cpu)
351{
352 __rcu_offline_cpu(cpu, &rcu_preempt_state);
353}
354
355#endif /* #ifdef CONFIG_HOTPLUG_CPU */
356
f41d911f
PM
357/*
358 * Check for a quiescent state from the current CPU. When a task blocks,
359 * the task is recorded in the corresponding CPU's rcu_node structure,
360 * which is checked elsewhere.
361 *
362 * Caller must disable hard irqs.
363 */
364static void rcu_preempt_check_callbacks(int cpu)
365{
366 struct task_struct *t = current;
367
368 if (t->rcu_read_lock_nesting == 0) {
c3422bea
PM
369 t->rcu_read_unlock_special &= ~RCU_READ_UNLOCK_NEED_QS;
370 rcu_preempt_qs(cpu);
f41d911f
PM
371 return;
372 }
a71fca58 373 if (per_cpu(rcu_preempt_data, cpu).qs_pending)
c3422bea 374 t->rcu_read_unlock_special |= RCU_READ_UNLOCK_NEED_QS;
f41d911f
PM
375}
376
377/*
378 * Process callbacks for preemptable RCU.
379 */
380static void rcu_preempt_process_callbacks(void)
381{
382 __rcu_process_callbacks(&rcu_preempt_state,
383 &__get_cpu_var(rcu_preempt_data));
384}
385
386/*
387 * Queue a preemptable-RCU callback for invocation after a grace period.
388 */
389void call_rcu(struct rcu_head *head, void (*func)(struct rcu_head *rcu))
390{
391 __call_rcu(head, func, &rcu_preempt_state);
392}
393EXPORT_SYMBOL_GPL(call_rcu);
394
395/*
396 * Check to see if there is any immediate preemptable-RCU-related work
397 * to be done.
398 */
399static int rcu_preempt_pending(int cpu)
400{
401 return __rcu_pending(&rcu_preempt_state,
402 &per_cpu(rcu_preempt_data, cpu));
403}
404
405/*
406 * Does preemptable RCU need the CPU to stay out of dynticks mode?
407 */
408static int rcu_preempt_needs_cpu(int cpu)
409{
410 return !!per_cpu(rcu_preempt_data, cpu).nxtlist;
411}
412
e74f4c45
PM
413/**
414 * rcu_barrier - Wait until all in-flight call_rcu() callbacks complete.
415 */
416void rcu_barrier(void)
417{
418 _rcu_barrier(&rcu_preempt_state, call_rcu);
419}
420EXPORT_SYMBOL_GPL(rcu_barrier);
421
f41d911f
PM
422/*
423 * Initialize preemptable RCU's per-CPU data.
424 */
425static void __cpuinit rcu_preempt_init_percpu_data(int cpu)
426{
427 rcu_init_percpu_data(cpu, &rcu_preempt_state, 1);
428}
429
e74f4c45
PM
430/*
431 * Move preemptable RCU's callbacks to ->orphan_cbs_list.
432 */
433static void rcu_preempt_send_cbs_to_orphanage(void)
434{
435 rcu_send_cbs_to_orphanage(&rcu_preempt_state);
436}
437
1eba8f84
PM
438/*
439 * Initialize preemptable RCU's state structures.
440 */
441static void __init __rcu_init_preempt(void)
442{
1eba8f84
PM
443 RCU_INIT_FLAVOR(&rcu_preempt_state, rcu_preempt_data);
444}
445
f41d911f
PM
446/*
447 * Check for a task exiting while in a preemptable-RCU read-side
448 * critical section, clean up if so. No need to issue warnings,
449 * as debug_check_no_locks_held() already does this if lockdep
450 * is enabled.
451 */
452void exit_rcu(void)
453{
454 struct task_struct *t = current;
455
456 if (t->rcu_read_lock_nesting == 0)
457 return;
458 t->rcu_read_lock_nesting = 1;
459 rcu_read_unlock();
460}
461
462#else /* #ifdef CONFIG_TREE_PREEMPT_RCU */
463
464/*
465 * Tell them what RCU they are running.
466 */
467static inline void rcu_bootup_announce(void)
468{
469 printk(KERN_INFO "Hierarchical RCU implementation.\n");
470}
471
472/*
473 * Return the number of RCU batches processed thus far for debug & stats.
474 */
475long rcu_batches_completed(void)
476{
477 return rcu_batches_completed_sched();
478}
479EXPORT_SYMBOL_GPL(rcu_batches_completed);
480
481/*
482 * Because preemptable RCU does not exist, we never have to check for
483 * CPUs being in quiescent states.
484 */
c3422bea 485static void rcu_preempt_note_context_switch(int cpu)
f41d911f
PM
486{
487}
488
fc2219d4
PM
489/*
490 * Because preemptable RCU does not exist, there are never any preempted
491 * RCU readers.
492 */
493static int rcu_preempted_readers(struct rcu_node *rnp)
494{
495 return 0;
496}
497
f41d911f
PM
498#ifdef CONFIG_RCU_CPU_STALL_DETECTOR
499
500/*
501 * Because preemptable RCU does not exist, we never have to check for
502 * tasks blocked within RCU read-side critical sections.
503 */
504static void rcu_print_task_stall(struct rcu_node *rnp)
505{
506}
507
508#endif /* #ifdef CONFIG_RCU_CPU_STALL_DETECTOR */
509
b0e165c0
PM
510/*
511 * Because there is no preemptable RCU, there can be no readers blocked,
49e29126
PM
512 * so there is no need to check for blocked tasks. So check only for
513 * bogus qsmask values.
b0e165c0
PM
514 */
515static void rcu_preempt_check_blocked_tasks(struct rcu_node *rnp)
516{
49e29126 517 WARN_ON_ONCE(rnp->qsmask);
b0e165c0
PM
518}
519
33f76148
PM
520#ifdef CONFIG_HOTPLUG_CPU
521
dd5d19ba
PM
522/*
523 * Because preemptable RCU does not exist, it never needs to migrate
524 * tasks that were blocked within RCU read-side critical sections.
525 */
526static void rcu_preempt_offline_tasks(struct rcu_state *rsp,
28ecd580
PM
527 struct rcu_node *rnp,
528 struct rcu_data *rdp)
dd5d19ba
PM
529{
530}
531
33f76148
PM
532/*
533 * Because preemptable RCU does not exist, it never needs CPU-offline
534 * processing.
535 */
536static void rcu_preempt_offline_cpu(int cpu)
537{
538}
539
540#endif /* #ifdef CONFIG_HOTPLUG_CPU */
541
f41d911f
PM
542/*
543 * Because preemptable RCU does not exist, it never has any callbacks
544 * to check.
545 */
1eba8f84 546static void rcu_preempt_check_callbacks(int cpu)
f41d911f
PM
547{
548}
549
550/*
551 * Because preemptable RCU does not exist, it never has any callbacks
552 * to process.
553 */
1eba8f84 554static void rcu_preempt_process_callbacks(void)
f41d911f
PM
555{
556}
557
558/*
559 * In classic RCU, call_rcu() is just call_rcu_sched().
560 */
561void call_rcu(struct rcu_head *head, void (*func)(struct rcu_head *rcu))
562{
563 call_rcu_sched(head, func);
564}
565EXPORT_SYMBOL_GPL(call_rcu);
566
567/*
568 * Because preemptable RCU does not exist, it never has any work to do.
569 */
570static int rcu_preempt_pending(int cpu)
571{
572 return 0;
573}
574
575/*
576 * Because preemptable RCU does not exist, it never needs any CPU.
577 */
578static int rcu_preempt_needs_cpu(int cpu)
579{
580 return 0;
581}
582
e74f4c45
PM
583/*
584 * Because preemptable RCU does not exist, rcu_barrier() is just
585 * another name for rcu_barrier_sched().
586 */
587void rcu_barrier(void)
588{
589 rcu_barrier_sched();
590}
591EXPORT_SYMBOL_GPL(rcu_barrier);
592
f41d911f
PM
593/*
594 * Because preemptable RCU does not exist, there is no per-CPU
595 * data to initialize.
596 */
597static void __cpuinit rcu_preempt_init_percpu_data(int cpu)
598{
599}
600
e74f4c45
PM
601/*
602 * Because there is no preemptable RCU, there are no callbacks to move.
603 */
604static void rcu_preempt_send_cbs_to_orphanage(void)
605{
606}
607
1eba8f84
PM
608/*
609 * Because preemptable RCU does not exist, it need not be initialized.
610 */
611static void __init __rcu_init_preempt(void)
612{
613}
614
f41d911f 615#endif /* #else #ifdef CONFIG_TREE_PREEMPT_RCU */