1 // SPDX-License-Identifier: GPL-2.0-only
3 * Machine check handler.
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
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>
40 #include <linux/debugfs.h>
41 #include <linux/irq_work.h>
42 #include <linux/export.h>
43 #include <linux/jump_label.h>
44 #include <linux/set_memory.h>
46 #include <asm/intel-family.h>
47 #include <asm/processor.h>
48 #include <asm/traps.h>
49 #include <asm/tlbflush.h>
52 #include <asm/reboot.h>
56 static DEFINE_MUTEX(mce_log_mutex);
58 /* sysfs synchronization */
59 static DEFINE_MUTEX(mce_sysfs_mutex);
61 #define CREATE_TRACE_POINTS
62 #include <trace/events/mce.h>
64 #define SPINUNIT 100 /* 100ns */
66 DEFINE_PER_CPU(unsigned, mce_exception_count);
68 DEFINE_PER_CPU_READ_MOSTLY(unsigned int, mce_num_banks);
71 u64 ctl; /* subevents to enable */
72 bool init; /* initialise bank? */
74 static DEFINE_PER_CPU_READ_MOSTLY(struct mce_bank[MAX_NR_BANKS], mce_banks_array);
77 /* One object for each MCE bank, shared by all CPUs */
79 struct device_attribute attr; /* device attribute */
80 char attrname[ATTR_LEN]; /* attribute name */
81 u8 bank; /* bank number */
83 static struct mce_bank_dev mce_bank_devs[MAX_NR_BANKS];
85 struct mce_vendor_flags mce_flags __read_mostly;
87 struct mca_config mca_cfg __read_mostly = {
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)
100 static DEFINE_PER_CPU(struct mce, mces_seen);
101 static unsigned long mce_need_notify;
102 static int cpu_missing;
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).
108 DEFINE_PER_CPU(mce_banks_t, mce_poll_banks) = {
109 [0 ... BITS_TO_LONGS(MAX_NR_BANKS)-1] = ~0UL
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.
119 mce_banks_t mce_banks_ce_disabled;
121 static struct work_struct mce_work;
122 static struct irq_work mce_irq_work;
124 static void (*quirk_no_way_out)(int bank, struct mce *m, struct pt_regs *regs);
127 * CPU/chipset specific EDAC code can register a notifier call here to print
128 * MCE errors in a human-readable form.
130 BLOCKING_NOTIFIER_HEAD(x86_mce_decoder_chain);
132 /* Do initial initialization of a struct mce */
133 void mce_setup(struct mce *m)
135 memset(m, 0, sizeof(struct mce));
136 m->cpu = m->extcpu = smp_processor_id();
137 /* need the internal __ version to avoid deadlocks */
138 m->time = __ktime_get_real_seconds();
139 m->cpuvendor = boot_cpu_data.x86_vendor;
140 m->cpuid = cpuid_eax(1);
141 m->socketid = cpu_data(m->extcpu).phys_proc_id;
142 m->apicid = cpu_data(m->extcpu).initial_apicid;
143 rdmsrl(MSR_IA32_MCG_CAP, m->mcgcap);
145 if (this_cpu_has(X86_FEATURE_INTEL_PPIN))
146 rdmsrl(MSR_PPIN, m->ppin);
148 m->microcode = boot_cpu_data.microcode;
151 DEFINE_PER_CPU(struct mce, injectm);
152 EXPORT_PER_CPU_SYMBOL_GPL(injectm);
154 void mce_log(struct mce *m)
156 if (!mce_gen_pool_add(m))
157 irq_work_queue(&mce_irq_work);
160 void mce_inject_log(struct mce *m)
162 mutex_lock(&mce_log_mutex);
164 mutex_unlock(&mce_log_mutex);
166 EXPORT_SYMBOL_GPL(mce_inject_log);
168 static struct notifier_block mce_srao_nb;
171 * We run the default notifier if we have only the SRAO, the first and the
172 * default notifier registered. I.e., the mandatory NUM_DEFAULT_NOTIFIERS
173 * notifiers registered on the chain.
175 #define NUM_DEFAULT_NOTIFIERS 3
176 static atomic_t num_notifiers;
178 void mce_register_decode_chain(struct notifier_block *nb)
180 if (WARN_ON(nb->priority > MCE_PRIO_MCELOG && nb->priority < MCE_PRIO_EDAC))
183 atomic_inc(&num_notifiers);
185 blocking_notifier_chain_register(&x86_mce_decoder_chain, nb);
187 EXPORT_SYMBOL_GPL(mce_register_decode_chain);
189 void mce_unregister_decode_chain(struct notifier_block *nb)
191 atomic_dec(&num_notifiers);
193 blocking_notifier_chain_unregister(&x86_mce_decoder_chain, nb);
195 EXPORT_SYMBOL_GPL(mce_unregister_decode_chain);
197 static inline u32 ctl_reg(int bank)
199 return MSR_IA32_MCx_CTL(bank);
202 static inline u32 status_reg(int bank)
204 return MSR_IA32_MCx_STATUS(bank);
207 static inline u32 addr_reg(int bank)
209 return MSR_IA32_MCx_ADDR(bank);
212 static inline u32 misc_reg(int bank)
214 return MSR_IA32_MCx_MISC(bank);
217 static inline u32 smca_ctl_reg(int bank)
219 return MSR_AMD64_SMCA_MCx_CTL(bank);
222 static inline u32 smca_status_reg(int bank)
224 return MSR_AMD64_SMCA_MCx_STATUS(bank);
227 static inline u32 smca_addr_reg(int bank)
229 return MSR_AMD64_SMCA_MCx_ADDR(bank);
232 static inline u32 smca_misc_reg(int bank)
234 return MSR_AMD64_SMCA_MCx_MISC(bank);
237 struct mca_msr_regs msr_ops = {
239 .status = status_reg,
244 static void __print_mce(struct mce *m)
246 pr_emerg(HW_ERR "CPU %d: Machine Check%s: %Lx Bank %d: %016Lx\n",
248 (m->mcgstatus & MCG_STATUS_MCIP ? " Exception" : ""),
249 m->mcgstatus, m->bank, m->status);
252 pr_emerg(HW_ERR "RIP%s %02x:<%016Lx> ",
253 !(m->mcgstatus & MCG_STATUS_EIPV) ? " !INEXACT!" : "",
256 if (m->cs == __KERNEL_CS)
257 pr_cont("{%pS}", (void *)(unsigned long)m->ip);
261 pr_emerg(HW_ERR "TSC %llx ", m->tsc);
263 pr_cont("ADDR %llx ", m->addr);
265 pr_cont("MISC %llx ", m->misc);
267 if (mce_flags.smca) {
269 pr_cont("SYND %llx ", m->synd);
271 pr_cont("IPID %llx ", m->ipid);
276 * Note this output is parsed by external tools and old fields
277 * should not be changed.
279 pr_emerg(HW_ERR "PROCESSOR %u:%x TIME %llu SOCKET %u APIC %x microcode %x\n",
280 m->cpuvendor, m->cpuid, m->time, m->socketid, m->apicid,
284 static void print_mce(struct mce *m)
288 if (m->cpuvendor != X86_VENDOR_AMD && m->cpuvendor != X86_VENDOR_HYGON)
289 pr_emerg_ratelimited(HW_ERR "Run the above through 'mcelog --ascii'\n");
292 #define PANIC_TIMEOUT 5 /* 5 seconds */
294 static atomic_t mce_panicked;
296 static int fake_panic;
297 static atomic_t mce_fake_panicked;
299 /* Panic in progress. Enable interrupts and wait for final IPI */
300 static void wait_for_panic(void)
302 long timeout = PANIC_TIMEOUT*USEC_PER_SEC;
306 while (timeout-- > 0)
308 if (panic_timeout == 0)
309 panic_timeout = mca_cfg.panic_timeout;
310 panic("Panicing machine check CPU died");
313 static void mce_panic(const char *msg, struct mce *final, char *exp)
316 struct llist_node *pending;
317 struct mce_evt_llist *l;
321 * Make sure only one CPU runs in machine check panic
323 if (atomic_inc_return(&mce_panicked) > 1)
330 /* Don't log too much for fake panic */
331 if (atomic_inc_return(&mce_fake_panicked) > 1)
334 pending = mce_gen_pool_prepare_records();
335 /* First print corrected ones that are still unlogged */
336 llist_for_each_entry(l, pending, llnode) {
337 struct mce *m = &l->mce;
338 if (!(m->status & MCI_STATUS_UC)) {
341 apei_err = apei_write_mce(m);
344 /* Now print uncorrected but with the final one last */
345 llist_for_each_entry(l, pending, llnode) {
346 struct mce *m = &l->mce;
347 if (!(m->status & MCI_STATUS_UC))
349 if (!final || mce_cmp(m, final)) {
352 apei_err = apei_write_mce(m);
358 apei_err = apei_write_mce(final);
361 pr_emerg(HW_ERR "Some CPUs didn't answer in synchronization\n");
363 pr_emerg(HW_ERR "Machine check: %s\n", exp);
365 if (panic_timeout == 0)
366 panic_timeout = mca_cfg.panic_timeout;
369 pr_emerg(HW_ERR "Fake kernel panic: %s\n", msg);
372 /* Support code for software error injection */
374 static int msr_to_offset(u32 msr)
376 unsigned bank = __this_cpu_read(injectm.bank);
378 if (msr == mca_cfg.rip_msr)
379 return offsetof(struct mce, ip);
380 if (msr == msr_ops.status(bank))
381 return offsetof(struct mce, status);
382 if (msr == msr_ops.addr(bank))
383 return offsetof(struct mce, addr);
384 if (msr == msr_ops.misc(bank))
385 return offsetof(struct mce, misc);
386 if (msr == MSR_IA32_MCG_STATUS)
387 return offsetof(struct mce, mcgstatus);
391 /* MSR access wrappers used for error injection */
392 static u64 mce_rdmsrl(u32 msr)
396 if (__this_cpu_read(injectm.finished)) {
397 int offset = msr_to_offset(msr);
401 return *(u64 *)((char *)this_cpu_ptr(&injectm) + offset);
404 if (rdmsrl_safe(msr, &v)) {
405 WARN_ONCE(1, "mce: Unable to read MSR 0x%x!\n", msr);
407 * Return zero in case the access faulted. This should
408 * not happen normally but can happen if the CPU does
409 * something weird, or if the code is buggy.
417 static void mce_wrmsrl(u32 msr, u64 v)
419 if (__this_cpu_read(injectm.finished)) {
420 int offset = msr_to_offset(msr);
423 *(u64 *)((char *)this_cpu_ptr(&injectm) + offset) = v;
430 * Collect all global (w.r.t. this processor) status about this machine
431 * check into our "mce" struct so that we can use it later to assess
432 * the severity of the problem as we read per-bank specific details.
434 static inline void mce_gather_info(struct mce *m, struct pt_regs *regs)
438 m->mcgstatus = mce_rdmsrl(MSR_IA32_MCG_STATUS);
441 * Get the address of the instruction at the time of
442 * the machine check error.
444 if (m->mcgstatus & (MCG_STATUS_RIPV|MCG_STATUS_EIPV)) {
449 * When in VM86 mode make the cs look like ring 3
450 * always. This is a lie, but it's better than passing
451 * the additional vm86 bit around everywhere.
453 if (v8086_mode(regs))
456 /* Use accurate RIP reporting if available. */
458 m->ip = mce_rdmsrl(mca_cfg.rip_msr);
462 int mce_available(struct cpuinfo_x86 *c)
464 if (mca_cfg.disabled)
466 return cpu_has(c, X86_FEATURE_MCE) && cpu_has(c, X86_FEATURE_MCA);
469 static void mce_schedule_work(void)
471 if (!mce_gen_pool_empty())
472 schedule_work(&mce_work);
475 static void mce_irq_work_cb(struct irq_work *entry)
481 * Check if the address reported by the CPU is in a format we can parse.
482 * It would be possible to add code for most other cases, but all would
483 * be somewhat complicated (e.g. segment offset would require an instruction
484 * parser). So only support physical addresses up to page granuality for now.
486 int mce_usable_address(struct mce *m)
488 if (!(m->status & MCI_STATUS_ADDRV))
491 /* Checks after this one are Intel-specific: */
492 if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL)
495 if (!(m->status & MCI_STATUS_MISCV))
498 if (MCI_MISC_ADDR_LSB(m->misc) > PAGE_SHIFT)
501 if (MCI_MISC_ADDR_MODE(m->misc) != MCI_MISC_ADDR_PHYS)
506 EXPORT_SYMBOL_GPL(mce_usable_address);
508 bool mce_is_memory_error(struct mce *m)
510 if (m->cpuvendor == X86_VENDOR_AMD ||
511 m->cpuvendor == X86_VENDOR_HYGON) {
512 return amd_mce_is_memory_error(m);
513 } else if (m->cpuvendor == X86_VENDOR_INTEL) {
515 * Intel SDM Volume 3B - 15.9.2 Compound Error Codes
517 * Bit 7 of the MCACOD field of IA32_MCi_STATUS is used for
518 * indicating a memory error. Bit 8 is used for indicating a
519 * cache hierarchy error. The combination of bit 2 and bit 3
520 * is used for indicating a `generic' cache hierarchy error
521 * But we can't just blindly check the above bits, because if
522 * bit 11 is set, then it is a bus/interconnect error - and
523 * either way the above bits just gives more detail on what
524 * bus/interconnect error happened. Note that bit 12 can be
525 * ignored, as it's the "filter" bit.
527 return (m->status & 0xef80) == BIT(7) ||
528 (m->status & 0xef00) == BIT(8) ||
529 (m->status & 0xeffc) == 0xc;
534 EXPORT_SYMBOL_GPL(mce_is_memory_error);
536 bool mce_is_correctable(struct mce *m)
538 if (m->cpuvendor == X86_VENDOR_AMD && m->status & MCI_STATUS_DEFERRED)
541 if (m->cpuvendor == X86_VENDOR_HYGON && m->status & MCI_STATUS_DEFERRED)
544 if (m->status & MCI_STATUS_UC)
549 EXPORT_SYMBOL_GPL(mce_is_correctable);
551 static bool cec_add_mce(struct mce *m)
556 /* We eat only correctable DRAM errors with usable addresses. */
557 if (mce_is_memory_error(m) &&
558 mce_is_correctable(m) &&
559 mce_usable_address(m))
560 if (!cec_add_elem(m->addr >> PAGE_SHIFT))
566 static int mce_first_notifier(struct notifier_block *nb, unsigned long val,
569 struct mce *m = (struct mce *)data;
577 /* Emit the trace record: */
580 set_bit(0, &mce_need_notify);
587 static struct notifier_block first_nb = {
588 .notifier_call = mce_first_notifier,
589 .priority = MCE_PRIO_FIRST,
592 static int srao_decode_notifier(struct notifier_block *nb, unsigned long val,
595 struct mce *mce = (struct mce *)data;
601 if (mce_usable_address(mce) && (mce->severity == MCE_AO_SEVERITY)) {
602 pfn = mce->addr >> PAGE_SHIFT;
603 if (!memory_failure(pfn, 0))
609 static struct notifier_block mce_srao_nb = {
610 .notifier_call = srao_decode_notifier,
611 .priority = MCE_PRIO_SRAO,
614 static int mce_default_notifier(struct notifier_block *nb, unsigned long val,
617 struct mce *m = (struct mce *)data;
622 if (atomic_read(&num_notifiers) > NUM_DEFAULT_NOTIFIERS)
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,
637 * Read ADDR and MISC registers.
639 static void mce_read_aux(struct mce *m, int i)
641 if (m->status & MCI_STATUS_MISCV)
642 m->misc = mce_rdmsrl(msr_ops.misc(i));
644 if (m->status & MCI_STATUS_ADDRV) {
645 m->addr = mce_rdmsrl(msr_ops.addr(i));
648 * Mask the reported address by the reported granularity.
650 if (mca_cfg.ser && (m->status & MCI_STATUS_MISCV)) {
651 u8 shift = MCI_MISC_ADDR_LSB(m->misc);
657 * Extract [55:<lsb>] where lsb is the least significant
658 * *valid* bit of the address bits.
660 if (mce_flags.smca) {
661 u8 lsb = (m->addr >> 56) & 0x3f;
663 m->addr &= GENMASK_ULL(55, lsb);
667 if (mce_flags.smca) {
668 m->ipid = mce_rdmsrl(MSR_AMD64_SMCA_MCx_IPID(i));
670 if (m->status & MCI_STATUS_SYNDV)
671 m->synd = mce_rdmsrl(MSR_AMD64_SMCA_MCx_SYND(i));
675 DEFINE_PER_CPU(unsigned, mce_poll_count);
678 * Poll for corrected events or events that happened before reset.
679 * Those are just logged through /dev/mcelog.
681 * This is executed in standard interrupt context.
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.
692 bool machine_check_poll(enum mcp_flags flags, mce_banks_t *b)
694 struct mce_bank *mce_banks = this_cpu_ptr(mce_banks_array);
695 bool error_seen = false;
699 this_cpu_inc(mce_poll_count);
701 mce_gather_info(&m, NULL);
703 if (flags & MCP_TIMESTAMP)
706 for (i = 0; i < this_cpu_read(mce_num_banks); i++) {
707 if (!mce_banks[i].ctl || !test_bit(i, *b))
715 m.status = mce_rdmsrl(msr_ops.status(i));
717 /* If this entry is not valid, ignore it */
718 if (!(m.status & MCI_STATUS_VAL))
722 * If we are logging everything (at CPU online) or this
723 * is a corrected error, then we must log it.
725 if ((flags & MCP_UC) || !(m.status & MCI_STATUS_UC))
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
735 if (m.status & MCI_STATUS_UC)
740 /* Log "not enabled" (speculative) errors */
741 if (!(m.status & MCI_STATUS_EN))
745 * Log UCNA (SDM: 15.6.3 "UCR Error Classification")
746 * UC == 1 && PCC == 0 && S == 0
748 if (!(m.status & MCI_STATUS_PCC) && !(m.status & MCI_STATUS_S))
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.
763 m.severity = mce_severity(&m, mca_cfg.tolerant, NULL, false);
766 * Don't get the IP here because it's unlikely to
767 * have anything to do with the actual error location.
769 if (!(flags & MCP_DONTLOG) && !mca_cfg.dont_log_ce)
771 else if (mce_usable_address(&m)) {
773 * Although we skipped logging this, we still want
774 * to take action. Add to the pool so the registered
775 * notifiers will see it.
777 if (!mce_gen_pool_add(&m))
782 * Clear state for this bank.
784 mce_wrmsrl(msr_ops.status(i), 0);
788 * Don't clear MCG_STATUS here because it's only defined for
796 EXPORT_SYMBOL_GPL(machine_check_poll);
799 * Do a quick check if any of the events requires a panic.
800 * This decides if we keep the events around or clear them.
802 static int mce_no_way_out(struct mce *m, char **msg, unsigned long *validp,
803 struct pt_regs *regs)
808 for (i = 0; i < this_cpu_read(mce_num_banks); i++) {
809 m->status = mce_rdmsrl(msr_ops.status(i));
810 if (!(m->status & MCI_STATUS_VAL))
813 __set_bit(i, validp);
814 if (quirk_no_way_out)
815 quirk_no_way_out(i, m, regs);
817 if (mce_severity(m, mca_cfg.tolerant, &tmp, true) >= MCE_PANIC_SEVERITY) {
828 * Variable to establish order between CPUs while scanning.
829 * Each CPU spins initially until executing is equal its number.
831 static atomic_t mce_executing;
834 * Defines order of CPUs on entry. First CPU becomes Monarch.
836 static atomic_t mce_callin;
839 * Check if a timeout waiting for other CPUs happened.
841 static int mce_timed_out(u64 *t, const char *msg)
844 * The others already did panic for some reason.
845 * Bail out like in a timeout.
846 * rmb() to tell the compiler that system_state
847 * might have been modified by someone else.
850 if (atomic_read(&mce_panicked))
852 if (!mca_cfg.monarch_timeout)
854 if ((s64)*t < SPINUNIT) {
855 if (mca_cfg.tolerant <= 1)
856 mce_panic(msg, NULL, NULL);
862 touch_nmi_watchdog();
867 * The Monarch's reign. The Monarch is the CPU who entered
868 * the machine check handler first. It waits for the others to
869 * raise the exception too and then grades them. When any
870 * error is fatal panic. Only then let the others continue.
872 * The other CPUs entering the MCE handler will be controlled by the
873 * Monarch. They are called Subjects.
875 * This way we prevent any potential data corruption in a unrecoverable case
876 * and also makes sure always all CPU's errors are examined.
878 * Also this detects the case of a machine check event coming from outer
879 * space (not detected by any CPUs) In this case some external agent wants
880 * us to shut down, so panic too.
882 * The other CPUs might still decide to panic if the handler happens
883 * in a unrecoverable place, but in this case the system is in a semi-stable
884 * state and won't corrupt anything by itself. It's ok to let the others
885 * continue for a bit first.
887 * All the spin loops have timeouts; when a timeout happens a CPU
888 * typically elects itself to be Monarch.
890 static void mce_reign(void)
893 struct mce *m = NULL;
894 int global_worst = 0;
899 * This CPU is the Monarch and the other CPUs have run
900 * through their handlers.
901 * Grade the severity of the errors of all the CPUs.
903 for_each_possible_cpu(cpu) {
904 int severity = mce_severity(&per_cpu(mces_seen, cpu),
907 if (severity > global_worst) {
909 global_worst = severity;
910 m = &per_cpu(mces_seen, cpu);
915 * Cannot recover? Panic here then.
916 * This dumps all the mces in the log buffer and stops the
919 if (m && global_worst >= MCE_PANIC_SEVERITY && mca_cfg.tolerant < 3)
920 mce_panic("Fatal machine check", m, msg);
923 * For UC somewhere we let the CPU who detects it handle it.
924 * Also must let continue the others, otherwise the handling
925 * CPU could deadlock on a lock.
929 * No machine check event found. Must be some external
930 * source or one CPU is hung. Panic.
932 if (global_worst <= MCE_KEEP_SEVERITY && mca_cfg.tolerant < 3)
933 mce_panic("Fatal machine check from unknown source", NULL, NULL);
936 * Now clear all the mces_seen so that they don't reappear on
939 for_each_possible_cpu(cpu)
940 memset(&per_cpu(mces_seen, cpu), 0, sizeof(struct mce));
943 static atomic_t global_nwo;
946 * Start of Monarch synchronization. This waits until all CPUs have
947 * entered the exception handler and then determines if any of them
948 * saw a fatal event that requires panic. Then it executes them
949 * in the entry order.
950 * TBD double check parallel CPU hotunplug
952 static int mce_start(int *no_way_out)
955 int cpus = num_online_cpus();
956 u64 timeout = (u64)mca_cfg.monarch_timeout * NSEC_PER_USEC;
961 atomic_add(*no_way_out, &global_nwo);
963 * Rely on the implied barrier below, such that global_nwo
964 * is updated before mce_callin.
966 order = atomic_inc_return(&mce_callin);
971 while (atomic_read(&mce_callin) != cpus) {
972 if (mce_timed_out(&timeout,
973 "Timeout: Not all CPUs entered broadcast exception handler")) {
974 atomic_set(&global_nwo, 0);
981 * mce_callin should be read before global_nwo
987 * Monarch: Starts executing now, the others wait.
989 atomic_set(&mce_executing, 1);
992 * Subject: Now start the scanning loop one by one in
993 * the original callin order.
994 * This way when there are any shared banks it will be
995 * only seen by one CPU before cleared, avoiding duplicates.
997 while (atomic_read(&mce_executing) < order) {
998 if (mce_timed_out(&timeout,
999 "Timeout: Subject CPUs unable to finish machine check processing")) {
1000 atomic_set(&global_nwo, 0);
1008 * Cache the global no_way_out state.
1010 *no_way_out = atomic_read(&global_nwo);
1016 * Synchronize between CPUs after main scanning loop.
1017 * This invokes the bulk of the Monarch processing.
1019 static int mce_end(int order)
1022 u64 timeout = (u64)mca_cfg.monarch_timeout * NSEC_PER_USEC;
1030 * Allow others to run.
1032 atomic_inc(&mce_executing);
1035 /* CHECKME: Can this race with a parallel hotplug? */
1036 int cpus = num_online_cpus();
1039 * Monarch: Wait for everyone to go through their scanning
1042 while (atomic_read(&mce_executing) <= cpus) {
1043 if (mce_timed_out(&timeout,
1044 "Timeout: Monarch CPU unable to finish machine check processing"))
1054 * Subject: Wait for Monarch to finish.
1056 while (atomic_read(&mce_executing) != 0) {
1057 if (mce_timed_out(&timeout,
1058 "Timeout: Monarch CPU did not finish machine check processing"))
1064 * Don't reset anything. That's done by the Monarch.
1070 * Reset all global state.
1073 atomic_set(&global_nwo, 0);
1074 atomic_set(&mce_callin, 0);
1078 * Let others run again.
1080 atomic_set(&mce_executing, 0);
1084 static void mce_clear_state(unsigned long *toclear)
1088 for (i = 0; i < this_cpu_read(mce_num_banks); i++) {
1089 if (test_bit(i, toclear))
1090 mce_wrmsrl(msr_ops.status(i), 0);
1094 static int do_memory_failure(struct mce *m)
1096 int flags = MF_ACTION_REQUIRED;
1099 pr_err("Uncorrected hardware memory error in user-access at %llx", m->addr);
1100 if (!(m->mcgstatus & MCG_STATUS_RIPV))
1101 flags |= MF_MUST_KILL;
1102 ret = memory_failure(m->addr >> PAGE_SHIFT, flags);
1104 pr_err("Memory error not recovered");
1106 set_mce_nospec(m->addr >> PAGE_SHIFT);
1112 * Cases where we avoid rendezvous handler timeout:
1113 * 1) If this CPU is offline.
1115 * 2) If crashing_cpu was set, e.g. we're entering kdump and we need to
1116 * skip those CPUs which remain looping in the 1st kernel - see
1117 * crash_nmi_callback().
1119 * Note: there still is a small window between kexec-ing and the new,
1120 * kdump kernel establishing a new #MC handler where a broadcasted MCE
1121 * might not get handled properly.
1123 static bool __mc_check_crashing_cpu(int cpu)
1125 if (cpu_is_offline(cpu) ||
1126 (crashing_cpu != -1 && crashing_cpu != cpu)) {
1129 mcgstatus = mce_rdmsrl(MSR_IA32_MCG_STATUS);
1130 if (mcgstatus & MCG_STATUS_RIPV) {
1131 mce_wrmsrl(MSR_IA32_MCG_STATUS, 0);
1138 static void __mc_scan_banks(struct mce *m, struct mce *final,
1139 unsigned long *toclear, unsigned long *valid_banks,
1140 int no_way_out, int *worst)
1142 struct mce_bank *mce_banks = this_cpu_ptr(mce_banks_array);
1143 struct mca_config *cfg = &mca_cfg;
1146 for (i = 0; i < this_cpu_read(mce_num_banks); i++) {
1147 __clear_bit(i, toclear);
1148 if (!test_bit(i, valid_banks))
1151 if (!mce_banks[i].ctl)
1158 m->status = mce_rdmsrl(msr_ops.status(i));
1159 if (!(m->status & MCI_STATUS_VAL))
1163 * Corrected or non-signaled errors are handled by
1164 * machine_check_poll(). Leave them alone, unless this panics.
1166 if (!(m->status & (cfg->ser ? MCI_STATUS_S : MCI_STATUS_UC)) &&
1170 /* Set taint even when machine check was not enabled. */
1171 add_taint(TAINT_MACHINE_CHECK, LOCKDEP_NOW_UNRELIABLE);
1173 severity = mce_severity(m, cfg->tolerant, NULL, true);
1176 * When machine check was for corrected/deferred handler don't
1177 * touch, unless we're panicking.
1179 if ((severity == MCE_KEEP_SEVERITY ||
1180 severity == MCE_UCNA_SEVERITY) && !no_way_out)
1183 __set_bit(i, toclear);
1185 /* Machine check event was not enabled. Clear, but ignore. */
1186 if (severity == MCE_NO_SEVERITY)
1191 /* assuming valid severity level != 0 */
1192 m->severity = severity;
1196 if (severity > *worst) {
1202 /* mce_clear_state will clear *final, save locally for use later */
1207 * The actual machine check handler. This only handles real
1208 * exceptions when something got corrupted coming in through int 18.
1210 * This is executed in NMI context not subject to normal locking rules. This
1211 * implies that most kernel services cannot be safely used. Don't even
1212 * think about putting a printk in there!
1214 * On Intel systems this is entered on all CPUs in parallel through
1215 * MCE broadcast. However some CPUs might be broken beyond repair,
1216 * so be always careful when synchronizing with others.
1218 void do_machine_check(struct pt_regs *regs, long error_code)
1220 DECLARE_BITMAP(valid_banks, MAX_NR_BANKS);
1221 DECLARE_BITMAP(toclear, MAX_NR_BANKS);
1222 struct mca_config *cfg = &mca_cfg;
1223 int cpu = smp_processor_id();
1224 char *msg = "Unknown";
1225 struct mce m, *final;
1229 * Establish sequential order between the CPUs entering the machine
1235 * If no_way_out gets set, there is no safe way to recover from this
1236 * MCE. If mca_cfg.tolerant is cranked up, we'll try anyway.
1241 * If kill_it gets set, there might be a way to recover from this
1247 * MCEs are always local on AMD. Same is determined by MCG_STATUS_LMCES
1252 if (__mc_check_crashing_cpu(cpu))
1257 this_cpu_inc(mce_exception_count);
1259 mce_gather_info(&m, regs);
1262 final = this_cpu_ptr(&mces_seen);
1265 memset(valid_banks, 0, sizeof(valid_banks));
1266 no_way_out = mce_no_way_out(&m, &msg, valid_banks, regs);
1271 * When no restart IP might need to kill or panic.
1272 * Assume the worst for now, but if we find the
1273 * severity is MCE_AR_SEVERITY we have other options.
1275 if (!(m.mcgstatus & MCG_STATUS_RIPV))
1279 * Check if this MCE is signaled to only this logical processor,
1282 if (m.cpuvendor == X86_VENDOR_INTEL)
1283 lmce = m.mcgstatus & MCG_STATUS_LMCES;
1286 * Local machine check may already know that we have to panic.
1287 * Broadcast machine check begins rendezvous in mce_start()
1288 * Go through all banks in exclusion of the other CPUs. This way we
1289 * don't report duplicated events on shared banks because the first one
1290 * to see it will clear it.
1294 mce_panic("Fatal local machine check", &m, msg);
1296 order = mce_start(&no_way_out);
1299 __mc_scan_banks(&m, final, toclear, valid_banks, no_way_out, &worst);
1302 mce_clear_state(toclear);
1305 * Do most of the synchronization with other CPUs.
1306 * When there's any problem use only local no_way_out state.
1309 if (mce_end(order) < 0)
1310 no_way_out = worst >= MCE_PANIC_SEVERITY;
1313 * If there was a fatal machine check we should have
1314 * already called mce_panic earlier in this function.
1315 * Since we re-read the banks, we might have found
1316 * something new. Check again to see if we found a
1317 * fatal error. We call "mce_severity()" again to
1318 * make sure we have the right "msg".
1320 if (worst >= MCE_PANIC_SEVERITY && mca_cfg.tolerant < 3) {
1321 mce_severity(&m, cfg->tolerant, &msg, true);
1322 mce_panic("Local fatal machine check!", &m, msg);
1327 * If tolerant is at an insane level we drop requests to kill
1328 * processes and continue even when there is no way out.
1330 if (cfg->tolerant == 3)
1332 else if (no_way_out)
1333 mce_panic("Fatal machine check on current CPU", &m, msg);
1336 irq_work_queue(&mce_irq_work);
1338 mce_wrmsrl(MSR_IA32_MCG_STATUS, 0);
1342 if (worst != MCE_AR_SEVERITY && !kill_it)
1345 /* Fault was in user mode and we need to take some action */
1346 if ((m.cs & 3) == 3) {
1347 ist_begin_non_atomic(regs);
1350 if (kill_it || do_memory_failure(&m))
1351 force_sig(SIGBUS, current);
1352 local_irq_disable();
1353 ist_end_non_atomic();
1355 if (!fixup_exception(regs, X86_TRAP_MC, error_code, 0))
1356 mce_panic("Failed kernel mode recovery", &m, NULL);
1362 EXPORT_SYMBOL_GPL(do_machine_check);
1364 #ifndef CONFIG_MEMORY_FAILURE
1365 int memory_failure(unsigned long pfn, int flags)
1367 /* mce_severity() should not hand us an ACTION_REQUIRED error */
1368 BUG_ON(flags & MF_ACTION_REQUIRED);
1369 pr_err("Uncorrected memory error in page 0x%lx ignored\n"
1370 "Rebuild kernel with CONFIG_MEMORY_FAILURE=y for smarter handling\n",
1378 * Periodic polling timer for "silent" machine check errors. If the
1379 * poller finds an MCE, poll 2x faster. When the poller finds no more
1380 * errors, poll 2x slower (up to check_interval seconds).
1382 static unsigned long check_interval = INITIAL_CHECK_INTERVAL;
1384 static DEFINE_PER_CPU(unsigned long, mce_next_interval); /* in jiffies */
1385 static DEFINE_PER_CPU(struct timer_list, mce_timer);
1387 static unsigned long mce_adjust_timer_default(unsigned long interval)
1392 static unsigned long (*mce_adjust_timer)(unsigned long interval) = mce_adjust_timer_default;
1394 static void __start_timer(struct timer_list *t, unsigned long interval)
1396 unsigned long when = jiffies + interval;
1397 unsigned long flags;
1399 local_irq_save(flags);
1401 if (!timer_pending(t) || time_before(when, t->expires))
1402 mod_timer(t, round_jiffies(when));
1404 local_irq_restore(flags);
1407 static void mce_timer_fn(struct timer_list *t)
1409 struct timer_list *cpu_t = this_cpu_ptr(&mce_timer);
1412 WARN_ON(cpu_t != t);
1414 iv = __this_cpu_read(mce_next_interval);
1416 if (mce_available(this_cpu_ptr(&cpu_info))) {
1417 machine_check_poll(0, this_cpu_ptr(&mce_poll_banks));
1419 if (mce_intel_cmci_poll()) {
1420 iv = mce_adjust_timer(iv);
1426 * Alert userspace if needed. If we logged an MCE, reduce the polling
1427 * interval, otherwise increase the polling interval.
1429 if (mce_notify_irq())
1430 iv = max(iv / 2, (unsigned long) HZ/100);
1432 iv = min(iv * 2, round_jiffies_relative(check_interval * HZ));
1435 __this_cpu_write(mce_next_interval, iv);
1436 __start_timer(t, iv);
1440 * Ensure that the timer is firing in @interval from now.
1442 void mce_timer_kick(unsigned long interval)
1444 struct timer_list *t = this_cpu_ptr(&mce_timer);
1445 unsigned long iv = __this_cpu_read(mce_next_interval);
1447 __start_timer(t, interval);
1450 __this_cpu_write(mce_next_interval, interval);
1453 /* Must not be called in IRQ context where del_timer_sync() can deadlock */
1454 static void mce_timer_delete_all(void)
1458 for_each_online_cpu(cpu)
1459 del_timer_sync(&per_cpu(mce_timer, cpu));
1463 * Notify the user(s) about new machine check events.
1464 * Can be called from interrupt context, but not from machine check/NMI
1467 int mce_notify_irq(void)
1469 /* Not more than two messages every minute */
1470 static DEFINE_RATELIMIT_STATE(ratelimit, 60*HZ, 2);
1472 if (test_and_clear_bit(0, &mce_need_notify)) {
1475 if (__ratelimit(&ratelimit))
1476 pr_info(HW_ERR "Machine check events logged\n");
1482 EXPORT_SYMBOL_GPL(mce_notify_irq);
1484 static void __mcheck_cpu_mce_banks_init(void)
1486 struct mce_bank *mce_banks = this_cpu_ptr(mce_banks_array);
1487 u8 n_banks = this_cpu_read(mce_num_banks);
1490 for (i = 0; i < n_banks; i++) {
1491 struct mce_bank *b = &mce_banks[i];
1494 * Init them all, __mcheck_cpu_apply_quirks() is going to apply
1495 * the required vendor quirks before
1496 * __mcheck_cpu_init_clear_banks() does the final bank setup.
1504 * Initialize Machine Checks for a CPU.
1506 static void __mcheck_cpu_cap_init(void)
1511 rdmsrl(MSR_IA32_MCG_CAP, cap);
1513 b = cap & MCG_BANKCNT_MASK;
1515 if (b > MAX_NR_BANKS) {
1516 pr_warn("CPU%d: Using only %u machine check banks out of %u\n",
1517 smp_processor_id(), MAX_NR_BANKS, b);
1521 this_cpu_write(mce_num_banks, b);
1523 __mcheck_cpu_mce_banks_init();
1525 /* Use accurate RIP reporting if available. */
1526 if ((cap & MCG_EXT_P) && MCG_EXT_CNT(cap) >= 9)
1527 mca_cfg.rip_msr = MSR_IA32_MCG_EIP;
1529 if (cap & MCG_SER_P)
1533 static void __mcheck_cpu_init_generic(void)
1535 enum mcp_flags m_fl = 0;
1536 mce_banks_t all_banks;
1539 if (!mca_cfg.bootlog)
1543 * Log the machine checks left over from the previous reset.
1545 bitmap_fill(all_banks, MAX_NR_BANKS);
1546 machine_check_poll(MCP_UC | m_fl, &all_banks);
1548 cr4_set_bits(X86_CR4_MCE);
1550 rdmsrl(MSR_IA32_MCG_CAP, cap);
1551 if (cap & MCG_CTL_P)
1552 wrmsr(MSR_IA32_MCG_CTL, 0xffffffff, 0xffffffff);
1555 static void __mcheck_cpu_init_clear_banks(void)
1557 struct mce_bank *mce_banks = this_cpu_ptr(mce_banks_array);
1560 for (i = 0; i < this_cpu_read(mce_num_banks); i++) {
1561 struct mce_bank *b = &mce_banks[i];
1565 wrmsrl(msr_ops.ctl(i), b->ctl);
1566 wrmsrl(msr_ops.status(i), 0);
1571 * Do a final check to see if there are any unused/RAZ banks.
1573 * This must be done after the banks have been initialized and any quirks have
1576 * Do not call this from any user-initiated flows, e.g. CPU hotplug or sysfs.
1577 * Otherwise, a user who disables a bank will not be able to re-enable it
1578 * without a system reboot.
1580 static void __mcheck_cpu_check_banks(void)
1582 struct mce_bank *mce_banks = this_cpu_ptr(mce_banks_array);
1586 for (i = 0; i < this_cpu_read(mce_num_banks); i++) {
1587 struct mce_bank *b = &mce_banks[i];
1592 rdmsrl(msr_ops.ctl(i), msrval);
1598 * During IFU recovery Sandy Bridge -EP4S processors set the RIPV and
1599 * EIPV bits in MCG_STATUS to zero on the affected logical processor (SDM
1600 * Vol 3B Table 15-20). But this confuses both the code that determines
1601 * whether the machine check occurred in kernel or user mode, and also
1602 * the severity assessment code. Pretend that EIPV was set, and take the
1603 * ip/cs values from the pt_regs that mce_gather_info() ignored earlier.
1605 static void quirk_sandybridge_ifu(int bank, struct mce *m, struct pt_regs *regs)
1609 if ((m->mcgstatus & (MCG_STATUS_EIPV|MCG_STATUS_RIPV)) != 0)
1611 if ((m->status & (MCI_STATUS_OVER|MCI_STATUS_UC|
1612 MCI_STATUS_EN|MCI_STATUS_MISCV|MCI_STATUS_ADDRV|
1613 MCI_STATUS_PCC|MCI_STATUS_S|MCI_STATUS_AR|
1615 (MCI_STATUS_UC|MCI_STATUS_EN|
1616 MCI_STATUS_MISCV|MCI_STATUS_ADDRV|MCI_STATUS_S|
1617 MCI_STATUS_AR|MCACOD_INSTR))
1620 m->mcgstatus |= MCG_STATUS_EIPV;
1625 /* Add per CPU specific workarounds here */
1626 static int __mcheck_cpu_apply_quirks(struct cpuinfo_x86 *c)
1628 struct mce_bank *mce_banks = this_cpu_ptr(mce_banks_array);
1629 struct mca_config *cfg = &mca_cfg;
1631 if (c->x86_vendor == X86_VENDOR_UNKNOWN) {
1632 pr_info("unknown CPU type - not enabling MCE support\n");
1636 /* This should be disabled by the BIOS, but isn't always */
1637 if (c->x86_vendor == X86_VENDOR_AMD) {
1638 if (c->x86 == 15 && this_cpu_read(mce_num_banks) > 4) {
1640 * disable GART TBL walk error reporting, which
1641 * trips off incorrectly with the IOMMU & 3ware
1644 clear_bit(10, (unsigned long *)&mce_banks[4].ctl);
1646 if (c->x86 < 0x11 && cfg->bootlog < 0) {
1648 * Lots of broken BIOS around that don't clear them
1649 * by default and leave crap in there. Don't log:
1654 * Various K7s with broken bank 0 around. Always disable
1657 if (c->x86 == 6 && this_cpu_read(mce_num_banks) > 0)
1658 mce_banks[0].ctl = 0;
1661 * overflow_recov is supported for F15h Models 00h-0fh
1662 * even though we don't have a CPUID bit for it.
1664 if (c->x86 == 0x15 && c->x86_model <= 0xf)
1665 mce_flags.overflow_recov = 1;
1669 if (c->x86_vendor == X86_VENDOR_INTEL) {
1671 * SDM documents that on family 6 bank 0 should not be written
1672 * because it aliases to another special BIOS controlled
1674 * But it's not aliased anymore on model 0x1a+
1675 * Don't ignore bank 0 completely because there could be a
1676 * valid event later, merely don't write CTL0.
1679 if (c->x86 == 6 && c->x86_model < 0x1A && this_cpu_read(mce_num_banks) > 0)
1680 mce_banks[0].init = 0;
1683 * All newer Intel systems support MCE broadcasting. Enable
1684 * synchronization with a one second timeout.
1686 if ((c->x86 > 6 || (c->x86 == 6 && c->x86_model >= 0xe)) &&
1687 cfg->monarch_timeout < 0)
1688 cfg->monarch_timeout = USEC_PER_SEC;
1691 * There are also broken BIOSes on some Pentium M and
1694 if (c->x86 == 6 && c->x86_model <= 13 && cfg->bootlog < 0)
1697 if (c->x86 == 6 && c->x86_model == 45)
1698 quirk_no_way_out = quirk_sandybridge_ifu;
1700 if (cfg->monarch_timeout < 0)
1701 cfg->monarch_timeout = 0;
1702 if (cfg->bootlog != 0)
1703 cfg->panic_timeout = 30;
1708 static int __mcheck_cpu_ancient_init(struct cpuinfo_x86 *c)
1713 switch (c->x86_vendor) {
1714 case X86_VENDOR_INTEL:
1715 intel_p5_mcheck_init(c);
1718 case X86_VENDOR_CENTAUR:
1719 winchip_mcheck_init(c);
1730 * Init basic CPU features needed for early decoding of MCEs.
1732 static void __mcheck_cpu_init_early(struct cpuinfo_x86 *c)
1734 if (c->x86_vendor == X86_VENDOR_AMD || c->x86_vendor == X86_VENDOR_HYGON) {
1735 mce_flags.overflow_recov = !!cpu_has(c, X86_FEATURE_OVERFLOW_RECOV);
1736 mce_flags.succor = !!cpu_has(c, X86_FEATURE_SUCCOR);
1737 mce_flags.smca = !!cpu_has(c, X86_FEATURE_SMCA);
1739 if (mce_flags.smca) {
1740 msr_ops.ctl = smca_ctl_reg;
1741 msr_ops.status = smca_status_reg;
1742 msr_ops.addr = smca_addr_reg;
1743 msr_ops.misc = smca_misc_reg;
1748 static void mce_centaur_feature_init(struct cpuinfo_x86 *c)
1750 struct mca_config *cfg = &mca_cfg;
1753 * All newer Centaur CPUs support MCE broadcasting. Enable
1754 * synchronization with a one second timeout.
1756 if ((c->x86 == 6 && c->x86_model == 0xf && c->x86_stepping >= 0xe) ||
1758 if (cfg->monarch_timeout < 0)
1759 cfg->monarch_timeout = USEC_PER_SEC;
1763 static void __mcheck_cpu_init_vendor(struct cpuinfo_x86 *c)
1765 switch (c->x86_vendor) {
1766 case X86_VENDOR_INTEL:
1767 mce_intel_feature_init(c);
1768 mce_adjust_timer = cmci_intel_adjust_timer;
1771 case X86_VENDOR_AMD: {
1772 mce_amd_feature_init(c);
1776 case X86_VENDOR_HYGON:
1777 mce_hygon_feature_init(c);
1780 case X86_VENDOR_CENTAUR:
1781 mce_centaur_feature_init(c);
1789 static void __mcheck_cpu_clear_vendor(struct cpuinfo_x86 *c)
1791 switch (c->x86_vendor) {
1792 case X86_VENDOR_INTEL:
1793 mce_intel_feature_clear(c);
1800 static void mce_start_timer(struct timer_list *t)
1802 unsigned long iv = check_interval * HZ;
1804 if (mca_cfg.ignore_ce || !iv)
1807 this_cpu_write(mce_next_interval, iv);
1808 __start_timer(t, iv);
1811 static void __mcheck_cpu_setup_timer(void)
1813 struct timer_list *t = this_cpu_ptr(&mce_timer);
1815 timer_setup(t, mce_timer_fn, TIMER_PINNED);
1818 static void __mcheck_cpu_init_timer(void)
1820 struct timer_list *t = this_cpu_ptr(&mce_timer);
1822 timer_setup(t, mce_timer_fn, TIMER_PINNED);
1826 bool filter_mce(struct mce *m)
1828 if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
1829 return amd_filter_mce(m);
1834 /* Handle unconfigured int18 (should never happen) */
1835 static void unexpected_machine_check(struct pt_regs *regs, long error_code)
1837 pr_err("CPU#%d: Unexpected int18 (Machine Check)\n",
1838 smp_processor_id());
1841 /* Call the installed machine check handler for this CPU setup. */
1842 void (*machine_check_vector)(struct pt_regs *, long error_code) =
1843 unexpected_machine_check;
1845 dotraplinkage void do_mce(struct pt_regs *regs, long error_code)
1847 machine_check_vector(regs, error_code);
1851 * Called for each booted CPU to set up machine checks.
1852 * Must be called with preempt off:
1854 void mcheck_cpu_init(struct cpuinfo_x86 *c)
1856 if (mca_cfg.disabled)
1859 if (__mcheck_cpu_ancient_init(c))
1862 if (!mce_available(c))
1865 __mcheck_cpu_cap_init();
1867 if (__mcheck_cpu_apply_quirks(c) < 0) {
1868 mca_cfg.disabled = 1;
1872 if (mce_gen_pool_init()) {
1873 mca_cfg.disabled = 1;
1874 pr_emerg("Couldn't allocate MCE records pool!\n");
1878 machine_check_vector = do_machine_check;
1880 __mcheck_cpu_init_early(c);
1881 __mcheck_cpu_init_generic();
1882 __mcheck_cpu_init_vendor(c);
1883 __mcheck_cpu_init_clear_banks();
1884 __mcheck_cpu_check_banks();
1885 __mcheck_cpu_setup_timer();
1889 * Called for each booted CPU to clear some machine checks opt-ins
1891 void mcheck_cpu_clear(struct cpuinfo_x86 *c)
1893 if (mca_cfg.disabled)
1896 if (!mce_available(c))
1900 * Possibly to clear general settings generic to x86
1901 * __mcheck_cpu_clear_generic(c);
1903 __mcheck_cpu_clear_vendor(c);
1907 static void __mce_disable_bank(void *arg)
1909 int bank = *((int *)arg);
1910 __clear_bit(bank, this_cpu_ptr(mce_poll_banks));
1911 cmci_disable_bank(bank);
1914 void mce_disable_bank(int bank)
1916 if (bank >= this_cpu_read(mce_num_banks)) {
1918 "Ignoring request to disable invalid MCA bank %d.\n",
1922 set_bit(bank, mce_banks_ce_disabled);
1923 on_each_cpu(__mce_disable_bank, &bank, 1);
1927 * mce=off Disables machine check
1928 * mce=no_cmci Disables CMCI
1929 * mce=no_lmce Disables LMCE
1930 * mce=dont_log_ce Clears corrected events silently, no log created for CEs.
1931 * mce=ignore_ce Disables polling and CMCI, corrected events are not cleared.
1932 * mce=TOLERANCELEVEL[,monarchtimeout] (number, see above)
1933 * monarchtimeout is how long to wait for other CPUs on machine
1934 * check, or 0 to not wait
1935 * mce=bootlog Log MCEs from before booting. Disabled by default on AMD Fam10h
1937 * mce=nobootlog Don't log MCEs from before booting.
1938 * mce=bios_cmci_threshold Don't program the CMCI threshold
1939 * mce=recovery force enable memcpy_mcsafe()
1941 static int __init mcheck_enable(char *str)
1943 struct mca_config *cfg = &mca_cfg;
1951 if (!strcmp(str, "off"))
1953 else if (!strcmp(str, "no_cmci"))
1954 cfg->cmci_disabled = true;
1955 else if (!strcmp(str, "no_lmce"))
1956 cfg->lmce_disabled = 1;
1957 else if (!strcmp(str, "dont_log_ce"))
1958 cfg->dont_log_ce = true;
1959 else if (!strcmp(str, "ignore_ce"))
1960 cfg->ignore_ce = true;
1961 else if (!strcmp(str, "bootlog") || !strcmp(str, "nobootlog"))
1962 cfg->bootlog = (str[0] == 'b');
1963 else if (!strcmp(str, "bios_cmci_threshold"))
1964 cfg->bios_cmci_threshold = 1;
1965 else if (!strcmp(str, "recovery"))
1967 else if (isdigit(str[0])) {
1968 if (get_option(&str, &cfg->tolerant) == 2)
1969 get_option(&str, &(cfg->monarch_timeout));
1971 pr_info("mce argument %s ignored. Please use /sys\n", str);
1976 __setup("mce", mcheck_enable);
1978 int __init mcheck_init(void)
1980 mcheck_intel_therm_init();
1981 mce_register_decode_chain(&first_nb);
1982 mce_register_decode_chain(&mce_srao_nb);
1983 mce_register_decode_chain(&mce_default_nb);
1984 mcheck_vendor_init_severity();
1986 INIT_WORK(&mce_work, mce_gen_pool_process);
1987 init_irq_work(&mce_irq_work, mce_irq_work_cb);
1993 * mce_syscore: PM support
1997 * Disable machine checks on suspend and shutdown. We can't really handle
2000 static void mce_disable_error_reporting(void)
2002 struct mce_bank *mce_banks = this_cpu_ptr(mce_banks_array);
2005 for (i = 0; i < this_cpu_read(mce_num_banks); i++) {
2006 struct mce_bank *b = &mce_banks[i];
2009 wrmsrl(msr_ops.ctl(i), 0);
2014 static void vendor_disable_error_reporting(void)
2017 * Don't clear on Intel or AMD or Hygon CPUs. Some of these MSRs
2019 * Disabling them for just a single offlined CPU is bad, since it will
2020 * inhibit reporting for all shared resources on the socket like the
2021 * last level cache (LLC), the integrated memory controller (iMC), etc.
2023 if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL ||
2024 boot_cpu_data.x86_vendor == X86_VENDOR_HYGON ||
2025 boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
2028 mce_disable_error_reporting();
2031 static int mce_syscore_suspend(void)
2033 vendor_disable_error_reporting();
2037 static void mce_syscore_shutdown(void)
2039 vendor_disable_error_reporting();
2043 * On resume clear all MCE state. Don't want to see leftovers from the BIOS.
2044 * Only one CPU is active at this time, the others get re-added later using
2047 static void mce_syscore_resume(void)
2049 __mcheck_cpu_init_generic();
2050 __mcheck_cpu_init_vendor(raw_cpu_ptr(&cpu_info));
2051 __mcheck_cpu_init_clear_banks();
2054 static struct syscore_ops mce_syscore_ops = {
2055 .suspend = mce_syscore_suspend,
2056 .shutdown = mce_syscore_shutdown,
2057 .resume = mce_syscore_resume,
2061 * mce_device: Sysfs support
2064 static void mce_cpu_restart(void *data)
2066 if (!mce_available(raw_cpu_ptr(&cpu_info)))
2068 __mcheck_cpu_init_generic();
2069 __mcheck_cpu_init_clear_banks();
2070 __mcheck_cpu_init_timer();
2073 /* Reinit MCEs after user configuration changes */
2074 static void mce_restart(void)
2076 mce_timer_delete_all();
2077 on_each_cpu(mce_cpu_restart, NULL, 1);
2080 /* Toggle features for corrected errors */
2081 static void mce_disable_cmci(void *data)
2083 if (!mce_available(raw_cpu_ptr(&cpu_info)))
2088 static void mce_enable_ce(void *all)
2090 if (!mce_available(raw_cpu_ptr(&cpu_info)))
2095 __mcheck_cpu_init_timer();
2098 static struct bus_type mce_subsys = {
2099 .name = "machinecheck",
2100 .dev_name = "machinecheck",
2103 DEFINE_PER_CPU(struct device *, mce_device);
2105 static inline struct mce_bank_dev *attr_to_bank(struct device_attribute *attr)
2107 return container_of(attr, struct mce_bank_dev, attr);
2110 static ssize_t show_bank(struct device *s, struct device_attribute *attr,
2113 u8 bank = attr_to_bank(attr)->bank;
2116 if (bank >= per_cpu(mce_num_banks, s->id))
2119 b = &per_cpu(mce_banks_array, s->id)[bank];
2124 return sprintf(buf, "%llx\n", b->ctl);
2127 static ssize_t set_bank(struct device *s, struct device_attribute *attr,
2128 const char *buf, size_t size)
2130 u8 bank = attr_to_bank(attr)->bank;
2134 if (kstrtou64(buf, 0, &new) < 0)
2137 if (bank >= per_cpu(mce_num_banks, s->id))
2140 b = &per_cpu(mce_banks_array, s->id)[bank];
2151 static ssize_t set_ignore_ce(struct device *s,
2152 struct device_attribute *attr,
2153 const char *buf, size_t size)
2157 if (kstrtou64(buf, 0, &new) < 0)
2160 mutex_lock(&mce_sysfs_mutex);
2161 if (mca_cfg.ignore_ce ^ !!new) {
2163 /* disable ce features */
2164 mce_timer_delete_all();
2165 on_each_cpu(mce_disable_cmci, NULL, 1);
2166 mca_cfg.ignore_ce = true;
2168 /* enable ce features */
2169 mca_cfg.ignore_ce = false;
2170 on_each_cpu(mce_enable_ce, (void *)1, 1);
2173 mutex_unlock(&mce_sysfs_mutex);
2178 static ssize_t set_cmci_disabled(struct device *s,
2179 struct device_attribute *attr,
2180 const char *buf, size_t size)
2184 if (kstrtou64(buf, 0, &new) < 0)
2187 mutex_lock(&mce_sysfs_mutex);
2188 if (mca_cfg.cmci_disabled ^ !!new) {
2191 on_each_cpu(mce_disable_cmci, NULL, 1);
2192 mca_cfg.cmci_disabled = true;
2195 mca_cfg.cmci_disabled = false;
2196 on_each_cpu(mce_enable_ce, NULL, 1);
2199 mutex_unlock(&mce_sysfs_mutex);
2204 static ssize_t store_int_with_restart(struct device *s,
2205 struct device_attribute *attr,
2206 const char *buf, size_t size)
2208 unsigned long old_check_interval = check_interval;
2209 ssize_t ret = device_store_ulong(s, attr, buf, size);
2211 if (check_interval == old_check_interval)
2214 mutex_lock(&mce_sysfs_mutex);
2216 mutex_unlock(&mce_sysfs_mutex);
2221 static DEVICE_INT_ATTR(tolerant, 0644, mca_cfg.tolerant);
2222 static DEVICE_INT_ATTR(monarch_timeout, 0644, mca_cfg.monarch_timeout);
2223 static DEVICE_BOOL_ATTR(dont_log_ce, 0644, mca_cfg.dont_log_ce);
2225 static struct dev_ext_attribute dev_attr_check_interval = {
2226 __ATTR(check_interval, 0644, device_show_int, store_int_with_restart),
2230 static struct dev_ext_attribute dev_attr_ignore_ce = {
2231 __ATTR(ignore_ce, 0644, device_show_bool, set_ignore_ce),
2235 static struct dev_ext_attribute dev_attr_cmci_disabled = {
2236 __ATTR(cmci_disabled, 0644, device_show_bool, set_cmci_disabled),
2237 &mca_cfg.cmci_disabled
2240 static struct device_attribute *mce_device_attrs[] = {
2241 &dev_attr_tolerant.attr,
2242 &dev_attr_check_interval.attr,
2243 #ifdef CONFIG_X86_MCELOG_LEGACY
2246 &dev_attr_monarch_timeout.attr,
2247 &dev_attr_dont_log_ce.attr,
2248 &dev_attr_ignore_ce.attr,
2249 &dev_attr_cmci_disabled.attr,
2253 static cpumask_var_t mce_device_initialized;
2255 static void mce_device_release(struct device *dev)
2260 /* Per CPU device init. All of the CPUs still share the same bank device: */
2261 static int mce_device_create(unsigned int cpu)
2267 if (!mce_available(&boot_cpu_data))
2270 dev = per_cpu(mce_device, cpu);
2274 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2278 dev->bus = &mce_subsys;
2279 dev->release = &mce_device_release;
2281 err = device_register(dev);
2287 for (i = 0; mce_device_attrs[i]; i++) {
2288 err = device_create_file(dev, mce_device_attrs[i]);
2292 for (j = 0; j < per_cpu(mce_num_banks, cpu); j++) {
2293 err = device_create_file(dev, &mce_bank_devs[j].attr);
2297 cpumask_set_cpu(cpu, mce_device_initialized);
2298 per_cpu(mce_device, cpu) = dev;
2303 device_remove_file(dev, &mce_bank_devs[j].attr);
2306 device_remove_file(dev, mce_device_attrs[i]);
2308 device_unregister(dev);
2313 static void mce_device_remove(unsigned int cpu)
2315 struct device *dev = per_cpu(mce_device, cpu);
2318 if (!cpumask_test_cpu(cpu, mce_device_initialized))
2321 for (i = 0; mce_device_attrs[i]; i++)
2322 device_remove_file(dev, mce_device_attrs[i]);
2324 for (i = 0; i < per_cpu(mce_num_banks, cpu); i++)
2325 device_remove_file(dev, &mce_bank_devs[i].attr);
2327 device_unregister(dev);
2328 cpumask_clear_cpu(cpu, mce_device_initialized);
2329 per_cpu(mce_device, cpu) = NULL;
2332 /* Make sure there are no machine checks on offlined CPUs. */
2333 static void mce_disable_cpu(void)
2335 if (!mce_available(raw_cpu_ptr(&cpu_info)))
2338 if (!cpuhp_tasks_frozen)
2341 vendor_disable_error_reporting();
2344 static void mce_reenable_cpu(void)
2346 struct mce_bank *mce_banks = this_cpu_ptr(mce_banks_array);
2349 if (!mce_available(raw_cpu_ptr(&cpu_info)))
2352 if (!cpuhp_tasks_frozen)
2354 for (i = 0; i < this_cpu_read(mce_num_banks); i++) {
2355 struct mce_bank *b = &mce_banks[i];
2358 wrmsrl(msr_ops.ctl(i), b->ctl);
2362 static int mce_cpu_dead(unsigned int cpu)
2364 mce_intel_hcpu_update(cpu);
2366 /* intentionally ignoring frozen here */
2367 if (!cpuhp_tasks_frozen)
2372 static int mce_cpu_online(unsigned int cpu)
2374 struct timer_list *t = this_cpu_ptr(&mce_timer);
2377 mce_device_create(cpu);
2379 ret = mce_threshold_create_device(cpu);
2381 mce_device_remove(cpu);
2389 static int mce_cpu_pre_down(unsigned int cpu)
2391 struct timer_list *t = this_cpu_ptr(&mce_timer);
2395 mce_threshold_remove_device(cpu);
2396 mce_device_remove(cpu);
2400 static __init void mce_init_banks(void)
2404 for (i = 0; i < MAX_NR_BANKS; i++) {
2405 struct mce_bank_dev *b = &mce_bank_devs[i];
2406 struct device_attribute *a = &b->attr;
2410 sysfs_attr_init(&a->attr);
2411 a->attr.name = b->attrname;
2412 snprintf(b->attrname, ATTR_LEN, "bank%d", i);
2414 a->attr.mode = 0644;
2415 a->show = show_bank;
2416 a->store = set_bank;
2420 static __init int mcheck_init_device(void)
2425 * Check if we have a spare virtual bit. This will only become
2426 * a problem if/when we move beyond 5-level page tables.
2428 MAYBE_BUILD_BUG_ON(__VIRTUAL_MASK_SHIFT >= 63);
2430 if (!mce_available(&boot_cpu_data)) {
2435 if (!zalloc_cpumask_var(&mce_device_initialized, GFP_KERNEL)) {
2442 err = subsys_system_register(&mce_subsys, NULL);
2446 err = cpuhp_setup_state(CPUHP_X86_MCE_DEAD, "x86/mce:dead", NULL,
2451 err = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "x86/mce:online",
2452 mce_cpu_online, mce_cpu_pre_down);
2454 goto err_out_online;
2456 register_syscore_ops(&mce_syscore_ops);
2461 cpuhp_remove_state(CPUHP_X86_MCE_DEAD);
2464 free_cpumask_var(mce_device_initialized);
2467 pr_err("Unable to init MCE device (rc: %d)\n", err);
2471 device_initcall_sync(mcheck_init_device);
2474 * Old style boot options parsing. Only for compatibility.
2476 static int __init mcheck_disable(char *str)
2478 mca_cfg.disabled = 1;
2481 __setup("nomce", mcheck_disable);
2483 #ifdef CONFIG_DEBUG_FS
2484 struct dentry *mce_get_debugfs_dir(void)
2486 static struct dentry *dmce;
2489 dmce = debugfs_create_dir("mce", NULL);
2494 static void mce_reset(void)
2497 atomic_set(&mce_fake_panicked, 0);
2498 atomic_set(&mce_executing, 0);
2499 atomic_set(&mce_callin, 0);
2500 atomic_set(&global_nwo, 0);
2503 static int fake_panic_get(void *data, u64 *val)
2509 static int fake_panic_set(void *data, u64 val)
2516 DEFINE_DEBUGFS_ATTRIBUTE(fake_panic_fops, fake_panic_get, fake_panic_set,
2519 static void __init mcheck_debugfs_init(void)
2521 struct dentry *dmce;
2523 dmce = mce_get_debugfs_dir();
2524 debugfs_create_file_unsafe("fake_panic", 0444, dmce, NULL,
2528 static void __init mcheck_debugfs_init(void) { }
2531 DEFINE_STATIC_KEY_FALSE(mcsafe_key);
2532 EXPORT_SYMBOL_GPL(mcsafe_key);
2534 static int __init mcheck_late_init(void)
2536 if (mca_cfg.recovery)
2537 static_branch_inc(&mcsafe_key);
2539 mcheck_debugfs_init();
2543 * Flush out everything that has been logged during early boot, now that
2544 * everything has been initialized (workqueues, decoders, ...).
2546 mce_schedule_work();
2550 late_initcall(mcheck_late_init);