提交 e02e68d3 编写于 作者: T Tim Hockin 提交者: Linus Torvalds

x86_64: support poll() on /dev/mcelog

Background:
 /dev/mcelog is typically polled manually.  This is less than optimal for
 situations where accurate accounting of MCEs is important.  Calling
 poll() on /dev/mcelog does not work.

Description:
 This patch adds support for poll() to /dev/mcelog.  This results in
 immediate wakeup of user apps whenever the poller finds MCEs.  Because
 the exception handler can not take any locks, it can not call the wakeup
 itself.  Instead, it uses a thread_info flag (TIF_MCE_NOTIFY) which is
 caught at the next return from interrupt or exit from idle, calling the
 mce_user_notify() routine.  This patch also disables the "fake panic"
 path of the mce_panic(), because it results in printk()s in the exception
 handler and crashy systems.

 This patch also does some small cleanup for essentially unused variables,
 and moves the user notification into the body of the poller, so it is
 only called once per poll, rather than once per CPU.

Result:
 Applications can now poll() on /dev/mcelog.  When an error is logged
 (whether through the poller or through an exception) the applications are
 woken up promptly.  This should not affect any previous behaviors.  If no
 MCEs are being logged, there is no overhead.

Alternatives:
 I considered simply supporting poll() through the poller and not using
 TIF_MCE_NOTIFY at all.  However, the time between an uncorrectable error
 happening and the user application being notified is *the*most* critical
 window for us.  Many uncorrectable errors can be logged to the network if
 given a chance.

 I also considered doing the MCE poll directly from the idle notifier, but
 decided that was overkill.

Testing:
 I used an error-injecting DIMM to create lots of correctable DRAM errors
 and verified that my user app is woken up in sync with the polling interval.
 I also used the northbridge to inject uncorrectable ECC errors, and
 verified (printk() to the rescue) that the notify routine is called and the
 user app does wake up.  I built with PREEMPT on and off, and verified
 that my machine survives MCEs.

