devcoredump.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. /* if data isn't read by userspace after 5 minutes then delete it */
  34. #define DEVCD_TIMEOUT (HZ * 60 * 5)
  35. struct devcd_entry {
  36. struct device devcd_dev;
  37. const void *data;
  38. size_t datalen;
  39. struct module *owner;
  40. ssize_t (*read)(char *buffer, loff_t offset, size_t count,
  41. const void *data, size_t datalen);
  42. void (*free)(const void *data);
  43. struct delayed_work del_wk;
  44. struct device *failing_dev;
  45. };
  46. static struct devcd_entry *dev_to_devcd(struct device *dev)
  47. {
  48. return container_of(dev, struct devcd_entry, devcd_dev);
  49. }
  50. static void devcd_dev_release(struct device *dev)
  51. {
  52. struct devcd_entry *devcd = dev_to_devcd(dev);
  53. devcd->free(devcd->data);
  54. module_put(devcd->owner);
  55. /*
  56. * this seems racy, but I don't see a notifier or such on
  57. * a struct device to know when it goes away?
  58. */
  59. if (devcd->failing_dev->kobj.sd)
  60. sysfs_delete_link(&devcd->failing_dev->kobj, &dev->kobj,
  61. "devcoredump");
  62. put_device(devcd->failing_dev);
  63. kfree(devcd);
  64. }
  65. static void devcd_del(struct work_struct *wk)
  66. {
  67. struct devcd_entry *devcd;
  68. devcd = container_of(wk, struct devcd_entry, del_wk.work);
  69. device_del(&devcd->devcd_dev);
  70. put_device(&devcd->devcd_dev);
  71. }
  72. static ssize_t devcd_data_read(struct file *filp, struct kobject *kobj,
  73. struct bin_attribute *bin_attr,
  74. char *buffer, loff_t offset, size_t count)
  75. {
  76. struct device *dev = kobj_to_dev(kobj);
  77. struct devcd_entry *devcd = dev_to_devcd(dev);
  78. return devcd->read(buffer, offset, count, devcd->data, devcd->datalen);
  79. }
  80. static ssize_t devcd_data_write(struct file *filp, struct kobject *kobj,
  81. struct bin_attribute *bin_attr,
  82. char *buffer, loff_t offset, size_t count)
  83. {
  84. struct device *dev = kobj_to_dev(kobj);
  85. struct devcd_entry *devcd = dev_to_devcd(dev);
  86. mod_delayed_work(system_wq, &devcd->del_wk, 0);
  87. return count;
  88. }
  89. static struct bin_attribute devcd_attr_data = {
  90. .attr = { .name = "data", .mode = S_IRUSR | S_IWUSR, },
  91. .size = 0,
  92. .read = devcd_data_read,
  93. .write = devcd_data_write,
  94. };
  95. static struct bin_attribute *devcd_dev_bin_attrs[] = {
  96. &devcd_attr_data, NULL,
  97. };
  98. static const struct attribute_group devcd_dev_group = {
  99. .bin_attrs = devcd_dev_bin_attrs,
  100. };
  101. static const struct attribute_group *devcd_dev_groups[] = {
  102. &devcd_dev_group, NULL,
  103. };
  104. static struct class devcd_class = {
  105. .name = "devcoredump",
  106. .owner = THIS_MODULE,
  107. .dev_release = devcd_dev_release,
  108. .dev_groups = devcd_dev_groups,
  109. };
  110. static ssize_t devcd_readv(char *buffer, loff_t offset, size_t count,
  111. const void *data, size_t datalen)
  112. {
  113. if (offset > datalen)
  114. return -EINVAL;
  115. if (offset + count > datalen)
  116. count = datalen - offset;
  117. if (count)
  118. memcpy(buffer, ((u8 *)data) + offset, count);
  119. return count;
  120. }
  121. /**
  122. * dev_coredumpv - create device coredump with vmalloc data
  123. * @dev: the struct device for the crashed device
  124. * @data: vmalloc data containing the device coredump
  125. * @datalen: length of the data
  126. * @gfp: allocation flags
  127. *
  128. * This function takes ownership of the vmalloc'ed data and will free
  129. * it when it is no longer used. See dev_coredumpm() for more information.
  130. */
  131. void dev_coredumpv(struct device *dev, const void *data, size_t datalen,
  132. gfp_t gfp)
  133. {
  134. dev_coredumpm(dev, NULL, data, datalen, gfp, devcd_readv, vfree);
  135. }
  136. EXPORT_SYMBOL_GPL(dev_coredumpv);
  137. static int devcd_match_failing(struct device *dev, const void *failing)
  138. {
  139. struct devcd_entry *devcd = dev_to_devcd(dev);
  140. return devcd->failing_dev == failing;
  141. }
  142. /**
  143. * dev_coredumpm - create device coredump with read/free methods
  144. * @dev: the struct device for the crashed device
  145. * @owner: the module that contains the read/free functions, use %THIS_MODULE
  146. * @data: data cookie for the @read/@free functions
  147. * @datalen: length of the data
  148. * @gfp: allocation flags
  149. * @read: function to read from the given buffer
  150. * @free: function to free the given buffer
  151. *
  152. * Creates a new device coredump for the given device. If a previous one hasn't
  153. * been read yet, the new coredump is discarded. The data lifetime is determined
  154. * by the device coredump framework and when it is no longer needed the @free
  155. * function will be called to free the data.
  156. */
  157. void dev_coredumpm(struct device *dev, struct module *owner,
  158. const void *data, size_t datalen, gfp_t gfp,
  159. ssize_t (*read)(char *buffer, loff_t offset, size_t count,
  160. const void *data, size_t datalen),
  161. void (*free)(const void *data))
  162. {
  163. static atomic_t devcd_count = ATOMIC_INIT(0);
  164. struct devcd_entry *devcd;
  165. struct device *existing;
  166. existing = class_find_device(&devcd_class, NULL, dev,
  167. devcd_match_failing);
  168. if (existing) {
  169. put_device(existing);
  170. goto free;
  171. }
  172. if (!try_module_get(owner))
  173. goto free;
  174. devcd = kzalloc(sizeof(*devcd), gfp);
  175. if (!devcd)
  176. goto put_module;
  177. devcd->owner = owner;
  178. devcd->data = data;
  179. devcd->datalen = datalen;
  180. devcd->read = read;
  181. devcd->free = free;
  182. devcd->failing_dev = get_device(dev);
  183. device_initialize(&devcd->devcd_dev);
  184. dev_set_name(&devcd->devcd_dev, "devcd%d",
  185. atomic_inc_return(&devcd_count));
  186. devcd->devcd_dev.class = &devcd_class;
  187. if (device_add(&devcd->devcd_dev))
  188. goto put_device;
  189. if (sysfs_create_link(&devcd->devcd_dev.kobj, &dev->kobj,
  190. "failing_device"))
  191. /* nothing - symlink will be missing */;
  192. if (sysfs_create_link(&dev->kobj, &devcd->devcd_dev.kobj,
  193. "devcoredump"))
  194. /* nothing - symlink will be missing */;
  195. INIT_DELAYED_WORK(&devcd->del_wk, devcd_del);
  196. schedule_delayed_work(&devcd->del_wk, DEVCD_TIMEOUT);
  197. return;
  198. put_device:
  199. put_device(&devcd->devcd_dev);
  200. put_module:
  201. module_put(owner);
  202. free:
  203. free(data);
  204. }
  205. EXPORT_SYMBOL_GPL(dev_coredumpm);
  206. static int __init devcoredump_init(void)
  207. {
  208. return class_register(&devcd_class);
  209. }
  210. __initcall(devcoredump_init);
  211. static int devcd_free(struct device *dev, void *data)
  212. {
  213. struct devcd_entry *devcd = dev_to_devcd(dev);
  214. flush_delayed_work(&devcd->del_wk);
  215. return 0;
  216. }
  217. static void __exit devcoredump_exit(void)
  218. {
  219. class_for_each_device(&devcd_class, NULL, NULL, devcd_free);
  220. class_unregister(&devcd_class);
  221. }
  222. __exitcall(devcoredump_exit);