rcupdate.c 12 KB

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