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