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