blob: 2387cb74f72998c239e73d293f3506be863602e5 [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
Seiji Aguchi9f244e92013-01-11 18:09:41 +0000164bool pstore_cannot_block_path(enum kmsg_dump_reason reason)
165{
166 /*
167 * In case of NMI path, pstore shouldn't be blocked
168 * regardless of reason.
169 */
170 if (in_nmi())
171 return true;
172
173 switch (reason) {
174 /* In panic case, other cpus are stopped by smp_send_stop(). */
175 case KMSG_DUMP_PANIC:
176 /* Emergency restart shouldn't be blocked by spin lock. */
177 case KMSG_DUMP_EMERG:
178 return true;
179 default:
180 return false;
181 }
182}
183EXPORT_SYMBOL_GPL(pstore_cannot_block_path);
184
Arnd Bergmann58eb5b672018-03-15 16:34:08 +0100185#if IS_ENABLED(CONFIG_PSTORE_DEFLATE_COMPRESS)
Geliang Tangcb3bee02018-03-09 18:51:07 +0800186static int zbufsize_deflate(size_t size)
Aruna Balakrishnaiahb0aad7a2013-08-16 13:53:10 -0700187{
Aruna Balakrishnaiah7de8fe22013-09-11 10:57:41 -0700188 size_t cmpr;
Aruna Balakrishnaiahb0aad7a2013-08-16 13:53:10 -0700189
Geliang Tangcb3bee02018-03-09 18:51:07 +0800190 switch (size) {
Aruna Balakrishnaiah7de8fe22013-09-11 10:57:41 -0700191 /* buffer range for efivars */
192 case 1000 ... 2000:
193 cmpr = 56;
194 break;
195 case 2001 ... 3000:
196 cmpr = 54;
197 break;
198 case 3001 ... 3999:
199 cmpr = 52;
200 break;
201 /* buffer range for nvram, erst */
202 case 4000 ... 10000:
203 cmpr = 45;
204 break;
205 default:
206 cmpr = 60;
207 break;
208 }
209
Geliang Tangcb3bee02018-03-09 18:51:07 +0800210 return (size * 100) / cmpr;
Geliang Tang8cfc8dd2016-02-18 22:04:22 +0800211}
Geliang Tang8cfc8dd2016-02-18 22:04:22 +0800212#endif
213
Arnd Bergmann58eb5b672018-03-15 16:34:08 +0100214#if IS_ENABLED(CONFIG_PSTORE_LZO_COMPRESS)
Geliang Tangcb3bee02018-03-09 18:51:07 +0800215static int zbufsize_lzo(size_t size)
Geliang Tang8cfc8dd2016-02-18 22:04:22 +0800216{
Geliang Tangcb3bee02018-03-09 18:51:07 +0800217 return lzo1x_worst_compress(size);
Geliang Tang8cfc8dd2016-02-18 22:04:22 +0800218}
Geliang Tang8cfc8dd2016-02-18 22:04:22 +0800219#endif
220
Arnd Bergmann58eb5b672018-03-15 16:34:08 +0100221#if IS_ENABLED(CONFIG_PSTORE_LZ4_COMPRESS) || IS_ENABLED(CONFIG_PSTORE_LZ4HC_COMPRESS)
Geliang Tangcb3bee02018-03-09 18:51:07 +0800222static int zbufsize_lz4(size_t size)
Geliang Tang8cfc8dd2016-02-18 22:04:22 +0800223{
Geliang Tangcb3bee02018-03-09 18:51:07 +0800224 return LZ4_compressBound(size);
Geliang Tang239b7162018-02-13 14:40:39 +0800225}
Geliang Tang239b7162018-02-13 14:40:39 +0800226#endif
227
Arnd Bergmann58eb5b672018-03-15 16:34:08 +0100228#if IS_ENABLED(CONFIG_PSTORE_842_COMPRESS)
Geliang Tangcb3bee02018-03-09 18:51:07 +0800229static int zbufsize_842(size_t size)
Geliang Tang239b7162018-02-13 14:40:39 +0800230{
Kees Cook55597402018-03-06 15:15:24 -0800231 return size;
Geliang Tang239b7162018-02-13 14:40:39 +0800232}
Kees Cookfe1d4752018-03-06 15:57:38 -0800233#endif
Geliang Tang8cfc8dd2016-02-18 22:04:22 +0800234
Geliang Tang1021bcf2018-08-01 19:23:37 +0800235#if IS_ENABLED(CONFIG_PSTORE_ZSTD_COMPRESS)
236static int zbufsize_zstd(size_t size)
237{
238 return ZSTD_compressBound(size);
239}
240#endif
241
Kees Cookfe1d4752018-03-06 15:57:38 -0800242static const struct pstore_zbackend *zbackend __ro_after_init;
243
244static const struct pstore_zbackend zbackends[] = {
Arnd Bergmann58eb5b672018-03-15 16:34:08 +0100245#if IS_ENABLED(CONFIG_PSTORE_DEFLATE_COMPRESS)
Kees Cookfe1d4752018-03-06 15:57:38 -0800246 {
Geliang Tangcb3bee02018-03-09 18:51:07 +0800247 .zbufsize = zbufsize_deflate,
248 .name = "deflate",
Kees Cookfe1d4752018-03-06 15:57:38 -0800249 },
250#endif
Arnd Bergmann58eb5b672018-03-15 16:34:08 +0100251#if IS_ENABLED(CONFIG_PSTORE_LZO_COMPRESS)
Kees Cookfe1d4752018-03-06 15:57:38 -0800252 {
Geliang Tangcb3bee02018-03-09 18:51:07 +0800253 .zbufsize = zbufsize_lzo,
Kees Cookfe1d4752018-03-06 15:57:38 -0800254 .name = "lzo",
255 },
256#endif
Arnd Bergmann58eb5b672018-03-15 16:34:08 +0100257#if IS_ENABLED(CONFIG_PSTORE_LZ4_COMPRESS)
Kees Cookfe1d4752018-03-06 15:57:38 -0800258 {
Geliang Tangcb3bee02018-03-09 18:51:07 +0800259 .zbufsize = zbufsize_lz4,
Kees Cookfe1d4752018-03-06 15:57:38 -0800260 .name = "lz4",
261 },
262#endif
Arnd Bergmann58eb5b672018-03-15 16:34:08 +0100263#if IS_ENABLED(CONFIG_PSTORE_LZ4HC_COMPRESS)
Kees Cookfe1d4752018-03-06 15:57:38 -0800264 {
Geliang Tangcb3bee02018-03-09 18:51:07 +0800265 .zbufsize = zbufsize_lz4,
Kees Cookfe1d4752018-03-06 15:57:38 -0800266 .name = "lz4hc",
267 },
268#endif
Arnd Bergmann58eb5b672018-03-15 16:34:08 +0100269#if IS_ENABLED(CONFIG_PSTORE_842_COMPRESS)
Kees Cookfe1d4752018-03-06 15:57:38 -0800270 {
Geliang Tangcb3bee02018-03-09 18:51:07 +0800271 .zbufsize = zbufsize_842,
Kees Cookfe1d4752018-03-06 15:57:38 -0800272 .name = "842",
273 },
274#endif
Geliang Tang1021bcf2018-08-01 19:23:37 +0800275#if IS_ENABLED(CONFIG_PSTORE_ZSTD_COMPRESS)
276 {
277 .zbufsize = zbufsize_zstd,
278 .name = "zstd",
279 },
280#endif
Kees Cookfe1d4752018-03-06 15:57:38 -0800281 { }
Geliang Tang8cfc8dd2016-02-18 22:04:22 +0800282};
Geliang Tang8cfc8dd2016-02-18 22:04:22 +0800283
284static int pstore_compress(const void *in, void *out,
Geliang Tangcb3bee02018-03-09 18:51:07 +0800285 unsigned int inlen, unsigned int outlen)
Geliang Tang8cfc8dd2016-02-18 22:04:22 +0800286{
Geliang Tangcb3bee02018-03-09 18:51:07 +0800287 int ret;
288
289 ret = crypto_comp_compress(tfm, in, inlen, out, &outlen);
290 if (ret) {
291 pr_err("crypto_comp_compress failed, ret = %d!\n", ret);
292 return ret;
293 }
294
295 return outlen;
Geliang Tang8cfc8dd2016-02-18 22:04:22 +0800296}
297
Geliang Tang8cfc8dd2016-02-18 22:04:22 +0800298static void allocate_buf_for_compression(void)
299{
Kees Cook95047b02018-10-17 14:00:12 -0700300 struct crypto_comp *ctx;
301 int size;
302 char *buf;
303
304 /* Skip if not built-in or compression backend not selected yet. */
Tobias Regnerye698aaf2018-04-06 09:25:17 +0200305 if (!IS_ENABLED(CONFIG_PSTORE_COMPRESS) || !zbackend)
Geliang Tangcb3bee02018-03-09 18:51:07 +0800306 return;
307
Kees Cook95047b02018-10-17 14:00:12 -0700308 /* Skip if no pstore backend yet or compression init already done. */
309 if (!psinfo || tfm)
310 return;
311
Geliang Tangcb3bee02018-03-09 18:51:07 +0800312 if (!crypto_has_comp(zbackend->name, 0, 0)) {
Kees Cook95047b02018-10-17 14:00:12 -0700313 pr_err("Unknown compression: %s\n", zbackend->name);
Geliang Tangcb3bee02018-03-09 18:51:07 +0800314 return;
315 }
316
Kees Cook95047b02018-10-17 14:00:12 -0700317 size = zbackend->zbufsize(psinfo->bufsize);
318 if (size <= 0) {
319 pr_err("Invalid compression size for %s: %d\n",
320 zbackend->name, size);
Geliang Tangcb3bee02018-03-09 18:51:07 +0800321 return;
322 }
323
Kees Cook95047b02018-10-17 14:00:12 -0700324 buf = kmalloc(size, GFP_KERNEL);
325 if (!buf) {
326 pr_err("Failed %d byte compression buffer allocation for: %s\n",
327 size, zbackend->name);
Geliang Tangcb3bee02018-03-09 18:51:07 +0800328 return;
Geliang Tang8cfc8dd2016-02-18 22:04:22 +0800329 }
Kees Cook95047b02018-10-17 14:00:12 -0700330
331 ctx = crypto_alloc_comp(zbackend->name, 0, 0);
332 if (IS_ERR_OR_NULL(ctx)) {
333 kfree(buf);
334 pr_err("crypto_alloc_comp('%s') failed: %ld\n", zbackend->name,
335 PTR_ERR(ctx));
336 return;
337 }
338
339 /* A non-NULL big_oops_buf indicates compression is available. */
340 tfm = ctx;
341 big_oops_buf_sz = size;
342 big_oops_buf = buf;
343
Kees Cook0eed84f2018-11-01 14:03:07 -0700344 pr_info("Using crash dump compression: %s\n", zbackend->name);
Geliang Tang8cfc8dd2016-02-18 22:04:22 +0800345}
346
347static void free_buf_for_compression(void)
348{
Kees Cook95047b02018-10-17 14:00:12 -0700349 if (IS_ENABLED(CONFIG_PSTORE_COMPRESS) && tfm)
Geliang Tangcb3bee02018-03-09 18:51:07 +0800350 crypto_free_comp(tfm);
351 kfree(big_oops_buf);
352 big_oops_buf = NULL;
353 big_oops_buf_sz = 0;
Geliang Tangee1d2672015-10-20 00:39:03 -0700354}
355
Aruna Balakrishnaiahb0aad7a2013-08-16 13:53:10 -0700356/*
357 * Called when compression fails, since the printk buffer
358 * would be fetched for compression calling it again when
359 * compression fails would have moved the iterator of
360 * printk buffer which results in fetching old contents.
361 * Copy the recent messages from big_oops_buf to psinfo->buf
362 */
363static size_t copy_kmsg_to_buffer(int hsize, size_t len)
364{
365 size_t total_len;
366 size_t diff;
367
368 total_len = hsize + len;
369
370 if (total_len > psinfo->bufsize) {
371 diff = total_len - psinfo->bufsize + hsize;
372 memcpy(psinfo->buf, big_oops_buf, hsize);
373 memcpy(psinfo->buf + hsize, big_oops_buf + diff,
374 psinfo->bufsize - hsize);
375 total_len = psinfo->bufsize;
376 } else
377 memcpy(psinfo->buf, big_oops_buf, total_len);
378
379 return total_len;
380}
381
Kees Cooke581ca82017-05-19 15:10:31 -0700382void pstore_record_init(struct pstore_record *record,
383 struct pstore_info *psinfo)
384{
385 memset(record, 0, sizeof(*record));
386
387 record->psi = psinfo;
Kees Cookc7f3c5952017-05-19 15:29:10 -0700388
389 /* Report zeroed timestamp if called before timekeeping has resumed. */
Kees Cook7aaa8222018-05-14 15:50:52 -0700390 record->time = ns_to_timespec64(ktime_get_real_fast_ns());
Kees Cooke581ca82017-05-19 15:10:31 -0700391}
392
Tony Luckca01d6d2010-12-28 14:25:21 -0800393/*
Kees Cook0eed84f2018-11-01 14:03:07 -0700394 * callback from kmsg_dump. Save as much as we can (up to kmsg_bytes) from the
395 * end of the buffer.
Tony Luckca01d6d2010-12-28 14:25:21 -0800396 */
397static void pstore_dump(struct kmsg_dumper *dumper,
Kay Sieverse2ae7152012-06-15 14:07:51 +0200398 enum kmsg_dump_reason reason)
Tony Luckca01d6d2010-12-28 14:25:21 -0800399{
Kay Sieverse2ae7152012-06-15 14:07:51 +0200400 unsigned long total = 0;
Seiji Aguchi381b8722012-03-16 15:36:59 -0700401 const char *why;
Matthew Garrettb94fdd02011-07-21 16:57:54 -0400402 unsigned int part = 1;
Don Zickusabd4d552011-08-12 10:54:51 -0700403 unsigned long flags = 0;
Namhyung Kim98e44fda2016-05-18 21:00:05 +0900404 int is_locked;
Kay Sieverse2ae7152012-06-15 14:07:51 +0200405 int ret;
Tony Luckca01d6d2010-12-28 14:25:21 -0800406
Seiji Aguchi381b8722012-03-16 15:36:59 -0700407 why = get_reason_str(reason);
Tony Luck9f6af272011-03-22 16:01:49 -0700408
Seiji Aguchi9f244e92013-01-11 18:09:41 +0000409 if (pstore_cannot_block_path(reason)) {
410 is_locked = spin_trylock_irqsave(&psinfo->buf_lock, flags);
411 if (!is_locked) {
412 pr_err("pstore dump routine blocked in %s path, may corrupt error record\n"
413 , in_nmi() ? "NMI" : why);
Li Pengcheng959217c2016-11-05 10:15:59 +0800414 return;
Seiji Aguchi9f244e92013-01-11 18:09:41 +0000415 }
Namhyung Kim98e44fda2016-05-18 21:00:05 +0900416 } else {
Don Zickusabd4d552011-08-12 10:54:51 -0700417 spin_lock_irqsave(&psinfo->buf_lock, flags);
Namhyung Kim98e44fda2016-05-18 21:00:05 +0900418 is_locked = 1;
419 }
Tony Luckca01d6d2010-12-28 14:25:21 -0800420 oopscount++;
421 while (total < kmsg_bytes) {
Kay Sieverse2ae7152012-06-15 14:07:51 +0200422 char *dst;
Kees Cook76cc9582017-03-03 23:28:53 -0800423 size_t dst_size;
424 int header_size;
Aruna Balakrishnaiahb0aad7a2013-08-16 13:53:10 -0700425 int zipped_len = -1;
Kees Cook76cc9582017-03-03 23:28:53 -0800426 size_t dump_size;
Kees Cooke581ca82017-05-19 15:10:31 -0700427 struct pstore_record record;
428
429 pstore_record_init(&record, psinfo);
430 record.type = PSTORE_TYPE_DMESG;
431 record.count = oopscount;
432 record.reason = reason;
433 record.part = part;
434 record.buf = psinfo->buf;
Kay Sieverse2ae7152012-06-15 14:07:51 +0200435
Konstantin Khlebnikovf0e2efc2015-05-21 09:26:19 -0700436 if (big_oops_buf && is_locked) {
Aruna Balakrishnaiahb0aad7a2013-08-16 13:53:10 -0700437 dst = big_oops_buf;
Kees Cook76cc9582017-03-03 23:28:53 -0800438 dst_size = big_oops_buf_sz;
Namhyung Kim235f6d12016-05-18 21:00:06 +0900439 } else {
440 dst = psinfo->buf;
Kees Cook76cc9582017-03-03 23:28:53 -0800441 dst_size = psinfo->bufsize;
Namhyung Kim235f6d12016-05-18 21:00:06 +0900442 }
Tony Luckca01d6d2010-12-28 14:25:21 -0800443
Kees Cook76cc9582017-03-03 23:28:53 -0800444 /* Write dump header. */
445 header_size = snprintf(dst, dst_size, "%s#%d Part%u\n", why,
446 oopscount, part);
447 dst_size -= header_size;
Aruna Balakrishnaiahb0aad7a2013-08-16 13:53:10 -0700448
Kees Cook76cc9582017-03-03 23:28:53 -0800449 /* Write dump contents. */
450 if (!kmsg_dump_get_buffer(dumper, true, dst + header_size,
451 dst_size, &dump_size))
Namhyung Kim235f6d12016-05-18 21:00:06 +0900452 break;
453
454 if (big_oops_buf && is_locked) {
Aruna Balakrishnaiahb0aad7a2013-08-16 13:53:10 -0700455 zipped_len = pstore_compress(dst, psinfo->buf,
Kees Cook76cc9582017-03-03 23:28:53 -0800456 header_size + dump_size,
457 psinfo->bufsize);
Aruna Balakrishnaiahb0aad7a2013-08-16 13:53:10 -0700458
459 if (zipped_len > 0) {
Kees Cook76cc9582017-03-03 23:28:53 -0800460 record.compressed = true;
461 record.size = zipped_len;
Aruna Balakrishnaiahb0aad7a2013-08-16 13:53:10 -0700462 } else {
Kees Cook76cc9582017-03-03 23:28:53 -0800463 record.size = copy_kmsg_to_buffer(header_size,
464 dump_size);
Aruna Balakrishnaiahb0aad7a2013-08-16 13:53:10 -0700465 }
466 } else {
Kees Cook76cc9582017-03-03 23:28:53 -0800467 record.size = header_size + dump_size;
Aruna Balakrishnaiahb0aad7a2013-08-16 13:53:10 -0700468 }
Tony Luckca01d6d2010-12-28 14:25:21 -0800469
Kees Cook76cc9582017-03-03 23:28:53 -0800470 ret = psinfo->write(&record);
Chen Gongb238b8f2011-10-12 09:17:24 -0700471 if (ret == 0 && reason == KMSG_DUMP_OOPS && pstore_is_mounted())
Luck, Tony6dda9262011-08-11 15:14:39 -0700472 pstore_new_entry = 1;
Kay Sieverse2ae7152012-06-15 14:07:51 +0200473
Kees Cook76cc9582017-03-03 23:28:53 -0800474 total += record.size;
Matthew Garrett56280682011-07-21 16:57:53 -0400475 part++;
Tony Luckca01d6d2010-12-28 14:25:21 -0800476 }
Namhyung Kim98e44fda2016-05-18 21:00:05 +0900477 if (is_locked)
Don Zickusabd4d552011-08-12 10:54:51 -0700478 spin_unlock_irqrestore(&psinfo->buf_lock, flags);
Tony Luckca01d6d2010-12-28 14:25:21 -0800479}
480
481static struct kmsg_dumper pstore_dumper = {
482 .dump = pstore_dump,
483};
484
Geliang Tang306e5c22015-10-31 23:23:15 +0800485/*
486 * Register with kmsg_dump to save last part of console log on panic.
487 */
Geliang Tang18730412015-10-20 00:39:02 -0700488static void pstore_register_kmsg(void)
489{
490 kmsg_dump_register(&pstore_dumper);
491}
492
Geliang Tangee1d2672015-10-20 00:39:03 -0700493static void pstore_unregister_kmsg(void)
494{
495 kmsg_dump_unregister(&pstore_dumper);
496}
497
Anton Vorontsovf29e5952012-05-26 06:20:19 -0700498#ifdef CONFIG_PSTORE_CONSOLE
499static void pstore_console_write(struct console *con, const char *s, unsigned c)
500{
Kees Cookb77fa612018-11-01 14:08:07 -0700501 struct pstore_record record;
Anton Vorontsovf29e5952012-05-26 06:20:19 -0700502
Kees Cookb77fa612018-11-01 14:08:07 -0700503 pstore_record_init(&record, psinfo);
504 record.type = PSTORE_TYPE_CONSOLE;
Anton Vorontsovf29e5952012-05-26 06:20:19 -0700505
Kees Cookb77fa612018-11-01 14:08:07 -0700506 record.buf = (char *)s;
507 record.size = c;
508 psinfo->write(&record);
Anton Vorontsovf29e5952012-05-26 06:20:19 -0700509}
510
511static struct console pstore_console = {
512 .name = "pstore",
513 .write = pstore_console_write,
514 .flags = CON_PRINTBUFFER | CON_ENABLED | CON_ANYTIME,
515 .index = -1,
516};
517
518static void pstore_register_console(void)
519{
520 register_console(&pstore_console);
521}
Geliang Tangee1d2672015-10-20 00:39:03 -0700522
523static void pstore_unregister_console(void)
524{
525 unregister_console(&pstore_console);
526}
Anton Vorontsovf29e5952012-05-26 06:20:19 -0700527#else
528static void pstore_register_console(void) {}
Geliang Tangee1d2672015-10-20 00:39:03 -0700529static void pstore_unregister_console(void) {}
Anton Vorontsovf29e5952012-05-26 06:20:19 -0700530#endif
531
Kees Cook4c9ec212017-03-05 22:41:10 -0800532static int pstore_write_user_compat(struct pstore_record *record,
533 const char __user *buf)
Mark Salyzyn5bf6d1b2016-09-01 08:13:46 -0700534{
Kees Cook30800d92017-03-07 13:57:11 -0800535 int ret = 0;
Mark Salyzyn5bf6d1b2016-09-01 08:13:46 -0700536
Kees Cook30800d92017-03-07 13:57:11 -0800537 if (record->buf)
538 return -EINVAL;
Mark Salyzyn5bf6d1b2016-09-01 08:13:46 -0700539
Geliang Tang077090a2017-04-29 09:45:16 +0800540 record->buf = memdup_user(buf, record->size);
Hirofumi Nakagawadfd6fa32017-09-26 03:21:27 +0900541 if (IS_ERR(record->buf)) {
Geliang Tang077090a2017-04-29 09:45:16 +0800542 ret = PTR_ERR(record->buf);
Kees Cook30800d92017-03-07 13:57:11 -0800543 goto out;
Mark Salyzyn5bf6d1b2016-09-01 08:13:46 -0700544 }
Kees Cook30800d92017-03-07 13:57:11 -0800545
546 ret = record->psi->write(record);
547
Kees Cook30800d92017-03-07 13:57:11 -0800548 kfree(record->buf);
Geliang Tang077090a2017-04-29 09:45:16 +0800549out:
Kees Cook30800d92017-03-07 13:57:11 -0800550 record->buf = NULL;
551
552 return unlikely(ret < 0) ? ret : record->size;
Mark Salyzyn5bf6d1b2016-09-01 08:13:46 -0700553}
554
Tony Luckca01d6d2010-12-28 14:25:21 -0800555/*
556 * platform specific persistent storage driver registers with
557 * us here. If pstore is already mounted, call the platform
558 * read function right away to populate the file system. If not
559 * then the pstore mount code will call us later to fill out
560 * the file system.
Tony Luckca01d6d2010-12-28 14:25:21 -0800561 */
562int pstore_register(struct pstore_info *psi)
563{
564 struct module *owner = psi->owner;
565
Kees Cook0d7cd092017-03-03 12:11:40 -0800566 if (backend && strcmp(backend, psi->name)) {
567 pr_warn("ignoring unexpected backend '%s'\n", psi->name);
Lenny Szubowicz8e48b1a2013-06-28 17:11:33 -0400568 return -EPERM;
Kees Cook0d7cd092017-03-03 12:11:40 -0800569 }
Lenny Szubowicz8e48b1a2013-06-28 17:11:33 -0400570
Kees Cook4c9ec212017-03-05 22:41:10 -0800571 /* Sanity check flags. */
572 if (!psi->flags) {
573 pr_warn("backend '%s' must support at least one frontend\n",
574 psi->name);
575 return -EINVAL;
576 }
577
578 /* Check for required functions. */
579 if (!psi->read || !psi->write) {
580 pr_warn("backend '%s' must implement read() and write()\n",
581 psi->name);
582 return -EINVAL;
583 }
584
Tony Luckca01d6d2010-12-28 14:25:21 -0800585 spin_lock(&pstore_lock);
586 if (psinfo) {
Kees Cook0d7cd092017-03-03 12:11:40 -0800587 pr_warn("backend '%s' already loaded: ignoring '%s'\n",
588 psinfo->name, psi->name);
Tony Luckca01d6d2010-12-28 14:25:21 -0800589 spin_unlock(&pstore_lock);
590 return -EBUSY;
591 }
Matthew Garrettdee28e72011-07-21 16:57:55 -0400592
Kees Cook4c9ec212017-03-05 22:41:10 -0800593 if (!psi->write_user)
594 psi->write_user = pstore_write_user_compat;
Tony Luckca01d6d2010-12-28 14:25:21 -0800595 psinfo = psi;
Kees Cookf6f82852011-11-17 12:58:07 -0800596 mutex_init(&psinfo->read_mutex);
Tony Luckca01d6d2010-12-28 14:25:21 -0800597 spin_unlock(&pstore_lock);
598
599 if (owner && !try_module_get(owner)) {
600 psinfo = NULL;
601 return -EINVAL;
602 }
603
Aruna Balakrishnaiahb0aad7a2013-08-16 13:53:10 -0700604 allocate_buf_for_compression();
605
Tony Luckca01d6d2010-12-28 14:25:21 -0800606 if (pstore_is_mounted())
Luck, Tony6dda9262011-08-11 15:14:39 -0700607 pstore_get_records(0);
Tony Luckca01d6d2010-12-28 14:25:21 -0800608
Namhyung Kimc950fd62016-07-28 00:08:25 +0900609 if (psi->flags & PSTORE_FLAGS_DMESG)
610 pstore_register_kmsg();
611 if (psi->flags & PSTORE_FLAGS_CONSOLE)
Luck, Tonydf36ac12013-12-18 15:17:10 -0800612 pstore_register_console();
Namhyung Kimc950fd62016-07-28 00:08:25 +0900613 if (psi->flags & PSTORE_FLAGS_FTRACE)
Luck, Tonydf36ac12013-12-18 15:17:10 -0800614 pstore_register_ftrace();
Namhyung Kimc950fd62016-07-28 00:08:25 +0900615 if (psi->flags & PSTORE_FLAGS_PMSG)
Mark Salyzyn9d5438f2015-01-16 16:01:10 -0800616 pstore_register_pmsg();
Tony Luckca01d6d2010-12-28 14:25:21 -0800617
Kees Cook6330d552017-03-06 12:42:12 -0800618 /* Start watching for new records, if desired. */
Anton Vorontsova3f5f072012-05-26 06:20:28 -0700619 if (pstore_update_ms >= 0) {
620 pstore_timer.expires = jiffies +
621 msecs_to_jiffies(pstore_update_ms);
622 add_timer(&pstore_timer);
623 }
Luck, Tony6dda9262011-08-11 15:14:39 -0700624
Wang Long42222c22015-05-21 09:34:22 -0700625 /*
626 * Update the module parameter backend, so it is visible
627 * through /sys/module/pstore/parameters/backend
628 */
629 backend = psi->name;
630
Fabian Frederickef748852014-06-06 14:37:31 -0700631 pr_info("Registered %s as persistent store backend\n", psi->name);
Lenny Szubowicz8e48b1a2013-06-28 17:11:33 -0400632
Kees Cook1344dd82017-03-03 17:45:38 -0800633 module_put(owner);
634
Tony Luckca01d6d2010-12-28 14:25:21 -0800635 return 0;
636}
637EXPORT_SYMBOL_GPL(pstore_register);
638
Geliang Tangee1d2672015-10-20 00:39:03 -0700639void pstore_unregister(struct pstore_info *psi)
640{
Kees Cook6330d552017-03-06 12:42:12 -0800641 /* Stop timer and make sure all work has finished. */
642 pstore_update_ms = -1;
643 del_timer_sync(&pstore_timer);
644 flush_work(&pstore_work);
645
Namhyung Kimc950fd62016-07-28 00:08:25 +0900646 if (psi->flags & PSTORE_FLAGS_PMSG)
Kees Cooka1db8062016-05-19 10:59:03 -0400647 pstore_unregister_pmsg();
Namhyung Kimc950fd62016-07-28 00:08:25 +0900648 if (psi->flags & PSTORE_FLAGS_FTRACE)
Kees Cooka1db8062016-05-19 10:59:03 -0400649 pstore_unregister_ftrace();
Namhyung Kimc950fd62016-07-28 00:08:25 +0900650 if (psi->flags & PSTORE_FLAGS_CONSOLE)
Kees Cooka1db8062016-05-19 10:59:03 -0400651 pstore_unregister_console();
Namhyung Kimc950fd62016-07-28 00:08:25 +0900652 if (psi->flags & PSTORE_FLAGS_DMESG)
653 pstore_unregister_kmsg();
Geliang Tangee1d2672015-10-20 00:39:03 -0700654
655 free_buf_for_compression();
656
657 psinfo = NULL;
658 backend = NULL;
659}
660EXPORT_SYMBOL_GPL(pstore_unregister);
661
Kees Cook634f8f52017-03-03 17:35:25 -0800662static void decompress_record(struct pstore_record *record)
663{
Kees Cookbdabc8e2018-10-26 01:17:07 -0700664 int ret;
Kees Cook634f8f52017-03-03 17:35:25 -0800665 int unzipped_len;
Kees Cookbdabc8e2018-10-26 01:17:07 -0700666 char *unzipped, *workspace;
Kees Cook634f8f52017-03-03 17:35:25 -0800667
Ankit Kumar4a16d1c2017-05-23 11:16:52 +0530668 if (!record->compressed)
669 return;
670
Kees Cook634f8f52017-03-03 17:35:25 -0800671 /* Only PSTORE_TYPE_DMESG support compression. */
Ankit Kumar4a16d1c2017-05-23 11:16:52 +0530672 if (record->type != PSTORE_TYPE_DMESG) {
Kees Cook634f8f52017-03-03 17:35:25 -0800673 pr_warn("ignored compressed record type %d\n", record->type);
674 return;
675 }
676
Kees Cookbdabc8e2018-10-26 01:17:07 -0700677 /* Missing compression buffer means compression was not initialized. */
Kees Cook634f8f52017-03-03 17:35:25 -0800678 if (!big_oops_buf) {
Kees Cookbdabc8e2018-10-26 01:17:07 -0700679 pr_warn("no decompression method initialized!\n");
Kees Cook634f8f52017-03-03 17:35:25 -0800680 return;
681 }
682
Kees Cookbdabc8e2018-10-26 01:17:07 -0700683 /* Allocate enough space to hold max decompression and ECC. */
684 unzipped_len = big_oops_buf_sz;
685 workspace = kmalloc(unzipped_len + record->ecc_notice_size,
686 GFP_KERNEL);
687 if (!workspace)
Kees Cook7e8cc8d2017-03-04 22:28:46 -0800688 return;
Kees Cook7e8cc8d2017-03-04 22:28:46 -0800689
Kees Cookbdabc8e2018-10-26 01:17:07 -0700690 /* After decompression "unzipped_len" is almost certainly smaller. */
691 ret = crypto_comp_decompress(tfm, record->buf, record->size,
692 workspace, &unzipped_len);
693 if (ret) {
694 pr_err("crypto_comp_decompress failed, ret = %d!\n", ret);
695 kfree(workspace);
Kees Cook7e8cc8d2017-03-04 22:28:46 -0800696 return;
697 }
Kees Cook7e8cc8d2017-03-04 22:28:46 -0800698
699 /* Append ECC notice to decompressed buffer. */
Kees Cookbdabc8e2018-10-26 01:17:07 -0700700 memcpy(workspace + unzipped_len, record->buf + record->size,
Kees Cook7e8cc8d2017-03-04 22:28:46 -0800701 record->ecc_notice_size);
702
Kees Cookbdabc8e2018-10-26 01:17:07 -0700703 /* Copy decompressed contents into an minimum-sized allocation. */
704 unzipped = kmemdup(workspace, unzipped_len + record->ecc_notice_size,
705 GFP_KERNEL);
706 kfree(workspace);
707 if (!unzipped)
708 return;
709
710 /* Swap out compressed contents with decompressed contents. */
Kees Cook7e8cc8d2017-03-04 22:28:46 -0800711 kfree(record->buf);
Kees Cookbdabc8e2018-10-26 01:17:07 -0700712 record->buf = unzipped;
Kees Cook7e8cc8d2017-03-04 22:28:46 -0800713 record->size = unzipped_len;
714 record->compressed = false;
Kees Cook634f8f52017-03-03 17:35:25 -0800715}
716
Tony Luckca01d6d2010-12-28 14:25:21 -0800717/*
Kees Cook3a7d2fd2017-04-27 15:53:21 -0700718 * Read all the records from one persistent store backend. Create
Luck, Tony6dda9262011-08-11 15:14:39 -0700719 * files in our filesystem. Don't warn about -EEXIST errors
720 * when we are re-scanning the backing store looking to add new
721 * error records.
Tony Luckca01d6d2010-12-28 14:25:21 -0800722 */
Kees Cook3a7d2fd2017-04-27 15:53:21 -0700723void pstore_get_backend_records(struct pstore_info *psi,
724 struct dentry *root, int quiet)
Tony Luckca01d6d2010-12-28 14:25:21 -0800725{
Kees Cook2a2b0ac2017-03-04 22:57:26 -0800726 int failed = 0;
Kees Cook656de422017-05-16 12:03:31 -0700727 unsigned int stop_loop = 65536;
Tony Luckca01d6d2010-12-28 14:25:21 -0800728
Kees Cook3a7d2fd2017-04-27 15:53:21 -0700729 if (!psi || !root)
Tony Luckca01d6d2010-12-28 14:25:21 -0800730 return;
731
Kees Cookf6f82852011-11-17 12:58:07 -0800732 mutex_lock(&psi->read_mutex);
Kees Cook2174f6d2011-11-18 13:49:00 -0800733 if (psi->open && psi->open(psi))
Chen Gong06cf91b2011-05-16 11:00:27 -0700734 goto out;
735
Kees Cook1dfff7d2017-03-04 22:46:41 -0800736 /*
737 * Backend callback read() allocates record.buf. decompress_record()
738 * may reallocate record.buf. On success, pstore_mkfile() will keep
739 * the record.buf, so free it only on failure.
740 */
Kees Cook656de422017-05-16 12:03:31 -0700741 for (; stop_loop; stop_loop--) {
Kees Cook2a2b0ac2017-03-04 22:57:26 -0800742 struct pstore_record *record;
743 int rc;
744
745 record = kzalloc(sizeof(*record), GFP_KERNEL);
746 if (!record) {
747 pr_err("out of memory creating record\n");
748 break;
749 }
Kees Cooke581ca82017-05-19 15:10:31 -0700750 pstore_record_init(record, psi);
Kees Cook2a2b0ac2017-03-04 22:57:26 -0800751
752 record->size = psi->read(record);
753
754 /* No more records left in backend? */
Douglas Andersonf6525b92017-05-30 15:50:38 -0700755 if (record->size <= 0) {
756 kfree(record);
Kees Cook2a2b0ac2017-03-04 22:57:26 -0800757 break;
Douglas Andersonf6525b92017-05-30 15:50:38 -0700758 }
Kees Cook2a2b0ac2017-03-04 22:57:26 -0800759
760 decompress_record(record);
Kees Cook3a7d2fd2017-04-27 15:53:21 -0700761 rc = pstore_mkfile(root, record);
Kees Cook1dfff7d2017-03-04 22:46:41 -0800762 if (rc) {
Kees Cook83f70f02017-03-04 23:12:24 -0800763 /* pstore_mkfile() did not take record, so free it. */
Kees Cook2a2b0ac2017-03-04 22:57:26 -0800764 kfree(record->buf);
Kees Cook83f70f02017-03-04 23:12:24 -0800765 kfree(record);
Kees Cook1dfff7d2017-03-04 22:46:41 -0800766 if (rc != -EEXIST || !quiet)
767 failed++;
768 }
Tony Luckca01d6d2010-12-28 14:25:21 -0800769 }
Kees Cook2174f6d2011-11-18 13:49:00 -0800770 if (psi->close)
771 psi->close(psi);
Chen Gong06cf91b2011-05-16 11:00:27 -0700772out:
Kees Cookf6f82852011-11-17 12:58:07 -0800773 mutex_unlock(&psi->read_mutex);
Tony Luckca01d6d2010-12-28 14:25:21 -0800774
775 if (failed)
Kees Cook656de422017-05-16 12:03:31 -0700776 pr_warn("failed to create %d record(s) from '%s'\n",
Fabian Frederickef748852014-06-06 14:37:31 -0700777 failed, psi->name);
Kees Cook656de422017-05-16 12:03:31 -0700778 if (!stop_loop)
779 pr_err("looping? Too many records seen from '%s'\n",
780 psi->name);
Tony Luckca01d6d2010-12-28 14:25:21 -0800781}
782
Luck, Tony6dda9262011-08-11 15:14:39 -0700783static void pstore_dowork(struct work_struct *work)
784{
785 pstore_get_records(1);
786}
787
Kees Cook24ed9602017-08-28 11:28:21 -0700788static void pstore_timefunc(struct timer_list *unused)
Luck, Tony6dda9262011-08-11 15:14:39 -0700789{
790 if (pstore_new_entry) {
791 pstore_new_entry = 0;
792 schedule_work(&pstore_work);
793 }
794
Kees Cook6330d552017-03-06 12:42:12 -0800795 if (pstore_update_ms >= 0)
796 mod_timer(&pstore_timer,
797 jiffies + msecs_to_jiffies(pstore_update_ms));
Luck, Tony6dda9262011-08-11 15:14:39 -0700798}
799
Kees Cookfe1d4752018-03-06 15:57:38 -0800800void __init pstore_choose_compression(void)
801{
802 const struct pstore_zbackend *step;
803
804 if (!compress)
805 return;
806
807 for (step = zbackends; step->name; step++) {
808 if (!strcmp(compress, step->name)) {
809 zbackend = step;
Kees Cookfe1d4752018-03-06 15:57:38 -0800810 return;
811 }
812 }
813}
814
Kees Cookcb095af2018-10-18 11:17:42 -0700815static int __init pstore_init(void)
816{
817 int ret;
818
819 pstore_choose_compression();
820
Joel Fernandes (Google)41603162018-10-17 03:13:55 -0700821 /*
822 * Check if any pstore backends registered earlier but did not
823 * initialize compression because crypto was not ready. If so,
824 * initialize compression now.
825 */
Kees Cook95047b02018-10-17 14:00:12 -0700826 allocate_buf_for_compression();
Joel Fernandes (Google)41603162018-10-17 03:13:55 -0700827
Kees Cookcb095af2018-10-18 11:17:42 -0700828 ret = pstore_init_fs();
829 if (ret)
830 return ret;
831
832 return 0;
833}
Joel Fernandes (Google)41603162018-10-17 03:13:55 -0700834late_initcall(pstore_init);
Kees Cookcb095af2018-10-18 11:17:42 -0700835
836static void __exit pstore_exit(void)
837{
838 pstore_exit_fs();
839}
840module_exit(pstore_exit)
841
Kees Cookfe1d4752018-03-06 15:57:38 -0800842module_param(compress, charp, 0444);
843MODULE_PARM_DESC(compress, "Pstore compression to use");
844
Matthew Garrettdee28e72011-07-21 16:57:55 -0400845module_param(backend, charp, 0444);
846MODULE_PARM_DESC(backend, "Pstore backend to use");
Kees Cookcb095af2018-10-18 11:17:42 -0700847
848MODULE_AUTHOR("Tony Luck <tony.luck@intel.com>");
849MODULE_LICENSE("GPL");