platform.c 19.5 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
#include <linux/scatterlist.h>
32 33
#include <linux/slab.h>
#include <linux/uaccess.h>
34
#include <linux/jiffies.h>
35
#include <linux/workqueue.h>
36

37 38
#include <crypto/acompress.h>

39 40
#include "internal.h"

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

53 54 55 56 57 58 59 60 61 62 63 64 65
/* 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",
};

66 67
static int pstore_new_entry;

68
static void pstore_timefunc(struct timer_list *);
69
static DEFINE_TIMER(pstore_timer, pstore_timefunc);
70 71 72 73

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

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

82
static char *backend;
83 84 85
module_param(backend, charp, 0444);
MODULE_PARM_DESC(backend, "specific backend to use");

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

95
/* Compression parameters */
96 97
static struct crypto_acomp *tfm;
static struct acomp_req *creq;
98 99

struct pstore_zbackend {
G
Geliang Tang 已提交
100
	int (*zbufsize)(size_t size);
101 102
	const char *name;
};
103 104 105 106

static char *big_oops_buf;
static size_t big_oops_buf_sz;

107
/* How much of the console log to snapshot */
108
unsigned long kmsg_bytes = CONFIG_PSTORE_DEFAULT_KMSG_BYTES;
109

110
void pstore_set_kmsg_bytes(int bytes)
111
{
112
	kmsg_bytes = bytes;
113 114 115 116 117
}

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

118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
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);

