rcu: slim down rcutiny by removing rcu_scheduler_active and friends
[safe/jmp/linux-2.6] / kernel / rcutree_plugin.h
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 #include <linux/delay.h>
28
29 #ifdef CONFIG_TREE_PREEMPT_RCU
30
31 struct rcu_state rcu_preempt_state = RCU_STATE_INITIALIZER(rcu_preempt_state);
32 DEFINE_PER_CPU(struct rcu_data, rcu_preempt_data);
33
34 static int rcu_preempted_readers_exp(struct rcu_node *rnp);
35
36 /*
37  * Tell them what RCU they are running.
38  */
39 static void __init rcu_bootup_announce(void)
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  */
49 long rcu_batches_completed_preempt(void)
50 {
51         return rcu_preempt_state.completed;
52 }
53 EXPORT_SYMBOL_GPL(rcu_batches_completed_preempt);
54
55 /*
56  * Return the number of RCU batches processed thus far for debug & stats.
57  */
58 long rcu_batches_completed(void)
59 {
60         return rcu_batches_completed_preempt();
61 }
62 EXPORT_SYMBOL_GPL(rcu_batches_completed);
63
64 /*
65  * Force a quiescent state for preemptible RCU.
66  */
67 void rcu_force_quiescent_state(void)
68 {
69         force_quiescent_state(&rcu_preempt_state, 0);
70 }
71 EXPORT_SYMBOL_GPL(rcu_force_quiescent_state);
72
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.
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.
82  */
83 static void rcu_preempt_qs(int cpu)
84 {
85         struct rcu_data *rdp = &per_cpu(rcu_preempt_data, cpu);
86
87         rdp->passed_quiesc_completed = rdp->gpnum - 1;
88         barrier();
89         rdp->passed_quiesc = 1;
90         current->rcu_read_unlock_special &= ~RCU_READ_UNLOCK_NEED_QS;
91 }
92
93 /*
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.
105  */
106 static void rcu_preempt_note_context_switch(int cpu)
107 {
108         struct task_struct *t = current;
109         unsigned long flags;
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;
120                 raw_spin_lock_irqsave(&rnp->lock, flags);
121                 t->rcu_read_unlock_special |= RCU_READ_UNLOCK_BLOCKED;
122                 t->rcu_blocked_node = rnp;
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.
133                  *
134                  * But first, note that the current CPU must still be
135                  * on line!
136                  */
137                 WARN_ON_ONCE((rdp->grpmask & rnp->qsmaskinit) == 0);
138                 WARN_ON_ONCE(!list_empty(&t->rcu_node_entry));
139                 phase = (rnp->gpnum + !(rnp->qsmask & rdp->grpmask)) & 0x1;
140                 list_add(&t->rcu_node_entry, &rnp->blocked_tasks[phase]);
141                 raw_spin_unlock_irqrestore(&rnp->lock, flags);
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          */
153         local_irq_save(flags);
154         rcu_preempt_qs(cpu);
155         local_irq_restore(flags);
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  */
163 void __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 }
168 EXPORT_SYMBOL_GPL(__rcu_read_lock);
169
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  */
175 static int rcu_preempted_readers(struct rcu_node *rnp)
176 {
177         int phase = rnp->gpnum & 0x1;
178
179         return !list_empty(&rnp->blocked_tasks[phase]) ||
180                !list_empty(&rnp->blocked_tasks[phase + 2]);
181 }
182
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  */
190 static void rcu_report_unblock_qs_rnp(struct rcu_node *rnp, unsigned long flags)
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)) {
197                 raw_spin_unlock_irqrestore(&rnp->lock, flags);
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                  */
208                 rcu_report_qs_rsp(&rcu_preempt_state, flags);
209                 return;
210         }
211
212         /* Report up the rest of the hierarchy. */
213         mask = rnp->grpmask;
214         raw_spin_unlock(&rnp->lock);    /* irqs remain disabled. */
215         raw_spin_lock(&rnp_p->lock);    /* irqs already disabled. */
216         rcu_report_qs_rnp(mask, &rcu_preempt_state, rnp_p, flags);
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  */
224 static void rcu_read_unlock_special(struct task_struct *t)
225 {
226         int empty;
227         int empty_exp;
228         unsigned long flags;
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) {
244                 rcu_preempt_qs(smp_processor_id());
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
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 (;;) {
263                         rnp = t->rcu_blocked_node;
264                         raw_spin_lock(&rnp->lock);  /* irqs already disabled. */
265                         if (rnp == t->rcu_blocked_node)
266                                 break;
267                         raw_spin_unlock(&rnp->lock); /* irqs remain disabled. */
268                 }
269                 empty = !rcu_preempted_readers(rnp);
270                 empty_exp = !rcu_preempted_readers_exp(rnp);
271                 smp_mb(); /* ensure expedited fastpath sees end of RCU c-s. */
272                 list_del_init(&t->rcu_node_entry);
273                 t->rcu_blocked_node = NULL;
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.
278                  * Note that rcu_report_unblock_qs_rnp() releases rnp->lock.
279                  */
280                 if (empty)
281                         raw_spin_unlock_irqrestore(&rnp->lock, flags);
282                 else
283                         rcu_report_unblock_qs_rnp(rnp, flags);
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);
291         } else {
292                 local_irq_restore(flags);
293         }
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  */
303 void __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);
311 #ifdef CONFIG_PROVE_LOCKING
312         WARN_ON_ONCE(ACCESS_ONCE(t->rcu_read_lock_nesting) < 0);
313 #endif /* #ifdef CONFIG_PROVE_LOCKING */
314 }
315 EXPORT_SYMBOL_GPL(__rcu_read_unlock);
316
317 #ifdef CONFIG_RCU_CPU_STALL_DETECTOR
318
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  */
325 static 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  */
346 static 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
357 static void rcu_print_detail_task_stall(struct rcu_state *rsp)
358 {
359 }
360
361 #endif /* #else #ifdef CONFIG_RCU_CPU_STALL_VERBOSE */
362
363 /*
364  * Scan the current list of tasks blocked within RCU read-side critical
365  * sections, printing out the tid of each.
366  */
367 static void rcu_print_task_stall(struct rcu_node *rnp)
368 {
369         struct list_head *lp;
370         int phase;
371         struct task_struct *t;
372
373         if (rcu_preempted_readers(rnp)) {
374                 phase = rnp->gpnum & 0x1;
375                 lp = &rnp->blocked_tasks[phase];
376                 list_for_each_entry(t, lp, rcu_node_entry)
377                         printk(" P%d", t->pid);
378         }
379 }
380
381 #endif /* #ifdef CONFIG_RCU_CPU_STALL_DETECTOR */
382
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  */
390 static void rcu_preempt_check_blocked_tasks(struct rcu_node *rnp)
391 {
392         WARN_ON_ONCE(rcu_preempted_readers(rnp));
393         WARN_ON_ONCE(rnp->qsmask);
394 }
395
396 #ifdef CONFIG_HOTPLUG_CPU
397
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.
404  * Returns true if there were tasks blocking the current RCU grace
405  * period.
406  *
407  * Returns 1 if there was previously a task blocking the current grace
408  * period on the specified rcu_node structure.
409  *
410  * The caller must hold rnp->lock with irqs disabled.
411  */
412 static int rcu_preempt_offline_tasks(struct rcu_state *rsp,
413                                      struct rcu_node *rnp,
414                                      struct rcu_data *rdp)
415 {
416         int i;
417         struct list_head *lp;
418         struct list_head *lp_root;
419         int retval = 0;
420         struct rcu_node *rnp_root = rcu_get_root(rsp);
421         struct task_struct *tp;
422
423         if (rnp == rnp_root) {
424                 WARN_ONCE(1, "Last CPU thought to be offlined?");
425                 return 0;  /* Shouldn't happen: at least one CPU online. */
426         }
427         WARN_ON_ONCE(rnp != rdp->mynode &&
428                      (!list_empty(&rnp->blocked_tasks[0]) ||
429                       !list_empty(&rnp->blocked_tasks[1]) ||
430                       !list_empty(&rnp->blocked_tasks[2]) ||
431                       !list_empty(&rnp->blocked_tasks[3])));
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          */
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++) {
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);
448                         raw_spin_lock(&rnp_root->lock); /* irqs already disabled */
449                         list_del(&tp->rcu_node_entry);
450                         tp->rcu_blocked_node = rnp_root;
451                         list_add(&tp->rcu_node_entry, lp_root);
452                         raw_spin_unlock(&rnp_root->lock); /* irqs remain disabled */
453                 }
454         }
455         return retval;
456 }
457
458 /*
459  * Do CPU-offline processing for preemptable RCU.
460  */
461 static 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
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  */
475 static void rcu_preempt_check_callbacks(int cpu)
476 {
477         struct task_struct *t = current;
478
479         if (t->rcu_read_lock_nesting == 0) {
480                 rcu_preempt_qs(cpu);
481                 return;
482         }
483         if (per_cpu(rcu_preempt_data, cpu).qs_pending)
484                 t->rcu_read_unlock_special |= RCU_READ_UNLOCK_NEED_QS;
485 }
486
487 /*
488  * Process callbacks for preemptable RCU.
489  */
490 static 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  */
499 void call_rcu(struct rcu_head *head, void (*func)(struct rcu_head *rcu))
500 {
501         __call_rcu(head, func, &rcu_preempt_state);
502 }
503 EXPORT_SYMBOL_GPL(call_rcu);
504
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  */
514 void 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 }
527 EXPORT_SYMBOL_GPL(synchronize_rcu);
528
529 static DECLARE_WAIT_QUEUE_HEAD(sync_rcu_preempt_exp_wq);
530 static long sync_rcu_preempt_exp_count;
531 static 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  */
539 static 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  */
554 static 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  */
570 static void rcu_report_exp_rnp(struct rcu_state *rsp, struct rcu_node *rnp)
571 {
572         unsigned long flags;
573         unsigned long mask;
574
575         raw_spin_lock_irqsave(&rnp->lock, flags);
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;
584                 raw_spin_unlock(&rnp->lock); /* irqs remain disabled */
585                 rnp = rnp->parent;
586                 raw_spin_lock(&rnp->lock); /* irqs already disabled */
587                 rnp->expmask &= ~mask;
588         }
589         raw_spin_unlock_irqrestore(&rnp->lock, flags);
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  */
599 static void
600 sync_rcu_preempt_exp_init(struct rcu_state *rsp, struct rcu_node *rnp)
601 {
602         int must_wait;
603
604         raw_spin_lock(&rnp->lock); /* irqs already disabled */
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);
608         raw_spin_unlock(&rnp->lock); /* irqs remain disabled */
609         if (!must_wait)
610                 rcu_report_exp_rnp(rsp, rnp);
611 }
612
613 /*
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.
619  */
620 void synchronize_rcu_expedited(void)
621 {
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
653         raw_spin_lock_irqsave(&rsp->onofflock, flags);
654
655         /* Initialize ->expmask for all non-leaf rcu_node structures. */
656         rcu_for_each_nonleaf_node_breadth_first(rsp, rnp) {
657                 raw_spin_lock(&rnp->lock); /* irqs already disabled. */
658                 rnp->expmask = rnp->qsmaskinit;
659                 raw_spin_unlock(&rnp->lock); /* irqs remain disabled. */
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
668         raw_spin_unlock_irqrestore(&rsp->onofflock, flags);
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)++;
678 unlock_mb_ret:
679         mutex_unlock(&sync_rcu_preempt_exp_mutex);
680 mb_ret:
681         smp_mb(); /* ensure subsequent action seen after grace period. */
682 }
683 EXPORT_SYMBOL_GPL(synchronize_rcu_expedited);
684
685 /*
686  * Check to see if there is any immediate preemptable-RCU-related work
687  * to be done.
688  */
689 static 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  */
698 static int rcu_preempt_needs_cpu(int cpu)
699 {
700         return !!per_cpu(rcu_preempt_data, cpu).nxtlist;
701 }
702
703 /**
704  * rcu_barrier - Wait until all in-flight call_rcu() callbacks complete.
705  */
706 void rcu_barrier(void)
707 {
708         _rcu_barrier(&rcu_preempt_state, call_rcu);
709 }
710 EXPORT_SYMBOL_GPL(rcu_barrier);
711
712 /*
713  * Initialize preemptable RCU's per-CPU data.
714  */
715 static void __cpuinit rcu_preempt_init_percpu_data(int cpu)
716 {
717         rcu_init_percpu_data(cpu, &rcu_preempt_state, 1);
718 }
719
720 /*
721  * Move preemptable RCU's callbacks to ->orphan_cbs_list.
722  */
723 static void rcu_preempt_send_cbs_to_orphanage(void)
724 {
725         rcu_send_cbs_to_orphanage(&rcu_preempt_state);
726 }
727
728 /*
729  * Initialize preemptable RCU's state structures.
730  */
731 static void __init __rcu_init_preempt(void)
732 {
733         RCU_INIT_FLAVOR(&rcu_preempt_state, rcu_preempt_data);
734 }
735
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  */
742 void 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  */
757 static void __init rcu_bootup_announce(void)
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  */
765 long rcu_batches_completed(void)
766 {
767         return rcu_batches_completed_sched();
768 }
769 EXPORT_SYMBOL_GPL(rcu_batches_completed);
770
771 /*
772  * Force a quiescent state for RCU, which, because there is no preemptible
773  * RCU, becomes the same as rcu-sched.
774  */
775 void rcu_force_quiescent_state(void)
776 {
777         rcu_sched_force_quiescent_state();
778 }
779 EXPORT_SYMBOL_GPL(rcu_force_quiescent_state);
780
781 /*
782  * Because preemptable RCU does not exist, we never have to check for
783  * CPUs being in quiescent states.
784  */
785 static void rcu_preempt_note_context_switch(int cpu)
786 {
787 }
788
789 /*
790  * Because preemptable RCU does not exist, there are never any preempted
791  * RCU readers.
792  */
793 static int rcu_preempted_readers(struct rcu_node *rnp)
794 {
795         return 0;
796 }
797
798 #ifdef CONFIG_HOTPLUG_CPU
799
800 /* Because preemptible RCU does not exist, no quieting of tasks. */
801 static void rcu_report_unblock_qs_rnp(struct rcu_node *rnp, unsigned long flags)
802 {
803         raw_spin_unlock_irqrestore(&rnp->lock, flags);
804 }
805
806 #endif /* #ifdef CONFIG_HOTPLUG_CPU */
807
808 #ifdef CONFIG_RCU_CPU_STALL_DETECTOR
809
810 /*
811  * Because preemptable RCU does not exist, we never have to check for
812  * tasks blocked within RCU read-side critical sections.
813  */
814 static void rcu_print_detail_task_stall(struct rcu_state *rsp)
815 {
816 }
817
818 /*
819  * Because preemptable RCU does not exist, we never have to check for
820  * tasks blocked within RCU read-side critical sections.
821  */
822 static void rcu_print_task_stall(struct rcu_node *rnp)
823 {
824 }
825
826 #endif /* #ifdef CONFIG_RCU_CPU_STALL_DETECTOR */
827
828 /*
829  * Because there is no preemptable RCU, there can be no readers blocked,
830  * so there is no need to check for blocked tasks.  So check only for
831  * bogus qsmask values.
832  */
833 static void rcu_preempt_check_blocked_tasks(struct rcu_node *rnp)
834 {
835         WARN_ON_ONCE(rnp->qsmask);
836 }
837
838 #ifdef CONFIG_HOTPLUG_CPU
839
840 /*
841  * Because preemptable RCU does not exist, it never needs to migrate
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.
845  */
846 static int rcu_preempt_offline_tasks(struct rcu_state *rsp,
847                                      struct rcu_node *rnp,
848                                      struct rcu_data *rdp)
849 {
850         return 0;
851 }
852
853 /*
854  * Because preemptable RCU does not exist, it never needs CPU-offline
855  * processing.
856  */
857 static void rcu_preempt_offline_cpu(int cpu)
858 {
859 }
860
861 #endif /* #ifdef CONFIG_HOTPLUG_CPU */
862
863 /*
864  * Because preemptable RCU does not exist, it never has any callbacks
865  * to check.
866  */
867 static void rcu_preempt_check_callbacks(int cpu)
868 {
869 }
870
871 /*
872  * Because preemptable RCU does not exist, it never has any callbacks
873  * to process.
874  */
875 static void rcu_preempt_process_callbacks(void)
876 {
877 }
878
879 /*
880  * In classic RCU, call_rcu() is just call_rcu_sched().
881  */
882 void call_rcu(struct rcu_head *head, void (*func)(struct rcu_head *rcu))
883 {
884         call_rcu_sched(head, func);
885 }
886 EXPORT_SYMBOL_GPL(call_rcu);
887
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  */
892 void synchronize_rcu_expedited(void)
893 {
894         synchronize_sched_expedited();
895 }
896 EXPORT_SYMBOL_GPL(synchronize_rcu_expedited);
897
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  */
905 static 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
912 /*
913  * Because preemptable RCU does not exist, it never has any work to do.
914  */
915 static 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  */
923 static int rcu_preempt_needs_cpu(int cpu)
924 {
925         return 0;
926 }
927
928 /*
929  * Because preemptable RCU does not exist, rcu_barrier() is just
930  * another name for rcu_barrier_sched().
931  */
932 void rcu_barrier(void)
933 {
934         rcu_barrier_sched();
935 }
936 EXPORT_SYMBOL_GPL(rcu_barrier);
937
938 /*
939  * Because preemptable RCU does not exist, there is no per-CPU
940  * data to initialize.
941  */
942 static void __cpuinit rcu_preempt_init_percpu_data(int cpu)
943 {
944 }
945
946 /*
947  * Because there is no preemptable RCU, there are no callbacks to move.
948  */
949 static void rcu_preempt_send_cbs_to_orphanage(void)
950 {
951 }
952
953 /*
954  * Because preemptable RCU does not exist, it need not be initialized.
955  */
956 static void __init __rcu_init_preempt(void)
957 {
958 }
959
960 #endif /* #else #ifdef CONFIG_TREE_PREEMPT_RCU */
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  */
974 int rcu_needs_cpu(int cpu)
975 {
976         return rcu_needs_cpu_quick_check(cpu);
977 }
978
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  */
984 static void rcu_needs_cpu_flush(void)
985 {
986 }
987
988 #else /* #if !defined(CONFIG_RCU_FAST_NO_HZ) */
989
990 #define RCU_NEEDS_CPU_FLUSHES 5
991 static DEFINE_PER_CPU(int, rcu_dyntick_drain);
992 static DEFINE_PER_CPU(unsigned long, rcu_dyntick_holdoff);
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.
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.
1010  */
1011 int rcu_needs_cpu(int cpu)
1012 {
1013         int c = 0;
1014         int thatcpu;
1015
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
1020         /* Don't bother unless we are the last non-dyntick-idle CPU. */
1021         for_each_cpu_not(thatcpu, nohz_cpu_mask)
1022                 if (cpu_online(thatcpu) && thatcpu != cpu) {
1023                         per_cpu(rcu_dyntick_drain, cpu) = 0;
1024                         per_cpu(rcu_dyntick_holdoff, cpu) = jiffies - 1;
1025                         return rcu_needs_cpu_quick_check(cpu);
1026                 }
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. */
1034                 per_cpu(rcu_dyntick_holdoff, cpu) = jiffies;
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;
1048         }
1049
1050         /* If RCU callbacks are still pending, RCU still needs this CPU. */
1051         if (c)
1052                 raise_softirq(RCU_SOFTIRQ);
1053         return c;
1054 }
1055
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  */
1060 static void rcu_needs_cpu_flush(void)
1061 {
1062         int cpu = smp_processor_id();
1063         unsigned long flags;
1064
1065         if (per_cpu(rcu_dyntick_drain, cpu) <= 0)
1066                 return;
1067         local_irq_save(flags);
1068         (void)rcu_needs_cpu(cpu);
1069         local_irq_restore(flags);
1070 }
1071
1072 #endif /* #else #if !defined(CONFIG_RCU_FAST_NO_HZ) */