rcupdate.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. * Author: Dipankar Sarma <dipankar@in.ibm.com>
  21. *
  22. * Based on the original work by Paul McKenney <paulmck@us.ibm.com>
  23. * and inputs from Rusty Russell, Andrea Arcangeli and Andi Kleen.
  24. * Papers:
  25. * http://www.rdrop.com/users/paulmck/paper/rclockpdcsproof.pdf
  26. * http://lse.sourceforge.net/locking/rclock_OLS.2001.05.01c.sc.pdf (OLS2001)
  27. *
  28. * For detailed explanation of Read-Copy Update mechanism see -
  29. * http://lse.sourceforge.net/locking/rcupdate.html
  30. *
  31. */
  32. #ifndef __LINUX_RCUPDATE_H
  33. #define __LINUX_RCUPDATE_H
  34. #include <linux/cache.h>
  35. #include <linux/spinlock.h>
  36. #include <linux/threads.h>
  37. #include <linux/cpumask.h>
  38. #include <linux/seqlock.h>
  39. #include <linux/lockdep.h>
  40. #include <linux/completion.h>
  41. /**
  42. * struct rcu_head - callback structure for use with RCU
  43. * @next: next update requests in a list
  44. * @func: actual update function to call after the grace period.
  45. */
  46. struct rcu_head {
  47. struct rcu_head *next;
  48. void (*func)(struct rcu_head *head);
  49. };
  50. /* Exported common interfaces */
  51. extern void synchronize_rcu_bh(void);
  52. extern void synchronize_sched(void);
  53. extern void rcu_barrier(void);
  54. extern void rcu_barrier_bh(void);
  55. extern void rcu_barrier_sched(void);
  56. extern void synchronize_sched_expedited(void);
  57. extern int sched_expedited_torture_stats(char *page);
  58. /* Internal to kernel */
  59. extern void rcu_init(void);
  60. #if defined(CONFIG_TREE_RCU) || defined(CONFIG_TREE_PREEMPT_RCU)
  61. #include <linux/rcutree.h>
  62. #elif defined(CONFIG_TINY_RCU)
  63. #include <linux/rcutiny.h>
  64. #else
  65. #error "Unknown RCU implementation specified to kernel configuration"
  66. #endif
  67. #define RCU_HEAD_INIT { .next = NULL, .func = NULL }
  68. #define RCU_HEAD(head) struct rcu_head head = RCU_HEAD_INIT
  69. #define INIT_RCU_HEAD(ptr) do { \
  70. (ptr)->next = NULL; (ptr)->func = NULL; \
  71. } while (0)
  72. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  73. extern struct lockdep_map rcu_lock_map;
  74. # define rcu_read_acquire() \
  75. lock_acquire(&rcu_lock_map, 0, 0, 2, 1, NULL, _THIS_IP_)
  76. # define rcu_read_release() lock_release(&rcu_lock_map, 1, _THIS_IP_)
  77. #else
  78. # define rcu_read_acquire() do { } while (0)
  79. # define rcu_read_release() do { } while (0)
  80. #endif
  81. /**
  82. * rcu_read_lock - mark the beginning of an RCU read-side critical section.
  83. *
  84. * When synchronize_rcu() is invoked on one CPU while other CPUs
  85. * are within RCU read-side critical sections, then the
  86. * synchronize_rcu() is guaranteed to block until after all the other
  87. * CPUs exit their critical sections. Similarly, if call_rcu() is invoked
  88. * on one CPU while other CPUs are within RCU read-side critical
  89. * sections, invocation of the corresponding RCU callback is deferred
  90. * until after the all the other CPUs exit their critical sections.
  91. *
  92. * Note, however, that RCU callbacks are permitted to run concurrently
  93. * with RCU read-side critical sections. One way that this can happen
  94. * is via the following sequence of events: (1) CPU 0 enters an RCU
  95. * read-side critical section, (2) CPU 1 invokes call_rcu() to register
  96. * an RCU callback, (3) CPU 0 exits the RCU read-side critical section,
  97. * (4) CPU 2 enters a RCU read-side critical section, (5) the RCU
  98. * callback is invoked. This is legal, because the RCU read-side critical
  99. * section that was running concurrently with the call_rcu() (and which
  100. * therefore might be referencing something that the corresponding RCU
  101. * callback would free up) has completed before the corresponding
  102. * RCU callback is invoked.
  103. *
  104. * RCU read-side critical sections may be nested. Any deferred actions
  105. * will be deferred until the outermost RCU read-side critical section
  106. * completes.
  107. *
  108. * It is illegal to block while in an RCU read-side critical section.
  109. */
  110. static inline void rcu_read_lock(void)
  111. {
  112. __rcu_read_lock();
  113. __acquire(RCU);
  114. rcu_read_acquire();
  115. }
  116. /*
  117. * So where is rcu_write_lock()? It does not exist, as there is no
  118. * way for writers to lock out RCU readers. This is a feature, not
  119. * a bug -- this property is what provides RCU's performance benefits.
  120. * Of course, writers must coordinate with each other. The normal
  121. * spinlock primitives work well for this, but any other technique may be
  122. * used as well. RCU does not care how the writers keep out of each
  123. * others' way, as long as they do so.
  124. */
  125. /**
  126. * rcu_read_unlock - marks the end of an RCU read-side critical section.
  127. *
  128. * See rcu_read_lock() for more information.
  129. */
  130. static inline void rcu_read_unlock(void)
  131. {
  132. rcu_read_release();
  133. __release(RCU);
  134. __rcu_read_unlock();
  135. }
  136. /**
  137. * rcu_read_lock_bh - mark the beginning of a softirq-only RCU critical section
  138. *
  139. * This is equivalent of rcu_read_lock(), but to be used when updates
  140. * are being done using call_rcu_bh(). Since call_rcu_bh() callbacks
  141. * consider completion of a softirq handler to be a quiescent state,
  142. * a process in RCU read-side critical section must be protected by
  143. * disabling softirqs. Read-side critical sections in interrupt context
  144. * can use just rcu_read_lock().
  145. *
  146. */
  147. static inline void rcu_read_lock_bh(void)
  148. {
  149. __rcu_read_lock_bh();
  150. __acquire(RCU_BH);
  151. rcu_read_acquire();
  152. }
  153. /*
  154. * rcu_read_unlock_bh - marks the end of a softirq-only RCU critical section
  155. *
  156. * See rcu_read_lock_bh() for more information.
  157. */
  158. static inline void rcu_read_unlock_bh(void)
  159. {
  160. rcu_read_release();
  161. __release(RCU_BH);
  162. __rcu_read_unlock_bh();
  163. }
  164. /**
  165. * rcu_read_lock_sched - mark the beginning of a RCU-classic critical section
  166. *
  167. * Should be used with either
  168. * - synchronize_sched()
  169. * or
  170. * - call_rcu_sched() and rcu_barrier_sched()
  171. * on the write-side to insure proper synchronization.
  172. */
  173. static inline void rcu_read_lock_sched(void)
  174. {
  175. preempt_disable();
  176. __acquire(RCU_SCHED);
  177. rcu_read_acquire();
  178. }
  179. /* Used by lockdep and tracing: cannot be traced, cannot call lockdep. */
  180. static inline notrace void rcu_read_lock_sched_notrace(void)
  181. {
  182. preempt_disable_notrace();
  183. __acquire(RCU_SCHED);
  184. }
  185. /*
  186. * rcu_read_unlock_sched - marks the end of a RCU-classic critical section
  187. *
  188. * See rcu_read_lock_sched for more information.
  189. */
  190. static inline void rcu_read_unlock_sched(void)
  191. {
  192. rcu_read_release();
  193. __release(RCU_SCHED);
  194. preempt_enable();
  195. }
  196. /* Used by lockdep and tracing: cannot be traced, cannot call lockdep. */
  197. static inline notrace void rcu_read_unlock_sched_notrace(void)
  198. {
  199. __release(RCU_SCHED);
  200. preempt_enable_notrace();
  201. }
  202. /**
  203. * rcu_dereference - fetch an RCU-protected pointer in an
  204. * RCU read-side critical section. This pointer may later
  205. * be safely dereferenced.
  206. *
  207. * Inserts memory barriers on architectures that require them
  208. * (currently only the Alpha), and, more importantly, documents
  209. * exactly which pointers are protected by RCU.
  210. */
  211. #define rcu_dereference(p) ({ \
  212. typeof(p) _________p1 = ACCESS_ONCE(p); \
  213. smp_read_barrier_depends(); \
  214. (_________p1); \
  215. })
  216. /**
  217. * rcu_assign_pointer - assign (publicize) a pointer to a newly
  218. * initialized structure that will be dereferenced by RCU read-side
  219. * critical sections. Returns the value assigned.
  220. *
  221. * Inserts memory barriers on architectures that require them
  222. * (pretty much all of them other than x86), and also prevents
  223. * the compiler from reordering the code that initializes the
  224. * structure after the pointer assignment. More importantly, this
  225. * call documents which pointers will be dereferenced by RCU read-side
  226. * code.
  227. */
  228. #define rcu_assign_pointer(p, v) \
  229. ({ \
  230. if (!__builtin_constant_p(v) || \
  231. ((v) != NULL)) \
  232. smp_wmb(); \
  233. (p) = (v); \
  234. })
  235. /* Infrastructure to implement the synchronize_() primitives. */
  236. struct rcu_synchronize {
  237. struct rcu_head head;
  238. struct completion completion;
  239. };
  240. extern void wakeme_after_rcu(struct rcu_head *head);
  241. /**
  242. * call_rcu - Queue an RCU callback for invocation after a grace period.
  243. * @head: structure to be used for queueing the RCU updates.
  244. * @func: actual update function to be invoked after the grace period
  245. *
  246. * The update function will be invoked some time after a full grace
  247. * period elapses, in other words after all currently executing RCU
  248. * read-side critical sections have completed. RCU read-side critical
  249. * sections are delimited by rcu_read_lock() and rcu_read_unlock(),
  250. * and may be nested.
  251. */
  252. extern void call_rcu(struct rcu_head *head,
  253. void (*func)(struct rcu_head *head));
  254. /**
  255. * call_rcu_bh - Queue an RCU for invocation after a quicker grace period.
  256. * @head: structure to be used for queueing the RCU updates.
  257. * @func: actual update function to be invoked after the grace period
  258. *
  259. * The update function will be invoked some time after a full grace
  260. * period elapses, in other words after all currently executing RCU
  261. * read-side critical sections have completed. call_rcu_bh() assumes
  262. * that the read-side critical sections end on completion of a softirq
  263. * handler. This means that read-side critical sections in process
  264. * context must not be interrupted by softirqs. This interface is to be
  265. * used when most of the read-side critical sections are in softirq context.
  266. * RCU read-side critical sections are delimited by :
  267. * - rcu_read_lock() and rcu_read_unlock(), if in interrupt context.
  268. * OR
  269. * - rcu_read_lock_bh() and rcu_read_unlock_bh(), if in process context.
  270. * These may be nested.
  271. */
  272. extern void call_rcu_bh(struct rcu_head *head,
  273. void (*func)(struct rcu_head *head));
  274. #endif /* __LINUX_RCUPDATE_H */