platform.c 18.8 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-only
2 3 4
/*
 * Persistent Storage - platform driver interface parts.
 *
5
 * Copyright (C) 2007-2008 Google, Inc.
6 7 8
 * Copyright (C) 2010 Intel Corporation <tony.luck@intel.com>
 */

F
Fabian Frederick 已提交
9 10
#define pr_fmt(fmt) "pstore: " fmt

11 12 13 14 15
#include <linux/atomic.h>
#include <linux/types.h>
#include <linux/errno.h>
#include <linux/init.h>
#include <linux/kmsg_dump.h>
16
#include <linux/console.h>
17 18
#include <linux/module.h>
#include <linux/pstore.h>
A
Arnd Bergmann 已提交
19
#if IS_ENABLED(CONFIG_PSTORE_LZO_COMPRESS)
20 21
#include <linux/lzo.h>
#endif
A
Arnd Bergmann 已提交
22
#if IS_ENABLED(CONFIG_PSTORE_LZ4_COMPRESS) || IS_ENABLED(CONFIG_PSTORE_LZ4HC_COMPRESS)
23 24
#include <linux/lz4.h>
#endif
25 26 27
#if IS_ENABLED(CONFIG_PSTORE_ZSTD_COMPRESS)
#include <linux/zstd.h>
#endif
G
Geliang Tang 已提交
28
#include <linux/crypto.h>
29
#include <linux/string.h>
30
#include <linux/timer.h>
31 32
#include <linux/slab.h>
#include <linux/uaccess.h>
33
#include <linux/jiffies.h>
34
#include <linux/workqueue.h>
35 36 37

#include "internal.h"

38 39 40 41 42
/*
 * We defer making "oops" entries appear in pstore - see
 * whether the system is actually still running well enough
 * to let someone see the entry
 */
43
static int pstore_update_ms = -1;
44 45
module_param_named(update_ms, pstore_update_ms, int, 0600);
MODULE_PARM_DESC(update_ms, "milliseconds before pstore updates its content "
46
		 "(default is -1, which means runtime updates are disabled; "
47
		 "enabling this option may not be safe; it may lead to further "
48
		 "corruption on Oopses)");
49

50 51 52 53 54 55 56 57 58 59 60 61 62
/* Names should be in the same order as the enum pstore_type_id */
static const char * const pstore_type_names[] = {
	"dmesg",
	"mce",
	"console",
	"ftrace",
	"rtas",
	"powerpc-ofw",
	"powerpc-common",
	"pmsg",
	"powerpc-opal",
};

63 64
static int pstore_new_entry;

65
static void pstore_timefunc(struct timer_list *);
66
static DEFINE_TIMER(pstore_timer, pstore_timefunc);
67 68 69 70

static void pstore_dowork(struct work_struct *);
static DECLARE_WORK(pstore_work, pstore_dowork);

71
/*
72 73 74
 * psinfo_lock protects "psinfo" during calls to
 * pstore_register(), pstore_unregister(), and
 * the filesystem mount/unmount routines.
75
 */
76
static DEFINE_MUTEX(psinfo_lock);
77
struct pstore_info *psinfo;
78

79
static char *backend;
80 81 82
module_param(backend, charp, 0444);
MODULE_PARM_DESC(backend, "specific backend to use");

83 84 85 86 87 88
static char *compress =
#ifdef CONFIG_PSTORE_COMPRESS_DEFAULT
		CONFIG_PSTORE_COMPRESS_DEFAULT;
#else
		NULL;
#endif
89 90
module_param(compress, charp, 0444);
MODULE_PARM_DESC(compress, "compression to use");
91

92
/* Compression parameters */
G
Geliang Tang 已提交
93
static struct crypto_comp *tfm;
94 95

struct pstore_zbackend {
G
Geliang Tang 已提交
96
	int (*zbufsize)(size_t size);
97 98
	const char *name;
};
99 100 101 102

