platform.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. bool compressed = false;
  136. dst = psinfo->buf;
  137. hsize = sprintf(dst, "%s#%d Part%d\n", why, oopscount, part);
  138. size = psinfo->bufsize - hsize;
  139. dst += hsize;
  140. if (!kmsg_dump_get_buffer(dumper, true, dst, size, &len))
  141. break;
  142. ret = psinfo->write(PSTORE_TYPE_DMESG, reason, &id, part,
  143. oopscount, compressed, hsize + len, psinfo);
  144. if (ret == 0 && reason == KMSG_DUMP_OOPS && pstore_is_mounted())
  145. pstore_new_entry = 1;
  146. total += hsize + len;
  147. part++;
  148. }
  149. if (pstore_cannot_block_path(reason)) {
  150. if (is_locked)
  151. spin_unlock_irqrestore(&psinfo->buf_lock, flags);
  152. } else
  153. spin_unlock_irqrestore(&psinfo->buf_lock, flags);
  154. }
  155. static struct kmsg_dumper pstore_dumper = {
  156. .dump = pstore_dump,
  157. };
  158. #ifdef CONFIG_PSTORE_CONSOLE
  159. static void pstore_console_write(struct console *con, const char *s, unsigned c)
  160. {
  161. const char *e = s + c;
  162. while (s < e) {
  163. unsigned long flags;
  164. u64 id;
  165. if (c > psinfo->bufsize)
  166. c = psinfo->bufsize;
  167. if (oops_in_progress) {
  168. if (!spin_trylock_irqsave(&psinfo->buf_lock, flags))
  169. break;
  170. } else {
  171. spin_lock_irqsave(&psinfo->buf_lock, flags);
  172. }
  173. memcpy(psinfo->buf, s, c);
  174. psinfo->write(PSTORE_TYPE_CONSOLE, 0, &id, 0, 0, 0, c, psinfo);
  175. spin_unlock_irqrestore(&psinfo->buf_lock, flags);
  176. s += c;
  177. c = e - s;
  178. }
  179. }
  180. static struct console pstore_console = {
  181. .name = "pstore",
  182. .write = pstore_console_write,
  183. .flags = CON_PRINTBUFFER | CON_ENABLED | CON_ANYTIME,
  184. .index = -1,
  185. };
  186. static void pstore_register_console(void)
  187. {
  188. register_console(&pstore_console);
  189. }
  190. #else
  191. static void pstore_register_console(void) {}
  192. #endif
  193. static int pstore_write_compat(enum pstore_type_id type,
  194. enum kmsg_dump_reason reason,
  195. u64 *id, unsigned int part, int count,
  196. bool compressed, size_t size,
  197. struct pstore_info *psi)
  198. {
  199. return psi->write_buf(type, reason, id, part, psinfo->buf, compressed,
  200. size, psi);
  201. }
  202. /*
  203. * platform specific persistent storage driver registers with
  204. * us here. If pstore is already mounted, call the platform
  205. * read function right away to populate the file system. If not
  206. * then the pstore mount code will call us later to fill out
  207. * the file system.
  208. *
  209. * Register with kmsg_dump to save last part of console log on panic.
  210. */
  211. int pstore_register(struct pstore_info *psi)
  212. {
  213. struct module *owner = psi->owner;
  214. if (backend && strcmp(backend, psi->name))
  215. return -EPERM;
  216. spin_lock(&pstore_lock);
  217. if (psinfo) {
  218. spin_unlock(&pstore_lock);
  219. return -EBUSY;
  220. }
  221. if (!psi->write)
  222. psi->write = pstore_write_compat;
  223. psinfo = psi;
  224. mutex_init(&psinfo->read_mutex);
  225. spin_unlock(&pstore_lock);
  226. if (owner && !try_module_get(owner)) {
  227. psinfo = NULL;
  228. return -EINVAL;
  229. }
  230. if (pstore_is_mounted())
  231. pstore_get_records(0);
  232. kmsg_dump_register(&pstore_dumper);
  233. pstore_register_console();
  234. pstore_register_ftrace();
  235. if (pstore_update_ms >= 0) {
  236. pstore_timer.expires = jiffies +
  237. msecs_to_jiffies(pstore_update_ms);
  238. add_timer(&pstore_timer);
  239. }
  240. pr_info("pstore: Registered %s as persistent store backend\n",
  241. psi->name);
  242. return 0;
  243. }
  244. EXPORT_SYMBOL_GPL(pstore_register);
  245. /*
  246. * Read all the records from the persistent store. Create
  247. * files in our filesystem. Don't warn about -EEXIST errors
  248. * when we are re-scanning the backing store looking to add new
  249. * error records.
  250. */
  251. void pstore_get_records(int quiet)
  252. {
  253. struct pstore_info *psi = psinfo;
  254. char *buf = NULL;
  255. ssize_t size;
  256. u64 id;
  257. int count;
  258. enum pstore_type_id type;
  259. struct timespec time;
  260. int failed = 0, rc;
  261. if (!psi)
  262. return;
  263. mutex_lock(&psi->read_mutex);
  264. if (psi->open && psi->open(psi))
  265. goto out;
  266. while ((size = psi->read(&id, &type, &count, &time, &buf, psi)) > 0) {
  267. rc = pstore_mkfile(type, psi->name, id, count, buf,
  268. (size_t)size, time, psi);
  269. kfree(buf);
  270. buf = NULL;
  271. if (rc && (rc != -EEXIST || !quiet))
  272. failed++;
  273. }
  274. if (psi->close)
  275. psi->close(psi);
  276. out:
  277. mutex_unlock(&psi->read_mutex);
  278. if (failed)
  279. printk(KERN_WARNING "pstore: failed to load %d record(s) from '%s'\n",
  280. failed, psi->name);
  281. }
  282. static void pstore_dowork(struct work_struct *work)
  283. {
  284. pstore_get_records(1);
  285. }
  286. static void pstore_timefunc(unsigned long dummy)
  287. {
  288. if (pstore_new_entry) {
  289. pstore_new_entry = 0;
  290. schedule_work(&pstore_work);
  291. }
  292. mod_timer(&pstore_timer, jiffies + msecs_to_jiffies(pstore_update_ms));
  293. }
  294. module_param(backend, charp, 0444);
  295. MODULE_PARM_DESC(backend, "Pstore backend to use");