platform.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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/zlib.h>
  29. #include <linux/string.h>
  30. #include <linux/timer.h>
  31. #include <linux/slab.h>
  32. #include <linux/uaccess.h>
  33. #include <linux/hardirq.h>
  34. #include <linux/jiffies.h>
  35. #include <linux/workqueue.h>
  36. #include "internal.h"
  37. /*
  38. * We defer making "oops" entries appear in pstore - see
  39. * whether the system is actually still running well enough
  40. * to let someone see the entry
  41. */
  42. static int pstore_update_ms = -1;
  43. module_param_named(update_ms, pstore_update_ms, int, 0600);
  44. MODULE_PARM_DESC(update_ms, "milliseconds before pstore updates its content "
  45. "(default is -1, which means runtime updates are disabled; "
  46. "enabling this option is not safe, it may lead to further "
  47. "corruption on Oopses)");
  48. static int pstore_new_entry;
  49. static void pstore_timefunc(unsigned long);
  50. static DEFINE_TIMER(pstore_timer, pstore_timefunc, 0, 0);
  51. static void pstore_dowork(struct work_struct *);
  52. static DECLARE_WORK(pstore_work, pstore_dowork);
  53. /*
  54. * pstore_lock just protects "psinfo" during
  55. * calls to pstore_register()
  56. */
  57. static DEFINE_SPINLOCK(pstore_lock);
  58. struct pstore_info *psinfo;
  59. static char *backend;
  60. /* Compression parameters */
  61. #define COMPR_LEVEL 6
  62. #define WINDOW_BITS 12
  63. #define MEM_LEVEL 4
  64. static struct z_stream_s stream;
  65. static char *big_oops_buf;
  66. static size_t big_oops_buf_sz;
  67. /* How much of the console log to snapshot */
  68. static unsigned long kmsg_bytes = 10240;
  69. void pstore_set_kmsg_bytes(int bytes)
  70. {
  71. kmsg_bytes = bytes;
  72. }
  73. /* Tag each group of saved records with a sequence number */
  74. static int oopscount;
  75. static const char *get_reason_str(enum kmsg_dump_reason reason)
  76. {
  77. switch (reason) {
  78. case KMSG_DUMP_PANIC:
  79. return "Panic";
  80. case KMSG_DUMP_OOPS:
  81. return "Oops";
  82. case KMSG_DUMP_EMERG:
  83. return "Emergency";
  84. case KMSG_DUMP_RESTART:
  85. return "Restart";
  86. case KMSG_DUMP_HALT:
  87. return "Halt";
  88. case KMSG_DUMP_POWEROFF:
  89. return "Poweroff";
  90. default:
  91. return "Unknown";
  92. }
  93. }
  94. bool pstore_cannot_block_path(enum kmsg_dump_reason reason)
  95. {
  96. /*
  97. * In case of NMI path, pstore shouldn't be blocked
  98. * regardless of reason.
  99. */
  100. if (in_nmi())
  101. return true;
  102. switch (reason) {
  103. /* In panic case, other cpus are stopped by smp_send_stop(). */
  104. case KMSG_DUMP_PANIC:
  105. /* Emergency restart shouldn't be blocked by spin lock. */
  106. case KMSG_DUMP_EMERG:
  107. return true;
  108. default:
  109. return false;
  110. }
  111. }
  112. EXPORT_SYMBOL_GPL(pstore_cannot_block_path);
  113. /* Derived from logfs_compress() */
  114. static int pstore_compress(const void *in, void *out, size_t inlen,
  115. size_t outlen)
  116. {
  117. int err, ret;
  118. ret = -EIO;
  119. err = zlib_deflateInit2(&stream, COMPR_LEVEL, Z_DEFLATED, WINDOW_BITS,
  120. MEM_LEVEL, Z_DEFAULT_STRATEGY);
  121. if (err != Z_OK)
  122. goto error;
  123. stream.next_in = in;
  124. stream.avail_in = inlen;
  125. stream.total_in = 0;
  126. stream.next_out = out;
  127. stream.avail_out = outlen;
  128. stream.total_out = 0;
  129. err = zlib_deflate(&stream, Z_FINISH);
  130. if (err != Z_STREAM_END)
  131. goto error;
  132. err = zlib_deflateEnd(&stream);
  133. if (err != Z_OK)
  134. goto error;
  135. if (stream.total_out >= stream.total_in)
  136. goto error;
  137. ret = stream.total_out;
  138. error:
  139. return ret;
  140. }
  141. static void allocate_buf_for_compression(void)
  142. {
  143. size_t size;
  144. big_oops_buf_sz = (psinfo->bufsize * 100) / 45;
  145. big_oops_buf = kmalloc(big_oops_buf_sz, GFP_KERNEL);
  146. if (big_oops_buf) {
  147. size = max(zlib_deflate_workspacesize(WINDOW_BITS, MEM_LEVEL),
  148. zlib_inflate_workspacesize());
  149. stream.workspace = kmalloc(size, GFP_KERNEL);
  150. if (!stream.workspace) {
  151. pr_err("pstore: No memory for compression workspace; "
  152. "skipping compression\n");
  153. kfree(big_oops_buf);
  154. big_oops_buf = NULL;
  155. }
  156. } else {
  157. pr_err("No memory for uncompressed data; "
  158. "skipping compression\n");
  159. stream.workspace = NULL;
  160. }
  161. }
  162. /*
  163. * Called when compression fails, since the printk buffer
  164. * would be fetched for compression calling it again when
  165. * compression fails would have moved the iterator of
  166. * printk buffer which results in fetching old contents.
  167. * Copy the recent messages from big_oops_buf to psinfo->buf
  168. */
  169. static size_t copy_kmsg_to_buffer(int hsize, size_t len)
  170. {
  171. size_t total_len;
  172. size_t diff;
  173. total_len = hsize + len;
  174. if (total_len > psinfo->bufsize) {
  175. diff = total_len - psinfo->bufsize + hsize;
  176. memcpy(psinfo->buf, big_oops_buf, hsize);
  177. memcpy(psinfo->buf + hsize, big_oops_buf + diff,
  178. psinfo->bufsize - hsize);
  179. total_len = psinfo->bufsize;
  180. } else
  181. memcpy(psinfo->buf, big_oops_buf, total_len);
  182. return total_len;
  183. }
  184. /*
  185. * callback from kmsg_dump. (s2,l2) has the most recently
  186. * written bytes, older bytes are in (s1,l1). Save as much
  187. * as we can from the end of the buffer.
  188. */
  189. static void pstore_dump(struct kmsg_dumper *dumper,
  190. enum kmsg_dump_reason reason)
  191. {
  192. unsigned long total = 0;
  193. const char *why;
  194. u64 id;
  195. unsigned int part = 1;
  196. unsigned long flags = 0;
  197. int is_locked = 0;
  198. int ret;
  199. why = get_reason_str(reason);
  200. if (pstore_cannot_block_path(reason)) {
  201. is_locked = spin_trylock_irqsave(&psinfo->buf_lock, flags);
  202. if (!is_locked) {
  203. pr_err("pstore dump routine blocked in %s path, may corrupt error record\n"
  204. , in_nmi() ? "NMI" : why);
  205. }
  206. } else
  207. spin_lock_irqsave(&psinfo->buf_lock, flags);
  208. oopscount++;
  209. while (total < kmsg_bytes) {
  210. char *dst;
  211. unsigned long size;
  212. int hsize;
  213. int zipped_len = -1;
  214. size_t len;
  215. bool compressed;
  216. size_t total_len;
  217. if (big_oops_buf) {
  218. dst = big_oops_buf;
  219. hsize = sprintf(dst, "%s#%d Part%d\n", why,
  220. oopscount, part);
  221. size = big_oops_buf_sz - hsize;
  222. if (!kmsg_dump_get_buffer(dumper, true, dst + hsize,
  223. size, &len))
  224. break;
  225. zipped_len = pstore_compress(dst, psinfo->buf,
  226. hsize + len, psinfo->bufsize);
  227. if (zipped_len > 0) {
  228. compressed = true;
  229. total_len = zipped_len;
  230. } else {
  231. pr_err("pstore: compression failed for Part %d"
  232. " returned %d\n", part, zipped_len);
  233. pr_err("pstore: Capture uncompressed"
  234. " oops/panic report of Part %d\n", part);
  235. compressed = false;
  236. total_len = copy_kmsg_to_buffer(hsize, len);
  237. }
  238. } else {
  239. dst = psinfo->buf;
  240. hsize = sprintf(dst, "%s#%d Part%d\n", why, oopscount,
  241. part);
  242. size = psinfo->bufsize - hsize;
  243. dst += hsize;
  244. if (!kmsg_dump_get_buffer(dumper, true, dst,
  245. size, &len))
  246. break;
  247. compressed = false;
  248. total_len = hsize + len;
  249. }
  250. ret = psinfo->write(PSTORE_TYPE_DMESG, reason, &id, part,
  251. oopscount, compressed, total_len, psinfo);
  252. if (ret == 0 && reason == KMSG_DUMP_OOPS && pstore_is_mounted())
  253. pstore_new_entry = 1;
  254. total += total_len;
  255. part++;
  256. }
  257. if (pstore_cannot_block_path(reason)) {
  258. if (is_locked)
  259. spin_unlock_irqrestore(&psinfo->buf_lock, flags);
  260. } else
  261. spin_unlock_irqrestore(&psinfo->buf_lock, flags);
  262. }
  263. static struct kmsg_dumper pstore_dumper = {
  264. .dump = pstore_dump,
  265. };
  266. #ifdef CONFIG_PSTORE_CONSOLE
  267. static void pstore_console_write(struct console *con, const char *s, unsigned c)
  268. {
  269. const char *e = s + c;
  270. while (s < e) {
  271. unsigned long flags;
  272. u64 id;
  273. if (c > psinfo->bufsize)
  274. c = psinfo->bufsize;
  275. if (oops_in_progress) {
  276. if (!spin_trylock_irqsave(&psinfo->buf_lock, flags))
  277. break;
  278. } else {
  279. spin_lock_irqsave(&psinfo->buf_lock, flags);
  280. }
  281. memcpy(psinfo->buf, s, c);
  282. psinfo->write(PSTORE_TYPE_CONSOLE, 0, &id, 0, 0, 0, c, psinfo);
  283. spin_unlock_irqrestore(&psinfo->buf_lock, flags);
  284. s += c;
  285. c = e - s;
  286. }
  287. }
  288. static struct console pstore_console = {
  289. .name = "pstore",
  290. .write = pstore_console_write,
  291. .flags = CON_PRINTBUFFER | CON_ENABLED | CON_ANYTIME,
  292. .index = -1,
  293. };
  294. static void pstore_register_console(void)
  295. {
  296. register_console(&pstore_console);
  297. }
  298. #else
  299. static void pstore_register_console(void) {}
  300. #endif
  301. static int pstore_write_compat(enum pstore_type_id type,
  302. enum kmsg_dump_reason reason,
  303. u64 *id, unsigned int part, int count,
  304. bool compressed, size_t size,
  305. struct pstore_info *psi)
  306. {
  307. return psi->write_buf(type, reason, id, part, psinfo->buf, compressed,
  308. size, psi);
  309. }
  310. /*
  311. * platform specific persistent storage driver registers with
  312. * us here. If pstore is already mounted, call the platform
  313. * read function right away to populate the file system. If not
  314. * then the pstore mount code will call us later to fill out
  315. * the file system.
  316. *
  317. * Register with kmsg_dump to save last part of console log on panic.
  318. */
  319. int pstore_register(struct pstore_info *psi)
  320. {
  321. struct module *owner = psi->owner;
  322. if (backend && strcmp(backend, psi->name))
  323. return -EPERM;
  324. spin_lock(&pstore_lock);
  325. if (psinfo) {
  326. spin_unlock(&pstore_lock);
  327. return -EBUSY;
  328. }
  329. if (!psi->write)
  330. psi->write = pstore_write_compat;
  331. psinfo = psi;
  332. mutex_init(&psinfo->read_mutex);
  333. spin_unlock(&pstore_lock);
  334. if (owner && !try_module_get(owner)) {
  335. psinfo = NULL;
  336. return -EINVAL;
  337. }
  338. allocate_buf_for_compression();
  339. if (pstore_is_mounted())
  340. pstore_get_records(0);
  341. kmsg_dump_register(&pstore_dumper);
  342. pstore_register_console();
  343. pstore_register_ftrace();
  344. if (pstore_update_ms >= 0) {
  345. pstore_timer.expires = jiffies +
  346. msecs_to_jiffies(pstore_update_ms);
  347. add_timer(&pstore_timer);
  348. }
  349. pr_info("pstore: Registered %s as persistent store backend\n",
  350. psi->name);
  351. return 0;
  352. }
  353. EXPORT_SYMBOL_GPL(pstore_register);
  354. /*
  355. * Read all the records from the persistent store. Create
  356. * files in our filesystem. Don't warn about -EEXIST errors
  357. * when we are re-scanning the backing store looking to add new
  358. * error records.
  359. */
  360. void pstore_get_records(int quiet)
  361. {
  362. struct pstore_info *psi = psinfo;
  363. char *buf = NULL;
  364. ssize_t size;
  365. u64 id;
  366. int count;
  367. enum pstore_type_id type;
  368. struct timespec time;
  369. int failed = 0, rc;
  370. bool compressed;
  371. if (!psi)
  372. return;
  373. mutex_lock(&psi->read_mutex);
  374. if (psi->open && psi->open(psi))
  375. goto out;
  376. while ((size = psi->read(&id, &type, &count, &time, &buf, &compressed,
  377. psi)) > 0) {
  378. rc = pstore_mkfile(type, psi->name, id, count, buf,
  379. (size_t)size, time, psi);
  380. kfree(buf);
  381. buf = NULL;
  382. if (rc && (rc != -EEXIST || !quiet))
  383. failed++;
  384. }
  385. if (psi->close)
  386. psi->close(psi);
  387. out:
  388. mutex_unlock(&psi->read_mutex);
  389. if (failed)
  390. printk(KERN_WARNING "pstore: failed to load %d record(s) from '%s'\n",
  391. failed, psi->name);
  392. }
  393. static void pstore_dowork(struct work_struct *work)
  394. {
  395. pstore_get_records(1);
  396. }
  397. static void pstore_timefunc(unsigned long dummy)
  398. {
  399. if (pstore_new_entry) {
  400. pstore_new_entry = 0;
  401. schedule_work(&pstore_work);
  402. }
  403. mod_timer(&pstore_timer, jiffies + msecs_to_jiffies(pstore_update_ms));
  404. }
  405. module_param(backend, charp, 0444);
  406. MODULE_PARM_DESC(backend, "Pstore backend to use");