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> |
Arnd Bergmann | 58eb5b67 | 2018-03-15 16:34:08 +0100 | [diff] [blame] | 31 | #if IS_ENABLED(CONFIG_PSTORE_LZO_COMPRESS) |
Geliang Tang | 8cfc8dd | 2016-02-18 22:04:22 +0800 | [diff] [blame] | 32 | #include <linux/lzo.h> |
| 33 | #endif |
Arnd Bergmann | 58eb5b67 | 2018-03-15 16:34:08 +0100 | [diff] [blame] | 34 | #if IS_ENABLED(CONFIG_PSTORE_LZ4_COMPRESS) || IS_ENABLED(CONFIG_PSTORE_LZ4HC_COMPRESS) |
Geliang Tang | 8cfc8dd | 2016-02-18 22:04:22 +0800 | [diff] [blame] | 35 | #include <linux/lz4.h> |
| 36 | #endif |
Geliang Tang | 1021bcf | 2018-08-01 19:23:37 +0800 | [diff] [blame] | 37 | #if IS_ENABLED(CONFIG_PSTORE_ZSTD_COMPRESS) |
| 38 | #include <linux/zstd.h> |
| 39 | #endif |
Geliang Tang | cb3bee0 | 2018-03-09 18:51:07 +0800 | [diff] [blame] | 40 | #include <linux/crypto.h> |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 41 | #include <linux/string.h> |
Luck, Tony | 6dda926 | 2011-08-11 15:14:39 -0700 | [diff] [blame] | 42 | #include <linux/timer.h> |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 43 | #include <linux/slab.h> |
| 44 | #include <linux/uaccess.h> |
Anton Vorontsov | a3f5f07 | 2012-05-26 06:20:28 -0700 | [diff] [blame] | 45 | #include <linux/jiffies.h> |
Luck, Tony | 6dda926 | 2011-08-11 15:14:39 -0700 | [diff] [blame] | 46 | #include <linux/workqueue.h> |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 47 | |
| 48 | #include "internal.h" |
| 49 | |
| 50 | /* |
Luck, Tony | 6dda926 | 2011-08-11 15:14:39 -0700 | [diff] [blame] | 51 | * We defer making "oops" entries appear in pstore - see |
| 52 | * whether the system is actually still running well enough |
| 53 | * to let someone see the entry |
| 54 | */ |
Anton Vorontsov | 521f7288 | 2012-05-26 06:20:29 -0700 | [diff] [blame] | 55 | static int pstore_update_ms = -1; |
Anton Vorontsov | a3f5f07 | 2012-05-26 06:20:28 -0700 | [diff] [blame] | 56 | module_param_named(update_ms, pstore_update_ms, int, 0600); |
| 57 | MODULE_PARM_DESC(update_ms, "milliseconds before pstore updates its content " |
Anton Vorontsov | 521f7288 | 2012-05-26 06:20:29 -0700 | [diff] [blame] | 58 | "(default is -1, which means runtime updates are disabled; " |
| 59 | "enabling this option is not safe, it may lead to further " |
| 60 | "corruption on Oopses)"); |
Luck, Tony | 6dda926 | 2011-08-11 15:14:39 -0700 | [diff] [blame] | 61 | |
| 62 | static int pstore_new_entry; |
| 63 | |
Kees Cook | 24ed960 | 2017-08-28 11:28:21 -0700 | [diff] [blame] | 64 | static void pstore_timefunc(struct timer_list *); |
Kees Cook | 1d27e3e | 2017-10-04 16:27:04 -0700 | [diff] [blame] | 65 | static DEFINE_TIMER(pstore_timer, pstore_timefunc); |
Luck, Tony | 6dda926 | 2011-08-11 15:14:39 -0700 | [diff] [blame] | 66 | |
| 67 | static void pstore_dowork(struct work_struct *); |
| 68 | static DECLARE_WORK(pstore_work, pstore_dowork); |
| 69 | |
| 70 | /* |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 71 | * pstore_lock just protects "psinfo" during |
| 72 | * calls to pstore_register() |
| 73 | */ |
| 74 | static DEFINE_SPINLOCK(pstore_lock); |
Anton Vorontsov | 060287b | 2012-07-09 17:10:41 -0700 | [diff] [blame] | 75 | struct pstore_info *psinfo; |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 76 | |
Matthew Garrett | dee28e7 | 2011-07-21 16:57:55 -0400 | [diff] [blame] | 77 | static char *backend; |
Kees Cook | fe1d475 | 2018-03-06 15:57:38 -0800 | [diff] [blame] | 78 | static char *compress = |
| 79 | #ifdef CONFIG_PSTORE_COMPRESS_DEFAULT |
| 80 | CONFIG_PSTORE_COMPRESS_DEFAULT; |
| 81 | #else |
| 82 | NULL; |
| 83 | #endif |
Matthew Garrett | dee28e7 | 2011-07-21 16:57:55 -0400 | [diff] [blame] | 84 | |
Aruna Balakrishnaiah | b0aad7a | 2013-08-16 13:53:10 -0700 | [diff] [blame] | 85 | /* Compression parameters */ |
Geliang Tang | cb3bee0 | 2018-03-09 18:51:07 +0800 | [diff] [blame] | 86 | static struct crypto_comp *tfm; |
Geliang Tang | 8cfc8dd | 2016-02-18 22:04:22 +0800 | [diff] [blame] | 87 | |
| 88 | struct pstore_zbackend { |
Geliang Tang | cb3bee0 | 2018-03-09 18:51:07 +0800 | [diff] [blame] | 89 | int (*zbufsize)(size_t size); |
Geliang Tang | 8cfc8dd | 2016-02-18 22:04:22 +0800 | [diff] [blame] | 90 | const char *name; |
| 91 | }; |
Aruna Balakrishnaiah | b0aad7a | 2013-08-16 13:53:10 -0700 | [diff] [blame] | 92 | |
| 93 | static char *big_oops_buf; |
| 94 | static size_t big_oops_buf_sz; |
| 95 | |
Luck, Tony | 366f7e7 | 2011-03-18 15:33:43 -0700 | [diff] [blame] | 96 | /* How much of the console log to snapshot */ |
David Howells | 349d743 | 2017-07-05 16:24:34 +0100 | [diff] [blame] | 97 | unsigned long kmsg_bytes = PSTORE_DEFAULT_KMSG_BYTES; |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 98 | |
Luck, Tony | 366f7e7 | 2011-03-18 15:33:43 -0700 | [diff] [blame] | 99 | void pstore_set_kmsg_bytes(int bytes) |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 100 | { |
Luck, Tony | 366f7e7 | 2011-03-18 15:33:43 -0700 | [diff] [blame] | 101 | kmsg_bytes = bytes; |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 102 | } |
| 103 | |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 104 | /* Tag each group of saved records with a sequence number */ |
| 105 | static int oopscount; |
| 106 | |
Seiji Aguchi | 381b872 | 2012-03-16 15:36:59 -0700 | [diff] [blame] | 107 | static const char *get_reason_str(enum kmsg_dump_reason reason) |
| 108 | { |
| 109 | switch (reason) { |
| 110 | case KMSG_DUMP_PANIC: |
| 111 | return "Panic"; |
| 112 | case KMSG_DUMP_OOPS: |
| 113 | return "Oops"; |
| 114 | case KMSG_DUMP_EMERG: |
| 115 | return "Emergency"; |
| 116 | case KMSG_DUMP_RESTART: |
| 117 | return "Restart"; |
| 118 | case KMSG_DUMP_HALT: |
| 119 | return "Halt"; |
| 120 | case KMSG_DUMP_POWEROFF: |
| 121 | return "Poweroff"; |
| 122 | default: |
| 123 | return "Unknown"; |
| 124 | } |
| 125 | } |
Tony Luck | 9f6af27 | 2011-03-22 16:01:49 -0700 | [diff] [blame] | 126 | |
Seiji Aguchi | 9f244e9 | 2013-01-11 18:09:41 +0000 | [diff] [blame] | 127 | bool pstore_cannot_block_path(enum kmsg_dump_reason reason) |
| 128 | { |
| 129 | /* |
| 130 | * In case of NMI path, pstore shouldn't be blocked |
| 131 | * regardless of reason. |
| 132 | */ |
| 133 | if (in_nmi()) |
| 134 | return true; |
| 135 | |
| 136 | switch (reason) { |
| 137 | /* In panic case, other cpus are stopped by smp_send_stop(). */ |
| 138 | case KMSG_DUMP_PANIC: |
| 139 | /* Emergency restart shouldn't be blocked by spin lock. */ |
| 140 | case KMSG_DUMP_EMERG: |
| 141 | return true; |
| 142 | default: |
| 143 | return false; |
| 144 | } |
| 145 | } |
| 146 | EXPORT_SYMBOL_GPL(pstore_cannot_block_path); |
| 147 | |
Arnd Bergmann | 58eb5b67 | 2018-03-15 16:34:08 +0100 | [diff] [blame] | 148 | #if IS_ENABLED(CONFIG_PSTORE_DEFLATE_COMPRESS) |
Geliang Tang | cb3bee0 | 2018-03-09 18:51:07 +0800 | [diff] [blame] | 149 | static int zbufsize_deflate(size_t size) |
Aruna Balakrishnaiah | b0aad7a | 2013-08-16 13:53:10 -0700 | [diff] [blame] | 150 | { |
Aruna Balakrishnaiah | 7de8fe2 | 2013-09-11 10:57:41 -0700 | [diff] [blame] | 151 | size_t cmpr; |
Aruna Balakrishnaiah | b0aad7a | 2013-08-16 13:53:10 -0700 | [diff] [blame] | 152 | |
Geliang Tang | cb3bee0 | 2018-03-09 18:51:07 +0800 | [diff] [blame] | 153 | switch (size) { |
Aruna Balakrishnaiah | 7de8fe2 | 2013-09-11 10:57:41 -0700 | [diff] [blame] | 154 | /* buffer range for efivars */ |
| 155 | case 1000 ... 2000: |
| 156 | cmpr = 56; |
| 157 | break; |
| 158 | case 2001 ... 3000: |
| 159 | cmpr = 54; |
| 160 | break; |
| 161 | case 3001 ... 3999: |
| 162 | cmpr = 52; |
| 163 | break; |
| 164 | /* buffer range for nvram, erst */ |
| 165 | case 4000 ... 10000: |
| 166 | cmpr = 45; |
| 167 | break; |
| 168 | default: |
| 169 | cmpr = 60; |
| 170 | break; |
| 171 | } |
| 172 | |
Geliang Tang | cb3bee0 | 2018-03-09 18:51:07 +0800 | [diff] [blame] | 173 | return (size * 100) / cmpr; |
Geliang Tang | 8cfc8dd | 2016-02-18 22:04:22 +0800 | [diff] [blame] | 174 | } |
Geliang Tang | 8cfc8dd | 2016-02-18 22:04:22 +0800 | [diff] [blame] | 175 | #endif |
| 176 | |
Arnd Bergmann | 58eb5b67 | 2018-03-15 16:34:08 +0100 | [diff] [blame] | 177 | #if IS_ENABLED(CONFIG_PSTORE_LZO_COMPRESS) |
Geliang Tang | cb3bee0 | 2018-03-09 18:51:07 +0800 | [diff] [blame] | 178 | static int zbufsize_lzo(size_t size) |
Geliang Tang | 8cfc8dd | 2016-02-18 22:04:22 +0800 | [diff] [blame] | 179 | { |
Geliang Tang | cb3bee0 | 2018-03-09 18:51:07 +0800 | [diff] [blame] | 180 | return lzo1x_worst_compress(size); |
Geliang Tang | 8cfc8dd | 2016-02-18 22:04:22 +0800 | [diff] [blame] | 181 | } |
Geliang Tang | 8cfc8dd | 2016-02-18 22:04:22 +0800 | [diff] [blame] | 182 | #endif |
| 183 | |
Arnd Bergmann | 58eb5b67 | 2018-03-15 16:34:08 +0100 | [diff] [blame] | 184 | #if IS_ENABLED(CONFIG_PSTORE_LZ4_COMPRESS) || IS_ENABLED(CONFIG_PSTORE_LZ4HC_COMPRESS) |
Geliang Tang | cb3bee0 | 2018-03-09 18:51:07 +0800 | [diff] [blame] | 185 | static int zbufsize_lz4(size_t size) |
Geliang Tang | 8cfc8dd | 2016-02-18 22:04:22 +0800 | [diff] [blame] | 186 | { |
Geliang Tang | cb3bee0 | 2018-03-09 18:51:07 +0800 | [diff] [blame] | 187 | return LZ4_compressBound(size); |
Geliang Tang | 239b716 | 2018-02-13 14:40:39 +0800 | [diff] [blame] | 188 | } |
Geliang Tang | 239b716 | 2018-02-13 14:40:39 +0800 | [diff] [blame] | 189 | #endif |
| 190 | |
Arnd Bergmann | 58eb5b67 | 2018-03-15 16:34:08 +0100 | [diff] [blame] | 191 | #if IS_ENABLED(CONFIG_PSTORE_842_COMPRESS) |
Geliang Tang | cb3bee0 | 2018-03-09 18:51:07 +0800 | [diff] [blame] | 192 | static int zbufsize_842(size_t size) |
Geliang Tang | 239b716 | 2018-02-13 14:40:39 +0800 | [diff] [blame] | 193 | { |
Kees Cook | 5559740 | 2018-03-06 15:15:24 -0800 | [diff] [blame] | 194 | return size; |
Geliang Tang | 239b716 | 2018-02-13 14:40:39 +0800 | [diff] [blame] | 195 | } |
Kees Cook | fe1d475 | 2018-03-06 15:57:38 -0800 | [diff] [blame] | 196 | #endif |
Geliang Tang | 8cfc8dd | 2016-02-18 22:04:22 +0800 | [diff] [blame] | 197 | |
Geliang Tang | 1021bcf | 2018-08-01 19:23:37 +0800 | [diff] [blame] | 198 | #if IS_ENABLED(CONFIG_PSTORE_ZSTD_COMPRESS) |
| 199 | static int zbufsize_zstd(size_t size) |
| 200 | { |
| 201 | return ZSTD_compressBound(size); |
| 202 | } |
| 203 | #endif |
| 204 | |
Kees Cook | fe1d475 | 2018-03-06 15:57:38 -0800 | [diff] [blame] | 205 | static const struct pstore_zbackend *zbackend __ro_after_init; |
| 206 | |
| 207 | static const struct pstore_zbackend zbackends[] = { |
Arnd Bergmann | 58eb5b67 | 2018-03-15 16:34:08 +0100 | [diff] [blame] | 208 | #if IS_ENABLED(CONFIG_PSTORE_DEFLATE_COMPRESS) |
Kees Cook | fe1d475 | 2018-03-06 15:57:38 -0800 | [diff] [blame] | 209 | { |
Geliang Tang | cb3bee0 | 2018-03-09 18:51:07 +0800 | [diff] [blame] | 210 | .zbufsize = zbufsize_deflate, |
| 211 | .name = "deflate", |
Kees Cook | fe1d475 | 2018-03-06 15:57:38 -0800 | [diff] [blame] | 212 | }, |
| 213 | #endif |
Arnd Bergmann | 58eb5b67 | 2018-03-15 16:34:08 +0100 | [diff] [blame] | 214 | #if IS_ENABLED(CONFIG_PSTORE_LZO_COMPRESS) |
Kees Cook | fe1d475 | 2018-03-06 15:57:38 -0800 | [diff] [blame] | 215 | { |
Geliang Tang | cb3bee0 | 2018-03-09 18:51:07 +0800 | [diff] [blame] | 216 | .zbufsize = zbufsize_lzo, |
Kees Cook | fe1d475 | 2018-03-06 15:57:38 -0800 | [diff] [blame] | 217 | .name = "lzo", |
| 218 | }, |
| 219 | #endif |
Arnd Bergmann | 58eb5b67 | 2018-03-15 16:34:08 +0100 | [diff] [blame] | 220 | #if IS_ENABLED(CONFIG_PSTORE_LZ4_COMPRESS) |
Kees Cook | fe1d475 | 2018-03-06 15:57:38 -0800 | [diff] [blame] | 221 | { |
Geliang Tang | cb3bee0 | 2018-03-09 18:51:07 +0800 | [diff] [blame] | 222 | .zbufsize = zbufsize_lz4, |
Kees Cook | fe1d475 | 2018-03-06 15:57:38 -0800 | [diff] [blame] | 223 | .name = "lz4", |
| 224 | }, |
| 225 | #endif |
Arnd Bergmann | 58eb5b67 | 2018-03-15 16:34:08 +0100 | [diff] [blame] | 226 | #if IS_ENABLED(CONFIG_PSTORE_LZ4HC_COMPRESS) |
Kees Cook | fe1d475 | 2018-03-06 15:57:38 -0800 | [diff] [blame] | 227 | { |
Geliang Tang | cb3bee0 | 2018-03-09 18:51:07 +0800 | [diff] [blame] | 228 | .zbufsize = zbufsize_lz4, |
Kees Cook | fe1d475 | 2018-03-06 15:57:38 -0800 | [diff] [blame] | 229 | .name = "lz4hc", |
| 230 | }, |
| 231 | #endif |
Arnd Bergmann | 58eb5b67 | 2018-03-15 16:34:08 +0100 | [diff] [blame] | 232 | #if IS_ENABLED(CONFIG_PSTORE_842_COMPRESS) |
Kees Cook | fe1d475 | 2018-03-06 15:57:38 -0800 | [diff] [blame] | 233 | { |
Geliang Tang | cb3bee0 | 2018-03-09 18:51:07 +0800 | [diff] [blame] | 234 | .zbufsize = zbufsize_842, |
Kees Cook | fe1d475 | 2018-03-06 15:57:38 -0800 | [diff] [blame] | 235 | .name = "842", |
| 236 | }, |
| 237 | #endif |
Geliang Tang | 1021bcf | 2018-08-01 19:23:37 +0800 | [diff] [blame] | 238 | #if IS_ENABLED(CONFIG_PSTORE_ZSTD_COMPRESS) |
| 239 | { |
| 240 | .zbufsize = zbufsize_zstd, |
| 241 | .name = "zstd", |
| 242 | }, |
| 243 | #endif |
Kees Cook | fe1d475 | 2018-03-06 15:57:38 -0800 | [diff] [blame] | 244 | { } |
Geliang Tang | 8cfc8dd | 2016-02-18 22:04:22 +0800 | [diff] [blame] | 245 | }; |
Geliang Tang | 8cfc8dd | 2016-02-18 22:04:22 +0800 | [diff] [blame] | 246 | |
| 247 | static int pstore_compress(const void *in, void *out, |
Geliang Tang | cb3bee0 | 2018-03-09 18:51:07 +0800 | [diff] [blame] | 248 | unsigned int inlen, unsigned int outlen) |
Geliang Tang | 8cfc8dd | 2016-02-18 22:04:22 +0800 | [diff] [blame] | 249 | { |
Geliang Tang | cb3bee0 | 2018-03-09 18:51:07 +0800 | [diff] [blame] | 250 | int ret; |
| 251 | |
| 252 | ret = crypto_comp_compress(tfm, in, inlen, out, &outlen); |
| 253 | if (ret) { |
| 254 | pr_err("crypto_comp_compress failed, ret = %d!\n", ret); |
| 255 | return ret; |
| 256 | } |
| 257 | |
| 258 | return outlen; |
Geliang Tang | 8cfc8dd | 2016-02-18 22:04:22 +0800 | [diff] [blame] | 259 | } |
| 260 | |
Geliang Tang | cb3bee0 | 2018-03-09 18:51:07 +0800 | [diff] [blame] | 261 | static int pstore_decompress(void *in, void *out, |
| 262 | unsigned int inlen, unsigned int outlen) |
Geliang Tang | 8cfc8dd | 2016-02-18 22:04:22 +0800 | [diff] [blame] | 263 | { |
Geliang Tang | cb3bee0 | 2018-03-09 18:51:07 +0800 | [diff] [blame] | 264 | int ret; |
| 265 | |
| 266 | ret = crypto_comp_decompress(tfm, in, inlen, out, &outlen); |
| 267 | if (ret) { |
| 268 | pr_err("crypto_comp_decompress failed, ret = %d!\n", ret); |
| 269 | return ret; |
| 270 | } |
| 271 | |
| 272 | return outlen; |
Geliang Tang | 8cfc8dd | 2016-02-18 22:04:22 +0800 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | static void allocate_buf_for_compression(void) |
| 276 | { |
Tobias Regnery | e698aaf | 2018-04-06 09:25:17 +0200 | [diff] [blame] | 277 | if (!IS_ENABLED(CONFIG_PSTORE_COMPRESS) || !zbackend) |
Geliang Tang | cb3bee0 | 2018-03-09 18:51:07 +0800 | [diff] [blame] | 278 | return; |
| 279 | |
| 280 | if (!crypto_has_comp(zbackend->name, 0, 0)) { |
| 281 | pr_err("No %s compression\n", zbackend->name); |
| 282 | return; |
| 283 | } |
| 284 | |
| 285 | big_oops_buf_sz = zbackend->zbufsize(psinfo->bufsize); |
| 286 | if (big_oops_buf_sz <= 0) |
| 287 | return; |
| 288 | |
| 289 | big_oops_buf = kmalloc(big_oops_buf_sz, GFP_KERNEL); |
| 290 | if (!big_oops_buf) { |
Geliang Tang | 8cfc8dd | 2016-02-18 22:04:22 +0800 | [diff] [blame] | 291 | pr_err("allocate compression buffer error!\n"); |
Geliang Tang | cb3bee0 | 2018-03-09 18:51:07 +0800 | [diff] [blame] | 292 | return; |
| 293 | } |
| 294 | |
| 295 | tfm = crypto_alloc_comp(zbackend->name, 0, 0); |
| 296 | if (IS_ERR_OR_NULL(tfm)) { |
| 297 | kfree(big_oops_buf); |
| 298 | big_oops_buf = NULL; |
| 299 | pr_err("crypto_alloc_comp() failed!\n"); |
| 300 | return; |
Geliang Tang | 8cfc8dd | 2016-02-18 22:04:22 +0800 | [diff] [blame] | 301 | } |
| 302 | } |
| 303 | |
| 304 | static void free_buf_for_compression(void) |
| 305 | { |
Tobias Regnery | e698aaf | 2018-04-06 09:25:17 +0200 | [diff] [blame] | 306 | if (IS_ENABLED(CONFIG_PSTORE_COMPRESS) && !IS_ERR_OR_NULL(tfm)) |
Geliang Tang | cb3bee0 | 2018-03-09 18:51:07 +0800 | [diff] [blame] | 307 | crypto_free_comp(tfm); |
| 308 | kfree(big_oops_buf); |
| 309 | big_oops_buf = NULL; |
| 310 | big_oops_buf_sz = 0; |
Geliang Tang | ee1d267 | 2015-10-20 00:39:03 -0700 | [diff] [blame] | 311 | } |
| 312 | |
Aruna Balakrishnaiah | b0aad7a | 2013-08-16 13:53:10 -0700 | [diff] [blame] | 313 | /* |
| 314 | * Called when compression fails, since the printk buffer |
| 315 | * would be fetched for compression calling it again when |
| 316 | * compression fails would have moved the iterator of |
| 317 | * printk buffer which results in fetching old contents. |
| 318 | * Copy the recent messages from big_oops_buf to psinfo->buf |
| 319 | */ |
| 320 | static size_t copy_kmsg_to_buffer(int hsize, size_t len) |
| 321 | { |
| 322 | size_t total_len; |
| 323 | size_t diff; |
| 324 | |
| 325 | total_len = hsize + len; |
| 326 | |
| 327 | if (total_len > psinfo->bufsize) { |
| 328 | diff = total_len - psinfo->bufsize + hsize; |
| 329 | memcpy(psinfo->buf, big_oops_buf, hsize); |
| 330 | memcpy(psinfo->buf + hsize, big_oops_buf + diff, |
| 331 | psinfo->bufsize - hsize); |
| 332 | total_len = psinfo->bufsize; |
| 333 | } else |
| 334 | memcpy(psinfo->buf, big_oops_buf, total_len); |
| 335 | |
| 336 | return total_len; |
| 337 | } |
| 338 | |
Kees Cook | e581ca8 | 2017-05-19 15:10:31 -0700 | [diff] [blame] | 339 | void pstore_record_init(struct pstore_record *record, |
| 340 | struct pstore_info *psinfo) |
| 341 | { |
| 342 | memset(record, 0, sizeof(*record)); |
| 343 | |
| 344 | record->psi = psinfo; |
Kees Cook | c7f3c595 | 2017-05-19 15:29:10 -0700 | [diff] [blame] | 345 | |
| 346 | /* Report zeroed timestamp if called before timekeeping has resumed. */ |
Kees Cook | 7aaa822 | 2018-05-14 15:50:52 -0700 | [diff] [blame] | 347 | record->time = ns_to_timespec64(ktime_get_real_fast_ns()); |
Kees Cook | e581ca8 | 2017-05-19 15:10:31 -0700 | [diff] [blame] | 348 | } |
| 349 | |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 350 | /* |
| 351 | * callback from kmsg_dump. (s2,l2) has the most recently |
| 352 | * written bytes, older bytes are in (s1,l1). Save as much |
| 353 | * as we can from the end of the buffer. |
| 354 | */ |
| 355 | static void pstore_dump(struct kmsg_dumper *dumper, |
Kay Sievers | e2ae715 | 2012-06-15 14:07:51 +0200 | [diff] [blame] | 356 | enum kmsg_dump_reason reason) |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 357 | { |
Kay Sievers | e2ae715 | 2012-06-15 14:07:51 +0200 | [diff] [blame] | 358 | unsigned long total = 0; |
Seiji Aguchi | 381b872 | 2012-03-16 15:36:59 -0700 | [diff] [blame] | 359 | const char *why; |
Matthew Garrett | b94fdd0 | 2011-07-21 16:57:54 -0400 | [diff] [blame] | 360 | unsigned int part = 1; |
Don Zickus | abd4d55 | 2011-08-12 10:54:51 -0700 | [diff] [blame] | 361 | unsigned long flags = 0; |
Namhyung Kim | 98e44fda | 2016-05-18 21:00:05 +0900 | [diff] [blame] | 362 | int is_locked; |
Kay Sievers | e2ae715 | 2012-06-15 14:07:51 +0200 | [diff] [blame] | 363 | int ret; |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 364 | |
Seiji Aguchi | 381b872 | 2012-03-16 15:36:59 -0700 | [diff] [blame] | 365 | why = get_reason_str(reason); |
Tony Luck | 9f6af27 | 2011-03-22 16:01:49 -0700 | [diff] [blame] | 366 | |
Seiji Aguchi | 9f244e9 | 2013-01-11 18:09:41 +0000 | [diff] [blame] | 367 | if (pstore_cannot_block_path(reason)) { |
| 368 | is_locked = spin_trylock_irqsave(&psinfo->buf_lock, flags); |
| 369 | if (!is_locked) { |
| 370 | pr_err("pstore dump routine blocked in %s path, may corrupt error record\n" |
| 371 | , in_nmi() ? "NMI" : why); |
Li Pengcheng | 959217c8 | 2016-11-05 10:15:59 +0800 | [diff] [blame] | 372 | return; |
Seiji Aguchi | 9f244e9 | 2013-01-11 18:09:41 +0000 | [diff] [blame] | 373 | } |
Namhyung Kim | 98e44fda | 2016-05-18 21:00:05 +0900 | [diff] [blame] | 374 | } else { |
Don Zickus | abd4d55 | 2011-08-12 10:54:51 -0700 | [diff] [blame] | 375 | spin_lock_irqsave(&psinfo->buf_lock, flags); |
Namhyung Kim | 98e44fda | 2016-05-18 21:00:05 +0900 | [diff] [blame] | 376 | is_locked = 1; |
| 377 | } |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 378 | oopscount++; |
| 379 | while (total < kmsg_bytes) { |
Kay Sievers | e2ae715 | 2012-06-15 14:07:51 +0200 | [diff] [blame] | 380 | char *dst; |
Kees Cook | 76cc958 | 2017-03-03 23:28:53 -0800 | [diff] [blame] | 381 | size_t dst_size; |
| 382 | int header_size; |
Aruna Balakrishnaiah | b0aad7a | 2013-08-16 13:53:10 -0700 | [diff] [blame] | 383 | int zipped_len = -1; |
Kees Cook | 76cc958 | 2017-03-03 23:28:53 -0800 | [diff] [blame] | 384 | size_t dump_size; |
Kees Cook | e581ca8 | 2017-05-19 15:10:31 -0700 | [diff] [blame] | 385 | struct pstore_record record; |
| 386 | |
| 387 | pstore_record_init(&record, psinfo); |
| 388 | record.type = PSTORE_TYPE_DMESG; |
| 389 | record.count = oopscount; |
| 390 | record.reason = reason; |
| 391 | record.part = part; |
| 392 | record.buf = psinfo->buf; |
Kay Sievers | e2ae715 | 2012-06-15 14:07:51 +0200 | [diff] [blame] | 393 | |
Konstantin Khlebnikov | f0e2efc | 2015-05-21 09:26:19 -0700 | [diff] [blame] | 394 | if (big_oops_buf && is_locked) { |
Aruna Balakrishnaiah | b0aad7a | 2013-08-16 13:53:10 -0700 | [diff] [blame] | 395 | dst = big_oops_buf; |
Kees Cook | 76cc958 | 2017-03-03 23:28:53 -0800 | [diff] [blame] | 396 | dst_size = big_oops_buf_sz; |
Namhyung Kim | 235f6d1 | 2016-05-18 21:00:06 +0900 | [diff] [blame] | 397 | } else { |
| 398 | dst = psinfo->buf; |
Kees Cook | 76cc958 | 2017-03-03 23:28:53 -0800 | [diff] [blame] | 399 | dst_size = psinfo->bufsize; |
Namhyung Kim | 235f6d1 | 2016-05-18 21:00:06 +0900 | [diff] [blame] | 400 | } |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 401 | |
Kees Cook | 76cc958 | 2017-03-03 23:28:53 -0800 | [diff] [blame] | 402 | /* Write dump header. */ |
| 403 | header_size = snprintf(dst, dst_size, "%s#%d Part%u\n", why, |
| 404 | oopscount, part); |
| 405 | dst_size -= header_size; |
Aruna Balakrishnaiah | b0aad7a | 2013-08-16 13:53:10 -0700 | [diff] [blame] | 406 | |
Kees Cook | 76cc958 | 2017-03-03 23:28:53 -0800 | [diff] [blame] | 407 | /* Write dump contents. */ |
| 408 | if (!kmsg_dump_get_buffer(dumper, true, dst + header_size, |
| 409 | dst_size, &dump_size)) |
Namhyung Kim | 235f6d1 | 2016-05-18 21:00:06 +0900 | [diff] [blame] | 410 | break; |
| 411 | |
| 412 | if (big_oops_buf && is_locked) { |
Aruna Balakrishnaiah | b0aad7a | 2013-08-16 13:53:10 -0700 | [diff] [blame] | 413 | zipped_len = pstore_compress(dst, psinfo->buf, |
Kees Cook | 76cc958 | 2017-03-03 23:28:53 -0800 | [diff] [blame] | 414 | header_size + dump_size, |
| 415 | psinfo->bufsize); |
Aruna Balakrishnaiah | b0aad7a | 2013-08-16 13:53:10 -0700 | [diff] [blame] | 416 | |
| 417 | if (zipped_len > 0) { |
Kees Cook | 76cc958 | 2017-03-03 23:28:53 -0800 | [diff] [blame] | 418 | record.compressed = true; |
| 419 | record.size = zipped_len; |
Aruna Balakrishnaiah | b0aad7a | 2013-08-16 13:53:10 -0700 | [diff] [blame] | 420 | } else { |
Kees Cook | 76cc958 | 2017-03-03 23:28:53 -0800 | [diff] [blame] | 421 | record.size = copy_kmsg_to_buffer(header_size, |
| 422 | dump_size); |
Aruna Balakrishnaiah | b0aad7a | 2013-08-16 13:53:10 -0700 | [diff] [blame] | 423 | } |
| 424 | } else { |
Kees Cook | 76cc958 | 2017-03-03 23:28:53 -0800 | [diff] [blame] | 425 | record.size = header_size + dump_size; |
Aruna Balakrishnaiah | b0aad7a | 2013-08-16 13:53:10 -0700 | [diff] [blame] | 426 | } |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 427 | |
Kees Cook | 76cc958 | 2017-03-03 23:28:53 -0800 | [diff] [blame] | 428 | ret = psinfo->write(&record); |
Chen Gong | b238b8f | 2011-10-12 09:17:24 -0700 | [diff] [blame] | 429 | if (ret == 0 && reason == KMSG_DUMP_OOPS && pstore_is_mounted()) |
Luck, Tony | 6dda926 | 2011-08-11 15:14:39 -0700 | [diff] [blame] | 430 | pstore_new_entry = 1; |
Kay Sievers | e2ae715 | 2012-06-15 14:07:51 +0200 | [diff] [blame] | 431 | |
Kees Cook | 76cc958 | 2017-03-03 23:28:53 -0800 | [diff] [blame] | 432 | total += record.size; |
Matthew Garrett | 5628068 | 2011-07-21 16:57:53 -0400 | [diff] [blame] | 433 | part++; |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 434 | } |
Namhyung Kim | 98e44fda | 2016-05-18 21:00:05 +0900 | [diff] [blame] | 435 | if (is_locked) |
Don Zickus | abd4d55 | 2011-08-12 10:54:51 -0700 | [diff] [blame] | 436 | spin_unlock_irqrestore(&psinfo->buf_lock, flags); |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | static struct kmsg_dumper pstore_dumper = { |
| 440 | .dump = pstore_dump, |
| 441 | }; |
| 442 | |
Geliang Tang | 306e5c2 | 2015-10-31 23:23:15 +0800 | [diff] [blame] | 443 | /* |
| 444 | * Register with kmsg_dump to save last part of console log on panic. |
| 445 | */ |
Geliang Tang | 1873041 | 2015-10-20 00:39:02 -0700 | [diff] [blame] | 446 | static void pstore_register_kmsg(void) |
| 447 | { |
| 448 | kmsg_dump_register(&pstore_dumper); |
| 449 | } |
| 450 | |
Geliang Tang | ee1d267 | 2015-10-20 00:39:03 -0700 | [diff] [blame] | 451 | static void pstore_unregister_kmsg(void) |
| 452 | { |
| 453 | kmsg_dump_unregister(&pstore_dumper); |
| 454 | } |
| 455 | |
Anton Vorontsov | f29e595 | 2012-05-26 06:20:19 -0700 | [diff] [blame] | 456 | #ifdef CONFIG_PSTORE_CONSOLE |
| 457 | static void pstore_console_write(struct console *con, const char *s, unsigned c) |
| 458 | { |
| 459 | const char *e = s + c; |
| 460 | |
| 461 | while (s < e) { |
Kees Cook | e581ca8 | 2017-05-19 15:10:31 -0700 | [diff] [blame] | 462 | struct pstore_record record; |
Anton Vorontsov | f29e595 | 2012-05-26 06:20:19 -0700 | [diff] [blame] | 463 | unsigned long flags; |
| 464 | |
Kees Cook | e581ca8 | 2017-05-19 15:10:31 -0700 | [diff] [blame] | 465 | pstore_record_init(&record, psinfo); |
| 466 | record.type = PSTORE_TYPE_CONSOLE; |
| 467 | |
Anton Vorontsov | f29e595 | 2012-05-26 06:20:19 -0700 | [diff] [blame] | 468 | if (c > psinfo->bufsize) |
| 469 | c = psinfo->bufsize; |
Chuansheng Liu | 80c9d03 | 2012-09-18 01:43:44 +0800 | [diff] [blame] | 470 | |
| 471 | if (oops_in_progress) { |
| 472 | if (!spin_trylock_irqsave(&psinfo->buf_lock, flags)) |
| 473 | break; |
| 474 | } else { |
| 475 | spin_lock_irqsave(&psinfo->buf_lock, flags); |
| 476 | } |
Kees Cook | b10b471 | 2017-03-05 00:27:54 -0800 | [diff] [blame] | 477 | record.buf = (char *)s; |
| 478 | record.size = c; |
Kees Cook | 4c9ec21 | 2017-03-05 22:41:10 -0800 | [diff] [blame] | 479 | psinfo->write(&record); |
Anton Vorontsov | f29e595 | 2012-05-26 06:20:19 -0700 | [diff] [blame] | 480 | spin_unlock_irqrestore(&psinfo->buf_lock, flags); |
| 481 | s += c; |
| 482 | c = e - s; |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | static struct console pstore_console = { |
| 487 | .name = "pstore", |
| 488 | .write = pstore_console_write, |
| 489 | .flags = CON_PRINTBUFFER | CON_ENABLED | CON_ANYTIME, |
| 490 | .index = -1, |
| 491 | }; |
| 492 | |
| 493 | static void pstore_register_console(void) |
| 494 | { |
| 495 | register_console(&pstore_console); |
| 496 | } |
Geliang Tang | ee1d267 | 2015-10-20 00:39:03 -0700 | [diff] [blame] | 497 | |
| 498 | static void pstore_unregister_console(void) |
| 499 | { |
| 500 | unregister_console(&pstore_console); |
| 501 | } |
Anton Vorontsov | f29e595 | 2012-05-26 06:20:19 -0700 | [diff] [blame] | 502 | #else |
| 503 | static void pstore_register_console(void) {} |
Geliang Tang | ee1d267 | 2015-10-20 00:39:03 -0700 | [diff] [blame] | 504 | static void pstore_unregister_console(void) {} |
Anton Vorontsov | f29e595 | 2012-05-26 06:20:19 -0700 | [diff] [blame] | 505 | #endif |
| 506 | |
Kees Cook | 4c9ec21 | 2017-03-05 22:41:10 -0800 | [diff] [blame] | 507 | static int pstore_write_user_compat(struct pstore_record *record, |
| 508 | const char __user *buf) |
Mark Salyzyn | 5bf6d1b | 2016-09-01 08:13:46 -0700 | [diff] [blame] | 509 | { |
Kees Cook | 30800d9 | 2017-03-07 13:57:11 -0800 | [diff] [blame] | 510 | int ret = 0; |
Mark Salyzyn | 5bf6d1b | 2016-09-01 08:13:46 -0700 | [diff] [blame] | 511 | |
Kees Cook | 30800d9 | 2017-03-07 13:57:11 -0800 | [diff] [blame] | 512 | if (record->buf) |
| 513 | return -EINVAL; |
Mark Salyzyn | 5bf6d1b | 2016-09-01 08:13:46 -0700 | [diff] [blame] | 514 | |
Geliang Tang | 077090a | 2017-04-29 09:45:16 +0800 | [diff] [blame] | 515 | record->buf = memdup_user(buf, record->size); |
Hirofumi Nakagawa | dfd6fa3 | 2017-09-26 03:21:27 +0900 | [diff] [blame] | 516 | if (IS_ERR(record->buf)) { |
Geliang Tang | 077090a | 2017-04-29 09:45:16 +0800 | [diff] [blame] | 517 | ret = PTR_ERR(record->buf); |
Kees Cook | 30800d9 | 2017-03-07 13:57:11 -0800 | [diff] [blame] | 518 | goto out; |
Mark Salyzyn | 5bf6d1b | 2016-09-01 08:13:46 -0700 | [diff] [blame] | 519 | } |
Kees Cook | 30800d9 | 2017-03-07 13:57:11 -0800 | [diff] [blame] | 520 | |
| 521 | ret = record->psi->write(record); |
| 522 | |
Kees Cook | 30800d9 | 2017-03-07 13:57:11 -0800 | [diff] [blame] | 523 | kfree(record->buf); |
Geliang Tang | 077090a | 2017-04-29 09:45:16 +0800 | [diff] [blame] | 524 | out: |
Kees Cook | 30800d9 | 2017-03-07 13:57:11 -0800 | [diff] [blame] | 525 | record->buf = NULL; |
| 526 | |
| 527 | return unlikely(ret < 0) ? ret : record->size; |
Mark Salyzyn | 5bf6d1b | 2016-09-01 08:13:46 -0700 | [diff] [blame] | 528 | } |
| 529 | |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 530 | /* |
| 531 | * platform specific persistent storage driver registers with |
| 532 | * us here. If pstore is already mounted, call the platform |
| 533 | * read function right away to populate the file system. If not |
| 534 | * then the pstore mount code will call us later to fill out |
| 535 | * the file system. |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 536 | */ |
| 537 | int pstore_register(struct pstore_info *psi) |
| 538 | { |
| 539 | struct module *owner = psi->owner; |
| 540 | |
Kees Cook | 0d7cd09 | 2017-03-03 12:11:40 -0800 | [diff] [blame] | 541 | if (backend && strcmp(backend, psi->name)) { |
| 542 | pr_warn("ignoring unexpected backend '%s'\n", psi->name); |
Lenny Szubowicz | 8e48b1a | 2013-06-28 17:11:33 -0400 | [diff] [blame] | 543 | return -EPERM; |
Kees Cook | 0d7cd09 | 2017-03-03 12:11:40 -0800 | [diff] [blame] | 544 | } |
Lenny Szubowicz | 8e48b1a | 2013-06-28 17:11:33 -0400 | [diff] [blame] | 545 | |
Kees Cook | 4c9ec21 | 2017-03-05 22:41:10 -0800 | [diff] [blame] | 546 | /* Sanity check flags. */ |
| 547 | if (!psi->flags) { |
| 548 | pr_warn("backend '%s' must support at least one frontend\n", |
| 549 | psi->name); |
| 550 | return -EINVAL; |
| 551 | } |
| 552 | |
| 553 | /* Check for required functions. */ |
| 554 | if (!psi->read || !psi->write) { |
| 555 | pr_warn("backend '%s' must implement read() and write()\n", |
| 556 | psi->name); |
| 557 | return -EINVAL; |
| 558 | } |
| 559 | |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 560 | spin_lock(&pstore_lock); |
| 561 | if (psinfo) { |
Kees Cook | 0d7cd09 | 2017-03-03 12:11:40 -0800 | [diff] [blame] | 562 | pr_warn("backend '%s' already loaded: ignoring '%s'\n", |
| 563 | psinfo->name, psi->name); |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 564 | spin_unlock(&pstore_lock); |
| 565 | return -EBUSY; |
| 566 | } |
Matthew Garrett | dee28e7 | 2011-07-21 16:57:55 -0400 | [diff] [blame] | 567 | |
Kees Cook | 4c9ec21 | 2017-03-05 22:41:10 -0800 | [diff] [blame] | 568 | if (!psi->write_user) |
| 569 | psi->write_user = pstore_write_user_compat; |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 570 | psinfo = psi; |
Kees Cook | f6f8285 | 2011-11-17 12:58:07 -0800 | [diff] [blame] | 571 | mutex_init(&psinfo->read_mutex); |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 572 | spin_unlock(&pstore_lock); |
| 573 | |
| 574 | if (owner && !try_module_get(owner)) { |
| 575 | psinfo = NULL; |
| 576 | return -EINVAL; |
| 577 | } |
| 578 | |
Aruna Balakrishnaiah | b0aad7a | 2013-08-16 13:53:10 -0700 | [diff] [blame] | 579 | allocate_buf_for_compression(); |
| 580 | |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 581 | if (pstore_is_mounted()) |
Luck, Tony | 6dda926 | 2011-08-11 15:14:39 -0700 | [diff] [blame] | 582 | pstore_get_records(0); |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 583 | |
Namhyung Kim | c950fd6 | 2016-07-28 00:08:25 +0900 | [diff] [blame] | 584 | if (psi->flags & PSTORE_FLAGS_DMESG) |
| 585 | pstore_register_kmsg(); |
| 586 | if (psi->flags & PSTORE_FLAGS_CONSOLE) |
Luck, Tony | df36ac1 | 2013-12-18 15:17:10 -0800 | [diff] [blame] | 587 | pstore_register_console(); |
Namhyung Kim | c950fd6 | 2016-07-28 00:08:25 +0900 | [diff] [blame] | 588 | if (psi->flags & PSTORE_FLAGS_FTRACE) |
Luck, Tony | df36ac1 | 2013-12-18 15:17:10 -0800 | [diff] [blame] | 589 | pstore_register_ftrace(); |
Namhyung Kim | c950fd6 | 2016-07-28 00:08:25 +0900 | [diff] [blame] | 590 | if (psi->flags & PSTORE_FLAGS_PMSG) |
Mark Salyzyn | 9d5438f | 2015-01-16 16:01:10 -0800 | [diff] [blame] | 591 | pstore_register_pmsg(); |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 592 | |
Kees Cook | 6330d55 | 2017-03-06 12:42:12 -0800 | [diff] [blame] | 593 | /* Start watching for new records, if desired. */ |
Anton Vorontsov | a3f5f07 | 2012-05-26 06:20:28 -0700 | [diff] [blame] | 594 | if (pstore_update_ms >= 0) { |
| 595 | pstore_timer.expires = jiffies + |
| 596 | msecs_to_jiffies(pstore_update_ms); |
| 597 | add_timer(&pstore_timer); |
| 598 | } |
Luck, Tony | 6dda926 | 2011-08-11 15:14:39 -0700 | [diff] [blame] | 599 | |
Wang Long | 42222c2 | 2015-05-21 09:34:22 -0700 | [diff] [blame] | 600 | /* |
| 601 | * Update the module parameter backend, so it is visible |
| 602 | * through /sys/module/pstore/parameters/backend |
| 603 | */ |
| 604 | backend = psi->name; |
| 605 | |
Fabian Frederick | ef74885 | 2014-06-06 14:37:31 -0700 | [diff] [blame] | 606 | pr_info("Registered %s as persistent store backend\n", psi->name); |
Lenny Szubowicz | 8e48b1a | 2013-06-28 17:11:33 -0400 | [diff] [blame] | 607 | |
Kees Cook | 1344dd8 | 2017-03-03 17:45:38 -0800 | [diff] [blame] | 608 | module_put(owner); |
| 609 | |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 610 | return 0; |
| 611 | } |
| 612 | EXPORT_SYMBOL_GPL(pstore_register); |
| 613 | |
Geliang Tang | ee1d267 | 2015-10-20 00:39:03 -0700 | [diff] [blame] | 614 | void pstore_unregister(struct pstore_info *psi) |
| 615 | { |
Kees Cook | 6330d55 | 2017-03-06 12:42:12 -0800 | [diff] [blame] | 616 | /* Stop timer and make sure all work has finished. */ |
| 617 | pstore_update_ms = -1; |
| 618 | del_timer_sync(&pstore_timer); |
| 619 | flush_work(&pstore_work); |
| 620 | |
Namhyung Kim | c950fd6 | 2016-07-28 00:08:25 +0900 | [diff] [blame] | 621 | if (psi->flags & PSTORE_FLAGS_PMSG) |
Kees Cook | a1db806 | 2016-05-19 10:59:03 -0400 | [diff] [blame] | 622 | pstore_unregister_pmsg(); |
Namhyung Kim | c950fd6 | 2016-07-28 00:08:25 +0900 | [diff] [blame] | 623 | if (psi->flags & PSTORE_FLAGS_FTRACE) |
Kees Cook | a1db806 | 2016-05-19 10:59:03 -0400 | [diff] [blame] | 624 | pstore_unregister_ftrace(); |
Namhyung Kim | c950fd6 | 2016-07-28 00:08:25 +0900 | [diff] [blame] | 625 | if (psi->flags & PSTORE_FLAGS_CONSOLE) |
Kees Cook | a1db806 | 2016-05-19 10:59:03 -0400 | [diff] [blame] | 626 | pstore_unregister_console(); |
Namhyung Kim | c950fd6 | 2016-07-28 00:08:25 +0900 | [diff] [blame] | 627 | if (psi->flags & PSTORE_FLAGS_DMESG) |
| 628 | pstore_unregister_kmsg(); |
Geliang Tang | ee1d267 | 2015-10-20 00:39:03 -0700 | [diff] [blame] | 629 | |
| 630 | free_buf_for_compression(); |
| 631 | |
| 632 | psinfo = NULL; |
| 633 | backend = NULL; |
| 634 | } |
| 635 | EXPORT_SYMBOL_GPL(pstore_unregister); |
| 636 | |
Kees Cook | 634f8f5 | 2017-03-03 17:35:25 -0800 | [diff] [blame] | 637 | static void decompress_record(struct pstore_record *record) |
| 638 | { |
| 639 | int unzipped_len; |
Kees Cook | 7e8cc8d | 2017-03-04 22:28:46 -0800 | [diff] [blame] | 640 | char *decompressed; |
Kees Cook | 634f8f5 | 2017-03-03 17:35:25 -0800 | [diff] [blame] | 641 | |
Ankit Kumar | 4a16d1c | 2017-05-23 11:16:52 +0530 | [diff] [blame] | 642 | if (!record->compressed) |
| 643 | return; |
| 644 | |
Kees Cook | 634f8f5 | 2017-03-03 17:35:25 -0800 | [diff] [blame] | 645 | /* Only PSTORE_TYPE_DMESG support compression. */ |
Ankit Kumar | 4a16d1c | 2017-05-23 11:16:52 +0530 | [diff] [blame] | 646 | if (record->type != PSTORE_TYPE_DMESG) { |
Kees Cook | 634f8f5 | 2017-03-03 17:35:25 -0800 | [diff] [blame] | 647 | pr_warn("ignored compressed record type %d\n", record->type); |
| 648 | return; |
| 649 | } |
| 650 | |
| 651 | /* No compression method has created the common buffer. */ |
| 652 | if (!big_oops_buf) { |
| 653 | pr_warn("no decompression buffer allocated\n"); |
| 654 | return; |
| 655 | } |
| 656 | |
| 657 | unzipped_len = pstore_decompress(record->buf, big_oops_buf, |
| 658 | record->size, big_oops_buf_sz); |
Kees Cook | 7e8cc8d | 2017-03-04 22:28:46 -0800 | [diff] [blame] | 659 | if (unzipped_len <= 0) { |
Kees Cook | 634f8f5 | 2017-03-03 17:35:25 -0800 | [diff] [blame] | 660 | pr_err("decompression failed: %d\n", unzipped_len); |
Kees Cook | 7e8cc8d | 2017-03-04 22:28:46 -0800 | [diff] [blame] | 661 | return; |
| 662 | } |
| 663 | |
| 664 | /* Build new buffer for decompressed contents. */ |
| 665 | decompressed = kmalloc(unzipped_len + record->ecc_notice_size, |
| 666 | GFP_KERNEL); |
| 667 | if (!decompressed) { |
| 668 | pr_err("decompression ran out of memory\n"); |
| 669 | return; |
| 670 | } |
| 671 | memcpy(decompressed, big_oops_buf, unzipped_len); |
| 672 | |
| 673 | /* Append ECC notice to decompressed buffer. */ |
| 674 | memcpy(decompressed + unzipped_len, record->buf + record->size, |
| 675 | record->ecc_notice_size); |
| 676 | |
| 677 | /* Swap out compresed contents with decompressed contents. */ |
| 678 | kfree(record->buf); |
| 679 | record->buf = decompressed; |
| 680 | record->size = unzipped_len; |
| 681 | record->compressed = false; |
Kees Cook | 634f8f5 | 2017-03-03 17:35:25 -0800 | [diff] [blame] | 682 | } |
| 683 | |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 684 | /* |
Kees Cook | 3a7d2fd | 2017-04-27 15:53:21 -0700 | [diff] [blame] | 685 | * Read all the records from one persistent store backend. Create |
Luck, Tony | 6dda926 | 2011-08-11 15:14:39 -0700 | [diff] [blame] | 686 | * files in our filesystem. Don't warn about -EEXIST errors |
| 687 | * when we are re-scanning the backing store looking to add new |
| 688 | * error records. |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 689 | */ |
Kees Cook | 3a7d2fd | 2017-04-27 15:53:21 -0700 | [diff] [blame] | 690 | void pstore_get_backend_records(struct pstore_info *psi, |
| 691 | struct dentry *root, int quiet) |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 692 | { |
Kees Cook | 2a2b0ac | 2017-03-04 22:57:26 -0800 | [diff] [blame] | 693 | int failed = 0; |
Kees Cook | 656de42 | 2017-05-16 12:03:31 -0700 | [diff] [blame] | 694 | unsigned int stop_loop = 65536; |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 695 | |
Kees Cook | 3a7d2fd | 2017-04-27 15:53:21 -0700 | [diff] [blame] | 696 | if (!psi || !root) |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 697 | return; |
| 698 | |
Kees Cook | f6f8285 | 2011-11-17 12:58:07 -0800 | [diff] [blame] | 699 | mutex_lock(&psi->read_mutex); |
Kees Cook | 2174f6d | 2011-11-18 13:49:00 -0800 | [diff] [blame] | 700 | if (psi->open && psi->open(psi)) |
Chen Gong | 06cf91b | 2011-05-16 11:00:27 -0700 | [diff] [blame] | 701 | goto out; |
| 702 | |
Kees Cook | 1dfff7d | 2017-03-04 22:46:41 -0800 | [diff] [blame] | 703 | /* |
| 704 | * Backend callback read() allocates record.buf. decompress_record() |
| 705 | * may reallocate record.buf. On success, pstore_mkfile() will keep |
| 706 | * the record.buf, so free it only on failure. |
| 707 | */ |
Kees Cook | 656de42 | 2017-05-16 12:03:31 -0700 | [diff] [blame] | 708 | for (; stop_loop; stop_loop--) { |
Kees Cook | 2a2b0ac | 2017-03-04 22:57:26 -0800 | [diff] [blame] | 709 | struct pstore_record *record; |
| 710 | int rc; |
| 711 | |
| 712 | record = kzalloc(sizeof(*record), GFP_KERNEL); |
| 713 | if (!record) { |
| 714 | pr_err("out of memory creating record\n"); |
| 715 | break; |
| 716 | } |
Kees Cook | e581ca8 | 2017-05-19 15:10:31 -0700 | [diff] [blame] | 717 | pstore_record_init(record, psi); |
Kees Cook | 2a2b0ac | 2017-03-04 22:57:26 -0800 | [diff] [blame] | 718 | |
| 719 | record->size = psi->read(record); |
| 720 | |
| 721 | /* No more records left in backend? */ |
Douglas Anderson | f6525b9 | 2017-05-30 15:50:38 -0700 | [diff] [blame] | 722 | if (record->size <= 0) { |
| 723 | kfree(record); |
Kees Cook | 2a2b0ac | 2017-03-04 22:57:26 -0800 | [diff] [blame] | 724 | break; |
Douglas Anderson | f6525b9 | 2017-05-30 15:50:38 -0700 | [diff] [blame] | 725 | } |
Kees Cook | 2a2b0ac | 2017-03-04 22:57:26 -0800 | [diff] [blame] | 726 | |
| 727 | decompress_record(record); |
Kees Cook | 3a7d2fd | 2017-04-27 15:53:21 -0700 | [diff] [blame] | 728 | rc = pstore_mkfile(root, record); |
Kees Cook | 1dfff7d | 2017-03-04 22:46:41 -0800 | [diff] [blame] | 729 | if (rc) { |
Kees Cook | 83f70f0 | 2017-03-04 23:12:24 -0800 | [diff] [blame] | 730 | /* pstore_mkfile() did not take record, so free it. */ |
Kees Cook | 2a2b0ac | 2017-03-04 22:57:26 -0800 | [diff] [blame] | 731 | kfree(record->buf); |
Kees Cook | 83f70f0 | 2017-03-04 23:12:24 -0800 | [diff] [blame] | 732 | kfree(record); |
Kees Cook | 1dfff7d | 2017-03-04 22:46:41 -0800 | [diff] [blame] | 733 | if (rc != -EEXIST || !quiet) |
| 734 | failed++; |
| 735 | } |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 736 | } |
Kees Cook | 2174f6d | 2011-11-18 13:49:00 -0800 | [diff] [blame] | 737 | if (psi->close) |
| 738 | psi->close(psi); |
Chen Gong | 06cf91b | 2011-05-16 11:00:27 -0700 | [diff] [blame] | 739 | out: |
Kees Cook | f6f8285 | 2011-11-17 12:58:07 -0800 | [diff] [blame] | 740 | mutex_unlock(&psi->read_mutex); |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 741 | |
| 742 | if (failed) |
Kees Cook | 656de42 | 2017-05-16 12:03:31 -0700 | [diff] [blame] | 743 | pr_warn("failed to create %d record(s) from '%s'\n", |
Fabian Frederick | ef74885 | 2014-06-06 14:37:31 -0700 | [diff] [blame] | 744 | failed, psi->name); |
Kees Cook | 656de42 | 2017-05-16 12:03:31 -0700 | [diff] [blame] | 745 | if (!stop_loop) |
| 746 | pr_err("looping? Too many records seen from '%s'\n", |
| 747 | psi->name); |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 748 | } |
| 749 | |
Luck, Tony | 6dda926 | 2011-08-11 15:14:39 -0700 | [diff] [blame] | 750 | static void pstore_dowork(struct work_struct *work) |
| 751 | { |
| 752 | pstore_get_records(1); |
| 753 | } |
| 754 | |
Kees Cook | 24ed960 | 2017-08-28 11:28:21 -0700 | [diff] [blame] | 755 | static void pstore_timefunc(struct timer_list *unused) |
Luck, Tony | 6dda926 | 2011-08-11 15:14:39 -0700 | [diff] [blame] | 756 | { |
| 757 | if (pstore_new_entry) { |
| 758 | pstore_new_entry = 0; |
| 759 | schedule_work(&pstore_work); |
| 760 | } |
| 761 | |
Kees Cook | 6330d55 | 2017-03-06 12:42:12 -0800 | [diff] [blame] | 762 | if (pstore_update_ms >= 0) |
| 763 | mod_timer(&pstore_timer, |
| 764 | jiffies + msecs_to_jiffies(pstore_update_ms)); |
Luck, Tony | 6dda926 | 2011-08-11 15:14:39 -0700 | [diff] [blame] | 765 | } |
| 766 | |
Kees Cook | fe1d475 | 2018-03-06 15:57:38 -0800 | [diff] [blame] | 767 | void __init pstore_choose_compression(void) |
| 768 | { |
| 769 | const struct pstore_zbackend *step; |
| 770 | |
| 771 | if (!compress) |
| 772 | return; |
| 773 | |
| 774 | for (step = zbackends; step->name; step++) { |
| 775 | if (!strcmp(compress, step->name)) { |
| 776 | zbackend = step; |
| 777 | pr_info("using %s compression\n", zbackend->name); |
| 778 | return; |
| 779 | } |
| 780 | } |
| 781 | } |
| 782 | |
Kees Cook | cb095af | 2018-10-18 11:17:42 -0700 | [diff] [blame] | 783 | static int __init pstore_init(void) |
| 784 | { |
| 785 | int ret; |
| 786 | |
| 787 | pstore_choose_compression(); |
| 788 | |
Joel Fernandes (Google) | 4160316 | 2018-10-17 03:13:55 -0700 | [diff] [blame^] | 789 | /* |
| 790 | * Check if any pstore backends registered earlier but did not |
| 791 | * initialize compression because crypto was not ready. If so, |
| 792 | * initialize compression now. |
| 793 | */ |
| 794 | if (psinfo && !tfm) |
| 795 | allocate_buf_for_compression(); |
| 796 | |
Kees Cook | cb095af | 2018-10-18 11:17:42 -0700 | [diff] [blame] | 797 | ret = pstore_init_fs(); |
| 798 | if (ret) |
| 799 | return ret; |
| 800 | |
| 801 | return 0; |
| 802 | } |
Joel Fernandes (Google) | 4160316 | 2018-10-17 03:13:55 -0700 | [diff] [blame^] | 803 | late_initcall(pstore_init); |
Kees Cook | cb095af | 2018-10-18 11:17:42 -0700 | [diff] [blame] | 804 | |
| 805 | static void __exit pstore_exit(void) |
| 806 | { |
| 807 | pstore_exit_fs(); |
| 808 | } |
| 809 | module_exit(pstore_exit) |
| 810 | |
Kees Cook | fe1d475 | 2018-03-06 15:57:38 -0800 | [diff] [blame] | 811 | module_param(compress, charp, 0444); |
| 812 | MODULE_PARM_DESC(compress, "Pstore compression to use"); |
| 813 | |
Matthew Garrett | dee28e7 | 2011-07-21 16:57:55 -0400 | [diff] [blame] | 814 | module_param(backend, charp, 0444); |
| 815 | MODULE_PARM_DESC(backend, "Pstore backend to use"); |
Kees Cook | cb095af | 2018-10-18 11:17:42 -0700 | [diff] [blame] | 816 | |
| 817 | MODULE_AUTHOR("Tony Luck <tony.luck@intel.com>"); |
| 818 | MODULE_LICENSE("GPL"); |