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 /* Names should be in the same order as the enum pstore_type_id */
63 static const char * const pstore_type_names
[] = {
75 static int pstore_new_entry
;
77 static void pstore_timefunc(struct timer_list
*);
78 static DEFINE_TIMER(pstore_timer
, pstore_timefunc
);
80 static void pstore_dowork(struct work_struct
*);
81 static DECLARE_WORK(pstore_work
, pstore_dowork
);
84 * pstore_lock just protects "psinfo" during
85 * calls to pstore_register()
87 static DEFINE_SPINLOCK(pstore_lock
);
88 struct pstore_info
*psinfo
;
91 static char *compress
=
92 #ifdef CONFIG_PSTORE_COMPRESS_DEFAULT
93 CONFIG_PSTORE_COMPRESS_DEFAULT
;
98 /* Compression parameters */
99 static struct crypto_comp
*tfm
;
101 struct pstore_zbackend
{
102 int (*zbufsize
)(size_t size
);
106 static char *big_oops_buf
;
107 static size_t big_oops_buf_sz
;
109 /* How much of the console log to snapshot */
110 unsigned long kmsg_bytes
= PSTORE_DEFAULT_KMSG_BYTES
;
112 void pstore_set_kmsg_bytes(int bytes
)
117 /* Tag each group of saved records with a sequence number */
118 static int oopscount
;
120 const char *pstore_type_to_name(enum pstore_type_id type
)
122 BUILD_BUG_ON(ARRAY_SIZE(pstore_type_names
) != PSTORE_TYPE_MAX
);
124 if (WARN_ON_ONCE(type
>= PSTORE_TYPE_MAX
))
127 return pstore_type_names
[type
];
129 EXPORT_SYMBOL_GPL(pstore_type_to_name
);
131 enum pstore_type_id
pstore_name_to_type(const char *name
)
135 for (i
= 0; i
< PSTORE_TYPE_MAX
; i
++) {
136 if (!strcmp(pstore_type_names
[i
], name
))
140 return PSTORE_TYPE_MAX
;
142 EXPORT_SYMBOL_GPL(pstore_name_to_type
);
144 static const char *get_reason_str(enum kmsg_dump_reason reason
)
147 case KMSG_DUMP_PANIC
:
151 case KMSG_DUMP_EMERG
:
153 case KMSG_DUMP_RESTART
:
157 case KMSG_DUMP_POWEROFF
:
165 * Should pstore_dump() wait for a concurrent pstore_dump()? If
166 * not, the current pstore_dump() will report a failure to dump
169 static bool pstore_cannot_wait(enum kmsg_dump_reason reason
)
171 /* In NMI path, pstore shouldn't block regardless of reason. */
176 /* In panic case, other cpus are stopped by smp_send_stop(). */
177 case KMSG_DUMP_PANIC
:
178 /* Emergency restart shouldn't be blocked. */
179 case KMSG_DUMP_EMERG
:
186 #if IS_ENABLED(CONFIG_PSTORE_DEFLATE_COMPRESS)
187 static int zbufsize_deflate(size_t size
)
192 /* buffer range for efivars */
202 /* buffer range for nvram, erst */
211 return (size
* 100) / cmpr
;
215 #if IS_ENABLED(CONFIG_PSTORE_LZO_COMPRESS)
216 static int zbufsize_lzo(size_t size
)
218 return lzo1x_worst_compress(size
);
222 #if IS_ENABLED(CONFIG_PSTORE_LZ4_COMPRESS) || IS_ENABLED(CONFIG_PSTORE_LZ4HC_COMPRESS)
223 static int zbufsize_lz4(size_t size
)
225 return LZ4_compressBound(size
);
229 #if IS_ENABLED(CONFIG_PSTORE_842_COMPRESS)
230 static int zbufsize_842(size_t size
)
236 #if IS_ENABLED(CONFIG_PSTORE_ZSTD_COMPRESS)
237 static int zbufsize_zstd(size_t size
)
239 return ZSTD_compressBound(size
);
243 static const struct pstore_zbackend
*zbackend __ro_after_init
;
245 static const struct pstore_zbackend zbackends
[] = {
246 #if IS_ENABLED(CONFIG_PSTORE_DEFLATE_COMPRESS)
248 .zbufsize
= zbufsize_deflate
,
252 #if IS_ENABLED(CONFIG_PSTORE_LZO_COMPRESS)
254 .zbufsize
= zbufsize_lzo
,
258 #if IS_ENABLED(CONFIG_PSTORE_LZ4_COMPRESS)
260 .zbufsize
= zbufsize_lz4
,
264 #if IS_ENABLED(CONFIG_PSTORE_LZ4HC_COMPRESS)
266 .zbufsize
= zbufsize_lz4
,
270 #if IS_ENABLED(CONFIG_PSTORE_842_COMPRESS)
272 .zbufsize
= zbufsize_842
,
276 #if IS_ENABLED(CONFIG_PSTORE_ZSTD_COMPRESS)
278 .zbufsize
= zbufsize_zstd
,
285 static int pstore_compress(const void *in
, void *out
,
286 unsigned int inlen
, unsigned int outlen
)
290 ret
= crypto_comp_compress(tfm
, in
, inlen
, out
, &outlen
);
292 pr_err("crypto_comp_compress failed, ret = %d!\n", ret
);
299 static void allocate_buf_for_compression(void)
301 struct crypto_comp
*ctx
;
305 /* Skip if not built-in or compression backend not selected yet. */
306 if (!IS_ENABLED(CONFIG_PSTORE_COMPRESS
) || !zbackend
)
309 /* Skip if no pstore backend yet or compression init already done. */
313 if (!crypto_has_comp(zbackend
->name
, 0, 0)) {
314 pr_err("Unknown compression: %s\n", zbackend
->name
);
318 size
= zbackend
->zbufsize(psinfo
->bufsize
);
320 pr_err("Invalid compression size for %s: %d\n",
321 zbackend
->name
, size
);
325 buf
= kmalloc(size
, GFP_KERNEL
);
327 pr_err("Failed %d byte compression buffer allocation for: %s\n",
328 size
, zbackend
->name
);
332 ctx
= crypto_alloc_comp(zbackend
->name
, 0, 0);
333 if (IS_ERR_OR_NULL(ctx
)) {
335 pr_err("crypto_alloc_comp('%s') failed: %ld\n", zbackend
->name
,
340 /* A non-NULL big_oops_buf indicates compression is available. */
342 big_oops_buf_sz
= size
;
345 pr_info("Using crash dump compression: %s\n", zbackend
->name
);
348 static void free_buf_for_compression(void)
350 if (IS_ENABLED(CONFIG_PSTORE_COMPRESS
) && tfm
)
351 crypto_free_comp(tfm
);
358 * Called when compression fails, since the printk buffer
359 * would be fetched for compression calling it again when
360 * compression fails would have moved the iterator of
361 * printk buffer which results in fetching old contents.
362 * Copy the recent messages from big_oops_buf to psinfo->buf
364 static size_t copy_kmsg_to_buffer(int hsize
, size_t len
)
369 total_len
= hsize
+ len
;
371 if (total_len
> psinfo
->bufsize
) {
372 diff
= total_len
- psinfo
->bufsize
+ hsize
;
373 memcpy(psinfo
->buf
, big_oops_buf
, hsize
);
374 memcpy(psinfo
->buf
+ hsize
, big_oops_buf
+ diff
,
375 psinfo
->bufsize
- hsize
);
376 total_len
= psinfo
->bufsize
;
378 memcpy(psinfo
->buf
, big_oops_buf
, total_len
);
383 void pstore_record_init(struct pstore_record
*record
,
384 struct pstore_info
*psinfo
)
386 memset(record
, 0, sizeof(*record
));
388 record
->psi
= psinfo
;
390 /* Report zeroed timestamp if called before timekeeping has resumed. */
391 record
->time
= ns_to_timespec64(ktime_get_real_fast_ns());
395 * callback from kmsg_dump. Save as much as we can (up to kmsg_bytes) from the
398 static void pstore_dump(struct kmsg_dumper
*dumper
,
399 enum kmsg_dump_reason reason
)
401 unsigned long total
= 0;
403 unsigned int part
= 1;
406 why
= get_reason_str(reason
);
408 if (down_trylock(&psinfo
->buf_lock
)) {
409 /* Failed to acquire lock: give up if we cannot wait. */
410 if (pstore_cannot_wait(reason
)) {
411 pr_err("dump skipped in %s path: may corrupt error record\n",
412 in_nmi() ? "NMI" : why
);
415 if (down_interruptible(&psinfo
->buf_lock
)) {
416 pr_err("could not grab semaphore?!\n");
422 while (total
< kmsg_bytes
) {
428 struct pstore_record record
;
430 pstore_record_init(&record
, psinfo
);
431 record
.type
= PSTORE_TYPE_DMESG
;
432 record
.count
= oopscount
;
433 record
.reason
= reason
;
435 record
.buf
= psinfo
->buf
;
439 dst_size
= big_oops_buf_sz
;
442 dst_size
= psinfo
->bufsize
;
445 /* Write dump header. */
446 header_size
= snprintf(dst
, dst_size
, "%s#%d Part%u\n", why
,
448 dst_size
-= header_size
;
450 /* Write dump contents. */
451 if (!kmsg_dump_get_buffer(dumper
, true, dst
+ header_size
,
452 dst_size
, &dump_size
))
456 zipped_len
= pstore_compress(dst
, psinfo
->buf
,
457 header_size
+ dump_size
,
460 if (zipped_len
> 0) {
461 record
.compressed
= true;
462 record
.size
= zipped_len
;
464 record
.size
= copy_kmsg_to_buffer(header_size
,
468 record
.size
= header_size
+ dump_size
;
471 ret
= psinfo
->write(&record
);
472 if (ret
== 0 && reason
== KMSG_DUMP_OOPS
&& pstore_is_mounted())
473 pstore_new_entry
= 1;
475 total
+= record
.size
;
479 up(&psinfo
->buf_lock
);
482 static struct kmsg_dumper pstore_dumper
= {
487 * Register with kmsg_dump to save last part of console log on panic.
489 static void pstore_register_kmsg(void)
491 kmsg_dump_register(&pstore_dumper
);
494 static void pstore_unregister_kmsg(void)
496 kmsg_dump_unregister(&pstore_dumper
);
499 #ifdef CONFIG_PSTORE_CONSOLE
500 static void pstore_console_write(struct console
*con
, const char *s
, unsigned c
)
502 struct pstore_record record
;
507 pstore_record_init(&record
, psinfo
);
508 record
.type
= PSTORE_TYPE_CONSOLE
;
510 record
.buf
= (char *)s
;
512 psinfo
->write(&record
);
515 static struct console pstore_console
= {
517 .write
= pstore_console_write
,
518 .flags
= CON_PRINTBUFFER
| CON_ENABLED
| CON_ANYTIME
,
522 static void pstore_register_console(void)
524 register_console(&pstore_console
);
527 static void pstore_unregister_console(void)
529 unregister_console(&pstore_console
);
532 static void pstore_register_console(void) {}
533 static void pstore_unregister_console(void) {}
536 static int pstore_write_user_compat(struct pstore_record
*record
,
537 const char __user
*buf
)
544 record
->buf
= memdup_user(buf
, record
->size
);
545 if (IS_ERR(record
->buf
)) {
546 ret
= PTR_ERR(record
->buf
);
550 ret
= record
->psi
->write(record
);
556 return unlikely(ret
< 0) ? ret
: record
->size
;
560 * platform specific persistent storage driver registers with
561 * us here. If pstore is already mounted, call the platform
562 * read function right away to populate the file system. If not
563 * then the pstore mount code will call us later to fill out
566 int pstore_register(struct pstore_info
*psi
)
568 struct module
*owner
= psi
->owner
;
570 if (backend
&& strcmp(backend
, psi
->name
)) {
571 pr_warn("ignoring unexpected backend '%s'\n", psi
->name
);
575 /* Sanity check flags. */
577 pr_warn("backend '%s' must support at least one frontend\n",
582 /* Check for required functions. */
583 if (!psi
->read
|| !psi
->write
) {
584 pr_warn("backend '%s' must implement read() and write()\n",
589 spin_lock(&pstore_lock
);
591 pr_warn("backend '%s' already loaded: ignoring '%s'\n",
592 psinfo
->name
, psi
->name
);
593 spin_unlock(&pstore_lock
);
597 if (!psi
->write_user
)
598 psi
->write_user
= pstore_write_user_compat
;
600 mutex_init(&psinfo
->read_mutex
);
601 sema_init(&psinfo
->buf_lock
, 1);
602 spin_unlock(&pstore_lock
);
604 if (owner
&& !try_module_get(owner
)) {
609 allocate_buf_for_compression();
611 if (pstore_is_mounted())
612 pstore_get_records(0);
614 if (psi
->flags
& PSTORE_FLAGS_DMESG
)
615 pstore_register_kmsg();
616 if (psi
->flags
& PSTORE_FLAGS_CONSOLE
)
617 pstore_register_console();
618 if (psi
->flags
& PSTORE_FLAGS_FTRACE
)
619 pstore_register_ftrace();
620 if (psi
->flags
& PSTORE_FLAGS_PMSG
)
621 pstore_register_pmsg();
623 /* Start watching for new records, if desired. */
624 if (pstore_update_ms
>= 0) {
625 pstore_timer
.expires
= jiffies
+
626 msecs_to_jiffies(pstore_update_ms
);
627 add_timer(&pstore_timer
);
631 * Update the module parameter backend, so it is visible
632 * through /sys/module/pstore/parameters/backend
636 pr_info("Registered %s as persistent store backend\n", psi
->name
);
642 EXPORT_SYMBOL_GPL(pstore_register
);
644 void pstore_unregister(struct pstore_info
*psi
)
646 /* Stop timer and make sure all work has finished. */
647 pstore_update_ms
= -1;
648 del_timer_sync(&pstore_timer
);
649 flush_work(&pstore_work
);
651 if (psi
->flags
& PSTORE_FLAGS_PMSG
)
652 pstore_unregister_pmsg();
653 if (psi
->flags
& PSTORE_FLAGS_FTRACE
)
654 pstore_unregister_ftrace();
655 if (psi
->flags
& PSTORE_FLAGS_CONSOLE
)
656 pstore_unregister_console();
657 if (psi
->flags
& PSTORE_FLAGS_DMESG
)
658 pstore_unregister_kmsg();
660 free_buf_for_compression();
665 EXPORT_SYMBOL_GPL(pstore_unregister
);
667 static void decompress_record(struct pstore_record
*record
)
671 char *unzipped
, *workspace
;
673 if (!record
->compressed
)
676 /* Only PSTORE_TYPE_DMESG support compression. */
677 if (record
->type
!= PSTORE_TYPE_DMESG
) {
678 pr_warn("ignored compressed record type %d\n", record
->type
);
682 /* Missing compression buffer means compression was not initialized. */
684 pr_warn("no decompression method initialized!\n");
688 /* Allocate enough space to hold max decompression and ECC. */
689 unzipped_len
= big_oops_buf_sz
;
690 workspace
= kmalloc(unzipped_len
+ record
->ecc_notice_size
,
695 /* After decompression "unzipped_len" is almost certainly smaller. */
696 ret
= crypto_comp_decompress(tfm
, record
->buf
, record
->size
,
697 workspace
, &unzipped_len
);
699 pr_err("crypto_comp_decompress failed, ret = %d!\n", ret
);
704 /* Append ECC notice to decompressed buffer. */
705 memcpy(workspace
+ unzipped_len
, record
->buf
+ record
->size
,
706 record
->ecc_notice_size
);
708 /* Copy decompressed contents into an minimum-sized allocation. */
709 unzipped
= kmemdup(workspace
, unzipped_len
+ record
->ecc_notice_size
,
715 /* Swap out compressed contents with decompressed contents. */
717 record
->buf
= unzipped
;
718 record
->size
= unzipped_len
;
719 record
->compressed
= false;
723 * Read all the records from one persistent store backend. Create
724 * files in our filesystem. Don't warn about -EEXIST errors
725 * when we are re-scanning the backing store looking to add new
728 void pstore_get_backend_records(struct pstore_info
*psi
,
729 struct dentry
*root
, int quiet
)
732 unsigned int stop_loop
= 65536;
737 mutex_lock(&psi
->read_mutex
);
738 if (psi
->open
&& psi
->open(psi
))
742 * Backend callback read() allocates record.buf. decompress_record()
743 * may reallocate record.buf. On success, pstore_mkfile() will keep
744 * the record.buf, so free it only on failure.
746 for (; stop_loop
; stop_loop
--) {
747 struct pstore_record
*record
;
750 record
= kzalloc(sizeof(*record
), GFP_KERNEL
);
752 pr_err("out of memory creating record\n");
755 pstore_record_init(record
, psi
);
757 record
->size
= psi
->read(record
);
759 /* No more records left in backend? */
760 if (record
->size
<= 0) {
765 decompress_record(record
);
766 rc
= pstore_mkfile(root
, record
);
768 /* pstore_mkfile() did not take record, so free it. */
771 if (rc
!= -EEXIST
|| !quiet
)
778 mutex_unlock(&psi
->read_mutex
);
781 pr_warn("failed to create %d record(s) from '%s'\n",
784 pr_err("looping? Too many records seen from '%s'\n",
788 static void pstore_dowork(struct work_struct
*work
)
790 pstore_get_records(1);
793 static void pstore_timefunc(struct timer_list
*unused
)
795 if (pstore_new_entry
) {
796 pstore_new_entry
= 0;
797 schedule_work(&pstore_work
);
800 if (pstore_update_ms
>= 0)
801 mod_timer(&pstore_timer
,
802 jiffies
+ msecs_to_jiffies(pstore_update_ms
));
805 void __init
pstore_choose_compression(void)
807 const struct pstore_zbackend
*step
;
812 for (step
= zbackends
; step
->name
; step
++) {
813 if (!strcmp(compress
, step
->name
)) {
820 static int __init
pstore_init(void)
824 pstore_choose_compression();
827 * Check if any pstore backends registered earlier but did not
828 * initialize compression because crypto was not ready. If so,
829 * initialize compression now.
831 allocate_buf_for_compression();
833 ret
= pstore_init_fs();
839 late_initcall(pstore_init
);
841 static void __exit
pstore_exit(void)
845 module_exit(pstore_exit
)
847 module_param(compress
, charp
, 0444);
848 MODULE_PARM_DESC(compress
, "Pstore compression to use");
850 module_param(backend
, charp
, 0444);
851 MODULE_PARM_DESC(backend
, "Pstore backend to use");
853 MODULE_AUTHOR("Tony Luck <tony.luck@intel.com>");
854 MODULE_LICENSE("GPL");