2 * This file is provided under the GPLv2 license.
6 * Copyright(c) 2014 Intel Mobile Communications GmbH
7 * Copyright(c) 2015 Intel Deutschland GmbH
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of version 2 of the GNU General Public License as
11 * published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * The full GNU General Public License is included in this distribution
19 * in the file called COPYING.
21 * Contact Information:
22 * Intel Linux Wireless <ilw@linux.intel.com>
23 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
25 * Author: Johannes Berg <johannes@sipsolutions.net>
27 #include <linux/module.h>
28 #include <linux/device.h>
29 #include <linux/devcoredump.h>
30 #include <linux/list.h>
31 #include <linux/slab.h>
33 #include <linux/workqueue.h>
35 static struct class devcd_class
;
37 /* global disable flag, for security purposes */
38 static bool devcd_disabled
;
40 /* if data isn't read by userspace after 5 minutes then delete it */
41 #define DEVCD_TIMEOUT (HZ * 60 * 5)
44 struct device devcd_dev
;
48 ssize_t (*read
)(char *buffer
, loff_t offset
, size_t count
,
49 void *data
, size_t datalen
);
50 void (*free
)(void *data
);
51 struct delayed_work del_wk
;
52 struct device
*failing_dev
;
55 static struct devcd_entry
*dev_to_devcd(struct device
*dev
)
57 return container_of(dev
, struct devcd_entry
, devcd_dev
);
60 static void devcd_dev_release(struct device
*dev
)
62 struct devcd_entry
*devcd
= dev_to_devcd(dev
);
64 devcd
->free(devcd
->data
);
65 module_put(devcd
->owner
);
68 * this seems racy, but I don't see a notifier or such on
69 * a struct device to know when it goes away?
71 if (devcd
->failing_dev
->kobj
.sd
)
72 sysfs_delete_link(&devcd
->failing_dev
->kobj
, &dev
->kobj
,
75 put_device(devcd
->failing_dev
);
79 static void devcd_del(struct work_struct
*wk
)
81 struct devcd_entry
*devcd
;
83 devcd
= container_of(wk
, struct devcd_entry
, del_wk
.work
);
85 device_del(&devcd
->devcd_dev
);
86 put_device(&devcd
->devcd_dev
);
89 static ssize_t
devcd_data_read(struct file
*filp
, struct kobject
*kobj
,
90 struct bin_attribute
*bin_attr
,
91 char *buffer
, loff_t offset
, size_t count
)
93 struct device
*dev
= kobj_to_dev(kobj
);
94 struct devcd_entry
*devcd
= dev_to_devcd(dev
);
96 return devcd
->read(buffer
, offset
, count
, devcd
->data
, devcd
->datalen
);
99 static ssize_t
devcd_data_write(struct file
*filp
, struct kobject
*kobj
,
100 struct bin_attribute
*bin_attr
,
101 char *buffer
, loff_t offset
, size_t count
)
103 struct device
*dev
= kobj_to_dev(kobj
);
104 struct devcd_entry
*devcd
= dev_to_devcd(dev
);
106 mod_delayed_work(system_wq
, &devcd
->del_wk
, 0);
111 static struct bin_attribute devcd_attr_data
= {
112 .attr
= { .name
= "data", .mode
= S_IRUSR
| S_IWUSR
, },
114 .read
= devcd_data_read
,
115 .write
= devcd_data_write
,
118 static struct bin_attribute
*devcd_dev_bin_attrs
[] = {
119 &devcd_attr_data
, NULL
,
122 static const struct attribute_group devcd_dev_group
= {
123 .bin_attrs
= devcd_dev_bin_attrs
,
126 static const struct attribute_group
*devcd_dev_groups
[] = {
127 &devcd_dev_group
, NULL
,
130 static int devcd_free(struct device
*dev
, void *data
)
132 struct devcd_entry
*devcd
= dev_to_devcd(dev
);
134 flush_delayed_work(&devcd
->del_wk
);
138 static ssize_t
disabled_show(struct class *class, struct class_attribute
*attr
,
141 return sprintf(buf
, "%d\n", devcd_disabled
);
144 static ssize_t
disabled_store(struct class *class, struct class_attribute
*attr
,
145 const char *buf
, size_t count
)
147 long tmp
= simple_strtol(buf
, NULL
, 10);
150 * This essentially makes the attribute write-once, since you can't
151 * go back to not having it disabled. This is intentional, it serves
152 * as a system lockdown feature.
157 devcd_disabled
= true;
159 class_for_each_device(&devcd_class
, NULL
, NULL
, devcd_free
);
164 static struct class_attribute devcd_class_attrs
[] = {
169 static struct class devcd_class
= {
170 .name
= "devcoredump",
171 .owner
= THIS_MODULE
,
172 .dev_release
= devcd_dev_release
,
173 .dev_groups
= devcd_dev_groups
,
174 .class_attrs
= devcd_class_attrs
,
177 static ssize_t
devcd_readv(char *buffer
, loff_t offset
, size_t count
,
178 void *data
, size_t datalen
)
180 if (offset
> datalen
)
183 if (offset
+ count
> datalen
)
184 count
= datalen
- offset
;
187 memcpy(buffer
, ((u8
*)data
) + offset
, count
);
192 static void devcd_freev(void *data
)
198 * dev_coredumpv - create device coredump with vmalloc data
199 * @dev: the struct device for the crashed device
200 * @data: vmalloc data containing the device coredump
201 * @datalen: length of the data
202 * @gfp: allocation flags
204 * This function takes ownership of the vmalloc'ed data and will free
205 * it when it is no longer used. See dev_coredumpm() for more information.
207 void dev_coredumpv(struct device
*dev
, void *data
, size_t datalen
,
210 dev_coredumpm(dev
, NULL
, data
, datalen
, gfp
, devcd_readv
, devcd_freev
);
212 EXPORT_SYMBOL_GPL(dev_coredumpv
);
214 static int devcd_match_failing(struct device
*dev
, const void *failing
)
216 struct devcd_entry
*devcd
= dev_to_devcd(dev
);
218 return devcd
->failing_dev
== failing
;
222 * devcd_free_sgtable - free all the memory of the given scatterlist table
223 * (i.e. both pages and scatterlist instances)
224 * NOTE: if two tables allocated with devcd_alloc_sgtable and then chained
225 * using the sg_chain function then that function should be called only once
226 * on the chained table
227 * @table: pointer to sg_table to free
229 static void devcd_free_sgtable(void *data
)
231 _devcd_free_sgtable(data
);
235 * devcd_read_from_table - copy data from sg_table to a given buffer
236 * and return the number of bytes read
237 * @buffer: the buffer to copy the data to it
238 * @buf_len: the length of the buffer
239 * @data: the scatterlist table to copy from
240 * @offset: start copy from @offset@ bytes from the head of the data
241 * in the given scatterlist
242 * @data_len: the length of the data in the sg_table
244 static ssize_t
devcd_read_from_sgtable(char *buffer
, loff_t offset
,
245 size_t buf_len
, void *data
,
248 struct scatterlist
*table
= data
;
250 if (offset
> data_len
)
253 if (offset
+ buf_len
> data_len
)
254 buf_len
= data_len
- offset
;
255 return sg_pcopy_to_buffer(table
, sg_nents(table
), buffer
, buf_len
,
260 * dev_coredumpm - create device coredump with read/free methods
261 * @dev: the struct device for the crashed device
262 * @owner: the module that contains the read/free functions, use %THIS_MODULE
263 * @data: data cookie for the @read/@free functions
264 * @datalen: length of the data
265 * @gfp: allocation flags
266 * @read: function to read from the given buffer
267 * @free: function to free the given buffer
269 * Creates a new device coredump for the given device. If a previous one hasn't
270 * been read yet, the new coredump is discarded. The data lifetime is determined
271 * by the device coredump framework and when it is no longer needed the @free
272 * function will be called to free the data.
274 void dev_coredumpm(struct device
*dev
, struct module
*owner
,
275 void *data
, size_t datalen
, gfp_t gfp
,
276 ssize_t (*read
)(char *buffer
, loff_t offset
, size_t count
,
277 void *data
, size_t datalen
),
278 void (*free
)(void *data
))
280 static atomic_t devcd_count
= ATOMIC_INIT(0);
281 struct devcd_entry
*devcd
;
282 struct device
*existing
;
287 existing
= class_find_device(&devcd_class
, NULL
, dev
,
288 devcd_match_failing
);
290 put_device(existing
);
294 if (!try_module_get(owner
))
297 devcd
= kzalloc(sizeof(*devcd
), gfp
);
301 devcd
->owner
= owner
;
303 devcd
->datalen
= datalen
;
306 devcd
->failing_dev
= get_device(dev
);
308 device_initialize(&devcd
->devcd_dev
);
310 dev_set_name(&devcd
->devcd_dev
, "devcd%d",
311 atomic_inc_return(&devcd_count
));
312 devcd
->devcd_dev
.class = &devcd_class
;
314 if (device_add(&devcd
->devcd_dev
))
317 if (sysfs_create_link(&devcd
->devcd_dev
.kobj
, &dev
->kobj
,
319 /* nothing - symlink will be missing */;
321 if (sysfs_create_link(&dev
->kobj
, &devcd
->devcd_dev
.kobj
,
323 /* nothing - symlink will be missing */;
325 INIT_DELAYED_WORK(&devcd
->del_wk
, devcd_del
);
326 schedule_delayed_work(&devcd
->del_wk
, DEVCD_TIMEOUT
);
330 put_device(&devcd
->devcd_dev
);
336 EXPORT_SYMBOL_GPL(dev_coredumpm
);
339 * dev_coredumpmsg - create device coredump that uses scatterlist as data
341 * @dev: the struct device for the crashed device
342 * @table: the dump data
343 * @datalen: length of the data
344 * @gfp: allocation flags
346 * Creates a new device coredump for the given device. If a previous one hasn't
347 * been read yet, the new coredump is discarded. The data lifetime is determined
348 * by the device coredump framework and when it is no longer needed
349 * it will free the data.
351 void dev_coredumpsg(struct device
*dev
, struct scatterlist
*table
,
352 size_t datalen
, gfp_t gfp
)
354 dev_coredumpm(dev
, NULL
, table
, datalen
, gfp
, devcd_read_from_sgtable
,
357 EXPORT_SYMBOL_GPL(dev_coredumpsg
);
359 static int __init
devcoredump_init(void)
361 return class_register(&devcd_class
);
363 __initcall(devcoredump_init
);
365 static void __exit
devcoredump_exit(void)
367 class_for_each_device(&devcd_class
, NULL
, NULL
, devcd_free
);
368 class_unregister(&devcd_class
);
370 __exitcall(devcoredump_exit
);