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