]> bbs.cooldavid.org Git - net-next-2.6.git/blame - kernel/rcutree_plugin.h
rcu: disable CPU stall warnings upon panic
[net-next-2.6.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
d9a3da06 27#include <linux/delay.h>
f41d911f
PM
28
29#ifdef CONFIG_TREE_PREEMPT_RCU
30
31struct rcu_state rcu_preempt_state = RCU_STATE_INITIALIZER(rcu_preempt_state);
32DEFINE_PER_CPU(struct rcu_data, rcu_preempt_data);
33
d9a3da06
PM
34static int rcu_preempted_readers_exp(struct rcu_node *rnp);
35
f41d911f
PM
36/*
37 * Tell them what RCU they are running.
38 */
0e0fc1c2 39static void __init rcu_bootup_announce(void)
f41d911f
PM
40{
41 printk(KERN_INFO
42 "Experimental preemptable hierarchical RCU implementation.\n");
43}
44
45/*
46 * Return the number of RCU-preempt batches processed thus far
47 * for debug and statistics.
48 */
49long rcu_batches_completed_preempt(void)
50{
51 return rcu_preempt_state.completed;
52}
53EXPORT_SYMBOL_GPL(rcu_batches_completed_preempt);
54
55/*
56 * Return the number of RCU batches processed thus far for debug & stats.
57 */
58long rcu_batches_completed(void)
59{
60 return rcu_batches_completed_preempt();
61}
62EXPORT_SYMBOL_GPL(rcu_batches_completed);
63
bf66f18e
PM
64/*
65 * Force a quiescent state for preemptible RCU.
66 */
67void rcu_force_quiescent_state(void)
68{
69 force_quiescent_state(&rcu_preempt_state, 0);
70}
71EXPORT_SYMBOL_GPL(rcu_force_quiescent_state);
72
f41d911f
PM
73/*
74 * Record a preemptable-RCU quiescent state for the specified CPU. Note
75 * that this just means that the task currently running on the CPU is
76 * not in a quiescent state. There might be any number of tasks blocked
77 * while in an RCU read-side critical section.
25502a6c
PM
78 *
79 * Unlike the other rcu_*_qs() functions, callers to this function
80 * must disable irqs in order to protect the assignment to
81 * ->rcu_read_unlock_special.
f41d911f 82 */
c3422bea 83static void rcu_preempt_qs(int cpu)
f41d911f
PM
84{
85 struct rcu_data *rdp = &per_cpu(rcu_preempt_data, cpu);
25502a6c 86
c64ac3ce 87 rdp->passed_quiesc_completed = rdp->gpnum - 1;
c3422bea
PM
88 barrier();
89 rdp->passed_quiesc = 1;
25502a6c 90 current->rcu_read_unlock_special &= ~RCU_READ_UNLOCK_NEED_QS;
f41d911f
PM
91}
92
93/*
c3422bea
PM
94 * We have entered the scheduler, and the current task might soon be
95 * context-switched away from. If this task is in an RCU read-side
96 * critical section, we will no longer be able to rely on the CPU to
97 * record that fact, so we enqueue the task on the appropriate entry
98 * of the blocked_tasks[] array. The task will dequeue itself when
99 * it exits the outermost enclosing RCU read-side critical section.
100 * Therefore, the current grace period cannot be permitted to complete
101 * until the blocked_tasks[] entry indexed by the low-order bit of
102 * rnp->gpnum empties.
103 *
104 * Caller must disable preemption.
f41d911f 105 */
c3422bea 106static void rcu_preempt_note_context_switch(int cpu)
f41d911f
PM
107{
108 struct task_struct *t = current;
c3422bea 109 unsigned long flags;
f41d911f
PM
110 int phase;
111 struct rcu_data *rdp;
112 struct rcu_node *rnp;
113
114 if (t->rcu_read_lock_nesting &&
115 (t->rcu_read_unlock_special & RCU_READ_UNLOCK_BLOCKED) == 0) {
116
117 /* Possibly blocking in an RCU read-side critical section. */
118 rdp = rcu_preempt_state.rda[cpu];
119 rnp = rdp->mynode;
1304afb2 120 raw_spin_lock_irqsave(&rnp->lock, flags);
f41d911f 121 t->rcu_read_unlock_special |= RCU_READ_UNLOCK_BLOCKED;
86848966 122 t->rcu_blocked_node = rnp;
f41d911f
PM
123
124 /*
125 * If this CPU has already checked in, then this task
126 * will hold up the next grace period rather than the
127 * current grace period. Queue the task accordingly.
128 * If the task is queued for the current grace period
129 * (i.e., this CPU has not yet passed through a quiescent
130 * state for the current grace period), then as long
131 * as that task remains queued, the current grace period
132 * cannot end.
b0e165c0
PM
133 *
134 * But first, note that the current CPU must still be
135 * on line!
f41d911f 136 */
b0e165c0 137 WARN_ON_ONCE((rdp->grpmask & rnp->qsmaskinit) == 0);
e7d8842e
PM
138 WARN_ON_ONCE(!list_empty(&t->rcu_node_entry));
139 phase = (rnp->gpnum + !(rnp->qsmask & rdp->grpmask)) & 0x1;
f41d911f 140 list_add(&t->rcu_node_entry, &rnp->blocked_tasks[phase]);
1304afb2 141 raw_spin_unlock_irqrestore(&rnp->lock, flags);
f41d911f
PM
142 }
143
144 /*
145 * Either we were not in an RCU read-side critical section to
146 * begin with, or we have now recorded that critical section
147 * globally. Either way, we can now note a quiescent state
148 * for this CPU. Again, if we were in an RCU read-side critical
149 * section, and if that critical section was blocking the current
150 * grace period, then the fact that the task has been enqueued
151 * means that we continue to block the current grace period.
152 */
e7d8842e 153 local_irq_save(flags);
25502a6c 154 rcu_preempt_qs(cpu);
e7d8842e 155 local_irq_restore(flags);
f41d911f
PM
156}
157
158/*
159 * Tree-preemptable RCU implementation for rcu_read_lock().
160 * Just increment ->rcu_read_lock_nesting, shared state will be updated
161 * if we block.
162 */
163void __rcu_read_lock(void)
164{
165 ACCESS_ONCE(current->rcu_read_lock_nesting)++;
166 barrier(); /* needed if we ever invoke rcu_read_lock in rcutree.c */
167}
168EXPORT_SYMBOL_GPL(__rcu_read_lock);
169
fc2219d4
PM
170/*
171 * Check for preempted RCU readers blocking the current grace period
172 * for the specified rcu_node structure. If the caller needs a reliable
173 * answer, it must hold the rcu_node's ->lock.
174 */
175static int rcu_preempted_readers(struct rcu_node *rnp)
176{
d9a3da06
PM
177 int phase = rnp->gpnum & 0x1;
178
179 return !list_empty(&rnp->blocked_tasks[phase]) ||
180 !list_empty(&rnp->blocked_tasks[phase + 2]);
fc2219d4
PM
181}
182
b668c9cf
PM
183/*
184 * Record a quiescent state for all tasks that were previously queued
185 * on the specified rcu_node structure and that were blocking the current
186 * RCU grace period. The caller must hold the specified rnp->lock with
187 * irqs disabled, and this lock is released upon return, but irqs remain
188 * disabled.
189 */
d3f6bad3 190static void rcu_report_unblock_qs_rnp(struct rcu_node *rnp, unsigned long flags)
b668c9cf
PM
191 __releases(rnp->lock)
192{
193 unsigned long mask;
194 struct rcu_node *rnp_p;
195
196 if (rnp->qsmask != 0 || rcu_preempted_readers(rnp)) {
1304afb2 197 raw_spin_unlock_irqrestore(&rnp->lock, flags);
b668c9cf
PM
198 return; /* Still need more quiescent states! */
199 }
200
201 rnp_p = rnp->parent;
202 if (rnp_p == NULL) {
203 /*
204 * Either there is only one rcu_node in the tree,
205 * or tasks were kicked up to root rcu_node due to
206 * CPUs going offline.
207 */
d3f6bad3 208 rcu_report_qs_rsp(&rcu_preempt_state, flags);
b668c9cf
PM
209 return;
210 }
211
212 /* Report up the rest of the hierarchy. */
213 mask = rnp->grpmask;
1304afb2
PM
214 raw_spin_unlock(&rnp->lock); /* irqs remain disabled. */
215 raw_spin_lock(&rnp_p->lock); /* irqs already disabled. */
d3f6bad3 216 rcu_report_qs_rnp(mask, &rcu_preempt_state, rnp_p, flags);
b668c9cf
PM
217}
218
219/*
220 * Handle special cases during rcu_read_unlock(), such as needing to
221 * notify RCU core processing or task having blocked during the RCU
222 * read-side critical section.
223 */
f41d911f
PM
224static void rcu_read_unlock_special(struct task_struct *t)
225{
226 int empty;
d9a3da06 227 int empty_exp;
f41d911f 228 unsigned long flags;
f41d911f
PM
229 struct rcu_node *rnp;
230 int special;
231
232 /* NMI handlers cannot block and cannot safely manipulate state. */
233 if (in_nmi())
234 return;
235
236 local_irq_save(flags);
237
238 /*
239 * If RCU core is waiting for this CPU to exit critical section,
240 * let it know that we have done so.
241 */
242 special = t->rcu_read_unlock_special;
243 if (special & RCU_READ_UNLOCK_NEED_QS) {
c3422bea 244 rcu_preempt_qs(smp_processor_id());
f41d911f
PM
245 }
246
247 /* Hardware IRQ handlers cannot block. */
248 if (in_irq()) {
249 local_irq_restore(flags);
250 return;
251 }
252
253 /* Clean up if blocked during RCU read-side critical section. */
254 if (special & RCU_READ_UNLOCK_BLOCKED) {
255 t->rcu_read_unlock_special &= ~RCU_READ_UNLOCK_BLOCKED;
256
dd5d19ba
PM
257 /*
258 * Remove this task from the list it blocked on. The
259 * task can migrate while we acquire the lock, but at
260 * most one time. So at most two passes through loop.
261 */
262 for (;;) {
86848966 263 rnp = t->rcu_blocked_node;
1304afb2 264 raw_spin_lock(&rnp->lock); /* irqs already disabled. */
86848966 265 if (rnp == t->rcu_blocked_node)
dd5d19ba 266 break;
1304afb2 267 raw_spin_unlock(&rnp->lock); /* irqs remain disabled. */
dd5d19ba 268 }
fc2219d4 269 empty = !rcu_preempted_readers(rnp);
d9a3da06
PM
270 empty_exp = !rcu_preempted_readers_exp(rnp);
271 smp_mb(); /* ensure expedited fastpath sees end of RCU c-s. */
f41d911f 272 list_del_init(&t->rcu_node_entry);
dd5d19ba 273 t->rcu_blocked_node = NULL;
f41d911f
PM
274
275 /*
276 * If this was the last task on the current list, and if
277 * we aren't waiting on any CPUs, report the quiescent state.
d3f6bad3 278 * Note that rcu_report_unblock_qs_rnp() releases rnp->lock.
f41d911f 279 */
b668c9cf 280 if (empty)
1304afb2 281 raw_spin_unlock_irqrestore(&rnp->lock, flags);
b668c9cf 282 else
d3f6bad3 283 rcu_report_unblock_qs_rnp(rnp, flags);
d9a3da06
PM
284
285 /*
286 * If this was the last task on the expedited lists,
287 * then we need to report up the rcu_node hierarchy.
288 */
289 if (!empty_exp && !rcu_preempted_readers_exp(rnp))
290 rcu_report_exp_rnp(&rcu_preempt_state, rnp);
b668c9cf
PM
291 } else {
292 local_irq_restore(flags);
f41d911f 293 }
f41d911f
PM
294}
295
296/*
297 * Tree-preemptable RCU implementation for rcu_read_unlock().
298 * Decrement ->rcu_read_lock_nesting. If the result is zero (outermost
299 * rcu_read_unlock()) and ->rcu_read_unlock_special is non-zero, then
300 * invoke rcu_read_unlock_special() to clean up after a context switch
301 * in an RCU read-side critical section and other special cases.
302 */
303void __rcu_read_unlock(void)
304{
305 struct task_struct *t = current;
306
307 barrier(); /* needed if we ever invoke rcu_read_unlock in rcutree.c */
308 if (--ACCESS_ONCE(t->rcu_read_lock_nesting) == 0 &&
309 unlikely(ACCESS_ONCE(t->rcu_read_unlock_special)))
310 rcu_read_unlock_special(t);
cba8244a
PM
311#ifdef CONFIG_PROVE_LOCKING
312 WARN_ON_ONCE(ACCESS_ONCE(t->rcu_read_lock_nesting) < 0);
313#endif /* #ifdef CONFIG_PROVE_LOCKING */
f41d911f
PM
314}
315EXPORT_SYMBOL_GPL(__rcu_read_unlock);
316
317#ifdef CONFIG_RCU_CPU_STALL_DETECTOR
318
1ed509a2
PM
319#ifdef CONFIG_RCU_CPU_STALL_VERBOSE
320
321/*
322 * Dump detailed information for all tasks blocking the current RCU
323 * grace period on the specified rcu_node structure.
324 */
325static void rcu_print_detail_task_stall_rnp(struct rcu_node *rnp)
326{
327 unsigned long flags;
328 struct list_head *lp;
329 int phase;
330 struct task_struct *t;
331
332 if (rcu_preempted_readers(rnp)) {
333 raw_spin_lock_irqsave(&rnp->lock, flags);
334 phase = rnp->gpnum & 0x1;
335 lp = &rnp->blocked_tasks[phase];
336 list_for_each_entry(t, lp, rcu_node_entry)
337 sched_show_task(t);
338 raw_spin_unlock_irqrestore(&rnp->lock, flags);
339 }
340}
341
342/*
343 * Dump detailed information for all tasks blocking the current RCU
344 * grace period.
345 */
346static void rcu_print_detail_task_stall(struct rcu_state *rsp)
347{
348 struct rcu_node *rnp = rcu_get_root(rsp);
349
350 rcu_print_detail_task_stall_rnp(rnp);
351 rcu_for_each_leaf_node(rsp, rnp)
352 rcu_print_detail_task_stall_rnp(rnp);
353}
354
355#else /* #ifdef CONFIG_RCU_CPU_STALL_VERBOSE */
356
357static void rcu_print_detail_task_stall(struct rcu_state *rsp)
358{
359}
360
361#endif /* #else #ifdef CONFIG_RCU_CPU_STALL_VERBOSE */
362
f41d911f
PM
363/*
364 * Scan the current list of tasks blocked within RCU read-side critical
365 * sections, printing out the tid of each.
366 */
367static void rcu_print_task_stall(struct rcu_node *rnp)
368{
f41d911f 369 struct list_head *lp;
fc2219d4 370 int phase;
f41d911f
PM
371 struct task_struct *t;
372
fc2219d4 373 if (rcu_preempted_readers(rnp)) {
fc2219d4 374 phase = rnp->gpnum & 0x1;
f41d911f
PM
375 lp = &rnp->blocked_tasks[phase];
376 list_for_each_entry(t, lp, rcu_node_entry)
377 printk(" P%d", t->pid);
f41d911f
PM
378 }
379}
380
381#endif /* #ifdef CONFIG_RCU_CPU_STALL_DETECTOR */
382
b0e165c0
PM
383/*
384 * Check that the list of blocked tasks for the newly completed grace
385 * period is in fact empty. It is a serious bug to complete a grace
386 * period that still has RCU readers blocked! This function must be
387 * invoked -before- updating this rnp's ->gpnum, and the rnp's ->lock
388 * must be held by the caller.
389 */
390static void rcu_preempt_check_blocked_tasks(struct rcu_node *rnp)
391{
fc2219d4 392 WARN_ON_ONCE(rcu_preempted_readers(rnp));
28ecd580 393 WARN_ON_ONCE(rnp->qsmask);
b0e165c0
PM
394}
395
33f76148
PM
396#ifdef CONFIG_HOTPLUG_CPU
397
dd5d19ba
PM
398/*
399 * Handle tasklist migration for case in which all CPUs covered by the
400 * specified rcu_node have gone offline. Move them up to the root
401 * rcu_node. The reason for not just moving them to the immediate
402 * parent is to remove the need for rcu_read_unlock_special() to
403 * make more than two attempts to acquire the target rcu_node's lock.
b668c9cf
PM
404 * Returns true if there were tasks blocking the current RCU grace
405 * period.
dd5d19ba 406 *
237c80c5
PM
407 * Returns 1 if there was previously a task blocking the current grace
408 * period on the specified rcu_node structure.
409 *
dd5d19ba
PM
410 * The caller must hold rnp->lock with irqs disabled.
411 */
237c80c5
PM
412static int rcu_preempt_offline_tasks(struct rcu_state *rsp,
413 struct rcu_node *rnp,
414 struct rcu_data *rdp)
dd5d19ba
PM
415{
416 int i;
417 struct list_head *lp;
418 struct list_head *lp_root;
d9a3da06 419 int retval = 0;
dd5d19ba
PM
420 struct rcu_node *rnp_root = rcu_get_root(rsp);
421 struct task_struct *tp;
422
86848966
PM
423 if (rnp == rnp_root) {
424 WARN_ONCE(1, "Last CPU thought to be offlined?");
237c80c5 425 return 0; /* Shouldn't happen: at least one CPU online. */
86848966 426 }
28ecd580
PM
427 WARN_ON_ONCE(rnp != rdp->mynode &&
428 (!list_empty(&rnp->blocked_tasks[0]) ||
d9a3da06
PM
429 !list_empty(&rnp->blocked_tasks[1]) ||
430 !list_empty(&rnp->blocked_tasks[2]) ||
431 !list_empty(&rnp->blocked_tasks[3])));
dd5d19ba
PM
432
433 /*
434 * Move tasks up to root rcu_node. Rely on the fact that the
435 * root rcu_node can be at most one ahead of the rest of the
436 * rcu_nodes in terms of gp_num value. This fact allows us to
437 * move the blocked_tasks[] array directly, element by element.
438 */
d9a3da06
PM
439 if (rcu_preempted_readers(rnp))
440 retval |= RCU_OFL_TASKS_NORM_GP;
441 if (rcu_preempted_readers_exp(rnp))
442 retval |= RCU_OFL_TASKS_EXP_GP;
443 for (i = 0; i < 4; i++) {
dd5d19ba
PM
444 lp = &rnp->blocked_tasks[i];
445 lp_root = &rnp_root->blocked_tasks[i];
446 while (!list_empty(lp)) {
447 tp = list_entry(lp->next, typeof(*tp), rcu_node_entry);
1304afb2 448 raw_spin_lock(&rnp_root->lock); /* irqs already disabled */
dd5d19ba
PM
449 list_del(&tp->rcu_node_entry);
450 tp->rcu_blocked_node = rnp_root;
451 list_add(&tp->rcu_node_entry, lp_root);
1304afb2 452 raw_spin_unlock(&rnp_root->lock); /* irqs remain disabled */
dd5d19ba
PM
453 }
454 }
237c80c5 455 return retval;
dd5d19ba
PM
456}
457
33f76148
PM
458/*
459 * Do CPU-offline processing for preemptable RCU.
460 */
461static void rcu_preempt_offline_cpu(int cpu)
462{
463 __rcu_offline_cpu(cpu, &rcu_preempt_state);
464}
465
466#endif /* #ifdef CONFIG_HOTPLUG_CPU */
467
f41d911f
PM
468/*
469 * Check for a quiescent state from the current CPU. When a task blocks,
470 * the task is recorded in the corresponding CPU's rcu_node structure,
471 * which is checked elsewhere.
472 *
473 * Caller must disable hard irqs.
474 */
475static void rcu_preempt_check_callbacks(int cpu)
476{
477 struct task_struct *t = current;
478
479 if (t->rcu_read_lock_nesting == 0) {
c3422bea 480 rcu_preempt_qs(cpu);
f41d911f
PM
481 return;
482 }
a71fca58 483 if (per_cpu(rcu_preempt_data, cpu).qs_pending)
c3422bea 484 t->rcu_read_unlock_special |= RCU_READ_UNLOCK_NEED_QS;
f41d911f
PM
485}
486
487/*
488 * Process callbacks for preemptable RCU.
489 */
490static void rcu_preempt_process_callbacks(void)
491{
492 __rcu_process_callbacks(&rcu_preempt_state,
493 &__get_cpu_var(rcu_preempt_data));
494}
495
496/*
497 * Queue a preemptable-RCU callback for invocation after a grace period.
498 */
499void call_rcu(struct rcu_head *head, void (*func)(struct rcu_head *rcu))
500{
501 __call_rcu(head, func, &rcu_preempt_state);
502}
503EXPORT_SYMBOL_GPL(call_rcu);
504
6ebb237b
PM
505/**
506 * synchronize_rcu - wait until a grace period has elapsed.
507 *
508 * Control will return to the caller some time after a full grace
509 * period has elapsed, in other words after all currently executing RCU
510 * read-side critical sections have completed. RCU read-side critical
511 * sections are delimited by rcu_read_lock() and rcu_read_unlock(),
512 * and may be nested.
513 */
514void synchronize_rcu(void)
515{
516 struct rcu_synchronize rcu;
517
518 if (!rcu_scheduler_active)
519 return;
520
521 init_completion(&rcu.completion);
522 /* Will wake me after RCU finished. */
523 call_rcu(&rcu.head, wakeme_after_rcu);
524 /* Wait for it. */
525 wait_for_completion(&rcu.completion);
526}
527EXPORT_SYMBOL_GPL(synchronize_rcu);
528
d9a3da06
PM
529static DECLARE_WAIT_QUEUE_HEAD(sync_rcu_preempt_exp_wq);
530static long sync_rcu_preempt_exp_count;
531static DEFINE_MUTEX(sync_rcu_preempt_exp_mutex);
532
533/*
534 * Return non-zero if there are any tasks in RCU read-side critical
535 * sections blocking the current preemptible-RCU expedited grace period.
536 * If there is no preemptible-RCU expedited grace period currently in
537 * progress, returns zero unconditionally.
538 */
539static int rcu_preempted_readers_exp(struct rcu_node *rnp)
540{
541 return !list_empty(&rnp->blocked_tasks[2]) ||
542 !list_empty(&rnp->blocked_tasks[3]);
543}
544
545/*
546 * return non-zero if there is no RCU expedited grace period in progress
547 * for the specified rcu_node structure, in other words, if all CPUs and
548 * tasks covered by the specified rcu_node structure have done their bit
549 * for the current expedited grace period. Works only for preemptible
550 * RCU -- other RCU implementation use other means.
551 *
552 * Caller must hold sync_rcu_preempt_exp_mutex.
553 */
554static int sync_rcu_preempt_exp_done(struct rcu_node *rnp)
555{
556 return !rcu_preempted_readers_exp(rnp) &&
557 ACCESS_ONCE(rnp->expmask) == 0;
558}
559
560/*
561 * Report the exit from RCU read-side critical section for the last task
562 * that queued itself during or before the current expedited preemptible-RCU
563 * grace period. This event is reported either to the rcu_node structure on
564 * which the task was queued or to one of that rcu_node structure's ancestors,
565 * recursively up the tree. (Calm down, calm down, we do the recursion
566 * iteratively!)
567 *
568 * Caller must hold sync_rcu_preempt_exp_mutex.
569 */
570static void rcu_report_exp_rnp(struct rcu_state *rsp, struct rcu_node *rnp)
571{
572 unsigned long flags;
573 unsigned long mask;
574
1304afb2 575 raw_spin_lock_irqsave(&rnp->lock, flags);
d9a3da06
PM
576 for (;;) {
577 if (!sync_rcu_preempt_exp_done(rnp))
578 break;
579 if (rnp->parent == NULL) {
580 wake_up(&sync_rcu_preempt_exp_wq);
581 break;
582 }
583 mask = rnp->grpmask;
1304afb2 584 raw_spin_unlock(&rnp->lock); /* irqs remain disabled */
d9a3da06 585 rnp = rnp->parent;
1304afb2 586 raw_spin_lock(&rnp->lock); /* irqs already disabled */
d9a3da06
PM
587 rnp->expmask &= ~mask;
588 }
1304afb2 589 raw_spin_unlock_irqrestore(&rnp->lock, flags);
d9a3da06
PM
590}
591
592/*
593 * Snapshot the tasks blocking the newly started preemptible-RCU expedited
594 * grace period for the specified rcu_node structure. If there are no such
595 * tasks, report it up the rcu_node hierarchy.
596 *
597 * Caller must hold sync_rcu_preempt_exp_mutex and rsp->onofflock.
598 */
599static void
600sync_rcu_preempt_exp_init(struct rcu_state *rsp, struct rcu_node *rnp)
601{
602 int must_wait;
603
1304afb2 604 raw_spin_lock(&rnp->lock); /* irqs already disabled */
d9a3da06
PM
605 list_splice_init(&rnp->blocked_tasks[0], &rnp->blocked_tasks[2]);
606 list_splice_init(&rnp->blocked_tasks[1], &rnp->blocked_tasks[3]);
607 must_wait = rcu_preempted_readers_exp(rnp);
1304afb2 608 raw_spin_unlock(&rnp->lock); /* irqs remain disabled */
d9a3da06
PM
609 if (!must_wait)
610 rcu_report_exp_rnp(rsp, rnp);
611}
612
019129d5 613/*
d9a3da06
PM
614 * Wait for an rcu-preempt grace period, but expedite it. The basic idea
615 * is to invoke synchronize_sched_expedited() to push all the tasks to
616 * the ->blocked_tasks[] lists, move all entries from the first set of
617 * ->blocked_tasks[] lists to the second set, and finally wait for this
618 * second set to drain.
019129d5
PM
619 */
620void synchronize_rcu_expedited(void)
621{
d9a3da06
PM
622 unsigned long flags;
623 struct rcu_node *rnp;
624 struct rcu_state *rsp = &rcu_preempt_state;
625 long snap;
626 int trycount = 0;
627
628 smp_mb(); /* Caller's modifications seen first by other CPUs. */
629 snap = ACCESS_ONCE(sync_rcu_preempt_exp_count) + 1;
630 smp_mb(); /* Above access cannot bleed into critical section. */
631
632 /*
633 * Acquire lock, falling back to synchronize_rcu() if too many
634 * lock-acquisition failures. Of course, if someone does the
635 * expedited grace period for us, just leave.
636 */
637 while (!mutex_trylock(&sync_rcu_preempt_exp_mutex)) {
638 if (trycount++ < 10)
639 udelay(trycount * num_online_cpus());
640 else {
641 synchronize_rcu();
642 return;
643 }
644 if ((ACCESS_ONCE(sync_rcu_preempt_exp_count) - snap) > 0)
645 goto mb_ret; /* Others did our work for us. */
646 }
647 if ((ACCESS_ONCE(sync_rcu_preempt_exp_count) - snap) > 0)
648 goto unlock_mb_ret; /* Others did our work for us. */
649
650 /* force all RCU readers onto blocked_tasks[]. */
651 synchronize_sched_expedited();
652
1304afb2 653 raw_spin_lock_irqsave(&rsp->onofflock, flags);
d9a3da06
PM
654
655 /* Initialize ->expmask for all non-leaf rcu_node structures. */
656 rcu_for_each_nonleaf_node_breadth_first(rsp, rnp) {
1304afb2 657 raw_spin_lock(&rnp->lock); /* irqs already disabled. */
d9a3da06 658 rnp->expmask = rnp->qsmaskinit;
1304afb2 659 raw_spin_unlock(&rnp->lock); /* irqs remain disabled. */
d9a3da06
PM
660 }
661
662 /* Snapshot current state of ->blocked_tasks[] lists. */
663 rcu_for_each_leaf_node(rsp, rnp)
664 sync_rcu_preempt_exp_init(rsp, rnp);
665 if (NUM_RCU_NODES > 1)
666 sync_rcu_preempt_exp_init(rsp, rcu_get_root(rsp));
667
1304afb2 668 raw_spin_unlock_irqrestore(&rsp->onofflock, flags);
d9a3da06
PM
669
670 /* Wait for snapshotted ->blocked_tasks[] lists to drain. */
671 rnp = rcu_get_root(rsp);
672 wait_event(sync_rcu_preempt_exp_wq,
673 sync_rcu_preempt_exp_done(rnp));
674
675 /* Clean up and exit. */
676 smp_mb(); /* ensure expedited GP seen before counter increment. */
677 ACCESS_ONCE(sync_rcu_preempt_exp_count)++;
678unlock_mb_ret:
679 mutex_unlock(&sync_rcu_preempt_exp_mutex);
680mb_ret:
681 smp_mb(); /* ensure subsequent action seen after grace period. */
019129d5
PM
682}
683EXPORT_SYMBOL_GPL(synchronize_rcu_expedited);
684
f41d911f
PM
685/*
686 * Check to see if there is any immediate preemptable-RCU-related work
687 * to be done.
688 */
689static int rcu_preempt_pending(int cpu)
690{
691 return __rcu_pending(&rcu_preempt_state,
692 &per_cpu(rcu_preempt_data, cpu));
693}
694
695/*
696 * Does preemptable RCU need the CPU to stay out of dynticks mode?
697 */
698static int rcu_preempt_needs_cpu(int cpu)
699{
700 return !!per_cpu(rcu_preempt_data, cpu).nxtlist;
701}
702
e74f4c45
PM
703/**
704 * rcu_barrier - Wait until all in-flight call_rcu() callbacks complete.
705 */
706void rcu_barrier(void)
707{
708 _rcu_barrier(&rcu_preempt_state, call_rcu);
709}
710EXPORT_SYMBOL_GPL(rcu_barrier);
711
f41d911f
PM
712/*
713 * Initialize preemptable RCU's per-CPU data.
714 */
715static void __cpuinit rcu_preempt_init_percpu_data(int cpu)
716{
717 rcu_init_percpu_data(cpu, &rcu_preempt_state, 1);
718}
719
e74f4c45
PM
720/*
721 * Move preemptable RCU's callbacks to ->orphan_cbs_list.
722 */
723static void rcu_preempt_send_cbs_to_orphanage(void)
724{
725 rcu_send_cbs_to_orphanage(&rcu_preempt_state);
726}
727
1eba8f84
PM
728/*
729 * Initialize preemptable RCU's state structures.
730 */
731static void __init __rcu_init_preempt(void)
732{
1eba8f84
PM
733 RCU_INIT_FLAVOR(&rcu_preempt_state, rcu_preempt_data);
734}
735
f41d911f
PM
736/*
737 * Check for a task exiting while in a preemptable-RCU read-side
738 * critical section, clean up if so. No need to issue warnings,
739 * as debug_check_no_locks_held() already does this if lockdep
740 * is enabled.
741 */
742void exit_rcu(void)
743{
744 struct task_struct *t = current;
745
746 if (t->rcu_read_lock_nesting == 0)
747 return;
748 t->rcu_read_lock_nesting = 1;
749 rcu_read_unlock();
750}
751
752#else /* #ifdef CONFIG_TREE_PREEMPT_RCU */
753
754/*
755 * Tell them what RCU they are running.
756 */
0e0fc1c2 757static void __init rcu_bootup_announce(void)
f41d911f
PM
758{
759 printk(KERN_INFO "Hierarchical RCU implementation.\n");
760}
761
762/*
763 * Return the number of RCU batches processed thus far for debug & stats.
764 */
765long rcu_batches_completed(void)
766{
767 return rcu_batches_completed_sched();
768}
769EXPORT_SYMBOL_GPL(rcu_batches_completed);
770
bf66f18e
PM
771/*
772 * Force a quiescent state for RCU, which, because there is no preemptible
773 * RCU, becomes the same as rcu-sched.
774 */
775void rcu_force_quiescent_state(void)
776{
777 rcu_sched_force_quiescent_state();
778}
779EXPORT_SYMBOL_GPL(rcu_force_quiescent_state);
780
f41d911f
PM
781/*
782 * Because preemptable RCU does not exist, we never have to check for
783 * CPUs being in quiescent states.
784 */
c3422bea 785static void rcu_preempt_note_context_switch(int cpu)
f41d911f
PM
786{
787}
788
fc2219d4
PM
789/*
790 * Because preemptable RCU does not exist, there are never any preempted
791 * RCU readers.
792 */
793static int rcu_preempted_readers(struct rcu_node *rnp)
794{
795 return 0;
796}
797
b668c9cf
PM
798#ifdef CONFIG_HOTPLUG_CPU
799
800/* Because preemptible RCU does not exist, no quieting of tasks. */
d3f6bad3 801static void rcu_report_unblock_qs_rnp(struct rcu_node *rnp, unsigned long flags)
b668c9cf 802{
1304afb2 803 raw_spin_unlock_irqrestore(&rnp->lock, flags);
b668c9cf
PM
804}
805
806#endif /* #ifdef CONFIG_HOTPLUG_CPU */
807
f41d911f
PM
808#ifdef CONFIG_RCU_CPU_STALL_DETECTOR
809
1ed509a2
PM
810/*
811 * Because preemptable RCU does not exist, we never have to check for
812 * tasks blocked within RCU read-side critical sections.
813 */
814static void rcu_print_detail_task_stall(struct rcu_state *rsp)
815{
816}
817
f41d911f
PM
818/*
819 * Because preemptable RCU does not exist, we never have to check for
820 * tasks blocked within RCU read-side critical sections.
821 */
822static void rcu_print_task_stall(struct rcu_node *rnp)
823{
824}
825
826#endif /* #ifdef CONFIG_RCU_CPU_STALL_DETECTOR */
827
b0e165c0
PM
828/*
829 * Because there is no preemptable RCU, there can be no readers blocked,
49e29126
PM
830 * so there is no need to check for blocked tasks. So check only for
831 * bogus qsmask values.
b0e165c0
PM
832 */
833static void rcu_preempt_check_blocked_tasks(struct rcu_node *rnp)
834{
49e29126 835 WARN_ON_ONCE(rnp->qsmask);
b0e165c0
PM
836}
837
33f76148
PM
838#ifdef CONFIG_HOTPLUG_CPU
839
dd5d19ba
PM
840/*
841 * Because preemptable RCU does not exist, it never needs to migrate
237c80c5
PM
842 * tasks that were blocked within RCU read-side critical sections, and
843 * such non-existent tasks cannot possibly have been blocking the current
844 * grace period.
dd5d19ba 845 */
237c80c5
PM
846static int rcu_preempt_offline_tasks(struct rcu_state *rsp,
847 struct rcu_node *rnp,
848 struct rcu_data *rdp)
dd5d19ba 849{
237c80c5 850 return 0;
dd5d19ba
PM
851}
852
33f76148
PM
853/*
854 * Because preemptable RCU does not exist, it never needs CPU-offline
855 * processing.
856 */
857static void rcu_preempt_offline_cpu(int cpu)
858{
859}
860
861#endif /* #ifdef CONFIG_HOTPLUG_CPU */
862
f41d911f
PM
863/*
864 * Because preemptable RCU does not exist, it never has any callbacks
865 * to check.
866 */
1eba8f84 867static void rcu_preempt_check_callbacks(int cpu)
f41d911f
PM
868{
869}
870
871/*
872 * Because preemptable RCU does not exist, it never has any callbacks
873 * to process.
874 */
1eba8f84 875static void rcu_preempt_process_callbacks(void)
f41d911f
PM
876{
877}
878
879/*
880 * In classic RCU, call_rcu() is just call_rcu_sched().
881 */
882void call_rcu(struct rcu_head *head, void (*func)(struct rcu_head *rcu))
883{
884 call_rcu_sched(head, func);
885}
886EXPORT_SYMBOL_GPL(call_rcu);
887
019129d5
PM
888/*
889 * Wait for an rcu-preempt grace period, but make it happen quickly.
890 * But because preemptable RCU does not exist, map to rcu-sched.
891 */
892void synchronize_rcu_expedited(void)
893{
894 synchronize_sched_expedited();
895}
896EXPORT_SYMBOL_GPL(synchronize_rcu_expedited);
897
d9a3da06
PM
898#ifdef CONFIG_HOTPLUG_CPU
899
900/*
901 * Because preemptable RCU does not exist, there is never any need to
902 * report on tasks preempted in RCU read-side critical sections during
903 * expedited RCU grace periods.
904 */
905static void rcu_report_exp_rnp(struct rcu_state *rsp, struct rcu_node *rnp)
906{
907 return;
908}
909
910#endif /* #ifdef CONFIG_HOTPLUG_CPU */
911
f41d911f
PM
912/*
913 * Because preemptable RCU does not exist, it never has any work to do.
914 */
915static int rcu_preempt_pending(int cpu)
916{
917 return 0;
918}
919
920/*
921 * Because preemptable RCU does not exist, it never needs any CPU.
922 */
923static int rcu_preempt_needs_cpu(int cpu)
924{
925 return 0;
926}
927
e74f4c45
PM
928/*
929 * Because preemptable RCU does not exist, rcu_barrier() is just
930 * another name for rcu_barrier_sched().
931 */
932void rcu_barrier(void)
933{
934 rcu_barrier_sched();
935}
936EXPORT_SYMBOL_GPL(rcu_barrier);
937
f41d911f
PM
938/*
939 * Because preemptable RCU does not exist, there is no per-CPU
940 * data to initialize.
941 */
942static void __cpuinit rcu_preempt_init_percpu_data(int cpu)
943{
944}
945
e74f4c45
PM
946/*
947 * Because there is no preemptable RCU, there are no callbacks to move.
948 */
949static void rcu_preempt_send_cbs_to_orphanage(void)
950{
951}
952
1eba8f84
PM
953/*
954 * Because preemptable RCU does not exist, it need not be initialized.
955 */
956static void __init __rcu_init_preempt(void)
957{
958}
959
f41d911f 960#endif /* #else #ifdef CONFIG_TREE_PREEMPT_RCU */
8bd93a2c
PM
961
962#if !defined(CONFIG_RCU_FAST_NO_HZ)
963
964/*
965 * Check to see if any future RCU-related work will need to be done
966 * by the current CPU, even if none need be done immediately, returning
967 * 1 if so. This function is part of the RCU implementation; it is -not-
968 * an exported member of the RCU API.
969 *
970 * Because we have preemptible RCU, just check whether this CPU needs
971 * any flavor of RCU. Do not chew up lots of CPU cycles with preemption
972 * disabled in a most-likely vain attempt to cause RCU not to need this CPU.
973 */
974int rcu_needs_cpu(int cpu)
975{
976 return rcu_needs_cpu_quick_check(cpu);
977}
978
a47cd880
PM
979/*
980 * Check to see if we need to continue a callback-flush operations to
981 * allow the last CPU to enter dyntick-idle mode. But fast dyntick-idle
982 * entry is not configured, so we never do need to.
983 */
984static void rcu_needs_cpu_flush(void)
985{
986}
987
8bd93a2c
PM
988#else /* #if !defined(CONFIG_RCU_FAST_NO_HZ) */
989
990#define RCU_NEEDS_CPU_FLUSHES 5
a47cd880 991static DEFINE_PER_CPU(int, rcu_dyntick_drain);
71da8132 992static DEFINE_PER_CPU(unsigned long, rcu_dyntick_holdoff);
8bd93a2c
PM
993
994/*
995 * Check to see if any future RCU-related work will need to be done
996 * by the current CPU, even if none need be done immediately, returning
997 * 1 if so. This function is part of the RCU implementation; it is -not-
998 * an exported member of the RCU API.
999 *
1000 * Because we are not supporting preemptible RCU, attempt to accelerate
1001 * any current grace periods so that RCU no longer needs this CPU, but
1002 * only if all other CPUs are already in dynticks-idle mode. This will
1003 * allow the CPU cores to be powered down immediately, as opposed to after
1004 * waiting many milliseconds for grace periods to elapse.
a47cd880
PM
1005 *
1006 * Because it is not legal to invoke rcu_process_callbacks() with irqs
1007 * disabled, we do one pass of force_quiescent_state(), then do a
1008 * raise_softirq() to cause rcu_process_callbacks() to be invoked later.
1009 * The per-cpu rcu_dyntick_drain variable controls the sequencing.
8bd93a2c
PM
1010 */
1011int rcu_needs_cpu(int cpu)
1012{
a47cd880 1013 int c = 0;
8bd93a2c
PM
1014 int thatcpu;
1015
622ea685
PM
1016 /* Check for being in the holdoff period. */
1017 if (per_cpu(rcu_dyntick_holdoff, cpu) == jiffies)
1018 return rcu_needs_cpu_quick_check(cpu);
1019
8bd93a2c
PM
1020 /* Don't bother unless we are the last non-dyntick-idle CPU. */
1021 for_each_cpu_not(thatcpu, nohz_cpu_mask)
5db35673 1022 if (cpu_online(thatcpu) && thatcpu != cpu) {
a47cd880 1023 per_cpu(rcu_dyntick_drain, cpu) = 0;
71da8132 1024 per_cpu(rcu_dyntick_holdoff, cpu) = jiffies - 1;
8bd93a2c 1025 return rcu_needs_cpu_quick_check(cpu);
8bd93a2c 1026 }
a47cd880
PM
1027
1028 /* Check and update the rcu_dyntick_drain sequencing. */
1029 if (per_cpu(rcu_dyntick_drain, cpu) <= 0) {
1030 /* First time through, initialize the counter. */
1031 per_cpu(rcu_dyntick_drain, cpu) = RCU_NEEDS_CPU_FLUSHES;
1032 } else if (--per_cpu(rcu_dyntick_drain, cpu) <= 0) {
1033 /* We have hit the limit, so time to give up. */
71da8132 1034 per_cpu(rcu_dyntick_holdoff, cpu) = jiffies;
a47cd880
PM
1035 return rcu_needs_cpu_quick_check(cpu);
1036 }
1037
1038 /* Do one step pushing remaining RCU callbacks through. */
1039 if (per_cpu(rcu_sched_data, cpu).nxtlist) {
1040 rcu_sched_qs(cpu);
1041 force_quiescent_state(&rcu_sched_state, 0);
1042 c = c || per_cpu(rcu_sched_data, cpu).nxtlist;
1043 }
1044 if (per_cpu(rcu_bh_data, cpu).nxtlist) {
1045 rcu_bh_qs(cpu);
1046 force_quiescent_state(&rcu_bh_state, 0);
1047 c = c || per_cpu(rcu_bh_data, cpu).nxtlist;
8bd93a2c
PM
1048 }
1049
1050 /* If RCU callbacks are still pending, RCU still needs this CPU. */
622ea685 1051 if (c)
a47cd880 1052 raise_softirq(RCU_SOFTIRQ);
8bd93a2c
PM
1053 return c;
1054}
1055
a47cd880
PM
1056/*
1057 * Check to see if we need to continue a callback-flush operations to
1058 * allow the last CPU to enter dyntick-idle mode.
1059 */
1060static void rcu_needs_cpu_flush(void)
1061{
1062 int cpu = smp_processor_id();
71da8132 1063 unsigned long flags;
a47cd880
PM
1064
1065 if (per_cpu(rcu_dyntick_drain, cpu) <= 0)
1066 return;
71da8132 1067 local_irq_save(flags);
a47cd880 1068 (void)rcu_needs_cpu(cpu);
71da8132 1069 local_irq_restore(flags);
a47cd880
PM
1070}
1071
8bd93a2c 1072#endif /* #else #if !defined(CONFIG_RCU_FAST_NO_HZ) */