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> 26c95dbf27SIngo Molnar #include <linux/sched.h> 27c95dbf27SIngo Molnar #include <linux/sysrq.h> 28c95dbf27SIngo Molnar #include <linux/init.h> 29c95dbf27SIngo Molnar #include <linux/nmi.h> 3008d78658SVitaly Kuznetsov #include <linux/console.h> 312553b67aSJosh Poimboeuf #include <linux/bug.h> 327a46ec0eSKees Cook #include <linux/ratelimit.h> 33b1fca27dSAndi Kleen #include <linux/debugfs.h> 34b1fca27dSAndi Kleen #include <asm/sections.h> 351da177e4SLinus Torvalds 36c7ff0d9cSTAMUKI Shoichi #define PANIC_TIMER_STEP 100 37c7ff0d9cSTAMUKI Shoichi #define PANIC_BLINK_SPD 18 38c7ff0d9cSTAMUKI Shoichi 392a01bb38SKyle McMartin int panic_on_oops = CONFIG_PANIC_ON_OOPS_VALUE; 40bc4f2f54SKees Cook static unsigned long tainted_mask = 41bc4f2f54SKees Cook IS_ENABLED(CONFIG_GCC_PLUGIN_RANDSTRUCT) ? (1 << TAINT_RANDSTRUCT) : 0; 42dd287796SAndrew Morton static int pause_on_oops; 43dd287796SAndrew Morton static int pause_on_oops_flag; 44dd287796SAndrew Morton static DEFINE_SPINLOCK(pause_on_oops_lock); 455375b708SHATAYAMA Daisuke bool crash_kexec_post_notifiers; 469e3961a0SPrarit Bhargava int panic_on_warn __read_mostly; 471da177e4SLinus Torvalds 485800dc3cSJason Baron int panic_timeout = CONFIG_PANIC_TIMEOUT; 4981e88fdcSHuang Ying EXPORT_SYMBOL_GPL(panic_timeout); 501da177e4SLinus Torvalds 51d999bd93SFeng Tang #define PANIC_PRINT_TASK_INFO 0x00000001 52d999bd93SFeng Tang #define PANIC_PRINT_MEM_INFO 0x00000002 53d999bd93SFeng Tang #define PANIC_PRINT_TIMER_INFO 0x00000004 54d999bd93SFeng Tang #define PANIC_PRINT_LOCK_INFO 0x00000008 55d999bd93SFeng Tang #define PANIC_PRINT_FTRACE_INFO 0x00000010 56de6da1e8SFeng Tang #define PANIC_PRINT_ALL_PRINTK_MSG 0x00000020 5781c9d43fSFeng Tang unsigned long panic_print; 58d999bd93SFeng Tang 59e041c683SAlan Stern ATOMIC_NOTIFIER_HEAD(panic_notifier_list); 601da177e4SLinus Torvalds 611da177e4SLinus Torvalds EXPORT_SYMBOL(panic_notifier_list); 621da177e4SLinus Torvalds 63c7ff0d9cSTAMUKI Shoichi static long no_blink(int state) 648aeee85aSAnton Blanchard { 65c7ff0d9cSTAMUKI Shoichi return 0; 66c7ff0d9cSTAMUKI Shoichi } 678aeee85aSAnton Blanchard 68c7ff0d9cSTAMUKI Shoichi /* Returns how long it waited in ms */ 69c7ff0d9cSTAMUKI Shoichi long (*panic_blink)(int state); 70c7ff0d9cSTAMUKI Shoichi EXPORT_SYMBOL(panic_blink); 718aeee85aSAnton Blanchard 7293e13a36SMichael Holzheu /* 7393e13a36SMichael Holzheu * Stop ourself in panic -- architecture code may override this 7493e13a36SMichael Holzheu */ 7593e13a36SMichael Holzheu void __weak panic_smp_self_stop(void) 7693e13a36SMichael Holzheu { 7793e13a36SMichael Holzheu while (1) 7893e13a36SMichael Holzheu cpu_relax(); 7993e13a36SMichael Holzheu } 8093e13a36SMichael Holzheu 8158c5661fSHidehiro Kawai /* 8258c5661fSHidehiro Kawai * Stop ourselves in NMI context if another CPU has already panicked. Arch code 8358c5661fSHidehiro Kawai * may override this to prepare for crash dumping, e.g. save regs info. 8458c5661fSHidehiro Kawai */ 8558c5661fSHidehiro Kawai void __weak nmi_panic_self_stop(struct pt_regs *regs) 8658c5661fSHidehiro Kawai { 8758c5661fSHidehiro Kawai panic_smp_self_stop(); 8858c5661fSHidehiro Kawai } 8958c5661fSHidehiro Kawai 900ee59413SHidehiro Kawai /* 910ee59413SHidehiro Kawai * Stop other CPUs in panic. Architecture dependent code may override this 920ee59413SHidehiro Kawai * with more suitable version. For example, if the architecture supports 930ee59413SHidehiro Kawai * crash dump, it should save registers of each stopped CPU and disable 940ee59413SHidehiro Kawai * per-CPU features such as virtualization extensions. 950ee59413SHidehiro Kawai */ 960ee59413SHidehiro Kawai void __weak crash_smp_send_stop(void) 970ee59413SHidehiro Kawai { 980ee59413SHidehiro Kawai static int cpus_stopped; 990ee59413SHidehiro Kawai 1000ee59413SHidehiro Kawai /* 1010ee59413SHidehiro Kawai * This function can be called twice in panic path, but obviously 1020ee59413SHidehiro Kawai * we execute this only once. 1030ee59413SHidehiro Kawai */ 1040ee59413SHidehiro Kawai if (cpus_stopped) 1050ee59413SHidehiro Kawai return; 1060ee59413SHidehiro Kawai 1070ee59413SHidehiro Kawai /* 1080ee59413SHidehiro Kawai * Note smp_send_stop is the usual smp shutdown function, which 1090ee59413SHidehiro Kawai * unfortunately means it may not be hardened to work in a panic 1100ee59413SHidehiro Kawai * situation. 1110ee59413SHidehiro Kawai */ 1120ee59413SHidehiro Kawai smp_send_stop(); 1130ee59413SHidehiro Kawai cpus_stopped = 1; 1140ee59413SHidehiro Kawai } 1150ee59413SHidehiro Kawai 1161717f209SHidehiro Kawai atomic_t panic_cpu = ATOMIC_INIT(PANIC_CPU_INVALID); 1171717f209SHidehiro Kawai 118ebc41f20SHidehiro Kawai /* 119ebc41f20SHidehiro Kawai * A variant of panic() called from NMI context. We return if we've already 120ebc41f20SHidehiro Kawai * panicked on this CPU. If another CPU already panicked, loop in 121ebc41f20SHidehiro Kawai * nmi_panic_self_stop() which can provide architecture dependent code such 122ebc41f20SHidehiro Kawai * as saving register state for crash dump. 123ebc41f20SHidehiro Kawai */ 124ebc41f20SHidehiro Kawai void nmi_panic(struct pt_regs *regs, const char *msg) 125ebc41f20SHidehiro Kawai { 126ebc41f20SHidehiro Kawai int old_cpu, cpu; 127ebc41f20SHidehiro Kawai 128ebc41f20SHidehiro Kawai cpu = raw_smp_processor_id(); 129ebc41f20SHidehiro Kawai old_cpu = atomic_cmpxchg(&panic_cpu, PANIC_CPU_INVALID, cpu); 130ebc41f20SHidehiro Kawai 131ebc41f20SHidehiro Kawai if (old_cpu == PANIC_CPU_INVALID) 132ebc41f20SHidehiro Kawai panic("%s", msg); 133ebc41f20SHidehiro Kawai else if (old_cpu != cpu) 134ebc41f20SHidehiro Kawai nmi_panic_self_stop(regs); 135ebc41f20SHidehiro Kawai } 136ebc41f20SHidehiro Kawai EXPORT_SYMBOL(nmi_panic); 137ebc41f20SHidehiro Kawai 138d999bd93SFeng Tang static void panic_print_sys_info(void) 139d999bd93SFeng Tang { 140de6da1e8SFeng Tang if (panic_print & PANIC_PRINT_ALL_PRINTK_MSG) 141de6da1e8SFeng Tang console_flush_on_panic(CONSOLE_REPLAY_ALL); 142de6da1e8SFeng Tang 143d999bd93SFeng Tang if (panic_print & PANIC_PRINT_TASK_INFO) 144d999bd93SFeng Tang show_state(); 145d999bd93SFeng Tang 146d999bd93SFeng Tang if (panic_print & PANIC_PRINT_MEM_INFO) 147d999bd93SFeng Tang show_mem(0, NULL); 148d999bd93SFeng Tang 149d999bd93SFeng Tang if (panic_print & PANIC_PRINT_TIMER_INFO) 150d999bd93SFeng Tang sysrq_timer_list_show(); 151d999bd93SFeng Tang 152d999bd93SFeng Tang if (panic_print & PANIC_PRINT_LOCK_INFO) 153d999bd93SFeng Tang debug_show_all_locks(); 154d999bd93SFeng Tang 155d999bd93SFeng Tang if (panic_print & PANIC_PRINT_FTRACE_INFO) 156d999bd93SFeng Tang ftrace_dump(DUMP_ALL); 157d999bd93SFeng Tang } 158d999bd93SFeng Tang 1591da177e4SLinus Torvalds /** 1601da177e4SLinus Torvalds * panic - halt the system 1611da177e4SLinus Torvalds * @fmt: The text string to print 1621da177e4SLinus Torvalds * 1631da177e4SLinus Torvalds * Display a message, then perform cleanups. 1641da177e4SLinus Torvalds * 1651da177e4SLinus Torvalds * This function never returns. 1661da177e4SLinus Torvalds */ 1679402c95fSJoe Perches void panic(const char *fmt, ...) 1681da177e4SLinus Torvalds { 1691da177e4SLinus Torvalds static char buf[1024]; 1701da177e4SLinus Torvalds va_list args; 171b49dec1cSBorislav Petkov long i, i_next = 0, len; 172c7ff0d9cSTAMUKI Shoichi int state = 0; 1731717f209SHidehiro Kawai int old_cpu, this_cpu; 174b26e27ddSHidehiro Kawai bool _crash_kexec_post_notifiers = crash_kexec_post_notifiers; 1751da177e4SLinus Torvalds 176dc009d92SEric W. Biederman /* 177190320c3SVikram Mulukutla * Disable local interrupts. This will prevent panic_smp_self_stop 178190320c3SVikram Mulukutla * from deadlocking the first cpu that invokes the panic, since 179190320c3SVikram Mulukutla * there is nothing to prevent an interrupt handler (that runs 1801717f209SHidehiro Kawai * after setting panic_cpu) from invoking panic() again. 181190320c3SVikram Mulukutla */ 182190320c3SVikram Mulukutla local_irq_disable(); 183190320c3SVikram Mulukutla 184190320c3SVikram Mulukutla /* 185c95dbf27SIngo Molnar * It's possible to come here directly from a panic-assertion and 186c95dbf27SIngo Molnar * not have preempt disabled. Some functions called from here want 187dc009d92SEric W. Biederman * preempt to be disabled. No point enabling it later though... 18893e13a36SMichael Holzheu * 18993e13a36SMichael Holzheu * Only one CPU is allowed to execute the panic code from here. For 19093e13a36SMichael Holzheu * multiple parallel invocations of panic, all other CPUs either 19193e13a36SMichael Holzheu * stop themself or will wait until they are stopped by the 1st CPU 19293e13a36SMichael Holzheu * with smp_send_stop(). 1931717f209SHidehiro Kawai * 1941717f209SHidehiro Kawai * `old_cpu == PANIC_CPU_INVALID' means this is the 1st CPU which 1951717f209SHidehiro Kawai * comes here, so go ahead. 1961717f209SHidehiro Kawai * `old_cpu == this_cpu' means we came from nmi_panic() which sets 1971717f209SHidehiro Kawai * panic_cpu to this CPU. In this case, this is also the 1st CPU. 198dc009d92SEric W. Biederman */ 1991717f209SHidehiro Kawai this_cpu = raw_smp_processor_id(); 2001717f209SHidehiro Kawai old_cpu = atomic_cmpxchg(&panic_cpu, PANIC_CPU_INVALID, this_cpu); 2011717f209SHidehiro Kawai 2021717f209SHidehiro Kawai if (old_cpu != PANIC_CPU_INVALID && old_cpu != this_cpu) 20393e13a36SMichael Holzheu panic_smp_self_stop(); 204dc009d92SEric W. Biederman 2055b530fc1SAnton Blanchard console_verbose(); 2061da177e4SLinus Torvalds bust_spinlocks(1); 2071da177e4SLinus Torvalds va_start(args, fmt); 208b49dec1cSBorislav Petkov len = vscnprintf(buf, sizeof(buf), fmt, args); 2091da177e4SLinus Torvalds va_end(args); 210b49dec1cSBorislav Petkov 211b49dec1cSBorislav Petkov if (len && buf[len - 1] == '\n') 212b49dec1cSBorislav Petkov buf[len - 1] = '\0'; 213b49dec1cSBorislav Petkov 214d7c0847fSFabian Frederick pr_emerg("Kernel panic - not syncing: %s\n", buf); 2155cb27301SIngo Molnar #ifdef CONFIG_DEBUG_BUGVERBOSE 2166e6f0a1fSAndi Kleen /* 2176e6f0a1fSAndi Kleen * Avoid nested stack-dumping if a panic occurs during oops processing 2186e6f0a1fSAndi Kleen */ 219026ee1f6SJason Wessel if (!test_taint(TAINT_DIE) && oops_in_progress <= 1) 2205cb27301SIngo Molnar dump_stack(); 2215cb27301SIngo Molnar #endif 2221da177e4SLinus Torvalds 223dc009d92SEric W. Biederman /* 2247d92bda2SDouglas Anderson * If kgdb is enabled, give it a chance to run before we stop all 2257d92bda2SDouglas Anderson * the other CPUs or else we won't be able to debug processes left 2267d92bda2SDouglas Anderson * running on them. 2277d92bda2SDouglas Anderson */ 2287d92bda2SDouglas Anderson kgdb_panic(buf); 2297d92bda2SDouglas Anderson 2307d92bda2SDouglas Anderson /* 231dc009d92SEric W. Biederman * If we have crashed and we have a crash kernel loaded let it handle 232dc009d92SEric W. Biederman * everything else. 233f06e5153SMasami Hiramatsu * If we want to run this after calling panic_notifiers, pass 234f06e5153SMasami Hiramatsu * the "crash_kexec_post_notifiers" option to the kernel. 2357bbee5caSHidehiro Kawai * 2367bbee5caSHidehiro Kawai * Bypass the panic_cpu check and call __crash_kexec directly. 237dc009d92SEric W. Biederman */ 238b26e27ddSHidehiro Kawai if (!_crash_kexec_post_notifiers) { 239f92bac3bSSergey Senozhatsky printk_safe_flush_on_panic(); 2407bbee5caSHidehiro Kawai __crash_kexec(NULL); 241dc009d92SEric W. Biederman 242dc009d92SEric W. Biederman /* 243dc009d92SEric W. Biederman * Note smp_send_stop is the usual smp shutdown function, which 2440ee59413SHidehiro Kawai * unfortunately means it may not be hardened to work in a 2450ee59413SHidehiro Kawai * panic situation. 246dc009d92SEric W. Biederman */ 2471da177e4SLinus Torvalds smp_send_stop(); 2480ee59413SHidehiro Kawai } else { 2490ee59413SHidehiro Kawai /* 2500ee59413SHidehiro Kawai * If we want to do crash dump after notifier calls and 2510ee59413SHidehiro Kawai * kmsg_dump, we will need architecture dependent extra 2520ee59413SHidehiro Kawai * works in addition to stopping other CPUs. 2530ee59413SHidehiro Kawai */ 2540ee59413SHidehiro Kawai crash_smp_send_stop(); 2550ee59413SHidehiro Kawai } 2561da177e4SLinus Torvalds 2576723734cSKees Cook /* 2586723734cSKees Cook * Run any panic handlers, including those that might need to 2596723734cSKees Cook * add information to the kmsg dump output. 2606723734cSKees Cook */ 261e041c683SAlan Stern atomic_notifier_call_chain(&panic_notifier_list, 0, buf); 2621da177e4SLinus Torvalds 263cf9b1106SPetr Mladek /* Call flush even twice. It tries harder with a single online CPU */ 264f92bac3bSSergey Senozhatsky printk_safe_flush_on_panic(); 2656723734cSKees Cook kmsg_dump(KMSG_DUMP_PANIC); 2666723734cSKees Cook 267f06e5153SMasami Hiramatsu /* 268f06e5153SMasami Hiramatsu * If you doubt kdump always works fine in any situation, 269f06e5153SMasami Hiramatsu * "crash_kexec_post_notifiers" offers you a chance to run 270f06e5153SMasami Hiramatsu * panic_notifiers and dumping kmsg before kdump. 271f06e5153SMasami Hiramatsu * Note: since some panic_notifiers can make crashed kernel 272f06e5153SMasami Hiramatsu * more unstable, it can increase risks of the kdump failure too. 2737bbee5caSHidehiro Kawai * 2747bbee5caSHidehiro Kawai * Bypass the panic_cpu check and call __crash_kexec directly. 275f06e5153SMasami Hiramatsu */ 276b26e27ddSHidehiro Kawai if (_crash_kexec_post_notifiers) 2777bbee5caSHidehiro Kawai __crash_kexec(NULL); 278f06e5153SMasami Hiramatsu 279c7c3f05eSSergey Senozhatsky #ifdef CONFIG_VT 280c7c3f05eSSergey Senozhatsky unblank_screen(); 281c7c3f05eSSergey Senozhatsky #endif 282c7c3f05eSSergey Senozhatsky console_unblank(); 283d014e889SAaro Koskinen 28408d78658SVitaly Kuznetsov /* 28508d78658SVitaly Kuznetsov * We may have ended up stopping the CPU holding the lock (in 28608d78658SVitaly Kuznetsov * smp_send_stop()) while still having some valuable data in the console 28708d78658SVitaly Kuznetsov * buffer. Try to acquire the lock then release it regardless of the 2887625b3a0SVitaly Kuznetsov * result. The release will also print the buffers out. Locks debug 2897625b3a0SVitaly Kuznetsov * should be disabled to avoid reporting bad unlock balance when 2907625b3a0SVitaly Kuznetsov * panic() is not being callled from OOPS. 29108d78658SVitaly Kuznetsov */ 2927625b3a0SVitaly Kuznetsov debug_locks_off(); 293de6da1e8SFeng Tang console_flush_on_panic(CONSOLE_FLUSH_PENDING); 29408d78658SVitaly Kuznetsov 295d999bd93SFeng Tang panic_print_sys_info(); 296d999bd93SFeng Tang 297c7ff0d9cSTAMUKI Shoichi if (!panic_blink) 298c7ff0d9cSTAMUKI Shoichi panic_blink = no_blink; 299c7ff0d9cSTAMUKI Shoichi 300dc009d92SEric W. Biederman if (panic_timeout > 0) { 3011da177e4SLinus Torvalds /* 3021da177e4SLinus Torvalds * Delay timeout seconds before rebooting the machine. 303c95dbf27SIngo Molnar * We can't use the "normal" timers since we just panicked. 3041da177e4SLinus Torvalds */ 305ff7a28a0SJiri Slaby pr_emerg("Rebooting in %d seconds..\n", panic_timeout); 306c95dbf27SIngo Molnar 307c7ff0d9cSTAMUKI Shoichi for (i = 0; i < panic_timeout * 1000; i += PANIC_TIMER_STEP) { 3081da177e4SLinus Torvalds touch_nmi_watchdog(); 309c7ff0d9cSTAMUKI Shoichi if (i >= i_next) { 310c7ff0d9cSTAMUKI Shoichi i += panic_blink(state ^= 1); 311c7ff0d9cSTAMUKI Shoichi i_next = i + 3600 / PANIC_BLINK_SPD; 312c7ff0d9cSTAMUKI Shoichi } 313c7ff0d9cSTAMUKI Shoichi mdelay(PANIC_TIMER_STEP); 3141da177e4SLinus Torvalds } 3154302fbc8SHugh Dickins } 3164302fbc8SHugh Dickins if (panic_timeout != 0) { 317c95dbf27SIngo Molnar /* 318c95dbf27SIngo Molnar * This will not be a clean reboot, with everything 3192f048ea8SEric W. Biederman * shutting down. But if there is a chance of 3202f048ea8SEric W. Biederman * rebooting the system it will be rebooted. 3211da177e4SLinus Torvalds */ 322b287a25aSAaro Koskinen if (panic_reboot_mode != REBOOT_UNDEFINED) 323b287a25aSAaro Koskinen reboot_mode = panic_reboot_mode; 3242f048ea8SEric W. Biederman emergency_restart(); 3251da177e4SLinus Torvalds } 3261da177e4SLinus Torvalds #ifdef __sparc__ 3271da177e4SLinus Torvalds { 3281da177e4SLinus Torvalds extern int stop_a_enabled; 329a271c241STom 'spot' Callaway /* Make sure the user can actually press Stop-A (L1-A) */ 3301da177e4SLinus Torvalds stop_a_enabled = 1; 3317db60d05SVijay Kumar pr_emerg("Press Stop-A (L1-A) from sun keyboard or send break\n" 3327db60d05SVijay Kumar "twice on console to return to the boot prom\n"); 3331da177e4SLinus Torvalds } 3341da177e4SLinus Torvalds #endif 335347a8dc3SMartin Schwidefsky #if defined(CONFIG_S390) 33698587c2dSMartin Schwidefsky disabled_wait(); 3371da177e4SLinus Torvalds #endif 3385ad75105SBorislav Petkov pr_emerg("---[ end Kernel panic - not syncing: %s ]---\n", buf); 339c39ea0b9SFeng Tang 340c39ea0b9SFeng Tang /* Do not scroll important messages printed above */ 341c39ea0b9SFeng Tang suppress_printk = 1; 3421da177e4SLinus Torvalds local_irq_enable(); 343c7ff0d9cSTAMUKI Shoichi for (i = 0; ; i += PANIC_TIMER_STEP) { 344c22db941SJan Beulich touch_softlockup_watchdog(); 345c7ff0d9cSTAMUKI Shoichi if (i >= i_next) { 346c7ff0d9cSTAMUKI Shoichi i += panic_blink(state ^= 1); 347c7ff0d9cSTAMUKI Shoichi i_next = i + 3600 / PANIC_BLINK_SPD; 348c7ff0d9cSTAMUKI Shoichi } 349c7ff0d9cSTAMUKI Shoichi mdelay(PANIC_TIMER_STEP); 3501da177e4SLinus Torvalds } 3511da177e4SLinus Torvalds } 3521da177e4SLinus Torvalds 3531da177e4SLinus Torvalds EXPORT_SYMBOL(panic); 3541da177e4SLinus Torvalds 3557fd8329bSPetr Mladek /* 3567fd8329bSPetr Mladek * TAINT_FORCED_RMMOD could be a per-module flag but the module 3577fd8329bSPetr Mladek * is being removed anyway. 3587fd8329bSPetr Mladek */ 3597fd8329bSPetr Mladek const struct taint_flag taint_flags[TAINT_FLAGS_COUNT] = { 36047d4b263SKees Cook [ TAINT_PROPRIETARY_MODULE ] = { 'P', 'G', true }, 36147d4b263SKees Cook [ TAINT_FORCED_MODULE ] = { 'F', ' ', true }, 36247d4b263SKees Cook [ TAINT_CPU_OUT_OF_SPEC ] = { 'S', ' ', false }, 36347d4b263SKees Cook [ TAINT_FORCED_RMMOD ] = { 'R', ' ', false }, 36447d4b263SKees Cook [ TAINT_MACHINE_CHECK ] = { 'M', ' ', false }, 36547d4b263SKees Cook [ TAINT_BAD_PAGE ] = { 'B', ' ', false }, 36647d4b263SKees Cook [ TAINT_USER ] = { 'U', ' ', false }, 36747d4b263SKees Cook [ TAINT_DIE ] = { 'D', ' ', false }, 36847d4b263SKees Cook [ TAINT_OVERRIDDEN_ACPI_TABLE ] = { 'A', ' ', false }, 36947d4b263SKees Cook [ TAINT_WARN ] = { 'W', ' ', false }, 37047d4b263SKees Cook [ TAINT_CRAP ] = { 'C', ' ', true }, 37147d4b263SKees Cook [ TAINT_FIRMWARE_WORKAROUND ] = { 'I', ' ', false }, 37247d4b263SKees Cook [ TAINT_OOT_MODULE ] = { 'O', ' ', true }, 37347d4b263SKees Cook [ TAINT_UNSIGNED_MODULE ] = { 'E', ' ', true }, 37447d4b263SKees Cook [ TAINT_SOFTLOCKUP ] = { 'L', ' ', false }, 37547d4b263SKees Cook [ TAINT_LIVEPATCH ] = { 'K', ' ', true }, 37647d4b263SKees Cook [ TAINT_AUX ] = { 'X', ' ', true }, 377bc4f2f54SKees Cook [ TAINT_RANDSTRUCT ] = { 'T', ' ', true }, 37825ddbb18SAndi Kleen }; 37925ddbb18SAndi Kleen 3801da177e4SLinus Torvalds /** 3811da177e4SLinus Torvalds * print_tainted - return a string to represent the kernel taint state. 3821da177e4SLinus Torvalds * 38357043247SMauro Carvalho Chehab * For individual taint flag meanings, see Documentation/admin-guide/sysctl/kernel.rst 3841da177e4SLinus Torvalds * 3859c4560e5SKees Cook * The string is overwritten by the next call to print_tainted(), 3869c4560e5SKees Cook * but is always NULL terminated. 3871da177e4SLinus Torvalds */ 3881da177e4SLinus Torvalds const char *print_tainted(void) 3891da177e4SLinus Torvalds { 3907fd8329bSPetr Mladek static char buf[TAINT_FLAGS_COUNT + sizeof("Tainted: ")]; 39125ddbb18SAndi Kleen 39247d4b263SKees Cook BUILD_BUG_ON(ARRAY_SIZE(taint_flags) != TAINT_FLAGS_COUNT); 39347d4b263SKees Cook 39425ddbb18SAndi Kleen if (tainted_mask) { 39525ddbb18SAndi Kleen char *s; 39625ddbb18SAndi Kleen int i; 39725ddbb18SAndi Kleen 39825ddbb18SAndi Kleen s = buf + sprintf(buf, "Tainted: "); 3997fd8329bSPetr Mladek for (i = 0; i < TAINT_FLAGS_COUNT; i++) { 4007fd8329bSPetr Mladek const struct taint_flag *t = &taint_flags[i]; 4017fd8329bSPetr Mladek *s++ = test_bit(i, &tainted_mask) ? 4025eb7c0d0SLarry Finger t->c_true : t->c_false; 4031da177e4SLinus Torvalds } 40425ddbb18SAndi Kleen *s = 0; 40525ddbb18SAndi Kleen } else 4061da177e4SLinus Torvalds snprintf(buf, sizeof(buf), "Not tainted"); 407c95dbf27SIngo Molnar 408c95dbf27SIngo Molnar return buf; 4091da177e4SLinus Torvalds } 4101da177e4SLinus Torvalds 41125ddbb18SAndi Kleen int test_taint(unsigned flag) 41225ddbb18SAndi Kleen { 41325ddbb18SAndi Kleen return test_bit(flag, &tainted_mask); 41425ddbb18SAndi Kleen } 41525ddbb18SAndi Kleen EXPORT_SYMBOL(test_taint); 41625ddbb18SAndi Kleen 41725ddbb18SAndi Kleen unsigned long get_taint(void) 41825ddbb18SAndi Kleen { 41925ddbb18SAndi Kleen return tainted_mask; 42025ddbb18SAndi Kleen } 42125ddbb18SAndi Kleen 422373d4d09SRusty Russell /** 423373d4d09SRusty Russell * add_taint: add a taint flag if not already set. 424373d4d09SRusty Russell * @flag: one of the TAINT_* constants. 425373d4d09SRusty Russell * @lockdep_ok: whether lock debugging is still OK. 426373d4d09SRusty Russell * 427373d4d09SRusty Russell * If something bad has gone wrong, you'll want @lockdebug_ok = false, but for 428373d4d09SRusty Russell * some notewortht-but-not-corrupting cases, it can be set to true. 4299eeba613SFrederic Weisbecker */ 430373d4d09SRusty Russell void add_taint(unsigned flag, enum lockdep_ok lockdep_ok) 431373d4d09SRusty Russell { 432373d4d09SRusty Russell if (lockdep_ok == LOCKDEP_NOW_UNRELIABLE && __debug_locks_off()) 433d7c0847fSFabian Frederick pr_warn("Disabling lock debugging due to kernel taint\n"); 4349eeba613SFrederic Weisbecker 43525ddbb18SAndi Kleen set_bit(flag, &tainted_mask); 4361da177e4SLinus Torvalds } 4371da177e4SLinus Torvalds EXPORT_SYMBOL(add_taint); 438dd287796SAndrew Morton 439dd287796SAndrew Morton static void spin_msec(int msecs) 440dd287796SAndrew Morton { 441dd287796SAndrew Morton int i; 442dd287796SAndrew Morton 443dd287796SAndrew Morton for (i = 0; i < msecs; i++) { 444dd287796SAndrew Morton touch_nmi_watchdog(); 445dd287796SAndrew Morton mdelay(1); 446dd287796SAndrew Morton } 447dd287796SAndrew Morton } 448dd287796SAndrew Morton 449dd287796SAndrew Morton /* 450dd287796SAndrew Morton * It just happens that oops_enter() and oops_exit() are identically 451dd287796SAndrew Morton * implemented... 452dd287796SAndrew Morton */ 453dd287796SAndrew Morton static void do_oops_enter_exit(void) 454dd287796SAndrew Morton { 455dd287796SAndrew Morton unsigned long flags; 456dd287796SAndrew Morton static int spin_counter; 457dd287796SAndrew Morton 458dd287796SAndrew Morton if (!pause_on_oops) 459dd287796SAndrew Morton return; 460dd287796SAndrew Morton 461dd287796SAndrew Morton spin_lock_irqsave(&pause_on_oops_lock, flags); 462dd287796SAndrew Morton if (pause_on_oops_flag == 0) { 463dd287796SAndrew Morton /* This CPU may now print the oops message */ 464dd287796SAndrew Morton pause_on_oops_flag = 1; 465dd287796SAndrew Morton } else { 466dd287796SAndrew Morton /* We need to stall this CPU */ 467dd287796SAndrew Morton if (!spin_counter) { 468dd287796SAndrew Morton /* This CPU gets to do the counting */ 469dd287796SAndrew Morton spin_counter = pause_on_oops; 470dd287796SAndrew Morton do { 471dd287796SAndrew Morton spin_unlock(&pause_on_oops_lock); 472dd287796SAndrew Morton spin_msec(MSEC_PER_SEC); 473dd287796SAndrew Morton spin_lock(&pause_on_oops_lock); 474dd287796SAndrew Morton } while (--spin_counter); 475dd287796SAndrew Morton pause_on_oops_flag = 0; 476dd287796SAndrew Morton } else { 477dd287796SAndrew Morton /* This CPU waits for a different one */ 478dd287796SAndrew Morton while (spin_counter) { 479dd287796SAndrew Morton spin_unlock(&pause_on_oops_lock); 480dd287796SAndrew Morton spin_msec(1); 481dd287796SAndrew Morton spin_lock(&pause_on_oops_lock); 482dd287796SAndrew Morton } 483dd287796SAndrew Morton } 484dd287796SAndrew Morton } 485dd287796SAndrew Morton spin_unlock_irqrestore(&pause_on_oops_lock, flags); 486dd287796SAndrew Morton } 487dd287796SAndrew Morton 488dd287796SAndrew Morton /* 489c95dbf27SIngo Molnar * Return true if the calling CPU is allowed to print oops-related info. 490c95dbf27SIngo Molnar * This is a bit racy.. 491dd287796SAndrew Morton */ 492dd287796SAndrew Morton int oops_may_print(void) 493dd287796SAndrew Morton { 494dd287796SAndrew Morton return pause_on_oops_flag == 0; 495dd287796SAndrew Morton } 496dd287796SAndrew Morton 497dd287796SAndrew Morton /* 498dd287796SAndrew Morton * Called when the architecture enters its oops handler, before it prints 499c95dbf27SIngo Molnar * anything. If this is the first CPU to oops, and it's oopsing the first 500c95dbf27SIngo Molnar * time then let it proceed. 501dd287796SAndrew Morton * 502c95dbf27SIngo Molnar * This is all enabled by the pause_on_oops kernel boot option. We do all 503c95dbf27SIngo Molnar * this to ensure that oopses don't scroll off the screen. It has the 504c95dbf27SIngo Molnar * side-effect of preventing later-oopsing CPUs from mucking up the display, 505c95dbf27SIngo Molnar * too. 506dd287796SAndrew Morton * 507c95dbf27SIngo Molnar * It turns out that the CPU which is allowed to print ends up pausing for 508c95dbf27SIngo Molnar * the right duration, whereas all the other CPUs pause for twice as long: 509c95dbf27SIngo Molnar * once in oops_enter(), once in oops_exit(). 510dd287796SAndrew Morton */ 511dd287796SAndrew Morton void oops_enter(void) 512dd287796SAndrew Morton { 513bdff7870SThomas Gleixner tracing_off(); 514c95dbf27SIngo Molnar /* can't trust the integrity of the kernel anymore: */ 515c95dbf27SIngo Molnar debug_locks_off(); 516dd287796SAndrew Morton do_oops_enter_exit(); 517dd287796SAndrew Morton } 518dd287796SAndrew Morton 519dd287796SAndrew Morton /* 5202c3b20e9SArjan van de Ven * 64-bit random ID for oopses: 5212c3b20e9SArjan van de Ven */ 5222c3b20e9SArjan van de Ven static u64 oops_id; 5232c3b20e9SArjan van de Ven 5242c3b20e9SArjan van de Ven static int init_oops_id(void) 5252c3b20e9SArjan van de Ven { 5262c3b20e9SArjan van de Ven if (!oops_id) 5272c3b20e9SArjan van de Ven get_random_bytes(&oops_id, sizeof(oops_id)); 528d6624f99SArjan van de Ven else 529d6624f99SArjan van de Ven oops_id++; 5302c3b20e9SArjan van de Ven 5312c3b20e9SArjan van de Ven return 0; 5322c3b20e9SArjan van de Ven } 5332c3b20e9SArjan van de Ven late_initcall(init_oops_id); 5342c3b20e9SArjan van de Ven 535863a6049SAnton Blanchard void print_oops_end_marker(void) 53671c33911SArjan van de Ven { 53771c33911SArjan van de Ven init_oops_id(); 538d7c0847fSFabian Frederick pr_warn("---[ end trace %016llx ]---\n", (unsigned long long)oops_id); 53971c33911SArjan van de Ven } 54071c33911SArjan van de Ven 5412c3b20e9SArjan van de Ven /* 542dd287796SAndrew Morton * Called when the architecture exits its oops handler, after printing 543dd287796SAndrew Morton * everything. 544dd287796SAndrew Morton */ 545dd287796SAndrew Morton void oops_exit(void) 546dd287796SAndrew Morton { 547dd287796SAndrew Morton do_oops_enter_exit(); 54871c33911SArjan van de Ven print_oops_end_marker(); 549456b565cSSimon Kagstrom kmsg_dump(KMSG_DUMP_OOPS); 550dd287796SAndrew Morton } 5513162f751SArjan van de Ven 5522553b67aSJosh Poimboeuf struct warn_args { 5530f6f49a8SLinus Torvalds const char *fmt; 554a8f18b90SArjan van de Ven va_list args; 5550f6f49a8SLinus Torvalds }; 5560f6f49a8SLinus Torvalds 5572553b67aSJosh Poimboeuf void __warn(const char *file, int line, void *caller, unsigned taint, 5582553b67aSJosh Poimboeuf struct pt_regs *regs, struct warn_args *args) 5590f6f49a8SLinus Torvalds { 560de7edd31SSteven Rostedt (Red Hat) disable_trace_on_warning(); 561de7edd31SSteven Rostedt (Red Hat) 5622553b67aSJosh Poimboeuf if (file) 5632553b67aSJosh Poimboeuf pr_warn("WARNING: CPU: %d PID: %d at %s:%d %pS\n", 5642553b67aSJosh Poimboeuf raw_smp_processor_id(), current->pid, file, line, 5652553b67aSJosh Poimboeuf caller); 5662553b67aSJosh Poimboeuf else 5672553b67aSJosh Poimboeuf pr_warn("WARNING: CPU: %d PID: %d at %pS\n", 5682553b67aSJosh Poimboeuf raw_smp_processor_id(), current->pid, caller); 56974853dbaSArjan van de Ven 5700f6f49a8SLinus Torvalds if (args) 5710f6f49a8SLinus Torvalds vprintk(args->fmt, args->args); 572a8f18b90SArjan van de Ven 5739e3961a0SPrarit Bhargava if (panic_on_warn) { 5749e3961a0SPrarit Bhargava /* 5759e3961a0SPrarit Bhargava * This thread may hit another WARN() in the panic path. 5769e3961a0SPrarit Bhargava * Resetting this prevents additional WARN() from panicking the 5779e3961a0SPrarit Bhargava * system on this thread. Other threads are blocked by the 5789e3961a0SPrarit Bhargava * panic_mutex in panic(). 5799e3961a0SPrarit Bhargava */ 5809e3961a0SPrarit Bhargava panic_on_warn = 0; 5819e3961a0SPrarit Bhargava panic("panic_on_warn set ...\n"); 5829e3961a0SPrarit Bhargava } 5839e3961a0SPrarit Bhargava 584a8f18b90SArjan van de Ven print_modules(); 5852553b67aSJosh Poimboeuf 5862553b67aSJosh Poimboeuf if (regs) 5872553b67aSJosh Poimboeuf show_regs(regs); 5882553b67aSJosh Poimboeuf else 589a8f18b90SArjan van de Ven dump_stack(); 5902553b67aSJosh Poimboeuf 5914c281074SSteven Rostedt (VMware) print_irqtrace_events(current); 5924c281074SSteven Rostedt (VMware) 593a8f18b90SArjan van de Ven print_oops_end_marker(); 5942553b67aSJosh Poimboeuf 595373d4d09SRusty Russell /* Just a warning, don't kill lockdep. */ 596373d4d09SRusty Russell add_taint(taint, LOCKDEP_STILL_OK); 597a8f18b90SArjan van de Ven } 5980f6f49a8SLinus Torvalds 599*2da1ead4SKees Cook #ifndef __WARN_FLAGS 600ee871133SKees Cook void warn_slowpath_fmt(const char *file, int line, unsigned taint, 601ee871133SKees Cook const char *fmt, ...) 602b2be0527SBen Hutchings { 6032553b67aSJosh Poimboeuf struct warn_args args; 604b2be0527SBen Hutchings 605f2f84b05SKees Cook pr_warn(CUT_HERE); 606d38aba49SKees Cook 607d38aba49SKees Cook if (!fmt) { 608f2f84b05SKees Cook __warn(file, line, __builtin_return_address(0), taint, 609f2f84b05SKees Cook NULL, NULL); 610f2f84b05SKees Cook return; 611f2f84b05SKees Cook } 612f2f84b05SKees Cook 613b2be0527SBen Hutchings args.fmt = fmt; 614b2be0527SBen Hutchings va_start(args.args, fmt); 6152553b67aSJosh Poimboeuf __warn(file, line, __builtin_return_address(0), taint, NULL, &args); 616b2be0527SBen Hutchings va_end(args.args); 617b2be0527SBen Hutchings } 618ee871133SKees Cook EXPORT_SYMBOL(warn_slowpath_fmt); 619a7bed27aSKees Cook #else 620a7bed27aSKees Cook void __warn_printk(const char *fmt, ...) 621a7bed27aSKees Cook { 622a7bed27aSKees Cook va_list args; 623a7bed27aSKees Cook 624a7bed27aSKees Cook pr_warn(CUT_HERE); 625a7bed27aSKees Cook 626a7bed27aSKees Cook va_start(args, fmt); 627a7bed27aSKees Cook vprintk(fmt, args); 628a7bed27aSKees Cook va_end(args); 629a7bed27aSKees Cook } 630a7bed27aSKees Cook EXPORT_SYMBOL(__warn_printk); 63179b4cc5eSArjan van de Ven #endif 63279b4cc5eSArjan van de Ven 633b1fca27dSAndi Kleen #ifdef CONFIG_BUG 634b1fca27dSAndi Kleen 635b1fca27dSAndi Kleen /* Support resetting WARN*_ONCE state */ 636b1fca27dSAndi Kleen 637b1fca27dSAndi Kleen static int clear_warn_once_set(void *data, u64 val) 638b1fca27dSAndi Kleen { 639aaf5dcfbSAndi Kleen generic_bug_clear_once(); 640b1fca27dSAndi Kleen memset(__start_once, 0, __end_once - __start_once); 641b1fca27dSAndi Kleen return 0; 642b1fca27dSAndi Kleen } 643b1fca27dSAndi Kleen 6444169680eSYueHaibing DEFINE_DEBUGFS_ATTRIBUTE(clear_warn_once_fops, NULL, clear_warn_once_set, 645b1fca27dSAndi Kleen "%lld\n"); 646b1fca27dSAndi Kleen 647b1fca27dSAndi Kleen static __init int register_warn_debugfs(void) 648b1fca27dSAndi Kleen { 649b1fca27dSAndi Kleen /* Don't care about failure */ 6504169680eSYueHaibing debugfs_create_file_unsafe("clear_warn_once", 0200, NULL, NULL, 6514169680eSYueHaibing &clear_warn_once_fops); 652b1fca27dSAndi Kleen return 0; 653b1fca27dSAndi Kleen } 654b1fca27dSAndi Kleen 655b1fca27dSAndi Kleen device_initcall(register_warn_debugfs); 656b1fca27dSAndi Kleen #endif 657b1fca27dSAndi Kleen 658050e9baaSLinus Torvalds #ifdef CONFIG_STACKPROTECTOR 65954371a43SArjan van de Ven 6603162f751SArjan van de Ven /* 6613162f751SArjan van de Ven * Called when gcc's -fstack-protector feature is used, and 6623162f751SArjan van de Ven * gcc detects corruption of the on-stack canary value 6633162f751SArjan van de Ven */ 664a7330c99SAndi Kleen __visible void __stack_chk_fail(void) 6653162f751SArjan van de Ven { 66695c4fb78SBorislav Petkov panic("stack-protector: Kernel stack is corrupted in: %pB", 667517a92c4SIngo Molnar __builtin_return_address(0)); 6683162f751SArjan van de Ven } 6693162f751SArjan van de Ven EXPORT_SYMBOL(__stack_chk_fail); 67054371a43SArjan van de Ven 6713162f751SArjan van de Ven #endif 672f44dd164SRusty Russell 6737a46ec0eSKees Cook #ifdef CONFIG_ARCH_HAS_REFCOUNT 6747a46ec0eSKees Cook void refcount_error_report(struct pt_regs *regs, const char *err) 6757a46ec0eSKees Cook { 6767a46ec0eSKees Cook WARN_RATELIMIT(1, "refcount_t %s at %pB in %s[%d], uid/euid: %u/%u\n", 6777a46ec0eSKees Cook err, (void *)instruction_pointer(regs), 6787a46ec0eSKees Cook current->comm, task_pid_nr(current), 6797a46ec0eSKees Cook from_kuid_munged(&init_user_ns, current_uid()), 6807a46ec0eSKees Cook from_kuid_munged(&init_user_ns, current_euid())); 6817a46ec0eSKees Cook } 6827a46ec0eSKees Cook #endif 6837a46ec0eSKees Cook 684f44dd164SRusty Russell core_param(panic, panic_timeout, int, 0644); 685d999bd93SFeng Tang core_param(panic_print, panic_print, ulong, 0644); 686f44dd164SRusty Russell core_param(pause_on_oops, pause_on_oops, int, 0644); 6879e3961a0SPrarit Bhargava core_param(panic_on_warn, panic_on_warn, int, 0644); 688b26e27ddSHidehiro Kawai core_param(crash_kexec_post_notifiers, crash_kexec_post_notifiers, bool, 0644); 689f06e5153SMasami Hiramatsu 690d404ab0aSOlaf Hering static int __init oops_setup(char *s) 691d404ab0aSOlaf Hering { 692d404ab0aSOlaf Hering if (!s) 693d404ab0aSOlaf Hering return -EINVAL; 694d404ab0aSOlaf Hering if (!strcmp(s, "panic")) 695d404ab0aSOlaf Hering panic_on_oops = 1; 696d404ab0aSOlaf Hering return 0; 697d404ab0aSOlaf Hering } 698d404ab0aSOlaf Hering early_param("oops", oops_setup); 699