rcu: add tracing for TINY_RCU and TINY_PREEMPT_RCU
[linux-2.6.git] / kernel / rcutiny_plugin.h
1 /*
2  * Read-Copy Update mechanism for mutual exclusion, the Bloatwatch edition
3  * Internal non-public definitions that provide either classic
4  * or preemptible 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 (c) 2010 Linaro
21  *
22  * Author: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
23  */
24
25 #include <linux/kthread.h>
26 #include <linux/debugfs.h>
27 #include <linux/seq_file.h>
28
29 #ifdef CONFIG_RCU_TRACE
30 #define RCU_TRACE(stmt) stmt
31 #else /* #ifdef CONFIG_RCU_TRACE */
32 #define RCU_TRACE(stmt)
33 #endif /* #else #ifdef CONFIG_RCU_TRACE */
34
35 /* Global control variables for rcupdate callback mechanism. */
36 struct rcu_ctrlblk {
37         struct rcu_head *rcucblist;     /* List of pending callbacks (CBs). */
38         struct rcu_head **donetail;     /* ->next pointer of last "done" CB. */
39         struct rcu_head **curtail;      /* ->next pointer of last CB. */
40         RCU_TRACE(long qlen);           /* Number of pending CBs. */
41 };
42
43 /* Definition for rcupdate control block. */
44 static struct rcu_ctrlblk rcu_sched_ctrlblk = {
45         .donetail       = &rcu_sched_ctrlblk.rcucblist,
46         .curtail        = &rcu_sched_ctrlblk.rcucblist,
47 };
48
49 static struct rcu_ctrlblk rcu_bh_ctrlblk = {
50         .donetail       = &rcu_bh_ctrlblk.rcucblist,
51         .curtail        = &rcu_bh_ctrlblk.rcucblist,
52 };
53
54 #ifdef CONFIG_DEBUG_LOCK_ALLOC
55 int rcu_scheduler_active __read_mostly;
56 EXPORT_SYMBOL_GPL(rcu_scheduler_active);
57 #endif /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
58
59 #ifdef CONFIG_TINY_PREEMPT_RCU
60
61 #include <linux/delay.h>
62
63 /* Global control variables for preemptible RCU. */
64 struct rcu_preempt_ctrlblk {
65         struct rcu_ctrlblk rcb; /* curtail: ->next ptr of last CB for GP. */
66         struct rcu_head **nexttail;
67                                 /* Tasks blocked in a preemptible RCU */
68                                 /*  read-side critical section while an */
69                                 /*  preemptible-RCU grace period is in */
70                                 /*  progress must wait for a later grace */
71                                 /*  period.  This pointer points to the */
72                                 /*  ->next pointer of the last task that */
73                                 /*  must wait for a later grace period, or */
74                                 /*  to &->rcb.rcucblist if there is no */
75                                 /*  such task. */
76         struct list_head blkd_tasks;
77                                 /* Tasks blocked in RCU read-side critical */
78                                 /*  section.  Tasks are placed at the head */
79                                 /*  of this list and age towards the tail. */
80         struct list_head *gp_tasks;
81                                 /* Pointer to the first task blocking the */
82                                 /*  current grace period, or NULL if there */
83                                 /*  is no such task. */
84         struct list_head *exp_tasks;
85                                 /* Pointer to first task blocking the */
86                                 /*  current expedited grace period, or NULL */
87                                 /*  if there is no such task.  If there */
88                                 /*  is no current expedited grace period, */
89                                 /*  then there cannot be any such task. */
90 #ifdef CONFIG_RCU_BOOST
91         struct list_head *boost_tasks;
92                                 /* Pointer to first task that needs to be */
93                                 /*  priority-boosted, or NULL if no priority */
94                                 /*  boosting is needed.  If there is no */
95                                 /*  current or expedited grace period, there */
96                                 /*  can be no such task. */
97 #endif /* #ifdef CONFIG_RCU_BOOST */
98         u8 gpnum;               /* Current grace period. */
99         u8 gpcpu;               /* Last grace period blocked by the CPU. */
100         u8 completed;           /* Last grace period completed. */
101                                 /*  If all three are equal, RCU is idle. */
102 #ifdef CONFIG_RCU_BOOST
103         s8 boosted_this_gp;     /* Has boosting already happened? */
104         unsigned long boost_time; /* When to start boosting (jiffies) */
105 #endif /* #ifdef CONFIG_RCU_BOOST */
106 #ifdef CONFIG_RCU_TRACE
107         unsigned long n_grace_periods;
108 #ifdef CONFIG_RCU_BOOST
109         unsigned long n_tasks_boosted;
110         unsigned long n_exp_boosts;
111         unsigned long n_normal_boosts;
112         unsigned long n_normal_balk_blkd_tasks;
113         unsigned long n_normal_balk_gp_tasks;
114         unsigned long n_normal_balk_boost_tasks;
115         unsigned long n_normal_balk_boosted;
116         unsigned long n_normal_balk_notyet;
117         unsigned long n_normal_balk_nos;
118         unsigned long n_exp_balk_blkd_tasks;
119         unsigned long n_exp_balk_nos;
120 #endif /* #ifdef CONFIG_RCU_BOOST */
121 #endif /* #ifdef CONFIG_RCU_TRACE */
122 };
123
124 static struct rcu_preempt_ctrlblk rcu_preempt_ctrlblk = {
125         .rcb.donetail = &rcu_preempt_ctrlblk.rcb.rcucblist,
126         .rcb.curtail = &rcu_preempt_ctrlblk.rcb.rcucblist,
127         .nexttail = &rcu_preempt_ctrlblk.rcb.rcucblist,
128         .blkd_tasks = LIST_HEAD_INIT(rcu_preempt_ctrlblk.blkd_tasks),
129 };
130
131 static int rcu_preempted_readers_exp(void);
132 static void rcu_report_exp_done(void);
133
134 /*
135  * Return true if the CPU has not yet responded to the current grace period.
136  */
137 static int rcu_cpu_blocking_cur_gp(void)
138 {
139         return rcu_preempt_ctrlblk.gpcpu != rcu_preempt_ctrlblk.gpnum;
140 }
141
142 /*
143  * Check for a running RCU reader.  Because there is only one CPU,
144  * there can be but one running RCU reader at a time.  ;-)
145  */
146 static int rcu_preempt_running_reader(void)
147 {
148         return current->rcu_read_lock_nesting;
149 }
150
151 /*
152  * Check for preempted RCU readers blocking any grace period.
153  * If the caller needs a reliable answer, it must disable hard irqs.
154  */
155 static int rcu_preempt_blocked_readers_any(void)
156 {
157         return !list_empty(&rcu_preempt_ctrlblk.blkd_tasks);
158 }
159
160 /*
161  * Check for preempted RCU readers blocking the current grace period.
162  * If the caller needs a reliable answer, it must disable hard irqs.
163  */
164 static int rcu_preempt_blocked_readers_cgp(void)
165 {
166         return rcu_preempt_ctrlblk.gp_tasks != NULL;
167 }
168
169 /*
170  * Return true if another preemptible-RCU grace period is needed.
171  */
172 static int rcu_preempt_needs_another_gp(void)
173 {
174         return *rcu_preempt_ctrlblk.rcb.curtail != NULL;
175 }
176
177 /*
178  * Return true if a preemptible-RCU grace period is in progress.
179  * The caller must disable hardirqs.
180  */
181 static int rcu_preempt_gp_in_progress(void)
182 {
183         return rcu_preempt_ctrlblk.completed != rcu_preempt_ctrlblk.gpnum;
184 }
185
186 /*
187  * Advance a ->blkd_tasks-list pointer to the next entry, instead
188  * returning NULL if at the end of the list.
189  */
190 static struct list_head *rcu_next_node_entry(struct task_struct *t)
191 {
192         struct list_head *np;
193
194         np = t->rcu_node_entry.next;
195         if (np == &rcu_preempt_ctrlblk.blkd_tasks)
196                 np = NULL;
197         return np;
198 }
199
200 #ifdef CONFIG_RCU_TRACE
201
202 #ifdef CONFIG_RCU_BOOST
203 static void rcu_initiate_boost_trace(void);
204 static void rcu_initiate_exp_boost_trace(void);
205 #endif /* #ifdef CONFIG_RCU_BOOST */
206
207 /*
208  * Dump additional statistice for TINY_PREEMPT_RCU.
209  */
210 static void show_tiny_preempt_stats(struct seq_file *m)
211 {
212         seq_printf(m, "rcu_preempt: qlen=%ld gp=%lu g%u/p%u/c%u tasks=%c%c%c\n",
213                    rcu_preempt_ctrlblk.rcb.qlen,
214                    rcu_preempt_ctrlblk.n_grace_periods,
215                    rcu_preempt_ctrlblk.gpnum,
216                    rcu_preempt_ctrlblk.gpcpu,
217                    rcu_preempt_ctrlblk.completed,
218                    "T."[list_empty(&rcu_preempt_ctrlblk.blkd_tasks)],
219                    "N."[!rcu_preempt_ctrlblk.gp_tasks],
220                    "E."[!rcu_preempt_ctrlblk.exp_tasks]);
221 #ifdef CONFIG_RCU_BOOST
222         seq_printf(m, "             ttb=%c btg=",
223                    "B."[!rcu_preempt_ctrlblk.boost_tasks]);
224         switch (rcu_preempt_ctrlblk.boosted_this_gp) {
225         case -1:
226                 seq_puts(m, "exp");
227                 break;
228         case 0:
229                 seq_puts(m, "no");
230                 break;
231         case 1:
232                 seq_puts(m, "done");
233                 break;
234         default:
235                 seq_printf(m, "?%d?", rcu_preempt_ctrlblk.boosted_this_gp);
236         }
237         seq_printf(m, " ntb=%lu neb=%lu nnb=%lu j=%04x bt=%04x\n",
238                    rcu_preempt_ctrlblk.n_tasks_boosted,
239                    rcu_preempt_ctrlblk.n_exp_boosts,
240                    rcu_preempt_ctrlblk.n_normal_boosts,
241                    (int)(jiffies & 0xffff),
242                    (int)(rcu_preempt_ctrlblk.boost_time & 0xffff));
243         seq_printf(m, "             %s: nt=%lu gt=%lu bt=%lu b=%lu ny=%lu nos=%lu\n",
244                    "normal balk",
245                    rcu_preempt_ctrlblk.n_normal_balk_blkd_tasks,
246                    rcu_preempt_ctrlblk.n_normal_balk_gp_tasks,
247                    rcu_preempt_ctrlblk.n_normal_balk_boost_tasks,
248                    rcu_preempt_ctrlblk.n_normal_balk_boosted,
249                    rcu_preempt_ctrlblk.n_normal_balk_notyet,
250                    rcu_preempt_ctrlblk.n_normal_balk_nos);
251         seq_printf(m, "             exp balk: bt=%lu nos=%lu\n",
252                    rcu_preempt_ctrlblk.n_exp_balk_blkd_tasks,
253                    rcu_preempt_ctrlblk.n_exp_balk_nos);
254 #endif /* #ifdef CONFIG_RCU_BOOST */
255 }
256
257 #endif /* #ifdef CONFIG_RCU_TRACE */
258
259 #ifdef CONFIG_RCU_BOOST
260
261 #include "rtmutex_common.h"
262
263 /*
264  * Carry out RCU priority boosting on the task indicated by ->boost_tasks,
265  * and advance ->boost_tasks to the next task in the ->blkd_tasks list.
266  */
267 static int rcu_boost(void)
268 {
269         unsigned long flags;
270         struct rt_mutex mtx;
271         struct list_head *np;
272         struct task_struct *t;
273
274         if (rcu_preempt_ctrlblk.boost_tasks == NULL)
275                 return 0;  /* Nothing to boost. */
276         raw_local_irq_save(flags);
277         rcu_preempt_ctrlblk.boosted_this_gp++;
278         t = container_of(rcu_preempt_ctrlblk.boost_tasks, struct task_struct,
279                          rcu_node_entry);
280         np = rcu_next_node_entry(t);
281         rt_mutex_init_proxy_locked(&mtx, t);
282         t->rcu_boost_mutex = &mtx;
283         t->rcu_read_unlock_special |= RCU_READ_UNLOCK_BOOSTED;
284         raw_local_irq_restore(flags);
285         rt_mutex_lock(&mtx);
286         RCU_TRACE(rcu_preempt_ctrlblk.n_tasks_boosted++);
287         rt_mutex_unlock(&mtx);
288         return rcu_preempt_ctrlblk.boost_tasks != NULL;
289 }
290
291 /*
292  * Check to see if it is now time to start boosting RCU readers blocking
293  * the current grace period, and, if so, tell the rcu_kthread_task to
294  * start boosting them.  If there is an expedited boost in progress,
295  * we wait for it to complete.
296  *
297  * If there are no blocked readers blocking the current grace period,
298  * return 0 to let the caller know, otherwise return 1.  Note that this
299  * return value is independent of whether or not boosting was done.
300  */
301 static int rcu_initiate_boost(void)
302 {
303         if (!rcu_preempt_blocked_readers_cgp()) {
304                 RCU_TRACE(rcu_preempt_ctrlblk.n_normal_balk_blkd_tasks++);
305                 return 0;
306         }
307         if (rcu_preempt_ctrlblk.gp_tasks != NULL &&
308             rcu_preempt_ctrlblk.boost_tasks == NULL &&
309             rcu_preempt_ctrlblk.boosted_this_gp == 0 &&
310             ULONG_CMP_GE(jiffies, rcu_preempt_ctrlblk.boost_time)) {
311                 rcu_preempt_ctrlblk.boost_tasks = rcu_preempt_ctrlblk.gp_tasks;
312                 invoke_rcu_kthread();
313                 RCU_TRACE(rcu_preempt_ctrlblk.n_normal_boosts++);
314         } else
315                 RCU_TRACE(rcu_initiate_boost_trace());
316         return 1;
317 }
318
319 /*
320  * Initiate boosting for an expedited grace period.
321  */
322 static void rcu_initiate_expedited_boost(void)
323 {
324         unsigned long flags;
325
326         raw_local_irq_save(flags);
327         if (!list_empty(&rcu_preempt_ctrlblk.blkd_tasks)) {
328                 rcu_preempt_ctrlblk.boost_tasks =
329                         rcu_preempt_ctrlblk.blkd_tasks.next;
330                 rcu_preempt_ctrlblk.boosted_this_gp = -1;
331                 invoke_rcu_kthread();
332                 RCU_TRACE(rcu_preempt_ctrlblk.n_exp_boosts++);
333         } else
334                 RCU_TRACE(rcu_initiate_exp_boost_trace());
335         raw_local_irq_restore(flags);
336 }
337
338 #define RCU_BOOST_DELAY_JIFFIES DIV_ROUND_UP(CONFIG_RCU_BOOST_DELAY * HZ, 1000);
339
340 /*
341  * Do priority-boost accounting for the start of a new grace period.
342  */
343 static void rcu_preempt_boost_start_gp(void)
344 {
345         rcu_preempt_ctrlblk.boost_time = jiffies + RCU_BOOST_DELAY_JIFFIES;
346         if (rcu_preempt_ctrlblk.boosted_this_gp > 0)
347                 rcu_preempt_ctrlblk.boosted_this_gp = 0;
348 }
349
350 #else /* #ifdef CONFIG_RCU_BOOST */
351
352 /*
353  * If there is no RCU priority boosting, we don't boost.
354  */
355 static int rcu_boost(void)
356 {
357         return 0;
358 }
359
360 /*
361  * If there is no RCU priority boosting, we don't initiate boosting,
362  * but we do indicate whether there are blocked readers blocking the
363  * current grace period.
364  */
365 static int rcu_initiate_boost(void)
366 {
367         return rcu_preempt_blocked_readers_cgp();
368 }
369
370 /*
371  * If there is no RCU priority boosting, we don't initiate expedited boosting.
372  */
373 static void rcu_initiate_expedited_boost(void)
374 {
375 }
376
377 /*
378  * If there is no RCU priority boosting, nothing to do at grace-period start.
379  */
380 static void rcu_preempt_boost_start_gp(void)
381 {
382 }
383
384 #endif /* else #ifdef CONFIG_RCU_BOOST */
385
386 /*
387  * Record a preemptible-RCU quiescent state for the specified CPU.  Note
388  * that this just means that the task currently running on the CPU is
389  * in a quiescent state.  There might be any number of tasks blocked
390  * while in an RCU read-side critical section.
391  *
392  * Unlike the other rcu_*_qs() functions, callers to this function
393  * must disable irqs in order to protect the assignment to
394  * ->rcu_read_unlock_special.
395  *
396  * Because this is a single-CPU implementation, the only way a grace
397  * period can end is if the CPU is in a quiescent state.  The reason is
398  * that a blocked preemptible-RCU reader can exit its critical section
399  * only if the CPU is running it at the time.  Therefore, when the
400  * last task blocking the current grace period exits its RCU read-side
401  * critical section, neither the CPU nor blocked tasks will be stopping
402  * the current grace period.  (In contrast, SMP implementations
403  * might have CPUs running in RCU read-side critical sections that
404  * block later grace periods -- but this is not possible given only
405  * one CPU.)
406  */
407 static void rcu_preempt_cpu_qs(void)
408 {
409         /* Record both CPU and task as having responded to current GP. */
410         rcu_preempt_ctrlblk.gpcpu = rcu_preempt_ctrlblk.gpnum;
411         current->rcu_read_unlock_special &= ~RCU_READ_UNLOCK_NEED_QS;
412
413         /* If there is no GP then there is nothing more to do.  */
414         if (!rcu_preempt_gp_in_progress())
415                 return;
416         /*
417          * Check up on boosting.  If there are no readers blocking the
418          * current grace period, leave.
419          */
420         if (rcu_initiate_boost())
421                 return;
422
423         /* Advance callbacks. */
424         rcu_preempt_ctrlblk.completed = rcu_preempt_ctrlblk.gpnum;
425         rcu_preempt_ctrlblk.rcb.donetail = rcu_preempt_ctrlblk.rcb.curtail;
426         rcu_preempt_ctrlblk.rcb.curtail = rcu_preempt_ctrlblk.nexttail;
427
428         /* If there are no blocked readers, next GP is done instantly. */
429         if (!rcu_preempt_blocked_readers_any())
430                 rcu_preempt_ctrlblk.rcb.donetail = rcu_preempt_ctrlblk.nexttail;
431
432         /* If there are done callbacks, cause them to be invoked. */
433         if (*rcu_preempt_ctrlblk.rcb.donetail != NULL)
434                 invoke_rcu_kthread();
435 }
436
437 /*
438  * Start a new RCU grace period if warranted.  Hard irqs must be disabled.
439  */
440 static void rcu_preempt_start_gp(void)
441 {
442         if (!rcu_preempt_gp_in_progress() && rcu_preempt_needs_another_gp()) {
443
444                 /* Official start of GP. */
445                 rcu_preempt_ctrlblk.gpnum++;
446                 RCU_TRACE(rcu_preempt_ctrlblk.n_grace_periods++);
447
448                 /* Any blocked RCU readers block new GP. */
449                 if (rcu_preempt_blocked_readers_any())
450                         rcu_preempt_ctrlblk.gp_tasks =
451                                 rcu_preempt_ctrlblk.blkd_tasks.next;
452
453                 /* Set up for RCU priority boosting. */
454                 rcu_preempt_boost_start_gp();
455
456                 /* If there is no running reader, CPU is done with GP. */
457                 if (!rcu_preempt_running_reader())
458                         rcu_preempt_cpu_qs();
459         }
460 }
461
462 /*
463  * We have entered the scheduler, and the current task might soon be
464  * context-switched away from.  If this task is in an RCU read-side
465  * critical section, we will no longer be able to rely on the CPU to
466  * record that fact, so we enqueue the task on the blkd_tasks list.
467  * If the task started after the current grace period began, as recorded
468  * by ->gpcpu, we enqueue at the beginning of the list.  Otherwise
469  * before the element referenced by ->gp_tasks (or at the tail if
470  * ->gp_tasks is NULL) and point ->gp_tasks at the newly added element.
471  * The task will dequeue itself when it exits the outermost enclosing
472  * RCU read-side critical section.  Therefore, the current grace period
473  * cannot be permitted to complete until the ->gp_tasks pointer becomes
474  * NULL.
475  *
476  * Caller must disable preemption.
477  */
478 void rcu_preempt_note_context_switch(void)
479 {
480         struct task_struct *t = current;
481         unsigned long flags;
482
483         local_irq_save(flags); /* must exclude scheduler_tick(). */
484         if (rcu_preempt_running_reader() &&
485             (t->rcu_read_unlock_special & RCU_READ_UNLOCK_BLOCKED) == 0) {
486
487                 /* Possibly blocking in an RCU read-side critical section. */
488                 t->rcu_read_unlock_special |= RCU_READ_UNLOCK_BLOCKED;
489
490                 /*
491                  * If this CPU has already checked in, then this task
492                  * will hold up the next grace period rather than the
493                  * current grace period.  Queue the task accordingly.
494                  * If the task is queued for the current grace period
495                  * (i.e., this CPU has not yet passed through a quiescent
496                  * state for the current grace period), then as long
497                  * as that task remains queued, the current grace period
498                  * cannot end.
499                  */
500                 list_add(&t->rcu_node_entry, &rcu_preempt_ctrlblk.blkd_tasks);
501                 if (rcu_cpu_blocking_cur_gp())
502                         rcu_preempt_ctrlblk.gp_tasks = &t->rcu_node_entry;
503         }
504
505         /*
506          * Either we were not in an RCU read-side critical section to
507          * begin with, or we have now recorded that critical section
508          * globally.  Either way, we can now note a quiescent state
509          * for this CPU.  Again, if we were in an RCU read-side critical
510          * section, and if that critical section was blocking the current
511          * grace period, then the fact that the task has been enqueued
512          * means that current grace period continues to be blocked.
513          */
514         rcu_preempt_cpu_qs();
515         local_irq_restore(flags);
516 }
517
518 /*
519  * Tiny-preemptible RCU implementation for rcu_read_lock().
520  * Just increment ->rcu_read_lock_nesting, shared state will be updated
521  * if we block.
522  */
523 void __rcu_read_lock(void)
524 {
525         current->rcu_read_lock_nesting++;
526         barrier();  /* needed if we ever invoke rcu_read_lock in rcutiny.c */
527 }
528 EXPORT_SYMBOL_GPL(__rcu_read_lock);
529
530 /*
531  * Handle special cases during rcu_read_unlock(), such as needing to
532  * notify RCU core processing or task having blocked during the RCU
533  * read-side critical section.
534  */
535 static void rcu_read_unlock_special(struct task_struct *t)
536 {
537         int empty;
538         int empty_exp;
539         unsigned long flags;
540         struct list_head *np;
541         int special;
542
543         /*
544          * NMI handlers cannot block and cannot safely manipulate state.
545          * They therefore cannot possibly be special, so just leave.
546          */
547         if (in_nmi())
548                 return;
549
550         local_irq_save(flags);
551
552         /*
553          * If RCU core is waiting for this CPU to exit critical section,
554          * let it know that we have done so.
555          */
556         special = t->rcu_read_unlock_special;
557         if (special & RCU_READ_UNLOCK_NEED_QS)
558                 rcu_preempt_cpu_qs();
559
560         /* Hardware IRQ handlers cannot block. */
561         if (in_irq()) {
562                 local_irq_restore(flags);
563                 return;
564         }
565
566         /* Clean up if blocked during RCU read-side critical section. */
567         if (special & RCU_READ_UNLOCK_BLOCKED) {
568                 t->rcu_read_unlock_special &= ~RCU_READ_UNLOCK_BLOCKED;
569
570                 /*
571                  * Remove this task from the ->blkd_tasks list and adjust
572                  * any pointers that might have been referencing it.
573                  */
574                 empty = !rcu_preempt_blocked_readers_cgp();
575                 empty_exp = rcu_preempt_ctrlblk.exp_tasks == NULL;
576                 np = rcu_next_node_entry(t);
577                 list_del(&t->rcu_node_entry);
578                 if (&t->rcu_node_entry == rcu_preempt_ctrlblk.gp_tasks)
579                         rcu_preempt_ctrlblk.gp_tasks = np;
580                 if (&t->rcu_node_entry == rcu_preempt_ctrlblk.exp_tasks)
581                         rcu_preempt_ctrlblk.exp_tasks = np;
582 #ifdef CONFIG_RCU_BOOST
583                 if (&t->rcu_node_entry == rcu_preempt_ctrlblk.boost_tasks)
584                         rcu_preempt_ctrlblk.boost_tasks = np;
585 #endif /* #ifdef CONFIG_RCU_BOOST */
586                 INIT_LIST_HEAD(&t->rcu_node_entry);
587
588                 /*
589                  * If this was the last task on the current list, and if
590                  * we aren't waiting on the CPU, report the quiescent state
591                  * and start a new grace period if needed.
592                  */
593                 if (!empty && !rcu_preempt_blocked_readers_cgp()) {
594                         rcu_preempt_cpu_qs();
595                         rcu_preempt_start_gp();
596                 }
597
598                 /*
599                  * If this was the last task on the expedited lists,
600                  * then we need wake up the waiting task.
601                  */
602                 if (!empty_exp && rcu_preempt_ctrlblk.exp_tasks == NULL)
603                         rcu_report_exp_done();
604         }
605 #ifdef CONFIG_RCU_BOOST
606         /* Unboost self if was boosted. */
607         if (special & RCU_READ_UNLOCK_BOOSTED) {
608                 t->rcu_read_unlock_special &= ~RCU_READ_UNLOCK_BOOSTED;
609                 rt_mutex_unlock(t->rcu_boost_mutex);
610                 t->rcu_boost_mutex = NULL;
611         }
612 #endif /* #ifdef CONFIG_RCU_BOOST */
613         local_irq_restore(flags);
614 }
615
616 /*
617  * Tiny-preemptible RCU implementation for rcu_read_unlock().
618  * Decrement ->rcu_read_lock_nesting.  If the result is zero (outermost
619  * rcu_read_unlock()) and ->rcu_read_unlock_special is non-zero, then
620  * invoke rcu_read_unlock_special() to clean up after a context switch
621  * in an RCU read-side critical section and other special cases.
622  */
623 void __rcu_read_unlock(void)
624 {
625         struct task_struct *t = current;
626
627         barrier();  /* needed if we ever invoke rcu_read_unlock in rcutiny.c */
628         --t->rcu_read_lock_nesting;
629         barrier();  /* decrement before load of ->rcu_read_unlock_special */
630         if (t->rcu_read_lock_nesting == 0 &&
631             unlikely(ACCESS_ONCE(t->rcu_read_unlock_special)))
632                 rcu_read_unlock_special(t);
633 #ifdef CONFIG_PROVE_LOCKING
634         WARN_ON_ONCE(t->rcu_read_lock_nesting < 0);
635 #endif /* #ifdef CONFIG_PROVE_LOCKING */
636 }
637 EXPORT_SYMBOL_GPL(__rcu_read_unlock);
638
639 /*
640  * Check for a quiescent state from the current CPU.  When a task blocks,
641  * the task is recorded in the rcu_preempt_ctrlblk structure, which is
642  * checked elsewhere.  This is called from the scheduling-clock interrupt.
643  *
644  * Caller must disable hard irqs.
645  */
646 static void rcu_preempt_check_callbacks(void)
647 {
648         struct task_struct *t = current;
649
650         if (rcu_preempt_gp_in_progress() &&
651             (!rcu_preempt_running_reader() ||
652              !rcu_cpu_blocking_cur_gp()))
653                 rcu_preempt_cpu_qs();
654         if (&rcu_preempt_ctrlblk.rcb.rcucblist !=
655             rcu_preempt_ctrlblk.rcb.donetail)
656                 invoke_rcu_kthread();
657         if (rcu_preempt_gp_in_progress() &&
658             rcu_cpu_blocking_cur_gp() &&
659             rcu_preempt_running_reader())
660                 t->rcu_read_unlock_special |= RCU_READ_UNLOCK_NEED_QS;
661 }
662
663 /*
664  * TINY_PREEMPT_RCU has an extra callback-list tail pointer to
665  * update, so this is invoked from rcu_process_callbacks() to
666  * handle that case.  Of course, it is invoked for all flavors of
667  * RCU, but RCU callbacks can appear only on one of the lists, and
668  * neither ->nexttail nor ->donetail can possibly be NULL, so there
669  * is no need for an explicit check.
670  */
671 static void rcu_preempt_remove_callbacks(struct rcu_ctrlblk *rcp)
672 {
673         if (rcu_preempt_ctrlblk.nexttail == rcp->donetail)
674                 rcu_preempt_ctrlblk.nexttail = &rcp->rcucblist;
675 }
676
677 /*
678  * Process callbacks for preemptible RCU.
679  */
680 static void rcu_preempt_process_callbacks(void)
681 {
682         rcu_process_callbacks(&rcu_preempt_ctrlblk.rcb);
683 }
684
685 /*
686  * Queue a preemptible -RCU callback for invocation after a grace period.
687  */
688 void call_rcu(struct rcu_head *head, void (*func)(struct rcu_head *rcu))
689 {
690         unsigned long flags;
691
692         debug_rcu_head_queue(head);
693         head->func = func;
694         head->next = NULL;
695
696         local_irq_save(flags);
697         *rcu_preempt_ctrlblk.nexttail = head;
698         rcu_preempt_ctrlblk.nexttail = &head->next;
699         RCU_TRACE(rcu_preempt_ctrlblk.rcb.qlen++);
700         rcu_preempt_start_gp();  /* checks to see if GP needed. */
701         local_irq_restore(flags);
702 }
703 EXPORT_SYMBOL_GPL(call_rcu);
704
705 void rcu_barrier(void)
706 {
707         struct rcu_synchronize rcu;
708
709         init_rcu_head_on_stack(&rcu.head);
710         init_completion(&rcu.completion);
711         /* Will wake me after RCU finished. */
712         call_rcu(&rcu.head, wakeme_after_rcu);
713         /* Wait for it. */
714         wait_for_completion(&rcu.completion);
715         destroy_rcu_head_on_stack(&rcu.head);
716 }
717 EXPORT_SYMBOL_GPL(rcu_barrier);
718
719 /*
720  * synchronize_rcu - wait until a grace period has elapsed.
721  *
722  * Control will return to the caller some time after a full grace
723  * period has elapsed, in other words after all currently executing RCU
724  * read-side critical sections have completed.  RCU read-side critical
725  * sections are delimited by rcu_read_lock() and rcu_read_unlock(),
726  * and may be nested.
727  */
728 void synchronize_rcu(void)
729 {
730 #ifdef CONFIG_DEBUG_LOCK_ALLOC
731         if (!rcu_scheduler_active)
732                 return;
733 #endif /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
734
735         WARN_ON_ONCE(rcu_preempt_running_reader());
736         if (!rcu_preempt_blocked_readers_any())
737                 return;
738
739         /* Once we get past the fastpath checks, same code as rcu_barrier(). */
740         rcu_barrier();
741 }
742 EXPORT_SYMBOL_GPL(synchronize_rcu);
743
744 static DECLARE_WAIT_QUEUE_HEAD(sync_rcu_preempt_exp_wq);
745 static unsigned long sync_rcu_preempt_exp_count;
746 static DEFINE_MUTEX(sync_rcu_preempt_exp_mutex);
747
748 /*
749  * Return non-zero if there are any tasks in RCU read-side critical
750  * sections blocking the current preemptible-RCU expedited grace period.
751  * If there is no preemptible-RCU expedited grace period currently in
752  * progress, returns zero unconditionally.
753  */
754 static int rcu_preempted_readers_exp(void)
755 {
756         return rcu_preempt_ctrlblk.exp_tasks != NULL;
757 }
758
759 /*
760  * Report the exit from RCU read-side critical section for the last task
761  * that queued itself during or before the current expedited preemptible-RCU
762  * grace period.
763  */
764 static void rcu_report_exp_done(void)
765 {
766         wake_up(&sync_rcu_preempt_exp_wq);
767 }
768
769 /*
770  * Wait for an rcu-preempt grace period, but expedite it.  The basic idea
771  * is to rely in the fact that there is but one CPU, and that it is
772  * illegal for a task to invoke synchronize_rcu_expedited() while in a
773  * preemptible-RCU read-side critical section.  Therefore, any such
774  * critical sections must correspond to blocked tasks, which must therefore
775  * be on the ->blkd_tasks list.  So just record the current head of the
776  * list in the ->exp_tasks pointer, and wait for all tasks including and
777  * after the task pointed to by ->exp_tasks to drain.
778  */
779 void synchronize_rcu_expedited(void)
780 {
781         unsigned long flags;
782         struct rcu_preempt_ctrlblk *rpcp = &rcu_preempt_ctrlblk;
783         unsigned long snap;
784
785         barrier(); /* ensure prior action seen before grace period. */
786
787         WARN_ON_ONCE(rcu_preempt_running_reader());
788
789         /*
790          * Acquire lock so that there is only one preemptible RCU grace
791          * period in flight.  Of course, if someone does the expedited
792          * grace period for us while we are acquiring the lock, just leave.
793          */
794         snap = sync_rcu_preempt_exp_count + 1;
795         mutex_lock(&sync_rcu_preempt_exp_mutex);
796         if (ULONG_CMP_LT(snap, sync_rcu_preempt_exp_count))
797                 goto unlock_mb_ret; /* Others did our work for us. */
798
799         local_irq_save(flags);
800
801         /*
802          * All RCU readers have to already be on blkd_tasks because
803          * we cannot legally be executing in an RCU read-side critical
804          * section.
805          */
806
807         /* Snapshot current head of ->blkd_tasks list. */
808         rpcp->exp_tasks = rpcp->blkd_tasks.next;
809         if (rpcp->exp_tasks == &rpcp->blkd_tasks)
810                 rpcp->exp_tasks = NULL;
811         local_irq_restore(flags);
812
813         /* Wait for tail of ->blkd_tasks list to drain. */
814         if (rcu_preempted_readers_exp())
815                 rcu_initiate_expedited_boost();
816                 wait_event(sync_rcu_preempt_exp_wq,
817                            !rcu_preempted_readers_exp());
818
819         /* Clean up and exit. */
820         barrier(); /* ensure expedited GP seen before counter increment. */
821         sync_rcu_preempt_exp_count++;
822 unlock_mb_ret:
823         mutex_unlock(&sync_rcu_preempt_exp_mutex);
824         barrier(); /* ensure subsequent action seen after grace period. */
825 }
826 EXPORT_SYMBOL_GPL(synchronize_rcu_expedited);
827
828 /*
829  * Does preemptible RCU need the CPU to stay out of dynticks mode?
830  */
831 int rcu_preempt_needs_cpu(void)
832 {
833         if (!rcu_preempt_running_reader())
834                 rcu_preempt_cpu_qs();
835         return rcu_preempt_ctrlblk.rcb.rcucblist != NULL;
836 }
837
838 /*
839  * Check for a task exiting while in a preemptible -RCU read-side
840  * critical section, clean up if so.  No need to issue warnings,
841  * as debug_check_no_locks_held() already does this if lockdep
842  * is enabled.
843  */
844 void exit_rcu(void)
845 {
846         struct task_struct *t = current;
847
848         if (t->rcu_read_lock_nesting == 0)
849                 return;
850         t->rcu_read_lock_nesting = 1;
851         rcu_read_unlock();
852 }
853
854 #else /* #ifdef CONFIG_TINY_PREEMPT_RCU */
855
856 #ifdef CONFIG_RCU_TRACE
857
858 /*
859  * Because preemptible RCU does not exist, it is not necessary to
860  * dump out its statistics.
861  */
862 static void show_tiny_preempt_stats(struct seq_file *m)
863 {
864 }
865
866 #endif /* #ifdef CONFIG_RCU_TRACE */
867
868 /*
869  * Because preemptible RCU does not exist, it is never necessary to
870  * boost preempted RCU readers.
871  */
872 static int rcu_boost(void)
873 {
874         return 0;
875 }
876
877 /*
878  * Because preemptible RCU does not exist, it never has any callbacks
879  * to check.
880  */
881 static void rcu_preempt_check_callbacks(void)
882 {
883 }
884
885 /*
886  * Because preemptible RCU does not exist, it never has any callbacks
887  * to remove.
888  */
889 static void rcu_preempt_remove_callbacks(struct rcu_ctrlblk *rcp)
890 {
891 }
892
893 /*
894  * Because preemptible RCU does not exist, it never has any callbacks
895  * to process.
896  */
897 static void rcu_preempt_process_callbacks(void)
898 {
899 }
900
901 #endif /* #else #ifdef CONFIG_TINY_PREEMPT_RCU */
902
903 #ifdef CONFIG_DEBUG_LOCK_ALLOC
904 #include <linux/kernel_stat.h>
905
906 /*
907  * During boot, we forgive RCU lockdep issues.  After this function is
908  * invoked, we start taking RCU lockdep issues seriously.
909  */
910 void __init rcu_scheduler_starting(void)
911 {
912         WARN_ON(nr_context_switches() > 0);
913         rcu_scheduler_active = 1;
914 }
915
916 #endif /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
917
918 #ifdef CONFIG_RCU_BOOST
919 #define RCU_BOOST_PRIO CONFIG_RCU_BOOST_PRIO
920 #else /* #ifdef CONFIG_RCU_BOOST */
921 #define RCU_BOOST_PRIO 1
922 #endif /* #else #ifdef CONFIG_RCU_BOOST */
923
924 #ifdef CONFIG_RCU_TRACE
925
926 #ifdef CONFIG_RCU_BOOST
927
928 static void rcu_initiate_boost_trace(void)
929 {
930         if (rcu_preempt_ctrlblk.gp_tasks == NULL)
931                 rcu_preempt_ctrlblk.n_normal_balk_gp_tasks++;
932         else if (rcu_preempt_ctrlblk.boost_tasks != NULL)
933                 rcu_preempt_ctrlblk.n_normal_balk_boost_tasks++;
934         else if (rcu_preempt_ctrlblk.boosted_this_gp != 0)
935                 rcu_preempt_ctrlblk.n_normal_balk_boosted++;
936         else if (!ULONG_CMP_GE(jiffies, rcu_preempt_ctrlblk.boost_time))
937                 rcu_preempt_ctrlblk.n_normal_balk_notyet++;
938         else
939                 rcu_preempt_ctrlblk.n_normal_balk_nos++;
940 }
941
942 static void rcu_initiate_exp_boost_trace(void)
943 {
944         if (list_empty(&rcu_preempt_ctrlblk.blkd_tasks))
945                 rcu_preempt_ctrlblk.n_exp_balk_blkd_tasks++;
946         else
947                 rcu_preempt_ctrlblk.n_exp_balk_nos++;
948 }
949
950 #endif /* #ifdef CONFIG_RCU_BOOST */
951
952 static void rcu_trace_sub_qlen(struct rcu_ctrlblk *rcp, int n)
953 {
954         unsigned long flags;
955
956         raw_local_irq_save(flags);
957         rcp->qlen -= n;
958         raw_local_irq_restore(flags);
959 }
960
961 /*
962  * Dump statistics for TINY_RCU, such as they are.
963  */
964 static int show_tiny_stats(struct seq_file *m, void *unused)
965 {
966         show_tiny_preempt_stats(m);
967         seq_printf(m, "rcu_sched: qlen: %ld\n", rcu_sched_ctrlblk.qlen);
968         seq_printf(m, "rcu_bh: qlen: %ld\n", rcu_bh_ctrlblk.qlen);
969         return 0;
970 }
971
972 static int show_tiny_stats_open(struct inode *inode, struct file *file)
973 {
974         return single_open(file, show_tiny_stats, NULL);
975 }
976
977 static const struct file_operations show_tiny_stats_fops = {
978         .owner = THIS_MODULE,
979         .open = show_tiny_stats_open,
980         .read = seq_read,
981         .llseek = seq_lseek,
982         .release = single_release,
983 };
984
985 static struct dentry *rcudir;
986
987 static int __init rcutiny_trace_init(void)
988 {
989         struct dentry *retval;
990
991         rcudir = debugfs_create_dir("rcu", NULL);
992         if (!rcudir)
993                 goto free_out;
994         retval = debugfs_create_file("rcudata", 0444, rcudir,
995                                      NULL, &show_tiny_stats_fops);
996         if (!retval)
997                 goto free_out;
998         return 0;
999 free_out:
1000         debugfs_remove_recursive(rcudir);
1001         return 1;
1002 }
1003
1004 static void __exit rcutiny_trace_cleanup(void)
1005 {
1006         debugfs_remove_recursive(rcudir);
1007 }
1008
1009 module_init(rcutiny_trace_init);
1010 module_exit(rcutiny_trace_cleanup);
1011
1012 MODULE_AUTHOR("Paul E. McKenney");
1013 MODULE_DESCRIPTION("Read-Copy Update tracing for tiny implementation");
1014 MODULE_LICENSE("GPL");
1015
1016 #endif /* #ifdef CONFIG_RCU_TRACE */