devcoredump.c 9.9 KB

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