2 * Detect hard lockups on a system
4 * started by Don Zickus, Copyright (C) 2010 Red Hat, Inc.
6 * Note: Most of this code is borrowed heavily from the original softlockup
7 * detector, so thanks to Ingo for the initial implementation.
8 * Some chunks also taken from the old x86-specific nmi watchdog code, thanks
9 * to those contributors as well.
12 #define pr_fmt(fmt) "NMI watchdog: " fmt
14 #include <linux/nmi.h>
15 #include <linux/module.h>
16 #include <asm/irq_regs.h>
17 #include <linux/perf_event.h>
19 static DEFINE_PER_CPU(bool, hard_watchdog_warn
);
20 static DEFINE_PER_CPU(bool, watchdog_nmi_touch
);
21 static DEFINE_PER_CPU(struct perf_event
*, watchdog_ev
);
25 * Should we panic when a soft-lockup or hard-lockup occurs:
27 unsigned int __read_mostly hardlockup_panic
=
28 CONFIG_BOOTPARAM_HARDLOCKUP_PANIC_VALUE
;
29 static unsigned long hardlockup_allcpu_dumped
;
31 * We may not want to enable hard lockup detection by default in all cases,
32 * for example when running the kernel as a guest on a hypervisor. In these
33 * cases this function can be called to disable hard lockup detection. This
34 * function should only be executed once by the boot processor before the
35 * kernel command line parameters are parsed, because otherwise it is not
36 * possible to override this in hardlockup_panic_setup().
38 void hardlockup_detector_disable(void)
40 watchdog_enabled
&= ~NMI_WATCHDOG_ENABLED
;
43 static int __init
hardlockup_panic_setup(char *str
)
45 if (!strncmp(str
, "panic", 5))
47 else if (!strncmp(str
, "nopanic", 7))
49 else if (!strncmp(str
, "0", 1))
50 watchdog_enabled
&= ~NMI_WATCHDOG_ENABLED
;
51 else if (!strncmp(str
, "1", 1))
52 watchdog_enabled
|= NMI_WATCHDOG_ENABLED
;
55 __setup("nmi_watchdog=", hardlockup_panic_setup
);
57 void touch_nmi_watchdog(void)
60 * Using __raw here because some code paths have
61 * preemption enabled. If preemption is enabled
62 * then interrupts should be enabled too, in which
63 * case we shouldn't have to worry about the watchdog
66 raw_cpu_write(watchdog_nmi_touch
, true);
67 touch_softlockup_watchdog();
69 EXPORT_SYMBOL(touch_nmi_watchdog
);
71 static struct perf_event_attr wd_hw_attr
= {
72 .type
= PERF_TYPE_HARDWARE
,
73 .config
= PERF_COUNT_HW_CPU_CYCLES
,
74 .size
= sizeof(struct perf_event_attr
),
79 /* Callback function for perf event subsystem */
80 static void watchdog_overflow_callback(struct perf_event
*event
,
81 struct perf_sample_data
*data
,
84 /* Ensure the watchdog never gets throttled */
85 event
->hw
.interrupts
= 0;
87 if (__this_cpu_read(watchdog_nmi_touch
) == true) {
88 __this_cpu_write(watchdog_nmi_touch
, false);
92 /* check for a hardlockup
93 * This is done by making sure our timer interrupt
94 * is incrementing. The timer interrupt should have
95 * fired multiple times before we overflow'd. If it hasn't
96 * then this is a good indication the cpu is stuck
98 if (is_hardlockup()) {
99 int this_cpu
= smp_processor_id();
101 /* only print hardlockups once */
102 if (__this_cpu_read(hard_watchdog_warn
) == true)
105 pr_emerg("Watchdog detected hard LOCKUP on cpu %d", this_cpu
);
107 print_irqtrace_events(current
);
114 * Perform all-CPU dump only once to avoid multiple hardlockups
115 * generating interleaving traces
117 if (sysctl_hardlockup_all_cpu_backtrace
&&
118 !test_and_set_bit(0, &hardlockup_allcpu_dumped
))
119 trigger_allbutself_cpu_backtrace();
121 if (hardlockup_panic
)
122 nmi_panic(regs
, "Hard LOCKUP");
124 __this_cpu_write(hard_watchdog_warn
, true);
128 __this_cpu_write(hard_watchdog_warn
, false);
133 * People like the simple clean cpu node info on boot.
134 * Reduce the watchdog noise by only printing messages
135 * that are different from what cpu0 displayed.
137 static unsigned long cpu0_err
;
139 int watchdog_nmi_enable(unsigned int cpu
)
141 struct perf_event_attr
*wd_attr
;
142 struct perf_event
*event
= per_cpu(watchdog_ev
, cpu
);
144 /* nothing to do if the hard lockup detector is disabled */
145 if (!(watchdog_enabled
& NMI_WATCHDOG_ENABLED
))
148 /* is it already setup and enabled? */
149 if (event
&& event
->state
> PERF_EVENT_STATE_OFF
)
152 /* it is setup but not enabled */
156 wd_attr
= &wd_hw_attr
;
157 wd_attr
->sample_period
= hw_nmi_get_sample_period(watchdog_thresh
);
159 /* Try to register using hardware perf events */
160 event
= perf_event_create_kernel_counter(wd_attr
, cpu
, NULL
, watchdog_overflow_callback
, NULL
);
162 /* save cpu0 error for future comparision */
163 if (cpu
== 0 && IS_ERR(event
))
164 cpu0_err
= PTR_ERR(event
);
166 if (!IS_ERR(event
)) {
167 /* only print for cpu0 or different than cpu0 */
168 if (cpu
== 0 || cpu0_err
)
169 pr_info("enabled on all CPUs, permanently consumes one hw-PMU counter.\n");
174 * Disable the hard lockup detector if _any_ CPU fails to set up
175 * set up the hardware perf event. The watchdog() function checks
176 * the NMI_WATCHDOG_ENABLED bit periodically.
178 * The barriers are for syncing up watchdog_enabled across all the
179 * cpus, as clear_bit() does not use barriers.
181 smp_mb__before_atomic();
182 clear_bit(NMI_WATCHDOG_ENABLED_BIT
, &watchdog_enabled
);
183 smp_mb__after_atomic();
185 /* skip displaying the same error again */
186 if (cpu
> 0 && (PTR_ERR(event
) == cpu0_err
))
187 return PTR_ERR(event
);
189 /* vary the KERN level based on the returned errno */
190 if (PTR_ERR(event
) == -EOPNOTSUPP
)
191 pr_info("disabled (cpu%i): not supported (no LAPIC?)\n", cpu
);
192 else if (PTR_ERR(event
) == -ENOENT
)
193 pr_warn("disabled (cpu%i): hardware events not enabled\n",
196 pr_err("disabled (cpu%i): unable to create perf event: %ld\n",
197 cpu
, PTR_ERR(event
));
199 pr_info("Shutting down hard lockup detector on all cpus\n");
201 return PTR_ERR(event
);
205 per_cpu(watchdog_ev
, cpu
) = event
;
207 perf_event_enable(per_cpu(watchdog_ev
, cpu
));
212 void watchdog_nmi_disable(unsigned int cpu
)
214 struct perf_event
*event
= per_cpu(watchdog_ev
, cpu
);
217 perf_event_disable(event
);
218 per_cpu(watchdog_ev
, cpu
) = NULL
;
220 /* should be in cleanup, but blocks oprofile */
221 perf_event_release_kernel(event
);
224 /* watchdog_nmi_enable() expects this to be zero initially. */