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
hardlockup_count_show(struct kobject * kobj,struct kobj_attribute * attr,char * page)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
kernel_hardlockup_sysfs_init(void)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 */
hardlockup_detector_disable(void)98 void __init hardlockup_detector_disable(void)
99 {
100 watchdog_hardlockup_user_enabled = 0;
101 }
102
hardlockup_panic_setup(char * str)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
arch_touch_nmi_watchdog(void)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
watchdog_hardlockup_touch_cpu(unsigned int cpu)149 void watchdog_hardlockup_touch_cpu(unsigned int cpu)
150 {
151 per_cpu(watchdog_hardlockup_touched, cpu) = true;
152 }
153
is_hardlockup(unsigned int cpu)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
watchdog_hardlockup_kick(void)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
watchdog_hardlockup_check(unsigned int cpu,struct pt_regs * regs)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
watchdog_hardlockup_kick(void)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 */
watchdog_hardlockup_enable(unsigned int cpu)265 void __weak watchdog_hardlockup_enable(unsigned int cpu) { }
266
watchdog_hardlockup_disable(unsigned int cpu)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 */
watchdog_hardlockup_probe(void)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 */
watchdog_hardlockup_stop(void)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 */
watchdog_hardlockup_start(void)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 */
lockup_detector_update_enable(void)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
softlockup_count_show(struct kobject * kobj,struct kobj_attribute * attr,char * page)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
kernel_softlockup_sysfs_init(void)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
softlockup_panic_setup(char * str)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
nowatchdog_setup(char * str)379 static int __init nowatchdog_setup(char *str)
380 {
381 watchdog_user_enabled = 0;
382 return 1;
383 }
384 __setup("nowatchdog", nowatchdog_setup);
385
nosoftlockup_setup(char * str)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
watchdog_thresh_setup(char * str)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 */
get_16bit_precision(u64 data_ns)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
update_cpustat(void)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
print_cpustat(void)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. */
tabulate_irq_count(struct irq_counts * irq_counts,int irq,u32 counts,int rank)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 */
need_counting_irqs(void)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
start_counting_irqs(void)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
stop_counting_irqs(void)537 static void stop_counting_irqs(void)
538 {
539 __this_cpu_write(snapshot_taken, false);
540 }
541
print_irq_counts(void)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
report_cpu_status(void)581 static void report_cpu_status(void)
582 {
583 print_cpustat();
584 print_irq_counts();
585 }
586 #else
update_cpustat(void)587 static inline void update_cpustat(void) { }
report_cpu_status(void)588 static inline void report_cpu_status(void) { }
need_counting_irqs(void)589 static inline bool need_counting_irqs(void) { return false; }
start_counting_irqs(void)590 static inline void start_counting_irqs(void) { }
stop_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 */
get_softlockup_thresh(void)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 */
get_timestamp(void)611 static unsigned long get_timestamp(void)
612 {
613 return running_clock() >> 30LL; /* 2^30 ~= 10^9 */
614 }
615
set_sample_period(void)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
update_report_ts(void)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 */
update_touch_ts(void)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 */
touch_softlockup_watchdog_sched(void)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
touch_softlockup_watchdog(void)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
touch_all_softlockup_watchdogs(void)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
touch_softlockup_watchdog_sync(void)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
is_softlockup(unsigned long touch_ts,unsigned long period_ts,unsigned long now)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 */
softlockup_fn(void * data)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 */
watchdog_timer_fn(struct hrtimer * hrtimer)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 /*
756 * pass the buddy check if a panic is in process
757 */
758 if (panic_in_progress())
759 return HRTIMER_NORESTART;
760
761 watchdog_hardlockup_kick();
762
763 /* kick the softlockup detector */
764 if (completion_done(this_cpu_ptr(&softlockup_completion))) {
765 reinit_completion(this_cpu_ptr(&softlockup_completion));
766 stop_one_cpu_nowait(smp_processor_id(),
767 softlockup_fn, NULL,
768 this_cpu_ptr(&softlockup_stop_work));
769 }
770
771 /* .. and repeat */
772 hrtimer_forward_now(hrtimer, ns_to_ktime(sample_period));
773
774 /*
775 * Read the current timestamp first. It might become invalid anytime
776 * when a virtual machine is stopped by the host or when the watchog
777 * is touched from NMI.
778 */
779 now = get_timestamp();
780 /*
781 * If a virtual machine is stopped by the host it can look to
782 * the watchdog like a soft lockup. This function touches the watchdog.
783 */
784 kvm_check_and_clear_guest_paused();
785 /*
786 * The stored timestamp is comparable with @now only when not touched.
787 * It might get touched anytime from NMI. Make sure that is_softlockup()
788 * uses the same (valid) value.
789 */
790 period_ts = READ_ONCE(*this_cpu_ptr(&watchdog_report_ts));
791
792 update_cpustat();
793
794 /* Reset the interval when touched by known problematic code. */
795 if (period_ts == SOFTLOCKUP_DELAY_REPORT) {
796 if (unlikely(__this_cpu_read(softlockup_touch_sync))) {
797 /*
798 * If the time stamp was touched atomically
799 * make sure the scheduler tick is up to date.
800 */
801 __this_cpu_write(softlockup_touch_sync, false);
802 sched_clock_tick();
803 }
804
805 update_report_ts();
806 return HRTIMER_RESTART;
807 }
808
809 /* Check for a softlockup. */
810 touch_ts = __this_cpu_read(watchdog_touch_ts);
811 duration = is_softlockup(touch_ts, period_ts, now);
812 if (unlikely(duration)) {
813 #ifdef CONFIG_SYSFS
814 ++softlockup_count;
815 #endif
816
817 /*
818 * Prevent multiple soft-lockup reports if one cpu is already
819 * engaged in dumping all cpu back traces.
820 */
821 if (softlockup_all_cpu_backtrace) {
822 if (test_and_set_bit_lock(0, &soft_lockup_nmi_warn))
823 return HRTIMER_RESTART;
824 }
825
826 /* Start period for the next softlockup warning. */
827 update_report_ts();
828
829 printk_cpu_sync_get_irqsave(flags);
830 pr_emerg("BUG: soft lockup - CPU#%d stuck for %us! [%s:%d]\n",
831 smp_processor_id(), duration,
832 current->comm, task_pid_nr(current));
833 report_cpu_status();
834 print_modules();
835 print_irqtrace_events(current);
836 if (regs)
837 show_regs(regs);
838 else
839 dump_stack();
840 printk_cpu_sync_put_irqrestore(flags);
841
842 if (softlockup_all_cpu_backtrace) {
843 trigger_allbutcpu_cpu_backtrace(smp_processor_id());
844 if (!softlockup_panic)
845 clear_bit_unlock(0, &soft_lockup_nmi_warn);
846 }
847
848 add_taint(TAINT_SOFTLOCKUP, LOCKDEP_STILL_OK);
849 if (softlockup_panic)
850 panic("softlockup: hung tasks");
851 }
852
853 return HRTIMER_RESTART;
854 }
855
watchdog_enable(unsigned int cpu)856 static void watchdog_enable(unsigned int cpu)
857 {
858 struct hrtimer *hrtimer = this_cpu_ptr(&watchdog_hrtimer);
859 struct completion *done = this_cpu_ptr(&softlockup_completion);
860
861 WARN_ON_ONCE(cpu != smp_processor_id());
862
863 init_completion(done);
864 complete(done);
865
866 /*
867 * Start the timer first to prevent the hardlockup watchdog triggering
868 * before the timer has a chance to fire.
869 */
870 hrtimer_setup(hrtimer, watchdog_timer_fn, CLOCK_MONOTONIC, HRTIMER_MODE_REL_HARD);
871 hrtimer_start(hrtimer, ns_to_ktime(sample_period),
872 HRTIMER_MODE_REL_PINNED_HARD);
873
874 /* Initialize timestamp */
875 update_touch_ts();
876 /* Enable the hardlockup detector */
877 if (watchdog_enabled & WATCHDOG_HARDLOCKUP_ENABLED)
878 watchdog_hardlockup_enable(cpu);
879 }
880
watchdog_disable(unsigned int cpu)881 static void watchdog_disable(unsigned int cpu)
882 {
883 struct hrtimer *hrtimer = this_cpu_ptr(&watchdog_hrtimer);
884
885 WARN_ON_ONCE(cpu != smp_processor_id());
886
887 /*
888 * Disable the hardlockup detector first. That prevents that a large
889 * delay between disabling the timer and disabling the hardlockup
890 * detector causes a false positive.
891 */
892 watchdog_hardlockup_disable(cpu);
893 hrtimer_cancel(hrtimer);
894 wait_for_completion(this_cpu_ptr(&softlockup_completion));
895 }
896
softlockup_stop_fn(void * data)897 static int softlockup_stop_fn(void *data)
898 {
899 watchdog_disable(smp_processor_id());
900 return 0;
901 }
902
softlockup_stop_all(void)903 static void softlockup_stop_all(void)
904 {
905 int cpu;
906
907 if (!softlockup_initialized)
908 return;
909
910 for_each_cpu(cpu, &watchdog_allowed_mask)
911 smp_call_on_cpu(cpu, softlockup_stop_fn, NULL, false);
912
913 cpumask_clear(&watchdog_allowed_mask);
914 }
915
softlockup_start_fn(void * data)916 static int softlockup_start_fn(void *data)
917 {
918 watchdog_enable(smp_processor_id());
919 return 0;
920 }
921
softlockup_start_all(void)922 static void softlockup_start_all(void)
923 {
924 int cpu;
925
926 cpumask_copy(&watchdog_allowed_mask, &watchdog_cpumask);
927 for_each_cpu(cpu, &watchdog_allowed_mask)
928 smp_call_on_cpu(cpu, softlockup_start_fn, NULL, false);
929 }
930
lockup_detector_online_cpu(unsigned int cpu)931 int lockup_detector_online_cpu(unsigned int cpu)
932 {
933 if (cpumask_test_cpu(cpu, &watchdog_allowed_mask))
934 watchdog_enable(cpu);
935 return 0;
936 }
937
lockup_detector_offline_cpu(unsigned int cpu)938 int lockup_detector_offline_cpu(unsigned int cpu)
939 {
940 if (cpumask_test_cpu(cpu, &watchdog_allowed_mask))
941 watchdog_disable(cpu);
942 return 0;
943 }
944
__lockup_detector_reconfigure(bool thresh_changed)945 static void __lockup_detector_reconfigure(bool thresh_changed)
946 {
947 cpus_read_lock();
948 watchdog_hardlockup_stop();
949
950 softlockup_stop_all();
951 /*
952 * To prevent watchdog_timer_fn from using the old interval and
953 * the new watchdog_thresh at the same time, which could lead to
954 * false softlockup reports, it is necessary to update the
955 * watchdog_thresh after the softlockup is completed.
956 */
957 if (thresh_changed)
958 watchdog_thresh = READ_ONCE(watchdog_thresh_next);
959 set_sample_period();
960 lockup_detector_update_enable();
961 if (watchdog_enabled && watchdog_thresh)
962 softlockup_start_all();
963
964 watchdog_hardlockup_start();
965 cpus_read_unlock();
966 }
967
lockup_detector_reconfigure(void)968 void lockup_detector_reconfigure(void)
969 {
970 mutex_lock(&watchdog_mutex);
971 __lockup_detector_reconfigure(false);
972 mutex_unlock(&watchdog_mutex);
973 }
974
975 /*
976 * Create the watchdog infrastructure and configure the detector(s).
977 */
lockup_detector_setup(void)978 static __init void lockup_detector_setup(void)
979 {
980 /*
981 * If sysctl is off and watchdog got disabled on the command line,
982 * nothing to do here.
983 */
984 lockup_detector_update_enable();
985
986 if (!IS_ENABLED(CONFIG_SYSCTL) &&
987 !(watchdog_enabled && watchdog_thresh))
988 return;
989
990 mutex_lock(&watchdog_mutex);
991 __lockup_detector_reconfigure(false);
992 softlockup_initialized = true;
993 mutex_unlock(&watchdog_mutex);
994 }
995
996 #else /* CONFIG_SOFTLOCKUP_DETECTOR */
__lockup_detector_reconfigure(bool thresh_changed)997 static void __lockup_detector_reconfigure(bool thresh_changed)
998 {
999 cpus_read_lock();
1000 watchdog_hardlockup_stop();
1001 if (thresh_changed)
1002 watchdog_thresh = READ_ONCE(watchdog_thresh_next);
1003 lockup_detector_update_enable();
1004 watchdog_hardlockup_start();
1005 cpus_read_unlock();
1006 }
lockup_detector_reconfigure(void)1007 void lockup_detector_reconfigure(void)
1008 {
1009 __lockup_detector_reconfigure(false);
1010 }
lockup_detector_setup(void)1011 static inline void lockup_detector_setup(void)
1012 {
1013 __lockup_detector_reconfigure(false);
1014 }
1015 #endif /* !CONFIG_SOFTLOCKUP_DETECTOR */
1016
1017 /**
1018 * lockup_detector_soft_poweroff - Interface to stop lockup detector(s)
1019 *
1020 * Special interface for parisc. It prevents lockup detector warnings from
1021 * the default pm_poweroff() function which busy loops forever.
1022 */
lockup_detector_soft_poweroff(void)1023 void lockup_detector_soft_poweroff(void)
1024 {
1025 watchdog_enabled = 0;
1026 }
1027
1028 #ifdef CONFIG_SYSCTL
1029
1030 /* Propagate any changes to the watchdog infrastructure */
proc_watchdog_update(bool thresh_changed)1031 static void proc_watchdog_update(bool thresh_changed)
1032 {
1033 /* Remove impossible cpus to keep sysctl output clean. */
1034 cpumask_and(&watchdog_cpumask, &watchdog_cpumask, cpu_possible_mask);
1035 __lockup_detector_reconfigure(thresh_changed);
1036 }
1037
1038 /*
1039 * common function for watchdog, nmi_watchdog and soft_watchdog parameter
1040 *
1041 * caller | table->data points to | 'which'
1042 * -------------------|----------------------------------|-------------------------------
1043 * proc_watchdog | watchdog_user_enabled | WATCHDOG_HARDLOCKUP_ENABLED |
1044 * | | WATCHDOG_SOFTOCKUP_ENABLED
1045 * -------------------|----------------------------------|-------------------------------
1046 * proc_nmi_watchdog | watchdog_hardlockup_user_enabled | WATCHDOG_HARDLOCKUP_ENABLED
1047 * -------------------|----------------------------------|-------------------------------
1048 * proc_soft_watchdog | watchdog_softlockup_user_enabled | WATCHDOG_SOFTOCKUP_ENABLED
1049 */
proc_watchdog_common(int which,const struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos)1050 static int proc_watchdog_common(int which, const struct ctl_table *table, int write,
1051 void *buffer, size_t *lenp, loff_t *ppos)
1052 {
1053 int err, old, *param = table->data;
1054
1055 mutex_lock(&watchdog_mutex);
1056
1057 old = *param;
1058 if (!write) {
1059 /*
1060 * On read synchronize the userspace interface. This is a
1061 * racy snapshot.
1062 */
1063 *param = (watchdog_enabled & which) != 0;
1064 err = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
1065 *param = old;
1066 } else {
1067 err = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
1068 if (!err && old != READ_ONCE(*param))
1069 proc_watchdog_update(false);
1070 }
1071 mutex_unlock(&watchdog_mutex);
1072 return err;
1073 }
1074
1075 /*
1076 * /proc/sys/kernel/watchdog
1077 */
proc_watchdog(const struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos)1078 static int proc_watchdog(const struct ctl_table *table, int write,
1079 void *buffer, size_t *lenp, loff_t *ppos)
1080 {
1081 return proc_watchdog_common(WATCHDOG_HARDLOCKUP_ENABLED |
1082 WATCHDOG_SOFTOCKUP_ENABLED,
1083 table, write, buffer, lenp, ppos);
1084 }
1085
1086 /*
1087 * /proc/sys/kernel/nmi_watchdog
1088 */
proc_nmi_watchdog(const struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos)1089 static int proc_nmi_watchdog(const struct ctl_table *table, int write,
1090 void *buffer, size_t *lenp, loff_t *ppos)
1091 {
1092 if (!watchdog_hardlockup_available && write)
1093 return -ENOTSUPP;
1094 return proc_watchdog_common(WATCHDOG_HARDLOCKUP_ENABLED,
1095 table, write, buffer, lenp, ppos);
1096 }
1097
1098 #ifdef CONFIG_SOFTLOCKUP_DETECTOR
1099 /*
1100 * /proc/sys/kernel/soft_watchdog
1101 */
proc_soft_watchdog(const struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos)1102 static int proc_soft_watchdog(const struct ctl_table *table, int write,
1103 void *buffer, size_t *lenp, loff_t *ppos)
1104 {
1105 return proc_watchdog_common(WATCHDOG_SOFTOCKUP_ENABLED,
1106 table, write, buffer, lenp, ppos);
1107 }
1108 #endif
1109
1110 /*
1111 * /proc/sys/kernel/watchdog_thresh
1112 */
proc_watchdog_thresh(const struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos)1113 static int proc_watchdog_thresh(const struct ctl_table *table, int write,
1114 void *buffer, size_t *lenp, loff_t *ppos)
1115 {
1116 int err, old;
1117
1118 mutex_lock(&watchdog_mutex);
1119
1120 watchdog_thresh_next = READ_ONCE(watchdog_thresh);
1121
1122 old = watchdog_thresh_next;
1123 err = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
1124
1125 if (!err && write && old != READ_ONCE(watchdog_thresh_next))
1126 proc_watchdog_update(true);
1127
1128 mutex_unlock(&watchdog_mutex);
1129 return err;
1130 }
1131
1132 /*
1133 * The cpumask is the mask of possible cpus that the watchdog can run
1134 * on, not the mask of cpus it is actually running on. This allows the
1135 * user to specify a mask that will include cpus that have not yet
1136 * been brought online, if desired.
1137 */
proc_watchdog_cpumask(const struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos)1138 static int proc_watchdog_cpumask(const struct ctl_table *table, int write,
1139 void *buffer, size_t *lenp, loff_t *ppos)
1140 {
1141 int err;
1142
1143 mutex_lock(&watchdog_mutex);
1144
1145 err = proc_do_large_bitmap(table, write, buffer, lenp, ppos);
1146 if (!err && write)
1147 proc_watchdog_update(false);
1148
1149 mutex_unlock(&watchdog_mutex);
1150 return err;
1151 }
1152
1153 static const int sixty = 60;
1154
1155 static const struct ctl_table watchdog_sysctls[] = {
1156 {
1157 .procname = "watchdog",
1158 .data = &watchdog_user_enabled,
1159 .maxlen = sizeof(int),
1160 .mode = 0644,
1161 .proc_handler = proc_watchdog,
1162 .extra1 = SYSCTL_ZERO,
1163 .extra2 = SYSCTL_ONE,
1164 },
1165 {
1166 .procname = "watchdog_thresh",
1167 .data = &watchdog_thresh_next,
1168 .maxlen = sizeof(int),
1169 .mode = 0644,
1170 .proc_handler = proc_watchdog_thresh,
1171 .extra1 = SYSCTL_ZERO,
1172 .extra2 = (void *)&sixty,
1173 },
1174 {
1175 .procname = "watchdog_cpumask",
1176 .data = &watchdog_cpumask_bits,
1177 .maxlen = NR_CPUS,
1178 .mode = 0644,
1179 .proc_handler = proc_watchdog_cpumask,
1180 },
1181 #ifdef CONFIG_SOFTLOCKUP_DETECTOR
1182 {
1183 .procname = "soft_watchdog",
1184 .data = &watchdog_softlockup_user_enabled,
1185 .maxlen = sizeof(int),
1186 .mode = 0644,
1187 .proc_handler = proc_soft_watchdog,
1188 .extra1 = SYSCTL_ZERO,
1189 .extra2 = SYSCTL_ONE,
1190 },
1191 {
1192 .procname = "softlockup_panic",
1193 .data = &softlockup_panic,
1194 .maxlen = sizeof(int),
1195 .mode = 0644,
1196 .proc_handler = proc_dointvec_minmax,
1197 .extra1 = SYSCTL_ZERO,
1198 .extra2 = SYSCTL_ONE,
1199 },
1200 #ifdef CONFIG_SMP
1201 {
1202 .procname = "softlockup_all_cpu_backtrace",
1203 .data = &sysctl_softlockup_all_cpu_backtrace,
1204 .maxlen = sizeof(int),
1205 .mode = 0644,
1206 .proc_handler = proc_dointvec_minmax,
1207 .extra1 = SYSCTL_ZERO,
1208 .extra2 = SYSCTL_ONE,
1209 },
1210 #endif /* CONFIG_SMP */
1211 #endif
1212 #ifdef CONFIG_HARDLOCKUP_DETECTOR
1213 {
1214 .procname = "hardlockup_panic",
1215 .data = &hardlockup_panic,
1216 .maxlen = sizeof(int),
1217 .mode = 0644,
1218 .proc_handler = proc_dointvec_minmax,
1219 .extra1 = SYSCTL_ZERO,
1220 .extra2 = SYSCTL_ONE,
1221 },
1222 #ifdef CONFIG_SMP
1223 {
1224 .procname = "hardlockup_all_cpu_backtrace",
1225 .data = &sysctl_hardlockup_all_cpu_backtrace,
1226 .maxlen = sizeof(int),
1227 .mode = 0644,
1228 .proc_handler = proc_dointvec_minmax,
1229 .extra1 = SYSCTL_ZERO,
1230 .extra2 = SYSCTL_ONE,
1231 },
1232 #endif /* CONFIG_SMP */
1233 #endif
1234 };
1235
1236 static struct ctl_table watchdog_hardlockup_sysctl[] = {
1237 {
1238 .procname = "nmi_watchdog",
1239 .data = &watchdog_hardlockup_user_enabled,
1240 .maxlen = sizeof(int),
1241 .mode = 0444,
1242 .proc_handler = proc_nmi_watchdog,
1243 .extra1 = SYSCTL_ZERO,
1244 .extra2 = SYSCTL_ONE,
1245 },
1246 };
1247
watchdog_sysctl_init(void)1248 static void __init watchdog_sysctl_init(void)
1249 {
1250 register_sysctl_init("kernel", watchdog_sysctls);
1251
1252 if (watchdog_hardlockup_available)
1253 watchdog_hardlockup_sysctl[0].mode = 0644;
1254 register_sysctl_init("kernel", watchdog_hardlockup_sysctl);
1255 }
1256
1257 #else
1258 #define watchdog_sysctl_init() do { } while (0)
1259 #endif /* CONFIG_SYSCTL */
1260
1261 static void __init lockup_detector_delay_init(struct work_struct *work);
1262 static bool allow_lockup_detector_init_retry __initdata;
1263
1264 static struct work_struct detector_work __initdata =
1265 __WORK_INITIALIZER(detector_work, lockup_detector_delay_init);
1266
lockup_detector_delay_init(struct work_struct * work)1267 static void __init lockup_detector_delay_init(struct work_struct *work)
1268 {
1269 int ret;
1270
1271 ret = watchdog_hardlockup_probe();
1272 if (ret) {
1273 if (ret == -ENODEV)
1274 pr_info("NMI not fully supported\n");
1275 else
1276 pr_info("Delayed init of the lockup detector failed: %d\n", ret);
1277 pr_info("Hard watchdog permanently disabled\n");
1278 return;
1279 }
1280
1281 allow_lockup_detector_init_retry = false;
1282
1283 watchdog_hardlockup_available = true;
1284 lockup_detector_setup();
1285 }
1286
1287 /*
1288 * lockup_detector_retry_init - retry init lockup detector if possible.
1289 *
1290 * Retry hardlockup detector init. It is useful when it requires some
1291 * functionality that has to be initialized later on a particular
1292 * platform.
1293 */
lockup_detector_retry_init(void)1294 void __init lockup_detector_retry_init(void)
1295 {
1296 /* Must be called before late init calls */
1297 if (!allow_lockup_detector_init_retry)
1298 return;
1299
1300 schedule_work(&detector_work);
1301 }
1302
1303 /*
1304 * Ensure that optional delayed hardlockup init is proceed before
1305 * the init code and memory is freed.
1306 */
lockup_detector_check(void)1307 static int __init lockup_detector_check(void)
1308 {
1309 /* Prevent any later retry. */
1310 allow_lockup_detector_init_retry = false;
1311
1312 /* Make sure no work is pending. */
1313 flush_work(&detector_work);
1314
1315 watchdog_sysctl_init();
1316
1317 return 0;
1318
1319 }
1320 late_initcall_sync(lockup_detector_check);
1321
lockup_detector_init(void)1322 void __init lockup_detector_init(void)
1323 {
1324 if (tick_nohz_full_enabled())
1325 pr_info("Disabling watchdog on nohz_full cores by default\n");
1326
1327 cpumask_copy(&watchdog_cpumask,
1328 housekeeping_cpumask(HK_TYPE_TIMER));
1329
1330 if (!watchdog_hardlockup_probe())
1331 watchdog_hardlockup_available = true;
1332 else
1333 allow_lockup_detector_init_retry = true;
1334
1335 lockup_detector_setup();
1336 }
1337