930-crashlog.patch 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. --- /dev/null
  2. +++ b/include/linux/crashlog.h
  3. @@ -0,0 +1,12 @@
  4. +#ifndef __CRASHLOG_H
  5. +#define __CRASHLOG_H
  6. +
  7. +#ifdef CONFIG_CRASHLOG
  8. +void __init crashlog_init_mem(struct bootmem_data *bdata);
  9. +#else
  10. +static inline void crashlog_init_mem(struct bootmem_data *bdata)
  11. +{
  12. +}
  13. +#endif
  14. +
  15. +#endif
  16. --- a/init/Kconfig
  17. +++ b/init/Kconfig
  18. @@ -715,6 +715,9 @@ config NET_NS
  19. Allow user space to create what appear to be multiple instances
  20. of the network stack.
  21. +config CRASHLOG
  22. + bool "Crash logging"
  23. +
  24. config BLK_DEV_INITRD
  25. bool "Initial RAM filesystem and RAM disk (initramfs/initrd) support"
  26. depends on BROKEN || !FRV
  27. --- a/kernel/Makefile
  28. +++ b/kernel/Makefile
  29. @@ -105,6 +105,7 @@ obj-$(CONFIG_PERF_EVENTS) += perf_event.
  30. obj-$(CONFIG_HAVE_HW_BREAKPOINT) += hw_breakpoint.o
  31. obj-$(CONFIG_USER_RETURN_NOTIFIER) += user-return-notifier.o
  32. obj-$(CONFIG_PADATA) += padata.o
  33. +obj-$(CONFIG_CRASHLOG) += crashlog.o
  34. ifneq ($(CONFIG_SCHED_OMIT_FRAME_POINTER),y)
  35. # According to Alan Modra <[email protected]>, the -fno-omit-frame-pointer is
  36. --- /dev/null
  37. +++ b/kernel/crashlog.c
  38. @@ -0,0 +1,171 @@
  39. +/*
  40. + * Crash information logger
  41. + * Copyright (C) 2010 Felix Fietkau <[email protected]>
  42. + *
  43. + * Based on ramoops.c
  44. + * Copyright (C) 2010 Marco Stornelli <[email protected]>
  45. + *
  46. + * This program is free software; you can redistribute it and/or
  47. + * modify it under the terms of the GNU General Public License
  48. + * version 2 as published by the Free Software Foundation.
  49. + *
  50. + * This program is distributed in the hope that it will be useful, but
  51. + * WITHOUT ANY WARRANTY; without even the implied warranty of
  52. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  53. + * General Public License for more details.
  54. + *
  55. + * You should have received a copy of the GNU General Public License
  56. + * along with this program; if not, write to the Free Software
  57. + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  58. + * 02110-1301 USA
  59. + *
  60. + */
  61. +
  62. +#include <linux/module.h>
  63. +#include <linux/bootmem.h>
  64. +#include <linux/debugfs.h>
  65. +#include <linux/crashlog.h>
  66. +#include <linux/kmsg_dump.h>
  67. +#include <linux/module.h>
  68. +#include <linux/pfn.h>
  69. +#include <asm/io.h>
  70. +
  71. +#define CRASHLOG_PAGES 4
  72. +#define CRASHLOG_SIZE (CRASHLOG_PAGES * PAGE_SIZE)
  73. +#define CRASHLOG_MAGIC 0xa1eedead
  74. +
  75. +/*
  76. + * Start the log at 1M before the end of RAM, as some boot loaders like
  77. + * to use the end of the RAM for stack usage and other things
  78. + * If this fails, fall back to using the last part.
  79. + */
  80. +#define CRASHLOG_OFFSET (1024 * 1024)
  81. +
  82. +struct crashlog_data {
  83. + u32 magic;
  84. + u32 len;
  85. + u8 data[];
  86. +};
  87. +
  88. +static struct debugfs_blob_wrapper crashlog_blob;
  89. +static unsigned long crashlog_addr = 0;
  90. +static struct crashlog_data *crashlog_buf;
  91. +static struct kmsg_dumper dump;
  92. +static bool first = true;
  93. +
  94. +extern struct list_head *crashlog_modules;
  95. +
  96. +void __init crashlog_init_mem(bootmem_data_t *bdata)
  97. +{
  98. + unsigned long addr;
  99. +
  100. + if (crashlog_addr)
  101. + return;
  102. +
  103. + addr = PFN_PHYS(bdata->node_low_pfn) - CRASHLOG_OFFSET;
  104. + if (reserve_bootmem(addr, CRASHLOG_SIZE, BOOTMEM_EXCLUSIVE) < 0) {
  105. + printk("Crashlog failed to allocate RAM at address 0x%lx\n", addr);
  106. + bdata->node_low_pfn -= CRASHLOG_PAGES;
  107. + addr = PFN_PHYS(bdata->node_low_pfn);
  108. + }
  109. + crashlog_addr = addr;
  110. +}
  111. +
  112. +static void __init crashlog_copy(void)
  113. +{
  114. + if (crashlog_buf->magic != CRASHLOG_MAGIC)
  115. + return;
  116. +
  117. + if (!crashlog_buf->len || crashlog_buf->len >
  118. + CRASHLOG_SIZE - sizeof(*crashlog_buf))
  119. + return;
  120. +
  121. + crashlog_blob.size = crashlog_buf->len;
  122. + crashlog_blob.data = kmemdup(crashlog_buf->data,
  123. + crashlog_buf->len, GFP_KERNEL);
  124. +
  125. + debugfs_create_blob("crashlog", 0700, NULL, &crashlog_blob);
  126. +}
  127. +
  128. +static int get_maxlen(void)
  129. +{
  130. + return CRASHLOG_SIZE - sizeof(*crashlog_buf) - crashlog_buf->len;
  131. +}
  132. +
  133. +static void crashlog_printf(const char *fmt, ...)
  134. +{
  135. + va_list args;
  136. + int len = get_maxlen();
  137. +
  138. + if (!len)
  139. + return;
  140. +
  141. + va_start(args, fmt);
  142. + crashlog_buf->len += vsnprintf(
  143. + &crashlog_buf->data[crashlog_buf->len],
  144. + len, fmt, args);
  145. + va_end(args);
  146. +}
  147. +
  148. +static void crashlog_do_dump(struct kmsg_dumper *dumper,
  149. + enum kmsg_dump_reason reason, const char *s1, unsigned long l1,
  150. + const char *s2, unsigned long l2)
  151. +{
  152. + unsigned long s1_start, s2_start;
  153. + unsigned long l1_cpy, l2_cpy;
  154. + struct timeval tv;
  155. + struct module *m;
  156. + char *buf;
  157. + int len;
  158. +
  159. + if (!first)
  160. + crashlog_printf("\n===================================\n");
  161. +
  162. + do_gettimeofday(&tv);
  163. + crashlog_printf("Time: %lu.%lu\n",
  164. + (long)tv.tv_sec, (long)tv.tv_usec);
  165. +
  166. + if (first) {
  167. + crashlog_printf("Modules:");
  168. + list_for_each_entry(m, crashlog_modules, list) {
  169. + crashlog_printf("\t%s@%p+%x", m->name,
  170. + m->module_core, m->core_size,
  171. + m->module_init, m->init_size);
  172. + }
  173. + crashlog_printf("\n");
  174. + first = false;
  175. + }
  176. +
  177. + buf = (char *)&crashlog_buf->data[crashlog_buf->len];
  178. + len = get_maxlen();
  179. +
  180. + l2_cpy = min(l2, (unsigned long)len);
  181. + l1_cpy = min(l1, (unsigned long)len - l2_cpy);
  182. +
  183. + s2_start = l2 - l2_cpy;
  184. + s1_start = l1 - l1_cpy;
  185. +
  186. + memcpy(buf, s1 + s1_start, l1_cpy);
  187. + memcpy(buf + l1_cpy, s2 + s2_start, l2_cpy);
  188. + crashlog_buf->len += l1_cpy + l2_cpy;
  189. +}
  190. +
  191. +
  192. +int __init crashlog_init_fs(void)
  193. +{
  194. + if (!crashlog_addr)
  195. + return -ENOMEM;
  196. +
  197. + crashlog_buf = ioremap(crashlog_addr, CRASHLOG_SIZE);
  198. +
  199. + crashlog_copy();
  200. +
  201. + crashlog_buf->magic = CRASHLOG_MAGIC;
  202. + crashlog_buf->len = 0;
  203. +
  204. + dump.dump = crashlog_do_dump;
  205. + kmsg_dump_register(&dump);
  206. +
  207. + return 0;
  208. +}
  209. +module_init(crashlog_init_fs);
  210. --- a/mm/bootmem.c
  211. +++ b/mm/bootmem.c
  212. @@ -15,6 +15,7 @@
  213. #include <linux/module.h>
  214. #include <linux/kmemleak.h>
  215. #include <linux/range.h>
  216. +#include <linux/crashlog.h>
  217. #include <asm/bug.h>
  218. #include <asm/io.h>
  219. @@ -226,6 +227,7 @@ static unsigned long __init free_all_boo
  220. if (!bdata->node_bootmem_map)
  221. return 0;
  222. + crashlog_init_mem(bdata);
  223. start = bdata->node_min_pfn;
  224. end = bdata->node_low_pfn;
  225. --- a/kernel/module.c
  226. +++ b/kernel/module.c
  227. @@ -79,6 +79,9 @@ EXPORT_TRACEPOINT_SYMBOL(module_get);
  228. DEFINE_MUTEX(module_mutex);
  229. EXPORT_SYMBOL_GPL(module_mutex);
  230. static LIST_HEAD(modules);
  231. +#ifdef CONFIG_CRASHLOG
  232. +struct list_head *crashlog_modules = &modules;
  233. +#endif
  234. /* Block module loading/unloading? */
  235. int modules_disabled = 0;