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