0001 ===================================================
0002 A Tour Through TREE_RCU's Data Structures [LWN.net]
0003 ===================================================
0004
0005 December 18, 2016
0006
0007 This article was contributed by Paul E. McKenney
0008
0009 Introduction
0010 ============
0011
0012 This document describes RCU's major data structures and their relationship
0013 to each other.
0014
0015 Data-Structure Relationships
0016 ============================
0017
0018 RCU is for all intents and purposes a large state machine, and its
0019 data structures maintain the state in such a way as to allow RCU readers
0020 to execute extremely quickly, while also processing the RCU grace periods
0021 requested by updaters in an efficient and extremely scalable fashion.
0022 The efficiency and scalability of RCU updaters is provided primarily
0023 by a combining tree, as shown below:
0024
0025 .. kernel-figure:: BigTreeClassicRCU.svg
0026
0027 This diagram shows an enclosing ``rcu_state`` structure containing a tree
0028 of ``rcu_node`` structures. Each leaf node of the ``rcu_node`` tree has up
0029 to 16 ``rcu_data`` structures associated with it, so that there are
0030 ``NR_CPUS`` number of ``rcu_data`` structures, one for each possible CPU.
0031 This structure is adjusted at boot time, if needed, to handle the common
0032 case where ``nr_cpu_ids`` is much less than ``NR_CPUs``.
0033 For example, a number of Linux distributions set ``NR_CPUs=4096``,
0034 which results in a three-level ``rcu_node`` tree.
0035 If the actual hardware has only 16 CPUs, RCU will adjust itself
0036 at boot time, resulting in an ``rcu_node`` tree with only a single node.
0037
0038 The purpose of this combining tree is to allow per-CPU events
0039 such as quiescent states, dyntick-idle transitions,
0040 and CPU hotplug operations to be processed efficiently
0041 and scalably.
0042 Quiescent states are recorded by the per-CPU ``rcu_data`` structures,
0043 and other events are recorded by the leaf-level ``rcu_node``
0044 structures.
0045 All of these events are combined at each level of the tree until finally
0046 grace periods are completed at the tree's root ``rcu_node``
0047 structure.
0048 A grace period can be completed at the root once every CPU
0049 (or, in the case of ``CONFIG_PREEMPT_RCU``, task)
0050 has passed through a quiescent state.
0051 Once a grace period has completed, record of that fact is propagated
0052 back down the tree.
0053
0054 As can be seen from the diagram, on a 64-bit system
0055 a two-level tree with 64 leaves can accommodate 1,024 CPUs, with a fanout
0056 of 64 at the root and a fanout of 16 at the leaves.
0057
0058 +-----------------------------------------------------------------------+
0059 | **Quick Quiz**: |
0060 +-----------------------------------------------------------------------+
0061 | Why isn't the fanout at the leaves also 64? |
0062 +-----------------------------------------------------------------------+
0063 | **Answer**: |
0064 +-----------------------------------------------------------------------+
0065 | Because there are more types of events that affect the leaf-level |
0066 | ``rcu_node`` structures than further up the tree. Therefore, if the |
0067 | leaf ``rcu_node`` structures have fanout of 64, the contention on |
0068 | these structures' ``->structures`` becomes excessive. Experimentation |
0069 | on a wide variety of systems has shown that a fanout of 16 works well |
0070 | for the leaves of the ``rcu_node`` tree. |
0071 | |
0072 | Of course, further experience with systems having hundreds or |
0073 | thousands of CPUs may demonstrate that the fanout for the non-leaf |
0074 | ``rcu_node`` structures must also be reduced. Such reduction can be |
0075 | easily carried out when and if it proves necessary. In the meantime, |
0076 | if you are using such a system and running into contention problems |
0077 | on the non-leaf ``rcu_node`` structures, you may use the |
0078 | ``CONFIG_RCU_FANOUT`` kernel configuration parameter to reduce the |
0079 | non-leaf fanout as needed. |
0080 | |
0081 | Kernels built for systems with strong NUMA characteristics might |
0082 | also need to adjust ``CONFIG_RCU_FANOUT`` so that the domains of |
0083 | the ``rcu_node`` structures align with hardware boundaries. |
0084 | However, there has thus far been no need for this. |
0085 +-----------------------------------------------------------------------+
0086
0087 If your system has more than 1,024 CPUs (or more than 512 CPUs on a
0088 32-bit system), then RCU will automatically add more levels to the tree.
0089 For example, if you are crazy enough to build a 64-bit system with
0090 65,536 CPUs, RCU would configure the ``rcu_node`` tree as follows:
0091
0092 .. kernel-figure:: HugeTreeClassicRCU.svg
0093
0094 RCU currently permits up to a four-level tree, which on a 64-bit system
0095 accommodates up to 4,194,304 CPUs, though only a mere 524,288 CPUs for
0096 32-bit systems. On the other hand, you can set both
0097 ``CONFIG_RCU_FANOUT`` and ``CONFIG_RCU_FANOUT_LEAF`` to be as small as
0098 2, which would result in a 16-CPU test using a 4-level tree. This can be
0099 useful for testing large-system capabilities on small test machines.
0100
0101 This multi-level combining tree allows us to get most of the performance
0102 and scalability benefits of partitioning, even though RCU grace-period
0103 detection is inherently a global operation. The trick here is that only
0104 the last CPU to report a quiescent state into a given ``rcu_node``
0105 structure need advance to the ``rcu_node`` structure at the next level
0106 up the tree. This means that at the leaf-level ``rcu_node`` structure,
0107 only one access out of sixteen will progress up the tree. For the
0108 internal ``rcu_node`` structures, the situation is even more extreme:
0109 Only one access out of sixty-four will progress up the tree. Because the
0110 vast majority of the CPUs do not progress up the tree, the lock
0111 contention remains roughly constant up the tree. No matter how many CPUs
0112 there are in the system, at most 64 quiescent-state reports per grace
0113 period will progress all the way to the root ``rcu_node`` structure,
0114 thus ensuring that the lock contention on that root ``rcu_node``
0115 structure remains acceptably low.
0116
0117 In effect, the combining tree acts like a big shock absorber, keeping
0118 lock contention under control at all tree levels regardless of the level
0119 of loading on the system.
0120
0121 RCU updaters wait for normal grace periods by registering RCU callbacks,
0122 either directly via ``call_rcu()`` or indirectly via
0123 ``synchronize_rcu()`` and friends. RCU callbacks are represented by
0124 ``rcu_head`` structures, which are queued on ``rcu_data`` structures
0125 while they are waiting for a grace period to elapse, as shown in the
0126 following figure:
0127
0128 .. kernel-figure:: BigTreePreemptRCUBHdyntickCB.svg
0129
0130 This figure shows how ``TREE_RCU``'s and ``PREEMPT_RCU``'s major data
0131 structures are related. Lesser data structures will be introduced with
0132 the algorithms that make use of them.
0133
0134 Note that each of the data structures in the above figure has its own
0135 synchronization:
0136
0137 #. Each ``rcu_state`` structures has a lock and a mutex, and some fields
0138 are protected by the corresponding root ``rcu_node`` structure's lock.
0139 #. Each ``rcu_node`` structure has a spinlock.
0140 #. The fields in ``rcu_data`` are private to the corresponding CPU,
0141 although a few can be read and written by other CPUs.
0142
0143 It is important to note that different data structures can have very
0144 different ideas about the state of RCU at any given time. For but one
0145 example, awareness of the start or end of a given RCU grace period
0146 propagates slowly through the data structures. This slow propagation is
0147 absolutely necessary for RCU to have good read-side performance. If this
0148 balkanized implementation seems foreign to you, one useful trick is to
0149 consider each instance of these data structures to be a different
0150 person, each having the usual slightly different view of reality.
0151
0152 The general role of each of these data structures is as follows:
0153
0154 #. ``rcu_state``: This structure forms the interconnection between the
0155 ``rcu_node`` and ``rcu_data`` structures, tracks grace periods,
0156 serves as short-term repository for callbacks orphaned by CPU-hotplug
0157 events, maintains ``rcu_barrier()`` state, tracks expedited
0158 grace-period state, and maintains state used to force quiescent
0159 states when grace periods extend too long,
0160 #. ``rcu_node``: This structure forms the combining tree that propagates
0161 quiescent-state information from the leaves to the root, and also
0162 propagates grace-period information from the root to the leaves. It
0163 provides local copies of the grace-period state in order to allow
0164 this information to be accessed in a synchronized manner without
0165 suffering the scalability limitations that would otherwise be imposed
0166 by global locking. In ``CONFIG_PREEMPT_RCU`` kernels, it manages the
0167 lists of tasks that have blocked while in their current RCU read-side
0168 critical section. In ``CONFIG_PREEMPT_RCU`` with
0169 ``CONFIG_RCU_BOOST``, it manages the per-\ ``rcu_node``
0170 priority-boosting kernel threads (kthreads) and state. Finally, it
0171 records CPU-hotplug state in order to determine which CPUs should be
0172 ignored during a given grace period.
0173 #. ``rcu_data``: This per-CPU structure is the focus of quiescent-state
0174 detection and RCU callback queuing. It also tracks its relationship
0175 to the corresponding leaf ``rcu_node`` structure to allow
0176 more-efficient propagation of quiescent states up the ``rcu_node``
0177 combining tree. Like the ``rcu_node`` structure, it provides a local
0178 copy of the grace-period information to allow for-free synchronized
0179 access to this information from the corresponding CPU. Finally, this
0180 structure records past dyntick-idle state for the corresponding CPU
0181 and also tracks statistics.
0182 #. ``rcu_head``: This structure represents RCU callbacks, and is the
0183 only structure allocated and managed by RCU users. The ``rcu_head``
0184 structure is normally embedded within the RCU-protected data
0185 structure.
0186
0187 If all you wanted from this article was a general notion of how RCU's
0188 data structures are related, you are done. Otherwise, each of the
0189 following sections give more details on the ``rcu_state``, ``rcu_node``
0190 and ``rcu_data`` data structures.
0191
0192 The ``rcu_state`` Structure
0193 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
0194
0195 The ``rcu_state`` structure is the base structure that represents the
0196 state of RCU in the system. This structure forms the interconnection
0197 between the ``rcu_node`` and ``rcu_data`` structures, tracks grace
0198 periods, contains the lock used to synchronize with CPU-hotplug events,
0199 and maintains state used to force quiescent states when grace periods
0200 extend too long,
0201
0202 A few of the ``rcu_state`` structure's fields are discussed, singly and
0203 in groups, in the following sections. The more specialized fields are
0204 covered in the discussion of their use.
0205
0206 Relationship to rcu_node and rcu_data Structures
0207 ''''''''''''''''''''''''''''''''''''''''''''''''
0208
0209 This portion of the ``rcu_state`` structure is declared as follows:
0210
0211 ::
0212
0213 1 struct rcu_node node[NUM_RCU_NODES];
0214 2 struct rcu_node *level[NUM_RCU_LVLS + 1];
0215 3 struct rcu_data __percpu *rda;
0216
0217 +-----------------------------------------------------------------------+
0218 | **Quick Quiz**: |
0219 +-----------------------------------------------------------------------+
0220 | Wait a minute! You said that the ``rcu_node`` structures formed a |
0221 | tree, but they are declared as a flat array! What gives? |
0222 +-----------------------------------------------------------------------+
0223 | **Answer**: |
0224 +-----------------------------------------------------------------------+
0225 | The tree is laid out in the array. The first node In the array is the |
0226 | head, the next set of nodes in the array are children of the head |
0227 | node, and so on until the last set of nodes in the array are the |
0228 | leaves. |
0229 | See the following diagrams to see how this works. |
0230 +-----------------------------------------------------------------------+
0231
0232 The ``rcu_node`` tree is embedded into the ``->node[]`` array as shown
0233 in the following figure:
0234
0235 .. kernel-figure:: TreeMapping.svg
0236
0237 One interesting consequence of this mapping is that a breadth-first
0238 traversal of the tree is implemented as a simple linear scan of the
0239 array, which is in fact what the ``rcu_for_each_node_breadth_first()``
0240 macro does. This macro is used at the beginning and ends of grace
0241 periods.
0242
0243 Each entry of the ``->level`` array references the first ``rcu_node``
0244 structure on the corresponding level of the tree, for example, as shown
0245 below:
0246
0247 .. kernel-figure:: TreeMappingLevel.svg
0248
0249 The zero\ :sup:`th` element of the array references the root
0250 ``rcu_node`` structure, the first element references the first child of
0251 the root ``rcu_node``, and finally the second element references the
0252 first leaf ``rcu_node`` structure.
0253
0254 For whatever it is worth, if you draw the tree to be tree-shaped rather
0255 than array-shaped, it is easy to draw a planar representation:
0256
0257 .. kernel-figure:: TreeLevel.svg
0258
0259 Finally, the ``->rda`` field references a per-CPU pointer to the
0260 corresponding CPU's ``rcu_data`` structure.
0261
0262 All of these fields are constant once initialization is complete, and
0263 therefore need no protection.
0264
0265 Grace-Period Tracking
0266 '''''''''''''''''''''
0267
0268 This portion of the ``rcu_state`` structure is declared as follows:
0269
0270 ::
0271
0272 1 unsigned long gp_seq;
0273
0274 RCU grace periods are numbered, and the ``->gp_seq`` field contains the
0275 current grace-period sequence number. The bottom two bits are the state
0276 of the current grace period, which can be zero for not yet started or
0277 one for in progress. In other words, if the bottom two bits of
0278 ``->gp_seq`` are zero, then RCU is idle. Any other value in the bottom
0279 two bits indicates that something is broken. This field is protected by
0280 the root ``rcu_node`` structure's ``->lock`` field.
0281
0282 There are ``->gp_seq`` fields in the ``rcu_node`` and ``rcu_data``
0283 structures as well. The fields in the ``rcu_state`` structure represent
0284 the most current value, and those of the other structures are compared
0285 in order to detect the beginnings and ends of grace periods in a
0286 distributed fashion. The values flow from ``rcu_state`` to ``rcu_node``
0287 (down the tree from the root to the leaves) to ``rcu_data``.
0288
0289 Miscellaneous
0290 '''''''''''''
0291
0292 This portion of the ``rcu_state`` structure is declared as follows:
0293
0294 ::
0295
0296 1 unsigned long gp_max;
0297 2 char abbr;
0298 3 char *name;
0299
0300 The ``->gp_max`` field tracks the duration of the longest grace period
0301 in jiffies. It is protected by the root ``rcu_node``'s ``->lock``.
0302
0303 The ``->name`` and ``->abbr`` fields distinguish between preemptible RCU
0304 (“rcu_preempt” and “p”) and non-preemptible RCU (“rcu_sched” and “s”).
0305 These fields are used for diagnostic and tracing purposes.
0306
0307 The ``rcu_node`` Structure
0308 ~~~~~~~~~~~~~~~~~~~~~~~~~~
0309
0310 The ``rcu_node`` structures form the combining tree that propagates
0311 quiescent-state information from the leaves to the root and also that
0312 propagates grace-period information from the root down to the leaves.
0313 They provides local copies of the grace-period state in order to allow
0314 this information to be accessed in a synchronized manner without
0315 suffering the scalability limitations that would otherwise be imposed by
0316 global locking. In ``CONFIG_PREEMPT_RCU`` kernels, they manage the lists
0317 of tasks that have blocked while in their current RCU read-side critical
0318 section. In ``CONFIG_PREEMPT_RCU`` with ``CONFIG_RCU_BOOST``, they
0319 manage the per-\ ``rcu_node`` priority-boosting kernel threads
0320 (kthreads) and state. Finally, they record CPU-hotplug state in order to
0321 determine which CPUs should be ignored during a given grace period.
0322
0323 The ``rcu_node`` structure's fields are discussed, singly and in groups,
0324 in the following sections.
0325
0326 Connection to Combining Tree
0327 ''''''''''''''''''''''''''''
0328
0329 This portion of the ``rcu_node`` structure is declared as follows:
0330
0331 ::
0332
0333 1 struct rcu_node *parent;
0334 2 u8 level;
0335 3 u8 grpnum;
0336 4 unsigned long grpmask;
0337 5 int grplo;
0338 6 int grphi;
0339
0340 The ``->parent`` pointer references the ``rcu_node`` one level up in the
0341 tree, and is ``NULL`` for the root ``rcu_node``. The RCU implementation
0342 makes heavy use of this field to push quiescent states up the tree. The
0343 ``->level`` field gives the level in the tree, with the root being at
0344 level zero, its children at level one, and so on. The ``->grpnum`` field
0345 gives this node's position within the children of its parent, so this
0346 number can range between 0 and 31 on 32-bit systems and between 0 and 63
0347 on 64-bit systems. The ``->level`` and ``->grpnum`` fields are used only
0348 during initialization and for tracing. The ``->grpmask`` field is the
0349 bitmask counterpart of ``->grpnum``, and therefore always has exactly
0350 one bit set. This mask is used to clear the bit corresponding to this
0351 ``rcu_node`` structure in its parent's bitmasks, which are described
0352 later. Finally, the ``->grplo`` and ``->grphi`` fields contain the
0353 lowest and highest numbered CPU served by this ``rcu_node`` structure,
0354 respectively.
0355
0356 All of these fields are constant, and thus do not require any
0357 synchronization.
0358
0359 Synchronization
0360 '''''''''''''''
0361
0362 This field of the ``rcu_node`` structure is declared as follows:
0363
0364 ::
0365
0366 1 raw_spinlock_t lock;
0367
0368 This field is used to protect the remaining fields in this structure,
0369 unless otherwise stated. That said, all of the fields in this structure
0370 can be accessed without locking for tracing purposes. Yes, this can
0371 result in confusing traces, but better some tracing confusion than to be
0372 heisenbugged out of existence.
0373
0374 .. _grace-period-tracking-1:
0375
0376 Grace-Period Tracking
0377 '''''''''''''''''''''
0378
0379 This portion of the ``rcu_node`` structure is declared as follows:
0380
0381 ::
0382
0383 1 unsigned long gp_seq;
0384 2 unsigned long gp_seq_needed;
0385
0386 The ``rcu_node`` structures' ``->gp_seq`` fields are the counterparts of
0387 the field of the same name in the ``rcu_state`` structure. They each may
0388 lag up to one step behind their ``rcu_state`` counterpart. If the bottom
0389 two bits of a given ``rcu_node`` structure's ``->gp_seq`` field is zero,
0390 then this ``rcu_node`` structure believes that RCU is idle.
0391
0392 The ``>gp_seq`` field of each ``rcu_node`` structure is updated at the
0393 beginning and the end of each grace period.
0394
0395 The ``->gp_seq_needed`` fields record the furthest-in-the-future grace
0396 period request seen by the corresponding ``rcu_node`` structure. The
0397 request is considered fulfilled when the value of the ``->gp_seq`` field
0398 equals or exceeds that of the ``->gp_seq_needed`` field.
0399
0400 +-----------------------------------------------------------------------+
0401 | **Quick Quiz**: |
0402 +-----------------------------------------------------------------------+
0403 | Suppose that this ``rcu_node`` structure doesn't see a request for a |
0404 | very long time. Won't wrapping of the ``->gp_seq`` field cause |
0405 | problems? |
0406 +-----------------------------------------------------------------------+
0407 | **Answer**: |
0408 +-----------------------------------------------------------------------+
0409 | No, because if the ``->gp_seq_needed`` field lags behind the |
0410 | ``->gp_seq`` field, the ``->gp_seq_needed`` field will be updated at |
0411 | the end of the grace period. Modulo-arithmetic comparisons therefore |
0412 | will always get the correct answer, even with wrapping. |
0413 +-----------------------------------------------------------------------+
0414
0415 Quiescent-State Tracking
0416 ''''''''''''''''''''''''
0417
0418 These fields manage the propagation of quiescent states up the combining
0419 tree.
0420
0421 This portion of the ``rcu_node`` structure has fields as follows:
0422
0423 ::
0424
0425 1 unsigned long qsmask;
0426 2 unsigned long expmask;
0427 3 unsigned long qsmaskinit;
0428 4 unsigned long expmaskinit;
0429
0430 The ``->qsmask`` field tracks which of this ``rcu_node`` structure's
0431 children still need to report quiescent states for the current normal
0432 grace period. Such children will have a value of 1 in their
0433 corresponding bit. Note that the leaf ``rcu_node`` structures should be
0434 thought of as having ``rcu_data`` structures as their children.
0435 Similarly, the ``->expmask`` field tracks which of this ``rcu_node``
0436 structure's children still need to report quiescent states for the
0437 current expedited grace period. An expedited grace period has the same
0438 conceptual properties as a normal grace period, but the expedited
0439 implementation accepts extreme CPU overhead to obtain much lower
0440 grace-period latency, for example, consuming a few tens of microseconds
0441 worth of CPU time to reduce grace-period duration from milliseconds to
0442 tens of microseconds. The ``->qsmaskinit`` field tracks which of this
0443 ``rcu_node`` structure's children cover for at least one online CPU.
0444 This mask is used to initialize ``->qsmask``, and ``->expmaskinit`` is
0445 used to initialize ``->expmask`` and the beginning of the normal and
0446 expedited grace periods, respectively.
0447
0448 +-----------------------------------------------------------------------+
0449 | **Quick Quiz**: |
0450 +-----------------------------------------------------------------------+
0451 | Why are these bitmasks protected by locking? Come on, haven't you |
0452 | heard of atomic instructions??? |
0453 +-----------------------------------------------------------------------+
0454 | **Answer**: |
0455 +-----------------------------------------------------------------------+
0456 | Lockless grace-period computation! Such a tantalizing possibility! |
0457 | But consider the following sequence of events: |
0458 | |
0459 | #. CPU 0 has been in dyntick-idle mode for quite some time. When it |
0460 | wakes up, it notices that the current RCU grace period needs it to |
0461 | report in, so it sets a flag where the scheduling clock interrupt |
0462 | will find it. |
0463 | #. Meanwhile, CPU 1 is running ``force_quiescent_state()``, and |
0464 | notices that CPU 0 has been in dyntick idle mode, which qualifies |
0465 | as an extended quiescent state. |
0466 | #. CPU 0's scheduling clock interrupt fires in the middle of an RCU |
0467 | read-side critical section, and notices that the RCU core needs |
0468 | something, so commences RCU softirq processing. |
0469 | #. CPU 0's softirq handler executes and is just about ready to report |
0470 | its quiescent state up the ``rcu_node`` tree. |
0471 | #. But CPU 1 beats it to the punch, completing the current grace |
0472 | period and starting a new one. |
0473 | #. CPU 0 now reports its quiescent state for the wrong grace period. |
0474 | That grace period might now end before the RCU read-side critical |
0475 | section. If that happens, disaster will ensue. |
0476 | |
0477 | So the locking is absolutely required in order to coordinate clearing |
0478 | of the bits with updating of the grace-period sequence number in |
0479 | ``->gp_seq``. |
0480 +-----------------------------------------------------------------------+
0481
0482 Blocked-Task Management
0483 '''''''''''''''''''''''
0484
0485 ``PREEMPT_RCU`` allows tasks to be preempted in the midst of their RCU
0486 read-side critical sections, and these tasks must be tracked explicitly.
0487 The details of exactly why and how they are tracked will be covered in a
0488 separate article on RCU read-side processing. For now, it is enough to
0489 know that the ``rcu_node`` structure tracks them.
0490
0491 ::
0492
0493 1 struct list_head blkd_tasks;
0494 2 struct list_head *gp_tasks;
0495 3 struct list_head *exp_tasks;
0496 4 bool wait_blkd_tasks;
0497
0498 The ``->blkd_tasks`` field is a list header for the list of blocked and
0499 preempted tasks. As tasks undergo context switches within RCU read-side
0500 critical sections, their ``task_struct`` structures are enqueued (via
0501 the ``task_struct``'s ``->rcu_node_entry`` field) onto the head of the
0502 ``->blkd_tasks`` list for the leaf ``rcu_node`` structure corresponding
0503 to the CPU on which the outgoing context switch executed. As these tasks
0504 later exit their RCU read-side critical sections, they remove themselves
0505 from the list. This list is therefore in reverse time order, so that if
0506 one of the tasks is blocking the current grace period, all subsequent
0507 tasks must also be blocking that same grace period. Therefore, a single
0508 pointer into this list suffices to track all tasks blocking a given
0509 grace period. That pointer is stored in ``->gp_tasks`` for normal grace
0510 periods and in ``->exp_tasks`` for expedited grace periods. These last
0511 two fields are ``NULL`` if either there is no grace period in flight or
0512 if there are no blocked tasks preventing that grace period from
0513 completing. If either of these two pointers is referencing a task that
0514 removes itself from the ``->blkd_tasks`` list, then that task must
0515 advance the pointer to the next task on the list, or set the pointer to
0516 ``NULL`` if there are no subsequent tasks on the list.
0517
0518 For example, suppose that tasks T1, T2, and T3 are all hard-affinitied
0519 to the largest-numbered CPU in the system. Then if task T1 blocked in an
0520 RCU read-side critical section, then an expedited grace period started,
0521 then task T2 blocked in an RCU read-side critical section, then a normal
0522 grace period started, and finally task 3 blocked in an RCU read-side
0523 critical section, then the state of the last leaf ``rcu_node``
0524 structure's blocked-task list would be as shown below:
0525
0526 .. kernel-figure:: blkd_task.svg
0527
0528 Task T1 is blocking both grace periods, task T2 is blocking only the
0529 normal grace period, and task T3 is blocking neither grace period. Note
0530 that these tasks will not remove themselves from this list immediately
0531 upon resuming execution. They will instead remain on the list until they
0532 execute the outermost ``rcu_read_unlock()`` that ends their RCU
0533 read-side critical section.
0534
0535 The ``->wait_blkd_tasks`` field indicates whether or not the current
0536 grace period is waiting on a blocked task.
0537
0538 Sizing the ``rcu_node`` Array
0539 '''''''''''''''''''''''''''''
0540
0541 The ``rcu_node`` array is sized via a series of C-preprocessor
0542 expressions as follows:
0543
0544 ::
0545
0546 1 #ifdef CONFIG_RCU_FANOUT
0547 2 #define RCU_FANOUT CONFIG_RCU_FANOUT
0548 3 #else
0549 4 # ifdef CONFIG_64BIT
0550 5 # define RCU_FANOUT 64
0551 6 # else
0552 7 # define RCU_FANOUT 32
0553 8 # endif
0554 9 #endif
0555 10
0556 11 #ifdef CONFIG_RCU_FANOUT_LEAF
0557 12 #define RCU_FANOUT_LEAF CONFIG_RCU_FANOUT_LEAF
0558 13 #else
0559 14 # ifdef CONFIG_64BIT
0560 15 # define RCU_FANOUT_LEAF 64
0561 16 # else
0562 17 # define RCU_FANOUT_LEAF 32
0563 18 # endif
0564 19 #endif
0565 20
0566 21 #define RCU_FANOUT_1 (RCU_FANOUT_LEAF)
0567 22 #define RCU_FANOUT_2 (RCU_FANOUT_1 * RCU_FANOUT)
0568 23 #define RCU_FANOUT_3 (RCU_FANOUT_2 * RCU_FANOUT)
0569 24 #define RCU_FANOUT_4 (RCU_FANOUT_3 * RCU_FANOUT)
0570 25
0571 26 #if NR_CPUS <= RCU_FANOUT_1
0572 27 # define RCU_NUM_LVLS 1
0573 28 # define NUM_RCU_LVL_0 1
0574 29 # define NUM_RCU_NODES NUM_RCU_LVL_0
0575 30 # define NUM_RCU_LVL_INIT { NUM_RCU_LVL_0 }
0576 31 # define RCU_NODE_NAME_INIT { "rcu_node_0" }
0577 32 # define RCU_FQS_NAME_INIT { "rcu_node_fqs_0" }
0578 33 # define RCU_EXP_NAME_INIT { "rcu_node_exp_0" }
0579 34 #elif NR_CPUS <= RCU_FANOUT_2
0580 35 # define RCU_NUM_LVLS 2
0581 36 # define NUM_RCU_LVL_0 1
0582 37 # define NUM_RCU_LVL_1 DIV_ROUND_UP(NR_CPUS, RCU_FANOUT_1)
0583 38 # define NUM_RCU_NODES (NUM_RCU_LVL_0 + NUM_RCU_LVL_1)
0584 39 # define NUM_RCU_LVL_INIT { NUM_RCU_LVL_0, NUM_RCU_LVL_1 }
0585 40 # define RCU_NODE_NAME_INIT { "rcu_node_0", "rcu_node_1" }
0586 41 # define RCU_FQS_NAME_INIT { "rcu_node_fqs_0", "rcu_node_fqs_1" }
0587 42 # define RCU_EXP_NAME_INIT { "rcu_node_exp_0", "rcu_node_exp_1" }
0588 43 #elif NR_CPUS <= RCU_FANOUT_3
0589 44 # define RCU_NUM_LVLS 3
0590 45 # define NUM_RCU_LVL_0 1
0591 46 # define NUM_RCU_LVL_1 DIV_ROUND_UP(NR_CPUS, RCU_FANOUT_2)
0592 47 # define NUM_RCU_LVL_2 DIV_ROUND_UP(NR_CPUS, RCU_FANOUT_1)
0593 48 # define NUM_RCU_NODES (NUM_RCU_LVL_0 + NUM_RCU_LVL_1 + NUM_RCU_LVL_2)
0594 49 # define NUM_RCU_LVL_INIT { NUM_RCU_LVL_0, NUM_RCU_LVL_1, NUM_RCU_LVL_2 }
0595 50 # define RCU_NODE_NAME_INIT { "rcu_node_0", "rcu_node_1", "rcu_node_2" }
0596 51 # define RCU_FQS_NAME_INIT { "rcu_node_fqs_0", "rcu_node_fqs_1", "rcu_node_fqs_2" }
0597 52 # define RCU_EXP_NAME_INIT { "rcu_node_exp_0", "rcu_node_exp_1", "rcu_node_exp_2" }
0598 53 #elif NR_CPUS <= RCU_FANOUT_4
0599 54 # define RCU_NUM_LVLS 4
0600 55 # define NUM_RCU_LVL_0 1
0601 56 # define NUM_RCU_LVL_1 DIV_ROUND_UP(NR_CPUS, RCU_FANOUT_3)
0602 57 # define NUM_RCU_LVL_2 DIV_ROUND_UP(NR_CPUS, RCU_FANOUT_2)
0603 58 # define NUM_RCU_LVL_3 DIV_ROUND_UP(NR_CPUS, RCU_FANOUT_1)
0604 59 # define NUM_RCU_NODES (NUM_RCU_LVL_0 + NUM_RCU_LVL_1 + NUM_RCU_LVL_2 + NUM_RCU_LVL_3)
0605 60 # define NUM_RCU_LVL_INIT { NUM_RCU_LVL_0, NUM_RCU_LVL_1, NUM_RCU_LVL_2, NUM_RCU_LVL_3 }
0606 61 # define RCU_NODE_NAME_INIT { "rcu_node_0", "rcu_node_1", "rcu_node_2", "rcu_node_3" }
0607 62 # define RCU_FQS_NAME_INIT { "rcu_node_fqs_0", "rcu_node_fqs_1", "rcu_node_fqs_2", "rcu_node_fqs_3" }
0608 63 # define RCU_EXP_NAME_INIT { "rcu_node_exp_0", "rcu_node_exp_1", "rcu_node_exp_2", "rcu_node_exp_3" }
0609 64 #else
0610 65 # error "CONFIG_RCU_FANOUT insufficient for NR_CPUS"
0611 66 #endif
0612
0613 The maximum number of levels in the ``rcu_node`` structure is currently
0614 limited to four, as specified by lines 21-24 and the structure of the
0615 subsequent “if” statement. For 32-bit systems, this allows
0616 16*32*32*32=524,288 CPUs, which should be sufficient for the next few
0617 years at least. For 64-bit systems, 16*64*64*64=4,194,304 CPUs is
0618 allowed, which should see us through the next decade or so. This
0619 four-level tree also allows kernels built with ``CONFIG_RCU_FANOUT=8``
0620 to support up to 4096 CPUs, which might be useful in very large systems
0621 having eight CPUs per socket (but please note that no one has yet shown
0622 any measurable performance degradation due to misaligned socket and
0623 ``rcu_node`` boundaries). In addition, building kernels with a full four
0624 levels of ``rcu_node`` tree permits better testing of RCU's
0625 combining-tree code.
0626
0627 The ``RCU_FANOUT`` symbol controls how many children are permitted at
0628 each non-leaf level of the ``rcu_node`` tree. If the
0629 ``CONFIG_RCU_FANOUT`` Kconfig option is not specified, it is set based
0630 on the word size of the system, which is also the Kconfig default.
0631
0632 The ``RCU_FANOUT_LEAF`` symbol controls how many CPUs are handled by
0633 each leaf ``rcu_node`` structure. Experience has shown that allowing a
0634 given leaf ``rcu_node`` structure to handle 64 CPUs, as permitted by the
0635 number of bits in the ``->qsmask`` field on a 64-bit system, results in
0636 excessive contention for the leaf ``rcu_node`` structures' ``->lock``
0637 fields. The number of CPUs per leaf ``rcu_node`` structure is therefore
0638 limited to 16 given the default value of ``CONFIG_RCU_FANOUT_LEAF``. If
0639 ``CONFIG_RCU_FANOUT_LEAF`` is unspecified, the value selected is based
0640 on the word size of the system, just as for ``CONFIG_RCU_FANOUT``.
0641 Lines 11-19 perform this computation.
0642
0643 Lines 21-24 compute the maximum number of CPUs supported by a
0644 single-level (which contains a single ``rcu_node`` structure),
0645 two-level, three-level, and four-level ``rcu_node`` tree, respectively,
0646 given the fanout specified by ``RCU_FANOUT`` and ``RCU_FANOUT_LEAF``.
0647 These numbers of CPUs are retained in the ``RCU_FANOUT_1``,
0648 ``RCU_FANOUT_2``, ``RCU_FANOUT_3``, and ``RCU_FANOUT_4`` C-preprocessor
0649 variables, respectively.
0650
0651 These variables are used to control the C-preprocessor ``#if`` statement
0652 spanning lines 26-66 that computes the number of ``rcu_node`` structures
0653 required for each level of the tree, as well as the number of levels
0654 required. The number of levels is placed in the ``NUM_RCU_LVLS``
0655 C-preprocessor variable by lines 27, 35, 44, and 54. The number of
0656 ``rcu_node`` structures for the topmost level of the tree is always
0657 exactly one, and this value is unconditionally placed into
0658 ``NUM_RCU_LVL_0`` by lines 28, 36, 45, and 55. The rest of the levels
0659 (if any) of the ``rcu_node`` tree are computed by dividing the maximum
0660 number of CPUs by the fanout supported by the number of levels from the
0661 current level down, rounding up. This computation is performed by
0662 lines 37, 46-47, and 56-58. Lines 31-33, 40-42, 50-52, and 62-63 create
0663 initializers for lockdep lock-class names. Finally, lines 64-66 produce
0664 an error if the maximum number of CPUs is too large for the specified
0665 fanout.
0666
0667 The ``rcu_segcblist`` Structure
0668 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0669
0670 The ``rcu_segcblist`` structure maintains a segmented list of callbacks
0671 as follows:
0672
0673 ::
0674
0675 1 #define RCU_DONE_TAIL 0
0676 2 #define RCU_WAIT_TAIL 1
0677 3 #define RCU_NEXT_READY_TAIL 2
0678 4 #define RCU_NEXT_TAIL 3
0679 5 #define RCU_CBLIST_NSEGS 4
0680 6
0681 7 struct rcu_segcblist {
0682 8 struct rcu_head *head;
0683 9 struct rcu_head **tails[RCU_CBLIST_NSEGS];
0684 10 unsigned long gp_seq[RCU_CBLIST_NSEGS];
0685 11 long len;
0686 12 long len_lazy;
0687 13 };
0688
0689 The segments are as follows:
0690
0691 #. ``RCU_DONE_TAIL``: Callbacks whose grace periods have elapsed. These
0692 callbacks are ready to be invoked.
0693 #. ``RCU_WAIT_TAIL``: Callbacks that are waiting for the current grace
0694 period. Note that different CPUs can have different ideas about which
0695 grace period is current, hence the ``->gp_seq`` field.
0696 #. ``RCU_NEXT_READY_TAIL``: Callbacks waiting for the next grace period
0697 to start.
0698 #. ``RCU_NEXT_TAIL``: Callbacks that have not yet been associated with a
0699 grace period.
0700
0701 The ``->head`` pointer references the first callback or is ``NULL`` if
0702 the list contains no callbacks (which is *not* the same as being empty).
0703 Each element of the ``->tails[]`` array references the ``->next``
0704 pointer of the last callback in the corresponding segment of the list,
0705 or the list's ``->head`` pointer if that segment and all previous
0706 segments are empty. If the corresponding segment is empty but some
0707 previous segment is not empty, then the array element is identical to
0708 its predecessor. Older callbacks are closer to the head of the list, and
0709 new callbacks are added at the tail. This relationship between the
0710 ``->head`` pointer, the ``->tails[]`` array, and the callbacks is shown
0711 in this diagram:
0712
0713 .. kernel-figure:: nxtlist.svg
0714
0715 In this figure, the ``->head`` pointer references the first RCU callback
0716 in the list. The ``->tails[RCU_DONE_TAIL]`` array element references the
0717 ``->head`` pointer itself, indicating that none of the callbacks is
0718 ready to invoke. The ``->tails[RCU_WAIT_TAIL]`` array element references
0719 callback CB 2's ``->next`` pointer, which indicates that CB 1 and CB 2
0720 are both waiting on the current grace period, give or take possible
0721 disagreements about exactly which grace period is the current one. The
0722 ``->tails[RCU_NEXT_READY_TAIL]`` array element references the same RCU
0723 callback that ``->tails[RCU_WAIT_TAIL]`` does, which indicates that
0724 there are no callbacks waiting on the next RCU grace period. The
0725 ``->tails[RCU_NEXT_TAIL]`` array element references CB 4's ``->next``
0726 pointer, indicating that all the remaining RCU callbacks have not yet
0727 been assigned to an RCU grace period. Note that the
0728 ``->tails[RCU_NEXT_TAIL]`` array element always references the last RCU
0729 callback's ``->next`` pointer unless the callback list is empty, in
0730 which case it references the ``->head`` pointer.
0731
0732 There is one additional important special case for the
0733 ``->tails[RCU_NEXT_TAIL]`` array element: It can be ``NULL`` when this
0734 list is *disabled*. Lists are disabled when the corresponding CPU is
0735 offline or when the corresponding CPU's callbacks are offloaded to a
0736 kthread, both of which are described elsewhere.
0737
0738 CPUs advance their callbacks from the ``RCU_NEXT_TAIL`` to the
0739 ``RCU_NEXT_READY_TAIL`` to the ``RCU_WAIT_TAIL`` to the
0740 ``RCU_DONE_TAIL`` list segments as grace periods advance.
0741
0742 The ``->gp_seq[]`` array records grace-period numbers corresponding to
0743 the list segments. This is what allows different CPUs to have different
0744 ideas as to which is the current grace period while still avoiding
0745 premature invocation of their callbacks. In particular, this allows CPUs
0746 that go idle for extended periods to determine which of their callbacks
0747 are ready to be invoked after reawakening.
0748
0749 The ``->len`` counter contains the number of callbacks in ``->head``,
0750 and the ``->len_lazy`` contains the number of those callbacks that are
0751 known to only free memory, and whose invocation can therefore be safely
0752 deferred.
0753
0754 .. important::
0755
0756 It is the ``->len`` field that determines whether or
0757 not there are callbacks associated with this ``rcu_segcblist``
0758 structure, *not* the ``->head`` pointer. The reason for this is that all
0759 the ready-to-invoke callbacks (that is, those in the ``RCU_DONE_TAIL``
0760 segment) are extracted all at once at callback-invocation time
0761 (``rcu_do_batch``), due to which ``->head`` may be set to NULL if there
0762 are no not-done callbacks remaining in the ``rcu_segcblist``. If
0763 callback invocation must be postponed, for example, because a
0764 high-priority process just woke up on this CPU, then the remaining
0765 callbacks are placed back on the ``RCU_DONE_TAIL`` segment and
0766 ``->head`` once again points to the start of the segment. In short, the
0767 head field can briefly be ``NULL`` even though the CPU has callbacks
0768 present the entire time. Therefore, it is not appropriate to test the
0769 ``->head`` pointer for ``NULL``.
0770
0771 In contrast, the ``->len`` and ``->len_lazy`` counts are adjusted only
0772 after the corresponding callbacks have been invoked. This means that the
0773 ``->len`` count is zero only if the ``rcu_segcblist`` structure really
0774 is devoid of callbacks. Of course, off-CPU sampling of the ``->len``
0775 count requires careful use of appropriate synchronization, for example,
0776 memory barriers. This synchronization can be a bit subtle, particularly
0777 in the case of ``rcu_barrier()``.
0778
0779 The ``rcu_data`` Structure
0780 ~~~~~~~~~~~~~~~~~~~~~~~~~~
0781
0782 The ``rcu_data`` maintains the per-CPU state for the RCU subsystem. The
0783 fields in this structure may be accessed only from the corresponding CPU
0784 (and from tracing) unless otherwise stated. This structure is the focus
0785 of quiescent-state detection and RCU callback queuing. It also tracks
0786 its relationship to the corresponding leaf ``rcu_node`` structure to
0787 allow more-efficient propagation of quiescent states up the ``rcu_node``
0788 combining tree. Like the ``rcu_node`` structure, it provides a local
0789 copy of the grace-period information to allow for-free synchronized
0790 access to this information from the corresponding CPU. Finally, this
0791 structure records past dyntick-idle state for the corresponding CPU and
0792 also tracks statistics.
0793
0794 The ``rcu_data`` structure's fields are discussed, singly and in groups,
0795 in the following sections.
0796
0797 Connection to Other Data Structures
0798 '''''''''''''''''''''''''''''''''''
0799
0800 This portion of the ``rcu_data`` structure is declared as follows:
0801
0802 ::
0803
0804 1 int cpu;
0805 2 struct rcu_node *mynode;
0806 3 unsigned long grpmask;
0807 4 bool beenonline;
0808
0809 The ``->cpu`` field contains the number of the corresponding CPU and the
0810 ``->mynode`` field references the corresponding ``rcu_node`` structure.
0811 The ``->mynode`` is used to propagate quiescent states up the combining
0812 tree. These two fields are constant and therefore do not require
0813 synchronization.
0814
0815 The ``->grpmask`` field indicates the bit in the ``->mynode->qsmask``
0816 corresponding to this ``rcu_data`` structure, and is also used when
0817 propagating quiescent states. The ``->beenonline`` flag is set whenever
0818 the corresponding CPU comes online, which means that the debugfs tracing
0819 need not dump out any ``rcu_data`` structure for which this flag is not
0820 set.
0821
0822 Quiescent-State and Grace-Period Tracking
0823 '''''''''''''''''''''''''''''''''''''''''
0824
0825 This portion of the ``rcu_data`` structure is declared as follows:
0826
0827 ::
0828
0829 1 unsigned long gp_seq;
0830 2 unsigned long gp_seq_needed;
0831 3 bool cpu_no_qs;
0832 4 bool core_needs_qs;
0833 5 bool gpwrap;
0834
0835 The ``->gp_seq`` field is the counterpart of the field of the same name
0836 in the ``rcu_state`` and ``rcu_node`` structures. The
0837 ``->gp_seq_needed`` field is the counterpart of the field of the same
0838 name in the rcu_node structure. They may each lag up to one behind their
0839 ``rcu_node`` counterparts, but in ``CONFIG_NO_HZ_IDLE`` and
0840 ``CONFIG_NO_HZ_FULL`` kernels can lag arbitrarily far behind for CPUs in
0841 dyntick-idle mode (but these counters will catch up upon exit from
0842 dyntick-idle mode). If the lower two bits of a given ``rcu_data``
0843 structure's ``->gp_seq`` are zero, then this ``rcu_data`` structure
0844 believes that RCU is idle.
0845
0846 +-----------------------------------------------------------------------+
0847 | **Quick Quiz**: |
0848 +-----------------------------------------------------------------------+
0849 | All this replication of the grace period numbers can only cause |
0850 | massive confusion. Why not just keep a global sequence number and be |
0851 | done with it??? |
0852 +-----------------------------------------------------------------------+
0853 | **Answer**: |
0854 +-----------------------------------------------------------------------+
0855 | Because if there was only a single global sequence numbers, there |
0856 | would need to be a single global lock to allow safely accessing and |
0857 | updating it. And if we are not going to have a single global lock, we |
0858 | need to carefully manage the numbers on a per-node basis. Recall from |
0859 | the answer to a previous Quick Quiz that the consequences of applying |
0860 | a previously sampled quiescent state to the wrong grace period are |
0861 | quite severe. |
0862 +-----------------------------------------------------------------------+
0863
0864 The ``->cpu_no_qs`` flag indicates that the CPU has not yet passed
0865 through a quiescent state, while the ``->core_needs_qs`` flag indicates
0866 that the RCU core needs a quiescent state from the corresponding CPU.
0867 The ``->gpwrap`` field indicates that the corresponding CPU has remained
0868 idle for so long that the ``gp_seq`` counter is in danger of overflow,
0869 which will cause the CPU to disregard the values of its counters on its
0870 next exit from idle.
0871
0872 RCU Callback Handling
0873 '''''''''''''''''''''
0874
0875 In the absence of CPU-hotplug events, RCU callbacks are invoked by the
0876 same CPU that registered them. This is strictly a cache-locality
0877 optimization: callbacks can and do get invoked on CPUs other than the
0878 one that registered them. After all, if the CPU that registered a given
0879 callback has gone offline before the callback can be invoked, there
0880 really is no other choice.
0881
0882 This portion of the ``rcu_data`` structure is declared as follows:
0883
0884 ::
0885
0886 1 struct rcu_segcblist cblist;
0887 2 long qlen_last_fqs_check;
0888 3 unsigned long n_cbs_invoked;
0889 4 unsigned long n_nocbs_invoked;
0890 5 unsigned long n_cbs_orphaned;
0891 6 unsigned long n_cbs_adopted;
0892 7 unsigned long n_force_qs_snap;
0893 8 long blimit;
0894
0895 The ``->cblist`` structure is the segmented callback list described
0896 earlier. The CPU advances the callbacks in its ``rcu_data`` structure
0897 whenever it notices that another RCU grace period has completed. The CPU
0898 detects the completion of an RCU grace period by noticing that the value
0899 of its ``rcu_data`` structure's ``->gp_seq`` field differs from that of
0900 its leaf ``rcu_node`` structure. Recall that each ``rcu_node``
0901 structure's ``->gp_seq`` field is updated at the beginnings and ends of
0902 each grace period.
0903
0904 The ``->qlen_last_fqs_check`` and ``->n_force_qs_snap`` coordinate the
0905 forcing of quiescent states from ``call_rcu()`` and friends when
0906 callback lists grow excessively long.
0907
0908 The ``->n_cbs_invoked``, ``->n_cbs_orphaned``, and ``->n_cbs_adopted``
0909 fields count the number of callbacks invoked, sent to other CPUs when
0910 this CPU goes offline, and received from other CPUs when those other
0911 CPUs go offline. The ``->n_nocbs_invoked`` is used when the CPU's
0912 callbacks are offloaded to a kthread.
0913
0914 Finally, the ``->blimit`` counter is the maximum number of RCU callbacks
0915 that may be invoked at a given time.
0916
0917 Dyntick-Idle Handling
0918 '''''''''''''''''''''
0919
0920 This portion of the ``rcu_data`` structure is declared as follows:
0921
0922 ::
0923
0924 1 int dynticks_snap;
0925 2 unsigned long dynticks_fqs;
0926
0927 The ``->dynticks_snap`` field is used to take a snapshot of the
0928 corresponding CPU's dyntick-idle state when forcing quiescent states,
0929 and is therefore accessed from other CPUs. Finally, the
0930 ``->dynticks_fqs`` field is used to count the number of times this CPU
0931 is determined to be in dyntick-idle state, and is used for tracing and
0932 debugging purposes.
0933
0934 This portion of the rcu_data structure is declared as follows:
0935
0936 ::
0937
0938 1 long dynticks_nesting;
0939 2 long dynticks_nmi_nesting;
0940 3 atomic_t dynticks;
0941 4 bool rcu_need_heavy_qs;
0942 5 bool rcu_urgent_qs;
0943
0944 These fields in the rcu_data structure maintain the per-CPU dyntick-idle
0945 state for the corresponding CPU. The fields may be accessed only from
0946 the corresponding CPU (and from tracing) unless otherwise stated.
0947
0948 The ``->dynticks_nesting`` field counts the nesting depth of process
0949 execution, so that in normal circumstances this counter has value zero
0950 or one. NMIs, irqs, and tracers are counted by the
0951 ``->dynticks_nmi_nesting`` field. Because NMIs cannot be masked, changes
0952 to this variable have to be undertaken carefully using an algorithm
0953 provided by Andy Lutomirski. The initial transition from idle adds one,
0954 and nested transitions add two, so that a nesting level of five is
0955 represented by a ``->dynticks_nmi_nesting`` value of nine. This counter
0956 can therefore be thought of as counting the number of reasons why this
0957 CPU cannot be permitted to enter dyntick-idle mode, aside from
0958 process-level transitions.
0959
0960 However, it turns out that when running in non-idle kernel context, the
0961 Linux kernel is fully capable of entering interrupt handlers that never
0962 exit and perhaps also vice versa. Therefore, whenever the
0963 ``->dynticks_nesting`` field is incremented up from zero, the
0964 ``->dynticks_nmi_nesting`` field is set to a large positive number, and
0965 whenever the ``->dynticks_nesting`` field is decremented down to zero,
0966 the ``->dynticks_nmi_nesting`` field is set to zero. Assuming that
0967 the number of misnested interrupts is not sufficient to overflow the
0968 counter, this approach corrects the ``->dynticks_nmi_nesting`` field
0969 every time the corresponding CPU enters the idle loop from process
0970 context.
0971
0972 The ``->dynticks`` field counts the corresponding CPU's transitions to
0973 and from either dyntick-idle or user mode, so that this counter has an
0974 even value when the CPU is in dyntick-idle mode or user mode and an odd
0975 value otherwise. The transitions to/from user mode need to be counted
0976 for user mode adaptive-ticks support (see Documentation/timers/no_hz.rst).
0977
0978 The ``->rcu_need_heavy_qs`` field is used to record the fact that the
0979 RCU core code would really like to see a quiescent state from the
0980 corresponding CPU, so much so that it is willing to call for
0981 heavy-weight dyntick-counter operations. This flag is checked by RCU's
0982 context-switch and ``cond_resched()`` code, which provide a momentary
0983 idle sojourn in response.
0984
0985 Finally, the ``->rcu_urgent_qs`` field is used to record the fact that
0986 the RCU core code would really like to see a quiescent state from the
0987 corresponding CPU, with the various other fields indicating just how
0988 badly RCU wants this quiescent state. This flag is checked by RCU's
0989 context-switch path (``rcu_note_context_switch``) and the cond_resched
0990 code.
0991
0992 +-----------------------------------------------------------------------+
0993 | **Quick Quiz**: |
0994 +-----------------------------------------------------------------------+
0995 | Why not simply combine the ``->dynticks_nesting`` and |
0996 | ``->dynticks_nmi_nesting`` counters into a single counter that just |
0997 | counts the number of reasons that the corresponding CPU is non-idle? |
0998 +-----------------------------------------------------------------------+
0999 | **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
1006 Additional fields are present for some special-purpose builds, and are
1007 discussed separately.
1008
1009 The ``rcu_head`` Structure
1010 ~~~~~~~~~~~~~~~~~~~~~~~~~~
1011
1012 Each ``rcu_head`` structure represents an RCU callback. These structures
1013 are normally embedded within RCU-protected data structures whose
1014 algorithms use asynchronous grace periods. In contrast, when using
1015 algorithms that block waiting for RCU grace periods, RCU users need not
1016 provide ``rcu_head`` structures.
1017
1018 The ``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
1025 The ``->next`` field is used to link the ``rcu_head`` structures
1026 together in the lists within the ``rcu_data`` structures. The ``->func``
1027 field is a pointer to the function to be called when the callback is
1028 ready to be invoked, and this function is passed a pointer to the
1029 ``rcu_head`` structure. However, ``kfree_rcu()`` uses the ``->func``
1030 field to record the offset of the ``rcu_head`` structure within the
1031 enclosing RCU-protected data structure.
1032
1033 Both of these fields are used internally by RCU. From the viewpoint of
1034 RCU 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
1052 RCU-Specific Fields in the ``task_struct`` Structure
1053 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1054
1055 The ``CONFIG_PREEMPT_RCU`` implementation uses some additional fields in
1056 the ``task_struct`` structure:
1057
1058 ::
1059
1060 1 #ifdef CONFIG_PREEMPT_RCU
1061 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_RCU
1067 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
1073 The ``->rcu_read_lock_nesting`` field records the nesting level for RCU
1074 read-side critical sections, and the ``->rcu_read_unlock_special`` field
1075 is a bitmask that records special conditions that require
1076 ``rcu_read_unlock()`` to do additional work. The ``->rcu_node_entry``
1077 field is used to form lists of tasks that have blocked within
1078 preemptible-RCU read-side critical sections and the
1079 ``->rcu_blocked_node`` field references the ``rcu_node`` structure whose
1080 list this task is a member of, or ``NULL`` if it is not blocked within a
1081 preemptible-RCU read-side critical section.
1082
1083 The ``->rcu_tasks_nvcsw`` field tracks the number of voluntary context
1084 switches that this task had undergone at the beginning of the current
1085 tasks-RCU grace period, ``->rcu_tasks_holdout`` is set if the current
1086 tasks-RCU grace period is waiting on this task,
1087 ``->rcu_tasks_holdout_list`` is a list element enqueuing this task on
1088 the holdout list, and ``->rcu_tasks_idle_cpu`` tracks which CPU this
1089 idle task is running, but only if the task is currently running, that
1090 is, if the CPU is currently idle.
1091
1092 Accessor Functions
1093 ~~~~~~~~~~~~~~~~~~
1094
1095 The following listing shows the ``rcu_get_root()``,
1096 ``rcu_for_each_node_breadth_first`` and ``rcu_for_each_leaf_node()``
1097 function 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 5
1106 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 9
1110 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
1114 The ``rcu_get_root()`` simply returns a pointer to the first element of
1115 the specified ``rcu_state`` structure's ``->node[]`` array, which is the
1116 root ``rcu_node`` structure.
1117
1118 As noted earlier, the ``rcu_for_each_node_breadth_first()`` macro takes
1119 advantage of the layout of the ``rcu_node`` structures in the
1120 ``rcu_state`` structure's ``->node[]`` array, performing a breadth-first
1121 traversal by simply traversing the array in order. Similarly, the
1122 ``rcu_for_each_leaf_node()`` macro traverses only the last part of the
1123 array, 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
1137 Summary
1138 ~~~~~~~
1139
1140 So the state of RCU is represented by an ``rcu_state`` structure, which
1141 contains a combining tree of ``rcu_node`` and ``rcu_data`` structures.
1142 Finally, in ``CONFIG_NO_HZ_IDLE`` kernels, each CPU's dyntick-idle state
1143 is tracked by dynticks-related fields in the ``rcu_data`` structure. If
1144 you made it this far, you are well prepared to read the code
1145 walkthroughs in the other articles in this series.
1146
1147 Acknowledgments
1148 ~~~~~~~~~~~~~~~
1149
1150 I owe thanks to Cyrill Gorcunov, Mathieu Desnoyers, Dhaval Giani, Paul
1151 Turner, Abhishek Srivastava, Matt Kowalczyk, and Serge Hallyn for
1152 helping me get this document into a more human-readable state.
1153
1154 Legal Statement
1155 ~~~~~~~~~~~~~~~
1156
1157 This work represents the view of the author and does not necessarily
1158 represent the view of IBM.
1159
1160 Linux is a registered trademark of Linus Torvalds.
1161
1162 Other company, product, and service names may be trademarks or service
1163 marks of others.