1 // SPDX-License-Identifier: GPL-2.0-only
3 * Persistent Storage - platform driver interface parts.
5 * Copyright (C) 2007-2008 Google, Inc.
6 * Copyright (C) 2010 Intel Corporation <tony.luck@intel.com>
9 #define pr_fmt(fmt) "pstore: " fmt
11 #include <linux/atomic.h>
12 #include <linux/types.h>
13 #include <linux/errno.h>
14 #include <linux/init.h>
15 #include <linux/kmsg_dump.h>
16 #include <linux/console.h>
17 #include <linux/module.h>
18 #include <linux/pstore.h>
19 #include <linux/crypto.h>
20 #include <linux/string.h>
21 #include <linux/timer.h>
22 #include <linux/slab.h>
23 #include <linux/uaccess.h>
24 #include <linux/jiffies.h>
25 #include <linux/workqueue.h>
30 * We defer making "oops" entries appear in pstore - see
31 * whether the system is actually still running well enough
32 * to let someone see the entry
34 static int pstore_update_ms = -1;
35 module_param_named(update_ms, pstore_update_ms, int, 0600);
36 MODULE_PARM_DESC(update_ms, "milliseconds before pstore updates its content "
37 "(default is -1, which means runtime updates are disabled; "
38 "enabling this option may not be safe; it may lead to further "
39 "corruption on Oopses)");
41 /* Names should be in the same order as the enum pstore_type_id */
42 static const char * const pstore_type_names[] = {
54 static int pstore_new_entry;
56 static void pstore_timefunc(struct timer_list *);
57 static DEFINE_TIMER(pstore_timer, pstore_timefunc);
59 static void pstore_dowork(struct work_struct *);
60 static DECLARE_WORK(pstore_work, pstore_dowork);
63 * psinfo_lock protects "psinfo" during calls to
64 * pstore_register(), pstore_unregister(), and
65 * the filesystem mount/unmount routines.
67 static DEFINE_MUTEX(psinfo_lock);
68 struct pstore_info *psinfo;
71 module_param(backend, charp, 0444);
72 MODULE_PARM_DESC(backend, "specific backend to use");
74 static char *compress =
75 #ifdef CONFIG_PSTORE_COMPRESS_DEFAULT
76 CONFIG_PSTORE_COMPRESS_DEFAULT;
80 module_param(compress, charp, 0444);
81 MODULE_PARM_DESC(compress, "compression to use");
83 /* How much of the kernel log to snapshot */
84 unsigned long kmsg_bytes = CONFIG_PSTORE_DEFAULT_KMSG_BYTES;
85 module_param(kmsg_bytes, ulong, 0444);
86 MODULE_PARM_DESC(kmsg_bytes, "amount of kernel log to snapshot (in bytes)");
88 /* Compression parameters */
89 static struct crypto_comp *tfm;
91 static char *big_oops_buf;
93 void pstore_set_kmsg_bytes(int bytes)
98 /* Tag each group of saved records with a sequence number */
101 const char *pstore_type_to_name(enum pstore_type_id type)
103 BUILD_BUG_ON(ARRAY_SIZE(pstore_type_names) != PSTORE_TYPE_MAX);
105 if (WARN_ON_ONCE(type >= PSTORE_TYPE_MAX))
108 return pstore_type_names[type];
110 EXPORT_SYMBOL_GPL(pstore_type_to_name);
112 enum pstore_type_id pstore_name_to_type(const char *name)
116 for (i = 0; i < PSTORE_TYPE_MAX; i++) {
117 if (!strcmp(pstore_type_names[i], name))
121 return PSTORE_TYPE_MAX;
123 EXPORT_SYMBOL_GPL(pstore_name_to_type);
125 static void pstore_timer_kick(void)
127 if (pstore_update_ms < 0)
130 mod_timer(&pstore_timer, jiffies + msecs_to_jiffies(pstore_update_ms));
133 static bool pstore_cannot_block_path(enum kmsg_dump_reason reason)
136 * In case of NMI path, pstore shouldn't be blocked
137 * regardless of reason.
143 /* In panic case, other cpus are stopped by smp_send_stop(). */
144 case KMSG_DUMP_PANIC:
146 * Emergency restart shouldn't be blocked by spinning on
147 * pstore_info::buf_lock.
149 case KMSG_DUMP_EMERG:
156 static int pstore_compress(const void *in, void *out,
157 unsigned int inlen, unsigned int outlen)
161 if (!IS_ENABLED(CONFIG_PSTORE_COMPRESS))
164 ret = crypto_comp_compress(tfm, in, inlen, out, &outlen);
166 pr_err("crypto_comp_compress failed, ret = %d!\n", ret);
173 static void allocate_buf_for_compression(void)
175 struct crypto_comp *ctx;
178 /* Skip if not built-in or compression backend not selected yet. */
179 if (!IS_ENABLED(CONFIG_PSTORE_COMPRESS) || !compress)
182 /* Skip if no pstore backend yet or compression init already done. */
186 if (!crypto_has_comp(compress, 0, 0)) {
187 pr_err("Unknown compression: %s\n", compress);
192 * The compression buffer only needs to be as large as the maximum
193 * uncompressed record size, since any record that would be expanded by
194 * compression is just stored uncompressed.
196 buf = kmalloc(psinfo->bufsize, GFP_KERNEL);
198 pr_err("Failed %zu byte compression buffer allocation for: %s\n",
199 psinfo->bufsize, compress);
203 ctx = crypto_alloc_comp(compress, 0, 0);
204 if (IS_ERR_OR_NULL(ctx)) {
206 pr_err("crypto_alloc_comp('%s') failed: %ld\n", compress,
211 /* A non-NULL big_oops_buf indicates compression is available. */
215 pr_info("Using crash dump compression: %s\n", compress);
218 static void free_buf_for_compression(void)
220 if (IS_ENABLED(CONFIG_PSTORE_COMPRESS) && tfm) {
221 crypto_free_comp(tfm);
228 void pstore_record_init(struct pstore_record *record,
229 struct pstore_info *psinfo)
231 memset(record, 0, sizeof(*record));
233 record->psi = psinfo;
235 /* Report zeroed timestamp if called before timekeeping has resumed. */
236 record->time = ns_to_timespec64(ktime_get_real_fast_ns());
240 * callback from kmsg_dump. Save as much as we can (up to kmsg_bytes) from the
243 static void pstore_dump(struct kmsg_dumper *dumper,
244 enum kmsg_dump_reason reason)
246 struct kmsg_dump_iter iter;
247 unsigned long total = 0;
249 unsigned int part = 1;
250 unsigned long flags = 0;
254 why = kmsg_dump_reason_str(reason);
256 if (pstore_cannot_block_path(reason)) {
257 if (!spin_trylock_irqsave(&psinfo->buf_lock, flags)) {
258 pr_err("dump skipped in %s path because of concurrent dump\n",
259 in_nmi() ? "NMI" : why);
263 spin_lock_irqsave(&psinfo->buf_lock, flags);
266 kmsg_dump_rewind(&iter);
269 while (total < kmsg_bytes) {
275 struct pstore_record record;
277 pstore_record_init(&record, psinfo);
278 record.type = PSTORE_TYPE_DMESG;
279 record.count = oopscount;
280 record.reason = reason;
282 record.buf = psinfo->buf;
284 dst = big_oops_buf ?: psinfo->buf;
285 dst_size = psinfo->bufsize;
287 /* Write dump header. */
288 header_size = snprintf(dst, dst_size, "%s#%d Part%u\n", why,
290 dst_size -= header_size;
292 /* Write dump contents. */
293 if (!kmsg_dump_get_buffer(&iter, true, dst + header_size,
294 dst_size, &dump_size))
298 zipped_len = pstore_compress(dst, psinfo->buf,
299 header_size + dump_size,
302 if (zipped_len > 0) {
303 record.compressed = true;
304 record.size = zipped_len;
306 record.size = header_size + dump_size;
307 memcpy(psinfo->buf, dst, record.size);
310 record.size = header_size + dump_size;
313 ret = psinfo->write(&record);
314 if (ret == 0 && reason == KMSG_DUMP_OOPS) {
315 pstore_new_entry = 1;
318 /* Preserve only the first non-zero returned value. */
323 total += record.size;
326 spin_unlock_irqrestore(&psinfo->buf_lock, flags);
329 pr_err_once("backend (%s) writing error (%d)\n", psinfo->name,
334 static struct kmsg_dumper pstore_dumper = {
339 * Register with kmsg_dump to save last part of console log on panic.
341 static void pstore_register_kmsg(void)
343 kmsg_dump_register(&pstore_dumper);
346 static void pstore_unregister_kmsg(void)
348 kmsg_dump_unregister(&pstore_dumper);
351 #ifdef CONFIG_PSTORE_CONSOLE
352 static void pstore_console_write(struct console *con, const char *s, unsigned c)
354 struct pstore_record record;
359 pstore_record_init(&record, psinfo);
360 record.type = PSTORE_TYPE_CONSOLE;
362 record.buf = (char *)s;
364 psinfo->write(&record);
367 static struct console pstore_console = {
368 .write = pstore_console_write,
372 static void pstore_register_console(void)
374 /* Show which backend is going to get console writes. */
375 strscpy(pstore_console.name, psinfo->name,
376 sizeof(pstore_console.name));
378 * Always initialize flags here since prior unregister_console()
379 * calls may have changed settings (specifically CON_ENABLED).
381 pstore_console.flags = CON_PRINTBUFFER | CON_ENABLED | CON_ANYTIME;
382 register_console(&pstore_console);
385 static void pstore_unregister_console(void)
387 unregister_console(&pstore_console);
390 static void pstore_register_console(void) {}
391 static void pstore_unregister_console(void) {}
394 static int pstore_write_user_compat(struct pstore_record *record,
395 const char __user *buf)
402 record->buf = memdup_user(buf, record->size);
403 if (IS_ERR(record->buf)) {
404 ret = PTR_ERR(record->buf);
408 ret = record->psi->write(record);
414 return unlikely(ret < 0) ? ret : record->size;
418 * platform specific persistent storage driver registers with
419 * us here. If pstore is already mounted, call the platform
420 * read function right away to populate the file system. If not
421 * then the pstore mount code will call us later to fill out
424 int pstore_register(struct pstore_info *psi)
426 if (backend && strcmp(backend, psi->name)) {
427 pr_warn("backend '%s' already in use: ignoring '%s'\n",
432 /* Sanity check flags. */
434 pr_warn("backend '%s' must support at least one frontend\n",
439 /* Check for required functions. */
440 if (!psi->read || !psi->write) {
441 pr_warn("backend '%s' must implement read() and write()\n",
446 mutex_lock(&psinfo_lock);
448 pr_warn("backend '%s' already loaded: ignoring '%s'\n",
449 psinfo->name, psi->name);
450 mutex_unlock(&psinfo_lock);
454 if (!psi->write_user)
455 psi->write_user = pstore_write_user_compat;
457 mutex_init(&psinfo->read_mutex);
458 spin_lock_init(&psinfo->buf_lock);
460 if (psi->flags & PSTORE_FLAGS_DMESG)
461 allocate_buf_for_compression();
463 pstore_get_records(0);
465 if (psi->flags & PSTORE_FLAGS_DMESG) {
466 pstore_dumper.max_reason = psinfo->max_reason;
467 pstore_register_kmsg();
469 if (psi->flags & PSTORE_FLAGS_CONSOLE)
470 pstore_register_console();
471 if (psi->flags & PSTORE_FLAGS_FTRACE)
472 pstore_register_ftrace();
473 if (psi->flags & PSTORE_FLAGS_PMSG)
474 pstore_register_pmsg();
476 /* Start watching for new records, if desired. */
480 * Update the module parameter backend, so it is visible
481 * through /sys/module/pstore/parameters/backend
483 backend = kstrdup(psi->name, GFP_KERNEL);
485 pr_info("Registered %s as persistent store backend\n", psi->name);
487 mutex_unlock(&psinfo_lock);
490 EXPORT_SYMBOL_GPL(pstore_register);
492 void pstore_unregister(struct pstore_info *psi)
494 /* It's okay to unregister nothing. */
498 mutex_lock(&psinfo_lock);
500 /* Only one backend can be registered at a time. */
501 if (WARN_ON(psi != psinfo)) {
502 mutex_unlock(&psinfo_lock);
506 /* Unregister all callbacks. */
507 if (psi->flags & PSTORE_FLAGS_PMSG)
508 pstore_unregister_pmsg();
509 if (psi->flags & PSTORE_FLAGS_FTRACE)
510 pstore_unregister_ftrace();
511 if (psi->flags & PSTORE_FLAGS_CONSOLE)
512 pstore_unregister_console();
513 if (psi->flags & PSTORE_FLAGS_DMESG)
514 pstore_unregister_kmsg();
516 /* Stop timer and make sure all work has finished. */
517 del_timer_sync(&pstore_timer);
518 flush_work(&pstore_work);
520 /* Remove all backend records from filesystem tree. */
521 pstore_put_backend_records(psi);
523 free_buf_for_compression();
529 pr_info("Unregistered %s as persistent store backend\n", psi->name);
530 mutex_unlock(&psinfo_lock);
532 EXPORT_SYMBOL_GPL(pstore_unregister);
534 static void decompress_record(struct pstore_record *record)
538 char *unzipped, *workspace;
540 if (!IS_ENABLED(CONFIG_PSTORE_COMPRESS) || !record->compressed)
543 /* Only PSTORE_TYPE_DMESG support compression. */
544 if (record->type != PSTORE_TYPE_DMESG) {
545 pr_warn("ignored compressed record type %d\n", record->type);
549 /* Missing compression buffer means compression was not initialized. */
551 pr_warn("no decompression method initialized!\n");
555 /* Allocate enough space to hold max decompression and ECC. */
556 workspace = kmalloc(psinfo->bufsize + record->ecc_notice_size,
561 /* After decompression "unzipped_len" is almost certainly smaller. */
562 ret = crypto_comp_decompress(tfm, record->buf, record->size,
563 workspace, &unzipped_len);
565 pr_err("crypto_comp_decompress failed, ret = %d!\n", ret);
570 /* Append ECC notice to decompressed buffer. */
571 memcpy(workspace + unzipped_len, record->buf + record->size,
572 record->ecc_notice_size);
574 /* Copy decompressed contents into an minimum-sized allocation. */
575 unzipped = kmemdup(workspace, unzipped_len + record->ecc_notice_size,
581 /* Swap out compressed contents with decompressed contents. */
583 record->buf = unzipped;
584 record->size = unzipped_len;
585 record->compressed = false;
589 * Read all the records from one persistent store backend. Create
590 * files in our filesystem. Don't warn about -EEXIST errors
591 * when we are re-scanning the backing store looking to add new
594 void pstore_get_backend_records(struct pstore_info *psi,
595 struct dentry *root, int quiet)
598 unsigned int stop_loop = 65536;
603 mutex_lock(&psi->read_mutex);
604 if (psi->open && psi->open(psi))
608 * Backend callback read() allocates record.buf. decompress_record()
609 * may reallocate record.buf. On success, pstore_mkfile() will keep
610 * the record.buf, so free it only on failure.
612 for (; stop_loop; stop_loop--) {
613 struct pstore_record *record;
616 record = kzalloc(sizeof(*record), GFP_KERNEL);
618 pr_err("out of memory creating record\n");
621 pstore_record_init(record, psi);
623 record->size = psi->read(record);
625 /* No more records left in backend? */
626 if (record->size <= 0) {
631 decompress_record(record);
632 rc = pstore_mkfile(root, record);
634 /* pstore_mkfile() did not take record, so free it. */
638 if (rc != -EEXIST || !quiet)
645 mutex_unlock(&psi->read_mutex);
648 pr_warn("failed to create %d record(s) from '%s'\n",
651 pr_err("looping? Too many records seen from '%s'\n",
655 static void pstore_dowork(struct work_struct *work)
657 pstore_get_records(1);
660 static void pstore_timefunc(struct timer_list *unused)
662 if (pstore_new_entry) {
663 pstore_new_entry = 0;
664 schedule_work(&pstore_work);
670 static int __init pstore_init(void)
675 * Check if any pstore backends registered earlier but did not
676 * initialize compression because crypto was not ready. If so,
677 * initialize compression now.
679 allocate_buf_for_compression();
681 ret = pstore_init_fs();
683 free_buf_for_compression();
687 late_initcall(pstore_init);
689 static void __exit pstore_exit(void)
693 module_exit(pstore_exit)
695 MODULE_AUTHOR("Tony Luck <tony.luck@intel.com>");
696 MODULE_LICENSE("GPL");