devcoredump.c 9.3 KB

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