930-crashlog.patch 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. @@ -1279,6 +1279,10 @@ config RELAY
  24. If unsure, say N.
  25. +config CRASHLOG
  26. + bool "Crash logging"
  27. + depends on (!NO_BOOTMEM || HAVE_MEMBLOCK) && !(ARM || SPARC || PPC)
  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. @@ -96,6 +96,7 @@ obj-$(CONFIG_CRASH_DUMP) += crash_dump.o
  35. obj-$(CONFIG_JUMP_LABEL) += jump_label.o
  36. obj-$(CONFIG_CONTEXT_TRACKING) += context_tracking.o
  37. obj-$(CONFIG_TORTURE_TEST) += torture.o
  38. +obj-$(CONFIG_CRASHLOG) += crashlog.o
  39. $(obj)/configs.o: $(obj)/config_data.h
  40. --- /dev/null
  41. +++ b/kernel/crashlog.c
  42. @@ -0,0 +1,181 @@
  43. +/*
  44. + * Crash information logger
  45. + * Copyright (C) 2010 Felix Fietkau <nbd@nbd.name>
  46. + *
  47. + * Based on ramoops.c
  48. + * Copyright (C) 2010 Marco Stornelli <marco.stornelli@gmail.com>
  49. + *
  50. + * This program is free software; you can redistribute it and/or
  51. + * modify it under the terms of the GNU General Public License
  52. + * version 2 as published by the Free Software Foundation.
  53. + *
  54. + * This program is distributed in the hope that it will be useful, but
  55. + * WITHOUT ANY WARRANTY; without even the implied warranty of
  56. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  57. + * General Public License for more details.
  58. + *
  59. + * You should have received a copy of the GNU General Public License
  60. + * along with this program; if not, write to the Free Software
  61. + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  62. + * 02110-1301 USA
  63. + *
  64. + */
  65. +
  66. +#include <linux/module.h>
  67. +#include <linux/bootmem.h>
  68. +#include <linux/memblock.h>
  69. +#include <linux/debugfs.h>
  70. +#include <linux/crashlog.h>
  71. +#include <linux/kmsg_dump.h>
  72. +#include <linux/module.h>
  73. +#include <linux/pfn.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. +#ifndef CONFIG_NO_BOOTMEM
  102. +void __init crashlog_init_bootmem(bootmem_data_t *bdata)
  103. +{
  104. + unsigned long addr;
  105. +
  106. + if (crashlog_addr)
  107. + return;
  108. +
  109. + addr = PFN_PHYS(bdata->node_low_pfn) - CRASHLOG_OFFSET;
  110. + if (reserve_bootmem(addr, CRASHLOG_SIZE, BOOTMEM_EXCLUSIVE) < 0) {
  111. + printk("Crashlog failed to allocate RAM at address 0x%lx\n", addr);
  112. + bdata->node_low_pfn -= CRASHLOG_PAGES;
  113. + addr = PFN_PHYS(bdata->node_low_pfn);
  114. + }
  115. + crashlog_addr = addr;
  116. +}
  117. +#endif
  118. +
  119. +#ifdef CONFIG_HAVE_MEMBLOCK
  120. +void __init_memblock crashlog_init_memblock(phys_addr_t addr, phys_addr_t size)
  121. +{
  122. + if (crashlog_addr)
  123. + return;
  124. +
  125. + addr += size - CRASHLOG_OFFSET;
  126. + if (memblock_reserve(addr, CRASHLOG_SIZE)) {
  127. + printk("Crashlog failed to allocate RAM at address 0x%lx\n", (unsigned long) addr);
  128. + return;
  129. + }
  130. +
  131. + crashlog_addr = addr;
  132. +}
  133. +#endif
  134. +
  135. +static void __init crashlog_copy(void)
  136. +{
  137. + if (crashlog_buf->magic != CRASHLOG_MAGIC)
  138. + return;
  139. +
  140. + if (!crashlog_buf->len || crashlog_buf->len >
  141. + CRASHLOG_SIZE - sizeof(*crashlog_buf))
  142. + return;
  143. +
  144. + crashlog_blob.size = crashlog_buf->len;
  145. + crashlog_blob.data = kmemdup(crashlog_buf->data,
  146. + crashlog_buf->len, GFP_KERNEL);
  147. +
  148. + debugfs_create_blob("crashlog", 0700, NULL, &crashlog_blob);
  149. +}
  150. +
  151. +static int get_maxlen(void)
  152. +{
  153. + return CRASHLOG_SIZE - sizeof(*crashlog_buf) - crashlog_buf->len;
  154. +}
  155. +
  156. +static void crashlog_printf(const char *fmt, ...)
  157. +{
  158. + va_list args;
  159. + int len = get_maxlen();
  160. +
  161. + if (!len)
  162. + return;
  163. +
  164. + va_start(args, fmt);
  165. + crashlog_buf->len += vscnprintf(
  166. + &crashlog_buf->data[crashlog_buf->len],
  167. + len, fmt, args);
  168. + va_end(args);
  169. +}
  170. +
  171. +static void crashlog_do_dump(struct kmsg_dumper *dumper,
  172. + enum kmsg_dump_reason reason)
  173. +{
  174. + struct timeval tv;
  175. + struct module *m;
  176. + char *buf;
  177. + size_t len;
  178. +
  179. + if (!first)
  180. + crashlog_printf("\n===================================\n");
  181. +
  182. + do_gettimeofday(&tv);
  183. + crashlog_printf("Time: %lu.%lu\n",
  184. + (long)tv.tv_sec, (long)tv.tv_usec);
  185. +
  186. + if (first) {
  187. + crashlog_printf("Modules:");
  188. + list_for_each_entry(m, crashlog_modules, list) {
  189. + crashlog_printf("\t%s@%p+%x", m->name,
  190. + m->module_core, m->core_size,
  191. + m->module_init, m->init_size);
  192. + }
  193. + crashlog_printf("\n");
  194. + first = false;
  195. + }
  196. +
  197. + buf = (char *)&crashlog_buf->data[crashlog_buf->len];
  198. +
  199. + kmsg_dump_get_buffer(dumper, true, buf, get_maxlen(), &len);
  200. +
  201. + crashlog_buf->len += len;
  202. +}
  203. +
  204. +
  205. +int __init crashlog_init_fs(void)
  206. +{
  207. + if (!crashlog_addr)
  208. + return -ENOMEM;
  209. +
  210. + crashlog_buf = ioremap(crashlog_addr, CRASHLOG_SIZE);
  211. +
  212. + crashlog_copy();
  213. +
  214. + crashlog_buf->magic = CRASHLOG_MAGIC;
  215. + crashlog_buf->len = 0;
  216. +
  217. + dump.max_reason = KMSG_DUMP_OOPS;
  218. + dump.dump = crashlog_do_dump;
  219. + kmsg_dump_register(&dump);
  220. +
  221. + return 0;
  222. +}
  223. +module_init(crashlog_init_fs);
  224. --- a/mm/bootmem.c
  225. +++ b/mm/bootmem.c
  226. @@ -15,6 +15,7 @@
  227. #include <linux/export.h>
  228. #include <linux/kmemleak.h>
  229. #include <linux/range.h>
  230. +#include <linux/crashlog.h>
  231. #include <linux/memblock.h>
  232. #include <linux/bug.h>
  233. #include <linux/io.h>
  234. @@ -177,6 +178,7 @@ static unsigned long __init free_all_boo
  235. if (!bdata->node_bootmem_map)
  236. return 0;
  237. + crashlog_init_bootmem(bdata);
  238. map = bdata->node_bootmem_map;
  239. start = bdata->node_min_pfn;
  240. end = bdata->node_low_pfn;
  241. --- a/kernel/module.c
  242. +++ b/kernel/module.c
  243. @@ -105,6 +105,9 @@ static LIST_HEAD(modules);
  244. #ifdef CONFIG_KGDB_KDB
  245. struct list_head *kdb_modules = &modules; /* kdb needs the list of modules */
  246. #endif /* CONFIG_KGDB_KDB */
  247. +#ifdef CONFIG_CRASHLOG
  248. +struct list_head *crashlog_modules = &modules;
  249. +#endif
  250. #ifdef CONFIG_MODULE_SIG
  251. #ifdef CONFIG_MODULE_SIG_FORCE
  252. --- a/mm/memblock.c
  253. +++ b/mm/memblock.c
  254. @@ -19,6 +19,7 @@
  255. #include <linux/debugfs.h>
  256. #include <linux/seq_file.h>
  257. #include <linux/memblock.h>
  258. +#include <linux/crashlog.h>
  259. #include <asm-generic/sections.h>
  260. #include <linux/io.h>
  261. @@ -477,6 +478,8 @@ static void __init_memblock memblock_ins
  262. memblock_set_region_node(rgn, nid);
  263. type->cnt++;
  264. type->total_size += size;
  265. + if (type == &memblock.memory && idx == 0)
  266. + crashlog_init_memblock(base, size);
  267. }
  268. /**