rcu: move TINY_RCU from softirq to kthread
[linux-flexiantxendom0-natty.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
27 #ifdef CONFIG_TINY_PREEMPT_RCU
28
29 #include <linux/delay.h>
30
31 /* Global control variables for preemptible RCU. */
32 struct rcu_preempt_ctrlblk {
33         struct rcu_ctrlblk rcb; /* curtail: ->next ptr of last CB for GP. */
34         struct rcu_head **nexttail;
35                                 /* Tasks blocked in a preemptible RCU */
36                                 /*  read-side critical section while an */
37                                 /*  preemptible-RCU grace period is in */
38                                 /*  progress must wait for a later grace */
39                                 /*  period.  This pointer points to the */
40                                 /*  ->next pointer of the last task that */
41                                 /*  must wait for a later grace period, or */
42                                 /*  to &->rcb.rcucblist if there is no */
43                                 /*  such task. */
44         struct list_head blkd_tasks;
45                                 /* Tasks blocked in RCU read-side critical */
46                                 /*  section.  Tasks are placed at the head */
47                                 /*  of this list and age towards the tail. */
48         struct list_head *gp_tasks;
49                                 /* Pointer to the first task blocking the */
50                                 /*  current grace period, or NULL if there */
51                                 /*  is not such task. */
52         struct list_head *exp_tasks;
53                                 /* Pointer to first task blocking the */
54                                 /*  current expedited grace period, or NULL */
55                                 /*  if there is no such task.  If there */
56                                 /*  is no current expedited grace period, */
57                                 /*  then there cannot be any such task. */
58         u8 gpnum;               /* Current grace period. */
59         u8 gpcpu;               /* Last grace period blocked by the CPU. */
60         u8 completed;           /* Last grace period completed. */
61                                 /*  If all three are equal, RCU is idle. */
62 };
63
64 static struct rcu_preempt_ctrlblk rcu_preempt_ctrlblk = {
65         .rcb.donetail = &rcu_preempt_ctrlblk.rcb.rcucblist,
66         .rcb.curtail = &rcu_preempt_ctrlblk.rcb.rcucblist,
67         .nexttail = &rcu_preempt_ctrlblk.rcb.rcucblist,
68         .blkd_tasks = LIST_HEAD_INIT(rcu_preempt_ctrlblk.blkd_tasks),
69 };
70
71 static int rcu_preempted_readers_exp(void);
72 static void rcu_report_exp_done(void);
73
74 /*
75  * Return true if the CPU has not yet responded to the current grace period.
76  */
77 static int rcu_cpu_blocking_cur_gp(void)
78 {
79         return rcu_preempt_ctrlblk.gpcpu != rcu_preempt_ctrlblk.gpnum;
80 }
81
82 /*
83  * Check for a running RCU reader.  Because there is only one CPU,
84  * there can be but one running RCU reader at a time.  ;-)
85  */
86 static int rcu_preempt_running_reader(void)
87 {
88         return current->rcu_read_lock_nesting;
89 }
90
91 /*
92  * Check for preempted RCU readers blocking any grace period.
93  * If the caller needs a reliable answer, it must disable hard irqs.
94  */
95 static int rcu_preempt_blocked_readers_any(void)
96 {
97         return !list_empty(&rcu_preempt_ctrlblk.blkd_tasks);
98 }
99
100 /*
101  * Check for preempted RCU readers blocking the current grace period.
102  * If the caller needs a reliable answer, it must disable hard irqs.
103  */
104 static int rcu_preempt_blocked_readers_cgp(void)
105 {
106         return rcu_preempt_ctrlblk.gp_tasks != NULL;
107 }
108
109 /*
110  * Return true if another preemptible-RCU grace period is needed.
111  */
112 static int rcu_preempt_needs_another_gp(void)
113 {
114         return *rcu_preempt_ctrlblk.rcb.curtail != NULL;
115 }
116
117 /*
118  * Return true if a preemptible-RCU grace period is in progress.
119  * The caller must disable hardirqs.
120  */
121 static int rcu_preempt_gp_in_progress(void)
122 {
123         return rcu_preempt_ctrlblk.completed != rcu_preempt_ctrlblk.gpnum;
124 }
125
126 /*
127  * Record a preemptible-RCU quiescent state for the specified CPU.  Note
128  * that this just means that the task currently running on the CPU is
129  * in a quiescent state.  There might be any number of tasks blocked
130  * while in an RCU read-side critical section.
131  *
132  * Unlike the other rcu_*_qs() functions, callers to this function
133  * must disable irqs in order to protect the assignment to
134  * ->rcu_read_unlock_special.
135  *
136  * Because this is a single-CPU implementation, the only way a grace
137  * period can end is if the CPU is in a quiescent state.  The reason is
138  * that a blocked preemptible-RCU reader can exit its critical section
139  * only if the CPU is running it at the time.  Therefore, when the
140  * last task blocking the current grace period exits its RCU read-side
141  * critical section, neither the CPU nor blocked tasks will be stopping
142  * the current grace period.  (In contrast, SMP implementations
143  * might have CPUs running in RCU read-side critical sections that
144  * block later grace periods -- but this is not possible given only
145  * one CPU.)
146  */
147 static void rcu_preempt_cpu_qs(void)
148 {
149         /* Record both CPU and task as having responded to current GP. */
150         rcu_preempt_ctrlblk.gpcpu = rcu_preempt_ctrlblk.gpnum;
151         current->rcu_read_unlock_special &= ~RCU_READ_UNLOCK_NEED_QS;
152
153         /*
154          * If there is no GP, or if blocked readers are still blocking GP,
155          * then there is nothing more to do.
156          */
157         if (!rcu_preempt_gp_in_progress() || rcu_preempt_blocked_readers_cgp())
158                 return;
159
160         /* Advance callbacks. */
161         rcu_preempt_ctrlblk.completed = rcu_preempt_ctrlblk.gpnum;
162         rcu_preempt_ctrlblk.rcb.donetail = rcu_preempt_ctrlblk.rcb.curtail;
163         rcu_preempt_ctrlblk.rcb.curtail = rcu_preempt_ctrlblk.nexttail;
164
165         /* If there are no blocked readers, next GP is done instantly. */
166         if (!rcu_preempt_blocked_readers_any())
167                 rcu_preempt_ctrlblk.rcb.donetail = rcu_preempt_ctrlblk.nexttail;
168
169         /* If there are done callbacks, cause them to be invoked. */
170         if (*rcu_preempt_ctrlblk.rcb.donetail != NULL)
171                 invoke_rcu_cbs();
172 }
173
174 /*
175  * Start a new RCU grace period if warranted.  Hard irqs must be disabled.
176  */
177 static void rcu_preempt_start_gp(void)
178 {
179         if (!rcu_preempt_gp_in_progress() && rcu_preempt_needs_another_gp()) {
180
181                 /* Official start of GP. */
182                 rcu_preempt_ctrlblk.gpnum++;
183
184                 /* Any blocked RCU readers block new GP. */
185                 if (rcu_preempt_blocked_readers_any())
186                         rcu_preempt_ctrlblk.gp_tasks =
187                                 rcu_preempt_ctrlblk.blkd_tasks.next;
188
189                 /* If there is no running reader, CPU is done with GP. */
190                 if (!rcu_preempt_running_reader())
191                         rcu_preempt_cpu_qs();
192         }
193 }
194
195 /*
196  * We have entered the scheduler, and the current task might soon be
197  * context-switched away from.  If this task is in an RCU read-side
198  * critical section, we will no longer be able to rely on the CPU to
199  * record that fact, so we enqueue the task on the blkd_tasks list.
200  * If the task started after the current grace period began, as recorded
201  * by ->gpcpu, we enqueue at the beginning of the list.  Otherwise
202  * before the element referenced by ->gp_tasks (or at the tail if
203  * ->gp_tasks is NULL) and point ->gp_tasks at the newly added element.
204  * The task will dequeue itself when it exits the outermost enclosing
205  * RCU read-side critical section.  Therefore, the current grace period
206  * cannot be permitted to complete until the ->gp_tasks pointer becomes
207  * NULL.
208  *
209  * Caller must disable preemption.
210  */
211 void rcu_preempt_note_context_switch(void)
212 {
213         struct task_struct *t = current;
214         unsigned long flags;
215
216         local_irq_save(flags); /* must exclude scheduler_tick(). */
217         if (rcu_preempt_running_reader() &&
218             (t->rcu_read_unlock_special & RCU_READ_UNLOCK_BLOCKED) == 0) {
219
220                 /* Possibly blocking in an RCU read-side critical section. */
221                 t->rcu_read_unlock_special |= RCU_READ_UNLOCK_BLOCKED;
222
223                 /*
224                  * If this CPU has already checked in, then this task
225                  * will hold up the next grace period rather than the
226                  * current grace period.  Queue the task accordingly.
227                  * If the task is queued for the current grace period
228                  * (i.e., this CPU has not yet passed through a quiescent
229                  * state for the current grace period), then as long
230                  * as that task remains queued, the current grace period
231                  * cannot end.
232                  */
233                 list_add(&t->rcu_node_entry, &rcu_preempt_ctrlblk.blkd_tasks);
234                 if (rcu_cpu_blocking_cur_gp())
235                         rcu_preempt_ctrlblk.gp_tasks = &t->rcu_node_entry;
236         }
237
238         /*
239          * Either we were not in an RCU read-side critical section to
240          * begin with, or we have now recorded that critical section
241          * globally.  Either way, we can now note a quiescent state
242          * for this CPU.  Again, if we were in an RCU read-side critical
243          * section, and if that critical section was blocking the current
244          * grace period, then the fact that the task has been enqueued
245          * means that current grace period continues to be blocked.
246          */
247         rcu_preempt_cpu_qs();
248         local_irq_restore(flags);
249 }
250
251 /*
252  * Tiny-preemptible RCU implementation for rcu_read_lock().
253  * Just increment ->rcu_read_lock_nesting, shared state will be updated
254  * if we block.
255  */
256 void __rcu_read_lock(void)
257 {
258         current->rcu_read_lock_nesting++;
259         barrier();  /* needed if we ever invoke rcu_read_lock in rcutiny.c */
260 }
261 EXPORT_SYMBOL_GPL(__rcu_read_lock);
262
263 /*
264  * Handle special cases during rcu_read_unlock(), such as needing to
265  * notify RCU core processing or task having blocked during the RCU
266  * read-side critical section.
267  */
268 static void rcu_read_unlock_special(struct task_struct *t)
269 {
270         int empty;
271         int empty_exp;
272         unsigned long flags;
273         struct list_head *np;
274         int special;
275
276         /*
277          * NMI handlers cannot block and cannot safely manipulate state.
278          * They therefore cannot possibly be special, so just leave.
279          */
280         if (in_nmi())
281                 return;
282
283         local_irq_save(flags);
284
285         /*
286          * If RCU core is waiting for this CPU to exit critical section,
287          * let it know that we have done so.
288          */
289         special = t->rcu_read_unlock_special;
290         if (special & RCU_READ_UNLOCK_NEED_QS)
291                 rcu_preempt_cpu_qs();
292
293         /* Hardware IRQ handlers cannot block. */
294         if (in_irq()) {
295                 local_irq_restore(flags);
296                 return;
297         }
298
299         /* Clean up if blocked during RCU read-side critical section. */
300         if (special & RCU_READ_UNLOCK_BLOCKED) {
301                 t->rcu_read_unlock_special &= ~RCU_READ_UNLOCK_BLOCKED;
302
303                 /*
304                  * Remove this task from the ->blkd_tasks list and adjust
305                  * any pointers that might have been referencing it.
306                  */
307                 empty = !rcu_preempt_blocked_readers_cgp();
308                 empty_exp = rcu_preempt_ctrlblk.exp_tasks == NULL;
309                 np = t->rcu_node_entry.next;
310                 if (np == &rcu_preempt_ctrlblk.blkd_tasks)
311                         np = NULL;
312                 list_del(&t->rcu_node_entry);
313                 if (&t->rcu_node_entry == rcu_preempt_ctrlblk.gp_tasks)
314                         rcu_preempt_ctrlblk.gp_tasks = np;
315                 if (&t->rcu_node_entry == rcu_preempt_ctrlblk.exp_tasks)
316                         rcu_preempt_ctrlblk.exp_tasks = np;
317                 INIT_LIST_HEAD(&t->rcu_node_entry);
318
319                 /*
320                  * If this was the last task on the current list, and if
321                  * we aren't waiting on the CPU, report the quiescent state
322                  * and start a new grace period if needed.
323                  */
324                 if (!empty && !rcu_preempt_blocked_readers_cgp()) {
325                         rcu_preempt_cpu_qs();
326                         rcu_preempt_start_gp();
327                 }
328
329                 /*
330                  * If this was the last task on the expedited lists,
331                  * then we need wake up the waiting task.
332                  */
333                 if (!empty_exp && rcu_preempt_ctrlblk.exp_tasks == NULL)
334                         rcu_report_exp_done();
335         }
336         local_irq_restore(flags);
337 }
338
339 /*
340  * Tiny-preemptible RCU implementation for rcu_read_unlock().
341  * Decrement ->rcu_read_lock_nesting.  If the result is zero (outermost
342  * rcu_read_unlock()) and ->rcu_read_unlock_special is non-zero, then
343  * invoke rcu_read_unlock_special() to clean up after a context switch
344  * in an RCU read-side critical section and other special cases.
345  */
346 void __rcu_read_unlock(void)
347 {
348         struct task_struct *t = current;
349
350         barrier();  /* needed if we ever invoke rcu_read_unlock in rcutiny.c */
351         --t->rcu_read_lock_nesting;
352         barrier();  /* decrement before load of ->rcu_read_unlock_special */
353         if (t->rcu_read_lock_nesting == 0 &&
354             unlikely(ACCESS_ONCE(t->rcu_read_unlock_special)))
355                 rcu_read_unlock_special(t);
356 #ifdef CONFIG_PROVE_LOCKING
357         WARN_ON_ONCE(t->rcu_read_lock_nesting < 0);
358 #endif /* #ifdef CONFIG_PROVE_LOCKING */
359 }
360 EXPORT_SYMBOL_GPL(__rcu_read_unlock);
361
362 /*
363  * Check for a quiescent state from the current CPU.  When a task blocks,
364  * the task is recorded in the rcu_preempt_ctrlblk structure, which is
365  * checked elsewhere.  This is called from the scheduling-clock interrupt.
366  *
367  * Caller must disable hard irqs.
368  */
369 static void rcu_preempt_check_callbacks(void)
370 {
371         struct task_struct *t = current;
372
373         if (rcu_preempt_gp_in_progress() &&
374             (!rcu_preempt_running_reader() ||
375              !rcu_cpu_blocking_cur_gp()))
376                 rcu_preempt_cpu_qs();
377         if (&rcu_preempt_ctrlblk.rcb.rcucblist !=
378             rcu_preempt_ctrlblk.rcb.donetail)
379                 invoke_rcu_cbs();
380         if (rcu_preempt_gp_in_progress() &&
381             rcu_cpu_blocking_cur_gp() &&
382             rcu_preempt_running_reader())
383                 t->rcu_read_unlock_special |= RCU_READ_UNLOCK_NEED_QS;
384 }
385
386 /*
387  * TINY_PREEMPT_RCU has an extra callback-list tail pointer to
388  * update, so this is invoked from rcu_process_callbacks() to
389  * handle that case.  Of course, it is invoked for all flavors of
390  * RCU, but RCU callbacks can appear only on one of the lists, and
391  * neither ->nexttail nor ->donetail can possibly be NULL, so there
392  * is no need for an explicit check.
393  */
394 static void rcu_preempt_remove_callbacks(struct rcu_ctrlblk *rcp)
395 {
396         if (rcu_preempt_ctrlblk.nexttail == rcp->donetail)
397                 rcu_preempt_ctrlblk.nexttail = &rcp->rcucblist;
398 }
399
400 /*
401  * Process callbacks for preemptible RCU.
402  */
403 static void rcu_preempt_process_callbacks(void)
404 {
405         rcu_process_callbacks(&rcu_preempt_ctrlblk.rcb);
406 }
407
408 /*
409  * Queue a preemptible -RCU callback for invocation after a grace period.
410  */
411 void call_rcu(struct rcu_head *head, void (*func)(struct rcu_head *rcu))
412 {
413         unsigned long flags;
414
415         debug_rcu_head_queue(head);
416         head->func = func;
417         head->next = NULL;
418
419         local_irq_save(flags);
420         *rcu_preempt_ctrlblk.nexttail = head;
421         rcu_preempt_ctrlblk.nexttail = &head->next;
422         rcu_preempt_start_gp();  /* checks to see if GP needed. */
423         local_irq_restore(flags);
424 }
425 EXPORT_SYMBOL_GPL(call_rcu);
426
427 void rcu_barrier(void)
428 {
429         struct rcu_synchronize rcu;
430
431         init_rcu_head_on_stack(&rcu.head);
432         init_completion(&rcu.completion);
433         /* Will wake me after RCU finished. */
434         call_rcu(&rcu.head, wakeme_after_rcu);
435         /* Wait for it. */
436         wait_for_completion(&rcu.completion);
437         destroy_rcu_head_on_stack(&rcu.head);
438 }
439 EXPORT_SYMBOL_GPL(rcu_barrier);
440
441 /*
442  * synchronize_rcu - wait until a grace period has elapsed.
443  *
444  * Control will return to the caller some time after a full grace
445  * period has elapsed, in other words after all currently executing RCU
446  * read-side critical sections have completed.  RCU read-side critical
447  * sections are delimited by rcu_read_lock() and rcu_read_unlock(),
448  * and may be nested.
449  */
450 void synchronize_rcu(void)
451 {
452 #ifdef CONFIG_DEBUG_LOCK_ALLOC
453         if (!rcu_scheduler_active)
454                 return;
455 #endif /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
456
457         WARN_ON_ONCE(rcu_preempt_running_reader());
458         if (!rcu_preempt_blocked_readers_any())
459                 return;
460
461         /* Once we get past the fastpath checks, same code as rcu_barrier(). */
462         rcu_barrier();
463 }
464 EXPORT_SYMBOL_GPL(synchronize_rcu);
465
466 static DECLARE_WAIT_QUEUE_HEAD(sync_rcu_preempt_exp_wq);
467 static unsigned long sync_rcu_preempt_exp_count;
468 static DEFINE_MUTEX(sync_rcu_preempt_exp_mutex);
469
470 /*
471  * Return non-zero if there are any tasks in RCU read-side critical
472  * sections blocking the current preemptible-RCU expedited grace period.
473  * If there is no preemptible-RCU expedited grace period currently in
474  * progress, returns zero unconditionally.
475  */
476 static int rcu_preempted_readers_exp(void)
477 {
478         return rcu_preempt_ctrlblk.exp_tasks != NULL;
479 }
480
481 /*
482  * Report the exit from RCU read-side critical section for the last task
483  * that queued itself during or before the current expedited preemptible-RCU
484  * grace period.
485  */
486 static void rcu_report_exp_done(void)
487 {
488         wake_up(&sync_rcu_preempt_exp_wq);
489 }
490
491 /*
492  * Wait for an rcu-preempt grace period, but expedite it.  The basic idea
493  * is to rely in the fact that there is but one CPU, and that it is
494  * illegal for a task to invoke synchronize_rcu_expedited() while in a
495  * preemptible-RCU read-side critical section.  Therefore, any such
496  * critical sections must correspond to blocked tasks, which must therefore
497  * be on the ->blkd_tasks list.  So just record the current head of the
498  * list in the ->exp_tasks pointer, and wait for all tasks including and
499  * after the task pointed to by ->exp_tasks to drain.
500  */
501 void synchronize_rcu_expedited(void)
502 {
503         unsigned long flags;
504         struct rcu_preempt_ctrlblk *rpcp = &rcu_preempt_ctrlblk;
505         unsigned long snap;
506
507         barrier(); /* ensure prior action seen before grace period. */
508
509         WARN_ON_ONCE(rcu_preempt_running_reader());
510
511         /*
512          * Acquire lock so that there is only one preemptible RCU grace
513          * period in flight.  Of course, if someone does the expedited
514          * grace period for us while we are acquiring the lock, just leave.
515          */
516         snap = sync_rcu_preempt_exp_count + 1;
517         mutex_lock(&sync_rcu_preempt_exp_mutex);
518         if (ULONG_CMP_LT(snap, sync_rcu_preempt_exp_count))
519                 goto unlock_mb_ret; /* Others did our work for us. */
520
521         local_irq_save(flags);
522
523         /*
524          * All RCU readers have to already be on blkd_tasks because
525          * we cannot legally be executing in an RCU read-side critical
526          * section.
527          */
528
529         /* Snapshot current head of ->blkd_tasks list. */
530         rpcp->exp_tasks = rpcp->blkd_tasks.next;
531         if (rpcp->exp_tasks == &rpcp->blkd_tasks)
532                 rpcp->exp_tasks = NULL;
533         local_irq_restore(flags);
534
535         /* Wait for tail of ->blkd_tasks list to drain. */
536         if (rcu_preempted_readers_exp())
537                 wait_event(sync_rcu_preempt_exp_wq,
538                            !rcu_preempted_readers_exp());
539
540         /* Clean up and exit. */
541         barrier(); /* ensure expedited GP seen before counter increment. */
542         sync_rcu_preempt_exp_count++;
543 unlock_mb_ret:
544         mutex_unlock(&sync_rcu_preempt_exp_mutex);
545         barrier(); /* ensure subsequent action seen after grace period. */
546 }
547 EXPORT_SYMBOL_GPL(synchronize_rcu_expedited);
548
549 /*
550  * Does preemptible RCU need the CPU to stay out of dynticks mode?
551  */
552 int rcu_preempt_needs_cpu(void)
553 {
554         if (!rcu_preempt_running_reader())
555                 rcu_preempt_cpu_qs();
556         return rcu_preempt_ctrlblk.rcb.rcucblist != NULL;
557 }
558
559 /*
560  * Check for a task exiting while in a preemptible -RCU read-side
561  * critical section, clean up if so.  No need to issue warnings,
562  * as debug_check_no_locks_held() already does this if lockdep
563  * is enabled.
564  */
565 void exit_rcu(void)
566 {
567         struct task_struct *t = current;
568
569         if (t->rcu_read_lock_nesting == 0)
570                 return;
571         t->rcu_read_lock_nesting = 1;
572         rcu_read_unlock();
573 }
574
575 #else /* #ifdef CONFIG_TINY_PREEMPT_RCU */
576
577 /*
578  * Because preemptible RCU does not exist, it never has any callbacks
579  * to check.
580  */
581 static void rcu_preempt_check_callbacks(void)
582 {
583 }
584
585 /*
586  * Because preemptible RCU does not exist, it never has any callbacks
587  * to remove.
588  */
589 static void rcu_preempt_remove_callbacks(struct rcu_ctrlblk *rcp)
590 {
591 }
592
593 /*
594  * Because preemptible RCU does not exist, it never has any callbacks
595  * to process.
596  */
597 static void rcu_preempt_process_callbacks(void)
598 {
599 }
600
601 #endif /* #else #ifdef CONFIG_TINY_PREEMPT_RCU */
602
603 #ifdef CONFIG_DEBUG_LOCK_ALLOC
604 #include <linux/kernel_stat.h>
605
606 /*
607  * During boot, we forgive RCU lockdep issues.  After this function is
608  * invoked, we start taking RCU lockdep issues seriously.
609  */
610 void __init rcu_scheduler_starting(void)
611 {
612         WARN_ON(nr_context_switches() > 0);
613         rcu_scheduler_active = 1;
614 }
615
616 #endif /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */