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:
127 bool pstore_cannot_block_path(enum kmsg_dump_reason reason)
130 * In case of NMI path, pstore shouldn't be blocked
131 * regardless of reason.
137 /* In panic case, other cpus are stopped by smp_send_stop(). */
138 case KMSG_DUMP_PANIC:
139 /* Emergency restart shouldn't be blocked by spin lock. */
140 case KMSG_DUMP_EMERG:
146 EXPORT_SYMBOL_GPL(pstore_cannot_block_path);
148 #if IS_ENABLED(CONFIG_PSTORE_DEFLATE_COMPRESS)
149 static int zbufsize_deflate(size_t size)
154 /* buffer range for efivars */
164 /* buffer range for nvram, erst */
173 return (size * 100) / cmpr;
177 #if IS_ENABLED(CONFIG_PSTORE_LZO_COMPRESS)
178 static int zbufsize_lzo(size_t size)
180 return lzo1x_worst_compress(size);
184 #if IS_ENABLED(CONFIG_PSTORE_LZ4_COMPRESS) || IS_ENABLED(CONFIG_PSTORE_LZ4HC_COMPRESS)
185 static int zbufsize_lz4(size_t size)
187 return LZ4_compressBound(size);
191 #if IS_ENABLED(CONFIG_PSTORE_842_COMPRESS)
192 static int zbufsize_842(size_t size)
198 #if IS_ENABLED(CONFIG_PSTORE_ZSTD_COMPRESS)
199 static int zbufsize_zstd(size_t size)
201 return ZSTD_compressBound(size);
205 static const struct pstore_zbackend *zbackend __ro_after_init;
207 static const struct pstore_zbackend zbackends[] = {
208 #if IS_ENABLED(CONFIG_PSTORE_DEFLATE_COMPRESS)
210 .zbufsize = zbufsize_deflate,
214 #if IS_ENABLED(CONFIG_PSTORE_LZO_COMPRESS)
216 .zbufsize = zbufsize_lzo,
220 #if IS_ENABLED(CONFIG_PSTORE_LZ4_COMPRESS)
222 .zbufsize = zbufsize_lz4,
226 #if IS_ENABLED(CONFIG_PSTORE_LZ4HC_COMPRESS)
228 .zbufsize = zbufsize_lz4,
232 #if IS_ENABLED(CONFIG_PSTORE_842_COMPRESS)
234 .zbufsize = zbufsize_842,
238 #if IS_ENABLED(CONFIG_PSTORE_ZSTD_COMPRESS)
240 .zbufsize = zbufsize_zstd,
247 static int pstore_compress(const void *in, void *out,
248 unsigned int inlen, unsigned int outlen)
252 ret = crypto_comp_compress(tfm, in, inlen, out, &outlen);
254 pr_err("crypto_comp_compress failed, ret = %d!\n", ret);
261 static int pstore_decompress(void *in, void *out,
262 unsigned int inlen, unsigned int outlen)
266 ret = crypto_comp_decompress(tfm, in, inlen, out, &outlen);
268 pr_err("crypto_comp_decompress failed, ret = %d!\n", ret);
275 static void allocate_buf_for_compression(void)
277 struct crypto_comp *ctx;
281 /* Skip if not built-in or compression backend not selected yet. */
282 if (!IS_ENABLED(CONFIG_PSTORE_COMPRESS) || !zbackend)
285 /* Skip if no pstore backend yet or compression init already done. */
289 if (!crypto_has_comp(zbackend->name, 0, 0)) {
290 pr_err("Unknown compression: %s\n", zbackend->name);
294 size = zbackend->zbufsize(psinfo->bufsize);
296 pr_err("Invalid compression size for %s: %d\n",
297 zbackend->name, size);
301 buf = kmalloc(size, GFP_KERNEL);
303 pr_err("Failed %d byte compression buffer allocation for: %s\n",
304 size, zbackend->name);
308 ctx = crypto_alloc_comp(zbackend->name, 0, 0);
309 if (IS_ERR_OR_NULL(ctx)) {
311 pr_err("crypto_alloc_comp('%s') failed: %ld\n", zbackend->name,
316 /* A non-NULL big_oops_buf indicates compression is available. */
318 big_oops_buf_sz = size;
321 pr_info("Using compression: %s\n", zbackend->name);
324 static void free_buf_for_compression(void)
326 if (IS_ENABLED(CONFIG_PSTORE_COMPRESS) && tfm)
327 crypto_free_comp(tfm);
334 * Called when compression fails, since the printk buffer
335 * would be fetched for compression calling it again when
336 * compression fails would have moved the iterator of
337 * printk buffer which results in fetching old contents.
338 * Copy the recent messages from big_oops_buf to psinfo->buf
340 static size_t copy_kmsg_to_buffer(int hsize, size_t len)
345 total_len = hsize + len;
347 if (total_len > psinfo->bufsize) {
348 diff = total_len - psinfo->bufsize + hsize;
349 memcpy(psinfo->buf, big_oops_buf, hsize);
350 memcpy(psinfo->buf + hsize, big_oops_buf + diff,
351 psinfo->bufsize - hsize);
352 total_len = psinfo->bufsize;
354 memcpy(psinfo->buf, big_oops_buf, total_len);
359 void pstore_record_init(struct pstore_record *record,
360 struct pstore_info *psinfo)
362 memset(record, 0, sizeof(*record));
364 record->psi = psinfo;
366 /* Report zeroed timestamp if called before timekeeping has resumed. */
367 record->time = ns_to_timespec64(ktime_get_real_fast_ns());
371 * callback from kmsg_dump. (s2,l2) has the most recently
372 * written bytes, older bytes are in (s1,l1). Save as much
373 * as we can from the end of the buffer.
375 static void pstore_dump(struct kmsg_dumper *dumper,
376 enum kmsg_dump_reason reason)
378 unsigned long total = 0;
380 unsigned int part = 1;
381 unsigned long flags = 0;
385 why = get_reason_str(reason);
387 if (pstore_cannot_block_path(reason)) {
388 is_locked = spin_trylock_irqsave(&psinfo->buf_lock, flags);
390 pr_err("pstore dump routine blocked in %s path, may corrupt error record\n"
391 , in_nmi() ? "NMI" : why);
395 spin_lock_irqsave(&psinfo->buf_lock, flags);
399 while (total < kmsg_bytes) {
405 struct pstore_record record;
407 pstore_record_init(&record, psinfo);
408 record.type = PSTORE_TYPE_DMESG;
409 record.count = oopscount;
410 record.reason = reason;
412 record.buf = psinfo->buf;
414 if (big_oops_buf && is_locked) {
416 dst_size = big_oops_buf_sz;
419 dst_size = psinfo->bufsize;
422 /* Write dump header. */
423 header_size = snprintf(dst, dst_size, "%s#%d Part%u\n", why,
425 dst_size -= header_size;
427 /* Write dump contents. */
428 if (!kmsg_dump_get_buffer(dumper, true, dst + header_size,
429 dst_size, &dump_size))
432 if (big_oops_buf && is_locked) {
433 zipped_len = pstore_compress(dst, psinfo->buf,
434 header_size + dump_size,
437 if (zipped_len > 0) {
438 record.compressed = true;
439 record.size = zipped_len;
441 record.size = copy_kmsg_to_buffer(header_size,
445 record.size = header_size + dump_size;
448 ret = psinfo->write(&record);
449 if (ret == 0 && reason == KMSG_DUMP_OOPS && pstore_is_mounted())
450 pstore_new_entry = 1;
452 total += record.size;
456 spin_unlock_irqrestore(&psinfo->buf_lock, flags);
459 static struct kmsg_dumper pstore_dumper = {
464 * Register with kmsg_dump to save last part of console log on panic.
466 static void pstore_register_kmsg(void)
468 kmsg_dump_register(&pstore_dumper);
471 static void pstore_unregister_kmsg(void)
473 kmsg_dump_unregister(&pstore_dumper);
476 #ifdef CONFIG_PSTORE_CONSOLE
477 static void pstore_console_write(struct console *con, const char *s, unsigned c)
479 const char *e = s + c;
482 struct pstore_record record;
485 pstore_record_init(&record, psinfo);
486 record.type = PSTORE_TYPE_CONSOLE;
488 if (c > psinfo->bufsize)
491 if (oops_in_progress) {
492 if (!spin_trylock_irqsave(&psinfo->buf_lock, flags))
495 spin_lock_irqsave(&psinfo->buf_lock, flags);
497 record.buf = (char *)s;
499 psinfo->write(&record);
500 spin_unlock_irqrestore(&psinfo->buf_lock, flags);
506 static struct console pstore_console = {
508 .write = pstore_console_write,
509 .flags = CON_PRINTBUFFER | CON_ENABLED | CON_ANYTIME,
513 static void pstore_register_console(void)
515 register_console(&pstore_console);
518 static void pstore_unregister_console(void)
520 unregister_console(&pstore_console);
523 static void pstore_register_console(void) {}
524 static void pstore_unregister_console(void) {}
527 static int pstore_write_user_compat(struct pstore_record *record,
528 const char __user *buf)
535 record->buf = memdup_user(buf, record->size);
536 if (IS_ERR(record->buf)) {
537 ret = PTR_ERR(record->buf);
541 ret = record->psi->write(record);
547 return unlikely(ret < 0) ? ret : record->size;
551 * platform specific persistent storage driver registers with
552 * us here. If pstore is already mounted, call the platform
553 * read function right away to populate the file system. If not
554 * then the pstore mount code will call us later to fill out
557 int pstore_register(struct pstore_info *psi)
559 struct module *owner = psi->owner;
561 if (backend && strcmp(backend, psi->name)) {
562 pr_warn("ignoring unexpected backend '%s'\n", psi->name);
566 /* Sanity check flags. */
568 pr_warn("backend '%s' must support at least one frontend\n",
573 /* Check for required functions. */
574 if (!psi->read || !psi->write) {
575 pr_warn("backend '%s' must implement read() and write()\n",
580 spin_lock(&pstore_lock);
582 pr_warn("backend '%s' already loaded: ignoring '%s'\n",
583 psinfo->name, psi->name);
584 spin_unlock(&pstore_lock);
588 if (!psi->write_user)
589 psi->write_user = pstore_write_user_compat;
591 mutex_init(&psinfo->read_mutex);
592 spin_unlock(&pstore_lock);
594 if (owner && !try_module_get(owner)) {
599 allocate_buf_for_compression();
601 if (pstore_is_mounted())
602 pstore_get_records(0);
604 if (psi->flags & PSTORE_FLAGS_DMESG)
605 pstore_register_kmsg();
606 if (psi->flags & PSTORE_FLAGS_CONSOLE)
607 pstore_register_console();
608 if (psi->flags & PSTORE_FLAGS_FTRACE)
609 pstore_register_ftrace();
610 if (psi->flags & PSTORE_FLAGS_PMSG)
611 pstore_register_pmsg();
613 /* Start watching for new records, if desired. */
614 if (pstore_update_ms >= 0) {
615 pstore_timer.expires = jiffies +
616 msecs_to_jiffies(pstore_update_ms);
617 add_timer(&pstore_timer);
621 * Update the module parameter backend, so it is visible
622 * through /sys/module/pstore/parameters/backend
626 pr_info("Registered %s as persistent store backend\n", psi->name);
632 EXPORT_SYMBOL_GPL(pstore_register);
634 void pstore_unregister(struct pstore_info *psi)
636 /* Stop timer and make sure all work has finished. */
637 pstore_update_ms = -1;
638 del_timer_sync(&pstore_timer);
639 flush_work(&pstore_work);
641 if (psi->flags & PSTORE_FLAGS_PMSG)
642 pstore_unregister_pmsg();
643 if (psi->flags & PSTORE_FLAGS_FTRACE)
644 pstore_unregister_ftrace();
645 if (psi->flags & PSTORE_FLAGS_CONSOLE)
646 pstore_unregister_console();
647 if (psi->flags & PSTORE_FLAGS_DMESG)
648 pstore_unregister_kmsg();
650 free_buf_for_compression();
655 EXPORT_SYMBOL_GPL(pstore_unregister);
657 static void decompress_record(struct pstore_record *record)
662 if (!record->compressed)
665 /* Only PSTORE_TYPE_DMESG support compression. */
666 if (record->type != PSTORE_TYPE_DMESG) {
667 pr_warn("ignored compressed record type %d\n", record->type);
671 /* No compression method has created the common buffer. */
673 pr_warn("no decompression buffer allocated\n");
677 unzipped_len = pstore_decompress(record->buf, big_oops_buf,
678 record->size, big_oops_buf_sz);
679 if (unzipped_len <= 0) {
680 pr_err("decompression failed: %d\n", unzipped_len);
684 /* Build new buffer for decompressed contents. */
685 decompressed = kmalloc(unzipped_len + record->ecc_notice_size,
688 pr_err("decompression ran out of memory\n");
691 memcpy(decompressed, big_oops_buf, unzipped_len);
693 /* Append ECC notice to decompressed buffer. */
694 memcpy(decompressed + unzipped_len, record->buf + record->size,
695 record->ecc_notice_size);
697 /* Swap out compresed contents with decompressed contents. */
699 record->buf = decompressed;
700 record->size = unzipped_len;
701 record->compressed = false;
705 * Read all the records from one persistent store backend. Create
706 * files in our filesystem. Don't warn about -EEXIST errors
707 * when we are re-scanning the backing store looking to add new
710 void pstore_get_backend_records(struct pstore_info *psi,
711 struct dentry *root, int quiet)
714 unsigned int stop_loop = 65536;
719 mutex_lock(&psi->read_mutex);
720 if (psi->open && psi->open(psi))
724 * Backend callback read() allocates record.buf. decompress_record()
725 * may reallocate record.buf. On success, pstore_mkfile() will keep
726 * the record.buf, so free it only on failure.
728 for (; stop_loop; stop_loop--) {
729 struct pstore_record *record;
732 record = kzalloc(sizeof(*record), GFP_KERNEL);
734 pr_err("out of memory creating record\n");
737 pstore_record_init(record, psi);
739 record->size = psi->read(record);
741 /* No more records left in backend? */
742 if (record->size <= 0) {
747 decompress_record(record);
748 rc = pstore_mkfile(root, record);
750 /* pstore_mkfile() did not take record, so free it. */
753 if (rc != -EEXIST || !quiet)
760 mutex_unlock(&psi->read_mutex);
763 pr_warn("failed to create %d record(s) from '%s'\n",
766 pr_err("looping? Too many records seen from '%s'\n",
770 static void pstore_dowork(struct work_struct *work)
772 pstore_get_records(1);
775 static void pstore_timefunc(struct timer_list *unused)
777 if (pstore_new_entry) {
778 pstore_new_entry = 0;
779 schedule_work(&pstore_work);
782 if (pstore_update_ms >= 0)
783 mod_timer(&pstore_timer,
784 jiffies + msecs_to_jiffies(pstore_update_ms));
787 void __init pstore_choose_compression(void)
789 const struct pstore_zbackend *step;
794 for (step = zbackends; step->name; step++) {
795 if (!strcmp(compress, step->name)) {
802 static int __init pstore_init(void)
806 pstore_choose_compression();
809 * Check if any pstore backends registered earlier but did not
810 * initialize compression because crypto was not ready. If so,
811 * initialize compression now.
813 allocate_buf_for_compression();
815 ret = pstore_init_fs();
821 late_initcall(pstore_init);
823 static void __exit pstore_exit(void)
827 module_exit(pstore_exit)
829 module_param(compress, charp, 0444);
830 MODULE_PARM_DESC(compress, "Pstore compression to use");
832 module_param(backend, charp, 0444);
833 MODULE_PARM_DESC(backend, "Pstore backend to use");
835 MODULE_AUTHOR("Tony Luck <tony.luck@intel.com>");
836 MODULE_LICENSE("GPL");