devcoredump.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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 struct class_attribute devcd_class_attrs[] = {
  135. __ATTR_RW(disabled),
  136. __ATTR_NULL
  137. };
  138. static struct class devcd_class = {
  139. .name = "devcoredump",
  140. .owner = THIS_MODULE,
  141. .dev_release = devcd_dev_release,
  142. .dev_groups = devcd_dev_groups,
  143. .class_attrs = devcd_class_attrs,
  144. };
  145. static ssize_t devcd_readv(char *buffer, loff_t offset, size_t count,
  146. void *data, size_t datalen)
  147. {
  148. if (offset > datalen)
  149. return -EINVAL;
  150. if (offset + count > datalen)
  151. count = datalen - offset;
  152. if (count)
  153. memcpy(buffer, ((u8 *)data) + offset, count);
  154. return count;
  155. }
  156. static void devcd_freev(void *data)
  157. {
  158. vfree(data);
  159. }
  160. /**
  161. * dev_coredumpv - create device coredump with vmalloc data
  162. * @dev: the struct device for the crashed device
  163. * @data: vmalloc data containing the device coredump
  164. * @datalen: length of the data
  165. * @gfp: allocation flags
  166. *
  167. * This function takes ownership of the vmalloc'ed data and will free
  168. * it when it is no longer used. See dev_coredumpm() for more information.
  169. */
  170. void dev_coredumpv(struct device *dev, void *data, size_t datalen,
  171. gfp_t gfp)
  172. {
  173. dev_coredumpm(dev, NULL, data, datalen, gfp, devcd_readv, devcd_freev);
  174. }
  175. EXPORT_SYMBOL_GPL(dev_coredumpv);
  176. static int devcd_match_failing(struct device *dev, const void *failing)
  177. {
  178. struct devcd_entry *devcd = dev_to_devcd(dev);
  179. return devcd->failing_dev == failing;
  180. }
  181. /**
  182. * devcd_free_sgtable - free all the memory of the given scatterlist table
  183. * (i.e. both pages and scatterlist instances)
  184. * NOTE: if two tables allocated with devcd_alloc_sgtable and then chained
  185. * using the sg_chain function then that function should be called only once
  186. * on the chained table
  187. * @table: pointer to sg_table to free
  188. */
  189. static void devcd_free_sgtable(void *data)
  190. {
  191. _devcd_free_sgtable(data);
  192. }
  193. /**
  194. * devcd_read_from_table - copy data from sg_table to a given buffer
  195. * and return the number of bytes read
  196. * @buffer: the buffer to copy the data to it
  197. * @buf_len: the length of the buffer
  198. * @data: the scatterlist table to copy from
  199. * @offset: start copy from @offset@ bytes from the head of the data
  200. * in the given scatterlist
  201. * @data_len: the length of the data in the sg_table
  202. */
  203. static ssize_t devcd_read_from_sgtable(char *buffer, loff_t offset,
  204. size_t buf_len, void *data,
  205. size_t data_len)
  206. {
  207. struct scatterlist *table = data;
  208. if (offset > data_len)
  209. return -EINVAL;
  210. if (offset + buf_len > data_len)
  211. buf_len = data_len - offset;
  212. return sg_pcopy_to_buffer(table, sg_nents(table), buffer, buf_len,
  213. offset);
  214. }
  215. /**
  216. * dev_coredumpm - create device coredump with read/free methods
  217. * @dev: the struct device for the crashed device
  218. * @owner: the module that contains the read/free functions, use %THIS_MODULE
  219. * @data: data cookie for the @read/@free functions
  220. * @datalen: length of the data
  221. * @gfp: allocation flags
  222. * @read: function to read from the given buffer
  223. * @free: function to free the given buffer
  224. *
  225. * Creates a new device coredump for the given device. If a previous one hasn't
  226. * been read yet, the new coredump is discarded. The data lifetime is determined
  227. * by the device coredump framework and when it is no longer needed the @free
  228. * function will be called to free the data.
  229. */
  230. void dev_coredumpm(struct device *dev, struct module *owner,
  231. void *data, size_t datalen, gfp_t gfp,
  232. ssize_t (*read)(char *buffer, loff_t offset, size_t count,
  233. void *data, size_t datalen),
  234. void (*free)(void *data))
  235. {
  236. static atomic_t devcd_count = ATOMIC_INIT(0);
  237. struct devcd_entry *devcd;
  238. struct device *existing;
  239. if (devcd_disabled)
  240. goto free;
  241. existing = class_find_device(&devcd_class, NULL, dev,
  242. devcd_match_failing);
  243. if (existing) {
  244. put_device(existing);
  245. goto free;
  246. }
  247. if (!try_module_get(owner))
  248. goto free;
  249. devcd = kzalloc(sizeof(*devcd), gfp);
  250. if (!devcd)
  251. goto put_module;
  252. devcd->owner = owner;
  253. devcd->data = data;
  254. devcd->datalen = datalen;
  255. devcd->read = read;
  256. devcd->free = free;
  257. devcd->failing_dev = get_device(dev);
  258. device_initialize(&devcd->devcd_dev);
  259. dev_set_name(&devcd->devcd_dev, "devcd%d",
  260. atomic_inc_return(&devcd_count));
  261. devcd->devcd_dev.class = &devcd_class;
  262. if (device_add(&devcd->devcd_dev))
  263. goto put_device;
  264. if (sysfs_create_link(&devcd->devcd_dev.kobj, &dev->kobj,
  265. "failing_device"))
  266. /* nothing - symlink will be missing */;
  267. if (sysfs_create_link(&dev->kobj, &devcd->devcd_dev.kobj,
  268. "devcoredump"))
  269. /* nothing - symlink will be missing */;
  270. INIT_DELAYED_WORK(&devcd->del_wk, devcd_del);
  271. schedule_delayed_work(&devcd->del_wk, DEVCD_TIMEOUT);
  272. return;
  273. put_device:
  274. put_device(&devcd->devcd_dev);
  275. put_module:
  276. module_put(owner);
  277. free:
  278. free(data);
  279. }
  280. EXPORT_SYMBOL_GPL(dev_coredumpm);
  281. /**
  282. * dev_coredumpmsg - create device coredump that uses scatterlist as data
  283. * parameter
  284. * @dev: the struct device for the crashed device
  285. * @table: the dump data
  286. * @datalen: length of the data
  287. * @gfp: allocation flags
  288. *
  289. * Creates a new device coredump for the given device. If a previous one hasn't
  290. * been read yet, the new coredump is discarded. The data lifetime is determined
  291. * by the device coredump framework and when it is no longer needed
  292. * it will free the data.
  293. */
  294. void dev_coredumpsg(struct device *dev, struct scatterlist *table,
  295. size_t datalen, gfp_t gfp)
  296. {
  297. dev_coredumpm(dev, NULL, table, datalen, gfp, devcd_read_from_sgtable,
  298. devcd_free_sgtable);
  299. }
  300. EXPORT_SYMBOL_GPL(dev_coredumpsg);
  301. static int __init devcoredump_init(void)
  302. {
  303. return class_register(&devcd_class);
  304. }
  305. __initcall(devcoredump_init);
  306. static void __exit devcoredump_exit(void)
  307. {
  308. class_for_each_device(&devcd_class, NULL, NULL, devcd_free);
  309. class_unregister(&devcd_class);
  310. }
  311. __exitcall(devcoredump_exit);