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