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