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