]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * linux/arch/x86_64/nmi.c | |
3 | * | |
4 | * NMI watchdog support on APIC systems | |
5 | * | |
6 | * Started by Ingo Molnar <mingo@redhat.com> | |
7 | * | |
8 | * Fixes: | |
9 | * Mikael Pettersson : AMD K7 support for local APIC NMI watchdog. | |
10 | * Mikael Pettersson : Power Management for local APIC NMI watchdog. | |
11 | * Pavel Machek and | |
12 | * Mikael Pettersson : PM converted to driver model. Disable/enable API. | |
13 | */ | |
14 | ||
bb81a09e | 15 | #include <linux/nmi.h> |
1da177e4 | 16 | #include <linux/mm.h> |
1da177e4 | 17 | #include <linux/delay.h> |
1da177e4 | 18 | #include <linux/interrupt.h> |
1da177e4 LT |
19 | #include <linux/module.h> |
20 | #include <linux/sysdev.h> | |
1da177e4 | 21 | #include <linux/sysctl.h> |
eddb6fb9 | 22 | #include <linux/kprobes.h> |
bb81a09e | 23 | #include <linux/cpumask.h> |
1eeb66a1 | 24 | #include <linux/kdebug.h> |
1da177e4 LT |
25 | |
26 | #include <asm/smp.h> | |
1da177e4 | 27 | #include <asm/nmi.h> |
1da177e4 | 28 | #include <asm/proto.h> |
553f265f | 29 | #include <asm/mce.h> |
1da177e4 | 30 | |
29cbc78b AK |
31 | int unknown_nmi_panic; |
32 | int nmi_watchdog_enabled; | |
33 | int panic_on_unrecovered_nmi; | |
34 | ||
1714f9bf | 35 | static cpumask_t backtrace_mask = CPU_MASK_NONE; |
828f0afd | 36 | |
1da177e4 | 37 | /* nmi_active: |
f2802e7f DZ |
38 | * >0: the lapic NMI watchdog is active, but can be disabled |
39 | * <0: the lapic NMI watchdog has not been set up, and cannot | |
1da177e4 | 40 | * be enabled |
f2802e7f | 41 | * 0: the lapic NMI watchdog is disabled, but can be enabled |
1da177e4 | 42 | */ |
f2802e7f | 43 | atomic_t nmi_active = ATOMIC_INIT(0); /* oprofile uses this */ |
1da177e4 LT |
44 | int panic_on_timeout; |
45 | ||
46 | unsigned int nmi_watchdog = NMI_DEFAULT; | |
47 | static unsigned int nmi_hz = HZ; | |
1da177e4 | 48 | |
05cb007d | 49 | static DEFINE_PER_CPU(short, wd_enabled); |
1da177e4 | 50 | |
f2802e7f | 51 | /* local prototypes */ |
f2802e7f | 52 | static int unknown_nmi_panic_callback(struct pt_regs *regs, int cpu); |
75152114 | 53 | |
1da177e4 | 54 | /* Run after command line and cpu_init init, but before all other checks */ |
e33e89ab | 55 | void nmi_watchdog_default(void) |
1da177e4 LT |
56 | { |
57 | if (nmi_watchdog != NMI_DEFAULT) | |
58 | return; | |
8ce5e3e4 | 59 | nmi_watchdog = NMI_NONE; |
1da177e4 LT |
60 | } |
61 | ||
92715e28 RT |
62 | static int endflag __initdata = 0; |
63 | ||
75152114 AK |
64 | #ifdef CONFIG_SMP |
65 | /* The performance counters used by NMI_LOCAL_APIC don't trigger when | |
66 | * the CPU is idle. To make sure the NMI watchdog really ticks on all | |
67 | * CPUs during the test make them busy. | |
68 | */ | |
69 | static __init void nmi_cpu_busy(void *data) | |
1da177e4 | 70 | { |
366c7f55 | 71 | local_irq_enable_in_hardirq(); |
75152114 AK |
72 | /* Intentionally don't use cpu_relax here. This is |
73 | to make sure that the performance counter really ticks, | |
74 | even if there is a simulator or similar that catches the | |
75 | pause instruction. On a real HT machine this is fine because | |
76 | all other CPUs are busy with "useless" delay loops and don't | |
77 | care if they get somewhat less cycles. */ | |
92715e28 RT |
78 | while (endflag == 0) |
79 | mb(); | |
1da177e4 | 80 | } |
75152114 | 81 | #endif |
1da177e4 | 82 | |
75152114 | 83 | int __init check_nmi_watchdog (void) |
1da177e4 | 84 | { |
ac6b931c | 85 | int *counts; |
1da177e4 LT |
86 | int cpu; |
87 | ||
f2802e7f DZ |
88 | if ((nmi_watchdog == NMI_NONE) || (nmi_watchdog == NMI_DEFAULT)) |
89 | return 0; | |
90 | ||
91 | if (!atomic_read(&nmi_active)) | |
92 | return 0; | |
93 | ||
75152114 AK |
94 | counts = kmalloc(NR_CPUS * sizeof(int), GFP_KERNEL); |
95 | if (!counts) | |
96 | return -1; | |
1da177e4 | 97 | |
75152114 | 98 | printk(KERN_INFO "testing NMI watchdog ... "); |
ac6b931c | 99 | |
7554c3f0 | 100 | #ifdef CONFIG_SMP |
75152114 AK |
101 | if (nmi_watchdog == NMI_LOCAL_APIC) |
102 | smp_call_function(nmi_cpu_busy, (void *)&endflag, 0, 0); | |
7554c3f0 | 103 | #endif |
1da177e4 LT |
104 | |
105 | for (cpu = 0; cpu < NR_CPUS; cpu++) | |
df79efde | 106 | counts[cpu] = cpu_pda(cpu)->__nmi_count; |
1da177e4 | 107 | local_irq_enable(); |
0fb2ebfc | 108 | mdelay((20*1000)/nmi_hz); // wait 20 ticks |
1da177e4 | 109 | |
394e3902 | 110 | for_each_online_cpu(cpu) { |
05cb007d | 111 | if (!per_cpu(wd_enabled, cpu)) |
f2802e7f | 112 | continue; |
df79efde | 113 | if (cpu_pda(cpu)->__nmi_count - counts[cpu] <= 5) { |
75152114 | 114 | printk("CPU#%d: NMI appears to be stuck (%d->%d)!\n", |
1da177e4 | 115 | cpu, |
75152114 | 116 | counts[cpu], |
df79efde | 117 | cpu_pda(cpu)->__nmi_count); |
05cb007d | 118 | per_cpu(wd_enabled, cpu) = 0; |
f2802e7f | 119 | atomic_dec(&nmi_active); |
1da177e4 LT |
120 | } |
121 | } | |
f2802e7f DZ |
122 | if (!atomic_read(&nmi_active)) { |
123 | kfree(counts); | |
124 | atomic_set(&nmi_active, -1); | |
92715e28 | 125 | endflag = 1; |
f2802e7f DZ |
126 | return -1; |
127 | } | |
75152114 | 128 | endflag = 1; |
1da177e4 LT |
129 | printk("OK.\n"); |
130 | ||
131 | /* now that we know it works we can reduce NMI frequency to | |
132 | something more reasonable; makes a difference in some configs */ | |
05cb007d AK |
133 | if (nmi_watchdog == NMI_LOCAL_APIC) |
134 | nmi_hz = lapic_adjust_nmi_hz(1); | |
1da177e4 | 135 | |
ac6b931c | 136 | kfree(counts); |
1da177e4 LT |
137 | return 0; |
138 | } | |
139 | ||
140 | int __init setup_nmi_watchdog(char *str) | |
141 | { | |
142 | int nmi; | |
143 | ||
144 | if (!strncmp(str,"panic",5)) { | |
145 | panic_on_timeout = 1; | |
146 | str = strchr(str, ','); | |
147 | if (!str) | |
148 | return 1; | |
149 | ++str; | |
150 | } | |
151 | ||
152 | get_option(&str, &nmi); | |
153 | ||
f2802e7f | 154 | if ((nmi >= NMI_INVALID) || (nmi < NMI_NONE)) |
1da177e4 | 155 | return 0; |
f2802e7f | 156 | |
75152114 | 157 | nmi_watchdog = nmi; |
1da177e4 LT |
158 | return 1; |
159 | } | |
160 | ||
161 | __setup("nmi_watchdog=", setup_nmi_watchdog); | |
162 | ||
1da177e4 | 163 | |
5d0e600d IM |
164 | static void __acpi_nmi_disable(void *__unused) |
165 | { | |
166 | apic_write(APIC_LVT0, APIC_DM_NMI | APIC_LVT_MASKED); | |
167 | } | |
168 | ||
169 | /* | |
170 | * Disable timer based NMIs on all CPUs: | |
171 | */ | |
172 | void acpi_nmi_disable(void) | |
173 | { | |
174 | if (atomic_read(&nmi_active) && nmi_watchdog == NMI_IO_APIC) | |
175 | on_each_cpu(__acpi_nmi_disable, NULL, 0, 1); | |
176 | } | |
177 | ||
178 | static void __acpi_nmi_enable(void *__unused) | |
179 | { | |
180 | apic_write(APIC_LVT0, APIC_DM_NMI); | |
181 | } | |
182 | ||
183 | /* | |
184 | * Enable timer based NMIs on all CPUs: | |
185 | */ | |
186 | void acpi_nmi_enable(void) | |
187 | { | |
188 | if (atomic_read(&nmi_active) && nmi_watchdog == NMI_IO_APIC) | |
189 | on_each_cpu(__acpi_nmi_enable, NULL, 0, 1); | |
190 | } | |
1da177e4 LT |
191 | #ifdef CONFIG_PM |
192 | ||
193 | static int nmi_pm_active; /* nmi_active before suspend */ | |
194 | ||
829ca9a3 | 195 | static int lapic_nmi_suspend(struct sys_device *dev, pm_message_t state) |
1da177e4 | 196 | { |
4038f901 | 197 | /* only CPU0 goes here, other CPUs should be offline */ |
f2802e7f | 198 | nmi_pm_active = atomic_read(&nmi_active); |
4038f901 SL |
199 | stop_apic_nmi_watchdog(NULL); |
200 | BUG_ON(atomic_read(&nmi_active) != 0); | |
1da177e4 LT |
201 | return 0; |
202 | } | |
203 | ||
204 | static int lapic_nmi_resume(struct sys_device *dev) | |
205 | { | |
4038f901 SL |
206 | /* only CPU0 goes here, other CPUs should be offline */ |
207 | if (nmi_pm_active > 0) { | |
208 | setup_apic_nmi_watchdog(NULL); | |
209 | touch_nmi_watchdog(); | |
210 | } | |
1da177e4 LT |
211 | return 0; |
212 | } | |
213 | ||
214 | static struct sysdev_class nmi_sysclass = { | |
215 | set_kset_name("lapic_nmi"), | |
216 | .resume = lapic_nmi_resume, | |
217 | .suspend = lapic_nmi_suspend, | |
218 | }; | |
219 | ||
220 | static struct sys_device device_lapic_nmi = { | |
221 | .id = 0, | |
222 | .cls = &nmi_sysclass, | |
223 | }; | |
224 | ||
225 | static int __init init_lapic_nmi_sysfs(void) | |
226 | { | |
227 | int error; | |
228 | ||
f2802e7f DZ |
229 | /* should really be a BUG_ON but b/c this is an |
230 | * init call, it just doesn't work. -dcz | |
231 | */ | |
232 | if (nmi_watchdog != NMI_LOCAL_APIC) | |
233 | return 0; | |
234 | ||
235 | if ( atomic_read(&nmi_active) < 0 ) | |
1da177e4 LT |
236 | return 0; |
237 | ||
238 | error = sysdev_class_register(&nmi_sysclass); | |
239 | if (!error) | |
240 | error = sysdev_register(&device_lapic_nmi); | |
241 | return error; | |
242 | } | |
243 | /* must come after the local APIC's device_initcall() */ | |
244 | late_initcall(init_lapic_nmi_sysfs); | |
245 | ||
246 | #endif /* CONFIG_PM */ | |
247 | ||
f2802e7f DZ |
248 | void setup_apic_nmi_watchdog(void *unused) |
249 | { | |
05cb007d | 250 | if (__get_cpu_var(wd_enabled) == 1) |
4038f901 SL |
251 | return; |
252 | ||
253 | /* cheap hack to support suspend/resume */ | |
254 | /* if cpu0 is not active neither should the other cpus */ | |
255 | if ((smp_processor_id() != 0) && (atomic_read(&nmi_active) <= 0)) | |
256 | return; | |
257 | ||
05cb007d AK |
258 | switch (nmi_watchdog) { |
259 | case NMI_LOCAL_APIC: | |
260 | __get_cpu_var(wd_enabled) = 1; | |
261 | if (lapic_watchdog_init(nmi_hz) < 0) { | |
262 | __get_cpu_var(wd_enabled) = 0; | |
75152114 | 263 | return; |
f2802e7f | 264 | } |
05cb007d AK |
265 | /* FALL THROUGH */ |
266 | case NMI_IO_APIC: | |
267 | __get_cpu_var(wd_enabled) = 1; | |
268 | atomic_inc(&nmi_active); | |
f2802e7f | 269 | } |
f2802e7f | 270 | } |
75152114 | 271 | |
4038f901 | 272 | void stop_apic_nmi_watchdog(void *unused) |
f2802e7f DZ |
273 | { |
274 | /* only support LOCAL and IO APICs for now */ | |
275 | if ((nmi_watchdog != NMI_LOCAL_APIC) && | |
276 | (nmi_watchdog != NMI_IO_APIC)) | |
277 | return; | |
05cb007d | 278 | if (__get_cpu_var(wd_enabled) == 0) |
4038f901 | 279 | return; |
05cb007d AK |
280 | if (nmi_watchdog == NMI_LOCAL_APIC) |
281 | lapic_watchdog_stop(); | |
282 | __get_cpu_var(wd_enabled) = 0; | |
f2802e7f | 283 | atomic_dec(&nmi_active); |
1da177e4 LT |
284 | } |
285 | ||
286 | /* | |
287 | * the best way to detect whether a CPU has a 'hard lockup' problem | |
288 | * is to check it's local APIC timer IRQ counts. If they are not | |
289 | * changing then that CPU has some problem. | |
290 | * | |
291 | * as these watchdog NMI IRQs are generated on every CPU, we only | |
292 | * have to check the current processor. | |
1da177e4 LT |
293 | */ |
294 | ||
75152114 AK |
295 | static DEFINE_PER_CPU(unsigned, last_irq_sum); |
296 | static DEFINE_PER_CPU(local_t, alert_counter); | |
297 | static DEFINE_PER_CPU(int, nmi_touch); | |
1da177e4 | 298 | |
567f3e42 | 299 | void touch_nmi_watchdog(void) |
1da177e4 | 300 | { |
99019e91 JB |
301 | if (nmi_watchdog > 0) { |
302 | unsigned cpu; | |
1da177e4 | 303 | |
99019e91 JB |
304 | /* |
305 | * Tell other CPUs to reset their alert counters. We cannot | |
306 | * do it ourselves because the alert count increase is not | |
307 | * atomic. | |
308 | */ | |
567f3e42 AM |
309 | for_each_present_cpu(cpu) { |
310 | if (per_cpu(nmi_touch, cpu) != 1) | |
311 | per_cpu(nmi_touch, cpu) = 1; | |
312 | } | |
99019e91 | 313 | } |
8446f1d3 IM |
314 | |
315 | touch_softlockup_watchdog(); | |
1da177e4 LT |
316 | } |
317 | ||
3adbbcce | 318 | int __kprobes nmi_watchdog_tick(struct pt_regs * regs, unsigned reason) |
1da177e4 | 319 | { |
75152114 AK |
320 | int sum; |
321 | int touched = 0; | |
bb81a09e | 322 | int cpu = smp_processor_id(); |
05cb007d | 323 | int rc = 0; |
f2802e7f DZ |
324 | |
325 | /* check for other users first */ | |
326 | if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT) | |
327 | == NOTIFY_STOP) { | |
3adbbcce | 328 | rc = 1; |
f2802e7f DZ |
329 | touched = 1; |
330 | } | |
1da177e4 | 331 | |
1da177e4 | 332 | sum = read_pda(apic_timer_irqs); |
75152114 AK |
333 | if (__get_cpu_var(nmi_touch)) { |
334 | __get_cpu_var(nmi_touch) = 0; | |
335 | touched = 1; | |
336 | } | |
f2802e7f | 337 | |
bb81a09e AM |
338 | if (cpu_isset(cpu, backtrace_mask)) { |
339 | static DEFINE_SPINLOCK(lock); /* Serialise the printks */ | |
340 | ||
341 | spin_lock(&lock); | |
342 | printk("NMI backtrace for cpu %d\n", cpu); | |
343 | dump_stack(); | |
344 | spin_unlock(&lock); | |
345 | cpu_clear(cpu, backtrace_mask); | |
346 | } | |
347 | ||
553f265f AK |
348 | #ifdef CONFIG_X86_MCE |
349 | /* Could check oops_in_progress here too, but it's safer | |
350 | not too */ | |
351 | if (atomic_read(&mce_entry) > 0) | |
352 | touched = 1; | |
353 | #endif | |
f2802e7f | 354 | /* if the apic timer isn't firing, this cpu isn't doing much */ |
75152114 | 355 | if (!touched && __get_cpu_var(last_irq_sum) == sum) { |
1da177e4 LT |
356 | /* |
357 | * Ayiee, looks like this CPU is stuck ... | |
358 | * wait a few IRQs (5 seconds) before doing the oops ... | |
359 | */ | |
75152114 | 360 | local_inc(&__get_cpu_var(alert_counter)); |
f2802e7f | 361 | if (local_read(&__get_cpu_var(alert_counter)) == 5*nmi_hz) |
fac58550 AK |
362 | die_nmi("NMI Watchdog detected LOCKUP on CPU %d\n", regs, |
363 | panic_on_timeout); | |
1da177e4 | 364 | } else { |
75152114 AK |
365 | __get_cpu_var(last_irq_sum) = sum; |
366 | local_set(&__get_cpu_var(alert_counter), 0); | |
1da177e4 | 367 | } |
f2802e7f DZ |
368 | |
369 | /* see if the nmi watchdog went off */ | |
05cb007d AK |
370 | if (!__get_cpu_var(wd_enabled)) |
371 | return rc; | |
372 | switch (nmi_watchdog) { | |
373 | case NMI_LOCAL_APIC: | |
374 | rc |= lapic_wd_event(nmi_hz); | |
375 | break; | |
376 | case NMI_IO_APIC: | |
377 | /* don't know how to accurately check for this. | |
378 | * just assume it was a watchdog timer interrupt | |
379 | * This matches the old behaviour. | |
380 | */ | |
381 | rc = 1; | |
382 | break; | |
75152114 | 383 | } |
3adbbcce | 384 | return rc; |
1da177e4 LT |
385 | } |
386 | ||
8f4e956b AK |
387 | static unsigned ignore_nmis; |
388 | ||
eddb6fb9 | 389 | asmlinkage __kprobes void do_nmi(struct pt_regs * regs, long error_code) |
1da177e4 | 390 | { |
1da177e4 LT |
391 | nmi_enter(); |
392 | add_pda(__nmi_count,1); | |
8f4e956b AK |
393 | if (!ignore_nmis) |
394 | default_do_nmi(regs); | |
1da177e4 LT |
395 | nmi_exit(); |
396 | } | |
397 | ||
3adbbcce DZ |
398 | int do_nmi_callback(struct pt_regs * regs, int cpu) |
399 | { | |
2fbe7b25 DZ |
400 | #ifdef CONFIG_SYSCTL |
401 | if (unknown_nmi_panic) | |
402 | return unknown_nmi_panic_callback(regs, cpu); | |
403 | #endif | |
404 | return 0; | |
1da177e4 LT |
405 | } |
406 | ||
8f4e956b AK |
407 | void stop_nmi(void) |
408 | { | |
409 | acpi_nmi_disable(); | |
410 | ignore_nmis++; | |
411 | } | |
412 | ||
413 | void restart_nmi(void) | |
414 | { | |
415 | ignore_nmis--; | |
416 | acpi_nmi_enable(); | |
417 | } | |
418 | ||
1da177e4 LT |
419 | #ifdef CONFIG_SYSCTL |
420 | ||
421 | static int unknown_nmi_panic_callback(struct pt_regs *regs, int cpu) | |
422 | { | |
423 | unsigned char reason = get_nmi_reason(); | |
424 | char buf[64]; | |
425 | ||
2fbe7b25 | 426 | sprintf(buf, "NMI received for unknown reason %02x\n", reason); |
fac58550 | 427 | die_nmi(buf, regs, 1); /* Always panic here */ |
1da177e4 LT |
428 | return 0; |
429 | } | |
430 | ||
407984f1 DZ |
431 | /* |
432 | * proc handler for /proc/sys/kernel/nmi | |
433 | */ | |
434 | int proc_nmi_enabled(struct ctl_table *table, int write, struct file *file, | |
435 | void __user *buffer, size_t *length, loff_t *ppos) | |
436 | { | |
437 | int old_state; | |
438 | ||
439 | nmi_watchdog_enabled = (atomic_read(&nmi_active) > 0) ? 1 : 0; | |
440 | old_state = nmi_watchdog_enabled; | |
441 | proc_dointvec(table, write, file, buffer, length, ppos); | |
442 | if (!!old_state == !!nmi_watchdog_enabled) | |
443 | return 0; | |
444 | ||
445 | if (atomic_read(&nmi_active) < 0) { | |
446 | printk( KERN_WARNING "NMI watchdog is permanently disabled\n"); | |
e33e89ab | 447 | return -EIO; |
407984f1 DZ |
448 | } |
449 | ||
450 | /* if nmi_watchdog is not set yet, then set it */ | |
451 | nmi_watchdog_default(); | |
452 | ||
e33e89ab | 453 | if (nmi_watchdog == NMI_LOCAL_APIC) { |
407984f1 DZ |
454 | if (nmi_watchdog_enabled) |
455 | enable_lapic_nmi_watchdog(); | |
456 | else | |
457 | disable_lapic_nmi_watchdog(); | |
407984f1 | 458 | } else { |
e33e89ab | 459 | printk( KERN_WARNING |
407984f1 DZ |
460 | "NMI watchdog doesn't know what hardware to touch\n"); |
461 | return -EIO; | |
462 | } | |
463 | return 0; | |
464 | } | |
465 | ||
1da177e4 LT |
466 | #endif |
467 | ||
bb81a09e AM |
468 | void __trigger_all_cpu_backtrace(void) |
469 | { | |
470 | int i; | |
471 | ||
472 | backtrace_mask = cpu_online_map; | |
473 | /* Wait for up to 10 seconds for all CPUs to do the backtrace */ | |
474 | for (i = 0; i < 10 * 1000; i++) { | |
475 | if (cpus_empty(backtrace_mask)) | |
476 | break; | |
477 | mdelay(1); | |
478 | } | |
479 | } | |
480 | ||
1da177e4 LT |
481 | EXPORT_SYMBOL(nmi_active); |
482 | EXPORT_SYMBOL(nmi_watchdog); | |
1da177e4 | 483 | EXPORT_SYMBOL(touch_nmi_watchdog); |