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