1164 lines · plain
1===================================================2A Tour Through TREE_RCU's Data Structures [LWN.net]3===================================================4 5December 18, 20166 7This article was contributed by Paul E. McKenney8 9Introduction10============11 12This document describes RCU's major data structures and their relationship13to each other.14 15Data-Structure Relationships16============================17 18RCU is for all intents and purposes a large state machine, and its19data structures maintain the state in such a way as to allow RCU readers20to execute extremely quickly, while also processing the RCU grace periods21requested by updaters in an efficient and extremely scalable fashion.22The efficiency and scalability of RCU updaters is provided primarily23by a combining tree, as shown below:24 25.. kernel-figure:: BigTreeClassicRCU.svg26 27This diagram shows an enclosing ``rcu_state`` structure containing a tree28of ``rcu_node`` structures. Each leaf node of the ``rcu_node`` tree has up29to 16 ``rcu_data`` structures associated with it, so that there are30``NR_CPUS`` number of ``rcu_data`` structures, one for each possible CPU.31This structure is adjusted at boot time, if needed, to handle the common32case where ``nr_cpu_ids`` is much less than ``NR_CPUs``.33For example, a number of Linux distributions set ``NR_CPUs=4096``,34which results in a three-level ``rcu_node`` tree.35If the actual hardware has only 16 CPUs, RCU will adjust itself36at boot time, resulting in an ``rcu_node`` tree with only a single node.37 38The purpose of this combining tree is to allow per-CPU events39such as quiescent states, dyntick-idle transitions,40and CPU hotplug operations to be processed efficiently41and scalably.42Quiescent states are recorded by the per-CPU ``rcu_data`` structures,43and other events are recorded by the leaf-level ``rcu_node``44structures.45All of these events are combined at each level of the tree until finally46grace periods are completed at the tree's root ``rcu_node``47structure.48A grace period can be completed at the root once every CPU49(or, in the case of ``CONFIG_PREEMPT_RCU``, task)50has passed through a quiescent state.51Once a grace period has completed, record of that fact is propagated52back down the tree.53 54As can be seen from the diagram, on a 64-bit system55a two-level tree with 64 leaves can accommodate 1,024 CPUs, with a fanout56of 64 at the root and a fanout of 16 at the leaves.57 58+-----------------------------------------------------------------------+59| **Quick Quiz**: |60+-----------------------------------------------------------------------+61| Why isn't the fanout at the leaves also 64? |62+-----------------------------------------------------------------------+63| **Answer**: |64+-----------------------------------------------------------------------+65| Because there are more types of events that affect the leaf-level |66| ``rcu_node`` structures than further up the tree. Therefore, if the |67| leaf ``rcu_node`` structures have fanout of 64, the contention on |68| these structures' ``->structures`` becomes excessive. Experimentation |69| on a wide variety of systems has shown that a fanout of 16 works well |70| for the leaves of the ``rcu_node`` tree. |71| |72| Of course, further experience with systems having hundreds or |73| thousands of CPUs may demonstrate that the fanout for the non-leaf |74| ``rcu_node`` structures must also be reduced. Such reduction can be |75| easily carried out when and if it proves necessary. In the meantime, |76| if you are using such a system and running into contention problems |77| on the non-leaf ``rcu_node`` structures, you may use the |78| ``CONFIG_RCU_FANOUT`` kernel configuration parameter to reduce the |79| non-leaf fanout as needed. |80| |81| Kernels built for systems with strong NUMA characteristics might |82| also need to adjust ``CONFIG_RCU_FANOUT`` so that the domains of |83| the ``rcu_node`` structures align with hardware boundaries. |84| However, there has thus far been no need for this. |85+-----------------------------------------------------------------------+86 87If your system has more than 1,024 CPUs (or more than 512 CPUs on a8832-bit system), then RCU will automatically add more levels to the tree.89For example, if you are crazy enough to build a 64-bit system with9065,536 CPUs, RCU would configure the ``rcu_node`` tree as follows:91 92.. kernel-figure:: HugeTreeClassicRCU.svg93 94RCU currently permits up to a four-level tree, which on a 64-bit system95accommodates up to 4,194,304 CPUs, though only a mere 524,288 CPUs for9632-bit systems. On the other hand, you can set both97``CONFIG_RCU_FANOUT`` and ``CONFIG_RCU_FANOUT_LEAF`` to be as small as982, which would result in a 16-CPU test using a 4-level tree. This can be99useful for testing large-system capabilities on small test machines.100 101This multi-level combining tree allows us to get most of the performance102and scalability benefits of partitioning, even though RCU grace-period103detection is inherently a global operation. The trick here is that only104the last CPU to report a quiescent state into a given ``rcu_node``105structure need advance to the ``rcu_node`` structure at the next level106up the tree. This means that at the leaf-level ``rcu_node`` structure,107only one access out of sixteen will progress up the tree. For the108internal ``rcu_node`` structures, the situation is even more extreme:109Only one access out of sixty-four will progress up the tree. Because the110vast majority of the CPUs do not progress up the tree, the lock111contention remains roughly constant up the tree. No matter how many CPUs112there are in the system, at most 64 quiescent-state reports per grace113period will progress all the way to the root ``rcu_node`` structure,114thus ensuring that the lock contention on that root ``rcu_node``115structure remains acceptably low.116 117In effect, the combining tree acts like a big shock absorber, keeping118lock contention under control at all tree levels regardless of the level119of loading on the system.120 121RCU updaters wait for normal grace periods by registering RCU callbacks,122either directly via ``call_rcu()`` or indirectly via123``synchronize_rcu()`` and friends. RCU callbacks are represented by124``rcu_head`` structures, which are queued on ``rcu_data`` structures125while they are waiting for a grace period to elapse, as shown in the126following figure:127 128.. kernel-figure:: BigTreePreemptRCUBHdyntickCB.svg129 130This figure shows how ``TREE_RCU``'s and ``PREEMPT_RCU``'s major data131structures are related. Lesser data structures will be introduced with132the algorithms that make use of them.133 134Note that each of the data structures in the above figure has its own135synchronization:136 137#. Each ``rcu_state`` structures has a lock and a mutex, and some fields138 are protected by the corresponding root ``rcu_node`` structure's lock.139#. Each ``rcu_node`` structure has a spinlock.140#. The fields in ``rcu_data`` are private to the corresponding CPU,141 although a few can be read and written by other CPUs.142 143It is important to note that different data structures can have very144different ideas about the state of RCU at any given time. For but one145example, awareness of the start or end of a given RCU grace period146propagates slowly through the data structures. This slow propagation is147absolutely necessary for RCU to have good read-side performance. If this148balkanized implementation seems foreign to you, one useful trick is to149consider each instance of these data structures to be a different150person, each having the usual slightly different view of reality.151 152The general role of each of these data structures is as follows:153 154#. ``rcu_state``: This structure forms the interconnection between the155 ``rcu_node`` and ``rcu_data`` structures, tracks grace periods,156 serves as short-term repository for callbacks orphaned by CPU-hotplug157 events, maintains ``rcu_barrier()`` state, tracks expedited158 grace-period state, and maintains state used to force quiescent159 states when grace periods extend too long,160#. ``rcu_node``: This structure forms the combining tree that propagates161 quiescent-state information from the leaves to the root, and also162 propagates grace-period information from the root to the leaves. It163 provides local copies of the grace-period state in order to allow164 this information to be accessed in a synchronized manner without165 suffering the scalability limitations that would otherwise be imposed166 by global locking. In ``CONFIG_PREEMPT_RCU`` kernels, it manages the167 lists of tasks that have blocked while in their current RCU read-side168 critical section. In ``CONFIG_PREEMPT_RCU`` with169 ``CONFIG_RCU_BOOST``, it manages the per-\ ``rcu_node``170 priority-boosting kernel threads (kthreads) and state. Finally, it171 records CPU-hotplug state in order to determine which CPUs should be172 ignored during a given grace period.173#. ``rcu_data``: This per-CPU structure is the focus of quiescent-state174 detection and RCU callback queuing. It also tracks its relationship175 to the corresponding leaf ``rcu_node`` structure to allow176 more-efficient propagation of quiescent states up the ``rcu_node``177 combining tree. Like the ``rcu_node`` structure, it provides a local178 copy of the grace-period information to allow for-free synchronized179 access to this information from the corresponding CPU. Finally, this180 structure records past dyntick-idle state for the corresponding CPU181 and also tracks statistics.182#. ``rcu_head``: This structure represents RCU callbacks, and is the183 only structure allocated and managed by RCU users. The ``rcu_head``184 structure is normally embedded within the RCU-protected data185 structure.186 187If all you wanted from this article was a general notion of how RCU's188data structures are related, you are done. Otherwise, each of the189following sections give more details on the ``rcu_state``, ``rcu_node``190and ``rcu_data`` data structures.191 192The ``rcu_state`` Structure193~~~~~~~~~~~~~~~~~~~~~~~~~~~194 195The ``rcu_state`` structure is the base structure that represents the196state of RCU in the system. This structure forms the interconnection197between the ``rcu_node`` and ``rcu_data`` structures, tracks grace198periods, contains the lock used to synchronize with CPU-hotplug events,199and maintains state used to force quiescent states when grace periods200extend too long,201 202A few of the ``rcu_state`` structure's fields are discussed, singly and203in groups, in the following sections. The more specialized fields are204covered in the discussion of their use.205 206Relationship to rcu_node and rcu_data Structures207''''''''''''''''''''''''''''''''''''''''''''''''208 209This portion of the ``rcu_state`` structure is declared as follows:210 211::212 213 1 struct rcu_node node[NUM_RCU_NODES];214 2 struct rcu_node *level[NUM_RCU_LVLS + 1];215 3 struct rcu_data __percpu *rda;216 217+-----------------------------------------------------------------------+218| **Quick Quiz**: |219+-----------------------------------------------------------------------+220| Wait a minute! You said that the ``rcu_node`` structures formed a |221| tree, but they are declared as a flat array! What gives? |222+-----------------------------------------------------------------------+223| **Answer**: |224+-----------------------------------------------------------------------+225| The tree is laid out in the array. The first node In the array is the |226| head, the next set of nodes in the array are children of the head |227| node, and so on until the last set of nodes in the array are the |228| leaves. |229| See the following diagrams to see how this works. |230+-----------------------------------------------------------------------+231 232The ``rcu_node`` tree is embedded into the ``->node[]`` array as shown233in the following figure:234 235.. kernel-figure:: TreeMapping.svg236 237One interesting consequence of this mapping is that a breadth-first238traversal of the tree is implemented as a simple linear scan of the239array, which is in fact what the ``rcu_for_each_node_breadth_first()``240macro does. This macro is used at the beginning and ends of grace241periods.242 243Each entry of the ``->level`` array references the first ``rcu_node``244structure on the corresponding level of the tree, for example, as shown245below:246 247.. kernel-figure:: TreeMappingLevel.svg248 249The zero\ :sup:`th` element of the array references the root250``rcu_node`` structure, the first element references the first child of251the root ``rcu_node``, and finally the second element references the252first leaf ``rcu_node`` structure.253 254For whatever it is worth, if you draw the tree to be tree-shaped rather255than array-shaped, it is easy to draw a planar representation:256 257.. kernel-figure:: TreeLevel.svg258 259Finally, the ``->rda`` field references a per-CPU pointer to the260corresponding CPU's ``rcu_data`` structure.261 262All of these fields are constant once initialization is complete, and263therefore need no protection.264 265Grace-Period Tracking266'''''''''''''''''''''267 268This portion of the ``rcu_state`` structure is declared as follows:269 270::271 272 1 unsigned long gp_seq;273 274RCU grace periods are numbered, and the ``->gp_seq`` field contains the275current grace-period sequence number. The bottom two bits are the state276of the current grace period, which can be zero for not yet started or277one for in progress. In other words, if the bottom two bits of278``->gp_seq`` are zero, then RCU is idle. Any other value in the bottom279two bits indicates that something is broken. This field is protected by280the root ``rcu_node`` structure's ``->lock`` field.281 282There are ``->gp_seq`` fields in the ``rcu_node`` and ``rcu_data``283structures as well. The fields in the ``rcu_state`` structure represent284the most current value, and those of the other structures are compared285in order to detect the beginnings and ends of grace periods in a286distributed fashion. The values flow from ``rcu_state`` to ``rcu_node``287(down the tree from the root to the leaves) to ``rcu_data``.288 289Miscellaneous290'''''''''''''291 292This portion of the ``rcu_state`` structure is declared as follows:293 294::295 296 1 unsigned long gp_max;297 2 char abbr;298 3 char *name;299 300The ``->gp_max`` field tracks the duration of the longest grace period301in jiffies. It is protected by the root ``rcu_node``'s ``->lock``.302 303The ``->name`` and ``->abbr`` fields distinguish between preemptible RCU304(“rcu_preempt” and “p”) and non-preemptible RCU (“rcu_sched” and “s”).305These fields are used for diagnostic and tracing purposes.306 307The ``rcu_node`` Structure308~~~~~~~~~~~~~~~~~~~~~~~~~~309 310The ``rcu_node`` structures form the combining tree that propagates311quiescent-state information from the leaves to the root and also that312propagates grace-period information from the root down to the leaves.313They provides local copies of the grace-period state in order to allow314this information to be accessed in a synchronized manner without315suffering the scalability limitations that would otherwise be imposed by316global locking. In ``CONFIG_PREEMPT_RCU`` kernels, they manage the lists317of tasks that have blocked while in their current RCU read-side critical318section. In ``CONFIG_PREEMPT_RCU`` with ``CONFIG_RCU_BOOST``, they319manage the per-\ ``rcu_node`` priority-boosting kernel threads320(kthreads) and state. Finally, they record CPU-hotplug state in order to321determine which CPUs should be ignored during a given grace period.322 323The ``rcu_node`` structure's fields are discussed, singly and in groups,324in the following sections.325 326Connection to Combining Tree327''''''''''''''''''''''''''''328 329This portion of the ``rcu_node`` structure is declared as follows:330 331::332 333 1 struct rcu_node *parent;334 2 u8 level;335 3 u8 grpnum;336 4 unsigned long grpmask;337 5 int grplo;338 6 int grphi;339 340The ``->parent`` pointer references the ``rcu_node`` one level up in the341tree, and is ``NULL`` for the root ``rcu_node``. The RCU implementation342makes heavy use of this field to push quiescent states up the tree. The343``->level`` field gives the level in the tree, with the root being at344level zero, its children at level one, and so on. The ``->grpnum`` field345gives this node's position within the children of its parent, so this346number can range between 0 and 31 on 32-bit systems and between 0 and 63347on 64-bit systems. The ``->level`` and ``->grpnum`` fields are used only348during initialization and for tracing. The ``->grpmask`` field is the349bitmask counterpart of ``->grpnum``, and therefore always has exactly350one bit set. This mask is used to clear the bit corresponding to this351``rcu_node`` structure in its parent's bitmasks, which are described352later. Finally, the ``->grplo`` and ``->grphi`` fields contain the353lowest and highest numbered CPU served by this ``rcu_node`` structure,354respectively.355 356All of these fields are constant, and thus do not require any357synchronization.358 359Synchronization360'''''''''''''''361 362This field of the ``rcu_node`` structure is declared as follows:363 364::365 366 1 raw_spinlock_t lock;367 368This field is used to protect the remaining fields in this structure,369unless otherwise stated. That said, all of the fields in this structure370can be accessed without locking for tracing purposes. Yes, this can371result in confusing traces, but better some tracing confusion than to be372heisenbugged out of existence.373 374.. _grace-period-tracking-1:375 376Grace-Period Tracking377'''''''''''''''''''''378 379This portion of the ``rcu_node`` structure is declared as follows:380 381::382 383 1 unsigned long gp_seq;384 2 unsigned long gp_seq_needed;385 386The ``rcu_node`` structures' ``->gp_seq`` fields are the counterparts of387the field of the same name in the ``rcu_state`` structure. They each may388lag up to one step behind their ``rcu_state`` counterpart. If the bottom389two bits of a given ``rcu_node`` structure's ``->gp_seq`` field is zero,390then this ``rcu_node`` structure believes that RCU is idle.391 392The ``>gp_seq`` field of each ``rcu_node`` structure is updated at the393beginning and the end of each grace period.394 395The ``->gp_seq_needed`` fields record the furthest-in-the-future grace396period request seen by the corresponding ``rcu_node`` structure. The397request is considered fulfilled when the value of the ``->gp_seq`` field398equals or exceeds that of the ``->gp_seq_needed`` field.399 400+-----------------------------------------------------------------------+401| **Quick Quiz**: |402+-----------------------------------------------------------------------+403| Suppose that this ``rcu_node`` structure doesn't see a request for a |404| very long time. Won't wrapping of the ``->gp_seq`` field cause |405| problems? |406+-----------------------------------------------------------------------+407| **Answer**: |408+-----------------------------------------------------------------------+409| No, because if the ``->gp_seq_needed`` field lags behind the |410| ``->gp_seq`` field, the ``->gp_seq_needed`` field will be updated at |411| the end of the grace period. Modulo-arithmetic comparisons therefore |412| will always get the correct answer, even with wrapping. |413+-----------------------------------------------------------------------+414 415Quiescent-State Tracking416''''''''''''''''''''''''417 418These fields manage the propagation of quiescent states up the combining419tree.420 421This portion of the ``rcu_node`` structure has fields as follows:422 423::424 425 1 unsigned long qsmask;426 2 unsigned long expmask;427 3 unsigned long qsmaskinit;428 4 unsigned long expmaskinit;429 430The ``->qsmask`` field tracks which of this ``rcu_node`` structure's431children still need to report quiescent states for the current normal432grace period. Such children will have a value of 1 in their433corresponding bit. Note that the leaf ``rcu_node`` structures should be434thought of as having ``rcu_data`` structures as their children.435Similarly, the ``->expmask`` field tracks which of this ``rcu_node``436structure's children still need to report quiescent states for the437current expedited grace period. An expedited grace period has the same438conceptual properties as a normal grace period, but the expedited439implementation accepts extreme CPU overhead to obtain much lower440grace-period latency, for example, consuming a few tens of microseconds441worth of CPU time to reduce grace-period duration from milliseconds to442tens of microseconds. The ``->qsmaskinit`` field tracks which of this443``rcu_node`` structure's children cover for at least one online CPU.444This mask is used to initialize ``->qsmask``, and ``->expmaskinit`` is445used to initialize ``->expmask`` and the beginning of the normal and446expedited grace periods, respectively.447 448+-----------------------------------------------------------------------+449| **Quick Quiz**: |450+-----------------------------------------------------------------------+451| Why are these bitmasks protected by locking? Come on, haven't you |452| heard of atomic instructions??? |453+-----------------------------------------------------------------------+454| **Answer**: |455+-----------------------------------------------------------------------+456| Lockless grace-period computation! Such a tantalizing possibility! |457| But consider the following sequence of events: |458| |459| #. CPU 0 has been in dyntick-idle mode for quite some time. When it |460| wakes up, it notices that the current RCU grace period needs it to |461| report in, so it sets a flag where the scheduling clock interrupt |462| will find it. |463| #. Meanwhile, CPU 1 is running ``force_quiescent_state()``, and |464| notices that CPU 0 has been in dyntick idle mode, which qualifies |465| as an extended quiescent state. |466| #. CPU 0's scheduling clock interrupt fires in the middle of an RCU |467| read-side critical section, and notices that the RCU core needs |468| something, so commences RCU softirq processing. |469| #. CPU 0's softirq handler executes and is just about ready to report |470| its quiescent state up the ``rcu_node`` tree. |471| #. But CPU 1 beats it to the punch, completing the current grace |472| period and starting a new one. |473| #. CPU 0 now reports its quiescent state for the wrong grace period. |474| That grace period might now end before the RCU read-side critical |475| section. If that happens, disaster will ensue. |476| |477| So the locking is absolutely required in order to coordinate clearing |478| of the bits with updating of the grace-period sequence number in |479| ``->gp_seq``. |480+-----------------------------------------------------------------------+481 482Blocked-Task Management483'''''''''''''''''''''''484 485``PREEMPT_RCU`` allows tasks to be preempted in the midst of their RCU486read-side critical sections, and these tasks must be tracked explicitly.487The details of exactly why and how they are tracked will be covered in a488separate article on RCU read-side processing. For now, it is enough to489know that the ``rcu_node`` structure tracks them.490 491::492 493 1 struct list_head blkd_tasks;494 2 struct list_head *gp_tasks;495 3 struct list_head *exp_tasks;496 4 bool wait_blkd_tasks;497 498The ``->blkd_tasks`` field is a list header for the list of blocked and499preempted tasks. As tasks undergo context switches within RCU read-side500critical sections, their ``task_struct`` structures are enqueued (via501the ``task_struct``'s ``->rcu_node_entry`` field) onto the head of the502``->blkd_tasks`` list for the leaf ``rcu_node`` structure corresponding503to the CPU on which the outgoing context switch executed. As these tasks504later exit their RCU read-side critical sections, they remove themselves505from the list. This list is therefore in reverse time order, so that if506one of the tasks is blocking the current grace period, all subsequent507tasks must also be blocking that same grace period. Therefore, a single508pointer into this list suffices to track all tasks blocking a given509grace period. That pointer is stored in ``->gp_tasks`` for normal grace510periods and in ``->exp_tasks`` for expedited grace periods. These last511two fields are ``NULL`` if either there is no grace period in flight or512if there are no blocked tasks preventing that grace period from513completing. If either of these two pointers is referencing a task that514removes itself from the ``->blkd_tasks`` list, then that task must515advance the pointer to the next task on the list, or set the pointer to516``NULL`` if there are no subsequent tasks on the list.517 518For example, suppose that tasks T1, T2, and T3 are all hard-affinitied519to the largest-numbered CPU in the system. Then if task T1 blocked in an520RCU read-side critical section, then an expedited grace period started,521then task T2 blocked in an RCU read-side critical section, then a normal522grace period started, and finally task 3 blocked in an RCU read-side523critical section, then the state of the last leaf ``rcu_node``524structure's blocked-task list would be as shown below:525 526.. kernel-figure:: blkd_task.svg527 528Task T1 is blocking both grace periods, task T2 is blocking only the529normal grace period, and task T3 is blocking neither grace period. Note530that these tasks will not remove themselves from this list immediately531upon resuming execution. They will instead remain on the list until they532execute the outermost ``rcu_read_unlock()`` that ends their RCU533read-side critical section.534 535The ``->wait_blkd_tasks`` field indicates whether or not the current536grace period is waiting on a blocked task.537 538Sizing the ``rcu_node`` Array539'''''''''''''''''''''''''''''540 541The ``rcu_node`` array is sized via a series of C-preprocessor542expressions as follows:543 544::545 546 1 #ifdef CONFIG_RCU_FANOUT547 2 #define RCU_FANOUT CONFIG_RCU_FANOUT548 3 #else549 4 # ifdef CONFIG_64BIT550 5 # define RCU_FANOUT 64551 6 # else552 7 # define RCU_FANOUT 32553 8 # endif554 9 #endif555 10556 11 #ifdef CONFIG_RCU_FANOUT_LEAF557 12 #define RCU_FANOUT_LEAF CONFIG_RCU_FANOUT_LEAF558 13 #else559 14 # ifdef CONFIG_64BIT560 15 # define RCU_FANOUT_LEAF 64561 16 # else562 17 # define RCU_FANOUT_LEAF 32563 18 # endif564 19 #endif565 20566 21 #define RCU_FANOUT_1 (RCU_FANOUT_LEAF)567 22 #define RCU_FANOUT_2 (RCU_FANOUT_1 * RCU_FANOUT)568 23 #define RCU_FANOUT_3 (RCU_FANOUT_2 * RCU_FANOUT)569 24 #define RCU_FANOUT_4 (RCU_FANOUT_3 * RCU_FANOUT)570 25571 26 #if NR_CPUS <= RCU_FANOUT_1572 27 # define RCU_NUM_LVLS 1573 28 # define NUM_RCU_LVL_0 1574 29 # define NUM_RCU_NODES NUM_RCU_LVL_0575 30 # define NUM_RCU_LVL_INIT { NUM_RCU_LVL_0 }576 31 # define RCU_NODE_NAME_INIT { "rcu_node_0" }577 32 # define RCU_FQS_NAME_INIT { "rcu_node_fqs_0" }578 33 # define RCU_EXP_NAME_INIT { "rcu_node_exp_0" }579 34 #elif NR_CPUS <= RCU_FANOUT_2580 35 # define RCU_NUM_LVLS 2581 36 # define NUM_RCU_LVL_0 1582 37 # define NUM_RCU_LVL_1 DIV_ROUND_UP(NR_CPUS, RCU_FANOUT_1)583 38 # define NUM_RCU_NODES (NUM_RCU_LVL_0 + NUM_RCU_LVL_1)584 39 # define NUM_RCU_LVL_INIT { NUM_RCU_LVL_0, NUM_RCU_LVL_1 }585 40 # define RCU_NODE_NAME_INIT { "rcu_node_0", "rcu_node_1" }586 41 # define RCU_FQS_NAME_INIT { "rcu_node_fqs_0", "rcu_node_fqs_1" }587 42 # define RCU_EXP_NAME_INIT { "rcu_node_exp_0", "rcu_node_exp_1" }588 43 #elif NR_CPUS <= RCU_FANOUT_3589 44 # define RCU_NUM_LVLS 3590 45 # define NUM_RCU_LVL_0 1591 46 # define NUM_RCU_LVL_1 DIV_ROUND_UP(NR_CPUS, RCU_FANOUT_2)592 47 # define NUM_RCU_LVL_2 DIV_ROUND_UP(NR_CPUS, RCU_FANOUT_1)593 48 # define NUM_RCU_NODES (NUM_RCU_LVL_0 + NUM_RCU_LVL_1 + NUM_RCU_LVL_2)594 49 # define NUM_RCU_LVL_INIT { NUM_RCU_LVL_0, NUM_RCU_LVL_1, NUM_RCU_LVL_2 }595 50 # define RCU_NODE_NAME_INIT { "rcu_node_0", "rcu_node_1", "rcu_node_2" }596 51 # define RCU_FQS_NAME_INIT { "rcu_node_fqs_0", "rcu_node_fqs_1", "rcu_node_fqs_2" }597 52 # define RCU_EXP_NAME_INIT { "rcu_node_exp_0", "rcu_node_exp_1", "rcu_node_exp_2" }598 53 #elif NR_CPUS <= RCU_FANOUT_4599 54 # define RCU_NUM_LVLS 4600 55 # define NUM_RCU_LVL_0 1601 56 # define NUM_RCU_LVL_1 DIV_ROUND_UP(NR_CPUS, RCU_FANOUT_3)602 57 # define NUM_RCU_LVL_2 DIV_ROUND_UP(NR_CPUS, RCU_FANOUT_2)603 58 # define NUM_RCU_LVL_3 DIV_ROUND_UP(NR_CPUS, RCU_FANOUT_1)604 59 # define NUM_RCU_NODES (NUM_RCU_LVL_0 + NUM_RCU_LVL_1 + NUM_RCU_LVL_2 + NUM_RCU_LVL_3)605 60 # define NUM_RCU_LVL_INIT { NUM_RCU_LVL_0, NUM_RCU_LVL_1, NUM_RCU_LVL_2, NUM_RCU_LVL_3 }606 61 # define RCU_NODE_NAME_INIT { "rcu_node_0", "rcu_node_1", "rcu_node_2", "rcu_node_3" }607 62 # define RCU_FQS_NAME_INIT { "rcu_node_fqs_0", "rcu_node_fqs_1", "rcu_node_fqs_2", "rcu_node_fqs_3" }608 63 # define RCU_EXP_NAME_INIT { "rcu_node_exp_0", "rcu_node_exp_1", "rcu_node_exp_2", "rcu_node_exp_3" }609 64 #else610 65 # error "CONFIG_RCU_FANOUT insufficient for NR_CPUS"611 66 #endif612 613The maximum number of levels in the ``rcu_node`` structure is currently614limited to four, as specified by lines 21-24 and the structure of the615subsequent “if” statement. For 32-bit systems, this allows61616*32*32*32=524,288 CPUs, which should be sufficient for the next few617years at least. For 64-bit systems, 16*64*64*64=4,194,304 CPUs is618allowed, which should see us through the next decade or so. This619four-level tree also allows kernels built with ``CONFIG_RCU_FANOUT=8``620to support up to 4096 CPUs, which might be useful in very large systems621having eight CPUs per socket (but please note that no one has yet shown622any measurable performance degradation due to misaligned socket and623``rcu_node`` boundaries). In addition, building kernels with a full four624levels of ``rcu_node`` tree permits better testing of RCU's625combining-tree code.626 627The ``RCU_FANOUT`` symbol controls how many children are permitted at628each non-leaf level of the ``rcu_node`` tree. If the629``CONFIG_RCU_FANOUT`` Kconfig option is not specified, it is set based630on the word size of the system, which is also the Kconfig default.631 632The ``RCU_FANOUT_LEAF`` symbol controls how many CPUs are handled by633each leaf ``rcu_node`` structure. Experience has shown that allowing a634given leaf ``rcu_node`` structure to handle 64 CPUs, as permitted by the635number of bits in the ``->qsmask`` field on a 64-bit system, results in636excessive contention for the leaf ``rcu_node`` structures' ``->lock``637fields. The number of CPUs per leaf ``rcu_node`` structure is therefore638limited to 16 given the default value of ``CONFIG_RCU_FANOUT_LEAF``. If639``CONFIG_RCU_FANOUT_LEAF`` is unspecified, the value selected is based640on the word size of the system, just as for ``CONFIG_RCU_FANOUT``.641Lines 11-19 perform this computation.642 643Lines 21-24 compute the maximum number of CPUs supported by a644single-level (which contains a single ``rcu_node`` structure),645two-level, three-level, and four-level ``rcu_node`` tree, respectively,646given the fanout specified by ``RCU_FANOUT`` and ``RCU_FANOUT_LEAF``.647These numbers of CPUs are retained in the ``RCU_FANOUT_1``,648``RCU_FANOUT_2``, ``RCU_FANOUT_3``, and ``RCU_FANOUT_4`` C-preprocessor649variables, respectively.650 651These variables are used to control the C-preprocessor ``#if`` statement652spanning lines 26-66 that computes the number of ``rcu_node`` structures653required for each level of the tree, as well as the number of levels654required. The number of levels is placed in the ``NUM_RCU_LVLS``655C-preprocessor variable by lines 27, 35, 44, and 54. The number of656``rcu_node`` structures for the topmost level of the tree is always657exactly one, and this value is unconditionally placed into658``NUM_RCU_LVL_0`` by lines 28, 36, 45, and 55. The rest of the levels659(if any) of the ``rcu_node`` tree are computed by dividing the maximum660number of CPUs by the fanout supported by the number of levels from the661current level down, rounding up. This computation is performed by662lines 37, 46-47, and 56-58. Lines 31-33, 40-42, 50-52, and 62-63 create663initializers for lockdep lock-class names. Finally, lines 64-66 produce664an error if the maximum number of CPUs is too large for the specified665fanout.666 667The ``rcu_segcblist`` Structure668~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~669 670The ``rcu_segcblist`` structure maintains a segmented list of callbacks671as follows:672 673::674 675 1 #define RCU_DONE_TAIL 0676 2 #define RCU_WAIT_TAIL 1677 3 #define RCU_NEXT_READY_TAIL 2678 4 #define RCU_NEXT_TAIL 3679 5 #define RCU_CBLIST_NSEGS 4680 6681 7 struct rcu_segcblist {682 8 struct rcu_head *head;683 9 struct rcu_head **tails[RCU_CBLIST_NSEGS];684 10 unsigned long gp_seq[RCU_CBLIST_NSEGS];685 11 long len;686 12 long len_lazy;687 13 };688 689The segments are as follows:690 691#. ``RCU_DONE_TAIL``: Callbacks whose grace periods have elapsed. These692 callbacks are ready to be invoked.693#. ``RCU_WAIT_TAIL``: Callbacks that are waiting for the current grace694 period. Note that different CPUs can have different ideas about which695 grace period is current, hence the ``->gp_seq`` field.696#. ``RCU_NEXT_READY_TAIL``: Callbacks waiting for the next grace period697 to start.698#. ``RCU_NEXT_TAIL``: Callbacks that have not yet been associated with a699 grace period.700 701The ``->head`` pointer references the first callback or is ``NULL`` if702the list contains no callbacks (which is *not* the same as being empty).703Each element of the ``->tails[]`` array references the ``->next``704pointer of the last callback in the corresponding segment of the list,705or the list's ``->head`` pointer if that segment and all previous706segments are empty. If the corresponding segment is empty but some707previous segment is not empty, then the array element is identical to708its predecessor. Older callbacks are closer to the head of the list, and709new callbacks are added at the tail. This relationship between the710``->head`` pointer, the ``->tails[]`` array, and the callbacks is shown711in this diagram:712 713.. kernel-figure:: nxtlist.svg714 715In this figure, the ``->head`` pointer references the first RCU callback716in the list. The ``->tails[RCU_DONE_TAIL]`` array element references the717``->head`` pointer itself, indicating that none of the callbacks is718ready to invoke. The ``->tails[RCU_WAIT_TAIL]`` array element references719callback CB 2's ``->next`` pointer, which indicates that CB 1 and CB 2720are both waiting on the current grace period, give or take possible721disagreements about exactly which grace period is the current one. The722``->tails[RCU_NEXT_READY_TAIL]`` array element references the same RCU723callback that ``->tails[RCU_WAIT_TAIL]`` does, which indicates that724there are no callbacks waiting on the next RCU grace period. The725``->tails[RCU_NEXT_TAIL]`` array element references CB 4's ``->next``726pointer, indicating that all the remaining RCU callbacks have not yet727been assigned to an RCU grace period. Note that the728``->tails[RCU_NEXT_TAIL]`` array element always references the last RCU729callback's ``->next`` pointer unless the callback list is empty, in730which case it references the ``->head`` pointer.731 732There is one additional important special case for the733``->tails[RCU_NEXT_TAIL]`` array element: It can be ``NULL`` when this734list is *disabled*. Lists are disabled when the corresponding CPU is735offline or when the corresponding CPU's callbacks are offloaded to a736kthread, both of which are described elsewhere.737 738CPUs advance their callbacks from the ``RCU_NEXT_TAIL`` to the739``RCU_NEXT_READY_TAIL`` to the ``RCU_WAIT_TAIL`` to the740``RCU_DONE_TAIL`` list segments as grace periods advance.741 742The ``->gp_seq[]`` array records grace-period numbers corresponding to743the list segments. This is what allows different CPUs to have different744ideas as to which is the current grace period while still avoiding745premature invocation of their callbacks. In particular, this allows CPUs746that go idle for extended periods to determine which of their callbacks747are ready to be invoked after reawakening.748 749The ``->len`` counter contains the number of callbacks in ``->head``,750and the ``->len_lazy`` contains the number of those callbacks that are751known to only free memory, and whose invocation can therefore be safely752deferred.753 754.. important::755 756 It is the ``->len`` field that determines whether or757 not there are callbacks associated with this ``rcu_segcblist``758 structure, *not* the ``->head`` pointer. The reason for this is that all759 the ready-to-invoke callbacks (that is, those in the ``RCU_DONE_TAIL``760 segment) are extracted all at once at callback-invocation time761 (``rcu_do_batch``), due to which ``->head`` may be set to NULL if there762 are no not-done callbacks remaining in the ``rcu_segcblist``. If763 callback invocation must be postponed, for example, because a764 high-priority process just woke up on this CPU, then the remaining765 callbacks are placed back on the ``RCU_DONE_TAIL`` segment and766 ``->head`` once again points to the start of the segment. In short, the767 head field can briefly be ``NULL`` even though the CPU has callbacks768 present the entire time. Therefore, it is not appropriate to test the769 ``->head`` pointer for ``NULL``.770 771In contrast, the ``->len`` and ``->len_lazy`` counts are adjusted only772after the corresponding callbacks have been invoked. This means that the773``->len`` count is zero only if the ``rcu_segcblist`` structure really774is devoid of callbacks. Of course, off-CPU sampling of the ``->len``775count requires careful use of appropriate synchronization, for example,776memory barriers. This synchronization can be a bit subtle, particularly777in the case of ``rcu_barrier()``.778 779The ``rcu_data`` Structure780~~~~~~~~~~~~~~~~~~~~~~~~~~781 782The ``rcu_data`` maintains the per-CPU state for the RCU subsystem. The783fields in this structure may be accessed only from the corresponding CPU784(and from tracing) unless otherwise stated. This structure is the focus785of quiescent-state detection and RCU callback queuing. It also tracks786its relationship to the corresponding leaf ``rcu_node`` structure to787allow more-efficient propagation of quiescent states up the ``rcu_node``788combining tree. Like the ``rcu_node`` structure, it provides a local789copy of the grace-period information to allow for-free synchronized790access to this information from the corresponding CPU. Finally, this791structure records past dyntick-idle state for the corresponding CPU and792also tracks statistics.793 794The ``rcu_data`` structure's fields are discussed, singly and in groups,795in the following sections.796 797Connection to Other Data Structures798'''''''''''''''''''''''''''''''''''799 800This portion of the ``rcu_data`` structure is declared as follows:801 802::803 804 1 int cpu;805 2 struct rcu_node *mynode;806 3 unsigned long grpmask;807 4 bool beenonline;808 809The ``->cpu`` field contains the number of the corresponding CPU and the810``->mynode`` field references the corresponding ``rcu_node`` structure.811The ``->mynode`` is used to propagate quiescent states up the combining812tree. These two fields are constant and therefore do not require813synchronization.814 815The ``->grpmask`` field indicates the bit in the ``->mynode->qsmask``816corresponding to this ``rcu_data`` structure, and is also used when817propagating quiescent states. The ``->beenonline`` flag is set whenever818the corresponding CPU comes online, which means that the debugfs tracing819need not dump out any ``rcu_data`` structure for which this flag is not820set.821 822Quiescent-State and Grace-Period Tracking823'''''''''''''''''''''''''''''''''''''''''824 825This portion of the ``rcu_data`` structure is declared as follows:826 827::828 829 1 unsigned long gp_seq;830 2 unsigned long gp_seq_needed;831 3 bool cpu_no_qs;832 4 bool core_needs_qs;833 5 bool gpwrap;834 835The ``->gp_seq`` field is the counterpart of the field of the same name836in the ``rcu_state`` and ``rcu_node`` structures. The837``->gp_seq_needed`` field is the counterpart of the field of the same838name in the rcu_node structure. They may each lag up to one behind their839``rcu_node`` counterparts, but in ``CONFIG_NO_HZ_IDLE`` and840``CONFIG_NO_HZ_FULL`` kernels can lag arbitrarily far behind for CPUs in841dyntick-idle mode (but these counters will catch up upon exit from842dyntick-idle mode). If the lower two bits of a given ``rcu_data``843structure's ``->gp_seq`` are zero, then this ``rcu_data`` structure844believes that RCU is idle.845 846+-----------------------------------------------------------------------+847| **Quick Quiz**: |848+-----------------------------------------------------------------------+849| All this replication of the grace period numbers can only cause |850| massive confusion. Why not just keep a global sequence number and be |851| done with it??? |852+-----------------------------------------------------------------------+853| **Answer**: |854+-----------------------------------------------------------------------+855| Because if there was only a single global sequence numbers, there |856| would need to be a single global lock to allow safely accessing and |857| updating it. And if we are not going to have a single global lock, we |858| need to carefully manage the numbers on a per-node basis. Recall from |859| the answer to a previous Quick Quiz that the consequences of applying |860| a previously sampled quiescent state to the wrong grace period are |861| quite severe. |862+-----------------------------------------------------------------------+863 864The ``->cpu_no_qs`` flag indicates that the CPU has not yet passed865through a quiescent state, while the ``->core_needs_qs`` flag indicates866that the RCU core needs a quiescent state from the corresponding CPU.867The ``->gpwrap`` field indicates that the corresponding CPU has remained868idle for so long that the ``gp_seq`` counter is in danger of overflow,869which will cause the CPU to disregard the values of its counters on its870next exit from idle.871 872RCU Callback Handling873'''''''''''''''''''''874 875In the absence of CPU-hotplug events, RCU callbacks are invoked by the876same CPU that registered them. This is strictly a cache-locality877optimization: callbacks can and do get invoked on CPUs other than the878one that registered them. After all, if the CPU that registered a given879callback has gone offline before the callback can be invoked, there880really is no other choice.881 882This portion of the ``rcu_data`` structure is declared as follows:883 884::885 886 1 struct rcu_segcblist cblist;887 2 long qlen_last_fqs_check;888 3 unsigned long n_cbs_invoked;889 4 unsigned long n_nocbs_invoked;890 5 unsigned long n_cbs_orphaned;891 6 unsigned long n_cbs_adopted;892 7 unsigned long n_force_qs_snap;893 8 long blimit;894 895The ``->cblist`` structure is the segmented callback list described896earlier. The CPU advances the callbacks in its ``rcu_data`` structure897whenever it notices that another RCU grace period has completed. The CPU898detects the completion of an RCU grace period by noticing that the value899of its ``rcu_data`` structure's ``->gp_seq`` field differs from that of900its leaf ``rcu_node`` structure. Recall that each ``rcu_node``901structure's ``->gp_seq`` field is updated at the beginnings and ends of902each grace period.903 904The ``->qlen_last_fqs_check`` and ``->n_force_qs_snap`` coordinate the905forcing of quiescent states from ``call_rcu()`` and friends when906callback lists grow excessively long.907 908The ``->n_cbs_invoked``, ``->n_cbs_orphaned``, and ``->n_cbs_adopted``909fields count the number of callbacks invoked, sent to other CPUs when910this CPU goes offline, and received from other CPUs when those other911CPUs go offline. The ``->n_nocbs_invoked`` is used when the CPU's912callbacks are offloaded to a kthread.913 914Finally, the ``->blimit`` counter is the maximum number of RCU callbacks915that may be invoked at a given time.916 917Dyntick-Idle Handling918'''''''''''''''''''''919 920This portion of the ``rcu_data`` structure is declared as follows:921 922::923 924 1 int watching_snap;925 2 unsigned long dynticks_fqs;926 927The ``->watching_snap`` field is used to take a snapshot of the928corresponding CPU's dyntick-idle state when forcing quiescent states,929and is therefore accessed from other CPUs. Finally, the930``->dynticks_fqs`` field is used to count the number of times this CPU931is determined to be in dyntick-idle state, and is used for tracing and932debugging purposes.933 934This portion of the rcu_data structure is declared as follows:935 936::937 938 1 long nesting;939 2 long nmi_nesting;940 3 atomic_t dynticks;941 4 bool rcu_need_heavy_qs;942 5 bool rcu_urgent_qs;943 944These fields in the rcu_data structure maintain the per-CPU dyntick-idle945state for the corresponding CPU. The fields may be accessed only from946the corresponding CPU (and from tracing) unless otherwise stated.947 948The ``->nesting`` field counts the nesting depth of process949execution, so that in normal circumstances this counter has value zero950or one. NMIs, irqs, and tracers are counted by the951``->nmi_nesting`` field. Because NMIs cannot be masked, changes952to this variable have to be undertaken carefully using an algorithm953provided by Andy Lutomirski. The initial transition from idle adds one,954and nested transitions add two, so that a nesting level of five is955represented by a ``->nmi_nesting`` value of nine. This counter956can therefore be thought of as counting the number of reasons why this957CPU cannot be permitted to enter dyntick-idle mode, aside from958process-level transitions.959 960However, it turns out that when running in non-idle kernel context, the961Linux kernel is fully capable of entering interrupt handlers that never962exit and perhaps also vice versa. Therefore, whenever the963``->nesting`` field is incremented up from zero, the964``->nmi_nesting`` field is set to a large positive number, and965whenever the ``->nesting`` field is decremented down to zero,966the ``->nmi_nesting`` field is set to zero. Assuming that967the number of misnested interrupts is not sufficient to overflow the968counter, this approach corrects the ``->nmi_nesting`` field969every time the corresponding CPU enters the idle loop from process970context.971 972The ``->dynticks`` field counts the corresponding CPU's transitions to973and from either dyntick-idle or user mode, so that this counter has an974even value when the CPU is in dyntick-idle mode or user mode and an odd975value otherwise. The transitions to/from user mode need to be counted976for user mode adaptive-ticks support (see Documentation/timers/no_hz.rst).977 978The ``->rcu_need_heavy_qs`` field is used to record the fact that the979RCU core code would really like to see a quiescent state from the980corresponding CPU, so much so that it is willing to call for981heavy-weight dyntick-counter operations. This flag is checked by RCU's982context-switch and ``cond_resched()`` code, which provide a momentary983idle sojourn in response.984 985Finally, the ``->rcu_urgent_qs`` field is used to record the fact that986the RCU core code would really like to see a quiescent state from the987corresponding CPU, with the various other fields indicating just how988badly RCU wants this quiescent state. This flag is checked by RCU's989context-switch path (``rcu_note_context_switch``) and the cond_resched990code.991 992+-----------------------------------------------------------------------+993| **Quick Quiz**: |994+-----------------------------------------------------------------------+995| Why not simply combine the ``->nesting`` and |996| ``->nmi_nesting`` counters into a single counter that just |997| counts the number of reasons that the corresponding CPU is non-idle? |998+-----------------------------------------------------------------------+999| **Answer**: |1000+-----------------------------------------------------------------------+1001| Because this would fail in the presence of interrupts whose handlers |1002| never return and of handlers that manage to return from a made-up |1003| interrupt. |1004+-----------------------------------------------------------------------+1005 1006Additional fields are present for some special-purpose builds, and are1007discussed separately.1008 1009The ``rcu_head`` Structure1010~~~~~~~~~~~~~~~~~~~~~~~~~~1011 1012Each ``rcu_head`` structure represents an RCU callback. These structures1013are normally embedded within RCU-protected data structures whose1014algorithms use asynchronous grace periods. In contrast, when using1015algorithms that block waiting for RCU grace periods, RCU users need not1016provide ``rcu_head`` structures.1017 1018The ``rcu_head`` structure has fields as follows:1019 1020::1021 1022 1 struct rcu_head *next;1023 2 void (*func)(struct rcu_head *head);1024 1025The ``->next`` field is used to link the ``rcu_head`` structures1026together in the lists within the ``rcu_data`` structures. The ``->func``1027field is a pointer to the function to be called when the callback is1028ready to be invoked, and this function is passed a pointer to the1029``rcu_head`` structure. However, ``kfree_rcu()`` uses the ``->func``1030field to record the offset of the ``rcu_head`` structure within the1031enclosing RCU-protected data structure.1032 1033Both of these fields are used internally by RCU. From the viewpoint of1034RCU users, this structure is an opaque “cookie”.1035 1036+-----------------------------------------------------------------------+1037| **Quick Quiz**: |1038+-----------------------------------------------------------------------+1039| Given that the callback function ``->func`` is passed a pointer to |1040| the ``rcu_head`` structure, how is that function supposed to find the |1041| beginning of the enclosing RCU-protected data structure? |1042+-----------------------------------------------------------------------+1043| **Answer**: |1044+-----------------------------------------------------------------------+1045| In actual practice, there is a separate callback function per type of |1046| RCU-protected data structure. The callback function can therefore use |1047| the ``container_of()`` macro in the Linux kernel (or other |1048| pointer-manipulation facilities in other software environments) to |1049| find the beginning of the enclosing structure. |1050+-----------------------------------------------------------------------+1051 1052RCU-Specific Fields in the ``task_struct`` Structure1053~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~1054 1055The ``CONFIG_PREEMPT_RCU`` implementation uses some additional fields in1056the ``task_struct`` structure:1057 1058::1059 1060 1 #ifdef CONFIG_PREEMPT_RCU1061 2 int rcu_read_lock_nesting;1062 3 union rcu_special rcu_read_unlock_special;1063 4 struct list_head rcu_node_entry;1064 5 struct rcu_node *rcu_blocked_node;1065 6 #endif /* #ifdef CONFIG_PREEMPT_RCU */1066 7 #ifdef CONFIG_TASKS_RCU1067 8 unsigned long rcu_tasks_nvcsw;1068 9 bool rcu_tasks_holdout;1069 10 struct list_head rcu_tasks_holdout_list;1070 11 int rcu_tasks_idle_cpu;1071 12 #endif /* #ifdef CONFIG_TASKS_RCU */1072 1073The ``->rcu_read_lock_nesting`` field records the nesting level for RCU1074read-side critical sections, and the ``->rcu_read_unlock_special`` field1075is a bitmask that records special conditions that require1076``rcu_read_unlock()`` to do additional work. The ``->rcu_node_entry``1077field is used to form lists of tasks that have blocked within1078preemptible-RCU read-side critical sections and the1079``->rcu_blocked_node`` field references the ``rcu_node`` structure whose1080list this task is a member of, or ``NULL`` if it is not blocked within a1081preemptible-RCU read-side critical section.1082 1083The ``->rcu_tasks_nvcsw`` field tracks the number of voluntary context1084switches that this task had undergone at the beginning of the current1085tasks-RCU grace period, ``->rcu_tasks_holdout`` is set if the current1086tasks-RCU grace period is waiting on this task,1087``->rcu_tasks_holdout_list`` is a list element enqueuing this task on1088the holdout list, and ``->rcu_tasks_idle_cpu`` tracks which CPU this1089idle task is running, but only if the task is currently running, that1090is, if the CPU is currently idle.1091 1092Accessor Functions1093~~~~~~~~~~~~~~~~~~1094 1095The following listing shows the ``rcu_get_root()``,1096``rcu_for_each_node_breadth_first`` and ``rcu_for_each_leaf_node()``1097function and macros:1098 1099::1100 1101 1 static struct rcu_node *rcu_get_root(struct rcu_state *rsp)1102 2 {1103 3 return &rsp->node[0];1104 4 }1105 51106 6 #define rcu_for_each_node_breadth_first(rsp, rnp) \1107 7 for ((rnp) = &(rsp)->node[0]; \1108 8 (rnp) < &(rsp)->node[NUM_RCU_NODES]; (rnp)++)1109 91110 10 #define rcu_for_each_leaf_node(rsp, rnp) \1111 11 for ((rnp) = (rsp)->level[NUM_RCU_LVLS - 1]; \1112 12 (rnp) < &(rsp)->node[NUM_RCU_NODES]; (rnp)++)1113 1114The ``rcu_get_root()`` simply returns a pointer to the first element of1115the specified ``rcu_state`` structure's ``->node[]`` array, which is the1116root ``rcu_node`` structure.1117 1118As noted earlier, the ``rcu_for_each_node_breadth_first()`` macro takes1119advantage of the layout of the ``rcu_node`` structures in the1120``rcu_state`` structure's ``->node[]`` array, performing a breadth-first1121traversal by simply traversing the array in order. Similarly, the1122``rcu_for_each_leaf_node()`` macro traverses only the last part of the1123array, thus traversing only the leaf ``rcu_node`` structures.1124 1125+-----------------------------------------------------------------------+1126| **Quick Quiz**: |1127+-----------------------------------------------------------------------+1128| What does ``rcu_for_each_leaf_node()`` do if the ``rcu_node`` tree |1129| contains only a single node? |1130+-----------------------------------------------------------------------+1131| **Answer**: |1132+-----------------------------------------------------------------------+1133| In the single-node case, ``rcu_for_each_leaf_node()`` traverses the |1134| single node. |1135+-----------------------------------------------------------------------+1136 1137Summary1138~~~~~~~1139 1140So the state of RCU is represented by an ``rcu_state`` structure, which1141contains a combining tree of ``rcu_node`` and ``rcu_data`` structures.1142Finally, in ``CONFIG_NO_HZ_IDLE`` kernels, each CPU's dyntick-idle state1143is tracked by dynticks-related fields in the ``rcu_data`` structure. If1144you made it this far, you are well prepared to read the code1145walkthroughs in the other articles in this series.1146 1147Acknowledgments1148~~~~~~~~~~~~~~~1149 1150I owe thanks to Cyrill Gorcunov, Mathieu Desnoyers, Dhaval Giani, Paul1151Turner, Abhishek Srivastava, Matt Kowalczyk, and Serge Hallyn for1152helping me get this document into a more human-readable state.1153 1154Legal Statement1155~~~~~~~~~~~~~~~1156 1157This work represents the view of the author and does not necessarily1158represent the view of IBM.1159 1160Linux is a registered trademark of Linus Torvalds.1161 1162Other company, product, and service names may be trademarks or service1163marks of others.1164