rcupdate.h 17 KB

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