xref: /linux/kernel/rcu/tree_stall.h (revision e9ef810dfee7a2227da9d423aecb0ced35faddbe)
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/console.h>
11 #include <linux/kvm_para.h>
12 #include <linux/rcu_notifier.h>
13 #include <linux/smp.h>
14 
15 //////////////////////////////////////////////////////////////////////////////
16 //
17 // Controlling CPU stall warnings, including delay calculation.
18 
19 /* panic() on RCU Stall sysctl. */
20 static int sysctl_panic_on_rcu_stall __read_mostly;
21 static int sysctl_max_rcu_stall_to_panic __read_mostly;
22 
23 static const struct ctl_table rcu_stall_sysctl_table[] = {
24 	{
25 		.procname	= "panic_on_rcu_stall",
26 		.data		= &sysctl_panic_on_rcu_stall,
27 		.maxlen		= sizeof(sysctl_panic_on_rcu_stall),
28 		.mode		= 0644,
29 		.proc_handler	= proc_dointvec_minmax,
30 		.extra1		= SYSCTL_ZERO,
31 		.extra2		= SYSCTL_ONE,
32 	},
33 	{
34 		.procname	= "max_rcu_stall_to_panic",
35 		.data		= &sysctl_max_rcu_stall_to_panic,
36 		.maxlen		= sizeof(sysctl_max_rcu_stall_to_panic),
37 		.mode		= 0644,
38 		.proc_handler	= proc_dointvec_minmax,
39 		.extra1		= SYSCTL_ONE,
40 		.extra2		= SYSCTL_INT_MAX,
41 	},
42 };
43 
init_rcu_stall_sysctl(void)44 static int __init init_rcu_stall_sysctl(void)
45 {
46 	register_sysctl_init("kernel", rcu_stall_sysctl_table);
47 	return 0;
48 }
49 
50 subsys_initcall(init_rcu_stall_sysctl);
51 
52 #ifdef CONFIG_SYSFS
53 
54 static unsigned int rcu_stall_count;
55 
rcu_stall_count_show(struct kobject * kobj,struct kobj_attribute * attr,char * page)56 static ssize_t rcu_stall_count_show(struct kobject *kobj, struct kobj_attribute *attr,
57 				    char *page)
58 {
59 	return sysfs_emit(page, "%u\n", rcu_stall_count);
60 }
61 
62 static struct kobj_attribute rcu_stall_count_attr = __ATTR_RO(rcu_stall_count);
63 
kernel_rcu_stall_sysfs_init(void)64 static __init int kernel_rcu_stall_sysfs_init(void)
65 {
66 	sysfs_add_file_to_group(kernel_kobj, &rcu_stall_count_attr.attr, NULL);
67 	return 0;
68 }
69 
70 late_initcall(kernel_rcu_stall_sysfs_init);
71 
72 #endif // CONFIG_SYSFS
73 
74 #ifdef CONFIG_PROVE_RCU
75 #define RCU_STALL_DELAY_DELTA		(5 * HZ)
76 #else
77 #define RCU_STALL_DELAY_DELTA		0
78 #endif
79 #define RCU_STALL_MIGHT_DIV		8
80 #define RCU_STALL_MIGHT_MIN		(2 * HZ)
81 
rcu_exp_jiffies_till_stall_check(void)82 int rcu_exp_jiffies_till_stall_check(void)
83 {
84 	int cpu_stall_timeout = READ_ONCE(rcu_exp_cpu_stall_timeout);
85 	int exp_stall_delay_delta = 0;
86 	int till_stall_check;
87 
88 	// Zero says to use rcu_cpu_stall_timeout, but in milliseconds.
89 	if (!cpu_stall_timeout)
90 		cpu_stall_timeout = jiffies_to_msecs(rcu_jiffies_till_stall_check());
91 
92 	// Limit check must be consistent with the Kconfig limits for
93 	// CONFIG_RCU_EXP_CPU_STALL_TIMEOUT, so check the allowed range.
94 	// The minimum clamped value is "2UL", because at least one full
95 	// tick has to be guaranteed.
96 	till_stall_check = clamp(msecs_to_jiffies(cpu_stall_timeout), 2UL, 300UL * HZ);
97 
98 	if (cpu_stall_timeout && jiffies_to_msecs(till_stall_check) != cpu_stall_timeout)
99 		WRITE_ONCE(rcu_exp_cpu_stall_timeout, jiffies_to_msecs(till_stall_check));
100 
101 #ifdef CONFIG_PROVE_RCU
102 	/* Add extra ~25% out of till_stall_check. */
103 	exp_stall_delay_delta = ((till_stall_check * 25) / 100) + 1;
104 #endif
105 
106 	return till_stall_check + exp_stall_delay_delta;
107 }
108 EXPORT_SYMBOL_GPL(rcu_exp_jiffies_till_stall_check);
109 
110 /* Limit-check stall timeouts specified at boottime and runtime. */
rcu_jiffies_till_stall_check(void)111 int rcu_jiffies_till_stall_check(void)
112 {
113 	int till_stall_check = READ_ONCE(rcu_cpu_stall_timeout);
114 
115 	/*
116 	 * Limit check must be consistent with the Kconfig limits
117 	 * for CONFIG_RCU_CPU_STALL_TIMEOUT.
118 	 */
119 	if (till_stall_check < 3) {
120 		WRITE_ONCE(rcu_cpu_stall_timeout, 3);
121 		till_stall_check = 3;
122 	} else if (till_stall_check > 300) {
123 		WRITE_ONCE(rcu_cpu_stall_timeout, 300);
124 		till_stall_check = 300;
125 	}
126 	return till_stall_check * HZ + RCU_STALL_DELAY_DELTA;
127 }
128 EXPORT_SYMBOL_GPL(rcu_jiffies_till_stall_check);
129 
130 /* Don't do RCU CPU stall warnings during long sysrq printouts. */
rcu_sysrq_start(void)131 void rcu_sysrq_start(void)
132 {
133 	if (!rcu_cpu_stall_suppress)
134 		rcu_cpu_stall_suppress = 2;
135 }
136 
rcu_sysrq_end(void)137 void rcu_sysrq_end(void)
138 {
139 	if (rcu_cpu_stall_suppress == 2)
140 		rcu_cpu_stall_suppress = 0;
141 }
142 
143 /* Don't print RCU CPU stall warnings during a kernel panic. */
rcu_panic(struct notifier_block * this,unsigned long ev,void * ptr)144 static int rcu_panic(struct notifier_block *this, unsigned long ev, void *ptr)
145 {
146 	rcu_cpu_stall_suppress = 1;
147 	return NOTIFY_DONE;
148 }
149 
150 static struct notifier_block rcu_panic_block = {
151 	.notifier_call = rcu_panic,
152 };
153 
check_cpu_stall_init(void)154 static int __init check_cpu_stall_init(void)
155 {
156 	atomic_notifier_chain_register(&panic_notifier_list, &rcu_panic_block);
157 	return 0;
158 }
159 early_initcall(check_cpu_stall_init);
160 
161 /* If so specified via sysctl, panic, yielding cleaner stall-warning output. */
panic_on_rcu_stall(void)162 static void panic_on_rcu_stall(void)
163 {
164 	static int cpu_stall;
165 
166 	/*
167 	 * Attempt to kick out the BPF scheduler if it's installed and defer
168 	 * the panic to give the system a chance to recover.
169 	 */
170 	if (scx_rcu_cpu_stall())
171 		return;
172 
173 	if (++cpu_stall < sysctl_max_rcu_stall_to_panic)
174 		return;
175 
176 	if (sysctl_panic_on_rcu_stall)
177 		panic("RCU Stall\n");
178 }
179 
180 /**
181  * rcu_cpu_stall_reset - restart stall-warning timeout for current grace period
182  *
183  * To perform the reset request from the caller, disable stall detection until
184  * 3 fqs loops have passed. This is required to ensure a fresh jiffies is
185  * loaded.  It should be safe to do from the fqs loop as enough timer
186  * interrupts and context switches should have passed.
187  *
188  * The caller must disable hard irqs.
189  */
rcu_cpu_stall_reset(void)190 void rcu_cpu_stall_reset(void)
191 {
192 	WRITE_ONCE(rcu_state.nr_fqs_jiffies_stall, 3);
193 	WRITE_ONCE(rcu_state.jiffies_stall, ULONG_MAX);
194 }
195 
196 //////////////////////////////////////////////////////////////////////////////
197 //
198 // Interaction with RCU grace periods
199 
200 /* Start of new grace period, so record stall time (and forcing times). */
record_gp_stall_check_time(void)201 static void record_gp_stall_check_time(void)
202 {
203 	unsigned long j = jiffies;
204 	unsigned long j1;
205 
206 	WRITE_ONCE(rcu_state.gp_start, j);
207 	j1 = rcu_jiffies_till_stall_check();
208 	smp_mb(); // ->gp_start before ->jiffies_stall and caller's ->gp_seq.
209 	WRITE_ONCE(rcu_state.nr_fqs_jiffies_stall, 0);
210 	WRITE_ONCE(rcu_state.jiffies_stall, j + j1);
211 	rcu_state.jiffies_resched = j + j1 / 2;
212 	rcu_state.n_force_qs_gpstart = READ_ONCE(rcu_state.n_force_qs);
213 }
214 
215 /* Zero ->ticks_this_gp and snapshot the number of RCU softirq handlers. */
zero_cpu_stall_ticks(struct rcu_data * rdp)216 static void zero_cpu_stall_ticks(struct rcu_data *rdp)
217 {
218 	rdp->ticks_this_gp = 0;
219 	rdp->softirq_snap = kstat_softirqs_cpu(RCU_SOFTIRQ, smp_processor_id());
220 	WRITE_ONCE(rdp->last_fqs_resched, jiffies);
221 }
222 
223 /*
224  * If too much time has passed in the current grace period, and if
225  * so configured, go kick the relevant kthreads.
226  */
rcu_stall_kick_kthreads(void)227 static void rcu_stall_kick_kthreads(void)
228 {
229 	unsigned long j;
230 
231 	if (!READ_ONCE(rcu_kick_kthreads))
232 		return;
233 	j = READ_ONCE(rcu_state.jiffies_kick_kthreads);
234 	if (time_after(jiffies, j) && rcu_state.gp_kthread &&
235 	    (rcu_gp_in_progress() || READ_ONCE(rcu_state.gp_flags))) {
236 		WARN_ONCE(1, "Kicking %s grace-period kthread\n",
237 			  rcu_state.name);
238 		rcu_ftrace_dump(DUMP_ALL);
239 		wake_up_process(rcu_state.gp_kthread);
240 		WRITE_ONCE(rcu_state.jiffies_kick_kthreads, j + HZ);
241 	}
242 }
243 
244 /*
245  * Handler for the irq_work request posted about halfway into the RCU CPU
246  * stall timeout, and used to detect excessive irq disabling.  Set state
247  * appropriately, but just complain if there is unexpected state on entry.
248  */
rcu_iw_handler(struct irq_work * iwp)249 static void rcu_iw_handler(struct irq_work *iwp)
250 {
251 	struct rcu_data *rdp;
252 	struct rcu_node *rnp;
253 
254 	rdp = container_of(iwp, struct rcu_data, rcu_iw);
255 	rnp = rdp->mynode;
256 	raw_spin_lock_rcu_node(rnp);
257 	if (!WARN_ON_ONCE(!rdp->rcu_iw_pending)) {
258 		rdp->rcu_iw_gp_seq = rnp->gp_seq;
259 		rdp->rcu_iw_pending = false;
260 	}
261 	raw_spin_unlock_rcu_node(rnp);
262 }
263 
264 //////////////////////////////////////////////////////////////////////////////
265 //
266 // Printing RCU CPU stall warnings
267 
268 #ifdef CONFIG_PREEMPT_RCU
269 
270 /*
271  * Dump detailed information for all tasks blocking the current RCU
272  * grace period on the specified rcu_node structure.
273  */
rcu_print_detail_task_stall_rnp(struct rcu_node * rnp)274 static void rcu_print_detail_task_stall_rnp(struct rcu_node *rnp)
275 {
276 	unsigned long flags;
277 	struct task_struct *t;
278 
279 	raw_spin_lock_irqsave_rcu_node(rnp, flags);
280 	if (!rcu_preempt_blocked_readers_cgp(rnp)) {
281 		raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
282 		return;
283 	}
284 	t = list_entry(rnp->gp_tasks->prev,
285 		       struct task_struct, rcu_node_entry);
286 	list_for_each_entry_continue(t, &rnp->blkd_tasks, rcu_node_entry) {
287 		/*
288 		 * We could be printing a lot while holding a spinlock.
289 		 * Avoid triggering hard lockup.
290 		 */
291 		touch_nmi_watchdog();
292 		sched_show_task(t);
293 	}
294 	raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
295 }
296 
297 // Communicate task state back to the RCU CPU stall warning request.
298 struct rcu_stall_chk_rdr {
299 	int nesting;
300 	union rcu_special rs;
301 	bool on_blkd_list;
302 };
303 
304 /*
305  * Report out the state of a not-running task that is stalling the
306  * current RCU grace period.
307  */
check_slow_task(struct task_struct * t,void * arg)308 static int check_slow_task(struct task_struct *t, void *arg)
309 {
310 	struct rcu_stall_chk_rdr *rscrp = arg;
311 
312 	if (task_curr(t))
313 		return -EBUSY; // It is running, so decline to inspect it.
314 	rscrp->nesting = t->rcu_read_lock_nesting;
315 	rscrp->rs = t->rcu_read_unlock_special;
316 	rscrp->on_blkd_list = !list_empty(&t->rcu_node_entry);
317 	return 0;
318 }
319 
320 /*
321  * Scan the current list of tasks blocked within RCU read-side critical
322  * sections, printing out the tid of each of the first few of them.
323  */
rcu_print_task_stall(struct rcu_node * rnp,unsigned long flags)324 static int rcu_print_task_stall(struct rcu_node *rnp, unsigned long flags)
325 	__releases(rnp->lock)
326 {
327 	int i = 0;
328 	int ndetected = 0;
329 	struct rcu_stall_chk_rdr rscr;
330 	struct task_struct *t;
331 	struct task_struct *ts[8];
332 
333 	lockdep_assert_irqs_disabled();
334 	if (!rcu_preempt_blocked_readers_cgp(rnp)) {
335 		raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
336 		return 0;
337 	}
338 	pr_err("\tTasks blocked on level-%d rcu_node (CPUs %d-%d):",
339 	       rnp->level, rnp->grplo, rnp->grphi);
340 	t = list_entry(rnp->gp_tasks->prev,
341 		       struct task_struct, rcu_node_entry);
342 	list_for_each_entry_continue(t, &rnp->blkd_tasks, rcu_node_entry) {
343 		get_task_struct(t);
344 		ts[i++] = t;
345 		if (i >= ARRAY_SIZE(ts))
346 			break;
347 	}
348 	raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
349 	while (i) {
350 		t = ts[--i];
351 		if (task_call_func(t, check_slow_task, &rscr))
352 			pr_cont(" P%d", t->pid);
353 		else
354 			pr_cont(" P%d/%d:%c%c%c%c",
355 				t->pid, rscr.nesting,
356 				".b"[rscr.rs.b.blocked],
357 				".q"[rscr.rs.b.need_qs],
358 				".e"[rscr.rs.b.exp_hint],
359 				".l"[rscr.on_blkd_list]);
360 		lockdep_assert_irqs_disabled();
361 		put_task_struct(t);
362 		ndetected++;
363 	}
364 	pr_cont("\n");
365 	return ndetected;
366 }
367 
368 #else /* #ifdef CONFIG_PREEMPT_RCU */
369 
370 /*
371  * Because preemptible RCU does not exist, we never have to check for
372  * tasks blocked within RCU read-side critical sections.
373  */
rcu_print_detail_task_stall_rnp(struct rcu_node * rnp)374 static void rcu_print_detail_task_stall_rnp(struct rcu_node *rnp)
375 {
376 }
377 
378 /*
379  * Because preemptible RCU does not exist, we never have to check for
380  * tasks blocked within RCU read-side critical sections.
381  */
rcu_print_task_stall(struct rcu_node * rnp,unsigned long flags)382 static int rcu_print_task_stall(struct rcu_node *rnp, unsigned long flags)
383 	__releases(rnp->lock)
384 {
385 	raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
386 	return 0;
387 }
388 #endif /* #else #ifdef CONFIG_PREEMPT_RCU */
389 
390 /*
391  * Dump stacks of all tasks running on stalled CPUs.  First try using
392  * NMIs, but fall back to manual remote stack tracing on architectures
393  * that don't support NMI-based stack dumps.  The NMI-triggered stack
394  * traces are more accurate because they are printed by the target CPU.
395  */
rcu_dump_cpu_stacks(unsigned long gp_seq)396 static void rcu_dump_cpu_stacks(unsigned long gp_seq)
397 {
398 	int cpu;
399 	unsigned long flags;
400 	struct rcu_node *rnp;
401 
402 	rcu_for_each_leaf_node(rnp) {
403 		printk_deferred_enter();
404 		for_each_leaf_node_possible_cpu(rnp, cpu) {
405 			if (gp_seq != data_race(rcu_state.gp_seq)) {
406 				printk_deferred_exit();
407 				pr_err("INFO: Stall ended during stack backtracing.\n");
408 				return;
409 			}
410 			if (!(data_race(rnp->qsmask) & leaf_node_cpu_bit(rnp, cpu)))
411 				continue;
412 			raw_spin_lock_irqsave_rcu_node(rnp, flags);
413 			if (rnp->qsmask & leaf_node_cpu_bit(rnp, cpu)) {
414 				if (cpu_is_offline(cpu))
415 					pr_err("Offline CPU %d blocking current GP.\n", cpu);
416 				else
417 					dump_cpu_task(cpu);
418 			}
419 			raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
420 		}
421 		printk_deferred_exit();
422 	}
423 }
424 
425 static const char * const gp_state_names[] = {
426 	[RCU_GP_IDLE] = "RCU_GP_IDLE",
427 	[RCU_GP_WAIT_GPS] = "RCU_GP_WAIT_GPS",
428 	[RCU_GP_DONE_GPS] = "RCU_GP_DONE_GPS",
429 	[RCU_GP_ONOFF] = "RCU_GP_ONOFF",
430 	[RCU_GP_INIT] = "RCU_GP_INIT",
431 	[RCU_GP_WAIT_FQS] = "RCU_GP_WAIT_FQS",
432 	[RCU_GP_DOING_FQS] = "RCU_GP_DOING_FQS",
433 	[RCU_GP_CLEANUP] = "RCU_GP_CLEANUP",
434 	[RCU_GP_CLEANED] = "RCU_GP_CLEANED",
435 };
436 
437 /*
438  * Convert a ->gp_state value to a character string.
439  */
gp_state_getname(short gs)440 static const char *gp_state_getname(short gs)
441 {
442 	if (gs < 0 || gs >= ARRAY_SIZE(gp_state_names))
443 		return "???";
444 	return gp_state_names[gs];
445 }
446 
447 /* Is the RCU grace-period kthread being starved of CPU time? */
rcu_is_gp_kthread_starving(unsigned long * jp)448 static bool rcu_is_gp_kthread_starving(unsigned long *jp)
449 {
450 	unsigned long j = jiffies - READ_ONCE(rcu_state.gp_activity);
451 
452 	if (jp)
453 		*jp = j;
454 	return j > 2 * HZ;
455 }
456 
rcu_is_rcuc_kthread_starving(struct rcu_data * rdp,unsigned long * jp)457 static bool rcu_is_rcuc_kthread_starving(struct rcu_data *rdp, unsigned long *jp)
458 {
459 	int cpu;
460 	struct task_struct *rcuc;
461 	unsigned long j;
462 
463 	rcuc = rdp->rcu_cpu_kthread_task;
464 	if (!rcuc)
465 		return false;
466 
467 	cpu = task_cpu(rcuc);
468 	if (cpu_is_offline(cpu) || idle_cpu(cpu))
469 		return false;
470 
471 	j = jiffies - READ_ONCE(rdp->rcuc_activity);
472 
473 	if (jp)
474 		*jp = j;
475 	return j > 2 * HZ;
476 }
477 
print_cpu_stat_info(int cpu)478 static void print_cpu_stat_info(int cpu)
479 {
480 	struct rcu_snap_record rsr, *rsrp;
481 	struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
482 	struct kernel_cpustat *kcsp = &kcpustat_cpu(cpu);
483 
484 	if (!rcu_cpu_stall_cputime)
485 		return;
486 
487 	rsrp = &rdp->snap_record;
488 	if (rsrp->gp_seq != rdp->gp_seq)
489 		return;
490 
491 	rsr.cputime_irq     = kcpustat_field(kcsp, CPUTIME_IRQ, cpu);
492 	rsr.cputime_softirq = kcpustat_field(kcsp, CPUTIME_SOFTIRQ, cpu);
493 	rsr.cputime_system  = kcpustat_field(kcsp, CPUTIME_SYSTEM, cpu);
494 
495 	pr_err("\t         hardirqs   softirqs   csw/system\n");
496 	pr_err("\t number: %8lld %10d %12lld\n",
497 		kstat_cpu_irqs_sum(cpu) + arch_irq_stat_cpu(cpu) - rsrp->nr_hardirqs,
498 		kstat_cpu_softirqs_sum(cpu) - rsrp->nr_softirqs,
499 		nr_context_switches_cpu(cpu) - rsrp->nr_csw);
500 	pr_err("\tcputime: %8lld %10lld %12lld   ==> %d(ms)\n",
501 		div_u64(rsr.cputime_irq - rsrp->cputime_irq, NSEC_PER_MSEC),
502 		div_u64(rsr.cputime_softirq - rsrp->cputime_softirq, NSEC_PER_MSEC),
503 		div_u64(rsr.cputime_system - rsrp->cputime_system, NSEC_PER_MSEC),
504 		jiffies_to_msecs(jiffies - rsrp->jiffies));
505 }
506 
507 /*
508  * Print out diagnostic information for the specified stalled CPU.
509  *
510  * If the specified CPU is aware of the current RCU grace period, then
511  * print the number of scheduling clock interrupts the CPU has taken
512  * during the time that it has been aware.  Otherwise, print the number
513  * of RCU grace periods that this CPU is ignorant of, for example, "1"
514  * if the CPU was aware of the previous grace period.
515  *
516  * Also print out idle info.
517  */
print_cpu_stall_info(int cpu)518 static void print_cpu_stall_info(int cpu)
519 {
520 	unsigned long delta;
521 	bool falsepositive;
522 	struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
523 	char *ticks_title;
524 	unsigned long ticks_value;
525 	bool rcuc_starved;
526 	unsigned long j;
527 	char buf[32];
528 
529 	/*
530 	 * We could be printing a lot while holding a spinlock.  Avoid
531 	 * triggering hard lockup.
532 	 */
533 	touch_nmi_watchdog();
534 
535 	ticks_value = rcu_seq_ctr(rcu_state.gp_seq - rdp->gp_seq);
536 	if (ticks_value) {
537 		ticks_title = "GPs behind";
538 	} else {
539 		ticks_title = "ticks this GP";
540 		ticks_value = rdp->ticks_this_gp;
541 	}
542 	delta = rcu_seq_ctr(rdp->mynode->gp_seq - rdp->rcu_iw_gp_seq);
543 	falsepositive = rcu_is_gp_kthread_starving(NULL) &&
544 			rcu_watching_snap_in_eqs(ct_rcu_watching_cpu(cpu));
545 	rcuc_starved = rcu_is_rcuc_kthread_starving(rdp, &j);
546 	if (rcuc_starved)
547 		// Print signed value, as negative values indicate a probable bug.
548 		snprintf(buf, sizeof(buf), " rcuc=%ld jiffies(starved)", j);
549 	pr_err("\t%d-%c%c%c%c: (%lu %s) idle=%04x/%ld/%#lx softirq=%u/%u fqs=%ld%s%s\n",
550 	       cpu,
551 	       "O."[!!cpu_online(cpu)],
552 	       "o."[!!(rdp->grpmask & rdp->mynode->qsmaskinit)],
553 	       "N."[!!(rdp->grpmask & rdp->mynode->qsmaskinitnext)],
554 	       !IS_ENABLED(CONFIG_IRQ_WORK) ? '?' :
555 			rdp->rcu_iw_pending ? (int)min(delta, 9UL) + '0' :
556 				"!."[!delta],
557 	       ticks_value, ticks_title,
558 	       ct_rcu_watching_cpu(cpu) & 0xffff,
559 	       ct_nesting_cpu(cpu), ct_nmi_nesting_cpu(cpu),
560 	       rdp->softirq_snap, kstat_softirqs_cpu(RCU_SOFTIRQ, cpu),
561 	       data_race(rcu_state.n_force_qs) - rcu_state.n_force_qs_gpstart,
562 	       rcuc_starved ? buf : "",
563 	       falsepositive ? " (false positive?)" : "");
564 
565 	print_cpu_stat_info(cpu);
566 }
567 
568 /* Complain about starvation of grace-period kthread.  */
rcu_check_gp_kthread_starvation(void)569 static void rcu_check_gp_kthread_starvation(void)
570 {
571 	int cpu;
572 	struct task_struct *gpk = rcu_state.gp_kthread;
573 	unsigned long j;
574 
575 	if (rcu_is_gp_kthread_starving(&j)) {
576 		cpu = gpk ? task_cpu(gpk) : -1;
577 		pr_err("%s kthread starved for %ld jiffies! g%ld f%#x %s(%d) ->state=%#x ->cpu=%d\n",
578 		       rcu_state.name, j,
579 		       (long)rcu_seq_current(&rcu_state.gp_seq),
580 		       data_race(READ_ONCE(rcu_state.gp_flags)),
581 		       gp_state_getname(rcu_state.gp_state),
582 		       data_race(READ_ONCE(rcu_state.gp_state)),
583 		       gpk ? data_race(READ_ONCE(gpk->__state)) : ~0, cpu);
584 		if (gpk) {
585 			struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
586 
587 			pr_err("\tUnless %s kthread gets sufficient CPU time, OOM is now expected behavior.\n", rcu_state.name);
588 			pr_err("RCU grace-period kthread stack dump:\n");
589 			sched_show_task(gpk);
590 			if (cpu_is_offline(cpu)) {
591 				pr_err("RCU GP kthread last ran on offline CPU %d.\n", cpu);
592 			} else if (!(data_race(READ_ONCE(rdp->mynode->qsmask)) & rdp->grpmask)) {
593 				pr_err("Stack dump where RCU GP kthread last ran:\n");
594 				dump_cpu_task(cpu);
595 			}
596 			wake_up_process(gpk);
597 		}
598 	}
599 }
600 
601 /* Complain about missing wakeups from expired fqs wait timer */
rcu_check_gp_kthread_expired_fqs_timer(void)602 static void rcu_check_gp_kthread_expired_fqs_timer(void)
603 {
604 	struct task_struct *gpk = rcu_state.gp_kthread;
605 	short gp_state;
606 	unsigned long jiffies_fqs;
607 	int cpu;
608 
609 	/*
610 	 * Order reads of .gp_state and .jiffies_force_qs.
611 	 * Matching smp_wmb() is present in rcu_gp_fqs_loop().
612 	 */
613 	gp_state = smp_load_acquire(&rcu_state.gp_state);
614 	jiffies_fqs = READ_ONCE(rcu_state.jiffies_force_qs);
615 
616 	if (gp_state == RCU_GP_WAIT_FQS &&
617 	    time_after(jiffies, jiffies_fqs + RCU_STALL_MIGHT_MIN) &&
618 	    gpk && !READ_ONCE(gpk->on_rq)) {
619 		cpu = task_cpu(gpk);
620 		pr_err("%s kthread timer wakeup didn't happen for %ld jiffies! g%ld f%#x %s(%d) ->state=%#x\n",
621 		       rcu_state.name, (jiffies - jiffies_fqs),
622 		       (long)rcu_seq_current(&rcu_state.gp_seq),
623 		       data_race(READ_ONCE(rcu_state.gp_flags)), // Diagnostic read
624 		       gp_state_getname(RCU_GP_WAIT_FQS), RCU_GP_WAIT_FQS,
625 		       data_race(READ_ONCE(gpk->__state)));
626 		pr_err("\tPossible timer handling issue on cpu=%d timer-softirq=%u\n",
627 		       cpu, kstat_softirqs_cpu(TIMER_SOFTIRQ, cpu));
628 	}
629 }
630 
print_other_cpu_stall(unsigned long gp_seq,unsigned long gps)631 static void print_other_cpu_stall(unsigned long gp_seq, unsigned long gps)
632 {
633 	int cpu;
634 	unsigned long flags;
635 	unsigned long gpa;
636 	unsigned long j;
637 	int ndetected = 0;
638 	struct rcu_node *rnp;
639 	long totqlen = 0;
640 
641 	lockdep_assert_irqs_disabled();
642 
643 	/* Kick and suppress, if so configured. */
644 	rcu_stall_kick_kthreads();
645 	if (rcu_stall_is_suppressed())
646 		return;
647 
648 	nbcon_cpu_emergency_enter();
649 
650 	/*
651 	 * OK, time to rat on our buddy...
652 	 * See Documentation/RCU/stallwarn.rst for info on how to debug
653 	 * RCU CPU stall warnings.
654 	 */
655 	trace_rcu_stall_warning(rcu_state.name, TPS("StallDetected"));
656 	pr_err("INFO: %s detected stalls on CPUs/tasks:\n", rcu_state.name);
657 	rcu_for_each_leaf_node(rnp) {
658 		raw_spin_lock_irqsave_rcu_node(rnp, flags);
659 		if (rnp->qsmask != 0) {
660 			for_each_leaf_node_possible_cpu(rnp, cpu)
661 				if (rnp->qsmask & leaf_node_cpu_bit(rnp, cpu)) {
662 					print_cpu_stall_info(cpu);
663 					ndetected++;
664 				}
665 		}
666 		ndetected += rcu_print_task_stall(rnp, flags); // Releases rnp->lock.
667 		lockdep_assert_irqs_disabled();
668 	}
669 
670 	for_each_possible_cpu(cpu)
671 		totqlen += rcu_get_n_cbs_cpu(cpu);
672 	pr_err("\t(detected by %d, t=%ld jiffies, g=%ld, q=%lu ncpus=%d)\n",
673 	       smp_processor_id(), (long)(jiffies - gps),
674 	       (long)rcu_seq_current(&rcu_state.gp_seq), totqlen,
675 	       data_race(rcu_state.n_online_cpus)); // Diagnostic read
676 	if (ndetected) {
677 		rcu_dump_cpu_stacks(gp_seq);
678 
679 		/* Complain about tasks blocking the grace period. */
680 		rcu_for_each_leaf_node(rnp)
681 			rcu_print_detail_task_stall_rnp(rnp);
682 	} else {
683 		if (rcu_seq_current(&rcu_state.gp_seq) != gp_seq) {
684 			pr_err("INFO: Stall ended before state dump start\n");
685 		} else {
686 			j = jiffies;
687 			gpa = data_race(READ_ONCE(rcu_state.gp_activity));
688 			pr_err("All QSes seen, last %s kthread activity %ld (%ld-%ld), jiffies_till_next_fqs=%ld, root ->qsmask %#lx\n",
689 			       rcu_state.name, j - gpa, j, gpa,
690 			       data_race(READ_ONCE(jiffies_till_next_fqs)),
691 			       data_race(READ_ONCE(rcu_get_root()->qsmask)));
692 		}
693 	}
694 	/* Rewrite if needed in case of slow consoles. */
695 	if (ULONG_CMP_GE(jiffies, READ_ONCE(rcu_state.jiffies_stall)))
696 		WRITE_ONCE(rcu_state.jiffies_stall,
697 			   jiffies + 3 * rcu_jiffies_till_stall_check() + 3);
698 
699 	rcu_check_gp_kthread_expired_fqs_timer();
700 	rcu_check_gp_kthread_starvation();
701 
702 	nbcon_cpu_emergency_exit();
703 
704 	panic_on_rcu_stall();
705 
706 	rcu_force_quiescent_state();  /* Kick them all. */
707 }
708 
print_cpu_stall(unsigned long gp_seq,unsigned long gps)709 static void print_cpu_stall(unsigned long gp_seq, unsigned long gps)
710 {
711 	int cpu;
712 	unsigned long flags;
713 	struct rcu_data *rdp = this_cpu_ptr(&rcu_data);
714 	struct rcu_node *rnp = rcu_get_root();
715 	long totqlen = 0;
716 
717 	lockdep_assert_irqs_disabled();
718 
719 	/* Kick and suppress, if so configured. */
720 	rcu_stall_kick_kthreads();
721 	if (rcu_stall_is_suppressed())
722 		return;
723 
724 	nbcon_cpu_emergency_enter();
725 
726 	/*
727 	 * OK, time to rat on ourselves...
728 	 * See Documentation/RCU/stallwarn.rst for info on how to debug
729 	 * RCU CPU stall warnings.
730 	 */
731 	trace_rcu_stall_warning(rcu_state.name, TPS("SelfDetected"));
732 	pr_err("INFO: %s self-detected stall on CPU\n", rcu_state.name);
733 	raw_spin_lock_irqsave_rcu_node(rdp->mynode, flags);
734 	print_cpu_stall_info(smp_processor_id());
735 	raw_spin_unlock_irqrestore_rcu_node(rdp->mynode, flags);
736 	for_each_possible_cpu(cpu)
737 		totqlen += rcu_get_n_cbs_cpu(cpu);
738 	pr_err("\t(t=%lu jiffies g=%ld q=%lu ncpus=%d)\n",
739 		jiffies - gps,
740 		(long)rcu_seq_current(&rcu_state.gp_seq), totqlen,
741 		data_race(rcu_state.n_online_cpus)); // Diagnostic read
742 
743 	rcu_check_gp_kthread_expired_fqs_timer();
744 	rcu_check_gp_kthread_starvation();
745 
746 	rcu_dump_cpu_stacks(gp_seq);
747 
748 	raw_spin_lock_irqsave_rcu_node(rnp, flags);
749 	/* Rewrite if needed in case of slow consoles. */
750 	if (ULONG_CMP_GE(jiffies, READ_ONCE(rcu_state.jiffies_stall)))
751 		WRITE_ONCE(rcu_state.jiffies_stall,
752 			   jiffies + 3 * rcu_jiffies_till_stall_check() + 3);
753 	raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
754 
755 	nbcon_cpu_emergency_exit();
756 
757 	panic_on_rcu_stall();
758 
759 	/*
760 	 * Attempt to revive the RCU machinery by forcing a context switch.
761 	 *
762 	 * A context switch would normally allow the RCU state machine to make
763 	 * progress and it could be we're stuck in kernel space without context
764 	 * switches for an entirely unreasonable amount of time.
765 	 */
766 	set_tsk_need_resched(current);
767 	set_preempt_need_resched();
768 }
769 
770 static bool csd_lock_suppress_rcu_stall;
771 module_param(csd_lock_suppress_rcu_stall, bool, 0644);
772 
check_cpu_stall(struct rcu_data * rdp)773 static void check_cpu_stall(struct rcu_data *rdp)
774 {
775 	bool self_detected;
776 	unsigned long gs1;
777 	unsigned long gs2;
778 	unsigned long gps;
779 	unsigned long j;
780 	unsigned long jn;
781 	unsigned long js;
782 	struct rcu_node *rnp;
783 
784 	lockdep_assert_irqs_disabled();
785 	if ((rcu_stall_is_suppressed() && !READ_ONCE(rcu_kick_kthreads)) ||
786 	    !rcu_gp_in_progress())
787 		return;
788 	rcu_stall_kick_kthreads();
789 
790 	/*
791 	 * Check if it was requested (via rcu_cpu_stall_reset()) that the FQS
792 	 * loop has to set jiffies to ensure a non-stale jiffies value. This
793 	 * is required to have good jiffies value after coming out of long
794 	 * breaks of jiffies updates. Not doing so can cause false positives.
795 	 */
796 	if (READ_ONCE(rcu_state.nr_fqs_jiffies_stall) > 0)
797 		return;
798 
799 	j = jiffies;
800 
801 	/*
802 	 * Lots of memory barriers to reject false positives.
803 	 *
804 	 * The idea is to pick up rcu_state.gp_seq, then
805 	 * rcu_state.jiffies_stall, then rcu_state.gp_start, and finally
806 	 * another copy of rcu_state.gp_seq.  These values are updated in
807 	 * the opposite order with memory barriers (or equivalent) during
808 	 * grace-period initialization and cleanup.  Now, a false positive
809 	 * can occur if we get an new value of rcu_state.gp_start and a old
810 	 * value of rcu_state.jiffies_stall.  But given the memory barriers,
811 	 * the only way that this can happen is if one grace period ends
812 	 * and another starts between these two fetches.  This is detected
813 	 * by comparing the second fetch of rcu_state.gp_seq with the
814 	 * previous fetch from rcu_state.gp_seq.
815 	 *
816 	 * Given this check, comparisons of jiffies, rcu_state.jiffies_stall,
817 	 * and rcu_state.gp_start suffice to forestall false positives.
818 	 */
819 	gs1 = READ_ONCE(rcu_state.gp_seq);
820 	smp_rmb(); /* Pick up ->gp_seq first... */
821 	js = READ_ONCE(rcu_state.jiffies_stall);
822 	smp_rmb(); /* ...then ->jiffies_stall before the rest... */
823 	gps = READ_ONCE(rcu_state.gp_start);
824 	smp_rmb(); /* ...and finally ->gp_start before ->gp_seq again. */
825 	gs2 = READ_ONCE(rcu_state.gp_seq);
826 	if (gs1 != gs2 ||
827 	    ULONG_CMP_LT(j, js) ||
828 	    ULONG_CMP_GE(gps, js) ||
829 	    !rcu_seq_state(gs2))
830 		return; /* No stall or GP completed since entering function. */
831 	rnp = rdp->mynode;
832 	jn = jiffies + ULONG_MAX / 2;
833 	self_detected = READ_ONCE(rnp->qsmask) & rdp->grpmask;
834 	if (rcu_gp_in_progress() &&
835 	    (self_detected || ULONG_CMP_GE(j, js + RCU_STALL_RAT_DELAY)) &&
836 	    cmpxchg(&rcu_state.jiffies_stall, js, jn) == js) {
837 		/*
838 		 * If a virtual machine is stopped by the host it can look to
839 		 * the watchdog like an RCU stall. Check to see if the host
840 		 * stopped the vm.
841 		 */
842 		if (kvm_check_and_clear_guest_paused())
843 			return;
844 
845 #ifdef CONFIG_SYSFS
846 		++rcu_stall_count;
847 #endif
848 
849 		rcu_stall_notifier_call_chain(RCU_STALL_NOTIFY_NORM, (void *)j - gps);
850 		if (READ_ONCE(csd_lock_suppress_rcu_stall) && csd_lock_is_stuck()) {
851 			pr_err("INFO: %s detected stall, but suppressed full report due to a stuck CSD-lock.\n", rcu_state.name);
852 		} else if (self_detected) {
853 			/* We haven't checked in, so go dump stack. */
854 			print_cpu_stall(gs2, gps);
855 		} else {
856 			/* They had a few time units to dump stack, so complain. */
857 			print_other_cpu_stall(gs2, gps);
858 		}
859 
860 		if (READ_ONCE(rcu_cpu_stall_ftrace_dump))
861 			rcu_ftrace_dump(DUMP_ALL);
862 
863 		if (READ_ONCE(rcu_state.jiffies_stall) == jn) {
864 			jn = jiffies + 3 * rcu_jiffies_till_stall_check() + 3;
865 			WRITE_ONCE(rcu_state.jiffies_stall, jn);
866 		}
867 	}
868 }
869 
870 //////////////////////////////////////////////////////////////////////////////
871 //
872 // RCU forward-progress mechanisms, including for callback invocation.
873 
874 
875 /*
876  * Check to see if a failure to end RCU priority inversion was due to
877  * a CPU not passing through a quiescent state.  When this happens, there
878  * is nothing that RCU priority boosting can do to help, so we shouldn't
879  * count this as an RCU priority boosting failure.  A return of true says
880  * RCU priority boosting is to blame, and false says otherwise.  If false
881  * is returned, the first of the CPUs to blame is stored through cpup.
882  * If there was no CPU blocking the current grace period, but also nothing
883  * in need of being boosted, *cpup is set to -1.  This can happen in case
884  * of vCPU preemption while the last CPU is reporting its quiscent state,
885  * for example.
886  *
887  * If cpup is NULL, then a lockless quick check is carried out, suitable
888  * for high-rate usage.  On the other hand, if cpup is non-NULL, each
889  * rcu_node structure's ->lock is acquired, ruling out high-rate usage.
890  */
rcu_check_boost_fail(unsigned long gp_state,int * cpup)891 bool rcu_check_boost_fail(unsigned long gp_state, int *cpup)
892 {
893 	bool atb = false;
894 	int cpu;
895 	unsigned long flags;
896 	struct rcu_node *rnp;
897 
898 	rcu_for_each_leaf_node(rnp) {
899 		if (!cpup) {
900 			if (data_race(READ_ONCE(rnp->qsmask))) {
901 				return false;
902 			} else {
903 				if (READ_ONCE(rnp->gp_tasks))
904 					atb = true;
905 				continue;
906 			}
907 		}
908 		*cpup = -1;
909 		raw_spin_lock_irqsave_rcu_node(rnp, flags);
910 		if (rnp->gp_tasks)
911 			atb = true;
912 		if (!rnp->qsmask) {
913 			// No CPUs without quiescent states for this rnp.
914 			raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
915 			continue;
916 		}
917 		// Find the first holdout CPU.
918 		for_each_leaf_node_possible_cpu(rnp, cpu) {
919 			if (rnp->qsmask & (1UL << (cpu - rnp->grplo))) {
920 				raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
921 				*cpup = cpu;
922 				return false;
923 			}
924 		}
925 		raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
926 	}
927 	// Can't blame CPUs, so must blame RCU priority boosting.
928 	return atb;
929 }
930 EXPORT_SYMBOL_GPL(rcu_check_boost_fail);
931 
932 /*
933  * Show the state of the grace-period kthreads.
934  */
show_rcu_gp_kthreads(void)935 void show_rcu_gp_kthreads(void)
936 {
937 	unsigned long cbs = 0;
938 	int cpu;
939 	unsigned long j;
940 	unsigned long ja;
941 	unsigned long jr;
942 	unsigned long js;
943 	unsigned long jw;
944 	struct rcu_data *rdp;
945 	struct rcu_node *rnp;
946 	struct task_struct *t = READ_ONCE(rcu_state.gp_kthread);
947 
948 	j = jiffies;
949 	ja = j - data_race(READ_ONCE(rcu_state.gp_activity));
950 	jr = j - data_race(READ_ONCE(rcu_state.gp_req_activity));
951 	js = j - data_race(READ_ONCE(rcu_state.gp_start));
952 	jw = j - data_race(READ_ONCE(rcu_state.gp_wake_time));
953 	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",
954 		rcu_state.name, gp_state_getname(rcu_state.gp_state),
955 		data_race(READ_ONCE(rcu_state.gp_state)),
956 		t ? data_race(READ_ONCE(t->__state)) : 0x1ffff, t ? t->rt_priority : 0xffU,
957 		js, ja, jr, jw, (long)data_race(READ_ONCE(rcu_state.gp_wake_seq)),
958 		(long)data_race(READ_ONCE(rcu_state.gp_seq)),
959 		(long)data_race(READ_ONCE(rcu_get_root()->gp_seq_needed)),
960 		data_race(READ_ONCE(rcu_state.gp_max)),
961 		data_race(READ_ONCE(rcu_state.gp_flags)));
962 	rcu_for_each_node_breadth_first(rnp) {
963 		if (ULONG_CMP_GE(READ_ONCE(rcu_state.gp_seq), READ_ONCE(rnp->gp_seq_needed)) &&
964 		    !data_race(READ_ONCE(rnp->qsmask)) && !data_race(READ_ONCE(rnp->boost_tasks)) &&
965 		    !data_race(READ_ONCE(rnp->exp_tasks)) && !data_race(READ_ONCE(rnp->gp_tasks)))
966 			continue;
967 		pr_info("\trcu_node %d:%d ->gp_seq %ld ->gp_seq_needed %ld ->qsmask %#lx %c%c%c%c ->n_boosts %ld\n",
968 			rnp->grplo, rnp->grphi,
969 			(long)data_race(READ_ONCE(rnp->gp_seq)),
970 			(long)data_race(READ_ONCE(rnp->gp_seq_needed)),
971 			data_race(READ_ONCE(rnp->qsmask)),
972 			".b"[!!data_race(READ_ONCE(rnp->boost_kthread_task))],
973 			".B"[!!data_race(READ_ONCE(rnp->boost_tasks))],
974 			".E"[!!data_race(READ_ONCE(rnp->exp_tasks))],
975 			".G"[!!data_race(READ_ONCE(rnp->gp_tasks))],
976 			data_race(READ_ONCE(rnp->n_boosts)));
977 		if (!rcu_is_leaf_node(rnp))
978 			continue;
979 		for_each_leaf_node_possible_cpu(rnp, cpu) {
980 			rdp = per_cpu_ptr(&rcu_data, cpu);
981 			if (READ_ONCE(rdp->gpwrap) ||
982 			    ULONG_CMP_GE(READ_ONCE(rcu_state.gp_seq),
983 					 READ_ONCE(rdp->gp_seq_needed)))
984 				continue;
985 			pr_info("\tcpu %d ->gp_seq_needed %ld\n",
986 				cpu, (long)data_race(READ_ONCE(rdp->gp_seq_needed)));
987 		}
988 	}
989 	for_each_possible_cpu(cpu) {
990 		rdp = per_cpu_ptr(&rcu_data, cpu);
991 		cbs += data_race(READ_ONCE(rdp->n_cbs_invoked));
992 		show_rcu_nocb_state(rdp);
993 	}
994 	pr_info("RCU callbacks invoked since boot: %lu\n", cbs);
995 	show_rcu_tasks_gp_kthreads();
996 }
997 EXPORT_SYMBOL_GPL(show_rcu_gp_kthreads);
998 
999 /*
1000  * This function checks for grace-period requests that fail to motivate
1001  * RCU to come out of its idle mode.
1002  */
rcu_check_gp_start_stall(struct rcu_node * rnp,struct rcu_data * rdp,const unsigned long gpssdelay)1003 static void rcu_check_gp_start_stall(struct rcu_node *rnp, struct rcu_data *rdp,
1004 				     const unsigned long gpssdelay)
1005 {
1006 	unsigned long flags;
1007 	unsigned long j;
1008 	struct rcu_node *rnp_root = rcu_get_root();
1009 	static atomic_t warned = ATOMIC_INIT(0);
1010 
1011 	if (!IS_ENABLED(CONFIG_PROVE_RCU) || rcu_gp_in_progress() ||
1012 	    ULONG_CMP_GE(READ_ONCE(rnp_root->gp_seq),
1013 			 READ_ONCE(rnp_root->gp_seq_needed)) ||
1014 	    !smp_load_acquire(&rcu_state.gp_kthread)) // Get stable kthread.
1015 		return;
1016 	j = jiffies; /* Expensive access, and in common case don't get here. */
1017 	if (time_before(j, READ_ONCE(rcu_state.gp_req_activity) + gpssdelay) ||
1018 	    time_before(j, READ_ONCE(rcu_state.gp_activity) + gpssdelay) ||
1019 	    atomic_read(&warned))
1020 		return;
1021 
1022 	raw_spin_lock_irqsave_rcu_node(rnp, flags);
1023 	j = jiffies;
1024 	if (rcu_gp_in_progress() ||
1025 	    ULONG_CMP_GE(READ_ONCE(rnp_root->gp_seq),
1026 			 READ_ONCE(rnp_root->gp_seq_needed)) ||
1027 	    time_before(j, READ_ONCE(rcu_state.gp_req_activity) + gpssdelay) ||
1028 	    time_before(j, READ_ONCE(rcu_state.gp_activity) + gpssdelay) ||
1029 	    atomic_read(&warned)) {
1030 		raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
1031 		return;
1032 	}
1033 	/* Hold onto the leaf lock to make others see warned==1. */
1034 
1035 	if (rnp_root != rnp)
1036 		raw_spin_lock_rcu_node(rnp_root); /* irqs already disabled. */
1037 	j = jiffies;
1038 	if (rcu_gp_in_progress() ||
1039 	    ULONG_CMP_GE(READ_ONCE(rnp_root->gp_seq),
1040 			 READ_ONCE(rnp_root->gp_seq_needed)) ||
1041 	    time_before(j, READ_ONCE(rcu_state.gp_req_activity) + gpssdelay) ||
1042 	    time_before(j, READ_ONCE(rcu_state.gp_activity) + gpssdelay) ||
1043 	    atomic_xchg(&warned, 1)) {
1044 		if (rnp_root != rnp)
1045 			/* irqs remain disabled. */
1046 			raw_spin_unlock_rcu_node(rnp_root);
1047 		raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
1048 		return;
1049 	}
1050 	WARN_ON(1);
1051 	if (rnp_root != rnp)
1052 		raw_spin_unlock_rcu_node(rnp_root);
1053 	raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
1054 	show_rcu_gp_kthreads();
1055 }
1056 
1057 /*
1058  * Do a forward-progress check for rcutorture.  This is normally invoked
1059  * due to an OOM event.  The argument "j" gives the time period during
1060  * which rcutorture would like progress to have been made.
1061  */
rcu_fwd_progress_check(unsigned long j)1062 void rcu_fwd_progress_check(unsigned long j)
1063 {
1064 	unsigned long cbs;
1065 	int cpu;
1066 	unsigned long max_cbs = 0;
1067 	int max_cpu = -1;
1068 	struct rcu_data *rdp;
1069 
1070 	if (rcu_gp_in_progress()) {
1071 		pr_info("%s: GP age %lu jiffies\n",
1072 			__func__, jiffies - data_race(READ_ONCE(rcu_state.gp_start)));
1073 		show_rcu_gp_kthreads();
1074 	} else {
1075 		pr_info("%s: Last GP end %lu jiffies ago\n",
1076 			__func__, jiffies - data_race(READ_ONCE(rcu_state.gp_end)));
1077 		preempt_disable();
1078 		rdp = this_cpu_ptr(&rcu_data);
1079 		rcu_check_gp_start_stall(rdp->mynode, rdp, j);
1080 		preempt_enable();
1081 	}
1082 	for_each_possible_cpu(cpu) {
1083 		cbs = rcu_get_n_cbs_cpu(cpu);
1084 		if (!cbs)
1085 			continue;
1086 		if (max_cpu < 0)
1087 			pr_info("%s: callbacks", __func__);
1088 		pr_cont(" %d: %lu", cpu, cbs);
1089 		if (cbs <= max_cbs)
1090 			continue;
1091 		max_cbs = cbs;
1092 		max_cpu = cpu;
1093 	}
1094 	if (max_cpu >= 0)
1095 		pr_cont("\n");
1096 }
1097 EXPORT_SYMBOL_GPL(rcu_fwd_progress_check);
1098 
1099 /* Commandeer a sysrq key to dump RCU's tree. */
1100 static bool sysrq_rcu;
1101 module_param(sysrq_rcu, bool, 0444);
1102 
1103 /* Dump grace-period-request information due to commandeered sysrq. */
sysrq_show_rcu(u8 key)1104 static void sysrq_show_rcu(u8 key)
1105 {
1106 	show_rcu_gp_kthreads();
1107 }
1108 
1109 static const struct sysrq_key_op sysrq_rcudump_op = {
1110 	.handler = sysrq_show_rcu,
1111 	.help_msg = "show-rcu(y)",
1112 	.action_msg = "Show RCU tree",
1113 	.enable_mask = SYSRQ_ENABLE_DUMP,
1114 };
1115 
rcu_sysrq_init(void)1116 static int __init rcu_sysrq_init(void)
1117 {
1118 	if (sysrq_rcu)
1119 		return register_sysrq_key('y', &sysrq_rcudump_op);
1120 	return 0;
1121 }
1122 early_initcall(rcu_sysrq_init);
1123 
1124 #ifdef CONFIG_RCU_CPU_STALL_NOTIFIER
1125 
1126 //////////////////////////////////////////////////////////////////////////////
1127 //
1128 // RCU CPU stall-warning notifiers
1129 
1130 static ATOMIC_NOTIFIER_HEAD(rcu_cpu_stall_notifier_list);
1131 
1132 /**
1133  * rcu_stall_chain_notifier_register - Add an RCU CPU stall notifier
1134  * @n: Entry to add.
1135  *
1136  * Adds an RCU CPU stall notifier to an atomic notifier chain.
1137  * The @action passed to a notifier will be @RCU_STALL_NOTIFY_NORM or
1138  * friends.  The @data will be the duration of the stalled grace period,
1139  * in jiffies, coerced to a void* pointer.
1140  *
1141  * Returns 0 on success, %-EEXIST on error.
1142  */
rcu_stall_chain_notifier_register(struct notifier_block * n)1143 int rcu_stall_chain_notifier_register(struct notifier_block *n)
1144 {
1145 	int rcsn = rcu_cpu_stall_notifiers;
1146 
1147 	WARN(1, "Adding %pS() to RCU stall notifier list (%s).\n", n->notifier_call,
1148 	     rcsn ? "possibly suppressing RCU CPU stall warnings" : "failed, so all is well");
1149 	if (rcsn)
1150 		return atomic_notifier_chain_register(&rcu_cpu_stall_notifier_list, n);
1151 	return -EEXIST;
1152 }
1153 EXPORT_SYMBOL_GPL(rcu_stall_chain_notifier_register);
1154 
1155 /**
1156  * rcu_stall_chain_notifier_unregister - Remove an RCU CPU stall notifier
1157  * @n: Entry to add.
1158  *
1159  * Removes an RCU CPU stall notifier from an atomic notifier chain.
1160  *
1161  * Returns zero on success, %-ENOENT on failure.
1162  */
rcu_stall_chain_notifier_unregister(struct notifier_block * n)1163 int rcu_stall_chain_notifier_unregister(struct notifier_block *n)
1164 {
1165 	return atomic_notifier_chain_unregister(&rcu_cpu_stall_notifier_list, n);
1166 }
1167 EXPORT_SYMBOL_GPL(rcu_stall_chain_notifier_unregister);
1168 
1169 /*
1170  * rcu_stall_notifier_call_chain - Call functions in an RCU CPU stall notifier chain
1171  * @val: Value passed unmodified to notifier function
1172  * @v: Pointer passed unmodified to notifier function
1173  *
1174  * Calls each function in the RCU CPU stall notifier chain in turn, which
1175  * is an atomic call chain.  See atomic_notifier_call_chain() for more
1176  * information.
1177  *
1178  * This is for use within RCU, hence the omission of the extra asterisk
1179  * to indicate a non-kerneldoc format header comment.
1180  */
rcu_stall_notifier_call_chain(unsigned long val,void * v)1181 int rcu_stall_notifier_call_chain(unsigned long val, void *v)
1182 {
1183 	return atomic_notifier_call_chain(&rcu_cpu_stall_notifier_list, val, v);
1184 }
1185 
1186 #endif // #ifdef CONFIG_RCU_CPU_STALL_NOTIFIER
1187