static char *big_oops_buf;
static size_t big_oops_buf_sz;

103
/* How much of the console log to snapshot */
104
unsigned long kmsg_bytes = CONFIG_PSTORE_DEFAULT_KMSG_BYTES;
105

106
void pstore_set_kmsg_bytes(int bytes)
107
{
108
	kmsg_bytes = bytes;
109 110 111 112 113
}

/* Tag each group of saved records with a sequence number */
static int	oopscount;

114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
const char *pstore_type_to_name(enum pstore_type_id type)
{
	BUILD_BUG_ON(ARRAY_SIZE(pstore_type_names) != PSTORE_TYPE_MAX);

	if (WARN_ON_ONCE(type >= PSTORE_TYPE_MAX))
		return "unknown";

	return pstore_type_names[type];
}
EXPORT_SYMBOL_GPL(pstore_type_to_name);

enum pstore_type_id pstore_name_to_type(const char *name)
{
	int i;

	for (i = 0; i < PSTORE_TYPE_MAX; i++) {
		if (!strcmp(pstore_type_names[i], name))
			return i;
	}

	return PSTORE_TYPE_MAX;
}
EXPORT_SYMBOL_GPL(pstore_name_to_type);

138 139 140 141 142 143 144 145
static void pstore_timer_kick(void)
{
	if (pstore_update_ms < 0)
		return;

	mod_timer(&pstore_timer, jiffies + msecs_to_jiffies(pstore_update_ms));
}

K
Kees Cook 已提交
146 147 148 149 150 151
/*
 * Should pstore_dump() wait for a concurrent pstore_dump()? If
 * not, the current pstore_dump() will report a failure to dump
 * and return.
 */
static bool pstore_cannot_wait(enum kmsg_dump_reason reason)
152
{
K
Kees Cook 已提交
153
	/* In NMI path, pstore shouldn't block regardless of reason. */
154 155 156 157 158 159
	if (in_nmi())
		return true;

	switch (reason) {
	/* In panic case, other cpus are stopped by smp_send_stop(). */
	case KMSG_DUMP_PANIC:
K
Kees Cook 已提交
160
	/* Emergency restart shouldn't be blocked. */
161 162 163 164 165 166 167
	case KMSG_DUMP_EMERG:
		return true;
	default:
		return false;
	}
}

A
Arnd Bergmann 已提交
168
#if IS_ENABLED(CONFIG_PSTORE_DEFLATE_COMPRESS)
G
Geliang Tang 已提交
169
static int zbufsize_deflate(size_t size)
170
{
171 172
	size_t cmpr;

G
Geliang Tang 已提交
173
	switch (size) {
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
	/* buffer range for efivars */
	case 1000 ... 2000:
		cmpr = 56;
		break;
	case 2001 ... 3000:
		cmpr = 54;
		break;
	case 3001 ... 3999:
		cmpr = 52;
		break;
	/* buffer range for nvram, erst */
	case 4000 ... 10000:
		cmpr = 45;
		break;
	default:
		cmpr = 60;
		break;
	}
192

G
Geliang Tang 已提交
193
	return (size * 100) / cmpr;
194 195 196
}
#endif

A
Arnd Bergmann 已提交
197
#if IS_ENABLED(CONFIG_PSTORE_LZO_COMPRESS)
G
Geliang Tang 已提交
198
static int zbufsize_lzo(size_t size)
199
{
G
Geliang Tang 已提交
200
	return lzo1x_worst_compress(size);
201 202 203
}
#endif

A
Arnd Bergmann 已提交
204
#if IS_ENABLED(CONFIG_PSTORE_LZ4_COMPRESS) || IS_ENABLED(CONFIG_PSTORE_LZ4HC_COMPRESS)
G
Geliang Tang 已提交
205
static int zbufsize_lz4(size_t size)
206
{
G
Geliang Tang 已提交
207
	return LZ4_compressBound(size);
208
}
209 210
#endif

