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