blob: cbd4fa3c475ba4ec95709f5514a88a626301a17f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * 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
Ingo Molnar3d1ba1d2008-06-02 13:50:10 +020014#include <asm/apic.h>
15
Cyrill Gorcunov1798bc22008-05-24 19:36:41 +040016#include <linux/nmi.h>
17#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/sysdev.h>
22#include <linux/sysctl.h>
Don Zickus3e4ff112006-06-26 13:57:01 +020023#include <linux/percpu.h>
Fernando Luis Vázquez Cao06039752006-09-26 10:52:36 +020024#include <linux/kprobes.h>
Andrew Mortonbb81a092006-12-07 02:14:01 +010025#include <linux/cpumask.h>
Thomas Gleixnerf8b50352007-02-16 01:28:09 -080026#include <linux/kernel_stat.h>
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -070027#include <linux/kdebug.h>
Hiroshi Shimamoto88ff0a42008-05-27 18:49:39 -070028#include <linux/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Cyrill Gorcunov1798bc22008-05-24 19:36:41 +040030#include <asm/proto.h>
Ingo Molnar6e908942008-03-21 14:32:36 +010031#include <asm/timer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Cyrill Gorcunov1798bc22008-05-24 19:36:41 +040033#include <asm/mce.h>
34
35#include <mach_traps.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Andi Kleen29cbc782006-09-30 01:47:55 +020037int unknown_nmi_panic;
38int nmi_watchdog_enabled;
39
Andi Kleen1714f9b2007-04-16 10:30:27 +020040static cpumask_t backtrace_mask = CPU_MASK_NONE;
Andi Kleen09198e62007-05-02 19:27:20 +020041
Linus Torvalds1da177e2005-04-16 15:20:36 -070042/* nmi_active:
Don Zickusb7471c62006-09-26 10:52:26 +020043 * >0: the lapic NMI watchdog is active, but can be disabled
44 * <0: the lapic NMI watchdog has not been set up, and cannot
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 * be enabled
Don Zickusb7471c62006-09-26 10:52:26 +020046 * 0: the lapic NMI watchdog is disabled, but can be enabled
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 */
Don Zickusb7471c62006-09-26 10:52:26 +020048atomic_t nmi_active = ATOMIC_INIT(0); /* oprofile uses this */
Hiroshi Shimamoto88ff0a42008-05-27 18:49:39 -070049EXPORT_SYMBOL(nmi_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Don Zickusb7471c62006-09-26 10:52:26 +020051unsigned int nmi_watchdog = NMI_DEFAULT;
Hiroshi Shimamoto88ff0a42008-05-27 18:49:39 -070052EXPORT_SYMBOL(nmi_watchdog);
53
54static int panic_on_timeout;
Cyrill Gorcunov1798bc22008-05-24 19:36:41 +040055
Don Zickusb7471c62006-09-26 10:52:26 +020056static unsigned int nmi_hz = HZ;
Andi Kleen09198e62007-05-02 19:27:20 +020057static DEFINE_PER_CPU(short, wd_enabled);
Hiroshi Shimamoto88ff0a42008-05-27 18:49:39 -070058static int endflag __initdata;
Ravikiran G Thirumalai92715e22006-12-09 21:33:35 +010059
Cyrill Gorcunovfd5cea02008-05-24 19:36:40 +040060static inline unsigned int get_nmi_count(int cpu)
61{
Cyrill Gorcunov1798bc22008-05-24 19:36:41 +040062#ifdef CONFIG_X86_64
63 return cpu_pda(cpu)->__nmi_count;
64#else
Cyrill Gorcunovfd5cea02008-05-24 19:36:40 +040065 return nmi_count(cpu);
Cyrill Gorcunov1798bc22008-05-24 19:36:41 +040066#endif
Cyrill Gorcunovfd5cea02008-05-24 19:36:40 +040067}
68
69static inline int mce_in_progress(void)
70{
Cyrill Gorcunov1798bc22008-05-24 19:36:41 +040071#if defined(CONFIX_X86_64) && defined(CONFIG_X86_MCE)
72 return atomic_read(&mce_entry) > 0;
73#endif
Cyrill Gorcunovfd5cea02008-05-24 19:36:40 +040074 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 */
81static inline unsigned int get_timer_irqs(int cpu)
82{
Cyrill Gorcunov1798bc22008-05-24 19:36:41 +040083#ifdef CONFIG_X86_64
84 return read_pda(apic_timer_irqs) + read_pda(irq0_irqs);
85#else
Cyrill Gorcunovfd5cea02008-05-24 19:36:40 +040086 return per_cpu(irq_stat, cpu).apic_timer_irqs +
87 per_cpu(irq_stat, cpu).irq0_irqs;
Cyrill Gorcunov1798bc22008-05-24 19:36:41 +040088#endif
Cyrill Gorcunovfd5cea02008-05-24 19:36:40 +040089}
90
Cyrill Gorcunov19ec6732008-05-28 23:00:47 +040091#ifdef CONFIG_X86_64
Cyrill Gorcunov4b82b272008-05-24 19:36:35 +040092/* Run after command line and cpu_init init, but before all other checks */
93void nmi_watchdog_default(void)
94{
95 if (nmi_watchdog != NMI_DEFAULT)
96 return;
Cyrill Gorcunov1798bc22008-05-24 19:36:41 +040097 nmi_watchdog = NMI_NONE;
Cyrill Gorcunov4b82b272008-05-24 19:36:35 +040098}
Cyrill Gorcunov19ec6732008-05-28 23:00:47 +040099#endif
Cyrill Gorcunov4b82b272008-05-24 19:36:35 +0400100
Hiroshi Shimamoto416b7212008-01-30 13:30:33 +0100101#ifdef CONFIG_SMP
Cyrill Gorcunov1798bc22008-05-24 19:36:41 +0400102/*
103 * The performance counters used by NMI_LOCAL_APIC don't trigger when
Eric W. Biederman29b70082005-10-30 14:59:40 -0800104 * the CPU is idle. To make sure the NMI watchdog really ticks on all
105 * CPUs during the test make them busy.
106 */
107static __init void nmi_cpu_busy(void *data)
108{
Ingo Molnar366c7f52006-07-03 00:25:25 -0700109 local_irq_enable_in_hardirq();
Cyrill Gorcunov1798bc22008-05-24 19:36:41 +0400110 /*
111 * Intentionally don't use cpu_relax here. This is
112 * to make sure that the performance counter really ticks,
113 * even if there is a simulator or similar that catches the
114 * pause instruction. On a real HT machine this is fine because
115 * all other CPUs are busy with "useless" delay loops and don't
116 * care if they get somewhat less cycles.
117 */
Ravikiran G Thirumalai92715e22006-12-09 21:33:35 +0100118 while (endflag == 0)
119 mb();
Ingo Molnar04920072007-11-09 22:39:38 +0100120}
Hiroshi Shimamoto416b7212008-01-30 13:30:33 +0100121#endif
Eric W. Biederman29b70082005-10-30 14:59:40 -0800122
Glauber de Oliveira Costa6d60cd52008-03-19 14:25:36 -0300123int __init check_nmi_watchdog(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
Eric W. Biederman29b70082005-10-30 14:59:40 -0800125 unsigned int *prev_nmi_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 int cpu;
127
Cyrill Gorcunov1798bc22008-05-24 19:36:41 +0400128 if (nmi_watchdog == NMI_NONE || nmi_watchdog == NMI_DISABLED)
Don Zickusb7471c62006-09-26 10:52:26 +0200129 return 0;
130
131 if (!atomic_read(&nmi_active))
Jack F Vogel67701ae2005-05-01 08:58:48 -0700132 return 0;
133
Eric W. Biederman29b70082005-10-30 14:59:40 -0800134 prev_nmi_count = kmalloc(NR_CPUS * sizeof(int), GFP_KERNEL);
135 if (!prev_nmi_count)
Ingo Molnar6e908942008-03-21 14:32:36 +0100136 goto error;
Eric W. Biederman29b70082005-10-30 14:59:40 -0800137
Jack F Vogel67701ae2005-05-01 08:58:48 -0700138 printk(KERN_INFO "Testing NMI watchdog ... ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Hiroshi Shimamoto416b7212008-01-30 13:30:33 +0100140#ifdef CONFIG_SMP
Eric W. Biederman29b70082005-10-30 14:59:40 -0800141 if (nmi_watchdog == NMI_LOCAL_APIC)
142 smp_call_function(nmi_cpu_busy, (void *)&endflag, 0, 0);
Hiroshi Shimamoto416b7212008-01-30 13:30:33 +0100143#endif
Eric W. Biederman29b70082005-10-30 14:59:40 -0800144
KAMEZAWA Hiroyukic8912592006-03-28 01:56:39 -0800145 for_each_possible_cpu(cpu)
Cyrill Gorcunovfd5cea02008-05-24 19:36:40 +0400146 prev_nmi_count[cpu] = get_nmi_count(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 local_irq_enable();
Cyrill Gorcunov1798bc22008-05-24 19:36:41 +0400148 mdelay((20 * 1000) / nmi_hz); /* wait 20 ticks */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Cyrill Gorcunov7c2ba832008-05-24 19:36:39 +0400150 for_each_online_cpu(cpu) {
Andi Kleen09198e62007-05-02 19:27:20 +0200151 if (!per_cpu(wd_enabled, cpu))
Don Zickusb7471c62006-09-26 10:52:26 +0200152 continue;
Cyrill Gorcunovfd5cea02008-05-24 19:36:40 +0400153 if (get_nmi_count(cpu) - prev_nmi_count[cpu] <= 5) {
Don Zickus75bc1222007-12-04 17:19:07 +0100154 printk(KERN_WARNING "WARNING: CPU#%d: NMI "
155 "appears to be stuck (%d->%d)!\n",
Eric W. Biederman29b70082005-10-30 14:59:40 -0800156 cpu,
157 prev_nmi_count[cpu],
Cyrill Gorcunovfd5cea02008-05-24 19:36:40 +0400158 get_nmi_count(cpu));
Andi Kleen09198e62007-05-02 19:27:20 +0200159 per_cpu(wd_enabled, cpu) = 0;
Don Zickusb7471c62006-09-26 10:52:26 +0200160 atomic_dec(&nmi_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 }
162 }
Daniel Walkerd9586542007-09-06 16:59:54 +0200163 endflag = 1;
Don Zickusb7471c62006-09-26 10:52:26 +0200164 if (!atomic_read(&nmi_active)) {
165 kfree(prev_nmi_count);
166 atomic_set(&nmi_active, -1);
Ingo Molnar6e908942008-03-21 14:32:36 +0100167 goto error;
Don Zickusb7471c62006-09-26 10:52:26 +0200168 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 printk("OK.\n");
170
Cyrill Gorcunov1798bc22008-05-24 19:36:41 +0400171 /*
172 * now that we know it works we can reduce NMI frequency to
173 * something more reasonable; makes a difference in some configs
174 */
Andi Kleen09198e62007-05-02 19:27:20 +0200175 if (nmi_watchdog == NMI_LOCAL_APIC)
176 nmi_hz = lapic_adjust_nmi_hz(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Eric W. Biederman29b70082005-10-30 14:59:40 -0800178 kfree(prev_nmi_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 return 0;
Ingo Molnar6e908942008-03-21 14:32:36 +0100180
Cyrill Gorcunov1798bc22008-05-24 19:36:41 +0400181error:
182#ifdef CONFIG_X86_32
183 timer_ack = !cpu_has_tsc;
184#endif
Ingo Molnar6e908942008-03-21 14:32:36 +0100185 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186}
187
188static int __init setup_nmi_watchdog(char *str)
189{
190 int nmi;
191
Cyrill Gorcunovd1b946b2008-05-24 19:36:34 +0400192 if (!strncmp(str, "panic", 5)) {
193 panic_on_timeout = 1;
194 str = strchr(str, ',');
195 if (!str)
196 return 1;
197 ++str;
198 }
199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 get_option(&str, &nmi);
201
Cyrill Gorcunov1798bc22008-05-24 19:36:41 +0400202 if (nmi >= NMI_INVALID || nmi < NMI_NONE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 return 0;
Venkatesh Pallipadi58d9ce7d2007-01-22 20:40:34 -0800204
Don Zickusb7471c62006-09-26 10:52:26 +0200205 nmi_watchdog = nmi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 return 1;
207}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208__setup("nmi_watchdog=", setup_nmi_watchdog);
209
Cyrill Gorcunov1798bc22008-05-24 19:36:41 +0400210/*
211 * Suspend/resume support
212 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213#ifdef CONFIG_PM
214
215static int nmi_pm_active; /* nmi_active before suspend */
216
Pavel Machek438510f2005-04-16 15:25:24 -0700217static int lapic_nmi_suspend(struct sys_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
Shaohua Li4038f902006-09-26 10:52:27 +0200219 /* only CPU0 goes here, other CPUs should be offline */
Don Zickusb7471c62006-09-26 10:52:26 +0200220 nmi_pm_active = atomic_read(&nmi_active);
Shaohua Li4038f902006-09-26 10:52:27 +0200221 stop_apic_nmi_watchdog(NULL);
222 BUG_ON(atomic_read(&nmi_active) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 return 0;
224}
225
226static int lapic_nmi_resume(struct sys_device *dev)
227{
Shaohua Li4038f902006-09-26 10:52:27 +0200228 /* only CPU0 goes here, other CPUs should be offline */
229 if (nmi_pm_active > 0) {
230 setup_apic_nmi_watchdog(NULL);
231 touch_nmi_watchdog();
232 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 return 0;
234}
235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236static struct sysdev_class nmi_sysclass = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +0100237 .name = "lapic_nmi",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 .resume = lapic_nmi_resume,
239 .suspend = lapic_nmi_suspend,
240};
241
242static struct sys_device device_lapic_nmi = {
243 .id = 0,
244 .cls = &nmi_sysclass,
245};
246
247static int __init init_lapic_nmi_sysfs(void)
248{
249 int error;
250
Cyrill Gorcunov1798bc22008-05-24 19:36:41 +0400251 /*
252 * should really be a BUG_ON but b/c this is an
Don Zickusb7471c62006-09-26 10:52:26 +0200253 * init call, it just doesn't work. -dcz
254 */
255 if (nmi_watchdog != NMI_LOCAL_APIC)
256 return 0;
257
Andi Kleen09198e62007-05-02 19:27:20 +0200258 if (atomic_read(&nmi_active) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 return 0;
260
261 error = sysdev_class_register(&nmi_sysclass);
262 if (!error)
263 error = sysdev_register(&device_lapic_nmi);
264 return error;
265}
Cyrill Gorcunov1798bc22008-05-24 19:36:41 +0400266
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267/* must come after the local APIC's device_initcall() */
268late_initcall(init_lapic_nmi_sysfs);
269
270#endif /* CONFIG_PM */
271
Andi Kleen09198e62007-05-02 19:27:20 +0200272static void __acpi_nmi_enable(void *__unused)
273{
274 apic_write_around(APIC_LVT0, APIC_DM_NMI);
275}
276
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277/*
Andi Kleen09198e62007-05-02 19:27:20 +0200278 * Enable timer based NMIs on all CPUs:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 */
Andi Kleen09198e62007-05-02 19:27:20 +0200280void acpi_nmi_enable(void)
Jan Beulich7fbb4f62005-06-23 00:08:23 -0700281{
Andi Kleen09198e62007-05-02 19:27:20 +0200282 if (atomic_read(&nmi_active) && nmi_watchdog == NMI_IO_APIC)
283 on_each_cpu(__acpi_nmi_enable, NULL, 0, 1);
Jan Beulich7fbb4f62005-06-23 00:08:23 -0700284}
285
Andi Kleen09198e62007-05-02 19:27:20 +0200286static void __acpi_nmi_disable(void *__unused)
Venkatesh Pallipadi90ce4bc2007-02-13 13:26:22 +0100287{
Cyrill Gorcunovad63ba12008-05-24 19:36:36 +0400288 apic_write_around(APIC_LVT0, APIC_DM_NMI | APIC_LVT_MASKED);
Venkatesh Pallipadi90ce4bc2007-02-13 13:26:22 +0100289}
290
Andi Kleen09198e62007-05-02 19:27:20 +0200291/*
292 * Disable timer based NMIs on all CPUs:
293 */
294void acpi_nmi_disable(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295{
Andi Kleen09198e62007-05-02 19:27:20 +0200296 if (atomic_read(&nmi_active) && nmi_watchdog == NMI_IO_APIC)
297 on_each_cpu(__acpi_nmi_disable, NULL, 0, 1);
Venkatesh Pallipadi248dcb22006-09-26 10:52:27 +0200298}
299
Hiroshi Shimamoto416b7212008-01-30 13:30:33 +0100300void setup_apic_nmi_watchdog(void *unused)
Don Zickusb7471c62006-09-26 10:52:26 +0200301{
Andi Kleen09198e62007-05-02 19:27:20 +0200302 if (__get_cpu_var(wd_enabled))
Hiroshi Shimamoto416b7212008-01-30 13:30:33 +0100303 return;
Shaohua Li4038f902006-09-26 10:52:27 +0200304
305 /* cheap hack to support suspend/resume */
306 /* if cpu0 is not active neither should the other cpus */
Cyrill Gorcunov1798bc22008-05-24 19:36:41 +0400307 if (smp_processor_id() != 0 && atomic_read(&nmi_active) <= 0)
Shaohua Li4038f902006-09-26 10:52:27 +0200308 return;
309
Andi Kleen09198e62007-05-02 19:27:20 +0200310 switch (nmi_watchdog) {
311 case NMI_LOCAL_APIC:
Cyrill Gorcunov1798bc22008-05-24 19:36:41 +0400312 /* enable it before to avoid race with handler */
313 __get_cpu_var(wd_enabled) = 1;
Andi Kleen09198e62007-05-02 19:27:20 +0200314 if (lapic_watchdog_init(nmi_hz) < 0) {
315 __get_cpu_var(wd_enabled) = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 return;
317 }
Andi Kleen09198e62007-05-02 19:27:20 +0200318 /* FALL THROUGH */
319 case NMI_IO_APIC:
320 __get_cpu_var(wd_enabled) = 1;
321 atomic_inc(&nmi_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 }
Don Zickusb7471c62006-09-26 10:52:26 +0200323}
324
Shaohua Li4038f902006-09-26 10:52:27 +0200325void stop_apic_nmi_watchdog(void *unused)
Don Zickusb7471c62006-09-26 10:52:26 +0200326{
327 /* only support LOCAL and IO APICs for now */
Cyrill Gorcunov1798bc22008-05-24 19:36:41 +0400328 if (nmi_watchdog != NMI_LOCAL_APIC &&
329 nmi_watchdog != NMI_IO_APIC)
330 return;
Andi Kleen09198e62007-05-02 19:27:20 +0200331 if (__get_cpu_var(wd_enabled) == 0)
Shaohua Li4038f902006-09-26 10:52:27 +0200332 return;
Andi Kleen09198e62007-05-02 19:27:20 +0200333 if (nmi_watchdog == NMI_LOCAL_APIC)
334 lapic_watchdog_stop();
335 __get_cpu_var(wd_enabled) = 0;
Don Zickusb7471c62006-09-26 10:52:26 +0200336 atomic_dec(&nmi_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337}
338
339/*
340 * the best way to detect whether a CPU has a 'hard lockup' problem
341 * is to check it's local APIC timer IRQ counts. If they are not
342 * changing then that CPU has some problem.
343 *
344 * as these watchdog NMI IRQs are generated on every CPU, we only
345 * have to check the current processor.
346 *
347 * since NMIs don't listen to _any_ locks, we have to be extremely
348 * careful not to rely on unsafe variables. The printk might lock
349 * up though, so we have to break up any console locks first ...
Cyrill Gorcunov1798bc22008-05-24 19:36:41 +0400350 * [when there will be more tty-related locks, break them up here too!]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 */
352
Hiroshi Shimamotof7849462008-05-02 16:45:08 -0700353static DEFINE_PER_CPU(unsigned, last_irq_sum);
354static DEFINE_PER_CPU(local_t, alert_counter);
355static DEFINE_PER_CPU(int, nmi_touch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
Andrew Mortonf2890252007-07-17 04:03:57 -0700357void touch_nmi_watchdog(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358{
Jan Beulichc6ea3962006-12-07 02:14:09 +0100359 if (nmi_watchdog > 0) {
360 unsigned cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
Jan Beulichc6ea3962006-12-07 02:14:09 +0100362 /*
Hiroshi Shimamotof7849462008-05-02 16:45:08 -0700363 * Tell other CPUs to reset their alert counters. We cannot
364 * do it ourselves because the alert count increase is not
365 * atomic.
Jan Beulichc6ea3962006-12-07 02:14:09 +0100366 */
Andrew Mortonf2890252007-07-17 04:03:57 -0700367 for_each_present_cpu(cpu) {
Hiroshi Shimamotof7849462008-05-02 16:45:08 -0700368 if (per_cpu(nmi_touch, cpu) != 1)
369 per_cpu(nmi_touch, cpu) = 1;
Andrew Mortonf2890252007-07-17 04:03:57 -0700370 }
Jan Beulichc6ea3962006-12-07 02:14:09 +0100371 }
Ingo Molnar8446f1d2005-09-06 15:16:27 -0700372
373 /*
374 * Tickle the softlockup detector too:
375 */
376 touch_softlockup_watchdog();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377}
Michal Schmidt1e862402006-07-30 03:03:29 -0700378EXPORT_SYMBOL(touch_nmi_watchdog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Steven Rostedt5deb45e2008-04-19 19:19:55 +0200380notrace __kprobes int
381nmi_watchdog_tick(struct pt_regs *regs, unsigned reason)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 /*
384 * Since current_thread_info()-> is always on the stack, and we
385 * always switch the stack NMI-atomically, it's safe to use
386 * smp_processor_id().
387 */
Jesper Juhlb791cce2006-03-28 01:56:52 -0800388 unsigned int sum;
Don Zickusb7471c62006-09-26 10:52:26 +0200389 int touched = 0;
Jesper Juhlb791cce2006-03-28 01:56:52 -0800390 int cpu = smp_processor_id();
Hiroshi Shimamoto416b7212008-01-30 13:30:33 +0100391 int rc = 0;
Don Zickusb7471c62006-09-26 10:52:26 +0200392
393 /* check for other users first */
394 if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT)
395 == NOTIFY_STOP) {
Don Zickus3adbbcce2006-09-26 10:52:26 +0200396 rc = 1;
Don Zickusb7471c62006-09-26 10:52:26 +0200397 touched = 1;
398 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Cyrill Gorcunovfd5cea02008-05-24 19:36:40 +0400400 sum = get_timer_irqs(cpu);
401
402 if (__get_cpu_var(nmi_touch)) {
403 __get_cpu_var(nmi_touch) = 0;
404 touched = 1;
405 }
406
Andrew Mortonbb81a092006-12-07 02:14:01 +0100407 if (cpu_isset(cpu, backtrace_mask)) {
408 static DEFINE_SPINLOCK(lock); /* Serialise the printks */
409
410 spin_lock(&lock);
Hiroshi Shimamoto88ff0a42008-05-27 18:49:39 -0700411 printk(KERN_WARNING "NMI backtrace for cpu %d\n", cpu);
Andrew Mortonbb81a092006-12-07 02:14:01 +0100412 dump_stack();
413 spin_unlock(&lock);
414 cpu_clear(cpu, backtrace_mask);
415 }
416
Cyrill Gorcunovfd5cea02008-05-24 19:36:40 +0400417 /* Could check oops_in_progress here too, but it's safer not to */
418 if (mce_in_progress())
Hiroshi Shimamotof7849462008-05-02 16:45:08 -0700419 touched = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
Thomas Gleixnerf8b50352007-02-16 01:28:09 -0800421 /* if the none of the timers isn't firing, this cpu isn't doing much */
Hiroshi Shimamotof7849462008-05-02 16:45:08 -0700422 if (!touched && __get_cpu_var(last_irq_sum) == sum) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 /*
424 * Ayiee, looks like this CPU is stuck ...
425 * wait a few IRQs (5 seconds) before doing the oops ...
426 */
Hiroshi Shimamotof7849462008-05-02 16:45:08 -0700427 local_inc(&__get_cpu_var(alert_counter));
Cyrill Gorcunov1798bc22008-05-24 19:36:41 +0400428 if (local_read(&__get_cpu_var(alert_counter)) == 5 * nmi_hz)
George Anzinger748f2ed2005-09-03 15:56:48 -0700429 /*
430 * die_nmi will return ONLY if NOTIFY_STOP happens..
431 */
Cyrill Gorcunovddca03c2008-05-24 19:36:31 +0400432 die_nmi("BUG: NMI Watchdog detected LOCKUP",
Cyrill Gorcunovd1b946b2008-05-24 19:36:34 +0400433 regs, panic_on_timeout);
GOTO Masanorib884e252006-03-07 21:55:29 -0800434 } else {
Hiroshi Shimamotof7849462008-05-02 16:45:08 -0700435 __get_cpu_var(last_irq_sum) = sum;
436 local_set(&__get_cpu_var(alert_counter), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 }
Cyrill Gorcunov1798bc22008-05-24 19:36:41 +0400438
Don Zickusb7471c62006-09-26 10:52:26 +0200439 /* see if the nmi watchdog went off */
Andi Kleen09198e62007-05-02 19:27:20 +0200440 if (!__get_cpu_var(wd_enabled))
441 return rc;
442 switch (nmi_watchdog) {
443 case NMI_LOCAL_APIC:
444 rc |= lapic_wd_event(nmi_hz);
445 break;
446 case NMI_IO_APIC:
Cyrill Gorcunov1798bc22008-05-24 19:36:41 +0400447 /*
448 * don't know how to accurately check for this.
Andi Kleen09198e62007-05-02 19:27:20 +0200449 * just assume it was a watchdog timer interrupt
450 * This matches the old behaviour.
451 */
452 rc = 1;
453 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 }
Don Zickus3adbbcce2006-09-26 10:52:26 +0200455 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456}
457
458#ifdef CONFIG_SYSCTL
459
460static int unknown_nmi_panic_callback(struct pt_regs *regs, int cpu)
461{
462 unsigned char reason = get_nmi_reason();
463 char buf[64];
464
Don Zickus2fbe7b22006-09-26 10:52:27 +0200465 sprintf(buf, "NMI received for unknown reason %02x\n", reason);
Cyrill Gorcunov6c8decd2008-05-24 19:36:37 +0400466 die_nmi(buf, regs, 1); /* Always panic here */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 return 0;
468}
469
Don Zickus407984f2006-09-26 10:52:27 +0200470/*
Don Zickuse33e89a2006-09-26 10:52:27 +0200471 * proc handler for /proc/sys/kernel/nmi
Don Zickus407984f2006-09-26 10:52:27 +0200472 */
473int proc_nmi_enabled(struct ctl_table *table, int write, struct file *file,
474 void __user *buffer, size_t *length, loff_t *ppos)
475{
476 int old_state;
477
478 nmi_watchdog_enabled = (atomic_read(&nmi_active) > 0) ? 1 : 0;
479 old_state = nmi_watchdog_enabled;
480 proc_dointvec(table, write, file, buffer, length, ppos);
481 if (!!old_state == !!nmi_watchdog_enabled)
482 return 0;
483
Daniel Gollub0328ece2007-08-15 02:40:35 +0200484 if (atomic_read(&nmi_active) < 0 || nmi_watchdog == NMI_DISABLED) {
Cyrill Gorcunov1798bc22008-05-24 19:36:41 +0400485 printk(KERN_WARNING
486 "NMI watchdog is permanently disabled\n");
Don Zickuse33e89a2006-09-26 10:52:27 +0200487 return -EIO;
Don Zickus407984f2006-09-26 10:52:27 +0200488 }
489
Cyrill Gorcunov19ec6732008-05-28 23:00:47 +0400490#ifdef CONFIG_X86_64
Cyrill Gorcunov4b82b272008-05-24 19:36:35 +0400491 /* if nmi_watchdog is not set yet, then set it */
492 nmi_watchdog_default();
Cyrill Gorcunov19ec6732008-05-28 23:00:47 +0400493#else
494 if (lapic_watchdog_ok())
495 nmi_watchdog = NMI_LOCAL_APIC;
496 else
497 nmi_watchdog = NMI_IO_APIC;
498#endif
Don Zickus407984f2006-09-26 10:52:27 +0200499
Don Zickuse33e89a2006-09-26 10:52:27 +0200500 if (nmi_watchdog == NMI_LOCAL_APIC) {
Don Zickus407984f2006-09-26 10:52:27 +0200501 if (nmi_watchdog_enabled)
502 enable_lapic_nmi_watchdog();
503 else
504 disable_lapic_nmi_watchdog();
Don Zickus407984f2006-09-26 10:52:27 +0200505 } else {
Cyrill Gorcunov1798bc22008-05-24 19:36:41 +0400506 printk(KERN_WARNING
Don Zickus407984f2006-09-26 10:52:27 +0200507 "NMI watchdog doesn't know what hardware to touch\n");
508 return -EIO;
509 }
510 return 0;
511}
512
Cyrill Gorcunov1798bc22008-05-24 19:36:41 +0400513#endif /* CONFIG_SYSCTL */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
Li Zefana062bae2008-02-03 15:40:30 +0800515int do_nmi_callback(struct pt_regs *regs, int cpu)
516{
517#ifdef CONFIG_SYSCTL
518 if (unknown_nmi_panic)
519 return unknown_nmi_panic_callback(regs, cpu);
520#endif
521 return 0;
522}
523
Andrew Mortonbb81a092006-12-07 02:14:01 +0100524void __trigger_all_cpu_backtrace(void)
525{
526 int i;
527
528 backtrace_mask = cpu_online_map;
529 /* Wait for up to 10 seconds for all CPUs to do the backtrace */
530 for (i = 0; i < 10 * 1000; i++) {
531 if (cpus_empty(backtrace_mask))
532 break;
533 mdelay(1);
534 }
535}