rcupdate.c 12 KB

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