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