142 143 144 145 146 147 148 149
static void pstore_timer_kick(void)
{
	if (pstore_update_ms < 0)
		return;

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

150
static bool pstore_cannot_block_path(enum kmsg_dump_reason reason)
151
{
152 153 154 155
	/*
	 * In case of NMI path, pstore shouldn't be blocked
	 * regardless of reason.
	 */
156 157 158 159 160 161
	if (in_nmi())
		return true;

	switch (reason) {
	/* In panic case, other cpus are stopped by smp_send_stop(). */
	case KMSG_DUMP_PANIC:
162 163 164 165
	/*
	 * Emergency restart shouldn't be blocked by spinning on
	 * pstore_info::buf_lock.
	 */
166 167 168 169 170 171 172
	case KMSG_DUMP_EMERG:
		return true;
	default:
		return false;
	}
}

A
Arnd Bergmann 已提交
173
#if IS_ENABLED(CONFIG_PSTORE_DEFLATE_COMPRESS)
G
Geliang Tang 已提交
174
static int zbufsize_deflate(size_t size)
175
{
176 177
	size_t cmpr;

G
Geliang Tang 已提交
178
	switch (size) {
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
	/* 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;
	}
197

G
Geliang Tang 已提交
198
	return (size * 100) / cmpr;
199 200 201
}
#endif

A
Arnd Bergmann 已提交
202
#if IS_ENABLED(CONFIG_PSTORE_LZO_COMPRESS)
G
Geliang Tang 已提交
203
static int zbufsize_lzo(size_t size)
204
{
G
Geliang Tang 已提交
205
	return lzo1x_worst_compress(size);
206 207 208
}
#endif

A
Arnd Bergmann 已提交
209
#if IS_ENABLED(CONFIG_PSTORE_LZ4_COMPRESS) || IS_ENABLED(CONFIG_PSTORE_LZ4HC_COMPRESS)
G
Geliang Tang 已提交
210
static int zbufsize_lz4(size_t size)
211
{
G
Geliang Tang 已提交
212
	return LZ4_compressBound(size);
213
}
214 215
#endif

A
Arnd Bergmann 已提交
216
#if IS_ENABLED(CONFIG_PSTORE_842_COMPRESS)
G
Geliang Tang 已提交
217
static int zbufsize_842(size_t size)
218
{
219
	return size;
220
}
221 222
#endif

223 224 225
#if IS_ENABLED(CONFIG_PSTORE_ZSTD_COMPRESS)
static int zbufsize_zstd(size_t size)
{
N
Nick Terrell 已提交
226
	return zstd_compress_bound(size);
227 228 229
}
#endif

230 231 232
static const struct pstore_zbackend *zbackend __ro_after_init;

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

static int pstore_compress(const void *in, void *out,
G
Geliang Tang 已提交
273
			   unsigned int inlen, unsigned int outlen)
274
{
275
	struct scatterlist src, dst;
G
Geliang Tang 已提交
276 277
	int ret;

278
	if (!IS_ENABLED(CONFIG_PSTORE_COMPRESS))
279 280
		return -EINVAL;

281 282 283 284 285 286 287 288 289
	sg_init_table(&src, 1);
	sg_set_buf(&src, in, inlen);

	sg_init_table(&dst, 1);
	sg_set_buf(&dst, out, outlen);

	acomp_request_set_params(creq, &src, &dst, inlen, outlen);

	ret = crypto_acomp_compress(creq);
G
Geliang Tang 已提交
290 291 292 293 294 295
	if (ret) {
		pr_err("crypto_comp_compress failed, ret = %d!\n", ret);
		return ret;
	}

	return outlen;
296 297 298 299
}

static void allocate_buf_for_compression(void)
{
300
	struct crypto_acomp *acomp;
301 302 303 304
	int size;
	char *buf;

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

308 309 310 311
	/* Skip if no pstore backend yet or compression init already done. */
	if (!psinfo || tfm)
		return;

312
	if (!crypto_has_acomp(zbackend->name, 0, CRYPTO_ALG_ASYNC)) {
313
		pr_err("Unknown compression: %s\n", zbackend->name);
G
Geliang Tang 已提交
314 315 316
		return;
	}

317 318 319 320
	size = zbackend->zbufsize(psinfo->bufsize);
	if (size <= 0) {
		pr_err("Invalid compression size for %s: %d\n",
		       zbackend->name, size);
G
Geliang Tang 已提交
321
		return;
322
	}
G
Geliang Tang 已提交
323

324 325 326 327
	buf = kmalloc(size, GFP_KERNEL);
	if (!buf) {
		pr_err("Failed %d byte compression buffer allocation for: %s\n",
		       size, zbackend->name);
G
Geliang Tang 已提交
328 329 330
		return;
	}

331 332
	acomp = crypto_alloc_acomp(zbackend->name, 0, CRYPTO_ALG_ASYNC);
	if (IS_ERR_OR_NULL(acomp)) {
333 334
		kfree(buf);
		pr_err("crypto_alloc_comp('%s') failed: %ld\n", zbackend->name,
335 336 337 338 339 340 341 342 343
		       PTR_ERR(acomp));
		return;
	}

	creq = acomp_request_alloc(acomp);
	if (!creq) {
		crypto_free_acomp(acomp);
		kfree(buf);
		pr_err("acomp_request_alloc('%s') failed\n", zbackend->name);
G
Geliang Tang 已提交
344
		return;
345
	}
346 347

	/* A non-NULL big_oops_buf indicates compression is available. */
348
	tfm = acomp;
349 350 351
	big_oops_buf_sz = size;
	big_oops_buf = buf;

352
	pr_info("Using crash dump compression: %s\n", zbackend->name);
353 354 355 356
}

static void free_buf_for_compression(void)
{
357
	if (IS_ENABLED(CONFIG_PSTORE_COMPRESS) && tfm) {
358 359
		acomp_request_free(creq);
		crypto_free_acomp(tfm);
360 361
		tfm = NULL;
	}
G
Geliang Tang 已提交
362 363 364
	kfree(big_oops_buf);
	big_oops_buf = NULL;
	big_oops_buf_sz = 0;
G
Geliang Tang 已提交
365 366
}

367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
/*
 * 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;
}

393 394 395 396 397 398
void pstore_record_init(struct pstore_record *record,
			struct pstore_info *psinfo)
{
	memset(record, 0, sizeof(*record));

	record->psi = psinfo;
399 400

	/* Report zeroed timestamp if called before timekeeping has resumed. */
401
	record->time = ns_to_timespec64(ktime_get_real_fast_ns());
402 403
}

404
/*
405 406
 * callback from kmsg_dump. Save as much as we can (up to kmsg_bytes) from the
 * end of the buffer.
407 408
 */
static void pstore_dump(struct kmsg_dumper *dumper,
409
			enum kmsg_dump_reason reason)
410
{
411
	struct kmsg_dump_iter iter;
412
	unsigned long	total = 0;
413
	const char	*why;
M
Matthew Garrett 已提交
414
	unsigned int	part = 1;
415
	unsigned long	flags = 0;
416
	int		ret;
417

418
	why = kmsg_dump_reason_str(reason);
T
Tony Luck 已提交
419

420 421 422 423
	if (pstore_cannot_block_path(reason)) {
		if (!spin_trylock_irqsave(&psinfo->buf_lock, flags)) {
			pr_err("dump skipped in %s path because of concurrent dump\n",
					in_nmi() ? "NMI" : why);
424
			return;
425
		}
426 427
	} else {
		spin_lock_irqsave(&psinfo->buf_lock, flags);
428
	}
K
Kees Cook 已提交
429

430 431
	kmsg_dump_rewind(&iter);

432 433
	oopscount++;
	while (total < kmsg_bytes) {
434
		char *dst;
435 436
		size_t dst_size;
		int header_size;
437
		int zipped_len = -1;
438
		size_t dump_size;
439 440 441 442 443 444 445 446
		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;
447

K
Kees Cook 已提交
448
		if (big_oops_buf) {
449
			dst = big_oops_buf;
450
			dst_size = big_oops_buf_sz;
N
Namhyung Kim 已提交
451 452
		} else {
			dst = psinfo->buf;
453
			dst_size = psinfo->bufsize;
N
Namhyung Kim 已提交
454 455
		}

456 457 458 459
		/* Write dump header. */
		header_size = snprintf(dst, dst_size, "%s#%d Part%u\n", why,
				 oopscount, part);
		dst_size -= header_size;
460

461
		/* Write dump contents. */
462
		if (!kmsg_dump_get_buffer(&iter, true, dst + header_size,
463
					  dst_size, &dump_size))
N
Namhyung Kim 已提交
464
			break;
465

K
Kees Cook 已提交
466
		if (big_oops_buf) {
467
			zipped_len = pstore_compress(dst, psinfo->buf,
468 469
						header_size + dump_size,
						psinfo->bufsize);
470 471

			if (zipped_len > 0) {
472 473
				record.compressed = true;
				record.size = zipped_len;
474
			} else {
475 476
				record.size = copy_kmsg_to_buffer(header_size,
								  dump_size);
477 478
			}
		} else {
479
			record.size = header_size + dump_size;
480
		}
481

482
		ret = psinfo->write(&record);
483
		if (ret == 0 && reason == KMSG_DUMP_OOPS) {
484
			pstore_new_entry = 1;
485 486
			pstore_timer_kick();
		}
487

488
		total += record.size;
489
		part++;
490
	}
491
	spin_unlock_irqrestore(&psinfo->buf_lock, flags);
492 493 494 495 496 497
}

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

498 499 500
/*
 * Register with kmsg_dump to save last part of console log on panic.
 */
501 502 503 504 505
static void pstore_register_kmsg(void)
{
	kmsg_dump_register(&pstore_dumper);
}

G
Geliang Tang 已提交
506 507 508 509 510
static void pstore_unregister_kmsg(void)
{
	kmsg_dump_unregister(&pstore_dumper);
}

511 512 513
#ifdef CONFIG_PSTORE_CONSOLE
static void pstore_console_write(struct console *con, const char *s, unsigned c)
{
514
	struct pstore_record record;
515

516 517 518
	if (!c)
		return;

519 520
	pstore_record_init(&record, psinfo);
	record.type = PSTORE_TYPE_CONSOLE;
521

522 523 524
	record.buf = (char *)s;
	record.size = c;
	psinfo->write(&record);
525 526 527 528 529 530 531 532 533
}

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

static void pstore_register_console(void)
{
534 535 536
	/* Show which backend is going to get console writes. */
	strscpy(pstore_console.name, psinfo->name,
		sizeof(pstore_console.name));
537 538 539 540 541
	/*
	 * 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;
542 543
	register_console(&pstore_console);
}
G
Geliang Tang 已提交
544 545 546 547 548

static void pstore_unregister_console(void)
{
	unregister_console(&pstore_console);
}
549 550
#else
static void pstore_register_console(void) {}
G
Geliang Tang 已提交
551
static void pstore_unregister_console(void) {}
552 553
#endif

K
Kees Cook 已提交
554 555
static int pstore_write_user_compat(struct pstore_record *record,
				    const char __user *buf)
M
Mark Salyzyn 已提交
556
{
K
Kees Cook 已提交
557 558 559 560 561
	int ret = 0;

	if (record->buf)
		return -EINVAL;

G
Geliang Tang 已提交
562
	record->buf = memdup_user(buf, record->size);
563
	if (IS_ERR(record->buf)) {
G
Geliang Tang 已提交
564
		ret = PTR_ERR(record->buf);
K
Kees Cook 已提交
565
		goto out;
M
Mark Salyzyn 已提交
566
	}
K
Kees Cook 已提交
567 568 569 570

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

	kfree(record->buf);
G
Geliang Tang 已提交
571
out:
K
Kees Cook 已提交
572 573 574
	record->buf = NULL;

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

577 578 579 580 581 582 583 584 585
/*
 * 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)
{
586 587
	if (backend && strcmp(backend, psi->name)) {
		pr_warn("ignoring unexpected backend '%s'\n", psi->name);
588
		return -EPERM;
589
	}
590

K
Kees Cook 已提交
591 592 593 594 595 596 597 598 599 600 601 602 603 604
	/* 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;
	}

605
	mutex_lock(&psinfo_lock);
606
	if (psinfo) {
607 608
		pr_warn("backend '%s' already loaded: ignoring '%s'\n",
			psinfo->name, psi->name);
609
		mutex_unlock(&psinfo_lock);
610 611
		return -EBUSY;
	}
612

K
Kees Cook 已提交
613 614
	if (!psi->write_user)
		psi->write_user = pstore_write_user_compat;
615
	psinfo = psi;
616
	mutex_init(&psinfo->read_mutex);
617
	spin_lock_init(&psinfo->buf_lock);
618

619 620
	if (psi->flags & PSTORE_FLAGS_DMESG)
		allocate_buf_for_compression();
621

622
	pstore_get_records(0);
623

624 625
	if (psi->flags & PSTORE_FLAGS_DMESG) {
		pstore_dumper.max_reason = psinfo->max_reason;
626
		pstore_register_kmsg();
627
	}
628
	if (psi->flags & PSTORE_FLAGS_CONSOLE)
629
		pstore_register_console();
630
	if (psi->flags & PSTORE_FLAGS_FTRACE)
631
		pstore_register_ftrace();
632
	if (psi->flags & PSTORE_FLAGS_PMSG)
633
		pstore_register_pmsg();
634

635
	/* Start watching for new records, if desired. */
636
	pstore_timer_kick();
637

638 639 640 641
	/*
	 * Update the module parameter backend, so it is visible
	 * through /sys/module/pstore/parameters/backend
	 */
642
	backend = kstrdup(psi->name, GFP_KERNEL);
643

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

646
	mutex_unlock(&psinfo_lock);
647 648 649 650
	return 0;
}
EXPORT_SYMBOL_GPL(pstore_register);

G
Geliang Tang 已提交
651 652
void pstore_unregister(struct pstore_info *psi)
{
653 654 655 656 657 658 659 660 661 662 663 664
	/* 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;
	}

665
	/* Unregister all callbacks. */
666
	if (psi->flags & PSTORE_FLAGS_PMSG)
667
		pstore_unregister_pmsg();
668
	if (psi->flags & PSTORE_FLAGS_FTRACE)
669
		pstore_unregister_ftrace();
670
	if (psi->flags & PSTORE_FLAGS_CONSOLE)
671
		pstore_unregister_console();
672 673
	if (psi->flags & PSTORE_FLAGS_DMESG)
		pstore_unregister_kmsg();
G
Geliang Tang 已提交
674

675 676 677 678
	/* Stop timer and make sure all work has finished. */
	del_timer_sync(&pstore_timer);
	flush_work(&pstore_work);

679 680 681
	/* Remove all backend records from filesystem tree. */
	pstore_put_backend_records(psi);

G
Geliang Tang 已提交
682 683 684
	free_buf_for_compression();

	psinfo = NULL;
685
	kfree(backend);
G
Geliang Tang 已提交
686
	backend = NULL;
687
	mutex_unlock(&psinfo_lock);
G
Geliang Tang 已提交
688 689 690
}
EXPORT_SYMBOL_GPL(pstore_unregister);

691 692
static void decompress_record(struct pstore_record *record)
{
693
	int ret;
694
	int unzipped_len;
695
	char *unzipped, *workspace;
696 697
	struct acomp_req *dreq;
	struct scatterlist src, dst;
698

699
	if (!IS_ENABLED(CONFIG_PSTORE_COMPRESS) || !record->compressed)
700 701
		return;

702
	/* Only PSTORE_TYPE_DMESG support compression. */
703
	if (record->type != PSTORE_TYPE_DMESG) {
704 705 706 707
		pr_warn("ignored compressed record type %d\n", record->type);
		return;
	}

708
	/* Missing compression buffer means compression was not initialized. */
709
	if (!big_oops_buf) {
710
		pr_warn("no decompression method initialized!\n");
711 712 713
		return;
	}

714 715 716 717 718
	/* 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)
719 720
		return;

721 722 723 724 725 726 727 728 729 730 731 732 733 734
	dreq = acomp_request_alloc(tfm);
	if (!dreq) {
		kfree(workspace);
		return;
	}

	sg_init_table(&src, 1);
	sg_set_buf(&src, record->buf, record->size);

	sg_init_table(&dst, 1);
	sg_set_buf(&dst, workspace, unzipped_len);

	acomp_request_set_params(dreq, &src, &dst, record->size, unzipped_len);

735
	/* After decompression "unzipped_len" is almost certainly smaller. */
736
	ret = crypto_acomp_decompress(dreq);
737
	if (ret) {
738
		pr_err("crypto_acomp_decompress failed, ret = %d!\n", ret);
739
		kfree(workspace);
740 741 742 743
		return;
	}

	/* Append ECC notice to decompressed buffer. */
744
	unzipped_len = dreq->dlen;
745
	memcpy(workspace + unzipped_len, record->buf + record->size,
746 747
	       record->ecc_notice_size);

748 749 750 751
	/* Copy decompressed contents into an minimum-sized allocation. */
	unzipped = kmemdup(workspace, unzipped_len + record->ecc_notice_size,
			   GFP_KERNEL);
	kfree(workspace);
752
	acomp_request_free(dreq);
753 754 755 756
	if (!unzipped)
		return;

	/* Swap out compressed contents with decompressed contents. */
757
	kfree(record->buf);
758
	record->buf = unzipped;
759 760
	record->size = unzipped_len;
	record->compressed = false;
761 762
}

763
/*
764
 * Read all the records from one persistent store backend. Create
765 766 767
 * files in our filesystem.  Don't warn about -EEXIST errors
 * when we are re-scanning the backing store looking to add new
 * error records.
768
 */
769 770
void pstore_get_backend_records(struct pstore_info *psi,
				struct dentry *root, int quiet)
771
{
772
	int failed = 0;
773
	unsigned int stop_loop = 65536;
774

775
	if (!psi || !root)
776 777
		return;

778
	mutex_lock(&psi->read_mutex);
779
	if (psi->open && psi->open(psi))
780 781
		goto out;

782 783 784 785 786
	/*
	 * 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.
	 */
787
	for (; stop_loop; stop_loop--) {
788 789 790 791 792 793 794 795
		struct pstore_record *record;
		int rc;

		record = kzalloc(sizeof(*record), GFP_KERNEL);
		if (!record) {
			pr_err("out of memory creating record\n");
			break;
		}
796
		pstore_record_init(record, psi);
797 798 799 800

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

		/* No more records left in backend? */
801 802
		if (record->size <= 0) {
			kfree(record);
803
			break;
804
		}
805 806

		decompress_record(record);
807
		rc = pstore_mkfile(root, record);
808
		if (rc) {
809
			/* pstore_mkfile() did not take record, so free it. */
810
			kfree(record->buf);
811
			kfree(record);
812 813 814
			if (rc != -EEXIST || !quiet)
				failed++;
		}
815
	}
816 817
	if (psi->close)
		psi->close(psi);
818
out:
819
	mutex_unlock(&psi->read_mutex);
820 821

	if (failed)
822
		pr_warn("failed to create %d record(s) from '%s'\n",
F
Fabian Frederick 已提交
823
			failed, psi->name);
824 825 826
	if (!stop_loop)
		pr_err("looping? Too many records seen from '%s'\n",
			psi->name);
827 828
}

829 830 831 832 833
static void pstore_dowork(struct work_struct *work)
{
	pstore_get_records(1);
}

834
static void pstore_timefunc(struct timer_list *unused)
835 836 837 838 839 840
{
	if (pstore_new_entry) {
		pstore_new_entry = 0;
		schedule_work(&pstore_work);
	}

841
	pstore_timer_kick();
842 843
}

844
static void __init pstore_choose_compression(void)
845 846 847 848 849 850 851 852 853 854 855 856 857 858
{
	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 已提交
859 860 861 862 863 864
static int __init pstore_init(void)
{
	int ret;

	pstore_choose_compression();

865 866 867 868 869
	/*
	 * Check if any pstore backends registered earlier but did not
	 * initialize compression because crypto was not ready. If so,
	 * initialize compression now.
	 */
870
	allocate_buf_for_compression();
871

K
Kees Cook 已提交
872 873
	ret = pstore_init_fs();
	if (ret)
874
		free_buf_for_compression();
K
Kees Cook 已提交
875

876
	return ret;
K
Kees Cook 已提交
877
}
878
late_initcall(pstore_init);
K
Kees Cook 已提交
879 880 881 882 883 884 885 886 887

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

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