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