A
Arnd Bergmann 已提交
211
#if IS_ENABLED(CONFIG_PSTORE_842_COMPRESS)
G
Geliang Tang 已提交
212
static int zbufsize_842(size_t size)
213
{
214
	return size;
215
}
216 217
#endif

218 219 220 221 222 223 224
#if IS_ENABLED(CONFIG_PSTORE_ZSTD_COMPRESS)
static int zbufsize_zstd(size_t size)
{
	return ZSTD_compressBound(size);
}
#endif

225 226 227
static const struct pstore_zbackend *zbackend __ro_after_init;

static const struct pstore_zbackend zbackends[] = {
A
Arnd Bergmann 已提交
228
#if IS_ENABLED(CONFIG_PSTORE_DEFLATE_COMPRESS)
229
	{
G
Geliang Tang 已提交
230 231
		.zbufsize	= zbufsize_deflate,
		.name		= "deflate",
232 233
	},
#endif
A
Arnd Bergmann 已提交
234
#if IS_ENABLED(CONFIG_PSTORE_LZO_COMPRESS)
235
	{
G
Geliang Tang 已提交
236
		.zbufsize	= zbufsize_lzo,
237 238 239
		.name		= "lzo",
	},
#endif
A
Arnd Bergmann 已提交
240
#if IS_ENABLED(CONFIG_PSTORE_LZ4_COMPRESS)
241
	{
G
Geliang Tang 已提交
242
		.zbufsize	= zbufsize_lz4,
243 244 245
		.name		= "lz4",
	},
#endif
A
Arnd Bergmann 已提交
246
#if IS_ENABLED(CONFIG_PSTORE_LZ4HC_COMPRESS)
247
	{
G
Geliang Tang 已提交
248
		.zbufsize	= zbufsize_lz4,
249 250
		.name		= "lz4hc",
	},
251
#endif
A
Arnd Bergmann 已提交
252
#if IS_ENABLED(CONFIG_PSTORE_842_COMPRESS)
253
	{
G
Geliang Tang 已提交
254
		.zbufsize	= zbufsize_842,
255 256
		.name		= "842",
	},
257 258 259 260 261 262
#endif
#if IS_ENABLED(CONFIG_PSTORE_ZSTD_COMPRESS)
	{
		.zbufsize	= zbufsize_zstd,
		.name		= "zstd",
	},
263 264 265
#endif
	{ }
};
266 267

static int pstore_compress(const void *in, void *out,
G
Geliang Tang 已提交
268
			   unsigned int inlen, unsigned int outlen)
269
{
G
Geliang Tang 已提交
270 271
	int ret;

272
	if (!IS_ENABLED(CONFIG_PSTORE_COMPRESS))
273 274
		return -EINVAL;

G
Geliang Tang 已提交
275 276 277 278 279 280 281
	ret = crypto_comp_compress(tfm, in, inlen, out, &outlen);
	if (ret) {
		pr_err("crypto_comp_compress failed, ret = %d!\n", ret);
		return ret;
	}

	return outlen;
282 283 284 285
}

