blk.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Implements pstore backend driver that write to block (or non-block) storage
  4. * devices, using the pstore/zone API.
  5. */
  6. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  7. #include <linux/kernel.h>
  8. #include <linux/module.h>
  9. #include <linux/blkdev.h>
  10. #include <linux/string.h>
  11. #include <linux/of.h>
  12. #include <linux/of_address.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/pstore_blk.h>
  15. #include <linux/fs.h>
  16. #include <linux/file.h>
  17. #include <linux/init_syscalls.h>
  18. #include <linux/mount.h>
  19. static long kmsg_size = CONFIG_PSTORE_BLK_KMSG_SIZE;
  20. module_param(kmsg_size, long, 0400);
  21. MODULE_PARM_DESC(kmsg_size, "kmsg dump record size in kbytes");
  22. static int max_reason = CONFIG_PSTORE_BLK_MAX_REASON;
  23. module_param(max_reason, int, 0400);
  24. MODULE_PARM_DESC(max_reason,
  25. "maximum reason for kmsg dump (default 2: Oops and Panic)");
  26. #if IS_ENABLED(CONFIG_PSTORE_PMSG)
  27. static long pmsg_size = CONFIG_PSTORE_BLK_PMSG_SIZE;
  28. #else
  29. static long pmsg_size = -1;
  30. #endif
  31. module_param(pmsg_size, long, 0400);
  32. MODULE_PARM_DESC(pmsg_size, "pmsg size in kbytes");
  33. #if IS_ENABLED(CONFIG_PSTORE_CONSOLE)
  34. static long console_size = CONFIG_PSTORE_BLK_CONSOLE_SIZE;
  35. #else
  36. static long console_size = -1;
  37. #endif
  38. module_param(console_size, long, 0400);
  39. MODULE_PARM_DESC(console_size, "console size in kbytes");
  40. #if IS_ENABLED(CONFIG_PSTORE_FTRACE)
  41. static long ftrace_size = CONFIG_PSTORE_BLK_FTRACE_SIZE;
  42. #else
  43. static long ftrace_size = -1;
  44. #endif
  45. module_param(ftrace_size, long, 0400);
  46. MODULE_PARM_DESC(ftrace_size, "ftrace size in kbytes");
  47. static bool best_effort;
  48. module_param(best_effort, bool, 0400);
  49. MODULE_PARM_DESC(best_effort, "use best effort to write (i.e. do not require storage driver pstore support, default: off)");
  50. /*
  51. * blkdev - the block device to use for pstore storage
  52. * See Documentation/admin-guide/pstore-blk.rst for details.
  53. */
  54. static char blkdev[80] = CONFIG_PSTORE_BLK_BLKDEV;
  55. module_param_string(blkdev, blkdev, 80, 0400);
  56. MODULE_PARM_DESC(blkdev, "block device for pstore storage");
  57. /*
  58. * All globals must only be accessed under the pstore_blk_lock
  59. * during the register/unregister functions.
  60. */
  61. static DEFINE_MUTEX(pstore_blk_lock);
  62. static struct file *psblk_file;
  63. static struct pstore_device_info *pstore_device_info;
  64. #define check_size(name, alignsize) ({ \
  65. long _##name_ = (name); \
  66. _##name_ = _##name_ <= 0 ? 0 : (_##name_ * 1024); \
  67. if (_##name_ & ((alignsize) - 1)) { \
  68. pr_info(#name " must align to %d\n", \
  69. (alignsize)); \
  70. _##name_ = ALIGN(name, (alignsize)); \
  71. } \
  72. _##name_; \
  73. })
  74. #define verify_size(name, alignsize, enabled) { \
  75. long _##name_; \
  76. if (enabled) \
  77. _##name_ = check_size(name, alignsize); \
  78. else \
  79. _##name_ = 0; \
  80. /* Synchronize module parameters with results. */ \
  81. name = _##name_ / 1024; \
  82. dev->zone.name = _##name_; \
  83. }
  84. static int __register_pstore_device(struct pstore_device_info *dev)
  85. {
  86. int ret;
  87. lockdep_assert_held(&pstore_blk_lock);
  88. if (!dev) {
  89. pr_err("NULL device info\n");
  90. return -EINVAL;
  91. }
  92. if (!dev->zone.total_size) {
  93. pr_err("zero sized device\n");
  94. return -EINVAL;
  95. }
  96. if (!dev->zone.read) {
  97. pr_err("no read handler for device\n");
  98. return -EINVAL;
  99. }
  100. if (!dev->zone.write) {
  101. pr_err("no write handler for device\n");
  102. return -EINVAL;
  103. }
  104. /* someone already registered before */
  105. if (pstore_device_info)
  106. return -EBUSY;
  107. /* zero means no limit on which backends attempt to store. */
  108. if (!dev->flags)
  109. dev->flags = UINT_MAX;
  110. /* Copy in module parameters. */
  111. verify_size(kmsg_size, 4096, dev->flags & PSTORE_FLAGS_DMESG);
  112. verify_size(pmsg_size, 4096, dev->flags & PSTORE_FLAGS_PMSG);
  113. verify_size(console_size, 4096, dev->flags & PSTORE_FLAGS_CONSOLE);
  114. verify_size(ftrace_size, 4096, dev->flags & PSTORE_FLAGS_FTRACE);
  115. dev->zone.max_reason = max_reason;
  116. /* Initialize required zone ownership details. */
  117. dev->zone.name = KBUILD_MODNAME;
  118. dev->zone.owner = THIS_MODULE;
  119. ret = register_pstore_zone(&dev->zone);
  120. if (ret == 0)
  121. pstore_device_info = dev;
  122. return ret;
  123. }
  124. /**
  125. * register_pstore_device() - register non-block device to pstore/blk
  126. *
  127. * @dev: non-block device information
  128. *
  129. * Return:
  130. * * 0 - OK
  131. * * Others - something error.
  132. */
  133. int register_pstore_device(struct pstore_device_info *dev)
  134. {
  135. int ret;
  136. mutex_lock(&pstore_blk_lock);
  137. ret = __register_pstore_device(dev);
  138. mutex_unlock(&pstore_blk_lock);
  139. return ret;
  140. }
  141. EXPORT_SYMBOL_GPL(register_pstore_device);
  142. static void __unregister_pstore_device(struct pstore_device_info *dev)
  143. {
  144. lockdep_assert_held(&pstore_blk_lock);
  145. if (pstore_device_info && pstore_device_info == dev) {
  146. unregister_pstore_zone(&dev->zone);
  147. pstore_device_info = NULL;
  148. }
  149. }
  150. /**
  151. * unregister_pstore_device() - unregister non-block device from pstore/blk
  152. *
  153. * @dev: non-block device information
  154. */
  155. void unregister_pstore_device(struct pstore_device_info *dev)
  156. {
  157. mutex_lock(&pstore_blk_lock);
  158. __unregister_pstore_device(dev);
  159. mutex_unlock(&pstore_blk_lock);
  160. }
  161. EXPORT_SYMBOL_GPL(unregister_pstore_device);
  162. static ssize_t psblk_generic_blk_read(char *buf, size_t bytes, loff_t pos)
  163. {
  164. return kernel_read(psblk_file, buf, bytes, &pos);
  165. }
  166. static ssize_t psblk_generic_blk_write(const char *buf, size_t bytes,
  167. loff_t pos)
  168. {
  169. /* Console/Ftrace backend may handle buffer until flush dirty zones */
  170. if (in_interrupt() || irqs_disabled())
  171. return -EBUSY;
  172. return kernel_write(psblk_file, buf, bytes, &pos);
  173. }
  174. /*
  175. * This takes its configuration only from the module parameters now.
  176. */
  177. static int __register_pstore_blk(struct pstore_device_info *dev,
  178. const char *devpath)
  179. {
  180. int ret = -ENODEV;
  181. lockdep_assert_held(&pstore_blk_lock);
  182. psblk_file = filp_open(devpath, O_RDWR | O_DSYNC | O_NOATIME | O_EXCL, 0);
  183. if (IS_ERR(psblk_file)) {
  184. ret = PTR_ERR(psblk_file);
  185. pr_err("failed to open '%s': %d!\n", devpath, ret);
  186. goto err;
  187. }
  188. if (!S_ISBLK(file_inode(psblk_file)->i_mode)) {
  189. pr_err("'%s' is not block device!\n", devpath);
  190. goto err_fput;
  191. }
  192. dev->zone.total_size =
  193. bdev_nr_bytes(I_BDEV(psblk_file->f_mapping->host));
  194. ret = __register_pstore_device(dev);
  195. if (ret)
  196. goto err_fput;
  197. return 0;
  198. err_fput:
  199. fput(psblk_file);
  200. err:
  201. psblk_file = NULL;
  202. return ret;
  203. }
  204. /* get information of pstore/blk */
  205. int pstore_blk_get_config(struct pstore_blk_config *info)
  206. {
  207. strscpy(info->device, blkdev);
  208. info->max_reason = max_reason;
  209. info->kmsg_size = check_size(kmsg_size, 4096);
  210. info->pmsg_size = check_size(pmsg_size, 4096);
  211. info->ftrace_size = check_size(ftrace_size, 4096);
  212. info->console_size = check_size(console_size, 4096);
  213. return 0;
  214. }
  215. EXPORT_SYMBOL_GPL(pstore_blk_get_config);
  216. #ifndef MODULE
  217. static const char devname[] = "/dev/pstore-blk";
  218. static __init const char *early_boot_devpath(const char *initial_devname)
  219. {
  220. /*
  221. * During early boot the real root file system hasn't been
  222. * mounted yet, and no device nodes are present yet. Use the
  223. * same scheme to find the device that we use for mounting
  224. * the root file system.
  225. */
  226. dev_t dev;
  227. if (early_lookup_bdev(initial_devname, &dev)) {
  228. pr_err("failed to resolve '%s'!\n", initial_devname);
  229. return initial_devname;
  230. }
  231. init_unlink(devname);
  232. init_mknod(devname, S_IFBLK | 0600, new_encode_dev(dev));
  233. return devname;
  234. }
  235. #else
  236. static inline const char *early_boot_devpath(const char *initial_devname)
  237. {
  238. return initial_devname;
  239. }
  240. #endif
  241. static int __init __best_effort_init(void)
  242. {
  243. struct pstore_device_info *best_effort_dev;
  244. int ret;
  245. /* No best-effort mode requested. */
  246. if (!best_effort)
  247. return 0;
  248. /* Reject an empty blkdev. */
  249. if (!blkdev[0]) {
  250. pr_err("blkdev empty with best_effort=Y\n");
  251. return -EINVAL;
  252. }
  253. best_effort_dev = kzalloc(sizeof(*best_effort_dev), GFP_KERNEL);
  254. if (!best_effort_dev)
  255. return -ENOMEM;
  256. best_effort_dev->zone.read = psblk_generic_blk_read;
  257. best_effort_dev->zone.write = psblk_generic_blk_write;
  258. ret = __register_pstore_blk(best_effort_dev,
  259. early_boot_devpath(blkdev));
  260. if (ret)
  261. kfree(best_effort_dev);
  262. else
  263. pr_info("attached %s (%lu) (no dedicated panic_write!)\n",
  264. blkdev, best_effort_dev->zone.total_size);
  265. return ret;
  266. }
  267. static void __exit __best_effort_exit(void)
  268. {
  269. /*
  270. * Currently, the only user of psblk_file is best_effort, so
  271. * we can assume that pstore_device_info is associated with it.
  272. * Once there are "real" blk devices, there will need to be a
  273. * dedicated pstore_blk_info, etc.
  274. */
  275. if (psblk_file) {
  276. struct pstore_device_info *dev = pstore_device_info;
  277. __unregister_pstore_device(dev);
  278. kfree(dev);
  279. fput(psblk_file);
  280. psblk_file = NULL;
  281. }
  282. }
  283. static int __init pstore_blk_init(void)
  284. {
  285. int ret;
  286. mutex_lock(&pstore_blk_lock);
  287. ret = __best_effort_init();
  288. mutex_unlock(&pstore_blk_lock);
  289. return ret;
  290. }
  291. late_initcall(pstore_blk_init);
  292. static void __exit pstore_blk_exit(void)
  293. {
  294. mutex_lock(&pstore_blk_lock);
  295. __best_effort_exit();
  296. /* If we've been asked to unload, unregister any remaining device. */
  297. __unregister_pstore_device(pstore_device_info);
  298. mutex_unlock(&pstore_blk_lock);
  299. }
  300. module_exit(pstore_blk_exit);
  301. MODULE_LICENSE("GPL");
  302. MODULE_AUTHOR("WeiXiong Liao <liaoweixiong@allwinnertech.com>");
  303. MODULE_AUTHOR("Kees Cook <keescook@chromium.org>");
  304. MODULE_DESCRIPTION("pstore backend for block devices");