kernel: bump 4.14 to 4.14.98
[openwrt/openwrt.git] / target / linux / generic / hack-4.14 / 930-crashlog.patch
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
5
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
8
9 Signed-off-by: Felix Fietkau <nbd@nbd.name>
10 ---
11 include/linux/crashlog.h | 17 ++++
12 init/Kconfig | 4 +
13 kernel/Makefile | 1 +
14 kernel/crashlog.c | 213 +++++++++++++++++++++++++++++++++++++++++++++++
15 kernel/module.c | 3 +
16 mm/bootmem.c | 2 +
17 mm/memblock.c | 5 ++
18 7 files changed, 245 insertions(+)
19 create mode 100644 include/linux/crashlog.h
20 create mode 100644 kernel/crashlog.c
21
22 --- /dev/null
23 +++ b/include/linux/crashlog.h
24 @@ -0,0 +1,17 @@
25 +#ifndef __CRASHLOG_H
26 +#define __CRASHLOG_H
27 +
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);
31 +#else
32 +static inline void crashlog_init_bootmem(struct bootmem_data *bdata)
33 +{
34 +}
35 +
36 +static inline void crashlog_init_memblock(phys_addr_t addr, phys_addr_t size)
37 +{
38 +}
39 +#endif
40 +
41 +#endif
42 --- a/init/Kconfig
43 +++ b/init/Kconfig
44 @@ -1009,6 +1009,10 @@ config RELAY
45
46 If unsure, say N.
47
48 +config CRASHLOG
49 + bool "Crash logging"
50 + depends on (!NO_BOOTMEM || HAVE_MEMBLOCK)
51 +
52 config BLK_DEV_INITRD
53 bool "Initial RAM filesystem and RAM disk (initramfs/initrd) support"
54 depends on BROKEN || !FRV
55 --- a/kernel/Makefile
56 +++ b/kernel/Makefile
57 @@ -112,6 +112,7 @@ obj-$(CONFIG_CONTEXT_TRACKING) += contex
58 obj-$(CONFIG_TORTURE_TEST) += torture.o
59
60 obj-$(CONFIG_HAS_IOMEM) += memremap.o
61 +obj-$(CONFIG_CRASHLOG) += crashlog.o
62
63 $(obj)/configs.o: $(obj)/config_data.h
64
65 --- /dev/null
66 +++ b/kernel/crashlog.c
67 @@ -0,0 +1,213 @@
68 +/*
69 + * Crash information logger
70 + * Copyright (C) 2010 Felix Fietkau <nbd@nbd.name>
71 + *
72 + * Based on ramoops.c
73 + * Copyright (C) 2010 Marco Stornelli <marco.stornelli@gmail.com>
74 + *
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.
78 + *
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.
83 + *
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
87 + * 02110-1301 USA
88 + *
89 + */
90 +
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>
100 +#include <asm/io.h>
101 +
102 +#define CRASHLOG_PAGES 4
103 +#define CRASHLOG_SIZE (CRASHLOG_PAGES * PAGE_SIZE)
104 +#define CRASHLOG_MAGIC 0xa1eedead
105 +
106 +/*
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.
110 + */
111 +#define CRASHLOG_OFFSET (1024 * 1024)
112 +
113 +struct crashlog_data {
114 + u32 magic;
115 + u32 len;
116 + u8 data[];
117 +};
118 +
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;
124 +
125 +extern struct list_head *crashlog_modules;
126 +
127 +static bool crashlog_set_addr(phys_addr_t addr, phys_addr_t size)
128 +{
129 + /* Limit to lower 64 MB to avoid highmem */
130 + phys_addr_t limit = 64 * 1024 * 1024;
131 +
132 + if (crashlog_addr)
133 + return false;
134 +
135 + if (addr > limit)
136 + return false;
137 +
138 + if (addr + size > limit)
139 + size = limit - addr;
140 +
141 + crashlog_addr = addr;
142 +
143 + if (addr + size > CRASHLOG_OFFSET)
144 + crashlog_addr += size - CRASHLOG_OFFSET;
145 +
146 + return true;
147 +}
148 +
149 +#ifndef CONFIG_NO_BOOTMEM
150 +void __init crashlog_init_bootmem(bootmem_data_t *bdata)
151 +{
152 + phys_addr_t start, end;
153 +
154 + start = PFN_PHYS(bdata->node_low_pfn);
155 + end = PFN_PHYS(bdata->node_min_pfn);
156 + if (!crashlog_set_addr(start, end - start))
157 + return;
158 +
159 + if (reserve_bootmem(crashlog_addr, CRASHLOG_SIZE, BOOTMEM_EXCLUSIVE) < 0) {
160 + printk("Crashlog failed to allocate RAM at address 0x%lx\n",
161 + crashlog_addr);
162 + crashlog_addr = 0;
163 + }
164 +}
165 +#endif
166 +
167 +#ifdef CONFIG_HAVE_MEMBLOCK
168 +void __init_memblock crashlog_init_memblock(phys_addr_t addr, phys_addr_t size)
169 +{
170 + if (!crashlog_set_addr(addr, size))
171 + return;
172 +
173 + if (memblock_reserve(crashlog_addr, CRASHLOG_SIZE)) {
174 + printk("Crashlog failed to allocate RAM at address 0x%lx\n",
175 + crashlog_addr);
176 + crashlog_addr = 0;
177 + }
178 +}
179 +#endif
180 +
181 +static void __init crashlog_copy(void)
182 +{
183 + if (crashlog_buf->magic != CRASHLOG_MAGIC)
184 + return;
185 +
186 + if (!crashlog_buf->len || crashlog_buf->len >
187 + CRASHLOG_SIZE - sizeof(*crashlog_buf))
188 + return;
189 +
190 + crashlog_blob.size = crashlog_buf->len;
191 + crashlog_blob.data = kmemdup(crashlog_buf->data,
192 + crashlog_buf->len, GFP_KERNEL);
193 +
194 + debugfs_create_blob("crashlog", 0700, NULL, &crashlog_blob);
195 +}
196 +
197 +static int get_maxlen(void)
198 +{
199 + return CRASHLOG_SIZE - sizeof(*crashlog_buf) - crashlog_buf->len;
200 +}
201 +
202 +static void crashlog_printf(const char *fmt, ...)
203 +{
204 + va_list args;
205 + int len = get_maxlen();
206 +
207 + if (!len)
208 + return;
209 +
210 + va_start(args, fmt);
211 + crashlog_buf->len += vscnprintf(
212 + &crashlog_buf->data[crashlog_buf->len],
213 + len, fmt, args);
214 + va_end(args);
215 +}
216 +
217 +static void crashlog_do_dump(struct kmsg_dumper *dumper,
218 + enum kmsg_dump_reason reason)
219 +{
220 + struct timeval tv;
221 + struct module *m;
222 + char *buf;
223 + size_t len;
224 +
225 + if (!first)
226 + crashlog_printf("\n===================================\n");
227 +
228 + do_gettimeofday(&tv);
229 + crashlog_printf("Time: %lu.%lu\n",
230 + (long)tv.tv_sec, (long)tv.tv_usec);
231 +
232 + if (first) {
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);
238 + }
239 + crashlog_printf("\n");
240 + first = false;
241 + }
242 +
243 + buf = (char *)&crashlog_buf->data[crashlog_buf->len];
244 +
245 + kmsg_dump_get_buffer(dumper, true, buf, get_maxlen(), &len);
246 +
247 + crashlog_buf->len += len;
248 +}
249 +
250 +
251 +int __init crashlog_init_fs(void)
252 +{
253 + struct page *pages[CRASHLOG_PAGES];
254 + pgprot_t prot;
255 + int i;
256 +
257 + if (!crashlog_addr) {
258 + printk("No memory allocated for crashlog\n");
259 + return -ENOMEM;
260 + }
261 +
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);
265 +
266 + prot = pgprot_writecombine(PAGE_KERNEL);
267 + crashlog_buf = vmap(pages, CRASHLOG_PAGES, VM_MAP, prot);
268 +
269 + crashlog_copy();
270 +
271 + crashlog_buf->magic = CRASHLOG_MAGIC;
272 + crashlog_buf->len = 0;
273 +
274 + dump.max_reason = KMSG_DUMP_OOPS;
275 + dump.dump = crashlog_do_dump;
276 + kmsg_dump_register(&dump);
277 +
278 + return 0;
279 +}
280 +module_init(crashlog_init_fs);
281 --- a/kernel/module.c
282 +++ b/kernel/module.c
283 @@ -256,6 +256,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;
289 +#endif
290
291 static void module_assert_mutex(void)
292 {
293 --- a/mm/bootmem.c
294 +++ b/mm/bootmem.c
295 @@ -15,6 +15,7 @@
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 @@ -175,6 +176,7 @@ static unsigned long __init free_all_boo
304 if (!bdata->node_bootmem_map)
305 return 0;
306
307 + crashlog_init_bootmem(bdata);
308 map = bdata->node_bootmem_map;
309 start = bdata->node_min_pfn;
310 end = bdata->node_low_pfn;
311 --- a/mm/memblock.c
312 +++ b/mm/memblock.c
313 @@ -19,6 +19,7 @@
314 #include <linux/debugfs.h>
315 #include <linux/seq_file.h>
316 #include <linux/memblock.h>
317 +#include <linux/crashlog.h>
318
319 #include <asm/sections.h>
320 #include <linux/io.h>
321 @@ -483,6 +484,8 @@ static void __init_memblock memblock_ins
322 memblock_set_region_node(rgn, nid);
323 type->cnt++;
324 type->total_size += size;
325 + if (type == &memblock.memory)
326 + crashlog_init_memblock(base, size);
327 }
328
329 /**
330 @@ -522,6 +525,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);
336 return 0;
337 }
338 repeat: