Merge tag 'v3.10.68' into update
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / fs / pstore / ram.c
1 /*
2 * RAM Oops/Panic logger
3 *
4 * Copyright (C) 2010 Marco Stornelli <marco.stornelli@gmail.com>
5 * Copyright (C) 2011 Kees Cook <keescook@chromium.org>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * version 2 as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * 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., 51 Franklin St, Fifth Floor, Boston, MA
19 * 02110-1301 USA
20 *
21 */
22
23 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
24
25 #include <linux/kernel.h>
26 #include <linux/err.h>
27 #include <linux/module.h>
28 #include <linux/version.h>
29 #include <linux/pstore.h>
30 #include <linux/time.h>
31 #include <linux/io.h>
32 #include <linux/ioport.h>
33 #include <linux/platform_device.h>
34 #include <linux/slab.h>
35 #include <linux/compiler.h>
36 #include <linux/pstore_ram.h>
37
38 #define RAMOOPS_KERNMSG_HDR "===="
39 #define MIN_MEM_SIZE 4096UL
40
41 static ulong record_size = MIN_MEM_SIZE;
42 module_param(record_size, ulong, 0400);
43 MODULE_PARM_DESC(record_size,
44 "size of each dump done on oops/panic");
45
46 static ulong ramoops_console_size = MIN_MEM_SIZE;
47 module_param_named(console_size, ramoops_console_size, ulong, 0400);
48 MODULE_PARM_DESC(console_size, "size of kernel console log");
49
50 static ulong ramoops_ftrace_size = MIN_MEM_SIZE;
51 module_param_named(ftrace_size, ramoops_ftrace_size, ulong, 0400);
52 MODULE_PARM_DESC(ftrace_size, "size of ftrace log");
53
54 static ulong mem_address;
55 module_param(mem_address, ulong, 0400);
56 MODULE_PARM_DESC(mem_address,
57 "start of reserved RAM used to store oops/panic logs");
58
59 static ulong mem_size;
60 module_param(mem_size, ulong, 0400);
61 MODULE_PARM_DESC(mem_size,
62 "size of reserved RAM used to store oops/panic logs");
63
64 static unsigned int mem_type;
65 module_param(mem_type, uint, 0600);
66 MODULE_PARM_DESC(mem_type,
67 "set to 1 to try to use unbuffered memory (default 0)");
68
69 static int dump_oops = 1;
70 module_param(dump_oops, int, 0600);
71 MODULE_PARM_DESC(dump_oops,
72 "set to 1 to dump oopses, 0 to only dump panics (default 1)");
73
74 static int ramoops_ecc;
75 module_param_named(ecc, ramoops_ecc, int, 0600);
76 MODULE_PARM_DESC(ramoops_ecc,
77 "if non-zero, the option enables ECC support and specifies "
78 "ECC buffer size in bytes (1 is a special value, means 16 "
79 "bytes ECC)");
80
81 struct ramoops_context {
82 struct persistent_ram_zone **przs;
83 struct persistent_ram_zone *cprz;
84 struct persistent_ram_zone *fprz;
85 phys_addr_t phys_addr;
86 unsigned long size;
87 unsigned int memtype;
88 size_t record_size;
89 size_t console_size;
90 size_t ftrace_size;
91 int dump_oops;
92 struct persistent_ram_ecc_info ecc_info;
93 unsigned int max_dump_cnt;
94 unsigned int dump_write_cnt;
95 /* _read_cnt need clear on ramoops_pstore_open */
96 unsigned int dump_read_cnt;
97 unsigned int console_read_cnt;
98 unsigned int ftrace_read_cnt;
99 struct pstore_info pstore;
100 };
101
102 static struct platform_device *dummy;
103 static struct ramoops_platform_data *dummy_data;
104
105 static int ramoops_pstore_open(struct pstore_info *psi)
106 {
107 struct ramoops_context *cxt = psi->data;
108
109 cxt->dump_read_cnt = 0;
110 cxt->console_read_cnt = 0;
111 cxt->ftrace_read_cnt = 0;
112 return 0;
113 }
114
115 static struct persistent_ram_zone *
116 ramoops_get_next_prz(struct persistent_ram_zone *przs[], uint *c, uint max,
117 u64 *id,
118 enum pstore_type_id *typep, enum pstore_type_id type,
119 bool update)
120 {
121 struct persistent_ram_zone *prz;
122 int i = (*c)++;
123
124 if (i >= max)
125 return NULL;
126
127 prz = przs[i];
128 if (!prz)
129 return NULL;
130
131 /* Update old/shadowed buffer. */
132 if (update)
133 persistent_ram_save_old(prz);
134
135 if (!persistent_ram_old_size(prz))
136 return NULL;
137
138 *typep = type;
139 *id = i;
140
141 return prz;
142 }
143
144 static ssize_t ramoops_pstore_read(u64 *id, enum pstore_type_id *type,
145 int *count, struct timespec *time,
146 char **buf, struct pstore_info *psi)
147 {
148 ssize_t size;
149 ssize_t ecc_notice_size;
150 struct ramoops_context *cxt = psi->data;
151 struct persistent_ram_zone *prz;
152
153 prz = ramoops_get_next_prz(cxt->przs, &cxt->dump_read_cnt,
154 cxt->max_dump_cnt, id, type,
155 PSTORE_TYPE_DMESG, 1);
156 if (!prz)
157 prz = ramoops_get_next_prz(&cxt->cprz, &cxt->console_read_cnt,
158 1, id, type, PSTORE_TYPE_CONSOLE, 0);
159 if (!prz)
160 prz = ramoops_get_next_prz(&cxt->fprz, &cxt->ftrace_read_cnt,
161 1, id, type, PSTORE_TYPE_FTRACE, 0);
162 if (!prz)
163 return 0;
164
165 /* TODO(kees): Bogus time for the moment. */
166 time->tv_sec = 0;
167 time->tv_nsec = 0;
168
169 size = persistent_ram_old_size(prz);
170
171 /* ECC correction notice */
172 ecc_notice_size = persistent_ram_ecc_string(prz, NULL, 0);
173
174 *buf = kmalloc(size + ecc_notice_size + 1, GFP_KERNEL);
175 if (*buf == NULL)
176 return -ENOMEM;
177
178 memcpy(*buf, persistent_ram_old(prz), size);
179 persistent_ram_ecc_string(prz, *buf + size, ecc_notice_size + 1);
180
181 return size + ecc_notice_size;
182 }
183
184 static size_t ramoops_write_kmsg_hdr(struct persistent_ram_zone *prz)
185 {
186 char *hdr;
187 struct timespec timestamp;
188 size_t len;
189
190 /* Report zeroed timestamp if called before timekeeping has resumed. */
191 if (__getnstimeofday(&timestamp)) {
192 timestamp.tv_sec = 0;
193 timestamp.tv_nsec = 0;
194 }
195 hdr = kasprintf(GFP_ATOMIC, RAMOOPS_KERNMSG_HDR "%lu.%lu\n",
196 (long)timestamp.tv_sec, (long)(timestamp.tv_nsec / 1000));
197 WARN_ON_ONCE(!hdr);
198 len = hdr ? strlen(hdr) : 0;
199 persistent_ram_write(prz, hdr, len);
200 kfree(hdr);
201
202 return len;
203 }
204
205 static int notrace ramoops_pstore_write_buf(enum pstore_type_id type,
206 enum kmsg_dump_reason reason,
207 u64 *id, unsigned int part,
208 const char *buf, size_t size,
209 struct pstore_info *psi)
210 {
211 struct ramoops_context *cxt = psi->data;
212 struct persistent_ram_zone *prz;
213 size_t hlen;
214
215 if (type == PSTORE_TYPE_CONSOLE) {
216 if (!cxt->cprz)
217 return -ENOMEM;
218 persistent_ram_write(cxt->cprz, buf, size);
219 return 0;
220 } else if (type == PSTORE_TYPE_FTRACE) {
221 if (!cxt->fprz)
222 return -ENOMEM;
223 persistent_ram_write(cxt->fprz, buf, size);
224 return 0;
225 }
226
227 if (type != PSTORE_TYPE_DMESG)
228 return -EINVAL;
229
230 /* Out of the various dmesg dump types, ramoops is currently designed
231 * to only store crash logs, rather than storing general kernel logs.
232 */
233 if (reason != KMSG_DUMP_OOPS &&
234 reason != KMSG_DUMP_PANIC)
235 return -EINVAL;
236
237 /* Skip Oopes when configured to do so. */
238 if (reason == KMSG_DUMP_OOPS && !cxt->dump_oops)
239 return -EINVAL;
240
241 /* Explicitly only take the first part of any new crash.
242 * If our buffer is larger than kmsg_bytes, this can never happen,
243 * and if our buffer is smaller than kmsg_bytes, we don't want the
244 * report split across multiple records.
245 */
246 if (part != 1)
247 return -ENOSPC;
248
249 if (!cxt->przs)
250 return -ENOSPC;
251
252 prz = cxt->przs[cxt->dump_write_cnt];
253
254 hlen = ramoops_write_kmsg_hdr(prz);
255 if (size + hlen > prz->buffer_size)
256 size = prz->buffer_size - hlen;
257 persistent_ram_write(prz, buf, size);
258
259 cxt->dump_write_cnt = (cxt->dump_write_cnt + 1) % cxt->max_dump_cnt;
260
261 return 0;
262 }
263
264 static int ramoops_pstore_erase(enum pstore_type_id type, u64 id, int count,
265 struct timespec time, struct pstore_info *psi)
266 {
267 struct ramoops_context *cxt = psi->data;
268 struct persistent_ram_zone *prz;
269
270 switch (type) {
271 case PSTORE_TYPE_DMESG:
272 if (id >= cxt->max_dump_cnt)
273 return -EINVAL;
274 prz = cxt->przs[id];
275 break;
276 case PSTORE_TYPE_CONSOLE:
277 prz = cxt->cprz;
278 break;
279 case PSTORE_TYPE_FTRACE:
280 prz = cxt->fprz;
281 break;
282 default:
283 return -EINVAL;
284 }
285
286 persistent_ram_free_old(prz);
287 persistent_ram_zap(prz);
288
289 return 0;
290 }
291
292 static struct ramoops_context oops_cxt = {
293 .pstore = {
294 .owner = THIS_MODULE,
295 .name = "ramoops",
296 .open = ramoops_pstore_open,
297 .read = ramoops_pstore_read,
298 .write_buf = ramoops_pstore_write_buf,
299 .erase = ramoops_pstore_erase,
300 },
301 };
302
303 static void ramoops_free_przs(struct ramoops_context *cxt)
304 {
305 int i;
306
307 if (!cxt->przs)
308 return;
309
310 for (i = 0; !IS_ERR_OR_NULL(cxt->przs[i]); i++)
311 persistent_ram_free(cxt->przs[i]);
312 kfree(cxt->przs);
313 }
314
315 static int ramoops_init_przs(struct device *dev, struct ramoops_context *cxt,
316 phys_addr_t *paddr, size_t dump_mem_sz)
317 {
318 int err = -ENOMEM;
319 int i;
320
321 if (!cxt->record_size)
322 return 0;
323
324 if (*paddr + dump_mem_sz - cxt->phys_addr > cxt->size) {
325 dev_err(dev, "no room for dumps\n");
326 return -ENOMEM;
327 }
328
329 cxt->max_dump_cnt = dump_mem_sz / cxt->record_size;
330 if (!cxt->max_dump_cnt)
331 return -ENOMEM;
332
333 cxt->przs = kzalloc(sizeof(*cxt->przs) * cxt->max_dump_cnt,
334 GFP_KERNEL);
335 if (!cxt->przs) {
336 dev_err(dev, "failed to initialize a prz array for dumps\n");
337 return -ENOMEM;
338 }
339
340 for (i = 0; i < cxt->max_dump_cnt; i++) {
341 size_t sz = cxt->record_size;
342
343 cxt->przs[i] = persistent_ram_new(*paddr, sz, 0,
344 &cxt->ecc_info,
345 cxt->memtype);
346 if (IS_ERR(cxt->przs[i])) {
347 err = PTR_ERR(cxt->przs[i]);
348 dev_err(dev, "failed to request mem region (0x%zx@0x%llx): %d\n",
349 sz, (unsigned long long)*paddr, err);
350 goto fail_prz;
351 }
352 *paddr += sz;
353 }
354
355 return 0;
356 fail_prz:
357 ramoops_free_przs(cxt);
358 return err;
359 }
360
361 static int ramoops_init_prz(struct device *dev, struct ramoops_context *cxt,
362 struct persistent_ram_zone **prz,
363 phys_addr_t *paddr, size_t sz, u32 sig)
364 {
365 if (!sz)
366 return 0;
367
368 if (*paddr + sz - cxt->phys_addr > cxt->size) {
369 dev_err(dev, "no room for mem region (0x%zx@0x%llx) in (0x%lx@0x%llx)\n",
370 sz, (unsigned long long)*paddr,
371 cxt->size, (unsigned long long)cxt->phys_addr);
372 return -ENOMEM;
373 }
374
375 *prz = persistent_ram_new(*paddr, sz, sig, &cxt->ecc_info, cxt->memtype);
376 if (IS_ERR(*prz)) {
377 int err = PTR_ERR(*prz);
378
379 dev_err(dev, "failed to request mem region (0x%zx@0x%llx): %d\n",
380 sz, (unsigned long long)*paddr, err);
381 return err;
382 }
383
384 persistent_ram_zap(*prz);
385
386 *paddr += sz;
387
388 return 0;
389 }
390
391 void notrace ramoops_console_write_buf(const char *buf, size_t size)
392 {
393 struct ramoops_context *cxt = &oops_cxt;
394 persistent_ram_write(cxt->cprz, buf, size);
395 }
396
397 static int ramoops_probe(struct platform_device *pdev)
398 {
399 struct device *dev = &pdev->dev;
400 struct ramoops_platform_data *pdata = pdev->dev.platform_data;
401 struct ramoops_context *cxt = &oops_cxt;
402 size_t dump_mem_sz;
403 phys_addr_t paddr;
404 int err = -EINVAL;
405
406 /* Only a single ramoops area allowed at a time, so fail extra
407 * probes.
408 */
409 if (cxt->max_dump_cnt)
410 goto fail_out;
411
412 if (!pdata->mem_size || (!pdata->record_size && !pdata->console_size &&
413 !pdata->ftrace_size)) {
414 pr_err("The memory size and the record/console size must be "
415 "non-zero\n");
416 goto fail_out;
417 }
418
419 if (!is_power_of_2(pdata->mem_size))
420 pdata->mem_size = rounddown_pow_of_two(pdata->mem_size);
421 if (!is_power_of_2(pdata->record_size))
422 pdata->record_size = rounddown_pow_of_two(pdata->record_size);
423 if (!is_power_of_2(pdata->console_size))
424 pdata->console_size = rounddown_pow_of_two(pdata->console_size);
425 if (!is_power_of_2(pdata->ftrace_size))
426 pdata->ftrace_size = rounddown_pow_of_two(pdata->ftrace_size);
427
428 cxt->size = pdata->mem_size;
429 cxt->phys_addr = pdata->mem_address;
430 cxt->memtype = pdata->mem_type;
431 cxt->record_size = pdata->record_size;
432 cxt->console_size = pdata->console_size;
433 cxt->ftrace_size = pdata->ftrace_size;
434 cxt->dump_oops = pdata->dump_oops;
435 cxt->ecc_info = pdata->ecc_info;
436
437 paddr = cxt->phys_addr;
438
439 dump_mem_sz = cxt->size - cxt->console_size - cxt->ftrace_size;
440 err = ramoops_init_przs(dev, cxt, &paddr, dump_mem_sz);
441 if (err)
442 goto fail_out;
443
444 err = ramoops_init_prz(dev, cxt, &cxt->cprz, &paddr,
445 cxt->console_size, 0);
446 if (err)
447 goto fail_init_cprz;
448
449 err = ramoops_init_prz(dev, cxt, &cxt->fprz, &paddr, cxt->ftrace_size,
450 LINUX_VERSION_CODE);
451 if (err)
452 goto fail_init_fprz;
453
454 if (!cxt->przs && !cxt->cprz && !cxt->fprz) {
455 pr_err("memory size too small, minimum is %zu\n",
456 cxt->console_size + cxt->record_size +
457 cxt->ftrace_size);
458 err = -EINVAL;
459 goto fail_cnt;
460 }
461
462 cxt->pstore.data = cxt;
463 /*
464 * Console can handle any buffer size, so prefer LOG_LINE_MAX. If we
465 * have to handle dumps, we must have at least record_size buffer. And
466 * for ftrace, bufsize is irrelevant (if bufsize is 0, buf will be
467 * ZERO_SIZE_PTR).
468 */
469 if (cxt->console_size)
470 cxt->pstore.bufsize = 1024; /* LOG_LINE_MAX */
471 cxt->pstore.bufsize = max(cxt->record_size, cxt->pstore.bufsize);
472 cxt->pstore.buf = kmalloc(cxt->pstore.bufsize, GFP_KERNEL);
473 spin_lock_init(&cxt->pstore.buf_lock);
474 if (!cxt->pstore.buf) {
475 pr_err("cannot allocate pstore buffer\n");
476 err = -ENOMEM;
477 goto fail_clear;
478 }
479
480 err = pstore_register(&cxt->pstore);
481 if (err) {
482 pr_err("registering with pstore failed\n");
483 goto fail_buf;
484 }
485
486 /*
487 * Update the module parameter variables as well so they are visible
488 * through /sys/module/ramoops/parameters/
489 */
490 mem_size = pdata->mem_size;
491 mem_address = pdata->mem_address;
492 record_size = pdata->record_size;
493 dump_oops = pdata->dump_oops;
494
495 pr_info("attached 0x%lx@0x%llx, ecc: %d/%d\n",
496 cxt->size, (unsigned long long)cxt->phys_addr,
497 cxt->ecc_info.ecc_size, cxt->ecc_info.block_size);
498
499 return 0;
500
501 fail_buf:
502 kfree(cxt->pstore.buf);
503 fail_clear:
504 cxt->pstore.bufsize = 0;
505 cxt->max_dump_cnt = 0;
506 fail_cnt:
507 kfree(cxt->fprz);
508 fail_init_fprz:
509 kfree(cxt->cprz);
510 fail_init_cprz:
511 ramoops_free_przs(cxt);
512 fail_out:
513 return err;
514 }
515
516 static int __exit ramoops_remove(struct platform_device *pdev)
517 {
518 #if 0
519 /* TODO(kees): We cannot unload ramoops since pstore doesn't support
520 * unregistering yet.
521 */
522 struct ramoops_context *cxt = &oops_cxt;
523
524 iounmap(cxt->virt_addr);
525 release_mem_region(cxt->phys_addr, cxt->size);
526 cxt->max_dump_cnt = 0;
527
528 /* TODO(kees): When pstore supports unregistering, call it here. */
529 kfree(cxt->pstore.buf);
530 cxt->pstore.bufsize = 0;
531
532 return 0;
533 #endif
534 return -EBUSY;
535 }
536
537 static struct platform_driver ramoops_driver = {
538 .probe = ramoops_probe,
539 .remove = __exit_p(ramoops_remove),
540 .driver = {
541 .name = "ramoops",
542 .owner = THIS_MODULE,
543 },
544 };
545
546 static void ramoops_register_dummy(void)
547 {
548 if (!mem_size)
549 return;
550
551 pr_info("using module parameters\n");
552
553 dummy_data = kzalloc(sizeof(*dummy_data), GFP_KERNEL);
554 if (!dummy_data) {
555 pr_info("could not allocate pdata\n");
556 return;
557 }
558
559 dummy_data->mem_size = mem_size;
560 dummy_data->mem_address = mem_address;
561 dummy_data->mem_type = 0;
562 dummy_data->record_size = record_size;
563 dummy_data->console_size = ramoops_console_size;
564 dummy_data->ftrace_size = ramoops_ftrace_size;
565 dummy_data->dump_oops = dump_oops;
566 /*
567 * For backwards compatibility ramoops.ecc=1 means 16 bytes ECC
568 * (using 1 byte for ECC isn't much of use anyway).
569 */
570 dummy_data->ecc_info.ecc_size = ramoops_ecc == 1 ? 16 : ramoops_ecc;
571
572 dummy = platform_device_register_data(NULL, "ramoops", -1,
573 dummy_data, sizeof(struct ramoops_platform_data));
574 if (IS_ERR(dummy)) {
575 pr_info("could not create platform device: %ld\n",
576 PTR_ERR(dummy));
577 }
578 }
579
580 static int __init ramoops_init(void)
581 {
582 ramoops_register_dummy();
583 return platform_driver_register(&ramoops_driver);
584 }
585 postcore_initcall(ramoops_init);
586
587 static void __exit ramoops_exit(void)
588 {
589 platform_driver_unregister(&ramoops_driver);
590 platform_device_unregister(dummy);
591 kfree(dummy_data);
592 }
593 module_exit(ramoops_exit);
594
595 MODULE_LICENSE("GPL");
596 MODULE_AUTHOR("Marco Stornelli <marco.stornelli@gmail.com>");
597 MODULE_DESCRIPTION("RAM Oops/Panic logger/driver");