xref: /linux-6.15/kernel/panic.c (revision cf9b1106)
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 
741717f209SHidehiro Kawai atomic_t panic_cpu = ATOMIC_INIT(PANIC_CPU_INVALID);
751717f209SHidehiro Kawai 
76ebc41f20SHidehiro Kawai /*
77ebc41f20SHidehiro Kawai  * A variant of panic() called from NMI context. We return if we've already
78ebc41f20SHidehiro Kawai  * panicked on this CPU. If another CPU already panicked, loop in
79ebc41f20SHidehiro Kawai  * nmi_panic_self_stop() which can provide architecture dependent code such
80ebc41f20SHidehiro Kawai  * as saving register state for crash dump.
81ebc41f20SHidehiro Kawai  */
82ebc41f20SHidehiro Kawai void nmi_panic(struct pt_regs *regs, const char *msg)
83ebc41f20SHidehiro Kawai {
84ebc41f20SHidehiro Kawai 	int old_cpu, cpu;
85ebc41f20SHidehiro Kawai 
86ebc41f20SHidehiro Kawai 	cpu = raw_smp_processor_id();
87ebc41f20SHidehiro Kawai 	old_cpu = atomic_cmpxchg(&panic_cpu, PANIC_CPU_INVALID, cpu);
88ebc41f20SHidehiro Kawai 
89ebc41f20SHidehiro Kawai 	if (old_cpu == PANIC_CPU_INVALID)
90ebc41f20SHidehiro Kawai 		panic("%s", msg);
91ebc41f20SHidehiro Kawai 	else if (old_cpu != cpu)
92ebc41f20SHidehiro Kawai 		nmi_panic_self_stop(regs);
93ebc41f20SHidehiro Kawai }
94ebc41f20SHidehiro Kawai EXPORT_SYMBOL(nmi_panic);
95ebc41f20SHidehiro Kawai 
961da177e4SLinus Torvalds /**
971da177e4SLinus Torvalds  *	panic - halt the system
981da177e4SLinus Torvalds  *	@fmt: The text string to print
991da177e4SLinus Torvalds  *
1001da177e4SLinus Torvalds  *	Display a message, then perform cleanups.
1011da177e4SLinus Torvalds  *
1021da177e4SLinus Torvalds  *	This function never returns.
1031da177e4SLinus Torvalds  */
1049402c95fSJoe Perches void panic(const char *fmt, ...)
1051da177e4SLinus Torvalds {
1061da177e4SLinus Torvalds 	static char buf[1024];
1071da177e4SLinus Torvalds 	va_list args;
108c7ff0d9cSTAMUKI Shoichi 	long i, i_next = 0;
109c7ff0d9cSTAMUKI Shoichi 	int state = 0;
1101717f209SHidehiro Kawai 	int old_cpu, this_cpu;
1111da177e4SLinus Torvalds 
112dc009d92SEric W. Biederman 	/*
113190320c3SVikram Mulukutla 	 * Disable local interrupts. This will prevent panic_smp_self_stop
114190320c3SVikram Mulukutla 	 * from deadlocking the first cpu that invokes the panic, since
115190320c3SVikram Mulukutla 	 * there is nothing to prevent an interrupt handler (that runs
1161717f209SHidehiro Kawai 	 * after setting panic_cpu) from invoking panic() again.
117190320c3SVikram Mulukutla 	 */
118190320c3SVikram Mulukutla 	local_irq_disable();
119190320c3SVikram Mulukutla 
120190320c3SVikram Mulukutla 	/*
121c95dbf27SIngo Molnar 	 * It's possible to come here directly from a panic-assertion and
122c95dbf27SIngo Molnar 	 * not have preempt disabled. Some functions called from here want
123dc009d92SEric W. Biederman 	 * preempt to be disabled. No point enabling it later though...
12493e13a36SMichael Holzheu 	 *
12593e13a36SMichael Holzheu 	 * Only one CPU is allowed to execute the panic code from here. For
12693e13a36SMichael Holzheu 	 * multiple parallel invocations of panic, all other CPUs either
12793e13a36SMichael Holzheu 	 * stop themself or will wait until they are stopped by the 1st CPU
12893e13a36SMichael Holzheu 	 * with smp_send_stop().
1291717f209SHidehiro Kawai 	 *
1301717f209SHidehiro Kawai 	 * `old_cpu == PANIC_CPU_INVALID' means this is the 1st CPU which
1311717f209SHidehiro Kawai 	 * comes here, so go ahead.
1321717f209SHidehiro Kawai 	 * `old_cpu == this_cpu' means we came from nmi_panic() which sets
1331717f209SHidehiro Kawai 	 * panic_cpu to this CPU.  In this case, this is also the 1st CPU.
134dc009d92SEric W. Biederman 	 */
1351717f209SHidehiro Kawai 	this_cpu = raw_smp_processor_id();
1361717f209SHidehiro Kawai 	old_cpu  = atomic_cmpxchg(&panic_cpu, PANIC_CPU_INVALID, this_cpu);
1371717f209SHidehiro Kawai 
1381717f209SHidehiro Kawai 	if (old_cpu != PANIC_CPU_INVALID && old_cpu != this_cpu)
13993e13a36SMichael Holzheu 		panic_smp_self_stop();
140dc009d92SEric W. Biederman 
1415b530fc1SAnton Blanchard 	console_verbose();
1421da177e4SLinus Torvalds 	bust_spinlocks(1);
1431da177e4SLinus Torvalds 	va_start(args, fmt);
1441da177e4SLinus Torvalds 	vsnprintf(buf, sizeof(buf), fmt, args);
1451da177e4SLinus Torvalds 	va_end(args);
146d7c0847fSFabian Frederick 	pr_emerg("Kernel panic - not syncing: %s\n", buf);
1475cb27301SIngo Molnar #ifdef CONFIG_DEBUG_BUGVERBOSE
1486e6f0a1fSAndi Kleen 	/*
1496e6f0a1fSAndi Kleen 	 * Avoid nested stack-dumping if a panic occurs during oops processing
1506e6f0a1fSAndi Kleen 	 */
151026ee1f6SJason Wessel 	if (!test_taint(TAINT_DIE) && oops_in_progress <= 1)
1525cb27301SIngo Molnar 		dump_stack();
1535cb27301SIngo Molnar #endif
1541da177e4SLinus Torvalds 
155dc009d92SEric W. Biederman 	/*
156dc009d92SEric W. Biederman 	 * If we have crashed and we have a crash kernel loaded let it handle
157dc009d92SEric W. Biederman 	 * everything else.
158f06e5153SMasami Hiramatsu 	 * If we want to run this after calling panic_notifiers, pass
159f06e5153SMasami Hiramatsu 	 * the "crash_kexec_post_notifiers" option to the kernel.
1607bbee5caSHidehiro Kawai 	 *
1617bbee5caSHidehiro Kawai 	 * Bypass the panic_cpu check and call __crash_kexec directly.
162dc009d92SEric W. Biederman 	 */
163*cf9b1106SPetr Mladek 	if (!crash_kexec_post_notifiers) {
164*cf9b1106SPetr Mladek 		printk_nmi_flush_on_panic();
1657bbee5caSHidehiro Kawai 		__crash_kexec(NULL);
166*cf9b1106SPetr Mladek 	}
167dc009d92SEric W. Biederman 
168dc009d92SEric W. Biederman 	/*
169dc009d92SEric W. Biederman 	 * Note smp_send_stop is the usual smp shutdown function, which
170dc009d92SEric W. Biederman 	 * unfortunately means it may not be hardened to work in a panic
171dc009d92SEric W. Biederman 	 * situation.
172dc009d92SEric W. Biederman 	 */
1731da177e4SLinus Torvalds 	smp_send_stop();
1741da177e4SLinus Torvalds 
1756723734cSKees Cook 	/*
1766723734cSKees Cook 	 * Run any panic handlers, including those that might need to
1776723734cSKees Cook 	 * add information to the kmsg dump output.
1786723734cSKees Cook 	 */
179e041c683SAlan Stern 	atomic_notifier_call_chain(&panic_notifier_list, 0, buf);
1801da177e4SLinus Torvalds 
181*cf9b1106SPetr Mladek 	/* Call flush even twice. It tries harder with a single online CPU */
182*cf9b1106SPetr Mladek 	printk_nmi_flush_on_panic();
1836723734cSKees Cook 	kmsg_dump(KMSG_DUMP_PANIC);
1846723734cSKees Cook 
185f06e5153SMasami Hiramatsu 	/*
186f06e5153SMasami Hiramatsu 	 * If you doubt kdump always works fine in any situation,
187f06e5153SMasami Hiramatsu 	 * "crash_kexec_post_notifiers" offers you a chance to run
188f06e5153SMasami Hiramatsu 	 * panic_notifiers and dumping kmsg before kdump.
189f06e5153SMasami Hiramatsu 	 * Note: since some panic_notifiers can make crashed kernel
190f06e5153SMasami Hiramatsu 	 * more unstable, it can increase risks of the kdump failure too.
1917bbee5caSHidehiro Kawai 	 *
1927bbee5caSHidehiro Kawai 	 * Bypass the panic_cpu check and call __crash_kexec directly.
193f06e5153SMasami Hiramatsu 	 */
194f45d85ffSHATAYAMA Daisuke 	if (crash_kexec_post_notifiers)
1957bbee5caSHidehiro Kawai 		__crash_kexec(NULL);
196f06e5153SMasami Hiramatsu 
197d014e889SAaro Koskinen 	bust_spinlocks(0);
198d014e889SAaro Koskinen 
19908d78658SVitaly Kuznetsov 	/*
20008d78658SVitaly Kuznetsov 	 * We may have ended up stopping the CPU holding the lock (in
20108d78658SVitaly Kuznetsov 	 * smp_send_stop()) while still having some valuable data in the console
20208d78658SVitaly Kuznetsov 	 * buffer.  Try to acquire the lock then release it regardless of the
2037625b3a0SVitaly Kuznetsov 	 * result.  The release will also print the buffers out.  Locks debug
2047625b3a0SVitaly Kuznetsov 	 * should be disabled to avoid reporting bad unlock balance when
2057625b3a0SVitaly Kuznetsov 	 * panic() is not being callled from OOPS.
20608d78658SVitaly Kuznetsov 	 */
2077625b3a0SVitaly Kuznetsov 	debug_locks_off();
2088d91f8b1STejun Heo 	console_flush_on_panic();
20908d78658SVitaly Kuznetsov 
210c7ff0d9cSTAMUKI Shoichi 	if (!panic_blink)
211c7ff0d9cSTAMUKI Shoichi 		panic_blink = no_blink;
212c7ff0d9cSTAMUKI Shoichi 
213dc009d92SEric W. Biederman 	if (panic_timeout > 0) {
2141da177e4SLinus Torvalds 		/*
2151da177e4SLinus Torvalds 		 * Delay timeout seconds before rebooting the machine.
216c95dbf27SIngo Molnar 		 * We can't use the "normal" timers since we just panicked.
2171da177e4SLinus Torvalds 		 */
218d7c0847fSFabian Frederick 		pr_emerg("Rebooting in %d seconds..", panic_timeout);
219c95dbf27SIngo Molnar 
220c7ff0d9cSTAMUKI Shoichi 		for (i = 0; i < panic_timeout * 1000; i += PANIC_TIMER_STEP) {
2211da177e4SLinus Torvalds 			touch_nmi_watchdog();
222c7ff0d9cSTAMUKI Shoichi 			if (i >= i_next) {
223c7ff0d9cSTAMUKI Shoichi 				i += panic_blink(state ^= 1);
224c7ff0d9cSTAMUKI Shoichi 				i_next = i + 3600 / PANIC_BLINK_SPD;
225c7ff0d9cSTAMUKI Shoichi 			}
226c7ff0d9cSTAMUKI Shoichi 			mdelay(PANIC_TIMER_STEP);
2271da177e4SLinus Torvalds 		}
2284302fbc8SHugh Dickins 	}
2294302fbc8SHugh Dickins 	if (panic_timeout != 0) {
230c95dbf27SIngo Molnar 		/*
231c95dbf27SIngo Molnar 		 * This will not be a clean reboot, with everything
2322f048ea8SEric W. Biederman 		 * shutting down.  But if there is a chance of
2332f048ea8SEric W. Biederman 		 * rebooting the system it will be rebooted.
2341da177e4SLinus Torvalds 		 */
2352f048ea8SEric W. Biederman 		emergency_restart();
2361da177e4SLinus Torvalds 	}
2371da177e4SLinus Torvalds #ifdef __sparc__
2381da177e4SLinus Torvalds 	{
2391da177e4SLinus Torvalds 		extern int stop_a_enabled;
240a271c241STom 'spot' Callaway 		/* Make sure the user can actually press Stop-A (L1-A) */
2411da177e4SLinus Torvalds 		stop_a_enabled = 1;
242d7c0847fSFabian Frederick 		pr_emerg("Press Stop-A (L1-A) to return to the boot prom\n");
2431da177e4SLinus Torvalds 	}
2441da177e4SLinus Torvalds #endif
245347a8dc3SMartin Schwidefsky #if defined(CONFIG_S390)
246c95dbf27SIngo Molnar 	{
247c95dbf27SIngo Molnar 		unsigned long caller;
248c95dbf27SIngo Molnar 
249c95dbf27SIngo Molnar 		caller = (unsigned long)__builtin_return_address(0);
2501da177e4SLinus Torvalds 		disabled_wait(caller);
251c95dbf27SIngo Molnar 	}
2521da177e4SLinus Torvalds #endif
253d7c0847fSFabian Frederick 	pr_emerg("---[ end Kernel panic - not syncing: %s\n", buf);
2541da177e4SLinus Torvalds 	local_irq_enable();
255c7ff0d9cSTAMUKI Shoichi 	for (i = 0; ; i += PANIC_TIMER_STEP) {
256c22db941SJan Beulich 		touch_softlockup_watchdog();
257c7ff0d9cSTAMUKI Shoichi 		if (i >= i_next) {
258c7ff0d9cSTAMUKI Shoichi 			i += panic_blink(state ^= 1);
259c7ff0d9cSTAMUKI Shoichi 			i_next = i + 3600 / PANIC_BLINK_SPD;
260c7ff0d9cSTAMUKI Shoichi 		}
261c7ff0d9cSTAMUKI Shoichi 		mdelay(PANIC_TIMER_STEP);
2621da177e4SLinus Torvalds 	}
2631da177e4SLinus Torvalds }
2641da177e4SLinus Torvalds 
2651da177e4SLinus Torvalds EXPORT_SYMBOL(panic);
2661da177e4SLinus Torvalds 
2671da177e4SLinus Torvalds 
26825ddbb18SAndi Kleen struct tnt {
26925ddbb18SAndi Kleen 	u8	bit;
27025ddbb18SAndi Kleen 	char	true;
27125ddbb18SAndi Kleen 	char	false;
27225ddbb18SAndi Kleen };
27325ddbb18SAndi Kleen 
27425ddbb18SAndi Kleen static const struct tnt tnts[] = {
27525ddbb18SAndi Kleen 	{ TAINT_PROPRIETARY_MODULE,	'P', 'G' },
27625ddbb18SAndi Kleen 	{ TAINT_FORCED_MODULE,		'F', ' ' },
2778c90487cSDave Jones 	{ TAINT_CPU_OUT_OF_SPEC,	'S', ' ' },
27825ddbb18SAndi Kleen 	{ TAINT_FORCED_RMMOD,		'R', ' ' },
27925ddbb18SAndi Kleen 	{ TAINT_MACHINE_CHECK,		'M', ' ' },
28025ddbb18SAndi Kleen 	{ TAINT_BAD_PAGE,		'B', ' ' },
28125ddbb18SAndi Kleen 	{ TAINT_USER,			'U', ' ' },
28225ddbb18SAndi Kleen 	{ TAINT_DIE,			'D', ' ' },
28325ddbb18SAndi Kleen 	{ TAINT_OVERRIDDEN_ACPI_TABLE,	'A', ' ' },
28425ddbb18SAndi Kleen 	{ TAINT_WARN,			'W', ' ' },
28526e9a397SLinus Torvalds 	{ TAINT_CRAP,			'C', ' ' },
28692946bc7SBen Hutchings 	{ TAINT_FIRMWARE_WORKAROUND,	'I', ' ' },
2872449b8baSBen Hutchings 	{ TAINT_OOT_MODULE,		'O', ' ' },
28857673c2bSRusty Russell 	{ TAINT_UNSIGNED_MODULE,	'E', ' ' },
28969361eefSJosh Hunt 	{ TAINT_SOFTLOCKUP,		'L', ' ' },
290c5f45465SSeth Jennings 	{ TAINT_LIVEPATCH,		'K', ' ' },
29125ddbb18SAndi Kleen };
29225ddbb18SAndi Kleen 
2931da177e4SLinus Torvalds /**
2941da177e4SLinus Torvalds  *	print_tainted - return a string to represent the kernel taint state.
2951da177e4SLinus Torvalds  *
2961da177e4SLinus Torvalds  *  'P' - Proprietary module has been loaded.
2971da177e4SLinus Torvalds  *  'F' - Module has been forcibly loaded.
2981da177e4SLinus Torvalds  *  'S' - SMP with CPUs not designed for SMP.
2991da177e4SLinus Torvalds  *  'R' - User forced a module unload.
3001da177e4SLinus Torvalds  *  'M' - System experienced a machine check exception.
3011da177e4SLinus Torvalds  *  'B' - System has hit bad_page.
3021da177e4SLinus Torvalds  *  'U' - Userspace-defined naughtiness.
303a8005992SArjan van de Ven  *  'D' - Kernel has oopsed before
3041da177e4SLinus Torvalds  *  'A' - ACPI table overridden.
3051da177e4SLinus Torvalds  *  'W' - Taint on warning.
306061b1bd3SGreg Kroah-Hartman  *  'C' - modules from drivers/staging are loaded.
30792946bc7SBen Hutchings  *  'I' - Working around severe firmware bug.
3082449b8baSBen Hutchings  *  'O' - Out-of-tree module has been loaded.
30957673c2bSRusty Russell  *  'E' - Unsigned module has been loaded.
310bc53a3f4SXie XiuQi  *  'L' - A soft lockup has previously occurred.
311c5f45465SSeth Jennings  *  'K' - Kernel has been live patched.
3121da177e4SLinus Torvalds  *
313fe002a41SRobert P. J. Day  *	The string is overwritten by the next call to print_tainted().
3141da177e4SLinus Torvalds  */
3151da177e4SLinus Torvalds const char *print_tainted(void)
3161da177e4SLinus Torvalds {
31701284764SChen Gang 	static char buf[ARRAY_SIZE(tnts) + sizeof("Tainted: ")];
31825ddbb18SAndi Kleen 
31925ddbb18SAndi Kleen 	if (tainted_mask) {
32025ddbb18SAndi Kleen 		char *s;
32125ddbb18SAndi Kleen 		int i;
32225ddbb18SAndi Kleen 
32325ddbb18SAndi Kleen 		s = buf + sprintf(buf, "Tainted: ");
32425ddbb18SAndi Kleen 		for (i = 0; i < ARRAY_SIZE(tnts); i++) {
32525ddbb18SAndi Kleen 			const struct tnt *t = &tnts[i];
32625ddbb18SAndi Kleen 			*s++ = test_bit(t->bit, &tainted_mask) ?
32725ddbb18SAndi Kleen 					t->true : t->false;
3281da177e4SLinus Torvalds 		}
32925ddbb18SAndi Kleen 		*s = 0;
33025ddbb18SAndi Kleen 	} else
3311da177e4SLinus Torvalds 		snprintf(buf, sizeof(buf), "Not tainted");
332c95dbf27SIngo Molnar 
333c95dbf27SIngo Molnar 	return buf;
3341da177e4SLinus Torvalds }
3351da177e4SLinus Torvalds 
33625ddbb18SAndi Kleen int test_taint(unsigned flag)
33725ddbb18SAndi Kleen {
33825ddbb18SAndi Kleen 	return test_bit(flag, &tainted_mask);
33925ddbb18SAndi Kleen }
34025ddbb18SAndi Kleen EXPORT_SYMBOL(test_taint);
34125ddbb18SAndi Kleen 
34225ddbb18SAndi Kleen unsigned long get_taint(void)
34325ddbb18SAndi Kleen {
34425ddbb18SAndi Kleen 	return tainted_mask;
34525ddbb18SAndi Kleen }
34625ddbb18SAndi Kleen 
347373d4d09SRusty Russell /**
348373d4d09SRusty Russell  * add_taint: add a taint flag if not already set.
349373d4d09SRusty Russell  * @flag: one of the TAINT_* constants.
350373d4d09SRusty Russell  * @lockdep_ok: whether lock debugging is still OK.
351373d4d09SRusty Russell  *
352373d4d09SRusty Russell  * If something bad has gone wrong, you'll want @lockdebug_ok = false, but for
353373d4d09SRusty Russell  * some notewortht-but-not-corrupting cases, it can be set to true.
3549eeba613SFrederic Weisbecker  */
355373d4d09SRusty Russell void add_taint(unsigned flag, enum lockdep_ok lockdep_ok)
356373d4d09SRusty Russell {
357373d4d09SRusty Russell 	if (lockdep_ok == LOCKDEP_NOW_UNRELIABLE && __debug_locks_off())
358d7c0847fSFabian Frederick 		pr_warn("Disabling lock debugging due to kernel taint\n");
3599eeba613SFrederic Weisbecker 
36025ddbb18SAndi Kleen 	set_bit(flag, &tainted_mask);
3611da177e4SLinus Torvalds }
3621da177e4SLinus Torvalds EXPORT_SYMBOL(add_taint);
363dd287796SAndrew Morton 
364dd287796SAndrew Morton static void spin_msec(int msecs)
365dd287796SAndrew Morton {
366dd287796SAndrew Morton 	int i;
367dd287796SAndrew Morton 
368dd287796SAndrew Morton 	for (i = 0; i < msecs; i++) {
369dd287796SAndrew Morton 		touch_nmi_watchdog();
370dd287796SAndrew Morton 		mdelay(1);
371dd287796SAndrew Morton 	}
372dd287796SAndrew Morton }
373dd287796SAndrew Morton 
374dd287796SAndrew Morton /*
375dd287796SAndrew Morton  * It just happens that oops_enter() and oops_exit() are identically
376dd287796SAndrew Morton  * implemented...
377dd287796SAndrew Morton  */
378dd287796SAndrew Morton static void do_oops_enter_exit(void)
379dd287796SAndrew Morton {
380dd287796SAndrew Morton 	unsigned long flags;
381dd287796SAndrew Morton 	static int spin_counter;
382dd287796SAndrew Morton 
383dd287796SAndrew Morton 	if (!pause_on_oops)
384dd287796SAndrew Morton 		return;
385dd287796SAndrew Morton 
386dd287796SAndrew Morton 	spin_lock_irqsave(&pause_on_oops_lock, flags);
387dd287796SAndrew Morton 	if (pause_on_oops_flag == 0) {
388dd287796SAndrew Morton 		/* This CPU may now print the oops message */
389dd287796SAndrew Morton 		pause_on_oops_flag = 1;
390dd287796SAndrew Morton 	} else {
391dd287796SAndrew Morton 		/* We need to stall this CPU */
392dd287796SAndrew Morton 		if (!spin_counter) {
393dd287796SAndrew Morton 			/* This CPU gets to do the counting */
394dd287796SAndrew Morton 			spin_counter = pause_on_oops;
395dd287796SAndrew Morton 			do {
396dd287796SAndrew Morton 				spin_unlock(&pause_on_oops_lock);
397dd287796SAndrew Morton 				spin_msec(MSEC_PER_SEC);
398dd287796SAndrew Morton 				spin_lock(&pause_on_oops_lock);
399dd287796SAndrew Morton 			} while (--spin_counter);
400dd287796SAndrew Morton 			pause_on_oops_flag = 0;
401dd287796SAndrew Morton 		} else {
402dd287796SAndrew Morton 			/* This CPU waits for a different one */
403dd287796SAndrew Morton 			while (spin_counter) {
404dd287796SAndrew Morton 				spin_unlock(&pause_on_oops_lock);
405dd287796SAndrew Morton 				spin_msec(1);
406dd287796SAndrew Morton 				spin_lock(&pause_on_oops_lock);
407dd287796SAndrew Morton 			}
408dd287796SAndrew Morton 		}
409dd287796SAndrew Morton 	}
410dd287796SAndrew Morton 	spin_unlock_irqrestore(&pause_on_oops_lock, flags);
411dd287796SAndrew Morton }
412dd287796SAndrew Morton 
413dd287796SAndrew Morton /*
414c95dbf27SIngo Molnar  * Return true if the calling CPU is allowed to print oops-related info.
415c95dbf27SIngo Molnar  * This is a bit racy..
416dd287796SAndrew Morton  */
417dd287796SAndrew Morton int oops_may_print(void)
418dd287796SAndrew Morton {
419dd287796SAndrew Morton 	return pause_on_oops_flag == 0;
420dd287796SAndrew Morton }
421dd287796SAndrew Morton 
422dd287796SAndrew Morton /*
423dd287796SAndrew Morton  * Called when the architecture enters its oops handler, before it prints
424c95dbf27SIngo Molnar  * anything.  If this is the first CPU to oops, and it's oopsing the first
425c95dbf27SIngo Molnar  * time then let it proceed.
426dd287796SAndrew Morton  *
427c95dbf27SIngo Molnar  * This is all enabled by the pause_on_oops kernel boot option.  We do all
428c95dbf27SIngo Molnar  * this to ensure that oopses don't scroll off the screen.  It has the
429c95dbf27SIngo Molnar  * side-effect of preventing later-oopsing CPUs from mucking up the display,
430c95dbf27SIngo Molnar  * too.
431dd287796SAndrew Morton  *
432c95dbf27SIngo Molnar  * It turns out that the CPU which is allowed to print ends up pausing for
433c95dbf27SIngo Molnar  * the right duration, whereas all the other CPUs pause for twice as long:
434c95dbf27SIngo Molnar  * once in oops_enter(), once in oops_exit().
435dd287796SAndrew Morton  */
436dd287796SAndrew Morton void oops_enter(void)
437dd287796SAndrew Morton {
438bdff7870SThomas Gleixner 	tracing_off();
439c95dbf27SIngo Molnar 	/* can't trust the integrity of the kernel anymore: */
440c95dbf27SIngo Molnar 	debug_locks_off();
441dd287796SAndrew Morton 	do_oops_enter_exit();
442dd287796SAndrew Morton }
443dd287796SAndrew Morton 
444dd287796SAndrew Morton /*
4452c3b20e9SArjan van de Ven  * 64-bit random ID for oopses:
4462c3b20e9SArjan van de Ven  */
4472c3b20e9SArjan van de Ven static u64 oops_id;
4482c3b20e9SArjan van de Ven 
4492c3b20e9SArjan van de Ven static int init_oops_id(void)
4502c3b20e9SArjan van de Ven {
4512c3b20e9SArjan van de Ven 	if (!oops_id)
4522c3b20e9SArjan van de Ven 		get_random_bytes(&oops_id, sizeof(oops_id));
453d6624f99SArjan van de Ven 	else
454d6624f99SArjan van de Ven 		oops_id++;
4552c3b20e9SArjan van de Ven 
4562c3b20e9SArjan van de Ven 	return 0;
4572c3b20e9SArjan van de Ven }
4582c3b20e9SArjan van de Ven late_initcall(init_oops_id);
4592c3b20e9SArjan van de Ven 
460863a6049SAnton Blanchard void print_oops_end_marker(void)
46171c33911SArjan van de Ven {
46271c33911SArjan van de Ven 	init_oops_id();
463d7c0847fSFabian Frederick 	pr_warn("---[ end trace %016llx ]---\n", (unsigned long long)oops_id);
46471c33911SArjan van de Ven }
46571c33911SArjan van de Ven 
4662c3b20e9SArjan van de Ven /*
467dd287796SAndrew Morton  * Called when the architecture exits its oops handler, after printing
468dd287796SAndrew Morton  * everything.
469dd287796SAndrew Morton  */
470dd287796SAndrew Morton void oops_exit(void)
471dd287796SAndrew Morton {
472dd287796SAndrew Morton 	do_oops_enter_exit();
47371c33911SArjan van de Ven 	print_oops_end_marker();
474456b565cSSimon Kagstrom 	kmsg_dump(KMSG_DUMP_OOPS);
475dd287796SAndrew Morton }
4763162f751SArjan van de Ven 
4772553b67aSJosh Poimboeuf struct warn_args {
4780f6f49a8SLinus Torvalds 	const char *fmt;
479a8f18b90SArjan van de Ven 	va_list args;
4800f6f49a8SLinus Torvalds };
4810f6f49a8SLinus Torvalds 
4822553b67aSJosh Poimboeuf void __warn(const char *file, int line, void *caller, unsigned taint,
4832553b67aSJosh Poimboeuf 	    struct pt_regs *regs, struct warn_args *args)
4840f6f49a8SLinus Torvalds {
485de7edd31SSteven Rostedt (Red Hat) 	disable_trace_on_warning();
486de7edd31SSteven Rostedt (Red Hat) 
487dcb6b452SAlex Thorlton 	pr_warn("------------[ cut here ]------------\n");
4882553b67aSJosh Poimboeuf 
4892553b67aSJosh Poimboeuf 	if (file)
4902553b67aSJosh Poimboeuf 		pr_warn("WARNING: CPU: %d PID: %d at %s:%d %pS\n",
4912553b67aSJosh Poimboeuf 			raw_smp_processor_id(), current->pid, file, line,
4922553b67aSJosh Poimboeuf 			caller);
4932553b67aSJosh Poimboeuf 	else
4942553b67aSJosh Poimboeuf 		pr_warn("WARNING: CPU: %d PID: %d at %pS\n",
4952553b67aSJosh Poimboeuf 			raw_smp_processor_id(), current->pid, caller);
49674853dbaSArjan van de Ven 
4970f6f49a8SLinus Torvalds 	if (args)
4980f6f49a8SLinus Torvalds 		vprintk(args->fmt, args->args);
499a8f18b90SArjan van de Ven 
5009e3961a0SPrarit Bhargava 	if (panic_on_warn) {
5019e3961a0SPrarit Bhargava 		/*
5029e3961a0SPrarit Bhargava 		 * This thread may hit another WARN() in the panic path.
5039e3961a0SPrarit Bhargava 		 * Resetting this prevents additional WARN() from panicking the
5049e3961a0SPrarit Bhargava 		 * system on this thread.  Other threads are blocked by the
5059e3961a0SPrarit Bhargava 		 * panic_mutex in panic().
5069e3961a0SPrarit Bhargava 		 */
5079e3961a0SPrarit Bhargava 		panic_on_warn = 0;
5089e3961a0SPrarit Bhargava 		panic("panic_on_warn set ...\n");
5099e3961a0SPrarit Bhargava 	}
5109e3961a0SPrarit Bhargava 
511a8f18b90SArjan van de Ven 	print_modules();
5122553b67aSJosh Poimboeuf 
5132553b67aSJosh Poimboeuf 	if (regs)
5142553b67aSJosh Poimboeuf 		show_regs(regs);
5152553b67aSJosh Poimboeuf 	else
516a8f18b90SArjan van de Ven 		dump_stack();
5172553b67aSJosh Poimboeuf 
518a8f18b90SArjan van de Ven 	print_oops_end_marker();
5192553b67aSJosh Poimboeuf 
520373d4d09SRusty Russell 	/* Just a warning, don't kill lockdep. */
521373d4d09SRusty Russell 	add_taint(taint, LOCKDEP_STILL_OK);
522a8f18b90SArjan van de Ven }
5230f6f49a8SLinus Torvalds 
5242553b67aSJosh Poimboeuf #ifdef WANT_WARN_ON_SLOWPATH
5250f6f49a8SLinus Torvalds void warn_slowpath_fmt(const char *file, int line, const char *fmt, ...)
5260f6f49a8SLinus Torvalds {
5272553b67aSJosh Poimboeuf 	struct warn_args args;
5280f6f49a8SLinus Torvalds 
5290f6f49a8SLinus Torvalds 	args.fmt = fmt;
5300f6f49a8SLinus Torvalds 	va_start(args.args, fmt);
5312553b67aSJosh Poimboeuf 	__warn(file, line, __builtin_return_address(0), TAINT_WARN, NULL,
5322553b67aSJosh Poimboeuf 	       &args);
5330f6f49a8SLinus Torvalds 	va_end(args.args);
5340f6f49a8SLinus Torvalds }
53557adc4d2SAndi Kleen EXPORT_SYMBOL(warn_slowpath_fmt);
53657adc4d2SAndi Kleen 
537b2be0527SBen Hutchings void warn_slowpath_fmt_taint(const char *file, int line,
538b2be0527SBen Hutchings 			     unsigned taint, const char *fmt, ...)
539b2be0527SBen Hutchings {
5402553b67aSJosh Poimboeuf 	struct warn_args args;
541b2be0527SBen Hutchings 
542b2be0527SBen Hutchings 	args.fmt = fmt;
543b2be0527SBen Hutchings 	va_start(args.args, fmt);
5442553b67aSJosh Poimboeuf 	__warn(file, line, __builtin_return_address(0), taint, NULL, &args);
545b2be0527SBen Hutchings 	va_end(args.args);
546b2be0527SBen Hutchings }
547b2be0527SBen Hutchings EXPORT_SYMBOL(warn_slowpath_fmt_taint);
548b2be0527SBen Hutchings 
54957adc4d2SAndi Kleen void warn_slowpath_null(const char *file, int line)
55057adc4d2SAndi Kleen {
5512553b67aSJosh Poimboeuf 	__warn(file, line, __builtin_return_address(0), TAINT_WARN, NULL, NULL);
55257adc4d2SAndi Kleen }
55357adc4d2SAndi Kleen EXPORT_SYMBOL(warn_slowpath_null);
55479b4cc5eSArjan van de Ven #endif
55579b4cc5eSArjan van de Ven 
5563162f751SArjan van de Ven #ifdef CONFIG_CC_STACKPROTECTOR
55754371a43SArjan van de Ven 
5583162f751SArjan van de Ven /*
5593162f751SArjan van de Ven  * Called when gcc's -fstack-protector feature is used, and
5603162f751SArjan van de Ven  * gcc detects corruption of the on-stack canary value
5613162f751SArjan van de Ven  */
562a7330c99SAndi Kleen __visible void __stack_chk_fail(void)
5633162f751SArjan van de Ven {
564517a92c4SIngo Molnar 	panic("stack-protector: Kernel stack is corrupted in: %p\n",
565517a92c4SIngo Molnar 		__builtin_return_address(0));
5663162f751SArjan van de Ven }
5673162f751SArjan van de Ven EXPORT_SYMBOL(__stack_chk_fail);
56854371a43SArjan van de Ven 
5693162f751SArjan van de Ven #endif
570f44dd164SRusty Russell 
571f44dd164SRusty Russell core_param(panic, panic_timeout, int, 0644);
572f44dd164SRusty Russell core_param(pause_on_oops, pause_on_oops, int, 0644);
5739e3961a0SPrarit Bhargava core_param(panic_on_warn, panic_on_warn, int, 0644);
574d404ab0aSOlaf Hering 
575f06e5153SMasami Hiramatsu static int __init setup_crash_kexec_post_notifiers(char *s)
576f06e5153SMasami Hiramatsu {
577f06e5153SMasami Hiramatsu 	crash_kexec_post_notifiers = true;
578f06e5153SMasami Hiramatsu 	return 0;
579f06e5153SMasami Hiramatsu }
580f06e5153SMasami Hiramatsu early_param("crash_kexec_post_notifiers", setup_crash_kexec_post_notifiers);
581f06e5153SMasami Hiramatsu 
582d404ab0aSOlaf Hering static int __init oops_setup(char *s)
583d404ab0aSOlaf Hering {
584d404ab0aSOlaf Hering 	if (!s)
585d404ab0aSOlaf Hering 		return -EINVAL;
586d404ab0aSOlaf Hering 	if (!strcmp(s, "panic"))
587d404ab0aSOlaf Hering 		panic_on_oops = 1;
588d404ab0aSOlaf Hering 	return 0;
589d404ab0aSOlaf Hering }
590d404ab0aSOlaf Hering early_param("oops", oops_setup);
591