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