summaryrefslogtreecommitdiffstats
path: root/Documentation/RCU/Design/Expedited-Grace-Periods/Expedited-Grace-Periods.html
blob: 7394f034be65dec6e3e12f52ffb1371121155d1e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
        <html>
        <head><title>A Tour Through TREE_RCU's Expedited Grace Periods</title>
        <meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">

<h2>Introduction</h2>

This document describes RCU's expedited grace periods.
Unlike RCU's normal grace periods, which accept long latencies to attain
high efficiency and minimal disturbance, expedited grace periods accept
lower efficiency and significant disturbance to attain shorter latencies.

<p>
There are three flavors of RCU (RCU-bh, RCU-preempt, and RCU-sched),
but only two flavors of expedited grace periods because the RCU-bh
expedited grace period maps onto the RCU-sched expedited grace period.
Each of the remaining two implementations is covered in its own section.

<ol>
<li>	<a href="#Expedited Grace Period Design">
	Expedited Grace Period Design</a>
<li>	<a href="#RCU-preempt Expedited Grace Periods">
	RCU-preempt Expedited Grace Periods</a>
<li>	<a href="#RCU-sched Expedited Grace Periods">
	RCU-sched Expedited Grace Periods</a>
<li>	<a href="#Expedited Grace Period and CPU Hotplug">
	Expedited Grace Period and CPU Hotplug</a>
<li>	<a href="#Expedited Grace Period Refinements">
	Expedited Grace Period Refinements</a>
</ol>

<h2><a name="Expedited Grace Period Design">
Expedited Grace Period Design</a></h2>

<p>
The expedited RCU grace periods cannot be accused of being subtle,
given that they for all intents and purposes hammer every CPU that
has not yet provided a quiescent state for the current expedited
grace period.
The one saving grace is that the hammer has grown a bit smaller
over time:  The old call to <tt>try_stop_cpus()</tt> has been
replaced with a set of calls to <tt>smp_call_function_single()</tt>,
each of which results in an IPI to the target CPU.
The corresponding handler function checks the CPU's state, motivating
a faster quiescent state where possible, and triggering a report
of that quiescent state.
As always for RCU, once everything has spent some time in a quiescent
state, the expedited grace period has completed.

<p>
The details of the <tt>smp_call_function_single()</tt> handler's
operation depend on the RCU flavor, as described in the following
sections.

<h2><a name="RCU-preempt Expedited Grace Periods">
RCU-preempt Expedited Grace Periods</a></h2>

<p>
The overall flow of the handling of a given CPU by an RCU-preempt
expedited grace period is shown in the following diagram:

<p><img src="ExpRCUFlow.svg" alt="ExpRCUFlow.svg" width="55%">

<p>
The solid arrows denote direct action, for example, a function call.
The dotted arrows denote indirect action, for example, an IPI
or a state that is reached after some time.

<p>
If a given CPU is offline or idle, <tt>synchronize_rcu_expedited()</tt>
will ignore it because idle and offline CPUs are already residing
in quiescent states.
Otherwise, the expedited grace period will use
<tt>smp_call_function_single()</tt> to send the CPU an IPI, which
is handled by <tt>sync_rcu_exp_handler()</tt>.

<p>
However, because this is preemptible RCU, <tt>sync_rcu_exp_handler()</tt>
can check to see if the CPU is currently running in an RCU read-side
critical section.
If not, the handler can immediately report a quiescent state.
Otherwise, it sets flags so that the outermost <tt>rcu_read_unlock()</tt>
invocation will provide the needed quiescent-state report.
This flag-setting avoids the previous forced preemption of all
CPUs that might have RCU read-side critical sections.
In addition, this flag-setting is done so as to avoid increasing
the overhead of the common-case fastpath through the scheduler.

<p>
Again because this is preemptible RCU, an RCU read-side critical section
can be preempted.
When that happens, RCU will enqueue the task, which will the continue to
block the current expedited grace period until it resumes and finds its
outermost <tt>rcu_read_unlock()</tt>.
The CPU will report a quiescent state just after enqueuing the task because
the CPU is no longer blocking the grace period.
It is instead the preempted task doing the blocking.
The list of blocked tasks is managed by <tt>rcu_preempt_ctxt_queue()</tt>,
which is called from <tt>rcu_preempt_note_context_switch()</tt>, which
in turn is called from <tt>rcu_note_context_switch()</tt>, which in
turn is called from the scheduler.

