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