Linux 4.19.133
[linux/fpc-iii.git] / fs / pstore / platform.c
blobdcd9c3163587c120b6f9fc57add528f0d3d2f88f
1 /*
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>
33 #endif
34 #if IS_ENABLED(CONFIG_PSTORE_LZ4_COMPRESS) || IS_ENABLED(CONFIG_PSTORE_LZ4HC_COMPRESS)
35 #include <linux/lz4.h>
36 #endif
37 #if IS_ENABLED(CONFIG_PSTORE_ZSTD_COMPRESS)
38 #include <linux/zstd.h>
39 #endif
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>
48 #include "internal.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;
77 static char *backend;
78 static char *compress =
79 #ifdef CONFIG_PSTORE_COMPRESS_DEFAULT
80 CONFIG_PSTORE_COMPRESS_DEFAULT;
81 #else
82 NULL;
83 #endif
85 /* Compression parameters */
86 static struct crypto_comp *tfm;
88 struct pstore_zbackend {
89 int (*zbufsize)(size_t size);
90 const char *name;
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)
101 kmsg_bytes = 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)
109 switch (reason) {
110 case KMSG_DUMP_PANIC:
111 return "Panic";
112 case KMSG_DUMP_OOPS:
113 return "Oops";
114 case KMSG_DUMP_EMERG:
115 return "Emergency";
116 case KMSG_DUMP_RESTART:
117 return "Restart";
118 case KMSG_DUMP_HALT:
119 return "Halt";
120 case KMSG_DUMP_POWEROFF:
121 return "Poweroff";
122 default:
123 return "Unknown";
128 * Should pstore_dump() wait for a concurrent pstore_dump()? If
129 * not, the current pstore_dump() will report a failure to dump
130 * and return.
132 static bool pstore_cannot_wait(enum kmsg_dump_reason reason)
134 /* In NMI path, pstore shouldn't block regardless of reason. */
135 if (in_nmi())
136 return true;
138 switch (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:
143 return true;
144 default:
145 return false;
149 #if IS_ENABLED(CONFIG_PSTORE_DEFLATE_COMPRESS)
150 static int zbufsize_deflate(size_t size)
152 size_t cmpr;
154 switch (size) {
155 /* buffer range for efivars */
156 case 1000 ... 2000:
157 cmpr = 56;
158 break;
159 case 2001 ... 3000:
160 cmpr = 54;
161 break;
162 case 3001 ... 3999:
163 cmpr = 52;
164 break;
165 /* buffer range for nvram, erst */
166 case 4000 ... 10000:
167 cmpr = 45;
168 break;
169 default:
170 cmpr = 60;
171 break;
174 return (size * 100) / cmpr;
176 #endif
178 #if IS_ENABLED(CONFIG_PSTORE_LZO_COMPRESS)
179 static int zbufsize_lzo(size_t size)
181 return lzo1x_worst_compress(size);
183 #endif
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);
190 #endif
192 #if IS_ENABLED(CONFIG_PSTORE_842_COMPRESS)
193 static int zbufsize_842(size_t size)
195 return size;
197 #endif
199 #if IS_ENABLED(CONFIG_PSTORE_ZSTD_COMPRESS)
200 static int zbufsize_zstd(size_t size)
202 return ZSTD_compressBound(size);
204 #endif
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,
212 .name = "deflate",
214 #endif
215 #if IS_ENABLED(CONFIG_PSTORE_LZO_COMPRESS)
217 .zbufsize = zbufsize_lzo,
218 .name = "lzo",
220 #endif
221 #if IS_ENABLED(CONFIG_PSTORE_LZ4_COMPRESS)
223 .zbufsize = zbufsize_lz4,
224 .name = "lz4",
226 #endif
227 #if IS_ENABLED(CONFIG_PSTORE_LZ4HC_COMPRESS)
229 .zbufsize = zbufsize_lz4,
230 .name = "lz4hc",
232 #endif
233 #if IS_ENABLED(CONFIG_PSTORE_842_COMPRESS)
235 .zbufsize = zbufsize_842,
236 .name = "842",
238 #endif
239 #if IS_ENABLED(CONFIG_PSTORE_ZSTD_COMPRESS)
241 .zbufsize = zbufsize_zstd,
242 .name = "zstd",
244 #endif
248 static int pstore_compress(const void *in, void *out,
249 unsigned int inlen, unsigned int outlen)
251 int ret;
253 ret = crypto_comp_compress(tfm, in, inlen, out, &outlen);
254 if (ret) {
255 pr_err("crypto_comp_compress failed, ret = %d!\n", ret);
256 return ret;
259 return outlen;
262 static int pstore_decompress(void *in, void *out,
263 unsigned int inlen, unsigned int outlen)
265 int ret;
267 ret = crypto_comp_decompress(tfm, in, inlen, out, &outlen);
268 if (ret) {
269 pr_err("crypto_comp_decompress failed, ret = %d!\n", ret);
270 return ret;
273 return outlen;
276 static void allocate_buf_for_compression(void)
278 struct crypto_comp *ctx;
279 int size;
280 char *buf;
282 /* Skip if not built-in or compression backend not selected yet. */
283 if (!IS_ENABLED(CONFIG_PSTORE_COMPRESS) || !zbackend)
284 return;
286 /* Skip if no pstore backend yet or compression init already done. */
287 if (!psinfo || tfm)
288 return;
290 if (!crypto_has_comp(zbackend->name, 0, 0)) {
291 pr_err("Unknown compression: %s\n", zbackend->name);
292 return;
295 size = zbackend->zbufsize(psinfo->bufsize);
296 if (size <= 0) {
297 pr_err("Invalid compression size for %s: %d\n",
298 zbackend->name, size);
299 return;
302 buf = kmalloc(size, GFP_KERNEL);
303 if (!buf) {
304 pr_err("Failed %d byte compression buffer allocation for: %s\n",
305 size, zbackend->name);
306 return;
309 ctx = crypto_alloc_comp(zbackend->name, 0, 0);
310 if (IS_ERR_OR_NULL(ctx)) {
311 kfree(buf);
312 pr_err("crypto_alloc_comp('%s') failed: %ld\n", zbackend->name,
313 PTR_ERR(ctx));
314 return;
317 /* A non-NULL big_oops_buf indicates compression is available. */
318 tfm = ctx;
319 big_oops_buf_sz = size;
320 big_oops_buf = buf;
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);
329 tfm = NULL;
331 kfree(big_oops_buf);
332 big_oops_buf = NULL;
333 big_oops_buf_sz = 0;
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)
345 size_t total_len;
346 size_t diff;
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;
356 } else
357 memcpy(psinfo->buf, big_oops_buf, total_len);
359 return 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;
382 const char *why;
383 unsigned int part = 1;
384 int ret;
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);
393 return;
395 if (down_interruptible(&psinfo->buf_lock)) {
396 pr_err("could not grab semaphore?!\n");
397 return;
401 oopscount++;
402 while (total < kmsg_bytes) {
403 char *dst;
404 size_t dst_size;
405 int header_size;
406 int zipped_len = -1;
407 size_t dump_size;
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;
414 record.part = part;
415 record.buf = psinfo->buf;
417 if (big_oops_buf) {
418 dst = big_oops_buf;
419 dst_size = big_oops_buf_sz;
420 } else {
421 dst = psinfo->buf;
422 dst_size = psinfo->bufsize;
425 /* Write dump header. */
426 header_size = snprintf(dst, dst_size, "%s#%d Part%u\n", why,
427 oopscount, part);
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))
433 break;
435 if (big_oops_buf) {
436 zipped_len = pstore_compress(dst, psinfo->buf,
437 header_size + dump_size,
438 psinfo->bufsize);
440 if (zipped_len > 0) {
441 record.compressed = true;
442 record.size = zipped_len;
443 } else {
444 record.size = copy_kmsg_to_buffer(header_size,
445 dump_size);
447 } else {
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;
456 part++;
459 up(&psinfo->buf_lock);
462 static struct kmsg_dumper pstore_dumper = {
463 .dump = pstore_dump,
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;
488 record.size = c;
489 psinfo->write(&record);
492 static struct console pstore_console = {
493 .name = "pstore",
494 .write = pstore_console_write,
495 .flags = CON_PRINTBUFFER | CON_ENABLED | CON_ANYTIME,
496 .index = -1,
499 static void pstore_register_console(void)
501 register_console(&pstore_console);
504 static void pstore_unregister_console(void)
506 unregister_console(&pstore_console);
508 #else
509 static void pstore_register_console(void) {}
510 static void pstore_unregister_console(void) {}
511 #endif
513 static int pstore_write_user_compat(struct pstore_record *record,
514 const char __user *buf)
516 int ret = 0;
518 if (record->buf)
519 return -EINVAL;
521 record->buf = memdup_user(buf, record->size);
522 if (IS_ERR(record->buf)) {
523 ret = PTR_ERR(record->buf);
524 goto out;
527 ret = record->psi->write(record);
529 kfree(record->buf);
530 out:
531 record->buf = NULL;
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
541 * the file system.
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);
549 return -EPERM;
552 /* Sanity check flags. */
553 if (!psi->flags) {
554 pr_warn("backend '%s' must support at least one frontend\n",
555 psi->name);
556 return -EINVAL;
559 /* Check for required functions. */
560 if (!psi->read || !psi->write) {
561 pr_warn("backend '%s' must implement read() and write()\n",
562 psi->name);
563 return -EINVAL;
566 spin_lock(&pstore_lock);
567 if (psinfo) {
568 pr_warn("backend '%s' already loaded: ignoring '%s'\n",
569 psinfo->name, psi->name);
570 spin_unlock(&pstore_lock);
571 return -EBUSY;
574 if (!psi->write_user)
575 psi->write_user = pstore_write_user_compat;
576 psinfo = psi;
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)) {
582 psinfo = NULL;
583 return -EINVAL;
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
612 backend = psi->name;
614 pr_info("Registered %s as persistent store backend\n", psi->name);
616 module_put(owner);
618 return 0;
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();
640 psinfo = NULL;
641 backend = NULL;
643 EXPORT_SYMBOL_GPL(pstore_unregister);
645 static void decompress_record(struct pstore_record *record)
647 int unzipped_len;
648 char *decompressed;
650 if (!record->compressed)
651 return;
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);
656 return;
659 /* No compression method has created the common buffer. */
660 if (!big_oops_buf) {
661 pr_warn("no decompression buffer allocated\n");
662 return;
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);
669 return;
672 /* Build new buffer for decompressed contents. */
673 decompressed = kmalloc(unzipped_len + record->ecc_notice_size,
674 GFP_KERNEL);
675 if (!decompressed) {
676 pr_err("decompression ran out of memory\n");
677 return;
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. */
686 kfree(record->buf);
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
696 * error records.
698 void pstore_get_backend_records(struct pstore_info *psi,
699 struct dentry *root, int quiet)
701 int failed = 0;
702 unsigned int stop_loop = 65536;
704 if (!psi || !root)
705 return;
707 mutex_lock(&psi->read_mutex);
708 if (psi->open && psi->open(psi))
709 goto out;
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;
718 int rc;
720 record = kzalloc(sizeof(*record), GFP_KERNEL);
721 if (!record) {
722 pr_err("out of memory creating record\n");
723 break;
725 pstore_record_init(record, psi);
727 record->size = psi->read(record);
729 /* No more records left in backend? */
730 if (record->size <= 0) {
731 kfree(record);
732 break;
735 decompress_record(record);
736 rc = pstore_mkfile(root, record);
737 if (rc) {
738 /* pstore_mkfile() did not take record, so free it. */
739 kfree(record->buf);
740 kfree(record);
741 if (rc != -EEXIST || !quiet)
742 failed++;
745 if (psi->close)
746 psi->close(psi);
747 out:
748 mutex_unlock(&psi->read_mutex);
750 if (failed)
751 pr_warn("failed to create %d record(s) from '%s'\n",
752 failed, psi->name);
753 if (!stop_loop)
754 pr_err("looping? Too many records seen from '%s'\n",
755 psi->name);
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;
779 if (!compress)
780 return;
782 for (step = zbackends; step->name; step++) {
783 if (!strcmp(compress, step->name)) {
784 zbackend = step;
785 return;
790 static int __init pstore_init(void)
792 int ret;
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();
804 if (ret)
805 free_buf_for_compression();
807 return ret;
809 late_initcall(pstore_init);
811 static void __exit pstore_exit(void)
813 pstore_exit_fs();
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");