platform.c 13 KB

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