]>
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 TL |
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 TL |
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 TL |
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 | { | |
a9fb94a9 | 350 | if (IS_ENABLED(CONFIG_PSTORE_COMPRESS) && tfm) { |
cb3bee03 | 351 | crypto_free_comp(tfm); |
a9fb94a9 PHS |
352 | tfm = NULL; |
353 | } | |
cb3bee03 GT |
354 | kfree(big_oops_buf); |
355 | big_oops_buf = NULL; | |
356 | big_oops_buf_sz = 0; | |
ee1d2674 GT |
357 | } |
358 | ||
b0aad7a9 AB |
359 | /* |
360 | * Called when compression fails, since the printk buffer | |
361 | * would be fetched for compression calling it again when | |
362 | * compression fails would have moved the iterator of | |
363 | * printk buffer which results in fetching old contents. | |
364 | * Copy the recent messages from big_oops_buf to psinfo->buf | |
365 | */ | |
366 | static size_t copy_kmsg_to_buffer(int hsize, size_t len) | |
367 | { | |
368 | size_t total_len; | |
369 | size_t diff; | |
370 | ||
371 | total_len = hsize + len; | |
372 | ||
373 | if (total_len > psinfo->bufsize) { | |
374 | diff = total_len - psinfo->bufsize + hsize; | |
375 | memcpy(psinfo->buf, big_oops_buf, hsize); | |
376 | memcpy(psinfo->buf + hsize, big_oops_buf + diff, | |
377 | psinfo->bufsize - hsize); | |
378 | total_len = psinfo->bufsize; | |
379 | } else | |
380 | memcpy(psinfo->buf, big_oops_buf, total_len); | |
381 | ||
382 | return total_len; | |
383 | } | |
384 | ||
e581ca81 KC |
385 | void pstore_record_init(struct pstore_record *record, |
386 | struct pstore_info *psinfo) | |
387 | { | |
388 | memset(record, 0, sizeof(*record)); | |
389 | ||
390 | record->psi = psinfo; | |
c7f3c595 KC |
391 | |
392 | /* Report zeroed timestamp if called before timekeeping has resumed. */ | |
7aaa822e | 393 | record->time = ns_to_timespec64(ktime_get_real_fast_ns()); |
e581ca81 KC |
394 | } |
395 | ||
ca01d6dd | 396 | /* |
0eed84ff KC |
397 | * callback from kmsg_dump. Save as much as we can (up to kmsg_bytes) from the |
398 | * end of the buffer. | |
ca01d6dd TL |
399 | */ |
400 | static void pstore_dump(struct kmsg_dumper *dumper, | |
e2ae715d | 401 | enum kmsg_dump_reason reason) |
ca01d6dd | 402 | { |
e2ae715d | 403 | unsigned long total = 0; |
381b872c | 404 | const char *why; |
b94fdd07 | 405 | unsigned int part = 1; |
e2ae715d | 406 | int ret; |
ca01d6dd | 407 | |
381b872c | 408 | why = get_reason_str(reason); |
9f6af27f | 409 | |
ea84b580 KC |
410 | if (down_trylock(&psinfo->buf_lock)) { |
411 | /* Failed to acquire lock: give up if we cannot wait. */ | |
412 | if (pstore_cannot_wait(reason)) { | |
413 | pr_err("dump skipped in %s path: may corrupt error record\n", | |
414 | in_nmi() ? "NMI" : why); | |
415 | return; | |
416 | } | |
417 | if (down_interruptible(&psinfo->buf_lock)) { | |
418 | pr_err("could not grab semaphore?!\n"); | |
959217c8 | 419 | return; |
9f244e9c | 420 | } |
98e44fda | 421 | } |
ea84b580 | 422 | |
ca01d6dd TL |
423 | oopscount++; |
424 | while (total < kmsg_bytes) { | |
e2ae715d | 425 | char *dst; |
76cc9580 KC |
426 | size_t dst_size; |
427 | int header_size; | |
b0aad7a9 | 428 | int zipped_len = -1; |
76cc9580 | 429 | size_t dump_size; |
e581ca81 KC |
430 | struct pstore_record record; |
431 | ||
432 | pstore_record_init(&record, psinfo); | |
433 | record.type = PSTORE_TYPE_DMESG; | |
434 | record.count = oopscount; | |
435 | record.reason = reason; | |
436 | record.part = part; | |
437 | record.buf = psinfo->buf; | |
e2ae715d | 438 | |
ea84b580 | 439 | if (big_oops_buf) { |
b0aad7a9 | 440 | dst = big_oops_buf; |
76cc9580 | 441 | dst_size = big_oops_buf_sz; |
235f6d15 NK |
442 | } else { |
443 | dst = psinfo->buf; | |
76cc9580 | 444 | dst_size = psinfo->bufsize; |
235f6d15 NK |
445 | } |
446 | ||
76cc9580 KC |
447 | /* Write dump header. */ |
448 | header_size = snprintf(dst, dst_size, "%s#%d Part%u\n", why, | |
449 | oopscount, part); | |
450 | dst_size -= header_size; | |
ca01d6dd | 451 | |
76cc9580 KC |
452 | /* Write dump contents. */ |
453 | if (!kmsg_dump_get_buffer(dumper, true, dst + header_size, | |
454 | dst_size, &dump_size)) | |
235f6d15 | 455 | break; |
b0aad7a9 | 456 | |
ea84b580 | 457 | if (big_oops_buf) { |
b0aad7a9 | 458 | zipped_len = pstore_compress(dst, psinfo->buf, |
76cc9580 KC |
459 | header_size + dump_size, |
460 | psinfo->bufsize); | |
b0aad7a9 AB |
461 | |
462 | if (zipped_len > 0) { | |
76cc9580 KC |
463 | record.compressed = true; |
464 | record.size = zipped_len; | |
b0aad7a9 | 465 | } else { |
76cc9580 KC |
466 | record.size = copy_kmsg_to_buffer(header_size, |
467 | dump_size); | |
b0aad7a9 AB |
468 | } |
469 | } else { | |
76cc9580 | 470 | record.size = header_size + dump_size; |
b0aad7a9 | 471 | } |
ca01d6dd | 472 | |
76cc9580 | 473 | ret = psinfo->write(&record); |
b238b8fa | 474 | if (ret == 0 && reason == KMSG_DUMP_OOPS && pstore_is_mounted()) |
6dda9266 | 475 | pstore_new_entry = 1; |
e2ae715d | 476 | |
76cc9580 | 477 | total += record.size; |
56280682 | 478 | part++; |
ca01d6dd | 479 | } |
ea84b580 KC |
480 | |
481 | up(&psinfo->buf_lock); | |
ca01d6dd TL |
482 | } |
483 | ||
484 | static struct kmsg_dumper pstore_dumper = { | |
485 | .dump = pstore_dump, | |
486 | }; | |
487 | ||
306e5c2a GT |
488 | /* |
489 | * Register with kmsg_dump to save last part of console log on panic. | |
490 | */ | |
18730411 GT |
491 | static void pstore_register_kmsg(void) |
492 | { | |
493 | kmsg_dump_register(&pstore_dumper); | |
494 | } | |
495 | ||
ee1d2674 GT |
496 | static void pstore_unregister_kmsg(void) |
497 | { | |
498 | kmsg_dump_unregister(&pstore_dumper); | |
499 | } | |
500 | ||
f29e5956 AV |
501 | #ifdef CONFIG_PSTORE_CONSOLE |
502 | static void pstore_console_write(struct console *con, const char *s, unsigned c) | |
503 | { | |
b77fa617 | 504 | struct pstore_record record; |
f29e5956 | 505 | |
4c6c4d34 YH |
506 | if (!c) |
507 | return; | |
508 | ||
b77fa617 KC |
509 | pstore_record_init(&record, psinfo); |
510 | record.type = PSTORE_TYPE_CONSOLE; | |
80c9d03c | 511 | |
b77fa617 KC |
512 | record.buf = (char *)s; |
513 | record.size = c; | |
514 | psinfo->write(&record); | |
f29e5956 AV |
515 | } |
516 | ||
517 | static struct console pstore_console = { | |
518 | .name = "pstore", | |
519 | .write = pstore_console_write, | |
520 | .flags = CON_PRINTBUFFER | CON_ENABLED | CON_ANYTIME, | |
521 | .index = -1, | |
522 | }; | |
523 | ||
524 | static void pstore_register_console(void) | |
525 | { | |
526 | register_console(&pstore_console); | |
527 | } | |
ee1d2674 GT |
528 | |
529 | static void pstore_unregister_console(void) | |
530 | { | |
531 | unregister_console(&pstore_console); | |
532 | } | |
f29e5956 AV |
533 | #else |
534 | static void pstore_register_console(void) {} | |
ee1d2674 | 535 | static void pstore_unregister_console(void) {} |
f29e5956 AV |
536 | #endif |
537 | ||
4c9ec219 KC |
538 | static int pstore_write_user_compat(struct pstore_record *record, |
539 | const char __user *buf) | |
5bf6d1b9 | 540 | { |
30800d99 KC |
541 | int ret = 0; |
542 | ||
543 | if (record->buf) | |
544 | return -EINVAL; | |
545 | ||
077090af | 546 | record->buf = memdup_user(buf, record->size); |
dfd6fa39 | 547 | if (IS_ERR(record->buf)) { |
077090af | 548 | ret = PTR_ERR(record->buf); |
30800d99 | 549 | goto out; |
5bf6d1b9 | 550 | } |
30800d99 KC |
551 | |
552 | ret = record->psi->write(record); | |
553 | ||
30800d99 | 554 | kfree(record->buf); |
077090af | 555 | out: |
30800d99 KC |
556 | record->buf = NULL; |
557 | ||
558 | return unlikely(ret < 0) ? ret : record->size; | |
5bf6d1b9 MS |
559 | } |
560 | ||
ca01d6dd TL |
561 | /* |
562 | * platform specific persistent storage driver registers with | |
563 | * us here. If pstore is already mounted, call the platform | |
564 | * read function right away to populate the file system. If not | |
565 | * then the pstore mount code will call us later to fill out | |
566 | * the file system. | |
ca01d6dd TL |
567 | */ |
568 | int pstore_register(struct pstore_info *psi) | |
569 | { | |
570 | struct module *owner = psi->owner; | |
571 | ||
0d7cd09a KC |
572 | if (backend && strcmp(backend, psi->name)) { |
573 | pr_warn("ignoring unexpected backend '%s'\n", psi->name); | |
8e48b1a8 | 574 | return -EPERM; |
0d7cd09a | 575 | } |
8e48b1a8 | 576 | |
4c9ec219 KC |
577 | /* Sanity check flags. */ |
578 | if (!psi->flags) { | |
579 | pr_warn("backend '%s' must support at least one frontend\n", | |
580 | psi->name); | |
581 | return -EINVAL; | |
582 | } | |
583 | ||
584 | /* Check for required functions. */ | |
585 | if (!psi->read || !psi->write) { | |
586 | pr_warn("backend '%s' must implement read() and write()\n", | |
587 | psi->name); | |
588 | return -EINVAL; | |
589 | } | |
590 | ||
ca01d6dd TL |
591 | spin_lock(&pstore_lock); |
592 | if (psinfo) { | |
0d7cd09a KC |
593 | pr_warn("backend '%s' already loaded: ignoring '%s'\n", |
594 | psinfo->name, psi->name); | |
ca01d6dd TL |
595 | spin_unlock(&pstore_lock); |
596 | return -EBUSY; | |
597 | } | |
dee28e72 | 598 | |
4c9ec219 KC |
599 | if (!psi->write_user) |
600 | psi->write_user = pstore_write_user_compat; | |
ca01d6dd | 601 | psinfo = psi; |
f6f82851 | 602 | mutex_init(&psinfo->read_mutex); |
ea84b580 | 603 | sema_init(&psinfo->buf_lock, 1); |
ca01d6dd TL |
604 | spin_unlock(&pstore_lock); |
605 | ||
606 | if (owner && !try_module_get(owner)) { | |
607 | psinfo = NULL; | |
608 | return -EINVAL; | |
609 | } | |
610 | ||
b0aad7a9 AB |
611 | allocate_buf_for_compression(); |
612 | ||
ca01d6dd | 613 | if (pstore_is_mounted()) |
6dda9266 | 614 | pstore_get_records(0); |
ca01d6dd | 615 | |
c950fd6f NK |
616 | if (psi->flags & PSTORE_FLAGS_DMESG) |
617 | pstore_register_kmsg(); | |
618 | if (psi->flags & PSTORE_FLAGS_CONSOLE) | |
df36ac1b | 619 | pstore_register_console(); |
c950fd6f | 620 | if (psi->flags & PSTORE_FLAGS_FTRACE) |
df36ac1b | 621 | pstore_register_ftrace(); |
c950fd6f | 622 | if (psi->flags & PSTORE_FLAGS_PMSG) |
9d5438f4 | 623 | pstore_register_pmsg(); |
ca01d6dd | 624 | |
6330d553 | 625 | /* Start watching for new records, if desired. */ |
a3f5f075 AV |
626 | if (pstore_update_ms >= 0) { |
627 | pstore_timer.expires = jiffies + | |
628 | msecs_to_jiffies(pstore_update_ms); | |
629 | add_timer(&pstore_timer); | |
630 | } | |
6dda9266 | 631 | |
42222c2a WL |
632 | /* |
633 | * Update the module parameter backend, so it is visible | |
634 | * through /sys/module/pstore/parameters/backend | |
635 | */ | |
636 | backend = psi->name; | |
637 | ||
ef748853 | 638 | pr_info("Registered %s as persistent store backend\n", psi->name); |
8e48b1a8 | 639 | |
1344dd86 KC |
640 | module_put(owner); |
641 | ||
ca01d6dd TL |
642 | return 0; |
643 | } | |
644 | EXPORT_SYMBOL_GPL(pstore_register); | |
645 | ||
ee1d2674 GT |
646 | void pstore_unregister(struct pstore_info *psi) |
647 | { | |
6330d553 KC |
648 | /* Stop timer and make sure all work has finished. */ |
649 | pstore_update_ms = -1; | |
650 | del_timer_sync(&pstore_timer); | |
651 | flush_work(&pstore_work); | |
652 | ||
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 GT |
661 | |
662 | free_buf_for_compression(); | |
663 | ||
664 | psinfo = NULL; | |
665 | backend = NULL; | |
666 | } | |
667 | EXPORT_SYMBOL_GPL(pstore_unregister); | |
668 | ||
634f8f51 KC |
669 | static void decompress_record(struct pstore_record *record) |
670 | { | |
bdabc8e7 | 671 | int ret; |
634f8f51 | 672 | int unzipped_len; |
bdabc8e7 | 673 | char *unzipped, *workspace; |
634f8f51 | 674 | |
4a16d1cb AK |
675 | if (!record->compressed) |
676 | return; | |
677 | ||
634f8f51 | 678 | /* Only PSTORE_TYPE_DMESG support compression. */ |
4a16d1cb | 679 | if (record->type != PSTORE_TYPE_DMESG) { |
634f8f51 KC |
680 | pr_warn("ignored compressed record type %d\n", record->type); |
681 | return; | |
682 | } | |
683 | ||
bdabc8e7 | 684 | /* Missing compression buffer means compression was not initialized. */ |
634f8f51 | 685 | if (!big_oops_buf) { |
bdabc8e7 | 686 | pr_warn("no decompression method initialized!\n"); |
634f8f51 KC |
687 | return; |
688 | } | |
689 | ||
bdabc8e7 KC |
690 | /* Allocate enough space to hold max decompression and ECC. */ |
691 | unzipped_len = big_oops_buf_sz; | |
692 | workspace = kmalloc(unzipped_len + record->ecc_notice_size, | |
693 | GFP_KERNEL); | |
694 | if (!workspace) | |
7e8cc8dc | 695 | return; |
7e8cc8dc | 696 | |
bdabc8e7 KC |
697 | /* After decompression "unzipped_len" is almost certainly smaller. */ |
698 | ret = crypto_comp_decompress(tfm, record->buf, record->size, | |
699 | workspace, &unzipped_len); | |
700 | if (ret) { | |
701 | pr_err("crypto_comp_decompress failed, ret = %d!\n", ret); | |
702 | kfree(workspace); | |
7e8cc8dc KC |
703 | return; |
704 | } | |
7e8cc8dc KC |
705 | |
706 | /* Append ECC notice to decompressed buffer. */ | |
bdabc8e7 | 707 | memcpy(workspace + unzipped_len, record->buf + record->size, |
7e8cc8dc KC |
708 | record->ecc_notice_size); |
709 | ||
bdabc8e7 KC |
710 | /* Copy decompressed contents into an minimum-sized allocation. */ |
711 | unzipped = kmemdup(workspace, unzipped_len + record->ecc_notice_size, | |
712 | GFP_KERNEL); | |
713 | kfree(workspace); | |
714 | if (!unzipped) | |
715 | return; | |
716 | ||
717 | /* Swap out compressed contents with decompressed contents. */ | |
7e8cc8dc | 718 | kfree(record->buf); |
bdabc8e7 | 719 | record->buf = unzipped; |
7e8cc8dc KC |
720 | record->size = unzipped_len; |
721 | record->compressed = false; | |
634f8f51 KC |
722 | } |
723 | ||
ca01d6dd | 724 | /* |
3a7d2fd1 | 725 | * Read all the records from one persistent store backend. Create |
6dda9266 TL |
726 | * files in our filesystem. Don't warn about -EEXIST errors |
727 | * when we are re-scanning the backing store looking to add new | |
728 | * error records. | |
ca01d6dd | 729 | */ |
3a7d2fd1 KC |
730 | void pstore_get_backend_records(struct pstore_info *psi, |
731 | struct dentry *root, int quiet) | |
ca01d6dd | 732 | { |
2a2b0acf | 733 | int failed = 0; |
656de42e | 734 | unsigned int stop_loop = 65536; |
ca01d6dd | 735 | |
3a7d2fd1 | 736 | if (!psi || !root) |
ca01d6dd TL |
737 | return; |
738 | ||
f6f82851 | 739 | mutex_lock(&psi->read_mutex); |
2174f6df | 740 | if (psi->open && psi->open(psi)) |
06cf91b4 CG |
741 | goto out; |
742 | ||
1dfff7dd KC |
743 | /* |
744 | * Backend callback read() allocates record.buf. decompress_record() | |
745 | * may reallocate record.buf. On success, pstore_mkfile() will keep | |
746 | * the record.buf, so free it only on failure. | |
747 | */ | |
656de42e | 748 | for (; stop_loop; stop_loop--) { |
2a2b0acf KC |
749 | struct pstore_record *record; |
750 | int rc; | |
751 | ||
752 | record = kzalloc(sizeof(*record), GFP_KERNEL); | |
753 | if (!record) { | |
754 | pr_err("out of memory creating record\n"); | |
755 | break; | |
756 | } | |
e581ca81 | 757 | pstore_record_init(record, psi); |
2a2b0acf KC |
758 | |
759 | record->size = psi->read(record); | |
760 | ||
761 | /* No more records left in backend? */ | |
f6525b96 DA |
762 | if (record->size <= 0) { |
763 | kfree(record); | |
2a2b0acf | 764 | break; |
f6525b96 | 765 | } |
2a2b0acf KC |
766 | |
767 | decompress_record(record); | |
3a7d2fd1 | 768 | rc = pstore_mkfile(root, record); |
1dfff7dd | 769 | if (rc) { |
83f70f07 | 770 | /* pstore_mkfile() did not take record, so free it. */ |
2a2b0acf | 771 | kfree(record->buf); |
83f70f07 | 772 | kfree(record); |
1dfff7dd KC |
773 | if (rc != -EEXIST || !quiet) |
774 | failed++; | |
775 | } | |
ca01d6dd | 776 | } |
2174f6df KC |
777 | if (psi->close) |
778 | psi->close(psi); | |
06cf91b4 | 779 | out: |
f6f82851 | 780 | mutex_unlock(&psi->read_mutex); |
ca01d6dd TL |
781 | |
782 | if (failed) | |
656de42e | 783 | pr_warn("failed to create %d record(s) from '%s'\n", |
ef748853 | 784 | failed, psi->name); |
656de42e KC |
785 | if (!stop_loop) |
786 | pr_err("looping? Too many records seen from '%s'\n", | |
787 | psi->name); | |
ca01d6dd TL |
788 | } |
789 | ||
6dda9266 TL |
790 | static void pstore_dowork(struct work_struct *work) |
791 | { | |
792 | pstore_get_records(1); | |
793 | } | |
794 | ||
24ed960a | 795 | static void pstore_timefunc(struct timer_list *unused) |
6dda9266 TL |
796 | { |
797 | if (pstore_new_entry) { | |
798 | pstore_new_entry = 0; | |
799 | schedule_work(&pstore_work); | |
800 | } | |
801 | ||
6330d553 KC |
802 | if (pstore_update_ms >= 0) |
803 | mod_timer(&pstore_timer, | |
804 | jiffies + msecs_to_jiffies(pstore_update_ms)); | |
6dda9266 TL |
805 | } |
806 | ||
fe1d4758 KC |
807 | void __init pstore_choose_compression(void) |
808 | { | |
809 | const struct pstore_zbackend *step; | |
810 | ||
811 | if (!compress) | |
812 | return; | |
813 | ||
814 | for (step = zbackends; step->name; step++) { | |
815 | if (!strcmp(compress, step->name)) { | |
816 | zbackend = step; | |
fe1d4758 KC |
817 | return; |
818 | } | |
819 | } | |
820 | } | |
821 | ||
cb095afd KC |
822 | static int __init pstore_init(void) |
823 | { | |
824 | int ret; | |
825 | ||
826 | pstore_choose_compression(); | |
827 | ||
41603165 JFG |
828 | /* |
829 | * Check if any pstore backends registered earlier but did not | |
830 | * initialize compression because crypto was not ready. If so, | |
831 | * initialize compression now. | |
832 | */ | |
95047b05 | 833 | allocate_buf_for_compression(); |
41603165 | 834 | |
cb095afd KC |
835 | ret = pstore_init_fs(); |
836 | if (ret) | |
837 | return ret; | |
838 | ||
839 | return 0; | |
840 | } | |
41603165 | 841 | late_initcall(pstore_init); |
cb095afd KC |
842 | |
843 | static void __exit pstore_exit(void) | |
844 | { | |
845 | pstore_exit_fs(); | |
846 | } | |
847 | module_exit(pstore_exit) | |
848 | ||
fe1d4758 KC |
849 | module_param(compress, charp, 0444); |
850 | MODULE_PARM_DESC(compress, "Pstore compression to use"); | |
851 | ||
dee28e72 MG |
852 | module_param(backend, charp, 0444); |
853 | MODULE_PARM_DESC(backend, "Pstore backend to use"); | |
cb095afd KC |
854 | |
855 | MODULE_AUTHOR("Tony Luck <tony.luck@intel.com>"); | |
856 | MODULE_LICENSE("GPL"); |