rcu.h 15 KB

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