2 * Persistent Storage - platform driver interface parts.
4 * Copyright (C) 2007-2008 Google, Inc.
5 * Copyright (C) 2010 Intel Corporation <tony.luck@intel.com>
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.
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.
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
21 #define pr_fmt(fmt) "pstore: " fmt
23 #include <linux/atomic.h>
24 #include <linux/types.h>
25 #include <linux/errno.h>
26 #include <linux/init.h>
27 #include <linux/kmsg_dump.h>
28 #include <linux/console.h>
29 #include <linux/module.h>
30 #include <linux/pstore.h>
31 #if IS_ENABLED(CONFIG_PSTORE_LZO_COMPRESS)
32 #include <linux/lzo.h>
34 #if IS_ENABLED(CONFIG_PSTORE_LZ4_COMPRESS) || IS_ENABLED(CONFIG_PSTORE_LZ4HC_COMPRESS)
35 #include <linux/lz4.h>
37 #if IS_ENABLED(CONFIG_PSTORE_ZSTD_COMPRESS)
38 #include <linux/zstd.h>
40 #include <linux/crypto.h>
41 #include <linux/string.h>
42 #include <linux/timer.h>
43 #include <linux/slab.h>
44 #include <linux/uaccess.h>
45 #include <linux/jiffies.h>
46 #include <linux/workqueue.h>
51 * We defer making "oops" entries appear in pstore - see
52 * whether the system is actually still running well enough
53 * to let someone see the entry
55 static int pstore_update_ms = -1;
56 module_param_named(update_ms, pstore_update_ms, int, 0600);
57 MODULE_PARM_DESC(update_ms, "milliseconds before pstore updates its content "
58 "(default is -1, which means runtime updates are disabled; "
59 "enabling this option is not safe, it may lead to further "
60 "corruption on Oopses)");
62 static int pstore_new_entry;
64 static void pstore_timefunc(struct timer_list *);
65 static DEFINE_TIMER(pstore_timer, pstore_timefunc);
67 static void pstore_dowork(struct work_struct *);
68 static DECLARE_WORK(pstore_work, pstore_dowork);
71 * pstore_lock just protects "psinfo" during
72 * calls to pstore_register()
74 static DEFINE_SPINLOCK(pstore_lock);
75 struct pstore_info *psinfo;
78 static char *compress =
79 #ifdef CONFIG_PSTORE_COMPRESS_DEFAULT
80 CONFIG_PSTORE_COMPRESS_DEFAULT;
85 /* Compression parameters */
86 static struct crypto_comp *tfm;
88 struct pstore_zbackend {
89 int (*zbufsize)(size_t size);
93 static char *big_oops_buf;
94 static size_t big_oops_buf_sz;
96 /* How much of the console log to snapshot */
97 unsigned long kmsg_bytes = PSTORE_DEFAULT_KMSG_BYTES;
99 void pstore_set_kmsg_bytes(int bytes)
104 /* Tag each group of saved records with a sequence number */
105 static int oopscount;
107 static const char *get_reason_str(enum kmsg_dump_reason reason)
110 case KMSG_DUMP_PANIC:
114 case KMSG_DUMP_EMERG:
116 case KMSG_DUMP_RESTART:
120 case KMSG_DUMP_POWEROFF:
128 * Should pstore_dump() wait for a concurrent pstore_dump()? If
129 * not, the current pstore_dump() will report a failure to dump
132 static bool pstore_cannot_wait(enum kmsg_dump_reason reason)
134 /* In NMI path, pstore shouldn't block regardless of reason. */
139 /* In panic case, other cpus are stopped by smp_send_stop(). */
140 case KMSG_DUMP_PANIC:
141 /* Emergency restart shouldn't be blocked. */
142 case KMSG_DUMP_EMERG:
149 #if IS_ENABLED(CONFIG_PSTORE_DEFLATE_COMPRESS)
150 static int zbufsize_deflate(size_t size)
155 /* buffer range for efivars */
165 /* buffer range for nvram, erst */
174 return (size * 100) / cmpr;
178 #if IS_ENABLED(CONFIG_PSTORE_LZO_COMPRESS)
179 static int zbufsize_lzo(size_t size)
181 return lzo1x_worst_compress(size);
185 #if IS_ENABLED(CONFIG_PSTORE_LZ4_COMPRESS) || IS_ENABLED(CONFIG_PSTORE_LZ4HC_COMPRESS)
186 static int zbufsize_lz4(size_t size)
188 return LZ4_compressBound(size);
192 #if IS_ENABLED(CONFIG_PSTORE_842_COMPRESS)
193 static int zbufsize_842(size_t size)
199 #if IS_ENABLED(CONFIG_PSTORE_ZSTD_COMPRESS)
200 static int zbufsize_zstd(size_t size)
202 return ZSTD_compressBound(size);
206 static const struct pstore_zbackend *zbackend __ro_after_init;
208 static const struct pstore_zbackend zbackends[] = {
209 #if IS_ENABLED(CONFIG_PSTORE_DEFLATE_COMPRESS)
211 .zbufsize = zbufsize_deflate,
215 #if IS_ENABLED(CONFIG_PSTORE_LZO_COMPRESS)
217 .zbufsize = zbufsize_lzo,
221 #if IS_ENABLED(CONFIG_PSTORE_LZ4_COMPRESS)
223 .zbufsize = zbufsize_lz4,
227 #if IS_ENABLED(CONFIG_PSTORE_LZ4HC_COMPRESS)
229 .zbufsize = zbufsize_lz4,
233 #if IS_ENABLED(CONFIG_PSTORE_842_COMPRESS)
235 .zbufsize = zbufsize_842,
239 #if IS_ENABLED(CONFIG_PSTORE_ZSTD_COMPRESS)
241 .zbufsize = zbufsize_zstd,
248 static int pstore_compress(const void *in, void *out,
249 unsigned int inlen, unsigned int outlen)
253 if (!IS_ENABLED(CONFIG_PSTORE_COMPRESS))
256 ret = crypto_comp_compress(tfm, in, inlen, out, &outlen);
258 pr_err("crypto_comp_compress failed, ret = %d!\n", ret);
265 static int pstore_decompress(void *in, void *out,
266 unsigned int inlen, unsigned int outlen)
270 ret = crypto_comp_decompress(tfm, in, inlen, out, &outlen);
272 pr_err("crypto_comp_decompress failed, ret = %d!\n", ret);
279 static void allocate_buf_for_compression(void)
281 struct crypto_comp *ctx;
285 /* Skip if not built-in or compression backend not selected yet. */
286 if (!IS_ENABLED(CONFIG_PSTORE_COMPRESS) || !zbackend)
289 /* Skip if no pstore backend yet or compression init already done. */
293 if (!crypto_has_comp(zbackend->name, 0, 0)) {
294 pr_err("Unknown compression: %s\n", zbackend->name);
298 size = zbackend->zbufsize(psinfo->bufsize);
300 pr_err("Invalid compression size for %s: %d\n",
301 zbackend->name, size);
305 buf = kmalloc(size, GFP_KERNEL);
307 pr_err("Failed %d byte compression buffer allocation for: %s\n",
308 size, zbackend->name);
312 ctx = crypto_alloc_comp(zbackend->name, 0, 0);
313 if (IS_ERR_OR_NULL(ctx)) {
315 pr_err("crypto_alloc_comp('%s') failed: %ld\n", zbackend->name,
320 /* A non-NULL big_oops_buf indicates compression is available. */
322 big_oops_buf_sz = size;
325 pr_info("Using compression: %s\n", zbackend->name);
328 static void free_buf_for_compression(void)
330 if (IS_ENABLED(CONFIG_PSTORE_COMPRESS) && tfm) {
331 crypto_free_comp(tfm);
340 * Called when compression fails, since the printk buffer
341 * would be fetched for compression calling it again when
342 * compression fails would have moved the iterator of
343 * printk buffer which results in fetching old contents.
344 * Copy the recent messages from big_oops_buf to psinfo->buf
346 static size_t copy_kmsg_to_buffer(int hsize, size_t len)
351 total_len = hsize + len;
353 if (total_len > psinfo->bufsize) {
354 diff = total_len - psinfo->bufsize + hsize;
355 memcpy(psinfo->buf, big_oops_buf, hsize);
356 memcpy(psinfo->buf + hsize, big_oops_buf + diff,
357 psinfo->bufsize - hsize);
358 total_len = psinfo->bufsize;
360 memcpy(psinfo->buf, big_oops_buf, total_len);
365 void pstore_record_init(struct pstore_record *record,
366 struct pstore_info *psinfo)
368 memset(record, 0, sizeof(*record));
370 record->psi = psinfo;
372 /* Report zeroed timestamp if called before timekeeping has resumed. */
373 record->time = ns_to_timespec64(ktime_get_real_fast_ns());
377 * callback from kmsg_dump. (s2,l2) has the most recently
378 * written bytes, older bytes are in (s1,l1). Save as much
379 * as we can from the end of the buffer.
381 static void pstore_dump(struct kmsg_dumper *dumper,
382 enum kmsg_dump_reason reason)
384 unsigned long total = 0;
386 unsigned int part = 1;
389 why = get_reason_str(reason);
391 if (down_trylock(&psinfo->buf_lock)) {
392 /* Failed to acquire lock: give up if we cannot wait. */
393 if (pstore_cannot_wait(reason)) {
394 pr_err("dump skipped in %s path: may corrupt error record\n",
395 in_nmi() ? "NMI" : why);
398 if (down_interruptible(&psinfo->buf_lock)) {
399 pr_err("could not grab semaphore?!\n");
405 while (total < kmsg_bytes) {
411 struct pstore_record record;
413 pstore_record_init(&record, psinfo);
414 record.type = PSTORE_TYPE_DMESG;
415 record.count = oopscount;
416 record.reason = reason;
418 record.buf = psinfo->buf;
422 dst_size = big_oops_buf_sz;
425 dst_size = psinfo->bufsize;
428 /* Write dump header. */
429 header_size = snprintf(dst, dst_size, "%s#%d Part%u\n", why,
431 dst_size -= header_size;
433 /* Write dump contents. */
434 if (!kmsg_dump_get_buffer(dumper, true, dst + header_size,
435 dst_size, &dump_size))
439 zipped_len = pstore_compress(dst, psinfo->buf,
440 header_size + dump_size,
443 if (zipped_len > 0) {
444 record.compressed = true;
445 record.size = zipped_len;
447 record.size = copy_kmsg_to_buffer(header_size,
451 record.size = header_size + dump_size;
454 ret = psinfo->write(&record);
455 if (ret == 0 && reason == KMSG_DUMP_OOPS && pstore_is_mounted())
456 pstore_new_entry = 1;
458 total += record.size;
462 up(&psinfo->buf_lock);
465 static struct kmsg_dumper pstore_dumper = {
470 * Register with kmsg_dump to save last part of console log on panic.
472 static void pstore_register_kmsg(void)
474 kmsg_dump_register(&pstore_dumper);
477 static void pstore_unregister_kmsg(void)
479 kmsg_dump_unregister(&pstore_dumper);
482 #ifdef CONFIG_PSTORE_CONSOLE
483 static void pstore_console_write(struct console *con, const char *s, unsigned c)
485 struct pstore_record record;
487 pstore_record_init(&record, psinfo);
488 record.type = PSTORE_TYPE_CONSOLE;
490 record.buf = (char *)s;
492 psinfo->write(&record);
495 static struct console pstore_console = {
497 .write = pstore_console_write,
498 .flags = CON_PRINTBUFFER | CON_ENABLED | CON_ANYTIME,
502 static void pstore_register_console(void)
504 register_console(&pstore_console);
507 static void pstore_unregister_console(void)
509 unregister_console(&pstore_console);
512 static void pstore_register_console(void) {}
513 static void pstore_unregister_console(void) {}
516 static int pstore_write_user_compat(struct pstore_record *record,
517 const char __user *buf)
524 record->buf = memdup_user(buf, record->size);
525 if (IS_ERR(record->buf)) {
526 ret = PTR_ERR(record->buf);
530 ret = record->psi->write(record);
536 return unlikely(ret < 0) ? ret : record->size;
540 * platform specific persistent storage driver registers with
541 * us here. If pstore is already mounted, call the platform
542 * read function right away to populate the file system. If not
543 * then the pstore mount code will call us later to fill out
546 int pstore_register(struct pstore_info *psi)
548 struct module *owner = psi->owner;
550 if (backend && strcmp(backend, psi->name)) {
551 pr_warn("ignoring unexpected backend '%s'\n", psi->name);
555 /* Sanity check flags. */
557 pr_warn("backend '%s' must support at least one frontend\n",
562 /* Check for required functions. */
563 if (!psi->read || !psi->write) {
564 pr_warn("backend '%s' must implement read() and write()\n",
569 spin_lock(&pstore_lock);
571 pr_warn("backend '%s' already loaded: ignoring '%s'\n",
572 psinfo->name, psi->name);
573 spin_unlock(&pstore_lock);
577 if (!psi->write_user)
578 psi->write_user = pstore_write_user_compat;
580 mutex_init(&psinfo->read_mutex);
581 sema_init(&psinfo->buf_lock, 1);
582 spin_unlock(&pstore_lock);
584 if (owner && !try_module_get(owner)) {
589 if (psi->flags & PSTORE_FLAGS_DMESG)
590 allocate_buf_for_compression();
592 if (pstore_is_mounted())
593 pstore_get_records(0);
595 if (psi->flags & PSTORE_FLAGS_DMESG)
596 pstore_register_kmsg();
597 if (psi->flags & PSTORE_FLAGS_CONSOLE)
598 pstore_register_console();
599 if (psi->flags & PSTORE_FLAGS_FTRACE)
600 pstore_register_ftrace();
601 if (psi->flags & PSTORE_FLAGS_PMSG)
602 pstore_register_pmsg();
604 /* Start watching for new records, if desired. */
605 if (pstore_update_ms >= 0) {
606 pstore_timer.expires = jiffies +
607 msecs_to_jiffies(pstore_update_ms);
608 add_timer(&pstore_timer);
612 * Update the module parameter backend, so it is visible
613 * through /sys/module/pstore/parameters/backend
617 pr_info("Registered %s as persistent store backend\n", psi->name);
623 EXPORT_SYMBOL_GPL(pstore_register);
625 void pstore_unregister(struct pstore_info *psi)
627 /* Stop timer and make sure all work has finished. */
628 pstore_update_ms = -1;
629 del_timer_sync(&pstore_timer);
630 flush_work(&pstore_work);
632 if (psi->flags & PSTORE_FLAGS_PMSG)
633 pstore_unregister_pmsg();
634 if (psi->flags & PSTORE_FLAGS_FTRACE)
635 pstore_unregister_ftrace();
636 if (psi->flags & PSTORE_FLAGS_CONSOLE)
637 pstore_unregister_console();
638 if (psi->flags & PSTORE_FLAGS_DMESG)
639 pstore_unregister_kmsg();
641 free_buf_for_compression();
646 EXPORT_SYMBOL_GPL(pstore_unregister);
648 static void decompress_record(struct pstore_record *record)
653 if (!IS_ENABLED(CONFIG_PSTORE_COMPRESS) || !record->compressed)
656 /* Only PSTORE_TYPE_DMESG support compression. */
657 if (record->type != PSTORE_TYPE_DMESG) {
658 pr_warn("ignored compressed record type %d\n", record->type);
662 /* No compression method has created the common buffer. */
664 pr_warn("no decompression buffer allocated\n");
668 unzipped_len = pstore_decompress(record->buf, big_oops_buf,
669 record->size, big_oops_buf_sz);
670 if (unzipped_len <= 0) {
671 pr_err("decompression failed: %d\n", unzipped_len);
675 /* Build new buffer for decompressed contents. */
676 decompressed = kmalloc(unzipped_len + record->ecc_notice_size,
679 pr_err("decompression ran out of memory\n");
682 memcpy(decompressed, big_oops_buf, unzipped_len);
684 /* Append ECC notice to decompressed buffer. */
685 memcpy(decompressed + unzipped_len, record->buf + record->size,
686 record->ecc_notice_size);
688 /* Swap out compresed contents with decompressed contents. */
690 record->buf = decompressed;
691 record->size = unzipped_len;
692 record->compressed = false;
696 * Read all the records from one persistent store backend. Create
697 * files in our filesystem. Don't warn about -EEXIST errors
698 * when we are re-scanning the backing store looking to add new
701 void pstore_get_backend_records(struct pstore_info *psi,
702 struct dentry *root, int quiet)
705 unsigned int stop_loop = 65536;
710 mutex_lock(&psi->read_mutex);
711 if (psi->open && psi->open(psi))
715 * Backend callback read() allocates record.buf. decompress_record()
716 * may reallocate record.buf. On success, pstore_mkfile() will keep
717 * the record.buf, so free it only on failure.
719 for (; stop_loop; stop_loop--) {
720 struct pstore_record *record;
723 record = kzalloc(sizeof(*record), GFP_KERNEL);
725 pr_err("out of memory creating record\n");
728 pstore_record_init(record, psi);
730 record->size = psi->read(record);
732 /* No more records left in backend? */
733 if (record->size <= 0) {
738 decompress_record(record);
739 rc = pstore_mkfile(root, record);
741 /* pstore_mkfile() did not take record, so free it. */
744 if (rc != -EEXIST || !quiet)
751 mutex_unlock(&psi->read_mutex);
754 pr_warn("failed to create %d record(s) from '%s'\n",
757 pr_err("looping? Too many records seen from '%s'\n",
761 static void pstore_dowork(struct work_struct *work)
763 pstore_get_records(1);
766 static void pstore_timefunc(struct timer_list *unused)
768 if (pstore_new_entry) {
769 pstore_new_entry = 0;
770 schedule_work(&pstore_work);
773 if (pstore_update_ms >= 0)
774 mod_timer(&pstore_timer,
775 jiffies + msecs_to_jiffies(pstore_update_ms));
778 void __init pstore_choose_compression(void)
780 const struct pstore_zbackend *step;
785 for (step = zbackends; step->name; step++) {
786 if (!strcmp(compress, step->name)) {
793 static int __init pstore_init(void)
797 pstore_choose_compression();
800 * Check if any pstore backends registered earlier but did not
801 * initialize compression because crypto was not ready. If so,
802 * initialize compression now.
804 allocate_buf_for_compression();
806 ret = pstore_init_fs();
808 free_buf_for_compression();
812 late_initcall(pstore_init);
814 static void __exit pstore_exit(void)
818 module_exit(pstore_exit)
820 module_param(compress, charp, 0444);
821 MODULE_PARM_DESC(compress, "Pstore compression to use");
823 module_param(backend, charp, 0444);
824 MODULE_PARM_DESC(backend, "Pstore backend to use");
826 MODULE_AUTHOR("Tony Luck <tony.luck@intel.com>");
827 MODULE_LICENSE("GPL");