panic.c 13 KB

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