blob: ad573d8baf10c4820d5b8c4a32b3a18ae0c835ce [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Machine check handler.
Ingo Molnare9eee032009-04-08 12:31:17 +02003 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * K8 parts Copyright 2002,2003 Andi Kleen, SuSE Labs.
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02005 * Rest from unknown author(s).
6 * 2004 Andi Kleen. Rewrote most of it.
Andi Kleenb79109c2009-02-12 13:43:23 +01007 * Copyright 2008 Intel Corporation
8 * Author: Andi Kleen
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
Tim Hockine02e68d2007-07-21 17:10:36 +020010#include <linux/thread_info.h>
Ingo Molnare9eee032009-04-08 12:31:17 +020011#include <linux/capability.h>
12#include <linux/miscdevice.h>
Andi Kleen8457c842009-02-12 13:49:33 +010013#include <linux/ratelimit.h>
Ingo Molnare9eee032009-04-08 12:31:17 +020014#include <linux/kallsyms.h>
15#include <linux/rcupdate.h>
Ingo Molnare9eee032009-04-08 12:31:17 +020016#include <linux/kobject.h>
Hidetoshi Seto14a02532009-04-30 16:04:51 +090017#include <linux/uaccess.h>
Ingo Molnare9eee032009-04-08 12:31:17 +020018#include <linux/kdebug.h>
19#include <linux/kernel.h>
20#include <linux/percpu.h>
21#include <linux/string.h>
Kay Sievers8a25a2f2011-12-21 14:29:42 -080022#include <linux/device.h>
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +010023#include <linux/syscore_ops.h>
Andi Kleen3c079792009-05-27 21:56:55 +020024#include <linux/delay.h>
Ingo Molnare9eee032009-04-08 12:31:17 +020025#include <linux/ctype.h>
26#include <linux/sched.h>
27#include <linux/sysfs.h>
28#include <linux/types.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Ingo Molnare9eee032009-04-08 12:31:17 +020030#include <linux/init.h>
31#include <linux/kmod.h>
32#include <linux/poll.h>
Andi Kleen3c079792009-05-27 21:56:55 +020033#include <linux/nmi.h>
Ingo Molnare9eee032009-04-08 12:31:17 +020034#include <linux/cpu.h>
Hidetoshi Seto14a02532009-04-30 16:04:51 +090035#include <linux/smp.h>
Ingo Molnare9eee032009-04-08 12:31:17 +020036#include <linux/fs.h>
Andi Kleen9b1beaf2009-05-27 21:56:59 +020037#include <linux/mm.h>
Huang Ying5be9ed22009-07-31 09:41:42 +080038#include <linux/debugfs.h>
Hidetoshi Setob77e70b2011-06-08 10:56:02 +090039#include <linux/irq_work.h>
Paul Gortmaker69c60c82011-05-26 12:22:53 -040040#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Ingo Molnare9eee032009-04-08 12:31:17 +020042#include <asm/processor.h>
Ingo Molnare9eee032009-04-08 12:31:17 +020043#include <asm/mce.h>
44#include <asm/msr.h>
Ingo Molnare9eee032009-04-08 12:31:17 +020045
Andi Kleenbd19a5e2009-05-27 21:56:55 +020046#include "mce-internal.h"
Ingo Molnar711c2e42009-04-08 12:31:26 +020047
Hidetoshi Seto93b62c32011-06-08 11:00:45 +090048static DEFINE_MUTEX(mce_chrdev_read_mutex);
Ingo Molnar2aa2b50dd2010-03-14 08:57:03 +010049
Paul E. McKenneyf56e8a02010-03-05 15:03:27 -080050#define rcu_dereference_check_mce(p) \
Paul E. McKenneyec8c27e2010-04-30 06:45:36 -070051 rcu_dereference_index_check((p), \
Paul E. McKenneyf56e8a02010-03-05 15:03:27 -080052 rcu_read_lock_sched_held() || \
Hidetoshi Seto93b62c32011-06-08 11:00:45 +090053 lockdep_is_held(&mce_chrdev_read_mutex))
Paul E. McKenneyf56e8a02010-03-05 15:03:27 -080054
Hidetoshi Seto8968f9d2009-10-13 16:19:41 +090055#define CREATE_TRACE_POINTS
56#include <trace/events/mce.h>
57
Hidetoshi Seto4e5b3e62009-06-15 17:20:20 +090058int mce_disabled __read_mostly;
Andi Kleen04b2b1a2009-04-28 22:50:19 +020059
Ingo Molnare9eee032009-04-08 12:31:17 +020060#define MISC_MCELOG_MINOR 227
Andi Kleen0d7482e32009-02-17 23:07:13 +010061
Andi Kleen3c079792009-05-27 21:56:55 +020062#define SPINUNIT 100 /* 100ns */
63
Andi Kleen553f2652006-04-07 19:49:57 +020064atomic_t mce_entry;
65
Andi Kleen01ca79f2009-05-27 21:56:52 +020066DEFINE_PER_CPU(unsigned, mce_exception_count);
67
Tim Hockinbd784322007-07-21 17:10:37 +020068/*
69 * Tolerant levels:
70 * 0: always panic on uncorrected errors, log corrected errors
71 * 1: panic or SIGBUS on uncorrected errors, log corrected errors
72 * 2: SIGBUS or log uncorrected errors (if possible), log corrected errors
73 * 3: never panic or SIGBUS, log all errors (for testing only)
74 */
Hidetoshi Seto4e5b3e62009-06-15 17:20:20 +090075static int tolerant __read_mostly = 1;
76static int banks __read_mostly;
Hidetoshi Seto4e5b3e62009-06-15 17:20:20 +090077static int rip_msr __read_mostly;
78static int mce_bootlog __read_mostly = -1;
79static int monarch_timeout __read_mostly = -1;
80static int mce_panic_timeout __read_mostly;
81static int mce_dont_log_ce __read_mostly;
82int mce_cmci_disabled __read_mostly;
83int mce_ignore_ce __read_mostly;
84int mce_ser __read_mostly;
Andi Kleena98f0dd2007-02-13 13:26:23 +010085
Andi Kleencebe1822009-07-09 00:31:43 +020086struct mce_bank *mce_banks __read_mostly;
87
Hidetoshi Seto1020bcb2009-06-15 17:20:57 +090088/* User mode helper program triggered by machine check event */
89static unsigned long mce_need_notify;
90static char mce_helper[128];
91static char *mce_helper_argv[2] = { mce_helper, NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Hidetoshi Seto93b62c32011-06-08 11:00:45 +090093static DECLARE_WAIT_QUEUE_HEAD(mce_chrdev_wait);
94
Andi Kleen3c079792009-05-27 21:56:55 +020095static DEFINE_PER_CPU(struct mce, mces_seen);
96static int cpu_missing;
97
Andi Kleenee031c32009-02-12 13:49:34 +010098/* MCA banks polled by the period polling timer for corrected events */
99DEFINE_PER_CPU(mce_banks_t, mce_poll_banks) = {
100 [0 ... BITS_TO_LONGS(MAX_NR_BANKS)-1] = ~0UL
101};
102
Andi Kleen9b1beaf2009-05-27 21:56:59 +0200103static DEFINE_PER_CPU(struct work_struct, mce_work);
104
Borislav Petkov3653ada2011-12-04 15:12:09 +0100105/*
106 * CPU/chipset specific EDAC code can register a notifier call here to print
107 * MCE errors in a human-readable form.
108 */
109ATOMIC_NOTIFIER_HEAD(x86_mce_decoder_chain);
110
Andi Kleenb5f2fa42009-02-12 13:43:22 +0100111/* Do initial initialization of a struct mce */
112void mce_setup(struct mce *m)
113{
114 memset(m, 0, sizeof(struct mce));
Andi Kleend620c672009-05-27 21:56:56 +0200115 m->cpu = m->extcpu = smp_processor_id();
Andi Kleenb5f2fa42009-02-12 13:43:22 +0100116 rdtscll(m->tsc);
Andi Kleen8ee08342009-05-27 21:56:56 +0200117 /* We hope get_seconds stays lockless */
118 m->time = get_seconds();
119 m->cpuvendor = boot_cpu_data.x86_vendor;
120 m->cpuid = cpuid_eax(1);
Andi Kleen8ee08342009-05-27 21:56:56 +0200121 m->socketid = cpu_data(m->extcpu).phys_proc_id;
Andi Kleen8ee08342009-05-27 21:56:56 +0200122 m->apicid = cpu_data(m->extcpu).initial_apicid;
123 rdmsrl(MSR_IA32_MCG_CAP, m->mcgcap);
Andi Kleenb5f2fa42009-02-12 13:43:22 +0100124}
125
Andi Kleenea149b32009-04-29 19:31:00 +0200126DEFINE_PER_CPU(struct mce, injectm);
127EXPORT_PER_CPU_SYMBOL_GPL(injectm);
128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129/*
130 * Lockless MCE logging infrastructure.
131 * This avoids deadlocks on printk locks without having to break locks. Also
132 * separate MCEs from kernel messages to avoid bogus bug reports.
133 */
134
Adrian Bunk231fd902008-01-30 13:30:30 +0100135static struct mce_log mcelog = {
Andi Kleenf6fb0ac2009-05-27 21:56:55 +0200136 .signature = MCE_LOG_SIGNATURE,
137 .len = MCE_LOG_LEN,
138 .recordlen = sizeof(struct mce),
Thomas Gleixnerd88203d2007-10-23 22:37:23 +0200139};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
141void mce_log(struct mce *mce)
142{
143 unsigned next, entry;
Borislav Petkovf0cb5452011-07-18 11:24:45 -0300144 int ret = 0;
Ingo Molnare9eee032009-04-08 12:31:17 +0200145
Hidetoshi Seto8968f9d2009-10-13 16:19:41 +0900146 /* Emit the trace record: */
147 trace_mce_record(mce);
148
Borislav Petkovf0cb5452011-07-18 11:24:45 -0300149 ret = atomic_notifier_call_chain(&x86_mce_decoder_chain, 0, mce);
150 if (ret == NOTIFY_STOP)
151 return;
152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 mce->finished = 0;
Mike Waychison76441432005-09-30 00:01:27 +0200154 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 for (;;) {
Paul E. McKenneyf56e8a02010-03-05 15:03:27 -0800156 entry = rcu_dereference_check_mce(mcelog.next);
Andi Kleen673242c2005-09-12 18:49:24 +0200157 for (;;) {
Mauro Carvalho Chehab696e4092009-07-23 06:57:45 -0300158
159 /*
Ingo Molnare9eee032009-04-08 12:31:17 +0200160 * When the buffer fills up discard new entries.
161 * Assume that the earlier errors are the more
162 * interesting ones:
163 */
Andi Kleen673242c2005-09-12 18:49:24 +0200164 if (entry >= MCE_LOG_LEN) {
Hidetoshi Seto14a02532009-04-30 16:04:51 +0900165 set_bit(MCE_OVERFLOW,
166 (unsigned long *)&mcelog.flags);
Andi Kleen673242c2005-09-12 18:49:24 +0200167 return;
168 }
Ingo Molnare9eee032009-04-08 12:31:17 +0200169 /* Old left over entry. Skip: */
Andi Kleen673242c2005-09-12 18:49:24 +0200170 if (mcelog.entry[entry].finished) {
171 entry++;
172 continue;
173 }
Mike Waychison76441432005-09-30 00:01:27 +0200174 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 smp_rmb();
177 next = entry + 1;
178 if (cmpxchg(&mcelog.next, entry, next) == entry)
179 break;
180 }
181 memcpy(mcelog.entry + entry, mce, sizeof(struct mce));
Mike Waychison76441432005-09-30 00:01:27 +0200182 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 mcelog.entry[entry].finished = 1;
Mike Waychison76441432005-09-30 00:01:27 +0200184 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
Andi Kleena0189c72009-05-27 21:56:54 +0200186 mce->finished = 1;
Hidetoshi Seto1020bcb2009-06-15 17:20:57 +0900187 set_bit(0, &mce_need_notify);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188}
189
Borislav Petkov09371952011-12-08 12:28:33 +0100190static void drain_mcelog_buffer(void)
191{
192 unsigned int next, i, prev = 0;
193
194 next = rcu_dereference_check_mce(mcelog.next);
195
196 do {
197 struct mce *m;
198
199 /* drain what was logged during boot */
200 for (i = prev; i < next; i++) {
201 unsigned long start = jiffies;
202 unsigned retries = 1;
203
204 m = &mcelog.entry[i];
205
206 while (!m->finished) {
207 if (time_after_eq(jiffies, start + 2*retries))
208 retries++;
209
210 cpu_relax();
211
212 if (!m->finished && retries >= 4) {
213 pr_err("MCE: skipping error being logged currently!\n");
214 break;
215 }
216 }
217 smp_rmb();
218 atomic_notifier_call_chain(&x86_mce_decoder_chain, 0, m);
219 }
220
221 memset(mcelog.entry + prev, 0, (next - prev) * sizeof(*m));
222 prev = next;
223 next = cmpxchg(&mcelog.next, prev, 0);
224 } while (next != prev);
225}
226
227
Borislav Petkov3653ada2011-12-04 15:12:09 +0100228void mce_register_decode_chain(struct notifier_block *nb)
229{
230 atomic_notifier_chain_register(&x86_mce_decoder_chain, nb);
Borislav Petkov09371952011-12-08 12:28:33 +0100231 drain_mcelog_buffer();
Borislav Petkov3653ada2011-12-04 15:12:09 +0100232}
233EXPORT_SYMBOL_GPL(mce_register_decode_chain);
234
235void mce_unregister_decode_chain(struct notifier_block *nb)
236{
237 atomic_notifier_chain_unregister(&x86_mce_decoder_chain, nb);
238}
239EXPORT_SYMBOL_GPL(mce_unregister_decode_chain);
240
Hidetoshi Seto77e26cc2009-06-11 16:04:35 +0900241static void print_mce(struct mce *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242{
Borislav Petkovdffa4b22011-04-20 12:23:49 +0200243 int ret = 0;
244
Huang Yinga2d7b0d2010-06-08 14:35:39 +0800245 pr_emerg(HW_ERR "CPU %d: Machine Check Exception: %Lx Bank %d: %016Lx\n",
Andi Kleend620c672009-05-27 21:56:56 +0200246 m->extcpu, m->mcgstatus, m->bank, m->status);
Ingo Molnarf436f8b2009-10-01 16:14:32 +0200247
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100248 if (m->ip) {
Huang Yinga2d7b0d2010-06-08 14:35:39 +0800249 pr_emerg(HW_ERR "RIP%s %02x:<%016Lx> ",
Ingo Molnarf436f8b2009-10-01 16:14:32 +0200250 !(m->mcgstatus & MCG_STATUS_EIPV) ? " !INEXACT!" : "",
251 m->cs, m->ip);
252
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 if (m->cs == __KERNEL_CS)
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100254 print_symbol("{%s}", m->ip);
Ingo Molnarf436f8b2009-10-01 16:14:32 +0200255 pr_cont("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 }
Borislav Petkov549d0422009-07-24 13:51:42 +0200257
Huang Yinga2d7b0d2010-06-08 14:35:39 +0800258 pr_emerg(HW_ERR "TSC %llx ", m->tsc);
Ingo Molnarf436f8b2009-10-01 16:14:32 +0200259 if (m->addr)
260 pr_cont("ADDR %llx ", m->addr);
261 if (m->misc)
262 pr_cont("MISC %llx ", m->misc);
263
264 pr_cont("\n");
Andi Kleen506ed6b2011-10-12 17:46:33 -0700265 /*
266 * Note this output is parsed by external tools and old fields
267 * should not be changed.
268 */
Borislav Petkov881e23e2011-10-17 16:45:10 +0200269 pr_emerg(HW_ERR "PROCESSOR %u:%x TIME %llu SOCKET %u APIC %x microcode %x\n",
Andi Kleen506ed6b2011-10-12 17:46:33 -0700270 m->cpuvendor, m->cpuid, m->time, m->socketid, m->apicid,
271 cpu_data(m->extcpu).microcode);
Ingo Molnarf436f8b2009-10-01 16:14:32 +0200272
273 /*
274 * Print out human-readable details about the MCE error,
Borislav Petkovfb253192009-10-07 13:20:38 +0200275 * (if the CPU has an implementation for that)
Ingo Molnarf436f8b2009-10-01 16:14:32 +0200276 */
Borislav Petkovdffa4b22011-04-20 12:23:49 +0200277 ret = atomic_notifier_call_chain(&x86_mce_decoder_chain, 0, m);
278 if (ret == NOTIFY_STOP)
279 return;
280
281 pr_emerg_ratelimited(HW_ERR "Run the above through 'mcelog --ascii'\n");
Andi Kleen86503562009-05-27 21:56:58 +0200282}
283
Andi Kleenf94b61c2009-05-27 21:56:55 +0200284#define PANIC_TIMEOUT 5 /* 5 seconds */
285
286static atomic_t mce_paniced;
287
Huang Yingbf783f92009-07-31 09:41:43 +0800288static int fake_panic;
289static atomic_t mce_fake_paniced;
290
Andi Kleenf94b61c2009-05-27 21:56:55 +0200291/* Panic in progress. Enable interrupts and wait for final IPI */
292static void wait_for_panic(void)
293{
294 long timeout = PANIC_TIMEOUT*USEC_PER_SEC;
Ingo Molnarf436f8b2009-10-01 16:14:32 +0200295
Andi Kleenf94b61c2009-05-27 21:56:55 +0200296 preempt_disable();
297 local_irq_enable();
298 while (timeout-- > 0)
299 udelay(1);
Andi Kleen29b0f592009-05-27 21:56:56 +0200300 if (panic_timeout == 0)
301 panic_timeout = mce_panic_timeout;
Andi Kleenf94b61c2009-05-27 21:56:55 +0200302 panic("Panicing machine check CPU died");
303}
304
Andi Kleenbd19a5e2009-05-27 21:56:55 +0200305static void mce_panic(char *msg, struct mce *final, char *exp)
Thomas Gleixnerd88203d2007-10-23 22:37:23 +0200306{
Huang Ying482908b2010-05-18 14:35:22 +0800307 int i, apei_err = 0;
Tim Hockine02e68d2007-07-21 17:10:36 +0200308
Huang Yingbf783f92009-07-31 09:41:43 +0800309 if (!fake_panic) {
310 /*
311 * Make sure only one CPU runs in machine check panic
312 */
313 if (atomic_inc_return(&mce_paniced) > 1)
314 wait_for_panic();
315 barrier();
Andi Kleenf94b61c2009-05-27 21:56:55 +0200316
Huang Yingbf783f92009-07-31 09:41:43 +0800317 bust_spinlocks(1);
318 console_verbose();
319 } else {
320 /* Don't log too much for fake panic */
321 if (atomic_inc_return(&mce_fake_paniced) > 1)
322 return;
323 }
Andi Kleena0189c72009-05-27 21:56:54 +0200324 /* First print corrected ones that are still unlogged */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 for (i = 0; i < MCE_LOG_LEN; i++) {
Andi Kleena0189c72009-05-27 21:56:54 +0200326 struct mce *m = &mcelog.entry[i];
Hidetoshi Seto77e26cc2009-06-11 16:04:35 +0900327 if (!(m->status & MCI_STATUS_VAL))
328 continue;
Huang Ying482908b2010-05-18 14:35:22 +0800329 if (!(m->status & MCI_STATUS_UC)) {
Hidetoshi Seto77e26cc2009-06-11 16:04:35 +0900330 print_mce(m);
Huang Ying482908b2010-05-18 14:35:22 +0800331 if (!apei_err)
332 apei_err = apei_write_mce(m);
333 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 }
Andi Kleena0189c72009-05-27 21:56:54 +0200335 /* Now print uncorrected but with the final one last */
336 for (i = 0; i < MCE_LOG_LEN; i++) {
337 struct mce *m = &mcelog.entry[i];
338 if (!(m->status & MCI_STATUS_VAL))
339 continue;
Hidetoshi Seto77e26cc2009-06-11 16:04:35 +0900340 if (!(m->status & MCI_STATUS_UC))
341 continue;
Huang Ying482908b2010-05-18 14:35:22 +0800342 if (!final || memcmp(m, final, sizeof(struct mce))) {
Hidetoshi Seto77e26cc2009-06-11 16:04:35 +0900343 print_mce(m);
Huang Ying482908b2010-05-18 14:35:22 +0800344 if (!apei_err)
345 apei_err = apei_write_mce(m);
346 }
Andi Kleena0189c72009-05-27 21:56:54 +0200347 }
Huang Ying482908b2010-05-18 14:35:22 +0800348 if (final) {
Hidetoshi Seto77e26cc2009-06-11 16:04:35 +0900349 print_mce(final);
Huang Ying482908b2010-05-18 14:35:22 +0800350 if (!apei_err)
351 apei_err = apei_write_mce(final);
352 }
Andi Kleen3c079792009-05-27 21:56:55 +0200353 if (cpu_missing)
Huang Yinga2d7b0d2010-06-08 14:35:39 +0800354 pr_emerg(HW_ERR "Some CPUs didn't answer in synchronization\n");
Andi Kleenbd19a5e2009-05-27 21:56:55 +0200355 if (exp)
Huang Yinga2d7b0d2010-06-08 14:35:39 +0800356 pr_emerg(HW_ERR "Machine check: %s\n", exp);
Huang Yingbf783f92009-07-31 09:41:43 +0800357 if (!fake_panic) {
358 if (panic_timeout == 0)
359 panic_timeout = mce_panic_timeout;
360 panic(msg);
361 } else
Huang Yinga2d7b0d2010-06-08 14:35:39 +0800362 pr_emerg(HW_ERR "Fake kernel panic: %s\n", msg);
Thomas Gleixnerd88203d2007-10-23 22:37:23 +0200363}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
Andi Kleenea149b32009-04-29 19:31:00 +0200365/* Support code for software error injection */
366
367static int msr_to_offset(u32 msr)
368{
Tejun Heo0a3aee02010-12-18 16:28:55 +0100369 unsigned bank = __this_cpu_read(injectm.bank);
Ingo Molnarf436f8b2009-10-01 16:14:32 +0200370
Andi Kleenea149b32009-04-29 19:31:00 +0200371 if (msr == rip_msr)
372 return offsetof(struct mce, ip);
Andi Kleena2d32bc2009-07-09 00:31:44 +0200373 if (msr == MSR_IA32_MCx_STATUS(bank))
Andi Kleenea149b32009-04-29 19:31:00 +0200374 return offsetof(struct mce, status);
Andi Kleena2d32bc2009-07-09 00:31:44 +0200375 if (msr == MSR_IA32_MCx_ADDR(bank))
Andi Kleenea149b32009-04-29 19:31:00 +0200376 return offsetof(struct mce, addr);
Andi Kleena2d32bc2009-07-09 00:31:44 +0200377 if (msr == MSR_IA32_MCx_MISC(bank))
Andi Kleenea149b32009-04-29 19:31:00 +0200378 return offsetof(struct mce, misc);
379 if (msr == MSR_IA32_MCG_STATUS)
380 return offsetof(struct mce, mcgstatus);
381 return -1;
382}
383
Andi Kleen5f8c1a52009-04-29 19:29:12 +0200384/* MSR access wrappers used for error injection */
385static u64 mce_rdmsrl(u32 msr)
386{
387 u64 v;
Ingo Molnar11868a22009-09-23 17:49:55 +0200388
Tejun Heo0a3aee02010-12-18 16:28:55 +0100389 if (__this_cpu_read(injectm.finished)) {
Andi Kleenea149b32009-04-29 19:31:00 +0200390 int offset = msr_to_offset(msr);
Ingo Molnar11868a22009-09-23 17:49:55 +0200391
Andi Kleenea149b32009-04-29 19:31:00 +0200392 if (offset < 0)
393 return 0;
394 return *(u64 *)((char *)&__get_cpu_var(injectm) + offset);
395 }
Ingo Molnar11868a22009-09-23 17:49:55 +0200396
397 if (rdmsrl_safe(msr, &v)) {
398 WARN_ONCE(1, "mce: Unable to read msr %d!\n", msr);
399 /*
400 * Return zero in case the access faulted. This should
401 * not happen normally but can happen if the CPU does
402 * something weird, or if the code is buggy.
403 */
404 v = 0;
405 }
406
Andi Kleen5f8c1a52009-04-29 19:29:12 +0200407 return v;
408}
409
410static void mce_wrmsrl(u32 msr, u64 v)
411{
Tejun Heo0a3aee02010-12-18 16:28:55 +0100412 if (__this_cpu_read(injectm.finished)) {
Andi Kleenea149b32009-04-29 19:31:00 +0200413 int offset = msr_to_offset(msr);
Ingo Molnar11868a22009-09-23 17:49:55 +0200414
Andi Kleenea149b32009-04-29 19:31:00 +0200415 if (offset >= 0)
416 *(u64 *)((char *)&__get_cpu_var(injectm) + offset) = v;
417 return;
418 }
Andi Kleen5f8c1a52009-04-29 19:29:12 +0200419 wrmsrl(msr, v);
420}
421
Andi Kleen9b1beaf2009-05-27 21:56:59 +0200422/*
Hidetoshi Setob8325c52011-06-08 10:57:46 +0900423 * Collect all global (w.r.t. this processor) status about this machine
424 * check into our "mce" struct so that we can use it later to assess
425 * the severity of the problem as we read per-bank specific details.
426 */
427static inline void mce_gather_info(struct mce *m, struct pt_regs *regs)
428{
429 mce_setup(m);
430
431 m->mcgstatus = mce_rdmsrl(MSR_IA32_MCG_STATUS);
432 if (regs) {
433 /*
434 * Get the address of the instruction at the time of
435 * the machine check error.
436 */
437 if (m->mcgstatus & (MCG_STATUS_RIPV|MCG_STATUS_EIPV)) {
438 m->ip = regs->ip;
439 m->cs = regs->cs;
440 }
441 /* Use accurate RIP reporting if available. */
442 if (rip_msr)
443 m->ip = mce_rdmsrl(rip_msr);
444 }
445}
446
447/*
Andi Kleen9b1beaf2009-05-27 21:56:59 +0200448 * Simple lockless ring to communicate PFNs from the exception handler with the
449 * process context work function. This is vastly simplified because there's
450 * only a single reader and a single writer.
451 */
452#define MCE_RING_SIZE 16 /* we use one entry less */
453
454struct mce_ring {
455 unsigned short start;
456 unsigned short end;
457 unsigned long ring[MCE_RING_SIZE];
458};
459static DEFINE_PER_CPU(struct mce_ring, mce_ring);
460
461/* Runs with CPU affinity in workqueue */
462static int mce_ring_empty(void)
463{
464 struct mce_ring *r = &__get_cpu_var(mce_ring);
465
466 return r->start == r->end;
467}
468
469static int mce_ring_get(unsigned long *pfn)
470{
471 struct mce_ring *r;
472 int ret = 0;
473
474 *pfn = 0;
475 get_cpu();
476 r = &__get_cpu_var(mce_ring);
477 if (r->start == r->end)
478 goto out;
479 *pfn = r->ring[r->start];
480 r->start = (r->start + 1) % MCE_RING_SIZE;
481 ret = 1;
482out:
483 put_cpu();
484 return ret;
485}
486
487/* Always runs in MCE context with preempt off */
488static int mce_ring_add(unsigned long pfn)
489{
490 struct mce_ring *r = &__get_cpu_var(mce_ring);
491 unsigned next;
492
493 next = (r->end + 1) % MCE_RING_SIZE;
494 if (next == r->start)
495 return -1;
496 r->ring[r->end] = pfn;
497 wmb();
498 r->end = next;
499 return 0;
500}
501
Andi Kleen88ccbed2009-02-12 13:49:36 +0100502int mce_available(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503{
Andi Kleen04b2b1a2009-04-28 22:50:19 +0200504 if (mce_disabled)
Andi Kleen5b4408f2009-02-12 13:39:30 +0100505 return 0;
Akinobu Mita3d1712c2006-03-24 03:15:11 -0800506 return cpu_has(c, X86_FEATURE_MCE) && cpu_has(c, X86_FEATURE_MCA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507}
508
Andi Kleen9b1beaf2009-05-27 21:56:59 +0200509static void mce_schedule_work(void)
510{
511 if (!mce_ring_empty()) {
512 struct work_struct *work = &__get_cpu_var(mce_work);
513 if (!work_pending(work))
514 schedule_work(work);
515 }
516}
517
Hidetoshi Setob77e70b2011-06-08 10:56:02 +0900518DEFINE_PER_CPU(struct irq_work, mce_irq_work);
519
520static void mce_irq_work_cb(struct irq_work *entry)
Andi Kleenccc3c312009-05-27 21:56:54 +0200521{
Andi Kleen9ff36ee2009-05-27 21:56:58 +0200522 mce_notify_irq();
Andi Kleen9b1beaf2009-05-27 21:56:59 +0200523 mce_schedule_work();
Andi Kleenccc3c312009-05-27 21:56:54 +0200524}
Andi Kleenccc3c312009-05-27 21:56:54 +0200525
526static void mce_report_event(struct pt_regs *regs)
527{
528 if (regs->flags & (X86_VM_MASK|X86_EFLAGS_IF)) {
Andi Kleen9ff36ee2009-05-27 21:56:58 +0200529 mce_notify_irq();
Andi Kleen9b1beaf2009-05-27 21:56:59 +0200530 /*
531 * Triggering the work queue here is just an insurance
532 * policy in case the syscall exit notify handler
533 * doesn't run soon enough or ends up running on the
534 * wrong CPU (can happen when audit sleeps)
535 */
536 mce_schedule_work();
Andi Kleenccc3c312009-05-27 21:56:54 +0200537 return;
538 }
539
Hidetoshi Setob77e70b2011-06-08 10:56:02 +0900540 irq_work_queue(&__get_cpu_var(mce_irq_work));
Andi Kleenccc3c312009-05-27 21:56:54 +0200541}
542
Tony Luck85f926942011-12-13 09:48:13 -0800543/*
544 * Read ADDR and MISC registers.
545 */
546static void mce_read_aux(struct mce *m, int i)
547{
548 if (m->status & MCI_STATUS_MISCV)
549 m->misc = mce_rdmsrl(MSR_IA32_MCx_MISC(i));
550 if (m->status & MCI_STATUS_ADDRV) {
551 m->addr = mce_rdmsrl(MSR_IA32_MCx_ADDR(i));
552
553 /*
554 * Mask the reported address by the reported granularity.
555 */
556 if (mce_ser && (m->status & MCI_STATUS_MISCV)) {
557 u8 shift = MCI_MISC_ADDR_LSB(m->misc);
558 m->addr >>= shift;
559 m->addr <<= shift;
560 }
561 }
562}
563
Andi Kleenca84f692009-05-27 21:56:57 +0200564DEFINE_PER_CPU(unsigned, mce_poll_count);
565
Thomas Gleixnerd88203d2007-10-23 22:37:23 +0200566/*
Andi Kleenb79109c2009-02-12 13:43:23 +0100567 * Poll for corrected events or events that happened before reset.
568 * Those are just logged through /dev/mcelog.
569 *
570 * This is executed in standard interrupt context.
Andi Kleened7290d2009-05-27 21:56:57 +0200571 *
572 * Note: spec recommends to panic for fatal unsignalled
573 * errors here. However this would be quite problematic --
574 * we would need to reimplement the Monarch handling and
575 * it would mess up the exclusion between exception handler
576 * and poll hander -- * so we skip this for now.
577 * These cases should not happen anyways, or only when the CPU
578 * is already totally * confused. In this case it's likely it will
579 * not fully execute the machine check handler either.
Andi Kleenb79109c2009-02-12 13:43:23 +0100580 */
Andi Kleenee031c32009-02-12 13:49:34 +0100581void machine_check_poll(enum mcp_flags flags, mce_banks_t *b)
Andi Kleenb79109c2009-02-12 13:43:23 +0100582{
583 struct mce m;
584 int i;
585
Jan Beulich402af0d2010-04-21 15:21:51 +0100586 percpu_inc(mce_poll_count);
Andi Kleenca84f692009-05-27 21:56:57 +0200587
Hidetoshi Setob8325c52011-06-08 10:57:46 +0900588 mce_gather_info(&m, NULL);
Andi Kleenb79109c2009-02-12 13:43:23 +0100589
Andi Kleenb79109c2009-02-12 13:43:23 +0100590 for (i = 0; i < banks; i++) {
Andi Kleencebe1822009-07-09 00:31:43 +0200591 if (!mce_banks[i].ctl || !test_bit(i, *b))
Andi Kleenb79109c2009-02-12 13:43:23 +0100592 continue;
593
594 m.misc = 0;
595 m.addr = 0;
596 m.bank = i;
597 m.tsc = 0;
598
599 barrier();
Andi Kleena2d32bc2009-07-09 00:31:44 +0200600 m.status = mce_rdmsrl(MSR_IA32_MCx_STATUS(i));
Andi Kleenb79109c2009-02-12 13:43:23 +0100601 if (!(m.status & MCI_STATUS_VAL))
602 continue;
603
604 /*
Andi Kleened7290d2009-05-27 21:56:57 +0200605 * Uncorrected or signalled events are handled by the exception
606 * handler when it is enabled, so don't process those here.
Andi Kleenb79109c2009-02-12 13:43:23 +0100607 *
608 * TBD do the same check for MCI_STATUS_EN here?
609 */
Andi Kleened7290d2009-05-27 21:56:57 +0200610 if (!(flags & MCP_UC) &&
611 (m.status & (mce_ser ? MCI_STATUS_S : MCI_STATUS_UC)))
Andi Kleenb79109c2009-02-12 13:43:23 +0100612 continue;
613
Tony Luck85f926942011-12-13 09:48:13 -0800614 mce_read_aux(&m, i);
Andi Kleenb79109c2009-02-12 13:43:23 +0100615
616 if (!(flags & MCP_TIMESTAMP))
617 m.tsc = 0;
618 /*
619 * Don't get the IP here because it's unlikely to
620 * have anything to do with the actual error location.
621 */
Borislav Petkovf0cb5452011-07-18 11:24:45 -0300622 if (!(flags & MCP_DONTLOG) && !mce_dont_log_ce)
Andi Kleen5679af42009-04-07 17:06:55 +0200623 mce_log(&m);
Andi Kleenb79109c2009-02-12 13:43:23 +0100624
625 /*
626 * Clear state for this bank.
627 */
Andi Kleena2d32bc2009-07-09 00:31:44 +0200628 mce_wrmsrl(MSR_IA32_MCx_STATUS(i), 0);
Andi Kleenb79109c2009-02-12 13:43:23 +0100629 }
630
631 /*
632 * Don't clear MCG_STATUS here because it's only defined for
633 * exceptions.
634 */
Andi Kleen88921be2009-05-27 21:56:51 +0200635
636 sync_core();
Andi Kleenb79109c2009-02-12 13:43:23 +0100637}
Andi Kleenea149b32009-04-29 19:31:00 +0200638EXPORT_SYMBOL_GPL(machine_check_poll);
Andi Kleenb79109c2009-02-12 13:43:23 +0100639
640/*
Andi Kleenbd19a5e2009-05-27 21:56:55 +0200641 * Do a quick check if any of the events requires a panic.
642 * This decides if we keep the events around or clear them.
643 */
644static int mce_no_way_out(struct mce *m, char **msg)
645{
646 int i;
647
648 for (i = 0; i < banks; i++) {
Andi Kleena2d32bc2009-07-09 00:31:44 +0200649 m->status = mce_rdmsrl(MSR_IA32_MCx_STATUS(i));
Andi Kleenbd19a5e2009-05-27 21:56:55 +0200650 if (mce_severity(m, tolerant, msg) >= MCE_PANIC_SEVERITY)
651 return 1;
652 }
653 return 0;
654}
655
656/*
Andi Kleen3c079792009-05-27 21:56:55 +0200657 * Variable to establish order between CPUs while scanning.
658 * Each CPU spins initially until executing is equal its number.
659 */
660static atomic_t mce_executing;
661
662/*
663 * Defines order of CPUs on entry. First CPU becomes Monarch.
664 */
665static atomic_t mce_callin;
666
667/*
668 * Check if a timeout waiting for other CPUs happened.
669 */
670static int mce_timed_out(u64 *t)
671{
672 /*
673 * The others already did panic for some reason.
674 * Bail out like in a timeout.
675 * rmb() to tell the compiler that system_state
676 * might have been modified by someone else.
677 */
678 rmb();
679 if (atomic_read(&mce_paniced))
680 wait_for_panic();
681 if (!monarch_timeout)
682 goto out;
683 if ((s64)*t < SPINUNIT) {
684 /* CHECKME: Make panic default for 1 too? */
685 if (tolerant < 1)
686 mce_panic("Timeout synchronizing machine check over CPUs",
687 NULL, NULL);
688 cpu_missing = 1;
689 return 1;
690 }
691 *t -= SPINUNIT;
692out:
693 touch_nmi_watchdog();
694 return 0;
695}
696
697/*
698 * The Monarch's reign. The Monarch is the CPU who entered
699 * the machine check handler first. It waits for the others to
700 * raise the exception too and then grades them. When any
701 * error is fatal panic. Only then let the others continue.
702 *
703 * The other CPUs entering the MCE handler will be controlled by the
704 * Monarch. They are called Subjects.
705 *
706 * This way we prevent any potential data corruption in a unrecoverable case
707 * and also makes sure always all CPU's errors are examined.
708 *
Hidetoshi Seto680b6cf2009-08-26 16:20:36 +0900709 * Also this detects the case of a machine check event coming from outer
Andi Kleen3c079792009-05-27 21:56:55 +0200710 * space (not detected by any CPUs) In this case some external agent wants
711 * us to shut down, so panic too.
712 *
713 * The other CPUs might still decide to panic if the handler happens
714 * in a unrecoverable place, but in this case the system is in a semi-stable
715 * state and won't corrupt anything by itself. It's ok to let the others
716 * continue for a bit first.
717 *
718 * All the spin loops have timeouts; when a timeout happens a CPU
719 * typically elects itself to be Monarch.
720 */
721static void mce_reign(void)
722{
723 int cpu;
724 struct mce *m = NULL;
725 int global_worst = 0;
726 char *msg = NULL;
727 char *nmsg = NULL;
728
729 /*
730 * This CPU is the Monarch and the other CPUs have run
731 * through their handlers.
732 * Grade the severity of the errors of all the CPUs.
733 */
734 for_each_possible_cpu(cpu) {
735 int severity = mce_severity(&per_cpu(mces_seen, cpu), tolerant,
736 &nmsg);
737 if (severity > global_worst) {
738 msg = nmsg;
739 global_worst = severity;
740 m = &per_cpu(mces_seen, cpu);
741 }
742 }
743
744 /*
745 * Cannot recover? Panic here then.
746 * This dumps all the mces in the log buffer and stops the
747 * other CPUs.
748 */
749 if (m && global_worst >= MCE_PANIC_SEVERITY && tolerant < 3)
Andi Kleenac960372009-05-27 21:56:58 +0200750 mce_panic("Fatal Machine check", m, msg);
Andi Kleen3c079792009-05-27 21:56:55 +0200751
752 /*
753 * For UC somewhere we let the CPU who detects it handle it.
754 * Also must let continue the others, otherwise the handling
755 * CPU could deadlock on a lock.
756 */
757
758 /*
759 * No machine check event found. Must be some external
760 * source or one CPU is hung. Panic.
761 */
Hidetoshi Seto680b6cf2009-08-26 16:20:36 +0900762 if (global_worst <= MCE_KEEP_SEVERITY && tolerant < 3)
Andi Kleen3c079792009-05-27 21:56:55 +0200763 mce_panic("Machine check from unknown source", NULL, NULL);
764
765 /*
766 * Now clear all the mces_seen so that they don't reappear on
767 * the next mce.
768 */
769 for_each_possible_cpu(cpu)
770 memset(&per_cpu(mces_seen, cpu), 0, sizeof(struct mce));
771}
772
773static atomic_t global_nwo;
774
775/*
776 * Start of Monarch synchronization. This waits until all CPUs have
777 * entered the exception handler and then determines if any of them
778 * saw a fatal event that requires panic. Then it executes them
779 * in the entry order.
780 * TBD double check parallel CPU hotunplug
781 */
Hidetoshi Seto7fb06fc2009-06-15 18:18:43 +0900782static int mce_start(int *no_way_out)
Andi Kleen3c079792009-05-27 21:56:55 +0200783{
Hidetoshi Seto7fb06fc2009-06-15 18:18:43 +0900784 int order;
Andi Kleen3c079792009-05-27 21:56:55 +0200785 int cpus = num_online_cpus();
786 u64 timeout = (u64)monarch_timeout * NSEC_PER_USEC;
787
Hidetoshi Seto7fb06fc2009-06-15 18:18:43 +0900788 if (!timeout)
789 return -1;
Andi Kleen3c079792009-05-27 21:56:55 +0200790
Hidetoshi Seto7fb06fc2009-06-15 18:18:43 +0900791 atomic_add(*no_way_out, &global_nwo);
Huang Ying184e1fd2009-06-15 15:37:07 +0800792 /*
793 * global_nwo should be updated before mce_callin
794 */
795 smp_wmb();
Borislav Petkova95436e2009-06-20 23:28:22 -0700796 order = atomic_inc_return(&mce_callin);
Andi Kleen3c079792009-05-27 21:56:55 +0200797
798 /*
799 * Wait for everyone.
800 */
801 while (atomic_read(&mce_callin) != cpus) {
802 if (mce_timed_out(&timeout)) {
803 atomic_set(&global_nwo, 0);
Hidetoshi Seto7fb06fc2009-06-15 18:18:43 +0900804 return -1;
Andi Kleen3c079792009-05-27 21:56:55 +0200805 }
806 ndelay(SPINUNIT);
807 }
808
809 /*
Huang Ying184e1fd2009-06-15 15:37:07 +0800810 * mce_callin should be read before global_nwo
811 */
812 smp_rmb();
Hidetoshi Seto7fb06fc2009-06-15 18:18:43 +0900813
814 if (order == 1) {
815 /*
816 * Monarch: Starts executing now, the others wait.
817 */
818 atomic_set(&mce_executing, 1);
819 } else {
820 /*
821 * Subject: Now start the scanning loop one by one in
822 * the original callin order.
823 * This way when there are any shared banks it will be
824 * only seen by one CPU before cleared, avoiding duplicates.
825 */
826 while (atomic_read(&mce_executing) < order) {
827 if (mce_timed_out(&timeout)) {
828 atomic_set(&global_nwo, 0);
829 return -1;
830 }
831 ndelay(SPINUNIT);
832 }
833 }
834
Huang Ying184e1fd2009-06-15 15:37:07 +0800835 /*
Andi Kleen3c079792009-05-27 21:56:55 +0200836 * Cache the global no_way_out state.
837 */
Hidetoshi Seto7fb06fc2009-06-15 18:18:43 +0900838 *no_way_out = atomic_read(&global_nwo);
Andi Kleen3c079792009-05-27 21:56:55 +0200839
Hidetoshi Seto7fb06fc2009-06-15 18:18:43 +0900840 return order;
Andi Kleen3c079792009-05-27 21:56:55 +0200841}
842
843/*
844 * Synchronize between CPUs after main scanning loop.
845 * This invokes the bulk of the Monarch processing.
846 */
847static int mce_end(int order)
848{
849 int ret = -1;
850 u64 timeout = (u64)monarch_timeout * NSEC_PER_USEC;
851
852 if (!timeout)
853 goto reset;
854 if (order < 0)
855 goto reset;
856
857 /*
858 * Allow others to run.
859 */
860 atomic_inc(&mce_executing);
861
862 if (order == 1) {
863 /* CHECKME: Can this race with a parallel hotplug? */
864 int cpus = num_online_cpus();
865
866 /*
867 * Monarch: Wait for everyone to go through their scanning
868 * loops.
869 */
870 while (atomic_read(&mce_executing) <= cpus) {
871 if (mce_timed_out(&timeout))
872 goto reset;
873 ndelay(SPINUNIT);
874 }
875
876 mce_reign();
877 barrier();
878 ret = 0;
879 } else {
880 /*
881 * Subject: Wait for Monarch to finish.
882 */
883 while (atomic_read(&mce_executing) != 0) {
884 if (mce_timed_out(&timeout))
885 goto reset;
886 ndelay(SPINUNIT);
887 }
888
889 /*
890 * Don't reset anything. That's done by the Monarch.
891 */
892 return 0;
893 }
894
895 /*
896 * Reset all global state.
897 */
898reset:
899 atomic_set(&global_nwo, 0);
900 atomic_set(&mce_callin, 0);
901 barrier();
902
903 /*
904 * Let others run again.
905 */
906 atomic_set(&mce_executing, 0);
907 return ret;
908}
909
Andi Kleen9b1beaf2009-05-27 21:56:59 +0200910/*
911 * Check if the address reported by the CPU is in a format we can parse.
912 * It would be possible to add code for most other cases, but all would
913 * be somewhat complicated (e.g. segment offset would require an instruction
Lucas De Marchi0d2eb442011-03-17 16:24:16 -0300914 * parser). So only support physical addresses up to page granuality for now.
Andi Kleen9b1beaf2009-05-27 21:56:59 +0200915 */
916static int mce_usable_address(struct mce *m)
917{
918 if (!(m->status & MCI_STATUS_MISCV) || !(m->status & MCI_STATUS_ADDRV))
919 return 0;
Hidetoshi Seto2b90e772011-06-08 10:56:56 +0900920 if (MCI_MISC_ADDR_LSB(m->misc) > PAGE_SHIFT)
Andi Kleen9b1beaf2009-05-27 21:56:59 +0200921 return 0;
Hidetoshi Seto2b90e772011-06-08 10:56:56 +0900922 if (MCI_MISC_ADDR_MODE(m->misc) != MCI_MISC_ADDR_PHYS)
Andi Kleen9b1beaf2009-05-27 21:56:59 +0200923 return 0;
924 return 1;
925}
926
Andi Kleen3c079792009-05-27 21:56:55 +0200927static void mce_clear_state(unsigned long *toclear)
928{
929 int i;
930
931 for (i = 0; i < banks; i++) {
932 if (test_bit(i, toclear))
Andi Kleena2d32bc2009-07-09 00:31:44 +0200933 mce_wrmsrl(MSR_IA32_MCx_STATUS(i), 0);
Andi Kleen3c079792009-05-27 21:56:55 +0200934 }
935}
936
937/*
Tony Luckaf104e32011-12-14 15:55:20 -0800938 * Need to save faulting physical address associated with a process
939 * in the machine check handler some place where we can grab it back
940 * later in mce_notify_process()
941 */
942#define MCE_INFO_MAX 16
943
944struct mce_info {
945 atomic_t inuse;
946 struct task_struct *t;
947 __u64 paddr;
948} mce_info[MCE_INFO_MAX];
949
950static void mce_save_info(__u64 addr)
951{
952 struct mce_info *mi;
953
954 for (mi = mce_info; mi < &mce_info[MCE_INFO_MAX]; mi++) {
955 if (atomic_cmpxchg(&mi->inuse, 0, 1) == 0) {
956 mi->t = current;
957 mi->paddr = addr;
958 return;
959 }
960 }
961
962 mce_panic("Too many concurrent recoverable errors", NULL, NULL);
963}
964
965static struct mce_info *mce_find_info(void)
966{
967 struct mce_info *mi;
968
969 for (mi = mce_info; mi < &mce_info[MCE_INFO_MAX]; mi++)
970 if (atomic_read(&mi->inuse) && mi->t == current)
971 return mi;
972 return NULL;
973}
974
975static void mce_clear_info(struct mce_info *mi)
976{
977 atomic_set(&mi->inuse, 0);
978}
979
980/*
Andi Kleenb79109c2009-02-12 13:43:23 +0100981 * The actual machine check handler. This only handles real
982 * exceptions when something got corrupted coming in through int 18.
983 *
984 * This is executed in NMI context not subject to normal locking rules. This
985 * implies that most kernel services cannot be safely used. Don't even
986 * think about putting a printk in there!
Andi Kleen3c079792009-05-27 21:56:55 +0200987 *
988 * On Intel systems this is entered on all CPUs in parallel through
989 * MCE broadcast. However some CPUs might be broken beyond repair,
990 * so be always careful when synchronizing with others.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 */
Ingo Molnare9eee032009-04-08 12:31:17 +0200992void do_machine_check(struct pt_regs *regs, long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993{
Andi Kleen3c079792009-05-27 21:56:55 +0200994 struct mce m, *final;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 int i;
Andi Kleen3c079792009-05-27 21:56:55 +0200996 int worst = 0;
997 int severity;
998 /*
999 * Establish sequential order between the CPUs entering the machine
1000 * check handler.
1001 */
Hidetoshi Seto7fb06fc2009-06-15 18:18:43 +09001002 int order;
Tim Hockinbd784322007-07-21 17:10:37 +02001003 /*
1004 * If no_way_out gets set, there is no safe way to recover from this
1005 * MCE. If tolerant is cranked up, we'll try anyway.
1006 */
1007 int no_way_out = 0;
1008 /*
1009 * If kill_it gets set, there might be a way to recover from this
1010 * error.
1011 */
1012 int kill_it = 0;
Andi Kleenb79109c2009-02-12 13:43:23 +01001013 DECLARE_BITMAP(toclear, MAX_NR_BANKS);
Andi Kleenbd19a5e2009-05-27 21:56:55 +02001014 char *msg = "Unknown";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
Andi Kleen553f2652006-04-07 19:49:57 +02001016 atomic_inc(&mce_entry);
1017
Jan Beulich402af0d2010-04-21 15:21:51 +01001018 percpu_inc(mce_exception_count);
Andi Kleen01ca79f2009-05-27 21:56:52 +02001019
Andi Kleenb79109c2009-02-12 13:43:23 +01001020 if (!banks)
Andi Kleen32561692009-05-27 21:56:53 +02001021 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
Hidetoshi Setob8325c52011-06-08 10:57:46 +09001023 mce_gather_info(&m, regs);
Andi Kleenb5f2fa42009-02-12 13:43:22 +01001024
Andi Kleen3c079792009-05-27 21:56:55 +02001025 final = &__get_cpu_var(mces_seen);
1026 *final = m;
1027
Hidetoshi Seto680b6cf2009-08-26 16:20:36 +09001028 no_way_out = mce_no_way_out(&m, &msg);
1029
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 barrier();
1031
Andi Kleen3c079792009-05-27 21:56:55 +02001032 /*
Tony Lucka8c321f2012-01-03 11:45:45 -08001033 * When no restart IP might need to kill or panic.
1034 * Assume the worst for now, but if we find the
1035 * severity is MCE_AR_SEVERITY we have other options.
Andi Kleened7290d2009-05-27 21:56:57 +02001036 */
1037 if (!(m.mcgstatus & MCG_STATUS_RIPV))
1038 kill_it = 1;
1039
1040 /*
Andi Kleen3c079792009-05-27 21:56:55 +02001041 * Go through all the banks in exclusion of the other CPUs.
1042 * This way we don't report duplicated events on shared banks
1043 * because the first one to see it will clear it.
1044 */
Hidetoshi Seto7fb06fc2009-06-15 18:18:43 +09001045 order = mce_start(&no_way_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 for (i = 0; i < banks; i++) {
Andi Kleenb79109c2009-02-12 13:43:23 +01001047 __clear_bit(i, toclear);
Andi Kleencebe1822009-07-09 00:31:43 +02001048 if (!mce_banks[i].ctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 continue;
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001050
1051 m.misc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 m.addr = 0;
1053 m.bank = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
Andi Kleena2d32bc2009-07-09 00:31:44 +02001055 m.status = mce_rdmsrl(MSR_IA32_MCx_STATUS(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 if ((m.status & MCI_STATUS_VAL) == 0)
1057 continue;
1058
Andi Kleenb79109c2009-02-12 13:43:23 +01001059 /*
Andi Kleened7290d2009-05-27 21:56:57 +02001060 * Non uncorrected or non signaled errors are handled by
1061 * machine_check_poll. Leave them alone, unless this panics.
Andi Kleenb79109c2009-02-12 13:43:23 +01001062 */
Andi Kleened7290d2009-05-27 21:56:57 +02001063 if (!(m.status & (mce_ser ? MCI_STATUS_S : MCI_STATUS_UC)) &&
1064 !no_way_out)
Andi Kleenb79109c2009-02-12 13:43:23 +01001065 continue;
1066
1067 /*
1068 * Set taint even when machine check was not enabled.
1069 */
1070 add_taint(TAINT_MACHINE_CHECK);
1071
Andi Kleened7290d2009-05-27 21:56:57 +02001072 severity = mce_severity(&m, tolerant, NULL);
Andi Kleenb79109c2009-02-12 13:43:23 +01001073
Andi Kleened7290d2009-05-27 21:56:57 +02001074 /*
1075 * When machine check was for corrected handler don't touch,
1076 * unless we're panicing.
1077 */
1078 if (severity == MCE_KEEP_SEVERITY && !no_way_out)
1079 continue;
1080 __set_bit(i, toclear);
1081 if (severity == MCE_NO_SEVERITY) {
Andi Kleenb79109c2009-02-12 13:43:23 +01001082 /*
1083 * Machine check event was not enabled. Clear, but
1084 * ignore.
1085 */
1086 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 }
1088
Tony Luck85f926942011-12-13 09:48:13 -08001089 mce_read_aux(&m, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
Andi Kleen9b1beaf2009-05-27 21:56:59 +02001091 /*
1092 * Action optional error. Queue address for later processing.
1093 * When the ring overflows we just ignore the AO error.
1094 * RED-PEN add some logging mechanism when
1095 * usable_address or mce_add_ring fails.
1096 * RED-PEN don't ignore overflow for tolerant == 0
1097 */
1098 if (severity == MCE_AO_SEVERITY && mce_usable_address(&m))
1099 mce_ring_add(m.addr >> PAGE_SHIFT);
1100
Andi Kleenb79109c2009-02-12 13:43:23 +01001101 mce_log(&m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
Andi Kleen3c079792009-05-27 21:56:55 +02001103 if (severity > worst) {
1104 *final = m;
1105 worst = severity;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 }
1108
Tony Lucka8c321f2012-01-03 11:45:45 -08001109 /* mce_clear_state will clear *final, save locally for use later */
1110 m = *final;
1111
Andi Kleen3c079792009-05-27 21:56:55 +02001112 if (!no_way_out)
1113 mce_clear_state(toclear);
1114
Ingo Molnare9eee032009-04-08 12:31:17 +02001115 /*
Andi Kleen3c079792009-05-27 21:56:55 +02001116 * Do most of the synchronization with other CPUs.
1117 * When there's any problem use only local no_way_out state.
Ingo Molnare9eee032009-04-08 12:31:17 +02001118 */
Andi Kleen3c079792009-05-27 21:56:55 +02001119 if (mce_end(order) < 0)
1120 no_way_out = worst >= MCE_PANIC_SEVERITY;
Tim Hockinbd784322007-07-21 17:10:37 +02001121
1122 /*
Tony Lucka8c321f2012-01-03 11:45:45 -08001123 * At insane "tolerant" levels we take no action. Otherwise
1124 * we only die if we have no other choice. For less serious
1125 * issues we try to recover, or limit damage to the current
1126 * process.
Tim Hockinbd784322007-07-21 17:10:37 +02001127 */
Tony Lucka8c321f2012-01-03 11:45:45 -08001128 if (tolerant < 3) {
1129 if (no_way_out)
1130 mce_panic("Fatal machine check on current CPU", &m, msg);
1131 if (worst == MCE_AR_SEVERITY) {
1132 /* schedule action before return to userland */
1133 mce_save_info(m.addr);
1134 set_thread_flag(TIF_MCE_NOTIFY);
1135 } else if (kill_it) {
1136 force_sig(SIGBUS, current);
1137 }
1138 }
Tim Hockine02e68d2007-07-21 17:10:36 +02001139
Andi Kleen3c079792009-05-27 21:56:55 +02001140 if (worst > 0)
1141 mce_report_event(regs);
Andi Kleen5f8c1a52009-04-29 19:29:12 +02001142 mce_wrmsrl(MSR_IA32_MCG_STATUS, 0);
Andi Kleen32561692009-05-27 21:56:53 +02001143out:
Andi Kleen553f2652006-04-07 19:49:57 +02001144 atomic_dec(&mce_entry);
Andi Kleen88921be2009-05-27 21:56:51 +02001145 sync_core();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146}
Andi Kleenea149b32009-04-29 19:31:00 +02001147EXPORT_SYMBOL_GPL(do_machine_check);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
Tony Luckcd42f4a2011-12-15 10:48:12 -08001149#ifndef CONFIG_MEMORY_FAILURE
1150int memory_failure(unsigned long pfn, int vector, int flags)
Andi Kleen9b1beaf2009-05-27 21:56:59 +02001151{
Tony Lucka8c321f2012-01-03 11:45:45 -08001152 /* mce_severity() should not hand us an ACTION_REQUIRED error */
1153 BUG_ON(flags & MF_ACTION_REQUIRED);
Tony Luckcd42f4a2011-12-15 10:48:12 -08001154 printk(KERN_ERR "Uncorrected memory error in page 0x%lx ignored\n"
1155 "Rebuild kernel with CONFIG_MEMORY_FAILURE=y for smarter handling\n", pfn);
1156
1157 return 0;
Andi Kleen9b1beaf2009-05-27 21:56:59 +02001158}
Tony Luckcd42f4a2011-12-15 10:48:12 -08001159#endif
Andi Kleen9b1beaf2009-05-27 21:56:59 +02001160
1161/*
Tony Lucka8c321f2012-01-03 11:45:45 -08001162 * Called in process context that interrupted by MCE and marked with
1163 * TIF_MCE_NOTIFY, just before returning to erroneous userland.
1164 * This code is allowed to sleep.
1165 * Attempt possible recovery such as calling the high level VM handler to
1166 * process any corrupted pages, and kill/signal current process if required.
1167 * Action required errors are handled here.
Andi Kleen9b1beaf2009-05-27 21:56:59 +02001168 */
1169void mce_notify_process(void)
1170{
1171 unsigned long pfn;
Tony Lucka8c321f2012-01-03 11:45:45 -08001172 struct mce_info *mi = mce_find_info();
1173
1174 if (!mi)
1175 mce_panic("Lost physical address for unconsumed uncorrectable error", NULL, NULL);
1176 pfn = mi->paddr >> PAGE_SHIFT;
1177
1178 clear_thread_flag(TIF_MCE_NOTIFY);
1179
1180 pr_err("Uncorrected hardware memory error in user-access at %llx",
1181 mi->paddr);
1182 if (memory_failure(pfn, MCE_VECTOR, MF_ACTION_REQUIRED) < 0) {
1183 pr_err("Memory error not recovered");
1184 force_sig(SIGBUS, current);
1185 }
1186 mce_clear_info(mi);
Andi Kleen9b1beaf2009-05-27 21:56:59 +02001187}
1188
Tony Lucka8c321f2012-01-03 11:45:45 -08001189/*
1190 * Action optional processing happens here (picking up
1191 * from the list of faulting pages that do_machine_check()
1192 * placed into the "ring").
1193 */
Andi Kleen9b1beaf2009-05-27 21:56:59 +02001194static void mce_process_work(struct work_struct *dummy)
1195{
Tony Lucka8c321f2012-01-03 11:45:45 -08001196 unsigned long pfn;
1197
1198 while (mce_ring_get(&pfn))
1199 memory_failure(pfn, MCE_VECTOR, 0);
Andi Kleen9b1beaf2009-05-27 21:56:59 +02001200}
1201
Dmitriy Zavin15d5f832006-09-26 10:52:42 +02001202#ifdef CONFIG_X86_MCE_INTEL
1203/***
1204 * mce_log_therm_throt_event - Logs the thermal throttling event to mcelog
Simon Arlott676b1852007-10-20 01:25:36 +02001205 * @cpu: The CPU on which the event occurred.
Dmitriy Zavin15d5f832006-09-26 10:52:42 +02001206 * @status: Event status information
1207 *
1208 * This function should be called by the thermal interrupt after the
1209 * event has been processed and the decision was made to log the event
1210 * further.
1211 *
1212 * The status parameter will be saved to the 'status' field of 'struct mce'
1213 * and historically has been the register value of the
1214 * MSR_IA32_THERMAL_STATUS (Intel) msr.
1215 */
Andi Kleenb5f2fa42009-02-12 13:43:22 +01001216void mce_log_therm_throt_event(__u64 status)
Dmitriy Zavin15d5f832006-09-26 10:52:42 +02001217{
1218 struct mce m;
1219
Andi Kleenb5f2fa42009-02-12 13:43:22 +01001220 mce_setup(&m);
Dmitriy Zavin15d5f832006-09-26 10:52:42 +02001221 m.bank = MCE_THERMAL_BANK;
1222 m.status = status;
Dmitriy Zavin15d5f832006-09-26 10:52:42 +02001223 mce_log(&m);
1224}
1225#endif /* CONFIG_X86_MCE_INTEL */
1226
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227/*
Tim Hockin8a336b02007-05-02 19:27:19 +02001228 * Periodic polling timer for "silent" machine check errors. If the
1229 * poller finds an MCE, poll 2x faster. When the poller finds no more
1230 * errors, poll 2x slower (up to check_interval seconds).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232static int check_interval = 5 * 60; /* 5 minutes */
Ingo Molnare9eee032009-04-08 12:31:17 +02001233
Tejun Heo245b2e72009-06-24 15:13:48 +09001234static DEFINE_PER_CPU(int, mce_next_interval); /* in jiffies */
Andi Kleen52d168e2009-02-12 13:39:29 +01001235static DEFINE_PER_CPU(struct timer_list, mce_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
Borislav Petkov5e099542009-10-16 12:31:32 +02001237static void mce_start_timer(unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238{
Andi Kleen52d168e2009-02-12 13:39:29 +01001239 struct timer_list *t = &per_cpu(mce_timer, data);
Andi Kleen6298c512009-04-09 12:28:22 +02001240 int *n;
Andi Kleen52d168e2009-02-12 13:39:29 +01001241
1242 WARN_ON(smp_processor_id() != data);
1243
Tejun Heo7b543a52010-12-18 16:30:05 +01001244 if (mce_available(__this_cpu_ptr(&cpu_info))) {
Andi Kleenee031c32009-02-12 13:49:34 +01001245 machine_check_poll(MCP_TIMESTAMP,
1246 &__get_cpu_var(mce_poll_banks));
Ingo Molnare9eee032009-04-08 12:31:17 +02001247 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
1249 /*
Tim Hockine02e68d2007-07-21 17:10:36 +02001250 * Alert userspace if needed. If we logged an MCE, reduce the
1251 * polling interval, otherwise increase the polling interval.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 */
Tejun Heo245b2e72009-06-24 15:13:48 +09001253 n = &__get_cpu_var(mce_next_interval);
Andi Kleen9ff36ee2009-05-27 21:56:58 +02001254 if (mce_notify_irq())
Andi Kleen6298c512009-04-09 12:28:22 +02001255 *n = max(*n/2, HZ/100);
Hidetoshi Seto14a02532009-04-30 16:04:51 +09001256 else
Andi Kleen6298c512009-04-09 12:28:22 +02001257 *n = min(*n*2, (int)round_jiffies_relative(check_interval*HZ));
Tim Hockin8a336b02007-05-02 19:27:19 +02001258
Andi Kleen6298c512009-04-09 12:28:22 +02001259 t->expires = jiffies + *n;
Hidetoshi Seto5be60662009-06-24 09:21:10 +09001260 add_timer_on(t, smp_processor_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261}
1262
Hidetoshi Seto9aaef962011-06-17 04:40:36 -04001263/* Must not be called in IRQ context where del_timer_sync() can deadlock */
1264static void mce_timer_delete_all(void)
1265{
1266 int cpu;
1267
1268 for_each_online_cpu(cpu)
1269 del_timer_sync(&per_cpu(mce_timer, cpu));
1270}
1271
Andi Kleen9bd98402009-02-12 13:39:28 +01001272static void mce_do_trigger(struct work_struct *work)
1273{
Hidetoshi Seto1020bcb2009-06-15 17:20:57 +09001274 call_usermodehelper(mce_helper, mce_helper_argv, NULL, UMH_NO_WAIT);
Andi Kleen9bd98402009-02-12 13:39:28 +01001275}
1276
1277static DECLARE_WORK(mce_trigger_work, mce_do_trigger);
1278
Tim Hockine02e68d2007-07-21 17:10:36 +02001279/*
Andi Kleen9bd98402009-02-12 13:39:28 +01001280 * Notify the user(s) about new machine check events.
1281 * Can be called from interrupt context, but not from machine check/NMI
1282 * context.
Tim Hockine02e68d2007-07-21 17:10:36 +02001283 */
Andi Kleen9ff36ee2009-05-27 21:56:58 +02001284int mce_notify_irq(void)
Tim Hockine02e68d2007-07-21 17:10:36 +02001285{
Andi Kleen8457c842009-02-12 13:49:33 +01001286 /* Not more than two messages every minute */
1287 static DEFINE_RATELIMIT_STATE(ratelimit, 60*HZ, 2);
1288
Hidetoshi Seto1020bcb2009-06-15 17:20:57 +09001289 if (test_and_clear_bit(0, &mce_need_notify)) {
Hidetoshi Seto93b62c32011-06-08 11:00:45 +09001290 /* wake processes polling /dev/mcelog */
1291 wake_up_interruptible(&mce_chrdev_wait);
Andi Kleen9bd98402009-02-12 13:39:28 +01001292
1293 /*
1294 * There is no risk of missing notifications because
1295 * work_pending is always cleared before the function is
1296 * executed.
1297 */
Hidetoshi Seto1020bcb2009-06-15 17:20:57 +09001298 if (mce_helper[0] && !work_pending(&mce_trigger_work))
Andi Kleen9bd98402009-02-12 13:39:28 +01001299 schedule_work(&mce_trigger_work);
Tim Hockine02e68d2007-07-21 17:10:36 +02001300
Andi Kleen8457c842009-02-12 13:49:33 +01001301 if (__ratelimit(&ratelimit))
Huang Yinga2d7b0d2010-06-08 14:35:39 +08001302 pr_info(HW_ERR "Machine check events logged\n");
Tim Hockine02e68d2007-07-21 17:10:36 +02001303
1304 return 1;
1305 }
1306 return 0;
1307}
Andi Kleen9ff36ee2009-05-27 21:56:58 +02001308EXPORT_SYMBOL_GPL(mce_notify_irq);
Tim Hockine02e68d2007-07-21 17:10:36 +02001309
Hidetoshi Setocffd3772009-11-12 15:52:40 +09001310static int __cpuinit __mcheck_cpu_mce_banks_init(void)
Andi Kleencebe1822009-07-09 00:31:43 +02001311{
1312 int i;
1313
1314 mce_banks = kzalloc(banks * sizeof(struct mce_bank), GFP_KERNEL);
1315 if (!mce_banks)
1316 return -ENOMEM;
1317 for (i = 0; i < banks; i++) {
1318 struct mce_bank *b = &mce_banks[i];
Ingo Molnar11868a22009-09-23 17:49:55 +02001319
Andi Kleencebe1822009-07-09 00:31:43 +02001320 b->ctl = -1ULL;
1321 b->init = 1;
1322 }
1323 return 0;
1324}
1325
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001326/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 * Initialize Machine Checks for a CPU.
1328 */
Borislav Petkov5e099542009-10-16 12:31:32 +02001329static int __cpuinit __mcheck_cpu_cap_init(void)
Andi Kleen0d7482e32009-02-17 23:07:13 +01001330{
Andi Kleen0d7482e32009-02-17 23:07:13 +01001331 unsigned b;
Ingo Molnare9eee032009-04-08 12:31:17 +02001332 u64 cap;
Andi Kleen0d7482e32009-02-17 23:07:13 +01001333
1334 rdmsrl(MSR_IA32_MCG_CAP, cap);
Thomas Gleixner01c66802009-04-08 12:31:24 +02001335
1336 b = cap & MCG_BANKCNT_MASK;
Roland Dreier93ae5012009-10-15 14:21:14 -07001337 if (!banks)
1338 printk(KERN_INFO "mce: CPU supports %d MCE banks\n", b);
Ingo Molnarb6592942009-04-08 12:31:27 +02001339
Andi Kleen0d7482e32009-02-17 23:07:13 +01001340 if (b > MAX_NR_BANKS) {
1341 printk(KERN_WARNING
1342 "MCE: Using only %u machine check banks out of %u\n",
1343 MAX_NR_BANKS, b);
1344 b = MAX_NR_BANKS;
1345 }
1346
1347 /* Don't support asymmetric configurations today */
1348 WARN_ON(banks != 0 && b != banks);
1349 banks = b;
Andi Kleencebe1822009-07-09 00:31:43 +02001350 if (!mce_banks) {
Hidetoshi Setocffd3772009-11-12 15:52:40 +09001351 int err = __mcheck_cpu_mce_banks_init();
Ingo Molnar11868a22009-09-23 17:49:55 +02001352
Andi Kleencebe1822009-07-09 00:31:43 +02001353 if (err)
1354 return err;
Andi Kleen0d7482e32009-02-17 23:07:13 +01001355 }
1356
1357 /* Use accurate RIP reporting if available. */
Thomas Gleixner01c66802009-04-08 12:31:24 +02001358 if ((cap & MCG_EXT_P) && MCG_EXT_CNT(cap) >= 9)
Andi Kleen0d7482e32009-02-17 23:07:13 +01001359 rip_msr = MSR_IA32_MCG_EIP;
1360
Andi Kleened7290d2009-05-27 21:56:57 +02001361 if (cap & MCG_SER_P)
1362 mce_ser = 1;
1363
Andi Kleen0d7482e32009-02-17 23:07:13 +01001364 return 0;
1365}
1366
Borislav Petkov5e099542009-10-16 12:31:32 +02001367static void __mcheck_cpu_init_generic(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368{
Ingo Molnare9eee032009-04-08 12:31:17 +02001369 mce_banks_t all_banks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 u64 cap;
1371 int i;
1372
Andi Kleenb79109c2009-02-12 13:43:23 +01001373 /*
1374 * Log the machine checks left over from the previous reset.
1375 */
Andi Kleenee031c32009-02-12 13:49:34 +01001376 bitmap_fill(all_banks, MAX_NR_BANKS);
Andi Kleen5679af42009-04-07 17:06:55 +02001377 machine_check_poll(MCP_UC|(!mce_bootlog ? MCP_DONTLOG : 0), &all_banks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
1379 set_in_cr4(X86_CR4_MCE);
1380
Andi Kleen0d7482e32009-02-17 23:07:13 +01001381 rdmsrl(MSR_IA32_MCG_CAP, cap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 if (cap & MCG_CTL_P)
1383 wrmsr(MSR_IA32_MCG_CTL, 0xffffffff, 0xffffffff);
1384
1385 for (i = 0; i < banks; i++) {
Andi Kleencebe1822009-07-09 00:31:43 +02001386 struct mce_bank *b = &mce_banks[i];
Ingo Molnar11868a22009-09-23 17:49:55 +02001387
Andi Kleencebe1822009-07-09 00:31:43 +02001388 if (!b->init)
Andi Kleen06b7a7a2009-04-27 18:37:43 +02001389 continue;
Andi Kleena2d32bc2009-07-09 00:31:44 +02001390 wrmsrl(MSR_IA32_MCx_CTL(i), b->ctl);
1391 wrmsrl(MSR_IA32_MCx_STATUS(i), 0);
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001392 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393}
1394
1395/* Add per CPU specific workarounds here */
Borislav Petkov5e099542009-10-16 12:31:32 +02001396static int __cpuinit __mcheck_cpu_apply_quirks(struct cpuinfo_x86 *c)
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001397{
Ingo Molnare412cd22009-08-17 10:19:00 +02001398 if (c->x86_vendor == X86_VENDOR_UNKNOWN) {
1399 pr_info("MCE: unknown CPU type - not enabling MCE support.\n");
1400 return -EOPNOTSUPP;
1401 }
1402
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 /* This should be disabled by the BIOS, but isn't always */
Jan Beulich911f6a72008-04-22 16:22:21 +01001404 if (c->x86_vendor == X86_VENDOR_AMD) {
Ingo Molnare9eee032009-04-08 12:31:17 +02001405 if (c->x86 == 15 && banks > 4) {
1406 /*
1407 * disable GART TBL walk error reporting, which
1408 * trips off incorrectly with the IOMMU & 3ware
1409 * & Cerberus:
1410 */
Andi Kleencebe1822009-07-09 00:31:43 +02001411 clear_bit(10, (unsigned long *)&mce_banks[4].ctl);
Ingo Molnare9eee032009-04-08 12:31:17 +02001412 }
1413 if (c->x86 <= 17 && mce_bootlog < 0) {
1414 /*
1415 * Lots of broken BIOS around that don't clear them
1416 * by default and leave crap in there. Don't log:
1417 */
Jan Beulich911f6a72008-04-22 16:22:21 +01001418 mce_bootlog = 0;
Ingo Molnare9eee032009-04-08 12:31:17 +02001419 }
Andi Kleen2e6f6942009-04-27 18:42:48 +02001420 /*
1421 * Various K7s with broken bank 0 around. Always disable
1422 * by default.
1423 */
Andi Kleen203abd62009-06-15 14:52:01 +02001424 if (c->x86 == 6 && banks > 0)
Andi Kleencebe1822009-07-09 00:31:43 +02001425 mce_banks[0].ctl = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 }
Andi Kleene5835382005-11-05 17:25:54 +01001427
Andi Kleen06b7a7a2009-04-27 18:37:43 +02001428 if (c->x86_vendor == X86_VENDOR_INTEL) {
1429 /*
1430 * SDM documents that on family 6 bank 0 should not be written
1431 * because it aliases to another special BIOS controlled
1432 * register.
1433 * But it's not aliased anymore on model 0x1a+
1434 * Don't ignore bank 0 completely because there could be a
1435 * valid event later, merely don't write CTL0.
1436 */
1437
Andi Kleencebe1822009-07-09 00:31:43 +02001438 if (c->x86 == 6 && c->x86_model < 0x1A && banks > 0)
1439 mce_banks[0].init = 0;
Andi Kleen3c079792009-05-27 21:56:55 +02001440
1441 /*
1442 * All newer Intel systems support MCE broadcasting. Enable
1443 * synchronization with a one second timeout.
1444 */
1445 if ((c->x86 > 6 || (c->x86 == 6 && c->x86_model >= 0xe)) &&
1446 monarch_timeout < 0)
1447 monarch_timeout = USEC_PER_SEC;
Bartlomiej Zolnierkiewiczc7f6fa42009-07-28 23:52:54 +02001448
Ingo Molnare412cd22009-08-17 10:19:00 +02001449 /*
1450 * There are also broken BIOSes on some Pentium M and
1451 * earlier systems:
1452 */
1453 if (c->x86 == 6 && c->x86_model <= 13 && mce_bootlog < 0)
Bartlomiej Zolnierkiewiczc7f6fa42009-07-28 23:52:54 +02001454 mce_bootlog = 0;
Andi Kleen06b7a7a2009-04-27 18:37:43 +02001455 }
Andi Kleen3c079792009-05-27 21:56:55 +02001456 if (monarch_timeout < 0)
1457 monarch_timeout = 0;
Andi Kleen29b0f592009-05-27 21:56:56 +02001458 if (mce_bootlog != 0)
1459 mce_panic_timeout = 30;
Ingo Molnare412cd22009-08-17 10:19:00 +02001460
1461 return 0;
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001462}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463
Hidetoshi Seto3a97fc32011-06-08 10:58:35 +09001464static int __cpuinit __mcheck_cpu_ancient_init(struct cpuinfo_x86 *c)
Andi Kleen4efc0672009-04-28 19:07:31 +02001465{
1466 if (c->x86 != 5)
Hidetoshi Seto3a97fc32011-06-08 10:58:35 +09001467 return 0;
1468
Andi Kleen4efc0672009-04-28 19:07:31 +02001469 switch (c->x86_vendor) {
1470 case X86_VENDOR_INTEL:
Hidetoshi Setoc6978362009-06-15 17:22:49 +09001471 intel_p5_mcheck_init(c);
Hidetoshi Seto3a97fc32011-06-08 10:58:35 +09001472 return 1;
Andi Kleen4efc0672009-04-28 19:07:31 +02001473 break;
1474 case X86_VENDOR_CENTAUR:
1475 winchip_mcheck_init(c);
Hidetoshi Seto3a97fc32011-06-08 10:58:35 +09001476 return 1;
Andi Kleen4efc0672009-04-28 19:07:31 +02001477 break;
1478 }
Hidetoshi Seto3a97fc32011-06-08 10:58:35 +09001479
1480 return 0;
Andi Kleen4efc0672009-04-28 19:07:31 +02001481}
1482
Borislav Petkov5e099542009-10-16 12:31:32 +02001483static void __mcheck_cpu_init_vendor(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484{
1485 switch (c->x86_vendor) {
1486 case X86_VENDOR_INTEL:
1487 mce_intel_feature_init(c);
1488 break;
Jacob Shin89b831e2005-11-05 17:25:53 +01001489 case X86_VENDOR_AMD:
1490 mce_amd_feature_init(c);
1491 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 default:
1493 break;
1494 }
1495}
1496
Borislav Petkov5e099542009-10-16 12:31:32 +02001497static void __mcheck_cpu_init_timer(void)
Andi Kleen52d168e2009-02-12 13:39:29 +01001498{
1499 struct timer_list *t = &__get_cpu_var(mce_timer);
Tejun Heo245b2e72009-06-24 15:13:48 +09001500 int *n = &__get_cpu_var(mce_next_interval);
Andi Kleen52d168e2009-02-12 13:39:29 +01001501
Jan Beulichbc09eff2009-12-08 11:21:37 +09001502 setup_timer(t, mce_start_timer, smp_processor_id());
1503
Hidetoshi Seto62fdac52009-06-11 16:06:07 +09001504 if (mce_ignore_ce)
1505 return;
1506
Andi Kleen6298c512009-04-09 12:28:22 +02001507 *n = check_interval * HZ;
1508 if (!*n)
Andi Kleen52d168e2009-02-12 13:39:29 +01001509 return;
Andi Kleen6298c512009-04-09 12:28:22 +02001510 t->expires = round_jiffies(jiffies + *n);
Hidetoshi Seto5be60662009-06-24 09:21:10 +09001511 add_timer_on(t, smp_processor_id());
Andi Kleen52d168e2009-02-12 13:39:29 +01001512}
1513
Andi Kleen9eda8cb2009-07-09 00:31:42 +02001514/* Handle unconfigured int18 (should never happen) */
1515static void unexpected_machine_check(struct pt_regs *regs, long error_code)
1516{
1517 printk(KERN_ERR "CPU#%d: Unexpected int18 (Machine Check).\n",
1518 smp_processor_id());
1519}
1520
1521/* Call the installed machine check handler for this CPU setup. */
1522void (*machine_check_vector)(struct pt_regs *, long error_code) =
1523 unexpected_machine_check;
1524
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001525/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 * Called for each booted CPU to set up machine checks.
Ingo Molnare9eee032009-04-08 12:31:17 +02001527 * Must be called with preempt off:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 */
Borislav Petkov5e099542009-10-16 12:31:32 +02001529void __cpuinit mcheck_cpu_init(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530{
Andi Kleen4efc0672009-04-28 19:07:31 +02001531 if (mce_disabled)
1532 return;
1533
Hidetoshi Seto3a97fc32011-06-08 10:58:35 +09001534 if (__mcheck_cpu_ancient_init(c))
1535 return;
Andi Kleen4efc0672009-04-28 19:07:31 +02001536
Andi Kleen5b4408f2009-02-12 13:39:30 +01001537 if (!mce_available(c))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 return;
1539
Borislav Petkov5e099542009-10-16 12:31:32 +02001540 if (__mcheck_cpu_cap_init() < 0 || __mcheck_cpu_apply_quirks(c) < 0) {
Andi Kleen04b2b1a2009-04-28 22:50:19 +02001541 mce_disabled = 1;
Andi Kleen0d7482e32009-02-17 23:07:13 +01001542 return;
1543 }
Andi Kleen0d7482e32009-02-17 23:07:13 +01001544
Andi Kleen5d727922009-04-27 19:25:48 +02001545 machine_check_vector = do_machine_check;
1546
Borislav Petkov5e099542009-10-16 12:31:32 +02001547 __mcheck_cpu_init_generic();
1548 __mcheck_cpu_init_vendor(c);
1549 __mcheck_cpu_init_timer();
Andi Kleen9b1beaf2009-05-27 21:56:59 +02001550 INIT_WORK(&__get_cpu_var(mce_work), mce_process_work);
Hidetoshi Setob77e70b2011-06-08 10:56:02 +09001551 init_irq_work(&__get_cpu_var(mce_irq_work), &mce_irq_work_cb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552}
1553
1554/*
Hidetoshi Seto93b62c32011-06-08 11:00:45 +09001555 * mce_chrdev: Character device /dev/mcelog to read and clear the MCE log.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 */
1557
Hidetoshi Seto93b62c32011-06-08 11:00:45 +09001558static DEFINE_SPINLOCK(mce_chrdev_state_lock);
1559static int mce_chrdev_open_count; /* #times opened */
1560static int mce_chrdev_open_exclu; /* already open exclusive? */
Tim Hockinf528e7b2007-07-21 17:10:35 +02001561
Hidetoshi Seto93b62c32011-06-08 11:00:45 +09001562static int mce_chrdev_open(struct inode *inode, struct file *file)
Tim Hockinf528e7b2007-07-21 17:10:35 +02001563{
Hidetoshi Seto93b62c32011-06-08 11:00:45 +09001564 spin_lock(&mce_chrdev_state_lock);
Tim Hockinf528e7b2007-07-21 17:10:35 +02001565
Hidetoshi Seto93b62c32011-06-08 11:00:45 +09001566 if (mce_chrdev_open_exclu ||
1567 (mce_chrdev_open_count && (file->f_flags & O_EXCL))) {
1568 spin_unlock(&mce_chrdev_state_lock);
Ingo Molnare9eee032009-04-08 12:31:17 +02001569
Tim Hockinf528e7b2007-07-21 17:10:35 +02001570 return -EBUSY;
1571 }
1572
1573 if (file->f_flags & O_EXCL)
Hidetoshi Seto93b62c32011-06-08 11:00:45 +09001574 mce_chrdev_open_exclu = 1;
1575 mce_chrdev_open_count++;
Tim Hockinf528e7b2007-07-21 17:10:35 +02001576
Hidetoshi Seto93b62c32011-06-08 11:00:45 +09001577 spin_unlock(&mce_chrdev_state_lock);
Tim Hockinf528e7b2007-07-21 17:10:35 +02001578
Tim Hockinbd784322007-07-21 17:10:37 +02001579 return nonseekable_open(inode, file);
Tim Hockinf528e7b2007-07-21 17:10:35 +02001580}
1581
Hidetoshi Seto93b62c32011-06-08 11:00:45 +09001582static int mce_chrdev_release(struct inode *inode, struct file *file)
Tim Hockinf528e7b2007-07-21 17:10:35 +02001583{
Hidetoshi Seto93b62c32011-06-08 11:00:45 +09001584 spin_lock(&mce_chrdev_state_lock);
Tim Hockinf528e7b2007-07-21 17:10:35 +02001585
Hidetoshi Seto93b62c32011-06-08 11:00:45 +09001586 mce_chrdev_open_count--;
1587 mce_chrdev_open_exclu = 0;
Tim Hockinf528e7b2007-07-21 17:10:35 +02001588
Hidetoshi Seto93b62c32011-06-08 11:00:45 +09001589 spin_unlock(&mce_chrdev_state_lock);
Tim Hockinf528e7b2007-07-21 17:10:35 +02001590
1591 return 0;
1592}
1593
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001594static void collect_tscs(void *data)
1595{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 unsigned long *cpu_tsc = (unsigned long *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001598 rdtscll(cpu_tsc[smp_processor_id()]);
1599}
1600
Huang Ying482908b2010-05-18 14:35:22 +08001601static int mce_apei_read_done;
1602
1603/* Collect MCE record of previous boot in persistent storage via APEI ERST. */
1604static int __mce_read_apei(char __user **ubuf, size_t usize)
1605{
1606 int rc;
1607 u64 record_id;
1608 struct mce m;
1609
1610 if (usize < sizeof(struct mce))
1611 return -EINVAL;
1612
1613 rc = apei_read_mce(&m, &record_id);
1614 /* Error or no more MCE record */
1615 if (rc <= 0) {
1616 mce_apei_read_done = 1;
1617 return rc;
1618 }
1619 rc = -EFAULT;
1620 if (copy_to_user(*ubuf, &m, sizeof(struct mce)))
1621 return rc;
1622 /*
1623 * In fact, we should have cleared the record after that has
1624 * been flushed to the disk or sent to network in
1625 * /sbin/mcelog, but we have no interface to support that now,
1626 * so just clear it to avoid duplication.
1627 */
1628 rc = apei_clear_mce(record_id);
1629 if (rc) {
1630 mce_apei_read_done = 1;
1631 return rc;
1632 }
1633 *ubuf += sizeof(struct mce);
1634
1635 return 0;
1636}
1637
Hidetoshi Seto93b62c32011-06-08 11:00:45 +09001638static ssize_t mce_chrdev_read(struct file *filp, char __user *ubuf,
1639 size_t usize, loff_t *off)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 char __user *buf = ubuf;
Ingo Molnare9eee032009-04-08 12:31:17 +02001642 unsigned long *cpu_tsc;
1643 unsigned prev, next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 int i, err;
1645
Mike Travis6bca67f2008-07-18 18:11:27 -07001646 cpu_tsc = kmalloc(nr_cpu_ids * sizeof(long), GFP_KERNEL);
Andi Kleenf0de53b2005-04-16 15:25:10 -07001647 if (!cpu_tsc)
1648 return -ENOMEM;
1649
Hidetoshi Seto93b62c32011-06-08 11:00:45 +09001650 mutex_lock(&mce_chrdev_read_mutex);
Huang Ying482908b2010-05-18 14:35:22 +08001651
1652 if (!mce_apei_read_done) {
1653 err = __mce_read_apei(&buf, usize);
1654 if (err || buf != ubuf)
1655 goto out;
1656 }
1657
Paul E. McKenneyf56e8a02010-03-05 15:03:27 -08001658 next = rcu_dereference_check_mce(mcelog.next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659
1660 /* Only supports full reads right now */
Huang Ying482908b2010-05-18 14:35:22 +08001661 err = -EINVAL;
1662 if (*off != 0 || usize < MCE_LOG_LEN*sizeof(struct mce))
1663 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664
1665 err = 0;
Huang Yingef41df4342009-02-12 13:39:34 +01001666 prev = 0;
1667 do {
1668 for (i = prev; i < next; i++) {
1669 unsigned long start = jiffies;
Hidetoshi Seto559faa62011-06-08 11:00:08 +09001670 struct mce *m = &mcelog.entry[i];
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001671
Hidetoshi Seto559faa62011-06-08 11:00:08 +09001672 while (!m->finished) {
Huang Yingef41df4342009-02-12 13:39:34 +01001673 if (time_after_eq(jiffies, start + 2)) {
Hidetoshi Seto559faa62011-06-08 11:00:08 +09001674 memset(m, 0, sizeof(*m));
Huang Yingef41df4342009-02-12 13:39:34 +01001675 goto timeout;
1676 }
1677 cpu_relax();
Andi Kleen673242c2005-09-12 18:49:24 +02001678 }
Huang Yingef41df4342009-02-12 13:39:34 +01001679 smp_rmb();
Hidetoshi Seto559faa62011-06-08 11:00:08 +09001680 err |= copy_to_user(buf, m, sizeof(*m));
1681 buf += sizeof(*m);
Huang Yingef41df4342009-02-12 13:39:34 +01001682timeout:
1683 ;
Andi Kleen673242c2005-09-12 18:49:24 +02001684 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685
Huang Yingef41df4342009-02-12 13:39:34 +01001686 memset(mcelog.entry + prev, 0,
1687 (next - prev) * sizeof(struct mce));
1688 prev = next;
1689 next = cmpxchg(&mcelog.next, prev, 0);
1690 } while (next != prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691
Paul E. McKenneyb2b18662005-06-25 14:55:38 -07001692 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001694 /*
1695 * Collect entries that were still getting written before the
1696 * synchronize.
1697 */
Jens Axboe15c8b6c2008-05-09 09:39:44 +02001698 on_each_cpu(collect_tscs, cpu_tsc, 1);
Ingo Molnare9eee032009-04-08 12:31:17 +02001699
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001700 for (i = next; i < MCE_LOG_LEN; i++) {
Hidetoshi Seto559faa62011-06-08 11:00:08 +09001701 struct mce *m = &mcelog.entry[i];
1702
1703 if (m->finished && m->tsc < cpu_tsc[m->cpu]) {
1704 err |= copy_to_user(buf, m, sizeof(*m));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 smp_rmb();
Hidetoshi Seto559faa62011-06-08 11:00:08 +09001706 buf += sizeof(*m);
1707 memset(m, 0, sizeof(*m));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 }
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001709 }
Huang Ying482908b2010-05-18 14:35:22 +08001710
1711 if (err)
1712 err = -EFAULT;
1713
1714out:
Hidetoshi Seto93b62c32011-06-08 11:00:45 +09001715 mutex_unlock(&mce_chrdev_read_mutex);
Andi Kleenf0de53b2005-04-16 15:25:10 -07001716 kfree(cpu_tsc);
Ingo Molnare9eee032009-04-08 12:31:17 +02001717
Huang Ying482908b2010-05-18 14:35:22 +08001718 return err ? err : buf - ubuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719}
1720
Hidetoshi Seto93b62c32011-06-08 11:00:45 +09001721static unsigned int mce_chrdev_poll(struct file *file, poll_table *wait)
Tim Hockine02e68d2007-07-21 17:10:36 +02001722{
Hidetoshi Seto93b62c32011-06-08 11:00:45 +09001723 poll_wait(file, &mce_chrdev_wait, wait);
Paul E. McKenneya4dd9922011-04-01 07:15:14 -07001724 if (rcu_access_index(mcelog.next))
Tim Hockine02e68d2007-07-21 17:10:36 +02001725 return POLLIN | POLLRDNORM;
Huang Ying482908b2010-05-18 14:35:22 +08001726 if (!mce_apei_read_done && apei_check_mce())
1727 return POLLIN | POLLRDNORM;
Tim Hockine02e68d2007-07-21 17:10:36 +02001728 return 0;
1729}
1730
Hidetoshi Seto93b62c32011-06-08 11:00:45 +09001731static long mce_chrdev_ioctl(struct file *f, unsigned int cmd,
1732 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733{
1734 int __user *p = (int __user *)arg;
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001735
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 if (!capable(CAP_SYS_ADMIN))
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001737 return -EPERM;
Ingo Molnare9eee032009-04-08 12:31:17 +02001738
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 switch (cmd) {
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001740 case MCE_GET_RECORD_LEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 return put_user(sizeof(struct mce), p);
1742 case MCE_GET_LOG_LEN:
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001743 return put_user(MCE_LOG_LEN, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 case MCE_GETCLEAR_FLAGS: {
1745 unsigned flags;
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001746
1747 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 flags = mcelog.flags;
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001749 } while (cmpxchg(&mcelog.flags, flags, 0) != flags);
Ingo Molnare9eee032009-04-08 12:31:17 +02001750
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001751 return put_user(flags, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 }
1753 default:
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001754 return -ENOTTY;
1755 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756}
1757
Luck, Tony66f5ddf2011-11-03 11:46:47 -07001758static ssize_t (*mce_write)(struct file *filp, const char __user *ubuf,
1759 size_t usize, loff_t *off);
1760
1761void register_mce_write_callback(ssize_t (*fn)(struct file *filp,
1762 const char __user *ubuf,
1763 size_t usize, loff_t *off))
1764{
1765 mce_write = fn;
1766}
1767EXPORT_SYMBOL_GPL(register_mce_write_callback);
1768
1769ssize_t mce_chrdev_write(struct file *filp, const char __user *ubuf,
1770 size_t usize, loff_t *off)
1771{
1772 if (mce_write)
1773 return mce_write(filp, ubuf, usize, off);
1774 else
1775 return -EINVAL;
1776}
1777
1778static const struct file_operations mce_chrdev_ops = {
Hidetoshi Seto93b62c32011-06-08 11:00:45 +09001779 .open = mce_chrdev_open,
1780 .release = mce_chrdev_release,
1781 .read = mce_chrdev_read,
Luck, Tony66f5ddf2011-11-03 11:46:47 -07001782 .write = mce_chrdev_write,
Hidetoshi Seto93b62c32011-06-08 11:00:45 +09001783 .poll = mce_chrdev_poll,
1784 .unlocked_ioctl = mce_chrdev_ioctl,
1785 .llseek = no_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786};
1787
Hidetoshi Seto93b62c32011-06-08 11:00:45 +09001788static struct miscdevice mce_chrdev_device = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 MISC_MCELOG_MINOR,
1790 "mcelog",
1791 &mce_chrdev_ops,
1792};
1793
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001794/*
Hidetoshi Seto62fdac52009-06-11 16:06:07 +09001795 * mce=off Disables machine check
1796 * mce=no_cmci Disables CMCI
1797 * mce=dont_log_ce Clears corrected events silently, no log created for CEs.
1798 * mce=ignore_ce Disables polling and CMCI, corrected events are not cleared.
Andi Kleen3c079792009-05-27 21:56:55 +02001799 * mce=TOLERANCELEVEL[,monarchtimeout] (number, see above)
1800 * monarchtimeout is how long to wait for other CPUs on machine
1801 * check, or 0 to not wait
Hidetoshi Seto13503fa2009-03-26 17:39:20 +09001802 * mce=bootlog Log MCEs from before booting. Disabled by default on AMD.
1803 * mce=nobootlog Don't log MCEs from before booting.
1804 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805static int __init mcheck_enable(char *str)
1806{
Bartlomiej Zolnierkiewicze3346fc2009-07-28 23:55:09 +02001807 if (*str == 0) {
Andi Kleen4efc0672009-04-28 19:07:31 +02001808 enable_p5_mce();
Bartlomiej Zolnierkiewicze3346fc2009-07-28 23:55:09 +02001809 return 1;
1810 }
Andi Kleen4efc0672009-04-28 19:07:31 +02001811 if (*str == '=')
1812 str++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 if (!strcmp(str, "off"))
Andi Kleen04b2b1a2009-04-28 22:50:19 +02001814 mce_disabled = 1;
Hidetoshi Seto62fdac52009-06-11 16:06:07 +09001815 else if (!strcmp(str, "no_cmci"))
1816 mce_cmci_disabled = 1;
1817 else if (!strcmp(str, "dont_log_ce"))
1818 mce_dont_log_ce = 1;
1819 else if (!strcmp(str, "ignore_ce"))
1820 mce_ignore_ce = 1;
Hidetoshi Seto13503fa2009-03-26 17:39:20 +09001821 else if (!strcmp(str, "bootlog") || !strcmp(str, "nobootlog"))
1822 mce_bootlog = (str[0] == 'b');
Andi Kleen3c079792009-05-27 21:56:55 +02001823 else if (isdigit(str[0])) {
Andi Kleen8c566ef2005-09-12 18:49:24 +02001824 get_option(&str, &tolerant);
Andi Kleen3c079792009-05-27 21:56:55 +02001825 if (*str == ',') {
1826 ++str;
1827 get_option(&str, &monarch_timeout);
1828 }
1829 } else {
Andi Kleen4efc0672009-04-28 19:07:31 +02001830 printk(KERN_INFO "mce argument %s ignored. Please use /sys\n",
Hidetoshi Seto13503fa2009-03-26 17:39:20 +09001831 str);
1832 return 0;
1833 }
OGAWA Hirofumi9b410462006-03-31 02:30:33 -08001834 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835}
Andi Kleen4efc0672009-04-28 19:07:31 +02001836__setup("mce", mcheck_enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837
Yong Wanga2202aa2009-11-10 09:38:24 +08001838int __init mcheck_init(void)
Borislav Petkovb33a6362009-10-16 12:31:33 +02001839{
Yong Wanga2202aa2009-11-10 09:38:24 +08001840 mcheck_intel_therm_init();
1841
Borislav Petkovb33a6362009-10-16 12:31:33 +02001842 return 0;
1843}
Borislav Petkovb33a6362009-10-16 12:31:33 +02001844
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001845/*
Hidetoshi Setoc7cece82011-06-08 11:02:03 +09001846 * mce_syscore: PM support
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001847 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848
Andi Kleen973a2dd2009-02-12 13:39:32 +01001849/*
1850 * Disable machine checks on suspend and shutdown. We can't really handle
1851 * them later.
1852 */
Borislav Petkov5e099542009-10-16 12:31:32 +02001853static int mce_disable_error_reporting(void)
Andi Kleen973a2dd2009-02-12 13:39:32 +01001854{
1855 int i;
1856
Andi Kleen06b7a7a2009-04-27 18:37:43 +02001857 for (i = 0; i < banks; i++) {
Andi Kleencebe1822009-07-09 00:31:43 +02001858 struct mce_bank *b = &mce_banks[i];
Ingo Molnar11868a22009-09-23 17:49:55 +02001859
Andi Kleencebe1822009-07-09 00:31:43 +02001860 if (b->init)
Andi Kleena2d32bc2009-07-09 00:31:44 +02001861 wrmsrl(MSR_IA32_MCx_CTL(i), 0);
Andi Kleen06b7a7a2009-04-27 18:37:43 +02001862 }
Andi Kleen973a2dd2009-02-12 13:39:32 +01001863 return 0;
1864}
1865
Hidetoshi Setoc7cece82011-06-08 11:02:03 +09001866static int mce_syscore_suspend(void)
Andi Kleen973a2dd2009-02-12 13:39:32 +01001867{
Borislav Petkov5e099542009-10-16 12:31:32 +02001868 return mce_disable_error_reporting();
Andi Kleen973a2dd2009-02-12 13:39:32 +01001869}
1870
Hidetoshi Setoc7cece82011-06-08 11:02:03 +09001871static void mce_syscore_shutdown(void)
Andi Kleen973a2dd2009-02-12 13:39:32 +01001872{
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +01001873 mce_disable_error_reporting();
Andi Kleen973a2dd2009-02-12 13:39:32 +01001874}
1875
Ingo Molnare9eee032009-04-08 12:31:17 +02001876/*
1877 * On resume clear all MCE state. Don't want to see leftovers from the BIOS.
1878 * Only one CPU is active at this time, the others get re-added later using
1879 * CPU hotplug:
1880 */
Hidetoshi Setoc7cece82011-06-08 11:02:03 +09001881static void mce_syscore_resume(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882{
Borislav Petkov5e099542009-10-16 12:31:32 +02001883 __mcheck_cpu_init_generic();
Tejun Heo7b543a52010-12-18 16:30:05 +01001884 __mcheck_cpu_init_vendor(__this_cpu_ptr(&cpu_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885}
1886
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +01001887static struct syscore_ops mce_syscore_ops = {
Hidetoshi Setoc7cece82011-06-08 11:02:03 +09001888 .suspend = mce_syscore_suspend,
1889 .shutdown = mce_syscore_shutdown,
1890 .resume = mce_syscore_resume,
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +01001891};
1892
Hidetoshi Setoc7cece82011-06-08 11:02:03 +09001893/*
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001894 * mce_device: Sysfs support
Hidetoshi Setoc7cece82011-06-08 11:02:03 +09001895 */
1896
Andi Kleen52d168e2009-02-12 13:39:29 +01001897static void mce_cpu_restart(void *data)
1898{
Tejun Heo7b543a52010-12-18 16:30:05 +01001899 if (!mce_available(__this_cpu_ptr(&cpu_info)))
Hidetoshi Seto33edbf02009-06-15 17:18:45 +09001900 return;
Borislav Petkov5e099542009-10-16 12:31:32 +02001901 __mcheck_cpu_init_generic();
1902 __mcheck_cpu_init_timer();
Andi Kleen52d168e2009-02-12 13:39:29 +01001903}
1904
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905/* Reinit MCEs after user configuration changes */
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001906static void mce_restart(void)
1907{
Hidetoshi Seto9aaef962011-06-17 04:40:36 -04001908 mce_timer_delete_all();
Andi Kleen52d168e2009-02-12 13:39:29 +01001909 on_each_cpu(mce_cpu_restart, NULL, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910}
1911
Hidetoshi Seto9af43b52009-06-15 17:21:36 +09001912/* Toggle features for corrected errors */
Hidetoshi Seto9aaef962011-06-17 04:40:36 -04001913static void mce_disable_cmci(void *data)
Hidetoshi Seto9af43b52009-06-15 17:21:36 +09001914{
Tejun Heo7b543a52010-12-18 16:30:05 +01001915 if (!mce_available(__this_cpu_ptr(&cpu_info)))
Hidetoshi Seto9af43b52009-06-15 17:21:36 +09001916 return;
Hidetoshi Seto9af43b52009-06-15 17:21:36 +09001917 cmci_clear();
1918}
1919
1920static void mce_enable_ce(void *all)
1921{
Tejun Heo7b543a52010-12-18 16:30:05 +01001922 if (!mce_available(__this_cpu_ptr(&cpu_info)))
Hidetoshi Seto9af43b52009-06-15 17:21:36 +09001923 return;
1924 cmci_reenable();
1925 cmci_recheck();
1926 if (all)
Borislav Petkov5e099542009-10-16 12:31:32 +02001927 __mcheck_cpu_init_timer();
Hidetoshi Seto9af43b52009-06-15 17:21:36 +09001928}
1929
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001930static struct bus_type mce_subsys = {
Ingo Molnare9eee032009-04-08 12:31:17 +02001931 .name = "machinecheck",
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001932 .dev_name = "machinecheck",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933};
1934
Greg Kroah-Hartmane032d8072012-01-16 14:40:28 -08001935struct device *mce_device[CONFIG_NR_CPUS];
Ingo Molnare9eee032009-04-08 12:31:17 +02001936
1937__cpuinitdata
1938void (*threshold_cpu_callback)(unsigned long action, unsigned int cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001940static inline struct mce_bank *attr_to_bank(struct device_attribute *attr)
Andi Kleencebe1822009-07-09 00:31:43 +02001941{
1942 return container_of(attr, struct mce_bank, attr);
1943}
Andi Kleen0d7482e32009-02-17 23:07:13 +01001944
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001945static ssize_t show_bank(struct device *s, struct device_attribute *attr,
Andi Kleen0d7482e32009-02-17 23:07:13 +01001946 char *buf)
1947{
Andi Kleencebe1822009-07-09 00:31:43 +02001948 return sprintf(buf, "%llx\n", attr_to_bank(attr)->ctl);
Andi Kleen0d7482e32009-02-17 23:07:13 +01001949}
1950
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001951static ssize_t set_bank(struct device *s, struct device_attribute *attr,
Hidetoshi Seto9319cec2009-04-14 17:26:30 +09001952 const char *buf, size_t size)
Andi Kleen0d7482e32009-02-17 23:07:13 +01001953{
Hidetoshi Seto9319cec2009-04-14 17:26:30 +09001954 u64 new;
Ingo Molnare9eee032009-04-08 12:31:17 +02001955
Hidetoshi Seto9319cec2009-04-14 17:26:30 +09001956 if (strict_strtoull(buf, 0, &new) < 0)
Andi Kleen0d7482e32009-02-17 23:07:13 +01001957 return -EINVAL;
Ingo Molnare9eee032009-04-08 12:31:17 +02001958
Andi Kleencebe1822009-07-09 00:31:43 +02001959 attr_to_bank(attr)->ctl = new;
Andi Kleen0d7482e32009-02-17 23:07:13 +01001960 mce_restart();
Ingo Molnare9eee032009-04-08 12:31:17 +02001961
Hidetoshi Seto9319cec2009-04-14 17:26:30 +09001962 return size;
Andi Kleen0d7482e32009-02-17 23:07:13 +01001963}
Andi Kleena98f0dd2007-02-13 13:26:23 +01001964
Ingo Molnare9eee032009-04-08 12:31:17 +02001965static ssize_t
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001966show_trigger(struct device *s, struct device_attribute *attr, char *buf)
Andi Kleena98f0dd2007-02-13 13:26:23 +01001967{
Hidetoshi Seto1020bcb2009-06-15 17:20:57 +09001968 strcpy(buf, mce_helper);
Andi Kleena98f0dd2007-02-13 13:26:23 +01001969 strcat(buf, "\n");
Hidetoshi Seto1020bcb2009-06-15 17:20:57 +09001970 return strlen(mce_helper) + 1;
Andi Kleena98f0dd2007-02-13 13:26:23 +01001971}
1972
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001973static ssize_t set_trigger(struct device *s, struct device_attribute *attr,
Ingo Molnare9eee032009-04-08 12:31:17 +02001974 const char *buf, size_t siz)
Andi Kleena98f0dd2007-02-13 13:26:23 +01001975{
1976 char *p;
Ingo Molnare9eee032009-04-08 12:31:17 +02001977
Hidetoshi Seto1020bcb2009-06-15 17:20:57 +09001978 strncpy(mce_helper, buf, sizeof(mce_helper));
1979 mce_helper[sizeof(mce_helper)-1] = 0;
Hidetoshi Seto1020bcb2009-06-15 17:20:57 +09001980 p = strchr(mce_helper, '\n');
Ingo Molnare9eee032009-04-08 12:31:17 +02001981
Jan Beuliche9084ec2009-07-16 09:45:11 +01001982 if (p)
Ingo Molnare9eee032009-04-08 12:31:17 +02001983 *p = 0;
1984
Jan Beuliche9084ec2009-07-16 09:45:11 +01001985 return strlen(mce_helper) + !!p;
Andi Kleena98f0dd2007-02-13 13:26:23 +01001986}
1987
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001988static ssize_t set_ignore_ce(struct device *s,
1989 struct device_attribute *attr,
Hidetoshi Seto9af43b52009-06-15 17:21:36 +09001990 const char *buf, size_t size)
1991{
1992 u64 new;
1993
1994 if (strict_strtoull(buf, 0, &new) < 0)
1995 return -EINVAL;
1996
1997 if (mce_ignore_ce ^ !!new) {
1998 if (new) {
1999 /* disable ce features */
Hidetoshi Seto9aaef962011-06-17 04:40:36 -04002000 mce_timer_delete_all();
2001 on_each_cpu(mce_disable_cmci, NULL, 1);
Hidetoshi Seto9af43b52009-06-15 17:21:36 +09002002 mce_ignore_ce = 1;
2003 } else {
2004 /* enable ce features */
2005 mce_ignore_ce = 0;
2006 on_each_cpu(mce_enable_ce, (void *)1, 1);
2007 }
2008 }
2009 return size;
2010}
2011
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002012static ssize_t set_cmci_disabled(struct device *s,
2013 struct device_attribute *attr,
Hidetoshi Seto9af43b52009-06-15 17:21:36 +09002014 const char *buf, size_t size)
2015{
2016 u64 new;
2017
2018 if (strict_strtoull(buf, 0, &new) < 0)
2019 return -EINVAL;
2020
2021 if (mce_cmci_disabled ^ !!new) {
2022 if (new) {
2023 /* disable cmci */
Hidetoshi Seto9aaef962011-06-17 04:40:36 -04002024 on_each_cpu(mce_disable_cmci, NULL, 1);
Hidetoshi Seto9af43b52009-06-15 17:21:36 +09002025 mce_cmci_disabled = 1;
2026 } else {
2027 /* enable cmci */
2028 mce_cmci_disabled = 0;
2029 on_each_cpu(mce_enable_ce, NULL, 1);
2030 }
2031 }
2032 return size;
2033}
2034
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002035static ssize_t store_int_with_restart(struct device *s,
2036 struct device_attribute *attr,
Andi Kleenb56f6422009-05-27 21:56:52 +02002037 const char *buf, size_t size)
2038{
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002039 ssize_t ret = device_store_int(s, attr, buf, size);
Andi Kleenb56f6422009-05-27 21:56:52 +02002040 mce_restart();
2041 return ret;
2042}
2043
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002044static DEVICE_ATTR(trigger, 0644, show_trigger, set_trigger);
2045static DEVICE_INT_ATTR(tolerant, 0644, tolerant);
2046static DEVICE_INT_ATTR(monarch_timeout, 0644, monarch_timeout);
2047static DEVICE_INT_ATTR(dont_log_ce, 0644, mce_dont_log_ce);
Ingo Molnare9eee032009-04-08 12:31:17 +02002048
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002049static struct dev_ext_attribute dev_attr_check_interval = {
2050 __ATTR(check_interval, 0644, device_show_int, store_int_with_restart),
Andi Kleenb56f6422009-05-27 21:56:52 +02002051 &check_interval
2052};
Ingo Molnare9eee032009-04-08 12:31:17 +02002053
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002054static struct dev_ext_attribute dev_attr_ignore_ce = {
2055 __ATTR(ignore_ce, 0644, device_show_int, set_ignore_ce),
Hidetoshi Seto9af43b52009-06-15 17:21:36 +09002056 &mce_ignore_ce
2057};
2058
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002059static struct dev_ext_attribute dev_attr_cmci_disabled = {
2060 __ATTR(cmci_disabled, 0644, device_show_int, set_cmci_disabled),
Hidetoshi Seto9af43b52009-06-15 17:21:36 +09002061 &mce_cmci_disabled
2062};
2063
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002064static struct device_attribute *mce_device_attrs[] = {
2065 &dev_attr_tolerant.attr,
2066 &dev_attr_check_interval.attr,
2067 &dev_attr_trigger,
2068 &dev_attr_monarch_timeout.attr,
2069 &dev_attr_dont_log_ce.attr,
2070 &dev_attr_ignore_ce.attr,
2071 &dev_attr_cmci_disabled.attr,
Andi Kleena98f0dd2007-02-13 13:26:23 +01002072 NULL
2073};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002075static cpumask_var_t mce_device_initialized;
Andreas Herrmannbae19fe2007-11-14 17:00:44 -08002076
Greg Kroah-Hartmane032d8072012-01-16 14:40:28 -08002077static void mce_device_release(struct device *dev)
2078{
2079 kfree(dev);
2080}
2081
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002082/* Per cpu device init. All of the cpus still share the same ctrl bank: */
2083static __cpuinit int mce_device_create(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084{
Greg Kroah-Hartmane032d8072012-01-16 14:40:28 -08002085 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 int err;
Hidetoshi Setob1f49f92009-06-18 14:53:24 +09002087 int i, j;
Mike Travis92cb7612007-10-19 20:35:04 +02002088
Andreas Herrmann90367552007-11-07 02:12:58 +01002089 if (!mce_available(&boot_cpu_data))
Andi Kleen91c6d402005-07-28 21:15:39 -07002090 return -EIO;
2091
Greg Kroah-Hartmane032d8072012-01-16 14:40:28 -08002092 dev = kzalloc(sizeof *dev, GFP_KERNEL);
2093 if (!dev)
2094 return -ENOMEM;
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002095 dev->id = cpu;
2096 dev->bus = &mce_subsys;
Greg Kroah-Hartmane032d8072012-01-16 14:40:28 -08002097 dev->release = &mce_device_release;
Andi Kleen91c6d402005-07-28 21:15:39 -07002098
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002099 err = device_register(dev);
Akinobu Mitad435d862007-10-18 03:05:15 -07002100 if (err)
2101 return err;
Andi Kleen91c6d402005-07-28 21:15:39 -07002102
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002103 for (i = 0; mce_device_attrs[i]; i++) {
2104 err = device_create_file(dev, mce_device_attrs[i]);
Akinobu Mitad435d862007-10-18 03:05:15 -07002105 if (err)
2106 goto error;
Andi Kleen91c6d402005-07-28 21:15:39 -07002107 }
Hidetoshi Setob1f49f92009-06-18 14:53:24 +09002108 for (j = 0; j < banks; j++) {
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002109 err = device_create_file(dev, &mce_banks[j].attr);
Andi Kleen0d7482e32009-02-17 23:07:13 +01002110 if (err)
2111 goto error2;
2112 }
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002113 cpumask_set_cpu(cpu, mce_device_initialized);
Greg Kroah-Hartmane032d8072012-01-16 14:40:28 -08002114 mce_device[cpu] = dev;
Akinobu Mitad435d862007-10-18 03:05:15 -07002115
2116 return 0;
Andi Kleen0d7482e32009-02-17 23:07:13 +01002117error2:
Hidetoshi Setob1f49f92009-06-18 14:53:24 +09002118 while (--j >= 0)
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002119 device_remove_file(dev, &mce_banks[j].attr);
Akinobu Mitad435d862007-10-18 03:05:15 -07002120error:
Ingo Molnarcb491fc2009-04-08 12:31:17 +02002121 while (--i >= 0)
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002122 device_remove_file(dev, mce_device_attrs[i]);
Ingo Molnarcb491fc2009-04-08 12:31:17 +02002123
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002124 device_unregister(dev);
Akinobu Mitad435d862007-10-18 03:05:15 -07002125
Andi Kleen91c6d402005-07-28 21:15:39 -07002126 return err;
2127}
2128
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002129static __cpuinit void mce_device_remove(unsigned int cpu)
Andi Kleen91c6d402005-07-28 21:15:39 -07002130{
Greg Kroah-Hartmane032d8072012-01-16 14:40:28 -08002131 struct device *dev = mce_device[cpu];
Shaohua Li73ca5352006-01-11 22:43:06 +01002132 int i;
2133
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002134 if (!cpumask_test_cpu(cpu, mce_device_initialized))
Andreas Herrmannbae19fe2007-11-14 17:00:44 -08002135 return;
2136
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002137 for (i = 0; mce_device_attrs[i]; i++)
2138 device_remove_file(dev, mce_device_attrs[i]);
Ingo Molnarcb491fc2009-04-08 12:31:17 +02002139
Andi Kleen0d7482e32009-02-17 23:07:13 +01002140 for (i = 0; i < banks; i++)
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002141 device_remove_file(dev, &mce_banks[i].attr);
Ingo Molnarcb491fc2009-04-08 12:31:17 +02002142
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002143 device_unregister(dev);
2144 cpumask_clear_cpu(cpu, mce_device_initialized);
Greg Kroah-Hartmane032d8072012-01-16 14:40:28 -08002145 mce_device[cpu] = NULL;
Andi Kleen91c6d402005-07-28 21:15:39 -07002146}
Andi Kleen91c6d402005-07-28 21:15:39 -07002147
Andi Kleend6b75582009-02-12 13:39:31 +01002148/* Make sure there are no machine checks on offlined CPUs. */
Hidetoshi Seto767df1b2009-11-26 17:29:02 +09002149static void __cpuinit mce_disable_cpu(void *h)
Andi Kleend6b75582009-02-12 13:39:31 +01002150{
Andi Kleen88ccbed2009-02-12 13:49:36 +01002151 unsigned long action = *(unsigned long *)h;
Ingo Molnarcb491fc2009-04-08 12:31:17 +02002152 int i;
Andi Kleend6b75582009-02-12 13:39:31 +01002153
Tejun Heo7b543a52010-12-18 16:30:05 +01002154 if (!mce_available(__this_cpu_ptr(&cpu_info)))
Andi Kleend6b75582009-02-12 13:39:31 +01002155 return;
Hidetoshi Seto767df1b2009-11-26 17:29:02 +09002156
Andi Kleen88ccbed2009-02-12 13:49:36 +01002157 if (!(action & CPU_TASKS_FROZEN))
2158 cmci_clear();
Andi Kleen06b7a7a2009-04-27 18:37:43 +02002159 for (i = 0; i < banks; i++) {
Andi Kleencebe1822009-07-09 00:31:43 +02002160 struct mce_bank *b = &mce_banks[i];
Ingo Molnar11868a22009-09-23 17:49:55 +02002161
Andi Kleencebe1822009-07-09 00:31:43 +02002162 if (b->init)
Andi Kleena2d32bc2009-07-09 00:31:44 +02002163 wrmsrl(MSR_IA32_MCx_CTL(i), 0);
Andi Kleen06b7a7a2009-04-27 18:37:43 +02002164 }
Andi Kleend6b75582009-02-12 13:39:31 +01002165}
2166
Hidetoshi Seto767df1b2009-11-26 17:29:02 +09002167static void __cpuinit mce_reenable_cpu(void *h)
Andi Kleend6b75582009-02-12 13:39:31 +01002168{
Andi Kleen88ccbed2009-02-12 13:49:36 +01002169 unsigned long action = *(unsigned long *)h;
Ingo Molnare9eee032009-04-08 12:31:17 +02002170 int i;
Andi Kleend6b75582009-02-12 13:39:31 +01002171
Tejun Heo7b543a52010-12-18 16:30:05 +01002172 if (!mce_available(__this_cpu_ptr(&cpu_info)))
Andi Kleend6b75582009-02-12 13:39:31 +01002173 return;
Ingo Molnare9eee032009-04-08 12:31:17 +02002174
Andi Kleen88ccbed2009-02-12 13:49:36 +01002175 if (!(action & CPU_TASKS_FROZEN))
2176 cmci_reenable();
Andi Kleen06b7a7a2009-04-27 18:37:43 +02002177 for (i = 0; i < banks; i++) {
Andi Kleencebe1822009-07-09 00:31:43 +02002178 struct mce_bank *b = &mce_banks[i];
Ingo Molnar11868a22009-09-23 17:49:55 +02002179
Andi Kleencebe1822009-07-09 00:31:43 +02002180 if (b->init)
Andi Kleena2d32bc2009-07-09 00:31:44 +02002181 wrmsrl(MSR_IA32_MCx_CTL(i), b->ctl);
Andi Kleen06b7a7a2009-04-27 18:37:43 +02002182 }
Andi Kleend6b75582009-02-12 13:39:31 +01002183}
2184
Andi Kleen91c6d402005-07-28 21:15:39 -07002185/* Get notified when a cpu comes on/off. Be hotplug friendly. */
Ingo Molnare9eee032009-04-08 12:31:17 +02002186static int __cpuinit
2187mce_cpu_callback(struct notifier_block *nfb, unsigned long action, void *hcpu)
Andi Kleen91c6d402005-07-28 21:15:39 -07002188{
2189 unsigned int cpu = (unsigned long)hcpu;
Andi Kleen52d168e2009-02-12 13:39:29 +01002190 struct timer_list *t = &per_cpu(mce_timer, cpu);
Andi Kleen91c6d402005-07-28 21:15:39 -07002191
2192 switch (action) {
Andreas Herrmannbae19fe2007-11-14 17:00:44 -08002193 case CPU_ONLINE:
2194 case CPU_ONLINE_FROZEN:
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002195 mce_device_create(cpu);
Rafael J. Wysocki87357282008-08-22 22:23:09 +02002196 if (threshold_cpu_callback)
2197 threshold_cpu_callback(action, cpu);
Andi Kleen91c6d402005-07-28 21:15:39 -07002198 break;
Andi Kleen91c6d402005-07-28 21:15:39 -07002199 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07002200 case CPU_DEAD_FROZEN:
Rafael J. Wysocki87357282008-08-22 22:23:09 +02002201 if (threshold_cpu_callback)
2202 threshold_cpu_callback(action, cpu);
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002203 mce_device_remove(cpu);
Andi Kleen91c6d402005-07-28 21:15:39 -07002204 break;
Andi Kleen52d168e2009-02-12 13:39:29 +01002205 case CPU_DOWN_PREPARE:
2206 case CPU_DOWN_PREPARE_FROZEN:
2207 del_timer_sync(t);
Andi Kleen88ccbed2009-02-12 13:49:36 +01002208 smp_call_function_single(cpu, mce_disable_cpu, &action, 1);
Andi Kleen52d168e2009-02-12 13:39:29 +01002209 break;
2210 case CPU_DOWN_FAILED:
2211 case CPU_DOWN_FAILED_FROZEN:
Hidetoshi Setofe5ed912009-12-03 11:33:08 +09002212 if (!mce_ignore_ce && check_interval) {
2213 t->expires = round_jiffies(jiffies +
Tejun Heo245b2e72009-06-24 15:13:48 +09002214 __get_cpu_var(mce_next_interval));
Hidetoshi Setofe5ed912009-12-03 11:33:08 +09002215 add_timer_on(t, cpu);
2216 }
Andi Kleen88ccbed2009-02-12 13:49:36 +01002217 smp_call_function_single(cpu, mce_reenable_cpu, &action, 1);
2218 break;
2219 case CPU_POST_DEAD:
2220 /* intentionally ignoring frozen here */
2221 cmci_rediscover(cpu);
Andi Kleen52d168e2009-02-12 13:39:29 +01002222 break;
Andi Kleen91c6d402005-07-28 21:15:39 -07002223 }
Andreas Herrmannbae19fe2007-11-14 17:00:44 -08002224 return NOTIFY_OK;
Andi Kleen91c6d402005-07-28 21:15:39 -07002225}
2226
Sam Ravnborg1e356692008-01-30 13:33:36 +01002227static struct notifier_block mce_cpu_notifier __cpuinitdata = {
Andi Kleen91c6d402005-07-28 21:15:39 -07002228 .notifier_call = mce_cpu_callback,
2229};
2230
Andi Kleencebe1822009-07-09 00:31:43 +02002231static __init void mce_init_banks(void)
Andi Kleen0d7482e32009-02-17 23:07:13 +01002232{
2233 int i;
2234
Andi Kleen0d7482e32009-02-17 23:07:13 +01002235 for (i = 0; i < banks; i++) {
Andi Kleencebe1822009-07-09 00:31:43 +02002236 struct mce_bank *b = &mce_banks[i];
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002237 struct device_attribute *a = &b->attr;
Ingo Molnare9eee032009-04-08 12:31:17 +02002238
Eric W. Biedermana07e4152010-02-11 15:23:05 -08002239 sysfs_attr_init(&a->attr);
Andi Kleencebe1822009-07-09 00:31:43 +02002240 a->attr.name = b->attrname;
2241 snprintf(b->attrname, ATTR_LEN, "bank%d", i);
Ingo Molnare9eee032009-04-08 12:31:17 +02002242
2243 a->attr.mode = 0644;
2244 a->show = show_bank;
2245 a->store = set_bank;
Andi Kleen0d7482e32009-02-17 23:07:13 +01002246 }
Andi Kleen0d7482e32009-02-17 23:07:13 +01002247}
2248
Borislav Petkov5e099542009-10-16 12:31:32 +02002249static __init int mcheck_init_device(void)
Andi Kleen91c6d402005-07-28 21:15:39 -07002250{
2251 int err;
2252 int i = 0;
2253
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 if (!mce_available(&boot_cpu_data))
2255 return -EIO;
Andi Kleen0d7482e32009-02-17 23:07:13 +01002256
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002257 zalloc_cpumask_var(&mce_device_initialized, GFP_KERNEL);
Rusty Russell996867d2009-03-13 14:49:51 +10302258
Andi Kleencebe1822009-07-09 00:31:43 +02002259 mce_init_banks();
Andi Kleen0d7482e32009-02-17 23:07:13 +01002260
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002261 err = subsys_system_register(&mce_subsys, NULL);
Akinobu Mitad435d862007-10-18 03:05:15 -07002262 if (err)
2263 return err;
Andi Kleen91c6d402005-07-28 21:15:39 -07002264
2265 for_each_online_cpu(i) {
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002266 err = mce_device_create(i);
Akinobu Mitad435d862007-10-18 03:05:15 -07002267 if (err)
2268 return err;
Andi Kleen91c6d402005-07-28 21:15:39 -07002269 }
2270
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +01002271 register_syscore_ops(&mce_syscore_ops);
Chandra Seetharamanbe6b5a32006-07-30 03:03:37 -07002272 register_hotcpu_notifier(&mce_cpu_notifier);
Hidetoshi Seto93b62c32011-06-08 11:00:45 +09002273
2274 /* register character device /dev/mcelog */
2275 misc_register(&mce_chrdev_device);
Ingo Molnare9eee032009-04-08 12:31:17 +02002276
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278}
Borislav Petkov5e099542009-10-16 12:31:32 +02002279device_initcall(mcheck_init_device);
Ingo Molnara988d332009-04-08 12:31:25 +02002280
Andi Kleend7c3c9a2009-04-28 23:07:25 +02002281/*
2282 * Old style boot options parsing. Only for compatibility.
2283 */
2284static int __init mcheck_disable(char *str)
2285{
2286 mce_disabled = 1;
2287 return 1;
2288}
2289__setup("nomce", mcheck_disable);
Huang Ying5be9ed22009-07-31 09:41:42 +08002290
2291#ifdef CONFIG_DEBUG_FS
2292struct dentry *mce_get_debugfs_dir(void)
2293{
2294 static struct dentry *dmce;
2295
2296 if (!dmce)
2297 dmce = debugfs_create_dir("mce", NULL);
2298
2299 return dmce;
2300}
Huang Yingbf783f92009-07-31 09:41:43 +08002301
2302static void mce_reset(void)
2303{
2304 cpu_missing = 0;
2305 atomic_set(&mce_fake_paniced, 0);
2306 atomic_set(&mce_executing, 0);
2307 atomic_set(&mce_callin, 0);
2308 atomic_set(&global_nwo, 0);
2309}
2310
2311static int fake_panic_get(void *data, u64 *val)
2312{
2313 *val = fake_panic;
2314 return 0;
2315}
2316
2317static int fake_panic_set(void *data, u64 val)
2318{
2319 mce_reset();
2320 fake_panic = val;
2321 return 0;
2322}
2323
2324DEFINE_SIMPLE_ATTRIBUTE(fake_panic_fops, fake_panic_get,
2325 fake_panic_set, "%llu\n");
2326
Borislav Petkov5e099542009-10-16 12:31:32 +02002327static int __init mcheck_debugfs_init(void)
Huang Yingbf783f92009-07-31 09:41:43 +08002328{
2329 struct dentry *dmce, *ffake_panic;
2330
2331 dmce = mce_get_debugfs_dir();
2332 if (!dmce)
2333 return -ENOMEM;
2334 ffake_panic = debugfs_create_file("fake_panic", 0444, dmce, NULL,
2335 &fake_panic_fops);
2336 if (!ffake_panic)
2337 return -ENOMEM;
2338
2339 return 0;
2340}
Borislav Petkov5e099542009-10-16 12:31:32 +02002341late_initcall(mcheck_debugfs_init);
Huang Ying5be9ed22009-07-31 09:41:42 +08002342#endif