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