static void allocate_buf_for_compression(void)
{
286 287 288 289 290
	struct crypto_comp *ctx;
	int size;
	char *buf;

	/* Skip if not built-in or compression backend not selected yet. */
291
	if (!IS_ENABLED(CONFIG_PSTORE_COMPRESS) || !zbackend)
G
Geliang Tang 已提交
292 293
		return;

294 295 296 297
	/* Skip if no pstore backend yet or compression init already done. */
	if (!psinfo || tfm)
		return;

G
Geliang Tang 已提交
298
	if (!crypto_has_comp(zbackend->name, 0, 0)) {
299
		pr_err("Unknown compression: %s\n", zbackend->name);
G
Geliang Tang 已提交
300 301 302
		return;
	}

303 304 305 306
	size = zbackend->zbufsize(psinfo->bufsize);
	if (size <= 0) {
		pr_err("Invalid compression size for %s: %d\n",
		       zbackend->name, size);
G
Geliang Tang 已提交
307
		return;
308
	}
G
Geliang Tang 已提交
309

310 311 312 313
	buf = kmalloc(size, GFP_KERNEL);
	if (!buf) {
		pr_err("Failed %d byte compression buffer allocation for: %s\n",
		       size, zbackend->name);
G
Geliang Tang 已提交
314 315 316
		return;
	}

317 318 319 320 321
	ctx = crypto_alloc_comp(zbackend->name, 0, 0);
	if (IS_ERR_OR_NULL(ctx)) {
		kfree(buf);
		pr_err("crypto_alloc_comp('%s') failed: %ld\n", zbackend->name,
		       PTR_ERR(ctx));
G
Geliang Tang 已提交
322
		return;
323
	}
324 325 326 327 328 329

	/* A non-NULL big_oops_buf indicates compression is available. */
	tfm = ctx;
	big_oops_buf_sz = size;
	big_oops_buf = buf;

330
	pr_info("Using crash dump compression: %s\n", zbackend->name);
331 332 333 334
}

static void free_buf_for_compression(void)
{
335
	if (IS_ENABLED(CONFIG_PSTORE_COMPRESS) && tfm) {
G
Geliang Tang 已提交
336
		crypto_free_comp(tfm);
337 338
		tfm = NULL;
	}
G
Geliang Tang 已提交
339 340 341
	kfree(big_oops_buf);
	big_oops_buf = NULL;
	big_oops_buf_sz = 0;
G
Geliang Tang 已提交
342 343
}

344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
/*
 * Called when compression fails, since the printk buffer
 * would be fetched for compression calling it again when
 * compression fails would have moved the iterator of
 * printk buffer which results in fetching old contents.
 * Copy the recent messages from big_oops_buf to psinfo->buf
 */
static size_t copy_kmsg_to_buffer(int hsize, size_t len)
{
	size_t total_len;
	size_t diff;

	total_len = hsize + len;

	if (total_len > psinfo->bufsize) {
		diff = total_len - psinfo->bufsize + hsize;
		memcpy(psinfo->buf, big_oops_buf, hsize);
		memcpy(psinfo->buf + hsize, big_oops_buf + diff,
					psinfo->bufsize - hsize);
		total_len = psinfo->bufsize;
	} else
		memcpy(psinfo->buf, big_oops_buf, total_len);

	return total_len;
}

370 371 372 373 374 375
void pstore_record_init(struct pstore_record *record,
			struct pstore_info *psinfo)
{
	memset(record, 0, sizeof(*record));

	record->psi = psinfo;
376 377

	/* Report zeroed timestamp if called before timekeeping has resumed. */
378
	record->time = ns_to_timespec64(ktime_get_real_fast_ns());
379 380
}

381
/*
382 383
 * callback from kmsg_dump. Save as much as we can (up to kmsg_bytes) from the
 * end of the buffer.
384 385
 */
static void pstore_dump(struct kmsg_dumper *dumper,
386
			enum kmsg_dump_reason reason)
