panic.c 20 KB

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