2
0

930-crashlog.patch 7.8 KB

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