x86: call check_nmi_watchdog explicitly in native_smp_cpus_done
[safe/jmp/linux-2.6] / arch / x86 / kernel / nmi_32.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 <linux/delay.h>
15 #include <linux/interrupt.h>
16 #include <linux/module.h>
17 #include <linux/nmi.h>
18 #include <linux/sysdev.h>
19 #include <linux/sysctl.h>
20 #include <linux/percpu.h>
21 #include <linux/kprobes.h>
22 #include <linux/cpumask.h>
23 #include <linux/kernel_stat.h>
24 #include <linux/kdebug.h>
25
26 #include <asm/smp.h>
27 #include <asm/nmi.h>
28
29 #include "mach_traps.h"
30
31 int unknown_nmi_panic;
32 int nmi_watchdog_enabled;
33
34 static cpumask_t backtrace_mask = CPU_MASK_NONE;
35
36 /* nmi_active:
37  * >0: the lapic NMI watchdog is active, but can be disabled
38  * <0: the lapic NMI watchdog has not been set up, and cannot
39  *     be enabled
40  *  0: the lapic NMI watchdog is disabled, but can be enabled
41  */
42 atomic_t nmi_active = ATOMIC_INIT(0);           /* oprofile uses this */
43
44 unsigned int nmi_watchdog = NMI_DEFAULT;
45 static unsigned int nmi_hz = HZ;
46
47 static DEFINE_PER_CPU(short, wd_enabled);
48
49 static int endflag __initdata = 0;
50
51 #ifdef CONFIG_SMP
52 /* The performance counters used by NMI_LOCAL_APIC don't trigger when
53  * the CPU is idle. To make sure the NMI watchdog really ticks on all
54  * CPUs during the test make them busy.
55  */
56 static __init void nmi_cpu_busy(void *data)
57 {
58         local_irq_enable_in_hardirq();
59         /* Intentionally don't use cpu_relax here. This is
60            to make sure that the performance counter really ticks,
61            even if there is a simulator or similar that catches the
62            pause instruction. On a real HT machine this is fine because
63            all other CPUs are busy with "useless" delay loops and don't
64            care if they get somewhat less cycles. */
65         while (endflag == 0)
66                 mb();
67 }
68 #endif
69
70 int __init check_nmi_watchdog(void)
71 {
72         unsigned int *prev_nmi_count;
73         int cpu;
74
75         if ((nmi_watchdog == NMI_NONE) || (nmi_watchdog == NMI_DISABLED))
76                 return 0;
77
78         if (!atomic_read(&nmi_active))
79                 return 0;
80
81         prev_nmi_count = kmalloc(NR_CPUS * sizeof(int), GFP_KERNEL);
82         if (!prev_nmi_count)
83                 return -1;
84
85         printk(KERN_INFO "Testing NMI watchdog ... ");
86
87 #ifdef CONFIG_SMP
88         if (nmi_watchdog == NMI_LOCAL_APIC)
89                 smp_call_function(nmi_cpu_busy, (void *)&endflag, 0, 0);
90 #endif
91
92         for_each_possible_cpu(cpu)
93                 prev_nmi_count[cpu] = nmi_count(cpu);
94         local_irq_enable();
95         mdelay((20*1000)/nmi_hz); // wait 20 ticks
96
97         for_each_possible_cpu(cpu) {
98 #ifdef CONFIG_SMP
99                 /* Check cpu_callin_map here because that is set
100                    after the timer is started. */
101                 if (!cpu_isset(cpu, cpu_callin_map))
102                         continue;
103 #endif
104                 if (!per_cpu(wd_enabled, cpu))
105                         continue;
106                 if (nmi_count(cpu) - prev_nmi_count[cpu] <= 5) {
107                         printk(KERN_WARNING "WARNING: CPU#%d: NMI "
108                                 "appears to be stuck (%d->%d)!\n",
109                                 cpu,
110                                 prev_nmi_count[cpu],
111                                 nmi_count(cpu));
112                         per_cpu(wd_enabled, cpu) = 0;
113                         atomic_dec(&nmi_active);
114                 }
115         }
116         endflag = 1;
117         if (!atomic_read(&nmi_active)) {
118                 kfree(prev_nmi_count);
119                 atomic_set(&nmi_active, -1);
120                 return -1;
121         }
122         printk("OK.\n");
123
124         /* now that we know it works we can reduce NMI frequency to
125            something more reasonable; makes a difference in some configs */
126         if (nmi_watchdog == NMI_LOCAL_APIC)
127                 nmi_hz = lapic_adjust_nmi_hz(1);
128
129         kfree(prev_nmi_count);
130         return 0;
131 }
132
133 static int __init setup_nmi_watchdog(char *str)
134 {
135         int nmi;
136
137         get_option(&str, &nmi);
138
139         if ((nmi >= NMI_INVALID) || (nmi < NMI_NONE))
140                 return 0;
141
142         nmi_watchdog = nmi;
143         return 1;
144 }
145
146 __setup("nmi_watchdog=", setup_nmi_watchdog);
147
148
149 /* Suspend/resume support */
150
151 #ifdef CONFIG_PM
152
153 static int nmi_pm_active; /* nmi_active before suspend */
154
155 static int lapic_nmi_suspend(struct sys_device *dev, pm_message_t state)
156 {
157         /* only CPU0 goes here, other CPUs should be offline */
158         nmi_pm_active = atomic_read(&nmi_active);
159         stop_apic_nmi_watchdog(NULL);
160         BUG_ON(atomic_read(&nmi_active) != 0);
161         return 0;
162 }
163
164 static int lapic_nmi_resume(struct sys_device *dev)
165 {
166         /* only CPU0 goes here, other CPUs should be offline */
167         if (nmi_pm_active > 0) {
168                 setup_apic_nmi_watchdog(NULL);
169                 touch_nmi_watchdog();
170         }
171         return 0;
172 }
173
174
175 static struct sysdev_class nmi_sysclass = {
176         .name           = "lapic_nmi",
177         .resume         = lapic_nmi_resume,
178         .suspend        = lapic_nmi_suspend,
179 };
180
181 static struct sys_device device_lapic_nmi = {
182         .id     = 0,
183         .cls    = &nmi_sysclass,
184 };
185
186 static int __init init_lapic_nmi_sysfs(void)
187 {
188         int error;
189
190         /* should really be a BUG_ON but b/c this is an
191          * init call, it just doesn't work.  -dcz
192          */
193         if (nmi_watchdog != NMI_LOCAL_APIC)
194                 return 0;
195
196         if (atomic_read(&nmi_active) < 0)
197                 return 0;
198
199         error = sysdev_class_register(&nmi_sysclass);
200         if (!error)
201                 error = sysdev_register(&device_lapic_nmi);
202         return error;
203 }
204 /* must come after the local APIC's device_initcall() */
205 late_initcall(init_lapic_nmi_sysfs);
206
207 #endif  /* CONFIG_PM */
208
209 static void __acpi_nmi_enable(void *__unused)
210 {
211         apic_write_around(APIC_LVT0, APIC_DM_NMI);
212 }
213
214 /*
215  * Enable timer based NMIs on all CPUs:
216  */
217 void acpi_nmi_enable(void)
218 {
219         if (atomic_read(&nmi_active) && nmi_watchdog == NMI_IO_APIC)
220                 on_each_cpu(__acpi_nmi_enable, NULL, 0, 1);
221 }
222
223 static void __acpi_nmi_disable(void *__unused)
224 {
225         apic_write(APIC_LVT0, APIC_DM_NMI | APIC_LVT_MASKED);
226 }
227
228 /*
229  * Disable timer based NMIs on all CPUs:
230  */
231 void acpi_nmi_disable(void)
232 {
233         if (atomic_read(&nmi_active) && nmi_watchdog == NMI_IO_APIC)
234                 on_each_cpu(__acpi_nmi_disable, NULL, 0, 1);
235 }
236
237 void setup_apic_nmi_watchdog(void *unused)
238 {
239         if (__get_cpu_var(wd_enabled))
240                 return;
241
242         /* cheap hack to support suspend/resume */
243         /* if cpu0 is not active neither should the other cpus */
244         if ((smp_processor_id() != 0) && (atomic_read(&nmi_active) <= 0))
245                 return;
246
247         switch (nmi_watchdog) {
248         case NMI_LOCAL_APIC:
249                 __get_cpu_var(wd_enabled) = 1; /* enable it before to avoid race with handler */
250                 if (lapic_watchdog_init(nmi_hz) < 0) {
251                         __get_cpu_var(wd_enabled) = 0;
252                         return;
253                 }
254                 /* FALL THROUGH */
255         case NMI_IO_APIC:
256                 __get_cpu_var(wd_enabled) = 1;
257                 atomic_inc(&nmi_active);
258         }
259 }
260
261 void stop_apic_nmi_watchdog(void *unused)
262 {
263         /* only support LOCAL and IO APICs for now */
264         if ((nmi_watchdog != NMI_LOCAL_APIC) &&
265             (nmi_watchdog != NMI_IO_APIC))
266                 return;
267         if (__get_cpu_var(wd_enabled) == 0)
268                 return;
269         if (nmi_watchdog == NMI_LOCAL_APIC)
270                 lapic_watchdog_stop();
271         __get_cpu_var(wd_enabled) = 0;
272         atomic_dec(&nmi_active);
273 }
274
275 /*
276  * the best way to detect whether a CPU has a 'hard lockup' problem
277  * is to check it's local APIC timer IRQ counts. If they are not
278  * changing then that CPU has some problem.
279  *
280  * as these watchdog NMI IRQs are generated on every CPU, we only
281  * have to check the current processor.
282  *
283  * since NMIs don't listen to _any_ locks, we have to be extremely
284  * careful not to rely on unsafe variables. The printk might lock
285  * up though, so we have to break up any console locks first ...
286  * [when there will be more tty-related locks, break them up
287  *  here too!]
288  */
289
290 static unsigned int
291         last_irq_sums [NR_CPUS],
292         alert_counter [NR_CPUS];
293
294 void touch_nmi_watchdog(void)
295 {
296         if (nmi_watchdog > 0) {
297                 unsigned cpu;
298
299                 /*
300                  * Just reset the alert counters, (other CPUs might be
301                  * spinning on locks we hold):
302                  */
303                 for_each_present_cpu(cpu) {
304                         if (alert_counter[cpu])
305                                 alert_counter[cpu] = 0;
306                 }
307         }
308
309         /*
310          * Tickle the softlockup detector too:
311          */
312         touch_softlockup_watchdog();
313 }
314 EXPORT_SYMBOL(touch_nmi_watchdog);
315
316 extern void die_nmi(struct pt_regs *, const char *msg);
317
318 __kprobes int nmi_watchdog_tick(struct pt_regs * regs, unsigned reason)
319 {
320
321         /*
322          * Since current_thread_info()-> is always on the stack, and we
323          * always switch the stack NMI-atomically, it's safe to use
324          * smp_processor_id().
325          */
326         unsigned int sum;
327         int touched = 0;
328         int cpu = smp_processor_id();
329         int rc = 0;
330
331         /* check for other users first */
332         if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT)
333                         == NOTIFY_STOP) {
334                 rc = 1;
335                 touched = 1;
336         }
337
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
348         /*
349          * Take the local apic timer and PIT/HPET into account. We don't
350          * know which one is active, when we have highres/dyntick on
351          */
352         sum = per_cpu(irq_stat, cpu).apic_timer_irqs +
353                 per_cpu(irq_stat, cpu).irq0_irqs;
354
355         /* if the none of the timers isn't firing, this cpu isn't doing much */
356         if (!touched && last_irq_sums[cpu] == sum) {
357                 /*
358                  * Ayiee, looks like this CPU is stuck ...
359                  * wait a few IRQs (5 seconds) before doing the oops ...
360                  */
361                 alert_counter[cpu]++;
362                 if (alert_counter[cpu] == 5*nmi_hz)
363                         /*
364                          * die_nmi will return ONLY if NOTIFY_STOP happens..
365                          */
366                         die_nmi(regs, "BUG: NMI Watchdog detected LOCKUP");
367         } else {
368                 last_irq_sums[cpu] = sum;
369                 alert_counter[cpu] = 0;
370         }
371         /* see if the nmi watchdog went off */
372         if (!__get_cpu_var(wd_enabled))
373                 return rc;
374         switch (nmi_watchdog) {
375         case NMI_LOCAL_APIC:
376                 rc |= lapic_wd_event(nmi_hz);
377                 break;
378         case NMI_IO_APIC:
379                 /* don't know how to accurately check for this.
380                  * just assume it was a watchdog timer interrupt
381                  * This matches the old behaviour.
382                  */
383                 rc = 1;
384                 break;
385         }
386         return rc;
387 }
388
389 #ifdef CONFIG_SYSCTL
390
391 static int unknown_nmi_panic_callback(struct pt_regs *regs, int cpu)
392 {
393         unsigned char reason = get_nmi_reason();
394         char buf[64];
395
396         sprintf(buf, "NMI received for unknown reason %02x\n", reason);
397         die_nmi(regs, buf);
398         return 0;
399 }
400
401 /*
402  * proc handler for /proc/sys/kernel/nmi
403  */
404 int proc_nmi_enabled(struct ctl_table *table, int write, struct file *file,
405                         void __user *buffer, size_t *length, loff_t *ppos)
406 {
407         int old_state;
408
409         nmi_watchdog_enabled = (atomic_read(&nmi_active) > 0) ? 1 : 0;
410         old_state = nmi_watchdog_enabled;
411         proc_dointvec(table, write, file, buffer, length, ppos);
412         if (!!old_state == !!nmi_watchdog_enabled)
413                 return 0;
414
415         if (atomic_read(&nmi_active) < 0 || nmi_watchdog == NMI_DISABLED) {
416                 printk( KERN_WARNING "NMI watchdog is permanently disabled\n");
417                 return -EIO;
418         }
419
420         if (nmi_watchdog == NMI_DEFAULT) {
421                 if (lapic_watchdog_ok())
422                         nmi_watchdog = NMI_LOCAL_APIC;
423                 else
424                         nmi_watchdog = NMI_IO_APIC;
425         }
426
427         if (nmi_watchdog == NMI_LOCAL_APIC) {
428                 if (nmi_watchdog_enabled)
429                         enable_lapic_nmi_watchdog();
430                 else
431                         disable_lapic_nmi_watchdog();
432         } else {
433                 printk( KERN_WARNING
434                         "NMI watchdog doesn't know what hardware to touch\n");
435                 return -EIO;
436         }
437         return 0;
438 }
439
440 #endif
441
442 int do_nmi_callback(struct pt_regs *regs, int cpu)
443 {
444 #ifdef CONFIG_SYSCTL
445         if (unknown_nmi_panic)
446                 return unknown_nmi_panic_callback(regs, cpu);
447 #endif
448         return 0;
449 }
450
451 void __trigger_all_cpu_backtrace(void)
452 {
453         int i;
454
455         backtrace_mask = cpu_online_map;
456         /* Wait for up to 10 seconds for all CPUs to do the backtrace */
457         for (i = 0; i < 10 * 1000; i++) {
458                 if (cpus_empty(backtrace_mask))
459                         break;
460                 mdelay(1);
461         }
462 }
463
464 EXPORT_SYMBOL(nmi_active);
465 EXPORT_SYMBOL(nmi_watchdog);