11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * linux/kernel/panic.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Copyright (C) 1991, 1992 Linus Torvalds 51da177e4SLinus Torvalds */ 61da177e4SLinus Torvalds 71da177e4SLinus Torvalds /* 81da177e4SLinus Torvalds * This function is used through-out the kernel (including mm and fs) 91da177e4SLinus Torvalds * to indicate a major problem. 101da177e4SLinus Torvalds */ 11657b3010SAndrew Morton #include <linux/debug_locks.h> 12b17b0153SIngo Molnar #include <linux/sched/debug.h> 13c95dbf27SIngo Molnar #include <linux/interrupt.h> 14456b565cSSimon Kagstrom #include <linux/kmsg_dump.h> 1579b4cc5eSArjan van de Ven #include <linux/kallsyms.h> 16c95dbf27SIngo Molnar #include <linux/notifier.h> 17c95dbf27SIngo Molnar #include <linux/module.h> 18c95dbf27SIngo Molnar #include <linux/random.h> 19de7edd31SSteven Rostedt (Red Hat) #include <linux/ftrace.h> 20c95dbf27SIngo Molnar #include <linux/reboot.h> 21c95dbf27SIngo Molnar #include <linux/delay.h> 22c95dbf27SIngo Molnar #include <linux/kexec.h> 23c95dbf27SIngo Molnar #include <linux/sched.h> 24c95dbf27SIngo Molnar #include <linux/sysrq.h> 25c95dbf27SIngo Molnar #include <linux/init.h> 26c95dbf27SIngo Molnar #include <linux/nmi.h> 2708d78658SVitaly Kuznetsov #include <linux/console.h> 282553b67aSJosh Poimboeuf #include <linux/bug.h> 297a46ec0eSKees Cook #include <linux/ratelimit.h> 30b1fca27dSAndi Kleen #include <linux/debugfs.h> 31b1fca27dSAndi Kleen #include <asm/sections.h> 321da177e4SLinus Torvalds 33c7ff0d9cSTAMUKI Shoichi #define PANIC_TIMER_STEP 100 34c7ff0d9cSTAMUKI Shoichi #define PANIC_BLINK_SPD 18 35c7ff0d9cSTAMUKI Shoichi 362a01bb38SKyle McMartin int panic_on_oops = CONFIG_PANIC_ON_OOPS_VALUE; 3725ddbb18SAndi Kleen static unsigned long tainted_mask; 38dd287796SAndrew Morton static int pause_on_oops; 39dd287796SAndrew Morton static int pause_on_oops_flag; 40dd287796SAndrew Morton static DEFINE_SPINLOCK(pause_on_oops_lock); 415375b708SHATAYAMA Daisuke bool crash_kexec_post_notifiers; 429e3961a0SPrarit Bhargava int panic_on_warn __read_mostly; 431da177e4SLinus Torvalds 445800dc3cSJason Baron int panic_timeout = CONFIG_PANIC_TIMEOUT; 4581e88fdcSHuang Ying EXPORT_SYMBOL_GPL(panic_timeout); 461da177e4SLinus Torvalds 47e041c683SAlan Stern ATOMIC_NOTIFIER_HEAD(panic_notifier_list); 481da177e4SLinus Torvalds 491da177e4SLinus Torvalds EXPORT_SYMBOL(panic_notifier_list); 501da177e4SLinus Torvalds 51c7ff0d9cSTAMUKI Shoichi static long no_blink(int state) 528aeee85aSAnton Blanchard { 53c7ff0d9cSTAMUKI Shoichi return 0; 54c7ff0d9cSTAMUKI Shoichi } 558aeee85aSAnton Blanchard 56c7ff0d9cSTAMUKI Shoichi /* Returns how long it waited in ms */ 57c7ff0d9cSTAMUKI Shoichi long (*panic_blink)(int state); 58c7ff0d9cSTAMUKI Shoichi EXPORT_SYMBOL(panic_blink); 598aeee85aSAnton Blanchard 6093e13a36SMichael Holzheu /* 6193e13a36SMichael Holzheu * Stop ourself in panic -- architecture code may override this 6293e13a36SMichael Holzheu */ 6393e13a36SMichael Holzheu void __weak panic_smp_self_stop(void) 6493e13a36SMichael Holzheu { 6593e13a36SMichael Holzheu while (1) 6693e13a36SMichael Holzheu cpu_relax(); 6793e13a36SMichael Holzheu } 6893e13a36SMichael Holzheu 6958c5661fSHidehiro Kawai /* 7058c5661fSHidehiro Kawai * Stop ourselves in NMI context if another CPU has already panicked. Arch code 7158c5661fSHidehiro Kawai * may override this to prepare for crash dumping, e.g. save regs info. 7258c5661fSHidehiro Kawai */ 7358c5661fSHidehiro Kawai void __weak nmi_panic_self_stop(struct pt_regs *regs) 7458c5661fSHidehiro Kawai { 7558c5661fSHidehiro Kawai panic_smp_self_stop(); 7658c5661fSHidehiro Kawai } 7758c5661fSHidehiro Kawai 780ee59413SHidehiro Kawai /* 790ee59413SHidehiro Kawai * Stop other CPUs in panic. Architecture dependent code may override this 800ee59413SHidehiro Kawai * with more suitable version. For example, if the architecture supports 810ee59413SHidehiro Kawai * crash dump, it should save registers of each stopped CPU and disable 820ee59413SHidehiro Kawai * per-CPU features such as virtualization extensions. 830ee59413SHidehiro Kawai */ 840ee59413SHidehiro Kawai void __weak crash_smp_send_stop(void) 850ee59413SHidehiro Kawai { 860ee59413SHidehiro Kawai static int cpus_stopped; 870ee59413SHidehiro Kawai 880ee59413SHidehiro Kawai /* 890ee59413SHidehiro Kawai * This function can be called twice in panic path, but obviously 900ee59413SHidehiro Kawai * we execute this only once. 910ee59413SHidehiro Kawai */ 920ee59413SHidehiro Kawai if (cpus_stopped) 930ee59413SHidehiro Kawai return; 940ee59413SHidehiro Kawai 950ee59413SHidehiro Kawai /* 960ee59413SHidehiro Kawai * Note smp_send_stop is the usual smp shutdown function, which 970ee59413SHidehiro Kawai * unfortunately means it may not be hardened to work in a panic 980ee59413SHidehiro Kawai * situation. 990ee59413SHidehiro Kawai */ 1000ee59413SHidehiro Kawai smp_send_stop(); 1010ee59413SHidehiro Kawai cpus_stopped = 1; 1020ee59413SHidehiro Kawai } 1030ee59413SHidehiro Kawai 1041717f209SHidehiro Kawai atomic_t panic_cpu = ATOMIC_INIT(PANIC_CPU_INVALID); 1051717f209SHidehiro Kawai 106ebc41f20SHidehiro Kawai /* 107ebc41f20SHidehiro Kawai * A variant of panic() called from NMI context. We return if we've already 108ebc41f20SHidehiro Kawai * panicked on this CPU. If another CPU already panicked, loop in 109ebc41f20SHidehiro Kawai * nmi_panic_self_stop() which can provide architecture dependent code such 110ebc41f20SHidehiro Kawai * as saving register state for crash dump. 111ebc41f20SHidehiro Kawai */ 112ebc41f20SHidehiro Kawai void nmi_panic(struct pt_regs *regs, const char *msg) 113ebc41f20SHidehiro Kawai { 114ebc41f20SHidehiro Kawai int old_cpu, cpu; 115ebc41f20SHidehiro Kawai 116ebc41f20SHidehiro Kawai cpu = raw_smp_processor_id(); 117ebc41f20SHidehiro Kawai old_cpu = atomic_cmpxchg(&panic_cpu, PANIC_CPU_INVALID, cpu); 118ebc41f20SHidehiro Kawai 119ebc41f20SHidehiro Kawai if (old_cpu == PANIC_CPU_INVALID) 120ebc41f20SHidehiro Kawai panic("%s", msg); 121ebc41f20SHidehiro Kawai else if (old_cpu != cpu) 122ebc41f20SHidehiro Kawai nmi_panic_self_stop(regs); 123ebc41f20SHidehiro Kawai } 124ebc41f20SHidehiro Kawai EXPORT_SYMBOL(nmi_panic); 125ebc41f20SHidehiro Kawai 1261da177e4SLinus Torvalds /** 1271da177e4SLinus Torvalds * panic - halt the system 1281da177e4SLinus Torvalds * @fmt: The text string to print 1291da177e4SLinus Torvalds * 1301da177e4SLinus Torvalds * Display a message, then perform cleanups. 1311da177e4SLinus Torvalds * 1321da177e4SLinus Torvalds * This function never returns. 1331da177e4SLinus Torvalds */ 1349402c95fSJoe Perches void panic(const char *fmt, ...) 1351da177e4SLinus Torvalds { 1361da177e4SLinus Torvalds static char buf[1024]; 1371da177e4SLinus Torvalds va_list args; 138c7ff0d9cSTAMUKI Shoichi long i, i_next = 0; 139c7ff0d9cSTAMUKI Shoichi int state = 0; 1401717f209SHidehiro Kawai int old_cpu, this_cpu; 141b26e27ddSHidehiro Kawai bool _crash_kexec_post_notifiers = crash_kexec_post_notifiers; 1421da177e4SLinus Torvalds 143dc009d92SEric W. Biederman /* 144190320c3SVikram Mulukutla * Disable local interrupts. This will prevent panic_smp_self_stop 145190320c3SVikram Mulukutla * from deadlocking the first cpu that invokes the panic, since 146190320c3SVikram Mulukutla * there is nothing to prevent an interrupt handler (that runs 1471717f209SHidehiro Kawai * after setting panic_cpu) from invoking panic() again. 148190320c3SVikram Mulukutla */ 149190320c3SVikram Mulukutla local_irq_disable(); 150190320c3SVikram Mulukutla 151190320c3SVikram Mulukutla /* 152c95dbf27SIngo Molnar * It's possible to come here directly from a panic-assertion and 153c95dbf27SIngo Molnar * not have preempt disabled. Some functions called from here want 154dc009d92SEric W. Biederman * preempt to be disabled. No point enabling it later though... 15593e13a36SMichael Holzheu * 15693e13a36SMichael Holzheu * Only one CPU is allowed to execute the panic code from here. For 15793e13a36SMichael Holzheu * multiple parallel invocations of panic, all other CPUs either 15893e13a36SMichael Holzheu * stop themself or will wait until they are stopped by the 1st CPU 15993e13a36SMichael Holzheu * with smp_send_stop(). 1601717f209SHidehiro Kawai * 1611717f209SHidehiro Kawai * `old_cpu == PANIC_CPU_INVALID' means this is the 1st CPU which 1621717f209SHidehiro Kawai * comes here, so go ahead. 1631717f209SHidehiro Kawai * `old_cpu == this_cpu' means we came from nmi_panic() which sets 1641717f209SHidehiro Kawai * panic_cpu to this CPU. In this case, this is also the 1st CPU. 165dc009d92SEric W. Biederman */ 1661717f209SHidehiro Kawai this_cpu = raw_smp_processor_id(); 1671717f209SHidehiro Kawai old_cpu = atomic_cmpxchg(&panic_cpu, PANIC_CPU_INVALID, this_cpu); 1681717f209SHidehiro Kawai 1691717f209SHidehiro Kawai if (old_cpu != PANIC_CPU_INVALID && old_cpu != this_cpu) 17093e13a36SMichael Holzheu panic_smp_self_stop(); 171dc009d92SEric W. Biederman 1725b530fc1SAnton Blanchard console_verbose(); 1731da177e4SLinus Torvalds bust_spinlocks(1); 1741da177e4SLinus Torvalds va_start(args, fmt); 1751da177e4SLinus Torvalds vsnprintf(buf, sizeof(buf), fmt, args); 1761da177e4SLinus Torvalds va_end(args); 177d7c0847fSFabian Frederick pr_emerg("Kernel panic - not syncing: %s\n", buf); 1785cb27301SIngo Molnar #ifdef CONFIG_DEBUG_BUGVERBOSE 1796e6f0a1fSAndi Kleen /* 1806e6f0a1fSAndi Kleen * Avoid nested stack-dumping if a panic occurs during oops processing 1816e6f0a1fSAndi Kleen */ 182026ee1f6SJason Wessel if (!test_taint(TAINT_DIE) && oops_in_progress <= 1) 1835cb27301SIngo Molnar dump_stack(); 1845cb27301SIngo Molnar #endif 1851da177e4SLinus Torvalds 186dc009d92SEric W. Biederman /* 187dc009d92SEric W. Biederman * If we have crashed and we have a crash kernel loaded let it handle 188dc009d92SEric W. Biederman * everything else. 189f06e5153SMasami Hiramatsu * If we want to run this after calling panic_notifiers, pass 190f06e5153SMasami Hiramatsu * the "crash_kexec_post_notifiers" option to the kernel. 1917bbee5caSHidehiro Kawai * 1927bbee5caSHidehiro Kawai * Bypass the panic_cpu check and call __crash_kexec directly. 193dc009d92SEric W. Biederman */ 194b26e27ddSHidehiro Kawai if (!_crash_kexec_post_notifiers) { 195f92bac3bSSergey Senozhatsky printk_safe_flush_on_panic(); 1967bbee5caSHidehiro Kawai __crash_kexec(NULL); 197dc009d92SEric W. Biederman 198dc009d92SEric W. Biederman /* 199dc009d92SEric W. Biederman * Note smp_send_stop is the usual smp shutdown function, which 2000ee59413SHidehiro Kawai * unfortunately means it may not be hardened to work in a 2010ee59413SHidehiro Kawai * panic situation. 202dc009d92SEric W. Biederman */ 2031da177e4SLinus Torvalds smp_send_stop(); 2040ee59413SHidehiro Kawai } else { 2050ee59413SHidehiro Kawai /* 2060ee59413SHidehiro Kawai * If we want to do crash dump after notifier calls and 2070ee59413SHidehiro Kawai * kmsg_dump, we will need architecture dependent extra 2080ee59413SHidehiro Kawai * works in addition to stopping other CPUs. 2090ee59413SHidehiro Kawai */ 2100ee59413SHidehiro Kawai crash_smp_send_stop(); 2110ee59413SHidehiro Kawai } 2121da177e4SLinus Torvalds 2136723734cSKees Cook /* 2146723734cSKees Cook * Run any panic handlers, including those that might need to 2156723734cSKees Cook * add information to the kmsg dump output. 2166723734cSKees Cook */ 217e041c683SAlan Stern atomic_notifier_call_chain(&panic_notifier_list, 0, buf); 2181da177e4SLinus Torvalds 219cf9b1106SPetr Mladek /* Call flush even twice. It tries harder with a single online CPU */ 220f92bac3bSSergey Senozhatsky printk_safe_flush_on_panic(); 2216723734cSKees Cook kmsg_dump(KMSG_DUMP_PANIC); 2226723734cSKees Cook 223f06e5153SMasami Hiramatsu /* 224f06e5153SMasami Hiramatsu * If you doubt kdump always works fine in any situation, 225f06e5153SMasami Hiramatsu * "crash_kexec_post_notifiers" offers you a chance to run 226f06e5153SMasami Hiramatsu * panic_notifiers and dumping kmsg before kdump. 227f06e5153SMasami Hiramatsu * Note: since some panic_notifiers can make crashed kernel 228f06e5153SMasami Hiramatsu * more unstable, it can increase risks of the kdump failure too. 2297bbee5caSHidehiro Kawai * 2307bbee5caSHidehiro Kawai * Bypass the panic_cpu check and call __crash_kexec directly. 231f06e5153SMasami Hiramatsu */ 232b26e27ddSHidehiro Kawai if (_crash_kexec_post_notifiers) 2337bbee5caSHidehiro Kawai __crash_kexec(NULL); 234f06e5153SMasami Hiramatsu 235d014e889SAaro Koskinen bust_spinlocks(0); 236d014e889SAaro Koskinen 23708d78658SVitaly Kuznetsov /* 23808d78658SVitaly Kuznetsov * We may have ended up stopping the CPU holding the lock (in 23908d78658SVitaly Kuznetsov * smp_send_stop()) while still having some valuable data in the console 24008d78658SVitaly Kuznetsov * buffer. Try to acquire the lock then release it regardless of the 2417625b3a0SVitaly Kuznetsov * result. The release will also print the buffers out. Locks debug 2427625b3a0SVitaly Kuznetsov * should be disabled to avoid reporting bad unlock balance when 2437625b3a0SVitaly Kuznetsov * panic() is not being callled from OOPS. 24408d78658SVitaly Kuznetsov */ 2457625b3a0SVitaly Kuznetsov debug_locks_off(); 2468d91f8b1STejun Heo console_flush_on_panic(); 24708d78658SVitaly Kuznetsov 248c7ff0d9cSTAMUKI Shoichi if (!panic_blink) 249c7ff0d9cSTAMUKI Shoichi panic_blink = no_blink; 250c7ff0d9cSTAMUKI Shoichi 251dc009d92SEric W. Biederman if (panic_timeout > 0) { 2521da177e4SLinus Torvalds /* 2531da177e4SLinus Torvalds * Delay timeout seconds before rebooting the machine. 254c95dbf27SIngo Molnar * We can't use the "normal" timers since we just panicked. 2551da177e4SLinus Torvalds */ 256ff7a28a0SJiri Slaby pr_emerg("Rebooting in %d seconds..\n", panic_timeout); 257c95dbf27SIngo Molnar 258c7ff0d9cSTAMUKI Shoichi for (i = 0; i < panic_timeout * 1000; i += PANIC_TIMER_STEP) { 2591da177e4SLinus Torvalds touch_nmi_watchdog(); 260c7ff0d9cSTAMUKI Shoichi if (i >= i_next) { 261c7ff0d9cSTAMUKI Shoichi i += panic_blink(state ^= 1); 262c7ff0d9cSTAMUKI Shoichi i_next = i + 3600 / PANIC_BLINK_SPD; 263c7ff0d9cSTAMUKI Shoichi } 264c7ff0d9cSTAMUKI Shoichi mdelay(PANIC_TIMER_STEP); 2651da177e4SLinus Torvalds } 2664302fbc8SHugh Dickins } 2674302fbc8SHugh Dickins if (panic_timeout != 0) { 268c95dbf27SIngo Molnar /* 269c95dbf27SIngo Molnar * This will not be a clean reboot, with everything 2702f048ea8SEric W. Biederman * shutting down. But if there is a chance of 2712f048ea8SEric W. Biederman * rebooting the system it will be rebooted. 2721da177e4SLinus Torvalds */ 2732f048ea8SEric W. Biederman emergency_restart(); 2741da177e4SLinus Torvalds } 2751da177e4SLinus Torvalds #ifdef __sparc__ 2761da177e4SLinus Torvalds { 2771da177e4SLinus Torvalds extern int stop_a_enabled; 278a271c241STom 'spot' Callaway /* Make sure the user can actually press Stop-A (L1-A) */ 2791da177e4SLinus Torvalds stop_a_enabled = 1; 2807db60d05SVijay Kumar pr_emerg("Press Stop-A (L1-A) from sun keyboard or send break\n" 2817db60d05SVijay Kumar "twice on console to return to the boot prom\n"); 2821da177e4SLinus Torvalds } 2831da177e4SLinus Torvalds #endif 284347a8dc3SMartin Schwidefsky #if defined(CONFIG_S390) 285c95dbf27SIngo Molnar { 286c95dbf27SIngo Molnar unsigned long caller; 287c95dbf27SIngo Molnar 288c95dbf27SIngo Molnar caller = (unsigned long)__builtin_return_address(0); 2891da177e4SLinus Torvalds disabled_wait(caller); 290c95dbf27SIngo Molnar } 2911da177e4SLinus Torvalds #endif 292d7c0847fSFabian Frederick pr_emerg("---[ end Kernel panic - not syncing: %s\n", buf); 2931da177e4SLinus Torvalds local_irq_enable(); 294c7ff0d9cSTAMUKI Shoichi for (i = 0; ; i += PANIC_TIMER_STEP) { 295c22db941SJan Beulich touch_softlockup_watchdog(); 296c7ff0d9cSTAMUKI Shoichi if (i >= i_next) { 297c7ff0d9cSTAMUKI Shoichi i += panic_blink(state ^= 1); 298c7ff0d9cSTAMUKI Shoichi i_next = i + 3600 / PANIC_BLINK_SPD; 299c7ff0d9cSTAMUKI Shoichi } 300c7ff0d9cSTAMUKI Shoichi mdelay(PANIC_TIMER_STEP); 3011da177e4SLinus Torvalds } 3021da177e4SLinus Torvalds } 3031da177e4SLinus Torvalds 3041da177e4SLinus Torvalds EXPORT_SYMBOL(panic); 3051da177e4SLinus Torvalds 3067fd8329bSPetr Mladek /* 3077fd8329bSPetr Mladek * TAINT_FORCED_RMMOD could be a per-module flag but the module 3087fd8329bSPetr Mladek * is being removed anyway. 3097fd8329bSPetr Mladek */ 3107fd8329bSPetr Mladek const struct taint_flag taint_flags[TAINT_FLAGS_COUNT] = { 3117fd8329bSPetr Mladek { 'P', 'G', true }, /* TAINT_PROPRIETARY_MODULE */ 3127fd8329bSPetr Mladek { 'F', ' ', true }, /* TAINT_FORCED_MODULE */ 3137fd8329bSPetr Mladek { 'S', ' ', false }, /* TAINT_CPU_OUT_OF_SPEC */ 3147fd8329bSPetr Mladek { 'R', ' ', false }, /* TAINT_FORCED_RMMOD */ 3157fd8329bSPetr Mladek { 'M', ' ', false }, /* TAINT_MACHINE_CHECK */ 3167fd8329bSPetr Mladek { 'B', ' ', false }, /* TAINT_BAD_PAGE */ 3177fd8329bSPetr Mladek { 'U', ' ', false }, /* TAINT_USER */ 3187fd8329bSPetr Mladek { 'D', ' ', false }, /* TAINT_DIE */ 3197fd8329bSPetr Mladek { 'A', ' ', false }, /* TAINT_OVERRIDDEN_ACPI_TABLE */ 3207fd8329bSPetr Mladek { 'W', ' ', false }, /* TAINT_WARN */ 3217fd8329bSPetr Mladek { 'C', ' ', true }, /* TAINT_CRAP */ 3227fd8329bSPetr Mladek { 'I', ' ', false }, /* TAINT_FIRMWARE_WORKAROUND */ 3237fd8329bSPetr Mladek { 'O', ' ', true }, /* TAINT_OOT_MODULE */ 3247fd8329bSPetr Mladek { 'E', ' ', true }, /* TAINT_UNSIGNED_MODULE */ 3257fd8329bSPetr Mladek { 'L', ' ', false }, /* TAINT_SOFTLOCKUP */ 3267fd8329bSPetr Mladek { 'K', ' ', true }, /* TAINT_LIVEPATCH */ 3274efb442cSBorislav Petkov { 'X', ' ', true }, /* TAINT_AUX */ 32825ddbb18SAndi Kleen }; 32925ddbb18SAndi Kleen 3301da177e4SLinus Torvalds /** 3311da177e4SLinus Torvalds * print_tainted - return a string to represent the kernel taint state. 3321da177e4SLinus Torvalds * 3331da177e4SLinus Torvalds * 'P' - Proprietary module has been loaded. 3341da177e4SLinus Torvalds * 'F' - Module has been forcibly loaded. 3351da177e4SLinus Torvalds * 'S' - SMP with CPUs not designed for SMP. 3361da177e4SLinus Torvalds * 'R' - User forced a module unload. 3379aa5e993SDaniel Roesen * 'M' - System experienced a machine check exception. 3381da177e4SLinus Torvalds * 'B' - System has hit bad_page. 33934f5a398STheodore Ts'o * 'U' - Userspace-defined naughtiness. 340a8005992SArjan van de Ven * 'D' - Kernel has oopsed before 34195b570c9SNur Hussein * 'A' - ACPI table overridden. 34295b570c9SNur Hussein * 'W' - Taint on warning. 343061b1bd3SGreg Kroah-Hartman * 'C' - modules from drivers/staging are loaded. 34492946bc7SBen Hutchings * 'I' - Working around severe firmware bug. 3452449b8baSBen Hutchings * 'O' - Out-of-tree module has been loaded. 34657673c2bSRusty Russell * 'E' - Unsigned module has been loaded. 347bc53a3f4SXie XiuQi * 'L' - A soft lockup has previously occurred. 348c5f45465SSeth Jennings * 'K' - Kernel has been live patched. 3494efb442cSBorislav Petkov * 'X' - Auxiliary taint, for distros' use. 3501da177e4SLinus Torvalds * 351fe002a41SRobert P. J. Day * The string is overwritten by the next call to print_tainted(). 3521da177e4SLinus Torvalds */ 3531da177e4SLinus Torvalds const char *print_tainted(void) 3541da177e4SLinus Torvalds { 3557fd8329bSPetr Mladek static char buf[TAINT_FLAGS_COUNT + sizeof("Tainted: ")]; 35625ddbb18SAndi Kleen 35725ddbb18SAndi Kleen if (tainted_mask) { 35825ddbb18SAndi Kleen char *s; 35925ddbb18SAndi Kleen int i; 36025ddbb18SAndi Kleen 36125ddbb18SAndi Kleen s = buf + sprintf(buf, "Tainted: "); 3627fd8329bSPetr Mladek for (i = 0; i < TAINT_FLAGS_COUNT; i++) { 3637fd8329bSPetr Mladek const struct taint_flag *t = &taint_flags[i]; 3647fd8329bSPetr Mladek *s++ = test_bit(i, &tainted_mask) ? 3655eb7c0d0SLarry Finger t->c_true : t->c_false; 3661da177e4SLinus Torvalds } 36725ddbb18SAndi Kleen *s = 0; 36825ddbb18SAndi Kleen } else 3691da177e4SLinus Torvalds snprintf(buf, sizeof(buf), "Not tainted"); 370c95dbf27SIngo Molnar 371c95dbf27SIngo Molnar return buf; 3721da177e4SLinus Torvalds } 3731da177e4SLinus Torvalds 37425ddbb18SAndi Kleen int test_taint(unsigned flag) 37525ddbb18SAndi Kleen { 37625ddbb18SAndi Kleen return test_bit(flag, &tainted_mask); 37725ddbb18SAndi Kleen } 37825ddbb18SAndi Kleen EXPORT_SYMBOL(test_taint); 37925ddbb18SAndi Kleen 38025ddbb18SAndi Kleen unsigned long get_taint(void) 38125ddbb18SAndi Kleen { 38225ddbb18SAndi Kleen return tainted_mask; 38325ddbb18SAndi Kleen } 38425ddbb18SAndi Kleen 385373d4d09SRusty Russell /** 386373d4d09SRusty Russell * add_taint: add a taint flag if not already set. 387373d4d09SRusty Russell * @flag: one of the TAINT_* constants. 388373d4d09SRusty Russell * @lockdep_ok: whether lock debugging is still OK. 389373d4d09SRusty Russell * 390373d4d09SRusty Russell * If something bad has gone wrong, you'll want @lockdebug_ok = false, but for 391373d4d09SRusty Russell * some notewortht-but-not-corrupting cases, it can be set to true. 3929eeba613SFrederic Weisbecker */ 393373d4d09SRusty Russell void add_taint(unsigned flag, enum lockdep_ok lockdep_ok) 394373d4d09SRusty Russell { 395373d4d09SRusty Russell if (lockdep_ok == LOCKDEP_NOW_UNRELIABLE && __debug_locks_off()) 396d7c0847fSFabian Frederick pr_warn("Disabling lock debugging due to kernel taint\n"); 3979eeba613SFrederic Weisbecker 39825ddbb18SAndi Kleen set_bit(flag, &tainted_mask); 3991da177e4SLinus Torvalds } 4001da177e4SLinus Torvalds EXPORT_SYMBOL(add_taint); 401dd287796SAndrew Morton 402dd287796SAndrew Morton static void spin_msec(int msecs) 403dd287796SAndrew Morton { 404dd287796SAndrew Morton int i; 405dd287796SAndrew Morton 406dd287796SAndrew Morton for (i = 0; i < msecs; i++) { 407dd287796SAndrew Morton touch_nmi_watchdog(); 408dd287796SAndrew Morton mdelay(1); 409dd287796SAndrew Morton } 410dd287796SAndrew Morton } 411dd287796SAndrew Morton 412dd287796SAndrew Morton /* 413dd287796SAndrew Morton * It just happens that oops_enter() and oops_exit() are identically 414dd287796SAndrew Morton * implemented... 415dd287796SAndrew Morton */ 416dd287796SAndrew Morton static void do_oops_enter_exit(void) 417dd287796SAndrew Morton { 418dd287796SAndrew Morton unsigned long flags; 419dd287796SAndrew Morton static int spin_counter; 420dd287796SAndrew Morton 421dd287796SAndrew Morton if (!pause_on_oops) 422dd287796SAndrew Morton return; 423dd287796SAndrew Morton 424dd287796SAndrew Morton spin_lock_irqsave(&pause_on_oops_lock, flags); 425dd287796SAndrew Morton if (pause_on_oops_flag == 0) { 426dd287796SAndrew Morton /* This CPU may now print the oops message */ 427dd287796SAndrew Morton pause_on_oops_flag = 1; 428dd287796SAndrew Morton } else { 429dd287796SAndrew Morton /* We need to stall this CPU */ 430dd287796SAndrew Morton if (!spin_counter) { 431dd287796SAndrew Morton /* This CPU gets to do the counting */ 432dd287796SAndrew Morton spin_counter = pause_on_oops; 433dd287796SAndrew Morton do { 434dd287796SAndrew Morton spin_unlock(&pause_on_oops_lock); 435dd287796SAndrew Morton spin_msec(MSEC_PER_SEC); 436dd287796SAndrew Morton spin_lock(&pause_on_oops_lock); 437dd287796SAndrew Morton } while (--spin_counter); 438dd287796SAndrew Morton pause_on_oops_flag = 0; 439dd287796SAndrew Morton } else { 440dd287796SAndrew Morton /* This CPU waits for a different one */ 441dd287796SAndrew Morton while (spin_counter) { 442dd287796SAndrew Morton spin_unlock(&pause_on_oops_lock); 443dd287796SAndrew Morton spin_msec(1); 444dd287796SAndrew Morton spin_lock(&pause_on_oops_lock); 445dd287796SAndrew Morton } 446dd287796SAndrew Morton } 447dd287796SAndrew Morton } 448dd287796SAndrew Morton spin_unlock_irqrestore(&pause_on_oops_lock, flags); 449dd287796SAndrew Morton } 450dd287796SAndrew Morton 451dd287796SAndrew Morton /* 452c95dbf27SIngo Molnar * Return true if the calling CPU is allowed to print oops-related info. 453c95dbf27SIngo Molnar * This is a bit racy.. 454dd287796SAndrew Morton */ 455dd287796SAndrew Morton int oops_may_print(void) 456dd287796SAndrew Morton { 457dd287796SAndrew Morton return pause_on_oops_flag == 0; 458dd287796SAndrew Morton } 459dd287796SAndrew Morton 460dd287796SAndrew Morton /* 461dd287796SAndrew Morton * Called when the architecture enters its oops handler, before it prints 462c95dbf27SIngo Molnar * anything. If this is the first CPU to oops, and it's oopsing the first 463c95dbf27SIngo Molnar * time then let it proceed. 464dd287796SAndrew Morton * 465c95dbf27SIngo Molnar * This is all enabled by the pause_on_oops kernel boot option. We do all 466c95dbf27SIngo Molnar * this to ensure that oopses don't scroll off the screen. It has the 467c95dbf27SIngo Molnar * side-effect of preventing later-oopsing CPUs from mucking up the display, 468c95dbf27SIngo Molnar * too. 469dd287796SAndrew Morton * 470c95dbf27SIngo Molnar * It turns out that the CPU which is allowed to print ends up pausing for 471c95dbf27SIngo Molnar * the right duration, whereas all the other CPUs pause for twice as long: 472c95dbf27SIngo Molnar * once in oops_enter(), once in oops_exit(). 473dd287796SAndrew Morton */ 474dd287796SAndrew Morton void oops_enter(void) 475dd287796SAndrew Morton { 476bdff7870SThomas Gleixner tracing_off(); 477c95dbf27SIngo Molnar /* can't trust the integrity of the kernel anymore: */ 478c95dbf27SIngo Molnar debug_locks_off(); 479dd287796SAndrew Morton do_oops_enter_exit(); 480dd287796SAndrew Morton } 481dd287796SAndrew Morton 482dd287796SAndrew Morton /* 4832c3b20e9SArjan van de Ven * 64-bit random ID for oopses: 4842c3b20e9SArjan van de Ven */ 4852c3b20e9SArjan van de Ven static u64 oops_id; 4862c3b20e9SArjan van de Ven 4872c3b20e9SArjan van de Ven static int init_oops_id(void) 4882c3b20e9SArjan van de Ven { 4892c3b20e9SArjan van de Ven if (!oops_id) 4902c3b20e9SArjan van de Ven get_random_bytes(&oops_id, sizeof(oops_id)); 491d6624f99SArjan van de Ven else 492d6624f99SArjan van de Ven oops_id++; 4932c3b20e9SArjan van de Ven 4942c3b20e9SArjan van de Ven return 0; 4952c3b20e9SArjan van de Ven } 4962c3b20e9SArjan van de Ven late_initcall(init_oops_id); 4972c3b20e9SArjan van de Ven 498863a6049SAnton Blanchard void print_oops_end_marker(void) 49971c33911SArjan van de Ven { 50071c33911SArjan van de Ven init_oops_id(); 501d7c0847fSFabian Frederick pr_warn("---[ end trace %016llx ]---\n", (unsigned long long)oops_id); 50271c33911SArjan van de Ven } 50371c33911SArjan van de Ven 5042c3b20e9SArjan van de Ven /* 505dd287796SAndrew Morton * Called when the architecture exits its oops handler, after printing 506dd287796SAndrew Morton * everything. 507dd287796SAndrew Morton */ 508dd287796SAndrew Morton void oops_exit(void) 509dd287796SAndrew Morton { 510dd287796SAndrew Morton do_oops_enter_exit(); 51171c33911SArjan van de Ven print_oops_end_marker(); 512456b565cSSimon Kagstrom kmsg_dump(KMSG_DUMP_OOPS); 513dd287796SAndrew Morton } 5143162f751SArjan van de Ven 5152553b67aSJosh Poimboeuf struct warn_args { 5160f6f49a8SLinus Torvalds const char *fmt; 517a8f18b90SArjan van de Ven va_list args; 5180f6f49a8SLinus Torvalds }; 5190f6f49a8SLinus Torvalds 5202553b67aSJosh Poimboeuf void __warn(const char *file, int line, void *caller, unsigned taint, 5212553b67aSJosh Poimboeuf struct pt_regs *regs, struct warn_args *args) 5220f6f49a8SLinus Torvalds { 523de7edd31SSteven Rostedt (Red Hat) disable_trace_on_warning(); 524de7edd31SSteven Rostedt (Red Hat) 525a7bed27aSKees Cook if (args) 5262a8358d8SKees Cook pr_warn(CUT_HERE); 5272553b67aSJosh Poimboeuf 5282553b67aSJosh Poimboeuf if (file) 5292553b67aSJosh Poimboeuf pr_warn("WARNING: CPU: %d PID: %d at %s:%d %pS\n", 5302553b67aSJosh Poimboeuf raw_smp_processor_id(), current->pid, file, line, 5312553b67aSJosh Poimboeuf caller); 5322553b67aSJosh Poimboeuf else 5332553b67aSJosh Poimboeuf pr_warn("WARNING: CPU: %d PID: %d at %pS\n", 5342553b67aSJosh Poimboeuf raw_smp_processor_id(), current->pid, caller); 53574853dbaSArjan van de Ven 5360f6f49a8SLinus Torvalds if (args) 5370f6f49a8SLinus Torvalds vprintk(args->fmt, args->args); 538a8f18b90SArjan van de Ven 5399e3961a0SPrarit Bhargava if (panic_on_warn) { 5409e3961a0SPrarit Bhargava /* 5419e3961a0SPrarit Bhargava * This thread may hit another WARN() in the panic path. 5429e3961a0SPrarit Bhargava * Resetting this prevents additional WARN() from panicking the 5439e3961a0SPrarit Bhargava * system on this thread. Other threads are blocked by the 5449e3961a0SPrarit Bhargava * panic_mutex in panic(). 5459e3961a0SPrarit Bhargava */ 5469e3961a0SPrarit Bhargava panic_on_warn = 0; 5479e3961a0SPrarit Bhargava panic("panic_on_warn set ...\n"); 5489e3961a0SPrarit Bhargava } 5499e3961a0SPrarit Bhargava 550a8f18b90SArjan van de Ven print_modules(); 5512553b67aSJosh Poimboeuf 5522553b67aSJosh Poimboeuf if (regs) 5532553b67aSJosh Poimboeuf show_regs(regs); 5542553b67aSJosh Poimboeuf else 555a8f18b90SArjan van de Ven dump_stack(); 5562553b67aSJosh Poimboeuf 557a8f18b90SArjan van de Ven print_oops_end_marker(); 5582553b67aSJosh Poimboeuf 559373d4d09SRusty Russell /* Just a warning, don't kill lockdep. */ 560373d4d09SRusty Russell add_taint(taint, LOCKDEP_STILL_OK); 561a8f18b90SArjan van de Ven } 5620f6f49a8SLinus Torvalds 5632553b67aSJosh Poimboeuf #ifdef WANT_WARN_ON_SLOWPATH 5640f6f49a8SLinus Torvalds void warn_slowpath_fmt(const char *file, int line, const char *fmt, ...) 5650f6f49a8SLinus Torvalds { 5662553b67aSJosh Poimboeuf struct warn_args args; 5670f6f49a8SLinus Torvalds 5680f6f49a8SLinus Torvalds args.fmt = fmt; 5690f6f49a8SLinus Torvalds va_start(args.args, fmt); 5702553b67aSJosh Poimboeuf __warn(file, line, __builtin_return_address(0), TAINT_WARN, NULL, 5712553b67aSJosh Poimboeuf &args); 5720f6f49a8SLinus Torvalds va_end(args.args); 5730f6f49a8SLinus Torvalds } 57457adc4d2SAndi Kleen EXPORT_SYMBOL(warn_slowpath_fmt); 57557adc4d2SAndi Kleen 576b2be0527SBen Hutchings void warn_slowpath_fmt_taint(const char *file, int line, 577b2be0527SBen Hutchings unsigned taint, const char *fmt, ...) 578b2be0527SBen Hutchings { 5792553b67aSJosh Poimboeuf struct warn_args args; 580b2be0527SBen Hutchings 581b2be0527SBen Hutchings args.fmt = fmt; 582b2be0527SBen Hutchings va_start(args.args, fmt); 5832553b67aSJosh Poimboeuf __warn(file, line, __builtin_return_address(0), taint, NULL, &args); 584b2be0527SBen Hutchings va_end(args.args); 585b2be0527SBen Hutchings } 586b2be0527SBen Hutchings EXPORT_SYMBOL(warn_slowpath_fmt_taint); 587b2be0527SBen Hutchings 58857adc4d2SAndi Kleen void warn_slowpath_null(const char *file, int line) 58957adc4d2SAndi Kleen { 590a7bed27aSKees Cook pr_warn(CUT_HERE); 5912553b67aSJosh Poimboeuf __warn(file, line, __builtin_return_address(0), TAINT_WARN, NULL, NULL); 59257adc4d2SAndi Kleen } 59357adc4d2SAndi Kleen EXPORT_SYMBOL(warn_slowpath_null); 594a7bed27aSKees Cook #else 595a7bed27aSKees Cook void __warn_printk(const char *fmt, ...) 596a7bed27aSKees Cook { 597a7bed27aSKees Cook va_list args; 598a7bed27aSKees Cook 599a7bed27aSKees Cook pr_warn(CUT_HERE); 600a7bed27aSKees Cook 601a7bed27aSKees Cook va_start(args, fmt); 602a7bed27aSKees Cook vprintk(fmt, args); 603a7bed27aSKees Cook va_end(args); 604a7bed27aSKees Cook } 605a7bed27aSKees Cook EXPORT_SYMBOL(__warn_printk); 60679b4cc5eSArjan van de Ven #endif 60779b4cc5eSArjan van de Ven 608b1fca27dSAndi Kleen #ifdef CONFIG_BUG 609b1fca27dSAndi Kleen 610b1fca27dSAndi Kleen /* Support resetting WARN*_ONCE state */ 611b1fca27dSAndi Kleen 612b1fca27dSAndi Kleen static int clear_warn_once_set(void *data, u64 val) 613b1fca27dSAndi Kleen { 614aaf5dcfbSAndi Kleen generic_bug_clear_once(); 615b1fca27dSAndi Kleen memset(__start_once, 0, __end_once - __start_once); 616b1fca27dSAndi Kleen return 0; 617b1fca27dSAndi Kleen } 618b1fca27dSAndi Kleen 619b1fca27dSAndi Kleen DEFINE_SIMPLE_ATTRIBUTE(clear_warn_once_fops, 620b1fca27dSAndi Kleen NULL, 621b1fca27dSAndi Kleen clear_warn_once_set, 622b1fca27dSAndi Kleen "%lld\n"); 623b1fca27dSAndi Kleen 624b1fca27dSAndi Kleen static __init int register_warn_debugfs(void) 625b1fca27dSAndi Kleen { 626b1fca27dSAndi Kleen /* Don't care about failure */ 627aaf5dcfbSAndi Kleen debugfs_create_file("clear_warn_once", 0200, NULL, 628b1fca27dSAndi Kleen NULL, &clear_warn_once_fops); 629b1fca27dSAndi Kleen return 0; 630b1fca27dSAndi Kleen } 631b1fca27dSAndi Kleen 632b1fca27dSAndi Kleen device_initcall(register_warn_debugfs); 633b1fca27dSAndi Kleen #endif 634b1fca27dSAndi Kleen 6353162f751SArjan van de Ven #ifdef CONFIG_CC_STACKPROTECTOR 63654371a43SArjan van de Ven 6373162f751SArjan van de Ven /* 6383162f751SArjan van de Ven * Called when gcc's -fstack-protector feature is used, and 6393162f751SArjan van de Ven * gcc detects corruption of the on-stack canary value 6403162f751SArjan van de Ven */ 641a7330c99SAndi Kleen __visible void __stack_chk_fail(void) 6423162f751SArjan van de Ven { 643*0862ca42SKees Cook panic("stack-protector: Kernel stack is corrupted in: %pB\n", 644517a92c4SIngo Molnar __builtin_return_address(0)); 6453162f751SArjan van de Ven } 6463162f751SArjan van de Ven EXPORT_SYMBOL(__stack_chk_fail); 64754371a43SArjan van de Ven 6483162f751SArjan van de Ven #endif 649f44dd164SRusty Russell 6507a46ec0eSKees Cook #ifdef CONFIG_ARCH_HAS_REFCOUNT 6517a46ec0eSKees Cook void refcount_error_report(struct pt_regs *regs, const char *err) 6527a46ec0eSKees Cook { 6537a46ec0eSKees Cook WARN_RATELIMIT(1, "refcount_t %s at %pB in %s[%d], uid/euid: %u/%u\n", 6547a46ec0eSKees Cook err, (void *)instruction_pointer(regs), 6557a46ec0eSKees Cook current->comm, task_pid_nr(current), 6567a46ec0eSKees Cook from_kuid_munged(&init_user_ns, current_uid()), 6577a46ec0eSKees Cook from_kuid_munged(&init_user_ns, current_euid())); 6587a46ec0eSKees Cook } 6597a46ec0eSKees Cook #endif 6607a46ec0eSKees Cook 661f44dd164SRusty Russell core_param(panic, panic_timeout, int, 0644); 662f44dd164SRusty Russell core_param(pause_on_oops, pause_on_oops, int, 0644); 6639e3961a0SPrarit Bhargava core_param(panic_on_warn, panic_on_warn, int, 0644); 664b26e27ddSHidehiro Kawai core_param(crash_kexec_post_notifiers, crash_kexec_post_notifiers, bool, 0644); 665f06e5153SMasami Hiramatsu 666d404ab0aSOlaf Hering static int __init oops_setup(char *s) 667d404ab0aSOlaf Hering { 668d404ab0aSOlaf Hering if (!s) 669d404ab0aSOlaf Hering return -EINVAL; 670d404ab0aSOlaf Hering if (!strcmp(s, "panic")) 671d404ab0aSOlaf Hering panic_on_oops = 1; 672d404ab0aSOlaf Hering return 0; 673d404ab0aSOlaf Hering } 674d404ab0aSOlaf Hering early_param("oops", oops_setup); 675