1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * RCU CPU stall warnings for normal RCU grace periods
4 *
5 * Copyright IBM Corporation, 2019
6 *
7 * Author: Paul E. McKenney <paulmck@linux.ibm.com>
8 */
9
10 #include <linux/kvm_para.h>
11
12 //////////////////////////////////////////////////////////////////////////////
13 //
14 // Controlling CPU stall warnings, including delay calculation.
15
16 /* panic() on RCU Stall sysctl. */
17 int sysctl_panic_on_rcu_stall __read_mostly = CONFIG_BOOTPARAM_RCU_STALL_PANIC_VALUE;
18 ATOMIC_NOTIFIER_HEAD(rcu_stall_notifier_list);
19
20 #ifdef CONFIG_PROVE_RCU
21 #define RCU_STALL_DELAY_DELTA (5 * HZ)
22 #else
23 #define RCU_STALL_DELAY_DELTA 0
24 #endif
25 #define RCU_STALL_MIGHT_DIV 8
26 #define RCU_STALL_MIGHT_MIN (2 * HZ)
27
28 /* Limit-check stall timeouts specified at boottime and runtime. */
rcu_jiffies_till_stall_check(void)29 int rcu_jiffies_till_stall_check(void)
30 {
31 int till_stall_check = READ_ONCE(rcu_cpu_stall_timeout);
32
33 /*
34 * Limit check must be consistent with the Kconfig limits
35 * for CONFIG_RCU_CPU_STALL_TIMEOUT.
36 */
37 if (till_stall_check < 3) {
38 WRITE_ONCE(rcu_cpu_stall_timeout, 3);
39 till_stall_check = 3;
40 } else if (till_stall_check > 300) {
41 WRITE_ONCE(rcu_cpu_stall_timeout, 300);
42 till_stall_check = 300;
43 }
44 return till_stall_check * HZ + RCU_STALL_DELAY_DELTA;
45 }
46 EXPORT_SYMBOL_GPL(rcu_jiffies_till_stall_check);
47
48 /**
49 * rcu_gp_might_be_stalled - Is it likely that the grace period is stalled?
50 *
51 * Returns @true if the current grace period is sufficiently old that
52 * it is reasonable to assume that it might be stalled. This can be
53 * useful when deciding whether to allocate memory to enable RCU-mediated
54 * freeing on the one hand or just invoking synchronize_rcu() on the other.
55 * The latter is preferable when the grace period is stalled.
56 *
57 * Note that sampling of the .gp_start and .gp_seq fields must be done
58 * carefully to avoid false positives at the beginnings and ends of
59 * grace periods.
60 */
rcu_gp_might_be_stalled(void)61 bool rcu_gp_might_be_stalled(void)
62 {
63 unsigned long d = rcu_jiffies_till_stall_check() / RCU_STALL_MIGHT_DIV;
64 unsigned long j = jiffies;
65
66 if (d < RCU_STALL_MIGHT_MIN)
67 d = RCU_STALL_MIGHT_MIN;
68 smp_mb(); // jiffies before .gp_seq to avoid false positives.
69 if (!rcu_gp_in_progress())
70 return false;
71 // Long delays at this point avoids false positive, but a delay
72 // of ULONG_MAX/4 jiffies voids your no-false-positive warranty.
73 smp_mb(); // .gp_seq before second .gp_start
74 // And ditto here.
75 return !time_before(j, READ_ONCE(rcu_state.gp_start) + d);
76 }
77
78 /* Don't do RCU CPU stall warnings during long sysrq printouts. */
rcu_sysrq_start(void)79 void rcu_sysrq_start(void)
80 {
81 if (!rcu_cpu_stall_suppress)
82 rcu_cpu_stall_suppress = 2;
83 }
84
rcu_sysrq_end(void)85 void rcu_sysrq_end(void)
86 {
87 if (rcu_cpu_stall_suppress == 2)
88 rcu_cpu_stall_suppress = 0;
89 }
90
91 /* Don't print RCU CPU stall warnings during a kernel panic. */
rcu_panic(struct notifier_block * this,unsigned long ev,void * ptr)92 static int rcu_panic(struct notifier_block *this, unsigned long ev, void *ptr)
93 {
94 rcu_cpu_stall_suppress = 1;
95 return NOTIFY_DONE;
96 }
97
98 static struct notifier_block rcu_panic_block = {
99 .notifier_call = rcu_panic,
100 };
101
check_cpu_stall_init(void)102 static int __init check_cpu_stall_init(void)
103 {
104 atomic_notifier_chain_register(&panic_notifier_list, &rcu_panic_block);
105 return 0;
106 }
107 early_initcall(check_cpu_stall_init);
108
109 /* If so specified via sysctl, panic, yielding cleaner stall-warning output. */
panic_on_rcu_stall(void)110 static void panic_on_rcu_stall(void)
111 {
112 if (sysctl_panic_on_rcu_stall)
113 panic("RCU Stall\n");
114 }
115
116 /**
117 * rcu_cpu_stall_reset - prevent further stall warnings in current grace period
118 *
119 * Set the stall-warning timeout way off into the future, thus preventing
120 * any RCU CPU stall-warning messages from appearing in the current set of
121 * RCU grace periods.
122 *
123 * The caller must disable hard irqs.
124 */
rcu_cpu_stall_reset(void)125 void rcu_cpu_stall_reset(void)
126 {
127 WRITE_ONCE(rcu_state.jiffies_stall, jiffies + ULONG_MAX / 2);
128 }
129
130 //////////////////////////////////////////////////////////////////////////////
131 //
132 // Interaction with RCU grace periods
133
134 /* Start of new grace period, so record stall time (and forcing times). */
record_gp_stall_check_time(void)135 static void record_gp_stall_check_time(void)
136 {
137 unsigned long j = jiffies;
138 unsigned long j1;
139
140 WRITE_ONCE(rcu_state.gp_start, j);
141 j1 = rcu_jiffies_till_stall_check();
142 smp_mb(); // ->gp_start before ->jiffies_stall and caller's ->gp_seq.
143 WRITE_ONCE(rcu_state.jiffies_stall, j + j1);
144 rcu_state.jiffies_resched = j + j1 / 2;
145 rcu_state.n_force_qs_gpstart = READ_ONCE(rcu_state.n_force_qs);
146 }
147
148 /* Zero ->ticks_this_gp and snapshot the number of RCU softirq handlers. */
zero_cpu_stall_ticks(struct rcu_data * rdp)149 static void zero_cpu_stall_ticks(struct rcu_data *rdp)
150 {
151 rdp->ticks_this_gp = 0;
152 rdp->softirq_snap = kstat_softirqs_cpu(RCU_SOFTIRQ, smp_processor_id());
153 WRITE_ONCE(rdp->last_fqs_resched, jiffies);
154 }
155
156 /*
157 * If too much time has passed in the current grace period, and if
158 * so configured, go kick the relevant kthreads.
159 */
rcu_stall_kick_kthreads(void)160 static void rcu_stall_kick_kthreads(void)
161 {
162 unsigned long j;
163
164 if (!READ_ONCE(rcu_kick_kthreads))
165 return;
166 j = READ_ONCE(rcu_state.jiffies_kick_kthreads);
167 if (time_after(jiffies, j) && rcu_state.gp_kthread &&
168 (rcu_gp_in_progress() || READ_ONCE(rcu_state.gp_flags))) {
169 WARN_ONCE(1, "Kicking %s grace-period kthread\n",
170 rcu_state.name);
171 rcu_ftrace_dump(DUMP_ALL);
172 wake_up_process(rcu_state.gp_kthread);
173 WRITE_ONCE(rcu_state.jiffies_kick_kthreads, j + HZ);
174 }
175 }
176
177 /*
178 * Handler for the irq_work request posted about halfway into the RCU CPU
179 * stall timeout, and used to detect excessive irq disabling. Set state
180 * appropriately, but just complain if there is unexpected state on entry.
181 */
rcu_iw_handler(struct irq_work * iwp)182 static void rcu_iw_handler(struct irq_work *iwp)
183 {
184 struct rcu_data *rdp;
185 struct rcu_node *rnp;
186
187 rdp = container_of(iwp, struct rcu_data, rcu_iw);
188 rnp = rdp->mynode;
189 raw_spin_lock_rcu_node(rnp);
190 if (!WARN_ON_ONCE(!rdp->rcu_iw_pending)) {
191 rdp->rcu_iw_gp_seq = rnp->gp_seq;
192 rdp->rcu_iw_pending = false;
193 }
194 raw_spin_unlock_rcu_node(rnp);
195 }
196
197 //////////////////////////////////////////////////////////////////////////////
198 //
199 // Printing RCU CPU stall warnings
200
201 #ifdef CONFIG_PREEMPT_RCU
202
203 /*
204 * Dump detailed information for all tasks blocking the current RCU
205 * grace period on the specified rcu_node structure.
206 */
rcu_print_detail_task_stall_rnp(struct rcu_node * rnp)207 static void rcu_print_detail_task_stall_rnp(struct rcu_node *rnp)
208 {
209 unsigned long flags;
210 struct task_struct *t;
211
212 raw_spin_lock_irqsave_rcu_node(rnp, flags);
213 if (!rcu_preempt_blocked_readers_cgp(rnp)) {
214 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
215 return;
216 }
217 t = list_entry(rnp->gp_tasks->prev,
218 struct task_struct, rcu_node_entry);
219 list_for_each_entry_continue(t, &rnp->blkd_tasks, rcu_node_entry) {
220 /*
221 * We could be printing a lot while holding a spinlock.
222 * Avoid triggering hard lockup.
223 */
224 touch_nmi_watchdog();
225 sched_show_task(t);
226 }
227 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
228 }
229
230 // Communicate task state back to the RCU CPU stall warning request.
231 struct rcu_stall_chk_rdr {
232 int nesting;
233 union rcu_special rs;
234 bool on_blkd_list;
235 };
236
237 /*
238 * Report out the state of a not-running task that is stalling the
239 * current RCU grace period.
240 */
check_slow_task(struct task_struct * t,void * arg)241 static bool check_slow_task(struct task_struct *t, void *arg)
242 {
243 struct rcu_stall_chk_rdr *rscrp = arg;
244
245 if (task_curr(t))
246 return false; // It is running, so decline to inspect it.
247 rscrp->nesting = t->rcu_read_lock_nesting;
248 rscrp->rs = t->rcu_read_unlock_special;
249 rscrp->on_blkd_list = !list_empty(&t->rcu_node_entry);
250 return true;
251 }
252
253 /*
254 * Scan the current list of tasks blocked within RCU read-side critical
255 * sections, printing out the tid of each of the first few of them.
256 */
rcu_print_task_stall(struct rcu_node * rnp,unsigned long flags)257 static int rcu_print_task_stall(struct rcu_node *rnp, unsigned long flags)
258 __releases(rnp->lock)
259 {
260 int i = 0;
261 int ndetected = 0;
262 struct rcu_stall_chk_rdr rscr;
263 struct task_struct *t;
264 struct task_struct *ts[8];
265
266 lockdep_assert_irqs_disabled();
267 if (!rcu_preempt_blocked_readers_cgp(rnp)) {
268 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
269 return 0;
270 }
271 pr_err("\tTasks blocked on level-%d rcu_node (CPUs %d-%d):",
272 rnp->level, rnp->grplo, rnp->grphi);
273 t = list_entry(rnp->gp_tasks->prev,
274 struct task_struct, rcu_node_entry);
275 list_for_each_entry_continue(t, &rnp->blkd_tasks, rcu_node_entry) {
276 get_task_struct(t);
277 ts[i++] = t;
278 if (i >= ARRAY_SIZE(ts))
279 break;
280 }
281 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
282 while (i) {
283 t = ts[--i];
284 if (!try_invoke_on_locked_down_task(t, check_slow_task, &rscr))
285 pr_cont(" P%d", t->pid);
286 else
287 pr_cont(" P%d/%d:%c%c%c%c",
288 t->pid, rscr.nesting,
289 ".b"[rscr.rs.b.blocked],
290 ".q"[rscr.rs.b.need_qs],
291 ".e"[rscr.rs.b.exp_hint],
292 ".l"[rscr.on_blkd_list]);
293 lockdep_assert_irqs_disabled();
294 put_task_struct(t);
295 ndetected++;
296 }
297 pr_cont("\n");
298 return ndetected;
299 }
300
301 #else /* #ifdef CONFIG_PREEMPT_RCU */
302
303 /*
304 * Because preemptible RCU does not exist, we never have to check for
305 * tasks blocked within RCU read-side critical sections.
306 */
rcu_print_detail_task_stall_rnp(struct rcu_node * rnp)307 static void rcu_print_detail_task_stall_rnp(struct rcu_node *rnp)
308 {
309 }
310
311 /*
312 * Because preemptible RCU does not exist, we never have to check for
313 * tasks blocked within RCU read-side critical sections.
314 */
rcu_print_task_stall(struct rcu_node * rnp,unsigned long flags)315 static int rcu_print_task_stall(struct rcu_node *rnp, unsigned long flags)
316 {
317 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
318 return 0;
319 }
320 #endif /* #else #ifdef CONFIG_PREEMPT_RCU */
321
322 /*
323 * Dump stacks of all tasks running on stalled CPUs. First try using
324 * NMIs, but fall back to manual remote stack tracing on architectures
325 * that don't support NMI-based stack dumps. The NMI-triggered stack
326 * traces are more accurate because they are printed by the target CPU.
327 */
rcu_dump_cpu_stacks(void)328 static void rcu_dump_cpu_stacks(void)
329 {
330 int cpu;
331 unsigned long flags;
332 struct rcu_node *rnp;
333
334 rcu_for_each_leaf_node(rnp) {
335 raw_spin_lock_irqsave_rcu_node(rnp, flags);
336 for_each_leaf_node_possible_cpu(rnp, cpu)
337 if (rnp->qsmask & leaf_node_cpu_bit(rnp, cpu))
338 if (!trigger_single_cpu_backtrace(cpu))
339 dump_cpu_task(cpu);
340 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
341 }
342 }
343
344 #ifdef CONFIG_RCU_FAST_NO_HZ
345
print_cpu_stall_fast_no_hz(char * cp,int cpu)346 static void print_cpu_stall_fast_no_hz(char *cp, int cpu)
347 {
348 struct rcu_data *rdp = &per_cpu(rcu_data, cpu);
349
350 sprintf(cp, "last_accelerate: %04lx/%04lx dyntick_enabled: %d",
351 rdp->last_accelerate & 0xffff, jiffies & 0xffff,
352 !!rdp->tick_nohz_enabled_snap);
353 }
354
355 #else /* #ifdef CONFIG_RCU_FAST_NO_HZ */
356
print_cpu_stall_fast_no_hz(char * cp,int cpu)357 static void print_cpu_stall_fast_no_hz(char *cp, int cpu)
358 {
359 *cp = '\0';
360 }
361
362 #endif /* #else #ifdef CONFIG_RCU_FAST_NO_HZ */
363
364 static const char * const gp_state_names[] = {
365 [RCU_GP_IDLE] = "RCU_GP_IDLE",
366 [RCU_GP_WAIT_GPS] = "RCU_GP_WAIT_GPS",
367 [RCU_GP_DONE_GPS] = "RCU_GP_DONE_GPS",
368 [RCU_GP_ONOFF] = "RCU_GP_ONOFF",
369 [RCU_GP_INIT] = "RCU_GP_INIT",
370 [RCU_GP_WAIT_FQS] = "RCU_GP_WAIT_FQS",
371 [RCU_GP_DOING_FQS] = "RCU_GP_DOING_FQS",
372 [RCU_GP_CLEANUP] = "RCU_GP_CLEANUP",
373 [RCU_GP_CLEANED] = "RCU_GP_CLEANED",
374 };
375
376 /*
377 * Convert a ->gp_state value to a character string.
378 */
gp_state_getname(short gs)379 static const char *gp_state_getname(short gs)
380 {
381 if (gs < 0 || gs >= ARRAY_SIZE(gp_state_names))
382 return "???";
383 return gp_state_names[gs];
384 }
385
386 /* Is the RCU grace-period kthread being starved of CPU time? */
rcu_is_gp_kthread_starving(unsigned long * jp)387 static bool rcu_is_gp_kthread_starving(unsigned long *jp)
388 {
389 unsigned long j = jiffies - READ_ONCE(rcu_state.gp_activity);
390
391 if (jp)
392 *jp = j;
393 return j > 2 * HZ;
394 }
395
396 /*
397 * Print out diagnostic information for the specified stalled CPU.
398 *
399 * If the specified CPU is aware of the current RCU grace period, then
400 * print the number of scheduling clock interrupts the CPU has taken
401 * during the time that it has been aware. Otherwise, print the number
402 * of RCU grace periods that this CPU is ignorant of, for example, "1"
403 * if the CPU was aware of the previous grace period.
404 *
405 * Also print out idle and (if CONFIG_RCU_FAST_NO_HZ) idle-entry info.
406 */
print_cpu_stall_info(int cpu)407 static void print_cpu_stall_info(int cpu)
408 {
409 unsigned long delta;
410 bool falsepositive;
411 char fast_no_hz[72];
412 struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
413 char *ticks_title;
414 unsigned long ticks_value;
415
416 /*
417 * We could be printing a lot while holding a spinlock. Avoid
418 * triggering hard lockup.
419 */
420 touch_nmi_watchdog();
421
422 ticks_value = rcu_seq_ctr(rcu_state.gp_seq - rdp->gp_seq);
423 if (ticks_value) {
424 ticks_title = "GPs behind";
425 } else {
426 ticks_title = "ticks this GP";
427 ticks_value = rdp->ticks_this_gp;
428 }
429 print_cpu_stall_fast_no_hz(fast_no_hz, cpu);
430 delta = rcu_seq_ctr(rdp->mynode->gp_seq - rdp->rcu_iw_gp_seq);
431 falsepositive = rcu_is_gp_kthread_starving(NULL) &&
432 rcu_dynticks_in_eqs(rcu_dynticks_snap(rdp));
433 pr_err("\t%d-%c%c%c%c: (%lu %s) idle=%03x/%ld/%#lx softirq=%u/%u fqs=%ld %s%s\n",
434 cpu,
435 "O."[!!cpu_online(cpu)],
436 "o."[!!(rdp->grpmask & rdp->mynode->qsmaskinit)],
437 "N."[!!(rdp->grpmask & rdp->mynode->qsmaskinitnext)],
438 !IS_ENABLED(CONFIG_IRQ_WORK) ? '?' :
439 rdp->rcu_iw_pending ? (int)min(delta, 9UL) + '0' :
440 "!."[!delta],
441 ticks_value, ticks_title,
442 rcu_dynticks_snap(rdp) & 0xfff,
443 rdp->dynticks_nesting, rdp->dynticks_nmi_nesting,
444 rdp->softirq_snap, kstat_softirqs_cpu(RCU_SOFTIRQ, cpu),
445 data_race(rcu_state.n_force_qs) - rcu_state.n_force_qs_gpstart,
446 fast_no_hz,
447 falsepositive ? " (false positive?)" : "");
448 }
449
450 /* Complain about starvation of grace-period kthread. */
rcu_check_gp_kthread_starvation(void)451 static void rcu_check_gp_kthread_starvation(void)
452 {
453 struct task_struct *gpk = rcu_state.gp_kthread;
454 unsigned long j;
455
456 if (rcu_is_gp_kthread_starving(&j)) {
457 pr_err("%s kthread starved for %ld jiffies! g%ld f%#x %s(%d) ->state=%#lx ->cpu=%d\n",
458 rcu_state.name, j,
459 (long)rcu_seq_current(&rcu_state.gp_seq),
460 data_race(rcu_state.gp_flags),
461 gp_state_getname(rcu_state.gp_state), rcu_state.gp_state,
462 gpk ? gpk->state : ~0, gpk ? task_cpu(gpk) : -1);
463 if (gpk) {
464 pr_err("\tUnless %s kthread gets sufficient CPU time, OOM is now expected behavior.\n", rcu_state.name);
465 pr_err("RCU grace-period kthread stack dump:\n");
466 sched_show_task(gpk);
467 wake_up_process(gpk);
468 }
469 }
470 }
471
print_other_cpu_stall(unsigned long gp_seq,unsigned long gps)472 static void print_other_cpu_stall(unsigned long gp_seq, unsigned long gps)
473 {
474 int cpu;
475 unsigned long flags;
476 unsigned long gpa;
477 unsigned long j;
478 int ndetected = 0;
479 struct rcu_node *rnp;
480 long totqlen = 0;
481
482 lockdep_assert_irqs_disabled();
483
484 /* Kick and suppress, if so configured. */
485 rcu_stall_kick_kthreads();
486 if (rcu_stall_is_suppressed())
487 return;
488
489 /*
490 * OK, time to rat on our buddy...
491 * See Documentation/RCU/stallwarn.rst for info on how to debug
492 * RCU CPU stall warnings.
493 */
494 trace_rcu_stall_warning(rcu_state.name, TPS("StallDetected"));
495 pr_err("INFO: %s detected stalls on CPUs/tasks:\n", rcu_state.name);
496 rcu_for_each_leaf_node(rnp) {
497 raw_spin_lock_irqsave_rcu_node(rnp, flags);
498 if (rnp->qsmask != 0) {
499 for_each_leaf_node_possible_cpu(rnp, cpu)
500 if (rnp->qsmask & leaf_node_cpu_bit(rnp, cpu)) {
501 print_cpu_stall_info(cpu);
502 ndetected++;
503 }
504 }
505 ndetected += rcu_print_task_stall(rnp, flags); // Releases rnp->lock.
506 lockdep_assert_irqs_disabled();
507 }
508
509 for_each_possible_cpu(cpu)
510 totqlen += rcu_get_n_cbs_cpu(cpu);
511 pr_cont("\t(detected by %d, t=%ld jiffies, g=%ld, q=%lu)\n",
512 smp_processor_id(), (long)(jiffies - gps),
513 (long)rcu_seq_current(&rcu_state.gp_seq), totqlen);
514 if (ndetected) {
515 rcu_dump_cpu_stacks();
516
517 /* Complain about tasks blocking the grace period. */
518 rcu_for_each_leaf_node(rnp)
519 rcu_print_detail_task_stall_rnp(rnp);
520 } else {
521 if (rcu_seq_current(&rcu_state.gp_seq) != gp_seq) {
522 pr_err("INFO: Stall ended before state dump start\n");
523 } else {
524 j = jiffies;
525 gpa = data_race(rcu_state.gp_activity);
526 pr_err("All QSes seen, last %s kthread activity %ld (%ld-%ld), jiffies_till_next_fqs=%ld, root ->qsmask %#lx\n",
527 rcu_state.name, j - gpa, j, gpa,
528 data_race(jiffies_till_next_fqs),
529 rcu_get_root()->qsmask);
530 }
531 }
532 /* Rewrite if needed in case of slow consoles. */
533 if (ULONG_CMP_GE(jiffies, READ_ONCE(rcu_state.jiffies_stall)))
534 WRITE_ONCE(rcu_state.jiffies_stall,
535 jiffies + 3 * rcu_jiffies_till_stall_check() + 3);
536
537 rcu_check_gp_kthread_starvation();
538
539 atomic_notifier_call_chain(&rcu_stall_notifier_list, 0, NULL);
540
541 panic_on_rcu_stall();
542
543 rcu_force_quiescent_state(); /* Kick them all. */
544 }
545
print_cpu_stall(unsigned long gps)546 static void print_cpu_stall(unsigned long gps)
547 {
548 int cpu;
549 unsigned long flags;
550 struct rcu_data *rdp = this_cpu_ptr(&rcu_data);
551 struct rcu_node *rnp = rcu_get_root();
552 long totqlen = 0;
553
554 lockdep_assert_irqs_disabled();
555
556 /* Kick and suppress, if so configured. */
557 rcu_stall_kick_kthreads();
558 if (rcu_stall_is_suppressed())
559 return;
560
561 /*
562 * OK, time to rat on ourselves...
563 * See Documentation/RCU/stallwarn.rst for info on how to debug
564 * RCU CPU stall warnings.
565 */
566 trace_rcu_stall_warning(rcu_state.name, TPS("SelfDetected"));
567 pr_err("INFO: %s self-detected stall on CPU\n", rcu_state.name);
568 raw_spin_lock_irqsave_rcu_node(rdp->mynode, flags);
569 print_cpu_stall_info(smp_processor_id());
570 raw_spin_unlock_irqrestore_rcu_node(rdp->mynode, flags);
571 for_each_possible_cpu(cpu)
572 totqlen += rcu_get_n_cbs_cpu(cpu);
573 pr_cont("\t(t=%lu jiffies g=%ld q=%lu)\n",
574 jiffies - gps,
575 (long)rcu_seq_current(&rcu_state.gp_seq), totqlen);
576
577 rcu_check_gp_kthread_starvation();
578
579 rcu_dump_cpu_stacks();
580
581 raw_spin_lock_irqsave_rcu_node(rnp, flags);
582 /* Rewrite if needed in case of slow consoles. */
583 if (ULONG_CMP_GE(jiffies, READ_ONCE(rcu_state.jiffies_stall)))
584 WRITE_ONCE(rcu_state.jiffies_stall,
585 jiffies + 3 * rcu_jiffies_till_stall_check() + 3);
586 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
587
588 panic_on_rcu_stall();
589
590 /*
591 * Attempt to revive the RCU machinery by forcing a context switch.
592 *
593 * A context switch would normally allow the RCU state machine to make
594 * progress and it could be we're stuck in kernel space without context
595 * switches for an entirely unreasonable amount of time.
596 */
597 set_tsk_need_resched(current);
598 set_preempt_need_resched();
599 }
600
check_cpu_stall(struct rcu_data * rdp)601 static void check_cpu_stall(struct rcu_data *rdp)
602 {
603 unsigned long gs1;
604 unsigned long gs2;
605 unsigned long gps;
606 unsigned long j;
607 unsigned long jn;
608 unsigned long js;
609 struct rcu_node *rnp;
610
611 lockdep_assert_irqs_disabled();
612 if ((rcu_stall_is_suppressed() && !READ_ONCE(rcu_kick_kthreads)) ||
613 !rcu_gp_in_progress())
614 return;
615 rcu_stall_kick_kthreads();
616 j = jiffies;
617
618 /*
619 * Lots of memory barriers to reject false positives.
620 *
621 * The idea is to pick up rcu_state.gp_seq, then
622 * rcu_state.jiffies_stall, then rcu_state.gp_start, and finally
623 * another copy of rcu_state.gp_seq. These values are updated in
624 * the opposite order with memory barriers (or equivalent) during
625 * grace-period initialization and cleanup. Now, a false positive
626 * can occur if we get an new value of rcu_state.gp_start and a old
627 * value of rcu_state.jiffies_stall. But given the memory barriers,
628 * the only way that this can happen is if one grace period ends
629 * and another starts between these two fetches. This is detected
630 * by comparing the second fetch of rcu_state.gp_seq with the
631 * previous fetch from rcu_state.gp_seq.
632 *
633 * Given this check, comparisons of jiffies, rcu_state.jiffies_stall,
634 * and rcu_state.gp_start suffice to forestall false positives.
635 */
636 gs1 = READ_ONCE(rcu_state.gp_seq);
637 smp_rmb(); /* Pick up ->gp_seq first... */
638 js = READ_ONCE(rcu_state.jiffies_stall);
639 smp_rmb(); /* ...then ->jiffies_stall before the rest... */
640 gps = READ_ONCE(rcu_state.gp_start);
641 smp_rmb(); /* ...and finally ->gp_start before ->gp_seq again. */
642 gs2 = READ_ONCE(rcu_state.gp_seq);
643 if (gs1 != gs2 ||
644 ULONG_CMP_LT(j, js) ||
645 ULONG_CMP_GE(gps, js))
646 return; /* No stall or GP completed since entering function. */
647 rnp = rdp->mynode;
648 jn = jiffies + 3 * rcu_jiffies_till_stall_check() + 3;
649 if (rcu_gp_in_progress() &&
650 (READ_ONCE(rnp->qsmask) & rdp->grpmask) &&
651 cmpxchg(&rcu_state.jiffies_stall, js, jn) == js) {
652
653 /*
654 * If a virtual machine is stopped by the host it can look to
655 * the watchdog like an RCU stall. Check to see if the host
656 * stopped the vm.
657 */
658 if (kvm_check_and_clear_guest_paused())
659 return;
660
661 /* We haven't checked in, so go dump stack. */
662 print_cpu_stall(gps);
663 if (READ_ONCE(rcu_cpu_stall_ftrace_dump))
664 rcu_ftrace_dump(DUMP_ALL);
665
666 } else if (rcu_gp_in_progress() &&
667 ULONG_CMP_GE(j, js + RCU_STALL_RAT_DELAY) &&
668 cmpxchg(&rcu_state.jiffies_stall, js, jn) == js) {
669
670 /*
671 * If a virtual machine is stopped by the host it can look to
672 * the watchdog like an RCU stall. Check to see if the host
673 * stopped the vm.
674 */
675 if (kvm_check_and_clear_guest_paused())
676 return;
677
678 /* They had a few time units to dump stack, so complain. */
679 print_other_cpu_stall(gs2, gps);
680 if (READ_ONCE(rcu_cpu_stall_ftrace_dump))
681 rcu_ftrace_dump(DUMP_ALL);
682 }
683 }
684
685 //////////////////////////////////////////////////////////////////////////////
686 //
687 // RCU forward-progress mechanisms, including of callback invocation.
688
689
690 /*
691 * Show the state of the grace-period kthreads.
692 */
show_rcu_gp_kthreads(void)693 void show_rcu_gp_kthreads(void)
694 {
695 unsigned long cbs = 0;
696 int cpu;
697 unsigned long j;
698 unsigned long ja;
699 unsigned long jr;
700 unsigned long jw;
701 struct rcu_data *rdp;
702 struct rcu_node *rnp;
703 struct task_struct *t = READ_ONCE(rcu_state.gp_kthread);
704
705 j = jiffies;
706 ja = j - data_race(rcu_state.gp_activity);
707 jr = j - data_race(rcu_state.gp_req_activity);
708 jw = j - data_race(rcu_state.gp_wake_time);
709 pr_info("%s: wait state: %s(%d) ->state: %#lx delta ->gp_activity %lu ->gp_req_activity %lu ->gp_wake_time %lu ->gp_wake_seq %ld ->gp_seq %ld ->gp_seq_needed %ld ->gp_flags %#x\n",
710 rcu_state.name, gp_state_getname(rcu_state.gp_state),
711 rcu_state.gp_state, t ? t->state : 0x1ffffL,
712 ja, jr, jw, (long)data_race(rcu_state.gp_wake_seq),
713 (long)data_race(rcu_state.gp_seq),
714 (long)data_race(rcu_get_root()->gp_seq_needed),
715 data_race(rcu_state.gp_flags));
716 rcu_for_each_node_breadth_first(rnp) {
717 if (ULONG_CMP_GE(READ_ONCE(rcu_state.gp_seq),
718 READ_ONCE(rnp->gp_seq_needed)))
719 continue;
720 pr_info("\trcu_node %d:%d ->gp_seq %ld ->gp_seq_needed %ld\n",
721 rnp->grplo, rnp->grphi, (long)data_race(rnp->gp_seq),
722 (long)data_race(rnp->gp_seq_needed));
723 if (!rcu_is_leaf_node(rnp))
724 continue;
725 for_each_leaf_node_possible_cpu(rnp, cpu) {
726 rdp = per_cpu_ptr(&rcu_data, cpu);
727 if (READ_ONCE(rdp->gpwrap) ||
728 ULONG_CMP_GE(READ_ONCE(rcu_state.gp_seq),
729 READ_ONCE(rdp->gp_seq_needed)))
730 continue;
731 pr_info("\tcpu %d ->gp_seq_needed %ld\n",
732 cpu, (long)data_race(rdp->gp_seq_needed));
733 }
734 }
735 for_each_possible_cpu(cpu) {
736 rdp = per_cpu_ptr(&rcu_data, cpu);
737 cbs += data_race(rdp->n_cbs_invoked);
738 if (rcu_segcblist_is_offloaded(&rdp->cblist))
739 show_rcu_nocb_state(rdp);
740 }
741 pr_info("RCU callbacks invoked since boot: %lu\n", cbs);
742 show_rcu_tasks_gp_kthreads();
743 }
744 EXPORT_SYMBOL_GPL(show_rcu_gp_kthreads);
745
746 /*
747 * This function checks for grace-period requests that fail to motivate
748 * RCU to come out of its idle mode.
749 */
rcu_check_gp_start_stall(struct rcu_node * rnp,struct rcu_data * rdp,const unsigned long gpssdelay)750 static void rcu_check_gp_start_stall(struct rcu_node *rnp, struct rcu_data *rdp,
751 const unsigned long gpssdelay)
752 {
753 unsigned long flags;
754 unsigned long j;
755 struct rcu_node *rnp_root = rcu_get_root();
756 static atomic_t warned = ATOMIC_INIT(0);
757
758 if (!IS_ENABLED(CONFIG_PROVE_RCU) || rcu_gp_in_progress() ||
759 ULONG_CMP_GE(READ_ONCE(rnp_root->gp_seq),
760 READ_ONCE(rnp_root->gp_seq_needed)) ||
761 !smp_load_acquire(&rcu_state.gp_kthread)) // Get stable kthread.
762 return;
763 j = jiffies; /* Expensive access, and in common case don't get here. */
764 if (time_before(j, READ_ONCE(rcu_state.gp_req_activity) + gpssdelay) ||
765 time_before(j, READ_ONCE(rcu_state.gp_activity) + gpssdelay) ||
766 atomic_read(&warned))
767 return;
768
769 raw_spin_lock_irqsave_rcu_node(rnp, flags);
770 j = jiffies;
771 if (rcu_gp_in_progress() ||
772 ULONG_CMP_GE(READ_ONCE(rnp_root->gp_seq),
773 READ_ONCE(rnp_root->gp_seq_needed)) ||
774 time_before(j, READ_ONCE(rcu_state.gp_req_activity) + gpssdelay) ||
775 time_before(j, READ_ONCE(rcu_state.gp_activity) + gpssdelay) ||
776 atomic_read(&warned)) {
777 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
778 return;
779 }
780 /* Hold onto the leaf lock to make others see warned==1. */
781
782 if (rnp_root != rnp)
783 raw_spin_lock_rcu_node(rnp_root); /* irqs already disabled. */
784 j = jiffies;
785 if (rcu_gp_in_progress() ||
786 ULONG_CMP_GE(READ_ONCE(rnp_root->gp_seq),
787 READ_ONCE(rnp_root->gp_seq_needed)) ||
788 time_before(j, READ_ONCE(rcu_state.gp_req_activity) + gpssdelay) ||
789 time_before(j, READ_ONCE(rcu_state.gp_activity) + gpssdelay) ||
790 atomic_xchg(&warned, 1)) {
791 if (rnp_root != rnp)
792 /* irqs remain disabled. */
793 raw_spin_unlock_rcu_node(rnp_root);
794 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
795 return;
796 }
797 WARN_ON(1);
798 if (rnp_root != rnp)
799 raw_spin_unlock_rcu_node(rnp_root);
800 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
801 show_rcu_gp_kthreads();
802 }
803
804 /*
805 * Do a forward-progress check for rcutorture. This is normally invoked
806 * due to an OOM event. The argument "j" gives the time period during
807 * which rcutorture would like progress to have been made.
808 */
rcu_fwd_progress_check(unsigned long j)809 void rcu_fwd_progress_check(unsigned long j)
810 {
811 unsigned long cbs;
812 int cpu;
813 unsigned long max_cbs = 0;
814 int max_cpu = -1;
815 struct rcu_data *rdp;
816
817 if (rcu_gp_in_progress()) {
818 pr_info("%s: GP age %lu jiffies\n",
819 __func__, jiffies - rcu_state.gp_start);
820 show_rcu_gp_kthreads();
821 } else {
822 pr_info("%s: Last GP end %lu jiffies ago\n",
823 __func__, jiffies - rcu_state.gp_end);
824 preempt_disable();
825 rdp = this_cpu_ptr(&rcu_data);
826 rcu_check_gp_start_stall(rdp->mynode, rdp, j);
827 preempt_enable();
828 }
829 for_each_possible_cpu(cpu) {
830 cbs = rcu_get_n_cbs_cpu(cpu);
831 if (!cbs)
832 continue;
833 if (max_cpu < 0)
834 pr_info("%s: callbacks", __func__);
835 pr_cont(" %d: %lu", cpu, cbs);
836 if (cbs <= max_cbs)
837 continue;
838 max_cbs = cbs;
839 max_cpu = cpu;
840 }
841 if (max_cpu >= 0)
842 pr_cont("\n");
843 }
844 EXPORT_SYMBOL_GPL(rcu_fwd_progress_check);
845
846 /* Commandeer a sysrq key to dump RCU's tree. */
847 static bool sysrq_rcu;
848 module_param(sysrq_rcu, bool, 0444);
849
850 /* Dump grace-period-request information due to commandeered sysrq. */
sysrq_show_rcu(int key)851 static void sysrq_show_rcu(int key)
852 {
853 show_rcu_gp_kthreads();
854 }
855
856 static const struct sysrq_key_op sysrq_rcudump_op = {
857 .handler = sysrq_show_rcu,
858 .help_msg = "show-rcu(y)",
859 .action_msg = "Show RCU tree",
860 .enable_mask = SYSRQ_ENABLE_DUMP,
861 };
862
rcu_sysrq_init(void)863 static int __init rcu_sysrq_init(void)
864 {
865 if (sysrq_rcu)
866 return register_sysrq_key('y', &sysrq_rcudump_op);
867 return 0;
868 }
869 early_initcall(rcu_sysrq_init);
870