930-crashlog.patch 8.5 KB

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