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/kallsyms.h>
18 #include <linux/rcupdate.h>
19 #include <linux/kobject.h>
20 #include <linux/uaccess.h>
21 #include <linux/kdebug.h>
22 #include <linux/kernel.h>
23 #include <linux/percpu.h>
24 #include <linux/string.h>
25 #include <linux/device.h>
26 #include <linux/syscore_ops.h>
27 #include <linux/delay.h>
28 #include <linux/ctype.h>
29 #include <linux/sched.h>
30 #include <linux/sysfs.h>
31 #include <linux/types.h>
32 #include <linux/slab.h>
33 #include <linux/init.h>
34 #include <linux/kmod.h>
35 #include <linux/poll.h>
36 #include <linux/nmi.h>
37 #include <linux/cpu.h>
38 #include <linux/smp.h>
41 #include <linux/debugfs.h>
42 #include <linux/irq_work.h>
43 #include <linux/export.h>
45 #include <asm/processor.h>
49 #include "mce-internal.h"
51 static DEFINE_MUTEX(mce_chrdev_read_mutex
);
53 #define rcu_dereference_check_mce(p) \
54 rcu_dereference_index_check((p), \
55 rcu_read_lock_sched_held() || \
56 lockdep_is_held(&mce_chrdev_read_mutex))
58 #define CREATE_TRACE_POINTS
59 #include <trace/events/mce.h>
61 #define SPINUNIT 100 /* 100ns */
65 DEFINE_PER_CPU(unsigned, mce_exception_count
);
67 struct mce_bank
*mce_banks __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 /* User mode helper program triggered by machine check event */
83 static unsigned long mce_need_notify
;
84 static char mce_helper
[128];
85 static char *mce_helper_argv
[2] = { mce_helper
, NULL
};
87 static DECLARE_WAIT_QUEUE_HEAD(mce_chrdev_wait
);
89 static DEFINE_PER_CPU(struct mce
, mces_seen
);
90 static int cpu_missing
;
92 /* MCA banks polled by the period polling timer for corrected events */
93 DEFINE_PER_CPU(mce_banks_t
, mce_poll_banks
) = {
94 [0 ... BITS_TO_LONGS(MAX_NR_BANKS
)-1] = ~0UL
97 static DEFINE_PER_CPU(struct work_struct
, mce_work
);
99 static void (*quirk_no_way_out
)(int bank
, struct mce
*m
, struct pt_regs
*regs
);
102 * CPU/chipset specific EDAC code can register a notifier call here to print
103 * MCE errors in a human-readable form.
105 ATOMIC_NOTIFIER_HEAD(x86_mce_decoder_chain
);
107 /* Do initial initialization of a struct mce */
108 void mce_setup(struct mce
*m
)
110 memset(m
, 0, sizeof(struct mce
));
111 m
->cpu
= m
->extcpu
= smp_processor_id();
113 /* We hope get_seconds stays lockless */
114 m
->time
= get_seconds();
115 m
->cpuvendor
= boot_cpu_data
.x86_vendor
;
116 m
->cpuid
= cpuid_eax(1);
117 m
->socketid
= cpu_data(m
->extcpu
).phys_proc_id
;
118 m
->apicid
= cpu_data(m
->extcpu
).initial_apicid
;
119 rdmsrl(MSR_IA32_MCG_CAP
, m
->mcgcap
);
122 DEFINE_PER_CPU(struct mce
, injectm
);
123 EXPORT_PER_CPU_SYMBOL_GPL(injectm
);
126 * Lockless MCE logging infrastructure.
127 * This avoids deadlocks on printk locks without having to break locks. Also
128 * separate MCEs from kernel messages to avoid bogus bug reports.
131 static struct mce_log mcelog
= {
132 .signature
= MCE_LOG_SIGNATURE
,
134 .recordlen
= sizeof(struct mce
),
137 void mce_log(struct mce
*mce
)
139 unsigned next
, entry
;
142 /* Emit the trace record: */
143 trace_mce_record(mce
);
145 ret
= atomic_notifier_call_chain(&x86_mce_decoder_chain
, 0, mce
);
146 if (ret
== NOTIFY_STOP
)
152 entry
= rcu_dereference_check_mce(mcelog
.next
);
156 * When the buffer fills up discard new entries.
157 * Assume that the earlier errors are the more
160 if (entry
>= MCE_LOG_LEN
) {
161 set_bit(MCE_OVERFLOW
,
162 (unsigned long *)&mcelog
.flags
);
165 /* Old left over entry. Skip: */
166 if (mcelog
.entry
[entry
].finished
) {
174 if (cmpxchg(&mcelog
.next
, entry
, next
) == entry
)
177 memcpy(mcelog
.entry
+ entry
, mce
, sizeof(struct mce
));
179 mcelog
.entry
[entry
].finished
= 1;
183 set_bit(0, &mce_need_notify
);
186 static void drain_mcelog_buffer(void)
188 unsigned int next
, i
, prev
= 0;
190 next
= ACCESS_ONCE(mcelog
.next
);
195 /* drain what was logged during boot */
196 for (i
= prev
; i
< next
; i
++) {
197 unsigned long start
= jiffies
;
198 unsigned retries
= 1;
200 m
= &mcelog
.entry
[i
];
202 while (!m
->finished
) {
203 if (time_after_eq(jiffies
, start
+ 2*retries
))
208 if (!m
->finished
&& retries
>= 4) {
209 pr_err("skipping error being logged currently!\n");
214 atomic_notifier_call_chain(&x86_mce_decoder_chain
, 0, m
);
217 memset(mcelog
.entry
+ prev
, 0, (next
- prev
) * sizeof(*m
));
219 next
= cmpxchg(&mcelog
.next
, prev
, 0);
220 } while (next
!= prev
);
224 void mce_register_decode_chain(struct notifier_block
*nb
)
226 atomic_notifier_chain_register(&x86_mce_decoder_chain
, nb
);
227 drain_mcelog_buffer();
229 EXPORT_SYMBOL_GPL(mce_register_decode_chain
);
231 void mce_unregister_decode_chain(struct notifier_block
*nb
)
233 atomic_notifier_chain_unregister(&x86_mce_decoder_chain
, nb
);
235 EXPORT_SYMBOL_GPL(mce_unregister_decode_chain
);
237 static void print_mce(struct mce
*m
)
241 pr_emerg(HW_ERR
"CPU %d: Machine Check Exception: %Lx Bank %d: %016Lx\n",
242 m
->extcpu
, m
->mcgstatus
, m
->bank
, m
->status
);
245 pr_emerg(HW_ERR
"RIP%s %02x:<%016Lx> ",
246 !(m
->mcgstatus
& MCG_STATUS_EIPV
) ? " !INEXACT!" : "",
249 if (m
->cs
== __KERNEL_CS
)
250 print_symbol("{%s}", m
->ip
);
254 pr_emerg(HW_ERR
"TSC %llx ", m
->tsc
);
256 pr_cont("ADDR %llx ", m
->addr
);
258 pr_cont("MISC %llx ", m
->misc
);
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
,
267 cpu_data(m
->extcpu
).microcode
);
270 * Print out human-readable details about the MCE error,
271 * (if the CPU has an implementation for that)
273 ret
= atomic_notifier_call_chain(&x86_mce_decoder_chain
, 0, m
);
274 if (ret
== NOTIFY_STOP
)
277 pr_emerg_ratelimited(HW_ERR
"Run the above through 'mcelog --ascii'\n");
280 #define PANIC_TIMEOUT 5 /* 5 seconds */
282 static atomic_t mce_paniced
;
284 static int fake_panic
;
285 static atomic_t mce_fake_paniced
;
287 /* Panic in progress. Enable interrupts and wait for final IPI */
288 static void wait_for_panic(void)
290 long timeout
= PANIC_TIMEOUT
*USEC_PER_SEC
;
294 while (timeout
-- > 0)
296 if (panic_timeout
== 0)
297 panic_timeout
= mca_cfg
.panic_timeout
;
298 panic("Panicing machine check CPU died");
301 static void mce_panic(char *msg
, struct mce
*final
, char *exp
)
307 * Make sure only one CPU runs in machine check panic
309 if (atomic_inc_return(&mce_paniced
) > 1)
316 /* Don't log too much for fake panic */
317 if (atomic_inc_return(&mce_fake_paniced
) > 1)
320 /* First print corrected ones that are still unlogged */
321 for (i
= 0; i
< MCE_LOG_LEN
; i
++) {
322 struct mce
*m
= &mcelog
.entry
[i
];
323 if (!(m
->status
& MCI_STATUS_VAL
))
325 if (!(m
->status
& MCI_STATUS_UC
)) {
328 apei_err
= apei_write_mce(m
);
331 /* Now print uncorrected but with the final one last */
332 for (i
= 0; i
< MCE_LOG_LEN
; i
++) {
333 struct mce
*m
= &mcelog
.entry
[i
];
334 if (!(m
->status
& MCI_STATUS_VAL
))
336 if (!(m
->status
& MCI_STATUS_UC
))
338 if (!final
|| memcmp(m
, final
, sizeof(struct mce
))) {
341 apei_err
= apei_write_mce(m
);
347 apei_err
= apei_write_mce(final
);
350 pr_emerg(HW_ERR
"Some CPUs didn't answer in synchronization\n");
352 pr_emerg(HW_ERR
"Machine check: %s\n", exp
);
354 if (panic_timeout
== 0)
355 panic_timeout
= mca_cfg
.panic_timeout
;
358 pr_emerg(HW_ERR
"Fake kernel panic: %s\n", msg
);
361 /* Support code for software error injection */
363 static int msr_to_offset(u32 msr
)
365 unsigned bank
= __this_cpu_read(injectm
.bank
);
367 if (msr
== mca_cfg
.rip_msr
)
368 return offsetof(struct mce
, ip
);
369 if (msr
== MSR_IA32_MCx_STATUS(bank
))
370 return offsetof(struct mce
, status
);
371 if (msr
== MSR_IA32_MCx_ADDR(bank
))
372 return offsetof(struct mce
, addr
);
373 if (msr
== MSR_IA32_MCx_MISC(bank
))
374 return offsetof(struct mce
, misc
);
375 if (msr
== MSR_IA32_MCG_STATUS
)
376 return offsetof(struct mce
, mcgstatus
);
380 /* MSR access wrappers used for error injection */
381 static u64
mce_rdmsrl(u32 msr
)
385 if (__this_cpu_read(injectm
.finished
)) {
386 int offset
= msr_to_offset(msr
);
390 return *(u64
*)((char *)&__get_cpu_var(injectm
) + offset
);
393 if (rdmsrl_safe(msr
, &v
)) {
394 WARN_ONCE(1, "mce: Unable to read msr %d!\n", msr
);
396 * Return zero in case the access faulted. This should
397 * not happen normally but can happen if the CPU does
398 * something weird, or if the code is buggy.
406 static void mce_wrmsrl(u32 msr
, u64 v
)
408 if (__this_cpu_read(injectm
.finished
)) {
409 int offset
= msr_to_offset(msr
);
412 *(u64
*)((char *)&__get_cpu_var(injectm
) + offset
) = v
;
419 * Collect all global (w.r.t. this processor) status about this machine
420 * check into our "mce" struct so that we can use it later to assess
421 * the severity of the problem as we read per-bank specific details.
423 static inline void mce_gather_info(struct mce
*m
, struct pt_regs
*regs
)
427 m
->mcgstatus
= mce_rdmsrl(MSR_IA32_MCG_STATUS
);
430 * Get the address of the instruction at the time of
431 * the machine check error.
433 if (m
->mcgstatus
& (MCG_STATUS_RIPV
|MCG_STATUS_EIPV
)) {
438 * When in VM86 mode make the cs look like ring 3
439 * always. This is a lie, but it's better than passing
440 * the additional vm86 bit around everywhere.
442 if (v8086_mode(regs
))
445 /* Use accurate RIP reporting if available. */
447 m
->ip
= mce_rdmsrl(mca_cfg
.rip_msr
);
452 * Simple lockless ring to communicate PFNs from the exception handler with the
453 * process context work function. This is vastly simplified because there's
454 * only a single reader and a single writer.
456 #define MCE_RING_SIZE 16 /* we use one entry less */
459 unsigned short start
;
461 unsigned long ring
[MCE_RING_SIZE
];
463 static DEFINE_PER_CPU(struct mce_ring
, mce_ring
);
465 /* Runs with CPU affinity in workqueue */
466 static int mce_ring_empty(void)
468 struct mce_ring
*r
= &__get_cpu_var(mce_ring
);
470 return r
->start
== r
->end
;
473 static int mce_ring_get(unsigned long *pfn
)
480 r
= &__get_cpu_var(mce_ring
);
481 if (r
->start
== r
->end
)
483 *pfn
= r
->ring
[r
->start
];
484 r
->start
= (r
->start
+ 1) % MCE_RING_SIZE
;
491 /* Always runs in MCE context with preempt off */
492 static int mce_ring_add(unsigned long pfn
)
494 struct mce_ring
*r
= &__get_cpu_var(mce_ring
);
497 next
= (r
->end
+ 1) % MCE_RING_SIZE
;
498 if (next
== r
->start
)
500 r
->ring
[r
->end
] = pfn
;
506 int mce_available(struct cpuinfo_x86
*c
)
508 if (mca_cfg
.disabled
)
510 return cpu_has(c
, X86_FEATURE_MCE
) && cpu_has(c
, X86_FEATURE_MCA
);
513 static void mce_schedule_work(void)
515 if (!mce_ring_empty()) {
516 struct work_struct
*work
= &__get_cpu_var(mce_work
);
517 if (!work_pending(work
))
522 DEFINE_PER_CPU(struct irq_work
, mce_irq_work
);
524 static void mce_irq_work_cb(struct irq_work
*entry
)
530 static void mce_report_event(struct pt_regs
*regs
)
532 if (regs
->flags
& (X86_VM_MASK
|X86_EFLAGS_IF
)) {
535 * Triggering the work queue here is just an insurance
536 * policy in case the syscall exit notify handler
537 * doesn't run soon enough or ends up running on the
538 * wrong CPU (can happen when audit sleeps)
544 irq_work_queue(&__get_cpu_var(mce_irq_work
));
548 * Read ADDR and MISC registers.
550 static void mce_read_aux(struct mce
*m
, int i
)
552 if (m
->status
& MCI_STATUS_MISCV
)
553 m
->misc
= mce_rdmsrl(MSR_IA32_MCx_MISC(i
));
554 if (m
->status
& MCI_STATUS_ADDRV
) {
555 m
->addr
= mce_rdmsrl(MSR_IA32_MCx_ADDR(i
));
558 * Mask the reported address by the reported granularity.
560 if (mca_cfg
.ser
&& (m
->status
& MCI_STATUS_MISCV
)) {
561 u8 shift
= MCI_MISC_ADDR_LSB(m
->misc
);
568 DEFINE_PER_CPU(unsigned, mce_poll_count
);
571 * Poll for corrected events or events that happened before reset.
572 * Those are just logged through /dev/mcelog.
574 * This is executed in standard interrupt context.
576 * Note: spec recommends to panic for fatal unsignalled
577 * errors here. However this would be quite problematic --
578 * we would need to reimplement the Monarch handling and
579 * it would mess up the exclusion between exception handler
580 * and poll hander -- * so we skip this for now.
581 * These cases should not happen anyways, or only when the CPU
582 * is already totally * confused. In this case it's likely it will
583 * not fully execute the machine check handler either.
585 void machine_check_poll(enum mcp_flags flags
, mce_banks_t
*b
)
590 this_cpu_inc(mce_poll_count
);
592 mce_gather_info(&m
, NULL
);
594 for (i
= 0; i
< mca_cfg
.banks
; i
++) {
595 if (!mce_banks
[i
].ctl
|| !test_bit(i
, *b
))
604 m
.status
= mce_rdmsrl(MSR_IA32_MCx_STATUS(i
));
605 if (!(m
.status
& MCI_STATUS_VAL
))
609 * Uncorrected or signalled events are handled by the exception
610 * handler when it is enabled, so don't process those here.
612 * TBD do the same check for MCI_STATUS_EN here?
614 if (!(flags
& MCP_UC
) &&
615 (m
.status
& (mca_cfg
.ser
? MCI_STATUS_S
: MCI_STATUS_UC
)))
620 if (!(flags
& MCP_TIMESTAMP
))
623 * Don't get the IP here because it's unlikely to
624 * have anything to do with the actual error location.
626 if (!(flags
& MCP_DONTLOG
) && !mca_cfg
.dont_log_ce
)
630 * Clear state for this bank.
632 mce_wrmsrl(MSR_IA32_MCx_STATUS(i
), 0);
636 * Don't clear MCG_STATUS here because it's only defined for
642 EXPORT_SYMBOL_GPL(machine_check_poll
);
645 * Do a quick check if any of the events requires a panic.
646 * This decides if we keep the events around or clear them.
648 static int mce_no_way_out(struct mce
*m
, char **msg
, unsigned long *validp
,
649 struct pt_regs
*regs
)
653 for (i
= 0; i
< mca_cfg
.banks
; i
++) {
654 m
->status
= mce_rdmsrl(MSR_IA32_MCx_STATUS(i
));
655 if (m
->status
& MCI_STATUS_VAL
) {
656 __set_bit(i
, validp
);
657 if (quirk_no_way_out
)
658 quirk_no_way_out(i
, m
, regs
);
660 if (mce_severity(m
, mca_cfg
.tolerant
, msg
) >= MCE_PANIC_SEVERITY
)
667 * Variable to establish order between CPUs while scanning.
668 * Each CPU spins initially until executing is equal its number.
670 static atomic_t mce_executing
;
673 * Defines order of CPUs on entry. First CPU becomes Monarch.
675 static atomic_t mce_callin
;
678 * Check if a timeout waiting for other CPUs happened.
680 static int mce_timed_out(u64
*t
)
683 * The others already did panic for some reason.
684 * Bail out like in a timeout.
685 * rmb() to tell the compiler that system_state
686 * might have been modified by someone else.
689 if (atomic_read(&mce_paniced
))
691 if (!mca_cfg
.monarch_timeout
)
693 if ((s64
)*t
< SPINUNIT
) {
694 /* CHECKME: Make panic default for 1 too? */
695 if (mca_cfg
.tolerant
< 1)
696 mce_panic("Timeout synchronizing machine check over CPUs",
703 touch_nmi_watchdog();
708 * The Monarch's reign. The Monarch is the CPU who entered
709 * the machine check handler first. It waits for the others to
710 * raise the exception too and then grades them. When any
711 * error is fatal panic. Only then let the others continue.
713 * The other CPUs entering the MCE handler will be controlled by the
714 * Monarch. They are called Subjects.
716 * This way we prevent any potential data corruption in a unrecoverable case
717 * and also makes sure always all CPU's errors are examined.
719 * Also this detects the case of a machine check event coming from outer
720 * space (not detected by any CPUs) In this case some external agent wants
721 * us to shut down, so panic too.
723 * The other CPUs might still decide to panic if the handler happens
724 * in a unrecoverable place, but in this case the system is in a semi-stable
725 * state and won't corrupt anything by itself. It's ok to let the others
726 * continue for a bit first.
728 * All the spin loops have timeouts; when a timeout happens a CPU
729 * typically elects itself to be Monarch.
731 static void mce_reign(void)
734 struct mce
*m
= NULL
;
735 int global_worst
= 0;
740 * This CPU is the Monarch and the other CPUs have run
741 * through their handlers.
742 * Grade the severity of the errors of all the CPUs.
744 for_each_possible_cpu(cpu
) {
745 int severity
= mce_severity(&per_cpu(mces_seen
, cpu
),
748 if (severity
> global_worst
) {
750 global_worst
= severity
;
751 m
= &per_cpu(mces_seen
, cpu
);
756 * Cannot recover? Panic here then.
757 * This dumps all the mces in the log buffer and stops the
760 if (m
&& global_worst
>= MCE_PANIC_SEVERITY
&& mca_cfg
.tolerant
< 3)
761 mce_panic("Fatal Machine check", m
, msg
);
764 * For UC somewhere we let the CPU who detects it handle it.
765 * Also must let continue the others, otherwise the handling
766 * CPU could deadlock on a lock.
770 * No machine check event found. Must be some external
771 * source or one CPU is hung. Panic.
773 if (global_worst
<= MCE_KEEP_SEVERITY
&& mca_cfg
.tolerant
< 3)
774 mce_panic("Machine check from unknown source", NULL
, NULL
);
777 * Now clear all the mces_seen so that they don't reappear on
780 for_each_possible_cpu(cpu
)
781 memset(&per_cpu(mces_seen
, cpu
), 0, sizeof(struct mce
));
784 static atomic_t global_nwo
;
787 * Start of Monarch synchronization. This waits until all CPUs have
788 * entered the exception handler and then determines if any of them
789 * saw a fatal event that requires panic. Then it executes them
790 * in the entry order.
791 * TBD double check parallel CPU hotunplug
793 static int mce_start(int *no_way_out
)
796 int cpus
= num_online_cpus();
797 u64 timeout
= (u64
)mca_cfg
.monarch_timeout
* NSEC_PER_USEC
;
802 atomic_add(*no_way_out
, &global_nwo
);
804 * global_nwo should be updated before mce_callin
807 order
= atomic_inc_return(&mce_callin
);
812 while (atomic_read(&mce_callin
) != cpus
) {
813 if (mce_timed_out(&timeout
)) {
814 atomic_set(&global_nwo
, 0);
821 * mce_callin should be read before global_nwo
827 * Monarch: Starts executing now, the others wait.
829 atomic_set(&mce_executing
, 1);
832 * Subject: Now start the scanning loop one by one in
833 * the original callin order.
834 * This way when there are any shared banks it will be
835 * only seen by one CPU before cleared, avoiding duplicates.
837 while (atomic_read(&mce_executing
) < order
) {
838 if (mce_timed_out(&timeout
)) {
839 atomic_set(&global_nwo
, 0);
847 * Cache the global no_way_out state.
849 *no_way_out
= atomic_read(&global_nwo
);
855 * Synchronize between CPUs after main scanning loop.
856 * This invokes the bulk of the Monarch processing.
858 static int mce_end(int order
)
861 u64 timeout
= (u64
)mca_cfg
.monarch_timeout
* NSEC_PER_USEC
;
869 * Allow others to run.
871 atomic_inc(&mce_executing
);
874 /* CHECKME: Can this race with a parallel hotplug? */
875 int cpus
= num_online_cpus();
878 * Monarch: Wait for everyone to go through their scanning
881 while (atomic_read(&mce_executing
) <= cpus
) {
882 if (mce_timed_out(&timeout
))
892 * Subject: Wait for Monarch to finish.
894 while (atomic_read(&mce_executing
) != 0) {
895 if (mce_timed_out(&timeout
))
901 * Don't reset anything. That's done by the Monarch.
907 * Reset all global state.
910 atomic_set(&global_nwo
, 0);
911 atomic_set(&mce_callin
, 0);
915 * Let others run again.
917 atomic_set(&mce_executing
, 0);
922 * Check if the address reported by the CPU is in a format we can parse.
923 * It would be possible to add code for most other cases, but all would
924 * be somewhat complicated (e.g. segment offset would require an instruction
925 * parser). So only support physical addresses up to page granuality for now.
927 static int mce_usable_address(struct mce
*m
)
929 if (!(m
->status
& MCI_STATUS_MISCV
) || !(m
->status
& MCI_STATUS_ADDRV
))
931 if (MCI_MISC_ADDR_LSB(m
->misc
) > PAGE_SHIFT
)
933 if (MCI_MISC_ADDR_MODE(m
->misc
) != MCI_MISC_ADDR_PHYS
)
938 static void mce_clear_state(unsigned long *toclear
)
942 for (i
= 0; i
< mca_cfg
.banks
; i
++) {
943 if (test_bit(i
, toclear
))
944 mce_wrmsrl(MSR_IA32_MCx_STATUS(i
), 0);
949 * Need to save faulting physical address associated with a process
950 * in the machine check handler some place where we can grab it back
951 * later in mce_notify_process()
953 #define MCE_INFO_MAX 16
957 struct task_struct
*t
;
960 } mce_info
[MCE_INFO_MAX
];
962 static void mce_save_info(__u64 addr
, int c
)
966 for (mi
= mce_info
; mi
< &mce_info
[MCE_INFO_MAX
]; mi
++) {
967 if (atomic_cmpxchg(&mi
->inuse
, 0, 1) == 0) {
975 mce_panic("Too many concurrent recoverable errors", NULL
, NULL
);
978 static struct mce_info
*mce_find_info(void)
982 for (mi
= mce_info
; mi
< &mce_info
[MCE_INFO_MAX
]; mi
++)
983 if (atomic_read(&mi
->inuse
) && mi
->t
== current
)
988 static void mce_clear_info(struct mce_info
*mi
)
990 atomic_set(&mi
->inuse
, 0);
994 * The actual machine check handler. This only handles real
995 * exceptions when something got corrupted coming in through int 18.
997 * This is executed in NMI context not subject to normal locking rules. This
998 * implies that most kernel services cannot be safely used. Don't even
999 * think about putting a printk in there!
1001 * On Intel systems this is entered on all CPUs in parallel through
1002 * MCE broadcast. However some CPUs might be broken beyond repair,
1003 * so be always careful when synchronizing with others.
1005 void do_machine_check(struct pt_regs
*regs
, long error_code
)
1007 struct mca_config
*cfg
= &mca_cfg
;
1008 struct mce m
, *final
;
1013 * Establish sequential order between the CPUs entering the machine
1018 * If no_way_out gets set, there is no safe way to recover from this
1019 * MCE. If mca_cfg.tolerant is cranked up, we'll try anyway.
1023 * If kill_it gets set, there might be a way to recover from this
1027 DECLARE_BITMAP(toclear
, MAX_NR_BANKS
);
1028 DECLARE_BITMAP(valid_banks
, MAX_NR_BANKS
);
1029 char *msg
= "Unknown";
1031 atomic_inc(&mce_entry
);
1033 this_cpu_inc(mce_exception_count
);
1038 mce_gather_info(&m
, regs
);
1040 final
= &__get_cpu_var(mces_seen
);
1043 memset(valid_banks
, 0, sizeof(valid_banks
));
1044 no_way_out
= mce_no_way_out(&m
, &msg
, valid_banks
, regs
);
1049 * When no restart IP might need to kill or panic.
1050 * Assume the worst for now, but if we find the
1051 * severity is MCE_AR_SEVERITY we have other options.
1053 if (!(m
.mcgstatus
& MCG_STATUS_RIPV
))
1057 * Go through all the banks in exclusion of the other CPUs.
1058 * This way we don't report duplicated events on shared banks
1059 * because the first one to see it will clear it.
1061 order
= mce_start(&no_way_out
);
1062 for (i
= 0; i
< cfg
->banks
; i
++) {
1063 __clear_bit(i
, toclear
);
1064 if (!test_bit(i
, valid_banks
))
1066 if (!mce_banks
[i
].ctl
)
1073 m
.status
= mce_rdmsrl(MSR_IA32_MCx_STATUS(i
));
1074 if ((m
.status
& MCI_STATUS_VAL
) == 0)
1078 * Non uncorrected or non signaled errors are handled by
1079 * machine_check_poll. Leave them alone, unless this panics.
1081 if (!(m
.status
& (cfg
->ser
? MCI_STATUS_S
: MCI_STATUS_UC
)) &&
1086 * Set taint even when machine check was not enabled.
1088 add_taint(TAINT_MACHINE_CHECK
);
1090 severity
= mce_severity(&m
, cfg
->tolerant
, NULL
);
1093 * When machine check was for corrected handler don't touch,
1094 * unless we're panicing.
1096 if (severity
== MCE_KEEP_SEVERITY
&& !no_way_out
)
1098 __set_bit(i
, toclear
);
1099 if (severity
== MCE_NO_SEVERITY
) {
1101 * Machine check event was not enabled. Clear, but
1107 mce_read_aux(&m
, i
);
1110 * Action optional error. Queue address for later processing.
1111 * When the ring overflows we just ignore the AO error.
1112 * RED-PEN add some logging mechanism when
1113 * usable_address or mce_add_ring fails.
1114 * RED-PEN don't ignore overflow for mca_cfg.tolerant == 0
1116 if (severity
== MCE_AO_SEVERITY
&& mce_usable_address(&m
))
1117 mce_ring_add(m
.addr
>> PAGE_SHIFT
);
1121 if (severity
> worst
) {
1127 /* mce_clear_state will clear *final, save locally for use later */
1131 mce_clear_state(toclear
);
1134 * Do most of the synchronization with other CPUs.
1135 * When there's any problem use only local no_way_out state.
1137 if (mce_end(order
) < 0)
1138 no_way_out
= worst
>= MCE_PANIC_SEVERITY
;
1141 * At insane "tolerant" levels we take no action. Otherwise
1142 * we only die if we have no other choice. For less serious
1143 * issues we try to recover, or limit damage to the current
1146 if (cfg
->tolerant
< 3) {
1148 mce_panic("Fatal machine check on current CPU", &m
, msg
);
1149 if (worst
== MCE_AR_SEVERITY
) {
1150 /* schedule action before return to userland */
1151 mce_save_info(m
.addr
, m
.mcgstatus
& MCG_STATUS_RIPV
);
1152 set_thread_flag(TIF_MCE_NOTIFY
);
1153 } else if (kill_it
) {
1154 force_sig(SIGBUS
, current
);
1159 mce_report_event(regs
);
1160 mce_wrmsrl(MSR_IA32_MCG_STATUS
, 0);
1162 atomic_dec(&mce_entry
);
1165 EXPORT_SYMBOL_GPL(do_machine_check
);
1167 #ifndef CONFIG_MEMORY_FAILURE
1168 int memory_failure(unsigned long pfn
, int vector
, int flags
)
1170 /* mce_severity() should not hand us an ACTION_REQUIRED error */
1171 BUG_ON(flags
& MF_ACTION_REQUIRED
);
1172 pr_err("Uncorrected memory error in page 0x%lx ignored\n"
1173 "Rebuild kernel with CONFIG_MEMORY_FAILURE=y for smarter handling\n",
1181 * Called in process context that interrupted by MCE and marked with
1182 * TIF_MCE_NOTIFY, just before returning to erroneous userland.
1183 * This code is allowed to sleep.
1184 * Attempt possible recovery such as calling the high level VM handler to
1185 * process any corrupted pages, and kill/signal current process if required.
1186 * Action required errors are handled here.
1188 void mce_notify_process(void)
1191 struct mce_info
*mi
= mce_find_info();
1192 int flags
= MF_ACTION_REQUIRED
;
1195 mce_panic("Lost physical address for unconsumed uncorrectable error", NULL
, NULL
);
1196 pfn
= mi
->paddr
>> PAGE_SHIFT
;
1198 clear_thread_flag(TIF_MCE_NOTIFY
);
1200 pr_err("Uncorrected hardware memory error in user-access at %llx",
1203 * We must call memory_failure() here even if the current process is
1204 * doomed. We still need to mark the page as poisoned and alert any
1205 * other users of the page.
1207 if (!mi
->restartable
)
1208 flags
|= MF_MUST_KILL
;
1209 if (memory_failure(pfn
, MCE_VECTOR
, flags
) < 0) {
1210 pr_err("Memory error not recovered");
1211 force_sig(SIGBUS
, current
);
1217 * Action optional processing happens here (picking up
1218 * from the list of faulting pages that do_machine_check()
1219 * placed into the "ring").
1221 static void mce_process_work(struct work_struct
*dummy
)
1225 while (mce_ring_get(&pfn
))
1226 memory_failure(pfn
, MCE_VECTOR
, 0);
1229 #ifdef CONFIG_X86_MCE_INTEL
1231 * mce_log_therm_throt_event - Logs the thermal throttling event to mcelog
1232 * @cpu: The CPU on which the event occurred.
1233 * @status: Event status information
1235 * This function should be called by the thermal interrupt after the
1236 * event has been processed and the decision was made to log the event
1239 * The status parameter will be saved to the 'status' field of 'struct mce'
1240 * and historically has been the register value of the
1241 * MSR_IA32_THERMAL_STATUS (Intel) msr.
1243 void mce_log_therm_throt_event(__u64 status
)
1248 m
.bank
= MCE_THERMAL_BANK
;
1252 #endif /* CONFIG_X86_MCE_INTEL */
1255 * Periodic polling timer for "silent" machine check errors. If the
1256 * poller finds an MCE, poll 2x faster. When the poller finds no more
1257 * errors, poll 2x slower (up to check_interval seconds).
1259 static unsigned long check_interval
= 5 * 60; /* 5 minutes */
1261 static DEFINE_PER_CPU(unsigned long, mce_next_interval
); /* in jiffies */
1262 static DEFINE_PER_CPU(struct timer_list
, mce_timer
);
1264 static unsigned long mce_adjust_timer_default(unsigned long interval
)
1269 static unsigned long (*mce_adjust_timer
)(unsigned long interval
) =
1270 mce_adjust_timer_default
;
1272 static void mce_timer_fn(unsigned long data
)
1274 struct timer_list
*t
= &__get_cpu_var(mce_timer
);
1277 WARN_ON(smp_processor_id() != data
);
1279 if (mce_available(__this_cpu_ptr(&cpu_info
))) {
1280 machine_check_poll(MCP_TIMESTAMP
,
1281 &__get_cpu_var(mce_poll_banks
));
1282 mce_intel_cmci_poll();
1286 * Alert userspace if needed. If we logged an MCE, reduce the
1287 * polling interval, otherwise increase the polling interval.
1289 iv
= __this_cpu_read(mce_next_interval
);
1290 if (mce_notify_irq()) {
1291 iv
= max(iv
/ 2, (unsigned long) HZ
/100);
1293 iv
= min(iv
* 2, round_jiffies_relative(check_interval
* HZ
));
1294 iv
= mce_adjust_timer(iv
);
1296 __this_cpu_write(mce_next_interval
, iv
);
1297 /* Might have become 0 after CMCI storm subsided */
1299 t
->expires
= jiffies
+ iv
;
1300 add_timer_on(t
, smp_processor_id());
1305 * Ensure that the timer is firing in @interval from now.
1307 void mce_timer_kick(unsigned long interval
)
1309 struct timer_list
*t
= &__get_cpu_var(mce_timer
);
1310 unsigned long when
= jiffies
+ interval
;
1311 unsigned long iv
= __this_cpu_read(mce_next_interval
);
1313 if (timer_pending(t
)) {
1314 if (time_before(when
, t
->expires
))
1315 mod_timer_pinned(t
, when
);
1317 t
->expires
= round_jiffies(when
);
1318 add_timer_on(t
, smp_processor_id());
1321 __this_cpu_write(mce_next_interval
, interval
);
1324 /* Must not be called in IRQ context where del_timer_sync() can deadlock */
1325 static void mce_timer_delete_all(void)
1329 for_each_online_cpu(cpu
)
1330 del_timer_sync(&per_cpu(mce_timer
, cpu
));
1333 static void mce_do_trigger(struct work_struct
*work
)
1335 call_usermodehelper(mce_helper
, mce_helper_argv
, NULL
, UMH_NO_WAIT
);
1338 static DECLARE_WORK(mce_trigger_work
, mce_do_trigger
);
1341 * Notify the user(s) about new machine check events.
1342 * Can be called from interrupt context, but not from machine check/NMI
1345 int mce_notify_irq(void)
1347 /* Not more than two messages every minute */
1348 static DEFINE_RATELIMIT_STATE(ratelimit
, 60*HZ
, 2);
1350 if (test_and_clear_bit(0, &mce_need_notify
)) {
1351 /* wake processes polling /dev/mcelog */
1352 wake_up_interruptible(&mce_chrdev_wait
);
1355 * There is no risk of missing notifications because
1356 * work_pending is always cleared before the function is
1359 if (mce_helper
[0] && !work_pending(&mce_trigger_work
))
1360 schedule_work(&mce_trigger_work
);
1362 if (__ratelimit(&ratelimit
))
1363 pr_info(HW_ERR
"Machine check events logged\n");
1369 EXPORT_SYMBOL_GPL(mce_notify_irq
);
1371 static int __cpuinit
__mcheck_cpu_mce_banks_init(void)
1374 u8 num_banks
= mca_cfg
.banks
;
1376 mce_banks
= kzalloc(num_banks
* sizeof(struct mce_bank
), GFP_KERNEL
);
1380 for (i
= 0; i
< num_banks
; i
++) {
1381 struct mce_bank
*b
= &mce_banks
[i
];
1390 * Initialize Machine Checks for a CPU.
1392 static int __cpuinit
__mcheck_cpu_cap_init(void)
1397 rdmsrl(MSR_IA32_MCG_CAP
, cap
);
1399 b
= cap
& MCG_BANKCNT_MASK
;
1401 pr_info("CPU supports %d MCE banks\n", b
);
1403 if (b
> MAX_NR_BANKS
) {
1404 pr_warn("Using only %u machine check banks out of %u\n",
1409 /* Don't support asymmetric configurations today */
1410 WARN_ON(mca_cfg
.banks
!= 0 && b
!= mca_cfg
.banks
);
1414 int err
= __mcheck_cpu_mce_banks_init();
1420 /* Use accurate RIP reporting if available. */
1421 if ((cap
& MCG_EXT_P
) && MCG_EXT_CNT(cap
) >= 9)
1422 mca_cfg
.rip_msr
= MSR_IA32_MCG_EIP
;
1424 if (cap
& MCG_SER_P
)
1430 static void __mcheck_cpu_init_generic(void)
1432 enum mcp_flags m_fl
= 0;
1433 mce_banks_t all_banks
;
1437 if (!mca_cfg
.bootlog
)
1441 * Log the machine checks left over from the previous reset.
1443 bitmap_fill(all_banks
, MAX_NR_BANKS
);
1444 machine_check_poll(MCP_UC
| m_fl
, &all_banks
);
1446 set_in_cr4(X86_CR4_MCE
);
1448 rdmsrl(MSR_IA32_MCG_CAP
, cap
);
1449 if (cap
& MCG_CTL_P
)
1450 wrmsr(MSR_IA32_MCG_CTL
, 0xffffffff, 0xffffffff);
1452 for (i
= 0; i
< mca_cfg
.banks
; i
++) {
1453 struct mce_bank
*b
= &mce_banks
[i
];
1457 wrmsrl(MSR_IA32_MCx_CTL(i
), b
->ctl
);
1458 wrmsrl(MSR_IA32_MCx_STATUS(i
), 0);
1463 * During IFU recovery Sandy Bridge -EP4S processors set the RIPV and
1464 * EIPV bits in MCG_STATUS to zero on the affected logical processor (SDM
1465 * Vol 3B Table 15-20). But this confuses both the code that determines
1466 * whether the machine check occurred in kernel or user mode, and also
1467 * the severity assessment code. Pretend that EIPV was set, and take the
1468 * ip/cs values from the pt_regs that mce_gather_info() ignored earlier.
1470 static void quirk_sandybridge_ifu(int bank
, struct mce
*m
, struct pt_regs
*regs
)
1474 if ((m
->mcgstatus
& (MCG_STATUS_EIPV
|MCG_STATUS_RIPV
)) != 0)
1476 if ((m
->status
& (MCI_STATUS_OVER
|MCI_STATUS_UC
|
1477 MCI_STATUS_EN
|MCI_STATUS_MISCV
|MCI_STATUS_ADDRV
|
1478 MCI_STATUS_PCC
|MCI_STATUS_S
|MCI_STATUS_AR
|
1480 (MCI_STATUS_UC
|MCI_STATUS_EN
|
1481 MCI_STATUS_MISCV
|MCI_STATUS_ADDRV
|MCI_STATUS_S
|
1482 MCI_STATUS_AR
|MCACOD_INSTR
))
1485 m
->mcgstatus
|= MCG_STATUS_EIPV
;
1490 /* Add per CPU specific workarounds here */
1491 static int __cpuinit
__mcheck_cpu_apply_quirks(struct cpuinfo_x86
*c
)
1493 struct mca_config
*cfg
= &mca_cfg
;
1495 if (c
->x86_vendor
== X86_VENDOR_UNKNOWN
) {
1496 pr_info("unknown CPU type - not enabling MCE support\n");
1500 /* This should be disabled by the BIOS, but isn't always */
1501 if (c
->x86_vendor
== X86_VENDOR_AMD
) {
1502 if (c
->x86
== 15 && cfg
->banks
> 4) {
1504 * disable GART TBL walk error reporting, which
1505 * trips off incorrectly with the IOMMU & 3ware
1508 clear_bit(10, (unsigned long *)&mce_banks
[4].ctl
);
1510 if (c
->x86
<= 17 && cfg
->bootlog
< 0) {
1512 * Lots of broken BIOS around that don't clear them
1513 * by default and leave crap in there. Don't log:
1518 * Various K7s with broken bank 0 around. Always disable
1521 if (c
->x86
== 6 && cfg
->banks
> 0)
1522 mce_banks
[0].ctl
= 0;
1525 * Turn off MC4_MISC thresholding banks on those models since
1526 * they're not supported there.
1528 if (c
->x86
== 0x15 &&
1529 (c
->x86_model
>= 0x10 && c
->x86_model
<= 0x1f)) {
1534 0x00000413, /* MC4_MISC0 */
1535 0xc0000408, /* MC4_MISC1 */
1538 rdmsrl(MSR_K7_HWCR
, hwcr
);
1540 /* McStatusWrEn has to be set */
1541 need_toggle
= !(hwcr
& BIT(18));
1544 wrmsrl(MSR_K7_HWCR
, hwcr
| BIT(18));
1546 for (i
= 0; i
< ARRAY_SIZE(msrs
); i
++) {
1547 rdmsrl(msrs
[i
], val
);
1550 if (val
& BIT_64(62)) {
1552 wrmsrl(msrs
[i
], val
);
1556 /* restore old settings */
1558 wrmsrl(MSR_K7_HWCR
, hwcr
);
1562 if (c
->x86_vendor
== X86_VENDOR_INTEL
) {
1564 * SDM documents that on family 6 bank 0 should not be written
1565 * because it aliases to another special BIOS controlled
1567 * But it's not aliased anymore on model 0x1a+
1568 * Don't ignore bank 0 completely because there could be a
1569 * valid event later, merely don't write CTL0.
1572 if (c
->x86
== 6 && c
->x86_model
< 0x1A && cfg
->banks
> 0)
1573 mce_banks
[0].init
= 0;
1576 * All newer Intel systems support MCE broadcasting. Enable
1577 * synchronization with a one second timeout.
1579 if ((c
->x86
> 6 || (c
->x86
== 6 && c
->x86_model
>= 0xe)) &&
1580 cfg
->monarch_timeout
< 0)
1581 cfg
->monarch_timeout
= USEC_PER_SEC
;
1584 * There are also broken BIOSes on some Pentium M and
1587 if (c
->x86
== 6 && c
->x86_model
<= 13 && cfg
->bootlog
< 0)
1590 if (c
->x86
== 6 && c
->x86_model
== 45)
1591 quirk_no_way_out
= quirk_sandybridge_ifu
;
1593 if (cfg
->monarch_timeout
< 0)
1594 cfg
->monarch_timeout
= 0;
1595 if (cfg
->bootlog
!= 0)
1596 cfg
->panic_timeout
= 30;
1601 static int __cpuinit
__mcheck_cpu_ancient_init(struct cpuinfo_x86
*c
)
1606 switch (c
->x86_vendor
) {
1607 case X86_VENDOR_INTEL
:
1608 intel_p5_mcheck_init(c
);
1611 case X86_VENDOR_CENTAUR
:
1612 winchip_mcheck_init(c
);
1620 static void __mcheck_cpu_init_vendor(struct cpuinfo_x86
*c
)
1622 switch (c
->x86_vendor
) {
1623 case X86_VENDOR_INTEL
:
1624 mce_intel_feature_init(c
);
1625 mce_adjust_timer
= mce_intel_adjust_timer
;
1627 case X86_VENDOR_AMD
:
1628 mce_amd_feature_init(c
);
1635 static void mce_start_timer(unsigned int cpu
, struct timer_list
*t
)
1637 unsigned long iv
= mce_adjust_timer(check_interval
* HZ
);
1639 __this_cpu_write(mce_next_interval
, iv
);
1641 if (mca_cfg
.ignore_ce
|| !iv
)
1644 t
->expires
= round_jiffies(jiffies
+ iv
);
1645 add_timer_on(t
, smp_processor_id());
1648 static void __mcheck_cpu_init_timer(void)
1650 struct timer_list
*t
= &__get_cpu_var(mce_timer
);
1651 unsigned int cpu
= smp_processor_id();
1653 setup_timer(t
, mce_timer_fn
, cpu
);
1654 mce_start_timer(cpu
, t
);
1657 /* Handle unconfigured int18 (should never happen) */
1658 static void unexpected_machine_check(struct pt_regs
*regs
, long error_code
)
1660 pr_err("CPU#%d: Unexpected int18 (Machine Check)\n",
1661 smp_processor_id());
1664 /* Call the installed machine check handler for this CPU setup. */
1665 void (*machine_check_vector
)(struct pt_regs
*, long error_code
) =
1666 unexpected_machine_check
;
1669 * Called for each booted CPU to set up machine checks.
1670 * Must be called with preempt off:
1672 void __cpuinit
mcheck_cpu_init(struct cpuinfo_x86
*c
)
1674 if (mca_cfg
.disabled
)
1677 if (__mcheck_cpu_ancient_init(c
))
1680 if (!mce_available(c
))
1683 if (__mcheck_cpu_cap_init() < 0 || __mcheck_cpu_apply_quirks(c
) < 0) {
1684 mca_cfg
.disabled
= true;
1688 machine_check_vector
= do_machine_check
;
1690 __mcheck_cpu_init_generic();
1691 __mcheck_cpu_init_vendor(c
);
1692 __mcheck_cpu_init_timer();
1693 INIT_WORK(&__get_cpu_var(mce_work
), mce_process_work
);
1694 init_irq_work(&__get_cpu_var(mce_irq_work
), &mce_irq_work_cb
);
1698 * mce_chrdev: Character device /dev/mcelog to read and clear the MCE log.
1701 static DEFINE_SPINLOCK(mce_chrdev_state_lock
);
1702 static int mce_chrdev_open_count
; /* #times opened */
1703 static int mce_chrdev_open_exclu
; /* already open exclusive? */
1705 static int mce_chrdev_open(struct inode
*inode
, struct file
*file
)
1707 spin_lock(&mce_chrdev_state_lock
);
1709 if (mce_chrdev_open_exclu
||
1710 (mce_chrdev_open_count
&& (file
->f_flags
& O_EXCL
))) {
1711 spin_unlock(&mce_chrdev_state_lock
);
1716 if (file
->f_flags
& O_EXCL
)
1717 mce_chrdev_open_exclu
= 1;
1718 mce_chrdev_open_count
++;
1720 spin_unlock(&mce_chrdev_state_lock
);
1722 return nonseekable_open(inode
, file
);
1725 static int mce_chrdev_release(struct inode
*inode
, struct file
*file
)
1727 spin_lock(&mce_chrdev_state_lock
);
1729 mce_chrdev_open_count
--;
1730 mce_chrdev_open_exclu
= 0;
1732 spin_unlock(&mce_chrdev_state_lock
);
1737 static void collect_tscs(void *data
)
1739 unsigned long *cpu_tsc
= (unsigned long *)data
;
1741 rdtscll(cpu_tsc
[smp_processor_id()]);
1744 static int mce_apei_read_done
;
1746 /* Collect MCE record of previous boot in persistent storage via APEI ERST. */
1747 static int __mce_read_apei(char __user
**ubuf
, size_t usize
)
1753 if (usize
< sizeof(struct mce
))
1756 rc
= apei_read_mce(&m
, &record_id
);
1757 /* Error or no more MCE record */
1759 mce_apei_read_done
= 1;
1761 * When ERST is disabled, mce_chrdev_read() should return
1762 * "no record" instead of "no device."
1769 if (copy_to_user(*ubuf
, &m
, sizeof(struct mce
)))
1772 * In fact, we should have cleared the record after that has
1773 * been flushed to the disk or sent to network in
1774 * /sbin/mcelog, but we have no interface to support that now,
1775 * so just clear it to avoid duplication.
1777 rc
= apei_clear_mce(record_id
);
1779 mce_apei_read_done
= 1;
1782 *ubuf
+= sizeof(struct mce
);
1787 static ssize_t
mce_chrdev_read(struct file
*filp
, char __user
*ubuf
,
1788 size_t usize
, loff_t
*off
)
1790 char __user
*buf
= ubuf
;
1791 unsigned long *cpu_tsc
;
1792 unsigned prev
, next
;
1795 cpu_tsc
= kmalloc(nr_cpu_ids
* sizeof(long), GFP_KERNEL
);
1799 mutex_lock(&mce_chrdev_read_mutex
);
1801 if (!mce_apei_read_done
) {
1802 err
= __mce_read_apei(&buf
, usize
);
1803 if (err
|| buf
!= ubuf
)
1807 next
= rcu_dereference_check_mce(mcelog
.next
);
1809 /* Only supports full reads right now */
1811 if (*off
!= 0 || usize
< MCE_LOG_LEN
*sizeof(struct mce
))
1817 for (i
= prev
; i
< next
; i
++) {
1818 unsigned long start
= jiffies
;
1819 struct mce
*m
= &mcelog
.entry
[i
];
1821 while (!m
->finished
) {
1822 if (time_after_eq(jiffies
, start
+ 2)) {
1823 memset(m
, 0, sizeof(*m
));
1829 err
|= copy_to_user(buf
, m
, sizeof(*m
));
1835 memset(mcelog
.entry
+ prev
, 0,
1836 (next
- prev
) * sizeof(struct mce
));
1838 next
= cmpxchg(&mcelog
.next
, prev
, 0);
1839 } while (next
!= prev
);
1841 synchronize_sched();
1844 * Collect entries that were still getting written before the
1847 on_each_cpu(collect_tscs
, cpu_tsc
, 1);
1849 for (i
= next
; i
< MCE_LOG_LEN
; i
++) {
1850 struct mce
*m
= &mcelog
.entry
[i
];
1852 if (m
->finished
&& m
->tsc
< cpu_tsc
[m
->cpu
]) {
1853 err
|= copy_to_user(buf
, m
, sizeof(*m
));
1856 memset(m
, 0, sizeof(*m
));
1864 mutex_unlock(&mce_chrdev_read_mutex
);
1867 return err
? err
: buf
- ubuf
;
1870 static unsigned int mce_chrdev_poll(struct file
*file
, poll_table
*wait
)
1872 poll_wait(file
, &mce_chrdev_wait
, wait
);
1873 if (rcu_access_index(mcelog
.next
))
1874 return POLLIN
| POLLRDNORM
;
1875 if (!mce_apei_read_done
&& apei_check_mce())
1876 return POLLIN
| POLLRDNORM
;
1880 static long mce_chrdev_ioctl(struct file
*f
, unsigned int cmd
,
1883 int __user
*p
= (int __user
*)arg
;
1885 if (!capable(CAP_SYS_ADMIN
))
1889 case MCE_GET_RECORD_LEN
:
1890 return put_user(sizeof(struct mce
), p
);
1891 case MCE_GET_LOG_LEN
:
1892 return put_user(MCE_LOG_LEN
, p
);
1893 case MCE_GETCLEAR_FLAGS
: {
1897 flags
= mcelog
.flags
;
1898 } while (cmpxchg(&mcelog
.flags
, flags
, 0) != flags
);
1900 return put_user(flags
, p
);
1907 static ssize_t (*mce_write
)(struct file
*filp
, const char __user
*ubuf
,
1908 size_t usize
, loff_t
*off
);
1910 void register_mce_write_callback(ssize_t (*fn
)(struct file
*filp
,
1911 const char __user
*ubuf
,
1912 size_t usize
, loff_t
*off
))
1916 EXPORT_SYMBOL_GPL(register_mce_write_callback
);
1918 ssize_t
mce_chrdev_write(struct file
*filp
, const char __user
*ubuf
,
1919 size_t usize
, loff_t
*off
)
1922 return mce_write(filp
, ubuf
, usize
, off
);
1927 static const struct file_operations mce_chrdev_ops
= {
1928 .open
= mce_chrdev_open
,
1929 .release
= mce_chrdev_release
,
1930 .read
= mce_chrdev_read
,
1931 .write
= mce_chrdev_write
,
1932 .poll
= mce_chrdev_poll
,
1933 .unlocked_ioctl
= mce_chrdev_ioctl
,
1934 .llseek
= no_llseek
,
1937 static struct miscdevice mce_chrdev_device
= {
1944 * mce=off Disables machine check
1945 * mce=no_cmci Disables CMCI
1946 * mce=dont_log_ce Clears corrected events silently, no log created for CEs.
1947 * mce=ignore_ce Disables polling and CMCI, corrected events are not cleared.
1948 * mce=TOLERANCELEVEL[,monarchtimeout] (number, see above)
1949 * monarchtimeout is how long to wait for other CPUs on machine
1950 * check, or 0 to not wait
1951 * mce=bootlog Log MCEs from before booting. Disabled by default on AMD.
1952 * mce=nobootlog Don't log MCEs from before booting.
1953 * mce=bios_cmci_threshold Don't program the CMCI threshold
1955 static int __init
mcheck_enable(char *str
)
1957 struct mca_config
*cfg
= &mca_cfg
;
1965 if (!strcmp(str
, "off"))
1966 cfg
->disabled
= true;
1967 else if (!strcmp(str
, "no_cmci"))
1968 cfg
->cmci_disabled
= true;
1969 else if (!strcmp(str
, "dont_log_ce"))
1970 cfg
->dont_log_ce
= true;
1971 else if (!strcmp(str
, "ignore_ce"))
1972 cfg
->ignore_ce
= true;
1973 else if (!strcmp(str
, "bootlog") || !strcmp(str
, "nobootlog"))
1974 cfg
->bootlog
= (str
[0] == 'b');
1975 else if (!strcmp(str
, "bios_cmci_threshold"))
1976 cfg
->bios_cmci_threshold
= true;
1977 else if (isdigit(str
[0])) {
1978 get_option(&str
, &(cfg
->tolerant
));
1981 get_option(&str
, &(cfg
->monarch_timeout
));
1984 pr_info("mce argument %s ignored. Please use /sys\n", str
);
1989 __setup("mce", mcheck_enable
);
1991 int __init
mcheck_init(void)
1993 mcheck_intel_therm_init();
1999 * mce_syscore: PM support
2003 * Disable machine checks on suspend and shutdown. We can't really handle
2006 static int mce_disable_error_reporting(void)
2010 for (i
= 0; i
< mca_cfg
.banks
; i
++) {
2011 struct mce_bank
*b
= &mce_banks
[i
];
2014 wrmsrl(MSR_IA32_MCx_CTL(i
), 0);
2019 static int mce_syscore_suspend(void)
2021 return mce_disable_error_reporting();
2024 static void mce_syscore_shutdown(void)
2026 mce_disable_error_reporting();
2030 * On resume clear all MCE state. Don't want to see leftovers from the BIOS.
2031 * Only one CPU is active at this time, the others get re-added later using
2034 static void mce_syscore_resume(void)
2036 __mcheck_cpu_init_generic();
2037 __mcheck_cpu_init_vendor(__this_cpu_ptr(&cpu_info
));
2040 static struct syscore_ops mce_syscore_ops
= {
2041 .suspend
= mce_syscore_suspend
,
2042 .shutdown
= mce_syscore_shutdown
,
2043 .resume
= mce_syscore_resume
,
2047 * mce_device: Sysfs support
2050 static void mce_cpu_restart(void *data
)
2052 if (!mce_available(__this_cpu_ptr(&cpu_info
)))
2054 __mcheck_cpu_init_generic();
2055 __mcheck_cpu_init_timer();
2058 /* Reinit MCEs after user configuration changes */
2059 static void mce_restart(void)
2061 mce_timer_delete_all();
2062 on_each_cpu(mce_cpu_restart
, NULL
, 1);
2065 /* Toggle features for corrected errors */
2066 static void mce_disable_cmci(void *data
)
2068 if (!mce_available(__this_cpu_ptr(&cpu_info
)))
2073 static void mce_enable_ce(void *all
)
2075 if (!mce_available(__this_cpu_ptr(&cpu_info
)))
2080 __mcheck_cpu_init_timer();
2083 static struct bus_type mce_subsys
= {
2084 .name
= "machinecheck",
2085 .dev_name
= "machinecheck",
2088 DEFINE_PER_CPU(struct device
*, mce_device
);
2091 void (*threshold_cpu_callback
)(unsigned long action
, unsigned int cpu
);
2093 static inline struct mce_bank
*attr_to_bank(struct device_attribute
*attr
)
2095 return container_of(attr
, struct mce_bank
, attr
);
2098 static ssize_t
show_bank(struct device
*s
, struct device_attribute
*attr
,
2101 return sprintf(buf
, "%llx\n", attr_to_bank(attr
)->ctl
);
2104 static ssize_t
set_bank(struct device
*s
, struct device_attribute
*attr
,
2105 const char *buf
, size_t size
)
2109 if (strict_strtoull(buf
, 0, &new) < 0)
2112 attr_to_bank(attr
)->ctl
= new;
2119 show_trigger(struct device
*s
, struct device_attribute
*attr
, char *buf
)
2121 strcpy(buf
, mce_helper
);
2123 return strlen(mce_helper
) + 1;
2126 static ssize_t
set_trigger(struct device
*s
, struct device_attribute
*attr
,
2127 const char *buf
, size_t siz
)
2131 strncpy(mce_helper
, buf
, sizeof(mce_helper
));
2132 mce_helper
[sizeof(mce_helper
)-1] = 0;
2133 p
= strchr(mce_helper
, '\n');
2138 return strlen(mce_helper
) + !!p
;
2141 static ssize_t
set_ignore_ce(struct device
*s
,
2142 struct device_attribute
*attr
,
2143 const char *buf
, size_t size
)
2147 if (strict_strtoull(buf
, 0, &new) < 0)
2150 if (mca_cfg
.ignore_ce
^ !!new) {
2152 /* disable ce features */
2153 mce_timer_delete_all();
2154 on_each_cpu(mce_disable_cmci
, NULL
, 1);
2155 mca_cfg
.ignore_ce
= true;
2157 /* enable ce features */
2158 mca_cfg
.ignore_ce
= false;
2159 on_each_cpu(mce_enable_ce
, (void *)1, 1);
2165 static ssize_t
set_cmci_disabled(struct device
*s
,
2166 struct device_attribute
*attr
,
2167 const char *buf
, size_t size
)
2171 if (strict_strtoull(buf
, 0, &new) < 0)
2174 if (mca_cfg
.cmci_disabled
^ !!new) {
2177 on_each_cpu(mce_disable_cmci
, NULL
, 1);
2178 mca_cfg
.cmci_disabled
= true;
2181 mca_cfg
.cmci_disabled
= false;
2182 on_each_cpu(mce_enable_ce
, NULL
, 1);
2188 static ssize_t
store_int_with_restart(struct device
*s
,
2189 struct device_attribute
*attr
,
2190 const char *buf
, size_t size
)
2192 ssize_t ret
= device_store_int(s
, attr
, buf
, size
);
2197 static DEVICE_ATTR(trigger
, 0644, show_trigger
, set_trigger
);
2198 static DEVICE_INT_ATTR(tolerant
, 0644, mca_cfg
.tolerant
);
2199 static DEVICE_INT_ATTR(monarch_timeout
, 0644, mca_cfg
.monarch_timeout
);
2200 static DEVICE_BOOL_ATTR(dont_log_ce
, 0644, mca_cfg
.dont_log_ce
);
2202 static struct dev_ext_attribute dev_attr_check_interval
= {
2203 __ATTR(check_interval
, 0644, device_show_int
, store_int_with_restart
),
2207 static struct dev_ext_attribute dev_attr_ignore_ce
= {
2208 __ATTR(ignore_ce
, 0644, device_show_bool
, set_ignore_ce
),
2212 static struct dev_ext_attribute dev_attr_cmci_disabled
= {
2213 __ATTR(cmci_disabled
, 0644, device_show_bool
, set_cmci_disabled
),
2214 &mca_cfg
.cmci_disabled
2217 static struct device_attribute
*mce_device_attrs
[] = {
2218 &dev_attr_tolerant
.attr
,
2219 &dev_attr_check_interval
.attr
,
2221 &dev_attr_monarch_timeout
.attr
,
2222 &dev_attr_dont_log_ce
.attr
,
2223 &dev_attr_ignore_ce
.attr
,
2224 &dev_attr_cmci_disabled
.attr
,
2228 static cpumask_var_t mce_device_initialized
;
2230 static void mce_device_release(struct device
*dev
)
2235 /* Per cpu device init. All of the cpus still share the same ctrl bank: */
2236 static __cpuinit
int mce_device_create(unsigned int cpu
)
2242 if (!mce_available(&boot_cpu_data
))
2245 dev
= kzalloc(sizeof *dev
, GFP_KERNEL
);
2249 dev
->bus
= &mce_subsys
;
2250 dev
->release
= &mce_device_release
;
2252 err
= device_register(dev
);
2256 for (i
= 0; mce_device_attrs
[i
]; i
++) {
2257 err
= device_create_file(dev
, mce_device_attrs
[i
]);
2261 for (j
= 0; j
< mca_cfg
.banks
; j
++) {
2262 err
= device_create_file(dev
, &mce_banks
[j
].attr
);
2266 cpumask_set_cpu(cpu
, mce_device_initialized
);
2267 per_cpu(mce_device
, cpu
) = dev
;
2272 device_remove_file(dev
, &mce_banks
[j
].attr
);
2275 device_remove_file(dev
, mce_device_attrs
[i
]);
2277 device_unregister(dev
);
2282 static __cpuinit
void mce_device_remove(unsigned int cpu
)
2284 struct device
*dev
= per_cpu(mce_device
, cpu
);
2287 if (!cpumask_test_cpu(cpu
, mce_device_initialized
))
2290 for (i
= 0; mce_device_attrs
[i
]; i
++)
2291 device_remove_file(dev
, mce_device_attrs
[i
]);
2293 for (i
= 0; i
< mca_cfg
.banks
; i
++)
2294 device_remove_file(dev
, &mce_banks
[i
].attr
);
2296 device_unregister(dev
);
2297 cpumask_clear_cpu(cpu
, mce_device_initialized
);
2298 per_cpu(mce_device
, cpu
) = NULL
;
2301 /* Make sure there are no machine checks on offlined CPUs. */
2302 static void __cpuinit
mce_disable_cpu(void *h
)
2304 unsigned long action
= *(unsigned long *)h
;
2307 if (!mce_available(__this_cpu_ptr(&cpu_info
)))
2310 if (!(action
& CPU_TASKS_FROZEN
))
2312 for (i
= 0; i
< mca_cfg
.banks
; i
++) {
2313 struct mce_bank
*b
= &mce_banks
[i
];
2316 wrmsrl(MSR_IA32_MCx_CTL(i
), 0);
2320 static void __cpuinit
mce_reenable_cpu(void *h
)
2322 unsigned long action
= *(unsigned long *)h
;
2325 if (!mce_available(__this_cpu_ptr(&cpu_info
)))
2328 if (!(action
& CPU_TASKS_FROZEN
))
2330 for (i
= 0; i
< mca_cfg
.banks
; i
++) {
2331 struct mce_bank
*b
= &mce_banks
[i
];
2334 wrmsrl(MSR_IA32_MCx_CTL(i
), b
->ctl
);
2338 /* Get notified when a cpu comes on/off. Be hotplug friendly. */
2339 static int __cpuinit
2340 mce_cpu_callback(struct notifier_block
*nfb
, unsigned long action
, void *hcpu
)
2342 unsigned int cpu
= (unsigned long)hcpu
;
2343 struct timer_list
*t
= &per_cpu(mce_timer
, cpu
);
2345 switch (action
& ~CPU_TASKS_FROZEN
) {
2347 mce_device_create(cpu
);
2348 if (threshold_cpu_callback
)
2349 threshold_cpu_callback(action
, cpu
);
2352 if (threshold_cpu_callback
)
2353 threshold_cpu_callback(action
, cpu
);
2354 mce_device_remove(cpu
);
2355 mce_intel_hcpu_update(cpu
);
2357 case CPU_DOWN_PREPARE
:
2358 smp_call_function_single(cpu
, mce_disable_cpu
, &action
, 1);
2361 case CPU_DOWN_FAILED
:
2362 smp_call_function_single(cpu
, mce_reenable_cpu
, &action
, 1);
2363 mce_start_timer(cpu
, t
);
2367 if (action
== CPU_POST_DEAD
) {
2368 /* intentionally ignoring frozen here */
2369 cmci_rediscover(cpu
);
2375 static struct notifier_block mce_cpu_notifier __cpuinitdata
= {
2376 .notifier_call
= mce_cpu_callback
,
2379 static __init
void mce_init_banks(void)
2383 for (i
= 0; i
< mca_cfg
.banks
; i
++) {
2384 struct mce_bank
*b
= &mce_banks
[i
];
2385 struct device_attribute
*a
= &b
->attr
;
2387 sysfs_attr_init(&a
->attr
);
2388 a
->attr
.name
= b
->attrname
;
2389 snprintf(b
->attrname
, ATTR_LEN
, "bank%d", i
);
2391 a
->attr
.mode
= 0644;
2392 a
->show
= show_bank
;
2393 a
->store
= set_bank
;
2397 static __init
int mcheck_init_device(void)
2402 if (!mce_available(&boot_cpu_data
))
2405 zalloc_cpumask_var(&mce_device_initialized
, GFP_KERNEL
);
2409 err
= subsys_system_register(&mce_subsys
, NULL
);
2413 for_each_online_cpu(i
) {
2414 err
= mce_device_create(i
);
2419 register_syscore_ops(&mce_syscore_ops
);
2420 register_hotcpu_notifier(&mce_cpu_notifier
);
2422 /* register character device /dev/mcelog */
2423 misc_register(&mce_chrdev_device
);
2427 device_initcall_sync(mcheck_init_device
);
2430 * Old style boot options parsing. Only for compatibility.
2432 static int __init
mcheck_disable(char *str
)
2434 mca_cfg
.disabled
= true;
2437 __setup("nomce", mcheck_disable
);
2439 #ifdef CONFIG_DEBUG_FS
2440 struct dentry
*mce_get_debugfs_dir(void)
2442 static struct dentry
*dmce
;
2445 dmce
= debugfs_create_dir("mce", NULL
);
2450 static void mce_reset(void)
2453 atomic_set(&mce_fake_paniced
, 0);
2454 atomic_set(&mce_executing
, 0);
2455 atomic_set(&mce_callin
, 0);
2456 atomic_set(&global_nwo
, 0);
2459 static int fake_panic_get(void *data
, u64
*val
)
2465 static int fake_panic_set(void *data
, u64 val
)
2472 DEFINE_SIMPLE_ATTRIBUTE(fake_panic_fops
, fake_panic_get
,
2473 fake_panic_set
, "%llu\n");
2475 static int __init
mcheck_debugfs_init(void)
2477 struct dentry
*dmce
, *ffake_panic
;
2479 dmce
= mce_get_debugfs_dir();
2482 ffake_panic
= debugfs_create_file("fake_panic", 0444, dmce
, NULL
,
2489 late_initcall(mcheck_debugfs_init
);