1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Persistent Storage - platform driver interface parts.
4 *
5 * Copyright (C) 2007-2008 Google, Inc.
6 * Copyright (C) 2010 Intel Corporation <tony.luck@intel.com>
7 */
8
9 #define pr_fmt(fmt) "pstore: " fmt
10
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>
16 #include <linux/console.h>
17 #include <linux/module.h>
18 #include <linux/pstore.h>
19 #ifdef CONFIG_PSTORE_BLACKBOX
20 #include <linux/stacktrace.h>
21 #include <linux/blackbox.h>
22 #endif
23 #if IS_ENABLED(CONFIG_PSTORE_LZO_COMPRESS)
24 #include <linux/lzo.h>
25 #endif
26 #if IS_ENABLED(CONFIG_PSTORE_LZ4_COMPRESS) || IS_ENABLED(CONFIG_PSTORE_LZ4HC_COMPRESS)
27 #include <linux/lz4.h>
28 #endif
29 #if IS_ENABLED(CONFIG_PSTORE_ZSTD_COMPRESS)
30 #include <linux/zstd.h>
31 #endif
32 #include <linux/crypto.h>
33 #include <linux/string.h>
34 #include <linux/timer.h>
35 #include <linux/slab.h>
36 #include <linux/uaccess.h>
37 #include <linux/jiffies.h>
38 #include <linux/workqueue.h>
39
40 #include "internal.h"
41
42 /*
43 * We defer making "oops" entries appear in pstore - see
44 * whether the system is actually still running well enough
45 * to let someone see the entry
46 */
47 static int pstore_update_ms = -1;
48 module_param_named(update_ms, pstore_update_ms, int, 0600);
49 MODULE_PARM_DESC(update_ms, "milliseconds before pstore updates its content "
50 "(default is -1, which means runtime updates are disabled; "
51 "enabling this option may not be safe; it may lead to further "
52 "corruption on Oopses)");
53
54 /* Names should be in the same order as the enum pstore_type_id */
55 static const char * const pstore_type_names[] = {
56 "dmesg",
57 "mce",
58 "console",
59 "ftrace",
60 "rtas",
61 "powerpc-ofw",
62 "powerpc-common",
63 "pmsg",
64 "powerpc-opal",
65 "blackbox",
66 };
67
68 static int pstore_new_entry;
69
70 static void pstore_timefunc(struct timer_list *);
71 static DEFINE_TIMER(pstore_timer, pstore_timefunc);
72
73 static void pstore_dowork(struct work_struct *);
74 static DECLARE_WORK(pstore_work, pstore_dowork);
75
76 /*
77 * psinfo_lock protects "psinfo" during calls to
78 * pstore_register(), pstore_unregister(), and
79 * the filesystem mount/unmount routines.
80 */
81 static DEFINE_MUTEX(psinfo_lock);
82 struct pstore_info *psinfo;
83
84 static char *backend;
85 module_param(backend, charp, 0444);
86 MODULE_PARM_DESC(backend, "specific backend to use");
87
88 static char *compress =
89 #ifdef CONFIG_PSTORE_COMPRESS_DEFAULT
90 CONFIG_PSTORE_COMPRESS_DEFAULT;
91 #else
92 NULL;
93 #endif
94 module_param(compress, charp, 0444);
95 MODULE_PARM_DESC(compress, "compression to use");
96
97 /* Compression parameters */
98 static struct crypto_comp *tfm;
99
100 struct pstore_zbackend {
101 int (*zbufsize)(size_t size);
102 const char *name;
103 };
104
105 static char *big_oops_buf;
106 static size_t big_oops_buf_sz;
107
108 /* How much of the console log to snapshot */
109 unsigned long kmsg_bytes = PSTORE_DEFAULT_KMSG_BYTES;
110
pstore_set_kmsg_bytes(int bytes)111 void pstore_set_kmsg_bytes(int bytes)
112 {
113 kmsg_bytes = bytes;
114 }
115
116 /* Tag each group of saved records with a sequence number */
117 static int oopscount;
118
pstore_type_to_name(enum pstore_type_id type)119 const char *pstore_type_to_name(enum pstore_type_id type)
120 {
121 BUILD_BUG_ON(ARRAY_SIZE(pstore_type_names) != PSTORE_TYPE_MAX);
122
123 if (WARN_ON_ONCE(type >= PSTORE_TYPE_MAX))
124 return "unknown";
125
126 return pstore_type_names[type];
127 }
128 EXPORT_SYMBOL_GPL(pstore_type_to_name);
129
pstore_name_to_type(const char * name)130 enum pstore_type_id pstore_name_to_type(const char *name)
131 {
132 int i;
133
134 for (i = 0; i < PSTORE_TYPE_MAX; i++) {
135 if (!strcmp(pstore_type_names[i], name))
136 return i;
137 }
138
139 return PSTORE_TYPE_MAX;
140 }
141 EXPORT_SYMBOL_GPL(pstore_name_to_type);
142
pstore_timer_kick(void)143 static void pstore_timer_kick(void)
144 {
145 if (pstore_update_ms < 0)
146 return;
147
148 mod_timer(&pstore_timer, jiffies + msecs_to_jiffies(pstore_update_ms));
149 }
150
pstore_cannot_block_path(enum kmsg_dump_reason reason)151 static bool pstore_cannot_block_path(enum kmsg_dump_reason reason)
152 {
153 /*
154 * In case of NMI path, pstore shouldn't be blocked
155 * regardless of reason.
156 */
157 if (in_nmi())
158 return true;
159
160 switch (reason) {
161 /* In panic case, other cpus are stopped by smp_send_stop(). */
162 case KMSG_DUMP_PANIC:
163 /*
164 * Emergency restart shouldn't be blocked by spinning on
165 * pstore_info::buf_lock.
166 */
167 case KMSG_DUMP_EMERG:
168 return true;
169 default:
170 return false;
171 }
172 }
173
174 #if IS_ENABLED(CONFIG_PSTORE_DEFLATE_COMPRESS)
zbufsize_deflate(size_t size)175 static int zbufsize_deflate(size_t size)
176 {
177 size_t cmpr;
178
179 switch (size) {
180 /* buffer range for efivars */
181 case 1000 ... 2000:
182 cmpr = 56;
183 break;
184 case 2001 ... 3000:
185 cmpr = 54;
186 break;
187 case 3001 ... 3999:
188 cmpr = 52;
189 break;
190 /* buffer range for nvram, erst */
191 case 4000 ... 10000:
192 cmpr = 45;
193 break;
194 default:
195 cmpr = 60;
196 break;
197 }
198
199 return (size * 100) / cmpr;
200 }
201 #endif
202
203 #if IS_ENABLED(CONFIG_PSTORE_LZO_COMPRESS)
zbufsize_lzo(size_t size)204 static int zbufsize_lzo(size_t size)
205 {
206 return lzo1x_worst_compress(size);
207 }
208 #endif
209
210 #if IS_ENABLED(CONFIG_PSTORE_LZ4_COMPRESS) || IS_ENABLED(CONFIG_PSTORE_LZ4HC_COMPRESS)
zbufsize_lz4(size_t size)211 static int zbufsize_lz4(size_t size)
212 {
213 return LZ4_compressBound(size);
214 }
215 #endif
216
217 #if IS_ENABLED(CONFIG_PSTORE_842_COMPRESS)
zbufsize_842(size_t size)218 static int zbufsize_842(size_t size)
219 {
220 return size;
221 }
222 #endif
223
224 #if IS_ENABLED(CONFIG_PSTORE_ZSTD_COMPRESS)
zbufsize_zstd(size_t size)225 static int zbufsize_zstd(size_t size)
226 {
227 return ZSTD_compressBound(size);
228 }
229 #endif
230
231 static const struct pstore_zbackend *zbackend __ro_after_init;
232
233 static const struct pstore_zbackend zbackends[] = {
234 #if IS_ENABLED(CONFIG_PSTORE_DEFLATE_COMPRESS)
235 {
236 .zbufsize = zbufsize_deflate,
237 .name = "deflate",
238 },
239 #endif
240 #if IS_ENABLED(CONFIG_PSTORE_LZO_COMPRESS)
241 {
242 .zbufsize = zbufsize_lzo,
243 .name = "lzo",
244 },
245 #endif
246 #if IS_ENABLED(CONFIG_PSTORE_LZ4_COMPRESS)
247 {
248 .zbufsize = zbufsize_lz4,
249 .name = "lz4",
250 },
251 #endif
252 #if IS_ENABLED(CONFIG_PSTORE_LZ4HC_COMPRESS)
253 {
254 .zbufsize = zbufsize_lz4,
255 .name = "lz4hc",
256 },
257 #endif
258 #if IS_ENABLED(CONFIG_PSTORE_842_COMPRESS)
259 {
260 .zbufsize = zbufsize_842,
261 .name = "842",
262 },
263 #endif
264 #if IS_ENABLED(CONFIG_PSTORE_ZSTD_COMPRESS)
265 {
266 .zbufsize = zbufsize_zstd,
267 .name = "zstd",
268 },
269 #endif
270 { }
271 };
272
pstore_compress(const void * in,void * out,unsigned int inlen,unsigned int outlen)273 static int pstore_compress(const void *in, void *out,
274 unsigned int inlen, unsigned int outlen)
275 {
276 int ret;
277
278 if (!IS_ENABLED(CONFIG_PSTORE_COMPRESS))
279 return -EINVAL;
280
281 ret = crypto_comp_compress(tfm, in, inlen, out, &outlen);
282 if (ret) {
283 pr_err("crypto_comp_compress failed, ret = %d!\n", ret);
284 return ret;
285 }
286
287 return outlen;
288 }
289
allocate_buf_for_compression(void)290 static void allocate_buf_for_compression(void)
291 {
292 struct crypto_comp *ctx;
293 int size;
294 char *buf;
295
296 /* Skip if not built-in or compression backend not selected yet. */
297 if (!IS_ENABLED(CONFIG_PSTORE_COMPRESS) || !zbackend)
298 return;
299
300 /* Skip if no pstore backend yet or compression init already done. */
301 if (!psinfo || tfm)
302 return;
303
304 if (!crypto_has_comp(zbackend->name, 0, 0)) {
305 pr_err("Unknown compression: %s\n", zbackend->name);
306 return;
307 }
308
309 size = zbackend->zbufsize(psinfo->bufsize);
310 if (size <= 0) {
311 pr_err("Invalid compression size for %s: %d\n",
312 zbackend->name, size);
313 return;
314 }
315
316 buf = kmalloc(size, GFP_KERNEL);
317 if (!buf) {
318 pr_err("Failed %d byte compression buffer allocation for: %s\n",
319 size, zbackend->name);
320 return;
321 }
322
323 ctx = crypto_alloc_comp(zbackend->name, 0, 0);
324 if (IS_ERR_OR_NULL(ctx)) {
325 kfree(buf);
326 pr_err("crypto_alloc_comp('%s') failed: %ld\n", zbackend->name,
327 PTR_ERR(ctx));
328 return;
329 }
330
331 /* A non-NULL big_oops_buf indicates compression is available. */
332 tfm = ctx;
333 big_oops_buf_sz = size;
334 big_oops_buf = buf;
335
336 pr_info("Using crash dump compression: %s\n", zbackend->name);
337 }
338
free_buf_for_compression(void)339 static void free_buf_for_compression(void)
340 {
341 if (IS_ENABLED(CONFIG_PSTORE_COMPRESS) && tfm) {
342 crypto_free_comp(tfm);
343 tfm = NULL;
344 }
345 kfree(big_oops_buf);
346 big_oops_buf = NULL;
347 big_oops_buf_sz = 0;
348 }
349
350 /*
351 * Called when compression fails, since the printk buffer
352 * would be fetched for compression calling it again when
353 * compression fails would have moved the iterator of
354 * printk buffer which results in fetching old contents.
355 * Copy the recent messages from big_oops_buf to psinfo->buf
356 */
copy_kmsg_to_buffer(int hsize,size_t len)357 static size_t copy_kmsg_to_buffer(int hsize, size_t len)
358 {
359 size_t total_len;
360 size_t diff;
361
362 total_len = hsize + len;
363
364 if (total_len > psinfo->bufsize) {
365 diff = total_len - psinfo->bufsize + hsize;
366 memcpy(psinfo->buf, big_oops_buf, hsize);
367 memcpy(psinfo->buf + hsize, big_oops_buf + diff,
368 psinfo->bufsize - hsize);
369 total_len = psinfo->bufsize;
370 } else
371 memcpy(psinfo->buf, big_oops_buf, total_len);
372
373 return total_len;
374 }
375
pstore_record_init(struct pstore_record * record,struct pstore_info * psinfo)376 void pstore_record_init(struct pstore_record *record,
377 struct pstore_info *psinfo)
378 {
379 memset(record, 0, sizeof(*record));
380
381 record->psi = psinfo;
382
383 /* Report zeroed timestamp if called before timekeeping has resumed. */
384 record->time = ns_to_timespec64(ktime_get_real_fast_ns());
385 }
386
387 /*
388 * Store the customised fault log
389 */
390 #ifdef CONFIG_PSTORE_BLACKBOX
391 #define PSTORE_FLAG "PSTORE"
392 #define CALLSTACK_MAX_ENTRIES 20
dump_stacktrace(char * pbuf,size_t buf_size,bool is_panic)393 static void dump_stacktrace(char *pbuf, size_t buf_size, bool is_panic)
394 {
395 int i;
396 size_t stack_len = 0;
397 size_t com_len = 0;
398 unsigned long entries[CALLSTACK_MAX_ENTRIES];
399 unsigned int nr_entries;
400 char tmp_buf[ERROR_DESC_MAX_LEN];
401 bool find_panic = false;
402
403 if (unlikely(!pbuf || !buf_size))
404 return;
405
406 memset(pbuf, 0, buf_size);
407 memset(tmp_buf, 0, sizeof(tmp_buf));
408 nr_entries = stack_trace_save(entries, ARRAY_SIZE(entries), 0);
409 com_len = scnprintf(pbuf, buf_size, "Comm:%s,CPU:%d,Stack:",
410 current->comm, raw_smp_processor_id());
411 for (i = 0; i < nr_entries; i++) {
412 if (stack_len >= sizeof(tmp_buf)) {
413 tmp_buf[sizeof(tmp_buf) - 1] = '\0';
414 break;
415 }
416 stack_len += scnprintf(tmp_buf + stack_len, sizeof(tmp_buf) - stack_len,
417 "%pS-", (void *)entries[i]);
418 if (!find_panic && is_panic) {
419 if (strncmp(tmp_buf, "panic", strlen("panic")) == 0)
420 find_panic = true;
421 else
422 (void)memset(tmp_buf, 0, sizeof(tmp_buf));
423 }
424 }
425 if (com_len >= buf_size)
426 return;
427 stack_len = min(buf_size - com_len, strlen(tmp_buf));
428 memcpy(pbuf + com_len, tmp_buf, stack_len);
429 *(pbuf + buf_size - 1) = '\0';
430 }
431
pstore_blackbox_dump(struct kmsg_dumper * dumper,enum kmsg_dump_reason reason)432 void pstore_blackbox_dump(struct kmsg_dumper *dumper, enum kmsg_dump_reason reason)
433 {
434 struct fault_log_info *pfault_log_info;
435 struct pstore_record record;
436 size_t dst_size;
437 const char *why;
438 char *dst;
439 unsigned long flags = 0;
440 int ret;
441
442 #if defined(CONFIG_PSTORE_BLK) || defined(CONFIG_PSTORE_RAM)
443 if (!pstore_ready)
444 return;
445 #endif
446
447 why = kmsg_dump_reason_str(reason);
448
449 if (pstore_cannot_block_path(reason)) {
450 if (!spin_trylock_irqsave(&psinfo->buf_lock, flags)) {
451 pr_err("dump skipped in %s path because of concurrent dump\n",
452 in_nmi() ? "NMI" : why);
453 return;
454 }
455 } else {
456 spin_lock_irqsave(&psinfo->buf_lock, flags);
457 }
458
459 pfault_log_info = (struct fault_log_info *)psinfo->buf;
460
461 memset(pfault_log_info, 0, sizeof(*pfault_log_info));
462
463 pstore_record_init(&record, psinfo);
464
465 record.type = PSTORE_TYPE_BLACKBOX;
466 record.reason = reason;
467
468 memcpy(pfault_log_info->flag, LOG_FLAG, strlen(LOG_FLAG));
469 strncpy(pfault_log_info->info.event, why,
470 min(strlen(why), sizeof(pfault_log_info->info.event) - 1));
471 strncpy(pfault_log_info->info.module, PSTORE_FLAG,
472 min(strlen(PSTORE_FLAG), sizeof(pfault_log_info->info.module) - 1));
473 get_timestamp(pfault_log_info->info.error_time, TIMESTAMP_MAX_LEN);
474 dump_stacktrace(pfault_log_info->info.error_desc, sizeof(pfault_log_info->info.error_desc), false);
475
476 record.buf = psinfo->buf;
477
478 dst = psinfo->buf;
479 dst_size = psinfo->bufsize;
480
481 dst_size -= sizeof(struct fault_log_info);
482
483 (void)kmsg_dump_get_buffer(dumper, true, dst + sizeof(struct fault_log_info), dst_size,
484 &(pfault_log_info->len));
485
486 record.size = sizeof(struct fault_log_info) + pfault_log_info->len;
487 ret = psinfo->write(&record);
488
489 spin_unlock_irqrestore(&psinfo->buf_lock, flags);
490 }
491 EXPORT_SYMBOL_GPL(pstore_blackbox_dump);
492 #endif
493
494 /*
495 * callback from kmsg_dump. Save as much as we can (up to kmsg_bytes) from the
496 * end of the buffer.
497 */
pstore_dump(struct kmsg_dumper * dumper,enum kmsg_dump_reason reason)498 static void pstore_dump(struct kmsg_dumper *dumper,
499 enum kmsg_dump_reason reason)
500 {
501 unsigned long total = 0;
502 const char *why;
503 unsigned int part = 1;
504 unsigned long flags = 0;
505 int ret;
506
507 why = kmsg_dump_reason_str(reason);
508
509 if (pstore_cannot_block_path(reason)) {
510 if (!spin_trylock_irqsave(&psinfo->buf_lock, flags)) {
511 pr_err("dump skipped in %s path because of concurrent dump\n",
512 in_nmi() ? "NMI" : why);
513 return;
514 }
515 } else {
516 spin_lock_irqsave(&psinfo->buf_lock, flags);
517 }
518
519 oopscount++;
520 while (total < kmsg_bytes) {
521 char *dst;
522 size_t dst_size;
523 int header_size;
524 int zipped_len = -1;
525 size_t dump_size;
526 struct pstore_record record;
527
528 pstore_record_init(&record, psinfo);
529 record.type = PSTORE_TYPE_DMESG;
530 record.count = oopscount;
531 record.reason = reason;
532 record.part = part;
533 record.buf = psinfo->buf;
534
535 if (big_oops_buf) {
536 dst = big_oops_buf;
537 dst_size = big_oops_buf_sz;
538 } else {
539 dst = psinfo->buf;
540 dst_size = psinfo->bufsize;
541 }
542
543 /* Write dump header. */
544 header_size = snprintf(dst, dst_size, "%s#%d Part%u\n", why,
545 oopscount, part);
546 dst_size -= header_size;
547
548 /* Write dump contents. */
549 if (!kmsg_dump_get_buffer(dumper, true, dst + header_size,
550 dst_size, &dump_size))
551 break;
552
553 if (big_oops_buf) {
554 zipped_len = pstore_compress(dst, psinfo->buf,
555 header_size + dump_size,
556 psinfo->bufsize);
557
558 if (zipped_len > 0) {
559 record.compressed = true;
560 record.size = zipped_len;
561 } else {
562 record.size = copy_kmsg_to_buffer(header_size,
563 dump_size);
564 }
565 } else {
566 record.size = header_size + dump_size;
567 }
568
569 ret = psinfo->write(&record);
570 if (ret == 0 && reason == KMSG_DUMP_OOPS) {
571 pstore_new_entry = 1;
572 pstore_timer_kick();
573 }
574
575 total += record.size;
576 part++;
577 }
578
579 spin_unlock_irqrestore(&psinfo->buf_lock, flags);
580 }
581
582 static struct kmsg_dumper pstore_dumper = {
583 .dump = pstore_dump,
584 };
585
586 /*
587 * Register with kmsg_dump to save last part of console log on panic.
588 */
pstore_register_kmsg(void)589 static void pstore_register_kmsg(void)
590 {
591 kmsg_dump_register(&pstore_dumper);
592 }
593
pstore_unregister_kmsg(void)594 static void pstore_unregister_kmsg(void)
595 {
596 kmsg_dump_unregister(&pstore_dumper);
597 }
598
599 #ifdef CONFIG_PSTORE_CONSOLE
pstore_console_write(struct console * con,const char * s,unsigned c)600 static void pstore_console_write(struct console *con, const char *s, unsigned c)
601 {
602 struct pstore_record record;
603
604 if (!c)
605 return;
606
607 pstore_record_init(&record, psinfo);
608 record.type = PSTORE_TYPE_CONSOLE;
609
610 record.buf = (char *)s;
611 record.size = c;
612 psinfo->write(&record);
613 }
614
615 static struct console pstore_console = {
616 .write = pstore_console_write,
617 .index = -1,
618 };
619
pstore_register_console(void)620 static void pstore_register_console(void)
621 {
622 /* Show which backend is going to get console writes. */
623 strscpy(pstore_console.name, psinfo->name,
624 sizeof(pstore_console.name));
625 /*
626 * Always initialize flags here since prior unregister_console()
627 * calls may have changed settings (specifically CON_ENABLED).
628 */
629 pstore_console.flags = CON_PRINTBUFFER | CON_ENABLED | CON_ANYTIME;
630 register_console(&pstore_console);
631 }
632
pstore_unregister_console(void)633 static void pstore_unregister_console(void)
634 {
635 unregister_console(&pstore_console);
636 }
637 #else
pstore_register_console(void)638 static void pstore_register_console(void) {}
pstore_unregister_console(void)639 static void pstore_unregister_console(void) {}
640 #endif
641
pstore_write_user_compat(struct pstore_record * record,const char __user * buf)642 static int pstore_write_user_compat(struct pstore_record *record,
643 const char __user *buf)
644 {
645 int ret = 0;
646
647 if (record->buf)
648 return -EINVAL;
649
650 record->buf = memdup_user(buf, record->size);
651 if (IS_ERR(record->buf)) {
652 ret = PTR_ERR(record->buf);
653 goto out;
654 }
655
656 ret = record->psi->write(record);
657
658 kfree(record->buf);
659 out:
660 record->buf = NULL;
661
662 return unlikely(ret < 0) ? ret : record->size;
663 }
664
665 /*
666 * platform specific persistent storage driver registers with
667 * us here. If pstore is already mounted, call the platform
668 * read function right away to populate the file system. If not
669 * then the pstore mount code will call us later to fill out
670 * the file system.
671 */
pstore_register(struct pstore_info * psi)672 int pstore_register(struct pstore_info *psi)
673 {
674 if (backend && strcmp(backend, psi->name)) {
675 pr_warn("ignoring unexpected backend '%s'\n", psi->name);
676 return -EPERM;
677 }
678
679 /* Sanity check flags. */
680 if (!psi->flags) {
681 pr_warn("backend '%s' must support at least one frontend\n",
682 psi->name);
683 return -EINVAL;
684 }
685
686 /* Check for required functions. */
687 if (!psi->read || !psi->write) {
688 pr_warn("backend '%s' must implement read() and write()\n",
689 psi->name);
690 return -EINVAL;
691 }
692
693 mutex_lock(&psinfo_lock);
694 if (psinfo) {
695 pr_warn("backend '%s' already loaded: ignoring '%s'\n",
696 psinfo->name, psi->name);
697 mutex_unlock(&psinfo_lock);
698 return -EBUSY;
699 }
700
701 if (!psi->write_user)
702 psi->write_user = pstore_write_user_compat;
703 psinfo = psi;
704 mutex_init(&psinfo->read_mutex);
705 spin_lock_init(&psinfo->buf_lock);
706
707 if (psi->flags & PSTORE_FLAGS_DMESG)
708 allocate_buf_for_compression();
709
710 pstore_get_records(0);
711
712 if (psi->flags & PSTORE_FLAGS_DMESG) {
713 pstore_dumper.max_reason = psinfo->max_reason;
714 pstore_register_kmsg();
715 }
716 if (psi->flags & PSTORE_FLAGS_CONSOLE)
717 pstore_register_console();
718 if (psi->flags & PSTORE_FLAGS_FTRACE)
719 pstore_register_ftrace();
720 if (psi->flags & PSTORE_FLAGS_PMSG)
721 pstore_register_pmsg();
722
723 /* Start watching for new records, if desired. */
724 pstore_timer_kick();
725
726 /*
727 * Update the module parameter backend, so it is visible
728 * through /sys/module/pstore/parameters/backend
729 */
730 backend = kstrdup(psi->name, GFP_KERNEL);
731
732 pr_info("Registered %s as persistent store backend\n", psi->name);
733
734 mutex_unlock(&psinfo_lock);
735 return 0;
736 }
737 EXPORT_SYMBOL_GPL(pstore_register);
738
pstore_unregister(struct pstore_info * psi)739 void pstore_unregister(struct pstore_info *psi)
740 {
741 /* It's okay to unregister nothing. */
742 if (!psi)
743 return;
744
745 mutex_lock(&psinfo_lock);
746
747 /* Only one backend can be registered at a time. */
748 if (WARN_ON(psi != psinfo)) {
749 mutex_unlock(&psinfo_lock);
750 return;
751 }
752
753 /* Unregister all callbacks. */
754 if (psi->flags & PSTORE_FLAGS_PMSG)
755 pstore_unregister_pmsg();
756 if (psi->flags & PSTORE_FLAGS_FTRACE)
757 pstore_unregister_ftrace();
758 if (psi->flags & PSTORE_FLAGS_CONSOLE)
759 pstore_unregister_console();
760 if (psi->flags & PSTORE_FLAGS_DMESG)
761 pstore_unregister_kmsg();
762
763 /* Stop timer and make sure all work has finished. */
764 del_timer_sync(&pstore_timer);
765 flush_work(&pstore_work);
766
767 /* Remove all backend records from filesystem tree. */
768 pstore_put_backend_records(psi);
769
770 free_buf_for_compression();
771
772 psinfo = NULL;
773 kfree(backend);
774 backend = NULL;
775 mutex_unlock(&psinfo_lock);
776 }
777 EXPORT_SYMBOL_GPL(pstore_unregister);
778
decompress_record(struct pstore_record * record)779 static void decompress_record(struct pstore_record *record)
780 {
781 int ret;
782 int unzipped_len;
783 char *unzipped, *workspace;
784
785 if (!IS_ENABLED(CONFIG_PSTORE_COMPRESS) || !record->compressed)
786 return;
787
788 /* Only PSTORE_TYPE_DMESG support compression. */
789 if (record->type != PSTORE_TYPE_DMESG) {
790 pr_warn("ignored compressed record type %d\n", record->type);
791 return;
792 }
793
794 /* Missing compression buffer means compression was not initialized. */
795 if (!big_oops_buf) {
796 pr_warn("no decompression method initialized!\n");
797 return;
798 }
799
800 /* Allocate enough space to hold max decompression and ECC. */
801 unzipped_len = big_oops_buf_sz;
802 workspace = kmalloc(unzipped_len + record->ecc_notice_size,
803 GFP_KERNEL);
804 if (!workspace)
805 return;
806
807 /* After decompression "unzipped_len" is almost certainly smaller. */
808 ret = crypto_comp_decompress(tfm, record->buf, record->size,
809 workspace, &unzipped_len);
810 if (ret) {
811 pr_err("crypto_comp_decompress failed, ret = %d!\n", ret);
812 kfree(workspace);
813 return;
814 }
815
816 /* Append ECC notice to decompressed buffer. */
817 memcpy(workspace + unzipped_len, record->buf + record->size,
818 record->ecc_notice_size);
819
820 /* Copy decompressed contents into an minimum-sized allocation. */
821 unzipped = kmemdup(workspace, unzipped_len + record->ecc_notice_size,
822 GFP_KERNEL);
823 kfree(workspace);
824 if (!unzipped)
825 return;
826
827 /* Swap out compressed contents with decompressed contents. */
828 kfree(record->buf);
829 record->buf = unzipped;
830 record->size = unzipped_len;
831 record->compressed = false;
832 }
833
834 /*
835 * Read all the records from one persistent store backend. Create
836 * files in our filesystem. Don't warn about -EEXIST errors
837 * when we are re-scanning the backing store looking to add new
838 * error records.
839 */
pstore_get_backend_records(struct pstore_info * psi,struct dentry * root,int quiet)840 void pstore_get_backend_records(struct pstore_info *psi,
841 struct dentry *root, int quiet)
842 {
843 int failed = 0;
844 unsigned int stop_loop = 65536;
845
846 if (!psi || !root)
847 return;
848
849 mutex_lock(&psi->read_mutex);
850 if (psi->open && psi->open(psi))
851 goto out;
852
853 /*
854 * Backend callback read() allocates record.buf. decompress_record()
855 * may reallocate record.buf. On success, pstore_mkfile() will keep
856 * the record.buf, so free it only on failure.
857 */
858 for (; stop_loop; stop_loop--) {
859 struct pstore_record *record;
860 int rc;
861
862 record = kzalloc(sizeof(*record), GFP_KERNEL);
863 if (!record) {
864 pr_err("out of memory creating record\n");
865 break;
866 }
867 pstore_record_init(record, psi);
868
869 record->size = psi->read(record);
870
871 /* No more records left in backend? */
872 if (record->size <= 0) {
873 kfree(record);
874 break;
875 }
876
877 decompress_record(record);
878 rc = pstore_mkfile(root, record);
879 if (rc) {
880 /* pstore_mkfile() did not take record, so free it. */
881 kfree(record->buf);
882 kfree(record);
883 if (rc != -EEXIST || !quiet)
884 failed++;
885 }
886 }
887 if (psi->close)
888 psi->close(psi);
889 out:
890 mutex_unlock(&psi->read_mutex);
891
892 if (failed)
893 pr_warn("failed to create %d record(s) from '%s'\n",
894 failed, psi->name);
895 if (!stop_loop)
896 pr_err("looping? Too many records seen from '%s'\n",
897 psi->name);
898 }
899
pstore_dowork(struct work_struct * work)900 static void pstore_dowork(struct work_struct *work)
901 {
902 pstore_get_records(1);
903 }
904
pstore_timefunc(struct timer_list * unused)905 static void pstore_timefunc(struct timer_list *unused)
906 {
907 if (pstore_new_entry) {
908 pstore_new_entry = 0;
909 schedule_work(&pstore_work);
910 }
911
912 pstore_timer_kick();
913 }
914
pstore_choose_compression(void)915 static void __init pstore_choose_compression(void)
916 {
917 const struct pstore_zbackend *step;
918
919 if (!compress)
920 return;
921
922 for (step = zbackends; step->name; step++) {
923 if (!strcmp(compress, step->name)) {
924 zbackend = step;
925 return;
926 }
927 }
928 }
929
pstore_init(void)930 static int __init pstore_init(void)
931 {
932 int ret;
933
934 pstore_choose_compression();
935
936 /*
937 * Check if any pstore backends registered earlier but did not
938 * initialize compression because crypto was not ready. If so,
939 * initialize compression now.
940 */
941 allocate_buf_for_compression();
942
943 ret = pstore_init_fs();
944 if (ret)
945 free_buf_for_compression();
946
947 return ret;
948 }
949 late_initcall(pstore_init);
950
pstore_exit(void)951 static void __exit pstore_exit(void)
952 {
953 pstore_exit_fs();
954 }
955 module_exit(pstore_exit)
956
957 MODULE_AUTHOR("Tony Luck <tony.luck@intel.com>");
958 MODULE_LICENSE("GPL");
959