platform.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  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_inflateInit2(&stream, WINDOW_BITS);
  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. compressed = false;
  276. total_len = copy_kmsg_to_buffer(hsize, len);
  277. }
  278. } else {
  279. dst = psinfo->buf;
  280. hsize = sprintf(dst, "%s#%d Part%d\n", why, oopscount,
  281. part);
  282. size = psinfo->bufsize - hsize;
  283. dst += hsize;
  284. if (!kmsg_dump_get_buffer(dumper, true, dst,
  285. size, &len))
  286. break;
  287. compressed = false;
  288. total_len = hsize + len;
  289. }
  290. ret = psinfo->write(PSTORE_TYPE_DMESG, reason, &id, part,
  291. oopscount, compressed, total_len, psinfo);
  292. if (ret == 0 && reason == KMSG_DUMP_OOPS && pstore_is_mounted())
  293. pstore_new_entry = 1;
  294. total += total_len;
  295. part++;
  296. }
  297. if (pstore_cannot_block_path(reason)) {
  298. if (is_locked)
  299. spin_unlock_irqrestore(&psinfo->buf_lock, flags);
  300. } else
  301. spin_unlock_irqrestore(&psinfo->buf_lock, flags);
  302. }
  303. static struct kmsg_dumper pstore_dumper = {
  304. .dump = pstore_dump,
  305. };
  306. #ifdef CONFIG_PSTORE_CONSOLE
  307. static void pstore_console_write(struct console *con, const char *s, unsigned c)
  308. {
  309. const char *e = s + c;
  310. while (s < e) {
  311. unsigned long flags;
  312. u64 id;
  313. if (c > psinfo->bufsize)
  314. c = psinfo->bufsize;
  315. if (oops_in_progress) {
  316. if (!spin_trylock_irqsave(&psinfo->buf_lock, flags))
  317. break;
  318. } else {
  319. spin_lock_irqsave(&psinfo->buf_lock, flags);
  320. }
  321. memcpy(psinfo->buf, s, c);
  322. psinfo->write(PSTORE_TYPE_CONSOLE, 0, &id, 0, 0, 0, c, psinfo);
  323. spin_unlock_irqrestore(&psinfo->buf_lock, flags);
  324. s += c;
  325. c = e - s;
  326. }
  327. }
  328. static struct console pstore_console = {
  329. .name = "pstore",
  330. .write = pstore_console_write,
  331. .flags = CON_PRINTBUFFER | CON_ENABLED | CON_ANYTIME,
  332. .index = -1,
  333. };
  334. static void pstore_register_console(void)
  335. {
  336. register_console(&pstore_console);
  337. }
  338. #else
  339. static void pstore_register_console(void) {}
  340. #endif
  341. static int pstore_write_compat(enum pstore_type_id type,
  342. enum kmsg_dump_reason reason,
  343. u64 *id, unsigned int part, int count,
  344. bool compressed, size_t size,
  345. struct pstore_info *psi)
  346. {
  347. return psi->write_buf(type, reason, id, part, psinfo->buf, compressed,
  348. size, psi);
  349. }
  350. /*
  351. * platform specific persistent storage driver registers with
  352. * us here. If pstore is already mounted, call the platform
  353. * read function right away to populate the file system. If not
  354. * then the pstore mount code will call us later to fill out
  355. * the file system.
  356. *
  357. * Register with kmsg_dump to save last part of console log on panic.
  358. */
  359. int pstore_register(struct pstore_info *psi)
  360. {
  361. struct module *owner = psi->owner;
  362. if (backend && strcmp(backend, psi->name))
  363. return -EPERM;
  364. spin_lock(&pstore_lock);
  365. if (psinfo) {
  366. spin_unlock(&pstore_lock);
  367. return -EBUSY;
  368. }
  369. if (!psi->write)
  370. psi->write = pstore_write_compat;
  371. psinfo = psi;
  372. mutex_init(&psinfo->read_mutex);
  373. spin_unlock(&pstore_lock);
  374. if (owner && !try_module_get(owner)) {
  375. psinfo = NULL;
  376. return -EINVAL;
  377. }
  378. allocate_buf_for_compression();
  379. if (pstore_is_mounted())
  380. pstore_get_records(0);
  381. kmsg_dump_register(&pstore_dumper);
  382. pstore_register_console();
  383. pstore_register_ftrace();
  384. if (pstore_update_ms >= 0) {
  385. pstore_timer.expires = jiffies +
  386. msecs_to_jiffies(pstore_update_ms);
  387. add_timer(&pstore_timer);
  388. }
  389. pr_info("pstore: Registered %s as persistent store backend\n",
  390. psi->name);
  391. return 0;
  392. }
  393. EXPORT_SYMBOL_GPL(pstore_register);
  394. /*
  395. * Read all the records from the persistent store. Create
  396. * files in our filesystem. Don't warn about -EEXIST errors
  397. * when we are re-scanning the backing store looking to add new
  398. * error records.
  399. */
  400. void pstore_get_records(int quiet)
  401. {
  402. struct pstore_info *psi = psinfo;
  403. char *buf = NULL;
  404. ssize_t size;
  405. u64 id;
  406. int count;
  407. enum pstore_type_id type;
  408. struct timespec time;
  409. int failed = 0, rc;
  410. bool compressed;
  411. int unzipped_len = -1;
  412. if (!psi)
  413. return;
  414. mutex_lock(&psi->read_mutex);
  415. if (psi->open && psi->open(psi))
  416. goto out;
  417. while ((size = psi->read(&id, &type, &count, &time, &buf, &compressed,
  418. psi)) > 0) {
  419. if (compressed && (type == PSTORE_TYPE_DMESG)) {
  420. if (big_oops_buf)
  421. unzipped_len = pstore_decompress(buf,
  422. big_oops_buf, size,
  423. big_oops_buf_sz);
  424. if (unzipped_len > 0) {
  425. buf = big_oops_buf;
  426. size = unzipped_len;
  427. compressed = false;
  428. } else {
  429. pr_err("pstore: decompression failed;"
  430. "returned %d\n", unzipped_len);
  431. compressed = true;
  432. }
  433. }
  434. rc = pstore_mkfile(type, psi->name, id, count, buf,
  435. compressed, (size_t)size, time, psi);
  436. if (unzipped_len < 0) {
  437. /* Free buffer other than big oops */
  438. kfree(buf);
  439. buf = NULL;
  440. } else
  441. unzipped_len = -1;
  442. if (rc && (rc != -EEXIST || !quiet))
  443. failed++;
  444. }
  445. if (psi->close)
  446. psi->close(psi);
  447. out:
  448. mutex_unlock(&psi->read_mutex);
  449. if (failed)
  450. printk(KERN_WARNING "pstore: failed to load %d record(s) from '%s'\n",
  451. failed, psi->name);
  452. }
  453. static void pstore_dowork(struct work_struct *work)
  454. {
  455. pstore_get_records(1);
  456. }
  457. static void pstore_timefunc(unsigned long dummy)
  458. {
  459. if (pstore_new_entry) {
  460. pstore_new_entry = 0;
  461. schedule_work(&pstore_work);
  462. }
  463. mod_timer(&pstore_timer, jiffies + msecs_to_jiffies(pstore_update_ms));
  464. }
  465. module_param(backend, charp, 0444);
  466. MODULE_PARM_DESC(backend, "Pstore backend to use");