xref: /linux-6.15/kernel/panic.c (revision 7a46ec0e)
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>
29*7a46ec0eSKees Cook #include <linux/ratelimit.h>
301da177e4SLinus Torvalds 
31c7ff0d9cSTAMUKI Shoichi #define PANIC_TIMER_STEP 100
32c7ff0d9cSTAMUKI Shoichi #define PANIC_BLINK_SPD 18
33c7ff0d9cSTAMUKI Shoichi 
342a01bb38SKyle McMartin int panic_on_oops = CONFIG_PANIC_ON_OOPS_VALUE;
3525ddbb18SAndi Kleen static unsigned long tainted_mask;
36dd287796SAndrew Morton static int pause_on_oops;
37dd287796SAndrew Morton static int pause_on_oops_flag;
38dd287796SAndrew Morton static DEFINE_SPINLOCK(pause_on_oops_lock);
395375b708SHATAYAMA Daisuke bool crash_kexec_post_notifiers;
409e3961a0SPrarit Bhargava int panic_on_warn __read_mostly;
411da177e4SLinus Torvalds 
425800dc3cSJason Baron int panic_timeout = CONFIG_PANIC_TIMEOUT;
4381e88fdcSHuang Ying EXPORT_SYMBOL_GPL(panic_timeout);
441da177e4SLinus Torvalds 
45e041c683SAlan Stern ATOMIC_NOTIFIER_HEAD(panic_notifier_list);
461da177e4SLinus Torvalds 
471da177e4SLinus Torvalds EXPORT_SYMBOL(panic_notifier_list);
481da177e4SLinus Torvalds 
49c7ff0d9cSTAMUKI Shoichi static long no_blink(int state)
508aeee85aSAnton Blanchard {
51c7ff0d9cSTAMUKI Shoichi 	return 0;
52c7ff0d9cSTAMUKI Shoichi }
538aeee85aSAnton Blanchard 
54c7ff0d9cSTAMUKI Shoichi /* Returns how long it waited in ms */
55c7ff0d9cSTAMUKI Shoichi long (*panic_blink)(int state);
56c7ff0d9cSTAMUKI Shoichi EXPORT_SYMBOL(panic_blink);
578aeee85aSAnton Blanchard 
5893e13a36SMichael Holzheu /*
5993e13a36SMichael Holzheu  * Stop ourself in panic -- architecture code may override this
6093e13a36SMichael Holzheu  */
6193e13a36SMichael Holzheu void __weak panic_smp_self_stop(void)
6293e13a36SMichael Holzheu {
6393e13a36SMichael Holzheu 	while (1)
6493e13a36SMichael Holzheu 		cpu_relax();
6593e13a36SMichael Holzheu }
6693e13a36SMichael Holzheu 
6758c5661fSHidehiro Kawai /*
6858c5661fSHidehiro Kawai  * Stop ourselves in NMI context if another CPU has already panicked. Arch code
6958c5661fSHidehiro Kawai  * may override this to prepare for crash dumping, e.g. save regs info.
7058c5661fSHidehiro Kawai  */
7158c5661fSHidehiro Kawai void __weak nmi_panic_self_stop(struct pt_regs *regs)
7258c5661fSHidehiro Kawai {
7358c5661fSHidehiro Kawai 	panic_smp_self_stop();
7458c5661fSHidehiro Kawai }
7558c5661fSHidehiro Kawai 
760ee59413SHidehiro Kawai /*
770ee59413SHidehiro Kawai  * Stop other CPUs in panic.  Architecture dependent code may override this
780ee59413SHidehiro Kawai  * with more suitable version.  For example, if the architecture supports
790ee59413SHidehiro Kawai  * crash dump, it should save registers of each stopped CPU and disable
800ee59413SHidehiro Kawai  * per-CPU features such as virtualization extensions.
810ee59413SHidehiro Kawai  */
820ee59413SHidehiro Kawai void __weak crash_smp_send_stop(void)
830ee59413SHidehiro Kawai {
840ee59413SHidehiro Kawai 	static int cpus_stopped;
850ee59413SHidehiro Kawai 
860ee59413SHidehiro Kawai 	/*
870ee59413SHidehiro Kawai 	 * This function can be called twice in panic path, but obviously
880ee59413SHidehiro Kawai 	 * we execute this only once.
890ee59413SHidehiro Kawai 	 */
900ee59413SHidehiro Kawai 	if (cpus_stopped)
910ee59413SHidehiro Kawai 		return;
920ee59413SHidehiro Kawai 
930ee59413SHidehiro Kawai 	/*
940ee59413SHidehiro Kawai 	 * Note smp_send_stop is the usual smp shutdown function, which
950ee59413SHidehiro Kawai 	 * unfortunately means it may not be hardened to work in a panic
960ee59413SHidehiro Kawai 	 * situation.
970ee59413SHidehiro Kawai 	 */
980ee59413SHidehiro Kawai 	smp_send_stop();
990ee59413SHidehiro Kawai 	cpus_stopped = 1;
1000ee59413SHidehiro Kawai }
1010ee59413SHidehiro Kawai 
1021717f209SHidehiro Kawai atomic_t panic_cpu = ATOMIC_INIT(PANIC_CPU_INVALID);
1031717f209SHidehiro Kawai 
104ebc41f20SHidehiro Kawai /*
105ebc41f20SHidehiro Kawai  * A variant of panic() called from NMI context. We return if we've already
106ebc41f20SHidehiro Kawai  * panicked on this CPU. If another CPU already panicked, loop in
107ebc41f20SHidehiro Kawai  * nmi_panic_self_stop() which can provide architecture dependent code such
108ebc41f20SHidehiro Kawai  * as saving register state for crash dump.
109ebc41f20SHidehiro Kawai  */
110ebc41f20SHidehiro Kawai void nmi_panic(struct pt_regs *regs, const char *msg)
111ebc41f20SHidehiro Kawai {
112ebc41f20SHidehiro Kawai 	int old_cpu, cpu;
113ebc41f20SHidehiro Kawai 
114ebc41f20SHidehiro Kawai 	cpu = raw_smp_processor_id();
115ebc41f20SHidehiro Kawai 	old_cpu = atomic_cmpxchg(&panic_cpu, PANIC_CPU_INVALID, cpu);
116ebc41f20SHidehiro Kawai 
117ebc41f20SHidehiro Kawai 	if (old_cpu == PANIC_CPU_INVALID)
118ebc41f20SHidehiro Kawai 		panic("%s", msg);
119ebc41f20SHidehiro Kawai 	else if (old_cpu != cpu)
120ebc41f20SHidehiro Kawai 		nmi_panic_self_stop(regs);
121ebc41f20SHidehiro Kawai }
122ebc41f20SHidehiro Kawai EXPORT_SYMBOL(nmi_panic);
123ebc41f20SHidehiro Kawai 
1241da177e4SLinus Torvalds /**
1251da177e4SLinus Torvalds  *	panic - halt the system
1261da177e4SLinus Torvalds  *	@fmt: The text string to print
1271da177e4SLinus Torvalds  *
1281da177e4SLinus Torvalds  *	Display a message, then perform cleanups.
1291da177e4SLinus Torvalds  *
1301da177e4SLinus Torvalds  *	This function never returns.
1311da177e4SLinus Torvalds  */
1329402c95fSJoe Perches void panic(const char *fmt, ...)
1331da177e4SLinus Torvalds {
1341da177e4SLinus Torvalds 	static char buf[1024];
1351da177e4SLinus Torvalds 	va_list args;
136c7ff0d9cSTAMUKI Shoichi 	long i, i_next = 0;
137c7ff0d9cSTAMUKI Shoichi 	int state = 0;
1381717f209SHidehiro Kawai 	int old_cpu, this_cpu;
139b26e27ddSHidehiro Kawai 	bool _crash_kexec_post_notifiers = crash_kexec_post_notifiers;
1401da177e4SLinus Torvalds 
141dc009d92SEric W. Biederman 	/*
142190320c3SVikram Mulukutla 	 * Disable local interrupts. This will prevent panic_smp_self_stop
143190320c3SVikram Mulukutla 	 * from deadlocking the first cpu that invokes the panic, since
144190320c3SVikram Mulukutla 	 * there is nothing to prevent an interrupt handler (that runs
1451717f209SHidehiro Kawai 	 * after setting panic_cpu) from invoking panic() again.
146190320c3SVikram Mulukutla 	 */
147190320c3SVikram Mulukutla 	local_irq_disable();
148190320c3SVikram Mulukutla 
149190320c3SVikram Mulukutla 	/*
150c95dbf27SIngo Molnar 	 * It's possible to come here directly from a panic-assertion and
151c95dbf27SIngo Molnar 	 * not have preempt disabled. Some functions called from here want
152dc009d92SEric W. Biederman 	 * preempt to be disabled. No point enabling it later though...
15393e13a36SMichael Holzheu 	 *
15493e13a36SMichael Holzheu 	 * Only one CPU is allowed to execute the panic code from here. For
15593e13a36SMichael Holzheu 	 * multiple parallel invocations of panic, all other CPUs either
15693e13a36SMichael Holzheu 	 * stop themself or will wait until they are stopped by the 1st CPU
15793e13a36SMichael Holzheu 	 * with smp_send_stop().
1581717f209SHidehiro Kawai 	 *
1591717f209SHidehiro Kawai 	 * `old_cpu == PANIC_CPU_INVALID' means this is the 1st CPU which
1601717f209SHidehiro Kawai 	 * comes here, so go ahead.
1611717f209SHidehiro Kawai 	 * `old_cpu == this_cpu' means we came from nmi_panic() which sets
1621717f209SHidehiro Kawai 	 * panic_cpu to this CPU.  In this case, this is also the 1st CPU.
163dc009d92SEric W. Biederman 	 */
1641717f209SHidehiro Kawai 	this_cpu = raw_smp_processor_id();
1651717f209SHidehiro Kawai 	old_cpu  = atomic_cmpxchg(&panic_cpu, PANIC_CPU_INVALID, this_cpu);
1661717f209SHidehiro Kawai 
1671717f209SHidehiro Kawai 	if (old_cpu != PANIC_CPU_INVALID && old_cpu != this_cpu)
16893e13a36SMichael Holzheu 		panic_smp_self_stop();
169dc009d92SEric W. Biederman 
1705b530fc1SAnton Blanchard 	console_verbose();
1711da177e4SLinus Torvalds 	bust_spinlocks(1);
1721da177e4SLinus Torvalds 	va_start(args, fmt);
1731da177e4SLinus Torvalds 	vsnprintf(buf, sizeof(buf), fmt, args);
1741da177e4SLinus Torvalds 	va_end(args);
175d7c0847fSFabian Frederick 	pr_emerg("Kernel panic - not syncing: %s\n", buf);
1765cb27301SIngo Molnar #ifdef CONFIG_DEBUG_BUGVERBOSE
1776e6f0a1fSAndi Kleen 	/*
1786e6f0a1fSAndi Kleen 	 * Avoid nested stack-dumping if a panic occurs during oops processing
1796e6f0a1fSAndi Kleen 	 */
180026ee1f6SJason Wessel 	if (!test_taint(TAINT_DIE) && oops_in_progress <= 1)
1815cb27301SIngo Molnar 		dump_stack();
1825cb27301SIngo Molnar #endif
1831da177e4SLinus Torvalds 
184dc009d92SEric W. Biederman 	/*
185dc009d92SEric W. Biederman 	 * If we have crashed and we have a crash kernel loaded let it handle
186dc009d92SEric W. Biederman 	 * everything else.
187f06e5153SMasami Hiramatsu 	 * If we want to run this after calling panic_notifiers, pass
188f06e5153SMasami Hiramatsu 	 * the "crash_kexec_post_notifiers" option to the kernel.
1897bbee5caSHidehiro Kawai 	 *
1907bbee5caSHidehiro Kawai 	 * Bypass the panic_cpu check and call __crash_kexec directly.
191dc009d92SEric W. Biederman 	 */
192b26e27ddSHidehiro Kawai 	if (!_crash_kexec_post_notifiers) {
193f92bac3bSSergey Senozhatsky 		printk_safe_flush_on_panic();
1947bbee5caSHidehiro Kawai 		__crash_kexec(NULL);
195dc009d92SEric W. Biederman 
196dc009d92SEric W. Biederman 		/*
197dc009d92SEric W. Biederman 		 * Note smp_send_stop is the usual smp shutdown function, which
1980ee59413SHidehiro Kawai 		 * unfortunately means it may not be hardened to work in a
1990ee59413SHidehiro Kawai 		 * panic situation.
200dc009d92SEric W. Biederman 		 */
2011da177e4SLinus Torvalds 		smp_send_stop();
2020ee59413SHidehiro Kawai 	} else {
2030ee59413SHidehiro Kawai 		/*
2040ee59413SHidehiro Kawai 		 * If we want to do crash dump after notifier calls and
2050ee59413SHidehiro Kawai 		 * kmsg_dump, we will need architecture dependent extra
2060ee59413SHidehiro Kawai 		 * works in addition to stopping other CPUs.
2070ee59413SHidehiro Kawai 		 */
2080ee59413SHidehiro Kawai 		crash_smp_send_stop();
2090ee59413SHidehiro Kawai 	}
2101da177e4SLinus Torvalds 
2116723734cSKees Cook 	/*
2126723734cSKees Cook 	 * Run any panic handlers, including those that might need to
2136723734cSKees Cook 	 * add information to the kmsg dump output.
2146723734cSKees Cook 	 */
215e041c683SAlan Stern 	atomic_notifier_call_chain(&panic_notifier_list, 0, buf);
2161da177e4SLinus Torvalds 
217cf9b1106SPetr Mladek 	/* Call flush even twice. It tries harder with a single online CPU */
218f92bac3bSSergey Senozhatsky 	printk_safe_flush_on_panic();
2196723734cSKees Cook 	kmsg_dump(KMSG_DUMP_PANIC);
2206723734cSKees Cook 
221f06e5153SMasami Hiramatsu 	/*
222f06e5153SMasami Hiramatsu 	 * If you doubt kdump always works fine in any situation,
223f06e5153SMasami Hiramatsu 	 * "crash_kexec_post_notifiers" offers you a chance to run
224f06e5153SMasami Hiramatsu 	 * panic_notifiers and dumping kmsg before kdump.
225f06e5153SMasami Hiramatsu 	 * Note: since some panic_notifiers can make crashed kernel
226f06e5153SMasami Hiramatsu 	 * more unstable, it can increase risks of the kdump failure too.
2277bbee5caSHidehiro Kawai 	 *
2287bbee5caSHidehiro Kawai 	 * Bypass the panic_cpu check and call __crash_kexec directly.
229f06e5153SMasami Hiramatsu 	 */
230b26e27ddSHidehiro Kawai 	if (_crash_kexec_post_notifiers)
2317bbee5caSHidehiro Kawai 		__crash_kexec(NULL);
232f06e5153SMasami Hiramatsu 
233d014e889SAaro Koskinen 	bust_spinlocks(0);
234d014e889SAaro Koskinen 
23508d78658SVitaly Kuznetsov 	/*
23608d78658SVitaly Kuznetsov 	 * We may have ended up stopping the CPU holding the lock (in
23708d78658SVitaly Kuznetsov 	 * smp_send_stop()) while still having some valuable data in the console
23808d78658SVitaly Kuznetsov 	 * buffer.  Try to acquire the lock then release it regardless of the
2397625b3a0SVitaly Kuznetsov 	 * result.  The release will also print the buffers out.  Locks debug
2407625b3a0SVitaly Kuznetsov 	 * should be disabled to avoid reporting bad unlock balance when
2417625b3a0SVitaly Kuznetsov 	 * panic() is not being callled from OOPS.
24208d78658SVitaly Kuznetsov 	 */
2437625b3a0SVitaly Kuznetsov 	debug_locks_off();
2448d91f8b1STejun Heo 	console_flush_on_panic();
24508d78658SVitaly Kuznetsov 
246c7ff0d9cSTAMUKI Shoichi 	if (!panic_blink)
247c7ff0d9cSTAMUKI Shoichi 		panic_blink = no_blink;
248c7ff0d9cSTAMUKI Shoichi 
249dc009d92SEric W. Biederman 	if (panic_timeout > 0) {
2501da177e4SLinus Torvalds 		/*
2511da177e4SLinus Torvalds 		 * Delay timeout seconds before rebooting the machine.
252c95dbf27SIngo Molnar 		 * We can't use the "normal" timers since we just panicked.
2531da177e4SLinus Torvalds 		 */
254ff7a28a0SJiri Slaby 		pr_emerg("Rebooting in %d seconds..\n", panic_timeout);
255c95dbf27SIngo Molnar 
256c7ff0d9cSTAMUKI Shoichi 		for (i = 0; i < panic_timeout * 1000; i += PANIC_TIMER_STEP) {
2571da177e4SLinus Torvalds 			touch_nmi_watchdog();
258c7ff0d9cSTAMUKI Shoichi 			if (i >= i_next) {
259c7ff0d9cSTAMUKI Shoichi 				i += panic_blink(state ^= 1);
260c7ff0d9cSTAMUKI Shoichi 				i_next = i + 3600 / PANIC_BLINK_SPD;
261c7ff0d9cSTAMUKI Shoichi 			}
262c7ff0d9cSTAMUKI Shoichi 			mdelay(PANIC_TIMER_STEP);
2631da177e4SLinus Torvalds 		}
2644302fbc8SHugh Dickins 	}
2654302fbc8SHugh Dickins 	if (panic_timeout != 0) {
266c95dbf27SIngo Molnar 		/*
267c95dbf27SIngo Molnar 		 * This will not be a clean reboot, with everything
2682f048ea8SEric W. Biederman 		 * shutting down.  But if there is a chance of
2692f048ea8SEric W. Biederman 		 * rebooting the system it will be rebooted.
2701da177e4SLinus Torvalds 		 */
2712f048ea8SEric W. Biederman 		emergency_restart();
2721da177e4SLinus Torvalds 	}
2731da177e4SLinus Torvalds #ifdef __sparc__
2741da177e4SLinus Torvalds 	{
2751da177e4SLinus Torvalds 		extern int stop_a_enabled;
276a271c241STom 'spot' Callaway 		/* Make sure the user can actually press Stop-A (L1-A) */
2771da177e4SLinus Torvalds 		stop_a_enabled = 1;
2787db60d05SVijay Kumar 		pr_emerg("Press Stop-A (L1-A) from sun keyboard or send break\n"
2797db60d05SVijay Kumar 			 "twice on console to return to the boot prom\n");
2801da177e4SLinus Torvalds 	}
2811da177e4SLinus Torvalds #endif
282347a8dc3SMartin Schwidefsky #if defined(CONFIG_S390)
283c95dbf27SIngo Molnar 	{
284c95dbf27SIngo Molnar 		unsigned long caller;
285c95dbf27SIngo Molnar 
286c95dbf27SIngo Molnar 		caller = (unsigned long)__builtin_return_address(0);
2871da177e4SLinus Torvalds 		disabled_wait(caller);
288c95dbf27SIngo Molnar 	}
2891da177e4SLinus Torvalds #endif
290d7c0847fSFabian Frederick 	pr_emerg("---[ end Kernel panic - not syncing: %s\n", buf);
2911da177e4SLinus Torvalds 	local_irq_enable();
292c7ff0d9cSTAMUKI Shoichi 	for (i = 0; ; i += PANIC_TIMER_STEP) {
293c22db941SJan Beulich 		touch_softlockup_watchdog();
294c7ff0d9cSTAMUKI Shoichi 		if (i >= i_next) {
295c7ff0d9cSTAMUKI Shoichi 			i += panic_blink(state ^= 1);
296c7ff0d9cSTAMUKI Shoichi 			i_next = i + 3600 / PANIC_BLINK_SPD;
297c7ff0d9cSTAMUKI Shoichi 		}
298c7ff0d9cSTAMUKI Shoichi 		mdelay(PANIC_TIMER_STEP);
2991da177e4SLinus Torvalds 	}
3001da177e4SLinus Torvalds }
3011da177e4SLinus Torvalds 
3021da177e4SLinus Torvalds EXPORT_SYMBOL(panic);
3031da177e4SLinus Torvalds 
3047fd8329bSPetr Mladek /*
3057fd8329bSPetr Mladek  * TAINT_FORCED_RMMOD could be a per-module flag but the module
3067fd8329bSPetr Mladek  * is being removed anyway.
3077fd8329bSPetr Mladek  */
3087fd8329bSPetr Mladek const struct taint_flag taint_flags[TAINT_FLAGS_COUNT] = {
3097fd8329bSPetr Mladek 	{ 'P', 'G', true },	/* TAINT_PROPRIETARY_MODULE */
3107fd8329bSPetr Mladek 	{ 'F', ' ', true },	/* TAINT_FORCED_MODULE */
3117fd8329bSPetr Mladek 	{ 'S', ' ', false },	/* TAINT_CPU_OUT_OF_SPEC */
3127fd8329bSPetr Mladek 	{ 'R', ' ', false },	/* TAINT_FORCED_RMMOD */
3137fd8329bSPetr Mladek 	{ 'M', ' ', false },	/* TAINT_MACHINE_CHECK */
3147fd8329bSPetr Mladek 	{ 'B', ' ', false },	/* TAINT_BAD_PAGE */
3157fd8329bSPetr Mladek 	{ 'U', ' ', false },	/* TAINT_USER */
3167fd8329bSPetr Mladek 	{ 'D', ' ', false },	/* TAINT_DIE */
3177fd8329bSPetr Mladek 	{ 'A', ' ', false },	/* TAINT_OVERRIDDEN_ACPI_TABLE */
3187fd8329bSPetr Mladek 	{ 'W', ' ', false },	/* TAINT_WARN */
3197fd8329bSPetr Mladek 	{ 'C', ' ', true },	/* TAINT_CRAP */
3207fd8329bSPetr Mladek 	{ 'I', ' ', false },	/* TAINT_FIRMWARE_WORKAROUND */
3217fd8329bSPetr Mladek 	{ 'O', ' ', true },	/* TAINT_OOT_MODULE */
3227fd8329bSPetr Mladek 	{ 'E', ' ', true },	/* TAINT_UNSIGNED_MODULE */
3237fd8329bSPetr Mladek 	{ 'L', ' ', false },	/* TAINT_SOFTLOCKUP */
3247fd8329bSPetr Mladek 	{ 'K', ' ', true },	/* TAINT_LIVEPATCH */
32525ddbb18SAndi Kleen };
32625ddbb18SAndi Kleen 
3271da177e4SLinus Torvalds /**
3281da177e4SLinus Torvalds  *	print_tainted - return a string to represent the kernel taint state.
3291da177e4SLinus Torvalds  *
3301da177e4SLinus Torvalds  *  'P' - Proprietary module has been loaded.
3311da177e4SLinus Torvalds  *  'F' - Module has been forcibly loaded.
3321da177e4SLinus Torvalds  *  'S' - SMP with CPUs not designed for SMP.
3331da177e4SLinus Torvalds  *  'R' - User forced a module unload.
3349aa5e993SDaniel Roesen  *  'M' - System experienced a machine check exception.
3351da177e4SLinus Torvalds  *  'B' - System has hit bad_page.
33634f5a398STheodore Ts'o  *  'U' - Userspace-defined naughtiness.
337a8005992SArjan van de Ven  *  'D' - Kernel has oopsed before
33895b570c9SNur Hussein  *  'A' - ACPI table overridden.
33995b570c9SNur Hussein  *  'W' - Taint on warning.
340061b1bd3SGreg Kroah-Hartman  *  'C' - modules from drivers/staging are loaded.
34192946bc7SBen Hutchings  *  'I' - Working around severe firmware bug.
3422449b8baSBen Hutchings  *  'O' - Out-of-tree module has been loaded.
34357673c2bSRusty Russell  *  'E' - Unsigned module has been loaded.
344bc53a3f4SXie XiuQi  *  'L' - A soft lockup has previously occurred.
345c5f45465SSeth Jennings  *  'K' - Kernel has been live patched.
3461da177e4SLinus Torvalds  *
347fe002a41SRobert P. J. Day  *	The string is overwritten by the next call to print_tainted().
3481da177e4SLinus Torvalds  */
3491da177e4SLinus Torvalds const char *print_tainted(void)
3501da177e4SLinus Torvalds {
3517fd8329bSPetr Mladek 	static char buf[TAINT_FLAGS_COUNT + sizeof("Tainted: ")];
35225ddbb18SAndi Kleen 
35325ddbb18SAndi Kleen 	if (tainted_mask) {
35425ddbb18SAndi Kleen 		char *s;
35525ddbb18SAndi Kleen 		int i;
35625ddbb18SAndi Kleen 
35725ddbb18SAndi Kleen 		s = buf + sprintf(buf, "Tainted: ");
3587fd8329bSPetr Mladek 		for (i = 0; i < TAINT_FLAGS_COUNT; i++) {
3597fd8329bSPetr Mladek 			const struct taint_flag *t = &taint_flags[i];
3607fd8329bSPetr Mladek 			*s++ = test_bit(i, &tainted_mask) ?
3615eb7c0d0SLarry Finger 					t->c_true : t->c_false;
3621da177e4SLinus Torvalds 		}
36325ddbb18SAndi Kleen 		*s = 0;
36425ddbb18SAndi Kleen 	} else
3651da177e4SLinus Torvalds 		snprintf(buf, sizeof(buf), "Not tainted");
366c95dbf27SIngo Molnar 
367c95dbf27SIngo Molnar 	return buf;
3681da177e4SLinus Torvalds }
3691da177e4SLinus Torvalds 
37025ddbb18SAndi Kleen int test_taint(unsigned flag)
37125ddbb18SAndi Kleen {
37225ddbb18SAndi Kleen 	return test_bit(flag, &tainted_mask);
37325ddbb18SAndi Kleen }
37425ddbb18SAndi Kleen EXPORT_SYMBOL(test_taint);
37525ddbb18SAndi Kleen 
37625ddbb18SAndi Kleen unsigned long get_taint(void)
37725ddbb18SAndi Kleen {
37825ddbb18SAndi Kleen 	return tainted_mask;
37925ddbb18SAndi Kleen }
38025ddbb18SAndi Kleen 
381373d4d09SRusty Russell /**
382373d4d09SRusty Russell  * add_taint: add a taint flag if not already set.
383373d4d09SRusty Russell  * @flag: one of the TAINT_* constants.
384373d4d09SRusty Russell  * @lockdep_ok: whether lock debugging is still OK.
385373d4d09SRusty Russell  *
386373d4d09SRusty Russell  * If something bad has gone wrong, you'll want @lockdebug_ok = false, but for
387373d4d09SRusty Russell  * some notewortht-but-not-corrupting cases, it can be set to true.
3889eeba613SFrederic Weisbecker  */
389373d4d09SRusty Russell void add_taint(unsigned flag, enum lockdep_ok lockdep_ok)
390373d4d09SRusty Russell {
391373d4d09SRusty Russell 	if (lockdep_ok == LOCKDEP_NOW_UNRELIABLE && __debug_locks_off())
392d7c0847fSFabian Frederick 		pr_warn("Disabling lock debugging due to kernel taint\n");
3939eeba613SFrederic Weisbecker 
39425ddbb18SAndi Kleen 	set_bit(flag, &tainted_mask);
3951da177e4SLinus Torvalds }
3961da177e4SLinus Torvalds EXPORT_SYMBOL(add_taint);
397dd287796SAndrew Morton 
398dd287796SAndrew Morton static void spin_msec(int msecs)
399dd287796SAndrew Morton {
400dd287796SAndrew Morton 	int i;
401dd287796SAndrew Morton 
402dd287796SAndrew Morton 	for (i = 0; i < msecs; i++) {
403dd287796SAndrew Morton 		touch_nmi_watchdog();
404dd287796SAndrew Morton 		mdelay(1);
405dd287796SAndrew Morton 	}
406dd287796SAndrew Morton }
407dd287796SAndrew Morton 
408dd287796SAndrew Morton /*
409dd287796SAndrew Morton  * It just happens that oops_enter() and oops_exit() are identically
410dd287796SAndrew Morton  * implemented...
411dd287796SAndrew Morton  */
412dd287796SAndrew Morton static void do_oops_enter_exit(void)
413dd287796SAndrew Morton {
414dd287796SAndrew Morton 	unsigned long flags;
415dd287796SAndrew Morton 	static int spin_counter;
416dd287796SAndrew Morton 
417dd287796SAndrew Morton 	if (!pause_on_oops)
418dd287796SAndrew Morton 		return;
419dd287796SAndrew Morton 
420dd287796SAndrew Morton 	spin_lock_irqsave(&pause_on_oops_lock, flags);
421dd287796SAndrew Morton 	if (pause_on_oops_flag == 0) {
422dd287796SAndrew Morton 		/* This CPU may now print the oops message */
423dd287796SAndrew Morton 		pause_on_oops_flag = 1;
424dd287796SAndrew Morton 	} else {
425dd287796SAndrew Morton 		/* We need to stall this CPU */
426dd287796SAndrew Morton 		if (!spin_counter) {
427dd287796SAndrew Morton 			/* This CPU gets to do the counting */
428dd287796SAndrew Morton 			spin_counter = pause_on_oops;
429dd287796SAndrew Morton 			do {
430dd287796SAndrew Morton 				spin_unlock(&pause_on_oops_lock);
431dd287796SAndrew Morton 				spin_msec(MSEC_PER_SEC);
432dd287796SAndrew Morton 				spin_lock(&pause_on_oops_lock);
433dd287796SAndrew Morton 			} while (--spin_counter);
434dd287796SAndrew Morton 			pause_on_oops_flag = 0;
435dd287796SAndrew Morton 		} else {
436dd287796SAndrew Morton 			/* This CPU waits for a different one */
437dd287796SAndrew Morton 			while (spin_counter) {
438dd287796SAndrew Morton 				spin_unlock(&pause_on_oops_lock);
439dd287796SAndrew Morton 				spin_msec(1);
440dd287796SAndrew Morton 				spin_lock(&pause_on_oops_lock);
441dd287796SAndrew Morton 			}
442dd287796SAndrew Morton 		}
443dd287796SAndrew Morton 	}
444dd287796SAndrew Morton 	spin_unlock_irqrestore(&pause_on_oops_lock, flags);
445dd287796SAndrew Morton }
446dd287796SAndrew Morton 
447dd287796SAndrew Morton /*
448c95dbf27SIngo Molnar  * Return true if the calling CPU is allowed to print oops-related info.
449c95dbf27SIngo Molnar  * This is a bit racy..
450dd287796SAndrew Morton  */
451dd287796SAndrew Morton int oops_may_print(void)
452dd287796SAndrew Morton {
453dd287796SAndrew Morton 	return pause_on_oops_flag == 0;
454dd287796SAndrew Morton }
455dd287796SAndrew Morton 
456dd287796SAndrew Morton /*
457dd287796SAndrew Morton  * Called when the architecture enters its oops handler, before it prints
458c95dbf27SIngo Molnar  * anything.  If this is the first CPU to oops, and it's oopsing the first
459c95dbf27SIngo Molnar  * time then let it proceed.
460dd287796SAndrew Morton  *
461c95dbf27SIngo Molnar  * This is all enabled by the pause_on_oops kernel boot option.  We do all
462c95dbf27SIngo Molnar  * this to ensure that oopses don't scroll off the screen.  It has the
463c95dbf27SIngo Molnar  * side-effect of preventing later-oopsing CPUs from mucking up the display,
464c95dbf27SIngo Molnar  * too.
465dd287796SAndrew Morton  *
466c95dbf27SIngo Molnar  * It turns out that the CPU which is allowed to print ends up pausing for
467c95dbf27SIngo Molnar  * the right duration, whereas all the other CPUs pause for twice as long:
468c95dbf27SIngo Molnar  * once in oops_enter(), once in oops_exit().
469dd287796SAndrew Morton  */
470dd287796SAndrew Morton void oops_enter(void)
471dd287796SAndrew Morton {
472bdff7870SThomas Gleixner 	tracing_off();
473c95dbf27SIngo Molnar 	/* can't trust the integrity of the kernel anymore: */
474c95dbf27SIngo Molnar 	debug_locks_off();
475dd287796SAndrew Morton 	do_oops_enter_exit();
476dd287796SAndrew Morton }
477dd287796SAndrew Morton 
478dd287796SAndrew Morton /*
4792c3b20e9SArjan van de Ven  * 64-bit random ID for oopses:
4802c3b20e9SArjan van de Ven  */
4812c3b20e9SArjan van de Ven static u64 oops_id;
4822c3b20e9SArjan van de Ven 
4832c3b20e9SArjan van de Ven static int init_oops_id(void)
4842c3b20e9SArjan van de Ven {
4852c3b20e9SArjan van de Ven 	if (!oops_id)
4862c3b20e9SArjan van de Ven 		get_random_bytes(&oops_id, sizeof(oops_id));
487d6624f99SArjan van de Ven 	else
488d6624f99SArjan van de Ven 		oops_id++;
4892c3b20e9SArjan van de Ven 
4902c3b20e9SArjan van de Ven 	return 0;
4912c3b20e9SArjan van de Ven }
4922c3b20e9SArjan van de Ven late_initcall(init_oops_id);
4932c3b20e9SArjan van de Ven 
494863a6049SAnton Blanchard void print_oops_end_marker(void)
49571c33911SArjan van de Ven {
49671c33911SArjan van de Ven 	init_oops_id();
497d7c0847fSFabian Frederick 	pr_warn("---[ end trace %016llx ]---\n", (unsigned long long)oops_id);
49871c33911SArjan van de Ven }
49971c33911SArjan van de Ven 
5002c3b20e9SArjan van de Ven /*
501dd287796SAndrew Morton  * Called when the architecture exits its oops handler, after printing
502dd287796SAndrew Morton  * everything.
503dd287796SAndrew Morton  */
504dd287796SAndrew Morton void oops_exit(void)
505dd287796SAndrew Morton {
506dd287796SAndrew Morton 	do_oops_enter_exit();
50771c33911SArjan van de Ven 	print_oops_end_marker();
508456b565cSSimon Kagstrom 	kmsg_dump(KMSG_DUMP_OOPS);
509dd287796SAndrew Morton }
5103162f751SArjan van de Ven 
5112553b67aSJosh Poimboeuf struct warn_args {
5120f6f49a8SLinus Torvalds 	const char *fmt;
513a8f18b90SArjan van de Ven 	va_list args;
5140f6f49a8SLinus Torvalds };
5150f6f49a8SLinus Torvalds 
5162553b67aSJosh Poimboeuf void __warn(const char *file, int line, void *caller, unsigned taint,
5172553b67aSJosh Poimboeuf 	    struct pt_regs *regs, struct warn_args *args)
5180f6f49a8SLinus Torvalds {
519de7edd31SSteven Rostedt (Red Hat) 	disable_trace_on_warning();
520de7edd31SSteven Rostedt (Red Hat) 
521dcb6b452SAlex Thorlton 	pr_warn("------------[ cut here ]------------\n");
5222553b67aSJosh Poimboeuf 
5232553b67aSJosh Poimboeuf 	if (file)
5242553b67aSJosh Poimboeuf 		pr_warn("WARNING: CPU: %d PID: %d at %s:%d %pS\n",
5252553b67aSJosh Poimboeuf 			raw_smp_processor_id(), current->pid, file, line,
5262553b67aSJosh Poimboeuf 			caller);
5272553b67aSJosh Poimboeuf 	else
5282553b67aSJosh Poimboeuf 		pr_warn("WARNING: CPU: %d PID: %d at %pS\n",
5292553b67aSJosh Poimboeuf 			raw_smp_processor_id(), current->pid, caller);
53074853dbaSArjan van de Ven 
5310f6f49a8SLinus Torvalds 	if (args)
5320f6f49a8SLinus Torvalds 		vprintk(args->fmt, args->args);
533a8f18b90SArjan van de Ven 
5349e3961a0SPrarit Bhargava 	if (panic_on_warn) {
5359e3961a0SPrarit Bhargava 		/*
5369e3961a0SPrarit Bhargava 		 * This thread may hit another WARN() in the panic path.
5379e3961a0SPrarit Bhargava 		 * Resetting this prevents additional WARN() from panicking the
5389e3961a0SPrarit Bhargava 		 * system on this thread.  Other threads are blocked by the
5399e3961a0SPrarit Bhargava 		 * panic_mutex in panic().
5409e3961a0SPrarit Bhargava 		 */
5419e3961a0SPrarit Bhargava 		panic_on_warn = 0;
5429e3961a0SPrarit Bhargava 		panic("panic_on_warn set ...\n");
5439e3961a0SPrarit Bhargava 	}
5449e3961a0SPrarit Bhargava 
545a8f18b90SArjan van de Ven 	print_modules();
5462553b67aSJosh Poimboeuf 
5472553b67aSJosh Poimboeuf 	if (regs)
5482553b67aSJosh Poimboeuf 		show_regs(regs);
5492553b67aSJosh Poimboeuf 	else
550a8f18b90SArjan van de Ven 		dump_stack();
5512553b67aSJosh Poimboeuf 
552a8f18b90SArjan van de Ven 	print_oops_end_marker();
5532553b67aSJosh Poimboeuf 
554373d4d09SRusty Russell 	/* Just a warning, don't kill lockdep. */
555373d4d09SRusty Russell 	add_taint(taint, LOCKDEP_STILL_OK);
556a8f18b90SArjan van de Ven }
5570f6f49a8SLinus Torvalds 
5582553b67aSJosh Poimboeuf #ifdef WANT_WARN_ON_SLOWPATH
5590f6f49a8SLinus Torvalds void warn_slowpath_fmt(const char *file, int line, const char *fmt, ...)
5600f6f49a8SLinus Torvalds {
5612553b67aSJosh Poimboeuf 	struct warn_args args;
5620f6f49a8SLinus Torvalds 
5630f6f49a8SLinus Torvalds 	args.fmt = fmt;
5640f6f49a8SLinus Torvalds 	va_start(args.args, fmt);
5652553b67aSJosh Poimboeuf 	__warn(file, line, __builtin_return_address(0), TAINT_WARN, NULL,
5662553b67aSJosh Poimboeuf 	       &args);
5670f6f49a8SLinus Torvalds 	va_end(args.args);
5680f6f49a8SLinus Torvalds }
56957adc4d2SAndi Kleen EXPORT_SYMBOL(warn_slowpath_fmt);
57057adc4d2SAndi Kleen 
571b2be0527SBen Hutchings void warn_slowpath_fmt_taint(const char *file, int line,
572b2be0527SBen Hutchings 			     unsigned taint, const char *fmt, ...)
573b2be0527SBen Hutchings {
5742553b67aSJosh Poimboeuf 	struct warn_args args;
575b2be0527SBen Hutchings 
576b2be0527SBen Hutchings 	args.fmt = fmt;
577b2be0527SBen Hutchings 	va_start(args.args, fmt);
5782553b67aSJosh Poimboeuf 	__warn(file, line, __builtin_return_address(0), taint, NULL, &args);
579b2be0527SBen Hutchings 	va_end(args.args);
580b2be0527SBen Hutchings }
581b2be0527SBen Hutchings EXPORT_SYMBOL(warn_slowpath_fmt_taint);
582b2be0527SBen Hutchings 
58357adc4d2SAndi Kleen void warn_slowpath_null(const char *file, int line)
58457adc4d2SAndi Kleen {
5852553b67aSJosh Poimboeuf 	__warn(file, line, __builtin_return_address(0), TAINT_WARN, NULL, NULL);
58657adc4d2SAndi Kleen }
58757adc4d2SAndi Kleen EXPORT_SYMBOL(warn_slowpath_null);
58879b4cc5eSArjan van de Ven #endif
58979b4cc5eSArjan van de Ven 
5903162f751SArjan van de Ven #ifdef CONFIG_CC_STACKPROTECTOR
59154371a43SArjan van de Ven 
5923162f751SArjan van de Ven /*
5933162f751SArjan van de Ven  * Called when gcc's -fstack-protector feature is used, and
5943162f751SArjan van de Ven  * gcc detects corruption of the on-stack canary value
5953162f751SArjan van de Ven  */
596a7330c99SAndi Kleen __visible void __stack_chk_fail(void)
5973162f751SArjan van de Ven {
598517a92c4SIngo Molnar 	panic("stack-protector: Kernel stack is corrupted in: %p\n",
599517a92c4SIngo Molnar 		__builtin_return_address(0));
6003162f751SArjan van de Ven }
6013162f751SArjan van de Ven EXPORT_SYMBOL(__stack_chk_fail);
60254371a43SArjan van de Ven 
6033162f751SArjan van de Ven #endif
604f44dd164SRusty Russell 
605*7a46ec0eSKees Cook #ifdef CONFIG_ARCH_HAS_REFCOUNT
606*7a46ec0eSKees Cook void refcount_error_report(struct pt_regs *regs, const char *err)
607*7a46ec0eSKees Cook {
608*7a46ec0eSKees Cook 	WARN_RATELIMIT(1, "refcount_t %s at %pB in %s[%d], uid/euid: %u/%u\n",
609*7a46ec0eSKees Cook 		err, (void *)instruction_pointer(regs),
610*7a46ec0eSKees Cook 		current->comm, task_pid_nr(current),
611*7a46ec0eSKees Cook 		from_kuid_munged(&init_user_ns, current_uid()),
612*7a46ec0eSKees Cook 		from_kuid_munged(&init_user_ns, current_euid()));
613*7a46ec0eSKees Cook }
614*7a46ec0eSKees Cook #endif
615*7a46ec0eSKees Cook 
616f44dd164SRusty Russell core_param(panic, panic_timeout, int, 0644);
617f44dd164SRusty Russell core_param(pause_on_oops, pause_on_oops, int, 0644);
6189e3961a0SPrarit Bhargava core_param(panic_on_warn, panic_on_warn, int, 0644);
619b26e27ddSHidehiro Kawai core_param(crash_kexec_post_notifiers, crash_kexec_post_notifiers, bool, 0644);
620f06e5153SMasami Hiramatsu 
621d404ab0aSOlaf Hering static int __init oops_setup(char *s)
622d404ab0aSOlaf Hering {
623d404ab0aSOlaf Hering 	if (!s)
624d404ab0aSOlaf Hering 		return -EINVAL;
625d404ab0aSOlaf Hering 	if (!strcmp(s, "panic"))
626d404ab0aSOlaf Hering 		panic_on_oops = 1;
627d404ab0aSOlaf Hering 	return 0;
628d404ab0aSOlaf Hering }
629d404ab0aSOlaf Hering early_param("oops", oops_setup);
630