rcutree.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /*
  2. * Read-Copy Update mechanism for mutual exclusion (tree-based version)
  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, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. *
  18. * Copyright IBM Corporation, 2008
  19. *
  20. * Author: Dipankar Sarma <dipankar@in.ibm.com>
  21. * Paul E. McKenney <paulmck@linux.vnet.ibm.com> Hierarchical algorithm
  22. *
  23. * Based on the original work by Paul McKenney <paulmck@us.ibm.com>
  24. * and inputs from Rusty Russell, Andrea Arcangeli and Andi Kleen.
  25. *
  26. * For detailed explanation of Read-Copy Update mechanism see -
  27. * Documentation/RCU
  28. */
  29. #ifndef __LINUX_RCUTREE_H
  30. #define __LINUX_RCUTREE_H
  31. #include <linux/cache.h>
  32. #include <linux/spinlock.h>
  33. #include <linux/threads.h>
  34. #include <linux/percpu.h>
  35. #include <linux/cpumask.h>
  36. #include <linux/seqlock.h>
  37. /*
  38. * Define shape of hierarchy based on NR_CPUS and CONFIG_RCU_FANOUT.
  39. * In theory, it should be possible to add more levels straightforwardly.
  40. * In practice, this has not been tested, so there is probably some
  41. * bug somewhere.
  42. */
  43. #define MAX_RCU_LVLS 3
  44. #define RCU_FANOUT (CONFIG_RCU_FANOUT)
  45. #define RCU_FANOUT_SQ (RCU_FANOUT * RCU_FANOUT)
  46. #define RCU_FANOUT_CUBE (RCU_FANOUT_SQ * RCU_FANOUT)
  47. #if NR_CPUS <= RCU_FANOUT
  48. # define NUM_RCU_LVLS 1
  49. # define NUM_RCU_LVL_0 1
  50. # define NUM_RCU_LVL_1 (NR_CPUS)
  51. # define NUM_RCU_LVL_2 0
  52. # define NUM_RCU_LVL_3 0
  53. #elif NR_CPUS <= RCU_FANOUT_SQ
  54. # define NUM_RCU_LVLS 2
  55. # define NUM_RCU_LVL_0 1
  56. # define NUM_RCU_LVL_1 (((NR_CPUS) + RCU_FANOUT - 1) / RCU_FANOUT)
  57. # define NUM_RCU_LVL_2 (NR_CPUS)
  58. # define NUM_RCU_LVL_3 0
  59. #elif NR_CPUS <= RCU_FANOUT_CUBE
  60. # define NUM_RCU_LVLS 3
  61. # define NUM_RCU_LVL_0 1
  62. # define NUM_RCU_LVL_1 (((NR_CPUS) + RCU_FANOUT_SQ - 1) / RCU_FANOUT_SQ)
  63. # define NUM_RCU_LVL_2 (((NR_CPUS) + (RCU_FANOUT) - 1) / (RCU_FANOUT))
  64. # define NUM_RCU_LVL_3 NR_CPUS
  65. #else
  66. # error "CONFIG_RCU_FANOUT insufficient for NR_CPUS"
  67. #endif /* #if (NR_CPUS) <= RCU_FANOUT */
  68. #define RCU_SUM (NUM_RCU_LVL_0 + NUM_RCU_LVL_1 + NUM_RCU_LVL_2 + NUM_RCU_LVL_3)
  69. #define NUM_RCU_NODES (RCU_SUM - NR_CPUS)
  70. /*
  71. * Dynticks per-CPU state.
  72. */
  73. struct rcu_dynticks {
  74. int dynticks_nesting; /* Track nesting level, sort of. */
  75. int dynticks; /* Even value for dynticks-idle, else odd. */
  76. int dynticks_nmi; /* Even value for either dynticks-idle or */
  77. /* not in nmi handler, else odd. So this */
  78. /* remains even for nmi from irq handler. */
  79. };
  80. /*
  81. * Definition for node within the RCU grace-period-detection hierarchy.
  82. */
  83. struct rcu_node {
  84. spinlock_t lock;
  85. unsigned long qsmask; /* CPUs or groups that need to switch in */
  86. /* order for current grace period to proceed.*/
  87. unsigned long qsmaskinit;
  88. /* Per-GP initialization for qsmask. */
  89. unsigned long grpmask; /* Mask to apply to parent qsmask. */
  90. int grplo; /* lowest-numbered CPU or group here. */
  91. int grphi; /* highest-numbered CPU or group here. */
  92. u8 grpnum; /* CPU/group number for next level up. */
  93. u8 level; /* root is at level 0. */
  94. struct rcu_node *parent;
  95. } ____cacheline_internodealigned_in_smp;
  96. /* Index values for nxttail array in struct rcu_data. */
  97. #define RCU_DONE_TAIL 0 /* Also RCU_WAIT head. */
  98. #define RCU_WAIT_TAIL 1 /* Also RCU_NEXT_READY head. */
  99. #define RCU_NEXT_READY_TAIL 2 /* Also RCU_NEXT head. */
  100. #define RCU_NEXT_TAIL 3
  101. #define RCU_NEXT_SIZE 4
  102. /* Per-CPU data for read-copy update. */
  103. struct rcu_data {
  104. /* 1) quiescent-state and grace-period handling : */
  105. long completed; /* Track rsp->completed gp number */
  106. /* in order to detect GP end. */
  107. long gpnum; /* Highest gp number that this CPU */
  108. /* is aware of having started. */
  109. long passed_quiesc_completed;
  110. /* Value of completed at time of qs. */
  111. bool passed_quiesc; /* User-mode/idle loop etc. */
  112. bool qs_pending; /* Core waits for quiesc state. */
  113. bool beenonline; /* CPU online at least once. */
  114. struct rcu_node *mynode; /* This CPU's leaf of hierarchy */
  115. unsigned long grpmask; /* Mask to apply to leaf qsmask. */
  116. /* 2) batch handling */
  117. /*
  118. * If nxtlist is not NULL, it is partitioned as follows.
  119. * Any of the partitions might be empty, in which case the
  120. * pointer to that partition will be equal to the pointer for
  121. * the following partition. When the list is empty, all of
  122. * the nxttail elements point to nxtlist, which is NULL.
  123. *
  124. * [*nxttail[RCU_NEXT_READY_TAIL], NULL = *nxttail[RCU_NEXT_TAIL]):
  125. * Entries that might have arrived after current GP ended
  126. * [*nxttail[RCU_WAIT_TAIL], *nxttail[RCU_NEXT_READY_TAIL]):
  127. * Entries known to have arrived before current GP ended
  128. * [*nxttail[RCU_DONE_TAIL], *nxttail[RCU_WAIT_TAIL]):
  129. * Entries that batch # <= ->completed - 1: waiting for current GP
  130. * [nxtlist, *nxttail[RCU_DONE_TAIL]):
  131. * Entries that batch # <= ->completed
  132. * The grace period for these entries has completed, and
  133. * the other grace-period-completed entries may be moved
  134. * here temporarily in rcu_process_callbacks().
  135. */
  136. struct rcu_head *nxtlist;
  137. struct rcu_head **nxttail[RCU_NEXT_SIZE];
  138. long qlen; /* # of queued callbacks */
  139. long blimit; /* Upper limit on a processed batch */
  140. #ifdef CONFIG_NO_HZ
  141. /* 3) dynticks interface. */
  142. struct rcu_dynticks *dynticks; /* Shared per-CPU dynticks state. */
  143. int dynticks_snap; /* Per-GP tracking for dynticks. */
  144. int dynticks_nmi_snap; /* Per-GP tracking for dynticks_nmi. */
  145. #endif /* #ifdef CONFIG_NO_HZ */
  146. /* 4) reasons this CPU needed to be kicked by force_quiescent_state */
  147. #ifdef CONFIG_NO_HZ
  148. unsigned long dynticks_fqs; /* Kicked due to dynticks idle. */
  149. #endif /* #ifdef CONFIG_NO_HZ */
  150. unsigned long offline_fqs; /* Kicked due to being offline. */
  151. unsigned long resched_ipi; /* Sent a resched IPI. */
  152. /* 5) state to allow this CPU to force_quiescent_state on others */
  153. long n_rcu_pending; /* rcu_pending() calls since boot. */
  154. long n_rcu_pending_force_qs; /* when to force quiescent states. */
  155. int cpu;
  156. };
  157. /* Values for signaled field in struct rcu_state. */
  158. #define RCU_GP_INIT 0 /* Grace period being initialized. */
  159. #define RCU_SAVE_DYNTICK 1 /* Need to scan dyntick state. */
  160. #define RCU_FORCE_QS 2 /* Need to force quiescent state. */
  161. #ifdef CONFIG_NO_HZ
  162. #define RCU_SIGNAL_INIT RCU_SAVE_DYNTICK
  163. #else /* #ifdef CONFIG_NO_HZ */
  164. #define RCU_SIGNAL_INIT RCU_FORCE_QS
  165. #endif /* #else #ifdef CONFIG_NO_HZ */
  166. #define RCU_JIFFIES_TILL_FORCE_QS 3 /* for rsp->jiffies_force_qs */
  167. #ifdef CONFIG_RCU_CPU_STALL_DETECTOR
  168. #define RCU_SECONDS_TILL_STALL_CHECK (10 * HZ) /* for rsp->jiffies_stall */
  169. #define RCU_SECONDS_TILL_STALL_RECHECK (30 * HZ) /* for rsp->jiffies_stall */
  170. #define RCU_STALL_RAT_DELAY 2 /* Allow other CPUs time */
  171. /* to take at least one */
  172. /* scheduling clock irq */
  173. /* before ratting on them. */
  174. #endif /* #ifdef CONFIG_RCU_CPU_STALL_DETECTOR */
  175. /*
  176. * RCU global state, including node hierarchy. This hierarchy is
  177. * represented in "heap" form in a dense array. The root (first level)
  178. * of the hierarchy is in ->node[0] (referenced by ->level[0]), the second
  179. * level in ->node[1] through ->node[m] (->node[1] referenced by ->level[1]),
  180. * and the third level in ->node[m+1] and following (->node[m+1] referenced
  181. * by ->level[2]). The number of levels is determined by the number of
  182. * CPUs and by CONFIG_RCU_FANOUT. Small systems will have a "hierarchy"
  183. * consisting of a single rcu_node.
  184. */
  185. struct rcu_state {
  186. struct rcu_node node[NUM_RCU_NODES]; /* Hierarchy. */
  187. struct rcu_node *level[NUM_RCU_LVLS]; /* Hierarchy levels. */
  188. u32 levelcnt[MAX_RCU_LVLS + 1]; /* # nodes in each level. */
  189. u8 levelspread[NUM_RCU_LVLS]; /* kids/node in each level. */
  190. struct rcu_data *rda[NR_CPUS]; /* array of rdp pointers. */
  191. /* The following fields are guarded by the root rcu_node's lock. */
  192. u8 signaled ____cacheline_internodealigned_in_smp;
  193. /* Force QS state. */
  194. long gpnum; /* Current gp number. */
  195. long completed; /* # of last completed gp. */
  196. spinlock_t onofflock; /* exclude on/offline and */
  197. /* starting new GP. */
  198. spinlock_t fqslock; /* Only one task forcing */
  199. /* quiescent states. */
  200. unsigned long jiffies_force_qs; /* Time at which to invoke */
  201. /* force_quiescent_state(). */
  202. unsigned long n_force_qs; /* Number of calls to */
  203. /* force_quiescent_state(). */
  204. unsigned long n_force_qs_lh; /* ~Number of calls leaving */
  205. /* due to lock unavailable. */
  206. unsigned long n_force_qs_ngp; /* Number of calls leaving */
  207. /* due to no GP active. */
  208. #ifdef CONFIG_RCU_CPU_STALL_DETECTOR
  209. unsigned long gp_start; /* Time at which GP started, */
  210. /* but in jiffies. */
  211. unsigned long jiffies_stall; /* Time at which to check */
  212. /* for CPU stalls. */
  213. #endif /* #ifdef CONFIG_RCU_CPU_STALL_DETECTOR */
  214. #ifdef CONFIG_NO_HZ
  215. long dynticks_completed; /* Value of completed @ snap. */
  216. #endif /* #ifdef CONFIG_NO_HZ */
  217. };
  218. extern void rcu_qsctr_inc(int cpu);
  219. extern void rcu_bh_qsctr_inc(int cpu);
  220. extern int rcu_pending(int cpu);
  221. extern int rcu_needs_cpu(int cpu);
  222. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  223. extern struct lockdep_map rcu_lock_map;
  224. # define rcu_read_acquire() \
  225. lock_acquire(&rcu_lock_map, 0, 0, 2, 1, NULL, _THIS_IP_)
  226. # define rcu_read_release() lock_release(&rcu_lock_map, 1, _THIS_IP_)
  227. #else
  228. # define rcu_read_acquire() do { } while (0)
  229. # define rcu_read_release() do { } while (0)
  230. #endif
  231. static inline void __rcu_read_lock(void)
  232. {
  233. preempt_disable();
  234. __acquire(RCU);
  235. rcu_read_acquire();
  236. }
  237. static inline void __rcu_read_unlock(void)
  238. {
  239. rcu_read_release();
  240. __release(RCU);
  241. preempt_enable();
  242. }
  243. static inline void __rcu_read_lock_bh(void)
  244. {
  245. local_bh_disable();
  246. __acquire(RCU_BH);
  247. rcu_read_acquire();
  248. }
  249. static inline void __rcu_read_unlock_bh(void)
  250. {
  251. rcu_read_release();
  252. __release(RCU_BH);
  253. local_bh_enable();
  254. }
  255. #define __synchronize_sched() synchronize_rcu()
  256. #define call_rcu_sched(head, func) call_rcu(head, func)
  257. static inline void rcu_init_sched(void)
  258. {
  259. }
  260. extern void __rcu_init(void);
  261. extern void rcu_check_callbacks(int cpu, int user);
  262. extern void rcu_restart_cpu(int cpu);
  263. extern long rcu_batches_completed(void);
  264. extern long rcu_batches_completed_bh(void);
  265. #ifdef CONFIG_NO_HZ
  266. void rcu_enter_nohz(void);
  267. void rcu_exit_nohz(void);
  268. #else /* CONFIG_NO_HZ */
  269. static inline void rcu_enter_nohz(void)
  270. {
  271. }
  272. static inline void rcu_exit_nohz(void)
  273. {
  274. }
  275. #endif /* CONFIG_NO_HZ */
  276. /* A context switch is a grace period for rcutree. */
  277. static inline int rcu_blocking_is_gp(void)
  278. {
  279. return num_online_cpus() == 1;
  280. }
  281. #endif /* __LINUX_RCUTREE_H */