platform.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /*
  2. * Persistent Storage - platform driver interface parts.
  3. *
  4. * Copyright (C) 2007-2008 Google, Inc.
  5. * Copyright (C) 2010 Intel Corporation <tony.luck@intel.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #include <linux/atomic.h>
  21. #include <linux/types.h>
  22. #include <linux/errno.h>
  23. #include <linux/init.h>
  24. #include <linux/kmsg_dump.h>
  25. #include <linux/console.h>
  26. #include <linux/module.h>
  27. #include <linux/pstore.h>
  28. #include <linux/string.h>
  29. #include <linux/timer.h>
  30. #include <linux/slab.h>
  31. #include <linux/uaccess.h>
  32. #include <linux/hardirq.h>
  33. #include <linux/jiffies.h>
  34. #include <linux/workqueue.h>
  35. #include "internal.h"
  36. /*
  37. * We defer making "oops" entries appear in pstore - see
  38. * whether the system is actually still running well enough
  39. * to let someone see the entry
  40. */
  41. static int pstore_update_ms = -1;
  42. module_param_named(update_ms, pstore_update_ms, int, 0600);
  43. MODULE_PARM_DESC(update_ms, "milliseconds before pstore updates its content "
  44. "(default is -1, which means runtime updates are disabled; "
  45. "enabling this option is not safe, it may lead to further "
  46. "corruption on Oopses)");
  47. static int pstore_new_entry;
  48. static void pstore_timefunc(unsigned long);
  49. static DEFINE_TIMER(pstore_timer, pstore_timefunc, 0, 0);
  50. static void pstore_dowork(struct work_struct *);
  51. static DECLARE_WORK(pstore_work, pstore_dowork);
  52. /*
  53. * pstore_lock just protects "psinfo" during
  54. * calls to pstore_register()
  55. */
  56. static DEFINE_SPINLOCK(pstore_lock);
  57. struct pstore_info *psinfo;
  58. static char *backend;
  59. /* How much of the console log to snapshot */
  60. static unsigned long kmsg_bytes = 10240;
  61. void pstore_set_kmsg_bytes(int bytes)
  62. {
  63. kmsg_bytes = bytes;
  64. }
  65. /* Tag each group of saved records with a sequence number */
  66. static int oopscount;
  67. static const char *get_reason_str(enum kmsg_dump_reason reason)
  68. {
  69. switch (reason) {
  70. case KMSG_DUMP_PANIC:
  71. return "Panic";
  72. case KMSG_DUMP_OOPS:
  73. return "Oops";
  74. case KMSG_DUMP_EMERG:
  75. return "Emergency";
  76. case KMSG_DUMP_RESTART:
  77. return "Restart";
  78. case KMSG_DUMP_HALT:
  79. return "Halt";
  80. case KMSG_DUMP_POWEROFF:
  81. return "Poweroff";
  82. default:
  83. return "Unknown";
  84. }
  85. }
  86. /*
  87. * callback from kmsg_dump. (s2,l2) has the most recently
  88. * written bytes, older bytes are in (s1,l1). Save as much
  89. * as we can from the end of the buffer.
  90. */
  91. static void pstore_dump(struct kmsg_dumper *dumper,
  92. enum kmsg_dump_reason reason)
  93. {
  94. unsigned long total = 0;
  95. const char *why;
  96. u64 id;
  97. unsigned int part = 1;
  98. unsigned long flags = 0;
  99. int is_locked = 0;
  100. int ret;
  101. why = get_reason_str(reason);
  102. if (in_nmi()) {
  103. is_locked = spin_trylock(&psinfo->buf_lock);
  104. if (!is_locked)
  105. pr_err("pstore dump routine blocked in NMI, may corrupt error record\n");
  106. } else
  107. spin_lock_irqsave(&psinfo->buf_lock, flags);
  108. oopscount++;
  109. while (total < kmsg_bytes) {
  110. char *dst;
  111. unsigned long size;
  112. int hsize;
  113. size_t len;
  114. dst = psinfo->buf;
  115. hsize = sprintf(dst, "%s#%d Part%d\n", why, oopscount, part);
  116. size = psinfo->bufsize - hsize;
  117. dst += hsize;
  118. if (!kmsg_dump_get_buffer(dumper, true, dst, size, &len))
  119. break;
  120. ret = psinfo->write(PSTORE_TYPE_DMESG, reason, &id, part,
  121. oopscount, hsize + len, psinfo);
  122. if (ret == 0 && reason == KMSG_DUMP_OOPS && pstore_is_mounted())
  123. pstore_new_entry = 1;
  124. total += hsize + len;
  125. part++;
  126. }
  127. if (in_nmi()) {
  128. if (is_locked)
  129. spin_unlock(&psinfo->buf_lock);
  130. } else
  131. spin_unlock_irqrestore(&psinfo->buf_lock, flags);
  132. }
  133. static struct kmsg_dumper pstore_dumper = {
  134. .dump = pstore_dump,
  135. };
  136. #ifdef CONFIG_PSTORE_CONSOLE
  137. static void pstore_console_write(struct console *con, const char *s, unsigned c)
  138. {
  139. const char *e = s + c;
  140. while (s < e) {
  141. unsigned long flags;
  142. u64 id;
  143. if (c > psinfo->bufsize)
  144. c = psinfo->bufsize;
  145. if (oops_in_progress) {
  146. if (!spin_trylock_irqsave(&psinfo->buf_lock, flags))
  147. break;
  148. } else {
  149. spin_lock_irqsave(&psinfo->buf_lock, flags);
  150. }
  151. memcpy(psinfo->buf, s, c);
  152. psinfo->write(PSTORE_TYPE_CONSOLE, 0, &id, 0, 0, c, psinfo);
  153. spin_unlock_irqrestore(&psinfo->buf_lock, flags);
  154. s += c;
  155. c = e - s;
  156. }
  157. }
  158. static struct console pstore_console = {
  159. .name = "pstore",
  160. .write = pstore_console_write,
  161. .flags = CON_PRINTBUFFER | CON_ENABLED | CON_ANYTIME,
  162. .index = -1,
  163. };
  164. static void pstore_register_console(void)
  165. {
  166. register_console(&pstore_console);
  167. }
  168. #else
  169. static void pstore_register_console(void) {}
  170. #endif
  171. static int pstore_write_compat(enum pstore_type_id type,
  172. enum kmsg_dump_reason reason,
  173. u64 *id, unsigned int part, int count,
  174. size_t size, struct pstore_info *psi)
  175. {
  176. return psi->write_buf(type, reason, id, part, psinfo->buf, size, psi);
  177. }
  178. /*
  179. * platform specific persistent storage driver registers with
  180. * us here. If pstore is already mounted, call the platform
  181. * read function right away to populate the file system. If not
  182. * then the pstore mount code will call us later to fill out
  183. * the file system.
  184. *
  185. * Register with kmsg_dump to save last part of console log on panic.
  186. */
  187. int pstore_register(struct pstore_info *psi)
  188. {
  189. struct module *owner = psi->owner;
  190. spin_lock(&pstore_lock);
  191. if (psinfo) {
  192. spin_unlock(&pstore_lock);
  193. return -EBUSY;
  194. }
  195. if (backend && strcmp(backend, psi->name)) {
  196. spin_unlock(&pstore_lock);
  197. return -EINVAL;
  198. }
  199. if (!psi->write)
  200. psi->write = pstore_write_compat;
  201. psinfo = psi;
  202. mutex_init(&psinfo->read_mutex);
  203. spin_unlock(&pstore_lock);
  204. if (owner && !try_module_get(owner)) {
  205. psinfo = NULL;
  206. return -EINVAL;
  207. }
  208. if (pstore_is_mounted())
  209. pstore_get_records(0);
  210. kmsg_dump_register(&pstore_dumper);
  211. pstore_register_console();
  212. pstore_register_ftrace();
  213. if (pstore_update_ms >= 0) {
  214. pstore_timer.expires = jiffies +
  215. msecs_to_jiffies(pstore_update_ms);
  216. add_timer(&pstore_timer);
  217. }
  218. return 0;
  219. }
  220. EXPORT_SYMBOL_GPL(pstore_register);
  221. /*
  222. * Read all the records from the persistent store. Create
  223. * files in our filesystem. Don't warn about -EEXIST errors
  224. * when we are re-scanning the backing store looking to add new
  225. * error records.
  226. */
  227. void pstore_get_records(int quiet)
  228. {
  229. struct pstore_info *psi = psinfo;
  230. char *buf = NULL;
  231. ssize_t size;
  232. u64 id;
  233. int count;
  234. enum pstore_type_id type;
  235. struct timespec time;
  236. int failed = 0, rc;
  237. if (!psi)
  238. return;
  239. mutex_lock(&psi->read_mutex);
  240. if (psi->open && psi->open(psi))
  241. goto out;
  242. while ((size = psi->read(&id, &type, &count, &time, &buf, psi)) > 0) {
  243. rc = pstore_mkfile(type, psi->name, id, count, buf,
  244. (size_t)size, time, psi);
  245. kfree(buf);
  246. buf = NULL;
  247. if (rc && (rc != -EEXIST || !quiet))
  248. failed++;
  249. }
  250. if (psi->close)
  251. psi->close(psi);
  252. out:
  253. mutex_unlock(&psi->read_mutex);
  254. if (failed)
  255. printk(KERN_WARNING "pstore: failed to load %d record(s) from '%s'\n",
  256. failed, psi->name);
  257. }
  258. static void pstore_dowork(struct work_struct *work)
  259. {
  260. pstore_get_records(1);
  261. }
  262. static void pstore_timefunc(unsigned long dummy)
  263. {
  264. if (pstore_new_entry) {
  265. pstore_new_entry = 0;
  266. schedule_work(&pstore_work);
  267. }
  268. mod_timer(&pstore_timer, jiffies + msecs_to_jiffies(pstore_update_ms));
  269. }
  270. module_param(backend, charp, 0444);
  271. MODULE_PARM_DESC(backend, "Pstore backend to use");