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/miscdevice.h>
14 #include <linux/slab.h>
15 #include <linux/kmod.h>
16 #include <linux/poll.h>
18 #include "mce-internal.h"
20 static BLOCKING_NOTIFIER_HEAD(mce_injector_chain);
22 static DEFINE_MUTEX(mce_chrdev_read_mutex);
24 static char mce_helper[128];
25 static char *mce_helper_argv[2] = { mce_helper, NULL };
27 #define mce_log_get_idx_check(p) \
29 RCU_LOCKDEP_WARN(!rcu_read_lock_sched_held() && \
30 !lockdep_is_held(&mce_chrdev_read_mutex), \
31 "suspicious mce_log_get_idx_check() usage"); \
32 smp_load_acquire(&(p)); \
36 * Lockless MCE logging infrastructure.
37 * This avoids deadlocks on printk locks without having to break locks. Also
38 * separate MCEs from kernel messages to avoid bogus bug reports.
41 static struct mce_log_buffer mcelog = {
42 .signature = MCE_LOG_SIGNATURE,
44 .recordlen = sizeof(struct mce),
47 static DECLARE_WAIT_QUEUE_HEAD(mce_chrdev_wait);
49 /* User mode helper program triggered by machine check event */
50 extern char mce_helper[128];
52 static int dev_mce_log(struct notifier_block *nb, unsigned long val,
55 struct mce *mce = (struct mce *)data;
56 unsigned int next, entry;
60 entry = mce_log_get_idx_check(mcelog.next);
64 * When the buffer fills up discard new entries.
65 * Assume that the earlier errors are the more
68 if (entry >= MCE_LOG_LEN) {
70 (unsigned long *)&mcelog.flags);
73 /* Old left over entry. Skip: */
74 if (mcelog.entry[entry].finished) {
82 if (cmpxchg(&mcelog.next, entry, next) == entry)
85 memcpy(mcelog.entry + entry, mce, sizeof(struct mce));
87 mcelog.entry[entry].finished = 1;
90 /* wake processes polling /dev/mcelog */
91 wake_up_interruptible(&mce_chrdev_wait);
96 static struct notifier_block dev_mcelog_nb = {
97 .notifier_call = dev_mce_log,
98 .priority = MCE_PRIO_MCELOG,
101 static void mce_do_trigger(struct work_struct *work)
103 call_usermodehelper(mce_helper, mce_helper_argv, NULL, UMH_NO_WAIT);
106 static DECLARE_WORK(mce_trigger_work, mce_do_trigger);
109 void mce_work_trigger(void)
112 schedule_work(&mce_trigger_work);
116 show_trigger(struct device *s, struct device_attribute *attr, char *buf)
118 strcpy(buf, mce_helper);
120 return strlen(mce_helper) + 1;
123 static ssize_t set_trigger(struct device *s, struct device_attribute *attr,
124 const char *buf, size_t siz)
128 strncpy(mce_helper, buf, sizeof(mce_helper));
129 mce_helper[sizeof(mce_helper)-1] = 0;
130 p = strchr(mce_helper, '\n');
135 return strlen(mce_helper) + !!p;
138 DEVICE_ATTR(trigger, 0644, show_trigger, set_trigger);
141 * mce_chrdev: Character device /dev/mcelog to read and clear the MCE log.
144 static DEFINE_SPINLOCK(mce_chrdev_state_lock);
145 static int mce_chrdev_open_count; /* #times opened */
146 static int mce_chrdev_open_exclu; /* already open exclusive? */
148 static int mce_chrdev_open(struct inode *inode, struct file *file)
150 spin_lock(&mce_chrdev_state_lock);
152 if (mce_chrdev_open_exclu ||
153 (mce_chrdev_open_count && (file->f_flags & O_EXCL))) {
154 spin_unlock(&mce_chrdev_state_lock);
159 if (file->f_flags & O_EXCL)
160 mce_chrdev_open_exclu = 1;
161 mce_chrdev_open_count++;
163 spin_unlock(&mce_chrdev_state_lock);
165 return nonseekable_open(inode, file);
168 static int mce_chrdev_release(struct inode *inode, struct file *file)
170 spin_lock(&mce_chrdev_state_lock);
172 mce_chrdev_open_count--;
173 mce_chrdev_open_exclu = 0;
175 spin_unlock(&mce_chrdev_state_lock);
180 static void collect_tscs(void *data)
182 unsigned long *cpu_tsc = (unsigned long *)data;
184 cpu_tsc[smp_processor_id()] = rdtsc();
187 static int mce_apei_read_done;
189 /* Collect MCE record of previous boot in persistent storage via APEI ERST. */
190 static int __mce_read_apei(char __user **ubuf, size_t usize)
196 if (usize < sizeof(struct mce))
199 rc = apei_read_mce(&m, &record_id);
200 /* Error or no more MCE record */
202 mce_apei_read_done = 1;
204 * When ERST is disabled, mce_chrdev_read() should return
205 * "no record" instead of "no device."
212 if (copy_to_user(*ubuf, &m, sizeof(struct mce)))
215 * In fact, we should have cleared the record after that has
216 * been flushed to the disk or sent to network in
217 * /sbin/mcelog, but we have no interface to support that now,
218 * so just clear it to avoid duplication.
220 rc = apei_clear_mce(record_id);
222 mce_apei_read_done = 1;
225 *ubuf += sizeof(struct mce);
230 static ssize_t mce_chrdev_read(struct file *filp, char __user *ubuf,
231 size_t usize, loff_t *off)
233 char __user *buf = ubuf;
234 unsigned long *cpu_tsc;
238 cpu_tsc = kmalloc(nr_cpu_ids * sizeof(long), GFP_KERNEL);
242 mutex_lock(&mce_chrdev_read_mutex);
244 if (!mce_apei_read_done) {
245 err = __mce_read_apei(&buf, usize);
246 if (err || buf != ubuf)
250 next = mce_log_get_idx_check(mcelog.next);
252 /* Only supports full reads right now */
254 if (*off != 0 || usize < MCE_LOG_LEN*sizeof(struct mce))
260 for (i = prev; i < next; i++) {
261 unsigned long start = jiffies;
262 struct mce *m = &mcelog.entry[i];
264 while (!m->finished) {
265 if (time_after_eq(jiffies, start + 2)) {
266 memset(m, 0, sizeof(*m));
272 err |= copy_to_user(buf, m, sizeof(*m));
278 memset(mcelog.entry + prev, 0,
279 (next - prev) * sizeof(struct mce));
281 next = cmpxchg(&mcelog.next, prev, 0);
282 } while (next != prev);
287 * Collect entries that were still getting written before the
290 on_each_cpu(collect_tscs, cpu_tsc, 1);
292 for (i = next; i < MCE_LOG_LEN; i++) {
293 struct mce *m = &mcelog.entry[i];
295 if (m->finished && m->tsc < cpu_tsc[m->cpu]) {
296 err |= copy_to_user(buf, m, sizeof(*m));
299 memset(m, 0, sizeof(*m));
307 mutex_unlock(&mce_chrdev_read_mutex);
310 return err ? err : buf - ubuf;
313 static unsigned int mce_chrdev_poll(struct file *file, poll_table *wait)
315 poll_wait(file, &mce_chrdev_wait, wait);
316 if (READ_ONCE(mcelog.next))
317 return POLLIN | POLLRDNORM;
318 if (!mce_apei_read_done && apei_check_mce())
319 return POLLIN | POLLRDNORM;
323 static long mce_chrdev_ioctl(struct file *f, unsigned int cmd,
326 int __user *p = (int __user *)arg;
328 if (!capable(CAP_SYS_ADMIN))
332 case MCE_GET_RECORD_LEN:
333 return put_user(sizeof(struct mce), p);
334 case MCE_GET_LOG_LEN:
335 return put_user(MCE_LOG_LEN, p);
336 case MCE_GETCLEAR_FLAGS: {
340 flags = mcelog.flags;
341 } while (cmpxchg(&mcelog.flags, flags, 0) != flags);
343 return put_user(flags, p);
350 void mce_register_injector_chain(struct notifier_block *nb)
352 blocking_notifier_chain_register(&mce_injector_chain, nb);
354 EXPORT_SYMBOL_GPL(mce_register_injector_chain);
356 void mce_unregister_injector_chain(struct notifier_block *nb)
358 blocking_notifier_chain_unregister(&mce_injector_chain, nb);
360 EXPORT_SYMBOL_GPL(mce_unregister_injector_chain);
362 static ssize_t mce_chrdev_write(struct file *filp, const char __user *ubuf,
363 size_t usize, loff_t *off)
367 if (!capable(CAP_SYS_ADMIN))
370 * There are some cases where real MSR reads could slip
373 if (!boot_cpu_has(X86_FEATURE_MCE) || !boot_cpu_has(X86_FEATURE_MCA))
376 if ((unsigned long)usize > sizeof(struct mce))
377 usize = sizeof(struct mce);
378 if (copy_from_user(&m, ubuf, usize))
381 if (m.extcpu >= num_possible_cpus() || !cpu_online(m.extcpu))
385 * Need to give user space some time to set everything up,
386 * so do it a jiffie or two later everywhere.
390 blocking_notifier_call_chain(&mce_injector_chain, 0, &m);
395 static const struct file_operations mce_chrdev_ops = {
396 .open = mce_chrdev_open,
397 .release = mce_chrdev_release,
398 .read = mce_chrdev_read,
399 .write = mce_chrdev_write,
400 .poll = mce_chrdev_poll,
401 .unlocked_ioctl = mce_chrdev_ioctl,
405 static struct miscdevice mce_chrdev_device = {
411 static __init int dev_mcelog_init_device(void)
415 /* register character device /dev/mcelog */
416 err = misc_register(&mce_chrdev_device);
419 /* Xen dom0 might have registered the device already. */
420 pr_info("Unable to init device /dev/mcelog, already registered");
422 pr_err("Unable to init device /dev/mcelog (rc: %d)\n", err);
427 mce_register_decode_chain(&dev_mcelog_nb);
430 device_initcall_sync(dev_mcelog_init_device);