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 
dev_mce_log(struct notifier_block * nb,unsigned long val,void * data)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 	if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD)
71 		mce->kflags |= MCE_HANDLED_MCELOG;
72 
73 	return NOTIFY_OK;
74 }
75 
76 static struct notifier_block dev_mcelog_nb = {
77 	.notifier_call	= dev_mce_log,
78 	.priority	= MCE_PRIO_MCELOG,
79 };
80 
mce_do_trigger(struct work_struct * work)81 static void mce_do_trigger(struct work_struct *work)
82 {
83 	call_usermodehelper(mce_helper, mce_helper_argv, NULL, UMH_NO_WAIT);
84 }
85 
86 static DECLARE_WORK(mce_trigger_work, mce_do_trigger);
87 
88 
mce_work_trigger(void)89 void mce_work_trigger(void)
90 {
91 	if (mce_helper[0])
92 		schedule_work(&mce_trigger_work);
93 }
94 
95 static ssize_t
show_trigger(struct device * s,struct device_attribute * attr,char * buf)96 show_trigger(struct device *s, struct device_attribute *attr, char *buf)
97 {
98 	strcpy(buf, mce_helper);
99 	strcat(buf, "\n");
100 	return strlen(mce_helper) + 1;
101 }
102 
set_trigger(struct device * s,struct device_attribute * attr,const char * buf,size_t siz)103 static ssize_t set_trigger(struct device *s, struct device_attribute *attr,
104 				const char *buf, size_t siz)
105 {
106 	char *p;
107 
108 	strncpy(mce_helper, buf, sizeof(mce_helper));
109 	mce_helper[sizeof(mce_helper)-1] = 0;
110 	p = strchr(mce_helper, '\n');
111 
112 	if (p)
113 		*p = 0;
114 
115 	return strlen(mce_helper) + !!p;
116 }
117 
118 DEVICE_ATTR(trigger, 0644, show_trigger, set_trigger);
119 
120 /*
121  * mce_chrdev: Character device /dev/mcelog to read and clear the MCE log.
122  */
123 
124 static DEFINE_SPINLOCK(mce_chrdev_state_lock);
125 static int mce_chrdev_open_count;	/* #times opened */
126 static int mce_chrdev_open_exclu;	/* already open exclusive? */
127 
mce_chrdev_open(struct inode * inode,struct file * file)128 static int mce_chrdev_open(struct inode *inode, struct file *file)
129 {
130 	spin_lock(&mce_chrdev_state_lock);
131 
132 	if (mce_chrdev_open_exclu ||
133 	    (mce_chrdev_open_count && (file->f_flags & O_EXCL))) {
134 		spin_unlock(&mce_chrdev_state_lock);
135 
136 		return -EBUSY;
137 	}
138 
139 	if (file->f_flags & O_EXCL)
140 		mce_chrdev_open_exclu = 1;
141 	mce_chrdev_open_count++;
142 
143 	spin_unlock(&mce_chrdev_state_lock);
144 
145 	return nonseekable_open(inode, file);
146 }
147 
mce_chrdev_release(struct inode * inode,struct file * file)148 static int mce_chrdev_release(struct inode *inode, struct file *file)
149 {
150 	spin_lock(&mce_chrdev_state_lock);
151 
152 	mce_chrdev_open_count--;
153 	mce_chrdev_open_exclu = 0;
154 
155 	spin_unlock(&mce_chrdev_state_lock);
156 
157 	return 0;
158 }
159 
160 static int mce_apei_read_done;
161 
162 /* Collect MCE record of previous boot in persistent storage via APEI ERST. */
__mce_read_apei(char __user ** ubuf,size_t usize)163 static int __mce_read_apei(char __user **ubuf, size_t usize)
164 {
165 	int rc;
166 	u64 record_id;
167 	struct mce m;
168 
169 	if (usize < sizeof(struct mce))
170 		return -EINVAL;
171 
172 	rc = apei_read_mce(&m, &record_id);
173 	/* Error or no more MCE record */
174 	if (rc <= 0) {
175 		mce_apei_read_done = 1;
176 		/*
177 		 * When ERST is disabled, mce_chrdev_read() should return
178 		 * "no record" instead of "no device."
179 		 */
180 		if (rc == -ENODEV)
181 			return 0;
182 		return rc;
183 	}
184 	rc = -EFAULT;
185 	if (copy_to_user(*ubuf, &m, sizeof(struct mce)))
186 		return rc;
187 	/*
188 	 * In fact, we should have cleared the record after that has
189 	 * been flushed to the disk or sent to network in
190 	 * /sbin/mcelog, but we have no interface to support that now,
191 	 * so just clear it to avoid duplication.
192 	 */
193 	rc = apei_clear_mce(record_id);
194 	if (rc) {
195 		mce_apei_read_done = 1;
196 		return rc;
197 	}
198 	*ubuf += sizeof(struct mce);
199 
200 	return 0;
201 }
202 
mce_chrdev_read(struct file * filp,char __user * ubuf,size_t usize,loff_t * off)203 static ssize_t mce_chrdev_read(struct file *filp, char __user *ubuf,
204 				size_t usize, loff_t *off)
205 {
206 	char __user *buf = ubuf;
207 	unsigned next;
208 	int i, err;
209 
210 	mutex_lock(&mce_chrdev_read_mutex);
211 
212 	if (!mce_apei_read_done) {
213 		err = __mce_read_apei(&buf, usize);
214 		if (err || buf != ubuf)
215 			goto out;
216 	}
217 
218 	/* Only supports full reads right now */
219 	err = -EINVAL;
220 	if (*off != 0 || usize < mcelog->len * sizeof(struct mce))
221 		goto out;
222 
223 	next = mcelog->next;
224 	err = 0;
225 
226 	for (i = 0; i < next; i++) {
227 		struct mce *m = &mcelog->entry[i];
228 
229 		err |= copy_to_user(buf, m, sizeof(*m));
230 		buf += sizeof(*m);
231 	}
232 
233 	memset(mcelog->entry, 0, next * sizeof(struct mce));
234 	mcelog->next = 0;
235 
236 	if (err)
237 		err = -EFAULT;
238 
239 out:
240 	mutex_unlock(&mce_chrdev_read_mutex);
241 
242 	return err ? err : buf - ubuf;
243 }
244 
mce_chrdev_poll(struct file * file,poll_table * wait)245 static __poll_t mce_chrdev_poll(struct file *file, poll_table *wait)
246 {
247 	poll_wait(file, &mce_chrdev_wait, wait);
248 	if (READ_ONCE(mcelog->next))
249 		return EPOLLIN | EPOLLRDNORM;
250 	if (!mce_apei_read_done && apei_check_mce())
251 		return EPOLLIN | EPOLLRDNORM;
252 	return 0;
253 }
254 
mce_chrdev_ioctl(struct file * f,unsigned int cmd,unsigned long arg)255 static long mce_chrdev_ioctl(struct file *f, unsigned int cmd,
256 				unsigned long arg)
257 {
258 	int __user *p = (int __user *)arg;
259 
260 	if (!capable(CAP_SYS_ADMIN))
261 		return -EPERM;
262 
263 	switch (cmd) {
264 	case MCE_GET_RECORD_LEN:
265 		return put_user(sizeof(struct mce), p);
266 	case MCE_GET_LOG_LEN:
267 		return put_user(mcelog->len, p);
268 	case MCE_GETCLEAR_FLAGS: {
269 		unsigned flags;
270 
271 		do {
272 			flags = mcelog->flags;
273 		} while (cmpxchg(&mcelog->flags, flags, 0) != flags);
274 
275 		return put_user(flags, p);
276 	}
277 	default:
278 		return -ENOTTY;
279 	}
280 }
281 
mce_register_injector_chain(struct notifier_block * nb)282 void mce_register_injector_chain(struct notifier_block *nb)
283 {
284 	blocking_notifier_chain_register(&mce_injector_chain, nb);
285 }
286 EXPORT_SYMBOL_GPL(mce_register_injector_chain);
287 
mce_unregister_injector_chain(struct notifier_block * nb)288 void mce_unregister_injector_chain(struct notifier_block *nb)
289 {
290 	blocking_notifier_chain_unregister(&mce_injector_chain, nb);
291 }
292 EXPORT_SYMBOL_GPL(mce_unregister_injector_chain);
293 
mce_chrdev_write(struct file * filp,const char __user * ubuf,size_t usize,loff_t * off)294 static ssize_t mce_chrdev_write(struct file *filp, const char __user *ubuf,
295 				size_t usize, loff_t *off)
296 {
297 	struct mce m;
298 
299 	if (!capable(CAP_SYS_ADMIN))
300 		return -EPERM;
301 	/*
302 	 * There are some cases where real MSR reads could slip
303 	 * through.
304 	 */
305 	if (!boot_cpu_has(X86_FEATURE_MCE) || !boot_cpu_has(X86_FEATURE_MCA))
306 		return -EIO;
307 
308 	if ((unsigned long)usize > sizeof(struct mce))
309 		usize = sizeof(struct mce);
310 	if (copy_from_user(&m, ubuf, usize))
311 		return -EFAULT;
312 
313 	if (m.extcpu >= num_possible_cpus() || !cpu_online(m.extcpu))
314 		return -EINVAL;
315 
316 	/*
317 	 * Need to give user space some time to set everything up,
318 	 * so do it a jiffie or two later everywhere.
319 	 */
320 	schedule_timeout(2);
321 
322 	blocking_notifier_call_chain(&mce_injector_chain, 0, &m);
323 
324 	return usize;
325 }
326 
327 static const struct file_operations mce_chrdev_ops = {
328 	.open			= mce_chrdev_open,
329 	.release		= mce_chrdev_release,
330 	.read			= mce_chrdev_read,
331 	.write			= mce_chrdev_write,
332 	.poll			= mce_chrdev_poll,
333 	.unlocked_ioctl		= mce_chrdev_ioctl,
334 	.compat_ioctl		= compat_ptr_ioctl,
335 	.llseek			= no_llseek,
336 };
337 
338 static struct miscdevice mce_chrdev_device = {
339 	MISC_MCELOG_MINOR,
340 	"mcelog",
341 	&mce_chrdev_ops,
342 };
343 
dev_mcelog_init_device(void)344 static __init int dev_mcelog_init_device(void)
345 {
346 	int mce_log_len;
347 	int err;
348 
349 	mce_log_len = max(MCE_LOG_MIN_LEN, num_online_cpus());
350 	mcelog = kzalloc(struct_size(mcelog, entry, mce_log_len), GFP_KERNEL);
351 	if (!mcelog)
352 		return -ENOMEM;
353 
354 	memcpy(mcelog->signature, MCE_LOG_SIGNATURE, sizeof(mcelog->signature));
355 	mcelog->len = mce_log_len;
356 	mcelog->recordlen = sizeof(struct mce);
357 
358 	/* register character device /dev/mcelog */
359 	err = misc_register(&mce_chrdev_device);
360 	if (err) {
361 		if (err == -EBUSY)
362 			/* Xen dom0 might have registered the device already. */
363 			pr_info("Unable to init device /dev/mcelog, already registered");
364 		else
365 			pr_err("Unable to init device /dev/mcelog (rc: %d)\n", err);
366 
367 		kfree(mcelog);
368 		return err;
369 	}
370 
371 	mce_register_decode_chain(&dev_mcelog_nb);
372 	return 0;
373 }
374 device_initcall_sync(dev_mcelog_init_device);
375