xref: /linux-6.15/drivers/base/devcoredump.c (revision 989d42e8)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * This file is provided under the GPLv2 license.
4  *
5  * GPL LICENSE SUMMARY
6  *
7  * Copyright(c) 2014 Intel Mobile Communications GmbH
8  * Copyright(c) 2015 Intel Deutschland GmbH
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of version 2 of the GNU General Public License as
12  * published by the Free Software Foundation.
13  *
14  * This program is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * General Public License for more details.
18  *
19  * The full GNU General Public License is included in this distribution
20  * in the file called COPYING.
21  *
22  * Contact Information:
23  *  Intel Linux Wireless <[email protected]>
24  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
25  *
26  * Author: Johannes Berg <[email protected]>
27  */
28 #include <linux/module.h>
29 #include <linux/device.h>
30 #include <linux/devcoredump.h>
31 #include <linux/list.h>
32 #include <linux/slab.h>
33 #include <linux/fs.h>
34 #include <linux/workqueue.h>
35 
36 static struct class devcd_class;
37 
38 /* global disable flag, for security purposes */
39 static bool devcd_disabled;
40 
41 /* if data isn't read by userspace after 5 minutes then delete it */
42 #define DEVCD_TIMEOUT	(HZ * 60 * 5)
43 
44 struct devcd_entry {
45 	struct device devcd_dev;
46 	void *data;
47 	size_t datalen;
48 	struct module *owner;
49 	ssize_t (*read)(char *buffer, loff_t offset, size_t count,
50 			void *data, size_t datalen);
51 	void (*free)(void *data);
52 	struct delayed_work del_wk;
53 	struct device *failing_dev;
54 };
55 
56 static struct devcd_entry *dev_to_devcd(struct device *dev)
57 {
58 	return container_of(dev, struct devcd_entry, devcd_dev);
59 }
60 
61 static void devcd_dev_release(struct device *dev)
62 {
63 	struct devcd_entry *devcd = dev_to_devcd(dev);
64 
65 	devcd->free(devcd->data);
66 	module_put(devcd->owner);
67 
68 	/*
69 	 * this seems racy, but I don't see a notifier or such on
70 	 * a struct device to know when it goes away?
71 	 */
72 	if (devcd->failing_dev->kobj.sd)
73 		sysfs_delete_link(&devcd->failing_dev->kobj, &dev->kobj,
74 				  "devcoredump");
75 
76 	put_device(devcd->failing_dev);
77 	kfree(devcd);
78 }
79 
80 static void devcd_del(struct work_struct *wk)
81 {
82 	struct devcd_entry *devcd;
83 
84 	devcd = container_of(wk, struct devcd_entry, del_wk.work);
85 
86 	device_del(&devcd->devcd_dev);
87 	put_device(&devcd->devcd_dev);
88 }
89 
90 static ssize_t devcd_data_read(struct file *filp, struct kobject *kobj,
91 			       struct bin_attribute *bin_attr,
92 			       char *buffer, loff_t offset, size_t count)
93 {
94 	struct device *dev = kobj_to_dev(kobj);
95 	struct devcd_entry *devcd = dev_to_devcd(dev);
96 
97 	return devcd->read(buffer, offset, count, devcd->data, devcd->datalen);
98 }
99 
100 static ssize_t devcd_data_write(struct file *filp, struct kobject *kobj,
101 				struct bin_attribute *bin_attr,
102 				char *buffer, loff_t offset, size_t count)
103 {
104 	struct device *dev = kobj_to_dev(kobj);
105 	struct devcd_entry *devcd = dev_to_devcd(dev);
106 
107 	mod_delayed_work(system_wq, &devcd->del_wk, 0);
108 
109 	return count;
110 }
111 
112 static struct bin_attribute devcd_attr_data = {
113 	.attr = { .name = "data", .mode = S_IRUSR | S_IWUSR, },
114 	.size = 0,
115 	.read = devcd_data_read,
116 	.write = devcd_data_write,
117 };
118 
119 static struct bin_attribute *devcd_dev_bin_attrs[] = {
120 	&devcd_attr_data, NULL,
121 };
122 
123 static const struct attribute_group devcd_dev_group = {
124 	.bin_attrs = devcd_dev_bin_attrs,
125 };
126 
127 static const struct attribute_group *devcd_dev_groups[] = {
128 	&devcd_dev_group, NULL,
129 };
130 
131 static int devcd_free(struct device *dev, void *data)
132 {
133 	struct devcd_entry *devcd = dev_to_devcd(dev);
134 
135 	flush_delayed_work(&devcd->del_wk);
136 	return 0;
137 }
138 
139 static ssize_t disabled_show(struct class *class, struct class_attribute *attr,
140 			     char *buf)
141 {
142 	return sprintf(buf, "%d\n", devcd_disabled);
143 }
144 
145 static ssize_t disabled_store(struct class *class, struct class_attribute *attr,
146 			      const char *buf, size_t count)
147 {
148 	long tmp = simple_strtol(buf, NULL, 10);
149 
150 	/*
151 	 * This essentially makes the attribute write-once, since you can't
152 	 * go back to not having it disabled. This is intentional, it serves
153 	 * as a system lockdown feature.
154 	 */
155 	if (tmp != 1)
156 		return -EINVAL;
157 
158 	devcd_disabled = true;
159 
160 	class_for_each_device(&devcd_class, NULL, NULL, devcd_free);
161 
162 	return count;
163 }
164 static CLASS_ATTR_RW(disabled);
165 
166 static struct attribute *devcd_class_attrs[] = {
167 	&class_attr_disabled.attr,
168 	NULL,
169 };
170 ATTRIBUTE_GROUPS(devcd_class);
171 
172 static struct class devcd_class = {
173 	.name		= "devcoredump",
174 	.owner		= THIS_MODULE,
175 	.dev_release	= devcd_dev_release,
176 	.dev_groups	= devcd_dev_groups,
177 	.class_groups	= devcd_class_groups,
178 };
179 
180 static ssize_t devcd_readv(char *buffer, loff_t offset, size_t count,
181 			   void *data, size_t datalen)
182 {
183 	if (offset > datalen)
184 		return -EINVAL;
185 
186 	if (offset + count > datalen)
187 		count = datalen - offset;
188 
189 	if (count)
190 		memcpy(buffer, ((u8 *)data) + offset, count);
191 
192 	return count;
193 }
194 
195 static void devcd_freev(void *data)
196 {
197 	vfree(data);
198 }
199 
200 /**
201  * dev_coredumpv - create device coredump with vmalloc data
202  * @dev: the struct device for the crashed device
203  * @data: vmalloc data containing the device coredump
204  * @datalen: length of the data
205  * @gfp: allocation flags
206  *
207  * This function takes ownership of the vmalloc'ed data and will free
208  * it when it is no longer used. See dev_coredumpm() for more information.
209  */
210 void dev_coredumpv(struct device *dev, void *data, size_t datalen,
211 		   gfp_t gfp)
212 {
213 	dev_coredumpm(dev, NULL, data, datalen, gfp, devcd_readv, devcd_freev);
214 }
215 EXPORT_SYMBOL_GPL(dev_coredumpv);
216 
217 static int devcd_match_failing(struct device *dev, const void *failing)
218 {
219 	struct devcd_entry *devcd = dev_to_devcd(dev);
220 
221 	return devcd->failing_dev == failing;
222 }
223 
224 /**
225  * devcd_free_sgtable - free all the memory of the given scatterlist table
226  * (i.e. both pages and scatterlist instances)
227  * NOTE: if two tables allocated with devcd_alloc_sgtable and then chained
228  * using the sg_chain function then that function should be called only once
229  * on the chained table
230  * @table: pointer to sg_table to free
231  */
232 static void devcd_free_sgtable(void *data)
233 {
234 	_devcd_free_sgtable(data);
235 }
236 
237 /**
238  * devcd_read_from_table - copy data from sg_table to a given buffer
239  * and return the number of bytes read
240  * @buffer: the buffer to copy the data to it
241  * @buf_len: the length of the buffer
242  * @data: the scatterlist table to copy from
243  * @offset: start copy from @offset@ bytes from the head of the data
244  *	in the given scatterlist
245  * @data_len: the length of the data in the sg_table
246  */
247 static ssize_t devcd_read_from_sgtable(char *buffer, loff_t offset,
248 				       size_t buf_len, void *data,
249 				       size_t data_len)
250 {
251 	struct scatterlist *table = data;
252 
253 	if (offset > data_len)
254 		return -EINVAL;
255 
256 	if (offset + buf_len > data_len)
257 		buf_len = data_len - offset;
258 	return sg_pcopy_to_buffer(table, sg_nents(table), buffer, buf_len,
259 				  offset);
260 }
261 
262 /**
263  * dev_coredumpm - create device coredump with read/free methods
264  * @dev: the struct device for the crashed device
265  * @owner: the module that contains the read/free functions, use %THIS_MODULE
266  * @data: data cookie for the @read/@free functions
267  * @datalen: length of the data
268  * @gfp: allocation flags
269  * @read: function to read from the given buffer
270  * @free: function to free the given buffer
271  *
272  * Creates a new device coredump for the given device. If a previous one hasn't
273  * been read yet, the new coredump is discarded. The data lifetime is determined
274  * by the device coredump framework and when it is no longer needed the @free
275  * function will be called to free the data.
276  */
277 void dev_coredumpm(struct device *dev, struct module *owner,
278 		   void *data, size_t datalen, gfp_t gfp,
279 		   ssize_t (*read)(char *buffer, loff_t offset, size_t count,
280 				   void *data, size_t datalen),
281 		   void (*free)(void *data))
282 {
283 	static atomic_t devcd_count = ATOMIC_INIT(0);
284 	struct devcd_entry *devcd;
285 	struct device *existing;
286 
287 	if (devcd_disabled)
288 		goto free;
289 
290 	existing = class_find_device(&devcd_class, NULL, dev,
291 				     devcd_match_failing);
292 	if (existing) {
293 		put_device(existing);
294 		goto free;
295 	}
296 
297 	if (!try_module_get(owner))
298 		goto free;
299 
300 	devcd = kzalloc(sizeof(*devcd), gfp);
301 	if (!devcd)
302 		goto put_module;
303 
304 	devcd->owner = owner;
305 	devcd->data = data;
306 	devcd->datalen = datalen;
307 	devcd->read = read;
308 	devcd->free = free;
309 	devcd->failing_dev = get_device(dev);
310 
311 	device_initialize(&devcd->devcd_dev);
312 
313 	dev_set_name(&devcd->devcd_dev, "devcd%d",
314 		     atomic_inc_return(&devcd_count));
315 	devcd->devcd_dev.class = &devcd_class;
316 
317 	if (device_add(&devcd->devcd_dev))
318 		goto put_device;
319 
320 	if (sysfs_create_link(&devcd->devcd_dev.kobj, &dev->kobj,
321 			      "failing_device"))
322 		/* nothing - symlink will be missing */;
323 
324 	if (sysfs_create_link(&dev->kobj, &devcd->devcd_dev.kobj,
325 			      "devcoredump"))
326 		/* nothing - symlink will be missing */;
327 
328 	INIT_DELAYED_WORK(&devcd->del_wk, devcd_del);
329 	schedule_delayed_work(&devcd->del_wk, DEVCD_TIMEOUT);
330 
331 	return;
332  put_device:
333 	put_device(&devcd->devcd_dev);
334  put_module:
335 	module_put(owner);
336  free:
337 	free(data);
338 }
339 EXPORT_SYMBOL_GPL(dev_coredumpm);
340 
341 /**
342  * dev_coredumpmsg - create device coredump that uses scatterlist as data
343  * parameter
344  * @dev: the struct device for the crashed device
345  * @table: the dump data
346  * @datalen: length of the data
347  * @gfp: allocation flags
348  *
349  * Creates a new device coredump for the given device. If a previous one hasn't
350  * been read yet, the new coredump is discarded. The data lifetime is determined
351  * by the device coredump framework and when it is no longer needed
352  * it will free the data.
353  */
354 void dev_coredumpsg(struct device *dev, struct scatterlist *table,
355 		    size_t datalen, gfp_t gfp)
356 {
357 	dev_coredumpm(dev, NULL, table, datalen, gfp, devcd_read_from_sgtable,
358 		      devcd_free_sgtable);
359 }
360 EXPORT_SYMBOL_GPL(dev_coredumpsg);
361 
362 static int __init devcoredump_init(void)
363 {
364 	return class_register(&devcd_class);
365 }
366 __initcall(devcoredump_init);
367 
368 static void __exit devcoredump_exit(void)
369 {
370 	class_for_each_device(&devcd_class, NULL, NULL, devcd_free);
371 	class_unregister(&devcd_class);
372 }
373 __exitcall(devcoredump_exit);
374