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 <linux/sched/debug.h>
18 #include <asm/irq_regs.h>
19 #include <linux/perf_event.h>
21 static DEFINE_PER_CPU(bool, hard_watchdog_warn);
22 static DEFINE_PER_CPU(bool, watchdog_nmi_touch);
23 static DEFINE_PER_CPU(struct perf_event *, watchdog_ev);
27 * Should we panic when a soft-lockup or hard-lockup occurs:
29 unsigned int __read_mostly hardlockup_panic =
30 CONFIG_BOOTPARAM_HARDLOCKUP_PANIC_VALUE;
31 static unsigned long hardlockup_allcpu_dumped;
33 * We may not want to enable hard lockup detection by default in all cases,
34 * for example when running the kernel as a guest on a hypervisor. In these
35 * cases this function can be called to disable hard lockup detection. This
36 * function should only be executed once by the boot processor before the
37 * kernel command line parameters are parsed, because otherwise it is not
38 * possible to override this in hardlockup_panic_setup().
40 void hardlockup_detector_disable(void)
42 watchdog_enabled &= ~NMI_WATCHDOG_ENABLED;
45 static int __init hardlockup_panic_setup(char *str)
47 if (!strncmp(str, "panic", 5))
49 else if (!strncmp(str, "nopanic", 7))
51 else if (!strncmp(str, "0", 1))
52 watchdog_enabled &= ~NMI_WATCHDOG_ENABLED;
53 else if (!strncmp(str, "1", 1))
54 watchdog_enabled |= NMI_WATCHDOG_ENABLED;
57 __setup("nmi_watchdog=", hardlockup_panic_setup);
59 void arch_touch_nmi_watchdog(void)
62 * Using __raw here because some code paths have
63 * preemption enabled. If preemption is enabled
64 * then interrupts should be enabled too, in which
65 * case we shouldn't have to worry about the watchdog
68 raw_cpu_write(watchdog_nmi_touch, true);
70 EXPORT_SYMBOL(arch_touch_nmi_watchdog);
72 static struct perf_event_attr wd_hw_attr = {
73 .type = PERF_TYPE_HARDWARE,
74 .config = PERF_COUNT_HW_CPU_CYCLES,
75 .size = sizeof(struct perf_event_attr),
80 /* Callback function for perf event subsystem */
81 static void watchdog_overflow_callback(struct perf_event *event,
82 struct perf_sample_data *data,
85 /* Ensure the watchdog never gets throttled */
86 event->hw.interrupts = 0;
88 if (atomic_read(&watchdog_park_in_progress) != 0)
91 if (__this_cpu_read(watchdog_nmi_touch) == true) {
92 __this_cpu_write(watchdog_nmi_touch, false);
96 /* check for a hardlockup
97 * This is done by making sure our timer interrupt
98 * is incrementing. The timer interrupt should have
99 * fired multiple times before we overflow'd. If it hasn't
100 * then this is a good indication the cpu is stuck
102 if (is_hardlockup()) {
103 int this_cpu = smp_processor_id();
105 /* only print hardlockups once */
106 if (__this_cpu_read(hard_watchdog_warn) == true)
109 pr_emerg("Watchdog detected hard LOCKUP on cpu %d", this_cpu);
111 print_irqtrace_events(current);
118 * Perform all-CPU dump only once to avoid multiple hardlockups
119 * generating interleaving traces
121 if (sysctl_hardlockup_all_cpu_backtrace &&
122 !test_and_set_bit(0, &hardlockup_allcpu_dumped))
123 trigger_allbutself_cpu_backtrace();
125 if (hardlockup_panic)
126 nmi_panic(regs, "Hard LOCKUP");
128 __this_cpu_write(hard_watchdog_warn, true);
132 __this_cpu_write(hard_watchdog_warn, false);
137 * People like the simple clean cpu node info on boot.
138 * Reduce the watchdog noise by only printing messages
139 * that are different from what cpu0 displayed.
141 static unsigned long firstcpu_err;
142 static atomic_t watchdog_cpus;
144 int watchdog_nmi_enable(unsigned int cpu)
146 struct perf_event_attr *wd_attr;
147 struct perf_event *event = per_cpu(watchdog_ev, cpu);
150 /* nothing to do if the hard lockup detector is disabled */
151 if (!(watchdog_enabled & NMI_WATCHDOG_ENABLED))
154 /* is it already setup and enabled? */
155 if (event && event->state > PERF_EVENT_STATE_OFF)
158 /* it is setup but not enabled */
162 if (atomic_inc_return(&watchdog_cpus) == 1)
165 wd_attr = &wd_hw_attr;
166 wd_attr->sample_period = hw_nmi_get_sample_period(watchdog_thresh);
168 /* Try to register using hardware perf events */
169 event = perf_event_create_kernel_counter(wd_attr, cpu, NULL, watchdog_overflow_callback, NULL);
171 /* save the first cpu's error for future comparision */
172 if (firstcpu && IS_ERR(event))
173 firstcpu_err = PTR_ERR(event);
175 if (!IS_ERR(event)) {
176 /* only print for the first cpu initialized */
177 if (firstcpu || firstcpu_err)
178 pr_info("enabled on all CPUs, permanently consumes one hw-PMU counter.\n");
183 * Disable the hard lockup detector if _any_ CPU fails to set up
184 * set up the hardware perf event. The watchdog() function checks
185 * the NMI_WATCHDOG_ENABLED bit periodically.
187 * The barriers are for syncing up watchdog_enabled across all the
188 * cpus, as clear_bit() does not use barriers.
190 smp_mb__before_atomic();
191 clear_bit(NMI_WATCHDOG_ENABLED_BIT, &watchdog_enabled);
192 smp_mb__after_atomic();
194 /* skip displaying the same error again */
195 if (!firstcpu && (PTR_ERR(event) == firstcpu_err))
196 return PTR_ERR(event);
198 /* vary the KERN level based on the returned errno */
199 if (PTR_ERR(event) == -EOPNOTSUPP)
200 pr_info("disabled (cpu%i): not supported (no LAPIC?)\n", cpu);
201 else if (PTR_ERR(event) == -ENOENT)
202 pr_warn("disabled (cpu%i): hardware events not enabled\n",
205 pr_err("disabled (cpu%i): unable to create perf event: %ld\n",
206 cpu, PTR_ERR(event));
208 pr_info("Shutting down hard lockup detector on all cpus\n");
210 return PTR_ERR(event);
214 per_cpu(watchdog_ev, cpu) = event;
216 perf_event_enable(per_cpu(watchdog_ev, cpu));
221 void watchdog_nmi_disable(unsigned int cpu)
223 struct perf_event *event = per_cpu(watchdog_ev, cpu);
226 perf_event_disable(event);
227 per_cpu(watchdog_ev, cpu) = NULL;
229 /* should be in cleanup, but blocks oprofile */
230 perf_event_release_kernel(event);
232 /* watchdog_nmi_enable() expects this to be zero initially. */
233 if (atomic_dec_and_test(&watchdog_cpus))