rcupdate.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /*
  2. * Read-Copy Update mechanism for mutual exclusion
  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, 2001
  19. *
  20. * Authors: Dipankar Sarma <dipankar@in.ibm.com>
  21. * Manfred Spraul <manfred@colorfullife.com>
  22. *
  23. * Based on the original work by Paul McKenney <paulmck@us.ibm.com>
  24. * and inputs from Rusty Russell, Andrea Arcangeli and Andi Kleen.
  25. * Papers:
  26. * http://www.rdrop.com/users/paulmck/paper/rclockpdcsproof.pdf
  27. * http://lse.sourceforge.net/locking/rclock_OLS.2001.05.01c.sc.pdf (OLS2001)
  28. *
  29. * For detailed explanation of Read-Copy Update mechanism see -
  30. * http://lse.sourceforge.net/locking/rcupdate.html
  31. *
  32. */
  33. #include <linux/types.h>
  34. #include <linux/kernel.h>
  35. #include <linux/init.h>
  36. #include <linux/spinlock.h>
  37. #include <linux/smp.h>
  38. #include <linux/interrupt.h>
  39. #include <linux/sched.h>
  40. #include <linux/atomic.h>
  41. #include <linux/bitops.h>
  42. #include <linux/percpu.h>
  43. #include <linux/notifier.h>
  44. #include <linux/cpu.h>
  45. #include <linux/mutex.h>
  46. #include <linux/export.h>
  47. #include <linux/hardirq.h>
  48. #include <linux/module.h>
  49. #define CREATE_TRACE_POINTS
  50. #include <trace/events/rcu.h>
  51. #include "rcu.h"
  52. module_param(rcu_expedited, int, 0);
  53. #ifdef CONFIG_PREEMPT_RCU
  54. /*
  55. * Check for a task exiting while in a preemptible-RCU read-side
  56. * critical section, clean up if so. No need to issue warnings,
  57. * as debug_check_no_locks_held() already does this if lockdep
  58. * is enabled.
  59. */
  60. void exit_rcu(void)
  61. {
  62. struct task_struct *t = current;
  63. if (likely(list_empty(&current->rcu_node_entry)))
  64. return;
  65. t->rcu_read_lock_nesting = 1;
  66. barrier();
  67. t->rcu_read_unlock_special = RCU_READ_UNLOCK_BLOCKED;
  68. __rcu_read_unlock();
  69. }
  70. #else /* #ifdef CONFIG_PREEMPT_RCU */
  71. void exit_rcu(void)
  72. {
  73. }
  74. #endif /* #else #ifdef CONFIG_PREEMPT_RCU */
  75. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  76. static struct lock_class_key rcu_lock_key;
  77. struct lockdep_map rcu_lock_map =
  78. STATIC_LOCKDEP_MAP_INIT("rcu_read_lock", &rcu_lock_key);
  79. EXPORT_SYMBOL_GPL(rcu_lock_map);
  80. static struct lock_class_key rcu_bh_lock_key;
  81. struct lockdep_map rcu_bh_lock_map =
  82. STATIC_LOCKDEP_MAP_INIT("rcu_read_lock_bh", &rcu_bh_lock_key);
  83. EXPORT_SYMBOL_GPL(rcu_bh_lock_map);
  84. static struct lock_class_key rcu_sched_lock_key;
  85. struct lockdep_map rcu_sched_lock_map =
  86. STATIC_LOCKDEP_MAP_INIT("rcu_read_lock_sched", &rcu_sched_lock_key);
  87. EXPORT_SYMBOL_GPL(rcu_sched_lock_map);
  88. #endif
  89. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  90. int debug_lockdep_rcu_enabled(void)
  91. {
  92. return rcu_scheduler_active && debug_locks &&
  93. current->lockdep_recursion == 0;
  94. }
  95. EXPORT_SYMBOL_GPL(debug_lockdep_rcu_enabled);
  96. /**
  97. * rcu_read_lock_bh_held() - might we be in RCU-bh read-side critical section?
  98. *
  99. * Check for bottom half being disabled, which covers both the
  100. * CONFIG_PROVE_RCU and not cases. Note that if someone uses
  101. * rcu_read_lock_bh(), but then later enables BH, lockdep (if enabled)
  102. * will show the situation. This is useful for debug checks in functions
  103. * that require that they be called within an RCU read-side critical
  104. * section.
  105. *
  106. * Check debug_lockdep_rcu_enabled() to prevent false positives during boot.
  107. *
  108. * Note that rcu_read_lock() is disallowed if the CPU is either idle or
  109. * offline from an RCU perspective, so check for those as well.
  110. */
  111. int rcu_read_lock_bh_held(void)
  112. {
  113. if (!debug_lockdep_rcu_enabled())
  114. return 1;
  115. if (rcu_is_cpu_idle())
  116. return 0;
  117. if (!rcu_lockdep_current_cpu_online())
  118. return 0;
  119. return in_softirq() || irqs_disabled();
  120. }
  121. EXPORT_SYMBOL_GPL(rcu_read_lock_bh_held);
  122. #endif /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
  123. struct rcu_synchronize {
  124. struct rcu_head head;
  125. struct completion completion;
  126. };
  127. /*
  128. * Awaken the corresponding synchronize_rcu() instance now that a
  129. * grace period has elapsed.
  130. */
  131. static void wakeme_after_rcu(struct rcu_head *head)
  132. {
  133. struct rcu_synchronize *rcu;
  134. rcu = container_of(head, struct rcu_synchronize, head);
  135. complete(&rcu->completion);
  136. }
  137. void wait_rcu_gp(call_rcu_func_t crf)
  138. {
  139. struct rcu_synchronize rcu;
  140. init_rcu_head_on_stack(&rcu.head);
  141. init_completion(&rcu.completion);
  142. /* Will wake me after RCU finished. */
  143. crf(&rcu.head, wakeme_after_rcu);
  144. /* Wait for it. */
  145. wait_for_completion(&rcu.completion);
  146. destroy_rcu_head_on_stack(&rcu.head);
  147. }
  148. EXPORT_SYMBOL_GPL(wait_rcu_gp);
  149. #ifdef CONFIG_PROVE_RCU
  150. /*
  151. * wrapper function to avoid #include problems.
  152. */
  153. int rcu_my_thread_group_empty(void)
  154. {
  155. return thread_group_empty(current);
  156. }
  157. EXPORT_SYMBOL_GPL(rcu_my_thread_group_empty);
  158. #endif /* #ifdef CONFIG_PROVE_RCU */
  159. #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD
  160. static inline void debug_init_rcu_head(struct rcu_head *head)
  161. {
  162. debug_object_init(head, &rcuhead_debug_descr);
  163. }
  164. static inline void debug_rcu_head_free(struct rcu_head *head)
  165. {
  166. debug_object_free(head, &rcuhead_debug_descr);
  167. }
  168. /*
  169. * fixup_init is called when:
  170. * - an active object is initialized
  171. */
  172. static int rcuhead_fixup_init(void *addr, enum debug_obj_state state)
  173. {
  174. struct rcu_head *head = addr;
  175. switch (state) {
  176. case ODEBUG_STATE_ACTIVE:
  177. /*
  178. * Ensure that queued callbacks are all executed.
  179. * If we detect that we are nested in a RCU read-side critical
  180. * section, we should simply fail, otherwise we would deadlock.
  181. * In !PREEMPT configurations, there is no way to tell if we are
  182. * in a RCU read-side critical section or not, so we never
  183. * attempt any fixup and just print a warning.
  184. */
  185. #ifndef CONFIG_PREEMPT
  186. WARN_ON_ONCE(1);
  187. return 0;
  188. #endif
  189. if (rcu_preempt_depth() != 0 || preempt_count() != 0 ||
  190. irqs_disabled()) {
  191. WARN_ON_ONCE(1);
  192. return 0;
  193. }
  194. rcu_barrier();
  195. rcu_barrier_sched();
  196. rcu_barrier_bh();
  197. debug_object_init(head, &rcuhead_debug_descr);
  198. return 1;
  199. default:
  200. return 0;
  201. }
  202. }
  203. /*
  204. * fixup_activate is called when:
  205. * - an active object is activated
  206. * - an unknown object is activated (might be a statically initialized object)
  207. * Activation is performed internally by call_rcu().
  208. */
  209. static int rcuhead_fixup_activate(void *addr, enum debug_obj_state state)
  210. {
  211. struct rcu_head *head = addr;
  212. switch (state) {
  213. case ODEBUG_STATE_NOTAVAILABLE:
  214. /*
  215. * This is not really a fixup. We just make sure that it is
  216. * tracked in the object tracker.
  217. */
  218. debug_object_init(head, &rcuhead_debug_descr);
  219. debug_object_activate(head, &rcuhead_debug_descr);
  220. return 0;
  221. case ODEBUG_STATE_ACTIVE:
  222. /*
  223. * Ensure that queued callbacks are all executed.
  224. * If we detect that we are nested in a RCU read-side critical
  225. * section, we should simply fail, otherwise we would deadlock.
  226. * In !PREEMPT configurations, there is no way to tell if we are
  227. * in a RCU read-side critical section or not, so we never
  228. * attempt any fixup and just print a warning.
  229. */
  230. #ifndef CONFIG_PREEMPT
  231. WARN_ON_ONCE(1);
  232. return 0;
  233. #endif
  234. if (rcu_preempt_depth() != 0 || preempt_count() != 0 ||
  235. irqs_disabled()) {
  236. WARN_ON_ONCE(1);
  237. return 0;
  238. }
  239. rcu_barrier();
  240. rcu_barrier_sched();
  241. rcu_barrier_bh();
  242. debug_object_activate(head, &rcuhead_debug_descr);
  243. return 1;
  244. default:
  245. return 0;
  246. }
  247. }
  248. /*
  249. * fixup_free is called when:
  250. * - an active object is freed
  251. */
  252. static int rcuhead_fixup_free(void *addr, enum debug_obj_state state)
  253. {
  254. struct rcu_head *head = addr;
  255. switch (state) {
  256. case ODEBUG_STATE_ACTIVE:
  257. /*
  258. * Ensure that queued callbacks are all executed.
  259. * If we detect that we are nested in a RCU read-side critical
  260. * section, we should simply fail, otherwise we would deadlock.
  261. * In !PREEMPT configurations, there is no way to tell if we are
  262. * in a RCU read-side critical section or not, so we never
  263. * attempt any fixup and just print a warning.
  264. */
  265. #ifndef CONFIG_PREEMPT
  266. WARN_ON_ONCE(1);
  267. return 0;
  268. #endif
  269. if (rcu_preempt_depth() != 0 || preempt_count() != 0 ||
  270. irqs_disabled()) {
  271. WARN_ON_ONCE(1);
  272. return 0;
  273. }
  274. rcu_barrier();
  275. rcu_barrier_sched();
  276. rcu_barrier_bh();
  277. debug_object_free(head, &rcuhead_debug_descr);
  278. return 1;
  279. default:
  280. return 0;
  281. }
  282. }
  283. /**
  284. * init_rcu_head_on_stack() - initialize on-stack rcu_head for debugobjects
  285. * @head: pointer to rcu_head structure to be initialized
  286. *
  287. * This function informs debugobjects of a new rcu_head structure that
  288. * has been allocated as an auto variable on the stack. This function
  289. * is not required for rcu_head structures that are statically defined or
  290. * that are dynamically allocated on the heap. This function has no
  291. * effect for !CONFIG_DEBUG_OBJECTS_RCU_HEAD kernel builds.
  292. */
  293. void init_rcu_head_on_stack(struct rcu_head *head)
  294. {
  295. debug_object_init_on_stack(head, &rcuhead_debug_descr);
  296. }
  297. EXPORT_SYMBOL_GPL(init_rcu_head_on_stack);
  298. /**
  299. * destroy_rcu_head_on_stack() - destroy on-stack rcu_head for debugobjects
  300. * @head: pointer to rcu_head structure to be initialized
  301. *
  302. * This function informs debugobjects that an on-stack rcu_head structure
  303. * is about to go out of scope. As with init_rcu_head_on_stack(), this
  304. * function is not required for rcu_head structures that are statically
  305. * defined or that are dynamically allocated on the heap. Also as with
  306. * init_rcu_head_on_stack(), this function has no effect for
  307. * !CONFIG_DEBUG_OBJECTS_RCU_HEAD kernel builds.
  308. */
  309. void destroy_rcu_head_on_stack(struct rcu_head *head)
  310. {
  311. debug_object_free(head, &rcuhead_debug_descr);
  312. }
  313. EXPORT_SYMBOL_GPL(destroy_rcu_head_on_stack);
  314. struct debug_obj_descr rcuhead_debug_descr = {
  315. .name = "rcu_head",
  316. .fixup_init = rcuhead_fixup_init,
  317. .fixup_activate = rcuhead_fixup_activate,
  318. .fixup_free = rcuhead_fixup_free,
  319. };
  320. EXPORT_SYMBOL_GPL(rcuhead_debug_descr);
  321. #endif /* #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD */
  322. #if defined(CONFIG_TREE_RCU) || defined(CONFIG_TREE_PREEMPT_RCU) || defined(CONFIG_RCU_TRACE)
  323. void do_trace_rcu_torture_read(char *rcutorturename, struct rcu_head *rhp)
  324. {
  325. trace_rcu_torture_read(rcutorturename, rhp);
  326. }
  327. EXPORT_SYMBOL_GPL(do_trace_rcu_torture_read);
  328. #else
  329. #define do_trace_rcu_torture_read(rcutorturename, rhp) do { } while (0)
  330. #endif