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