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