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