rcutree.h 11 KB

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