rcupdate.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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. extern int rcu_scheduler_active;
  61. extern void rcu_scheduler_starting(void);
  62. #if defined(CONFIG_TREE_RCU) || defined(CONFIG_TREE_PREEMPT_RCU)
  63. #include <linux/rcutree.h>
  64. #elif defined(CONFIG_TINY_RCU)
  65. #include <linux/rcutiny.h>
  66. #else
  67. #error "Unknown RCU implementation specified to kernel configuration"
  68. #endif
  69. #define RCU_HEAD_INIT { .next = NULL, .func = NULL }
  70. #define RCU_HEAD(head) struct rcu_head head = RCU_HEAD_INIT
  71. #define INIT_RCU_HEAD(ptr) do { \
  72. (ptr)->next = NULL; (ptr)->func = NULL; \
  73. } while (0)
  74. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  75. extern struct lockdep_map rcu_lock_map;
  76. # define rcu_read_acquire() \
  77. lock_acquire(&rcu_lock_map, 0, 0, 2, 1, NULL, _THIS_IP_)
  78. # define rcu_read_release() lock_release(&rcu_lock_map, 1, _THIS_IP_)
  79. extern struct lockdep_map rcu_bh_lock_map;
  80. # define rcu_read_acquire_bh() \
  81. lock_acquire(&rcu_bh_lock_map, 0, 0, 2, 1, NULL, _THIS_IP_)
  82. # define rcu_read_release_bh() lock_release(&rcu_bh_lock_map, 1, _THIS_IP_)
  83. extern struct lockdep_map rcu_sched_lock_map;
  84. # define rcu_read_acquire_sched() \
  85. lock_acquire(&rcu_sched_lock_map, 0, 0, 2, 1, NULL, _THIS_IP_)
  86. # define rcu_read_release_sched() \
  87. lock_release(&rcu_sched_lock_map, 1, _THIS_IP_)
  88. /**
  89. * rcu_read_lock_held - might we be in RCU read-side critical section?
  90. *
  91. * If CONFIG_PROVE_LOCKING is selected and enabled, returns nonzero iff in
  92. * an RCU read-side critical section. In absence of CONFIG_PROVE_LOCKING,
  93. * this assumes we are in an RCU read-side critical section unless it can
  94. * prove otherwise.
  95. */
  96. static inline int rcu_read_lock_held(void)
  97. {
  98. if (debug_locks)
  99. return lock_is_held(&rcu_lock_map);
  100. return 1;
  101. }
  102. /**
  103. * rcu_read_lock_bh_held - might we be in RCU-bh read-side critical section?
  104. *
  105. * If CONFIG_PROVE_LOCKING is selected and enabled, returns nonzero iff in
  106. * an RCU-bh read-side critical section. In absence of CONFIG_PROVE_LOCKING,
  107. * this assumes we are in an RCU-bh read-side critical section unless it can
  108. * prove otherwise.
  109. */
  110. static inline int rcu_read_lock_bh_held(void)
  111. {
  112. if (debug_locks)
  113. return lock_is_held(&rcu_bh_lock_map);
  114. return 1;
  115. }
  116. /**
  117. * rcu_read_lock_sched_held - might we be in RCU-sched read-side critical section?
  118. *
  119. * If CONFIG_PROVE_LOCKING is selected and enabled, returns nonzero iff in an
  120. * RCU-sched read-side critical section. In absence of CONFIG_PROVE_LOCKING,
  121. * this assumes we are in an RCU-sched read-side critical section unless it
  122. * can prove otherwise. Note that disabling of preemption (including
  123. * disabling irqs) counts as an RCU-sched read-side critical section.
  124. */
  125. static inline int rcu_read_lock_sched_held(void)
  126. {
  127. int lockdep_opinion = 0;
  128. if (debug_locks)
  129. lockdep_opinion = lock_is_held(&rcu_sched_lock_map);
  130. return lockdep_opinion || preempt_count() != 0 || !rcu_scheduler_active;
  131. }
  132. #else /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
  133. # define rcu_read_acquire() do { } while (0)
  134. # define rcu_read_release() do { } while (0)
  135. # define rcu_read_acquire_bh() do { } while (0)
  136. # define rcu_read_release_bh() do { } while (0)
  137. # define rcu_read_acquire_sched() do { } while (0)
  138. # define rcu_read_release_sched() do { } while (0)
  139. static inline int rcu_read_lock_held(void)
  140. {
  141. return 1;
  142. }
  143. static inline int rcu_read_lock_bh_held(void)
  144. {
  145. return 1;
  146. }
  147. static inline int rcu_read_lock_sched_held(void)
  148. {
  149. return preempt_count() != 0;
  150. }
  151. #endif /* #else #ifdef CONFIG_DEBUG_LOCK_ALLOC */
  152. #ifdef CONFIG_PROVE_RCU
  153. /**
  154. * rcu_dereference_check - rcu_dereference with debug checking
  155. *
  156. * Do an rcu_dereference(), but check that the context is correct.
  157. * For example, rcu_dereference_check(gp, rcu_read_lock_held()) to
  158. * ensure that the rcu_dereference_check() executes within an RCU
  159. * read-side critical section. It is also possible to check for
  160. * locks being held, for example, by using lockdep_is_held().
  161. */
  162. #define rcu_dereference_check(p, c) \
  163. ({ \
  164. if (debug_locks && !(c)) \
  165. lockdep_rcu_dereference(__FILE__, __LINE__); \
  166. rcu_dereference_raw(p); \
  167. })
  168. #else /* #ifdef CONFIG_PROVE_RCU */
  169. #define rcu_dereference_check(p, c) rcu_dereference_raw(p)
  170. #endif /* #else #ifdef CONFIG_PROVE_RCU */
  171. /**
  172. * rcu_read_lock - mark the beginning of an RCU read-side critical section.
  173. *
  174. * When synchronize_rcu() is invoked on one CPU while other CPUs
  175. * are within RCU read-side critical sections, then the
  176. * synchronize_rcu() is guaranteed to block until after all the other
  177. * CPUs exit their critical sections. Similarly, if call_rcu() is invoked
  178. * on one CPU while other CPUs are within RCU read-side critical
  179. * sections, invocation of the corresponding RCU callback is deferred
  180. * until after the all the other CPUs exit their critical sections.
  181. *
  182. * Note, however, that RCU callbacks are permitted to run concurrently
  183. * with RCU read-side critical sections. One way that this can happen
  184. * is via the following sequence of events: (1) CPU 0 enters an RCU
  185. * read-side critical section, (2) CPU 1 invokes call_rcu() to register
  186. * an RCU callback, (3) CPU 0 exits the RCU read-side critical section,
  187. * (4) CPU 2 enters a RCU read-side critical section, (5) the RCU
  188. * callback is invoked. This is legal, because the RCU read-side critical
  189. * section that was running concurrently with the call_rcu() (and which
  190. * therefore might be referencing something that the corresponding RCU
  191. * callback would free up) has completed before the corresponding
  192. * RCU callback is invoked.
  193. *
  194. * RCU read-side critical sections may be nested. Any deferred actions
  195. * will be deferred until the outermost RCU read-side critical section
  196. * completes.
  197. *
  198. * It is illegal to block while in an RCU read-side critical section.
  199. */
  200. static inline void rcu_read_lock(void)
  201. {
  202. __rcu_read_lock();
  203. __acquire(RCU);
  204. rcu_read_acquire();
  205. }
  206. /*
  207. * So where is rcu_write_lock()? It does not exist, as there is no
  208. * way for writers to lock out RCU readers. This is a feature, not
  209. * a bug -- this property is what provides RCU's performance benefits.
  210. * Of course, writers must coordinate with each other. The normal
  211. * spinlock primitives work well for this, but any other technique may be
  212. * used as well. RCU does not care how the writers keep out of each
  213. * others' way, as long as they do so.
  214. */
  215. /**
  216. * rcu_read_unlock - marks the end of an RCU read-side critical section.
  217. *
  218. * See rcu_read_lock() for more information.
  219. */
  220. static inline void rcu_read_unlock(void)
  221. {
  222. rcu_read_release();
  223. __release(RCU);
  224. __rcu_read_unlock();
  225. }
  226. /**
  227. * rcu_read_lock_bh - mark the beginning of a softirq-only RCU critical section
  228. *
  229. * This is equivalent of rcu_read_lock(), but to be used when updates
  230. * are being done using call_rcu_bh(). Since call_rcu_bh() callbacks
  231. * consider completion of a softirq handler to be a quiescent state,
  232. * a process in RCU read-side critical section must be protected by
  233. * disabling softirqs. Read-side critical sections in interrupt context
  234. * can use just rcu_read_lock().
  235. *
  236. */
  237. static inline void rcu_read_lock_bh(void)
  238. {
  239. __rcu_read_lock_bh();
  240. __acquire(RCU_BH);
  241. rcu_read_acquire_bh();
  242. }
  243. /*
  244. * rcu_read_unlock_bh - marks the end of a softirq-only RCU critical section
  245. *
  246. * See rcu_read_lock_bh() for more information.
  247. */
  248. static inline void rcu_read_unlock_bh(void)
  249. {
  250. rcu_read_release_bh();
  251. __release(RCU_BH);
  252. __rcu_read_unlock_bh();
  253. }
  254. /**
  255. * rcu_read_lock_sched - mark the beginning of a RCU-classic critical section
  256. *
  257. * Should be used with either
  258. * - synchronize_sched()
  259. * or
  260. * - call_rcu_sched() and rcu_barrier_sched()
  261. * on the write-side to insure proper synchronization.
  262. */
  263. static inline void rcu_read_lock_sched(void)
  264. {
  265. preempt_disable();
  266. __acquire(RCU_SCHED);
  267. rcu_read_acquire_sched();
  268. }
  269. /* Used by lockdep and tracing: cannot be traced, cannot call lockdep. */
  270. static inline notrace void rcu_read_lock_sched_notrace(void)
  271. {
  272. preempt_disable_notrace();
  273. __acquire(RCU_SCHED);
  274. }
  275. /*
  276. * rcu_read_unlock_sched - marks the end of a RCU-classic critical section
  277. *
  278. * See rcu_read_lock_sched for more information.
  279. */
  280. static inline void rcu_read_unlock_sched(void)
  281. {
  282. rcu_read_release_sched();
  283. __release(RCU_SCHED);
  284. preempt_enable();
  285. }
  286. /* Used by lockdep and tracing: cannot be traced, cannot call lockdep. */
  287. static inline notrace void rcu_read_unlock_sched_notrace(void)
  288. {
  289. __release(RCU_SCHED);
  290. preempt_enable_notrace();
  291. }
  292. /**
  293. * rcu_dereference_raw - fetch an RCU-protected pointer
  294. *
  295. * The caller must be within some flavor of RCU read-side critical
  296. * section, or must be otherwise preventing the pointer from changing,
  297. * for example, by holding an appropriate lock. This pointer may later
  298. * be safely dereferenced. It is the caller's responsibility to have
  299. * done the right thing, as this primitive does no checking of any kind.
  300. *
  301. * Inserts memory barriers on architectures that require them
  302. * (currently only the Alpha), and, more importantly, documents
  303. * exactly which pointers are protected by RCU.
  304. */
  305. #define rcu_dereference_raw(p) ({ \
  306. typeof(p) _________p1 = ACCESS_ONCE(p); \
  307. smp_read_barrier_depends(); \
  308. (_________p1); \
  309. })
  310. /**
  311. * rcu_dereference - fetch an RCU-protected pointer, checking for RCU
  312. *
  313. * Makes rcu_dereference_check() do the dirty work.
  314. */
  315. #define rcu_dereference(p) \
  316. rcu_dereference_check(p, rcu_read_lock_held())
  317. /**
  318. * rcu_dereference_bh - fetch an RCU-protected pointer, checking for RCU-bh
  319. *
  320. * Makes rcu_dereference_check() do the dirty work.
  321. */
  322. #define rcu_dereference_bh(p) \
  323. rcu_dereference_check(p, rcu_read_lock_bh_held())
  324. /**
  325. * rcu_dereference_sched - fetch RCU-protected pointer, checking for RCU-sched
  326. *
  327. * Makes rcu_dereference_check() do the dirty work.
  328. */
  329. #define rcu_dereference_sched(p) \
  330. rcu_dereference_check(p, rcu_read_lock_sched_held())
  331. /**
  332. * rcu_assign_pointer - assign (publicize) a pointer to a newly
  333. * initialized structure that will be dereferenced by RCU read-side
  334. * critical sections. Returns the value assigned.
  335. *
  336. * Inserts memory barriers on architectures that require them
  337. * (pretty much all of them other than x86), and also prevents
  338. * the compiler from reordering the code that initializes the
  339. * structure after the pointer assignment. More importantly, this
  340. * call documents which pointers will be dereferenced by RCU read-side
  341. * code.
  342. */
  343. #define rcu_assign_pointer(p, v) \
  344. ({ \
  345. if (!__builtin_constant_p(v) || \
  346. ((v) != NULL)) \
  347. smp_wmb(); \
  348. (p) = (v); \
  349. })
  350. /* Infrastructure to implement the synchronize_() primitives. */
  351. struct rcu_synchronize {
  352. struct rcu_head head;
  353. struct completion completion;
  354. };
  355. extern void wakeme_after_rcu(struct rcu_head *head);
  356. /**
  357. * call_rcu - Queue an RCU callback for invocation after a grace period.
  358. * @head: structure to be used for queueing the RCU updates.
  359. * @func: actual update function to be invoked after the grace period
  360. *
  361. * The update function will be invoked some time after a full grace
  362. * period elapses, in other words after all currently executing RCU
  363. * read-side critical sections have completed. RCU read-side critical
  364. * sections are delimited by rcu_read_lock() and rcu_read_unlock(),
  365. * and may be nested.
  366. */
  367. extern void call_rcu(struct rcu_head *head,
  368. void (*func)(struct rcu_head *head));
  369. /**
  370. * call_rcu_bh - Queue an RCU for invocation after a quicker grace period.
  371. * @head: structure to be used for queueing the RCU updates.
  372. * @func: actual update function to be invoked after the grace period
  373. *
  374. * The update function will be invoked some time after a full grace
  375. * period elapses, in other words after all currently executing RCU
  376. * read-side critical sections have completed. call_rcu_bh() assumes
  377. * that the read-side critical sections end on completion of a softirq
  378. * handler. This means that read-side critical sections in process
  379. * context must not be interrupted by softirqs. This interface is to be
  380. * used when most of the read-side critical sections are in softirq context.
  381. * RCU read-side critical sections are delimited by :
  382. * - rcu_read_lock() and rcu_read_unlock(), if in interrupt context.
  383. * OR
  384. * - rcu_read_lock_bh() and rcu_read_unlock_bh(), if in process context.
  385. * These may be nested.
  386. */
  387. extern void call_rcu_bh(struct rcu_head *head,
  388. void (*func)(struct rcu_head *head));
  389. #endif /* __LINUX_RCUPDATE_H */