blob: 29e87d3b2843610207ebd875071a4291bec02cfe [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
Andi Kleen3c079792009-05-27 21:56:55 +020063#define SPINUNIT 100 /* 100ns */
64
Andi Kleen553f2652006-04-07 19:49:57 +020065atomic_t mce_entry;
66
Andi Kleen01ca79f2009-05-27 21:56:52 +020067DEFINE_PER_CPU(unsigned, mce_exception_count);
68
Tim Hockinbd784322007-07-21 17:10:37 +020069/*
70 * Tolerant levels:
71 * 0: always panic on uncorrected errors, log corrected errors
72 * 1: panic or SIGBUS on uncorrected errors, log corrected errors
73 * 2: SIGBUS or log uncorrected errors (if possible), log corrected errors
74 * 3: never panic or SIGBUS, log all errors (for testing only)
75 */
Hidetoshi Seto4e5b3e62009-06-15 17:20:20 +090076static int tolerant __read_mostly = 1;
77static int banks __read_mostly;
Hidetoshi Seto4e5b3e62009-06-15 17:20:20 +090078static int rip_msr __read_mostly;
79static int mce_bootlog __read_mostly = -1;
80static int monarch_timeout __read_mostly = -1;
81static int mce_panic_timeout __read_mostly;
82static int mce_dont_log_ce __read_mostly;
83int mce_cmci_disabled __read_mostly;
84int mce_ignore_ce __read_mostly;
85int mce_ser __read_mostly;
Naveen N. Rao450cc202012-09-27 10:08:00 -070086int mce_bios_cmci_threshold __read_mostly;
Andi Kleena98f0dd2007-02-13 13:26:23 +010087
Andi Kleencebe1822009-07-09 00:31:43 +020088struct mce_bank *mce_banks __read_mostly;
89
Hidetoshi Seto1020bcb2009-06-15 17:20:57 +090090/* User mode helper program triggered by machine check event */
91static unsigned long mce_need_notify;
92static char mce_helper[128];
93static char *mce_helper_argv[2] = { mce_helper, NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Hidetoshi Seto93b62c32011-06-08 11:00:45 +090095static DECLARE_WAIT_QUEUE_HEAD(mce_chrdev_wait);
96
Andi Kleen3c079792009-05-27 21:56:55 +020097static DEFINE_PER_CPU(struct mce, mces_seen);
98static int cpu_missing;
99
Andi Kleenee031c32009-02-12 13:49:34 +0100100/* MCA banks polled by the period polling timer for corrected events */
101DEFINE_PER_CPU(mce_banks_t, mce_poll_banks) = {
102 [0 ... BITS_TO_LONGS(MAX_NR_BANKS)-1] = ~0UL
103};
104
Andi Kleen9b1beaf2009-05-27 21:56:59 +0200105static DEFINE_PER_CPU(struct work_struct, mce_work);
106
Tony Luck61b0fcc2012-07-19 11:28:46 -0700107static void (*quirk_no_way_out)(int bank, struct mce *m, struct pt_regs *regs);
108
Borislav Petkov3653ada2011-12-04 15:12:09 +0100109/*
110 * CPU/chipset specific EDAC code can register a notifier call here to print
111 * MCE errors in a human-readable form.
112 */
113ATOMIC_NOTIFIER_HEAD(x86_mce_decoder_chain);
114
Andi Kleenb5f2fa42009-02-12 13:43:22 +0100115/* Do initial initialization of a struct mce */
116void mce_setup(struct mce *m)
117{
118 memset(m, 0, sizeof(struct mce));
Andi Kleend620c672009-05-27 21:56:56 +0200119 m->cpu = m->extcpu = smp_processor_id();
Andi Kleenb5f2fa42009-02-12 13:43:22 +0100120 rdtscll(m->tsc);
Andi Kleen8ee08342009-05-27 21:56:56 +0200121 /* We hope get_seconds stays lockless */
122 m->time = get_seconds();
123 m->cpuvendor = boot_cpu_data.x86_vendor;
124 m->cpuid = cpuid_eax(1);
Andi Kleen8ee08342009-05-27 21:56:56 +0200125 m->socketid = cpu_data(m->extcpu).phys_proc_id;
Andi Kleen8ee08342009-05-27 21:56:56 +0200126 m->apicid = cpu_data(m->extcpu).initial_apicid;
127 rdmsrl(MSR_IA32_MCG_CAP, m->mcgcap);
Andi Kleenb5f2fa42009-02-12 13:43:22 +0100128}
129
Andi Kleenea149b32009-04-29 19:31:00 +0200130DEFINE_PER_CPU(struct mce, injectm);
131EXPORT_PER_CPU_SYMBOL_GPL(injectm);
132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133/*
134 * Lockless MCE logging infrastructure.
135 * This avoids deadlocks on printk locks without having to break locks. Also
136 * separate MCEs from kernel messages to avoid bogus bug reports.
137 */
138
Adrian Bunk231fd902008-01-30 13:30:30 +0100139static struct mce_log mcelog = {
Andi Kleenf6fb0ac2009-05-27 21:56:55 +0200140 .signature = MCE_LOG_SIGNATURE,
141 .len = MCE_LOG_LEN,
142 .recordlen = sizeof(struct mce),
Thomas Gleixnerd88203d2007-10-23 22:37:23 +0200143};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
145void mce_log(struct mce *mce)
146{
147 unsigned next, entry;
Borislav Petkovf0cb5452011-07-18 11:24:45 -0300148 int ret = 0;
Ingo Molnare9eee032009-04-08 12:31:17 +0200149
Hidetoshi Seto8968f9d2009-10-13 16:19:41 +0900150 /* Emit the trace record: */
151 trace_mce_record(mce);
152
Borislav Petkovf0cb5452011-07-18 11:24:45 -0300153 ret = atomic_notifier_call_chain(&x86_mce_decoder_chain, 0, mce);
154 if (ret == NOTIFY_STOP)
155 return;
156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 mce->finished = 0;
Mike Waychison76441432005-09-30 00:01:27 +0200158 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 for (;;) {
Paul E. McKenneyf56e8a02010-03-05 15:03:27 -0800160 entry = rcu_dereference_check_mce(mcelog.next);
Andi Kleen673242c2005-09-12 18:49:24 +0200161 for (;;) {
Mauro Carvalho Chehab696e4092009-07-23 06:57:45 -0300162
163 /*
Ingo Molnare9eee032009-04-08 12:31:17 +0200164 * When the buffer fills up discard new entries.
165 * Assume that the earlier errors are the more
166 * interesting ones:
167 */
Andi Kleen673242c2005-09-12 18:49:24 +0200168 if (entry >= MCE_LOG_LEN) {
Hidetoshi Seto14a02532009-04-30 16:04:51 +0900169 set_bit(MCE_OVERFLOW,
170 (unsigned long *)&mcelog.flags);
Andi Kleen673242c2005-09-12 18:49:24 +0200171 return;
172 }
Ingo Molnare9eee032009-04-08 12:31:17 +0200173 /* Old left over entry. Skip: */
Andi Kleen673242c2005-09-12 18:49:24 +0200174 if (mcelog.entry[entry].finished) {
175 entry++;
176 continue;
177 }
Mike Waychison76441432005-09-30 00:01:27 +0200178 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 smp_rmb();
181 next = entry + 1;
182 if (cmpxchg(&mcelog.next, entry, next) == entry)
183 break;
184 }
185 memcpy(mcelog.entry + entry, mce, sizeof(struct mce));
Mike Waychison76441432005-09-30 00:01:27 +0200186 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 mcelog.entry[entry].finished = 1;
Mike Waychison76441432005-09-30 00:01:27 +0200188 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
Andi Kleena0189c72009-05-27 21:56:54 +0200190 mce->finished = 1;
Hidetoshi Seto1020bcb2009-06-15 17:20:57 +0900191 set_bit(0, &mce_need_notify);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192}
193
Borislav Petkov09371952011-12-08 12:28:33 +0100194static void drain_mcelog_buffer(void)
195{
196 unsigned int next, i, prev = 0;
197
Srivatsa S. Bhatb11e3d72012-03-07 11:44:29 +0100198 next = ACCESS_ONCE(mcelog.next);
Borislav Petkov09371952011-12-08 12:28:33 +0100199
200 do {
201 struct mce *m;
202
203 /* drain what was logged during boot */
204 for (i = prev; i < next; i++) {
205 unsigned long start = jiffies;
206 unsigned retries = 1;
207
208 m = &mcelog.entry[i];
209
210 while (!m->finished) {
211 if (time_after_eq(jiffies, start + 2*retries))
212 retries++;
213
214 cpu_relax();
215
216 if (!m->finished && retries >= 4) {
Joe Perchesc767a542012-05-21 19:50:07 -0700217 pr_err("skipping error being logged currently!\n");
Borislav Petkov09371952011-12-08 12:28:33 +0100218 break;
219 }
220 }
221 smp_rmb();
222 atomic_notifier_call_chain(&x86_mce_decoder_chain, 0, m);
223 }
224
225 memset(mcelog.entry + prev, 0, (next - prev) * sizeof(*m));
226 prev = next;
227 next = cmpxchg(&mcelog.next, prev, 0);
228 } while (next != prev);
229}
230
231
Borislav Petkov3653ada2011-12-04 15:12:09 +0100232void mce_register_decode_chain(struct notifier_block *nb)
233{
234 atomic_notifier_chain_register(&x86_mce_decoder_chain, nb);
Borislav Petkov09371952011-12-08 12:28:33 +0100235 drain_mcelog_buffer();
Borislav Petkov3653ada2011-12-04 15:12:09 +0100236}
237EXPORT_SYMBOL_GPL(mce_register_decode_chain);
238
239void mce_unregister_decode_chain(struct notifier_block *nb)
240{
241 atomic_notifier_chain_unregister(&x86_mce_decoder_chain, nb);
242}
243EXPORT_SYMBOL_GPL(mce_unregister_decode_chain);
244
Hidetoshi Seto77e26cc2009-06-11 16:04:35 +0900245static void print_mce(struct mce *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246{
Borislav Petkovdffa4b22011-04-20 12:23:49 +0200247 int ret = 0;
248
Huang Yinga2d7b0d2010-06-08 14:35:39 +0800249 pr_emerg(HW_ERR "CPU %d: Machine Check Exception: %Lx Bank %d: %016Lx\n",
Andi Kleend620c672009-05-27 21:56:56 +0200250 m->extcpu, m->mcgstatus, m->bank, m->status);
Ingo Molnarf436f8b2009-10-01 16:14:32 +0200251
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100252 if (m->ip) {
Huang Yinga2d7b0d2010-06-08 14:35:39 +0800253 pr_emerg(HW_ERR "RIP%s %02x:<%016Lx> ",
Ingo Molnarf436f8b2009-10-01 16:14:32 +0200254 !(m->mcgstatus & MCG_STATUS_EIPV) ? " !INEXACT!" : "",
255 m->cs, m->ip);
256
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 if (m->cs == __KERNEL_CS)
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100258 print_symbol("{%s}", m->ip);
Ingo Molnarf436f8b2009-10-01 16:14:32 +0200259 pr_cont("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 }
Borislav Petkov549d0422009-07-24 13:51:42 +0200261
Huang Yinga2d7b0d2010-06-08 14:35:39 +0800262 pr_emerg(HW_ERR "TSC %llx ", m->tsc);
Ingo Molnarf436f8b2009-10-01 16:14:32 +0200263 if (m->addr)
264 pr_cont("ADDR %llx ", m->addr);
265 if (m->misc)
266 pr_cont("MISC %llx ", m->misc);
267
268 pr_cont("\n");
Andi Kleen506ed6b2011-10-12 17:46:33 -0700269 /*
270 * Note this output is parsed by external tools and old fields
271 * should not be changed.
272 */
Borislav Petkov881e23e2011-10-17 16:45:10 +0200273 pr_emerg(HW_ERR "PROCESSOR %u:%x TIME %llu SOCKET %u APIC %x microcode %x\n",
Andi Kleen506ed6b2011-10-12 17:46:33 -0700274 m->cpuvendor, m->cpuid, m->time, m->socketid, m->apicid,
275 cpu_data(m->extcpu).microcode);
Ingo Molnarf436f8b2009-10-01 16:14:32 +0200276
277 /*
278 * Print out human-readable details about the MCE error,
Borislav Petkovfb253192009-10-07 13:20:38 +0200279 * (if the CPU has an implementation for that)
Ingo Molnarf436f8b2009-10-01 16:14:32 +0200280 */
Borislav Petkovdffa4b22011-04-20 12:23:49 +0200281 ret = atomic_notifier_call_chain(&x86_mce_decoder_chain, 0, m);
282 if (ret == NOTIFY_STOP)
283 return;
284
285 pr_emerg_ratelimited(HW_ERR "Run the above through 'mcelog --ascii'\n");
Andi Kleen86503562009-05-27 21:56:58 +0200286}
287
Andi Kleenf94b61c2009-05-27 21:56:55 +0200288#define PANIC_TIMEOUT 5 /* 5 seconds */
289
290static atomic_t mce_paniced;
291
Huang Yingbf783f92009-07-31 09:41:43 +0800292static int fake_panic;
293static atomic_t mce_fake_paniced;
294
Andi Kleenf94b61c2009-05-27 21:56:55 +0200295/* Panic in progress. Enable interrupts and wait for final IPI */
296static void wait_for_panic(void)
297{
298 long timeout = PANIC_TIMEOUT*USEC_PER_SEC;
Ingo Molnarf436f8b2009-10-01 16:14:32 +0200299
Andi Kleenf94b61c2009-05-27 21:56:55 +0200300 preempt_disable();
301 local_irq_enable();
302 while (timeout-- > 0)
303 udelay(1);
Andi Kleen29b0f592009-05-27 21:56:56 +0200304 if (panic_timeout == 0)
305 panic_timeout = mce_panic_timeout;
Andi Kleenf94b61c2009-05-27 21:56:55 +0200306 panic("Panicing machine check CPU died");
307}
308
Andi Kleenbd19a5e2009-05-27 21:56:55 +0200309static void mce_panic(char *msg, struct mce *final, char *exp)
Thomas Gleixnerd88203d2007-10-23 22:37:23 +0200310{
Huang Ying482908b2010-05-18 14:35:22 +0800311 int i, apei_err = 0;
Tim Hockine02e68d2007-07-21 17:10:36 +0200312
Huang Yingbf783f92009-07-31 09:41:43 +0800313 if (!fake_panic) {
314 /*
315 * Make sure only one CPU runs in machine check panic
316 */
317 if (atomic_inc_return(&mce_paniced) > 1)
318 wait_for_panic();
319 barrier();
Andi Kleenf94b61c2009-05-27 21:56:55 +0200320
Huang Yingbf783f92009-07-31 09:41:43 +0800321 bust_spinlocks(1);
322 console_verbose();
323 } else {
324 /* Don't log too much for fake panic */
325 if (atomic_inc_return(&mce_fake_paniced) > 1)
326 return;
327 }
Andi Kleena0189c72009-05-27 21:56:54 +0200328 /* First print corrected ones that are still unlogged */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 for (i = 0; i < MCE_LOG_LEN; i++) {
Andi Kleena0189c72009-05-27 21:56:54 +0200330 struct mce *m = &mcelog.entry[i];
Hidetoshi Seto77e26cc2009-06-11 16:04:35 +0900331 if (!(m->status & MCI_STATUS_VAL))
332 continue;
Huang Ying482908b2010-05-18 14:35:22 +0800333 if (!(m->status & MCI_STATUS_UC)) {
Hidetoshi Seto77e26cc2009-06-11 16:04:35 +0900334 print_mce(m);
Huang Ying482908b2010-05-18 14:35:22 +0800335 if (!apei_err)
336 apei_err = apei_write_mce(m);
337 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 }
Andi Kleena0189c72009-05-27 21:56:54 +0200339 /* Now print uncorrected but with the final one last */
340 for (i = 0; i < MCE_LOG_LEN; i++) {
341 struct mce *m = &mcelog.entry[i];
342 if (!(m->status & MCI_STATUS_VAL))
343 continue;
Hidetoshi Seto77e26cc2009-06-11 16:04:35 +0900344 if (!(m->status & MCI_STATUS_UC))
345 continue;
Huang Ying482908b2010-05-18 14:35:22 +0800346 if (!final || memcmp(m, final, sizeof(struct mce))) {
Hidetoshi Seto77e26cc2009-06-11 16:04:35 +0900347 print_mce(m);
Huang Ying482908b2010-05-18 14:35:22 +0800348 if (!apei_err)
349 apei_err = apei_write_mce(m);
350 }
Andi Kleena0189c72009-05-27 21:56:54 +0200351 }
Huang Ying482908b2010-05-18 14:35:22 +0800352 if (final) {
Hidetoshi Seto77e26cc2009-06-11 16:04:35 +0900353 print_mce(final);
Huang Ying482908b2010-05-18 14:35:22 +0800354 if (!apei_err)
355 apei_err = apei_write_mce(final);
356 }
Andi Kleen3c079792009-05-27 21:56:55 +0200357 if (cpu_missing)
Huang Yinga2d7b0d2010-06-08 14:35:39 +0800358 pr_emerg(HW_ERR "Some CPUs didn't answer in synchronization\n");
Andi Kleenbd19a5e2009-05-27 21:56:55 +0200359 if (exp)
Huang Yinga2d7b0d2010-06-08 14:35:39 +0800360 pr_emerg(HW_ERR "Machine check: %s\n", exp);
Huang Yingbf783f92009-07-31 09:41:43 +0800361 if (!fake_panic) {
362 if (panic_timeout == 0)
363 panic_timeout = mce_panic_timeout;
364 panic(msg);
365 } else
Huang Yinga2d7b0d2010-06-08 14:35:39 +0800366 pr_emerg(HW_ERR "Fake kernel panic: %s\n", msg);
Thomas Gleixnerd88203d2007-10-23 22:37:23 +0200367}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
Andi Kleenea149b32009-04-29 19:31:00 +0200369/* Support code for software error injection */
370
371static int msr_to_offset(u32 msr)
372{
Tejun Heo0a3aee02010-12-18 16:28:55 +0100373 unsigned bank = __this_cpu_read(injectm.bank);
Ingo Molnarf436f8b2009-10-01 16:14:32 +0200374
Andi Kleenea149b32009-04-29 19:31:00 +0200375 if (msr == rip_msr)
376 return offsetof(struct mce, ip);
Andi Kleena2d32bc2009-07-09 00:31:44 +0200377 if (msr == MSR_IA32_MCx_STATUS(bank))
Andi Kleenea149b32009-04-29 19:31:00 +0200378 return offsetof(struct mce, status);
Andi Kleena2d32bc2009-07-09 00:31:44 +0200379 if (msr == MSR_IA32_MCx_ADDR(bank))
Andi Kleenea149b32009-04-29 19:31:00 +0200380 return offsetof(struct mce, addr);
Andi Kleena2d32bc2009-07-09 00:31:44 +0200381 if (msr == MSR_IA32_MCx_MISC(bank))
Andi Kleenea149b32009-04-29 19:31:00 +0200382 return offsetof(struct mce, misc);
383 if (msr == MSR_IA32_MCG_STATUS)
384 return offsetof(struct mce, mcgstatus);
385 return -1;
386}
387
Andi Kleen5f8c1a52009-04-29 19:29:12 +0200388/* MSR access wrappers used for error injection */
389static u64 mce_rdmsrl(u32 msr)
390{
391 u64 v;
Ingo Molnar11868a22009-09-23 17:49:55 +0200392
Tejun Heo0a3aee02010-12-18 16:28:55 +0100393 if (__this_cpu_read(injectm.finished)) {
Andi Kleenea149b32009-04-29 19:31:00 +0200394 int offset = msr_to_offset(msr);
Ingo Molnar11868a22009-09-23 17:49:55 +0200395
Andi Kleenea149b32009-04-29 19:31:00 +0200396 if (offset < 0)
397 return 0;
398 return *(u64 *)((char *)&__get_cpu_var(injectm) + offset);
399 }
Ingo Molnar11868a22009-09-23 17:49:55 +0200400
401 if (rdmsrl_safe(msr, &v)) {
402 WARN_ONCE(1, "mce: Unable to read msr %d!\n", msr);
403 /*
404 * Return zero in case the access faulted. This should
405 * not happen normally but can happen if the CPU does
406 * something weird, or if the code is buggy.
407 */
408 v = 0;
409 }
410
Andi Kleen5f8c1a52009-04-29 19:29:12 +0200411 return v;
412}
413
414static void mce_wrmsrl(u32 msr, u64 v)
415{
Tejun Heo0a3aee02010-12-18 16:28:55 +0100416 if (__this_cpu_read(injectm.finished)) {
Andi Kleenea149b32009-04-29 19:31:00 +0200417 int offset = msr_to_offset(msr);
Ingo Molnar11868a22009-09-23 17:49:55 +0200418
Andi Kleenea149b32009-04-29 19:31:00 +0200419 if (offset >= 0)
420 *(u64 *)((char *)&__get_cpu_var(injectm) + offset) = v;
421 return;
422 }
Andi Kleen5f8c1a52009-04-29 19:29:12 +0200423 wrmsrl(msr, v);
424}
425
Andi Kleen9b1beaf2009-05-27 21:56:59 +0200426/*
Hidetoshi Setob8325c52011-06-08 10:57:46 +0900427 * Collect all global (w.r.t. this processor) status about this machine
428 * check into our "mce" struct so that we can use it later to assess
429 * the severity of the problem as we read per-bank specific details.
430 */
431static inline void mce_gather_info(struct mce *m, struct pt_regs *regs)
432{
433 mce_setup(m);
434
435 m->mcgstatus = mce_rdmsrl(MSR_IA32_MCG_STATUS);
436 if (regs) {
437 /*
438 * Get the address of the instruction at the time of
439 * the machine check error.
440 */
441 if (m->mcgstatus & (MCG_STATUS_RIPV|MCG_STATUS_EIPV)) {
442 m->ip = regs->ip;
443 m->cs = regs->cs;
Andi Kleena129a7c2010-11-19 13:16:22 +0100444
445 /*
446 * When in VM86 mode make the cs look like ring 3
447 * always. This is a lie, but it's better than passing
448 * the additional vm86 bit around everywhere.
449 */
450 if (v8086_mode(regs))
451 m->cs |= 3;
Hidetoshi Setob8325c52011-06-08 10:57:46 +0900452 }
453 /* Use accurate RIP reporting if available. */
454 if (rip_msr)
455 m->ip = mce_rdmsrl(rip_msr);
456 }
457}
458
459/*
Andi Kleen9b1beaf2009-05-27 21:56:59 +0200460 * Simple lockless ring to communicate PFNs from the exception handler with the
461 * process context work function. This is vastly simplified because there's
462 * only a single reader and a single writer.
463 */
464#define MCE_RING_SIZE 16 /* we use one entry less */
465
466struct mce_ring {
467 unsigned short start;
468 unsigned short end;
469 unsigned long ring[MCE_RING_SIZE];
470};
471static DEFINE_PER_CPU(struct mce_ring, mce_ring);
472
473/* Runs with CPU affinity in workqueue */
474static int mce_ring_empty(void)
475{
476 struct mce_ring *r = &__get_cpu_var(mce_ring);
477
478 return r->start == r->end;
479}
480
481static int mce_ring_get(unsigned long *pfn)
482{
483 struct mce_ring *r;
484 int ret = 0;
485
486 *pfn = 0;
487 get_cpu();
488 r = &__get_cpu_var(mce_ring);
489 if (r->start == r->end)
490 goto out;
491 *pfn = r->ring[r->start];
492 r->start = (r->start + 1) % MCE_RING_SIZE;
493 ret = 1;
494out:
495 put_cpu();
496 return ret;
497}
498
499/* Always runs in MCE context with preempt off */
500static int mce_ring_add(unsigned long pfn)
501{
502 struct mce_ring *r = &__get_cpu_var(mce_ring);
503 unsigned next;
504
505 next = (r->end + 1) % MCE_RING_SIZE;
506 if (next == r->start)
507 return -1;
508 r->ring[r->end] = pfn;
509 wmb();
510 r->end = next;
511 return 0;
512}
513
Andi Kleen88ccbed2009-02-12 13:49:36 +0100514int mce_available(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515{
Andi Kleen04b2b1a2009-04-28 22:50:19 +0200516 if (mce_disabled)
Andi Kleen5b4408f2009-02-12 13:39:30 +0100517 return 0;
Akinobu Mita3d1712c2006-03-24 03:15:11 -0800518 return cpu_has(c, X86_FEATURE_MCE) && cpu_has(c, X86_FEATURE_MCA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519}
520
Andi Kleen9b1beaf2009-05-27 21:56:59 +0200521static void mce_schedule_work(void)
522{
523 if (!mce_ring_empty()) {
524 struct work_struct *work = &__get_cpu_var(mce_work);
525 if (!work_pending(work))
526 schedule_work(work);
527 }
528}
529
Hidetoshi Setob77e70b2011-06-08 10:56:02 +0900530DEFINE_PER_CPU(struct irq_work, mce_irq_work);
531
532static void mce_irq_work_cb(struct irq_work *entry)
Andi Kleenccc3c312009-05-27 21:56:54 +0200533{
Andi Kleen9ff36ee2009-05-27 21:56:58 +0200534 mce_notify_irq();
Andi Kleen9b1beaf2009-05-27 21:56:59 +0200535 mce_schedule_work();
Andi Kleenccc3c312009-05-27 21:56:54 +0200536}
Andi Kleenccc3c312009-05-27 21:56:54 +0200537
538static void mce_report_event(struct pt_regs *regs)
539{
540 if (regs->flags & (X86_VM_MASK|X86_EFLAGS_IF)) {
Andi Kleen9ff36ee2009-05-27 21:56:58 +0200541 mce_notify_irq();
Andi Kleen9b1beaf2009-05-27 21:56:59 +0200542 /*
543 * Triggering the work queue here is just an insurance
544 * policy in case the syscall exit notify handler
545 * doesn't run soon enough or ends up running on the
546 * wrong CPU (can happen when audit sleeps)
547 */
548 mce_schedule_work();
Andi Kleenccc3c312009-05-27 21:56:54 +0200549 return;
550 }
551
Hidetoshi Setob77e70b2011-06-08 10:56:02 +0900552 irq_work_queue(&__get_cpu_var(mce_irq_work));
Andi Kleenccc3c312009-05-27 21:56:54 +0200553}
554
Tony Luck85f926942011-12-13 09:48:13 -0800555/*
556 * Read ADDR and MISC registers.
557 */
558static void mce_read_aux(struct mce *m, int i)
559{
560 if (m->status & MCI_STATUS_MISCV)
561 m->misc = mce_rdmsrl(MSR_IA32_MCx_MISC(i));
562 if (m->status & MCI_STATUS_ADDRV) {
563 m->addr = mce_rdmsrl(MSR_IA32_MCx_ADDR(i));
564
565 /*
566 * Mask the reported address by the reported granularity.
567 */
568 if (mce_ser && (m->status & MCI_STATUS_MISCV)) {
569 u8 shift = MCI_MISC_ADDR_LSB(m->misc);
570 m->addr >>= shift;
571 m->addr <<= shift;
572 }
573 }
574}
575
Andi Kleenca84f692009-05-27 21:56:57 +0200576DEFINE_PER_CPU(unsigned, mce_poll_count);
577
Thomas Gleixnerd88203d2007-10-23 22:37:23 +0200578/*
Andi Kleenb79109c2009-02-12 13:43:23 +0100579 * Poll for corrected events or events that happened before reset.
580 * Those are just logged through /dev/mcelog.
581 *
582 * This is executed in standard interrupt context.
Andi Kleened7290d2009-05-27 21:56:57 +0200583 *
584 * Note: spec recommends to panic for fatal unsignalled
585 * errors here. However this would be quite problematic --
586 * we would need to reimplement the Monarch handling and
587 * it would mess up the exclusion between exception handler
588 * and poll hander -- * so we skip this for now.
589 * These cases should not happen anyways, or only when the CPU
590 * is already totally * confused. In this case it's likely it will
591 * not fully execute the machine check handler either.
Andi Kleenb79109c2009-02-12 13:43:23 +0100592 */
Andi Kleenee031c32009-02-12 13:49:34 +0100593void machine_check_poll(enum mcp_flags flags, mce_banks_t *b)
Andi Kleenb79109c2009-02-12 13:43:23 +0100594{
595 struct mce m;
596 int i;
597
Alex Shic6ae41e2012-05-11 15:35:27 +0800598 this_cpu_inc(mce_poll_count);
Andi Kleenca84f692009-05-27 21:56:57 +0200599
Hidetoshi Setob8325c52011-06-08 10:57:46 +0900600 mce_gather_info(&m, NULL);
Andi Kleenb79109c2009-02-12 13:43:23 +0100601
Andi Kleenb79109c2009-02-12 13:43:23 +0100602 for (i = 0; i < banks; i++) {
Andi Kleencebe1822009-07-09 00:31:43 +0200603 if (!mce_banks[i].ctl || !test_bit(i, *b))
Andi Kleenb79109c2009-02-12 13:43:23 +0100604 continue;
605
606 m.misc = 0;
607 m.addr = 0;
608 m.bank = i;
609 m.tsc = 0;
610
611 barrier();
Andi Kleena2d32bc2009-07-09 00:31:44 +0200612 m.status = mce_rdmsrl(MSR_IA32_MCx_STATUS(i));
Andi Kleenb79109c2009-02-12 13:43:23 +0100613 if (!(m.status & MCI_STATUS_VAL))
614 continue;
615
616 /*
Andi Kleened7290d2009-05-27 21:56:57 +0200617 * Uncorrected or signalled events are handled by the exception
618 * handler when it is enabled, so don't process those here.
Andi Kleenb79109c2009-02-12 13:43:23 +0100619 *
620 * TBD do the same check for MCI_STATUS_EN here?
621 */
Andi Kleened7290d2009-05-27 21:56:57 +0200622 if (!(flags & MCP_UC) &&
623 (m.status & (mce_ser ? MCI_STATUS_S : MCI_STATUS_UC)))
Andi Kleenb79109c2009-02-12 13:43:23 +0100624 continue;
625
Tony Luck85f926942011-12-13 09:48:13 -0800626 mce_read_aux(&m, i);
Andi Kleenb79109c2009-02-12 13:43:23 +0100627
628 if (!(flags & MCP_TIMESTAMP))
629 m.tsc = 0;
630 /*
631 * Don't get the IP here because it's unlikely to
632 * have anything to do with the actual error location.
633 */
Borislav Petkovf0cb5452011-07-18 11:24:45 -0300634 if (!(flags & MCP_DONTLOG) && !mce_dont_log_ce)
Andi Kleen5679af42009-04-07 17:06:55 +0200635 mce_log(&m);
Andi Kleenb79109c2009-02-12 13:43:23 +0100636
637 /*
638 * Clear state for this bank.
639 */
Andi Kleena2d32bc2009-07-09 00:31:44 +0200640 mce_wrmsrl(MSR_IA32_MCx_STATUS(i), 0);
Andi Kleenb79109c2009-02-12 13:43:23 +0100641 }
642
643 /*
644 * Don't clear MCG_STATUS here because it's only defined for
645 * exceptions.
646 */
Andi Kleen88921be2009-05-27 21:56:51 +0200647
648 sync_core();
Andi Kleenb79109c2009-02-12 13:43:23 +0100649}
Andi Kleenea149b32009-04-29 19:31:00 +0200650EXPORT_SYMBOL_GPL(machine_check_poll);
Andi Kleenb79109c2009-02-12 13:43:23 +0100651
652/*
Andi Kleenbd19a5e2009-05-27 21:56:55 +0200653 * Do a quick check if any of the events requires a panic.
654 * This decides if we keep the events around or clear them.
655 */
Tony Luck61b0fcc2012-07-19 11:28:46 -0700656static int mce_no_way_out(struct mce *m, char **msg, unsigned long *validp,
657 struct pt_regs *regs)
Andi Kleenbd19a5e2009-05-27 21:56:55 +0200658{
Tony Luck95022b82012-04-18 15:19:40 -0700659 int i, ret = 0;
Andi Kleenbd19a5e2009-05-27 21:56:55 +0200660
661 for (i = 0; i < banks; i++) {
Andi Kleena2d32bc2009-07-09 00:31:44 +0200662 m->status = mce_rdmsrl(MSR_IA32_MCx_STATUS(i));
Tony Luck61b0fcc2012-07-19 11:28:46 -0700663 if (m->status & MCI_STATUS_VAL) {
Tony Luck95022b82012-04-18 15:19:40 -0700664 __set_bit(i, validp);
Tony Luck61b0fcc2012-07-19 11:28:46 -0700665 if (quirk_no_way_out)
666 quirk_no_way_out(i, m, regs);
667 }
Andi Kleenbd19a5e2009-05-27 21:56:55 +0200668 if (mce_severity(m, tolerant, msg) >= MCE_PANIC_SEVERITY)
Tony Luck95022b82012-04-18 15:19:40 -0700669 ret = 1;
Andi Kleenbd19a5e2009-05-27 21:56:55 +0200670 }
Tony Luck95022b82012-04-18 15:19:40 -0700671 return ret;
Andi Kleenbd19a5e2009-05-27 21:56:55 +0200672}
673
674/*
Andi Kleen3c079792009-05-27 21:56:55 +0200675 * Variable to establish order between CPUs while scanning.
676 * Each CPU spins initially until executing is equal its number.
677 */
678static atomic_t mce_executing;
679
680/*
681 * Defines order of CPUs on entry. First CPU becomes Monarch.
682 */
683static atomic_t mce_callin;
684
685/*
686 * Check if a timeout waiting for other CPUs happened.
687 */
688static int mce_timed_out(u64 *t)
689{
690 /*
691 * The others already did panic for some reason.
692 * Bail out like in a timeout.
693 * rmb() to tell the compiler that system_state
694 * might have been modified by someone else.
695 */
696 rmb();
697 if (atomic_read(&mce_paniced))
698 wait_for_panic();
699 if (!monarch_timeout)
700 goto out;
701 if ((s64)*t < SPINUNIT) {
702 /* CHECKME: Make panic default for 1 too? */
703 if (tolerant < 1)
704 mce_panic("Timeout synchronizing machine check over CPUs",
705 NULL, NULL);
706 cpu_missing = 1;
707 return 1;
708 }
709 *t -= SPINUNIT;
710out:
711 touch_nmi_watchdog();
712 return 0;
713}
714
715/*
716 * The Monarch's reign. The Monarch is the CPU who entered
717 * the machine check handler first. It waits for the others to
718 * raise the exception too and then grades them. When any
719 * error is fatal panic. Only then let the others continue.
720 *
721 * The other CPUs entering the MCE handler will be controlled by the
722 * Monarch. They are called Subjects.
723 *
724 * This way we prevent any potential data corruption in a unrecoverable case
725 * and also makes sure always all CPU's errors are examined.
726 *
Hidetoshi Seto680b6cf2009-08-26 16:20:36 +0900727 * Also this detects the case of a machine check event coming from outer
Andi Kleen3c079792009-05-27 21:56:55 +0200728 * space (not detected by any CPUs) In this case some external agent wants
729 * us to shut down, so panic too.
730 *
731 * The other CPUs might still decide to panic if the handler happens
732 * in a unrecoverable place, but in this case the system is in a semi-stable
733 * state and won't corrupt anything by itself. It's ok to let the others
734 * continue for a bit first.
735 *
736 * All the spin loops have timeouts; when a timeout happens a CPU
737 * typically elects itself to be Monarch.
738 */
739static void mce_reign(void)
740{
741 int cpu;
742 struct mce *m = NULL;
743 int global_worst = 0;
744 char *msg = NULL;
745 char *nmsg = NULL;
746
747 /*
748 * This CPU is the Monarch and the other CPUs have run
749 * through their handlers.
750 * Grade the severity of the errors of all the CPUs.
751 */
752 for_each_possible_cpu(cpu) {
753 int severity = mce_severity(&per_cpu(mces_seen, cpu), tolerant,
754 &nmsg);
755 if (severity > global_worst) {
756 msg = nmsg;
757 global_worst = severity;
758 m = &per_cpu(mces_seen, cpu);
759 }
760 }
761
762 /*
763 * Cannot recover? Panic here then.
764 * This dumps all the mces in the log buffer and stops the
765 * other CPUs.
766 */
767 if (m && global_worst >= MCE_PANIC_SEVERITY && tolerant < 3)
Andi Kleenac960372009-05-27 21:56:58 +0200768 mce_panic("Fatal Machine check", m, msg);
Andi Kleen3c079792009-05-27 21:56:55 +0200769
770 /*
771 * For UC somewhere we let the CPU who detects it handle it.
772 * Also must let continue the others, otherwise the handling
773 * CPU could deadlock on a lock.
774 */
775
776 /*
777 * No machine check event found. Must be some external
778 * source or one CPU is hung. Panic.
779 */
Hidetoshi Seto680b6cf2009-08-26 16:20:36 +0900780 if (global_worst <= MCE_KEEP_SEVERITY && tolerant < 3)
Andi Kleen3c079792009-05-27 21:56:55 +0200781 mce_panic("Machine check from unknown source", NULL, NULL);
782
783 /*
784 * Now clear all the mces_seen so that they don't reappear on
785 * the next mce.
786 */
787 for_each_possible_cpu(cpu)
788 memset(&per_cpu(mces_seen, cpu), 0, sizeof(struct mce));
789}
790
791static atomic_t global_nwo;
792
793/*
794 * Start of Monarch synchronization. This waits until all CPUs have
795 * entered the exception handler and then determines if any of them
796 * saw a fatal event that requires panic. Then it executes them
797 * in the entry order.
798 * TBD double check parallel CPU hotunplug
799 */
Hidetoshi Seto7fb06fc2009-06-15 18:18:43 +0900800static int mce_start(int *no_way_out)
Andi Kleen3c079792009-05-27 21:56:55 +0200801{
Hidetoshi Seto7fb06fc2009-06-15 18:18:43 +0900802 int order;
Andi Kleen3c079792009-05-27 21:56:55 +0200803 int cpus = num_online_cpus();
804 u64 timeout = (u64)monarch_timeout * NSEC_PER_USEC;
805
Hidetoshi Seto7fb06fc2009-06-15 18:18:43 +0900806 if (!timeout)
807 return -1;
Andi Kleen3c079792009-05-27 21:56:55 +0200808
Hidetoshi Seto7fb06fc2009-06-15 18:18:43 +0900809 atomic_add(*no_way_out, &global_nwo);
Huang Ying184e1fd2009-06-15 15:37:07 +0800810 /*
811 * global_nwo should be updated before mce_callin
812 */
813 smp_wmb();
Borislav Petkova95436e2009-06-20 23:28:22 -0700814 order = atomic_inc_return(&mce_callin);
Andi Kleen3c079792009-05-27 21:56:55 +0200815
816 /*
817 * Wait for everyone.
818 */
819 while (atomic_read(&mce_callin) != cpus) {
820 if (mce_timed_out(&timeout)) {
821 atomic_set(&global_nwo, 0);
Hidetoshi Seto7fb06fc2009-06-15 18:18:43 +0900822 return -1;
Andi Kleen3c079792009-05-27 21:56:55 +0200823 }
824 ndelay(SPINUNIT);
825 }
826
827 /*
Huang Ying184e1fd2009-06-15 15:37:07 +0800828 * mce_callin should be read before global_nwo
829 */
830 smp_rmb();
Hidetoshi Seto7fb06fc2009-06-15 18:18:43 +0900831
832 if (order == 1) {
833 /*
834 * Monarch: Starts executing now, the others wait.
835 */
836 atomic_set(&mce_executing, 1);
837 } else {
838 /*
839 * Subject: Now start the scanning loop one by one in
840 * the original callin order.
841 * This way when there are any shared banks it will be
842 * only seen by one CPU before cleared, avoiding duplicates.
843 */
844 while (atomic_read(&mce_executing) < order) {
845 if (mce_timed_out(&timeout)) {
846 atomic_set(&global_nwo, 0);
847 return -1;
848 }
849 ndelay(SPINUNIT);
850 }
851 }
852
Huang Ying184e1fd2009-06-15 15:37:07 +0800853 /*
Andi Kleen3c079792009-05-27 21:56:55 +0200854 * Cache the global no_way_out state.
855 */
Hidetoshi Seto7fb06fc2009-06-15 18:18:43 +0900856 *no_way_out = atomic_read(&global_nwo);
Andi Kleen3c079792009-05-27 21:56:55 +0200857
Hidetoshi Seto7fb06fc2009-06-15 18:18:43 +0900858 return order;
Andi Kleen3c079792009-05-27 21:56:55 +0200859}
860
861/*
862 * Synchronize between CPUs after main scanning loop.
863 * This invokes the bulk of the Monarch processing.
864 */
865static int mce_end(int order)
866{
867 int ret = -1;
868 u64 timeout = (u64)monarch_timeout * NSEC_PER_USEC;
869
870 if (!timeout)
871 goto reset;
872 if (order < 0)
873 goto reset;
874
875 /*
876 * Allow others to run.
877 */
878 atomic_inc(&mce_executing);
879
880 if (order == 1) {
881 /* CHECKME: Can this race with a parallel hotplug? */
882 int cpus = num_online_cpus();
883
884 /*
885 * Monarch: Wait for everyone to go through their scanning
886 * loops.
887 */
888 while (atomic_read(&mce_executing) <= cpus) {
889 if (mce_timed_out(&timeout))
890 goto reset;
891 ndelay(SPINUNIT);
892 }
893
894 mce_reign();
895 barrier();
896 ret = 0;
897 } else {
898 /*
899 * Subject: Wait for Monarch to finish.
900 */
901 while (atomic_read(&mce_executing) != 0) {
902 if (mce_timed_out(&timeout))
903 goto reset;
904 ndelay(SPINUNIT);
905 }
906
907 /*
908 * Don't reset anything. That's done by the Monarch.
909 */
910 return 0;
911 }
912
913 /*
914 * Reset all global state.
915 */
916reset:
917 atomic_set(&global_nwo, 0);
918 atomic_set(&mce_callin, 0);
919 barrier();
920
921 /*
922 * Let others run again.
923 */
924 atomic_set(&mce_executing, 0);
925 return ret;
926}
927
Andi Kleen9b1beaf2009-05-27 21:56:59 +0200928/*
929 * Check if the address reported by the CPU is in a format we can parse.
930 * It would be possible to add code for most other cases, but all would
931 * be somewhat complicated (e.g. segment offset would require an instruction
Lucas De Marchi0d2eb442011-03-17 16:24:16 -0300932 * parser). So only support physical addresses up to page granuality for now.
Andi Kleen9b1beaf2009-05-27 21:56:59 +0200933 */
934static int mce_usable_address(struct mce *m)
935{
936 if (!(m->status & MCI_STATUS_MISCV) || !(m->status & MCI_STATUS_ADDRV))
937 return 0;
Hidetoshi Seto2b90e772011-06-08 10:56:56 +0900938 if (MCI_MISC_ADDR_LSB(m->misc) > PAGE_SHIFT)
Andi Kleen9b1beaf2009-05-27 21:56:59 +0200939 return 0;
Hidetoshi Seto2b90e772011-06-08 10:56:56 +0900940 if (MCI_MISC_ADDR_MODE(m->misc) != MCI_MISC_ADDR_PHYS)
Andi Kleen9b1beaf2009-05-27 21:56:59 +0200941 return 0;
942 return 1;
943}
944
Andi Kleen3c079792009-05-27 21:56:55 +0200945static void mce_clear_state(unsigned long *toclear)
946{
947 int i;
948
949 for (i = 0; i < banks; i++) {
950 if (test_bit(i, toclear))
Andi Kleena2d32bc2009-07-09 00:31:44 +0200951 mce_wrmsrl(MSR_IA32_MCx_STATUS(i), 0);
Andi Kleen3c079792009-05-27 21:56:55 +0200952 }
953}
954
955/*
Tony Luckaf104e32011-12-14 15:55:20 -0800956 * Need to save faulting physical address associated with a process
957 * in the machine check handler some place where we can grab it back
958 * later in mce_notify_process()
959 */
960#define MCE_INFO_MAX 16
961
962struct mce_info {
963 atomic_t inuse;
964 struct task_struct *t;
965 __u64 paddr;
Tony Luckdad17432012-05-14 15:07:48 -0700966 int restartable;
Tony Luckaf104e32011-12-14 15:55:20 -0800967} mce_info[MCE_INFO_MAX];
968
Tony Luckdad17432012-05-14 15:07:48 -0700969static void mce_save_info(__u64 addr, int c)
Tony Luckaf104e32011-12-14 15:55:20 -0800970{
971 struct mce_info *mi;
972
973 for (mi = mce_info; mi < &mce_info[MCE_INFO_MAX]; mi++) {
974 if (atomic_cmpxchg(&mi->inuse, 0, 1) == 0) {
975 mi->t = current;
976 mi->paddr = addr;
Tony Luckdad17432012-05-14 15:07:48 -0700977 mi->restartable = c;
Tony Luckaf104e32011-12-14 15:55:20 -0800978 return;
979 }
980 }
981
982 mce_panic("Too many concurrent recoverable errors", NULL, NULL);
983}
984
985static struct mce_info *mce_find_info(void)
986{
987 struct mce_info *mi;
988
989 for (mi = mce_info; mi < &mce_info[MCE_INFO_MAX]; mi++)
990 if (atomic_read(&mi->inuse) && mi->t == current)
991 return mi;
992 return NULL;
993}
994
995static void mce_clear_info(struct mce_info *mi)
996{
997 atomic_set(&mi->inuse, 0);
998}
999
1000/*
Andi Kleenb79109c2009-02-12 13:43:23 +01001001 * The actual machine check handler. This only handles real
1002 * exceptions when something got corrupted coming in through int 18.
1003 *
1004 * This is executed in NMI context not subject to normal locking rules. This
1005 * implies that most kernel services cannot be safely used. Don't even
1006 * think about putting a printk in there!
Andi Kleen3c079792009-05-27 21:56:55 +02001007 *
1008 * On Intel systems this is entered on all CPUs in parallel through
1009 * MCE broadcast. However some CPUs might be broken beyond repair,
1010 * so be always careful when synchronizing with others.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 */
Ingo Molnare9eee032009-04-08 12:31:17 +02001012void do_machine_check(struct pt_regs *regs, long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013{
Andi Kleen3c079792009-05-27 21:56:55 +02001014 struct mce m, *final;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 int i;
Andi Kleen3c079792009-05-27 21:56:55 +02001016 int worst = 0;
1017 int severity;
1018 /*
1019 * Establish sequential order between the CPUs entering the machine
1020 * check handler.
1021 */
Hidetoshi Seto7fb06fc2009-06-15 18:18:43 +09001022 int order;
Tim Hockinbd784322007-07-21 17:10:37 +02001023 /*
1024 * If no_way_out gets set, there is no safe way to recover from this
1025 * MCE. If tolerant is cranked up, we'll try anyway.
1026 */
1027 int no_way_out = 0;
1028 /*
1029 * If kill_it gets set, there might be a way to recover from this
1030 * error.
1031 */
1032 int kill_it = 0;
Andi Kleenb79109c2009-02-12 13:43:23 +01001033 DECLARE_BITMAP(toclear, MAX_NR_BANKS);
Tony Luck95022b82012-04-18 15:19:40 -07001034 DECLARE_BITMAP(valid_banks, MAX_NR_BANKS);
Andi Kleenbd19a5e2009-05-27 21:56:55 +02001035 char *msg = "Unknown";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
Andi Kleen553f2652006-04-07 19:49:57 +02001037 atomic_inc(&mce_entry);
1038
Alex Shic6ae41e2012-05-11 15:35:27 +08001039 this_cpu_inc(mce_exception_count);
Andi Kleen01ca79f2009-05-27 21:56:52 +02001040
Andi Kleenb79109c2009-02-12 13:43:23 +01001041 if (!banks)
Andi Kleen32561692009-05-27 21:56:53 +02001042 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
Hidetoshi Setob8325c52011-06-08 10:57:46 +09001044 mce_gather_info(&m, regs);
Andi Kleenb5f2fa42009-02-12 13:43:22 +01001045
Andi Kleen3c079792009-05-27 21:56:55 +02001046 final = &__get_cpu_var(mces_seen);
1047 *final = m;
1048
Tony Luck95022b82012-04-18 15:19:40 -07001049 memset(valid_banks, 0, sizeof(valid_banks));
Tony Luck61b0fcc2012-07-19 11:28:46 -07001050 no_way_out = mce_no_way_out(&m, &msg, valid_banks, regs);
Hidetoshi Seto680b6cf2009-08-26 16:20:36 +09001051
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 barrier();
1053
Andi Kleen3c079792009-05-27 21:56:55 +02001054 /*
Tony Lucka8c321f2012-01-03 11:45:45 -08001055 * When no restart IP might need to kill or panic.
1056 * Assume the worst for now, but if we find the
1057 * severity is MCE_AR_SEVERITY we have other options.
Andi Kleened7290d2009-05-27 21:56:57 +02001058 */
1059 if (!(m.mcgstatus & MCG_STATUS_RIPV))
1060 kill_it = 1;
1061
1062 /*
Andi Kleen3c079792009-05-27 21:56:55 +02001063 * Go through all the banks in exclusion of the other CPUs.
1064 * This way we don't report duplicated events on shared banks
1065 * because the first one to see it will clear it.
1066 */
Hidetoshi Seto7fb06fc2009-06-15 18:18:43 +09001067 order = mce_start(&no_way_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 for (i = 0; i < banks; i++) {
Andi Kleenb79109c2009-02-12 13:43:23 +01001069 __clear_bit(i, toclear);
Tony Luck95022b82012-04-18 15:19:40 -07001070 if (!test_bit(i, valid_banks))
1071 continue;
Andi Kleencebe1822009-07-09 00:31:43 +02001072 if (!mce_banks[i].ctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 continue;
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001074
1075 m.misc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 m.addr = 0;
1077 m.bank = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
Andi Kleena2d32bc2009-07-09 00:31:44 +02001079 m.status = mce_rdmsrl(MSR_IA32_MCx_STATUS(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 if ((m.status & MCI_STATUS_VAL) == 0)
1081 continue;
1082
Andi Kleenb79109c2009-02-12 13:43:23 +01001083 /*
Andi Kleened7290d2009-05-27 21:56:57 +02001084 * Non uncorrected or non signaled errors are handled by
1085 * machine_check_poll. Leave them alone, unless this panics.
Andi Kleenb79109c2009-02-12 13:43:23 +01001086 */
Andi Kleened7290d2009-05-27 21:56:57 +02001087 if (!(m.status & (mce_ser ? MCI_STATUS_S : MCI_STATUS_UC)) &&
1088 !no_way_out)
Andi Kleenb79109c2009-02-12 13:43:23 +01001089 continue;
1090
1091 /*
1092 * Set taint even when machine check was not enabled.
1093 */
1094 add_taint(TAINT_MACHINE_CHECK);
1095
Andi Kleened7290d2009-05-27 21:56:57 +02001096 severity = mce_severity(&m, tolerant, NULL);
Andi Kleenb79109c2009-02-12 13:43:23 +01001097
Andi Kleened7290d2009-05-27 21:56:57 +02001098 /*
1099 * When machine check was for corrected handler don't touch,
1100 * unless we're panicing.
1101 */
1102 if (severity == MCE_KEEP_SEVERITY && !no_way_out)
1103 continue;
1104 __set_bit(i, toclear);
1105 if (severity == MCE_NO_SEVERITY) {
Andi Kleenb79109c2009-02-12 13:43:23 +01001106 /*
1107 * Machine check event was not enabled. Clear, but
1108 * ignore.
1109 */
1110 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 }
1112
Tony Luck85f926942011-12-13 09:48:13 -08001113 mce_read_aux(&m, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
Andi Kleen9b1beaf2009-05-27 21:56:59 +02001115 /*
1116 * Action optional error. Queue address for later processing.
1117 * When the ring overflows we just ignore the AO error.
1118 * RED-PEN add some logging mechanism when
1119 * usable_address or mce_add_ring fails.
1120 * RED-PEN don't ignore overflow for tolerant == 0
1121 */
1122 if (severity == MCE_AO_SEVERITY && mce_usable_address(&m))
1123 mce_ring_add(m.addr >> PAGE_SHIFT);
1124
Andi Kleenb79109c2009-02-12 13:43:23 +01001125 mce_log(&m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
Andi Kleen3c079792009-05-27 21:56:55 +02001127 if (severity > worst) {
1128 *final = m;
1129 worst = severity;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 }
1132
Tony Lucka8c321f2012-01-03 11:45:45 -08001133 /* mce_clear_state will clear *final, save locally for use later */
1134 m = *final;
1135
Andi Kleen3c079792009-05-27 21:56:55 +02001136 if (!no_way_out)
1137 mce_clear_state(toclear);
1138
Ingo Molnare9eee032009-04-08 12:31:17 +02001139 /*
Andi Kleen3c079792009-05-27 21:56:55 +02001140 * Do most of the synchronization with other CPUs.
1141 * When there's any problem use only local no_way_out state.
Ingo Molnare9eee032009-04-08 12:31:17 +02001142 */
Andi Kleen3c079792009-05-27 21:56:55 +02001143 if (mce_end(order) < 0)
1144 no_way_out = worst >= MCE_PANIC_SEVERITY;
Tim Hockinbd784322007-07-21 17:10:37 +02001145
1146 /*
Tony Lucka8c321f2012-01-03 11:45:45 -08001147 * At insane "tolerant" levels we take no action. Otherwise
1148 * we only die if we have no other choice. For less serious
1149 * issues we try to recover, or limit damage to the current
1150 * process.
Tim Hockinbd784322007-07-21 17:10:37 +02001151 */
Tony Lucka8c321f2012-01-03 11:45:45 -08001152 if (tolerant < 3) {
1153 if (no_way_out)
1154 mce_panic("Fatal machine check on current CPU", &m, msg);
1155 if (worst == MCE_AR_SEVERITY) {
1156 /* schedule action before return to userland */
Tony Luckdad17432012-05-14 15:07:48 -07001157 mce_save_info(m.addr, m.mcgstatus & MCG_STATUS_RIPV);
Tony Lucka8c321f2012-01-03 11:45:45 -08001158 set_thread_flag(TIF_MCE_NOTIFY);
1159 } else if (kill_it) {
1160 force_sig(SIGBUS, current);
1161 }
1162 }
Tim Hockine02e68d2007-07-21 17:10:36 +02001163
Andi Kleen3c079792009-05-27 21:56:55 +02001164 if (worst > 0)
1165 mce_report_event(regs);
Andi Kleen5f8c1a52009-04-29 19:29:12 +02001166 mce_wrmsrl(MSR_IA32_MCG_STATUS, 0);
Andi Kleen32561692009-05-27 21:56:53 +02001167out:
Andi Kleen553f2652006-04-07 19:49:57 +02001168 atomic_dec(&mce_entry);
Andi Kleen88921be2009-05-27 21:56:51 +02001169 sync_core();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170}
Andi Kleenea149b32009-04-29 19:31:00 +02001171EXPORT_SYMBOL_GPL(do_machine_check);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
Tony Luckcd42f4a2011-12-15 10:48:12 -08001173#ifndef CONFIG_MEMORY_FAILURE
1174int memory_failure(unsigned long pfn, int vector, int flags)
Andi Kleen9b1beaf2009-05-27 21:56:59 +02001175{
Tony Lucka8c321f2012-01-03 11:45:45 -08001176 /* mce_severity() should not hand us an ACTION_REQUIRED error */
1177 BUG_ON(flags & MF_ACTION_REQUIRED);
Joe Perchesc767a542012-05-21 19:50:07 -07001178 pr_err("Uncorrected memory error in page 0x%lx ignored\n"
1179 "Rebuild kernel with CONFIG_MEMORY_FAILURE=y for smarter handling\n",
1180 pfn);
Tony Luckcd42f4a2011-12-15 10:48:12 -08001181
1182 return 0;
Andi Kleen9b1beaf2009-05-27 21:56:59 +02001183}
Tony Luckcd42f4a2011-12-15 10:48:12 -08001184#endif
Andi Kleen9b1beaf2009-05-27 21:56:59 +02001185
1186/*
Tony Lucka8c321f2012-01-03 11:45:45 -08001187 * Called in process context that interrupted by MCE and marked with
1188 * TIF_MCE_NOTIFY, just before returning to erroneous userland.
1189 * This code is allowed to sleep.
1190 * Attempt possible recovery such as calling the high level VM handler to
1191 * process any corrupted pages, and kill/signal current process if required.
1192 * Action required errors are handled here.
Andi Kleen9b1beaf2009-05-27 21:56:59 +02001193 */
1194void mce_notify_process(void)
1195{
1196 unsigned long pfn;
Tony Lucka8c321f2012-01-03 11:45:45 -08001197 struct mce_info *mi = mce_find_info();
Tony Luck6751ed62012-07-11 10:20:47 -07001198 int flags = MF_ACTION_REQUIRED;
Tony Lucka8c321f2012-01-03 11:45:45 -08001199
1200 if (!mi)
1201 mce_panic("Lost physical address for unconsumed uncorrectable error", NULL, NULL);
1202 pfn = mi->paddr >> PAGE_SHIFT;
1203
1204 clear_thread_flag(TIF_MCE_NOTIFY);
1205
1206 pr_err("Uncorrected hardware memory error in user-access at %llx",
1207 mi->paddr);
Tony Luckdad17432012-05-14 15:07:48 -07001208 /*
1209 * We must call memory_failure() here even if the current process is
1210 * doomed. We still need to mark the page as poisoned and alert any
1211 * other users of the page.
1212 */
Tony Luck6751ed62012-07-11 10:20:47 -07001213 if (!mi->restartable)
1214 flags |= MF_MUST_KILL;
1215 if (memory_failure(pfn, MCE_VECTOR, flags) < 0) {
Tony Lucka8c321f2012-01-03 11:45:45 -08001216 pr_err("Memory error not recovered");
1217 force_sig(SIGBUS, current);
1218 }
1219 mce_clear_info(mi);
Andi Kleen9b1beaf2009-05-27 21:56:59 +02001220}
1221
Tony Lucka8c321f2012-01-03 11:45:45 -08001222/*
1223 * Action optional processing happens here (picking up
1224 * from the list of faulting pages that do_machine_check()
1225 * placed into the "ring").
1226 */
Andi Kleen9b1beaf2009-05-27 21:56:59 +02001227static void mce_process_work(struct work_struct *dummy)
1228{
Tony Lucka8c321f2012-01-03 11:45:45 -08001229 unsigned long pfn;
1230
1231 while (mce_ring_get(&pfn))
1232 memory_failure(pfn, MCE_VECTOR, 0);
Andi Kleen9b1beaf2009-05-27 21:56:59 +02001233}
1234
Dmitriy Zavin15d5f832006-09-26 10:52:42 +02001235#ifdef CONFIG_X86_MCE_INTEL
1236/***
1237 * mce_log_therm_throt_event - Logs the thermal throttling event to mcelog
Simon Arlott676b1852007-10-20 01:25:36 +02001238 * @cpu: The CPU on which the event occurred.
Dmitriy Zavin15d5f832006-09-26 10:52:42 +02001239 * @status: Event status information
1240 *
1241 * This function should be called by the thermal interrupt after the
1242 * event has been processed and the decision was made to log the event
1243 * further.
1244 *
1245 * The status parameter will be saved to the 'status' field of 'struct mce'
1246 * and historically has been the register value of the
1247 * MSR_IA32_THERMAL_STATUS (Intel) msr.
1248 */
Andi Kleenb5f2fa42009-02-12 13:43:22 +01001249void mce_log_therm_throt_event(__u64 status)
Dmitriy Zavin15d5f832006-09-26 10:52:42 +02001250{
1251 struct mce m;
1252
Andi Kleenb5f2fa42009-02-12 13:43:22 +01001253 mce_setup(&m);
Dmitriy Zavin15d5f832006-09-26 10:52:42 +02001254 m.bank = MCE_THERMAL_BANK;
1255 m.status = status;
Dmitriy Zavin15d5f832006-09-26 10:52:42 +02001256 mce_log(&m);
1257}
1258#endif /* CONFIG_X86_MCE_INTEL */
1259
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260/*
Tim Hockin8a336b02007-05-02 19:27:19 +02001261 * Periodic polling timer for "silent" machine check errors. If the
1262 * poller finds an MCE, poll 2x faster. When the poller finds no more
1263 * errors, poll 2x slower (up to check_interval seconds).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 */
Thomas Gleixner82f7af02012-05-24 17:54:51 +00001265static unsigned long check_interval = 5 * 60; /* 5 minutes */
Ingo Molnare9eee032009-04-08 12:31:17 +02001266
Thomas Gleixner82f7af02012-05-24 17:54:51 +00001267static DEFINE_PER_CPU(unsigned long, mce_next_interval); /* in jiffies */
Andi Kleen52d168e2009-02-12 13:39:29 +01001268static DEFINE_PER_CPU(struct timer_list, mce_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
Chen Gong55babd82012-08-09 11:44:51 -07001270static unsigned long mce_adjust_timer_default(unsigned long interval)
1271{
1272 return interval;
1273}
1274
1275static unsigned long (*mce_adjust_timer)(unsigned long interval) =
1276 mce_adjust_timer_default;
1277
Thomas Gleixner82f7af02012-05-24 17:54:51 +00001278static void mce_timer_fn(unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279{
Thomas Gleixner82f7af02012-05-24 17:54:51 +00001280 struct timer_list *t = &__get_cpu_var(mce_timer);
1281 unsigned long iv;
Andi Kleen52d168e2009-02-12 13:39:29 +01001282
1283 WARN_ON(smp_processor_id() != data);
1284
Tejun Heo7b543a52010-12-18 16:30:05 +01001285 if (mce_available(__this_cpu_ptr(&cpu_info))) {
Andi Kleenee031c32009-02-12 13:49:34 +01001286 machine_check_poll(MCP_TIMESTAMP,
1287 &__get_cpu_var(mce_poll_banks));
Chen Gong55babd82012-08-09 11:44:51 -07001288 mce_intel_cmci_poll();
Ingo Molnare9eee032009-04-08 12:31:17 +02001289 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
1291 /*
Tim Hockine02e68d2007-07-21 17:10:36 +02001292 * Alert userspace if needed. If we logged an MCE, reduce the
1293 * polling interval, otherwise increase the polling interval.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 */
Thomas Gleixner82f7af02012-05-24 17:54:51 +00001295 iv = __this_cpu_read(mce_next_interval);
Chen Gong55babd82012-08-09 11:44:51 -07001296 if (mce_notify_irq()) {
Chen Gong958fb3c2012-06-05 10:35:02 +08001297 iv = max(iv / 2, (unsigned long) HZ/100);
Chen Gong55babd82012-08-09 11:44:51 -07001298 } else {
Thomas Gleixner82f7af02012-05-24 17:54:51 +00001299 iv = min(iv * 2, round_jiffies_relative(check_interval * HZ));
Chen Gong55babd82012-08-09 11:44:51 -07001300 iv = mce_adjust_timer(iv);
1301 }
Thomas Gleixner82f7af02012-05-24 17:54:51 +00001302 __this_cpu_write(mce_next_interval, iv);
Chen Gong55babd82012-08-09 11:44:51 -07001303 /* Might have become 0 after CMCI storm subsided */
1304 if (iv) {
1305 t->expires = jiffies + iv;
1306 add_timer_on(t, smp_processor_id());
1307 }
1308}
Tim Hockin8a336b02007-05-02 19:27:19 +02001309
Chen Gong55babd82012-08-09 11:44:51 -07001310/*
1311 * Ensure that the timer is firing in @interval from now.
1312 */
1313void mce_timer_kick(unsigned long interval)
1314{
1315 struct timer_list *t = &__get_cpu_var(mce_timer);
1316 unsigned long when = jiffies + interval;
1317 unsigned long iv = __this_cpu_read(mce_next_interval);
1318
1319 if (timer_pending(t)) {
1320 if (time_before(when, t->expires))
1321 mod_timer_pinned(t, when);
1322 } else {
1323 t->expires = round_jiffies(when);
1324 add_timer_on(t, smp_processor_id());
1325 }
1326 if (interval < iv)
1327 __this_cpu_write(mce_next_interval, interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328}
1329
Hidetoshi Seto9aaef962011-06-17 04:40:36 -04001330/* Must not be called in IRQ context where del_timer_sync() can deadlock */
1331static void mce_timer_delete_all(void)
1332{
1333 int cpu;
1334
1335 for_each_online_cpu(cpu)
1336 del_timer_sync(&per_cpu(mce_timer, cpu));
1337}
1338
Andi Kleen9bd98402009-02-12 13:39:28 +01001339static void mce_do_trigger(struct work_struct *work)
1340{
Hidetoshi Seto1020bcb2009-06-15 17:20:57 +09001341 call_usermodehelper(mce_helper, mce_helper_argv, NULL, UMH_NO_WAIT);
Andi Kleen9bd98402009-02-12 13:39:28 +01001342}
1343
1344static DECLARE_WORK(mce_trigger_work, mce_do_trigger);
1345
Tim Hockine02e68d2007-07-21 17:10:36 +02001346/*
Andi Kleen9bd98402009-02-12 13:39:28 +01001347 * Notify the user(s) about new machine check events.
1348 * Can be called from interrupt context, but not from machine check/NMI
1349 * context.
Tim Hockine02e68d2007-07-21 17:10:36 +02001350 */
Andi Kleen9ff36ee2009-05-27 21:56:58 +02001351int mce_notify_irq(void)
Tim Hockine02e68d2007-07-21 17:10:36 +02001352{
Andi Kleen8457c842009-02-12 13:49:33 +01001353 /* Not more than two messages every minute */
1354 static DEFINE_RATELIMIT_STATE(ratelimit, 60*HZ, 2);
1355
Hidetoshi Seto1020bcb2009-06-15 17:20:57 +09001356 if (test_and_clear_bit(0, &mce_need_notify)) {
Hidetoshi Seto93b62c32011-06-08 11:00:45 +09001357 /* wake processes polling /dev/mcelog */
1358 wake_up_interruptible(&mce_chrdev_wait);
Andi Kleen9bd98402009-02-12 13:39:28 +01001359
1360 /*
1361 * There is no risk of missing notifications because
1362 * work_pending is always cleared before the function is
1363 * executed.
1364 */
Hidetoshi Seto1020bcb2009-06-15 17:20:57 +09001365 if (mce_helper[0] && !work_pending(&mce_trigger_work))
Andi Kleen9bd98402009-02-12 13:39:28 +01001366 schedule_work(&mce_trigger_work);
Tim Hockine02e68d2007-07-21 17:10:36 +02001367
Andi Kleen8457c842009-02-12 13:49:33 +01001368 if (__ratelimit(&ratelimit))
Huang Yinga2d7b0d2010-06-08 14:35:39 +08001369 pr_info(HW_ERR "Machine check events logged\n");
Tim Hockine02e68d2007-07-21 17:10:36 +02001370
1371 return 1;
1372 }
1373 return 0;
1374}
Andi Kleen9ff36ee2009-05-27 21:56:58 +02001375EXPORT_SYMBOL_GPL(mce_notify_irq);
Tim Hockine02e68d2007-07-21 17:10:36 +02001376
Hidetoshi Setocffd3772009-11-12 15:52:40 +09001377static int __cpuinit __mcheck_cpu_mce_banks_init(void)
Andi Kleencebe1822009-07-09 00:31:43 +02001378{
1379 int i;
1380
1381 mce_banks = kzalloc(banks * sizeof(struct mce_bank), GFP_KERNEL);
1382 if (!mce_banks)
1383 return -ENOMEM;
1384 for (i = 0; i < banks; i++) {
1385 struct mce_bank *b = &mce_banks[i];
Ingo Molnar11868a22009-09-23 17:49:55 +02001386
Andi Kleencebe1822009-07-09 00:31:43 +02001387 b->ctl = -1ULL;
1388 b->init = 1;
1389 }
1390 return 0;
1391}
1392
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001393/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 * Initialize Machine Checks for a CPU.
1395 */
Borislav Petkov5e099542009-10-16 12:31:32 +02001396static int __cpuinit __mcheck_cpu_cap_init(void)
Andi Kleen0d7482e32009-02-17 23:07:13 +01001397{
Andi Kleen0d7482e32009-02-17 23:07:13 +01001398 unsigned b;
Ingo Molnare9eee032009-04-08 12:31:17 +02001399 u64 cap;
Andi Kleen0d7482e32009-02-17 23:07:13 +01001400
1401 rdmsrl(MSR_IA32_MCG_CAP, cap);
Thomas Gleixner01c66802009-04-08 12:31:24 +02001402
1403 b = cap & MCG_BANKCNT_MASK;
Roland Dreier93ae5012009-10-15 14:21:14 -07001404 if (!banks)
Joe Perchesc767a542012-05-21 19:50:07 -07001405 pr_info("CPU supports %d MCE banks\n", b);
Ingo Molnarb6592942009-04-08 12:31:27 +02001406
Andi Kleen0d7482e32009-02-17 23:07:13 +01001407 if (b > MAX_NR_BANKS) {
Joe Perchesc767a542012-05-21 19:50:07 -07001408 pr_warn("Using only %u machine check banks out of %u\n",
Andi Kleen0d7482e32009-02-17 23:07:13 +01001409 MAX_NR_BANKS, b);
1410 b = MAX_NR_BANKS;
1411 }
1412
1413 /* Don't support asymmetric configurations today */
1414 WARN_ON(banks != 0 && b != banks);
1415 banks = b;
Andi Kleencebe1822009-07-09 00:31:43 +02001416 if (!mce_banks) {
Hidetoshi Setocffd3772009-11-12 15:52:40 +09001417 int err = __mcheck_cpu_mce_banks_init();
Ingo Molnar11868a22009-09-23 17:49:55 +02001418
Andi Kleencebe1822009-07-09 00:31:43 +02001419 if (err)
1420 return err;
Andi Kleen0d7482e32009-02-17 23:07:13 +01001421 }
1422
1423 /* Use accurate RIP reporting if available. */
Thomas Gleixner01c66802009-04-08 12:31:24 +02001424 if ((cap & MCG_EXT_P) && MCG_EXT_CNT(cap) >= 9)
Andi Kleen0d7482e32009-02-17 23:07:13 +01001425 rip_msr = MSR_IA32_MCG_EIP;
1426
Andi Kleened7290d2009-05-27 21:56:57 +02001427 if (cap & MCG_SER_P)
1428 mce_ser = 1;
1429
Andi Kleen0d7482e32009-02-17 23:07:13 +01001430 return 0;
1431}
1432
Borislav Petkov5e099542009-10-16 12:31:32 +02001433static void __mcheck_cpu_init_generic(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434{
Ingo Molnare9eee032009-04-08 12:31:17 +02001435 mce_banks_t all_banks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 u64 cap;
1437 int i;
1438
Andi Kleenb79109c2009-02-12 13:43:23 +01001439 /*
1440 * Log the machine checks left over from the previous reset.
1441 */
Andi Kleenee031c32009-02-12 13:49:34 +01001442 bitmap_fill(all_banks, MAX_NR_BANKS);
Andi Kleen5679af42009-04-07 17:06:55 +02001443 machine_check_poll(MCP_UC|(!mce_bootlog ? MCP_DONTLOG : 0), &all_banks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
1445 set_in_cr4(X86_CR4_MCE);
1446
Andi Kleen0d7482e32009-02-17 23:07:13 +01001447 rdmsrl(MSR_IA32_MCG_CAP, cap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 if (cap & MCG_CTL_P)
1449 wrmsr(MSR_IA32_MCG_CTL, 0xffffffff, 0xffffffff);
1450
1451 for (i = 0; i < banks; i++) {
Andi Kleencebe1822009-07-09 00:31:43 +02001452 struct mce_bank *b = &mce_banks[i];
Ingo Molnar11868a22009-09-23 17:49:55 +02001453
Andi Kleencebe1822009-07-09 00:31:43 +02001454 if (!b->init)
Andi Kleen06b7a7a2009-04-27 18:37:43 +02001455 continue;
Andi Kleena2d32bc2009-07-09 00:31:44 +02001456 wrmsrl(MSR_IA32_MCx_CTL(i), b->ctl);
1457 wrmsrl(MSR_IA32_MCx_STATUS(i), 0);
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001458 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459}
1460
Tony Luck61b0fcc2012-07-19 11:28:46 -07001461/*
1462 * During IFU recovery Sandy Bridge -EP4S processors set the RIPV and
1463 * EIPV bits in MCG_STATUS to zero on the affected logical processor (SDM
1464 * Vol 3B Table 15-20). But this confuses both the code that determines
1465 * whether the machine check occurred in kernel or user mode, and also
1466 * the severity assessment code. Pretend that EIPV was set, and take the
1467 * ip/cs values from the pt_regs that mce_gather_info() ignored earlier.
1468 */
1469static void quirk_sandybridge_ifu(int bank, struct mce *m, struct pt_regs *regs)
1470{
1471 if (bank != 0)
1472 return;
1473 if ((m->mcgstatus & (MCG_STATUS_EIPV|MCG_STATUS_RIPV)) != 0)
1474 return;
1475 if ((m->status & (MCI_STATUS_OVER|MCI_STATUS_UC|
1476 MCI_STATUS_EN|MCI_STATUS_MISCV|MCI_STATUS_ADDRV|
1477 MCI_STATUS_PCC|MCI_STATUS_S|MCI_STATUS_AR|
1478 MCACOD)) !=
1479 (MCI_STATUS_UC|MCI_STATUS_EN|
1480 MCI_STATUS_MISCV|MCI_STATUS_ADDRV|MCI_STATUS_S|
1481 MCI_STATUS_AR|MCACOD_INSTR))
1482 return;
1483
1484 m->mcgstatus |= MCG_STATUS_EIPV;
1485 m->ip = regs->ip;
1486 m->cs = regs->cs;
1487}
1488
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489/* Add per CPU specific workarounds here */
Borislav Petkov5e099542009-10-16 12:31:32 +02001490static int __cpuinit __mcheck_cpu_apply_quirks(struct cpuinfo_x86 *c)
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001491{
Ingo Molnare412cd22009-08-17 10:19:00 +02001492 if (c->x86_vendor == X86_VENDOR_UNKNOWN) {
Joe Perchesc767a542012-05-21 19:50:07 -07001493 pr_info("unknown CPU type - not enabling MCE support\n");
Ingo Molnare412cd22009-08-17 10:19:00 +02001494 return -EOPNOTSUPP;
1495 }
1496
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 /* This should be disabled by the BIOS, but isn't always */
Jan Beulich911f6a72008-04-22 16:22:21 +01001498 if (c->x86_vendor == X86_VENDOR_AMD) {
Ingo Molnare9eee032009-04-08 12:31:17 +02001499 if (c->x86 == 15 && banks > 4) {
1500 /*
1501 * disable GART TBL walk error reporting, which
1502 * trips off incorrectly with the IOMMU & 3ware
1503 * & Cerberus:
1504 */
Andi Kleencebe1822009-07-09 00:31:43 +02001505 clear_bit(10, (unsigned long *)&mce_banks[4].ctl);
Ingo Molnare9eee032009-04-08 12:31:17 +02001506 }
1507 if (c->x86 <= 17 && mce_bootlog < 0) {
1508 /*
1509 * Lots of broken BIOS around that don't clear them
1510 * by default and leave crap in there. Don't log:
1511 */
Jan Beulich911f6a72008-04-22 16:22:21 +01001512 mce_bootlog = 0;
Ingo Molnare9eee032009-04-08 12:31:17 +02001513 }
Andi Kleen2e6f6942009-04-27 18:42:48 +02001514 /*
1515 * Various K7s with broken bank 0 around. Always disable
1516 * by default.
1517 */
Andi Kleen203abd62009-06-15 14:52:01 +02001518 if (c->x86 == 6 && banks > 0)
Andi Kleencebe1822009-07-09 00:31:43 +02001519 mce_banks[0].ctl = 0;
Borislav Petkov575203b2012-04-20 18:01:34 +02001520
1521 /*
1522 * Turn off MC4_MISC thresholding banks on those models since
1523 * they're not supported there.
1524 */
1525 if (c->x86 == 0x15 &&
1526 (c->x86_model >= 0x10 && c->x86_model <= 0x1f)) {
1527 int i;
1528 u64 val, hwcr;
1529 bool need_toggle;
1530 u32 msrs[] = {
1531 0x00000413, /* MC4_MISC0 */
1532 0xc0000408, /* MC4_MISC1 */
1533 };
1534
1535 rdmsrl(MSR_K7_HWCR, hwcr);
1536
1537 /* McStatusWrEn has to be set */
1538 need_toggle = !(hwcr & BIT(18));
1539
1540 if (need_toggle)
1541 wrmsrl(MSR_K7_HWCR, hwcr | BIT(18));
1542
1543 for (i = 0; i < ARRAY_SIZE(msrs); i++) {
1544 rdmsrl(msrs[i], val);
1545
1546 /* CntP bit set? */
Borislav Petkov80f0336102012-05-22 12:53:46 +02001547 if (val & BIT_64(62)) {
1548 val &= ~BIT_64(62);
1549 wrmsrl(msrs[i], val);
Borislav Petkov575203b2012-04-20 18:01:34 +02001550 }
1551 }
1552
1553 /* restore old settings */
1554 if (need_toggle)
1555 wrmsrl(MSR_K7_HWCR, hwcr);
1556 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 }
Andi Kleene5835382005-11-05 17:25:54 +01001558
Andi Kleen06b7a7a2009-04-27 18:37:43 +02001559 if (c->x86_vendor == X86_VENDOR_INTEL) {
1560 /*
1561 * SDM documents that on family 6 bank 0 should not be written
1562 * because it aliases to another special BIOS controlled
1563 * register.
1564 * But it's not aliased anymore on model 0x1a+
1565 * Don't ignore bank 0 completely because there could be a
1566 * valid event later, merely don't write CTL0.
1567 */
1568
Andi Kleencebe1822009-07-09 00:31:43 +02001569 if (c->x86 == 6 && c->x86_model < 0x1A && banks > 0)
1570 mce_banks[0].init = 0;
Andi Kleen3c079792009-05-27 21:56:55 +02001571
1572 /*
1573 * All newer Intel systems support MCE broadcasting. Enable
1574 * synchronization with a one second timeout.
1575 */
1576 if ((c->x86 > 6 || (c->x86 == 6 && c->x86_model >= 0xe)) &&
1577 monarch_timeout < 0)
1578 monarch_timeout = USEC_PER_SEC;
Bartlomiej Zolnierkiewiczc7f6fa42009-07-28 23:52:54 +02001579
Ingo Molnare412cd22009-08-17 10:19:00 +02001580 /*
1581 * There are also broken BIOSes on some Pentium M and
1582 * earlier systems:
1583 */
1584 if (c->x86 == 6 && c->x86_model <= 13 && mce_bootlog < 0)
Bartlomiej Zolnierkiewiczc7f6fa42009-07-28 23:52:54 +02001585 mce_bootlog = 0;
Tony Luck61b0fcc2012-07-19 11:28:46 -07001586
1587 if (c->x86 == 6 && c->x86_model == 45)
1588 quirk_no_way_out = quirk_sandybridge_ifu;
Andi Kleen06b7a7a2009-04-27 18:37:43 +02001589 }
Andi Kleen3c079792009-05-27 21:56:55 +02001590 if (monarch_timeout < 0)
1591 monarch_timeout = 0;
Andi Kleen29b0f592009-05-27 21:56:56 +02001592 if (mce_bootlog != 0)
1593 mce_panic_timeout = 30;
Ingo Molnare412cd22009-08-17 10:19:00 +02001594
1595 return 0;
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001596}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597
Hidetoshi Seto3a97fc32011-06-08 10:58:35 +09001598static int __cpuinit __mcheck_cpu_ancient_init(struct cpuinfo_x86 *c)
Andi Kleen4efc0672009-04-28 19:07:31 +02001599{
1600 if (c->x86 != 5)
Hidetoshi Seto3a97fc32011-06-08 10:58:35 +09001601 return 0;
1602
Andi Kleen4efc0672009-04-28 19:07:31 +02001603 switch (c->x86_vendor) {
1604 case X86_VENDOR_INTEL:
Hidetoshi Setoc6978362009-06-15 17:22:49 +09001605 intel_p5_mcheck_init(c);
Hidetoshi Seto3a97fc32011-06-08 10:58:35 +09001606 return 1;
Andi Kleen4efc0672009-04-28 19:07:31 +02001607 break;
1608 case X86_VENDOR_CENTAUR:
1609 winchip_mcheck_init(c);
Hidetoshi Seto3a97fc32011-06-08 10:58:35 +09001610 return 1;
Andi Kleen4efc0672009-04-28 19:07:31 +02001611 break;
1612 }
Hidetoshi Seto3a97fc32011-06-08 10:58:35 +09001613
1614 return 0;
Andi Kleen4efc0672009-04-28 19:07:31 +02001615}
1616
Borislav Petkov5e099542009-10-16 12:31:32 +02001617static void __mcheck_cpu_init_vendor(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618{
1619 switch (c->x86_vendor) {
1620 case X86_VENDOR_INTEL:
1621 mce_intel_feature_init(c);
Chen Gong55babd82012-08-09 11:44:51 -07001622 mce_adjust_timer = mce_intel_adjust_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 break;
Jacob Shin89b831e2005-11-05 17:25:53 +01001624 case X86_VENDOR_AMD:
1625 mce_amd_feature_init(c);
1626 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 default:
1628 break;
1629 }
1630}
1631
Thomas Gleixner26c3c282012-07-19 13:59:39 -04001632static void mce_start_timer(unsigned int cpu, struct timer_list *t)
1633{
Chen Gong55babd82012-08-09 11:44:51 -07001634 unsigned long iv = mce_adjust_timer(check_interval * HZ);
Thomas Gleixner26c3c282012-07-19 13:59:39 -04001635
1636 __this_cpu_write(mce_next_interval, iv);
1637
1638 if (mce_ignore_ce || !iv)
1639 return;
1640
1641 t->expires = round_jiffies(jiffies + iv);
1642 add_timer_on(t, smp_processor_id());
1643}
1644
Borislav Petkov5e099542009-10-16 12:31:32 +02001645static void __mcheck_cpu_init_timer(void)
Andi Kleen52d168e2009-02-12 13:39:29 +01001646{
1647 struct timer_list *t = &__get_cpu_var(mce_timer);
Thomas Gleixner26c3c282012-07-19 13:59:39 -04001648 unsigned int cpu = smp_processor_id();
Andi Kleen52d168e2009-02-12 13:39:29 +01001649
Thomas Gleixner26c3c282012-07-19 13:59:39 -04001650 setup_timer(t, mce_timer_fn, cpu);
1651 mce_start_timer(cpu, t);
Andi Kleen52d168e2009-02-12 13:39:29 +01001652}
1653
Andi Kleen9eda8cb2009-07-09 00:31:42 +02001654/* Handle unconfigured int18 (should never happen) */
1655static void unexpected_machine_check(struct pt_regs *regs, long error_code)
1656{
Joe Perchesc767a542012-05-21 19:50:07 -07001657 pr_err("CPU#%d: Unexpected int18 (Machine Check)\n",
Andi Kleen9eda8cb2009-07-09 00:31:42 +02001658 smp_processor_id());
1659}
1660
1661/* Call the installed machine check handler for this CPU setup. */
1662void (*machine_check_vector)(struct pt_regs *, long error_code) =
1663 unexpected_machine_check;
1664
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001665/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 * Called for each booted CPU to set up machine checks.
Ingo Molnare9eee032009-04-08 12:31:17 +02001667 * Must be called with preempt off:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 */
Borislav Petkov5e099542009-10-16 12:31:32 +02001669void __cpuinit mcheck_cpu_init(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670{
Andi Kleen4efc0672009-04-28 19:07:31 +02001671 if (mce_disabled)
1672 return;
1673
Hidetoshi Seto3a97fc32011-06-08 10:58:35 +09001674 if (__mcheck_cpu_ancient_init(c))
1675 return;
Andi Kleen4efc0672009-04-28 19:07:31 +02001676
Andi Kleen5b4408f2009-02-12 13:39:30 +01001677 if (!mce_available(c))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 return;
1679
Borislav Petkov5e099542009-10-16 12:31:32 +02001680 if (__mcheck_cpu_cap_init() < 0 || __mcheck_cpu_apply_quirks(c) < 0) {
Andi Kleen04b2b1a2009-04-28 22:50:19 +02001681 mce_disabled = 1;
Andi Kleen0d7482e32009-02-17 23:07:13 +01001682 return;
1683 }
Andi Kleen0d7482e32009-02-17 23:07:13 +01001684
Andi Kleen5d727922009-04-27 19:25:48 +02001685 machine_check_vector = do_machine_check;
1686
Borislav Petkov5e099542009-10-16 12:31:32 +02001687 __mcheck_cpu_init_generic();
1688 __mcheck_cpu_init_vendor(c);
1689 __mcheck_cpu_init_timer();
Andi Kleen9b1beaf2009-05-27 21:56:59 +02001690 INIT_WORK(&__get_cpu_var(mce_work), mce_process_work);
Hidetoshi Setob77e70b2011-06-08 10:56:02 +09001691 init_irq_work(&__get_cpu_var(mce_irq_work), &mce_irq_work_cb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692}
1693
1694/*
Hidetoshi Seto93b62c32011-06-08 11:00:45 +09001695 * mce_chrdev: Character device /dev/mcelog to read and clear the MCE log.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 */
1697
Hidetoshi Seto93b62c32011-06-08 11:00:45 +09001698static DEFINE_SPINLOCK(mce_chrdev_state_lock);
1699static int mce_chrdev_open_count; /* #times opened */
1700static int mce_chrdev_open_exclu; /* already open exclusive? */
Tim Hockinf528e7b2007-07-21 17:10:35 +02001701
Hidetoshi Seto93b62c32011-06-08 11:00:45 +09001702static int mce_chrdev_open(struct inode *inode, struct file *file)
Tim Hockinf528e7b2007-07-21 17:10:35 +02001703{
Hidetoshi Seto93b62c32011-06-08 11:00:45 +09001704 spin_lock(&mce_chrdev_state_lock);
Tim Hockinf528e7b2007-07-21 17:10:35 +02001705
Hidetoshi Seto93b62c32011-06-08 11:00:45 +09001706 if (mce_chrdev_open_exclu ||
1707 (mce_chrdev_open_count && (file->f_flags & O_EXCL))) {
1708 spin_unlock(&mce_chrdev_state_lock);
Ingo Molnare9eee032009-04-08 12:31:17 +02001709
Tim Hockinf528e7b2007-07-21 17:10:35 +02001710 return -EBUSY;
1711 }
1712
1713 if (file->f_flags & O_EXCL)
Hidetoshi Seto93b62c32011-06-08 11:00:45 +09001714 mce_chrdev_open_exclu = 1;
1715 mce_chrdev_open_count++;
Tim Hockinf528e7b2007-07-21 17:10:35 +02001716
Hidetoshi Seto93b62c32011-06-08 11:00:45 +09001717 spin_unlock(&mce_chrdev_state_lock);
Tim Hockinf528e7b2007-07-21 17:10:35 +02001718
Tim Hockinbd784322007-07-21 17:10:37 +02001719 return nonseekable_open(inode, file);
Tim Hockinf528e7b2007-07-21 17:10:35 +02001720}
1721
Hidetoshi Seto93b62c32011-06-08 11:00:45 +09001722static int mce_chrdev_release(struct inode *inode, struct file *file)
Tim Hockinf528e7b2007-07-21 17:10:35 +02001723{
Hidetoshi Seto93b62c32011-06-08 11:00:45 +09001724 spin_lock(&mce_chrdev_state_lock);
Tim Hockinf528e7b2007-07-21 17:10:35 +02001725
Hidetoshi Seto93b62c32011-06-08 11:00:45 +09001726 mce_chrdev_open_count--;
1727 mce_chrdev_open_exclu = 0;
Tim Hockinf528e7b2007-07-21 17:10:35 +02001728
Hidetoshi Seto93b62c32011-06-08 11:00:45 +09001729 spin_unlock(&mce_chrdev_state_lock);
Tim Hockinf528e7b2007-07-21 17:10:35 +02001730
1731 return 0;
1732}
1733
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001734static void collect_tscs(void *data)
1735{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 unsigned long *cpu_tsc = (unsigned long *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001738 rdtscll(cpu_tsc[smp_processor_id()]);
1739}
1740
Huang Ying482908b2010-05-18 14:35:22 +08001741static int mce_apei_read_done;
1742
1743/* Collect MCE record of previous boot in persistent storage via APEI ERST. */
1744static int __mce_read_apei(char __user **ubuf, size_t usize)
1745{
1746 int rc;
1747 u64 record_id;
1748 struct mce m;
1749
1750 if (usize < sizeof(struct mce))
1751 return -EINVAL;
1752
1753 rc = apei_read_mce(&m, &record_id);
1754 /* Error or no more MCE record */
1755 if (rc <= 0) {
1756 mce_apei_read_done = 1;
Naoya Horiguchifadd85f2012-01-23 15:54:52 -05001757 /*
1758 * When ERST is disabled, mce_chrdev_read() should return
1759 * "no record" instead of "no device."
1760 */
1761 if (rc == -ENODEV)
1762 return 0;
Huang Ying482908b2010-05-18 14:35:22 +08001763 return rc;
1764 }
1765 rc = -EFAULT;
1766 if (copy_to_user(*ubuf, &m, sizeof(struct mce)))
1767 return rc;
1768 /*
1769 * In fact, we should have cleared the record after that has
1770 * been flushed to the disk or sent to network in
1771 * /sbin/mcelog, but we have no interface to support that now,
1772 * so just clear it to avoid duplication.
1773 */
1774 rc = apei_clear_mce(record_id);
1775 if (rc) {
1776 mce_apei_read_done = 1;
1777 return rc;
1778 }
1779 *ubuf += sizeof(struct mce);
1780
1781 return 0;
1782}
1783
Hidetoshi Seto93b62c32011-06-08 11:00:45 +09001784static ssize_t mce_chrdev_read(struct file *filp, char __user *ubuf,
1785 size_t usize, loff_t *off)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 char __user *buf = ubuf;
Ingo Molnare9eee032009-04-08 12:31:17 +02001788 unsigned long *cpu_tsc;
1789 unsigned prev, next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 int i, err;
1791
Mike Travis6bca67f2008-07-18 18:11:27 -07001792 cpu_tsc = kmalloc(nr_cpu_ids * sizeof(long), GFP_KERNEL);
Andi Kleenf0de53b2005-04-16 15:25:10 -07001793 if (!cpu_tsc)
1794 return -ENOMEM;
1795
Hidetoshi Seto93b62c32011-06-08 11:00:45 +09001796 mutex_lock(&mce_chrdev_read_mutex);
Huang Ying482908b2010-05-18 14:35:22 +08001797
1798 if (!mce_apei_read_done) {
1799 err = __mce_read_apei(&buf, usize);
1800 if (err || buf != ubuf)
1801 goto out;
1802 }
1803
Paul E. McKenneyf56e8a02010-03-05 15:03:27 -08001804 next = rcu_dereference_check_mce(mcelog.next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
1806 /* Only supports full reads right now */
Huang Ying482908b2010-05-18 14:35:22 +08001807 err = -EINVAL;
1808 if (*off != 0 || usize < MCE_LOG_LEN*sizeof(struct mce))
1809 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810
1811 err = 0;
Huang Yingef41df4342009-02-12 13:39:34 +01001812 prev = 0;
1813 do {
1814 for (i = prev; i < next; i++) {
1815 unsigned long start = jiffies;
Hidetoshi Seto559faa62011-06-08 11:00:08 +09001816 struct mce *m = &mcelog.entry[i];
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001817
Hidetoshi Seto559faa62011-06-08 11:00:08 +09001818 while (!m->finished) {
Huang Yingef41df4342009-02-12 13:39:34 +01001819 if (time_after_eq(jiffies, start + 2)) {
Hidetoshi Seto559faa62011-06-08 11:00:08 +09001820 memset(m, 0, sizeof(*m));
Huang Yingef41df4342009-02-12 13:39:34 +01001821 goto timeout;
1822 }
1823 cpu_relax();
Andi Kleen673242c2005-09-12 18:49:24 +02001824 }
Huang Yingef41df4342009-02-12 13:39:34 +01001825 smp_rmb();
Hidetoshi Seto559faa62011-06-08 11:00:08 +09001826 err |= copy_to_user(buf, m, sizeof(*m));
1827 buf += sizeof(*m);
Huang Yingef41df4342009-02-12 13:39:34 +01001828timeout:
1829 ;
Andi Kleen673242c2005-09-12 18:49:24 +02001830 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831
Huang Yingef41df4342009-02-12 13:39:34 +01001832 memset(mcelog.entry + prev, 0,
1833 (next - prev) * sizeof(struct mce));
1834 prev = next;
1835 next = cmpxchg(&mcelog.next, prev, 0);
1836 } while (next != prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837
Paul E. McKenneyb2b18662005-06-25 14:55:38 -07001838 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001840 /*
1841 * Collect entries that were still getting written before the
1842 * synchronize.
1843 */
Jens Axboe15c8b6c2008-05-09 09:39:44 +02001844 on_each_cpu(collect_tscs, cpu_tsc, 1);
Ingo Molnare9eee032009-04-08 12:31:17 +02001845
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001846 for (i = next; i < MCE_LOG_LEN; i++) {
Hidetoshi Seto559faa62011-06-08 11:00:08 +09001847 struct mce *m = &mcelog.entry[i];
1848
1849 if (m->finished && m->tsc < cpu_tsc[m->cpu]) {
1850 err |= copy_to_user(buf, m, sizeof(*m));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 smp_rmb();
Hidetoshi Seto559faa62011-06-08 11:00:08 +09001852 buf += sizeof(*m);
1853 memset(m, 0, sizeof(*m));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 }
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001855 }
Huang Ying482908b2010-05-18 14:35:22 +08001856
1857 if (err)
1858 err = -EFAULT;
1859
1860out:
Hidetoshi Seto93b62c32011-06-08 11:00:45 +09001861 mutex_unlock(&mce_chrdev_read_mutex);
Andi Kleenf0de53b2005-04-16 15:25:10 -07001862 kfree(cpu_tsc);
Ingo Molnare9eee032009-04-08 12:31:17 +02001863
Huang Ying482908b2010-05-18 14:35:22 +08001864 return err ? err : buf - ubuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865}
1866
Hidetoshi Seto93b62c32011-06-08 11:00:45 +09001867static unsigned int mce_chrdev_poll(struct file *file, poll_table *wait)
Tim Hockine02e68d2007-07-21 17:10:36 +02001868{
Hidetoshi Seto93b62c32011-06-08 11:00:45 +09001869 poll_wait(file, &mce_chrdev_wait, wait);
Paul E. McKenneya4dd9922011-04-01 07:15:14 -07001870 if (rcu_access_index(mcelog.next))
Tim Hockine02e68d2007-07-21 17:10:36 +02001871 return POLLIN | POLLRDNORM;
Huang Ying482908b2010-05-18 14:35:22 +08001872 if (!mce_apei_read_done && apei_check_mce())
1873 return POLLIN | POLLRDNORM;
Tim Hockine02e68d2007-07-21 17:10:36 +02001874 return 0;
1875}
1876
Hidetoshi Seto93b62c32011-06-08 11:00:45 +09001877static long mce_chrdev_ioctl(struct file *f, unsigned int cmd,
1878 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879{
1880 int __user *p = (int __user *)arg;
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001881
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 if (!capable(CAP_SYS_ADMIN))
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001883 return -EPERM;
Ingo Molnare9eee032009-04-08 12:31:17 +02001884
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 switch (cmd) {
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001886 case MCE_GET_RECORD_LEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 return put_user(sizeof(struct mce), p);
1888 case MCE_GET_LOG_LEN:
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001889 return put_user(MCE_LOG_LEN, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 case MCE_GETCLEAR_FLAGS: {
1891 unsigned flags;
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001892
1893 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 flags = mcelog.flags;
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001895 } while (cmpxchg(&mcelog.flags, flags, 0) != flags);
Ingo Molnare9eee032009-04-08 12:31:17 +02001896
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001897 return put_user(flags, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 }
1899 default:
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001900 return -ENOTTY;
1901 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902}
1903
Luck, Tony66f5ddf2011-11-03 11:46:47 -07001904static ssize_t (*mce_write)(struct file *filp, const char __user *ubuf,
1905 size_t usize, loff_t *off);
1906
1907void register_mce_write_callback(ssize_t (*fn)(struct file *filp,
1908 const char __user *ubuf,
1909 size_t usize, loff_t *off))
1910{
1911 mce_write = fn;
1912}
1913EXPORT_SYMBOL_GPL(register_mce_write_callback);
1914
1915ssize_t mce_chrdev_write(struct file *filp, const char __user *ubuf,
1916 size_t usize, loff_t *off)
1917{
1918 if (mce_write)
1919 return mce_write(filp, ubuf, usize, off);
1920 else
1921 return -EINVAL;
1922}
1923
1924static const struct file_operations mce_chrdev_ops = {
Hidetoshi Seto93b62c32011-06-08 11:00:45 +09001925 .open = mce_chrdev_open,
1926 .release = mce_chrdev_release,
1927 .read = mce_chrdev_read,
Luck, Tony66f5ddf2011-11-03 11:46:47 -07001928 .write = mce_chrdev_write,
Hidetoshi Seto93b62c32011-06-08 11:00:45 +09001929 .poll = mce_chrdev_poll,
1930 .unlocked_ioctl = mce_chrdev_ioctl,
1931 .llseek = no_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932};
1933
Hidetoshi Seto93b62c32011-06-08 11:00:45 +09001934static struct miscdevice mce_chrdev_device = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 MISC_MCELOG_MINOR,
1936 "mcelog",
1937 &mce_chrdev_ops,
1938};
1939
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001940/*
Hidetoshi Seto62fdac52009-06-11 16:06:07 +09001941 * mce=off Disables machine check
1942 * mce=no_cmci Disables CMCI
1943 * mce=dont_log_ce Clears corrected events silently, no log created for CEs.
1944 * mce=ignore_ce Disables polling and CMCI, corrected events are not cleared.
Andi Kleen3c079792009-05-27 21:56:55 +02001945 * mce=TOLERANCELEVEL[,monarchtimeout] (number, see above)
1946 * monarchtimeout is how long to wait for other CPUs on machine
1947 * check, or 0 to not wait
Hidetoshi Seto13503fa2009-03-26 17:39:20 +09001948 * mce=bootlog Log MCEs from before booting. Disabled by default on AMD.
1949 * mce=nobootlog Don't log MCEs from before booting.
Naveen N. Rao450cc202012-09-27 10:08:00 -07001950 * mce=bios_cmci_threshold Don't program the CMCI threshold
Hidetoshi Seto13503fa2009-03-26 17:39:20 +09001951 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952static int __init mcheck_enable(char *str)
1953{
Bartlomiej Zolnierkiewicze3346fc2009-07-28 23:55:09 +02001954 if (*str == 0) {
Andi Kleen4efc0672009-04-28 19:07:31 +02001955 enable_p5_mce();
Bartlomiej Zolnierkiewicze3346fc2009-07-28 23:55:09 +02001956 return 1;
1957 }
Andi Kleen4efc0672009-04-28 19:07:31 +02001958 if (*str == '=')
1959 str++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 if (!strcmp(str, "off"))
Andi Kleen04b2b1a2009-04-28 22:50:19 +02001961 mce_disabled = 1;
Hidetoshi Seto62fdac52009-06-11 16:06:07 +09001962 else if (!strcmp(str, "no_cmci"))
1963 mce_cmci_disabled = 1;
1964 else if (!strcmp(str, "dont_log_ce"))
1965 mce_dont_log_ce = 1;
1966 else if (!strcmp(str, "ignore_ce"))
1967 mce_ignore_ce = 1;
Hidetoshi Seto13503fa2009-03-26 17:39:20 +09001968 else if (!strcmp(str, "bootlog") || !strcmp(str, "nobootlog"))
1969 mce_bootlog = (str[0] == 'b');
Naveen N. Rao450cc202012-09-27 10:08:00 -07001970 else if (!strcmp(str, "bios_cmci_threshold"))
1971 mce_bios_cmci_threshold = 1;
Andi Kleen3c079792009-05-27 21:56:55 +02001972 else if (isdigit(str[0])) {
Andi Kleen8c566ef2005-09-12 18:49:24 +02001973 get_option(&str, &tolerant);
Andi Kleen3c079792009-05-27 21:56:55 +02001974 if (*str == ',') {
1975 ++str;
1976 get_option(&str, &monarch_timeout);
1977 }
1978 } else {
Joe Perchesc767a542012-05-21 19:50:07 -07001979 pr_info("mce argument %s ignored. Please use /sys\n", str);
Hidetoshi Seto13503fa2009-03-26 17:39:20 +09001980 return 0;
1981 }
OGAWA Hirofumi9b410462006-03-31 02:30:33 -08001982 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983}
Andi Kleen4efc0672009-04-28 19:07:31 +02001984__setup("mce", mcheck_enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985
Yong Wanga2202aa2009-11-10 09:38:24 +08001986int __init mcheck_init(void)
Borislav Petkovb33a6362009-10-16 12:31:33 +02001987{
Yong Wanga2202aa2009-11-10 09:38:24 +08001988 mcheck_intel_therm_init();
1989
Borislav Petkovb33a6362009-10-16 12:31:33 +02001990 return 0;
1991}
Borislav Petkovb33a6362009-10-16 12:31:33 +02001992
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001993/*
Hidetoshi Setoc7cece82011-06-08 11:02:03 +09001994 * mce_syscore: PM support
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001995 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996
Andi Kleen973a2dd2009-02-12 13:39:32 +01001997/*
1998 * Disable machine checks on suspend and shutdown. We can't really handle
1999 * them later.
2000 */
Borislav Petkov5e099542009-10-16 12:31:32 +02002001static int mce_disable_error_reporting(void)
Andi Kleen973a2dd2009-02-12 13:39:32 +01002002{
2003 int i;
2004
Andi Kleen06b7a7a2009-04-27 18:37:43 +02002005 for (i = 0; i < banks; i++) {
Andi Kleencebe1822009-07-09 00:31:43 +02002006 struct mce_bank *b = &mce_banks[i];
Ingo Molnar11868a22009-09-23 17:49:55 +02002007
Andi Kleencebe1822009-07-09 00:31:43 +02002008 if (b->init)
Andi Kleena2d32bc2009-07-09 00:31:44 +02002009 wrmsrl(MSR_IA32_MCx_CTL(i), 0);
Andi Kleen06b7a7a2009-04-27 18:37:43 +02002010 }
Andi Kleen973a2dd2009-02-12 13:39:32 +01002011 return 0;
2012}
2013
Hidetoshi Setoc7cece82011-06-08 11:02:03 +09002014static int mce_syscore_suspend(void)
Andi Kleen973a2dd2009-02-12 13:39:32 +01002015{
Borislav Petkov5e099542009-10-16 12:31:32 +02002016 return mce_disable_error_reporting();
Andi Kleen973a2dd2009-02-12 13:39:32 +01002017}
2018
Hidetoshi Setoc7cece82011-06-08 11:02:03 +09002019static void mce_syscore_shutdown(void)
Andi Kleen973a2dd2009-02-12 13:39:32 +01002020{
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +01002021 mce_disable_error_reporting();
Andi Kleen973a2dd2009-02-12 13:39:32 +01002022}
2023
Ingo Molnare9eee032009-04-08 12:31:17 +02002024/*
2025 * On resume clear all MCE state. Don't want to see leftovers from the BIOS.
2026 * Only one CPU is active at this time, the others get re-added later using
2027 * CPU hotplug:
2028 */
Hidetoshi Setoc7cece82011-06-08 11:02:03 +09002029static void mce_syscore_resume(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030{
Borislav Petkov5e099542009-10-16 12:31:32 +02002031 __mcheck_cpu_init_generic();
Tejun Heo7b543a52010-12-18 16:30:05 +01002032 __mcheck_cpu_init_vendor(__this_cpu_ptr(&cpu_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033}
2034
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +01002035static struct syscore_ops mce_syscore_ops = {
Hidetoshi Setoc7cece82011-06-08 11:02:03 +09002036 .suspend = mce_syscore_suspend,
2037 .shutdown = mce_syscore_shutdown,
2038 .resume = mce_syscore_resume,
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +01002039};
2040
Hidetoshi Setoc7cece82011-06-08 11:02:03 +09002041/*
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002042 * mce_device: Sysfs support
Hidetoshi Setoc7cece82011-06-08 11:02:03 +09002043 */
2044
Andi Kleen52d168e2009-02-12 13:39:29 +01002045static void mce_cpu_restart(void *data)
2046{
Tejun Heo7b543a52010-12-18 16:30:05 +01002047 if (!mce_available(__this_cpu_ptr(&cpu_info)))
Hidetoshi Seto33edbf02009-06-15 17:18:45 +09002048 return;
Borislav Petkov5e099542009-10-16 12:31:32 +02002049 __mcheck_cpu_init_generic();
2050 __mcheck_cpu_init_timer();
Andi Kleen52d168e2009-02-12 13:39:29 +01002051}
2052
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053/* Reinit MCEs after user configuration changes */
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02002054static void mce_restart(void)
2055{
Hidetoshi Seto9aaef962011-06-17 04:40:36 -04002056 mce_timer_delete_all();
Andi Kleen52d168e2009-02-12 13:39:29 +01002057 on_each_cpu(mce_cpu_restart, NULL, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058}
2059
Hidetoshi Seto9af43b52009-06-15 17:21:36 +09002060/* Toggle features for corrected errors */
Hidetoshi Seto9aaef962011-06-17 04:40:36 -04002061static void mce_disable_cmci(void *data)
Hidetoshi Seto9af43b52009-06-15 17:21:36 +09002062{
Tejun Heo7b543a52010-12-18 16:30:05 +01002063 if (!mce_available(__this_cpu_ptr(&cpu_info)))
Hidetoshi Seto9af43b52009-06-15 17:21:36 +09002064 return;
Hidetoshi Seto9af43b52009-06-15 17:21:36 +09002065 cmci_clear();
2066}
2067
2068static void mce_enable_ce(void *all)
2069{
Tejun Heo7b543a52010-12-18 16:30:05 +01002070 if (!mce_available(__this_cpu_ptr(&cpu_info)))
Hidetoshi Seto9af43b52009-06-15 17:21:36 +09002071 return;
2072 cmci_reenable();
2073 cmci_recheck();
2074 if (all)
Borislav Petkov5e099542009-10-16 12:31:32 +02002075 __mcheck_cpu_init_timer();
Hidetoshi Seto9af43b52009-06-15 17:21:36 +09002076}
2077
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002078static struct bus_type mce_subsys = {
Ingo Molnare9eee032009-04-08 12:31:17 +02002079 .name = "machinecheck",
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002080 .dev_name = "machinecheck",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081};
2082
Greg Kroah-Hartmand6126ef2012-01-26 15:49:14 -08002083DEFINE_PER_CPU(struct device *, mce_device);
Ingo Molnare9eee032009-04-08 12:31:17 +02002084
2085__cpuinitdata
2086void (*threshold_cpu_callback)(unsigned long action, unsigned int cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002088static inline struct mce_bank *attr_to_bank(struct device_attribute *attr)
Andi Kleencebe1822009-07-09 00:31:43 +02002089{
2090 return container_of(attr, struct mce_bank, attr);
2091}
Andi Kleen0d7482e32009-02-17 23:07:13 +01002092
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002093static ssize_t show_bank(struct device *s, struct device_attribute *attr,
Andi Kleen0d7482e32009-02-17 23:07:13 +01002094 char *buf)
2095{
Andi Kleencebe1822009-07-09 00:31:43 +02002096 return sprintf(buf, "%llx\n", attr_to_bank(attr)->ctl);
Andi Kleen0d7482e32009-02-17 23:07:13 +01002097}
2098
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002099static ssize_t set_bank(struct device *s, struct device_attribute *attr,
Hidetoshi Seto9319cec2009-04-14 17:26:30 +09002100 const char *buf, size_t size)
Andi Kleen0d7482e32009-02-17 23:07:13 +01002101{
Hidetoshi Seto9319cec2009-04-14 17:26:30 +09002102 u64 new;
Ingo Molnare9eee032009-04-08 12:31:17 +02002103
Hidetoshi Seto9319cec2009-04-14 17:26:30 +09002104 if (strict_strtoull(buf, 0, &new) < 0)
Andi Kleen0d7482e32009-02-17 23:07:13 +01002105 return -EINVAL;
Ingo Molnare9eee032009-04-08 12:31:17 +02002106
Andi Kleencebe1822009-07-09 00:31:43 +02002107 attr_to_bank(attr)->ctl = new;
Andi Kleen0d7482e32009-02-17 23:07:13 +01002108 mce_restart();
Ingo Molnare9eee032009-04-08 12:31:17 +02002109
Hidetoshi Seto9319cec2009-04-14 17:26:30 +09002110 return size;
Andi Kleen0d7482e32009-02-17 23:07:13 +01002111}
Andi Kleena98f0dd2007-02-13 13:26:23 +01002112
Ingo Molnare9eee032009-04-08 12:31:17 +02002113static ssize_t
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002114show_trigger(struct device *s, struct device_attribute *attr, char *buf)
Andi Kleena98f0dd2007-02-13 13:26:23 +01002115{
Hidetoshi Seto1020bcb2009-06-15 17:20:57 +09002116 strcpy(buf, mce_helper);
Andi Kleena98f0dd2007-02-13 13:26:23 +01002117 strcat(buf, "\n");
Hidetoshi Seto1020bcb2009-06-15 17:20:57 +09002118 return strlen(mce_helper) + 1;
Andi Kleena98f0dd2007-02-13 13:26:23 +01002119}
2120
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002121static ssize_t set_trigger(struct device *s, struct device_attribute *attr,
Ingo Molnare9eee032009-04-08 12:31:17 +02002122 const char *buf, size_t siz)
Andi Kleena98f0dd2007-02-13 13:26:23 +01002123{
2124 char *p;
Ingo Molnare9eee032009-04-08 12:31:17 +02002125
Hidetoshi Seto1020bcb2009-06-15 17:20:57 +09002126 strncpy(mce_helper, buf, sizeof(mce_helper));
2127 mce_helper[sizeof(mce_helper)-1] = 0;
Hidetoshi Seto1020bcb2009-06-15 17:20:57 +09002128 p = strchr(mce_helper, '\n');
Ingo Molnare9eee032009-04-08 12:31:17 +02002129
Jan Beuliche9084ec2009-07-16 09:45:11 +01002130 if (p)
Ingo Molnare9eee032009-04-08 12:31:17 +02002131 *p = 0;
2132
Jan Beuliche9084ec2009-07-16 09:45:11 +01002133 return strlen(mce_helper) + !!p;
Andi Kleena98f0dd2007-02-13 13:26:23 +01002134}
2135
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002136static ssize_t set_ignore_ce(struct device *s,
2137 struct device_attribute *attr,
Hidetoshi Seto9af43b52009-06-15 17:21:36 +09002138 const char *buf, size_t size)
2139{
2140 u64 new;
2141
2142 if (strict_strtoull(buf, 0, &new) < 0)
2143 return -EINVAL;
2144
2145 if (mce_ignore_ce ^ !!new) {
2146 if (new) {
2147 /* disable ce features */
Hidetoshi Seto9aaef962011-06-17 04:40:36 -04002148 mce_timer_delete_all();
2149 on_each_cpu(mce_disable_cmci, NULL, 1);
Hidetoshi Seto9af43b52009-06-15 17:21:36 +09002150 mce_ignore_ce = 1;
2151 } else {
2152 /* enable ce features */
2153 mce_ignore_ce = 0;
2154 on_each_cpu(mce_enable_ce, (void *)1, 1);
2155 }
2156 }
2157 return size;
2158}
2159
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002160static ssize_t set_cmci_disabled(struct device *s,
2161 struct device_attribute *attr,
Hidetoshi Seto9af43b52009-06-15 17:21:36 +09002162 const char *buf, size_t size)
2163{
2164 u64 new;
2165
2166 if (strict_strtoull(buf, 0, &new) < 0)
2167 return -EINVAL;
2168
2169 if (mce_cmci_disabled ^ !!new) {
2170 if (new) {
2171 /* disable cmci */
Hidetoshi Seto9aaef962011-06-17 04:40:36 -04002172 on_each_cpu(mce_disable_cmci, NULL, 1);
Hidetoshi Seto9af43b52009-06-15 17:21:36 +09002173 mce_cmci_disabled = 1;
2174 } else {
2175 /* enable cmci */
2176 mce_cmci_disabled = 0;
2177 on_each_cpu(mce_enable_ce, NULL, 1);
2178 }
2179 }
2180 return size;
2181}
2182
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002183static ssize_t store_int_with_restart(struct device *s,
2184 struct device_attribute *attr,
Andi Kleenb56f6422009-05-27 21:56:52 +02002185 const char *buf, size_t size)
2186{
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002187 ssize_t ret = device_store_int(s, attr, buf, size);
Andi Kleenb56f6422009-05-27 21:56:52 +02002188 mce_restart();
2189 return ret;
2190}
2191
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002192static DEVICE_ATTR(trigger, 0644, show_trigger, set_trigger);
2193static DEVICE_INT_ATTR(tolerant, 0644, tolerant);
2194static DEVICE_INT_ATTR(monarch_timeout, 0644, monarch_timeout);
2195static DEVICE_INT_ATTR(dont_log_ce, 0644, mce_dont_log_ce);
Ingo Molnare9eee032009-04-08 12:31:17 +02002196
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002197static struct dev_ext_attribute dev_attr_check_interval = {
2198 __ATTR(check_interval, 0644, device_show_int, store_int_with_restart),
Andi Kleenb56f6422009-05-27 21:56:52 +02002199 &check_interval
2200};
Ingo Molnare9eee032009-04-08 12:31:17 +02002201
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002202static struct dev_ext_attribute dev_attr_ignore_ce = {
2203 __ATTR(ignore_ce, 0644, device_show_int, set_ignore_ce),
Hidetoshi Seto9af43b52009-06-15 17:21:36 +09002204 &mce_ignore_ce
2205};
2206
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002207static struct dev_ext_attribute dev_attr_cmci_disabled = {
2208 __ATTR(cmci_disabled, 0644, device_show_int, set_cmci_disabled),
Hidetoshi Seto9af43b52009-06-15 17:21:36 +09002209 &mce_cmci_disabled
2210};
2211
Naveen N. Rao450cc202012-09-27 10:08:00 -07002212static struct dev_ext_attribute dev_attr_bios_cmci_threshold = {
2213 __ATTR(bios_cmci_threshold, 0444, device_show_int, NULL),
2214 &mce_bios_cmci_threshold
2215};
2216
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002217static struct device_attribute *mce_device_attrs[] = {
2218 &dev_attr_tolerant.attr,
2219 &dev_attr_check_interval.attr,
2220 &dev_attr_trigger,
2221 &dev_attr_monarch_timeout.attr,
2222 &dev_attr_dont_log_ce.attr,
2223 &dev_attr_ignore_ce.attr,
2224 &dev_attr_cmci_disabled.attr,
Naveen N. Rao450cc202012-09-27 10:08:00 -07002225 &dev_attr_bios_cmci_threshold.attr,
Andi Kleena98f0dd2007-02-13 13:26:23 +01002226 NULL
2227};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002229static cpumask_var_t mce_device_initialized;
Andreas Herrmannbae19fe2007-11-14 17:00:44 -08002230
Greg Kroah-Hartmane032d8072012-01-16 14:40:28 -08002231static void mce_device_release(struct device *dev)
2232{
2233 kfree(dev);
2234}
2235
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002236/* Per cpu device init. All of the cpus still share the same ctrl bank: */
2237static __cpuinit int mce_device_create(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238{
Greg Kroah-Hartmane032d8072012-01-16 14:40:28 -08002239 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 int err;
Hidetoshi Setob1f49f92009-06-18 14:53:24 +09002241 int i, j;
Mike Travis92cb7612007-10-19 20:35:04 +02002242
Andreas Herrmann90367552007-11-07 02:12:58 +01002243 if (!mce_available(&boot_cpu_data))
Andi Kleen91c6d402005-07-28 21:15:39 -07002244 return -EIO;
2245
Greg Kroah-Hartmane032d8072012-01-16 14:40:28 -08002246 dev = kzalloc(sizeof *dev, GFP_KERNEL);
2247 if (!dev)
2248 return -ENOMEM;
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002249 dev->id = cpu;
2250 dev->bus = &mce_subsys;
Greg Kroah-Hartmane032d8072012-01-16 14:40:28 -08002251 dev->release = &mce_device_release;
Andi Kleen91c6d402005-07-28 21:15:39 -07002252
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002253 err = device_register(dev);
Akinobu Mitad435d862007-10-18 03:05:15 -07002254 if (err)
2255 return err;
Andi Kleen91c6d402005-07-28 21:15:39 -07002256
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002257 for (i = 0; mce_device_attrs[i]; i++) {
2258 err = device_create_file(dev, mce_device_attrs[i]);
Akinobu Mitad435d862007-10-18 03:05:15 -07002259 if (err)
2260 goto error;
Andi Kleen91c6d402005-07-28 21:15:39 -07002261 }
Hidetoshi Setob1f49f92009-06-18 14:53:24 +09002262 for (j = 0; j < banks; j++) {
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002263 err = device_create_file(dev, &mce_banks[j].attr);
Andi Kleen0d7482e32009-02-17 23:07:13 +01002264 if (err)
2265 goto error2;
2266 }
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002267 cpumask_set_cpu(cpu, mce_device_initialized);
Greg Kroah-Hartmand6126ef2012-01-26 15:49:14 -08002268 per_cpu(mce_device, cpu) = dev;
Akinobu Mitad435d862007-10-18 03:05:15 -07002269
2270 return 0;
Andi Kleen0d7482e32009-02-17 23:07:13 +01002271error2:
Hidetoshi Setob1f49f92009-06-18 14:53:24 +09002272 while (--j >= 0)
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002273 device_remove_file(dev, &mce_banks[j].attr);
Akinobu Mitad435d862007-10-18 03:05:15 -07002274error:
Ingo Molnarcb491fc2009-04-08 12:31:17 +02002275 while (--i >= 0)
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002276 device_remove_file(dev, mce_device_attrs[i]);
Ingo Molnarcb491fc2009-04-08 12:31:17 +02002277
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002278 device_unregister(dev);
Akinobu Mitad435d862007-10-18 03:05:15 -07002279
Andi Kleen91c6d402005-07-28 21:15:39 -07002280 return err;
2281}
2282
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002283static __cpuinit void mce_device_remove(unsigned int cpu)
Andi Kleen91c6d402005-07-28 21:15:39 -07002284{
Greg Kroah-Hartmand6126ef2012-01-26 15:49:14 -08002285 struct device *dev = per_cpu(mce_device, cpu);
Shaohua Li73ca5352006-01-11 22:43:06 +01002286 int i;
2287
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002288 if (!cpumask_test_cpu(cpu, mce_device_initialized))
Andreas Herrmannbae19fe2007-11-14 17:00:44 -08002289 return;
2290
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002291 for (i = 0; mce_device_attrs[i]; i++)
2292 device_remove_file(dev, mce_device_attrs[i]);
Ingo Molnarcb491fc2009-04-08 12:31:17 +02002293
Andi Kleen0d7482e32009-02-17 23:07:13 +01002294 for (i = 0; i < banks; i++)
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002295 device_remove_file(dev, &mce_banks[i].attr);
Ingo Molnarcb491fc2009-04-08 12:31:17 +02002296
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002297 device_unregister(dev);
2298 cpumask_clear_cpu(cpu, mce_device_initialized);
Greg Kroah-Hartmand6126ef2012-01-26 15:49:14 -08002299 per_cpu(mce_device, cpu) = NULL;
Andi Kleen91c6d402005-07-28 21:15:39 -07002300}
Andi Kleen91c6d402005-07-28 21:15:39 -07002301
Andi Kleend6b75582009-02-12 13:39:31 +01002302/* Make sure there are no machine checks on offlined CPUs. */
Hidetoshi Seto767df1b2009-11-26 17:29:02 +09002303static void __cpuinit mce_disable_cpu(void *h)
Andi Kleend6b75582009-02-12 13:39:31 +01002304{
Andi Kleen88ccbed2009-02-12 13:49:36 +01002305 unsigned long action = *(unsigned long *)h;
Ingo Molnarcb491fc2009-04-08 12:31:17 +02002306 int i;
Andi Kleend6b75582009-02-12 13:39:31 +01002307
Tejun Heo7b543a52010-12-18 16:30:05 +01002308 if (!mce_available(__this_cpu_ptr(&cpu_info)))
Andi Kleend6b75582009-02-12 13:39:31 +01002309 return;
Hidetoshi Seto767df1b2009-11-26 17:29:02 +09002310
Andi Kleen88ccbed2009-02-12 13:49:36 +01002311 if (!(action & CPU_TASKS_FROZEN))
2312 cmci_clear();
Andi Kleen06b7a7a2009-04-27 18:37:43 +02002313 for (i = 0; i < banks; i++) {
Andi Kleencebe1822009-07-09 00:31:43 +02002314 struct mce_bank *b = &mce_banks[i];
Ingo Molnar11868a22009-09-23 17:49:55 +02002315
Andi Kleencebe1822009-07-09 00:31:43 +02002316 if (b->init)
Andi Kleena2d32bc2009-07-09 00:31:44 +02002317 wrmsrl(MSR_IA32_MCx_CTL(i), 0);
Andi Kleen06b7a7a2009-04-27 18:37:43 +02002318 }
Andi Kleend6b75582009-02-12 13:39:31 +01002319}
2320
Hidetoshi Seto767df1b2009-11-26 17:29:02 +09002321static void __cpuinit mce_reenable_cpu(void *h)
Andi Kleend6b75582009-02-12 13:39:31 +01002322{
Andi Kleen88ccbed2009-02-12 13:49:36 +01002323 unsigned long action = *(unsigned long *)h;
Ingo Molnare9eee032009-04-08 12:31:17 +02002324 int i;
Andi Kleend6b75582009-02-12 13:39:31 +01002325
Tejun Heo7b543a52010-12-18 16:30:05 +01002326 if (!mce_available(__this_cpu_ptr(&cpu_info)))
Andi Kleend6b75582009-02-12 13:39:31 +01002327 return;
Ingo Molnare9eee032009-04-08 12:31:17 +02002328
Andi Kleen88ccbed2009-02-12 13:49:36 +01002329 if (!(action & CPU_TASKS_FROZEN))
2330 cmci_reenable();
Andi Kleen06b7a7a2009-04-27 18:37:43 +02002331 for (i = 0; i < banks; i++) {
Andi Kleencebe1822009-07-09 00:31:43 +02002332 struct mce_bank *b = &mce_banks[i];
Ingo Molnar11868a22009-09-23 17:49:55 +02002333
Andi Kleencebe1822009-07-09 00:31:43 +02002334 if (b->init)
Andi Kleena2d32bc2009-07-09 00:31:44 +02002335 wrmsrl(MSR_IA32_MCx_CTL(i), b->ctl);
Andi Kleen06b7a7a2009-04-27 18:37:43 +02002336 }
Andi Kleend6b75582009-02-12 13:39:31 +01002337}
2338
Andi Kleen91c6d402005-07-28 21:15:39 -07002339/* Get notified when a cpu comes on/off. Be hotplug friendly. */
Ingo Molnare9eee032009-04-08 12:31:17 +02002340static int __cpuinit
2341mce_cpu_callback(struct notifier_block *nfb, unsigned long action, void *hcpu)
Andi Kleen91c6d402005-07-28 21:15:39 -07002342{
2343 unsigned int cpu = (unsigned long)hcpu;
Andi Kleen52d168e2009-02-12 13:39:29 +01002344 struct timer_list *t = &per_cpu(mce_timer, cpu);
Andi Kleen91c6d402005-07-28 21:15:39 -07002345
Thomas Gleixner1a65f972012-07-19 13:59:40 -04002346 switch (action & ~CPU_TASKS_FROZEN) {
Andreas Herrmannbae19fe2007-11-14 17:00:44 -08002347 case CPU_ONLINE:
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002348 mce_device_create(cpu);
Rafael J. Wysocki87357282008-08-22 22:23:09 +02002349 if (threshold_cpu_callback)
2350 threshold_cpu_callback(action, cpu);
Andi Kleen91c6d402005-07-28 21:15:39 -07002351 break;
Andi Kleen91c6d402005-07-28 21:15:39 -07002352 case CPU_DEAD:
Rafael J. Wysocki87357282008-08-22 22:23:09 +02002353 if (threshold_cpu_callback)
2354 threshold_cpu_callback(action, cpu);
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002355 mce_device_remove(cpu);
Chen Gong55babd82012-08-09 11:44:51 -07002356 mce_intel_hcpu_update(cpu);
Andi Kleen91c6d402005-07-28 21:15:39 -07002357 break;
Andi Kleen52d168e2009-02-12 13:39:29 +01002358 case CPU_DOWN_PREPARE:
Andi Kleen88ccbed2009-02-12 13:49:36 +01002359 smp_call_function_single(cpu, mce_disable_cpu, &action, 1);
Chen Gong55babd82012-08-09 11:44:51 -07002360 del_timer_sync(t);
Andi Kleen52d168e2009-02-12 13:39:29 +01002361 break;
2362 case CPU_DOWN_FAILED:
Andi Kleen88ccbed2009-02-12 13:49:36 +01002363 smp_call_function_single(cpu, mce_reenable_cpu, &action, 1);
Thomas Gleixner26c3c282012-07-19 13:59:39 -04002364 mce_start_timer(cpu, t);
Andi Kleen88ccbed2009-02-12 13:49:36 +01002365 break;
Thomas Gleixner1a65f972012-07-19 13:59:40 -04002366 }
2367
2368 if (action == CPU_POST_DEAD) {
Andi Kleen88ccbed2009-02-12 13:49:36 +01002369 /* intentionally ignoring frozen here */
2370 cmci_rediscover(cpu);
Andi Kleen91c6d402005-07-28 21:15:39 -07002371 }
Thomas Gleixner1a65f972012-07-19 13:59:40 -04002372
Andreas Herrmannbae19fe2007-11-14 17:00:44 -08002373 return NOTIFY_OK;
Andi Kleen91c6d402005-07-28 21:15:39 -07002374}
2375
Sam Ravnborg1e356692008-01-30 13:33:36 +01002376static struct notifier_block mce_cpu_notifier __cpuinitdata = {
Andi Kleen91c6d402005-07-28 21:15:39 -07002377 .notifier_call = mce_cpu_callback,
2378};
2379
Andi Kleencebe1822009-07-09 00:31:43 +02002380static __init void mce_init_banks(void)
Andi Kleen0d7482e32009-02-17 23:07:13 +01002381{
2382 int i;
2383
Andi Kleen0d7482e32009-02-17 23:07:13 +01002384 for (i = 0; i < banks; i++) {
Andi Kleencebe1822009-07-09 00:31:43 +02002385 struct mce_bank *b = &mce_banks[i];
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002386 struct device_attribute *a = &b->attr;
Ingo Molnare9eee032009-04-08 12:31:17 +02002387
Eric W. Biedermana07e4152010-02-11 15:23:05 -08002388 sysfs_attr_init(&a->attr);
Andi Kleencebe1822009-07-09 00:31:43 +02002389 a->attr.name = b->attrname;
2390 snprintf(b->attrname, ATTR_LEN, "bank%d", i);
Ingo Molnare9eee032009-04-08 12:31:17 +02002391
2392 a->attr.mode = 0644;
2393 a->show = show_bank;
2394 a->store = set_bank;
Andi Kleen0d7482e32009-02-17 23:07:13 +01002395 }
Andi Kleen0d7482e32009-02-17 23:07:13 +01002396}
2397
Borislav Petkov5e099542009-10-16 12:31:32 +02002398static __init int mcheck_init_device(void)
Andi Kleen91c6d402005-07-28 21:15:39 -07002399{
2400 int err;
2401 int i = 0;
2402
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 if (!mce_available(&boot_cpu_data))
2404 return -EIO;
Andi Kleen0d7482e32009-02-17 23:07:13 +01002405
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002406 zalloc_cpumask_var(&mce_device_initialized, GFP_KERNEL);
Rusty Russell996867d2009-03-13 14:49:51 +10302407
Andi Kleencebe1822009-07-09 00:31:43 +02002408 mce_init_banks();
Andi Kleen0d7482e32009-02-17 23:07:13 +01002409
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002410 err = subsys_system_register(&mce_subsys, NULL);
Akinobu Mitad435d862007-10-18 03:05:15 -07002411 if (err)
2412 return err;
Andi Kleen91c6d402005-07-28 21:15:39 -07002413
2414 for_each_online_cpu(i) {
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002415 err = mce_device_create(i);
Akinobu Mitad435d862007-10-18 03:05:15 -07002416 if (err)
2417 return err;
Andi Kleen91c6d402005-07-28 21:15:39 -07002418 }
2419
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +01002420 register_syscore_ops(&mce_syscore_ops);
Chandra Seetharamanbe6b5a32006-07-30 03:03:37 -07002421 register_hotcpu_notifier(&mce_cpu_notifier);
Hidetoshi Seto93b62c32011-06-08 11:00:45 +09002422
2423 /* register character device /dev/mcelog */
2424 misc_register(&mce_chrdev_device);
Ingo Molnare9eee032009-04-08 12:31:17 +02002425
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427}
Liu, Jinsongcef12ee2012-06-07 19:56:51 +08002428device_initcall_sync(mcheck_init_device);
Ingo Molnara988d332009-04-08 12:31:25 +02002429
Andi Kleend7c3c9a2009-04-28 23:07:25 +02002430/*
2431 * Old style boot options parsing. Only for compatibility.
2432 */
2433static int __init mcheck_disable(char *str)
2434{
2435 mce_disabled = 1;
2436 return 1;
2437}
2438__setup("nomce", mcheck_disable);
Huang Ying5be9ed22009-07-31 09:41:42 +08002439
2440#ifdef CONFIG_DEBUG_FS
2441struct dentry *mce_get_debugfs_dir(void)
2442{
2443 static struct dentry *dmce;
2444
2445 if (!dmce)
2446 dmce = debugfs_create_dir("mce", NULL);
2447
2448 return dmce;
2449}
Huang Yingbf783f92009-07-31 09:41:43 +08002450
2451static void mce_reset(void)
2452{
2453 cpu_missing = 0;
2454 atomic_set(&mce_fake_paniced, 0);
2455 atomic_set(&mce_executing, 0);
2456 atomic_set(&mce_callin, 0);
2457 atomic_set(&global_nwo, 0);
2458}
2459
2460static int fake_panic_get(void *data, u64 *val)
2461{
2462 *val = fake_panic;
2463 return 0;
2464}
2465
2466static int fake_panic_set(void *data, u64 val)
2467{
2468 mce_reset();
2469 fake_panic = val;
2470 return 0;
2471}
2472
2473DEFINE_SIMPLE_ATTRIBUTE(fake_panic_fops, fake_panic_get,
2474 fake_panic_set, "%llu\n");
2475
Borislav Petkov5e099542009-10-16 12:31:32 +02002476static int __init mcheck_debugfs_init(void)
Huang Yingbf783f92009-07-31 09:41:43 +08002477{
2478 struct dentry *dmce, *ffake_panic;
2479
2480 dmce = mce_get_debugfs_dir();
2481 if (!dmce)
2482 return -ENOMEM;
2483 ffake_panic = debugfs_create_file("fake_panic", 0444, dmce, NULL,
2484 &fake_panic_fops);
2485 if (!ffake_panic)
2486 return -ENOMEM;
2487
2488 return 0;
2489}
Borislav Petkov5e099542009-10-16 12:31:32 +02002490late_initcall(mcheck_debugfs_init);
Huang Ying5be9ed22009-07-31 09:41:42 +08002491#endif