platform.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  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. /* Derived from logfs_uncompress */
  142. static int pstore_decompress(void *in, void *out, size_t inlen, size_t outlen)
  143. {
  144. int err, ret;
  145. ret = -EIO;
  146. err = zlib_inflateInit(&stream);
  147. if (err != Z_OK)
  148. goto error;
  149. stream.next_in = in;
  150. stream.avail_in = inlen;
  151. stream.total_in = 0;
  152. stream.next_out = out;
  153. stream.avail_out = outlen;
  154. stream.total_out = 0;
  155. err = zlib_inflate(&stream, Z_FINISH);
  156. if (err != Z_STREAM_END)
  157. goto error;
  158. err = zlib_inflateEnd(&stream);
  159. if (err != Z_OK)
  160. goto error;
  161. ret = stream.total_out;
  162. error:
  163. return ret;
  164. }
  165. static void allocate_buf_for_compression(void)
  166. {
  167. size_t size;
  168. big_oops_buf_sz = (psinfo->bufsize * 100) / 45;
  169. big_oops_buf = kmalloc(big_oops_buf_sz, GFP_KERNEL);
  170. if (big_oops_buf) {
  171. size = max(zlib_deflate_workspacesize(WINDOW_BITS, MEM_LEVEL),
  172. zlib_inflate_workspacesize());
  173. stream.workspace = kmalloc(size, GFP_KERNEL);
  174. if (!stream.workspace) {
  175. pr_err("pstore: No memory for compression workspace; "
  176. "skipping compression\n");
  177. kfree(big_oops_buf);
  178. big_oops_buf = NULL;
  179. }
  180. } else {
  181. pr_err("No memory for uncompressed data; "
  182. "skipping compression\n");
  183. stream.workspace = NULL;
  184. }
  185. }
  186. /*
  187. * Called when compression fails, since the printk buffer
  188. * would be fetched for compression calling it again when
  189. * compression fails would have moved the iterator of
  190. * printk buffer which results in fetching old contents.
  191. * Copy the recent messages from big_oops_buf to psinfo->buf
  192. */
  193. static size_t copy_kmsg_to_buffer(int hsize, size_t len)
  194. {
  195. size_t total_len;
  196. size_t diff;
  197. total_len = hsize + len;
  198. if (total_len > psinfo->bufsize) {
  199. diff = total_len - psinfo->bufsize + hsize;
  200. memcpy(psinfo->buf, big_oops_buf, hsize);
  201. memcpy(psinfo->buf + hsize, big_oops_buf + diff,
  202. psinfo->bufsize - hsize);
  203. total_len = psinfo->bufsize;
  204. } else
  205. memcpy(psinfo->buf, big_oops_buf, total_len);
  206. return total_len;
  207. }
  208. /*
  209. * callback from kmsg_dump. (s2,l2) has the most recently
  210. * written bytes, older bytes are in (s1,l1). Save as much
  211. * as we can from the end of the buffer.
  212. */
  213. static void pstore_dump(struct kmsg_dumper *dumper,
  214. enum kmsg_dump_reason reason)
  215. {
  216. unsigned long total = 0;
  217. const char *why;
  218. u64 id;
  219. unsigned int part = 1;
  220. unsigned long flags = 0;
  221. int is_locked = 0;
  222. int ret;
  223. why = get_reason_str(reason);
  224. if (pstore_cannot_block_path(reason)) {
  225. is_locked = spin_trylock_irqsave(&psinfo->buf_lock, flags);
  226. if (!is_locked) {
  227. pr_err("pstore dump routine blocked in %s path, may corrupt error record\n"
  228. , in_nmi() ? "NMI" : why);
  229. }
  230. } else
  231. spin_lock_irqsave(&psinfo->buf_lock, flags);
  232. oopscount++;
  233. while (total < kmsg_bytes) {
  234. char *dst;
  235. unsigned long size;
  236. int hsize;
  237. int zipped_len = -1;
  238. size_t len;
  239. bool compressed;
  240. size_t total_len;
  241. if (big_oops_buf) {
  242. dst = big_oops_buf;
  243. hsize = sprintf(dst, "%s#%d Part%d\n", why,
  244. oopscount, part);
  245. size = big_oops_buf_sz - hsize;
  246. if (!kmsg_dump_get_buffer(dumper, true, dst + hsize,
  247. size, &len))
  248. break;
  249. zipped_len = pstore_compress(dst, psinfo->buf,
  250. hsize + len, psinfo->bufsize);
  251. if (zipped_len > 0) {
  252. compressed = true;
  253. total_len = zipped_len;
  254. } else {
  255. pr_err("pstore: compression failed for Part %d"
  256. " returned %d\n", part, zipped_len);
  257. pr_err("pstore: Capture uncompressed"
  258. " oops/panic report of Part %d\n", part);
  259. compressed = false;
  260. total_len = copy_kmsg_to_buffer(hsize, len);
  261. }
  262. } else {
  263. dst = psinfo->buf;
  264. hsize = sprintf(dst, "%s#%d Part%d\n", why, oopscount,
  265. part);
  266. size = psinfo->bufsize - hsize;
  267. dst += hsize;
  268. if (!kmsg_dump_get_buffer(dumper, true, dst,
  269. size, &len))
  270. break;
  271. compressed = false;
  272. total_len = hsize + len;
  273. }
  274. ret = psinfo->write(PSTORE_TYPE_DMESG, reason, &id, part,
  275. oopscount, compressed, total_len, psinfo);
  276. if (ret == 0 && reason == KMSG_DUMP_OOPS && pstore_is_mounted())
  277. pstore_new_entry = 1;
  278. total += total_len;
  279. part++;
  280. }
  281. if (pstore_cannot_block_path(reason)) {
  282. if (is_locked)
  283. spin_unlock_irqrestore(&psinfo->buf_lock, flags);
  284. } else
  285. spin_unlock_irqrestore(&psinfo->buf_lock, flags);
  286. }
  287. static struct kmsg_dumper pstore_dumper = {
  288. .dump = pstore_dump,
  289. };
  290. #ifdef CONFIG_PSTORE_CONSOLE
  291. static void pstore_console_write(struct console *con, const char *s, unsigned c)
  292. {
  293. const char *e = s + c;
  294. while (s < e) {
  295. unsigned long flags;
  296. u64 id;
  297. if (c > psinfo->bufsize)
  298. c = psinfo->bufsize;
  299. if (oops_in_progress) {
  300. if (!spin_trylock_irqsave(&psinfo->buf_lock, flags))
  301. break;
  302. } else {
  303. spin_lock_irqsave(&psinfo->buf_lock, flags);
  304. }
  305. memcpy(psinfo->buf, s, c);
  306. psinfo->write(PSTORE_TYPE_CONSOLE, 0, &id, 0, 0, 0, c, psinfo);
  307. spin_unlock_irqrestore(&psinfo->buf_lock, flags);
  308. s += c;
  309. c = e - s;
  310. }
  311. }
  312. static struct console pstore_console = {
  313. .name = "pstore",
  314. .write = pstore_console_write,
  315. .flags = CON_PRINTBUFFER | CON_ENABLED | CON_ANYTIME,
  316. .index = -1,
  317. };
  318. static void pstore_register_console(void)
  319. {
  320. register_console(&pstore_console);
  321. }
  322. #else
  323. static void pstore_register_console(void) {}
  324. #endif
  325. static int pstore_write_compat(enum pstore_type_id type,
  326. enum kmsg_dump_reason reason,
  327. u64 *id, unsigned int part, int count,
  328. bool compressed, size_t size,
  329. struct pstore_info *psi)
  330. {
  331. return psi->write_buf(type, reason, id, part, psinfo->buf, compressed,
  332. size, psi);
  333. }
  334. /*
  335. * platform specific persistent storage driver registers with
  336. * us here. If pstore is already mounted, call the platform
  337. * read function right away to populate the file system. If not
  338. * then the pstore mount code will call us later to fill out
  339. * the file system.
  340. *
  341. * Register with kmsg_dump to save last part of console log on panic.
  342. */
  343. int pstore_register(struct pstore_info *psi)
  344. {
  345. struct module *owner = psi->owner;
  346. if (backend && strcmp(backend, psi->name))
  347. return -EPERM;
  348. spin_lock(&pstore_lock);
  349. if (psinfo) {
  350. spin_unlock(&pstore_lock);
  351. return -EBUSY;
  352. }
  353. if (!psi->write)
  354. psi->write = pstore_write_compat;
  355. psinfo = psi;
  356. mutex_init(&psinfo->read_mutex);
  357. spin_unlock(&pstore_lock);
  358. if (owner && !try_module_get(owner)) {
  359. psinfo = NULL;
  360. return -EINVAL;
  361. }
  362. allocate_buf_for_compression();
  363. if (pstore_is_mounted())
  364. pstore_get_records(0);
  365. kmsg_dump_register(&pstore_dumper);
  366. pstore_register_console();
  367. pstore_register_ftrace();
  368. if (pstore_update_ms >= 0) {
  369. pstore_timer.expires = jiffies +
  370. msecs_to_jiffies(pstore_update_ms);
  371. add_timer(&pstore_timer);
  372. }
  373. pr_info("pstore: Registered %s as persistent store backend\n",
  374. psi->name);
  375. return 0;
  376. }
  377. EXPORT_SYMBOL_GPL(pstore_register);
  378. /*
  379. * Read all the records from the persistent store. Create
  380. * files in our filesystem. Don't warn about -EEXIST errors
  381. * when we are re-scanning the backing store looking to add new
  382. * error records.
  383. */
  384. void pstore_get_records(int quiet)
  385. {
  386. struct pstore_info *psi = psinfo;
  387. char *buf = NULL;
  388. ssize_t size;
  389. u64 id;
  390. int count;
  391. enum pstore_type_id type;
  392. struct timespec time;
  393. int failed = 0, rc;
  394. bool compressed;
  395. int unzipped_len = -1;
  396. if (!psi)
  397. return;
  398. mutex_lock(&psi->read_mutex);
  399. if (psi->open && psi->open(psi))
  400. goto out;
  401. while ((size = psi->read(&id, &type, &count, &time, &buf, &compressed,
  402. psi)) > 0) {
  403. if (compressed && (type == PSTORE_TYPE_DMESG)) {
  404. if (big_oops_buf)
  405. unzipped_len = pstore_decompress(buf,
  406. big_oops_buf, size,
  407. big_oops_buf_sz);
  408. if (unzipped_len > 0) {
  409. buf = big_oops_buf;
  410. size = unzipped_len;
  411. compressed = false;
  412. } else {
  413. pr_err("pstore: decompression failed;"
  414. "returned %d\n", unzipped_len);
  415. compressed = true;
  416. }
  417. }
  418. rc = pstore_mkfile(type, psi->name, id, count, buf,
  419. compressed, (size_t)size, time, psi);
  420. if (unzipped_len < 0) {
  421. /* Free buffer other than big oops */
  422. kfree(buf);
  423. buf = NULL;
  424. } else
  425. unzipped_len = -1;
  426. if (rc && (rc != -EEXIST || !quiet))
  427. failed++;
  428. }
  429. if (psi->close)
  430. psi->close(psi);
  431. out:
  432. mutex_unlock(&psi->read_mutex);
  433. if (failed)
  434. printk(KERN_WARNING "pstore: failed to load %d record(s) from '%s'\n",
  435. failed, psi->name);
  436. }
  437. static void pstore_dowork(struct work_struct *work)
  438. {
  439. pstore_get_records(1);
  440. }
  441. static void pstore_timefunc(unsigned long dummy)
  442. {
  443. if (pstore_new_entry) {
  444. pstore_new_entry = 0;
  445. schedule_work(&pstore_work);
  446. }
  447. mod_timer(&pstore_timer, jiffies + msecs_to_jiffies(pstore_update_ms));
  448. }
  449. module_param(backend, charp, 0444);
  450. MODULE_PARM_DESC(backend, "Pstore backend to use");