x86, mce: Use HW_ERR in MCE handler
[linux-flexiantxendom0-3.2.10.git] / arch / x86 / kernel / cpu / mcheck / mce.c
1 /*
2  * Machine check handler.
3  *
4  * K8 parts Copyright 2002,2003 Andi Kleen, SuSE Labs.
5  * Rest from unknown author(s).
6  * 2004 Andi Kleen. Rewrote most of it.
7  * Copyright 2008 Intel Corporation
8  * Author: Andi Kleen
9  */
10 #include <linux/thread_info.h>
11 #include <linux/capability.h>
12 #include <linux/miscdevice.h>
13 #include <linux/interrupt.h>
14 #include <linux/ratelimit.h>
15 #include <linux/kallsyms.h>
16 #include <linux/rcupdate.h>
17 #include <linux/kobject.h>
18 #include <linux/uaccess.h>
19 #include <linux/kdebug.h>
20 #include <linux/kernel.h>
21 #include <linux/percpu.h>
22 #include <linux/string.h>
23 #include <linux/sysdev.h>
24 #include <linux/delay.h>
25 #include <linux/ctype.h>
26 #include <linux/sched.h>
27 #include <linux/sysfs.h>
28 #include <linux/types.h>
29 #include <linux/slab.h>
30 #include <linux/init.h>
31 #include <linux/kmod.h>
32 #include <linux/poll.h>
33 #include <linux/nmi.h>
34 #include <linux/cpu.h>
35 #include <linux/smp.h>
36 #include <linux/fs.h>
37 #include <linux/mm.h>
38 #include <linux/debugfs.h>
39 #include <linux/edac_mce.h>
40
41 #include <asm/processor.h>
42 #include <asm/hw_irq.h>
43 #include <asm/apic.h>
44 #include <asm/idle.h>
45 #include <asm/ipi.h>
46 #include <asm/mce.h>
47 #include <asm/msr.h>
48
49 #include "mce-internal.h"
50
51 static DEFINE_MUTEX(mce_read_mutex);
52
53 #define rcu_dereference_check_mce(p) \
54         rcu_dereference_check((p), \
55                               rcu_read_lock_sched_held() || \
56                               lockdep_is_held(&mce_read_mutex))
57
58 #define CREATE_TRACE_POINTS
59 #include <trace/events/mce.h>
60
61 int mce_disabled __read_mostly;
62
63 #define MISC_MCELOG_MINOR       227
64
65 #define SPINUNIT 100    /* 100ns */
66
67 atomic_t mce_entry;
68
69 DEFINE_PER_CPU(unsigned, mce_exception_count);
70
71 /*
72  * Tolerant levels:
73  *   0: always panic on uncorrected errors, log corrected errors
74  *   1: panic or SIGBUS on uncorrected errors, log corrected errors
75  *   2: SIGBUS or log uncorrected errors (if possible), log corrected errors
76  *   3: never panic or SIGBUS, log all errors (for testing only)
77  */
78 static int                      tolerant                __read_mostly = 1;
79 static int                      banks                   __read_mostly;
80 static int                      rip_msr                 __read_mostly;
81 static int                      mce_bootlog             __read_mostly = -1;
82 static int                      monarch_timeout         __read_mostly = -1;
83 static int                      mce_panic_timeout       __read_mostly;
84 static int                      mce_dont_log_ce         __read_mostly;
85 int                             mce_cmci_disabled       __read_mostly;
86 int                             mce_ignore_ce           __read_mostly;
87 int                             mce_ser                 __read_mostly;
88
89 struct mce_bank                *mce_banks               __read_mostly;
90
91 /* User mode helper program triggered by machine check event */
92 static unsigned long            mce_need_notify;
93 static char                     mce_helper[128];
94 static char                     *mce_helper_argv[2] = { mce_helper, NULL };
95
96 static DECLARE_WAIT_QUEUE_HEAD(mce_wait);
97 static DEFINE_PER_CPU(struct mce, mces_seen);
98 static int                      cpu_missing;
99
100 /*
101  * CPU/chipset specific EDAC code can register a notifier call here to print
102  * MCE errors in a human-readable form.
103  */
104 ATOMIC_NOTIFIER_HEAD(x86_mce_decoder_chain);
105 EXPORT_SYMBOL_GPL(x86_mce_decoder_chain);
106
107 static int default_decode_mce(struct notifier_block *nb, unsigned long val,
108                                void *data)
109 {
110         pr_emerg(HW_ERR "No human readable MCE decoding support on this CPU type.\n");
111         pr_emerg(HW_ERR "Run the message through 'mcelog --ascii' to decode.\n");
112
113         return NOTIFY_STOP;
114 }
115
116 static struct notifier_block mce_dec_nb = {
117         .notifier_call = default_decode_mce,
118         .priority      = -1,
119 };
120
121 /* MCA banks polled by the period polling timer for corrected events */
122 DEFINE_PER_CPU(mce_banks_t, mce_poll_banks) = {
123         [0 ... BITS_TO_LONGS(MAX_NR_BANKS)-1] = ~0UL
124 };
125
126 static DEFINE_PER_CPU(struct work_struct, mce_work);
127
128 /* Do initial initialization of a struct mce */
129 void mce_setup(struct mce *m)
130 {
131         memset(m, 0, sizeof(struct mce));
132         m->cpu = m->extcpu = smp_processor_id();
133         rdtscll(m->tsc);
134         /* We hope get_seconds stays lockless */
135         m->time = get_seconds();
136         m->cpuvendor = boot_cpu_data.x86_vendor;
137         m->cpuid = cpuid_eax(1);
138 #ifdef CONFIG_SMP
139         m->socketid = cpu_data(m->extcpu).phys_proc_id;
140 #endif
141         m->apicid = cpu_data(m->extcpu).initial_apicid;
142         rdmsrl(MSR_IA32_MCG_CAP, m->mcgcap);
143 }
144
145 DEFINE_PER_CPU(struct mce, injectm);
146 EXPORT_PER_CPU_SYMBOL_GPL(injectm);
147
148 /*
149  * Lockless MCE logging infrastructure.
150  * This avoids deadlocks on printk locks without having to break locks. Also
151  * separate MCEs from kernel messages to avoid bogus bug reports.
152  */
153
154 static struct mce_log mcelog = {
155         .signature      = MCE_LOG_SIGNATURE,
156         .len            = MCE_LOG_LEN,
157         .recordlen      = sizeof(struct mce),
158 };
159
160 void mce_log(struct mce *mce)
161 {
162         unsigned next, entry;
163
164         /* Emit the trace record: */
165         trace_mce_record(mce);
166
167         mce->finished = 0;
168         wmb();
169         for (;;) {
170                 entry = rcu_dereference_check_mce(mcelog.next);
171                 for (;;) {
172                         /*
173                          * If edac_mce is enabled, it will check the error type
174                          * and will process it, if it is a known error.
175                          * Otherwise, the error will be sent through mcelog
176                          * interface
177                          */
178                         if (edac_mce_parse(mce))
179                                 return;
180
181                         /*
182                          * When the buffer fills up discard new entries.
183                          * Assume that the earlier errors are the more
184                          * interesting ones:
185                          */
186                         if (entry >= MCE_LOG_LEN) {
187                                 set_bit(MCE_OVERFLOW,
188                                         (unsigned long *)&mcelog.flags);
189                                 return;
190                         }
191                         /* Old left over entry. Skip: */
192                         if (mcelog.entry[entry].finished) {
193                                 entry++;
194                                 continue;
195                         }
196                         break;
197                 }
198                 smp_rmb();
199                 next = entry + 1;
200                 if (cmpxchg(&mcelog.next, entry, next) == entry)
201                         break;
202         }
203         memcpy(mcelog.entry + entry, mce, sizeof(struct mce));
204         wmb();
205         mcelog.entry[entry].finished = 1;
206         wmb();
207
208         mce->finished = 1;
209         set_bit(0, &mce_need_notify);
210 }
211
212 static void print_mce(struct mce *m)
213 {
214         pr_emerg(HW_ERR "CPU %d: Machine Check Exception: %Lx Bank %d: %016Lx\n",
215                m->extcpu, m->mcgstatus, m->bank, m->status);
216
217         if (m->ip) {
218                 pr_emerg(HW_ERR "RIP%s %02x:<%016Lx> ",
219                         !(m->mcgstatus & MCG_STATUS_EIPV) ? " !INEXACT!" : "",
220                                 m->cs, m->ip);
221
222                 if (m->cs == __KERNEL_CS)
223                         print_symbol("{%s}", m->ip);
224                 pr_cont("\n");
225         }
226
227         pr_emerg(HW_ERR "TSC %llx ", m->tsc);
228         if (m->addr)
229                 pr_cont("ADDR %llx ", m->addr);
230         if (m->misc)
231                 pr_cont("MISC %llx ", m->misc);
232
233         pr_cont("\n");
234         pr_emerg(HW_ERR "PROCESSOR %u:%x TIME %llu SOCKET %u APIC %x\n",
235                 m->cpuvendor, m->cpuid, m->time, m->socketid, m->apicid);
236
237         /*
238          * Print out human-readable details about the MCE error,
239          * (if the CPU has an implementation for that)
240          */
241         atomic_notifier_call_chain(&x86_mce_decoder_chain, 0, m);
242 }
243
244 #define PANIC_TIMEOUT 5 /* 5 seconds */
245
246 static atomic_t mce_paniced;
247
248 static int fake_panic;
249 static atomic_t mce_fake_paniced;
250
251 /* Panic in progress. Enable interrupts and wait for final IPI */
252 static void wait_for_panic(void)
253 {
254         long timeout = PANIC_TIMEOUT*USEC_PER_SEC;
255
256         preempt_disable();
257         local_irq_enable();
258         while (timeout-- > 0)
259                 udelay(1);
260         if (panic_timeout == 0)
261                 panic_timeout = mce_panic_timeout;
262         panic("Panicing machine check CPU died");
263 }
264
265 static void mce_panic(char *msg, struct mce *final, char *exp)
266 {
267         int i, apei_err = 0;
268
269         if (!fake_panic) {
270                 /*
271                  * Make sure only one CPU runs in machine check panic
272                  */
273                 if (atomic_inc_return(&mce_paniced) > 1)
274                         wait_for_panic();
275                 barrier();
276
277                 bust_spinlocks(1);
278                 console_verbose();
279         } else {
280                 /* Don't log too much for fake panic */
281                 if (atomic_inc_return(&mce_fake_paniced) > 1)
282                         return;
283         }
284         /* First print corrected ones that are still unlogged */
285         for (i = 0; i < MCE_LOG_LEN; i++) {
286                 struct mce *m = &mcelog.entry[i];
287                 if (!(m->status & MCI_STATUS_VAL))
288                         continue;
289                 if (!(m->status & MCI_STATUS_UC)) {
290                         print_mce(m);
291                         if (!apei_err)
292                                 apei_err = apei_write_mce(m);
293                 }
294         }
295         /* Now print uncorrected but with the final one last */
296         for (i = 0; i < MCE_LOG_LEN; i++) {
297                 struct mce *m = &mcelog.entry[i];
298                 if (!(m->status & MCI_STATUS_VAL))
299                         continue;
300                 if (!(m->status & MCI_STATUS_UC))
301                         continue;
302                 if (!final || memcmp(m, final, sizeof(struct mce))) {
303                         print_mce(m);
304                         if (!apei_err)
305                                 apei_err = apei_write_mce(m);
306                 }
307         }
308         if (final) {
309                 print_mce(final);
310                 if (!apei_err)
311                         apei_err = apei_write_mce(final);
312         }
313         if (cpu_missing)
314                 pr_emerg(HW_ERR "Some CPUs didn't answer in synchronization\n");
315         if (exp)
316                 pr_emerg(HW_ERR "Machine check: %s\n", exp);
317         if (!fake_panic) {
318                 if (panic_timeout == 0)
319                         panic_timeout = mce_panic_timeout;
320                 panic(msg);
321         } else
322                 pr_emerg(HW_ERR "Fake kernel panic: %s\n", msg);
323 }
324
325 /* Support code for software error injection */
326
327 static int msr_to_offset(u32 msr)
328 {
329         unsigned bank = __get_cpu_var(injectm.bank);
330
331         if (msr == rip_msr)
332                 return offsetof(struct mce, ip);
333         if (msr == MSR_IA32_MCx_STATUS(bank))
334                 return offsetof(struct mce, status);
335         if (msr == MSR_IA32_MCx_ADDR(bank))
336                 return offsetof(struct mce, addr);
337         if (msr == MSR_IA32_MCx_MISC(bank))
338                 return offsetof(struct mce, misc);
339         if (msr == MSR_IA32_MCG_STATUS)
340                 return offsetof(struct mce, mcgstatus);
341         return -1;
342 }
343
344 /* MSR access wrappers used for error injection */
345 static u64 mce_rdmsrl(u32 msr)
346 {
347         u64 v;
348
349         if (__get_cpu_var(injectm).finished) {
350                 int offset = msr_to_offset(msr);
351
352                 if (offset < 0)
353                         return 0;
354                 return *(u64 *)((char *)&__get_cpu_var(injectm) + offset);
355         }
356
357         if (rdmsrl_safe(msr, &v)) {
358                 WARN_ONCE(1, "mce: Unable to read msr %d!\n", msr);
359                 /*
360                  * Return zero in case the access faulted. This should
361                  * not happen normally but can happen if the CPU does
362                  * something weird, or if the code is buggy.
363                  */
364                 v = 0;
365         }
366
367         return v;
368 }
369
370 static void mce_wrmsrl(u32 msr, u64 v)
371 {
372         if (__get_cpu_var(injectm).finished) {
373                 int offset = msr_to_offset(msr);
374
375                 if (offset >= 0)
376                         *(u64 *)((char *)&__get_cpu_var(injectm) + offset) = v;
377                 return;
378         }
379         wrmsrl(msr, v);
380 }
381
382 /*
383  * Simple lockless ring to communicate PFNs from the exception handler with the
384  * process context work function. This is vastly simplified because there's
385  * only a single reader and a single writer.
386  */
387 #define MCE_RING_SIZE 16        /* we use one entry less */
388
389 struct mce_ring {
390         unsigned short start;
391         unsigned short end;
392         unsigned long ring[MCE_RING_SIZE];
393 };
394 static DEFINE_PER_CPU(struct mce_ring, mce_ring);
395
396 /* Runs with CPU affinity in workqueue */
397 static int mce_ring_empty(void)
398 {
399         struct mce_ring *r = &__get_cpu_var(mce_ring);
400
401         return r->start == r->end;
402 }
403
404 static int mce_ring_get(unsigned long *pfn)
405 {
406         struct mce_ring *r;
407         int ret = 0;
408
409         *pfn = 0;
410         get_cpu();
411         r = &__get_cpu_var(mce_ring);
412         if (r->start == r->end)
413                 goto out;
414         *pfn = r->ring[r->start];
415         r->start = (r->start + 1) % MCE_RING_SIZE;
416         ret = 1;
417 out:
418         put_cpu();
419         return ret;
420 }
421
422 /* Always runs in MCE context with preempt off */
423 static int mce_ring_add(unsigned long pfn)
424 {
425         struct mce_ring *r = &__get_cpu_var(mce_ring);
426         unsigned next;
427
428         next = (r->end + 1) % MCE_RING_SIZE;
429         if (next == r->start)
430                 return -1;
431         r->ring[r->end] = pfn;
432         wmb();
433         r->end = next;
434         return 0;
435 }
436
437 int mce_available(struct cpuinfo_x86 *c)
438 {
439         if (mce_disabled)
440                 return 0;
441         return cpu_has(c, X86_FEATURE_MCE) && cpu_has(c, X86_FEATURE_MCA);
442 }
443
444 static void mce_schedule_work(void)
445 {
446         if (!mce_ring_empty()) {
447                 struct work_struct *work = &__get_cpu_var(mce_work);
448                 if (!work_pending(work))
449                         schedule_work(work);
450         }
451 }
452
453 /*
454  * Get the address of the instruction at the time of the machine check
455  * error.
456  */
457 static inline void mce_get_rip(struct mce *m, struct pt_regs *regs)
458 {
459
460         if (regs && (m->mcgstatus & (MCG_STATUS_RIPV|MCG_STATUS_EIPV))) {
461                 m->ip = regs->ip;
462                 m->cs = regs->cs;
463         } else {
464                 m->ip = 0;
465                 m->cs = 0;
466         }
467         if (rip_msr)
468                 m->ip = mce_rdmsrl(rip_msr);
469 }
470
471 #ifdef CONFIG_X86_LOCAL_APIC
472 /*
473  * Called after interrupts have been reenabled again
474  * when a MCE happened during an interrupts off region
475  * in the kernel.
476  */
477 asmlinkage void smp_mce_self_interrupt(struct pt_regs *regs)
478 {
479         ack_APIC_irq();
480         exit_idle();
481         irq_enter();
482         mce_notify_irq();
483         mce_schedule_work();
484         irq_exit();
485 }
486 #endif
487
488 static void mce_report_event(struct pt_regs *regs)
489 {
490         if (regs->flags & (X86_VM_MASK|X86_EFLAGS_IF)) {
491                 mce_notify_irq();
492                 /*
493                  * Triggering the work queue here is just an insurance
494                  * policy in case the syscall exit notify handler
495                  * doesn't run soon enough or ends up running on the
496                  * wrong CPU (can happen when audit sleeps)
497                  */
498                 mce_schedule_work();
499                 return;
500         }
501
502 #ifdef CONFIG_X86_LOCAL_APIC
503         /*
504          * Without APIC do not notify. The event will be picked
505          * up eventually.
506          */
507         if (!cpu_has_apic)
508                 return;
509
510         /*
511          * When interrupts are disabled we cannot use
512          * kernel services safely. Trigger an self interrupt
513          * through the APIC to instead do the notification
514          * after interrupts are reenabled again.
515          */
516         apic->send_IPI_self(MCE_SELF_VECTOR);
517
518         /*
519          * Wait for idle afterwards again so that we don't leave the
520          * APIC in a non idle state because the normal APIC writes
521          * cannot exclude us.
522          */
523         apic_wait_icr_idle();
524 #endif
525 }
526
527 DEFINE_PER_CPU(unsigned, mce_poll_count);
528
529 /*
530  * Poll for corrected events or events that happened before reset.
531  * Those are just logged through /dev/mcelog.
532  *
533  * This is executed in standard interrupt context.
534  *
535  * Note: spec recommends to panic for fatal unsignalled
536  * errors here. However this would be quite problematic --
537  * we would need to reimplement the Monarch handling and
538  * it would mess up the exclusion between exception handler
539  * and poll hander -- * so we skip this for now.
540  * These cases should not happen anyways, or only when the CPU
541  * is already totally * confused. In this case it's likely it will
542  * not fully execute the machine check handler either.
543  */
544 void machine_check_poll(enum mcp_flags flags, mce_banks_t *b)
545 {
546         struct mce m;
547         int i;
548
549         percpu_inc(mce_poll_count);
550
551         mce_setup(&m);
552
553         m.mcgstatus = mce_rdmsrl(MSR_IA32_MCG_STATUS);
554         for (i = 0; i < banks; i++) {
555                 if (!mce_banks[i].ctl || !test_bit(i, *b))
556                         continue;
557
558                 m.misc = 0;
559                 m.addr = 0;
560                 m.bank = i;
561                 m.tsc = 0;
562
563                 barrier();
564                 m.status = mce_rdmsrl(MSR_IA32_MCx_STATUS(i));
565                 if (!(m.status & MCI_STATUS_VAL))
566                         continue;
567
568                 /*
569                  * Uncorrected or signalled events are handled by the exception
570                  * handler when it is enabled, so don't process those here.
571                  *
572                  * TBD do the same check for MCI_STATUS_EN here?
573                  */
574                 if (!(flags & MCP_UC) &&
575                     (m.status & (mce_ser ? MCI_STATUS_S : MCI_STATUS_UC)))
576                         continue;
577
578                 if (m.status & MCI_STATUS_MISCV)
579                         m.misc = mce_rdmsrl(MSR_IA32_MCx_MISC(i));
580                 if (m.status & MCI_STATUS_ADDRV)
581                         m.addr = mce_rdmsrl(MSR_IA32_MCx_ADDR(i));
582
583                 if (!(flags & MCP_TIMESTAMP))
584                         m.tsc = 0;
585                 /*
586                  * Don't get the IP here because it's unlikely to
587                  * have anything to do with the actual error location.
588                  */
589                 if (!(flags & MCP_DONTLOG) && !mce_dont_log_ce) {
590                         mce_log(&m);
591                         add_taint(TAINT_MACHINE_CHECK);
592                 }
593
594                 /*
595                  * Clear state for this bank.
596                  */
597                 mce_wrmsrl(MSR_IA32_MCx_STATUS(i), 0);
598         }
599
600         /*
601          * Don't clear MCG_STATUS here because it's only defined for
602          * exceptions.
603          */
604
605         sync_core();
606 }
607 EXPORT_SYMBOL_GPL(machine_check_poll);
608
609 /*
610  * Do a quick check if any of the events requires a panic.
611  * This decides if we keep the events around or clear them.
612  */
613 static int mce_no_way_out(struct mce *m, char **msg)
614 {
615         int i;
616
617         for (i = 0; i < banks; i++) {
618                 m->status = mce_rdmsrl(MSR_IA32_MCx_STATUS(i));
619                 if (mce_severity(m, tolerant, msg) >= MCE_PANIC_SEVERITY)
620                         return 1;
621         }
622         return 0;
623 }
624
625 /*
626  * Variable to establish order between CPUs while scanning.
627  * Each CPU spins initially until executing is equal its number.
628  */
629 static atomic_t mce_executing;
630
631 /*
632  * Defines order of CPUs on entry. First CPU becomes Monarch.
633  */
634 static atomic_t mce_callin;
635
636 /*
637  * Check if a timeout waiting for other CPUs happened.
638  */
639 static int mce_timed_out(u64 *t)
640 {
641         /*
642          * The others already did panic for some reason.
643          * Bail out like in a timeout.
644          * rmb() to tell the compiler that system_state
645          * might have been modified by someone else.
646          */
647         rmb();
648         if (atomic_read(&mce_paniced))
649                 wait_for_panic();
650         if (!monarch_timeout)
651                 goto out;
652         if ((s64)*t < SPINUNIT) {
653                 /* CHECKME: Make panic default for 1 too? */
654                 if (tolerant < 1)
655                         mce_panic("Timeout synchronizing machine check over CPUs",
656                                   NULL, NULL);
657                 cpu_missing = 1;
658                 return 1;
659         }
660         *t -= SPINUNIT;
661 out:
662         touch_nmi_watchdog();
663         return 0;
664 }
665
666 /*
667  * The Monarch's reign.  The Monarch is the CPU who entered
668  * the machine check handler first. It waits for the others to
669  * raise the exception too and then grades them. When any
670  * error is fatal panic. Only then let the others continue.
671  *
672  * The other CPUs entering the MCE handler will be controlled by the
673  * Monarch. They are called Subjects.
674  *
675  * This way we prevent any potential data corruption in a unrecoverable case
676  * and also makes sure always all CPU's errors are examined.
677  *
678  * Also this detects the case of a machine check event coming from outer
679  * space (not detected by any CPUs) In this case some external agent wants
680  * us to shut down, so panic too.
681  *
682  * The other CPUs might still decide to panic if the handler happens
683  * in a unrecoverable place, but in this case the system is in a semi-stable
684  * state and won't corrupt anything by itself. It's ok to let the others
685  * continue for a bit first.
686  *
687  * All the spin loops have timeouts; when a timeout happens a CPU
688  * typically elects itself to be Monarch.
689  */
690 static void mce_reign(void)
691 {
692         int cpu;
693         struct mce *m = NULL;
694         int global_worst = 0;
695         char *msg = NULL;
696         char *nmsg = NULL;
697
698         /*
699          * This CPU is the Monarch and the other CPUs have run
700          * through their handlers.
701          * Grade the severity of the errors of all the CPUs.
702          */
703         for_each_possible_cpu(cpu) {
704                 int severity = mce_severity(&per_cpu(mces_seen, cpu), tolerant,
705                                             &nmsg);
706                 if (severity > global_worst) {
707                         msg = nmsg;
708                         global_worst = severity;
709                         m = &per_cpu(mces_seen, cpu);
710                 }
711         }
712
713         /*
714          * Cannot recover? Panic here then.
715          * This dumps all the mces in the log buffer and stops the
716          * other CPUs.
717          */
718         if (m && global_worst >= MCE_PANIC_SEVERITY && tolerant < 3)
719                 mce_panic("Fatal Machine check", m, msg);
720
721         /*
722          * For UC somewhere we let the CPU who detects it handle it.
723          * Also must let continue the others, otherwise the handling
724          * CPU could deadlock on a lock.
725          */
726
727         /*
728          * No machine check event found. Must be some external
729          * source or one CPU is hung. Panic.
730          */
731         if (global_worst <= MCE_KEEP_SEVERITY && tolerant < 3)
732                 mce_panic("Machine check from unknown source", NULL, NULL);
733
734         /*
735          * Now clear all the mces_seen so that they don't reappear on
736          * the next mce.
737          */
738         for_each_possible_cpu(cpu)
739                 memset(&per_cpu(mces_seen, cpu), 0, sizeof(struct mce));
740 }
741
742 static atomic_t global_nwo;
743
744 /*
745  * Start of Monarch synchronization. This waits until all CPUs have
746  * entered the exception handler and then determines if any of them
747  * saw a fatal event that requires panic. Then it executes them
748  * in the entry order.
749  * TBD double check parallel CPU hotunplug
750  */
751 static int mce_start(int *no_way_out)
752 {
753         int order;
754         int cpus = num_online_cpus();
755         u64 timeout = (u64)monarch_timeout * NSEC_PER_USEC;
756
757         if (!timeout)
758                 return -1;
759
760         atomic_add(*no_way_out, &global_nwo);
761         /*
762          * global_nwo should be updated before mce_callin
763          */
764         smp_wmb();
765         order = atomic_inc_return(&mce_callin);
766
767         /*
768          * Wait for everyone.
769          */
770         while (atomic_read(&mce_callin) != cpus) {
771                 if (mce_timed_out(&timeout)) {
772                         atomic_set(&global_nwo, 0);
773                         return -1;
774                 }
775                 ndelay(SPINUNIT);
776         }
777
778         /*
779          * mce_callin should be read before global_nwo
780          */
781         smp_rmb();
782
783         if (order == 1) {
784                 /*
785                  * Monarch: Starts executing now, the others wait.
786                  */
787                 atomic_set(&mce_executing, 1);
788         } else {
789                 /*
790                  * Subject: Now start the scanning loop one by one in
791                  * the original callin order.
792                  * This way when there are any shared banks it will be
793                  * only seen by one CPU before cleared, avoiding duplicates.
794                  */
795                 while (atomic_read(&mce_executing) < order) {
796                         if (mce_timed_out(&timeout)) {
797                                 atomic_set(&global_nwo, 0);
798                                 return -1;
799                         }
800                         ndelay(SPINUNIT);
801                 }
802         }
803
804         /*
805          * Cache the global no_way_out state.
806          */
807         *no_way_out = atomic_read(&global_nwo);
808
809         return order;
810 }
811
812 /*
813  * Synchronize between CPUs after main scanning loop.
814  * This invokes the bulk of the Monarch processing.
815  */
816 static int mce_end(int order)
817 {
818         int ret = -1;
819         u64 timeout = (u64)monarch_timeout * NSEC_PER_USEC;
820
821         if (!timeout)
822                 goto reset;
823         if (order < 0)
824                 goto reset;
825
826         /*
827          * Allow others to run.
828          */
829         atomic_inc(&mce_executing);
830
831         if (order == 1) {
832                 /* CHECKME: Can this race with a parallel hotplug? */
833                 int cpus = num_online_cpus();
834
835                 /*
836                  * Monarch: Wait for everyone to go through their scanning
837                  * loops.
838                  */
839                 while (atomic_read(&mce_executing) <= cpus) {
840                         if (mce_timed_out(&timeout))
841                                 goto reset;
842                         ndelay(SPINUNIT);
843                 }
844
845                 mce_reign();
846                 barrier();
847                 ret = 0;
848         } else {
849                 /*
850                  * Subject: Wait for Monarch to finish.
851                  */
852                 while (atomic_read(&mce_executing) != 0) {
853                         if (mce_timed_out(&timeout))
854                                 goto reset;
855                         ndelay(SPINUNIT);
856                 }
857
858                 /*
859                  * Don't reset anything. That's done by the Monarch.
860                  */
861                 return 0;
862         }
863
864         /*
865          * Reset all global state.
866          */
867 reset:
868         atomic_set(&global_nwo, 0);
869         atomic_set(&mce_callin, 0);
870         barrier();
871
872         /*
873          * Let others run again.
874          */
875         atomic_set(&mce_executing, 0);
876         return ret;
877 }
878
879 /*
880  * Check if the address reported by the CPU is in a format we can parse.
881  * It would be possible to add code for most other cases, but all would
882  * be somewhat complicated (e.g. segment offset would require an instruction
883  * parser). So only support physical addresses upto page granuality for now.
884  */
885 static int mce_usable_address(struct mce *m)
886 {
887         if (!(m->status & MCI_STATUS_MISCV) || !(m->status & MCI_STATUS_ADDRV))
888                 return 0;
889         if ((m->misc & 0x3f) > PAGE_SHIFT)
890                 return 0;
891         if (((m->misc >> 6) & 7) != MCM_ADDR_PHYS)
892                 return 0;
893         return 1;
894 }
895
896 static void mce_clear_state(unsigned long *toclear)
897 {
898         int i;
899
900         for (i = 0; i < banks; i++) {
901                 if (test_bit(i, toclear))
902                         mce_wrmsrl(MSR_IA32_MCx_STATUS(i), 0);
903         }
904 }
905
906 /*
907  * The actual machine check handler. This only handles real
908  * exceptions when something got corrupted coming in through int 18.
909  *
910  * This is executed in NMI context not subject to normal locking rules. This
911  * implies that most kernel services cannot be safely used. Don't even
912  * think about putting a printk in there!
913  *
914  * On Intel systems this is entered on all CPUs in parallel through
915  * MCE broadcast. However some CPUs might be broken beyond repair,
916  * so be always careful when synchronizing with others.
917  */
918 void do_machine_check(struct pt_regs *regs, long error_code)
919 {
920         struct mce m, *final;
921         int i;
922         int worst = 0;
923         int severity;
924         /*
925          * Establish sequential order between the CPUs entering the machine
926          * check handler.
927          */
928         int order;
929         /*
930          * If no_way_out gets set, there is no safe way to recover from this
931          * MCE.  If tolerant is cranked up, we'll try anyway.
932          */
933         int no_way_out = 0;
934         /*
935          * If kill_it gets set, there might be a way to recover from this
936          * error.
937          */
938         int kill_it = 0;
939         DECLARE_BITMAP(toclear, MAX_NR_BANKS);
940         char *msg = "Unknown";
941
942         atomic_inc(&mce_entry);
943
944         percpu_inc(mce_exception_count);
945
946         if (notify_die(DIE_NMI, "machine check", regs, error_code,
947                            18, SIGKILL) == NOTIFY_STOP)
948                 goto out;
949         if (!banks)
950                 goto out;
951
952         mce_setup(&m);
953
954         m.mcgstatus = mce_rdmsrl(MSR_IA32_MCG_STATUS);
955         final = &__get_cpu_var(mces_seen);
956         *final = m;
957
958         no_way_out = mce_no_way_out(&m, &msg);
959
960         barrier();
961
962         /*
963          * When no restart IP must always kill or panic.
964          */
965         if (!(m.mcgstatus & MCG_STATUS_RIPV))
966                 kill_it = 1;
967
968         /*
969          * Go through all the banks in exclusion of the other CPUs.
970          * This way we don't report duplicated events on shared banks
971          * because the first one to see it will clear it.
972          */
973         order = mce_start(&no_way_out);
974         for (i = 0; i < banks; i++) {
975                 __clear_bit(i, toclear);
976                 if (!mce_banks[i].ctl)
977                         continue;
978
979                 m.misc = 0;
980                 m.addr = 0;
981                 m.bank = i;
982
983                 m.status = mce_rdmsrl(MSR_IA32_MCx_STATUS(i));
984                 if ((m.status & MCI_STATUS_VAL) == 0)
985                         continue;
986
987                 /*
988                  * Non uncorrected or non signaled errors are handled by
989                  * machine_check_poll. Leave them alone, unless this panics.
990                  */
991                 if (!(m.status & (mce_ser ? MCI_STATUS_S : MCI_STATUS_UC)) &&
992                         !no_way_out)
993                         continue;
994
995                 /*
996                  * Set taint even when machine check was not enabled.
997                  */
998                 add_taint(TAINT_MACHINE_CHECK);
999
1000                 severity = mce_severity(&m, tolerant, NULL);
1001
1002                 /*
1003                  * When machine check was for corrected handler don't touch,
1004                  * unless we're panicing.
1005                  */
1006                 if (severity == MCE_KEEP_SEVERITY && !no_way_out)
1007                         continue;
1008                 __set_bit(i, toclear);
1009                 if (severity == MCE_NO_SEVERITY) {
1010                         /*
1011                          * Machine check event was not enabled. Clear, but
1012                          * ignore.
1013                          */
1014                         continue;
1015                 }
1016
1017                 /*
1018                  * Kill on action required.
1019                  */
1020                 if (severity == MCE_AR_SEVERITY)
1021                         kill_it = 1;
1022
1023                 if (m.status & MCI_STATUS_MISCV)
1024                         m.misc = mce_rdmsrl(MSR_IA32_MCx_MISC(i));
1025                 if (m.status & MCI_STATUS_ADDRV)
1026                         m.addr = mce_rdmsrl(MSR_IA32_MCx_ADDR(i));
1027
1028                 /*
1029                  * Action optional error. Queue address for later processing.
1030                  * When the ring overflows we just ignore the AO error.
1031                  * RED-PEN add some logging mechanism when
1032                  * usable_address or mce_add_ring fails.
1033                  * RED-PEN don't ignore overflow for tolerant == 0
1034                  */
1035                 if (severity == MCE_AO_SEVERITY && mce_usable_address(&m))
1036                         mce_ring_add(m.addr >> PAGE_SHIFT);
1037
1038                 mce_get_rip(&m, regs);
1039                 mce_log(&m);
1040
1041                 if (severity > worst) {
1042                         *final = m;
1043                         worst = severity;
1044                 }
1045         }
1046
1047         if (!no_way_out)
1048                 mce_clear_state(toclear);
1049
1050         /*
1051          * Do most of the synchronization with other CPUs.
1052          * When there's any problem use only local no_way_out state.
1053          */
1054         if (mce_end(order) < 0)
1055                 no_way_out = worst >= MCE_PANIC_SEVERITY;
1056
1057         /*
1058          * If we have decided that we just CAN'T continue, and the user
1059          * has not set tolerant to an insane level, give up and die.
1060          *
1061          * This is mainly used in the case when the system doesn't
1062          * support MCE broadcasting or it has been disabled.
1063          */
1064         if (no_way_out && tolerant < 3)
1065                 mce_panic("Fatal machine check on current CPU", final, msg);
1066
1067         /*
1068          * If the error seems to be unrecoverable, something should be
1069          * done.  Try to kill as little as possible.  If we can kill just
1070          * one task, do that.  If the user has set the tolerance very
1071          * high, don't try to do anything at all.
1072          */
1073
1074         if (kill_it && tolerant < 3)
1075                 force_sig(SIGBUS, current);
1076
1077         /* notify userspace ASAP */
1078         set_thread_flag(TIF_MCE_NOTIFY);
1079
1080         if (worst > 0)
1081                 mce_report_event(regs);
1082         mce_wrmsrl(MSR_IA32_MCG_STATUS, 0);
1083 out:
1084         atomic_dec(&mce_entry);
1085         sync_core();
1086 }
1087 EXPORT_SYMBOL_GPL(do_machine_check);
1088
1089 /* dummy to break dependency. actual code is in mm/memory-failure.c */
1090 void __attribute__((weak)) memory_failure(unsigned long pfn, int vector)
1091 {
1092         printk(KERN_ERR "Action optional memory failure at %lx ignored\n", pfn);
1093 }
1094
1095 /*
1096  * Called after mce notification in process context. This code
1097  * is allowed to sleep. Call the high level VM handler to process
1098  * any corrupted pages.
1099  * Assume that the work queue code only calls this one at a time
1100  * per CPU.
1101  * Note we don't disable preemption, so this code might run on the wrong
1102  * CPU. In this case the event is picked up by the scheduled work queue.
1103  * This is merely a fast path to expedite processing in some common
1104  * cases.
1105  */
1106 void mce_notify_process(void)
1107 {
1108         unsigned long pfn;
1109         mce_notify_irq();
1110         while (mce_ring_get(&pfn))
1111                 memory_failure(pfn, MCE_VECTOR);
1112 }
1113
1114 static void mce_process_work(struct work_struct *dummy)
1115 {
1116         mce_notify_process();
1117 }
1118
1119 #ifdef CONFIG_X86_MCE_INTEL
1120 /***
1121  * mce_log_therm_throt_event - Logs the thermal throttling event to mcelog
1122  * @cpu: The CPU on which the event occurred.
1123  * @status: Event status information
1124  *
1125  * This function should be called by the thermal interrupt after the
1126  * event has been processed and the decision was made to log the event
1127  * further.
1128  *
1129  * The status parameter will be saved to the 'status' field of 'struct mce'
1130  * and historically has been the register value of the
1131  * MSR_IA32_THERMAL_STATUS (Intel) msr.
1132  */
1133 void mce_log_therm_throt_event(__u64 status)
1134 {
1135         struct mce m;
1136
1137         mce_setup(&m);
1138         m.bank = MCE_THERMAL_BANK;
1139         m.status = status;
1140         mce_log(&m);
1141 }
1142 #endif /* CONFIG_X86_MCE_INTEL */
1143
1144 /*
1145  * Periodic polling timer for "silent" machine check errors.  If the
1146  * poller finds an MCE, poll 2x faster.  When the poller finds no more
1147  * errors, poll 2x slower (up to check_interval seconds).
1148  */
1149 static int check_interval = 5 * 60; /* 5 minutes */
1150
1151 static DEFINE_PER_CPU(int, mce_next_interval); /* in jiffies */
1152 static DEFINE_PER_CPU(struct timer_list, mce_timer);
1153
1154 static void mce_start_timer(unsigned long data)
1155 {
1156         struct timer_list *t = &per_cpu(mce_timer, data);
1157         int *n;
1158
1159         WARN_ON(smp_processor_id() != data);
1160
1161         if (mce_available(&current_cpu_data)) {
1162                 machine_check_poll(MCP_TIMESTAMP,
1163                                 &__get_cpu_var(mce_poll_banks));
1164         }
1165
1166         /*
1167          * Alert userspace if needed.  If we logged an MCE, reduce the
1168          * polling interval, otherwise increase the polling interval.
1169          */
1170         n = &__get_cpu_var(mce_next_interval);
1171         if (mce_notify_irq())
1172                 *n = max(*n/2, HZ/100);
1173         else
1174                 *n = min(*n*2, (int)round_jiffies_relative(check_interval*HZ));
1175
1176         t->expires = jiffies + *n;
1177         add_timer_on(t, smp_processor_id());
1178 }
1179
1180 static void mce_do_trigger(struct work_struct *work)
1181 {
1182         call_usermodehelper(mce_helper, mce_helper_argv, NULL, UMH_NO_WAIT);
1183 }
1184
1185 static DECLARE_WORK(mce_trigger_work, mce_do_trigger);
1186
1187 /*
1188  * Notify the user(s) about new machine check events.
1189  * Can be called from interrupt context, but not from machine check/NMI
1190  * context.
1191  */
1192 int mce_notify_irq(void)
1193 {
1194         /* Not more than two messages every minute */
1195         static DEFINE_RATELIMIT_STATE(ratelimit, 60*HZ, 2);
1196
1197         clear_thread_flag(TIF_MCE_NOTIFY);
1198
1199         if (test_and_clear_bit(0, &mce_need_notify)) {
1200                 wake_up_interruptible(&mce_wait);
1201
1202                 /*
1203                  * There is no risk of missing notifications because
1204                  * work_pending is always cleared before the function is
1205                  * executed.
1206                  */
1207                 if (mce_helper[0] && !work_pending(&mce_trigger_work))
1208                         schedule_work(&mce_trigger_work);
1209
1210                 if (__ratelimit(&ratelimit))
1211                         pr_info(HW_ERR "Machine check events logged\n");
1212
1213                 return 1;
1214         }
1215         return 0;
1216 }
1217 EXPORT_SYMBOL_GPL(mce_notify_irq);
1218
1219 static int __cpuinit __mcheck_cpu_mce_banks_init(void)
1220 {
1221         int i;
1222
1223         mce_banks = kzalloc(banks * sizeof(struct mce_bank), GFP_KERNEL);
1224         if (!mce_banks)
1225                 return -ENOMEM;
1226         for (i = 0; i < banks; i++) {
1227                 struct mce_bank *b = &mce_banks[i];
1228
1229                 b->ctl = -1ULL;
1230                 b->init = 1;
1231         }
1232         return 0;
1233 }
1234
1235 /*
1236  * Initialize Machine Checks for a CPU.
1237  */
1238 static int __cpuinit __mcheck_cpu_cap_init(void)
1239 {
1240         unsigned b;
1241         u64 cap;
1242
1243         rdmsrl(MSR_IA32_MCG_CAP, cap);
1244
1245         b = cap & MCG_BANKCNT_MASK;
1246         if (!banks)
1247                 printk(KERN_INFO "mce: CPU supports %d MCE banks\n", b);
1248
1249         if (b > MAX_NR_BANKS) {
1250                 printk(KERN_WARNING
1251                        "MCE: Using only %u machine check banks out of %u\n",
1252                         MAX_NR_BANKS, b);
1253                 b = MAX_NR_BANKS;
1254         }
1255
1256         /* Don't support asymmetric configurations today */
1257         WARN_ON(banks != 0 && b != banks);
1258         banks = b;
1259         if (!mce_banks) {
1260                 int err = __mcheck_cpu_mce_banks_init();
1261
1262                 if (err)
1263                         return err;
1264         }
1265
1266         /* Use accurate RIP reporting if available. */
1267         if ((cap & MCG_EXT_P) && MCG_EXT_CNT(cap) >= 9)
1268                 rip_msr = MSR_IA32_MCG_EIP;
1269
1270         if (cap & MCG_SER_P)
1271                 mce_ser = 1;
1272
1273         return 0;
1274 }
1275
1276 static void __mcheck_cpu_init_generic(void)
1277 {
1278         mce_banks_t all_banks;
1279         u64 cap;
1280         int i;
1281
1282         /*
1283          * Log the machine checks left over from the previous reset.
1284          */
1285         bitmap_fill(all_banks, MAX_NR_BANKS);
1286         machine_check_poll(MCP_UC|(!mce_bootlog ? MCP_DONTLOG : 0), &all_banks);
1287
1288         set_in_cr4(X86_CR4_MCE);
1289
1290         rdmsrl(MSR_IA32_MCG_CAP, cap);
1291         if (cap & MCG_CTL_P)
1292                 wrmsr(MSR_IA32_MCG_CTL, 0xffffffff, 0xffffffff);
1293
1294         for (i = 0; i < banks; i++) {
1295                 struct mce_bank *b = &mce_banks[i];
1296
1297                 if (!b->init)
1298                         continue;
1299                 wrmsrl(MSR_IA32_MCx_CTL(i), b->ctl);
1300                 wrmsrl(MSR_IA32_MCx_STATUS(i), 0);
1301         }
1302 }
1303
1304 /* Add per CPU specific workarounds here */
1305 static int __cpuinit __mcheck_cpu_apply_quirks(struct cpuinfo_x86 *c)
1306 {
1307         if (c->x86_vendor == X86_VENDOR_UNKNOWN) {
1308                 pr_info("MCE: unknown CPU type - not enabling MCE support.\n");
1309                 return -EOPNOTSUPP;
1310         }
1311
1312         /* This should be disabled by the BIOS, but isn't always */
1313         if (c->x86_vendor == X86_VENDOR_AMD) {
1314                 if (c->x86 == 15 && banks > 4) {
1315                         /*
1316                          * disable GART TBL walk error reporting, which
1317                          * trips off incorrectly with the IOMMU & 3ware
1318                          * & Cerberus:
1319                          */
1320                         clear_bit(10, (unsigned long *)&mce_banks[4].ctl);
1321                 }
1322                 if (c->x86 <= 17 && mce_bootlog < 0) {
1323                         /*
1324                          * Lots of broken BIOS around that don't clear them
1325                          * by default and leave crap in there. Don't log:
1326                          */
1327                         mce_bootlog = 0;
1328                 }
1329                 /*
1330                  * Various K7s with broken bank 0 around. Always disable
1331                  * by default.
1332                  */
1333                  if (c->x86 == 6 && banks > 0)
1334                         mce_banks[0].ctl = 0;
1335         }
1336
1337         if (c->x86_vendor == X86_VENDOR_INTEL) {
1338                 /*
1339                  * SDM documents that on family 6 bank 0 should not be written
1340                  * because it aliases to another special BIOS controlled
1341                  * register.
1342                  * But it's not aliased anymore on model 0x1a+
1343                  * Don't ignore bank 0 completely because there could be a
1344                  * valid event later, merely don't write CTL0.
1345                  */
1346
1347                 if (c->x86 == 6 && c->x86_model < 0x1A && banks > 0)
1348                         mce_banks[0].init = 0;
1349
1350                 /*
1351                  * All newer Intel systems support MCE broadcasting. Enable
1352                  * synchronization with a one second timeout.
1353                  */
1354                 if ((c->x86 > 6 || (c->x86 == 6 && c->x86_model >= 0xe)) &&
1355                         monarch_timeout < 0)
1356                         monarch_timeout = USEC_PER_SEC;
1357
1358                 /*
1359                  * There are also broken BIOSes on some Pentium M and
1360                  * earlier systems:
1361                  */
1362                 if (c->x86 == 6 && c->x86_model <= 13 && mce_bootlog < 0)
1363                         mce_bootlog = 0;
1364         }
1365         if (monarch_timeout < 0)
1366                 monarch_timeout = 0;
1367         if (mce_bootlog != 0)
1368                 mce_panic_timeout = 30;
1369
1370         return 0;
1371 }
1372
1373 static void __cpuinit __mcheck_cpu_ancient_init(struct cpuinfo_x86 *c)
1374 {
1375         if (c->x86 != 5)
1376                 return;
1377         switch (c->x86_vendor) {
1378         case X86_VENDOR_INTEL:
1379                 intel_p5_mcheck_init(c);
1380                 break;
1381         case X86_VENDOR_CENTAUR:
1382                 winchip_mcheck_init(c);
1383                 break;
1384         }
1385 }
1386
1387 static void __mcheck_cpu_init_vendor(struct cpuinfo_x86 *c)
1388 {
1389         switch (c->x86_vendor) {
1390         case X86_VENDOR_INTEL:
1391                 mce_intel_feature_init(c);
1392                 break;
1393         case X86_VENDOR_AMD:
1394                 mce_amd_feature_init(c);
1395                 break;
1396         default:
1397                 break;
1398         }
1399 }
1400
1401 static void __mcheck_cpu_init_timer(void)
1402 {
1403         struct timer_list *t = &__get_cpu_var(mce_timer);
1404         int *n = &__get_cpu_var(mce_next_interval);
1405
1406         setup_timer(t, mce_start_timer, smp_processor_id());
1407
1408         if (mce_ignore_ce)
1409                 return;
1410
1411         *n = check_interval * HZ;
1412         if (!*n)
1413                 return;
1414         t->expires = round_jiffies(jiffies + *n);
1415         add_timer_on(t, smp_processor_id());
1416 }
1417
1418 /* Handle unconfigured int18 (should never happen) */
1419 static void unexpected_machine_check(struct pt_regs *regs, long error_code)
1420 {
1421         printk(KERN_ERR "CPU#%d: Unexpected int18 (Machine Check).\n",
1422                smp_processor_id());
1423 }
1424
1425 /* Call the installed machine check handler for this CPU setup. */
1426 void (*machine_check_vector)(struct pt_regs *, long error_code) =
1427                                                 unexpected_machine_check;
1428
1429 /*
1430  * Called for each booted CPU to set up machine checks.
1431  * Must be called with preempt off:
1432  */
1433 void __cpuinit mcheck_cpu_init(struct cpuinfo_x86 *c)
1434 {
1435         if (mce_disabled)
1436                 return;
1437
1438         __mcheck_cpu_ancient_init(c);
1439
1440         if (!mce_available(c))
1441                 return;
1442
1443         if (__mcheck_cpu_cap_init() < 0 || __mcheck_cpu_apply_quirks(c) < 0) {
1444                 mce_disabled = 1;
1445                 return;
1446         }
1447
1448         machine_check_vector = do_machine_check;
1449
1450         __mcheck_cpu_init_generic();
1451         __mcheck_cpu_init_vendor(c);
1452         __mcheck_cpu_init_timer();
1453         INIT_WORK(&__get_cpu_var(mce_work), mce_process_work);
1454
1455 }
1456
1457 /*
1458  * Character device to read and clear the MCE log.
1459  */
1460
1461 static DEFINE_SPINLOCK(mce_state_lock);
1462 static int              open_count;             /* #times opened */
1463 static int              open_exclu;             /* already open exclusive? */
1464
1465 static int mce_open(struct inode *inode, struct file *file)
1466 {
1467         spin_lock(&mce_state_lock);
1468
1469         if (open_exclu || (open_count && (file->f_flags & O_EXCL))) {
1470                 spin_unlock(&mce_state_lock);
1471
1472                 return -EBUSY;
1473         }
1474
1475         if (file->f_flags & O_EXCL)
1476                 open_exclu = 1;
1477         open_count++;
1478
1479         spin_unlock(&mce_state_lock);
1480
1481         return nonseekable_open(inode, file);
1482 }
1483
1484 static int mce_release(struct inode *inode, struct file *file)
1485 {
1486         spin_lock(&mce_state_lock);
1487
1488         open_count--;
1489         open_exclu = 0;
1490
1491         spin_unlock(&mce_state_lock);
1492
1493         return 0;
1494 }
1495
1496 static void collect_tscs(void *data)
1497 {
1498         unsigned long *cpu_tsc = (unsigned long *)data;
1499
1500         rdtscll(cpu_tsc[smp_processor_id()]);
1501 }
1502
1503 static int mce_apei_read_done;
1504
1505 /* Collect MCE record of previous boot in persistent storage via APEI ERST. */
1506 static int __mce_read_apei(char __user **ubuf, size_t usize)
1507 {
1508         int rc;
1509         u64 record_id;
1510         struct mce m;
1511
1512         if (usize < sizeof(struct mce))
1513                 return -EINVAL;
1514
1515         rc = apei_read_mce(&m, &record_id);
1516         /* Error or no more MCE record */
1517         if (rc <= 0) {
1518                 mce_apei_read_done = 1;
1519                 return rc;
1520         }
1521         rc = -EFAULT;
1522         if (copy_to_user(*ubuf, &m, sizeof(struct mce)))
1523                 return rc;
1524         /*
1525          * In fact, we should have cleared the record after that has
1526          * been flushed to the disk or sent to network in
1527          * /sbin/mcelog, but we have no interface to support that now,
1528          * so just clear it to avoid duplication.
1529          */
1530         rc = apei_clear_mce(record_id);
1531         if (rc) {
1532                 mce_apei_read_done = 1;
1533                 return rc;
1534         }
1535         *ubuf += sizeof(struct mce);
1536
1537         return 0;
1538 }
1539
1540 static ssize_t mce_read(struct file *filp, char __user *ubuf, size_t usize,
1541                         loff_t *off)
1542 {
1543         char __user *buf = ubuf;
1544         unsigned long *cpu_tsc;
1545         unsigned prev, next;
1546         int i, err;
1547
1548         cpu_tsc = kmalloc(nr_cpu_ids * sizeof(long), GFP_KERNEL);
1549         if (!cpu_tsc)
1550                 return -ENOMEM;
1551
1552         mutex_lock(&mce_read_mutex);
1553
1554         if (!mce_apei_read_done) {
1555                 err = __mce_read_apei(&buf, usize);
1556                 if (err || buf != ubuf)
1557                         goto out;
1558         }
1559
1560         next = rcu_dereference_check_mce(mcelog.next);
1561
1562         /* Only supports full reads right now */
1563         err = -EINVAL;
1564         if (*off != 0 || usize < MCE_LOG_LEN*sizeof(struct mce))
1565                 goto out;
1566
1567         err = 0;
1568         prev = 0;
1569         do {
1570                 for (i = prev; i < next; i++) {
1571                         unsigned long start = jiffies;
1572
1573                         while (!mcelog.entry[i].finished) {
1574                                 if (time_after_eq(jiffies, start + 2)) {
1575                                         memset(mcelog.entry + i, 0,
1576                                                sizeof(struct mce));
1577                                         goto timeout;
1578                                 }
1579                                 cpu_relax();
1580                         }
1581                         smp_rmb();
1582                         err |= copy_to_user(buf, mcelog.entry + i,
1583                                             sizeof(struct mce));
1584                         buf += sizeof(struct mce);
1585 timeout:
1586                         ;
1587                 }
1588
1589                 memset(mcelog.entry + prev, 0,
1590                        (next - prev) * sizeof(struct mce));
1591                 prev = next;
1592                 next = cmpxchg(&mcelog.next, prev, 0);
1593         } while (next != prev);
1594
1595         synchronize_sched();
1596
1597         /*
1598          * Collect entries that were still getting written before the
1599          * synchronize.
1600          */
1601         on_each_cpu(collect_tscs, cpu_tsc, 1);
1602
1603         for (i = next; i < MCE_LOG_LEN; i++) {
1604                 if (mcelog.entry[i].finished &&
1605                     mcelog.entry[i].tsc < cpu_tsc[mcelog.entry[i].cpu]) {
1606                         err |= copy_to_user(buf, mcelog.entry+i,
1607                                             sizeof(struct mce));
1608                         smp_rmb();
1609                         buf += sizeof(struct mce);
1610                         memset(&mcelog.entry[i], 0, sizeof(struct mce));
1611                 }
1612         }
1613
1614         if (err)
1615                 err = -EFAULT;
1616
1617 out:
1618         mutex_unlock(&mce_read_mutex);
1619         kfree(cpu_tsc);
1620
1621         return err ? err : buf - ubuf;
1622 }
1623
1624 static unsigned int mce_poll(struct file *file, poll_table *wait)
1625 {
1626         poll_wait(file, &mce_wait, wait);
1627         if (rcu_dereference_check_mce(mcelog.next))
1628                 return POLLIN | POLLRDNORM;
1629         if (!mce_apei_read_done && apei_check_mce())
1630                 return POLLIN | POLLRDNORM;
1631         return 0;
1632 }
1633
1634 static long mce_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
1635 {
1636         int __user *p = (int __user *)arg;
1637
1638         if (!capable(CAP_SYS_ADMIN))
1639                 return -EPERM;
1640
1641         switch (cmd) {
1642         case MCE_GET_RECORD_LEN:
1643                 return put_user(sizeof(struct mce), p);
1644         case MCE_GET_LOG_LEN:
1645                 return put_user(MCE_LOG_LEN, p);
1646         case MCE_GETCLEAR_FLAGS: {
1647                 unsigned flags;
1648
1649                 do {
1650                         flags = mcelog.flags;
1651                 } while (cmpxchg(&mcelog.flags, flags, 0) != flags);
1652
1653                 return put_user(flags, p);
1654         }
1655         default:
1656                 return -ENOTTY;
1657         }
1658 }
1659
1660 /* Modified in mce-inject.c, so not static or const */
1661 struct file_operations mce_chrdev_ops = {
1662         .open                   = mce_open,
1663         .release                = mce_release,
1664         .read                   = mce_read,
1665         .poll                   = mce_poll,
1666         .unlocked_ioctl         = mce_ioctl,
1667 };
1668 EXPORT_SYMBOL_GPL(mce_chrdev_ops);
1669
1670 static struct miscdevice mce_log_device = {
1671         MISC_MCELOG_MINOR,
1672         "mcelog",
1673         &mce_chrdev_ops,
1674 };
1675
1676 /*
1677  * mce=off Disables machine check
1678  * mce=no_cmci Disables CMCI
1679  * mce=dont_log_ce Clears corrected events silently, no log created for CEs.
1680  * mce=ignore_ce Disables polling and CMCI, corrected events are not cleared.
1681  * mce=TOLERANCELEVEL[,monarchtimeout] (number, see above)
1682  *      monarchtimeout is how long to wait for other CPUs on machine
1683  *      check, or 0 to not wait
1684  * mce=bootlog Log MCEs from before booting. Disabled by default on AMD.
1685  * mce=nobootlog Don't log MCEs from before booting.
1686  */
1687 static int __init mcheck_enable(char *str)
1688 {
1689         if (*str == 0) {
1690                 enable_p5_mce();
1691                 return 1;
1692         }
1693         if (*str == '=')
1694                 str++;
1695         if (!strcmp(str, "off"))
1696                 mce_disabled = 1;
1697         else if (!strcmp(str, "no_cmci"))
1698                 mce_cmci_disabled = 1;
1699         else if (!strcmp(str, "dont_log_ce"))
1700                 mce_dont_log_ce = 1;
1701         else if (!strcmp(str, "ignore_ce"))
1702                 mce_ignore_ce = 1;
1703         else if (!strcmp(str, "bootlog") || !strcmp(str, "nobootlog"))
1704                 mce_bootlog = (str[0] == 'b');
1705         else if (isdigit(str[0])) {
1706                 get_option(&str, &tolerant);
1707                 if (*str == ',') {
1708                         ++str;
1709                         get_option(&str, &monarch_timeout);
1710                 }
1711         } else {
1712                 printk(KERN_INFO "mce argument %s ignored. Please use /sys\n",
1713                        str);
1714                 return 0;
1715         }
1716         return 1;
1717 }
1718 __setup("mce", mcheck_enable);
1719
1720 int __init mcheck_init(void)
1721 {
1722         atomic_notifier_chain_register(&x86_mce_decoder_chain, &mce_dec_nb);
1723
1724         mcheck_intel_therm_init();
1725
1726         return 0;
1727 }
1728
1729 /*
1730  * Sysfs support
1731  */
1732
1733 /*
1734  * Disable machine checks on suspend and shutdown. We can't really handle
1735  * them later.
1736  */
1737 static int mce_disable_error_reporting(void)
1738 {
1739         int i;
1740
1741         for (i = 0; i < banks; i++) {
1742                 struct mce_bank *b = &mce_banks[i];
1743
1744                 if (b->init)
1745                         wrmsrl(MSR_IA32_MCx_CTL(i), 0);
1746         }
1747         return 0;
1748 }
1749
1750 static int mce_suspend(struct sys_device *dev, pm_message_t state)
1751 {
1752         return mce_disable_error_reporting();
1753 }
1754
1755 static int mce_shutdown(struct sys_device *dev)
1756 {
1757         return mce_disable_error_reporting();
1758 }
1759
1760 /*
1761  * On resume clear all MCE state. Don't want to see leftovers from the BIOS.
1762  * Only one CPU is active at this time, the others get re-added later using
1763  * CPU hotplug:
1764  */
1765 static int mce_resume(struct sys_device *dev)
1766 {
1767         __mcheck_cpu_init_generic();
1768         __mcheck_cpu_init_vendor(&current_cpu_data);
1769
1770         return 0;
1771 }
1772
1773 static void mce_cpu_restart(void *data)
1774 {
1775         del_timer_sync(&__get_cpu_var(mce_timer));
1776         if (!mce_available(&current_cpu_data))
1777                 return;
1778         __mcheck_cpu_init_generic();
1779         __mcheck_cpu_init_timer();
1780 }
1781
1782 /* Reinit MCEs after user configuration changes */
1783 static void mce_restart(void)
1784 {
1785         on_each_cpu(mce_cpu_restart, NULL, 1);
1786 }
1787
1788 /* Toggle features for corrected errors */
1789 static void mce_disable_ce(void *all)
1790 {
1791         if (!mce_available(&current_cpu_data))
1792                 return;
1793         if (all)
1794                 del_timer_sync(&__get_cpu_var(mce_timer));
1795         cmci_clear();
1796 }
1797
1798 static void mce_enable_ce(void *all)
1799 {
1800         if (!mce_available(&current_cpu_data))
1801                 return;
1802         cmci_reenable();
1803         cmci_recheck();
1804         if (all)
1805                 __mcheck_cpu_init_timer();
1806 }
1807
1808 static struct sysdev_class mce_sysclass = {
1809         .suspend        = mce_suspend,
1810         .shutdown       = mce_shutdown,
1811         .resume         = mce_resume,
1812         .name           = "machinecheck",
1813 };
1814
1815 DEFINE_PER_CPU(struct sys_device, mce_dev);
1816
1817 __cpuinitdata
1818 void (*threshold_cpu_callback)(unsigned long action, unsigned int cpu);
1819
1820 static inline struct mce_bank *attr_to_bank(struct sysdev_attribute *attr)
1821 {
1822         return container_of(attr, struct mce_bank, attr);
1823 }
1824
1825 static ssize_t show_bank(struct sys_device *s, struct sysdev_attribute *attr,
1826                          char *buf)
1827 {
1828         return sprintf(buf, "%llx\n", attr_to_bank(attr)->ctl);
1829 }
1830
1831 static ssize_t set_bank(struct sys_device *s, struct sysdev_attribute *attr,
1832                         const char *buf, size_t size)
1833 {
1834         u64 new;
1835
1836         if (strict_strtoull(buf, 0, &new) < 0)
1837                 return -EINVAL;
1838
1839         attr_to_bank(attr)->ctl = new;
1840         mce_restart();
1841
1842         return size;
1843 }
1844
1845 static ssize_t
1846 show_trigger(struct sys_device *s, struct sysdev_attribute *attr, char *buf)
1847 {
1848         strcpy(buf, mce_helper);
1849         strcat(buf, "\n");
1850         return strlen(mce_helper) + 1;
1851 }
1852
1853 static ssize_t set_trigger(struct sys_device *s, struct sysdev_attribute *attr,
1854                                 const char *buf, size_t siz)
1855 {
1856         char *p;
1857
1858         strncpy(mce_helper, buf, sizeof(mce_helper));
1859         mce_helper[sizeof(mce_helper)-1] = 0;
1860         p = strchr(mce_helper, '\n');
1861
1862         if (p)
1863                 *p = 0;
1864
1865         return strlen(mce_helper) + !!p;
1866 }
1867
1868 static ssize_t set_ignore_ce(struct sys_device *s,
1869                              struct sysdev_attribute *attr,
1870                              const char *buf, size_t size)
1871 {
1872         u64 new;
1873
1874         if (strict_strtoull(buf, 0, &new) < 0)
1875                 return -EINVAL;
1876
1877         if (mce_ignore_ce ^ !!new) {
1878                 if (new) {
1879                         /* disable ce features */
1880                         on_each_cpu(mce_disable_ce, (void *)1, 1);
1881                         mce_ignore_ce = 1;
1882                 } else {
1883                         /* enable ce features */
1884                         mce_ignore_ce = 0;
1885                         on_each_cpu(mce_enable_ce, (void *)1, 1);
1886                 }
1887         }
1888         return size;
1889 }
1890
1891 static ssize_t set_cmci_disabled(struct sys_device *s,
1892                                  struct sysdev_attribute *attr,
1893                                  const char *buf, size_t size)
1894 {
1895         u64 new;
1896
1897         if (strict_strtoull(buf, 0, &new) < 0)
1898                 return -EINVAL;
1899
1900         if (mce_cmci_disabled ^ !!new) {
1901                 if (new) {
1902                         /* disable cmci */
1903                         on_each_cpu(mce_disable_ce, NULL, 1);
1904                         mce_cmci_disabled = 1;
1905                 } else {
1906                         /* enable cmci */
1907                         mce_cmci_disabled = 0;
1908                         on_each_cpu(mce_enable_ce, NULL, 1);
1909                 }
1910         }
1911         return size;
1912 }
1913
1914 static ssize_t store_int_with_restart(struct sys_device *s,
1915                                       struct sysdev_attribute *attr,
1916                                       const char *buf, size_t size)
1917 {
1918         ssize_t ret = sysdev_store_int(s, attr, buf, size);
1919         mce_restart();
1920         return ret;
1921 }
1922
1923 static SYSDEV_ATTR(trigger, 0644, show_trigger, set_trigger);
1924 static SYSDEV_INT_ATTR(tolerant, 0644, tolerant);
1925 static SYSDEV_INT_ATTR(monarch_timeout, 0644, monarch_timeout);
1926 static SYSDEV_INT_ATTR(dont_log_ce, 0644, mce_dont_log_ce);
1927
1928 static struct sysdev_ext_attribute attr_check_interval = {
1929         _SYSDEV_ATTR(check_interval, 0644, sysdev_show_int,
1930                      store_int_with_restart),
1931         &check_interval
1932 };
1933
1934 static struct sysdev_ext_attribute attr_ignore_ce = {
1935         _SYSDEV_ATTR(ignore_ce, 0644, sysdev_show_int, set_ignore_ce),
1936         &mce_ignore_ce
1937 };
1938
1939 static struct sysdev_ext_attribute attr_cmci_disabled = {
1940         _SYSDEV_ATTR(cmci_disabled, 0644, sysdev_show_int, set_cmci_disabled),
1941         &mce_cmci_disabled
1942 };
1943
1944 static struct sysdev_attribute *mce_attrs[] = {
1945         &attr_tolerant.attr,
1946         &attr_check_interval.attr,
1947         &attr_trigger,
1948         &attr_monarch_timeout.attr,
1949         &attr_dont_log_ce.attr,
1950         &attr_ignore_ce.attr,
1951         &attr_cmci_disabled.attr,
1952         NULL
1953 };
1954
1955 static cpumask_var_t mce_dev_initialized;
1956
1957 /* Per cpu sysdev init. All of the cpus still share the same ctrl bank: */
1958 static __cpuinit int mce_create_device(unsigned int cpu)
1959 {
1960         int err;
1961         int i, j;
1962
1963         if (!mce_available(&boot_cpu_data))
1964                 return -EIO;
1965
1966         memset(&per_cpu(mce_dev, cpu).kobj, 0, sizeof(struct kobject));
1967         per_cpu(mce_dev, cpu).id        = cpu;
1968         per_cpu(mce_dev, cpu).cls       = &mce_sysclass;
1969
1970         err = sysdev_register(&per_cpu(mce_dev, cpu));
1971         if (err)
1972                 return err;
1973
1974         for (i = 0; mce_attrs[i]; i++) {
1975                 err = sysdev_create_file(&per_cpu(mce_dev, cpu), mce_attrs[i]);
1976                 if (err)
1977                         goto error;
1978         }
1979         for (j = 0; j < banks; j++) {
1980                 err = sysdev_create_file(&per_cpu(mce_dev, cpu),
1981                                         &mce_banks[j].attr);
1982                 if (err)
1983                         goto error2;
1984         }
1985         cpumask_set_cpu(cpu, mce_dev_initialized);
1986
1987         return 0;
1988 error2:
1989         while (--j >= 0)
1990                 sysdev_remove_file(&per_cpu(mce_dev, cpu), &mce_banks[j].attr);
1991 error:
1992         while (--i >= 0)
1993                 sysdev_remove_file(&per_cpu(mce_dev, cpu), mce_attrs[i]);
1994
1995         sysdev_unregister(&per_cpu(mce_dev, cpu));
1996
1997         return err;
1998 }
1999
2000 static __cpuinit void mce_remove_device(unsigned int cpu)
2001 {
2002         int i;
2003
2004         if (!cpumask_test_cpu(cpu, mce_dev_initialized))
2005                 return;
2006
2007         for (i = 0; mce_attrs[i]; i++)
2008                 sysdev_remove_file(&per_cpu(mce_dev, cpu), mce_attrs[i]);
2009
2010         for (i = 0; i < banks; i++)
2011                 sysdev_remove_file(&per_cpu(mce_dev, cpu), &mce_banks[i].attr);
2012
2013         sysdev_unregister(&per_cpu(mce_dev, cpu));
2014         cpumask_clear_cpu(cpu, mce_dev_initialized);
2015 }
2016
2017 /* Make sure there are no machine checks on offlined CPUs. */
2018 static void __cpuinit mce_disable_cpu(void *h)
2019 {
2020         unsigned long action = *(unsigned long *)h;
2021         int i;
2022
2023         if (!mce_available(&current_cpu_data))
2024                 return;
2025
2026         if (!(action & CPU_TASKS_FROZEN))
2027                 cmci_clear();
2028         for (i = 0; i < banks; i++) {
2029                 struct mce_bank *b = &mce_banks[i];
2030
2031                 if (b->init)
2032                         wrmsrl(MSR_IA32_MCx_CTL(i), 0);
2033         }
2034 }
2035
2036 static void __cpuinit mce_reenable_cpu(void *h)
2037 {
2038         unsigned long action = *(unsigned long *)h;
2039         int i;
2040
2041         if (!mce_available(&current_cpu_data))
2042                 return;
2043
2044         if (!(action & CPU_TASKS_FROZEN))
2045                 cmci_reenable();
2046         for (i = 0; i < banks; i++) {
2047                 struct mce_bank *b = &mce_banks[i];
2048
2049                 if (b->init)
2050                         wrmsrl(MSR_IA32_MCx_CTL(i), b->ctl);
2051         }
2052 }
2053
2054 /* Get notified when a cpu comes on/off. Be hotplug friendly. */
2055 static int __cpuinit
2056 mce_cpu_callback(struct notifier_block *nfb, unsigned long action, void *hcpu)
2057 {
2058         unsigned int cpu = (unsigned long)hcpu;
2059         struct timer_list *t = &per_cpu(mce_timer, cpu);
2060
2061         switch (action) {
2062         case CPU_ONLINE:
2063         case CPU_ONLINE_FROZEN:
2064                 mce_create_device(cpu);
2065                 if (threshold_cpu_callback)
2066                         threshold_cpu_callback(action, cpu);
2067                 break;
2068         case CPU_DEAD:
2069         case CPU_DEAD_FROZEN:
2070                 if (threshold_cpu_callback)
2071                         threshold_cpu_callback(action, cpu);
2072                 mce_remove_device(cpu);
2073                 break;
2074         case CPU_DOWN_PREPARE:
2075         case CPU_DOWN_PREPARE_FROZEN:
2076                 del_timer_sync(t);
2077                 smp_call_function_single(cpu, mce_disable_cpu, &action, 1);
2078                 break;
2079         case CPU_DOWN_FAILED:
2080         case CPU_DOWN_FAILED_FROZEN:
2081                 if (!mce_ignore_ce && check_interval) {
2082                         t->expires = round_jiffies(jiffies +
2083                                            __get_cpu_var(mce_next_interval));
2084                         add_timer_on(t, cpu);
2085                 }
2086                 smp_call_function_single(cpu, mce_reenable_cpu, &action, 1);
2087                 break;
2088         case CPU_POST_DEAD:
2089                 /* intentionally ignoring frozen here */
2090                 cmci_rediscover(cpu);
2091                 break;
2092         }
2093         return NOTIFY_OK;
2094 }
2095
2096 static struct notifier_block mce_cpu_notifier __cpuinitdata = {
2097         .notifier_call = mce_cpu_callback,
2098 };
2099
2100 static __init void mce_init_banks(void)
2101 {
2102         int i;
2103
2104         for (i = 0; i < banks; i++) {
2105                 struct mce_bank *b = &mce_banks[i];
2106                 struct sysdev_attribute *a = &b->attr;
2107
2108                 sysfs_attr_init(&a->attr);
2109                 a->attr.name    = b->attrname;
2110                 snprintf(b->attrname, ATTR_LEN, "bank%d", i);
2111
2112                 a->attr.mode    = 0644;
2113                 a->show         = show_bank;
2114                 a->store        = set_bank;
2115         }
2116 }
2117
2118 static __init int mcheck_init_device(void)
2119 {
2120         int err;
2121         int i = 0;
2122
2123         if (!mce_available(&boot_cpu_data))
2124                 return -EIO;
2125
2126         zalloc_cpumask_var(&mce_dev_initialized, GFP_KERNEL);
2127
2128         mce_init_banks();
2129
2130         err = sysdev_class_register(&mce_sysclass);
2131         if (err)
2132                 return err;
2133
2134         for_each_online_cpu(i) {
2135                 err = mce_create_device(i);
2136                 if (err)
2137                         return err;
2138         }
2139
2140         register_hotcpu_notifier(&mce_cpu_notifier);
2141         misc_register(&mce_log_device);
2142
2143         return err;
2144 }
2145
2146 device_initcall(mcheck_init_device);
2147
2148 /*
2149  * Old style boot options parsing. Only for compatibility.
2150  */
2151 static int __init mcheck_disable(char *str)
2152 {
2153         mce_disabled = 1;
2154         return 1;
2155 }
2156 __setup("nomce", mcheck_disable);
2157
2158 #ifdef CONFIG_DEBUG_FS
2159 struct dentry *mce_get_debugfs_dir(void)
2160 {
2161         static struct dentry *dmce;
2162
2163         if (!dmce)
2164                 dmce = debugfs_create_dir("mce", NULL);
2165
2166         return dmce;
2167 }
2168
2169 static void mce_reset(void)
2170 {
2171         cpu_missing = 0;
2172         atomic_set(&mce_fake_paniced, 0);
2173         atomic_set(&mce_executing, 0);
2174         atomic_set(&mce_callin, 0);
2175         atomic_set(&global_nwo, 0);
2176 }
2177
2178 static int fake_panic_get(void *data, u64 *val)
2179 {
2180         *val = fake_panic;
2181         return 0;
2182 }
2183
2184 static int fake_panic_set(void *data, u64 val)
2185 {
2186         mce_reset();
2187         fake_panic = val;
2188         return 0;
2189 }
2190
2191 DEFINE_SIMPLE_ATTRIBUTE(fake_panic_fops, fake_panic_get,
2192                         fake_panic_set, "%llu\n");
2193
2194 static int __init mcheck_debugfs_init(void)
2195 {
2196         struct dentry *dmce, *ffake_panic;
2197
2198         dmce = mce_get_debugfs_dir();
2199         if (!dmce)
2200                 return -ENOMEM;
2201         ffake_panic = debugfs_create_file("fake_panic", 0444, dmce, NULL,
2202                                           &fake_panic_fops);
2203         if (!ffake_panic)
2204                 return -ENOMEM;
2205
2206         return 0;
2207 }
2208 late_initcall(mcheck_debugfs_init);
2209 #endif