rcupdate.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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 <asm/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/module.h>
  47. #include <linux/kernel_stat.h>
  48. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  49. static struct lock_class_key rcu_lock_key;
  50. struct lockdep_map rcu_lock_map =
  51. STATIC_LOCKDEP_MAP_INIT("rcu_read_lock", &rcu_lock_key);
  52. EXPORT_SYMBOL_GPL(rcu_lock_map);
  53. #endif
  54. static DEFINE_PER_CPU(struct rcu_head, rcu_barrier_head) = {NULL};
  55. static atomic_t rcu_barrier_cpu_count;
  56. static DEFINE_MUTEX(rcu_barrier_mutex);
  57. static struct completion rcu_barrier_completion;
  58. int rcu_scheduler_active __read_mostly;
  59. static atomic_t rcu_migrate_type_count = ATOMIC_INIT(0);
  60. static struct rcu_head rcu_migrate_head[3];
  61. static DECLARE_WAIT_QUEUE_HEAD(rcu_migrate_wq);
  62. /*
  63. * Awaken the corresponding synchronize_rcu() instance now that a
  64. * grace period has elapsed.
  65. */
  66. void wakeme_after_rcu(struct rcu_head *head)
  67. {
  68. struct rcu_synchronize *rcu;
  69. rcu = container_of(head, struct rcu_synchronize, head);
  70. complete(&rcu->completion);
  71. }
  72. #ifdef CONFIG_TREE_PREEMPT_RCU
  73. /**
  74. * synchronize_rcu - wait until a grace period has elapsed.
  75. *
  76. * Control will return to the caller some time after a full grace
  77. * period has elapsed, in other words after all currently executing RCU
  78. * read-side critical sections have completed. RCU read-side critical
  79. * sections are delimited by rcu_read_lock() and rcu_read_unlock(),
  80. * and may be nested.
  81. */
  82. void synchronize_rcu(void)
  83. {
  84. struct rcu_synchronize rcu;
  85. if (!rcu_scheduler_active)
  86. return;
  87. init_completion(&rcu.completion);
  88. /* Will wake me after RCU finished. */
  89. call_rcu(&rcu.head, wakeme_after_rcu);
  90. /* Wait for it. */
  91. wait_for_completion(&rcu.completion);
  92. }
  93. EXPORT_SYMBOL_GPL(synchronize_rcu);
  94. #endif /* #ifdef CONFIG_TREE_PREEMPT_RCU */
  95. /**
  96. * synchronize_sched - wait until an rcu-sched grace period has elapsed.
  97. *
  98. * Control will return to the caller some time after a full rcu-sched
  99. * grace period has elapsed, in other words after all currently executing
  100. * rcu-sched read-side critical sections have completed. These read-side
  101. * critical sections are delimited by rcu_read_lock_sched() and
  102. * rcu_read_unlock_sched(), and may be nested. Note that preempt_disable(),
  103. * local_irq_disable(), and so on may be used in place of
  104. * rcu_read_lock_sched().
  105. *
  106. * This means that all preempt_disable code sequences, including NMI and
  107. * hardware-interrupt handlers, in progress on entry will have completed
  108. * before this primitive returns. However, this does not guarantee that
  109. * softirq handlers will have completed, since in some kernels, these
  110. * handlers can run in process context, and can block.
  111. *
  112. * This primitive provides the guarantees made by the (now removed)
  113. * synchronize_kernel() API. In contrast, synchronize_rcu() only
  114. * guarantees that rcu_read_lock() sections will have completed.
  115. * In "classic RCU", these two guarantees happen to be one and
  116. * the same, but can differ in realtime RCU implementations.
  117. */
  118. void synchronize_sched(void)
  119. {
  120. struct rcu_synchronize rcu;
  121. if (rcu_blocking_is_gp())
  122. return;
  123. init_completion(&rcu.completion);
  124. /* Will wake me after RCU finished. */
  125. call_rcu_sched(&rcu.head, wakeme_after_rcu);
  126. /* Wait for it. */
  127. wait_for_completion(&rcu.completion);
  128. }
  129. EXPORT_SYMBOL_GPL(synchronize_sched);
  130. /**
  131. * synchronize_rcu_bh - wait until an rcu_bh grace period has elapsed.
  132. *
  133. * Control will return to the caller some time after a full rcu_bh grace
  134. * period has elapsed, in other words after all currently executing rcu_bh
  135. * read-side critical sections have completed. RCU read-side critical
  136. * sections are delimited by rcu_read_lock_bh() and rcu_read_unlock_bh(),
  137. * and may be nested.
  138. */
  139. void synchronize_rcu_bh(void)
  140. {
  141. struct rcu_synchronize rcu;
  142. if (rcu_blocking_is_gp())
  143. return;
  144. init_completion(&rcu.completion);
  145. /* Will wake me after RCU finished. */
  146. call_rcu_bh(&rcu.head, wakeme_after_rcu);
  147. /* Wait for it. */
  148. wait_for_completion(&rcu.completion);
  149. }
  150. EXPORT_SYMBOL_GPL(synchronize_rcu_bh);
  151. static void rcu_barrier_callback(struct rcu_head *notused)
  152. {
  153. if (atomic_dec_and_test(&rcu_barrier_cpu_count))
  154. complete(&rcu_barrier_completion);
  155. }
  156. /*
  157. * Called with preemption disabled, and from cross-cpu IRQ context.
  158. */
  159. static void rcu_barrier_func(void *type)
  160. {
  161. int cpu = smp_processor_id();
  162. struct rcu_head *head = &per_cpu(rcu_barrier_head, cpu);
  163. void (*call_rcu_func)(struct rcu_head *head,
  164. void (*func)(struct rcu_head *head));
  165. atomic_inc(&rcu_barrier_cpu_count);
  166. call_rcu_func = type;
  167. call_rcu_func(head, rcu_barrier_callback);
  168. }
  169. static inline void wait_migrated_callbacks(void)
  170. {
  171. wait_event(rcu_migrate_wq, !atomic_read(&rcu_migrate_type_count));
  172. smp_mb(); /* In case we didn't sleep. */
  173. }
  174. /*
  175. * Orchestrate the specified type of RCU barrier, waiting for all
  176. * RCU callbacks of the specified type to complete.
  177. */
  178. static void _rcu_barrier(void (*call_rcu_func)(struct rcu_head *head,
  179. void (*func)(struct rcu_head *head)))
  180. {
  181. BUG_ON(in_interrupt());
  182. /* Take cpucontrol mutex to protect against CPU hotplug */
  183. mutex_lock(&rcu_barrier_mutex);
  184. init_completion(&rcu_barrier_completion);
  185. /*
  186. * Initialize rcu_barrier_cpu_count to 1, then invoke
  187. * rcu_barrier_func() on each CPU, so that each CPU also has
  188. * incremented rcu_barrier_cpu_count. Only then is it safe to
  189. * decrement rcu_barrier_cpu_count -- otherwise the first CPU
  190. * might complete its grace period before all of the other CPUs
  191. * did their increment, causing this function to return too
  192. * early.
  193. */
  194. atomic_set(&rcu_barrier_cpu_count, 1);
  195. on_each_cpu(rcu_barrier_func, (void *)call_rcu_func, 1);
  196. if (atomic_dec_and_test(&rcu_barrier_cpu_count))
  197. complete(&rcu_barrier_completion);
  198. wait_for_completion(&rcu_barrier_completion);
  199. mutex_unlock(&rcu_barrier_mutex);
  200. wait_migrated_callbacks();
  201. }
  202. /**
  203. * rcu_barrier - Wait until all in-flight call_rcu() callbacks complete.
  204. */
  205. void rcu_barrier(void)
  206. {
  207. _rcu_barrier(call_rcu);
  208. }
  209. EXPORT_SYMBOL_GPL(rcu_barrier);
  210. /**
  211. * rcu_barrier_bh - Wait until all in-flight call_rcu_bh() callbacks complete.
  212. */
  213. void rcu_barrier_bh(void)
  214. {
  215. _rcu_barrier(call_rcu_bh);
  216. }
  217. EXPORT_SYMBOL_GPL(rcu_barrier_bh);
  218. /**
  219. * rcu_barrier_sched - Wait for in-flight call_rcu_sched() callbacks.
  220. */
  221. void rcu_barrier_sched(void)
  222. {
  223. _rcu_barrier(call_rcu_sched);
  224. }
  225. EXPORT_SYMBOL_GPL(rcu_barrier_sched);
  226. static void rcu_migrate_callback(struct rcu_head *notused)
  227. {
  228. if (atomic_dec_and_test(&rcu_migrate_type_count))
  229. wake_up(&rcu_migrate_wq);
  230. }
  231. static int __cpuinit rcu_barrier_cpu_hotplug(struct notifier_block *self,
  232. unsigned long action, void *hcpu)
  233. {
  234. rcu_cpu_notify(self, action, hcpu);
  235. if (action == CPU_DYING) {
  236. /*
  237. * preempt_disable() in on_each_cpu() prevents stop_machine(),
  238. * so when "on_each_cpu(rcu_barrier_func, (void *)type, 1);"
  239. * returns, all online cpus have queued rcu_barrier_func(),
  240. * and the dead cpu(if it exist) queues rcu_migrate_callback()s.
  241. *
  242. * These callbacks ensure _rcu_barrier() waits for all
  243. * RCU callbacks of the specified type to complete.
  244. */
  245. atomic_set(&rcu_migrate_type_count, 3);
  246. call_rcu_bh(rcu_migrate_head, rcu_migrate_callback);
  247. call_rcu_sched(rcu_migrate_head + 1, rcu_migrate_callback);
  248. call_rcu(rcu_migrate_head + 2, rcu_migrate_callback);
  249. } else if (action == CPU_DOWN_PREPARE) {
  250. /* Don't need to wait until next removal operation. */
  251. /* rcu_migrate_head is protected by cpu_add_remove_lock */
  252. wait_migrated_callbacks();
  253. }
  254. return NOTIFY_OK;
  255. }
  256. void __init rcu_init(void)
  257. {
  258. int i;
  259. __rcu_init();
  260. cpu_notifier(rcu_barrier_cpu_hotplug, 0);
  261. /*
  262. * We don't need protection against CPU-hotplug here because
  263. * this is called early in boot, before either interrupts
  264. * or the scheduler are operational.
  265. */
  266. for_each_online_cpu(i)
  267. rcu_barrier_cpu_hotplug(NULL, CPU_UP_PREPARE, (void *)(long)i);
  268. }
  269. void rcu_scheduler_starting(void)
  270. {
  271. WARN_ON(num_online_cpus() != 1);
  272. WARN_ON(nr_context_switches() > 0);
  273. rcu_scheduler_active = 1;
  274. }