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