panic.c 14 KB

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