rcupdate.c 10 KB

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