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> 17104fd0b5SYuxiao 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 10194160062SArd Biesheuvel /* 10294160062SArd Biesheuvel * Compression is only used for dmesg output, which consists of low-entropy 10394160062SArd Biesheuvel * ASCII text, and so we can assume worst-case 60%. 10494160062SArd Biesheuvel */ 10594160062SArd Biesheuvel #define DMESG_COMP_PERCENT 60 10694160062SArd Biesheuvel 107b0aad7a9SAruna Balakrishnaiah static char *big_oops_buf; 10894160062SArd Biesheuvel static size_t max_compressed_size; 109b0aad7a9SAruna Balakrishnaiah 110366f7e7aSLuck, Tony void pstore_set_kmsg_bytes(int bytes) 111ca01d6ddSTony Luck { 112366f7e7aSLuck, Tony kmsg_bytes = bytes; 113ca01d6ddSTony Luck } 114ca01d6ddSTony Luck 115ca01d6ddSTony Luck /* Tag each group of saved records with a sequence number */ 116ca01d6ddSTony Luck static int oopscount; 117ca01d6ddSTony Luck 118f0f23e54SJoel Fernandes (Google) const char *pstore_type_to_name(enum pstore_type_id type) 119f0f23e54SJoel Fernandes (Google) { 120f0f23e54SJoel Fernandes (Google) BUILD_BUG_ON(ARRAY_SIZE(pstore_type_names) != PSTORE_TYPE_MAX); 121f0f23e54SJoel Fernandes (Google) 122f0f23e54SJoel Fernandes (Google) if (WARN_ON_ONCE(type >= PSTORE_TYPE_MAX)) 123f0f23e54SJoel Fernandes (Google) return "unknown"; 124f0f23e54SJoel Fernandes (Google) 125f0f23e54SJoel Fernandes (Google) return pstore_type_names[type]; 126f0f23e54SJoel Fernandes (Google) } 127f0f23e54SJoel Fernandes (Google) EXPORT_SYMBOL_GPL(pstore_type_to_name); 128f0f23e54SJoel Fernandes (Google) 129f0f23e54SJoel Fernandes (Google) enum pstore_type_id pstore_name_to_type(const char *name) 130f0f23e54SJoel Fernandes (Google) { 131f0f23e54SJoel Fernandes (Google) int i; 132f0f23e54SJoel Fernandes (Google) 133f0f23e54SJoel Fernandes (Google) for (i = 0; i < PSTORE_TYPE_MAX; i++) { 134f0f23e54SJoel Fernandes (Google) if (!strcmp(pstore_type_names[i], name)) 135f0f23e54SJoel Fernandes (Google) return i; 136f0f23e54SJoel Fernandes (Google) } 137f0f23e54SJoel Fernandes (Google) 138f0f23e54SJoel Fernandes (Google) return PSTORE_TYPE_MAX; 139f0f23e54SJoel Fernandes (Google) } 140f0f23e54SJoel Fernandes (Google) EXPORT_SYMBOL_GPL(pstore_name_to_type); 141f0f23e54SJoel Fernandes (Google) 14278c83c82SKees Cook static void pstore_timer_kick(void) 14378c83c82SKees Cook { 14478c83c82SKees Cook if (pstore_update_ms < 0) 14578c83c82SKees Cook return; 14678c83c82SKees Cook 14778c83c82SKees Cook mod_timer(&pstore_timer, jiffies + msecs_to_jiffies(pstore_update_ms)); 14878c83c82SKees Cook } 14978c83c82SKees Cook 1508126b1c7SJann Horn static bool pstore_cannot_block_path(enum kmsg_dump_reason reason) 151ea84b580SKees Cook { 1528126b1c7SJann Horn /* 1538126b1c7SJann Horn * In case of NMI path, pstore shouldn't be blocked 1548126b1c7SJann Horn * regardless of reason. 1558126b1c7SJann Horn */ 1569f244e9cSSeiji Aguchi if (in_nmi()) 1579f244e9cSSeiji Aguchi return true; 1589f244e9cSSeiji Aguchi 1599f244e9cSSeiji Aguchi switch (reason) { 1609f244e9cSSeiji Aguchi /* In panic case, other cpus are stopped by smp_send_stop(). */ 1619f244e9cSSeiji Aguchi case KMSG_DUMP_PANIC: 1628126b1c7SJann Horn /* 1638126b1c7SJann Horn * Emergency restart shouldn't be blocked by spinning on 1648126b1c7SJann Horn * pstore_info::buf_lock. 1658126b1c7SJann Horn */ 1669f244e9cSSeiji Aguchi case KMSG_DUMP_EMERG: 1679f244e9cSSeiji Aguchi return true; 1689f244e9cSSeiji Aguchi default: 1699f244e9cSSeiji Aguchi return false; 1709f244e9cSSeiji Aguchi } 1719f244e9cSSeiji Aguchi } 1729f244e9cSSeiji Aguchi 1738cfc8ddcSGeliang Tang static int pstore_compress(const void *in, void *out, 174cb3bee03SGeliang Tang unsigned int inlen, unsigned int outlen) 1758cfc8ddcSGeliang Tang { 176438b8050SArd Biesheuvel struct z_stream_s zstream = { 177438b8050SArd Biesheuvel .next_in = in, 178438b8050SArd Biesheuvel .avail_in = inlen, 179438b8050SArd Biesheuvel .next_out = out, 180438b8050SArd Biesheuvel .avail_out = outlen, 181438b8050SArd Biesheuvel .workspace = compress_workspace, 182438b8050SArd Biesheuvel }; 183cb3bee03SGeliang Tang int ret; 184cb3bee03SGeliang Tang 18519d8e914SJiri Bohac if (!IS_ENABLED(CONFIG_PSTORE_COMPRESS)) 186fd49e032SMatteo Croce return -EINVAL; 187fd49e032SMatteo Croce 188438b8050SArd Biesheuvel ret = zlib_deflateInit2(&zstream, Z_DEFAULT_COMPRESSION, Z_DEFLATED, 189438b8050SArd Biesheuvel -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY); 190438b8050SArd Biesheuvel if (ret != Z_OK) 191438b8050SArd Biesheuvel return -EINVAL; 1928cfc8ddcSGeliang Tang 193438b8050SArd Biesheuvel ret = zlib_deflate(&zstream, Z_FINISH); 194438b8050SArd Biesheuvel if (ret != Z_STREAM_END) 195438b8050SArd Biesheuvel return -EINVAL; 196438b8050SArd Biesheuvel 197438b8050SArd Biesheuvel ret = zlib_deflateEnd(&zstream); 198438b8050SArd Biesheuvel if (ret != Z_OK) 199438b8050SArd Biesheuvel pr_warn_once("zlib_deflateEnd() failed: %d\n", ret); 200438b8050SArd Biesheuvel 201438b8050SArd Biesheuvel return zstream.total_out; 202cb3bee03SGeliang Tang } 203cb3bee03SGeliang Tang 2048cfc8ddcSGeliang Tang static void allocate_buf_for_compression(void) 2058cfc8ddcSGeliang Tang { 20694160062SArd Biesheuvel size_t compressed_size; 20795047b05SKees Cook char *buf; 20895047b05SKees Cook 209438b8050SArd Biesheuvel /* Skip if not built-in or compression disabled. */ 210438b8050SArd Biesheuvel if (!IS_ENABLED(CONFIG_PSTORE_COMPRESS) || !compress || 211438b8050SArd Biesheuvel !strcmp(compress, "none")) { 212438b8050SArd Biesheuvel compress = NULL; 213cb3bee03SGeliang Tang return; 214438b8050SArd Biesheuvel } 215cb3bee03SGeliang Tang 216438b8050SArd Biesheuvel if (strcmp(compress, "deflate")) { 217438b8050SArd Biesheuvel pr_err("Unsupported compression '%s', falling back to deflate\n", 218438b8050SArd Biesheuvel compress); 219438b8050SArd Biesheuvel compress = "deflate"; 220cb3bee03SGeliang Tang } 221cb3bee03SGeliang Tang 2221756ddeaSArd Biesheuvel /* 2231756ddeaSArd Biesheuvel * The compression buffer only needs to be as large as the maximum 2241756ddeaSArd Biesheuvel * uncompressed record size, since any record that would be expanded by 2251756ddeaSArd Biesheuvel * compression is just stored uncompressed. 2261756ddeaSArd Biesheuvel */ 22794160062SArd Biesheuvel compressed_size = (psinfo->bufsize * 100) / DMESG_COMP_PERCENT; 22894160062SArd Biesheuvel buf = kvzalloc(compressed_size, GFP_KERNEL); 22995047b05SKees Cook if (!buf) { 2301756ddeaSArd Biesheuvel pr_err("Failed %zu byte compression buffer allocation for: %s\n", 2311756ddeaSArd Biesheuvel psinfo->bufsize, compress); 232cb3bee03SGeliang Tang return; 2338cfc8ddcSGeliang Tang } 23495047b05SKees Cook 235438b8050SArd Biesheuvel compress_workspace = 236438b8050SArd Biesheuvel vmalloc(zlib_deflate_workspacesize(MAX_WBITS, DEF_MEM_LEVEL)); 237438b8050SArd Biesheuvel if (!compress_workspace) { 238438b8050SArd Biesheuvel pr_err("Failed to allocate zlib deflate workspace\n"); 239104fd0b5SYuxiao Zhang kvfree(buf); 24095047b05SKees Cook return; 24195047b05SKees Cook } 24295047b05SKees Cook 24395047b05SKees Cook /* A non-NULL big_oops_buf indicates compression is available. */ 24495047b05SKees Cook big_oops_buf = buf; 24594160062SArd Biesheuvel max_compressed_size = compressed_size; 24695047b05SKees Cook 2471756ddeaSArd Biesheuvel pr_info("Using crash dump compression: %s\n", compress); 2488cfc8ddcSGeliang Tang } 2498cfc8ddcSGeliang Tang 2508cfc8ddcSGeliang Tang static void free_buf_for_compression(void) 2518cfc8ddcSGeliang Tang { 252438b8050SArd Biesheuvel if (IS_ENABLED(CONFIG_PSTORE_COMPRESS) && compress_workspace) { 253438b8050SArd Biesheuvel vfree(compress_workspace); 254438b8050SArd Biesheuvel compress_workspace = NULL; 255a9fb94a9SPi-Hsun Shih } 256438b8050SArd Biesheuvel 257104fd0b5SYuxiao Zhang kvfree(big_oops_buf); 258cb3bee03SGeliang Tang big_oops_buf = NULL; 25994160062SArd Biesheuvel max_compressed_size = 0; 260b0aad7a9SAruna Balakrishnaiah } 261b0aad7a9SAruna Balakrishnaiah 262e581ca81SKees Cook void pstore_record_init(struct pstore_record *record, 263e581ca81SKees Cook struct pstore_info *psinfo) 264e581ca81SKees Cook { 265e581ca81SKees Cook memset(record, 0, sizeof(*record)); 266e581ca81SKees Cook 267e581ca81SKees Cook record->psi = psinfo; 268c7f3c595SKees Cook 269c7f3c595SKees Cook /* Report zeroed timestamp if called before timekeeping has resumed. */ 2707aaa822eSKees Cook record->time = ns_to_timespec64(ktime_get_real_fast_ns()); 271e581ca81SKees Cook } 272e581ca81SKees Cook 273ca01d6ddSTony Luck /* 2740eed84ffSKees Cook * callback from kmsg_dump. Save as much as we can (up to kmsg_bytes) from the 2750eed84ffSKees Cook * end of the buffer. 276ca01d6ddSTony Luck */ 277ca01d6ddSTony Luck static void pstore_dump(struct kmsg_dumper *dumper, 278e2ae715dSKay Sievers enum kmsg_dump_reason reason) 279ca01d6ddSTony Luck { 280f9f3f02dSJohn Ogness struct kmsg_dump_iter iter; 281e2ae715dSKay Sievers unsigned long total = 0; 282381b872cSSeiji Aguchi const char *why; 283b94fdd07SMatthew Garrett unsigned int part = 1; 2848126b1c7SJann Horn unsigned long flags = 0; 28538b91847SGuilherme G. Piccoli int saved_ret = 0; 286e2ae715dSKay Sievers int ret; 287ca01d6ddSTony Luck 288fb13cb8aSKees Cook why = kmsg_dump_reason_str(reason); 2899f6af27fSTony Luck 2908126b1c7SJann Horn if (pstore_cannot_block_path(reason)) { 2918126b1c7SJann Horn if (!spin_trylock_irqsave(&psinfo->buf_lock, flags)) { 2928126b1c7SJann Horn pr_err("dump skipped in %s path because of concurrent dump\n", 293ea84b580SKees Cook in_nmi() ? "NMI" : why); 294959217c8SLi Pengcheng return; 2959f244e9cSSeiji Aguchi } 2968126b1c7SJann Horn } else { 2978126b1c7SJann Horn spin_lock_irqsave(&psinfo->buf_lock, flags); 298ea84b580SKees Cook } 299ea84b580SKees Cook 300f9f3f02dSJohn Ogness kmsg_dump_rewind(&iter); 301f9f3f02dSJohn Ogness 302ca01d6ddSTony Luck oopscount++; 303ca01d6ddSTony Luck while (total < kmsg_bytes) { 304e2ae715dSKay Sievers char *dst; 30576cc9580SKees Cook size_t dst_size; 30676cc9580SKees Cook int header_size; 307b0aad7a9SAruna Balakrishnaiah int zipped_len = -1; 30876cc9580SKees Cook size_t dump_size; 309e581ca81SKees Cook struct pstore_record record; 310e581ca81SKees Cook 311e581ca81SKees Cook pstore_record_init(&record, psinfo); 312e581ca81SKees Cook record.type = PSTORE_TYPE_DMESG; 313e581ca81SKees Cook record.count = oopscount; 314e581ca81SKees Cook record.reason = reason; 315e581ca81SKees Cook record.part = part; 316e581ca81SKees Cook record.buf = psinfo->buf; 317e2ae715dSKay Sievers 3181756ddeaSArd Biesheuvel dst = big_oops_buf ?: psinfo->buf; 31994160062SArd Biesheuvel dst_size = max_compressed_size ?: psinfo->bufsize; 320235f6d15SNamhyung Kim 32176cc9580SKees Cook /* Write dump header. */ 32276cc9580SKees Cook header_size = snprintf(dst, dst_size, "%s#%d Part%u\n", why, 32376cc9580SKees Cook oopscount, part); 32476cc9580SKees Cook dst_size -= header_size; 325b0aad7a9SAruna Balakrishnaiah 32676cc9580SKees Cook /* Write dump contents. */ 327f9f3f02dSJohn Ogness if (!kmsg_dump_get_buffer(&iter, true, dst + header_size, 32876cc9580SKees Cook dst_size, &dump_size)) 329b0aad7a9SAruna Balakrishnaiah break; 330b0aad7a9SAruna Balakrishnaiah 331ea84b580SKees Cook if (big_oops_buf) { 332b0aad7a9SAruna Balakrishnaiah zipped_len = pstore_compress(dst, psinfo->buf, 33376cc9580SKees Cook header_size + dump_size, 33476cc9580SKees Cook psinfo->bufsize); 335b0aad7a9SAruna Balakrishnaiah 336b0aad7a9SAruna Balakrishnaiah if (zipped_len > 0) { 33776cc9580SKees Cook record.compressed = true; 33876cc9580SKees Cook record.size = zipped_len; 339b0aad7a9SAruna Balakrishnaiah } else { 34094160062SArd Biesheuvel /* 34194160062SArd Biesheuvel * Compression failed, so the buffer is most 34294160062SArd Biesheuvel * likely filled with binary data that does not 34394160062SArd Biesheuvel * compress as well as ASCII text. Copy as much 34494160062SArd Biesheuvel * of the uncompressed data as possible into 34594160062SArd Biesheuvel * the pstore record, and discard the rest. 34694160062SArd Biesheuvel */ 34794160062SArd Biesheuvel record.size = psinfo->bufsize; 34894160062SArd Biesheuvel memcpy(psinfo->buf, dst, psinfo->bufsize); 349b0aad7a9SAruna Balakrishnaiah } 350b0aad7a9SAruna Balakrishnaiah } else { 35176cc9580SKees Cook record.size = header_size + dump_size; 352b0aad7a9SAruna Balakrishnaiah } 353b0aad7a9SAruna Balakrishnaiah 35476cc9580SKees Cook ret = psinfo->write(&record); 35578c83c82SKees Cook if (ret == 0 && reason == KMSG_DUMP_OOPS) { 3566dda9266SLuck, Tony pstore_new_entry = 1; 35778c83c82SKees Cook pstore_timer_kick(); 35838b91847SGuilherme G. Piccoli } else { 35938b91847SGuilherme G. Piccoli /* Preserve only the first non-zero returned value. */ 36038b91847SGuilherme G. Piccoli if (!saved_ret) 36138b91847SGuilherme G. Piccoli saved_ret = ret; 36278c83c82SKees Cook } 363e2ae715dSKay Sievers 36476cc9580SKees Cook total += record.size; 36556280682SMatthew Garrett part++; 366ca01d6ddSTony Luck } 3678126b1c7SJann Horn spin_unlock_irqrestore(&psinfo->buf_lock, flags); 36838b91847SGuilherme G. Piccoli 36938b91847SGuilherme G. Piccoli if (saved_ret) { 37038b91847SGuilherme G. Piccoli pr_err_once("backend (%s) writing error (%d)\n", psinfo->name, 37138b91847SGuilherme G. Piccoli saved_ret); 37238b91847SGuilherme G. Piccoli } 373ca01d6ddSTony Luck } 374ca01d6ddSTony Luck 375ca01d6ddSTony Luck static struct kmsg_dumper pstore_dumper = { 376ca01d6ddSTony Luck .dump = pstore_dump, 377ca01d6ddSTony Luck }; 378ca01d6ddSTony Luck 379306e5c2aSGeliang Tang /* 380306e5c2aSGeliang Tang * Register with kmsg_dump to save last part of console log on panic. 381306e5c2aSGeliang Tang */ 38218730411SGeliang Tang static void pstore_register_kmsg(void) 38318730411SGeliang Tang { 38418730411SGeliang Tang kmsg_dump_register(&pstore_dumper); 38518730411SGeliang Tang } 38618730411SGeliang Tang 387ee1d2674SGeliang Tang static void pstore_unregister_kmsg(void) 388ee1d2674SGeliang Tang { 389ee1d2674SGeliang Tang kmsg_dump_unregister(&pstore_dumper); 390ee1d2674SGeliang Tang } 391ee1d2674SGeliang Tang 392f29e5956SAnton Vorontsov #ifdef CONFIG_PSTORE_CONSOLE 393f29e5956SAnton Vorontsov static void pstore_console_write(struct console *con, const char *s, unsigned c) 394f29e5956SAnton Vorontsov { 395e581ca81SKees Cook struct pstore_record record; 396f29e5956SAnton Vorontsov 3974c6c4d34SYue Hu if (!c) 3984c6c4d34SYue Hu return; 3994c6c4d34SYue Hu 400e581ca81SKees Cook pstore_record_init(&record, psinfo); 401e581ca81SKees Cook record.type = PSTORE_TYPE_CONSOLE; 402e581ca81SKees Cook 403b10b4711SKees Cook record.buf = (char *)s; 404b10b4711SKees Cook record.size = c; 4054c9ec219SKees Cook psinfo->write(&record); 406f29e5956SAnton Vorontsov } 407f29e5956SAnton Vorontsov 408f29e5956SAnton Vorontsov static struct console pstore_console = { 409f29e5956SAnton Vorontsov .write = pstore_console_write, 410f29e5956SAnton Vorontsov .index = -1, 411f29e5956SAnton Vorontsov }; 412f29e5956SAnton Vorontsov 413f29e5956SAnton Vorontsov static void pstore_register_console(void) 414f29e5956SAnton Vorontsov { 415d195c390SKees Cook /* Show which backend is going to get console writes. */ 416d195c390SKees Cook strscpy(pstore_console.name, psinfo->name, 417d195c390SKees Cook sizeof(pstore_console.name)); 418b7753fc7SKees Cook /* 419b7753fc7SKees Cook * Always initialize flags here since prior unregister_console() 420b7753fc7SKees Cook * calls may have changed settings (specifically CON_ENABLED). 421b7753fc7SKees Cook */ 422b7753fc7SKees Cook pstore_console.flags = CON_PRINTBUFFER | CON_ENABLED | CON_ANYTIME; 423f29e5956SAnton Vorontsov register_console(&pstore_console); 424f29e5956SAnton Vorontsov } 425ee1d2674SGeliang Tang 426ee1d2674SGeliang Tang static void pstore_unregister_console(void) 427ee1d2674SGeliang Tang { 428ee1d2674SGeliang Tang unregister_console(&pstore_console); 429ee1d2674SGeliang Tang } 430f29e5956SAnton Vorontsov #else 431f29e5956SAnton Vorontsov static void pstore_register_console(void) {} 432ee1d2674SGeliang Tang static void pstore_unregister_console(void) {} 433f29e5956SAnton Vorontsov #endif 434f29e5956SAnton Vorontsov 4354c9ec219SKees Cook static int pstore_write_user_compat(struct pstore_record *record, 436fdd03118SKees Cook const char __user *buf) 4375bf6d1b9SMark Salyzyn { 43830800d99SKees Cook int ret = 0; 4395bf6d1b9SMark Salyzyn 44030800d99SKees Cook if (record->buf) 44130800d99SKees Cook return -EINVAL; 4425bf6d1b9SMark Salyzyn 443104fd0b5SYuxiao Zhang record->buf = vmemdup_user(buf, record->size); 444dfd6fa39SHirofumi Nakagawa if (IS_ERR(record->buf)) { 445077090afSGeliang Tang ret = PTR_ERR(record->buf); 44630800d99SKees Cook goto out; 4475bf6d1b9SMark Salyzyn } 44830800d99SKees Cook 4494c9ec219SKees Cook ret = record->psi->write(record); 45030800d99SKees Cook 451104fd0b5SYuxiao Zhang kvfree(record->buf); 452077090afSGeliang Tang out: 45330800d99SKees Cook record->buf = NULL; 45430800d99SKees Cook 45530800d99SKees Cook return unlikely(ret < 0) ? ret : record->size; 4565bf6d1b9SMark Salyzyn } 4575bf6d1b9SMark Salyzyn 458ca01d6ddSTony Luck /* 459ca01d6ddSTony Luck * platform specific persistent storage driver registers with 460ca01d6ddSTony Luck * us here. If pstore is already mounted, call the platform 461ca01d6ddSTony Luck * read function right away to populate the file system. If not 462ca01d6ddSTony Luck * then the pstore mount code will call us later to fill out 463ca01d6ddSTony Luck * the file system. 464ca01d6ddSTony Luck */ 465ca01d6ddSTony Luck int pstore_register(struct pstore_info *psi) 466ca01d6ddSTony Luck { 467*ad5cb6deSJiasheng Jiang char *new_backend; 468*ad5cb6deSJiasheng Jiang 4690d7cd09aSKees Cook if (backend && strcmp(backend, psi->name)) { 470d85644dcSGuilherme G. Piccoli pr_warn("backend '%s' already in use: ignoring '%s'\n", 471d85644dcSGuilherme G. Piccoli backend, psi->name); 472d85644dcSGuilherme G. Piccoli return -EBUSY; 4730d7cd09aSKees Cook } 4748e48b1a8SLenny Szubowicz 4754c9ec219SKees Cook /* Sanity check flags. */ 4764c9ec219SKees Cook if (!psi->flags) { 4774c9ec219SKees Cook pr_warn("backend '%s' must support at least one frontend\n", 4784c9ec219SKees Cook psi->name); 4794c9ec219SKees Cook return -EINVAL; 4804c9ec219SKees Cook } 4814c9ec219SKees Cook 4824c9ec219SKees Cook /* Check for required functions. */ 4834c9ec219SKees Cook if (!psi->read || !psi->write) { 4844c9ec219SKees Cook pr_warn("backend '%s' must implement read() and write()\n", 4854c9ec219SKees Cook psi->name); 4864c9ec219SKees Cook return -EINVAL; 4874c9ec219SKees Cook } 4884c9ec219SKees Cook 489*ad5cb6deSJiasheng Jiang new_backend = kstrdup(psi->name, GFP_KERNEL); 490*ad5cb6deSJiasheng Jiang if (!new_backend) 491*ad5cb6deSJiasheng Jiang return -ENOMEM; 492*ad5cb6deSJiasheng Jiang 493cab12fd0SKees Cook mutex_lock(&psinfo_lock); 494ca01d6ddSTony Luck if (psinfo) { 4950d7cd09aSKees Cook pr_warn("backend '%s' already loaded: ignoring '%s'\n", 4960d7cd09aSKees Cook psinfo->name, psi->name); 497cab12fd0SKees Cook mutex_unlock(&psinfo_lock); 498*ad5cb6deSJiasheng Jiang kfree(new_backend); 499ca01d6ddSTony Luck return -EBUSY; 500ca01d6ddSTony Luck } 501dee28e72SMatthew Garrett 5024c9ec219SKees Cook if (!psi->write_user) 5034c9ec219SKees Cook psi->write_user = pstore_write_user_compat; 504ca01d6ddSTony Luck psinfo = psi; 505f6f82851SKees Cook mutex_init(&psinfo->read_mutex); 5068126b1c7SJann Horn spin_lock_init(&psinfo->buf_lock); 507ca01d6ddSTony Luck 5088880fa32SKees Cook if (psi->flags & PSTORE_FLAGS_DMESG) 509b0aad7a9SAruna Balakrishnaiah allocate_buf_for_compression(); 510b0aad7a9SAruna Balakrishnaiah 5116dda9266SLuck, Tony pstore_get_records(0); 512ca01d6ddSTony Luck 5133524e688SPavel Tatashin if (psi->flags & PSTORE_FLAGS_DMESG) { 5143524e688SPavel Tatashin pstore_dumper.max_reason = psinfo->max_reason; 51518730411SGeliang Tang pstore_register_kmsg(); 5163524e688SPavel Tatashin } 517c950fd6fSNamhyung Kim if (psi->flags & PSTORE_FLAGS_CONSOLE) 518f29e5956SAnton Vorontsov pstore_register_console(); 519c950fd6fSNamhyung Kim if (psi->flags & PSTORE_FLAGS_FTRACE) 52065f8c95eSAnton Vorontsov pstore_register_ftrace(); 521c950fd6fSNamhyung Kim if (psi->flags & PSTORE_FLAGS_PMSG) 5229d5438f4SMark Salyzyn pstore_register_pmsg(); 523ca01d6ddSTony Luck 5246330d553SKees Cook /* Start watching for new records, if desired. */ 52578c83c82SKees Cook pstore_timer_kick(); 5266dda9266SLuck, Tony 52742222c2aSWang Long /* 52842222c2aSWang Long * Update the module parameter backend, so it is visible 52942222c2aSWang Long * through /sys/module/pstore/parameters/backend 53042222c2aSWang Long */ 531*ad5cb6deSJiasheng Jiang backend = new_backend; 53242222c2aSWang Long 533ef748853SFabian Frederick pr_info("Registered %s as persistent store backend\n", psi->name); 5348e48b1a8SLenny Szubowicz 5356248a066SKees Cook mutex_unlock(&psinfo_lock); 536ca01d6ddSTony Luck return 0; 537ca01d6ddSTony Luck } 538ca01d6ddSTony Luck EXPORT_SYMBOL_GPL(pstore_register); 539ca01d6ddSTony Luck 540ee1d2674SGeliang Tang void pstore_unregister(struct pstore_info *psi) 541ee1d2674SGeliang Tang { 5426248a066SKees Cook /* It's okay to unregister nothing. */ 5436248a066SKees Cook if (!psi) 5446248a066SKees Cook return; 5456248a066SKees Cook 5466248a066SKees Cook mutex_lock(&psinfo_lock); 5476248a066SKees Cook 5486248a066SKees Cook /* Only one backend can be registered at a time. */ 5496248a066SKees Cook if (WARN_ON(psi != psinfo)) { 5506248a066SKees Cook mutex_unlock(&psinfo_lock); 5516248a066SKees Cook return; 5526248a066SKees Cook } 5536248a066SKees Cook 55478c83c82SKees Cook /* Unregister all callbacks. */ 555c950fd6fSNamhyung Kim if (psi->flags & PSTORE_FLAGS_PMSG) 556ee1d2674SGeliang Tang pstore_unregister_pmsg(); 557c950fd6fSNamhyung Kim if (psi->flags & PSTORE_FLAGS_FTRACE) 558ee1d2674SGeliang Tang pstore_unregister_ftrace(); 559c950fd6fSNamhyung Kim if (psi->flags & PSTORE_FLAGS_CONSOLE) 560ee1d2674SGeliang Tang pstore_unregister_console(); 561c950fd6fSNamhyung Kim if (psi->flags & PSTORE_FLAGS_DMESG) 562ee1d2674SGeliang Tang pstore_unregister_kmsg(); 563ee1d2674SGeliang Tang 56478c83c82SKees Cook /* Stop timer and make sure all work has finished. */ 56578c83c82SKees Cook del_timer_sync(&pstore_timer); 56678c83c82SKees Cook flush_work(&pstore_work); 56778c83c82SKees Cook 568609e28bbSKees Cook /* Remove all backend records from filesystem tree. */ 569609e28bbSKees Cook pstore_put_backend_records(psi); 570609e28bbSKees Cook 571ee1d2674SGeliang Tang free_buf_for_compression(); 572ee1d2674SGeliang Tang 573ee1d2674SGeliang Tang psinfo = NULL; 574563ca40dSKees Cook kfree(backend); 575ee1d2674SGeliang Tang backend = NULL; 5766a14f198SGuilherme G. Piccoli 5776a14f198SGuilherme G. Piccoli pr_info("Unregistered %s as persistent store backend\n", psi->name); 5786248a066SKees Cook mutex_unlock(&psinfo_lock); 579ee1d2674SGeliang Tang } 580ee1d2674SGeliang Tang EXPORT_SYMBOL_GPL(pstore_unregister); 581ee1d2674SGeliang Tang 582438b8050SArd Biesheuvel static void decompress_record(struct pstore_record *record, 583438b8050SArd Biesheuvel struct z_stream_s *zstream) 584634f8f51SKees Cook { 585bdabc8e7SKees Cook int ret; 586634f8f51SKees Cook int unzipped_len; 587bdabc8e7SKees Cook char *unzipped, *workspace; 58894160062SArd Biesheuvel size_t max_uncompressed_size; 589634f8f51SKees Cook 59019d8e914SJiri Bohac if (!IS_ENABLED(CONFIG_PSTORE_COMPRESS) || !record->compressed) 5914a16d1cbSAnkit Kumar return; 5924a16d1cbSAnkit Kumar 593634f8f51SKees Cook /* Only PSTORE_TYPE_DMESG support compression. */ 5944a16d1cbSAnkit Kumar if (record->type != PSTORE_TYPE_DMESG) { 595634f8f51SKees Cook pr_warn("ignored compressed record type %d\n", record->type); 596634f8f51SKees Cook return; 597634f8f51SKees Cook } 598634f8f51SKees Cook 599bdabc8e7SKees Cook /* Missing compression buffer means compression was not initialized. */ 600438b8050SArd Biesheuvel if (!zstream->workspace) { 601bdabc8e7SKees Cook pr_warn("no decompression method initialized!\n"); 602634f8f51SKees Cook return; 603634f8f51SKees Cook } 604634f8f51SKees Cook 605438b8050SArd Biesheuvel ret = zlib_inflateReset(zstream); 606438b8050SArd Biesheuvel if (ret != Z_OK) { 607438b8050SArd Biesheuvel pr_err("zlib_inflateReset() failed, ret = %d!\n", ret); 608438b8050SArd Biesheuvel return; 609438b8050SArd Biesheuvel } 610438b8050SArd Biesheuvel 611bdabc8e7SKees Cook /* Allocate enough space to hold max decompression and ECC. */ 61294160062SArd Biesheuvel max_uncompressed_size = 3 * psinfo->bufsize; 61394160062SArd Biesheuvel workspace = kvzalloc(max_uncompressed_size + record->ecc_notice_size, 6147e8cc8dcSKees Cook GFP_KERNEL); 615bdabc8e7SKees Cook if (!workspace) 616bdabc8e7SKees Cook return; 617bdabc8e7SKees Cook 618438b8050SArd Biesheuvel zstream->next_in = record->buf; 619438b8050SArd Biesheuvel zstream->avail_in = record->size; 620438b8050SArd Biesheuvel zstream->next_out = workspace; 62194160062SArd Biesheuvel zstream->avail_out = max_uncompressed_size; 622438b8050SArd Biesheuvel 623438b8050SArd Biesheuvel ret = zlib_inflate(zstream, Z_FINISH); 624438b8050SArd Biesheuvel if (ret != Z_STREAM_END) { 62594160062SArd Biesheuvel pr_err_ratelimited("zlib_inflate() failed, ret = %d!\n", ret); 626104fd0b5SYuxiao Zhang kvfree(workspace); 6277e8cc8dcSKees Cook return; 6287e8cc8dcSKees Cook } 6297e8cc8dcSKees Cook 630438b8050SArd Biesheuvel unzipped_len = zstream->total_out; 631438b8050SArd Biesheuvel 6327e8cc8dcSKees Cook /* Append ECC notice to decompressed buffer. */ 633bdabc8e7SKees Cook memcpy(workspace + unzipped_len, record->buf + record->size, 634634f8f51SKees Cook record->ecc_notice_size); 6357e8cc8dcSKees Cook 636bdabc8e7SKees Cook /* Copy decompressed contents into an minimum-sized allocation. */ 637104fd0b5SYuxiao Zhang unzipped = kvmemdup(workspace, unzipped_len + record->ecc_notice_size, 638bdabc8e7SKees Cook GFP_KERNEL); 639104fd0b5SYuxiao Zhang kvfree(workspace); 640bdabc8e7SKees Cook if (!unzipped) 641bdabc8e7SKees Cook return; 642bdabc8e7SKees Cook 643bdabc8e7SKees Cook /* Swap out compressed contents with decompressed contents. */ 644104fd0b5SYuxiao Zhang kvfree(record->buf); 645bdabc8e7SKees Cook record->buf = unzipped; 646634f8f51SKees Cook record->size = unzipped_len; 647634f8f51SKees Cook record->compressed = false; 648634f8f51SKees Cook } 649634f8f51SKees Cook 650ca01d6ddSTony Luck /* 6513a7d2fd1SKees Cook * Read all the records from one persistent store backend. Create 6526dda9266SLuck, Tony * files in our filesystem. Don't warn about -EEXIST errors 6536dda9266SLuck, Tony * when we are re-scanning the backing store looking to add new 6546dda9266SLuck, Tony * error records. 655ca01d6ddSTony Luck */ 6563a7d2fd1SKees Cook void pstore_get_backend_records(struct pstore_info *psi, 6573a7d2fd1SKees Cook struct dentry *root, int quiet) 658ca01d6ddSTony Luck { 6592a2b0acfSKees Cook int failed = 0; 660656de42eSKees Cook unsigned int stop_loop = 65536; 661438b8050SArd Biesheuvel struct z_stream_s zstream = {}; 662ca01d6ddSTony Luck 6633a7d2fd1SKees Cook if (!psi || !root) 664ca01d6ddSTony Luck return; 665ca01d6ddSTony Luck 666438b8050SArd Biesheuvel if (IS_ENABLED(CONFIG_PSTORE_COMPRESS) && compress) { 667438b8050SArd Biesheuvel zstream.workspace = kvmalloc(zlib_inflate_workspacesize(), 668438b8050SArd Biesheuvel GFP_KERNEL); 669438b8050SArd Biesheuvel zlib_inflateInit2(&zstream, -DEF_WBITS); 670438b8050SArd Biesheuvel } 671438b8050SArd Biesheuvel 672f6f82851SKees Cook mutex_lock(&psi->read_mutex); 6732174f6dfSKees Cook if (psi->open && psi->open(psi)) 67406cf91b4SChen Gong goto out; 67506cf91b4SChen Gong 6761dfff7ddSKees Cook /* 6771dfff7ddSKees Cook * Backend callback read() allocates record.buf. decompress_record() 6781dfff7ddSKees Cook * may reallocate record.buf. On success, pstore_mkfile() will keep 6791dfff7ddSKees Cook * the record.buf, so free it only on failure. 6801dfff7ddSKees Cook */ 681656de42eSKees Cook for (; stop_loop; stop_loop--) { 6822a2b0acfSKees Cook struct pstore_record *record; 6832a2b0acfSKees Cook int rc; 6842a2b0acfSKees Cook 6852a2b0acfSKees Cook record = kzalloc(sizeof(*record), GFP_KERNEL); 6862a2b0acfSKees Cook if (!record) { 6872a2b0acfSKees Cook pr_err("out of memory creating record\n"); 6882a2b0acfSKees Cook break; 6892a2b0acfSKees Cook } 690e581ca81SKees Cook pstore_record_init(record, psi); 6912a2b0acfSKees Cook 6922a2b0acfSKees Cook record->size = psi->read(record); 6932a2b0acfSKees Cook 6942a2b0acfSKees Cook /* No more records left in backend? */ 695f6525b96SDouglas Anderson if (record->size <= 0) { 696f6525b96SDouglas Anderson kfree(record); 6972a2b0acfSKees Cook break; 698f6525b96SDouglas Anderson } 6992a2b0acfSKees Cook 700438b8050SArd Biesheuvel decompress_record(record, &zstream); 7013a7d2fd1SKees Cook rc = pstore_mkfile(root, record); 7021dfff7ddSKees Cook if (rc) { 70383f70f07SKees Cook /* pstore_mkfile() did not take record, so free it. */ 704104fd0b5SYuxiao Zhang kvfree(record->buf); 7058ca869b2SArd Biesheuvel kfree(record->priv); 70683f70f07SKees Cook kfree(record); 7071dfff7ddSKees Cook if (rc != -EEXIST || !quiet) 7081dfff7ddSKees Cook failed++; 7091dfff7ddSKees Cook } 710ca01d6ddSTony Luck } 7112174f6dfSKees Cook if (psi->close) 71206cf91b4SChen Gong psi->close(psi); 71306cf91b4SChen Gong out: 714f6f82851SKees Cook mutex_unlock(&psi->read_mutex); 715ca01d6ddSTony Luck 716438b8050SArd Biesheuvel if (IS_ENABLED(CONFIG_PSTORE_COMPRESS) && compress) { 717438b8050SArd Biesheuvel if (zlib_inflateEnd(&zstream) != Z_OK) 718438b8050SArd Biesheuvel pr_warn("zlib_inflateEnd() failed\n"); 719438b8050SArd Biesheuvel kvfree(zstream.workspace); 720438b8050SArd Biesheuvel } 721438b8050SArd Biesheuvel 722ca01d6ddSTony Luck if (failed) 723656de42eSKees Cook pr_warn("failed to create %d record(s) from '%s'\n", 724ca01d6ddSTony Luck failed, psi->name); 725656de42eSKees Cook if (!stop_loop) 726656de42eSKees Cook pr_err("looping? Too many records seen from '%s'\n", 727656de42eSKees Cook psi->name); 728ca01d6ddSTony Luck } 729ca01d6ddSTony Luck 7306dda9266SLuck, Tony static void pstore_dowork(struct work_struct *work) 7316dda9266SLuck, Tony { 7326dda9266SLuck, Tony pstore_get_records(1); 7336dda9266SLuck, Tony } 7346dda9266SLuck, Tony 73524ed960aSKees Cook static void pstore_timefunc(struct timer_list *unused) 7366dda9266SLuck, Tony { 7376dda9266SLuck, Tony if (pstore_new_entry) { 7386dda9266SLuck, Tony pstore_new_entry = 0; 7396dda9266SLuck, Tony schedule_work(&pstore_work); 7406dda9266SLuck, Tony } 7416dda9266SLuck, Tony 74278c83c82SKees Cook pstore_timer_kick(); 7436dda9266SLuck, Tony } 7446dda9266SLuck, Tony 745cb095afdSKees Cook static int __init pstore_init(void) 746cb095afdSKees Cook { 747cb095afdSKees Cook int ret; 748cb095afdSKees Cook 749cb095afdSKees Cook ret = pstore_init_fs(); 750cb095afdSKees Cook if (ret) 7518a57d6d4Schenqiwu free_buf_for_compression(); 752cb095afdSKees Cook 7538a57d6d4Schenqiwu return ret; 754cb095afdSKees Cook } 75541603165SJoel Fernandes (Google) late_initcall(pstore_init); 756cb095afdSKees Cook 757cb095afdSKees Cook static void __exit pstore_exit(void) 758cb095afdSKees Cook { 759cb095afdSKees Cook pstore_exit_fs(); 760cb095afdSKees Cook } 761cb095afdSKees Cook module_exit(pstore_exit) 762cb095afdSKees Cook 763cb095afdSKees Cook MODULE_AUTHOR("Tony Luck <tony.luck@intel.com>"); 764cb095afdSKees Cook MODULE_LICENSE("GPL"); 765