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> 318cfc8ddcSGeliang Tang #ifdef CONFIG_PSTORE_ZLIB_COMPRESS 32b0aad7a9SAruna Balakrishnaiah #include <linux/zlib.h> 338cfc8ddcSGeliang Tang #endif 348cfc8ddcSGeliang Tang #ifdef CONFIG_PSTORE_LZO_COMPRESS 358cfc8ddcSGeliang Tang #include <linux/lzo.h> 368cfc8ddcSGeliang Tang #endif 37239b7161SGeliang Tang #if defined(CONFIG_PSTORE_LZ4_COMPRESS) || defined(CONFIG_PSTORE_LZ4HC_COMPRESS) 388cfc8ddcSGeliang Tang #include <linux/lz4.h> 398cfc8ddcSGeliang Tang #endif 40239b7161SGeliang Tang #ifdef CONFIG_PSTORE_842_COMPRESS 41239b7161SGeliang Tang #include <linux/sw842.h> 42239b7161SGeliang Tang #endif 43ca01d6ddSTony Luck #include <linux/string.h> 446dda9266SLuck, Tony #include <linux/timer.h> 45ca01d6ddSTony Luck #include <linux/slab.h> 46ca01d6ddSTony Luck #include <linux/uaccess.h> 47a3f5f075SAnton Vorontsov #include <linux/jiffies.h> 486dda9266SLuck, Tony #include <linux/workqueue.h> 49ca01d6ddSTony Luck 50ca01d6ddSTony Luck #include "internal.h" 51ca01d6ddSTony Luck 52ca01d6ddSTony Luck /* 536dda9266SLuck, Tony * We defer making "oops" entries appear in pstore - see 546dda9266SLuck, Tony * whether the system is actually still running well enough 556dda9266SLuck, Tony * to let someone see the entry 566dda9266SLuck, Tony */ 57521f7288SAnton Vorontsov static int pstore_update_ms = -1; 58a3f5f075SAnton Vorontsov module_param_named(update_ms, pstore_update_ms, int, 0600); 59a3f5f075SAnton Vorontsov MODULE_PARM_DESC(update_ms, "milliseconds before pstore updates its content " 60521f7288SAnton Vorontsov "(default is -1, which means runtime updates are disabled; " 61521f7288SAnton Vorontsov "enabling this option is not safe, it may lead to further " 62521f7288SAnton Vorontsov "corruption on Oopses)"); 636dda9266SLuck, Tony 646dda9266SLuck, Tony static int pstore_new_entry; 656dda9266SLuck, Tony 6624ed960aSKees Cook static void pstore_timefunc(struct timer_list *); 671d27e3e2SKees Cook static DEFINE_TIMER(pstore_timer, pstore_timefunc); 686dda9266SLuck, Tony 696dda9266SLuck, Tony static void pstore_dowork(struct work_struct *); 706dda9266SLuck, Tony static DECLARE_WORK(pstore_work, pstore_dowork); 716dda9266SLuck, Tony 726dda9266SLuck, Tony /* 73ca01d6ddSTony Luck * pstore_lock just protects "psinfo" during 74ca01d6ddSTony Luck * calls to pstore_register() 75ca01d6ddSTony Luck */ 76ca01d6ddSTony Luck static DEFINE_SPINLOCK(pstore_lock); 77060287b8SAnton Vorontsov struct pstore_info *psinfo; 78ca01d6ddSTony Luck 79dee28e72SMatthew Garrett static char *backend; 80dee28e72SMatthew Garrett 81b0aad7a9SAruna Balakrishnaiah /* Compression parameters */ 828cfc8ddcSGeliang Tang #ifdef CONFIG_PSTORE_ZLIB_COMPRESS 83b0aad7a9SAruna Balakrishnaiah #define COMPR_LEVEL 6 84b0aad7a9SAruna Balakrishnaiah #define WINDOW_BITS 12 85b0aad7a9SAruna Balakrishnaiah #define MEM_LEVEL 4 86b0aad7a9SAruna Balakrishnaiah static struct z_stream_s stream; 878cfc8ddcSGeliang Tang #else 888cfc8ddcSGeliang Tang static unsigned char *workspace; 898cfc8ddcSGeliang Tang #endif 908cfc8ddcSGeliang Tang 918cfc8ddcSGeliang Tang struct pstore_zbackend { 928cfc8ddcSGeliang Tang int (*compress)(const void *in, void *out, size_t inlen, size_t outlen); 938cfc8ddcSGeliang Tang int (*decompress)(void *in, void *out, size_t inlen, size_t outlen); 948cfc8ddcSGeliang Tang void (*allocate)(void); 958cfc8ddcSGeliang Tang void (*free)(void); 968cfc8ddcSGeliang Tang 978cfc8ddcSGeliang Tang const char *name; 988cfc8ddcSGeliang Tang }; 99b0aad7a9SAruna Balakrishnaiah 100b0aad7a9SAruna Balakrishnaiah static char *big_oops_buf; 101b0aad7a9SAruna Balakrishnaiah static size_t big_oops_buf_sz; 102b0aad7a9SAruna Balakrishnaiah 103366f7e7aSLuck, Tony /* How much of the console log to snapshot */ 104349d7438SDavid Howells unsigned long kmsg_bytes = PSTORE_DEFAULT_KMSG_BYTES; 105ca01d6ddSTony Luck 106366f7e7aSLuck, Tony void pstore_set_kmsg_bytes(int bytes) 107ca01d6ddSTony Luck { 108366f7e7aSLuck, Tony kmsg_bytes = bytes; 109ca01d6ddSTony Luck } 110ca01d6ddSTony Luck 111ca01d6ddSTony Luck /* Tag each group of saved records with a sequence number */ 112ca01d6ddSTony Luck static int oopscount; 113ca01d6ddSTony Luck 114381b872cSSeiji Aguchi static const char *get_reason_str(enum kmsg_dump_reason reason) 115381b872cSSeiji Aguchi { 116381b872cSSeiji Aguchi switch (reason) { 117381b872cSSeiji Aguchi case KMSG_DUMP_PANIC: 118381b872cSSeiji Aguchi return "Panic"; 119381b872cSSeiji Aguchi case KMSG_DUMP_OOPS: 120381b872cSSeiji Aguchi return "Oops"; 121381b872cSSeiji Aguchi case KMSG_DUMP_EMERG: 122381b872cSSeiji Aguchi return "Emergency"; 123381b872cSSeiji Aguchi case KMSG_DUMP_RESTART: 124381b872cSSeiji Aguchi return "Restart"; 125381b872cSSeiji Aguchi case KMSG_DUMP_HALT: 126381b872cSSeiji Aguchi return "Halt"; 127381b872cSSeiji Aguchi case KMSG_DUMP_POWEROFF: 128381b872cSSeiji Aguchi return "Poweroff"; 129381b872cSSeiji Aguchi default: 130381b872cSSeiji Aguchi return "Unknown"; 131381b872cSSeiji Aguchi } 132381b872cSSeiji Aguchi } 1339f6af27fSTony Luck 1349f244e9cSSeiji Aguchi bool pstore_cannot_block_path(enum kmsg_dump_reason reason) 1359f244e9cSSeiji Aguchi { 1369f244e9cSSeiji Aguchi /* 1379f244e9cSSeiji Aguchi * In case of NMI path, pstore shouldn't be blocked 1389f244e9cSSeiji Aguchi * regardless of reason. 1399f244e9cSSeiji Aguchi */ 1409f244e9cSSeiji Aguchi if (in_nmi()) 1419f244e9cSSeiji Aguchi return true; 1429f244e9cSSeiji Aguchi 1439f244e9cSSeiji Aguchi switch (reason) { 1449f244e9cSSeiji Aguchi /* In panic case, other cpus are stopped by smp_send_stop(). */ 1459f244e9cSSeiji Aguchi case KMSG_DUMP_PANIC: 1469f244e9cSSeiji Aguchi /* Emergency restart shouldn't be blocked by spin lock. */ 1479f244e9cSSeiji Aguchi case KMSG_DUMP_EMERG: 1489f244e9cSSeiji Aguchi return true; 1499f244e9cSSeiji Aguchi default: 1509f244e9cSSeiji Aguchi return false; 1519f244e9cSSeiji Aguchi } 1529f244e9cSSeiji Aguchi } 1539f244e9cSSeiji Aguchi EXPORT_SYMBOL_GPL(pstore_cannot_block_path); 1549f244e9cSSeiji Aguchi 1558cfc8ddcSGeliang Tang #ifdef CONFIG_PSTORE_ZLIB_COMPRESS 156b0aad7a9SAruna Balakrishnaiah /* Derived from logfs_compress() */ 1578cfc8ddcSGeliang Tang static int compress_zlib(const void *in, void *out, size_t inlen, size_t outlen) 158b0aad7a9SAruna Balakrishnaiah { 159b0aad7a9SAruna Balakrishnaiah int err, ret; 160b0aad7a9SAruna Balakrishnaiah 161b0aad7a9SAruna Balakrishnaiah ret = -EIO; 162b0aad7a9SAruna Balakrishnaiah err = zlib_deflateInit2(&stream, COMPR_LEVEL, Z_DEFLATED, WINDOW_BITS, 163b0aad7a9SAruna Balakrishnaiah MEM_LEVEL, Z_DEFAULT_STRATEGY); 164b0aad7a9SAruna Balakrishnaiah if (err != Z_OK) 165b0aad7a9SAruna Balakrishnaiah goto error; 166b0aad7a9SAruna Balakrishnaiah 167b0aad7a9SAruna Balakrishnaiah stream.next_in = in; 168b0aad7a9SAruna Balakrishnaiah stream.avail_in = inlen; 169b0aad7a9SAruna Balakrishnaiah stream.total_in = 0; 170b0aad7a9SAruna Balakrishnaiah stream.next_out = out; 171b0aad7a9SAruna Balakrishnaiah stream.avail_out = outlen; 172b0aad7a9SAruna Balakrishnaiah stream.total_out = 0; 173b0aad7a9SAruna Balakrishnaiah 174b0aad7a9SAruna Balakrishnaiah err = zlib_deflate(&stream, Z_FINISH); 175b0aad7a9SAruna Balakrishnaiah if (err != Z_STREAM_END) 176b0aad7a9SAruna Balakrishnaiah goto error; 177b0aad7a9SAruna Balakrishnaiah 178b0aad7a9SAruna Balakrishnaiah err = zlib_deflateEnd(&stream); 179b0aad7a9SAruna Balakrishnaiah if (err != Z_OK) 180b0aad7a9SAruna Balakrishnaiah goto error; 181b0aad7a9SAruna Balakrishnaiah 182b0aad7a9SAruna Balakrishnaiah if (stream.total_out >= stream.total_in) 183b0aad7a9SAruna Balakrishnaiah goto error; 184b0aad7a9SAruna Balakrishnaiah 185b0aad7a9SAruna Balakrishnaiah ret = stream.total_out; 186b0aad7a9SAruna Balakrishnaiah error: 187b0aad7a9SAruna Balakrishnaiah return ret; 188b0aad7a9SAruna Balakrishnaiah } 189b0aad7a9SAruna Balakrishnaiah 190adb42f5eSAruna Balakrishnaiah /* Derived from logfs_uncompress */ 1918cfc8ddcSGeliang Tang static int decompress_zlib(void *in, void *out, size_t inlen, size_t outlen) 192adb42f5eSAruna Balakrishnaiah { 193adb42f5eSAruna Balakrishnaiah int err, ret; 194adb42f5eSAruna Balakrishnaiah 195adb42f5eSAruna Balakrishnaiah ret = -EIO; 196b61edf8eSAruna Balakrishnaiah err = zlib_inflateInit2(&stream, WINDOW_BITS); 197adb42f5eSAruna Balakrishnaiah if (err != Z_OK) 198adb42f5eSAruna Balakrishnaiah goto error; 199adb42f5eSAruna Balakrishnaiah 200adb42f5eSAruna Balakrishnaiah stream.next_in = in; 201adb42f5eSAruna Balakrishnaiah stream.avail_in = inlen; 202adb42f5eSAruna Balakrishnaiah stream.total_in = 0; 203adb42f5eSAruna Balakrishnaiah stream.next_out = out; 204adb42f5eSAruna Balakrishnaiah stream.avail_out = outlen; 205adb42f5eSAruna Balakrishnaiah stream.total_out = 0; 206adb42f5eSAruna Balakrishnaiah 207adb42f5eSAruna Balakrishnaiah err = zlib_inflate(&stream, Z_FINISH); 208adb42f5eSAruna Balakrishnaiah if (err != Z_STREAM_END) 209adb42f5eSAruna Balakrishnaiah goto error; 210adb42f5eSAruna Balakrishnaiah 211adb42f5eSAruna Balakrishnaiah err = zlib_inflateEnd(&stream); 212adb42f5eSAruna Balakrishnaiah if (err != Z_OK) 213adb42f5eSAruna Balakrishnaiah goto error; 214adb42f5eSAruna Balakrishnaiah 215adb42f5eSAruna Balakrishnaiah ret = stream.total_out; 216adb42f5eSAruna Balakrishnaiah error: 217adb42f5eSAruna Balakrishnaiah return ret; 218adb42f5eSAruna Balakrishnaiah } 219adb42f5eSAruna Balakrishnaiah 2208cfc8ddcSGeliang Tang static void allocate_zlib(void) 221b0aad7a9SAruna Balakrishnaiah { 222b0aad7a9SAruna Balakrishnaiah size_t size; 2237de8fe2fSAruna Balakrishnaiah size_t cmpr; 224b0aad7a9SAruna Balakrishnaiah 2257de8fe2fSAruna Balakrishnaiah switch (psinfo->bufsize) { 2267de8fe2fSAruna Balakrishnaiah /* buffer range for efivars */ 2277de8fe2fSAruna Balakrishnaiah case 1000 ... 2000: 2287de8fe2fSAruna Balakrishnaiah cmpr = 56; 2297de8fe2fSAruna Balakrishnaiah break; 2307de8fe2fSAruna Balakrishnaiah case 2001 ... 3000: 2317de8fe2fSAruna Balakrishnaiah cmpr = 54; 2327de8fe2fSAruna Balakrishnaiah break; 2337de8fe2fSAruna Balakrishnaiah case 3001 ... 3999: 2347de8fe2fSAruna Balakrishnaiah cmpr = 52; 2357de8fe2fSAruna Balakrishnaiah break; 2367de8fe2fSAruna Balakrishnaiah /* buffer range for nvram, erst */ 2377de8fe2fSAruna Balakrishnaiah case 4000 ... 10000: 2387de8fe2fSAruna Balakrishnaiah cmpr = 45; 2397de8fe2fSAruna Balakrishnaiah break; 2407de8fe2fSAruna Balakrishnaiah default: 2417de8fe2fSAruna Balakrishnaiah cmpr = 60; 2427de8fe2fSAruna Balakrishnaiah break; 2437de8fe2fSAruna Balakrishnaiah } 2447de8fe2fSAruna Balakrishnaiah 2457de8fe2fSAruna Balakrishnaiah big_oops_buf_sz = (psinfo->bufsize * 100) / cmpr; 246b0aad7a9SAruna Balakrishnaiah big_oops_buf = kmalloc(big_oops_buf_sz, GFP_KERNEL); 247b0aad7a9SAruna Balakrishnaiah if (big_oops_buf) { 248b0aad7a9SAruna Balakrishnaiah size = max(zlib_deflate_workspacesize(WINDOW_BITS, MEM_LEVEL), 249b0aad7a9SAruna Balakrishnaiah zlib_inflate_workspacesize()); 250b0aad7a9SAruna Balakrishnaiah stream.workspace = kmalloc(size, GFP_KERNEL); 251b0aad7a9SAruna Balakrishnaiah if (!stream.workspace) { 252ef748853SFabian Frederick pr_err("No memory for compression workspace; skipping compression\n"); 253b0aad7a9SAruna Balakrishnaiah kfree(big_oops_buf); 254b0aad7a9SAruna Balakrishnaiah big_oops_buf = NULL; 255b0aad7a9SAruna Balakrishnaiah } 256b0aad7a9SAruna Balakrishnaiah } else { 257ef748853SFabian Frederick pr_err("No memory for uncompressed data; skipping compression\n"); 258b0aad7a9SAruna Balakrishnaiah stream.workspace = NULL; 259b0aad7a9SAruna Balakrishnaiah } 260b0aad7a9SAruna Balakrishnaiah 261b0aad7a9SAruna Balakrishnaiah } 262b0aad7a9SAruna Balakrishnaiah 2638cfc8ddcSGeliang Tang static void free_zlib(void) 264ee1d2674SGeliang Tang { 265ee1d2674SGeliang Tang kfree(stream.workspace); 266ee1d2674SGeliang Tang stream.workspace = NULL; 267ee1d2674SGeliang Tang kfree(big_oops_buf); 268ee1d2674SGeliang Tang big_oops_buf = NULL; 2698cfc8ddcSGeliang Tang big_oops_buf_sz = 0; 2708cfc8ddcSGeliang Tang } 2718cfc8ddcSGeliang Tang 2723faf9354SBhumika Goyal static const struct pstore_zbackend backend_zlib = { 2738cfc8ddcSGeliang Tang .compress = compress_zlib, 2748cfc8ddcSGeliang Tang .decompress = decompress_zlib, 2758cfc8ddcSGeliang Tang .allocate = allocate_zlib, 2768cfc8ddcSGeliang Tang .free = free_zlib, 2778cfc8ddcSGeliang Tang .name = "zlib", 2788cfc8ddcSGeliang Tang }; 2798cfc8ddcSGeliang Tang #endif 2808cfc8ddcSGeliang Tang 2818cfc8ddcSGeliang Tang #ifdef CONFIG_PSTORE_LZO_COMPRESS 2828cfc8ddcSGeliang Tang static int compress_lzo(const void *in, void *out, size_t inlen, size_t outlen) 2838cfc8ddcSGeliang Tang { 2848cfc8ddcSGeliang Tang int ret; 2858cfc8ddcSGeliang Tang 2868cfc8ddcSGeliang Tang ret = lzo1x_1_compress(in, inlen, out, &outlen, workspace); 2878cfc8ddcSGeliang Tang if (ret != LZO_E_OK) { 2888cfc8ddcSGeliang Tang pr_err("lzo_compress error, ret = %d!\n", ret); 2898cfc8ddcSGeliang Tang return -EIO; 2908cfc8ddcSGeliang Tang } 2918cfc8ddcSGeliang Tang 2928cfc8ddcSGeliang Tang return outlen; 2938cfc8ddcSGeliang Tang } 2948cfc8ddcSGeliang Tang 2958cfc8ddcSGeliang Tang static int decompress_lzo(void *in, void *out, size_t inlen, size_t outlen) 2968cfc8ddcSGeliang Tang { 2978cfc8ddcSGeliang Tang int ret; 2988cfc8ddcSGeliang Tang 2998cfc8ddcSGeliang Tang ret = lzo1x_decompress_safe(in, inlen, out, &outlen); 3008cfc8ddcSGeliang Tang if (ret != LZO_E_OK) { 3018cfc8ddcSGeliang Tang pr_err("lzo_decompress error, ret = %d!\n", ret); 3028cfc8ddcSGeliang Tang return -EIO; 3038cfc8ddcSGeliang Tang } 3048cfc8ddcSGeliang Tang 3058cfc8ddcSGeliang Tang return outlen; 3068cfc8ddcSGeliang Tang } 3078cfc8ddcSGeliang Tang 3088cfc8ddcSGeliang Tang static void allocate_lzo(void) 3098cfc8ddcSGeliang Tang { 3108cfc8ddcSGeliang Tang big_oops_buf_sz = lzo1x_worst_compress(psinfo->bufsize); 3118cfc8ddcSGeliang Tang big_oops_buf = kmalloc(big_oops_buf_sz, GFP_KERNEL); 3128cfc8ddcSGeliang Tang if (big_oops_buf) { 3138cfc8ddcSGeliang Tang workspace = kmalloc(LZO1X_MEM_COMPRESS, GFP_KERNEL); 3148cfc8ddcSGeliang Tang if (!workspace) { 3158cfc8ddcSGeliang Tang pr_err("No memory for compression workspace; skipping compression\n"); 3168cfc8ddcSGeliang Tang kfree(big_oops_buf); 3178cfc8ddcSGeliang Tang big_oops_buf = NULL; 3188cfc8ddcSGeliang Tang } 3198cfc8ddcSGeliang Tang } else { 3208cfc8ddcSGeliang Tang pr_err("No memory for uncompressed data; skipping compression\n"); 3218cfc8ddcSGeliang Tang workspace = NULL; 3228cfc8ddcSGeliang Tang } 3238cfc8ddcSGeliang Tang } 3248cfc8ddcSGeliang Tang 3258cfc8ddcSGeliang Tang static void free_lzo(void) 3268cfc8ddcSGeliang Tang { 3278cfc8ddcSGeliang Tang kfree(workspace); 3288cfc8ddcSGeliang Tang kfree(big_oops_buf); 3298cfc8ddcSGeliang Tang big_oops_buf = NULL; 3308cfc8ddcSGeliang Tang big_oops_buf_sz = 0; 3318cfc8ddcSGeliang Tang } 3328cfc8ddcSGeliang Tang 3333faf9354SBhumika Goyal static const struct pstore_zbackend backend_lzo = { 3348cfc8ddcSGeliang Tang .compress = compress_lzo, 3358cfc8ddcSGeliang Tang .decompress = decompress_lzo, 3368cfc8ddcSGeliang Tang .allocate = allocate_lzo, 3378cfc8ddcSGeliang Tang .free = free_lzo, 3388cfc8ddcSGeliang Tang .name = "lzo", 3398cfc8ddcSGeliang Tang }; 3408cfc8ddcSGeliang Tang #endif 3418cfc8ddcSGeliang Tang 342239b7161SGeliang Tang #if defined(CONFIG_PSTORE_LZ4_COMPRESS) || defined(CONFIG_PSTORE_LZ4HC_COMPRESS) 3438cfc8ddcSGeliang Tang static int decompress_lz4(void *in, void *out, size_t inlen, size_t outlen) 3448cfc8ddcSGeliang Tang { 3458cfc8ddcSGeliang Tang int ret; 3468cfc8ddcSGeliang Tang 347d21b5ff1SSven Schmidt ret = LZ4_decompress_safe(in, out, inlen, outlen); 348d21b5ff1SSven Schmidt if (ret < 0) { 349d21b5ff1SSven Schmidt /* 350d21b5ff1SSven Schmidt * LZ4_decompress_safe will return an error code 351d21b5ff1SSven Schmidt * (< 0) if decompression failed 352d21b5ff1SSven Schmidt */ 353d21b5ff1SSven Schmidt pr_err("LZ4_decompress_safe error, ret = %d!\n", ret); 3548cfc8ddcSGeliang Tang return -EIO; 3558cfc8ddcSGeliang Tang } 3568cfc8ddcSGeliang Tang 357d21b5ff1SSven Schmidt return ret; 3588cfc8ddcSGeliang Tang } 3598cfc8ddcSGeliang Tang 360239b7161SGeliang Tang static void free_lz4(void) 361239b7161SGeliang Tang { 362239b7161SGeliang Tang kfree(workspace); 363239b7161SGeliang Tang kfree(big_oops_buf); 364239b7161SGeliang Tang big_oops_buf = NULL; 365239b7161SGeliang Tang big_oops_buf_sz = 0; 366239b7161SGeliang Tang } 367239b7161SGeliang Tang #endif 368239b7161SGeliang Tang 369239b7161SGeliang Tang #ifdef CONFIG_PSTORE_LZ4_COMPRESS 370239b7161SGeliang Tang static int compress_lz4(const void *in, void *out, size_t inlen, size_t outlen) 371239b7161SGeliang Tang { 372239b7161SGeliang Tang int ret; 373239b7161SGeliang Tang 374239b7161SGeliang Tang ret = LZ4_compress_default(in, out, inlen, outlen, workspace); 375239b7161SGeliang Tang if (!ret) { 376239b7161SGeliang Tang pr_err("LZ4_compress_default error; compression failed!\n"); 377239b7161SGeliang Tang return -EIO; 378239b7161SGeliang Tang } 379239b7161SGeliang Tang 380239b7161SGeliang Tang return ret; 381239b7161SGeliang Tang } 382239b7161SGeliang Tang 3838cfc8ddcSGeliang Tang static void allocate_lz4(void) 3848cfc8ddcSGeliang Tang { 385d21b5ff1SSven Schmidt big_oops_buf_sz = LZ4_compressBound(psinfo->bufsize); 3868cfc8ddcSGeliang Tang big_oops_buf = kmalloc(big_oops_buf_sz, GFP_KERNEL); 3878cfc8ddcSGeliang Tang if (big_oops_buf) { 3888cfc8ddcSGeliang Tang workspace = kmalloc(LZ4_MEM_COMPRESS, GFP_KERNEL); 3898cfc8ddcSGeliang Tang if (!workspace) { 3908cfc8ddcSGeliang Tang pr_err("No memory for compression workspace; skipping compression\n"); 3918cfc8ddcSGeliang Tang kfree(big_oops_buf); 3928cfc8ddcSGeliang Tang big_oops_buf = NULL; 3938cfc8ddcSGeliang Tang } 3948cfc8ddcSGeliang Tang } else { 3958cfc8ddcSGeliang Tang pr_err("No memory for uncompressed data; skipping compression\n"); 3968cfc8ddcSGeliang Tang workspace = NULL; 3978cfc8ddcSGeliang Tang } 3988cfc8ddcSGeliang Tang } 3998cfc8ddcSGeliang Tang 400239b7161SGeliang Tang static const struct pstore_zbackend backend_lz4 = { 401239b7161SGeliang Tang .compress = compress_lz4, 402239b7161SGeliang Tang .decompress = decompress_lz4, 403239b7161SGeliang Tang .allocate = allocate_lz4, 404239b7161SGeliang Tang .free = free_lz4, 405239b7161SGeliang Tang .name = "lz4", 406239b7161SGeliang Tang }; 407239b7161SGeliang Tang #endif 408239b7161SGeliang Tang 409239b7161SGeliang Tang #ifdef CONFIG_PSTORE_LZ4HC_COMPRESS 410239b7161SGeliang Tang static int compress_lz4hc(const void *in, void *out, 411239b7161SGeliang Tang size_t inlen, size_t outlen) 412239b7161SGeliang Tang { 413239b7161SGeliang Tang int ret; 414239b7161SGeliang Tang 415239b7161SGeliang Tang ret = LZ4_compress_HC(in, out, inlen, outlen, 416239b7161SGeliang Tang LZ4HC_DEFAULT_CLEVEL, workspace); 417239b7161SGeliang Tang if (!ret) { 418239b7161SGeliang Tang pr_err("LZ4_compress_HC error; compression failed!\n"); 419239b7161SGeliang Tang return -EIO; 420239b7161SGeliang Tang } 421239b7161SGeliang Tang 422239b7161SGeliang Tang return ret; 423239b7161SGeliang Tang } 424239b7161SGeliang Tang 425239b7161SGeliang Tang static void allocate_lz4hc(void) 426239b7161SGeliang Tang { 427239b7161SGeliang Tang big_oops_buf_sz = LZ4_compressBound(psinfo->bufsize); 428239b7161SGeliang Tang big_oops_buf = kmalloc(big_oops_buf_sz, GFP_KERNEL); 429239b7161SGeliang Tang if (big_oops_buf) { 430239b7161SGeliang Tang workspace = kmalloc(LZ4HC_MEM_COMPRESS, GFP_KERNEL); 431239b7161SGeliang Tang if (!workspace) { 432239b7161SGeliang Tang pr_err("No memory for compression workspace; skipping compression\n"); 433239b7161SGeliang Tang kfree(big_oops_buf); 434239b7161SGeliang Tang big_oops_buf = NULL; 435239b7161SGeliang Tang } 436239b7161SGeliang Tang } else { 437239b7161SGeliang Tang pr_err("No memory for uncompressed data; skipping compression\n"); 438239b7161SGeliang Tang workspace = NULL; 439239b7161SGeliang Tang } 440239b7161SGeliang Tang } 441239b7161SGeliang Tang 442239b7161SGeliang Tang static const struct pstore_zbackend backend_lz4hc = { 443239b7161SGeliang Tang .compress = compress_lz4hc, 444239b7161SGeliang Tang .decompress = decompress_lz4, 445239b7161SGeliang Tang .allocate = allocate_lz4hc, 446239b7161SGeliang Tang .free = free_lz4, 447239b7161SGeliang Tang .name = "lz4hc", 448239b7161SGeliang Tang }; 449239b7161SGeliang Tang #endif 450239b7161SGeliang Tang 451239b7161SGeliang Tang #ifdef CONFIG_PSTORE_842_COMPRESS 452239b7161SGeliang Tang static int compress_842(const void *in, void *out, size_t inlen, size_t outlen) 453239b7161SGeliang Tang { 454239b7161SGeliang Tang int ret; 455*55597406SKees Cook unsigned int size; 456239b7161SGeliang Tang 457*55597406SKees Cook if (outlen > UINT_MAX) 458*55597406SKees Cook return -EIO; 459*55597406SKees Cook size = outlen; 460*55597406SKees Cook 461*55597406SKees Cook ret = sw842_compress(in, inlen, out, &size, workspace); 462239b7161SGeliang Tang if (ret) { 463239b7161SGeliang Tang pr_err("sw842_compress error; compression failed!\n"); 464239b7161SGeliang Tang return ret; 465239b7161SGeliang Tang } 466239b7161SGeliang Tang 467*55597406SKees Cook return size; 468239b7161SGeliang Tang } 469239b7161SGeliang Tang 470239b7161SGeliang Tang static int decompress_842(void *in, void *out, size_t inlen, size_t outlen) 471239b7161SGeliang Tang { 472239b7161SGeliang Tang int ret; 473*55597406SKees Cook unsigned int size; 474239b7161SGeliang Tang 475*55597406SKees Cook if (outlen > UINT_MAX) 476*55597406SKees Cook return -EIO; 477*55597406SKees Cook size = outlen; 478*55597406SKees Cook 479*55597406SKees Cook ret = sw842_decompress(in, inlen, out, &size); 480239b7161SGeliang Tang if (ret) { 481239b7161SGeliang Tang pr_err("sw842_decompress error, ret = %d!\n", ret); 482239b7161SGeliang Tang return ret; 483239b7161SGeliang Tang } 484239b7161SGeliang Tang 485*55597406SKees Cook return size; 486239b7161SGeliang Tang } 487239b7161SGeliang Tang 488239b7161SGeliang Tang static void allocate_842(void) 489239b7161SGeliang Tang { 490239b7161SGeliang Tang big_oops_buf_sz = psinfo->bufsize; 491239b7161SGeliang Tang big_oops_buf = kmalloc(big_oops_buf_sz, GFP_KERNEL); 492239b7161SGeliang Tang if (big_oops_buf) { 493239b7161SGeliang Tang workspace = kmalloc(SW842_MEM_COMPRESS, GFP_KERNEL); 494239b7161SGeliang Tang if (!workspace) { 495239b7161SGeliang Tang kfree(big_oops_buf); 496239b7161SGeliang Tang big_oops_buf = NULL; 497239b7161SGeliang Tang } 498239b7161SGeliang Tang } else { 499239b7161SGeliang Tang pr_err("No memory for uncompressed data; skipping compression\n"); 500239b7161SGeliang Tang workspace = NULL; 501239b7161SGeliang Tang } 502239b7161SGeliang Tang } 503239b7161SGeliang Tang 504239b7161SGeliang Tang static void free_842(void) 5058cfc8ddcSGeliang Tang { 5068cfc8ddcSGeliang Tang kfree(workspace); 5078cfc8ddcSGeliang Tang kfree(big_oops_buf); 5088cfc8ddcSGeliang Tang big_oops_buf = NULL; 5098cfc8ddcSGeliang Tang big_oops_buf_sz = 0; 5108cfc8ddcSGeliang Tang } 5118cfc8ddcSGeliang Tang 512239b7161SGeliang Tang static const struct pstore_zbackend backend_842 = { 513239b7161SGeliang Tang .compress = compress_842, 514239b7161SGeliang Tang .decompress = decompress_842, 515239b7161SGeliang Tang .allocate = allocate_842, 516239b7161SGeliang Tang .free = free_842, 517239b7161SGeliang Tang .name = "842", 5188cfc8ddcSGeliang Tang }; 5198cfc8ddcSGeliang Tang #endif 5208cfc8ddcSGeliang Tang 5213faf9354SBhumika Goyal static const struct pstore_zbackend *zbackend = 5228cfc8ddcSGeliang Tang #if defined(CONFIG_PSTORE_ZLIB_COMPRESS) 5238cfc8ddcSGeliang Tang &backend_zlib; 5248cfc8ddcSGeliang Tang #elif defined(CONFIG_PSTORE_LZO_COMPRESS) 5258cfc8ddcSGeliang Tang &backend_lzo; 5268cfc8ddcSGeliang Tang #elif defined(CONFIG_PSTORE_LZ4_COMPRESS) 5278cfc8ddcSGeliang Tang &backend_lz4; 528239b7161SGeliang Tang #elif defined(CONFIG_PSTORE_LZ4HC_COMPRESS) 529239b7161SGeliang Tang &backend_lz4hc; 530239b7161SGeliang Tang #elif defined(CONFIG_PSTORE_842_COMPRESS) 531239b7161SGeliang Tang &backend_842; 5328cfc8ddcSGeliang Tang #else 5338cfc8ddcSGeliang Tang NULL; 5348cfc8ddcSGeliang Tang #endif 5358cfc8ddcSGeliang Tang 5368cfc8ddcSGeliang Tang static int pstore_compress(const void *in, void *out, 5378cfc8ddcSGeliang Tang size_t inlen, size_t outlen) 5388cfc8ddcSGeliang Tang { 5398cfc8ddcSGeliang Tang if (zbackend) 5408cfc8ddcSGeliang Tang return zbackend->compress(in, out, inlen, outlen); 5418cfc8ddcSGeliang Tang else 5428cfc8ddcSGeliang Tang return -EIO; 5438cfc8ddcSGeliang Tang } 5448cfc8ddcSGeliang Tang 5458cfc8ddcSGeliang Tang static int pstore_decompress(void *in, void *out, size_t inlen, size_t outlen) 5468cfc8ddcSGeliang Tang { 5478cfc8ddcSGeliang Tang if (zbackend) 5488cfc8ddcSGeliang Tang return zbackend->decompress(in, out, inlen, outlen); 5498cfc8ddcSGeliang Tang else 5508cfc8ddcSGeliang Tang return -EIO; 5518cfc8ddcSGeliang Tang } 5528cfc8ddcSGeliang Tang 5538cfc8ddcSGeliang Tang static void allocate_buf_for_compression(void) 5548cfc8ddcSGeliang Tang { 5558cfc8ddcSGeliang Tang if (zbackend) { 5568cfc8ddcSGeliang Tang pr_info("using %s compression\n", zbackend->name); 5578cfc8ddcSGeliang Tang zbackend->allocate(); 5588cfc8ddcSGeliang Tang } else { 5598cfc8ddcSGeliang Tang pr_err("allocate compression buffer error!\n"); 5608cfc8ddcSGeliang Tang } 5618cfc8ddcSGeliang Tang } 5628cfc8ddcSGeliang Tang 5638cfc8ddcSGeliang Tang static void free_buf_for_compression(void) 5648cfc8ddcSGeliang Tang { 5658cfc8ddcSGeliang Tang if (zbackend) 5668cfc8ddcSGeliang Tang zbackend->free(); 5678cfc8ddcSGeliang Tang else 5688cfc8ddcSGeliang Tang pr_err("free compression buffer error!\n"); 569ee1d2674SGeliang Tang } 570ee1d2674SGeliang Tang 571b0aad7a9SAruna Balakrishnaiah /* 572b0aad7a9SAruna Balakrishnaiah * Called when compression fails, since the printk buffer 573b0aad7a9SAruna Balakrishnaiah * would be fetched for compression calling it again when 574b0aad7a9SAruna Balakrishnaiah * compression fails would have moved the iterator of 575b0aad7a9SAruna Balakrishnaiah * printk buffer which results in fetching old contents. 576b0aad7a9SAruna Balakrishnaiah * Copy the recent messages from big_oops_buf to psinfo->buf 577b0aad7a9SAruna Balakrishnaiah */ 578b0aad7a9SAruna Balakrishnaiah static size_t copy_kmsg_to_buffer(int hsize, size_t len) 579b0aad7a9SAruna Balakrishnaiah { 580b0aad7a9SAruna Balakrishnaiah size_t total_len; 581b0aad7a9SAruna Balakrishnaiah size_t diff; 582b0aad7a9SAruna Balakrishnaiah 583b0aad7a9SAruna Balakrishnaiah total_len = hsize + len; 584b0aad7a9SAruna Balakrishnaiah 585b0aad7a9SAruna Balakrishnaiah if (total_len > psinfo->bufsize) { 586b0aad7a9SAruna Balakrishnaiah diff = total_len - psinfo->bufsize + hsize; 587b0aad7a9SAruna Balakrishnaiah memcpy(psinfo->buf, big_oops_buf, hsize); 588b0aad7a9SAruna Balakrishnaiah memcpy(psinfo->buf + hsize, big_oops_buf + diff, 589b0aad7a9SAruna Balakrishnaiah psinfo->bufsize - hsize); 590b0aad7a9SAruna Balakrishnaiah total_len = psinfo->bufsize; 591b0aad7a9SAruna Balakrishnaiah } else 592b0aad7a9SAruna Balakrishnaiah memcpy(psinfo->buf, big_oops_buf, total_len); 593b0aad7a9SAruna Balakrishnaiah 594b0aad7a9SAruna Balakrishnaiah return total_len; 595b0aad7a9SAruna Balakrishnaiah } 596b0aad7a9SAruna Balakrishnaiah 597e581ca81SKees Cook void pstore_record_init(struct pstore_record *record, 598e581ca81SKees Cook struct pstore_info *psinfo) 599e581ca81SKees Cook { 600e581ca81SKees Cook memset(record, 0, sizeof(*record)); 601e581ca81SKees Cook 602e581ca81SKees Cook record->psi = psinfo; 603c7f3c595SKees Cook 604c7f3c595SKees Cook /* Report zeroed timestamp if called before timekeeping has resumed. */ 605df27067eSArnd Bergmann record->time = ns_to_timespec(ktime_get_real_fast_ns()); 606e581ca81SKees Cook } 607e581ca81SKees Cook 608ca01d6ddSTony Luck /* 609ca01d6ddSTony Luck * callback from kmsg_dump. (s2,l2) has the most recently 610ca01d6ddSTony Luck * written bytes, older bytes are in (s1,l1). Save as much 611ca01d6ddSTony Luck * as we can from the end of the buffer. 612ca01d6ddSTony Luck */ 613ca01d6ddSTony Luck static void pstore_dump(struct kmsg_dumper *dumper, 614e2ae715dSKay Sievers enum kmsg_dump_reason reason) 615ca01d6ddSTony Luck { 616e2ae715dSKay Sievers unsigned long total = 0; 617381b872cSSeiji Aguchi const char *why; 618b94fdd07SMatthew Garrett unsigned int part = 1; 619abd4d558SDon Zickus unsigned long flags = 0; 62098e44fdaSNamhyung Kim int is_locked; 621e2ae715dSKay Sievers int ret; 622ca01d6ddSTony Luck 623381b872cSSeiji Aguchi why = get_reason_str(reason); 6249f6af27fSTony Luck 6259f244e9cSSeiji Aguchi if (pstore_cannot_block_path(reason)) { 6269f244e9cSSeiji Aguchi is_locked = spin_trylock_irqsave(&psinfo->buf_lock, flags); 6279f244e9cSSeiji Aguchi if (!is_locked) { 6289f244e9cSSeiji Aguchi pr_err("pstore dump routine blocked in %s path, may corrupt error record\n" 6299f244e9cSSeiji Aguchi , in_nmi() ? "NMI" : why); 630959217c8SLi Pengcheng return; 6319f244e9cSSeiji Aguchi } 63298e44fdaSNamhyung Kim } else { 633abd4d558SDon Zickus spin_lock_irqsave(&psinfo->buf_lock, flags); 63498e44fdaSNamhyung Kim is_locked = 1; 63598e44fdaSNamhyung Kim } 636ca01d6ddSTony Luck oopscount++; 637ca01d6ddSTony Luck while (total < kmsg_bytes) { 638e2ae715dSKay Sievers char *dst; 63976cc9580SKees Cook size_t dst_size; 64076cc9580SKees Cook int header_size; 641b0aad7a9SAruna Balakrishnaiah int zipped_len = -1; 64276cc9580SKees Cook size_t dump_size; 643e581ca81SKees Cook struct pstore_record record; 644e581ca81SKees Cook 645e581ca81SKees Cook pstore_record_init(&record, psinfo); 646e581ca81SKees Cook record.type = PSTORE_TYPE_DMESG; 647e581ca81SKees Cook record.count = oopscount; 648e581ca81SKees Cook record.reason = reason; 649e581ca81SKees Cook record.part = part; 650e581ca81SKees Cook record.buf = psinfo->buf; 651e2ae715dSKay Sievers 652f0e2efcfSKonstantin Khlebnikov if (big_oops_buf && is_locked) { 653b0aad7a9SAruna Balakrishnaiah dst = big_oops_buf; 65476cc9580SKees Cook dst_size = big_oops_buf_sz; 655235f6d15SNamhyung Kim } else { 656235f6d15SNamhyung Kim dst = psinfo->buf; 65776cc9580SKees Cook dst_size = psinfo->bufsize; 658235f6d15SNamhyung Kim } 659235f6d15SNamhyung Kim 66076cc9580SKees Cook /* Write dump header. */ 66176cc9580SKees Cook header_size = snprintf(dst, dst_size, "%s#%d Part%u\n", why, 66276cc9580SKees Cook oopscount, part); 66376cc9580SKees Cook dst_size -= header_size; 664b0aad7a9SAruna Balakrishnaiah 66576cc9580SKees Cook /* Write dump contents. */ 66676cc9580SKees Cook if (!kmsg_dump_get_buffer(dumper, true, dst + header_size, 66776cc9580SKees Cook dst_size, &dump_size)) 668b0aad7a9SAruna Balakrishnaiah break; 669b0aad7a9SAruna Balakrishnaiah 670235f6d15SNamhyung Kim if (big_oops_buf && is_locked) { 671b0aad7a9SAruna Balakrishnaiah zipped_len = pstore_compress(dst, psinfo->buf, 67276cc9580SKees Cook header_size + dump_size, 67376cc9580SKees Cook psinfo->bufsize); 674b0aad7a9SAruna Balakrishnaiah 675b0aad7a9SAruna Balakrishnaiah if (zipped_len > 0) { 67676cc9580SKees Cook record.compressed = true; 67776cc9580SKees Cook record.size = zipped_len; 678b0aad7a9SAruna Balakrishnaiah } else { 67976cc9580SKees Cook record.size = copy_kmsg_to_buffer(header_size, 68076cc9580SKees Cook dump_size); 681b0aad7a9SAruna Balakrishnaiah } 682b0aad7a9SAruna Balakrishnaiah } else { 68376cc9580SKees Cook record.size = header_size + dump_size; 684b0aad7a9SAruna Balakrishnaiah } 685b0aad7a9SAruna Balakrishnaiah 68676cc9580SKees Cook ret = psinfo->write(&record); 687b238b8faSChen Gong if (ret == 0 && reason == KMSG_DUMP_OOPS && pstore_is_mounted()) 6886dda9266SLuck, Tony pstore_new_entry = 1; 689e2ae715dSKay Sievers 69076cc9580SKees Cook total += record.size; 69156280682SMatthew Garrett part++; 692ca01d6ddSTony Luck } 693abd4d558SDon Zickus if (is_locked) 6949f244e9cSSeiji Aguchi spin_unlock_irqrestore(&psinfo->buf_lock, flags); 695ca01d6ddSTony Luck } 696ca01d6ddSTony Luck 697ca01d6ddSTony Luck static struct kmsg_dumper pstore_dumper = { 698ca01d6ddSTony Luck .dump = pstore_dump, 699ca01d6ddSTony Luck }; 700ca01d6ddSTony Luck 701306e5c2aSGeliang Tang /* 702306e5c2aSGeliang Tang * Register with kmsg_dump to save last part of console log on panic. 703306e5c2aSGeliang Tang */ 70418730411SGeliang Tang static void pstore_register_kmsg(void) 70518730411SGeliang Tang { 70618730411SGeliang Tang kmsg_dump_register(&pstore_dumper); 70718730411SGeliang Tang } 70818730411SGeliang Tang 709ee1d2674SGeliang Tang static void pstore_unregister_kmsg(void) 710ee1d2674SGeliang Tang { 711ee1d2674SGeliang Tang kmsg_dump_unregister(&pstore_dumper); 712ee1d2674SGeliang Tang } 713ee1d2674SGeliang Tang 714f29e5956SAnton Vorontsov #ifdef CONFIG_PSTORE_CONSOLE 715f29e5956SAnton Vorontsov static void pstore_console_write(struct console *con, const char *s, unsigned c) 716f29e5956SAnton Vorontsov { 717f29e5956SAnton Vorontsov const char *e = s + c; 718f29e5956SAnton Vorontsov 719f29e5956SAnton Vorontsov while (s < e) { 720e581ca81SKees Cook struct pstore_record record; 721f29e5956SAnton Vorontsov unsigned long flags; 722f29e5956SAnton Vorontsov 723e581ca81SKees Cook pstore_record_init(&record, psinfo); 724e581ca81SKees Cook record.type = PSTORE_TYPE_CONSOLE; 725e581ca81SKees Cook 726f29e5956SAnton Vorontsov if (c > psinfo->bufsize) 727f29e5956SAnton Vorontsov c = psinfo->bufsize; 72880c9d03cSChuansheng Liu 72980c9d03cSChuansheng Liu if (oops_in_progress) { 73080c9d03cSChuansheng Liu if (!spin_trylock_irqsave(&psinfo->buf_lock, flags)) 73180c9d03cSChuansheng Liu break; 73280c9d03cSChuansheng Liu } else { 733f29e5956SAnton Vorontsov spin_lock_irqsave(&psinfo->buf_lock, flags); 73480c9d03cSChuansheng Liu } 735b10b4711SKees Cook record.buf = (char *)s; 736b10b4711SKees Cook record.size = c; 7374c9ec219SKees Cook psinfo->write(&record); 738f29e5956SAnton Vorontsov spin_unlock_irqrestore(&psinfo->buf_lock, flags); 739f29e5956SAnton Vorontsov s += c; 740f29e5956SAnton Vorontsov c = e - s; 741f29e5956SAnton Vorontsov } 742f29e5956SAnton Vorontsov } 743f29e5956SAnton Vorontsov 744f29e5956SAnton Vorontsov static struct console pstore_console = { 745f29e5956SAnton Vorontsov .name = "pstore", 746f29e5956SAnton Vorontsov .write = pstore_console_write, 747f29e5956SAnton Vorontsov .flags = CON_PRINTBUFFER | CON_ENABLED | CON_ANYTIME, 748f29e5956SAnton Vorontsov .index = -1, 749f29e5956SAnton Vorontsov }; 750f29e5956SAnton Vorontsov 751f29e5956SAnton Vorontsov static void pstore_register_console(void) 752f29e5956SAnton Vorontsov { 753f29e5956SAnton Vorontsov register_console(&pstore_console); 754f29e5956SAnton Vorontsov } 755ee1d2674SGeliang Tang 756ee1d2674SGeliang Tang static void pstore_unregister_console(void) 757ee1d2674SGeliang Tang { 758ee1d2674SGeliang Tang unregister_console(&pstore_console); 759ee1d2674SGeliang Tang } 760f29e5956SAnton Vorontsov #else 761f29e5956SAnton Vorontsov static void pstore_register_console(void) {} 762ee1d2674SGeliang Tang static void pstore_unregister_console(void) {} 763f29e5956SAnton Vorontsov #endif 764f29e5956SAnton Vorontsov 7654c9ec219SKees Cook static int pstore_write_user_compat(struct pstore_record *record, 766fdd03118SKees Cook const char __user *buf) 7675bf6d1b9SMark Salyzyn { 76830800d99SKees Cook int ret = 0; 7695bf6d1b9SMark Salyzyn 77030800d99SKees Cook if (record->buf) 77130800d99SKees Cook return -EINVAL; 7725bf6d1b9SMark Salyzyn 773077090afSGeliang Tang record->buf = memdup_user(buf, record->size); 774dfd6fa39SHirofumi Nakagawa if (IS_ERR(record->buf)) { 775077090afSGeliang Tang ret = PTR_ERR(record->buf); 77630800d99SKees Cook goto out; 7775bf6d1b9SMark Salyzyn } 77830800d99SKees Cook 7794c9ec219SKees Cook ret = record->psi->write(record); 78030800d99SKees Cook 78130800d99SKees Cook kfree(record->buf); 782077090afSGeliang Tang out: 78330800d99SKees Cook record->buf = NULL; 78430800d99SKees Cook 78530800d99SKees Cook return unlikely(ret < 0) ? ret : record->size; 7865bf6d1b9SMark Salyzyn } 7875bf6d1b9SMark Salyzyn 788ca01d6ddSTony Luck /* 789ca01d6ddSTony Luck * platform specific persistent storage driver registers with 790ca01d6ddSTony Luck * us here. If pstore is already mounted, call the platform 791ca01d6ddSTony Luck * read function right away to populate the file system. If not 792ca01d6ddSTony Luck * then the pstore mount code will call us later to fill out 793ca01d6ddSTony Luck * the file system. 794ca01d6ddSTony Luck */ 795ca01d6ddSTony Luck int pstore_register(struct pstore_info *psi) 796ca01d6ddSTony Luck { 797ca01d6ddSTony Luck struct module *owner = psi->owner; 798ca01d6ddSTony Luck 7990d7cd09aSKees Cook if (backend && strcmp(backend, psi->name)) { 8000d7cd09aSKees Cook pr_warn("ignoring unexpected backend '%s'\n", psi->name); 8018e48b1a8SLenny Szubowicz return -EPERM; 8020d7cd09aSKees Cook } 8038e48b1a8SLenny Szubowicz 8044c9ec219SKees Cook /* Sanity check flags. */ 8054c9ec219SKees Cook if (!psi->flags) { 8064c9ec219SKees Cook pr_warn("backend '%s' must support at least one frontend\n", 8074c9ec219SKees Cook psi->name); 8084c9ec219SKees Cook return -EINVAL; 8094c9ec219SKees Cook } 8104c9ec219SKees Cook 8114c9ec219SKees Cook /* Check for required functions. */ 8124c9ec219SKees Cook if (!psi->read || !psi->write) { 8134c9ec219SKees Cook pr_warn("backend '%s' must implement read() and write()\n", 8144c9ec219SKees Cook psi->name); 8154c9ec219SKees Cook return -EINVAL; 8164c9ec219SKees Cook } 8174c9ec219SKees Cook 818ca01d6ddSTony Luck spin_lock(&pstore_lock); 819ca01d6ddSTony Luck if (psinfo) { 8200d7cd09aSKees Cook pr_warn("backend '%s' already loaded: ignoring '%s'\n", 8210d7cd09aSKees Cook psinfo->name, psi->name); 822ca01d6ddSTony Luck spin_unlock(&pstore_lock); 823ca01d6ddSTony Luck return -EBUSY; 824ca01d6ddSTony Luck } 825dee28e72SMatthew Garrett 8264c9ec219SKees Cook if (!psi->write_user) 8274c9ec219SKees Cook psi->write_user = pstore_write_user_compat; 828ca01d6ddSTony Luck psinfo = psi; 829f6f82851SKees Cook mutex_init(&psinfo->read_mutex); 830ca01d6ddSTony Luck spin_unlock(&pstore_lock); 831ca01d6ddSTony Luck 832ca01d6ddSTony Luck if (owner && !try_module_get(owner)) { 833ca01d6ddSTony Luck psinfo = NULL; 834ca01d6ddSTony Luck return -EINVAL; 835ca01d6ddSTony Luck } 836ca01d6ddSTony Luck 837b0aad7a9SAruna Balakrishnaiah allocate_buf_for_compression(); 838b0aad7a9SAruna Balakrishnaiah 839ca01d6ddSTony Luck if (pstore_is_mounted()) 8406dda9266SLuck, Tony pstore_get_records(0); 841ca01d6ddSTony Luck 842c950fd6fSNamhyung Kim if (psi->flags & PSTORE_FLAGS_DMESG) 84318730411SGeliang Tang pstore_register_kmsg(); 844c950fd6fSNamhyung Kim if (psi->flags & PSTORE_FLAGS_CONSOLE) 845f29e5956SAnton Vorontsov pstore_register_console(); 846c950fd6fSNamhyung Kim if (psi->flags & PSTORE_FLAGS_FTRACE) 84765f8c95eSAnton Vorontsov pstore_register_ftrace(); 848c950fd6fSNamhyung Kim if (psi->flags & PSTORE_FLAGS_PMSG) 8499d5438f4SMark Salyzyn pstore_register_pmsg(); 850ca01d6ddSTony Luck 8516330d553SKees Cook /* Start watching for new records, if desired. */ 852a3f5f075SAnton Vorontsov if (pstore_update_ms >= 0) { 853a3f5f075SAnton Vorontsov pstore_timer.expires = jiffies + 854a3f5f075SAnton Vorontsov msecs_to_jiffies(pstore_update_ms); 8556dda9266SLuck, Tony add_timer(&pstore_timer); 856a3f5f075SAnton Vorontsov } 8576dda9266SLuck, Tony 85842222c2aSWang Long /* 85942222c2aSWang Long * Update the module parameter backend, so it is visible 86042222c2aSWang Long * through /sys/module/pstore/parameters/backend 86142222c2aSWang Long */ 86242222c2aSWang Long backend = psi->name; 86342222c2aSWang Long 864ef748853SFabian Frederick pr_info("Registered %s as persistent store backend\n", psi->name); 8658e48b1a8SLenny Szubowicz 8661344dd86SKees Cook module_put(owner); 8671344dd86SKees Cook 868ca01d6ddSTony Luck return 0; 869ca01d6ddSTony Luck } 870ca01d6ddSTony Luck EXPORT_SYMBOL_GPL(pstore_register); 871ca01d6ddSTony Luck 872ee1d2674SGeliang Tang void pstore_unregister(struct pstore_info *psi) 873ee1d2674SGeliang Tang { 8746330d553SKees Cook /* Stop timer and make sure all work has finished. */ 8756330d553SKees Cook pstore_update_ms = -1; 8766330d553SKees Cook del_timer_sync(&pstore_timer); 8776330d553SKees Cook flush_work(&pstore_work); 8786330d553SKees Cook 879c950fd6fSNamhyung Kim if (psi->flags & PSTORE_FLAGS_PMSG) 880ee1d2674SGeliang Tang pstore_unregister_pmsg(); 881c950fd6fSNamhyung Kim if (psi->flags & PSTORE_FLAGS_FTRACE) 882ee1d2674SGeliang Tang pstore_unregister_ftrace(); 883c950fd6fSNamhyung Kim if (psi->flags & PSTORE_FLAGS_CONSOLE) 884ee1d2674SGeliang Tang pstore_unregister_console(); 885c950fd6fSNamhyung Kim if (psi->flags & PSTORE_FLAGS_DMESG) 886ee1d2674SGeliang Tang pstore_unregister_kmsg(); 887ee1d2674SGeliang Tang 888ee1d2674SGeliang Tang free_buf_for_compression(); 889ee1d2674SGeliang Tang 890ee1d2674SGeliang Tang psinfo = NULL; 891ee1d2674SGeliang Tang backend = NULL; 892ee1d2674SGeliang Tang } 893ee1d2674SGeliang Tang EXPORT_SYMBOL_GPL(pstore_unregister); 894ee1d2674SGeliang Tang 895634f8f51SKees Cook static void decompress_record(struct pstore_record *record) 896634f8f51SKees Cook { 897634f8f51SKees Cook int unzipped_len; 8987e8cc8dcSKees Cook char *decompressed; 899634f8f51SKees Cook 9004a16d1cbSAnkit Kumar if (!record->compressed) 9014a16d1cbSAnkit Kumar return; 9024a16d1cbSAnkit Kumar 903634f8f51SKees Cook /* Only PSTORE_TYPE_DMESG support compression. */ 9044a16d1cbSAnkit Kumar if (record->type != PSTORE_TYPE_DMESG) { 905634f8f51SKees Cook pr_warn("ignored compressed record type %d\n", record->type); 906634f8f51SKees Cook return; 907634f8f51SKees Cook } 908634f8f51SKees Cook 909634f8f51SKees Cook /* No compression method has created the common buffer. */ 910634f8f51SKees Cook if (!big_oops_buf) { 911634f8f51SKees Cook pr_warn("no decompression buffer allocated\n"); 912634f8f51SKees Cook return; 913634f8f51SKees Cook } 914634f8f51SKees Cook 915634f8f51SKees Cook unzipped_len = pstore_decompress(record->buf, big_oops_buf, 916634f8f51SKees Cook record->size, big_oops_buf_sz); 9177e8cc8dcSKees Cook if (unzipped_len <= 0) { 9187e8cc8dcSKees Cook pr_err("decompression failed: %d\n", unzipped_len); 9197e8cc8dcSKees Cook return; 9207e8cc8dcSKees Cook } 9217e8cc8dcSKees Cook 9227e8cc8dcSKees Cook /* Build new buffer for decompressed contents. */ 9237e8cc8dcSKees Cook decompressed = kmalloc(unzipped_len + record->ecc_notice_size, 9247e8cc8dcSKees Cook GFP_KERNEL); 9257e8cc8dcSKees Cook if (!decompressed) { 9267e8cc8dcSKees Cook pr_err("decompression ran out of memory\n"); 9277e8cc8dcSKees Cook return; 9287e8cc8dcSKees Cook } 9297e8cc8dcSKees Cook memcpy(decompressed, big_oops_buf, unzipped_len); 9307e8cc8dcSKees Cook 9317e8cc8dcSKees Cook /* Append ECC notice to decompressed buffer. */ 9327e8cc8dcSKees Cook memcpy(decompressed + unzipped_len, record->buf + record->size, 933634f8f51SKees Cook record->ecc_notice_size); 9347e8cc8dcSKees Cook 9357e8cc8dcSKees Cook /* Swap out compresed contents with decompressed contents. */ 936634f8f51SKees Cook kfree(record->buf); 9377e8cc8dcSKees Cook record->buf = decompressed; 938634f8f51SKees Cook record->size = unzipped_len; 939634f8f51SKees Cook record->compressed = false; 940634f8f51SKees Cook } 941634f8f51SKees Cook 942ca01d6ddSTony Luck /* 9433a7d2fd1SKees Cook * Read all the records from one persistent store backend. Create 9446dda9266SLuck, Tony * files in our filesystem. Don't warn about -EEXIST errors 9456dda9266SLuck, Tony * when we are re-scanning the backing store looking to add new 9466dda9266SLuck, Tony * error records. 947ca01d6ddSTony Luck */ 9483a7d2fd1SKees Cook void pstore_get_backend_records(struct pstore_info *psi, 9493a7d2fd1SKees Cook struct dentry *root, int quiet) 950ca01d6ddSTony Luck { 9512a2b0acfSKees Cook int failed = 0; 952656de42eSKees Cook unsigned int stop_loop = 65536; 953ca01d6ddSTony Luck 9543a7d2fd1SKees Cook if (!psi || !root) 955ca01d6ddSTony Luck return; 956ca01d6ddSTony Luck 957f6f82851SKees Cook mutex_lock(&psi->read_mutex); 9582174f6dfSKees Cook if (psi->open && psi->open(psi)) 95906cf91b4SChen Gong goto out; 96006cf91b4SChen Gong 9611dfff7ddSKees Cook /* 9621dfff7ddSKees Cook * Backend callback read() allocates record.buf. decompress_record() 9631dfff7ddSKees Cook * may reallocate record.buf. On success, pstore_mkfile() will keep 9641dfff7ddSKees Cook * the record.buf, so free it only on failure. 9651dfff7ddSKees Cook */ 966656de42eSKees Cook for (; stop_loop; stop_loop--) { 9672a2b0acfSKees Cook struct pstore_record *record; 9682a2b0acfSKees Cook int rc; 9692a2b0acfSKees Cook 9702a2b0acfSKees Cook record = kzalloc(sizeof(*record), GFP_KERNEL); 9712a2b0acfSKees Cook if (!record) { 9722a2b0acfSKees Cook pr_err("out of memory creating record\n"); 9732a2b0acfSKees Cook break; 9742a2b0acfSKees Cook } 975e581ca81SKees Cook pstore_record_init(record, psi); 9762a2b0acfSKees Cook 9772a2b0acfSKees Cook record->size = psi->read(record); 9782a2b0acfSKees Cook 9792a2b0acfSKees Cook /* No more records left in backend? */ 980f6525b96SDouglas Anderson if (record->size <= 0) { 981f6525b96SDouglas Anderson kfree(record); 9822a2b0acfSKees Cook break; 983f6525b96SDouglas Anderson } 9842a2b0acfSKees Cook 9852a2b0acfSKees Cook decompress_record(record); 9863a7d2fd1SKees Cook rc = pstore_mkfile(root, record); 9871dfff7ddSKees Cook if (rc) { 98883f70f07SKees Cook /* pstore_mkfile() did not take record, so free it. */ 9892a2b0acfSKees Cook kfree(record->buf); 99083f70f07SKees Cook kfree(record); 9911dfff7ddSKees Cook if (rc != -EEXIST || !quiet) 9921dfff7ddSKees Cook failed++; 9931dfff7ddSKees Cook } 994ca01d6ddSTony Luck } 9952174f6dfSKees Cook if (psi->close) 99606cf91b4SChen Gong psi->close(psi); 99706cf91b4SChen Gong out: 998f6f82851SKees Cook mutex_unlock(&psi->read_mutex); 999ca01d6ddSTony Luck 1000ca01d6ddSTony Luck if (failed) 1001656de42eSKees Cook pr_warn("failed to create %d record(s) from '%s'\n", 1002ca01d6ddSTony Luck failed, psi->name); 1003656de42eSKees Cook if (!stop_loop) 1004656de42eSKees Cook pr_err("looping? Too many records seen from '%s'\n", 1005656de42eSKees Cook psi->name); 1006ca01d6ddSTony Luck } 1007ca01d6ddSTony Luck 10086dda9266SLuck, Tony static void pstore_dowork(struct work_struct *work) 10096dda9266SLuck, Tony { 10106dda9266SLuck, Tony pstore_get_records(1); 10116dda9266SLuck, Tony } 10126dda9266SLuck, Tony 101324ed960aSKees Cook static void pstore_timefunc(struct timer_list *unused) 10146dda9266SLuck, Tony { 10156dda9266SLuck, Tony if (pstore_new_entry) { 10166dda9266SLuck, Tony pstore_new_entry = 0; 10176dda9266SLuck, Tony schedule_work(&pstore_work); 10186dda9266SLuck, Tony } 10196dda9266SLuck, Tony 10206330d553SKees Cook if (pstore_update_ms >= 0) 10216330d553SKees Cook mod_timer(&pstore_timer, 10226330d553SKees Cook jiffies + msecs_to_jiffies(pstore_update_ms)); 10236dda9266SLuck, Tony } 10246dda9266SLuck, Tony 1025dee28e72SMatthew Garrett module_param(backend, charp, 0444); 1026dee28e72SMatthew Garrett MODULE_PARM_DESC(backend, "Pstore backend to use"); 1027