dumpstack.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. /*
  2. * Copyright (C) 1991, 1992 Linus Torvalds
  3. * Copyright (C) 2000, 2001, 2002 Andi Kleen, SuSE Labs
  4. */
  5. #include <linux/kallsyms.h>
  6. #include <linux/kprobes.h>
  7. #include <linux/uaccess.h>
  8. #include <linux/utsname.h>
  9. #include <linux/hardirq.h>
  10. #include <linux/kdebug.h>
  11. #include <linux/module.h>
  12. #include <linux/ptrace.h>
  13. #include <linux/sched/debug.h>
  14. #include <linux/sched/task_stack.h>
  15. #include <linux/ftrace.h>
  16. #include <linux/kexec.h>
  17. #include <linux/bug.h>
  18. #include <linux/nmi.h>
  19. #include <linux/sysfs.h>
  20. #include <linux/kasan.h>
  21. #include <asm/cpu_entry_area.h>
  22. #include <asm/stacktrace.h>
  23. #include <asm/unwind.h>
  24. int panic_on_unrecovered_nmi;
  25. int panic_on_io_nmi;
  26. static int die_counter;
  27. static struct pt_regs exec_summary_regs;
  28. bool noinstr in_task_stack(unsigned long *stack, struct task_struct *task,
  29. struct stack_info *info)
  30. {
  31. unsigned long *begin = task_stack_page(task);
  32. unsigned long *end = task_stack_page(task) + THREAD_SIZE;
  33. if (stack < begin || stack >= end)
  34. return false;
  35. info->type = STACK_TYPE_TASK;
  36. info->begin = begin;
  37. info->end = end;
  38. info->next_sp = NULL;
  39. return true;
  40. }
  41. /* Called from get_stack_info_noinstr - so must be noinstr too */
  42. bool noinstr in_entry_stack(unsigned long *stack, struct stack_info *info)
  43. {
  44. struct entry_stack *ss = cpu_entry_stack(smp_processor_id());
  45. void *begin = ss;
  46. void *end = ss + 1;
  47. if ((void *)stack < begin || (void *)stack >= end)
  48. return false;
  49. info->type = STACK_TYPE_ENTRY;
  50. info->begin = begin;
  51. info->end = end;
  52. info->next_sp = NULL;
  53. return true;
  54. }
  55. static void printk_stack_address(unsigned long address, int reliable,
  56. const char *log_lvl)
  57. {
  58. touch_nmi_watchdog();
  59. printk("%s %s%pBb\n", log_lvl, reliable ? "" : "? ", (void *)address);
  60. }
  61. static int copy_code(struct pt_regs *regs, u8 *buf, unsigned long src,
  62. unsigned int nbytes)
  63. {
  64. if (!user_mode(regs))
  65. return copy_from_kernel_nofault(buf, (u8 *)src, nbytes);
  66. /* The user space code from other tasks cannot be accessed. */
  67. if (regs != task_pt_regs(current))
  68. return -EPERM;
  69. /*
  70. * Even if named copy_from_user_nmi() this can be invoked from
  71. * other contexts and will not try to resolve a pagefault, which is
  72. * the correct thing to do here as this code can be called from any
  73. * context.
  74. */
  75. return copy_from_user_nmi(buf, (void __user *)src, nbytes);
  76. }
  77. /*
  78. * There are a couple of reasons for the 2/3rd prologue, courtesy of Linus:
  79. *
  80. * In case where we don't have the exact kernel image (which, if we did, we can
  81. * simply disassemble and navigate to the RIP), the purpose of the bigger
  82. * prologue is to have more context and to be able to correlate the code from
  83. * the different toolchains better.
  84. *
  85. * In addition, it helps in recreating the register allocation of the failing
  86. * kernel and thus make sense of the register dump.
  87. *
  88. * What is more, the additional complication of a variable length insn arch like
  89. * x86 warrants having longer byte sequence before rIP so that the disassembler
  90. * can "sync" up properly and find instruction boundaries when decoding the
  91. * opcode bytes.
  92. *
  93. * Thus, the 2/3rds prologue and 64 byte OPCODE_BUFSIZE is just a random
  94. * guesstimate in attempt to achieve all of the above.
  95. */
  96. void show_opcodes(struct pt_regs *regs, const char *loglvl)
  97. {
  98. #define PROLOGUE_SIZE 42
  99. #define EPILOGUE_SIZE 21
  100. #define OPCODE_BUFSIZE (PROLOGUE_SIZE + 1 + EPILOGUE_SIZE)
  101. u8 opcodes[OPCODE_BUFSIZE];
  102. unsigned long prologue = regs->ip - PROLOGUE_SIZE;
  103. switch (copy_code(regs, opcodes, prologue, sizeof(opcodes))) {
  104. case 0:
  105. printk("%sCode: %" __stringify(PROLOGUE_SIZE) "ph <%02x> %"
  106. __stringify(EPILOGUE_SIZE) "ph\n", loglvl, opcodes,
  107. opcodes[PROLOGUE_SIZE], opcodes + PROLOGUE_SIZE + 1);
  108. break;
  109. case -EPERM:
  110. /* No access to the user space stack of other tasks. Ignore. */
  111. break;
  112. default:
  113. printk("%sCode: Unable to access opcode bytes at 0x%lx.\n",
  114. loglvl, prologue);
  115. break;
  116. }
  117. }
  118. void show_ip(struct pt_regs *regs, const char *loglvl)
  119. {
  120. #ifdef CONFIG_X86_32
  121. printk("%sEIP: %pS\n", loglvl, (void *)regs->ip);
  122. #else
  123. printk("%sRIP: %04x:%pS\n", loglvl, (int)regs->cs, (void *)regs->ip);
  124. #endif
  125. show_opcodes(regs, loglvl);
  126. }
  127. void show_iret_regs(struct pt_regs *regs, const char *log_lvl)
  128. {
  129. show_ip(regs, log_lvl);
  130. printk("%sRSP: %04x:%016lx EFLAGS: %08lx", log_lvl, (int)regs->ss,
  131. regs->sp, regs->flags);
  132. }
  133. static void show_regs_if_on_stack(struct stack_info *info, struct pt_regs *regs,
  134. bool partial, const char *log_lvl)
  135. {
  136. /*
  137. * These on_stack() checks aren't strictly necessary: the unwind code
  138. * has already validated the 'regs' pointer. The checks are done for
  139. * ordering reasons: if the registers are on the next stack, we don't
  140. * want to print them out yet. Otherwise they'll be shown as part of
  141. * the wrong stack. Later, when show_trace_log_lvl() switches to the
  142. * next stack, this function will be called again with the same regs so
  143. * they can be printed in the right context.
  144. */
  145. if (!partial && on_stack(info, regs, sizeof(*regs))) {
  146. __show_regs(regs, SHOW_REGS_SHORT, log_lvl);
  147. } else if (partial && on_stack(info, (void *)regs + IRET_FRAME_OFFSET,
  148. IRET_FRAME_SIZE)) {
  149. /*
  150. * When an interrupt or exception occurs in entry code, the
  151. * full pt_regs might not have been saved yet. In that case
  152. * just print the iret frame.
  153. */
  154. show_iret_regs(regs, log_lvl);
  155. }
  156. }
  157. /*
  158. * This function reads pointers from the stack and dereferences them. The
  159. * pointers may not have their KMSAN shadow set up properly, which may result
  160. * in false positive reports. Disable instrumentation to avoid those.
  161. */
  162. __no_kmsan_checks
  163. static void show_trace_log_lvl(struct task_struct *task, struct pt_regs *regs,
  164. unsigned long *stack, const char *log_lvl)
  165. {
  166. struct unwind_state state;
  167. struct stack_info stack_info = {0};
  168. unsigned long visit_mask = 0;
  169. int graph_idx = 0;
  170. bool partial = false;
  171. printk("%sCall Trace:\n", log_lvl);
  172. unwind_start(&state, task, regs, stack);
  173. regs = unwind_get_entry_regs(&state, &partial);
  174. /*
  175. * Iterate through the stacks, starting with the current stack pointer.
  176. * Each stack has a pointer to the next one.
  177. *
  178. * x86-64 can have several stacks:
  179. * - task stack
  180. * - interrupt stack
  181. * - HW exception stacks (double fault, nmi, debug, mce)
  182. * - entry stack
  183. *
  184. * x86-32 can have up to four stacks:
  185. * - task stack
  186. * - softirq stack
  187. * - hardirq stack
  188. * - entry stack
  189. */
  190. for (stack = stack ?: get_stack_pointer(task, regs);
  191. stack;
  192. stack = stack_info.next_sp) {
  193. const char *stack_name;
  194. stack = PTR_ALIGN(stack, sizeof(long));
  195. if (get_stack_info(stack, task, &stack_info, &visit_mask)) {
  196. /*
  197. * We weren't on a valid stack. It's possible that
  198. * we overflowed a valid stack into a guard page.
  199. * See if the next page up is valid so that we can
  200. * generate some kind of backtrace if this happens.
  201. */
  202. stack = (unsigned long *)PAGE_ALIGN((unsigned long)stack);
  203. if (get_stack_info(stack, task, &stack_info, &visit_mask))
  204. break;
  205. }
  206. stack_name = stack_type_name(stack_info.type);
  207. if (stack_name)
  208. printk("%s <%s>\n", log_lvl, stack_name);
  209. if (regs)
  210. show_regs_if_on_stack(&stack_info, regs, partial, log_lvl);
  211. /*
  212. * Scan the stack, printing any text addresses we find. At the
  213. * same time, follow proper stack frames with the unwinder.
  214. *
  215. * Addresses found during the scan which are not reported by
  216. * the unwinder are considered to be additional clues which are
  217. * sometimes useful for debugging and are prefixed with '?'.
  218. * This also serves as a failsafe option in case the unwinder
  219. * goes off in the weeds.
  220. */
  221. for (; stack < stack_info.end; stack++) {
  222. unsigned long real_addr;
  223. int reliable = 0;
  224. unsigned long addr = READ_ONCE_NOCHECK(*stack);
  225. unsigned long *ret_addr_p =
  226. unwind_get_return_address_ptr(&state);
  227. if (!__kernel_text_address(addr))
  228. continue;
  229. /*
  230. * Don't print regs->ip again if it was already printed
  231. * by show_regs_if_on_stack().
  232. */
  233. if (regs && stack == &regs->ip)
  234. goto next;
  235. if (stack == ret_addr_p)
  236. reliable = 1;
  237. /*
  238. * When function graph tracing is enabled for a
  239. * function, its return address on the stack is
  240. * replaced with the address of an ftrace handler
  241. * (return_to_handler). In that case, before printing
  242. * the "real" address, we want to print the handler
  243. * address as an "unreliable" hint that function graph
  244. * tracing was involved.
  245. */
  246. real_addr = ftrace_graph_ret_addr(task, &graph_idx,
  247. addr, stack);
  248. if (real_addr != addr)
  249. printk_stack_address(addr, 0, log_lvl);
  250. printk_stack_address(real_addr, reliable, log_lvl);
  251. if (!reliable)
  252. continue;
  253. next:
  254. /*
  255. * Get the next frame from the unwinder. No need to
  256. * check for an error: if anything goes wrong, the rest
  257. * of the addresses will just be printed as unreliable.
  258. */
  259. unwind_next_frame(&state);
  260. /* if the frame has entry regs, print them */
  261. regs = unwind_get_entry_regs(&state, &partial);
  262. if (regs)
  263. show_regs_if_on_stack(&stack_info, regs, partial, log_lvl);
  264. }
  265. if (stack_name)
  266. printk("%s </%s>\n", log_lvl, stack_name);
  267. }
  268. }
  269. void show_stack(struct task_struct *task, unsigned long *sp,
  270. const char *loglvl)
  271. {
  272. task = task ? : current;
  273. /*
  274. * Stack frames below this one aren't interesting. Don't show them
  275. * if we're printing for %current.
  276. */
  277. if (!sp && task == current)
  278. sp = get_stack_pointer(current, NULL);
  279. show_trace_log_lvl(task, NULL, sp, loglvl);
  280. }
  281. void show_stack_regs(struct pt_regs *regs)
  282. {
  283. show_trace_log_lvl(current, regs, NULL, KERN_DEFAULT);
  284. }
  285. static arch_spinlock_t die_lock = __ARCH_SPIN_LOCK_UNLOCKED;
  286. static int die_owner = -1;
  287. static unsigned int die_nest_count;
  288. unsigned long oops_begin(void)
  289. {
  290. int cpu;
  291. unsigned long flags;
  292. oops_enter();
  293. /* racy, but better than risking deadlock. */
  294. raw_local_irq_save(flags);
  295. cpu = smp_processor_id();
  296. if (!arch_spin_trylock(&die_lock)) {
  297. if (cpu == die_owner)
  298. /* nested oops. should stop eventually */;
  299. else
  300. arch_spin_lock(&die_lock);
  301. }
  302. die_nest_count++;
  303. die_owner = cpu;
  304. console_verbose();
  305. bust_spinlocks(1);
  306. return flags;
  307. }
  308. NOKPROBE_SYMBOL(oops_begin);
  309. void __noreturn rewind_stack_and_make_dead(int signr);
  310. void oops_end(unsigned long flags, struct pt_regs *regs, int signr)
  311. {
  312. if (regs && kexec_should_crash(current))
  313. crash_kexec(regs);
  314. bust_spinlocks(0);
  315. die_owner = -1;
  316. add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
  317. die_nest_count--;
  318. if (!die_nest_count)
  319. /* Nest count reaches zero, release the lock. */
  320. arch_spin_unlock(&die_lock);
  321. raw_local_irq_restore(flags);
  322. oops_exit();
  323. /* Executive summary in case the oops scrolled away */
  324. __show_regs(&exec_summary_regs, SHOW_REGS_ALL, KERN_DEFAULT);
  325. if (!signr)
  326. return;
  327. if (in_interrupt())
  328. panic("Fatal exception in interrupt");
  329. if (panic_on_oops)
  330. panic("Fatal exception");
  331. /*
  332. * We're not going to return, but we might be on an IST stack or
  333. * have very little stack space left. Rewind the stack and kill
  334. * the task.
  335. * Before we rewind the stack, we have to tell KASAN that we're going to
  336. * reuse the task stack and that existing poisons are invalid.
  337. */
  338. kasan_unpoison_task_stack(current);
  339. rewind_stack_and_make_dead(signr);
  340. }
  341. NOKPROBE_SYMBOL(oops_end);
  342. static void __die_header(const char *str, struct pt_regs *regs, long err)
  343. {
  344. const char *pr = "";
  345. /* Save the regs of the first oops for the executive summary later. */
  346. if (!die_counter)
  347. exec_summary_regs = *regs;
  348. if (IS_ENABLED(CONFIG_PREEMPTION))
  349. pr = IS_ENABLED(CONFIG_PREEMPT_RT) ? " PREEMPT_RT" : " PREEMPT";
  350. printk(KERN_DEFAULT
  351. "%s: %04lx [#%d]%s%s%s%s%s\n", str, err & 0xffff, ++die_counter,
  352. pr,
  353. IS_ENABLED(CONFIG_SMP) ? " SMP" : "",
  354. debug_pagealloc_enabled() ? " DEBUG_PAGEALLOC" : "",
  355. IS_ENABLED(CONFIG_KASAN) ? " KASAN" : "",
  356. IS_ENABLED(CONFIG_PAGE_TABLE_ISOLATION) ?
  357. (boot_cpu_has(X86_FEATURE_PTI) ? " PTI" : " NOPTI") : "");
  358. }
  359. NOKPROBE_SYMBOL(__die_header);
  360. static int __die_body(const char *str, struct pt_regs *regs, long err)
  361. {
  362. show_regs(regs);
  363. print_modules();
  364. if (notify_die(DIE_OOPS, str, regs, err,
  365. current->thread.trap_nr, SIGSEGV) == NOTIFY_STOP)
  366. return 1;
  367. return 0;
  368. }
  369. NOKPROBE_SYMBOL(__die_body);
  370. int __die(const char *str, struct pt_regs *regs, long err)
  371. {
  372. __die_header(str, regs, err);
  373. return __die_body(str, regs, err);
  374. }
  375. NOKPROBE_SYMBOL(__die);
  376. /*
  377. * This is gone through when something in the kernel has done something bad
  378. * and is about to be terminated:
  379. */
  380. void die(const char *str, struct pt_regs *regs, long err)
  381. {
  382. unsigned long flags = oops_begin();
  383. int sig = SIGSEGV;
  384. if (__die(str, regs, err))
  385. sig = 0;
  386. oops_end(flags, regs, sig);
  387. }
  388. void die_addr(const char *str, struct pt_regs *regs, long err, long gp_addr)
  389. {
  390. unsigned long flags = oops_begin();
  391. int sig = SIGSEGV;
  392. __die_header(str, regs, err);
  393. if (gp_addr)
  394. kasan_non_canonical_hook(gp_addr);
  395. if (__die_body(str, regs, err))
  396. sig = 0;
  397. oops_end(flags, regs, sig);
  398. }
  399. void show_regs(struct pt_regs *regs)
  400. {
  401. enum show_regs_mode print_kernel_regs;
  402. show_regs_print_info(KERN_DEFAULT);
  403. print_kernel_regs = user_mode(regs) ? SHOW_REGS_USER : SHOW_REGS_ALL;
  404. __show_regs(regs, print_kernel_regs, KERN_DEFAULT);
  405. /*
  406. * When in-kernel, we also print out the stack at the time of the fault..
  407. */
  408. if (!user_mode(regs))
  409. show_trace_log_lvl(current, regs, NULL, KERN_DEFAULT);
  410. }