Data-Structures.html 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  2. "http://www.w3.org/TR/html4/loose.dtd">
  3. <html>
  4. <head><title>A Tour Through TREE_RCU's Data Structures [LWN.net]</title>
  5. <meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
  6. <p>December 18, 2016</p>
  7. <p>This article was contributed by Paul E.&nbsp;McKenney</p>
  8. <h3>Introduction</h3>
  9. This document describes RCU's major data structures and their relationship
  10. to each other.
  11. <ol>
  12. <li> <a href="#Data-Structure Relationships">
  13. Data-Structure Relationships</a>
  14. <li> <a href="#The rcu_state Structure">
  15. The <tt>rcu_state</tt> Structure</a>
  16. <li> <a href="#The rcu_node Structure">
  17. The <tt>rcu_node</tt> Structure</a>
  18. <li> <a href="#The rcu_data Structure">
  19. The <tt>rcu_data</tt> Structure</a>
  20. <li> <a href="#The rcu_dynticks Structure">
  21. The <tt>rcu_dynticks</tt> Structure</a>
  22. <li> <a href="#The rcu_head Structure">
  23. The <tt>rcu_head</tt> Structure</a>
  24. <li> <a href="#RCU-Specific Fields in the task_struct Structure">
  25. RCU-Specific Fields in the <tt>task_struct</tt> Structure</a>
  26. <li> <a href="#Accessor Functions">
  27. Accessor Functions</a>
  28. </ol>
  29. <h3><a name="Data-Structure Relationships">Data-Structure Relationships</a></h3>
  30. <p>RCU is for all intents and purposes a large state machine, and its
  31. data structures maintain the state in such a way as to allow RCU readers
  32. to execute extremely quickly, while also processing the RCU grace periods
  33. requested by updaters in an efficient and extremely scalable fashion.
  34. The efficiency and scalability of RCU updaters is provided primarily
  35. by a combining tree, as shown below:
  36. </p><p><img src="BigTreeClassicRCU.svg" alt="BigTreeClassicRCU.svg" width="30%">
  37. </p><p>This diagram shows an enclosing <tt>rcu_state</tt> structure
  38. containing a tree of <tt>rcu_node</tt> structures.
  39. Each leaf node of the <tt>rcu_node</tt> tree has up to 16
  40. <tt>rcu_data</tt> structures associated with it, so that there
  41. are <tt>NR_CPUS</tt> number of <tt>rcu_data</tt> structures,
  42. one for each possible CPU.
  43. This structure is adjusted at boot time, if needed, to handle the
  44. common case where <tt>nr_cpu_ids</tt> is much less than
  45. <tt>NR_CPUs</tt>.
  46. For example, a number of Linux distributions set <tt>NR_CPUs=4096</tt>,
  47. which results in a three-level <tt>rcu_node</tt> tree.
  48. If the actual hardware has only 16 CPUs, RCU will adjust itself
  49. at boot time, resulting in an <tt>rcu_node</tt> tree with only a single node.
  50. </p><p>The purpose of this combining tree is to allow per-CPU events
  51. such as quiescent states, dyntick-idle transitions,
  52. and CPU hotplug operations to be processed efficiently
  53. and scalably.
  54. Quiescent states are recorded by the per-CPU <tt>rcu_data</tt> structures,
  55. and other events are recorded by the leaf-level <tt>rcu_node</tt>
  56. structures.
  57. All of these events are combined at each level of the tree until finally
  58. grace periods are completed at the tree's root <tt>rcu_node</tt>
  59. structure.
  60. A grace period can be completed at the root once every CPU
  61. (or, in the case of <tt>CONFIG_PREEMPT_RCU</tt>, task)
  62. has passed through a quiescent state.
  63. Once a grace period has completed, record of that fact is propagated
  64. back down the tree.
  65. </p><p>As can be seen from the diagram, on a 64-bit system
  66. a two-level tree with 64 leaves can accommodate 1,024 CPUs, with a fanout
  67. of 64 at the root and a fanout of 16 at the leaves.
  68. <table>
  69. <tr><th>&nbsp;</th></tr>
  70. <tr><th align="left">Quick Quiz:</th></tr>
  71. <tr><td>
  72. Why isn't the fanout at the leaves also 64?
  73. </td></tr>
  74. <tr><th align="left">Answer:</th></tr>
  75. <tr><td bgcolor="#ffffff"><font color="ffffff">
  76. Because there are more types of events that affect the leaf-level
  77. <tt>rcu_node</tt> structures than further up the tree.
  78. Therefore, if the leaf <tt>rcu_node</tt> structures have fanout of
  79. 64, the contention on these structures' <tt>-&gt;structures</tt>
  80. becomes excessive.
  81. Experimentation on a wide variety of systems has shown that a fanout
  82. of 16 works well for the leaves of the <tt>rcu_node</tt> tree.
  83. </font>
  84. <p><font color="ffffff">Of course, further experience with
  85. systems having hundreds or thousands of CPUs may demonstrate
  86. that the fanout for the non-leaf <tt>rcu_node</tt> structures
  87. must also be reduced.
  88. Such reduction can be easily carried out when and if it proves
  89. necessary.
  90. In the meantime, if you are using such a system and running into
  91. contention problems on the non-leaf <tt>rcu_node</tt> structures,
  92. you may use the <tt>CONFIG_RCU_FANOUT</tt> kernel configuration
  93. parameter to reduce the non-leaf fanout as needed.
  94. </font>
  95. <p><font color="ffffff">Kernels built for systems with
  96. strong NUMA characteristics might also need to adjust
  97. <tt>CONFIG_RCU_FANOUT</tt> so that the domains of the
  98. <tt>rcu_node</tt> structures align with hardware boundaries.
  99. However, there has thus far been no need for this.
  100. </font></td></tr>
  101. <tr><td>&nbsp;</td></tr>
  102. </table>
  103. <p>If your system has more than 1,024 CPUs (or more than 512 CPUs on
  104. a 32-bit system), then RCU will automatically add more levels to the
  105. tree.
  106. For example, if you are crazy enough to build a 64-bit system with 65,536
  107. CPUs, RCU would configure the <tt>rcu_node</tt> tree as follows:
  108. </p><p><img src="HugeTreeClassicRCU.svg" alt="HugeTreeClassicRCU.svg" width="50%">
  109. </p><p>RCU currently permits up to a four-level tree, which on a 64-bit system
  110. accommodates up to 4,194,304 CPUs, though only a mere 524,288 CPUs for
  111. 32-bit systems.
  112. On the other hand, you can set <tt>CONFIG_RCU_FANOUT</tt> to be
  113. as small as 2 if you wish, which would permit only 16 CPUs, which
  114. is useful for testing.
  115. </p><p>This multi-level combining tree allows us to get most of the
  116. performance and scalability
  117. benefits of partitioning, even though RCU grace-period detection is
  118. inherently a global operation.
  119. The trick here is that only the last CPU to report a quiescent state
  120. into a given <tt>rcu_node</tt> structure need advance to the <tt>rcu_node</tt>
  121. structure at the next level up the tree.
  122. This means that at the leaf-level <tt>rcu_node</tt> structure, only
  123. one access out of sixteen will progress up the tree.
  124. For the internal <tt>rcu_node</tt> structures, the situation is even
  125. more extreme: Only one access out of sixty-four will progress up
  126. the tree.
  127. Because the vast majority of the CPUs do not progress up the tree,
  128. the lock contention remains roughly constant up the tree.
  129. No matter how many CPUs there are in the system, at most 64 quiescent-state
  130. reports per grace period will progress all the way to the root
  131. <tt>rcu_node</tt> structure, thus ensuring that the lock contention
  132. on that root <tt>rcu_node</tt> structure remains acceptably low.
  133. </p><p>In effect, the combining tree acts like a big shock absorber,
  134. keeping lock contention under control at all tree levels regardless
  135. of the level of loading on the system.
  136. </p><p>The Linux kernel actually supports multiple flavors of RCU
  137. running concurrently, so RCU builds separate data structures for each
  138. flavor.
  139. For example, for <tt>CONFIG_TREE_RCU=y</tt> kernels, RCU provides
  140. rcu_sched and rcu_bh, as shown below:
  141. </p><p><img src="BigTreeClassicRCUBH.svg" alt="BigTreeClassicRCUBH.svg" width="33%">
  142. </p><p>Energy efficiency is increasingly important, and for that
  143. reason the Linux kernel provides <tt>CONFIG_NO_HZ_IDLE</tt>, which
  144. turns off the scheduling-clock interrupts on idle CPUs, which in
  145. turn allows those CPUs to attain deeper sleep states and to consume
  146. less energy.
  147. CPUs whose scheduling-clock interrupts have been turned off are
  148. said to be in <i>dyntick-idle mode</i>.
  149. RCU must handle dyntick-idle CPUs specially
  150. because RCU would otherwise wake up each CPU on every grace period,
  151. which would defeat the whole purpose of <tt>CONFIG_NO_HZ_IDLE</tt>.
  152. RCU uses the <tt>rcu_dynticks</tt> structure to track
  153. which CPUs are in dyntick idle mode, as shown below:
  154. </p><p><img src="BigTreeClassicRCUBHdyntick.svg" alt="BigTreeClassicRCUBHdyntick.svg" width="33%">
  155. </p><p>However, if a CPU is in dyntick-idle mode, it is in that mode
  156. for all flavors of RCU.
  157. Therefore, a single <tt>rcu_dynticks</tt> structure is allocated per
  158. CPU, and all of a given CPU's <tt>rcu_data</tt> structures share
  159. that <tt>rcu_dynticks</tt>, as shown in the figure.
  160. </p><p>Kernels built with <tt>CONFIG_PREEMPT_RCU</tt> support
  161. rcu_preempt in addition to rcu_sched and rcu_bh, as shown below:
  162. </p><p><img src="BigTreePreemptRCUBHdyntick.svg" alt="BigTreePreemptRCUBHdyntick.svg" width="35%">
  163. </p><p>RCU updaters wait for normal grace periods by registering
  164. RCU callbacks, either directly via <tt>call_rcu()</tt> and
  165. friends (namely <tt>call_rcu_bh()</tt> and <tt>call_rcu_sched()</tt>),
  166. there being a separate interface per flavor of RCU)
  167. or indirectly via <tt>synchronize_rcu()</tt> and friends.
  168. RCU callbacks are represented by <tt>rcu_head</tt> structures,
  169. which are queued on <tt>rcu_data</tt> structures while they are
  170. waiting for a grace period to elapse, as shown in the following figure:
  171. </p><p><img src="BigTreePreemptRCUBHdyntickCB.svg" alt="BigTreePreemptRCUBHdyntickCB.svg" width="40%">
  172. </p><p>This figure shows how <tt>TREE_RCU</tt>'s and
  173. <tt>PREEMPT_RCU</tt>'s major data structures are related.
  174. Lesser data structures will be introduced with the algorithms that
  175. make use of them.
  176. </p><p>Note that each of the data structures in the above figure has
  177. its own synchronization:
  178. <p><ol>
  179. <li> Each <tt>rcu_state</tt> structures has a lock and a mutex,
  180. and some fields are protected by the corresponding root
  181. <tt>rcu_node</tt> structure's lock.
  182. <li> Each <tt>rcu_node</tt> structure has a spinlock.
  183. <li> The fields in <tt>rcu_data</tt> are private to the corresponding
  184. CPU, although a few can be read and written by other CPUs.
  185. <li> Similarly, the fields in <tt>rcu_dynticks</tt> are private
  186. to the corresponding CPU, although a few can be read by
  187. other CPUs.
  188. </ol>
  189. <p>It is important to note that different data structures can have
  190. very different ideas about the state of RCU at any given time.
  191. For but one example, awareness of the start or end of a given RCU
  192. grace period propagates slowly through the data structures.
  193. This slow propagation is absolutely necessary for RCU to have good
  194. read-side performance.
  195. If this balkanized implementation seems foreign to you, one useful
  196. trick is to consider each instance of these data structures to be
  197. a different person, each having the usual slightly different
  198. view of reality.
  199. </p><p>The general role of each of these data structures is as
  200. follows:
  201. </p><ol>
  202. <li> <tt>rcu_state</tt>:
  203. This structure forms the interconnection between the
  204. <tt>rcu_node</tt> and <tt>rcu_data</tt> structures,
  205. tracks grace periods, serves as short-term repository
  206. for callbacks orphaned by CPU-hotplug events,
  207. maintains <tt>rcu_barrier()</tt> state,
  208. tracks expedited grace-period state,
  209. and maintains state used to force quiescent states when
  210. grace periods extend too long,
  211. <li> <tt>rcu_node</tt>: This structure forms the combining
  212. tree that propagates quiescent-state
  213. information from the leaves to the root, and also propagates
  214. grace-period information from the root to the leaves.
  215. It provides local copies of the grace-period state in order
  216. to allow this information to be accessed in a synchronized
  217. manner without suffering the scalability limitations that
  218. would otherwise be imposed by global locking.
  219. In <tt>CONFIG_PREEMPT_RCU</tt> kernels, it manages the lists
  220. of tasks that have blocked while in their current
  221. RCU read-side critical section.
  222. In <tt>CONFIG_PREEMPT_RCU</tt> with
  223. <tt>CONFIG_RCU_BOOST</tt>, it manages the
  224. per-<tt>rcu_node</tt> priority-boosting
  225. kernel threads (kthreads) and state.
  226. Finally, it records CPU-hotplug state in order to determine
  227. which CPUs should be ignored during a given grace period.
  228. <li> <tt>rcu_data</tt>: This per-CPU structure is the
  229. focus of quiescent-state detection and RCU callback queuing.
  230. It also tracks its relationship to the corresponding leaf
  231. <tt>rcu_node</tt> structure to allow more-efficient
  232. propagation of quiescent states up the <tt>rcu_node</tt>
  233. combining tree.
  234. Like the <tt>rcu_node</tt> structure, it provides a local
  235. copy of the grace-period information to allow for-free
  236. synchronized
  237. access to this information from the corresponding CPU.
  238. Finally, this structure records past dyntick-idle state
  239. for the corresponding CPU and also tracks statistics.
  240. <li> <tt>rcu_dynticks</tt>:
  241. This per-CPU structure tracks the current dyntick-idle
  242. state for the corresponding CPU.
  243. Unlike the other three structures, the <tt>rcu_dynticks</tt>
  244. structure is not replicated per RCU flavor.
  245. <li> <tt>rcu_head</tt>:
  246. This structure represents RCU callbacks, and is the
  247. only structure allocated and managed by RCU users.
  248. The <tt>rcu_head</tt> structure is normally embedded
  249. within the RCU-protected data structure.
  250. </ol>
  251. <p>If all you wanted from this article was a general notion of how
  252. RCU's data structures are related, you are done.
  253. Otherwise, each of the following sections give more details on
  254. the <tt>rcu_state</tt>, <tt>rcu_node</tt>, <tt>rcu_data</tt>,
  255. and <tt>rcu_dynticks</tt> data structures.
  256. <h3><a name="The rcu_state Structure">
  257. The <tt>rcu_state</tt> Structure</a></h3>
  258. <p>The <tt>rcu_state</tt> structure is the base structure that
  259. represents a flavor of RCU.
  260. This structure forms the interconnection between the
  261. <tt>rcu_node</tt> and <tt>rcu_data</tt> structures,
  262. tracks grace periods, contains the lock used to
  263. synchronize with CPU-hotplug events,
  264. and maintains state used to force quiescent states when
  265. grace periods extend too long,
  266. </p><p>A few of the <tt>rcu_state</tt> structure's fields are discussed,
  267. singly and in groups, in the following sections.
  268. The more specialized fields are covered in the discussion of their
  269. use.
  270. <h5>Relationship to rcu_node and rcu_data Structures</h5>
  271. This portion of the <tt>rcu_state</tt> structure is declared
  272. as follows:
  273. <pre>
  274. 1 struct rcu_node node[NUM_RCU_NODES];
  275. 2 struct rcu_node *level[NUM_RCU_LVLS + 1];
  276. 3 struct rcu_data __percpu *rda;
  277. </pre>
  278. <table>
  279. <tr><th>&nbsp;</th></tr>
  280. <tr><th align="left">Quick Quiz:</th></tr>
  281. <tr><td>
  282. Wait a minute!
  283. You said that the <tt>rcu_node</tt> structures formed a tree,
  284. but they are declared as a flat array!
  285. What gives?
  286. </td></tr>
  287. <tr><th align="left">Answer:</th></tr>
  288. <tr><td bgcolor="#ffffff"><font color="ffffff">
  289. The tree is laid out in the array.
  290. The first node In the array is the head, the next set of nodes in the
  291. array are children of the head node, and so on until the last set of
  292. nodes in the array are the leaves.
  293. </font>
  294. <p><font color="ffffff">See the following diagrams to see how
  295. this works.
  296. </font></td></tr>
  297. <tr><td>&nbsp;</td></tr>
  298. </table>
  299. <p>The <tt>rcu_node</tt> tree is embedded into the
  300. <tt>-&gt;node[]</tt> array as shown in the following figure:
  301. </p><p><img src="TreeMapping.svg" alt="TreeMapping.svg" width="40%">
  302. </p><p>One interesting consequence of this mapping is that a
  303. breadth-first traversal of the tree is implemented as a simple
  304. linear scan of the array, which is in fact what the
  305. <tt>rcu_for_each_node_breadth_first()</tt> macro does.
  306. This macro is used at the beginning and ends of grace periods.
  307. </p><p>Each entry of the <tt>-&gt;level</tt> array references
  308. the first <tt>rcu_node</tt> structure on the corresponding level
  309. of the tree, for example, as shown below:
  310. </p><p><img src="TreeMappingLevel.svg" alt="TreeMappingLevel.svg" width="40%">
  311. </p><p>The zero<sup>th</sup> element of the array references the root
  312. <tt>rcu_node</tt> structure, the first element references the
  313. first child of the root <tt>rcu_node</tt>, and finally the second
  314. element references the first leaf <tt>rcu_node</tt> structure.
  315. </p><p>For whatever it is worth, if you draw the tree to be tree-shaped
  316. rather than array-shaped, it is easy to draw a planar representation:
  317. </p><p><img src="TreeLevel.svg" alt="TreeLevel.svg" width="60%">
  318. </p><p>Finally, the <tt>-&gt;rda</tt> field references a per-CPU
  319. pointer to the corresponding CPU's <tt>rcu_data</tt> structure.
  320. </p><p>All of these fields are constant once initialization is complete,
  321. and therefore need no protection.
  322. <h5>Grace-Period Tracking</h5>
  323. <p>This portion of the <tt>rcu_state</tt> structure is declared
  324. as follows:
  325. <pre>
  326. 1 unsigned long gpnum;
  327. 2 unsigned long completed;
  328. </pre>
  329. <p>RCU grace periods are numbered, and
  330. the <tt>-&gt;gpnum</tt> field contains the number of the grace
  331. period that started most recently.
  332. The <tt>-&gt;completed</tt> field contains the number of the
  333. grace period that completed most recently.
  334. If the two fields are equal, the RCU grace period that most recently
  335. started has already completed, and therefore the corresponding
  336. flavor of RCU is idle.
  337. If <tt>-&gt;gpnum</tt> is one greater than <tt>-&gt;completed</tt>,
  338. then <tt>-&gt;gpnum</tt> gives the number of the current RCU
  339. grace period, which has not yet completed.
  340. Any other combination of values indicates that something is broken.
  341. These two fields are protected by the root <tt>rcu_node</tt>'s
  342. <tt>-&gt;lock</tt> field.
  343. </p><p>There are <tt>-&gt;gpnum</tt> and <tt>-&gt;completed</tt> fields
  344. in the <tt>rcu_node</tt> and <tt>rcu_data</tt> structures
  345. as well.
  346. The fields in the <tt>rcu_state</tt> structure represent the
  347. most current values, and those of the other structures are compared
  348. in order to detect the start of a new grace period in a distributed
  349. fashion.
  350. The values flow from <tt>rcu_state</tt> to <tt>rcu_node</tt>
  351. (down the tree from the root to the leaves) to <tt>rcu_data</tt>.
  352. <h5>Miscellaneous</h5>
  353. <p>This portion of the <tt>rcu_state</tt> structure is declared
  354. as follows:
  355. <pre>
  356. 1 unsigned long gp_max;
  357. 2 char abbr;
  358. 3 char *name;
  359. </pre>
  360. <p>The <tt>-&gt;gp_max</tt> field tracks the duration of the longest
  361. grace period in jiffies.
  362. It is protected by the root <tt>rcu_node</tt>'s <tt>-&gt;lock</tt>.
  363. <p>The <tt>-&gt;name</tt> field points to the name of the RCU flavor
  364. (for example, &ldquo;rcu_sched&rdquo;), and is constant.
  365. The <tt>-&gt;abbr</tt> field contains a one-character abbreviation,
  366. for example, &ldquo;s&rdquo; for RCU-sched.
  367. <h3><a name="The rcu_node Structure">
  368. The <tt>rcu_node</tt> Structure</a></h3>
  369. <p>The <tt>rcu_node</tt> structures form the combining
  370. tree that propagates quiescent-state
  371. information from the leaves to the root and also that propagates
  372. grace-period information from the root down to the leaves.
  373. They provides local copies of the grace-period state in order
  374. to allow this information to be accessed in a synchronized
  375. manner without suffering the scalability limitations that
  376. would otherwise be imposed by global locking.
  377. In <tt>CONFIG_PREEMPT_RCU</tt> kernels, they manage the lists
  378. of tasks that have blocked while in their current
  379. RCU read-side critical section.
  380. In <tt>CONFIG_PREEMPT_RCU</tt> with
  381. <tt>CONFIG_RCU_BOOST</tt>, they manage the
  382. per-<tt>rcu_node</tt> priority-boosting
  383. kernel threads (kthreads) and state.
  384. Finally, they record CPU-hotplug state in order to determine
  385. which CPUs should be ignored during a given grace period.
  386. </p><p>The <tt>rcu_node</tt> structure's fields are discussed,
  387. singly and in groups, in the following sections.
  388. <h5>Connection to Combining Tree</h5>
  389. <p>This portion of the <tt>rcu_node</tt> structure is declared
  390. as follows:
  391. <pre>
  392. 1 struct rcu_node *parent;
  393. 2 u8 level;
  394. 3 u8 grpnum;
  395. 4 unsigned long grpmask;
  396. 5 int grplo;
  397. 6 int grphi;
  398. </pre>
  399. <p>The <tt>-&gt;parent</tt> pointer references the <tt>rcu_node</tt>
  400. one level up in the tree, and is <tt>NULL</tt> for the root
  401. <tt>rcu_node</tt>.
  402. The RCU implementation makes heavy use of this field to push quiescent
  403. states up the tree.
  404. The <tt>-&gt;level</tt> field gives the level in the tree, with
  405. the root being at level zero, its children at level one, and so on.
  406. The <tt>-&gt;grpnum</tt> field gives this node's position within
  407. the children of its parent, so this number can range between 0 and 31
  408. on 32-bit systems and between 0 and 63 on 64-bit systems.
  409. The <tt>-&gt;level</tt> and <tt>-&gt;grpnum</tt> fields are
  410. used only during initialization and for tracing.
  411. The <tt>-&gt;grpmask</tt> field is the bitmask counterpart of
  412. <tt>-&gt;grpnum</tt>, and therefore always has exactly one bit set.
  413. This mask is used to clear the bit corresponding to this <tt>rcu_node</tt>
  414. structure in its parent's bitmasks, which are described later.
  415. Finally, the <tt>-&gt;grplo</tt> and <tt>-&gt;grphi</tt> fields
  416. contain the lowest and highest numbered CPU served by this
  417. <tt>rcu_node</tt> structure, respectively.
  418. </p><p>All of these fields are constant, and thus do not require any
  419. synchronization.
  420. <h5>Synchronization</h5>
  421. <p>This field of the <tt>rcu_node</tt> structure is declared
  422. as follows:
  423. <pre>
  424. 1 raw_spinlock_t lock;
  425. </pre>
  426. <p>This field is used to protect the remaining fields in this structure,
  427. unless otherwise stated.
  428. That said, all of the fields in this structure can be accessed without
  429. locking for tracing purposes.
  430. Yes, this can result in confusing traces, but better some tracing confusion
  431. than to be heisenbugged out of existence.
  432. <h5>Grace-Period Tracking</h5>
  433. <p>This portion of the <tt>rcu_node</tt> structure is declared
  434. as follows:
  435. <pre>
  436. 1 unsigned long gpnum;
  437. 2 unsigned long completed;
  438. </pre>
  439. <p>These fields are the counterparts of the fields of the same name in
  440. the <tt>rcu_state</tt> structure.
  441. They each may lag up to one behind their <tt>rcu_state</tt>
  442. counterparts.
  443. If a given <tt>rcu_node</tt> structure's <tt>-&gt;gpnum</tt> and
  444. <tt>-&gt;complete</tt> fields are equal, then this <tt>rcu_node</tt>
  445. structure believes that RCU is idle.
  446. Otherwise, as with the <tt>rcu_state</tt> structure,
  447. the <tt>-&gt;gpnum</tt> field will be one greater than the
  448. <tt>-&gt;complete</tt> fields, with <tt>-&gt;gpnum</tt>
  449. indicating which grace period this <tt>rcu_node</tt> believes
  450. is still being waited for.
  451. </p><p>The <tt>&gt;gpnum</tt> field of each <tt>rcu_node</tt>
  452. structure is updated at the beginning
  453. of each grace period, and the <tt>-&gt;completed</tt> fields are
  454. updated at the end of each grace period.
  455. <h5>Quiescent-State Tracking</h5>
  456. <p>These fields manage the propagation of quiescent states up the
  457. combining tree.
  458. </p><p>This portion of the <tt>rcu_node</tt> structure has fields
  459. as follows:
  460. <pre>
  461. 1 unsigned long qsmask;
  462. 2 unsigned long expmask;
  463. 3 unsigned long qsmaskinit;
  464. 4 unsigned long expmaskinit;
  465. </pre>
  466. <p>The <tt>-&gt;qsmask</tt> field tracks which of this
  467. <tt>rcu_node</tt> structure's children still need to report
  468. quiescent states for the current normal grace period.
  469. Such children will have a value of 1 in their corresponding bit.
  470. Note that the leaf <tt>rcu_node</tt> structures should be
  471. thought of as having <tt>rcu_data</tt> structures as their
  472. children.
  473. Similarly, the <tt>-&gt;expmask</tt> field tracks which
  474. of this <tt>rcu_node</tt> structure's children still need to report
  475. quiescent states for the current expedited grace period.
  476. An expedited grace period has
  477. the same conceptual properties as a normal grace period, but the
  478. expedited implementation accepts extreme CPU overhead to obtain
  479. much lower grace-period latency, for example, consuming a few
  480. tens of microseconds worth of CPU time to reduce grace-period
  481. duration from milliseconds to tens of microseconds.
  482. The <tt>-&gt;qsmaskinit</tt> field tracks which of this
  483. <tt>rcu_node</tt> structure's children cover for at least
  484. one online CPU.
  485. This mask is used to initialize <tt>-&gt;qsmask</tt>,
  486. and <tt>-&gt;expmaskinit</tt> is used to initialize
  487. <tt>-&gt;expmask</tt> and the beginning of the
  488. normal and expedited grace periods, respectively.
  489. <table>
  490. <tr><th>&nbsp;</th></tr>
  491. <tr><th align="left">Quick Quiz:</th></tr>
  492. <tr><td>
  493. Why are these bitmasks protected by locking?
  494. Come on, haven't you heard of atomic instructions???
  495. </td></tr>
  496. <tr><th align="left">Answer:</th></tr>
  497. <tr><td bgcolor="#ffffff"><font color="ffffff">
  498. Lockless grace-period computation! Such a tantalizing possibility!
  499. </font>
  500. <p><font color="ffffff">But consider the following sequence of events:
  501. </font>
  502. <ol>
  503. <li> <font color="ffffff">CPU&nbsp;0 has been in dyntick-idle
  504. mode for quite some time.
  505. When it wakes up, it notices that the current RCU
  506. grace period needs it to report in, so it sets a
  507. flag where the scheduling clock interrupt will find it.
  508. </font><p>
  509. <li> <font color="ffffff">Meanwhile, CPU&nbsp;1 is running
  510. <tt>force_quiescent_state()</tt>,
  511. and notices that CPU&nbsp;0 has been in dyntick idle mode,
  512. which qualifies as an extended quiescent state.
  513. </font><p>
  514. <li> <font color="ffffff">CPU&nbsp;0's scheduling clock
  515. interrupt fires in the
  516. middle of an RCU read-side critical section, and notices
  517. that the RCU core needs something, so commences RCU softirq
  518. processing.
  519. </font>
  520. <p>
  521. <li> <font color="ffffff">CPU&nbsp;0's softirq handler
  522. executes and is just about ready
  523. to report its quiescent state up the <tt>rcu_node</tt>
  524. tree.
  525. </font><p>
  526. <li> <font color="ffffff">But CPU&nbsp;1 beats it to the punch,
  527. completing the current
  528. grace period and starting a new one.
  529. </font><p>
  530. <li> <font color="ffffff">CPU&nbsp;0 now reports its quiescent
  531. state for the wrong
  532. grace period.
  533. That grace period might now end before the RCU read-side
  534. critical section.
  535. If that happens, disaster will ensue.
  536. </font>
  537. </ol>
  538. <p><font color="ffffff">So the locking is absolutely required in
  539. order to coordinate
  540. clearing of the bits with the grace-period numbers in
  541. <tt>-&gt;gpnum</tt> and <tt>-&gt;completed</tt>.
  542. </font></td></tr>
  543. <tr><td>&nbsp;</td></tr>
  544. </table>
  545. <h5>Blocked-Task Management</h5>
  546. <p><tt>PREEMPT_RCU</tt> allows tasks to be preempted in the
  547. midst of their RCU read-side critical sections, and these tasks
  548. must be tracked explicitly.
  549. The details of exactly why and how they are tracked will be covered
  550. in a separate article on RCU read-side processing.
  551. For now, it is enough to know that the <tt>rcu_node</tt>
  552. structure tracks them.
  553. <pre>
  554. 1 struct list_head blkd_tasks;
  555. 2 struct list_head *gp_tasks;
  556. 3 struct list_head *exp_tasks;
  557. 4 bool wait_blkd_tasks;
  558. </pre>
  559. <p>The <tt>-&gt;blkd_tasks</tt> field is a list header for
  560. the list of blocked and preempted tasks.
  561. As tasks undergo context switches within RCU read-side critical
  562. sections, their <tt>task_struct</tt> structures are enqueued
  563. (via the <tt>task_struct</tt>'s <tt>-&gt;rcu_node_entry</tt>
  564. field) onto the head of the <tt>-&gt;blkd_tasks</tt> list for the
  565. leaf <tt>rcu_node</tt> structure corresponding to the CPU
  566. on which the outgoing context switch executed.
  567. As these tasks later exit their RCU read-side critical sections,
  568. they remove themselves from the list.
  569. This list is therefore in reverse time order, so that if one of the tasks
  570. is blocking the current grace period, all subsequent tasks must
  571. also be blocking that same grace period.
  572. Therefore, a single pointer into this list suffices to track
  573. all tasks blocking a given grace period.
  574. That pointer is stored in <tt>-&gt;gp_tasks</tt> for normal
  575. grace periods and in <tt>-&gt;exp_tasks</tt> for expedited
  576. grace periods.
  577. These last two fields are <tt>NULL</tt> if either there is
  578. no grace period in flight or if there are no blocked tasks
  579. preventing that grace period from completing.
  580. If either of these two pointers is referencing a task that
  581. removes itself from the <tt>-&gt;blkd_tasks</tt> list,
  582. then that task must advance the pointer to the next task on
  583. the list, or set the pointer to <tt>NULL</tt> if there
  584. are no subsequent tasks on the list.
  585. </p><p>For example, suppose that tasks&nbsp;T1, T2, and&nbsp;T3 are
  586. all hard-affinitied to the largest-numbered CPU in the system.
  587. Then if task&nbsp;T1 blocked in an RCU read-side
  588. critical section, then an expedited grace period started,
  589. then task&nbsp;T2 blocked in an RCU read-side critical section,
  590. then a normal grace period started, and finally task&nbsp;3 blocked
  591. in an RCU read-side critical section, then the state of the
  592. last leaf <tt>rcu_node</tt> structure's blocked-task list
  593. would be as shown below:
  594. </p><p><img src="blkd_task.svg" alt="blkd_task.svg" width="60%">
  595. </p><p>Task&nbsp;T1 is blocking both grace periods, task&nbsp;T2 is
  596. blocking only the normal grace period, and task&nbsp;T3 is blocking
  597. neither grace period.
  598. Note that these tasks will not remove themselves from this list
  599. immediately upon resuming execution.
  600. They will instead remain on the list until they execute the outermost
  601. <tt>rcu_read_unlock()</tt> that ends their RCU read-side critical
  602. section.
  603. <p>
  604. The <tt>-&gt;wait_blkd_tasks</tt> field indicates whether or not
  605. the current grace period is waiting on a blocked task.
  606. <h5>Sizing the <tt>rcu_node</tt> Array</h5>
  607. <p>The <tt>rcu_node</tt> array is sized via a series of
  608. C-preprocessor expressions as follows:
  609. <pre>
  610. 1 #ifdef CONFIG_RCU_FANOUT
  611. 2 #define RCU_FANOUT CONFIG_RCU_FANOUT
  612. 3 #else
  613. 4 # ifdef CONFIG_64BIT
  614. 5 # define RCU_FANOUT 64
  615. 6 # else
  616. 7 # define RCU_FANOUT 32
  617. 8 # endif
  618. 9 #endif
  619. 10
  620. 11 #ifdef CONFIG_RCU_FANOUT_LEAF
  621. 12 #define RCU_FANOUT_LEAF CONFIG_RCU_FANOUT_LEAF
  622. 13 #else
  623. 14 # ifdef CONFIG_64BIT
  624. 15 # define RCU_FANOUT_LEAF 64
  625. 16 # else
  626. 17 # define RCU_FANOUT_LEAF 32
  627. 18 # endif
  628. 19 #endif
  629. 20
  630. 21 #define RCU_FANOUT_1 (RCU_FANOUT_LEAF)
  631. 22 #define RCU_FANOUT_2 (RCU_FANOUT_1 * RCU_FANOUT)
  632. 23 #define RCU_FANOUT_3 (RCU_FANOUT_2 * RCU_FANOUT)
  633. 24 #define RCU_FANOUT_4 (RCU_FANOUT_3 * RCU_FANOUT)
  634. 25
  635. 26 #if NR_CPUS &lt;= RCU_FANOUT_1
  636. 27 # define RCU_NUM_LVLS 1
  637. 28 # define NUM_RCU_LVL_0 1
  638. 29 # define NUM_RCU_NODES NUM_RCU_LVL_0
  639. 30 # define NUM_RCU_LVL_INIT { NUM_RCU_LVL_0 }
  640. 31 # define RCU_NODE_NAME_INIT { "rcu_node_0" }
  641. 32 # define RCU_FQS_NAME_INIT { "rcu_node_fqs_0" }
  642. 33 # define RCU_EXP_NAME_INIT { "rcu_node_exp_0" }
  643. 34 #elif NR_CPUS &lt;= RCU_FANOUT_2
  644. 35 # define RCU_NUM_LVLS 2
  645. 36 # define NUM_RCU_LVL_0 1
  646. 37 # define NUM_RCU_LVL_1 DIV_ROUND_UP(NR_CPUS, RCU_FANOUT_1)
  647. 38 # define NUM_RCU_NODES (NUM_RCU_LVL_0 + NUM_RCU_LVL_1)
  648. 39 # define NUM_RCU_LVL_INIT { NUM_RCU_LVL_0, NUM_RCU_LVL_1 }
  649. 40 # define RCU_NODE_NAME_INIT { "rcu_node_0", "rcu_node_1" }
  650. 41 # define RCU_FQS_NAME_INIT { "rcu_node_fqs_0", "rcu_node_fqs_1" }
  651. 42 # define RCU_EXP_NAME_INIT { "rcu_node_exp_0", "rcu_node_exp_1" }
  652. 43 #elif NR_CPUS &lt;= RCU_FANOUT_3
  653. 44 # define RCU_NUM_LVLS 3
  654. 45 # define NUM_RCU_LVL_0 1
  655. 46 # define NUM_RCU_LVL_1 DIV_ROUND_UP(NR_CPUS, RCU_FANOUT_2)
  656. 47 # define NUM_RCU_LVL_2 DIV_ROUND_UP(NR_CPUS, RCU_FANOUT_1)
  657. 48 # define NUM_RCU_NODES (NUM_RCU_LVL_0 + NUM_RCU_LVL_1 + NUM_RCU_LVL_2)
  658. 49 # define NUM_RCU_LVL_INIT { NUM_RCU_LVL_0, NUM_RCU_LVL_1, NUM_RCU_LVL_2 }
  659. 50 # define RCU_NODE_NAME_INIT { "rcu_node_0", "rcu_node_1", "rcu_node_2" }
  660. 51 # define RCU_FQS_NAME_INIT { "rcu_node_fqs_0", "rcu_node_fqs_1", "rcu_node_fqs_2" }
  661. 52 # define RCU_EXP_NAME_INIT { "rcu_node_exp_0", "rcu_node_exp_1", "rcu_node_exp_2" }
  662. 53 #elif NR_CPUS &lt;= RCU_FANOUT_4
  663. 54 # define RCU_NUM_LVLS 4
  664. 55 # define NUM_RCU_LVL_0 1
  665. 56 # define NUM_RCU_LVL_1 DIV_ROUND_UP(NR_CPUS, RCU_FANOUT_3)
  666. 57 # define NUM_RCU_LVL_2 DIV_ROUND_UP(NR_CPUS, RCU_FANOUT_2)
  667. 58 # define NUM_RCU_LVL_3 DIV_ROUND_UP(NR_CPUS, RCU_FANOUT_1)
  668. 59 # define NUM_RCU_NODES (NUM_RCU_LVL_0 + NUM_RCU_LVL_1 + NUM_RCU_LVL_2 + NUM_RCU_LVL_3)
  669. 60 # define NUM_RCU_LVL_INIT { NUM_RCU_LVL_0, NUM_RCU_LVL_1, NUM_RCU_LVL_2, NUM_RCU_LVL_3 }
  670. 61 # define RCU_NODE_NAME_INIT { "rcu_node_0", "rcu_node_1", "rcu_node_2", "rcu_node_3" }
  671. 62 # define RCU_FQS_NAME_INIT { "rcu_node_fqs_0", "rcu_node_fqs_1", "rcu_node_fqs_2", "rcu_node_fqs_3" }
  672. 63 # define RCU_EXP_NAME_INIT { "rcu_node_exp_0", "rcu_node_exp_1", "rcu_node_exp_2", "rcu_node_exp_3" }
  673. 64 #else
  674. 65 # error "CONFIG_RCU_FANOUT insufficient for NR_CPUS"
  675. 66 #endif
  676. </pre>
  677. <p>The maximum number of levels in the <tt>rcu_node</tt> structure
  678. is currently limited to four, as specified by lines&nbsp;21-24
  679. and the structure of the subsequent &ldquo;if&rdquo; statement.
  680. For 32-bit systems, this allows 16*32*32*32=524,288 CPUs, which
  681. should be sufficient for the next few years at least.
  682. For 64-bit systems, 16*64*64*64=4,194,304 CPUs is allowed, which
  683. should see us through the next decade or so.
  684. This four-level tree also allows kernels built with
  685. <tt>CONFIG_RCU_FANOUT=8</tt> to support up to 4096 CPUs,
  686. which might be useful in very large systems having eight CPUs per
  687. socket (but please note that no one has yet shown any measurable
  688. performance degradation due to misaligned socket and <tt>rcu_node</tt>
  689. boundaries).
  690. In addition, building kernels with a full four levels of <tt>rcu_node</tt>
  691. tree permits better testing of RCU's combining-tree code.
  692. </p><p>The <tt>RCU_FANOUT</tt> symbol controls how many children
  693. are permitted at each non-leaf level of the <tt>rcu_node</tt> tree.
  694. If the <tt>CONFIG_RCU_FANOUT</tt> Kconfig option is not specified,
  695. it is set based on the word size of the system, which is also
  696. the Kconfig default.
  697. </p><p>The <tt>RCU_FANOUT_LEAF</tt> symbol controls how many CPUs are
  698. handled by each leaf <tt>rcu_node</tt> structure.
  699. Experience has shown that allowing a given leaf <tt>rcu_node</tt>
  700. structure to handle 64 CPUs, as permitted by the number of bits in
  701. the <tt>-&gt;qsmask</tt> field on a 64-bit system, results in
  702. excessive contention for the leaf <tt>rcu_node</tt> structures'
  703. <tt>-&gt;lock</tt> fields.
  704. The number of CPUs per leaf <tt>rcu_node</tt> structure is therefore
  705. limited to 16 given the default value of <tt>CONFIG_RCU_FANOUT_LEAF</tt>.
  706. If <tt>CONFIG_RCU_FANOUT_LEAF</tt> is unspecified, the value
  707. selected is based on the word size of the system, just as for
  708. <tt>CONFIG_RCU_FANOUT</tt>.
  709. Lines&nbsp;11-19 perform this computation.
  710. </p><p>Lines&nbsp;21-24 compute the maximum number of CPUs supported by
  711. a single-level (which contains a single <tt>rcu_node</tt> structure),
  712. two-level, three-level, and four-level <tt>rcu_node</tt> tree,
  713. respectively, given the fanout specified by <tt>RCU_FANOUT</tt>
  714. and <tt>RCU_FANOUT_LEAF</tt>.
  715. These numbers of CPUs are retained in the
  716. <tt>RCU_FANOUT_1</tt>,
  717. <tt>RCU_FANOUT_2</tt>,
  718. <tt>RCU_FANOUT_3</tt>, and
  719. <tt>RCU_FANOUT_4</tt>
  720. C-preprocessor variables, respectively.
  721. </p><p>These variables are used to control the C-preprocessor <tt>#if</tt>
  722. statement spanning lines&nbsp;26-66 that computes the number of
  723. <tt>rcu_node</tt> structures required for each level of the tree,
  724. as well as the number of levels required.
  725. The number of levels is placed in the <tt>NUM_RCU_LVLS</tt>
  726. C-preprocessor variable by lines&nbsp;27, 35, 44, and&nbsp;54.
  727. The number of <tt>rcu_node</tt> structures for the topmost level
  728. of the tree is always exactly one, and this value is unconditionally
  729. placed into <tt>NUM_RCU_LVL_0</tt> by lines&nbsp;28, 36, 45, and&nbsp;55.
  730. The rest of the levels (if any) of the <tt>rcu_node</tt> tree
  731. are computed by dividing the maximum number of CPUs by the
  732. fanout supported by the number of levels from the current level down,
  733. rounding up. This computation is performed by lines&nbsp;37,
  734. 46-47, and&nbsp;56-58.
  735. Lines&nbsp;31-33, 40-42, 50-52, and&nbsp;62-63 create initializers
  736. for lockdep lock-class names.
  737. Finally, lines&nbsp;64-66 produce an error if the maximum number of
  738. CPUs is too large for the specified fanout.
  739. <h3><a name="The rcu_data Structure">
  740. The <tt>rcu_data</tt> Structure</a></h3>
  741. <p>The <tt>rcu_data</tt> maintains the per-CPU state for the
  742. corresponding flavor of RCU.
  743. The fields in this structure may be accessed only from the corresponding
  744. CPU (and from tracing) unless otherwise stated.
  745. This structure is the
  746. focus of quiescent-state detection and RCU callback queuing.
  747. It also tracks its relationship to the corresponding leaf
  748. <tt>rcu_node</tt> structure to allow more-efficient
  749. propagation of quiescent states up the <tt>rcu_node</tt>
  750. combining tree.
  751. Like the <tt>rcu_node</tt> structure, it provides a local
  752. copy of the grace-period information to allow for-free
  753. synchronized
  754. access to this information from the corresponding CPU.
  755. Finally, this structure records past dyntick-idle state
  756. for the corresponding CPU and also tracks statistics.
  757. </p><p>The <tt>rcu_data</tt> structure's fields are discussed,
  758. singly and in groups, in the following sections.
  759. <h5>Connection to Other Data Structures</h5>
  760. <p>This portion of the <tt>rcu_data</tt> structure is declared
  761. as follows:
  762. <pre>
  763. 1 int cpu;
  764. 2 struct rcu_state *rsp;
  765. 3 struct rcu_node *mynode;
  766. 4 struct rcu_dynticks *dynticks;
  767. 5 unsigned long grpmask;
  768. 6 bool beenonline;
  769. </pre>
  770. <p>The <tt>-&gt;cpu</tt> field contains the number of the
  771. corresponding CPU, the <tt>-&gt;rsp</tt> pointer references
  772. the corresponding <tt>rcu_state</tt> structure (and is most frequently
  773. used to locate the name of the corresponding flavor of RCU for tracing),
  774. and the <tt>-&gt;mynode</tt> field references the corresponding
  775. <tt>rcu_node</tt> structure.
  776. The <tt>-&gt;mynode</tt> is used to propagate quiescent states
  777. up the combining tree.
  778. <p>The <tt>-&gt;dynticks</tt> pointer references the
  779. <tt>rcu_dynticks</tt> structure corresponding to this
  780. CPU.
  781. Recall that a single per-CPU instance of the <tt>rcu_dynticks</tt>
  782. structure is shared among all flavors of RCU.
  783. These first four fields are constant and therefore require not
  784. synchronization.
  785. </p><p>The <tt>-&gt;grpmask</tt> field indicates the bit in
  786. the <tt>-&gt;mynode-&gt;qsmask</tt> corresponding to this
  787. <tt>rcu_data</tt> structure, and is also used when propagating
  788. quiescent states.
  789. The <tt>-&gt;beenonline</tt> flag is set whenever the corresponding
  790. CPU comes online, which means that the debugfs tracing need not dump
  791. out any <tt>rcu_data</tt> structure for which this flag is not set.
  792. <h5>Quiescent-State and Grace-Period Tracking</h5>
  793. <p>This portion of the <tt>rcu_data</tt> structure is declared
  794. as follows:
  795. <pre>
  796. 1 unsigned long completed;
  797. 2 unsigned long gpnum;
  798. 3 bool cpu_no_qs;
  799. 4 bool core_needs_qs;
  800. 5 bool gpwrap;
  801. 6 unsigned long rcu_qs_ctr_snap;
  802. </pre>
  803. <p>The <tt>completed</tt> and <tt>gpnum</tt>
  804. fields are the counterparts of the fields of the same name
  805. in the <tt>rcu_state</tt> and <tt>rcu_node</tt> structures.
  806. They may each lag up to one behind their <tt>rcu_node</tt>
  807. counterparts, but in <tt>CONFIG_NO_HZ_IDLE</tt> and
  808. <tt>CONFIG_NO_HZ_FULL</tt> kernels can lag
  809. arbitrarily far behind for CPUs in dyntick-idle mode (but these counters
  810. will catch up upon exit from dyntick-idle mode).
  811. If a given <tt>rcu_data</tt> structure's <tt>-&gt;gpnum</tt> and
  812. <tt>-&gt;complete</tt> fields are equal, then this <tt>rcu_data</tt>
  813. structure believes that RCU is idle.
  814. Otherwise, as with the <tt>rcu_state</tt> and <tt>rcu_node</tt>
  815. structure,
  816. the <tt>-&gt;gpnum</tt> field will be one greater than the
  817. <tt>-&gt;complete</tt> fields, with <tt>-&gt;gpnum</tt>
  818. indicating which grace period this <tt>rcu_data</tt> believes
  819. is still being waited for.
  820. <table>
  821. <tr><th>&nbsp;</th></tr>
  822. <tr><th align="left">Quick Quiz:</th></tr>
  823. <tr><td>
  824. All this replication of the grace period numbers can only cause
  825. massive confusion.
  826. Why not just keep a global pair of counters and be done with it???
  827. </td></tr>
  828. <tr><th align="left">Answer:</th></tr>
  829. <tr><td bgcolor="#ffffff"><font color="ffffff">
  830. Because if there was only a single global pair of grace-period
  831. numbers, there would need to be a single global lock to allow
  832. safely accessing and updating them.
  833. And if we are not going to have a single global lock, we need
  834. to carefully manage the numbers on a per-node basis.
  835. Recall from the answer to a previous Quick Quiz that the consequences
  836. of applying a previously sampled quiescent state to the wrong
  837. grace period are quite severe.
  838. </font></td></tr>
  839. <tr><td>&nbsp;</td></tr>
  840. </table>
  841. <p>The <tt>-&gt;cpu_no_qs</tt> flag indicates that the
  842. CPU has not yet passed through a quiescent state,
  843. while the <tt>-&gt;core_needs_qs</tt> flag indicates that the
  844. RCU core needs a quiescent state from the corresponding CPU.
  845. The <tt>-&gt;gpwrap</tt> field indicates that the corresponding
  846. CPU has remained idle for so long that the <tt>completed</tt>
  847. and <tt>gpnum</tt> counters are in danger of overflow, which
  848. will cause the CPU to disregard the values of its counters on
  849. its next exit from idle.
  850. Finally, the <tt>rcu_qs_ctr_snap</tt> field is used to detect
  851. cases where a given operation has resulted in a quiescent state
  852. for all flavors of RCU, for example, <tt>cond_resched_rcu_qs()</tt>.
  853. <h5>RCU Callback Handling</h5>
  854. <p>In the absence of CPU-hotplug events, RCU callbacks are invoked by
  855. the same CPU that registered them.
  856. This is strictly a cache-locality optimization: callbacks can and
  857. do get invoked on CPUs other than the one that registered them.
  858. After all, if the CPU that registered a given callback has gone
  859. offline before the callback can be invoked, there really is no other
  860. choice.
  861. </p><p>This portion of the <tt>rcu_data</tt> structure is declared
  862. as follows:
  863. <pre>
  864. 1 struct rcu_head *nxtlist;
  865. 2 struct rcu_head **nxttail[RCU_NEXT_SIZE];
  866. 3 unsigned long nxtcompleted[RCU_NEXT_SIZE];
  867. 4 long qlen_lazy;
  868. 5 long qlen;
  869. 6 long qlen_last_fqs_check;
  870. 7 unsigned long n_force_qs_snap;
  871. 8 unsigned long n_cbs_invoked;
  872. 9 unsigned long n_cbs_orphaned;
  873. 10 unsigned long n_cbs_adopted;
  874. 11 long blimit;
  875. </pre>
  876. <p>The <tt>-&gt;nxtlist</tt> pointer and the
  877. <tt>-&gt;nxttail[]</tt> array form a four-segment list with
  878. older callbacks near the head and newer ones near the tail.
  879. Each segment contains callbacks with the corresponding relationship
  880. to the current grace period.
  881. The pointer out of the end of each of the four segments is referenced
  882. by the element of the <tt>-&gt;nxttail[]</tt> array indexed by
  883. <tt>RCU_DONE_TAIL</tt> (for callbacks handled by a prior grace period),
  884. <tt>RCU_WAIT_TAIL</tt> (for callbacks waiting on the current grace period),
  885. <tt>RCU_NEXT_READY_TAIL</tt> (for callbacks that will wait on the next
  886. grace period), and
  887. <tt>RCU_NEXT_TAIL</tt> (for callbacks that are not yet associated
  888. with a specific grace period)
  889. respectively, as shown in the following figure.
  890. </p><p><img src="nxtlist.svg" alt="nxtlist.svg" width="40%">
  891. </p><p>In this figure, the <tt>-&gt;nxtlist</tt> pointer references the
  892. first
  893. RCU callback in the list.
  894. The <tt>-&gt;nxttail[RCU_DONE_TAIL]</tt> array element references
  895. the <tt>-&gt;nxtlist</tt> pointer itself, indicating that none
  896. of the callbacks is ready to invoke.
  897. The <tt>-&gt;nxttail[RCU_WAIT_TAIL]</tt> array element references callback
  898. CB&nbsp;2's <tt>-&gt;next</tt> pointer, which indicates that
  899. CB&nbsp;1 and CB&nbsp;2 are both waiting on the current grace period.
  900. The <tt>-&gt;nxttail[RCU_NEXT_READY_TAIL]</tt> array element
  901. references the same RCU callback that <tt>-&gt;nxttail[RCU_WAIT_TAIL]</tt>
  902. does, which indicates that there are no callbacks waiting on the next
  903. RCU grace period.
  904. The <tt>-&gt;nxttail[RCU_NEXT_TAIL]</tt> array element references
  905. CB&nbsp;4's <tt>-&gt;next</tt> pointer, indicating that all the
  906. remaining RCU callbacks have not yet been assigned to an RCU grace
  907. period.
  908. Note that the <tt>-&gt;nxttail[RCU_NEXT_TAIL]</tt> array element
  909. always references the last RCU callback's <tt>-&gt;next</tt> pointer
  910. unless the callback list is empty, in which case it references
  911. the <tt>-&gt;nxtlist</tt> pointer.
  912. </p><p>CPUs advance their callbacks from the
  913. <tt>RCU_NEXT_TAIL</tt> to the <tt>RCU_NEXT_READY_TAIL</tt> to the
  914. <tt>RCU_WAIT_TAIL</tt> to the <tt>RCU_DONE_TAIL</tt> list segments
  915. as grace periods advance.
  916. The CPU advances the callbacks in its <tt>rcu_data</tt> structure
  917. whenever it notices that another RCU grace period has completed.
  918. The CPU detects the completion of an RCU grace period by noticing
  919. that the value of its <tt>rcu_data</tt> structure's
  920. <tt>-&gt;completed</tt> field differs from that of its leaf
  921. <tt>rcu_node</tt> structure.
  922. Recall that each <tt>rcu_node</tt> structure's
  923. <tt>-&gt;completed</tt> field is updated at the end of each
  924. grace period.
  925. </p><p>The <tt>-&gt;nxtcompleted[]</tt> array records grace-period
  926. numbers corresponding to the list segments.
  927. This allows CPUs that go idle for extended periods to determine
  928. which of their callbacks are ready to be invoked after reawakening.
  929. </p><p>The <tt>-&gt;qlen</tt> counter contains the number of
  930. callbacks in <tt>-&gt;nxtlist</tt>, and the
  931. <tt>-&gt;qlen_lazy</tt> contains the number of those callbacks that
  932. are known to only free memory, and whose invocation can therefore
  933. be safely deferred.
  934. The <tt>-&gt;qlen_last_fqs_check</tt> and
  935. <tt>-&gt;n_force_qs_snap</tt> coordinate the forcing of quiescent
  936. states from <tt>call_rcu()</tt> and friends when callback
  937. lists grow excessively long.
  938. </p><p>The <tt>-&gt;n_cbs_invoked</tt>,
  939. <tt>-&gt;n_cbs_orphaned</tt>, and <tt>-&gt;n_cbs_adopted</tt>
  940. fields count the number of callbacks invoked,
  941. sent to other CPUs when this CPU goes offline,
  942. and received from other CPUs when those other CPUs go offline.
  943. Finally, the <tt>-&gt;blimit</tt> counter is the maximum number of
  944. RCU callbacks that may be invoked at a given time.
  945. <h5>Dyntick-Idle Handling</h5>
  946. <p>This portion of the <tt>rcu_data</tt> structure is declared
  947. as follows:
  948. <pre>
  949. 1 int dynticks_snap;
  950. 2 unsigned long dynticks_fqs;
  951. </pre>
  952. The <tt>-&gt;dynticks_snap</tt> field is used to take a snapshot
  953. of the corresponding CPU's dyntick-idle state when forcing
  954. quiescent states, and is therefore accessed from other CPUs.
  955. Finally, the <tt>-&gt;dynticks_fqs</tt> field is used to
  956. count the number of times this CPU is determined to be in
  957. dyntick-idle state, and is used for tracing and debugging purposes.
  958. <h3><a name="The rcu_dynticks Structure">
  959. The <tt>rcu_dynticks</tt> Structure</a></h3>
  960. <p>The <tt>rcu_dynticks</tt> maintains the per-CPU dyntick-idle state
  961. for the corresponding CPU.
  962. Unlike the other structures, <tt>rcu_dynticks</tt> is not
  963. replicated over the different flavors of RCU.
  964. The fields in this structure may be accessed only from the corresponding
  965. CPU (and from tracing) unless otherwise stated.
  966. Its fields are as follows:
  967. <pre>
  968. 1 int dynticks_nesting;
  969. 2 int dynticks_nmi_nesting;
  970. 3 atomic_t dynticks;
  971. </pre>
  972. <p>The <tt>-&gt;dynticks_nesting</tt> field counts the
  973. nesting depth of normal interrupts.
  974. In addition, this counter is incremented when exiting dyntick-idle
  975. mode and decremented when entering it.
  976. This counter can therefore be thought of as counting the number
  977. of reasons why this CPU cannot be permitted to enter dyntick-idle
  978. mode, aside from non-maskable interrupts (NMIs).
  979. NMIs are counted by the <tt>-&gt;dynticks_nmi_nesting</tt>
  980. field, except that NMIs that interrupt non-dyntick-idle execution
  981. are not counted.
  982. </p><p>Finally, the <tt>-&gt;dynticks</tt> field counts the corresponding
  983. CPU's transitions to and from dyntick-idle mode, so that this counter
  984. has an even value when the CPU is in dyntick-idle mode and an odd
  985. value otherwise.
  986. <table>
  987. <tr><th>&nbsp;</th></tr>
  988. <tr><th align="left">Quick Quiz:</th></tr>
  989. <tr><td>
  990. Why not just count all NMIs?
  991. Wouldn't that be simpler and less error prone?
  992. </td></tr>
  993. <tr><th align="left">Answer:</th></tr>
  994. <tr><td bgcolor="#ffffff"><font color="ffffff">
  995. It seems simpler only until you think hard about how to go about
  996. updating the <tt>rcu_dynticks</tt> structure's
  997. <tt>-&gt;dynticks</tt> field.
  998. </font></td></tr>
  999. <tr><td>&nbsp;</td></tr>
  1000. </table>
  1001. <p>Additional fields are present for some special-purpose
  1002. builds, and are discussed separately.
  1003. <h3><a name="The rcu_head Structure">
  1004. The <tt>rcu_head</tt> Structure</a></h3>
  1005. <p>Each <tt>rcu_head</tt> structure represents an RCU callback.
  1006. These structures are normally embedded within RCU-protected data
  1007. structures whose algorithms use asynchronous grace periods.
  1008. In contrast, when using algorithms that block waiting for RCU grace periods,
  1009. RCU users need not provide <tt>rcu_head</tt> structures.
  1010. </p><p>The <tt>rcu_head</tt> structure has fields as follows:
  1011. <pre>
  1012. 1 struct rcu_head *next;
  1013. 2 void (*func)(struct rcu_head *head);
  1014. </pre>
  1015. <p>The <tt>-&gt;next</tt> field is used
  1016. to link the <tt>rcu_head</tt> structures together in the
  1017. lists within the <tt>rcu_data</tt> structures.
  1018. The <tt>-&gt;func</tt> field is a pointer to the function
  1019. to be called when the callback is ready to be invoked, and
  1020. this function is passed a pointer to the <tt>rcu_head</tt>
  1021. structure.
  1022. However, <tt>kfree_rcu()</tt> uses the <tt>-&gt;func</tt>
  1023. field to record the offset of the <tt>rcu_head</tt>
  1024. structure within the enclosing RCU-protected data structure.
  1025. </p><p>Both of these fields are used internally by RCU.
  1026. From the viewpoint of RCU users, this structure is an
  1027. opaque &ldquo;cookie&rdquo;.
  1028. <table>
  1029. <tr><th>&nbsp;</th></tr>
  1030. <tr><th align="left">Quick Quiz:</th></tr>
  1031. <tr><td>
  1032. Given that the callback function <tt>-&gt;func</tt>
  1033. is passed a pointer to the <tt>rcu_head</tt> structure,
  1034. how is that function supposed to find the beginning of the
  1035. enclosing RCU-protected data structure?
  1036. </td></tr>
  1037. <tr><th align="left">Answer:</th></tr>
  1038. <tr><td bgcolor="#ffffff"><font color="ffffff">
  1039. In actual practice, there is a separate callback function per
  1040. type of RCU-protected data structure.
  1041. The callback function can therefore use the <tt>container_of()</tt>
  1042. macro in the Linux kernel (or other pointer-manipulation facilities
  1043. in other software environments) to find the beginning of the
  1044. enclosing structure.
  1045. </font></td></tr>
  1046. <tr><td>&nbsp;</td></tr>
  1047. </table>
  1048. <h3><a name="RCU-Specific Fields in the task_struct Structure">
  1049. RCU-Specific Fields in the <tt>task_struct</tt> Structure</a></h3>
  1050. <p>The <tt>CONFIG_PREEMPT_RCU</tt> implementation uses some
  1051. additional fields in the <tt>task_struct</tt> structure:
  1052. <pre>
  1053. 1 #ifdef CONFIG_PREEMPT_RCU
  1054. 2 int rcu_read_lock_nesting;
  1055. 3 union rcu_special rcu_read_unlock_special;
  1056. 4 struct list_head rcu_node_entry;
  1057. 5 struct rcu_node *rcu_blocked_node;
  1058. 6 #endif /* #ifdef CONFIG_PREEMPT_RCU */
  1059. 7 #ifdef CONFIG_TASKS_RCU
  1060. 8 unsigned long rcu_tasks_nvcsw;
  1061. 9 bool rcu_tasks_holdout;
  1062. 10 struct list_head rcu_tasks_holdout_list;
  1063. 11 int rcu_tasks_idle_cpu;
  1064. 12 #endif /* #ifdef CONFIG_TASKS_RCU */
  1065. </pre>
  1066. <p>The <tt>-&gt;rcu_read_lock_nesting</tt> field records the
  1067. nesting level for RCU read-side critical sections, and
  1068. the <tt>-&gt;rcu_read_unlock_special</tt> field is a bitmask
  1069. that records special conditions that require <tt>rcu_read_unlock()</tt>
  1070. to do additional work.
  1071. The <tt>-&gt;rcu_node_entry</tt> field is used to form lists of
  1072. tasks that have blocked within preemptible-RCU read-side critical
  1073. sections and the <tt>-&gt;rcu_blocked_node</tt> field references
  1074. the <tt>rcu_node</tt> structure whose list this task is a member of,
  1075. or <tt>NULL</tt> if it is not blocked within a preemptible-RCU
  1076. read-side critical section.
  1077. <p>The <tt>-&gt;rcu_tasks_nvcsw</tt> field tracks the number of
  1078. voluntary context switches that this task had undergone at the
  1079. beginning of the current tasks-RCU grace period,
  1080. <tt>-&gt;rcu_tasks_holdout</tt> is set if the current tasks-RCU
  1081. grace period is waiting on this task, <tt>-&gt;rcu_tasks_holdout_list</tt>
  1082. is a list element enqueuing this task on the holdout list,
  1083. and <tt>-&gt;rcu_tasks_idle_cpu</tt> tracks which CPU this
  1084. idle task is running, but only if the task is currently running,
  1085. that is, if the CPU is currently idle.
  1086. <h3><a name="Accessor Functions">
  1087. Accessor Functions</a></h3>
  1088. <p>The following listing shows the
  1089. <tt>rcu_get_root()</tt>, <tt>rcu_for_each_node_breadth_first</tt>,
  1090. <tt>rcu_for_each_nonleaf_node_breadth_first()</tt>, and
  1091. <tt>rcu_for_each_leaf_node()</tt> function and macros:
  1092. <pre>
  1093. 1 static struct rcu_node *rcu_get_root(struct rcu_state *rsp)
  1094. 2 {
  1095. 3 return &amp;rsp-&gt;node[0];
  1096. 4 }
  1097. 5
  1098. 6 #define rcu_for_each_node_breadth_first(rsp, rnp) \
  1099. 7 for ((rnp) = &amp;(rsp)-&gt;node[0]; \
  1100. 8 (rnp) &lt; &amp;(rsp)-&gt;node[NUM_RCU_NODES]; (rnp)++)
  1101. 9
  1102. 10 #define rcu_for_each_nonleaf_node_breadth_first(rsp, rnp) \
  1103. 11 for ((rnp) = &amp;(rsp)-&gt;node[0]; \
  1104. 12 (rnp) &lt; (rsp)-&gt;level[NUM_RCU_LVLS - 1]; (rnp)++)
  1105. 13
  1106. 14 #define rcu_for_each_leaf_node(rsp, rnp) \
  1107. 15 for ((rnp) = (rsp)-&gt;level[NUM_RCU_LVLS - 1]; \
  1108. 16 (rnp) &lt; &amp;(rsp)-&gt;node[NUM_RCU_NODES]; (rnp)++)
  1109. </pre>
  1110. <p>The <tt>rcu_get_root()</tt> simply returns a pointer to the
  1111. first element of the specified <tt>rcu_state</tt> structure's
  1112. <tt>-&gt;node[]</tt> array, which is the root <tt>rcu_node</tt>
  1113. structure.
  1114. </p><p>As noted earlier, the <tt>rcu_for_each_node_breadth_first()</tt>
  1115. macro takes advantage of the layout of the <tt>rcu_node</tt>
  1116. structures in the <tt>rcu_state</tt> structure's
  1117. <tt>-&gt;node[]</tt> array, performing a breadth-first traversal by
  1118. simply traversing the array in order.
  1119. The <tt>rcu_for_each_nonleaf_node_breadth_first()</tt> macro operates
  1120. similarly, but traverses only the first part of the array, thus excluding
  1121. the leaf <tt>rcu_node</tt> structures.
  1122. Finally, the <tt>rcu_for_each_leaf_node()</tt> macro traverses only
  1123. the last part of the array, thus traversing only the leaf
  1124. <tt>rcu_node</tt> structures.
  1125. <table>
  1126. <tr><th>&nbsp;</th></tr>
  1127. <tr><th align="left">Quick Quiz:</th></tr>
  1128. <tr><td>
  1129. What do <tt>rcu_for_each_nonleaf_node_breadth_first()</tt> and
  1130. <tt>rcu_for_each_leaf_node()</tt> do if the <tt>rcu_node</tt> tree
  1131. contains only a single node?
  1132. </td></tr>
  1133. <tr><th align="left">Answer:</th></tr>
  1134. <tr><td bgcolor="#ffffff"><font color="ffffff">
  1135. In the single-node case,
  1136. <tt>rcu_for_each_nonleaf_node_breadth_first()</tt> is a no-op
  1137. and <tt>rcu_for_each_leaf_node()</tt> traverses the single node.
  1138. </font></td></tr>
  1139. <tr><td>&nbsp;</td></tr>
  1140. </table>
  1141. <h3><a name="Summary">
  1142. Summary</a></h3>
  1143. So each flavor of RCU is represented by an <tt>rcu_state</tt> structure,
  1144. which contains a combining tree of <tt>rcu_node</tt> and
  1145. <tt>rcu_data</tt> structures.
  1146. Finally, in <tt>CONFIG_NO_HZ_IDLE</tt> kernels, each CPU's dyntick-idle
  1147. state is tracked by an <tt>rcu_dynticks</tt> structure.
  1148. If you made it this far, you are well prepared to read the code
  1149. walkthroughs in the other articles in this series.
  1150. <h3><a name="Acknowledgments">
  1151. Acknowledgments</a></h3>
  1152. I owe thanks to Cyrill Gorcunov, Mathieu Desnoyers, Dhaval Giani, Paul
  1153. Turner, Abhishek Srivastava, Matt Kowalczyk, and Serge Hallyn
  1154. for helping me get this document into a more human-readable state.
  1155. <h3><a name="Legal Statement">
  1156. Legal Statement</a></h3>
  1157. <p>This work represents the view of the author and does not necessarily
  1158. represent the view of IBM.
  1159. </p><p>Linux is a registered trademark of Linus Torvalds.
  1160. </p><p>Other company, product, and service names may be trademarks or
  1161. service marks of others.
  1162. </body></html>