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