platform.c 13 KB

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