12 #define pr_fmt(fmt) "NMI watchdog: " fmt
23 #include <linux/module.h>
24 #include <linux/sysctl.h>
27 #include <asm/irq_regs.h>
28 #include <linux/kvm_para.h>
29 #include <linux/perf_event.h>
42 #ifdef CONFIG_HARDLOCKUP_DETECTOR
53 #ifdef CONFIG_HARDLOCKUP_DETECTOR
54 static int hardlockup_panic =
55 CONFIG_BOOTPARAM_HARDLOCKUP_PANIC_VALUE;
57 static int __init hardlockup_panic_setup(
char *
str)
61 else if (!
strncmp(str,
"nopanic", 7))
67 __setup(
"nmi_watchdog=", hardlockup_panic_setup);
71 CONFIG_BOOTPARAM_SOFTLOCKUP_PANIC_VALUE;
73 static int __init softlockup_panic_setup(
char *str)
79 __setup(
"softlockup_panic=", softlockup_panic_setup);
81 static int __init nowatchdog_setup(
char *str)
86 __setup(
"nowatchdog", nowatchdog_setup);
89 static int __init nosoftlockup_setup(
char *str)
94 __setup(
"nosoftlockup", nosoftlockup_setup);
104 static int get_softlockup_thresh(
void)
114 static unsigned long get_timestamp(
int this_cpu)
119 static u64 get_sample_period(
void)
132 static void __touch_watchdog(
void)
155 per_cpu(watchdog_touch_ts, cpu) = 0;
158 #ifdef CONFIG_HARDLOCKUP_DETECTOR
165 if (
per_cpu(watchdog_nmi_touch, cpu) !=
true)
166 per_cpu(watchdog_nmi_touch, cpu) =
true;
181 #ifdef CONFIG_HARDLOCKUP_DETECTOR
183 static int is_hardlockup(
void)
195 static int is_softlockup(
unsigned long touch_ts)
200 if (
time_after(now, touch_ts + get_softlockup_thresh()))
201 return now - touch_ts;
206 #ifdef CONFIG_HARDLOCKUP_DETECTOR
218 struct perf_sample_data *
data,
222 event->hw.interrupts = 0;
235 if (is_hardlockup()) {
242 if (hardlockup_panic)
243 panic(
"Watchdog detected hard LOCKUP on cpu %d", this_cpu);
245 WARN(1,
"Watchdog detected hard LOCKUP on cpu %d", this_cpu);
256 static void watchdog_interrupt_count(
void)
261 static int watchdog_nmi_enable(
unsigned int cpu);
262 static void watchdog_nmi_disable(
unsigned int cpu);
272 watchdog_interrupt_count();
278 hrtimer_forward_now(hrtimer, ns_to_ktime(get_sample_period()));
302 duration = is_softlockup(touch_ts);
320 print_irqtrace_events(
current);
326 if (softlockup_panic)
327 panic(
"softlockup: hung tasks");
335 static void watchdog_set_prio(
unsigned int policy,
unsigned int prio)
342 static void watchdog_enable(
unsigned int cpu)
352 watchdog_nmi_enable(cpu);
356 hrtimer->
function = watchdog_timer_fn;
367 static void watchdog_disable(
unsigned int cpu)
377 watchdog_nmi_disable(cpu);
380 static int watchdog_should_run(
unsigned int cpu)
394 static void watchdog(
unsigned int cpu)
401 #ifdef CONFIG_HARDLOCKUP_DETECTOR
407 static unsigned long cpu0_err;
409 static int watchdog_nmi_enable(
unsigned int cpu)
422 wd_attr = &wd_hw_attr;
429 if (cpu == 0 && IS_ERR(event))
430 cpu0_err = PTR_ERR(event);
432 if (!IS_ERR(event)) {
434 if (cpu == 0 || cpu0_err)
435 pr_info(
"enabled on all CPUs, permanently consumes one hw-PMU counter.\n");
440 if (cpu > 0 && (PTR_ERR(event) == cpu0_err))
441 return PTR_ERR(event);
445 pr_info(
"disabled (cpu%i): not supported (no LAPIC?)\n", cpu);
446 else if (PTR_ERR(event) == -
ENOENT)
447 pr_warning(
"disabled (cpu%i): hardware events not enabled\n",
450 pr_err(
"disabled (cpu%i): unable to create perf event: %ld\n",
451 cpu, PTR_ERR(event));
452 return PTR_ERR(event);
463 static void watchdog_nmi_disable(
unsigned int cpu)
477 static int watchdog_nmi_enable(
unsigned int cpu) {
return 0; }
478 static void watchdog_nmi_disable(
unsigned int cpu) {
return; }
484 static void watchdog_enable_all_cpus(
void)
488 if (watchdog_disabled) {
489 watchdog_disabled = 0;
495 static
void watchdog_disable_all_cpus(
void)
499 if (!watchdog_disabled) {
500 watchdog_disabled = 1;
511 void __user *
buffer,
size_t *lenp, loff_t *ppos)
515 if (watchdog_disabled < 0)
523 watchdog_enable_all_cpus();
525 watchdog_disable_all_cpus();
532 .store = &softlockup_watchdog,
533 .thread_should_run = watchdog_should_run,
535 .thread_comm =
"watchdog/%u",
536 .setup = watchdog_enable,
537 .park = watchdog_disable,
538 .unpark = watchdog_enable,
544 pr_err(
"Failed to create watchdog threads, disabled\n");
545 watchdog_disabled = -
ENODEV;