• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Detect hard and soft lockups on a system
4  *
5  * started by Don Zickus, Copyright (C) 2010 Red Hat, Inc.
6  *
7  * Note: Most of this code is borrowed heavily from the original softlockup
8  * detector, so thanks to Ingo for the initial implementation.
9  * Some chunks also taken from the old x86-specific nmi watchdog code, thanks
10  * to those contributors as well.
11  */
12 
13 #define pr_fmt(fmt) "watchdog: " fmt
14 
15 #include <linux/cpu.h>
16 #include <linux/init.h>
17 #include <linux/irq.h>
18 #include <linux/irqdesc.h>
19 #include <linux/kernel_stat.h>
20 #include <linux/kvm_para.h>
21 #include <linux/math64.h>
22 #include <linux/mm.h>
23 #include <linux/module.h>
24 #include <linux/nmi.h>
25 #include <linux/stop_machine.h>
26 #include <linux/sysctl.h>
27 #include <linux/tick.h>
28 
29 #include <linux/sched/clock.h>
30 #include <linux/sched/debug.h>
31 #include <linux/sched/isolation.h>
32 
33 #include <asm/irq_regs.h>
34 
35 #include <trace/hooks/softlockup.h>
36 
37 static DEFINE_MUTEX(watchdog_mutex);
38 
39 #if defined(CONFIG_HARDLOCKUP_DETECTOR) || defined(CONFIG_HARDLOCKUP_DETECTOR_SPARC64)
40 # define WATCHDOG_HARDLOCKUP_DEFAULT	1
41 #else
42 # define WATCHDOG_HARDLOCKUP_DEFAULT	0
43 #endif
44 
45 #define NUM_SAMPLE_PERIODS	5
46 
47 unsigned long __read_mostly watchdog_enabled;
48 int __read_mostly watchdog_user_enabled = 1;
49 static int __read_mostly watchdog_hardlockup_user_enabled = WATCHDOG_HARDLOCKUP_DEFAULT;
50 static int __read_mostly watchdog_softlockup_user_enabled = 1;
51 int __read_mostly watchdog_thresh = 10;
52 static int __read_mostly watchdog_thresh_next;
53 static int __read_mostly watchdog_hardlockup_available;
54 
55 struct cpumask watchdog_cpumask __read_mostly;
56 unsigned long *watchdog_cpumask_bits = cpumask_bits(&watchdog_cpumask);
57 
58 #ifdef CONFIG_HARDLOCKUP_DETECTOR
59 
60 # ifdef CONFIG_SMP
61 int __read_mostly sysctl_hardlockup_all_cpu_backtrace;
62 # endif /* CONFIG_SMP */
63 
64 /*
65  * Should we panic when a soft-lockup or hard-lockup occurs:
66  */
67 unsigned int __read_mostly hardlockup_panic =
68 			IS_ENABLED(CONFIG_BOOTPARAM_HARDLOCKUP_PANIC);
69 /*
70  * We may not want to enable hard lockup detection by default in all cases,
71  * for example when running the kernel as a guest on a hypervisor. In these
72  * cases this function can be called to disable hard lockup detection. This
73  * function should only be executed once by the boot processor before the
74  * kernel command line parameters are parsed, because otherwise it is not
75  * possible to override this in hardlockup_panic_setup().
76  */
hardlockup_detector_disable(void)77 void __init hardlockup_detector_disable(void)
78 {
79 	watchdog_hardlockup_user_enabled = 0;
80 }
81 
hardlockup_panic_setup(char * str)82 static int __init hardlockup_panic_setup(char *str)
83 {
84 next:
85 	if (!strncmp(str, "panic", 5))
86 		hardlockup_panic = 1;
87 	else if (!strncmp(str, "nopanic", 7))
88 		hardlockup_panic = 0;
89 	else if (!strncmp(str, "0", 1))
90 		watchdog_hardlockup_user_enabled = 0;
91 	else if (!strncmp(str, "1", 1))
92 		watchdog_hardlockup_user_enabled = 1;
93 	else if (!strncmp(str, "r", 1))
94 		hardlockup_config_perf_event(str + 1);
95 	while (*(str++)) {
96 		if (*str == ',') {
97 			str++;
98 			goto next;
99 		}
100 	}
101 	return 1;
102 }
103 __setup("nmi_watchdog=", hardlockup_panic_setup);
104 
105 #endif /* CONFIG_HARDLOCKUP_DETECTOR */
106 
107 #if defined(CONFIG_HARDLOCKUP_DETECTOR_COUNTS_HRTIMER)
108 
109 static DEFINE_PER_CPU(atomic_t, hrtimer_interrupts);
110 static DEFINE_PER_CPU(int, hrtimer_interrupts_saved);
111 static DEFINE_PER_CPU(bool, watchdog_hardlockup_warned);
112 static DEFINE_PER_CPU(bool, watchdog_hardlockup_touched);
113 static unsigned long hard_lockup_nmi_warn;
114 
arch_touch_nmi_watchdog(void)115 notrace void arch_touch_nmi_watchdog(void)
116 {
117 	/*
118 	 * Using __raw here because some code paths have
119 	 * preemption enabled.  If preemption is enabled
120 	 * then interrupts should be enabled too, in which
121 	 * case we shouldn't have to worry about the watchdog
122 	 * going off.
123 	 */
124 	raw_cpu_write(watchdog_hardlockup_touched, true);
125 }
126 EXPORT_SYMBOL(arch_touch_nmi_watchdog);
127 
watchdog_hardlockup_touch_cpu(unsigned int cpu)128 void watchdog_hardlockup_touch_cpu(unsigned int cpu)
129 {
130 	per_cpu(watchdog_hardlockup_touched, cpu) = true;
131 }
132 
is_hardlockup(unsigned int cpu)133 static bool is_hardlockup(unsigned int cpu)
134 {
135 	int hrint = atomic_read(&per_cpu(hrtimer_interrupts, cpu));
136 
137 	if (per_cpu(hrtimer_interrupts_saved, cpu) == hrint)
138 		return true;
139 
140 	/*
141 	 * NOTE: we don't need any fancy atomic_t or READ_ONCE/WRITE_ONCE
142 	 * for hrtimer_interrupts_saved. hrtimer_interrupts_saved is
143 	 * written/read by a single CPU.
144 	 */
145 	per_cpu(hrtimer_interrupts_saved, cpu) = hrint;
146 
147 	return false;
148 }
149 
watchdog_hardlockup_kick(void)150 static void watchdog_hardlockup_kick(void)
151 {
152 	int new_interrupts;
153 
154 	new_interrupts = atomic_inc_return(this_cpu_ptr(&hrtimer_interrupts));
155 	watchdog_buddy_check_hardlockup(new_interrupts);
156 }
157 
watchdog_hardlockup_check(unsigned int cpu,struct pt_regs * regs)158 void watchdog_hardlockup_check(unsigned int cpu, struct pt_regs *regs)
159 {
160 	if (per_cpu(watchdog_hardlockup_touched, cpu)) {
161 		per_cpu(watchdog_hardlockup_touched, cpu) = false;
162 		return;
163 	}
164 
165 	/*
166 	 * Check for a hardlockup by making sure the CPU's timer
167 	 * interrupt is incrementing. The timer interrupt should have
168 	 * fired multiple times before we overflow'd. If it hasn't
169 	 * then this is a good indication the cpu is stuck
170 	 */
171 	if (is_hardlockup(cpu)) {
172 		unsigned int this_cpu = smp_processor_id();
173 		unsigned long flags;
174 
175 		/* Only print hardlockups once. */
176 		if (per_cpu(watchdog_hardlockup_warned, cpu))
177 			return;
178 
179 		/*
180 		 * Prevent multiple hard-lockup reports if one cpu is already
181 		 * engaged in dumping all cpu back traces.
182 		 */
183 		if (sysctl_hardlockup_all_cpu_backtrace) {
184 			if (test_and_set_bit_lock(0, &hard_lockup_nmi_warn))
185 				return;
186 		}
187 
188 		/*
189 		 * NOTE: we call printk_cpu_sync_get_irqsave() after printing
190 		 * the lockup message. While it would be nice to serialize
191 		 * that printout, we really want to make sure that if some
192 		 * other CPU somehow locked up while holding the lock associated
193 		 * with printk_cpu_sync_get_irqsave() that we can still at least
194 		 * get the message about the lockup out.
195 		 */
196 		pr_emerg("Watchdog detected hard LOCKUP on cpu %d\n", cpu);
197 		printk_cpu_sync_get_irqsave(flags);
198 
199 		print_modules();
200 		print_irqtrace_events(current);
201 		if (cpu == this_cpu) {
202 			if (regs)
203 				show_regs(regs);
204 			else
205 				dump_stack();
206 			printk_cpu_sync_put_irqrestore(flags);
207 		} else {
208 			printk_cpu_sync_put_irqrestore(flags);
209 			trigger_single_cpu_backtrace(cpu);
210 		}
211 
212 		if (sysctl_hardlockup_all_cpu_backtrace) {
213 			trigger_allbutcpu_cpu_backtrace(cpu);
214 			if (!hardlockup_panic)
215 				clear_bit_unlock(0, &hard_lockup_nmi_warn);
216 		}
217 
218 		if (hardlockup_panic)
219 			nmi_panic(regs, "Hard LOCKUP");
220 
221 		per_cpu(watchdog_hardlockup_warned, cpu) = true;
222 	} else {
223 		per_cpu(watchdog_hardlockup_warned, cpu) = false;
224 	}
225 }
226 
227 #else /* CONFIG_HARDLOCKUP_DETECTOR_COUNTS_HRTIMER */
228 
watchdog_hardlockup_kick(void)229 static inline void watchdog_hardlockup_kick(void) { }
230 
231 #endif /* !CONFIG_HARDLOCKUP_DETECTOR_COUNTS_HRTIMER */
232 
233 /*
234  * These functions can be overridden based on the configured hardlockdup detector.
235  *
236  * watchdog_hardlockup_enable/disable can be implemented to start and stop when
237  * softlockup watchdog start and stop. The detector must select the
238  * SOFTLOCKUP_DETECTOR Kconfig.
239  */
watchdog_hardlockup_enable(unsigned int cpu)240 void __weak watchdog_hardlockup_enable(unsigned int cpu) { }
241 
watchdog_hardlockup_disable(unsigned int cpu)242 void __weak watchdog_hardlockup_disable(unsigned int cpu) { }
243 
244 /*
245  * Watchdog-detector specific API.
246  *
247  * Return 0 when hardlockup watchdog is available, negative value otherwise.
248  * Note that the negative value means that a delayed probe might
249  * succeed later.
250  */
watchdog_hardlockup_probe(void)251 int __weak __init watchdog_hardlockup_probe(void)
252 {
253 	return -ENODEV;
254 }
255 
256 /**
257  * watchdog_hardlockup_stop - Stop the watchdog for reconfiguration
258  *
259  * The reconfiguration steps are:
260  * watchdog_hardlockup_stop();
261  * update_variables();
262  * watchdog_hardlockup_start();
263  */
watchdog_hardlockup_stop(void)264 void __weak watchdog_hardlockup_stop(void) { }
265 
266 /**
267  * watchdog_hardlockup_start - Start the watchdog after reconfiguration
268  *
269  * Counterpart to watchdog_hardlockup_stop().
270  *
271  * The following variables have been updated in update_variables() and
272  * contain the currently valid configuration:
273  * - watchdog_enabled
274  * - watchdog_thresh
275  * - watchdog_cpumask
276  */
watchdog_hardlockup_start(void)277 void __weak watchdog_hardlockup_start(void) { }
278 
279 /**
280  * lockup_detector_update_enable - Update the sysctl enable bit
281  *
282  * Caller needs to make sure that the hard watchdogs are off, so this
283  * can't race with watchdog_hardlockup_disable().
284  */
lockup_detector_update_enable(void)285 static void lockup_detector_update_enable(void)
286 {
287 	watchdog_enabled = 0;
288 	if (!watchdog_user_enabled)
289 		return;
290 	if (watchdog_hardlockup_available && watchdog_hardlockup_user_enabled)
291 		watchdog_enabled |= WATCHDOG_HARDLOCKUP_ENABLED;
292 	if (watchdog_softlockup_user_enabled)
293 		watchdog_enabled |= WATCHDOG_SOFTOCKUP_ENABLED;
294 }
295 
296 #ifdef CONFIG_SOFTLOCKUP_DETECTOR
297 
298 /*
299  * Delay the soflockup report when running a known slow code.
300  * It does _not_ affect the timestamp of the last successdul reschedule.
301  */
302 #define SOFTLOCKUP_DELAY_REPORT	ULONG_MAX
303 
304 #ifdef CONFIG_SMP
305 int __read_mostly sysctl_softlockup_all_cpu_backtrace;
306 #endif
307 
308 static struct cpumask watchdog_allowed_mask __read_mostly;
309 
310 /* Global variables, exported for sysctl */
311 unsigned int __read_mostly softlockup_panic =
312 			IS_ENABLED(CONFIG_BOOTPARAM_SOFTLOCKUP_PANIC);
313 
314 static bool softlockup_initialized __read_mostly;
315 static u64 __read_mostly sample_period;
316 
317 /* Timestamp taken after the last successful reschedule. */
318 static DEFINE_PER_CPU(unsigned long, watchdog_touch_ts);
319 /* Timestamp of the last softlockup report. */
320 static DEFINE_PER_CPU(unsigned long, watchdog_report_ts);
321 static DEFINE_PER_CPU(struct hrtimer, watchdog_hrtimer);
322 static DEFINE_PER_CPU(bool, softlockup_touch_sync);
323 static unsigned long soft_lockup_nmi_warn;
324 
softlockup_panic_setup(char * str)325 static int __init softlockup_panic_setup(char *str)
326 {
327 	softlockup_panic = simple_strtoul(str, NULL, 0);
328 	return 1;
329 }
330 __setup("softlockup_panic=", softlockup_panic_setup);
331 
nowatchdog_setup(char * str)332 static int __init nowatchdog_setup(char *str)
333 {
334 	watchdog_user_enabled = 0;
335 	return 1;
336 }
337 __setup("nowatchdog", nowatchdog_setup);
338 
nosoftlockup_setup(char * str)339 static int __init nosoftlockup_setup(char *str)
340 {
341 	watchdog_softlockup_user_enabled = 0;
342 	return 1;
343 }
344 __setup("nosoftlockup", nosoftlockup_setup);
345 
watchdog_thresh_setup(char * str)346 static int __init watchdog_thresh_setup(char *str)
347 {
348 	get_option(&str, &watchdog_thresh);
349 	return 1;
350 }
351 __setup("watchdog_thresh=", watchdog_thresh_setup);
352 
353 #ifdef CONFIG_SOFTLOCKUP_DETECTOR_INTR_STORM
354 enum stats_per_group {
355 	STATS_SYSTEM,
356 	STATS_SOFTIRQ,
357 	STATS_HARDIRQ,
358 	STATS_IDLE,
359 	NUM_STATS_PER_GROUP,
360 };
361 
362 static const enum cpu_usage_stat tracked_stats[NUM_STATS_PER_GROUP] = {
363 	CPUTIME_SYSTEM,
364 	CPUTIME_SOFTIRQ,
365 	CPUTIME_IRQ,
366 	CPUTIME_IDLE,
367 };
368 
369 static DEFINE_PER_CPU(u16, cpustat_old[NUM_STATS_PER_GROUP]);
370 static DEFINE_PER_CPU(u8, cpustat_util[NUM_SAMPLE_PERIODS][NUM_STATS_PER_GROUP]);
371 static DEFINE_PER_CPU(u8, cpustat_tail);
372 
373 /*
374  * We don't need nanosecond resolution. A granularity of 16ms is
375  * sufficient for our precision, allowing us to use u16 to store
376  * cpustats, which will roll over roughly every ~1000 seconds.
377  * 2^24 ~= 16 * 10^6
378  */
get_16bit_precision(u64 data_ns)379 static u16 get_16bit_precision(u64 data_ns)
380 {
381 	return data_ns >> 24LL; /* 2^24ns ~= 16.8ms */
382 }
383 
update_cpustat(void)384 static void update_cpustat(void)
385 {
386 	int i;
387 	u8 util;
388 	u16 old_stat, new_stat;
389 	struct kernel_cpustat kcpustat;
390 	u64 *cpustat = kcpustat.cpustat;
391 	u8 tail = __this_cpu_read(cpustat_tail);
392 	u16 sample_period_16 = get_16bit_precision(sample_period);
393 
394 	kcpustat_cpu_fetch(&kcpustat, smp_processor_id());
395 
396 	for (i = 0; i < NUM_STATS_PER_GROUP; i++) {
397 		old_stat = __this_cpu_read(cpustat_old[i]);
398 		new_stat = get_16bit_precision(cpustat[tracked_stats[i]]);
399 		util = DIV_ROUND_UP(100 * (new_stat - old_stat), sample_period_16);
400 		__this_cpu_write(cpustat_util[tail][i], util);
401 		__this_cpu_write(cpustat_old[i], new_stat);
402 	}
403 
404 	__this_cpu_write(cpustat_tail, (tail + 1) % NUM_SAMPLE_PERIODS);
405 }
406 
print_cpustat(void)407 static void print_cpustat(void)
408 {
409 	int i, group;
410 	u8 tail = __this_cpu_read(cpustat_tail);
411 	u64 sample_period_second = sample_period;
412 
413 	do_div(sample_period_second, NSEC_PER_SEC);
414 
415 	/*
416 	 * Outputting the "watchdog" prefix on every line is redundant and not
417 	 * concise, and the original alarm information is sufficient for
418 	 * positioning in logs, hence here printk() is used instead of pr_crit().
419 	 */
420 	printk(KERN_CRIT "CPU#%d Utilization every %llus during lockup:\n",
421 	       smp_processor_id(), sample_period_second);
422 
423 	for (i = 0; i < NUM_SAMPLE_PERIODS; i++) {
424 		group = (tail + i) % NUM_SAMPLE_PERIODS;
425 		printk(KERN_CRIT "\t#%d: %3u%% system,\t%3u%% softirq,\t"
426 			"%3u%% hardirq,\t%3u%% idle\n", i + 1,
427 			__this_cpu_read(cpustat_util[group][STATS_SYSTEM]),
428 			__this_cpu_read(cpustat_util[group][STATS_SOFTIRQ]),
429 			__this_cpu_read(cpustat_util[group][STATS_HARDIRQ]),
430 			__this_cpu_read(cpustat_util[group][STATS_IDLE]));
431 	}
432 }
433 
434 #define HARDIRQ_PERCENT_THRESH          50
435 #define NUM_HARDIRQ_REPORT              5
436 struct irq_counts {
437 	int irq;
438 	u32 counts;
439 };
440 
441 static DEFINE_PER_CPU(bool, snapshot_taken);
442 
443 /* Tabulate the most frequent interrupts. */
tabulate_irq_count(struct irq_counts * irq_counts,int irq,u32 counts,int rank)444 static void tabulate_irq_count(struct irq_counts *irq_counts, int irq, u32 counts, int rank)
445 {
446 	int i;
447 	struct irq_counts new_count = {irq, counts};
448 
449 	for (i = 0; i < rank; i++) {
450 		if (counts > irq_counts[i].counts)
451 			swap(new_count, irq_counts[i]);
452 	}
453 }
454 
455 /*
456  * If the hardirq time exceeds HARDIRQ_PERCENT_THRESH% of the sample_period,
457  * then the cause of softlockup might be interrupt storm. In this case, it
458  * would be useful to start interrupt counting.
459  */
need_counting_irqs(void)460 static bool need_counting_irqs(void)
461 {
462 	u8 util;
463 	int tail = __this_cpu_read(cpustat_tail);
464 
465 	tail = (tail + NUM_HARDIRQ_REPORT - 1) % NUM_HARDIRQ_REPORT;
466 	util = __this_cpu_read(cpustat_util[tail][STATS_HARDIRQ]);
467 	return util > HARDIRQ_PERCENT_THRESH;
468 }
469 
start_counting_irqs(void)470 static void start_counting_irqs(void)
471 {
472 	if (!__this_cpu_read(snapshot_taken)) {
473 		kstat_snapshot_irqs();
474 		__this_cpu_write(snapshot_taken, true);
475 	}
476 }
477 
stop_counting_irqs(void)478 static void stop_counting_irqs(void)
479 {
480 	__this_cpu_write(snapshot_taken, false);
481 }
482 
print_irq_counts(void)483 static void print_irq_counts(void)
484 {
485 	unsigned int i, count;
486 	struct irq_counts irq_counts_sorted[NUM_HARDIRQ_REPORT] = {
487 		{-1, 0}, {-1, 0}, {-1, 0}, {-1, 0}, {-1, 0}
488 	};
489 
490 	if (__this_cpu_read(snapshot_taken)) {
491 		for_each_active_irq(i) {
492 			count = kstat_get_irq_since_snapshot(i);
493 			tabulate_irq_count(irq_counts_sorted, i, count, NUM_HARDIRQ_REPORT);
494 		}
495 
496 		/*
497 		 * Outputting the "watchdog" prefix on every line is redundant and not
498 		 * concise, and the original alarm information is sufficient for
499 		 * positioning in logs, hence here printk() is used instead of pr_crit().
500 		 */
501 		printk(KERN_CRIT "CPU#%d Detect HardIRQ Time exceeds %d%%. Most frequent HardIRQs:\n",
502 		       smp_processor_id(), HARDIRQ_PERCENT_THRESH);
503 
504 		for (i = 0; i < NUM_HARDIRQ_REPORT; i++) {
505 			if (irq_counts_sorted[i].irq == -1)
506 				break;
507 
508 			printk(KERN_CRIT "\t#%u: %-10u\tirq#%d\n",
509 			       i + 1, irq_counts_sorted[i].counts,
510 			       irq_counts_sorted[i].irq);
511 		}
512 
513 		/*
514 		 * If the hardirq time is less than HARDIRQ_PERCENT_THRESH% in the last
515 		 * sample_period, then we suspect the interrupt storm might be subsiding.
516 		 */
517 		if (!need_counting_irqs())
518 			stop_counting_irqs();
519 	}
520 }
521 
report_cpu_status(void)522 static void report_cpu_status(void)
523 {
524 	print_cpustat();
525 	print_irq_counts();
526 }
527 #else
update_cpustat(void)528 static inline void update_cpustat(void) { }
report_cpu_status(void)529 static inline void report_cpu_status(void) { }
need_counting_irqs(void)530 static inline bool need_counting_irqs(void) { return false; }
start_counting_irqs(void)531 static inline void start_counting_irqs(void) { }
stop_counting_irqs(void)532 static inline void stop_counting_irqs(void) { }
533 #endif
534 
535 /*
536  * Hard-lockup warnings should be triggered after just a few seconds. Soft-
537  * lockups can have false positives under extreme conditions. So we generally
538  * want a higher threshold for soft lockups than for hard lockups. So we couple
539  * the thresholds with a factor: we make the soft threshold twice the amount of
540  * time the hard threshold is.
541  */
get_softlockup_thresh(void)542 static int get_softlockup_thresh(void)
543 {
544 	return watchdog_thresh * 2;
545 }
546 
547 /*
548  * Returns seconds, approximately.  We don't need nanosecond
549  * resolution, and we don't need to waste time with a big divide when
550  * 2^30ns == 1.074s.
551  */
get_timestamp(void)552 static unsigned long get_timestamp(void)
553 {
554 	return running_clock() >> 30LL;  /* 2^30 ~= 10^9 */
555 }
556 
set_sample_period(void)557 static void set_sample_period(void)
558 {
559 	/*
560 	 * convert watchdog_thresh from seconds to ns
561 	 * the divide by 5 is to give hrtimer several chances (two
562 	 * or three with the current relation between the soft
563 	 * and hard thresholds) to increment before the
564 	 * hardlockup detector generates a warning
565 	 */
566 	sample_period = get_softlockup_thresh() * ((u64)NSEC_PER_SEC / NUM_SAMPLE_PERIODS);
567 	watchdog_update_hrtimer_threshold(sample_period);
568 }
569 
update_report_ts(void)570 static void update_report_ts(void)
571 {
572 	__this_cpu_write(watchdog_report_ts, get_timestamp());
573 }
574 
575 /* Commands for resetting the watchdog */
update_touch_ts(void)576 static void update_touch_ts(void)
577 {
578 	__this_cpu_write(watchdog_touch_ts, get_timestamp());
579 	update_report_ts();
580 }
581 
582 /**
583  * touch_softlockup_watchdog_sched - touch watchdog on scheduler stalls
584  *
585  * Call when the scheduler may have stalled for legitimate reasons
586  * preventing the watchdog task from executing - e.g. the scheduler
587  * entering idle state.  This should only be used for scheduler events.
588  * Use touch_softlockup_watchdog() for everything else.
589  */
touch_softlockup_watchdog_sched(void)590 notrace void touch_softlockup_watchdog_sched(void)
591 {
592 	/*
593 	 * Preemption can be enabled.  It doesn't matter which CPU's watchdog
594 	 * report period gets restarted here, so use the raw_ operation.
595 	 */
596 	raw_cpu_write(watchdog_report_ts, SOFTLOCKUP_DELAY_REPORT);
597 }
598 
touch_softlockup_watchdog(void)599 notrace void touch_softlockup_watchdog(void)
600 {
601 	touch_softlockup_watchdog_sched();
602 	wq_watchdog_touch(raw_smp_processor_id());
603 }
604 EXPORT_SYMBOL(touch_softlockup_watchdog);
605 
touch_all_softlockup_watchdogs(void)606 void touch_all_softlockup_watchdogs(void)
607 {
608 	int cpu;
609 
610 	/*
611 	 * watchdog_mutex cannpt be taken here, as this might be called
612 	 * from (soft)interrupt context, so the access to
613 	 * watchdog_allowed_cpumask might race with a concurrent update.
614 	 *
615 	 * The watchdog time stamp can race against a concurrent real
616 	 * update as well, the only side effect might be a cycle delay for
617 	 * the softlockup check.
618 	 */
619 	for_each_cpu(cpu, &watchdog_allowed_mask) {
620 		per_cpu(watchdog_report_ts, cpu) = SOFTLOCKUP_DELAY_REPORT;
621 		wq_watchdog_touch(cpu);
622 	}
623 }
624 
touch_softlockup_watchdog_sync(void)625 void touch_softlockup_watchdog_sync(void)
626 {
627 	__this_cpu_write(softlockup_touch_sync, true);
628 	__this_cpu_write(watchdog_report_ts, SOFTLOCKUP_DELAY_REPORT);
629 }
630 
is_softlockup(unsigned long touch_ts,unsigned long period_ts,unsigned long now)631 static int is_softlockup(unsigned long touch_ts,
632 			 unsigned long period_ts,
633 			 unsigned long now)
634 {
635 	if ((watchdog_enabled & WATCHDOG_SOFTOCKUP_ENABLED) && watchdog_thresh) {
636 		/*
637 		 * If period_ts has not been updated during a sample_period, then
638 		 * in the subsequent few sample_periods, period_ts might also not
639 		 * be updated, which could indicate a potential softlockup. In
640 		 * this case, if we suspect the cause of the potential softlockup
641 		 * might be interrupt storm, then we need to count the interrupts
642 		 * to find which interrupt is storming.
643 		 */
644 		if (time_after_eq(now, period_ts + get_softlockup_thresh() / NUM_SAMPLE_PERIODS) &&
645 		    need_counting_irqs())
646 			start_counting_irqs();
647 
648 		/* Warn about unreasonable delays. */
649 		if (time_after(now, period_ts + get_softlockup_thresh()))
650 			return now - touch_ts;
651 	}
652 	return 0;
653 }
654 
655 /* watchdog detector functions */
656 static DEFINE_PER_CPU(struct completion, softlockup_completion);
657 static DEFINE_PER_CPU(struct cpu_stop_work, softlockup_stop_work);
658 
659 /*
660  * The watchdog feed function - touches the timestamp.
661  *
662  * It only runs once every sample_period seconds (4 seconds by
663  * default) to reset the softlockup timestamp. If this gets delayed
664  * for more than 2*watchdog_thresh seconds then the debug-printout
665  * triggers in watchdog_timer_fn().
666  */
softlockup_fn(void * data)667 static int softlockup_fn(void *data)
668 {
669 	update_touch_ts();
670 	stop_counting_irqs();
671 	complete(this_cpu_ptr(&softlockup_completion));
672 
673 	return 0;
674 }
675 
676 /* watchdog kicker functions */
watchdog_timer_fn(struct hrtimer * hrtimer)677 static enum hrtimer_restart watchdog_timer_fn(struct hrtimer *hrtimer)
678 {
679 	unsigned long touch_ts, period_ts, now;
680 	struct pt_regs *regs = get_irq_regs();
681 	int duration;
682 	int softlockup_all_cpu_backtrace = sysctl_softlockup_all_cpu_backtrace;
683 	unsigned long flags;
684 
685 	if (!watchdog_enabled)
686 		return HRTIMER_NORESTART;
687 
688 	watchdog_hardlockup_kick();
689 
690 	/* kick the softlockup detector */
691 	if (completion_done(this_cpu_ptr(&softlockup_completion))) {
692 		reinit_completion(this_cpu_ptr(&softlockup_completion));
693 		stop_one_cpu_nowait(smp_processor_id(),
694 				softlockup_fn, NULL,
695 				this_cpu_ptr(&softlockup_stop_work));
696 	}
697 
698 	/* .. and repeat */
699 	hrtimer_forward_now(hrtimer, ns_to_ktime(sample_period));
700 
701 	/*
702 	 * Read the current timestamp first. It might become invalid anytime
703 	 * when a virtual machine is stopped by the host or when the watchog
704 	 * is touched from NMI.
705 	 */
706 	now = get_timestamp();
707 	/*
708 	 * If a virtual machine is stopped by the host it can look to
709 	 * the watchdog like a soft lockup. This function touches the watchdog.
710 	 */
711 	kvm_check_and_clear_guest_paused();
712 	/*
713 	 * The stored timestamp is comparable with @now only when not touched.
714 	 * It might get touched anytime from NMI. Make sure that is_softlockup()
715 	 * uses the same (valid) value.
716 	 */
717 	period_ts = READ_ONCE(*this_cpu_ptr(&watchdog_report_ts));
718 
719 	update_cpustat();
720 
721 	/* Reset the interval when touched by known problematic code. */
722 	if (period_ts == SOFTLOCKUP_DELAY_REPORT) {
723 		if (unlikely(__this_cpu_read(softlockup_touch_sync))) {
724 			/*
725 			 * If the time stamp was touched atomically
726 			 * make sure the scheduler tick is up to date.
727 			 */
728 			__this_cpu_write(softlockup_touch_sync, false);
729 			sched_clock_tick();
730 		}
731 
732 		update_report_ts();
733 		return HRTIMER_RESTART;
734 	}
735 
736 	/* Check for a softlockup. */
737 	touch_ts = __this_cpu_read(watchdog_touch_ts);
738 	duration = is_softlockup(touch_ts, period_ts, now);
739 	if (unlikely(duration)) {
740 		/*
741 		 * Prevent multiple soft-lockup reports if one cpu is already
742 		 * engaged in dumping all cpu back traces.
743 		 */
744 		if (softlockup_all_cpu_backtrace) {
745 			if (test_and_set_bit_lock(0, &soft_lockup_nmi_warn))
746 				return HRTIMER_RESTART;
747 		}
748 
749 		/* Start period for the next softlockup warning. */
750 		update_report_ts();
751 
752 		printk_cpu_sync_get_irqsave(flags);
753 		pr_emerg("BUG: soft lockup - CPU#%d stuck for %us! [%s:%d]\n",
754 			smp_processor_id(), duration,
755 			current->comm, task_pid_nr(current));
756 		report_cpu_status();
757 		print_modules();
758 		print_irqtrace_events(current);
759 		if (regs)
760 			show_regs(regs);
761 		else
762 			dump_stack();
763 		printk_cpu_sync_put_irqrestore(flags);
764 
765 		if (softlockup_all_cpu_backtrace) {
766 			trigger_allbutcpu_cpu_backtrace(smp_processor_id());
767 			if (!softlockup_panic)
768 				clear_bit_unlock(0, &soft_lockup_nmi_warn);
769 		}
770 
771 		trace_android_vh_watchdog_timer_softlockup(duration, regs, !!softlockup_panic);
772 		add_taint(TAINT_SOFTLOCKUP, LOCKDEP_STILL_OK);
773 		if (softlockup_panic)
774 			panic("softlockup: hung tasks");
775 	}
776 
777 	return HRTIMER_RESTART;
778 }
779 
watchdog_enable(unsigned int cpu)780 static void watchdog_enable(unsigned int cpu)
781 {
782 	struct hrtimer *hrtimer = this_cpu_ptr(&watchdog_hrtimer);
783 	struct completion *done = this_cpu_ptr(&softlockup_completion);
784 
785 	WARN_ON_ONCE(cpu != smp_processor_id());
786 
787 	init_completion(done);
788 	complete(done);
789 
790 	/*
791 	 * Start the timer first to prevent the hardlockup watchdog triggering
792 	 * before the timer has a chance to fire.
793 	 */
794 	hrtimer_init(hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_HARD);
795 	hrtimer->function = watchdog_timer_fn;
796 	hrtimer_start(hrtimer, ns_to_ktime(sample_period),
797 		      HRTIMER_MODE_REL_PINNED_HARD);
798 
799 	/* Initialize timestamp */
800 	update_touch_ts();
801 	/* Enable the hardlockup detector */
802 	if (watchdog_enabled & WATCHDOG_HARDLOCKUP_ENABLED)
803 		watchdog_hardlockup_enable(cpu);
804 }
805 
watchdog_disable(unsigned int cpu)806 static void watchdog_disable(unsigned int cpu)
807 {
808 	struct hrtimer *hrtimer = this_cpu_ptr(&watchdog_hrtimer);
809 
810 	WARN_ON_ONCE(cpu != smp_processor_id());
811 
812 	/*
813 	 * Disable the hardlockup detector first. That prevents that a large
814 	 * delay between disabling the timer and disabling the hardlockup
815 	 * detector causes a false positive.
816 	 */
817 	watchdog_hardlockup_disable(cpu);
818 	hrtimer_cancel(hrtimer);
819 	wait_for_completion(this_cpu_ptr(&softlockup_completion));
820 }
821 
softlockup_stop_fn(void * data)822 static int softlockup_stop_fn(void *data)
823 {
824 	watchdog_disable(smp_processor_id());
825 	return 0;
826 }
827 
softlockup_stop_all(void)828 static void softlockup_stop_all(void)
829 {
830 	int cpu;
831 
832 	if (!softlockup_initialized)
833 		return;
834 
835 	for_each_cpu(cpu, &watchdog_allowed_mask)
836 		smp_call_on_cpu(cpu, softlockup_stop_fn, NULL, false);
837 
838 	cpumask_clear(&watchdog_allowed_mask);
839 }
840 
softlockup_start_fn(void * data)841 static int softlockup_start_fn(void *data)
842 {
843 	watchdog_enable(smp_processor_id());
844 	return 0;
845 }
846 
softlockup_start_all(void)847 static void softlockup_start_all(void)
848 {
849 	int cpu;
850 
851 	cpumask_copy(&watchdog_allowed_mask, &watchdog_cpumask);
852 	for_each_cpu(cpu, &watchdog_allowed_mask)
853 		smp_call_on_cpu(cpu, softlockup_start_fn, NULL, false);
854 }
855 
lockup_detector_online_cpu(unsigned int cpu)856 int lockup_detector_online_cpu(unsigned int cpu)
857 {
858 	if (cpumask_test_cpu(cpu, &watchdog_allowed_mask))
859 		watchdog_enable(cpu);
860 	return 0;
861 }
862 
lockup_detector_offline_cpu(unsigned int cpu)863 int lockup_detector_offline_cpu(unsigned int cpu)
864 {
865 	if (cpumask_test_cpu(cpu, &watchdog_allowed_mask))
866 		watchdog_disable(cpu);
867 	return 0;
868 }
869 
__lockup_detector_reconfigure(bool thresh_changed)870 static void __lockup_detector_reconfigure(bool thresh_changed)
871 {
872 	cpus_read_lock();
873 	watchdog_hardlockup_stop();
874 
875 	softlockup_stop_all();
876 	/*
877 	 * To prevent watchdog_timer_fn from using the old interval and
878 	 * the new watchdog_thresh at the same time, which could lead to
879 	 * false softlockup reports, it is necessary to update the
880 	 * watchdog_thresh after the softlockup is completed.
881 	 */
882 	if (thresh_changed)
883 		watchdog_thresh = READ_ONCE(watchdog_thresh_next);
884 	set_sample_period();
885 	lockup_detector_update_enable();
886 	if (watchdog_enabled && watchdog_thresh)
887 		softlockup_start_all();
888 
889 	watchdog_hardlockup_start();
890 	cpus_read_unlock();
891 }
892 
lockup_detector_reconfigure(void)893 void lockup_detector_reconfigure(void)
894 {
895 	mutex_lock(&watchdog_mutex);
896 	__lockup_detector_reconfigure(false);
897 	mutex_unlock(&watchdog_mutex);
898 }
899 
900 /*
901  * Create the watchdog infrastructure and configure the detector(s).
902  */
lockup_detector_setup(void)903 static __init void lockup_detector_setup(void)
904 {
905 	/*
906 	 * If sysctl is off and watchdog got disabled on the command line,
907 	 * nothing to do here.
908 	 */
909 	lockup_detector_update_enable();
910 
911 	if (!IS_ENABLED(CONFIG_SYSCTL) &&
912 	    !(watchdog_enabled && watchdog_thresh))
913 		return;
914 
915 	mutex_lock(&watchdog_mutex);
916 	__lockup_detector_reconfigure(false);
917 	softlockup_initialized = true;
918 	mutex_unlock(&watchdog_mutex);
919 }
920 
921 #else /* CONFIG_SOFTLOCKUP_DETECTOR */
__lockup_detector_reconfigure(bool thresh_changed)922 static void __lockup_detector_reconfigure(bool thresh_changed)
923 {
924 	cpus_read_lock();
925 	watchdog_hardlockup_stop();
926 	if (thresh_changed)
927 		watchdog_thresh = READ_ONCE(watchdog_thresh_next);
928 	lockup_detector_update_enable();
929 	watchdog_hardlockup_start();
930 	cpus_read_unlock();
931 }
lockup_detector_reconfigure(void)932 void lockup_detector_reconfigure(void)
933 {
934 	__lockup_detector_reconfigure(false);
935 }
lockup_detector_setup(void)936 static inline void lockup_detector_setup(void)
937 {
938 	__lockup_detector_reconfigure(false);
939 }
940 #endif /* !CONFIG_SOFTLOCKUP_DETECTOR */
941 
942 /**
943  * lockup_detector_soft_poweroff - Interface to stop lockup detector(s)
944  *
945  * Special interface for parisc. It prevents lockup detector warnings from
946  * the default pm_poweroff() function which busy loops forever.
947  */
lockup_detector_soft_poweroff(void)948 void lockup_detector_soft_poweroff(void)
949 {
950 	watchdog_enabled = 0;
951 }
952 
953 #ifdef CONFIG_SYSCTL
954 
955 /* Propagate any changes to the watchdog infrastructure */
proc_watchdog_update(bool thresh_changed)956 static void proc_watchdog_update(bool thresh_changed)
957 {
958 	/* Remove impossible cpus to keep sysctl output clean. */
959 	cpumask_and(&watchdog_cpumask, &watchdog_cpumask, cpu_possible_mask);
960 	__lockup_detector_reconfigure(thresh_changed);
961 }
962 
963 /*
964  * common function for watchdog, nmi_watchdog and soft_watchdog parameter
965  *
966  * caller             | table->data points to            | 'which'
967  * -------------------|----------------------------------|-------------------------------
968  * proc_watchdog      | watchdog_user_enabled            | WATCHDOG_HARDLOCKUP_ENABLED |
969  *                    |                                  | WATCHDOG_SOFTOCKUP_ENABLED
970  * -------------------|----------------------------------|-------------------------------
971  * proc_nmi_watchdog  | watchdog_hardlockup_user_enabled | WATCHDOG_HARDLOCKUP_ENABLED
972  * -------------------|----------------------------------|-------------------------------
973  * proc_soft_watchdog | watchdog_softlockup_user_enabled | WATCHDOG_SOFTOCKUP_ENABLED
974  */
proc_watchdog_common(int which,const struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos)975 static int proc_watchdog_common(int which, const struct ctl_table *table, int write,
976 				void *buffer, size_t *lenp, loff_t *ppos)
977 {
978 	int err, old, *param = table->data;
979 
980 	mutex_lock(&watchdog_mutex);
981 
982 	if (!write) {
983 		/*
984 		 * On read synchronize the userspace interface. This is a
985 		 * racy snapshot.
986 		 */
987 		*param = (watchdog_enabled & which) != 0;
988 		err = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
989 	} else {
990 		old = READ_ONCE(*param);
991 		err = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
992 		if (!err && old != READ_ONCE(*param))
993 			proc_watchdog_update(false);
994 	}
995 	mutex_unlock(&watchdog_mutex);
996 	return err;
997 }
998 
999 /*
1000  * /proc/sys/kernel/watchdog
1001  */
proc_watchdog(const struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos)1002 static int proc_watchdog(const struct ctl_table *table, int write,
1003 			 void *buffer, size_t *lenp, loff_t *ppos)
1004 {
1005 	return proc_watchdog_common(WATCHDOG_HARDLOCKUP_ENABLED |
1006 				    WATCHDOG_SOFTOCKUP_ENABLED,
1007 				    table, write, buffer, lenp, ppos);
1008 }
1009 
1010 /*
1011  * /proc/sys/kernel/nmi_watchdog
1012  */
proc_nmi_watchdog(const struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos)1013 static int proc_nmi_watchdog(const struct ctl_table *table, int write,
1014 			     void *buffer, size_t *lenp, loff_t *ppos)
1015 {
1016 	if (!watchdog_hardlockup_available && write)
1017 		return -ENOTSUPP;
1018 	return proc_watchdog_common(WATCHDOG_HARDLOCKUP_ENABLED,
1019 				    table, write, buffer, lenp, ppos);
1020 }
1021 
1022 #ifdef CONFIG_SOFTLOCKUP_DETECTOR
1023 /*
1024  * /proc/sys/kernel/soft_watchdog
1025  */
proc_soft_watchdog(const struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos)1026 static int proc_soft_watchdog(const struct ctl_table *table, int write,
1027 			      void *buffer, size_t *lenp, loff_t *ppos)
1028 {
1029 	return proc_watchdog_common(WATCHDOG_SOFTOCKUP_ENABLED,
1030 				    table, write, buffer, lenp, ppos);
1031 }
1032 #endif
1033 
1034 /*
1035  * /proc/sys/kernel/watchdog_thresh
1036  */
proc_watchdog_thresh(const struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos)1037 static int proc_watchdog_thresh(const struct ctl_table *table, int write,
1038 				void *buffer, size_t *lenp, loff_t *ppos)
1039 {
1040 	int err, old;
1041 
1042 	mutex_lock(&watchdog_mutex);
1043 
1044 	watchdog_thresh_next = READ_ONCE(watchdog_thresh);
1045 
1046 	old = watchdog_thresh_next;
1047 	err = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
1048 
1049 	if (!err && write && old != READ_ONCE(watchdog_thresh_next))
1050 		proc_watchdog_update(true);
1051 
1052 	mutex_unlock(&watchdog_mutex);
1053 	return err;
1054 }
1055 
1056 /*
1057  * The cpumask is the mask of possible cpus that the watchdog can run
1058  * on, not the mask of cpus it is actually running on.  This allows the
1059  * user to specify a mask that will include cpus that have not yet
1060  * been brought online, if desired.
1061  */
proc_watchdog_cpumask(const struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos)1062 static int proc_watchdog_cpumask(const struct ctl_table *table, int write,
1063 				 void *buffer, size_t *lenp, loff_t *ppos)
1064 {
1065 	int err;
1066 
1067 	mutex_lock(&watchdog_mutex);
1068 
1069 	err = proc_do_large_bitmap(table, write, buffer, lenp, ppos);
1070 	if (!err && write)
1071 		proc_watchdog_update(false);
1072 
1073 	mutex_unlock(&watchdog_mutex);
1074 	return err;
1075 }
1076 
1077 static const int sixty = 60;
1078 
1079 static struct ctl_table watchdog_sysctls[] = {
1080 	{
1081 		.procname       = "watchdog",
1082 		.data		= &watchdog_user_enabled,
1083 		.maxlen		= sizeof(int),
1084 		.mode		= 0644,
1085 		.proc_handler   = proc_watchdog,
1086 		.extra1		= SYSCTL_ZERO,
1087 		.extra2		= SYSCTL_ONE,
1088 	},
1089 	{
1090 		.procname	= "watchdog_thresh",
1091 		.data		= &watchdog_thresh_next,
1092 		.maxlen		= sizeof(int),
1093 		.mode		= 0644,
1094 		.proc_handler	= proc_watchdog_thresh,
1095 		.extra1		= SYSCTL_ZERO,
1096 		.extra2		= (void *)&sixty,
1097 	},
1098 	{
1099 		.procname	= "watchdog_cpumask",
1100 		.data		= &watchdog_cpumask_bits,
1101 		.maxlen		= NR_CPUS,
1102 		.mode		= 0644,
1103 		.proc_handler	= proc_watchdog_cpumask,
1104 	},
1105 #ifdef CONFIG_SOFTLOCKUP_DETECTOR
1106 	{
1107 		.procname       = "soft_watchdog",
1108 		.data		= &watchdog_softlockup_user_enabled,
1109 		.maxlen		= sizeof(int),
1110 		.mode		= 0644,
1111 		.proc_handler   = proc_soft_watchdog,
1112 		.extra1		= SYSCTL_ZERO,
1113 		.extra2		= SYSCTL_ONE,
1114 	},
1115 	{
1116 		.procname	= "softlockup_panic",
1117 		.data		= &softlockup_panic,
1118 		.maxlen		= sizeof(int),
1119 		.mode		= 0644,
1120 		.proc_handler	= proc_dointvec_minmax,
1121 		.extra1		= SYSCTL_ZERO,
1122 		.extra2		= SYSCTL_ONE,
1123 	},
1124 #ifdef CONFIG_SMP
1125 	{
1126 		.procname	= "softlockup_all_cpu_backtrace",
1127 		.data		= &sysctl_softlockup_all_cpu_backtrace,
1128 		.maxlen		= sizeof(int),
1129 		.mode		= 0644,
1130 		.proc_handler	= proc_dointvec_minmax,
1131 		.extra1		= SYSCTL_ZERO,
1132 		.extra2		= SYSCTL_ONE,
1133 	},
1134 #endif /* CONFIG_SMP */
1135 #endif
1136 #ifdef CONFIG_HARDLOCKUP_DETECTOR
1137 	{
1138 		.procname	= "hardlockup_panic",
1139 		.data		= &hardlockup_panic,
1140 		.maxlen		= sizeof(int),
1141 		.mode		= 0644,
1142 		.proc_handler	= proc_dointvec_minmax,
1143 		.extra1		= SYSCTL_ZERO,
1144 		.extra2		= SYSCTL_ONE,
1145 	},
1146 #ifdef CONFIG_SMP
1147 	{
1148 		.procname	= "hardlockup_all_cpu_backtrace",
1149 		.data		= &sysctl_hardlockup_all_cpu_backtrace,
1150 		.maxlen		= sizeof(int),
1151 		.mode		= 0644,
1152 		.proc_handler	= proc_dointvec_minmax,
1153 		.extra1		= SYSCTL_ZERO,
1154 		.extra2		= SYSCTL_ONE,
1155 	},
1156 #endif /* CONFIG_SMP */
1157 #endif
1158 };
1159 
1160 static struct ctl_table watchdog_hardlockup_sysctl[] = {
1161 	{
1162 		.procname       = "nmi_watchdog",
1163 		.data		= &watchdog_hardlockup_user_enabled,
1164 		.maxlen		= sizeof(int),
1165 		.mode		= 0444,
1166 		.proc_handler   = proc_nmi_watchdog,
1167 		.extra1		= SYSCTL_ZERO,
1168 		.extra2		= SYSCTL_ONE,
1169 	},
1170 };
1171 
watchdog_sysctl_init(void)1172 static void __init watchdog_sysctl_init(void)
1173 {
1174 	register_sysctl_init("kernel", watchdog_sysctls);
1175 
1176 	if (watchdog_hardlockup_available)
1177 		watchdog_hardlockup_sysctl[0].mode = 0644;
1178 	register_sysctl_init("kernel", watchdog_hardlockup_sysctl);
1179 }
1180 
1181 #else
1182 #define watchdog_sysctl_init() do { } while (0)
1183 #endif /* CONFIG_SYSCTL */
1184 
1185 static void __init lockup_detector_delay_init(struct work_struct *work);
1186 static bool allow_lockup_detector_init_retry __initdata;
1187 
1188 static struct work_struct detector_work __initdata =
1189 		__WORK_INITIALIZER(detector_work, lockup_detector_delay_init);
1190 
lockup_detector_delay_init(struct work_struct * work)1191 static void __init lockup_detector_delay_init(struct work_struct *work)
1192 {
1193 	int ret;
1194 
1195 	ret = watchdog_hardlockup_probe();
1196 	if (ret) {
1197 		if (ret == -ENODEV)
1198 			pr_info("NMI not fully supported\n");
1199 		else
1200 			pr_info("Delayed init of the lockup detector failed: %d\n", ret);
1201 		pr_info("Hard watchdog permanently disabled\n");
1202 		return;
1203 	}
1204 
1205 	allow_lockup_detector_init_retry = false;
1206 
1207 	watchdog_hardlockup_available = true;
1208 	lockup_detector_setup();
1209 }
1210 
1211 /*
1212  * lockup_detector_retry_init - retry init lockup detector if possible.
1213  *
1214  * Retry hardlockup detector init. It is useful when it requires some
1215  * functionality that has to be initialized later on a particular
1216  * platform.
1217  */
lockup_detector_retry_init(void)1218 void __init lockup_detector_retry_init(void)
1219 {
1220 	/* Must be called before late init calls */
1221 	if (!allow_lockup_detector_init_retry)
1222 		return;
1223 
1224 	schedule_work(&detector_work);
1225 }
1226 
1227 /*
1228  * Ensure that optional delayed hardlockup init is proceed before
1229  * the init code and memory is freed.
1230  */
lockup_detector_check(void)1231 static int __init lockup_detector_check(void)
1232 {
1233 	/* Prevent any later retry. */
1234 	allow_lockup_detector_init_retry = false;
1235 
1236 	/* Make sure no work is pending. */
1237 	flush_work(&detector_work);
1238 
1239 	watchdog_sysctl_init();
1240 
1241 	return 0;
1242 
1243 }
1244 late_initcall_sync(lockup_detector_check);
1245 
lockup_detector_init(void)1246 void __init lockup_detector_init(void)
1247 {
1248 	if (tick_nohz_full_enabled())
1249 		pr_info("Disabling watchdog on nohz_full cores by default\n");
1250 
1251 	cpumask_copy(&watchdog_cpumask,
1252 		     housekeeping_cpumask(HK_TYPE_TIMER));
1253 
1254 	if (!watchdog_hardlockup_probe())
1255 		watchdog_hardlockup_available = true;
1256 	else
1257 		allow_lockup_detector_init_retry = true;
1258 
1259 	lockup_detector_setup();
1260 }
1261