panic.c 16 KB

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