2 * This file is provided under the GPLv2 license.
6 * Copyright(c) 2014 Intel Mobile Communications GmbH
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of version 2 of the GNU General Public License as
10 * published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * The full GNU General Public License is included in this distribution
18 * in the file called COPYING.
20 * Contact Information:
21 * Intel Linux Wireless <ilw@linux.intel.com>
22 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
24 * Author: Johannes Berg <johannes@sipsolutions.net>
26 #include <linux/module.h>
27 #include <linux/device.h>
28 #include <linux/devcoredump.h>
29 #include <linux/list.h>
30 #include <linux/slab.h>
32 #include <linux/workqueue.h>
34 /* if data isn't read by userspace after 5 minutes then delete it */
35 #define DEVCD_TIMEOUT (HZ * 60 * 5)
38 struct device devcd_dev
;
42 ssize_t (*read
)(char *buffer
, loff_t offset
, size_t count
,
43 const void *data
, size_t datalen
);
44 void (*free
)(const void *data
);
45 struct delayed_work del_wk
;
46 struct device
*failing_dev
;
49 static struct devcd_entry
*dev_to_devcd(struct device
*dev
)
51 return container_of(dev
, struct devcd_entry
, devcd_dev
);
54 static void devcd_dev_release(struct device
*dev
)
56 struct devcd_entry
*devcd
= dev_to_devcd(dev
);
58 devcd
->free(devcd
->data
);
59 module_put(devcd
->owner
);
62 * this seems racy, but I don't see a notifier or such on
63 * a struct device to know when it goes away?
65 if (devcd
->failing_dev
->kobj
.sd
)
66 sysfs_delete_link(&devcd
->failing_dev
->kobj
, &dev
->kobj
,
69 put_device(devcd
->failing_dev
);
73 static void devcd_del(struct work_struct
*wk
)
75 struct devcd_entry
*devcd
;
77 devcd
= container_of(wk
, struct devcd_entry
, del_wk
.work
);
79 device_del(&devcd
->devcd_dev
);
80 put_device(&devcd
->devcd_dev
);
83 static ssize_t
devcd_data_read(struct file
*filp
, struct kobject
*kobj
,
84 struct bin_attribute
*bin_attr
,
85 char *buffer
, loff_t offset
, size_t count
)
87 struct device
*dev
= kobj_to_dev(kobj
);
88 struct devcd_entry
*devcd
= dev_to_devcd(dev
);
90 return devcd
->read(buffer
, offset
, count
, devcd
->data
, devcd
->datalen
);
93 static ssize_t
devcd_data_write(struct file
*filp
, struct kobject
*kobj
,
94 struct bin_attribute
*bin_attr
,
95 char *buffer
, loff_t offset
, size_t count
)
97 struct device
*dev
= kobj_to_dev(kobj
);
98 struct devcd_entry
*devcd
= dev_to_devcd(dev
);
100 mod_delayed_work(system_wq
, &devcd
->del_wk
, 0);
105 static struct bin_attribute devcd_attr_data
= {
106 .attr
= { .name
= "data", .mode
= S_IRUSR
| S_IWUSR
, },
108 .read
= devcd_data_read
,
109 .write
= devcd_data_write
,
112 static struct bin_attribute
*devcd_dev_bin_attrs
[] = {
113 &devcd_attr_data
, NULL
,
116 static const struct attribute_group devcd_dev_group
= {
117 .bin_attrs
= devcd_dev_bin_attrs
,
120 static const struct attribute_group
*devcd_dev_groups
[] = {
121 &devcd_dev_group
, NULL
,
124 static struct class devcd_class
= {
125 .name
= "devcoredump",
126 .owner
= THIS_MODULE
,
127 .dev_release
= devcd_dev_release
,
128 .dev_groups
= devcd_dev_groups
,
131 static ssize_t
devcd_readv(char *buffer
, loff_t offset
, size_t count
,
132 const void *data
, size_t datalen
)
134 if (offset
> datalen
)
137 if (offset
+ count
> datalen
)
138 count
= datalen
- offset
;
141 memcpy(buffer
, ((u8
*)data
) + offset
, count
);
147 * dev_coredumpv - create device coredump with vmalloc data
148 * @dev: the struct device for the crashed device
149 * @data: vmalloc data containing the device coredump
150 * @datalen: length of the data
151 * @gfp: allocation flags
153 * This function takes ownership of the vmalloc'ed data and will free
154 * it when it is no longer used. See dev_coredumpm() for more information.
156 void dev_coredumpv(struct device
*dev
, const void *data
, size_t datalen
,
159 dev_coredumpm(dev
, NULL
, data
, datalen
, gfp
, devcd_readv
, vfree
);
161 EXPORT_SYMBOL_GPL(dev_coredumpv
);
163 static int devcd_match_failing(struct device
*dev
, const void *failing
)
165 struct devcd_entry
*devcd
= dev_to_devcd(dev
);
167 return devcd
->failing_dev
== failing
;
171 * dev_coredumpm - create device coredump with read/free methods
172 * @dev: the struct device for the crashed device
173 * @owner: the module that contains the read/free functions, use %THIS_MODULE
174 * @data: data cookie for the @read/@free functions
175 * @datalen: length of the data
176 * @gfp: allocation flags
177 * @read: function to read from the given buffer
178 * @free: function to free the given buffer
180 * Creates a new device coredump for the given device. If a previous one hasn't
181 * been read yet, the new coredump is discarded. The data lifetime is determined
182 * by the device coredump framework and when it is no longer needed the @free
183 * function will be called to free the data.
185 void dev_coredumpm(struct device
*dev
, struct module
*owner
,
186 const void *data
, size_t datalen
, gfp_t gfp
,
187 ssize_t (*read
)(char *buffer
, loff_t offset
, size_t count
,
188 const void *data
, size_t datalen
),
189 void (*free
)(const void *data
))
191 static atomic_t devcd_count
= ATOMIC_INIT(0);
192 struct devcd_entry
*devcd
;
193 struct device
*existing
;
195 existing
= class_find_device(&devcd_class
, NULL
, dev
,
196 devcd_match_failing
);
198 put_device(existing
);
202 if (!try_module_get(owner
))
205 devcd
= kzalloc(sizeof(*devcd
), gfp
);
209 devcd
->owner
= owner
;
211 devcd
->datalen
= datalen
;
214 devcd
->failing_dev
= get_device(dev
);
216 device_initialize(&devcd
->devcd_dev
);
218 dev_set_name(&devcd
->devcd_dev
, "devcd%d",
219 atomic_inc_return(&devcd_count
));
220 devcd
->devcd_dev
.class = &devcd_class
;
222 if (device_add(&devcd
->devcd_dev
))
225 if (sysfs_create_link(&devcd
->devcd_dev
.kobj
, &dev
->kobj
,
227 /* nothing - symlink will be missing */;
229 if (sysfs_create_link(&dev
->kobj
, &devcd
->devcd_dev
.kobj
,
231 /* nothing - symlink will be missing */;
233 INIT_DELAYED_WORK(&devcd
->del_wk
, devcd_del
);
234 schedule_delayed_work(&devcd
->del_wk
, DEVCD_TIMEOUT
);
238 put_device(&devcd
->devcd_dev
);
244 EXPORT_SYMBOL_GPL(dev_coredumpm
);
246 static int __init
devcoredump_init(void)
248 return class_register(&devcd_class
);
250 __initcall(devcoredump_init
);
252 static int devcd_free(struct device
*dev
, void *data
)
254 struct devcd_entry
*devcd
= dev_to_devcd(dev
);
256 flush_delayed_work(&devcd
->del_wk
);
260 static void __exit
devcoredump_exit(void)
262 class_for_each_device(&devcd_class
, NULL
, NULL
, devcd_free
);
263 class_unregister(&devcd_class
);
265 __exitcall(devcoredump_exit
);