tiny.c 10 KB

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