rcutiny.c 10 KB

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