rcutiny.c 9.6 KB

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