rcutiny.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. * Read-Copy Update mechanism for mutual exclusion, the Bloatwatch edition.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. *
  18. * Copyright IBM Corporation, 2008
  19. *
  20. * Author: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
  21. *
  22. * For detailed explanation of Read-Copy Update mechanism see -
  23. * Documentation/RCU
  24. */
  25. #include <linux/completion.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/notifier.h>
  28. #include <linux/rcupdate.h>
  29. #include <linux/kernel.h>
  30. #include <linux/export.h>
  31. #include <linux/mutex.h>
  32. #include <linux/sched.h>
  33. #include <linux/types.h>
  34. #include <linux/init.h>
  35. #include <linux/time.h>
  36. #include <linux/cpu.h>
  37. #include <linux/prefetch.h>
  38. #ifdef CONFIG_RCU_TRACE
  39. #include <trace/events/rcu.h>
  40. #endif /* #else #ifdef CONFIG_RCU_TRACE */
  41. #include "rcu.h"
  42. /* Forward declarations for rcutiny_plugin.h. */
  43. struct rcu_ctrlblk;
  44. static void invoke_rcu_callbacks(void);
  45. static void __rcu_process_callbacks(struct rcu_ctrlblk *rcp);
  46. static void rcu_process_callbacks(struct softirq_action *unused);
  47. static void __call_rcu(struct rcu_head *head,
  48. void (*func)(struct rcu_head *rcu),
  49. struct rcu_ctrlblk *rcp);
  50. #include "rcutiny_plugin.h"
  51. #ifdef CONFIG_NO_HZ
  52. static long rcu_dynticks_nesting = 1;
  53. /*
  54. * Enter dynticks-idle mode, which is an extended quiescent state
  55. * if we have fully entered that mode (i.e., if the new value of
  56. * dynticks_nesting is zero).
  57. */
  58. void rcu_enter_nohz(void)
  59. {
  60. if (--rcu_dynticks_nesting == 0)
  61. rcu_sched_qs(0); /* implies rcu_bh_qsctr_inc(0) */
  62. }
  63. /*
  64. * Exit dynticks-idle mode, so that we are no longer in an extended
  65. * quiescent state.
  66. */
  67. void rcu_exit_nohz(void)
  68. {
  69. rcu_dynticks_nesting++;
  70. }
  71. #endif /* #ifdef CONFIG_NO_HZ */
  72. /*
  73. * Helper function for rcu_sched_qs() and rcu_bh_qs().
  74. * Also irqs are disabled to avoid confusion due to interrupt handlers
  75. * invoking call_rcu().
  76. */
  77. static int rcu_qsctr_help(struct rcu_ctrlblk *rcp)
  78. {
  79. if (rcp->rcucblist != NULL &&
  80. rcp->donetail != rcp->curtail) {
  81. rcp->donetail = rcp->curtail;
  82. return 1;
  83. }
  84. return 0;
  85. }
  86. /*
  87. * Record an rcu quiescent state. And an rcu_bh quiescent state while we
  88. * are at it, given that any rcu quiescent state is also an rcu_bh
  89. * quiescent state. Use "+" instead of "||" to defeat short circuiting.
  90. */
  91. void rcu_sched_qs(int cpu)
  92. {
  93. unsigned long flags;
  94. local_irq_save(flags);
  95. if (rcu_qsctr_help(&rcu_sched_ctrlblk) +
  96. rcu_qsctr_help(&rcu_bh_ctrlblk))
  97. invoke_rcu_callbacks();
  98. local_irq_restore(flags);
  99. }
  100. /*
  101. * Record an rcu_bh quiescent state.
  102. */
  103. void rcu_bh_qs(int cpu)
  104. {
  105. unsigned long flags;
  106. local_irq_save(flags);
  107. if (rcu_qsctr_help(&rcu_bh_ctrlblk))
  108. invoke_rcu_callbacks();
  109. local_irq_restore(flags);
  110. }
  111. /*
  112. * Check to see if the scheduling-clock interrupt came from an extended
  113. * quiescent state, and, if so, tell RCU about it.
  114. */
  115. void rcu_check_callbacks(int cpu, int user)
  116. {
  117. if (user ||
  118. (idle_cpu(cpu) &&
  119. !in_softirq() &&
  120. hardirq_count() <= (1 << HARDIRQ_SHIFT)))
  121. rcu_sched_qs(cpu);
  122. else if (!in_softirq())
  123. rcu_bh_qs(cpu);
  124. rcu_preempt_check_callbacks();
  125. }
  126. /*
  127. * Invoke the RCU callbacks on the specified rcu_ctrlkblk structure
  128. * whose grace period has elapsed.
  129. */
  130. static void __rcu_process_callbacks(struct rcu_ctrlblk *rcp)
  131. {
  132. char *rn = NULL;
  133. struct rcu_head *next, *list;
  134. unsigned long flags;
  135. RCU_TRACE(int cb_count = 0);
  136. /* If no RCU callbacks ready to invoke, just return. */
  137. if (&rcp->rcucblist == rcp->donetail) {
  138. RCU_TRACE(trace_rcu_batch_start(rcp->name, 0, -1));
  139. RCU_TRACE(trace_rcu_batch_end(rcp->name, 0));
  140. return;
  141. }
  142. /* Move the ready-to-invoke callbacks to a local list. */
  143. local_irq_save(flags);
  144. RCU_TRACE(trace_rcu_batch_start(rcp->name, 0, -1));
  145. list = rcp->rcucblist;
  146. rcp->rcucblist = *rcp->donetail;
  147. *rcp->donetail = NULL;
  148. if (rcp->curtail == rcp->donetail)
  149. rcp->curtail = &rcp->rcucblist;
  150. rcu_preempt_remove_callbacks(rcp);
  151. rcp->donetail = &rcp->rcucblist;
  152. local_irq_restore(flags);
  153. /* Invoke the callbacks on the local list. */
  154. RCU_TRACE(rn = rcp->name);
  155. while (list) {
  156. next = list->next;
  157. prefetch(next);
  158. debug_rcu_head_unqueue(list);
  159. local_bh_disable();
  160. __rcu_reclaim(rn, list);
  161. local_bh_enable();
  162. list = next;
  163. RCU_TRACE(cb_count++);
  164. }
  165. RCU_TRACE(rcu_trace_sub_qlen(rcp, cb_count));
  166. RCU_TRACE(trace_rcu_batch_end(rcp->name, cb_count));
  167. }
  168. static void rcu_process_callbacks(struct softirq_action *unused)
  169. {
  170. __rcu_process_callbacks(&rcu_sched_ctrlblk);
  171. __rcu_process_callbacks(&rcu_bh_ctrlblk);
  172. rcu_preempt_process_callbacks();
  173. }
  174. /*
  175. * Wait for a grace period to elapse. But it is illegal to invoke
  176. * synchronize_sched() from within an RCU read-side critical section.
  177. * Therefore, any legal call to synchronize_sched() is a quiescent
  178. * state, and so on a UP system, synchronize_sched() need do nothing.
  179. * Ditto for synchronize_rcu_bh(). (But Lai Jiangshan points out the
  180. * benefits of doing might_sleep() to reduce latency.)
  181. *
  182. * Cool, huh? (Due to Josh Triplett.)
  183. *
  184. * But we want to make this a static inline later. The cond_resched()
  185. * currently makes this problematic.
  186. */
  187. void synchronize_sched(void)
  188. {
  189. cond_resched();
  190. }
  191. EXPORT_SYMBOL_GPL(synchronize_sched);
  192. /*
  193. * Helper function for call_rcu() and call_rcu_bh().
  194. */
  195. static void __call_rcu(struct rcu_head *head,
  196. void (*func)(struct rcu_head *rcu),
  197. struct rcu_ctrlblk *rcp)
  198. {
  199. unsigned long flags;
  200. debug_rcu_head_queue(head);
  201. head->func = func;
  202. head->next = NULL;
  203. local_irq_save(flags);
  204. *rcp->curtail = head;
  205. rcp->curtail = &head->next;
  206. RCU_TRACE(rcp->qlen++);
  207. local_irq_restore(flags);
  208. }
  209. /*
  210. * Post an RCU callback to be invoked after the end of an RCU-sched grace
  211. * period. But since we have but one CPU, that would be after any
  212. * quiescent state.
  213. */
  214. void call_rcu_sched(struct rcu_head *head, void (*func)(struct rcu_head *rcu))
  215. {
  216. __call_rcu(head, func, &rcu_sched_ctrlblk);
  217. }
  218. EXPORT_SYMBOL_GPL(call_rcu_sched);
  219. /*
  220. * Post an RCU bottom-half callback to be invoked after any subsequent
  221. * quiescent state.
  222. */
  223. void call_rcu_bh(struct rcu_head *head, void (*func)(struct rcu_head *rcu))
  224. {
  225. __call_rcu(head, func, &rcu_bh_ctrlblk);
  226. }
  227. EXPORT_SYMBOL_GPL(call_rcu_bh);