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