platform.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. bool pstore_cannot_block_path(enum kmsg_dump_reason reason)
  87. {
  88. /*
  89. * In case of NMI path, pstore shouldn't be blocked
  90. * regardless of reason.
  91. */
  92. if (in_nmi())
  93. return true;
  94. switch (reason) {
  95. /* In panic case, other cpus are stopped by smp_send_stop(). */
  96. case KMSG_DUMP_PANIC:
  97. /* Emergency restart shouldn't be blocked by spin lock. */
  98. case KMSG_DUMP_EMERG:
  99. return true;
  100. default:
  101. return false;
  102. }
  103. }
  104. EXPORT_SYMBOL_GPL(pstore_cannot_block_path);
  105. /*
  106. * callback from kmsg_dump. (s2,l2) has the most recently
  107. * written bytes, older bytes are in (s1,l1). Save as much
  108. * as we can from the end of the buffer.
  109. */
  110. static void pstore_dump(struct kmsg_dumper *dumper,
  111. enum kmsg_dump_reason reason)
  112. {
  113. unsigned long total = 0;
  114. const char *why;
  115. u64 id;
  116. unsigned int part = 1;
  117. unsigned long flags = 0;
  118. int is_locked = 0;
  119. int ret;
  120. why = get_reason_str(reason);
  121. if (pstore_cannot_block_path(reason)) {
  122. is_locked = spin_trylock_irqsave(&psinfo->buf_lock, flags);
  123. if (!is_locked) {
  124. pr_err("pstore dump routine blocked in %s path, may corrupt error record\n"
  125. , in_nmi() ? "NMI" : why);
  126. }
  127. } else
  128. spin_lock_irqsave(&psinfo->buf_lock, flags);
  129. oopscount++;
  130. while (total < kmsg_bytes) {
  131. char *dst;
  132. unsigned long size;
  133. int hsize;
  134. size_t len;
  135. dst = psinfo->buf;
  136. hsize = sprintf(dst, "%s#%d Part%d\n", why, oopscount, part);
  137. size = psinfo->bufsize - hsize;
  138. dst += hsize;
  139. if (!kmsg_dump_get_buffer(dumper, true, dst, size, &len))
  140. break;
  141. ret = psinfo->write(PSTORE_TYPE_DMESG, reason, &id, part,
  142. oopscount, hsize + len, psinfo);
  143. if (ret == 0 && reason == KMSG_DUMP_OOPS && pstore_is_mounted())
  144. pstore_new_entry = 1;
  145. total += hsize + len;
  146. part++;
  147. }
  148. if (pstore_cannot_block_path(reason)) {
  149. if (is_locked)
  150. spin_unlock_irqrestore(&psinfo->buf_lock, flags);
  151. } else
  152. spin_unlock_irqrestore(&psinfo->buf_lock, flags);
  153. }
  154. static struct kmsg_dumper pstore_dumper = {
  155. .dump = pstore_dump,
  156. };
  157. #ifdef CONFIG_PSTORE_CONSOLE
  158. static void pstore_console_write(struct console *con, const char *s, unsigned c)
  159. {
  160. const char *e = s + c;
  161. while (s < e) {
  162. unsigned long flags;
  163. u64 id;
  164. if (c > psinfo->bufsize)
  165. c = psinfo->bufsize;
  166. if (oops_in_progress) {
  167. if (!spin_trylock_irqsave(&psinfo->buf_lock, flags))
  168. break;
  169. } else {
  170. spin_lock_irqsave(&psinfo->buf_lock, flags);
  171. }
  172. memcpy(psinfo->buf, s, c);
  173. psinfo->write(PSTORE_TYPE_CONSOLE, 0, &id, 0, 0, c, psinfo);
  174. spin_unlock_irqrestore(&psinfo->buf_lock, flags);
  175. s += c;
  176. c = e - s;
  177. }
  178. }
  179. static struct console pstore_console = {
  180. .name = "pstore",
  181. .write = pstore_console_write,
  182. .flags = CON_PRINTBUFFER | CON_ENABLED | CON_ANYTIME,
  183. .index = -1,
  184. };
  185. static void pstore_register_console(void)
  186. {
  187. register_console(&pstore_console);
  188. }
  189. #else
  190. static void pstore_register_console(void) {}
  191. #endif
  192. static int pstore_write_compat(enum pstore_type_id type,
  193. enum kmsg_dump_reason reason,
  194. u64 *id, unsigned int part, int count,
  195. size_t size, struct pstore_info *psi)
  196. {
  197. return psi->write_buf(type, reason, id, part, psinfo->buf, size, psi);
  198. }
  199. /*
  200. * platform specific persistent storage driver registers with
  201. * us here. If pstore is already mounted, call the platform
  202. * read function right away to populate the file system. If not
  203. * then the pstore mount code will call us later to fill out
  204. * the file system.
  205. *
  206. * Register with kmsg_dump to save last part of console log on panic.
  207. */
  208. int pstore_register(struct pstore_info *psi)
  209. {
  210. struct module *owner = psi->owner;
  211. spin_lock(&pstore_lock);
  212. if (psinfo) {
  213. spin_unlock(&pstore_lock);
  214. return -EBUSY;
  215. }
  216. if (backend && strcmp(backend, psi->name)) {
  217. spin_unlock(&pstore_lock);
  218. return -EINVAL;
  219. }
  220. if (!psi->write)
  221. psi->write = pstore_write_compat;
  222. psinfo = psi;
  223. mutex_init(&psinfo->read_mutex);
  224. spin_unlock(&pstore_lock);
  225. if (owner && !try_module_get(owner)) {
  226. psinfo = NULL;
  227. return -EINVAL;
  228. }
  229. if (pstore_is_mounted())
  230. pstore_get_records(0);
  231. kmsg_dump_register(&pstore_dumper);
  232. pstore_register_console();
  233. pstore_register_ftrace();
  234. if (pstore_update_ms >= 0) {
  235. pstore_timer.expires = jiffies +
  236. msecs_to_jiffies(pstore_update_ms);
  237. add_timer(&pstore_timer);
  238. }
  239. return 0;
  240. }
  241. EXPORT_SYMBOL_GPL(pstore_register);
  242. /*
  243. * Read all the records from the persistent store. Create
  244. * files in our filesystem. Don't warn about -EEXIST errors
  245. * when we are re-scanning the backing store looking to add new
  246. * error records.
  247. */
  248. void pstore_get_records(int quiet)
  249. {
  250. struct pstore_info *psi = psinfo;
  251. char *buf = NULL;
  252. ssize_t size;
  253. u64 id;
  254. int count;
  255. enum pstore_type_id type;
  256. struct timespec time;
  257. int failed = 0, rc;
  258. if (!psi)
  259. return;
  260. mutex_lock(&psi->read_mutex);
  261. if (psi->open && psi->open(psi))
  262. goto out;
  263. while ((size = psi->read(&id, &type, &count, &time, &buf, psi)) > 0) {
  264. rc = pstore_mkfile(type, psi->name, id, count, buf,
  265. (size_t)size, time, psi);
  266. kfree(buf);
  267. buf = NULL;
  268. if (rc && (rc != -EEXIST || !quiet))
  269. failed++;
  270. }
  271. if (psi->close)
  272. psi->close(psi);
  273. out:
  274. mutex_unlock(&psi->read_mutex);
  275. if (failed)
  276. printk(KERN_WARNING "pstore: failed to load %d record(s) from '%s'\n",
  277. failed, psi->name);
  278. }
  279. static void pstore_dowork(struct work_struct *work)
  280. {
  281. pstore_get_records(1);
  282. }
  283. static void pstore_timefunc(unsigned long dummy)
  284. {
  285. if (pstore_new_entry) {
  286. pstore_new_entry = 0;
  287. schedule_work(&pstore_work);
  288. }
  289. mod_timer(&pstore_timer, jiffies + msecs_to_jiffies(pstore_update_ms));
  290. }
  291. module_param(backend, charp, 0444);
  292. MODULE_PARM_DESC(backend, "Pstore backend to use");