Data-Structures.rst 55 KB

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