<table>
<tr><th>&nbsp;</th></tr>
<tr><th align="left">Quick Quiz:</th></tr>
<tr><td>
	Why not just have the expedited grace period check the
	state of all the CPUs?
	After all, that would avoid all those real-time-unfriendly IPIs.
</td></tr>
<tr><th align="left">Answer:</th></tr>
<tr><td bgcolor="#ffffff"><font color="ffffff">
	Because we want the RCU read-side critical sections to run fast,
	which means no memory barriers.
	Therefore, it is not possible to safely check the state from some
	other CPU.
	And even if it was possible to safely check the state, it would
	still be necessary to IPI the CPU to safely interact with the
	upcoming <tt>rcu_read_unlock()</tt> invocation, which means that
	the remote state testing would not help the worst-case
	latency that real-time applications care about.

	<p><font color="ffffff">One way to prevent your real-time
	application from getting hit with these IPIs is to
	build your kernel with <tt>CONFIG_NO_HZ_FULL=y</tt>.
	RCU would then perceive the CPU running your application
	as being idle, and it would be able to safely detect that
	state without needing to IPI the CPU.
</font></td></tr>
<tr><td>&nbsp;</td></tr>
</table>

<p>
Please note that this is just the overall flow:
Additional complications can arise due to races with CPUs going idle
or offline, among other things.

<h2><a name="RCU-sched Expedited Grace Periods">
RCU-sched Expedited Grace Periods</a></h2>

<p>
The overall flow of the handling of a given CPU by an RCU-sched
expedited grace period is shown in the following diagram:

<p><img src="ExpSchedFlow.svg" alt="ExpSchedFlow.svg" width="55%">

<p>
As with RCU-preempt's <tt>synchronize_rcu_expedited()</tt>,
<tt>synchronize_sched_expedited()</tt> ignores offline and
idle CPUs, again because they are in remotely detectable
quiescent states.
However, the <tt>synchronize_rcu_expedited()</tt> handler
is <tt>sync_sched_exp_handler()</tt>, and because the
<tt>rcu_read_lock_sched()</tt> and <tt>rcu_read_unlock_sched()</tt>
leave no trace of their invocation, in general it is not possible to tell
whether or not the current CPU is in an RCU read-side critical section.
The best that <tt>sync_sched_exp_handler()</tt> can do is to check
for idle, on the off-chance that the CPU went idle while the IPI
was in flight.
If the CPU is idle, then tt>sync_sched_exp_handler()</tt> reports
the quiescent state.

<p>
Otherwise, the handler invokes <tt>resched_cpu()</tt>, which forces
a future context switch.
At the time of the context switch, the CPU reports the quiescent state.
Should the CPU go offline first, it will report the quiescent state
at that time.

<h2><a name="Expedited Grace Period and CPU Hotplug">
Expedited Grace Period and CPU Hotplug</a></h2>

<p>
The expedited nature of expedited grace periods require a much tighter
interaction with CPU hotplug operations than is required for normal
grace periods.
In addition, attempting to IPI offline CPUs will result in splats, but
failing to IPI online CPUs can result in too-short grace periods.
Neither option is acceptable in production kernels.

<p>
The interaction between expedited grace periods and CPU hotplug operations
is carried out at several levels:

<ol>
<li>	The number of CPUs that have ever been online is tracked
	by the <tt>rcu_state</tt> structure's <tt>-&gt;ncpus</tt>
	field.
	The <tt>rcu_state</tt> structure's <tt>-&gt;ncpus_snap</tt>
	field tracks the number of CPUs that have ever been online
	at the beginning of an RCU expedited grace period.
	Note that this number never decreases, at least in the absence
	of a time machine.
