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
;
22 struct device devcd_dev
;
26 * Here, mutex is required to serialize the calls to del_wk work between
27 * user/kernel space which happens when devcd is added with device_add()
28 * and that sends uevent to user space. User space reads the uevents,
29 * and calls to devcd_data_write() which try to modify the work which is
30 * not even initialized/queued from devcoredump.
36 * dev_coredump() uevent sent to user space
37 * device_add() ======================> user space process Y reads the
38 * uevents writes to devcd fd
39 * which results into writes to
43 * try_to_grab_pending()
47 * schedule_delayed_work()
50 * Also, mutex alone would not be enough to avoid scheduling of
51 * del_wk work after it get flush from a call to devcd_free()
56 * mutex_lock() devcd_data_write()
57 * flush_delayed_work()
62 * So, delete_work flag is required.
67 ssize_t (*read
)(char *buffer
, loff_t offset
, size_t count
,
68 void *data
, size_t datalen
);
69 void (*free
)(void *data
);
70 struct delayed_work del_wk
;
71 struct device
*failing_dev
;
74 static struct devcd_entry
*dev_to_devcd(struct device
*dev
)
76 return container_of(dev
, struct devcd_entry
, devcd_dev
);
79 static void devcd_dev_release(struct device
*dev
)
81 struct devcd_entry
*devcd
= dev_to_devcd(dev
);
83 devcd
->free(devcd
->data
);
84 module_put(devcd
->owner
);
87 * this seems racy, but I don't see a notifier or such on
88 * a struct device to know when it goes away?
90 if (devcd
->failing_dev
->kobj
.sd
)
91 sysfs_delete_link(&devcd
->failing_dev
->kobj
, &dev
->kobj
,
94 put_device(devcd
->failing_dev
);
98 static void devcd_del(struct work_struct
*wk
)
100 struct devcd_entry
*devcd
;
102 devcd
= container_of(wk
, struct devcd_entry
, del_wk
.work
);
104 device_del(&devcd
->devcd_dev
);
105 put_device(&devcd
->devcd_dev
);
108 static ssize_t
devcd_data_read(struct file
*filp
, struct kobject
*kobj
,
109 struct bin_attribute
*bin_attr
,
110 char *buffer
, loff_t offset
, size_t count
)
112 struct device
*dev
= kobj_to_dev(kobj
);
113 struct devcd_entry
*devcd
= dev_to_devcd(dev
);
115 return devcd
->read(buffer
, offset
, count
, devcd
->data
, devcd
->datalen
);
118 static ssize_t
devcd_data_write(struct file
*filp
, struct kobject
*kobj
,
119 struct bin_attribute
*bin_attr
,
120 char *buffer
, loff_t offset
, size_t count
)
122 struct device
*dev
= kobj_to_dev(kobj
);
123 struct devcd_entry
*devcd
= dev_to_devcd(dev
);
125 mutex_lock(&devcd
->mutex
);
126 if (!devcd
->delete_work
) {
127 devcd
->delete_work
= true;
128 mod_delayed_work(system_wq
, &devcd
->del_wk
, 0);
130 mutex_unlock(&devcd
->mutex
);
135 static struct bin_attribute devcd_attr_data
= {
136 .attr
= { .name
= "data", .mode
= S_IRUSR
| S_IWUSR
, },
138 .read
= devcd_data_read
,
139 .write
= devcd_data_write
,
142 static struct bin_attribute
*devcd_dev_bin_attrs
[] = {
143 &devcd_attr_data
, NULL
,
146 static const struct attribute_group devcd_dev_group
= {
147 .bin_attrs
= devcd_dev_bin_attrs
,
150 static const struct attribute_group
*devcd_dev_groups
[] = {
151 &devcd_dev_group
, NULL
,
154 static int devcd_free(struct device
*dev
, void *data
)
156 struct devcd_entry
*devcd
= dev_to_devcd(dev
);
158 mutex_lock(&devcd
->mutex
);
159 if (!devcd
->delete_work
)
160 devcd
->delete_work
= true;
162 flush_delayed_work(&devcd
->del_wk
);
163 mutex_unlock(&devcd
->mutex
);
167 static ssize_t
disabled_show(const struct class *class, const struct class_attribute
*attr
,
170 return sysfs_emit(buf
, "%d\n", devcd_disabled
);
175 * disabled_store() worker()
176 * class_for_each_device(&devcd_class,
177 * NULL, NULL, devcd_free)
180 * while ((dev = class_dev_iter_next(&iter))
183 * put_device() <- last reference
184 * error = fn(dev, data) devcd_dev_release()
185 * devcd_free(dev, data) kfree(devcd)
186 * mutex_lock(&devcd->mutex);
189 * In the above diagram, It looks like disabled_store() would be racing with parallely
190 * running devcd_del() and result in memory abort while acquiring devcd->mutex which
191 * is called after kfree of devcd memory after dropping its last reference with
192 * put_device(). However, this will not happens as fn(dev, data) runs
193 * with its own reference to device via klist_node so it is not its last reference.
194 * so, above situation would not occur.
197 static ssize_t
disabled_store(const struct class *class, const struct class_attribute
*attr
,
198 const char *buf
, size_t count
)
200 long tmp
= simple_strtol(buf
, NULL
, 10);
203 * This essentially makes the attribute write-once, since you can't
204 * go back to not having it disabled. This is intentional, it serves
205 * as a system lockdown feature.
210 devcd_disabled
= true;
212 class_for_each_device(&devcd_class
, NULL
, NULL
, devcd_free
);
216 static CLASS_ATTR_RW(disabled
);
218 static struct attribute
*devcd_class_attrs
[] = {
219 &class_attr_disabled
.attr
,
222 ATTRIBUTE_GROUPS(devcd_class
);
224 static struct class devcd_class
= {
225 .name
= "devcoredump",
226 .dev_release
= devcd_dev_release
,
227 .dev_groups
= devcd_dev_groups
,
228 .class_groups
= devcd_class_groups
,
231 static ssize_t
devcd_readv(char *buffer
, loff_t offset
, size_t count
,
232 void *data
, size_t datalen
)
234 return memory_read_from_buffer(buffer
, count
, &offset
, data
, datalen
);
237 static void devcd_freev(void *data
)
243 * dev_coredumpv - create device coredump with vmalloc data
244 * @dev: the struct device for the crashed device
245 * @data: vmalloc data containing the device coredump
246 * @datalen: length of the data
247 * @gfp: allocation flags
249 * This function takes ownership of the vmalloc'ed data and will free
250 * it when it is no longer used. See dev_coredumpm() for more information.
252 void dev_coredumpv(struct device
*dev
, void *data
, size_t datalen
,
255 dev_coredumpm(dev
, NULL
, data
, datalen
, gfp
, devcd_readv
, devcd_freev
);
257 EXPORT_SYMBOL_GPL(dev_coredumpv
);
259 static int devcd_match_failing(struct device
*dev
, const void *failing
)
261 struct devcd_entry
*devcd
= dev_to_devcd(dev
);
263 return devcd
->failing_dev
== failing
;
267 * devcd_free_sgtable - free all the memory of the given scatterlist table
268 * (i.e. both pages and scatterlist instances)
269 * NOTE: if two tables allocated with devcd_alloc_sgtable and then chained
270 * using the sg_chain function then that function should be called only once
271 * on the chained table
272 * @data: pointer to sg_table to free
274 static void devcd_free_sgtable(void *data
)
276 _devcd_free_sgtable(data
);
280 * devcd_read_from_sgtable - copy data from sg_table to a given buffer
281 * and return the number of bytes read
282 * @buffer: the buffer to copy the data to it
283 * @buf_len: the length of the buffer
284 * @data: the scatterlist table to copy from
285 * @offset: start copy from @offset@ bytes from the head of the data
286 * in the given scatterlist
287 * @data_len: the length of the data in the sg_table
289 static ssize_t
devcd_read_from_sgtable(char *buffer
, loff_t offset
,
290 size_t buf_len
, void *data
,
293 struct scatterlist
*table
= data
;
295 if (offset
> data_len
)
298 if (offset
+ buf_len
> data_len
)
299 buf_len
= data_len
- offset
;
300 return sg_pcopy_to_buffer(table
, sg_nents(table
), buffer
, buf_len
,
305 * dev_coredump_put - remove device coredump
306 * @dev: the struct device for the crashed device
308 * dev_coredump_put() removes coredump, if exists, for a given device from
309 * the file system and free its associated data otherwise, does nothing.
311 * It is useful for modules that do not want to keep coredump
312 * available after its unload.
314 void dev_coredump_put(struct device
*dev
)
316 struct device
*existing
;
318 existing
= class_find_device(&devcd_class
, NULL
, dev
,
319 devcd_match_failing
);
321 devcd_free(existing
, NULL
);
322 put_device(existing
);
325 EXPORT_SYMBOL_GPL(dev_coredump_put
);
328 * dev_coredumpm_timeout - create device coredump with read/free methods with a
330 * @dev: the struct device for the crashed device
331 * @owner: the module that contains the read/free functions, use %THIS_MODULE
332 * @data: data cookie for the @read/@free functions
333 * @datalen: length of the data
334 * @gfp: allocation flags
335 * @read: function to read from the given buffer
336 * @free: function to free the given buffer
337 * @timeout: time in jiffies to remove coredump
339 * Creates a new device coredump for the given device. If a previous one hasn't
340 * been read yet, the new coredump is discarded. The data lifetime is determined
341 * by the device coredump framework and when it is no longer needed the @free
342 * function will be called to free the data.
344 void dev_coredumpm_timeout(struct device
*dev
, struct module
*owner
,
345 void *data
, size_t datalen
, gfp_t gfp
,
346 ssize_t (*read
)(char *buffer
, loff_t offset
,
347 size_t count
, void *data
,
349 void (*free
)(void *data
),
350 unsigned long timeout
)
352 static atomic_t devcd_count
= ATOMIC_INIT(0);
353 struct devcd_entry
*devcd
;
354 struct device
*existing
;
359 existing
= class_find_device(&devcd_class
, NULL
, dev
,
360 devcd_match_failing
);
362 put_device(existing
);
366 if (!try_module_get(owner
))
369 devcd
= kzalloc(sizeof(*devcd
), gfp
);
373 devcd
->owner
= owner
;
375 devcd
->datalen
= datalen
;
378 devcd
->failing_dev
= get_device(dev
);
379 devcd
->delete_work
= false;
381 mutex_init(&devcd
->mutex
);
382 device_initialize(&devcd
->devcd_dev
);
384 dev_set_name(&devcd
->devcd_dev
, "devcd%d",
385 atomic_inc_return(&devcd_count
));
386 devcd
->devcd_dev
.class = &devcd_class
;
388 mutex_lock(&devcd
->mutex
);
389 dev_set_uevent_suppress(&devcd
->devcd_dev
, true);
390 if (device_add(&devcd
->devcd_dev
))
394 * These should normally not fail, but there is no problem
395 * continuing without the links, so just warn instead of
398 if (sysfs_create_link(&devcd
->devcd_dev
.kobj
, &dev
->kobj
,
400 sysfs_create_link(&dev
->kobj
, &devcd
->devcd_dev
.kobj
,
402 dev_warn(dev
, "devcoredump create_link failed\n");
404 dev_set_uevent_suppress(&devcd
->devcd_dev
, false);
405 kobject_uevent(&devcd
->devcd_dev
.kobj
, KOBJ_ADD
);
406 INIT_DELAYED_WORK(&devcd
->del_wk
, devcd_del
);
407 schedule_delayed_work(&devcd
->del_wk
, timeout
);
408 mutex_unlock(&devcd
->mutex
);
411 put_device(&devcd
->devcd_dev
);
412 mutex_unlock(&devcd
->mutex
);
418 EXPORT_SYMBOL_GPL(dev_coredumpm_timeout
);
421 * dev_coredumpsg - create device coredump that uses scatterlist as data
423 * @dev: the struct device for the crashed device
424 * @table: the dump data
425 * @datalen: length of the data
426 * @gfp: allocation flags
428 * Creates a new device coredump for the given device. If a previous one hasn't
429 * been read yet, the new coredump is discarded. The data lifetime is determined
430 * by the device coredump framework and when it is no longer needed
431 * it will free the data.
433 void dev_coredumpsg(struct device
*dev
, struct scatterlist
*table
,
434 size_t datalen
, gfp_t gfp
)
436 dev_coredumpm(dev
, NULL
, table
, datalen
, gfp
, devcd_read_from_sgtable
,
439 EXPORT_SYMBOL_GPL(dev_coredumpsg
);
441 static int __init
devcoredump_init(void)
443 return class_register(&devcd_class
);
445 __initcall(devcoredump_init
);
447 static void __exit
devcoredump_exit(void)
449 class_for_each_device(&devcd_class
, NULL
, NULL
, devcd_free
);
450 class_unregister(&devcd_class
);
452 __exitcall(devcoredump_exit
);