rcutiny.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. static long long rcu_dynticks_nesting = LLONG_MAX / 2;
  52. /* Common code for rcu_idle_enter() and rcu_irq_exit(), see kernel/rcutree.c. */
  53. static void rcu_idle_enter_common(void)
  54. {
  55. if (rcu_dynticks_nesting) {
  56. RCU_TRACE(trace_rcu_dyntick("--=", rcu_dynticks_nesting));
  57. return;
  58. }
  59. RCU_TRACE(trace_rcu_dyntick("Start", rcu_dynticks_nesting));
  60. if (!idle_cpu(smp_processor_id())) {
  61. WARN_ON_ONCE(1); /* must be idle task! */
  62. RCU_TRACE(trace_rcu_dyntick("Error on entry: not idle task",
  63. rcu_dynticks_nesting));
  64. ftrace_dump(DUMP_ALL);
  65. }
  66. rcu_sched_qs(0); /* implies rcu_bh_qsctr_inc(0) */
  67. }
  68. /*
  69. * Enter idle, which is an extended quiescent state if we have fully
  70. * entered that mode (i.e., if the new value of dynticks_nesting is zero).
  71. */
  72. void rcu_idle_enter(void)
  73. {
  74. unsigned long flags;
  75. local_irq_save(flags);
  76. rcu_dynticks_nesting = 0;
  77. rcu_idle_enter_common();
  78. local_irq_restore(flags);
  79. }
  80. /*
  81. * Exit an interrupt handler towards idle.
  82. */
  83. void rcu_irq_exit(void)
  84. {
  85. unsigned long flags;
  86. local_irq_save(flags);
  87. rcu_dynticks_nesting--;
  88. WARN_ON_ONCE(rcu_dynticks_nesting < 0);
  89. rcu_idle_enter_common();
  90. local_irq_restore(flags);
  91. }
  92. /* Common code for rcu_idle_exit() and rcu_irq_enter(), see kernel/rcutree.c. */
  93. static void rcu_idle_exit_common(long long oldval)
  94. {
  95. if (oldval) {
  96. RCU_TRACE(trace_rcu_dyntick("++=", rcu_dynticks_nesting));
  97. return;
  98. }
  99. RCU_TRACE(trace_rcu_dyntick("End", oldval));
  100. if (!idle_cpu(smp_processor_id())) {
  101. WARN_ON_ONCE(1); /* must be idle task! */
  102. RCU_TRACE(trace_rcu_dyntick("Error on exit: not idle task",
  103. oldval));
  104. ftrace_dump(DUMP_ALL);
  105. }
  106. }
  107. /*
  108. * Exit idle, so that we are no longer in an extended quiescent state.
  109. */
  110. void rcu_idle_exit(void)
  111. {
  112. unsigned long flags;
  113. long long oldval;
  114. local_irq_save(flags);
  115. oldval = rcu_dynticks_nesting;
  116. WARN_ON_ONCE(oldval != 0);
  117. rcu_dynticks_nesting = LLONG_MAX / 2;
  118. rcu_idle_exit_common(oldval);
  119. local_irq_restore(flags);
  120. }
  121. /*
  122. * Enter an interrupt handler, moving away from idle.
  123. */
  124. void rcu_irq_enter(void)
  125. {
  126. unsigned long flags;
  127. long long oldval;
  128. local_irq_save(flags);
  129. oldval = rcu_dynticks_nesting;
  130. rcu_dynticks_nesting++;
  131. WARN_ON_ONCE(rcu_dynticks_nesting == 0);
  132. rcu_idle_exit_common(oldval);
  133. local_irq_restore(flags);
  134. }
  135. #ifdef CONFIG_PROVE_RCU
  136. /*
  137. * Test whether RCU thinks that the current CPU is idle.
  138. */
  139. int rcu_is_cpu_idle(void)
  140. {
  141. return !rcu_dynticks_nesting;
  142. }
  143. EXPORT_SYMBOL(rcu_is_cpu_idle);
  144. #endif /* #ifdef CONFIG_PROVE_RCU */
  145. /*
  146. * Test whether the current CPU was interrupted from idle. Nested
  147. * interrupts don't count, we must be running at the first interrupt
  148. * level.
  149. */
  150. int rcu_is_cpu_rrupt_from_idle(void)
  151. {
  152. return rcu_dynticks_nesting <= 0;
  153. }
  154. /*
  155. * Helper function for rcu_sched_qs() and rcu_bh_qs().
  156. * Also irqs are disabled to avoid confusion due to interrupt handlers
  157. * invoking call_rcu().
  158. */
  159. static int rcu_qsctr_help(struct rcu_ctrlblk *rcp)
  160. {
  161. if (rcp->rcucblist != NULL &&
  162. rcp->donetail != rcp->curtail) {
  163. rcp->donetail = rcp->curtail;
  164. return 1;
  165. }
  166. return 0;
  167. }
  168. /*
  169. * Record an rcu quiescent state. And an rcu_bh quiescent state while we
  170. * are at it, given that any rcu quiescent state is also an rcu_bh
  171. * quiescent state. Use "+" instead of "||" to defeat short circuiting.
  172. */
  173. void rcu_sched_qs(int cpu)
  174. {
  175. unsigned long flags;
  176. local_irq_save(flags);
  177. if (rcu_qsctr_help(&rcu_sched_ctrlblk) +
  178. rcu_qsctr_help(&rcu_bh_ctrlblk))
  179. invoke_rcu_callbacks();
  180. local_irq_restore(flags);
  181. }
  182. /*
  183. * Record an rcu_bh quiescent state.
  184. */
  185. void rcu_bh_qs(int cpu)
  186. {
  187. unsigned long flags;
  188. local_irq_save(flags);
  189. if (rcu_qsctr_help(&rcu_bh_ctrlblk))
  190. invoke_rcu_callbacks();
  191. local_irq_restore(flags);
  192. }
  193. /*
  194. * Check to see if the scheduling-clock interrupt came from an extended
  195. * quiescent state, and, if so, tell RCU about it. This function must
  196. * be called from hardirq context. It is normally called from the
  197. * scheduling-clock interrupt.
  198. */
  199. void rcu_check_callbacks(int cpu, int user)
  200. {
  201. if (user || rcu_is_cpu_rrupt_from_idle())
  202. rcu_sched_qs(cpu);
  203. else if (!in_softirq())
  204. rcu_bh_qs(cpu);
  205. rcu_preempt_check_callbacks();
  206. }
  207. /*
  208. * Invoke the RCU callbacks on the specified rcu_ctrlkblk structure
  209. * whose grace period has elapsed.
  210. */
  211. static void __rcu_process_callbacks(struct rcu_ctrlblk *rcp)
  212. {
  213. char *rn = NULL;
  214. struct rcu_head *next, *list;
  215. unsigned long flags;
  216. RCU_TRACE(int cb_count = 0);
  217. /* If no RCU callbacks ready to invoke, just return. */
  218. if (&rcp->rcucblist == rcp->donetail) {
  219. RCU_TRACE(trace_rcu_batch_start(rcp->name, 0, -1));
  220. RCU_TRACE(trace_rcu_batch_end(rcp->name, 0));
  221. return;
  222. }
  223. /* Move the ready-to-invoke callbacks to a local list. */
  224. local_irq_save(flags);
  225. RCU_TRACE(trace_rcu_batch_start(rcp->name, 0, -1));
  226. list = rcp->rcucblist;
  227. rcp->rcucblist = *rcp->donetail;
  228. *rcp->donetail = NULL;
  229. if (rcp->curtail == rcp->donetail)
  230. rcp->curtail = &rcp->rcucblist;
  231. rcu_preempt_remove_callbacks(rcp);
  232. rcp->donetail = &rcp->rcucblist;
  233. local_irq_restore(flags);
  234. /* Invoke the callbacks on the local list. */
  235. RCU_TRACE(rn = rcp->name);
  236. while (list) {
  237. next = list->next;
  238. prefetch(next);
  239. debug_rcu_head_unqueue(list);
  240. local_bh_disable();
  241. __rcu_reclaim(rn, list);
  242. local_bh_enable();
  243. list = next;
  244. RCU_TRACE(cb_count++);
  245. }
  246. RCU_TRACE(rcu_trace_sub_qlen(rcp, cb_count));
  247. RCU_TRACE(trace_rcu_batch_end(rcp->name, cb_count));
  248. }
  249. static void rcu_process_callbacks(struct softirq_action *unused)
  250. {
  251. __rcu_process_callbacks(&rcu_sched_ctrlblk);
  252. __rcu_process_callbacks(&rcu_bh_ctrlblk);
  253. rcu_preempt_process_callbacks();
  254. }
  255. /*
  256. * Wait for a grace period to elapse. But it is illegal to invoke
  257. * synchronize_sched() from within an RCU read-side critical section.
  258. * Therefore, any legal call to synchronize_sched() is a quiescent
  259. * state, and so on a UP system, synchronize_sched() need do nothing.
  260. * Ditto for synchronize_rcu_bh(). (But Lai Jiangshan points out the
  261. * benefits of doing might_sleep() to reduce latency.)
  262. *
  263. * Cool, huh? (Due to Josh Triplett.)
  264. *
  265. * But we want to make this a static inline later. The cond_resched()
  266. * currently makes this problematic.
  267. */
  268. void synchronize_sched(void)
  269. {
  270. cond_resched();
  271. }
  272. EXPORT_SYMBOL_GPL(synchronize_sched);
  273. /*
  274. * Helper function for call_rcu() and call_rcu_bh().
  275. */
  276. static void __call_rcu(struct rcu_head *head,
  277. void (*func)(struct rcu_head *rcu),
  278. struct rcu_ctrlblk *rcp)
  279. {
  280. unsigned long flags;
  281. debug_rcu_head_queue(head);
  282. head->func = func;
  283. head->next = NULL;
  284. local_irq_save(flags);
  285. *rcp->curtail = head;
  286. rcp->curtail = &head->next;
  287. RCU_TRACE(rcp->qlen++);
  288. local_irq_restore(flags);
  289. }
  290. /*
  291. * Post an RCU callback to be invoked after the end of an RCU-sched grace
  292. * period. But since we have but one CPU, that would be after any
  293. * quiescent state.
  294. */
  295. void call_rcu_sched(struct rcu_head *head, void (*func)(struct rcu_head *rcu))
  296. {
  297. __call_rcu(head, func, &rcu_sched_ctrlblk);
  298. }
  299. EXPORT_SYMBOL_GPL(call_rcu_sched);
  300. /*
  301. * Post an RCU bottom-half callback to be invoked after any subsequent
  302. * quiescent state.
  303. */
  304. void call_rcu_bh(struct rcu_head *head, void (*func)(struct rcu_head *rcu))
  305. {
  306. __call_rcu(head, func, &rcu_bh_ctrlblk);
  307. }
  308. EXPORT_SYMBOL_GPL(call_rcu_bh);