387
{
388
	struct kmsg_dump_iter iter;
389
	unsigned long	total = 0;
390
	const char	*why;
M
Matthew Garrett 已提交
391
	unsigned int	part = 1;
392
	int		ret;
393

394
	why = kmsg_dump_reason_str(reason);
T
Tony Luck 已提交
395

K
Kees Cook 已提交
396 397 398 399 400 401 402 403 404
	if (down_trylock(&psinfo->buf_lock)) {
		/* Failed to acquire lock: give up if we cannot wait. */
		if (pstore_cannot_wait(reason)) {
			pr_err("dump skipped in %s path: may corrupt error record\n",
				in_nmi() ? "NMI" : why);
			return;
		}
		if (down_interruptible(&psinfo->buf_lock)) {
			pr_err("could not grab semaphore?!\n");
405
			return;
406
		}
407
	}
K
Kees Cook 已提交
408

409 410
	kmsg_dump_rewind(&iter);

411 412
	oopscount++;
	while (total < kmsg_bytes) {
413
		char *dst;
414 415
		size_t dst_size;
		int header_size;
416
		int zipped_len = -1;
417
		size_t dump_size;
418 419 420 421 422 423 424 425
		struct pstore_record record;

		pstore_record_init(&record, psinfo);
		record.type = PSTORE_TYPE_DMESG;
		record.count = oopscount;
		record.reason = reason;
		record.part = part;
		record.buf = psinfo->buf;
426

K
Kees Cook 已提交
427
		if (big_oops_buf) {
428
			dst = big_oops_buf;
429
			dst_size = big_oops_buf_sz;
N
Namhyung Kim 已提交
430 431
		} else {
			dst = psinfo->buf;
432
			dst_size = psinfo->bufsize;
N
Namhyung Kim 已提交
433 434
		}

435 436 437 438
		/* Write dump header. */
		header_size = snprintf(dst, dst_size, "%s#%d Part%u\n", why,
				 oopscount, part);
		dst_size -= header_size;
439

440
		/* Write dump contents. */
441
		if (!kmsg_dump_get_buffer(&iter, true, dst + header_size,
442
					  dst_size, &dump_size))
N
Namhyung Kim 已提交
443
			break;
444

K
Kees Cook 已提交
445
		if (big_oops_buf) {
446
			zipped_len = pstore_compress(dst, psinfo->buf,
447 448
						header_size + dump_size,
						psinfo->bufsize);
449 450

			if (zipped_len > 0) {
451 452
				record.compressed = true;
				record.size = zipped_len;
453
			} else {
454 455
				record.size = copy_kmsg_to_buffer(header_size,
								  dump_size);
456 457
			}
		} else {
458
			record.size = header_size + dump_size;
459
		}
460

461
		ret = psinfo->write(&record);
462
		if (ret == 0 && reason == KMSG_DUMP_OOPS) {
463
			pstore_new_entry = 1;
464 465
			pstore_timer_kick();
		}
466

467
		total += record.size;
468
		part++;
469
	}
K
Kees Cook 已提交
470 471

	up(&psinfo->buf_lock);
472 473 474 475 476 477
}

static struct kmsg_dumper pstore_dumper = {
	.dump = pstore_dump,
};

478 479 480
/*
 * Register with kmsg_dump to save last part of console log on panic.
 */
481 482 483 484 485
static void pstore_register_kmsg(void)
{
	kmsg_dump_register(&pstore_dumper);
}

G
Geliang Tang 已提交
486 487 488 489 490
static void pstore_unregister_kmsg(void)
{
	kmsg_dump_unregister(&pstore_dumper);
}

491 492 493
#ifdef CONFIG_PSTORE_CONSOLE
static void pstore_console_write(struct console *con, const char *s, unsigned c)
{
494
	struct pstore_record record;
495

496 497 498
	if (!c)
		return;

499 500
	pstore_record_init(&record, psinfo);
	record.type = PSTORE_TYPE_CONSOLE;
501

502 503 504
	record.buf = (char *)s;
	record.size = c;
	psinfo->write(&record);
505 506 507 508 509 510 511 512 513
}

static struct console pstore_console = {
	.write	= pstore_console_write,
	.index	= -1,
};

static void pstore_register_console(void)
{
514 515 516
	/* Show which backend is going to get console writes. */
	strscpy(pstore_console.name, psinfo->name,
		sizeof(pstore_console.name));
517 518 519 520 521
	/*
	 * Always initialize flags here since prior unregister_console()
	 * calls may have changed settings (specifically CON_ENABLED).
	 */
	pstore_console.flags = CON_PRINTBUFFER | CON_ENABLED | CON_ANYTIME;
522 523
	register_console(&pstore_console);
}
G
Geliang Tang 已提交
524 525 526 527 528

