]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - fs/pstore/ram.c
pstore: Remove superfluous memory size check
[mirror_ubuntu-bionic-kernel.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 int ramoops_read_kmsg_hdr(char *buffer, struct timespec *time,
145 bool *compressed)
146 {
147 char data_type;
148 int header_length = 0;
149
150 if (sscanf(buffer, RAMOOPS_KERNMSG_HDR "%lu.%lu-%c\n%n", &time->tv_sec,
151 &time->tv_nsec, &data_type, &header_length) == 3) {
152 if (data_type == 'C')
153 *compressed = true;
154 else
155 *compressed = false;
156 } else if (sscanf(buffer, RAMOOPS_KERNMSG_HDR "%lu.%lu\n%n",
157 &time->tv_sec, &time->tv_nsec, &header_length) == 2) {
158 *compressed = false;
159 } else {
160 time->tv_sec = 0;
161 time->tv_nsec = 0;
162 *compressed = false;
163 }
164 return header_length;
165 }
166
167 static ssize_t ramoops_pstore_read(u64 *id, enum pstore_type_id *type,
168 int *count, struct timespec *time,
169 char **buf, bool *compressed,
170 struct pstore_info *psi)
171 {
172 ssize_t size;
173 ssize_t ecc_notice_size;
174 struct ramoops_context *cxt = psi->data;
175 struct persistent_ram_zone *prz;
176 int header_length;
177
178 prz = ramoops_get_next_prz(cxt->przs, &cxt->dump_read_cnt,
179 cxt->max_dump_cnt, id, type,
180 PSTORE_TYPE_DMESG, 1);
181 if (!prz)
182 prz = ramoops_get_next_prz(&cxt->cprz, &cxt->console_read_cnt,
183 1, id, type, PSTORE_TYPE_CONSOLE, 0);
184 if (!prz)
185 prz = ramoops_get_next_prz(&cxt->fprz, &cxt->ftrace_read_cnt,
186 1, id, type, PSTORE_TYPE_FTRACE, 0);
187 if (!prz)
188 return 0;
189
190 if (!persistent_ram_old(prz))
191 return 0;
192
193 size = persistent_ram_old_size(prz);
194 header_length = ramoops_read_kmsg_hdr(persistent_ram_old(prz), time,
195 compressed);
196 size -= header_length;
197
198 /* ECC correction notice */
199 ecc_notice_size = persistent_ram_ecc_string(prz, NULL, 0);
200
201 *buf = kmalloc(size + ecc_notice_size + 1, GFP_KERNEL);
202 if (*buf == NULL)
203 return -ENOMEM;
204
205 memcpy(*buf, (char *)persistent_ram_old(prz) + header_length, size);
206 persistent_ram_ecc_string(prz, *buf + size, ecc_notice_size + 1);
207
208 return size + ecc_notice_size;
209 }
210
211 static size_t ramoops_write_kmsg_hdr(struct persistent_ram_zone *prz,
212 bool compressed)
213 {
214 char *hdr;
215 struct timespec timestamp;
216 size_t len;
217
218 /* Report zeroed timestamp if called before timekeeping has resumed. */
219 if (__getnstimeofday(&timestamp)) {
220 timestamp.tv_sec = 0;
221 timestamp.tv_nsec = 0;
222 }
223 hdr = kasprintf(GFP_ATOMIC, RAMOOPS_KERNMSG_HDR "%lu.%lu-%c\n",
224 (long)timestamp.tv_sec, (long)(timestamp.tv_nsec / 1000),
225 compressed ? 'C' : 'D');
226 WARN_ON_ONCE(!hdr);
227 len = hdr ? strlen(hdr) : 0;
228 persistent_ram_write(prz, hdr, len);
229 kfree(hdr);
230
231 return len;
232 }
233
234 static int notrace ramoops_pstore_write_buf(enum pstore_type_id type,
235 enum kmsg_dump_reason reason,
236 u64 *id, unsigned int part,
237 const char *buf,
238 bool compressed, size_t size,
239 struct pstore_info *psi)
240 {
241 struct ramoops_context *cxt = psi->data;
242 struct persistent_ram_zone *prz;
243 size_t hlen;
244
245 if (type == PSTORE_TYPE_CONSOLE) {
246 if (!cxt->cprz)
247 return -ENOMEM;
248 persistent_ram_write(cxt->cprz, buf, size);
249 return 0;
250 } else if (type == PSTORE_TYPE_FTRACE) {
251 if (!cxt->fprz)
252 return -ENOMEM;
253 persistent_ram_write(cxt->fprz, buf, size);
254 return 0;
255 }
256
257 if (type != PSTORE_TYPE_DMESG)
258 return -EINVAL;
259
260 /* Out of the various dmesg dump types, ramoops is currently designed
261 * to only store crash logs, rather than storing general kernel logs.
262 */
263 if (reason != KMSG_DUMP_OOPS &&
264 reason != KMSG_DUMP_PANIC)
265 return -EINVAL;
266
267 /* Skip Oopes when configured to do so. */
268 if (reason == KMSG_DUMP_OOPS && !cxt->dump_oops)
269 return -EINVAL;
270
271 /* Explicitly only take the first part of any new crash.
272 * If our buffer is larger than kmsg_bytes, this can never happen,
273 * and if our buffer is smaller than kmsg_bytes, we don't want the
274 * report split across multiple records.
275 */
276 if (part != 1)
277 return -ENOSPC;
278
279 if (!cxt->przs)
280 return -ENOSPC;
281
282 prz = cxt->przs[cxt->dump_write_cnt];
283
284 hlen = ramoops_write_kmsg_hdr(prz, compressed);
285 if (size + hlen > prz->buffer_size)
286 size = prz->buffer_size - hlen;
287 persistent_ram_write(prz, buf, size);
288
289 cxt->dump_write_cnt = (cxt->dump_write_cnt + 1) % cxt->max_dump_cnt;
290
291 return 0;
292 }
293
294 static int ramoops_pstore_erase(enum pstore_type_id type, u64 id, int count,
295 struct timespec time, struct pstore_info *psi)
296 {
297 struct ramoops_context *cxt = psi->data;
298 struct persistent_ram_zone *prz;
299
300 switch (type) {
301 case PSTORE_TYPE_DMESG:
302 if (id >= cxt->max_dump_cnt)
303 return -EINVAL;
304 prz = cxt->przs[id];
305 break;
306 case PSTORE_TYPE_CONSOLE:
307 prz = cxt->cprz;
308 break;
309 case PSTORE_TYPE_FTRACE:
310 prz = cxt->fprz;
311 break;
312 default:
313 return -EINVAL;
314 }
315
316 persistent_ram_free_old(prz);
317 persistent_ram_zap(prz);
318
319 return 0;
320 }
321
322 static struct ramoops_context oops_cxt = {
323 .pstore = {
324 .owner = THIS_MODULE,
325 .name = "ramoops",
326 .open = ramoops_pstore_open,
327 .read = ramoops_pstore_read,
328 .write_buf = ramoops_pstore_write_buf,
329 .erase = ramoops_pstore_erase,
330 },
331 };
332
333 static void ramoops_free_przs(struct ramoops_context *cxt)
334 {
335 int i;
336
337 cxt->max_dump_cnt = 0;
338 if (!cxt->przs)
339 return;
340
341 for (i = 0; !IS_ERR_OR_NULL(cxt->przs[i]); i++)
342 persistent_ram_free(cxt->przs[i]);
343 kfree(cxt->przs);
344 }
345
346 static int ramoops_init_przs(struct device *dev, struct ramoops_context *cxt,
347 phys_addr_t *paddr, size_t dump_mem_sz)
348 {
349 int err = -ENOMEM;
350 int i;
351
352 if (!cxt->record_size)
353 return 0;
354
355 if (*paddr + dump_mem_sz - cxt->phys_addr > cxt->size) {
356 dev_err(dev, "no room for dumps\n");
357 return -ENOMEM;
358 }
359
360 cxt->max_dump_cnt = dump_mem_sz / cxt->record_size;
361 if (!cxt->max_dump_cnt)
362 return -ENOMEM;
363
364 cxt->przs = kzalloc(sizeof(*cxt->przs) * cxt->max_dump_cnt,
365 GFP_KERNEL);
366 if (!cxt->przs) {
367 dev_err(dev, "failed to initialize a prz array for dumps\n");
368 goto fail_prz;
369 }
370
371 for (i = 0; i < cxt->max_dump_cnt; i++) {
372 size_t sz = cxt->record_size;
373
374 cxt->przs[i] = persistent_ram_new(*paddr, sz, 0,
375 &cxt->ecc_info,
376 cxt->memtype);
377 if (IS_ERR(cxt->przs[i])) {
378 err = PTR_ERR(cxt->przs[i]);
379 dev_err(dev, "failed to request mem region (0x%zx@0x%llx): %d\n",
380 sz, (unsigned long long)*paddr, err);
381 goto fail_prz;
382 }
383 *paddr += sz;
384 }
385
386 return 0;
387 fail_prz:
388 ramoops_free_przs(cxt);
389 return err;
390 }
391
392 static int ramoops_init_prz(struct device *dev, struct ramoops_context *cxt,
393 struct persistent_ram_zone **prz,
394 phys_addr_t *paddr, size_t sz, u32 sig)
395 {
396 if (!sz)
397 return 0;
398
399 if (*paddr + sz - cxt->phys_addr > cxt->size) {
400 dev_err(dev, "no room for mem region (0x%zx@0x%llx) in (0x%lx@0x%llx)\n",
401 sz, (unsigned long long)*paddr,
402 cxt->size, (unsigned long long)cxt->phys_addr);
403 return -ENOMEM;
404 }
405
406 *prz = persistent_ram_new(*paddr, sz, sig, &cxt->ecc_info, cxt->memtype);
407 if (IS_ERR(*prz)) {
408 int err = PTR_ERR(*prz);
409
410 dev_err(dev, "failed to request mem region (0x%zx@0x%llx): %d\n",
411 sz, (unsigned long long)*paddr, err);
412 return err;
413 }
414
415 persistent_ram_zap(*prz);
416
417 *paddr += sz;
418
419 return 0;
420 }
421
422 static int ramoops_probe(struct platform_device *pdev)
423 {
424 struct device *dev = &pdev->dev;
425 struct ramoops_platform_data *pdata = pdev->dev.platform_data;
426 struct ramoops_context *cxt = &oops_cxt;
427 size_t dump_mem_sz;
428 phys_addr_t paddr;
429 int err = -EINVAL;
430
431 /* Only a single ramoops area allowed at a time, so fail extra
432 * probes.
433 */
434 if (cxt->max_dump_cnt)
435 goto fail_out;
436
437 if (!pdata->mem_size || (!pdata->record_size && !pdata->console_size &&
438 !pdata->ftrace_size)) {
439 pr_err("The memory size and the record/console size must be "
440 "non-zero\n");
441 goto fail_out;
442 }
443
444 if (pdata->record_size && !is_power_of_2(pdata->record_size))
445 pdata->record_size = rounddown_pow_of_two(pdata->record_size);
446 if (pdata->console_size && !is_power_of_2(pdata->console_size))
447 pdata->console_size = rounddown_pow_of_two(pdata->console_size);
448 if (pdata->ftrace_size && !is_power_of_2(pdata->ftrace_size))
449 pdata->ftrace_size = rounddown_pow_of_two(pdata->ftrace_size);
450
451 cxt->size = pdata->mem_size;
452 cxt->phys_addr = pdata->mem_address;
453 cxt->memtype = pdata->mem_type;
454 cxt->record_size = pdata->record_size;
455 cxt->console_size = pdata->console_size;
456 cxt->ftrace_size = pdata->ftrace_size;
457 cxt->dump_oops = pdata->dump_oops;
458 cxt->ecc_info = pdata->ecc_info;
459
460 paddr = cxt->phys_addr;
461
462 dump_mem_sz = cxt->size - cxt->console_size - cxt->ftrace_size;
463 err = ramoops_init_przs(dev, cxt, &paddr, dump_mem_sz);
464 if (err)
465 goto fail_out;
466
467 err = ramoops_init_prz(dev, cxt, &cxt->cprz, &paddr,
468 cxt->console_size, 0);
469 if (err)
470 goto fail_init_cprz;
471
472 err = ramoops_init_prz(dev, cxt, &cxt->fprz, &paddr, cxt->ftrace_size,
473 LINUX_VERSION_CODE);
474 if (err)
475 goto fail_init_fprz;
476
477 cxt->pstore.data = cxt;
478 /*
479 * Console can handle any buffer size, so prefer LOG_LINE_MAX. If we
480 * have to handle dumps, we must have at least record_size buffer. And
481 * for ftrace, bufsize is irrelevant (if bufsize is 0, buf will be
482 * ZERO_SIZE_PTR).
483 */
484 if (cxt->console_size)
485 cxt->pstore.bufsize = 1024; /* LOG_LINE_MAX */
486 cxt->pstore.bufsize = max(cxt->record_size, cxt->pstore.bufsize);
487 cxt->pstore.buf = kmalloc(cxt->pstore.bufsize, GFP_KERNEL);
488 spin_lock_init(&cxt->pstore.buf_lock);
489 if (!cxt->pstore.buf) {
490 pr_err("cannot allocate pstore buffer\n");
491 err = -ENOMEM;
492 goto fail_clear;
493 }
494
495 err = pstore_register(&cxt->pstore);
496 if (err) {
497 pr_err("registering with pstore failed\n");
498 goto fail_buf;
499 }
500
501 /*
502 * Update the module parameter variables as well so they are visible
503 * through /sys/module/ramoops/parameters/
504 */
505 mem_size = pdata->mem_size;
506 mem_address = pdata->mem_address;
507 record_size = pdata->record_size;
508 dump_oops = pdata->dump_oops;
509
510 pr_info("attached 0x%lx@0x%llx, ecc: %d/%d\n",
511 cxt->size, (unsigned long long)cxt->phys_addr,
512 cxt->ecc_info.ecc_size, cxt->ecc_info.block_size);
513
514 return 0;
515
516 fail_buf:
517 kfree(cxt->pstore.buf);
518 fail_clear:
519 cxt->pstore.bufsize = 0;
520 kfree(cxt->fprz);
521 fail_init_fprz:
522 kfree(cxt->cprz);
523 fail_init_cprz:
524 ramoops_free_przs(cxt);
525 fail_out:
526 return err;
527 }
528
529 static int __exit ramoops_remove(struct platform_device *pdev)
530 {
531 #if 0
532 /* TODO(kees): We cannot unload ramoops since pstore doesn't support
533 * unregistering yet.
534 */
535 struct ramoops_context *cxt = &oops_cxt;
536
537 iounmap(cxt->virt_addr);
538 release_mem_region(cxt->phys_addr, cxt->size);
539 cxt->max_dump_cnt = 0;
540
541 /* TODO(kees): When pstore supports unregistering, call it here. */
542 kfree(cxt->pstore.buf);
543 cxt->pstore.bufsize = 0;
544
545 return 0;
546 #endif
547 return -EBUSY;
548 }
549
550 static struct platform_driver ramoops_driver = {
551 .probe = ramoops_probe,
552 .remove = __exit_p(ramoops_remove),
553 .driver = {
554 .name = "ramoops",
555 },
556 };
557
558 static void ramoops_register_dummy(void)
559 {
560 if (!mem_size)
561 return;
562
563 pr_info("using module parameters\n");
564
565 dummy_data = kzalloc(sizeof(*dummy_data), GFP_KERNEL);
566 if (!dummy_data) {
567 pr_info("could not allocate pdata\n");
568 return;
569 }
570
571 dummy_data->mem_size = mem_size;
572 dummy_data->mem_address = mem_address;
573 dummy_data->mem_type = 0;
574 dummy_data->record_size = record_size;
575 dummy_data->console_size = ramoops_console_size;
576 dummy_data->ftrace_size = ramoops_ftrace_size;
577 dummy_data->dump_oops = dump_oops;
578 /*
579 * For backwards compatibility ramoops.ecc=1 means 16 bytes ECC
580 * (using 1 byte for ECC isn't much of use anyway).
581 */
582 dummy_data->ecc_info.ecc_size = ramoops_ecc == 1 ? 16 : ramoops_ecc;
583
584 dummy = platform_device_register_data(NULL, "ramoops", -1,
585 dummy_data, sizeof(struct ramoops_platform_data));
586 if (IS_ERR(dummy)) {
587 pr_info("could not create platform device: %ld\n",
588 PTR_ERR(dummy));
589 }
590 }
591
592 static int __init ramoops_init(void)
593 {
594 ramoops_register_dummy();
595 return platform_driver_register(&ramoops_driver);
596 }
597 postcore_initcall(ramoops_init);
598
599 static void __exit ramoops_exit(void)
600 {
601 platform_driver_unregister(&ramoops_driver);
602 platform_device_unregister(dummy);
603 kfree(dummy_data);
604 }
605 module_exit(ramoops_exit);
606
607 MODULE_LICENSE("GPL");
608 MODULE_AUTHOR("Marco Stornelli <marco.stornelli@gmail.com>");
609 MODULE_DESCRIPTION("RAM Oops/Panic logger/driver");