rcu.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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. /* Returns first leaf rcu_node of the specified RCU flavor. */
  232. #define rcu_first_leaf_node(rsp) ((rsp)->level[rcu_num_lvls - 1])
  233. /* Is this rcu_node a leaf? */
  234. #define rcu_is_leaf_node(rnp) ((rnp)->level == rcu_num_lvls - 1)
  235. /*
  236. * Do a full breadth-first scan of the rcu_node structures for the
  237. * specified rcu_state structure.
  238. */
  239. #define rcu_for_each_node_breadth_first(rsp, rnp) \
  240. for ((rnp) = &(rsp)->node[0]; \
  241. (rnp) < &(rsp)->node[rcu_num_nodes]; (rnp)++)
  242. /*
  243. * Do a breadth-first scan of the non-leaf rcu_node structures for the
  244. * specified rcu_state structure. Note that if there is a singleton
  245. * rcu_node tree with but one rcu_node structure, this loop is a no-op.
  246. */
  247. #define rcu_for_each_nonleaf_node_breadth_first(rsp, rnp) \
  248. for ((rnp) = &(rsp)->node[0]; !rcu_is_leaf_node(rsp, rnp); (rnp)++)
  249. /*
  250. * Scan the leaves of the rcu_node hierarchy for the specified rcu_state
  251. * structure. Note that if there is a singleton rcu_node tree with but
  252. * one rcu_node structure, this loop -will- visit the rcu_node structure.
  253. * It is still a leaf node, even if it is also the root node.
  254. */
  255. #define rcu_for_each_leaf_node(rsp, rnp) \
  256. for ((rnp) = rcu_first_leaf_node(rsp); \
  257. (rnp) < &(rsp)->node[rcu_num_nodes]; (rnp)++)
  258. /*
  259. * Iterate over all possible CPUs in a leaf RCU node.
  260. */
  261. #define for_each_leaf_node_possible_cpu(rnp, cpu) \
  262. for ((cpu) = cpumask_next((rnp)->grplo - 1, cpu_possible_mask); \
  263. (cpu) <= rnp->grphi; \
  264. (cpu) = cpumask_next((cpu), cpu_possible_mask))
  265. /*
  266. * Iterate over all CPUs in a leaf RCU node's specified mask.
  267. */
  268. #define rcu_find_next_bit(rnp, cpu, mask) \
  269. ((rnp)->grplo + find_next_bit(&(mask), BITS_PER_LONG, (cpu)))
  270. #define for_each_leaf_node_cpu_mask(rnp, cpu, mask) \
  271. for ((cpu) = rcu_find_next_bit((rnp), 0, (mask)); \
  272. (cpu) <= rnp->grphi; \
  273. (cpu) = rcu_find_next_bit((rnp), (cpu) + 1 - (rnp->grplo), (mask)))
  274. /*
  275. * Wrappers for the rcu_node::lock acquire and release.
  276. *
  277. * Because the rcu_nodes form a tree, the tree traversal locking will observe
  278. * different lock values, this in turn means that an UNLOCK of one level
  279. * followed by a LOCK of another level does not imply a full memory barrier;
  280. * and most importantly transitivity is lost.
  281. *
  282. * In order to restore full ordering between tree levels, augment the regular
  283. * lock acquire functions with smp_mb__after_unlock_lock().
  284. *
  285. * As ->lock of struct rcu_node is a __private field, therefore one should use
  286. * these wrappers rather than directly call raw_spin_{lock,unlock}* on ->lock.
  287. */
  288. #define raw_spin_lock_rcu_node(p) \
  289. do { \
  290. raw_spin_lock(&ACCESS_PRIVATE(p, lock)); \
  291. smp_mb__after_unlock_lock(); \
  292. } while (0)
  293. #define raw_spin_unlock_rcu_node(p) raw_spin_unlock(&ACCESS_PRIVATE(p, lock))
  294. #define raw_spin_lock_irq_rcu_node(p) \
  295. do { \
  296. raw_spin_lock_irq(&ACCESS_PRIVATE(p, lock)); \
  297. smp_mb__after_unlock_lock(); \
  298. } while (0)
  299. #define raw_spin_unlock_irq_rcu_node(p) \
  300. raw_spin_unlock_irq(&ACCESS_PRIVATE(p, lock))
  301. #define raw_spin_lock_irqsave_rcu_node(p, flags) \
  302. do { \
  303. raw_spin_lock_irqsave(&ACCESS_PRIVATE(p, lock), flags); \
  304. smp_mb__after_unlock_lock(); \
  305. } while (0)
  306. #define raw_spin_unlock_irqrestore_rcu_node(p, flags) \
  307. raw_spin_unlock_irqrestore(&ACCESS_PRIVATE(p, lock), flags)
  308. #define raw_spin_trylock_rcu_node(p) \
  309. ({ \
  310. bool ___locked = raw_spin_trylock(&ACCESS_PRIVATE(p, lock)); \
  311. \
  312. if (___locked) \
  313. smp_mb__after_unlock_lock(); \
  314. ___locked; \
  315. })
  316. #define raw_lockdep_assert_held_rcu_node(p) \
  317. lockdep_assert_held(&ACCESS_PRIVATE(p, lock))
  318. #endif /* #if defined(SRCU) || !defined(TINY_RCU) */
  319. #ifdef CONFIG_TINY_RCU
  320. /* Tiny RCU doesn't expedite, as its purpose in life is instead to be tiny. */
  321. static inline bool rcu_gp_is_normal(void) { return true; }
  322. static inline bool rcu_gp_is_expedited(void) { return false; }
  323. static inline void rcu_expedite_gp(void) { }
  324. static inline void rcu_unexpedite_gp(void) { }
  325. static inline void rcu_request_urgent_qs_task(struct task_struct *t) { }
  326. #else /* #ifdef CONFIG_TINY_RCU */
  327. bool rcu_gp_is_normal(void); /* Internal RCU use. */
  328. bool rcu_gp_is_expedited(void); /* Internal RCU use. */
  329. void rcu_expedite_gp(void);
  330. void rcu_unexpedite_gp(void);
  331. void rcupdate_announce_bootup_oddness(void);
  332. void rcu_request_urgent_qs_task(struct task_struct *t);
  333. #endif /* #else #ifdef CONFIG_TINY_RCU */
  334. #define RCU_SCHEDULER_INACTIVE 0
  335. #define RCU_SCHEDULER_INIT 1
  336. #define RCU_SCHEDULER_RUNNING 2
  337. enum rcutorture_type {
  338. RCU_FLAVOR,
  339. RCU_BH_FLAVOR,
  340. RCU_SCHED_FLAVOR,
  341. RCU_TASKS_FLAVOR,
  342. SRCU_FLAVOR,
  343. INVALID_RCU_FLAVOR
  344. };
  345. #if defined(CONFIG_TREE_RCU) || defined(CONFIG_PREEMPT_RCU)
  346. void rcutorture_get_gp_data(enum rcutorture_type test_type, int *flags,
  347. unsigned long *gpnum, unsigned long *completed);
  348. void rcutorture_record_test_transition(void);
  349. void rcutorture_record_progress(unsigned long vernum);
  350. void do_trace_rcu_torture_read(const char *rcutorturename,
  351. struct rcu_head *rhp,
  352. unsigned long secs,
  353. unsigned long c_old,
  354. unsigned long c);
  355. #else
  356. static inline void rcutorture_get_gp_data(enum rcutorture_type test_type,
  357. int *flags,
  358. unsigned long *gpnum,
  359. unsigned long *completed)
  360. {
  361. *flags = 0;
  362. *gpnum = 0;
  363. *completed = 0;
  364. }
  365. static inline void rcutorture_record_test_transition(void) { }
  366. static inline void rcutorture_record_progress(unsigned long vernum) { }
  367. #ifdef CONFIG_RCU_TRACE
  368. void do_trace_rcu_torture_read(const char *rcutorturename,
  369. struct rcu_head *rhp,
  370. unsigned long secs,
  371. unsigned long c_old,
  372. unsigned long c);
  373. #else
  374. #define do_trace_rcu_torture_read(rcutorturename, rhp, secs, c_old, c) \
  375. do { } while (0)
  376. #endif
  377. #endif
  378. #ifdef CONFIG_TINY_SRCU
  379. static inline void srcutorture_get_gp_data(enum rcutorture_type test_type,
  380. struct srcu_struct *sp, int *flags,
  381. unsigned long *gpnum,
  382. unsigned long *completed)
  383. {
  384. if (test_type != SRCU_FLAVOR)
  385. return;
  386. *flags = 0;
  387. *completed = sp->srcu_idx;
  388. *gpnum = *completed;
  389. }
  390. #elif defined(CONFIG_TREE_SRCU)
  391. void srcutorture_get_gp_data(enum rcutorture_type test_type,
  392. struct srcu_struct *sp, int *flags,
  393. unsigned long *gpnum, unsigned long *completed);
  394. #endif
  395. #ifdef CONFIG_TINY_RCU
  396. static inline unsigned long rcu_batches_started(void) { return 0; }
  397. static inline unsigned long rcu_batches_started_bh(void) { return 0; }
  398. static inline unsigned long rcu_batches_started_sched(void) { return 0; }
  399. static inline unsigned long rcu_batches_completed(void) { return 0; }
  400. static inline unsigned long rcu_batches_completed_bh(void) { return 0; }
  401. static inline unsigned long rcu_batches_completed_sched(void) { return 0; }
  402. static inline unsigned long rcu_exp_batches_completed(void) { return 0; }
  403. static inline unsigned long rcu_exp_batches_completed_sched(void) { return 0; }
  404. static inline unsigned long
  405. srcu_batches_completed(struct srcu_struct *sp) { return 0; }
  406. static inline void rcu_force_quiescent_state(void) { }
  407. static inline void rcu_bh_force_quiescent_state(void) { }
  408. static inline void rcu_sched_force_quiescent_state(void) { }
  409. static inline void show_rcu_gp_kthreads(void) { }
  410. #else /* #ifdef CONFIG_TINY_RCU */
  411. extern unsigned long rcutorture_testseq;
  412. extern unsigned long rcutorture_vernum;
  413. unsigned long rcu_batches_started(void);
  414. unsigned long rcu_batches_started_bh(void);
  415. unsigned long rcu_batches_started_sched(void);
  416. unsigned long rcu_batches_completed(void);
  417. unsigned long rcu_batches_completed_bh(void);
  418. unsigned long rcu_batches_completed_sched(void);
  419. unsigned long rcu_exp_batches_completed(void);
  420. unsigned long rcu_exp_batches_completed_sched(void);
  421. unsigned long srcu_batches_completed(struct srcu_struct *sp);
  422. void show_rcu_gp_kthreads(void);
  423. void rcu_force_quiescent_state(void);
  424. void rcu_bh_force_quiescent_state(void);
  425. void rcu_sched_force_quiescent_state(void);
  426. extern struct workqueue_struct *rcu_gp_wq;
  427. extern struct workqueue_struct *rcu_par_gp_wq;
  428. #endif /* #else #ifdef CONFIG_TINY_RCU */
  429. #ifdef CONFIG_RCU_NOCB_CPU
  430. bool rcu_is_nocb_cpu(int cpu);
  431. #else
  432. static inline bool rcu_is_nocb_cpu(int cpu) { return false; }
  433. #endif
  434. #endif /* __LINUX_RCU_H */