[wli@holomorphy.com: build fix]
Signed-off-by: NTim Hockin <thockin@google.com>
Signed-off-by: NWilliam Irwin <bill.irwin@oracle.com>
Signed-off-by: NAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: NAndi Kleen <ak@suse.de>
Signed-off-by: NLinus Torvalds <torvalds@linux-foundation.org>
上级 f528e7ba
...@@ -282,7 +282,7 @@ sysret_careful: ...@@ -282,7 +282,7 @@ sysret_careful:
sysret_signal: sysret_signal:
TRACE_IRQS_ON TRACE_IRQS_ON
sti sti
testl $(_TIF_SIGPENDING|_TIF_NOTIFY_RESUME|_TIF_SINGLESTEP),%edx testl $(_TIF_SIGPENDING|_TIF_NOTIFY_RESUME|_TIF_SINGLESTEP|_TIF_MCE_NOTIFY),%edx
jz 1f jz 1f
/* Really a signal */ /* Really a signal */
...@@ -375,7 +375,7 @@ int_very_careful: ...@@ -375,7 +375,7 @@ int_very_careful:
jmp int_restore_rest jmp int_restore_rest
int_signal: int_signal:
testl $(_TIF_NOTIFY_RESUME|_TIF_SIGPENDING|_TIF_SINGLESTEP),%edx testl $(_TIF_NOTIFY_RESUME|_TIF_SIGPENDING|_TIF_SINGLESTEP|_TIF_MCE_NOTIFY),%edx
jz 1f jz 1f
movq %rsp,%rdi # &ptregs -> arg1 movq %rsp,%rdi # &ptregs -> arg1
xorl %esi,%esi # oldset -> arg2 xorl %esi,%esi # oldset -> arg2
...@@ -599,7 +599,7 @@ retint_careful: ...@@ -599,7 +599,7 @@ retint_careful:
jmp retint_check jmp retint_check
retint_signal: retint_signal:
testl $(_TIF_SIGPENDING|_TIF_NOTIFY_RESUME|_TIF_SINGLESTEP),%edx testl $(_TIF_SIGPENDING|_TIF_NOTIFY_RESUME|_TIF_SINGLESTEP|_TIF_MCE_NOTIFY),%edx
jz retint_swapgs jz retint_swapgs
TRACE_IRQS_ON TRACE_IRQS_ON
sti sti
......
...@@ -18,6 +18,8 @@ ...@@ -18,6 +18,8 @@
#include <linux/capability.h> #include <linux/capability.h>
#include <linux/cpu.h> #include <linux/cpu.h>
#include <linux/percpu.h> #include <linux/percpu.h>
#include <linux/poll.h>
#include <linux/thread_info.h>
#include <linux/ctype.h> #include <linux/ctype.h>
#include <linux/kmod.h> #include <linux/kmod.h>
#include <linux/kdebug.h> #include <linux/kdebug.h>
...@@ -26,6 +28,7 @@ ...@@ -26,6 +28,7 @@
#include <asm/mce.h> #include <asm/mce.h>
#include <asm/uaccess.h> #include <asm/uaccess.h>
#include <asm/smp.h> #include <asm/smp.h>
#include <asm/idle.h>
#define MISC_MCELOG_MINOR 227 #define MISC_MCELOG_MINOR 227
#define NR_BANKS 6 #define NR_BANKS 6
...@@ -39,8 +42,7 @@ static int mce_dont_init; ...@@ -39,8 +42,7 @@ static int mce_dont_init;
static int tolerant = 1; static int tolerant = 1;
static int banks; static int banks;
static unsigned long bank[NR_BANKS] = { [0 ... NR_BANKS-1] = ~0UL }; static unsigned long bank[NR_BANKS] = { [0 ... NR_BANKS-1] = ~0UL };
static unsigned long console_logged; static unsigned long notify_user;
static int notify_user;
static int rip_msr; static int rip_msr;
static int mce_bootlog = 1; static int mce_bootlog = 1;
static atomic_t mce_events; static atomic_t mce_events;
...@@ -48,6 +50,8 @@ static atomic_t mce_events; ...@@ -48,6 +50,8 @@ static atomic_t mce_events;
static char trigger[128]; static char trigger[128];
static char *trigger_argv[2] = { trigger, NULL }; static char *trigger_argv[2] = { trigger, NULL };
static DECLARE_WAIT_QUEUE_HEAD(mce_wait);
/* /*
* Lockless MCE logging infrastructure. * Lockless MCE logging infrastructure.
* This avoids deadlocks on printk locks without having to break locks. Also * This avoids deadlocks on printk locks without having to break locks. Also
...@@ -94,8 +98,7 @@ void mce_log(struct mce *mce) ...@@ -94,8 +98,7 @@ void mce_log(struct mce *mce)
mcelog.entry[entry].finished = 1; mcelog.entry[entry].finished = 1;
wmb(); wmb();
if (!test_and_set_bit(0, &console_logged)) set_bit(0, &notify_user);
notify_user = 1;
} }
static void print_mce(struct mce *m) static void print_mce(struct mce *m)
...@@ -128,6 +131,10 @@ static void print_mce(struct mce *m) ...@@ -128,6 +131,10 @@ static void print_mce(struct mce *m)
static void mce_panic(char *msg, struct mce *backup, unsigned long start) static void mce_panic(char *msg, struct mce *backup, unsigned long start)
{ {
int i; int i;
if (tolerant >= 3)
return;
oops_begin(); oops_begin();
for (i = 0; i < MCE_LOG_LEN; i++) { for (i = 0; i < MCE_LOG_LEN; i++) {
unsigned long tsc = mcelog.entry[i].tsc; unsigned long tsc = mcelog.entry[i].tsc;
...@@ -139,10 +146,7 @@ static void mce_panic(char *msg, struct mce *backup, unsigned long start) ...@@ -139,10 +146,7 @@ static void mce_panic(char *msg, struct mce *backup, unsigned long start)
} }
if (backup) if (backup)
print_mce(backup); print_mce(backup);
if (tolerant >= 3) panic(msg);
printk("Fake panic: %s\n", msg);
else
panic(msg);
} }
static int mce_available(struct cpuinfo_x86 *c) static int mce_available(struct cpuinfo_x86 *c)
...@@ -167,17 +171,6 @@ static inline void mce_get_rip(struct mce *m, struct pt_regs *regs) ...@@ -167,17 +171,6 @@ static inline void mce_get_rip(struct mce *m, struct pt_regs *regs)
} }
} }
static void do_mce_trigger(void)
{
static atomic_t mce_logged;
int events = atomic_read(&mce_events);
if (events != atomic_read(&mce_logged) && trigger[0]) {
/* Small race window, but should be harmless. */
atomic_set(&mce_logged, events);
call_usermodehelper(trigger, trigger_argv, NULL, UMH_NO_WAIT);
}
}
/* /*
* The actual machine check handler * The actual machine check handler
*/ */
...@@ -251,12 +244,8 @@ void do_machine_check(struct pt_regs * regs, long error_code) ...@@ -251,12 +244,8 @@ void do_machine_check(struct pt_regs * regs, long error_code)
} }
/* Never do anything final in the polling timer */ /* Never do anything final in the polling timer */
if (!regs) { if (!regs)
/* Normal interrupt context here. Call trigger for any new
events. */
do_mce_trigger();
goto out; goto out;
}
/* If we didn't find an uncorrectable error, pick /* If we didn't find an uncorrectable error, pick
the last one (shouldn't happen, just being safe). */ the last one (shouldn't happen, just being safe). */
...@@ -288,6 +277,9 @@ void do_machine_check(struct pt_regs * regs, long error_code) ...@@ -288,6 +277,9 @@ void do_machine_check(struct pt_regs * regs, long error_code)
do_exit(SIGBUS); do_exit(SIGBUS);
} }
/* notify userspace ASAP */
set_thread_flag(TIF_MCE_NOTIFY);
out: out:
/* Last thing done in the machine check exception to clear state. */ /* Last thing done in the machine check exception to clear state. */
wrmsrl(MSR_IA32_MCG_STATUS, 0); wrmsrl(MSR_IA32_MCG_STATUS, 0);
...@@ -344,37 +336,67 @@ static void mcheck_timer(struct work_struct *work) ...@@ -344,37 +336,67 @@ static void mcheck_timer(struct work_struct *work)
on_each_cpu(mcheck_check_cpu, NULL, 1, 1); on_each_cpu(mcheck_check_cpu, NULL, 1, 1);
/* /*
* It's ok to read stale data here for notify_user and * Alert userspace if needed. If we logged an MCE, reduce the
* console_logged as we'll simply get the updated versions * polling interval, otherwise increase the polling interval.
* on the next mcheck_timer execution and atomic operations
* on console_logged act as synchronization for notify_user
* writes.
*/ */
if (notify_user && console_logged) { if (mce_notify_user()) {
next_interval = max(next_interval/2, HZ/100);
} else {
next_interval = min(next_interval*2, check_interval*HZ);
}
schedule_delayed_work(&mcheck_work, next_interval);
}
/*
* This is only called from process context. This is where we do
* anything we need to alert userspace about new MCEs. This is called
* directly from the poller and also from entry.S and idle, thanks to
* TIF_MCE_NOTIFY.
*/
int mce_notify_user(void)
{
clear_thread_flag(TIF_MCE_NOTIFY);
if (test_and_clear_bit(0, &notify_user)) {
static unsigned long last_print; static unsigned long last_print;
unsigned long now = jiffies; unsigned long now = jiffies;
/* if we logged an MCE, reduce the polling interval */ wake_up_interruptible(&mce_wait);
next_interval = max(next_interval/2, HZ/100); if (trigger[0])
notify_user = 0; call_usermodehelper(trigger, trigger_argv, NULL,
clear_bit(0, &console_logged); UMH_NO_WAIT);
if (time_after_eq(now, last_print + (check_interval*HZ))) { if (time_after_eq(now, last_print + (check_interval*HZ))) {
last_print = now; last_print = now;
printk(KERN_INFO "Machine check events logged\n"); printk(KERN_INFO "Machine check events logged\n");
} }
} else {
next_interval = min(next_interval*2, check_interval*HZ); return 1;
} }
return 0;
}
schedule_delayed_work(&mcheck_work, next_interval); /* see if the idle task needs to notify userspace */
static int
mce_idle_callback(struct notifier_block *nfb, unsigned long action, void *junk)
{
/* IDLE_END should be safe - interrupts are back on */
if (action == IDLE_END && test_thread_flag(TIF_MCE_NOTIFY))
mce_notify_user();
return NOTIFY_OK;
} }
static struct notifier_block mce_idle_notifier = {
.notifier_call = mce_idle_callback,
};
static __init int periodic_mcheck_init(void) static __init int periodic_mcheck_init(void)
{ {
next_interval = check_interval * HZ; next_interval = check_interval * HZ;
if (next_interval) if (next_interval)
schedule_delayed_work(&mcheck_work, next_interval); schedule_delayed_work(&mcheck_work, next_interval);
idle_notifier_register(&mce_idle_notifier);
return 0; return 0;
} }
__initcall(periodic_mcheck_init); __initcall(periodic_mcheck_init);
...@@ -566,6 +588,14 @@ static ssize_t mce_read(struct file *filp, char __user *ubuf, size_t usize, loff ...@@ -566,6 +588,14 @@ static ssize_t mce_read(struct file *filp, char __user *ubuf, size_t usize, loff
return err ? -EFAULT : buf - ubuf; return err ? -EFAULT : buf - ubuf;
} }
static unsigned int mce_poll(struct file *file, poll_table *wait)
{
poll_wait(file, &mce_wait, wait);
if (rcu_dereference(mcelog.next))
return POLLIN | POLLRDNORM;
return 0;
}
static int mce_ioctl(struct inode *i, struct file *f,unsigned int cmd, unsigned long arg) static int mce_ioctl(struct inode *i, struct file *f,unsigned int cmd, unsigned long arg)
{ {
int __user *p = (int __user *)arg; int __user *p = (int __user *)arg;
...@@ -592,6 +622,7 @@ static const struct file_operations mce_chrdev_ops = { ...@@ -592,6 +622,7 @@ static const struct file_operations mce_chrdev_ops = {
.open = mce_open, .open = mce_open,
.release = mce_release, .release = mce_release,
.read = mce_read, .read = mce_read,
.poll = mce_poll,
.ioctl = mce_ioctl, .ioctl = mce_ioctl,
}; };
......
...@@ -26,6 +26,7 @@ ...@@ -26,6 +26,7 @@
#include <asm/i387.h> #include <asm/i387.h>
#include <asm/proto.h> #include <asm/proto.h>
#include <asm/ia32_unistd.h> #include <asm/ia32_unistd.h>
#include <asm/mce.h>
/* #define DEBUG_SIG 1 */ /* #define DEBUG_SIG 1 */
...@@ -472,6 +473,12 @@ do_notify_resume(struct pt_regs *regs, void *unused, __u32 thread_info_flags) ...@@ -472,6 +473,12 @@ do_notify_resume(struct pt_regs *regs, void *unused, __u32 thread_info_flags)
clear_thread_flag(TIF_SINGLESTEP); clear_thread_flag(TIF_SINGLESTEP);
} }
#ifdef CONFIG_X86_MCE
/* notify userspace of pending MCEs */
if (thread_info_flags & _TIF_MCE_NOTIFY)
mce_notify_user();
#endif /* CONFIG_X86_MCE */
/* deal with pending signal delivery */ /* deal with pending signal delivery */
if (thread_info_flags & (_TIF_SIGPENDING|_TIF_RESTORE_SIGMASK)) if (thread_info_flags & (_TIF_SIGPENDING|_TIF_RESTORE_SIGMASK))
do_signal(regs); do_signal(regs);
......
...@@ -105,6 +105,8 @@ extern atomic_t mce_entry; ...@@ -105,6 +105,8 @@ extern atomic_t mce_entry;
extern void do_machine_check(struct pt_regs *, long); extern void do_machine_check(struct pt_regs *, long);
extern int mce_notify_user(void);
#endif #endif
#endif #endif
...@@ -115,6 +115,7 @@ static inline struct thread_info *stack_thread_info(void) ...@@ -115,6 +115,7 @@ static inline struct thread_info *stack_thread_info(void)
#define TIF_SYSCALL_AUDIT 7 /* syscall auditing active */ #define TIF_SYSCALL_AUDIT 7 /* syscall auditing active */
#define TIF_SECCOMP 8 /* secure computing */ #define TIF_SECCOMP 8 /* secure computing */
#define TIF_RESTORE_SIGMASK 9 /* restore signal mask in do_signal */ #define TIF_RESTORE_SIGMASK 9 /* restore signal mask in do_signal */
#define TIF_MCE_NOTIFY 10 /* notify userspace of an MCE */
/* 16 free */ /* 16 free */
#define TIF_IA32 17 /* 32bit process */ #define TIF_IA32 17 /* 32bit process */
#define TIF_FORK 18 /* ret_from_fork */ #define TIF_FORK 18 /* ret_from_fork */
...@@ -133,6 +134,7 @@ static inline struct thread_info *stack_thread_info(void) ...@@ -133,6 +134,7 @@ static inline struct thread_info *stack_thread_info(void)
#define _TIF_SYSCALL_AUDIT (1<<TIF_SYSCALL_AUDIT) #define _TIF_SYSCALL_AUDIT (1<<TIF_SYSCALL_AUDIT)
#define _TIF_SECCOMP (1<<TIF_SECCOMP) #define _TIF_SECCOMP (1<<TIF_SECCOMP)
#define _TIF_RESTORE_SIGMASK (1<<TIF_RESTORE_SIGMASK) #define _TIF_RESTORE_SIGMASK (1<<TIF_RESTORE_SIGMASK)
#define _TIF_MCE_NOTIFY (1<<TIF_MCE_NOTIFY)
#define _TIF_IA32 (1<<TIF_IA32) #define _TIF_IA32 (1<<TIF_IA32)
#define _TIF_FORK (1<<TIF_FORK) #define _TIF_FORK (1<<TIF_FORK)
#define _TIF_ABI_PENDING (1<<TIF_ABI_PENDING) #define _TIF_ABI_PENDING (1<<TIF_ABI_PENDING)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册