platform.c 13 KB

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