static void pstore_unregister_console(void)
{
	unregister_console(&pstore_console);
}
529 530
#else
static void pstore_register_console(void) {}
G
Geliang Tang 已提交
531
static void pstore_unregister_console(void) {}
532 533
#endif

K
Kees Cook 已提交
534 535
static int pstore_write_user_compat(struct pstore_record *record,
				    const char __user *buf)
M
Mark Salyzyn 已提交
536
{
K
Kees Cook 已提交
537 538 539 540 541
	int ret = 0;

	if (record->buf)
		return -EINVAL;

G
Geliang Tang 已提交
542
	record->buf = memdup_user(buf, record->size);
543
	if (IS_ERR(record->buf)) {
G
Geliang Tang 已提交
544
		ret = PTR_ERR(record->buf);
K
Kees Cook 已提交
545
		goto out;
M
Mark Salyzyn 已提交
546
	}
K
Kees Cook 已提交
547 548 549 550

	ret = record->psi->write(record);

	kfree(record->buf);
G
Geliang Tang 已提交
551
out:
K
Kees Cook 已提交
552 553 554
	record->buf = NULL;

	return unlikely(ret < 0) ? ret : record->size;
M
Mark Salyzyn 已提交
555 556
}

557 558 559 560 561 562 563 564 565
/*
 * platform specific persistent storage driver registers with
 * us here. If pstore is already mounted, call the platform
 * read function right away to populate the file system. If not
 * then the pstore mount code will call us later to fill out
 * the file system.
 */
int pstore_register(struct pstore_info *psi)
{
566 567
	if (backend && strcmp(backend, psi->name)) {
		pr_warn("ignoring unexpected backend '%s'\n", psi->name);
568
		return -EPERM;
569
	}
570

K
Kees Cook 已提交
571 572 573 574 575 576 577 578 579 580 581 582 583 584
	/* Sanity check flags. */
	if (!psi->flags) {
		pr_warn("backend '%s' must support at least one frontend\n",
			psi->name);
		return -EINVAL;
	}

	/* Check for required functions. */
	if (!psi->read || !psi->write) {
		pr_warn("backend '%s' must implement read() and write()\n",
			psi->name);
		return -EINVAL;
	}

585
	mutex_lock(&psinfo_lock);
586
	if (psinfo) {
587 588
		pr_warn("backend '%s' already loaded: ignoring '%s'\n",
			psinfo->name, psi->name);
589
		mutex_unlock(&psinfo_lock);
590 591
		return -EBUSY;
	}
592

K
Kees Cook 已提交
593 594
	if (!psi->write_user)
		psi->write_user = pstore_write_user_compat;
595
	psinfo = psi;
596
	mutex_init(&psinfo->read_mutex);
K
Kees Cook 已提交
597
	sema_init(&psinfo->buf_lock, 1);
598

599 600
	if (psi->flags & PSTORE_FLAGS_DMESG)
		allocate_buf_for_compression();
601

602
	pstore_get_records(0);
603

604 605
	if (psi->flags & PSTORE_FLAGS_DMESG) {
		pstore_dumper.max_reason = psinfo->max_reason;
606
		pstore_register_kmsg();
607
	}
608
	if (psi->flags & PSTORE_FLAGS_CONSOLE)
609
		pstore_register_console();
610
	if (psi->flags & PSTORE_FLAGS_FTRACE)
611
		pstore_register_ftrace();
612
	if (psi->flags & PSTORE_FLAGS_PMSG)
613
		pstore_register_pmsg();
614

615
	/* Start watching for new records, if desired. */
616
	pstore_timer_kick();
617

618 619 620 621
	/*
	 * Update the module parameter backend, so it is visible
	 * through /sys/module/pstore/parameters/backend
	 */
622
	backend = kstrdup(psi->name, GFP_KERNEL);
623

F
Fabian Frederick 已提交
624
	pr_info("Registered %s as persistent store backend\n", psi->name);
625

626
	mutex_unlock(&psinfo_lock);
627 628 629 630
	return 0;
}
EXPORT_SYMBOL_GPL(pstore_register);

