Data-Structures.html 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334
  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>January 27, 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. At the end we have the
  30. <a href="#Answers to Quick Quizzes">answers to the quick quizzes</a>.
  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 gpnum;
  329. 2 unsigned long completed;
  330. </pre>
  331. <p>RCU grace periods are numbered, and
  332. the <tt>-&gt;gpnum</tt> field contains the number of the grace
  333. period that started most recently.
  334. The <tt>-&gt;completed</tt> field contains the number of the
  335. grace period that completed most recently.
  336. If the two fields are equal, the RCU grace period that most recently
  337. started has already completed, and therefore the corresponding
  338. flavor of RCU is idle.
  339. If <tt>-&gt;gpnum</tt> is one greater than <tt>-&gt;completed</tt>,
  340. then <tt>-&gt;gpnum</tt> gives the number of the current RCU
  341. grace period, which has not yet completed.
  342. Any other combination of values indicates that something is broken.
  343. These two fields are protected by the root <tt>rcu_node</tt>'s
  344. <tt>-&gt;lock</tt> field.
  345. </p><p>There are <tt>-&gt;gpnum</tt> and <tt>-&gt;completed</tt> fields
  346. in the <tt>rcu_node</tt> and <tt>rcu_data</tt> structures
  347. as well.
  348. The fields in the <tt>rcu_state</tt> structure represent the
  349. most current values, and those of the other structures are compared
  350. in order to detect the start of a new grace period in a distributed
  351. fashion.
  352. The values flow from <tt>rcu_state</tt> to <tt>rcu_node</tt>
  353. (down the tree from the root to the leaves) to <tt>rcu_data</tt>.
  354. <h5>Miscellaneous</h5>
  355. <p>This portion of the <tt>rcu_state</tt> structure is declared
  356. as follows:
  357. <pre>
  358. 1 unsigned long gp_max;
  359. 2 char abbr;
  360. 3 char *name;
  361. </pre>
  362. <p>The <tt>-&gt;gp_max</tt> field tracks the duration of the longest
  363. grace period in jiffies.
  364. It is protected by the root <tt>rcu_node</tt>'s <tt>-&gt;lock</tt>.
  365. <p>The <tt>-&gt;name</tt> field points to the name of the RCU flavor
  366. (for example, &ldquo;rcu_sched&rdquo;), and is constant.
  367. The <tt>-&gt;abbr</tt> field contains a one-character abbreviation,
  368. for example, &ldquo;s&rdquo; for RCU-sched.
  369. <h3><a name="The rcu_node Structure">
  370. The <tt>rcu_node</tt> Structure</a></h3>
  371. <p>The <tt>rcu_node</tt> structures form the combining
  372. tree that propagates quiescent-state
  373. information from the leaves to the root and also that propagates
  374. grace-period information from the root down to the leaves.
  375. They provides local copies of the grace-period state in order
  376. to allow this information to be accessed in a synchronized
  377. manner without suffering the scalability limitations that
  378. would otherwise be imposed by global locking.
  379. In <tt>CONFIG_PREEMPT_RCU</tt> kernels, they manage the lists
  380. of tasks that have blocked while in their current
  381. RCU read-side critical section.
  382. In <tt>CONFIG_PREEMPT_RCU</tt> with
  383. <tt>CONFIG_RCU_BOOST</tt>, they manage the
  384. per-<tt>rcu_node</tt> priority-boosting
  385. kernel threads (kthreads) and state.
  386. Finally, they record CPU-hotplug state in order to determine
  387. which CPUs should be ignored during a given grace period.
  388. </p><p>The <tt>rcu_node</tt> structure's fields are discussed,
  389. singly and in groups, in the following sections.
  390. <h5>Connection to Combining Tree</h5>
  391. <p>This portion of the <tt>rcu_node</tt> structure is declared
  392. as follows:
  393. <pre>
  394. 1 struct rcu_node *parent;
  395. 2 u8 level;
  396. 3 u8 grpnum;
  397. 4 unsigned long grpmask;
  398. 5 int grplo;
  399. 6 int grphi;
  400. </pre>
  401. <p>The <tt>-&gt;parent</tt> pointer references the <tt>rcu_node</tt>
  402. one level up in the tree, and is <tt>NULL</tt> for the root
  403. <tt>rcu_node</tt>.
  404. The RCU implementation makes heavy use of this field to push quiescent
  405. states up the tree.
  406. The <tt>-&gt;level</tt> field gives the level in the tree, with
  407. the root being at level zero, its children at level one, and so on.
  408. The <tt>-&gt;grpnum</tt> field gives this node's position within
  409. the children of its parent, so this number can range between 0 and 31
  410. on 32-bit systems and between 0 and 63 on 64-bit systems.
  411. The <tt>-&gt;level</tt> and <tt>-&gt;grpnum</tt> fields are
  412. used only during initialization and for tracing.
  413. The <tt>-&gt;grpmask</tt> field is the bitmask counterpart of
  414. <tt>-&gt;grpnum</tt>, and therefore always has exactly one bit set.
  415. This mask is used to clear the bit corresponding to this <tt>rcu_node</tt>
  416. structure in its parent's bitmasks, which are described later.
  417. Finally, the <tt>-&gt;grplo</tt> and <tt>-&gt;grphi</tt> fields
  418. contain the lowest and highest numbered CPU served by this
  419. <tt>rcu_node</tt> structure, respectively.
  420. </p><p>All of these fields are constant, and thus do not require any
  421. synchronization.
  422. <h5>Synchronization</h5>
  423. <p>This field of the <tt>rcu_node</tt> structure is declared
  424. as follows:
  425. <pre>
  426. 1 raw_spinlock_t lock;
  427. </pre>
  428. <p>This field is used to protect the remaining fields in this structure,
  429. unless otherwise stated.
  430. That said, all of the fields in this structure can be accessed without
  431. locking for tracing purposes.
  432. Yes, this can result in confusing traces, but better some tracing confusion
  433. than to be heisenbugged out of existence.
  434. <h5>Grace-Period Tracking</h5>
  435. <p>This portion of the <tt>rcu_node</tt> structure is declared
  436. as follows:
  437. <pre>
  438. 1 unsigned long gpnum;
  439. 2 unsigned long completed;
  440. </pre>
  441. <p>These fields are the counterparts of the fields of the same name in
  442. the <tt>rcu_state</tt> structure.
  443. They each may lag up to one behind their <tt>rcu_state</tt>
  444. counterparts.
  445. If a given <tt>rcu_node</tt> structure's <tt>-&gt;gpnum</tt> and
  446. <tt>-&gt;complete</tt> fields are equal, then this <tt>rcu_node</tt>
  447. structure believes that RCU is idle.
  448. Otherwise, as with the <tt>rcu_state</tt> structure,
  449. the <tt>-&gt;gpnum</tt> field will be one greater than the
  450. <tt>-&gt;complete</tt> fields, with <tt>-&gt;gpnum</tt>
  451. indicating which grace period this <tt>rcu_node</tt> believes
  452. is still being waited for.
  453. </p><p>The <tt>&gt;gpnum</tt> field of each <tt>rcu_node</tt>
  454. structure is updated at the beginning
  455. of each grace period, and the <tt>-&gt;completed</tt> fields are
  456. updated at the end of each grace period.
  457. <h5>Quiescent-State Tracking</h5>
  458. <p>These fields manage the propagation of quiescent states up the
  459. combining tree.
  460. </p><p>This portion of the <tt>rcu_node</tt> structure has fields
  461. as follows:
  462. <pre>
  463. 1 unsigned long qsmask;
  464. 2 unsigned long expmask;
  465. 3 unsigned long qsmaskinit;
  466. 4 unsigned long expmaskinit;
  467. </pre>
  468. <p>The <tt>-&gt;qsmask</tt> field tracks which of this
  469. <tt>rcu_node</tt> structure's children still need to report
  470. quiescent states for the current normal grace period.
  471. Such children will have a value of 1 in their corresponding bit.
  472. Note that the leaf <tt>rcu_node</tt> structures should be
  473. thought of as having <tt>rcu_data</tt> structures as their
  474. children.
  475. Similarly, the <tt>-&gt;expmask</tt> field tracks which
  476. of this <tt>rcu_node</tt> structure's children still need to report
  477. quiescent states for the current expedited grace period.
  478. An expedited grace period has
  479. the same conceptual properties as a normal grace period, but the
  480. expedited implementation accepts extreme CPU overhead to obtain
  481. much lower grace-period latency, for example, consuming a few
  482. tens of microseconds worth of CPU time to reduce grace-period
  483. duration from milliseconds to tens of microseconds.
  484. The <tt>-&gt;qsmaskinit</tt> field tracks which of this
  485. <tt>rcu_node</tt> structure's children cover for at least
  486. one online CPU.
  487. This mask is used to initialize <tt>-&gt;qsmask</tt>,
  488. and <tt>-&gt;expmaskinit</tt> is used to initialize
  489. <tt>-&gt;expmask</tt> and the beginning of the
  490. normal and expedited grace periods, respectively.
  491. <table>
  492. <tr><th>&nbsp;</th></tr>
  493. <tr><th align="left">Quick Quiz:</th></tr>
  494. <tr><td>
  495. Why are these bitmasks protected by locking?
  496. Come on, haven't you heard of atomic instructions???
  497. </td></tr>
  498. <tr><th align="left">Answer:</th></tr>
  499. <tr><td bgcolor="#ffffff"><font color="ffffff">
  500. Lockless grace-period computation! Such a tantalizing possibility!
  501. </font>
  502. <p><font color="ffffff">But consider the following sequence of events:
  503. </font>
  504. <ol>
  505. <li> <font color="ffffff">CPU&nbsp;0 has been in dyntick-idle
  506. mode for quite some time.
  507. When it wakes up, it notices that the current RCU
  508. grace period needs it to report in, so it sets a
  509. flag where the scheduling clock interrupt will find it.
  510. </font><p>
  511. <li> <font color="ffffff">Meanwhile, CPU&nbsp;1 is running
  512. <tt>force_quiescent_state()</tt>,
  513. and notices that CPU&nbsp;0 has been in dyntick idle mode,
  514. which qualifies as an extended quiescent state.
  515. </font><p>
  516. <li> <font color="ffffff">CPU&nbsp;0's scheduling clock
  517. interrupt fires in the
  518. middle of an RCU read-side critical section, and notices
  519. that the RCU core needs something, so commences RCU softirq
  520. processing.
  521. </font>
  522. <p>
  523. <li> <font color="ffffff">CPU&nbsp;0's softirq handler
  524. executes and is just about ready
  525. to report its quiescent state up the <tt>rcu_node</tt>
  526. tree.
  527. </font><p>
  528. <li> <font color="ffffff">But CPU&nbsp;1 beats it to the punch,
  529. completing the current
  530. grace period and starting a new one.
  531. </font><p>
  532. <li> <font color="ffffff">CPU&nbsp;0 now reports its quiescent
  533. state for the wrong
  534. grace period.
  535. That grace period might now end before the RCU read-side
  536. critical section.
  537. If that happens, disaster will ensue.
  538. </font>
  539. </ol>
  540. <p><font color="ffffff">So the locking is absolutely required in
  541. order to coordinate
  542. clearing of the bits with the grace-period numbers in
  543. <tt>-&gt;gpnum</tt> and <tt>-&gt;completed</tt>.
  544. </font></td></tr>
  545. <tr><td>&nbsp;</td></tr>
  546. </table>
  547. <h5>Blocked-Task Management</h5>
  548. <p><tt>PREEMPT_RCU</tt> allows tasks to be preempted in the
  549. midst of their RCU read-side critical sections, and these tasks
  550. must be tracked explicitly.
  551. The details of exactly why and how they are tracked will be covered
  552. in a separate article on RCU read-side processing.
  553. For now, it is enough to know that the <tt>rcu_node</tt>
  554. structure tracks them.
  555. <pre>
  556. 1 struct list_head blkd_tasks;
  557. 2 struct list_head *gp_tasks;
  558. 3 struct list_head *exp_tasks;
  559. 4 bool wait_blkd_tasks;
  560. </pre>
  561. <p>The <tt>-&gt;blkd_tasks</tt> field is a list header for
  562. the list of blocked and preempted tasks.
  563. As tasks undergo context switches within RCU read-side critical
  564. sections, their <tt>task_struct</tt> structures are enqueued
  565. (via the <tt>task_struct</tt>'s <tt>-&gt;rcu_node_entry</tt>
  566. field) onto the head of the <tt>-&gt;blkd_tasks</tt> list for the
  567. leaf <tt>rcu_node</tt> structure corresponding to the CPU
  568. on which the outgoing context switch executed.
  569. As these tasks later exit their RCU read-side critical sections,
  570. they remove themselves from the list.
  571. This list is therefore in reverse time order, so that if one of the tasks
  572. is blocking the current grace period, all subsequent tasks must
  573. also be blocking that same grace period.
  574. Therefore, a single pointer into this list suffices to track
  575. all tasks blocking a given grace period.
  576. That pointer is stored in <tt>-&gt;gp_tasks</tt> for normal
  577. grace periods and in <tt>-&gt;exp_tasks</tt> for expedited
  578. grace periods.
  579. These last two fields are <tt>NULL</tt> if either there is
  580. no grace period in flight or if there are no blocked tasks
  581. preventing that grace period from completing.
  582. If either of these two pointers is referencing a task that
  583. removes itself from the <tt>-&gt;blkd_tasks</tt> list,
  584. then that task must advance the pointer to the next task on
  585. the list, or set the pointer to <tt>NULL</tt> if there
  586. are no subsequent tasks on the list.
  587. </p><p>For example, suppose that tasks&nbsp;T1, T2, and&nbsp;T3 are
  588. all hard-affinitied to the largest-numbered CPU in the system.
  589. Then if task&nbsp;T1 blocked in an RCU read-side
  590. critical section, then an expedited grace period started,
  591. then task&nbsp;T2 blocked in an RCU read-side critical section,
  592. then a normal grace period started, and finally task&nbsp;3 blocked
  593. in an RCU read-side critical section, then the state of the
  594. last leaf <tt>rcu_node</tt> structure's blocked-task list
  595. would be as shown below:
  596. </p><p><img src="blkd_task.svg" alt="blkd_task.svg" width="60%">
  597. </p><p>Task&nbsp;T1 is blocking both grace periods, task&nbsp;T2 is
  598. blocking only the normal grace period, and task&nbsp;T3 is blocking
  599. neither grace period.
  600. Note that these tasks will not remove themselves from this list
  601. immediately upon resuming execution.
  602. They will instead remain on the list until they execute the outermost
  603. <tt>rcu_read_unlock()</tt> that ends their RCU read-side critical
  604. section.
  605. <p>
  606. The <tt>-&gt;wait_blkd_tasks</tt> field indicates whether or not
  607. the current grace period is waiting on a blocked task.
  608. <h5>Sizing the <tt>rcu_node</tt> Array</h5>
  609. <p>The <tt>rcu_node</tt> array is sized via a series of
  610. C-preprocessor expressions as follows:
  611. <pre>
  612. 1 #ifdef CONFIG_RCU_FANOUT
  613. 2 #define RCU_FANOUT CONFIG_RCU_FANOUT
  614. 3 #else
  615. 4 # ifdef CONFIG_64BIT
  616. 5 # define RCU_FANOUT 64
  617. 6 # else
  618. 7 # define RCU_FANOUT 32
  619. 8 # endif
  620. 9 #endif
  621. 10
  622. 11 #ifdef CONFIG_RCU_FANOUT_LEAF
  623. 12 #define RCU_FANOUT_LEAF CONFIG_RCU_FANOUT_LEAF
  624. 13 #else
  625. 14 # ifdef CONFIG_64BIT
  626. 15 # define RCU_FANOUT_LEAF 64
  627. 16 # else
  628. 17 # define RCU_FANOUT_LEAF 32
  629. 18 # endif
  630. 19 #endif
  631. 20
  632. 21 #define RCU_FANOUT_1 (RCU_FANOUT_LEAF)
  633. 22 #define RCU_FANOUT_2 (RCU_FANOUT_1 * RCU_FANOUT)
  634. 23 #define RCU_FANOUT_3 (RCU_FANOUT_2 * RCU_FANOUT)
  635. 24 #define RCU_FANOUT_4 (RCU_FANOUT_3 * RCU_FANOUT)
  636. 25
  637. 26 #if NR_CPUS &lt;= RCU_FANOUT_1
  638. 27 # define RCU_NUM_LVLS 1
  639. 28 # define NUM_RCU_LVL_0 1
  640. 29 # define NUM_RCU_NODES NUM_RCU_LVL_0
  641. 30 # define NUM_RCU_LVL_INIT { NUM_RCU_LVL_0 }
  642. 31 # define RCU_NODE_NAME_INIT { "rcu_node_0" }
  643. 32 # define RCU_FQS_NAME_INIT { "rcu_node_fqs_0" }
  644. 33 # define RCU_EXP_NAME_INIT { "rcu_node_exp_0" }
  645. 34 #elif NR_CPUS &lt;= RCU_FANOUT_2
  646. 35 # define RCU_NUM_LVLS 2
  647. 36 # define NUM_RCU_LVL_0 1
  648. 37 # define NUM_RCU_LVL_1 DIV_ROUND_UP(NR_CPUS, RCU_FANOUT_1)
  649. 38 # define NUM_RCU_NODES (NUM_RCU_LVL_0 + NUM_RCU_LVL_1)
  650. 39 # define NUM_RCU_LVL_INIT { NUM_RCU_LVL_0, NUM_RCU_LVL_1 }
  651. 40 # define RCU_NODE_NAME_INIT { "rcu_node_0", "rcu_node_1" }
  652. 41 # define RCU_FQS_NAME_INIT { "rcu_node_fqs_0", "rcu_node_fqs_1" }
  653. 42 # define RCU_EXP_NAME_INIT { "rcu_node_exp_0", "rcu_node_exp_1" }
  654. 43 #elif NR_CPUS &lt;= RCU_FANOUT_3
  655. 44 # define RCU_NUM_LVLS 3
  656. 45 # define NUM_RCU_LVL_0 1
  657. 46 # define NUM_RCU_LVL_1 DIV_ROUND_UP(NR_CPUS, RCU_FANOUT_2)
  658. 47 # define NUM_RCU_LVL_2 DIV_ROUND_UP(NR_CPUS, RCU_FANOUT_1)
  659. 48 # define NUM_RCU_NODES (NUM_RCU_LVL_0 + NUM_RCU_LVL_1 + NUM_RCU_LVL_2)
  660. 49 # define NUM_RCU_LVL_INIT { NUM_RCU_LVL_0, NUM_RCU_LVL_1, NUM_RCU_LVL_2 }
  661. 50 # define RCU_NODE_NAME_INIT { "rcu_node_0", "rcu_node_1", "rcu_node_2" }
  662. 51 # define RCU_FQS_NAME_INIT { "rcu_node_fqs_0", "rcu_node_fqs_1", "rcu_node_fqs_2" }
  663. 52 # define RCU_EXP_NAME_INIT { "rcu_node_exp_0", "rcu_node_exp_1", "rcu_node_exp_2" }
  664. 53 #elif NR_CPUS &lt;= RCU_FANOUT_4
  665. 54 # define RCU_NUM_LVLS 4
  666. 55 # define NUM_RCU_LVL_0 1
  667. 56 # define NUM_RCU_LVL_1 DIV_ROUND_UP(NR_CPUS, RCU_FANOUT_3)
  668. 57 # define NUM_RCU_LVL_2 DIV_ROUND_UP(NR_CPUS, RCU_FANOUT_2)
  669. 58 # define NUM_RCU_LVL_3 DIV_ROUND_UP(NR_CPUS, RCU_FANOUT_1)
  670. 59 # define NUM_RCU_NODES (NUM_RCU_LVL_0 + NUM_RCU_LVL_1 + NUM_RCU_LVL_2 + NUM_RCU_LVL_3)
  671. 60 # define NUM_RCU_LVL_INIT { NUM_RCU_LVL_0, NUM_RCU_LVL_1, NUM_RCU_LVL_2, NUM_RCU_LVL_3 }
  672. 61 # define RCU_NODE_NAME_INIT { "rcu_node_0", "rcu_node_1", "rcu_node_2", "rcu_node_3" }
  673. 62 # define RCU_FQS_NAME_INIT { "rcu_node_fqs_0", "rcu_node_fqs_1", "rcu_node_fqs_2", "rcu_node_fqs_3" }
  674. 63 # define RCU_EXP_NAME_INIT { "rcu_node_exp_0", "rcu_node_exp_1", "rcu_node_exp_2", "rcu_node_exp_3" }
  675. 64 #else
  676. 65 # error "CONFIG_RCU_FANOUT insufficient for NR_CPUS"
  677. 66 #endif
  678. </pre>
  679. <p>The maximum number of levels in the <tt>rcu_node</tt> structure
  680. is currently limited to four, as specified by lines&nbsp;21-24
  681. and the structure of the subsequent &ldquo;if&rdquo; statement.
  682. For 32-bit systems, this allows 16*32*32*32=524,288 CPUs, which
  683. should be sufficient for the next few years at least.
  684. For 64-bit systems, 16*64*64*64=4,194,304 CPUs is allowed, which
  685. should see us through the next decade or so.
  686. This four-level tree also allows kernels built with
  687. <tt>CONFIG_RCU_FANOUT=8</tt> to support up to 4096 CPUs,
  688. which might be useful in very large systems having eight CPUs per
  689. socket (but please note that no one has yet shown any measurable
  690. performance degradation due to misaligned socket and <tt>rcu_node</tt>
  691. boundaries).
  692. In addition, building kernels with a full four levels of <tt>rcu_node</tt>
  693. tree permits better testing of RCU's combining-tree code.
  694. </p><p>The <tt>RCU_FANOUT</tt> symbol controls how many children
  695. are permitted at each non-leaf level of the <tt>rcu_node</tt> tree.
  696. If the <tt>CONFIG_RCU_FANOUT</tt> Kconfig option is not specified,
  697. it is set based on the word size of the system, which is also
  698. the Kconfig default.
  699. </p><p>The <tt>RCU_FANOUT_LEAF</tt> symbol controls how many CPUs are
  700. handled by each leaf <tt>rcu_node</tt> structure.
  701. Experience has shown that allowing a given leaf <tt>rcu_node</tt>
  702. structure to handle 64 CPUs, as permitted by the number of bits in
  703. the <tt>-&gt;qsmask</tt> field on a 64-bit system, results in
  704. excessive contention for the leaf <tt>rcu_node</tt> structures'
  705. <tt>-&gt;lock</tt> fields.
  706. The number of CPUs per leaf <tt>rcu_node</tt> structure is therefore
  707. limited to 16 given the default value of <tt>CONFIG_RCU_FANOUT_LEAF</tt>.
  708. If <tt>CONFIG_RCU_FANOUT_LEAF</tt> is unspecified, the value
  709. selected is based on the word size of the system, just as for
  710. <tt>CONFIG_RCU_FANOUT</tt>.
  711. Lines&nbsp;11-19 perform this computation.
  712. </p><p>Lines&nbsp;21-24 compute the maximum number of CPUs supported by
  713. a single-level (which contains a single <tt>rcu_node</tt> structure),
  714. two-level, three-level, and four-level <tt>rcu_node</tt> tree,
  715. respectively, given the fanout specified by <tt>RCU_FANOUT</tt>
  716. and <tt>RCU_FANOUT_LEAF</tt>.
  717. These numbers of CPUs are retained in the
  718. <tt>RCU_FANOUT_1</tt>,
  719. <tt>RCU_FANOUT_2</tt>,
  720. <tt>RCU_FANOUT_3</tt>, and
  721. <tt>RCU_FANOUT_4</tt>
  722. C-preprocessor variables, respectively.
  723. </p><p>These variables are used to control the C-preprocessor <tt>#if</tt>
  724. statement spanning lines&nbsp;26-66 that computes the number of
  725. <tt>rcu_node</tt> structures required for each level of the tree,
  726. as well as the number of levels required.
  727. The number of levels is placed in the <tt>NUM_RCU_LVLS</tt>
  728. C-preprocessor variable by lines&nbsp;27, 35, 44, and&nbsp;54.
  729. The number of <tt>rcu_node</tt> structures for the topmost level
  730. of the tree is always exactly one, and this value is unconditionally
  731. placed into <tt>NUM_RCU_LVL_0</tt> by lines&nbsp;28, 36, 45, and&nbsp;55.
  732. The rest of the levels (if any) of the <tt>rcu_node</tt> tree
  733. are computed by dividing the maximum number of CPUs by the
  734. fanout supported by the number of levels from the current level down,
  735. rounding up. This computation is performed by lines&nbsp;37,
  736. 46-47, and&nbsp;56-58.
  737. Lines&nbsp;31-33, 40-42, 50-52, and&nbsp;62-63 create initializers
  738. for lockdep lock-class names.
  739. Finally, lines&nbsp;64-66 produce an error if the maximum number of
  740. CPUs is too large for the specified fanout.
  741. <h3><a name="The rcu_data Structure">
  742. The <tt>rcu_data</tt> Structure</a></h3>
  743. <p>The <tt>rcu_data</tt> maintains the per-CPU state for the
  744. corresponding flavor of RCU.
  745. The fields in this structure may be accessed only from the corresponding
  746. CPU (and from tracing) unless otherwise stated.
  747. This structure is the
  748. focus of quiescent-state detection and RCU callback queuing.
  749. It also tracks its relationship to the corresponding leaf
  750. <tt>rcu_node</tt> structure to allow more-efficient
  751. propagation of quiescent states up the <tt>rcu_node</tt>
  752. combining tree.
  753. Like the <tt>rcu_node</tt> structure, it provides a local
  754. copy of the grace-period information to allow for-free
  755. synchronized
  756. access to this information from the corresponding CPU.
  757. Finally, this structure records past dyntick-idle state
  758. for the corresponding CPU and also tracks statistics.
  759. </p><p>The <tt>rcu_data</tt> structure's fields are discussed,
  760. singly and in groups, in the following sections.
  761. <h5>Connection to Other Data Structures</h5>
  762. <p>This portion of the <tt>rcu_data</tt> structure is declared
  763. as follows:
  764. <pre>
  765. 1 int cpu;
  766. 2 struct rcu_state *rsp;
  767. 3 struct rcu_node *mynode;
  768. 4 struct rcu_dynticks *dynticks;
  769. 5 unsigned long grpmask;
  770. 6 bool beenonline;
  771. </pre>
  772. <p>The <tt>-&gt;cpu</tt> field contains the number of the
  773. corresponding CPU, the <tt>-&gt;rsp</tt> pointer references
  774. the corresponding <tt>rcu_state</tt> structure (and is most frequently
  775. used to locate the name of the corresponding flavor of RCU for tracing),
  776. and the <tt>-&gt;mynode</tt> field references the corresponding
  777. <tt>rcu_node</tt> structure.
  778. The <tt>-&gt;mynode</tt> is used to propagate quiescent states
  779. up the combining tree.
  780. <p>The <tt>-&gt;dynticks</tt> pointer references the
  781. <tt>rcu_dynticks</tt> structure corresponding to this
  782. CPU.
  783. Recall that a single per-CPU instance of the <tt>rcu_dynticks</tt>
  784. structure is shared among all flavors of RCU.
  785. These first four fields are constant and therefore require not
  786. synchronization.
  787. </p><p>The <tt>-&gt;grpmask</tt> field indicates the bit in
  788. the <tt>-&gt;mynode-&gt;qsmask</tt> corresponding to this
  789. <tt>rcu_data</tt> structure, and is also used when propagating
  790. quiescent states.
  791. The <tt>-&gt;beenonline</tt> flag is set whenever the corresponding
  792. CPU comes online, which means that the debugfs tracing need not dump
  793. out any <tt>rcu_data</tt> structure for which this flag is not set.
  794. <h5>Quiescent-State and Grace-Period Tracking</h5>
  795. <p>This portion of the <tt>rcu_data</tt> structure is declared
  796. as follows:
  797. <pre>
  798. 1 unsigned long completed;
  799. 2 unsigned long gpnum;
  800. 3 bool cpu_no_qs;
  801. 4 bool core_needs_qs;
  802. 5 bool gpwrap;
  803. 6 unsigned long rcu_qs_ctr_snap;
  804. </pre>
  805. <p>The <tt>completed</tt> and <tt>gpnum</tt>
  806. fields are the counterparts of the fields of the same name
  807. in the <tt>rcu_state</tt> and <tt>rcu_node</tt> structures.
  808. They may each lag up to one behind their <tt>rcu_node</tt>
  809. counterparts, but in <tt>CONFIG_NO_HZ_IDLE</tt> and
  810. <tt>CONFIG_NO_HZ_FULL</tt> kernels can lag
  811. arbitrarily far behind for CPUs in dyntick-idle mode (but these counters
  812. will catch up upon exit from dyntick-idle mode).
  813. If a given <tt>rcu_data</tt> structure's <tt>-&gt;gpnum</tt> and
  814. <tt>-&gt;complete</tt> fields are equal, then this <tt>rcu_data</tt>
  815. structure believes that RCU is idle.
  816. Otherwise, as with the <tt>rcu_state</tt> and <tt>rcu_node</tt>
  817. structure,
  818. the <tt>-&gt;gpnum</tt> field will be one greater than the
  819. <tt>-&gt;complete</tt> fields, with <tt>-&gt;gpnum</tt>
  820. indicating which grace period this <tt>rcu_data</tt> believes
  821. is still being waited for.
  822. <table>
  823. <tr><th>&nbsp;</th></tr>
  824. <tr><th align="left">Quick Quiz:</th></tr>
  825. <tr><td>
  826. All this replication of the grace period numbers can only cause
  827. massive confusion.
  828. Why not just keep a global pair of counters and be done with it???
  829. </td></tr>
  830. <tr><th align="left">Answer:</th></tr>
  831. <tr><td bgcolor="#ffffff"><font color="ffffff">
  832. Because if there was only a single global pair of grace-period
  833. numbers, there would need to be a single global lock to allow
  834. safely accessing and updating them.
  835. And if we are not going to have a single global lock, we need
  836. to carefully manage the numbers on a per-node basis.
  837. Recall from the answer to a previous Quick Quiz that the consequences
  838. of applying a previously sampled quiescent state to the wrong
  839. grace period are quite severe.
  840. </font></td></tr>
  841. <tr><td>&nbsp;</td></tr>
  842. </table>
  843. <p>The <tt>-&gt;cpu_no_qs</tt> flag indicates that the
  844. CPU has not yet passed through a quiescent state,
  845. while the <tt>-&gt;core_needs_qs</tt> flag indicates that the
  846. RCU core needs a quiescent state from the corresponding CPU.
  847. The <tt>-&gt;gpwrap</tt> field indicates that the corresponding
  848. CPU has remained idle for so long that the <tt>completed</tt>
  849. and <tt>gpnum</tt> counters are in danger of overflow, which
  850. will cause the CPU to disregard the values of its counters on
  851. its next exit from idle.
  852. Finally, the <tt>rcu_qs_ctr_snap</tt> field is used to detect
  853. cases where a given operation has resulted in a quiescent state
  854. for all flavors of RCU, for example, <tt>cond_resched_rcu_qs()</tt>.
  855. <h5>RCU Callback Handling</h5>
  856. <p>In the absence of CPU-hotplug events, RCU callbacks are invoked by
  857. the same CPU that registered them.
  858. This is strictly a cache-locality optimization: callbacks can and
  859. do get invoked on CPUs other than the one that registered them.
  860. After all, if the CPU that registered a given callback has gone
  861. offline before the callback can be invoked, there really is no other
  862. choice.
  863. </p><p>This portion of the <tt>rcu_data</tt> structure is declared
  864. as follows:
  865. <pre>
  866. 1 struct rcu_head *nxtlist;
  867. 2 struct rcu_head **nxttail[RCU_NEXT_SIZE];
  868. 3 unsigned long nxtcompleted[RCU_NEXT_SIZE];
  869. 4 long qlen_lazy;
  870. 5 long qlen;
  871. 6 long qlen_last_fqs_check;
  872. 7 unsigned long n_force_qs_snap;
  873. 8 unsigned long n_cbs_invoked;
  874. 9 unsigned long n_cbs_orphaned;
  875. 10 unsigned long n_cbs_adopted;
  876. 11 long blimit;
  877. </pre>
  878. <p>The <tt>-&gt;nxtlist</tt> pointer and the
  879. <tt>-&gt;nxttail[]</tt> array form a four-segment list with
  880. older callbacks near the head and newer ones near the tail.
  881. Each segment contains callbacks with the corresponding relationship
  882. to the current grace period.
  883. The pointer out of the end of each of the four segments is referenced
  884. by the element of the <tt>-&gt;nxttail[]</tt> array indexed by
  885. <tt>RCU_DONE_TAIL</tt> (for callbacks handled by a prior grace period),
  886. <tt>RCU_WAIT_TAIL</tt> (for callbacks waiting on the current grace period),
  887. <tt>RCU_NEXT_READY_TAIL</tt> (for callbacks that will wait on the next
  888. grace period), and
  889. <tt>RCU_NEXT_TAIL</tt> (for callbacks that are not yet associated
  890. with a specific grace period)
  891. respectively, as shown in the following figure.
  892. </p><p><img src="nxtlist.svg" alt="nxtlist.svg" width="40%">
  893. </p><p>In this figure, the <tt>-&gt;nxtlist</tt> pointer references the
  894. first
  895. RCU callback in the list.
  896. The <tt>-&gt;nxttail[RCU_DONE_TAIL]</tt> array element references
  897. the <tt>-&gt;nxtlist</tt> pointer itself, indicating that none
  898. of the callbacks is ready to invoke.
  899. The <tt>-&gt;nxttail[RCU_WAIT_TAIL]</tt> array element references callback
  900. CB&nbsp;2's <tt>-&gt;next</tt> pointer, which indicates that
  901. CB&nbsp;1 and CB&nbsp;2 are both waiting on the current grace period.
  902. The <tt>-&gt;nxttail[RCU_NEXT_READY_TAIL]</tt> array element
  903. references the same RCU callback that <tt>-&gt;nxttail[RCU_WAIT_TAIL]</tt>
  904. does, which indicates that there are no callbacks waiting on the next
  905. RCU grace period.
  906. The <tt>-&gt;nxttail[RCU_NEXT_TAIL]</tt> array element references
  907. CB&nbsp;4's <tt>-&gt;next</tt> pointer, indicating that all the
  908. remaining RCU callbacks have not yet been assigned to an RCU grace
  909. period.
  910. Note that the <tt>-&gt;nxttail[RCU_NEXT_TAIL]</tt> array element
  911. always references the last RCU callback's <tt>-&gt;next</tt> pointer
  912. unless the callback list is empty, in which case it references
  913. the <tt>-&gt;nxtlist</tt> pointer.
  914. </p><p>CPUs advance their callbacks from the
  915. <tt>RCU_NEXT_TAIL</tt> to the <tt>RCU_NEXT_READY_TAIL</tt> to the
  916. <tt>RCU_WAIT_TAIL</tt> to the <tt>RCU_DONE_TAIL</tt> list segments
  917. as grace periods advance.
  918. The CPU advances the callbacks in its <tt>rcu_data</tt> structure
  919. whenever it notices that another RCU grace period has completed.
  920. The CPU detects the completion of an RCU grace period by noticing
  921. that the value of its <tt>rcu_data</tt> structure's
  922. <tt>-&gt;completed</tt> field differs from that of its leaf
  923. <tt>rcu_node</tt> structure.
  924. Recall that each <tt>rcu_node</tt> structure's
  925. <tt>-&gt;completed</tt> field is updated at the end of each
  926. grace period.
  927. </p><p>The <tt>-&gt;nxtcompleted[]</tt> array records grace-period
  928. numbers corresponding to the list segments.
  929. This allows CPUs that go idle for extended periods to determine
  930. which of their callbacks are ready to be invoked after reawakening.
  931. </p><p>The <tt>-&gt;qlen</tt> counter contains the number of
  932. callbacks in <tt>-&gt;nxtlist</tt>, and the
  933. <tt>-&gt;qlen_lazy</tt> contains the number of those callbacks that
  934. are known to only free memory, and whose invocation can therefore
  935. be safely deferred.
  936. The <tt>-&gt;qlen_last_fqs_check</tt> and
  937. <tt>-&gt;n_force_qs_snap</tt> coordinate the forcing of quiescent
  938. states from <tt>call_rcu()</tt> and friends when callback
  939. lists grow excessively long.
  940. </p><p>The <tt>-&gt;n_cbs_invoked</tt>,
  941. <tt>-&gt;n_cbs_orphaned</tt>, and <tt>-&gt;n_cbs_adopted</tt>
  942. fields count the number of callbacks invoked,
  943. sent to other CPUs when this CPU goes offline,
  944. and received from other CPUs when those other CPUs go offline.
  945. Finally, the <tt>-&gt;blimit</tt> counter is the maximum number of
  946. RCU callbacks that may be invoked at a given time.
  947. <h5>Dyntick-Idle Handling</h5>
  948. <p>This portion of the <tt>rcu_data</tt> structure is declared
  949. as follows:
  950. <pre>
  951. 1 int dynticks_snap;
  952. 2 unsigned long dynticks_fqs;
  953. </pre>
  954. The <tt>-&gt;dynticks_snap</tt> field is used to take a snapshot
  955. of the corresponding CPU's dyntick-idle state when forcing
  956. quiescent states, and is therefore accessed from other CPUs.
  957. Finally, the <tt>-&gt;dynticks_fqs</tt> field is used to
  958. count the number of times this CPU is determined to be in
  959. dyntick-idle state, and is used for tracing and debugging purposes.
  960. <h3><a name="The rcu_dynticks Structure">
  961. The <tt>rcu_dynticks</tt> Structure</a></h3>
  962. <p>The <tt>rcu_dynticks</tt> maintains the per-CPU dyntick-idle state
  963. for the corresponding CPU.
  964. Unlike the other structures, <tt>rcu_dynticks</tt> is not
  965. replicated over the different flavors of RCU.
  966. The fields in this structure may be accessed only from the corresponding
  967. CPU (and from tracing) unless otherwise stated.
  968. Its fields are as follows:
  969. <pre>
  970. 1 int dynticks_nesting;
  971. 2 int dynticks_nmi_nesting;
  972. 3 atomic_t dynticks;
  973. </pre>
  974. <p>The <tt>-&gt;dynticks_nesting</tt> field counts the
  975. nesting depth of normal interrupts.
  976. In addition, this counter is incremented when exiting dyntick-idle
  977. mode and decremented when entering it.
  978. This counter can therefore be thought of as counting the number
  979. of reasons why this CPU cannot be permitted to enter dyntick-idle
  980. mode, aside from non-maskable interrupts (NMIs).
  981. NMIs are counted by the <tt>-&gt;dynticks_nmi_nesting</tt>
  982. field, except that NMIs that interrupt non-dyntick-idle execution
  983. are not counted.
  984. </p><p>Finally, the <tt>-&gt;dynticks</tt> field counts the corresponding
  985. CPU's transitions to and from dyntick-idle mode, so that this counter
  986. has an even value when the CPU is in dyntick-idle mode and an odd
  987. value otherwise.
  988. <table>
  989. <tr><th>&nbsp;</th></tr>
  990. <tr><th align="left">Quick Quiz:</th></tr>
  991. <tr><td>
  992. Why not just count all NMIs?
  993. Wouldn't that be simpler and less error prone?
  994. </td></tr>
  995. <tr><th align="left">Answer:</th></tr>
  996. <tr><td bgcolor="#ffffff"><font color="ffffff">
  997. It seems simpler only until you think hard about how to go about
  998. updating the <tt>rcu_dynticks</tt> structure's
  999. <tt>-&gt;dynticks</tt> field.
  1000. </font></td></tr>
  1001. <tr><td>&nbsp;</td></tr>
  1002. </table>
  1003. <p>Additional fields are present for some special-purpose
  1004. builds, and are discussed separately.
  1005. <h3><a name="The rcu_head Structure">
  1006. The <tt>rcu_head</tt> Structure</a></h3>
  1007. <p>Each <tt>rcu_head</tt> structure represents an RCU callback.
  1008. These structures are normally embedded within RCU-protected data
  1009. structures whose algorithms use asynchronous grace periods.
  1010. In contrast, when using algorithms that block waiting for RCU grace periods,
  1011. RCU users need not provide <tt>rcu_head</tt> structures.
  1012. </p><p>The <tt>rcu_head</tt> structure has fields as follows:
  1013. <pre>
  1014. 1 struct rcu_head *next;
  1015. 2 void (*func)(struct rcu_head *head);
  1016. </pre>
  1017. <p>The <tt>-&gt;next</tt> field is used
  1018. to link the <tt>rcu_head</tt> structures together in the
  1019. lists within the <tt>rcu_data</tt> structures.
  1020. The <tt>-&gt;func</tt> field is a pointer to the function
  1021. to be called when the callback is ready to be invoked, and
  1022. this function is passed a pointer to the <tt>rcu_head</tt>
  1023. structure.
  1024. However, <tt>kfree_rcu()</tt> uses the <tt>-&gt;func</tt>
  1025. field to record the offset of the <tt>rcu_head</tt>
  1026. structure within the enclosing RCU-protected data structure.
  1027. </p><p>Both of these fields are used internally by RCU.
  1028. From the viewpoint of RCU users, this structure is an
  1029. opaque &ldquo;cookie&rdquo;.
  1030. <table>
  1031. <tr><th>&nbsp;</th></tr>
  1032. <tr><th align="left">Quick Quiz:</th></tr>
  1033. <tr><td>
  1034. Given that the callback function <tt>-&gt;func</tt>
  1035. is passed a pointer to the <tt>rcu_head</tt> structure,
  1036. how is that function supposed to find the beginning of the
  1037. enclosing RCU-protected data structure?
  1038. </td></tr>
  1039. <tr><th align="left">Answer:</th></tr>
  1040. <tr><td bgcolor="#ffffff"><font color="ffffff">
  1041. In actual practice, there is a separate callback function per
  1042. type of RCU-protected data structure.
  1043. The callback function can therefore use the <tt>container_of()</tt>
  1044. macro in the Linux kernel (or other pointer-manipulation facilities
  1045. in other software environments) to find the beginning of the
  1046. enclosing structure.
  1047. </font></td></tr>
  1048. <tr><td>&nbsp;</td></tr>
  1049. </table>
  1050. <h3><a name="RCU-Specific Fields in the task_struct Structure">
  1051. RCU-Specific Fields in the <tt>task_struct</tt> Structure</a></h3>
  1052. <p>The <tt>CONFIG_PREEMPT_RCU</tt> implementation uses some
  1053. additional fields in the <tt>task_struct</tt> structure:
  1054. <pre>
  1055. 1 #ifdef CONFIG_PREEMPT_RCU
  1056. 2 int rcu_read_lock_nesting;
  1057. 3 union rcu_special rcu_read_unlock_special;
  1058. 4 struct list_head rcu_node_entry;
  1059. 5 struct rcu_node *rcu_blocked_node;
  1060. 6 #endif /* #ifdef CONFIG_PREEMPT_RCU */
  1061. 7 #ifdef CONFIG_TASKS_RCU
  1062. 8 unsigned long rcu_tasks_nvcsw;
  1063. 9 bool rcu_tasks_holdout;
  1064. 10 struct list_head rcu_tasks_holdout_list;
  1065. 11 int rcu_tasks_idle_cpu;
  1066. 12 #endif /* #ifdef CONFIG_TASKS_RCU */
  1067. </pre>
  1068. <p>The <tt>-&gt;rcu_read_lock_nesting</tt> field records the
  1069. nesting level for RCU read-side critical sections, and
  1070. the <tt>-&gt;rcu_read_unlock_special</tt> field is a bitmask
  1071. that records special conditions that require <tt>rcu_read_unlock()</tt>
  1072. to do additional work.
  1073. The <tt>-&gt;rcu_node_entry</tt> field is used to form lists of
  1074. tasks that have blocked within preemptible-RCU read-side critical
  1075. sections and the <tt>-&gt;rcu_blocked_node</tt> field references
  1076. the <tt>rcu_node</tt> structure whose list this task is a member of,
  1077. or <tt>NULL</tt> if it is not blocked within a preemptible-RCU
  1078. read-side critical section.
  1079. <p>The <tt>-&gt;rcu_tasks_nvcsw</tt> field tracks the number of
  1080. voluntary context switches that this task had undergone at the
  1081. beginning of the current tasks-RCU grace period,
  1082. <tt>-&gt;rcu_tasks_holdout</tt> is set if the current tasks-RCU
  1083. grace period is waiting on this task, <tt>-&gt;rcu_tasks_holdout_list</tt>
  1084. is a list element enqueuing this task on the holdout list,
  1085. and <tt>-&gt;rcu_tasks_idle_cpu</tt> tracks which CPU this
  1086. idle task is running, but only if the task is currently running,
  1087. that is, if the CPU is currently idle.
  1088. <h3><a name="Accessor Functions">
  1089. Accessor Functions</a></h3>
  1090. <p>The following listing shows the
  1091. <tt>rcu_get_root()</tt>, <tt>rcu_for_each_node_breadth_first</tt>,
  1092. <tt>rcu_for_each_nonleaf_node_breadth_first()</tt>, and
  1093. <tt>rcu_for_each_leaf_node()</tt> function and macros:
  1094. <pre>
  1095. 1 static struct rcu_node *rcu_get_root(struct rcu_state *rsp)
  1096. 2 {
  1097. 3 return &amp;rsp-&gt;node[0];
  1098. 4 }
  1099. 5
  1100. 6 #define rcu_for_each_node_breadth_first(rsp, rnp) \
  1101. 7 for ((rnp) = &amp;(rsp)-&gt;node[0]; \
  1102. 8 (rnp) &lt; &amp;(rsp)-&gt;node[NUM_RCU_NODES]; (rnp)++)
  1103. 9
  1104. 10 #define rcu_for_each_nonleaf_node_breadth_first(rsp, rnp) \
  1105. 11 for ((rnp) = &amp;(rsp)-&gt;node[0]; \
  1106. 12 (rnp) &lt; (rsp)-&gt;level[NUM_RCU_LVLS - 1]; (rnp)++)
  1107. 13
  1108. 14 #define rcu_for_each_leaf_node(rsp, rnp) \
  1109. 15 for ((rnp) = (rsp)-&gt;level[NUM_RCU_LVLS - 1]; \
  1110. 16 (rnp) &lt; &amp;(rsp)-&gt;node[NUM_RCU_NODES]; (rnp)++)
  1111. </pre>
  1112. <p>The <tt>rcu_get_root()</tt> simply returns a pointer to the
  1113. first element of the specified <tt>rcu_state</tt> structure's
  1114. <tt>-&gt;node[]</tt> array, which is the root <tt>rcu_node</tt>
  1115. structure.
  1116. </p><p>As noted earlier, the <tt>rcu_for_each_node_breadth_first()</tt>
  1117. macro takes advantage of the layout of the <tt>rcu_node</tt>
  1118. structures in the <tt>rcu_state</tt> structure's
  1119. <tt>-&gt;node[]</tt> array, performing a breadth-first traversal by
  1120. simply traversing the array in order.
  1121. The <tt>rcu_for_each_nonleaf_node_breadth_first()</tt> macro operates
  1122. similarly, but traverses only the first part of the array, thus excluding
  1123. the leaf <tt>rcu_node</tt> structures.
  1124. Finally, the <tt>rcu_for_each_leaf_node()</tt> macro traverses only
  1125. the last part of the array, thus traversing only the leaf
  1126. <tt>rcu_node</tt> structures.
  1127. <table>
  1128. <tr><th>&nbsp;</th></tr>
  1129. <tr><th align="left">Quick Quiz:</th></tr>
  1130. <tr><td>
  1131. What do <tt>rcu_for_each_nonleaf_node_breadth_first()</tt> and
  1132. <tt>rcu_for_each_leaf_node()</tt> do if the <tt>rcu_node</tt> tree
  1133. contains only a single node?
  1134. </td></tr>
  1135. <tr><th align="left">Answer:</th></tr>
  1136. <tr><td bgcolor="#ffffff"><font color="ffffff">
  1137. In the single-node case,
  1138. <tt>rcu_for_each_nonleaf_node_breadth_first()</tt> is a no-op
  1139. and <tt>rcu_for_each_leaf_node()</tt> traverses the single node.
  1140. </font></td></tr>
  1141. <tr><td>&nbsp;</td></tr>
  1142. </table>
  1143. <h3><a name="Summary">
  1144. Summary</a></h3>
  1145. So each flavor of RCU is represented by an <tt>rcu_state</tt> structure,
  1146. which contains a combining tree of <tt>rcu_node</tt> and
  1147. <tt>rcu_data</tt> structures.
  1148. Finally, in <tt>CONFIG_NO_HZ_IDLE</tt> kernels, each CPU's dyntick-idle
  1149. state is tracked by an <tt>rcu_dynticks</tt> structure.
  1150. If you made it this far, you are well prepared to read the code
  1151. walkthroughs in the other articles in this series.
  1152. <h3><a name="Acknowledgments">
  1153. Acknowledgments</a></h3>
  1154. I owe thanks to Cyrill Gorcunov, Mathieu Desnoyers, Dhaval Giani, Paul
  1155. Turner, Abhishek Srivastava, Matt Kowalczyk, and Serge Hallyn
  1156. for helping me get this document into a more human-readable state.
  1157. <h3><a name="Legal Statement">
  1158. Legal Statement</a></h3>
  1159. <p>This work represents the view of the author and does not necessarily
  1160. represent the view of IBM.
  1161. </p><p>Linux is a registered trademark of Linus Torvalds.
  1162. </p><p>Other company, product, and service names may be trademarks or
  1163. service marks of others.
  1164. </body></html>