rcupdate.h 16 KB

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