x86/extable: Rework the exception table mechanics
[platform/kernel/linux-rpi.git] / arch / x86 / kernel / cpu / mce / core.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Machine check handler.
4  *
5  * K8 parts Copyright 2002,2003 Andi Kleen, SuSE Labs.
6  * Rest from unknown author(s).
7  * 2004 Andi Kleen. Rewrote most of it.
8  * Copyright 2008 Intel Corporation
9  * Author: Andi Kleen
10  */
11
12 #include <linux/thread_info.h>
13 #include <linux/capability.h>
14 #include <linux/miscdevice.h>
15 #include <linux/ratelimit.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/device.h>
24 #include <linux/syscore_ops.h>
25 #include <linux/delay.h>
26 #include <linux/ctype.h>
27 #include <linux/sched.h>
28 #include <linux/sysfs.h>
29 #include <linux/types.h>
30 #include <linux/slab.h>
31 #include <linux/init.h>
32 #include <linux/kmod.h>
33 #include <linux/poll.h>
34 #include <linux/nmi.h>
35 #include <linux/cpu.h>
36 #include <linux/ras.h>
37 #include <linux/smp.h>
38 #include <linux/fs.h>
39 #include <linux/mm.h>
40 #include <linux/debugfs.h>
41 #include <linux/irq_work.h>
42 #include <linux/export.h>
43 #include <linux/set_memory.h>
44 #include <linux/sync_core.h>
45 #include <linux/task_work.h>
46 #include <linux/hardirq.h>
47
48 #include <asm/intel-family.h>
49 #include <asm/processor.h>
50 #include <asm/traps.h>
51 #include <asm/tlbflush.h>
52 #include <asm/mce.h>
53 #include <asm/msr.h>
54 #include <asm/reboot.h>
55
56 #include "internal.h"
57
58 /* sysfs synchronization */
59 static DEFINE_MUTEX(mce_sysfs_mutex);
60
61 #define CREATE_TRACE_POINTS
62 #include <trace/events/mce.h>
63
64 #define SPINUNIT                100     /* 100ns */
65
66 DEFINE_PER_CPU(unsigned, mce_exception_count);
67
68 DEFINE_PER_CPU_READ_MOSTLY(unsigned int, mce_num_banks);
69
70 struct mce_bank {
71         u64                     ctl;                    /* subevents to enable */
72         bool                    init;                   /* initialise bank? */
73 };
74 static DEFINE_PER_CPU_READ_MOSTLY(struct mce_bank[MAX_NR_BANKS], mce_banks_array);
75
76 #define ATTR_LEN               16
77 /* One object for each MCE bank, shared by all CPUs */
78 struct mce_bank_dev {
79         struct device_attribute attr;                   /* device attribute */
80         char                    attrname[ATTR_LEN];     /* attribute name */
81         u8                      bank;                   /* bank number */
82 };
83 static struct mce_bank_dev mce_bank_devs[MAX_NR_BANKS];
84
85 struct mce_vendor_flags mce_flags __read_mostly;
86
87 struct mca_config mca_cfg __read_mostly = {
88         .bootlog  = -1,
89         /*
90          * Tolerant levels:
91          * 0: always panic on uncorrected errors, log corrected errors
92          * 1: panic or SIGBUS on uncorrected errors, log corrected errors
93          * 2: SIGBUS or log uncorrected errors (if possible), log corr. errors
94          * 3: never panic or SIGBUS, log all errors (for testing only)
95          */
96         .tolerant = 1,
97         .monarch_timeout = -1
98 };
99
100 static DEFINE_PER_CPU(struct mce, mces_seen);
101 static unsigned long mce_need_notify;
102 static int cpu_missing;
103
104 /*
105  * MCA banks polled by the period polling timer for corrected events.
106  * With Intel CMCI, this only has MCA banks which do not support CMCI (if any).
107  */
108 DEFINE_PER_CPU(mce_banks_t, mce_poll_banks) = {
109         [0 ... BITS_TO_LONGS(MAX_NR_BANKS)-1] = ~0UL
110 };
111
112 /*
113  * MCA banks controlled through firmware first for corrected errors.
114  * This is a global list of banks for which we won't enable CMCI and we
115  * won't poll. Firmware controls these banks and is responsible for
116  * reporting corrected errors through GHES. Uncorrected/recoverable
117  * errors are still notified through a machine check.
118  */
119 mce_banks_t mce_banks_ce_disabled;
120
121 static struct work_struct mce_work;
122 static struct irq_work mce_irq_work;
123
124 static void (*quirk_no_way_out)(int bank, struct mce *m, struct pt_regs *regs);
125
126 /*
127  * CPU/chipset specific EDAC code can register a notifier call here to print
128  * MCE errors in a human-readable form.
129  */
130 BLOCKING_NOTIFIER_HEAD(x86_mce_decoder_chain);
131
132 /* Do initial initialization of a struct mce */
133 noinstr void mce_setup(struct mce *m)
134 {
135         memset(m, 0, sizeof(struct mce));
136         m->cpu = m->extcpu = smp_processor_id();
137         /* need the internal __ version to avoid deadlocks */
138         m->time = __ktime_get_real_seconds();
139         m->cpuvendor = boot_cpu_data.x86_vendor;
140         m->cpuid = cpuid_eax(1);
141         m->socketid = cpu_data(m->extcpu).phys_proc_id;
142         m->apicid = cpu_data(m->extcpu).initial_apicid;
143         m->mcgcap = __rdmsr(MSR_IA32_MCG_CAP);
144
145         if (this_cpu_has(X86_FEATURE_INTEL_PPIN))
146                 m->ppin = __rdmsr(MSR_PPIN);
147         else if (this_cpu_has(X86_FEATURE_AMD_PPIN))
148                 m->ppin = __rdmsr(MSR_AMD_PPIN);
149
150         m->microcode = boot_cpu_data.microcode;
151 }
152
153 DEFINE_PER_CPU(struct mce, injectm);
154 EXPORT_PER_CPU_SYMBOL_GPL(injectm);
155
156 void mce_log(struct mce *m)
157 {
158         if (!mce_gen_pool_add(m))
159                 irq_work_queue(&mce_irq_work);
160 }
161 EXPORT_SYMBOL_GPL(mce_log);
162
163 void mce_register_decode_chain(struct notifier_block *nb)
164 {
165         if (WARN_ON(nb->priority < MCE_PRIO_LOWEST ||
166                     nb->priority > MCE_PRIO_HIGHEST))
167                 return;
168
169         blocking_notifier_chain_register(&x86_mce_decoder_chain, nb);
170 }
171 EXPORT_SYMBOL_GPL(mce_register_decode_chain);
172
173 void mce_unregister_decode_chain(struct notifier_block *nb)
174 {
175         blocking_notifier_chain_unregister(&x86_mce_decoder_chain, nb);
176 }
177 EXPORT_SYMBOL_GPL(mce_unregister_decode_chain);
178
179 static inline u32 ctl_reg(int bank)
180 {
181         return MSR_IA32_MCx_CTL(bank);
182 }
183
184 static inline u32 status_reg(int bank)
185 {
186         return MSR_IA32_MCx_STATUS(bank);
187 }
188
189 static inline u32 addr_reg(int bank)
190 {
191         return MSR_IA32_MCx_ADDR(bank);
192 }
193
194 static inline u32 misc_reg(int bank)
195 {
196         return MSR_IA32_MCx_MISC(bank);
197 }
198
199 static inline u32 smca_ctl_reg(int bank)
200 {
201         return MSR_AMD64_SMCA_MCx_CTL(bank);
202 }
203
204 static inline u32 smca_status_reg(int bank)
205 {
206         return MSR_AMD64_SMCA_MCx_STATUS(bank);
207 }
208
209 static inline u32 smca_addr_reg(int bank)
210 {
211         return MSR_AMD64_SMCA_MCx_ADDR(bank);
212 }
213
214 static inline u32 smca_misc_reg(int bank)
215 {
216         return MSR_AMD64_SMCA_MCx_MISC(bank);
217 }
218
219 struct mca_msr_regs msr_ops = {
220         .ctl    = ctl_reg,
221         .status = status_reg,
222         .addr   = addr_reg,
223         .misc   = misc_reg
224 };
225
226 static void __print_mce(struct mce *m)
227 {
228         pr_emerg(HW_ERR "CPU %d: Machine Check%s: %Lx Bank %d: %016Lx\n",
229                  m->extcpu,
230                  (m->mcgstatus & MCG_STATUS_MCIP ? " Exception" : ""),
231                  m->mcgstatus, m->bank, m->status);
232
233         if (m->ip) {
234                 pr_emerg(HW_ERR "RIP%s %02x:<%016Lx> ",
235                         !(m->mcgstatus & MCG_STATUS_EIPV) ? " !INEXACT!" : "",
236                         m->cs, m->ip);
237
238                 if (m->cs == __KERNEL_CS)
239                         pr_cont("{%pS}", (void *)(unsigned long)m->ip);
240                 pr_cont("\n");
241         }
242
243         pr_emerg(HW_ERR "TSC %llx ", m->tsc);
244         if (m->addr)
245                 pr_cont("ADDR %llx ", m->addr);
246         if (m->misc)
247                 pr_cont("MISC %llx ", m->misc);
248         if (m->ppin)
249                 pr_cont("PPIN %llx ", m->ppin);
250
251         if (mce_flags.smca) {
252                 if (m->synd)
253                         pr_cont("SYND %llx ", m->synd);
254                 if (m->ipid)
255                         pr_cont("IPID %llx ", m->ipid);
256         }
257
258         pr_cont("\n");
259
260         /*
261          * Note this output is parsed by external tools and old fields
262          * should not be changed.
263          */
264         pr_emerg(HW_ERR "PROCESSOR %u:%x TIME %llu SOCKET %u APIC %x microcode %x\n",
265                 m->cpuvendor, m->cpuid, m->time, m->socketid, m->apicid,
266                 m->microcode);
267 }
268
269 static void print_mce(struct mce *m)
270 {
271         __print_mce(m);
272
273         if (m->cpuvendor != X86_VENDOR_AMD && m->cpuvendor != X86_VENDOR_HYGON)
274                 pr_emerg_ratelimited(HW_ERR "Run the above through 'mcelog --ascii'\n");
275 }
276
277 #define PANIC_TIMEOUT 5 /* 5 seconds */
278
279 static atomic_t mce_panicked;
280
281 static int fake_panic;
282 static atomic_t mce_fake_panicked;
283
284 /* Panic in progress. Enable interrupts and wait for final IPI */
285 static void wait_for_panic(void)
286 {
287         long timeout = PANIC_TIMEOUT*USEC_PER_SEC;
288
289         preempt_disable();
290         local_irq_enable();
291         while (timeout-- > 0)
292                 udelay(1);
293         if (panic_timeout == 0)
294                 panic_timeout = mca_cfg.panic_timeout;
295         panic("Panicing machine check CPU died");
296 }
297
298 static noinstr void mce_panic(const char *msg, struct mce *final, char *exp)
299 {
300         struct llist_node *pending;
301         struct mce_evt_llist *l;
302         int apei_err = 0;
303
304         /*
305          * Allow instrumentation around external facilities usage. Not that it
306          * matters a whole lot since the machine is going to panic anyway.
307          */
308         instrumentation_begin();
309
310         if (!fake_panic) {
311                 /*
312                  * Make sure only one CPU runs in machine check panic
313                  */
314                 if (atomic_inc_return(&mce_panicked) > 1)
315                         wait_for_panic();
316                 barrier();
317
318                 bust_spinlocks(1);
319                 console_verbose();
320         } else {
321                 /* Don't log too much for fake panic */
322                 if (atomic_inc_return(&mce_fake_panicked) > 1)
323                         goto out;
324         }
325         pending = mce_gen_pool_prepare_records();
326         /* First print corrected ones that are still unlogged */
327         llist_for_each_entry(l, pending, llnode) {
328                 struct mce *m = &l->mce;
329                 if (!(m->status & MCI_STATUS_UC)) {
330                         print_mce(m);
331                         if (!apei_err)
332                                 apei_err = apei_write_mce(m);
333                 }
334         }
335         /* Now print uncorrected but with the final one last */
336         llist_for_each_entry(l, pending, llnode) {
337                 struct mce *m = &l->mce;
338                 if (!(m->status & MCI_STATUS_UC))
339                         continue;
340                 if (!final || mce_cmp(m, final)) {
341                         print_mce(m);
342                         if (!apei_err)
343                                 apei_err = apei_write_mce(m);
344                 }
345         }
346         if (final) {
347                 print_mce(final);
348                 if (!apei_err)
349                         apei_err = apei_write_mce(final);
350         }
351         if (cpu_missing)
352                 pr_emerg(HW_ERR "Some CPUs didn't answer in synchronization\n");
353         if (exp)
354                 pr_emerg(HW_ERR "Machine check: %s\n", exp);
355         if (!fake_panic) {
356                 if (panic_timeout == 0)
357                         panic_timeout = mca_cfg.panic_timeout;
358                 panic(msg);
359         } else
360                 pr_emerg(HW_ERR "Fake kernel panic: %s\n", msg);
361
362 out:
363         instrumentation_end();
364 }
365
366 /* Support code for software error injection */
367
368 static int msr_to_offset(u32 msr)
369 {
370         unsigned bank = __this_cpu_read(injectm.bank);
371
372         if (msr == mca_cfg.rip_msr)
373                 return offsetof(struct mce, ip);
374         if (msr == msr_ops.status(bank))
375                 return offsetof(struct mce, status);
376         if (msr == msr_ops.addr(bank))
377                 return offsetof(struct mce, addr);
378         if (msr == msr_ops.misc(bank))
379                 return offsetof(struct mce, misc);
380         if (msr == MSR_IA32_MCG_STATUS)
381                 return offsetof(struct mce, mcgstatus);
382         return -1;
383 }
384
385 void ex_handler_msr_mce(struct pt_regs *regs, bool wrmsr)
386 {
387         if (wrmsr) {
388                 pr_emerg("MSR access error: WRMSR to 0x%x (tried to write 0x%08x%08x) at rIP: 0x%lx (%pS)\n",
389                          (unsigned int)regs->cx, (unsigned int)regs->dx, (unsigned int)regs->ax,
390                          regs->ip, (void *)regs->ip);
391         } else {
392                 pr_emerg("MSR access error: RDMSR from 0x%x at rIP: 0x%lx (%pS)\n",
393                          (unsigned int)regs->cx, regs->ip, (void *)regs->ip);
394         }
395
396         show_stack_regs(regs);
397
398         panic("MCA architectural violation!\n");
399
400         while (true)
401                 cpu_relax();
402 }
403
404 /* MSR access wrappers used for error injection */
405 static noinstr u64 mce_rdmsrl(u32 msr)
406 {
407         DECLARE_ARGS(val, low, high);
408
409         if (__this_cpu_read(injectm.finished)) {
410                 int offset;
411                 u64 ret;
412
413                 instrumentation_begin();
414
415                 offset = msr_to_offset(msr);
416                 if (offset < 0)
417                         ret = 0;
418                 else
419                         ret = *(u64 *)((char *)this_cpu_ptr(&injectm) + offset);
420
421                 instrumentation_end();
422
423                 return ret;
424         }
425
426         /*
427          * RDMSR on MCA MSRs should not fault. If they do, this is very much an
428          * architectural violation and needs to be reported to hw vendor. Panic
429          * the box to not allow any further progress.
430          */
431         asm volatile("1: rdmsr\n"
432                      "2:\n"
433                      _ASM_EXTABLE_TYPE(1b, 2b, EX_TYPE_RDMSR_IN_MCE)
434                      : EAX_EDX_RET(val, low, high) : "c" (msr));
435
436
437         return EAX_EDX_VAL(val, low, high);
438 }
439
440 static noinstr void mce_wrmsrl(u32 msr, u64 v)
441 {
442         u32 low, high;
443
444         if (__this_cpu_read(injectm.finished)) {
445                 int offset;
446
447                 instrumentation_begin();
448
449                 offset = msr_to_offset(msr);
450                 if (offset >= 0)
451                         *(u64 *)((char *)this_cpu_ptr(&injectm) + offset) = v;
452
453                 instrumentation_end();
454
455                 return;
456         }
457
458         low  = (u32)v;
459         high = (u32)(v >> 32);
460
461         /* See comment in mce_rdmsrl() */
462         asm volatile("1: wrmsr\n"
463                      "2:\n"
464                      _ASM_EXTABLE_TYPE(1b, 2b, EX_TYPE_WRMSR_IN_MCE)
465                      : : "c" (msr), "a"(low), "d" (high) : "memory");
466 }
467
468 /*
469  * Collect all global (w.r.t. this processor) status about this machine
470  * check into our "mce" struct so that we can use it later to assess
471  * the severity of the problem as we read per-bank specific details.
472  */
473 static inline void mce_gather_info(struct mce *m, struct pt_regs *regs)
474 {
475         mce_setup(m);
476
477         m->mcgstatus = mce_rdmsrl(MSR_IA32_MCG_STATUS);
478         if (regs) {
479                 /*
480                  * Get the address of the instruction at the time of
481                  * the machine check error.
482                  */
483                 if (m->mcgstatus & (MCG_STATUS_RIPV|MCG_STATUS_EIPV)) {
484                         m->ip = regs->ip;
485                         m->cs = regs->cs;
486
487                         /*
488                          * When in VM86 mode make the cs look like ring 3
489                          * always. This is a lie, but it's better than passing
490                          * the additional vm86 bit around everywhere.
491                          */
492                         if (v8086_mode(regs))
493                                 m->cs |= 3;
494                 }
495                 /* Use accurate RIP reporting if available. */
496                 if (mca_cfg.rip_msr)
497                         m->ip = mce_rdmsrl(mca_cfg.rip_msr);
498         }
499 }
500
501 int mce_available(struct cpuinfo_x86 *c)
502 {
503         if (mca_cfg.disabled)
504                 return 0;
505         return cpu_has(c, X86_FEATURE_MCE) && cpu_has(c, X86_FEATURE_MCA);
506 }
507
508 static void mce_schedule_work(void)
509 {
510         if (!mce_gen_pool_empty())
511                 schedule_work(&mce_work);
512 }
513
514 static void mce_irq_work_cb(struct irq_work *entry)
515 {
516         mce_schedule_work();
517 }
518
519 /*
520  * Check if the address reported by the CPU is in a format we can parse.
521  * It would be possible to add code for most other cases, but all would
522  * be somewhat complicated (e.g. segment offset would require an instruction
523  * parser). So only support physical addresses up to page granularity for now.
524  */
525 int mce_usable_address(struct mce *m)
526 {
527         if (!(m->status & MCI_STATUS_ADDRV))
528                 return 0;
529
530         /* Checks after this one are Intel/Zhaoxin-specific: */
531         if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL &&
532             boot_cpu_data.x86_vendor != X86_VENDOR_ZHAOXIN)
533                 return 1;
534
535         if (!(m->status & MCI_STATUS_MISCV))
536                 return 0;
537
538         if (MCI_MISC_ADDR_LSB(m->misc) > PAGE_SHIFT)
539                 return 0;
540
541         if (MCI_MISC_ADDR_MODE(m->misc) != MCI_MISC_ADDR_PHYS)
542                 return 0;
543
544         return 1;
545 }
546 EXPORT_SYMBOL_GPL(mce_usable_address);
547
548 bool mce_is_memory_error(struct mce *m)
549 {
550         switch (m->cpuvendor) {
551         case X86_VENDOR_AMD:
552         case X86_VENDOR_HYGON:
553                 return amd_mce_is_memory_error(m);
554
555         case X86_VENDOR_INTEL:
556         case X86_VENDOR_ZHAOXIN:
557                 /*
558                  * Intel SDM Volume 3B - 15.9.2 Compound Error Codes
559                  *
560                  * Bit 7 of the MCACOD field of IA32_MCi_STATUS is used for
561                  * indicating a memory error. Bit 8 is used for indicating a
562                  * cache hierarchy error. The combination of bit 2 and bit 3
563                  * is used for indicating a `generic' cache hierarchy error
564                  * But we can't just blindly check the above bits, because if
565                  * bit 11 is set, then it is a bus/interconnect error - and
566                  * either way the above bits just gives more detail on what
567                  * bus/interconnect error happened. Note that bit 12 can be
568                  * ignored, as it's the "filter" bit.
569                  */
570                 return (m->status & 0xef80) == BIT(7) ||
571                        (m->status & 0xef00) == BIT(8) ||
572                        (m->status & 0xeffc) == 0xc;
573
574         default:
575                 return false;
576         }
577 }
578 EXPORT_SYMBOL_GPL(mce_is_memory_error);
579
580 static bool whole_page(struct mce *m)
581 {
582         if (!mca_cfg.ser || !(m->status & MCI_STATUS_MISCV))
583                 return true;
584
585         return MCI_MISC_ADDR_LSB(m->misc) >= PAGE_SHIFT;
586 }
587
588 bool mce_is_correctable(struct mce *m)
589 {
590         if (m->cpuvendor == X86_VENDOR_AMD && m->status & MCI_STATUS_DEFERRED)
591                 return false;
592
593         if (m->cpuvendor == X86_VENDOR_HYGON && m->status & MCI_STATUS_DEFERRED)
594                 return false;
595
596         if (m->status & MCI_STATUS_UC)
597                 return false;
598
599         return true;
600 }
601 EXPORT_SYMBOL_GPL(mce_is_correctable);
602
603 static int mce_early_notifier(struct notifier_block *nb, unsigned long val,
604                               void *data)
605 {
606         struct mce *m = (struct mce *)data;
607
608         if (!m)
609                 return NOTIFY_DONE;
610
611         /* Emit the trace record: */
612         trace_mce_record(m);
613
614         set_bit(0, &mce_need_notify);
615
616         mce_notify_irq();
617
618         return NOTIFY_DONE;
619 }
620
621 static struct notifier_block early_nb = {
622         .notifier_call  = mce_early_notifier,
623         .priority       = MCE_PRIO_EARLY,
624 };
625
626 static int uc_decode_notifier(struct notifier_block *nb, unsigned long val,
627                               void *data)
628 {
629         struct mce *mce = (struct mce *)data;
630         unsigned long pfn;
631
632         if (!mce || !mce_usable_address(mce))
633                 return NOTIFY_DONE;
634
635         if (mce->severity != MCE_AO_SEVERITY &&
636             mce->severity != MCE_DEFERRED_SEVERITY)
637                 return NOTIFY_DONE;
638
639         pfn = mce->addr >> PAGE_SHIFT;
640         if (!memory_failure(pfn, 0)) {
641                 set_mce_nospec(pfn, whole_page(mce));
642                 mce->kflags |= MCE_HANDLED_UC;
643         }
644
645         return NOTIFY_OK;
646 }
647
648 static struct notifier_block mce_uc_nb = {
649         .notifier_call  = uc_decode_notifier,
650         .priority       = MCE_PRIO_UC,
651 };
652
653 static int mce_default_notifier(struct notifier_block *nb, unsigned long val,
654                                 void *data)
655 {
656         struct mce *m = (struct mce *)data;
657
658         if (!m)
659                 return NOTIFY_DONE;
660
661         if (mca_cfg.print_all || !m->kflags)
662                 __print_mce(m);
663
664         return NOTIFY_DONE;
665 }
666
667 static struct notifier_block mce_default_nb = {
668         .notifier_call  = mce_default_notifier,
669         /* lowest prio, we want it to run last. */
670         .priority       = MCE_PRIO_LOWEST,
671 };
672
673 /*
674  * Read ADDR and MISC registers.
675  */
676 static noinstr void mce_read_aux(struct mce *m, int i)
677 {
678         if (m->status & MCI_STATUS_MISCV)
679                 m->misc = mce_rdmsrl(msr_ops.misc(i));
680
681         if (m->status & MCI_STATUS_ADDRV) {
682                 m->addr = mce_rdmsrl(msr_ops.addr(i));
683
684                 /*
685                  * Mask the reported address by the reported granularity.
686                  */
687                 if (mca_cfg.ser && (m->status & MCI_STATUS_MISCV)) {
688                         u8 shift = MCI_MISC_ADDR_LSB(m->misc);
689                         m->addr >>= shift;
690                         m->addr <<= shift;
691                 }
692
693                 /*
694                  * Extract [55:<lsb>] where lsb is the least significant
695                  * *valid* bit of the address bits.
696                  */
697                 if (mce_flags.smca) {
698                         u8 lsb = (m->addr >> 56) & 0x3f;
699
700                         m->addr &= GENMASK_ULL(55, lsb);
701                 }
702         }
703
704         if (mce_flags.smca) {
705                 m->ipid = mce_rdmsrl(MSR_AMD64_SMCA_MCx_IPID(i));
706
707                 if (m->status & MCI_STATUS_SYNDV)
708                         m->synd = mce_rdmsrl(MSR_AMD64_SMCA_MCx_SYND(i));
709         }
710 }
711
712 DEFINE_PER_CPU(unsigned, mce_poll_count);
713
714 /*
715  * Poll for corrected events or events that happened before reset.
716  * Those are just logged through /dev/mcelog.
717  *
718  * This is executed in standard interrupt context.
719  *
720  * Note: spec recommends to panic for fatal unsignalled
721  * errors here. However this would be quite problematic --
722  * we would need to reimplement the Monarch handling and
723  * it would mess up the exclusion between exception handler
724  * and poll handler -- * so we skip this for now.
725  * These cases should not happen anyways, or only when the CPU
726  * is already totally * confused. In this case it's likely it will
727  * not fully execute the machine check handler either.
728  */
729 bool machine_check_poll(enum mcp_flags flags, mce_banks_t *b)
730 {
731         struct mce_bank *mce_banks = this_cpu_ptr(mce_banks_array);
732         bool error_seen = false;
733         struct mce m;
734         int i;
735
736         this_cpu_inc(mce_poll_count);
737
738         mce_gather_info(&m, NULL);
739
740         if (flags & MCP_TIMESTAMP)
741                 m.tsc = rdtsc();
742
743         for (i = 0; i < this_cpu_read(mce_num_banks); i++) {
744                 if (!mce_banks[i].ctl || !test_bit(i, *b))
745                         continue;
746
747                 m.misc = 0;
748                 m.addr = 0;
749                 m.bank = i;
750
751                 barrier();
752                 m.status = mce_rdmsrl(msr_ops.status(i));
753
754                 /* If this entry is not valid, ignore it */
755                 if (!(m.status & MCI_STATUS_VAL))
756                         continue;
757
758                 /*
759                  * If we are logging everything (at CPU online) or this
760                  * is a corrected error, then we must log it.
761                  */
762                 if ((flags & MCP_UC) || !(m.status & MCI_STATUS_UC))
763                         goto log_it;
764
765                 /*
766                  * Newer Intel systems that support software error
767                  * recovery need to make additional checks. Other
768                  * CPUs should skip over uncorrected errors, but log
769                  * everything else.
770                  */
771                 if (!mca_cfg.ser) {
772                         if (m.status & MCI_STATUS_UC)
773                                 continue;
774                         goto log_it;
775                 }
776
777                 /* Log "not enabled" (speculative) errors */
778                 if (!(m.status & MCI_STATUS_EN))
779                         goto log_it;
780
781                 /*
782                  * Log UCNA (SDM: 15.6.3 "UCR Error Classification")
783                  * UC == 1 && PCC == 0 && S == 0
784                  */
785                 if (!(m.status & MCI_STATUS_PCC) && !(m.status & MCI_STATUS_S))
786                         goto log_it;
787
788                 /*
789                  * Skip anything else. Presumption is that our read of this
790                  * bank is racing with a machine check. Leave the log alone
791                  * for do_machine_check() to deal with it.
792                  */
793                 continue;
794
795 log_it:
796                 error_seen = true;
797
798                 if (flags & MCP_DONTLOG)
799                         goto clear_it;
800
801                 mce_read_aux(&m, i);
802                 m.severity = mce_severity(&m, NULL, mca_cfg.tolerant, NULL, false);
803                 /*
804                  * Don't get the IP here because it's unlikely to
805                  * have anything to do with the actual error location.
806                  */
807
808                 if (mca_cfg.dont_log_ce && !mce_usable_address(&m))
809                         goto clear_it;
810
811                 if (flags & MCP_QUEUE_LOG)
812                         mce_gen_pool_add(&m);
813                 else
814                         mce_log(&m);
815
816 clear_it:
817                 /*
818                  * Clear state for this bank.
819                  */
820                 mce_wrmsrl(msr_ops.status(i), 0);
821         }
822
823         /*
824          * Don't clear MCG_STATUS here because it's only defined for
825          * exceptions.
826          */
827
828         sync_core();
829
830         return error_seen;
831 }
832 EXPORT_SYMBOL_GPL(machine_check_poll);
833
834 /*
835  * Do a quick check if any of the events requires a panic.
836  * This decides if we keep the events around or clear them.
837  */
838 static int mce_no_way_out(struct mce *m, char **msg, unsigned long *validp,
839                           struct pt_regs *regs)
840 {
841         char *tmp = *msg;
842         int i;
843
844         for (i = 0; i < this_cpu_read(mce_num_banks); i++) {
845                 m->status = mce_rdmsrl(msr_ops.status(i));
846                 if (!(m->status & MCI_STATUS_VAL))
847                         continue;
848
849                 __set_bit(i, validp);
850                 if (quirk_no_way_out)
851                         quirk_no_way_out(i, m, regs);
852
853                 m->bank = i;
854                 if (mce_severity(m, regs, mca_cfg.tolerant, &tmp, true) >= MCE_PANIC_SEVERITY) {
855                         mce_read_aux(m, i);
856                         *msg = tmp;
857                         return 1;
858                 }
859         }
860         return 0;
861 }
862
863 /*
864  * Variable to establish order between CPUs while scanning.
865  * Each CPU spins initially until executing is equal its number.
866  */
867 static atomic_t mce_executing;
868
869 /*
870  * Defines order of CPUs on entry. First CPU becomes Monarch.
871  */
872 static atomic_t mce_callin;
873
874 /*
875  * Track which CPUs entered the MCA broadcast synchronization and which not in
876  * order to print holdouts.
877  */
878 static cpumask_t mce_missing_cpus = CPU_MASK_ALL;
879
880 /*
881  * Check if a timeout waiting for other CPUs happened.
882  */
883 static int mce_timed_out(u64 *t, const char *msg)
884 {
885         /*
886          * The others already did panic for some reason.
887          * Bail out like in a timeout.
888          * rmb() to tell the compiler that system_state
889          * might have been modified by someone else.
890          */
891         rmb();
892         if (atomic_read(&mce_panicked))
893                 wait_for_panic();
894         if (!mca_cfg.monarch_timeout)
895                 goto out;
896         if ((s64)*t < SPINUNIT) {
897                 if (mca_cfg.tolerant <= 1) {
898                         if (cpumask_and(&mce_missing_cpus, cpu_online_mask, &mce_missing_cpus))
899                                 pr_emerg("CPUs not responding to MCE broadcast (may include false positives): %*pbl\n",
900                                          cpumask_pr_args(&mce_missing_cpus));
901                         mce_panic(msg, NULL, NULL);
902                 }
903                 cpu_missing = 1;
904                 return 1;
905         }
906         *t -= SPINUNIT;
907 out:
908         touch_nmi_watchdog();
909         return 0;
910 }
911
912 /*
913  * The Monarch's reign.  The Monarch is the CPU who entered
914  * the machine check handler first. It waits for the others to
915  * raise the exception too and then grades them. When any
916  * error is fatal panic. Only then let the others continue.
917  *
918  * The other CPUs entering the MCE handler will be controlled by the
919  * Monarch. They are called Subjects.
920  *
921  * This way we prevent any potential data corruption in a unrecoverable case
922  * and also makes sure always all CPU's errors are examined.
923  *
924  * Also this detects the case of a machine check event coming from outer
925  * space (not detected by any CPUs) In this case some external agent wants
926  * us to shut down, so panic too.
927  *
928  * The other CPUs might still decide to panic if the handler happens
929  * in a unrecoverable place, but in this case the system is in a semi-stable
930  * state and won't corrupt anything by itself. It's ok to let the others
931  * continue for a bit first.
932  *
933  * All the spin loops have timeouts; when a timeout happens a CPU
934  * typically elects itself to be Monarch.
935  */
936 static void mce_reign(void)
937 {
938         int cpu;
939         struct mce *m = NULL;
940         int global_worst = 0;
941         char *msg = NULL;
942
943         /*
944          * This CPU is the Monarch and the other CPUs have run
945          * through their handlers.
946          * Grade the severity of the errors of all the CPUs.
947          */
948         for_each_possible_cpu(cpu) {
949                 struct mce *mtmp = &per_cpu(mces_seen, cpu);
950
951                 if (mtmp->severity > global_worst) {
952                         global_worst = mtmp->severity;
953                         m = &per_cpu(mces_seen, cpu);
954                 }
955         }
956
957         /*
958          * Cannot recover? Panic here then.
959          * This dumps all the mces in the log buffer and stops the
960          * other CPUs.
961          */
962         if (m && global_worst >= MCE_PANIC_SEVERITY && mca_cfg.tolerant < 3) {
963                 /* call mce_severity() to get "msg" for panic */
964                 mce_severity(m, NULL, mca_cfg.tolerant, &msg, true);
965                 mce_panic("Fatal machine check", m, msg);
966         }
967
968         /*
969          * For UC somewhere we let the CPU who detects it handle it.
970          * Also must let continue the others, otherwise the handling
971          * CPU could deadlock on a lock.
972          */
973
974         /*
975          * No machine check event found. Must be some external
976          * source or one CPU is hung. Panic.
977          */
978         if (global_worst <= MCE_KEEP_SEVERITY && mca_cfg.tolerant < 3)
979                 mce_panic("Fatal machine check from unknown source", NULL, NULL);
980
981         /*
982          * Now clear all the mces_seen so that they don't reappear on
983          * the next mce.
984          */
985         for_each_possible_cpu(cpu)
986                 memset(&per_cpu(mces_seen, cpu), 0, sizeof(struct mce));
987 }
988
989 static atomic_t global_nwo;
990
991 /*
992  * Start of Monarch synchronization. This waits until all CPUs have
993  * entered the exception handler and then determines if any of them
994  * saw a fatal event that requires panic. Then it executes them
995  * in the entry order.
996  * TBD double check parallel CPU hotunplug
997  */
998 static int mce_start(int *no_way_out)
999 {
1000         int order;
1001         int cpus = num_online_cpus();
1002         u64 timeout = (u64)mca_cfg.monarch_timeout * NSEC_PER_USEC;
1003
1004         if (!timeout)
1005                 return -1;
1006
1007         atomic_add(*no_way_out, &global_nwo);
1008         /*
1009          * Rely on the implied barrier below, such that global_nwo
1010          * is updated before mce_callin.
1011          */
1012         order = atomic_inc_return(&mce_callin);
1013         cpumask_clear_cpu(smp_processor_id(), &mce_missing_cpus);
1014
1015         /*
1016          * Wait for everyone.
1017          */
1018         while (atomic_read(&mce_callin) != cpus) {
1019                 if (mce_timed_out(&timeout,
1020                                   "Timeout: Not all CPUs entered broadcast exception handler")) {
1021                         atomic_set(&global_nwo, 0);
1022                         return -1;
1023                 }
1024                 ndelay(SPINUNIT);
1025         }
1026
1027         /*
1028          * mce_callin should be read before global_nwo
1029          */
1030         smp_rmb();
1031
1032         if (order == 1) {
1033                 /*
1034                  * Monarch: Starts executing now, the others wait.
1035                  */
1036                 atomic_set(&mce_executing, 1);
1037         } else {
1038                 /*
1039                  * Subject: Now start the scanning loop one by one in
1040                  * the original callin order.
1041                  * This way when there are any shared banks it will be
1042                  * only seen by one CPU before cleared, avoiding duplicates.
1043                  */
1044                 while (atomic_read(&mce_executing) < order) {
1045                         if (mce_timed_out(&timeout,
1046                                           "Timeout: Subject CPUs unable to finish machine check processing")) {
1047                                 atomic_set(&global_nwo, 0);
1048                                 return -1;
1049                         }
1050                         ndelay(SPINUNIT);
1051                 }
1052         }
1053
1054         /*
1055          * Cache the global no_way_out state.
1056          */
1057         *no_way_out = atomic_read(&global_nwo);
1058
1059         return order;
1060 }
1061
1062 /*
1063  * Synchronize between CPUs after main scanning loop.
1064  * This invokes the bulk of the Monarch processing.
1065  */
1066 static noinstr int mce_end(int order)
1067 {
1068         u64 timeout = (u64)mca_cfg.monarch_timeout * NSEC_PER_USEC;
1069         int ret = -1;
1070
1071         /* Allow instrumentation around external facilities. */
1072         instrumentation_begin();
1073
1074         if (!timeout)
1075                 goto reset;
1076         if (order < 0)
1077                 goto reset;
1078
1079         /*
1080          * Allow others to run.
1081          */
1082         atomic_inc(&mce_executing);
1083
1084         if (order == 1) {
1085                 /* CHECKME: Can this race with a parallel hotplug? */
1086                 int cpus = num_online_cpus();
1087
1088                 /*
1089                  * Monarch: Wait for everyone to go through their scanning
1090                  * loops.
1091                  */
1092                 while (atomic_read(&mce_executing) <= cpus) {
1093                         if (mce_timed_out(&timeout,
1094                                           "Timeout: Monarch CPU unable to finish machine check processing"))
1095                                 goto reset;
1096                         ndelay(SPINUNIT);
1097                 }
1098
1099                 mce_reign();
1100                 barrier();
1101                 ret = 0;
1102         } else {
1103                 /*
1104                  * Subject: Wait for Monarch to finish.
1105                  */
1106                 while (atomic_read(&mce_executing) != 0) {
1107                         if (mce_timed_out(&timeout,
1108                                           "Timeout: Monarch CPU did not finish machine check processing"))
1109                                 goto reset;
1110                         ndelay(SPINUNIT);
1111                 }
1112
1113                 /*
1114                  * Don't reset anything. That's done by the Monarch.
1115                  */
1116                 ret = 0;
1117                 goto out;
1118         }
1119
1120         /*
1121          * Reset all global state.
1122          */
1123 reset:
1124         atomic_set(&global_nwo, 0);
1125         atomic_set(&mce_callin, 0);
1126         cpumask_setall(&mce_missing_cpus);
1127         barrier();
1128
1129         /*
1130          * Let others run again.
1131          */
1132         atomic_set(&mce_executing, 0);
1133
1134 out:
1135         instrumentation_end();
1136
1137         return ret;
1138 }
1139
1140 static void mce_clear_state(unsigned long *toclear)
1141 {
1142         int i;
1143
1144         for (i = 0; i < this_cpu_read(mce_num_banks); i++) {
1145                 if (test_bit(i, toclear))
1146                         mce_wrmsrl(msr_ops.status(i), 0);
1147         }
1148 }
1149
1150 /*
1151  * Cases where we avoid rendezvous handler timeout:
1152  * 1) If this CPU is offline.
1153  *
1154  * 2) If crashing_cpu was set, e.g. we're entering kdump and we need to
1155  *  skip those CPUs which remain looping in the 1st kernel - see
1156  *  crash_nmi_callback().
1157  *
1158  * Note: there still is a small window between kexec-ing and the new,
1159  * kdump kernel establishing a new #MC handler where a broadcasted MCE
1160  * might not get handled properly.
1161  */
1162 static noinstr bool mce_check_crashing_cpu(void)
1163 {
1164         unsigned int cpu = smp_processor_id();
1165
1166         if (arch_cpu_is_offline(cpu) ||
1167             (crashing_cpu != -1 && crashing_cpu != cpu)) {
1168                 u64 mcgstatus;
1169
1170                 mcgstatus = __rdmsr(MSR_IA32_MCG_STATUS);
1171
1172                 if (boot_cpu_data.x86_vendor == X86_VENDOR_ZHAOXIN) {
1173                         if (mcgstatus & MCG_STATUS_LMCES)
1174                                 return false;
1175                 }
1176
1177                 if (mcgstatus & MCG_STATUS_RIPV) {
1178                         __wrmsr(MSR_IA32_MCG_STATUS, 0, 0);
1179                         return true;
1180                 }
1181         }
1182         return false;
1183 }
1184
1185 static void __mc_scan_banks(struct mce *m, struct pt_regs *regs, struct mce *final,
1186                             unsigned long *toclear, unsigned long *valid_banks,
1187                             int no_way_out, int *worst)
1188 {
1189         struct mce_bank *mce_banks = this_cpu_ptr(mce_banks_array);
1190         struct mca_config *cfg = &mca_cfg;
1191         int severity, i;
1192
1193         for (i = 0; i < this_cpu_read(mce_num_banks); i++) {
1194                 __clear_bit(i, toclear);
1195                 if (!test_bit(i, valid_banks))
1196                         continue;
1197
1198                 if (!mce_banks[i].ctl)
1199                         continue;
1200
1201                 m->misc = 0;
1202                 m->addr = 0;
1203                 m->bank = i;
1204
1205                 m->status = mce_rdmsrl(msr_ops.status(i));
1206                 if (!(m->status & MCI_STATUS_VAL))
1207                         continue;
1208
1209                 /*
1210                  * Corrected or non-signaled errors are handled by
1211                  * machine_check_poll(). Leave them alone, unless this panics.
1212                  */
1213                 if (!(m->status & (cfg->ser ? MCI_STATUS_S : MCI_STATUS_UC)) &&
1214                         !no_way_out)
1215                         continue;
1216
1217                 /* Set taint even when machine check was not enabled. */
1218                 add_taint(TAINT_MACHINE_CHECK, LOCKDEP_NOW_UNRELIABLE);
1219
1220                 severity = mce_severity(m, regs, cfg->tolerant, NULL, true);
1221
1222                 /*
1223                  * When machine check was for corrected/deferred handler don't
1224                  * touch, unless we're panicking.
1225                  */
1226                 if ((severity == MCE_KEEP_SEVERITY ||
1227                      severity == MCE_UCNA_SEVERITY) && !no_way_out)
1228                         continue;
1229
1230                 __set_bit(i, toclear);
1231
1232                 /* Machine check event was not enabled. Clear, but ignore. */
1233                 if (severity == MCE_NO_SEVERITY)
1234                         continue;
1235
1236                 mce_read_aux(m, i);
1237
1238                 /* assuming valid severity level != 0 */
1239                 m->severity = severity;
1240
1241                 mce_log(m);
1242
1243                 if (severity > *worst) {
1244                         *final = *m;
1245                         *worst = severity;
1246                 }
1247         }
1248
1249         /* mce_clear_state will clear *final, save locally for use later */
1250         *m = *final;
1251 }
1252
1253 static void kill_me_now(struct callback_head *ch)
1254 {
1255         struct task_struct *p = container_of(ch, struct task_struct, mce_kill_me);
1256
1257         p->mce_count = 0;
1258         force_sig(SIGBUS);
1259 }
1260
1261 static void kill_me_maybe(struct callback_head *cb)
1262 {
1263         struct task_struct *p = container_of(cb, struct task_struct, mce_kill_me);
1264         int flags = MF_ACTION_REQUIRED;
1265         int ret;
1266
1267         p->mce_count = 0;
1268         pr_err("Uncorrected hardware memory error in user-access at %llx", p->mce_addr);
1269
1270         if (!p->mce_ripv)
1271                 flags |= MF_MUST_KILL;
1272
1273         ret = memory_failure(p->mce_addr >> PAGE_SHIFT, flags);
1274         if (!ret && !(p->mce_kflags & MCE_IN_KERNEL_COPYIN)) {
1275                 set_mce_nospec(p->mce_addr >> PAGE_SHIFT, p->mce_whole_page);
1276                 sync_core();
1277                 return;
1278         }
1279
1280         /*
1281          * -EHWPOISON from memory_failure() means that it already sent SIGBUS
1282          * to the current process with the proper error info,
1283          * -EOPNOTSUPP means hwpoison_filter() filtered the error event,
1284          *
1285          * In both cases, no further processing is required.
1286          */
1287         if (ret == -EHWPOISON || ret == -EOPNOTSUPP)
1288                 return;
1289
1290         if (p->mce_vaddr != (void __user *)-1l) {
1291                 force_sig_mceerr(BUS_MCEERR_AR, p->mce_vaddr, PAGE_SHIFT);
1292         } else {
1293                 pr_err("Memory error not recovered");
1294                 kill_me_now(cb);
1295         }
1296 }
1297
1298 static void queue_task_work(struct mce *m, char *msg, int kill_current_task)
1299 {
1300         int count = ++current->mce_count;
1301
1302         /* First call, save all the details */
1303         if (count == 1) {
1304                 current->mce_addr = m->addr;
1305                 current->mce_kflags = m->kflags;
1306                 current->mce_ripv = !!(m->mcgstatus & MCG_STATUS_RIPV);
1307                 current->mce_whole_page = whole_page(m);
1308
1309                 if (kill_current_task)
1310                         current->mce_kill_me.func = kill_me_now;
1311                 else
1312                         current->mce_kill_me.func = kill_me_maybe;
1313         }
1314
1315         /* Ten is likely overkill. Don't expect more than two faults before task_work() */
1316         if (count > 10)
1317                 mce_panic("Too many consecutive machine checks while accessing user data", m, msg);
1318
1319         /* Second or later call, make sure page address matches the one from first call */
1320         if (count > 1 && (current->mce_addr >> PAGE_SHIFT) != (m->addr >> PAGE_SHIFT))
1321                 mce_panic("Consecutive machine checks to different user pages", m, msg);
1322
1323         /* Do not call task_work_add() more than once */
1324         if (count > 1)
1325                 return;
1326
1327         task_work_add(current, &current->mce_kill_me, TWA_RESUME);
1328 }
1329
1330 /*
1331  * The actual machine check handler. This only handles real
1332  * exceptions when something got corrupted coming in through int 18.
1333  *
1334  * This is executed in NMI context not subject to normal locking rules. This
1335  * implies that most kernel services cannot be safely used. Don't even
1336  * think about putting a printk in there!
1337  *
1338  * On Intel systems this is entered on all CPUs in parallel through
1339  * MCE broadcast. However some CPUs might be broken beyond repair,
1340  * so be always careful when synchronizing with others.
1341  *
1342  * Tracing and kprobes are disabled: if we interrupted a kernel context
1343  * with IF=1, we need to minimize stack usage.  There are also recursion
1344  * issues: if the machine check was due to a failure of the memory
1345  * backing the user stack, tracing that reads the user stack will cause
1346  * potentially infinite recursion.
1347  */
1348 noinstr void do_machine_check(struct pt_regs *regs)
1349 {
1350         DECLARE_BITMAP(valid_banks, MAX_NR_BANKS);
1351         DECLARE_BITMAP(toclear, MAX_NR_BANKS);
1352         struct mca_config *cfg = &mca_cfg;
1353         struct mce m, *final;
1354         char *msg = NULL;
1355         int worst = 0;
1356
1357         /*
1358          * Establish sequential order between the CPUs entering the machine
1359          * check handler.
1360          */
1361         int order = -1;
1362
1363         /*
1364          * If no_way_out gets set, there is no safe way to recover from this
1365          * MCE.  If mca_cfg.tolerant is cranked up, we'll try anyway.
1366          */
1367         int no_way_out = 0;
1368
1369         /*
1370          * If kill_current_task is not set, there might be a way to recover from this
1371          * error.
1372          */
1373         int kill_current_task = 0;
1374
1375         /*
1376          * MCEs are always local on AMD. Same is determined by MCG_STATUS_LMCES
1377          * on Intel.
1378          */
1379         int lmce = 1;
1380
1381         this_cpu_inc(mce_exception_count);
1382
1383         mce_gather_info(&m, regs);
1384         m.tsc = rdtsc();
1385
1386         final = this_cpu_ptr(&mces_seen);
1387         *final = m;
1388
1389         memset(valid_banks, 0, sizeof(valid_banks));
1390         no_way_out = mce_no_way_out(&m, &msg, valid_banks, regs);
1391
1392         barrier();
1393
1394         /*
1395          * When no restart IP might need to kill or panic.
1396          * Assume the worst for now, but if we find the
1397          * severity is MCE_AR_SEVERITY we have other options.
1398          */
1399         if (!(m.mcgstatus & MCG_STATUS_RIPV))
1400                 kill_current_task = (cfg->tolerant == 3) ? 0 : 1;
1401         /*
1402          * Check if this MCE is signaled to only this logical processor,
1403          * on Intel, Zhaoxin only.
1404          */
1405         if (m.cpuvendor == X86_VENDOR_INTEL ||
1406             m.cpuvendor == X86_VENDOR_ZHAOXIN)
1407                 lmce = m.mcgstatus & MCG_STATUS_LMCES;
1408
1409         /*
1410          * Local machine check may already know that we have to panic.
1411          * Broadcast machine check begins rendezvous in mce_start()
1412          * Go through all banks in exclusion of the other CPUs. This way we
1413          * don't report duplicated events on shared banks because the first one
1414          * to see it will clear it.
1415          */
1416         if (lmce) {
1417                 if (no_way_out && cfg->tolerant < 3)
1418                         mce_panic("Fatal local machine check", &m, msg);
1419         } else {
1420                 order = mce_start(&no_way_out);
1421         }
1422
1423         __mc_scan_banks(&m, regs, final, toclear, valid_banks, no_way_out, &worst);
1424
1425         if (!no_way_out)
1426                 mce_clear_state(toclear);
1427
1428         /*
1429          * Do most of the synchronization with other CPUs.
1430          * When there's any problem use only local no_way_out state.
1431          */
1432         if (!lmce) {
1433                 if (mce_end(order) < 0) {
1434                         if (!no_way_out)
1435                                 no_way_out = worst >= MCE_PANIC_SEVERITY;
1436
1437                         if (no_way_out && cfg->tolerant < 3)
1438                                 mce_panic("Fatal machine check on current CPU", &m, msg);
1439                 }
1440         } else {
1441                 /*
1442                  * If there was a fatal machine check we should have
1443                  * already called mce_panic earlier in this function.
1444                  * Since we re-read the banks, we might have found
1445                  * something new. Check again to see if we found a
1446                  * fatal error. We call "mce_severity()" again to
1447                  * make sure we have the right "msg".
1448                  */
1449                 if (worst >= MCE_PANIC_SEVERITY && mca_cfg.tolerant < 3) {
1450                         mce_severity(&m, regs, cfg->tolerant, &msg, true);
1451                         mce_panic("Local fatal machine check!", &m, msg);
1452                 }
1453         }
1454
1455         if (worst != MCE_AR_SEVERITY && !kill_current_task)
1456                 goto out;
1457
1458         /*
1459          * Enable instrumentation around the external facilities like
1460          * task_work_add() (via queue_task_work()), fixup_exception() etc.
1461          * For now, that is. Fixing this properly would need a lot more involved
1462          * reorganization.
1463          */
1464         instrumentation_begin();
1465
1466         /* Fault was in user mode and we need to take some action */
1467         if ((m.cs & 3) == 3) {
1468                 /* If this triggers there is no way to recover. Die hard. */
1469                 BUG_ON(!on_thread_stack() || !user_mode(regs));
1470
1471                 queue_task_work(&m, msg, kill_current_task);
1472
1473         } else {
1474                 /*
1475                  * Handle an MCE which has happened in kernel space but from
1476                  * which the kernel can recover: ex_has_fault_handler() has
1477                  * already verified that the rIP at which the error happened is
1478                  * a rIP from which the kernel can recover (by jumping to
1479                  * recovery code specified in _ASM_EXTABLE_FAULT()) and the
1480                  * corresponding exception handler which would do that is the
1481                  * proper one.
1482                  */
1483                 if (m.kflags & MCE_IN_KERNEL_RECOV) {
1484                         if (!fixup_exception(regs, X86_TRAP_MC, 0, 0))
1485                                 mce_panic("Failed kernel mode recovery", &m, msg);
1486                 }
1487
1488                 if (m.kflags & MCE_IN_KERNEL_COPYIN)
1489                         queue_task_work(&m, msg, kill_current_task);
1490         }
1491
1492         instrumentation_end();
1493
1494 out:
1495         mce_wrmsrl(MSR_IA32_MCG_STATUS, 0);
1496 }
1497 EXPORT_SYMBOL_GPL(do_machine_check);
1498
1499 #ifndef CONFIG_MEMORY_FAILURE
1500 int memory_failure(unsigned long pfn, int flags)
1501 {
1502         /* mce_severity() should not hand us an ACTION_REQUIRED error */
1503         BUG_ON(flags & MF_ACTION_REQUIRED);
1504         pr_err("Uncorrected memory error in page 0x%lx ignored\n"
1505                "Rebuild kernel with CONFIG_MEMORY_FAILURE=y for smarter handling\n",
1506                pfn);
1507
1508         return 0;
1509 }
1510 #endif
1511
1512 /*
1513  * Periodic polling timer for "silent" machine check errors.  If the
1514  * poller finds an MCE, poll 2x faster.  When the poller finds no more
1515  * errors, poll 2x slower (up to check_interval seconds).
1516  */
1517 static unsigned long check_interval = INITIAL_CHECK_INTERVAL;
1518
1519 static DEFINE_PER_CPU(unsigned long, mce_next_interval); /* in jiffies */
1520 static DEFINE_PER_CPU(struct timer_list, mce_timer);
1521
1522 static unsigned long mce_adjust_timer_default(unsigned long interval)
1523 {
1524         return interval;
1525 }
1526
1527 static unsigned long (*mce_adjust_timer)(unsigned long interval) = mce_adjust_timer_default;
1528
1529 static void __start_timer(struct timer_list *t, unsigned long interval)
1530 {
1531         unsigned long when = jiffies + interval;
1532         unsigned long flags;
1533
1534         local_irq_save(flags);
1535
1536         if (!timer_pending(t) || time_before(when, t->expires))
1537                 mod_timer(t, round_jiffies(when));
1538
1539         local_irq_restore(flags);
1540 }
1541
1542 static void mce_timer_fn(struct timer_list *t)
1543 {
1544         struct timer_list *cpu_t = this_cpu_ptr(&mce_timer);
1545         unsigned long iv;
1546
1547         WARN_ON(cpu_t != t);
1548
1549         iv = __this_cpu_read(mce_next_interval);
1550
1551         if (mce_available(this_cpu_ptr(&cpu_info))) {
1552                 machine_check_poll(0, this_cpu_ptr(&mce_poll_banks));
1553
1554                 if (mce_intel_cmci_poll()) {
1555                         iv = mce_adjust_timer(iv);
1556                         goto done;
1557                 }
1558         }
1559
1560         /*
1561          * Alert userspace if needed. If we logged an MCE, reduce the polling
1562          * interval, otherwise increase the polling interval.
1563          */
1564         if (mce_notify_irq())
1565                 iv = max(iv / 2, (unsigned long) HZ/100);
1566         else
1567                 iv = min(iv * 2, round_jiffies_relative(check_interval * HZ));
1568
1569 done:
1570         __this_cpu_write(mce_next_interval, iv);
1571         __start_timer(t, iv);
1572 }
1573
1574 /*
1575  * Ensure that the timer is firing in @interval from now.
1576  */
1577 void mce_timer_kick(unsigned long interval)
1578 {
1579         struct timer_list *t = this_cpu_ptr(&mce_timer);
1580         unsigned long iv = __this_cpu_read(mce_next_interval);
1581
1582         __start_timer(t, interval);
1583
1584         if (interval < iv)
1585                 __this_cpu_write(mce_next_interval, interval);
1586 }
1587
1588 /* Must not be called in IRQ context where del_timer_sync() can deadlock */
1589 static void mce_timer_delete_all(void)
1590 {
1591         int cpu;
1592
1593         for_each_online_cpu(cpu)
1594                 del_timer_sync(&per_cpu(mce_timer, cpu));
1595 }
1596
1597 /*
1598  * Notify the user(s) about new machine check events.
1599  * Can be called from interrupt context, but not from machine check/NMI
1600  * context.
1601  */
1602 int mce_notify_irq(void)
1603 {
1604         /* Not more than two messages every minute */
1605         static DEFINE_RATELIMIT_STATE(ratelimit, 60*HZ, 2);
1606
1607         if (test_and_clear_bit(0, &mce_need_notify)) {
1608                 mce_work_trigger();
1609
1610                 if (__ratelimit(&ratelimit))
1611                         pr_info(HW_ERR "Machine check events logged\n");
1612
1613                 return 1;
1614         }
1615         return 0;
1616 }
1617 EXPORT_SYMBOL_GPL(mce_notify_irq);
1618
1619 static void __mcheck_cpu_mce_banks_init(void)
1620 {
1621         struct mce_bank *mce_banks = this_cpu_ptr(mce_banks_array);
1622         u8 n_banks = this_cpu_read(mce_num_banks);
1623         int i;
1624
1625         for (i = 0; i < n_banks; i++) {
1626                 struct mce_bank *b = &mce_banks[i];
1627
1628                 /*
1629                  * Init them all, __mcheck_cpu_apply_quirks() is going to apply
1630                  * the required vendor quirks before
1631                  * __mcheck_cpu_init_clear_banks() does the final bank setup.
1632                  */
1633                 b->ctl = -1ULL;
1634                 b->init = true;
1635         }
1636 }
1637
1638 /*
1639  * Initialize Machine Checks for a CPU.
1640  */
1641 static void __mcheck_cpu_cap_init(void)
1642 {
1643         u64 cap;
1644         u8 b;
1645
1646         rdmsrl(MSR_IA32_MCG_CAP, cap);
1647
1648         b = cap & MCG_BANKCNT_MASK;
1649
1650         if (b > MAX_NR_BANKS) {
1651                 pr_warn("CPU%d: Using only %u machine check banks out of %u\n",
1652                         smp_processor_id(), MAX_NR_BANKS, b);
1653                 b = MAX_NR_BANKS;
1654         }
1655
1656         this_cpu_write(mce_num_banks, b);
1657
1658         __mcheck_cpu_mce_banks_init();
1659
1660         /* Use accurate RIP reporting if available. */
1661         if ((cap & MCG_EXT_P) && MCG_EXT_CNT(cap) >= 9)
1662                 mca_cfg.rip_msr = MSR_IA32_MCG_EIP;
1663
1664         if (cap & MCG_SER_P)
1665                 mca_cfg.ser = 1;
1666 }
1667
1668 static void __mcheck_cpu_init_generic(void)
1669 {
1670         enum mcp_flags m_fl = 0;
1671         mce_banks_t all_banks;
1672         u64 cap;
1673
1674         if (!mca_cfg.bootlog)
1675                 m_fl = MCP_DONTLOG;
1676
1677         /*
1678          * Log the machine checks left over from the previous reset. Log them
1679          * only, do not start processing them. That will happen in mcheck_late_init()
1680          * when all consumers have been registered on the notifier chain.
1681          */
1682         bitmap_fill(all_banks, MAX_NR_BANKS);
1683         machine_check_poll(MCP_UC | MCP_QUEUE_LOG | m_fl, &all_banks);
1684
1685         cr4_set_bits(X86_CR4_MCE);
1686
1687         rdmsrl(MSR_IA32_MCG_CAP, cap);
1688         if (cap & MCG_CTL_P)
1689                 wrmsr(MSR_IA32_MCG_CTL, 0xffffffff, 0xffffffff);
1690 }
1691
1692 static void __mcheck_cpu_init_clear_banks(void)
1693 {
1694         struct mce_bank *mce_banks = this_cpu_ptr(mce_banks_array);
1695         int i;
1696
1697         for (i = 0; i < this_cpu_read(mce_num_banks); i++) {
1698                 struct mce_bank *b = &mce_banks[i];
1699
1700                 if (!b->init)
1701                         continue;
1702                 wrmsrl(msr_ops.ctl(i), b->ctl);
1703                 wrmsrl(msr_ops.status(i), 0);
1704         }
1705 }
1706
1707 /*
1708  * Do a final check to see if there are any unused/RAZ banks.
1709  *
1710  * This must be done after the banks have been initialized and any quirks have
1711  * been applied.
1712  *
1713  * Do not call this from any user-initiated flows, e.g. CPU hotplug or sysfs.
1714  * Otherwise, a user who disables a bank will not be able to re-enable it
1715  * without a system reboot.
1716  */
1717 static void __mcheck_cpu_check_banks(void)
1718 {
1719         struct mce_bank *mce_banks = this_cpu_ptr(mce_banks_array);
1720         u64 msrval;
1721         int i;
1722
1723         for (i = 0; i < this_cpu_read(mce_num_banks); i++) {
1724                 struct mce_bank *b = &mce_banks[i];
1725
1726                 if (!b->init)
1727                         continue;
1728
1729                 rdmsrl(msr_ops.ctl(i), msrval);
1730                 b->init = !!msrval;
1731         }
1732 }
1733
1734 /*
1735  * During IFU recovery Sandy Bridge -EP4S processors set the RIPV and
1736  * EIPV bits in MCG_STATUS to zero on the affected logical processor (SDM
1737  * Vol 3B Table 15-20). But this confuses both the code that determines
1738  * whether the machine check occurred in kernel or user mode, and also
1739  * the severity assessment code. Pretend that EIPV was set, and take the
1740  * ip/cs values from the pt_regs that mce_gather_info() ignored earlier.
1741  */
1742 static void quirk_sandybridge_ifu(int bank, struct mce *m, struct pt_regs *regs)
1743 {
1744         if (bank != 0)
1745                 return;
1746         if ((m->mcgstatus & (MCG_STATUS_EIPV|MCG_STATUS_RIPV)) != 0)
1747                 return;
1748         if ((m->status & (MCI_STATUS_OVER|MCI_STATUS_UC|
1749                           MCI_STATUS_EN|MCI_STATUS_MISCV|MCI_STATUS_ADDRV|
1750                           MCI_STATUS_PCC|MCI_STATUS_S|MCI_STATUS_AR|
1751                           MCACOD)) !=
1752                          (MCI_STATUS_UC|MCI_STATUS_EN|
1753                           MCI_STATUS_MISCV|MCI_STATUS_ADDRV|MCI_STATUS_S|
1754                           MCI_STATUS_AR|MCACOD_INSTR))
1755                 return;
1756
1757         m->mcgstatus |= MCG_STATUS_EIPV;
1758         m->ip = regs->ip;
1759         m->cs = regs->cs;
1760 }
1761
1762 /* Add per CPU specific workarounds here */
1763 static int __mcheck_cpu_apply_quirks(struct cpuinfo_x86 *c)
1764 {
1765         struct mce_bank *mce_banks = this_cpu_ptr(mce_banks_array);
1766         struct mca_config *cfg = &mca_cfg;
1767
1768         if (c->x86_vendor == X86_VENDOR_UNKNOWN) {
1769                 pr_info("unknown CPU type - not enabling MCE support\n");
1770                 return -EOPNOTSUPP;
1771         }
1772
1773         /* This should be disabled by the BIOS, but isn't always */
1774         if (c->x86_vendor == X86_VENDOR_AMD) {
1775                 if (c->x86 == 15 && this_cpu_read(mce_num_banks) > 4) {
1776                         /*
1777                          * disable GART TBL walk error reporting, which
1778                          * trips off incorrectly with the IOMMU & 3ware
1779                          * & Cerberus:
1780                          */
1781                         clear_bit(10, (unsigned long *)&mce_banks[4].ctl);
1782                 }
1783                 if (c->x86 < 0x11 && cfg->bootlog < 0) {
1784                         /*
1785                          * Lots of broken BIOS around that don't clear them
1786                          * by default and leave crap in there. Don't log:
1787                          */
1788                         cfg->bootlog = 0;
1789                 }
1790                 /*
1791                  * Various K7s with broken bank 0 around. Always disable
1792                  * by default.
1793                  */
1794                 if (c->x86 == 6 && this_cpu_read(mce_num_banks) > 0)
1795                         mce_banks[0].ctl = 0;
1796
1797                 /*
1798                  * overflow_recov is supported for F15h Models 00h-0fh
1799                  * even though we don't have a CPUID bit for it.
1800                  */
1801                 if (c->x86 == 0x15 && c->x86_model <= 0xf)
1802                         mce_flags.overflow_recov = 1;
1803
1804         }
1805
1806         if (c->x86_vendor == X86_VENDOR_INTEL) {
1807                 /*
1808                  * SDM documents that on family 6 bank 0 should not be written
1809                  * because it aliases to another special BIOS controlled
1810                  * register.
1811                  * But it's not aliased anymore on model 0x1a+
1812                  * Don't ignore bank 0 completely because there could be a
1813                  * valid event later, merely don't write CTL0.
1814                  */
1815
1816                 if (c->x86 == 6 && c->x86_model < 0x1A && this_cpu_read(mce_num_banks) > 0)
1817                         mce_banks[0].init = false;
1818
1819                 /*
1820                  * All newer Intel systems support MCE broadcasting. Enable
1821                  * synchronization with a one second timeout.
1822                  */
1823                 if ((c->x86 > 6 || (c->x86 == 6 && c->x86_model >= 0xe)) &&
1824                         cfg->monarch_timeout < 0)
1825                         cfg->monarch_timeout = USEC_PER_SEC;
1826
1827                 /*
1828                  * There are also broken BIOSes on some Pentium M and
1829                  * earlier systems:
1830                  */
1831                 if (c->x86 == 6 && c->x86_model <= 13 && cfg->bootlog < 0)
1832                         cfg->bootlog = 0;
1833
1834                 if (c->x86 == 6 && c->x86_model == 45)
1835                         quirk_no_way_out = quirk_sandybridge_ifu;
1836         }
1837
1838         if (c->x86_vendor == X86_VENDOR_ZHAOXIN) {
1839                 /*
1840                  * All newer Zhaoxin CPUs support MCE broadcasting. Enable
1841                  * synchronization with a one second timeout.
1842                  */
1843                 if (c->x86 > 6 || (c->x86_model == 0x19 || c->x86_model == 0x1f)) {
1844                         if (cfg->monarch_timeout < 0)
1845                                 cfg->monarch_timeout = USEC_PER_SEC;
1846                 }
1847         }
1848
1849         if (cfg->monarch_timeout < 0)
1850                 cfg->monarch_timeout = 0;
1851         if (cfg->bootlog != 0)
1852                 cfg->panic_timeout = 30;
1853
1854         return 0;
1855 }
1856
1857 static int __mcheck_cpu_ancient_init(struct cpuinfo_x86 *c)
1858 {
1859         if (c->x86 != 5)
1860                 return 0;
1861
1862         switch (c->x86_vendor) {
1863         case X86_VENDOR_INTEL:
1864                 intel_p5_mcheck_init(c);
1865                 return 1;
1866         case X86_VENDOR_CENTAUR:
1867                 winchip_mcheck_init(c);
1868                 return 1;
1869         default:
1870                 return 0;
1871         }
1872
1873         return 0;
1874 }
1875
1876 /*
1877  * Init basic CPU features needed for early decoding of MCEs.
1878  */
1879 static void __mcheck_cpu_init_early(struct cpuinfo_x86 *c)
1880 {
1881         if (c->x86_vendor == X86_VENDOR_AMD || c->x86_vendor == X86_VENDOR_HYGON) {
1882                 mce_flags.overflow_recov = !!cpu_has(c, X86_FEATURE_OVERFLOW_RECOV);
1883                 mce_flags.succor         = !!cpu_has(c, X86_FEATURE_SUCCOR);
1884                 mce_flags.smca           = !!cpu_has(c, X86_FEATURE_SMCA);
1885                 mce_flags.amd_threshold  = 1;
1886
1887                 if (mce_flags.smca) {
1888                         msr_ops.ctl     = smca_ctl_reg;
1889                         msr_ops.status  = smca_status_reg;
1890                         msr_ops.addr    = smca_addr_reg;
1891                         msr_ops.misc    = smca_misc_reg;
1892                 }
1893         }
1894 }
1895
1896 static void mce_centaur_feature_init(struct cpuinfo_x86 *c)
1897 {
1898         struct mca_config *cfg = &mca_cfg;
1899
1900          /*
1901           * All newer Centaur CPUs support MCE broadcasting. Enable
1902           * synchronization with a one second timeout.
1903           */
1904         if ((c->x86 == 6 && c->x86_model == 0xf && c->x86_stepping >= 0xe) ||
1905              c->x86 > 6) {
1906                 if (cfg->monarch_timeout < 0)
1907                         cfg->monarch_timeout = USEC_PER_SEC;
1908         }
1909 }
1910
1911 static void mce_zhaoxin_feature_init(struct cpuinfo_x86 *c)
1912 {
1913         struct mce_bank *mce_banks = this_cpu_ptr(mce_banks_array);
1914
1915         /*
1916          * These CPUs have MCA bank 8 which reports only one error type called
1917          * SVAD (System View Address Decoder). The reporting of that error is
1918          * controlled by IA32_MC8.CTL.0.
1919          *
1920          * If enabled, prefetching on these CPUs will cause SVAD MCE when
1921          * virtual machines start and result in a system  panic. Always disable
1922          * bank 8 SVAD error by default.
1923          */
1924         if ((c->x86 == 7 && c->x86_model == 0x1b) ||
1925             (c->x86_model == 0x19 || c->x86_model == 0x1f)) {
1926                 if (this_cpu_read(mce_num_banks) > 8)
1927                         mce_banks[8].ctl = 0;
1928         }
1929
1930         intel_init_cmci();
1931         intel_init_lmce();
1932         mce_adjust_timer = cmci_intel_adjust_timer;
1933 }
1934
1935 static void mce_zhaoxin_feature_clear(struct cpuinfo_x86 *c)
1936 {
1937         intel_clear_lmce();
1938 }
1939
1940 static void __mcheck_cpu_init_vendor(struct cpuinfo_x86 *c)
1941 {
1942         switch (c->x86_vendor) {
1943         case X86_VENDOR_INTEL:
1944                 mce_intel_feature_init(c);
1945                 mce_adjust_timer = cmci_intel_adjust_timer;
1946                 break;
1947
1948         case X86_VENDOR_AMD: {
1949                 mce_amd_feature_init(c);
1950                 break;
1951                 }
1952
1953         case X86_VENDOR_HYGON:
1954                 mce_hygon_feature_init(c);
1955                 break;
1956
1957         case X86_VENDOR_CENTAUR:
1958                 mce_centaur_feature_init(c);
1959                 break;
1960
1961         case X86_VENDOR_ZHAOXIN:
1962                 mce_zhaoxin_feature_init(c);
1963                 break;
1964
1965         default:
1966                 break;
1967         }
1968 }
1969
1970 static void __mcheck_cpu_clear_vendor(struct cpuinfo_x86 *c)
1971 {
1972         switch (c->x86_vendor) {
1973         case X86_VENDOR_INTEL:
1974                 mce_intel_feature_clear(c);
1975                 break;
1976
1977         case X86_VENDOR_ZHAOXIN:
1978                 mce_zhaoxin_feature_clear(c);
1979                 break;
1980
1981         default:
1982                 break;
1983         }
1984 }
1985
1986 static void mce_start_timer(struct timer_list *t)
1987 {
1988         unsigned long iv = check_interval * HZ;
1989
1990         if (mca_cfg.ignore_ce || !iv)
1991                 return;
1992
1993         this_cpu_write(mce_next_interval, iv);
1994         __start_timer(t, iv);
1995 }
1996
1997 static void __mcheck_cpu_setup_timer(void)
1998 {
1999         struct timer_list *t = this_cpu_ptr(&mce_timer);
2000
2001         timer_setup(t, mce_timer_fn, TIMER_PINNED);
2002 }
2003
2004 static void __mcheck_cpu_init_timer(void)
2005 {
2006         struct timer_list *t = this_cpu_ptr(&mce_timer);
2007
2008         timer_setup(t, mce_timer_fn, TIMER_PINNED);
2009         mce_start_timer(t);
2010 }
2011
2012 bool filter_mce(struct mce *m)
2013 {
2014         if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
2015                 return amd_filter_mce(m);
2016         if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)
2017                 return intel_filter_mce(m);
2018
2019         return false;
2020 }
2021
2022 /* Handle unconfigured int18 (should never happen) */
2023 static noinstr void unexpected_machine_check(struct pt_regs *regs)
2024 {
2025         instrumentation_begin();
2026         pr_err("CPU#%d: Unexpected int18 (Machine Check)\n",
2027                smp_processor_id());
2028         instrumentation_end();
2029 }
2030
2031 /* Call the installed machine check handler for this CPU setup. */
2032 void (*machine_check_vector)(struct pt_regs *) = unexpected_machine_check;
2033
2034 static __always_inline void exc_machine_check_kernel(struct pt_regs *regs)
2035 {
2036         irqentry_state_t irq_state;
2037
2038         WARN_ON_ONCE(user_mode(regs));
2039
2040         /*
2041          * Only required when from kernel mode. See
2042          * mce_check_crashing_cpu() for details.
2043          */
2044         if (machine_check_vector == do_machine_check &&
2045             mce_check_crashing_cpu())
2046                 return;
2047
2048         irq_state = irqentry_nmi_enter(regs);
2049         /*
2050          * The call targets are marked noinstr, but objtool can't figure
2051          * that out because it's an indirect call. Annotate it.
2052          */
2053         instrumentation_begin();
2054
2055         machine_check_vector(regs);
2056
2057         instrumentation_end();
2058         irqentry_nmi_exit(regs, irq_state);
2059 }
2060
2061 static __always_inline void exc_machine_check_user(struct pt_regs *regs)
2062 {
2063         irqentry_enter_from_user_mode(regs);
2064         instrumentation_begin();
2065
2066         machine_check_vector(regs);
2067
2068         instrumentation_end();
2069         irqentry_exit_to_user_mode(regs);
2070 }
2071
2072 #ifdef CONFIG_X86_64
2073 /* MCE hit kernel mode */
2074 DEFINE_IDTENTRY_MCE(exc_machine_check)
2075 {
2076         unsigned long dr7;
2077
2078         dr7 = local_db_save();
2079         exc_machine_check_kernel(regs);
2080         local_db_restore(dr7);
2081 }
2082
2083 /* The user mode variant. */
2084 DEFINE_IDTENTRY_MCE_USER(exc_machine_check)
2085 {
2086         unsigned long dr7;
2087
2088         dr7 = local_db_save();
2089         exc_machine_check_user(regs);
2090         local_db_restore(dr7);
2091 }
2092 #else
2093 /* 32bit unified entry point */
2094 DEFINE_IDTENTRY_RAW(exc_machine_check)
2095 {
2096         unsigned long dr7;
2097
2098         dr7 = local_db_save();
2099         if (user_mode(regs))
2100                 exc_machine_check_user(regs);
2101         else
2102                 exc_machine_check_kernel(regs);
2103         local_db_restore(dr7);
2104 }
2105 #endif
2106
2107 /*
2108  * Called for each booted CPU to set up machine checks.
2109  * Must be called with preempt off:
2110  */
2111 void mcheck_cpu_init(struct cpuinfo_x86 *c)
2112 {
2113         if (mca_cfg.disabled)
2114                 return;
2115
2116         if (__mcheck_cpu_ancient_init(c))
2117                 return;
2118
2119         if (!mce_available(c))
2120                 return;
2121
2122         __mcheck_cpu_cap_init();
2123
2124         if (__mcheck_cpu_apply_quirks(c) < 0) {
2125                 mca_cfg.disabled = 1;
2126                 return;
2127         }
2128
2129         if (mce_gen_pool_init()) {
2130                 mca_cfg.disabled = 1;
2131                 pr_emerg("Couldn't allocate MCE records pool!\n");
2132                 return;
2133         }
2134
2135         machine_check_vector = do_machine_check;
2136
2137         __mcheck_cpu_init_early(c);
2138         __mcheck_cpu_init_generic();
2139         __mcheck_cpu_init_vendor(c);
2140         __mcheck_cpu_init_clear_banks();
2141         __mcheck_cpu_check_banks();
2142         __mcheck_cpu_setup_timer();
2143 }
2144
2145 /*
2146  * Called for each booted CPU to clear some machine checks opt-ins
2147  */
2148 void mcheck_cpu_clear(struct cpuinfo_x86 *c)
2149 {
2150         if (mca_cfg.disabled)
2151                 return;
2152
2153         if (!mce_available(c))
2154                 return;
2155
2156         /*
2157          * Possibly to clear general settings generic to x86
2158          * __mcheck_cpu_clear_generic(c);
2159          */
2160         __mcheck_cpu_clear_vendor(c);
2161
2162 }
2163
2164 static void __mce_disable_bank(void *arg)
2165 {
2166         int bank = *((int *)arg);
2167         __clear_bit(bank, this_cpu_ptr(mce_poll_banks));
2168         cmci_disable_bank(bank);
2169 }
2170
2171 void mce_disable_bank(int bank)
2172 {
2173         if (bank >= this_cpu_read(mce_num_banks)) {
2174                 pr_warn(FW_BUG
2175                         "Ignoring request to disable invalid MCA bank %d.\n",
2176                         bank);
2177                 return;
2178         }
2179         set_bit(bank, mce_banks_ce_disabled);
2180         on_each_cpu(__mce_disable_bank, &bank, 1);
2181 }
2182
2183 /*
2184  * mce=off Disables machine check
2185  * mce=no_cmci Disables CMCI
2186  * mce=no_lmce Disables LMCE
2187  * mce=dont_log_ce Clears corrected events silently, no log created for CEs.
2188  * mce=print_all Print all machine check logs to console
2189  * mce=ignore_ce Disables polling and CMCI, corrected events are not cleared.
2190  * mce=TOLERANCELEVEL[,monarchtimeout] (number, see above)
2191  *      monarchtimeout is how long to wait for other CPUs on machine
2192  *      check, or 0 to not wait
2193  * mce=bootlog Log MCEs from before booting. Disabled by default on AMD Fam10h
2194         and older.
2195  * mce=nobootlog Don't log MCEs from before booting.
2196  * mce=bios_cmci_threshold Don't program the CMCI threshold
2197  * mce=recovery force enable copy_mc_fragile()
2198  */
2199 static int __init mcheck_enable(char *str)
2200 {
2201         struct mca_config *cfg = &mca_cfg;
2202
2203         if (*str == 0) {
2204                 enable_p5_mce();
2205                 return 1;
2206         }
2207         if (*str == '=')
2208                 str++;
2209         if (!strcmp(str, "off"))
2210                 cfg->disabled = 1;
2211         else if (!strcmp(str, "no_cmci"))
2212                 cfg->cmci_disabled = true;
2213         else if (!strcmp(str, "no_lmce"))
2214                 cfg->lmce_disabled = 1;
2215         else if (!strcmp(str, "dont_log_ce"))
2216                 cfg->dont_log_ce = true;
2217         else if (!strcmp(str, "print_all"))
2218                 cfg->print_all = true;
2219         else if (!strcmp(str, "ignore_ce"))
2220                 cfg->ignore_ce = true;
2221         else if (!strcmp(str, "bootlog") || !strcmp(str, "nobootlog"))
2222                 cfg->bootlog = (str[0] == 'b');
2223         else if (!strcmp(str, "bios_cmci_threshold"))
2224                 cfg->bios_cmci_threshold = 1;
2225         else if (!strcmp(str, "recovery"))
2226                 cfg->recovery = 1;
2227         else if (isdigit(str[0])) {
2228                 if (get_option(&str, &cfg->tolerant) == 2)
2229                         get_option(&str, &(cfg->monarch_timeout));
2230         } else {
2231                 pr_info("mce argument %s ignored. Please use /sys\n", str);
2232                 return 0;
2233         }
2234         return 1;
2235 }
2236 __setup("mce", mcheck_enable);
2237
2238 int __init mcheck_init(void)
2239 {
2240         mce_register_decode_chain(&early_nb);
2241         mce_register_decode_chain(&mce_uc_nb);
2242         mce_register_decode_chain(&mce_default_nb);
2243         mcheck_vendor_init_severity();
2244
2245         INIT_WORK(&mce_work, mce_gen_pool_process);
2246         init_irq_work(&mce_irq_work, mce_irq_work_cb);
2247
2248         return 0;
2249 }
2250
2251 /*
2252  * mce_syscore: PM support
2253  */
2254
2255 /*
2256  * Disable machine checks on suspend and shutdown. We can't really handle
2257  * them later.
2258  */
2259 static void mce_disable_error_reporting(void)
2260 {
2261         struct mce_bank *mce_banks = this_cpu_ptr(mce_banks_array);
2262         int i;
2263
2264         for (i = 0; i < this_cpu_read(mce_num_banks); i++) {
2265                 struct mce_bank *b = &mce_banks[i];
2266
2267                 if (b->init)
2268                         wrmsrl(msr_ops.ctl(i), 0);
2269         }
2270         return;
2271 }
2272
2273 static void vendor_disable_error_reporting(void)
2274 {
2275         /*
2276          * Don't clear on Intel or AMD or Hygon or Zhaoxin CPUs. Some of these
2277          * MSRs are socket-wide. Disabling them for just a single offlined CPU
2278          * is bad, since it will inhibit reporting for all shared resources on
2279          * the socket like the last level cache (LLC), the integrated memory
2280          * controller (iMC), etc.
2281          */
2282         if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL ||
2283             boot_cpu_data.x86_vendor == X86_VENDOR_HYGON ||
2284             boot_cpu_data.x86_vendor == X86_VENDOR_AMD ||
2285             boot_cpu_data.x86_vendor == X86_VENDOR_ZHAOXIN)
2286                 return;
2287
2288         mce_disable_error_reporting();
2289 }
2290
2291 static int mce_syscore_suspend(void)
2292 {
2293         vendor_disable_error_reporting();
2294         return 0;
2295 }
2296
2297 static void mce_syscore_shutdown(void)
2298 {
2299         vendor_disable_error_reporting();
2300 }
2301
2302 /*
2303  * On resume clear all MCE state. Don't want to see leftovers from the BIOS.
2304  * Only one CPU is active at this time, the others get re-added later using
2305  * CPU hotplug:
2306  */
2307 static void mce_syscore_resume(void)
2308 {
2309         __mcheck_cpu_init_generic();
2310         __mcheck_cpu_init_vendor(raw_cpu_ptr(&cpu_info));
2311         __mcheck_cpu_init_clear_banks();
2312 }
2313
2314 static struct syscore_ops mce_syscore_ops = {
2315         .suspend        = mce_syscore_suspend,
2316         .shutdown       = mce_syscore_shutdown,
2317         .resume         = mce_syscore_resume,
2318 };
2319
2320 /*
2321  * mce_device: Sysfs support
2322  */
2323
2324 static void mce_cpu_restart(void *data)
2325 {
2326         if (!mce_available(raw_cpu_ptr(&cpu_info)))
2327                 return;
2328         __mcheck_cpu_init_generic();
2329         __mcheck_cpu_init_clear_banks();
2330         __mcheck_cpu_init_timer();
2331 }
2332
2333 /* Reinit MCEs after user configuration changes */
2334 static void mce_restart(void)
2335 {
2336         mce_timer_delete_all();
2337         on_each_cpu(mce_cpu_restart, NULL, 1);
2338 }
2339
2340 /* Toggle features for corrected errors */
2341 static void mce_disable_cmci(void *data)
2342 {
2343         if (!mce_available(raw_cpu_ptr(&cpu_info)))
2344                 return;
2345         cmci_clear();
2346 }
2347
2348 static void mce_enable_ce(void *all)
2349 {
2350         if (!mce_available(raw_cpu_ptr(&cpu_info)))
2351                 return;
2352         cmci_reenable();
2353         cmci_recheck();
2354         if (all)
2355                 __mcheck_cpu_init_timer();
2356 }
2357
2358 static struct bus_type mce_subsys = {
2359         .name           = "machinecheck",
2360         .dev_name       = "machinecheck",
2361 };
2362
2363 DEFINE_PER_CPU(struct device *, mce_device);
2364
2365 static inline struct mce_bank_dev *attr_to_bank(struct device_attribute *attr)
2366 {
2367         return container_of(attr, struct mce_bank_dev, attr);
2368 }
2369
2370 static ssize_t show_bank(struct device *s, struct device_attribute *attr,
2371                          char *buf)
2372 {
2373         u8 bank = attr_to_bank(attr)->bank;
2374         struct mce_bank *b;
2375
2376         if (bank >= per_cpu(mce_num_banks, s->id))
2377                 return -EINVAL;
2378
2379         b = &per_cpu(mce_banks_array, s->id)[bank];
2380
2381         if (!b->init)
2382                 return -ENODEV;
2383
2384         return sprintf(buf, "%llx\n", b->ctl);
2385 }
2386
2387 static ssize_t set_bank(struct device *s, struct device_attribute *attr,
2388                         const char *buf, size_t size)
2389 {
2390         u8 bank = attr_to_bank(attr)->bank;
2391         struct mce_bank *b;
2392         u64 new;
2393
2394         if (kstrtou64(buf, 0, &new) < 0)
2395                 return -EINVAL;
2396
2397         if (bank >= per_cpu(mce_num_banks, s->id))
2398                 return -EINVAL;
2399
2400         b = &per_cpu(mce_banks_array, s->id)[bank];
2401
2402         if (!b->init)
2403                 return -ENODEV;
2404
2405         b->ctl = new;
2406         mce_restart();
2407
2408         return size;
2409 }
2410
2411 static ssize_t set_ignore_ce(struct device *s,
2412                              struct device_attribute *attr,
2413                              const char *buf, size_t size)
2414 {
2415         u64 new;
2416
2417         if (kstrtou64(buf, 0, &new) < 0)
2418                 return -EINVAL;
2419
2420         mutex_lock(&mce_sysfs_mutex);
2421         if (mca_cfg.ignore_ce ^ !!new) {
2422                 if (new) {
2423                         /* disable ce features */
2424                         mce_timer_delete_all();
2425                         on_each_cpu(mce_disable_cmci, NULL, 1);
2426                         mca_cfg.ignore_ce = true;
2427                 } else {
2428                         /* enable ce features */
2429                         mca_cfg.ignore_ce = false;
2430                         on_each_cpu(mce_enable_ce, (void *)1, 1);
2431                 }
2432         }
2433         mutex_unlock(&mce_sysfs_mutex);
2434
2435         return size;
2436 }
2437
2438 static ssize_t set_cmci_disabled(struct device *s,
2439                                  struct device_attribute *attr,
2440                                  const char *buf, size_t size)
2441 {
2442         u64 new;
2443
2444         if (kstrtou64(buf, 0, &new) < 0)
2445                 return -EINVAL;
2446
2447         mutex_lock(&mce_sysfs_mutex);
2448         if (mca_cfg.cmci_disabled ^ !!new) {
2449                 if (new) {
2450                         /* disable cmci */
2451                         on_each_cpu(mce_disable_cmci, NULL, 1);
2452                         mca_cfg.cmci_disabled = true;
2453                 } else {
2454                         /* enable cmci */
2455                         mca_cfg.cmci_disabled = false;
2456                         on_each_cpu(mce_enable_ce, NULL, 1);
2457                 }
2458         }
2459         mutex_unlock(&mce_sysfs_mutex);
2460
2461         return size;
2462 }
2463
2464 static ssize_t store_int_with_restart(struct device *s,
2465                                       struct device_attribute *attr,
2466                                       const char *buf, size_t size)
2467 {
2468         unsigned long old_check_interval = check_interval;
2469         ssize_t ret = device_store_ulong(s, attr, buf, size);
2470
2471         if (check_interval == old_check_interval)
2472                 return ret;
2473
2474         mutex_lock(&mce_sysfs_mutex);
2475         mce_restart();
2476         mutex_unlock(&mce_sysfs_mutex);
2477
2478         return ret;
2479 }
2480
2481 static DEVICE_INT_ATTR(tolerant, 0644, mca_cfg.tolerant);
2482 static DEVICE_INT_ATTR(monarch_timeout, 0644, mca_cfg.monarch_timeout);
2483 static DEVICE_BOOL_ATTR(dont_log_ce, 0644, mca_cfg.dont_log_ce);
2484 static DEVICE_BOOL_ATTR(print_all, 0644, mca_cfg.print_all);
2485
2486 static struct dev_ext_attribute dev_attr_check_interval = {
2487         __ATTR(check_interval, 0644, device_show_int, store_int_with_restart),
2488         &check_interval
2489 };
2490
2491 static struct dev_ext_attribute dev_attr_ignore_ce = {
2492         __ATTR(ignore_ce, 0644, device_show_bool, set_ignore_ce),
2493         &mca_cfg.ignore_ce
2494 };
2495
2496 static struct dev_ext_attribute dev_attr_cmci_disabled = {
2497         __ATTR(cmci_disabled, 0644, device_show_bool, set_cmci_disabled),
2498         &mca_cfg.cmci_disabled
2499 };
2500
2501 static struct device_attribute *mce_device_attrs[] = {
2502         &dev_attr_tolerant.attr,
2503         &dev_attr_check_interval.attr,
2504 #ifdef CONFIG_X86_MCELOG_LEGACY
2505         &dev_attr_trigger,
2506 #endif
2507         &dev_attr_monarch_timeout.attr,
2508         &dev_attr_dont_log_ce.attr,
2509         &dev_attr_print_all.attr,
2510         &dev_attr_ignore_ce.attr,
2511         &dev_attr_cmci_disabled.attr,
2512         NULL
2513 };
2514
2515 static cpumask_var_t mce_device_initialized;
2516
2517 static void mce_device_release(struct device *dev)
2518 {
2519         kfree(dev);
2520 }
2521
2522 /* Per CPU device init. All of the CPUs still share the same bank device: */
2523 static int mce_device_create(unsigned int cpu)
2524 {
2525         struct device *dev;
2526         int err;
2527         int i, j;
2528
2529         if (!mce_available(&boot_cpu_data))
2530                 return -EIO;
2531
2532         dev = per_cpu(mce_device, cpu);
2533         if (dev)
2534                 return 0;
2535
2536         dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2537         if (!dev)
2538                 return -ENOMEM;
2539         dev->id  = cpu;
2540         dev->bus = &mce_subsys;
2541         dev->release = &mce_device_release;
2542
2543         err = device_register(dev);
2544         if (err) {
2545                 put_device(dev);
2546                 return err;
2547         }
2548
2549         for (i = 0; mce_device_attrs[i]; i++) {
2550                 err = device_create_file(dev, mce_device_attrs[i]);
2551                 if (err)
2552                         goto error;
2553         }
2554         for (j = 0; j < per_cpu(mce_num_banks, cpu); j++) {
2555                 err = device_create_file(dev, &mce_bank_devs[j].attr);
2556                 if (err)
2557                         goto error2;
2558         }
2559         cpumask_set_cpu(cpu, mce_device_initialized);
2560         per_cpu(mce_device, cpu) = dev;
2561
2562         return 0;
2563 error2:
2564         while (--j >= 0)
2565                 device_remove_file(dev, &mce_bank_devs[j].attr);
2566 error:
2567         while (--i >= 0)
2568                 device_remove_file(dev, mce_device_attrs[i]);
2569
2570         device_unregister(dev);
2571
2572         return err;
2573 }
2574
2575 static void mce_device_remove(unsigned int cpu)
2576 {
2577         struct device *dev = per_cpu(mce_device, cpu);
2578         int i;
2579
2580         if (!cpumask_test_cpu(cpu, mce_device_initialized))
2581                 return;
2582
2583         for (i = 0; mce_device_attrs[i]; i++)
2584                 device_remove_file(dev, mce_device_attrs[i]);
2585
2586         for (i = 0; i < per_cpu(mce_num_banks, cpu); i++)
2587                 device_remove_file(dev, &mce_bank_devs[i].attr);
2588
2589         device_unregister(dev);
2590         cpumask_clear_cpu(cpu, mce_device_initialized);
2591         per_cpu(mce_device, cpu) = NULL;
2592 }
2593
2594 /* Make sure there are no machine checks on offlined CPUs. */
2595 static void mce_disable_cpu(void)
2596 {
2597         if (!mce_available(raw_cpu_ptr(&cpu_info)))
2598                 return;
2599
2600         if (!cpuhp_tasks_frozen)
2601                 cmci_clear();
2602
2603         vendor_disable_error_reporting();
2604 }
2605
2606 static void mce_reenable_cpu(void)
2607 {
2608         struct mce_bank *mce_banks = this_cpu_ptr(mce_banks_array);
2609         int i;
2610
2611         if (!mce_available(raw_cpu_ptr(&cpu_info)))
2612                 return;
2613
2614         if (!cpuhp_tasks_frozen)
2615                 cmci_reenable();
2616         for (i = 0; i < this_cpu_read(mce_num_banks); i++) {
2617                 struct mce_bank *b = &mce_banks[i];
2618
2619                 if (b->init)
2620                         wrmsrl(msr_ops.ctl(i), b->ctl);
2621         }
2622 }
2623
2624 static int mce_cpu_dead(unsigned int cpu)
2625 {
2626         mce_intel_hcpu_update(cpu);
2627
2628         /* intentionally ignoring frozen here */
2629         if (!cpuhp_tasks_frozen)
2630                 cmci_rediscover();
2631         return 0;
2632 }
2633
2634 static int mce_cpu_online(unsigned int cpu)
2635 {
2636         struct timer_list *t = this_cpu_ptr(&mce_timer);
2637         int ret;
2638
2639         mce_device_create(cpu);
2640
2641         ret = mce_threshold_create_device(cpu);
2642         if (ret) {
2643                 mce_device_remove(cpu);
2644                 return ret;
2645         }
2646         mce_reenable_cpu();
2647         mce_start_timer(t);
2648         return 0;
2649 }
2650
2651 static int mce_cpu_pre_down(unsigned int cpu)
2652 {
2653         struct timer_list *t = this_cpu_ptr(&mce_timer);
2654
2655         mce_disable_cpu();
2656         del_timer_sync(t);
2657         mce_threshold_remove_device(cpu);
2658         mce_device_remove(cpu);
2659         return 0;
2660 }
2661
2662 static __init void mce_init_banks(void)
2663 {
2664         int i;
2665
2666         for (i = 0; i < MAX_NR_BANKS; i++) {
2667                 struct mce_bank_dev *b = &mce_bank_devs[i];
2668                 struct device_attribute *a = &b->attr;
2669
2670                 b->bank = i;
2671
2672                 sysfs_attr_init(&a->attr);
2673                 a->attr.name    = b->attrname;
2674                 snprintf(b->attrname, ATTR_LEN, "bank%d", i);
2675
2676                 a->attr.mode    = 0644;
2677                 a->show         = show_bank;
2678                 a->store        = set_bank;
2679         }
2680 }
2681
2682 /*
2683  * When running on XEN, this initcall is ordered against the XEN mcelog
2684  * initcall:
2685  *
2686  *   device_initcall(xen_late_init_mcelog);
2687  *   device_initcall_sync(mcheck_init_device);
2688  */
2689 static __init int mcheck_init_device(void)
2690 {
2691         int err;
2692
2693         /*
2694          * Check if we have a spare virtual bit. This will only become
2695          * a problem if/when we move beyond 5-level page tables.
2696          */
2697         MAYBE_BUILD_BUG_ON(__VIRTUAL_MASK_SHIFT >= 63);
2698
2699         if (!mce_available(&boot_cpu_data)) {
2700                 err = -EIO;
2701                 goto err_out;
2702         }
2703
2704         if (!zalloc_cpumask_var(&mce_device_initialized, GFP_KERNEL)) {
2705                 err = -ENOMEM;
2706                 goto err_out;
2707         }
2708
2709         mce_init_banks();
2710
2711         err = subsys_system_register(&mce_subsys, NULL);
2712         if (err)
2713                 goto err_out_mem;
2714
2715         err = cpuhp_setup_state(CPUHP_X86_MCE_DEAD, "x86/mce:dead", NULL,
2716                                 mce_cpu_dead);
2717         if (err)
2718                 goto err_out_mem;
2719
2720         /*
2721          * Invokes mce_cpu_online() on all CPUs which are online when
2722          * the state is installed.
2723          */
2724         err = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "x86/mce:online",
2725                                 mce_cpu_online, mce_cpu_pre_down);
2726         if (err < 0)
2727                 goto err_out_online;
2728
2729         register_syscore_ops(&mce_syscore_ops);
2730
2731         return 0;
2732
2733 err_out_online:
2734         cpuhp_remove_state(CPUHP_X86_MCE_DEAD);
2735
2736 err_out_mem:
2737         free_cpumask_var(mce_device_initialized);
2738
2739 err_out:
2740         pr_err("Unable to init MCE device (rc: %d)\n", err);
2741
2742         return err;
2743 }
2744 device_initcall_sync(mcheck_init_device);
2745
2746 /*
2747  * Old style boot options parsing. Only for compatibility.
2748  */
2749 static int __init mcheck_disable(char *str)
2750 {
2751         mca_cfg.disabled = 1;
2752         return 1;
2753 }
2754 __setup("nomce", mcheck_disable);
2755
2756 #ifdef CONFIG_DEBUG_FS
2757 struct dentry *mce_get_debugfs_dir(void)
2758 {
2759         static struct dentry *dmce;
2760
2761         if (!dmce)
2762                 dmce = debugfs_create_dir("mce", NULL);
2763
2764         return dmce;
2765 }
2766
2767 static void mce_reset(void)
2768 {
2769         cpu_missing = 0;
2770         atomic_set(&mce_fake_panicked, 0);
2771         atomic_set(&mce_executing, 0);
2772         atomic_set(&mce_callin, 0);
2773         atomic_set(&global_nwo, 0);
2774         cpumask_setall(&mce_missing_cpus);
2775 }
2776
2777 static int fake_panic_get(void *data, u64 *val)
2778 {
2779         *val = fake_panic;
2780         return 0;
2781 }
2782
2783 static int fake_panic_set(void *data, u64 val)
2784 {
2785         mce_reset();
2786         fake_panic = val;
2787         return 0;
2788 }
2789
2790 DEFINE_DEBUGFS_ATTRIBUTE(fake_panic_fops, fake_panic_get, fake_panic_set,
2791                          "%llu\n");
2792
2793 static void __init mcheck_debugfs_init(void)
2794 {
2795         struct dentry *dmce;
2796
2797         dmce = mce_get_debugfs_dir();
2798         debugfs_create_file_unsafe("fake_panic", 0444, dmce, NULL,
2799                                    &fake_panic_fops);
2800 }
2801 #else
2802 static void __init mcheck_debugfs_init(void) { }
2803 #endif
2804
2805 static int __init mcheck_late_init(void)
2806 {
2807         if (mca_cfg.recovery)
2808                 enable_copy_mc_fragile();
2809
2810         mcheck_debugfs_init();
2811
2812         /*
2813          * Flush out everything that has been logged during early boot, now that
2814          * everything has been initialized (workqueues, decoders, ...).
2815          */
2816         mce_schedule_work();
2817
2818         return 0;
2819 }
2820 late_initcall(mcheck_late_init);