kernel: split patches folder up into backport, pending and hack folders
[openwrt/staging/wigyori.git] / target / linux / generic / hack-4.9 / 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 diff --git a/include/linux/crashlog.h b/include/linux/crashlog.h
23 new file mode 100644
24 index 000000000000..7a9ee6138cc4
25 --- /dev/null
26 +++ b/include/linux/crashlog.h
27 @@ -0,0 +1,17 @@
28 +#ifndef __CRASHLOG_H
29 +#define __CRASHLOG_H
30 +
31 +#ifdef CONFIG_CRASHLOG
32 +void crashlog_init_bootmem(struct bootmem_data *bdata);
33 +void crashlog_init_memblock(phys_addr_t addr, phys_addr_t size);
34 +#else
35 +static inline void crashlog_init_bootmem(struct bootmem_data *bdata)
36 +{
37 +}
38 +
39 +static inline void crashlog_init_memblock(phys_addr_t addr, phys_addr_t size)
40 +{
41 +}
42 +#endif
43 +
44 +#endif
45 diff --git a/init/Kconfig b/init/Kconfig
46 index 9f2c0cabbc97..6d8f388fb9b3 100644
47 --- a/init/Kconfig
48 +++ b/init/Kconfig
49 @@ -1298,6 +1298,10 @@ config RELAY
50
51 If unsure, say N.
52
53 +config CRASHLOG
54 + bool "Crash logging"
55 + depends on (!NO_BOOTMEM || HAVE_MEMBLOCK)
56 +
57 config BLK_DEV_INITRD
58 bool "Initial RAM filesystem and RAM disk (initramfs/initrd) support"
59 depends on BROKEN || !FRV
60 diff --git a/kernel/Makefile b/kernel/Makefile
61 index 314e7d62f5f0..5fe0d8c3cb35 100644
62 --- a/kernel/Makefile
63 +++ b/kernel/Makefile
64 @@ -113,6 +113,7 @@ obj-$(CONFIG_TORTURE_TEST) += torture.o
65 obj-$(CONFIG_MEMBARRIER) += membarrier.o
66
67 obj-$(CONFIG_HAS_IOMEM) += memremap.o
68 +obj-$(CONFIG_CRASHLOG) += crashlog.o
69
70 $(obj)/configs.o: $(obj)/config_data.h
71
72 diff --git a/kernel/crashlog.c b/kernel/crashlog.c
73 new file mode 100644
74 index 000000000000..11894b276e54
75 --- /dev/null
76 +++ b/kernel/crashlog.c
77 @@ -0,0 +1,213 @@
78 +/*
79 + * Crash information logger
80 + * Copyright (C) 2010 Felix Fietkau <nbd@nbd.name>
81 + *
82 + * Based on ramoops.c
83 + * Copyright (C) 2010 Marco Stornelli <marco.stornelli@gmail.com>
84 + *
85 + * This program is free software; you can redistribute it and/or
86 + * modify it under the terms of the GNU General Public License
87 + * version 2 as published by the Free Software Foundation.
88 + *
89 + * This program is distributed in the hope that it will be useful, but
90 + * WITHOUT ANY WARRANTY; without even the implied warranty of
91 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
92 + * General Public License for more details.
93 + *
94 + * You should have received a copy of the GNU General Public License
95 + * along with this program; if not, write to the Free Software
96 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
97 + * 02110-1301 USA
98 + *
99 + */
100 +
101 +#include <linux/module.h>
102 +#include <linux/bootmem.h>
103 +#include <linux/memblock.h>
104 +#include <linux/debugfs.h>
105 +#include <linux/crashlog.h>
106 +#include <linux/kmsg_dump.h>
107 +#include <linux/module.h>
108 +#include <linux/pfn.h>
109 +#include <linux/vmalloc.h>
110 +#include <asm/io.h>
111 +
112 +#define CRASHLOG_PAGES 4
113 +#define CRASHLOG_SIZE (CRASHLOG_PAGES * PAGE_SIZE)
114 +#define CRASHLOG_MAGIC 0xa1eedead
115 +
116 +/*
117 + * Start the log at 1M before the end of RAM, as some boot loaders like
118 + * to use the end of the RAM for stack usage and other things
119 + * If this fails, fall back to using the last part.
120 + */
121 +#define CRASHLOG_OFFSET (1024 * 1024)
122 +
123 +struct crashlog_data {
124 + u32 magic;
125 + u32 len;
126 + u8 data[];
127 +};
128 +
129 +static struct debugfs_blob_wrapper crashlog_blob;
130 +static unsigned long crashlog_addr = 0;
131 +static struct crashlog_data *crashlog_buf;
132 +static struct kmsg_dumper dump;
133 +static bool first = true;
134 +
135 +extern struct list_head *crashlog_modules;
136 +
137 +static bool crashlog_set_addr(phys_addr_t addr, phys_addr_t size)
138 +{
139 + /* Limit to lower 64 MB to avoid highmem */
140 + phys_addr_t limit = 64 * 1024 * 1024;
141 +
142 + if (crashlog_addr)
143 + return false;
144 +
145 + if (addr > limit)
146 + return false;
147 +
148 + if (addr + size > limit)
149 + size = limit - addr;
150 +
151 + crashlog_addr = addr;
152 +
153 + if (addr + size > CRASHLOG_OFFSET)
154 + crashlog_addr += size - CRASHLOG_OFFSET;
155 +
156 + return true;
157 +}
158 +
159 +#ifndef CONFIG_NO_BOOTMEM
160 +void __init crashlog_init_bootmem(bootmem_data_t *bdata)
161 +{
162 + phys_addr_t start, end;
163 +
164 + start = PFN_PHYS(bdata->node_low_pfn);
165 + end = PFN_PHYS(bdata->node_min_pfn);
166 + if (!crashlog_set_addr(start, end - start))
167 + return;
168 +
169 + if (reserve_bootmem(crashlog_addr, CRASHLOG_SIZE, BOOTMEM_EXCLUSIVE) < 0) {
170 + printk("Crashlog failed to allocate RAM at address 0x%lx\n",
171 + crashlog_addr);
172 + crashlog_addr = 0;
173 + }
174 +}
175 +#endif
176 +
177 +#ifdef CONFIG_HAVE_MEMBLOCK
178 +void __init_memblock crashlog_init_memblock(phys_addr_t addr, phys_addr_t size)
179 +{
180 + if (!crashlog_set_addr(addr, size))
181 + return;
182 +
183 + if (memblock_reserve(crashlog_addr, CRASHLOG_SIZE)) {
184 + printk("Crashlog failed to allocate RAM at address 0x%lx\n",
185 + crashlog_addr);
186 + crashlog_addr = 0;
187 + }
188 +}
189 +#endif
190 +
191 +static void __init crashlog_copy(void)
192 +{
193 + if (crashlog_buf->magic != CRASHLOG_MAGIC)
194 + return;
195 +
196 + if (!crashlog_buf->len || crashlog_buf->len >
197 + CRASHLOG_SIZE - sizeof(*crashlog_buf))
198 + return;
199 +
200 + crashlog_blob.size = crashlog_buf->len;
201 + crashlog_blob.data = kmemdup(crashlog_buf->data,
202 + crashlog_buf->len, GFP_KERNEL);
203 +
204 + debugfs_create_blob("crashlog", 0700, NULL, &crashlog_blob);
205 +}
206 +
207 +static int get_maxlen(void)
208 +{
209 + return CRASHLOG_SIZE - sizeof(*crashlog_buf) - crashlog_buf->len;
210 +}
211 +
212 +static void crashlog_printf(const char *fmt, ...)
213 +{
214 + va_list args;
215 + int len = get_maxlen();
216 +
217 + if (!len)
218 + return;
219 +
220 + va_start(args, fmt);
221 + crashlog_buf->len += vscnprintf(
222 + &crashlog_buf->data[crashlog_buf->len],
223 + len, fmt, args);
224 + va_end(args);
225 +}
226 +
227 +static void crashlog_do_dump(struct kmsg_dumper *dumper,
228 + enum kmsg_dump_reason reason)
229 +{
230 + struct timeval tv;
231 + struct module *m;
232 + char *buf;
233 + size_t len;
234 +
235 + if (!first)
236 + crashlog_printf("\n===================================\n");
237 +
238 + do_gettimeofday(&tv);
239 + crashlog_printf("Time: %lu.%lu\n",
240 + (long)tv.tv_sec, (long)tv.tv_usec);
241 +
242 + if (first) {
243 + crashlog_printf("Modules:");
244 + list_for_each_entry(m, crashlog_modules, list) {
245 + crashlog_printf("\t%s@%p+%x", m->name,
246 + m->core_layout.base, m->core_layout.size,
247 + m->init_layout.base, m->init_layout.size);
248 + }
249 + crashlog_printf("\n");
250 + first = false;
251 + }
252 +
253 + buf = (char *)&crashlog_buf->data[crashlog_buf->len];
254 +
255 + kmsg_dump_get_buffer(dumper, true, buf, get_maxlen(), &len);
256 +
257 + crashlog_buf->len += len;
258 +}
259 +
260 +
261 +int __init crashlog_init_fs(void)
262 +{
263 + struct page *pages[CRASHLOG_PAGES];
264 + pgprot_t prot;
265 + int i;
266 +
267 + if (!crashlog_addr) {
268 + printk("No memory allocated for crashlog\n");
269 + return -ENOMEM;
270 + }
271 +
272 + printk("Crashlog allocated RAM at address 0x%lx\n", (unsigned long) crashlog_addr);
273 + for (i = 0; i < CRASHLOG_PAGES; i++)
274 + pages[i] = pfn_to_page((crashlog_addr >> PAGE_SHIFT) + i);
275 +
276 + prot = pgprot_writecombine(PAGE_KERNEL);
277 + crashlog_buf = vmap(pages, CRASHLOG_PAGES, VM_MAP, prot);
278 +
279 + crashlog_copy();
280 +
281 + crashlog_buf->magic = CRASHLOG_MAGIC;
282 + crashlog_buf->len = 0;
283 +
284 + dump.max_reason = KMSG_DUMP_OOPS;
285 + dump.dump = crashlog_do_dump;
286 + kmsg_dump_register(&dump);
287 +
288 + return 0;
289 +}
290 +module_init(crashlog_init_fs);
291 diff --git a/kernel/module.c b/kernel/module.c
292 index 80b5ac4181c6..51d6e4e6f436 100644
293 --- a/kernel/module.c
294 +++ b/kernel/module.c
295 @@ -253,6 +253,9 @@ static void mod_update_bounds(struct module *mod)
296 #ifdef CONFIG_KGDB_KDB
297 struct list_head *kdb_modules = &modules; /* kdb needs the list of modules */
298 #endif /* CONFIG_KGDB_KDB */
299 +#ifdef CONFIG_CRASHLOG
300 +struct list_head *crashlog_modules = &modules;
301 +#endif
302
303 static void module_assert_mutex(void)
304 {
305 diff --git a/mm/bootmem.c b/mm/bootmem.c
306 index e8a55a3c9feb..e0f3c0bf2709 100644
307 --- a/mm/bootmem.c
308 +++ b/mm/bootmem.c
309 @@ -14,6 +14,7 @@
310 #include <linux/export.h>
311 #include <linux/kmemleak.h>
312 #include <linux/range.h>
313 +#include <linux/crashlog.h>
314 #include <linux/bug.h>
315 #include <linux/io.h>
316 #include <linux/bootmem.h>
317 @@ -174,6 +175,7 @@ static unsigned long __init free_all_bootmem_core(bootmem_data_t *bdata)
318 if (!bdata->node_bootmem_map)
319 return 0;
320
321 + crashlog_init_bootmem(bdata);
322 map = bdata->node_bootmem_map;
323 start = bdata->node_min_pfn;
324 end = bdata->node_low_pfn;
325 diff --git a/mm/memblock.c b/mm/memblock.c
326 index 68849d0ead09..aef9ca5e7f52 100644
327 --- a/mm/memblock.c
328 +++ b/mm/memblock.c
329 @@ -19,6 +19,7 @@
330 #include <linux/debugfs.h>
331 #include <linux/seq_file.h>
332 #include <linux/memblock.h>
333 +#include <linux/crashlog.h>
334
335 #include <asm/sections.h>
336 #include <linux/io.h>
337 @@ -499,6 +500,8 @@ static void __init_memblock memblock_insert_region(struct memblock_type *type,
338 memblock_set_region_node(rgn, nid);
339 type->cnt++;
340 type->total_size += size;
341 + if (type == &memblock.memory)
342 + crashlog_init_memblock(base, size);
343 }
344
345 /**
346 @@ -538,6 +541,8 @@ int __init_memblock memblock_add_range(struct memblock_type *type,
347 type->regions[0].flags = flags;
348 memblock_set_region_node(&type->regions[0], nid);
349 type->total_size = size;
350 + if (type == &memblock.memory)
351 + crashlog_init_memblock(base, size);
352 return 0;
353 }
354 repeat:
355 --
356 2.11.0
357