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 int kmsg_bytes = CONFIG_PSTORE_DEFAULT_KMSG_BYTES;
101 module_param(kmsg_bytes, uint, 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(unsigned int bytes)115 void pstore_set_kmsg_bytes(unsigned int bytes)
116 {
117 WRITE_ONCE(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 int remaining = READ_ONCE(kmsg_bytes);
391 unsigned long total = 0;
392 const char *why;
393 unsigned int part = 1;
394 unsigned long flags = 0;
395 int saved_ret = 0;
396 int ret;
397
398 why = kmsg_dump_reason_str(reason);
399
400 if (pstore_cannot_block_path(reason)) {
401 if (!spin_trylock_irqsave(&psinfo->buf_lock, flags)) {
402 pr_err("dump skipped in %s path because of concurrent dump\n",
403 in_nmi() ? "NMI" : why);
404 return;
405 }
406 } else {
407 spin_lock_irqsave(&psinfo->buf_lock, flags);
408 }
409
410 kmsg_dump_rewind(&iter);
411
412 oopscount++;
413 while (total < remaining) {
414 char *dst;
415 size_t dst_size;
416 int header_size;
417 int zipped_len = -1;
418 size_t dump_size;
419 struct pstore_record record;
420
421 pstore_record_init(&record, psinfo);
422 record.type = PSTORE_TYPE_DMESG;
423 record.count = oopscount;
424 record.reason = reason;
425 record.part = part;
426 record.buf = psinfo->buf;
427
428 dst = big_oops_buf ?: psinfo->buf;
429 dst_size = max_compressed_size ?: psinfo->bufsize;
430
431 /* Write dump header. */
432 header_size = snprintf(dst, dst_size, "%s#%d Part%u\n", why,
433 oopscount, part);
434 dst_size -= header_size;
435
436 /* Write dump contents. */
437 if (!kmsg_dump_get_buffer(&iter, true, dst + header_size,
438 dst_size, &dump_size))
439 break;
440
441 if (big_oops_buf) {
442 zipped_len = pstore_compress(dst, psinfo->buf,
443 header_size + dump_size,
444 psinfo->bufsize);
445
446 if (zipped_len > 0) {
447 record.compressed = true;
448 record.size = zipped_len;
449 } else {
450 /*
451 * Compression failed, so the buffer is most
452 * likely filled with binary data that does not
453 * compress as well as ASCII text. Copy as much
454 * of the uncompressed data as possible into
455 * the pstore record, and discard the rest.
456 */
457 record.size = psinfo->bufsize;
458 memcpy(psinfo->buf, dst, psinfo->bufsize);
459 }
460 } else {
461 record.size = header_size + dump_size;
462 }
463
464 ret = psinfo->write(&record);
465 if (ret == 0 && reason == KMSG_DUMP_OOPS) {
466 pstore_new_entry = 1;
467 pstore_timer_kick();
468 } else {
469 /* Preserve only the first non-zero returned value. */
470 if (!saved_ret)
471 saved_ret = ret;
472 }
473
474 total += record.size;
475 part++;
476 }
477 spin_unlock_irqrestore(&psinfo->buf_lock, flags);
478
479 if (saved_ret) {
480 pr_err_once("backend (%s) writing error (%d)\n", psinfo->name,
481 saved_ret);
482 }
483 }
484
485 static struct kmsg_dumper pstore_dumper = {
486 .dump = pstore_dump,
487 };
488
489 /*
490 * Register with kmsg_dump to save last part of console log on panic.
491 */
pstore_register_kmsg(void)492 static void pstore_register_kmsg(void)
493 {
494 kmsg_dump_register(&pstore_dumper);
495 }
496
pstore_unregister_kmsg(void)497 static void pstore_unregister_kmsg(void)
498 {
499 kmsg_dump_unregister(&pstore_dumper);
500 }
501
502 #ifdef CONFIG_PSTORE_CONSOLE
pstore_console_write(struct console * con,const char * s,unsigned c)503 static void pstore_console_write(struct console *con, const char *s, unsigned c)
504 {
505 struct pstore_record record;
506
507 if (!c)
508 return;
509
510 pstore_record_init(&record, psinfo);
511 record.type = PSTORE_TYPE_CONSOLE;
512
513 record.buf = (char *)s;
514 record.size = c;
515 psinfo->write(&record);
516 }
517
518 static struct console pstore_console = {
519 .write = pstore_console_write,
520 .index = -1,
521 };
522
pstore_register_console(void)523 static void pstore_register_console(void)
524 {
525 /* Show which backend is going to get console writes. */
526 strscpy(pstore_console.name, psinfo->name,
527 sizeof(pstore_console.name));
528 /*
529 * Always initialize flags here since prior unregister_console()
530 * calls may have changed settings (specifically CON_ENABLED).
531 */
532 pstore_console.flags = CON_PRINTBUFFER | CON_ENABLED | CON_ANYTIME;
533 register_console(&pstore_console);
534 }
535
pstore_unregister_console(void)536 static void pstore_unregister_console(void)
537 {
538 unregister_console(&pstore_console);
539 }
540 #else
pstore_register_console(void)541 static void pstore_register_console(void) {}
pstore_unregister_console(void)542 static void pstore_unregister_console(void) {}
543 #endif
544
pstore_write_user_compat(struct pstore_record * record,const char __user * buf)545 static int pstore_write_user_compat(struct pstore_record *record,
546 const char __user *buf)
547 {
548 int ret = 0;
549
550 if (record->buf)
551 return -EINVAL;
552
553 record->buf = vmemdup_user(buf, record->size);
554 if (IS_ERR(record->buf)) {
555 ret = PTR_ERR(record->buf);
556 goto out;
557 }
558
559 ret = record->psi->write(record);
560
561 kvfree(record->buf);
562 out:
563 record->buf = NULL;
564
565 return unlikely(ret < 0) ? ret : record->size;
566 }
567
568 /*
569 * platform specific persistent storage driver registers with
570 * us here. If pstore is already mounted, call the platform
571 * read function right away to populate the file system. If not
572 * then the pstore mount code will call us later to fill out
573 * the file system.
574 */
pstore_register(struct pstore_info * psi)575 int pstore_register(struct pstore_info *psi)
576 {
577 char *new_backend;
578
579 if (backend && strcmp(backend, psi->name)) {
580 pr_warn("backend '%s' already in use: ignoring '%s'\n",
581 backend, psi->name);
582 return -EBUSY;
583 }
584
585 /* Sanity check flags. */
586 if (!psi->flags) {
587 pr_warn("backend '%s' must support at least one frontend\n",
588 psi->name);
589 return -EINVAL;
590 }
591
592 /* Check for required functions. */
593 if (!psi->read || !psi->write) {
594 pr_warn("backend '%s' must implement read() and write()\n",
595 psi->name);
596 return -EINVAL;
597 }
598
599 new_backend = kstrdup(psi->name, GFP_KERNEL);
600 if (!new_backend)
601 return -ENOMEM;
602
603 mutex_lock(&psinfo_lock);
604 if (psinfo) {
605 pr_warn("backend '%s' already loaded: ignoring '%s'\n",
606 psinfo->name, psi->name);
607 mutex_unlock(&psinfo_lock);
608 kfree(new_backend);
609 return -EBUSY;
610 }
611
612 if (!psi->write_user)
613 psi->write_user = pstore_write_user_compat;
614 psinfo = psi;
615 mutex_init(&psinfo->read_mutex);
616 spin_lock_init(&psinfo->buf_lock);
617
618 if (psi->flags & PSTORE_FLAGS_DMESG)
619 allocate_buf_for_compression();
620
621 pstore_get_records(0);
622
623 if (psi->flags & PSTORE_FLAGS_DMESG) {
624 pstore_dumper.max_reason = psinfo->max_reason;
625 pstore_register_kmsg();
626 }
627 if (psi->flags & PSTORE_FLAGS_CONSOLE)
628 pstore_register_console();
629 if (psi->flags & PSTORE_FLAGS_FTRACE)
630 pstore_register_ftrace();
631 if (psi->flags & PSTORE_FLAGS_PMSG)
632 pstore_register_pmsg();
633
634 /* Start watching for new records, if desired. */
635 pstore_timer_kick();
636
637 /*
638 * Update the module parameter backend, so it is visible
639 * through /sys/module/pstore/parameters/backend
640 */
641 backend = new_backend;
642
643 pr_info("Registered %s as persistent store backend\n", psi->name);
644
645 mutex_unlock(&psinfo_lock);
646 return 0;
647 }
648 EXPORT_SYMBOL_GPL(pstore_register);
649
pstore_unregister(struct pstore_info * psi)650 void pstore_unregister(struct pstore_info *psi)
651 {
652 /* It's okay to unregister nothing. */
653 if (!psi)
654 return;
655
656 mutex_lock(&psinfo_lock);
657
658 /* Only one backend can be registered at a time. */
659 if (WARN_ON(psi != psinfo)) {
660 mutex_unlock(&psinfo_lock);
661 return;
662 }
663
664 /* Unregister all callbacks. */
665 if (psi->flags & PSTORE_FLAGS_PMSG)
666 pstore_unregister_pmsg();
667 if (psi->flags & PSTORE_FLAGS_FTRACE)
668 pstore_unregister_ftrace();
669 if (psi->flags & PSTORE_FLAGS_CONSOLE)
670 pstore_unregister_console();
671 if (psi->flags & PSTORE_FLAGS_DMESG)
672 pstore_unregister_kmsg();
673
674 /* Stop timer and make sure all work has finished. */
675 del_timer_sync(&pstore_timer);
676 flush_work(&pstore_work);
677
678 /* Remove all backend records from filesystem tree. */
679 pstore_put_backend_records(psi);
680
681 free_buf_for_compression();
682
683 psinfo = NULL;
684 kfree(backend);
685 backend = NULL;
686
687 pr_info("Unregistered %s as persistent store backend\n", psi->name);
688 mutex_unlock(&psinfo_lock);
689 }
690 EXPORT_SYMBOL_GPL(pstore_unregister);
691
decompress_record(struct pstore_record * record,struct z_stream_s * zstream)692 static void decompress_record(struct pstore_record *record,
693 struct z_stream_s *zstream)
694 {
695 int ret;
696 int unzipped_len;
697 char *unzipped, *workspace;
698 size_t max_uncompressed_size;
699
700 if (!IS_ENABLED(CONFIG_PSTORE_COMPRESS) || !record->compressed)
701 return;
702
703 /* Only PSTORE_TYPE_DMESG support compression. */
704 if (record->type != PSTORE_TYPE_DMESG) {
705 pr_warn("ignored compressed record type %d\n", record->type);
706 return;
707 }
708
709 /* Missing compression buffer means compression was not initialized. */
710 if (!zstream->workspace) {
711 pr_warn("no decompression method initialized!\n");
712 return;
713 }
714
715 ret = zlib_inflateReset(zstream);
716 if (ret != Z_OK) {
717 pr_err("zlib_inflateReset() failed, ret = %d!\n", ret);
718 return;
719 }
720
721 /* Allocate enough space to hold max decompression and ECC. */
722 max_uncompressed_size = 3 * psinfo->bufsize;
723 workspace = kvzalloc(max_uncompressed_size + record->ecc_notice_size,
724 GFP_KERNEL);
725 if (!workspace)
726 return;
727
728 zstream->next_in = record->buf;
729 zstream->avail_in = record->size;
730 zstream->next_out = workspace;
731 zstream->avail_out = max_uncompressed_size;
732
733 ret = zlib_inflate(zstream, Z_FINISH);
734 if (ret != Z_STREAM_END) {
735 pr_err_ratelimited("zlib_inflate() failed, ret = %d!\n", ret);
736 kvfree(workspace);
737 return;
738 }
739
740 unzipped_len = zstream->total_out;
741
742 /* Append ECC notice to decompressed buffer. */
743 memcpy(workspace + unzipped_len, record->buf + record->size,
744 record->ecc_notice_size);
745
746 /* Copy decompressed contents into an minimum-sized allocation. */
747 unzipped = kvmemdup(workspace, unzipped_len + record->ecc_notice_size,
748 GFP_KERNEL);
749 kvfree(workspace);
750 if (!unzipped)
751 return;
752
753 /* Swap out compressed contents with decompressed contents. */
754 kvfree(record->buf);
755 record->buf = unzipped;
756 record->size = unzipped_len;
757 record->compressed = false;
758 }
759
760 /*
761 * Read all the records from one persistent store backend. Create
762 * files in our filesystem. Don't warn about -EEXIST errors
763 * when we are re-scanning the backing store looking to add new
764 * error records.
765 */
pstore_get_backend_records(struct pstore_info * psi,struct dentry * root,int quiet)766 void pstore_get_backend_records(struct pstore_info *psi,
767 struct dentry *root, int quiet)
768 {
769 int failed = 0;
770 unsigned int stop_loop = 65536;
771 struct z_stream_s zstream = {};
772
773 if (!psi || !root)
774 return;
775
776 if (IS_ENABLED(CONFIG_PSTORE_COMPRESS) && compress) {
777 zstream.workspace = kvmalloc(zlib_inflate_workspacesize(),
778 GFP_KERNEL);
779 zlib_inflateInit2(&zstream, -DEF_WBITS);
780 }
781
782 mutex_lock(&psi->read_mutex);
783 if (psi->open && psi->open(psi))
784 goto out;
785
786 /*
787 * Backend callback read() allocates record.buf. decompress_record()
788 * may reallocate record.buf. On success, pstore_mkfile() will keep
789 * the record.buf, so free it only on failure.
790 */
791 for (; stop_loop; stop_loop--) {
792 struct pstore_record *record;
793 int rc;
794
795 record = kzalloc(sizeof(*record), GFP_KERNEL);
796 if (!record) {
797 pr_err("out of memory creating record\n");
798 break;
799 }
800 pstore_record_init(record, psi);
801
802 record->size = psi->read(record);
803
804 /* No more records left in backend? */
805 if (record->size <= 0) {
806 kfree(record);
807 break;
808 }
809
810 decompress_record(record, &zstream);
811 rc = pstore_mkfile(root, record);
812 if (rc) {
813 /* pstore_mkfile() did not take record, so free it. */
814 kvfree(record->buf);
815 kfree(record->priv);
816 kfree(record);
817 if (rc != -EEXIST || !quiet)
818 failed++;
819 }
820 }
821 if (psi->close)
822 psi->close(psi);
823 out:
824 mutex_unlock(&psi->read_mutex);
825
826 if (IS_ENABLED(CONFIG_PSTORE_COMPRESS) && compress) {
827 if (zlib_inflateEnd(&zstream) != Z_OK)
828 pr_warn("zlib_inflateEnd() failed\n");
829 kvfree(zstream.workspace);
830 }
831
832 if (failed)
833 pr_warn("failed to create %d record(s) from '%s'\n",
834 failed, psi->name);
835 if (!stop_loop)
836 pr_err("looping? Too many records seen from '%s'\n",
837 psi->name);
838 }
839
pstore_dowork(struct work_struct * work)840 static void pstore_dowork(struct work_struct *work)
841 {
842 pstore_get_records(1);
843 }
844
pstore_timefunc(struct timer_list * unused)845 static void pstore_timefunc(struct timer_list *unused)
846 {
847 if (pstore_new_entry) {
848 pstore_new_entry = 0;
849 schedule_work(&pstore_work);
850 }
851
852 pstore_timer_kick();
853 }
854
pstore_init(void)855 static int __init pstore_init(void)
856 {
857 int ret;
858
859 ret = pstore_init_fs();
860 if (ret)
861 free_buf_for_compression();
862
863 return ret;
864 }
865 late_initcall(pstore_init);
866
pstore_exit(void)867 static void __exit pstore_exit(void)
868 {
869 pstore_exit_fs();
870 }
871 module_exit(pstore_exit)
872
873 MODULE_AUTHOR("Tony Luck <tony.luck@intel.com>");
874 MODULE_LICENSE("GPL");
875