xref: /linux/kernel/rcu/tree_stall.h (revision 83684c4e4d62cb02b2e4d0d18963d1035439278e)
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(const struct cpumask * stalled_mask)162 static void panic_on_rcu_stall(const struct cpumask *stalled_mask)
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(stalled_mask))
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 
483 	if (!rcu_cpu_stall_cputime)
484 		return;
485 
486 	rsrp = &rdp->snap_record;
487 	if (rsrp->gp_seq != rdp->gp_seq)
488 		return;
489 
490 	rsr.cputime_irq     = kcpustat_field(CPUTIME_IRQ, cpu);
491 	rsr.cputime_softirq = kcpustat_field(CPUTIME_SOFTIRQ, cpu);
492 	rsr.cputime_system  = kcpustat_field(CPUTIME_SYSTEM, cpu);
493 
494 	pr_err("\t         hardirqs   softirqs   csw/system\n");
495 	pr_err("\t number: %8lld %10d %12lld\n",
496 		kstat_cpu_irqs_sum(cpu) + arch_irq_stat_cpu(cpu) - rsrp->nr_hardirqs,
497 		kstat_cpu_softirqs_sum(cpu) - rsrp->nr_softirqs,
498 		nr_context_switches_cpu(cpu) - rsrp->nr_csw);
499 	pr_err("\tcputime: %8lld %10lld %12lld   ==> %d(ms)\n",
500 		div_u64(rsr.cputime_irq - rsrp->cputime_irq, NSEC_PER_MSEC),
501 		div_u64(rsr.cputime_softirq - rsrp->cputime_softirq, NSEC_PER_MSEC),
502 		div_u64(rsr.cputime_system - rsrp->cputime_system, NSEC_PER_MSEC),
503 		jiffies_to_msecs(jiffies - rsrp->jiffies));
504 }
505 
506 /*
507  * Print out diagnostic information for the specified stalled CPU.
508  *
509  * If the specified CPU is aware of the current RCU grace period, then
510  * print the number of scheduling clock interrupts the CPU has taken
511  * during the time that it has been aware.  Otherwise, print the number
512  * of RCU grace periods that this CPU is ignorant of, for example, "1"
513  * if the CPU was aware of the previous grace period.
514  *
515  * Also print out idle info.
516  */
print_cpu_stall_info(int cpu)517 static void print_cpu_stall_info(int cpu)
518 {
519 	unsigned long delta;
520 	bool falsepositive;
521 	struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
522 	char *ticks_title;
523 	unsigned long ticks_value;
524 	bool rcuc_starved;
525 	unsigned long j;
526 	char buf[32];
527 
528 	/*
529 	 * We could be printing a lot while holding a spinlock.  Avoid
530 	 * triggering hard lockup.
531 	 */
532 	touch_nmi_watchdog();
533 
534 	ticks_value = rcu_seq_ctr(rcu_state.gp_seq - rdp->gp_seq);
535 	if (ticks_value) {
536 		ticks_title = "GPs behind";
537 	} else {
538 		ticks_title = "ticks this GP";
539 		ticks_value = rdp->ticks_this_gp;
540 	}
541 	delta = rcu_seq_ctr(rdp->mynode->gp_seq - rdp->rcu_iw_gp_seq);
542 	falsepositive = rcu_is_gp_kthread_starving(NULL) &&
543 			rcu_watching_snap_in_eqs(ct_rcu_watching_cpu(cpu));
544 	rcuc_starved = rcu_is_rcuc_kthread_starving(rdp, &j);
545 	if (rcuc_starved)
546 		// Print signed value, as negative values indicate a probable bug.
547 		snprintf(buf, sizeof(buf), " rcuc=%ld jiffies(starved)", j);
548 	pr_err("\t%d-%c%c%c%c: (%lu %s) idle=%04x/%ld/%#lx softirq=%u/%u fqs=%ld%s%s\n",
549 	       cpu,
550 	       "O."[!!cpu_online(cpu)],
551 	       "o."[!!(rdp->grpmask & rdp->mynode->qsmaskinit)],
552 	       "N."[!!(rdp->grpmask & rdp->mynode->qsmaskinitnext)],
553 	       !IS_ENABLED(CONFIG_IRQ_WORK) ? '?' :
554 			rdp->rcu_iw_pending ? (int)min(delta, 9UL) + '0' :
555 				"!."[!delta],
556 	       ticks_value, ticks_title,
557 	       ct_rcu_watching_cpu(cpu) & 0xffff,
558 	       ct_nesting_cpu(cpu), ct_nmi_nesting_cpu(cpu),
559 	       rdp->softirq_snap, kstat_softirqs_cpu(RCU_SOFTIRQ, cpu),
560 	       data_race(rcu_state.n_force_qs) - rcu_state.n_force_qs_gpstart,
561 	       rcuc_starved ? buf : "",
562 	       falsepositive ? " (false positive?)" : "");
563 
564 	print_cpu_stat_info(cpu);
565 }
566 
567 /* Complain about starvation of grace-period kthread.  */
rcu_check_gp_kthread_starvation(void)568 static void rcu_check_gp_kthread_starvation(void)
569 {
570 	int cpu;
571 	struct task_struct *gpk = rcu_state.gp_kthread;
572 	unsigned long j;
573 
574 	if (rcu_is_gp_kthread_starving(&j)) {
575 		cpu = gpk ? task_cpu(gpk) : -1;
576 		pr_err("%s kthread starved for %ld jiffies! g%ld f%#x %s(%d) ->state=%c ->cpu=%d\n",
577 		       rcu_state.name, j,
578 		       (long)rcu_seq_current(&rcu_state.gp_seq),
579 		       data_race(READ_ONCE(rcu_state.gp_flags)),
580 		       gp_state_getname(rcu_state.gp_state),
581 		       data_race(READ_ONCE(rcu_state.gp_state)),
582 		       gpk ? task_state_to_char(gpk) : '?', cpu);
583 		if (gpk) {
584 			struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
585 
586 			pr_err("\tUnless %s kthread gets sufficient CPU time, OOM is now expected behavior.\n", rcu_state.name);
587 			pr_err("RCU grace-period kthread stack dump:\n");
588 			sched_show_task(gpk);
589 			if (cpu_is_offline(cpu)) {
590 				pr_err("RCU GP kthread last ran on offline CPU %d.\n", cpu);
591 			} else if (!(data_race(READ_ONCE(rdp->mynode->qsmask)) & rdp->grpmask)) {
592 				pr_err("Stack dump where RCU GP kthread last ran:\n");
593 				dump_cpu_task(cpu);
594 			}
595 			wake_up_process(gpk);
596 		}
597 	}
598 }
599 
600 /* Complain about missing wakeups from expired fqs wait timer */
rcu_check_gp_kthread_expired_fqs_timer(void)601 static void rcu_check_gp_kthread_expired_fqs_timer(void)
602 {
603 	struct task_struct *gpk = rcu_state.gp_kthread;
604 	short gp_state;
605 	unsigned long jiffies_fqs;
606 	int cpu;
607 
608 	/*
609 	 * Order reads of .gp_state and .jiffies_force_qs.
610 	 * Matching smp_wmb() is present in rcu_gp_fqs_loop().
611 	 */
612 	gp_state = smp_load_acquire(&rcu_state.gp_state);
613 	jiffies_fqs = READ_ONCE(rcu_state.jiffies_force_qs);
614 
615 	if (gp_state == RCU_GP_WAIT_FQS &&
616 	    time_after(jiffies, jiffies_fqs + RCU_STALL_MIGHT_MIN) &&
617 	    gpk && !READ_ONCE(gpk->on_rq)) {
618 		cpu = task_cpu(gpk);
619 		pr_err("%s kthread timer wakeup didn't happen for %ld jiffies! g%ld f%#x %s(%d) ->state=%c\n",
620 		       rcu_state.name, (jiffies - jiffies_fqs),
621 		       (long)rcu_seq_current(&rcu_state.gp_seq),
622 		       data_race(READ_ONCE(rcu_state.gp_flags)), // Diagnostic read
623 		       gp_state_getname(RCU_GP_WAIT_FQS), RCU_GP_WAIT_FQS,
624 		       task_state_to_char(gpk));
625 		pr_err("\tPossible timer handling issue on cpu=%d timer-softirq=%u\n",
626 		       cpu, kstat_softirqs_cpu(TIMER_SOFTIRQ, cpu));
627 	}
628 }
629 
print_other_cpu_stall(unsigned long gp_seq,unsigned long gps)630 static void print_other_cpu_stall(unsigned long gp_seq, unsigned long gps)
631 {
632 	int cpu;
633 	unsigned long flags;
634 	unsigned long gpa;
635 	unsigned long j;
636 	int ndetected = 0;
637 	struct rcu_node *rnp;
638 	long totqlen = 0;
639 
640 	lockdep_assert_irqs_disabled();
641 
642 	/* Kick and suppress, if so configured. */
643 	rcu_stall_kick_kthreads();
644 	if (rcu_stall_is_suppressed())
645 		return;
646 
647 	cpumask_clear(&rcu_stall_cpumask);
648 
649 	nbcon_cpu_emergency_enter();
650 
651 	/*
652 	 * OK, time to rat on our buddy...
653 	 * See Documentation/RCU/stallwarn.rst for info on how to debug
654 	 * RCU CPU stall warnings.
655 	 */
656 	trace_rcu_stall_warning(rcu_state.name, TPS("StallDetected"));
657 	pr_err("INFO: %s detected stalls on CPUs/tasks:\n", rcu_state.name);
658 	rcu_for_each_leaf_node(rnp) {
659 		raw_spin_lock_irqsave_rcu_node(rnp, flags);
660 		if (rnp->qsmask != 0) {
661 			for_each_leaf_node_possible_cpu(rnp, cpu)
662 				if (rnp->qsmask & leaf_node_cpu_bit(rnp, cpu)) {
663 					print_cpu_stall_info(cpu);
664 					cpumask_set_cpu(cpu, &rcu_stall_cpumask);
665 					ndetected++;
666 				}
667 		}
668 		ndetected += rcu_print_task_stall(rnp, flags); // Releases rnp->lock.
669 		lockdep_assert_irqs_disabled();
670 	}
671 
672 	for_each_possible_cpu(cpu)
673 		totqlen += rcu_get_n_cbs_cpu(cpu);
674 	pr_err("\t(detected by %d, t=%ld jiffies, g=%ld, q=%lu ncpus=%d)\n",
675 	       smp_processor_id(), (long)(jiffies - gps),
676 	       (long)rcu_seq_current(&rcu_state.gp_seq), totqlen,
677 	       data_race(rcu_state.n_online_cpus)); // Diagnostic read
678 	if (ndetected) {
679 		rcu_dump_cpu_stacks(gp_seq);
680 
681 		/* Complain about tasks blocking the grace period. */
682 		rcu_for_each_leaf_node(rnp)
683 			rcu_print_detail_task_stall_rnp(rnp);
684 	} else {
685 		if (rcu_seq_current(&rcu_state.gp_seq) != gp_seq) {
686 			pr_err("INFO: Stall ended before state dump start\n");
687 		} else {
688 			j = jiffies;
689 			gpa = data_race(READ_ONCE(rcu_state.gp_activity));
690 			pr_err("All QSes seen, last %s kthread activity %ld (%ld-%ld), jiffies_till_next_fqs=%ld, root ->qsmask %#lx\n",
691 			       rcu_state.name, j - gpa, j, gpa,
692 			       data_race(READ_ONCE(jiffies_till_next_fqs)),
693 			       data_race(READ_ONCE(rcu_get_root()->qsmask)));
694 		}
695 	}
696 	/* Rewrite if needed in case of slow consoles. */
697 	if (ULONG_CMP_GE(jiffies, READ_ONCE(rcu_state.jiffies_stall)))
698 		WRITE_ONCE(rcu_state.jiffies_stall,
699 			   jiffies + 3 * rcu_jiffies_till_stall_check() + 3);
700 
701 	rcu_check_gp_kthread_expired_fqs_timer();
702 	rcu_check_gp_kthread_starvation();
703 
704 	nbcon_cpu_emergency_exit();
705 
706 	panic_on_rcu_stall(&rcu_stall_cpumask);
707 
708 	rcu_force_quiescent_state();  /* Kick them all. */
709 }
710 
print_cpu_stall(unsigned long gp_seq,unsigned long gps)711 static void print_cpu_stall(unsigned long gp_seq, unsigned long gps)
712 {
713 	int cpu;
714 	unsigned long flags;
715 	struct rcu_data *rdp = this_cpu_ptr(&rcu_data);
716 	struct rcu_node *rnp = rcu_get_root();
717 	long totqlen = 0;
718 
719 	lockdep_assert_irqs_disabled();
720 
721 	/* Kick and suppress, if so configured. */
722 	rcu_stall_kick_kthreads();
723 	if (rcu_stall_is_suppressed())
724 		return;
725 
726 	nbcon_cpu_emergency_enter();
727 
728 	/*
729 	 * OK, time to rat on ourselves...
730 	 * See Documentation/RCU/stallwarn.rst for info on how to debug
731 	 * RCU CPU stall warnings.
732 	 */
733 	trace_rcu_stall_warning(rcu_state.name, TPS("SelfDetected"));
734 	pr_err("INFO: %s self-detected stall on CPU\n", rcu_state.name);
735 	raw_spin_lock_irqsave_rcu_node(rdp->mynode, flags);
736 	print_cpu_stall_info(smp_processor_id());
737 	raw_spin_unlock_irqrestore_rcu_node(rdp->mynode, flags);
738 	for_each_possible_cpu(cpu)
739 		totqlen += rcu_get_n_cbs_cpu(cpu);
740 	pr_err("\t(t=%lu jiffies g=%ld q=%lu ncpus=%d)\n",
741 		jiffies - gps,
742 		(long)rcu_seq_current(&rcu_state.gp_seq), totqlen,
743 		data_race(rcu_state.n_online_cpus)); // Diagnostic read
744 
745 	rcu_check_gp_kthread_expired_fqs_timer();
746 	rcu_check_gp_kthread_starvation();
747 
748 	rcu_dump_cpu_stacks(gp_seq);
749 
750 	raw_spin_lock_irqsave_rcu_node(rnp, flags);
751 	/* Rewrite if needed in case of slow consoles. */
752 	if (ULONG_CMP_GE(jiffies, READ_ONCE(rcu_state.jiffies_stall)))
753 		WRITE_ONCE(rcu_state.jiffies_stall,
754 			   jiffies + 3 * rcu_jiffies_till_stall_check() + 3);
755 	raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
756 
757 	nbcon_cpu_emergency_exit();
758 
759 	cpumask_clear(&rcu_stall_cpumask);
760 	cpumask_set_cpu(smp_processor_id(), &rcu_stall_cpumask);
761 	panic_on_rcu_stall(&rcu_stall_cpumask);
762 
763 	/*
764 	 * Attempt to revive the RCU machinery by forcing a context switch.
765 	 *
766 	 * A context switch would normally allow the RCU state machine to make
767 	 * progress and it could be we're stuck in kernel space without context
768 	 * switches for an entirely unreasonable amount of time.
769 	 */
770 	set_need_resched_current();
771 }
772 
773 static bool csd_lock_suppress_rcu_stall;
774 module_param(csd_lock_suppress_rcu_stall, bool, 0644);
775 
check_cpu_stall(struct rcu_data * rdp)776 static void check_cpu_stall(struct rcu_data *rdp)
777 {
778 	bool self_detected;
779 	unsigned long gs1;
780 	unsigned long gs2;
781 	unsigned long gps;
782 	unsigned long j;
783 	unsigned long jn;
784 	unsigned long js;
785 	struct rcu_node *rnp;
786 
787 	lockdep_assert_irqs_disabled();
788 	if ((rcu_stall_is_suppressed() && !READ_ONCE(rcu_kick_kthreads)) ||
789 	    !rcu_gp_in_progress())
790 		return;
791 	rcu_stall_kick_kthreads();
792 
793 	/*
794 	 * Check if it was requested (via rcu_cpu_stall_reset()) that the FQS
795 	 * loop has to set jiffies to ensure a non-stale jiffies value. This
796 	 * is required to have good jiffies value after coming out of long
797 	 * breaks of jiffies updates. Not doing so can cause false positives.
798 	 */
799 	if (READ_ONCE(rcu_state.nr_fqs_jiffies_stall) > 0)
800 		return;
801 
802 	j = jiffies;
803 
804 	/*
805 	 * Lots of memory barriers to reject false positives.
806 	 *
807 	 * The idea is to pick up rcu_state.gp_seq, then
808 	 * rcu_state.jiffies_stall, then rcu_state.gp_start, and finally
809 	 * another copy of rcu_state.gp_seq.  These values are updated in
810 	 * the opposite order with memory barriers (or equivalent) during
811 	 * grace-period initialization and cleanup.  Now, a false positive
812 	 * can occur if we get an new value of rcu_state.gp_start and a old
813 	 * value of rcu_state.jiffies_stall.  But given the memory barriers,
814 	 * the only way that this can happen is if one grace period ends
815 	 * and another starts between these two fetches.  This is detected
816 	 * by comparing the second fetch of rcu_state.gp_seq with the
817 	 * previous fetch from rcu_state.gp_seq.
818 	 *
819 	 * Given this check, comparisons of jiffies, rcu_state.jiffies_stall,
820 	 * and rcu_state.gp_start suffice to forestall false positives.
821 	 */
822 	gs1 = READ_ONCE(rcu_state.gp_seq);
823 	smp_rmb(); /* Pick up ->gp_seq first... */
824 	js = READ_ONCE(rcu_state.jiffies_stall);
825 	smp_rmb(); /* ...then ->jiffies_stall before the rest... */
826 	gps = READ_ONCE(rcu_state.gp_start);
827 	smp_rmb(); /* ...and finally ->gp_start before ->gp_seq again. */
828 	gs2 = READ_ONCE(rcu_state.gp_seq);
829 	if (gs1 != gs2 ||
830 	    ULONG_CMP_LT(j, js) ||
831 	    ULONG_CMP_GE(gps, js) ||
832 	    !rcu_seq_state(gs2))
833 		return; /* No stall or GP completed since entering function. */
834 	rnp = rdp->mynode;
835 	jn = jiffies + ULONG_MAX / 2;
836 	self_detected = READ_ONCE(rnp->qsmask) & rdp->grpmask;
837 	if (rcu_gp_in_progress() &&
838 	    (self_detected || ULONG_CMP_GE(j, js + RCU_STALL_RAT_DELAY)) &&
839 	    cmpxchg(&rcu_state.jiffies_stall, js, jn) == js) {
840 		/*
841 		 * If a virtual machine is stopped by the host it can look to
842 		 * the watchdog like an RCU stall. Check to see if the host
843 		 * stopped the vm.
844 		 */
845 		if (kvm_check_and_clear_guest_paused())
846 			return;
847 
848 #ifdef CONFIG_SYSFS
849 		++rcu_stall_count;
850 #endif
851 
852 		rcu_stall_notifier_call_chain(RCU_STALL_NOTIFY_NORM, (void *)j - gps);
853 		if (READ_ONCE(csd_lock_suppress_rcu_stall) && csd_lock_is_stuck()) {
854 			pr_err("INFO: %s detected stall, but suppressed full report due to a stuck CSD-lock.\n", rcu_state.name);
855 		} else if (self_detected) {
856 			/* We haven't checked in, so go dump stack. */
857 			print_cpu_stall(gs2, gps);
858 		} else {
859 			/* They had a few time units to dump stack, so complain. */
860 			print_other_cpu_stall(gs2, gps);
861 		}
862 
863 		if (READ_ONCE(rcu_cpu_stall_ftrace_dump))
864 			rcu_ftrace_dump(DUMP_ALL);
865 
866 		if (READ_ONCE(rcu_state.jiffies_stall) == jn) {
867 			jn = jiffies + 3 * rcu_jiffies_till_stall_check() + 3;
868 			WRITE_ONCE(rcu_state.jiffies_stall, jn);
869 		}
870 	}
871 }
872 
873 //////////////////////////////////////////////////////////////////////////////
874 //
875 // RCU forward-progress mechanisms, including for callback invocation.
876 
877 
878 /*
879  * Check to see if a failure to end RCU priority inversion was due to
880  * a CPU not passing through a quiescent state.  When this happens, there
881  * is nothing that RCU priority boosting can do to help, so we shouldn't
882  * count this as an RCU priority boosting failure.  A return of true says
883  * RCU priority boosting is to blame, and false says otherwise.  If false
884  * is returned, the first of the CPUs to blame is stored through cpup.
885  * If there was no CPU blocking the current grace period, but also nothing
886  * in need of being boosted, *cpup is set to -1.  This can happen in case
887  * of vCPU preemption while the last CPU is reporting its quiscent state,
888  * for example.
889  *
890  * If cpup is NULL, then a lockless quick check is carried out, suitable
891  * for high-rate usage.  On the other hand, if cpup is non-NULL, each
892  * rcu_node structure's ->lock is acquired, ruling out high-rate usage.
893  */
rcu_check_boost_fail(unsigned long gp_state,int * cpup)894 bool rcu_check_boost_fail(unsigned long gp_state, int *cpup)
895 {
896 	bool atb = false;
897 	int cpu;
898 	unsigned long flags;
899 	struct rcu_node *rnp;
900 
901 	rcu_for_each_leaf_node(rnp) {
902 		if (!cpup) {
903 			if (data_race(READ_ONCE(rnp->qsmask))) {
904 				return false;
905 			} else {
906 				if (READ_ONCE(rnp->gp_tasks))
907 					atb = true;
908 				continue;
909 			}
910 		}
911 		*cpup = -1;
912 		raw_spin_lock_irqsave_rcu_node(rnp, flags);
913 		if (rnp->gp_tasks)
914 			atb = true;
915 		if (!rnp->qsmask) {
916 			// No CPUs without quiescent states for this rnp.
917 			raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
918 			continue;
919 		}
920 		// Find the first holdout CPU.
921 		for_each_leaf_node_possible_cpu(rnp, cpu) {
922 			if (rnp->qsmask & (1UL << (cpu - rnp->grplo))) {
923 				raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
924 				*cpup = cpu;
925 				return false;
926 			}
927 		}
928 		raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
929 	}
930 	// Can't blame CPUs, so must blame RCU priority boosting.
931 	return atb;
932 }
933 EXPORT_SYMBOL_GPL(rcu_check_boost_fail);
934 
show_rcu_state(void)935 static noinline_for_stack void show_rcu_state(void)
936 {
937 	unsigned long j;
938 	unsigned long ja;
939 	unsigned long jr;
940 	unsigned long js;
941 	unsigned long jw;
942 	struct task_struct *t = READ_ONCE(rcu_state.gp_kthread);
943 
944 	j = jiffies;
945 	ja = j - data_race(READ_ONCE(rcu_state.gp_activity));
946 	jr = j - data_race(READ_ONCE(rcu_state.gp_req_activity));
947 	js = j - data_race(READ_ONCE(rcu_state.gp_start));
948 	jw = j - data_race(READ_ONCE(rcu_state.gp_wake_time));
949 	pr_info("%s: wait state: %s(%d) ->state: %c ->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",
950 		rcu_state.name, gp_state_getname(rcu_state.gp_state),
951 		data_race(READ_ONCE(rcu_state.gp_state)),
952 		t ? task_state_to_char(t) : '?', t ? t->rt_priority : 0xffU,
953 		js, ja, jr, jw, (long)data_race(READ_ONCE(rcu_state.gp_wake_seq)),
954 		(long)data_race(READ_ONCE(rcu_state.gp_seq)),
955 		(long)data_race(READ_ONCE(rcu_get_root()->gp_seq_needed)),
956 		data_race(READ_ONCE(rcu_state.gp_max)),
957 		data_race(READ_ONCE(rcu_state.gp_flags)));
958 }
959 
show_rcu_node(struct rcu_node * rnp)960 static noinline_for_stack void show_rcu_node(struct rcu_node *rnp)
961 {
962 	pr_info("\trcu_node %d:%d ->gp_seq %ld ->gp_seq_needed %ld ->qsmask %#lx %c%c%c%c ->n_boosts %ld\n",
963 		rnp->grplo, rnp->grphi,
964 		(long)data_race(READ_ONCE(rnp->gp_seq)),
965 		(long)data_race(READ_ONCE(rnp->gp_seq_needed)),
966 		data_race(READ_ONCE(rnp->qsmask)),
967 		".b"[!!data_race(READ_ONCE(rnp->boost_kthread_task))],
968 		".B"[!!data_race(READ_ONCE(rnp->boost_tasks))],
969 		".E"[!!data_race(READ_ONCE(rnp->exp_tasks))],
970 		".G"[!!data_race(READ_ONCE(rnp->gp_tasks))],
971 		data_race(READ_ONCE(rnp->n_boosts)));
972 }
973 
974 /*
975  * Show the state of the grace-period kthreads.
976  */
show_rcu_gp_kthreads(void)977 void show_rcu_gp_kthreads(void)
978 {
979 	unsigned long cbs = 0;
980 	int cpu;
981 	struct rcu_data *rdp;
982 	struct rcu_node *rnp;
983 
984 	show_rcu_state();
985 	rcu_for_each_node_breadth_first(rnp) {
986 		if (ULONG_CMP_GE(READ_ONCE(rcu_state.gp_seq), READ_ONCE(rnp->gp_seq_needed)) &&
987 		    !data_race(READ_ONCE(rnp->qsmask)) && !data_race(READ_ONCE(rnp->boost_tasks)) &&
988 		    !data_race(READ_ONCE(rnp->exp_tasks)) && !data_race(READ_ONCE(rnp->gp_tasks)))
989 			continue;
990 		show_rcu_node(rnp);
991 		if (!rcu_is_leaf_node(rnp))
992 			continue;
993 		for_each_leaf_node_possible_cpu(rnp, cpu) {
994 			rdp = per_cpu_ptr(&rcu_data, cpu);
995 			if (READ_ONCE(rdp->gpwrap) ||
996 			    ULONG_CMP_GE(READ_ONCE(rcu_state.gp_seq),
997 					 READ_ONCE(rdp->gp_seq_needed)))
998 				continue;
999 			pr_info("\tcpu %d ->gp_seq_needed %ld\n",
1000 				cpu, (long)data_race(READ_ONCE(rdp->gp_seq_needed)));
1001 		}
1002 	}
1003 	for_each_possible_cpu(cpu) {
1004 		rdp = per_cpu_ptr(&rcu_data, cpu);
1005 		cbs += data_race(READ_ONCE(rdp->n_cbs_invoked));
1006 		show_rcu_nocb_state(rdp);
1007 	}
1008 	pr_info("RCU callbacks invoked since boot: %lu\n", cbs);
1009 	show_rcu_tasks_gp_kthreads();
1010 }
1011 EXPORT_SYMBOL_GPL(show_rcu_gp_kthreads);
1012 
1013 /*
1014  * This function checks for grace-period requests that fail to motivate
1015  * RCU to come out of its idle mode.
1016  */
rcu_check_gp_start_stall(struct rcu_node * rnp,const unsigned long gpssdelay)1017 static void rcu_check_gp_start_stall(struct rcu_node *rnp, const unsigned long gpssdelay)
1018 {
1019 	unsigned long flags;
1020 	unsigned long j;
1021 	struct rcu_node *rnp_root = rcu_get_root();
1022 	static atomic_t warned = ATOMIC_INIT(0);
1023 
1024 	if (!IS_ENABLED(CONFIG_PROVE_RCU) || rcu_gp_in_progress() ||
1025 	    ULONG_CMP_GE(READ_ONCE(rnp_root->gp_seq),
1026 			 READ_ONCE(rnp_root->gp_seq_needed)) ||
1027 	    !smp_load_acquire(&rcu_state.gp_kthread)) // Get stable kthread.
1028 		return;
1029 	j = jiffies; /* Expensive access, and in common case don't get here. */
1030 	if (time_before(j, READ_ONCE(rcu_state.gp_req_activity) + gpssdelay) ||
1031 	    time_before(j, READ_ONCE(rcu_state.gp_activity) + gpssdelay) ||
1032 	    atomic_read(&warned))
1033 		return;
1034 
1035 	raw_spin_lock_irqsave_rcu_node(rnp, flags);
1036 	j = jiffies;
1037 	if (rcu_gp_in_progress() ||
1038 	    ULONG_CMP_GE(READ_ONCE(rnp_root->gp_seq),
1039 			 READ_ONCE(rnp_root->gp_seq_needed)) ||
1040 	    time_before(j, READ_ONCE(rcu_state.gp_req_activity) + gpssdelay) ||
1041 	    time_before(j, READ_ONCE(rcu_state.gp_activity) + gpssdelay) ||
1042 	    atomic_read(&warned)) {
1043 		raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
1044 		return;
1045 	}
1046 	/* Hold onto the leaf lock to make others see warned==1. */
1047 
1048 	if (rnp_root != rnp)
1049 		raw_spin_lock_rcu_node(rnp_root); /* irqs already disabled. */
1050 	j = jiffies;
1051 	if (rcu_gp_in_progress() ||
1052 	    ULONG_CMP_GE(READ_ONCE(rnp_root->gp_seq),
1053 			 READ_ONCE(rnp_root->gp_seq_needed)) ||
1054 	    time_before(j, READ_ONCE(rcu_state.gp_req_activity) + gpssdelay) ||
1055 	    time_before(j, READ_ONCE(rcu_state.gp_activity) + gpssdelay) ||
1056 	    atomic_xchg(&warned, 1)) {
1057 		if (rnp_root != rnp)
1058 			/* irqs remain disabled. */
1059 			raw_spin_unlock_rcu_node(rnp_root);
1060 		raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
1061 		return;
1062 	}
1063 	WARN_ON(1);
1064 	if (rnp_root != rnp)
1065 		raw_spin_unlock_rcu_node(rnp_root);
1066 	raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
1067 	show_rcu_gp_kthreads();
1068 }
1069 
1070 /*
1071  * Do a forward-progress check for rcutorture.  This is normally invoked
1072  * due to an OOM event.  The argument "j" gives the time period during
1073  * which rcutorture would like progress to have been made.
1074  */
rcu_fwd_progress_check(unsigned long j)1075 void rcu_fwd_progress_check(unsigned long j)
1076 {
1077 	unsigned long cbs;
1078 	int cpu;
1079 	unsigned long max_cbs = 0;
1080 	int max_cpu = -1;
1081 	struct rcu_data *rdp;
1082 
1083 	if (rcu_gp_in_progress()) {
1084 		pr_info("%s: GP age %lu jiffies\n",
1085 			__func__, jiffies - data_race(READ_ONCE(rcu_state.gp_start)));
1086 		show_rcu_gp_kthreads();
1087 	} else {
1088 		pr_info("%s: Last GP end %lu jiffies ago\n",
1089 			__func__, jiffies - data_race(READ_ONCE(rcu_state.gp_end)));
1090 		preempt_disable();
1091 		rdp = this_cpu_ptr(&rcu_data);
1092 		rcu_check_gp_start_stall(rdp->mynode, j);
1093 		preempt_enable();
1094 	}
1095 	for_each_possible_cpu(cpu) {
1096 		cbs = rcu_get_n_cbs_cpu(cpu);
1097 		if (!cbs)
1098 			continue;
1099 		if (max_cpu < 0)
1100 			pr_info("%s: callbacks", __func__);
1101 		pr_cont(" %d: %lu", cpu, cbs);
1102 		if (cbs <= max_cbs)
1103 			continue;
1104 		max_cbs = cbs;
1105 		max_cpu = cpu;
1106 	}
1107 	if (max_cpu >= 0)
1108 		pr_cont("\n");
1109 }
1110 EXPORT_SYMBOL_GPL(rcu_fwd_progress_check);
1111 
1112 /* Commandeer a sysrq key to dump RCU's tree. */
1113 static bool sysrq_rcu;
1114 module_param(sysrq_rcu, bool, 0444);
1115 
1116 /* Dump grace-period-request information due to commandeered sysrq. */
sysrq_show_rcu(u8 key)1117 static void sysrq_show_rcu(u8 key)
1118 {
1119 	show_rcu_gp_kthreads();
1120 }
1121 
1122 static const struct sysrq_key_op sysrq_rcudump_op = {
1123 	.handler = sysrq_show_rcu,
1124 	.help_msg = "show-rcu(y)",
1125 	.action_msg = "Show RCU tree",
1126 	.enable_mask = SYSRQ_ENABLE_DUMP,
1127 };
1128 
rcu_sysrq_init(void)1129 static int __init rcu_sysrq_init(void)
1130 {
1131 	if (sysrq_rcu)
1132 		return register_sysrq_key('y', &sysrq_rcudump_op);
1133 	return 0;
1134 }
1135 early_initcall(rcu_sysrq_init);
1136 
1137 #ifdef CONFIG_RCU_CPU_STALL_NOTIFIER
1138 
1139 //////////////////////////////////////////////////////////////////////////////
1140 //
1141 // RCU CPU stall-warning notifiers
1142 
1143 static ATOMIC_NOTIFIER_HEAD(rcu_cpu_stall_notifier_list);
1144 
1145 /**
1146  * rcu_stall_chain_notifier_register - Add an RCU CPU stall notifier
1147  * @n: Entry to add.
1148  *
1149  * Adds an RCU CPU stall notifier to an atomic notifier chain.
1150  * The @action passed to a notifier will be @RCU_STALL_NOTIFY_NORM or
1151  * friends.  The @data will be the duration of the stalled grace period,
1152  * in jiffies, coerced to a void* pointer.
1153  *
1154  * Returns 0 on success, %-EEXIST on error.
1155  */
rcu_stall_chain_notifier_register(struct notifier_block * n)1156 int rcu_stall_chain_notifier_register(struct notifier_block *n)
1157 {
1158 	int rcsn = rcu_cpu_stall_notifiers;
1159 
1160 	WARN(1, "Adding %pS() to RCU stall notifier list (%s).\n", n->notifier_call,
1161 	     rcsn ? "possibly suppressing RCU CPU stall warnings" : "failed, so all is well");
1162 	if (rcsn)
1163 		return atomic_notifier_chain_register(&rcu_cpu_stall_notifier_list, n);
1164 	return -EEXIST;
1165 }
1166 EXPORT_SYMBOL_GPL(rcu_stall_chain_notifier_register);
1167 
1168 /**
1169  * rcu_stall_chain_notifier_unregister - Remove an RCU CPU stall notifier
1170  * @n: Entry to add.
1171  *
1172  * Removes an RCU CPU stall notifier from an atomic notifier chain.
1173  *
1174  * Returns zero on success, %-ENOENT on failure.
1175  */
rcu_stall_chain_notifier_unregister(struct notifier_block * n)1176 int rcu_stall_chain_notifier_unregister(struct notifier_block *n)
1177 {
1178 	return atomic_notifier_chain_unregister(&rcu_cpu_stall_notifier_list, n);
1179 }
1180 EXPORT_SYMBOL_GPL(rcu_stall_chain_notifier_unregister);
1181 
1182 /*
1183  * rcu_stall_notifier_call_chain - Call functions in an RCU CPU stall notifier chain
1184  * @val: Value passed unmodified to notifier function
1185  * @v: Pointer passed unmodified to notifier function
1186  *
1187  * Calls each function in the RCU CPU stall notifier chain in turn, which
1188  * is an atomic call chain.  See atomic_notifier_call_chain() for more
1189  * information.
1190  *
1191  * This is for use within RCU, hence the omission of the extra asterisk
1192  * to indicate a non-kerneldoc format header comment.
1193  */
rcu_stall_notifier_call_chain(unsigned long val,void * v)1194 int rcu_stall_notifier_call_chain(unsigned long val, void *v)
1195 {
1196 	return atomic_notifier_call_chain(&rcu_cpu_stall_notifier_list, val, v);
1197 }
1198 
1199 #endif // #ifdef CONFIG_RCU_CPU_STALL_NOTIFIER
1200