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