Data-Structures.html 59 KB

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