2 * Machine check handler.
4 * K8 parts Copyright 2002,2003 Andi Kleen, SuSE Labs.
5 * Rest from unknown author(s).
6 * 2004 Andi Kleen. Rewrote most of it.
7 * Copyright 2008 Intel Corporation
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13 #include <linux/thread_info.h>
14 #include <linux/capability.h>
15 #include <linux/miscdevice.h>
16 #include <linux/ratelimit.h>
17 #include <linux/rcupdate.h>
18 #include <linux/kobject.h>
19 #include <linux/uaccess.h>
20 #include <linux/kdebug.h>
21 #include <linux/kernel.h>
22 #include <linux/percpu.h>
23 #include <linux/string.h>
24 #include <linux/device.h>
25 #include <linux/syscore_ops.h>
26 #include <linux/delay.h>
27 #include <linux/ctype.h>
28 #include <linux/sched.h>
29 #include <linux/sysfs.h>
30 #include <linux/types.h>
31 #include <linux/slab.h>
32 #include <linux/init.h>
33 #include <linux/kmod.h>
34 #include <linux/poll.h>
35 #include <linux/nmi.h>
36 #include <linux/cpu.h>
37 #include <linux/ras.h>
38 #include <linux/smp.h>
41 #include <linux/debugfs.h>
42 #include <linux/irq_work.h>
43 #include <linux/export.h>
44 #include <linux/jump_label.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>
53 #include <asm/set_memory.h>
55 #include "mce-internal.h"
57 static DEFINE_MUTEX(mce_log_mutex);
59 #define CREATE_TRACE_POINTS
60 #include <trace/events/mce.h>
62 #define SPINUNIT 100 /* 100ns */
64 DEFINE_PER_CPU(unsigned, mce_exception_count);
66 struct mce_bank *mce_banks __read_mostly;
67 struct mce_vendor_flags mce_flags __read_mostly;
69 struct mca_config mca_cfg __read_mostly = {
73 * 0: always panic on uncorrected errors, log corrected errors
74 * 1: panic or SIGBUS on uncorrected errors, log corrected errors
75 * 2: SIGBUS or log uncorrected errors (if possible), log corr. errors
76 * 3: never panic or SIGBUS, log all errors (for testing only)
82 static DEFINE_PER_CPU(struct mce, mces_seen);
83 static unsigned long mce_need_notify;
84 static int cpu_missing;
87 * MCA banks polled by the period polling timer for corrected events.
88 * With Intel CMCI, this only has MCA banks which do not support CMCI (if any).
90 DEFINE_PER_CPU(mce_banks_t, mce_poll_banks) = {
91 [0 ... BITS_TO_LONGS(MAX_NR_BANKS)-1] = ~0UL
95 * MCA banks controlled through firmware first for corrected errors.
96 * This is a global list of banks for which we won't enable CMCI and we
97 * won't poll. Firmware controls these banks and is responsible for
98 * reporting corrected errors through GHES. Uncorrected/recoverable
99 * errors are still notified through a machine check.
101 mce_banks_t mce_banks_ce_disabled;
103 static struct work_struct mce_work;
104 static struct irq_work mce_irq_work;
106 static void (*quirk_no_way_out)(int bank, struct mce *m, struct pt_regs *regs);
108 #ifndef mce_unmap_kpfn
109 static void mce_unmap_kpfn(unsigned long pfn);
113 * CPU/chipset specific EDAC code can register a notifier call here to print
114 * MCE errors in a human-readable form.
116 BLOCKING_NOTIFIER_HEAD(x86_mce_decoder_chain);
118 /* Do initial initialization of a struct mce */
119 void mce_setup(struct mce *m)
121 memset(m, 0, sizeof(struct mce));
122 m->cpu = m->extcpu = smp_processor_id();
123 /* We hope get_seconds stays lockless */
124 m->time = get_seconds();
125 m->cpuvendor = boot_cpu_data.x86_vendor;
126 m->cpuid = cpuid_eax(1);
127 m->socketid = cpu_data(m->extcpu).phys_proc_id;
128 m->apicid = cpu_data(m->extcpu).initial_apicid;
129 rdmsrl(MSR_IA32_MCG_CAP, m->mcgcap);
131 if (this_cpu_has(X86_FEATURE_INTEL_PPIN))
132 rdmsrl(MSR_PPIN, m->ppin);
134 m->microcode = boot_cpu_data.microcode;
137 DEFINE_PER_CPU(struct mce, injectm);
138 EXPORT_PER_CPU_SYMBOL_GPL(injectm);
140 void mce_log(struct mce *m)
142 if (!mce_gen_pool_add(m))
143 irq_work_queue(&mce_irq_work);
146 void mce_inject_log(struct mce *m)
148 mutex_lock(&mce_log_mutex);
150 mutex_unlock(&mce_log_mutex);
152 EXPORT_SYMBOL_GPL(mce_inject_log);
154 static struct notifier_block mce_srao_nb;
157 * We run the default notifier if we have only the SRAO, the first and the
158 * default notifier registered. I.e., the mandatory NUM_DEFAULT_NOTIFIERS
159 * notifiers registered on the chain.
161 #define NUM_DEFAULT_NOTIFIERS 3
162 static atomic_t num_notifiers;
164 void mce_register_decode_chain(struct notifier_block *nb)
166 if (WARN_ON(nb->priority > MCE_PRIO_MCELOG && nb->priority < MCE_PRIO_EDAC))
169 atomic_inc(&num_notifiers);
171 blocking_notifier_chain_register(&x86_mce_decoder_chain, nb);
173 EXPORT_SYMBOL_GPL(mce_register_decode_chain);
175 void mce_unregister_decode_chain(struct notifier_block *nb)
177 atomic_dec(&num_notifiers);
179 blocking_notifier_chain_unregister(&x86_mce_decoder_chain, nb);
181 EXPORT_SYMBOL_GPL(mce_unregister_decode_chain);
183 static inline u32 ctl_reg(int bank)
185 return MSR_IA32_MCx_CTL(bank);
188 static inline u32 status_reg(int bank)
190 return MSR_IA32_MCx_STATUS(bank);
193 static inline u32 addr_reg(int bank)
195 return MSR_IA32_MCx_ADDR(bank);
198 static inline u32 misc_reg(int bank)
200 return MSR_IA32_MCx_MISC(bank);
203 static inline u32 smca_ctl_reg(int bank)
205 return MSR_AMD64_SMCA_MCx_CTL(bank);
208 static inline u32 smca_status_reg(int bank)
210 return MSR_AMD64_SMCA_MCx_STATUS(bank);
213 static inline u32 smca_addr_reg(int bank)
215 return MSR_AMD64_SMCA_MCx_ADDR(bank);
218 static inline u32 smca_misc_reg(int bank)
220 return MSR_AMD64_SMCA_MCx_MISC(bank);
223 struct mca_msr_regs msr_ops = {
225 .status = status_reg,
230 static void __print_mce(struct mce *m)
232 pr_emerg(HW_ERR "CPU %d: Machine Check%s: %Lx Bank %d: %016Lx\n",
234 (m->mcgstatus & MCG_STATUS_MCIP ? " Exception" : ""),
235 m->mcgstatus, m->bank, m->status);
238 pr_emerg(HW_ERR "RIP%s %02x:<%016Lx> ",
239 !(m->mcgstatus & MCG_STATUS_EIPV) ? " !INEXACT!" : "",
242 if (m->cs == __KERNEL_CS)
243 pr_cont("{%pS}", (void *)(unsigned long)m->ip);
247 pr_emerg(HW_ERR "TSC %llx ", m->tsc);
249 pr_cont("ADDR %llx ", m->addr);
251 pr_cont("MISC %llx ", m->misc);
253 if (mce_flags.smca) {
255 pr_cont("SYND %llx ", m->synd);
257 pr_cont("IPID %llx ", m->ipid);
262 * Note this output is parsed by external tools and old fields
263 * should not be changed.
265 pr_emerg(HW_ERR "PROCESSOR %u:%x TIME %llu SOCKET %u APIC %x microcode %x\n",
266 m->cpuvendor, m->cpuid, m->time, m->socketid, m->apicid,
270 static void print_mce(struct mce *m)
273 pr_emerg_ratelimited(HW_ERR "Run the above through 'mcelog --ascii'\n");
276 #define PANIC_TIMEOUT 5 /* 5 seconds */
278 static atomic_t mce_panicked;
280 static int fake_panic;
281 static atomic_t mce_fake_panicked;
283 /* Panic in progress. Enable interrupts and wait for final IPI */
284 static void wait_for_panic(void)
286 long timeout = PANIC_TIMEOUT*USEC_PER_SEC;
290 while (timeout-- > 0)
292 if (panic_timeout == 0)
293 panic_timeout = mca_cfg.panic_timeout;
294 panic("Panicing machine check CPU died");
297 static void mce_panic(const char *msg, struct mce *final, char *exp)
300 struct llist_node *pending;
301 struct mce_evt_llist *l;
305 * Make sure only one CPU runs in machine check panic
307 if (atomic_inc_return(&mce_panicked) > 1)
314 /* Don't log too much for fake panic */
315 if (atomic_inc_return(&mce_fake_panicked) > 1)
318 pending = mce_gen_pool_prepare_records();
319 /* First print corrected ones that are still unlogged */
320 llist_for_each_entry(l, pending, llnode) {
321 struct mce *m = &l->mce;
322 if (!(m->status & MCI_STATUS_UC)) {
325 apei_err = apei_write_mce(m);
328 /* Now print uncorrected but with the final one last */
329 llist_for_each_entry(l, pending, llnode) {
330 struct mce *m = &l->mce;
331 if (!(m->status & MCI_STATUS_UC))
333 if (!final || mce_cmp(m, final)) {
336 apei_err = apei_write_mce(m);
342 apei_err = apei_write_mce(final);
345 pr_emerg(HW_ERR "Some CPUs didn't answer in synchronization\n");
347 pr_emerg(HW_ERR "Machine check: %s\n", exp);
349 if (panic_timeout == 0)
350 panic_timeout = mca_cfg.panic_timeout;
353 pr_emerg(HW_ERR "Fake kernel panic: %s\n", msg);
356 /* Support code for software error injection */
358 static int msr_to_offset(u32 msr)
360 unsigned bank = __this_cpu_read(injectm.bank);
362 if (msr == mca_cfg.rip_msr)
363 return offsetof(struct mce, ip);
364 if (msr == msr_ops.status(bank))
365 return offsetof(struct mce, status);
366 if (msr == msr_ops.addr(bank))
367 return offsetof(struct mce, addr);
368 if (msr == msr_ops.misc(bank))
369 return offsetof(struct mce, misc);
370 if (msr == MSR_IA32_MCG_STATUS)
371 return offsetof(struct mce, mcgstatus);
375 /* MSR access wrappers used for error injection */
376 static u64 mce_rdmsrl(u32 msr)
380 if (__this_cpu_read(injectm.finished)) {
381 int offset = msr_to_offset(msr);
385 return *(u64 *)((char *)this_cpu_ptr(&injectm) + offset);
388 if (rdmsrl_safe(msr, &v)) {
389 WARN_ONCE(1, "mce: Unable to read MSR 0x%x!\n", msr);
391 * Return zero in case the access faulted. This should
392 * not happen normally but can happen if the CPU does
393 * something weird, or if the code is buggy.
401 static void mce_wrmsrl(u32 msr, u64 v)
403 if (__this_cpu_read(injectm.finished)) {
404 int offset = msr_to_offset(msr);
407 *(u64 *)((char *)this_cpu_ptr(&injectm) + offset) = v;
414 * Collect all global (w.r.t. this processor) status about this machine
415 * check into our "mce" struct so that we can use it later to assess
416 * the severity of the problem as we read per-bank specific details.
418 static inline void mce_gather_info(struct mce *m, struct pt_regs *regs)
422 m->mcgstatus = mce_rdmsrl(MSR_IA32_MCG_STATUS);
425 * Get the address of the instruction at the time of
426 * the machine check error.
428 if (m->mcgstatus & (MCG_STATUS_RIPV|MCG_STATUS_EIPV)) {
433 * When in VM86 mode make the cs look like ring 3
434 * always. This is a lie, but it's better than passing
435 * the additional vm86 bit around everywhere.
437 if (v8086_mode(regs))
440 /* Use accurate RIP reporting if available. */
442 m->ip = mce_rdmsrl(mca_cfg.rip_msr);
446 int mce_available(struct cpuinfo_x86 *c)
448 if (mca_cfg.disabled)
450 return cpu_has(c, X86_FEATURE_MCE) && cpu_has(c, X86_FEATURE_MCA);
453 static void mce_schedule_work(void)
455 if (!mce_gen_pool_empty())
456 schedule_work(&mce_work);
459 static void mce_irq_work_cb(struct irq_work *entry)
464 static void mce_report_event(struct pt_regs *regs)
466 if (regs->flags & (X86_VM_MASK|X86_EFLAGS_IF)) {
469 * Triggering the work queue here is just an insurance
470 * policy in case the syscall exit notify handler
471 * doesn't run soon enough or ends up running on the
472 * wrong CPU (can happen when audit sleeps)
478 irq_work_queue(&mce_irq_work);
482 * Check if the address reported by the CPU is in a format we can parse.
483 * It would be possible to add code for most other cases, but all would
484 * be somewhat complicated (e.g. segment offset would require an instruction
485 * parser). So only support physical addresses up to page granuality for now.
487 static int mce_usable_address(struct mce *m)
489 if (!(m->status & MCI_STATUS_ADDRV))
492 /* Checks after this one are Intel-specific: */
493 if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL)
496 if (!(m->status & MCI_STATUS_MISCV))
499 if (MCI_MISC_ADDR_LSB(m->misc) > PAGE_SHIFT)
502 if (MCI_MISC_ADDR_MODE(m->misc) != MCI_MISC_ADDR_PHYS)
508 bool mce_is_memory_error(struct mce *m)
510 if (m->cpuvendor == X86_VENDOR_AMD) {
511 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 static bool mce_is_correctable(struct mce *m)
538 if (m->cpuvendor == X86_VENDOR_AMD && m->status & MCI_STATUS_DEFERRED)
541 if (m->status & MCI_STATUS_UC)
547 static bool cec_add_mce(struct mce *m)
552 /* We eat only correctable DRAM errors with usable addresses. */
553 if (mce_is_memory_error(m) &&
554 mce_is_correctable(m) &&
555 mce_usable_address(m))
556 if (!cec_add_elem(m->addr >> PAGE_SHIFT))
562 static int mce_first_notifier(struct notifier_block *nb, unsigned long val,
565 struct mce *m = (struct mce *)data;
573 /* Emit the trace record: */
576 set_bit(0, &mce_need_notify);
583 static struct notifier_block first_nb = {
584 .notifier_call = mce_first_notifier,
585 .priority = MCE_PRIO_FIRST,
588 static int srao_decode_notifier(struct notifier_block *nb, unsigned long val,
591 struct mce *mce = (struct mce *)data;
597 if (mce_usable_address(mce) && (mce->severity == MCE_AO_SEVERITY)) {
598 pfn = mce->addr >> PAGE_SHIFT;
599 if (!memory_failure(pfn, 0))
605 static struct notifier_block mce_srao_nb = {
606 .notifier_call = srao_decode_notifier,
607 .priority = MCE_PRIO_SRAO,
610 static int mce_default_notifier(struct notifier_block *nb, unsigned long val,
613 struct mce *m = (struct mce *)data;
618 if (atomic_read(&num_notifiers) > NUM_DEFAULT_NOTIFIERS)
626 static struct notifier_block mce_default_nb = {
627 .notifier_call = mce_default_notifier,
628 /* lowest prio, we want it to run last. */
629 .priority = MCE_PRIO_LOWEST,
633 * Read ADDR and MISC registers.
635 static void mce_read_aux(struct mce *m, int i)
637 if (m->status & MCI_STATUS_MISCV)
638 m->misc = mce_rdmsrl(msr_ops.misc(i));
640 if (m->status & MCI_STATUS_ADDRV) {
641 m->addr = mce_rdmsrl(msr_ops.addr(i));
644 * Mask the reported address by the reported granularity.
646 if (mca_cfg.ser && (m->status & MCI_STATUS_MISCV)) {
647 u8 shift = MCI_MISC_ADDR_LSB(m->misc);
653 * Extract [55:<lsb>] where lsb is the least significant
654 * *valid* bit of the address bits.
656 if (mce_flags.smca) {
657 u8 lsb = (m->addr >> 56) & 0x3f;
659 m->addr &= GENMASK_ULL(55, lsb);
663 if (mce_flags.smca) {
664 m->ipid = mce_rdmsrl(MSR_AMD64_SMCA_MCx_IPID(i));
666 if (m->status & MCI_STATUS_SYNDV)
667 m->synd = mce_rdmsrl(MSR_AMD64_SMCA_MCx_SYND(i));
671 DEFINE_PER_CPU(unsigned, mce_poll_count);
674 * Poll for corrected events or events that happened before reset.
675 * Those are just logged through /dev/mcelog.
677 * This is executed in standard interrupt context.
679 * Note: spec recommends to panic for fatal unsignalled
680 * errors here. However this would be quite problematic --
681 * we would need to reimplement the Monarch handling and
682 * it would mess up the exclusion between exception handler
683 * and poll hander -- * so we skip this for now.
684 * These cases should not happen anyways, or only when the CPU
685 * is already totally * confused. In this case it's likely it will
686 * not fully execute the machine check handler either.
688 bool machine_check_poll(enum mcp_flags flags, mce_banks_t *b)
690 bool error_seen = false;
694 this_cpu_inc(mce_poll_count);
696 mce_gather_info(&m, NULL);
698 if (flags & MCP_TIMESTAMP)
701 for (i = 0; i < mca_cfg.banks; i++) {
702 if (!mce_banks[i].ctl || !test_bit(i, *b))
710 m.status = mce_rdmsrl(msr_ops.status(i));
711 if (!(m.status & MCI_STATUS_VAL))
715 * Uncorrected or signalled events are handled by the exception
716 * handler when it is enabled, so don't process those here.
718 * TBD do the same check for MCI_STATUS_EN here?
720 if (!(flags & MCP_UC) &&
721 (m.status & (mca_cfg.ser ? MCI_STATUS_S : MCI_STATUS_UC)))
728 m.severity = mce_severity(&m, mca_cfg.tolerant, NULL, false);
731 * Don't get the IP here because it's unlikely to
732 * have anything to do with the actual error location.
734 if (!(flags & MCP_DONTLOG) && !mca_cfg.dont_log_ce)
736 else if (mce_usable_address(&m)) {
738 * Although we skipped logging this, we still want
739 * to take action. Add to the pool so the registered
740 * notifiers will see it.
742 if (!mce_gen_pool_add(&m))
747 * Clear state for this bank.
749 mce_wrmsrl(msr_ops.status(i), 0);
753 * Don't clear MCG_STATUS here because it's only defined for
761 EXPORT_SYMBOL_GPL(machine_check_poll);
764 * Do a quick check if any of the events requires a panic.
765 * This decides if we keep the events around or clear them.
767 static int mce_no_way_out(struct mce *m, char **msg, unsigned long *validp,
768 struct pt_regs *regs)
773 for (i = 0; i < mca_cfg.banks; i++) {
774 m->status = mce_rdmsrl(msr_ops.status(i));
775 if (m->status & MCI_STATUS_VAL) {
776 __set_bit(i, validp);
777 if (quirk_no_way_out)
778 quirk_no_way_out(i, m, regs);
781 if (mce_severity(m, mca_cfg.tolerant, &tmp, true) >= MCE_PANIC_SEVERITY) {
790 * Variable to establish order between CPUs while scanning.
791 * Each CPU spins initially until executing is equal its number.
793 static atomic_t mce_executing;
796 * Defines order of CPUs on entry. First CPU becomes Monarch.
798 static atomic_t mce_callin;
801 * Check if a timeout waiting for other CPUs happened.
803 static int mce_timed_out(u64 *t, const char *msg)
806 * The others already did panic for some reason.
807 * Bail out like in a timeout.
808 * rmb() to tell the compiler that system_state
809 * might have been modified by someone else.
812 if (atomic_read(&mce_panicked))
814 if (!mca_cfg.monarch_timeout)
816 if ((s64)*t < SPINUNIT) {
817 if (mca_cfg.tolerant <= 1)
818 mce_panic(msg, NULL, NULL);
824 touch_nmi_watchdog();
829 * The Monarch's reign. The Monarch is the CPU who entered
830 * the machine check handler first. It waits for the others to
831 * raise the exception too and then grades them. When any
832 * error is fatal panic. Only then let the others continue.
834 * The other CPUs entering the MCE handler will be controlled by the
835 * Monarch. They are called Subjects.
837 * This way we prevent any potential data corruption in a unrecoverable case
838 * and also makes sure always all CPU's errors are examined.
840 * Also this detects the case of a machine check event coming from outer
841 * space (not detected by any CPUs) In this case some external agent wants
842 * us to shut down, so panic too.
844 * The other CPUs might still decide to panic if the handler happens
845 * in a unrecoverable place, but in this case the system is in a semi-stable
846 * state and won't corrupt anything by itself. It's ok to let the others
847 * continue for a bit first.
849 * All the spin loops have timeouts; when a timeout happens a CPU
850 * typically elects itself to be Monarch.
852 static void mce_reign(void)
855 struct mce *m = NULL;
856 int global_worst = 0;
861 * This CPU is the Monarch and the other CPUs have run
862 * through their handlers.
863 * Grade the severity of the errors of all the CPUs.
865 for_each_possible_cpu(cpu) {
866 int severity = mce_severity(&per_cpu(mces_seen, cpu),
869 if (severity > global_worst) {
871 global_worst = severity;
872 m = &per_cpu(mces_seen, cpu);
877 * Cannot recover? Panic here then.
878 * This dumps all the mces in the log buffer and stops the
881 if (m && global_worst >= MCE_PANIC_SEVERITY && mca_cfg.tolerant < 3)
882 mce_panic("Fatal machine check", m, msg);
885 * For UC somewhere we let the CPU who detects it handle it.
886 * Also must let continue the others, otherwise the handling
887 * CPU could deadlock on a lock.
891 * No machine check event found. Must be some external
892 * source or one CPU is hung. Panic.
894 if (global_worst <= MCE_KEEP_SEVERITY && mca_cfg.tolerant < 3)
895 mce_panic("Fatal machine check from unknown source", NULL, NULL);
898 * Now clear all the mces_seen so that they don't reappear on
901 for_each_possible_cpu(cpu)
902 memset(&per_cpu(mces_seen, cpu), 0, sizeof(struct mce));
905 static atomic_t global_nwo;
908 * Start of Monarch synchronization. This waits until all CPUs have
909 * entered the exception handler and then determines if any of them
910 * saw a fatal event that requires panic. Then it executes them
911 * in the entry order.
912 * TBD double check parallel CPU hotunplug
914 static int mce_start(int *no_way_out)
917 int cpus = num_online_cpus();
918 u64 timeout = (u64)mca_cfg.monarch_timeout * NSEC_PER_USEC;
923 atomic_add(*no_way_out, &global_nwo);
925 * Rely on the implied barrier below, such that global_nwo
926 * is updated before mce_callin.
928 order = atomic_inc_return(&mce_callin);
933 while (atomic_read(&mce_callin) != cpus) {
934 if (mce_timed_out(&timeout,
935 "Timeout: Not all CPUs entered broadcast exception handler")) {
936 atomic_set(&global_nwo, 0);
943 * mce_callin should be read before global_nwo
949 * Monarch: Starts executing now, the others wait.
951 atomic_set(&mce_executing, 1);
954 * Subject: Now start the scanning loop one by one in
955 * the original callin order.
956 * This way when there are any shared banks it will be
957 * only seen by one CPU before cleared, avoiding duplicates.
959 while (atomic_read(&mce_executing) < order) {
960 if (mce_timed_out(&timeout,
961 "Timeout: Subject CPUs unable to finish machine check processing")) {
962 atomic_set(&global_nwo, 0);
970 * Cache the global no_way_out state.
972 *no_way_out = atomic_read(&global_nwo);
978 * Synchronize between CPUs after main scanning loop.
979 * This invokes the bulk of the Monarch processing.
981 static int mce_end(int order)
984 u64 timeout = (u64)mca_cfg.monarch_timeout * NSEC_PER_USEC;
992 * Allow others to run.
994 atomic_inc(&mce_executing);
997 /* CHECKME: Can this race with a parallel hotplug? */
998 int cpus = num_online_cpus();
1001 * Monarch: Wait for everyone to go through their scanning
1004 while (atomic_read(&mce_executing) <= cpus) {
1005 if (mce_timed_out(&timeout,
1006 "Timeout: Monarch CPU unable to finish machine check processing"))
1016 * Subject: Wait for Monarch to finish.
1018 while (atomic_read(&mce_executing) != 0) {
1019 if (mce_timed_out(&timeout,
1020 "Timeout: Monarch CPU did not finish machine check processing"))
1026 * Don't reset anything. That's done by the Monarch.
1032 * Reset all global state.
1035 atomic_set(&global_nwo, 0);
1036 atomic_set(&mce_callin, 0);
1040 * Let others run again.
1042 atomic_set(&mce_executing, 0);
1046 static void mce_clear_state(unsigned long *toclear)
1050 for (i = 0; i < mca_cfg.banks; i++) {
1051 if (test_bit(i, toclear))
1052 mce_wrmsrl(msr_ops.status(i), 0);
1056 static int do_memory_failure(struct mce *m)
1058 int flags = MF_ACTION_REQUIRED;
1061 pr_err("Uncorrected hardware memory error in user-access at %llx", m->addr);
1062 if (!(m->mcgstatus & MCG_STATUS_RIPV))
1063 flags |= MF_MUST_KILL;
1064 ret = memory_failure(m->addr >> PAGE_SHIFT, flags);
1066 pr_err("Memory error not recovered");
1068 mce_unmap_kpfn(m->addr >> PAGE_SHIFT);
1072 #ifndef mce_unmap_kpfn
1073 static void mce_unmap_kpfn(unsigned long pfn)
1075 unsigned long decoy_addr;
1078 * Unmap this page from the kernel 1:1 mappings to make sure
1079 * we don't log more errors because of speculative access to
1081 * We would like to just call:
1082 * set_memory_np((unsigned long)pfn_to_kaddr(pfn), 1);
1083 * but doing that would radically increase the odds of a
1084 * speculative access to the poison page because we'd have
1085 * the virtual address of the kernel 1:1 mapping sitting
1086 * around in registers.
1087 * Instead we get tricky. We create a non-canonical address
1088 * that looks just like the one we want, but has bit 63 flipped.
1089 * This relies on set_memory_np() not checking whether we passed
1094 * Build time check to see if we have a spare virtual bit. Don't want
1095 * to leave this until run time because most developers don't have a
1096 * system that can exercise this code path. This will only become a
1097 * problem if/when we move beyond 5-level page tables.
1099 * Hard code "9" here because cpp doesn't grok ilog2(PTRS_PER_PGD)
1101 #if PGDIR_SHIFT + 9 < 63
1102 decoy_addr = (pfn << PAGE_SHIFT) + (PAGE_OFFSET ^ BIT(63));
1104 #error "no unused virtual bit available"
1107 if (set_memory_np(decoy_addr, 1))
1108 pr_warn("Could not invalidate pfn=0x%lx from 1:1 map\n", pfn);
1113 * The actual machine check handler. This only handles real
1114 * exceptions when something got corrupted coming in through int 18.
1116 * This is executed in NMI context not subject to normal locking rules. This
1117 * implies that most kernel services cannot be safely used. Don't even
1118 * think about putting a printk in there!
1120 * On Intel systems this is entered on all CPUs in parallel through
1121 * MCE broadcast. However some CPUs might be broken beyond repair,
1122 * so be always careful when synchronizing with others.
1124 void do_machine_check(struct pt_regs *regs, long error_code)
1126 struct mca_config *cfg = &mca_cfg;
1127 struct mce m, *final;
1133 * Establish sequential order between the CPUs entering the machine
1138 * If no_way_out gets set, there is no safe way to recover from this
1139 * MCE. If mca_cfg.tolerant is cranked up, we'll try anyway.
1143 * If kill_it gets set, there might be a way to recover from this
1147 DECLARE_BITMAP(toclear, MAX_NR_BANKS);
1148 DECLARE_BITMAP(valid_banks, MAX_NR_BANKS);
1149 char *msg = "Unknown";
1152 * MCEs are always local on AMD. Same is determined by MCG_STATUS_LMCES
1156 int cpu = smp_processor_id();
1159 * Cases where we avoid rendezvous handler timeout:
1160 * 1) If this CPU is offline.
1162 * 2) If crashing_cpu was set, e.g. we're entering kdump and we need to
1163 * skip those CPUs which remain looping in the 1st kernel - see
1164 * crash_nmi_callback().
1166 * Note: there still is a small window between kexec-ing and the new,
1167 * kdump kernel establishing a new #MC handler where a broadcasted MCE
1168 * might not get handled properly.
1170 if (cpu_is_offline(cpu) ||
1171 (crashing_cpu != -1 && crashing_cpu != cpu)) {
1174 mcgstatus = mce_rdmsrl(MSR_IA32_MCG_STATUS);
1175 if (mcgstatus & MCG_STATUS_RIPV) {
1176 mce_wrmsrl(MSR_IA32_MCG_STATUS, 0);
1183 this_cpu_inc(mce_exception_count);
1188 mce_gather_info(&m, regs);
1191 final = this_cpu_ptr(&mces_seen);
1194 memset(valid_banks, 0, sizeof(valid_banks));
1195 no_way_out = mce_no_way_out(&m, &msg, valid_banks, regs);
1200 * When no restart IP might need to kill or panic.
1201 * Assume the worst for now, but if we find the
1202 * severity is MCE_AR_SEVERITY we have other options.
1204 if (!(m.mcgstatus & MCG_STATUS_RIPV))
1208 * Check if this MCE is signaled to only this logical processor,
1211 if (m.cpuvendor == X86_VENDOR_INTEL)
1212 lmce = m.mcgstatus & MCG_STATUS_LMCES;
1215 * Go through all banks in exclusion of the other CPUs. This way we
1216 * don't report duplicated events on shared banks because the first one
1217 * to see it will clear it. If this is a Local MCE, then no need to
1218 * perform rendezvous.
1221 order = mce_start(&no_way_out);
1223 for (i = 0; i < cfg->banks; i++) {
1224 __clear_bit(i, toclear);
1225 if (!test_bit(i, valid_banks))
1227 if (!mce_banks[i].ctl)
1234 m.status = mce_rdmsrl(msr_ops.status(i));
1235 if ((m.status & MCI_STATUS_VAL) == 0)
1239 * Non uncorrected or non signaled errors are handled by
1240 * machine_check_poll. Leave them alone, unless this panics.
1242 if (!(m.status & (cfg->ser ? MCI_STATUS_S : MCI_STATUS_UC)) &&
1247 * Set taint even when machine check was not enabled.
1249 add_taint(TAINT_MACHINE_CHECK, LOCKDEP_NOW_UNRELIABLE);
1251 severity = mce_severity(&m, cfg->tolerant, NULL, true);
1254 * When machine check was for corrected/deferred handler don't
1255 * touch, unless we're panicing.
1257 if ((severity == MCE_KEEP_SEVERITY ||
1258 severity == MCE_UCNA_SEVERITY) && !no_way_out)
1260 __set_bit(i, toclear);
1261 if (severity == MCE_NO_SEVERITY) {
1263 * Machine check event was not enabled. Clear, but
1269 mce_read_aux(&m, i);
1271 /* assuming valid severity level != 0 */
1272 m.severity = severity;
1276 if (severity > worst) {
1282 /* mce_clear_state will clear *final, save locally for use later */
1286 mce_clear_state(toclear);
1289 * Do most of the synchronization with other CPUs.
1290 * When there's any problem use only local no_way_out state.
1293 if (mce_end(order) < 0)
1294 no_way_out = worst >= MCE_PANIC_SEVERITY;
1297 * Local MCE skipped calling mce_reign()
1298 * If we found a fatal error, we need to panic here.
1300 if (worst >= MCE_PANIC_SEVERITY && mca_cfg.tolerant < 3)
1301 mce_panic("Machine check from unknown source",
1306 * If tolerant is at an insane level we drop requests to kill
1307 * processes and continue even when there is no way out.
1309 if (cfg->tolerant == 3)
1311 else if (no_way_out)
1312 mce_panic("Fatal machine check on current CPU", &m, msg);
1315 mce_report_event(regs);
1316 mce_wrmsrl(MSR_IA32_MCG_STATUS, 0);
1320 if (worst != MCE_AR_SEVERITY && !kill_it)
1323 /* Fault was in user mode and we need to take some action */
1324 if ((m.cs & 3) == 3) {
1325 ist_begin_non_atomic(regs);
1328 if (kill_it || do_memory_failure(&m))
1329 force_sig(SIGBUS, current);
1330 local_irq_disable();
1331 ist_end_non_atomic();
1333 if (!fixup_exception(regs, X86_TRAP_MC))
1334 mce_panic("Failed kernel mode recovery", &m, NULL);
1340 EXPORT_SYMBOL_GPL(do_machine_check);
1342 #ifndef CONFIG_MEMORY_FAILURE
1343 int memory_failure(unsigned long pfn, int flags)
1345 /* mce_severity() should not hand us an ACTION_REQUIRED error */
1346 BUG_ON(flags & MF_ACTION_REQUIRED);
1347 pr_err("Uncorrected memory error in page 0x%lx ignored\n"
1348 "Rebuild kernel with CONFIG_MEMORY_FAILURE=y for smarter handling\n",
1356 * Periodic polling timer for "silent" machine check errors. If the
1357 * poller finds an MCE, poll 2x faster. When the poller finds no more
1358 * errors, poll 2x slower (up to check_interval seconds).
1360 static unsigned long check_interval = INITIAL_CHECK_INTERVAL;
1362 static DEFINE_PER_CPU(unsigned long, mce_next_interval); /* in jiffies */
1363 static DEFINE_PER_CPU(struct timer_list, mce_timer);
1365 static unsigned long mce_adjust_timer_default(unsigned long interval)
1370 static unsigned long (*mce_adjust_timer)(unsigned long interval) = mce_adjust_timer_default;
1372 static void __start_timer(struct timer_list *t, unsigned long interval)
1374 unsigned long when = jiffies + interval;
1375 unsigned long flags;
1377 local_irq_save(flags);
1379 if (!timer_pending(t) || time_before(when, t->expires))
1380 mod_timer(t, round_jiffies(when));
1382 local_irq_restore(flags);
1385 static void mce_timer_fn(struct timer_list *t)
1387 struct timer_list *cpu_t = this_cpu_ptr(&mce_timer);
1390 WARN_ON(cpu_t != t);
1392 iv = __this_cpu_read(mce_next_interval);
1394 if (mce_available(this_cpu_ptr(&cpu_info))) {
1395 machine_check_poll(0, this_cpu_ptr(&mce_poll_banks));
1397 if (mce_intel_cmci_poll()) {
1398 iv = mce_adjust_timer(iv);
1404 * Alert userspace if needed. If we logged an MCE, reduce the polling
1405 * interval, otherwise increase the polling interval.
1407 if (mce_notify_irq())
1408 iv = max(iv / 2, (unsigned long) HZ/100);
1410 iv = min(iv * 2, round_jiffies_relative(check_interval * HZ));
1413 __this_cpu_write(mce_next_interval, iv);
1414 __start_timer(t, iv);
1418 * Ensure that the timer is firing in @interval from now.
1420 void mce_timer_kick(unsigned long interval)
1422 struct timer_list *t = this_cpu_ptr(&mce_timer);
1423 unsigned long iv = __this_cpu_read(mce_next_interval);
1425 __start_timer(t, interval);
1428 __this_cpu_write(mce_next_interval, interval);
1431 /* Must not be called in IRQ context where del_timer_sync() can deadlock */
1432 static void mce_timer_delete_all(void)
1436 for_each_online_cpu(cpu)
1437 del_timer_sync(&per_cpu(mce_timer, cpu));
1441 * Notify the user(s) about new machine check events.
1442 * Can be called from interrupt context, but not from machine check/NMI
1445 int mce_notify_irq(void)
1447 /* Not more than two messages every minute */
1448 static DEFINE_RATELIMIT_STATE(ratelimit, 60*HZ, 2);
1450 if (test_and_clear_bit(0, &mce_need_notify)) {
1453 if (__ratelimit(&ratelimit))
1454 pr_info(HW_ERR "Machine check events logged\n");
1460 EXPORT_SYMBOL_GPL(mce_notify_irq);
1462 static int __mcheck_cpu_mce_banks_init(void)
1465 u8 num_banks = mca_cfg.banks;
1467 mce_banks = kzalloc(num_banks * sizeof(struct mce_bank), GFP_KERNEL);
1471 for (i = 0; i < num_banks; i++) {
1472 struct mce_bank *b = &mce_banks[i];
1481 * Initialize Machine Checks for a CPU.
1483 static int __mcheck_cpu_cap_init(void)
1488 rdmsrl(MSR_IA32_MCG_CAP, cap);
1490 b = cap & MCG_BANKCNT_MASK;
1492 pr_info("CPU supports %d MCE banks\n", b);
1494 if (b > MAX_NR_BANKS) {
1495 pr_warn("Using only %u machine check banks out of %u\n",
1500 /* Don't support asymmetric configurations today */
1501 WARN_ON(mca_cfg.banks != 0 && b != mca_cfg.banks);
1505 int err = __mcheck_cpu_mce_banks_init();
1511 /* Use accurate RIP reporting if available. */
1512 if ((cap & MCG_EXT_P) && MCG_EXT_CNT(cap) >= 9)
1513 mca_cfg.rip_msr = MSR_IA32_MCG_EIP;
1515 if (cap & MCG_SER_P)
1521 static void __mcheck_cpu_init_generic(void)
1523 enum mcp_flags m_fl = 0;
1524 mce_banks_t all_banks;
1527 if (!mca_cfg.bootlog)
1531 * Log the machine checks left over from the previous reset.
1533 bitmap_fill(all_banks, MAX_NR_BANKS);
1534 machine_check_poll(MCP_UC | m_fl, &all_banks);
1536 cr4_set_bits(X86_CR4_MCE);
1538 rdmsrl(MSR_IA32_MCG_CAP, cap);
1539 if (cap & MCG_CTL_P)
1540 wrmsr(MSR_IA32_MCG_CTL, 0xffffffff, 0xffffffff);
1543 static void __mcheck_cpu_init_clear_banks(void)
1547 for (i = 0; i < mca_cfg.banks; i++) {
1548 struct mce_bank *b = &mce_banks[i];
1552 wrmsrl(msr_ops.ctl(i), b->ctl);
1553 wrmsrl(msr_ops.status(i), 0);
1558 * During IFU recovery Sandy Bridge -EP4S processors set the RIPV and
1559 * EIPV bits in MCG_STATUS to zero on the affected logical processor (SDM
1560 * Vol 3B Table 15-20). But this confuses both the code that determines
1561 * whether the machine check occurred in kernel or user mode, and also
1562 * the severity assessment code. Pretend that EIPV was set, and take the
1563 * ip/cs values from the pt_regs that mce_gather_info() ignored earlier.
1565 static void quirk_sandybridge_ifu(int bank, struct mce *m, struct pt_regs *regs)
1569 if ((m->mcgstatus & (MCG_STATUS_EIPV|MCG_STATUS_RIPV)) != 0)
1571 if ((m->status & (MCI_STATUS_OVER|MCI_STATUS_UC|
1572 MCI_STATUS_EN|MCI_STATUS_MISCV|MCI_STATUS_ADDRV|
1573 MCI_STATUS_PCC|MCI_STATUS_S|MCI_STATUS_AR|
1575 (MCI_STATUS_UC|MCI_STATUS_EN|
1576 MCI_STATUS_MISCV|MCI_STATUS_ADDRV|MCI_STATUS_S|
1577 MCI_STATUS_AR|MCACOD_INSTR))
1580 m->mcgstatus |= MCG_STATUS_EIPV;
1585 /* Add per CPU specific workarounds here */
1586 static int __mcheck_cpu_apply_quirks(struct cpuinfo_x86 *c)
1588 struct mca_config *cfg = &mca_cfg;
1590 if (c->x86_vendor == X86_VENDOR_UNKNOWN) {
1591 pr_info("unknown CPU type - not enabling MCE support\n");
1595 /* This should be disabled by the BIOS, but isn't always */
1596 if (c->x86_vendor == X86_VENDOR_AMD) {
1597 if (c->x86 == 15 && cfg->banks > 4) {
1599 * disable GART TBL walk error reporting, which
1600 * trips off incorrectly with the IOMMU & 3ware
1603 clear_bit(10, (unsigned long *)&mce_banks[4].ctl);
1605 if (c->x86 < 0x11 && cfg->bootlog < 0) {
1607 * Lots of broken BIOS around that don't clear them
1608 * by default and leave crap in there. Don't log:
1613 * Various K7s with broken bank 0 around. Always disable
1616 if (c->x86 == 6 && cfg->banks > 0)
1617 mce_banks[0].ctl = 0;
1620 * overflow_recov is supported for F15h Models 00h-0fh
1621 * even though we don't have a CPUID bit for it.
1623 if (c->x86 == 0x15 && c->x86_model <= 0xf)
1624 mce_flags.overflow_recov = 1;
1627 * Turn off MC4_MISC thresholding banks on those models since
1628 * they're not supported there.
1630 if (c->x86 == 0x15 &&
1631 (c->x86_model >= 0x10 && c->x86_model <= 0x1f)) {
1636 0x00000413, /* MC4_MISC0 */
1637 0xc0000408, /* MC4_MISC1 */
1640 rdmsrl(MSR_K7_HWCR, hwcr);
1642 /* McStatusWrEn has to be set */
1643 need_toggle = !(hwcr & BIT(18));
1646 wrmsrl(MSR_K7_HWCR, hwcr | BIT(18));
1648 /* Clear CntP bit safely */
1649 for (i = 0; i < ARRAY_SIZE(msrs); i++)
1650 msr_clear_bit(msrs[i], 62);
1652 /* restore old settings */
1654 wrmsrl(MSR_K7_HWCR, hwcr);
1658 if (c->x86_vendor == X86_VENDOR_INTEL) {
1660 * SDM documents that on family 6 bank 0 should not be written
1661 * because it aliases to another special BIOS controlled
1663 * But it's not aliased anymore on model 0x1a+
1664 * Don't ignore bank 0 completely because there could be a
1665 * valid event later, merely don't write CTL0.
1668 if (c->x86 == 6 && c->x86_model < 0x1A && cfg->banks > 0)
1669 mce_banks[0].init = 0;
1672 * All newer Intel systems support MCE broadcasting. Enable
1673 * synchronization with a one second timeout.
1675 if ((c->x86 > 6 || (c->x86 == 6 && c->x86_model >= 0xe)) &&
1676 cfg->monarch_timeout < 0)
1677 cfg->monarch_timeout = USEC_PER_SEC;
1680 * There are also broken BIOSes on some Pentium M and
1683 if (c->x86 == 6 && c->x86_model <= 13 && cfg->bootlog < 0)
1686 if (c->x86 == 6 && c->x86_model == 45)
1687 quirk_no_way_out = quirk_sandybridge_ifu;
1689 if (cfg->monarch_timeout < 0)
1690 cfg->monarch_timeout = 0;
1691 if (cfg->bootlog != 0)
1692 cfg->panic_timeout = 30;
1697 static int __mcheck_cpu_ancient_init(struct cpuinfo_x86 *c)
1702 switch (c->x86_vendor) {
1703 case X86_VENDOR_INTEL:
1704 intel_p5_mcheck_init(c);
1707 case X86_VENDOR_CENTAUR:
1708 winchip_mcheck_init(c);
1719 * Init basic CPU features needed for early decoding of MCEs.
1721 static void __mcheck_cpu_init_early(struct cpuinfo_x86 *c)
1723 if (c->x86_vendor == X86_VENDOR_AMD) {
1724 mce_flags.overflow_recov = !!cpu_has(c, X86_FEATURE_OVERFLOW_RECOV);
1725 mce_flags.succor = !!cpu_has(c, X86_FEATURE_SUCCOR);
1726 mce_flags.smca = !!cpu_has(c, X86_FEATURE_SMCA);
1728 if (mce_flags.smca) {
1729 msr_ops.ctl = smca_ctl_reg;
1730 msr_ops.status = smca_status_reg;
1731 msr_ops.addr = smca_addr_reg;
1732 msr_ops.misc = smca_misc_reg;
1737 static void __mcheck_cpu_init_vendor(struct cpuinfo_x86 *c)
1739 switch (c->x86_vendor) {
1740 case X86_VENDOR_INTEL:
1741 mce_intel_feature_init(c);
1742 mce_adjust_timer = cmci_intel_adjust_timer;
1745 case X86_VENDOR_AMD: {
1746 mce_amd_feature_init(c);
1755 static void __mcheck_cpu_clear_vendor(struct cpuinfo_x86 *c)
1757 switch (c->x86_vendor) {
1758 case X86_VENDOR_INTEL:
1759 mce_intel_feature_clear(c);
1766 static void mce_start_timer(struct timer_list *t)
1768 unsigned long iv = check_interval * HZ;
1770 if (mca_cfg.ignore_ce || !iv)
1773 this_cpu_write(mce_next_interval, iv);
1774 __start_timer(t, iv);
1777 static void __mcheck_cpu_setup_timer(void)
1779 struct timer_list *t = this_cpu_ptr(&mce_timer);
1781 timer_setup(t, mce_timer_fn, TIMER_PINNED);
1784 static void __mcheck_cpu_init_timer(void)
1786 struct timer_list *t = this_cpu_ptr(&mce_timer);
1788 timer_setup(t, mce_timer_fn, TIMER_PINNED);
1792 /* Handle unconfigured int18 (should never happen) */
1793 static void unexpected_machine_check(struct pt_regs *regs, long error_code)
1795 pr_err("CPU#%d: Unexpected int18 (Machine Check)\n",
1796 smp_processor_id());
1799 /* Call the installed machine check handler for this CPU setup. */
1800 void (*machine_check_vector)(struct pt_regs *, long error_code) =
1801 unexpected_machine_check;
1803 dotraplinkage void do_mce(struct pt_regs *regs, long error_code)
1805 machine_check_vector(regs, error_code);
1809 * Called for each booted CPU to set up machine checks.
1810 * Must be called with preempt off:
1812 void mcheck_cpu_init(struct cpuinfo_x86 *c)
1814 if (mca_cfg.disabled)
1817 if (__mcheck_cpu_ancient_init(c))
1820 if (!mce_available(c))
1823 if (__mcheck_cpu_cap_init() < 0 || __mcheck_cpu_apply_quirks(c) < 0) {
1824 mca_cfg.disabled = true;
1828 if (mce_gen_pool_init()) {
1829 mca_cfg.disabled = true;
1830 pr_emerg("Couldn't allocate MCE records pool!\n");
1834 machine_check_vector = do_machine_check;
1836 __mcheck_cpu_init_early(c);
1837 __mcheck_cpu_init_generic();
1838 __mcheck_cpu_init_vendor(c);
1839 __mcheck_cpu_init_clear_banks();
1840 __mcheck_cpu_setup_timer();
1844 * Called for each booted CPU to clear some machine checks opt-ins
1846 void mcheck_cpu_clear(struct cpuinfo_x86 *c)
1848 if (mca_cfg.disabled)
1851 if (!mce_available(c))
1855 * Possibly to clear general settings generic to x86
1856 * __mcheck_cpu_clear_generic(c);
1858 __mcheck_cpu_clear_vendor(c);
1862 static void __mce_disable_bank(void *arg)
1864 int bank = *((int *)arg);
1865 __clear_bit(bank, this_cpu_ptr(mce_poll_banks));
1866 cmci_disable_bank(bank);
1869 void mce_disable_bank(int bank)
1871 if (bank >= mca_cfg.banks) {
1873 "Ignoring request to disable invalid MCA bank %d.\n",
1877 set_bit(bank, mce_banks_ce_disabled);
1878 on_each_cpu(__mce_disable_bank, &bank, 1);
1882 * mce=off Disables machine check
1883 * mce=no_cmci Disables CMCI
1884 * mce=no_lmce Disables LMCE
1885 * mce=dont_log_ce Clears corrected events silently, no log created for CEs.
1886 * mce=ignore_ce Disables polling and CMCI, corrected events are not cleared.
1887 * mce=TOLERANCELEVEL[,monarchtimeout] (number, see above)
1888 * monarchtimeout is how long to wait for other CPUs on machine
1889 * check, or 0 to not wait
1890 * mce=bootlog Log MCEs from before booting. Disabled by default on AMD Fam10h
1892 * mce=nobootlog Don't log MCEs from before booting.
1893 * mce=bios_cmci_threshold Don't program the CMCI threshold
1894 * mce=recovery force enable memcpy_mcsafe()
1896 static int __init mcheck_enable(char *str)
1898 struct mca_config *cfg = &mca_cfg;
1906 if (!strcmp(str, "off"))
1907 cfg->disabled = true;
1908 else if (!strcmp(str, "no_cmci"))
1909 cfg->cmci_disabled = true;
1910 else if (!strcmp(str, "no_lmce"))
1911 cfg->lmce_disabled = true;
1912 else if (!strcmp(str, "dont_log_ce"))
1913 cfg->dont_log_ce = true;
1914 else if (!strcmp(str, "ignore_ce"))
1915 cfg->ignore_ce = true;
1916 else if (!strcmp(str, "bootlog") || !strcmp(str, "nobootlog"))
1917 cfg->bootlog = (str[0] == 'b');
1918 else if (!strcmp(str, "bios_cmci_threshold"))
1919 cfg->bios_cmci_threshold = true;
1920 else if (!strcmp(str, "recovery"))
1921 cfg->recovery = true;
1922 else if (isdigit(str[0])) {
1923 if (get_option(&str, &cfg->tolerant) == 2)
1924 get_option(&str, &(cfg->monarch_timeout));
1926 pr_info("mce argument %s ignored. Please use /sys\n", str);
1931 __setup("mce", mcheck_enable);
1933 int __init mcheck_init(void)
1935 mcheck_intel_therm_init();
1936 mce_register_decode_chain(&first_nb);
1937 mce_register_decode_chain(&mce_srao_nb);
1938 mce_register_decode_chain(&mce_default_nb);
1939 mcheck_vendor_init_severity();
1941 INIT_WORK(&mce_work, mce_gen_pool_process);
1942 init_irq_work(&mce_irq_work, mce_irq_work_cb);
1948 * mce_syscore: PM support
1952 * Disable machine checks on suspend and shutdown. We can't really handle
1955 static void mce_disable_error_reporting(void)
1959 for (i = 0; i < mca_cfg.banks; i++) {
1960 struct mce_bank *b = &mce_banks[i];
1963 wrmsrl(msr_ops.ctl(i), 0);
1968 static void vendor_disable_error_reporting(void)
1971 * Don't clear on Intel or AMD CPUs. Some of these MSRs are socket-wide.
1972 * Disabling them for just a single offlined CPU is bad, since it will
1973 * inhibit reporting for all shared resources on the socket like the
1974 * last level cache (LLC), the integrated memory controller (iMC), etc.
1976 if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL ||
1977 boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
1980 mce_disable_error_reporting();
1983 static int mce_syscore_suspend(void)
1985 vendor_disable_error_reporting();
1989 static void mce_syscore_shutdown(void)
1991 vendor_disable_error_reporting();
1995 * On resume clear all MCE state. Don't want to see leftovers from the BIOS.
1996 * Only one CPU is active at this time, the others get re-added later using
1999 static void mce_syscore_resume(void)
2001 __mcheck_cpu_init_generic();
2002 __mcheck_cpu_init_vendor(raw_cpu_ptr(&cpu_info));
2003 __mcheck_cpu_init_clear_banks();
2006 static struct syscore_ops mce_syscore_ops = {
2007 .suspend = mce_syscore_suspend,
2008 .shutdown = mce_syscore_shutdown,
2009 .resume = mce_syscore_resume,
2013 * mce_device: Sysfs support
2016 static void mce_cpu_restart(void *data)
2018 if (!mce_available(raw_cpu_ptr(&cpu_info)))
2020 __mcheck_cpu_init_generic();
2021 __mcheck_cpu_init_clear_banks();
2022 __mcheck_cpu_init_timer();
2025 /* Reinit MCEs after user configuration changes */
2026 static void mce_restart(void)
2028 mce_timer_delete_all();
2029 on_each_cpu(mce_cpu_restart, NULL, 1);
2032 /* Toggle features for corrected errors */
2033 static void mce_disable_cmci(void *data)
2035 if (!mce_available(raw_cpu_ptr(&cpu_info)))
2040 static void mce_enable_ce(void *all)
2042 if (!mce_available(raw_cpu_ptr(&cpu_info)))
2047 __mcheck_cpu_init_timer();
2050 static struct bus_type mce_subsys = {
2051 .name = "machinecheck",
2052 .dev_name = "machinecheck",
2055 DEFINE_PER_CPU(struct device *, mce_device);
2057 static inline struct mce_bank *attr_to_bank(struct device_attribute *attr)
2059 return container_of(attr, struct mce_bank, attr);
2062 static ssize_t show_bank(struct device *s, struct device_attribute *attr,
2065 return sprintf(buf, "%llx\n", attr_to_bank(attr)->ctl);
2068 static ssize_t set_bank(struct device *s, struct device_attribute *attr,
2069 const char *buf, size_t size)
2073 if (kstrtou64(buf, 0, &new) < 0)
2076 attr_to_bank(attr)->ctl = new;
2082 static ssize_t set_ignore_ce(struct device *s,
2083 struct device_attribute *attr,
2084 const char *buf, size_t size)
2088 if (kstrtou64(buf, 0, &new) < 0)
2091 if (mca_cfg.ignore_ce ^ !!new) {
2093 /* disable ce features */
2094 mce_timer_delete_all();
2095 on_each_cpu(mce_disable_cmci, NULL, 1);
2096 mca_cfg.ignore_ce = true;
2098 /* enable ce features */
2099 mca_cfg.ignore_ce = false;
2100 on_each_cpu(mce_enable_ce, (void *)1, 1);
2106 static ssize_t set_cmci_disabled(struct device *s,
2107 struct device_attribute *attr,
2108 const char *buf, size_t size)
2112 if (kstrtou64(buf, 0, &new) < 0)
2115 if (mca_cfg.cmci_disabled ^ !!new) {
2118 on_each_cpu(mce_disable_cmci, NULL, 1);
2119 mca_cfg.cmci_disabled = true;
2122 mca_cfg.cmci_disabled = false;
2123 on_each_cpu(mce_enable_ce, NULL, 1);
2129 static ssize_t store_int_with_restart(struct device *s,
2130 struct device_attribute *attr,
2131 const char *buf, size_t size)
2133 ssize_t ret = device_store_int(s, attr, buf, size);
2138 static DEVICE_INT_ATTR(tolerant, 0644, mca_cfg.tolerant);
2139 static DEVICE_INT_ATTR(monarch_timeout, 0644, mca_cfg.monarch_timeout);
2140 static DEVICE_BOOL_ATTR(dont_log_ce, 0644, mca_cfg.dont_log_ce);
2142 static struct dev_ext_attribute dev_attr_check_interval = {
2143 __ATTR(check_interval, 0644, device_show_int, store_int_with_restart),
2147 static struct dev_ext_attribute dev_attr_ignore_ce = {
2148 __ATTR(ignore_ce, 0644, device_show_bool, set_ignore_ce),
2152 static struct dev_ext_attribute dev_attr_cmci_disabled = {
2153 __ATTR(cmci_disabled, 0644, device_show_bool, set_cmci_disabled),
2154 &mca_cfg.cmci_disabled
2157 static struct device_attribute *mce_device_attrs[] = {
2158 &dev_attr_tolerant.attr,
2159 &dev_attr_check_interval.attr,
2160 #ifdef CONFIG_X86_MCELOG_LEGACY
2163 &dev_attr_monarch_timeout.attr,
2164 &dev_attr_dont_log_ce.attr,
2165 &dev_attr_ignore_ce.attr,
2166 &dev_attr_cmci_disabled.attr,
2170 static cpumask_var_t mce_device_initialized;
2172 static void mce_device_release(struct device *dev)
2177 /* Per cpu device init. All of the cpus still share the same ctrl bank: */
2178 static int mce_device_create(unsigned int cpu)
2184 if (!mce_available(&boot_cpu_data))
2187 dev = per_cpu(mce_device, cpu);
2191 dev = kzalloc(sizeof *dev, GFP_KERNEL);
2195 dev->bus = &mce_subsys;
2196 dev->release = &mce_device_release;
2198 err = device_register(dev);
2204 for (i = 0; mce_device_attrs[i]; i++) {
2205 err = device_create_file(dev, mce_device_attrs[i]);
2209 for (j = 0; j < mca_cfg.banks; j++) {
2210 err = device_create_file(dev, &mce_banks[j].attr);
2214 cpumask_set_cpu(cpu, mce_device_initialized);
2215 per_cpu(mce_device, cpu) = dev;
2220 device_remove_file(dev, &mce_banks[j].attr);
2223 device_remove_file(dev, mce_device_attrs[i]);
2225 device_unregister(dev);
2230 static void mce_device_remove(unsigned int cpu)
2232 struct device *dev = per_cpu(mce_device, cpu);
2235 if (!cpumask_test_cpu(cpu, mce_device_initialized))
2238 for (i = 0; mce_device_attrs[i]; i++)
2239 device_remove_file(dev, mce_device_attrs[i]);
2241 for (i = 0; i < mca_cfg.banks; i++)
2242 device_remove_file(dev, &mce_banks[i].attr);
2244 device_unregister(dev);
2245 cpumask_clear_cpu(cpu, mce_device_initialized);
2246 per_cpu(mce_device, cpu) = NULL;
2249 /* Make sure there are no machine checks on offlined CPUs. */
2250 static void mce_disable_cpu(void)
2252 if (!mce_available(raw_cpu_ptr(&cpu_info)))
2255 if (!cpuhp_tasks_frozen)
2258 vendor_disable_error_reporting();
2261 static void mce_reenable_cpu(void)
2265 if (!mce_available(raw_cpu_ptr(&cpu_info)))
2268 if (!cpuhp_tasks_frozen)
2270 for (i = 0; i < mca_cfg.banks; i++) {
2271 struct mce_bank *b = &mce_banks[i];
2274 wrmsrl(msr_ops.ctl(i), b->ctl);
2278 static int mce_cpu_dead(unsigned int cpu)
2280 mce_intel_hcpu_update(cpu);
2282 /* intentionally ignoring frozen here */
2283 if (!cpuhp_tasks_frozen)
2288 static int mce_cpu_online(unsigned int cpu)
2290 struct timer_list *t = this_cpu_ptr(&mce_timer);
2293 mce_device_create(cpu);
2295 ret = mce_threshold_create_device(cpu);
2297 mce_device_remove(cpu);
2305 static int mce_cpu_pre_down(unsigned int cpu)
2307 struct timer_list *t = this_cpu_ptr(&mce_timer);
2311 mce_threshold_remove_device(cpu);
2312 mce_device_remove(cpu);
2316 static __init void mce_init_banks(void)
2320 for (i = 0; i < mca_cfg.banks; i++) {
2321 struct mce_bank *b = &mce_banks[i];
2322 struct device_attribute *a = &b->attr;
2324 sysfs_attr_init(&a->attr);
2325 a->attr.name = b->attrname;
2326 snprintf(b->attrname, ATTR_LEN, "bank%d", i);
2328 a->attr.mode = 0644;
2329 a->show = show_bank;
2330 a->store = set_bank;
2334 static __init int mcheck_init_device(void)
2338 if (!mce_available(&boot_cpu_data)) {
2343 if (!zalloc_cpumask_var(&mce_device_initialized, GFP_KERNEL)) {
2350 err = subsys_system_register(&mce_subsys, NULL);
2354 err = cpuhp_setup_state(CPUHP_X86_MCE_DEAD, "x86/mce:dead", NULL,
2359 err = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "x86/mce:online",
2360 mce_cpu_online, mce_cpu_pre_down);
2362 goto err_out_online;
2364 register_syscore_ops(&mce_syscore_ops);
2369 cpuhp_remove_state(CPUHP_X86_MCE_DEAD);
2372 free_cpumask_var(mce_device_initialized);
2375 pr_err("Unable to init MCE device (rc: %d)\n", err);
2379 device_initcall_sync(mcheck_init_device);
2382 * Old style boot options parsing. Only for compatibility.
2384 static int __init mcheck_disable(char *str)
2386 mca_cfg.disabled = true;
2389 __setup("nomce", mcheck_disable);
2391 #ifdef CONFIG_DEBUG_FS
2392 struct dentry *mce_get_debugfs_dir(void)
2394 static struct dentry *dmce;
2397 dmce = debugfs_create_dir("mce", NULL);
2402 static void mce_reset(void)
2405 atomic_set(&mce_fake_panicked, 0);
2406 atomic_set(&mce_executing, 0);
2407 atomic_set(&mce_callin, 0);
2408 atomic_set(&global_nwo, 0);
2411 static int fake_panic_get(void *data, u64 *val)
2417 static int fake_panic_set(void *data, u64 val)
2424 DEFINE_SIMPLE_ATTRIBUTE(fake_panic_fops, fake_panic_get,
2425 fake_panic_set, "%llu\n");
2427 static int __init mcheck_debugfs_init(void)
2429 struct dentry *dmce, *ffake_panic;
2431 dmce = mce_get_debugfs_dir();
2434 ffake_panic = debugfs_create_file("fake_panic", 0444, dmce, NULL,
2442 static int __init mcheck_debugfs_init(void) { return -EINVAL; }
2445 DEFINE_STATIC_KEY_FALSE(mcsafe_key);
2446 EXPORT_SYMBOL_GPL(mcsafe_key);
2448 static int __init mcheck_late_init(void)
2450 if (mca_cfg.recovery)
2451 static_branch_inc(&mcsafe_key);
2453 mcheck_debugfs_init();
2457 * Flush out everything that has been logged during early boot, now that
2458 * everything has been initialized (workqueues, decoders, ...).
2460 mce_schedule_work();
2464 late_initcall(mcheck_late_init);