panic.c 16 KB

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