xref: /openbmc/linux/fs/pstore/platform.c (revision a9fb94a99bb515d8720ba8440ce3aba84aec80f8)
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 		tfm = NULL;
353 	}
354 	kfree(big_oops_buf);
355 	big_oops_buf = NULL;
356 	big_oops_buf_sz = 0;
357 }
358 
359 /*
360  * Called when compression fails, since the printk buffer
361  * would be fetched for compression calling it again when
362  * compression fails would have moved the iterator of
363  * printk buffer which results in fetching old contents.
364  * Copy the recent messages from big_oops_buf to psinfo->buf
365  */
366 static size_t copy_kmsg_to_buffer(int hsize, size_t len)
367 {
368 	size_t total_len;
369 	size_t diff;
370 
371 	total_len = hsize + len;
372 
373 	if (total_len > psinfo->bufsize) {
374 		diff = total_len - psinfo->bufsize + hsize;
375 		memcpy(psinfo->buf, big_oops_buf, hsize);
376 		memcpy(psinfo->buf + hsize, big_oops_buf + diff,
377 					psinfo->bufsize - hsize);
378 		total_len = psinfo->bufsize;
379 	} else
380 		memcpy(psinfo->buf, big_oops_buf, total_len);
381 
382 	return total_len;
383 }
384 
385 void pstore_record_init(struct pstore_record *record,
386 			struct pstore_info *psinfo)
387 {
388 	memset(record, 0, sizeof(*record));
389 
390 	record->psi = psinfo;
391 
392 	/* Report zeroed timestamp if called before timekeeping has resumed. */
393 	record->time = ns_to_timespec64(ktime_get_real_fast_ns());
394 }
395 
396 /*
397  * callback from kmsg_dump. Save as much as we can (up to kmsg_bytes) from the
398  * end of the buffer.
399  */
400 static void pstore_dump(struct kmsg_dumper *dumper,
401 			enum kmsg_dump_reason reason)
402 {
403 	unsigned long	total = 0;
404 	const char	*why;
405 	unsigned int	part = 1;
406 	int		ret;
407 
408 	why = get_reason_str(reason);
409 
410 	if (down_trylock(&psinfo->buf_lock)) {
411 		/* Failed to acquire lock: give up if we cannot wait. */
412 		if (pstore_cannot_wait(reason)) {
413 			pr_err("dump skipped in %s path: may corrupt error record\n",
414 				in_nmi() ? "NMI" : why);
415 			return;
416 		}
417 		if (down_interruptible(&psinfo->buf_lock)) {
418 			pr_err("could not grab semaphore?!\n");
419 			return;
420 		}
421 	}
422 
423 	oopscount++;
424 	while (total < kmsg_bytes) {
425 		char *dst;
426 		size_t dst_size;
427 		int header_size;
428 		int zipped_len = -1;
429 		size_t dump_size;
430 		struct pstore_record record;
431 
432 		pstore_record_init(&record, psinfo);
433 		record.type = PSTORE_TYPE_DMESG;
434 		record.count = oopscount;
435 		record.reason = reason;
436 		record.part = part;
437 		record.buf = psinfo->buf;
438 
439 		if (big_oops_buf) {
440 			dst = big_oops_buf;
441 			dst_size = big_oops_buf_sz;
442 		} else {
443 			dst = psinfo->buf;
444 			dst_size = psinfo->bufsize;
445 		}
446 
447 		/* Write dump header. */
448 		header_size = snprintf(dst, dst_size, "%s#%d Part%u\n", why,
449 				 oopscount, part);
450 		dst_size -= header_size;
451 
452 		/* Write dump contents. */
453 		if (!kmsg_dump_get_buffer(dumper, true, dst + header_size,
454 					  dst_size, &dump_size))
455 			break;
456 
457 		if (big_oops_buf) {
458 			zipped_len = pstore_compress(dst, psinfo->buf,
459 						header_size + dump_size,
460 						psinfo->bufsize);
461 
462 			if (zipped_len > 0) {
463 				record.compressed = true;
464 				record.size = zipped_len;
465 			} else {
466 				record.size = copy_kmsg_to_buffer(header_size,
467 								  dump_size);
468 			}
469 		} else {
470 			record.size = header_size + dump_size;
471 		}
472 
473 		ret = psinfo->write(&record);
474 		if (ret == 0 && reason == KMSG_DUMP_OOPS && pstore_is_mounted())
475 			pstore_new_entry = 1;
476 
477 		total += record.size;
478 		part++;
479 	}
480 
481 	up(&psinfo->buf_lock);
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  */
491 static void pstore_register_kmsg(void)
492 {
493 	kmsg_dump_register(&pstore_dumper);
494 }
495 
496 static void pstore_unregister_kmsg(void)
497 {
498 	kmsg_dump_unregister(&pstore_dumper);
499 }
500 
501 #ifdef CONFIG_PSTORE_CONSOLE
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 	.name	= "pstore",
519 	.write	= pstore_console_write,
520 	.flags	= CON_PRINTBUFFER | CON_ENABLED | CON_ANYTIME,
521 	.index	= -1,
522 };
523 
524 static void pstore_register_console(void)
525 {
526 	register_console(&pstore_console);
527 }
528 
529 static void pstore_unregister_console(void)
530 {
531 	unregister_console(&pstore_console);
532 }
533 #else
534 static void pstore_register_console(void) {}
535 static void pstore_unregister_console(void) {}
536 #endif
537 
538 static int pstore_write_user_compat(struct pstore_record *record,
539 				    const char __user *buf)
540 {
541 	int ret = 0;
542 
543 	if (record->buf)
544 		return -EINVAL;
545 
546 	record->buf = memdup_user(buf, record->size);
547 	if (IS_ERR(record->buf)) {
548 		ret = PTR_ERR(record->buf);
549 		goto out;
550 	}
551 
552 	ret = record->psi->write(record);
553 
554 	kfree(record->buf);
555 out:
556 	record->buf = NULL;
557 
558 	return unlikely(ret < 0) ? ret : record->size;
559 }
560 
561 /*
562  * platform specific persistent storage driver registers with
563  * us here. If pstore is already mounted, call the platform
564  * read function right away to populate the file system. If not
565  * then the pstore mount code will call us later to fill out
566  * the file system.
567  */
568 int pstore_register(struct pstore_info *psi)
569 {
570 	struct module *owner = psi->owner;
571 
572 	if (backend && strcmp(backend, psi->name)) {
573 		pr_warn("ignoring unexpected backend '%s'\n", psi->name);
574 		return -EPERM;
575 	}
576 
577 	/* Sanity check flags. */
578 	if (!psi->flags) {
579 		pr_warn("backend '%s' must support at least one frontend\n",
580 			psi->name);
581 		return -EINVAL;
582 	}
583 
584 	/* Check for required functions. */
585 	if (!psi->read || !psi->write) {
586 		pr_warn("backend '%s' must implement read() and write()\n",
587 			psi->name);
588 		return -EINVAL;
589 	}
590 
591 	spin_lock(&pstore_lock);
592 	if (psinfo) {
593 		pr_warn("backend '%s' already loaded: ignoring '%s'\n",
594 			psinfo->name, psi->name);
595 		spin_unlock(&pstore_lock);
596 		return -EBUSY;
597 	}
598 
599 	if (!psi->write_user)
600 		psi->write_user = pstore_write_user_compat;
601 	psinfo = psi;
602 	mutex_init(&psinfo->read_mutex);
603 	sema_init(&psinfo->buf_lock, 1);
604 	spin_unlock(&pstore_lock);
605 
606 	if (owner && !try_module_get(owner)) {
607 		psinfo = NULL;
608 		return -EINVAL;
609 	}
610 
611 	allocate_buf_for_compression();
612 
613 	if (pstore_is_mounted())
614 		pstore_get_records(0);
615 
616 	if (psi->flags & PSTORE_FLAGS_DMESG)
617 		pstore_register_kmsg();
618 	if (psi->flags & PSTORE_FLAGS_CONSOLE)
619 		pstore_register_console();
620 	if (psi->flags & PSTORE_FLAGS_FTRACE)
621 		pstore_register_ftrace();
622 	if (psi->flags & PSTORE_FLAGS_PMSG)
623 		pstore_register_pmsg();
624 
625 	/* Start watching for new records, if desired. */
626 	if (pstore_update_ms >= 0) {
627 		pstore_timer.expires = jiffies +
628 			msecs_to_jiffies(pstore_update_ms);
629 		add_timer(&pstore_timer);
630 	}
631 
632 	/*
633 	 * Update the module parameter backend, so it is visible
634 	 * through /sys/module/pstore/parameters/backend
635 	 */
636 	backend = psi->name;
637 
638 	pr_info("Registered %s as persistent store backend\n", psi->name);
639 
640 	module_put(owner);
641 
642 	return 0;
643 }
644 EXPORT_SYMBOL_GPL(pstore_register);
645 
646 void pstore_unregister(struct pstore_info *psi)
647 {
648 	/* Stop timer and make sure all work has finished. */
649 	pstore_update_ms = -1;
650 	del_timer_sync(&pstore_timer);
651 	flush_work(&pstore_work);
652 
653 	if (psi->flags & PSTORE_FLAGS_PMSG)
654 		pstore_unregister_pmsg();
655 	if (psi->flags & PSTORE_FLAGS_FTRACE)
656 		pstore_unregister_ftrace();
657 	if (psi->flags & PSTORE_FLAGS_CONSOLE)
658 		pstore_unregister_console();
659 	if (psi->flags & PSTORE_FLAGS_DMESG)
660 		pstore_unregister_kmsg();
661 
662 	free_buf_for_compression();
663 
664 	psinfo = NULL;
665 	backend = NULL;
666 }
667 EXPORT_SYMBOL_GPL(pstore_unregister);
668 
669 static void decompress_record(struct pstore_record *record)
670 {
671 	int ret;
672 	int unzipped_len;
673 	char *unzipped, *workspace;
674 
675 	if (!record->compressed)
676 		return;
677 
678 	/* Only PSTORE_TYPE_DMESG support compression. */
679 	if (record->type != PSTORE_TYPE_DMESG) {
680 		pr_warn("ignored compressed record type %d\n", record->type);
681 		return;
682 	}
683 
684 	/* Missing compression buffer means compression was not initialized. */
685 	if (!big_oops_buf) {
686 		pr_warn("no decompression method initialized!\n");
687 		return;
688 	}
689 
690 	/* Allocate enough space to hold max decompression and ECC. */
691 	unzipped_len = big_oops_buf_sz;
692 	workspace = kmalloc(unzipped_len + record->ecc_notice_size,
693 			    GFP_KERNEL);
694 	if (!workspace)
695 		return;
696 
697 	/* After decompression "unzipped_len" is almost certainly smaller. */
698 	ret = crypto_comp_decompress(tfm, record->buf, record->size,
699 					  workspace, &unzipped_len);
700 	if (ret) {
701 		pr_err("crypto_comp_decompress failed, ret = %d!\n", ret);
702 		kfree(workspace);
703 		return;
704 	}
705 
706 	/* Append ECC notice to decompressed buffer. */
707 	memcpy(workspace + unzipped_len, record->buf + record->size,
708 	       record->ecc_notice_size);
709 
710 	/* Copy decompressed contents into an minimum-sized allocation. */
711 	unzipped = kmemdup(workspace, unzipped_len + record->ecc_notice_size,
712 			   GFP_KERNEL);
713 	kfree(workspace);
714 	if (!unzipped)
715 		return;
716 
717 	/* Swap out compressed contents with decompressed contents. */
718 	kfree(record->buf);
719 	record->buf = unzipped;
720 	record->size = unzipped_len;
721 	record->compressed = false;
722 }
723 
724 /*
725  * Read all the records from one persistent store backend. Create
726  * files in our filesystem.  Don't warn about -EEXIST errors
727  * when we are re-scanning the backing store looking to add new
728  * error records.
729  */
730 void pstore_get_backend_records(struct pstore_info *psi,
731 				struct dentry *root, int quiet)
732 {
733 	int failed = 0;
734 	unsigned int stop_loop = 65536;
735 
736 	if (!psi || !root)
737 		return;
738 
739 	mutex_lock(&psi->read_mutex);
740 	if (psi->open && psi->open(psi))
741 		goto out;
742 
743 	/*
744 	 * Backend callback read() allocates record.buf. decompress_record()
745 	 * may reallocate record.buf. On success, pstore_mkfile() will keep
746 	 * the record.buf, so free it only on failure.
747 	 */
748 	for (; stop_loop; stop_loop--) {
749 		struct pstore_record *record;
750 		int rc;
751 
752 		record = kzalloc(sizeof(*record), GFP_KERNEL);
753 		if (!record) {
754 			pr_err("out of memory creating record\n");
755 			break;
756 		}
757 		pstore_record_init(record, psi);
758 
759 		record->size = psi->read(record);
760 
761 		/* No more records left in backend? */
762 		if (record->size <= 0) {
763 			kfree(record);
764 			break;
765 		}
766 
767 		decompress_record(record);
768 		rc = pstore_mkfile(root, record);
769 		if (rc) {
770 			/* pstore_mkfile() did not take record, so free it. */
771 			kfree(record->buf);
772 			kfree(record);
773 			if (rc != -EEXIST || !quiet)
774 				failed++;
775 		}
776 	}
777 	if (psi->close)
778 		psi->close(psi);
779 out:
780 	mutex_unlock(&psi->read_mutex);
781 
782 	if (failed)
783 		pr_warn("failed to create %d record(s) from '%s'\n",
784 			failed, psi->name);
785 	if (!stop_loop)
786 		pr_err("looping? Too many records seen from '%s'\n",
787 			psi->name);
788 }
789 
790 static void pstore_dowork(struct work_struct *work)
791 {
792 	pstore_get_records(1);
793 }
794 
795 static void pstore_timefunc(struct timer_list *unused)
796 {
797 	if (pstore_new_entry) {
798 		pstore_new_entry = 0;
799 		schedule_work(&pstore_work);
800 	}
801 
802 	if (pstore_update_ms >= 0)
803 		mod_timer(&pstore_timer,
804 			  jiffies + msecs_to_jiffies(pstore_update_ms));
805 }
806 
807 void __init pstore_choose_compression(void)
808 {
809 	const struct pstore_zbackend *step;
810 
811 	if (!compress)
812 		return;
813 
814 	for (step = zbackends; step->name; step++) {
815 		if (!strcmp(compress, step->name)) {
816 			zbackend = step;
817 			return;
818 		}
819 	}
820 }
821 
822 static int __init pstore_init(void)
823 {
824 	int ret;
825 
826 	pstore_choose_compression();
827 
828 	/*
829 	 * Check if any pstore backends registered earlier but did not
830 	 * initialize compression because crypto was not ready. If so,
831 	 * initialize compression now.
832 	 */
833 	allocate_buf_for_compression();
834 
835 	ret = pstore_init_fs();
836 	if (ret)
837 		return ret;
838 
839 	return 0;
840 }
841 late_initcall(pstore_init);
842 
843 static void __exit pstore_exit(void)
844 {
845 	pstore_exit_fs();
846 }
847 module_exit(pstore_exit)
848 
849 module_param(compress, charp, 0444);
850 MODULE_PARM_DESC(compress, "Pstore compression to use");
851 
852 module_param(backend, charp, 0444);
853 MODULE_PARM_DESC(backend, "Pstore backend to use");
854 
855 MODULE_AUTHOR("Tony Luck <tony.luck@intel.com>");
856 MODULE_LICENSE("GPL");
857