devcoredump.c 9.9 KB

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