xref: /openbmc/linux/fs/pstore/ram.c (revision c819e2cf)
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 	if (!cxt->przs && !cxt->cprz && !cxt->fprz) {
478 		pr_err("memory size too small, minimum is %zu\n",
479 			cxt->console_size + cxt->record_size +
480 			cxt->ftrace_size);
481 		err = -EINVAL;
482 		goto fail_cnt;
483 	}
484 
485 	cxt->pstore.data = cxt;
486 	/*
487 	 * Console can handle any buffer size, so prefer LOG_LINE_MAX. If we
488 	 * have to handle dumps, we must have at least record_size buffer. And
489 	 * for ftrace, bufsize is irrelevant (if bufsize is 0, buf will be
490 	 * ZERO_SIZE_PTR).
491 	 */
492 	if (cxt->console_size)
493 		cxt->pstore.bufsize = 1024; /* LOG_LINE_MAX */
494 	cxt->pstore.bufsize = max(cxt->record_size, cxt->pstore.bufsize);
495 	cxt->pstore.buf = kmalloc(cxt->pstore.bufsize, GFP_KERNEL);
496 	spin_lock_init(&cxt->pstore.buf_lock);
497 	if (!cxt->pstore.buf) {
498 		pr_err("cannot allocate pstore buffer\n");
499 		err = -ENOMEM;
500 		goto fail_clear;
501 	}
502 
503 	err = pstore_register(&cxt->pstore);
504 	if (err) {
505 		pr_err("registering with pstore failed\n");
506 		goto fail_buf;
507 	}
508 
509 	/*
510 	 * Update the module parameter variables as well so they are visible
511 	 * through /sys/module/ramoops/parameters/
512 	 */
513 	mem_size = pdata->mem_size;
514 	mem_address = pdata->mem_address;
515 	record_size = pdata->record_size;
516 	dump_oops = pdata->dump_oops;
517 
518 	pr_info("attached 0x%lx@0x%llx, ecc: %d/%d\n",
519 		cxt->size, (unsigned long long)cxt->phys_addr,
520 		cxt->ecc_info.ecc_size, cxt->ecc_info.block_size);
521 
522 	return 0;
523 
524 fail_buf:
525 	kfree(cxt->pstore.buf);
526 fail_clear:
527 	cxt->pstore.bufsize = 0;
528 fail_cnt:
529 	kfree(cxt->fprz);
530 fail_init_fprz:
531 	kfree(cxt->cprz);
532 fail_init_cprz:
533 	ramoops_free_przs(cxt);
534 fail_out:
535 	return err;
536 }
537 
538 static int __exit ramoops_remove(struct platform_device *pdev)
539 {
540 #if 0
541 	/* TODO(kees): We cannot unload ramoops since pstore doesn't support
542 	 * unregistering yet.
543 	 */
544 	struct ramoops_context *cxt = &oops_cxt;
545 
546 	iounmap(cxt->virt_addr);
547 	release_mem_region(cxt->phys_addr, cxt->size);
548 	cxt->max_dump_cnt = 0;
549 
550 	/* TODO(kees): When pstore supports unregistering, call it here. */
551 	kfree(cxt->pstore.buf);
552 	cxt->pstore.bufsize = 0;
553 
554 	return 0;
555 #endif
556 	return -EBUSY;
557 }
558 
559 static struct platform_driver ramoops_driver = {
560 	.probe		= ramoops_probe,
561 	.remove		= __exit_p(ramoops_remove),
562 	.driver		= {
563 		.name	= "ramoops",
564 	},
565 };
566 
567 static void ramoops_register_dummy(void)
568 {
569 	if (!mem_size)
570 		return;
571 
572 	pr_info("using module parameters\n");
573 
574 	dummy_data = kzalloc(sizeof(*dummy_data), GFP_KERNEL);
575 	if (!dummy_data) {
576 		pr_info("could not allocate pdata\n");
577 		return;
578 	}
579 
580 	dummy_data->mem_size = mem_size;
581 	dummy_data->mem_address = mem_address;
582 	dummy_data->mem_type = 0;
583 	dummy_data->record_size = record_size;
584 	dummy_data->console_size = ramoops_console_size;
585 	dummy_data->ftrace_size = ramoops_ftrace_size;
586 	dummy_data->dump_oops = dump_oops;
587 	/*
588 	 * For backwards compatibility ramoops.ecc=1 means 16 bytes ECC
589 	 * (using 1 byte for ECC isn't much of use anyway).
590 	 */
591 	dummy_data->ecc_info.ecc_size = ramoops_ecc == 1 ? 16 : ramoops_ecc;
592 
593 	dummy = platform_device_register_data(NULL, "ramoops", -1,
594 			dummy_data, sizeof(struct ramoops_platform_data));
595 	if (IS_ERR(dummy)) {
596 		pr_info("could not create platform device: %ld\n",
597 			PTR_ERR(dummy));
598 	}
599 }
600 
601 static int __init ramoops_init(void)
602 {
603 	ramoops_register_dummy();
604 	return platform_driver_register(&ramoops_driver);
605 }
606 postcore_initcall(ramoops_init);
607 
608 static void __exit ramoops_exit(void)
609 {
610 	platform_driver_unregister(&ramoops_driver);
611 	platform_device_unregister(dummy);
612 	kfree(dummy_data);
613 }
614 module_exit(ramoops_exit);
615 
616 MODULE_LICENSE("GPL");
617 MODULE_AUTHOR("Marco Stornelli <marco.stornelli@gmail.com>");
618 MODULE_DESCRIPTION("RAM Oops/Panic logger/driver");
619