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