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 ret
= crypto_comp_compress(tfm
, in
, inlen
, out
, &outlen
);
255 pr_err("crypto_comp_compress failed, ret = %d!\n", ret
);
262 static int pstore_decompress(void *in
, void *out
,
263 unsigned int inlen
, unsigned int outlen
)
267 ret
= crypto_comp_decompress(tfm
, in
, inlen
, out
, &outlen
);
269 pr_err("crypto_comp_decompress failed, ret = %d!\n", ret
);
276 static void allocate_buf_for_compression(void)
278 struct crypto_comp
*ctx
;
282 /* Skip if not built-in or compression backend not selected yet. */
283 if (!IS_ENABLED(CONFIG_PSTORE_COMPRESS
) || !zbackend
)
286 /* Skip if no pstore backend yet or compression init already done. */
290 if (!crypto_has_comp(zbackend
->name
, 0, 0)) {
291 pr_err("Unknown compression: %s\n", zbackend
->name
);
295 size
= zbackend
->zbufsize(psinfo
->bufsize
);
297 pr_err("Invalid compression size for %s: %d\n",
298 zbackend
->name
, size
);
302 buf
= kmalloc(size
, GFP_KERNEL
);
304 pr_err("Failed %d byte compression buffer allocation for: %s\n",
305 size
, zbackend
->name
);
309 ctx
= crypto_alloc_comp(zbackend
->name
, 0, 0);
310 if (IS_ERR_OR_NULL(ctx
)) {
312 pr_err("crypto_alloc_comp('%s') failed: %ld\n", zbackend
->name
,
317 /* A non-NULL big_oops_buf indicates compression is available. */
319 big_oops_buf_sz
= size
;
322 pr_info("Using compression: %s\n", zbackend
->name
);
325 static void free_buf_for_compression(void)
327 if (IS_ENABLED(CONFIG_PSTORE_COMPRESS
) && tfm
) {
328 crypto_free_comp(tfm
);
337 * Called when compression fails, since the printk buffer
338 * would be fetched for compression calling it again when
339 * compression fails would have moved the iterator of
340 * printk buffer which results in fetching old contents.
341 * Copy the recent messages from big_oops_buf to psinfo->buf
343 static size_t copy_kmsg_to_buffer(int hsize
, size_t len
)
348 total_len
= hsize
+ len
;
350 if (total_len
> psinfo
->bufsize
) {
351 diff
= total_len
- psinfo
->bufsize
+ hsize
;
352 memcpy(psinfo
->buf
, big_oops_buf
, hsize
);
353 memcpy(psinfo
->buf
+ hsize
, big_oops_buf
+ diff
,
354 psinfo
->bufsize
- hsize
);
355 total_len
= psinfo
->bufsize
;
357 memcpy(psinfo
->buf
, big_oops_buf
, total_len
);
362 void pstore_record_init(struct pstore_record
*record
,
363 struct pstore_info
*psinfo
)
365 memset(record
, 0, sizeof(*record
));
367 record
->psi
= psinfo
;
369 /* Report zeroed timestamp if called before timekeeping has resumed. */
370 record
->time
= ns_to_timespec64(ktime_get_real_fast_ns());
374 * callback from kmsg_dump. (s2,l2) has the most recently
375 * written bytes, older bytes are in (s1,l1). Save as much
376 * as we can from the end of the buffer.
378 static void pstore_dump(struct kmsg_dumper
*dumper
,
379 enum kmsg_dump_reason reason
)
381 unsigned long total
= 0;
383 unsigned int part
= 1;
386 why
= get_reason_str(reason
);
388 if (down_trylock(&psinfo
->buf_lock
)) {
389 /* Failed to acquire lock: give up if we cannot wait. */
390 if (pstore_cannot_wait(reason
)) {
391 pr_err("dump skipped in %s path: may corrupt error record\n",
392 in_nmi() ? "NMI" : why
);
395 if (down_interruptible(&psinfo
->buf_lock
)) {
396 pr_err("could not grab semaphore?!\n");
402 while (total
< kmsg_bytes
) {
408 struct pstore_record record
;
410 pstore_record_init(&record
, psinfo
);
411 record
.type
= PSTORE_TYPE_DMESG
;
412 record
.count
= oopscount
;
413 record
.reason
= reason
;
415 record
.buf
= psinfo
->buf
;
419 dst_size
= big_oops_buf_sz
;
422 dst_size
= psinfo
->bufsize
;
425 /* Write dump header. */
426 header_size
= snprintf(dst
, dst_size
, "%s#%d Part%u\n", why
,
428 dst_size
-= header_size
;
430 /* Write dump contents. */
431 if (!kmsg_dump_get_buffer(dumper
, true, dst
+ header_size
,
432 dst_size
, &dump_size
))
436 zipped_len
= pstore_compress(dst
, psinfo
->buf
,
437 header_size
+ dump_size
,
440 if (zipped_len
> 0) {
441 record
.compressed
= true;
442 record
.size
= zipped_len
;
444 record
.size
= copy_kmsg_to_buffer(header_size
,
448 record
.size
= header_size
+ dump_size
;
451 ret
= psinfo
->write(&record
);
452 if (ret
== 0 && reason
== KMSG_DUMP_OOPS
&& pstore_is_mounted())
453 pstore_new_entry
= 1;
455 total
+= record
.size
;
459 up(&psinfo
->buf_lock
);
462 static struct kmsg_dumper pstore_dumper
= {
467 * Register with kmsg_dump to save last part of console log on panic.
469 static void pstore_register_kmsg(void)
471 kmsg_dump_register(&pstore_dumper
);
474 static void pstore_unregister_kmsg(void)
476 kmsg_dump_unregister(&pstore_dumper
);
479 #ifdef CONFIG_PSTORE_CONSOLE
480 static void pstore_console_write(struct console
*con
, const char *s
, unsigned c
)
482 struct pstore_record record
;
484 pstore_record_init(&record
, psinfo
);
485 record
.type
= PSTORE_TYPE_CONSOLE
;
487 record
.buf
= (char *)s
;
489 psinfo
->write(&record
);
492 static struct console pstore_console
= {
494 .write
= pstore_console_write
,
495 .flags
= CON_PRINTBUFFER
| CON_ENABLED
| CON_ANYTIME
,
499 static void pstore_register_console(void)
501 register_console(&pstore_console
);
504 static void pstore_unregister_console(void)
506 unregister_console(&pstore_console
);
509 static void pstore_register_console(void) {}
510 static void pstore_unregister_console(void) {}
513 static int pstore_write_user_compat(struct pstore_record
*record
,
514 const char __user
*buf
)
521 record
->buf
= memdup_user(buf
, record
->size
);
522 if (IS_ERR(record
->buf
)) {
523 ret
= PTR_ERR(record
->buf
);
527 ret
= record
->psi
->write(record
);
533 return unlikely(ret
< 0) ? ret
: record
->size
;
537 * platform specific persistent storage driver registers with
538 * us here. If pstore is already mounted, call the platform
539 * read function right away to populate the file system. If not
540 * then the pstore mount code will call us later to fill out
543 int pstore_register(struct pstore_info
*psi
)
545 struct module
*owner
= psi
->owner
;
547 if (backend
&& strcmp(backend
, psi
->name
)) {
548 pr_warn("ignoring unexpected backend '%s'\n", psi
->name
);
552 /* Sanity check flags. */
554 pr_warn("backend '%s' must support at least one frontend\n",
559 /* Check for required functions. */
560 if (!psi
->read
|| !psi
->write
) {
561 pr_warn("backend '%s' must implement read() and write()\n",
566 spin_lock(&pstore_lock
);
568 pr_warn("backend '%s' already loaded: ignoring '%s'\n",
569 psinfo
->name
, psi
->name
);
570 spin_unlock(&pstore_lock
);
574 if (!psi
->write_user
)
575 psi
->write_user
= pstore_write_user_compat
;
577 mutex_init(&psinfo
->read_mutex
);
578 sema_init(&psinfo
->buf_lock
, 1);
579 spin_unlock(&pstore_lock
);
581 if (owner
&& !try_module_get(owner
)) {
586 if (psi
->flags
& PSTORE_FLAGS_DMESG
)
587 allocate_buf_for_compression();
589 if (pstore_is_mounted())
590 pstore_get_records(0);
592 if (psi
->flags
& PSTORE_FLAGS_DMESG
)
593 pstore_register_kmsg();
594 if (psi
->flags
& PSTORE_FLAGS_CONSOLE
)
595 pstore_register_console();
596 if (psi
->flags
& PSTORE_FLAGS_FTRACE
)
597 pstore_register_ftrace();
598 if (psi
->flags
& PSTORE_FLAGS_PMSG
)
599 pstore_register_pmsg();
601 /* Start watching for new records, if desired. */
602 if (pstore_update_ms
>= 0) {
603 pstore_timer
.expires
= jiffies
+
604 msecs_to_jiffies(pstore_update_ms
);
605 add_timer(&pstore_timer
);
609 * Update the module parameter backend, so it is visible
610 * through /sys/module/pstore/parameters/backend
614 pr_info("Registered %s as persistent store backend\n", psi
->name
);
620 EXPORT_SYMBOL_GPL(pstore_register
);
622 void pstore_unregister(struct pstore_info
*psi
)
624 /* Stop timer and make sure all work has finished. */
625 pstore_update_ms
= -1;
626 del_timer_sync(&pstore_timer
);
627 flush_work(&pstore_work
);
629 if (psi
->flags
& PSTORE_FLAGS_PMSG
)
630 pstore_unregister_pmsg();
631 if (psi
->flags
& PSTORE_FLAGS_FTRACE
)
632 pstore_unregister_ftrace();
633 if (psi
->flags
& PSTORE_FLAGS_CONSOLE
)
634 pstore_unregister_console();
635 if (psi
->flags
& PSTORE_FLAGS_DMESG
)
636 pstore_unregister_kmsg();
638 free_buf_for_compression();
643 EXPORT_SYMBOL_GPL(pstore_unregister
);
645 static void decompress_record(struct pstore_record
*record
)
650 if (!record
->compressed
)
653 /* Only PSTORE_TYPE_DMESG support compression. */
654 if (record
->type
!= PSTORE_TYPE_DMESG
) {
655 pr_warn("ignored compressed record type %d\n", record
->type
);
659 /* No compression method has created the common buffer. */
661 pr_warn("no decompression buffer allocated\n");
665 unzipped_len
= pstore_decompress(record
->buf
, big_oops_buf
,
666 record
->size
, big_oops_buf_sz
);
667 if (unzipped_len
<= 0) {
668 pr_err("decompression failed: %d\n", unzipped_len
);
672 /* Build new buffer for decompressed contents. */
673 decompressed
= kmalloc(unzipped_len
+ record
->ecc_notice_size
,
676 pr_err("decompression ran out of memory\n");
679 memcpy(decompressed
, big_oops_buf
, unzipped_len
);
681 /* Append ECC notice to decompressed buffer. */
682 memcpy(decompressed
+ unzipped_len
, record
->buf
+ record
->size
,
683 record
->ecc_notice_size
);
685 /* Swap out compresed contents with decompressed contents. */
687 record
->buf
= decompressed
;
688 record
->size
= unzipped_len
;
689 record
->compressed
= false;
693 * Read all the records from one persistent store backend. Create
694 * files in our filesystem. Don't warn about -EEXIST errors
695 * when we are re-scanning the backing store looking to add new
698 void pstore_get_backend_records(struct pstore_info
*psi
,
699 struct dentry
*root
, int quiet
)
702 unsigned int stop_loop
= 65536;
707 mutex_lock(&psi
->read_mutex
);
708 if (psi
->open
&& psi
->open(psi
))
712 * Backend callback read() allocates record.buf. decompress_record()
713 * may reallocate record.buf. On success, pstore_mkfile() will keep
714 * the record.buf, so free it only on failure.
716 for (; stop_loop
; stop_loop
--) {
717 struct pstore_record
*record
;
720 record
= kzalloc(sizeof(*record
), GFP_KERNEL
);
722 pr_err("out of memory creating record\n");
725 pstore_record_init(record
, psi
);
727 record
->size
= psi
->read(record
);
729 /* No more records left in backend? */
730 if (record
->size
<= 0) {
735 decompress_record(record
);
736 rc
= pstore_mkfile(root
, record
);
738 /* pstore_mkfile() did not take record, so free it. */
741 if (rc
!= -EEXIST
|| !quiet
)
748 mutex_unlock(&psi
->read_mutex
);
751 pr_warn("failed to create %d record(s) from '%s'\n",
754 pr_err("looping? Too many records seen from '%s'\n",
758 static void pstore_dowork(struct work_struct
*work
)
760 pstore_get_records(1);
763 static void pstore_timefunc(struct timer_list
*unused
)
765 if (pstore_new_entry
) {
766 pstore_new_entry
= 0;
767 schedule_work(&pstore_work
);
770 if (pstore_update_ms
>= 0)
771 mod_timer(&pstore_timer
,
772 jiffies
+ msecs_to_jiffies(pstore_update_ms
));
775 void __init
pstore_choose_compression(void)
777 const struct pstore_zbackend
*step
;
782 for (step
= zbackends
; step
->name
; step
++) {
783 if (!strcmp(compress
, step
->name
)) {
790 static int __init
pstore_init(void)
794 pstore_choose_compression();
797 * Check if any pstore backends registered earlier but did not
798 * initialize compression because crypto was not ready. If so,
799 * initialize compression now.
801 allocate_buf_for_compression();
803 ret
= pstore_init_fs();
805 free_buf_for_compression();
809 late_initcall(pstore_init
);
811 static void __exit
pstore_exit(void)
815 module_exit(pstore_exit
)
817 module_param(compress
, charp
, 0444);
818 MODULE_PARM_DESC(compress
, "Pstore compression to use");
820 module_param(backend
, charp
, 0444);
821 MODULE_PARM_DESC(backend
, "Pstore backend to use");
823 MODULE_AUTHOR("Tony Luck <tony.luck@intel.com>");
824 MODULE_LICENSE("GPL");