blob: 3d7024662d29534147a5d5cb9bd78b82a4e22397 [file] [log] [blame]
Thomas Gleixner45051532019-05-29 16:57:47 -07001// SPDX-License-Identifier: GPL-2.0-only
Tony Luckca01d6d2010-12-28 14:25:21 -08002/*
3 * Persistent Storage - platform driver interface parts.
4 *
Anton Vorontsovf29e5952012-05-26 06:20:19 -07005 * Copyright (C) 2007-2008 Google, Inc.
Tony Luckca01d6d2010-12-28 14:25:21 -08006 * Copyright (C) 2010 Intel Corporation <tony.luck@intel.com>
Tony Luckca01d6d2010-12-28 14:25:21 -08007 */
8
Fabian Frederickef748852014-06-06 14:37:31 -07009#define pr_fmt(fmt) "pstore: " fmt
10
Tony Luckca01d6d2010-12-28 14:25:21 -080011#include <linux/atomic.h>
12#include <linux/types.h>
13#include <linux/errno.h>
14#include <linux/init.h>
15#include <linux/kmsg_dump.h>
Anton Vorontsovf29e5952012-05-26 06:20:19 -070016#include <linux/console.h>
Tony Luckca01d6d2010-12-28 14:25:21 -080017#include <linux/module.h>
18#include <linux/pstore.h>
Arnd Bergmann58eb5b672018-03-15 16:34:08 +010019#if IS_ENABLED(CONFIG_PSTORE_LZO_COMPRESS)
Geliang Tang8cfc8dd2016-02-18 22:04:22 +080020#include <linux/lzo.h>
21#endif
Arnd Bergmann58eb5b672018-03-15 16:34:08 +010022#if IS_ENABLED(CONFIG_PSTORE_LZ4_COMPRESS) || IS_ENABLED(CONFIG_PSTORE_LZ4HC_COMPRESS)
Geliang Tang8cfc8dd2016-02-18 22:04:22 +080023#include <linux/lz4.h>
24#endif
Geliang Tang1021bcf2018-08-01 19:23:37 +080025#if IS_ENABLED(CONFIG_PSTORE_ZSTD_COMPRESS)
26#include <linux/zstd.h>
27#endif
Geliang Tangcb3bee02018-03-09 18:51:07 +080028#include <linux/crypto.h>
Tony Luckca01d6d2010-12-28 14:25:21 -080029#include <linux/string.h>
Luck, Tony6dda9262011-08-11 15:14:39 -070030#include <linux/timer.h>
Tony Luckca01d6d2010-12-28 14:25:21 -080031#include <linux/slab.h>
32#include <linux/uaccess.h>
Anton Vorontsova3f5f072012-05-26 06:20:28 -070033#include <linux/jiffies.h>
Luck, Tony6dda9262011-08-11 15:14:39 -070034#include <linux/workqueue.h>
Tony Luckca01d6d2010-12-28 14:25:21 -080035
36#include "internal.h"
37
38/*
Luck, Tony6dda9262011-08-11 15:14:39 -070039 * We defer making "oops" entries appear in pstore - see
40 * whether the system is actually still running well enough
41 * to let someone see the entry
42 */
Anton Vorontsov521f72882012-05-26 06:20:29 -070043static int pstore_update_ms = -1;
Anton Vorontsova3f5f072012-05-26 06:20:28 -070044module_param_named(update_ms, pstore_update_ms, int, 0600);
45MODULE_PARM_DESC(update_ms, "milliseconds before pstore updates its content "
Anton Vorontsov521f72882012-05-26 06:20:29 -070046 "(default is -1, which means runtime updates are disabled; "
47 "enabling this option is not safe, it may lead to further "
48 "corruption on Oopses)");
Luck, Tony6dda9262011-08-11 15:14:39 -070049
Joel Fernandes (Google)f0f23e52018-11-03 16:38:16 -070050/* Names should be in the same order as the enum pstore_type_id */
51static const char * const pstore_type_names[] = {
52 "dmesg",
53 "mce",
54 "console",
55 "ftrace",
56 "rtas",
57 "powerpc-ofw",
58 "powerpc-common",
59 "pmsg",
60 "powerpc-opal",
61};
62
Luck, Tony6dda9262011-08-11 15:14:39 -070063static int pstore_new_entry;
64
Kees Cook24ed9602017-08-28 11:28:21 -070065static void pstore_timefunc(struct timer_list *);
Kees Cook1d27e3e2017-10-04 16:27:04 -070066static DEFINE_TIMER(pstore_timer, pstore_timefunc);
Luck, Tony6dda9262011-08-11 15:14:39 -070067
68static void pstore_dowork(struct work_struct *);
69static DECLARE_WORK(pstore_work, pstore_dowork);
70
71/*
Tony Luckca01d6d2010-12-28 14:25:21 -080072 * pstore_lock just protects "psinfo" during
73 * calls to pstore_register()
74 */
75static DEFINE_SPINLOCK(pstore_lock);
Anton Vorontsov060287b2012-07-09 17:10:41 -070076struct pstore_info *psinfo;
Tony Luckca01d6d2010-12-28 14:25:21 -080077
Matthew Garrettdee28e72011-07-21 16:57:55 -040078static char *backend;
Kees Cookfe1d4752018-03-06 15:57:38 -080079static char *compress =
80#ifdef CONFIG_PSTORE_COMPRESS_DEFAULT
81 CONFIG_PSTORE_COMPRESS_DEFAULT;
82#else
83 NULL;
84#endif
Matthew Garrettdee28e72011-07-21 16:57:55 -040085
Aruna Balakrishnaiahb0aad7a2013-08-16 13:53:10 -070086/* Compression parameters */
Geliang Tangcb3bee02018-03-09 18:51:07 +080087static struct crypto_comp *tfm;
Geliang Tang8cfc8dd2016-02-18 22:04:22 +080088
89struct pstore_zbackend {
Geliang Tangcb3bee02018-03-09 18:51:07 +080090 int (*zbufsize)(size_t size);
Geliang Tang8cfc8dd2016-02-18 22:04:22 +080091 const char *name;
92};
Aruna Balakrishnaiahb0aad7a2013-08-16 13:53:10 -070093
94static char *big_oops_buf;
95static size_t big_oops_buf_sz;
96
Luck, Tony366f7e72011-03-18 15:33:43 -070097/* How much of the console log to snapshot */
David Howells349d7432017-07-05 16:24:34 +010098unsigned long kmsg_bytes = PSTORE_DEFAULT_KMSG_BYTES;
Tony Luckca01d6d2010-12-28 14:25:21 -080099
Luck, Tony366f7e72011-03-18 15:33:43 -0700100void pstore_set_kmsg_bytes(int bytes)
Tony Luckca01d6d2010-12-28 14:25:21 -0800101{
Luck, Tony366f7e72011-03-18 15:33:43 -0700102 kmsg_bytes = bytes;
Tony Luckca01d6d2010-12-28 14:25:21 -0800103}
104
Tony Luckca01d6d2010-12-28 14:25:21 -0800105/* Tag each group of saved records with a sequence number */
106static int oopscount;
107
Joel Fernandes (Google)f0f23e52018-11-03 16:38:16 -0700108const char *pstore_type_to_name(enum pstore_type_id type)
109{
110 BUILD_BUG_ON(ARRAY_SIZE(pstore_type_names) != PSTORE_TYPE_MAX);
111
112 if (WARN_ON_ONCE(type >= PSTORE_TYPE_MAX))
113 return "unknown";
114
115 return pstore_type_names[type];
116}
117EXPORT_SYMBOL_GPL(pstore_type_to_name);
118
119enum pstore_type_id pstore_name_to_type(const char *name)
120{
121 int i;
122
123 for (i = 0; i < PSTORE_TYPE_MAX; i++) {
124 if (!strcmp(pstore_type_names[i], name))
125 return i;
126 }
127
128 return PSTORE_TYPE_MAX;
129}
130EXPORT_SYMBOL_GPL(pstore_name_to_type);
131
Seiji Aguchi381b8722012-03-16 15:36:59 -0700132static const char *get_reason_str(enum kmsg_dump_reason reason)
133{
134 switch (reason) {
135 case KMSG_DUMP_PANIC:
136 return "Panic";
137 case KMSG_DUMP_OOPS:
138 return "Oops";
139 case KMSG_DUMP_EMERG:
140 return "Emergency";
141 case KMSG_DUMP_RESTART:
142 return "Restart";
143 case KMSG_DUMP_HALT:
144 return "Halt";
145 case KMSG_DUMP_POWEROFF:
146 return "Poweroff";
147 default:
148 return "Unknown";
149 }
150}
Tony Luck9f6af272011-03-22 16:01:49 -0700151
Kees Cookea84b582018-11-30 14:36:58 -0800152/*
153 * Should pstore_dump() wait for a concurrent pstore_dump()? If
154 * not, the current pstore_dump() will report a failure to dump
155 * and return.
156 */
157static bool pstore_cannot_wait(enum kmsg_dump_reason reason)
Seiji Aguchi9f244e92013-01-11 18:09:41 +0000158{
Kees Cookea84b582018-11-30 14:36:58 -0800159 /* In NMI path, pstore shouldn't block regardless of reason. */
Seiji Aguchi9f244e92013-01-11 18:09:41 +0000160 if (in_nmi())
161 return true;
162
163 switch (reason) {
164 /* In panic case, other cpus are stopped by smp_send_stop(). */
165 case KMSG_DUMP_PANIC:
Kees Cookea84b582018-11-30 14:36:58 -0800166 /* Emergency restart shouldn't be blocked. */
Seiji Aguchi9f244e92013-01-11 18:09:41 +0000167 case KMSG_DUMP_EMERG:
168 return true;
169 default:
170 return false;
171 }
172}
Seiji Aguchi9f244e92013-01-11 18:09:41 +0000173
Arnd Bergmann58eb5b672018-03-15 16:34:08 +0100174#if IS_ENABLED(CONFIG_PSTORE_DEFLATE_COMPRESS)
Geliang Tangcb3bee02018-03-09 18:51:07 +0800175static int zbufsize_deflate(size_t size)
Aruna Balakrishnaiahb0aad7a2013-08-16 13:53:10 -0700176{
Aruna Balakrishnaiah7de8fe22013-09-11 10:57:41 -0700177 size_t cmpr;
Aruna Balakrishnaiahb0aad7a2013-08-16 13:53:10 -0700178
Geliang Tangcb3bee02018-03-09 18:51:07 +0800179 switch (size) {
Aruna Balakrishnaiah7de8fe22013-09-11 10:57:41 -0700180 /* buffer range for efivars */
181 case 1000 ... 2000:
182 cmpr = 56;
183 break;
184 case 2001 ... 3000:
185 cmpr = 54;
186 break;
187 case 3001 ... 3999:
188 cmpr = 52;
189 break;
190 /* buffer range for nvram, erst */
191 case 4000 ... 10000:
192 cmpr = 45;
193 break;
194 default:
195 cmpr = 60;
196 break;
197 }
198
Geliang Tangcb3bee02018-03-09 18:51:07 +0800199 return (size * 100) / cmpr;
Geliang Tang8cfc8dd2016-02-18 22:04:22 +0800200}
Geliang Tang8cfc8dd2016-02-18 22:04:22 +0800201#endif
202
Arnd Bergmann58eb5b672018-03-15 16:34:08 +0100203#if IS_ENABLED(CONFIG_PSTORE_LZO_COMPRESS)
Geliang Tangcb3bee02018-03-09 18:51:07 +0800204static int zbufsize_lzo(size_t size)
Geliang Tang8cfc8dd2016-02-18 22:04:22 +0800205{
Geliang Tangcb3bee02018-03-09 18:51:07 +0800206 return lzo1x_worst_compress(size);
Geliang Tang8cfc8dd2016-02-18 22:04:22 +0800207}
Geliang Tang8cfc8dd2016-02-18 22:04:22 +0800208#endif
209
Arnd Bergmann58eb5b672018-03-15 16:34:08 +0100210#if IS_ENABLED(CONFIG_PSTORE_LZ4_COMPRESS) || IS_ENABLED(CONFIG_PSTORE_LZ4HC_COMPRESS)
Geliang Tangcb3bee02018-03-09 18:51:07 +0800211static int zbufsize_lz4(size_t size)
Geliang Tang8cfc8dd2016-02-18 22:04:22 +0800212{
Geliang Tangcb3bee02018-03-09 18:51:07 +0800213 return LZ4_compressBound(size);
Geliang Tang239b7162018-02-13 14:40:39 +0800214}
Geliang Tang239b7162018-02-13 14:40:39 +0800215#endif
216
Arnd Bergmann58eb5b672018-03-15 16:34:08 +0100217#if IS_ENABLED(CONFIG_PSTORE_842_COMPRESS)
Geliang Tangcb3bee02018-03-09 18:51:07 +0800218static int zbufsize_842(size_t size)
Geliang Tang239b7162018-02-13 14:40:39 +0800219{
Kees Cook55597402018-03-06 15:15:24 -0800220 return size;
Geliang Tang239b7162018-02-13 14:40:39 +0800221}
Kees Cookfe1d4752018-03-06 15:57:38 -0800222#endif
Geliang Tang8cfc8dd2016-02-18 22:04:22 +0800223
Geliang Tang1021bcf2018-08-01 19:23:37 +0800224#if IS_ENABLED(CONFIG_PSTORE_ZSTD_COMPRESS)
225static int zbufsize_zstd(size_t size)
226{
227 return ZSTD_compressBound(size);
228}
229#endif
230
Kees Cookfe1d4752018-03-06 15:57:38 -0800231static const struct pstore_zbackend *zbackend __ro_after_init;
232
233static const struct pstore_zbackend zbackends[] = {
Arnd Bergmann58eb5b672018-03-15 16:34:08 +0100234#if IS_ENABLED(CONFIG_PSTORE_DEFLATE_COMPRESS)
Kees Cookfe1d4752018-03-06 15:57:38 -0800235 {
Geliang Tangcb3bee02018-03-09 18:51:07 +0800236 .zbufsize = zbufsize_deflate,
237 .name = "deflate",
Kees Cookfe1d4752018-03-06 15:57:38 -0800238 },
239#endif
Arnd Bergmann58eb5b672018-03-15 16:34:08 +0100240#if IS_ENABLED(CONFIG_PSTORE_LZO_COMPRESS)
Kees Cookfe1d4752018-03-06 15:57:38 -0800241 {
Geliang Tangcb3bee02018-03-09 18:51:07 +0800242 .zbufsize = zbufsize_lzo,
Kees Cookfe1d4752018-03-06 15:57:38 -0800243 .name = "lzo",
244 },
245#endif
Arnd Bergmann58eb5b672018-03-15 16:34:08 +0100246#if IS_ENABLED(CONFIG_PSTORE_LZ4_COMPRESS)
Kees Cookfe1d4752018-03-06 15:57:38 -0800247 {
Geliang Tangcb3bee02018-03-09 18:51:07 +0800248 .zbufsize = zbufsize_lz4,
Kees Cookfe1d4752018-03-06 15:57:38 -0800249 .name = "lz4",
250 },
251#endif
Arnd Bergmann58eb5b672018-03-15 16:34:08 +0100252#if IS_ENABLED(CONFIG_PSTORE_LZ4HC_COMPRESS)
Kees Cookfe1d4752018-03-06 15:57:38 -0800253 {
Geliang Tangcb3bee02018-03-09 18:51:07 +0800254 .zbufsize = zbufsize_lz4,
Kees Cookfe1d4752018-03-06 15:57:38 -0800255 .name = "lz4hc",
256 },
257#endif
Arnd Bergmann58eb5b672018-03-15 16:34:08 +0100258#if IS_ENABLED(CONFIG_PSTORE_842_COMPRESS)
Kees Cookfe1d4752018-03-06 15:57:38 -0800259 {
Geliang Tangcb3bee02018-03-09 18:51:07 +0800260 .zbufsize = zbufsize_842,
Kees Cookfe1d4752018-03-06 15:57:38 -0800261 .name = "842",
262 },
263#endif
Geliang Tang1021bcf2018-08-01 19:23:37 +0800264#if IS_ENABLED(CONFIG_PSTORE_ZSTD_COMPRESS)
265 {
266 .zbufsize = zbufsize_zstd,
267 .name = "zstd",
268 },
269#endif
Kees Cookfe1d4752018-03-06 15:57:38 -0800270 { }
Geliang Tang8cfc8dd2016-02-18 22:04:22 +0800271};
Geliang Tang8cfc8dd2016-02-18 22:04:22 +0800272
273static int pstore_compress(const void *in, void *out,
Geliang Tangcb3bee02018-03-09 18:51:07 +0800274 unsigned int inlen, unsigned int outlen)
Geliang Tang8cfc8dd2016-02-18 22:04:22 +0800275{
Geliang Tangcb3bee02018-03-09 18:51:07 +0800276 int ret;
277
278 ret = crypto_comp_compress(tfm, in, inlen, out, &outlen);
279 if (ret) {
280 pr_err("crypto_comp_compress failed, ret = %d!\n", ret);
281 return ret;
282 }
283
284 return outlen;
Geliang Tang8cfc8dd2016-02-18 22:04:22 +0800285}
286
Geliang Tang8cfc8dd2016-02-18 22:04:22 +0800287static void allocate_buf_for_compression(void)
288{
Kees Cook95047b02018-10-17 14:00:12 -0700289 struct crypto_comp *ctx;
290 int size;
291 char *buf;
292
293 /* Skip if not built-in or compression backend not selected yet. */
Tobias Regnerye698aaf2018-04-06 09:25:17 +0200294 if (!IS_ENABLED(CONFIG_PSTORE_COMPRESS) || !zbackend)
Geliang Tangcb3bee02018-03-09 18:51:07 +0800295 return;
296
Kees Cook95047b02018-10-17 14:00:12 -0700297 /* Skip if no pstore backend yet or compression init already done. */
298 if (!psinfo || tfm)
299 return;
300
Geliang Tangcb3bee02018-03-09 18:51:07 +0800301 if (!crypto_has_comp(zbackend->name, 0, 0)) {
Kees Cook95047b02018-10-17 14:00:12 -0700302 pr_err("Unknown compression: %s\n", zbackend->name);
Geliang Tangcb3bee02018-03-09 18:51:07 +0800303 return;
304 }
305
Kees Cook95047b02018-10-17 14:00:12 -0700306 size = zbackend->zbufsize(psinfo->bufsize);
307 if (size <= 0) {
308 pr_err("Invalid compression size for %s: %d\n",
309 zbackend->name, size);
Geliang Tangcb3bee02018-03-09 18:51:07 +0800310 return;
311 }
312
Kees Cook95047b02018-10-17 14:00:12 -0700313 buf = kmalloc(size, GFP_KERNEL);
314 if (!buf) {
315 pr_err("Failed %d byte compression buffer allocation for: %s\n",
316 size, zbackend->name);
Geliang Tangcb3bee02018-03-09 18:51:07 +0800317 return;
Geliang Tang8cfc8dd2016-02-18 22:04:22 +0800318 }
Kees Cook95047b02018-10-17 14:00:12 -0700319
320 ctx = crypto_alloc_comp(zbackend->name, 0, 0);
321 if (IS_ERR_OR_NULL(ctx)) {
322 kfree(buf);
323 pr_err("crypto_alloc_comp('%s') failed: %ld\n", zbackend->name,
324 PTR_ERR(ctx));
325 return;
326 }
327
328 /* A non-NULL big_oops_buf indicates compression is available. */
329 tfm = ctx;
330 big_oops_buf_sz = size;
331 big_oops_buf = buf;
332
Kees Cook0eed84f2018-11-01 14:03:07 -0700333 pr_info("Using crash dump compression: %s\n", zbackend->name);
Geliang Tang8cfc8dd2016-02-18 22:04:22 +0800334}
335
336static void free_buf_for_compression(void)
337{
Pi-Hsun Shiha9fb94a2019-05-20 14:51:19 +0800338 if (IS_ENABLED(CONFIG_PSTORE_COMPRESS) && tfm) {
Geliang Tangcb3bee02018-03-09 18:51:07 +0800339 crypto_free_comp(tfm);
Pi-Hsun Shiha9fb94a2019-05-20 14:51:19 +0800340 tfm = NULL;
341 }
Geliang Tangcb3bee02018-03-09 18:51:07 +0800342 kfree(big_oops_buf);
343 big_oops_buf = NULL;
344 big_oops_buf_sz = 0;
Geliang Tangee1d2672015-10-20 00:39:03 -0700345}
346
Aruna Balakrishnaiahb0aad7a2013-08-16 13:53:10 -0700347/*
348 * Called when compression fails, since the printk buffer
349 * would be fetched for compression calling it again when
350 * compression fails would have moved the iterator of
351 * printk buffer which results in fetching old contents.
352 * Copy the recent messages from big_oops_buf to psinfo->buf
353 */
354static size_t copy_kmsg_to_buffer(int hsize, size_t len)
355{
356 size_t total_len;
357 size_t diff;
358
359 total_len = hsize + len;
360
361 if (total_len > psinfo->bufsize) {
362 diff = total_len - psinfo->bufsize + hsize;
363 memcpy(psinfo->buf, big_oops_buf, hsize);
364 memcpy(psinfo->buf + hsize, big_oops_buf + diff,
365 psinfo->bufsize - hsize);
366 total_len = psinfo->bufsize;
367 } else
368 memcpy(psinfo->buf, big_oops_buf, total_len);
369
370 return total_len;
371}
372
Kees Cooke581ca82017-05-19 15:10:31 -0700373void pstore_record_init(struct pstore_record *record,
374 struct pstore_info *psinfo)
375{
376 memset(record, 0, sizeof(*record));
377
378 record->psi = psinfo;
Kees Cookc7f3c5952017-05-19 15:29:10 -0700379
380 /* Report zeroed timestamp if called before timekeeping has resumed. */
Kees Cook7aaa8222018-05-14 15:50:52 -0700381 record->time = ns_to_timespec64(ktime_get_real_fast_ns());
Kees Cooke581ca82017-05-19 15:10:31 -0700382}
383
Tony Luckca01d6d2010-12-28 14:25:21 -0800384/*
Kees Cook0eed84f2018-11-01 14:03:07 -0700385 * callback from kmsg_dump. Save as much as we can (up to kmsg_bytes) from the
386 * end of the buffer.
Tony Luckca01d6d2010-12-28 14:25:21 -0800387 */
388static void pstore_dump(struct kmsg_dumper *dumper,
Kay Sieverse2ae7152012-06-15 14:07:51 +0200389 enum kmsg_dump_reason reason)
Tony Luckca01d6d2010-12-28 14:25:21 -0800390{
Kay Sieverse2ae7152012-06-15 14:07:51 +0200391 unsigned long total = 0;
Seiji Aguchi381b8722012-03-16 15:36:59 -0700392 const char *why;
Matthew Garrettb94fdd02011-07-21 16:57:54 -0400393 unsigned int part = 1;
Kay Sieverse2ae7152012-06-15 14:07:51 +0200394 int ret;
Tony Luckca01d6d2010-12-28 14:25:21 -0800395
Seiji Aguchi381b8722012-03-16 15:36:59 -0700396 why = get_reason_str(reason);
Tony Luck9f6af272011-03-22 16:01:49 -0700397
Kees Cookea84b582018-11-30 14:36:58 -0800398 if (down_trylock(&psinfo->buf_lock)) {
399 /* Failed to acquire lock: give up if we cannot wait. */
400 if (pstore_cannot_wait(reason)) {
401 pr_err("dump skipped in %s path: may corrupt error record\n",
402 in_nmi() ? "NMI" : why);
Li Pengcheng959217c82016-11-05 10:15:59 +0800403 return;
Seiji Aguchi9f244e92013-01-11 18:09:41 +0000404 }
Kees Cookea84b582018-11-30 14:36:58 -0800405 if (down_interruptible(&psinfo->buf_lock)) {
406 pr_err("could not grab semaphore?!\n");
407 return;
408 }
Namhyung Kim98e44fda2016-05-18 21:00:05 +0900409 }
Kees Cookea84b582018-11-30 14:36:58 -0800410
Tony Luckca01d6d2010-12-28 14:25:21 -0800411 oopscount++;
412 while (total < kmsg_bytes) {
Kay Sieverse2ae7152012-06-15 14:07:51 +0200413 char *dst;
Kees Cook76cc9582017-03-03 23:28:53 -0800414 size_t dst_size;
415 int header_size;
Aruna Balakrishnaiahb0aad7a2013-08-16 13:53:10 -0700416 int zipped_len = -1;
Kees Cook76cc9582017-03-03 23:28:53 -0800417 size_t dump_size;
Kees Cooke581ca82017-05-19 15:10:31 -0700418 struct pstore_record record;
419
420 pstore_record_init(&record, psinfo);
421 record.type = PSTORE_TYPE_DMESG;
422 record.count = oopscount;
423 record.reason = reason;
424 record.part = part;
425 record.buf = psinfo->buf;
Kay Sieverse2ae7152012-06-15 14:07:51 +0200426
Kees Cookea84b582018-11-30 14:36:58 -0800427 if (big_oops_buf) {
Aruna Balakrishnaiahb0aad7a2013-08-16 13:53:10 -0700428 dst = big_oops_buf;
Kees Cook76cc9582017-03-03 23:28:53 -0800429 dst_size = big_oops_buf_sz;
Namhyung Kim235f6d12016-05-18 21:00:06 +0900430 } else {
431 dst = psinfo->buf;
Kees Cook76cc9582017-03-03 23:28:53 -0800432 dst_size = psinfo->bufsize;
Namhyung Kim235f6d12016-05-18 21:00:06 +0900433 }
Tony Luckca01d6d2010-12-28 14:25:21 -0800434
Kees Cook76cc9582017-03-03 23:28:53 -0800435 /* Write dump header. */
436 header_size = snprintf(dst, dst_size, "%s#%d Part%u\n", why,
437 oopscount, part);
438 dst_size -= header_size;
Aruna Balakrishnaiahb0aad7a2013-08-16 13:53:10 -0700439
Kees Cook76cc9582017-03-03 23:28:53 -0800440 /* Write dump contents. */
441 if (!kmsg_dump_get_buffer(dumper, true, dst + header_size,
442 dst_size, &dump_size))
Namhyung Kim235f6d12016-05-18 21:00:06 +0900443 break;
444
Kees Cookea84b582018-11-30 14:36:58 -0800445 if (big_oops_buf) {
Aruna Balakrishnaiahb0aad7a2013-08-16 13:53:10 -0700446 zipped_len = pstore_compress(dst, psinfo->buf,
Kees Cook76cc9582017-03-03 23:28:53 -0800447 header_size + dump_size,
448 psinfo->bufsize);
Aruna Balakrishnaiahb0aad7a2013-08-16 13:53:10 -0700449
450 if (zipped_len > 0) {
Kees Cook76cc9582017-03-03 23:28:53 -0800451 record.compressed = true;
452 record.size = zipped_len;
Aruna Balakrishnaiahb0aad7a2013-08-16 13:53:10 -0700453 } else {
Kees Cook76cc9582017-03-03 23:28:53 -0800454 record.size = copy_kmsg_to_buffer(header_size,
455 dump_size);
Aruna Balakrishnaiahb0aad7a2013-08-16 13:53:10 -0700456 }
457 } else {
Kees Cook76cc9582017-03-03 23:28:53 -0800458 record.size = header_size + dump_size;
Aruna Balakrishnaiahb0aad7a2013-08-16 13:53:10 -0700459 }
Tony Luckca01d6d2010-12-28 14:25:21 -0800460
Kees Cook76cc9582017-03-03 23:28:53 -0800461 ret = psinfo->write(&record);
Chen Gongb238b8f2011-10-12 09:17:24 -0700462 if (ret == 0 && reason == KMSG_DUMP_OOPS && pstore_is_mounted())
Luck, Tony6dda9262011-08-11 15:14:39 -0700463 pstore_new_entry = 1;
Kay Sieverse2ae7152012-06-15 14:07:51 +0200464
Kees Cook76cc9582017-03-03 23:28:53 -0800465 total += record.size;
Matthew Garrett56280682011-07-21 16:57:53 -0400466 part++;
Tony Luckca01d6d2010-12-28 14:25:21 -0800467 }
Kees Cookea84b582018-11-30 14:36:58 -0800468
469 up(&psinfo->buf_lock);
Tony Luckca01d6d2010-12-28 14:25:21 -0800470}
471
472static struct kmsg_dumper pstore_dumper = {
473 .dump = pstore_dump,
474};
475
Geliang Tang306e5c22015-10-31 23:23:15 +0800476/*
477 * Register with kmsg_dump to save last part of console log on panic.
478 */
Geliang Tang18730412015-10-20 00:39:02 -0700479static void pstore_register_kmsg(void)
480{
481 kmsg_dump_register(&pstore_dumper);
482}
483
Geliang Tangee1d2672015-10-20 00:39:03 -0700484static void pstore_unregister_kmsg(void)
485{
486 kmsg_dump_unregister(&pstore_dumper);
487}
488
Anton Vorontsovf29e5952012-05-26 06:20:19 -0700489#ifdef CONFIG_PSTORE_CONSOLE
490static void pstore_console_write(struct console *con, const char *s, unsigned c)
491{
Kees Cookb77fa612018-11-01 14:08:07 -0700492 struct pstore_record record;
Anton Vorontsovf29e5952012-05-26 06:20:19 -0700493
Yue Hu4c6c4d32019-01-31 18:12:46 +0800494 if (!c)
495 return;
496
Kees Cookb77fa612018-11-01 14:08:07 -0700497 pstore_record_init(&record, psinfo);
498 record.type = PSTORE_TYPE_CONSOLE;
Anton Vorontsovf29e5952012-05-26 06:20:19 -0700499
Kees Cookb77fa612018-11-01 14:08:07 -0700500 record.buf = (char *)s;
501 record.size = c;
502 psinfo->write(&record);
Anton Vorontsovf29e5952012-05-26 06:20:19 -0700503}
504
505static struct console pstore_console = {
506 .name = "pstore",
507 .write = pstore_console_write,
508 .flags = CON_PRINTBUFFER | CON_ENABLED | CON_ANYTIME,
509 .index = -1,
510};
511
512static void pstore_register_console(void)
513{
514 register_console(&pstore_console);
515}
Geliang Tangee1d2672015-10-20 00:39:03 -0700516
517static void pstore_unregister_console(void)
518{
519 unregister_console(&pstore_console);
520}
Anton Vorontsovf29e5952012-05-26 06:20:19 -0700521#else
522static void pstore_register_console(void) {}
Geliang Tangee1d2672015-10-20 00:39:03 -0700523static void pstore_unregister_console(void) {}
Anton Vorontsovf29e5952012-05-26 06:20:19 -0700524#endif
525
Kees Cook4c9ec212017-03-05 22:41:10 -0800526static int pstore_write_user_compat(struct pstore_record *record,
527 const char __user *buf)
Mark Salyzyn5bf6d1b2016-09-01 08:13:46 -0700528{
Kees Cook30800d92017-03-07 13:57:11 -0800529 int ret = 0;
Mark Salyzyn5bf6d1b2016-09-01 08:13:46 -0700530
Kees Cook30800d92017-03-07 13:57:11 -0800531 if (record->buf)
532 return -EINVAL;
Mark Salyzyn5bf6d1b2016-09-01 08:13:46 -0700533
Geliang Tang077090a2017-04-29 09:45:16 +0800534 record->buf = memdup_user(buf, record->size);
Hirofumi Nakagawadfd6fa32017-09-26 03:21:27 +0900535 if (IS_ERR(record->buf)) {
Geliang Tang077090a2017-04-29 09:45:16 +0800536 ret = PTR_ERR(record->buf);
Kees Cook30800d92017-03-07 13:57:11 -0800537 goto out;
Mark Salyzyn5bf6d1b2016-09-01 08:13:46 -0700538 }
Kees Cook30800d92017-03-07 13:57:11 -0800539
540 ret = record->psi->write(record);
541
Kees Cook30800d92017-03-07 13:57:11 -0800542 kfree(record->buf);
Geliang Tang077090a2017-04-29 09:45:16 +0800543out:
Kees Cook30800d92017-03-07 13:57:11 -0800544 record->buf = NULL;
545
546 return unlikely(ret < 0) ? ret : record->size;
Mark Salyzyn5bf6d1b2016-09-01 08:13:46 -0700547}
548
Tony Luckca01d6d2010-12-28 14:25:21 -0800549/*
550 * platform specific persistent storage driver registers with
551 * us here. If pstore is already mounted, call the platform
552 * read function right away to populate the file system. If not
553 * then the pstore mount code will call us later to fill out
554 * the file system.
Tony Luckca01d6d2010-12-28 14:25:21 -0800555 */
556int pstore_register(struct pstore_info *psi)
557{
558 struct module *owner = psi->owner;
559
Kees Cook0d7cd092017-03-03 12:11:40 -0800560 if (backend && strcmp(backend, psi->name)) {
561 pr_warn("ignoring unexpected backend '%s'\n", psi->name);
Lenny Szubowicz8e48b1a2013-06-28 17:11:33 -0400562 return -EPERM;
Kees Cook0d7cd092017-03-03 12:11:40 -0800563 }
Lenny Szubowicz8e48b1a2013-06-28 17:11:33 -0400564
Kees Cook4c9ec212017-03-05 22:41:10 -0800565 /* Sanity check flags. */
566 if (!psi->flags) {
567 pr_warn("backend '%s' must support at least one frontend\n",
568 psi->name);
569 return -EINVAL;
570 }
571
572 /* Check for required functions. */
573 if (!psi->read || !psi->write) {
574 pr_warn("backend '%s' must implement read() and write()\n",
575 psi->name);
576 return -EINVAL;
577 }
578
Tony Luckca01d6d2010-12-28 14:25:21 -0800579 spin_lock(&pstore_lock);
580 if (psinfo) {
Kees Cook0d7cd092017-03-03 12:11:40 -0800581 pr_warn("backend '%s' already loaded: ignoring '%s'\n",
582 psinfo->name, psi->name);
Tony Luckca01d6d2010-12-28 14:25:21 -0800583 spin_unlock(&pstore_lock);
584 return -EBUSY;
585 }
Matthew Garrettdee28e72011-07-21 16:57:55 -0400586
Kees Cook4c9ec212017-03-05 22:41:10 -0800587 if (!psi->write_user)
588 psi->write_user = pstore_write_user_compat;
Tony Luckca01d6d2010-12-28 14:25:21 -0800589 psinfo = psi;
Kees Cookf6f82852011-11-17 12:58:07 -0800590 mutex_init(&psinfo->read_mutex);
Kees Cookea84b582018-11-30 14:36:58 -0800591 sema_init(&psinfo->buf_lock, 1);
Tony Luckca01d6d2010-12-28 14:25:21 -0800592 spin_unlock(&pstore_lock);
593
594 if (owner && !try_module_get(owner)) {
595 psinfo = NULL;
596 return -EINVAL;
597 }
598
Kees Cook8880fa32019-05-30 23:37:29 -0700599 if (psi->flags & PSTORE_FLAGS_DMESG)
600 allocate_buf_for_compression();
Aruna Balakrishnaiahb0aad7a2013-08-16 13:53:10 -0700601
Tony Luckca01d6d2010-12-28 14:25:21 -0800602 if (pstore_is_mounted())
Luck, Tony6dda9262011-08-11 15:14:39 -0700603 pstore_get_records(0);
Tony Luckca01d6d2010-12-28 14:25:21 -0800604
Namhyung Kimc950fd62016-07-28 00:08:25 +0900605 if (psi->flags & PSTORE_FLAGS_DMESG)
606 pstore_register_kmsg();
607 if (psi->flags & PSTORE_FLAGS_CONSOLE)
Luck, Tonydf36ac12013-12-18 15:17:10 -0800608 pstore_register_console();
Namhyung Kimc950fd62016-07-28 00:08:25 +0900609 if (psi->flags & PSTORE_FLAGS_FTRACE)
Luck, Tonydf36ac12013-12-18 15:17:10 -0800610 pstore_register_ftrace();
Namhyung Kimc950fd62016-07-28 00:08:25 +0900611 if (psi->flags & PSTORE_FLAGS_PMSG)
Mark Salyzyn9d5438f2015-01-16 16:01:10 -0800612 pstore_register_pmsg();
Tony Luckca01d6d2010-12-28 14:25:21 -0800613
Kees Cook6330d552017-03-06 12:42:12 -0800614 /* Start watching for new records, if desired. */
Anton Vorontsova3f5f072012-05-26 06:20:28 -0700615 if (pstore_update_ms >= 0) {
616 pstore_timer.expires = jiffies +
617 msecs_to_jiffies(pstore_update_ms);
618 add_timer(&pstore_timer);
619 }
Luck, Tony6dda9262011-08-11 15:14:39 -0700620
Wang Long42222c22015-05-21 09:34:22 -0700621 /*
622 * Update the module parameter backend, so it is visible
623 * through /sys/module/pstore/parameters/backend
624 */
625 backend = psi->name;
626
Fabian Frederickef748852014-06-06 14:37:31 -0700627 pr_info("Registered %s as persistent store backend\n", psi->name);
Lenny Szubowicz8e48b1a2013-06-28 17:11:33 -0400628
Kees Cook1344dd82017-03-03 17:45:38 -0800629 module_put(owner);
630
Tony Luckca01d6d2010-12-28 14:25:21 -0800631 return 0;
632}
633EXPORT_SYMBOL_GPL(pstore_register);
634
Geliang Tangee1d2672015-10-20 00:39:03 -0700635void pstore_unregister(struct pstore_info *psi)
636{
Kees Cook6330d552017-03-06 12:42:12 -0800637 /* Stop timer and make sure all work has finished. */
638 pstore_update_ms = -1;
639 del_timer_sync(&pstore_timer);
640 flush_work(&pstore_work);
641
Namhyung Kimc950fd62016-07-28 00:08:25 +0900642 if (psi->flags & PSTORE_FLAGS_PMSG)
Kees Cooka1db8062016-05-19 10:59:03 -0400643 pstore_unregister_pmsg();
Namhyung Kimc950fd62016-07-28 00:08:25 +0900644 if (psi->flags & PSTORE_FLAGS_FTRACE)
Kees Cooka1db8062016-05-19 10:59:03 -0400645 pstore_unregister_ftrace();
Namhyung Kimc950fd62016-07-28 00:08:25 +0900646 if (psi->flags & PSTORE_FLAGS_CONSOLE)
Kees Cooka1db8062016-05-19 10:59:03 -0400647 pstore_unregister_console();
Namhyung Kimc950fd62016-07-28 00:08:25 +0900648 if (psi->flags & PSTORE_FLAGS_DMESG)
649 pstore_unregister_kmsg();
Geliang Tangee1d2672015-10-20 00:39:03 -0700650
651 free_buf_for_compression();
652
653 psinfo = NULL;
654 backend = NULL;
655}
656EXPORT_SYMBOL_GPL(pstore_unregister);
657
Kees Cook634f8f52017-03-03 17:35:25 -0800658static void decompress_record(struct pstore_record *record)
659{
Kees Cookbdabc8e2018-10-26 01:17:07 -0700660 int ret;
Kees Cook634f8f52017-03-03 17:35:25 -0800661 int unzipped_len;
Kees Cookbdabc8e2018-10-26 01:17:07 -0700662 char *unzipped, *workspace;
Kees Cook634f8f52017-03-03 17:35:25 -0800663
Ankit Kumar4a16d1c2017-05-23 11:16:52 +0530664 if (!record->compressed)
665 return;
666
Kees Cook634f8f52017-03-03 17:35:25 -0800667 /* Only PSTORE_TYPE_DMESG support compression. */
Ankit Kumar4a16d1c2017-05-23 11:16:52 +0530668 if (record->type != PSTORE_TYPE_DMESG) {
Kees Cook634f8f52017-03-03 17:35:25 -0800669 pr_warn("ignored compressed record type %d\n", record->type);
670 return;
671 }
672
Kees Cookbdabc8e2018-10-26 01:17:07 -0700673 /* Missing compression buffer means compression was not initialized. */
Kees Cook634f8f52017-03-03 17:35:25 -0800674 if (!big_oops_buf) {
Kees Cookbdabc8e2018-10-26 01:17:07 -0700675 pr_warn("no decompression method initialized!\n");
Kees Cook634f8f52017-03-03 17:35:25 -0800676 return;
677 }
678
Kees Cookbdabc8e2018-10-26 01:17:07 -0700679 /* Allocate enough space to hold max decompression and ECC. */
680 unzipped_len = big_oops_buf_sz;
681 workspace = kmalloc(unzipped_len + record->ecc_notice_size,
682 GFP_KERNEL);
683 if (!workspace)
Kees Cook7e8cc8d2017-03-04 22:28:46 -0800684 return;
Kees Cook7e8cc8d2017-03-04 22:28:46 -0800685
Kees Cookbdabc8e2018-10-26 01:17:07 -0700686 /* After decompression "unzipped_len" is almost certainly smaller. */
687 ret = crypto_comp_decompress(tfm, record->buf, record->size,
688 workspace, &unzipped_len);
689 if (ret) {
690 pr_err("crypto_comp_decompress failed, ret = %d!\n", ret);
691 kfree(workspace);
Kees Cook7e8cc8d2017-03-04 22:28:46 -0800692 return;
693 }
Kees Cook7e8cc8d2017-03-04 22:28:46 -0800694
695 /* Append ECC notice to decompressed buffer. */
Kees Cookbdabc8e2018-10-26 01:17:07 -0700696 memcpy(workspace + unzipped_len, record->buf + record->size,
Kees Cook7e8cc8d2017-03-04 22:28:46 -0800697 record->ecc_notice_size);
698
Kees Cookbdabc8e2018-10-26 01:17:07 -0700699 /* Copy decompressed contents into an minimum-sized allocation. */
700 unzipped = kmemdup(workspace, unzipped_len + record->ecc_notice_size,
701 GFP_KERNEL);
702 kfree(workspace);
703 if (!unzipped)
704 return;
705
706 /* Swap out compressed contents with decompressed contents. */
Kees Cook7e8cc8d2017-03-04 22:28:46 -0800707 kfree(record->buf);
Kees Cookbdabc8e2018-10-26 01:17:07 -0700708 record->buf = unzipped;
Kees Cook7e8cc8d2017-03-04 22:28:46 -0800709 record->size = unzipped_len;
710 record->compressed = false;
Kees Cook634f8f52017-03-03 17:35:25 -0800711}
712
Tony Luckca01d6d2010-12-28 14:25:21 -0800713/*
Kees Cook3a7d2fd2017-04-27 15:53:21 -0700714 * Read all the records from one persistent store backend. Create
Luck, Tony6dda9262011-08-11 15:14:39 -0700715 * files in our filesystem. Don't warn about -EEXIST errors
716 * when we are re-scanning the backing store looking to add new
717 * error records.
Tony Luckca01d6d2010-12-28 14:25:21 -0800718 */
Kees Cook3a7d2fd2017-04-27 15:53:21 -0700719void pstore_get_backend_records(struct pstore_info *psi,
720 struct dentry *root, int quiet)
Tony Luckca01d6d2010-12-28 14:25:21 -0800721{
Kees Cook2a2b0ac2017-03-04 22:57:26 -0800722 int failed = 0;
Kees Cook656de422017-05-16 12:03:31 -0700723 unsigned int stop_loop = 65536;
Tony Luckca01d6d2010-12-28 14:25:21 -0800724
Kees Cook3a7d2fd2017-04-27 15:53:21 -0700725 if (!psi || !root)
Tony Luckca01d6d2010-12-28 14:25:21 -0800726 return;
727
Kees Cookf6f82852011-11-17 12:58:07 -0800728 mutex_lock(&psi->read_mutex);
Kees Cook2174f6d2011-11-18 13:49:00 -0800729 if (psi->open && psi->open(psi))
Chen Gong06cf91b2011-05-16 11:00:27 -0700730 goto out;
731
Kees Cook1dfff7d2017-03-04 22:46:41 -0800732 /*
733 * Backend callback read() allocates record.buf. decompress_record()
734 * may reallocate record.buf. On success, pstore_mkfile() will keep
735 * the record.buf, so free it only on failure.
736 */
Kees Cook656de422017-05-16 12:03:31 -0700737 for (; stop_loop; stop_loop--) {
Kees Cook2a2b0ac2017-03-04 22:57:26 -0800738 struct pstore_record *record;
739 int rc;
740
741 record = kzalloc(sizeof(*record), GFP_KERNEL);
742 if (!record) {
743 pr_err("out of memory creating record\n");
744 break;
745 }
Kees Cooke581ca82017-05-19 15:10:31 -0700746 pstore_record_init(record, psi);
Kees Cook2a2b0ac2017-03-04 22:57:26 -0800747
748 record->size = psi->read(record);
749
750 /* No more records left in backend? */
Douglas Andersonf6525b92017-05-30 15:50:38 -0700751 if (record->size <= 0) {
752 kfree(record);
Kees Cook2a2b0ac2017-03-04 22:57:26 -0800753 break;
Douglas Andersonf6525b92017-05-30 15:50:38 -0700754 }
Kees Cook2a2b0ac2017-03-04 22:57:26 -0800755
756 decompress_record(record);
Kees Cook3a7d2fd2017-04-27 15:53:21 -0700757 rc = pstore_mkfile(root, record);
Kees Cook1dfff7d2017-03-04 22:46:41 -0800758 if (rc) {
Kees Cook83f70f02017-03-04 23:12:24 -0800759 /* pstore_mkfile() did not take record, so free it. */
Kees Cook2a2b0ac2017-03-04 22:57:26 -0800760 kfree(record->buf);
Kees Cook83f70f02017-03-04 23:12:24 -0800761 kfree(record);
Kees Cook1dfff7d2017-03-04 22:46:41 -0800762 if (rc != -EEXIST || !quiet)
763 failed++;
764 }
Tony Luckca01d6d2010-12-28 14:25:21 -0800765 }
Kees Cook2174f6d2011-11-18 13:49:00 -0800766 if (psi->close)
767 psi->close(psi);
Chen Gong06cf91b2011-05-16 11:00:27 -0700768out:
Kees Cookf6f82852011-11-17 12:58:07 -0800769 mutex_unlock(&psi->read_mutex);
Tony Luckca01d6d2010-12-28 14:25:21 -0800770
771 if (failed)
Kees Cook656de422017-05-16 12:03:31 -0700772 pr_warn("failed to create %d record(s) from '%s'\n",
Fabian Frederickef748852014-06-06 14:37:31 -0700773 failed, psi->name);
Kees Cook656de422017-05-16 12:03:31 -0700774 if (!stop_loop)
775 pr_err("looping? Too many records seen from '%s'\n",
776 psi->name);
Tony Luckca01d6d2010-12-28 14:25:21 -0800777}
778
Luck, Tony6dda9262011-08-11 15:14:39 -0700779static void pstore_dowork(struct work_struct *work)
780{
781 pstore_get_records(1);
782}
783
Kees Cook24ed9602017-08-28 11:28:21 -0700784static void pstore_timefunc(struct timer_list *unused)
Luck, Tony6dda9262011-08-11 15:14:39 -0700785{
786 if (pstore_new_entry) {
787 pstore_new_entry = 0;
788 schedule_work(&pstore_work);
789 }
790
Kees Cook6330d552017-03-06 12:42:12 -0800791 if (pstore_update_ms >= 0)
792 mod_timer(&pstore_timer,
793 jiffies + msecs_to_jiffies(pstore_update_ms));
Luck, Tony6dda9262011-08-11 15:14:39 -0700794}
795
Kees Cookfe1d4752018-03-06 15:57:38 -0800796void __init pstore_choose_compression(void)
797{
798 const struct pstore_zbackend *step;
799
800 if (!compress)
801 return;
802
803 for (step = zbackends; step->name; step++) {
804 if (!strcmp(compress, step->name)) {
805 zbackend = step;
Kees Cookfe1d4752018-03-06 15:57:38 -0800806 return;
807 }
808 }
809}
810
Kees Cookcb095af2018-10-18 11:17:42 -0700811static int __init pstore_init(void)
812{
813 int ret;
814
815 pstore_choose_compression();
816
Joel Fernandes (Google)41603162018-10-17 03:13:55 -0700817 /*
818 * Check if any pstore backends registered earlier but did not
819 * initialize compression because crypto was not ready. If so,
820 * initialize compression now.
821 */
Kees Cook95047b02018-10-17 14:00:12 -0700822 allocate_buf_for_compression();
Joel Fernandes (Google)41603162018-10-17 03:13:55 -0700823
Kees Cookcb095af2018-10-18 11:17:42 -0700824 ret = pstore_init_fs();
825 if (ret)
826 return ret;
827
828 return 0;
829}
Joel Fernandes (Google)41603162018-10-17 03:13:55 -0700830late_initcall(pstore_init);
Kees Cookcb095af2018-10-18 11:17:42 -0700831
832static void __exit pstore_exit(void)
833{
834 pstore_exit_fs();
835}
836module_exit(pstore_exit)
837
Kees Cookfe1d4752018-03-06 15:57:38 -0800838module_param(compress, charp, 0444);
839MODULE_PARM_DESC(compress, "Pstore compression to use");
840
Matthew Garrettdee28e72011-07-21 16:57:55 -0400841module_param(backend, charp, 0444);
842MODULE_PARM_DESC(backend, "Pstore backend to use");
Kees Cookcb095af2018-10-18 11:17:42 -0700843
844MODULE_AUTHOR("Tony Luck <tony.luck@intel.com>");
845MODULE_LICENSE("GPL");