rcupdate.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. #define CREATE_TRACE_POINTS
  49. #include <trace/events/rcu.h>
  50. #include "rcu.h"
  51. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  52. static struct lock_class_key rcu_lock_key;
  53. struct lockdep_map rcu_lock_map =
  54. STATIC_LOCKDEP_MAP_INIT("rcu_read_lock", &rcu_lock_key);
  55. EXPORT_SYMBOL_GPL(rcu_lock_map);
  56. static struct lock_class_key rcu_bh_lock_key;
  57. struct lockdep_map rcu_bh_lock_map =
  58. STATIC_LOCKDEP_MAP_INIT("rcu_read_lock_bh", &rcu_bh_lock_key);
  59. EXPORT_SYMBOL_GPL(rcu_bh_lock_map);
  60. static struct lock_class_key rcu_sched_lock_key;
  61. struct lockdep_map rcu_sched_lock_map =
  62. STATIC_LOCKDEP_MAP_INIT("rcu_read_lock_sched", &rcu_sched_lock_key);
  63. EXPORT_SYMBOL_GPL(rcu_sched_lock_map);
  64. #endif
  65. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  66. int debug_lockdep_rcu_enabled(void)
  67. {
  68. return rcu_scheduler_active && debug_locks &&
  69. current->lockdep_recursion == 0;
  70. }
  71. EXPORT_SYMBOL_GPL(debug_lockdep_rcu_enabled);
  72. /**
  73. * rcu_read_lock_bh_held() - might we be in RCU-bh read-side critical section?
  74. *
  75. * Check for bottom half being disabled, which covers both the
  76. * CONFIG_PROVE_RCU and not cases. Note that if someone uses
  77. * rcu_read_lock_bh(), but then later enables BH, lockdep (if enabled)
  78. * will show the situation. This is useful for debug checks in functions
  79. * that require that they be called within an RCU read-side critical
  80. * section.
  81. *
  82. * Check debug_lockdep_rcu_enabled() to prevent false positives during boot.
  83. */
  84. int rcu_read_lock_bh_held(void)
  85. {
  86. if (!debug_lockdep_rcu_enabled())
  87. return 1;
  88. return in_softirq() || irqs_disabled();
  89. }
  90. EXPORT_SYMBOL_GPL(rcu_read_lock_bh_held);
  91. #endif /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
  92. struct rcu_synchronize {
  93. struct rcu_head head;
  94. struct completion completion;
  95. };
  96. /*
  97. * Awaken the corresponding synchronize_rcu() instance now that a
  98. * grace period has elapsed.
  99. */
  100. static void wakeme_after_rcu(struct rcu_head *head)
  101. {
  102. struct rcu_synchronize *rcu;
  103. rcu = container_of(head, struct rcu_synchronize, head);
  104. complete(&rcu->completion);
  105. }
  106. void wait_rcu_gp(call_rcu_func_t crf)
  107. {
  108. struct rcu_synchronize rcu;
  109. init_rcu_head_on_stack(&rcu.head);
  110. init_completion(&rcu.completion);
  111. /* Will wake me after RCU finished. */
  112. crf(&rcu.head, wakeme_after_rcu);
  113. /* Wait for it. */
  114. wait_for_completion(&rcu.completion);
  115. destroy_rcu_head_on_stack(&rcu.head);
  116. }
  117. EXPORT_SYMBOL_GPL(wait_rcu_gp);
  118. #ifdef CONFIG_PROVE_RCU
  119. /*
  120. * wrapper function to avoid #include problems.
  121. */
  122. int rcu_my_thread_group_empty(void)
  123. {
  124. return thread_group_empty(current);
  125. }
  126. EXPORT_SYMBOL_GPL(rcu_my_thread_group_empty);
  127. #endif /* #ifdef CONFIG_PROVE_RCU */
  128. #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD
  129. static inline void debug_init_rcu_head(struct rcu_head *head)
  130. {
  131. debug_object_init(head, &rcuhead_debug_descr);
  132. }
  133. static inline void debug_rcu_head_free(struct rcu_head *head)
  134. {
  135. debug_object_free(head, &rcuhead_debug_descr);
  136. }
  137. /*
  138. * fixup_init is called when:
  139. * - an active object is initialized
  140. */
  141. static int rcuhead_fixup_init(void *addr, enum debug_obj_state state)
  142. {
  143. struct rcu_head *head = addr;
  144. switch (state) {
  145. case ODEBUG_STATE_ACTIVE:
  146. /*
  147. * Ensure that queued callbacks are all executed.
  148. * If we detect that we are nested in a RCU read-side critical
  149. * section, we should simply fail, otherwise we would deadlock.
  150. * In !PREEMPT configurations, there is no way to tell if we are
  151. * in a RCU read-side critical section or not, so we never
  152. * attempt any fixup and just print a warning.
  153. */
  154. #ifndef CONFIG_PREEMPT
  155. WARN_ON_ONCE(1);
  156. return 0;
  157. #endif
  158. if (rcu_preempt_depth() != 0 || preempt_count() != 0 ||
  159. irqs_disabled()) {
  160. WARN_ON_ONCE(1);
  161. return 0;
  162. }
  163. rcu_barrier();
  164. rcu_barrier_sched();
  165. rcu_barrier_bh();
  166. debug_object_init(head, &rcuhead_debug_descr);
  167. return 1;
  168. default:
  169. return 0;
  170. }
  171. }
  172. /*
  173. * fixup_activate is called when:
  174. * - an active object is activated
  175. * - an unknown object is activated (might be a statically initialized object)
  176. * Activation is performed internally by call_rcu().
  177. */
  178. static int rcuhead_fixup_activate(void *addr, enum debug_obj_state state)
  179. {
  180. struct rcu_head *head = addr;
  181. switch (state) {
  182. case ODEBUG_STATE_NOTAVAILABLE:
  183. /*
  184. * This is not really a fixup. We just make sure that it is
  185. * tracked in the object tracker.
  186. */
  187. debug_object_init(head, &rcuhead_debug_descr);
  188. debug_object_activate(head, &rcuhead_debug_descr);
  189. return 0;
  190. case ODEBUG_STATE_ACTIVE:
  191. /*
  192. * Ensure that queued callbacks are all executed.
  193. * If we detect that we are nested in a RCU read-side critical
  194. * section, we should simply fail, otherwise we would deadlock.
  195. * In !PREEMPT configurations, there is no way to tell if we are
  196. * in a RCU read-side critical section or not, so we never
  197. * attempt any fixup and just print a warning.
  198. */
  199. #ifndef CONFIG_PREEMPT
  200. WARN_ON_ONCE(1);
  201. return 0;
  202. #endif
  203. if (rcu_preempt_depth() != 0 || preempt_count() != 0 ||
  204. irqs_disabled()) {
  205. WARN_ON_ONCE(1);
  206. return 0;
  207. }
  208. rcu_barrier();
  209. rcu_barrier_sched();
  210. rcu_barrier_bh();
  211. debug_object_activate(head, &rcuhead_debug_descr);
  212. return 1;
  213. default:
  214. return 0;
  215. }
  216. }
  217. /*
  218. * fixup_free is called when:
  219. * - an active object is freed
  220. */
  221. static int rcuhead_fixup_free(void *addr, enum debug_obj_state state)
  222. {
  223. struct rcu_head *head = addr;
  224. switch (state) {
  225. case ODEBUG_STATE_ACTIVE:
  226. /*
  227. * Ensure that queued callbacks are all executed.
  228. * If we detect that we are nested in a RCU read-side critical
  229. * section, we should simply fail, otherwise we would deadlock.
  230. * In !PREEMPT configurations, there is no way to tell if we are
  231. * in a RCU read-side critical section or not, so we never
  232. * attempt any fixup and just print a warning.
  233. */
  234. #ifndef CONFIG_PREEMPT
  235. WARN_ON_ONCE(1);
  236. return 0;
  237. #endif
  238. if (rcu_preempt_depth() != 0 || preempt_count() != 0 ||
  239. irqs_disabled()) {
  240. WARN_ON_ONCE(1);
  241. return 0;
  242. }
  243. rcu_barrier();
  244. rcu_barrier_sched();
  245. rcu_barrier_bh();
  246. debug_object_free(head, &rcuhead_debug_descr);
  247. return 1;
  248. default:
  249. return 0;
  250. }
  251. }
  252. /**
  253. * init_rcu_head_on_stack() - initialize on-stack rcu_head for debugobjects
  254. * @head: pointer to rcu_head structure to be initialized
  255. *
  256. * This function informs debugobjects of a new rcu_head structure that
  257. * has been allocated as an auto variable on the stack. This function
  258. * is not required for rcu_head structures that are statically defined or
  259. * that are dynamically allocated on the heap. This function has no
  260. * effect for !CONFIG_DEBUG_OBJECTS_RCU_HEAD kernel builds.
  261. */
  262. void init_rcu_head_on_stack(struct rcu_head *head)
  263. {
  264. debug_object_init_on_stack(head, &rcuhead_debug_descr);
  265. }
  266. EXPORT_SYMBOL_GPL(init_rcu_head_on_stack);
  267. /**
  268. * destroy_rcu_head_on_stack() - destroy on-stack rcu_head for debugobjects
  269. * @head: pointer to rcu_head structure to be initialized
  270. *
  271. * This function informs debugobjects that an on-stack rcu_head structure
  272. * is about to go out of scope. As with init_rcu_head_on_stack(), this
  273. * function is not required for rcu_head structures that are statically
  274. * defined or that are dynamically allocated on the heap. Also as with
  275. * init_rcu_head_on_stack(), this function has no effect for
  276. * !CONFIG_DEBUG_OBJECTS_RCU_HEAD kernel builds.
  277. */
  278. void destroy_rcu_head_on_stack(struct rcu_head *head)
  279. {
  280. debug_object_free(head, &rcuhead_debug_descr);
  281. }
  282. EXPORT_SYMBOL_GPL(destroy_rcu_head_on_stack);
  283. struct debug_obj_descr rcuhead_debug_descr = {
  284. .name = "rcu_head",
  285. .fixup_init = rcuhead_fixup_init,
  286. .fixup_activate = rcuhead_fixup_activate,
  287. .fixup_free = rcuhead_fixup_free,
  288. };
  289. EXPORT_SYMBOL_GPL(rcuhead_debug_descr);
  290. #endif /* #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD */