145051539SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 2ca01d6ddSTony Luck /* 3ca01d6ddSTony Luck * Persistent Storage - platform driver interface parts. 4ca01d6ddSTony Luck * 5f29e5956SAnton Vorontsov * Copyright (C) 2007-2008 Google, Inc. 6ca01d6ddSTony Luck * Copyright (C) 2010 Intel Corporation <tony.luck@intel.com> 7ca01d6ddSTony Luck */ 8ca01d6ddSTony Luck 9ef748853SFabian Frederick #define pr_fmt(fmt) "pstore: " fmt 10ef748853SFabian Frederick 11ca01d6ddSTony Luck #include <linux/atomic.h> 12ca01d6ddSTony Luck #include <linux/types.h> 13ca01d6ddSTony Luck #include <linux/errno.h> 14ca01d6ddSTony Luck #include <linux/init.h> 15ca01d6ddSTony Luck #include <linux/kmsg_dump.h> 16f29e5956SAnton Vorontsov #include <linux/console.h> 17*104fd0b5SYuxiao Zhang #include <linux/mm.h> 18ca01d6ddSTony Luck #include <linux/module.h> 19ca01d6ddSTony Luck #include <linux/pstore.h> 20ca01d6ddSTony Luck #include <linux/string.h> 216dda9266SLuck, Tony #include <linux/timer.h> 22ca01d6ddSTony Luck #include <linux/slab.h> 23ca01d6ddSTony Luck #include <linux/uaccess.h> 24a3f5f075SAnton Vorontsov #include <linux/jiffies.h> 25438b8050SArd Biesheuvel #include <linux/vmalloc.h> 266dda9266SLuck, Tony #include <linux/workqueue.h> 27438b8050SArd Biesheuvel #include <linux/zlib.h> 28ca01d6ddSTony Luck 29ca01d6ddSTony Luck #include "internal.h" 30ca01d6ddSTony Luck 31ca01d6ddSTony Luck /* 326dda9266SLuck, Tony * We defer making "oops" entries appear in pstore - see 336dda9266SLuck, Tony * whether the system is actually still running well enough 346dda9266SLuck, Tony * to let someone see the entry 356dda9266SLuck, Tony */ 36521f7288SAnton Vorontsov static int pstore_update_ms = -1; 37a3f5f075SAnton Vorontsov module_param_named(update_ms, pstore_update_ms, int, 0600); 38a3f5f075SAnton Vorontsov MODULE_PARM_DESC(update_ms, "milliseconds before pstore updates its content " 39521f7288SAnton Vorontsov "(default is -1, which means runtime updates are disabled; " 4078c83c82SKees Cook "enabling this option may not be safe; it may lead to further " 41521f7288SAnton Vorontsov "corruption on Oopses)"); 426dda9266SLuck, Tony 43f0f23e54SJoel Fernandes (Google) /* Names should be in the same order as the enum pstore_type_id */ 44f0f23e54SJoel Fernandes (Google) static const char * const pstore_type_names[] = { 45f0f23e54SJoel Fernandes (Google) "dmesg", 46f0f23e54SJoel Fernandes (Google) "mce", 47f0f23e54SJoel Fernandes (Google) "console", 48f0f23e54SJoel Fernandes (Google) "ftrace", 49f0f23e54SJoel Fernandes (Google) "rtas", 50f0f23e54SJoel Fernandes (Google) "powerpc-ofw", 51f0f23e54SJoel Fernandes (Google) "powerpc-common", 52f0f23e54SJoel Fernandes (Google) "pmsg", 53f0f23e54SJoel Fernandes (Google) "powerpc-opal", 54f0f23e54SJoel Fernandes (Google) }; 55f0f23e54SJoel Fernandes (Google) 566dda9266SLuck, Tony static int pstore_new_entry; 576dda9266SLuck, Tony 5824ed960aSKees Cook static void pstore_timefunc(struct timer_list *); 591d27e3e2SKees Cook static DEFINE_TIMER(pstore_timer, pstore_timefunc); 606dda9266SLuck, Tony 616dda9266SLuck, Tony static void pstore_dowork(struct work_struct *); 626dda9266SLuck, Tony static DECLARE_WORK(pstore_work, pstore_dowork); 636dda9266SLuck, Tony 646dda9266SLuck, Tony /* 656248a066SKees Cook * psinfo_lock protects "psinfo" during calls to 666248a066SKees Cook * pstore_register(), pstore_unregister(), and 676248a066SKees Cook * the filesystem mount/unmount routines. 68ca01d6ddSTony Luck */ 69cab12fd0SKees Cook static DEFINE_MUTEX(psinfo_lock); 70060287b8SAnton Vorontsov struct pstore_info *psinfo; 71ca01d6ddSTony Luck 72dee28e72SMatthew Garrett static char *backend; 73d973f7d8SKees Cook module_param(backend, charp, 0444); 74d973f7d8SKees Cook MODULE_PARM_DESC(backend, "specific backend to use"); 75d973f7d8SKees Cook 76438b8050SArd Biesheuvel /* 77438b8050SArd Biesheuvel * pstore no longer implements compression via the crypto API, and only 78438b8050SArd Biesheuvel * supports zlib deflate compression implemented using the zlib library 79438b8050SArd Biesheuvel * interface. This removes additional complexity which is hard to justify for a 80438b8050SArd Biesheuvel * diagnostic facility that has to operate in conditions where the system may 81438b8050SArd Biesheuvel * have become unstable. Zlib deflate is comparatively small in terms of code 82438b8050SArd Biesheuvel * size, and compresses ASCII text comparatively well. In terms of compression 83438b8050SArd Biesheuvel * speed, deflate is not the best performer but for recording the log output on 84438b8050SArd Biesheuvel * a kernel panic, this is not considered critical. 85438b8050SArd Biesheuvel * 86438b8050SArd Biesheuvel * The only remaining arguments supported by the compress= module parameter are 87438b8050SArd Biesheuvel * 'deflate' and 'none'. To retain compatibility with existing installations, 88438b8050SArd Biesheuvel * all other values are logged and replaced with 'deflate'. 89438b8050SArd Biesheuvel */ 90438b8050SArd Biesheuvel static char *compress = "deflate"; 91d973f7d8SKees Cook module_param(compress, charp, 0444); 92d973f7d8SKees Cook MODULE_PARM_DESC(compress, "compression to use"); 93dee28e72SMatthew Garrett 948f5de3fdSGuilherme G. Piccoli /* How much of the kernel log to snapshot */ 958f5de3fdSGuilherme G. Piccoli unsigned long kmsg_bytes = CONFIG_PSTORE_DEFAULT_KMSG_BYTES; 968f5de3fdSGuilherme G. Piccoli module_param(kmsg_bytes, ulong, 0444); 978f5de3fdSGuilherme G. Piccoli MODULE_PARM_DESC(kmsg_bytes, "amount of kernel log to snapshot (in bytes)"); 988f5de3fdSGuilherme G. Piccoli 99438b8050SArd Biesheuvel static void *compress_workspace; 1008cfc8ddcSGeliang Tang 101b0aad7a9SAruna Balakrishnaiah static char *big_oops_buf; 102b0aad7a9SAruna Balakrishnaiah 103366f7e7aSLuck, Tony void pstore_set_kmsg_bytes(int bytes) 104ca01d6ddSTony Luck { 105366f7e7aSLuck, Tony kmsg_bytes = bytes; 106ca01d6ddSTony Luck } 107ca01d6ddSTony Luck 108ca01d6ddSTony Luck /* Tag each group of saved records with a sequence number */ 109ca01d6ddSTony Luck static int oopscount; 110ca01d6ddSTony Luck 111f0f23e54SJoel Fernandes (Google) const char *pstore_type_to_name(enum pstore_type_id type) 112f0f23e54SJoel Fernandes (Google) { 113f0f23e54SJoel Fernandes (Google) BUILD_BUG_ON(ARRAY_SIZE(pstore_type_names) != PSTORE_TYPE_MAX); 114f0f23e54SJoel Fernandes (Google) 115f0f23e54SJoel Fernandes (Google) if (WARN_ON_ONCE(type >= PSTORE_TYPE_MAX)) 116f0f23e54SJoel Fernandes (Google) return "unknown"; 117f0f23e54SJoel Fernandes (Google) 118f0f23e54SJoel Fernandes (Google) return pstore_type_names[type]; 119f0f23e54SJoel Fernandes (Google) } 120f0f23e54SJoel Fernandes (Google) EXPORT_SYMBOL_GPL(pstore_type_to_name); 121f0f23e54SJoel Fernandes (Google) 122f0f23e54SJoel Fernandes (Google) enum pstore_type_id pstore_name_to_type(const char *name) 123f0f23e54SJoel Fernandes (Google) { 124f0f23e54SJoel Fernandes (Google) int i; 125f0f23e54SJoel Fernandes (Google) 126f0f23e54SJoel Fernandes (Google) for (i = 0; i < PSTORE_TYPE_MAX; i++) { 127f0f23e54SJoel Fernandes (Google) if (!strcmp(pstore_type_names[i], name)) 128f0f23e54SJoel Fernandes (Google) return i; 129f0f23e54SJoel Fernandes (Google) } 130f0f23e54SJoel Fernandes (Google) 131f0f23e54SJoel Fernandes (Google) return PSTORE_TYPE_MAX; 132f0f23e54SJoel Fernandes (Google) } 133f0f23e54SJoel Fernandes (Google) EXPORT_SYMBOL_GPL(pstore_name_to_type); 134f0f23e54SJoel Fernandes (Google) 13578c83c82SKees Cook static void pstore_timer_kick(void) 13678c83c82SKees Cook { 13778c83c82SKees Cook if (pstore_update_ms < 0) 13878c83c82SKees Cook return; 13978c83c82SKees Cook 14078c83c82SKees Cook mod_timer(&pstore_timer, jiffies + msecs_to_jiffies(pstore_update_ms)); 14178c83c82SKees Cook } 14278c83c82SKees Cook 1438126b1c7SJann Horn static bool pstore_cannot_block_path(enum kmsg_dump_reason reason) 144ea84b580SKees Cook { 1458126b1c7SJann Horn /* 1468126b1c7SJann Horn * In case of NMI path, pstore shouldn't be blocked 1478126b1c7SJann Horn * regardless of reason. 1488126b1c7SJann Horn */ 1499f244e9cSSeiji Aguchi if (in_nmi()) 1509f244e9cSSeiji Aguchi return true; 1519f244e9cSSeiji Aguchi 1529f244e9cSSeiji Aguchi switch (reason) { 1539f244e9cSSeiji Aguchi /* In panic case, other cpus are stopped by smp_send_stop(). */ 1549f244e9cSSeiji Aguchi case KMSG_DUMP_PANIC: 1558126b1c7SJann Horn /* 1568126b1c7SJann Horn * Emergency restart shouldn't be blocked by spinning on 1578126b1c7SJann Horn * pstore_info::buf_lock. 1588126b1c7SJann Horn */ 1599f244e9cSSeiji Aguchi case KMSG_DUMP_EMERG: 1609f244e9cSSeiji Aguchi return true; 1619f244e9cSSeiji Aguchi default: 1629f244e9cSSeiji Aguchi return false; 1639f244e9cSSeiji Aguchi } 1649f244e9cSSeiji Aguchi } 1659f244e9cSSeiji Aguchi 1668cfc8ddcSGeliang Tang static int pstore_compress(const void *in, void *out, 167cb3bee03SGeliang Tang unsigned int inlen, unsigned int outlen) 1688cfc8ddcSGeliang Tang { 169438b8050SArd Biesheuvel struct z_stream_s zstream = { 170438b8050SArd Biesheuvel .next_in = in, 171438b8050SArd Biesheuvel .avail_in = inlen, 172438b8050SArd Biesheuvel .next_out = out, 173438b8050SArd Biesheuvel .avail_out = outlen, 174438b8050SArd Biesheuvel .workspace = compress_workspace, 175438b8050SArd Biesheuvel }; 176cb3bee03SGeliang Tang int ret; 177cb3bee03SGeliang Tang 17819d8e914SJiri Bohac if (!IS_ENABLED(CONFIG_PSTORE_COMPRESS)) 179fd49e032SMatteo Croce return -EINVAL; 180fd49e032SMatteo Croce 181438b8050SArd Biesheuvel ret = zlib_deflateInit2(&zstream, Z_DEFAULT_COMPRESSION, Z_DEFLATED, 182438b8050SArd Biesheuvel -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY); 183438b8050SArd Biesheuvel if (ret != Z_OK) 184438b8050SArd Biesheuvel return -EINVAL; 1858cfc8ddcSGeliang Tang 186438b8050SArd Biesheuvel ret = zlib_deflate(&zstream, Z_FINISH); 187438b8050SArd Biesheuvel if (ret != Z_STREAM_END) 188438b8050SArd Biesheuvel return -EINVAL; 189438b8050SArd Biesheuvel 190438b8050SArd Biesheuvel ret = zlib_deflateEnd(&zstream); 191438b8050SArd Biesheuvel if (ret != Z_OK) 192438b8050SArd Biesheuvel pr_warn_once("zlib_deflateEnd() failed: %d\n", ret); 193438b8050SArd Biesheuvel 194438b8050SArd Biesheuvel return zstream.total_out; 195cb3bee03SGeliang Tang } 196cb3bee03SGeliang Tang 1978cfc8ddcSGeliang Tang static void allocate_buf_for_compression(void) 1988cfc8ddcSGeliang Tang { 19995047b05SKees Cook char *buf; 20095047b05SKees Cook 201438b8050SArd Biesheuvel /* Skip if not built-in or compression disabled. */ 202438b8050SArd Biesheuvel if (!IS_ENABLED(CONFIG_PSTORE_COMPRESS) || !compress || 203438b8050SArd Biesheuvel !strcmp(compress, "none")) { 204438b8050SArd Biesheuvel compress = NULL; 205cb3bee03SGeliang Tang return; 206438b8050SArd Biesheuvel } 207cb3bee03SGeliang Tang 208438b8050SArd Biesheuvel if (strcmp(compress, "deflate")) { 209438b8050SArd Biesheuvel pr_err("Unsupported compression '%s', falling back to deflate\n", 210438b8050SArd Biesheuvel compress); 211438b8050SArd Biesheuvel compress = "deflate"; 212cb3bee03SGeliang Tang } 213cb3bee03SGeliang Tang 2141756ddeaSArd Biesheuvel /* 2151756ddeaSArd Biesheuvel * The compression buffer only needs to be as large as the maximum 2161756ddeaSArd Biesheuvel * uncompressed record size, since any record that would be expanded by 2171756ddeaSArd Biesheuvel * compression is just stored uncompressed. 2181756ddeaSArd Biesheuvel */ 219*104fd0b5SYuxiao Zhang buf = kvzalloc(psinfo->bufsize, GFP_KERNEL); 22095047b05SKees Cook if (!buf) { 2211756ddeaSArd Biesheuvel pr_err("Failed %zu byte compression buffer allocation for: %s\n", 2221756ddeaSArd Biesheuvel psinfo->bufsize, compress); 223cb3bee03SGeliang Tang return; 2248cfc8ddcSGeliang Tang } 22595047b05SKees Cook 226438b8050SArd Biesheuvel compress_workspace = 227438b8050SArd Biesheuvel vmalloc(zlib_deflate_workspacesize(MAX_WBITS, DEF_MEM_LEVEL)); 228438b8050SArd Biesheuvel if (!compress_workspace) { 229438b8050SArd Biesheuvel pr_err("Failed to allocate zlib deflate workspace\n"); 230*104fd0b5SYuxiao Zhang kvfree(buf); 23195047b05SKees Cook return; 23295047b05SKees Cook } 23395047b05SKees Cook 23495047b05SKees Cook /* A non-NULL big_oops_buf indicates compression is available. */ 23595047b05SKees Cook big_oops_buf = buf; 23695047b05SKees Cook 2371756ddeaSArd Biesheuvel pr_info("Using crash dump compression: %s\n", compress); 2388cfc8ddcSGeliang Tang } 2398cfc8ddcSGeliang Tang 2408cfc8ddcSGeliang Tang static void free_buf_for_compression(void) 2418cfc8ddcSGeliang Tang { 242438b8050SArd Biesheuvel if (IS_ENABLED(CONFIG_PSTORE_COMPRESS) && compress_workspace) { 243438b8050SArd Biesheuvel vfree(compress_workspace); 244438b8050SArd Biesheuvel compress_workspace = NULL; 245a9fb94a9SPi-Hsun Shih } 246438b8050SArd Biesheuvel 247*104fd0b5SYuxiao Zhang kvfree(big_oops_buf); 248cb3bee03SGeliang Tang big_oops_buf = NULL; 249b0aad7a9SAruna Balakrishnaiah } 250b0aad7a9SAruna Balakrishnaiah 251e581ca81SKees Cook void pstore_record_init(struct pstore_record *record, 252e581ca81SKees Cook struct pstore_info *psinfo) 253e581ca81SKees Cook { 254e581ca81SKees Cook memset(record, 0, sizeof(*record)); 255e581ca81SKees Cook 256e581ca81SKees Cook record->psi = psinfo; 257c7f3c595SKees Cook 258c7f3c595SKees Cook /* Report zeroed timestamp if called before timekeeping has resumed. */ 2597aaa822eSKees Cook record->time = ns_to_timespec64(ktime_get_real_fast_ns()); 260e581ca81SKees Cook } 261e581ca81SKees Cook 262ca01d6ddSTony Luck /* 2630eed84ffSKees Cook * callback from kmsg_dump. Save as much as we can (up to kmsg_bytes) from the 2640eed84ffSKees Cook * end of the buffer. 265ca01d6ddSTony Luck */ 266ca01d6ddSTony Luck static void pstore_dump(struct kmsg_dumper *dumper, 267e2ae715dSKay Sievers enum kmsg_dump_reason reason) 268ca01d6ddSTony Luck { 269f9f3f02dSJohn Ogness struct kmsg_dump_iter iter; 270e2ae715dSKay Sievers unsigned long total = 0; 271381b872cSSeiji Aguchi const char *why; 272b94fdd07SMatthew Garrett unsigned int part = 1; 2738126b1c7SJann Horn unsigned long flags = 0; 27438b91847SGuilherme G. Piccoli int saved_ret = 0; 275e2ae715dSKay Sievers int ret; 276ca01d6ddSTony Luck 277fb13cb8aSKees Cook why = kmsg_dump_reason_str(reason); 2789f6af27fSTony Luck 2798126b1c7SJann Horn if (pstore_cannot_block_path(reason)) { 2808126b1c7SJann Horn if (!spin_trylock_irqsave(&psinfo->buf_lock, flags)) { 2818126b1c7SJann Horn pr_err("dump skipped in %s path because of concurrent dump\n", 282ea84b580SKees Cook in_nmi() ? "NMI" : why); 283959217c8SLi Pengcheng return; 2849f244e9cSSeiji Aguchi } 2858126b1c7SJann Horn } else { 2868126b1c7SJann Horn spin_lock_irqsave(&psinfo->buf_lock, flags); 287ea84b580SKees Cook } 288ea84b580SKees Cook 289f9f3f02dSJohn Ogness kmsg_dump_rewind(&iter); 290f9f3f02dSJohn Ogness 291ca01d6ddSTony Luck oopscount++; 292ca01d6ddSTony Luck while (total < kmsg_bytes) { 293e2ae715dSKay Sievers char *dst; 29476cc9580SKees Cook size_t dst_size; 29576cc9580SKees Cook int header_size; 296b0aad7a9SAruna Balakrishnaiah int zipped_len = -1; 29776cc9580SKees Cook size_t dump_size; 298e581ca81SKees Cook struct pstore_record record; 299e581ca81SKees Cook 300e581ca81SKees Cook pstore_record_init(&record, psinfo); 301e581ca81SKees Cook record.type = PSTORE_TYPE_DMESG; 302e581ca81SKees Cook record.count = oopscount; 303e581ca81SKees Cook record.reason = reason; 304e581ca81SKees Cook record.part = part; 305e581ca81SKees Cook record.buf = psinfo->buf; 306e2ae715dSKay Sievers 3071756ddeaSArd Biesheuvel dst = big_oops_buf ?: psinfo->buf; 30876cc9580SKees Cook dst_size = psinfo->bufsize; 309235f6d15SNamhyung Kim 31076cc9580SKees Cook /* Write dump header. */ 31176cc9580SKees Cook header_size = snprintf(dst, dst_size, "%s#%d Part%u\n", why, 31276cc9580SKees Cook oopscount, part); 31376cc9580SKees Cook dst_size -= header_size; 314b0aad7a9SAruna Balakrishnaiah 31576cc9580SKees Cook /* Write dump contents. */ 316f9f3f02dSJohn Ogness if (!kmsg_dump_get_buffer(&iter, true, dst + header_size, 31776cc9580SKees Cook dst_size, &dump_size)) 318b0aad7a9SAruna Balakrishnaiah break; 319b0aad7a9SAruna Balakrishnaiah 320ea84b580SKees Cook if (big_oops_buf) { 321b0aad7a9SAruna Balakrishnaiah zipped_len = pstore_compress(dst, psinfo->buf, 32276cc9580SKees Cook header_size + dump_size, 32376cc9580SKees Cook psinfo->bufsize); 324b0aad7a9SAruna Balakrishnaiah 325b0aad7a9SAruna Balakrishnaiah if (zipped_len > 0) { 32676cc9580SKees Cook record.compressed = true; 32776cc9580SKees Cook record.size = zipped_len; 328b0aad7a9SAruna Balakrishnaiah } else { 3291756ddeaSArd Biesheuvel record.size = header_size + dump_size; 3301756ddeaSArd Biesheuvel memcpy(psinfo->buf, dst, record.size); 331b0aad7a9SAruna Balakrishnaiah } 332b0aad7a9SAruna Balakrishnaiah } else { 33376cc9580SKees Cook record.size = header_size + dump_size; 334b0aad7a9SAruna Balakrishnaiah } 335b0aad7a9SAruna Balakrishnaiah 33676cc9580SKees Cook ret = psinfo->write(&record); 33778c83c82SKees Cook if (ret == 0 && reason == KMSG_DUMP_OOPS) { 3386dda9266SLuck, Tony pstore_new_entry = 1; 33978c83c82SKees Cook pstore_timer_kick(); 34038b91847SGuilherme G. Piccoli } else { 34138b91847SGuilherme G. Piccoli /* Preserve only the first non-zero returned value. */ 34238b91847SGuilherme G. Piccoli if (!saved_ret) 34338b91847SGuilherme G. Piccoli saved_ret = ret; 34478c83c82SKees Cook } 345e2ae715dSKay Sievers 34676cc9580SKees Cook total += record.size; 34756280682SMatthew Garrett part++; 348ca01d6ddSTony Luck } 3498126b1c7SJann Horn spin_unlock_irqrestore(&psinfo->buf_lock, flags); 35038b91847SGuilherme G. Piccoli 35138b91847SGuilherme G. Piccoli if (saved_ret) { 35238b91847SGuilherme G. Piccoli pr_err_once("backend (%s) writing error (%d)\n", psinfo->name, 35338b91847SGuilherme G. Piccoli saved_ret); 35438b91847SGuilherme G. Piccoli } 355ca01d6ddSTony Luck } 356ca01d6ddSTony Luck 357ca01d6ddSTony Luck static struct kmsg_dumper pstore_dumper = { 358ca01d6ddSTony Luck .dump = pstore_dump, 359ca01d6ddSTony Luck }; 360ca01d6ddSTony Luck 361306e5c2aSGeliang Tang /* 362306e5c2aSGeliang Tang * Register with kmsg_dump to save last part of console log on panic. 363306e5c2aSGeliang Tang */ 36418730411SGeliang Tang static void pstore_register_kmsg(void) 36518730411SGeliang Tang { 36618730411SGeliang Tang kmsg_dump_register(&pstore_dumper); 36718730411SGeliang Tang } 36818730411SGeliang Tang 369ee1d2674SGeliang Tang static void pstore_unregister_kmsg(void) 370ee1d2674SGeliang Tang { 371ee1d2674SGeliang Tang kmsg_dump_unregister(&pstore_dumper); 372ee1d2674SGeliang Tang } 373ee1d2674SGeliang Tang 374f29e5956SAnton Vorontsov #ifdef CONFIG_PSTORE_CONSOLE 375f29e5956SAnton Vorontsov static void pstore_console_write(struct console *con, const char *s, unsigned c) 376f29e5956SAnton Vorontsov { 377e581ca81SKees Cook struct pstore_record record; 378f29e5956SAnton Vorontsov 3794c6c4d34SYue Hu if (!c) 3804c6c4d34SYue Hu return; 3814c6c4d34SYue Hu 382e581ca81SKees Cook pstore_record_init(&record, psinfo); 383e581ca81SKees Cook record.type = PSTORE_TYPE_CONSOLE; 384e581ca81SKees Cook 385b10b4711SKees Cook record.buf = (char *)s; 386b10b4711SKees Cook record.size = c; 3874c9ec219SKees Cook psinfo->write(&record); 388f29e5956SAnton Vorontsov } 389f29e5956SAnton Vorontsov 390f29e5956SAnton Vorontsov static struct console pstore_console = { 391f29e5956SAnton Vorontsov .write = pstore_console_write, 392f29e5956SAnton Vorontsov .index = -1, 393f29e5956SAnton Vorontsov }; 394f29e5956SAnton Vorontsov 395f29e5956SAnton Vorontsov static void pstore_register_console(void) 396f29e5956SAnton Vorontsov { 397d195c390SKees Cook /* Show which backend is going to get console writes. */ 398d195c390SKees Cook strscpy(pstore_console.name, psinfo->name, 399d195c390SKees Cook sizeof(pstore_console.name)); 400b7753fc7SKees Cook /* 401b7753fc7SKees Cook * Always initialize flags here since prior unregister_console() 402b7753fc7SKees Cook * calls may have changed settings (specifically CON_ENABLED). 403b7753fc7SKees Cook */ 404b7753fc7SKees Cook pstore_console.flags = CON_PRINTBUFFER | CON_ENABLED | CON_ANYTIME; 405f29e5956SAnton Vorontsov register_console(&pstore_console); 406f29e5956SAnton Vorontsov } 407ee1d2674SGeliang Tang 408ee1d2674SGeliang Tang static void pstore_unregister_console(void) 409ee1d2674SGeliang Tang { 410ee1d2674SGeliang Tang unregister_console(&pstore_console); 411ee1d2674SGeliang Tang } 412f29e5956SAnton Vorontsov #else 413f29e5956SAnton Vorontsov static void pstore_register_console(void) {} 414ee1d2674SGeliang Tang static void pstore_unregister_console(void) {} 415f29e5956SAnton Vorontsov #endif 416f29e5956SAnton Vorontsov 4174c9ec219SKees Cook static int pstore_write_user_compat(struct pstore_record *record, 418fdd03118SKees Cook const char __user *buf) 4195bf6d1b9SMark Salyzyn { 42030800d99SKees Cook int ret = 0; 4215bf6d1b9SMark Salyzyn 42230800d99SKees Cook if (record->buf) 42330800d99SKees Cook return -EINVAL; 4245bf6d1b9SMark Salyzyn 425*104fd0b5SYuxiao Zhang record->buf = vmemdup_user(buf, record->size); 426dfd6fa39SHirofumi Nakagawa if (IS_ERR(record->buf)) { 427077090afSGeliang Tang ret = PTR_ERR(record->buf); 42830800d99SKees Cook goto out; 4295bf6d1b9SMark Salyzyn } 43030800d99SKees Cook 4314c9ec219SKees Cook ret = record->psi->write(record); 43230800d99SKees Cook 433*104fd0b5SYuxiao Zhang kvfree(record->buf); 434077090afSGeliang Tang out: 43530800d99SKees Cook record->buf = NULL; 43630800d99SKees Cook 43730800d99SKees Cook return unlikely(ret < 0) ? ret : record->size; 4385bf6d1b9SMark Salyzyn } 4395bf6d1b9SMark Salyzyn 440ca01d6ddSTony Luck /* 441ca01d6ddSTony Luck * platform specific persistent storage driver registers with 442ca01d6ddSTony Luck * us here. If pstore is already mounted, call the platform 443ca01d6ddSTony Luck * read function right away to populate the file system. If not 444ca01d6ddSTony Luck * then the pstore mount code will call us later to fill out 445ca01d6ddSTony Luck * the file system. 446ca01d6ddSTony Luck */ 447ca01d6ddSTony Luck int pstore_register(struct pstore_info *psi) 448ca01d6ddSTony Luck { 4490d7cd09aSKees Cook if (backend && strcmp(backend, psi->name)) { 450d85644dcSGuilherme G. Piccoli pr_warn("backend '%s' already in use: ignoring '%s'\n", 451d85644dcSGuilherme G. Piccoli backend, psi->name); 452d85644dcSGuilherme G. Piccoli return -EBUSY; 4530d7cd09aSKees Cook } 4548e48b1a8SLenny Szubowicz 4554c9ec219SKees Cook /* Sanity check flags. */ 4564c9ec219SKees Cook if (!psi->flags) { 4574c9ec219SKees Cook pr_warn("backend '%s' must support at least one frontend\n", 4584c9ec219SKees Cook psi->name); 4594c9ec219SKees Cook return -EINVAL; 4604c9ec219SKees Cook } 4614c9ec219SKees Cook 4624c9ec219SKees Cook /* Check for required functions. */ 4634c9ec219SKees Cook if (!psi->read || !psi->write) { 4644c9ec219SKees Cook pr_warn("backend '%s' must implement read() and write()\n", 4654c9ec219SKees Cook psi->name); 4664c9ec219SKees Cook return -EINVAL; 4674c9ec219SKees Cook } 4684c9ec219SKees Cook 469cab12fd0SKees Cook mutex_lock(&psinfo_lock); 470ca01d6ddSTony Luck if (psinfo) { 4710d7cd09aSKees Cook pr_warn("backend '%s' already loaded: ignoring '%s'\n", 4720d7cd09aSKees Cook psinfo->name, psi->name); 473cab12fd0SKees Cook mutex_unlock(&psinfo_lock); 474ca01d6ddSTony Luck return -EBUSY; 475ca01d6ddSTony Luck } 476dee28e72SMatthew Garrett 4774c9ec219SKees Cook if (!psi->write_user) 4784c9ec219SKees Cook psi->write_user = pstore_write_user_compat; 479ca01d6ddSTony Luck psinfo = psi; 480f6f82851SKees Cook mutex_init(&psinfo->read_mutex); 4818126b1c7SJann Horn spin_lock_init(&psinfo->buf_lock); 482ca01d6ddSTony Luck 4838880fa32SKees Cook if (psi->flags & PSTORE_FLAGS_DMESG) 484b0aad7a9SAruna Balakrishnaiah allocate_buf_for_compression(); 485b0aad7a9SAruna Balakrishnaiah 4866dda9266SLuck, Tony pstore_get_records(0); 487ca01d6ddSTony Luck 4883524e688SPavel Tatashin if (psi->flags & PSTORE_FLAGS_DMESG) { 4893524e688SPavel Tatashin pstore_dumper.max_reason = psinfo->max_reason; 49018730411SGeliang Tang pstore_register_kmsg(); 4913524e688SPavel Tatashin } 492c950fd6fSNamhyung Kim if (psi->flags & PSTORE_FLAGS_CONSOLE) 493f29e5956SAnton Vorontsov pstore_register_console(); 494c950fd6fSNamhyung Kim if (psi->flags & PSTORE_FLAGS_FTRACE) 49565f8c95eSAnton Vorontsov pstore_register_ftrace(); 496c950fd6fSNamhyung Kim if (psi->flags & PSTORE_FLAGS_PMSG) 4979d5438f4SMark Salyzyn pstore_register_pmsg(); 498ca01d6ddSTony Luck 4996330d553SKees Cook /* Start watching for new records, if desired. */ 50078c83c82SKees Cook pstore_timer_kick(); 5016dda9266SLuck, Tony 50242222c2aSWang Long /* 50342222c2aSWang Long * Update the module parameter backend, so it is visible 50442222c2aSWang Long * through /sys/module/pstore/parameters/backend 50542222c2aSWang Long */ 506563ca40dSKees Cook backend = kstrdup(psi->name, GFP_KERNEL); 50742222c2aSWang Long 508ef748853SFabian Frederick pr_info("Registered %s as persistent store backend\n", psi->name); 5098e48b1a8SLenny Szubowicz 5106248a066SKees Cook mutex_unlock(&psinfo_lock); 511ca01d6ddSTony Luck return 0; 512ca01d6ddSTony Luck } 513ca01d6ddSTony Luck EXPORT_SYMBOL_GPL(pstore_register); 514ca01d6ddSTony Luck 515ee1d2674SGeliang Tang void pstore_unregister(struct pstore_info *psi) 516ee1d2674SGeliang Tang { 5176248a066SKees Cook /* It's okay to unregister nothing. */ 5186248a066SKees Cook if (!psi) 5196248a066SKees Cook return; 5206248a066SKees Cook 5216248a066SKees Cook mutex_lock(&psinfo_lock); 5226248a066SKees Cook 5236248a066SKees Cook /* Only one backend can be registered at a time. */ 5246248a066SKees Cook if (WARN_ON(psi != psinfo)) { 5256248a066SKees Cook mutex_unlock(&psinfo_lock); 5266248a066SKees Cook return; 5276248a066SKees Cook } 5286248a066SKees Cook 52978c83c82SKees Cook /* Unregister all callbacks. */ 530c950fd6fSNamhyung Kim if (psi->flags & PSTORE_FLAGS_PMSG) 531ee1d2674SGeliang Tang pstore_unregister_pmsg(); 532c950fd6fSNamhyung Kim if (psi->flags & PSTORE_FLAGS_FTRACE) 533ee1d2674SGeliang Tang pstore_unregister_ftrace(); 534c950fd6fSNamhyung Kim if (psi->flags & PSTORE_FLAGS_CONSOLE) 535ee1d2674SGeliang Tang pstore_unregister_console(); 536c950fd6fSNamhyung Kim if (psi->flags & PSTORE_FLAGS_DMESG) 537ee1d2674SGeliang Tang pstore_unregister_kmsg(); 538ee1d2674SGeliang Tang 53978c83c82SKees Cook /* Stop timer and make sure all work has finished. */ 54078c83c82SKees Cook del_timer_sync(&pstore_timer); 54178c83c82SKees Cook flush_work(&pstore_work); 54278c83c82SKees Cook 543609e28bbSKees Cook /* Remove all backend records from filesystem tree. */ 544609e28bbSKees Cook pstore_put_backend_records(psi); 545609e28bbSKees Cook 546ee1d2674SGeliang Tang free_buf_for_compression(); 547ee1d2674SGeliang Tang 548ee1d2674SGeliang Tang psinfo = NULL; 549563ca40dSKees Cook kfree(backend); 550ee1d2674SGeliang Tang backend = NULL; 5516a14f198SGuilherme G. Piccoli 5526a14f198SGuilherme G. Piccoli pr_info("Unregistered %s as persistent store backend\n", psi->name); 5536248a066SKees Cook mutex_unlock(&psinfo_lock); 554ee1d2674SGeliang Tang } 555ee1d2674SGeliang Tang EXPORT_SYMBOL_GPL(pstore_unregister); 556ee1d2674SGeliang Tang 557438b8050SArd Biesheuvel static void decompress_record(struct pstore_record *record, 558438b8050SArd Biesheuvel struct z_stream_s *zstream) 559634f8f51SKees Cook { 560bdabc8e7SKees Cook int ret; 561634f8f51SKees Cook int unzipped_len; 562bdabc8e7SKees Cook char *unzipped, *workspace; 563634f8f51SKees Cook 56419d8e914SJiri Bohac if (!IS_ENABLED(CONFIG_PSTORE_COMPRESS) || !record->compressed) 5654a16d1cbSAnkit Kumar return; 5664a16d1cbSAnkit Kumar 567634f8f51SKees Cook /* Only PSTORE_TYPE_DMESG support compression. */ 5684a16d1cbSAnkit Kumar if (record->type != PSTORE_TYPE_DMESG) { 569634f8f51SKees Cook pr_warn("ignored compressed record type %d\n", record->type); 570634f8f51SKees Cook return; 571634f8f51SKees Cook } 572634f8f51SKees Cook 573bdabc8e7SKees Cook /* Missing compression buffer means compression was not initialized. */ 574438b8050SArd Biesheuvel if (!zstream->workspace) { 575bdabc8e7SKees Cook pr_warn("no decompression method initialized!\n"); 576634f8f51SKees Cook return; 577634f8f51SKees Cook } 578634f8f51SKees Cook 579438b8050SArd Biesheuvel ret = zlib_inflateReset(zstream); 580438b8050SArd Biesheuvel if (ret != Z_OK) { 581438b8050SArd Biesheuvel pr_err("zlib_inflateReset() failed, ret = %d!\n", ret); 582438b8050SArd Biesheuvel return; 583438b8050SArd Biesheuvel } 584438b8050SArd Biesheuvel 585bdabc8e7SKees Cook /* Allocate enough space to hold max decompression and ECC. */ 586*104fd0b5SYuxiao Zhang workspace = kvzalloc(psinfo->bufsize + record->ecc_notice_size, 5877e8cc8dcSKees Cook GFP_KERNEL); 588bdabc8e7SKees Cook if (!workspace) 589bdabc8e7SKees Cook return; 590bdabc8e7SKees Cook 591438b8050SArd Biesheuvel zstream->next_in = record->buf; 592438b8050SArd Biesheuvel zstream->avail_in = record->size; 593438b8050SArd Biesheuvel zstream->next_out = workspace; 594438b8050SArd Biesheuvel zstream->avail_out = psinfo->bufsize; 595438b8050SArd Biesheuvel 596438b8050SArd Biesheuvel ret = zlib_inflate(zstream, Z_FINISH); 597438b8050SArd Biesheuvel if (ret != Z_STREAM_END) { 598438b8050SArd Biesheuvel pr_err("zlib_inflate() failed, ret = %d!\n", ret); 599*104fd0b5SYuxiao Zhang kvfree(workspace); 6007e8cc8dcSKees Cook return; 6017e8cc8dcSKees Cook } 6027e8cc8dcSKees Cook 603438b8050SArd Biesheuvel unzipped_len = zstream->total_out; 604438b8050SArd Biesheuvel 6057e8cc8dcSKees Cook /* Append ECC notice to decompressed buffer. */ 606bdabc8e7SKees Cook memcpy(workspace + unzipped_len, record->buf + record->size, 607634f8f51SKees Cook record->ecc_notice_size); 6087e8cc8dcSKees Cook 609bdabc8e7SKees Cook /* Copy decompressed contents into an minimum-sized allocation. */ 610*104fd0b5SYuxiao Zhang unzipped = kvmemdup(workspace, unzipped_len + record->ecc_notice_size, 611bdabc8e7SKees Cook GFP_KERNEL); 612*104fd0b5SYuxiao Zhang kvfree(workspace); 613bdabc8e7SKees Cook if (!unzipped) 614bdabc8e7SKees Cook return; 615bdabc8e7SKees Cook 616bdabc8e7SKees Cook /* Swap out compressed contents with decompressed contents. */ 617*104fd0b5SYuxiao Zhang kvfree(record->buf); 618bdabc8e7SKees Cook record->buf = unzipped; 619634f8f51SKees Cook record->size = unzipped_len; 620634f8f51SKees Cook record->compressed = false; 621634f8f51SKees Cook } 622634f8f51SKees Cook 623ca01d6ddSTony Luck /* 6243a7d2fd1SKees Cook * Read all the records from one persistent store backend. Create 6256dda9266SLuck, Tony * files in our filesystem. Don't warn about -EEXIST errors 6266dda9266SLuck, Tony * when we are re-scanning the backing store looking to add new 6276dda9266SLuck, Tony * error records. 628ca01d6ddSTony Luck */ 6293a7d2fd1SKees Cook void pstore_get_backend_records(struct pstore_info *psi, 6303a7d2fd1SKees Cook struct dentry *root, int quiet) 631ca01d6ddSTony Luck { 6322a2b0acfSKees Cook int failed = 0; 633656de42eSKees Cook unsigned int stop_loop = 65536; 634438b8050SArd Biesheuvel struct z_stream_s zstream = {}; 635ca01d6ddSTony Luck 6363a7d2fd1SKees Cook if (!psi || !root) 637ca01d6ddSTony Luck return; 638ca01d6ddSTony Luck 639438b8050SArd Biesheuvel if (IS_ENABLED(CONFIG_PSTORE_COMPRESS) && compress) { 640438b8050SArd Biesheuvel zstream.workspace = kvmalloc(zlib_inflate_workspacesize(), 641438b8050SArd Biesheuvel GFP_KERNEL); 642438b8050SArd Biesheuvel zlib_inflateInit2(&zstream, -DEF_WBITS); 643438b8050SArd Biesheuvel } 644438b8050SArd Biesheuvel 645f6f82851SKees Cook mutex_lock(&psi->read_mutex); 6462174f6dfSKees Cook if (psi->open && psi->open(psi)) 64706cf91b4SChen Gong goto out; 64806cf91b4SChen Gong 6491dfff7ddSKees Cook /* 6501dfff7ddSKees Cook * Backend callback read() allocates record.buf. decompress_record() 6511dfff7ddSKees Cook * may reallocate record.buf. On success, pstore_mkfile() will keep 6521dfff7ddSKees Cook * the record.buf, so free it only on failure. 6531dfff7ddSKees Cook */ 654656de42eSKees Cook for (; stop_loop; stop_loop--) { 6552a2b0acfSKees Cook struct pstore_record *record; 6562a2b0acfSKees Cook int rc; 6572a2b0acfSKees Cook 6582a2b0acfSKees Cook record = kzalloc(sizeof(*record), GFP_KERNEL); 6592a2b0acfSKees Cook if (!record) { 6602a2b0acfSKees Cook pr_err("out of memory creating record\n"); 6612a2b0acfSKees Cook break; 6622a2b0acfSKees Cook } 663e581ca81SKees Cook pstore_record_init(record, psi); 6642a2b0acfSKees Cook 6652a2b0acfSKees Cook record->size = psi->read(record); 6662a2b0acfSKees Cook 6672a2b0acfSKees Cook /* No more records left in backend? */ 668f6525b96SDouglas Anderson if (record->size <= 0) { 669f6525b96SDouglas Anderson kfree(record); 6702a2b0acfSKees Cook break; 671f6525b96SDouglas Anderson } 6722a2b0acfSKees Cook 673438b8050SArd Biesheuvel decompress_record(record, &zstream); 6743a7d2fd1SKees Cook rc = pstore_mkfile(root, record); 6751dfff7ddSKees Cook if (rc) { 67683f70f07SKees Cook /* pstore_mkfile() did not take record, so free it. */ 677*104fd0b5SYuxiao Zhang kvfree(record->buf); 6788ca869b2SArd Biesheuvel kfree(record->priv); 67983f70f07SKees Cook kfree(record); 6801dfff7ddSKees Cook if (rc != -EEXIST || !quiet) 6811dfff7ddSKees Cook failed++; 6821dfff7ddSKees Cook } 683ca01d6ddSTony Luck } 6842174f6dfSKees Cook if (psi->close) 68506cf91b4SChen Gong psi->close(psi); 68606cf91b4SChen Gong out: 687f6f82851SKees Cook mutex_unlock(&psi->read_mutex); 688ca01d6ddSTony Luck 689438b8050SArd Biesheuvel if (IS_ENABLED(CONFIG_PSTORE_COMPRESS) && compress) { 690438b8050SArd Biesheuvel if (zlib_inflateEnd(&zstream) != Z_OK) 691438b8050SArd Biesheuvel pr_warn("zlib_inflateEnd() failed\n"); 692438b8050SArd Biesheuvel kvfree(zstream.workspace); 693438b8050SArd Biesheuvel } 694438b8050SArd Biesheuvel 695ca01d6ddSTony Luck if (failed) 696656de42eSKees Cook pr_warn("failed to create %d record(s) from '%s'\n", 697ca01d6ddSTony Luck failed, psi->name); 698656de42eSKees Cook if (!stop_loop) 699656de42eSKees Cook pr_err("looping? Too many records seen from '%s'\n", 700656de42eSKees Cook psi->name); 701ca01d6ddSTony Luck } 702ca01d6ddSTony Luck 7036dda9266SLuck, Tony static void pstore_dowork(struct work_struct *work) 7046dda9266SLuck, Tony { 7056dda9266SLuck, Tony pstore_get_records(1); 7066dda9266SLuck, Tony } 7076dda9266SLuck, Tony 70824ed960aSKees Cook static void pstore_timefunc(struct timer_list *unused) 7096dda9266SLuck, Tony { 7106dda9266SLuck, Tony if (pstore_new_entry) { 7116dda9266SLuck, Tony pstore_new_entry = 0; 7126dda9266SLuck, Tony schedule_work(&pstore_work); 7136dda9266SLuck, Tony } 7146dda9266SLuck, Tony 71578c83c82SKees Cook pstore_timer_kick(); 7166dda9266SLuck, Tony } 7176dda9266SLuck, Tony 718cb095afdSKees Cook static int __init pstore_init(void) 719cb095afdSKees Cook { 720cb095afdSKees Cook int ret; 721cb095afdSKees Cook 722cb095afdSKees Cook ret = pstore_init_fs(); 723cb095afdSKees Cook if (ret) 7248a57d6d4Schenqiwu free_buf_for_compression(); 725cb095afdSKees Cook 7268a57d6d4Schenqiwu return ret; 727cb095afdSKees Cook } 72841603165SJoel Fernandes (Google) late_initcall(pstore_init); 729cb095afdSKees Cook 730cb095afdSKees Cook static void __exit pstore_exit(void) 731cb095afdSKees Cook { 732cb095afdSKees Cook pstore_exit_fs(); 733cb095afdSKees Cook } 734cb095afdSKees Cook module_exit(pstore_exit) 735cb095afdSKees Cook 736cb095afdSKees Cook MODULE_AUTHOR("Tony Luck <tony.luck@intel.com>"); 737cb095afdSKees Cook MODULE_LICENSE("GPL"); 738