Merge branch 'linus' into sched/core
[linux-3.10.git] / arch / x86 / kernel / apic / nmi.c
1 /*
2  *  NMI watchdog support on APIC systems
3  *
4  *  Started by Ingo Molnar <mingo@redhat.com>
5  *
6  *  Fixes:
7  *  Mikael Pettersson   : AMD K7 support for local APIC NMI watchdog.
8  *  Mikael Pettersson   : Power Management for local APIC NMI watchdog.
9  *  Mikael Pettersson   : Pentium 4 support for local APIC NMI watchdog.
10  *  Pavel Machek and
11  *  Mikael Pettersson   : PM converted to driver model. Disable/enable API.
12  */
13
14 #include <asm/apic.h>
15
16 #include <linux/nmi.h>
17 #include <linux/mm.h>
18 #include <linux/delay.h>
19 #include <linux/interrupt.h>
20 #include <linux/module.h>
21 #include <linux/slab.h>
22 #include <linux/sysdev.h>
23 #include <linux/sysctl.h>
24 #include <linux/percpu.h>
25 #include <linux/kprobes.h>
26 #include <linux/cpumask.h>
27 #include <linux/kernel_stat.h>
28 #include <linux/kdebug.h>
29 #include <linux/smp.h>
30
31 #include <asm/i8259.h>
32 #include <asm/io_apic.h>
33 #include <asm/proto.h>
34 #include <asm/timer.h>
35
36 #include <asm/mce.h>
37
38 #include <asm/mach_traps.h>
39
40 int unknown_nmi_panic;
41 int nmi_watchdog_enabled;
42
43 /* For reliability, we're prepared to waste bits here. */
44 static DECLARE_BITMAP(backtrace_mask, NR_CPUS) __read_mostly;
45
46 /* nmi_active:
47  * >0: the lapic NMI watchdog is active, but can be disabled
48  * <0: the lapic NMI watchdog has not been set up, and cannot
49  *     be enabled
50  *  0: the lapic NMI watchdog is disabled, but can be enabled
51  */
52 atomic_t nmi_active = ATOMIC_INIT(0);           /* oprofile uses this */
53 EXPORT_SYMBOL(nmi_active);
54
55 unsigned int nmi_watchdog = NMI_NONE;
56 EXPORT_SYMBOL(nmi_watchdog);
57
58 static int panic_on_timeout;
59
60 static unsigned int nmi_hz = HZ;
61 static DEFINE_PER_CPU(short, wd_enabled);
62 static int endflag __initdata;
63
64 static inline unsigned int get_nmi_count(int cpu)
65 {
66         return per_cpu(irq_stat, cpu).__nmi_count;
67 }
68
69 static inline int mce_in_progress(void)
70 {
71 #if defined(CONFIG_X86_MCE)
72         return atomic_read(&mce_entry) > 0;
73 #endif
74         return 0;
75 }
76
77 /*
78  * Take the local apic timer and PIT/HPET into account. We don't
79  * know which one is active, when we have highres/dyntick on
80  */
81 static inline unsigned int get_timer_irqs(int cpu)
82 {
83         return per_cpu(irq_stat, cpu).apic_timer_irqs +
84                 per_cpu(irq_stat, cpu).irq0_irqs;
85 }
86
87 #ifdef CONFIG_SMP
88 /*
89  * The performance counters used by NMI_LOCAL_APIC don't trigger when
90  * the CPU is idle. To make sure the NMI watchdog really ticks on all
91  * CPUs during the test make them busy.
92  */
93 static __init void nmi_cpu_busy(void *data)
94 {
95         local_irq_enable_in_hardirq();
96         /*
97          * Intentionally don't use cpu_relax here. This is
98          * to make sure that the performance counter really ticks,
99          * even if there is a simulator or similar that catches the
100          * pause instruction. On a real HT machine this is fine because
101          * all other CPUs are busy with "useless" delay loops and don't
102          * care if they get somewhat less cycles.
103          */
104         while (endflag == 0)
105                 mb();
106 }
107 #endif
108
109 static void report_broken_nmi(int cpu, unsigned int *prev_nmi_count)
110 {
111         printk(KERN_CONT "\n");
112
113         printk(KERN_WARNING
114                 "WARNING: CPU#%d: NMI appears to be stuck (%d->%d)!\n",
115                         cpu, prev_nmi_count[cpu], get_nmi_count(cpu));
116
117         printk(KERN_WARNING
118                 "Please report this to bugzilla.kernel.org,\n");
119         printk(KERN_WARNING
120                 "and attach the output of the 'dmesg' command.\n");
121
122         per_cpu(wd_enabled, cpu) = 0;
123         atomic_dec(&nmi_active);
124 }
125
126 static void __acpi_nmi_disable(void *__unused)
127 {
128         apic_write(APIC_LVT0, APIC_DM_NMI | APIC_LVT_MASKED);
129 }
130
131 int __init check_nmi_watchdog(void)
132 {
133         unsigned int *prev_nmi_count;
134         int cpu;
135
136         if (!nmi_watchdog_active() || !atomic_read(&nmi_active))
137                 return 0;
138
139         prev_nmi_count = kmalloc(nr_cpu_ids * sizeof(int), GFP_KERNEL);
140         if (!prev_nmi_count)
141                 goto error;
142
143         printk(KERN_INFO "Testing NMI watchdog ... ");
144
145 #ifdef CONFIG_SMP
146         if (nmi_watchdog == NMI_LOCAL_APIC)
147                 smp_call_function(nmi_cpu_busy, (void *)&endflag, 0);
148 #endif
149
150         for_each_possible_cpu(cpu)
151                 prev_nmi_count[cpu] = get_nmi_count(cpu);
152         local_irq_enable();
153         mdelay((20 * 1000) / nmi_hz); /* wait 20 ticks */
154
155         for_each_online_cpu(cpu) {
156                 if (!per_cpu(wd_enabled, cpu))
157                         continue;
158                 if (get_nmi_count(cpu) - prev_nmi_count[cpu] <= 5)
159                         report_broken_nmi(cpu, prev_nmi_count);
160         }
161         endflag = 1;
162         if (!atomic_read(&nmi_active)) {
163                 kfree(prev_nmi_count);
164                 atomic_set(&nmi_active, -1);
165                 goto error;
166         }
167         printk("OK.\n");
168
169         /*
170          * now that we know it works we can reduce NMI frequency to
171          * something more reasonable; makes a difference in some configs
172          */
173         if (nmi_watchdog == NMI_LOCAL_APIC)
174                 nmi_hz = lapic_adjust_nmi_hz(1);
175
176         kfree(prev_nmi_count);
177         return 0;
178 error:
179         if (nmi_watchdog == NMI_IO_APIC) {
180                 if (!timer_through_8259)
181                         legacy_pic->chip->mask(0);
182                 on_each_cpu(__acpi_nmi_disable, NULL, 1);
183         }
184
185 #ifdef CONFIG_X86_32
186         timer_ack = 0;
187 #endif
188         return -1;
189 }
190
191 static int __init setup_nmi_watchdog(char *str)
192 {
193         unsigned int nmi;
194
195         if (!strncmp(str, "panic", 5)) {
196                 panic_on_timeout = 1;
197                 str = strchr(str, ',');
198                 if (!str)
199                         return 1;
200                 ++str;
201         }
202
203         if (!strncmp(str, "lapic", 5))
204                 nmi_watchdog = NMI_LOCAL_APIC;
205         else if (!strncmp(str, "ioapic", 6))
206                 nmi_watchdog = NMI_IO_APIC;
207         else {
208                 get_option(&str, &nmi);
209                 if (nmi >= NMI_INVALID)
210                         return 0;
211                 nmi_watchdog = nmi;
212         }
213
214         return 1;
215 }
216 __setup("nmi_watchdog=", setup_nmi_watchdog);
217
218 /*
219  * Suspend/resume support
220  */
221 #ifdef CONFIG_PM
222
223 static int nmi_pm_active; /* nmi_active before suspend */
224
225 static int lapic_nmi_suspend(struct sys_device *dev, pm_message_t state)
226 {
227         /* only CPU0 goes here, other CPUs should be offline */
228         nmi_pm_active = atomic_read(&nmi_active);
229         stop_apic_nmi_watchdog(NULL);
230         BUG_ON(atomic_read(&nmi_active) != 0);
231         return 0;
232 }
233
234 static int lapic_nmi_resume(struct sys_device *dev)
235 {
236         /* only CPU0 goes here, other CPUs should be offline */
237         if (nmi_pm_active > 0) {
238                 setup_apic_nmi_watchdog(NULL);
239                 touch_nmi_watchdog();
240         }
241         return 0;
242 }
243
244 static struct sysdev_class nmi_sysclass = {
245         .name           = "lapic_nmi",
246         .resume         = lapic_nmi_resume,
247         .suspend        = lapic_nmi_suspend,
248 };
249
250 static struct sys_device device_lapic_nmi = {
251         .id     = 0,
252         .cls    = &nmi_sysclass,
253 };
254
255 static int __init init_lapic_nmi_sysfs(void)
256 {
257         int error;
258
259         /*
260          * should really be a BUG_ON but b/c this is an
261          * init call, it just doesn't work.  -dcz
262          */
263         if (nmi_watchdog != NMI_LOCAL_APIC)
264                 return 0;
265
266         if (atomic_read(&nmi_active) < 0)
267                 return 0;
268
269         error = sysdev_class_register(&nmi_sysclass);
270         if (!error)
271                 error = sysdev_register(&device_lapic_nmi);
272         return error;
273 }
274
275 /* must come after the local APIC's device_initcall() */
276 late_initcall(init_lapic_nmi_sysfs);
277
278 #endif  /* CONFIG_PM */
279
280 static void __acpi_nmi_enable(void *__unused)
281 {
282         apic_write(APIC_LVT0, APIC_DM_NMI);
283 }
284
285 /*
286  * Enable timer based NMIs on all CPUs:
287  */
288 void acpi_nmi_enable(void)
289 {
290         if (atomic_read(&nmi_active) && nmi_watchdog == NMI_IO_APIC)
291                 on_each_cpu(__acpi_nmi_enable, NULL, 1);
292 }
293
294 /*
295  * Disable timer based NMIs on all CPUs:
296  */
297 void acpi_nmi_disable(void)
298 {
299         if (atomic_read(&nmi_active) && nmi_watchdog == NMI_IO_APIC)
300                 on_each_cpu(__acpi_nmi_disable, NULL, 1);
301 }
302
303 /*
304  * This function is called as soon the LAPIC NMI watchdog driver has everything
305  * in place and it's ready to check if the NMIs belong to the NMI watchdog
306  */
307 void cpu_nmi_set_wd_enabled(void)
308 {
309         __get_cpu_var(wd_enabled) = 1;
310 }
311
312 void setup_apic_nmi_watchdog(void *unused)
313 {
314         if (__get_cpu_var(wd_enabled))
315                 return;
316
317         /* cheap hack to support suspend/resume */
318         /* if cpu0 is not active neither should the other cpus */
319         if (smp_processor_id() != 0 && atomic_read(&nmi_active) <= 0)
320                 return;
321
322         switch (nmi_watchdog) {
323         case NMI_LOCAL_APIC:
324                 if (lapic_watchdog_init(nmi_hz) < 0) {
325                         __get_cpu_var(wd_enabled) = 0;
326                         return;
327                 }
328                 /* FALL THROUGH */
329         case NMI_IO_APIC:
330                 __get_cpu_var(wd_enabled) = 1;
331                 atomic_inc(&nmi_active);
332         }
333 }
334
335 void stop_apic_nmi_watchdog(void *unused)
336 {
337         /* only support LOCAL and IO APICs for now */
338         if (!nmi_watchdog_active())
339                 return;
340         if (__get_cpu_var(wd_enabled) == 0)
341                 return;
342         if (nmi_watchdog == NMI_LOCAL_APIC)
343                 lapic_watchdog_stop();
344         else
345                 __acpi_nmi_disable(NULL);
346         __get_cpu_var(wd_enabled) = 0;
347         atomic_dec(&nmi_active);
348 }
349
350 /*
351  * the best way to detect whether a CPU has a 'hard lockup' problem
352  * is to check it's local APIC timer IRQ counts. If they are not
353  * changing then that CPU has some problem.
354  *
355  * as these watchdog NMI IRQs are generated on every CPU, we only
356  * have to check the current processor.
357  *
358  * since NMIs don't listen to _any_ locks, we have to be extremely
359  * careful not to rely on unsafe variables. The printk might lock
360  * up though, so we have to break up any console locks first ...
361  * [when there will be more tty-related locks, break them up here too!]
362  */
363
364 static DEFINE_PER_CPU(unsigned, last_irq_sum);
365 static DEFINE_PER_CPU(long, alert_counter);
366 static DEFINE_PER_CPU(int, nmi_touch);
367
368 void touch_nmi_watchdog(void)
369 {
370         if (nmi_watchdog_active()) {
371                 unsigned cpu;
372
373                 /*
374                  * Tell other CPUs to reset their alert counters. We cannot
375                  * do it ourselves because the alert count increase is not
376                  * atomic.
377                  */
378                 for_each_present_cpu(cpu) {
379                         if (per_cpu(nmi_touch, cpu) != 1)
380                                 per_cpu(nmi_touch, cpu) = 1;
381                 }
382         }
383
384         /*
385          * Tickle the softlockup detector too:
386          */
387         touch_softlockup_watchdog();
388 }
389 EXPORT_SYMBOL(touch_nmi_watchdog);
390
391 notrace __kprobes int
392 nmi_watchdog_tick(struct pt_regs *regs, unsigned reason)
393 {
394         /*
395          * Since current_thread_info()-> is always on the stack, and we
396          * always switch the stack NMI-atomically, it's safe to use
397          * smp_processor_id().
398          */
399         unsigned int sum;
400         int touched = 0;
401         int cpu = smp_processor_id();
402         int rc = 0;
403
404         /* check for other users first */
405         if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT)
406                         == NOTIFY_STOP) {
407                 rc = 1;
408                 touched = 1;
409         }
410
411         sum = get_timer_irqs(cpu);
412
413         if (__get_cpu_var(nmi_touch)) {
414                 __get_cpu_var(nmi_touch) = 0;
415                 touched = 1;
416         }
417
418         /* We can be called before check_nmi_watchdog, hence NULL check. */
419         if (cpumask_test_cpu(cpu, to_cpumask(backtrace_mask))) {
420                 static DEFINE_RAW_SPINLOCK(lock); /* Serialise the printks */
421
422                 raw_spin_lock(&lock);
423                 printk(KERN_WARNING "NMI backtrace for cpu %d\n", cpu);
424                 show_regs(regs);
425                 dump_stack();
426                 raw_spin_unlock(&lock);
427                 cpumask_clear_cpu(cpu, to_cpumask(backtrace_mask));
428
429                 rc = 1;
430         }
431
432         /* Could check oops_in_progress here too, but it's safer not to */
433         if (mce_in_progress())
434                 touched = 1;
435
436         /* if the none of the timers isn't firing, this cpu isn't doing much */
437         if (!touched && __get_cpu_var(last_irq_sum) == sum) {
438                 /*
439                  * Ayiee, looks like this CPU is stuck ...
440                  * wait a few IRQs (5 seconds) before doing the oops ...
441                  */
442                 __this_cpu_inc(alert_counter);
443                 if (__this_cpu_read(alert_counter) == 5 * nmi_hz)
444                         /*
445                          * die_nmi will return ONLY if NOTIFY_STOP happens..
446                          */
447                         die_nmi("BUG: NMI Watchdog detected LOCKUP",
448                                 regs, panic_on_timeout);
449         } else {
450                 __get_cpu_var(last_irq_sum) = sum;
451                 __this_cpu_write(alert_counter, 0);
452         }
453
454         /* see if the nmi watchdog went off */
455         if (!__get_cpu_var(wd_enabled))
456                 return rc;
457         switch (nmi_watchdog) {
458         case NMI_LOCAL_APIC:
459                 rc |= lapic_wd_event(nmi_hz);
460                 break;
461         case NMI_IO_APIC:
462                 /*
463                  * don't know how to accurately check for this.
464                  * just assume it was a watchdog timer interrupt
465                  * This matches the old behaviour.
466                  */
467                 rc = 1;
468                 break;
469         }
470         return rc;
471 }
472
473 #ifdef CONFIG_SYSCTL
474
475 static void enable_ioapic_nmi_watchdog_single(void *unused)
476 {
477         __get_cpu_var(wd_enabled) = 1;
478         atomic_inc(&nmi_active);
479         __acpi_nmi_enable(NULL);
480 }
481
482 static void enable_ioapic_nmi_watchdog(void)
483 {
484         on_each_cpu(enable_ioapic_nmi_watchdog_single, NULL, 1);
485         touch_nmi_watchdog();
486 }
487
488 static void disable_ioapic_nmi_watchdog(void)
489 {
490         on_each_cpu(stop_apic_nmi_watchdog, NULL, 1);
491 }
492
493 static int __init setup_unknown_nmi_panic(char *str)
494 {
495         unknown_nmi_panic = 1;
496         return 1;
497 }
498 __setup("unknown_nmi_panic", setup_unknown_nmi_panic);
499
500 static int unknown_nmi_panic_callback(struct pt_regs *regs, int cpu)
501 {
502         unsigned char reason = get_nmi_reason();
503         char buf[64];
504
505         sprintf(buf, "NMI received for unknown reason %02x\n", reason);
506         die_nmi(buf, regs, 1); /* Always panic here */
507         return 0;
508 }
509
510 /*
511  * proc handler for /proc/sys/kernel/nmi
512  */
513 int proc_nmi_enabled(struct ctl_table *table, int write,
514                         void __user *buffer, size_t *length, loff_t *ppos)
515 {
516         int old_state;
517
518         nmi_watchdog_enabled = (atomic_read(&nmi_active) > 0) ? 1 : 0;
519         old_state = nmi_watchdog_enabled;
520         proc_dointvec(table, write, buffer, length, ppos);
521         if (!!old_state == !!nmi_watchdog_enabled)
522                 return 0;
523
524         if (atomic_read(&nmi_active) < 0 || !nmi_watchdog_active()) {
525                 printk(KERN_WARNING
526                         "NMI watchdog is permanently disabled\n");
527                 return -EIO;
528         }
529
530         if (nmi_watchdog == NMI_LOCAL_APIC) {
531                 if (nmi_watchdog_enabled)
532                         enable_lapic_nmi_watchdog();
533                 else
534                         disable_lapic_nmi_watchdog();
535         } else if (nmi_watchdog == NMI_IO_APIC) {
536                 if (nmi_watchdog_enabled)
537                         enable_ioapic_nmi_watchdog();
538                 else
539                         disable_ioapic_nmi_watchdog();
540         } else {
541                 printk(KERN_WARNING
542                         "NMI watchdog doesn't know what hardware to touch\n");
543                 return -EIO;
544         }
545         return 0;
546 }
547
548 #endif /* CONFIG_SYSCTL */
549
550 int do_nmi_callback(struct pt_regs *regs, int cpu)
551 {
552 #ifdef CONFIG_SYSCTL
553         if (unknown_nmi_panic)
554                 return unknown_nmi_panic_callback(regs, cpu);
555 #endif
556         return 0;
557 }
558
559 void arch_trigger_all_cpu_backtrace(void)
560 {
561         int i;
562
563         cpumask_copy(to_cpumask(backtrace_mask), cpu_online_mask);
564
565         printk(KERN_INFO "sending NMI to all CPUs:\n");
566         apic->send_IPI_all(NMI_VECTOR);
567
568         /* Wait for up to 10 seconds for all CPUs to do the backtrace */
569         for (i = 0; i < 10 * 1000; i++) {
570                 if (cpumask_empty(to_cpumask(backtrace_mask)))
571                         break;
572                 mdelay(1);
573         }
574 }