<li>	The identities of the CPUs that have ever been online is
	tracked by the <tt>rcu_node</tt> structure's
	<tt>-&gt;expmaskinitnext</tt> field.
	The <tt>rcu_node</tt> structure's <tt>-&gt;expmaskinit</tt>
	field tracks the identities of the CPUs that were online
	at least once at the beginning of the most recent RCU
	expedited grace period.
	The <tt>rcu_state</tt> structure's <tt>-&gt;ncpus</tt> and
	<tt>-&gt;ncpus_snap</tt> fields are used to detect when
	new CPUs have come online for the first time, that is,
	when the <tt>rcu_node</tt> structure's <tt>-&gt;expmaskinitnext</tt>
	field has changed since the beginning of the last RCU
	expedited grace period, which triggers an update of each
	<tt>rcu_node</tt> structure's <tt>-&gt;expmaskinit</tt>
	field from its <tt>-&gt;expmaskinitnext</tt> field.
<li>	Each <tt>rcu_node</tt> structure's <tt>-&gt;expmaskinit</tt>
	field is used to initialize that structure's
	<tt>-&gt;expmask</tt> at the beginning of each RCU
	expedited grace period.
	This means that only those CPUs that have been online at least
	once will be considered for a given grace period.
<li>	Any CPU that goes offline will clear its bit in its leaf
	<tt>rcu_node</tt> structure's <tt>-&gt;qsmaskinitnext</tt>
	field, so any CPU with that bit clear can safely be ignored.
	However, it is possible for a CPU coming online or going offline
	to have this bit set for some time while <tt>cpu_online</tt>
	returns <tt>false</tt>.
<li>	For each non-idle CPU that RCU believes is currently online, the grace
	period invokes <tt>smp_call_function_single()</tt>.
	If this succeeds, the CPU was fully online.
	Failure indicates that the CPU is in the process of coming online
	or going offline, in which case it is necessary to wait for a
	short time period and try again.
	The purpose of this wait (or series of waits, as the case may be)
	is to permit a concurrent CPU-hotplug operation to complete.
<li>	In the case of RCU-sched, one of the last acts of an outgoing CPU
	is to invoke <tt>rcu_report_dead()</tt>, which
	reports a quiescent state for that CPU.
	However, this is likely paranoia-induced redundancy. <!-- @@@ -->
</ol>

<table>
<tr><th>&nbsp;</th></tr>
<tr><th align="left">Quick Quiz:</th></tr>
<tr><td>
	Why all the dancing around with multiple counters and masks
	tracking CPUs that were once online?
	Why not just have a single set of masks tracking the currently
	online CPUs and be done with it?
</td></tr>
<tr><th align="left">Answer:</th></tr>
<tr><td bgcolor="#ffffff"><font color="ffffff">
	Maintaining single set of masks tracking the online CPUs <i>sounds</i>
	easier, at least until you try working out all the race conditions
	between grace-period initialization and CPU-hotplug operations.
	For example, suppose initialization is progressing down the
	tree while a CPU-offline operation is progressing up the tree.
	This situation can result in bits set at the top of the tree
	that have no counterparts at the bottom of the tree.
	Those bits will never be cleared, which will result in
	grace-period hangs.
	In short, that way lies madness, to say nothing of a great many
	bugs, hangs, and deadlocks.

	<p><font color="ffffff">
	In contrast, the current multi-mask multi-counter scheme ensures
	that grace-period initialization will always see consistent masks
	up and down the tree, which brings significant simplifications
	over the single-mask method.

	<p><font color="ffffff">
	This is an instance of
	<a href="http://www.cs.columbia.edu/~library/TR-repository/reports/reports-1992/cucs-039-92.ps.gz"><font color="ffffff">
	deferring work in order to avoid synchronization</a>.
	Lazily recording CPU-hotplug events at the beginning of the next
	grace period greatly simplifies maintenance of the CPU-tracking
	bitmasks in the <tt>rcu_node</tt> tree.
</font></td></tr>
<tr><td>&nbsp;</td></tr>
</table>

<h2><a name="Expedited Grace Period Refinements">
Expedited Grace Period Refinements</a></h2>

<ol>
<li>	<a href="#Idle-CPU Checks">Idle-CPU checks</a>.
<li>	<a href="#Batching via Sequence Counter">
	Batching via sequence counter</a>.
<li>	<a href="#Funnel Locking and Wait/Wakeup">
	Funnel locking and wait/wakeup</a>.
<li>	<