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