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