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 = 51bc4f2f54SKees Cook IS_ENABLED(CONFIG_GCC_PLUGIN_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 6981c9d43fSFeng Tang unsigned long panic_print; 70d999bd93SFeng Tang 71e041c683SAlan Stern ATOMIC_NOTIFIER_HEAD(panic_notifier_list); 721da177e4SLinus Torvalds 731da177e4SLinus Torvalds EXPORT_SYMBOL(panic_notifier_list); 741da177e4SLinus Torvalds 75c7ff0d9cSTAMUKI Shoichi static long no_blink(int state) 768aeee85aSAnton Blanchard { 77c7ff0d9cSTAMUKI Shoichi return 0; 78c7ff0d9cSTAMUKI Shoichi } 798aeee85aSAnton Blanchard 80c7ff0d9cSTAMUKI Shoichi /* Returns how long it waited in ms */ 81c7ff0d9cSTAMUKI Shoichi long (*panic_blink)(int state); 82c7ff0d9cSTAMUKI Shoichi EXPORT_SYMBOL(panic_blink); 838aeee85aSAnton Blanchard 8493e13a36SMichael Holzheu /* 8593e13a36SMichael Holzheu * Stop ourself in panic -- architecture code may override this 8693e13a36SMichael Holzheu */ 8793e13a36SMichael Holzheu void __weak panic_smp_self_stop(void) 8893e13a36SMichael Holzheu { 8993e13a36SMichael Holzheu while (1) 9093e13a36SMichael Holzheu cpu_relax(); 9193e13a36SMichael Holzheu } 9293e13a36SMichael Holzheu 9358c5661fSHidehiro Kawai /* 9458c5661fSHidehiro Kawai * Stop ourselves in NMI context if another CPU has already panicked. Arch code 9558c5661fSHidehiro Kawai * may override this to prepare for crash dumping, e.g. save regs info. 9658c5661fSHidehiro Kawai */ 9758c5661fSHidehiro Kawai void __weak nmi_panic_self_stop(struct pt_regs *regs) 9858c5661fSHidehiro Kawai { 9958c5661fSHidehiro Kawai panic_smp_self_stop(); 10058c5661fSHidehiro Kawai } 10158c5661fSHidehiro Kawai 1020ee59413SHidehiro Kawai /* 1030ee59413SHidehiro Kawai * Stop other CPUs in panic. Architecture dependent code may override this 1040ee59413SHidehiro Kawai * with more suitable version. For example, if the architecture supports 1050ee59413SHidehiro Kawai * crash dump, it should save registers of each stopped CPU and disable 1060ee59413SHidehiro Kawai * per-CPU features such as virtualization extensions. 1070ee59413SHidehiro Kawai */ 1080ee59413SHidehiro Kawai void __weak crash_smp_send_stop(void) 1090ee59413SHidehiro Kawai { 1100ee59413SHidehiro Kawai static int cpus_stopped; 1110ee59413SHidehiro Kawai 1120ee59413SHidehiro Kawai /* 1130ee59413SHidehiro Kawai * This function can be called twice in panic path, but obviously 1140ee59413SHidehiro Kawai * we execute this only once. 1150ee59413SHidehiro Kawai */ 1160ee59413SHidehiro Kawai if (cpus_stopped) 1170ee59413SHidehiro Kawai return; 1180ee59413SHidehiro Kawai 1190ee59413SHidehiro Kawai /* 1200ee59413SHidehiro Kawai * Note smp_send_stop is the usual smp shutdown function, which 1210ee59413SHidehiro Kawai * unfortunately means it may not be hardened to work in a panic 1220ee59413SHidehiro Kawai * situation. 1230ee59413SHidehiro Kawai */ 1240ee59413SHidehiro Kawai smp_send_stop(); 1250ee59413SHidehiro Kawai cpus_stopped = 1; 1260ee59413SHidehiro Kawai } 1270ee59413SHidehiro Kawai 1281717f209SHidehiro Kawai atomic_t panic_cpu = ATOMIC_INIT(PANIC_CPU_INVALID); 1291717f209SHidehiro Kawai 130ebc41f20SHidehiro Kawai /* 131ebc41f20SHidehiro Kawai * A variant of panic() called from NMI context. We return if we've already 132ebc41f20SHidehiro Kawai * panicked on this CPU. If another CPU already panicked, loop in 133ebc41f20SHidehiro Kawai * nmi_panic_self_stop() which can provide architecture dependent code such 134ebc41f20SHidehiro Kawai * as saving register state for crash dump. 135ebc41f20SHidehiro Kawai */ 136ebc41f20SHidehiro Kawai void nmi_panic(struct pt_regs *regs, const char *msg) 137ebc41f20SHidehiro Kawai { 138ebc41f20SHidehiro Kawai int old_cpu, cpu; 139ebc41f20SHidehiro Kawai 140ebc41f20SHidehiro Kawai cpu = raw_smp_processor_id(); 141ebc41f20SHidehiro Kawai old_cpu = atomic_cmpxchg(&panic_cpu, PANIC_CPU_INVALID, cpu); 142ebc41f20SHidehiro Kawai 143ebc41f20SHidehiro Kawai if (old_cpu == PANIC_CPU_INVALID) 144ebc41f20SHidehiro Kawai panic("%s", msg); 145ebc41f20SHidehiro Kawai else if (old_cpu != cpu) 146ebc41f20SHidehiro Kawai nmi_panic_self_stop(regs); 147ebc41f20SHidehiro Kawai } 148ebc41f20SHidehiro Kawai EXPORT_SYMBOL(nmi_panic); 149ebc41f20SHidehiro Kawai 150d999bd93SFeng Tang static void panic_print_sys_info(void) 151d999bd93SFeng Tang { 152de6da1e8SFeng Tang if (panic_print & PANIC_PRINT_ALL_PRINTK_MSG) 153de6da1e8SFeng Tang console_flush_on_panic(CONSOLE_REPLAY_ALL); 154de6da1e8SFeng Tang 155d999bd93SFeng Tang if (panic_print & PANIC_PRINT_TASK_INFO) 156d999bd93SFeng Tang show_state(); 157d999bd93SFeng Tang 158d999bd93SFeng Tang if (panic_print & PANIC_PRINT_MEM_INFO) 159d999bd93SFeng Tang show_mem(0, NULL); 160d999bd93SFeng Tang 161d999bd93SFeng Tang if (panic_print & PANIC_PRINT_TIMER_INFO) 162d999bd93SFeng Tang sysrq_timer_list_show(); 163d999bd93SFeng Tang 164d999bd93SFeng Tang if (panic_print & PANIC_PRINT_LOCK_INFO) 165d999bd93SFeng Tang debug_show_all_locks(); 166d999bd93SFeng Tang 167d999bd93SFeng Tang if (panic_print & PANIC_PRINT_FTRACE_INFO) 168d999bd93SFeng Tang ftrace_dump(DUMP_ALL); 169d999bd93SFeng Tang } 170d999bd93SFeng Tang 1711da177e4SLinus Torvalds /** 1721da177e4SLinus Torvalds * panic - halt the system 1731da177e4SLinus Torvalds * @fmt: The text string to print 1741da177e4SLinus Torvalds * 1751da177e4SLinus Torvalds * Display a message, then perform cleanups. 1761da177e4SLinus Torvalds * 1771da177e4SLinus Torvalds * This function never returns. 1781da177e4SLinus Torvalds */ 1799402c95fSJoe Perches void panic(const char *fmt, ...) 1801da177e4SLinus Torvalds { 1811da177e4SLinus Torvalds static char buf[1024]; 1821da177e4SLinus Torvalds va_list args; 183b49dec1cSBorislav Petkov long i, i_next = 0, len; 184c7ff0d9cSTAMUKI Shoichi int state = 0; 1851717f209SHidehiro Kawai int old_cpu, this_cpu; 186b26e27ddSHidehiro Kawai bool _crash_kexec_post_notifiers = crash_kexec_post_notifiers; 1871da177e4SLinus Torvalds 188dc009d92SEric W. Biederman /* 189190320c3SVikram Mulukutla * Disable local interrupts. This will prevent panic_smp_self_stop 190190320c3SVikram Mulukutla * from deadlocking the first cpu that invokes the panic, since 191190320c3SVikram Mulukutla * there is nothing to prevent an interrupt handler (that runs 1921717f209SHidehiro Kawai * after setting panic_cpu) from invoking panic() again. 193190320c3SVikram Mulukutla */ 194190320c3SVikram Mulukutla local_irq_disable(); 19520bb759aSWill Deacon preempt_disable_notrace(); 196190320c3SVikram Mulukutla 197190320c3SVikram Mulukutla /* 198c95dbf27SIngo Molnar * It's possible to come here directly from a panic-assertion and 199c95dbf27SIngo Molnar * not have preempt disabled. Some functions called from here want 200dc009d92SEric W. Biederman * preempt to be disabled. No point enabling it later though... 20193e13a36SMichael Holzheu * 20293e13a36SMichael Holzheu * Only one CPU is allowed to execute the panic code from here. For 20393e13a36SMichael Holzheu * multiple parallel invocations of panic, all other CPUs either 20493e13a36SMichael Holzheu * stop themself or will wait until they are stopped by the 1st CPU 20593e13a36SMichael Holzheu * with smp_send_stop(). 2061717f209SHidehiro Kawai * 2071717f209SHidehiro Kawai * `old_cpu == PANIC_CPU_INVALID' means this is the 1st CPU which 2081717f209SHidehiro Kawai * comes here, so go ahead. 2091717f209SHidehiro Kawai * `old_cpu == this_cpu' means we came from nmi_panic() which sets 2101717f209SHidehiro Kawai * panic_cpu to this CPU. In this case, this is also the 1st CPU. 211dc009d92SEric W. Biederman */ 2121717f209SHidehiro Kawai this_cpu = raw_smp_processor_id(); 2131717f209SHidehiro Kawai old_cpu = atomic_cmpxchg(&panic_cpu, PANIC_CPU_INVALID, this_cpu); 2141717f209SHidehiro Kawai 2151717f209SHidehiro Kawai if (old_cpu != PANIC_CPU_INVALID && old_cpu != this_cpu) 21693e13a36SMichael Holzheu panic_smp_self_stop(); 217dc009d92SEric W. Biederman 2185b530fc1SAnton Blanchard console_verbose(); 2191da177e4SLinus Torvalds bust_spinlocks(1); 2201da177e4SLinus Torvalds va_start(args, fmt); 221b49dec1cSBorislav Petkov len = vscnprintf(buf, sizeof(buf), fmt, args); 2221da177e4SLinus Torvalds va_end(args); 223b49dec1cSBorislav Petkov 224b49dec1cSBorislav Petkov if (len && buf[len - 1] == '\n') 225b49dec1cSBorislav Petkov buf[len - 1] = '\0'; 226b49dec1cSBorislav Petkov 227d7c0847fSFabian Frederick pr_emerg("Kernel panic - not syncing: %s\n", buf); 2285cb27301SIngo Molnar #ifdef CONFIG_DEBUG_BUGVERBOSE 2296e6f0a1fSAndi Kleen /* 2306e6f0a1fSAndi Kleen * Avoid nested stack-dumping if a panic occurs during oops processing 2316e6f0a1fSAndi Kleen */ 232026ee1f6SJason Wessel if (!test_taint(TAINT_DIE) && oops_in_progress <= 1) 2335cb27301SIngo Molnar dump_stack(); 2345cb27301SIngo Molnar #endif 2351da177e4SLinus Torvalds 236dc009d92SEric W. Biederman /* 2377d92bda2SDouglas Anderson * If kgdb is enabled, give it a chance to run before we stop all 2387d92bda2SDouglas Anderson * the other CPUs or else we won't be able to debug processes left 2397d92bda2SDouglas Anderson * running on them. 2407d92bda2SDouglas Anderson */ 2417d92bda2SDouglas Anderson kgdb_panic(buf); 2427d92bda2SDouglas Anderson 2437d92bda2SDouglas Anderson /* 244dc009d92SEric W. Biederman * If we have crashed and we have a crash kernel loaded let it handle 245dc009d92SEric W. Biederman * everything else. 246f06e5153SMasami Hiramatsu * If we want to run this after calling panic_notifiers, pass 247f06e5153SMasami Hiramatsu * the "crash_kexec_post_notifiers" option to the kernel. 2487bbee5caSHidehiro Kawai * 2497bbee5caSHidehiro Kawai * Bypass the panic_cpu check and call __crash_kexec directly. 250dc009d92SEric W. Biederman */ 251b26e27ddSHidehiro Kawai if (!_crash_kexec_post_notifiers) { 2527bbee5caSHidehiro Kawai __crash_kexec(NULL); 253dc009d92SEric W. Biederman 254dc009d92SEric W. Biederman /* 255dc009d92SEric W. Biederman * Note smp_send_stop is the usual smp shutdown function, which 2560ee59413SHidehiro Kawai * unfortunately means it may not be hardened to work in a 2570ee59413SHidehiro Kawai * panic situation. 258dc009d92SEric W. Biederman */ 2591da177e4SLinus Torvalds smp_send_stop(); 2600ee59413SHidehiro Kawai } else { 2610ee59413SHidehiro Kawai /* 2620ee59413SHidehiro Kawai * If we want to do crash dump after notifier calls and 2630ee59413SHidehiro Kawai * kmsg_dump, we will need architecture dependent extra 2640ee59413SHidehiro Kawai * works in addition to stopping other CPUs. 2650ee59413SHidehiro Kawai */ 2660ee59413SHidehiro Kawai crash_smp_send_stop(); 2670ee59413SHidehiro Kawai } 2681da177e4SLinus Torvalds 2696723734cSKees Cook /* 2706723734cSKees Cook * Run any panic handlers, including those that might need to 2716723734cSKees Cook * add information to the kmsg dump output. 2726723734cSKees Cook */ 273e041c683SAlan Stern atomic_notifier_call_chain(&panic_notifier_list, 0, buf); 2741da177e4SLinus Torvalds 2756723734cSKees Cook kmsg_dump(KMSG_DUMP_PANIC); 2766723734cSKees Cook 277f06e5153SMasami Hiramatsu /* 278f06e5153SMasami Hiramatsu * If you doubt kdump always works fine in any situation, 279f06e5153SMasami Hiramatsu * "crash_kexec_post_notifiers" offers you a chance to run 280f06e5153SMasami Hiramatsu * panic_notifiers and dumping kmsg before kdump. 281f06e5153SMasami Hiramatsu * Note: since some panic_notifiers can make crashed kernel 282f06e5153SMasami Hiramatsu * more unstable, it can increase risks of the kdump failure too. 2837bbee5caSHidehiro Kawai * 2847bbee5caSHidehiro Kawai * Bypass the panic_cpu check and call __crash_kexec directly. 285f06e5153SMasami Hiramatsu */ 286b26e27ddSHidehiro Kawai if (_crash_kexec_post_notifiers) 2877bbee5caSHidehiro Kawai __crash_kexec(NULL); 288f06e5153SMasami Hiramatsu 289c7c3f05eSSergey Senozhatsky #ifdef CONFIG_VT 290c7c3f05eSSergey Senozhatsky unblank_screen(); 291c7c3f05eSSergey Senozhatsky #endif 292c7c3f05eSSergey Senozhatsky console_unblank(); 293d014e889SAaro Koskinen 29408d78658SVitaly Kuznetsov /* 29508d78658SVitaly Kuznetsov * We may have ended up stopping the CPU holding the lock (in 29608d78658SVitaly Kuznetsov * smp_send_stop()) while still having some valuable data in the console 29708d78658SVitaly Kuznetsov * buffer. Try to acquire the lock then release it regardless of the 2987625b3a0SVitaly Kuznetsov * result. The release will also print the buffers out. Locks debug 2997625b3a0SVitaly Kuznetsov * should be disabled to avoid reporting bad unlock balance when 3007625b3a0SVitaly Kuznetsov * panic() is not being callled from OOPS. 30108d78658SVitaly Kuznetsov */ 3027625b3a0SVitaly Kuznetsov debug_locks_off(); 303de6da1e8SFeng Tang console_flush_on_panic(CONSOLE_FLUSH_PENDING); 30408d78658SVitaly Kuznetsov 305d999bd93SFeng Tang panic_print_sys_info(); 306d999bd93SFeng Tang 307c7ff0d9cSTAMUKI Shoichi if (!panic_blink) 308c7ff0d9cSTAMUKI Shoichi panic_blink = no_blink; 309c7ff0d9cSTAMUKI Shoichi 310dc009d92SEric W. Biederman if (panic_timeout > 0) { 3111da177e4SLinus Torvalds /* 3121da177e4SLinus Torvalds * Delay timeout seconds before rebooting the machine. 313c95dbf27SIngo Molnar * We can't use the "normal" timers since we just panicked. 3141da177e4SLinus Torvalds */ 315ff7a28a0SJiri Slaby pr_emerg("Rebooting in %d seconds..\n", panic_timeout); 316c95dbf27SIngo Molnar 317c7ff0d9cSTAMUKI Shoichi for (i = 0; i < panic_timeout * 1000; i += PANIC_TIMER_STEP) { 3181da177e4SLinus Torvalds touch_nmi_watchdog(); 319c7ff0d9cSTAMUKI Shoichi if (i >= i_next) { 320c7ff0d9cSTAMUKI Shoichi i += panic_blink(state ^= 1); 321c7ff0d9cSTAMUKI Shoichi i_next = i + 3600 / PANIC_BLINK_SPD; 322c7ff0d9cSTAMUKI Shoichi } 323c7ff0d9cSTAMUKI Shoichi mdelay(PANIC_TIMER_STEP); 3241da177e4SLinus Torvalds } 3254302fbc8SHugh Dickins } 3264302fbc8SHugh Dickins if (panic_timeout != 0) { 327c95dbf27SIngo Molnar /* 328c95dbf27SIngo Molnar * This will not be a clean reboot, with everything 3292f048ea8SEric W. Biederman * shutting down. But if there is a chance of 3302f048ea8SEric W. Biederman * rebooting the system it will be rebooted. 3311da177e4SLinus Torvalds */ 332b287a25aSAaro Koskinen if (panic_reboot_mode != REBOOT_UNDEFINED) 333b287a25aSAaro Koskinen reboot_mode = panic_reboot_mode; 3342f048ea8SEric W. Biederman emergency_restart(); 3351da177e4SLinus Torvalds } 3361da177e4SLinus Torvalds #ifdef __sparc__ 3371da177e4SLinus Torvalds { 3381da177e4SLinus Torvalds extern int stop_a_enabled; 339a271c241STom 'spot' Callaway /* Make sure the user can actually press Stop-A (L1-A) */ 3401da177e4SLinus Torvalds stop_a_enabled = 1; 3417db60d05SVijay Kumar pr_emerg("Press Stop-A (L1-A) from sun keyboard or send break\n" 3427db60d05SVijay Kumar "twice on console to return to the boot prom\n"); 3431da177e4SLinus Torvalds } 3441da177e4SLinus Torvalds #endif 345347a8dc3SMartin Schwidefsky #if defined(CONFIG_S390) 34698587c2dSMartin Schwidefsky disabled_wait(); 3471da177e4SLinus Torvalds #endif 3485ad75105SBorislav Petkov pr_emerg("---[ end Kernel panic - not syncing: %s ]---\n", buf); 349c39ea0b9SFeng Tang 350c39ea0b9SFeng Tang /* Do not scroll important messages printed above */ 351c39ea0b9SFeng Tang suppress_printk = 1; 3521da177e4SLinus Torvalds local_irq_enable(); 353c7ff0d9cSTAMUKI Shoichi for (i = 0; ; i += PANIC_TIMER_STEP) { 354c22db941SJan Beulich touch_softlockup_watchdog(); 355c7ff0d9cSTAMUKI Shoichi if (i >= i_next) { 356c7ff0d9cSTAMUKI Shoichi i += panic_blink(state ^= 1); 357c7ff0d9cSTAMUKI Shoichi i_next = i + 3600 / PANIC_BLINK_SPD; 358c7ff0d9cSTAMUKI Shoichi } 359c7ff0d9cSTAMUKI Shoichi mdelay(PANIC_TIMER_STEP); 3601da177e4SLinus Torvalds } 3611da177e4SLinus Torvalds } 3621da177e4SLinus Torvalds 3631da177e4SLinus Torvalds EXPORT_SYMBOL(panic); 3641da177e4SLinus Torvalds 3657fd8329bSPetr Mladek /* 3667fd8329bSPetr Mladek * TAINT_FORCED_RMMOD could be a per-module flag but the module 3677fd8329bSPetr Mladek * is being removed anyway. 3687fd8329bSPetr Mladek */ 3697fd8329bSPetr Mladek const struct taint_flag taint_flags[TAINT_FLAGS_COUNT] = { 37047d4b263SKees Cook [ TAINT_PROPRIETARY_MODULE ] = { 'P', 'G', true }, 37147d4b263SKees Cook [ TAINT_FORCED_MODULE ] = { 'F', ' ', true }, 37247d4b263SKees Cook [ TAINT_CPU_OUT_OF_SPEC ] = { 'S', ' ', false }, 37347d4b263SKees Cook [ TAINT_FORCED_RMMOD ] = { 'R', ' ', false }, 37447d4b263SKees Cook [ TAINT_MACHINE_CHECK ] = { 'M', ' ', false }, 37547d4b263SKees Cook [ TAINT_BAD_PAGE ] = { 'B', ' ', false }, 37647d4b263SKees Cook [ TAINT_USER ] = { 'U', ' ', false }, 37747d4b263SKees Cook [ TAINT_DIE ] = { 'D', ' ', false }, 37847d4b263SKees Cook [ TAINT_OVERRIDDEN_ACPI_TABLE ] = { 'A', ' ', false }, 37947d4b263SKees Cook [ TAINT_WARN ] = { 'W', ' ', false }, 38047d4b263SKees Cook [ TAINT_CRAP ] = { 'C', ' ', true }, 38147d4b263SKees Cook [ TAINT_FIRMWARE_WORKAROUND ] = { 'I', ' ', false }, 38247d4b263SKees Cook [ TAINT_OOT_MODULE ] = { 'O', ' ', true }, 38347d4b263SKees Cook [ TAINT_UNSIGNED_MODULE ] = { 'E', ' ', true }, 38447d4b263SKees Cook [ TAINT_SOFTLOCKUP ] = { 'L', ' ', false }, 38547d4b263SKees Cook [ TAINT_LIVEPATCH ] = { 'K', ' ', true }, 38647d4b263SKees Cook [ TAINT_AUX ] = { 'X', ' ', true }, 387bc4f2f54SKees Cook [ TAINT_RANDSTRUCT ] = { 'T', ' ', true }, 38825ddbb18SAndi Kleen }; 38925ddbb18SAndi Kleen 3901da177e4SLinus Torvalds /** 3911da177e4SLinus Torvalds * print_tainted - return a string to represent the kernel taint state. 3921da177e4SLinus Torvalds * 39357043247SMauro Carvalho Chehab * For individual taint flag meanings, see Documentation/admin-guide/sysctl/kernel.rst 3941da177e4SLinus Torvalds * 3959c4560e5SKees Cook * The string is overwritten by the next call to print_tainted(), 3969c4560e5SKees Cook * but is always NULL terminated. 3971da177e4SLinus Torvalds */ 3981da177e4SLinus Torvalds const char *print_tainted(void) 3991da177e4SLinus Torvalds { 4007fd8329bSPetr Mladek static char buf[TAINT_FLAGS_COUNT + sizeof("Tainted: ")]; 40125ddbb18SAndi Kleen 40247d4b263SKees Cook BUILD_BUG_ON(ARRAY_SIZE(taint_flags) != TAINT_FLAGS_COUNT); 40347d4b263SKees Cook 40425ddbb18SAndi Kleen if (tainted_mask) { 40525ddbb18SAndi Kleen char *s; 40625ddbb18SAndi Kleen int i; 40725ddbb18SAndi Kleen 40825ddbb18SAndi Kleen s = buf + sprintf(buf, "Tainted: "); 4097fd8329bSPetr Mladek for (i = 0; i < TAINT_FLAGS_COUNT; i++) { 4107fd8329bSPetr Mladek const struct taint_flag *t = &taint_flags[i]; 4117fd8329bSPetr Mladek *s++ = test_bit(i, &tainted_mask) ? 4125eb7c0d0SLarry Finger t->c_true : t->c_false; 4131da177e4SLinus Torvalds } 41425ddbb18SAndi Kleen *s = 0; 41525ddbb18SAndi Kleen } else 4161da177e4SLinus Torvalds snprintf(buf, sizeof(buf), "Not tainted"); 417c95dbf27SIngo Molnar 418c95dbf27SIngo Molnar return buf; 4191da177e4SLinus Torvalds } 4201da177e4SLinus Torvalds 42125ddbb18SAndi Kleen int test_taint(unsigned flag) 42225ddbb18SAndi Kleen { 42325ddbb18SAndi Kleen return test_bit(flag, &tainted_mask); 42425ddbb18SAndi Kleen } 42525ddbb18SAndi Kleen EXPORT_SYMBOL(test_taint); 42625ddbb18SAndi Kleen 42725ddbb18SAndi Kleen unsigned long get_taint(void) 42825ddbb18SAndi Kleen { 42925ddbb18SAndi Kleen return tainted_mask; 43025ddbb18SAndi Kleen } 43125ddbb18SAndi Kleen 432373d4d09SRusty Russell /** 433373d4d09SRusty Russell * add_taint: add a taint flag if not already set. 434373d4d09SRusty Russell * @flag: one of the TAINT_* constants. 435373d4d09SRusty Russell * @lockdep_ok: whether lock debugging is still OK. 436373d4d09SRusty Russell * 437373d4d09SRusty Russell * If something bad has gone wrong, you'll want @lockdebug_ok = false, but for 438373d4d09SRusty Russell * some notewortht-but-not-corrupting cases, it can be set to true. 4399eeba613SFrederic Weisbecker */ 440373d4d09SRusty Russell void add_taint(unsigned flag, enum lockdep_ok lockdep_ok) 441373d4d09SRusty Russell { 442373d4d09SRusty Russell if (lockdep_ok == LOCKDEP_NOW_UNRELIABLE && __debug_locks_off()) 443d7c0847fSFabian Frederick pr_warn("Disabling lock debugging due to kernel taint\n"); 4449eeba613SFrederic Weisbecker 44525ddbb18SAndi Kleen set_bit(flag, &tainted_mask); 446db38d5c1SRafael Aquini 447db38d5c1SRafael Aquini if (tainted_mask & panic_on_taint) { 448db38d5c1SRafael Aquini panic_on_taint = 0; 449db38d5c1SRafael Aquini panic("panic_on_taint set ..."); 450db38d5c1SRafael Aquini } 4511da177e4SLinus Torvalds } 4521da177e4SLinus Torvalds EXPORT_SYMBOL(add_taint); 453dd287796SAndrew Morton 454dd287796SAndrew Morton static void spin_msec(int msecs) 455dd287796SAndrew Morton { 456dd287796SAndrew Morton int i; 457dd287796SAndrew Morton 458dd287796SAndrew Morton for (i = 0; i < msecs; i++) { 459dd287796SAndrew Morton touch_nmi_watchdog(); 460dd287796SAndrew Morton mdelay(1); 461dd287796SAndrew Morton } 462dd287796SAndrew Morton } 463dd287796SAndrew Morton 464dd287796SAndrew Morton /* 465dd287796SAndrew Morton * It just happens that oops_enter() and oops_exit() are identically 466dd287796SAndrew Morton * implemented... 467dd287796SAndrew Morton */ 468dd287796SAndrew Morton static void do_oops_enter_exit(void) 469dd287796SAndrew Morton { 470dd287796SAndrew Morton unsigned long flags; 471dd287796SAndrew Morton static int spin_counter; 472dd287796SAndrew Morton 473dd287796SAndrew Morton if (!pause_on_oops) 474dd287796SAndrew Morton return; 475dd287796SAndrew Morton 476dd287796SAndrew Morton spin_lock_irqsave(&pause_on_oops_lock, flags); 477dd287796SAndrew Morton if (pause_on_oops_flag == 0) { 478dd287796SAndrew Morton /* This CPU may now print the oops message */ 479dd287796SAndrew Morton pause_on_oops_flag = 1; 480dd287796SAndrew Morton } else { 481dd287796SAndrew Morton /* We need to stall this CPU */ 482dd287796SAndrew Morton if (!spin_counter) { 483dd287796SAndrew Morton /* This CPU gets to do the counting */ 484dd287796SAndrew Morton spin_counter = pause_on_oops; 485dd287796SAndrew Morton do { 486dd287796SAndrew Morton spin_unlock(&pause_on_oops_lock); 487dd287796SAndrew Morton spin_msec(MSEC_PER_SEC); 488dd287796SAndrew Morton spin_lock(&pause_on_oops_lock); 489dd287796SAndrew Morton } while (--spin_counter); 490dd287796SAndrew Morton pause_on_oops_flag = 0; 491dd287796SAndrew Morton } else { 492dd287796SAndrew Morton /* This CPU waits for a different one */ 493dd287796SAndrew Morton while (spin_counter) { 494dd287796SAndrew Morton spin_unlock(&pause_on_oops_lock); 495dd287796SAndrew Morton spin_msec(1); 496dd287796SAndrew Morton spin_lock(&pause_on_oops_lock); 497dd287796SAndrew Morton } 498dd287796SAndrew Morton } 499dd287796SAndrew Morton } 500dd287796SAndrew Morton spin_unlock_irqrestore(&pause_on_oops_lock, flags); 501dd287796SAndrew Morton } 502dd287796SAndrew Morton 503dd287796SAndrew Morton /* 504c95dbf27SIngo Molnar * Return true if the calling CPU is allowed to print oops-related info. 505c95dbf27SIngo Molnar * This is a bit racy.. 506dd287796SAndrew Morton */ 50779076e12STiezhu Yang bool oops_may_print(void) 508dd287796SAndrew Morton { 509dd287796SAndrew Morton return pause_on_oops_flag == 0; 510dd287796SAndrew Morton } 511dd287796SAndrew Morton 512dd287796SAndrew Morton /* 513dd287796SAndrew Morton * Called when the architecture enters its oops handler, before it prints 514c95dbf27SIngo Molnar * anything. If this is the first CPU to oops, and it's oopsing the first 515c95dbf27SIngo Molnar * time then let it proceed. 516dd287796SAndrew Morton * 517c95dbf27SIngo Molnar * This is all enabled by the pause_on_oops kernel boot option. We do all 518c95dbf27SIngo Molnar * this to ensure that oopses don't scroll off the screen. It has the 519c95dbf27SIngo Molnar * side-effect of preventing later-oopsing CPUs from mucking up the display, 520c95dbf27SIngo Molnar * too. 521dd287796SAndrew Morton * 522c95dbf27SIngo Molnar * It turns out that the CPU which is allowed to print ends up pausing for 523c95dbf27SIngo Molnar * the right duration, whereas all the other CPUs pause for twice as long: 524c95dbf27SIngo Molnar * once in oops_enter(), once in oops_exit(). 525dd287796SAndrew Morton */ 526dd287796SAndrew Morton void oops_enter(void) 527dd287796SAndrew Morton { 528bdff7870SThomas Gleixner tracing_off(); 529c95dbf27SIngo Molnar /* can't trust the integrity of the kernel anymore: */ 530c95dbf27SIngo Molnar debug_locks_off(); 531dd287796SAndrew Morton do_oops_enter_exit(); 53260c958d8SGuilherme G. Piccoli 53360c958d8SGuilherme G. Piccoli if (sysctl_oops_all_cpu_backtrace) 53460c958d8SGuilherme G. Piccoli trigger_all_cpu_backtrace(); 535dd287796SAndrew Morton } 536dd287796SAndrew Morton 53763037f74SYue Hu static void print_oops_end_marker(void) 53871c33911SArjan van de Ven { 539e83a4472SSebastian Andrzej Siewior pr_warn("---[ end trace %016llx ]---\n", 0ULL); 54071c33911SArjan van de Ven } 54171c33911SArjan van de Ven 5422c3b20e9SArjan van de Ven /* 543dd287796SAndrew Morton * Called when the architecture exits its oops handler, after printing 544dd287796SAndrew Morton * everything. 545dd287796SAndrew Morton */ 546dd287796SAndrew Morton void oops_exit(void) 547dd287796SAndrew Morton { 548dd287796SAndrew Morton do_oops_enter_exit(); 54971c33911SArjan van de Ven print_oops_end_marker(); 550456b565cSSimon Kagstrom kmsg_dump(KMSG_DUMP_OOPS); 551dd287796SAndrew Morton } 5523162f751SArjan van de Ven 5532553b67aSJosh Poimboeuf struct warn_args { 5540f6f49a8SLinus Torvalds const char *fmt; 555a8f18b90SArjan van de Ven va_list args; 5560f6f49a8SLinus Torvalds }; 5570f6f49a8SLinus Torvalds 5582553b67aSJosh Poimboeuf void __warn(const char *file, int line, void *caller, unsigned taint, 5592553b67aSJosh Poimboeuf struct pt_regs *regs, struct warn_args *args) 5600f6f49a8SLinus Torvalds { 561de7edd31SSteven Rostedt (Red Hat) disable_trace_on_warning(); 562de7edd31SSteven Rostedt (Red Hat) 563*2bb2b7b5SJohn Ogness printk_prefer_direct_enter(); 564*2bb2b7b5SJohn Ogness 5652553b67aSJosh Poimboeuf if (file) 5662553b67aSJosh Poimboeuf pr_warn("WARNING: CPU: %d PID: %d at %s:%d %pS\n", 5672553b67aSJosh Poimboeuf raw_smp_processor_id(), current->pid, file, line, 5682553b67aSJosh Poimboeuf caller); 5692553b67aSJosh Poimboeuf else 5702553b67aSJosh Poimboeuf pr_warn("WARNING: CPU: %d PID: %d at %pS\n", 5712553b67aSJosh Poimboeuf raw_smp_processor_id(), current->pid, caller); 57274853dbaSArjan van de Ven 5730f6f49a8SLinus Torvalds if (args) 5740f6f49a8SLinus Torvalds vprintk(args->fmt, args->args); 575a8f18b90SArjan van de Ven 5763f388f28SAlexey Kardashevskiy print_modules(); 5773f388f28SAlexey Kardashevskiy 5783f388f28SAlexey Kardashevskiy if (regs) 5793f388f28SAlexey Kardashevskiy show_regs(regs); 5803f388f28SAlexey Kardashevskiy 5819e3961a0SPrarit Bhargava if (panic_on_warn) { 5829e3961a0SPrarit Bhargava /* 5839e3961a0SPrarit Bhargava * This thread may hit another WARN() in the panic path. 5849e3961a0SPrarit Bhargava * Resetting this prevents additional WARN() from panicking the 5859e3961a0SPrarit Bhargava * system on this thread. Other threads are blocked by the 5869e3961a0SPrarit Bhargava * panic_mutex in panic(). 5879e3961a0SPrarit Bhargava */ 5889e3961a0SPrarit Bhargava panic_on_warn = 0; 5899e3961a0SPrarit Bhargava panic("panic_on_warn set ...\n"); 5909e3961a0SPrarit Bhargava } 5919e3961a0SPrarit Bhargava 5922f31ad64SChristophe Leroy if (!regs) 593a8f18b90SArjan van de Ven dump_stack(); 5942553b67aSJosh Poimboeuf 5954c281074SSteven Rostedt (VMware) print_irqtrace_events(current); 5964c281074SSteven Rostedt (VMware) 597a8f18b90SArjan van de Ven print_oops_end_marker(); 59823b36fecSMarco Elver trace_error_report_end(ERROR_DETECTOR_WARN, (unsigned long)caller); 5992553b67aSJosh Poimboeuf 600373d4d09SRusty Russell /* Just a warning, don't kill lockdep. */ 601373d4d09SRusty Russell add_taint(taint, LOCKDEP_STILL_OK); 602*2bb2b7b5SJohn Ogness 603*2bb2b7b5SJohn Ogness printk_prefer_direct_exit(); 604a8f18b90SArjan van de Ven } 6050f6f49a8SLinus Torvalds 6062da1ead4SKees Cook #ifndef __WARN_FLAGS 607ee871133SKees Cook void warn_slowpath_fmt(const char *file, int line, unsigned taint, 608ee871133SKees Cook const char *fmt, ...) 609b2be0527SBen Hutchings { 6102553b67aSJosh Poimboeuf struct warn_args args; 611b2be0527SBen Hutchings 612f2f84b05SKees Cook pr_warn(CUT_HERE); 613d38aba49SKees Cook 614d38aba49SKees Cook if (!fmt) { 615f2f84b05SKees Cook __warn(file, line, __builtin_return_address(0), taint, 616f2f84b05SKees Cook NULL, NULL); 617f2f84b05SKees Cook return; 618f2f84b05SKees Cook } 619f2f84b05SKees Cook 620b2be0527SBen Hutchings args.fmt = fmt; 621b2be0527SBen Hutchings va_start(args.args, fmt); 6222553b67aSJosh Poimboeuf __warn(file, line, __builtin_return_address(0), taint, NULL, &args); 623b2be0527SBen Hutchings va_end(args.args); 624b2be0527SBen Hutchings } 625ee871133SKees Cook EXPORT_SYMBOL(warn_slowpath_fmt); 626a7bed27aSKees Cook #else 627a7bed27aSKees Cook void __warn_printk(const char *fmt, ...) 628a7bed27aSKees Cook { 629a7bed27aSKees Cook va_list args; 630a7bed27aSKees Cook 631a7bed27aSKees Cook pr_warn(CUT_HERE); 632a7bed27aSKees Cook 633a7bed27aSKees Cook va_start(args, fmt); 634a7bed27aSKees Cook vprintk(fmt, args); 635a7bed27aSKees Cook va_end(args); 636a7bed27aSKees Cook } 637a7bed27aSKees Cook EXPORT_SYMBOL(__warn_printk); 63879b4cc5eSArjan van de Ven #endif 63979b4cc5eSArjan van de Ven 640b1fca27dSAndi Kleen #ifdef CONFIG_BUG 641b1fca27dSAndi Kleen 642b1fca27dSAndi Kleen /* Support resetting WARN*_ONCE state */ 643b1fca27dSAndi Kleen 644b1fca27dSAndi Kleen static int clear_warn_once_set(void *data, u64 val) 645b1fca27dSAndi Kleen { 646aaf5dcfbSAndi Kleen generic_bug_clear_once(); 647b1fca27dSAndi Kleen memset(__start_once, 0, __end_once - __start_once); 648b1fca27dSAndi Kleen return 0; 649b1fca27dSAndi Kleen } 650b1fca27dSAndi Kleen 6514169680eSYueHaibing DEFINE_DEBUGFS_ATTRIBUTE(clear_warn_once_fops, NULL, clear_warn_once_set, 652b1fca27dSAndi Kleen "%lld\n"); 653b1fca27dSAndi Kleen 654b1fca27dSAndi Kleen static __init int register_warn_debugfs(void) 655b1fca27dSAndi Kleen { 656b1fca27dSAndi Kleen /* Don't care about failure */ 6574169680eSYueHaibing debugfs_create_file_unsafe("clear_warn_once", 0200, NULL, NULL, 6584169680eSYueHaibing &clear_warn_once_fops); 659b1fca27dSAndi Kleen return 0; 660b1fca27dSAndi Kleen } 661b1fca27dSAndi Kleen 662b1fca27dSAndi Kleen device_initcall(register_warn_debugfs); 663b1fca27dSAndi Kleen #endif 664b1fca27dSAndi Kleen 665050e9baaSLinus Torvalds #ifdef CONFIG_STACKPROTECTOR 66654371a43SArjan van de Ven 6673162f751SArjan van de Ven /* 6683162f751SArjan van de Ven * Called when gcc's -fstack-protector feature is used, and 6693162f751SArjan van de Ven * gcc detects corruption of the on-stack canary value 6703162f751SArjan van de Ven */ 6715916d5f9SThomas Gleixner __visible noinstr void __stack_chk_fail(void) 6723162f751SArjan van de Ven { 6735916d5f9SThomas Gleixner instrumentation_begin(); 67495c4fb78SBorislav Petkov panic("stack-protector: Kernel stack is corrupted in: %pB", 675517a92c4SIngo Molnar __builtin_return_address(0)); 6765916d5f9SThomas Gleixner instrumentation_end(); 6773162f751SArjan van de Ven } 6783162f751SArjan van de Ven EXPORT_SYMBOL(__stack_chk_fail); 67954371a43SArjan van de Ven 6803162f751SArjan van de Ven #endif 681f44dd164SRusty Russell 682f44dd164SRusty Russell core_param(panic, panic_timeout, int, 0644); 683d999bd93SFeng Tang core_param(panic_print, panic_print, ulong, 0644); 684f44dd164SRusty Russell core_param(pause_on_oops, pause_on_oops, int, 0644); 6859e3961a0SPrarit Bhargava core_param(panic_on_warn, panic_on_warn, int, 0644); 686b26e27ddSHidehiro Kawai core_param(crash_kexec_post_notifiers, crash_kexec_post_notifiers, bool, 0644); 687f06e5153SMasami Hiramatsu 688d404ab0aSOlaf Hering static int __init oops_setup(char *s) 689d404ab0aSOlaf Hering { 690d404ab0aSOlaf Hering if (!s) 691d404ab0aSOlaf Hering return -EINVAL; 692d404ab0aSOlaf Hering if (!strcmp(s, "panic")) 693d404ab0aSOlaf Hering panic_on_oops = 1; 694d404ab0aSOlaf Hering return 0; 695d404ab0aSOlaf Hering } 696d404ab0aSOlaf Hering early_param("oops", oops_setup); 697db38d5c1SRafael Aquini 698db38d5c1SRafael Aquini static int __init panic_on_taint_setup(char *s) 699db38d5c1SRafael Aquini { 700db38d5c1SRafael Aquini char *taint_str; 701db38d5c1SRafael Aquini 702db38d5c1SRafael Aquini if (!s) 703db38d5c1SRafael Aquini return -EINVAL; 704db38d5c1SRafael Aquini 705db38d5c1SRafael Aquini taint_str = strsep(&s, ","); 706db38d5c1SRafael Aquini if (kstrtoul(taint_str, 16, &panic_on_taint)) 707db38d5c1SRafael Aquini return -EINVAL; 708db38d5c1SRafael Aquini 709db38d5c1SRafael Aquini /* make sure panic_on_taint doesn't hold out-of-range TAINT flags */ 710db38d5c1SRafael Aquini panic_on_taint &= TAINT_FLAGS_MAX; 711db38d5c1SRafael Aquini 712db38d5c1SRafael Aquini if (!panic_on_taint) 713db38d5c1SRafael Aquini return -EINVAL; 714db38d5c1SRafael Aquini 715db38d5c1SRafael Aquini if (s && !strcmp(s, "nousertaint")) 716db38d5c1SRafael Aquini panic_on_taint_nousertaint = true; 717db38d5c1SRafael Aquini 718db38d5c1SRafael Aquini pr_info("panic_on_taint: bitmask=0x%lx nousertaint_mode=%sabled\n", 719db38d5c1SRafael Aquini panic_on_taint, panic_on_taint_nousertaint ? "en" : "dis"); 720db38d5c1SRafael Aquini 721db38d5c1SRafael Aquini return 0; 722db38d5c1SRafael Aquini } 723db38d5c1SRafael Aquini early_param("panic_on_taint", panic_on_taint_setup); 724