panic.c 20 KB

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