rcupdate.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780
  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_init(void);
  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_dereference_index_check(p, c) \
  291. ({ \
  292. typeof(p) _________p1 = ACCESS_ONCE(p); \
  293. rcu_lockdep_assert(c); \
  294. smp_read_barrier_depends(); \
  295. (_________p1); \
  296. })
  297. #define __rcu_assign_pointer(p, v, space) \
  298. ({ \
  299. if (!__builtin_constant_p(v) || \
  300. ((v) != NULL)) \
  301. smp_wmb(); \
  302. (p) = (typeof(*v) __force space *)(v); \
  303. })
  304. /**
  305. * rcu_access_pointer() - fetch RCU pointer with no dereferencing
  306. * @p: The pointer to read
  307. *
  308. * Return the value of the specified RCU-protected pointer, but omit the
  309. * smp_read_barrier_depends() and keep the ACCESS_ONCE(). This is useful
  310. * when the value of this pointer is accessed, but the pointer is not
  311. * dereferenced, for example, when testing an RCU-protected pointer against
  312. * NULL. Although rcu_access_pointer() may also be used in cases where
  313. * update-side locks prevent the value of the pointer from changing, you
  314. * should instead use rcu_dereference_protected() for this use case.
  315. */
  316. #define rcu_access_pointer(p) __rcu_access_pointer((p), __rcu)
  317. /**
  318. * rcu_dereference_check() - rcu_dereference with debug checking
  319. * @p: The pointer to read, prior to dereferencing
  320. * @c: The conditions under which the dereference will take place
  321. *
  322. * Do an rcu_dereference(), but check that the conditions under which the
  323. * dereference will take place are correct. Typically the conditions
  324. * indicate the various locking conditions that should be held at that
  325. * point. The check should return true if the conditions are satisfied.
  326. * An implicit check for being in an RCU read-side critical section
  327. * (rcu_read_lock()) is included.
  328. *
  329. * For example:
  330. *
  331. * bar = rcu_dereference_check(foo->bar, lockdep_is_held(&foo->lock));
  332. *
  333. * could be used to indicate to lockdep that foo->bar may only be dereferenced
  334. * if either rcu_read_lock() is held, or that the lock required to replace
  335. * the bar struct at foo->bar is held.
  336. *
  337. * Note that the list of conditions may also include indications of when a lock
  338. * need not be held, for example during initialisation or destruction of the
  339. * target struct:
  340. *
  341. * bar = rcu_dereference_check(foo->bar, lockdep_is_held(&foo->lock) ||
  342. * atomic_read(&foo->usage) == 0);
  343. *
  344. * Inserts memory barriers on architectures that require them
  345. * (currently only the Alpha), prevents the compiler from refetching
  346. * (and from merging fetches), and, more importantly, documents exactly
  347. * which pointers are protected by RCU and checks that the pointer is
  348. * annotated as __rcu.
  349. */
  350. #define rcu_dereference_check(p, c) \
  351. __rcu_dereference_check((p), rcu_read_lock_held() || (c), __rcu)
  352. /**
  353. * rcu_dereference_bh_check() - rcu_dereference_bh with debug checking
  354. * @p: The pointer to read, prior to dereferencing
  355. * @c: The conditions under which the dereference will take place
  356. *
  357. * This is the RCU-bh counterpart to rcu_dereference_check().
  358. */
  359. #define rcu_dereference_bh_check(p, c) \
  360. __rcu_dereference_check((p), rcu_read_lock_bh_held() || (c), __rcu)
  361. /**
  362. * rcu_dereference_sched_check() - rcu_dereference_sched with debug checking
  363. * @p: The pointer to read, prior to dereferencing
  364. * @c: The conditions under which the dereference will take place
  365. *
  366. * This is the RCU-sched counterpart to rcu_dereference_check().
  367. */
  368. #define rcu_dereference_sched_check(p, c) \
  369. __rcu_dereference_check((p), rcu_read_lock_sched_held() || (c), \
  370. __rcu)
  371. #define rcu_dereference_raw(p) rcu_dereference_check(p, 1) /*@@@ needed? @@@*/
  372. /**
  373. * rcu_dereference_index_check() - rcu_dereference for indices with debug checking
  374. * @p: The pointer to read, prior to dereferencing
  375. * @c: The conditions under which the dereference will take place
  376. *
  377. * Similar to rcu_dereference_check(), but omits the sparse checking.
  378. * This allows rcu_dereference_index_check() to be used on integers,
  379. * which can then be used as array indices. Attempting to use
  380. * rcu_dereference_check() on an integer will give compiler warnings
  381. * because the sparse address-space mechanism relies on dereferencing
  382. * the RCU-protected pointer. Dereferencing integers is not something
  383. * that even gcc will put up with.
  384. *
  385. * Note that this function does not implicitly check for RCU read-side
  386. * critical sections. If this function gains lots of uses, it might
  387. * make sense to provide versions for each flavor of RCU, but it does
  388. * not make sense as of early 2010.
  389. */
  390. #define rcu_dereference_index_check(p, c) \
  391. __rcu_dereference_index_check((p), (c))
  392. /**
  393. * rcu_dereference_protected() - fetch RCU pointer when updates prevented
  394. * @p: The pointer to read, prior to dereferencing
  395. * @c: The conditions under which the dereference will take place
  396. *
  397. * Return the value of the specified RCU-protected pointer, but omit
  398. * both the smp_read_barrier_depends() and the ACCESS_ONCE(). This
  399. * is useful in cases where update-side locks prevent the value of the
  400. * pointer from changing. Please note that this primitive does -not-
  401. * prevent the compiler from repeating this reference or combining it
  402. * with other references, so it should not be used without protection
  403. * of appropriate locks.
  404. *
  405. * This function is only for update-side use. Using this function
  406. * when protected only by rcu_read_lock() will result in infrequent
  407. * but very ugly failures.
  408. */
  409. #define rcu_dereference_protected(p, c) \
  410. __rcu_dereference_protected((p), (c), __rcu)
  411. /**
  412. * rcu_dereference_bh_protected() - fetch RCU-bh 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. * This is the RCU-bh counterpart to rcu_dereference_protected().
  417. */
  418. #define rcu_dereference_bh_protected(p, c) \
  419. __rcu_dereference_protected((p), (c), __rcu)
  420. /**
  421. * rcu_dereference_sched_protected() - fetch RCU-sched pointer when updates prevented
  422. * @p: The pointer to read, prior to dereferencing
  423. * @c: The conditions under which the dereference will take place
  424. *
  425. * This is the RCU-sched counterpart to rcu_dereference_protected().
  426. */
  427. #define rcu_dereference_sched_protected(p, c) \
  428. __rcu_dereference_protected((p), (c), __rcu)
  429. /**
  430. * rcu_dereference() - fetch RCU-protected pointer for dereferencing
  431. * @p: The pointer to read, prior to dereferencing
  432. *
  433. * This is a simple wrapper around rcu_dereference_check().
  434. */
  435. #define rcu_dereference(p) rcu_dereference_check(p, 0)
  436. /**
  437. * rcu_dereference_bh() - fetch an RCU-bh-protected pointer for dereferencing
  438. * @p: The pointer to read, prior to dereferencing
  439. *
  440. * Makes rcu_dereference_check() do the dirty work.
  441. */
  442. #define rcu_dereference_bh(p) rcu_dereference_bh_check(p, 0)
  443. /**
  444. * rcu_dereference_sched() - fetch RCU-sched-protected pointer for dereferencing
  445. * @p: The pointer to read, prior to dereferencing
  446. *
  447. * Makes rcu_dereference_check() do the dirty work.
  448. */
  449. #define rcu_dereference_sched(p) rcu_dereference_sched_check(p, 0)
  450. /**
  451. * rcu_read_lock() - mark the beginning of an RCU read-side critical section
  452. *
  453. * When synchronize_rcu() is invoked on one CPU while other CPUs
  454. * are within RCU read-side critical sections, then the
  455. * synchronize_rcu() is guaranteed to block until after all the other
  456. * CPUs exit their critical sections. Similarly, if call_rcu() is invoked
  457. * on one CPU while other CPUs are within RCU read-side critical
  458. * sections, invocation of the corresponding RCU callback is deferred
  459. * until after the all the other CPUs exit their critical sections.
  460. *
  461. * Note, however, that RCU callbacks are permitted to run concurrently
  462. * with new RCU read-side critical sections. One way that this can happen
  463. * is via the following sequence of events: (1) CPU 0 enters an RCU
  464. * read-side critical section, (2) CPU 1 invokes call_rcu() to register
  465. * an RCU callback, (3) CPU 0 exits the RCU read-side critical section,
  466. * (4) CPU 2 enters a RCU read-side critical section, (5) the RCU
  467. * callback is invoked. This is legal, because the RCU read-side critical
  468. * section that was running concurrently with the call_rcu() (and which
  469. * therefore might be referencing something that the corresponding RCU
  470. * callback would free up) has completed before the corresponding
  471. * RCU callback is invoked.
  472. *
  473. * RCU read-side critical sections may be nested. Any deferred actions
  474. * will be deferred until the outermost RCU read-side critical section
  475. * completes.
  476. *
  477. * You can avoid reading and understanding the next paragraph by
  478. * following this rule: don't put anything in an rcu_read_lock() RCU
  479. * read-side critical section that would block in a !PREEMPT kernel.
  480. * But if you want the full story, read on!
  481. *
  482. * In non-preemptible RCU implementations (TREE_RCU and TINY_RCU), it
  483. * is illegal to block while in an RCU read-side critical section. In
  484. * preemptible RCU implementations (TREE_PREEMPT_RCU and TINY_PREEMPT_RCU)
  485. * in CONFIG_PREEMPT kernel builds, RCU read-side critical sections may
  486. * be preempted, but explicit blocking is illegal. Finally, in preemptible
  487. * RCU implementations in real-time (CONFIG_PREEMPT_RT) kernel builds,
  488. * RCU read-side critical sections may be preempted and they may also
  489. * block, but only when acquiring spinlocks that are subject to priority
  490. * inheritance.
  491. */
  492. static inline void rcu_read_lock(void)
  493. {
  494. __rcu_read_lock();
  495. __acquire(RCU);
  496. rcu_read_acquire();
  497. }
  498. /*
  499. * So where is rcu_write_lock()? It does not exist, as there is no
  500. * way for writers to lock out RCU readers. This is a feature, not
  501. * a bug -- this property is what provides RCU's performance benefits.
  502. * Of course, writers must coordinate with each other. The normal
  503. * spinlock primitives work well for this, but any other technique may be
  504. * used as well. RCU does not care how the writers keep out of each
  505. * others' way, as long as they do so.
  506. */
  507. /**
  508. * rcu_read_unlock() - marks the end of an RCU read-side critical section.
  509. *
  510. * See rcu_read_lock() for more information.
  511. */
  512. static inline void rcu_read_unlock(void)
  513. {
  514. rcu_read_release();
  515. __release(RCU);
  516. __rcu_read_unlock();
  517. }
  518. /**
  519. * rcu_read_lock_bh() - mark the beginning of an RCU-bh critical section
  520. *
  521. * This is equivalent of rcu_read_lock(), but to be used when updates
  522. * are being done using call_rcu_bh() or synchronize_rcu_bh(). Since
  523. * both call_rcu_bh() and synchronize_rcu_bh() consider completion of a
  524. * softirq handler to be a quiescent state, a process in RCU read-side
  525. * critical section must be protected by disabling softirqs. Read-side
  526. * critical sections in interrupt context can use just rcu_read_lock(),
  527. * though this should at least be commented to avoid confusing people
  528. * reading the code.
  529. */
  530. static inline void rcu_read_lock_bh(void)
  531. {
  532. __rcu_read_lock_bh();
  533. __acquire(RCU_BH);
  534. rcu_read_acquire_bh();
  535. }
  536. /*
  537. * rcu_read_unlock_bh - marks the end of a softirq-only RCU critical section
  538. *
  539. * See rcu_read_lock_bh() for more information.
  540. */
  541. static inline void rcu_read_unlock_bh(void)
  542. {
  543. rcu_read_release_bh();
  544. __release(RCU_BH);
  545. __rcu_read_unlock_bh();
  546. }
  547. /**
  548. * rcu_read_lock_sched() - mark the beginning of a RCU-sched critical section
  549. *
  550. * This is equivalent of rcu_read_lock(), but to be used when updates
  551. * are being done using call_rcu_sched() or synchronize_rcu_sched().
  552. * Read-side critical sections can also be introduced by anything that
  553. * disables preemption, including local_irq_disable() and friends.
  554. */
  555. static inline void rcu_read_lock_sched(void)
  556. {
  557. preempt_disable();
  558. __acquire(RCU_SCHED);
  559. rcu_read_acquire_sched();
  560. }
  561. /* Used by lockdep and tracing: cannot be traced, cannot call lockdep. */
  562. static inline notrace void rcu_read_lock_sched_notrace(void)
  563. {
  564. preempt_disable_notrace();
  565. __acquire(RCU_SCHED);
  566. }
  567. /*
  568. * rcu_read_unlock_sched - marks the end of a RCU-classic critical section
  569. *
  570. * See rcu_read_lock_sched for more information.
  571. */
  572. static inline void rcu_read_unlock_sched(void)
  573. {
  574. rcu_read_release_sched();
  575. __release(RCU_SCHED);
  576. preempt_enable();
  577. }
  578. /* Used by lockdep and tracing: cannot be traced, cannot call lockdep. */
  579. static inline notrace void rcu_read_unlock_sched_notrace(void)
  580. {
  581. __release(RCU_SCHED);
  582. preempt_enable_notrace();
  583. }
  584. /**
  585. * rcu_assign_pointer() - assign to RCU-protected pointer
  586. * @p: pointer to assign to
  587. * @v: value to assign (publish)
  588. *
  589. * Assigns the specified value to the specified RCU-protected
  590. * pointer, ensuring that any concurrent RCU readers will see
  591. * any prior initialization. Returns the value assigned.
  592. *
  593. * Inserts memory barriers on architectures that require them
  594. * (pretty much all of them other than x86), and also prevents
  595. * the compiler from reordering the code that initializes the
  596. * structure after the pointer assignment. More importantly, this
  597. * call documents which pointers will be dereferenced by RCU read-side
  598. * code.
  599. */
  600. #define rcu_assign_pointer(p, v) \
  601. __rcu_assign_pointer((p), (v), __rcu)
  602. /**
  603. * RCU_INIT_POINTER() - initialize an RCU protected pointer
  604. *
  605. * Initialize an RCU-protected pointer in such a way to avoid RCU-lockdep
  606. * splats.
  607. */
  608. #define RCU_INIT_POINTER(p, v) \
  609. p = (typeof(*v) __force __rcu *)(v)
  610. /* Infrastructure to implement the synchronize_() primitives. */
  611. struct rcu_synchronize {
  612. struct rcu_head head;
  613. struct completion completion;
  614. };
  615. extern void wakeme_after_rcu(struct rcu_head *head);
  616. #ifdef CONFIG_PREEMPT_RCU
  617. /**
  618. * call_rcu() - Queue an RCU callback for invocation after a grace period.
  619. * @head: structure to be used for queueing the RCU updates.
  620. * @func: actual callback function to be invoked after the grace period
  621. *
  622. * The callback function will be invoked some time after a full grace
  623. * period elapses, in other words after all pre-existing RCU read-side
  624. * critical sections have completed. However, the callback function
  625. * might well execute concurrently with RCU read-side critical sections
  626. * that started after call_rcu() was invoked. RCU read-side critical
  627. * sections are delimited by rcu_read_lock() and rcu_read_unlock(),
  628. * and may be nested.
  629. */
  630. extern void call_rcu(struct rcu_head *head,
  631. void (*func)(struct rcu_head *head));
  632. #else /* #ifdef CONFIG_PREEMPT_RCU */
  633. /* In classic RCU, call_rcu() is just call_rcu_sched(). */
  634. #define call_rcu call_rcu_sched
  635. #endif /* #else #ifdef CONFIG_PREEMPT_RCU */
  636. /**
  637. * call_rcu_bh() - Queue an RCU for invocation after a quicker 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 currently executing RCU
  643. * read-side critical sections have completed. call_rcu_bh() assumes
  644. * that the read-side critical sections end on completion of a softirq
  645. * handler. This means that read-side critical sections in process
  646. * context must not be interrupted by softirqs. This interface is to be
  647. * used when most of the read-side critical sections are in softirq context.
  648. * RCU read-side critical sections are delimited by :
  649. * - rcu_read_lock() and rcu_read_unlock(), if in interrupt context.
  650. * OR
  651. * - rcu_read_lock_bh() and rcu_read_unlock_bh(), if in process context.
  652. * These may be nested.
  653. */
  654. extern void call_rcu_bh(struct rcu_head *head,
  655. void (*func)(struct rcu_head *head));
  656. /*
  657. * debug_rcu_head_queue()/debug_rcu_head_unqueue() are used internally
  658. * by call_rcu() and rcu callback execution, and are therefore not part of the
  659. * RCU API. Leaving in rcupdate.h because they are used by all RCU flavors.
  660. */
  661. #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD
  662. # define STATE_RCU_HEAD_READY 0
  663. # define STATE_RCU_HEAD_QUEUED 1
  664. extern struct debug_obj_descr rcuhead_debug_descr;
  665. static inline void debug_rcu_head_queue(struct rcu_head *head)
  666. {
  667. debug_object_activate(head, &rcuhead_debug_descr);
  668. debug_object_active_state(head, &rcuhead_debug_descr,
  669. STATE_RCU_HEAD_READY,
  670. STATE_RCU_HEAD_QUEUED);
  671. }
  672. static inline void debug_rcu_head_unqueue(struct rcu_head *head)
  673. {
  674. debug_object_active_state(head, &rcuhead_debug_descr,
  675. STATE_RCU_HEAD_QUEUED,
  676. STATE_RCU_HEAD_READY);
  677. debug_object_deactivate(head, &rcuhead_debug_descr);
  678. }
  679. #else /* !CONFIG_DEBUG_OBJECTS_RCU_HEAD */
  680. static inline void debug_rcu_head_queue(struct rcu_head *head)
  681. {
  682. }
  683. static inline void debug_rcu_head_unqueue(struct rcu_head *head)
  684. {
  685. }
  686. #endif /* #else !CONFIG_DEBUG_OBJECTS_RCU_HEAD */
  687. #endif /* __LINUX_RCUPDATE_H */