blob: 3133684a87547e23714a0b3bd4298acf38bdef18 [file] [log] [blame]
Greg Kroah-Hartman989d42e2017-11-07 17:30:07 +01001// SPDX-License-Identifier: GPL-2.0
Johannes Berg833c9542014-09-12 09:01:56 +02002/*
3 * This file is provided under the GPLv2 license.
4 *
5 * GPL LICENSE SUMMARY
6 *
7 * Copyright(c) 2014 Intel Mobile Communications GmbH
Aviya Erenfeld52256632016-04-14 11:59:31 +02008 * Copyright(c) 2015 Intel Deutschland GmbH
Johannes Berg833c9542014-09-12 09:01:56 +02009 *
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 <ilw@linux.intel.com>
24 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
25 *
26 * Author: Johannes Berg <johannes@sipsolutions.net>
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
Johannes Bergd4533322014-11-13 22:16:29 +010036static struct class devcd_class;
37
38/* global disable flag, for security purposes */
39static bool devcd_disabled;
40
Johannes Berg833c9542014-09-12 09:01:56 +020041/* if data isn't read by userspace after 5 minutes then delete it */
42#define DEVCD_TIMEOUT (HZ * 60 * 5)
43
44struct devcd_entry {
45 struct device devcd_dev;
Aviya Erenfeld52256632016-04-14 11:59:31 +020046 void *data;
Johannes Berg833c9542014-09-12 09:01:56 +020047 size_t datalen;
48 struct module *owner;
49 ssize_t (*read)(char *buffer, loff_t offset, size_t count,
Aviya Erenfeld52256632016-04-14 11:59:31 +020050 void *data, size_t datalen);
51 void (*free)(void *data);
Johannes Berg833c9542014-09-12 09:01:56 +020052 struct delayed_work del_wk;
53 struct device *failing_dev;
54};
55
56static struct devcd_entry *dev_to_devcd(struct device *dev)
57{
58 return container_of(dev, struct devcd_entry, devcd_dev);
59}
60
61static 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
80static 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
90static 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
100static 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
112static 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
119static struct bin_attribute *devcd_dev_bin_attrs[] = {
120 &devcd_attr_data, NULL,
121};
122
123static const struct attribute_group devcd_dev_group = {
124 .bin_attrs = devcd_dev_bin_attrs,
125};
126
127static const struct attribute_group *devcd_dev_groups[] = {
128 &devcd_dev_group, NULL,
129};
130
Johannes Bergd4533322014-11-13 22:16:29 +0100131static 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
139static 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
145static 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}
Greg Kroah-Hartmanf76d2522016-11-28 16:41:58 +0100164static CLASS_ATTR_RW(disabled);
Johannes Bergd4533322014-11-13 22:16:29 +0100165
Greg Kroah-Hartmanf76d2522016-11-28 16:41:58 +0100166static struct attribute *devcd_class_attrs[] = {
167 &class_attr_disabled.attr,
168 NULL,
Johannes Bergd4533322014-11-13 22:16:29 +0100169};
Greg Kroah-Hartmanf76d2522016-11-28 16:41:58 +0100170ATTRIBUTE_GROUPS(devcd_class);
Johannes Bergd4533322014-11-13 22:16:29 +0100171
Johannes Berg833c9542014-09-12 09:01:56 +0200172static struct class devcd_class = {
173 .name = "devcoredump",
174 .owner = THIS_MODULE,
175 .dev_release = devcd_dev_release,
176 .dev_groups = devcd_dev_groups,
Greg Kroah-Hartmanf76d2522016-11-28 16:41:58 +0100177 .class_groups = devcd_class_groups,
Johannes Berg833c9542014-09-12 09:01:56 +0200178};
179
180static ssize_t devcd_readv(char *buffer, loff_t offset, size_t count,
Aviya Erenfeld52256632016-04-14 11:59:31 +0200181 void *data, size_t datalen)
Johannes Berg833c9542014-09-12 09:01:56 +0200182{
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
Aviya Erenfeld52256632016-04-14 11:59:31 +0200195static void devcd_freev(void *data)
196{
197 vfree(data);
198}
199
Johannes Berg833c9542014-09-12 09:01:56 +0200200/**
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 */
Aviya Erenfeld52256632016-04-14 11:59:31 +0200210void dev_coredumpv(struct device *dev, void *data, size_t datalen,
Johannes Berg833c9542014-09-12 09:01:56 +0200211 gfp_t gfp)
212{
Aviya Erenfeld52256632016-04-14 11:59:31 +0200213 dev_coredumpm(dev, NULL, data, datalen, gfp, devcd_readv, devcd_freev);
Johannes Berg833c9542014-09-12 09:01:56 +0200214}
215EXPORT_SYMBOL_GPL(dev_coredumpv);
216
217static 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/**
Aviya Erenfeld52256632016-04-14 11:59:31 +0200225 * 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 */
232static 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 */
247static 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/**
Johannes Berg833c9542014-09-12 09:01:56 +0200263 * 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 */
277void dev_coredumpm(struct device *dev, struct module *owner,
Aviya Erenfeld52256632016-04-14 11:59:31 +0200278 void *data, size_t datalen, gfp_t gfp,
Johannes Berg833c9542014-09-12 09:01:56 +0200279 ssize_t (*read)(char *buffer, loff_t offset, size_t count,
Aviya Erenfeld52256632016-04-14 11:59:31 +0200280 void *data, size_t datalen),
281 void (*free)(void *data))
Johannes Berg833c9542014-09-12 09:01:56 +0200282{
283 static atomic_t devcd_count = ATOMIC_INIT(0);
284 struct devcd_entry *devcd;
285 struct device *existing;
286
Johannes Bergd4533322014-11-13 22:16:29 +0100287 if (devcd_disabled)
288 goto free;
289
Johannes Berg833c9542014-09-12 09:01:56 +0200290 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}
339EXPORT_SYMBOL_GPL(dev_coredumpm);
340
Aviya Erenfeld52256632016-04-14 11:59:31 +0200341/**
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 */
354void 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}
360EXPORT_SYMBOL_GPL(dev_coredumpsg);
361
Johannes Berg833c9542014-09-12 09:01:56 +0200362static int __init devcoredump_init(void)
363{
364 return class_register(&devcd_class);
365}
366__initcall(devcoredump_init);
367
Johannes Berg833c9542014-09-12 09:01:56 +0200368static 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);