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