]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - fs/pstore/platform.c
Merge tag 'thermal-v5.14-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/therma...
[mirror_ubuntu-jammy-kernel.git] / fs / pstore / platform.c
CommitLineData
45051539 1// SPDX-License-Identifier: GPL-2.0-only
ca01d6dd
TL
2/*
3 * Persistent Storage - platform driver interface parts.
4 *
f29e5956 5 * Copyright (C) 2007-2008 Google, Inc.
ca01d6dd 6 * Copyright (C) 2010 Intel Corporation <tony.luck@intel.com>
ca01d6dd
TL
7 */
8
ef748853
FF
9#define pr_fmt(fmt) "pstore: " fmt
10
ca01d6dd
TL
11#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>
f29e5956 16#include <linux/console.h>
ca01d6dd
TL
17#include <linux/module.h>
18#include <linux/pstore.h>
58eb5b67 19#if IS_ENABLED(CONFIG_PSTORE_LZO_COMPRESS)
8cfc8ddc
GT
20#include <linux/lzo.h>
21#endif
58eb5b67 22#if IS_ENABLED(CONFIG_PSTORE_LZ4_COMPRESS) || IS_ENABLED(CONFIG_PSTORE_LZ4HC_COMPRESS)
8cfc8ddc
GT
23#include <linux/lz4.h>
24#endif
1021bcf4
GT
25#if IS_ENABLED(CONFIG_PSTORE_ZSTD_COMPRESS)
26#include <linux/zstd.h>
27#endif
cb3bee03 28#include <linux/crypto.h>
ca01d6dd 29#include <linux/string.h>
6dda9266 30#include <linux/timer.h>
ca01d6dd
TL
31#include <linux/slab.h>
32#include <linux/uaccess.h>
a3f5f075 33#include <linux/jiffies.h>
6dda9266 34#include <linux/workqueue.h>
ca01d6dd
TL
35
36#include "internal.h"
37
6dda9266
TL
38/*
39 * 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 */
521f7288 43static int pstore_update_ms = -1;
a3f5f075
AV
44module_param_named(update_ms, pstore_update_ms, int, 0600);
45MODULE_PARM_DESC(update_ms, "milliseconds before pstore updates its content "
521f7288 46 "(default is -1, which means runtime updates are disabled; "
78c83c82 47 "enabling this option may not be safe; it may lead to further "
521f7288 48 "corruption on Oopses)");
6dda9266 49
f0f23e54
JFG
50/* 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
6dda9266
TL
63static int pstore_new_entry;
64
24ed960a 65static void pstore_timefunc(struct timer_list *);
1d27e3e2 66static DEFINE_TIMER(pstore_timer, pstore_timefunc);
6dda9266
TL
67
68static void pstore_dowork(struct work_struct *);
69static DECLARE_WORK(pstore_work, pstore_dowork);
70
ca01d6dd 71/*
6248a066
KC
72 * psinfo_lock protects "psinfo" during calls to
73 * pstore_register(), pstore_unregister(), and
74 * the filesystem mount/unmount routines.
ca01d6dd 75 */
cab12fd0 76static DEFINE_MUTEX(psinfo_lock);
060287b8 77struct pstore_info *psinfo;
ca01d6dd 78
dee28e72 79static char *backend;
d973f7d8
KC
80module_param(backend, charp, 0444);
81MODULE_PARM_DESC(backend, "specific backend to use");
82
fe1d4758
KC
83static char *compress =
84#ifdef CONFIG_PSTORE_COMPRESS_DEFAULT
85 CONFIG_PSTORE_COMPRESS_DEFAULT;
86#else
87 NULL;
88#endif
d973f7d8
KC
89module_param(compress, charp, 0444);
90MODULE_PARM_DESC(compress, "compression to use");
dee28e72 91
b0aad7a9 92/* Compression parameters */
cb3bee03 93static struct crypto_comp *tfm;
8cfc8ddc
GT
94
95struct pstore_zbackend {
cb3bee03 96 int (*zbufsize)(size_t size);
8cfc8ddc
GT
97 const char *name;
98};
b0aad7a9
AB
99
100static char *big_oops_buf;
101static size_t big_oops_buf_sz;
102
366f7e7a 103/* How much of the console log to snapshot */
26fecbf7 104unsigned long kmsg_bytes = CONFIG_PSTORE_DEFAULT_KMSG_BYTES;
ca01d6dd 105
366f7e7a 106void pstore_set_kmsg_bytes(int bytes)
ca01d6dd 107{
366f7e7a 108 kmsg_bytes = bytes;
ca01d6dd
TL
109}
110
ca01d6dd
TL
111/* Tag each group of saved records with a sequence number */
112static int oopscount;
113
f0f23e54
JFG
114const char *pstore_type_to_name(enum pstore_type_id type)
115{
116 BUILD_BUG_ON(ARRAY_SIZE(pstore_type_names) != PSTORE_TYPE_MAX);
117
118 if (WARN_ON_ONCE(type >= PSTORE_TYPE_MAX))
119 return "unknown";
120
121 return pstore_type_names[type];
122}
123EXPORT_SYMBOL_GPL(pstore_type_to_name);
124
125enum pstore_type_id pstore_name_to_type(const char *name)
126{
127 int i;
128
129 for (i = 0; i < PSTORE_TYPE_MAX; i++) {
130 if (!strcmp(pstore_type_names[i], name))
131 return i;
132 }
133
134 return PSTORE_TYPE_MAX;
135}
136EXPORT_SYMBOL_GPL(pstore_name_to_type);
137
78c83c82
KC
138static void pstore_timer_kick(void)
139{
140 if (pstore_update_ms < 0)
141 return;
142
143 mod_timer(&pstore_timer, jiffies + msecs_to_jiffies(pstore_update_ms));
144}
145
ea84b580
KC
146/*
147 * Should pstore_dump() wait for a concurrent pstore_dump()? If
148 * not, the current pstore_dump() will report a failure to dump
149 * and return.
150 */
151static bool pstore_cannot_wait(enum kmsg_dump_reason reason)
9f244e9c 152{
ea84b580 153 /* In NMI path, pstore shouldn't block regardless of reason. */
9f244e9c
SA
154 if (in_nmi())
155 return true;
156
157 switch (reason) {
158 /* In panic case, other cpus are stopped by smp_send_stop(). */
159 case KMSG_DUMP_PANIC:
ea84b580 160 /* Emergency restart shouldn't be blocked. */
9f244e9c
SA
161 case KMSG_DUMP_EMERG:
162 return true;
163 default:
164 return false;
165 }
166}
9f244e9c 167
58eb5b67 168#if IS_ENABLED(CONFIG_PSTORE_DEFLATE_COMPRESS)
cb3bee03 169static int zbufsize_deflate(size_t size)
adb42f5e 170{
7de8fe2f
AB
171 size_t cmpr;
172
cb3bee03 173 switch (size) {
7de8fe2f
AB
174 /* buffer range for efivars */
175 case 1000 ... 2000:
176 cmpr = 56;
177 break;
178 case 2001 ... 3000:
179 cmpr = 54;
180 break;
181 case 3001 ... 3999:
182 cmpr = 52;
183 break;
184 /* buffer range for nvram, erst */
185 case 4000 ... 10000:
186 cmpr = 45;
187 break;
188 default:
189 cmpr = 60;
190 break;
191 }
b0aad7a9 192
cb3bee03 193 return (size * 100) / cmpr;
8cfc8ddc 194}
8cfc8ddc
GT
195#endif
196
58eb5b67 197#if IS_ENABLED(CONFIG_PSTORE_LZO_COMPRESS)
cb3bee03 198static int zbufsize_lzo(size_t size)
8cfc8ddc 199{
cb3bee03 200 return lzo1x_worst_compress(size);
8cfc8ddc 201}
8cfc8ddc
GT
202#endif
203
58eb5b67 204#if IS_ENABLED(CONFIG_PSTORE_LZ4_COMPRESS) || IS_ENABLED(CONFIG_PSTORE_LZ4HC_COMPRESS)
cb3bee03 205static int zbufsize_lz4(size_t size)
8cfc8ddc 206{
cb3bee03 207 return LZ4_compressBound(size);
8cfc8ddc 208}
239b7161
GT
209#endif
210
58eb5b67 211#if IS_ENABLED(CONFIG_PSTORE_842_COMPRESS)
cb3bee03 212static int zbufsize_842(size_t size)
239b7161 213{
55597406 214 return size;
239b7161 215}
8cfc8ddc
GT
216#endif
217
1021bcf4
GT
218#if IS_ENABLED(CONFIG_PSTORE_ZSTD_COMPRESS)
219static int zbufsize_zstd(size_t size)
220{
221 return ZSTD_compressBound(size);
222}
223#endif
224
fe1d4758
KC
225static const struct pstore_zbackend *zbackend __ro_after_init;
226
227static const struct pstore_zbackend zbackends[] = {
58eb5b67 228#if IS_ENABLED(CONFIG_PSTORE_DEFLATE_COMPRESS)
fe1d4758 229 {
cb3bee03
GT
230 .zbufsize = zbufsize_deflate,
231 .name = "deflate",
fe1d4758
KC
232 },
233#endif
58eb5b67 234#if IS_ENABLED(CONFIG_PSTORE_LZO_COMPRESS)
fe1d4758 235 {
cb3bee03 236 .zbufsize = zbufsize_lzo,
fe1d4758
KC
237 .name = "lzo",
238 },
239#endif
58eb5b67 240#if IS_ENABLED(CONFIG_PSTORE_LZ4_COMPRESS)
fe1d4758 241 {
cb3bee03 242 .zbufsize = zbufsize_lz4,
fe1d4758
KC
243 .name = "lz4",
244 },
245#endif
58eb5b67 246#if IS_ENABLED(CONFIG_PSTORE_LZ4HC_COMPRESS)
fe1d4758 247 {
cb3bee03 248 .zbufsize = zbufsize_lz4,
fe1d4758
KC
249 .name = "lz4hc",
250 },
8cfc8ddc 251#endif
58eb5b67 252#if IS_ENABLED(CONFIG_PSTORE_842_COMPRESS)
fe1d4758 253 {
cb3bee03 254 .zbufsize = zbufsize_842,
fe1d4758
KC
255 .name = "842",
256 },
1021bcf4
GT
257#endif
258#if IS_ENABLED(CONFIG_PSTORE_ZSTD_COMPRESS)
259 {
260 .zbufsize = zbufsize_zstd,
261 .name = "zstd",
262 },
fe1d4758
KC
263#endif
264 { }
265};
8cfc8ddc
GT
266
267static int pstore_compress(const void *in, void *out,
cb3bee03 268 unsigned int inlen, unsigned int outlen)
8cfc8ddc 269{
cb3bee03
GT
270 int ret;
271
19d8e914 272 if (!IS_ENABLED(CONFIG_PSTORE_COMPRESS))
fd49e032
MC
273 return -EINVAL;
274
cb3bee03
GT
275 ret = crypto_comp_compress(tfm, in, inlen, out, &outlen);
276 if (ret) {
277 pr_err("crypto_comp_compress failed, ret = %d!\n", ret);
278 return ret;
279 }
280
281 return outlen;
8cfc8ddc
GT
282}
283
8cfc8ddc
GT
284static void allocate_buf_for_compression(void)
285{
95047b05
KC
286 struct crypto_comp *ctx;
287 int size;
288 char *buf;
289
290 /* Skip if not built-in or compression backend not selected yet. */
e698aaf3 291 if (!IS_ENABLED(CONFIG_PSTORE_COMPRESS) || !zbackend)
cb3bee03
GT
292 return;
293
95047b05
KC
294 /* Skip if no pstore backend yet or compression init already done. */
295 if (!psinfo || tfm)
296 return;
297
cb3bee03 298 if (!crypto_has_comp(zbackend->name, 0, 0)) {
95047b05 299 pr_err("Unknown compression: %s\n", zbackend->name);
cb3bee03
GT
300 return;
301 }
302
95047b05
KC
303 size = zbackend->zbufsize(psinfo->bufsize);
304 if (size <= 0) {
305 pr_err("Invalid compression size for %s: %d\n",
306 zbackend->name, size);
cb3bee03 307 return;
95047b05 308 }
cb3bee03 309
95047b05
KC
310 buf = kmalloc(size, GFP_KERNEL);
311 if (!buf) {
312 pr_err("Failed %d byte compression buffer allocation for: %s\n",
313 size, zbackend->name);
cb3bee03
GT
314 return;
315 }
316
95047b05
KC
317 ctx = crypto_alloc_comp(zbackend->name, 0, 0);
318 if (IS_ERR_OR_NULL(ctx)) {
319 kfree(buf);
320 pr_err("crypto_alloc_comp('%s') failed: %ld\n", zbackend->name,
321 PTR_ERR(ctx));
cb3bee03 322 return;
8cfc8ddc 323 }
95047b05
KC
324
325 /* A non-NULL big_oops_buf indicates compression is available. */
326 tfm = ctx;
327 big_oops_buf_sz = size;
328 big_oops_buf = buf;
329
0eed84ff 330 pr_info("Using crash dump compression: %s\n", zbackend->name);
8cfc8ddc
GT
331}
332
333static void free_buf_for_compression(void)
334{
a9fb94a9 335 if (IS_ENABLED(CONFIG_PSTORE_COMPRESS) && tfm) {
cb3bee03 336 crypto_free_comp(tfm);
a9fb94a9
PHS
337 tfm = NULL;
338 }
cb3bee03
GT
339 kfree(big_oops_buf);
340 big_oops_buf = NULL;
341 big_oops_buf_sz = 0;
ee1d2674
GT
342}
343
b0aad7a9
AB
344/*
345 * Called when compression fails, since the printk buffer
346 * would be fetched for compression calling it again when
347 * compression fails would have moved the iterator of
348 * printk buffer which results in fetching old contents.
349 * Copy the recent messages from big_oops_buf to psinfo->buf
350 */
351static size_t copy_kmsg_to_buffer(int hsize, size_t len)
352{
353 size_t total_len;
354 size_t diff;
355
356 total_len = hsize + len;
357
358 if (total_len > psinfo->bufsize) {
359 diff = total_len - psinfo->bufsize + hsize;
360 memcpy(psinfo->buf, big_oops_buf, hsize);
361 memcpy(psinfo->buf + hsize, big_oops_buf + diff,
362 psinfo->bufsize - hsize);
363 total_len = psinfo->bufsize;
364 } else
365 memcpy(psinfo->buf, big_oops_buf, total_len);
366
367 return total_len;
368}
369
e581ca81
KC
370void pstore_record_init(struct pstore_record *record,
371 struct pstore_info *psinfo)
372{
373 memset(record, 0, sizeof(*record));
374
375 record->psi = psinfo;
c7f3c595
KC
376
377 /* Report zeroed timestamp if called before timekeeping has resumed. */
7aaa822e 378 record->time = ns_to_timespec64(ktime_get_real_fast_ns());
e581ca81
KC
379}
380
ca01d6dd 381/*
0eed84ff
KC
382 * callback from kmsg_dump. Save as much as we can (up to kmsg_bytes) from the
383 * end of the buffer.
ca01d6dd
TL
384 */
385static void pstore_dump(struct kmsg_dumper *dumper,
e2ae715d 386 enum kmsg_dump_reason reason)
ca01d6dd 387{
f9f3f02d 388 struct kmsg_dump_iter iter;
e2ae715d 389 unsigned long total = 0;
381b872c 390 const char *why;
b94fdd07 391 unsigned int part = 1;
e2ae715d 392 int ret;
ca01d6dd 393
fb13cb8a 394 why = kmsg_dump_reason_str(reason);
9f6af27f 395
ea84b580
KC
396 if (down_trylock(&psinfo->buf_lock)) {
397 /* Failed to acquire lock: give up if we cannot wait. */
398 if (pstore_cannot_wait(reason)) {
399 pr_err("dump skipped in %s path: may corrupt error record\n",
400 in_nmi() ? "NMI" : why);
401 return;
402 }
403 if (down_interruptible(&psinfo->buf_lock)) {
404 pr_err("could not grab semaphore?!\n");
959217c8 405 return;
9f244e9c 406 }
98e44fda 407 }
ea84b580 408
f9f3f02d
JO
409 kmsg_dump_rewind(&iter);
410
ca01d6dd
TL
411 oopscount++;
412 while (total < kmsg_bytes) {
e2ae715d 413 char *dst;
76cc9580
KC
414 size_t dst_size;
415 int header_size;
b0aad7a9 416 int zipped_len = -1;
76cc9580 417 size_t dump_size;
e581ca81
KC
418 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;
e2ae715d 426
ea84b580 427 if (big_oops_buf) {
b0aad7a9 428 dst = big_oops_buf;
76cc9580 429 dst_size = big_oops_buf_sz;
235f6d15
NK
430 } else {
431 dst = psinfo->buf;
76cc9580 432 dst_size = psinfo->bufsize;
235f6d15
NK
433 }
434
76cc9580
KC
435 /* Write dump header. */
436 header_size = snprintf(dst, dst_size, "%s#%d Part%u\n", why,
437 oopscount, part);
438 dst_size -= header_size;
ca01d6dd 439
76cc9580 440 /* Write dump contents. */
f9f3f02d 441 if (!kmsg_dump_get_buffer(&iter, true, dst + header_size,
76cc9580 442 dst_size, &dump_size))
235f6d15 443 break;
b0aad7a9 444
ea84b580 445 if (big_oops_buf) {
b0aad7a9 446 zipped_len = pstore_compress(dst, psinfo->buf,
76cc9580
KC
447 header_size + dump_size,
448 psinfo->bufsize);
b0aad7a9
AB
449
450 if (zipped_len > 0) {
76cc9580
KC
451 record.compressed = true;
452 record.size = zipped_len;
b0aad7a9 453 } else {
76cc9580
KC
454 record.size = copy_kmsg_to_buffer(header_size,
455 dump_size);
b0aad7a9
AB
456 }
457 } else {
76cc9580 458 record.size = header_size + dump_size;
b0aad7a9 459 }
ca01d6dd 460
76cc9580 461 ret = psinfo->write(&record);
78c83c82 462 if (ret == 0 && reason == KMSG_DUMP_OOPS) {
6dda9266 463 pstore_new_entry = 1;
78c83c82
KC
464 pstore_timer_kick();
465 }
e2ae715d 466
76cc9580 467 total += record.size;
56280682 468 part++;
ca01d6dd 469 }
ea84b580
KC
470
471 up(&psinfo->buf_lock);
ca01d6dd
TL
472}
473
474static struct kmsg_dumper pstore_dumper = {
475 .dump = pstore_dump,
476};
477
306e5c2a
GT
478/*
479 * Register with kmsg_dump to save last part of console log on panic.
480 */
18730411
GT
481static void pstore_register_kmsg(void)
482{
483 kmsg_dump_register(&pstore_dumper);
484}
485
ee1d2674
GT
486static void pstore_unregister_kmsg(void)
487{
488 kmsg_dump_unregister(&pstore_dumper);
489}
490
f29e5956
AV
491#ifdef CONFIG_PSTORE_CONSOLE
492static void pstore_console_write(struct console *con, const char *s, unsigned c)
493{
b77fa617 494 struct pstore_record record;
f29e5956 495
4c6c4d34
YH
496 if (!c)
497 return;
498
b77fa617
KC
499 pstore_record_init(&record, psinfo);
500 record.type = PSTORE_TYPE_CONSOLE;
80c9d03c 501
b77fa617
KC
502 record.buf = (char *)s;
503 record.size = c;
504 psinfo->write(&record);
f29e5956
AV
505}
506
507static struct console pstore_console = {
f29e5956 508 .write = pstore_console_write,
f29e5956
AV
509 .index = -1,
510};
511
512static void pstore_register_console(void)
513{
d195c390
KC
514 /* Show which backend is going to get console writes. */
515 strscpy(pstore_console.name, psinfo->name,
516 sizeof(pstore_console.name));
b7753fc7
KC
517 /*
518 * Always initialize flags here since prior unregister_console()
519 * calls may have changed settings (specifically CON_ENABLED).
520 */
521 pstore_console.flags = CON_PRINTBUFFER | CON_ENABLED | CON_ANYTIME;
f29e5956
AV
522 register_console(&pstore_console);
523}
ee1d2674
GT
524
525static void pstore_unregister_console(void)
526{
527 unregister_console(&pstore_console);
528}
f29e5956
AV
529#else
530static void pstore_register_console(void) {}
ee1d2674 531static void pstore_unregister_console(void) {}
f29e5956
AV
532#endif
533
4c9ec219
KC
534static int pstore_write_user_compat(struct pstore_record *record,
535 const char __user *buf)
5bf6d1b9 536{
30800d99
KC
537 int ret = 0;
538
539 if (record->buf)
540 return -EINVAL;
541
077090af 542 record->buf = memdup_user(buf, record->size);
dfd6fa39 543 if (IS_ERR(record->buf)) {
077090af 544 ret = PTR_ERR(record->buf);
30800d99 545 goto out;
5bf6d1b9 546 }
30800d99
KC
547
548 ret = record->psi->write(record);
549
30800d99 550 kfree(record->buf);
077090af 551out:
30800d99
KC
552 record->buf = NULL;
553
554 return unlikely(ret < 0) ? ret : record->size;
5bf6d1b9
MS
555}
556
ca01d6dd
TL
557/*
558 * platform specific persistent storage driver registers with
559 * us here. If pstore is already mounted, call the platform
560 * read function right away to populate the file system. If not
561 * then the pstore mount code will call us later to fill out
562 * the file system.
ca01d6dd
TL
563 */
564int pstore_register(struct pstore_info *psi)
565{
0d7cd09a
KC
566 if (backend && strcmp(backend, psi->name)) {
567 pr_warn("ignoring unexpected backend '%s'\n", psi->name);
8e48b1a8 568 return -EPERM;
0d7cd09a 569 }
8e48b1a8 570
4c9ec219
KC
571 /* 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
cab12fd0 585 mutex_lock(&psinfo_lock);
ca01d6dd 586 if (psinfo) {
0d7cd09a
KC
587 pr_warn("backend '%s' already loaded: ignoring '%s'\n",
588 psinfo->name, psi->name);
cab12fd0 589 mutex_unlock(&psinfo_lock);
ca01d6dd
TL
590 return -EBUSY;
591 }
dee28e72 592
4c9ec219
KC
593 if (!psi->write_user)
594 psi->write_user = pstore_write_user_compat;
ca01d6dd 595 psinfo = psi;
f6f82851 596 mutex_init(&psinfo->read_mutex);
ea84b580 597 sema_init(&psinfo->buf_lock, 1);
ca01d6dd 598
8880fa32
KC
599 if (psi->flags & PSTORE_FLAGS_DMESG)
600 allocate_buf_for_compression();
b0aad7a9 601
27e5041a 602 pstore_get_records(0);
ca01d6dd 603
3524e688
PT
604 if (psi->flags & PSTORE_FLAGS_DMESG) {
605 pstore_dumper.max_reason = psinfo->max_reason;
c950fd6f 606 pstore_register_kmsg();
3524e688 607 }
c950fd6f 608 if (psi->flags & PSTORE_FLAGS_CONSOLE)
df36ac1b 609 pstore_register_console();
c950fd6f 610 if (psi->flags & PSTORE_FLAGS_FTRACE)
df36ac1b 611 pstore_register_ftrace();
c950fd6f 612 if (psi->flags & PSTORE_FLAGS_PMSG)
9d5438f4 613 pstore_register_pmsg();
ca01d6dd 614
6330d553 615 /* Start watching for new records, if desired. */
78c83c82 616 pstore_timer_kick();
6dda9266 617
42222c2a
WL
618 /*
619 * Update the module parameter backend, so it is visible
620 * through /sys/module/pstore/parameters/backend
621 */
563ca40d 622 backend = kstrdup(psi->name, GFP_KERNEL);
42222c2a 623
ef748853 624 pr_info("Registered %s as persistent store backend\n", psi->name);
8e48b1a8 625
6248a066 626 mutex_unlock(&psinfo_lock);
ca01d6dd
TL
627 return 0;
628}
629EXPORT_SYMBOL_GPL(pstore_register);
630
ee1d2674
GT
631void pstore_unregister(struct pstore_info *psi)
632{
6248a066
KC
633 /* It's okay to unregister nothing. */
634 if (!psi)
635 return;
636
637 mutex_lock(&psinfo_lock);
638
639 /* Only one backend can be registered at a time. */
640 if (WARN_ON(psi != psinfo)) {
641 mutex_unlock(&psinfo_lock);
642 return;
643 }
644
78c83c82 645 /* Unregister all callbacks. */
c950fd6f 646 if (psi->flags & PSTORE_FLAGS_PMSG)
a1db8060 647 pstore_unregister_pmsg();
c950fd6f 648 if (psi->flags & PSTORE_FLAGS_FTRACE)
a1db8060 649 pstore_unregister_ftrace();
c950fd6f 650 if (psi->flags & PSTORE_FLAGS_CONSOLE)
a1db8060 651 pstore_unregister_console();
c950fd6f
NK
652 if (psi->flags & PSTORE_FLAGS_DMESG)
653 pstore_unregister_kmsg();
ee1d2674 654
78c83c82
KC
655 /* Stop timer and make sure all work has finished. */
656 del_timer_sync(&pstore_timer);
657 flush_work(&pstore_work);
658
609e28bb
KC
659 /* Remove all backend records from filesystem tree. */
660 pstore_put_backend_records(psi);
661
ee1d2674
GT
662 free_buf_for_compression();
663
664 psinfo = NULL;
563ca40d 665 kfree(backend);
ee1d2674 666 backend = NULL;
6248a066 667 mutex_unlock(&psinfo_lock);
ee1d2674
GT
668}
669EXPORT_SYMBOL_GPL(pstore_unregister);
670
634f8f51
KC
671static void decompress_record(struct pstore_record *record)
672{
bdabc8e7 673 int ret;
634f8f51 674 int unzipped_len;
bdabc8e7 675 char *unzipped, *workspace;
634f8f51 676
19d8e914 677 if (!IS_ENABLED(CONFIG_PSTORE_COMPRESS) || !record->compressed)
4a16d1cb
AK
678 return;
679
634f8f51 680 /* Only PSTORE_TYPE_DMESG support compression. */
4a16d1cb 681 if (record->type != PSTORE_TYPE_DMESG) {
634f8f51
KC
682 pr_warn("ignored compressed record type %d\n", record->type);
683 return;
684 }
685
bdabc8e7 686 /* Missing compression buffer means compression was not initialized. */
634f8f51 687 if (!big_oops_buf) {
bdabc8e7 688 pr_warn("no decompression method initialized!\n");
634f8f51
KC
689 return;
690 }
691
bdabc8e7
KC
692 /* Allocate enough space to hold max decompression and ECC. */
693 unzipped_len = big_oops_buf_sz;
694 workspace = kmalloc(unzipped_len + record->ecc_notice_size,
695 GFP_KERNEL);
696 if (!workspace)
7e8cc8dc 697 return;
7e8cc8dc 698
bdabc8e7
KC
699 /* After decompression "unzipped_len" is almost certainly smaller. */
700 ret = crypto_comp_decompress(tfm, record->buf, record->size,
701 workspace, &unzipped_len);
702 if (ret) {
703 pr_err("crypto_comp_decompress failed, ret = %d!\n", ret);
704 kfree(workspace);
7e8cc8dc
KC
705 return;
706 }
7e8cc8dc
KC
707
708 /* Append ECC notice to decompressed buffer. */
bdabc8e7 709 memcpy(workspace + unzipped_len, record->buf + record->size,
7e8cc8dc
KC
710 record->ecc_notice_size);
711
bdabc8e7
KC
712 /* Copy decompressed contents into an minimum-sized allocation. */
713 unzipped = kmemdup(workspace, unzipped_len + record->ecc_notice_size,
714 GFP_KERNEL);
715 kfree(workspace);
716 if (!unzipped)
717 return;
718
719 /* Swap out compressed contents with decompressed contents. */
7e8cc8dc 720 kfree(record->buf);
bdabc8e7 721 record->buf = unzipped;
7e8cc8dc
KC
722 record->size = unzipped_len;
723 record->compressed = false;
634f8f51
KC
724}
725
ca01d6dd 726/*
3a7d2fd1 727 * Read all the records from one persistent store backend. Create
6dda9266
TL
728 * files in our filesystem. Don't warn about -EEXIST errors
729 * when we are re-scanning the backing store looking to add new
730 * error records.
ca01d6dd 731 */
3a7d2fd1
KC
732void pstore_get_backend_records(struct pstore_info *psi,
733 struct dentry *root, int quiet)
ca01d6dd 734{
2a2b0acf 735 int failed = 0;
656de42e 736 unsigned int stop_loop = 65536;
ca01d6dd 737
3a7d2fd1 738 if (!psi || !root)
ca01d6dd
TL
739 return;
740
f6f82851 741 mutex_lock(&psi->read_mutex);
2174f6df 742 if (psi->open && psi->open(psi))
06cf91b4
CG
743 goto out;
744
1dfff7dd
KC
745 /*
746 * Backend callback read() allocates record.buf. decompress_record()
747 * may reallocate record.buf. On success, pstore_mkfile() will keep
748 * the record.buf, so free it only on failure.
749 */
656de42e 750 for (; stop_loop; stop_loop--) {
2a2b0acf
KC
751 struct pstore_record *record;
752 int rc;
753
754 record = kzalloc(sizeof(*record), GFP_KERNEL);
755 if (!record) {
756 pr_err("out of memory creating record\n");
757 break;
758 }
e581ca81 759 pstore_record_init(record, psi);
2a2b0acf
KC
760
761 record->size = psi->read(record);
762
763 /* No more records left in backend? */
f6525b96
DA
764 if (record->size <= 0) {
765 kfree(record);
2a2b0acf 766 break;
f6525b96 767 }
2a2b0acf
KC
768
769 decompress_record(record);
3a7d2fd1 770 rc = pstore_mkfile(root, record);
1dfff7dd 771 if (rc) {
83f70f07 772 /* pstore_mkfile() did not take record, so free it. */
2a2b0acf 773 kfree(record->buf);
83f70f07 774 kfree(record);
1dfff7dd
KC
775 if (rc != -EEXIST || !quiet)
776 failed++;
777 }
ca01d6dd 778 }
2174f6df
KC
779 if (psi->close)
780 psi->close(psi);
06cf91b4 781out:
f6f82851 782 mutex_unlock(&psi->read_mutex);
ca01d6dd
TL
783
784 if (failed)
656de42e 785 pr_warn("failed to create %d record(s) from '%s'\n",
ef748853 786 failed, psi->name);
656de42e
KC
787 if (!stop_loop)
788 pr_err("looping? Too many records seen from '%s'\n",
789 psi->name);
ca01d6dd
TL
790}
791
6dda9266
TL
792static void pstore_dowork(struct work_struct *work)
793{
794 pstore_get_records(1);
795}
796
24ed960a 797static void pstore_timefunc(struct timer_list *unused)
6dda9266
TL
798{
799 if (pstore_new_entry) {
800 pstore_new_entry = 0;
801 schedule_work(&pstore_work);
802 }
803
78c83c82 804 pstore_timer_kick();
6dda9266
TL
805}
806
8d82cee2 807static void __init pstore_choose_compression(void)
fe1d4758
KC
808{
809 const struct pstore_zbackend *step;
810
811 if (!compress)
812 return;
813
814 for (step = zbackends; step->name; step++) {
815 if (!strcmp(compress, step->name)) {
816 zbackend = step;
fe1d4758
KC
817 return;
818 }
819 }
820}
821
cb095afd
KC
822static int __init pstore_init(void)
823{
824 int ret;
825
826 pstore_choose_compression();
827
41603165
JFG
828 /*
829 * Check if any pstore backends registered earlier but did not
830 * initialize compression because crypto was not ready. If so,
831 * initialize compression now.
832 */
95047b05 833 allocate_buf_for_compression();
41603165 834
cb095afd
KC
835 ret = pstore_init_fs();
836 if (ret)
8a57d6d4 837 free_buf_for_compression();
cb095afd 838
8a57d6d4 839 return ret;
cb095afd 840}
41603165 841late_initcall(pstore_init);
cb095afd
KC
842
843static void __exit pstore_exit(void)
844{
845 pstore_exit_fs();
846}
847module_exit(pstore_exit)
848
cb095afd
KC
849MODULE_AUTHOR("Tony Luck <tony.luck@intel.com>");
850MODULE_LICENSE("GPL");