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. @@ -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) && !(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. @@ -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,181 @@
  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 <asm/io.h>
  74. +
  75. +#define CRASHLOG_PAGES 4
  76. +#define CRASHLOG_SIZE (CRASHLOG_PAGES * PAGE_SIZE)
  77. +#define CRASHLOG_MAGIC 0xa1eedead
  78. +
  79. +/*
  80. + * Start the log at 1M before the end of RAM, as some boot loaders like
  81. + * to use the end of the RAM for stack usage and other things
  82. + * If this fails, fall back to using the last part.
  83. + */
  84. +#define CRASHLOG_OFFSET (1024 * 1024)
  85. +
  86. +struct crashlog_data {
  87. + u32 magic;
  88. + u32 len;
  89. + u8 data[];
  90. +};
  91. +
  92. +static struct debugfs_blob_wrapper crashlog_blob;
  93. +static unsigned long crashlog_addr = 0;
  94. +static struct crashlog_data *crashlog_buf;
  95. +static struct kmsg_dumper dump;
  96. +static bool first = true;
  97. +
  98. +extern struct list_head *crashlog_modules;
  99. +
  100. +#ifndef CONFIG_NO_BOOTMEM
  101. +void __init crashlog_init_bootmem(bootmem_data_t *bdata)
  102. +{
  103. + unsigned long addr;
  104. +
  105. + if (crashlog_addr)
  106. + return;
  107. +
  108. + addr = PFN_PHYS(bdata->node_low_pfn) - CRASHLOG_OFFSET;
  109. + if (reserve_bootmem(addr, CRASHLOG_SIZE, BOOTMEM_EXCLUSIVE) < 0) {
  110. + printk("Crashlog failed to allocate RAM at address 0x%lx\n", addr);
  111. + bdata->node_low_pfn -= CRASHLOG_PAGES;
  112. + addr = PFN_PHYS(bdata->node_low_pfn);
  113. + }
  114. + crashlog_addr = addr;
  115. +}
  116. +#endif
  117. +
  118. +#ifdef CONFIG_HAVE_MEMBLOCK
  119. +void __init_memblock crashlog_init_memblock(phys_addr_t addr, phys_addr_t size)
  120. +{
  121. + if (crashlog_addr)
  122. + return;
  123. +
  124. + addr += size - CRASHLOG_OFFSET;
  125. + if (memblock_reserve(addr, CRASHLOG_SIZE)) {
  126. + printk("Crashlog failed to allocate RAM at address 0x%lx\n", (unsigned long) addr);
  127. + return;
  128. + }
  129. +
  130. + crashlog_addr = addr;
  131. +}
  132. +#endif
  133. +
  134. +static void __init crashlog_copy(void)
  135. +{
  136. + if (crashlog_buf->magic != CRASHLOG_MAGIC)
  137. + return;
  138. +
  139. + if (!crashlog_buf->len || crashlog_buf->len >
  140. + CRASHLOG_SIZE - sizeof(*crashlog_buf))
  141. + return;
  142. +
  143. + crashlog_blob.size = crashlog_buf->len;
  144. + crashlog_blob.data = kmemdup(crashlog_buf->data,
  145. + crashlog_buf->len, GFP_KERNEL);
  146. +
  147. + debugfs_create_blob("crashlog", 0700, NULL, &crashlog_blob);
  148. +}
  149. +
  150. +static int get_maxlen(void)
  151. +{
  152. + return CRASHLOG_SIZE - sizeof(*crashlog_buf) - crashlog_buf->len;
  153. +}
  154. +
  155. +static void crashlog_printf(const char *fmt, ...)
  156. +{
  157. + va_list args;
  158. + int len = get_maxlen();
  159. +
  160. + if (!len)
  161. + return;
  162. +
  163. + va_start(args, fmt);
  164. + crashlog_buf->len += vscnprintf(
  165. + &crashlog_buf->data[crashlog_buf->len],
  166. + len, fmt, args);
  167. + va_end(args);
  168. +}
  169. +
  170. +static void crashlog_do_dump(struct kmsg_dumper *dumper,
  171. + enum kmsg_dump_reason reason)
  172. +{
  173. + struct timeval tv;
  174. + struct module *m;
  175. + char *buf;
  176. + size_t len;
  177. +
  178. + if (!first)
  179. + crashlog_printf("\n===================================\n");
  180. +
  181. + do_gettimeofday(&tv);
  182. + crashlog_printf("Time: %lu.%lu\n",
  183. + (long)tv.tv_sec, (long)tv.tv_usec);
  184. +
  185. + if (first) {
  186. + crashlog_printf("Modules:");
  187. + list_for_each_entry(m, crashlog_modules, list) {
  188. + crashlog_printf("\t%s@%p+%x", m->name,
  189. + m->module_core, m->core_size,
  190. + m->module_init, m->init_size);
  191. + }
  192. + crashlog_printf("\n");
  193. + first = false;
  194. + }
  195. +
  196. + buf = (char *)&crashlog_buf->data[crashlog_buf->len];
  197. +
  198. + kmsg_dump_get_buffer(dumper, true, buf, get_maxlen(), &len);
  199. +
  200. + crashlog_buf->len += len;
  201. +}
  202. +
  203. +
  204. +int __init crashlog_init_fs(void)
  205. +{
  206. + if (!crashlog_addr)
  207. + return -ENOMEM;
  208. +
  209. + crashlog_buf = ioremap(crashlog_addr, CRASHLOG_SIZE);
  210. +
  211. + crashlog_copy();
  212. +
  213. + crashlog_buf->magic = CRASHLOG_MAGIC;
  214. + crashlog_buf->len = 0;
  215. +
  216. + dump.max_reason = KMSG_DUMP_OOPS;
  217. + dump.dump = crashlog_do_dump;
  218. + kmsg_dump_register(&dump);
  219. +
  220. + return 0;
  221. +}
  222. +module_init(crashlog_init_fs);
  223. --- a/mm/bootmem.c
  224. +++ b/mm/bootmem.c
  225. @@ -15,6 +15,7 @@
  226. #include <linux/export.h>
  227. #include <linux/kmemleak.h>
  228. #include <linux/range.h>
  229. +#include <linux/crashlog.h>
  230. #include <linux/memblock.h>
  231. #include <linux/bug.h>
  232. #include <linux/io.h>
  233. @@ -177,6 +178,7 @@ static unsigned long __init free_all_boo
  234. if (!bdata->node_bootmem_map)
  235. return 0;
  236. + crashlog_init_bootmem(bdata);
  237. map = bdata->node_bootmem_map;
  238. start = bdata->node_min_pfn;
  239. end = bdata->node_low_pfn;
  240. --- a/kernel/module.c
  241. +++ b/kernel/module.c
  242. @@ -275,6 +275,9 @@ static void mod_update_bounds(struct mod
  243. #ifdef CONFIG_KGDB_KDB
  244. struct list_head *kdb_modules = &modules; /* kdb needs the list of modules */
  245. #endif /* CONFIG_KGDB_KDB */
  246. +#ifdef CONFIG_CRASHLOG
  247. +struct list_head *crashlog_modules = &modules;
  248. +#endif
  249. static void module_assert_mutex(void)
  250. {
  251. --- a/mm/memblock.c
  252. +++ b/mm/memblock.c
  253. @@ -19,6 +19,7 @@
  254. #include <linux/debugfs.h>
  255. #include <linux/seq_file.h>
  256. #include <linux/memblock.h>
  257. +#include <linux/crashlog.h>
  258. #include <asm-generic/sections.h>
  259. #include <linux/io.h>
  260. @@ -503,6 +504,8 @@ static void __init_memblock memblock_ins
  261. memblock_set_region_node(rgn, nid);
  262. type->cnt++;
  263. type->total_size += size;
  264. + if (type == &memblock.memory && idx == 0)
  265. + crashlog_init_memblock(base, size);
  266. }
  267. /**