rcupdate.h 27 KB

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