dev-mcelog.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  11. #include <linux/miscdevice.h>
  12. #include <linux/slab.h>
  13. #include <linux/kmod.h>
  14. #include <linux/poll.h>
  15. #include "mce-internal.h"
  16. static BLOCKING_NOTIFIER_HEAD(mce_injector_chain);
  17. static DEFINE_MUTEX(mce_chrdev_read_mutex);
  18. static char mce_helper[128];
  19. static char *mce_helper_argv[2] = { mce_helper, NULL };
  20. /*
  21. * Lockless MCE logging infrastructure.
  22. * This avoids deadlocks on printk locks without having to break locks. Also
  23. * separate MCEs from kernel messages to avoid bogus bug reports.
  24. */
  25. static struct mce_log_buffer mcelog = {
  26. .signature = MCE_LOG_SIGNATURE,
  27. .len = MCE_LOG_LEN,
  28. .recordlen = sizeof(struct mce),
  29. };
  30. static DECLARE_WAIT_QUEUE_HEAD(mce_chrdev_wait);
  31. static int dev_mce_log(struct notifier_block *nb, unsigned long val,
  32. void *data)
  33. {
  34. struct mce *mce = (struct mce *)data;
  35. unsigned int entry;
  36. mutex_lock(&mce_chrdev_read_mutex);
  37. entry = mcelog.next;
  38. /*
  39. * When the buffer fills up discard new entries. Assume that the
  40. * earlier errors are the more interesting ones:
  41. */
  42. if (entry >= MCE_LOG_LEN) {
  43. set_bit(MCE_OVERFLOW, (unsigned long *)&mcelog.flags);
  44. goto unlock;
  45. }
  46. mcelog.next = entry + 1;
  47. memcpy(mcelog.entry + entry, mce, sizeof(struct mce));
  48. mcelog.entry[entry].finished = 1;
  49. /* wake processes polling /dev/mcelog */
  50. wake_up_interruptible(&mce_chrdev_wait);
  51. unlock:
  52. mutex_unlock(&mce_chrdev_read_mutex);
  53. return NOTIFY_OK;
  54. }
  55. static struct notifier_block dev_mcelog_nb = {
  56. .notifier_call = dev_mce_log,
  57. .priority = MCE_PRIO_MCELOG,
  58. };
  59. static void mce_do_trigger(struct work_struct *work)
  60. {
  61. call_usermodehelper(mce_helper, mce_helper_argv, NULL, UMH_NO_WAIT);
  62. }
  63. static DECLARE_WORK(mce_trigger_work, mce_do_trigger);
  64. void mce_work_trigger(void)
  65. {
  66. if (mce_helper[0])
  67. schedule_work(&mce_trigger_work);
  68. }
  69. static ssize_t
  70. show_trigger(struct device *s, struct device_attribute *attr, char *buf)
  71. {
  72. strcpy(buf, mce_helper);
  73. strcat(buf, "\n");
  74. return strlen(mce_helper) + 1;
  75. }
  76. static ssize_t set_trigger(struct device *s, struct device_attribute *attr,
  77. const char *buf, size_t siz)
  78. {
  79. char *p;
  80. strncpy(mce_helper, buf, sizeof(mce_helper));
  81. mce_helper[sizeof(mce_helper)-1] = 0;
  82. p = strchr(mce_helper, '\n');
  83. if (p)
  84. *p = 0;
  85. return strlen(mce_helper) + !!p;
  86. }
  87. DEVICE_ATTR(trigger, 0644, show_trigger, set_trigger);
  88. /*
  89. * mce_chrdev: Character device /dev/mcelog to read and clear the MCE log.
  90. */
  91. static DEFINE_SPINLOCK(mce_chrdev_state_lock);
  92. static int mce_chrdev_open_count; /* #times opened */
  93. static int mce_chrdev_open_exclu; /* already open exclusive? */
  94. static int mce_chrdev_open(struct inode *inode, struct file *file)
  95. {
  96. spin_lock(&mce_chrdev_state_lock);
  97. if (mce_chrdev_open_exclu ||
  98. (mce_chrdev_open_count && (file->f_flags & O_EXCL))) {
  99. spin_unlock(&mce_chrdev_state_lock);
  100. return -EBUSY;
  101. }
  102. if (file->f_flags & O_EXCL)
  103. mce_chrdev_open_exclu = 1;
  104. mce_chrdev_open_count++;
  105. spin_unlock(&mce_chrdev_state_lock);
  106. return nonseekable_open(inode, file);
  107. }
  108. static int mce_chrdev_release(struct inode *inode, struct file *file)
  109. {
  110. spin_lock(&mce_chrdev_state_lock);
  111. mce_chrdev_open_count--;
  112. mce_chrdev_open_exclu = 0;
  113. spin_unlock(&mce_chrdev_state_lock);
  114. return 0;
  115. }
  116. static int mce_apei_read_done;
  117. /* Collect MCE record of previous boot in persistent storage via APEI ERST. */
  118. static int __mce_read_apei(char __user **ubuf, size_t usize)
  119. {
  120. int rc;
  121. u64 record_id;
  122. struct mce m;
  123. if (usize < sizeof(struct mce))
  124. return -EINVAL;
  125. rc = apei_read_mce(&m, &record_id);
  126. /* Error or no more MCE record */
  127. if (rc <= 0) {
  128. mce_apei_read_done = 1;
  129. /*
  130. * When ERST is disabled, mce_chrdev_read() should return
  131. * "no record" instead of "no device."
  132. */
  133. if (rc == -ENODEV)
  134. return 0;
  135. return rc;
  136. }
  137. rc = -EFAULT;
  138. if (copy_to_user(*ubuf, &m, sizeof(struct mce)))
  139. return rc;
  140. /*
  141. * In fact, we should have cleared the record after that has
  142. * been flushed to the disk or sent to network in
  143. * /sbin/mcelog, but we have no interface to support that now,
  144. * so just clear it to avoid duplication.
  145. */
  146. rc = apei_clear_mce(record_id);
  147. if (rc) {
  148. mce_apei_read_done = 1;
  149. return rc;
  150. }
  151. *ubuf += sizeof(struct mce);
  152. return 0;
  153. }
  154. static ssize_t mce_chrdev_read(struct file *filp, char __user *ubuf,
  155. size_t usize, loff_t *off)
  156. {
  157. char __user *buf = ubuf;
  158. unsigned next;
  159. int i, err;
  160. mutex_lock(&mce_chrdev_read_mutex);
  161. if (!mce_apei_read_done) {
  162. err = __mce_read_apei(&buf, usize);
  163. if (err || buf != ubuf)
  164. goto out;
  165. }
  166. /* Only supports full reads right now */
  167. err = -EINVAL;
  168. if (*off != 0 || usize < MCE_LOG_LEN*sizeof(struct mce))
  169. goto out;
  170. next = mcelog.next;
  171. err = 0;
  172. for (i = 0; i < next; i++) {
  173. struct mce *m = &mcelog.entry[i];
  174. err |= copy_to_user(buf, m, sizeof(*m));
  175. buf += sizeof(*m);
  176. }
  177. memset(mcelog.entry, 0, next * sizeof(struct mce));
  178. mcelog.next = 0;
  179. if (err)
  180. err = -EFAULT;
  181. out:
  182. mutex_unlock(&mce_chrdev_read_mutex);
  183. return err ? err : buf - ubuf;
  184. }
  185. static __poll_t mce_chrdev_poll(struct file *file, poll_table *wait)
  186. {
  187. poll_wait(file, &mce_chrdev_wait, wait);
  188. if (READ_ONCE(mcelog.next))
  189. return EPOLLIN | EPOLLRDNORM;
  190. if (!mce_apei_read_done && apei_check_mce())
  191. return EPOLLIN | EPOLLRDNORM;
  192. return 0;
  193. }
  194. static long mce_chrdev_ioctl(struct file *f, unsigned int cmd,
  195. unsigned long arg)
  196. {
  197. int __user *p = (int __user *)arg;
  198. if (!capable(CAP_SYS_ADMIN))
  199. return -EPERM;
  200. switch (cmd) {
  201. case MCE_GET_RECORD_LEN:
  202. return put_user(sizeof(struct mce), p);
  203. case MCE_GET_LOG_LEN:
  204. return put_user(MCE_LOG_LEN, p);
  205. case MCE_GETCLEAR_FLAGS: {
  206. unsigned flags;
  207. do {
  208. flags = mcelog.flags;
  209. } while (cmpxchg(&mcelog.flags, flags, 0) != flags);
  210. return put_user(flags, p);
  211. }
  212. default:
  213. return -ENOTTY;
  214. }
  215. }
  216. void mce_register_injector_chain(struct notifier_block *nb)
  217. {
  218. blocking_notifier_chain_register(&mce_injector_chain, nb);
  219. }
  220. EXPORT_SYMBOL_GPL(mce_register_injector_chain);
  221. void mce_unregister_injector_chain(struct notifier_block *nb)
  222. {
  223. blocking_notifier_chain_unregister(&mce_injector_chain, nb);
  224. }
  225. EXPORT_SYMBOL_GPL(mce_unregister_injector_chain);
  226. static ssize_t mce_chrdev_write(struct file *filp, const char __user *ubuf,
  227. size_t usize, loff_t *off)
  228. {
  229. struct mce m;
  230. if (!capable(CAP_SYS_ADMIN))
  231. return -EPERM;
  232. /*
  233. * There are some cases where real MSR reads could slip
  234. * through.
  235. */
  236. if (!boot_cpu_has(X86_FEATURE_MCE) || !boot_cpu_has(X86_FEATURE_MCA))
  237. return -EIO;
  238. if ((unsigned long)usize > sizeof(struct mce))
  239. usize = sizeof(struct mce);
  240. if (copy_from_user(&m, ubuf, usize))
  241. return -EFAULT;
  242. if (m.extcpu >= num_possible_cpus() || !cpu_online(m.extcpu))
  243. return -EINVAL;
  244. /*
  245. * Need to give user space some time to set everything up,
  246. * so do it a jiffie or two later everywhere.
  247. */
  248. schedule_timeout(2);
  249. blocking_notifier_call_chain(&mce_injector_chain, 0, &m);
  250. return usize;
  251. }
  252. static const struct file_operations mce_chrdev_ops = {
  253. .open = mce_chrdev_open,
  254. .release = mce_chrdev_release,
  255. .read = mce_chrdev_read,
  256. .write = mce_chrdev_write,
  257. .poll = mce_chrdev_poll,
  258. .unlocked_ioctl = mce_chrdev_ioctl,
  259. .llseek = no_llseek,
  260. };
  261. static struct miscdevice mce_chrdev_device = {
  262. MISC_MCELOG_MINOR,
  263. "mcelog",
  264. &mce_chrdev_ops,
  265. };
  266. static __init int dev_mcelog_init_device(void)
  267. {
  268. int err;
  269. /* register character device /dev/mcelog */
  270. err = misc_register(&mce_chrdev_device);
  271. if (err) {
  272. if (err == -EBUSY)
  273. /* Xen dom0 might have registered the device already. */
  274. pr_info("Unable to init device /dev/mcelog, already registered");
  275. else
  276. pr_err("Unable to init device /dev/mcelog (rc: %d)\n", err);
  277. return err;
  278. }
  279. mce_register_decode_chain(&dev_mcelog_nb);
  280. return 0;
  281. }
  282. device_initcall_sync(dev_mcelog_init_device);