G
Geliang Tang 已提交
631 632
void pstore_unregister(struct pstore_info *psi)
{
633 634 635 636 637 638 639 640 641 642 643 644
	/* It's okay to unregister nothing. */
	if (!psi)
		return;

	mutex_lock(&psinfo_lock);

	/* Only one backend can be registered at a time. */
	if (WARN_ON(psi != psinfo)) {
		mutex_unlock(&psinfo_lock);
		return;
	}

645
	/* Unregister all callbacks. */
646
	if (psi->flags & PSTORE_FLAGS_PMSG)
647
		pstore_unregister_pmsg();
648
	if (psi->flags & PSTORE_FLAGS_FTRACE)
649
		pstore_unregister_ftrace();
650
	if (psi->flags & PSTORE_FLAGS_CONSOLE)
651
		pstore_unregister_console();
652 653
	if (psi->flags & PSTORE_FLAGS_DMESG)
		pstore_unregister_kmsg();
G
Geliang Tang 已提交
654

655 656 657 658
	/* Stop timer and make sure all work has finished. */
	del_timer_sync(&pstore_timer);
	flush_work(&pstore_work);

659 660 661
	/* Remove all backend records from filesystem tree. */
	pstore_put_backend_records(psi);

G
Geliang Tang 已提交
662 663 664
	free_buf_for_compression();

	psinfo = NULL;
665
	kfree(backend);
G
Geliang Tang 已提交
666
	backend = NULL;
667
	mutex_unlock(&psinfo_lock);
G
Geliang Tang 已提交
668 669 670
}
EXPORT_SYMBOL_GPL(pstore_unregister);

671 672
static void decompress_record(struct pstore_record *record)
{
673
	int ret;
674
	int unzipped_len;
675
	char *unzipped, *workspace;
676

677
	if (!IS_ENABLED(CONFIG_PSTORE_COMPRESS) || !record->compressed)
678 679
		return;

680
	/* Only PSTORE_TYPE_DMESG support compression. */
681
	if (record->type != PSTORE_TYPE_DMESG) {
682 683 684 685
		pr_warn("ignored compressed record type %d\n", record->type);
		return;
	}

686
	/* Missing compression buffer means compression was not initialized. */
687
	if (!big_oops_buf) {
688
		pr_warn("no decompression method initialized!\n");
689 690 691
		return;
	}

692 693 694 695 696
	/* Allocate enough space to hold max decompression and ECC. */
	unzipped_len = big_oops_buf_sz;
	workspace = kmalloc(unzipped_len + record->ecc_notice_size,
			    GFP_KERNEL);
	if (!workspace)
697 698
		return;

699 700 701 702 703 704
	/* After decompression "unzipped_len" is almost certainly smaller. */
	ret = crypto_comp_decompress(tfm, record->buf, record->size,
					  workspace, &unzipped_len);
	if (ret) {
		pr_err("crypto_comp_decompress failed, ret = %d!\n", ret);
		kfree(workspace);
705 706 707 708
		return;
	}

	/* Append ECC notice to decompressed buffer. */
709
	memcpy(workspace + unzipped_len, record->buf + record->size,
710 711
	       record->ecc_notice_size);

712 713 714 715 716 717 718 719
	/* Copy decompressed contents into an minimum-sized allocation. */
	unzipped = kmemdup(workspace, unzipped_len + record->ecc_notice_size,
			   GFP_KERNEL);
	kfree(workspace);
	if (!unzipped)
		return;

	/* Swap out compressed contents with decompressed contents. */
720
	kfree(record->buf);
721
	record->buf = unzipped;
722 723
	record->size = unzipped_len;
	record->compressed = false;
724 725
}

