rcu.h 15 KB

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