rcu.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. /*
  2. * Read-Copy Update definitions shared among RCU implementations.
  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, you can access it online at
  16. * http://www.gnu.org/licenses/gpl-2.0.html.
  17. *
  18. * Copyright IBM Corporation, 2011
  19. *
  20. * Author: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
  21. */
  22. #ifndef __LINUX_RCU_H
  23. #define __LINUX_RCU_H
  24. #include <trace/events/rcu.h>
  25. #ifdef CONFIG_RCU_TRACE
  26. #define RCU_TRACE(stmt) stmt
  27. #else /* #ifdef CONFIG_RCU_TRACE */
  28. #define RCU_TRACE(stmt)
  29. #endif /* #else #ifdef CONFIG_RCU_TRACE */
  30. /* Offset to allow for unmatched rcu_irq_{enter,exit}(). */
  31. #define DYNTICK_IRQ_NONIDLE ((LONG_MAX / 2) + 1)
  32. /*
  33. * Grace-period counter management.
  34. */
  35. #define RCU_SEQ_CTR_SHIFT 2
  36. #define RCU_SEQ_STATE_MASK ((1 << RCU_SEQ_CTR_SHIFT) - 1)
  37. /*
  38. * Return the counter portion of a sequence number previously returned
  39. * by rcu_seq_snap() or rcu_seq_current().
  40. */
  41. static inline unsigned long rcu_seq_ctr(unsigned long s)
  42. {
  43. return s >> RCU_SEQ_CTR_SHIFT;
  44. }
  45. /*
  46. * Return the state portion of a sequence number previously returned
  47. * by rcu_seq_snap() or rcu_seq_current().
  48. */
  49. static inline int rcu_seq_state(unsigned long s)
  50. {
  51. return s & RCU_SEQ_STATE_MASK;
  52. }
  53. /*
  54. * Set the state portion of the pointed-to sequence number.
  55. * The caller is responsible for preventing conflicting updates.
  56. */
  57. static inline void rcu_seq_set_state(unsigned long *sp, int newstate)
  58. {
  59. WARN_ON_ONCE(newstate & ~RCU_SEQ_STATE_MASK);
  60. WRITE_ONCE(*sp, (*sp & ~RCU_SEQ_STATE_MASK) + newstate);
  61. }
  62. /* Adjust sequence number for start of update-side operation. */
  63. static inline void rcu_seq_start(unsigned long *sp)
  64. {
  65. WRITE_ONCE(*sp, *sp + 1);
  66. smp_mb(); /* Ensure update-side operation after counter increment. */
  67. WARN_ON_ONCE(rcu_seq_state(*sp) != 1);
  68. }
  69. /* Adjust sequence number for end of update-side operation. */
  70. static inline void rcu_seq_end(unsigned long *sp)
  71. {
  72. smp_mb(); /* Ensure update-side operation before counter increment. */
  73. WARN_ON_ONCE(!rcu_seq_state(*sp));
  74. WRITE_ONCE(*sp, (*sp | RCU_SEQ_STATE_MASK) + 1);
  75. }
  76. /* Take a snapshot of the update side's sequence number. */
  77. static inline unsigned long rcu_seq_snap(unsigned long *sp)
  78. {
  79. unsigned long s;
  80. s = (READ_ONCE(*sp) + 2 * RCU_SEQ_STATE_MASK + 1) & ~RCU_SEQ_STATE_MASK;
  81. smp_mb(); /* Above access must not bleed into critical section. */
  82. return s;
  83. }
  84. /* Return the current value the update side's sequence number, no ordering. */
  85. static inline unsigned long rcu_seq_current(unsigned long *sp)
  86. {
  87. return READ_ONCE(*sp);
  88. }
  89. /*
  90. * Given a snapshot from rcu_seq_snap(), determine whether or not a
  91. * full update-side operation has occurred.
  92. */
  93. static inline bool rcu_seq_done(unsigned long *sp, unsigned long s)
  94. {
  95. return ULONG_CMP_GE(READ_ONCE(*sp), s);
  96. }
  97. /*
  98. * debug_rcu_head_queue()/debug_rcu_head_unqueue() are used internally
  99. * by call_rcu() and rcu callback execution, and are therefore not part of the
  100. * RCU API. Leaving in rcupdate.h because they are used by all RCU flavors.
  101. */
  102. #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD
  103. # define STATE_RCU_HEAD_READY 0
  104. # define STATE_RCU_HEAD_QUEUED 1
  105. extern struct debug_obj_descr rcuhead_debug_descr;
  106. static inline int debug_rcu_head_queue(struct rcu_head *head)
  107. {
  108. int r1;
  109. r1 = debug_object_activate(head, &rcuhead_debug_descr);
  110. debug_object_active_state(head, &rcuhead_debug_descr,
  111. STATE_RCU_HEAD_READY,
  112. STATE_RCU_HEAD_QUEUED);
  113. return r1;
  114. }
  115. static inline void debug_rcu_head_unqueue(struct rcu_head *head)
  116. {
  117. debug_object_active_state(head, &rcuhead_debug_descr,
  118. STATE_RCU_HEAD_QUEUED,
  119. STATE_RCU_HEAD_READY);
  120. debug_object_deactivate(head, &rcuhead_debug_descr);
  121. }
  122. #else /* !CONFIG_DEBUG_OBJECTS_RCU_HEAD */
  123. static inline int debug_rcu_head_queue(struct rcu_head *head)
  124. {
  125. return 0;
  126. }
  127. static inline void debug_rcu_head_unqueue(struct rcu_head *head)
  128. {
  129. }
  130. #endif /* #else !CONFIG_DEBUG_OBJECTS_RCU_HEAD */
  131. void kfree(const void *);
  132. /*
  133. * Reclaim the specified callback, either by invoking it (non-lazy case)
  134. * or freeing it directly (lazy case). Return true if lazy, false otherwise.
  135. */
  136. static inline bool __rcu_reclaim(const char *rn, struct rcu_head *head)
  137. {
  138. unsigned long offset = (unsigned long)head->func;
  139. rcu_lock_acquire(&rcu_callback_map);
  140. if (__is_kfree_rcu_offset(offset)) {
  141. RCU_TRACE(trace_rcu_invoke_kfree_callback(rn, head, offset);)
  142. kfree((void *)head - offset);
  143. rcu_lock_release(&rcu_callback_map);
  144. return true;
  145. } else {
  146. RCU_TRACE(trace_rcu_invoke_callback(rn, head);)
  147. head->func(head);
  148. rcu_lock_release(&rcu_callback_map);
  149. return false;
  150. }
  151. }
  152. #ifdef CONFIG_RCU_STALL_COMMON
  153. extern int rcu_cpu_stall_suppress;
  154. int rcu_jiffies_till_stall_check(void);
  155. #define rcu_ftrace_dump_stall_suppress() \
  156. do { \
  157. if (!rcu_cpu_stall_suppress) \
  158. rcu_cpu_stall_suppress = 3; \
  159. } while (0)
  160. #define rcu_ftrace_dump_stall_unsuppress() \
  161. do { \
  162. if (rcu_cpu_stall_suppress == 3) \
  163. rcu_cpu_stall_suppress = 0; \
  164. } while (0)
  165. #else /* #endif #ifdef CONFIG_RCU_STALL_COMMON */
  166. #define rcu_ftrace_dump_stall_suppress()
  167. #define rcu_ftrace_dump_stall_unsuppress()
  168. #endif /* #ifdef CONFIG_RCU_STALL_COMMON */
  169. /*
  170. * Strings used in tracepoints need to be exported via the
  171. * tracing system such that tools like perf and trace-cmd can
  172. * translate the string address pointers to actual text.
  173. */
  174. #define TPS(x) tracepoint_string(x)
  175. /*
  176. * Dump the ftrace buffer, but only one time per callsite per boot.
  177. */
  178. #define rcu_ftrace_dump(oops_dump_mode) \
  179. do { \
  180. static atomic_t ___rfd_beenhere = ATOMIC_INIT(0); \
  181. \
  182. if (!atomic_read(&___rfd_beenhere) && \
  183. !atomic_xchg(&___rfd_beenhere, 1)) { \
  184. tracing_off(); \
  185. rcu_ftrace_dump_stall_suppress(); \
  186. ftrace_dump(oops_dump_mode); \
  187. rcu_ftrace_dump_stall_unsuppress(); \
  188. } \
  189. } while (0)
  190. void rcu_early_boot_tests(void);
  191. void rcu_test_sync_prims(void);
  192. /*
  193. * This function really isn't for public consumption, but RCU is special in
  194. * that context switches can allow the state machine to make progress.
  195. */
  196. extern void resched_cpu(int cpu);
  197. #if defined(SRCU) || !defined(TINY_RCU)
  198. #include <linux/rcu_node_tree.h>
  199. extern int rcu_num_lvls;
  200. extern int num_rcu_lvl[];
  201. extern int rcu_num_nodes;
  202. static bool rcu_fanout_exact;
  203. static int rcu_fanout_leaf;
  204. /*
  205. * Compute the per-level fanout, either using the exact fanout specified
  206. * or balancing the tree, depending on the rcu_fanout_exact boot parameter.
  207. */
  208. static inline void rcu_init_levelspread(int *levelspread, const int *levelcnt)
  209. {
  210. int i;
  211. if (rcu_fanout_exact) {
  212. levelspread[rcu_num_lvls - 1] = rcu_fanout_leaf;
  213. for (i = rcu_num_lvls - 2; i >= 0; i--)
  214. levelspread[i] = RCU_FANOUT;
  215. } else {
  216. int ccur;
  217. int cprv;
  218. cprv = nr_cpu_ids;
  219. for (i = rcu_num_lvls - 1; i >= 0; i--) {
  220. ccur = levelcnt[i];
  221. levelspread[i] = (cprv + ccur - 1) / ccur;
  222. cprv = ccur;
  223. }
  224. }
  225. }
  226. /*
  227. * Do a full breadth-first scan of the rcu_node structures for the
  228. * specified rcu_state structure.
  229. */
  230. #define rcu_for_each_node_breadth_first(rsp, rnp) \
  231. for ((rnp) = &(rsp)->node[0]; \
  232. (rnp) < &(rsp)->node[rcu_num_nodes]; (rnp)++)
  233. /*
  234. * Do a breadth-first scan of the non-leaf rcu_node structures for the
  235. * specified rcu_state structure. Note that if there is a singleton
  236. * rcu_node tree with but one rcu_node structure, this loop is a no-op.
  237. */
  238. #define rcu_for_each_nonleaf_node_breadth_first(rsp, rnp) \
  239. for ((rnp) = &(rsp)->node[0]; \
  240. (rnp) < (rsp)->level[rcu_num_lvls - 1]; (rnp)++)
  241. /*
  242. * Scan the leaves of the rcu_node hierarchy for the specified rcu_state
  243. * structure. Note that if there is a singleton rcu_node tree with but
  244. * one rcu_node structure, this loop -will- visit the rcu_node structure.
  245. * It is still a leaf node, even if it is also the root node.
  246. */
  247. #define rcu_for_each_leaf_node(rsp, rnp) \
  248. for ((rnp) = (rsp)->level[rcu_num_lvls - 1]; \
  249. (rnp) < &(rsp)->node[rcu_num_nodes]; (rnp)++)
  250. /*
  251. * Iterate over all possible CPUs in a leaf RCU node.
  252. */
  253. #define for_each_leaf_node_possible_cpu(rnp, cpu) \
  254. for ((cpu) = cpumask_next(rnp->grplo - 1, cpu_possible_mask); \
  255. cpu <= rnp->grphi; \
  256. cpu = cpumask_next((cpu), cpu_possible_mask))
  257. /*
  258. * Wrappers for the rcu_node::lock acquire and release.
  259. *
  260. * Because the rcu_nodes form a tree, the tree traversal locking will observe
  261. * different lock values, this in turn means that an UNLOCK of one level
  262. * followed by a LOCK of another level does not imply a full memory barrier;
  263. * and most importantly transitivity is lost.
  264. *
  265. * In order to restore full ordering between tree levels, augment the regular
  266. * lock acquire functions with smp_mb__after_unlock_lock().
  267. *
  268. * As ->lock of struct rcu_node is a __private field, therefore one should use
  269. * these wrappers rather than directly call raw_spin_{lock,unlock}* on ->lock.
  270. */
  271. #define raw_spin_lock_rcu_node(p) \
  272. do { \
  273. raw_spin_lock(&ACCESS_PRIVATE(p, lock)); \
  274. smp_mb__after_unlock_lock(); \
  275. } while (0)
  276. #define raw_spin_unlock_rcu_node(p) raw_spin_unlock(&ACCESS_PRIVATE(p, lock))
  277. #define raw_spin_lock_irq_rcu_node(p) \
  278. do { \
  279. raw_spin_lock_irq(&ACCESS_PRIVATE(p, lock)); \
  280. smp_mb__after_unlock_lock(); \
  281. } while (0)
  282. #define raw_spin_unlock_irq_rcu_node(p) \
  283. raw_spin_unlock_irq(&ACCESS_PRIVATE(p, lock))
  284. #define raw_spin_lock_irqsave_rcu_node(p, flags) \
  285. do { \
  286. raw_spin_lock_irqsave(&ACCESS_PRIVATE(p, lock), flags); \
  287. smp_mb__after_unlock_lock(); \
  288. } while (0)
  289. #define raw_spin_unlock_irqrestore_rcu_node(p, flags) \
  290. raw_spin_unlock_irqrestore(&ACCESS_PRIVATE(p, lock), flags)
  291. #define raw_spin_trylock_rcu_node(p) \
  292. ({ \
  293. bool ___locked = raw_spin_trylock(&ACCESS_PRIVATE(p, lock)); \
  294. \
  295. if (___locked) \
  296. smp_mb__after_unlock_lock(); \
  297. ___locked; \
  298. })
  299. #define raw_lockdep_assert_held_rcu_node(p) \
  300. lockdep_assert_held(&ACCESS_PRIVATE(p, lock))
  301. #endif /* #if defined(SRCU) || !defined(TINY_RCU) */
  302. #ifdef CONFIG_TINY_RCU
  303. /* Tiny RCU doesn't expedite, as its purpose in life is instead to be tiny. */
  304. static inline bool rcu_gp_is_normal(void) { return true; }
  305. static inline bool rcu_gp_is_expedited(void) { return false; }
  306. static inline void rcu_expedite_gp(void) { }
  307. static inline void rcu_unexpedite_gp(void) { }
  308. static inline void rcu_request_urgent_qs_task(struct task_struct *t) { }
  309. #else /* #ifdef CONFIG_TINY_RCU */
  310. bool rcu_gp_is_normal(void); /* Internal RCU use. */
  311. bool rcu_gp_is_expedited(void); /* Internal RCU use. */
  312. void rcu_expedite_gp(void);
  313. void rcu_unexpedite_gp(void);
  314. void rcupdate_announce_bootup_oddness(void);
  315. void rcu_request_urgent_qs_task(struct task_struct *t);
  316. #endif /* #else #ifdef CONFIG_TINY_RCU */
  317. #define RCU_SCHEDULER_INACTIVE 0
  318. #define RCU_SCHEDULER_INIT 1
  319. #define RCU_SCHEDULER_RUNNING 2
  320. enum rcutorture_type {
  321. RCU_FLAVOR,
  322. RCU_BH_FLAVOR,
  323. RCU_SCHED_FLAVOR,
  324. RCU_TASKS_FLAVOR,
  325. SRCU_FLAVOR,
  326. INVALID_RCU_FLAVOR
  327. };
  328. #if defined(CONFIG_TREE_RCU) || defined(CONFIG_PREEMPT_RCU)
  329. void rcutorture_get_gp_data(enum rcutorture_type test_type, int *flags,
  330. unsigned long *gpnum, unsigned long *completed);
  331. void rcutorture_record_test_transition(void);
  332. void rcutorture_record_progress(unsigned long vernum);
  333. void do_trace_rcu_torture_read(const char *rcutorturename,
  334. struct rcu_head *rhp,
  335. unsigned long secs,
  336. unsigned long c_old,
  337. unsigned long c);
  338. #else
  339. static inline void rcutorture_get_gp_data(enum rcutorture_type test_type,
  340. int *flags,
  341. unsigned long *gpnum,
  342. unsigned long *completed)
  343. {
  344. *flags = 0;
  345. *gpnum = 0;
  346. *completed = 0;
  347. }
  348. static inline void rcutorture_record_test_transition(void) { }
  349. static inline void rcutorture_record_progress(unsigned long vernum) { }
  350. #ifdef CONFIG_RCU_TRACE
  351. void do_trace_rcu_torture_read(const char *rcutorturename,
  352. struct rcu_head *rhp,
  353. unsigned long secs,
  354. unsigned long c_old,
  355. unsigned long c);
  356. #else
  357. #define do_trace_rcu_torture_read(rcutorturename, rhp, secs, c_old, c) \
  358. do { } while (0)
  359. #endif
  360. #endif
  361. #ifdef CONFIG_TINY_SRCU
  362. static inline void srcutorture_get_gp_data(enum rcutorture_type test_type,
  363. struct srcu_struct *sp, int *flags,
  364. unsigned long *gpnum,
  365. unsigned long *completed)
  366. {
  367. if (test_type != SRCU_FLAVOR)
  368. return;
  369. *flags = 0;
  370. *completed = sp->srcu_idx;
  371. *gpnum = *completed;
  372. }
  373. #elif defined(CONFIG_TREE_SRCU)
  374. void srcutorture_get_gp_data(enum rcutorture_type test_type,
  375. struct srcu_struct *sp, int *flags,
  376. unsigned long *gpnum, unsigned long *completed);
  377. #endif
  378. #ifdef CONFIG_TINY_RCU
  379. static inline unsigned long rcu_batches_started(void) { return 0; }
  380. static inline unsigned long rcu_batches_started_bh(void) { return 0; }
  381. static inline unsigned long rcu_batches_started_sched(void) { return 0; }
  382. static inline unsigned long rcu_batches_completed(void) { return 0; }
  383. static inline unsigned long rcu_batches_completed_bh(void) { return 0; }
  384. static inline unsigned long rcu_batches_completed_sched(void) { return 0; }
  385. static inline unsigned long rcu_exp_batches_completed(void) { return 0; }
  386. static inline unsigned long rcu_exp_batches_completed_sched(void) { return 0; }
  387. static inline unsigned long
  388. srcu_batches_completed(struct srcu_struct *sp) { return 0; }
  389. static inline void rcu_force_quiescent_state(void) { }
  390. static inline void rcu_bh_force_quiescent_state(void) { }
  391. static inline void rcu_sched_force_quiescent_state(void) { }
  392. static inline void show_rcu_gp_kthreads(void) { }
  393. #else /* #ifdef CONFIG_TINY_RCU */
  394. extern unsigned long rcutorture_testseq;
  395. extern unsigned long rcutorture_vernum;
  396. unsigned long rcu_batches_started(void);
  397. unsigned long rcu_batches_started_bh(void);
  398. unsigned long rcu_batches_started_sched(void);
  399. unsigned long rcu_batches_completed(void);
  400. unsigned long rcu_batches_completed_bh(void);
  401. unsigned long rcu_batches_completed_sched(void);
  402. unsigned long rcu_exp_batches_completed(void);
  403. unsigned long rcu_exp_batches_completed_sched(void);
  404. unsigned long srcu_batches_completed(struct srcu_struct *sp);
  405. void show_rcu_gp_kthreads(void);
  406. void rcu_force_quiescent_state(void);
  407. void rcu_bh_force_quiescent_state(void);
  408. void rcu_sched_force_quiescent_state(void);
  409. #endif /* #else #ifdef CONFIG_TINY_RCU */
  410. #ifdef CONFIG_RCU_NOCB_CPU
  411. bool rcu_is_nocb_cpu(int cpu);
  412. #else
  413. static inline bool rcu_is_nocb_cpu(int cpu) { return false; }
  414. #endif
  415. #endif /* __LINUX_RCU_H */