930-crashlog.patch 8.5 KB

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