devcoredump.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /*
  2. * This file is provided under the GPLv2 license.
  3. *
  4. * GPL LICENSE SUMMARY
  5. *
  6. * Copyright(c) 2014 Intel Mobile Communications GmbH
  7. *
  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.
  11. *
  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.
  16. *
  17. * The full GNU General Public License is included in this distribution
  18. * in the file called COPYING.
  19. *
  20. * Contact Information:
  21. * Intel Linux Wireless <ilw@linux.intel.com>
  22. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  23. *
  24. * Author: Johannes Berg <johannes@sipsolutions.net>
  25. */
  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>
  31. #include <linux/fs.h>
  32. #include <linux/workqueue.h>
  33. static struct class devcd_class;
  34. /* global disable flag, for security purposes */
  35. static bool devcd_disabled;
  36. /* if data isn't read by userspace after 5 minutes then delete it */
  37. #define DEVCD_TIMEOUT (HZ * 60 * 5)
  38. struct devcd_entry {
  39. struct device devcd_dev;
  40. const void *data;
  41. size_t datalen;
  42. struct module *owner;
  43. ssize_t (*read)(char *buffer, loff_t offset, size_t count,
  44. const void *data, size_t datalen);
  45. void (*free)(const void *data);
  46. struct delayed_work del_wk;
  47. struct device *failing_dev;
  48. };
  49. static struct devcd_entry *dev_to_devcd(struct device *dev)
  50. {
  51. return container_of(dev, struct devcd_entry, devcd_dev);
  52. }
  53. static void devcd_dev_release(struct device *dev)
  54. {
  55. struct devcd_entry *devcd = dev_to_devcd(dev);
  56. devcd->free(devcd->data);
  57. module_put(devcd->owner);
  58. /*
  59. * this seems racy, but I don't see a notifier or such on
  60. * a struct device to know when it goes away?
  61. */
  62. if (devcd->failing_dev->kobj.sd)
  63. sysfs_delete_link(&devcd->failing_dev->kobj, &dev->kobj,
  64. "devcoredump");
  65. put_device(devcd->failing_dev);
  66. kfree(devcd);
  67. }
  68. static void devcd_del(struct work_struct *wk)
  69. {
  70. struct devcd_entry *devcd;
  71. devcd = container_of(wk, struct devcd_entry, del_wk.work);
  72. device_del(&devcd->devcd_dev);
  73. put_device(&devcd->devcd_dev);
  74. }
  75. static ssize_t devcd_data_read(struct file *filp, struct kobject *kobj,
  76. struct bin_attribute *bin_attr,
  77. char *buffer, loff_t offset, size_t count)
  78. {
  79. struct device *dev = kobj_to_dev(kobj);
  80. struct devcd_entry *devcd = dev_to_devcd(dev);
  81. return devcd->read(buffer, offset, count, devcd->data, devcd->datalen);
  82. }
  83. static ssize_t devcd_data_write(struct file *filp, struct kobject *kobj,
  84. struct bin_attribute *bin_attr,
  85. char *buffer, loff_t offset, size_t count)
  86. {
  87. struct device *dev = kobj_to_dev(kobj);
  88. struct devcd_entry *devcd = dev_to_devcd(dev);
  89. mod_delayed_work(system_wq, &devcd->del_wk, 0);
  90. return count;
  91. }
  92. static struct bin_attribute devcd_attr_data = {
  93. .attr = { .name = "data", .mode = S_IRUSR | S_IWUSR, },
  94. .size = 0,
  95. .read = devcd_data_read,
  96. .write = devcd_data_write,
  97. };
  98. static struct bin_attribute *devcd_dev_bin_attrs[] = {
  99. &devcd_attr_data, NULL,
  100. };
  101. static const struct attribute_group devcd_dev_group = {
  102. .bin_attrs = devcd_dev_bin_attrs,
  103. };
  104. static const struct attribute_group *devcd_dev_groups[] = {
  105. &devcd_dev_group, NULL,
  106. };
  107. static int devcd_free(struct device *dev, void *data)
  108. {
  109. struct devcd_entry *devcd = dev_to_devcd(dev);
  110. flush_delayed_work(&devcd->del_wk);
  111. return 0;
  112. }
  113. static ssize_t disabled_show(struct class *class, struct class_attribute *attr,
  114. char *buf)
  115. {
  116. return sprintf(buf, "%d\n", devcd_disabled);
  117. }
  118. static ssize_t disabled_store(struct class *class, struct class_attribute *attr,
  119. const char *buf, size_t count)
  120. {
  121. long tmp = simple_strtol(buf, NULL, 10);
  122. /*
  123. * This essentially makes the attribute write-once, since you can't
  124. * go back to not having it disabled. This is intentional, it serves
  125. * as a system lockdown feature.
  126. */
  127. if (tmp != 1)
  128. return -EINVAL;
  129. devcd_disabled = true;
  130. class_for_each_device(&devcd_class, NULL, NULL, devcd_free);
  131. return count;
  132. }
  133. static struct class_attribute devcd_class_attrs[] = {
  134. __ATTR_RW(disabled),
  135. __ATTR_NULL
  136. };
  137. static struct class devcd_class = {
  138. .name = "devcoredump",
  139. .owner = THIS_MODULE,
  140. .dev_release = devcd_dev_release,
  141. .dev_groups = devcd_dev_groups,
  142. .class_attrs = devcd_class_attrs,
  143. };
  144. static ssize_t devcd_readv(char *buffer, loff_t offset, size_t count,
  145. const void *data, size_t datalen)
  146. {
  147. if (offset > datalen)
  148. return -EINVAL;
  149. if (offset + count > datalen)
  150. count = datalen - offset;
  151. if (count)
  152. memcpy(buffer, ((u8 *)data) + offset, count);
  153. return count;
  154. }
  155. /**
  156. * dev_coredumpv - create device coredump with vmalloc data
  157. * @dev: the struct device for the crashed device
  158. * @data: vmalloc data containing the device coredump
  159. * @datalen: length of the data
  160. * @gfp: allocation flags
  161. *
  162. * This function takes ownership of the vmalloc'ed data and will free
  163. * it when it is no longer used. See dev_coredumpm() for more information.
  164. */
  165. void dev_coredumpv(struct device *dev, const void *data, size_t datalen,
  166. gfp_t gfp)
  167. {
  168. dev_coredumpm(dev, NULL, data, datalen, gfp, devcd_readv, vfree);
  169. }
  170. EXPORT_SYMBOL_GPL(dev_coredumpv);
  171. static int devcd_match_failing(struct device *dev, const void *failing)
  172. {
  173. struct devcd_entry *devcd = dev_to_devcd(dev);
  174. return devcd->failing_dev == failing;
  175. }
  176. /**
  177. * dev_coredumpm - create device coredump with read/free methods
  178. * @dev: the struct device for the crashed device
  179. * @owner: the module that contains the read/free functions, use %THIS_MODULE
  180. * @data: data cookie for the @read/@free functions
  181. * @datalen: length of the data
  182. * @gfp: allocation flags
  183. * @read: function to read from the given buffer
  184. * @free: function to free the given buffer
  185. *
  186. * Creates a new device coredump for the given device. If a previous one hasn't
  187. * been read yet, the new coredump is discarded. The data lifetime is determined
  188. * by the device coredump framework and when it is no longer needed the @free
  189. * function will be called to free the data.
  190. */
  191. void dev_coredumpm(struct device *dev, struct module *owner,
  192. const void *data, size_t datalen, gfp_t gfp,
  193. ssize_t (*read)(char *buffer, loff_t offset, size_t count,
  194. const void *data, size_t datalen),
  195. void (*free)(const void *data))
  196. {
  197. static atomic_t devcd_count = ATOMIC_INIT(0);
  198. struct devcd_entry *devcd;
  199. struct device *existing;
  200. if (devcd_disabled)
  201. goto free;
  202. existing = class_find_device(&devcd_class, NULL, dev,
  203. devcd_match_failing);
  204. if (existing) {
  205. put_device(existing);
  206. goto free;
  207. }
  208. if (!try_module_get(owner))
  209. goto free;
  210. devcd = kzalloc(sizeof(*devcd), gfp);
  211. if (!devcd)
  212. goto put_module;
  213. devcd->owner = owner;
  214. devcd->data = data;
  215. devcd->datalen = datalen;
  216. devcd->read = read;
  217. devcd->free = free;
  218. devcd->failing_dev = get_device(dev);
  219. device_initialize(&devcd->devcd_dev);
  220. dev_set_name(&devcd->devcd_dev, "devcd%d",
  221. atomic_inc_return(&devcd_count));
  222. devcd->devcd_dev.class = &devcd_class;
  223. if (device_add(&devcd->devcd_dev))
  224. goto put_device;
  225. if (sysfs_create_link(&devcd->devcd_dev.kobj, &dev->kobj,
  226. "failing_device"))
  227. /* nothing - symlink will be missing */;
  228. if (sysfs_create_link(&dev->kobj, &devcd->devcd_dev.kobj,
  229. "devcoredump"))
  230. /* nothing - symlink will be missing */;
  231. INIT_DELAYED_WORK(&devcd->del_wk, devcd_del);
  232. schedule_delayed_work(&devcd->del_wk, DEVCD_TIMEOUT);
  233. return;
  234. put_device:
  235. put_device(&devcd->devcd_dev);
  236. put_module:
  237. module_put(owner);
  238. free:
  239. free(data);
  240. }
  241. EXPORT_SYMBOL_GPL(dev_coredumpm);
  242. static int __init devcoredump_init(void)
  243. {
  244. return class_register(&devcd_class);
  245. }
  246. __initcall(devcoredump_init);
  247. static void __exit devcoredump_exit(void)
  248. {
  249. class_for_each_device(&devcd_class, NULL, NULL, devcd_free);
  250. class_unregister(&devcd_class);
  251. }
  252. __exitcall(devcoredump_exit);