rcupdate.h 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * Read-Copy Update mechanism for mutual exclusion
  4. *
  5. * Copyright IBM Corporation, 2001
  6. *
  7. * Author: Dipankar Sarma <[email protected]>
  8. *
  9. * Based on the original work by Paul McKenney <[email protected]>
  10. * and inputs from Rusty Russell, Andrea Arcangeli and Andi Kleen.
  11. * Papers:
  12. * http://www.rdrop.com/users/paulmck/paper/rclockpdcsproof.pdf
  13. * http://lse.sourceforge.net/locking/rclock_OLS.2001.05.01c.sc.pdf (OLS2001)
  14. *
  15. * For detailed explanation of Read-Copy Update mechanism see -
  16. * http://lse.sourceforge.net/locking/rcupdate.html
  17. *
  18. */
  19. #ifndef __LINUX_RCUPDATE_H
  20. #define __LINUX_RCUPDATE_H
  21. #include <linux/types.h>
  22. #include <linux/compiler.h>
  23. #include <linux/atomic.h>
  24. #include <linux/irqflags.h>
  25. #include <linux/preempt.h>
  26. #include <linux/bottom_half.h>
  27. #include <linux/lockdep.h>
  28. #include <asm/processor.h>
  29. #include <linux/cpumask.h>
  30. #include <linux/context_tracking_irq.h>
  31. #define ULONG_CMP_GE(a, b) (ULONG_MAX / 2 >= (a) - (b))
  32. #define ULONG_CMP_LT(a, b) (ULONG_MAX / 2 < (a) - (b))
  33. #define ulong2long(a) (*(long *)(&(a)))
  34. #define USHORT_CMP_GE(a, b) (USHRT_MAX / 2 >= (unsigned short)((a) - (b)))
  35. #define USHORT_CMP_LT(a, b) (USHRT_MAX / 2 < (unsigned short)((a) - (b)))
  36. /* Exported common interfaces */
  37. void call_rcu(struct rcu_head *head, rcu_callback_t func);
  38. void rcu_barrier_tasks(void);
  39. void rcu_barrier_tasks_rude(void);
  40. void synchronize_rcu(void);
  41. struct rcu_gp_oldstate;
  42. unsigned long get_completed_synchronize_rcu(void);
  43. void get_completed_synchronize_rcu_full(struct rcu_gp_oldstate *rgosp);
  44. // Maximum number of unsigned long values corresponding to
  45. // not-yet-completed RCU grace periods.
  46. #define NUM_ACTIVE_RCU_POLL_OLDSTATE 2
  47. /**
  48. * same_state_synchronize_rcu - Are two old-state values identical?
  49. * @oldstate1: First old-state value.
  50. * @oldstate2: Second old-state value.
  51. *
  52. * The two old-state values must have been obtained from either
  53. * get_state_synchronize_rcu(), start_poll_synchronize_rcu(), or
  54. * get_completed_synchronize_rcu(). Returns @true if the two values are
  55. * identical and @false otherwise. This allows structures whose lifetimes
  56. * are tracked by old-state values to push these values to a list header,
  57. * allowing those structures to be slightly smaller.
  58. */
  59. static inline bool same_state_synchronize_rcu(unsigned long oldstate1, unsigned long oldstate2)
  60. {
  61. return oldstate1 == oldstate2;
  62. }
  63. #ifdef CONFIG_PREEMPT_RCU
  64. void __rcu_read_lock(void);
  65. void __rcu_read_unlock(void);
  66. /*
  67. * Defined as a macro as it is a very low level header included from
  68. * areas that don't even know about current. This gives the rcu_read_lock()
  69. * nesting depth, but makes sense only if CONFIG_PREEMPT_RCU -- in other
  70. * types of kernel builds, the rcu_read_lock() nesting depth is unknowable.
  71. */
  72. #define rcu_preempt_depth() READ_ONCE(current->rcu_read_lock_nesting)
  73. #else /* #ifdef CONFIG_PREEMPT_RCU */
  74. #ifdef CONFIG_TINY_RCU
  75. #define rcu_read_unlock_strict() do { } while (0)
  76. #else
  77. void rcu_read_unlock_strict(void);
  78. #endif
  79. static inline void __rcu_read_lock(void)
  80. {
  81. preempt_disable();
  82. }
  83. static inline void __rcu_read_unlock(void)
  84. {
  85. preempt_enable();
  86. if (IS_ENABLED(CONFIG_RCU_STRICT_GRACE_PERIOD))
  87. rcu_read_unlock_strict();
  88. }
  89. static inline int rcu_preempt_depth(void)
  90. {
  91. return 0;
  92. }
  93. #endif /* #else #ifdef CONFIG_PREEMPT_RCU */
  94. #ifdef CONFIG_RCU_LAZY
  95. void call_rcu_hurry(struct rcu_head *head, rcu_callback_t func);
  96. #else
  97. static inline void call_rcu_hurry(struct rcu_head *head, rcu_callback_t func)
  98. {
  99. call_rcu(head, func);
  100. }
  101. #endif
  102. /* Internal to kernel */
  103. void rcu_init(void);
  104. extern int rcu_scheduler_active;
  105. void rcu_sched_clock_irq(int user);
  106. void rcu_report_dead(unsigned int cpu);
  107. void rcutree_migrate_callbacks(int cpu);
  108. #ifdef CONFIG_TASKS_RCU_GENERIC
  109. void rcu_init_tasks_generic(void);
  110. #else
  111. static inline void rcu_init_tasks_generic(void) { }
  112. #endif
  113. #ifdef CONFIG_RCU_STALL_COMMON
  114. void rcu_sysrq_start(void);
  115. void rcu_sysrq_end(void);
  116. #else /* #ifdef CONFIG_RCU_STALL_COMMON */
  117. static inline void rcu_sysrq_start(void) { }
  118. static inline void rcu_sysrq_end(void) { }
  119. #endif /* #else #ifdef CONFIG_RCU_STALL_COMMON */
  120. #if defined(CONFIG_NO_HZ_FULL) && (!defined(CONFIG_GENERIC_ENTRY) || !defined(CONFIG_KVM_XFER_TO_GUEST_WORK))
  121. void rcu_irq_work_resched(void);
  122. #else
  123. static inline void rcu_irq_work_resched(void) { }
  124. #endif
  125. #ifdef CONFIG_RCU_NOCB_CPU
  126. void rcu_init_nohz(void);
  127. int rcu_nocb_cpu_offload(int cpu);
  128. int rcu_nocb_cpu_deoffload(int cpu);
  129. void rcu_nocb_flush_deferred_wakeup(void);
  130. #else /* #ifdef CONFIG_RCU_NOCB_CPU */
  131. static inline void rcu_init_nohz(void) { }
  132. static inline int rcu_nocb_cpu_offload(int cpu) { return -EINVAL; }
  133. static inline int rcu_nocb_cpu_deoffload(int cpu) { return 0; }
  134. static inline void rcu_nocb_flush_deferred_wakeup(void) { }
  135. #endif /* #else #ifdef CONFIG_RCU_NOCB_CPU */
  136. /**
  137. * RCU_NONIDLE - Indicate idle-loop code that needs RCU readers
  138. * @a: Code that RCU needs to pay attention to.
  139. *
  140. * RCU read-side critical sections are forbidden in the inner idle loop,
  141. * that is, between the ct_idle_enter() and the ct_idle_exit() -- RCU
  142. * will happily ignore any such read-side critical sections. However,
  143. * things like powertop need tracepoints in the inner idle loop.
  144. *
  145. * This macro provides the way out: RCU_NONIDLE(do_something_with_RCU())
  146. * will tell RCU that it needs to pay attention, invoke its argument
  147. * (in this example, calling the do_something_with_RCU() function),
  148. * and then tell RCU to go back to ignoring this CPU. It is permissible
  149. * to nest RCU_NONIDLE() wrappers, but not indefinitely (but the limit is
  150. * on the order of a million or so, even on 32-bit systems). It is
  151. * not legal to block within RCU_NONIDLE(), nor is it permissible to
  152. * transfer control either into or out of RCU_NONIDLE()'s statement.
  153. */
  154. #define RCU_NONIDLE(a) \
  155. do { \
  156. ct_irq_enter_irqson(); \
  157. do { a; } while (0); \
  158. ct_irq_exit_irqson(); \
  159. } while (0)
  160. /*
  161. * Note a quasi-voluntary context switch for RCU-tasks's benefit.
  162. * This is a macro rather than an inline function to avoid #include hell.
  163. */
  164. #ifdef CONFIG_TASKS_RCU_GENERIC
  165. # ifdef CONFIG_TASKS_RCU
  166. # define rcu_tasks_classic_qs(t, preempt) \
  167. do { \
  168. if (!(preempt) && READ_ONCE((t)->rcu_tasks_holdout)) \
  169. WRITE_ONCE((t)->rcu_tasks_holdout, false); \
  170. } while (0)
  171. void call_rcu_tasks(struct rcu_head *head, rcu_callback_t func);
  172. void synchronize_rcu_tasks(void);
  173. # else
  174. # define rcu_tasks_classic_qs(t, preempt) do { } while (0)
  175. # define call_rcu_tasks call_rcu
  176. # define synchronize_rcu_tasks synchronize_rcu
  177. # endif
  178. # ifdef CONFIG_TASKS_TRACE_RCU
  179. // Bits for ->trc_reader_special.b.need_qs field.
  180. #define TRC_NEED_QS 0x1 // Task needs a quiescent state.
  181. #define TRC_NEED_QS_CHECKED 0x2 // Task has been checked for needing quiescent state.
  182. u8 rcu_trc_cmpxchg_need_qs(struct task_struct *t, u8 old, u8 new);
  183. void rcu_tasks_trace_qs_blkd(struct task_struct *t);
  184. # define rcu_tasks_trace_qs(t) \
  185. do { \
  186. int ___rttq_nesting = READ_ONCE((t)->trc_reader_nesting); \
  187. \
  188. if (likely(!READ_ONCE((t)->trc_reader_special.b.need_qs)) && \
  189. likely(!___rttq_nesting)) { \
  190. rcu_trc_cmpxchg_need_qs((t), 0, TRC_NEED_QS_CHECKED); \
  191. } else if (___rttq_nesting && ___rttq_nesting != INT_MIN && \
  192. !READ_ONCE((t)->trc_reader_special.b.blocked)) { \
  193. rcu_tasks_trace_qs_blkd(t); \
  194. } \
  195. } while (0)
  196. # else
  197. # define rcu_tasks_trace_qs(t) do { } while (0)
  198. # endif
  199. #define rcu_tasks_qs(t, preempt) \
  200. do { \
  201. rcu_tasks_classic_qs((t), (preempt)); \
  202. rcu_tasks_trace_qs(t); \
  203. } while (0)
  204. # ifdef CONFIG_TASKS_RUDE_RCU
  205. void call_rcu_tasks_rude(struct rcu_head *head, rcu_callback_t func);
  206. void synchronize_rcu_tasks_rude(void);
  207. # endif
  208. #define rcu_note_voluntary_context_switch(t) rcu_tasks_qs(t, false)
  209. void exit_tasks_rcu_start(void);
  210. void exit_tasks_rcu_stop(void);
  211. void exit_tasks_rcu_finish(void);
  212. #else /* #ifdef CONFIG_TASKS_RCU_GENERIC */
  213. #define rcu_tasks_classic_qs(t, preempt) do { } while (0)
  214. #define rcu_tasks_qs(t, preempt) do { } while (0)
  215. #define rcu_note_voluntary_context_switch(t) do { } while (0)
  216. #define call_rcu_tasks call_rcu
  217. #define synchronize_rcu_tasks synchronize_rcu
  218. static inline void exit_tasks_rcu_start(void) { }
  219. static inline void exit_tasks_rcu_stop(void) { }
  220. static inline void exit_tasks_rcu_finish(void) { }
  221. #endif /* #else #ifdef CONFIG_TASKS_RCU_GENERIC */
  222. /**
  223. * cond_resched_tasks_rcu_qs - Report potential quiescent states to RCU
  224. *
  225. * This macro resembles cond_resched(), except that it is defined to
  226. * report potential quiescent states to RCU-tasks even if the cond_resched()
  227. * machinery were to be shut off, as some advocate for PREEMPTION kernels.
  228. */
  229. #define cond_resched_tasks_rcu_qs() \
  230. do { \
  231. rcu_tasks_qs(current, false); \
  232. cond_resched(); \
  233. } while (0)
  234. /*
  235. * Infrastructure to implement the synchronize_() primitives in
  236. * TREE_RCU and rcu_barrier_() primitives in TINY_RCU.
  237. */
  238. #if defined(CONFIG_TREE_RCU)
  239. #include <linux/rcutree.h>
  240. #elif defined(CONFIG_TINY_RCU)
  241. #include <linux/rcutiny.h>
  242. #else
  243. #error "Unknown RCU implementation specified to kernel configuration"
  244. #endif
  245. /*
  246. * The init_rcu_head_on_stack() and destroy_rcu_head_on_stack() calls
  247. * are needed for dynamic initialization and destruction of rcu_head
  248. * on the stack, and init_rcu_head()/destroy_rcu_head() are needed for
  249. * dynamic initialization and destruction of statically allocated rcu_head
  250. * structures. However, rcu_head structures allocated dynamically in the
  251. * heap don't need any initialization.
  252. */
  253. #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD
  254. void init_rcu_head(struct rcu_head *head);
  255. void destroy_rcu_head(struct rcu_head *head);
  256. void init_rcu_head_on_stack(struct rcu_head *head);
  257. void destroy_rcu_head_on_stack(struct rcu_head *head);
  258. #else /* !CONFIG_DEBUG_OBJECTS_RCU_HEAD */
  259. static inline void init_rcu_head(struct rcu_head *head) { }
  260. static inline void destroy_rcu_head(struct rcu_head *head) { }
  261. static inline void init_rcu_head_on_stack(struct rcu_head *head) { }
  262. static inline void destroy_rcu_head_on_stack(struct rcu_head *head) { }
  263. #endif /* #else !CONFIG_DEBUG_OBJECTS_RCU_HEAD */
  264. #if defined(CONFIG_HOTPLUG_CPU) && defined(CONFIG_PROVE_RCU)
  265. bool rcu_lockdep_current_cpu_online(void);
  266. #else /* #if defined(CONFIG_HOTPLUG_CPU) && defined(CONFIG_PROVE_RCU) */
  267. static inline bool rcu_lockdep_current_cpu_online(void) { return true; }
  268. #endif /* #else #if defined(CONFIG_HOTPLUG_CPU) && defined(CONFIG_PROVE_RCU) */
  269. extern struct lockdep_map rcu_lock_map;
  270. extern struct lockdep_map rcu_bh_lock_map;
  271. extern struct lockdep_map rcu_sched_lock_map;
  272. extern struct lockdep_map rcu_callback_map;
  273. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  274. static inline void rcu_lock_acquire(struct lockdep_map *map)
  275. {
  276. lock_acquire(map, 0, 0, 2, 0, NULL, _THIS_IP_);
  277. }
  278. static inline void rcu_lock_release(struct lockdep_map *map)
  279. {
  280. lock_release(map, _THIS_IP_);
  281. }
  282. int debug_lockdep_rcu_enabled(void);
  283. int rcu_read_lock_held(void);
  284. int rcu_read_lock_bh_held(void);
  285. int rcu_read_lock_sched_held(void);
  286. int rcu_read_lock_any_held(void);
  287. #else /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
  288. # define rcu_lock_acquire(a) do { } while (0)
  289. # define rcu_lock_release(a) do { } while (0)
  290. static inline int rcu_read_lock_held(void)
  291. {
  292. return 1;
  293. }
  294. static inline int rcu_read_lock_bh_held(void)
  295. {
  296. return 1;
  297. }
  298. static inline int rcu_read_lock_sched_held(void)
  299. {
  300. return !preemptible();
  301. }
  302. static inline int rcu_read_lock_any_held(void)
  303. {
  304. return !preemptible();
  305. }
  306. #endif /* #else #ifdef CONFIG_DEBUG_LOCK_ALLOC */
  307. #ifdef CONFIG_PROVE_RCU
  308. /**
  309. * RCU_LOCKDEP_WARN - emit lockdep splat if specified condition is met
  310. * @c: condition to check
  311. * @s: informative message
  312. *
  313. * This checks debug_lockdep_rcu_enabled() before checking (c) to
  314. * prevent early boot splats due to lockdep not yet being initialized,
  315. * and rechecks it after checking (c) to prevent false-positive splats
  316. * due to races with lockdep being disabled. See commit 3066820034b5dd
  317. * ("rcu: Reject RCU_LOCKDEP_WARN() false positives") for more detail.
  318. */
  319. #define RCU_LOCKDEP_WARN(c, s) \
  320. do { \
  321. static bool __section(".data.unlikely") __warned; \
  322. if (debug_lockdep_rcu_enabled() && (c) && \
  323. debug_lockdep_rcu_enabled() && !__warned) { \
  324. __warned = true; \
  325. lockdep_rcu_suspicious(__FILE__, __LINE__, s); \
  326. } \
  327. } while (0)
  328. #if defined(CONFIG_PROVE_RCU) && !defined(CONFIG_PREEMPT_RCU)
  329. static inline void rcu_preempt_sleep_check(void)
  330. {
  331. RCU_LOCKDEP_WARN(lock_is_held(&rcu_lock_map),
  332. "Illegal context switch in RCU read-side critical section");
  333. }
  334. #else /* #ifdef CONFIG_PROVE_RCU */
  335. static inline void rcu_preempt_sleep_check(void) { }
  336. #endif /* #else #ifdef CONFIG_PROVE_RCU */
  337. #define rcu_sleep_check() \
  338. do { \
  339. rcu_preempt_sleep_check(); \
  340. if (!IS_ENABLED(CONFIG_PREEMPT_RT)) \
  341. RCU_LOCKDEP_WARN(lock_is_held(&rcu_bh_lock_map), \
  342. "Illegal context switch in RCU-bh read-side critical section"); \
  343. RCU_LOCKDEP_WARN(lock_is_held(&rcu_sched_lock_map), \
  344. "Illegal context switch in RCU-sched read-side critical section"); \
  345. } while (0)
  346. #else /* #ifdef CONFIG_PROVE_RCU */
  347. #define RCU_LOCKDEP_WARN(c, s) do { } while (0 && (c))
  348. #define rcu_sleep_check() do { } while (0)
  349. #endif /* #else #ifdef CONFIG_PROVE_RCU */
  350. /*
  351. * Helper functions for rcu_dereference_check(), rcu_dereference_protected()
  352. * and rcu_assign_pointer(). Some of these could be folded into their
  353. * callers, but they are left separate in order to ease introduction of
  354. * multiple pointers markings to match different RCU implementations
  355. * (e.g., __srcu), should this make sense in the future.
  356. */
  357. #ifdef __CHECKER__
  358. #define rcu_check_sparse(p, space) \
  359. ((void)(((typeof(*p) space *)p) == p))
  360. #else /* #ifdef __CHECKER__ */
  361. #define rcu_check_sparse(p, space)
  362. #endif /* #else #ifdef __CHECKER__ */
  363. #define __unrcu_pointer(p, local) \
  364. ({ \
  365. typeof(*p) *local = (typeof(*p) *__force)(p); \
  366. rcu_check_sparse(p, __rcu); \
  367. ((typeof(*p) __force __kernel *)(local)); \
  368. })
  369. /**
  370. * unrcu_pointer - mark a pointer as not being RCU protected
  371. * @p: pointer needing to lose its __rcu property
  372. *
  373. * Converts @p from an __rcu pointer to a __kernel pointer.
  374. * This allows an __rcu pointer to be used with xchg() and friends.
  375. */
  376. #define unrcu_pointer(p) __unrcu_pointer(p, __UNIQUE_ID(rcu))
  377. #define __rcu_access_pointer(p, local, space) \
  378. ({ \
  379. typeof(*p) *local = (typeof(*p) *__force)READ_ONCE(p); \
  380. rcu_check_sparse(p, space); \
  381. ((typeof(*p) __force __kernel *)(local)); \
  382. })
  383. #define __rcu_dereference_check(p, local, c, space) \
  384. ({ \
  385. /* Dependency order vs. p above. */ \
  386. typeof(*p) *local = (typeof(*p) *__force)READ_ONCE(p); \
  387. RCU_LOCKDEP_WARN(!(c), "suspicious rcu_dereference_check() usage"); \
  388. rcu_check_sparse(p, space); \
  389. ((typeof(*p) __force __kernel *)(local)); \
  390. })
  391. #define __rcu_dereference_protected(p, local, c, space) \
  392. ({ \
  393. RCU_LOCKDEP_WARN(!(c), "suspicious rcu_dereference_protected() usage"); \
  394. rcu_check_sparse(p, space); \
  395. ((typeof(*p) __force __kernel *)(p)); \
  396. })
  397. #define __rcu_dereference_raw(p, local) \
  398. ({ \
  399. /* Dependency order vs. p above. */ \
  400. typeof(p) local = READ_ONCE(p); \
  401. ((typeof(*p) __force __kernel *)(local)); \
  402. })
  403. #define rcu_dereference_raw(p) __rcu_dereference_raw(p, __UNIQUE_ID(rcu))
  404. /**
  405. * RCU_INITIALIZER() - statically initialize an RCU-protected global variable
  406. * @v: The value to statically initialize with.
  407. */
  408. #define RCU_INITIALIZER(v) (typeof(*(v)) __force __rcu *)(v)
  409. /**
  410. * rcu_assign_pointer() - assign to RCU-protected pointer
  411. * @p: pointer to assign to
  412. * @v: value to assign (publish)
  413. *
  414. * Assigns the specified value to the specified RCU-protected
  415. * pointer, ensuring that any concurrent RCU readers will see
  416. * any prior initialization.
  417. *
  418. * Inserts memory barriers on architectures that require them
  419. * (which is most of them), and also prevents the compiler from
  420. * reordering the code that initializes the structure after the pointer
  421. * assignment. More importantly, this call documents which pointers
  422. * will be dereferenced by RCU read-side code.
  423. *
  424. * In some special cases, you may use RCU_INIT_POINTER() instead
  425. * of rcu_assign_pointer(). RCU_INIT_POINTER() is a bit faster due
  426. * to the fact that it does not constrain either the CPU or the compiler.
  427. * That said, using RCU_INIT_POINTER() when you should have used
  428. * rcu_assign_pointer() is a very bad thing that results in
  429. * impossible-to-diagnose memory corruption. So please be careful.
  430. * See the RCU_INIT_POINTER() comment header for details.
  431. *
  432. * Note that rcu_assign_pointer() evaluates each of its arguments only
  433. * once, appearances notwithstanding. One of the "extra" evaluations
  434. * is in typeof() and the other visible only to sparse (__CHECKER__),
  435. * neither of which actually execute the argument. As with most cpp
  436. * macros, this execute-arguments-only-once property is important, so
  437. * please be careful when making changes to rcu_assign_pointer() and the
  438. * other macros that it invokes.
  439. */
  440. #define rcu_assign_pointer(p, v) \
  441. do { \
  442. uintptr_t _r_a_p__v = (uintptr_t)(v); \
  443. rcu_check_sparse(p, __rcu); \
  444. \
  445. if (__builtin_constant_p(v) && (_r_a_p__v) == (uintptr_t)NULL) \
  446. WRITE_ONCE((p), (typeof(p))(_r_a_p__v)); \
  447. else \
  448. smp_store_release(&p, RCU_INITIALIZER((typeof(p))_r_a_p__v)); \
  449. } while (0)
  450. /**
  451. * rcu_replace_pointer() - replace an RCU pointer, returning its old value
  452. * @rcu_ptr: RCU pointer, whose old value is returned
  453. * @ptr: regular pointer
  454. * @c: the lockdep conditions under which the dereference will take place
  455. *
  456. * Perform a replacement, where @rcu_ptr is an RCU-annotated
  457. * pointer and @c is the lockdep argument that is passed to the
  458. * rcu_dereference_protected() call used to read that pointer. The old
  459. * value of @rcu_ptr is returned, and @rcu_ptr is set to @ptr.
  460. */
  461. #define rcu_replace_pointer(rcu_ptr, ptr, c) \
  462. ({ \
  463. typeof(ptr) __tmp = rcu_dereference_protected((rcu_ptr), (c)); \
  464. rcu_assign_pointer((rcu_ptr), (ptr)); \
  465. __tmp; \
  466. })
  467. /**
  468. * rcu_access_pointer() - fetch RCU pointer with no dereferencing
  469. * @p: The pointer to read
  470. *
  471. * Return the value of the specified RCU-protected pointer, but omit the
  472. * lockdep checks for being in an RCU read-side critical section. This is
  473. * useful when the value of this pointer is accessed, but the pointer is
  474. * not dereferenced, for example, when testing an RCU-protected pointer
  475. * against NULL. Although rcu_access_pointer() may also be used in cases
  476. * where update-side locks prevent the value of the pointer from changing,
  477. * you should instead use rcu_dereference_protected() for this use case.
  478. * Within an RCU read-side critical section, there is little reason to
  479. * use rcu_access_pointer().
  480. *
  481. * It is usually best to test the rcu_access_pointer() return value
  482. * directly in order to avoid accidental dereferences being introduced
  483. * by later inattentive changes. In other words, assigning the
  484. * rcu_access_pointer() return value to a local variable results in an
  485. * accident waiting to happen.
  486. *
  487. * It is also permissible to use rcu_access_pointer() when read-side
  488. * access to the pointer was removed at least one grace period ago, as is
  489. * the case in the context of the RCU callback that is freeing up the data,
  490. * or after a synchronize_rcu() returns. This can be useful when tearing
  491. * down multi-linked structures after a grace period has elapsed. However,
  492. * rcu_dereference_protected() is normally preferred for this use case.
  493. */
  494. #define rcu_access_pointer(p) __rcu_access_pointer((p), __UNIQUE_ID(rcu), __rcu)
  495. /**
  496. * rcu_dereference_check() - rcu_dereference with debug checking
  497. * @p: The pointer to read, prior to dereferencing
  498. * @c: The conditions under which the dereference will take place
  499. *
  500. * Do an rcu_dereference(), but check that the conditions under which the
  501. * dereference will take place are correct. Typically the conditions
  502. * indicate the various locking conditions that should be held at that
  503. * point. The check should return true if the conditions are satisfied.
  504. * An implicit check for being in an RCU read-side critical section
  505. * (rcu_read_lock()) is included.
  506. *
  507. * For example:
  508. *
  509. * bar = rcu_dereference_check(foo->bar, lockdep_is_held(&foo->lock));
  510. *
  511. * could be used to indicate to lockdep that foo->bar may only be dereferenced
  512. * if either rcu_read_lock() is held, or that the lock required to replace
  513. * the bar struct at foo->bar is held.
  514. *
  515. * Note that the list of conditions may also include indications of when a lock
  516. * need not be held, for example during initialisation or destruction of the
  517. * target struct:
  518. *
  519. * bar = rcu_dereference_check(foo->bar, lockdep_is_held(&foo->lock) ||
  520. * atomic_read(&foo->usage) == 0);
  521. *
  522. * Inserts memory barriers on architectures that require them
  523. * (currently only the Alpha), prevents the compiler from refetching
  524. * (and from merging fetches), and, more importantly, documents exactly
  525. * which pointers are protected by RCU and checks that the pointer is
  526. * annotated as __rcu.
  527. */
  528. #define rcu_dereference_check(p, c) \
  529. __rcu_dereference_check((p), __UNIQUE_ID(rcu), \
  530. (c) || rcu_read_lock_held(), __rcu)
  531. /**
  532. * rcu_dereference_bh_check() - rcu_dereference_bh with debug checking
  533. * @p: The pointer to read, prior to dereferencing
  534. * @c: The conditions under which the dereference will take place
  535. *
  536. * This is the RCU-bh counterpart to rcu_dereference_check(). However,
  537. * please note that starting in v5.0 kernels, vanilla RCU grace periods
  538. * wait for local_bh_disable() regions of code in addition to regions of
  539. * code demarked by rcu_read_lock() and rcu_read_unlock(). This means
  540. * that synchronize_rcu(), call_rcu, and friends all take not only
  541. * rcu_read_lock() but also rcu_read_lock_bh() into account.
  542. */
  543. #define rcu_dereference_bh_check(p, c) \
  544. __rcu_dereference_check((p), __UNIQUE_ID(rcu), \
  545. (c) || rcu_read_lock_bh_held(), __rcu)
  546. /**
  547. * rcu_dereference_sched_check() - rcu_dereference_sched with debug checking
  548. * @p: The pointer to read, prior to dereferencing
  549. * @c: The conditions under which the dereference will take place
  550. *
  551. * This is the RCU-sched counterpart to rcu_dereference_check().
  552. * However, please note that starting in v5.0 kernels, vanilla RCU grace
  553. * periods wait for preempt_disable() regions of code in addition to
  554. * regions of code demarked by rcu_read_lock() and rcu_read_unlock().
  555. * This means that synchronize_rcu(), call_rcu, and friends all take not
  556. * only rcu_read_lock() but also rcu_read_lock_sched() into account.
  557. */
  558. #define rcu_dereference_sched_check(p, c) \
  559. __rcu_dereference_check((p), __UNIQUE_ID(rcu), \
  560. (c) || rcu_read_lock_sched_held(), \
  561. __rcu)
  562. /*
  563. * The tracing infrastructure traces RCU (we want that), but unfortunately
  564. * some of the RCU checks causes tracing to lock up the system.
  565. *
  566. * The no-tracing version of rcu_dereference_raw() must not call
  567. * rcu_read_lock_held().
  568. */
  569. #define rcu_dereference_raw_check(p) \
  570. __rcu_dereference_check((p), __UNIQUE_ID(rcu), 1, __rcu)
  571. /**
  572. * rcu_dereference_protected() - fetch RCU pointer when updates prevented
  573. * @p: The pointer to read, prior to dereferencing
  574. * @c: The conditions under which the dereference will take place
  575. *
  576. * Return the value of the specified RCU-protected pointer, but omit
  577. * the READ_ONCE(). This is useful in cases where update-side locks
  578. * prevent the value of the pointer from changing. Please note that this
  579. * primitive does *not* prevent the compiler from repeating this reference
  580. * or combining it with other references, so it should not be used without
  581. * protection of appropriate locks.
  582. *
  583. * This function is only for update-side use. Using this function
  584. * when protected only by rcu_read_lock() will result in infrequent
  585. * but very ugly failures.
  586. */
  587. #define rcu_dereference_protected(p, c) \
  588. __rcu_dereference_protected((p), __UNIQUE_ID(rcu), (c), __rcu)
  589. /**
  590. * rcu_dereference() - fetch RCU-protected pointer for dereferencing
  591. * @p: The pointer to read, prior to dereferencing
  592. *
  593. * This is a simple wrapper around rcu_dereference_check().
  594. */
  595. #define rcu_dereference(p) rcu_dereference_check(p, 0)
  596. /**
  597. * rcu_dereference_bh() - fetch an RCU-bh-protected pointer for dereferencing
  598. * @p: The pointer to read, prior to dereferencing
  599. *
  600. * Makes rcu_dereference_check() do the dirty work.
  601. */
  602. #define rcu_dereference_bh(p) rcu_dereference_bh_check(p, 0)
  603. /**
  604. * rcu_dereference_sched() - fetch RCU-sched-protected pointer for dereferencing
  605. * @p: The pointer to read, prior to dereferencing
  606. *
  607. * Makes rcu_dereference_check() do the dirty work.
  608. */
  609. #define rcu_dereference_sched(p) rcu_dereference_sched_check(p, 0)
  610. /**
  611. * rcu_pointer_handoff() - Hand off a pointer from RCU to other mechanism
  612. * @p: The pointer to hand off
  613. *
  614. * This is simply an identity function, but it documents where a pointer
  615. * is handed off from RCU to some other synchronization mechanism, for
  616. * example, reference counting or locking. In C11, it would map to
  617. * kill_dependency(). It could be used as follows::
  618. *
  619. * rcu_read_lock();
  620. * p = rcu_dereference(gp);
  621. * long_lived = is_long_lived(p);
  622. * if (long_lived) {
  623. * if (!atomic_inc_not_zero(p->refcnt))
  624. * long_lived = false;
  625. * else
  626. * p = rcu_pointer_handoff(p);
  627. * }
  628. * rcu_read_unlock();
  629. */
  630. #define rcu_pointer_handoff(p) (p)
  631. /**
  632. * rcu_read_lock() - mark the beginning of an RCU read-side critical section
  633. *
  634. * When synchronize_rcu() is invoked on one CPU while other CPUs
  635. * are within RCU read-side critical sections, then the
  636. * synchronize_rcu() is guaranteed to block until after all the other
  637. * CPUs exit their critical sections. Similarly, if call_rcu() is invoked
  638. * on one CPU while other CPUs are within RCU read-side critical
  639. * sections, invocation of the corresponding RCU callback is deferred
  640. * until after the all the other CPUs exit their critical sections.
  641. *
  642. * In v5.0 and later kernels, synchronize_rcu() and call_rcu() also
  643. * wait for regions of code with preemption disabled, including regions of
  644. * code with interrupts or softirqs disabled. In pre-v5.0 kernels, which
  645. * define synchronize_sched(), only code enclosed within rcu_read_lock()
  646. * and rcu_read_unlock() are guaranteed to be waited for.
  647. *
  648. * Note, however, that RCU callbacks are permitted to run concurrently
  649. * with new RCU read-side critical sections. One way that this can happen
  650. * is via the following sequence of events: (1) CPU 0 enters an RCU
  651. * read-side critical section, (2) CPU 1 invokes call_rcu() to register
  652. * an RCU callback, (3) CPU 0 exits the RCU read-side critical section,
  653. * (4) CPU 2 enters a RCU read-side critical section, (5) the RCU
  654. * callback is invoked. This is legal, because the RCU read-side critical
  655. * section that was running concurrently with the call_rcu() (and which
  656. * therefore might be referencing something that the corresponding RCU
  657. * callback would free up) has completed before the corresponding
  658. * RCU callback is invoked.
  659. *
  660. * RCU read-side critical sections may be nested. Any deferred actions
  661. * will be deferred until the outermost RCU read-side critical section
  662. * completes.
  663. *
  664. * You can avoid reading and understanding the next paragraph by
  665. * following this rule: don't put anything in an rcu_read_lock() RCU
  666. * read-side critical section that would block in a !PREEMPTION kernel.
  667. * But if you want the full story, read on!
  668. *
  669. * In non-preemptible RCU implementations (pure TREE_RCU and TINY_RCU),
  670. * it is illegal to block while in an RCU read-side critical section.
  671. * In preemptible RCU implementations (PREEMPT_RCU) in CONFIG_PREEMPTION
  672. * kernel builds, RCU read-side critical sections may be preempted,
  673. * but explicit blocking is illegal. Finally, in preemptible RCU
  674. * implementations in real-time (with -rt patchset) kernel builds, RCU
  675. * read-side critical sections may be preempted and they may also block, but
  676. * only when acquiring spinlocks that are subject to priority inheritance.
  677. */
  678. static __always_inline void rcu_read_lock(void)
  679. {
  680. __rcu_read_lock();
  681. __acquire(RCU);
  682. rcu_lock_acquire(&rcu_lock_map);
  683. RCU_LOCKDEP_WARN(!rcu_is_watching(),
  684. "rcu_read_lock() used illegally while idle");
  685. }
  686. /*
  687. * So where is rcu_write_lock()? It does not exist, as there is no
  688. * way for writers to lock out RCU readers. This is a feature, not
  689. * a bug -- this property is what provides RCU's performance benefits.
  690. * Of course, writers must coordinate with each other. The normal
  691. * spinlock primitives work well for this, but any other technique may be
  692. * used as well. RCU does not care how the writers keep out of each
  693. * others' way, as long as they do so.
  694. */
  695. /**
  696. * rcu_read_unlock() - marks the end of an RCU read-side critical section.
  697. *
  698. * In almost all situations, rcu_read_unlock() is immune from deadlock.
  699. * In recent kernels that have consolidated synchronize_sched() and
  700. * synchronize_rcu_bh() into synchronize_rcu(), this deadlock immunity
  701. * also extends to the scheduler's runqueue and priority-inheritance
  702. * spinlocks, courtesy of the quiescent-state deferral that is carried
  703. * out when rcu_read_unlock() is invoked with interrupts disabled.
  704. *
  705. * See rcu_read_lock() for more information.
  706. */
  707. static inline void rcu_read_unlock(void)
  708. {
  709. RCU_LOCKDEP_WARN(!rcu_is_watching(),
  710. "rcu_read_unlock() used illegally while idle");
  711. __release(RCU);
  712. __rcu_read_unlock();
  713. rcu_lock_release(&rcu_lock_map); /* Keep acq info for rls diags. */
  714. }
  715. /**
  716. * rcu_read_lock_bh() - mark the beginning of an RCU-bh critical section
  717. *
  718. * This is equivalent to rcu_read_lock(), but also disables softirqs.
  719. * Note that anything else that disables softirqs can also serve as an RCU
  720. * read-side critical section. However, please note that this equivalence
  721. * applies only to v5.0 and later. Before v5.0, rcu_read_lock() and
  722. * rcu_read_lock_bh() were unrelated.
  723. *
  724. * Note that rcu_read_lock_bh() and the matching rcu_read_unlock_bh()
  725. * must occur in the same context, for example, it is illegal to invoke
  726. * rcu_read_unlock_bh() from one task if the matching rcu_read_lock_bh()
  727. * was invoked from some other task.
  728. */
  729. static inline void rcu_read_lock_bh(void)
  730. {
  731. local_bh_disable();
  732. __acquire(RCU_BH);
  733. rcu_lock_acquire(&rcu_bh_lock_map);
  734. RCU_LOCKDEP_WARN(!rcu_is_watching(),
  735. "rcu_read_lock_bh() used illegally while idle");
  736. }
  737. /**
  738. * rcu_read_unlock_bh() - marks the end of a softirq-only RCU critical section
  739. *
  740. * See rcu_read_lock_bh() for more information.
  741. */
  742. static inline void rcu_read_unlock_bh(void)
  743. {
  744. RCU_LOCKDEP_WARN(!rcu_is_watching(),
  745. "rcu_read_unlock_bh() used illegally while idle");
  746. rcu_lock_release(&rcu_bh_lock_map);
  747. __release(RCU_BH);
  748. local_bh_enable();
  749. }
  750. /**
  751. * rcu_read_lock_sched() - mark the beginning of a RCU-sched critical section
  752. *
  753. * This is equivalent to rcu_read_lock(), but also disables preemption.
  754. * Read-side critical sections can also be introduced by anything else that
  755. * disables preemption, including local_irq_disable() and friends. However,
  756. * please note that the equivalence to rcu_read_lock() applies only to
  757. * v5.0 and later. Before v5.0, rcu_read_lock() and rcu_read_lock_sched()
  758. * were unrelated.
  759. *
  760. * Note that rcu_read_lock_sched() and the matching rcu_read_unlock_sched()
  761. * must occur in the same context, for example, it is illegal to invoke
  762. * rcu_read_unlock_sched() from process context if the matching
  763. * rcu_read_lock_sched() was invoked from an NMI handler.
  764. */
  765. static inline void rcu_read_lock_sched(void)
  766. {
  767. preempt_disable();
  768. __acquire(RCU_SCHED);
  769. rcu_lock_acquire(&rcu_sched_lock_map);
  770. RCU_LOCKDEP_WARN(!rcu_is_watching(),
  771. "rcu_read_lock_sched() used illegally while idle");
  772. }
  773. /* Used by lockdep and tracing: cannot be traced, cannot call lockdep. */
  774. static inline notrace void rcu_read_lock_sched_notrace(void)
  775. {
  776. preempt_disable_notrace();
  777. __acquire(RCU_SCHED);
  778. }
  779. /**
  780. * rcu_read_unlock_sched() - marks the end of a RCU-classic critical section
  781. *
  782. * See rcu_read_lock_sched() for more information.
  783. */
  784. static inline void rcu_read_unlock_sched(void)
  785. {
  786. RCU_LOCKDEP_WARN(!rcu_is_watching(),
  787. "rcu_read_unlock_sched() used illegally while idle");
  788. rcu_lock_release(&rcu_sched_lock_map);
  789. __release(RCU_SCHED);
  790. preempt_enable();
  791. }
  792. /* Used by lockdep and tracing: cannot be traced, cannot call lockdep. */
  793. static inline notrace void rcu_read_unlock_sched_notrace(void)
  794. {
  795. __release(RCU_SCHED);
  796. preempt_enable_notrace();
  797. }
  798. /**
  799. * RCU_INIT_POINTER() - initialize an RCU protected pointer
  800. * @p: The pointer to be initialized.
  801. * @v: The value to initialized the pointer to.
  802. *
  803. * Initialize an RCU-protected pointer in special cases where readers
  804. * do not need ordering constraints on the CPU or the compiler. These
  805. * special cases are:
  806. *
  807. * 1. This use of RCU_INIT_POINTER() is NULLing out the pointer *or*
  808. * 2. The caller has taken whatever steps are required to prevent
  809. * RCU readers from concurrently accessing this pointer *or*
  810. * 3. The referenced data structure has already been exposed to
  811. * readers either at compile time or via rcu_assign_pointer() *and*
  812. *
  813. * a. You have not made *any* reader-visible changes to
  814. * this structure since then *or*
  815. * b. It is OK for readers accessing this structure from its
  816. * new location to see the old state of the structure. (For
  817. * example, the changes were to statistical counters or to
  818. * other state where exact synchronization is not required.)
  819. *
  820. * Failure to follow these rules governing use of RCU_INIT_POINTER() will
  821. * result in impossible-to-diagnose memory corruption. As in the structures
  822. * will look OK in crash dumps, but any concurrent RCU readers might
  823. * see pre-initialized values of the referenced data structure. So
  824. * please be very careful how you use RCU_INIT_POINTER()!!!
  825. *
  826. * If you are creating an RCU-protected linked structure that is accessed
  827. * by a single external-to-structure RCU-protected pointer, then you may
  828. * use RCU_INIT_POINTER() to initialize the internal RCU-protected
  829. * pointers, but you must use rcu_assign_pointer() to initialize the
  830. * external-to-structure pointer *after* you have completely initialized
  831. * the reader-accessible portions of the linked structure.
  832. *
  833. * Note that unlike rcu_assign_pointer(), RCU_INIT_POINTER() provides no
  834. * ordering guarantees for either the CPU or the compiler.
  835. */
  836. #define RCU_INIT_POINTER(p, v) \
  837. do { \
  838. rcu_check_sparse(p, __rcu); \
  839. WRITE_ONCE(p, RCU_INITIALIZER(v)); \
  840. } while (0)
  841. /**
  842. * RCU_POINTER_INITIALIZER() - statically initialize an RCU protected pointer
  843. * @p: The pointer to be initialized.
  844. * @v: The value to initialized the pointer to.
  845. *
  846. * GCC-style initialization for an RCU-protected pointer in a structure field.
  847. */
  848. #define RCU_POINTER_INITIALIZER(p, v) \
  849. .p = RCU_INITIALIZER(v)
  850. /*
  851. * Does the specified offset indicate that the corresponding rcu_head
  852. * structure can be handled by kvfree_rcu()?
  853. */
  854. #define __is_kvfree_rcu_offset(offset) ((offset) < 4096)
  855. /**
  856. * kfree_rcu() - kfree an object after a grace period.
  857. * @ptr: pointer to kfree for both single- and double-argument invocations.
  858. * @rhf: the name of the struct rcu_head within the type of @ptr,
  859. * but only for double-argument invocations.
  860. *
  861. * Many rcu callbacks functions just call kfree() on the base structure.
  862. * These functions are trivial, but their size adds up, and furthermore
  863. * when they are used in a kernel module, that module must invoke the
  864. * high-latency rcu_barrier() function at module-unload time.
  865. *
  866. * The kfree_rcu() function handles this issue. Rather than encoding a
  867. * function address in the embedded rcu_head structure, kfree_rcu() instead
  868. * encodes the offset of the rcu_head structure within the base structure.
  869. * Because the functions are not allowed in the low-order 4096 bytes of
  870. * kernel virtual memory, offsets up to 4095 bytes can be accommodated.
  871. * If the offset is larger than 4095 bytes, a compile-time error will
  872. * be generated in kvfree_rcu_arg_2(). If this error is triggered, you can
  873. * either fall back to use of call_rcu() or rearrange the structure to
  874. * position the rcu_head structure into the first 4096 bytes.
  875. *
  876. * Note that the allowable offset might decrease in the future, for example,
  877. * to allow something like kmem_cache_free_rcu().
  878. *
  879. * The BUILD_BUG_ON check must not involve any function calls, hence the
  880. * checks are done in macros here.
  881. */
  882. #define kfree_rcu(ptr, rhf...) kvfree_rcu(ptr, ## rhf)
  883. /**
  884. * kvfree_rcu() - kvfree an object after a grace period.
  885. *
  886. * This macro consists of one or two arguments and it is
  887. * based on whether an object is head-less or not. If it
  888. * has a head then a semantic stays the same as it used
  889. * to be before:
  890. *
  891. * kvfree_rcu(ptr, rhf);
  892. *
  893. * where @ptr is a pointer to kvfree(), @rhf is the name
  894. * of the rcu_head structure within the type of @ptr.
  895. *
  896. * When it comes to head-less variant, only one argument
  897. * is passed and that is just a pointer which has to be
  898. * freed after a grace period. Therefore the semantic is
  899. *
  900. * kvfree_rcu(ptr);
  901. *
  902. * where @ptr is the pointer to be freed by kvfree().
  903. *
  904. * Please note, head-less way of freeing is permitted to
  905. * use from a context that has to follow might_sleep()
  906. * annotation. Otherwise, please switch and embed the
  907. * rcu_head structure within the type of @ptr.
  908. */
  909. #define kvfree_rcu(...) KVFREE_GET_MACRO(__VA_ARGS__, \
  910. kvfree_rcu_arg_2, kvfree_rcu_arg_1)(__VA_ARGS__)
  911. #define KVFREE_GET_MACRO(_1, _2, NAME, ...) NAME
  912. #define kvfree_rcu_arg_2(ptr, rhf) \
  913. do { \
  914. typeof (ptr) ___p = (ptr); \
  915. \
  916. if (___p) { \
  917. BUILD_BUG_ON(!__is_kvfree_rcu_offset(offsetof(typeof(*(ptr)), rhf))); \
  918. kvfree_call_rcu(&((___p)->rhf), (rcu_callback_t)(unsigned long) \
  919. (offsetof(typeof(*(ptr)), rhf))); \
  920. } \
  921. } while (0)
  922. #define kvfree_rcu_arg_1(ptr) \
  923. do { \
  924. typeof(ptr) ___p = (ptr); \
  925. \
  926. if (___p) \
  927. kvfree_call_rcu(NULL, (rcu_callback_t) (___p)); \
  928. } while (0)
  929. /*
  930. * Place this after a lock-acquisition primitive to guarantee that
  931. * an UNLOCK+LOCK pair acts as a full barrier. This guarantee applies
  932. * if the UNLOCK and LOCK are executed by the same CPU or if the
  933. * UNLOCK and LOCK operate on the same lock variable.
  934. */
  935. #ifdef CONFIG_ARCH_WEAK_RELEASE_ACQUIRE
  936. #define smp_mb__after_unlock_lock() smp_mb() /* Full ordering for lock. */
  937. #else /* #ifdef CONFIG_ARCH_WEAK_RELEASE_ACQUIRE */
  938. #define smp_mb__after_unlock_lock() do { } while (0)
  939. #endif /* #else #ifdef CONFIG_ARCH_WEAK_RELEASE_ACQUIRE */
  940. /* Has the specified rcu_head structure been handed to call_rcu()? */
  941. /**
  942. * rcu_head_init - Initialize rcu_head for rcu_head_after_call_rcu()
  943. * @rhp: The rcu_head structure to initialize.
  944. *
  945. * If you intend to invoke rcu_head_after_call_rcu() to test whether a
  946. * given rcu_head structure has already been passed to call_rcu(), then
  947. * you must also invoke this rcu_head_init() function on it just after
  948. * allocating that structure. Calls to this function must not race with
  949. * calls to call_rcu(), rcu_head_after_call_rcu(), or callback invocation.
  950. */
  951. static inline void rcu_head_init(struct rcu_head *rhp)
  952. {
  953. rhp->func = (rcu_callback_t)~0L;
  954. }
  955. /**
  956. * rcu_head_after_call_rcu() - Has this rcu_head been passed to call_rcu()?
  957. * @rhp: The rcu_head structure to test.
  958. * @f: The function passed to call_rcu() along with @rhp.
  959. *
  960. * Returns @true if the @rhp has been passed to call_rcu() with @func,
  961. * and @false otherwise. Emits a warning in any other case, including
  962. * the case where @rhp has already been invoked after a grace period.
  963. * Calls to this function must not race with callback invocation. One way
  964. * to avoid such races is to enclose the call to rcu_head_after_call_rcu()
  965. * in an RCU read-side critical section that includes a read-side fetch
  966. * of the pointer to the structure containing @rhp.
  967. */
  968. static inline bool
  969. rcu_head_after_call_rcu(struct rcu_head *rhp, rcu_callback_t f)
  970. {
  971. rcu_callback_t func = READ_ONCE(rhp->func);
  972. if (func == f)
  973. return true;
  974. WARN_ON_ONCE(func != (rcu_callback_t)~0L);
  975. return false;
  976. }
  977. /* kernel/ksysfs.c definitions */
  978. extern int rcu_expedited;
  979. extern int rcu_normal;
  980. #endif /* __LINUX_RCUPDATE_H */