xref: /openbmc/linux/arch/x86/kernel/cpu/mce/dev-mcelog.c (revision 26cfd12b)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * /dev/mcelog driver
4  *
5  * K8 parts Copyright 2002,2003 Andi Kleen, SuSE Labs.
6  * Rest from unknown author(s).
7  * 2004 Andi Kleen. Rewrote most of it.
8  * Copyright 2008 Intel Corporation
9  * Author: Andi Kleen
10  */
11 
12 #include <linux/miscdevice.h>
13 #include <linux/slab.h>
14 #include <linux/kmod.h>
15 #include <linux/poll.h>
16 
17 #include "internal.h"
18 
19 static BLOCKING_NOTIFIER_HEAD(mce_injector_chain);
20 
21 static DEFINE_MUTEX(mce_chrdev_read_mutex);
22 
23 static char mce_helper[128];
24 static char *mce_helper_argv[2] = { mce_helper, NULL };
25 
26 /*
27  * Lockless MCE logging infrastructure.
28  * This avoids deadlocks on printk locks without having to break locks. Also
29  * separate MCEs from kernel messages to avoid bogus bug reports.
30  */
31 
32 static struct mce_log_buffer *mcelog;
33 
34 static DECLARE_WAIT_QUEUE_HEAD(mce_chrdev_wait);
35 
36 static int dev_mce_log(struct notifier_block *nb, unsigned long val,
37 				void *data)
38 {
39 	struct mce *mce = (struct mce *)data;
40 	unsigned int entry;
41 
42 	if (mce->kflags & MCE_HANDLED_CEC)
43 		return NOTIFY_DONE;
44 
45 	mutex_lock(&mce_chrdev_read_mutex);
46 
47 	entry = mcelog->next;
48 
49 	/*
50 	 * When the buffer fills up discard new entries. Assume that the
51 	 * earlier errors are the more interesting ones:
52 	 */
53 	if (entry >= mcelog->len) {
54 		set_bit(MCE_OVERFLOW, (unsigned long *)&mcelog->flags);
55 		goto unlock;
56 	}
57 
58 	mcelog->next = entry + 1;
59 
60 	memcpy(mcelog->entry + entry, mce, sizeof(struct mce));
61 	mcelog->entry[entry].finished = 1;
62 	mcelog->entry[entry].kflags = 0;
63 
64 	/* wake processes polling /dev/mcelog */
65 	wake_up_interruptible(&mce_chrdev_wait);
66 
67 unlock:
68 	mutex_unlock(&mce_chrdev_read_mutex);
69 
70 	mce->kflags |= MCE_HANDLED_MCELOG;
71 	return NOTIFY_OK;
72 }
73 
74 static struct notifier_block dev_mcelog_nb = {
75 	.notifier_call	= dev_mce_log,
76 	.priority	= MCE_PRIO_MCELOG,
77 };
78 
79 static void mce_do_trigger(struct work_struct *work)
80 {
81 	call_usermodehelper(mce_helper, mce_helper_argv, NULL, UMH_NO_WAIT);
82 }
83 
84 static DECLARE_WORK(mce_trigger_work, mce_do_trigger);
85 
86 
87 void mce_work_trigger(void)
88 {
89 	if (mce_helper[0])
90 		schedule_work(&mce_trigger_work);
91 }
92 
93 static ssize_t
94 show_trigger(struct device *s, struct device_attribute *attr, char *buf)
95 {
96 	strcpy(buf, mce_helper);
97 	strcat(buf, "\n");
98 	return strlen(mce_helper) + 1;
99 }
100 
101 static ssize_t set_trigger(struct device *s, struct device_attribute *attr,
102 				const char *buf, size_t siz)
103 {
104 	char *p;
105 
106 	strncpy(mce_helper, buf, sizeof(mce_helper));
107 	mce_helper[sizeof(mce_helper)-1] = 0;
108 	p = strchr(mce_helper, '\n');
109 
110 	if (p)
111 		*p = 0;
112 
113 	return strlen(mce_helper) + !!p;
114 }
115 
116 DEVICE_ATTR(trigger, 0644, show_trigger, set_trigger);
117 
118 /*
119  * mce_chrdev: Character device /dev/mcelog to read and clear the MCE log.
120  */
121 
122 static DEFINE_SPINLOCK(mce_chrdev_state_lock);
123 static int mce_chrdev_open_count;	/* #times opened */
124 static int mce_chrdev_open_exclu;	/* already open exclusive? */
125 
126 static int mce_chrdev_open(struct inode *inode, struct file *file)
127 {
128 	spin_lock(&mce_chrdev_state_lock);
129 
130 	if (mce_chrdev_open_exclu ||
131 	    (mce_chrdev_open_count && (file->f_flags & O_EXCL))) {
132 		spin_unlock(&mce_chrdev_state_lock);
133 
134 		return -EBUSY;
135 	}
136 
137 	if (file->f_flags & O_EXCL)
138 		mce_chrdev_open_exclu = 1;
139 	mce_chrdev_open_count++;
140 
141 	spin_unlock(&mce_chrdev_state_lock);
142 
143 	return nonseekable_open(inode, file);
144 }
145 
146 static int mce_chrdev_release(struct inode *inode, struct file *file)
147 {
148 	spin_lock(&mce_chrdev_state_lock);
149 
150 	mce_chrdev_open_count--;
151 	mce_chrdev_open_exclu = 0;
152 
153 	spin_unlock(&mce_chrdev_state_lock);
154 
155 	return 0;
156 }
157 
158 static int mce_apei_read_done;
159 
160 /* Collect MCE record of previous boot in persistent storage via APEI ERST. */
161 static int __mce_read_apei(char __user **ubuf, size_t usize)
162 {
163 	int rc;
164 	u64 record_id;
165 	struct mce m;
166 
167 	if (usize < sizeof(struct mce))
168 		return -EINVAL;
169 
170 	rc = apei_read_mce(&m, &record_id);
171 	/* Error or no more MCE record */
172 	if (rc <= 0) {
173 		mce_apei_read_done = 1;
174 		/*
175 		 * When ERST is disabled, mce_chrdev_read() should return
176 		 * "no record" instead of "no device."
177 		 */
178 		if (rc == -ENODEV)
179 			return 0;
180 		return rc;
181 	}
182 	rc = -EFAULT;
183 	if (copy_to_user(*ubuf, &m, sizeof(struct mce)))
184 		return rc;
185 	/*
186 	 * In fact, we should have cleared the record after that has
187 	 * been flushed to the disk or sent to network in
188 	 * /sbin/mcelog, but we have no interface to support that now,
189 	 * so just clear it to avoid duplication.
190 	 */
191 	rc = apei_clear_mce(record_id);
192 	if (rc) {
193 		mce_apei_read_done = 1;
194 		return rc;
195 	}
196 	*ubuf += sizeof(struct mce);
197 
198 	return 0;
199 }
200 
201 static ssize_t mce_chrdev_read(struct file *filp, char __user *ubuf,
202 				size_t usize, loff_t *off)
203 {
204 	char __user *buf = ubuf;
205 	unsigned next;
206 	int i, err;
207 
208 	mutex_lock(&mce_chrdev_read_mutex);
209 
210 	if (!mce_apei_read_done) {
211 		err = __mce_read_apei(&buf, usize);
212 		if (err || buf != ubuf)
213 			goto out;
214 	}
215 
216 	/* Only supports full reads right now */
217 	err = -EINVAL;
218 	if (*off != 0 || usize < mcelog->len * sizeof(struct mce))
219 		goto out;
220 
221 	next = mcelog->next;
222 	err = 0;
223 
224 	for (i = 0; i < next; i++) {
225 		struct mce *m = &mcelog->entry[i];
226 
227 		err |= copy_to_user(buf, m, sizeof(*m));
228 		buf += sizeof(*m);
229 	}
230 
231 	memset(mcelog->entry, 0, next * sizeof(struct mce));
232 	mcelog->next = 0;
233 
234 	if (err)
235 		err = -EFAULT;
236 
237 out:
238 	mutex_unlock(&mce_chrdev_read_mutex);
239 
240 	return err ? err : buf - ubuf;
241 }
242 
243 static __poll_t mce_chrdev_poll(struct file *file, poll_table *wait)
244 {
245 	poll_wait(file, &mce_chrdev_wait, wait);
246 	if (READ_ONCE(mcelog->next))
247 		return EPOLLIN | EPOLLRDNORM;
248 	if (!mce_apei_read_done && apei_check_mce())
249 		return EPOLLIN | EPOLLRDNORM;
250 	return 0;
251 }
252 
253 static long mce_chrdev_ioctl(struct file *f, unsigned int cmd,
254 				unsigned long arg)
255 {
256 	int __user *p = (int __user *)arg;
257 
258 	if (!capable(CAP_SYS_ADMIN))
259 		return -EPERM;
260 
261 	switch (cmd) {
262 	case MCE_GET_RECORD_LEN:
263 		return put_user(sizeof(struct mce), p);
264 	case MCE_GET_LOG_LEN:
265 		return put_user(mcelog->len, p);
266 	case MCE_GETCLEAR_FLAGS: {
267 		unsigned flags;
268 
269 		do {
270 			flags = mcelog->flags;
271 		} while (cmpxchg(&mcelog->flags, flags, 0) != flags);
272 
273 		return put_user(flags, p);
274 	}
275 	default:
276 		return -ENOTTY;
277 	}
278 }
279 
280 void mce_register_injector_chain(struct notifier_block *nb)
281 {
282 	blocking_notifier_chain_register(&mce_injector_chain, nb);
283 }
284 EXPORT_SYMBOL_GPL(mce_register_injector_chain);
285 
286 void mce_unregister_injector_chain(struct notifier_block *nb)
287 {
288 	blocking_notifier_chain_unregister(&mce_injector_chain, nb);
289 }
290 EXPORT_SYMBOL_GPL(mce_unregister_injector_chain);
291 
292 static ssize_t mce_chrdev_write(struct file *filp, const char __user *ubuf,
293 				size_t usize, loff_t *off)
294 {
295 	struct mce m;
296 
297 	if (!capable(CAP_SYS_ADMIN))
298 		return -EPERM;
299 	/*
300 	 * There are some cases where real MSR reads could slip
301 	 * through.
302 	 */
303 	if (!boot_cpu_has(X86_FEATURE_MCE) || !boot_cpu_has(X86_FEATURE_MCA))
304 		return -EIO;
305 
306 	if ((unsigned long)usize > sizeof(struct mce))
307 		usize = sizeof(struct mce);
308 	if (copy_from_user(&m, ubuf, usize))
309 		return -EFAULT;
310 
311 	if (m.extcpu >= num_possible_cpus() || !cpu_online(m.extcpu))
312 		return -EINVAL;
313 
314 	/*
315 	 * Need to give user space some time to set everything up,
316 	 * so do it a jiffie or two later everywhere.
317 	 */
318 	schedule_timeout(2);
319 
320 	blocking_notifier_call_chain(&mce_injector_chain, 0, &m);
321 
322 	return usize;
323 }
324 
325 static const struct file_operations mce_chrdev_ops = {
326 	.open			= mce_chrdev_open,
327 	.release		= mce_chrdev_release,
328 	.read			= mce_chrdev_read,
329 	.write			= mce_chrdev_write,
330 	.poll			= mce_chrdev_poll,
331 	.unlocked_ioctl		= mce_chrdev_ioctl,
332 	.compat_ioctl		= compat_ptr_ioctl,
333 	.llseek			= no_llseek,
334 };
335 
336 static struct miscdevice mce_chrdev_device = {
337 	MISC_MCELOG_MINOR,
338 	"mcelog",
339 	&mce_chrdev_ops,
340 };
341 
342 static __init int dev_mcelog_init_device(void)
343 {
344 	int mce_log_len;
345 	int err;
346 
347 	mce_log_len = max(MCE_LOG_MIN_LEN, num_online_cpus());
348 	mcelog = kzalloc(sizeof(*mcelog) + mce_log_len * sizeof(struct mce), GFP_KERNEL);
349 	if (!mcelog)
350 		return -ENOMEM;
351 
352 	memcpy(mcelog->signature, MCE_LOG_SIGNATURE, sizeof(mcelog->signature));
353 	mcelog->len = mce_log_len;
354 	mcelog->recordlen = sizeof(struct mce);
355 
356 	/* register character device /dev/mcelog */
357 	err = misc_register(&mce_chrdev_device);
358 	if (err) {
359 		if (err == -EBUSY)
360 			/* Xen dom0 might have registered the device already. */
361 			pr_info("Unable to init device /dev/mcelog, already registered");
362 		else
363 			pr_err("Unable to init device /dev/mcelog (rc: %d)\n", err);
364 
365 		kfree(mcelog);
366 		return err;
367 	}
368 
369 	mce_register_decode_chain(&dev_mcelog_nb);
370 	return 0;
371 }
372 device_initcall_sync(dev_mcelog_init_device);
373