xref: /linux-6.15/kernel/panic.c (revision bc53a3f4)
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>
261da177e4SLinus Torvalds 
27c7ff0d9cSTAMUKI Shoichi #define PANIC_TIMER_STEP 100
28c7ff0d9cSTAMUKI Shoichi #define PANIC_BLINK_SPD 18
29c7ff0d9cSTAMUKI Shoichi 
302a01bb38SKyle McMartin int panic_on_oops = CONFIG_PANIC_ON_OOPS_VALUE;
3125ddbb18SAndi Kleen static unsigned long tainted_mask;
32dd287796SAndrew Morton static int pause_on_oops;
33dd287796SAndrew Morton static int pause_on_oops_flag;
34dd287796SAndrew Morton static DEFINE_SPINLOCK(pause_on_oops_lock);
35f06e5153SMasami Hiramatsu static bool crash_kexec_post_notifiers;
361da177e4SLinus Torvalds 
375800dc3cSJason Baron int panic_timeout = CONFIG_PANIC_TIMEOUT;
3881e88fdcSHuang Ying EXPORT_SYMBOL_GPL(panic_timeout);
391da177e4SLinus Torvalds 
40e041c683SAlan Stern ATOMIC_NOTIFIER_HEAD(panic_notifier_list);
411da177e4SLinus Torvalds 
421da177e4SLinus Torvalds EXPORT_SYMBOL(panic_notifier_list);
431da177e4SLinus Torvalds 
44c7ff0d9cSTAMUKI Shoichi static long no_blink(int state)
458aeee85aSAnton Blanchard {
46c7ff0d9cSTAMUKI Shoichi 	return 0;
47c7ff0d9cSTAMUKI Shoichi }
488aeee85aSAnton Blanchard 
49c7ff0d9cSTAMUKI Shoichi /* Returns how long it waited in ms */
50c7ff0d9cSTAMUKI Shoichi long (*panic_blink)(int state);
51c7ff0d9cSTAMUKI Shoichi EXPORT_SYMBOL(panic_blink);
528aeee85aSAnton Blanchard 
5393e13a36SMichael Holzheu /*
5493e13a36SMichael Holzheu  * Stop ourself in panic -- architecture code may override this
5593e13a36SMichael Holzheu  */
5693e13a36SMichael Holzheu void __weak panic_smp_self_stop(void)
5793e13a36SMichael Holzheu {
5893e13a36SMichael Holzheu 	while (1)
5993e13a36SMichael Holzheu 		cpu_relax();
6093e13a36SMichael Holzheu }
6193e13a36SMichael Holzheu 
621da177e4SLinus Torvalds /**
631da177e4SLinus Torvalds  *	panic - halt the system
641da177e4SLinus Torvalds  *	@fmt: The text string to print
651da177e4SLinus Torvalds  *
661da177e4SLinus Torvalds  *	Display a message, then perform cleanups.
671da177e4SLinus Torvalds  *
681da177e4SLinus Torvalds  *	This function never returns.
691da177e4SLinus Torvalds  */
709402c95fSJoe Perches void panic(const char *fmt, ...)
711da177e4SLinus Torvalds {
7293e13a36SMichael Holzheu 	static DEFINE_SPINLOCK(panic_lock);
731da177e4SLinus Torvalds 	static char buf[1024];
741da177e4SLinus Torvalds 	va_list args;
75c7ff0d9cSTAMUKI Shoichi 	long i, i_next = 0;
76c7ff0d9cSTAMUKI Shoichi 	int state = 0;
771da177e4SLinus Torvalds 
78dc009d92SEric W. Biederman 	/*
79190320c3SVikram Mulukutla 	 * Disable local interrupts. This will prevent panic_smp_self_stop
80190320c3SVikram Mulukutla 	 * from deadlocking the first cpu that invokes the panic, since
81190320c3SVikram Mulukutla 	 * there is nothing to prevent an interrupt handler (that runs
82190320c3SVikram Mulukutla 	 * after the panic_lock is acquired) from invoking panic again.
83190320c3SVikram Mulukutla 	 */
84190320c3SVikram Mulukutla 	local_irq_disable();
85190320c3SVikram Mulukutla 
86190320c3SVikram Mulukutla 	/*
87c95dbf27SIngo Molnar 	 * It's possible to come here directly from a panic-assertion and
88c95dbf27SIngo Molnar 	 * not have preempt disabled. Some functions called from here want
89dc009d92SEric W. Biederman 	 * preempt to be disabled. No point enabling it later though...
9093e13a36SMichael Holzheu 	 *
9193e13a36SMichael Holzheu 	 * Only one CPU is allowed to execute the panic code from here. For
9293e13a36SMichael Holzheu 	 * multiple parallel invocations of panic, all other CPUs either
9393e13a36SMichael Holzheu 	 * stop themself or will wait until they are stopped by the 1st CPU
9493e13a36SMichael Holzheu 	 * with smp_send_stop().
95dc009d92SEric W. Biederman 	 */
9693e13a36SMichael Holzheu 	if (!spin_trylock(&panic_lock))
9793e13a36SMichael Holzheu 		panic_smp_self_stop();
98dc009d92SEric W. Biederman 
995b530fc1SAnton Blanchard 	console_verbose();
1001da177e4SLinus Torvalds 	bust_spinlocks(1);
1011da177e4SLinus Torvalds 	va_start(args, fmt);
1021da177e4SLinus Torvalds 	vsnprintf(buf, sizeof(buf), fmt, args);
1031da177e4SLinus Torvalds 	va_end(args);
104d7c0847fSFabian Frederick 	pr_emerg("Kernel panic - not syncing: %s\n", buf);
1055cb27301SIngo Molnar #ifdef CONFIG_DEBUG_BUGVERBOSE
1066e6f0a1fSAndi Kleen 	/*
1076e6f0a1fSAndi Kleen 	 * Avoid nested stack-dumping if a panic occurs during oops processing
1086e6f0a1fSAndi Kleen 	 */
109026ee1f6SJason Wessel 	if (!test_taint(TAINT_DIE) && oops_in_progress <= 1)
1105cb27301SIngo Molnar 		dump_stack();
1115cb27301SIngo Molnar #endif
1121da177e4SLinus Torvalds 
113dc009d92SEric W. Biederman 	/*
114dc009d92SEric W. Biederman 	 * If we have crashed and we have a crash kernel loaded let it handle
115dc009d92SEric W. Biederman 	 * everything else.
116f06e5153SMasami Hiramatsu 	 * If we want to run this after calling panic_notifiers, pass
117f06e5153SMasami Hiramatsu 	 * the "crash_kexec_post_notifiers" option to the kernel.
118dc009d92SEric W. Biederman 	 */
119f06e5153SMasami Hiramatsu 	if (!crash_kexec_post_notifiers)
1206e274d14SAlexander Nyberg 		crash_kexec(NULL);
121dc009d92SEric W. Biederman 
122dc009d92SEric W. Biederman 	/*
123dc009d92SEric W. Biederman 	 * Note smp_send_stop is the usual smp shutdown function, which
124dc009d92SEric W. Biederman 	 * unfortunately means it may not be hardened to work in a panic
125dc009d92SEric W. Biederman 	 * situation.
126dc009d92SEric W. Biederman 	 */
1271da177e4SLinus Torvalds 	smp_send_stop();
1281da177e4SLinus Torvalds 
1296723734cSKees Cook 	/*
1306723734cSKees Cook 	 * Run any panic handlers, including those that might need to
1316723734cSKees Cook 	 * add information to the kmsg dump output.
1326723734cSKees Cook 	 */
133e041c683SAlan Stern 	atomic_notifier_call_chain(&panic_notifier_list, 0, buf);
1341da177e4SLinus Torvalds 
1356723734cSKees Cook 	kmsg_dump(KMSG_DUMP_PANIC);
1366723734cSKees Cook 
137f06e5153SMasami Hiramatsu 	/*
138f06e5153SMasami Hiramatsu 	 * If you doubt kdump always works fine in any situation,
139f06e5153SMasami Hiramatsu 	 * "crash_kexec_post_notifiers" offers you a chance to run
140f06e5153SMasami Hiramatsu 	 * panic_notifiers and dumping kmsg before kdump.
141f06e5153SMasami Hiramatsu 	 * Note: since some panic_notifiers can make crashed kernel
142f06e5153SMasami Hiramatsu 	 * more unstable, it can increase risks of the kdump failure too.
143f06e5153SMasami Hiramatsu 	 */
144f06e5153SMasami Hiramatsu 	crash_kexec(NULL);
145f06e5153SMasami Hiramatsu 
146d014e889SAaro Koskinen 	bust_spinlocks(0);
147d014e889SAaro Koskinen 
148c7ff0d9cSTAMUKI Shoichi 	if (!panic_blink)
149c7ff0d9cSTAMUKI Shoichi 		panic_blink = no_blink;
150c7ff0d9cSTAMUKI Shoichi 
151dc009d92SEric W. Biederman 	if (panic_timeout > 0) {
1521da177e4SLinus Torvalds 		/*
1531da177e4SLinus Torvalds 		 * Delay timeout seconds before rebooting the machine.
154c95dbf27SIngo Molnar 		 * We can't use the "normal" timers since we just panicked.
1551da177e4SLinus Torvalds 		 */
156d7c0847fSFabian Frederick 		pr_emerg("Rebooting in %d seconds..", panic_timeout);
157c95dbf27SIngo Molnar 
158c7ff0d9cSTAMUKI Shoichi 		for (i = 0; i < panic_timeout * 1000; i += PANIC_TIMER_STEP) {
1591da177e4SLinus Torvalds 			touch_nmi_watchdog();
160c7ff0d9cSTAMUKI Shoichi 			if (i >= i_next) {
161c7ff0d9cSTAMUKI Shoichi 				i += panic_blink(state ^= 1);
162c7ff0d9cSTAMUKI Shoichi 				i_next = i + 3600 / PANIC_BLINK_SPD;
163c7ff0d9cSTAMUKI Shoichi 			}
164c7ff0d9cSTAMUKI Shoichi 			mdelay(PANIC_TIMER_STEP);
1651da177e4SLinus Torvalds 		}
1664302fbc8SHugh Dickins 	}
1674302fbc8SHugh Dickins 	if (panic_timeout != 0) {
168c95dbf27SIngo Molnar 		/*
169c95dbf27SIngo Molnar 		 * This will not be a clean reboot, with everything
1702f048ea8SEric W. Biederman 		 * shutting down.  But if there is a chance of
1712f048ea8SEric W. Biederman 		 * rebooting the system it will be rebooted.
1721da177e4SLinus Torvalds 		 */
1732f048ea8SEric W. Biederman 		emergency_restart();
1741da177e4SLinus Torvalds 	}
1751da177e4SLinus Torvalds #ifdef __sparc__
1761da177e4SLinus Torvalds 	{
1771da177e4SLinus Torvalds 		extern int stop_a_enabled;
178a271c241STom 'spot' Callaway 		/* Make sure the user can actually press Stop-A (L1-A) */
1791da177e4SLinus Torvalds 		stop_a_enabled = 1;
180d7c0847fSFabian Frederick 		pr_emerg("Press Stop-A (L1-A) to return to the boot prom\n");
1811da177e4SLinus Torvalds 	}
1821da177e4SLinus Torvalds #endif
183347a8dc3SMartin Schwidefsky #if defined(CONFIG_S390)
184c95dbf27SIngo Molnar 	{
185c95dbf27SIngo Molnar 		unsigned long caller;
186c95dbf27SIngo Molnar 
187c95dbf27SIngo Molnar 		caller = (unsigned long)__builtin_return_address(0);
1881da177e4SLinus Torvalds 		disabled_wait(caller);
189c95dbf27SIngo Molnar 	}
1901da177e4SLinus Torvalds #endif
191d7c0847fSFabian Frederick 	pr_emerg("---[ end Kernel panic - not syncing: %s\n", buf);
1921da177e4SLinus Torvalds 	local_irq_enable();
193c7ff0d9cSTAMUKI Shoichi 	for (i = 0; ; i += PANIC_TIMER_STEP) {
194c22db941SJan Beulich 		touch_softlockup_watchdog();
195c7ff0d9cSTAMUKI Shoichi 		if (i >= i_next) {
196c7ff0d9cSTAMUKI Shoichi 			i += panic_blink(state ^= 1);
197c7ff0d9cSTAMUKI Shoichi 			i_next = i + 3600 / PANIC_BLINK_SPD;
198c7ff0d9cSTAMUKI Shoichi 		}
199c7ff0d9cSTAMUKI Shoichi 		mdelay(PANIC_TIMER_STEP);
2001da177e4SLinus Torvalds 	}
2011da177e4SLinus Torvalds }
2021da177e4SLinus Torvalds 
2031da177e4SLinus Torvalds EXPORT_SYMBOL(panic);
2041da177e4SLinus Torvalds 
2051da177e4SLinus Torvalds 
20625ddbb18SAndi Kleen struct tnt {
20725ddbb18SAndi Kleen 	u8	bit;
20825ddbb18SAndi Kleen 	char	true;
20925ddbb18SAndi Kleen 	char	false;
21025ddbb18SAndi Kleen };
21125ddbb18SAndi Kleen 
21225ddbb18SAndi Kleen static const struct tnt tnts[] = {
21325ddbb18SAndi Kleen 	{ TAINT_PROPRIETARY_MODULE,	'P', 'G' },
21425ddbb18SAndi Kleen 	{ TAINT_FORCED_MODULE,		'F', ' ' },
2158c90487cSDave Jones 	{ TAINT_CPU_OUT_OF_SPEC,	'S', ' ' },
21625ddbb18SAndi Kleen 	{ TAINT_FORCED_RMMOD,		'R', ' ' },
21725ddbb18SAndi Kleen 	{ TAINT_MACHINE_CHECK,		'M', ' ' },
21825ddbb18SAndi Kleen 	{ TAINT_BAD_PAGE,		'B', ' ' },
21925ddbb18SAndi Kleen 	{ TAINT_USER,			'U', ' ' },
22025ddbb18SAndi Kleen 	{ TAINT_DIE,			'D', ' ' },
22125ddbb18SAndi Kleen 	{ TAINT_OVERRIDDEN_ACPI_TABLE,	'A', ' ' },
22225ddbb18SAndi Kleen 	{ TAINT_WARN,			'W', ' ' },
22326e9a397SLinus Torvalds 	{ TAINT_CRAP,			'C', ' ' },
22492946bc7SBen Hutchings 	{ TAINT_FIRMWARE_WORKAROUND,	'I', ' ' },
2252449b8baSBen Hutchings 	{ TAINT_OOT_MODULE,		'O', ' ' },
22657673c2bSRusty Russell 	{ TAINT_UNSIGNED_MODULE,	'E', ' ' },
22769361eefSJosh Hunt 	{ TAINT_SOFTLOCKUP,		'L', ' ' },
22825ddbb18SAndi Kleen };
22925ddbb18SAndi Kleen 
2301da177e4SLinus Torvalds /**
2311da177e4SLinus Torvalds  *	print_tainted - return a string to represent the kernel taint state.
2321da177e4SLinus Torvalds  *
2331da177e4SLinus Torvalds  *  'P' - Proprietary module has been loaded.
2341da177e4SLinus Torvalds  *  'F' - Module has been forcibly loaded.
2351da177e4SLinus Torvalds  *  'S' - SMP with CPUs not designed for SMP.
2361da177e4SLinus Torvalds  *  'R' - User forced a module unload.
2371da177e4SLinus Torvalds  *  'M' - System experienced a machine check exception.
2381da177e4SLinus Torvalds  *  'B' - System has hit bad_page.
2391da177e4SLinus Torvalds  *  'U' - Userspace-defined naughtiness.
240a8005992SArjan van de Ven  *  'D' - Kernel has oopsed before
2411da177e4SLinus Torvalds  *  'A' - ACPI table overridden.
2421da177e4SLinus Torvalds  *  'W' - Taint on warning.
243061b1bd3SGreg Kroah-Hartman  *  'C' - modules from drivers/staging are loaded.
24492946bc7SBen Hutchings  *  'I' - Working around severe firmware bug.
2452449b8baSBen Hutchings  *  'O' - Out-of-tree module has been loaded.
24657673c2bSRusty Russell  *  'E' - Unsigned module has been loaded.
247*bc53a3f4SXie XiuQi  *  'L' - A soft lockup has previously occurred.
2481da177e4SLinus Torvalds  *
249fe002a41SRobert P. J. Day  *	The string is overwritten by the next call to print_tainted().
2501da177e4SLinus Torvalds  */
2511da177e4SLinus Torvalds const char *print_tainted(void)
2521da177e4SLinus Torvalds {
25301284764SChen Gang 	static char buf[ARRAY_SIZE(tnts) + sizeof("Tainted: ")];
25425ddbb18SAndi Kleen 
25525ddbb18SAndi Kleen 	if (tainted_mask) {
25625ddbb18SAndi Kleen 		char *s;
25725ddbb18SAndi Kleen 		int i;
25825ddbb18SAndi Kleen 
25925ddbb18SAndi Kleen 		s = buf + sprintf(buf, "Tainted: ");
26025ddbb18SAndi Kleen 		for (i = 0; i < ARRAY_SIZE(tnts); i++) {
26125ddbb18SAndi Kleen 			const struct tnt *t = &tnts[i];
26225ddbb18SAndi Kleen 			*s++ = test_bit(t->bit, &tainted_mask) ?
26325ddbb18SAndi Kleen 					t->true : t->false;
2641da177e4SLinus Torvalds 		}
26525ddbb18SAndi Kleen 		*s = 0;
26625ddbb18SAndi Kleen 	} else
2671da177e4SLinus Torvalds 		snprintf(buf, sizeof(buf), "Not tainted");
268c95dbf27SIngo Molnar 
269c95dbf27SIngo Molnar 	return buf;
2701da177e4SLinus Torvalds }
2711da177e4SLinus Torvalds 
27225ddbb18SAndi Kleen int test_taint(unsigned flag)
27325ddbb18SAndi Kleen {
27425ddbb18SAndi Kleen 	return test_bit(flag, &tainted_mask);
27525ddbb18SAndi Kleen }
27625ddbb18SAndi Kleen EXPORT_SYMBOL(test_taint);
27725ddbb18SAndi Kleen 
27825ddbb18SAndi Kleen unsigned long get_taint(void)
27925ddbb18SAndi Kleen {
28025ddbb18SAndi Kleen 	return tainted_mask;
28125ddbb18SAndi Kleen }
28225ddbb18SAndi Kleen 
283373d4d09SRusty Russell /**
284373d4d09SRusty Russell  * add_taint: add a taint flag if not already set.
285373d4d09SRusty Russell  * @flag: one of the TAINT_* constants.
286373d4d09SRusty Russell  * @lockdep_ok: whether lock debugging is still OK.
287373d4d09SRusty Russell  *
288373d4d09SRusty Russell  * If something bad has gone wrong, you'll want @lockdebug_ok = false, but for
289373d4d09SRusty Russell  * some notewortht-but-not-corrupting cases, it can be set to true.
2909eeba613SFrederic Weisbecker  */
291373d4d09SRusty Russell void add_taint(unsigned flag, enum lockdep_ok lockdep_ok)
292373d4d09SRusty Russell {
293373d4d09SRusty Russell 	if (lockdep_ok == LOCKDEP_NOW_UNRELIABLE && __debug_locks_off())
294d7c0847fSFabian Frederick 		pr_warn("Disabling lock debugging due to kernel taint\n");
2959eeba613SFrederic Weisbecker 
29625ddbb18SAndi Kleen 	set_bit(flag, &tainted_mask);
2971da177e4SLinus Torvalds }
2981da177e4SLinus Torvalds EXPORT_SYMBOL(add_taint);
299dd287796SAndrew Morton 
300dd287796SAndrew Morton static void spin_msec(int msecs)
301dd287796SAndrew Morton {
302dd287796SAndrew Morton 	int i;
303dd287796SAndrew Morton 
304dd287796SAndrew Morton 	for (i = 0; i < msecs; i++) {
305dd287796SAndrew Morton 		touch_nmi_watchdog();
306dd287796SAndrew Morton 		mdelay(1);
307dd287796SAndrew Morton 	}
308dd287796SAndrew Morton }
309dd287796SAndrew Morton 
310dd287796SAndrew Morton /*
311dd287796SAndrew Morton  * It just happens that oops_enter() and oops_exit() are identically
312dd287796SAndrew Morton  * implemented...
313dd287796SAndrew Morton  */
314dd287796SAndrew Morton static void do_oops_enter_exit(void)
315dd287796SAndrew Morton {
316dd287796SAndrew Morton 	unsigned long flags;
317dd287796SAndrew Morton 	static int spin_counter;
318dd287796SAndrew Morton 
319dd287796SAndrew Morton 	if (!pause_on_oops)
320dd287796SAndrew Morton 		return;
321dd287796SAndrew Morton 
322dd287796SAndrew Morton 	spin_lock_irqsave(&pause_on_oops_lock, flags);
323dd287796SAndrew Morton 	if (pause_on_oops_flag == 0) {
324dd287796SAndrew Morton 		/* This CPU may now print the oops message */
325dd287796SAndrew Morton 		pause_on_oops_flag = 1;
326dd287796SAndrew Morton 	} else {
327dd287796SAndrew Morton 		/* We need to stall this CPU */
328dd287796SAndrew Morton 		if (!spin_counter) {
329dd287796SAndrew Morton 			/* This CPU gets to do the counting */
330dd287796SAndrew Morton 			spin_counter = pause_on_oops;
331dd287796SAndrew Morton 			do {
332dd287796SAndrew Morton 				spin_unlock(&pause_on_oops_lock);
333dd287796SAndrew Morton 				spin_msec(MSEC_PER_SEC);
334dd287796SAndrew Morton 				spin_lock(&pause_on_oops_lock);
335dd287796SAndrew Morton 			} while (--spin_counter);
336dd287796SAndrew Morton 			pause_on_oops_flag = 0;
337dd287796SAndrew Morton 		} else {
338dd287796SAndrew Morton 			/* This CPU waits for a different one */
339dd287796SAndrew Morton 			while (spin_counter) {
340dd287796SAndrew Morton 				spin_unlock(&pause_on_oops_lock);
341dd287796SAndrew Morton 				spin_msec(1);
342dd287796SAndrew Morton 				spin_lock(&pause_on_oops_lock);
343dd287796SAndrew Morton 			}
344dd287796SAndrew Morton 		}
345dd287796SAndrew Morton 	}
346dd287796SAndrew Morton 	spin_unlock_irqrestore(&pause_on_oops_lock, flags);
347dd287796SAndrew Morton }
348dd287796SAndrew Morton 
349dd287796SAndrew Morton /*
350c95dbf27SIngo Molnar  * Return true if the calling CPU is allowed to print oops-related info.
351c95dbf27SIngo Molnar  * This is a bit racy..
352dd287796SAndrew Morton  */
353dd287796SAndrew Morton int oops_may_print(void)
354dd287796SAndrew Morton {
355dd287796SAndrew Morton 	return pause_on_oops_flag == 0;
356dd287796SAndrew Morton }
357dd287796SAndrew Morton 
358dd287796SAndrew Morton /*
359dd287796SAndrew Morton  * Called when the architecture enters its oops handler, before it prints
360c95dbf27SIngo Molnar  * anything.  If this is the first CPU to oops, and it's oopsing the first
361c95dbf27SIngo Molnar  * time then let it proceed.
362dd287796SAndrew Morton  *
363c95dbf27SIngo Molnar  * This is all enabled by the pause_on_oops kernel boot option.  We do all
364c95dbf27SIngo Molnar  * this to ensure that oopses don't scroll off the screen.  It has the
365c95dbf27SIngo Molnar  * side-effect of preventing later-oopsing CPUs from mucking up the display,
366c95dbf27SIngo Molnar  * too.
367dd287796SAndrew Morton  *
368c95dbf27SIngo Molnar  * It turns out that the CPU which is allowed to print ends up pausing for
369c95dbf27SIngo Molnar  * the right duration, whereas all the other CPUs pause for twice as long:
370c95dbf27SIngo Molnar  * once in oops_enter(), once in oops_exit().
371dd287796SAndrew Morton  */
372dd287796SAndrew Morton void oops_enter(void)
373dd287796SAndrew Morton {
374bdff7870SThomas Gleixner 	tracing_off();
375c95dbf27SIngo Molnar 	/* can't trust the integrity of the kernel anymore: */
376c95dbf27SIngo Molnar 	debug_locks_off();
377dd287796SAndrew Morton 	do_oops_enter_exit();
378dd287796SAndrew Morton }
379dd287796SAndrew Morton 
380dd287796SAndrew Morton /*
3812c3b20e9SArjan van de Ven  * 64-bit random ID for oopses:
3822c3b20e9SArjan van de Ven  */
3832c3b20e9SArjan van de Ven static u64 oops_id;
3842c3b20e9SArjan van de Ven 
3852c3b20e9SArjan van de Ven static int init_oops_id(void)
3862c3b20e9SArjan van de Ven {
3872c3b20e9SArjan van de Ven 	if (!oops_id)
3882c3b20e9SArjan van de Ven 		get_random_bytes(&oops_id, sizeof(oops_id));
389d6624f99SArjan van de Ven 	else
390d6624f99SArjan van de Ven 		oops_id++;
3912c3b20e9SArjan van de Ven 
3922c3b20e9SArjan van de Ven 	return 0;
3932c3b20e9SArjan van de Ven }
3942c3b20e9SArjan van de Ven late_initcall(init_oops_id);
3952c3b20e9SArjan van de Ven 
396863a6049SAnton Blanchard void print_oops_end_marker(void)
39771c33911SArjan van de Ven {
39871c33911SArjan van de Ven 	init_oops_id();
399d7c0847fSFabian Frederick 	pr_warn("---[ end trace %016llx ]---\n", (unsigned long long)oops_id);
40071c33911SArjan van de Ven }
40171c33911SArjan van de Ven 
4022c3b20e9SArjan van de Ven /*
403dd287796SAndrew Morton  * Called when the architecture exits its oops handler, after printing
404dd287796SAndrew Morton  * everything.
405dd287796SAndrew Morton  */
406dd287796SAndrew Morton void oops_exit(void)
407dd287796SAndrew Morton {
408dd287796SAndrew Morton 	do_oops_enter_exit();
40971c33911SArjan van de Ven 	print_oops_end_marker();
410456b565cSSimon Kagstrom 	kmsg_dump(KMSG_DUMP_OOPS);
411dd287796SAndrew Morton }
4123162f751SArjan van de Ven 
41379b4cc5eSArjan van de Ven #ifdef WANT_WARN_ON_SLOWPATH
4140f6f49a8SLinus Torvalds struct slowpath_args {
4150f6f49a8SLinus Torvalds 	const char *fmt;
416a8f18b90SArjan van de Ven 	va_list args;
4170f6f49a8SLinus Torvalds };
4180f6f49a8SLinus Torvalds 
419b2be0527SBen Hutchings static void warn_slowpath_common(const char *file, int line, void *caller,
420b2be0527SBen Hutchings 				 unsigned taint, struct slowpath_args *args)
4210f6f49a8SLinus Torvalds {
422de7edd31SSteven Rostedt (Red Hat) 	disable_trace_on_warning();
423de7edd31SSteven Rostedt (Red Hat) 
424dcb6b452SAlex Thorlton 	pr_warn("------------[ cut here ]------------\n");
425dcb6b452SAlex Thorlton 	pr_warn("WARNING: CPU: %d PID: %d at %s:%d %pS()\n",
426dcb6b452SAlex Thorlton 		raw_smp_processor_id(), current->pid, file, line, caller);
42774853dbaSArjan van de Ven 
4280f6f49a8SLinus Torvalds 	if (args)
4290f6f49a8SLinus Torvalds 		vprintk(args->fmt, args->args);
430a8f18b90SArjan van de Ven 
431a8f18b90SArjan van de Ven 	print_modules();
432a8f18b90SArjan van de Ven 	dump_stack();
433a8f18b90SArjan van de Ven 	print_oops_end_marker();
434373d4d09SRusty Russell 	/* Just a warning, don't kill lockdep. */
435373d4d09SRusty Russell 	add_taint(taint, LOCKDEP_STILL_OK);
436a8f18b90SArjan van de Ven }
4370f6f49a8SLinus Torvalds 
4380f6f49a8SLinus Torvalds void warn_slowpath_fmt(const char *file, int line, const char *fmt, ...)
4390f6f49a8SLinus Torvalds {
4400f6f49a8SLinus Torvalds 	struct slowpath_args args;
4410f6f49a8SLinus Torvalds 
4420f6f49a8SLinus Torvalds 	args.fmt = fmt;
4430f6f49a8SLinus Torvalds 	va_start(args.args, fmt);
444b2be0527SBen Hutchings 	warn_slowpath_common(file, line, __builtin_return_address(0),
445b2be0527SBen Hutchings 			     TAINT_WARN, &args);
4460f6f49a8SLinus Torvalds 	va_end(args.args);
4470f6f49a8SLinus Torvalds }
44857adc4d2SAndi Kleen EXPORT_SYMBOL(warn_slowpath_fmt);
44957adc4d2SAndi Kleen 
450b2be0527SBen Hutchings void warn_slowpath_fmt_taint(const char *file, int line,
451b2be0527SBen Hutchings 			     unsigned taint, const char *fmt, ...)
452b2be0527SBen Hutchings {
453b2be0527SBen Hutchings 	struct slowpath_args args;
454b2be0527SBen Hutchings 
455b2be0527SBen Hutchings 	args.fmt = fmt;
456b2be0527SBen Hutchings 	va_start(args.args, fmt);
457b2be0527SBen Hutchings 	warn_slowpath_common(file, line, __builtin_return_address(0),
458b2be0527SBen Hutchings 			     taint, &args);
459b2be0527SBen Hutchings 	va_end(args.args);
460b2be0527SBen Hutchings }
461b2be0527SBen Hutchings EXPORT_SYMBOL(warn_slowpath_fmt_taint);
462b2be0527SBen Hutchings 
46357adc4d2SAndi Kleen void warn_slowpath_null(const char *file, int line)
46457adc4d2SAndi Kleen {
465b2be0527SBen Hutchings 	warn_slowpath_common(file, line, __builtin_return_address(0),
466b2be0527SBen Hutchings 			     TAINT_WARN, NULL);
46757adc4d2SAndi Kleen }
46857adc4d2SAndi Kleen EXPORT_SYMBOL(warn_slowpath_null);
46979b4cc5eSArjan van de Ven #endif
47079b4cc5eSArjan van de Ven 
4713162f751SArjan van de Ven #ifdef CONFIG_CC_STACKPROTECTOR
47254371a43SArjan van de Ven 
4733162f751SArjan van de Ven /*
4743162f751SArjan van de Ven  * Called when gcc's -fstack-protector feature is used, and
4753162f751SArjan van de Ven  * gcc detects corruption of the on-stack canary value
4763162f751SArjan van de Ven  */
477a7330c99SAndi Kleen __visible void __stack_chk_fail(void)
4783162f751SArjan van de Ven {
479517a92c4SIngo Molnar 	panic("stack-protector: Kernel stack is corrupted in: %p\n",
480517a92c4SIngo Molnar 		__builtin_return_address(0));
4813162f751SArjan van de Ven }
4823162f751SArjan van de Ven EXPORT_SYMBOL(__stack_chk_fail);
48354371a43SArjan van de Ven 
4843162f751SArjan van de Ven #endif
485f44dd164SRusty Russell 
486f44dd164SRusty Russell core_param(panic, panic_timeout, int, 0644);
487f44dd164SRusty Russell core_param(pause_on_oops, pause_on_oops, int, 0644);
488d404ab0aSOlaf Hering 
489f06e5153SMasami Hiramatsu static int __init setup_crash_kexec_post_notifiers(char *s)
490f06e5153SMasami Hiramatsu {
491f06e5153SMasami Hiramatsu 	crash_kexec_post_notifiers = true;
492f06e5153SMasami Hiramatsu 	return 0;
493f06e5153SMasami Hiramatsu }
494f06e5153SMasami Hiramatsu early_param("crash_kexec_post_notifiers", setup_crash_kexec_post_notifiers);
495f06e5153SMasami Hiramatsu 
496d404ab0aSOlaf Hering static int __init oops_setup(char *s)
497d404ab0aSOlaf Hering {
498d404ab0aSOlaf Hering 	if (!s)
499d404ab0aSOlaf Hering 		return -EINVAL;
500d404ab0aSOlaf Hering 	if (!strcmp(s, "panic"))
501d404ab0aSOlaf Hering 		panic_on_oops = 1;
502d404ab0aSOlaf Hering 	return 0;
503d404ab0aSOlaf Hering }
504d404ab0aSOlaf Hering early_param("oops", oops_setup);
505