• 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 int sysctl_max_rcu_stall_to_panic __read_mostly;
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 	static int cpu_stall;
113 
114 	if (++cpu_stall < sysctl_max_rcu_stall_to_panic)
115 		return;
116 
117 	if (sysctl_panic_on_rcu_stall)
118 		panic("RCU Stall\n");
119 }
120 
121 /**
122  * rcu_cpu_stall_reset - restart stall-warning timeout for current grace period
123  *
124  * To perform the reset request from the caller, disable stall detection until
125  * 3 fqs loops have passed. This is required to ensure a fresh jiffies is
126  * loaded.  It should be safe to do from the fqs loop as enough timer
127  * interrupts and context switches should have passed.
128  *
129  * The caller must disable hard irqs.
130  */
rcu_cpu_stall_reset(void)131 void rcu_cpu_stall_reset(void)
132 {
133 	WRITE_ONCE(rcu_state.nr_fqs_jiffies_stall, 3);
134 	WRITE_ONCE(rcu_state.jiffies_stall, ULONG_MAX);
135 }
136 
137 //////////////////////////////////////////////////////////////////////////////
138 //
139 // Interaction with RCU grace periods
140 
141 /* Start of new grace period, so record stall time (and forcing times). */
record_gp_stall_check_time(void)142 static void record_gp_stall_check_time(void)
143 {
144 	unsigned long j = jiffies;
145 	unsigned long j1;
146 
147 	WRITE_ONCE(rcu_state.gp_start, j);
148 	j1 = rcu_jiffies_till_stall_check();
149 	smp_mb(); // ->gp_start before ->jiffies_stall and caller's ->gp_seq.
150 	WRITE_ONCE(rcu_state.nr_fqs_jiffies_stall, 0);
151 	WRITE_ONCE(rcu_state.jiffies_stall, j + j1);
152 	rcu_state.jiffies_resched = j + j1 / 2;
153 	rcu_state.n_force_qs_gpstart = READ_ONCE(rcu_state.n_force_qs);
154 }
155 
156 /* Zero ->ticks_this_gp and snapshot the number of RCU softirq handlers. */
zero_cpu_stall_ticks(struct rcu_data * rdp)157 static void zero_cpu_stall_ticks(struct rcu_data *rdp)
158 {
159 	rdp->ticks_this_gp = 0;
160 	rdp->softirq_snap = kstat_softirqs_cpu(RCU_SOFTIRQ, smp_processor_id());
161 	WRITE_ONCE(rdp->last_fqs_resched, jiffies);
162 }
163 
164 /*
165  * If too much time has passed in the current grace period, and if
166  * so configured, go kick the relevant kthreads.
167  */
rcu_stall_kick_kthreads(void)168 static void rcu_stall_kick_kthreads(void)
169 {
170 	unsigned long j;
171 
172 	if (!READ_ONCE(rcu_kick_kthreads))
173 		return;
174 	j = READ_ONCE(rcu_state.jiffies_kick_kthreads);
175 	if (time_after(jiffies, j) && rcu_state.gp_kthread &&
176 	    (rcu_gp_in_progress() || READ_ONCE(rcu_state.gp_flags))) {
177 		WARN_ONCE(1, "Kicking %s grace-period kthread\n",
178 			  rcu_state.name);
179 		rcu_ftrace_dump(DUMP_ALL);
180 		wake_up_process(rcu_state.gp_kthread);
181 		WRITE_ONCE(rcu_state.jiffies_kick_kthreads, j + HZ);
182 	}
183 }
184 
185 /*
186  * Handler for the irq_work request posted about halfway into the RCU CPU
187  * stall timeout, and used to detect excessive irq disabling.  Set state
188  * appropriately, but just complain if there is unexpected state on entry.
189  */
rcu_iw_handler(struct irq_work * iwp)190 static void rcu_iw_handler(struct irq_work *iwp)
191 {
192 	struct rcu_data *rdp;
193 	struct rcu_node *rnp;
194 
195 	rdp = container_of(iwp, struct rcu_data, rcu_iw);
196 	rnp = rdp->mynode;
197 	raw_spin_lock_rcu_node(rnp);
198 	if (!WARN_ON_ONCE(!rdp->rcu_iw_pending)) {
199 		rdp->rcu_iw_gp_seq = rnp->gp_seq;
200 		rdp->rcu_iw_pending = false;
201 	}
202 	raw_spin_unlock_rcu_node(rnp);
203 }
204 
205 //////////////////////////////////////////////////////////////////////////////
206 //
207 // Printing RCU CPU stall warnings
208 
209 #ifdef CONFIG_PREEMPT_RCU
210 
211 /*
212  * Dump detailed information for all tasks blocking the current RCU
213  * grace period on the specified rcu_node structure.
214  */
rcu_print_detail_task_stall_rnp(struct rcu_node * rnp)215 static void rcu_print_detail_task_stall_rnp(struct rcu_node *rnp)
216 {
217 	unsigned long flags;
218 	struct task_struct *t;
219 
220 	raw_spin_lock_irqsave_rcu_node(rnp, flags);
221 	if (!rcu_preempt_blocked_readers_cgp(rnp)) {
222 		raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
223 		return;
224 	}
225 	t = list_entry(rnp->gp_tasks->prev,
226 		       struct task_struct, rcu_node_entry);
227 	list_for_each_entry_continue(t, &rnp->blkd_tasks, rcu_node_entry) {
228 		/*
229 		 * We could be printing a lot while holding a spinlock.
230 		 * Avoid triggering hard lockup.
231 		 */
232 		touch_nmi_watchdog();
233 		sched_show_task(t);
234 	}
235 	raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
236 }
237 
238 // Communicate task state back to the RCU CPU stall warning request.
239 struct rcu_stall_chk_rdr {
240 	int nesting;
241 	union rcu_special rs;
242 	bool on_blkd_list;
243 };
244 
245 /*
246  * Report out the state of a not-running task that is stalling the
247  * current RCU grace period.
248  */
check_slow_task(struct task_struct * t,void * arg)249 static bool check_slow_task(struct task_struct *t, void *arg)
250 {
251 	struct rcu_stall_chk_rdr *rscrp = arg;
252 
253 	if (task_curr(t))
254 		return false; // It is running, so decline to inspect it.
255 	rscrp->nesting = t->rcu_read_lock_nesting;
256 	rscrp->rs = t->rcu_read_unlock_special;
257 	rscrp->on_blkd_list = !list_empty(&t->rcu_node_entry);
258 	return true;
259 }
260 
261 /*
262  * Scan the current list of tasks blocked within RCU read-side critical
263  * sections, printing out the tid of each of the first few of them.
264  */
rcu_print_task_stall(struct rcu_node * rnp,unsigned long flags)265 static int rcu_print_task_stall(struct rcu_node *rnp, unsigned long flags)
266 	__releases(rnp->lock)
267 {
268 	int i = 0;
269 	int ndetected = 0;
270 	struct rcu_stall_chk_rdr rscr;
271 	struct task_struct *t;
272 	struct task_struct *ts[8];
273 
274 	lockdep_assert_irqs_disabled();
275 	if (!rcu_preempt_blocked_readers_cgp(rnp)) {
276 		raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
277 		return 0;
278 	}
279 	pr_err("\tTasks blocked on level-%d rcu_node (CPUs %d-%d):",
280 	       rnp->level, rnp->grplo, rnp->grphi);
281 	t = list_entry(rnp->gp_tasks->prev,
282 		       struct task_struct, rcu_node_entry);
283 	list_for_each_entry_continue(t, &rnp->blkd_tasks, rcu_node_entry) {
284 		get_task_struct(t);
285 		ts[i++] = t;
286 		if (i >= ARRAY_SIZE(ts))
287 			break;
288 	}
289 	raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
290 	while (i) {
291 		t = ts[--i];
292 		if (!try_invoke_on_locked_down_task(t, check_slow_task, &rscr))
293 			pr_cont(" P%d", t->pid);
294 		else
295 			pr_cont(" P%d/%d:%c%c%c%c",
296 				t->pid, rscr.nesting,
297 				".b"[rscr.rs.b.blocked],
298 				".q"[rscr.rs.b.need_qs],
299 				".e"[rscr.rs.b.exp_hint],
300 				".l"[rscr.on_blkd_list]);
301 		lockdep_assert_irqs_disabled();
302 		put_task_struct(t);
303 		ndetected++;
304 	}
305 	pr_cont("\n");
306 	return ndetected;
307 }
308 
309 #else /* #ifdef CONFIG_PREEMPT_RCU */
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_detail_task_stall_rnp(struct rcu_node * rnp)315 static void rcu_print_detail_task_stall_rnp(struct rcu_node *rnp)
316 {
317 }
318 
319 /*
320  * Because preemptible RCU does not exist, we never have to check for
321  * tasks blocked within RCU read-side critical sections.
322  */
rcu_print_task_stall(struct rcu_node * rnp,unsigned long flags)323 static int rcu_print_task_stall(struct rcu_node *rnp, unsigned long flags)
324 	__releases(rnp->lock)
325 {
326 	raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
327 	return 0;
328 }
329 #endif /* #else #ifdef CONFIG_PREEMPT_RCU */
330 
331 /*
332  * Dump stacks of all tasks running on stalled CPUs.  First try using
333  * NMIs, but fall back to manual remote stack tracing on architectures
334  * that don't support NMI-based stack dumps.  The NMI-triggered stack
335  * traces are more accurate because they are printed by the target CPU.
336  */
rcu_dump_cpu_stacks(void)337 static void rcu_dump_cpu_stacks(void)
338 {
339 	int cpu;
340 	unsigned long flags;
341 	struct rcu_node *rnp;
342 
343 	rcu_for_each_leaf_node(rnp) {
344 		raw_spin_lock_irqsave_rcu_node(rnp, flags);
345 		for_each_leaf_node_possible_cpu(rnp, cpu)
346 			if (rnp->qsmask & leaf_node_cpu_bit(rnp, cpu)) {
347 				if (cpu_is_offline(cpu))
348 					pr_err("Offline CPU %d blocking current GP.\n", cpu);
349 				else if (!trigger_single_cpu_backtrace(cpu))
350 					dump_cpu_task(cpu);
351 			}
352 		raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
353 	}
354 }
355 
356 #ifdef CONFIG_RCU_FAST_NO_HZ
357 
print_cpu_stall_fast_no_hz(char * cp,int cpu)358 static void print_cpu_stall_fast_no_hz(char *cp, int cpu)
359 {
360 	struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
361 
362 	sprintf(cp, "last_accelerate: %04lx/%04lx dyntick_enabled: %d",
363 		rdp->last_accelerate & 0xffff, jiffies & 0xffff,
364 		!!rdp->tick_nohz_enabled_snap);
365 }
366 
367 #else /* #ifdef CONFIG_RCU_FAST_NO_HZ */
368 
print_cpu_stall_fast_no_hz(char * cp,int cpu)369 static void print_cpu_stall_fast_no_hz(char *cp, int cpu)
370 {
371 	*cp = '\0';
372 }
373 
374 #endif /* #else #ifdef CONFIG_RCU_FAST_NO_HZ */
375 
376 static const char * const gp_state_names[] = {
377 	[RCU_GP_IDLE] = "RCU_GP_IDLE",
378 	[RCU_GP_WAIT_GPS] = "RCU_GP_WAIT_GPS",
379 	[RCU_GP_DONE_GPS] = "RCU_GP_DONE_GPS",
380 	[RCU_GP_ONOFF] = "RCU_GP_ONOFF",
381 	[RCU_GP_INIT] = "RCU_GP_INIT",
382 	[RCU_GP_WAIT_FQS] = "RCU_GP_WAIT_FQS",
383 	[RCU_GP_DOING_FQS] = "RCU_GP_DOING_FQS",
384 	[RCU_GP_CLEANUP] = "RCU_GP_CLEANUP",
385 	[RCU_GP_CLEANED] = "RCU_GP_CLEANED",
386 };
387 
388 /*
389  * Convert a ->gp_state value to a character string.
390  */
gp_state_getname(short gs)391 static const char *gp_state_getname(short gs)
392 {
393 	if (gs < 0 || gs >= ARRAY_SIZE(gp_state_names))
394 		return "???";
395 	return gp_state_names[gs];
396 }
397 
398 /* Is the RCU grace-period kthread being starved of CPU time? */
rcu_is_gp_kthread_starving(unsigned long * jp)399 static bool rcu_is_gp_kthread_starving(unsigned long *jp)
400 {
401 	unsigned long j = jiffies - READ_ONCE(rcu_state.gp_activity);
402 
403 	if (jp)
404 		*jp = j;
405 	return j > 2 * HZ;
406 }
407 
408 /*
409  * Print out diagnostic information for the specified stalled CPU.
410  *
411  * If the specified CPU is aware of the current RCU grace period, then
412  * print the number of scheduling clock interrupts the CPU has taken
413  * during the time that it has been aware.  Otherwise, print the number
414  * of RCU grace periods that this CPU is ignorant of, for example, "1"
415  * if the CPU was aware of the previous grace period.
416  *
417  * Also print out idle and (if CONFIG_RCU_FAST_NO_HZ) idle-entry info.
418  */
print_cpu_stall_info(int cpu)419 static void print_cpu_stall_info(int cpu)
420 {
421 	unsigned long delta;
422 	bool falsepositive;
423 	char fast_no_hz[72];
424 	struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
425 	char *ticks_title;
426 	unsigned long ticks_value;
427 
428 	/*
429 	 * We could be printing a lot while holding a spinlock.  Avoid
430 	 * triggering hard lockup.
431 	 */
432 	touch_nmi_watchdog();
433 
434 	ticks_value = rcu_seq_ctr(rcu_state.gp_seq - rdp->gp_seq);
435 	if (ticks_value) {
436 		ticks_title = "GPs behind";
437 	} else {
438 		ticks_title = "ticks this GP";
439 		ticks_value = rdp->ticks_this_gp;
440 	}
441 	print_cpu_stall_fast_no_hz(fast_no_hz, cpu);
442 	delta = rcu_seq_ctr(rdp->mynode->gp_seq - rdp->rcu_iw_gp_seq);
443 	falsepositive = rcu_is_gp_kthread_starving(NULL) &&
444 			rcu_dynticks_in_eqs(rcu_dynticks_snap(rdp));
445 	pr_err("\t%d-%c%c%c%c: (%lu %s) idle=%03x/%ld/%#lx softirq=%u/%u fqs=%ld %s%s\n",
446 	       cpu,
447 	       "O."[!!cpu_online(cpu)],
448 	       "o."[!!(rdp->grpmask & rdp->mynode->qsmaskinit)],
449 	       "N."[!!(rdp->grpmask & rdp->mynode->qsmaskinitnext)],
450 	       !IS_ENABLED(CONFIG_IRQ_WORK) ? '?' :
451 			rdp->rcu_iw_pending ? (int)min(delta, 9UL) + '0' :
452 				"!."[!delta],
453 	       ticks_value, ticks_title,
454 	       rcu_dynticks_snap(rdp) & 0xfff,
455 	       rdp->dynticks_nesting, rdp->dynticks_nmi_nesting,
456 	       rdp->softirq_snap, kstat_softirqs_cpu(RCU_SOFTIRQ, cpu),
457 	       data_race(rcu_state.n_force_qs) - rcu_state.n_force_qs_gpstart,
458 	       fast_no_hz,
459 	       falsepositive ? " (false positive?)" : "");
460 }
461 
462 /* Complain about starvation of grace-period kthread.  */
rcu_check_gp_kthread_starvation(void)463 static void rcu_check_gp_kthread_starvation(void)
464 {
465 	int cpu;
466 	struct task_struct *gpk = rcu_state.gp_kthread;
467 	unsigned long j;
468 
469 	if (rcu_is_gp_kthread_starving(&j)) {
470 		cpu = gpk ? task_cpu(gpk) : -1;
471 		pr_err("%s kthread starved for %ld jiffies! g%ld f%#x %s(%d) ->state=%#x ->cpu=%d\n",
472 		       rcu_state.name, j,
473 		       (long)rcu_seq_current(&rcu_state.gp_seq),
474 		       data_race(READ_ONCE(rcu_state.gp_flags)),
475 		       gp_state_getname(rcu_state.gp_state),
476 		       data_race(READ_ONCE(rcu_state.gp_state)),
477 		       gpk ? data_race(READ_ONCE(gpk->__state)) : ~0, cpu);
478 		if (gpk) {
479 			pr_err("\tUnless %s kthread gets sufficient CPU time, OOM is now expected behavior.\n", rcu_state.name);
480 			pr_err("RCU grace-period kthread stack dump:\n");
481 			sched_show_task(gpk);
482 			if (cpu >= 0) {
483 				if (cpu_is_offline(cpu)) {
484 					pr_err("RCU GP kthread last ran on offline CPU %d.\n", cpu);
485 				} else  {
486 					pr_err("Stack dump where RCU GP kthread last ran:\n");
487 					if (!trigger_single_cpu_backtrace(cpu))
488 						dump_cpu_task(cpu);
489 				}
490 			}
491 			wake_up_process(gpk);
492 		}
493 	}
494 }
495 
496 /* Complain about missing wakeups from expired fqs wait timer */
rcu_check_gp_kthread_expired_fqs_timer(void)497 static void rcu_check_gp_kthread_expired_fqs_timer(void)
498 {
499 	struct task_struct *gpk = rcu_state.gp_kthread;
500 	short gp_state;
501 	unsigned long jiffies_fqs;
502 	int cpu;
503 
504 	/*
505 	 * Order reads of .gp_state and .jiffies_force_qs.
506 	 * Matching smp_wmb() is present in rcu_gp_fqs_loop().
507 	 */
508 	gp_state = smp_load_acquire(&rcu_state.gp_state);
509 	jiffies_fqs = READ_ONCE(rcu_state.jiffies_force_qs);
510 
511 	if (gp_state == RCU_GP_WAIT_FQS &&
512 	    time_after(jiffies, jiffies_fqs + RCU_STALL_MIGHT_MIN) &&
513 	    gpk && !READ_ONCE(gpk->on_rq)) {
514 		cpu = task_cpu(gpk);
515 		pr_err("%s kthread timer wakeup didn't happen for %ld jiffies! g%ld f%#x %s(%d) ->state=%#x\n",
516 		       rcu_state.name, (jiffies - jiffies_fqs),
517 		       (long)rcu_seq_current(&rcu_state.gp_seq),
518 		       data_race(rcu_state.gp_flags),
519 		       gp_state_getname(RCU_GP_WAIT_FQS), RCU_GP_WAIT_FQS,
520 		       data_race(READ_ONCE(gpk->__state)));
521 		pr_err("\tPossible timer handling issue on cpu=%d timer-softirq=%u\n",
522 		       cpu, kstat_softirqs_cpu(TIMER_SOFTIRQ, cpu));
523 	}
524 }
525 
print_other_cpu_stall(unsigned long gp_seq,unsigned long gps)526 static void print_other_cpu_stall(unsigned long gp_seq, unsigned long gps)
527 {
528 	int cpu;
529 	unsigned long flags;
530 	unsigned long gpa;
531 	unsigned long j;
532 	int ndetected = 0;
533 	struct rcu_node *rnp;
534 	long totqlen = 0;
535 
536 	lockdep_assert_irqs_disabled();
537 
538 	/* Kick and suppress, if so configured. */
539 	rcu_stall_kick_kthreads();
540 	if (rcu_stall_is_suppressed())
541 		return;
542 
543 	/*
544 	 * OK, time to rat on our buddy...
545 	 * See Documentation/RCU/stallwarn.rst for info on how to debug
546 	 * RCU CPU stall warnings.
547 	 */
548 	trace_rcu_stall_warning(rcu_state.name, TPS("StallDetected"));
549 	pr_err("INFO: %s detected stalls on CPUs/tasks:\n", rcu_state.name);
550 	rcu_for_each_leaf_node(rnp) {
551 		raw_spin_lock_irqsave_rcu_node(rnp, flags);
552 		if (rnp->qsmask != 0) {
553 			for_each_leaf_node_possible_cpu(rnp, cpu)
554 				if (rnp->qsmask & leaf_node_cpu_bit(rnp, cpu)) {
555 					print_cpu_stall_info(cpu);
556 					ndetected++;
557 				}
558 		}
559 		ndetected += rcu_print_task_stall(rnp, flags); // Releases rnp->lock.
560 		lockdep_assert_irqs_disabled();
561 	}
562 
563 	for_each_possible_cpu(cpu)
564 		totqlen += rcu_get_n_cbs_cpu(cpu);
565 	pr_cont("\t(detected by %d, t=%ld jiffies, g=%ld, q=%lu)\n",
566 	       smp_processor_id(), (long)(jiffies - gps),
567 	       (long)rcu_seq_current(&rcu_state.gp_seq), totqlen);
568 	if (ndetected) {
569 		rcu_dump_cpu_stacks();
570 
571 		/* Complain about tasks blocking the grace period. */
572 		rcu_for_each_leaf_node(rnp)
573 			rcu_print_detail_task_stall_rnp(rnp);
574 	} else {
575 		if (rcu_seq_current(&rcu_state.gp_seq) != gp_seq) {
576 			pr_err("INFO: Stall ended before state dump start\n");
577 		} else {
578 			j = jiffies;
579 			gpa = data_race(READ_ONCE(rcu_state.gp_activity));
580 			pr_err("All QSes seen, last %s kthread activity %ld (%ld-%ld), jiffies_till_next_fqs=%ld, root ->qsmask %#lx\n",
581 			       rcu_state.name, j - gpa, j, gpa,
582 			       data_race(READ_ONCE(jiffies_till_next_fqs)),
583 			       data_race(READ_ONCE(rcu_get_root()->qsmask)));
584 		}
585 	}
586 	/* Rewrite if needed in case of slow consoles. */
587 	if (ULONG_CMP_GE(jiffies, READ_ONCE(rcu_state.jiffies_stall)))
588 		WRITE_ONCE(rcu_state.jiffies_stall,
589 			   jiffies + 3 * rcu_jiffies_till_stall_check() + 3);
590 
591 	rcu_check_gp_kthread_expired_fqs_timer();
592 	rcu_check_gp_kthread_starvation();
593 
594 	panic_on_rcu_stall();
595 
596 	rcu_force_quiescent_state();  /* Kick them all. */
597 }
598 
print_cpu_stall(unsigned long gps)599 static void print_cpu_stall(unsigned long gps)
600 {
601 	int cpu;
602 	unsigned long flags;
603 	struct rcu_data *rdp = this_cpu_ptr(&rcu_data);
604 	struct rcu_node *rnp = rcu_get_root();
605 	long totqlen = 0;
606 
607 	lockdep_assert_irqs_disabled();
608 
609 	/* Kick and suppress, if so configured. */
610 	rcu_stall_kick_kthreads();
611 	if (rcu_stall_is_suppressed())
612 		return;
613 
614 	/*
615 	 * OK, time to rat on ourselves...
616 	 * See Documentation/RCU/stallwarn.rst for info on how to debug
617 	 * RCU CPU stall warnings.
618 	 */
619 	trace_rcu_stall_warning(rcu_state.name, TPS("SelfDetected"));
620 	pr_err("INFO: %s self-detected stall on CPU\n", rcu_state.name);
621 	raw_spin_lock_irqsave_rcu_node(rdp->mynode, flags);
622 	print_cpu_stall_info(smp_processor_id());
623 	raw_spin_unlock_irqrestore_rcu_node(rdp->mynode, flags);
624 	for_each_possible_cpu(cpu)
625 		totqlen += rcu_get_n_cbs_cpu(cpu);
626 	pr_cont("\t(t=%lu jiffies g=%ld q=%lu)\n",
627 		jiffies - gps,
628 		(long)rcu_seq_current(&rcu_state.gp_seq), totqlen);
629 
630 	rcu_check_gp_kthread_expired_fqs_timer();
631 	rcu_check_gp_kthread_starvation();
632 
633 	rcu_dump_cpu_stacks();
634 
635 	raw_spin_lock_irqsave_rcu_node(rnp, flags);
636 	/* Rewrite if needed in case of slow consoles. */
637 	if (ULONG_CMP_GE(jiffies, READ_ONCE(rcu_state.jiffies_stall)))
638 		WRITE_ONCE(rcu_state.jiffies_stall,
639 			   jiffies + 3 * rcu_jiffies_till_stall_check() + 3);
640 	raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
641 
642 	panic_on_rcu_stall();
643 
644 	/*
645 	 * Attempt to revive the RCU machinery by forcing a context switch.
646 	 *
647 	 * A context switch would normally allow the RCU state machine to make
648 	 * progress and it could be we're stuck in kernel space without context
649 	 * switches for an entirely unreasonable amount of time.
650 	 */
651 	set_tsk_need_resched(current);
652 	set_preempt_need_resched();
653 }
654 
check_cpu_stall(struct rcu_data * rdp)655 static void check_cpu_stall(struct rcu_data *rdp)
656 {
657 	bool didstall = false;
658 	unsigned long gs1;
659 	unsigned long gs2;
660 	unsigned long gps;
661 	unsigned long j;
662 	unsigned long jn;
663 	unsigned long js;
664 	struct rcu_node *rnp;
665 
666 	lockdep_assert_irqs_disabled();
667 	if ((rcu_stall_is_suppressed() && !READ_ONCE(rcu_kick_kthreads)) ||
668 	    !rcu_gp_in_progress())
669 		return;
670 	rcu_stall_kick_kthreads();
671 
672 	/*
673 	 * Check if it was requested (via rcu_cpu_stall_reset()) that the FQS
674 	 * loop has to set jiffies to ensure a non-stale jiffies value. This
675 	 * is required to have good jiffies value after coming out of long
676 	 * breaks of jiffies updates. Not doing so can cause false positives.
677 	 */
678 	if (READ_ONCE(rcu_state.nr_fqs_jiffies_stall) > 0)
679 		return;
680 
681 	j = jiffies;
682 
683 	/*
684 	 * Lots of memory barriers to reject false positives.
685 	 *
686 	 * The idea is to pick up rcu_state.gp_seq, then
687 	 * rcu_state.jiffies_stall, then rcu_state.gp_start, and finally
688 	 * another copy of rcu_state.gp_seq.  These values are updated in
689 	 * the opposite order with memory barriers (or equivalent) during
690 	 * grace-period initialization and cleanup.  Now, a false positive
691 	 * can occur if we get an new value of rcu_state.gp_start and a old
692 	 * value of rcu_state.jiffies_stall.  But given the memory barriers,
693 	 * the only way that this can happen is if one grace period ends
694 	 * and another starts between these two fetches.  This is detected
695 	 * by comparing the second fetch of rcu_state.gp_seq with the
696 	 * previous fetch from rcu_state.gp_seq.
697 	 *
698 	 * Given this check, comparisons of jiffies, rcu_state.jiffies_stall,
699 	 * and rcu_state.gp_start suffice to forestall false positives.
700 	 */
701 	gs1 = READ_ONCE(rcu_state.gp_seq);
702 	smp_rmb(); /* Pick up ->gp_seq first... */
703 	js = READ_ONCE(rcu_state.jiffies_stall);
704 	smp_rmb(); /* ...then ->jiffies_stall before the rest... */
705 	gps = READ_ONCE(rcu_state.gp_start);
706 	smp_rmb(); /* ...and finally ->gp_start before ->gp_seq again. */
707 	gs2 = READ_ONCE(rcu_state.gp_seq);
708 	if (gs1 != gs2 ||
709 	    ULONG_CMP_LT(j, js) ||
710 	    ULONG_CMP_GE(gps, js))
711 		return; /* No stall or GP completed since entering function. */
712 	rnp = rdp->mynode;
713 	jn = jiffies + ULONG_MAX / 2;
714 	if (rcu_gp_in_progress() &&
715 	    (READ_ONCE(rnp->qsmask) & rdp->grpmask) &&
716 	    cmpxchg(&rcu_state.jiffies_stall, js, jn) == js) {
717 
718 		/*
719 		 * If a virtual machine is stopped by the host it can look to
720 		 * the watchdog like an RCU stall. Check to see if the host
721 		 * stopped the vm.
722 		 */
723 		if (kvm_check_and_clear_guest_paused())
724 			return;
725 
726 		/* We haven't checked in, so go dump stack. */
727 		print_cpu_stall(gps);
728 		if (READ_ONCE(rcu_cpu_stall_ftrace_dump))
729 			rcu_ftrace_dump(DUMP_ALL);
730 		didstall = true;
731 
732 	} else if (rcu_gp_in_progress() &&
733 		   ULONG_CMP_GE(j, js + RCU_STALL_RAT_DELAY) &&
734 		   cmpxchg(&rcu_state.jiffies_stall, js, jn) == js) {
735 
736 		/*
737 		 * If a virtual machine is stopped by the host it can look to
738 		 * the watchdog like an RCU stall. Check to see if the host
739 		 * stopped the vm.
740 		 */
741 		if (kvm_check_and_clear_guest_paused())
742 			return;
743 
744 		/* They had a few time units to dump stack, so complain. */
745 		print_other_cpu_stall(gs2, gps);
746 		if (READ_ONCE(rcu_cpu_stall_ftrace_dump))
747 			rcu_ftrace_dump(DUMP_ALL);
748 		didstall = true;
749 	}
750 	if (didstall && READ_ONCE(rcu_state.jiffies_stall) == jn) {
751 		jn = jiffies + 3 * rcu_jiffies_till_stall_check() + 3;
752 		WRITE_ONCE(rcu_state.jiffies_stall, jn);
753 	}
754 }
755 
756 //////////////////////////////////////////////////////////////////////////////
757 //
758 // RCU forward-progress mechanisms, including of callback invocation.
759 
760 
761 /*
762  * Check to see if a failure to end RCU priority inversion was due to
763  * a CPU not passing through a quiescent state.  When this happens, there
764  * is nothing that RCU priority boosting can do to help, so we shouldn't
765  * count this as an RCU priority boosting failure.  A return of true says
766  * RCU priority boosting is to blame, and false says otherwise.  If false
767  * is returned, the first of the CPUs to blame is stored through cpup.
768  * If there was no CPU blocking the current grace period, but also nothing
769  * in need of being boosted, *cpup is set to -1.  This can happen in case
770  * of vCPU preemption while the last CPU is reporting its quiscent state,
771  * for example.
772  *
773  * If cpup is NULL, then a lockless quick check is carried out, suitable
774  * for high-rate usage.  On the other hand, if cpup is non-NULL, each
775  * rcu_node structure's ->lock is acquired, ruling out high-rate usage.
776  */
rcu_check_boost_fail(unsigned long gp_state,int * cpup)777 bool rcu_check_boost_fail(unsigned long gp_state, int *cpup)
778 {
779 	bool atb = false;
780 	int cpu;
781 	unsigned long flags;
782 	struct rcu_node *rnp;
783 
784 	rcu_for_each_leaf_node(rnp) {
785 		if (!cpup) {
786 			if (data_race(READ_ONCE(rnp->qsmask))) {
787 				return false;
788 			} else {
789 				if (READ_ONCE(rnp->gp_tasks))
790 					atb = true;
791 				continue;
792 			}
793 		}
794 		*cpup = -1;
795 		raw_spin_lock_irqsave_rcu_node(rnp, flags);
796 		if (rnp->gp_tasks)
797 			atb = true;
798 		if (!rnp->qsmask) {
799 			// No CPUs without quiescent states for this rnp.
800 			raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
801 			continue;
802 		}
803 		// Find the first holdout CPU.
804 		for_each_leaf_node_possible_cpu(rnp, cpu) {
805 			if (rnp->qsmask & (1UL << (cpu - rnp->grplo))) {
806 				raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
807 				*cpup = cpu;
808 				return false;
809 			}
810 		}
811 		raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
812 	}
813 	// Can't blame CPUs, so must blame RCU priority boosting.
814 	return atb;
815 }
816 EXPORT_SYMBOL_GPL(rcu_check_boost_fail);
817 
818 /*
819  * Show the state of the grace-period kthreads.
820  */
show_rcu_gp_kthreads(void)821 void show_rcu_gp_kthreads(void)
822 {
823 	unsigned long cbs = 0;
824 	int cpu;
825 	unsigned long j;
826 	unsigned long ja;
827 	unsigned long jr;
828 	unsigned long js;
829 	unsigned long jw;
830 	struct rcu_data *rdp;
831 	struct rcu_node *rnp;
832 	struct task_struct *t = READ_ONCE(rcu_state.gp_kthread);
833 
834 	j = jiffies;
835 	ja = j - data_race(READ_ONCE(rcu_state.gp_activity));
836 	jr = j - data_race(READ_ONCE(rcu_state.gp_req_activity));
837 	js = j - data_race(READ_ONCE(rcu_state.gp_start));
838 	jw = j - data_race(READ_ONCE(rcu_state.gp_wake_time));
839 	pr_info("%s: wait state: %s(%d) ->state: %#x ->rt_priority %u delta ->gp_start %lu ->gp_activity %lu ->gp_req_activity %lu ->gp_wake_time %lu ->gp_wake_seq %ld ->gp_seq %ld ->gp_seq_needed %ld ->gp_max %lu ->gp_flags %#x\n",
840 		rcu_state.name, gp_state_getname(rcu_state.gp_state),
841 		data_race(READ_ONCE(rcu_state.gp_state)),
842 		t ? data_race(READ_ONCE(t->__state)) : 0x1ffff, t ? t->rt_priority : 0xffU,
843 		js, ja, jr, jw, (long)data_race(READ_ONCE(rcu_state.gp_wake_seq)),
844 		(long)data_race(READ_ONCE(rcu_state.gp_seq)),
845 		(long)data_race(READ_ONCE(rcu_get_root()->gp_seq_needed)),
846 		data_race(READ_ONCE(rcu_state.gp_max)),
847 		data_race(READ_ONCE(rcu_state.gp_flags)));
848 	rcu_for_each_node_breadth_first(rnp) {
849 		if (ULONG_CMP_GE(READ_ONCE(rcu_state.gp_seq), READ_ONCE(rnp->gp_seq_needed)) &&
850 		    !data_race(READ_ONCE(rnp->qsmask)) && !data_race(READ_ONCE(rnp->boost_tasks)) &&
851 		    !data_race(READ_ONCE(rnp->exp_tasks)) && !data_race(READ_ONCE(rnp->gp_tasks)))
852 			continue;
853 		pr_info("\trcu_node %d:%d ->gp_seq %ld ->gp_seq_needed %ld ->qsmask %#lx %c%c%c%c ->n_boosts %ld\n",
854 			rnp->grplo, rnp->grphi,
855 			(long)data_race(READ_ONCE(rnp->gp_seq)),
856 			(long)data_race(READ_ONCE(rnp->gp_seq_needed)),
857 			data_race(READ_ONCE(rnp->qsmask)),
858 			".b"[!!data_race(READ_ONCE(rnp->boost_kthread_task))],
859 			".B"[!!data_race(READ_ONCE(rnp->boost_tasks))],
860 			".E"[!!data_race(READ_ONCE(rnp->exp_tasks))],
861 			".G"[!!data_race(READ_ONCE(rnp->gp_tasks))],
862 			data_race(READ_ONCE(rnp->n_boosts)));
863 		if (!rcu_is_leaf_node(rnp))
864 			continue;
865 		for_each_leaf_node_possible_cpu(rnp, cpu) {
866 			rdp = per_cpu_ptr(&rcu_data, cpu);
867 			if (READ_ONCE(rdp->gpwrap) ||
868 			    ULONG_CMP_GE(READ_ONCE(rcu_state.gp_seq),
869 					 READ_ONCE(rdp->gp_seq_needed)))
870 				continue;
871 			pr_info("\tcpu %d ->gp_seq_needed %ld\n",
872 				cpu, (long)data_race(READ_ONCE(rdp->gp_seq_needed)));
873 		}
874 	}
875 	for_each_possible_cpu(cpu) {
876 		rdp = per_cpu_ptr(&rcu_data, cpu);
877 		cbs += data_race(READ_ONCE(rdp->n_cbs_invoked));
878 		if (rcu_segcblist_is_offloaded(&rdp->cblist))
879 			show_rcu_nocb_state(rdp);
880 	}
881 	pr_info("RCU callbacks invoked since boot: %lu\n", cbs);
882 	show_rcu_tasks_gp_kthreads();
883 }
884 EXPORT_SYMBOL_GPL(show_rcu_gp_kthreads);
885 
886 /*
887  * This function checks for grace-period requests that fail to motivate
888  * RCU to come out of its idle mode.
889  */
rcu_check_gp_start_stall(struct rcu_node * rnp,struct rcu_data * rdp,const unsigned long gpssdelay)890 static void rcu_check_gp_start_stall(struct rcu_node *rnp, struct rcu_data *rdp,
891 				     const unsigned long gpssdelay)
892 {
893 	unsigned long flags;
894 	unsigned long j;
895 	struct rcu_node *rnp_root = rcu_get_root();
896 	static atomic_t warned = ATOMIC_INIT(0);
897 
898 	if (!IS_ENABLED(CONFIG_PROVE_RCU) || rcu_gp_in_progress() ||
899 	    ULONG_CMP_GE(READ_ONCE(rnp_root->gp_seq),
900 			 READ_ONCE(rnp_root->gp_seq_needed)) ||
901 	    !smp_load_acquire(&rcu_state.gp_kthread)) // Get stable kthread.
902 		return;
903 	j = jiffies; /* Expensive access, and in common case don't get here. */
904 	if (time_before(j, READ_ONCE(rcu_state.gp_req_activity) + gpssdelay) ||
905 	    time_before(j, READ_ONCE(rcu_state.gp_activity) + gpssdelay) ||
906 	    atomic_read(&warned))
907 		return;
908 
909 	raw_spin_lock_irqsave_rcu_node(rnp, flags);
910 	j = jiffies;
911 	if (rcu_gp_in_progress() ||
912 	    ULONG_CMP_GE(READ_ONCE(rnp_root->gp_seq),
913 			 READ_ONCE(rnp_root->gp_seq_needed)) ||
914 	    time_before(j, READ_ONCE(rcu_state.gp_req_activity) + gpssdelay) ||
915 	    time_before(j, READ_ONCE(rcu_state.gp_activity) + gpssdelay) ||
916 	    atomic_read(&warned)) {
917 		raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
918 		return;
919 	}
920 	/* Hold onto the leaf lock to make others see warned==1. */
921 
922 	if (rnp_root != rnp)
923 		raw_spin_lock_rcu_node(rnp_root); /* irqs already disabled. */
924 	j = jiffies;
925 	if (rcu_gp_in_progress() ||
926 	    ULONG_CMP_GE(READ_ONCE(rnp_root->gp_seq),
927 			 READ_ONCE(rnp_root->gp_seq_needed)) ||
928 	    time_before(j, READ_ONCE(rcu_state.gp_req_activity) + gpssdelay) ||
929 	    time_before(j, READ_ONCE(rcu_state.gp_activity) + gpssdelay) ||
930 	    atomic_xchg(&warned, 1)) {
931 		if (rnp_root != rnp)
932 			/* irqs remain disabled. */
933 			raw_spin_unlock_rcu_node(rnp_root);
934 		raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
935 		return;
936 	}
937 	WARN_ON(1);
938 	if (rnp_root != rnp)
939 		raw_spin_unlock_rcu_node(rnp_root);
940 	raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
941 	show_rcu_gp_kthreads();
942 }
943 
944 /*
945  * Do a forward-progress check for rcutorture.  This is normally invoked
946  * due to an OOM event.  The argument "j" gives the time period during
947  * which rcutorture would like progress to have been made.
948  */
rcu_fwd_progress_check(unsigned long j)949 void rcu_fwd_progress_check(unsigned long j)
950 {
951 	unsigned long cbs;
952 	int cpu;
953 	unsigned long max_cbs = 0;
954 	int max_cpu = -1;
955 	struct rcu_data *rdp;
956 
957 	if (rcu_gp_in_progress()) {
958 		pr_info("%s: GP age %lu jiffies\n",
959 			__func__, jiffies - data_race(READ_ONCE(rcu_state.gp_start)));
960 		show_rcu_gp_kthreads();
961 	} else {
962 		pr_info("%s: Last GP end %lu jiffies ago\n",
963 			__func__, jiffies - data_race(READ_ONCE(rcu_state.gp_end)));
964 		preempt_disable();
965 		rdp = this_cpu_ptr(&rcu_data);
966 		rcu_check_gp_start_stall(rdp->mynode, rdp, j);
967 		preempt_enable();
968 	}
969 	for_each_possible_cpu(cpu) {
970 		cbs = rcu_get_n_cbs_cpu(cpu);
971 		if (!cbs)
972 			continue;
973 		if (max_cpu < 0)
974 			pr_info("%s: callbacks", __func__);
975 		pr_cont(" %d: %lu", cpu, cbs);
976 		if (cbs <= max_cbs)
977 			continue;
978 		max_cbs = cbs;
979 		max_cpu = cpu;
980 	}
981 	if (max_cpu >= 0)
982 		pr_cont("\n");
983 }
984 EXPORT_SYMBOL_GPL(rcu_fwd_progress_check);
985 
986 /* Commandeer a sysrq key to dump RCU's tree. */
987 static bool sysrq_rcu;
988 module_param(sysrq_rcu, bool, 0444);
989 
990 /* Dump grace-period-request information due to commandeered sysrq. */
sysrq_show_rcu(int key)991 static void sysrq_show_rcu(int key)
992 {
993 	show_rcu_gp_kthreads();
994 }
995 
996 static const struct sysrq_key_op sysrq_rcudump_op = {
997 	.handler = sysrq_show_rcu,
998 	.help_msg = "show-rcu(y)",
999 	.action_msg = "Show RCU tree",
1000 	.enable_mask = SYSRQ_ENABLE_DUMP,
1001 };
1002 
rcu_sysrq_init(void)1003 static int __init rcu_sysrq_init(void)
1004 {
1005 	if (sysrq_rcu)
1006 		return register_sysrq_key('y', &sysrq_rcudump_op);
1007 	return 0;
1008 }
1009 early_initcall(rcu_sysrq_init);
1010