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