1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * linux/kernel/panic.c 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * Copyright (C) 1991, 1992 Linus Torvalds 61da177e4SLinus Torvalds */ 71da177e4SLinus Torvalds 81da177e4SLinus Torvalds /* 91da177e4SLinus Torvalds * This function is used through-out the kernel (including mm and fs) 101da177e4SLinus Torvalds * to indicate a major problem. 111da177e4SLinus Torvalds */ 12657b3010SAndrew Morton #include <linux/debug_locks.h> 13b17b0153SIngo Molnar #include <linux/sched/debug.h> 14c95dbf27SIngo Molnar #include <linux/interrupt.h> 157d92bda2SDouglas Anderson #include <linux/kgdb.h> 16456b565cSSimon Kagstrom #include <linux/kmsg_dump.h> 1779b4cc5eSArjan van de Ven #include <linux/kallsyms.h> 18c95dbf27SIngo Molnar #include <linux/notifier.h> 19c7c3f05eSSergey Senozhatsky #include <linux/vt_kern.h> 20c95dbf27SIngo Molnar #include <linux/module.h> 21c95dbf27SIngo Molnar #include <linux/random.h> 22de7edd31SSteven Rostedt (Red Hat) #include <linux/ftrace.h> 23c95dbf27SIngo Molnar #include <linux/reboot.h> 24c95dbf27SIngo Molnar #include <linux/delay.h> 25c95dbf27SIngo Molnar #include <linux/kexec.h> 26f39650deSAndy Shevchenko #include <linux/panic_notifier.h> 27c95dbf27SIngo Molnar #include <linux/sched.h> 28c95dbf27SIngo Molnar #include <linux/sysrq.h> 29c95dbf27SIngo Molnar #include <linux/init.h> 30c95dbf27SIngo Molnar #include <linux/nmi.h> 3108d78658SVitaly Kuznetsov #include <linux/console.h> 322553b67aSJosh Poimboeuf #include <linux/bug.h> 337a46ec0eSKees Cook #include <linux/ratelimit.h> 34b1fca27dSAndi Kleen #include <linux/debugfs.h> 3523b36fecSMarco Elver #include <trace/events/error_report.h> 36b1fca27dSAndi Kleen #include <asm/sections.h> 371da177e4SLinus Torvalds 38c7ff0d9cSTAMUKI Shoichi #define PANIC_TIMER_STEP 100 39c7ff0d9cSTAMUKI Shoichi #define PANIC_BLINK_SPD 18 40c7ff0d9cSTAMUKI Shoichi 4160c958d8SGuilherme G. Piccoli #ifdef CONFIG_SMP 4260c958d8SGuilherme G. Piccoli /* 4360c958d8SGuilherme G. Piccoli * Should we dump all CPUs backtraces in an oops event? 4460c958d8SGuilherme G. Piccoli * Defaults to 0, can be changed via sysctl. 4560c958d8SGuilherme G. Piccoli */ 4660c958d8SGuilherme G. Piccoli unsigned int __read_mostly sysctl_oops_all_cpu_backtrace; 4760c958d8SGuilherme G. Piccoli #endif /* CONFIG_SMP */ 4860c958d8SGuilherme G. Piccoli 492a01bb38SKyle McMartin int panic_on_oops = CONFIG_PANIC_ON_OOPS_VALUE; 50bc4f2f54SKees Cook static unsigned long tainted_mask = 51*595b893eSKees Cook IS_ENABLED(CONFIG_RANDSTRUCT) ? (1 << TAINT_RANDSTRUCT) : 0; 52dd287796SAndrew Morton static int pause_on_oops; 53dd287796SAndrew Morton static int pause_on_oops_flag; 54dd287796SAndrew Morton static DEFINE_SPINLOCK(pause_on_oops_lock); 555375b708SHATAYAMA Daisuke bool crash_kexec_post_notifiers; 569e3961a0SPrarit Bhargava int panic_on_warn __read_mostly; 57db38d5c1SRafael Aquini unsigned long panic_on_taint; 58db38d5c1SRafael Aquini bool panic_on_taint_nousertaint = false; 591da177e4SLinus Torvalds 605800dc3cSJason Baron int panic_timeout = CONFIG_PANIC_TIMEOUT; 6181e88fdcSHuang Ying EXPORT_SYMBOL_GPL(panic_timeout); 621da177e4SLinus Torvalds 63d999bd93SFeng Tang #define PANIC_PRINT_TASK_INFO 0x00000001 64d999bd93SFeng Tang #define PANIC_PRINT_MEM_INFO 0x00000002 65d999bd93SFeng Tang #define PANIC_PRINT_TIMER_INFO 0x00000004 66d999bd93SFeng Tang #define PANIC_PRINT_LOCK_INFO 0x00000008 67d999bd93SFeng Tang #define PANIC_PRINT_FTRACE_INFO 0x00000010 68de6da1e8SFeng Tang #define PANIC_PRINT_ALL_PRINTK_MSG 0x00000020 698d470a45SGuilherme G. Piccoli #define PANIC_PRINT_ALL_CPU_BT 0x00000040 7081c9d43fSFeng Tang unsigned long panic_print; 71d999bd93SFeng Tang 72e041c683SAlan Stern ATOMIC_NOTIFIER_HEAD(panic_notifier_list); 731da177e4SLinus Torvalds 741da177e4SLinus Torvalds EXPORT_SYMBOL(panic_notifier_list); 751da177e4SLinus Torvalds 76c7ff0d9cSTAMUKI Shoichi static long no_blink(int state) 778aeee85aSAnton Blanchard { 78c7ff0d9cSTAMUKI Shoichi return 0; 79c7ff0d9cSTAMUKI Shoichi } 808aeee85aSAnton Blanchard 81c7ff0d9cSTAMUKI Shoichi /* Returns how long it waited in ms */ 82c7ff0d9cSTAMUKI Shoichi long (*panic_blink)(int state); 83c7ff0d9cSTAMUKI Shoichi EXPORT_SYMBOL(panic_blink); 848aeee85aSAnton Blanchard 8593e13a36SMichael Holzheu /* 8693e13a36SMichael Holzheu * Stop ourself in panic -- architecture code may override this 8793e13a36SMichael Holzheu */ 8893e13a36SMichael Holzheu void __weak panic_smp_self_stop(void) 8993e13a36SMichael Holzheu { 9093e13a36SMichael Holzheu while (1) 9193e13a36SMichael Holzheu cpu_relax(); 9293e13a36SMichael Holzheu } 9393e13a36SMichael Holzheu 9458c5661fSHidehiro Kawai /* 9558c5661fSHidehiro Kawai * Stop ourselves in NMI context if another CPU has already panicked. Arch code 9658c5661fSHidehiro Kawai * may override this to prepare for crash dumping, e.g. save regs info. 9758c5661fSHidehiro Kawai */ 9858c5661fSHidehiro Kawai void __weak nmi_panic_self_stop(struct pt_regs *regs) 9958c5661fSHidehiro Kawai { 10058c5661fSHidehiro Kawai panic_smp_self_stop(); 10158c5661fSHidehiro Kawai } 10258c5661fSHidehiro Kawai 1030ee59413SHidehiro Kawai /* 1040ee59413SHidehiro Kawai * Stop other CPUs in panic. Architecture dependent code may override this 1050ee59413SHidehiro Kawai * with more suitable version. For example, if the architecture supports 1060ee59413SHidehiro Kawai * crash dump, it should save registers of each stopped CPU and disable 1070ee59413SHidehiro Kawai * per-CPU features such as virtualization extensions. 1080ee59413SHidehiro Kawai */ 1090ee59413SHidehiro Kawai void __weak crash_smp_send_stop(void) 1100ee59413SHidehiro Kawai { 1110ee59413SHidehiro Kawai static int cpus_stopped; 1120ee59413SHidehiro Kawai 1130ee59413SHidehiro Kawai /* 1140ee59413SHidehiro Kawai * This function can be called twice in panic path, but obviously 1150ee59413SHidehiro Kawai * we execute this only once. 1160ee59413SHidehiro Kawai */ 1170ee59413SHidehiro Kawai if (cpus_stopped) 1180ee59413SHidehiro Kawai return; 1190ee59413SHidehiro Kawai 1200ee59413SHidehiro Kawai /* 1210ee59413SHidehiro Kawai * Note smp_send_stop is the usual smp shutdown function, which 1220ee59413SHidehiro Kawai * unfortunately means it may not be hardened to work in a panic 1230ee59413SHidehiro Kawai * situation. 1240ee59413SHidehiro Kawai */ 1250ee59413SHidehiro Kawai smp_send_stop(); 1260ee59413SHidehiro Kawai cpus_stopped = 1; 1270ee59413SHidehiro Kawai } 1280ee59413SHidehiro Kawai 1291717f209SHidehiro Kawai atomic_t panic_cpu = ATOMIC_INIT(PANIC_CPU_INVALID); 1301717f209SHidehiro Kawai 131ebc41f20SHidehiro Kawai /* 132ebc41f20SHidehiro Kawai * A variant of panic() called from NMI context. We return if we've already 133ebc41f20SHidehiro Kawai * panicked on this CPU. If another CPU already panicked, loop in 134ebc41f20SHidehiro Kawai * nmi_panic_self_stop() which can provide architecture dependent code such 135ebc41f20SHidehiro Kawai * as saving register state for crash dump. 136ebc41f20SHidehiro Kawai */ 137ebc41f20SHidehiro Kawai void nmi_panic(struct pt_regs *regs, const char *msg) 138ebc41f20SHidehiro Kawai { 139ebc41f20SHidehiro Kawai int old_cpu, cpu; 140ebc41f20SHidehiro Kawai 141ebc41f20SHidehiro Kawai cpu = raw_smp_processor_id(); 142ebc41f20SHidehiro Kawai old_cpu = atomic_cmpxchg(&panic_cpu, PANIC_CPU_INVALID, cpu); 143ebc41f20SHidehiro Kawai 144ebc41f20SHidehiro Kawai if (old_cpu == PANIC_CPU_INVALID) 145ebc41f20SHidehiro Kawai panic("%s", msg); 146ebc41f20SHidehiro Kawai else if (old_cpu != cpu) 147ebc41f20SHidehiro Kawai nmi_panic_self_stop(regs); 148ebc41f20SHidehiro Kawai } 149ebc41f20SHidehiro Kawai EXPORT_SYMBOL(nmi_panic); 150ebc41f20SHidehiro Kawai 151f953f140SGuilherme G. Piccoli static void panic_print_sys_info(bool console_flush) 152d999bd93SFeng Tang { 153f953f140SGuilherme G. Piccoli if (console_flush) { 154de6da1e8SFeng Tang if (panic_print & PANIC_PRINT_ALL_PRINTK_MSG) 155de6da1e8SFeng Tang console_flush_on_panic(CONSOLE_REPLAY_ALL); 156f953f140SGuilherme G. Piccoli return; 157f953f140SGuilherme G. Piccoli } 158de6da1e8SFeng Tang 1598d470a45SGuilherme G. Piccoli if (panic_print & PANIC_PRINT_ALL_CPU_BT) 1608d470a45SGuilherme G. Piccoli trigger_all_cpu_backtrace(); 1618d470a45SGuilherme G. Piccoli 162d999bd93SFeng Tang if (panic_print & PANIC_PRINT_TASK_INFO) 163d999bd93SFeng Tang show_state(); 164d999bd93SFeng Tang 165d999bd93SFeng Tang if (panic_print & PANIC_PRINT_MEM_INFO) 166d999bd93SFeng Tang show_mem(0, NULL); 167d999bd93SFeng Tang 168d999bd93SFeng Tang if (panic_print & PANIC_PRINT_TIMER_INFO) 169d999bd93SFeng Tang sysrq_timer_list_show(); 170d999bd93SFeng Tang 171d999bd93SFeng Tang if (panic_print & PANIC_PRINT_LOCK_INFO) 172d999bd93SFeng Tang debug_show_all_locks(); 173d999bd93SFeng Tang 174d999bd93SFeng Tang if (panic_print & PANIC_PRINT_FTRACE_INFO) 175d999bd93SFeng Tang ftrace_dump(DUMP_ALL); 176d999bd93SFeng Tang } 177d999bd93SFeng Tang 1781da177e4SLinus Torvalds /** 1791da177e4SLinus Torvalds * panic - halt the system 1801da177e4SLinus Torvalds * @fmt: The text string to print 1811da177e4SLinus Torvalds * 1821da177e4SLinus Torvalds * Display a message, then perform cleanups. 1831da177e4SLinus Torvalds * 1841da177e4SLinus Torvalds * This function never returns. 1851da177e4SLinus Torvalds */ 1869402c95fSJoe Perches void panic(const char *fmt, ...) 1871da177e4SLinus Torvalds { 1881da177e4SLinus Torvalds static char buf[1024]; 1891da177e4SLinus Torvalds va_list args; 190b49dec1cSBorislav Petkov long i, i_next = 0, len; 191c7ff0d9cSTAMUKI Shoichi int state = 0; 1921717f209SHidehiro Kawai int old_cpu, this_cpu; 193b26e27ddSHidehiro Kawai bool _crash_kexec_post_notifiers = crash_kexec_post_notifiers; 1941da177e4SLinus Torvalds 1951a2383e8STiezhu Yang if (panic_on_warn) { 1961a2383e8STiezhu Yang /* 1971a2383e8STiezhu Yang * This thread may hit another WARN() in the panic path. 1981a2383e8STiezhu Yang * Resetting this prevents additional WARN() from panicking the 1991a2383e8STiezhu Yang * system on this thread. Other threads are blocked by the 2001a2383e8STiezhu Yang * panic_mutex in panic(). 2011a2383e8STiezhu Yang */ 2021a2383e8STiezhu Yang panic_on_warn = 0; 2031a2383e8STiezhu Yang } 2041a2383e8STiezhu Yang 205dc009d92SEric W. Biederman /* 206190320c3SVikram Mulukutla * Disable local interrupts. This will prevent panic_smp_self_stop 207190320c3SVikram Mulukutla * from deadlocking the first cpu that invokes the panic, since 208190320c3SVikram Mulukutla * there is nothing to prevent an interrupt handler (that runs 2091717f209SHidehiro Kawai * after setting panic_cpu) from invoking panic() again. 210190320c3SVikram Mulukutla */ 211190320c3SVikram Mulukutla local_irq_disable(); 21220bb759aSWill Deacon preempt_disable_notrace(); 213190320c3SVikram Mulukutla 214190320c3SVikram Mulukutla /* 215c95dbf27SIngo Molnar * It's possible to come here directly from a panic-assertion and 216c95dbf27SIngo Molnar * not have preempt disabled. Some functions called from here want 217dc009d92SEric W. Biederman * preempt to be disabled. No point enabling it later though... 21893e13a36SMichael Holzheu * 21993e13a36SMichael Holzheu * Only one CPU is allowed to execute the panic code from here. For 22093e13a36SMichael Holzheu * multiple parallel invocations of panic, all other CPUs either 22193e13a36SMichael Holzheu * stop themself or will wait until they are stopped by the 1st CPU 22293e13a36SMichael Holzheu * with smp_send_stop(). 2231717f209SHidehiro Kawai * 2241717f209SHidehiro Kawai * `old_cpu == PANIC_CPU_INVALID' means this is the 1st CPU which 2251717f209SHidehiro Kawai * comes here, so go ahead. 2261717f209SHidehiro Kawai * `old_cpu == this_cpu' means we came from nmi_panic() which sets 2271717f209SHidehiro Kawai * panic_cpu to this CPU. In this case, this is also the 1st CPU. 228dc009d92SEric W. Biederman */ 2291717f209SHidehiro Kawai this_cpu = raw_smp_processor_id(); 2301717f209SHidehiro Kawai old_cpu = atomic_cmpxchg(&panic_cpu, PANIC_CPU_INVALID, this_cpu); 2311717f209SHidehiro Kawai 2321717f209SHidehiro Kawai if (old_cpu != PANIC_CPU_INVALID && old_cpu != this_cpu) 23393e13a36SMichael Holzheu panic_smp_self_stop(); 234dc009d92SEric W. Biederman 2355b530fc1SAnton Blanchard console_verbose(); 2361da177e4SLinus Torvalds bust_spinlocks(1); 2371da177e4SLinus Torvalds va_start(args, fmt); 238b49dec1cSBorislav Petkov len = vscnprintf(buf, sizeof(buf), fmt, args); 2391da177e4SLinus Torvalds va_end(args); 240b49dec1cSBorislav Petkov 241b49dec1cSBorislav Petkov if (len && buf[len - 1] == '\n') 242b49dec1cSBorislav Petkov buf[len - 1] = '\0'; 243b49dec1cSBorislav Petkov 244d7c0847fSFabian Frederick pr_emerg("Kernel panic - not syncing: %s\n", buf); 2455cb27301SIngo Molnar #ifdef CONFIG_DEBUG_BUGVERBOSE 2466e6f0a1fSAndi Kleen /* 2476e6f0a1fSAndi Kleen * Avoid nested stack-dumping if a panic occurs during oops processing 2486e6f0a1fSAndi Kleen */ 249026ee1f6SJason Wessel if (!test_taint(TAINT_DIE) && oops_in_progress <= 1) 2505cb27301SIngo Molnar dump_stack(); 2515cb27301SIngo Molnar #endif 2521da177e4SLinus Torvalds 253dc009d92SEric W. Biederman /* 2547d92bda2SDouglas Anderson * If kgdb is enabled, give it a chance to run before we stop all 2557d92bda2SDouglas Anderson * the other CPUs or else we won't be able to debug processes left 2567d92bda2SDouglas Anderson * running on them. 2577d92bda2SDouglas Anderson */ 2587d92bda2SDouglas Anderson kgdb_panic(buf); 2597d92bda2SDouglas Anderson 2607d92bda2SDouglas Anderson /* 261dc009d92SEric W. Biederman * If we have crashed and we have a crash kernel loaded let it handle 262dc009d92SEric W. Biederman * everything else. 263f06e5153SMasami Hiramatsu * If we want to run this after calling panic_notifiers, pass 264f06e5153SMasami Hiramatsu * the "crash_kexec_post_notifiers" option to the kernel. 2657bbee5caSHidehiro Kawai * 2667bbee5caSHidehiro Kawai * Bypass the panic_cpu check and call __crash_kexec directly. 267dc009d92SEric W. Biederman */ 268b26e27ddSHidehiro Kawai if (!_crash_kexec_post_notifiers) { 2697bbee5caSHidehiro Kawai __crash_kexec(NULL); 270dc009d92SEric W. Biederman 271dc009d92SEric W. Biederman /* 272dc009d92SEric W. Biederman * Note smp_send_stop is the usual smp shutdown function, which 2730ee59413SHidehiro Kawai * unfortunately means it may not be hardened to work in a 2740ee59413SHidehiro Kawai * panic situation. 275dc009d92SEric W. Biederman */ 2761da177e4SLinus Torvalds smp_send_stop(); 2770ee59413SHidehiro Kawai } else { 2780ee59413SHidehiro Kawai /* 2790ee59413SHidehiro Kawai * If we want to do crash dump after notifier calls and 2800ee59413SHidehiro Kawai * kmsg_dump, we will need architecture dependent extra 2810ee59413SHidehiro Kawai * works in addition to stopping other CPUs. 2820ee59413SHidehiro Kawai */ 2830ee59413SHidehiro Kawai crash_smp_send_stop(); 2840ee59413SHidehiro Kawai } 2851da177e4SLinus Torvalds 2866723734cSKees Cook /* 2876723734cSKees Cook * Run any panic handlers, including those that might need to 2886723734cSKees Cook * add information to the kmsg dump output. 2896723734cSKees Cook */ 290e041c683SAlan Stern atomic_notifier_call_chain(&panic_notifier_list, 0, buf); 2911da177e4SLinus Torvalds 292f953f140SGuilherme G. Piccoli panic_print_sys_info(false); 293f953f140SGuilherme G. Piccoli 2946723734cSKees Cook kmsg_dump(KMSG_DUMP_PANIC); 2956723734cSKees Cook 296f06e5153SMasami Hiramatsu /* 297f06e5153SMasami Hiramatsu * If you doubt kdump always works fine in any situation, 298f06e5153SMasami Hiramatsu * "crash_kexec_post_notifiers" offers you a chance to run 299f06e5153SMasami Hiramatsu * panic_notifiers and dumping kmsg before kdump. 300f06e5153SMasami Hiramatsu * Note: since some panic_notifiers can make crashed kernel 301f06e5153SMasami Hiramatsu * more unstable, it can increase risks of the kdump failure too. 3027bbee5caSHidehiro Kawai * 3037bbee5caSHidehiro Kawai * Bypass the panic_cpu check and call __crash_kexec directly. 304f06e5153SMasami Hiramatsu */ 305b26e27ddSHidehiro Kawai if (_crash_kexec_post_notifiers) 3067bbee5caSHidehiro Kawai __crash_kexec(NULL); 307f06e5153SMasami Hiramatsu 308c7c3f05eSSergey Senozhatsky #ifdef CONFIG_VT 309c7c3f05eSSergey Senozhatsky unblank_screen(); 310c7c3f05eSSergey Senozhatsky #endif 311c7c3f05eSSergey Senozhatsky console_unblank(); 312d014e889SAaro Koskinen 31308d78658SVitaly Kuznetsov /* 31408d78658SVitaly Kuznetsov * We may have ended up stopping the CPU holding the lock (in 31508d78658SVitaly Kuznetsov * smp_send_stop()) while still having some valuable data in the console 31608d78658SVitaly Kuznetsov * buffer. Try to acquire the lock then release it regardless of the 3177625b3a0SVitaly Kuznetsov * result. The release will also print the buffers out. Locks debug 3187625b3a0SVitaly Kuznetsov * should be disabled to avoid reporting bad unlock balance when 3197625b3a0SVitaly Kuznetsov * panic() is not being callled from OOPS. 32008d78658SVitaly Kuznetsov */ 3217625b3a0SVitaly Kuznetsov debug_locks_off(); 322de6da1e8SFeng Tang console_flush_on_panic(CONSOLE_FLUSH_PENDING); 32308d78658SVitaly Kuznetsov 324f953f140SGuilherme G. Piccoli panic_print_sys_info(true); 325d999bd93SFeng Tang 326c7ff0d9cSTAMUKI Shoichi if (!panic_blink) 327c7ff0d9cSTAMUKI Shoichi panic_blink = no_blink; 328c7ff0d9cSTAMUKI Shoichi 329dc009d92SEric W. Biederman if (panic_timeout > 0) { 3301da177e4SLinus Torvalds /* 3311da177e4SLinus Torvalds * Delay timeout seconds before rebooting the machine. 332c95dbf27SIngo Molnar * We can't use the "normal" timers since we just panicked. 3331da177e4SLinus Torvalds */ 334ff7a28a0SJiri Slaby pr_emerg("Rebooting in %d seconds..\n", panic_timeout); 335c95dbf27SIngo Molnar 336c7ff0d9cSTAMUKI Shoichi for (i = 0; i < panic_timeout * 1000; i += PANIC_TIMER_STEP) { 3371da177e4SLinus Torvalds touch_nmi_watchdog(); 338c7ff0d9cSTAMUKI Shoichi if (i >= i_next) { 339c7ff0d9cSTAMUKI Shoichi i += panic_blink(state ^= 1); 340c7ff0d9cSTAMUKI Shoichi i_next = i + 3600 / PANIC_BLINK_SPD; 341c7ff0d9cSTAMUKI Shoichi } 342c7ff0d9cSTAMUKI Shoichi mdelay(PANIC_TIMER_STEP); 3431da177e4SLinus Torvalds } 3444302fbc8SHugh Dickins } 3454302fbc8SHugh Dickins if (panic_timeout != 0) { 346c95dbf27SIngo Molnar /* 347c95dbf27SIngo Molnar * This will not be a clean reboot, with everything 3482f048ea8SEric W. Biederman * shutting down. But if there is a chance of 3492f048ea8SEric W. Biederman * rebooting the system it will be rebooted. 3501da177e4SLinus Torvalds */ 351b287a25aSAaro Koskinen if (panic_reboot_mode != REBOOT_UNDEFINED) 352b287a25aSAaro Koskinen reboot_mode = panic_reboot_mode; 3532f048ea8SEric W. Biederman emergency_restart(); 3541da177e4SLinus Torvalds } 3551da177e4SLinus Torvalds #ifdef __sparc__ 3561da177e4SLinus Torvalds { 3571da177e4SLinus Torvalds extern int stop_a_enabled; 358a271c241STom 'spot' Callaway /* Make sure the user can actually press Stop-A (L1-A) */ 3591da177e4SLinus Torvalds stop_a_enabled = 1; 3607db60d05SVijay Kumar pr_emerg("Press Stop-A (L1-A) from sun keyboard or send break\n" 3617db60d05SVijay Kumar "twice on console to return to the boot prom\n"); 3621da177e4SLinus Torvalds } 3631da177e4SLinus Torvalds #endif 364347a8dc3SMartin Schwidefsky #if defined(CONFIG_S390) 36598587c2dSMartin Schwidefsky disabled_wait(); 3661da177e4SLinus Torvalds #endif 3675ad75105SBorislav Petkov pr_emerg("---[ end Kernel panic - not syncing: %s ]---\n", buf); 368c39ea0b9SFeng Tang 369c39ea0b9SFeng Tang /* Do not scroll important messages printed above */ 370c39ea0b9SFeng Tang suppress_printk = 1; 3711da177e4SLinus Torvalds local_irq_enable(); 372c7ff0d9cSTAMUKI Shoichi for (i = 0; ; i += PANIC_TIMER_STEP) { 373c22db941SJan Beulich touch_softlockup_watchdog(); 374c7ff0d9cSTAMUKI Shoichi if (i >= i_next) { 375c7ff0d9cSTAMUKI Shoichi i += panic_blink(state ^= 1); 376c7ff0d9cSTAMUKI Shoichi i_next = i + 3600 / PANIC_BLINK_SPD; 377c7ff0d9cSTAMUKI Shoichi } 378c7ff0d9cSTAMUKI Shoichi mdelay(PANIC_TIMER_STEP); 3791da177e4SLinus Torvalds } 3801da177e4SLinus Torvalds } 3811da177e4SLinus Torvalds 3821da177e4SLinus Torvalds EXPORT_SYMBOL(panic); 3831da177e4SLinus Torvalds 3847fd8329bSPetr Mladek /* 3857fd8329bSPetr Mladek * TAINT_FORCED_RMMOD could be a per-module flag but the module 3867fd8329bSPetr Mladek * is being removed anyway. 3877fd8329bSPetr Mladek */ 3887fd8329bSPetr Mladek const struct taint_flag taint_flags[TAINT_FLAGS_COUNT] = { 38947d4b263SKees Cook [ TAINT_PROPRIETARY_MODULE ] = { 'P', 'G', true }, 39047d4b263SKees Cook [ TAINT_FORCED_MODULE ] = { 'F', ' ', true }, 39147d4b263SKees Cook [ TAINT_CPU_OUT_OF_SPEC ] = { 'S', ' ', false }, 39247d4b263SKees Cook [ TAINT_FORCED_RMMOD ] = { 'R', ' ', false }, 39347d4b263SKees Cook [ TAINT_MACHINE_CHECK ] = { 'M', ' ', false }, 39447d4b263SKees Cook [ TAINT_BAD_PAGE ] = { 'B', ' ', false }, 39547d4b263SKees Cook [ TAINT_USER ] = { 'U', ' ', false }, 39647d4b263SKees Cook [ TAINT_DIE ] = { 'D', ' ', false }, 39747d4b263SKees Cook [ TAINT_OVERRIDDEN_ACPI_TABLE ] = { 'A', ' ', false }, 39847d4b263SKees Cook [ TAINT_WARN ] = { 'W', ' ', false }, 39947d4b263SKees Cook [ TAINT_CRAP ] = { 'C', ' ', true }, 40047d4b263SKees Cook [ TAINT_FIRMWARE_WORKAROUND ] = { 'I', ' ', false }, 40147d4b263SKees Cook [ TAINT_OOT_MODULE ] = { 'O', ' ', true }, 40247d4b263SKees Cook [ TAINT_UNSIGNED_MODULE ] = { 'E', ' ', true }, 40347d4b263SKees Cook [ TAINT_SOFTLOCKUP ] = { 'L', ' ', false }, 40447d4b263SKees Cook [ TAINT_LIVEPATCH ] = { 'K', ' ', true }, 40547d4b263SKees Cook [ TAINT_AUX ] = { 'X', ' ', true }, 406bc4f2f54SKees Cook [ TAINT_RANDSTRUCT ] = { 'T', ' ', true }, 40725ddbb18SAndi Kleen }; 40825ddbb18SAndi Kleen 4091da177e4SLinus Torvalds /** 4101da177e4SLinus Torvalds * print_tainted - return a string to represent the kernel taint state. 4111da177e4SLinus Torvalds * 41257043247SMauro Carvalho Chehab * For individual taint flag meanings, see Documentation/admin-guide/sysctl/kernel.rst 4131da177e4SLinus Torvalds * 4149c4560e5SKees Cook * The string is overwritten by the next call to print_tainted(), 4159c4560e5SKees Cook * but is always NULL terminated. 4161da177e4SLinus Torvalds */ 4171da177e4SLinus Torvalds const char *print_tainted(void) 4181da177e4SLinus Torvalds { 4197fd8329bSPetr Mladek static char buf[TAINT_FLAGS_COUNT + sizeof("Tainted: ")]; 42025ddbb18SAndi Kleen 42147d4b263SKees Cook BUILD_BUG_ON(ARRAY_SIZE(taint_flags) != TAINT_FLAGS_COUNT); 42247d4b263SKees Cook 42325ddbb18SAndi Kleen if (tainted_mask) { 42425ddbb18SAndi Kleen char *s; 42525ddbb18SAndi Kleen int i; 42625ddbb18SAndi Kleen 42725ddbb18SAndi Kleen s = buf + sprintf(buf, "Tainted: "); 4287fd8329bSPetr Mladek for (i = 0; i < TAINT_FLAGS_COUNT; i++) { 4297fd8329bSPetr Mladek const struct taint_flag *t = &taint_flags[i]; 4307fd8329bSPetr Mladek *s++ = test_bit(i, &tainted_mask) ? 4315eb7c0d0SLarry Finger t->c_true : t->c_false; 4321da177e4SLinus Torvalds } 43325ddbb18SAndi Kleen *s = 0; 43425ddbb18SAndi Kleen } else 4351da177e4SLinus Torvalds snprintf(buf, sizeof(buf), "Not tainted"); 436c95dbf27SIngo Molnar 437c95dbf27SIngo Molnar return buf; 4381da177e4SLinus Torvalds } 4391da177e4SLinus Torvalds 44025ddbb18SAndi Kleen int test_taint(unsigned flag) 44125ddbb18SAndi Kleen { 44225ddbb18SAndi Kleen return test_bit(flag, &tainted_mask); 44325ddbb18SAndi Kleen } 44425ddbb18SAndi Kleen EXPORT_SYMBOL(test_taint); 44525ddbb18SAndi Kleen 44625ddbb18SAndi Kleen unsigned long get_taint(void) 44725ddbb18SAndi Kleen { 44825ddbb18SAndi Kleen return tainted_mask; 44925ddbb18SAndi Kleen } 45025ddbb18SAndi Kleen 451373d4d09SRusty Russell /** 452373d4d09SRusty Russell * add_taint: add a taint flag if not already set. 453373d4d09SRusty Russell * @flag: one of the TAINT_* constants. 454373d4d09SRusty Russell * @lockdep_ok: whether lock debugging is still OK. 455373d4d09SRusty Russell * 456373d4d09SRusty Russell * If something bad has gone wrong, you'll want @lockdebug_ok = false, but for 457373d4d09SRusty Russell * some notewortht-but-not-corrupting cases, it can be set to true. 4589eeba613SFrederic Weisbecker */ 459373d4d09SRusty Russell void add_taint(unsigned flag, enum lockdep_ok lockdep_ok) 460373d4d09SRusty Russell { 461373d4d09SRusty Russell if (lockdep_ok == LOCKDEP_NOW_UNRELIABLE && __debug_locks_off()) 462d7c0847fSFabian Frederick pr_warn("Disabling lock debugging due to kernel taint\n"); 4639eeba613SFrederic Weisbecker 46425ddbb18SAndi Kleen set_bit(flag, &tainted_mask); 465db38d5c1SRafael Aquini 466db38d5c1SRafael Aquini if (tainted_mask & panic_on_taint) { 467db38d5c1SRafael Aquini panic_on_taint = 0; 468db38d5c1SRafael Aquini panic("panic_on_taint set ..."); 469db38d5c1SRafael Aquini } 4701da177e4SLinus Torvalds } 4711da177e4SLinus Torvalds EXPORT_SYMBOL(add_taint); 472dd287796SAndrew Morton 473dd287796SAndrew Morton static void spin_msec(int msecs) 474dd287796SAndrew Morton { 475dd287796SAndrew Morton int i; 476dd287796SAndrew Morton 477dd287796SAndrew Morton for (i = 0; i < msecs; i++) { 478dd287796SAndrew Morton touch_nmi_watchdog(); 479dd287796SAndrew Morton mdelay(1); 480dd287796SAndrew Morton } 481dd287796SAndrew Morton } 482dd287796SAndrew Morton 483dd287796SAndrew Morton /* 484dd287796SAndrew Morton * It just happens that oops_enter() and oops_exit() are identically 485dd287796SAndrew Morton * implemented... 486dd287796SAndrew Morton */ 487dd287796SAndrew Morton static void do_oops_enter_exit(void) 488dd287796SAndrew Morton { 489dd287796SAndrew Morton unsigned long flags; 490dd287796SAndrew Morton static int spin_counter; 491dd287796SAndrew Morton 492dd287796SAndrew Morton if (!pause_on_oops) 493dd287796SAndrew Morton return; 494dd287796SAndrew Morton 495dd287796SAndrew Morton spin_lock_irqsave(&pause_on_oops_lock, flags); 496dd287796SAndrew Morton if (pause_on_oops_flag == 0) { 497dd287796SAndrew Morton /* This CPU may now print the oops message */ 498dd287796SAndrew Morton pause_on_oops_flag = 1; 499dd287796SAndrew Morton } else { 500dd287796SAndrew Morton /* We need to stall this CPU */ 501dd287796SAndrew Morton if (!spin_counter) { 502dd287796SAndrew Morton /* This CPU gets to do the counting */ 503dd287796SAndrew Morton spin_counter = pause_on_oops; 504dd287796SAndrew Morton do { 505dd287796SAndrew Morton spin_unlock(&pause_on_oops_lock); 506dd287796SAndrew Morton spin_msec(MSEC_PER_SEC); 507dd287796SAndrew Morton spin_lock(&pause_on_oops_lock); 508dd287796SAndrew Morton } while (--spin_counter); 509dd287796SAndrew Morton pause_on_oops_flag = 0; 510dd287796SAndrew Morton } else { 511dd287796SAndrew Morton /* This CPU waits for a different one */ 512dd287796SAndrew Morton while (spin_counter) { 513dd287796SAndrew Morton spin_unlock(&pause_on_oops_lock); 514dd287796SAndrew Morton spin_msec(1); 515dd287796SAndrew Morton spin_lock(&pause_on_oops_lock); 516dd287796SAndrew Morton } 517dd287796SAndrew Morton } 518dd287796SAndrew Morton } 519dd287796SAndrew Morton spin_unlock_irqrestore(&pause_on_oops_lock, flags); 520dd287796SAndrew Morton } 521dd287796SAndrew Morton 522dd287796SAndrew Morton /* 523c95dbf27SIngo Molnar * Return true if the calling CPU is allowed to print oops-related info. 524c95dbf27SIngo Molnar * This is a bit racy.. 525dd287796SAndrew Morton */ 52679076e12STiezhu Yang bool oops_may_print(void) 527dd287796SAndrew Morton { 528dd287796SAndrew Morton return pause_on_oops_flag == 0; 529dd287796SAndrew Morton } 530dd287796SAndrew Morton 531dd287796SAndrew Morton /* 532dd287796SAndrew Morton * Called when the architecture enters its oops handler, before it prints 533c95dbf27SIngo Molnar * anything. If this is the first CPU to oops, and it's oopsing the first 534c95dbf27SIngo Molnar * time then let it proceed. 535dd287796SAndrew Morton * 536c95dbf27SIngo Molnar * This is all enabled by the pause_on_oops kernel boot option. We do all 537c95dbf27SIngo Molnar * this to ensure that oopses don't scroll off the screen. It has the 538c95dbf27SIngo Molnar * side-effect of preventing later-oopsing CPUs from mucking up the display, 539c95dbf27SIngo Molnar * too. 540dd287796SAndrew Morton * 541c95dbf27SIngo Molnar * It turns out that the CPU which is allowed to print ends up pausing for 542c95dbf27SIngo Molnar * the right duration, whereas all the other CPUs pause for twice as long: 543c95dbf27SIngo Molnar * once in oops_enter(), once in oops_exit(). 544dd287796SAndrew Morton */ 545dd287796SAndrew Morton void oops_enter(void) 546dd287796SAndrew Morton { 547bdff7870SThomas Gleixner tracing_off(); 548c95dbf27SIngo Molnar /* can't trust the integrity of the kernel anymore: */ 549c95dbf27SIngo Molnar debug_locks_off(); 550dd287796SAndrew Morton do_oops_enter_exit(); 55160c958d8SGuilherme G. Piccoli 55260c958d8SGuilherme G. Piccoli if (sysctl_oops_all_cpu_backtrace) 55360c958d8SGuilherme G. Piccoli trigger_all_cpu_backtrace(); 554dd287796SAndrew Morton } 555dd287796SAndrew Morton 55663037f74SYue Hu static void print_oops_end_marker(void) 55771c33911SArjan van de Ven { 558e83a4472SSebastian Andrzej Siewior pr_warn("---[ end trace %016llx ]---\n", 0ULL); 55971c33911SArjan van de Ven } 56071c33911SArjan van de Ven 5612c3b20e9SArjan van de Ven /* 562dd287796SAndrew Morton * Called when the architecture exits its oops handler, after printing 563dd287796SAndrew Morton * everything. 564dd287796SAndrew Morton */ 565dd287796SAndrew Morton void oops_exit(void) 566dd287796SAndrew Morton { 567dd287796SAndrew Morton do_oops_enter_exit(); 56871c33911SArjan van de Ven print_oops_end_marker(); 569456b565cSSimon Kagstrom kmsg_dump(KMSG_DUMP_OOPS); 570dd287796SAndrew Morton } 5713162f751SArjan van de Ven 5722553b67aSJosh Poimboeuf struct warn_args { 5730f6f49a8SLinus Torvalds const char *fmt; 574a8f18b90SArjan van de Ven va_list args; 5750f6f49a8SLinus Torvalds }; 5760f6f49a8SLinus Torvalds 5772553b67aSJosh Poimboeuf void __warn(const char *file, int line, void *caller, unsigned taint, 5782553b67aSJosh Poimboeuf struct pt_regs *regs, struct warn_args *args) 5790f6f49a8SLinus Torvalds { 580de7edd31SSteven Rostedt (Red Hat) disable_trace_on_warning(); 581de7edd31SSteven Rostedt (Red Hat) 5822553b67aSJosh Poimboeuf if (file) 5832553b67aSJosh Poimboeuf pr_warn("WARNING: CPU: %d PID: %d at %s:%d %pS\n", 5842553b67aSJosh Poimboeuf raw_smp_processor_id(), current->pid, file, line, 5852553b67aSJosh Poimboeuf caller); 5862553b67aSJosh Poimboeuf else 5872553b67aSJosh Poimboeuf pr_warn("WARNING: CPU: %d PID: %d at %pS\n", 5882553b67aSJosh Poimboeuf raw_smp_processor_id(), current->pid, caller); 58974853dbaSArjan van de Ven 5900f6f49a8SLinus Torvalds if (args) 5910f6f49a8SLinus Torvalds vprintk(args->fmt, args->args); 592a8f18b90SArjan van de Ven 5933f388f28SAlexey Kardashevskiy print_modules(); 5943f388f28SAlexey Kardashevskiy 5953f388f28SAlexey Kardashevskiy if (regs) 5963f388f28SAlexey Kardashevskiy show_regs(regs); 5973f388f28SAlexey Kardashevskiy 5981a2383e8STiezhu Yang if (panic_on_warn) 5999e3961a0SPrarit Bhargava panic("panic_on_warn set ...\n"); 6009e3961a0SPrarit Bhargava 6012f31ad64SChristophe Leroy if (!regs) 602a8f18b90SArjan van de Ven dump_stack(); 6032553b67aSJosh Poimboeuf 6044c281074SSteven Rostedt (VMware) print_irqtrace_events(current); 6054c281074SSteven Rostedt (VMware) 606a8f18b90SArjan van de Ven print_oops_end_marker(); 60723b36fecSMarco Elver trace_error_report_end(ERROR_DETECTOR_WARN, (unsigned long)caller); 6082553b67aSJosh Poimboeuf 609373d4d09SRusty Russell /* Just a warning, don't kill lockdep. */ 610373d4d09SRusty Russell add_taint(taint, LOCKDEP_STILL_OK); 611a8f18b90SArjan van de Ven } 6120f6f49a8SLinus Torvalds 6132da1ead4SKees Cook #ifndef __WARN_FLAGS 614ee871133SKees Cook void warn_slowpath_fmt(const char *file, int line, unsigned taint, 615ee871133SKees Cook const char *fmt, ...) 616b2be0527SBen Hutchings { 6172553b67aSJosh Poimboeuf struct warn_args args; 618b2be0527SBen Hutchings 619f2f84b05SKees Cook pr_warn(CUT_HERE); 620d38aba49SKees Cook 621d38aba49SKees Cook if (!fmt) { 622f2f84b05SKees Cook __warn(file, line, __builtin_return_address(0), taint, 623f2f84b05SKees Cook NULL, NULL); 624f2f84b05SKees Cook return; 625f2f84b05SKees Cook } 626f2f84b05SKees Cook 627b2be0527SBen Hutchings args.fmt = fmt; 628b2be0527SBen Hutchings va_start(args.args, fmt); 6292553b67aSJosh Poimboeuf __warn(file, line, __builtin_return_address(0), taint, NULL, &args); 630b2be0527SBen Hutchings va_end(args.args); 631b2be0527SBen Hutchings } 632ee871133SKees Cook EXPORT_SYMBOL(warn_slowpath_fmt); 633a7bed27aSKees Cook #else 634a7bed27aSKees Cook void __warn_printk(const char *fmt, ...) 635a7bed27aSKees Cook { 636a7bed27aSKees Cook va_list args; 637a7bed27aSKees Cook 638a7bed27aSKees Cook pr_warn(CUT_HERE); 639a7bed27aSKees Cook 640a7bed27aSKees Cook va_start(args, fmt); 641a7bed27aSKees Cook vprintk(fmt, args); 642a7bed27aSKees Cook va_end(args); 643a7bed27aSKees Cook } 644a7bed27aSKees Cook EXPORT_SYMBOL(__warn_printk); 64579b4cc5eSArjan van de Ven #endif 64679b4cc5eSArjan van de Ven 647b1fca27dSAndi Kleen #ifdef CONFIG_BUG 648b1fca27dSAndi Kleen 649b1fca27dSAndi Kleen /* Support resetting WARN*_ONCE state */ 650b1fca27dSAndi Kleen 651b1fca27dSAndi Kleen static int clear_warn_once_set(void *data, u64 val) 652b1fca27dSAndi Kleen { 653aaf5dcfbSAndi Kleen generic_bug_clear_once(); 654b1fca27dSAndi Kleen memset(__start_once, 0, __end_once - __start_once); 655b1fca27dSAndi Kleen return 0; 656b1fca27dSAndi Kleen } 657b1fca27dSAndi Kleen 6584169680eSYueHaibing DEFINE_DEBUGFS_ATTRIBUTE(clear_warn_once_fops, NULL, clear_warn_once_set, 659b1fca27dSAndi Kleen "%lld\n"); 660b1fca27dSAndi Kleen 661b1fca27dSAndi Kleen static __init int register_warn_debugfs(void) 662b1fca27dSAndi Kleen { 663b1fca27dSAndi Kleen /* Don't care about failure */ 6644169680eSYueHaibing debugfs_create_file_unsafe("clear_warn_once", 0200, NULL, NULL, 6654169680eSYueHaibing &clear_warn_once_fops); 666b1fca27dSAndi Kleen return 0; 667b1fca27dSAndi Kleen } 668b1fca27dSAndi Kleen 669b1fca27dSAndi Kleen device_initcall(register_warn_debugfs); 670b1fca27dSAndi Kleen #endif 671b1fca27dSAndi Kleen 672050e9baaSLinus Torvalds #ifdef CONFIG_STACKPROTECTOR 67354371a43SArjan van de Ven 6743162f751SArjan van de Ven /* 6753162f751SArjan van de Ven * Called when gcc's -fstack-protector feature is used, and 6763162f751SArjan van de Ven * gcc detects corruption of the on-stack canary value 6773162f751SArjan van de Ven */ 6785916d5f9SThomas Gleixner __visible noinstr void __stack_chk_fail(void) 6793162f751SArjan van de Ven { 6805916d5f9SThomas Gleixner instrumentation_begin(); 68195c4fb78SBorislav Petkov panic("stack-protector: Kernel stack is corrupted in: %pB", 682517a92c4SIngo Molnar __builtin_return_address(0)); 6835916d5f9SThomas Gleixner instrumentation_end(); 6843162f751SArjan van de Ven } 6853162f751SArjan van de Ven EXPORT_SYMBOL(__stack_chk_fail); 68654371a43SArjan van de Ven 6873162f751SArjan van de Ven #endif 688f44dd164SRusty Russell 689f44dd164SRusty Russell core_param(panic, panic_timeout, int, 0644); 690d999bd93SFeng Tang core_param(panic_print, panic_print, ulong, 0644); 691f44dd164SRusty Russell core_param(pause_on_oops, pause_on_oops, int, 0644); 6929e3961a0SPrarit Bhargava core_param(panic_on_warn, panic_on_warn, int, 0644); 693b26e27ddSHidehiro Kawai core_param(crash_kexec_post_notifiers, crash_kexec_post_notifiers, bool, 0644); 694f06e5153SMasami Hiramatsu 695d404ab0aSOlaf Hering static int __init oops_setup(char *s) 696d404ab0aSOlaf Hering { 697d404ab0aSOlaf Hering if (!s) 698d404ab0aSOlaf Hering return -EINVAL; 699d404ab0aSOlaf Hering if (!strcmp(s, "panic")) 700d404ab0aSOlaf Hering panic_on_oops = 1; 701d404ab0aSOlaf Hering return 0; 702d404ab0aSOlaf Hering } 703d404ab0aSOlaf Hering early_param("oops", oops_setup); 704db38d5c1SRafael Aquini 705db38d5c1SRafael Aquini static int __init panic_on_taint_setup(char *s) 706db38d5c1SRafael Aquini { 707db38d5c1SRafael Aquini char *taint_str; 708db38d5c1SRafael Aquini 709db38d5c1SRafael Aquini if (!s) 710db38d5c1SRafael Aquini return -EINVAL; 711db38d5c1SRafael Aquini 712db38d5c1SRafael Aquini taint_str = strsep(&s, ","); 713db38d5c1SRafael Aquini if (kstrtoul(taint_str, 16, &panic_on_taint)) 714db38d5c1SRafael Aquini return -EINVAL; 715db38d5c1SRafael Aquini 716db38d5c1SRafael Aquini /* make sure panic_on_taint doesn't hold out-of-range TAINT flags */ 717db38d5c1SRafael Aquini panic_on_taint &= TAINT_FLAGS_MAX; 718db38d5c1SRafael Aquini 719db38d5c1SRafael Aquini if (!panic_on_taint) 720db38d5c1SRafael Aquini return -EINVAL; 721db38d5c1SRafael Aquini 722db38d5c1SRafael Aquini if (s && !strcmp(s, "nousertaint")) 723db38d5c1SRafael Aquini panic_on_taint_nousertaint = true; 724db38d5c1SRafael Aquini 725db38d5c1SRafael Aquini pr_info("panic_on_taint: bitmask=0x%lx nousertaint_mode=%sabled\n", 726db38d5c1SRafael Aquini panic_on_taint, panic_on_taint_nousertaint ? "en" : "dis"); 727db38d5c1SRafael Aquini 728db38d5c1SRafael Aquini return 0; 729db38d5c1SRafael Aquini } 730db38d5c1SRafael Aquini early_param("panic_on_taint", panic_on_taint_setup); 731