1ca01d6ddSTony Luck /* 2ca01d6ddSTony Luck * Persistent Storage - platform driver interface parts. 3ca01d6ddSTony Luck * 4f29e5956SAnton Vorontsov * Copyright (C) 2007-2008 Google, Inc. 5ca01d6ddSTony Luck * Copyright (C) 2010 Intel Corporation <tony.luck@intel.com> 6ca01d6ddSTony Luck * 7ca01d6ddSTony Luck * This program is free software; you can redistribute it and/or modify 8ca01d6ddSTony Luck * it under the terms of the GNU General Public License version 2 as 9ca01d6ddSTony Luck * published by the Free Software Foundation. 10ca01d6ddSTony Luck * 11ca01d6ddSTony Luck * This program is distributed in the hope that it will be useful, 12ca01d6ddSTony Luck * but WITHOUT ANY WARRANTY; without even the implied warranty of 13ca01d6ddSTony Luck * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14ca01d6ddSTony Luck * GNU General Public License for more details. 15ca01d6ddSTony Luck * 16ca01d6ddSTony Luck * You should have received a copy of the GNU General Public License 17ca01d6ddSTony Luck * along with this program; if not, write to the Free Software 18ca01d6ddSTony Luck * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 19ca01d6ddSTony Luck */ 20ca01d6ddSTony Luck 21ef748853SFabian Frederick #define pr_fmt(fmt) "pstore: " fmt 22ef748853SFabian Frederick 23ca01d6ddSTony Luck #include <linux/atomic.h> 24ca01d6ddSTony Luck #include <linux/types.h> 25ca01d6ddSTony Luck #include <linux/errno.h> 26ca01d6ddSTony Luck #include <linux/init.h> 27ca01d6ddSTony Luck #include <linux/kmsg_dump.h> 28f29e5956SAnton Vorontsov #include <linux/console.h> 29ca01d6ddSTony Luck #include <linux/module.h> 30ca01d6ddSTony Luck #include <linux/pstore.h> 3158eb5b67SArnd Bergmann #if IS_ENABLED(CONFIG_PSTORE_LZO_COMPRESS) 328cfc8ddcSGeliang Tang #include <linux/lzo.h> 338cfc8ddcSGeliang Tang #endif 3458eb5b67SArnd Bergmann #if IS_ENABLED(CONFIG_PSTORE_LZ4_COMPRESS) || IS_ENABLED(CONFIG_PSTORE_LZ4HC_COMPRESS) 358cfc8ddcSGeliang Tang #include <linux/lz4.h> 368cfc8ddcSGeliang Tang #endif 37*1021bcf4SGeliang Tang #if IS_ENABLED(CONFIG_PSTORE_ZSTD_COMPRESS) 38*1021bcf4SGeliang Tang #include <linux/zstd.h> 39*1021bcf4SGeliang Tang #endif 40cb3bee03SGeliang Tang #include <linux/crypto.h> 41ca01d6ddSTony Luck #include <linux/string.h> 426dda9266SLuck, Tony #include <linux/timer.h> 43ca01d6ddSTony Luck #include <linux/slab.h> 44ca01d6ddSTony Luck #include <linux/uaccess.h> 45a3f5f075SAnton Vorontsov #include <linux/jiffies.h> 466dda9266SLuck, Tony #include <linux/workqueue.h> 47ca01d6ddSTony Luck 48ca01d6ddSTony Luck #include "internal.h" 49ca01d6ddSTony Luck 50ca01d6ddSTony Luck /* 516dda9266SLuck, Tony * We defer making "oops" entries appear in pstore - see 526dda9266SLuck, Tony * whether the system is actually still running well enough 536dda9266SLuck, Tony * to let someone see the entry 546dda9266SLuck, Tony */ 55521f7288SAnton Vorontsov static int pstore_update_ms = -1; 56a3f5f075SAnton Vorontsov module_param_named(update_ms, pstore_update_ms, int, 0600); 57a3f5f075SAnton Vorontsov MODULE_PARM_DESC(update_ms, "milliseconds before pstore updates its content " 58521f7288SAnton Vorontsov "(default is -1, which means runtime updates are disabled; " 59521f7288SAnton Vorontsov "enabling this option is not safe, it may lead to further " 60521f7288SAnton Vorontsov "corruption on Oopses)"); 616dda9266SLuck, Tony 626dda9266SLuck, Tony static int pstore_new_entry; 636dda9266SLuck, Tony 6424ed960aSKees Cook static void pstore_timefunc(struct timer_list *); 651d27e3e2SKees Cook static DEFINE_TIMER(pstore_timer, pstore_timefunc); 666dda9266SLuck, Tony 676dda9266SLuck, Tony static void pstore_dowork(struct work_struct *); 686dda9266SLuck, Tony static DECLARE_WORK(pstore_work, pstore_dowork); 696dda9266SLuck, Tony 706dda9266SLuck, Tony /* 71ca01d6ddSTony Luck * pstore_lock just protects "psinfo" during 72ca01d6ddSTony Luck * calls to pstore_register() 73ca01d6ddSTony Luck */ 74ca01d6ddSTony Luck static DEFINE_SPINLOCK(pstore_lock); 75060287b8SAnton Vorontsov struct pstore_info *psinfo; 76ca01d6ddSTony Luck 77dee28e72SMatthew Garrett static char *backend; 78fe1d4758SKees Cook static char *compress = 79fe1d4758SKees Cook #ifdef CONFIG_PSTORE_COMPRESS_DEFAULT 80fe1d4758SKees Cook CONFIG_PSTORE_COMPRESS_DEFAULT; 81fe1d4758SKees Cook #else 82fe1d4758SKees Cook NULL; 83fe1d4758SKees Cook #endif 84dee28e72SMatthew Garrett 85b0aad7a9SAruna Balakrishnaiah /* Compression parameters */ 86cb3bee03SGeliang Tang static struct crypto_comp *tfm; 878cfc8ddcSGeliang Tang 888cfc8ddcSGeliang Tang struct pstore_zbackend { 89cb3bee03SGeliang Tang int (*zbufsize)(size_t size); 908cfc8ddcSGeliang Tang const char *name; 918cfc8ddcSGeliang Tang }; 92b0aad7a9SAruna Balakrishnaiah 93b0aad7a9SAruna Balakrishnaiah static char *big_oops_buf; 94b0aad7a9SAruna Balakrishnaiah static size_t big_oops_buf_sz; 95b0aad7a9SAruna Balakrishnaiah 96366f7e7aSLuck, Tony /* How much of the console log to snapshot */ 97349d7438SDavid Howells unsigned long kmsg_bytes = PSTORE_DEFAULT_KMSG_BYTES; 98ca01d6ddSTony Luck 99366f7e7aSLuck, Tony void pstore_set_kmsg_bytes(int bytes) 100ca01d6ddSTony Luck { 101366f7e7aSLuck, Tony kmsg_bytes = bytes; 102ca01d6ddSTony Luck } 103ca01d6ddSTony Luck 104ca01d6ddSTony Luck /* Tag each group of saved records with a sequence number */ 105ca01d6ddSTony Luck static int oopscount; 106ca01d6ddSTony Luck 107381b872cSSeiji Aguchi static const char *get_reason_str(enum kmsg_dump_reason reason) 108381b872cSSeiji Aguchi { 109381b872cSSeiji Aguchi switch (reason) { 110381b872cSSeiji Aguchi case KMSG_DUMP_PANIC: 111381b872cSSeiji Aguchi return "Panic"; 112381b872cSSeiji Aguchi case KMSG_DUMP_OOPS: 113381b872cSSeiji Aguchi return "Oops"; 114381b872cSSeiji Aguchi case KMSG_DUMP_EMERG: 115381b872cSSeiji Aguchi return "Emergency"; 116381b872cSSeiji Aguchi case KMSG_DUMP_RESTART: 117381b872cSSeiji Aguchi return "Restart"; 118381b872cSSeiji Aguchi case KMSG_DUMP_HALT: 119381b872cSSeiji Aguchi return "Halt"; 120381b872cSSeiji Aguchi case KMSG_DUMP_POWEROFF: 121381b872cSSeiji Aguchi return "Poweroff"; 122381b872cSSeiji Aguchi default: 123381b872cSSeiji Aguchi return "Unknown"; 124381b872cSSeiji Aguchi } 125381b872cSSeiji Aguchi } 1269f6af27fSTony Luck 1279f244e9cSSeiji Aguchi bool pstore_cannot_block_path(enum kmsg_dump_reason reason) 1289f244e9cSSeiji Aguchi { 1299f244e9cSSeiji Aguchi /* 1309f244e9cSSeiji Aguchi * In case of NMI path, pstore shouldn't be blocked 1319f244e9cSSeiji Aguchi * regardless of reason. 1329f244e9cSSeiji Aguchi */ 1339f244e9cSSeiji Aguchi if (in_nmi()) 1349f244e9cSSeiji Aguchi return true; 1359f244e9cSSeiji Aguchi 1369f244e9cSSeiji Aguchi switch (reason) { 1379f244e9cSSeiji Aguchi /* In panic case, other cpus are stopped by smp_send_stop(). */ 1389f244e9cSSeiji Aguchi case KMSG_DUMP_PANIC: 1399f244e9cSSeiji Aguchi /* Emergency restart shouldn't be blocked by spin lock. */ 1409f244e9cSSeiji Aguchi case KMSG_DUMP_EMERG: 1419f244e9cSSeiji Aguchi return true; 1429f244e9cSSeiji Aguchi default: 1439f244e9cSSeiji Aguchi return false; 1449f244e9cSSeiji Aguchi } 1459f244e9cSSeiji Aguchi } 1469f244e9cSSeiji Aguchi EXPORT_SYMBOL_GPL(pstore_cannot_block_path); 1479f244e9cSSeiji Aguchi 14858eb5b67SArnd Bergmann #if IS_ENABLED(CONFIG_PSTORE_DEFLATE_COMPRESS) 149cb3bee03SGeliang Tang static int zbufsize_deflate(size_t size) 150b0aad7a9SAruna Balakrishnaiah { 1517de8fe2fSAruna Balakrishnaiah size_t cmpr; 152b0aad7a9SAruna Balakrishnaiah 153cb3bee03SGeliang Tang switch (size) { 1547de8fe2fSAruna Balakrishnaiah /* buffer range for efivars */ 1557de8fe2fSAruna Balakrishnaiah case 1000 ... 2000: 1567de8fe2fSAruna Balakrishnaiah cmpr = 56; 1577de8fe2fSAruna Balakrishnaiah break; 1587de8fe2fSAruna Balakrishnaiah case 2001 ... 3000: 1597de8fe2fSAruna Balakrishnaiah cmpr = 54; 1607de8fe2fSAruna Balakrishnaiah break; 1617de8fe2fSAruna Balakrishnaiah case 3001 ... 3999: 1627de8fe2fSAruna Balakrishnaiah cmpr = 52; 1637de8fe2fSAruna Balakrishnaiah break; 1647de8fe2fSAruna Balakrishnaiah /* buffer range for nvram, erst */ 1657de8fe2fSAruna Balakrishnaiah case 4000 ... 10000: 1667de8fe2fSAruna Balakrishnaiah cmpr = 45; 1677de8fe2fSAruna Balakrishnaiah break; 1687de8fe2fSAruna Balakrishnaiah default: 1697de8fe2fSAruna Balakrishnaiah cmpr = 60; 1707de8fe2fSAruna Balakrishnaiah break; 1717de8fe2fSAruna Balakrishnaiah } 1727de8fe2fSAruna Balakrishnaiah 173cb3bee03SGeliang Tang return (size * 100) / cmpr; 1748cfc8ddcSGeliang Tang } 1758cfc8ddcSGeliang Tang #endif 1768cfc8ddcSGeliang Tang 17758eb5b67SArnd Bergmann #if IS_ENABLED(CONFIG_PSTORE_LZO_COMPRESS) 178cb3bee03SGeliang Tang static int zbufsize_lzo(size_t size) 1798cfc8ddcSGeliang Tang { 180cb3bee03SGeliang Tang return lzo1x_worst_compress(size); 1818cfc8ddcSGeliang Tang } 1828cfc8ddcSGeliang Tang #endif 1838cfc8ddcSGeliang Tang 18458eb5b67SArnd Bergmann #if IS_ENABLED(CONFIG_PSTORE_LZ4_COMPRESS) || IS_ENABLED(CONFIG_PSTORE_LZ4HC_COMPRESS) 185cb3bee03SGeliang Tang static int zbufsize_lz4(size_t size) 1868cfc8ddcSGeliang Tang { 187cb3bee03SGeliang Tang return LZ4_compressBound(size); 188239b7161SGeliang Tang } 189239b7161SGeliang Tang #endif 190239b7161SGeliang Tang 19158eb5b67SArnd Bergmann #if IS_ENABLED(CONFIG_PSTORE_842_COMPRESS) 192cb3bee03SGeliang Tang static int zbufsize_842(size_t size) 193239b7161SGeliang Tang { 19455597406SKees Cook return size; 195239b7161SGeliang Tang } 196fe1d4758SKees Cook #endif 1978cfc8ddcSGeliang Tang 198*1021bcf4SGeliang Tang #if IS_ENABLED(CONFIG_PSTORE_ZSTD_COMPRESS) 199*1021bcf4SGeliang Tang static int zbufsize_zstd(size_t size) 200*1021bcf4SGeliang Tang { 201*1021bcf4SGeliang Tang return ZSTD_compressBound(size); 202*1021bcf4SGeliang Tang } 203*1021bcf4SGeliang Tang #endif 204*1021bcf4SGeliang Tang 205fe1d4758SKees Cook static const struct pstore_zbackend *zbackend __ro_after_init; 206fe1d4758SKees Cook 207fe1d4758SKees Cook static const struct pstore_zbackend zbackends[] = { 20858eb5b67SArnd Bergmann #if IS_ENABLED(CONFIG_PSTORE_DEFLATE_COMPRESS) 209fe1d4758SKees Cook { 210cb3bee03SGeliang Tang .zbufsize = zbufsize_deflate, 211cb3bee03SGeliang Tang .name = "deflate", 212fe1d4758SKees Cook }, 213fe1d4758SKees Cook #endif 21458eb5b67SArnd Bergmann #if IS_ENABLED(CONFIG_PSTORE_LZO_COMPRESS) 215fe1d4758SKees Cook { 216cb3bee03SGeliang Tang .zbufsize = zbufsize_lzo, 217fe1d4758SKees Cook .name = "lzo", 218fe1d4758SKees Cook }, 219fe1d4758SKees Cook #endif 22058eb5b67SArnd Bergmann #if IS_ENABLED(CONFIG_PSTORE_LZ4_COMPRESS) 221fe1d4758SKees Cook { 222cb3bee03SGeliang Tang .zbufsize = zbufsize_lz4, 223fe1d4758SKees Cook .name = "lz4", 224fe1d4758SKees Cook }, 225fe1d4758SKees Cook #endif 22658eb5b67SArnd Bergmann #if IS_ENABLED(CONFIG_PSTORE_LZ4HC_COMPRESS) 227fe1d4758SKees Cook { 228cb3bee03SGeliang Tang .zbufsize = zbufsize_lz4, 229fe1d4758SKees Cook .name = "lz4hc", 230fe1d4758SKees Cook }, 231fe1d4758SKees Cook #endif 23258eb5b67SArnd Bergmann #if IS_ENABLED(CONFIG_PSTORE_842_COMPRESS) 233fe1d4758SKees Cook { 234cb3bee03SGeliang Tang .zbufsize = zbufsize_842, 235239b7161SGeliang Tang .name = "842", 236fe1d4758SKees Cook }, 237fe1d4758SKees Cook #endif 238*1021bcf4SGeliang Tang #if IS_ENABLED(CONFIG_PSTORE_ZSTD_COMPRESS) 239*1021bcf4SGeliang Tang { 240*1021bcf4SGeliang Tang .zbufsize = zbufsize_zstd, 241*1021bcf4SGeliang Tang .name = "zstd", 242*1021bcf4SGeliang Tang }, 243*1021bcf4SGeliang Tang #endif 244fe1d4758SKees Cook { } 2458cfc8ddcSGeliang Tang }; 2468cfc8ddcSGeliang Tang 2478cfc8ddcSGeliang Tang static int pstore_compress(const void *in, void *out, 248cb3bee03SGeliang Tang unsigned int inlen, unsigned int outlen) 2498cfc8ddcSGeliang Tang { 250cb3bee03SGeliang Tang int ret; 251cb3bee03SGeliang Tang 252cb3bee03SGeliang Tang ret = crypto_comp_compress(tfm, in, inlen, out, &outlen); 253cb3bee03SGeliang Tang if (ret) { 254cb3bee03SGeliang Tang pr_err("crypto_comp_compress failed, ret = %d!\n", ret); 255cb3bee03SGeliang Tang return ret; 2568cfc8ddcSGeliang Tang } 2578cfc8ddcSGeliang Tang 258cb3bee03SGeliang Tang return outlen; 259cb3bee03SGeliang Tang } 260cb3bee03SGeliang Tang 261cb3bee03SGeliang Tang static int pstore_decompress(void *in, void *out, 262cb3bee03SGeliang Tang unsigned int inlen, unsigned int outlen) 2638cfc8ddcSGeliang Tang { 264cb3bee03SGeliang Tang int ret; 265cb3bee03SGeliang Tang 266cb3bee03SGeliang Tang ret = crypto_comp_decompress(tfm, in, inlen, out, &outlen); 267cb3bee03SGeliang Tang if (ret) { 268cb3bee03SGeliang Tang pr_err("crypto_comp_decompress failed, ret = %d!\n", ret); 269cb3bee03SGeliang Tang return ret; 270cb3bee03SGeliang Tang } 271cb3bee03SGeliang Tang 272cb3bee03SGeliang Tang return outlen; 2738cfc8ddcSGeliang Tang } 2748cfc8ddcSGeliang Tang 2758cfc8ddcSGeliang Tang static void allocate_buf_for_compression(void) 2768cfc8ddcSGeliang Tang { 277e698aaf3STobias Regnery if (!IS_ENABLED(CONFIG_PSTORE_COMPRESS) || !zbackend) 278cb3bee03SGeliang Tang return; 279cb3bee03SGeliang Tang 280cb3bee03SGeliang Tang if (!crypto_has_comp(zbackend->name, 0, 0)) { 281cb3bee03SGeliang Tang pr_err("No %s compression\n", zbackend->name); 282cb3bee03SGeliang Tang return; 283cb3bee03SGeliang Tang } 284cb3bee03SGeliang Tang 285cb3bee03SGeliang Tang big_oops_buf_sz = zbackend->zbufsize(psinfo->bufsize); 286cb3bee03SGeliang Tang if (big_oops_buf_sz <= 0) 287cb3bee03SGeliang Tang return; 288cb3bee03SGeliang Tang 289cb3bee03SGeliang Tang big_oops_buf = kmalloc(big_oops_buf_sz, GFP_KERNEL); 290cb3bee03SGeliang Tang if (!big_oops_buf) { 2918cfc8ddcSGeliang Tang pr_err("allocate compression buffer error!\n"); 292cb3bee03SGeliang Tang return; 293cb3bee03SGeliang Tang } 294cb3bee03SGeliang Tang 295cb3bee03SGeliang Tang tfm = crypto_alloc_comp(zbackend->name, 0, 0); 296cb3bee03SGeliang Tang if (IS_ERR_OR_NULL(tfm)) { 297cb3bee03SGeliang Tang kfree(big_oops_buf); 298cb3bee03SGeliang Tang big_oops_buf = NULL; 299cb3bee03SGeliang Tang pr_err("crypto_alloc_comp() failed!\n"); 300cb3bee03SGeliang Tang return; 3018cfc8ddcSGeliang Tang } 3028cfc8ddcSGeliang Tang } 3038cfc8ddcSGeliang Tang 3048cfc8ddcSGeliang Tang static void free_buf_for_compression(void) 3058cfc8ddcSGeliang Tang { 306e698aaf3STobias Regnery if (IS_ENABLED(CONFIG_PSTORE_COMPRESS) && !IS_ERR_OR_NULL(tfm)) 307cb3bee03SGeliang Tang crypto_free_comp(tfm); 308cb3bee03SGeliang Tang kfree(big_oops_buf); 309cb3bee03SGeliang Tang big_oops_buf = NULL; 310cb3bee03SGeliang Tang big_oops_buf_sz = 0; 311ee1d2674SGeliang Tang } 312ee1d2674SGeliang Tang 313b0aad7a9SAruna Balakrishnaiah /* 314b0aad7a9SAruna Balakrishnaiah * Called when compression fails, since the printk buffer 315b0aad7a9SAruna Balakrishnaiah * would be fetched for compression calling it again when 316b0aad7a9SAruna Balakrishnaiah * compression fails would have moved the iterator of 317b0aad7a9SAruna Balakrishnaiah * printk buffer which results in fetching old contents. 318b0aad7a9SAruna Balakrishnaiah * Copy the recent messages from big_oops_buf to psinfo->buf 319b0aad7a9SAruna Balakrishnaiah */ 320b0aad7a9SAruna Balakrishnaiah static size_t copy_kmsg_to_buffer(int hsize, size_t len) 321b0aad7a9SAruna Balakrishnaiah { 322b0aad7a9SAruna Balakrishnaiah size_t total_len; 323b0aad7a9SAruna Balakrishnaiah size_t diff; 324b0aad7a9SAruna Balakrishnaiah 325b0aad7a9SAruna Balakrishnaiah total_len = hsize + len; 326b0aad7a9SAruna Balakrishnaiah 327b0aad7a9SAruna Balakrishnaiah if (total_len > psinfo->bufsize) { 328b0aad7a9SAruna Balakrishnaiah diff = total_len - psinfo->bufsize + hsize; 329b0aad7a9SAruna Balakrishnaiah memcpy(psinfo->buf, big_oops_buf, hsize); 330b0aad7a9SAruna Balakrishnaiah memcpy(psinfo->buf + hsize, big_oops_buf + diff, 331b0aad7a9SAruna Balakrishnaiah psinfo->bufsize - hsize); 332b0aad7a9SAruna Balakrishnaiah total_len = psinfo->bufsize; 333b0aad7a9SAruna Balakrishnaiah } else 334b0aad7a9SAruna Balakrishnaiah memcpy(psinfo->buf, big_oops_buf, total_len); 335b0aad7a9SAruna Balakrishnaiah 336b0aad7a9SAruna Balakrishnaiah return total_len; 337b0aad7a9SAruna Balakrishnaiah } 338b0aad7a9SAruna Balakrishnaiah 339e581ca81SKees Cook void pstore_record_init(struct pstore_record *record, 340e581ca81SKees Cook struct pstore_info *psinfo) 341e581ca81SKees Cook { 342e581ca81SKees Cook memset(record, 0, sizeof(*record)); 343e581ca81SKees Cook 344e581ca81SKees Cook record->psi = psinfo; 345c7f3c595SKees Cook 346c7f3c595SKees Cook /* Report zeroed timestamp if called before timekeeping has resumed. */ 3477aaa822eSKees Cook record->time = ns_to_timespec64(ktime_get_real_fast_ns()); 348e581ca81SKees Cook } 349e581ca81SKees Cook 350ca01d6ddSTony Luck /* 351ca01d6ddSTony Luck * callback from kmsg_dump. (s2,l2) has the most recently 352ca01d6ddSTony Luck * written bytes, older bytes are in (s1,l1). Save as much 353ca01d6ddSTony Luck * as we can from the end of the buffer. 354ca01d6ddSTony Luck */ 355ca01d6ddSTony Luck static void pstore_dump(struct kmsg_dumper *dumper, 356e2ae715dSKay Sievers enum kmsg_dump_reason reason) 357ca01d6ddSTony Luck { 358e2ae715dSKay Sievers unsigned long total = 0; 359381b872cSSeiji Aguchi const char *why; 360b94fdd07SMatthew Garrett unsigned int part = 1; 361abd4d558SDon Zickus unsigned long flags = 0; 36298e44fdaSNamhyung Kim int is_locked; 363e2ae715dSKay Sievers int ret; 364ca01d6ddSTony Luck 365381b872cSSeiji Aguchi why = get_reason_str(reason); 3669f6af27fSTony Luck 3679f244e9cSSeiji Aguchi if (pstore_cannot_block_path(reason)) { 3689f244e9cSSeiji Aguchi is_locked = spin_trylock_irqsave(&psinfo->buf_lock, flags); 3699f244e9cSSeiji Aguchi if (!is_locked) { 3709f244e9cSSeiji Aguchi pr_err("pstore dump routine blocked in %s path, may corrupt error record\n" 3719f244e9cSSeiji Aguchi , in_nmi() ? "NMI" : why); 372959217c8SLi Pengcheng return; 3739f244e9cSSeiji Aguchi } 37498e44fdaSNamhyung Kim } else { 375abd4d558SDon Zickus spin_lock_irqsave(&psinfo->buf_lock, flags); 37698e44fdaSNamhyung Kim is_locked = 1; 37798e44fdaSNamhyung Kim } 378ca01d6ddSTony Luck oopscount++; 379ca01d6ddSTony Luck while (total < kmsg_bytes) { 380e2ae715dSKay Sievers char *dst; 38176cc9580SKees Cook size_t dst_size; 38276cc9580SKees Cook int header_size; 383b0aad7a9SAruna Balakrishnaiah int zipped_len = -1; 38476cc9580SKees Cook size_t dump_size; 385e581ca81SKees Cook struct pstore_record record; 386e581ca81SKees Cook 387e581ca81SKees Cook pstore_record_init(&record, psinfo); 388e581ca81SKees Cook record.type = PSTORE_TYPE_DMESG; 389e581ca81SKees Cook record.count = oopscount; 390e581ca81SKees Cook record.reason = reason; 391e581ca81SKees Cook record.part = part; 392e581ca81SKees Cook record.buf = psinfo->buf; 393e2ae715dSKay Sievers 394f0e2efcfSKonstantin Khlebnikov if (big_oops_buf && is_locked) { 395b0aad7a9SAruna Balakrishnaiah dst = big_oops_buf; 39676cc9580SKees Cook dst_size = big_oops_buf_sz; 397235f6d15SNamhyung Kim } else { 398235f6d15SNamhyung Kim dst = psinfo->buf; 39976cc9580SKees Cook dst_size = psinfo->bufsize; 400235f6d15SNamhyung Kim } 401235f6d15SNamhyung Kim 40276cc9580SKees Cook /* Write dump header. */ 40376cc9580SKees Cook header_size = snprintf(dst, dst_size, "%s#%d Part%u\n", why, 40476cc9580SKees Cook oopscount, part); 40576cc9580SKees Cook dst_size -= header_size; 406b0aad7a9SAruna Balakrishnaiah 40776cc9580SKees Cook /* Write dump contents. */ 40876cc9580SKees Cook if (!kmsg_dump_get_buffer(dumper, true, dst + header_size, 40976cc9580SKees Cook dst_size, &dump_size)) 410b0aad7a9SAruna Balakrishnaiah break; 411b0aad7a9SAruna Balakrishnaiah 412235f6d15SNamhyung Kim if (big_oops_buf && is_locked) { 413b0aad7a9SAruna Balakrishnaiah zipped_len = pstore_compress(dst, psinfo->buf, 41476cc9580SKees Cook header_size + dump_size, 41576cc9580SKees Cook psinfo->bufsize); 416b0aad7a9SAruna Balakrishnaiah 417b0aad7a9SAruna Balakrishnaiah if (zipped_len > 0) { 41876cc9580SKees Cook record.compressed = true; 41976cc9580SKees Cook record.size = zipped_len; 420b0aad7a9SAruna Balakrishnaiah } else { 42176cc9580SKees Cook record.size = copy_kmsg_to_buffer(header_size, 42276cc9580SKees Cook dump_size); 423b0aad7a9SAruna Balakrishnaiah } 424b0aad7a9SAruna Balakrishnaiah } else { 42576cc9580SKees Cook record.size = header_size + dump_size; 426b0aad7a9SAruna Balakrishnaiah } 427b0aad7a9SAruna Balakrishnaiah 42876cc9580SKees Cook ret = psinfo->write(&record); 429b238b8faSChen Gong if (ret == 0 && reason == KMSG_DUMP_OOPS && pstore_is_mounted()) 4306dda9266SLuck, Tony pstore_new_entry = 1; 431e2ae715dSKay Sievers 43276cc9580SKees Cook total += record.size; 43356280682SMatthew Garrett part++; 434ca01d6ddSTony Luck } 435abd4d558SDon Zickus if (is_locked) 4369f244e9cSSeiji Aguchi spin_unlock_irqrestore(&psinfo->buf_lock, flags); 437ca01d6ddSTony Luck } 438ca01d6ddSTony Luck 439ca01d6ddSTony Luck static struct kmsg_dumper pstore_dumper = { 440ca01d6ddSTony Luck .dump = pstore_dump, 441ca01d6ddSTony Luck }; 442ca01d6ddSTony Luck 443306e5c2aSGeliang Tang /* 444306e5c2aSGeliang Tang * Register with kmsg_dump to save last part of console log on panic. 445306e5c2aSGeliang Tang */ 44618730411SGeliang Tang static void pstore_register_kmsg(void) 44718730411SGeliang Tang { 44818730411SGeliang Tang kmsg_dump_register(&pstore_dumper); 44918730411SGeliang Tang } 45018730411SGeliang Tang 451ee1d2674SGeliang Tang static void pstore_unregister_kmsg(void) 452ee1d2674SGeliang Tang { 453ee1d2674SGeliang Tang kmsg_dump_unregister(&pstore_dumper); 454ee1d2674SGeliang Tang } 455ee1d2674SGeliang Tang 456f29e5956SAnton Vorontsov #ifdef CONFIG_PSTORE_CONSOLE 457f29e5956SAnton Vorontsov static void pstore_console_write(struct console *con, const char *s, unsigned c) 458f29e5956SAnton Vorontsov { 459f29e5956SAnton Vorontsov const char *e = s + c; 460f29e5956SAnton Vorontsov 461f29e5956SAnton Vorontsov while (s < e) { 462e581ca81SKees Cook struct pstore_record record; 463f29e5956SAnton Vorontsov unsigned long flags; 464f29e5956SAnton Vorontsov 465e581ca81SKees Cook pstore_record_init(&record, psinfo); 466e581ca81SKees Cook record.type = PSTORE_TYPE_CONSOLE; 467e581ca81SKees Cook 468f29e5956SAnton Vorontsov if (c > psinfo->bufsize) 469f29e5956SAnton Vorontsov c = psinfo->bufsize; 47080c9d03cSChuansheng Liu 47180c9d03cSChuansheng Liu if (oops_in_progress) { 47280c9d03cSChuansheng Liu if (!spin_trylock_irqsave(&psinfo->buf_lock, flags)) 47380c9d03cSChuansheng Liu break; 47480c9d03cSChuansheng Liu } else { 475f29e5956SAnton Vorontsov spin_lock_irqsave(&psinfo->buf_lock, flags); 47680c9d03cSChuansheng Liu } 477b10b4711SKees Cook record.buf = (char *)s; 478b10b4711SKees Cook record.size = c; 4794c9ec219SKees Cook psinfo->write(&record); 480f29e5956SAnton Vorontsov spin_unlock_irqrestore(&psinfo->buf_lock, flags); 481f29e5956SAnton Vorontsov s += c; 482f29e5956SAnton Vorontsov c = e - s; 483f29e5956SAnton Vorontsov } 484f29e5956SAnton Vorontsov } 485f29e5956SAnton Vorontsov 486f29e5956SAnton Vorontsov static struct console pstore_console = { 487f29e5956SAnton Vorontsov .name = "pstore", 488f29e5956SAnton Vorontsov .write = pstore_console_write, 489f29e5956SAnton Vorontsov .flags = CON_PRINTBUFFER | CON_ENABLED | CON_ANYTIME, 490f29e5956SAnton Vorontsov .index = -1, 491f29e5956SAnton Vorontsov }; 492f29e5956SAnton Vorontsov 493f29e5956SAnton Vorontsov static void pstore_register_console(void) 494f29e5956SAnton Vorontsov { 495f29e5956SAnton Vorontsov register_console(&pstore_console); 496f29e5956SAnton Vorontsov } 497ee1d2674SGeliang Tang 498ee1d2674SGeliang Tang static void pstore_unregister_console(void) 499ee1d2674SGeliang Tang { 500ee1d2674SGeliang Tang unregister_console(&pstore_console); 501ee1d2674SGeliang Tang } 502f29e5956SAnton Vorontsov #else 503f29e5956SAnton Vorontsov static void pstore_register_console(void) {} 504ee1d2674SGeliang Tang static void pstore_unregister_console(void) {} 505f29e5956SAnton Vorontsov #endif 506f29e5956SAnton Vorontsov 5074c9ec219SKees Cook static int pstore_write_user_compat(struct pstore_record *record, 508fdd03118SKees Cook const char __user *buf) 5095bf6d1b9SMark Salyzyn { 51030800d99SKees Cook int ret = 0; 5115bf6d1b9SMark Salyzyn 51230800d99SKees Cook if (record->buf) 51330800d99SKees Cook return -EINVAL; 5145bf6d1b9SMark Salyzyn 515077090afSGeliang Tang record->buf = memdup_user(buf, record->size); 516dfd6fa39SHirofumi Nakagawa if (IS_ERR(record->buf)) { 517077090afSGeliang Tang ret = PTR_ERR(record->buf); 51830800d99SKees Cook goto out; 5195bf6d1b9SMark Salyzyn } 52030800d99SKees Cook 5214c9ec219SKees Cook ret = record->psi->write(record); 52230800d99SKees Cook 52330800d99SKees Cook kfree(record->buf); 524077090afSGeliang Tang out: 52530800d99SKees Cook record->buf = NULL; 52630800d99SKees Cook 52730800d99SKees Cook return unlikely(ret < 0) ? ret : record->size; 5285bf6d1b9SMark Salyzyn } 5295bf6d1b9SMark Salyzyn 530ca01d6ddSTony Luck /* 531ca01d6ddSTony Luck * platform specific persistent storage driver registers with 532ca01d6ddSTony Luck * us here. If pstore is already mounted, call the platform 533ca01d6ddSTony Luck * read function right away to populate the file system. If not 534ca01d6ddSTony Luck * then the pstore mount code will call us later to fill out 535ca01d6ddSTony Luck * the file system. 536ca01d6ddSTony Luck */ 537ca01d6ddSTony Luck int pstore_register(struct pstore_info *psi) 538ca01d6ddSTony Luck { 539ca01d6ddSTony Luck struct module *owner = psi->owner; 540ca01d6ddSTony Luck 5410d7cd09aSKees Cook if (backend && strcmp(backend, psi->name)) { 5420d7cd09aSKees Cook pr_warn("ignoring unexpected backend '%s'\n", psi->name); 5438e48b1a8SLenny Szubowicz return -EPERM; 5440d7cd09aSKees Cook } 5458e48b1a8SLenny Szubowicz 5464c9ec219SKees Cook /* Sanity check flags. */ 5474c9ec219SKees Cook if (!psi->flags) { 5484c9ec219SKees Cook pr_warn("backend '%s' must support at least one frontend\n", 5494c9ec219SKees Cook psi->name); 5504c9ec219SKees Cook return -EINVAL; 5514c9ec219SKees Cook } 5524c9ec219SKees Cook 5534c9ec219SKees Cook /* Check for required functions. */ 5544c9ec219SKees Cook if (!psi->read || !psi->write) { 5554c9ec219SKees Cook pr_warn("backend '%s' must implement read() and write()\n", 5564c9ec219SKees Cook psi->name); 5574c9ec219SKees Cook return -EINVAL; 5584c9ec219SKees Cook } 5594c9ec219SKees Cook 560ca01d6ddSTony Luck spin_lock(&pstore_lock); 561ca01d6ddSTony Luck if (psinfo) { 5620d7cd09aSKees Cook pr_warn("backend '%s' already loaded: ignoring '%s'\n", 5630d7cd09aSKees Cook psinfo->name, psi->name); 564ca01d6ddSTony Luck spin_unlock(&pstore_lock); 565ca01d6ddSTony Luck return -EBUSY; 566ca01d6ddSTony Luck } 567dee28e72SMatthew Garrett 5684c9ec219SKees Cook if (!psi->write_user) 5694c9ec219SKees Cook psi->write_user = pstore_write_user_compat; 570ca01d6ddSTony Luck psinfo = psi; 571f6f82851SKees Cook mutex_init(&psinfo->read_mutex); 572ca01d6ddSTony Luck spin_unlock(&pstore_lock); 573ca01d6ddSTony Luck 574ca01d6ddSTony Luck if (owner && !try_module_get(owner)) { 575ca01d6ddSTony Luck psinfo = NULL; 576ca01d6ddSTony Luck return -EINVAL; 577ca01d6ddSTony Luck } 578ca01d6ddSTony Luck 579b0aad7a9SAruna Balakrishnaiah allocate_buf_for_compression(); 580b0aad7a9SAruna Balakrishnaiah 581ca01d6ddSTony Luck if (pstore_is_mounted()) 5826dda9266SLuck, Tony pstore_get_records(0); 583ca01d6ddSTony Luck 584c950fd6fSNamhyung Kim if (psi->flags & PSTORE_FLAGS_DMESG) 58518730411SGeliang Tang pstore_register_kmsg(); 586c950fd6fSNamhyung Kim if (psi->flags & PSTORE_FLAGS_CONSOLE) 587f29e5956SAnton Vorontsov pstore_register_console(); 588c950fd6fSNamhyung Kim if (psi->flags & PSTORE_FLAGS_FTRACE) 58965f8c95eSAnton Vorontsov pstore_register_ftrace(); 590c950fd6fSNamhyung Kim if (psi->flags & PSTORE_FLAGS_PMSG) 5919d5438f4SMark Salyzyn pstore_register_pmsg(); 592ca01d6ddSTony Luck 5936330d553SKees Cook /* Start watching for new records, if desired. */ 594a3f5f075SAnton Vorontsov if (pstore_update_ms >= 0) { 595a3f5f075SAnton Vorontsov pstore_timer.expires = jiffies + 596a3f5f075SAnton Vorontsov msecs_to_jiffies(pstore_update_ms); 5976dda9266SLuck, Tony add_timer(&pstore_timer); 598a3f5f075SAnton Vorontsov } 5996dda9266SLuck, Tony 60042222c2aSWang Long /* 60142222c2aSWang Long * Update the module parameter backend, so it is visible 60242222c2aSWang Long * through /sys/module/pstore/parameters/backend 60342222c2aSWang Long */ 60442222c2aSWang Long backend = psi->name; 60542222c2aSWang Long 606ef748853SFabian Frederick pr_info("Registered %s as persistent store backend\n", psi->name); 6078e48b1a8SLenny Szubowicz 6081344dd86SKees Cook module_put(owner); 6091344dd86SKees Cook 610ca01d6ddSTony Luck return 0; 611ca01d6ddSTony Luck } 612ca01d6ddSTony Luck EXPORT_SYMBOL_GPL(pstore_register); 613ca01d6ddSTony Luck 614ee1d2674SGeliang Tang void pstore_unregister(struct pstore_info *psi) 615ee1d2674SGeliang Tang { 6166330d553SKees Cook /* Stop timer and make sure all work has finished. */ 6176330d553SKees Cook pstore_update_ms = -1; 6186330d553SKees Cook del_timer_sync(&pstore_timer); 6196330d553SKees Cook flush_work(&pstore_work); 6206330d553SKees Cook 621c950fd6fSNamhyung Kim if (psi->flags & PSTORE_FLAGS_PMSG) 622ee1d2674SGeliang Tang pstore_unregister_pmsg(); 623c950fd6fSNamhyung Kim if (psi->flags & PSTORE_FLAGS_FTRACE) 624ee1d2674SGeliang Tang pstore_unregister_ftrace(); 625c950fd6fSNamhyung Kim if (psi->flags & PSTORE_FLAGS_CONSOLE) 626ee1d2674SGeliang Tang pstore_unregister_console(); 627c950fd6fSNamhyung Kim if (psi->flags & PSTORE_FLAGS_DMESG) 628ee1d2674SGeliang Tang pstore_unregister_kmsg(); 629ee1d2674SGeliang Tang 630ee1d2674SGeliang Tang free_buf_for_compression(); 631ee1d2674SGeliang Tang 632ee1d2674SGeliang Tang psinfo = NULL; 633ee1d2674SGeliang Tang backend = NULL; 634ee1d2674SGeliang Tang } 635ee1d2674SGeliang Tang EXPORT_SYMBOL_GPL(pstore_unregister); 636ee1d2674SGeliang Tang 637634f8f51SKees Cook static void decompress_record(struct pstore_record *record) 638634f8f51SKees Cook { 639634f8f51SKees Cook int unzipped_len; 6407e8cc8dcSKees Cook char *decompressed; 641634f8f51SKees Cook 6424a16d1cbSAnkit Kumar if (!record->compressed) 6434a16d1cbSAnkit Kumar return; 6444a16d1cbSAnkit Kumar 645634f8f51SKees Cook /* Only PSTORE_TYPE_DMESG support compression. */ 6464a16d1cbSAnkit Kumar if (record->type != PSTORE_TYPE_DMESG) { 647634f8f51SKees Cook pr_warn("ignored compressed record type %d\n", record->type); 648634f8f51SKees Cook return; 649634f8f51SKees Cook } 650634f8f51SKees Cook 651634f8f51SKees Cook /* No compression method has created the common buffer. */ 652634f8f51SKees Cook if (!big_oops_buf) { 653634f8f51SKees Cook pr_warn("no decompression buffer allocated\n"); 654634f8f51SKees Cook return; 655634f8f51SKees Cook } 656634f8f51SKees Cook 657634f8f51SKees Cook unzipped_len = pstore_decompress(record->buf, big_oops_buf, 658634f8f51SKees Cook record->size, big_oops_buf_sz); 6597e8cc8dcSKees Cook if (unzipped_len <= 0) { 6607e8cc8dcSKees Cook pr_err("decompression failed: %d\n", unzipped_len); 6617e8cc8dcSKees Cook return; 6627e8cc8dcSKees Cook } 6637e8cc8dcSKees Cook 6647e8cc8dcSKees Cook /* Build new buffer for decompressed contents. */ 6657e8cc8dcSKees Cook decompressed = kmalloc(unzipped_len + record->ecc_notice_size, 6667e8cc8dcSKees Cook GFP_KERNEL); 6677e8cc8dcSKees Cook if (!decompressed) { 6687e8cc8dcSKees Cook pr_err("decompression ran out of memory\n"); 6697e8cc8dcSKees Cook return; 6707e8cc8dcSKees Cook } 6717e8cc8dcSKees Cook memcpy(decompressed, big_oops_buf, unzipped_len); 6727e8cc8dcSKees Cook 6737e8cc8dcSKees Cook /* Append ECC notice to decompressed buffer. */ 6747e8cc8dcSKees Cook memcpy(decompressed + unzipped_len, record->buf + record->size, 675634f8f51SKees Cook record->ecc_notice_size); 6767e8cc8dcSKees Cook 6777e8cc8dcSKees Cook /* Swap out compresed contents with decompressed contents. */ 678634f8f51SKees Cook kfree(record->buf); 6797e8cc8dcSKees Cook record->buf = decompressed; 680634f8f51SKees Cook record->size = unzipped_len; 681634f8f51SKees Cook record->compressed = false; 682634f8f51SKees Cook } 683634f8f51SKees Cook 684ca01d6ddSTony Luck /* 6853a7d2fd1SKees Cook * Read all the records from one persistent store backend. Create 6866dda9266SLuck, Tony * files in our filesystem. Don't warn about -EEXIST errors 6876dda9266SLuck, Tony * when we are re-scanning the backing store looking to add new 6886dda9266SLuck, Tony * error records. 689ca01d6ddSTony Luck */ 6903a7d2fd1SKees Cook void pstore_get_backend_records(struct pstore_info *psi, 6913a7d2fd1SKees Cook struct dentry *root, int quiet) 692ca01d6ddSTony Luck { 6932a2b0acfSKees Cook int failed = 0; 694656de42eSKees Cook unsigned int stop_loop = 65536; 695ca01d6ddSTony Luck 6963a7d2fd1SKees Cook if (!psi || !root) 697ca01d6ddSTony Luck return; 698ca01d6ddSTony Luck 699f6f82851SKees Cook mutex_lock(&psi->read_mutex); 7002174f6dfSKees Cook if (psi->open && psi->open(psi)) 70106cf91b4SChen Gong goto out; 70206cf91b4SChen Gong 7031dfff7ddSKees Cook /* 7041dfff7ddSKees Cook * Backend callback read() allocates record.buf. decompress_record() 7051dfff7ddSKees Cook * may reallocate record.buf. On success, pstore_mkfile() will keep 7061dfff7ddSKees Cook * the record.buf, so free it only on failure. 7071dfff7ddSKees Cook */ 708656de42eSKees Cook for (; stop_loop; stop_loop--) { 7092a2b0acfSKees Cook struct pstore_record *record; 7102a2b0acfSKees Cook int rc; 7112a2b0acfSKees Cook 7122a2b0acfSKees Cook record = kzalloc(sizeof(*record), GFP_KERNEL); 7132a2b0acfSKees Cook if (!record) { 7142a2b0acfSKees Cook pr_err("out of memory creating record\n"); 7152a2b0acfSKees Cook break; 7162a2b0acfSKees Cook } 717e581ca81SKees Cook pstore_record_init(record, psi); 7182a2b0acfSKees Cook 7192a2b0acfSKees Cook record->size = psi->read(record); 7202a2b0acfSKees Cook 7212a2b0acfSKees Cook /* No more records left in backend? */ 722f6525b96SDouglas Anderson if (record->size <= 0) { 723f6525b96SDouglas Anderson kfree(record); 7242a2b0acfSKees Cook break; 725f6525b96SDouglas Anderson } 7262a2b0acfSKees Cook 7272a2b0acfSKees Cook decompress_record(record); 7283a7d2fd1SKees Cook rc = pstore_mkfile(root, record); 7291dfff7ddSKees Cook if (rc) { 73083f70f07SKees Cook /* pstore_mkfile() did not take record, so free it. */ 7312a2b0acfSKees Cook kfree(record->buf); 73283f70f07SKees Cook kfree(record); 7331dfff7ddSKees Cook if (rc != -EEXIST || !quiet) 7341dfff7ddSKees Cook failed++; 7351dfff7ddSKees Cook } 736ca01d6ddSTony Luck } 7372174f6dfSKees Cook if (psi->close) 73806cf91b4SChen Gong psi->close(psi); 73906cf91b4SChen Gong out: 740f6f82851SKees Cook mutex_unlock(&psi->read_mutex); 741ca01d6ddSTony Luck 742ca01d6ddSTony Luck if (failed) 743656de42eSKees Cook pr_warn("failed to create %d record(s) from '%s'\n", 744ca01d6ddSTony Luck failed, psi->name); 745656de42eSKees Cook if (!stop_loop) 746656de42eSKees Cook pr_err("looping? Too many records seen from '%s'\n", 747656de42eSKees Cook psi->name); 748ca01d6ddSTony Luck } 749ca01d6ddSTony Luck 7506dda9266SLuck, Tony static void pstore_dowork(struct work_struct *work) 7516dda9266SLuck, Tony { 7526dda9266SLuck, Tony pstore_get_records(1); 7536dda9266SLuck, Tony } 7546dda9266SLuck, Tony 75524ed960aSKees Cook static void pstore_timefunc(struct timer_list *unused) 7566dda9266SLuck, Tony { 7576dda9266SLuck, Tony if (pstore_new_entry) { 7586dda9266SLuck, Tony pstore_new_entry = 0; 7596dda9266SLuck, Tony schedule_work(&pstore_work); 7606dda9266SLuck, Tony } 7616dda9266SLuck, Tony 7626330d553SKees Cook if (pstore_update_ms >= 0) 7636330d553SKees Cook mod_timer(&pstore_timer, 7646330d553SKees Cook jiffies + msecs_to_jiffies(pstore_update_ms)); 7656dda9266SLuck, Tony } 7666dda9266SLuck, Tony 767fe1d4758SKees Cook void __init pstore_choose_compression(void) 768fe1d4758SKees Cook { 769fe1d4758SKees Cook const struct pstore_zbackend *step; 770fe1d4758SKees Cook 771fe1d4758SKees Cook if (!compress) 772fe1d4758SKees Cook return; 773fe1d4758SKees Cook 774fe1d4758SKees Cook for (step = zbackends; step->name; step++) { 775fe1d4758SKees Cook if (!strcmp(compress, step->name)) { 776fe1d4758SKees Cook zbackend = step; 777fe1d4758SKees Cook pr_info("using %s compression\n", zbackend->name); 778fe1d4758SKees Cook return; 779fe1d4758SKees Cook } 780fe1d4758SKees Cook } 781fe1d4758SKees Cook } 782fe1d4758SKees Cook 783fe1d4758SKees Cook module_param(compress, charp, 0444); 784fe1d4758SKees Cook MODULE_PARM_DESC(compress, "Pstore compression to use"); 785fe1d4758SKees Cook 786dee28e72SMatthew Garrett module_param(backend, charp, 0444); 787dee28e72SMatthew Garrett MODULE_PARM_DESC(backend, "Pstore backend to use"); 788