platform.c 13 KB

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