blob: 9473e8772fd19bba99ac568a2d7afda3af86aa6f [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 */
Joe Perchesc767a542012-05-21 19:50:07 -070010
11#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12
Tim Hockine02e68d2007-07-21 17:10:36 +020013#include <linux/thread_info.h>
Ingo Molnare9eee032009-04-08 12:31:17 +020014#include <linux/capability.h>
15#include <linux/miscdevice.h>
Andi Kleen8457c842009-02-12 13:49:33 +010016#include <linux/ratelimit.h>
Ingo Molnare9eee032009-04-08 12:31:17 +020017#include <linux/kallsyms.h>
18#include <linux/rcupdate.h>
Ingo Molnare9eee032009-04-08 12:31:17 +020019#include <linux/kobject.h>
Hidetoshi Seto14a02532009-04-30 16:04:51 +090020#include <linux/uaccess.h>
Ingo Molnare9eee032009-04-08 12:31:17 +020021#include <linux/kdebug.h>
22#include <linux/kernel.h>
23#include <linux/percpu.h>
24#include <linux/string.h>
Kay Sievers8a25a2f2011-12-21 14:29:42 -080025#include <linux/device.h>
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +010026#include <linux/syscore_ops.h>
Andi Kleen3c079792009-05-27 21:56:55 +020027#include <linux/delay.h>
Ingo Molnare9eee032009-04-08 12:31:17 +020028#include <linux/ctype.h>
29#include <linux/sched.h>
30#include <linux/sysfs.h>
31#include <linux/types.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
Ingo Molnare9eee032009-04-08 12:31:17 +020033#include <linux/init.h>
34#include <linux/kmod.h>
35#include <linux/poll.h>
Andi Kleen3c079792009-05-27 21:56:55 +020036#include <linux/nmi.h>
Ingo Molnare9eee032009-04-08 12:31:17 +020037#include <linux/cpu.h>
Hidetoshi Seto14a02532009-04-30 16:04:51 +090038#include <linux/smp.h>
Ingo Molnare9eee032009-04-08 12:31:17 +020039#include <linux/fs.h>
Andi Kleen9b1beaf2009-05-27 21:56:59 +020040#include <linux/mm.h>
Huang Ying5be9ed22009-07-31 09:41:42 +080041#include <linux/debugfs.h>
Hidetoshi Setob77e70b2011-06-08 10:56:02 +090042#include <linux/irq_work.h>
Paul Gortmaker69c60c82011-05-26 12:22:53 -040043#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Ingo Molnare9eee032009-04-08 12:31:17 +020045#include <asm/processor.h>
Ingo Molnare9eee032009-04-08 12:31:17 +020046#include <asm/mce.h>
47#include <asm/msr.h>
Ingo Molnare9eee032009-04-08 12:31:17 +020048
Andi Kleenbd19a5e2009-05-27 21:56:55 +020049#include "mce-internal.h"
Ingo Molnar711c2e42009-04-08 12:31:26 +020050
Hidetoshi Seto93b62c32011-06-08 11:00:45 +090051static DEFINE_MUTEX(mce_chrdev_read_mutex);
Ingo Molnar2aa2b50dd2010-03-14 08:57:03 +010052
Paul E. McKenneyf56e8a02010-03-05 15:03:27 -080053#define rcu_dereference_check_mce(p) \
Paul E. McKenneyec8c27e2010-04-30 06:45:36 -070054 rcu_dereference_index_check((p), \
Paul E. McKenneyf56e8a02010-03-05 15:03:27 -080055 rcu_read_lock_sched_held() || \
Hidetoshi Seto93b62c32011-06-08 11:00:45 +090056 lockdep_is_held(&mce_chrdev_read_mutex))
Paul E. McKenneyf56e8a02010-03-05 15:03:27 -080057
Hidetoshi Seto8968f9d2009-10-13 16:19:41 +090058#define CREATE_TRACE_POINTS
59#include <trace/events/mce.h>
60
Hidetoshi Seto4e5b3e62009-06-15 17:20:20 +090061int mce_disabled __read_mostly;
Andi Kleen04b2b1a2009-04-28 22:50:19 +020062
Ingo Molnare9eee032009-04-08 12:31:17 +020063#define MISC_MCELOG_MINOR 227
Andi Kleen0d7482e32009-02-17 23:07:13 +010064
Andi Kleen3c079792009-05-27 21:56:55 +020065#define SPINUNIT 100 /* 100ns */
66
Andi Kleen553f2652006-04-07 19:49:57 +020067atomic_t mce_entry;
68
Andi Kleen01ca79f2009-05-27 21:56:52 +020069DEFINE_PER_CPU(unsigned, mce_exception_count);
70
Tim Hockinbd784322007-07-21 17:10:37 +020071/*
72 * Tolerant levels:
73 * 0: always panic on uncorrected errors, log corrected errors
74 * 1: panic or SIGBUS on uncorrected errors, log corrected errors
75 * 2: SIGBUS or log uncorrected errors (if possible), log corrected errors
76 * 3: never panic or SIGBUS, log all errors (for testing only)
77 */
Hidetoshi Seto4e5b3e62009-06-15 17:20:20 +090078static int tolerant __read_mostly = 1;
79static int banks __read_mostly;
Hidetoshi Seto4e5b3e62009-06-15 17:20:20 +090080static int rip_msr __read_mostly;
81static int mce_bootlog __read_mostly = -1;
82static int monarch_timeout __read_mostly = -1;
83static int mce_panic_timeout __read_mostly;
84static int mce_dont_log_ce __read_mostly;
85int mce_cmci_disabled __read_mostly;
86int mce_ignore_ce __read_mostly;
87int mce_ser __read_mostly;
Andi Kleena98f0dd2007-02-13 13:26:23 +010088
Andi Kleencebe1822009-07-09 00:31:43 +020089struct mce_bank *mce_banks __read_mostly;
90
Hidetoshi Seto1020bcb2009-06-15 17:20:57 +090091/* User mode helper program triggered by machine check event */
92static unsigned long mce_need_notify;
93static char mce_helper[128];
94static char *mce_helper_argv[2] = { mce_helper, NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Hidetoshi Seto93b62c32011-06-08 11:00:45 +090096static DECLARE_WAIT_QUEUE_HEAD(mce_chrdev_wait);
97
Andi Kleen3c079792009-05-27 21:56:55 +020098static DEFINE_PER_CPU(struct mce, mces_seen);
99static int cpu_missing;
100
Andi Kleenee031c32009-02-12 13:49:34 +0100101/* MCA banks polled by the period polling timer for corrected events */
102DEFINE_PER_CPU(mce_banks_t, mce_poll_banks) = {
103 [0 ... BITS_TO_LONGS(MAX_NR_BANKS)-1] = ~0UL
104};
105
Andi Kleen9b1beaf2009-05-27 21:56:59 +0200106static DEFINE_PER_CPU(struct work_struct, mce_work);
107
Borislav Petkov3653ada2011-12-04 15:12:09 +0100108/*
109 * CPU/chipset specific EDAC code can register a notifier call here to print
110 * MCE errors in a human-readable form.
111 */
112ATOMIC_NOTIFIER_HEAD(x86_mce_decoder_chain);
113
Andi Kleenb5f2fa42009-02-12 13:43:22 +0100114/* Do initial initialization of a struct mce */
115void mce_setup(struct mce *m)
116{
117 memset(m, 0, sizeof(struct mce));
Andi Kleend620c672009-05-27 21:56:56 +0200118 m->cpu = m->extcpu = smp_processor_id();
Andi Kleenb5f2fa42009-02-12 13:43:22 +0100119 rdtscll(m->tsc);
Andi Kleen8ee08342009-05-27 21:56:56 +0200120 /* We hope get_seconds stays lockless */
121 m->time = get_seconds();
122 m->cpuvendor = boot_cpu_data.x86_vendor;
123 m->cpuid = cpuid_eax(1);
Andi Kleen8ee08342009-05-27 21:56:56 +0200124 m->socketid = cpu_data(m->extcpu).phys_proc_id;
Andi Kleen8ee08342009-05-27 21:56:56 +0200125 m->apicid = cpu_data(m->extcpu).initial_apicid;
126 rdmsrl(MSR_IA32_MCG_CAP, m->mcgcap);
Andi Kleenb5f2fa42009-02-12 13:43:22 +0100127}
128
Andi Kleenea149b32009-04-29 19:31:00 +0200129DEFINE_PER_CPU(struct mce, injectm);
130EXPORT_PER_CPU_SYMBOL_GPL(injectm);
131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132/*
133 * Lockless MCE logging infrastructure.
134 * This avoids deadlocks on printk locks without having to break locks. Also
135 * separate MCEs from kernel messages to avoid bogus bug reports.
136 */
137
Adrian Bunk231fd902008-01-30 13:30:30 +0100138static struct mce_log mcelog = {
Andi Kleenf6fb0ac2009-05-27 21:56:55 +0200139 .signature = MCE_LOG_SIGNATURE,
140 .len = MCE_LOG_LEN,
141 .recordlen = sizeof(struct mce),
Thomas Gleixnerd88203d2007-10-23 22:37:23 +0200142};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
144void mce_log(struct mce *mce)
145{
146 unsigned next, entry;
Borislav Petkovf0cb5452011-07-18 11:24:45 -0300147 int ret = 0;
Ingo Molnare9eee032009-04-08 12:31:17 +0200148
Hidetoshi Seto8968f9d2009-10-13 16:19:41 +0900149 /* Emit the trace record: */
150 trace_mce_record(mce);
151
Borislav Petkovf0cb5452011-07-18 11:24:45 -0300152 ret = atomic_notifier_call_chain(&x86_mce_decoder_chain, 0, mce);
153 if (ret == NOTIFY_STOP)
154 return;
155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 mce->finished = 0;
Mike Waychison76441432005-09-30 00:01:27 +0200157 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 for (;;) {
Paul E. McKenneyf56e8a02010-03-05 15:03:27 -0800159 entry = rcu_dereference_check_mce(mcelog.next);
Andi Kleen673242c2005-09-12 18:49:24 +0200160 for (;;) {
Mauro Carvalho Chehab696e4092009-07-23 06:57:45 -0300161
162 /*
Ingo Molnare9eee032009-04-08 12:31:17 +0200163 * When the buffer fills up discard new entries.
164 * Assume that the earlier errors are the more
165 * interesting ones:
166 */
Andi Kleen673242c2005-09-12 18:49:24 +0200167 if (entry >= MCE_LOG_LEN) {
Hidetoshi Seto14a02532009-04-30 16:04:51 +0900168 set_bit(MCE_OVERFLOW,
169 (unsigned long *)&mcelog.flags);
Andi Kleen673242c2005-09-12 18:49:24 +0200170 return;
171 }
Ingo Molnare9eee032009-04-08 12:31:17 +0200172 /* Old left over entry. Skip: */
Andi Kleen673242c2005-09-12 18:49:24 +0200173 if (mcelog.entry[entry].finished) {
174 entry++;
175 continue;
176 }
Mike Waychison76441432005-09-30 00:01:27 +0200177 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 smp_rmb();
180 next = entry + 1;
181 if (cmpxchg(&mcelog.next, entry, next) == entry)
182 break;
183 }
184 memcpy(mcelog.entry + entry, mce, sizeof(struct mce));
Mike Waychison76441432005-09-30 00:01:27 +0200185 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 mcelog.entry[entry].finished = 1;
Mike Waychison76441432005-09-30 00:01:27 +0200187 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
Andi Kleena0189c72009-05-27 21:56:54 +0200189 mce->finished = 1;
Hidetoshi Seto1020bcb2009-06-15 17:20:57 +0900190 set_bit(0, &mce_need_notify);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191}
192
Borislav Petkov09371952011-12-08 12:28:33 +0100193static void drain_mcelog_buffer(void)
194{
195 unsigned int next, i, prev = 0;
196
Srivatsa S. Bhatb11e3d72012-03-07 11:44:29 +0100197 next = ACCESS_ONCE(mcelog.next);
Borislav Petkov09371952011-12-08 12:28:33 +0100198
199 do {
200 struct mce *m;
201
202 /* drain what was logged during boot */
203 for (i = prev; i < next; i++) {
204 unsigned long start = jiffies;
205 unsigned retries = 1;
206
207 m = &mcelog.entry[i];
208
209 while (!m->finished) {
210 if (time_after_eq(jiffies, start + 2*retries))
211 retries++;
212
213 cpu_relax();
214
215 if (!m->finished && retries >= 4) {
Joe Perchesc767a542012-05-21 19:50:07 -0700216 pr_err("skipping error being logged currently!\n");
Borislav Petkov09371952011-12-08 12:28:33 +0100217 break;
218 }
219 }
220 smp_rmb();
221 atomic_notifier_call_chain(&x86_mce_decoder_chain, 0, m);
222 }
223
224 memset(mcelog.entry + prev, 0, (next - prev) * sizeof(*m));
225 prev = next;
226 next = cmpxchg(&mcelog.next, prev, 0);
227 } while (next != prev);
228}
229
230
Borislav Petkov3653ada2011-12-04 15:12:09 +0100231void mce_register_decode_chain(struct notifier_block *nb)
232{
233 atomic_notifier_chain_register(&x86_mce_decoder_chain, nb);
Borislav Petkov09371952011-12-08 12:28:33 +0100234 drain_mcelog_buffer();
Borislav Petkov3653ada2011-12-04 15:12:09 +0100235}
236EXPORT_SYMBOL_GPL(mce_register_decode_chain);
237
238void mce_unregister_decode_chain(struct notifier_block *nb)
239{
240 atomic_notifier_chain_unregister(&x86_mce_decoder_chain, nb);
241}
242EXPORT_SYMBOL_GPL(mce_unregister_decode_chain);
243
Hidetoshi Seto77e26cc2009-06-11 16:04:35 +0900244static void print_mce(struct mce *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245{
Borislav Petkovdffa4b22011-04-20 12:23:49 +0200246 int ret = 0;
247
Huang Yinga2d7b0d2010-06-08 14:35:39 +0800248 pr_emerg(HW_ERR "CPU %d: Machine Check Exception: %Lx Bank %d: %016Lx\n",
Andi Kleend620c672009-05-27 21:56:56 +0200249 m->extcpu, m->mcgstatus, m->bank, m->status);
Ingo Molnarf436f8b2009-10-01 16:14:32 +0200250
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100251 if (m->ip) {
Huang Yinga2d7b0d2010-06-08 14:35:39 +0800252 pr_emerg(HW_ERR "RIP%s %02x:<%016Lx> ",
Ingo Molnarf436f8b2009-10-01 16:14:32 +0200253 !(m->mcgstatus & MCG_STATUS_EIPV) ? " !INEXACT!" : "",
254 m->cs, m->ip);
255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 if (m->cs == __KERNEL_CS)
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100257 print_symbol("{%s}", m->ip);
Ingo Molnarf436f8b2009-10-01 16:14:32 +0200258 pr_cont("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 }
Borislav Petkov549d0422009-07-24 13:51:42 +0200260
Huang Yinga2d7b0d2010-06-08 14:35:39 +0800261 pr_emerg(HW_ERR "TSC %llx ", m->tsc);
Ingo Molnarf436f8b2009-10-01 16:14:32 +0200262 if (m->addr)
263 pr_cont("ADDR %llx ", m->addr);
264 if (m->misc)
265 pr_cont("MISC %llx ", m->misc);
266
267 pr_cont("\n");
Andi Kleen506ed6b2011-10-12 17:46:33 -0700268 /*
269 * Note this output is parsed by external tools and old fields
270 * should not be changed.
271 */
Borislav Petkov881e23e2011-10-17 16:45:10 +0200272 pr_emerg(HW_ERR "PROCESSOR %u:%x TIME %llu SOCKET %u APIC %x microcode %x\n",
Andi Kleen506ed6b2011-10-12 17:46:33 -0700273 m->cpuvendor, m->cpuid, m->time, m->socketid, m->apicid,
274 cpu_data(m->extcpu).microcode);
Ingo Molnarf436f8b2009-10-01 16:14:32 +0200275
276 /*
277 * Print out human-readable details about the MCE error,
Borislav Petkovfb253192009-10-07 13:20:38 +0200278 * (if the CPU has an implementation for that)
Ingo Molnarf436f8b2009-10-01 16:14:32 +0200279 */
Borislav Petkovdffa4b22011-04-20 12:23:49 +0200280 ret = atomic_notifier_call_chain(&x86_mce_decoder_chain, 0, m);
281 if (ret == NOTIFY_STOP)
282 return;
283
284 pr_emerg_ratelimited(HW_ERR "Run the above through 'mcelog --ascii'\n");
Andi Kleen86503562009-05-27 21:56:58 +0200285}
286
Andi Kleenf94b61c2009-05-27 21:56:55 +0200287#define PANIC_TIMEOUT 5 /* 5 seconds */
288
289static atomic_t mce_paniced;
290
Huang Yingbf783f92009-07-31 09:41:43 +0800291static int fake_panic;
292static atomic_t mce_fake_paniced;
293
Andi Kleenf94b61c2009-05-27 21:56:55 +0200294/* Panic in progress. Enable interrupts and wait for final IPI */
295static void wait_for_panic(void)
296{
297 long timeout = PANIC_TIMEOUT*USEC_PER_SEC;
Ingo Molnarf436f8b2009-10-01 16:14:32 +0200298
Andi Kleenf94b61c2009-05-27 21:56:55 +0200299 preempt_disable();
300 local_irq_enable();
301 while (timeout-- > 0)
302 udelay(1);
Andi Kleen29b0f592009-05-27 21:56:56 +0200303 if (panic_timeout == 0)
304 panic_timeout = mce_panic_timeout;
Andi Kleenf94b61c2009-05-27 21:56:55 +0200305 panic("Panicing machine check CPU died");
306}
307
Andi Kleenbd19a5e2009-05-27 21:56:55 +0200308static void mce_panic(char *msg, struct mce *final, char *exp)
Thomas Gleixnerd88203d2007-10-23 22:37:23 +0200309{
Huang Ying482908b2010-05-18 14:35:22 +0800310 int i, apei_err = 0;
Tim Hockine02e68d2007-07-21 17:10:36 +0200311
Huang Yingbf783f92009-07-31 09:41:43 +0800312 if (!fake_panic) {
313 /*
314 * Make sure only one CPU runs in machine check panic
315 */
316 if (atomic_inc_return(&mce_paniced) > 1)
317 wait_for_panic();
318 barrier();
Andi Kleenf94b61c2009-05-27 21:56:55 +0200319
Huang Yingbf783f92009-07-31 09:41:43 +0800320 bust_spinlocks(1);
321 console_verbose();
322 } else {
323 /* Don't log too much for fake panic */
324 if (atomic_inc_return(&mce_fake_paniced) > 1)
325 return;
326 }
Andi Kleena0189c72009-05-27 21:56:54 +0200327 /* First print corrected ones that are still unlogged */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 for (i = 0; i < MCE_LOG_LEN; i++) {
Andi Kleena0189c72009-05-27 21:56:54 +0200329 struct mce *m = &mcelog.entry[i];
Hidetoshi Seto77e26cc2009-06-11 16:04:35 +0900330 if (!(m->status & MCI_STATUS_VAL))
331 continue;
Huang Ying482908b2010-05-18 14:35:22 +0800332 if (!(m->status & MCI_STATUS_UC)) {
Hidetoshi Seto77e26cc2009-06-11 16:04:35 +0900333 print_mce(m);
Huang Ying482908b2010-05-18 14:35:22 +0800334 if (!apei_err)
335 apei_err = apei_write_mce(m);
336 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 }
Andi Kleena0189c72009-05-27 21:56:54 +0200338 /* Now print uncorrected but with the final one last */
339 for (i = 0; i < MCE_LOG_LEN; i++) {
340 struct mce *m = &mcelog.entry[i];
341 if (!(m->status & MCI_STATUS_VAL))
342 continue;
Hidetoshi Seto77e26cc2009-06-11 16:04:35 +0900343 if (!(m->status & MCI_STATUS_UC))
344 continue;
Huang Ying482908b2010-05-18 14:35:22 +0800345 if (!final || memcmp(m, final, sizeof(struct mce))) {
Hidetoshi Seto77e26cc2009-06-11 16:04:35 +0900346 print_mce(m);
Huang Ying482908b2010-05-18 14:35:22 +0800347 if (!apei_err)
348 apei_err = apei_write_mce(m);
349 }
Andi Kleena0189c72009-05-27 21:56:54 +0200350 }
Huang Ying482908b2010-05-18 14:35:22 +0800351 if (final) {
Hidetoshi Seto77e26cc2009-06-11 16:04:35 +0900352 print_mce(final);
Huang Ying482908b2010-05-18 14:35:22 +0800353 if (!apei_err)
354 apei_err = apei_write_mce(final);
355 }
Andi Kleen3c079792009-05-27 21:56:55 +0200356 if (cpu_missing)
Huang Yinga2d7b0d2010-06-08 14:35:39 +0800357 pr_emerg(HW_ERR "Some CPUs didn't answer in synchronization\n");
Andi Kleenbd19a5e2009-05-27 21:56:55 +0200358 if (exp)
Huang Yinga2d7b0d2010-06-08 14:35:39 +0800359 pr_emerg(HW_ERR "Machine check: %s\n", exp);
Huang Yingbf783f92009-07-31 09:41:43 +0800360 if (!fake_panic) {
361 if (panic_timeout == 0)
362 panic_timeout = mce_panic_timeout;
363 panic(msg);
364 } else
Huang Yinga2d7b0d2010-06-08 14:35:39 +0800365 pr_emerg(HW_ERR "Fake kernel panic: %s\n", msg);
Thomas Gleixnerd88203d2007-10-23 22:37:23 +0200366}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
Andi Kleenea149b32009-04-29 19:31:00 +0200368/* Support code for software error injection */
369
370static int msr_to_offset(u32 msr)
371{
Tejun Heo0a3aee02010-12-18 16:28:55 +0100372 unsigned bank = __this_cpu_read(injectm.bank);
Ingo Molnarf436f8b2009-10-01 16:14:32 +0200373
Andi Kleenea149b32009-04-29 19:31:00 +0200374 if (msr == rip_msr)
375 return offsetof(struct mce, ip);
Andi Kleena2d32bc2009-07-09 00:31:44 +0200376 if (msr == MSR_IA32_MCx_STATUS(bank))
Andi Kleenea149b32009-04-29 19:31:00 +0200377 return offsetof(struct mce, status);
Andi Kleena2d32bc2009-07-09 00:31:44 +0200378 if (msr == MSR_IA32_MCx_ADDR(bank))
Andi Kleenea149b32009-04-29 19:31:00 +0200379 return offsetof(struct mce, addr);
Andi Kleena2d32bc2009-07-09 00:31:44 +0200380 if (msr == MSR_IA32_MCx_MISC(bank))
Andi Kleenea149b32009-04-29 19:31:00 +0200381 return offsetof(struct mce, misc);
382 if (msr == MSR_IA32_MCG_STATUS)
383 return offsetof(struct mce, mcgstatus);
384 return -1;
385}
386
Andi Kleen5f8c1a52009-04-29 19:29:12 +0200387/* MSR access wrappers used for error injection */
388static u64 mce_rdmsrl(u32 msr)
389{
390 u64 v;
Ingo Molnar11868a22009-09-23 17:49:55 +0200391
Tejun Heo0a3aee02010-12-18 16:28:55 +0100392 if (__this_cpu_read(injectm.finished)) {
Andi Kleenea149b32009-04-29 19:31:00 +0200393 int offset = msr_to_offset(msr);
Ingo Molnar11868a22009-09-23 17:49:55 +0200394
Andi Kleenea149b32009-04-29 19:31:00 +0200395 if (offset < 0)
396 return 0;
397 return *(u64 *)((char *)&__get_cpu_var(injectm) + offset);
398 }
Ingo Molnar11868a22009-09-23 17:49:55 +0200399
400 if (rdmsrl_safe(msr, &v)) {
401 WARN_ONCE(1, "mce: Unable to read msr %d!\n", msr);
402 /*
403 * Return zero in case the access faulted. This should
404 * not happen normally but can happen if the CPU does
405 * something weird, or if the code is buggy.
406 */
407 v = 0;
408 }
409
Andi Kleen5f8c1a52009-04-29 19:29:12 +0200410 return v;
411}
412
413static void mce_wrmsrl(u32 msr, u64 v)
414{
Tejun Heo0a3aee02010-12-18 16:28:55 +0100415 if (__this_cpu_read(injectm.finished)) {
Andi Kleenea149b32009-04-29 19:31:00 +0200416 int offset = msr_to_offset(msr);
Ingo Molnar11868a22009-09-23 17:49:55 +0200417
Andi Kleenea149b32009-04-29 19:31:00 +0200418 if (offset >= 0)
419 *(u64 *)((char *)&__get_cpu_var(injectm) + offset) = v;
420 return;
421 }
Andi Kleen5f8c1a52009-04-29 19:29:12 +0200422 wrmsrl(msr, v);
423}
424
Andi Kleen9b1beaf2009-05-27 21:56:59 +0200425/*
Hidetoshi Setob8325c52011-06-08 10:57:46 +0900426 * Collect all global (w.r.t. this processor) status about this machine
427 * check into our "mce" struct so that we can use it later to assess
428 * the severity of the problem as we read per-bank specific details.
429 */
430static inline void mce_gather_info(struct mce *m, struct pt_regs *regs)
431{
432 mce_setup(m);
433
434 m->mcgstatus = mce_rdmsrl(MSR_IA32_MCG_STATUS);
435 if (regs) {
436 /*
437 * Get the address of the instruction at the time of
438 * the machine check error.
439 */
440 if (m->mcgstatus & (MCG_STATUS_RIPV|MCG_STATUS_EIPV)) {
441 m->ip = regs->ip;
442 m->cs = regs->cs;
Andi Kleena129a7c2010-11-19 13:16:22 +0100443
444 /*
445 * When in VM86 mode make the cs look like ring 3
446 * always. This is a lie, but it's better than passing
447 * the additional vm86 bit around everywhere.
448 */
449 if (v8086_mode(regs))
450 m->cs |= 3;
Hidetoshi Setob8325c52011-06-08 10:57:46 +0900451 }
452 /* Use accurate RIP reporting if available. */
453 if (rip_msr)
454 m->ip = mce_rdmsrl(rip_msr);
455 }
456}
457
458/*
Andi Kleen9b1beaf2009-05-27 21:56:59 +0200459 * Simple lockless ring to communicate PFNs from the exception handler with the
460 * process context work function. This is vastly simplified because there's
461 * only a single reader and a single writer.
462 */
463#define MCE_RING_SIZE 16 /* we use one entry less */
464
465struct mce_ring {
466 unsigned short start;
467 unsigned short end;
468 unsigned long ring[MCE_RING_SIZE];
469};
470static DEFINE_PER_CPU(struct mce_ring, mce_ring);
471
472/* Runs with CPU affinity in workqueue */
473static int mce_ring_empty(void)
474{
475 struct mce_ring *r = &__get_cpu_var(mce_ring);
476
477 return r->start == r->end;
478}
479
480static int mce_ring_get(unsigned long *pfn)
481{
482 struct mce_ring *r;
483 int ret = 0;
484
485 *pfn = 0;
486 get_cpu();
487 r = &__get_cpu_var(mce_ring);
488 if (r->start == r->end)
489 goto out;
490 *pfn = r->ring[r->start];
491 r->start = (r->start + 1) % MCE_RING_SIZE;
492 ret = 1;
493out:
494 put_cpu();
495 return ret;
496}
497
498/* Always runs in MCE context with preempt off */
499static int mce_ring_add(unsigned long pfn)
500{
501 struct mce_ring *r = &__get_cpu_var(mce_ring);
502 unsigned next;
503
504 next = (r->end + 1) % MCE_RING_SIZE;
505 if (next == r->start)
506 return -1;
507 r->ring[r->end] = pfn;
508 wmb();
509 r->end = next;
510 return 0;
511}
512
Andi Kleen88ccbed2009-02-12 13:49:36 +0100513int mce_available(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514{
Andi Kleen04b2b1a2009-04-28 22:50:19 +0200515 if (mce_disabled)
Andi Kleen5b4408f2009-02-12 13:39:30 +0100516 return 0;
Akinobu Mita3d1712c2006-03-24 03:15:11 -0800517 return cpu_has(c, X86_FEATURE_MCE) && cpu_has(c, X86_FEATURE_MCA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518}
519
Andi Kleen9b1beaf2009-05-27 21:56:59 +0200520static void mce_schedule_work(void)
521{
522 if (!mce_ring_empty()) {
523 struct work_struct *work = &__get_cpu_var(mce_work);
524 if (!work_pending(work))
525 schedule_work(work);
526 }
527}
528
Hidetoshi Setob77e70b2011-06-08 10:56:02 +0900529DEFINE_PER_CPU(struct irq_work, mce_irq_work);
530
531static void mce_irq_work_cb(struct irq_work *entry)
Andi Kleenccc3c312009-05-27 21:56:54 +0200532{
Andi Kleen9ff36ee2009-05-27 21:56:58 +0200533 mce_notify_irq();
Andi Kleen9b1beaf2009-05-27 21:56:59 +0200534 mce_schedule_work();
Andi Kleenccc3c312009-05-27 21:56:54 +0200535}
Andi Kleenccc3c312009-05-27 21:56:54 +0200536
537static void mce_report_event(struct pt_regs *regs)
538{
539 if (regs->flags & (X86_VM_MASK|X86_EFLAGS_IF)) {
Andi Kleen9ff36ee2009-05-27 21:56:58 +0200540 mce_notify_irq();
Andi Kleen9b1beaf2009-05-27 21:56:59 +0200541 /*
542 * Triggering the work queue here is just an insurance
543 * policy in case the syscall exit notify handler
544 * doesn't run soon enough or ends up running on the
545 * wrong CPU (can happen when audit sleeps)
546 */
547 mce_schedule_work();
Andi Kleenccc3c312009-05-27 21:56:54 +0200548 return;
549 }
550
Hidetoshi Setob77e70b2011-06-08 10:56:02 +0900551 irq_work_queue(&__get_cpu_var(mce_irq_work));
Andi Kleenccc3c312009-05-27 21:56:54 +0200552}
553
Tony Luck85f926942011-12-13 09:48:13 -0800554/*
555 * Read ADDR and MISC registers.
556 */
557static void mce_read_aux(struct mce *m, int i)
558{
559 if (m->status & MCI_STATUS_MISCV)
560 m->misc = mce_rdmsrl(MSR_IA32_MCx_MISC(i));
561 if (m->status & MCI_STATUS_ADDRV) {
562 m->addr = mce_rdmsrl(MSR_IA32_MCx_ADDR(i));
563
564 /*
565 * Mask the reported address by the reported granularity.
566 */
567 if (mce_ser && (m->status & MCI_STATUS_MISCV)) {
568 u8 shift = MCI_MISC_ADDR_LSB(m->misc);
569 m->addr >>= shift;
570 m->addr <<= shift;
571 }
572 }
573}
574
Andi Kleenca84f692009-05-27 21:56:57 +0200575DEFINE_PER_CPU(unsigned, mce_poll_count);
576
Thomas Gleixnerd88203d2007-10-23 22:37:23 +0200577/*
Andi Kleenb79109c2009-02-12 13:43:23 +0100578 * Poll for corrected events or events that happened before reset.
579 * Those are just logged through /dev/mcelog.
580 *
581 * This is executed in standard interrupt context.
Andi Kleened7290d2009-05-27 21:56:57 +0200582 *
583 * Note: spec recommends to panic for fatal unsignalled
584 * errors here. However this would be quite problematic --
585 * we would need to reimplement the Monarch handling and
586 * it would mess up the exclusion between exception handler
587 * and poll hander -- * so we skip this for now.
588 * These cases should not happen anyways, or only when the CPU
589 * is already totally * confused. In this case it's likely it will
590 * not fully execute the machine check handler either.
Andi Kleenb79109c2009-02-12 13:43:23 +0100591 */
Andi Kleenee031c32009-02-12 13:49:34 +0100592void machine_check_poll(enum mcp_flags flags, mce_banks_t *b)
Andi Kleenb79109c2009-02-12 13:43:23 +0100593{
594 struct mce m;
595 int i;
596
Alex Shic6ae41e2012-05-11 15:35:27 +0800597 this_cpu_inc(mce_poll_count);
Andi Kleenca84f692009-05-27 21:56:57 +0200598
Hidetoshi Setob8325c52011-06-08 10:57:46 +0900599 mce_gather_info(&m, NULL);
Andi Kleenb79109c2009-02-12 13:43:23 +0100600
Andi Kleenb79109c2009-02-12 13:43:23 +0100601 for (i = 0; i < banks; i++) {
Andi Kleencebe1822009-07-09 00:31:43 +0200602 if (!mce_banks[i].ctl || !test_bit(i, *b))
Andi Kleenb79109c2009-02-12 13:43:23 +0100603 continue;
604
605 m.misc = 0;
606 m.addr = 0;
607 m.bank = i;
608 m.tsc = 0;
609
610 barrier();
Andi Kleena2d32bc2009-07-09 00:31:44 +0200611 m.status = mce_rdmsrl(MSR_IA32_MCx_STATUS(i));
Andi Kleenb79109c2009-02-12 13:43:23 +0100612 if (!(m.status & MCI_STATUS_VAL))
613 continue;
614
615 /*
Andi Kleened7290d2009-05-27 21:56:57 +0200616 * Uncorrected or signalled events are handled by the exception
617 * handler when it is enabled, so don't process those here.
Andi Kleenb79109c2009-02-12 13:43:23 +0100618 *
619 * TBD do the same check for MCI_STATUS_EN here?
620 */
Andi Kleened7290d2009-05-27 21:56:57 +0200621 if (!(flags & MCP_UC) &&
622 (m.status & (mce_ser ? MCI_STATUS_S : MCI_STATUS_UC)))
Andi Kleenb79109c2009-02-12 13:43:23 +0100623 continue;
624
Tony Luck85f926942011-12-13 09:48:13 -0800625 mce_read_aux(&m, i);
Andi Kleenb79109c2009-02-12 13:43:23 +0100626
627 if (!(flags & MCP_TIMESTAMP))
628 m.tsc = 0;
629 /*
630 * Don't get the IP here because it's unlikely to
631 * have anything to do with the actual error location.
632 */
Borislav Petkovf0cb5452011-07-18 11:24:45 -0300633 if (!(flags & MCP_DONTLOG) && !mce_dont_log_ce)
Andi Kleen5679af42009-04-07 17:06:55 +0200634 mce_log(&m);
Andi Kleenb79109c2009-02-12 13:43:23 +0100635
636 /*
637 * Clear state for this bank.
638 */
Andi Kleena2d32bc2009-07-09 00:31:44 +0200639 mce_wrmsrl(MSR_IA32_MCx_STATUS(i), 0);
Andi Kleenb79109c2009-02-12 13:43:23 +0100640 }
641
642 /*
643 * Don't clear MCG_STATUS here because it's only defined for
644 * exceptions.
645 */
Andi Kleen88921be2009-05-27 21:56:51 +0200646
647 sync_core();
Andi Kleenb79109c2009-02-12 13:43:23 +0100648}
Andi Kleenea149b32009-04-29 19:31:00 +0200649EXPORT_SYMBOL_GPL(machine_check_poll);
Andi Kleenb79109c2009-02-12 13:43:23 +0100650
651/*
Andi Kleenbd19a5e2009-05-27 21:56:55 +0200652 * Do a quick check if any of the events requires a panic.
653 * This decides if we keep the events around or clear them.
654 */
Tony Luck95022b82012-04-18 15:19:40 -0700655static int mce_no_way_out(struct mce *m, char **msg, unsigned long *validp)
Andi Kleenbd19a5e2009-05-27 21:56:55 +0200656{
Tony Luck95022b82012-04-18 15:19:40 -0700657 int i, ret = 0;
Andi Kleenbd19a5e2009-05-27 21:56:55 +0200658
659 for (i = 0; i < banks; i++) {
Andi Kleena2d32bc2009-07-09 00:31:44 +0200660 m->status = mce_rdmsrl(MSR_IA32_MCx_STATUS(i));
Tony Luck95022b82012-04-18 15:19:40 -0700661 if (m->status & MCI_STATUS_VAL)
662 __set_bit(i, validp);
Andi Kleenbd19a5e2009-05-27 21:56:55 +0200663 if (mce_severity(m, tolerant, msg) >= MCE_PANIC_SEVERITY)
Tony Luck95022b82012-04-18 15:19:40 -0700664 ret = 1;
Andi Kleenbd19a5e2009-05-27 21:56:55 +0200665 }
Tony Luck95022b82012-04-18 15:19:40 -0700666 return ret;
Andi Kleenbd19a5e2009-05-27 21:56:55 +0200667}
668
669/*
Andi Kleen3c079792009-05-27 21:56:55 +0200670 * Variable to establish order between CPUs while scanning.
671 * Each CPU spins initially until executing is equal its number.
672 */
673static atomic_t mce_executing;
674
675/*
676 * Defines order of CPUs on entry. First CPU becomes Monarch.
677 */
678static atomic_t mce_callin;
679
680/*
681 * Check if a timeout waiting for other CPUs happened.
682 */
683static int mce_timed_out(u64 *t)
684{
685 /*
686 * The others already did panic for some reason.
687 * Bail out like in a timeout.
688 * rmb() to tell the compiler that system_state
689 * might have been modified by someone else.
690 */
691 rmb();
692 if (atomic_read(&mce_paniced))
693 wait_for_panic();
694 if (!monarch_timeout)
695 goto out;
696 if ((s64)*t < SPINUNIT) {
697 /* CHECKME: Make panic default for 1 too? */
698 if (tolerant < 1)
699 mce_panic("Timeout synchronizing machine check over CPUs",
700 NULL, NULL);
701 cpu_missing = 1;
702 return 1;
703 }
704 *t -= SPINUNIT;
705out:
706 touch_nmi_watchdog();
707 return 0;
708}
709
710/*
711 * The Monarch's reign. The Monarch is the CPU who entered
712 * the machine check handler first. It waits for the others to
713 * raise the exception too and then grades them. When any
714 * error is fatal panic. Only then let the others continue.
715 *
716 * The other CPUs entering the MCE handler will be controlled by the
717 * Monarch. They are called Subjects.
718 *
719 * This way we prevent any potential data corruption in a unrecoverable case
720 * and also makes sure always all CPU's errors are examined.
721 *
Hidetoshi Seto680b6cf2009-08-26 16:20:36 +0900722 * Also this detects the case of a machine check event coming from outer
Andi Kleen3c079792009-05-27 21:56:55 +0200723 * space (not detected by any CPUs) In this case some external agent wants
724 * us to shut down, so panic too.
725 *
726 * The other CPUs might still decide to panic if the handler happens
727 * in a unrecoverable place, but in this case the system is in a semi-stable
728 * state and won't corrupt anything by itself. It's ok to let the others
729 * continue for a bit first.
730 *
731 * All the spin loops have timeouts; when a timeout happens a CPU
732 * typically elects itself to be Monarch.
733 */
734static void mce_reign(void)
735{
736 int cpu;
737 struct mce *m = NULL;
738 int global_worst = 0;
739 char *msg = NULL;
740 char *nmsg = NULL;
741
742 /*
743 * This CPU is the Monarch and the other CPUs have run
744 * through their handlers.
745 * Grade the severity of the errors of all the CPUs.
746 */
747 for_each_possible_cpu(cpu) {
748 int severity = mce_severity(&per_cpu(mces_seen, cpu), tolerant,
749 &nmsg);
750 if (severity > global_worst) {
751 msg = nmsg;
752 global_worst = severity;
753 m = &per_cpu(mces_seen, cpu);
754 }
755 }
756
757 /*
758 * Cannot recover? Panic here then.
759 * This dumps all the mces in the log buffer and stops the
760 * other CPUs.
761 */
762 if (m && global_worst >= MCE_PANIC_SEVERITY && tolerant < 3)
Andi Kleenac960372009-05-27 21:56:58 +0200763 mce_panic("Fatal Machine check", m, msg);
Andi Kleen3c079792009-05-27 21:56:55 +0200764
765 /*
766 * For UC somewhere we let the CPU who detects it handle it.
767 * Also must let continue the others, otherwise the handling
768 * CPU could deadlock on a lock.
769 */
770
771 /*
772 * No machine check event found. Must be some external
773 * source or one CPU is hung. Panic.
774 */
Hidetoshi Seto680b6cf2009-08-26 16:20:36 +0900775 if (global_worst <= MCE_KEEP_SEVERITY && tolerant < 3)
Andi Kleen3c079792009-05-27 21:56:55 +0200776 mce_panic("Machine check from unknown source", NULL, NULL);
777
778 /*
779 * Now clear all the mces_seen so that they don't reappear on
780 * the next mce.
781 */
782 for_each_possible_cpu(cpu)
783 memset(&per_cpu(mces_seen, cpu), 0, sizeof(struct mce));
784}
785
786static atomic_t global_nwo;
787
788/*
789 * Start of Monarch synchronization. This waits until all CPUs have
790 * entered the exception handler and then determines if any of them
791 * saw a fatal event that requires panic. Then it executes them
792 * in the entry order.
793 * TBD double check parallel CPU hotunplug
794 */
Hidetoshi Seto7fb06fc2009-06-15 18:18:43 +0900795static int mce_start(int *no_way_out)
Andi Kleen3c079792009-05-27 21:56:55 +0200796{
Hidetoshi Seto7fb06fc2009-06-15 18:18:43 +0900797 int order;
Andi Kleen3c079792009-05-27 21:56:55 +0200798 int cpus = num_online_cpus();
799 u64 timeout = (u64)monarch_timeout * NSEC_PER_USEC;
800
Hidetoshi Seto7fb06fc2009-06-15 18:18:43 +0900801 if (!timeout)
802 return -1;
Andi Kleen3c079792009-05-27 21:56:55 +0200803
Hidetoshi Seto7fb06fc2009-06-15 18:18:43 +0900804 atomic_add(*no_way_out, &global_nwo);
Huang Ying184e1fd2009-06-15 15:37:07 +0800805 /*
806 * global_nwo should be updated before mce_callin
807 */
808 smp_wmb();
Borislav Petkova95436e2009-06-20 23:28:22 -0700809 order = atomic_inc_return(&mce_callin);
Andi Kleen3c079792009-05-27 21:56:55 +0200810
811 /*
812 * Wait for everyone.
813 */
814 while (atomic_read(&mce_callin) != cpus) {
815 if (mce_timed_out(&timeout)) {
816 atomic_set(&global_nwo, 0);
Hidetoshi Seto7fb06fc2009-06-15 18:18:43 +0900817 return -1;
Andi Kleen3c079792009-05-27 21:56:55 +0200818 }
819 ndelay(SPINUNIT);
820 }
821
822 /*
Huang Ying184e1fd2009-06-15 15:37:07 +0800823 * mce_callin should be read before global_nwo
824 */
825 smp_rmb();
Hidetoshi Seto7fb06fc2009-06-15 18:18:43 +0900826
827 if (order == 1) {
828 /*
829 * Monarch: Starts executing now, the others wait.
830 */
831 atomic_set(&mce_executing, 1);
832 } else {
833 /*
834 * Subject: Now start the scanning loop one by one in
835 * the original callin order.
836 * This way when there are any shared banks it will be
837 * only seen by one CPU before cleared, avoiding duplicates.
838 */
839 while (atomic_read(&mce_executing) < order) {
840 if (mce_timed_out(&timeout)) {
841 atomic_set(&global_nwo, 0);
842 return -1;
843 }
844 ndelay(SPINUNIT);
845 }
846 }
847
Huang Ying184e1fd2009-06-15 15:37:07 +0800848 /*
Andi Kleen3c079792009-05-27 21:56:55 +0200849 * Cache the global no_way_out state.
850 */
Hidetoshi Seto7fb06fc2009-06-15 18:18:43 +0900851 *no_way_out = atomic_read(&global_nwo);
Andi Kleen3c079792009-05-27 21:56:55 +0200852
Hidetoshi Seto7fb06fc2009-06-15 18:18:43 +0900853 return order;
Andi Kleen3c079792009-05-27 21:56:55 +0200854}
855
856/*
857 * Synchronize between CPUs after main scanning loop.
858 * This invokes the bulk of the Monarch processing.
859 */
860static int mce_end(int order)
861{
862 int ret = -1;
863 u64 timeout = (u64)monarch_timeout * NSEC_PER_USEC;
864
865 if (!timeout)
866 goto reset;
867 if (order < 0)
868 goto reset;
869
870 /*
871 * Allow others to run.
872 */
873 atomic_inc(&mce_executing);
874
875 if (order == 1) {
876 /* CHECKME: Can this race with a parallel hotplug? */
877 int cpus = num_online_cpus();
878
879 /*
880 * Monarch: Wait for everyone to go through their scanning
881 * loops.
882 */
883 while (atomic_read(&mce_executing) <= cpus) {
884 if (mce_timed_out(&timeout))
885 goto reset;
886 ndelay(SPINUNIT);
887 }
888
889 mce_reign();
890 barrier();
891 ret = 0;
892 } else {
893 /*
894 * Subject: Wait for Monarch to finish.
895 */
896 while (atomic_read(&mce_executing) != 0) {
897 if (mce_timed_out(&timeout))
898 goto reset;
899 ndelay(SPINUNIT);
900 }
901
902 /*
903 * Don't reset anything. That's done by the Monarch.
904 */
905 return 0;
906 }
907
908 /*
909 * Reset all global state.
910 */
911reset:
912 atomic_set(&global_nwo, 0);
913 atomic_set(&mce_callin, 0);
914 barrier();
915
916 /*
917 * Let others run again.
918 */
919 atomic_set(&mce_executing, 0);
920 return ret;
921}
922
Andi Kleen9b1beaf2009-05-27 21:56:59 +0200923/*
924 * Check if the address reported by the CPU is in a format we can parse.
925 * It would be possible to add code for most other cases, but all would
926 * be somewhat complicated (e.g. segment offset would require an instruction
Lucas De Marchi0d2eb442011-03-17 16:24:16 -0300927 * parser). So only support physical addresses up to page granuality for now.
Andi Kleen9b1beaf2009-05-27 21:56:59 +0200928 */
929static int mce_usable_address(struct mce *m)
930{
931 if (!(m->status & MCI_STATUS_MISCV) || !(m->status & MCI_STATUS_ADDRV))
932 return 0;
Hidetoshi Seto2b90e772011-06-08 10:56:56 +0900933 if (MCI_MISC_ADDR_LSB(m->misc) > PAGE_SHIFT)
Andi Kleen9b1beaf2009-05-27 21:56:59 +0200934 return 0;
Hidetoshi Seto2b90e772011-06-08 10:56:56 +0900935 if (MCI_MISC_ADDR_MODE(m->misc) != MCI_MISC_ADDR_PHYS)
Andi Kleen9b1beaf2009-05-27 21:56:59 +0200936 return 0;
937 return 1;
938}
939
Andi Kleen3c079792009-05-27 21:56:55 +0200940static void mce_clear_state(unsigned long *toclear)
941{
942 int i;
943
944 for (i = 0; i < banks; i++) {
945 if (test_bit(i, toclear))
Andi Kleena2d32bc2009-07-09 00:31:44 +0200946 mce_wrmsrl(MSR_IA32_MCx_STATUS(i), 0);
Andi Kleen3c079792009-05-27 21:56:55 +0200947 }
948}
949
950/*
Tony Luckaf104e32011-12-14 15:55:20 -0800951 * Need to save faulting physical address associated with a process
952 * in the machine check handler some place where we can grab it back
953 * later in mce_notify_process()
954 */
955#define MCE_INFO_MAX 16
956
957struct mce_info {
958 atomic_t inuse;
959 struct task_struct *t;
960 __u64 paddr;
Tony Luckdad17432012-05-14 15:07:48 -0700961 int restartable;
Tony Luckaf104e32011-12-14 15:55:20 -0800962} mce_info[MCE_INFO_MAX];
963
Tony Luckdad17432012-05-14 15:07:48 -0700964static void mce_save_info(__u64 addr, int c)
Tony Luckaf104e32011-12-14 15:55:20 -0800965{
966 struct mce_info *mi;
967
968 for (mi = mce_info; mi < &mce_info[MCE_INFO_MAX]; mi++) {
969 if (atomic_cmpxchg(&mi->inuse, 0, 1) == 0) {
970 mi->t = current;
971 mi->paddr = addr;
Tony Luckdad17432012-05-14 15:07:48 -0700972 mi->restartable = c;
Tony Luckaf104e32011-12-14 15:55:20 -0800973 return;
974 }
975 }
976
977 mce_panic("Too many concurrent recoverable errors", NULL, NULL);
978}
979
980static struct mce_info *mce_find_info(void)
981{
982 struct mce_info *mi;
983
984 for (mi = mce_info; mi < &mce_info[MCE_INFO_MAX]; mi++)
985 if (atomic_read(&mi->inuse) && mi->t == current)
986 return mi;
987 return NULL;
988}
989
990static void mce_clear_info(struct mce_info *mi)
991{
992 atomic_set(&mi->inuse, 0);
993}
994
995/*
Andi Kleenb79109c2009-02-12 13:43:23 +0100996 * The actual machine check handler. This only handles real
997 * exceptions when something got corrupted coming in through int 18.
998 *
999 * This is executed in NMI context not subject to normal locking rules. This
1000 * implies that most kernel services cannot be safely used. Don't even
1001 * think about putting a printk in there!
Andi Kleen3c079792009-05-27 21:56:55 +02001002 *
1003 * On Intel systems this is entered on all CPUs in parallel through
1004 * MCE broadcast. However some CPUs might be broken beyond repair,
1005 * so be always careful when synchronizing with others.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 */
Ingo Molnare9eee032009-04-08 12:31:17 +02001007void do_machine_check(struct pt_regs *regs, long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008{
Andi Kleen3c079792009-05-27 21:56:55 +02001009 struct mce m, *final;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 int i;
Andi Kleen3c079792009-05-27 21:56:55 +02001011 int worst = 0;
1012 int severity;
1013 /*
1014 * Establish sequential order between the CPUs entering the machine
1015 * check handler.
1016 */
Hidetoshi Seto7fb06fc2009-06-15 18:18:43 +09001017 int order;
Tim Hockinbd784322007-07-21 17:10:37 +02001018 /*
1019 * If no_way_out gets set, there is no safe way to recover from this
1020 * MCE. If tolerant is cranked up, we'll try anyway.
1021 */
1022 int no_way_out = 0;
1023 /*
1024 * If kill_it gets set, there might be a way to recover from this
1025 * error.
1026 */
1027 int kill_it = 0;
Andi Kleenb79109c2009-02-12 13:43:23 +01001028 DECLARE_BITMAP(toclear, MAX_NR_BANKS);
Tony Luck95022b82012-04-18 15:19:40 -07001029 DECLARE_BITMAP(valid_banks, MAX_NR_BANKS);
Andi Kleenbd19a5e2009-05-27 21:56:55 +02001030 char *msg = "Unknown";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
Andi Kleen553f2652006-04-07 19:49:57 +02001032 atomic_inc(&mce_entry);
1033
Alex Shic6ae41e2012-05-11 15:35:27 +08001034 this_cpu_inc(mce_exception_count);
Andi Kleen01ca79f2009-05-27 21:56:52 +02001035
Andi Kleenb79109c2009-02-12 13:43:23 +01001036 if (!banks)
Andi Kleen32561692009-05-27 21:56:53 +02001037 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
Hidetoshi Setob8325c52011-06-08 10:57:46 +09001039 mce_gather_info(&m, regs);
Andi Kleenb5f2fa42009-02-12 13:43:22 +01001040
Andi Kleen3c079792009-05-27 21:56:55 +02001041 final = &__get_cpu_var(mces_seen);
1042 *final = m;
1043
Tony Luck95022b82012-04-18 15:19:40 -07001044 memset(valid_banks, 0, sizeof(valid_banks));
1045 no_way_out = mce_no_way_out(&m, &msg, valid_banks);
Hidetoshi Seto680b6cf2009-08-26 16:20:36 +09001046
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 barrier();
1048
Andi Kleen3c079792009-05-27 21:56:55 +02001049 /*
Tony Lucka8c321f2012-01-03 11:45:45 -08001050 * When no restart IP might need to kill or panic.
1051 * Assume the worst for now, but if we find the
1052 * severity is MCE_AR_SEVERITY we have other options.
Andi Kleened7290d2009-05-27 21:56:57 +02001053 */
1054 if (!(m.mcgstatus & MCG_STATUS_RIPV))
1055 kill_it = 1;
1056
1057 /*
Andi Kleen3c079792009-05-27 21:56:55 +02001058 * Go through all the banks in exclusion of the other CPUs.
1059 * This way we don't report duplicated events on shared banks
1060 * because the first one to see it will clear it.
1061 */
Hidetoshi Seto7fb06fc2009-06-15 18:18:43 +09001062 order = mce_start(&no_way_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 for (i = 0; i < banks; i++) {
Andi Kleenb79109c2009-02-12 13:43:23 +01001064 __clear_bit(i, toclear);
Tony Luck95022b82012-04-18 15:19:40 -07001065 if (!test_bit(i, valid_banks))
1066 continue;
Andi Kleencebe1822009-07-09 00:31:43 +02001067 if (!mce_banks[i].ctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 continue;
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001069
1070 m.misc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 m.addr = 0;
1072 m.bank = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
Andi Kleena2d32bc2009-07-09 00:31:44 +02001074 m.status = mce_rdmsrl(MSR_IA32_MCx_STATUS(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 if ((m.status & MCI_STATUS_VAL) == 0)
1076 continue;
1077
Andi Kleenb79109c2009-02-12 13:43:23 +01001078 /*
Andi Kleened7290d2009-05-27 21:56:57 +02001079 * Non uncorrected or non signaled errors are handled by
1080 * machine_check_poll. Leave them alone, unless this panics.
Andi Kleenb79109c2009-02-12 13:43:23 +01001081 */
Andi Kleened7290d2009-05-27 21:56:57 +02001082 if (!(m.status & (mce_ser ? MCI_STATUS_S : MCI_STATUS_UC)) &&
1083 !no_way_out)
Andi Kleenb79109c2009-02-12 13:43:23 +01001084 continue;
1085
1086 /*
1087 * Set taint even when machine check was not enabled.
1088 */
1089 add_taint(TAINT_MACHINE_CHECK);
1090
Andi Kleened7290d2009-05-27 21:56:57 +02001091 severity = mce_severity(&m, tolerant, NULL);
Andi Kleenb79109c2009-02-12 13:43:23 +01001092
Andi Kleened7290d2009-05-27 21:56:57 +02001093 /*
1094 * When machine check was for corrected handler don't touch,
1095 * unless we're panicing.
1096 */
1097 if (severity == MCE_KEEP_SEVERITY && !no_way_out)
1098 continue;
1099 __set_bit(i, toclear);
1100 if (severity == MCE_NO_SEVERITY) {
Andi Kleenb79109c2009-02-12 13:43:23 +01001101 /*
1102 * Machine check event was not enabled. Clear, but
1103 * ignore.
1104 */
1105 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 }
1107
Tony Luck85f926942011-12-13 09:48:13 -08001108 mce_read_aux(&m, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
Andi Kleen9b1beaf2009-05-27 21:56:59 +02001110 /*
1111 * Action optional error. Queue address for later processing.
1112 * When the ring overflows we just ignore the AO error.
1113 * RED-PEN add some logging mechanism when
1114 * usable_address or mce_add_ring fails.
1115 * RED-PEN don't ignore overflow for tolerant == 0
1116 */
1117 if (severity == MCE_AO_SEVERITY && mce_usable_address(&m))
1118 mce_ring_add(m.addr >> PAGE_SHIFT);
1119
Andi Kleenb79109c2009-02-12 13:43:23 +01001120 mce_log(&m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
Andi Kleen3c079792009-05-27 21:56:55 +02001122 if (severity > worst) {
1123 *final = m;
1124 worst = severity;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 }
1127
Tony Lucka8c321f2012-01-03 11:45:45 -08001128 /* mce_clear_state will clear *final, save locally for use later */
1129 m = *final;
1130
Andi Kleen3c079792009-05-27 21:56:55 +02001131 if (!no_way_out)
1132 mce_clear_state(toclear);
1133
Ingo Molnare9eee032009-04-08 12:31:17 +02001134 /*
Andi Kleen3c079792009-05-27 21:56:55 +02001135 * Do most of the synchronization with other CPUs.
1136 * When there's any problem use only local no_way_out state.
Ingo Molnare9eee032009-04-08 12:31:17 +02001137 */
Andi Kleen3c079792009-05-27 21:56:55 +02001138 if (mce_end(order) < 0)
1139 no_way_out = worst >= MCE_PANIC_SEVERITY;
Tim Hockinbd784322007-07-21 17:10:37 +02001140
1141 /*
Tony Lucka8c321f2012-01-03 11:45:45 -08001142 * At insane "tolerant" levels we take no action. Otherwise
1143 * we only die if we have no other choice. For less serious
1144 * issues we try to recover, or limit damage to the current
1145 * process.
Tim Hockinbd784322007-07-21 17:10:37 +02001146 */
Tony Lucka8c321f2012-01-03 11:45:45 -08001147 if (tolerant < 3) {
1148 if (no_way_out)
1149 mce_panic("Fatal machine check on current CPU", &m, msg);
1150 if (worst == MCE_AR_SEVERITY) {
1151 /* schedule action before return to userland */
Tony Luckdad17432012-05-14 15:07:48 -07001152 mce_save_info(m.addr, m.mcgstatus & MCG_STATUS_RIPV);
Tony Lucka8c321f2012-01-03 11:45:45 -08001153 set_thread_flag(TIF_MCE_NOTIFY);
1154 } else if (kill_it) {
1155 force_sig(SIGBUS, current);
1156 }
1157 }
Tim Hockine02e68d2007-07-21 17:10:36 +02001158
Andi Kleen3c079792009-05-27 21:56:55 +02001159 if (worst > 0)
1160 mce_report_event(regs);
Andi Kleen5f8c1a52009-04-29 19:29:12 +02001161 mce_wrmsrl(MSR_IA32_MCG_STATUS, 0);
Andi Kleen32561692009-05-27 21:56:53 +02001162out:
Andi Kleen553f2652006-04-07 19:49:57 +02001163 atomic_dec(&mce_entry);
Andi Kleen88921be2009-05-27 21:56:51 +02001164 sync_core();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165}
Andi Kleenea149b32009-04-29 19:31:00 +02001166EXPORT_SYMBOL_GPL(do_machine_check);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
Tony Luckcd42f4a2011-12-15 10:48:12 -08001168#ifndef CONFIG_MEMORY_FAILURE
1169int memory_failure(unsigned long pfn, int vector, int flags)
Andi Kleen9b1beaf2009-05-27 21:56:59 +02001170{
Tony Lucka8c321f2012-01-03 11:45:45 -08001171 /* mce_severity() should not hand us an ACTION_REQUIRED error */
1172 BUG_ON(flags & MF_ACTION_REQUIRED);
Joe Perchesc767a542012-05-21 19:50:07 -07001173 pr_err("Uncorrected memory error in page 0x%lx ignored\n"
1174 "Rebuild kernel with CONFIG_MEMORY_FAILURE=y for smarter handling\n",
1175 pfn);
Tony Luckcd42f4a2011-12-15 10:48:12 -08001176
1177 return 0;
Andi Kleen9b1beaf2009-05-27 21:56:59 +02001178}
Tony Luckcd42f4a2011-12-15 10:48:12 -08001179#endif
Andi Kleen9b1beaf2009-05-27 21:56:59 +02001180
1181/*
Tony Lucka8c321f2012-01-03 11:45:45 -08001182 * Called in process context that interrupted by MCE and marked with
1183 * TIF_MCE_NOTIFY, just before returning to erroneous userland.
1184 * This code is allowed to sleep.
1185 * Attempt possible recovery such as calling the high level VM handler to
1186 * process any corrupted pages, and kill/signal current process if required.
1187 * Action required errors are handled here.
Andi Kleen9b1beaf2009-05-27 21:56:59 +02001188 */
1189void mce_notify_process(void)
1190{
1191 unsigned long pfn;
Tony Lucka8c321f2012-01-03 11:45:45 -08001192 struct mce_info *mi = mce_find_info();
Tony Luck6751ed62012-07-11 10:20:47 -07001193 int flags = MF_ACTION_REQUIRED;
Tony Lucka8c321f2012-01-03 11:45:45 -08001194
1195 if (!mi)
1196 mce_panic("Lost physical address for unconsumed uncorrectable error", NULL, NULL);
1197 pfn = mi->paddr >> PAGE_SHIFT;
1198
1199 clear_thread_flag(TIF_MCE_NOTIFY);
1200
1201 pr_err("Uncorrected hardware memory error in user-access at %llx",
1202 mi->paddr);
Tony Luckdad17432012-05-14 15:07:48 -07001203 /*
1204 * We must call memory_failure() here even if the current process is
1205 * doomed. We still need to mark the page as poisoned and alert any
1206 * other users of the page.
1207 */
Tony Luck6751ed62012-07-11 10:20:47 -07001208 if (!mi->restartable)
1209 flags |= MF_MUST_KILL;
1210 if (memory_failure(pfn, MCE_VECTOR, flags) < 0) {
Tony Lucka8c321f2012-01-03 11:45:45 -08001211 pr_err("Memory error not recovered");
1212 force_sig(SIGBUS, current);
1213 }
1214 mce_clear_info(mi);
Andi Kleen9b1beaf2009-05-27 21:56:59 +02001215}
1216
Tony Lucka8c321f2012-01-03 11:45:45 -08001217/*
1218 * Action optional processing happens here (picking up
1219 * from the list of faulting pages that do_machine_check()
1220 * placed into the "ring").
1221 */
Andi Kleen9b1beaf2009-05-27 21:56:59 +02001222static void mce_process_work(struct work_struct *dummy)
1223{
Tony Lucka8c321f2012-01-03 11:45:45 -08001224 unsigned long pfn;
1225
1226 while (mce_ring_get(&pfn))
1227 memory_failure(pfn, MCE_VECTOR, 0);
Andi Kleen9b1beaf2009-05-27 21:56:59 +02001228}
1229
Dmitriy Zavin15d5f832006-09-26 10:52:42 +02001230#ifdef CONFIG_X86_MCE_INTEL
1231/***
1232 * mce_log_therm_throt_event - Logs the thermal throttling event to mcelog
Simon Arlott676b1852007-10-20 01:25:36 +02001233 * @cpu: The CPU on which the event occurred.
Dmitriy Zavin15d5f832006-09-26 10:52:42 +02001234 * @status: Event status information
1235 *
1236 * This function should be called by the thermal interrupt after the
1237 * event has been processed and the decision was made to log the event
1238 * further.
1239 *
1240 * The status parameter will be saved to the 'status' field of 'struct mce'
1241 * and historically has been the register value of the
1242 * MSR_IA32_THERMAL_STATUS (Intel) msr.
1243 */
Andi Kleenb5f2fa42009-02-12 13:43:22 +01001244void mce_log_therm_throt_event(__u64 status)
Dmitriy Zavin15d5f832006-09-26 10:52:42 +02001245{
1246 struct mce m;
1247
Andi Kleenb5f2fa42009-02-12 13:43:22 +01001248 mce_setup(&m);
Dmitriy Zavin15d5f832006-09-26 10:52:42 +02001249 m.bank = MCE_THERMAL_BANK;
1250 m.status = status;
Dmitriy Zavin15d5f832006-09-26 10:52:42 +02001251 mce_log(&m);
1252}
1253#endif /* CONFIG_X86_MCE_INTEL */
1254
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255/*
Tim Hockin8a336b02007-05-02 19:27:19 +02001256 * Periodic polling timer for "silent" machine check errors. If the
1257 * poller finds an MCE, poll 2x faster. When the poller finds no more
1258 * errors, poll 2x slower (up to check_interval seconds).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 */
Thomas Gleixner82f7af02012-05-24 17:54:51 +00001260static unsigned long check_interval = 5 * 60; /* 5 minutes */
Ingo Molnare9eee032009-04-08 12:31:17 +02001261
Thomas Gleixner82f7af02012-05-24 17:54:51 +00001262static DEFINE_PER_CPU(unsigned long, mce_next_interval); /* in jiffies */
Andi Kleen52d168e2009-02-12 13:39:29 +01001263static DEFINE_PER_CPU(struct timer_list, mce_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
Thomas Gleixner82f7af02012-05-24 17:54:51 +00001265static void mce_timer_fn(unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266{
Thomas Gleixner82f7af02012-05-24 17:54:51 +00001267 struct timer_list *t = &__get_cpu_var(mce_timer);
1268 unsigned long iv;
Andi Kleen52d168e2009-02-12 13:39:29 +01001269
1270 WARN_ON(smp_processor_id() != data);
1271
Tejun Heo7b543a52010-12-18 16:30:05 +01001272 if (mce_available(__this_cpu_ptr(&cpu_info))) {
Andi Kleenee031c32009-02-12 13:49:34 +01001273 machine_check_poll(MCP_TIMESTAMP,
1274 &__get_cpu_var(mce_poll_banks));
Ingo Molnare9eee032009-04-08 12:31:17 +02001275 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
1277 /*
Tim Hockine02e68d2007-07-21 17:10:36 +02001278 * Alert userspace if needed. If we logged an MCE, reduce the
1279 * polling interval, otherwise increase the polling interval.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 */
Thomas Gleixner82f7af02012-05-24 17:54:51 +00001281 iv = __this_cpu_read(mce_next_interval);
Andi Kleen9ff36ee2009-05-27 21:56:58 +02001282 if (mce_notify_irq())
Chen Gong958fb3c2012-06-05 10:35:02 +08001283 iv = max(iv / 2, (unsigned long) HZ/100);
Hidetoshi Seto14a02532009-04-30 16:04:51 +09001284 else
Thomas Gleixner82f7af02012-05-24 17:54:51 +00001285 iv = min(iv * 2, round_jiffies_relative(check_interval * HZ));
1286 __this_cpu_write(mce_next_interval, iv);
Tim Hockin8a336b02007-05-02 19:27:19 +02001287
Thomas Gleixner82f7af02012-05-24 17:54:51 +00001288 t->expires = jiffies + iv;
Hidetoshi Seto5be60662009-06-24 09:21:10 +09001289 add_timer_on(t, smp_processor_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290}
1291
Hidetoshi Seto9aaef962011-06-17 04:40:36 -04001292/* Must not be called in IRQ context where del_timer_sync() can deadlock */
1293static void mce_timer_delete_all(void)
1294{
1295 int cpu;
1296
1297 for_each_online_cpu(cpu)
1298 del_timer_sync(&per_cpu(mce_timer, cpu));
1299}
1300
Andi Kleen9bd98402009-02-12 13:39:28 +01001301static void mce_do_trigger(struct work_struct *work)
1302{
Hidetoshi Seto1020bcb2009-06-15 17:20:57 +09001303 call_usermodehelper(mce_helper, mce_helper_argv, NULL, UMH_NO_WAIT);
Andi Kleen9bd98402009-02-12 13:39:28 +01001304}
1305
1306static DECLARE_WORK(mce_trigger_work, mce_do_trigger);
1307
Tim Hockine02e68d2007-07-21 17:10:36 +02001308/*
Andi Kleen9bd98402009-02-12 13:39:28 +01001309 * Notify the user(s) about new machine check events.
1310 * Can be called from interrupt context, but not from machine check/NMI
1311 * context.
Tim Hockine02e68d2007-07-21 17:10:36 +02001312 */
Andi Kleen9ff36ee2009-05-27 21:56:58 +02001313int mce_notify_irq(void)
Tim Hockine02e68d2007-07-21 17:10:36 +02001314{
Andi Kleen8457c842009-02-12 13:49:33 +01001315 /* Not more than two messages every minute */
1316 static DEFINE_RATELIMIT_STATE(ratelimit, 60*HZ, 2);
1317
Hidetoshi Seto1020bcb2009-06-15 17:20:57 +09001318 if (test_and_clear_bit(0, &mce_need_notify)) {
Hidetoshi Seto93b62c32011-06-08 11:00:45 +09001319 /* wake processes polling /dev/mcelog */
1320 wake_up_interruptible(&mce_chrdev_wait);
Andi Kleen9bd98402009-02-12 13:39:28 +01001321
1322 /*
1323 * There is no risk of missing notifications because
1324 * work_pending is always cleared before the function is
1325 * executed.
1326 */
Hidetoshi Seto1020bcb2009-06-15 17:20:57 +09001327 if (mce_helper[0] && !work_pending(&mce_trigger_work))
Andi Kleen9bd98402009-02-12 13:39:28 +01001328 schedule_work(&mce_trigger_work);
Tim Hockine02e68d2007-07-21 17:10:36 +02001329
Andi Kleen8457c842009-02-12 13:49:33 +01001330 if (__ratelimit(&ratelimit))
Huang Yinga2d7b0d2010-06-08 14:35:39 +08001331 pr_info(HW_ERR "Machine check events logged\n");
Tim Hockine02e68d2007-07-21 17:10:36 +02001332
1333 return 1;
1334 }
1335 return 0;
1336}
Andi Kleen9ff36ee2009-05-27 21:56:58 +02001337EXPORT_SYMBOL_GPL(mce_notify_irq);
Tim Hockine02e68d2007-07-21 17:10:36 +02001338
Hidetoshi Setocffd3772009-11-12 15:52:40 +09001339static int __cpuinit __mcheck_cpu_mce_banks_init(void)
Andi Kleencebe1822009-07-09 00:31:43 +02001340{
1341 int i;
1342
1343 mce_banks = kzalloc(banks * sizeof(struct mce_bank), GFP_KERNEL);
1344 if (!mce_banks)
1345 return -ENOMEM;
1346 for (i = 0; i < banks; i++) {
1347 struct mce_bank *b = &mce_banks[i];
Ingo Molnar11868a22009-09-23 17:49:55 +02001348
Andi Kleencebe1822009-07-09 00:31:43 +02001349 b->ctl = -1ULL;
1350 b->init = 1;
1351 }
1352 return 0;
1353}
1354
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001355/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 * Initialize Machine Checks for a CPU.
1357 */
Borislav Petkov5e099542009-10-16 12:31:32 +02001358static int __cpuinit __mcheck_cpu_cap_init(void)
Andi Kleen0d7482e32009-02-17 23:07:13 +01001359{
Andi Kleen0d7482e32009-02-17 23:07:13 +01001360 unsigned b;
Ingo Molnare9eee032009-04-08 12:31:17 +02001361 u64 cap;
Andi Kleen0d7482e32009-02-17 23:07:13 +01001362
1363 rdmsrl(MSR_IA32_MCG_CAP, cap);
Thomas Gleixner01c66802009-04-08 12:31:24 +02001364
1365 b = cap & MCG_BANKCNT_MASK;
Roland Dreier93ae5012009-10-15 14:21:14 -07001366 if (!banks)
Joe Perchesc767a542012-05-21 19:50:07 -07001367 pr_info("CPU supports %d MCE banks\n", b);
Ingo Molnarb6592942009-04-08 12:31:27 +02001368
Andi Kleen0d7482e32009-02-17 23:07:13 +01001369 if (b > MAX_NR_BANKS) {
Joe Perchesc767a542012-05-21 19:50:07 -07001370 pr_warn("Using only %u machine check banks out of %u\n",
Andi Kleen0d7482e32009-02-17 23:07:13 +01001371 MAX_NR_BANKS, b);
1372 b = MAX_NR_BANKS;
1373 }
1374
1375 /* Don't support asymmetric configurations today */
1376 WARN_ON(banks != 0 && b != banks);
1377 banks = b;
Andi Kleencebe1822009-07-09 00:31:43 +02001378 if (!mce_banks) {
Hidetoshi Setocffd3772009-11-12 15:52:40 +09001379 int err = __mcheck_cpu_mce_banks_init();
Ingo Molnar11868a22009-09-23 17:49:55 +02001380
Andi Kleencebe1822009-07-09 00:31:43 +02001381 if (err)
1382 return err;
Andi Kleen0d7482e32009-02-17 23:07:13 +01001383 }
1384
1385 /* Use accurate RIP reporting if available. */
Thomas Gleixner01c66802009-04-08 12:31:24 +02001386 if ((cap & MCG_EXT_P) && MCG_EXT_CNT(cap) >= 9)
Andi Kleen0d7482e32009-02-17 23:07:13 +01001387 rip_msr = MSR_IA32_MCG_EIP;
1388
Andi Kleened7290d2009-05-27 21:56:57 +02001389 if (cap & MCG_SER_P)
1390 mce_ser = 1;
1391
Andi Kleen0d7482e32009-02-17 23:07:13 +01001392 return 0;
1393}
1394
Borislav Petkov5e099542009-10-16 12:31:32 +02001395static void __mcheck_cpu_init_generic(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396{
Ingo Molnare9eee032009-04-08 12:31:17 +02001397 mce_banks_t all_banks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 u64 cap;
1399 int i;
1400
Andi Kleenb79109c2009-02-12 13:43:23 +01001401 /*
1402 * Log the machine checks left over from the previous reset.
1403 */
Andi Kleenee031c32009-02-12 13:49:34 +01001404 bitmap_fill(all_banks, MAX_NR_BANKS);
Andi Kleen5679af42009-04-07 17:06:55 +02001405 machine_check_poll(MCP_UC|(!mce_bootlog ? MCP_DONTLOG : 0), &all_banks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
1407 set_in_cr4(X86_CR4_MCE);
1408
Andi Kleen0d7482e32009-02-17 23:07:13 +01001409 rdmsrl(MSR_IA32_MCG_CAP, cap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 if (cap & MCG_CTL_P)
1411 wrmsr(MSR_IA32_MCG_CTL, 0xffffffff, 0xffffffff);
1412
1413 for (i = 0; i < banks; i++) {
Andi Kleencebe1822009-07-09 00:31:43 +02001414 struct mce_bank *b = &mce_banks[i];
Ingo Molnar11868a22009-09-23 17:49:55 +02001415
Andi Kleencebe1822009-07-09 00:31:43 +02001416 if (!b->init)
Andi Kleen06b7a7a2009-04-27 18:37:43 +02001417 continue;
Andi Kleena2d32bc2009-07-09 00:31:44 +02001418 wrmsrl(MSR_IA32_MCx_CTL(i), b->ctl);
1419 wrmsrl(MSR_IA32_MCx_STATUS(i), 0);
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001420 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421}
1422
1423/* Add per CPU specific workarounds here */
Borislav Petkov5e099542009-10-16 12:31:32 +02001424static int __cpuinit __mcheck_cpu_apply_quirks(struct cpuinfo_x86 *c)
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001425{
Ingo Molnare412cd22009-08-17 10:19:00 +02001426 if (c->x86_vendor == X86_VENDOR_UNKNOWN) {
Joe Perchesc767a542012-05-21 19:50:07 -07001427 pr_info("unknown CPU type - not enabling MCE support\n");
Ingo Molnare412cd22009-08-17 10:19:00 +02001428 return -EOPNOTSUPP;
1429 }
1430
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 /* This should be disabled by the BIOS, but isn't always */
Jan Beulich911f6a72008-04-22 16:22:21 +01001432 if (c->x86_vendor == X86_VENDOR_AMD) {
Ingo Molnare9eee032009-04-08 12:31:17 +02001433 if (c->x86 == 15 && banks > 4) {
1434 /*
1435 * disable GART TBL walk error reporting, which
1436 * trips off incorrectly with the IOMMU & 3ware
1437 * & Cerberus:
1438 */
Andi Kleencebe1822009-07-09 00:31:43 +02001439 clear_bit(10, (unsigned long *)&mce_banks[4].ctl);
Ingo Molnare9eee032009-04-08 12:31:17 +02001440 }
1441 if (c->x86 <= 17 && mce_bootlog < 0) {
1442 /*
1443 * Lots of broken BIOS around that don't clear them
1444 * by default and leave crap in there. Don't log:
1445 */
Jan Beulich911f6a72008-04-22 16:22:21 +01001446 mce_bootlog = 0;
Ingo Molnare9eee032009-04-08 12:31:17 +02001447 }
Andi Kleen2e6f6942009-04-27 18:42:48 +02001448 /*
1449 * Various K7s with broken bank 0 around. Always disable
1450 * by default.
1451 */
Andi Kleen203abd62009-06-15 14:52:01 +02001452 if (c->x86 == 6 && banks > 0)
Andi Kleencebe1822009-07-09 00:31:43 +02001453 mce_banks[0].ctl = 0;
Borislav Petkov575203b2012-04-20 18:01:34 +02001454
1455 /*
1456 * Turn off MC4_MISC thresholding banks on those models since
1457 * they're not supported there.
1458 */
1459 if (c->x86 == 0x15 &&
1460 (c->x86_model >= 0x10 && c->x86_model <= 0x1f)) {
1461 int i;
1462 u64 val, hwcr;
1463 bool need_toggle;
1464 u32 msrs[] = {
1465 0x00000413, /* MC4_MISC0 */
1466 0xc0000408, /* MC4_MISC1 */
1467 };
1468
1469 rdmsrl(MSR_K7_HWCR, hwcr);
1470
1471 /* McStatusWrEn has to be set */
1472 need_toggle = !(hwcr & BIT(18));
1473
1474 if (need_toggle)
1475 wrmsrl(MSR_K7_HWCR, hwcr | BIT(18));
1476
1477 for (i = 0; i < ARRAY_SIZE(msrs); i++) {
1478 rdmsrl(msrs[i], val);
1479
1480 /* CntP bit set? */
Borislav Petkov80f0336102012-05-22 12:53:46 +02001481 if (val & BIT_64(62)) {
1482 val &= ~BIT_64(62);
1483 wrmsrl(msrs[i], val);
Borislav Petkov575203b2012-04-20 18:01:34 +02001484 }
1485 }
1486
1487 /* restore old settings */
1488 if (need_toggle)
1489 wrmsrl(MSR_K7_HWCR, hwcr);
1490 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 }
Andi Kleene5835382005-11-05 17:25:54 +01001492
Andi Kleen06b7a7a2009-04-27 18:37:43 +02001493 if (c->x86_vendor == X86_VENDOR_INTEL) {
1494 /*
1495 * SDM documents that on family 6 bank 0 should not be written
1496 * because it aliases to another special BIOS controlled
1497 * register.
1498 * But it's not aliased anymore on model 0x1a+
1499 * Don't ignore bank 0 completely because there could be a
1500 * valid event later, merely don't write CTL0.
1501 */
1502
Andi Kleencebe1822009-07-09 00:31:43 +02001503 if (c->x86 == 6 && c->x86_model < 0x1A && banks > 0)
1504 mce_banks[0].init = 0;
Andi Kleen3c079792009-05-27 21:56:55 +02001505
1506 /*
1507 * All newer Intel systems support MCE broadcasting. Enable
1508 * synchronization with a one second timeout.
1509 */
1510 if ((c->x86 > 6 || (c->x86 == 6 && c->x86_model >= 0xe)) &&
1511 monarch_timeout < 0)
1512 monarch_timeout = USEC_PER_SEC;
Bartlomiej Zolnierkiewiczc7f6fa42009-07-28 23:52:54 +02001513
Ingo Molnare412cd22009-08-17 10:19:00 +02001514 /*
1515 * There are also broken BIOSes on some Pentium M and
1516 * earlier systems:
1517 */
1518 if (c->x86 == 6 && c->x86_model <= 13 && mce_bootlog < 0)
Bartlomiej Zolnierkiewiczc7f6fa42009-07-28 23:52:54 +02001519 mce_bootlog = 0;
Andi Kleen06b7a7a2009-04-27 18:37:43 +02001520 }
Andi Kleen3c079792009-05-27 21:56:55 +02001521 if (monarch_timeout < 0)
1522 monarch_timeout = 0;
Andi Kleen29b0f592009-05-27 21:56:56 +02001523 if (mce_bootlog != 0)
1524 mce_panic_timeout = 30;
Ingo Molnare412cd22009-08-17 10:19:00 +02001525
1526 return 0;
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001527}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
Hidetoshi Seto3a97fc32011-06-08 10:58:35 +09001529static int __cpuinit __mcheck_cpu_ancient_init(struct cpuinfo_x86 *c)
Andi Kleen4efc0672009-04-28 19:07:31 +02001530{
1531 if (c->x86 != 5)
Hidetoshi Seto3a97fc32011-06-08 10:58:35 +09001532 return 0;
1533
Andi Kleen4efc0672009-04-28 19:07:31 +02001534 switch (c->x86_vendor) {
1535 case X86_VENDOR_INTEL:
Hidetoshi Setoc6978362009-06-15 17:22:49 +09001536 intel_p5_mcheck_init(c);
Hidetoshi Seto3a97fc32011-06-08 10:58:35 +09001537 return 1;
Andi Kleen4efc0672009-04-28 19:07:31 +02001538 break;
1539 case X86_VENDOR_CENTAUR:
1540 winchip_mcheck_init(c);
Hidetoshi Seto3a97fc32011-06-08 10:58:35 +09001541 return 1;
Andi Kleen4efc0672009-04-28 19:07:31 +02001542 break;
1543 }
Hidetoshi Seto3a97fc32011-06-08 10:58:35 +09001544
1545 return 0;
Andi Kleen4efc0672009-04-28 19:07:31 +02001546}
1547
Borislav Petkov5e099542009-10-16 12:31:32 +02001548static void __mcheck_cpu_init_vendor(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549{
1550 switch (c->x86_vendor) {
1551 case X86_VENDOR_INTEL:
1552 mce_intel_feature_init(c);
1553 break;
Jacob Shin89b831e2005-11-05 17:25:53 +01001554 case X86_VENDOR_AMD:
1555 mce_amd_feature_init(c);
1556 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 default:
1558 break;
1559 }
1560}
1561
Borislav Petkov5e099542009-10-16 12:31:32 +02001562static void __mcheck_cpu_init_timer(void)
Andi Kleen52d168e2009-02-12 13:39:29 +01001563{
1564 struct timer_list *t = &__get_cpu_var(mce_timer);
Thomas Gleixner1a87fc12012-06-06 11:33:21 +02001565 unsigned long iv = check_interval * HZ;
Andi Kleen52d168e2009-02-12 13:39:29 +01001566
Thomas Gleixner82f7af02012-05-24 17:54:51 +00001567 setup_timer(t, mce_timer_fn, smp_processor_id());
Jan Beulichbc09eff2009-12-08 11:21:37 +09001568
Hidetoshi Seto62fdac52009-06-11 16:06:07 +09001569 if (mce_ignore_ce)
1570 return;
1571
Thomas Gleixner82f7af02012-05-24 17:54:51 +00001572 __this_cpu_write(mce_next_interval, iv);
1573 if (!iv)
Andi Kleen52d168e2009-02-12 13:39:29 +01001574 return;
Thomas Gleixner82f7af02012-05-24 17:54:51 +00001575 t->expires = round_jiffies(jiffies + iv);
Hidetoshi Seto5be60662009-06-24 09:21:10 +09001576 add_timer_on(t, smp_processor_id());
Andi Kleen52d168e2009-02-12 13:39:29 +01001577}
1578
Andi Kleen9eda8cb2009-07-09 00:31:42 +02001579/* Handle unconfigured int18 (should never happen) */
1580static void unexpected_machine_check(struct pt_regs *regs, long error_code)
1581{
Joe Perchesc767a542012-05-21 19:50:07 -07001582 pr_err("CPU#%d: Unexpected int18 (Machine Check)\n",
Andi Kleen9eda8cb2009-07-09 00:31:42 +02001583 smp_processor_id());
1584}
1585
1586/* Call the installed machine check handler for this CPU setup. */
1587void (*machine_check_vector)(struct pt_regs *, long error_code) =
1588 unexpected_machine_check;
1589
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001590/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 * Called for each booted CPU to set up machine checks.
Ingo Molnare9eee032009-04-08 12:31:17 +02001592 * Must be called with preempt off:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 */
Borislav Petkov5e099542009-10-16 12:31:32 +02001594void __cpuinit mcheck_cpu_init(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595{
Andi Kleen4efc0672009-04-28 19:07:31 +02001596 if (mce_disabled)
1597 return;
1598
Hidetoshi Seto3a97fc32011-06-08 10:58:35 +09001599 if (__mcheck_cpu_ancient_init(c))
1600 return;
Andi Kleen4efc0672009-04-28 19:07:31 +02001601
Andi Kleen5b4408f2009-02-12 13:39:30 +01001602 if (!mce_available(c))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 return;
1604
Borislav Petkov5e099542009-10-16 12:31:32 +02001605 if (__mcheck_cpu_cap_init() < 0 || __mcheck_cpu_apply_quirks(c) < 0) {
Andi Kleen04b2b1a2009-04-28 22:50:19 +02001606 mce_disabled = 1;
Andi Kleen0d7482e32009-02-17 23:07:13 +01001607 return;
1608 }
Andi Kleen0d7482e32009-02-17 23:07:13 +01001609
Andi Kleen5d727922009-04-27 19:25:48 +02001610 machine_check_vector = do_machine_check;
1611
Borislav Petkov5e099542009-10-16 12:31:32 +02001612 __mcheck_cpu_init_generic();
1613 __mcheck_cpu_init_vendor(c);
1614 __mcheck_cpu_init_timer();
Andi Kleen9b1beaf2009-05-27 21:56:59 +02001615 INIT_WORK(&__get_cpu_var(mce_work), mce_process_work);
Hidetoshi Setob77e70b2011-06-08 10:56:02 +09001616 init_irq_work(&__get_cpu_var(mce_irq_work), &mce_irq_work_cb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617}
1618
1619/*
Hidetoshi Seto93b62c32011-06-08 11:00:45 +09001620 * mce_chrdev: Character device /dev/mcelog to read and clear the MCE log.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 */
1622
Hidetoshi Seto93b62c32011-06-08 11:00:45 +09001623static DEFINE_SPINLOCK(mce_chrdev_state_lock);
1624static int mce_chrdev_open_count; /* #times opened */
1625static int mce_chrdev_open_exclu; /* already open exclusive? */
Tim Hockinf528e7b2007-07-21 17:10:35 +02001626
Hidetoshi Seto93b62c32011-06-08 11:00:45 +09001627static int mce_chrdev_open(struct inode *inode, struct file *file)
Tim Hockinf528e7b2007-07-21 17:10:35 +02001628{
Hidetoshi Seto93b62c32011-06-08 11:00:45 +09001629 spin_lock(&mce_chrdev_state_lock);
Tim Hockinf528e7b2007-07-21 17:10:35 +02001630
Hidetoshi Seto93b62c32011-06-08 11:00:45 +09001631 if (mce_chrdev_open_exclu ||
1632 (mce_chrdev_open_count && (file->f_flags & O_EXCL))) {
1633 spin_unlock(&mce_chrdev_state_lock);
Ingo Molnare9eee032009-04-08 12:31:17 +02001634
Tim Hockinf528e7b2007-07-21 17:10:35 +02001635 return -EBUSY;
1636 }
1637
1638 if (file->f_flags & O_EXCL)
Hidetoshi Seto93b62c32011-06-08 11:00:45 +09001639 mce_chrdev_open_exclu = 1;
1640 mce_chrdev_open_count++;
Tim Hockinf528e7b2007-07-21 17:10:35 +02001641
Hidetoshi Seto93b62c32011-06-08 11:00:45 +09001642 spin_unlock(&mce_chrdev_state_lock);
Tim Hockinf528e7b2007-07-21 17:10:35 +02001643
Tim Hockinbd784322007-07-21 17:10:37 +02001644 return nonseekable_open(inode, file);
Tim Hockinf528e7b2007-07-21 17:10:35 +02001645}
1646
Hidetoshi Seto93b62c32011-06-08 11:00:45 +09001647static int mce_chrdev_release(struct inode *inode, struct file *file)
Tim Hockinf528e7b2007-07-21 17:10:35 +02001648{
Hidetoshi Seto93b62c32011-06-08 11:00:45 +09001649 spin_lock(&mce_chrdev_state_lock);
Tim Hockinf528e7b2007-07-21 17:10:35 +02001650
Hidetoshi Seto93b62c32011-06-08 11:00:45 +09001651 mce_chrdev_open_count--;
1652 mce_chrdev_open_exclu = 0;
Tim Hockinf528e7b2007-07-21 17:10:35 +02001653
Hidetoshi Seto93b62c32011-06-08 11:00:45 +09001654 spin_unlock(&mce_chrdev_state_lock);
Tim Hockinf528e7b2007-07-21 17:10:35 +02001655
1656 return 0;
1657}
1658
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001659static void collect_tscs(void *data)
1660{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 unsigned long *cpu_tsc = (unsigned long *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001663 rdtscll(cpu_tsc[smp_processor_id()]);
1664}
1665
Huang Ying482908b2010-05-18 14:35:22 +08001666static int mce_apei_read_done;
1667
1668/* Collect MCE record of previous boot in persistent storage via APEI ERST. */
1669static int __mce_read_apei(char __user **ubuf, size_t usize)
1670{
1671 int rc;
1672 u64 record_id;
1673 struct mce m;
1674
1675 if (usize < sizeof(struct mce))
1676 return -EINVAL;
1677
1678 rc = apei_read_mce(&m, &record_id);
1679 /* Error or no more MCE record */
1680 if (rc <= 0) {
1681 mce_apei_read_done = 1;
Naoya Horiguchifadd85f2012-01-23 15:54:52 -05001682 /*
1683 * When ERST is disabled, mce_chrdev_read() should return
1684 * "no record" instead of "no device."
1685 */
1686 if (rc == -ENODEV)
1687 return 0;
Huang Ying482908b2010-05-18 14:35:22 +08001688 return rc;
1689 }
1690 rc = -EFAULT;
1691 if (copy_to_user(*ubuf, &m, sizeof(struct mce)))
1692 return rc;
1693 /*
1694 * In fact, we should have cleared the record after that has
1695 * been flushed to the disk or sent to network in
1696 * /sbin/mcelog, but we have no interface to support that now,
1697 * so just clear it to avoid duplication.
1698 */
1699 rc = apei_clear_mce(record_id);
1700 if (rc) {
1701 mce_apei_read_done = 1;
1702 return rc;
1703 }
1704 *ubuf += sizeof(struct mce);
1705
1706 return 0;
1707}
1708
Hidetoshi Seto93b62c32011-06-08 11:00:45 +09001709static ssize_t mce_chrdev_read(struct file *filp, char __user *ubuf,
1710 size_t usize, loff_t *off)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 char __user *buf = ubuf;
Ingo Molnare9eee032009-04-08 12:31:17 +02001713 unsigned long *cpu_tsc;
1714 unsigned prev, next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 int i, err;
1716
Mike Travis6bca67f2008-07-18 18:11:27 -07001717 cpu_tsc = kmalloc(nr_cpu_ids * sizeof(long), GFP_KERNEL);
Andi Kleenf0de53b2005-04-16 15:25:10 -07001718 if (!cpu_tsc)
1719 return -ENOMEM;
1720
Hidetoshi Seto93b62c32011-06-08 11:00:45 +09001721 mutex_lock(&mce_chrdev_read_mutex);
Huang Ying482908b2010-05-18 14:35:22 +08001722
1723 if (!mce_apei_read_done) {
1724 err = __mce_read_apei(&buf, usize);
1725 if (err || buf != ubuf)
1726 goto out;
1727 }
1728
Paul E. McKenneyf56e8a02010-03-05 15:03:27 -08001729 next = rcu_dereference_check_mce(mcelog.next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730
1731 /* Only supports full reads right now */
Huang Ying482908b2010-05-18 14:35:22 +08001732 err = -EINVAL;
1733 if (*off != 0 || usize < MCE_LOG_LEN*sizeof(struct mce))
1734 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735
1736 err = 0;
Huang Yingef41df4342009-02-12 13:39:34 +01001737 prev = 0;
1738 do {
1739 for (i = prev; i < next; i++) {
1740 unsigned long start = jiffies;
Hidetoshi Seto559faa62011-06-08 11:00:08 +09001741 struct mce *m = &mcelog.entry[i];
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001742
Hidetoshi Seto559faa62011-06-08 11:00:08 +09001743 while (!m->finished) {
Huang Yingef41df4342009-02-12 13:39:34 +01001744 if (time_after_eq(jiffies, start + 2)) {
Hidetoshi Seto559faa62011-06-08 11:00:08 +09001745 memset(m, 0, sizeof(*m));
Huang Yingef41df4342009-02-12 13:39:34 +01001746 goto timeout;
1747 }
1748 cpu_relax();
Andi Kleen673242c2005-09-12 18:49:24 +02001749 }
Huang Yingef41df4342009-02-12 13:39:34 +01001750 smp_rmb();
Hidetoshi Seto559faa62011-06-08 11:00:08 +09001751 err |= copy_to_user(buf, m, sizeof(*m));
1752 buf += sizeof(*m);
Huang Yingef41df4342009-02-12 13:39:34 +01001753timeout:
1754 ;
Andi Kleen673242c2005-09-12 18:49:24 +02001755 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756
Huang Yingef41df4342009-02-12 13:39:34 +01001757 memset(mcelog.entry + prev, 0,
1758 (next - prev) * sizeof(struct mce));
1759 prev = next;
1760 next = cmpxchg(&mcelog.next, prev, 0);
1761 } while (next != prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762
Paul E. McKenneyb2b18662005-06-25 14:55:38 -07001763 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001765 /*
1766 * Collect entries that were still getting written before the
1767 * synchronize.
1768 */
Jens Axboe15c8b6c2008-05-09 09:39:44 +02001769 on_each_cpu(collect_tscs, cpu_tsc, 1);
Ingo Molnare9eee032009-04-08 12:31:17 +02001770
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001771 for (i = next; i < MCE_LOG_LEN; i++) {
Hidetoshi Seto559faa62011-06-08 11:00:08 +09001772 struct mce *m = &mcelog.entry[i];
1773
1774 if (m->finished && m->tsc < cpu_tsc[m->cpu]) {
1775 err |= copy_to_user(buf, m, sizeof(*m));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 smp_rmb();
Hidetoshi Seto559faa62011-06-08 11:00:08 +09001777 buf += sizeof(*m);
1778 memset(m, 0, sizeof(*m));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 }
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001780 }
Huang Ying482908b2010-05-18 14:35:22 +08001781
1782 if (err)
1783 err = -EFAULT;
1784
1785out:
Hidetoshi Seto93b62c32011-06-08 11:00:45 +09001786 mutex_unlock(&mce_chrdev_read_mutex);
Andi Kleenf0de53b2005-04-16 15:25:10 -07001787 kfree(cpu_tsc);
Ingo Molnare9eee032009-04-08 12:31:17 +02001788
Huang Ying482908b2010-05-18 14:35:22 +08001789 return err ? err : buf - ubuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790}
1791
Hidetoshi Seto93b62c32011-06-08 11:00:45 +09001792static unsigned int mce_chrdev_poll(struct file *file, poll_table *wait)
Tim Hockine02e68d2007-07-21 17:10:36 +02001793{
Hidetoshi Seto93b62c32011-06-08 11:00:45 +09001794 poll_wait(file, &mce_chrdev_wait, wait);
Paul E. McKenneya4dd9922011-04-01 07:15:14 -07001795 if (rcu_access_index(mcelog.next))
Tim Hockine02e68d2007-07-21 17:10:36 +02001796 return POLLIN | POLLRDNORM;
Huang Ying482908b2010-05-18 14:35:22 +08001797 if (!mce_apei_read_done && apei_check_mce())
1798 return POLLIN | POLLRDNORM;
Tim Hockine02e68d2007-07-21 17:10:36 +02001799 return 0;
1800}
1801
Hidetoshi Seto93b62c32011-06-08 11:00:45 +09001802static long mce_chrdev_ioctl(struct file *f, unsigned int cmd,
1803 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804{
1805 int __user *p = (int __user *)arg;
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001806
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 if (!capable(CAP_SYS_ADMIN))
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001808 return -EPERM;
Ingo Molnare9eee032009-04-08 12:31:17 +02001809
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 switch (cmd) {
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001811 case MCE_GET_RECORD_LEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 return put_user(sizeof(struct mce), p);
1813 case MCE_GET_LOG_LEN:
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001814 return put_user(MCE_LOG_LEN, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 case MCE_GETCLEAR_FLAGS: {
1816 unsigned flags;
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001817
1818 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 flags = mcelog.flags;
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001820 } while (cmpxchg(&mcelog.flags, flags, 0) != flags);
Ingo Molnare9eee032009-04-08 12:31:17 +02001821
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001822 return put_user(flags, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 }
1824 default:
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001825 return -ENOTTY;
1826 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827}
1828
Luck, Tony66f5ddf2011-11-03 11:46:47 -07001829static ssize_t (*mce_write)(struct file *filp, const char __user *ubuf,
1830 size_t usize, loff_t *off);
1831
1832void register_mce_write_callback(ssize_t (*fn)(struct file *filp,
1833 const char __user *ubuf,
1834 size_t usize, loff_t *off))
1835{
1836 mce_write = fn;
1837}
1838EXPORT_SYMBOL_GPL(register_mce_write_callback);
1839
1840ssize_t mce_chrdev_write(struct file *filp, const char __user *ubuf,
1841 size_t usize, loff_t *off)
1842{
1843 if (mce_write)
1844 return mce_write(filp, ubuf, usize, off);
1845 else
1846 return -EINVAL;
1847}
1848
1849static const struct file_operations mce_chrdev_ops = {
Hidetoshi Seto93b62c32011-06-08 11:00:45 +09001850 .open = mce_chrdev_open,
1851 .release = mce_chrdev_release,
1852 .read = mce_chrdev_read,
Luck, Tony66f5ddf2011-11-03 11:46:47 -07001853 .write = mce_chrdev_write,
Hidetoshi Seto93b62c32011-06-08 11:00:45 +09001854 .poll = mce_chrdev_poll,
1855 .unlocked_ioctl = mce_chrdev_ioctl,
1856 .llseek = no_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857};
1858
Hidetoshi Seto93b62c32011-06-08 11:00:45 +09001859static struct miscdevice mce_chrdev_device = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 MISC_MCELOG_MINOR,
1861 "mcelog",
1862 &mce_chrdev_ops,
1863};
1864
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001865/*
Hidetoshi Seto62fdac52009-06-11 16:06:07 +09001866 * mce=off Disables machine check
1867 * mce=no_cmci Disables CMCI
1868 * mce=dont_log_ce Clears corrected events silently, no log created for CEs.
1869 * mce=ignore_ce Disables polling and CMCI, corrected events are not cleared.
Andi Kleen3c079792009-05-27 21:56:55 +02001870 * mce=TOLERANCELEVEL[,monarchtimeout] (number, see above)
1871 * monarchtimeout is how long to wait for other CPUs on machine
1872 * check, or 0 to not wait
Hidetoshi Seto13503fa2009-03-26 17:39:20 +09001873 * mce=bootlog Log MCEs from before booting. Disabled by default on AMD.
1874 * mce=nobootlog Don't log MCEs from before booting.
1875 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876static int __init mcheck_enable(char *str)
1877{
Bartlomiej Zolnierkiewicze3346fc2009-07-28 23:55:09 +02001878 if (*str == 0) {
Andi Kleen4efc0672009-04-28 19:07:31 +02001879 enable_p5_mce();
Bartlomiej Zolnierkiewicze3346fc2009-07-28 23:55:09 +02001880 return 1;
1881 }
Andi Kleen4efc0672009-04-28 19:07:31 +02001882 if (*str == '=')
1883 str++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 if (!strcmp(str, "off"))
Andi Kleen04b2b1a2009-04-28 22:50:19 +02001885 mce_disabled = 1;
Hidetoshi Seto62fdac52009-06-11 16:06:07 +09001886 else if (!strcmp(str, "no_cmci"))
1887 mce_cmci_disabled = 1;
1888 else if (!strcmp(str, "dont_log_ce"))
1889 mce_dont_log_ce = 1;
1890 else if (!strcmp(str, "ignore_ce"))
1891 mce_ignore_ce = 1;
Hidetoshi Seto13503fa2009-03-26 17:39:20 +09001892 else if (!strcmp(str, "bootlog") || !strcmp(str, "nobootlog"))
1893 mce_bootlog = (str[0] == 'b');
Andi Kleen3c079792009-05-27 21:56:55 +02001894 else if (isdigit(str[0])) {
Andi Kleen8c566ef2005-09-12 18:49:24 +02001895 get_option(&str, &tolerant);
Andi Kleen3c079792009-05-27 21:56:55 +02001896 if (*str == ',') {
1897 ++str;
1898 get_option(&str, &monarch_timeout);
1899 }
1900 } else {
Joe Perchesc767a542012-05-21 19:50:07 -07001901 pr_info("mce argument %s ignored. Please use /sys\n", str);
Hidetoshi Seto13503fa2009-03-26 17:39:20 +09001902 return 0;
1903 }
OGAWA Hirofumi9b410462006-03-31 02:30:33 -08001904 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905}
Andi Kleen4efc0672009-04-28 19:07:31 +02001906__setup("mce", mcheck_enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907
Yong Wanga2202aa2009-11-10 09:38:24 +08001908int __init mcheck_init(void)
Borislav Petkovb33a6362009-10-16 12:31:33 +02001909{
Yong Wanga2202aa2009-11-10 09:38:24 +08001910 mcheck_intel_therm_init();
1911
Borislav Petkovb33a6362009-10-16 12:31:33 +02001912 return 0;
1913}
Borislav Petkovb33a6362009-10-16 12:31:33 +02001914
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001915/*
Hidetoshi Setoc7cece82011-06-08 11:02:03 +09001916 * mce_syscore: PM support
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001917 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918
Andi Kleen973a2dd2009-02-12 13:39:32 +01001919/*
1920 * Disable machine checks on suspend and shutdown. We can't really handle
1921 * them later.
1922 */
Borislav Petkov5e099542009-10-16 12:31:32 +02001923static int mce_disable_error_reporting(void)
Andi Kleen973a2dd2009-02-12 13:39:32 +01001924{
1925 int i;
1926
Andi Kleen06b7a7a2009-04-27 18:37:43 +02001927 for (i = 0; i < banks; i++) {
Andi Kleencebe1822009-07-09 00:31:43 +02001928 struct mce_bank *b = &mce_banks[i];
Ingo Molnar11868a22009-09-23 17:49:55 +02001929
Andi Kleencebe1822009-07-09 00:31:43 +02001930 if (b->init)
Andi Kleena2d32bc2009-07-09 00:31:44 +02001931 wrmsrl(MSR_IA32_MCx_CTL(i), 0);
Andi Kleen06b7a7a2009-04-27 18:37:43 +02001932 }
Andi Kleen973a2dd2009-02-12 13:39:32 +01001933 return 0;
1934}
1935
Hidetoshi Setoc7cece82011-06-08 11:02:03 +09001936static int mce_syscore_suspend(void)
Andi Kleen973a2dd2009-02-12 13:39:32 +01001937{
Borislav Petkov5e099542009-10-16 12:31:32 +02001938 return mce_disable_error_reporting();
Andi Kleen973a2dd2009-02-12 13:39:32 +01001939}
1940
Hidetoshi Setoc7cece82011-06-08 11:02:03 +09001941static void mce_syscore_shutdown(void)
Andi Kleen973a2dd2009-02-12 13:39:32 +01001942{
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +01001943 mce_disable_error_reporting();
Andi Kleen973a2dd2009-02-12 13:39:32 +01001944}
1945
Ingo Molnare9eee032009-04-08 12:31:17 +02001946/*
1947 * On resume clear all MCE state. Don't want to see leftovers from the BIOS.
1948 * Only one CPU is active at this time, the others get re-added later using
1949 * CPU hotplug:
1950 */
Hidetoshi Setoc7cece82011-06-08 11:02:03 +09001951static void mce_syscore_resume(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952{
Borislav Petkov5e099542009-10-16 12:31:32 +02001953 __mcheck_cpu_init_generic();
Tejun Heo7b543a52010-12-18 16:30:05 +01001954 __mcheck_cpu_init_vendor(__this_cpu_ptr(&cpu_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955}
1956
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +01001957static struct syscore_ops mce_syscore_ops = {
Hidetoshi Setoc7cece82011-06-08 11:02:03 +09001958 .suspend = mce_syscore_suspend,
1959 .shutdown = mce_syscore_shutdown,
1960 .resume = mce_syscore_resume,
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +01001961};
1962
Hidetoshi Setoc7cece82011-06-08 11:02:03 +09001963/*
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001964 * mce_device: Sysfs support
Hidetoshi Setoc7cece82011-06-08 11:02:03 +09001965 */
1966
Andi Kleen52d168e2009-02-12 13:39:29 +01001967static void mce_cpu_restart(void *data)
1968{
Tejun Heo7b543a52010-12-18 16:30:05 +01001969 if (!mce_available(__this_cpu_ptr(&cpu_info)))
Hidetoshi Seto33edbf02009-06-15 17:18:45 +09001970 return;
Borislav Petkov5e099542009-10-16 12:31:32 +02001971 __mcheck_cpu_init_generic();
1972 __mcheck_cpu_init_timer();
Andi Kleen52d168e2009-02-12 13:39:29 +01001973}
1974
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975/* Reinit MCEs after user configuration changes */
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001976static void mce_restart(void)
1977{
Hidetoshi Seto9aaef962011-06-17 04:40:36 -04001978 mce_timer_delete_all();
Andi Kleen52d168e2009-02-12 13:39:29 +01001979 on_each_cpu(mce_cpu_restart, NULL, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980}
1981
Hidetoshi Seto9af43b52009-06-15 17:21:36 +09001982/* Toggle features for corrected errors */
Hidetoshi Seto9aaef962011-06-17 04:40:36 -04001983static void mce_disable_cmci(void *data)
Hidetoshi Seto9af43b52009-06-15 17:21:36 +09001984{
Tejun Heo7b543a52010-12-18 16:30:05 +01001985 if (!mce_available(__this_cpu_ptr(&cpu_info)))
Hidetoshi Seto9af43b52009-06-15 17:21:36 +09001986 return;
Hidetoshi Seto9af43b52009-06-15 17:21:36 +09001987 cmci_clear();
1988}
1989
1990static void mce_enable_ce(void *all)
1991{
Tejun Heo7b543a52010-12-18 16:30:05 +01001992 if (!mce_available(__this_cpu_ptr(&cpu_info)))
Hidetoshi Seto9af43b52009-06-15 17:21:36 +09001993 return;
1994 cmci_reenable();
1995 cmci_recheck();
1996 if (all)
Borislav Petkov5e099542009-10-16 12:31:32 +02001997 __mcheck_cpu_init_timer();
Hidetoshi Seto9af43b52009-06-15 17:21:36 +09001998}
1999
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002000static struct bus_type mce_subsys = {
Ingo Molnare9eee032009-04-08 12:31:17 +02002001 .name = "machinecheck",
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002002 .dev_name = "machinecheck",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003};
2004
Greg Kroah-Hartmand6126ef2012-01-26 15:49:14 -08002005DEFINE_PER_CPU(struct device *, mce_device);
Ingo Molnare9eee032009-04-08 12:31:17 +02002006
2007__cpuinitdata
2008void (*threshold_cpu_callback)(unsigned long action, unsigned int cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002010static inline struct mce_bank *attr_to_bank(struct device_attribute *attr)
Andi Kleencebe1822009-07-09 00:31:43 +02002011{
2012 return container_of(attr, struct mce_bank, attr);
2013}
Andi Kleen0d7482e32009-02-17 23:07:13 +01002014
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002015static ssize_t show_bank(struct device *s, struct device_attribute *attr,
Andi Kleen0d7482e32009-02-17 23:07:13 +01002016 char *buf)
2017{
Andi Kleencebe1822009-07-09 00:31:43 +02002018 return sprintf(buf, "%llx\n", attr_to_bank(attr)->ctl);
Andi Kleen0d7482e32009-02-17 23:07:13 +01002019}
2020
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002021static ssize_t set_bank(struct device *s, struct device_attribute *attr,
Hidetoshi Seto9319cec2009-04-14 17:26:30 +09002022 const char *buf, size_t size)
Andi Kleen0d7482e32009-02-17 23:07:13 +01002023{
Hidetoshi Seto9319cec2009-04-14 17:26:30 +09002024 u64 new;
Ingo Molnare9eee032009-04-08 12:31:17 +02002025
Hidetoshi Seto9319cec2009-04-14 17:26:30 +09002026 if (strict_strtoull(buf, 0, &new) < 0)
Andi Kleen0d7482e32009-02-17 23:07:13 +01002027 return -EINVAL;
Ingo Molnare9eee032009-04-08 12:31:17 +02002028
Andi Kleencebe1822009-07-09 00:31:43 +02002029 attr_to_bank(attr)->ctl = new;
Andi Kleen0d7482e32009-02-17 23:07:13 +01002030 mce_restart();
Ingo Molnare9eee032009-04-08 12:31:17 +02002031
Hidetoshi Seto9319cec2009-04-14 17:26:30 +09002032 return size;
Andi Kleen0d7482e32009-02-17 23:07:13 +01002033}
Andi Kleena98f0dd2007-02-13 13:26:23 +01002034
Ingo Molnare9eee032009-04-08 12:31:17 +02002035static ssize_t
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002036show_trigger(struct device *s, struct device_attribute *attr, char *buf)
Andi Kleena98f0dd2007-02-13 13:26:23 +01002037{
Hidetoshi Seto1020bcb2009-06-15 17:20:57 +09002038 strcpy(buf, mce_helper);
Andi Kleena98f0dd2007-02-13 13:26:23 +01002039 strcat(buf, "\n");
Hidetoshi Seto1020bcb2009-06-15 17:20:57 +09002040 return strlen(mce_helper) + 1;
Andi Kleena98f0dd2007-02-13 13:26:23 +01002041}
2042
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002043static ssize_t set_trigger(struct device *s, struct device_attribute *attr,
Ingo Molnare9eee032009-04-08 12:31:17 +02002044 const char *buf, size_t siz)
Andi Kleena98f0dd2007-02-13 13:26:23 +01002045{
2046 char *p;
Ingo Molnare9eee032009-04-08 12:31:17 +02002047
Hidetoshi Seto1020bcb2009-06-15 17:20:57 +09002048 strncpy(mce_helper, buf, sizeof(mce_helper));
2049 mce_helper[sizeof(mce_helper)-1] = 0;
Hidetoshi Seto1020bcb2009-06-15 17:20:57 +09002050 p = strchr(mce_helper, '\n');
Ingo Molnare9eee032009-04-08 12:31:17 +02002051
Jan Beuliche9084ec2009-07-16 09:45:11 +01002052 if (p)
Ingo Molnare9eee032009-04-08 12:31:17 +02002053 *p = 0;
2054
Jan Beuliche9084ec2009-07-16 09:45:11 +01002055 return strlen(mce_helper) + !!p;
Andi Kleena98f0dd2007-02-13 13:26:23 +01002056}
2057
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002058static ssize_t set_ignore_ce(struct device *s,
2059 struct device_attribute *attr,
Hidetoshi Seto9af43b52009-06-15 17:21:36 +09002060 const char *buf, size_t size)
2061{
2062 u64 new;
2063
2064 if (strict_strtoull(buf, 0, &new) < 0)
2065 return -EINVAL;
2066
2067 if (mce_ignore_ce ^ !!new) {
2068 if (new) {
2069 /* disable ce features */
Hidetoshi Seto9aaef962011-06-17 04:40:36 -04002070 mce_timer_delete_all();
2071 on_each_cpu(mce_disable_cmci, NULL, 1);
Hidetoshi Seto9af43b52009-06-15 17:21:36 +09002072 mce_ignore_ce = 1;
2073 } else {
2074 /* enable ce features */
2075 mce_ignore_ce = 0;
2076 on_each_cpu(mce_enable_ce, (void *)1, 1);
2077 }
2078 }
2079 return size;
2080}
2081
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002082static ssize_t set_cmci_disabled(struct device *s,
2083 struct device_attribute *attr,
Hidetoshi Seto9af43b52009-06-15 17:21:36 +09002084 const char *buf, size_t size)
2085{
2086 u64 new;
2087
2088 if (strict_strtoull(buf, 0, &new) < 0)
2089 return -EINVAL;
2090
2091 if (mce_cmci_disabled ^ !!new) {
2092 if (new) {
2093 /* disable cmci */
Hidetoshi Seto9aaef962011-06-17 04:40:36 -04002094 on_each_cpu(mce_disable_cmci, NULL, 1);
Hidetoshi Seto9af43b52009-06-15 17:21:36 +09002095 mce_cmci_disabled = 1;
2096 } else {
2097 /* enable cmci */
2098 mce_cmci_disabled = 0;
2099 on_each_cpu(mce_enable_ce, NULL, 1);
2100 }
2101 }
2102 return size;
2103}
2104
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002105static ssize_t store_int_with_restart(struct device *s,
2106 struct device_attribute *attr,
Andi Kleenb56f6422009-05-27 21:56:52 +02002107 const char *buf, size_t size)
2108{
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002109 ssize_t ret = device_store_int(s, attr, buf, size);
Andi Kleenb56f6422009-05-27 21:56:52 +02002110 mce_restart();
2111 return ret;
2112}
2113
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002114static DEVICE_ATTR(trigger, 0644, show_trigger, set_trigger);
2115static DEVICE_INT_ATTR(tolerant, 0644, tolerant);
2116static DEVICE_INT_ATTR(monarch_timeout, 0644, monarch_timeout);
2117static DEVICE_INT_ATTR(dont_log_ce, 0644, mce_dont_log_ce);
Ingo Molnare9eee032009-04-08 12:31:17 +02002118
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002119static struct dev_ext_attribute dev_attr_check_interval = {
2120 __ATTR(check_interval, 0644, device_show_int, store_int_with_restart),
Andi Kleenb56f6422009-05-27 21:56:52 +02002121 &check_interval
2122};
Ingo Molnare9eee032009-04-08 12:31:17 +02002123
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002124static struct dev_ext_attribute dev_attr_ignore_ce = {
2125 __ATTR(ignore_ce, 0644, device_show_int, set_ignore_ce),
Hidetoshi Seto9af43b52009-06-15 17:21:36 +09002126 &mce_ignore_ce
2127};
2128
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002129static struct dev_ext_attribute dev_attr_cmci_disabled = {
2130 __ATTR(cmci_disabled, 0644, device_show_int, set_cmci_disabled),
Hidetoshi Seto9af43b52009-06-15 17:21:36 +09002131 &mce_cmci_disabled
2132};
2133
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002134static struct device_attribute *mce_device_attrs[] = {
2135 &dev_attr_tolerant.attr,
2136 &dev_attr_check_interval.attr,
2137 &dev_attr_trigger,
2138 &dev_attr_monarch_timeout.attr,
2139 &dev_attr_dont_log_ce.attr,
2140 &dev_attr_ignore_ce.attr,
2141 &dev_attr_cmci_disabled.attr,
Andi Kleena98f0dd2007-02-13 13:26:23 +01002142 NULL
2143};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002145static cpumask_var_t mce_device_initialized;
Andreas Herrmannbae19fe2007-11-14 17:00:44 -08002146
Greg Kroah-Hartmane032d8072012-01-16 14:40:28 -08002147static void mce_device_release(struct device *dev)
2148{
2149 kfree(dev);
2150}
2151
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002152/* Per cpu device init. All of the cpus still share the same ctrl bank: */
2153static __cpuinit int mce_device_create(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154{
Greg Kroah-Hartmane032d8072012-01-16 14:40:28 -08002155 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 int err;
Hidetoshi Setob1f49f92009-06-18 14:53:24 +09002157 int i, j;
Mike Travis92cb7612007-10-19 20:35:04 +02002158
Andreas Herrmann90367552007-11-07 02:12:58 +01002159 if (!mce_available(&boot_cpu_data))
Andi Kleen91c6d402005-07-28 21:15:39 -07002160 return -EIO;
2161
Greg Kroah-Hartmane032d8072012-01-16 14:40:28 -08002162 dev = kzalloc(sizeof *dev, GFP_KERNEL);
2163 if (!dev)
2164 return -ENOMEM;
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002165 dev->id = cpu;
2166 dev->bus = &mce_subsys;
Greg Kroah-Hartmane032d8072012-01-16 14:40:28 -08002167 dev->release = &mce_device_release;
Andi Kleen91c6d402005-07-28 21:15:39 -07002168
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002169 err = device_register(dev);
Akinobu Mitad435d862007-10-18 03:05:15 -07002170 if (err)
2171 return err;
Andi Kleen91c6d402005-07-28 21:15:39 -07002172
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002173 for (i = 0; mce_device_attrs[i]; i++) {
2174 err = device_create_file(dev, mce_device_attrs[i]);
Akinobu Mitad435d862007-10-18 03:05:15 -07002175 if (err)
2176 goto error;
Andi Kleen91c6d402005-07-28 21:15:39 -07002177 }
Hidetoshi Setob1f49f92009-06-18 14:53:24 +09002178 for (j = 0; j < banks; j++) {
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002179 err = device_create_file(dev, &mce_banks[j].attr);
Andi Kleen0d7482e32009-02-17 23:07:13 +01002180 if (err)
2181 goto error2;
2182 }
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002183 cpumask_set_cpu(cpu, mce_device_initialized);
Greg Kroah-Hartmand6126ef2012-01-26 15:49:14 -08002184 per_cpu(mce_device, cpu) = dev;
Akinobu Mitad435d862007-10-18 03:05:15 -07002185
2186 return 0;
Andi Kleen0d7482e32009-02-17 23:07:13 +01002187error2:
Hidetoshi Setob1f49f92009-06-18 14:53:24 +09002188 while (--j >= 0)
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002189 device_remove_file(dev, &mce_banks[j].attr);
Akinobu Mitad435d862007-10-18 03:05:15 -07002190error:
Ingo Molnarcb491fc2009-04-08 12:31:17 +02002191 while (--i >= 0)
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002192 device_remove_file(dev, mce_device_attrs[i]);
Ingo Molnarcb491fc2009-04-08 12:31:17 +02002193
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002194 device_unregister(dev);
Akinobu Mitad435d862007-10-18 03:05:15 -07002195
Andi Kleen91c6d402005-07-28 21:15:39 -07002196 return err;
2197}
2198
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002199static __cpuinit void mce_device_remove(unsigned int cpu)
Andi Kleen91c6d402005-07-28 21:15:39 -07002200{
Greg Kroah-Hartmand6126ef2012-01-26 15:49:14 -08002201 struct device *dev = per_cpu(mce_device, cpu);
Shaohua Li73ca5352006-01-11 22:43:06 +01002202 int i;
2203
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002204 if (!cpumask_test_cpu(cpu, mce_device_initialized))
Andreas Herrmannbae19fe2007-11-14 17:00:44 -08002205 return;
2206
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002207 for (i = 0; mce_device_attrs[i]; i++)
2208 device_remove_file(dev, mce_device_attrs[i]);
Ingo Molnarcb491fc2009-04-08 12:31:17 +02002209
Andi Kleen0d7482e32009-02-17 23:07:13 +01002210 for (i = 0; i < banks; i++)
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002211 device_remove_file(dev, &mce_banks[i].attr);
Ingo Molnarcb491fc2009-04-08 12:31:17 +02002212
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002213 device_unregister(dev);
2214 cpumask_clear_cpu(cpu, mce_device_initialized);
Greg Kroah-Hartmand6126ef2012-01-26 15:49:14 -08002215 per_cpu(mce_device, cpu) = NULL;
Andi Kleen91c6d402005-07-28 21:15:39 -07002216}
Andi Kleen91c6d402005-07-28 21:15:39 -07002217
Andi Kleend6b75582009-02-12 13:39:31 +01002218/* Make sure there are no machine checks on offlined CPUs. */
Hidetoshi Seto767df1b2009-11-26 17:29:02 +09002219static void __cpuinit mce_disable_cpu(void *h)
Andi Kleend6b75582009-02-12 13:39:31 +01002220{
Andi Kleen88ccbed2009-02-12 13:49:36 +01002221 unsigned long action = *(unsigned long *)h;
Ingo Molnarcb491fc2009-04-08 12:31:17 +02002222 int i;
Andi Kleend6b75582009-02-12 13:39:31 +01002223
Tejun Heo7b543a52010-12-18 16:30:05 +01002224 if (!mce_available(__this_cpu_ptr(&cpu_info)))
Andi Kleend6b75582009-02-12 13:39:31 +01002225 return;
Hidetoshi Seto767df1b2009-11-26 17:29:02 +09002226
Andi Kleen88ccbed2009-02-12 13:49:36 +01002227 if (!(action & CPU_TASKS_FROZEN))
2228 cmci_clear();
Andi Kleen06b7a7a2009-04-27 18:37:43 +02002229 for (i = 0; i < banks; i++) {
Andi Kleencebe1822009-07-09 00:31:43 +02002230 struct mce_bank *b = &mce_banks[i];
Ingo Molnar11868a22009-09-23 17:49:55 +02002231
Andi Kleencebe1822009-07-09 00:31:43 +02002232 if (b->init)
Andi Kleena2d32bc2009-07-09 00:31:44 +02002233 wrmsrl(MSR_IA32_MCx_CTL(i), 0);
Andi Kleen06b7a7a2009-04-27 18:37:43 +02002234 }
Andi Kleend6b75582009-02-12 13:39:31 +01002235}
2236
Hidetoshi Seto767df1b2009-11-26 17:29:02 +09002237static void __cpuinit mce_reenable_cpu(void *h)
Andi Kleend6b75582009-02-12 13:39:31 +01002238{
Andi Kleen88ccbed2009-02-12 13:49:36 +01002239 unsigned long action = *(unsigned long *)h;
Ingo Molnare9eee032009-04-08 12:31:17 +02002240 int i;
Andi Kleend6b75582009-02-12 13:39:31 +01002241
Tejun Heo7b543a52010-12-18 16:30:05 +01002242 if (!mce_available(__this_cpu_ptr(&cpu_info)))
Andi Kleend6b75582009-02-12 13:39:31 +01002243 return;
Ingo Molnare9eee032009-04-08 12:31:17 +02002244
Andi Kleen88ccbed2009-02-12 13:49:36 +01002245 if (!(action & CPU_TASKS_FROZEN))
2246 cmci_reenable();
Andi Kleen06b7a7a2009-04-27 18:37:43 +02002247 for (i = 0; i < banks; i++) {
Andi Kleencebe1822009-07-09 00:31:43 +02002248 struct mce_bank *b = &mce_banks[i];
Ingo Molnar11868a22009-09-23 17:49:55 +02002249
Andi Kleencebe1822009-07-09 00:31:43 +02002250 if (b->init)
Andi Kleena2d32bc2009-07-09 00:31:44 +02002251 wrmsrl(MSR_IA32_MCx_CTL(i), b->ctl);
Andi Kleen06b7a7a2009-04-27 18:37:43 +02002252 }
Andi Kleend6b75582009-02-12 13:39:31 +01002253}
2254
Andi Kleen91c6d402005-07-28 21:15:39 -07002255/* Get notified when a cpu comes on/off. Be hotplug friendly. */
Ingo Molnare9eee032009-04-08 12:31:17 +02002256static int __cpuinit
2257mce_cpu_callback(struct notifier_block *nfb, unsigned long action, void *hcpu)
Andi Kleen91c6d402005-07-28 21:15:39 -07002258{
2259 unsigned int cpu = (unsigned long)hcpu;
Andi Kleen52d168e2009-02-12 13:39:29 +01002260 struct timer_list *t = &per_cpu(mce_timer, cpu);
Andi Kleen91c6d402005-07-28 21:15:39 -07002261
2262 switch (action) {
Andreas Herrmannbae19fe2007-11-14 17:00:44 -08002263 case CPU_ONLINE:
2264 case CPU_ONLINE_FROZEN:
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002265 mce_device_create(cpu);
Rafael J. Wysocki87357282008-08-22 22:23:09 +02002266 if (threshold_cpu_callback)
2267 threshold_cpu_callback(action, cpu);
Andi Kleen91c6d402005-07-28 21:15:39 -07002268 break;
Andi Kleen91c6d402005-07-28 21:15:39 -07002269 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07002270 case CPU_DEAD_FROZEN:
Rafael J. Wysocki87357282008-08-22 22:23:09 +02002271 if (threshold_cpu_callback)
2272 threshold_cpu_callback(action, cpu);
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002273 mce_device_remove(cpu);
Andi Kleen91c6d402005-07-28 21:15:39 -07002274 break;
Andi Kleen52d168e2009-02-12 13:39:29 +01002275 case CPU_DOWN_PREPARE:
2276 case CPU_DOWN_PREPARE_FROZEN:
2277 del_timer_sync(t);
Andi Kleen88ccbed2009-02-12 13:49:36 +01002278 smp_call_function_single(cpu, mce_disable_cpu, &action, 1);
Andi Kleen52d168e2009-02-12 13:39:29 +01002279 break;
2280 case CPU_DOWN_FAILED:
2281 case CPU_DOWN_FAILED_FROZEN:
Hidetoshi Setofe5ed912009-12-03 11:33:08 +09002282 if (!mce_ignore_ce && check_interval) {
2283 t->expires = round_jiffies(jiffies +
Thomas Gleixner82f7af02012-05-24 17:54:51 +00002284 per_cpu(mce_next_interval, cpu));
Hidetoshi Setofe5ed912009-12-03 11:33:08 +09002285 add_timer_on(t, cpu);
2286 }
Andi Kleen88ccbed2009-02-12 13:49:36 +01002287 smp_call_function_single(cpu, mce_reenable_cpu, &action, 1);
2288 break;
2289 case CPU_POST_DEAD:
2290 /* intentionally ignoring frozen here */
2291 cmci_rediscover(cpu);
Andi Kleen52d168e2009-02-12 13:39:29 +01002292 break;
Andi Kleen91c6d402005-07-28 21:15:39 -07002293 }
Andreas Herrmannbae19fe2007-11-14 17:00:44 -08002294 return NOTIFY_OK;
Andi Kleen91c6d402005-07-28 21:15:39 -07002295}
2296
Sam Ravnborg1e356692008-01-30 13:33:36 +01002297static struct notifier_block mce_cpu_notifier __cpuinitdata = {
Andi Kleen91c6d402005-07-28 21:15:39 -07002298 .notifier_call = mce_cpu_callback,
2299};
2300
Andi Kleencebe1822009-07-09 00:31:43 +02002301static __init void mce_init_banks(void)
Andi Kleen0d7482e32009-02-17 23:07:13 +01002302{
2303 int i;
2304
Andi Kleen0d7482e32009-02-17 23:07:13 +01002305 for (i = 0; i < banks; i++) {
Andi Kleencebe1822009-07-09 00:31:43 +02002306 struct mce_bank *b = &mce_banks[i];
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002307 struct device_attribute *a = &b->attr;
Ingo Molnare9eee032009-04-08 12:31:17 +02002308
Eric W. Biedermana07e4152010-02-11 15:23:05 -08002309 sysfs_attr_init(&a->attr);
Andi Kleencebe1822009-07-09 00:31:43 +02002310 a->attr.name = b->attrname;
2311 snprintf(b->attrname, ATTR_LEN, "bank%d", i);
Ingo Molnare9eee032009-04-08 12:31:17 +02002312
2313 a->attr.mode = 0644;
2314 a->show = show_bank;
2315 a->store = set_bank;
Andi Kleen0d7482e32009-02-17 23:07:13 +01002316 }
Andi Kleen0d7482e32009-02-17 23:07:13 +01002317}
2318
Borislav Petkov5e099542009-10-16 12:31:32 +02002319static __init int mcheck_init_device(void)
Andi Kleen91c6d402005-07-28 21:15:39 -07002320{
2321 int err;
2322 int i = 0;
2323
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 if (!mce_available(&boot_cpu_data))
2325 return -EIO;
Andi Kleen0d7482e32009-02-17 23:07:13 +01002326
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002327 zalloc_cpumask_var(&mce_device_initialized, GFP_KERNEL);
Rusty Russell996867d2009-03-13 14:49:51 +10302328
Andi Kleencebe1822009-07-09 00:31:43 +02002329 mce_init_banks();
Andi Kleen0d7482e32009-02-17 23:07:13 +01002330
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002331 err = subsys_system_register(&mce_subsys, NULL);
Akinobu Mitad435d862007-10-18 03:05:15 -07002332 if (err)
2333 return err;
Andi Kleen91c6d402005-07-28 21:15:39 -07002334
2335 for_each_online_cpu(i) {
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002336 err = mce_device_create(i);
Akinobu Mitad435d862007-10-18 03:05:15 -07002337 if (err)
2338 return err;
Andi Kleen91c6d402005-07-28 21:15:39 -07002339 }
2340
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +01002341 register_syscore_ops(&mce_syscore_ops);
Chandra Seetharamanbe6b5a32006-07-30 03:03:37 -07002342 register_hotcpu_notifier(&mce_cpu_notifier);
Hidetoshi Seto93b62c32011-06-08 11:00:45 +09002343
2344 /* register character device /dev/mcelog */
2345 misc_register(&mce_chrdev_device);
Ingo Molnare9eee032009-04-08 12:31:17 +02002346
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348}
Borislav Petkov5e099542009-10-16 12:31:32 +02002349device_initcall(mcheck_init_device);
Ingo Molnara988d332009-04-08 12:31:25 +02002350
Andi Kleend7c3c9a2009-04-28 23:07:25 +02002351/*
2352 * Old style boot options parsing. Only for compatibility.
2353 */
2354static int __init mcheck_disable(char *str)
2355{
2356 mce_disabled = 1;
2357 return 1;
2358}
2359__setup("nomce", mcheck_disable);
Huang Ying5be9ed22009-07-31 09:41:42 +08002360
2361#ifdef CONFIG_DEBUG_FS
2362struct dentry *mce_get_debugfs_dir(void)
2363{
2364 static struct dentry *dmce;
2365
2366 if (!dmce)
2367 dmce = debugfs_create_dir("mce", NULL);
2368
2369 return dmce;
2370}
Huang Yingbf783f92009-07-31 09:41:43 +08002371
2372static void mce_reset(void)
2373{
2374 cpu_missing = 0;
2375 atomic_set(&mce_fake_paniced, 0);
2376 atomic_set(&mce_executing, 0);
2377 atomic_set(&mce_callin, 0);
2378 atomic_set(&global_nwo, 0);
2379}
2380
2381static int fake_panic_get(void *data, u64 *val)
2382{
2383 *val = fake_panic;
2384 return 0;
2385}
2386
2387static int fake_panic_set(void *data, u64 val)
2388{
2389 mce_reset();
2390 fake_panic = val;
2391 return 0;
2392}
2393
2394DEFINE_SIMPLE_ATTRIBUTE(fake_panic_fops, fake_panic_get,
2395 fake_panic_set, "%llu\n");
2396
Borislav Petkov5e099542009-10-16 12:31:32 +02002397static int __init mcheck_debugfs_init(void)
Huang Yingbf783f92009-07-31 09:41:43 +08002398{
2399 struct dentry *dmce, *ffake_panic;
2400
2401 dmce = mce_get_debugfs_dir();
2402 if (!dmce)
2403 return -ENOMEM;
2404 ffake_panic = debugfs_create_file("fake_panic", 0444, dmce, NULL,
2405 &fake_panic_fops);
2406 if (!ffake_panic)
2407 return -ENOMEM;
2408
2409 return 0;
2410}
Borislav Petkov5e099542009-10-16 12:31:32 +02002411late_initcall(mcheck_debugfs_init);
Huang Ying5be9ed22009-07-31 09:41:42 +08002412#endif