panic.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * linux/kernel/panic.c
  4. *
  5. * Copyright (C) 1991, 1992 Linus Torvalds
  6. */
  7. /*
  8. * This function is used through-out the kernel (including mm and fs)
  9. * to indicate a major problem.
  10. */
  11. #include <linux/debug_locks.h>
  12. #include <linux/sched/debug.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/kgdb.h>
  15. #include <linux/kmsg_dump.h>
  16. #include <linux/kallsyms.h>
  17. #include <linux/notifier.h>
  18. #include <linux/vt_kern.h>
  19. #include <linux/module.h>
  20. #include <linux/random.h>
  21. #include <linux/ftrace.h>
  22. #include <linux/reboot.h>
  23. #include <linux/delay.h>
  24. #include <linux/kexec.h>
  25. #include <linux/panic_notifier.h>
  26. #include <linux/sched.h>
  27. #include <linux/string_helpers.h>
  28. #include <linux/sysrq.h>
  29. #include <linux/init.h>
  30. #include <linux/nmi.h>
  31. #include <linux/console.h>
  32. #include <linux/bug.h>
  33. #include <linux/ratelimit.h>
  34. #include <linux/debugfs.h>
  35. #include <linux/sysfs.h>
  36. #include <linux/context_tracking.h>
  37. #include <linux/seq_buf.h>
  38. #include <trace/events/error_report.h>
  39. #include <asm/sections.h>
  40. #define PANIC_TIMER_STEP 100
  41. #define PANIC_BLINK_SPD 18
  42. #ifdef CONFIG_SMP
  43. /*
  44. * Should we dump all CPUs backtraces in an oops event?
  45. * Defaults to 0, can be changed via sysctl.
  46. */
  47. static unsigned int __read_mostly sysctl_oops_all_cpu_backtrace;
  48. #else
  49. #define sysctl_oops_all_cpu_backtrace 0
  50. #endif /* CONFIG_SMP */
  51. int panic_on_oops = CONFIG_PANIC_ON_OOPS_VALUE;
  52. static unsigned long tainted_mask =
  53. IS_ENABLED(CONFIG_RANDSTRUCT) ? (1 << TAINT_RANDSTRUCT) : 0;
  54. static int pause_on_oops;
  55. static int pause_on_oops_flag;
  56. static DEFINE_SPINLOCK(pause_on_oops_lock);
  57. bool crash_kexec_post_notifiers;
  58. int panic_on_warn __read_mostly;
  59. unsigned long panic_on_taint;
  60. bool panic_on_taint_nousertaint = false;
  61. static unsigned int warn_limit __read_mostly;
  62. bool panic_triggering_all_cpu_backtrace;
  63. int panic_timeout = CONFIG_PANIC_TIMEOUT;
  64. EXPORT_SYMBOL_GPL(panic_timeout);
  65. #define PANIC_PRINT_TASK_INFO 0x00000001
  66. #define PANIC_PRINT_MEM_INFO 0x00000002
  67. #define PANIC_PRINT_TIMER_INFO 0x00000004
  68. #define PANIC_PRINT_LOCK_INFO 0x00000008
  69. #define PANIC_PRINT_FTRACE_INFO 0x00000010
  70. #define PANIC_PRINT_ALL_PRINTK_MSG 0x00000020
  71. #define PANIC_PRINT_ALL_CPU_BT 0x00000040
  72. #define PANIC_PRINT_BLOCKED_TASKS 0x00000080
  73. unsigned long panic_print;
  74. ATOMIC_NOTIFIER_HEAD(panic_notifier_list);
  75. EXPORT_SYMBOL(panic_notifier_list);
  76. #ifdef CONFIG_SYSCTL
  77. static struct ctl_table kern_panic_table[] = {
  78. #ifdef CONFIG_SMP
  79. {
  80. .procname = "oops_all_cpu_backtrace",
  81. .data = &sysctl_oops_all_cpu_backtrace,
  82. .maxlen = sizeof(int),
  83. .mode = 0644,
  84. .proc_handler = proc_dointvec_minmax,
  85. .extra1 = SYSCTL_ZERO,
  86. .extra2 = SYSCTL_ONE,
  87. },
  88. #endif
  89. {
  90. .procname = "warn_limit",
  91. .data = &warn_limit,
  92. .maxlen = sizeof(warn_limit),
  93. .mode = 0644,
  94. .proc_handler = proc_douintvec,
  95. },
  96. };
  97. static __init int kernel_panic_sysctls_init(void)
  98. {
  99. register_sysctl_init("kernel", kern_panic_table);
  100. return 0;
  101. }
  102. late_initcall(kernel_panic_sysctls_init);
  103. #endif
  104. static atomic_t warn_count = ATOMIC_INIT(0);
  105. #ifdef CONFIG_SYSFS
  106. static ssize_t warn_count_show(struct kobject *kobj, struct kobj_attribute *attr,
  107. char *page)
  108. {
  109. return sysfs_emit(page, "%d\n", atomic_read(&warn_count));
  110. }
  111. static struct kobj_attribute warn_count_attr = __ATTR_RO(warn_count);
  112. static __init int kernel_panic_sysfs_init(void)
  113. {
  114. sysfs_add_file_to_group(kernel_kobj, &warn_count_attr.attr, NULL);
  115. return 0;
  116. }
  117. late_initcall(kernel_panic_sysfs_init);
  118. #endif
  119. static long no_blink(int state)
  120. {
  121. return 0;
  122. }
  123. /* Returns how long it waited in ms */
  124. long (*panic_blink)(int state);
  125. EXPORT_SYMBOL(panic_blink);
  126. /*
  127. * Stop ourself in panic -- architecture code may override this
  128. */
  129. void __weak __noreturn panic_smp_self_stop(void)
  130. {
  131. while (1)
  132. cpu_relax();
  133. }
  134. /*
  135. * Stop ourselves in NMI context if another CPU has already panicked. Arch code
  136. * may override this to prepare for crash dumping, e.g. save regs info.
  137. */
  138. void __weak __noreturn nmi_panic_self_stop(struct pt_regs *regs)
  139. {
  140. panic_smp_self_stop();
  141. }
  142. /*
  143. * Stop other CPUs in panic. Architecture dependent code may override this
  144. * with more suitable version. For example, if the architecture supports
  145. * crash dump, it should save registers of each stopped CPU and disable
  146. * per-CPU features such as virtualization extensions.
  147. */
  148. void __weak crash_smp_send_stop(void)
  149. {
  150. static int cpus_stopped;
  151. /*
  152. * This function can be called twice in panic path, but obviously
  153. * we execute this only once.
  154. */
  155. if (cpus_stopped)
  156. return;
  157. /*
  158. * Note smp_send_stop is the usual smp shutdown function, which
  159. * unfortunately means it may not be hardened to work in a panic
  160. * situation.
  161. */
  162. smp_send_stop();
  163. cpus_stopped = 1;
  164. }
  165. atomic_t panic_cpu = ATOMIC_INIT(PANIC_CPU_INVALID);
  166. /*
  167. * A variant of panic() called from NMI context. We return if we've already
  168. * panicked on this CPU. If another CPU already panicked, loop in
  169. * nmi_panic_self_stop() which can provide architecture dependent code such
  170. * as saving register state for crash dump.
  171. */
  172. void nmi_panic(struct pt_regs *regs, const char *msg)
  173. {
  174. int old_cpu, this_cpu;
  175. old_cpu = PANIC_CPU_INVALID;
  176. this_cpu = raw_smp_processor_id();
  177. /* atomic_try_cmpxchg updates old_cpu on failure */
  178. if (atomic_try_cmpxchg(&panic_cpu, &old_cpu, this_cpu))
  179. panic("%s", msg);
  180. else if (old_cpu != this_cpu)
  181. nmi_panic_self_stop(regs);
  182. }
  183. EXPORT_SYMBOL(nmi_panic);
  184. static void panic_print_sys_info(bool console_flush)
  185. {
  186. if (console_flush) {
  187. if (panic_print & PANIC_PRINT_ALL_PRINTK_MSG)
  188. console_flush_on_panic(CONSOLE_REPLAY_ALL);
  189. return;
  190. }
  191. if (panic_print & PANIC_PRINT_TASK_INFO)
  192. show_state();
  193. if (panic_print & PANIC_PRINT_MEM_INFO)
  194. show_mem();
  195. if (panic_print & PANIC_PRINT_TIMER_INFO)
  196. sysrq_timer_list_show();
  197. if (panic_print & PANIC_PRINT_LOCK_INFO)
  198. debug_show_all_locks();
  199. if (panic_print & PANIC_PRINT_FTRACE_INFO)
  200. ftrace_dump(DUMP_ALL);
  201. if (panic_print & PANIC_PRINT_BLOCKED_TASKS)
  202. show_state_filter(TASK_UNINTERRUPTIBLE);
  203. }
  204. void check_panic_on_warn(const char *origin)
  205. {
  206. unsigned int limit;
  207. if (panic_on_warn)
  208. panic("%s: panic_on_warn set ...\n", origin);
  209. limit = READ_ONCE(warn_limit);
  210. if (atomic_inc_return(&warn_count) >= limit && limit)
  211. panic("%s: system warned too often (kernel.warn_limit is %d)",
  212. origin, limit);
  213. }
  214. /*
  215. * Helper that triggers the NMI backtrace (if set in panic_print)
  216. * and then performs the secondary CPUs shutdown - we cannot have
  217. * the NMI backtrace after the CPUs are off!
  218. */
  219. static void panic_other_cpus_shutdown(bool crash_kexec)
  220. {
  221. if (panic_print & PANIC_PRINT_ALL_CPU_BT) {
  222. /* Temporary allow non-panic CPUs to write their backtraces. */
  223. panic_triggering_all_cpu_backtrace = true;
  224. trigger_all_cpu_backtrace();
  225. panic_triggering_all_cpu_backtrace = false;
  226. }
  227. /*
  228. * Note that smp_send_stop() is the usual SMP shutdown function,
  229. * which unfortunately may not be hardened to work in a panic
  230. * situation. If we want to do crash dump after notifier calls
  231. * and kmsg_dump, we will need architecture dependent extra
  232. * bits in addition to stopping other CPUs, hence we rely on
  233. * crash_smp_send_stop() for that.
  234. */
  235. if (!crash_kexec)
  236. smp_send_stop();
  237. else
  238. crash_smp_send_stop();
  239. }
  240. /**
  241. * panic - halt the system
  242. * @fmt: The text string to print
  243. *
  244. * Display a message, then perform cleanups.
  245. *
  246. * This function never returns.
  247. */
  248. void panic(const char *fmt, ...)
  249. {
  250. static char buf[1024];
  251. va_list args;
  252. long i, i_next = 0, len;
  253. int state = 0;
  254. int old_cpu, this_cpu;
  255. bool _crash_kexec_post_notifiers = crash_kexec_post_notifiers;
  256. if (panic_on_warn) {
  257. /*
  258. * This thread may hit another WARN() in the panic path.
  259. * Resetting this prevents additional WARN() from panicking the
  260. * system on this thread. Other threads are blocked by the
  261. * panic_mutex in panic().
  262. */
  263. panic_on_warn = 0;
  264. }
  265. /*
  266. * Disable local interrupts. This will prevent panic_smp_self_stop
  267. * from deadlocking the first cpu that invokes the panic, since
  268. * there is nothing to prevent an interrupt handler (that runs
  269. * after setting panic_cpu) from invoking panic() again.
  270. */
  271. local_irq_disable();
  272. preempt_disable_notrace();
  273. /*
  274. * It's possible to come here directly from a panic-assertion and
  275. * not have preempt disabled. Some functions called from here want
  276. * preempt to be disabled. No point enabling it later though...
  277. *
  278. * Only one CPU is allowed to execute the panic code from here. For
  279. * multiple parallel invocations of panic, all other CPUs either
  280. * stop themself or will wait until they are stopped by the 1st CPU
  281. * with smp_send_stop().
  282. *
  283. * cmpxchg success means this is the 1st CPU which comes here,
  284. * so go ahead.
  285. * `old_cpu == this_cpu' means we came from nmi_panic() which sets
  286. * panic_cpu to this CPU. In this case, this is also the 1st CPU.
  287. */
  288. old_cpu = PANIC_CPU_INVALID;
  289. this_cpu = raw_smp_processor_id();
  290. /* atomic_try_cmpxchg updates old_cpu on failure */
  291. if (atomic_try_cmpxchg(&panic_cpu, &old_cpu, this_cpu)) {
  292. /* go ahead */
  293. } else if (old_cpu != this_cpu)
  294. panic_smp_self_stop();
  295. console_verbose();
  296. bust_spinlocks(1);
  297. va_start(args, fmt);
  298. len = vscnprintf(buf, sizeof(buf), fmt, args);
  299. va_end(args);
  300. if (len && buf[len - 1] == '\n')
  301. buf[len - 1] = '\0';
  302. pr_emerg("Kernel panic - not syncing: %s\n", buf);
  303. #ifdef CONFIG_DEBUG_BUGVERBOSE
  304. /*
  305. * Avoid nested stack-dumping if a panic occurs during oops processing
  306. */
  307. if (!test_taint(TAINT_DIE) && oops_in_progress <= 1)
  308. dump_stack();
  309. #endif
  310. /*
  311. * If kgdb is enabled, give it a chance to run before we stop all
  312. * the other CPUs or else we won't be able to debug processes left
  313. * running on them.
  314. */
  315. kgdb_panic(buf);
  316. /*
  317. * If we have crashed and we have a crash kernel loaded let it handle
  318. * everything else.
  319. * If we want to run this after calling panic_notifiers, pass
  320. * the "crash_kexec_post_notifiers" option to the kernel.
  321. *
  322. * Bypass the panic_cpu check and call __crash_kexec directly.
  323. */
  324. if (!_crash_kexec_post_notifiers)
  325. __crash_kexec(NULL);
  326. panic_other_cpus_shutdown(_crash_kexec_post_notifiers);
  327. printk_legacy_allow_panic_sync();
  328. /*
  329. * Run any panic handlers, including those that might need to
  330. * add information to the kmsg dump output.
  331. */
  332. atomic_notifier_call_chain(&panic_notifier_list, 0, buf);
  333. panic_print_sys_info(false);
  334. kmsg_dump_desc(KMSG_DUMP_PANIC, buf);
  335. /*
  336. * If you doubt kdump always works fine in any situation,
  337. * "crash_kexec_post_notifiers" offers you a chance to run
  338. * panic_notifiers and dumping kmsg before kdump.
  339. * Note: since some panic_notifiers can make crashed kernel
  340. * more unstable, it can increase risks of the kdump failure too.
  341. *
  342. * Bypass the panic_cpu check and call __crash_kexec directly.
  343. */
  344. if (_crash_kexec_post_notifiers)
  345. __crash_kexec(NULL);
  346. console_unblank();
  347. /*
  348. * We may have ended up stopping the CPU holding the lock (in
  349. * smp_send_stop()) while still having some valuable data in the console
  350. * buffer. Try to acquire the lock then release it regardless of the
  351. * result. The release will also print the buffers out. Locks debug
  352. * should be disabled to avoid reporting bad unlock balance when
  353. * panic() is not being callled from OOPS.
  354. */
  355. debug_locks_off();
  356. console_flush_on_panic(CONSOLE_FLUSH_PENDING);
  357. panic_print_sys_info(true);
  358. if (!panic_blink)
  359. panic_blink = no_blink;
  360. if (panic_timeout > 0) {
  361. /*
  362. * Delay timeout seconds before rebooting the machine.
  363. * We can't use the "normal" timers since we just panicked.
  364. */
  365. pr_emerg("Rebooting in %d seconds..\n", panic_timeout);
  366. for (i = 0; i < panic_timeout * 1000; i += PANIC_TIMER_STEP) {
  367. touch_nmi_watchdog();
  368. if (i >= i_next) {
  369. i += panic_blink(state ^= 1);
  370. i_next = i + 3600 / PANIC_BLINK_SPD;
  371. }
  372. mdelay(PANIC_TIMER_STEP);
  373. }
  374. }
  375. if (panic_timeout != 0) {
  376. /*
  377. * This will not be a clean reboot, with everything
  378. * shutting down. But if there is a chance of
  379. * rebooting the system it will be rebooted.
  380. */
  381. if (panic_reboot_mode != REBOOT_UNDEFINED)
  382. reboot_mode = panic_reboot_mode;
  383. emergency_restart();
  384. }
  385. #ifdef __sparc__
  386. {
  387. extern int stop_a_enabled;
  388. /* Make sure the user can actually press Stop-A (L1-A) */
  389. stop_a_enabled = 1;
  390. pr_emerg("Press Stop-A (L1-A) from sun keyboard or send break\n"
  391. "twice on console to return to the boot prom\n");
  392. }
  393. #endif
  394. #if defined(CONFIG_S390)
  395. disabled_wait();
  396. #endif
  397. pr_emerg("---[ end Kernel panic - not syncing: %s ]---\n", buf);
  398. /* Do not scroll important messages printed above */
  399. suppress_printk = 1;
  400. /*
  401. * The final messages may not have been printed if in a context that
  402. * defers printing (such as NMI) and irq_work is not available.
  403. * Explicitly flush the kernel log buffer one last time.
  404. */
  405. console_flush_on_panic(CONSOLE_FLUSH_PENDING);
  406. nbcon_atomic_flush_unsafe();
  407. local_irq_enable();
  408. for (i = 0; ; i += PANIC_TIMER_STEP) {
  409. touch_softlockup_watchdog();
  410. if (i >= i_next) {
  411. i += panic_blink(state ^= 1);
  412. i_next = i + 3600 / PANIC_BLINK_SPD;
  413. }
  414. mdelay(PANIC_TIMER_STEP);
  415. }
  416. }
  417. EXPORT_SYMBOL(panic);
  418. #define TAINT_FLAG(taint, _c_true, _c_false, _module) \
  419. [ TAINT_##taint ] = { \
  420. .c_true = _c_true, .c_false = _c_false, \
  421. .module = _module, \
  422. .desc = #taint, \
  423. }
  424. /*
  425. * TAINT_FORCED_RMMOD could be a per-module flag but the module
  426. * is being removed anyway.
  427. */
  428. const struct taint_flag taint_flags[TAINT_FLAGS_COUNT] = {
  429. TAINT_FLAG(PROPRIETARY_MODULE, 'P', 'G', true),
  430. TAINT_FLAG(FORCED_MODULE, 'F', ' ', true),
  431. TAINT_FLAG(CPU_OUT_OF_SPEC, 'S', ' ', false),
  432. TAINT_FLAG(FORCED_RMMOD, 'R', ' ', false),
  433. TAINT_FLAG(MACHINE_CHECK, 'M', ' ', false),
  434. TAINT_FLAG(BAD_PAGE, 'B', ' ', false),
  435. TAINT_FLAG(USER, 'U', ' ', false),
  436. TAINT_FLAG(DIE, 'D', ' ', false),
  437. TAINT_FLAG(OVERRIDDEN_ACPI_TABLE, 'A', ' ', false),
  438. TAINT_FLAG(WARN, 'W', ' ', false),
  439. TAINT_FLAG(CRAP, 'C', ' ', true),
  440. TAINT_FLAG(FIRMWARE_WORKAROUND, 'I', ' ', false),
  441. TAINT_FLAG(OOT_MODULE, 'O', ' ', true),
  442. TAINT_FLAG(UNSIGNED_MODULE, 'E', ' ', true),
  443. TAINT_FLAG(SOFTLOCKUP, 'L', ' ', false),
  444. TAINT_FLAG(LIVEPATCH, 'K', ' ', true),
  445. TAINT_FLAG(AUX, 'X', ' ', true),
  446. TAINT_FLAG(RANDSTRUCT, 'T', ' ', true),
  447. TAINT_FLAG(TEST, 'N', ' ', true),
  448. };
  449. #undef TAINT_FLAG
  450. static void print_tainted_seq(struct seq_buf *s, bool verbose)
  451. {
  452. const char *sep = "";
  453. int i;
  454. if (!tainted_mask) {
  455. seq_buf_puts(s, "Not tainted");
  456. return;
  457. }
  458. seq_buf_printf(s, "Tainted: ");
  459. for (i = 0; i < TAINT_FLAGS_COUNT; i++) {
  460. const struct taint_flag *t = &taint_flags[i];
  461. bool is_set = test_bit(i, &tainted_mask);
  462. char c = is_set ? t->c_true : t->c_false;
  463. if (verbose) {
  464. if (is_set) {
  465. seq_buf_printf(s, "%s[%c]=%s", sep, c, t->desc);
  466. sep = ", ";
  467. }
  468. } else {
  469. seq_buf_putc(s, c);
  470. }
  471. }
  472. }
  473. static const char *_print_tainted(bool verbose)
  474. {
  475. /* FIXME: what should the size be? */
  476. static char buf[sizeof(taint_flags)];
  477. struct seq_buf s;
  478. BUILD_BUG_ON(ARRAY_SIZE(taint_flags) != TAINT_FLAGS_COUNT);
  479. seq_buf_init(&s, buf, sizeof(buf));
  480. print_tainted_seq(&s, verbose);
  481. return seq_buf_str(&s);
  482. }
  483. /**
  484. * print_tainted - return a string to represent the kernel taint state.
  485. *
  486. * For individual taint flag meanings, see Documentation/admin-guide/sysctl/kernel.rst
  487. *
  488. * The string is overwritten by the next call to print_tainted(),
  489. * but is always NULL terminated.
  490. */
  491. const char *print_tainted(void)
  492. {
  493. return _print_tainted(false);
  494. }
  495. /**
  496. * print_tainted_verbose - A more verbose version of print_tainted()
  497. */
  498. const char *print_tainted_verbose(void)
  499. {
  500. return _print_tainted(true);
  501. }
  502. int test_taint(unsigned flag)
  503. {
  504. return test_bit(flag, &tainted_mask);
  505. }
  506. EXPORT_SYMBOL(test_taint);
  507. unsigned long get_taint(void)
  508. {
  509. return tainted_mask;
  510. }
  511. /**
  512. * add_taint: add a taint flag if not already set.
  513. * @flag: one of the TAINT_* constants.
  514. * @lockdep_ok: whether lock debugging is still OK.
  515. *
  516. * If something bad has gone wrong, you'll want @lockdebug_ok = false, but for
  517. * some notewortht-but-not-corrupting cases, it can be set to true.
  518. */
  519. void add_taint(unsigned flag, enum lockdep_ok lockdep_ok)
  520. {
  521. if (lockdep_ok == LOCKDEP_NOW_UNRELIABLE && __debug_locks_off())
  522. pr_warn("Disabling lock debugging due to kernel taint\n");
  523. set_bit(flag, &tainted_mask);
  524. if (tainted_mask & panic_on_taint) {
  525. panic_on_taint = 0;
  526. panic("panic_on_taint set ...");
  527. }
  528. }
  529. EXPORT_SYMBOL(add_taint);
  530. static void spin_msec(int msecs)
  531. {
  532. int i;
  533. for (i = 0; i < msecs; i++) {
  534. touch_nmi_watchdog();
  535. mdelay(1);
  536. }
  537. }
  538. /*
  539. * It just happens that oops_enter() and oops_exit() are identically
  540. * implemented...
  541. */
  542. static void do_oops_enter_exit(void)
  543. {
  544. unsigned long flags;
  545. static int spin_counter;
  546. if (!pause_on_oops)
  547. return;
  548. spin_lock_irqsave(&pause_on_oops_lock, flags);
  549. if (pause_on_oops_flag == 0) {
  550. /* This CPU may now print the oops message */
  551. pause_on_oops_flag = 1;
  552. } else {
  553. /* We need to stall this CPU */
  554. if (!spin_counter) {
  555. /* This CPU gets to do the counting */
  556. spin_counter = pause_on_oops;
  557. do {
  558. spin_unlock(&pause_on_oops_lock);
  559. spin_msec(MSEC_PER_SEC);
  560. spin_lock(&pause_on_oops_lock);
  561. } while (--spin_counter);
  562. pause_on_oops_flag = 0;
  563. } else {
  564. /* This CPU waits for a different one */
  565. while (spin_counter) {
  566. spin_unlock(&pause_on_oops_lock);
  567. spin_msec(1);
  568. spin_lock(&pause_on_oops_lock);
  569. }
  570. }
  571. }
  572. spin_unlock_irqrestore(&pause_on_oops_lock, flags);
  573. }
  574. /*
  575. * Return true if the calling CPU is allowed to print oops-related info.
  576. * This is a bit racy..
  577. */
  578. bool oops_may_print(void)
  579. {
  580. return pause_on_oops_flag == 0;
  581. }
  582. /*
  583. * Called when the architecture enters its oops handler, before it prints
  584. * anything. If this is the first CPU to oops, and it's oopsing the first
  585. * time then let it proceed.
  586. *
  587. * This is all enabled by the pause_on_oops kernel boot option. We do all
  588. * this to ensure that oopses don't scroll off the screen. It has the
  589. * side-effect of preventing later-oopsing CPUs from mucking up the display,
  590. * too.
  591. *
  592. * It turns out that the CPU which is allowed to print ends up pausing for
  593. * the right duration, whereas all the other CPUs pause for twice as long:
  594. * once in oops_enter(), once in oops_exit().
  595. */
  596. void oops_enter(void)
  597. {
  598. nbcon_cpu_emergency_enter();
  599. tracing_off();
  600. /* can't trust the integrity of the kernel anymore: */
  601. debug_locks_off();
  602. do_oops_enter_exit();
  603. if (sysctl_oops_all_cpu_backtrace)
  604. trigger_all_cpu_backtrace();
  605. }
  606. static void print_oops_end_marker(void)
  607. {
  608. pr_warn("---[ end trace %016llx ]---\n", 0ULL);
  609. }
  610. /*
  611. * Called when the architecture exits its oops handler, after printing
  612. * everything.
  613. */
  614. void oops_exit(void)
  615. {
  616. do_oops_enter_exit();
  617. print_oops_end_marker();
  618. nbcon_cpu_emergency_exit();
  619. kmsg_dump(KMSG_DUMP_OOPS);
  620. }
  621. struct warn_args {
  622. const char *fmt;
  623. va_list args;
  624. };
  625. void __warn(const char *file, int line, void *caller, unsigned taint,
  626. struct pt_regs *regs, struct warn_args *args)
  627. {
  628. nbcon_cpu_emergency_enter();
  629. disable_trace_on_warning();
  630. if (file)
  631. pr_warn("WARNING: CPU: %d PID: %d at %s:%d %pS\n",
  632. raw_smp_processor_id(), current->pid, file, line,
  633. caller);
  634. else
  635. pr_warn("WARNING: CPU: %d PID: %d at %pS\n",
  636. raw_smp_processor_id(), current->pid, caller);
  637. #pragma GCC diagnostic push
  638. #ifndef __clang__
  639. #pragma GCC diagnostic ignored "-Wsuggest-attribute=format"
  640. #endif
  641. if (args)
  642. vprintk(args->fmt, args->args);
  643. #pragma GCC diagnostic pop
  644. print_modules();
  645. if (regs)
  646. show_regs(regs);
  647. check_panic_on_warn("kernel");
  648. if (!regs)
  649. dump_stack();
  650. print_irqtrace_events(current);
  651. print_oops_end_marker();
  652. trace_error_report_end(ERROR_DETECTOR_WARN, (unsigned long)caller);
  653. /* Just a warning, don't kill lockdep. */
  654. add_taint(taint, LOCKDEP_STILL_OK);
  655. nbcon_cpu_emergency_exit();
  656. }
  657. #ifdef CONFIG_BUG
  658. #ifndef __WARN_FLAGS
  659. void warn_slowpath_fmt(const char *file, int line, unsigned taint,
  660. const char *fmt, ...)
  661. {
  662. bool rcu = warn_rcu_enter();
  663. struct warn_args args;
  664. pr_warn(CUT_HERE);
  665. if (!fmt) {
  666. __warn(file, line, __builtin_return_address(0), taint,
  667. NULL, NULL);
  668. warn_rcu_exit(rcu);
  669. return;
  670. }
  671. args.fmt = fmt;
  672. va_start(args.args, fmt);
  673. __warn(file, line, __builtin_return_address(0), taint, NULL, &args);
  674. va_end(args.args);
  675. warn_rcu_exit(rcu);
  676. }
  677. EXPORT_SYMBOL(warn_slowpath_fmt);
  678. #else
  679. void __warn_printk(const char *fmt, ...)
  680. {
  681. bool rcu = warn_rcu_enter();
  682. va_list args;
  683. pr_warn(CUT_HERE);
  684. va_start(args, fmt);
  685. vprintk(fmt, args);
  686. va_end(args);
  687. warn_rcu_exit(rcu);
  688. }
  689. EXPORT_SYMBOL(__warn_printk);
  690. #endif
  691. /* Support resetting WARN*_ONCE state */
  692. static int clear_warn_once_set(void *data, u64 val)
  693. {
  694. generic_bug_clear_once();
  695. memset(__start_once, 0, __end_once - __start_once);
  696. return 0;
  697. }
  698. DEFINE_DEBUGFS_ATTRIBUTE(clear_warn_once_fops, NULL, clear_warn_once_set,
  699. "%lld\n");
  700. static __init int register_warn_debugfs(void)
  701. {
  702. /* Don't care about failure */
  703. debugfs_create_file_unsafe("clear_warn_once", 0200, NULL, NULL,
  704. &clear_warn_once_fops);
  705. return 0;
  706. }
  707. device_initcall(register_warn_debugfs);
  708. #endif
  709. #ifdef CONFIG_STACKPROTECTOR
  710. /*
  711. * Called when gcc's -fstack-protector feature is used, and
  712. * gcc detects corruption of the on-stack canary value
  713. */
  714. __visible noinstr void __stack_chk_fail(void)
  715. {
  716. unsigned long flags;
  717. instrumentation_begin();
  718. flags = user_access_save();
  719. panic("stack-protector: Kernel stack is corrupted in: %pB",
  720. __builtin_return_address(0));
  721. user_access_restore(flags);
  722. instrumentation_end();
  723. }
  724. EXPORT_SYMBOL(__stack_chk_fail);
  725. #endif
  726. core_param(panic, panic_timeout, int, 0644);
  727. core_param(panic_print, panic_print, ulong, 0644);
  728. core_param(pause_on_oops, pause_on_oops, int, 0644);
  729. core_param(panic_on_warn, panic_on_warn, int, 0644);
  730. core_param(crash_kexec_post_notifiers, crash_kexec_post_notifiers, bool, 0644);
  731. static int __init oops_setup(char *s)
  732. {
  733. if (!s)
  734. return -EINVAL;
  735. if (!strcmp(s, "panic"))
  736. panic_on_oops = 1;
  737. return 0;
  738. }
  739. early_param("oops", oops_setup);
  740. static int __init panic_on_taint_setup(char *s)
  741. {
  742. char *taint_str;
  743. if (!s)
  744. return -EINVAL;
  745. taint_str = strsep(&s, ",");
  746. if (kstrtoul(taint_str, 16, &panic_on_taint))
  747. return -EINVAL;
  748. /* make sure panic_on_taint doesn't hold out-of-range TAINT flags */
  749. panic_on_taint &= TAINT_FLAGS_MAX;
  750. if (!panic_on_taint)
  751. return -EINVAL;
  752. if (s && !strcmp(s, "nousertaint"))
  753. panic_on_taint_nousertaint = true;
  754. pr_info("panic_on_taint: bitmask=0x%lx nousertaint_mode=%s\n",
  755. panic_on_taint, str_enabled_disabled(panic_on_taint_nousertaint));
  756. return 0;
  757. }
  758. early_param("panic_on_taint", panic_on_taint_setup);