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