Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Persistent Storage - platform driver interface parts. |
| 3 | * |
Anton Vorontsov | f29e595 | 2012-05-26 06:20:19 -0700 | [diff] [blame] | 4 | * Copyright (C) 2007-2008 Google, Inc. |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 5 | * Copyright (C) 2010 Intel Corporation <tony.luck@intel.com> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License version 2 as |
| 9 | * published by the Free Software Foundation. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 | */ |
| 20 | |
Fabian Frederick | ef74885 | 2014-06-06 14:37:31 -0700 | [diff] [blame] | 21 | #define pr_fmt(fmt) "pstore: " fmt |
| 22 | |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 23 | #include <linux/atomic.h> |
| 24 | #include <linux/types.h> |
| 25 | #include <linux/errno.h> |
| 26 | #include <linux/init.h> |
| 27 | #include <linux/kmsg_dump.h> |
Anton Vorontsov | f29e595 | 2012-05-26 06:20:19 -0700 | [diff] [blame] | 28 | #include <linux/console.h> |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 29 | #include <linux/module.h> |
| 30 | #include <linux/pstore.h> |
Geliang Tang | 8cfc8dd | 2016-02-18 22:04:22 +0800 | [diff] [blame] | 31 | #ifdef CONFIG_PSTORE_ZLIB_COMPRESS |
Aruna Balakrishnaiah | b0aad7a | 2013-08-16 13:53:10 -0700 | [diff] [blame] | 32 | #include <linux/zlib.h> |
Geliang Tang | 8cfc8dd | 2016-02-18 22:04:22 +0800 | [diff] [blame] | 33 | #endif |
| 34 | #ifdef CONFIG_PSTORE_LZO_COMPRESS |
| 35 | #include <linux/lzo.h> |
| 36 | #endif |
Geliang Tang | 239b716 | 2018-02-13 14:40:39 +0800 | [diff] [blame] | 37 | #if defined(CONFIG_PSTORE_LZ4_COMPRESS) || defined(CONFIG_PSTORE_LZ4HC_COMPRESS) |
Geliang Tang | 8cfc8dd | 2016-02-18 22:04:22 +0800 | [diff] [blame] | 38 | #include <linux/lz4.h> |
| 39 | #endif |
Geliang Tang | 239b716 | 2018-02-13 14:40:39 +0800 | [diff] [blame] | 40 | #ifdef CONFIG_PSTORE_842_COMPRESS |
| 41 | #include <linux/sw842.h> |
| 42 | #endif |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 43 | #include <linux/string.h> |
Luck, Tony | 6dda926 | 2011-08-11 15:14:39 -0700 | [diff] [blame] | 44 | #include <linux/timer.h> |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 45 | #include <linux/slab.h> |
| 46 | #include <linux/uaccess.h> |
Anton Vorontsov | a3f5f07 | 2012-05-26 06:20:28 -0700 | [diff] [blame] | 47 | #include <linux/jiffies.h> |
Luck, Tony | 6dda926 | 2011-08-11 15:14:39 -0700 | [diff] [blame] | 48 | #include <linux/workqueue.h> |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 49 | |
| 50 | #include "internal.h" |
| 51 | |
| 52 | /* |
Luck, Tony | 6dda926 | 2011-08-11 15:14:39 -0700 | [diff] [blame] | 53 | * We defer making "oops" entries appear in pstore - see |
| 54 | * whether the system is actually still running well enough |
| 55 | * to let someone see the entry |
| 56 | */ |
Anton Vorontsov | 521f7288 | 2012-05-26 06:20:29 -0700 | [diff] [blame] | 57 | static int pstore_update_ms = -1; |
Anton Vorontsov | a3f5f07 | 2012-05-26 06:20:28 -0700 | [diff] [blame] | 58 | module_param_named(update_ms, pstore_update_ms, int, 0600); |
| 59 | MODULE_PARM_DESC(update_ms, "milliseconds before pstore updates its content " |
Anton Vorontsov | 521f7288 | 2012-05-26 06:20:29 -0700 | [diff] [blame] | 60 | "(default is -1, which means runtime updates are disabled; " |
| 61 | "enabling this option is not safe, it may lead to further " |
| 62 | "corruption on Oopses)"); |
Luck, Tony | 6dda926 | 2011-08-11 15:14:39 -0700 | [diff] [blame] | 63 | |
| 64 | static int pstore_new_entry; |
| 65 | |
Kees Cook | 24ed960 | 2017-08-28 11:28:21 -0700 | [diff] [blame] | 66 | static void pstore_timefunc(struct timer_list *); |
Kees Cook | 1d27e3e | 2017-10-04 16:27:04 -0700 | [diff] [blame] | 67 | static DEFINE_TIMER(pstore_timer, pstore_timefunc); |
Luck, Tony | 6dda926 | 2011-08-11 15:14:39 -0700 | [diff] [blame] | 68 | |
| 69 | static void pstore_dowork(struct work_struct *); |
| 70 | static DECLARE_WORK(pstore_work, pstore_dowork); |
| 71 | |
| 72 | /* |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 73 | * pstore_lock just protects "psinfo" during |
| 74 | * calls to pstore_register() |
| 75 | */ |
| 76 | static DEFINE_SPINLOCK(pstore_lock); |
Anton Vorontsov | 060287b | 2012-07-09 17:10:41 -0700 | [diff] [blame] | 77 | struct pstore_info *psinfo; |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 78 | |
Matthew Garrett | dee28e7 | 2011-07-21 16:57:55 -0400 | [diff] [blame] | 79 | static char *backend; |
| 80 | |
Aruna Balakrishnaiah | b0aad7a | 2013-08-16 13:53:10 -0700 | [diff] [blame] | 81 | /* Compression parameters */ |
Geliang Tang | 8cfc8dd | 2016-02-18 22:04:22 +0800 | [diff] [blame] | 82 | #ifdef CONFIG_PSTORE_ZLIB_COMPRESS |
Aruna Balakrishnaiah | b0aad7a | 2013-08-16 13:53:10 -0700 | [diff] [blame] | 83 | #define COMPR_LEVEL 6 |
| 84 | #define WINDOW_BITS 12 |
| 85 | #define MEM_LEVEL 4 |
| 86 | static struct z_stream_s stream; |
Geliang Tang | 8cfc8dd | 2016-02-18 22:04:22 +0800 | [diff] [blame] | 87 | #else |
| 88 | static unsigned char *workspace; |
| 89 | #endif |
| 90 | |
| 91 | struct pstore_zbackend { |
| 92 | int (*compress)(const void *in, void *out, size_t inlen, size_t outlen); |
| 93 | int (*decompress)(void *in, void *out, size_t inlen, size_t outlen); |
| 94 | void (*allocate)(void); |
| 95 | void (*free)(void); |
| 96 | |
| 97 | const char *name; |
| 98 | }; |
Aruna Balakrishnaiah | b0aad7a | 2013-08-16 13:53:10 -0700 | [diff] [blame] | 99 | |
| 100 | static char *big_oops_buf; |
| 101 | static size_t big_oops_buf_sz; |
| 102 | |
Luck, Tony | 366f7e7 | 2011-03-18 15:33:43 -0700 | [diff] [blame] | 103 | /* How much of the console log to snapshot */ |
David Howells | 349d743 | 2017-07-05 16:24:34 +0100 | [diff] [blame] | 104 | unsigned long kmsg_bytes = PSTORE_DEFAULT_KMSG_BYTES; |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 105 | |
Luck, Tony | 366f7e7 | 2011-03-18 15:33:43 -0700 | [diff] [blame] | 106 | void pstore_set_kmsg_bytes(int bytes) |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 107 | { |
Luck, Tony | 366f7e7 | 2011-03-18 15:33:43 -0700 | [diff] [blame] | 108 | kmsg_bytes = bytes; |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 109 | } |
| 110 | |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 111 | /* Tag each group of saved records with a sequence number */ |
| 112 | static int oopscount; |
| 113 | |
Seiji Aguchi | 381b872 | 2012-03-16 15:36:59 -0700 | [diff] [blame] | 114 | static const char *get_reason_str(enum kmsg_dump_reason reason) |
| 115 | { |
| 116 | switch (reason) { |
| 117 | case KMSG_DUMP_PANIC: |
| 118 | return "Panic"; |
| 119 | case KMSG_DUMP_OOPS: |
| 120 | return "Oops"; |
| 121 | case KMSG_DUMP_EMERG: |
| 122 | return "Emergency"; |
| 123 | case KMSG_DUMP_RESTART: |
| 124 | return "Restart"; |
| 125 | case KMSG_DUMP_HALT: |
| 126 | return "Halt"; |
| 127 | case KMSG_DUMP_POWEROFF: |
| 128 | return "Poweroff"; |
| 129 | default: |
| 130 | return "Unknown"; |
| 131 | } |
| 132 | } |
Tony Luck | 9f6af27 | 2011-03-22 16:01:49 -0700 | [diff] [blame] | 133 | |
Seiji Aguchi | 9f244e9 | 2013-01-11 18:09:41 +0000 | [diff] [blame] | 134 | bool pstore_cannot_block_path(enum kmsg_dump_reason reason) |
| 135 | { |
| 136 | /* |
| 137 | * In case of NMI path, pstore shouldn't be blocked |
| 138 | * regardless of reason. |
| 139 | */ |
| 140 | if (in_nmi()) |
| 141 | return true; |
| 142 | |
| 143 | switch (reason) { |
| 144 | /* In panic case, other cpus are stopped by smp_send_stop(). */ |
| 145 | case KMSG_DUMP_PANIC: |
| 146 | /* Emergency restart shouldn't be blocked by spin lock. */ |
| 147 | case KMSG_DUMP_EMERG: |
| 148 | return true; |
| 149 | default: |
| 150 | return false; |
| 151 | } |
| 152 | } |
| 153 | EXPORT_SYMBOL_GPL(pstore_cannot_block_path); |
| 154 | |
Geliang Tang | 8cfc8dd | 2016-02-18 22:04:22 +0800 | [diff] [blame] | 155 | #ifdef CONFIG_PSTORE_ZLIB_COMPRESS |
Aruna Balakrishnaiah | b0aad7a | 2013-08-16 13:53:10 -0700 | [diff] [blame] | 156 | /* Derived from logfs_compress() */ |
Geliang Tang | 8cfc8dd | 2016-02-18 22:04:22 +0800 | [diff] [blame] | 157 | static int compress_zlib(const void *in, void *out, size_t inlen, size_t outlen) |
Aruna Balakrishnaiah | b0aad7a | 2013-08-16 13:53:10 -0700 | [diff] [blame] | 158 | { |
| 159 | int err, ret; |
| 160 | |
| 161 | ret = -EIO; |
| 162 | err = zlib_deflateInit2(&stream, COMPR_LEVEL, Z_DEFLATED, WINDOW_BITS, |
| 163 | MEM_LEVEL, Z_DEFAULT_STRATEGY); |
| 164 | if (err != Z_OK) |
| 165 | goto error; |
| 166 | |
| 167 | stream.next_in = in; |
| 168 | stream.avail_in = inlen; |
| 169 | stream.total_in = 0; |
| 170 | stream.next_out = out; |
| 171 | stream.avail_out = outlen; |
| 172 | stream.total_out = 0; |
| 173 | |
| 174 | err = zlib_deflate(&stream, Z_FINISH); |
| 175 | if (err != Z_STREAM_END) |
| 176 | goto error; |
| 177 | |
| 178 | err = zlib_deflateEnd(&stream); |
| 179 | if (err != Z_OK) |
| 180 | goto error; |
| 181 | |
| 182 | if (stream.total_out >= stream.total_in) |
| 183 | goto error; |
| 184 | |
| 185 | ret = stream.total_out; |
| 186 | error: |
| 187 | return ret; |
| 188 | } |
| 189 | |
Aruna Balakrishnaiah | adb42f5 | 2013-08-16 13:53:28 -0700 | [diff] [blame] | 190 | /* Derived from logfs_uncompress */ |
Geliang Tang | 8cfc8dd | 2016-02-18 22:04:22 +0800 | [diff] [blame] | 191 | static int decompress_zlib(void *in, void *out, size_t inlen, size_t outlen) |
Aruna Balakrishnaiah | adb42f5 | 2013-08-16 13:53:28 -0700 | [diff] [blame] | 192 | { |
| 193 | int err, ret; |
| 194 | |
| 195 | ret = -EIO; |
Aruna Balakrishnaiah | b61edf8 | 2013-09-11 10:58:03 -0700 | [diff] [blame] | 196 | err = zlib_inflateInit2(&stream, WINDOW_BITS); |
Aruna Balakrishnaiah | adb42f5 | 2013-08-16 13:53:28 -0700 | [diff] [blame] | 197 | if (err != Z_OK) |
| 198 | goto error; |
| 199 | |
| 200 | stream.next_in = in; |
| 201 | stream.avail_in = inlen; |
| 202 | stream.total_in = 0; |
| 203 | stream.next_out = out; |
| 204 | stream.avail_out = outlen; |
| 205 | stream.total_out = 0; |
| 206 | |
| 207 | err = zlib_inflate(&stream, Z_FINISH); |
| 208 | if (err != Z_STREAM_END) |
| 209 | goto error; |
| 210 | |
| 211 | err = zlib_inflateEnd(&stream); |
| 212 | if (err != Z_OK) |
| 213 | goto error; |
| 214 | |
| 215 | ret = stream.total_out; |
| 216 | error: |
| 217 | return ret; |
| 218 | } |
| 219 | |
Geliang Tang | 8cfc8dd | 2016-02-18 22:04:22 +0800 | [diff] [blame] | 220 | static void allocate_zlib(void) |
Aruna Balakrishnaiah | b0aad7a | 2013-08-16 13:53:10 -0700 | [diff] [blame] | 221 | { |
| 222 | size_t size; |
Aruna Balakrishnaiah | 7de8fe2 | 2013-09-11 10:57:41 -0700 | [diff] [blame] | 223 | size_t cmpr; |
Aruna Balakrishnaiah | b0aad7a | 2013-08-16 13:53:10 -0700 | [diff] [blame] | 224 | |
Aruna Balakrishnaiah | 7de8fe2 | 2013-09-11 10:57:41 -0700 | [diff] [blame] | 225 | switch (psinfo->bufsize) { |
| 226 | /* buffer range for efivars */ |
| 227 | case 1000 ... 2000: |
| 228 | cmpr = 56; |
| 229 | break; |
| 230 | case 2001 ... 3000: |
| 231 | cmpr = 54; |
| 232 | break; |
| 233 | case 3001 ... 3999: |
| 234 | cmpr = 52; |
| 235 | break; |
| 236 | /* buffer range for nvram, erst */ |
| 237 | case 4000 ... 10000: |
| 238 | cmpr = 45; |
| 239 | break; |
| 240 | default: |
| 241 | cmpr = 60; |
| 242 | break; |
| 243 | } |
| 244 | |
| 245 | big_oops_buf_sz = (psinfo->bufsize * 100) / cmpr; |
Aruna Balakrishnaiah | b0aad7a | 2013-08-16 13:53:10 -0700 | [diff] [blame] | 246 | big_oops_buf = kmalloc(big_oops_buf_sz, GFP_KERNEL); |
| 247 | if (big_oops_buf) { |
| 248 | size = max(zlib_deflate_workspacesize(WINDOW_BITS, MEM_LEVEL), |
| 249 | zlib_inflate_workspacesize()); |
| 250 | stream.workspace = kmalloc(size, GFP_KERNEL); |
| 251 | if (!stream.workspace) { |
Fabian Frederick | ef74885 | 2014-06-06 14:37:31 -0700 | [diff] [blame] | 252 | pr_err("No memory for compression workspace; skipping compression\n"); |
Aruna Balakrishnaiah | b0aad7a | 2013-08-16 13:53:10 -0700 | [diff] [blame] | 253 | kfree(big_oops_buf); |
| 254 | big_oops_buf = NULL; |
| 255 | } |
| 256 | } else { |
Fabian Frederick | ef74885 | 2014-06-06 14:37:31 -0700 | [diff] [blame] | 257 | pr_err("No memory for uncompressed data; skipping compression\n"); |
Aruna Balakrishnaiah | b0aad7a | 2013-08-16 13:53:10 -0700 | [diff] [blame] | 258 | stream.workspace = NULL; |
| 259 | } |
| 260 | |
| 261 | } |
| 262 | |
Geliang Tang | 8cfc8dd | 2016-02-18 22:04:22 +0800 | [diff] [blame] | 263 | static void free_zlib(void) |
Geliang Tang | ee1d267 | 2015-10-20 00:39:03 -0700 | [diff] [blame] | 264 | { |
| 265 | kfree(stream.workspace); |
| 266 | stream.workspace = NULL; |
| 267 | kfree(big_oops_buf); |
| 268 | big_oops_buf = NULL; |
Geliang Tang | 8cfc8dd | 2016-02-18 22:04:22 +0800 | [diff] [blame] | 269 | big_oops_buf_sz = 0; |
| 270 | } |
| 271 | |
Bhumika Goyal | 3faf935 | 2017-02-19 15:07:53 +0530 | [diff] [blame] | 272 | static const struct pstore_zbackend backend_zlib = { |
Geliang Tang | 8cfc8dd | 2016-02-18 22:04:22 +0800 | [diff] [blame] | 273 | .compress = compress_zlib, |
| 274 | .decompress = decompress_zlib, |
| 275 | .allocate = allocate_zlib, |
| 276 | .free = free_zlib, |
| 277 | .name = "zlib", |
| 278 | }; |
| 279 | #endif |
| 280 | |
| 281 | #ifdef CONFIG_PSTORE_LZO_COMPRESS |
| 282 | static int compress_lzo(const void *in, void *out, size_t inlen, size_t outlen) |
| 283 | { |
| 284 | int ret; |
| 285 | |
| 286 | ret = lzo1x_1_compress(in, inlen, out, &outlen, workspace); |
| 287 | if (ret != LZO_E_OK) { |
| 288 | pr_err("lzo_compress error, ret = %d!\n", ret); |
| 289 | return -EIO; |
| 290 | } |
| 291 | |
| 292 | return outlen; |
| 293 | } |
| 294 | |
| 295 | static int decompress_lzo(void *in, void *out, size_t inlen, size_t outlen) |
| 296 | { |
| 297 | int ret; |
| 298 | |
| 299 | ret = lzo1x_decompress_safe(in, inlen, out, &outlen); |
| 300 | if (ret != LZO_E_OK) { |
| 301 | pr_err("lzo_decompress error, ret = %d!\n", ret); |
| 302 | return -EIO; |
| 303 | } |
| 304 | |
| 305 | return outlen; |
| 306 | } |
| 307 | |
| 308 | static void allocate_lzo(void) |
| 309 | { |
| 310 | big_oops_buf_sz = lzo1x_worst_compress(psinfo->bufsize); |
| 311 | big_oops_buf = kmalloc(big_oops_buf_sz, GFP_KERNEL); |
| 312 | if (big_oops_buf) { |
| 313 | workspace = kmalloc(LZO1X_MEM_COMPRESS, GFP_KERNEL); |
| 314 | if (!workspace) { |
| 315 | pr_err("No memory for compression workspace; skipping compression\n"); |
| 316 | kfree(big_oops_buf); |
| 317 | big_oops_buf = NULL; |
| 318 | } |
| 319 | } else { |
| 320 | pr_err("No memory for uncompressed data; skipping compression\n"); |
| 321 | workspace = NULL; |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | static void free_lzo(void) |
| 326 | { |
| 327 | kfree(workspace); |
| 328 | kfree(big_oops_buf); |
| 329 | big_oops_buf = NULL; |
| 330 | big_oops_buf_sz = 0; |
| 331 | } |
| 332 | |
Bhumika Goyal | 3faf935 | 2017-02-19 15:07:53 +0530 | [diff] [blame] | 333 | static const struct pstore_zbackend backend_lzo = { |
Geliang Tang | 8cfc8dd | 2016-02-18 22:04:22 +0800 | [diff] [blame] | 334 | .compress = compress_lzo, |
| 335 | .decompress = decompress_lzo, |
| 336 | .allocate = allocate_lzo, |
| 337 | .free = free_lzo, |
| 338 | .name = "lzo", |
| 339 | }; |
| 340 | #endif |
| 341 | |
Geliang Tang | 239b716 | 2018-02-13 14:40:39 +0800 | [diff] [blame] | 342 | #if defined(CONFIG_PSTORE_LZ4_COMPRESS) || defined(CONFIG_PSTORE_LZ4HC_COMPRESS) |
Geliang Tang | 8cfc8dd | 2016-02-18 22:04:22 +0800 | [diff] [blame] | 343 | static int decompress_lz4(void *in, void *out, size_t inlen, size_t outlen) |
| 344 | { |
| 345 | int ret; |
| 346 | |
Sven Schmidt | d21b5ff12 | 2017-02-24 15:01:22 -0800 | [diff] [blame] | 347 | ret = LZ4_decompress_safe(in, out, inlen, outlen); |
| 348 | if (ret < 0) { |
| 349 | /* |
| 350 | * LZ4_decompress_safe will return an error code |
| 351 | * (< 0) if decompression failed |
| 352 | */ |
| 353 | pr_err("LZ4_decompress_safe error, ret = %d!\n", ret); |
Geliang Tang | 8cfc8dd | 2016-02-18 22:04:22 +0800 | [diff] [blame] | 354 | return -EIO; |
| 355 | } |
| 356 | |
Sven Schmidt | d21b5ff12 | 2017-02-24 15:01:22 -0800 | [diff] [blame] | 357 | return ret; |
Geliang Tang | 8cfc8dd | 2016-02-18 22:04:22 +0800 | [diff] [blame] | 358 | } |
| 359 | |
Geliang Tang | 239b716 | 2018-02-13 14:40:39 +0800 | [diff] [blame] | 360 | static void free_lz4(void) |
| 361 | { |
| 362 | kfree(workspace); |
| 363 | kfree(big_oops_buf); |
| 364 | big_oops_buf = NULL; |
| 365 | big_oops_buf_sz = 0; |
| 366 | } |
| 367 | #endif |
| 368 | |
| 369 | #ifdef CONFIG_PSTORE_LZ4_COMPRESS |
| 370 | static int compress_lz4(const void *in, void *out, size_t inlen, size_t outlen) |
| 371 | { |
| 372 | int ret; |
| 373 | |
| 374 | ret = LZ4_compress_default(in, out, inlen, outlen, workspace); |
| 375 | if (!ret) { |
| 376 | pr_err("LZ4_compress_default error; compression failed!\n"); |
| 377 | return -EIO; |
| 378 | } |
| 379 | |
| 380 | return ret; |
| 381 | } |
| 382 | |
Geliang Tang | 8cfc8dd | 2016-02-18 22:04:22 +0800 | [diff] [blame] | 383 | static void allocate_lz4(void) |
| 384 | { |
Sven Schmidt | d21b5ff12 | 2017-02-24 15:01:22 -0800 | [diff] [blame] | 385 | big_oops_buf_sz = LZ4_compressBound(psinfo->bufsize); |
Geliang Tang | 8cfc8dd | 2016-02-18 22:04:22 +0800 | [diff] [blame] | 386 | big_oops_buf = kmalloc(big_oops_buf_sz, GFP_KERNEL); |
| 387 | if (big_oops_buf) { |
| 388 | workspace = kmalloc(LZ4_MEM_COMPRESS, GFP_KERNEL); |
| 389 | if (!workspace) { |
| 390 | pr_err("No memory for compression workspace; skipping compression\n"); |
| 391 | kfree(big_oops_buf); |
| 392 | big_oops_buf = NULL; |
| 393 | } |
| 394 | } else { |
| 395 | pr_err("No memory for uncompressed data; skipping compression\n"); |
| 396 | workspace = NULL; |
| 397 | } |
| 398 | } |
| 399 | |
Geliang Tang | 239b716 | 2018-02-13 14:40:39 +0800 | [diff] [blame] | 400 | static const struct pstore_zbackend backend_lz4 = { |
| 401 | .compress = compress_lz4, |
| 402 | .decompress = decompress_lz4, |
| 403 | .allocate = allocate_lz4, |
| 404 | .free = free_lz4, |
| 405 | .name = "lz4", |
| 406 | }; |
| 407 | #endif |
| 408 | |
| 409 | #ifdef CONFIG_PSTORE_LZ4HC_COMPRESS |
| 410 | static int compress_lz4hc(const void *in, void *out, |
| 411 | size_t inlen, size_t outlen) |
| 412 | { |
| 413 | int ret; |
| 414 | |
| 415 | ret = LZ4_compress_HC(in, out, inlen, outlen, |
| 416 | LZ4HC_DEFAULT_CLEVEL, workspace); |
| 417 | if (!ret) { |
| 418 | pr_err("LZ4_compress_HC error; compression failed!\n"); |
| 419 | return -EIO; |
| 420 | } |
| 421 | |
| 422 | return ret; |
| 423 | } |
| 424 | |
| 425 | static void allocate_lz4hc(void) |
| 426 | { |
| 427 | big_oops_buf_sz = LZ4_compressBound(psinfo->bufsize); |
| 428 | big_oops_buf = kmalloc(big_oops_buf_sz, GFP_KERNEL); |
| 429 | if (big_oops_buf) { |
| 430 | workspace = kmalloc(LZ4HC_MEM_COMPRESS, GFP_KERNEL); |
| 431 | if (!workspace) { |
| 432 | pr_err("No memory for compression workspace; skipping compression\n"); |
| 433 | kfree(big_oops_buf); |
| 434 | big_oops_buf = NULL; |
| 435 | } |
| 436 | } else { |
| 437 | pr_err("No memory for uncompressed data; skipping compression\n"); |
| 438 | workspace = NULL; |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | static const struct pstore_zbackend backend_lz4hc = { |
| 443 | .compress = compress_lz4hc, |
| 444 | .decompress = decompress_lz4, |
| 445 | .allocate = allocate_lz4hc, |
| 446 | .free = free_lz4, |
| 447 | .name = "lz4hc", |
| 448 | }; |
| 449 | #endif |
| 450 | |
| 451 | #ifdef CONFIG_PSTORE_842_COMPRESS |
| 452 | static int compress_842(const void *in, void *out, size_t inlen, size_t outlen) |
| 453 | { |
| 454 | int ret; |
Kees Cook | 5559740 | 2018-03-06 15:15:24 -0800 | [diff] [blame^] | 455 | unsigned int size; |
Geliang Tang | 239b716 | 2018-02-13 14:40:39 +0800 | [diff] [blame] | 456 | |
Kees Cook | 5559740 | 2018-03-06 15:15:24 -0800 | [diff] [blame^] | 457 | if (outlen > UINT_MAX) |
| 458 | return -EIO; |
| 459 | size = outlen; |
| 460 | |
| 461 | ret = sw842_compress(in, inlen, out, &size, workspace); |
Geliang Tang | 239b716 | 2018-02-13 14:40:39 +0800 | [diff] [blame] | 462 | if (ret) { |
| 463 | pr_err("sw842_compress error; compression failed!\n"); |
| 464 | return ret; |
| 465 | } |
| 466 | |
Kees Cook | 5559740 | 2018-03-06 15:15:24 -0800 | [diff] [blame^] | 467 | return size; |
Geliang Tang | 239b716 | 2018-02-13 14:40:39 +0800 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | static int decompress_842(void *in, void *out, size_t inlen, size_t outlen) |
| 471 | { |
| 472 | int ret; |
Kees Cook | 5559740 | 2018-03-06 15:15:24 -0800 | [diff] [blame^] | 473 | unsigned int size; |
Geliang Tang | 239b716 | 2018-02-13 14:40:39 +0800 | [diff] [blame] | 474 | |
Kees Cook | 5559740 | 2018-03-06 15:15:24 -0800 | [diff] [blame^] | 475 | if (outlen > UINT_MAX) |
| 476 | return -EIO; |
| 477 | size = outlen; |
| 478 | |
| 479 | ret = sw842_decompress(in, inlen, out, &size); |
Geliang Tang | 239b716 | 2018-02-13 14:40:39 +0800 | [diff] [blame] | 480 | if (ret) { |
| 481 | pr_err("sw842_decompress error, ret = %d!\n", ret); |
| 482 | return ret; |
| 483 | } |
| 484 | |
Kees Cook | 5559740 | 2018-03-06 15:15:24 -0800 | [diff] [blame^] | 485 | return size; |
Geliang Tang | 239b716 | 2018-02-13 14:40:39 +0800 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | static void allocate_842(void) |
| 489 | { |
| 490 | big_oops_buf_sz = psinfo->bufsize; |
| 491 | big_oops_buf = kmalloc(big_oops_buf_sz, GFP_KERNEL); |
| 492 | if (big_oops_buf) { |
| 493 | workspace = kmalloc(SW842_MEM_COMPRESS, GFP_KERNEL); |
| 494 | if (!workspace) { |
| 495 | kfree(big_oops_buf); |
| 496 | big_oops_buf = NULL; |
| 497 | } |
| 498 | } else { |
| 499 | pr_err("No memory for uncompressed data; skipping compression\n"); |
| 500 | workspace = NULL; |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | static void free_842(void) |
Geliang Tang | 8cfc8dd | 2016-02-18 22:04:22 +0800 | [diff] [blame] | 505 | { |
| 506 | kfree(workspace); |
| 507 | kfree(big_oops_buf); |
| 508 | big_oops_buf = NULL; |
| 509 | big_oops_buf_sz = 0; |
| 510 | } |
| 511 | |
Geliang Tang | 239b716 | 2018-02-13 14:40:39 +0800 | [diff] [blame] | 512 | static const struct pstore_zbackend backend_842 = { |
| 513 | .compress = compress_842, |
| 514 | .decompress = decompress_842, |
| 515 | .allocate = allocate_842, |
| 516 | .free = free_842, |
| 517 | .name = "842", |
Geliang Tang | 8cfc8dd | 2016-02-18 22:04:22 +0800 | [diff] [blame] | 518 | }; |
| 519 | #endif |
| 520 | |
Bhumika Goyal | 3faf935 | 2017-02-19 15:07:53 +0530 | [diff] [blame] | 521 | static const struct pstore_zbackend *zbackend = |
Geliang Tang | 8cfc8dd | 2016-02-18 22:04:22 +0800 | [diff] [blame] | 522 | #if defined(CONFIG_PSTORE_ZLIB_COMPRESS) |
| 523 | &backend_zlib; |
| 524 | #elif defined(CONFIG_PSTORE_LZO_COMPRESS) |
| 525 | &backend_lzo; |
| 526 | #elif defined(CONFIG_PSTORE_LZ4_COMPRESS) |
| 527 | &backend_lz4; |
Geliang Tang | 239b716 | 2018-02-13 14:40:39 +0800 | [diff] [blame] | 528 | #elif defined(CONFIG_PSTORE_LZ4HC_COMPRESS) |
| 529 | &backend_lz4hc; |
| 530 | #elif defined(CONFIG_PSTORE_842_COMPRESS) |
| 531 | &backend_842; |
Geliang Tang | 8cfc8dd | 2016-02-18 22:04:22 +0800 | [diff] [blame] | 532 | #else |
| 533 | NULL; |
| 534 | #endif |
| 535 | |
| 536 | static int pstore_compress(const void *in, void *out, |
| 537 | size_t inlen, size_t outlen) |
| 538 | { |
| 539 | if (zbackend) |
| 540 | return zbackend->compress(in, out, inlen, outlen); |
| 541 | else |
| 542 | return -EIO; |
| 543 | } |
| 544 | |
| 545 | static int pstore_decompress(void *in, void *out, size_t inlen, size_t outlen) |
| 546 | { |
| 547 | if (zbackend) |
| 548 | return zbackend->decompress(in, out, inlen, outlen); |
| 549 | else |
| 550 | return -EIO; |
| 551 | } |
| 552 | |
| 553 | static void allocate_buf_for_compression(void) |
| 554 | { |
| 555 | if (zbackend) { |
| 556 | pr_info("using %s compression\n", zbackend->name); |
| 557 | zbackend->allocate(); |
| 558 | } else { |
| 559 | pr_err("allocate compression buffer error!\n"); |
| 560 | } |
| 561 | } |
| 562 | |
| 563 | static void free_buf_for_compression(void) |
| 564 | { |
| 565 | if (zbackend) |
| 566 | zbackend->free(); |
| 567 | else |
| 568 | pr_err("free compression buffer error!\n"); |
Geliang Tang | ee1d267 | 2015-10-20 00:39:03 -0700 | [diff] [blame] | 569 | } |
| 570 | |
Aruna Balakrishnaiah | b0aad7a | 2013-08-16 13:53:10 -0700 | [diff] [blame] | 571 | /* |
| 572 | * Called when compression fails, since the printk buffer |
| 573 | * would be fetched for compression calling it again when |
| 574 | * compression fails would have moved the iterator of |
| 575 | * printk buffer which results in fetching old contents. |
| 576 | * Copy the recent messages from big_oops_buf to psinfo->buf |
| 577 | */ |
| 578 | static size_t copy_kmsg_to_buffer(int hsize, size_t len) |
| 579 | { |
| 580 | size_t total_len; |
| 581 | size_t diff; |
| 582 | |
| 583 | total_len = hsize + len; |
| 584 | |
| 585 | if (total_len > psinfo->bufsize) { |
| 586 | diff = total_len - psinfo->bufsize + hsize; |
| 587 | memcpy(psinfo->buf, big_oops_buf, hsize); |
| 588 | memcpy(psinfo->buf + hsize, big_oops_buf + diff, |
| 589 | psinfo->bufsize - hsize); |
| 590 | total_len = psinfo->bufsize; |
| 591 | } else |
| 592 | memcpy(psinfo->buf, big_oops_buf, total_len); |
| 593 | |
| 594 | return total_len; |
| 595 | } |
| 596 | |
Kees Cook | e581ca8 | 2017-05-19 15:10:31 -0700 | [diff] [blame] | 597 | void pstore_record_init(struct pstore_record *record, |
| 598 | struct pstore_info *psinfo) |
| 599 | { |
| 600 | memset(record, 0, sizeof(*record)); |
| 601 | |
| 602 | record->psi = psinfo; |
Kees Cook | c7f3c595 | 2017-05-19 15:29:10 -0700 | [diff] [blame] | 603 | |
| 604 | /* Report zeroed timestamp if called before timekeeping has resumed. */ |
Arnd Bergmann | df27067 | 2017-11-10 16:25:04 +0100 | [diff] [blame] | 605 | record->time = ns_to_timespec(ktime_get_real_fast_ns()); |
Kees Cook | e581ca8 | 2017-05-19 15:10:31 -0700 | [diff] [blame] | 606 | } |
| 607 | |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 608 | /* |
| 609 | * callback from kmsg_dump. (s2,l2) has the most recently |
| 610 | * written bytes, older bytes are in (s1,l1). Save as much |
| 611 | * as we can from the end of the buffer. |
| 612 | */ |
| 613 | static void pstore_dump(struct kmsg_dumper *dumper, |
Kay Sievers | e2ae715 | 2012-06-15 14:07:51 +0200 | [diff] [blame] | 614 | enum kmsg_dump_reason reason) |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 615 | { |
Kay Sievers | e2ae715 | 2012-06-15 14:07:51 +0200 | [diff] [blame] | 616 | unsigned long total = 0; |
Seiji Aguchi | 381b872 | 2012-03-16 15:36:59 -0700 | [diff] [blame] | 617 | const char *why; |
Matthew Garrett | b94fdd0 | 2011-07-21 16:57:54 -0400 | [diff] [blame] | 618 | unsigned int part = 1; |
Don Zickus | abd4d55 | 2011-08-12 10:54:51 -0700 | [diff] [blame] | 619 | unsigned long flags = 0; |
Namhyung Kim | 98e44fda | 2016-05-18 21:00:05 +0900 | [diff] [blame] | 620 | int is_locked; |
Kay Sievers | e2ae715 | 2012-06-15 14:07:51 +0200 | [diff] [blame] | 621 | int ret; |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 622 | |
Seiji Aguchi | 381b872 | 2012-03-16 15:36:59 -0700 | [diff] [blame] | 623 | why = get_reason_str(reason); |
Tony Luck | 9f6af27 | 2011-03-22 16:01:49 -0700 | [diff] [blame] | 624 | |
Seiji Aguchi | 9f244e9 | 2013-01-11 18:09:41 +0000 | [diff] [blame] | 625 | if (pstore_cannot_block_path(reason)) { |
| 626 | is_locked = spin_trylock_irqsave(&psinfo->buf_lock, flags); |
| 627 | if (!is_locked) { |
| 628 | pr_err("pstore dump routine blocked in %s path, may corrupt error record\n" |
| 629 | , in_nmi() ? "NMI" : why); |
Li Pengcheng | 959217c | 2016-11-05 10:15:59 +0800 | [diff] [blame] | 630 | return; |
Seiji Aguchi | 9f244e9 | 2013-01-11 18:09:41 +0000 | [diff] [blame] | 631 | } |
Namhyung Kim | 98e44fda | 2016-05-18 21:00:05 +0900 | [diff] [blame] | 632 | } else { |
Don Zickus | abd4d55 | 2011-08-12 10:54:51 -0700 | [diff] [blame] | 633 | spin_lock_irqsave(&psinfo->buf_lock, flags); |
Namhyung Kim | 98e44fda | 2016-05-18 21:00:05 +0900 | [diff] [blame] | 634 | is_locked = 1; |
| 635 | } |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 636 | oopscount++; |
| 637 | while (total < kmsg_bytes) { |
Kay Sievers | e2ae715 | 2012-06-15 14:07:51 +0200 | [diff] [blame] | 638 | char *dst; |
Kees Cook | 76cc958 | 2017-03-03 23:28:53 -0800 | [diff] [blame] | 639 | size_t dst_size; |
| 640 | int header_size; |
Aruna Balakrishnaiah | b0aad7a | 2013-08-16 13:53:10 -0700 | [diff] [blame] | 641 | int zipped_len = -1; |
Kees Cook | 76cc958 | 2017-03-03 23:28:53 -0800 | [diff] [blame] | 642 | size_t dump_size; |
Kees Cook | e581ca8 | 2017-05-19 15:10:31 -0700 | [diff] [blame] | 643 | struct pstore_record record; |
| 644 | |
| 645 | pstore_record_init(&record, psinfo); |
| 646 | record.type = PSTORE_TYPE_DMESG; |
| 647 | record.count = oopscount; |
| 648 | record.reason = reason; |
| 649 | record.part = part; |
| 650 | record.buf = psinfo->buf; |
Kay Sievers | e2ae715 | 2012-06-15 14:07:51 +0200 | [diff] [blame] | 651 | |
Konstantin Khlebnikov | f0e2efc | 2015-05-21 09:26:19 -0700 | [diff] [blame] | 652 | if (big_oops_buf && is_locked) { |
Aruna Balakrishnaiah | b0aad7a | 2013-08-16 13:53:10 -0700 | [diff] [blame] | 653 | dst = big_oops_buf; |
Kees Cook | 76cc958 | 2017-03-03 23:28:53 -0800 | [diff] [blame] | 654 | dst_size = big_oops_buf_sz; |
Namhyung Kim | 235f6d1 | 2016-05-18 21:00:06 +0900 | [diff] [blame] | 655 | } else { |
| 656 | dst = psinfo->buf; |
Kees Cook | 76cc958 | 2017-03-03 23:28:53 -0800 | [diff] [blame] | 657 | dst_size = psinfo->bufsize; |
Namhyung Kim | 235f6d1 | 2016-05-18 21:00:06 +0900 | [diff] [blame] | 658 | } |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 659 | |
Kees Cook | 76cc958 | 2017-03-03 23:28:53 -0800 | [diff] [blame] | 660 | /* Write dump header. */ |
| 661 | header_size = snprintf(dst, dst_size, "%s#%d Part%u\n", why, |
| 662 | oopscount, part); |
| 663 | dst_size -= header_size; |
Aruna Balakrishnaiah | b0aad7a | 2013-08-16 13:53:10 -0700 | [diff] [blame] | 664 | |
Kees Cook | 76cc958 | 2017-03-03 23:28:53 -0800 | [diff] [blame] | 665 | /* Write dump contents. */ |
| 666 | if (!kmsg_dump_get_buffer(dumper, true, dst + header_size, |
| 667 | dst_size, &dump_size)) |
Namhyung Kim | 235f6d1 | 2016-05-18 21:00:06 +0900 | [diff] [blame] | 668 | break; |
| 669 | |
| 670 | if (big_oops_buf && is_locked) { |
Aruna Balakrishnaiah | b0aad7a | 2013-08-16 13:53:10 -0700 | [diff] [blame] | 671 | zipped_len = pstore_compress(dst, psinfo->buf, |
Kees Cook | 76cc958 | 2017-03-03 23:28:53 -0800 | [diff] [blame] | 672 | header_size + dump_size, |
| 673 | psinfo->bufsize); |
Aruna Balakrishnaiah | b0aad7a | 2013-08-16 13:53:10 -0700 | [diff] [blame] | 674 | |
| 675 | if (zipped_len > 0) { |
Kees Cook | 76cc958 | 2017-03-03 23:28:53 -0800 | [diff] [blame] | 676 | record.compressed = true; |
| 677 | record.size = zipped_len; |
Aruna Balakrishnaiah | b0aad7a | 2013-08-16 13:53:10 -0700 | [diff] [blame] | 678 | } else { |
Kees Cook | 76cc958 | 2017-03-03 23:28:53 -0800 | [diff] [blame] | 679 | record.size = copy_kmsg_to_buffer(header_size, |
| 680 | dump_size); |
Aruna Balakrishnaiah | b0aad7a | 2013-08-16 13:53:10 -0700 | [diff] [blame] | 681 | } |
| 682 | } else { |
Kees Cook | 76cc958 | 2017-03-03 23:28:53 -0800 | [diff] [blame] | 683 | record.size = header_size + dump_size; |
Aruna Balakrishnaiah | b0aad7a | 2013-08-16 13:53:10 -0700 | [diff] [blame] | 684 | } |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 685 | |
Kees Cook | 76cc958 | 2017-03-03 23:28:53 -0800 | [diff] [blame] | 686 | ret = psinfo->write(&record); |
Chen Gong | b238b8f | 2011-10-12 09:17:24 -0700 | [diff] [blame] | 687 | if (ret == 0 && reason == KMSG_DUMP_OOPS && pstore_is_mounted()) |
Luck, Tony | 6dda926 | 2011-08-11 15:14:39 -0700 | [diff] [blame] | 688 | pstore_new_entry = 1; |
Kay Sievers | e2ae715 | 2012-06-15 14:07:51 +0200 | [diff] [blame] | 689 | |
Kees Cook | 76cc958 | 2017-03-03 23:28:53 -0800 | [diff] [blame] | 690 | total += record.size; |
Matthew Garrett | 5628068 | 2011-07-21 16:57:53 -0400 | [diff] [blame] | 691 | part++; |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 692 | } |
Namhyung Kim | 98e44fda | 2016-05-18 21:00:05 +0900 | [diff] [blame] | 693 | if (is_locked) |
Don Zickus | abd4d55 | 2011-08-12 10:54:51 -0700 | [diff] [blame] | 694 | spin_unlock_irqrestore(&psinfo->buf_lock, flags); |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 695 | } |
| 696 | |
| 697 | static struct kmsg_dumper pstore_dumper = { |
| 698 | .dump = pstore_dump, |
| 699 | }; |
| 700 | |
Geliang Tang | 306e5c2 | 2015-10-31 23:23:15 +0800 | [diff] [blame] | 701 | /* |
| 702 | * Register with kmsg_dump to save last part of console log on panic. |
| 703 | */ |
Geliang Tang | 1873041 | 2015-10-20 00:39:02 -0700 | [diff] [blame] | 704 | static void pstore_register_kmsg(void) |
| 705 | { |
| 706 | kmsg_dump_register(&pstore_dumper); |
| 707 | } |
| 708 | |
Geliang Tang | ee1d267 | 2015-10-20 00:39:03 -0700 | [diff] [blame] | 709 | static void pstore_unregister_kmsg(void) |
| 710 | { |
| 711 | kmsg_dump_unregister(&pstore_dumper); |
| 712 | } |
| 713 | |
Anton Vorontsov | f29e595 | 2012-05-26 06:20:19 -0700 | [diff] [blame] | 714 | #ifdef CONFIG_PSTORE_CONSOLE |
| 715 | static void pstore_console_write(struct console *con, const char *s, unsigned c) |
| 716 | { |
| 717 | const char *e = s + c; |
| 718 | |
| 719 | while (s < e) { |
Kees Cook | e581ca8 | 2017-05-19 15:10:31 -0700 | [diff] [blame] | 720 | struct pstore_record record; |
Anton Vorontsov | f29e595 | 2012-05-26 06:20:19 -0700 | [diff] [blame] | 721 | unsigned long flags; |
| 722 | |
Kees Cook | e581ca8 | 2017-05-19 15:10:31 -0700 | [diff] [blame] | 723 | pstore_record_init(&record, psinfo); |
| 724 | record.type = PSTORE_TYPE_CONSOLE; |
| 725 | |
Anton Vorontsov | f29e595 | 2012-05-26 06:20:19 -0700 | [diff] [blame] | 726 | if (c > psinfo->bufsize) |
| 727 | c = psinfo->bufsize; |
Chuansheng Liu | 80c9d03 | 2012-09-18 01:43:44 +0800 | [diff] [blame] | 728 | |
| 729 | if (oops_in_progress) { |
| 730 | if (!spin_trylock_irqsave(&psinfo->buf_lock, flags)) |
| 731 | break; |
| 732 | } else { |
| 733 | spin_lock_irqsave(&psinfo->buf_lock, flags); |
| 734 | } |
Kees Cook | b10b471 | 2017-03-05 00:27:54 -0800 | [diff] [blame] | 735 | record.buf = (char *)s; |
| 736 | record.size = c; |
Kees Cook | 4c9ec21 | 2017-03-05 22:41:10 -0800 | [diff] [blame] | 737 | psinfo->write(&record); |
Anton Vorontsov | f29e595 | 2012-05-26 06:20:19 -0700 | [diff] [blame] | 738 | spin_unlock_irqrestore(&psinfo->buf_lock, flags); |
| 739 | s += c; |
| 740 | c = e - s; |
| 741 | } |
| 742 | } |
| 743 | |
| 744 | static struct console pstore_console = { |
| 745 | .name = "pstore", |
| 746 | .write = pstore_console_write, |
| 747 | .flags = CON_PRINTBUFFER | CON_ENABLED | CON_ANYTIME, |
| 748 | .index = -1, |
| 749 | }; |
| 750 | |
| 751 | static void pstore_register_console(void) |
| 752 | { |
| 753 | register_console(&pstore_console); |
| 754 | } |
Geliang Tang | ee1d267 | 2015-10-20 00:39:03 -0700 | [diff] [blame] | 755 | |
| 756 | static void pstore_unregister_console(void) |
| 757 | { |
| 758 | unregister_console(&pstore_console); |
| 759 | } |
Anton Vorontsov | f29e595 | 2012-05-26 06:20:19 -0700 | [diff] [blame] | 760 | #else |
| 761 | static void pstore_register_console(void) {} |
Geliang Tang | ee1d267 | 2015-10-20 00:39:03 -0700 | [diff] [blame] | 762 | static void pstore_unregister_console(void) {} |
Anton Vorontsov | f29e595 | 2012-05-26 06:20:19 -0700 | [diff] [blame] | 763 | #endif |
| 764 | |
Kees Cook | 4c9ec21 | 2017-03-05 22:41:10 -0800 | [diff] [blame] | 765 | static int pstore_write_user_compat(struct pstore_record *record, |
| 766 | const char __user *buf) |
Mark Salyzyn | 5bf6d1b | 2016-09-01 08:13:46 -0700 | [diff] [blame] | 767 | { |
Kees Cook | 30800d9 | 2017-03-07 13:57:11 -0800 | [diff] [blame] | 768 | int ret = 0; |
Mark Salyzyn | 5bf6d1b | 2016-09-01 08:13:46 -0700 | [diff] [blame] | 769 | |
Kees Cook | 30800d9 | 2017-03-07 13:57:11 -0800 | [diff] [blame] | 770 | if (record->buf) |
| 771 | return -EINVAL; |
Mark Salyzyn | 5bf6d1b | 2016-09-01 08:13:46 -0700 | [diff] [blame] | 772 | |
Geliang Tang | 077090a | 2017-04-29 09:45:16 +0800 | [diff] [blame] | 773 | record->buf = memdup_user(buf, record->size); |
Hirofumi Nakagawa | dfd6fa3 | 2017-09-26 03:21:27 +0900 | [diff] [blame] | 774 | if (IS_ERR(record->buf)) { |
Geliang Tang | 077090a | 2017-04-29 09:45:16 +0800 | [diff] [blame] | 775 | ret = PTR_ERR(record->buf); |
Kees Cook | 30800d9 | 2017-03-07 13:57:11 -0800 | [diff] [blame] | 776 | goto out; |
Mark Salyzyn | 5bf6d1b | 2016-09-01 08:13:46 -0700 | [diff] [blame] | 777 | } |
Kees Cook | 30800d9 | 2017-03-07 13:57:11 -0800 | [diff] [blame] | 778 | |
| 779 | ret = record->psi->write(record); |
| 780 | |
Kees Cook | 30800d9 | 2017-03-07 13:57:11 -0800 | [diff] [blame] | 781 | kfree(record->buf); |
Geliang Tang | 077090a | 2017-04-29 09:45:16 +0800 | [diff] [blame] | 782 | out: |
Kees Cook | 30800d9 | 2017-03-07 13:57:11 -0800 | [diff] [blame] | 783 | record->buf = NULL; |
| 784 | |
| 785 | return unlikely(ret < 0) ? ret : record->size; |
Mark Salyzyn | 5bf6d1b | 2016-09-01 08:13:46 -0700 | [diff] [blame] | 786 | } |
| 787 | |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 788 | /* |
| 789 | * platform specific persistent storage driver registers with |
| 790 | * us here. If pstore is already mounted, call the platform |
| 791 | * read function right away to populate the file system. If not |
| 792 | * then the pstore mount code will call us later to fill out |
| 793 | * the file system. |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 794 | */ |
| 795 | int pstore_register(struct pstore_info *psi) |
| 796 | { |
| 797 | struct module *owner = psi->owner; |
| 798 | |
Kees Cook | 0d7cd09 | 2017-03-03 12:11:40 -0800 | [diff] [blame] | 799 | if (backend && strcmp(backend, psi->name)) { |
| 800 | pr_warn("ignoring unexpected backend '%s'\n", psi->name); |
Lenny Szubowicz | 8e48b1a | 2013-06-28 17:11:33 -0400 | [diff] [blame] | 801 | return -EPERM; |
Kees Cook | 0d7cd09 | 2017-03-03 12:11:40 -0800 | [diff] [blame] | 802 | } |
Lenny Szubowicz | 8e48b1a | 2013-06-28 17:11:33 -0400 | [diff] [blame] | 803 | |
Kees Cook | 4c9ec21 | 2017-03-05 22:41:10 -0800 | [diff] [blame] | 804 | /* Sanity check flags. */ |
| 805 | if (!psi->flags) { |
| 806 | pr_warn("backend '%s' must support at least one frontend\n", |
| 807 | psi->name); |
| 808 | return -EINVAL; |
| 809 | } |
| 810 | |
| 811 | /* Check for required functions. */ |
| 812 | if (!psi->read || !psi->write) { |
| 813 | pr_warn("backend '%s' must implement read() and write()\n", |
| 814 | psi->name); |
| 815 | return -EINVAL; |
| 816 | } |
| 817 | |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 818 | spin_lock(&pstore_lock); |
| 819 | if (psinfo) { |
Kees Cook | 0d7cd09 | 2017-03-03 12:11:40 -0800 | [diff] [blame] | 820 | pr_warn("backend '%s' already loaded: ignoring '%s'\n", |
| 821 | psinfo->name, psi->name); |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 822 | spin_unlock(&pstore_lock); |
| 823 | return -EBUSY; |
| 824 | } |
Matthew Garrett | dee28e7 | 2011-07-21 16:57:55 -0400 | [diff] [blame] | 825 | |
Kees Cook | 4c9ec21 | 2017-03-05 22:41:10 -0800 | [diff] [blame] | 826 | if (!psi->write_user) |
| 827 | psi->write_user = pstore_write_user_compat; |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 828 | psinfo = psi; |
Kees Cook | f6f8285 | 2011-11-17 12:58:07 -0800 | [diff] [blame] | 829 | mutex_init(&psinfo->read_mutex); |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 830 | spin_unlock(&pstore_lock); |
| 831 | |
| 832 | if (owner && !try_module_get(owner)) { |
| 833 | psinfo = NULL; |
| 834 | return -EINVAL; |
| 835 | } |
| 836 | |
Aruna Balakrishnaiah | b0aad7a | 2013-08-16 13:53:10 -0700 | [diff] [blame] | 837 | allocate_buf_for_compression(); |
| 838 | |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 839 | if (pstore_is_mounted()) |
Luck, Tony | 6dda926 | 2011-08-11 15:14:39 -0700 | [diff] [blame] | 840 | pstore_get_records(0); |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 841 | |
Namhyung Kim | c950fd6 | 2016-07-28 00:08:25 +0900 | [diff] [blame] | 842 | if (psi->flags & PSTORE_FLAGS_DMESG) |
| 843 | pstore_register_kmsg(); |
| 844 | if (psi->flags & PSTORE_FLAGS_CONSOLE) |
Luck, Tony | df36ac1 | 2013-12-18 15:17:10 -0800 | [diff] [blame] | 845 | pstore_register_console(); |
Namhyung Kim | c950fd6 | 2016-07-28 00:08:25 +0900 | [diff] [blame] | 846 | if (psi->flags & PSTORE_FLAGS_FTRACE) |
Luck, Tony | df36ac1 | 2013-12-18 15:17:10 -0800 | [diff] [blame] | 847 | pstore_register_ftrace(); |
Namhyung Kim | c950fd6 | 2016-07-28 00:08:25 +0900 | [diff] [blame] | 848 | if (psi->flags & PSTORE_FLAGS_PMSG) |
Mark Salyzyn | 9d5438f | 2015-01-16 16:01:10 -0800 | [diff] [blame] | 849 | pstore_register_pmsg(); |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 850 | |
Kees Cook | 6330d55 | 2017-03-06 12:42:12 -0800 | [diff] [blame] | 851 | /* Start watching for new records, if desired. */ |
Anton Vorontsov | a3f5f07 | 2012-05-26 06:20:28 -0700 | [diff] [blame] | 852 | if (pstore_update_ms >= 0) { |
| 853 | pstore_timer.expires = jiffies + |
| 854 | msecs_to_jiffies(pstore_update_ms); |
| 855 | add_timer(&pstore_timer); |
| 856 | } |
Luck, Tony | 6dda926 | 2011-08-11 15:14:39 -0700 | [diff] [blame] | 857 | |
Wang Long | 42222c2 | 2015-05-21 09:34:22 -0700 | [diff] [blame] | 858 | /* |
| 859 | * Update the module parameter backend, so it is visible |
| 860 | * through /sys/module/pstore/parameters/backend |
| 861 | */ |
| 862 | backend = psi->name; |
| 863 | |
Fabian Frederick | ef74885 | 2014-06-06 14:37:31 -0700 | [diff] [blame] | 864 | pr_info("Registered %s as persistent store backend\n", psi->name); |
Lenny Szubowicz | 8e48b1a | 2013-06-28 17:11:33 -0400 | [diff] [blame] | 865 | |
Kees Cook | 1344dd8 | 2017-03-03 17:45:38 -0800 | [diff] [blame] | 866 | module_put(owner); |
| 867 | |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 868 | return 0; |
| 869 | } |
| 870 | EXPORT_SYMBOL_GPL(pstore_register); |
| 871 | |
Geliang Tang | ee1d267 | 2015-10-20 00:39:03 -0700 | [diff] [blame] | 872 | void pstore_unregister(struct pstore_info *psi) |
| 873 | { |
Kees Cook | 6330d55 | 2017-03-06 12:42:12 -0800 | [diff] [blame] | 874 | /* Stop timer and make sure all work has finished. */ |
| 875 | pstore_update_ms = -1; |
| 876 | del_timer_sync(&pstore_timer); |
| 877 | flush_work(&pstore_work); |
| 878 | |
Namhyung Kim | c950fd6 | 2016-07-28 00:08:25 +0900 | [diff] [blame] | 879 | if (psi->flags & PSTORE_FLAGS_PMSG) |
Kees Cook | a1db806 | 2016-05-19 10:59:03 -0400 | [diff] [blame] | 880 | pstore_unregister_pmsg(); |
Namhyung Kim | c950fd6 | 2016-07-28 00:08:25 +0900 | [diff] [blame] | 881 | if (psi->flags & PSTORE_FLAGS_FTRACE) |
Kees Cook | a1db806 | 2016-05-19 10:59:03 -0400 | [diff] [blame] | 882 | pstore_unregister_ftrace(); |
Namhyung Kim | c950fd6 | 2016-07-28 00:08:25 +0900 | [diff] [blame] | 883 | if (psi->flags & PSTORE_FLAGS_CONSOLE) |
Kees Cook | a1db806 | 2016-05-19 10:59:03 -0400 | [diff] [blame] | 884 | pstore_unregister_console(); |
Namhyung Kim | c950fd6 | 2016-07-28 00:08:25 +0900 | [diff] [blame] | 885 | if (psi->flags & PSTORE_FLAGS_DMESG) |
| 886 | pstore_unregister_kmsg(); |
Geliang Tang | ee1d267 | 2015-10-20 00:39:03 -0700 | [diff] [blame] | 887 | |
| 888 | free_buf_for_compression(); |
| 889 | |
| 890 | psinfo = NULL; |
| 891 | backend = NULL; |
| 892 | } |
| 893 | EXPORT_SYMBOL_GPL(pstore_unregister); |
| 894 | |
Kees Cook | 634f8f5 | 2017-03-03 17:35:25 -0800 | [diff] [blame] | 895 | static void decompress_record(struct pstore_record *record) |
| 896 | { |
| 897 | int unzipped_len; |
Kees Cook | 7e8cc8d | 2017-03-04 22:28:46 -0800 | [diff] [blame] | 898 | char *decompressed; |
Kees Cook | 634f8f5 | 2017-03-03 17:35:25 -0800 | [diff] [blame] | 899 | |
Ankit Kumar | 4a16d1c | 2017-05-23 11:16:52 +0530 | [diff] [blame] | 900 | if (!record->compressed) |
| 901 | return; |
| 902 | |
Kees Cook | 634f8f5 | 2017-03-03 17:35:25 -0800 | [diff] [blame] | 903 | /* Only PSTORE_TYPE_DMESG support compression. */ |
Ankit Kumar | 4a16d1c | 2017-05-23 11:16:52 +0530 | [diff] [blame] | 904 | if (record->type != PSTORE_TYPE_DMESG) { |
Kees Cook | 634f8f5 | 2017-03-03 17:35:25 -0800 | [diff] [blame] | 905 | pr_warn("ignored compressed record type %d\n", record->type); |
| 906 | return; |
| 907 | } |
| 908 | |
| 909 | /* No compression method has created the common buffer. */ |
| 910 | if (!big_oops_buf) { |
| 911 | pr_warn("no decompression buffer allocated\n"); |
| 912 | return; |
| 913 | } |
| 914 | |
| 915 | unzipped_len = pstore_decompress(record->buf, big_oops_buf, |
| 916 | record->size, big_oops_buf_sz); |
Kees Cook | 7e8cc8d | 2017-03-04 22:28:46 -0800 | [diff] [blame] | 917 | if (unzipped_len <= 0) { |
Kees Cook | 634f8f5 | 2017-03-03 17:35:25 -0800 | [diff] [blame] | 918 | pr_err("decompression failed: %d\n", unzipped_len); |
Kees Cook | 7e8cc8d | 2017-03-04 22:28:46 -0800 | [diff] [blame] | 919 | return; |
| 920 | } |
| 921 | |
| 922 | /* Build new buffer for decompressed contents. */ |
| 923 | decompressed = kmalloc(unzipped_len + record->ecc_notice_size, |
| 924 | GFP_KERNEL); |
| 925 | if (!decompressed) { |
| 926 | pr_err("decompression ran out of memory\n"); |
| 927 | return; |
| 928 | } |
| 929 | memcpy(decompressed, big_oops_buf, unzipped_len); |
| 930 | |
| 931 | /* Append ECC notice to decompressed buffer. */ |
| 932 | memcpy(decompressed + unzipped_len, record->buf + record->size, |
| 933 | record->ecc_notice_size); |
| 934 | |
| 935 | /* Swap out compresed contents with decompressed contents. */ |
| 936 | kfree(record->buf); |
| 937 | record->buf = decompressed; |
| 938 | record->size = unzipped_len; |
| 939 | record->compressed = false; |
Kees Cook | 634f8f5 | 2017-03-03 17:35:25 -0800 | [diff] [blame] | 940 | } |
| 941 | |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 942 | /* |
Kees Cook | 3a7d2fd | 2017-04-27 15:53:21 -0700 | [diff] [blame] | 943 | * Read all the records from one persistent store backend. Create |
Luck, Tony | 6dda926 | 2011-08-11 15:14:39 -0700 | [diff] [blame] | 944 | * files in our filesystem. Don't warn about -EEXIST errors |
| 945 | * when we are re-scanning the backing store looking to add new |
| 946 | * error records. |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 947 | */ |
Kees Cook | 3a7d2fd | 2017-04-27 15:53:21 -0700 | [diff] [blame] | 948 | void pstore_get_backend_records(struct pstore_info *psi, |
| 949 | struct dentry *root, int quiet) |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 950 | { |
Kees Cook | 2a2b0ac | 2017-03-04 22:57:26 -0800 | [diff] [blame] | 951 | int failed = 0; |
Kees Cook | 656de42 | 2017-05-16 12:03:31 -0700 | [diff] [blame] | 952 | unsigned int stop_loop = 65536; |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 953 | |
Kees Cook | 3a7d2fd | 2017-04-27 15:53:21 -0700 | [diff] [blame] | 954 | if (!psi || !root) |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 955 | return; |
| 956 | |
Kees Cook | f6f8285 | 2011-11-17 12:58:07 -0800 | [diff] [blame] | 957 | mutex_lock(&psi->read_mutex); |
Kees Cook | 2174f6d | 2011-11-18 13:49:00 -0800 | [diff] [blame] | 958 | if (psi->open && psi->open(psi)) |
Chen Gong | 06cf91b | 2011-05-16 11:00:27 -0700 | [diff] [blame] | 959 | goto out; |
| 960 | |
Kees Cook | 1dfff7d | 2017-03-04 22:46:41 -0800 | [diff] [blame] | 961 | /* |
| 962 | * Backend callback read() allocates record.buf. decompress_record() |
| 963 | * may reallocate record.buf. On success, pstore_mkfile() will keep |
| 964 | * the record.buf, so free it only on failure. |
| 965 | */ |
Kees Cook | 656de42 | 2017-05-16 12:03:31 -0700 | [diff] [blame] | 966 | for (; stop_loop; stop_loop--) { |
Kees Cook | 2a2b0ac | 2017-03-04 22:57:26 -0800 | [diff] [blame] | 967 | struct pstore_record *record; |
| 968 | int rc; |
| 969 | |
| 970 | record = kzalloc(sizeof(*record), GFP_KERNEL); |
| 971 | if (!record) { |
| 972 | pr_err("out of memory creating record\n"); |
| 973 | break; |
| 974 | } |
Kees Cook | e581ca8 | 2017-05-19 15:10:31 -0700 | [diff] [blame] | 975 | pstore_record_init(record, psi); |
Kees Cook | 2a2b0ac | 2017-03-04 22:57:26 -0800 | [diff] [blame] | 976 | |
| 977 | record->size = psi->read(record); |
| 978 | |
| 979 | /* No more records left in backend? */ |
Douglas Anderson | f6525b9 | 2017-05-30 15:50:38 -0700 | [diff] [blame] | 980 | if (record->size <= 0) { |
| 981 | kfree(record); |
Kees Cook | 2a2b0ac | 2017-03-04 22:57:26 -0800 | [diff] [blame] | 982 | break; |
Douglas Anderson | f6525b9 | 2017-05-30 15:50:38 -0700 | [diff] [blame] | 983 | } |
Kees Cook | 2a2b0ac | 2017-03-04 22:57:26 -0800 | [diff] [blame] | 984 | |
| 985 | decompress_record(record); |
Kees Cook | 3a7d2fd | 2017-04-27 15:53:21 -0700 | [diff] [blame] | 986 | rc = pstore_mkfile(root, record); |
Kees Cook | 1dfff7d | 2017-03-04 22:46:41 -0800 | [diff] [blame] | 987 | if (rc) { |
Kees Cook | 83f70f0 | 2017-03-04 23:12:24 -0800 | [diff] [blame] | 988 | /* pstore_mkfile() did not take record, so free it. */ |
Kees Cook | 2a2b0ac | 2017-03-04 22:57:26 -0800 | [diff] [blame] | 989 | kfree(record->buf); |
Kees Cook | 83f70f0 | 2017-03-04 23:12:24 -0800 | [diff] [blame] | 990 | kfree(record); |
Kees Cook | 1dfff7d | 2017-03-04 22:46:41 -0800 | [diff] [blame] | 991 | if (rc != -EEXIST || !quiet) |
| 992 | failed++; |
| 993 | } |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 994 | } |
Kees Cook | 2174f6d | 2011-11-18 13:49:00 -0800 | [diff] [blame] | 995 | if (psi->close) |
| 996 | psi->close(psi); |
Chen Gong | 06cf91b | 2011-05-16 11:00:27 -0700 | [diff] [blame] | 997 | out: |
Kees Cook | f6f8285 | 2011-11-17 12:58:07 -0800 | [diff] [blame] | 998 | mutex_unlock(&psi->read_mutex); |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 999 | |
| 1000 | if (failed) |
Kees Cook | 656de42 | 2017-05-16 12:03:31 -0700 | [diff] [blame] | 1001 | pr_warn("failed to create %d record(s) from '%s'\n", |
Fabian Frederick | ef74885 | 2014-06-06 14:37:31 -0700 | [diff] [blame] | 1002 | failed, psi->name); |
Kees Cook | 656de42 | 2017-05-16 12:03:31 -0700 | [diff] [blame] | 1003 | if (!stop_loop) |
| 1004 | pr_err("looping? Too many records seen from '%s'\n", |
| 1005 | psi->name); |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 1006 | } |
| 1007 | |
Luck, Tony | 6dda926 | 2011-08-11 15:14:39 -0700 | [diff] [blame] | 1008 | static void pstore_dowork(struct work_struct *work) |
| 1009 | { |
| 1010 | pstore_get_records(1); |
| 1011 | } |
| 1012 | |
Kees Cook | 24ed960 | 2017-08-28 11:28:21 -0700 | [diff] [blame] | 1013 | static void pstore_timefunc(struct timer_list *unused) |
Luck, Tony | 6dda926 | 2011-08-11 15:14:39 -0700 | [diff] [blame] | 1014 | { |
| 1015 | if (pstore_new_entry) { |
| 1016 | pstore_new_entry = 0; |
| 1017 | schedule_work(&pstore_work); |
| 1018 | } |
| 1019 | |
Kees Cook | 6330d55 | 2017-03-06 12:42:12 -0800 | [diff] [blame] | 1020 | if (pstore_update_ms >= 0) |
| 1021 | mod_timer(&pstore_timer, |
| 1022 | jiffies + msecs_to_jiffies(pstore_update_ms)); |
Luck, Tony | 6dda926 | 2011-08-11 15:14:39 -0700 | [diff] [blame] | 1023 | } |
| 1024 | |
Matthew Garrett | dee28e7 | 2011-07-21 16:57:55 -0400 | [diff] [blame] | 1025 | module_param(backend, charp, 0444); |
| 1026 | MODULE_PARM_DESC(backend, "Pstore backend to use"); |