726
/*
727
 * Read all the records from one persistent store backend. Create
728 729 730
 * files in our filesystem.  Don't warn about -EEXIST errors
 * when we are re-scanning the backing store looking to add new
 * error records.
731
 */
732 733
void pstore_get_backend_records(struct pstore_info *psi,
				struct dentry *root, int quiet)
734
{
735
	int failed = 0;
736
	unsigned int stop_loop = 65536;
737

738
	if (!psi || !root)
739 740
		return;

741
	mutex_lock(&psi->read_mutex);
742
	if (psi->open && psi->open(psi))
743 744
		goto out;

745 746 747 748 749
	/*
	 * Backend callback read() allocates record.buf. decompress_record()
	 * may reallocate record.buf. On success, pstore_mkfile() will keep
	 * the record.buf, so free it only on failure.
	 */
750
	for (; stop_loop; stop_loop--) {
751 752 753 754 755 756 757 758
		struct pstore_record *record;
		int rc;

		record = kzalloc(sizeof(*record), GFP_KERNEL);
		if (!record) {
			pr_err("out of memory creating record\n");
			break;
		}
759
		pstore_record_init(record, psi);
760 761 762 763

		record->size = psi->read(record);

		/* No more records left in backend? */
764 765
		if (record->size <= 0) {
			kfree(record);
766
			break;
767
		}
768 769

		decompress_record(record);
770
		rc = pstore_mkfile(root, record);
771
		if (rc) {
772
			/* pstore_mkfile() did not take record, so free it. */
773
			kfree(record->buf);
774
			kfree(record);
775 776 777
			if (rc != -EEXIST || !quiet)
				failed++;
		}
778
	}
779 780
	if (psi->close)
		psi->close(psi);
781
out:
782
	mutex_unlock(&psi->read_mutex);
783 784

	if (failed)
785
		pr_warn("failed to create %d record(s) from '%s'\n",
F
Fabian Frederick 已提交
786
			failed, psi->name);
787 788 789
	if (!stop_loop)
		pr_err("looping? Too many records seen from '%s'\n",
			psi->name);
790 791
}

792 793 794 795 796
static void pstore_dowork(struct work_struct *work)
{
	pstore_get_records(1);
}

797
static void pstore_timefunc(struct timer_list *unused)
798 799 800 801 802 803
{
	if (pstore_new_entry) {
		pstore_new_entry = 0;
		schedule_work(&pstore_work);
	}

804
	pstore_timer_kick();
805 806
}

807
static void __init pstore_choose_compression(void)
808 809 810 811 812 813 814 815 816 817 818 819 820 821
{
	const struct pstore_zbackend *step;

	if (!compress)
		return;

	for (step = zbackends; step->name; step++) {
		if (!strcmp(compress, step->name)) {
			zbackend = step;
			return;
		}
	}
}

K
Kees Cook 已提交
822 823 824 825 826 827
static int __init pstore_init(void)
{
	int ret;

	pstore_choose_compression();

828 829 830 831 832
	/*
	 * Check if any pstore backends registered earlier but did not
	 * initialize compression because crypto was not ready. If so,
	 * initialize compression now.
	 */
833
	allocate_buf_for_compression();
834

K
Kees Cook 已提交
835 836
	ret = pstore_init_fs();
	if (ret)
837
		free_buf_for_compression();
K
Kees Cook 已提交
838

839
	return ret;
K
Kees Cook 已提交
840
}
841
late_initcall(pstore_init);
K
Kees Cook 已提交
842 843 844 845 846 847 848 849 850

static void __exit pstore_exit(void)
{
	pstore_exit_fs();
}
module_exit(pstore_exit)

MODULE_AUTHOR("Tony Luck <tony.luck@intel.com>");
MODULE_LICENSE("GPL");