1 From 6b1ab74a9917012d0c559edc4ed299d9228ac89f Mon Sep 17 00:00:00 2001
2 From: Felix Fietkau <nbd@nbd.name>
3 Date: Sat, 8 Jul 2017 08:26:47 +0200
4 Subject: kernel: add the new 'crashlog' feature
6 this tries to store kernel oops/panic logs in a fixed location in RAM to
7 recover them available to user space using debugfs
9 Signed-off-by: Felix Fietkau <nbd@nbd.name>
11 include/linux/crashlog.h | 17 ++++
14 kernel/crashlog.c | 213 +++++++++++++++++++++++++++++++++++++++++++++++
18 7 files changed, 245 insertions(+)
19 create mode 100644 include/linux/crashlog.h
20 create mode 100644 kernel/crashlog.c
23 +++ b/include/linux/crashlog.h
28 +#ifdef CONFIG_CRASHLOG
29 +void crashlog_init_bootmem(struct bootmem_data *bdata);
30 +void crashlog_init_memblock(phys_addr_t addr, phys_addr_t size);
32 +static inline void crashlog_init_bootmem(struct bootmem_data *bdata)
36 +static inline void crashlog_init_memblock(phys_addr_t addr, phys_addr_t size)
44 @@ -1298,6 +1298,10 @@ config RELAY
49 + bool "Crash logging"
50 + depends on (!NO_BOOTMEM || HAVE_MEMBLOCK)
53 bool "Initial RAM filesystem and RAM disk (initramfs/initrd) support"
54 depends on BROKEN || !FRV
57 @@ -113,6 +113,7 @@ obj-$(CONFIG_TORTURE_TEST) += torture.o
58 obj-$(CONFIG_MEMBARRIER) += membarrier.o
60 obj-$(CONFIG_HAS_IOMEM) += memremap.o
61 +obj-$(CONFIG_CRASHLOG) += crashlog.o
63 $(obj)/configs.o: $(obj)/config_data.h
66 +++ b/kernel/crashlog.c
69 + * Crash information logger
70 + * Copyright (C) 2010 Felix Fietkau <nbd@nbd.name>
72 + * Based on ramoops.c
73 + * Copyright (C) 2010 Marco Stornelli <marco.stornelli@gmail.com>
75 + * This program is free software; you can redistribute it and/or
76 + * modify it under the terms of the GNU General Public License
77 + * version 2 as published by the Free Software Foundation.
79 + * This program is distributed in the hope that it will be useful, but
80 + * WITHOUT ANY WARRANTY; without even the implied warranty of
81 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
82 + * General Public License for more details.
84 + * You should have received a copy of the GNU General Public License
85 + * along with this program; if not, write to the Free Software
86 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
91 +#include <linux/module.h>
92 +#include <linux/bootmem.h>
93 +#include <linux/memblock.h>
94 +#include <linux/debugfs.h>
95 +#include <linux/crashlog.h>
96 +#include <linux/kmsg_dump.h>
97 +#include <linux/module.h>
98 +#include <linux/pfn.h>
99 +#include <linux/vmalloc.h>
102 +#define CRASHLOG_PAGES 4
103 +#define CRASHLOG_SIZE (CRASHLOG_PAGES * PAGE_SIZE)
104 +#define CRASHLOG_MAGIC 0xa1eedead
107 + * Start the log at 1M before the end of RAM, as some boot loaders like
108 + * to use the end of the RAM for stack usage and other things
109 + * If this fails, fall back to using the last part.
111 +#define CRASHLOG_OFFSET (1024 * 1024)
113 +struct crashlog_data {
119 +static struct debugfs_blob_wrapper crashlog_blob;
120 +static unsigned long crashlog_addr = 0;
121 +static struct crashlog_data *crashlog_buf;
122 +static struct kmsg_dumper dump;
123 +static bool first = true;
125 +extern struct list_head *crashlog_modules;
127 +static bool crashlog_set_addr(phys_addr_t addr, phys_addr_t size)
129 + /* Limit to lower 64 MB to avoid highmem */
130 + phys_addr_t limit = 64 * 1024 * 1024;
138 + if (addr + size > limit)
139 + size = limit - addr;
141 + crashlog_addr = addr;
143 + if (addr + size > CRASHLOG_OFFSET)
144 + crashlog_addr += size - CRASHLOG_OFFSET;
149 +#ifndef CONFIG_NO_BOOTMEM
150 +void __init crashlog_init_bootmem(bootmem_data_t *bdata)
152 + phys_addr_t start, end;
154 + start = PFN_PHYS(bdata->node_low_pfn);
155 + end = PFN_PHYS(bdata->node_min_pfn);
156 + if (!crashlog_set_addr(start, end - start))
159 + if (reserve_bootmem(crashlog_addr, CRASHLOG_SIZE, BOOTMEM_EXCLUSIVE) < 0) {
160 + printk("Crashlog failed to allocate RAM at address 0x%lx\n",
167 +#ifdef CONFIG_HAVE_MEMBLOCK
168 +void __init_memblock crashlog_init_memblock(phys_addr_t addr, phys_addr_t size)
170 + if (!crashlog_set_addr(addr, size))
173 + if (memblock_reserve(crashlog_addr, CRASHLOG_SIZE)) {
174 + printk("Crashlog failed to allocate RAM at address 0x%lx\n",
181 +static void __init crashlog_copy(void)
183 + if (crashlog_buf->magic != CRASHLOG_MAGIC)
186 + if (!crashlog_buf->len || crashlog_buf->len >
187 + CRASHLOG_SIZE - sizeof(*crashlog_buf))
190 + crashlog_blob.size = crashlog_buf->len;
191 + crashlog_blob.data = kmemdup(crashlog_buf->data,
192 + crashlog_buf->len, GFP_KERNEL);
194 + debugfs_create_blob("crashlog", 0700, NULL, &crashlog_blob);
197 +static int get_maxlen(void)
199 + return CRASHLOG_SIZE - sizeof(*crashlog_buf) - crashlog_buf->len;
202 +static void crashlog_printf(const char *fmt, ...)
205 + int len = get_maxlen();
210 + va_start(args, fmt);
211 + crashlog_buf->len += vscnprintf(
212 + &crashlog_buf->data[crashlog_buf->len],
217 +static void crashlog_do_dump(struct kmsg_dumper *dumper,
218 + enum kmsg_dump_reason reason)
226 + crashlog_printf("\n===================================\n");
228 + do_gettimeofday(&tv);
229 + crashlog_printf("Time: %lu.%lu\n",
230 + (long)tv.tv_sec, (long)tv.tv_usec);
233 + crashlog_printf("Modules:");
234 + list_for_each_entry(m, crashlog_modules, list) {
235 + crashlog_printf("\t%s@%p+%x", m->name,
236 + m->core_layout.base, m->core_layout.size,
237 + m->init_layout.base, m->init_layout.size);
239 + crashlog_printf("\n");
243 + buf = (char *)&crashlog_buf->data[crashlog_buf->len];
245 + kmsg_dump_get_buffer(dumper, true, buf, get_maxlen(), &len);
247 + crashlog_buf->len += len;
251 +int __init crashlog_init_fs(void)
253 + struct page *pages[CRASHLOG_PAGES];
257 + if (!crashlog_addr) {
258 + printk("No memory allocated for crashlog\n");
262 + printk("Crashlog allocated RAM at address 0x%lx\n", (unsigned long) crashlog_addr);
263 + for (i = 0; i < CRASHLOG_PAGES; i++)
264 + pages[i] = pfn_to_page((crashlog_addr >> PAGE_SHIFT) + i);
266 + prot = pgprot_writecombine(PAGE_KERNEL);
267 + crashlog_buf = vmap(pages, CRASHLOG_PAGES, VM_MAP, prot);
271 + crashlog_buf->magic = CRASHLOG_MAGIC;
272 + crashlog_buf->len = 0;
274 + dump.max_reason = KMSG_DUMP_OOPS;
275 + dump.dump = crashlog_do_dump;
276 + kmsg_dump_register(&dump);
280 +module_init(crashlog_init_fs);
281 --- a/kernel/module.c
282 +++ b/kernel/module.c
283 @@ -253,6 +253,9 @@ static void mod_update_bounds(struct mod
284 #ifdef CONFIG_KGDB_KDB
285 struct list_head *kdb_modules = &modules; /* kdb needs the list of modules */
286 #endif /* CONFIG_KGDB_KDB */
287 +#ifdef CONFIG_CRASHLOG
288 +struct list_head *crashlog_modules = &modules;
291 static void module_assert_mutex(void)
296 #include <linux/export.h>
297 #include <linux/kmemleak.h>
298 #include <linux/range.h>
299 +#include <linux/crashlog.h>
300 #include <linux/bug.h>
301 #include <linux/io.h>
302 #include <linux/bootmem.h>
303 @@ -174,6 +175,7 @@ static unsigned long __init free_all_boo
304 if (!bdata->node_bootmem_map)
307 + crashlog_init_bootmem(bdata);
308 map = bdata->node_bootmem_map;
309 start = bdata->node_min_pfn;
310 end = bdata->node_low_pfn;
314 #include <linux/debugfs.h>
315 #include <linux/seq_file.h>
316 #include <linux/memblock.h>
317 +#include <linux/crashlog.h>
319 #include <asm/sections.h>
320 #include <linux/io.h>
321 @@ -495,6 +496,8 @@ static void __init_memblock memblock_ins
322 memblock_set_region_node(rgn, nid);
324 type->total_size += size;
325 + if (type == &memblock.memory)
326 + crashlog_init_memblock(base, size);
330 @@ -534,6 +537,8 @@ int __init_memblock memblock_add_range(s
331 type->regions[0].flags = flags;
332 memblock_set_region_node(&type->regions[0], nid);
333 type->total_size = size;
334 + if (type == &memblock.memory)
335 + crashlog_init_memblock(base, size);