panic.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. /*
  2. * linux/kernel/panic.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. */
  6. /*
  7. * This function is used through-out the kernel (including mm and fs)
  8. * to indicate a major problem.
  9. */
  10. #include <linux/debug_locks.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/kmsg_dump.h>
  13. #include <linux/kallsyms.h>
  14. #include <linux/notifier.h>
  15. #include <linux/module.h>
  16. #include <linux/random.h>
  17. #include <linux/ftrace.h>
  18. #include <linux/reboot.h>
  19. #include <linux/delay.h>
  20. #include <linux/kexec.h>
  21. #include <linux/sched.h>
  22. #include <linux/sysrq.h>
  23. #include <linux/init.h>
  24. #include <linux/nmi.h>
  25. #include <linux/console.h>
  26. #include <linux/bug.h>
  27. #define PANIC_TIMER_STEP 100
  28. #define PANIC_BLINK_SPD 18
  29. int panic_on_oops = CONFIG_PANIC_ON_OOPS_VALUE;
  30. static unsigned long tainted_mask;
  31. static int pause_on_oops;
  32. static int pause_on_oops_flag;
  33. static DEFINE_SPINLOCK(pause_on_oops_lock);
  34. bool crash_kexec_post_notifiers;
  35. int panic_on_warn __read_mostly;
  36. int panic_timeout = CONFIG_PANIC_TIMEOUT;
  37. EXPORT_SYMBOL_GPL(panic_timeout);
  38. ATOMIC_NOTIFIER_HEAD(panic_notifier_list);
  39. EXPORT_SYMBOL(panic_notifier_list);
  40. static long no_blink(int state)
  41. {
  42. return 0;
  43. }
  44. /* Returns how long it waited in ms */
  45. long (*panic_blink)(int state);
  46. EXPORT_SYMBOL(panic_blink);
  47. /*
  48. * Stop ourself in panic -- architecture code may override this
  49. */
  50. void __weak panic_smp_self_stop(void)
  51. {
  52. while (1)
  53. cpu_relax();
  54. }
  55. /*
  56. * Stop ourselves in NMI context if another CPU has already panicked. Arch code
  57. * may override this to prepare for crash dumping, e.g. save regs info.
  58. */
  59. void __weak nmi_panic_self_stop(struct pt_regs *regs)
  60. {
  61. panic_smp_self_stop();
  62. }
  63. atomic_t panic_cpu = ATOMIC_INIT(PANIC_CPU_INVALID);
  64. /**
  65. * panic - halt the system
  66. * @fmt: The text string to print
  67. *
  68. * Display a message, then perform cleanups.
  69. *
  70. * This function never returns.
  71. */
  72. void panic(const char *fmt, ...)
  73. {
  74. static char buf[1024];
  75. va_list args;
  76. long i, i_next = 0;
  77. int state = 0;
  78. int old_cpu, this_cpu;
  79. /*
  80. * Disable local interrupts. This will prevent panic_smp_self_stop
  81. * from deadlocking the first cpu that invokes the panic, since
  82. * there is nothing to prevent an interrupt handler (that runs
  83. * after setting panic_cpu) from invoking panic() again.
  84. */
  85. local_irq_disable();
  86. /*
  87. * It's possible to come here directly from a panic-assertion and
  88. * not have preempt disabled. Some functions called from here want
  89. * preempt to be disabled. No point enabling it later though...
  90. *
  91. * Only one CPU is allowed to execute the panic code from here. For
  92. * multiple parallel invocations of panic, all other CPUs either
  93. * stop themself or will wait until they are stopped by the 1st CPU
  94. * with smp_send_stop().
  95. *
  96. * `old_cpu == PANIC_CPU_INVALID' means this is the 1st CPU which
  97. * comes here, so go ahead.
  98. * `old_cpu == this_cpu' means we came from nmi_panic() which sets
  99. * panic_cpu to this CPU. In this case, this is also the 1st CPU.
  100. */
  101. this_cpu = raw_smp_processor_id();
  102. old_cpu = atomic_cmpxchg(&panic_cpu, PANIC_CPU_INVALID, this_cpu);
  103. if (old_cpu != PANIC_CPU_INVALID && old_cpu != this_cpu)
  104. panic_smp_self_stop();
  105. console_verbose();
  106. bust_spinlocks(1);
  107. va_start(args, fmt);
  108. vsnprintf(buf, sizeof(buf), fmt, args);
  109. va_end(args);
  110. pr_emerg("Kernel panic - not syncing: %s\n", buf);
  111. #ifdef CONFIG_DEBUG_BUGVERBOSE
  112. /*
  113. * Avoid nested stack-dumping if a panic occurs during oops processing
  114. */
  115. if (!test_taint(TAINT_DIE) && oops_in_progress <= 1)
  116. dump_stack();
  117. #endif
  118. /*
  119. * If we have crashed and we have a crash kernel loaded let it handle
  120. * everything else.
  121. * If we want to run this after calling panic_notifiers, pass
  122. * the "crash_kexec_post_notifiers" option to the kernel.
  123. *
  124. * Bypass the panic_cpu check and call __crash_kexec directly.
  125. */
  126. if (!crash_kexec_post_notifiers)
  127. __crash_kexec(NULL);
  128. /*
  129. * Note smp_send_stop is the usual smp shutdown function, which
  130. * unfortunately means it may not be hardened to work in a panic
  131. * situation.
  132. */
  133. smp_send_stop();
  134. /*
  135. * Run any panic handlers, including those that might need to
  136. * add information to the kmsg dump output.
  137. */
  138. atomic_notifier_call_chain(&panic_notifier_list, 0, buf);
  139. kmsg_dump(KMSG_DUMP_PANIC);
  140. /*
  141. * If you doubt kdump always works fine in any situation,
  142. * "crash_kexec_post_notifiers" offers you a chance to run
  143. * panic_notifiers and dumping kmsg before kdump.
  144. * Note: since some panic_notifiers can make crashed kernel
  145. * more unstable, it can increase risks of the kdump failure too.
  146. *
  147. * Bypass the panic_cpu check and call __crash_kexec directly.
  148. */
  149. if (crash_kexec_post_notifiers)
  150. __crash_kexec(NULL);
  151. bust_spinlocks(0);
  152. /*
  153. * We may have ended up stopping the CPU holding the lock (in
  154. * smp_send_stop()) while still having some valuable data in the console
  155. * buffer. Try to acquire the lock then release it regardless of the
  156. * result. The release will also print the buffers out. Locks debug
  157. * should be disabled to avoid reporting bad unlock balance when
  158. * panic() is not being callled from OOPS.
  159. */
  160. debug_locks_off();
  161. console_flush_on_panic();
  162. if (!panic_blink)
  163. panic_blink = no_blink;
  164. if (panic_timeout > 0) {
  165. /*
  166. * Delay timeout seconds before rebooting the machine.
  167. * We can't use the "normal" timers since we just panicked.
  168. */
  169. pr_emerg("Rebooting in %d seconds..", panic_timeout);
  170. for (i = 0; i < panic_timeout * 1000; i += PANIC_TIMER_STEP) {
  171. touch_nmi_watchdog();
  172. if (i >= i_next) {
  173. i += panic_blink(state ^= 1);
  174. i_next = i + 3600 / PANIC_BLINK_SPD;
  175. }
  176. mdelay(PANIC_TIMER_STEP);
  177. }
  178. }
  179. if (panic_timeout != 0) {
  180. /*
  181. * This will not be a clean reboot, with everything
  182. * shutting down. But if there is a chance of
  183. * rebooting the system it will be rebooted.
  184. */
  185. emergency_restart();
  186. }
  187. #ifdef __sparc__
  188. {
  189. extern int stop_a_enabled;
  190. /* Make sure the user can actually press Stop-A (L1-A) */
  191. stop_a_enabled = 1;
  192. pr_emerg("Press Stop-A (L1-A) to return to the boot prom\n");
  193. }
  194. #endif
  195. #if defined(CONFIG_S390)
  196. {
  197. unsigned long caller;
  198. caller = (unsigned long)__builtin_return_address(0);
  199. disabled_wait(caller);
  200. }
  201. #endif
  202. pr_emerg("---[ end Kernel panic - not syncing: %s\n", buf);
  203. local_irq_enable();
  204. for (i = 0; ; i += PANIC_TIMER_STEP) {
  205. touch_softlockup_watchdog();
  206. if (i >= i_next) {
  207. i += panic_blink(state ^= 1);
  208. i_next = i + 3600 / PANIC_BLINK_SPD;
  209. }
  210. mdelay(PANIC_TIMER_STEP);
  211. }
  212. }
  213. EXPORT_SYMBOL(panic);
  214. struct tnt {
  215. u8 bit;
  216. char true;
  217. char false;
  218. };
  219. static const struct tnt tnts[] = {
  220. { TAINT_PROPRIETARY_MODULE, 'P', 'G' },
  221. { TAINT_FORCED_MODULE, 'F', ' ' },
  222. { TAINT_CPU_OUT_OF_SPEC, 'S', ' ' },
  223. { TAINT_FORCED_RMMOD, 'R', ' ' },
  224. { TAINT_MACHINE_CHECK, 'M', ' ' },
  225. { TAINT_BAD_PAGE, 'B', ' ' },
  226. { TAINT_USER, 'U', ' ' },
  227. { TAINT_DIE, 'D', ' ' },
  228. { TAINT_OVERRIDDEN_ACPI_TABLE, 'A', ' ' },
  229. { TAINT_WARN, 'W', ' ' },
  230. { TAINT_CRAP, 'C', ' ' },
  231. { TAINT_FIRMWARE_WORKAROUND, 'I', ' ' },
  232. { TAINT_OOT_MODULE, 'O', ' ' },
  233. { TAINT_UNSIGNED_MODULE, 'E', ' ' },
  234. { TAINT_SOFTLOCKUP, 'L', ' ' },
  235. { TAINT_LIVEPATCH, 'K', ' ' },
  236. };
  237. /**
  238. * print_tainted - return a string to represent the kernel taint state.
  239. *
  240. * 'P' - Proprietary module has been loaded.
  241. * 'F' - Module has been forcibly loaded.
  242. * 'S' - SMP with CPUs not designed for SMP.
  243. * 'R' - User forced a module unload.
  244. * 'M' - System experienced a machine check exception.
  245. * 'B' - System has hit bad_page.
  246. * 'U' - Userspace-defined naughtiness.
  247. * 'D' - Kernel has oopsed before
  248. * 'A' - ACPI table overridden.
  249. * 'W' - Taint on warning.
  250. * 'C' - modules from drivers/staging are loaded.
  251. * 'I' - Working around severe firmware bug.
  252. * 'O' - Out-of-tree module has been loaded.
  253. * 'E' - Unsigned module has been loaded.
  254. * 'L' - A soft lockup has previously occurred.
  255. * 'K' - Kernel has been live patched.
  256. *
  257. * The string is overwritten by the next call to print_tainted().
  258. */
  259. const char *print_tainted(void)
  260. {
  261. static char buf[ARRAY_SIZE(tnts) + sizeof("Tainted: ")];
  262. if (tainted_mask) {
  263. char *s;
  264. int i;
  265. s = buf + sprintf(buf, "Tainted: ");
  266. for (i = 0; i < ARRAY_SIZE(tnts); i++) {
  267. const struct tnt *t = &tnts[i];
  268. *s++ = test_bit(t->bit, &tainted_mask) ?
  269. t->true : t->false;
  270. }
  271. *s = 0;
  272. } else
  273. snprintf(buf, sizeof(buf), "Not tainted");
  274. return buf;
  275. }
  276. int test_taint(unsigned flag)
  277. {
  278. return test_bit(flag, &tainted_mask);
  279. }
  280. EXPORT_SYMBOL(test_taint);
  281. unsigned long get_taint(void)
  282. {
  283. return tainted_mask;
  284. }
  285. /**
  286. * add_taint: add a taint flag if not already set.
  287. * @flag: one of the TAINT_* constants.
  288. * @lockdep_ok: whether lock debugging is still OK.
  289. *
  290. * If something bad has gone wrong, you'll want @lockdebug_ok = false, but for
  291. * some notewortht-but-not-corrupting cases, it can be set to true.
  292. */
  293. void add_taint(unsigned flag, enum lockdep_ok lockdep_ok)
  294. {
  295. if (lockdep_ok == LOCKDEP_NOW_UNRELIABLE && __debug_locks_off())
  296. pr_warn("Disabling lock debugging due to kernel taint\n");
  297. set_bit(flag, &tainted_mask);
  298. }
  299. EXPORT_SYMBOL(add_taint);
  300. static void spin_msec(int msecs)
  301. {
  302. int i;
  303. for (i = 0; i < msecs; i++) {
  304. touch_nmi_watchdog();
  305. mdelay(1);
  306. }
  307. }
  308. /*
  309. * It just happens that oops_enter() and oops_exit() are identically
  310. * implemented...
  311. */
  312. static void do_oops_enter_exit(void)
  313. {
  314. unsigned long flags;
  315. static int spin_counter;
  316. if (!pause_on_oops)
  317. return;
  318. spin_lock_irqsave(&pause_on_oops_lock, flags);
  319. if (pause_on_oops_flag == 0) {
  320. /* This CPU may now print the oops message */
  321. pause_on_oops_flag = 1;
  322. } else {
  323. /* We need to stall this CPU */
  324. if (!spin_counter) {
  325. /* This CPU gets to do the counting */
  326. spin_counter = pause_on_oops;
  327. do {
  328. spin_unlock(&pause_on_oops_lock);
  329. spin_msec(MSEC_PER_SEC);
  330. spin_lock(&pause_on_oops_lock);
  331. } while (--spin_counter);
  332. pause_on_oops_flag = 0;
  333. } else {
  334. /* This CPU waits for a different one */
  335. while (spin_counter) {
  336. spin_unlock(&pause_on_oops_lock);
  337. spin_msec(1);
  338. spin_lock(&pause_on_oops_lock);
  339. }
  340. }
  341. }
  342. spin_unlock_irqrestore(&pause_on_oops_lock, flags);
  343. }
  344. /*
  345. * Return true if the calling CPU is allowed to print oops-related info.
  346. * This is a bit racy..
  347. */
  348. int oops_may_print(void)
  349. {
  350. return pause_on_oops_flag == 0;
  351. }
  352. /*
  353. * Called when the architecture enters its oops handler, before it prints
  354. * anything. If this is the first CPU to oops, and it's oopsing the first
  355. * time then let it proceed.
  356. *
  357. * This is all enabled by the pause_on_oops kernel boot option. We do all
  358. * this to ensure that oopses don't scroll off the screen. It has the
  359. * side-effect of preventing later-oopsing CPUs from mucking up the display,
  360. * too.
  361. *
  362. * It turns out that the CPU which is allowed to print ends up pausing for
  363. * the right duration, whereas all the other CPUs pause for twice as long:
  364. * once in oops_enter(), once in oops_exit().
  365. */
  366. void oops_enter(void)
  367. {
  368. tracing_off();
  369. /* can't trust the integrity of the kernel anymore: */
  370. debug_locks_off();
  371. do_oops_enter_exit();
  372. }
  373. /*
  374. * 64-bit random ID for oopses:
  375. */
  376. static u64 oops_id;
  377. static int init_oops_id(void)
  378. {
  379. if (!oops_id)
  380. get_random_bytes(&oops_id, sizeof(oops_id));
  381. else
  382. oops_id++;
  383. return 0;
  384. }
  385. late_initcall(init_oops_id);
  386. void print_oops_end_marker(void)
  387. {
  388. init_oops_id();
  389. pr_warn("---[ end trace %016llx ]---\n", (unsigned long long)oops_id);
  390. }
  391. /*
  392. * Called when the architecture exits its oops handler, after printing
  393. * everything.
  394. */
  395. void oops_exit(void)
  396. {
  397. do_oops_enter_exit();
  398. print_oops_end_marker();
  399. kmsg_dump(KMSG_DUMP_OOPS);
  400. }
  401. struct warn_args {
  402. const char *fmt;
  403. va_list args;
  404. };
  405. void __warn(const char *file, int line, void *caller, unsigned taint,
  406. struct pt_regs *regs, struct warn_args *args)
  407. {
  408. disable_trace_on_warning();
  409. pr_warn("------------[ cut here ]------------\n");
  410. if (file)
  411. pr_warn("WARNING: CPU: %d PID: %d at %s:%d %pS\n",
  412. raw_smp_processor_id(), current->pid, file, line,
  413. caller);
  414. else
  415. pr_warn("WARNING: CPU: %d PID: %d at %pS\n",
  416. raw_smp_processor_id(), current->pid, caller);
  417. if (args)
  418. vprintk(args->fmt, args->args);
  419. if (panic_on_warn) {
  420. /*
  421. * This thread may hit another WARN() in the panic path.
  422. * Resetting this prevents additional WARN() from panicking the
  423. * system on this thread. Other threads are blocked by the
  424. * panic_mutex in panic().
  425. */
  426. panic_on_warn = 0;
  427. panic("panic_on_warn set ...\n");
  428. }
  429. print_modules();
  430. if (regs)
  431. show_regs(regs);
  432. else
  433. dump_stack();
  434. print_oops_end_marker();
  435. /* Just a warning, don't kill lockdep. */
  436. add_taint(taint, LOCKDEP_STILL_OK);
  437. }
  438. #ifdef WANT_WARN_ON_SLOWPATH
  439. void warn_slowpath_fmt(const char *file, int line, const char *fmt, ...)
  440. {
  441. struct warn_args args;
  442. args.fmt = fmt;
  443. va_start(args.args, fmt);
  444. __warn(file, line, __builtin_return_address(0), TAINT_WARN, NULL,
  445. &args);
  446. va_end(args.args);
  447. }
  448. EXPORT_SYMBOL(warn_slowpath_fmt);
  449. void warn_slowpath_fmt_taint(const char *file, int line,
  450. unsigned taint, const char *fmt, ...)
  451. {
  452. struct warn_args args;
  453. args.fmt = fmt;
  454. va_start(args.args, fmt);
  455. __warn(file, line, __builtin_return_address(0), taint, NULL, &args);
  456. va_end(args.args);
  457. }
  458. EXPORT_SYMBOL(warn_slowpath_fmt_taint);
  459. void warn_slowpath_null(const char *file, int line)
  460. {
  461. __warn(file, line, __builtin_return_address(0), TAINT_WARN, NULL, NULL);
  462. }
  463. EXPORT_SYMBOL(warn_slowpath_null);
  464. #endif
  465. #ifdef CONFIG_CC_STACKPROTECTOR
  466. /*
  467. * Called when gcc's -fstack-protector feature is used, and
  468. * gcc detects corruption of the on-stack canary value
  469. */
  470. __visible void __stack_chk_fail(void)
  471. {
  472. panic("stack-protector: Kernel stack is corrupted in: %p\n",
  473. __builtin_return_address(0));
  474. }
  475. EXPORT_SYMBOL(__stack_chk_fail);
  476. #endif
  477. core_param(panic, panic_timeout, int, 0644);
  478. core_param(pause_on_oops, pause_on_oops, int, 0644);
  479. core_param(panic_on_warn, panic_on_warn, int, 0644);
  480. static int __init setup_crash_kexec_post_notifiers(char *s)
  481. {
  482. crash_kexec_post_notifiers = true;
  483. return 0;
  484. }
  485. early_param("crash_kexec_post_notifiers", setup_crash_kexec_post_notifiers);
  486. static int __init oops_setup(char *s)
  487. {
  488. if (!s)
  489. return -EINVAL;
  490. if (!strcmp(s, "panic"))
  491. panic_on_oops = 1;
  492. return 0;
  493. }
  494. early_param("oops", oops_setup);