rcupdate.c 9.7 KB

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