panic.c 11 KB

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