]>
Commit | Line | Data |
---|---|---|
73ce0511 BM |
1 | /* |
2 | * Detect hard lockups on a system | |
3 | * | |
4 | * started by Don Zickus, Copyright (C) 2010 Red Hat, Inc. | |
5 | * | |
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. | |
10 | */ | |
11 | ||
12 | #define pr_fmt(fmt) "NMI watchdog: " fmt | |
13 | ||
14 | #include <linux/nmi.h> | |
15 | #include <linux/module.h> | |
16 | #include <asm/irq_regs.h> | |
17 | #include <linux/perf_event.h> | |
18 | ||
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); | |
22 | ||
23 | /* boot commands */ | |
24 | /* | |
25 | * Should we panic when a soft-lockup or hard-lockup occurs: | |
26 | */ | |
27 | unsigned int __read_mostly hardlockup_panic = | |
28 | CONFIG_BOOTPARAM_HARDLOCKUP_PANIC_VALUE; | |
29 | static unsigned long hardlockup_allcpu_dumped; | |
30 | /* | |
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(). | |
37 | */ | |
38 | void hardlockup_detector_disable(void) | |
39 | { | |
40 | watchdog_enabled &= ~NMI_WATCHDOG_ENABLED; | |
41 | } | |
42 | ||
43 | static int __init hardlockup_panic_setup(char *str) | |
44 | { | |
45 | if (!strncmp(str, "panic", 5)) | |
46 | hardlockup_panic = 1; | |
47 | else if (!strncmp(str, "nopanic", 7)) | |
48 | hardlockup_panic = 0; | |
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; | |
53 | return 1; | |
54 | } | |
55 | __setup("nmi_watchdog=", hardlockup_panic_setup); | |
56 | ||
57 | void touch_nmi_watchdog(void) | |
58 | { | |
59 | /* | |
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 | |
64 | * going off. | |
65 | */ | |
66 | raw_cpu_write(watchdog_nmi_touch, true); | |
67 | touch_softlockup_watchdog(); | |
68 | } | |
69 | EXPORT_SYMBOL(touch_nmi_watchdog); | |
70 | ||
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), | |
75 | .pinned = 1, | |
76 | .disabled = 1, | |
77 | }; | |
78 | ||
79 | /* Callback function for perf event subsystem */ | |
80 | static void watchdog_overflow_callback(struct perf_event *event, | |
81 | struct perf_sample_data *data, | |
82 | struct pt_regs *regs) | |
83 | { | |
84 | /* Ensure the watchdog never gets throttled */ | |
85 | event->hw.interrupts = 0; | |
86 | ||
b94f5118 DZ |
87 | if (atomic_read(&watchdog_park_in_progress) != 0) |
88 | return; | |
89 | ||
73ce0511 BM |
90 | if (__this_cpu_read(watchdog_nmi_touch) == true) { |
91 | __this_cpu_write(watchdog_nmi_touch, false); | |
92 | return; | |
93 | } | |
94 | ||
95 | /* check for a hardlockup | |
96 | * This is done by making sure our timer interrupt | |
97 | * is incrementing. The timer interrupt should have | |
98 | * fired multiple times before we overflow'd. If it hasn't | |
99 | * then this is a good indication the cpu is stuck | |
100 | */ | |
101 | if (is_hardlockup()) { | |
102 | int this_cpu = smp_processor_id(); | |
103 | ||
104 | /* only print hardlockups once */ | |
105 | if (__this_cpu_read(hard_watchdog_warn) == true) | |
106 | return; | |
107 | ||
108 | pr_emerg("Watchdog detected hard LOCKUP on cpu %d", this_cpu); | |
109 | print_modules(); | |
110 | print_irqtrace_events(current); | |
111 | if (regs) | |
112 | show_regs(regs); | |
113 | else | |
114 | dump_stack(); | |
115 | ||
116 | /* | |
117 | * Perform all-CPU dump only once to avoid multiple hardlockups | |
118 | * generating interleaving traces | |
119 | */ | |
120 | if (sysctl_hardlockup_all_cpu_backtrace && | |
121 | !test_and_set_bit(0, &hardlockup_allcpu_dumped)) | |
122 | trigger_allbutself_cpu_backtrace(); | |
123 | ||
124 | if (hardlockup_panic) | |
125 | nmi_panic(regs, "Hard LOCKUP"); | |
126 | ||
127 | __this_cpu_write(hard_watchdog_warn, true); | |
128 | return; | |
129 | } | |
130 | ||
131 | __this_cpu_write(hard_watchdog_warn, false); | |
132 | return; | |
133 | } | |
134 | ||
135 | /* | |
136 | * People like the simple clean cpu node info on boot. | |
137 | * Reduce the watchdog noise by only printing messages | |
138 | * that are different from what cpu0 displayed. | |
139 | */ | |
8dcde9de PB |
140 | static unsigned long firstcpu_err; |
141 | static atomic_t watchdog_cpus; | |
73ce0511 BM |
142 | |
143 | int watchdog_nmi_enable(unsigned int cpu) | |
144 | { | |
145 | struct perf_event_attr *wd_attr; | |
146 | struct perf_event *event = per_cpu(watchdog_ev, cpu); | |
8dcde9de | 147 | int firstcpu = 0; |
73ce0511 BM |
148 | |
149 | /* nothing to do if the hard lockup detector is disabled */ | |
150 | if (!(watchdog_enabled & NMI_WATCHDOG_ENABLED)) | |
151 | goto out; | |
152 | ||
153 | /* is it already setup and enabled? */ | |
154 | if (event && event->state > PERF_EVENT_STATE_OFF) | |
155 | goto out; | |
156 | ||
157 | /* it is setup but not enabled */ | |
158 | if (event != NULL) | |
159 | goto out_enable; | |
160 | ||
8dcde9de PB |
161 | if (atomic_inc_return(&watchdog_cpus) == 1) |
162 | firstcpu = 1; | |
163 | ||
73ce0511 BM |
164 | wd_attr = &wd_hw_attr; |
165 | wd_attr->sample_period = hw_nmi_get_sample_period(watchdog_thresh); | |
166 | ||
167 | /* Try to register using hardware perf events */ | |
168 | event = perf_event_create_kernel_counter(wd_attr, cpu, NULL, watchdog_overflow_callback, NULL); | |
169 | ||
8dcde9de PB |
170 | /* save the first cpu's error for future comparision */ |
171 | if (firstcpu && IS_ERR(event)) | |
172 | firstcpu_err = PTR_ERR(event); | |
73ce0511 BM |
173 | |
174 | if (!IS_ERR(event)) { | |
8dcde9de PB |
175 | /* only print for the first cpu initialized */ |
176 | if (firstcpu || firstcpu_err) | |
73ce0511 BM |
177 | pr_info("enabled on all CPUs, permanently consumes one hw-PMU counter.\n"); |
178 | goto out_save; | |
179 | } | |
180 | ||
181 | /* | |
182 | * Disable the hard lockup detector if _any_ CPU fails to set up | |
183 | * set up the hardware perf event. The watchdog() function checks | |
184 | * the NMI_WATCHDOG_ENABLED bit periodically. | |
185 | * | |
186 | * The barriers are for syncing up watchdog_enabled across all the | |
187 | * cpus, as clear_bit() does not use barriers. | |
188 | */ | |
189 | smp_mb__before_atomic(); | |
190 | clear_bit(NMI_WATCHDOG_ENABLED_BIT, &watchdog_enabled); | |
191 | smp_mb__after_atomic(); | |
192 | ||
193 | /* skip displaying the same error again */ | |
8dcde9de | 194 | if (!firstcpu && (PTR_ERR(event) == firstcpu_err)) |
73ce0511 BM |
195 | return PTR_ERR(event); |
196 | ||
197 | /* vary the KERN level based on the returned errno */ | |
198 | if (PTR_ERR(event) == -EOPNOTSUPP) | |
199 | pr_info("disabled (cpu%i): not supported (no LAPIC?)\n", cpu); | |
200 | else if (PTR_ERR(event) == -ENOENT) | |
201 | pr_warn("disabled (cpu%i): hardware events not enabled\n", | |
202 | cpu); | |
203 | else | |
204 | pr_err("disabled (cpu%i): unable to create perf event: %ld\n", | |
205 | cpu, PTR_ERR(event)); | |
206 | ||
207 | pr_info("Shutting down hard lockup detector on all cpus\n"); | |
208 | ||
209 | return PTR_ERR(event); | |
210 | ||
211 | /* success path */ | |
212 | out_save: | |
213 | per_cpu(watchdog_ev, cpu) = event; | |
214 | out_enable: | |
215 | perf_event_enable(per_cpu(watchdog_ev, cpu)); | |
216 | out: | |
217 | return 0; | |
218 | } | |
219 | ||
220 | void watchdog_nmi_disable(unsigned int cpu) | |
221 | { | |
222 | struct perf_event *event = per_cpu(watchdog_ev, cpu); | |
223 | ||
224 | if (event) { | |
225 | perf_event_disable(event); | |
226 | per_cpu(watchdog_ev, cpu) = NULL; | |
227 | ||
228 | /* should be in cleanup, but blocks oprofile */ | |
229 | perf_event_release_kernel(event); | |
8dcde9de | 230 | |
73ce0511 | 231 | /* watchdog_nmi_enable() expects this to be zero initially. */ |
8dcde9de PB |
232 | if (atomic_dec_and_test(&watchdog_cpus)) |
233 | firstcpu_err = 0; | |
73ce0511 BM |
234 | } |
235 | } |