blob: 75887a269b6420307a42beecf99ed8d8a55b9d34 [file] [log] [blame]
Tony Luckca01d6d2010-12-28 14:25:21 -08001/*
2 * Persistent Storage - platform driver interface parts.
3 *
Anton Vorontsovf29e5952012-05-26 06:20:19 -07004 * Copyright (C) 2007-2008 Google, Inc.
Tony Luckca01d6d2010-12-28 14:25:21 -08005 * 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 Frederickef748852014-06-06 14:37:31 -070021#define pr_fmt(fmt) "pstore: " fmt
22
Tony Luckca01d6d2010-12-28 14:25:21 -080023#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 Vorontsovf29e5952012-05-26 06:20:19 -070028#include <linux/console.h>
Tony Luckca01d6d2010-12-28 14:25:21 -080029#include <linux/module.h>
30#include <linux/pstore.h>
Arnd Bergmann58eb5b672018-03-15 16:34:08 +010031#if IS_ENABLED(CONFIG_PSTORE_LZO_COMPRESS)
Geliang Tang8cfc8dd2016-02-18 22:04:22 +080032#include <linux/lzo.h>
33#endif
Arnd Bergmann58eb5b672018-03-15 16:34:08 +010034#if IS_ENABLED(CONFIG_PSTORE_LZ4_COMPRESS) || IS_ENABLED(CONFIG_PSTORE_LZ4HC_COMPRESS)
Geliang Tang8cfc8dd2016-02-18 22:04:22 +080035#include <linux/lz4.h>
36#endif
Geliang Tang1021bcf2018-08-01 19:23:37 +080037#if IS_ENABLED(CONFIG_PSTORE_ZSTD_COMPRESS)
38#include <linux/zstd.h>
39#endif
Geliang Tangcb3bee02018-03-09 18:51:07 +080040#include <linux/crypto.h>
Tony Luckca01d6d2010-12-28 14:25:21 -080041#include <linux/string.h>
Luck, Tony6dda9262011-08-11 15:14:39 -070042#include <linux/timer.h>
Tony Luckca01d6d2010-12-28 14:25:21 -080043#include <linux/slab.h>
44#include <linux/uaccess.h>
Anton Vorontsova3f5f072012-05-26 06:20:28 -070045#include <linux/jiffies.h>
Luck, Tony6dda9262011-08-11 15:14:39 -070046#include <linux/workqueue.h>
Tony Luckca01d6d2010-12-28 14:25:21 -080047
48#include "internal.h"
49
50/*
Luck, Tony6dda9262011-08-11 15:14:39 -070051 * 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 Vorontsov521f72882012-05-26 06:20:29 -070055static int pstore_update_ms = -1;
Anton Vorontsova3f5f072012-05-26 06:20:28 -070056module_param_named(update_ms, pstore_update_ms, int, 0600);
57MODULE_PARM_DESC(update_ms, "milliseconds before pstore updates its content "
Anton Vorontsov521f72882012-05-26 06:20:29 -070058 "(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, Tony6dda9262011-08-11 15:14:39 -070061
Joel Fernandes (Google)f0f23e52018-11-03 16:38:16 -070062/* Names should be in the same order as the enum pstore_type_id */
63static 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, Tony6dda9262011-08-11 15:14:39 -070075static int pstore_new_entry;
76
Kees Cook24ed9602017-08-28 11:28:21 -070077static void pstore_timefunc(struct timer_list *);
Kees Cook1d27e3e2017-10-04 16:27:04 -070078static DEFINE_TIMER(pstore_timer, pstore_timefunc);
Luck, Tony6dda9262011-08-11 15:14:39 -070079
80static void pstore_dowork(struct work_struct *);
81static DECLARE_WORK(pstore_work, pstore_dowork);
82
83/*
Tony Luckca01d6d2010-12-28 14:25:21 -080084 * pstore_lock just protects "psinfo" during
85 * calls to pstore_register()
86 */
87static DEFINE_SPINLOCK(pstore_lock);
Anton Vorontsov060287b2012-07-09 17:10:41 -070088struct pstore_info *psinfo;
Tony Luckca01d6d2010-12-28 14:25:21 -080089
Matthew Garrettdee28e72011-07-21 16:57:55 -040090static char *backend;
Kees Cookfe1d4752018-03-06 15:57:38 -080091static char *compress =
92#ifdef CONFIG_PSTORE_COMPRESS_DEFAULT
93 CONFIG_PSTORE_COMPRESS_DEFAULT;
94#else
95 NULL;
96#endif
Matthew Garrettdee28e72011-07-21 16:57:55 -040097
Aruna Balakrishnaiahb0aad7a2013-08-16 13:53:10 -070098/* Compression parameters */
Geliang Tangcb3bee02018-03-09 18:51:07 +080099static struct crypto_comp *tfm;
Geliang Tang8cfc8dd2016-02-18 22:04:22 +0800100
101struct pstore_zbackend {
Geliang Tangcb3bee02018-03-09 18:51:07 +0800102 int (*zbufsize)(size_t size);
Geliang Tang8cfc8dd2016-02-18 22:04:22 +0800103 const char *name;
104};
Aruna Balakrishnaiahb0aad7a2013-08-16 13:53:10 -0700105
106static char *big_oops_buf;
107static size_t big_oops_buf_sz;
108
Luck, Tony366f7e72011-03-18 15:33:43 -0700109/* How much of the console log to snapshot */
David Howells349d7432017-07-05 16:24:34 +0100110unsigned long kmsg_bytes = PSTORE_DEFAULT_KMSG_BYTES;
Tony Luckca01d6d2010-12-28 14:25:21 -0800111
Luck, Tony366f7e72011-03-18 15:33:43 -0700112void pstore_set_kmsg_bytes(int bytes)
Tony Luckca01d6d2010-12-28 14:25:21 -0800113{
Luck, Tony366f7e72011-03-18 15:33:43 -0700114 kmsg_bytes = bytes;
Tony Luckca01d6d2010-12-28 14:25:21 -0800115}
116
Tony Luckca01d6d2010-12-28 14:25:21 -0800117/* Tag each group of saved records with a sequence number */
118static int oopscount;
119
Joel Fernandes (Google)f0f23e52018-11-03 16:38:16 -0700120const 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}
129EXPORT_SYMBOL_GPL(pstore_type_to_name);
130
131enum 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}
142EXPORT_SYMBOL_GPL(pstore_name_to_type);
143
Seiji Aguchi381b8722012-03-16 15:36:59 -0700144static 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 Luck9f6af272011-03-22 16:01:49 -0700163
Kees Cookea84b582018-11-30 14:36:58 -0800164/*
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 */
169static bool pstore_cannot_wait(enum kmsg_dump_reason reason)
Seiji Aguchi9f244e92013-01-11 18:09:41 +0000170{
Kees Cookea84b582018-11-30 14:36:58 -0800171 /* In NMI path, pstore shouldn't block regardless of reason. */
Seiji Aguchi9f244e92013-01-11 18:09:41 +0000172 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 Cookea84b582018-11-30 14:36:58 -0800178 /* Emergency restart shouldn't be blocked. */
Seiji Aguchi9f244e92013-01-11 18:09:41 +0000179 case KMSG_DUMP_EMERG:
180 return true;
181 default:
182 return false;
183 }
184}
Seiji Aguchi9f244e92013-01-11 18:09:41 +0000185
Arnd Bergmann58eb5b672018-03-15 16:34:08 +0100186#if IS_ENABLED(CONFIG_PSTORE_DEFLATE_COMPRESS)
Geliang Tangcb3bee02018-03-09 18:51:07 +0800187static int zbufsize_deflate(size_t size)
Aruna Balakrishnaiahb0aad7a2013-08-16 13:53:10 -0700188{
Aruna Balakrishnaiah7de8fe22013-09-11 10:57:41 -0700189 size_t cmpr;
Aruna Balakrishnaiahb0aad7a2013-08-16 13:53:10 -0700190
Geliang Tangcb3bee02018-03-09 18:51:07 +0800191 switch (size) {
Aruna Balakrishnaiah7de8fe22013-09-11 10:57:41 -0700192 /* 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 Tangcb3bee02018-03-09 18:51:07 +0800211 return (size * 100) / cmpr;
Geliang Tang8cfc8dd2016-02-18 22:04:22 +0800212}
Geliang Tang8cfc8dd2016-02-18 22:04:22 +0800213#endif
214
Arnd Bergmann58eb5b672018-03-15 16:34:08 +0100215#if IS_ENABLED(CONFIG_PSTORE_LZO_COMPRESS)
Geliang Tangcb3bee02018-03-09 18:51:07 +0800216static int zbufsize_lzo(size_t size)
Geliang Tang8cfc8dd2016-02-18 22:04:22 +0800217{
Geliang Tangcb3bee02018-03-09 18:51:07 +0800218 return lzo1x_worst_compress(size);
Geliang Tang8cfc8dd2016-02-18 22:04:22 +0800219}
Geliang Tang8cfc8dd2016-02-18 22:04:22 +0800220#endif
221
Arnd Bergmann58eb5b672018-03-15 16:34:08 +0100222#if IS_ENABLED(CONFIG_PSTORE_LZ4_COMPRESS) || IS_ENABLED(CONFIG_PSTORE_LZ4HC_COMPRESS)
Geliang Tangcb3bee02018-03-09 18:51:07 +0800223static int zbufsize_lz4(size_t size)
Geliang Tang8cfc8dd2016-02-18 22:04:22 +0800224{
Geliang Tangcb3bee02018-03-09 18:51:07 +0800225 return LZ4_compressBound(size);
Geliang Tang239b7162018-02-13 14:40:39 +0800226}
Geliang Tang239b7162018-02-13 14:40:39 +0800227#endif
228
Arnd Bergmann58eb5b672018-03-15 16:34:08 +0100229#if IS_ENABLED(CONFIG_PSTORE_842_COMPRESS)
Geliang Tangcb3bee02018-03-09 18:51:07 +0800230static int zbufsize_842(size_t size)
Geliang Tang239b7162018-02-13 14:40:39 +0800231{
Kees Cook55597402018-03-06 15:15:24 -0800232 return size;
Geliang Tang239b7162018-02-13 14:40:39 +0800233}
Kees Cookfe1d4752018-03-06 15:57:38 -0800234#endif
Geliang Tang8cfc8dd2016-02-18 22:04:22 +0800235
Geliang Tang1021bcf2018-08-01 19:23:37 +0800236#if IS_ENABLED(CONFIG_PSTORE_ZSTD_COMPRESS)
237static int zbufsize_zstd(size_t size)
238{
239 return ZSTD_compressBound(size);
240}
241#endif
242
Kees Cookfe1d4752018-03-06 15:57:38 -0800243static const struct pstore_zbackend *zbackend __ro_after_init;
244
245static const struct pstore_zbackend zbackends[] = {
Arnd Bergmann58eb5b672018-03-15 16:34:08 +0100246#if IS_ENABLED(CONFIG_PSTORE_DEFLATE_COMPRESS)
Kees Cookfe1d4752018-03-06 15:57:38 -0800247 {
Geliang Tangcb3bee02018-03-09 18:51:07 +0800248 .zbufsize = zbufsize_deflate,
249 .name = "deflate",
Kees Cookfe1d4752018-03-06 15:57:38 -0800250 },
251#endif
Arnd Bergmann58eb5b672018-03-15 16:34:08 +0100252#if IS_ENABLED(CONFIG_PSTORE_LZO_COMPRESS)
Kees Cookfe1d4752018-03-06 15:57:38 -0800253 {
Geliang Tangcb3bee02018-03-09 18:51:07 +0800254 .zbufsize = zbufsize_lzo,
Kees Cookfe1d4752018-03-06 15:57:38 -0800255 .name = "lzo",
256 },
257#endif
Arnd Bergmann58eb5b672018-03-15 16:34:08 +0100258#if IS_ENABLED(CONFIG_PSTORE_LZ4_COMPRESS)
Kees Cookfe1d4752018-03-06 15:57:38 -0800259 {
Geliang Tangcb3bee02018-03-09 18:51:07 +0800260 .zbufsize = zbufsize_lz4,
Kees Cookfe1d4752018-03-06 15:57:38 -0800261 .name = "lz4",
262 },
263#endif
Arnd Bergmann58eb5b672018-03-15 16:34:08 +0100264#if IS_ENABLED(CONFIG_PSTORE_LZ4HC_COMPRESS)
Kees Cookfe1d4752018-03-06 15:57:38 -0800265 {
Geliang Tangcb3bee02018-03-09 18:51:07 +0800266 .zbufsize = zbufsize_lz4,
Kees Cookfe1d4752018-03-06 15:57:38 -0800267 .name = "lz4hc",
268 },
269#endif
Arnd Bergmann58eb5b672018-03-15 16:34:08 +0100270#if IS_ENABLED(CONFIG_PSTORE_842_COMPRESS)
Kees Cookfe1d4752018-03-06 15:57:38 -0800271 {
Geliang Tangcb3bee02018-03-09 18:51:07 +0800272 .zbufsize = zbufsize_842,
Kees Cookfe1d4752018-03-06 15:57:38 -0800273 .name = "842",
274 },
275#endif
Geliang Tang1021bcf2018-08-01 19:23:37 +0800276#if IS_ENABLED(CONFIG_PSTORE_ZSTD_COMPRESS)
277 {
278 .zbufsize = zbufsize_zstd,
279 .name = "zstd",
280 },
281#endif
Kees Cookfe1d4752018-03-06 15:57:38 -0800282 { }
Geliang Tang8cfc8dd2016-02-18 22:04:22 +0800283};
Geliang Tang8cfc8dd2016-02-18 22:04:22 +0800284
285static int pstore_compress(const void *in, void *out,
Geliang Tangcb3bee02018-03-09 18:51:07 +0800286 unsigned int inlen, unsigned int outlen)
Geliang Tang8cfc8dd2016-02-18 22:04:22 +0800287{
Geliang Tangcb3bee02018-03-09 18:51:07 +0800288 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 Tang8cfc8dd2016-02-18 22:04:22 +0800297}
298
Geliang Tang8cfc8dd2016-02-18 22:04:22 +0800299static void allocate_buf_for_compression(void)
300{
Kees Cook95047b02018-10-17 14:00:12 -0700301 struct crypto_comp *ctx;
302 int size;
303 char *buf;
304
305 /* Skip if not built-in or compression backend not selected yet. */
Tobias Regnerye698aaf2018-04-06 09:25:17 +0200306 if (!IS_ENABLED(CONFIG_PSTORE_COMPRESS) || !zbackend)
Geliang Tangcb3bee02018-03-09 18:51:07 +0800307 return;
308
Kees Cook95047b02018-10-17 14:00:12 -0700309 /* Skip if no pstore backend yet or compression init already done. */
310 if (!psinfo || tfm)
311 return;
312
Geliang Tangcb3bee02018-03-09 18:51:07 +0800313 if (!crypto_has_comp(zbackend->name, 0, 0)) {
Kees Cook95047b02018-10-17 14:00:12 -0700314 pr_err("Unknown compression: %s\n", zbackend->name);
Geliang Tangcb3bee02018-03-09 18:51:07 +0800315 return;
316 }
317
Kees Cook95047b02018-10-17 14:00:12 -0700318 size = zbackend->zbufsize(psinfo->bufsize);
319 if (size <= 0) {
320 pr_err("Invalid compression size for %s: %d\n",
321 zbackend->name, size);
Geliang Tangcb3bee02018-03-09 18:51:07 +0800322 return;
323 }
324
Kees Cook95047b02018-10-17 14:00:12 -0700325 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 Tangcb3bee02018-03-09 18:51:07 +0800329 return;
Geliang Tang8cfc8dd2016-02-18 22:04:22 +0800330 }
Kees Cook95047b02018-10-17 14:00:12 -0700331
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 Cook0eed84f2018-11-01 14:03:07 -0700345 pr_info("Using crash dump compression: %s\n", zbackend->name);
Geliang Tang8cfc8dd2016-02-18 22:04:22 +0800346}
347
348static void free_buf_for_compression(void)
349{
Kees Cook95047b02018-10-17 14:00:12 -0700350 if (IS_ENABLED(CONFIG_PSTORE_COMPRESS) && tfm)
Geliang Tangcb3bee02018-03-09 18:51:07 +0800351 crypto_free_comp(tfm);
352 kfree(big_oops_buf);
353 big_oops_buf = NULL;
354 big_oops_buf_sz = 0;
Geliang Tangee1d2672015-10-20 00:39:03 -0700355}
356
Aruna Balakrishnaiahb0aad7a2013-08-16 13:53:10 -0700357/*
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 */
364static 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 Cooke581ca82017-05-19 15:10:31 -0700383void 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 Cookc7f3c5952017-05-19 15:29:10 -0700389
390 /* Report zeroed timestamp if called before timekeeping has resumed. */
Kees Cook7aaa8222018-05-14 15:50:52 -0700391 record->time = ns_to_timespec64(ktime_get_real_fast_ns());
Kees Cooke581ca82017-05-19 15:10:31 -0700392}
393
Tony Luckca01d6d2010-12-28 14:25:21 -0800394/*
Kees Cook0eed84f2018-11-01 14:03:07 -0700395 * callback from kmsg_dump. Save as much as we can (up to kmsg_bytes) from the
396 * end of the buffer.
Tony Luckca01d6d2010-12-28 14:25:21 -0800397 */
398static void pstore_dump(struct kmsg_dumper *dumper,
Kay Sieverse2ae7152012-06-15 14:07:51 +0200399 enum kmsg_dump_reason reason)
Tony Luckca01d6d2010-12-28 14:25:21 -0800400{
Kay Sieverse2ae7152012-06-15 14:07:51 +0200401 unsigned long total = 0;
Seiji Aguchi381b8722012-03-16 15:36:59 -0700402 const char *why;
Matthew Garrettb94fdd02011-07-21 16:57:54 -0400403 unsigned int part = 1;
Kay Sieverse2ae7152012-06-15 14:07:51 +0200404 int ret;
Tony Luckca01d6d2010-12-28 14:25:21 -0800405
Seiji Aguchi381b8722012-03-16 15:36:59 -0700406 why = get_reason_str(reason);
Tony Luck9f6af272011-03-22 16:01:49 -0700407
Kees Cookea84b582018-11-30 14:36:58 -0800408 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 Pengcheng959217c82016-11-05 10:15:59 +0800413 return;
Seiji Aguchi9f244e92013-01-11 18:09:41 +0000414 }
Kees Cookea84b582018-11-30 14:36:58 -0800415 if (down_interruptible(&psinfo->buf_lock)) {
416 pr_err("could not grab semaphore?!\n");
417 return;
418 }
Namhyung Kim98e44fda2016-05-18 21:00:05 +0900419 }
Kees Cookea84b582018-11-30 14:36:58 -0800420
Tony Luckca01d6d2010-12-28 14:25:21 -0800421 oopscount++;
422 while (total < kmsg_bytes) {
Kay Sieverse2ae7152012-06-15 14:07:51 +0200423 char *dst;
Kees Cook76cc9582017-03-03 23:28:53 -0800424 size_t dst_size;
425 int header_size;
Aruna Balakrishnaiahb0aad7a2013-08-16 13:53:10 -0700426 int zipped_len = -1;
Kees Cook76cc9582017-03-03 23:28:53 -0800427 size_t dump_size;
Kees Cooke581ca82017-05-19 15:10:31 -0700428 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 Sieverse2ae7152012-06-15 14:07:51 +0200436
Kees Cookea84b582018-11-30 14:36:58 -0800437 if (big_oops_buf) {
Aruna Balakrishnaiahb0aad7a2013-08-16 13:53:10 -0700438 dst = big_oops_buf;
Kees Cook76cc9582017-03-03 23:28:53 -0800439 dst_size = big_oops_buf_sz;
Namhyung Kim235f6d12016-05-18 21:00:06 +0900440 } else {
441 dst = psinfo->buf;
Kees Cook76cc9582017-03-03 23:28:53 -0800442 dst_size = psinfo->bufsize;
Namhyung Kim235f6d12016-05-18 21:00:06 +0900443 }
Tony Luckca01d6d2010-12-28 14:25:21 -0800444
Kees Cook76cc9582017-03-03 23:28:53 -0800445 /* 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 Balakrishnaiahb0aad7a2013-08-16 13:53:10 -0700449
Kees Cook76cc9582017-03-03 23:28:53 -0800450 /* Write dump contents. */
451 if (!kmsg_dump_get_buffer(dumper, true, dst + header_size,
452 dst_size, &dump_size))
Namhyung Kim235f6d12016-05-18 21:00:06 +0900453 break;
454
Kees Cookea84b582018-11-30 14:36:58 -0800455 if (big_oops_buf) {
Aruna Balakrishnaiahb0aad7a2013-08-16 13:53:10 -0700456 zipped_len = pstore_compress(dst, psinfo->buf,
Kees Cook76cc9582017-03-03 23:28:53 -0800457 header_size + dump_size,
458 psinfo->bufsize);
Aruna Balakrishnaiahb0aad7a2013-08-16 13:53:10 -0700459
460 if (zipped_len > 0) {
Kees Cook76cc9582017-03-03 23:28:53 -0800461 record.compressed = true;
462 record.size = zipped_len;
Aruna Balakrishnaiahb0aad7a2013-08-16 13:53:10 -0700463 } else {
Kees Cook76cc9582017-03-03 23:28:53 -0800464 record.size = copy_kmsg_to_buffer(header_size,
465 dump_size);
Aruna Balakrishnaiahb0aad7a2013-08-16 13:53:10 -0700466 }
467 } else {
Kees Cook76cc9582017-03-03 23:28:53 -0800468 record.size = header_size + dump_size;
Aruna Balakrishnaiahb0aad7a2013-08-16 13:53:10 -0700469 }
Tony Luckca01d6d2010-12-28 14:25:21 -0800470
Kees Cook76cc9582017-03-03 23:28:53 -0800471 ret = psinfo->write(&record);
Chen Gongb238b8f2011-10-12 09:17:24 -0700472 if (ret == 0 && reason == KMSG_DUMP_OOPS && pstore_is_mounted())
Luck, Tony6dda9262011-08-11 15:14:39 -0700473 pstore_new_entry = 1;
Kay Sieverse2ae7152012-06-15 14:07:51 +0200474
Kees Cook76cc9582017-03-03 23:28:53 -0800475 total += record.size;
Matthew Garrett56280682011-07-21 16:57:53 -0400476 part++;
Tony Luckca01d6d2010-12-28 14:25:21 -0800477 }
Kees Cookea84b582018-11-30 14:36:58 -0800478
479 up(&psinfo->buf_lock);
Tony Luckca01d6d2010-12-28 14:25:21 -0800480}
481
482static struct kmsg_dumper pstore_dumper = {
483 .dump = pstore_dump,
484};
485
Geliang Tang306e5c22015-10-31 23:23:15 +0800486/*
487 * Register with kmsg_dump to save last part of console log on panic.
488 */
Geliang Tang18730412015-10-20 00:39:02 -0700489static void pstore_register_kmsg(void)
490{
491 kmsg_dump_register(&pstore_dumper);
492}
493
Geliang Tangee1d2672015-10-20 00:39:03 -0700494static void pstore_unregister_kmsg(void)
495{
496 kmsg_dump_unregister(&pstore_dumper);
497}
498
Anton Vorontsovf29e5952012-05-26 06:20:19 -0700499#ifdef CONFIG_PSTORE_CONSOLE
500static void pstore_console_write(struct console *con, const char *s, unsigned c)
501{
Kees Cookb77fa612018-11-01 14:08:07 -0700502 struct pstore_record record;
Anton Vorontsovf29e5952012-05-26 06:20:19 -0700503
Yue Hu4c6c4d32019-01-31 18:12:46 +0800504 if (!c)
505 return;
506
Kees Cookb77fa612018-11-01 14:08:07 -0700507 pstore_record_init(&record, psinfo);
508 record.type = PSTORE_TYPE_CONSOLE;
Anton Vorontsovf29e5952012-05-26 06:20:19 -0700509
Kees Cookb77fa612018-11-01 14:08:07 -0700510 record.buf = (char *)s;
511 record.size = c;
512 psinfo->write(&record);
Anton Vorontsovf29e5952012-05-26 06:20:19 -0700513}
514
515static 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
522static void pstore_register_console(void)
523{
524 register_console(&pstore_console);
525}
Geliang Tangee1d2672015-10-20 00:39:03 -0700526
527static void pstore_unregister_console(void)
528{
529 unregister_console(&pstore_console);
530}
Anton Vorontsovf29e5952012-05-26 06:20:19 -0700531#else
532static void pstore_register_console(void) {}
Geliang Tangee1d2672015-10-20 00:39:03 -0700533static void pstore_unregister_console(void) {}
Anton Vorontsovf29e5952012-05-26 06:20:19 -0700534#endif
535
Kees Cook4c9ec212017-03-05 22:41:10 -0800536static int pstore_write_user_compat(struct pstore_record *record,
537 const char __user *buf)
Mark Salyzyn5bf6d1b2016-09-01 08:13:46 -0700538{
Kees Cook30800d92017-03-07 13:57:11 -0800539 int ret = 0;
Mark Salyzyn5bf6d1b2016-09-01 08:13:46 -0700540
Kees Cook30800d92017-03-07 13:57:11 -0800541 if (record->buf)
542 return -EINVAL;
Mark Salyzyn5bf6d1b2016-09-01 08:13:46 -0700543
Geliang Tang077090a2017-04-29 09:45:16 +0800544 record->buf = memdup_user(buf, record->size);
Hirofumi Nakagawadfd6fa32017-09-26 03:21:27 +0900545 if (IS_ERR(record->buf)) {
Geliang Tang077090a2017-04-29 09:45:16 +0800546 ret = PTR_ERR(record->buf);
Kees Cook30800d92017-03-07 13:57:11 -0800547 goto out;
Mark Salyzyn5bf6d1b2016-09-01 08:13:46 -0700548 }
Kees Cook30800d92017-03-07 13:57:11 -0800549
550 ret = record->psi->write(record);
551
Kees Cook30800d92017-03-07 13:57:11 -0800552 kfree(record->buf);
Geliang Tang077090a2017-04-29 09:45:16 +0800553out:
Kees Cook30800d92017-03-07 13:57:11 -0800554 record->buf = NULL;
555
556 return unlikely(ret < 0) ? ret : record->size;
Mark Salyzyn5bf6d1b2016-09-01 08:13:46 -0700557}
558
Tony Luckca01d6d2010-12-28 14:25:21 -0800559/*
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 Luckca01d6d2010-12-28 14:25:21 -0800565 */
566int pstore_register(struct pstore_info *psi)
567{
568 struct module *owner = psi->owner;
569
Kees Cook0d7cd092017-03-03 12:11:40 -0800570 if (backend && strcmp(backend, psi->name)) {
571 pr_warn("ignoring unexpected backend '%s'\n", psi->name);
Lenny Szubowicz8e48b1a2013-06-28 17:11:33 -0400572 return -EPERM;
Kees Cook0d7cd092017-03-03 12:11:40 -0800573 }
Lenny Szubowicz8e48b1a2013-06-28 17:11:33 -0400574
Kees Cook4c9ec212017-03-05 22:41:10 -0800575 /* 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 Luckca01d6d2010-12-28 14:25:21 -0800589 spin_lock(&pstore_lock);
590 if (psinfo) {
Kees Cook0d7cd092017-03-03 12:11:40 -0800591 pr_warn("backend '%s' already loaded: ignoring '%s'\n",
592 psinfo->name, psi->name);
Tony Luckca01d6d2010-12-28 14:25:21 -0800593 spin_unlock(&pstore_lock);
594 return -EBUSY;
595 }
Matthew Garrettdee28e72011-07-21 16:57:55 -0400596
Kees Cook4c9ec212017-03-05 22:41:10 -0800597 if (!psi->write_user)
598 psi->write_user = pstore_write_user_compat;
Tony Luckca01d6d2010-12-28 14:25:21 -0800599 psinfo = psi;
Kees Cookf6f82852011-11-17 12:58:07 -0800600 mutex_init(&psinfo->read_mutex);
Kees Cookea84b582018-11-30 14:36:58 -0800601 sema_init(&psinfo->buf_lock, 1);
Tony Luckca01d6d2010-12-28 14:25:21 -0800602 spin_unlock(&pstore_lock);
603
604 if (owner && !try_module_get(owner)) {
605 psinfo = NULL;
606 return -EINVAL;
607 }
608
Aruna Balakrishnaiahb0aad7a2013-08-16 13:53:10 -0700609 allocate_buf_for_compression();
610
Tony Luckca01d6d2010-12-28 14:25:21 -0800611 if (pstore_is_mounted())
Luck, Tony6dda9262011-08-11 15:14:39 -0700612 pstore_get_records(0);
Tony Luckca01d6d2010-12-28 14:25:21 -0800613
Namhyung Kimc950fd62016-07-28 00:08:25 +0900614 if (psi->flags & PSTORE_FLAGS_DMESG)
615 pstore_register_kmsg();
616 if (psi->flags & PSTORE_FLAGS_CONSOLE)
Luck, Tonydf36ac12013-12-18 15:17:10 -0800617 pstore_register_console();
Namhyung Kimc950fd62016-07-28 00:08:25 +0900618 if (psi->flags & PSTORE_FLAGS_FTRACE)
Luck, Tonydf36ac12013-12-18 15:17:10 -0800619 pstore_register_ftrace();
Namhyung Kimc950fd62016-07-28 00:08:25 +0900620 if (psi->flags & PSTORE_FLAGS_PMSG)
Mark Salyzyn9d5438f2015-01-16 16:01:10 -0800621 pstore_register_pmsg();
Tony Luckca01d6d2010-12-28 14:25:21 -0800622
Kees Cook6330d552017-03-06 12:42:12 -0800623 /* Start watching for new records, if desired. */
Anton Vorontsova3f5f072012-05-26 06:20:28 -0700624 if (pstore_update_ms >= 0) {
625 pstore_timer.expires = jiffies +
626 msecs_to_jiffies(pstore_update_ms);
627 add_timer(&pstore_timer);
628 }
Luck, Tony6dda9262011-08-11 15:14:39 -0700629
Wang Long42222c22015-05-21 09:34:22 -0700630 /*
631 * Update the module parameter backend, so it is visible
632 * through /sys/module/pstore/parameters/backend
633 */
634 backend = psi->name;
635
Fabian Frederickef748852014-06-06 14:37:31 -0700636 pr_info("Registered %s as persistent store backend\n", psi->name);
Lenny Szubowicz8e48b1a2013-06-28 17:11:33 -0400637
Kees Cook1344dd82017-03-03 17:45:38 -0800638 module_put(owner);
639
Tony Luckca01d6d2010-12-28 14:25:21 -0800640 return 0;
641}
642EXPORT_SYMBOL_GPL(pstore_register);
643
Geliang Tangee1d2672015-10-20 00:39:03 -0700644void pstore_unregister(struct pstore_info *psi)
645{
Kees Cook6330d552017-03-06 12:42:12 -0800646 /* 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 Kimc950fd62016-07-28 00:08:25 +0900651 if (psi->flags & PSTORE_FLAGS_PMSG)
Kees Cooka1db8062016-05-19 10:59:03 -0400652 pstore_unregister_pmsg();
Namhyung Kimc950fd62016-07-28 00:08:25 +0900653 if (psi->flags & PSTORE_FLAGS_FTRACE)
Kees Cooka1db8062016-05-19 10:59:03 -0400654 pstore_unregister_ftrace();
Namhyung Kimc950fd62016-07-28 00:08:25 +0900655 if (psi->flags & PSTORE_FLAGS_CONSOLE)
Kees Cooka1db8062016-05-19 10:59:03 -0400656 pstore_unregister_console();
Namhyung Kimc950fd62016-07-28 00:08:25 +0900657 if (psi->flags & PSTORE_FLAGS_DMESG)
658 pstore_unregister_kmsg();
Geliang Tangee1d2672015-10-20 00:39:03 -0700659
660 free_buf_for_compression();
661
662 psinfo = NULL;
663 backend = NULL;
664}
665EXPORT_SYMBOL_GPL(pstore_unregister);
666
Kees Cook634f8f52017-03-03 17:35:25 -0800667static void decompress_record(struct pstore_record *record)
668{
Kees Cookbdabc8e2018-10-26 01:17:07 -0700669 int ret;
Kees Cook634f8f52017-03-03 17:35:25 -0800670 int unzipped_len;
Kees Cookbdabc8e2018-10-26 01:17:07 -0700671 char *unzipped, *workspace;
Kees Cook634f8f52017-03-03 17:35:25 -0800672
Ankit Kumar4a16d1c2017-05-23 11:16:52 +0530673 if (!record->compressed)
674 return;
675
Kees Cook634f8f52017-03-03 17:35:25 -0800676 /* Only PSTORE_TYPE_DMESG support compression. */
Ankit Kumar4a16d1c2017-05-23 11:16:52 +0530677 if (record->type != PSTORE_TYPE_DMESG) {
Kees Cook634f8f52017-03-03 17:35:25 -0800678 pr_warn("ignored compressed record type %d\n", record->type);
679 return;
680 }
681
Kees Cookbdabc8e2018-10-26 01:17:07 -0700682 /* Missing compression buffer means compression was not initialized. */
Kees Cook634f8f52017-03-03 17:35:25 -0800683 if (!big_oops_buf) {
Kees Cookbdabc8e2018-10-26 01:17:07 -0700684 pr_warn("no decompression method initialized!\n");
Kees Cook634f8f52017-03-03 17:35:25 -0800685 return;
686 }
687
Kees Cookbdabc8e2018-10-26 01:17:07 -0700688 /* 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 Cook7e8cc8d2017-03-04 22:28:46 -0800693 return;
Kees Cook7e8cc8d2017-03-04 22:28:46 -0800694
Kees Cookbdabc8e2018-10-26 01:17:07 -0700695 /* 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 Cook7e8cc8d2017-03-04 22:28:46 -0800701 return;
702 }
Kees Cook7e8cc8d2017-03-04 22:28:46 -0800703
704 /* Append ECC notice to decompressed buffer. */
Kees Cookbdabc8e2018-10-26 01:17:07 -0700705 memcpy(workspace + unzipped_len, record->buf + record->size,
Kees Cook7e8cc8d2017-03-04 22:28:46 -0800706 record->ecc_notice_size);
707
Kees Cookbdabc8e2018-10-26 01:17:07 -0700708 /* 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 Cook7e8cc8d2017-03-04 22:28:46 -0800716 kfree(record->buf);
Kees Cookbdabc8e2018-10-26 01:17:07 -0700717 record->buf = unzipped;
Kees Cook7e8cc8d2017-03-04 22:28:46 -0800718 record->size = unzipped_len;
719 record->compressed = false;
Kees Cook634f8f52017-03-03 17:35:25 -0800720}
721
Tony Luckca01d6d2010-12-28 14:25:21 -0800722/*
Kees Cook3a7d2fd2017-04-27 15:53:21 -0700723 * Read all the records from one persistent store backend. Create
Luck, Tony6dda9262011-08-11 15:14:39 -0700724 * 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 Luckca01d6d2010-12-28 14:25:21 -0800727 */
Kees Cook3a7d2fd2017-04-27 15:53:21 -0700728void pstore_get_backend_records(struct pstore_info *psi,
729 struct dentry *root, int quiet)
Tony Luckca01d6d2010-12-28 14:25:21 -0800730{
Kees Cook2a2b0ac2017-03-04 22:57:26 -0800731 int failed = 0;
Kees Cook656de422017-05-16 12:03:31 -0700732 unsigned int stop_loop = 65536;
Tony Luckca01d6d2010-12-28 14:25:21 -0800733
Kees Cook3a7d2fd2017-04-27 15:53:21 -0700734 if (!psi || !root)
Tony Luckca01d6d2010-12-28 14:25:21 -0800735 return;
736
Kees Cookf6f82852011-11-17 12:58:07 -0800737 mutex_lock(&psi->read_mutex);
Kees Cook2174f6d2011-11-18 13:49:00 -0800738 if (psi->open && psi->open(psi))
Chen Gong06cf91b2011-05-16 11:00:27 -0700739 goto out;
740
Kees Cook1dfff7d2017-03-04 22:46:41 -0800741 /*
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 Cook656de422017-05-16 12:03:31 -0700746 for (; stop_loop; stop_loop--) {
Kees Cook2a2b0ac2017-03-04 22:57:26 -0800747 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 Cooke581ca82017-05-19 15:10:31 -0700755 pstore_record_init(record, psi);
Kees Cook2a2b0ac2017-03-04 22:57:26 -0800756
757 record->size = psi->read(record);
758
759 /* No more records left in backend? */
Douglas Andersonf6525b92017-05-30 15:50:38 -0700760 if (record->size <= 0) {
761 kfree(record);
Kees Cook2a2b0ac2017-03-04 22:57:26 -0800762 break;
Douglas Andersonf6525b92017-05-30 15:50:38 -0700763 }
Kees Cook2a2b0ac2017-03-04 22:57:26 -0800764
765 decompress_record(record);
Kees Cook3a7d2fd2017-04-27 15:53:21 -0700766 rc = pstore_mkfile(root, record);
Kees Cook1dfff7d2017-03-04 22:46:41 -0800767 if (rc) {
Kees Cook83f70f02017-03-04 23:12:24 -0800768 /* pstore_mkfile() did not take record, so free it. */
Kees Cook2a2b0ac2017-03-04 22:57:26 -0800769 kfree(record->buf);
Kees Cook83f70f02017-03-04 23:12:24 -0800770 kfree(record);
Kees Cook1dfff7d2017-03-04 22:46:41 -0800771 if (rc != -EEXIST || !quiet)
772 failed++;
773 }
Tony Luckca01d6d2010-12-28 14:25:21 -0800774 }
Kees Cook2174f6d2011-11-18 13:49:00 -0800775 if (psi->close)
776 psi->close(psi);
Chen Gong06cf91b2011-05-16 11:00:27 -0700777out:
Kees Cookf6f82852011-11-17 12:58:07 -0800778 mutex_unlock(&psi->read_mutex);
Tony Luckca01d6d2010-12-28 14:25:21 -0800779
780 if (failed)
Kees Cook656de422017-05-16 12:03:31 -0700781 pr_warn("failed to create %d record(s) from '%s'\n",
Fabian Frederickef748852014-06-06 14:37:31 -0700782 failed, psi->name);
Kees Cook656de422017-05-16 12:03:31 -0700783 if (!stop_loop)
784 pr_err("looping? Too many records seen from '%s'\n",
785 psi->name);
Tony Luckca01d6d2010-12-28 14:25:21 -0800786}
787
Luck, Tony6dda9262011-08-11 15:14:39 -0700788static void pstore_dowork(struct work_struct *work)
789{
790 pstore_get_records(1);
791}
792
Kees Cook24ed9602017-08-28 11:28:21 -0700793static void pstore_timefunc(struct timer_list *unused)
Luck, Tony6dda9262011-08-11 15:14:39 -0700794{
795 if (pstore_new_entry) {
796 pstore_new_entry = 0;
797 schedule_work(&pstore_work);
798 }
799
Kees Cook6330d552017-03-06 12:42:12 -0800800 if (pstore_update_ms >= 0)
801 mod_timer(&pstore_timer,
802 jiffies + msecs_to_jiffies(pstore_update_ms));
Luck, Tony6dda9262011-08-11 15:14:39 -0700803}
804
Kees Cookfe1d4752018-03-06 15:57:38 -0800805void __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 Cookfe1d4752018-03-06 15:57:38 -0800815 return;
816 }
817 }
818}
819
Kees Cookcb095af2018-10-18 11:17:42 -0700820static int __init pstore_init(void)
821{
822 int ret;
823
824 pstore_choose_compression();
825
Joel Fernandes (Google)41603162018-10-17 03:13:55 -0700826 /*
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 Cook95047b02018-10-17 14:00:12 -0700831 allocate_buf_for_compression();
Joel Fernandes (Google)41603162018-10-17 03:13:55 -0700832
Kees Cookcb095af2018-10-18 11:17:42 -0700833 ret = pstore_init_fs();
834 if (ret)
835 return ret;
836
837 return 0;
838}
Joel Fernandes (Google)41603162018-10-17 03:13:55 -0700839late_initcall(pstore_init);
Kees Cookcb095af2018-10-18 11:17:42 -0700840
841static void __exit pstore_exit(void)
842{
843 pstore_exit_fs();
844}
845module_exit(pstore_exit)
846
Kees Cookfe1d4752018-03-06 15:57:38 -0800847module_param(compress, charp, 0444);
848MODULE_PARM_DESC(compress, "Pstore compression to use");
849
Matthew Garrettdee28e72011-07-21 16:57:55 -0400850module_param(backend, charp, 0444);
851MODULE_PARM_DESC(backend, "Pstore backend to use");
Kees Cookcb095af2018-10-18 11:17:42 -0700852
853MODULE_AUTHOR("Tony Luck <tony.luck@intel.com>");
854MODULE_LICENSE("GPL");