1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright(c) 2014 Intel Mobile Communications GmbH
4 * Copyright(c) 2015 Intel Deutschland GmbH
6 * Author: Johannes Berg <johannes@sipsolutions.net>
8 #include <linux/module.h>
9 #include <linux/device.h>
10 #include <linux/devcoredump.h>
11 #include <linux/list.h>
12 #include <linux/slab.h>
14 #include <linux/workqueue.h>
16 static struct class devcd_class;
18 /* global disable flag, for security purposes */
19 static bool devcd_disabled;
21 /* if data isn't read by userspace after 5 minutes then delete it */
22 #define DEVCD_TIMEOUT (HZ * 60 * 5)
25 struct device devcd_dev;
29 * Here, mutex is required to serialize the calls to del_wk work between
30 * user/kernel space which happens when devcd is added with device_add()
31 * and that sends uevent to user space. User space reads the uevents,
32 * and calls to devcd_data_write() which try to modify the work which is
33 * not even initialized/queued from devcoredump.
39 * dev_coredump() uevent sent to user space
40 * device_add() ======================> user space process Y reads the
41 * uevents writes to devcd fd
42 * which results into writes to
46 * try_to_grab_pending()
50 * schedule_delayed_work()
53 * Also, mutex alone would not be enough to avoid scheduling of
54 * del_wk work after it get flush from a call to devcd_free()
59 * mutex_lock() devcd_data_write()
60 * flush_delayed_work()
65 * So, delete_work flag is required.
70 ssize_t (*read)(char *buffer, loff_t offset, size_t count,
71 void *data, size_t datalen);
72 void (*free)(void *data);
73 struct delayed_work del_wk;
74 struct device *failing_dev;
77 static struct devcd_entry *dev_to_devcd(struct device *dev)
79 return container_of(dev, struct devcd_entry, devcd_dev);
82 static void devcd_dev_release(struct device *dev)
84 struct devcd_entry *devcd = dev_to_devcd(dev);
86 devcd->free(devcd->data);
87 module_put(devcd->owner);
90 * this seems racy, but I don't see a notifier or such on
91 * a struct device to know when it goes away?
93 if (devcd->failing_dev->kobj.sd)
94 sysfs_delete_link(&devcd->failing_dev->kobj, &dev->kobj,
97 put_device(devcd->failing_dev);
101 static void devcd_del(struct work_struct *wk)
103 struct devcd_entry *devcd;
105 devcd = container_of(wk, struct devcd_entry, del_wk.work);
107 device_del(&devcd->devcd_dev);
108 put_device(&devcd->devcd_dev);
111 static ssize_t devcd_data_read(struct file *filp, struct kobject *kobj,
112 struct bin_attribute *bin_attr,
113 char *buffer, loff_t offset, size_t count)
115 struct device *dev = kobj_to_dev(kobj);
116 struct devcd_entry *devcd = dev_to_devcd(dev);
118 return devcd->read(buffer, offset, count, devcd->data, devcd->datalen);
121 static ssize_t devcd_data_write(struct file *filp, struct kobject *kobj,
122 struct bin_attribute *bin_attr,
123 char *buffer, loff_t offset, size_t count)
125 struct device *dev = kobj_to_dev(kobj);
126 struct devcd_entry *devcd = dev_to_devcd(dev);
128 mutex_lock(&devcd->mutex);
129 if (!devcd->delete_work) {
130 devcd->delete_work = true;
131 mod_delayed_work(system_wq, &devcd->del_wk, 0);
133 mutex_unlock(&devcd->mutex);
138 static struct bin_attribute devcd_attr_data = {
139 .attr = { .name = "data", .mode = S_IRUSR | S_IWUSR, },
141 .read = devcd_data_read,
142 .write = devcd_data_write,
145 static struct bin_attribute *devcd_dev_bin_attrs[] = {
146 &devcd_attr_data, NULL,
149 static const struct attribute_group devcd_dev_group = {
150 .bin_attrs = devcd_dev_bin_attrs,
153 static const struct attribute_group *devcd_dev_groups[] = {
154 &devcd_dev_group, NULL,
157 static int devcd_free(struct device *dev, void *data)
159 struct devcd_entry *devcd = dev_to_devcd(dev);
161 mutex_lock(&devcd->mutex);
162 if (!devcd->delete_work)
163 devcd->delete_work = true;
165 flush_delayed_work(&devcd->del_wk);
166 mutex_unlock(&devcd->mutex);
170 static ssize_t disabled_show(const struct class *class, const struct class_attribute *attr,
173 return sysfs_emit(buf, "%d\n", devcd_disabled);
178 * disabled_store() worker()
179 * class_for_each_device(&devcd_class,
180 * NULL, NULL, devcd_free)
183 * while ((dev = class_dev_iter_next(&iter))
186 * put_device() <- last reference
187 * error = fn(dev, data) devcd_dev_release()
188 * devcd_free(dev, data) kfree(devcd)
189 * mutex_lock(&devcd->mutex);
192 * In the above diagram, It looks like disabled_store() would be racing with parallely
193 * running devcd_del() and result in memory abort while acquiring devcd->mutex which
194 * is called after kfree of devcd memory after dropping its last reference with
195 * put_device(). However, this will not happens as fn(dev, data) runs
196 * with its own reference to device via klist_node so it is not its last reference.
197 * so, above situation would not occur.
200 static ssize_t disabled_store(const struct class *class, const struct class_attribute *attr,
201 const char *buf, size_t count)
203 long tmp = simple_strtol(buf, NULL, 10);
206 * This essentially makes the attribute write-once, since you can't
207 * go back to not having it disabled. This is intentional, it serves
208 * as a system lockdown feature.
213 devcd_disabled = true;
215 class_for_each_device(&devcd_class, NULL, NULL, devcd_free);
219 static CLASS_ATTR_RW(disabled);
221 static struct attribute *devcd_class_attrs[] = {
222 &class_attr_disabled.attr,
225 ATTRIBUTE_GROUPS(devcd_class);
227 static struct class devcd_class = {
228 .name = "devcoredump",
229 .dev_release = devcd_dev_release,
230 .dev_groups = devcd_dev_groups,
231 .class_groups = devcd_class_groups,
234 static ssize_t devcd_readv(char *buffer, loff_t offset, size_t count,
235 void *data, size_t datalen)
237 return memory_read_from_buffer(buffer, count, &offset, data, datalen);
240 static void devcd_freev(void *data)
246 * dev_coredumpv - create device coredump with vmalloc data
247 * @dev: the struct device for the crashed device
248 * @data: vmalloc data containing the device coredump
249 * @datalen: length of the data
250 * @gfp: allocation flags
252 * This function takes ownership of the vmalloc'ed data and will free
253 * it when it is no longer used. See dev_coredumpm() for more information.
255 void dev_coredumpv(struct device *dev, void *data, size_t datalen,
258 dev_coredumpm(dev, NULL, data, datalen, gfp, devcd_readv, devcd_freev);
260 EXPORT_SYMBOL_GPL(dev_coredumpv);
262 static int devcd_match_failing(struct device *dev, const void *failing)
264 struct devcd_entry *devcd = dev_to_devcd(dev);
266 return devcd->failing_dev == failing;
270 * devcd_free_sgtable - free all the memory of the given scatterlist table
271 * (i.e. both pages and scatterlist instances)
272 * NOTE: if two tables allocated with devcd_alloc_sgtable and then chained
273 * using the sg_chain function then that function should be called only once
274 * on the chained table
275 * @data: pointer to sg_table to free
277 static void devcd_free_sgtable(void *data)
279 _devcd_free_sgtable(data);
283 * devcd_read_from_sgtable - copy data from sg_table to a given buffer
284 * and return the number of bytes read
285 * @buffer: the buffer to copy the data to it
286 * @buf_len: the length of the buffer
287 * @data: the scatterlist table to copy from
288 * @offset: start copy from @offset@ bytes from the head of the data
289 * in the given scatterlist
290 * @data_len: the length of the data in the sg_table
292 static ssize_t devcd_read_from_sgtable(char *buffer, loff_t offset,
293 size_t buf_len, void *data,
296 struct scatterlist *table = data;
298 if (offset > data_len)
301 if (offset + buf_len > data_len)
302 buf_len = data_len - offset;
303 return sg_pcopy_to_buffer(table, sg_nents(table), buffer, buf_len,
308 * dev_coredumpm - create device coredump with read/free methods
309 * @dev: the struct device for the crashed device
310 * @owner: the module that contains the read/free functions, use %THIS_MODULE
311 * @data: data cookie for the @read/@free functions
312 * @datalen: length of the data
313 * @gfp: allocation flags
314 * @read: function to read from the given buffer
315 * @free: function to free the given buffer
317 * Creates a new device coredump for the given device. If a previous one hasn't
318 * been read yet, the new coredump is discarded. The data lifetime is determined
319 * by the device coredump framework and when it is no longer needed the @free
320 * function will be called to free the data.
322 void dev_coredumpm(struct device *dev, struct module *owner,
323 void *data, size_t datalen, gfp_t gfp,
324 ssize_t (*read)(char *buffer, loff_t offset, size_t count,
325 void *data, size_t datalen),
326 void (*free)(void *data))
328 static atomic_t devcd_count = ATOMIC_INIT(0);
329 struct devcd_entry *devcd;
330 struct device *existing;
335 existing = class_find_device(&devcd_class, NULL, dev,
336 devcd_match_failing);
338 put_device(existing);
342 if (!try_module_get(owner))
345 devcd = kzalloc(sizeof(*devcd), gfp);
349 devcd->owner = owner;
351 devcd->datalen = datalen;
354 devcd->failing_dev = get_device(dev);
355 devcd->delete_work = false;
357 mutex_init(&devcd->mutex);
358 device_initialize(&devcd->devcd_dev);
360 dev_set_name(&devcd->devcd_dev, "devcd%d",
361 atomic_inc_return(&devcd_count));
362 devcd->devcd_dev.class = &devcd_class;
364 mutex_lock(&devcd->mutex);
365 if (device_add(&devcd->devcd_dev))
369 * These should normally not fail, but there is no problem
370 * continuing without the links, so just warn instead of
373 if (sysfs_create_link(&devcd->devcd_dev.kobj, &dev->kobj,
375 sysfs_create_link(&dev->kobj, &devcd->devcd_dev.kobj,
377 dev_warn(dev, "devcoredump create_link failed\n");
379 INIT_DELAYED_WORK(&devcd->del_wk, devcd_del);
380 schedule_delayed_work(&devcd->del_wk, DEVCD_TIMEOUT);
381 mutex_unlock(&devcd->mutex);
384 put_device(&devcd->devcd_dev);
385 mutex_unlock(&devcd->mutex);
391 EXPORT_SYMBOL_GPL(dev_coredumpm);
394 * dev_coredumpsg - create device coredump that uses scatterlist as data
396 * @dev: the struct device for the crashed device
397 * @table: the dump data
398 * @datalen: length of the data
399 * @gfp: allocation flags
401 * Creates a new device coredump for the given device. If a previous one hasn't
402 * been read yet, the new coredump is discarded. The data lifetime is determined
403 * by the device coredump framework and when it is no longer needed
404 * it will free the data.
406 void dev_coredumpsg(struct device *dev, struct scatterlist *table,
407 size_t datalen, gfp_t gfp)
409 dev_coredumpm(dev, NULL, table, datalen, gfp, devcd_read_from_sgtable,
412 EXPORT_SYMBOL_GPL(dev_coredumpsg);
414 static int __init devcoredump_init(void)
416 return class_register(&devcd_class);
418 __initcall(devcoredump_init);
420 static void __exit devcoredump_exit(void)
422 class_for_each_device(&devcd_class, NULL, NULL, devcd_free);
423 class_unregister(&devcd_class);
425 __exitcall(devcoredump_exit);