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