rcupdate.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. if (rcu_is_cpu_idle())
  89. return 0;
  90. return in_softirq() || irqs_disabled();
  91. }
  92. EXPORT_SYMBOL_GPL(rcu_read_lock_bh_held);
  93. #endif /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
  94. struct rcu_synchronize {
  95. struct rcu_head head;
  96. struct completion completion;
  97. };
  98. /*
  99. * Awaken the corresponding synchronize_rcu() instance now that a
  100. * grace period has elapsed.
  101. */
  102. static void wakeme_after_rcu(struct rcu_head *head)
  103. {
  104. struct rcu_synchronize *rcu;
  105. rcu = container_of(head, struct rcu_synchronize, head);
  106. complete(&rcu->completion);
  107. }
  108. void wait_rcu_gp(call_rcu_func_t crf)
  109. {
  110. struct rcu_synchronize rcu;
  111. init_rcu_head_on_stack(&rcu.head);
  112. init_completion(&rcu.completion);
  113. /* Will wake me after RCU finished. */
  114. crf(&rcu.head, wakeme_after_rcu);
  115. /* Wait for it. */
  116. wait_for_completion(&rcu.completion);
  117. destroy_rcu_head_on_stack(&rcu.head);
  118. }
  119. EXPORT_SYMBOL_GPL(wait_rcu_gp);
  120. #ifdef CONFIG_PROVE_RCU
  121. /*
  122. * wrapper function to avoid #include problems.
  123. */
  124. int rcu_my_thread_group_empty(void)
  125. {
  126. return thread_group_empty(current);
  127. }
  128. EXPORT_SYMBOL_GPL(rcu_my_thread_group_empty);
  129. #endif /* #ifdef CONFIG_PROVE_RCU */
  130. #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD
  131. static inline void debug_init_rcu_head(struct rcu_head *head)
  132. {
  133. debug_object_init(head, &rcuhead_debug_descr);
  134. }
  135. static inline void debug_rcu_head_free(struct rcu_head *head)
  136. {
  137. debug_object_free(head, &rcuhead_debug_descr);
  138. }
  139. /*
  140. * fixup_init is called when:
  141. * - an active object is initialized
  142. */
  143. static int rcuhead_fixup_init(void *addr, enum debug_obj_state state)
  144. {
  145. struct rcu_head *head = addr;
  146. switch (state) {
  147. case ODEBUG_STATE_ACTIVE:
  148. /*
  149. * Ensure that queued callbacks are all executed.
  150. * If we detect that we are nested in a RCU read-side critical
  151. * section, we should simply fail, otherwise we would deadlock.
  152. * In !PREEMPT configurations, there is no way to tell if we are
  153. * in a RCU read-side critical section or not, so we never
  154. * attempt any fixup and just print a warning.
  155. */
  156. #ifndef CONFIG_PREEMPT
  157. WARN_ON_ONCE(1);
  158. return 0;
  159. #endif
  160. if (rcu_preempt_depth() != 0 || preempt_count() != 0 ||
  161. irqs_disabled()) {
  162. WARN_ON_ONCE(1);
  163. return 0;
  164. }
  165. rcu_barrier();
  166. rcu_barrier_sched();
  167. rcu_barrier_bh();
  168. debug_object_init(head, &rcuhead_debug_descr);
  169. return 1;
  170. default:
  171. return 0;
  172. }
  173. }
  174. /*
  175. * fixup_activate is called when:
  176. * - an active object is activated
  177. * - an unknown object is activated (might be a statically initialized object)
  178. * Activation is performed internally by call_rcu().
  179. */
  180. static int rcuhead_fixup_activate(void *addr, enum debug_obj_state state)
  181. {
  182. struct rcu_head *head = addr;
  183. switch (state) {
  184. case ODEBUG_STATE_NOTAVAILABLE:
  185. /*
  186. * This is not really a fixup. We just make sure that it is
  187. * tracked in the object tracker.
  188. */
  189. debug_object_init(head, &rcuhead_debug_descr);
  190. debug_object_activate(head, &rcuhead_debug_descr);
  191. return 0;
  192. case ODEBUG_STATE_ACTIVE:
  193. /*
  194. * Ensure that queued callbacks are all executed.
  195. * If we detect that we are nested in a RCU read-side critical
  196. * section, we should simply fail, otherwise we would deadlock.
  197. * In !PREEMPT configurations, there is no way to tell if we are
  198. * in a RCU read-side critical section or not, so we never
  199. * attempt any fixup and just print a warning.
  200. */
  201. #ifndef CONFIG_PREEMPT
  202. WARN_ON_ONCE(1);
  203. return 0;
  204. #endif
  205. if (rcu_preempt_depth() != 0 || preempt_count() != 0 ||
  206. irqs_disabled()) {
  207. WARN_ON_ONCE(1);
  208. return 0;
  209. }
  210. rcu_barrier();
  211. rcu_barrier_sched();
  212. rcu_barrier_bh();
  213. debug_object_activate(head, &rcuhead_debug_descr);
  214. return 1;
  215. default:
  216. return 0;
  217. }
  218. }
  219. /*
  220. * fixup_free is called when:
  221. * - an active object is freed
  222. */
  223. static int rcuhead_fixup_free(void *addr, enum debug_obj_state state)
  224. {
  225. struct rcu_head *head = addr;
  226. switch (state) {
  227. case ODEBUG_STATE_ACTIVE:
  228. /*
  229. * Ensure that queued callbacks are all executed.
  230. * If we detect that we are nested in a RCU read-side critical
  231. * section, we should simply fail, otherwise we would deadlock.
  232. * In !PREEMPT configurations, there is no way to tell if we are
  233. * in a RCU read-side critical section or not, so we never
  234. * attempt any fixup and just print a warning.
  235. */
  236. #ifndef CONFIG_PREEMPT
  237. WARN_ON_ONCE(1);
  238. return 0;
  239. #endif
  240. if (rcu_preempt_depth() != 0 || preempt_count() != 0 ||
  241. irqs_disabled()) {
  242. WARN_ON_ONCE(1);
  243. return 0;
  244. }
  245. rcu_barrier();
  246. rcu_barrier_sched();
  247. rcu_barrier_bh();
  248. debug_object_free(head, &rcuhead_debug_descr);
  249. return 1;
  250. default:
  251. return 0;
  252. }
  253. }
  254. /**
  255. * init_rcu_head_on_stack() - initialize on-stack rcu_head for debugobjects
  256. * @head: pointer to rcu_head structure to be initialized
  257. *
  258. * This function informs debugobjects of a new rcu_head structure that
  259. * has been allocated as an auto variable on the stack. This function
  260. * is not required for rcu_head structures that are statically defined or
  261. * that are dynamically allocated on the heap. This function has no
  262. * effect for !CONFIG_DEBUG_OBJECTS_RCU_HEAD kernel builds.
  263. */
  264. void init_rcu_head_on_stack(struct rcu_head *head)
  265. {
  266. debug_object_init_on_stack(head, &rcuhead_debug_descr);
  267. }
  268. EXPORT_SYMBOL_GPL(init_rcu_head_on_stack);
  269. /**
  270. * destroy_rcu_head_on_stack() - destroy on-stack rcu_head for debugobjects
  271. * @head: pointer to rcu_head structure to be initialized
  272. *
  273. * This function informs debugobjects that an on-stack rcu_head structure
  274. * is about to go out of scope. As with init_rcu_head_on_stack(), this
  275. * function is not required for rcu_head structures that are statically
  276. * defined or that are dynamically allocated on the heap. Also as with
  277. * init_rcu_head_on_stack(), this function has no effect for
  278. * !CONFIG_DEBUG_OBJECTS_RCU_HEAD kernel builds.
  279. */
  280. void destroy_rcu_head_on_stack(struct rcu_head *head)
  281. {
  282. debug_object_free(head, &rcuhead_debug_descr);
  283. }
  284. EXPORT_SYMBOL_GPL(destroy_rcu_head_on_stack);
  285. struct debug_obj_descr rcuhead_debug_descr = {
  286. .name = "rcu_head",
  287. .fixup_init = rcuhead_fixup_init,
  288. .fixup_activate = rcuhead_fixup_activate,
  289. .fixup_free = rcuhead_fixup_free,
  290. };
  291. EXPORT_SYMBOL_GPL(rcuhead_debug_descr);
  292. #endif /* #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD */
  293. #if defined(CONFIG_TREE_RCU) || defined(CONFIG_TREE_PREEMPT_RCU) || defined(CONFIG_RCU_TRACE)
  294. void do_trace_rcu_torture_read(char *rcutorturename, struct rcu_head *rhp)
  295. {
  296. trace_rcu_torture_read(rcutorturename, rhp);
  297. }
  298. EXPORT_SYMBOL_GPL(do_trace_rcu_torture_read);
  299. #else
  300. #define do_trace_rcu_torture_read(rcutorturename, rhp) do { } while (0)
  301. #endif