update.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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. #include <linux/module.h>
  50. #define CREATE_TRACE_POINTS
  51. #include <trace/events/rcu.h>
  52. #include "rcu.h"
  53. MODULE_ALIAS("rcupdate");
  54. #ifdef MODULE_PARAM_PREFIX
  55. #undef MODULE_PARAM_PREFIX
  56. #endif
  57. #define MODULE_PARAM_PREFIX "rcupdate."
  58. module_param(rcu_expedited, int, 0);
  59. #ifdef CONFIG_PREEMPT_RCU
  60. /*
  61. * Preemptible RCU implementation for rcu_read_lock().
  62. * Just increment ->rcu_read_lock_nesting, shared state will be updated
  63. * if we block.
  64. */
  65. void __rcu_read_lock(void)
  66. {
  67. current->rcu_read_lock_nesting++;
  68. barrier(); /* critical section after entry code. */
  69. }
  70. EXPORT_SYMBOL_GPL(__rcu_read_lock);
  71. /*
  72. * Preemptible RCU implementation for rcu_read_unlock().
  73. * Decrement ->rcu_read_lock_nesting. If the result is zero (outermost
  74. * rcu_read_unlock()) and ->rcu_read_unlock_special is non-zero, then
  75. * invoke rcu_read_unlock_special() to clean up after a context switch
  76. * in an RCU read-side critical section and other special cases.
  77. */
  78. void __rcu_read_unlock(void)
  79. {
  80. struct task_struct *t = current;
  81. if (t->rcu_read_lock_nesting != 1) {
  82. --t->rcu_read_lock_nesting;
  83. } else {
  84. barrier(); /* critical section before exit code. */
  85. t->rcu_read_lock_nesting = INT_MIN;
  86. #ifdef CONFIG_PROVE_RCU_DELAY
  87. udelay(10); /* Make preemption more probable. */
  88. #endif /* #ifdef CONFIG_PROVE_RCU_DELAY */
  89. barrier(); /* assign before ->rcu_read_unlock_special load */
  90. if (unlikely(ACCESS_ONCE(t->rcu_read_unlock_special)))
  91. rcu_read_unlock_special(t);
  92. barrier(); /* ->rcu_read_unlock_special load before assign */
  93. t->rcu_read_lock_nesting = 0;
  94. }
  95. #ifdef CONFIG_PROVE_LOCKING
  96. {
  97. int rrln = ACCESS_ONCE(t->rcu_read_lock_nesting);
  98. WARN_ON_ONCE(rrln < 0 && rrln > INT_MIN / 2);
  99. }
  100. #endif /* #ifdef CONFIG_PROVE_LOCKING */
  101. }
  102. EXPORT_SYMBOL_GPL(__rcu_read_unlock);
  103. #endif /* #ifdef CONFIG_PREEMPT_RCU */
  104. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  105. static struct lock_class_key rcu_lock_key;
  106. struct lockdep_map rcu_lock_map =
  107. STATIC_LOCKDEP_MAP_INIT("rcu_read_lock", &rcu_lock_key);
  108. EXPORT_SYMBOL_GPL(rcu_lock_map);
  109. static struct lock_class_key rcu_bh_lock_key;
  110. struct lockdep_map rcu_bh_lock_map =
  111. STATIC_LOCKDEP_MAP_INIT("rcu_read_lock_bh", &rcu_bh_lock_key);
  112. EXPORT_SYMBOL_GPL(rcu_bh_lock_map);
  113. static struct lock_class_key rcu_sched_lock_key;
  114. struct lockdep_map rcu_sched_lock_map =
  115. STATIC_LOCKDEP_MAP_INIT("rcu_read_lock_sched", &rcu_sched_lock_key);
  116. EXPORT_SYMBOL_GPL(rcu_sched_lock_map);
  117. int notrace debug_lockdep_rcu_enabled(void)
  118. {
  119. return rcu_scheduler_active && debug_locks &&
  120. current->lockdep_recursion == 0;
  121. }
  122. EXPORT_SYMBOL_GPL(debug_lockdep_rcu_enabled);
  123. /**
  124. * rcu_read_lock_bh_held() - might we be in RCU-bh read-side critical section?
  125. *
  126. * Check for bottom half being disabled, which covers both the
  127. * CONFIG_PROVE_RCU and not cases. Note that if someone uses
  128. * rcu_read_lock_bh(), but then later enables BH, lockdep (if enabled)
  129. * will show the situation. This is useful for debug checks in functions
  130. * that require that they be called within an RCU read-side critical
  131. * section.
  132. *
  133. * Check debug_lockdep_rcu_enabled() to prevent false positives during boot.
  134. *
  135. * Note that rcu_read_lock() is disallowed if the CPU is either idle or
  136. * offline from an RCU perspective, so check for those as well.
  137. */
  138. int rcu_read_lock_bh_held(void)
  139. {
  140. if (!debug_lockdep_rcu_enabled())
  141. return 1;
  142. if (!rcu_is_watching())
  143. return 0;
  144. if (!rcu_lockdep_current_cpu_online())
  145. return 0;
  146. return in_softirq() || irqs_disabled();
  147. }
  148. EXPORT_SYMBOL_GPL(rcu_read_lock_bh_held);
  149. #endif /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
  150. struct rcu_synchronize {
  151. struct rcu_head head;
  152. struct completion completion;
  153. };
  154. /*
  155. * Awaken the corresponding synchronize_rcu() instance now that a
  156. * grace period has elapsed.
  157. */
  158. static void wakeme_after_rcu(struct rcu_head *head)
  159. {
  160. struct rcu_synchronize *rcu;
  161. rcu = container_of(head, struct rcu_synchronize, head);
  162. complete(&rcu->completion);
  163. }
  164. void wait_rcu_gp(call_rcu_func_t crf)
  165. {
  166. struct rcu_synchronize rcu;
  167. init_rcu_head_on_stack(&rcu.head);
  168. init_completion(&rcu.completion);
  169. /* Will wake me after RCU finished. */
  170. crf(&rcu.head, wakeme_after_rcu);
  171. /* Wait for it. */
  172. wait_for_completion(&rcu.completion);
  173. destroy_rcu_head_on_stack(&rcu.head);
  174. }
  175. EXPORT_SYMBOL_GPL(wait_rcu_gp);
  176. #ifdef CONFIG_PROVE_RCU
  177. /*
  178. * wrapper function to avoid #include problems.
  179. */
  180. int rcu_my_thread_group_empty(void)
  181. {
  182. return thread_group_empty(current);
  183. }
  184. EXPORT_SYMBOL_GPL(rcu_my_thread_group_empty);
  185. #endif /* #ifdef CONFIG_PROVE_RCU */
  186. #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD
  187. static inline void debug_init_rcu_head(struct rcu_head *head)
  188. {
  189. debug_object_init(head, &rcuhead_debug_descr);
  190. }
  191. static inline void debug_rcu_head_free(struct rcu_head *head)
  192. {
  193. debug_object_free(head, &rcuhead_debug_descr);
  194. }
  195. /*
  196. * fixup_activate is called when:
  197. * - an active object is activated
  198. * - an unknown object is activated (might be a statically initialized object)
  199. * Activation is performed internally by call_rcu().
  200. */
  201. static int rcuhead_fixup_activate(void *addr, enum debug_obj_state state)
  202. {
  203. struct rcu_head *head = addr;
  204. switch (state) {
  205. case ODEBUG_STATE_NOTAVAILABLE:
  206. /*
  207. * This is not really a fixup. We just make sure that it is
  208. * tracked in the object tracker.
  209. */
  210. debug_object_init(head, &rcuhead_debug_descr);
  211. debug_object_activate(head, &rcuhead_debug_descr);
  212. return 0;
  213. default:
  214. return 1;
  215. }
  216. }
  217. /**
  218. * init_rcu_head_on_stack() - initialize on-stack rcu_head for debugobjects
  219. * @head: pointer to rcu_head structure to be initialized
  220. *
  221. * This function informs debugobjects of a new rcu_head structure that
  222. * has been allocated as an auto variable on the stack. This function
  223. * is not required for rcu_head structures that are statically defined or
  224. * that are dynamically allocated on the heap. This function has no
  225. * effect for !CONFIG_DEBUG_OBJECTS_RCU_HEAD kernel builds.
  226. */
  227. void init_rcu_head_on_stack(struct rcu_head *head)
  228. {
  229. debug_object_init_on_stack(head, &rcuhead_debug_descr);
  230. }
  231. EXPORT_SYMBOL_GPL(init_rcu_head_on_stack);
  232. /**
  233. * destroy_rcu_head_on_stack() - destroy on-stack rcu_head for debugobjects
  234. * @head: pointer to rcu_head structure to be initialized
  235. *
  236. * This function informs debugobjects that an on-stack rcu_head structure
  237. * is about to go out of scope. As with init_rcu_head_on_stack(), this
  238. * function is not required for rcu_head structures that are statically
  239. * defined or that are dynamically allocated on the heap. Also as with
  240. * init_rcu_head_on_stack(), this function has no effect for
  241. * !CONFIG_DEBUG_OBJECTS_RCU_HEAD kernel builds.
  242. */
  243. void destroy_rcu_head_on_stack(struct rcu_head *head)
  244. {
  245. debug_object_free(head, &rcuhead_debug_descr);
  246. }
  247. EXPORT_SYMBOL_GPL(destroy_rcu_head_on_stack);
  248. struct debug_obj_descr rcuhead_debug_descr = {
  249. .name = "rcu_head",
  250. .fixup_activate = rcuhead_fixup_activate,
  251. };
  252. EXPORT_SYMBOL_GPL(rcuhead_debug_descr);
  253. #endif /* #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD */
  254. #if defined(CONFIG_TREE_RCU) || defined(CONFIG_TREE_PREEMPT_RCU) || defined(CONFIG_RCU_TRACE)
  255. void do_trace_rcu_torture_read(const char *rcutorturename, struct rcu_head *rhp,
  256. unsigned long secs,
  257. unsigned long c_old, unsigned long c)
  258. {
  259. trace_rcu_torture_read(rcutorturename, rhp, secs, c_old, c);
  260. }
  261. EXPORT_SYMBOL_GPL(do_trace_rcu_torture_read);
  262. #else
  263. #define do_trace_rcu_torture_read(rcutorturename, rhp, secs, c_old, c) \
  264. do { } while (0)
  265. #endif
  266. #ifdef CONFIG_RCU_STALL_COMMON
  267. #ifdef CONFIG_PROVE_RCU
  268. #define RCU_STALL_DELAY_DELTA (5 * HZ)
  269. #else
  270. #define RCU_STALL_DELAY_DELTA 0
  271. #endif
  272. int rcu_cpu_stall_suppress __read_mostly; /* 1 = suppress stall warnings. */
  273. static int rcu_cpu_stall_timeout __read_mostly = CONFIG_RCU_CPU_STALL_TIMEOUT;
  274. module_param(rcu_cpu_stall_suppress, int, 0644);
  275. module_param(rcu_cpu_stall_timeout, int, 0644);
  276. int rcu_jiffies_till_stall_check(void)
  277. {
  278. int till_stall_check = ACCESS_ONCE(rcu_cpu_stall_timeout);
  279. /*
  280. * Limit check must be consistent with the Kconfig limits
  281. * for CONFIG_RCU_CPU_STALL_TIMEOUT.
  282. */
  283. if (till_stall_check < 3) {
  284. ACCESS_ONCE(rcu_cpu_stall_timeout) = 3;
  285. till_stall_check = 3;
  286. } else if (till_stall_check > 300) {
  287. ACCESS_ONCE(rcu_cpu_stall_timeout) = 300;
  288. till_stall_check = 300;
  289. }
  290. return till_stall_check * HZ + RCU_STALL_DELAY_DELTA;
  291. }
  292. static int rcu_panic(struct notifier_block *this, unsigned long ev, void *ptr)
  293. {
  294. rcu_cpu_stall_suppress = 1;
  295. return NOTIFY_DONE;
  296. }
  297. static struct notifier_block rcu_panic_block = {
  298. .notifier_call = rcu_panic,
  299. };
  300. static int __init check_cpu_stall_init(void)
  301. {
  302. atomic_notifier_chain_register(&panic_notifier_list, &rcu_panic_block);
  303. return 0;
  304. }
  305. early_initcall(check_cpu_stall_init);
  306. #endif /* #ifdef CONFIG_RCU_STALL_COMMON */