Merge tag 'sound-4.13-rc3' of git://git.kernel.org/pub/scm/linux/kernel/git/tiwai...
[sfrench/cifs-2.6.git] / arch / x86 / kernel / cpu / mcheck / dev-mcelog.c
1 /*
2  * /dev/mcelog driver
3  *
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
8  * Author: Andi Kleen
9  */
10
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12
13 #include <linux/miscdevice.h>
14 #include <linux/slab.h>
15 #include <linux/kmod.h>
16 #include <linux/poll.h>
17
18 #include "mce-internal.h"
19
20 static BLOCKING_NOTIFIER_HEAD(mce_injector_chain);
21
22 static DEFINE_MUTEX(mce_chrdev_read_mutex);
23
24 static char mce_helper[128];
25 static char *mce_helper_argv[2] = { mce_helper, NULL };
26
27 #define mce_log_get_idx_check(p) \
28 ({ \
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)); \
33 })
34
35 /*
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.
39  */
40
41 static struct mce_log_buffer mcelog = {
42         .signature      = MCE_LOG_SIGNATURE,
43         .len            = MCE_LOG_LEN,
44         .recordlen      = sizeof(struct mce),
45 };
46
47 static DECLARE_WAIT_QUEUE_HEAD(mce_chrdev_wait);
48
49 /* User mode helper program triggered by machine check event */
50 extern char                     mce_helper[128];
51
52 static int dev_mce_log(struct notifier_block *nb, unsigned long val,
53                                 void *data)
54 {
55         struct mce *mce = (struct mce *)data;
56         unsigned int next, entry;
57
58         wmb();
59         for (;;) {
60                 entry = mce_log_get_idx_check(mcelog.next);
61                 for (;;) {
62
63                         /*
64                          * When the buffer fills up discard new entries.
65                          * Assume that the earlier errors are the more
66                          * interesting ones:
67                          */
68                         if (entry >= MCE_LOG_LEN) {
69                                 set_bit(MCE_OVERFLOW,
70                                         (unsigned long *)&mcelog.flags);
71                                 return NOTIFY_OK;
72                         }
73                         /* Old left over entry. Skip: */
74                         if (mcelog.entry[entry].finished) {
75                                 entry++;
76                                 continue;
77                         }
78                         break;
79                 }
80                 smp_rmb();
81                 next = entry + 1;
82                 if (cmpxchg(&mcelog.next, entry, next) == entry)
83                         break;
84         }
85         memcpy(mcelog.entry + entry, mce, sizeof(struct mce));
86         wmb();
87         mcelog.entry[entry].finished = 1;
88         wmb();
89
90         /* wake processes polling /dev/mcelog */
91         wake_up_interruptible(&mce_chrdev_wait);
92
93         return NOTIFY_OK;
94 }
95
96 static struct notifier_block dev_mcelog_nb = {
97         .notifier_call  = dev_mce_log,
98         .priority       = MCE_PRIO_MCELOG,
99 };
100
101 static void mce_do_trigger(struct work_struct *work)
102 {
103         call_usermodehelper(mce_helper, mce_helper_argv, NULL, UMH_NO_WAIT);
104 }
105
106 static DECLARE_WORK(mce_trigger_work, mce_do_trigger);
107
108
109 void mce_work_trigger(void)
110 {
111         if (mce_helper[0])
112                 schedule_work(&mce_trigger_work);
113 }
114
115 static ssize_t
116 show_trigger(struct device *s, struct device_attribute *attr, char *buf)
117 {
118         strcpy(buf, mce_helper);
119         strcat(buf, "\n");
120         return strlen(mce_helper) + 1;
121 }
122
123 static ssize_t set_trigger(struct device *s, struct device_attribute *attr,
124                                 const char *buf, size_t siz)
125 {
126         char *p;
127
128         strncpy(mce_helper, buf, sizeof(mce_helper));
129         mce_helper[sizeof(mce_helper)-1] = 0;
130         p = strchr(mce_helper, '\n');
131
132         if (p)
133                 *p = 0;
134
135         return strlen(mce_helper) + !!p;
136 }
137
138 DEVICE_ATTR(trigger, 0644, show_trigger, set_trigger);
139
140 /*
141  * mce_chrdev: Character device /dev/mcelog to read and clear the MCE log.
142  */
143
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? */
147
148 static int mce_chrdev_open(struct inode *inode, struct file *file)
149 {
150         spin_lock(&mce_chrdev_state_lock);
151
152         if (mce_chrdev_open_exclu ||
153             (mce_chrdev_open_count && (file->f_flags & O_EXCL))) {
154                 spin_unlock(&mce_chrdev_state_lock);
155
156                 return -EBUSY;
157         }
158
159         if (file->f_flags & O_EXCL)
160                 mce_chrdev_open_exclu = 1;
161         mce_chrdev_open_count++;
162
163         spin_unlock(&mce_chrdev_state_lock);
164
165         return nonseekable_open(inode, file);
166 }
167
168 static int mce_chrdev_release(struct inode *inode, struct file *file)
169 {
170         spin_lock(&mce_chrdev_state_lock);
171
172         mce_chrdev_open_count--;
173         mce_chrdev_open_exclu = 0;
174
175         spin_unlock(&mce_chrdev_state_lock);
176
177         return 0;
178 }
179
180 static void collect_tscs(void *data)
181 {
182         unsigned long *cpu_tsc = (unsigned long *)data;
183
184         cpu_tsc[smp_processor_id()] = rdtsc();
185 }
186
187 static int mce_apei_read_done;
188
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)
191 {
192         int rc;
193         u64 record_id;
194         struct mce m;
195
196         if (usize < sizeof(struct mce))
197                 return -EINVAL;
198
199         rc = apei_read_mce(&m, &record_id);
200         /* Error or no more MCE record */
201         if (rc <= 0) {
202                 mce_apei_read_done = 1;
203                 /*
204                  * When ERST is disabled, mce_chrdev_read() should return
205                  * "no record" instead of "no device."
206                  */
207                 if (rc == -ENODEV)
208                         return 0;
209                 return rc;
210         }
211         rc = -EFAULT;
212         if (copy_to_user(*ubuf, &m, sizeof(struct mce)))
213                 return rc;
214         /*
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.
219          */
220         rc = apei_clear_mce(record_id);
221         if (rc) {
222                 mce_apei_read_done = 1;
223                 return rc;
224         }
225         *ubuf += sizeof(struct mce);
226
227         return 0;
228 }
229
230 static ssize_t mce_chrdev_read(struct file *filp, char __user *ubuf,
231                                 size_t usize, loff_t *off)
232 {
233         char __user *buf = ubuf;
234         unsigned long *cpu_tsc;
235         unsigned prev, next;
236         int i, err;
237
238         cpu_tsc = kmalloc(nr_cpu_ids * sizeof(long), GFP_KERNEL);
239         if (!cpu_tsc)
240                 return -ENOMEM;
241
242         mutex_lock(&mce_chrdev_read_mutex);
243
244         if (!mce_apei_read_done) {
245                 err = __mce_read_apei(&buf, usize);
246                 if (err || buf != ubuf)
247                         goto out;
248         }
249
250         next = mce_log_get_idx_check(mcelog.next);
251
252         /* Only supports full reads right now */
253         err = -EINVAL;
254         if (*off != 0 || usize < MCE_LOG_LEN*sizeof(struct mce))
255                 goto out;
256
257         err = 0;
258         prev = 0;
259         do {
260                 for (i = prev; i < next; i++) {
261                         unsigned long start = jiffies;
262                         struct mce *m = &mcelog.entry[i];
263
264                         while (!m->finished) {
265                                 if (time_after_eq(jiffies, start + 2)) {
266                                         memset(m, 0, sizeof(*m));
267                                         goto timeout;
268                                 }
269                                 cpu_relax();
270                         }
271                         smp_rmb();
272                         err |= copy_to_user(buf, m, sizeof(*m));
273                         buf += sizeof(*m);
274 timeout:
275                         ;
276                 }
277
278                 memset(mcelog.entry + prev, 0,
279                        (next - prev) * sizeof(struct mce));
280                 prev = next;
281                 next = cmpxchg(&mcelog.next, prev, 0);
282         } while (next != prev);
283
284         synchronize_sched();
285
286         /*
287          * Collect entries that were still getting written before the
288          * synchronize.
289          */
290         on_each_cpu(collect_tscs, cpu_tsc, 1);
291
292         for (i = next; i < MCE_LOG_LEN; i++) {
293                 struct mce *m = &mcelog.entry[i];
294
295                 if (m->finished && m->tsc < cpu_tsc[m->cpu]) {
296                         err |= copy_to_user(buf, m, sizeof(*m));
297                         smp_rmb();
298                         buf += sizeof(*m);
299                         memset(m, 0, sizeof(*m));
300                 }
301         }
302
303         if (err)
304                 err = -EFAULT;
305
306 out:
307         mutex_unlock(&mce_chrdev_read_mutex);
308         kfree(cpu_tsc);
309
310         return err ? err : buf - ubuf;
311 }
312
313 static unsigned int mce_chrdev_poll(struct file *file, poll_table *wait)
314 {
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;
320         return 0;
321 }
322
323 static long mce_chrdev_ioctl(struct file *f, unsigned int cmd,
324                                 unsigned long arg)
325 {
326         int __user *p = (int __user *)arg;
327
328         if (!capable(CAP_SYS_ADMIN))
329                 return -EPERM;
330
331         switch (cmd) {
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: {
337                 unsigned flags;
338
339                 do {
340                         flags = mcelog.flags;
341                 } while (cmpxchg(&mcelog.flags, flags, 0) != flags);
342
343                 return put_user(flags, p);
344         }
345         default:
346                 return -ENOTTY;
347         }
348 }
349
350 void mce_register_injector_chain(struct notifier_block *nb)
351 {
352         blocking_notifier_chain_register(&mce_injector_chain, nb);
353 }
354 EXPORT_SYMBOL_GPL(mce_register_injector_chain);
355
356 void mce_unregister_injector_chain(struct notifier_block *nb)
357 {
358         blocking_notifier_chain_unregister(&mce_injector_chain, nb);
359 }
360 EXPORT_SYMBOL_GPL(mce_unregister_injector_chain);
361
362 static ssize_t mce_chrdev_write(struct file *filp, const char __user *ubuf,
363                                 size_t usize, loff_t *off)
364 {
365         struct mce m;
366
367         if (!capable(CAP_SYS_ADMIN))
368                 return -EPERM;
369         /*
370          * There are some cases where real MSR reads could slip
371          * through.
372          */
373         if (!boot_cpu_has(X86_FEATURE_MCE) || !boot_cpu_has(X86_FEATURE_MCA))
374                 return -EIO;
375
376         if ((unsigned long)usize > sizeof(struct mce))
377                 usize = sizeof(struct mce);
378         if (copy_from_user(&m, ubuf, usize))
379                 return -EFAULT;
380
381         if (m.extcpu >= num_possible_cpus() || !cpu_online(m.extcpu))
382                 return -EINVAL;
383
384         /*
385          * Need to give user space some time to set everything up,
386          * so do it a jiffie or two later everywhere.
387          */
388         schedule_timeout(2);
389
390         blocking_notifier_call_chain(&mce_injector_chain, 0, &m);
391
392         return usize;
393 }
394
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,
402         .llseek                 = no_llseek,
403 };
404
405 static struct miscdevice mce_chrdev_device = {
406         MISC_MCELOG_MINOR,
407         "mcelog",
408         &mce_chrdev_ops,
409 };
410
411 static __init int dev_mcelog_init_device(void)
412 {
413         int err;
414
415         /* register character device /dev/mcelog */
416         err = misc_register(&mce_chrdev_device);
417         if (err) {
418                 if (err == -EBUSY)
419                         /* Xen dom0 might have registered the device already. */
420                         pr_info("Unable to init device /dev/mcelog, already registered");
421                 else
422                         pr_err("Unable to init device /dev/mcelog (rc: %d)\n", err);
423
424                 return err;
425         }
426
427         mce_register_decode_chain(&dev_mcelog_nb);
428         return 0;
429 }
430 device_initcall_sync(dev_mcelog_init_device);