xref: /linux-6.15/kernel/panic.c (revision f92bac3b)
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>
12c95dbf27SIngo Molnar #include <linux/interrupt.h>
13456b565cSSimon Kagstrom #include <linux/kmsg_dump.h>
1479b4cc5eSArjan van de Ven #include <linux/kallsyms.h>
15c95dbf27SIngo Molnar #include <linux/notifier.h>
16c95dbf27SIngo Molnar #include <linux/module.h>
17c95dbf27SIngo Molnar #include <linux/random.h>
18de7edd31SSteven Rostedt (Red Hat) #include <linux/ftrace.h>
19c95dbf27SIngo Molnar #include <linux/reboot.h>
20c95dbf27SIngo Molnar #include <linux/delay.h>
21c95dbf27SIngo Molnar #include <linux/kexec.h>
22c95dbf27SIngo Molnar #include <linux/sched.h>
23c95dbf27SIngo Molnar #include <linux/sysrq.h>
24c95dbf27SIngo Molnar #include <linux/init.h>
25c95dbf27SIngo Molnar #include <linux/nmi.h>
2608d78658SVitaly Kuznetsov #include <linux/console.h>
272553b67aSJosh Poimboeuf #include <linux/bug.h>
281da177e4SLinus Torvalds 
29c7ff0d9cSTAMUKI Shoichi #define PANIC_TIMER_STEP 100
30c7ff0d9cSTAMUKI Shoichi #define PANIC_BLINK_SPD 18
31c7ff0d9cSTAMUKI Shoichi 
322a01bb38SKyle McMartin int panic_on_oops = CONFIG_PANIC_ON_OOPS_VALUE;
3325ddbb18SAndi Kleen static unsigned long tainted_mask;
34dd287796SAndrew Morton static int pause_on_oops;
35dd287796SAndrew Morton static int pause_on_oops_flag;
36dd287796SAndrew Morton static DEFINE_SPINLOCK(pause_on_oops_lock);
375375b708SHATAYAMA Daisuke bool crash_kexec_post_notifiers;
389e3961a0SPrarit Bhargava int panic_on_warn __read_mostly;
391da177e4SLinus Torvalds 
405800dc3cSJason Baron int panic_timeout = CONFIG_PANIC_TIMEOUT;
4181e88fdcSHuang Ying EXPORT_SYMBOL_GPL(panic_timeout);
421da177e4SLinus Torvalds 
43e041c683SAlan Stern ATOMIC_NOTIFIER_HEAD(panic_notifier_list);
441da177e4SLinus Torvalds 
451da177e4SLinus Torvalds EXPORT_SYMBOL(panic_notifier_list);
461da177e4SLinus Torvalds 
47c7ff0d9cSTAMUKI Shoichi static long no_blink(int state)
488aeee85aSAnton Blanchard {
49c7ff0d9cSTAMUKI Shoichi 	return 0;
50c7ff0d9cSTAMUKI Shoichi }
518aeee85aSAnton Blanchard 
52c7ff0d9cSTAMUKI Shoichi /* Returns how long it waited in ms */
53c7ff0d9cSTAMUKI Shoichi long (*panic_blink)(int state);
54c7ff0d9cSTAMUKI Shoichi EXPORT_SYMBOL(panic_blink);
558aeee85aSAnton Blanchard 
5693e13a36SMichael Holzheu /*
5793e13a36SMichael Holzheu  * Stop ourself in panic -- architecture code may override this
5893e13a36SMichael Holzheu  */
5993e13a36SMichael Holzheu void __weak panic_smp_self_stop(void)
6093e13a36SMichael Holzheu {
6193e13a36SMichael Holzheu 	while (1)
6293e13a36SMichael Holzheu 		cpu_relax();
6393e13a36SMichael Holzheu }
6493e13a36SMichael Holzheu 
6558c5661fSHidehiro Kawai /*
6658c5661fSHidehiro Kawai  * Stop ourselves in NMI context if another CPU has already panicked. Arch code
6758c5661fSHidehiro Kawai  * may override this to prepare for crash dumping, e.g. save regs info.
6858c5661fSHidehiro Kawai  */
6958c5661fSHidehiro Kawai void __weak nmi_panic_self_stop(struct pt_regs *regs)
7058c5661fSHidehiro Kawai {
7158c5661fSHidehiro Kawai 	panic_smp_self_stop();
7258c5661fSHidehiro Kawai }
7358c5661fSHidehiro Kawai 
740ee59413SHidehiro Kawai /*
750ee59413SHidehiro Kawai  * Stop other CPUs in panic.  Architecture dependent code may override this
760ee59413SHidehiro Kawai  * with more suitable version.  For example, if the architecture supports
770ee59413SHidehiro Kawai  * crash dump, it should save registers of each stopped CPU and disable
780ee59413SHidehiro Kawai  * per-CPU features such as virtualization extensions.
790ee59413SHidehiro Kawai  */
800ee59413SHidehiro Kawai void __weak crash_smp_send_stop(void)
810ee59413SHidehiro Kawai {
820ee59413SHidehiro Kawai 	static int cpus_stopped;
830ee59413SHidehiro Kawai 
840ee59413SHidehiro Kawai 	/*
850ee59413SHidehiro Kawai 	 * This function can be called twice in panic path, but obviously
860ee59413SHidehiro Kawai 	 * we execute this only once.
870ee59413SHidehiro Kawai 	 */
880ee59413SHidehiro Kawai 	if (cpus_stopped)
890ee59413SHidehiro Kawai 		return;
900ee59413SHidehiro Kawai 
910ee59413SHidehiro Kawai 	/*
920ee59413SHidehiro Kawai 	 * Note smp_send_stop is the usual smp shutdown function, which
930ee59413SHidehiro Kawai 	 * unfortunately means it may not be hardened to work in a panic
940ee59413SHidehiro Kawai 	 * situation.
950ee59413SHidehiro Kawai 	 */
960ee59413SHidehiro Kawai 	smp_send_stop();
970ee59413SHidehiro Kawai 	cpus_stopped = 1;
980ee59413SHidehiro Kawai }
990ee59413SHidehiro Kawai 
1001717f209SHidehiro Kawai atomic_t panic_cpu = ATOMIC_INIT(PANIC_CPU_INVALID);
1011717f209SHidehiro Kawai 
102ebc41f20SHidehiro Kawai /*
103ebc41f20SHidehiro Kawai  * A variant of panic() called from NMI context. We return if we've already
104ebc41f20SHidehiro Kawai  * panicked on this CPU. If another CPU already panicked, loop in
105ebc41f20SHidehiro Kawai  * nmi_panic_self_stop() which can provide architecture dependent code such
106ebc41f20SHidehiro Kawai  * as saving register state for crash dump.
107ebc41f20SHidehiro Kawai  */
108ebc41f20SHidehiro Kawai void nmi_panic(struct pt_regs *regs, const char *msg)
109ebc41f20SHidehiro Kawai {
110ebc41f20SHidehiro Kawai 	int old_cpu, cpu;
111ebc41f20SHidehiro Kawai 
112ebc41f20SHidehiro Kawai 	cpu = raw_smp_processor_id();
113ebc41f20SHidehiro Kawai 	old_cpu = atomic_cmpxchg(&panic_cpu, PANIC_CPU_INVALID, cpu);
114ebc41f20SHidehiro Kawai 
115ebc41f20SHidehiro Kawai 	if (old_cpu == PANIC_CPU_INVALID)
116ebc41f20SHidehiro Kawai 		panic("%s", msg);
117ebc41f20SHidehiro Kawai 	else if (old_cpu != cpu)
118ebc41f20SHidehiro Kawai 		nmi_panic_self_stop(regs);
119ebc41f20SHidehiro Kawai }
120ebc41f20SHidehiro Kawai EXPORT_SYMBOL(nmi_panic);
121ebc41f20SHidehiro Kawai 
1221da177e4SLinus Torvalds /**
1231da177e4SLinus Torvalds  *	panic - halt the system
1241da177e4SLinus Torvalds  *	@fmt: The text string to print
1251da177e4SLinus Torvalds  *
1261da177e4SLinus Torvalds  *	Display a message, then perform cleanups.
1271da177e4SLinus Torvalds  *
1281da177e4SLinus Torvalds  *	This function never returns.
1291da177e4SLinus Torvalds  */
1309402c95fSJoe Perches void panic(const char *fmt, ...)
1311da177e4SLinus Torvalds {
1321da177e4SLinus Torvalds 	static char buf[1024];
1331da177e4SLinus Torvalds 	va_list args;
134c7ff0d9cSTAMUKI Shoichi 	long i, i_next = 0;
135c7ff0d9cSTAMUKI Shoichi 	int state = 0;
1361717f209SHidehiro Kawai 	int old_cpu, this_cpu;
137b26e27ddSHidehiro Kawai 	bool _crash_kexec_post_notifiers = crash_kexec_post_notifiers;
1381da177e4SLinus Torvalds 
139dc009d92SEric W. Biederman 	/*
140190320c3SVikram Mulukutla 	 * Disable local interrupts. This will prevent panic_smp_self_stop
141190320c3SVikram Mulukutla 	 * from deadlocking the first cpu that invokes the panic, since
142190320c3SVikram Mulukutla 	 * there is nothing to prevent an interrupt handler (that runs
1431717f209SHidehiro Kawai 	 * after setting panic_cpu) from invoking panic() again.
144190320c3SVikram Mulukutla 	 */
145190320c3SVikram Mulukutla 	local_irq_disable();
146190320c3SVikram Mulukutla 
147190320c3SVikram Mulukutla 	/*
148c95dbf27SIngo Molnar 	 * It's possible to come here directly from a panic-assertion and
149c95dbf27SIngo Molnar 	 * not have preempt disabled. Some functions called from here want
150dc009d92SEric W. Biederman 	 * preempt to be disabled. No point enabling it later though...
15193e13a36SMichael Holzheu 	 *
15293e13a36SMichael Holzheu 	 * Only one CPU is allowed to execute the panic code from here. For
15393e13a36SMichael Holzheu 	 * multiple parallel invocations of panic, all other CPUs either
15493e13a36SMichael Holzheu 	 * stop themself or will wait until they are stopped by the 1st CPU
15593e13a36SMichael Holzheu 	 * with smp_send_stop().
1561717f209SHidehiro Kawai 	 *
1571717f209SHidehiro Kawai 	 * `old_cpu == PANIC_CPU_INVALID' means this is the 1st CPU which
1581717f209SHidehiro Kawai 	 * comes here, so go ahead.
1591717f209SHidehiro Kawai 	 * `old_cpu == this_cpu' means we came from nmi_panic() which sets
1601717f209SHidehiro Kawai 	 * panic_cpu to this CPU.  In this case, this is also the 1st CPU.
161dc009d92SEric W. Biederman 	 */
1621717f209SHidehiro Kawai 	this_cpu = raw_smp_processor_id();
1631717f209SHidehiro Kawai 	old_cpu  = atomic_cmpxchg(&panic_cpu, PANIC_CPU_INVALID, this_cpu);
1641717f209SHidehiro Kawai 
1651717f209SHidehiro Kawai 	if (old_cpu != PANIC_CPU_INVALID && old_cpu != this_cpu)
16693e13a36SMichael Holzheu 		panic_smp_self_stop();
167dc009d92SEric W. Biederman 
1685b530fc1SAnton Blanchard 	console_verbose();
1691da177e4SLinus Torvalds 	bust_spinlocks(1);
1701da177e4SLinus Torvalds 	va_start(args, fmt);
1711da177e4SLinus Torvalds 	vsnprintf(buf, sizeof(buf), fmt, args);
1721da177e4SLinus Torvalds 	va_end(args);
173d7c0847fSFabian Frederick 	pr_emerg("Kernel panic - not syncing: %s\n", buf);
1745cb27301SIngo Molnar #ifdef CONFIG_DEBUG_BUGVERBOSE
1756e6f0a1fSAndi Kleen 	/*
1766e6f0a1fSAndi Kleen 	 * Avoid nested stack-dumping if a panic occurs during oops processing
1776e6f0a1fSAndi Kleen 	 */
178026ee1f6SJason Wessel 	if (!test_taint(TAINT_DIE) && oops_in_progress <= 1)
1795cb27301SIngo Molnar 		dump_stack();
1805cb27301SIngo Molnar #endif
1811da177e4SLinus Torvalds 
182dc009d92SEric W. Biederman 	/*
183dc009d92SEric W. Biederman 	 * If we have crashed and we have a crash kernel loaded let it handle
184dc009d92SEric W. Biederman 	 * everything else.
185f06e5153SMasami Hiramatsu 	 * If we want to run this after calling panic_notifiers, pass
186f06e5153SMasami Hiramatsu 	 * the "crash_kexec_post_notifiers" option to the kernel.
1877bbee5caSHidehiro Kawai 	 *
1887bbee5caSHidehiro Kawai 	 * Bypass the panic_cpu check and call __crash_kexec directly.
189dc009d92SEric W. Biederman 	 */
190b26e27ddSHidehiro Kawai 	if (!_crash_kexec_post_notifiers) {
191*f92bac3bSSergey Senozhatsky 		printk_safe_flush_on_panic();
1927bbee5caSHidehiro Kawai 		__crash_kexec(NULL);
193dc009d92SEric W. Biederman 
194dc009d92SEric W. Biederman 		/*
195dc009d92SEric W. Biederman 		 * Note smp_send_stop is the usual smp shutdown function, which
1960ee59413SHidehiro Kawai 		 * unfortunately means it may not be hardened to work in a
1970ee59413SHidehiro Kawai 		 * panic situation.
198dc009d92SEric W. Biederman 		 */
1991da177e4SLinus Torvalds 		smp_send_stop();
2000ee59413SHidehiro Kawai 	} else {
2010ee59413SHidehiro Kawai 		/*
2020ee59413SHidehiro Kawai 		 * If we want to do crash dump after notifier calls and
2030ee59413SHidehiro Kawai 		 * kmsg_dump, we will need architecture dependent extra
2040ee59413SHidehiro Kawai 		 * works in addition to stopping other CPUs.
2050ee59413SHidehiro Kawai 		 */
2060ee59413SHidehiro Kawai 		crash_smp_send_stop();
2070ee59413SHidehiro Kawai 	}
2081da177e4SLinus Torvalds 
2096723734cSKees Cook 	/*
2106723734cSKees Cook 	 * Run any panic handlers, including those that might need to
2116723734cSKees Cook 	 * add information to the kmsg dump output.
2126723734cSKees Cook 	 */
213e041c683SAlan Stern 	atomic_notifier_call_chain(&panic_notifier_list, 0, buf);
2141da177e4SLinus Torvalds 
215cf9b1106SPetr Mladek 	/* Call flush even twice. It tries harder with a single online CPU */
216*f92bac3bSSergey Senozhatsky 	printk_safe_flush_on_panic();
2176723734cSKees Cook 	kmsg_dump(KMSG_DUMP_PANIC);
2186723734cSKees Cook 
219f06e5153SMasami Hiramatsu 	/*
220f06e5153SMasami Hiramatsu 	 * If you doubt kdump always works fine in any situation,
221f06e5153SMasami Hiramatsu 	 * "crash_kexec_post_notifiers" offers you a chance to run
222f06e5153SMasami Hiramatsu 	 * panic_notifiers and dumping kmsg before kdump.
223f06e5153SMasami Hiramatsu 	 * Note: since some panic_notifiers can make crashed kernel
224f06e5153SMasami Hiramatsu 	 * more unstable, it can increase risks of the kdump failure too.
2257bbee5caSHidehiro Kawai 	 *
2267bbee5caSHidehiro Kawai 	 * Bypass the panic_cpu check and call __crash_kexec directly.
227f06e5153SMasami Hiramatsu 	 */
228b26e27ddSHidehiro Kawai 	if (_crash_kexec_post_notifiers)
2297bbee5caSHidehiro Kawai 		__crash_kexec(NULL);
230f06e5153SMasami Hiramatsu 
231d014e889SAaro Koskinen 	bust_spinlocks(0);
232d014e889SAaro Koskinen 
23308d78658SVitaly Kuznetsov 	/*
23408d78658SVitaly Kuznetsov 	 * We may have ended up stopping the CPU holding the lock (in
23508d78658SVitaly Kuznetsov 	 * smp_send_stop()) while still having some valuable data in the console
23608d78658SVitaly Kuznetsov 	 * buffer.  Try to acquire the lock then release it regardless of the
2377625b3a0SVitaly Kuznetsov 	 * result.  The release will also print the buffers out.  Locks debug
2387625b3a0SVitaly Kuznetsov 	 * should be disabled to avoid reporting bad unlock balance when
2397625b3a0SVitaly Kuznetsov 	 * panic() is not being callled from OOPS.
24008d78658SVitaly Kuznetsov 	 */
2417625b3a0SVitaly Kuznetsov 	debug_locks_off();
2428d91f8b1STejun Heo 	console_flush_on_panic();
24308d78658SVitaly Kuznetsov 
244c7ff0d9cSTAMUKI Shoichi 	if (!panic_blink)
245c7ff0d9cSTAMUKI Shoichi 		panic_blink = no_blink;
246c7ff0d9cSTAMUKI Shoichi 
247dc009d92SEric W. Biederman 	if (panic_timeout > 0) {
2481da177e4SLinus Torvalds 		/*
2491da177e4SLinus Torvalds 		 * Delay timeout seconds before rebooting the machine.
250c95dbf27SIngo Molnar 		 * We can't use the "normal" timers since we just panicked.
2511da177e4SLinus Torvalds 		 */
252d7c0847fSFabian Frederick 		pr_emerg("Rebooting in %d seconds..", panic_timeout);
253c95dbf27SIngo Molnar 
254c7ff0d9cSTAMUKI Shoichi 		for (i = 0; i < panic_timeout * 1000; i += PANIC_TIMER_STEP) {
2551da177e4SLinus Torvalds 			touch_nmi_watchdog();
256c7ff0d9cSTAMUKI Shoichi 			if (i >= i_next) {
257c7ff0d9cSTAMUKI Shoichi 				i += panic_blink(state ^= 1);
258c7ff0d9cSTAMUKI Shoichi 				i_next = i + 3600 / PANIC_BLINK_SPD;
259c7ff0d9cSTAMUKI Shoichi 			}
260c7ff0d9cSTAMUKI Shoichi 			mdelay(PANIC_TIMER_STEP);
2611da177e4SLinus Torvalds 		}
2624302fbc8SHugh Dickins 	}
2634302fbc8SHugh Dickins 	if (panic_timeout != 0) {
264c95dbf27SIngo Molnar 		/*
265c95dbf27SIngo Molnar 		 * This will not be a clean reboot, with everything
2662f048ea8SEric W. Biederman 		 * shutting down.  But if there is a chance of
2672f048ea8SEric W. Biederman 		 * rebooting the system it will be rebooted.
2681da177e4SLinus Torvalds 		 */
2692f048ea8SEric W. Biederman 		emergency_restart();
2701da177e4SLinus Torvalds 	}
2711da177e4SLinus Torvalds #ifdef __sparc__
2721da177e4SLinus Torvalds 	{
2731da177e4SLinus Torvalds 		extern int stop_a_enabled;
274a271c241STom 'spot' Callaway 		/* Make sure the user can actually press Stop-A (L1-A) */
2751da177e4SLinus Torvalds 		stop_a_enabled = 1;
276d7c0847fSFabian Frederick 		pr_emerg("Press Stop-A (L1-A) to return to the boot prom\n");
2771da177e4SLinus Torvalds 	}
2781da177e4SLinus Torvalds #endif
279347a8dc3SMartin Schwidefsky #if defined(CONFIG_S390)
280c95dbf27SIngo Molnar 	{
281c95dbf27SIngo Molnar 		unsigned long caller;
282c95dbf27SIngo Molnar 
283c95dbf27SIngo Molnar 		caller = (unsigned long)__builtin_return_address(0);
2841da177e4SLinus Torvalds 		disabled_wait(caller);
285c95dbf27SIngo Molnar 	}
2861da177e4SLinus Torvalds #endif
287d7c0847fSFabian Frederick 	pr_emerg("---[ end Kernel panic - not syncing: %s\n", buf);
2881da177e4SLinus Torvalds 	local_irq_enable();
289c7ff0d9cSTAMUKI Shoichi 	for (i = 0; ; i += PANIC_TIMER_STEP) {
290c22db941SJan Beulich 		touch_softlockup_watchdog();
291c7ff0d9cSTAMUKI Shoichi 		if (i >= i_next) {
292c7ff0d9cSTAMUKI Shoichi 			i += panic_blink(state ^= 1);
293c7ff0d9cSTAMUKI Shoichi 			i_next = i + 3600 / PANIC_BLINK_SPD;
294c7ff0d9cSTAMUKI Shoichi 		}
295c7ff0d9cSTAMUKI Shoichi 		mdelay(PANIC_TIMER_STEP);
2961da177e4SLinus Torvalds 	}
2971da177e4SLinus Torvalds }
2981da177e4SLinus Torvalds 
2991da177e4SLinus Torvalds EXPORT_SYMBOL(panic);
3001da177e4SLinus Torvalds 
3017fd8329bSPetr Mladek /*
3027fd8329bSPetr Mladek  * TAINT_FORCED_RMMOD could be a per-module flag but the module
3037fd8329bSPetr Mladek  * is being removed anyway.
3047fd8329bSPetr Mladek  */
3057fd8329bSPetr Mladek const struct taint_flag taint_flags[TAINT_FLAGS_COUNT] = {
3067fd8329bSPetr Mladek 	{ 'P', 'G', true },	/* TAINT_PROPRIETARY_MODULE */
3077fd8329bSPetr Mladek 	{ 'F', ' ', true },	/* TAINT_FORCED_MODULE */
3087fd8329bSPetr Mladek 	{ 'S', ' ', false },	/* TAINT_CPU_OUT_OF_SPEC */
3097fd8329bSPetr Mladek 	{ 'R', ' ', false },	/* TAINT_FORCED_RMMOD */
3107fd8329bSPetr Mladek 	{ 'M', ' ', false },	/* TAINT_MACHINE_CHECK */
3117fd8329bSPetr Mladek 	{ 'B', ' ', false },	/* TAINT_BAD_PAGE */
3127fd8329bSPetr Mladek 	{ 'U', ' ', false },	/* TAINT_USER */
3137fd8329bSPetr Mladek 	{ 'D', ' ', false },	/* TAINT_DIE */
3147fd8329bSPetr Mladek 	{ 'A', ' ', false },	/* TAINT_OVERRIDDEN_ACPI_TABLE */
3157fd8329bSPetr Mladek 	{ 'W', ' ', false },	/* TAINT_WARN */
3167fd8329bSPetr Mladek 	{ 'C', ' ', true },	/* TAINT_CRAP */
3177fd8329bSPetr Mladek 	{ 'I', ' ', false },	/* TAINT_FIRMWARE_WORKAROUND */
3187fd8329bSPetr Mladek 	{ 'O', ' ', true },	/* TAINT_OOT_MODULE */
3197fd8329bSPetr Mladek 	{ 'E', ' ', true },	/* TAINT_UNSIGNED_MODULE */
3207fd8329bSPetr Mladek 	{ 'L', ' ', false },	/* TAINT_SOFTLOCKUP */
3217fd8329bSPetr Mladek 	{ 'K', ' ', true },	/* TAINT_LIVEPATCH */
32225ddbb18SAndi Kleen };
32325ddbb18SAndi Kleen 
3241da177e4SLinus Torvalds /**
3251da177e4SLinus Torvalds  *	print_tainted - return a string to represent the kernel taint state.
3261da177e4SLinus Torvalds  *
3271da177e4SLinus Torvalds  *  'P' - Proprietary module has been loaded.
3281da177e4SLinus Torvalds  *  'F' - Module has been forcibly loaded.
3291da177e4SLinus Torvalds  *  'S' - SMP with CPUs not designed for SMP.
3301da177e4SLinus Torvalds  *  'R' - User forced a module unload.
3319aa5e993SDaniel Roesen  *  'M' - System experienced a machine check exception.
3321da177e4SLinus Torvalds  *  'B' - System has hit bad_page.
33334f5a398STheodore Ts'o  *  'U' - Userspace-defined naughtiness.
334a8005992SArjan van de Ven  *  'D' - Kernel has oopsed before
33595b570c9SNur Hussein  *  'A' - ACPI table overridden.
33695b570c9SNur Hussein  *  'W' - Taint on warning.
337061b1bd3SGreg Kroah-Hartman  *  'C' - modules from drivers/staging are loaded.
33892946bc7SBen Hutchings  *  'I' - Working around severe firmware bug.
3392449b8baSBen Hutchings  *  'O' - Out-of-tree module has been loaded.
34057673c2bSRusty Russell  *  'E' - Unsigned module has been loaded.
341bc53a3f4SXie XiuQi  *  'L' - A soft lockup has previously occurred.
342c5f45465SSeth Jennings  *  'K' - Kernel has been live patched.
3431da177e4SLinus Torvalds  *
344fe002a41SRobert P. J. Day  *	The string is overwritten by the next call to print_tainted().
3451da177e4SLinus Torvalds  */
3461da177e4SLinus Torvalds const char *print_tainted(void)
3471da177e4SLinus Torvalds {
3487fd8329bSPetr Mladek 	static char buf[TAINT_FLAGS_COUNT + sizeof("Tainted: ")];
34925ddbb18SAndi Kleen 
35025ddbb18SAndi Kleen 	if (tainted_mask) {
35125ddbb18SAndi Kleen 		char *s;
35225ddbb18SAndi Kleen 		int i;
35325ddbb18SAndi Kleen 
35425ddbb18SAndi Kleen 		s = buf + sprintf(buf, "Tainted: ");
3557fd8329bSPetr Mladek 		for (i = 0; i < TAINT_FLAGS_COUNT; i++) {
3567fd8329bSPetr Mladek 			const struct taint_flag *t = &taint_flags[i];
3577fd8329bSPetr Mladek 			*s++ = test_bit(i, &tainted_mask) ?
35825ddbb18SAndi Kleen 					t->true : t->false;
3591da177e4SLinus Torvalds 		}
36025ddbb18SAndi Kleen 		*s = 0;
36125ddbb18SAndi Kleen 	} else
3621da177e4SLinus Torvalds 		snprintf(buf, sizeof(buf), "Not tainted");
363c95dbf27SIngo Molnar 
364c95dbf27SIngo Molnar 	return buf;
3651da177e4SLinus Torvalds }
3661da177e4SLinus Torvalds 
36725ddbb18SAndi Kleen int test_taint(unsigned flag)
36825ddbb18SAndi Kleen {
36925ddbb18SAndi Kleen 	return test_bit(flag, &tainted_mask);
37025ddbb18SAndi Kleen }
37125ddbb18SAndi Kleen EXPORT_SYMBOL(test_taint);
37225ddbb18SAndi Kleen 
37325ddbb18SAndi Kleen unsigned long get_taint(void)
37425ddbb18SAndi Kleen {
37525ddbb18SAndi Kleen 	return tainted_mask;
37625ddbb18SAndi Kleen }
37725ddbb18SAndi Kleen 
378373d4d09SRusty Russell /**
379373d4d09SRusty Russell  * add_taint: add a taint flag if not already set.
380373d4d09SRusty Russell  * @flag: one of the TAINT_* constants.
381373d4d09SRusty Russell  * @lockdep_ok: whether lock debugging is still OK.
382373d4d09SRusty Russell  *
383373d4d09SRusty Russell  * If something bad has gone wrong, you'll want @lockdebug_ok = false, but for
384373d4d09SRusty Russell  * some notewortht-but-not-corrupting cases, it can be set to true.
3859eeba613SFrederic Weisbecker  */
386373d4d09SRusty Russell void add_taint(unsigned flag, enum lockdep_ok lockdep_ok)
387373d4d09SRusty Russell {
388373d4d09SRusty Russell 	if (lockdep_ok == LOCKDEP_NOW_UNRELIABLE && __debug_locks_off())
389d7c0847fSFabian Frederick 		pr_warn("Disabling lock debugging due to kernel taint\n");
3909eeba613SFrederic Weisbecker 
39125ddbb18SAndi Kleen 	set_bit(flag, &tainted_mask);
3921da177e4SLinus Torvalds }
3931da177e4SLinus Torvalds EXPORT_SYMBOL(add_taint);
394dd287796SAndrew Morton 
395dd287796SAndrew Morton static void spin_msec(int msecs)
396dd287796SAndrew Morton {
397dd287796SAndrew Morton 	int i;
398dd287796SAndrew Morton 
399dd287796SAndrew Morton 	for (i = 0; i < msecs; i++) {
400dd287796SAndrew Morton 		touch_nmi_watchdog();
401dd287796SAndrew Morton 		mdelay(1);
402dd287796SAndrew Morton 	}
403dd287796SAndrew Morton }
404dd287796SAndrew Morton 
405dd287796SAndrew Morton /*
406dd287796SAndrew Morton  * It just happens that oops_enter() and oops_exit() are identically
407dd287796SAndrew Morton  * implemented...
408dd287796SAndrew Morton  */
409dd287796SAndrew Morton static void do_oops_enter_exit(void)
410dd287796SAndrew Morton {
411dd287796SAndrew Morton 	unsigned long flags;
412dd287796SAndrew Morton 	static int spin_counter;
413dd287796SAndrew Morton 
414dd287796SAndrew Morton 	if (!pause_on_oops)
415dd287796SAndrew Morton 		return;
416dd287796SAndrew Morton 
417dd287796SAndrew Morton 	spin_lock_irqsave(&pause_on_oops_lock, flags);
418dd287796SAndrew Morton 	if (pause_on_oops_flag == 0) {
419dd287796SAndrew Morton 		/* This CPU may now print the oops message */
420dd287796SAndrew Morton 		pause_on_oops_flag = 1;
421dd287796SAndrew Morton 	} else {
422dd287796SAndrew Morton 		/* We need to stall this CPU */
423dd287796SAndrew Morton 		if (!spin_counter) {
424dd287796SAndrew Morton 			/* This CPU gets to do the counting */
425dd287796SAndrew Morton 			spin_counter = pause_on_oops;
426dd287796SAndrew Morton 			do {
427dd287796SAndrew Morton 				spin_unlock(&pause_on_oops_lock);
428dd287796SAndrew Morton 				spin_msec(MSEC_PER_SEC);
429dd287796SAndrew Morton 				spin_lock(&pause_on_oops_lock);
430dd287796SAndrew Morton 			} while (--spin_counter);
431dd287796SAndrew Morton 			pause_on_oops_flag = 0;
432dd287796SAndrew Morton 		} else {
433dd287796SAndrew Morton 			/* This CPU waits for a different one */
434dd287796SAndrew Morton 			while (spin_counter) {
435dd287796SAndrew Morton 				spin_unlock(&pause_on_oops_lock);
436dd287796SAndrew Morton 				spin_msec(1);
437dd287796SAndrew Morton 				spin_lock(&pause_on_oops_lock);
438dd287796SAndrew Morton 			}
439dd287796SAndrew Morton 		}
440dd287796SAndrew Morton 	}
441dd287796SAndrew Morton 	spin_unlock_irqrestore(&pause_on_oops_lock, flags);
442dd287796SAndrew Morton }
443dd287796SAndrew Morton 
444dd287796SAndrew Morton /*
445c95dbf27SIngo Molnar  * Return true if the calling CPU is allowed to print oops-related info.
446c95dbf27SIngo Molnar  * This is a bit racy..
447dd287796SAndrew Morton  */
448dd287796SAndrew Morton int oops_may_print(void)
449dd287796SAndrew Morton {
450dd287796SAndrew Morton 	return pause_on_oops_flag == 0;
451dd287796SAndrew Morton }
452dd287796SAndrew Morton 
453dd287796SAndrew Morton /*
454dd287796SAndrew Morton  * Called when the architecture enters its oops handler, before it prints
455c95dbf27SIngo Molnar  * anything.  If this is the first CPU to oops, and it's oopsing the first
456c95dbf27SIngo Molnar  * time then let it proceed.
457dd287796SAndrew Morton  *
458c95dbf27SIngo Molnar  * This is all enabled by the pause_on_oops kernel boot option.  We do all
459c95dbf27SIngo Molnar  * this to ensure that oopses don't scroll off the screen.  It has the
460c95dbf27SIngo Molnar  * side-effect of preventing later-oopsing CPUs from mucking up the display,
461c95dbf27SIngo Molnar  * too.
462dd287796SAndrew Morton  *
463c95dbf27SIngo Molnar  * It turns out that the CPU which is allowed to print ends up pausing for
464c95dbf27SIngo Molnar  * the right duration, whereas all the other CPUs pause for twice as long:
465c95dbf27SIngo Molnar  * once in oops_enter(), once in oops_exit().
466dd287796SAndrew Morton  */
467dd287796SAndrew Morton void oops_enter(void)
468dd287796SAndrew Morton {
469bdff7870SThomas Gleixner 	tracing_off();
470c95dbf27SIngo Molnar 	/* can't trust the integrity of the kernel anymore: */
471c95dbf27SIngo Molnar 	debug_locks_off();
472dd287796SAndrew Morton 	do_oops_enter_exit();
473dd287796SAndrew Morton }
474dd287796SAndrew Morton 
475dd287796SAndrew Morton /*
4762c3b20e9SArjan van de Ven  * 64-bit random ID for oopses:
4772c3b20e9SArjan van de Ven  */
4782c3b20e9SArjan van de Ven static u64 oops_id;
4792c3b20e9SArjan van de Ven 
4802c3b20e9SArjan van de Ven static int init_oops_id(void)
4812c3b20e9SArjan van de Ven {
4822c3b20e9SArjan van de Ven 	if (!oops_id)
4832c3b20e9SArjan van de Ven 		get_random_bytes(&oops_id, sizeof(oops_id));
484d6624f99SArjan van de Ven 	else
485d6624f99SArjan van de Ven 		oops_id++;
4862c3b20e9SArjan van de Ven 
4872c3b20e9SArjan van de Ven 	return 0;
4882c3b20e9SArjan van de Ven }
4892c3b20e9SArjan van de Ven late_initcall(init_oops_id);
4902c3b20e9SArjan van de Ven 
491863a6049SAnton Blanchard void print_oops_end_marker(void)
49271c33911SArjan van de Ven {
49371c33911SArjan van de Ven 	init_oops_id();
494d7c0847fSFabian Frederick 	pr_warn("---[ end trace %016llx ]---\n", (unsigned long long)oops_id);
49571c33911SArjan van de Ven }
49671c33911SArjan van de Ven 
4972c3b20e9SArjan van de Ven /*
498dd287796SAndrew Morton  * Called when the architecture exits its oops handler, after printing
499dd287796SAndrew Morton  * everything.
500dd287796SAndrew Morton  */
501dd287796SAndrew Morton void oops_exit(void)
502dd287796SAndrew Morton {
503dd287796SAndrew Morton 	do_oops_enter_exit();
50471c33911SArjan van de Ven 	print_oops_end_marker();
505456b565cSSimon Kagstrom 	kmsg_dump(KMSG_DUMP_OOPS);
506dd287796SAndrew Morton }
5073162f751SArjan van de Ven 
5082553b67aSJosh Poimboeuf struct warn_args {
5090f6f49a8SLinus Torvalds 	const char *fmt;
510a8f18b90SArjan van de Ven 	va_list args;
5110f6f49a8SLinus Torvalds };
5120f6f49a8SLinus Torvalds 
5132553b67aSJosh Poimboeuf void __warn(const char *file, int line, void *caller, unsigned taint,
5142553b67aSJosh Poimboeuf 	    struct pt_regs *regs, struct warn_args *args)
5150f6f49a8SLinus Torvalds {
516de7edd31SSteven Rostedt (Red Hat) 	disable_trace_on_warning();
517de7edd31SSteven Rostedt (Red Hat) 
518dcb6b452SAlex Thorlton 	pr_warn("------------[ cut here ]------------\n");
5192553b67aSJosh Poimboeuf 
5202553b67aSJosh Poimboeuf 	if (file)
5212553b67aSJosh Poimboeuf 		pr_warn("WARNING: CPU: %d PID: %d at %s:%d %pS\n",
5222553b67aSJosh Poimboeuf 			raw_smp_processor_id(), current->pid, file, line,
5232553b67aSJosh Poimboeuf 			caller);
5242553b67aSJosh Poimboeuf 	else
5252553b67aSJosh Poimboeuf 		pr_warn("WARNING: CPU: %d PID: %d at %pS\n",
5262553b67aSJosh Poimboeuf 			raw_smp_processor_id(), current->pid, caller);
52774853dbaSArjan van de Ven 
5280f6f49a8SLinus Torvalds 	if (args)
5290f6f49a8SLinus Torvalds 		vprintk(args->fmt, args->args);
530a8f18b90SArjan van de Ven 
5319e3961a0SPrarit Bhargava 	if (panic_on_warn) {
5329e3961a0SPrarit Bhargava 		/*
5339e3961a0SPrarit Bhargava 		 * This thread may hit another WARN() in the panic path.
5349e3961a0SPrarit Bhargava 		 * Resetting this prevents additional WARN() from panicking the
5359e3961a0SPrarit Bhargava 		 * system on this thread.  Other threads are blocked by the
5369e3961a0SPrarit Bhargava 		 * panic_mutex in panic().
5379e3961a0SPrarit Bhargava 		 */
5389e3961a0SPrarit Bhargava 		panic_on_warn = 0;
5399e3961a0SPrarit Bhargava 		panic("panic_on_warn set ...\n");
5409e3961a0SPrarit Bhargava 	}
5419e3961a0SPrarit Bhargava 
542a8f18b90SArjan van de Ven 	print_modules();
5432553b67aSJosh Poimboeuf 
5442553b67aSJosh Poimboeuf 	if (regs)
5452553b67aSJosh Poimboeuf 		show_regs(regs);
5462553b67aSJosh Poimboeuf 	else
547a8f18b90SArjan van de Ven 		dump_stack();
5482553b67aSJosh Poimboeuf 
549a8f18b90SArjan van de Ven 	print_oops_end_marker();
5502553b67aSJosh Poimboeuf 
551373d4d09SRusty Russell 	/* Just a warning, don't kill lockdep. */
552373d4d09SRusty Russell 	add_taint(taint, LOCKDEP_STILL_OK);
553a8f18b90SArjan van de Ven }
5540f6f49a8SLinus Torvalds 
5552553b67aSJosh Poimboeuf #ifdef WANT_WARN_ON_SLOWPATH
5560f6f49a8SLinus Torvalds void warn_slowpath_fmt(const char *file, int line, const char *fmt, ...)
5570f6f49a8SLinus Torvalds {
5582553b67aSJosh Poimboeuf 	struct warn_args args;
5590f6f49a8SLinus Torvalds 
5600f6f49a8SLinus Torvalds 	args.fmt = fmt;
5610f6f49a8SLinus Torvalds 	va_start(args.args, fmt);
5622553b67aSJosh Poimboeuf 	__warn(file, line, __builtin_return_address(0), TAINT_WARN, NULL,
5632553b67aSJosh Poimboeuf 	       &args);
5640f6f49a8SLinus Torvalds 	va_end(args.args);
5650f6f49a8SLinus Torvalds }
56657adc4d2SAndi Kleen EXPORT_SYMBOL(warn_slowpath_fmt);
56757adc4d2SAndi Kleen 
568b2be0527SBen Hutchings void warn_slowpath_fmt_taint(const char *file, int line,
569b2be0527SBen Hutchings 			     unsigned taint, const char *fmt, ...)
570b2be0527SBen Hutchings {
5712553b67aSJosh Poimboeuf 	struct warn_args args;
572b2be0527SBen Hutchings 
573b2be0527SBen Hutchings 	args.fmt = fmt;
574b2be0527SBen Hutchings 	va_start(args.args, fmt);
5752553b67aSJosh Poimboeuf 	__warn(file, line, __builtin_return_address(0), taint, NULL, &args);
576b2be0527SBen Hutchings 	va_end(args.args);
577b2be0527SBen Hutchings }
578b2be0527SBen Hutchings EXPORT_SYMBOL(warn_slowpath_fmt_taint);
579b2be0527SBen Hutchings 
58057adc4d2SAndi Kleen void warn_slowpath_null(const char *file, int line)
58157adc4d2SAndi Kleen {
5822553b67aSJosh Poimboeuf 	__warn(file, line, __builtin_return_address(0), TAINT_WARN, NULL, NULL);
58357adc4d2SAndi Kleen }
58457adc4d2SAndi Kleen EXPORT_SYMBOL(warn_slowpath_null);
58579b4cc5eSArjan van de Ven #endif
58679b4cc5eSArjan van de Ven 
5873162f751SArjan van de Ven #ifdef CONFIG_CC_STACKPROTECTOR
58854371a43SArjan van de Ven 
5893162f751SArjan van de Ven /*
5903162f751SArjan van de Ven  * Called when gcc's -fstack-protector feature is used, and
5913162f751SArjan van de Ven  * gcc detects corruption of the on-stack canary value
5923162f751SArjan van de Ven  */
593a7330c99SAndi Kleen __visible void __stack_chk_fail(void)
5943162f751SArjan van de Ven {
595517a92c4SIngo Molnar 	panic("stack-protector: Kernel stack is corrupted in: %p\n",
596517a92c4SIngo Molnar 		__builtin_return_address(0));
5973162f751SArjan van de Ven }
5983162f751SArjan van de Ven EXPORT_SYMBOL(__stack_chk_fail);
59954371a43SArjan van de Ven 
6003162f751SArjan van de Ven #endif
601f44dd164SRusty Russell 
602f44dd164SRusty Russell core_param(panic, panic_timeout, int, 0644);
603f44dd164SRusty Russell core_param(pause_on_oops, pause_on_oops, int, 0644);
6049e3961a0SPrarit Bhargava core_param(panic_on_warn, panic_on_warn, int, 0644);
605b26e27ddSHidehiro Kawai core_param(crash_kexec_post_notifiers, crash_kexec_post_notifiers, bool, 0644);
606f06e5153SMasami Hiramatsu 
607d404ab0aSOlaf Hering static int __init oops_setup(char *s)
608d404ab0aSOlaf Hering {
609d404ab0aSOlaf Hering 	if (!s)
610d404ab0aSOlaf Hering 		return -EINVAL;
611d404ab0aSOlaf Hering 	if (!strcmp(s, "panic"))
612d404ab0aSOlaf Hering 		panic_on_oops = 1;
613d404ab0aSOlaf Hering 	return 0;
614d404ab0aSOlaf Hering }
615d404ab0aSOlaf Hering early_param("oops", oops_setup);
616