rcupdate.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779
  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. #include <linux/debugobjects.h>
  42. #include <linux/compiler.h>
  43. #ifdef CONFIG_RCU_TORTURE_TEST
  44. extern int rcutorture_runnable; /* for sysctl */
  45. #endif /* #ifdef CONFIG_RCU_TORTURE_TEST */
  46. #define ULONG_CMP_GE(a, b) (ULONG_MAX / 2 >= (a) - (b))
  47. #define ULONG_CMP_LT(a, b) (ULONG_MAX / 2 < (a) - (b))
  48. /**
  49. * struct rcu_head - callback structure for use with RCU
  50. * @next: next update requests in a list
  51. * @func: actual update function to call after the grace period.
  52. */
  53. struct rcu_head {
  54. struct rcu_head *next;
  55. void (*func)(struct rcu_head *head);
  56. };
  57. /* Exported common interfaces */
  58. extern void call_rcu_sched(struct rcu_head *head,
  59. void (*func)(struct rcu_head *rcu));
  60. extern void synchronize_sched(void);
  61. extern void rcu_barrier_bh(void);
  62. extern void rcu_barrier_sched(void);
  63. extern void synchronize_sched_expedited(void);
  64. extern int sched_expedited_torture_stats(char *page);
  65. static inline void __rcu_read_lock_bh(void)
  66. {
  67. local_bh_disable();
  68. }
  69. static inline void __rcu_read_unlock_bh(void)
  70. {
  71. local_bh_enable();
  72. }
  73. #ifdef CONFIG_PREEMPT_RCU
  74. extern void __rcu_read_lock(void);
  75. extern void __rcu_read_unlock(void);
  76. void synchronize_rcu(void);
  77. /*
  78. * Defined as a macro as it is a very low level header included from
  79. * areas that don't even know about current. This gives the rcu_read_lock()
  80. * nesting depth, but makes sense only if CONFIG_PREEMPT_RCU -- in other
  81. * types of kernel builds, the rcu_read_lock() nesting depth is unknowable.
  82. */
  83. #define rcu_preempt_depth() (current->rcu_read_lock_nesting)
  84. #else /* #ifdef CONFIG_PREEMPT_RCU */
  85. static inline void __rcu_read_lock(void)
  86. {
  87. preempt_disable();
  88. }
  89. static inline void __rcu_read_unlock(void)
  90. {
  91. preempt_enable();
  92. }
  93. static inline void synchronize_rcu(void)
  94. {
  95. synchronize_sched();
  96. }
  97. static inline int rcu_preempt_depth(void)
  98. {
  99. return 0;
  100. }
  101. #endif /* #else #ifdef CONFIG_PREEMPT_RCU */
  102. /* Internal to kernel */
  103. extern void rcu_sched_qs(int cpu);
  104. extern void rcu_bh_qs(int cpu);
  105. extern void rcu_check_callbacks(int cpu, int user);
  106. struct notifier_block;
  107. #ifdef CONFIG_NO_HZ
  108. extern void rcu_enter_nohz(void);
  109. extern void rcu_exit_nohz(void);
  110. #else /* #ifdef CONFIG_NO_HZ */
  111. static inline void rcu_enter_nohz(void)
  112. {
  113. }
  114. static inline void rcu_exit_nohz(void)
  115. {
  116. }
  117. #endif /* #else #ifdef CONFIG_NO_HZ */
  118. #if defined(CONFIG_TREE_RCU) || defined(CONFIG_TREE_PREEMPT_RCU)
  119. #include <linux/rcutree.h>
  120. #elif defined(CONFIG_TINY_RCU) || defined(CONFIG_TINY_PREEMPT_RCU)
  121. #include <linux/rcutiny.h>
  122. #else
  123. #error "Unknown RCU implementation specified to kernel configuration"
  124. #endif
  125. /*
  126. * init_rcu_head_on_stack()/destroy_rcu_head_on_stack() are needed for dynamic
  127. * initialization and destruction of rcu_head on the stack. rcu_head structures
  128. * allocated dynamically in the heap or defined statically don't need any
  129. * initialization.
  130. */
  131. #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD
  132. extern void init_rcu_head_on_stack(struct rcu_head *head);
  133. extern void destroy_rcu_head_on_stack(struct rcu_head *head);
  134. #else /* !CONFIG_DEBUG_OBJECTS_RCU_HEAD */
  135. static inline void init_rcu_head_on_stack(struct rcu_head *head)
  136. {
  137. }
  138. static inline void destroy_rcu_head_on_stack(struct rcu_head *head)
  139. {
  140. }
  141. #endif /* #else !CONFIG_DEBUG_OBJECTS_RCU_HEAD */
  142. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  143. extern struct lockdep_map rcu_lock_map;
  144. # define rcu_read_acquire() \
  145. lock_acquire(&rcu_lock_map, 0, 0, 2, 1, NULL, _THIS_IP_)
  146. # define rcu_read_release() lock_release(&rcu_lock_map, 1, _THIS_IP_)
  147. extern struct lockdep_map rcu_bh_lock_map;
  148. # define rcu_read_acquire_bh() \
  149. lock_acquire(&rcu_bh_lock_map, 0, 0, 2, 1, NULL, _THIS_IP_)
  150. # define rcu_read_release_bh() lock_release(&rcu_bh_lock_map, 1, _THIS_IP_)
  151. extern struct lockdep_map rcu_sched_lock_map;
  152. # define rcu_read_acquire_sched() \
  153. lock_acquire(&rcu_sched_lock_map, 0, 0, 2, 1, NULL, _THIS_IP_)
  154. # define rcu_read_release_sched() \
  155. lock_release(&rcu_sched_lock_map, 1, _THIS_IP_)
  156. extern int debug_lockdep_rcu_enabled(void);
  157. /**
  158. * rcu_read_lock_held() - might we be in RCU read-side critical section?
  159. *
  160. * If CONFIG_DEBUG_LOCK_ALLOC is selected, returns nonzero iff in an RCU
  161. * read-side critical section. In absence of CONFIG_DEBUG_LOCK_ALLOC,
  162. * this assumes we are in an RCU read-side critical section unless it can
  163. * prove otherwise. This is useful for debug checks in functions that
  164. * require that they be called within an RCU read-side critical section.
  165. *
  166. * Checks debug_lockdep_rcu_enabled() to prevent false positives during boot
  167. * and while lockdep is disabled.
  168. */
  169. static inline int rcu_read_lock_held(void)
  170. {
  171. if (!debug_lockdep_rcu_enabled())
  172. return 1;
  173. return lock_is_held(&rcu_lock_map);
  174. }
  175. /*
  176. * rcu_read_lock_bh_held() is defined out of line to avoid #include-file
  177. * hell.
  178. */
  179. extern int rcu_read_lock_bh_held(void);
  180. /**
  181. * rcu_read_lock_sched_held() - might we be in RCU-sched read-side critical section?
  182. *
  183. * If CONFIG_DEBUG_LOCK_ALLOC is selected, returns nonzero iff in an
  184. * RCU-sched read-side critical section. In absence of
  185. * CONFIG_DEBUG_LOCK_ALLOC, this assumes we are in an RCU-sched read-side
  186. * critical section unless it can prove otherwise. Note that disabling
  187. * of preemption (including disabling irqs) counts as an RCU-sched
  188. * read-side critical section. This is useful for debug checks in functions
  189. * that required that they be called within an RCU-sched read-side
  190. * critical section.
  191. *
  192. * Check debug_lockdep_rcu_enabled() to prevent false positives during boot
  193. * and while lockdep is disabled.
  194. */
  195. #ifdef CONFIG_PREEMPT
  196. static inline int rcu_read_lock_sched_held(void)
  197. {
  198. int lockdep_opinion = 0;
  199. if (!debug_lockdep_rcu_enabled())
  200. return 1;
  201. if (debug_locks)
  202. lockdep_opinion = lock_is_held(&rcu_sched_lock_map);
  203. return lockdep_opinion || preempt_count() != 0 || irqs_disabled();
  204. }
  205. #else /* #ifdef CONFIG_PREEMPT */
  206. static inline int rcu_read_lock_sched_held(void)
  207. {
  208. return 1;
  209. }
  210. #endif /* #else #ifdef CONFIG_PREEMPT */
  211. #else /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
  212. # define rcu_read_acquire() do { } while (0)
  213. # define rcu_read_release() do { } while (0)
  214. # define rcu_read_acquire_bh() do { } while (0)
  215. # define rcu_read_release_bh() do { } while (0)
  216. # define rcu_read_acquire_sched() do { } while (0)
  217. # define rcu_read_release_sched() do { } while (0)
  218. static inline int rcu_read_lock_held(void)
  219. {
  220. return 1;
  221. }
  222. static inline int rcu_read_lock_bh_held(void)
  223. {
  224. return 1;
  225. }
  226. #ifdef CONFIG_PREEMPT
  227. static inline int rcu_read_lock_sched_held(void)
  228. {
  229. return preempt_count() != 0 || irqs_disabled();
  230. }
  231. #else /* #ifdef CONFIG_PREEMPT */
  232. static inline int rcu_read_lock_sched_held(void)
  233. {
  234. return 1;
  235. }
  236. #endif /* #else #ifdef CONFIG_PREEMPT */
  237. #endif /* #else #ifdef CONFIG_DEBUG_LOCK_ALLOC */
  238. #ifdef CONFIG_PROVE_RCU
  239. extern int rcu_my_thread_group_empty(void);
  240. /**
  241. * rcu_lockdep_assert - emit lockdep splat if specified condition not met
  242. * @c: condition to check
  243. */
  244. #define rcu_lockdep_assert(c) \
  245. do { \
  246. static bool __warned; \
  247. if (debug_lockdep_rcu_enabled() && !__warned && !(c)) { \
  248. __warned = true; \
  249. lockdep_rcu_dereference(__FILE__, __LINE__); \
  250. } \
  251. } while (0)
  252. #else /* #ifdef CONFIG_PROVE_RCU */
  253. #define rcu_lockdep_assert(c) do { } while (0)
  254. #endif /* #else #ifdef CONFIG_PROVE_RCU */
  255. /*
  256. * Helper functions for rcu_dereference_check(), rcu_dereference_protected()
  257. * and rcu_assign_pointer(). Some of these could be folded into their
  258. * callers, but they are left separate in order to ease introduction of
  259. * multiple flavors of pointers to match the multiple flavors of RCU
  260. * (e.g., __rcu_bh, * __rcu_sched, and __srcu), should this make sense in
  261. * the future.
  262. */
  263. #ifdef __CHECKER__
  264. #define rcu_dereference_sparse(p, space) \
  265. ((void)(((typeof(*p) space *)p) == p))
  266. #else /* #ifdef __CHECKER__ */
  267. #define rcu_dereference_sparse(p, space)
  268. #endif /* #else #ifdef __CHECKER__ */
  269. #define __rcu_access_pointer(p, space) \
  270. ({ \
  271. typeof(*p) *_________p1 = (typeof(*p)*__force )ACCESS_ONCE(p); \
  272. rcu_dereference_sparse(p, space); \
  273. ((typeof(*p) __force __kernel *)(_________p1)); \
  274. })
  275. #define __rcu_dereference_check(p, c, space) \
  276. ({ \
  277. typeof(*p) *_________p1 = (typeof(*p)*__force )ACCESS_ONCE(p); \
  278. rcu_lockdep_assert(c); \
  279. rcu_dereference_sparse(p, space); \
  280. smp_read_barrier_depends(); \
  281. ((typeof(*p) __force __kernel *)(_________p1)); \
  282. })
  283. #define __rcu_dereference_protected(p, c, space) \
  284. ({ \
  285. rcu_lockdep_assert(c); \
  286. rcu_dereference_sparse(p, space); \
  287. ((typeof(*p) __force __kernel *)(p)); \
  288. })
  289. #define __rcu_dereference_index_check(p, c) \
  290. ({ \
  291. typeof(p) _________p1 = ACCESS_ONCE(p); \
  292. rcu_lockdep_assert(c); \
  293. smp_read_barrier_depends(); \
  294. (_________p1); \
  295. })
  296. #define __rcu_assign_pointer(p, v, space) \
  297. ({ \
  298. if (!__builtin_constant_p(v) || \
  299. ((v) != NULL)) \
  300. smp_wmb(); \
  301. (p) = (typeof(*v) __force space *)(v); \
  302. })
  303. /**
  304. * rcu_access_pointer() - fetch RCU pointer with no dereferencing
  305. * @p: The pointer to read
  306. *
  307. * Return the value of the specified RCU-protected pointer, but omit the
  308. * smp_read_barrier_depends() and keep the ACCESS_ONCE(). This is useful
  309. * when the value of this pointer is accessed, but the pointer is not
  310. * dereferenced, for example, when testing an RCU-protected pointer against
  311. * NULL. Although rcu_access_pointer() may also be used in cases where
  312. * update-side locks prevent the value of the pointer from changing, you
  313. * should instead use rcu_dereference_protected() for this use case.
  314. */
  315. #define rcu_access_pointer(p) __rcu_access_pointer((p), __rcu)
  316. /**
  317. * rcu_dereference_check() - rcu_dereference with debug checking
  318. * @p: The pointer to read, prior to dereferencing
  319. * @c: The conditions under which the dereference will take place
  320. *
  321. * Do an rcu_dereference(), but check that the conditions under which the
  322. * dereference will take place are correct. Typically the conditions
  323. * indicate the various locking conditions that should be held at that
  324. * point. The check should return true if the conditions are satisfied.
  325. * An implicit check for being in an RCU read-side critical section
  326. * (rcu_read_lock()) is included.
  327. *
  328. * For example:
  329. *
  330. * bar = rcu_dereference_check(foo->bar, lockdep_is_held(&foo->lock));
  331. *
  332. * could be used to indicate to lockdep that foo->bar may only be dereferenced
  333. * if either rcu_read_lock() is held, or that the lock required to replace
  334. * the bar struct at foo->bar is held.
  335. *
  336. * Note that the list of conditions may also include indications of when a lock
  337. * need not be held, for example during initialisation or destruction of the
  338. * target struct:
  339. *
  340. * bar = rcu_dereference_check(foo->bar, lockdep_is_held(&foo->lock) ||
  341. * atomic_read(&foo->usage) == 0);
  342. *
  343. * Inserts memory barriers on architectures that require them
  344. * (currently only the Alpha), prevents the compiler from refetching
  345. * (and from merging fetches), and, more importantly, documents exactly
  346. * which pointers are protected by RCU and checks that the pointer is
  347. * annotated as __rcu.
  348. */
  349. #define rcu_dereference_check(p, c) \
  350. __rcu_dereference_check((p), rcu_read_lock_held() || (c), __rcu)
  351. /**
  352. * rcu_dereference_bh_check() - rcu_dereference_bh with debug checking
  353. * @p: The pointer to read, prior to dereferencing
  354. * @c: The conditions under which the dereference will take place
  355. *
  356. * This is the RCU-bh counterpart to rcu_dereference_check().
  357. */
  358. #define rcu_dereference_bh_check(p, c) \
  359. __rcu_dereference_check((p), rcu_read_lock_bh_held() || (c), __rcu)
  360. /**
  361. * rcu_dereference_sched_check() - rcu_dereference_sched with debug checking
  362. * @p: The pointer to read, prior to dereferencing
  363. * @c: The conditions under which the dereference will take place
  364. *
  365. * This is the RCU-sched counterpart to rcu_dereference_check().
  366. */
  367. #define rcu_dereference_sched_check(p, c) \
  368. __rcu_dereference_check((p), rcu_read_lock_sched_held() || (c), \
  369. __rcu)
  370. #define rcu_dereference_raw(p) rcu_dereference_check(p, 1) /*@@@ needed? @@@*/
  371. /**
  372. * rcu_dereference_index_check() - rcu_dereference for indices with debug checking
  373. * @p: The pointer to read, prior to dereferencing
  374. * @c: The conditions under which the dereference will take place
  375. *
  376. * Similar to rcu_dereference_check(), but omits the sparse checking.
  377. * This allows rcu_dereference_index_check() to be used on integers,
  378. * which can then be used as array indices. Attempting to use
  379. * rcu_dereference_check() on an integer will give compiler warnings
  380. * because the sparse address-space mechanism relies on dereferencing
  381. * the RCU-protected pointer. Dereferencing integers is not something
  382. * that even gcc will put up with.
  383. *
  384. * Note that this function does not implicitly check for RCU read-side
  385. * critical sections. If this function gains lots of uses, it might
  386. * make sense to provide versions for each flavor of RCU, but it does
  387. * not make sense as of early 2010.
  388. */
  389. #define rcu_dereference_index_check(p, c) \
  390. __rcu_dereference_index_check((p), (c))
  391. /**
  392. * rcu_dereference_protected() - fetch RCU pointer when updates prevented
  393. * @p: The pointer to read, prior to dereferencing
  394. * @c: The conditions under which the dereference will take place
  395. *
  396. * Return the value of the specified RCU-protected pointer, but omit
  397. * both the smp_read_barrier_depends() and the ACCESS_ONCE(). This
  398. * is useful in cases where update-side locks prevent the value of the
  399. * pointer from changing. Please note that this primitive does -not-
  400. * prevent the compiler from repeating this reference or combining it
  401. * with other references, so it should not be used without protection
  402. * of appropriate locks.
  403. *
  404. * This function is only for update-side use. Using this function
  405. * when protected only by rcu_read_lock() will result in infrequent
  406. * but very ugly failures.
  407. */
  408. #define rcu_dereference_protected(p, c) \
  409. __rcu_dereference_protected((p), (c), __rcu)
  410. /**
  411. * rcu_dereference_bh_protected() - fetch RCU-bh pointer when updates prevented
  412. * @p: The pointer to read, prior to dereferencing
  413. * @c: The conditions under which the dereference will take place
  414. *
  415. * This is the RCU-bh counterpart to rcu_dereference_protected().
  416. */
  417. #define rcu_dereference_bh_protected(p, c) \
  418. __rcu_dereference_protected((p), (c), __rcu)
  419. /**
  420. * rcu_dereference_sched_protected() - fetch RCU-sched pointer when updates prevented
  421. * @p: The pointer to read, prior to dereferencing
  422. * @c: The conditions under which the dereference will take place
  423. *
  424. * This is the RCU-sched counterpart to rcu_dereference_protected().
  425. */
  426. #define rcu_dereference_sched_protected(p, c) \
  427. __rcu_dereference_protected((p), (c), __rcu)
  428. /**
  429. * rcu_dereference() - fetch RCU-protected pointer for dereferencing
  430. * @p: The pointer to read, prior to dereferencing
  431. *
  432. * This is a simple wrapper around rcu_dereference_check().
  433. */
  434. #define rcu_dereference(p) rcu_dereference_check(p, 0)
  435. /**
  436. * rcu_dereference_bh() - fetch an RCU-bh-protected pointer for dereferencing
  437. * @p: The pointer to read, prior to dereferencing
  438. *
  439. * Makes rcu_dereference_check() do the dirty work.
  440. */
  441. #define rcu_dereference_bh(p) rcu_dereference_bh_check(p, 0)
  442. /**
  443. * rcu_dereference_sched() - fetch RCU-sched-protected pointer for dereferencing
  444. * @p: The pointer to read, prior to dereferencing
  445. *
  446. * Makes rcu_dereference_check() do the dirty work.
  447. */
  448. #define rcu_dereference_sched(p) rcu_dereference_sched_check(p, 0)
  449. /**
  450. * rcu_read_lock() - mark the beginning of an RCU read-side critical section
  451. *
  452. * When synchronize_rcu() is invoked on one CPU while other CPUs
  453. * are within RCU read-side critical sections, then the
  454. * synchronize_rcu() is guaranteed to block until after all the other
  455. * CPUs exit their critical sections. Similarly, if call_rcu() is invoked
  456. * on one CPU while other CPUs are within RCU read-side critical
  457. * sections, invocation of the corresponding RCU callback is deferred
  458. * until after the all the other CPUs exit their critical sections.
  459. *
  460. * Note, however, that RCU callbacks are permitted to run concurrently
  461. * with new RCU read-side critical sections. One way that this can happen
  462. * is via the following sequence of events: (1) CPU 0 enters an RCU
  463. * read-side critical section, (2) CPU 1 invokes call_rcu() to register
  464. * an RCU callback, (3) CPU 0 exits the RCU read-side critical section,
  465. * (4) CPU 2 enters a RCU read-side critical section, (5) the RCU
  466. * callback is invoked. This is legal, because the RCU read-side critical
  467. * section that was running concurrently with the call_rcu() (and which
  468. * therefore might be referencing something that the corresponding RCU
  469. * callback would free up) has completed before the corresponding
  470. * RCU callback is invoked.
  471. *
  472. * RCU read-side critical sections may be nested. Any deferred actions
  473. * will be deferred until the outermost RCU read-side critical section
  474. * completes.
  475. *
  476. * You can avoid reading and understanding the next paragraph by
  477. * following this rule: don't put anything in an rcu_read_lock() RCU
  478. * read-side critical section that would block in a !PREEMPT kernel.
  479. * But if you want the full story, read on!
  480. *
  481. * In non-preemptible RCU implementations (TREE_RCU and TINY_RCU), it
  482. * is illegal to block while in an RCU read-side critical section. In
  483. * preemptible RCU implementations (TREE_PREEMPT_RCU and TINY_PREEMPT_RCU)
  484. * in CONFIG_PREEMPT kernel builds, RCU read-side critical sections may
  485. * be preempted, but explicit blocking is illegal. Finally, in preemptible
  486. * RCU implementations in real-time (CONFIG_PREEMPT_RT) kernel builds,
  487. * RCU read-side critical sections may be preempted and they may also
  488. * block, but only when acquiring spinlocks that are subject to priority
  489. * inheritance.
  490. */
  491. static inline void rcu_read_lock(void)
  492. {
  493. __rcu_read_lock();
  494. __acquire(RCU);
  495. rcu_read_acquire();
  496. }
  497. /*
  498. * So where is rcu_write_lock()? It does not exist, as there is no
  499. * way for writers to lock out RCU readers. This is a feature, not
  500. * a bug -- this property is what provides RCU's performance benefits.
  501. * Of course, writers must coordinate with each other. The normal
  502. * spinlock primitives work well for this, but any other technique may be
  503. * used as well. RCU does not care how the writers keep out of each
  504. * others' way, as long as they do so.
  505. */
  506. /**
  507. * rcu_read_unlock() - marks the end of an RCU read-side critical section.
  508. *
  509. * See rcu_read_lock() for more information.
  510. */
  511. static inline void rcu_read_unlock(void)
  512. {
  513. rcu_read_release();
  514. __release(RCU);
  515. __rcu_read_unlock();
  516. }
  517. /**
  518. * rcu_read_lock_bh() - mark the beginning of an RCU-bh critical section
  519. *
  520. * This is equivalent of rcu_read_lock(), but to be used when updates
  521. * are being done using call_rcu_bh() or synchronize_rcu_bh(). Since
  522. * both call_rcu_bh() and synchronize_rcu_bh() consider completion of a
  523. * softirq handler to be a quiescent state, a process in RCU read-side
  524. * critical section must be protected by disabling softirqs. Read-side
  525. * critical sections in interrupt context can use just rcu_read_lock(),
  526. * though this should at least be commented to avoid confusing people
  527. * reading the code.
  528. */
  529. static inline void rcu_read_lock_bh(void)
  530. {
  531. __rcu_read_lock_bh();
  532. __acquire(RCU_BH);
  533. rcu_read_acquire_bh();
  534. }
  535. /*
  536. * rcu_read_unlock_bh - marks the end of a softirq-only RCU critical section
  537. *
  538. * See rcu_read_lock_bh() for more information.
  539. */
  540. static inline void rcu_read_unlock_bh(void)
  541. {
  542. rcu_read_release_bh();
  543. __release(RCU_BH);
  544. __rcu_read_unlock_bh();
  545. }
  546. /**
  547. * rcu_read_lock_sched() - mark the beginning of a RCU-sched critical section
  548. *
  549. * This is equivalent of rcu_read_lock(), but to be used when updates
  550. * are being done using call_rcu_sched() or synchronize_rcu_sched().
  551. * Read-side critical sections can also be introduced by anything that
  552. * disables preemption, including local_irq_disable() and friends.
  553. */
  554. static inline void rcu_read_lock_sched(void)
  555. {
  556. preempt_disable();
  557. __acquire(RCU_SCHED);
  558. rcu_read_acquire_sched();
  559. }
  560. /* Used by lockdep and tracing: cannot be traced, cannot call lockdep. */
  561. static inline notrace void rcu_read_lock_sched_notrace(void)
  562. {
  563. preempt_disable_notrace();
  564. __acquire(RCU_SCHED);
  565. }
  566. /*
  567. * rcu_read_unlock_sched - marks the end of a RCU-classic critical section
  568. *
  569. * See rcu_read_lock_sched for more information.
  570. */
  571. static inline void rcu_read_unlock_sched(void)
  572. {
  573. rcu_read_release_sched();
  574. __release(RCU_SCHED);
  575. preempt_enable();
  576. }
  577. /* Used by lockdep and tracing: cannot be traced, cannot call lockdep. */
  578. static inline notrace void rcu_read_unlock_sched_notrace(void)
  579. {
  580. __release(RCU_SCHED);
  581. preempt_enable_notrace();
  582. }
  583. /**
  584. * rcu_assign_pointer() - assign to RCU-protected pointer
  585. * @p: pointer to assign to
  586. * @v: value to assign (publish)
  587. *
  588. * Assigns the specified value to the specified RCU-protected
  589. * pointer, ensuring that any concurrent RCU readers will see
  590. * any prior initialization. Returns the value assigned.
  591. *
  592. * Inserts memory barriers on architectures that require them
  593. * (pretty much all of them other than x86), and also prevents
  594. * the compiler from reordering the code that initializes the
  595. * structure after the pointer assignment. More importantly, this
  596. * call documents which pointers will be dereferenced by RCU read-side
  597. * code.
  598. */
  599. #define rcu_assign_pointer(p, v) \
  600. __rcu_assign_pointer((p), (v), __rcu)
  601. /**
  602. * RCU_INIT_POINTER() - initialize an RCU protected pointer
  603. *
  604. * Initialize an RCU-protected pointer in such a way to avoid RCU-lockdep
  605. * splats.
  606. */
  607. #define RCU_INIT_POINTER(p, v) \
  608. p = (typeof(*v) __force __rcu *)(v)
  609. /* Infrastructure to implement the synchronize_() primitives. */
  610. struct rcu_synchronize {
  611. struct rcu_head head;
  612. struct completion completion;
  613. };
  614. extern void wakeme_after_rcu(struct rcu_head *head);
  615. #ifdef CONFIG_PREEMPT_RCU
  616. /**
  617. * call_rcu() - Queue an RCU callback for invocation after a grace period.
  618. * @head: structure to be used for queueing the RCU updates.
  619. * @func: actual callback function to be invoked after the grace period
  620. *
  621. * The callback function will be invoked some time after a full grace
  622. * period elapses, in other words after all pre-existing RCU read-side
  623. * critical sections have completed. However, the callback function
  624. * might well execute concurrently with RCU read-side critical sections
  625. * that started after call_rcu() was invoked. RCU read-side critical
  626. * sections are delimited by rcu_read_lock() and rcu_read_unlock(),
  627. * and may be nested.
  628. */
  629. extern void call_rcu(struct rcu_head *head,
  630. void (*func)(struct rcu_head *head));
  631. #else /* #ifdef CONFIG_PREEMPT_RCU */
  632. /* In classic RCU, call_rcu() is just call_rcu_sched(). */
  633. #define call_rcu call_rcu_sched
  634. #endif /* #else #ifdef CONFIG_PREEMPT_RCU */
  635. /**
  636. * call_rcu_bh() - Queue an RCU for invocation after a quicker grace period.
  637. * @head: structure to be used for queueing the RCU updates.
  638. * @func: actual callback function to be invoked after the grace period
  639. *
  640. * The callback function will be invoked some time after a full grace
  641. * period elapses, in other words after all currently executing RCU
  642. * read-side critical sections have completed. call_rcu_bh() assumes
  643. * that the read-side critical sections end on completion of a softirq
  644. * handler. This means that read-side critical sections in process
  645. * context must not be interrupted by softirqs. This interface is to be
  646. * used when most of the read-side critical sections are in softirq context.
  647. * RCU read-side critical sections are delimited by :
  648. * - rcu_read_lock() and rcu_read_unlock(), if in interrupt context.
  649. * OR
  650. * - rcu_read_lock_bh() and rcu_read_unlock_bh(), if in process context.
  651. * These may be nested.
  652. */
  653. extern void call_rcu_bh(struct rcu_head *head,
  654. void (*func)(struct rcu_head *head));
  655. /*
  656. * debug_rcu_head_queue()/debug_rcu_head_unqueue() are used internally
  657. * by call_rcu() and rcu callback execution, and are therefore not part of the
  658. * RCU API. Leaving in rcupdate.h because they are used by all RCU flavors.
  659. */
  660. #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD
  661. # define STATE_RCU_HEAD_READY 0
  662. # define STATE_RCU_HEAD_QUEUED 1
  663. extern struct debug_obj_descr rcuhead_debug_descr;
  664. static inline void debug_rcu_head_queue(struct rcu_head *head)
  665. {
  666. debug_object_activate(head, &rcuhead_debug_descr);
  667. debug_object_active_state(head, &rcuhead_debug_descr,
  668. STATE_RCU_HEAD_READY,
  669. STATE_RCU_HEAD_QUEUED);
  670. }
  671. static inline void debug_rcu_head_unqueue(struct rcu_head *head)
  672. {
  673. debug_object_active_state(head, &rcuhead_debug_descr,
  674. STATE_RCU_HEAD_QUEUED,
  675. STATE_RCU_HEAD_READY);
  676. debug_object_deactivate(head, &rcuhead_debug_descr);
  677. }
  678. #else /* !CONFIG_DEBUG_OBJECTS_RCU_HEAD */
  679. static inline void debug_rcu_head_queue(struct rcu_head *head)
  680. {
  681. }
  682. static inline void debug_rcu_head_unqueue(struct rcu_head *head)
  683. {
  684. }
  685. #endif /* #else !CONFIG_DEBUG_OBJECTS_RCU_HEAD */
  686. #endif /* __LINUX_RCUPDATE_H */