1Using RCU's CPU Stall Detector
2
3This document first discusses what sorts of issues RCU's CPU stall
4detector can locate, and then discusses kernel parameters and Kconfig
5options that can be used to fine-tune the detector's operation.  Finally,
6this document explains the stall detector's "splat" format.
7
8
9What Causes RCU CPU Stall Warnings?
10
11So your kernel printed an RCU CPU stall warning.  The next question is
12"What caused it?"  The following problems can result in RCU CPU stall
13warnings:
14
15o	A CPU looping in an RCU read-side critical section.
16
17o	A CPU looping with interrupts disabled.
18
19o	A CPU looping with preemption disabled.  This condition can
20	result in RCU-sched stalls and, if ksoftirqd is in use, RCU-bh
21	stalls.
22
23o	A CPU looping with bottom halves disabled.  This condition can
24	result in RCU-sched and RCU-bh stalls.
25
26o	For !CONFIG_PREEMPT kernels, a CPU looping anywhere in the kernel
27	without invoking schedule().  If the looping in the kernel is
28	really expected and desirable behavior, you might need to add
29	some calls to cond_resched().
30
31o	Booting Linux using a console connection that is too slow to
32	keep up with the boot-time console-message rate.  For example,
33	a 115Kbaud serial console can be -way- too slow to keep up
34	with boot-time message rates, and will frequently result in
35	RCU CPU stall warning messages.  Especially if you have added
36	debug printk()s.
37
38o	Anything that prevents RCU's grace-period kthreads from running.
39	This can result in the "All QSes seen" console-log message.
40	This message will include information on when the kthread last
41	ran and how often it should be expected to run.  It can also
42	result in the "rcu_.*kthread starved for" console-log message,
43	which will include additional debugging information.
44
45o	A CPU-bound real-time task in a CONFIG_PREEMPT kernel, which might
46	happen to preempt a low-priority task in the middle of an RCU
47	read-side critical section.   This is especially damaging if
48	that low-priority task is not permitted to run on any other CPU,
49	in which case the next RCU grace period can never complete, which
50	will eventually cause the system to run out of memory and hang.
51	While the system is in the process of running itself out of
52	memory, you might see stall-warning messages.
53
54o	A CPU-bound real-time task in a CONFIG_PREEMPT_RT kernel that
55	is running at a higher priority than the RCU softirq threads.
56	This will prevent RCU callbacks from ever being invoked,
57	and in a CONFIG_PREEMPT_RCU kernel will further prevent
58	RCU grace periods from ever completing.  Either way, the
59	system will eventually run out of memory and hang.  In the
60	CONFIG_PREEMPT_RCU case, you might see stall-warning
61	messages.
62
63o	A periodic interrupt whose handler takes longer than the time
64	interval between successive pairs of interrupts.  This can
65	prevent RCU's kthreads and softirq handlers from running.
66	Note that certain high-overhead debugging options, for example
67	the function_graph tracer, can result in interrupt handler taking
68	considerably longer than normal, which can in turn result in
69	RCU CPU stall warnings.
70
71o	Testing a workload on a fast system, tuning the stall-warning
72	timeout down to just barely avoid RCU CPU stall warnings, and then
73	running the same workload with the same stall-warning timeout on a
74	slow system.  Note that thermal throttling and on-demand governors
75	can cause a single system to be sometimes fast and sometimes slow!
76
77o	A hardware or software issue shuts off the scheduler-clock
78	interrupt on a CPU that is not in dyntick-idle mode.  This
79	problem really has happened, and seems to be most likely to
80	result in RCU CPU stall warnings for CONFIG_NO_HZ_COMMON=n kernels.
81
82o	A bug in the RCU implementation.
83
84o	A hardware failure.  This is quite unlikely, but has occurred
85	at least once in real life.  A CPU failed in a running system,
86	becoming unresponsive, but not causing an immediate crash.
87	This resulted in a series of RCU CPU stall warnings, eventually
88	leading the realization that the CPU had failed.
89
90The RCU, RCU-sched, RCU-bh, and RCU-tasks implementations have CPU stall
91warning.  Note that SRCU does -not- have CPU stall warnings.  Please note
92that RCU only detects CPU stalls when there is a grace period in progress.
93No grace period, no CPU stall warnings.
94
95To diagnose the cause of the stall, inspect the stack traces.
96The offending function will usually be near the top of the stack.
97If you have a series of stall warnings from a single extended stall,
98comparing the stack traces can often help determine where the stall
99is occurring, which will usually be in the function nearest the top of
100that portion of the stack which remains the same from trace to trace.
101If you can reliably trigger the stall, ftrace can be quite helpful.
102
103RCU bugs can often be debugged with the help of CONFIG_RCU_TRACE
104and with RCU's event tracing.  For information on RCU's event tracing,
105see include/trace/events/rcu.h.
106
107
108Fine-Tuning the RCU CPU Stall Detector
109
110The rcuupdate.rcu_cpu_stall_suppress module parameter disables RCU's
111CPU stall detector, which detects conditions that unduly delay RCU grace
112periods.  This module parameter enables CPU stall detection by default,
113but may be overridden via boot-time parameter or at runtime via sysfs.
114The stall detector's idea of what constitutes "unduly delayed" is
115controlled by a set of kernel configuration variables and cpp macros:
116
117CONFIG_RCU_CPU_STALL_TIMEOUT
118
119	This kernel configuration parameter defines the period of time
120	that RCU will wait from the beginning of a grace period until it
121	issues an RCU CPU stall warning.  This time period is normally
122	21 seconds.
123
124	This configuration parameter may be changed at runtime via the
125	/sys/module/rcupdate/parameters/rcu_cpu_stall_timeout, however
126	this parameter is checked only at the beginning of a cycle.
127	So if you are 10 seconds into a 40-second stall, setting this
128	sysfs parameter to (say) five will shorten the timeout for the
129	-next- stall, or the following warning for the current stall
130	(assuming the stall lasts long enough).  It will not affect the
131	timing of the next warning for the current stall.
132
133	Stall-warning messages may be enabled and disabled completely via
134	/sys/module/rcupdate/parameters/rcu_cpu_stall_suppress.
135
136RCU_STALL_DELAY_DELTA
137
138	Although the lockdep facility is extremely useful, it does add
139	some overhead.  Therefore, under CONFIG_PROVE_RCU, the
140	RCU_STALL_DELAY_DELTA macro allows five extra seconds before
141	giving an RCU CPU stall warning message.  (This is a cpp
142	macro, not a kernel configuration parameter.)
143
144RCU_STALL_RAT_DELAY
145
146	The CPU stall detector tries to make the offending CPU print its
147	own warnings, as this often gives better-quality stack traces.
148	However, if the offending CPU does not detect its own stall in
149	the number of jiffies specified by RCU_STALL_RAT_DELAY, then
150	some other CPU will complain.  This delay is normally set to
151	two jiffies.  (This is a cpp macro, not a kernel configuration
152	parameter.)
153
154rcupdate.rcu_task_stall_timeout
155
156	This boot/sysfs parameter controls the RCU-tasks stall warning
157	interval.  A value of zero or less suppresses RCU-tasks stall
158	warnings.  A positive value sets the stall-warning interval
159	in jiffies.  An RCU-tasks stall warning starts with the line:
160
161		INFO: rcu_tasks detected stalls on tasks:
162
163	And continues with the output of sched_show_task() for each
164	task stalling the current RCU-tasks grace period.
165
166
167Interpreting RCU's CPU Stall-Detector "Splats"
168
169For non-RCU-tasks flavors of RCU, when a CPU detects that it is stalling,
170it will print a message similar to the following:
171
172	INFO: rcu_sched detected stalls on CPUs/tasks:
173	2-...: (3 GPs behind) idle=06c/0/0 softirq=1453/1455 fqs=0
174	16-...: (0 ticks this GP) idle=81c/0/0 softirq=764/764 fqs=0
175	(detected by 32, t=2603 jiffies, g=7075, q=625)
176
177This message indicates that CPU 32 detected that CPUs 2 and 16 were both
178causing stalls, and that the stall was affecting RCU-sched.  This message
179will normally be followed by stack dumps for each CPU.  Please note that
180PREEMPT_RCU builds can be stalled by tasks as well as by CPUs, and that
181the tasks will be indicated by PID, for example, "P3421".  It is even
182possible for a rcu_preempt_state stall to be caused by both CPUs -and-
183tasks, in which case the offending CPUs and tasks will all be called
184out in the list.
185
186CPU 2's "(3 GPs behind)" indicates that this CPU has not interacted with
187the RCU core for the past three grace periods.  In contrast, CPU 16's "(0
188ticks this GP)" indicates that this CPU has not taken any scheduling-clock
189interrupts during the current stalled grace period.
190
191The "idle=" portion of the message prints the dyntick-idle state.
192The hex number before the first "/" is the low-order 12 bits of the
193dynticks counter, which will have an even-numbered value if the CPU
194is in dyntick-idle mode and an odd-numbered value otherwise.  The hex
195number between the two "/"s is the value of the nesting, which will be
196a small non-negative number if in the idle loop (as shown above) and a
197very large positive number otherwise.
198
199The "softirq=" portion of the message tracks the number of RCU softirq
200handlers that the stalled CPU has executed.  The number before the "/"
201is the number that had executed since boot at the time that this CPU
202last noted the beginning of a grace period, which might be the current
203(stalled) grace period, or it might be some earlier grace period (for
204example, if the CPU might have been in dyntick-idle mode for an extended
205time period.  The number after the "/" is the number that have executed
206since boot until the current time.  If this latter number stays constant
207across repeated stall-warning messages, it is possible that RCU's softirq
208handlers are no longer able to execute on this CPU.  This can happen if
209the stalled CPU is spinning with interrupts are disabled, or, in -rt
210kernels, if a high-priority process is starving RCU's softirq handler.
211
212The "fps=" shows the number of force-quiescent-state idle/offline
213detection passes that the grace-period kthread has made across this
214CPU since the last time that this CPU noted the beginning of a grace
215period.
216
217The "detected by" line indicates which CPU detected the stall (in this
218case, CPU 32), how many jiffies have elapsed since the start of the grace
219period (in this case 2603), the grace-period sequence number (7075), and
220an estimate of the total number of RCU callbacks queued across all CPUs
221(625 in this case).
222
223In kernels with CONFIG_RCU_FAST_NO_HZ, more information is printed
224for each CPU:
225
226	0: (64628 ticks this GP) idle=dd5/3fffffffffffffff/0 softirq=82/543 last_accelerate: a345/d342 nonlazy_posted: 25 .D
227
228The "last_accelerate:" prints the low-order 16 bits (in hex) of the
229jiffies counter when this CPU last invoked rcu_try_advance_all_cbs()
230from rcu_needs_cpu() or last invoked rcu_accelerate_cbs() from
231rcu_prepare_for_idle().  The "nonlazy_posted:" prints the number
232of non-lazy callbacks posted since the last call to rcu_needs_cpu().
233Finally, an "L" indicates that there are currently no non-lazy callbacks
234("." is printed otherwise, as shown above) and "D" indicates that
235dyntick-idle processing is enabled ("." is printed otherwise, for example,
236if disabled via the "nohz=" kernel boot parameter).
237
238If the grace period ends just as the stall warning starts printing,
239there will be a spurious stall-warning message, which will include
240the following:
241
242	INFO: Stall ended before state dump start
243
244This is rare, but does happen from time to time in real life.  It is also
245possible for a zero-jiffy stall to be flagged in this case, depending
246on how the stall warning and the grace-period initialization happen to
247interact.  Please note that it is not possible to entirely eliminate this
248sort of false positive without resorting to things like stop_machine(),
249which is overkill for this sort of problem.
250
251If all CPUs and tasks have passed through quiescent states, but the
252grace period has nevertheless failed to end, the stall-warning splat
253will include something like the following:
254
255	All QSes seen, last rcu_preempt kthread activity 23807 (4297905177-4297881370), jiffies_till_next_fqs=3, root ->qsmask 0x0
256
257The "23807" indicates that it has been more than 23 thousand jiffies
258since the grace-period kthread ran.  The "jiffies_till_next_fqs"
259indicates how frequently that kthread should run, giving the number
260of jiffies between force-quiescent-state scans, in this case three,
261which is way less than 23807.  Finally, the root rcu_node structure's
262->qsmask field is printed, which will normally be zero.
263
264If the relevant grace-period kthread has been unable to run prior to
265the stall warning, as was the case in the "All QSes seen" line above,
266the following additional line is printed:
267
268	kthread starved for 23807 jiffies! g7075 f0x0 RCU_GP_WAIT_FQS(3) ->state=0x1 ->cpu=5
269
270Starving the grace-period kthreads of CPU time can of course result
271in RCU CPU stall warnings even when all CPUs and tasks have passed
272through the required quiescent states.  The "g" number shows the current
273grace-period sequence number, the "f" precedes the ->gp_flags command
274to the grace-period kthread, the "RCU_GP_WAIT_FQS" indicates that the
275kthread is waiting for a short timeout, the "state" precedes value of the
276task_struct ->state field, and the "cpu" indicates that the grace-period
277kthread last ran on CPU 5.
278
279
280Multiple Warnings From One Stall
281
282If a stall lasts long enough, multiple stall-warning messages will be
283printed for it.  The second and subsequent messages are printed at
284longer intervals, so that the time between (say) the first and second
285message will be about three times the interval between the beginning
286of the stall and the first message.
287
288
289Stall Warnings for Expedited Grace Periods
290
291If an expedited grace period detects a stall, it will place a message
292like the following in dmesg:
293
294	INFO: rcu_sched detected expedited stalls on CPUs/tasks: { 7-... } 21119 jiffies s: 73 root: 0x2/.
295
296This indicates that CPU 7 has failed to respond to a reschedule IPI.
297The three periods (".") following the CPU number indicate that the CPU
298is online (otherwise the first period would instead have been "O"),
299that the CPU was online at the beginning of the expedited grace period
300(otherwise the second period would have instead been "o"), and that
301the CPU has been online at least once since boot (otherwise, the third
302period would instead have been "N").  The number before the "jiffies"
303indicates that the expedited grace period has been going on for 21,119
304jiffies.  The number following the "s:" indicates that the expedited
305grace-period sequence counter is 73.  The fact that this last value is
306odd indicates that an expedited grace period is in flight.  The number
307following "root:" is a bitmask that indicates which children of the root
308rcu_node structure correspond to CPUs and/or tasks that are blocking the
309current expedited grace period.  If the tree had more than one level,
310additional hex numbers would be printed for the states of the other
311rcu_node structures in the tree.
312
313As with normal grace periods, PREEMPT_RCU builds can be stalled by
314tasks as well as by CPUs, and that the tasks will be indicated by PID,
315for example, "P3421".
316
317It is entirely possible to see stall warnings from normal and from
318expedited grace periods at about the same time during the same run.
319