xref: /linux-6.15/kernel/panic.c (revision 8d91f8b1)
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>
271da177e4SLinus Torvalds 
28c7ff0d9cSTAMUKI Shoichi #define PANIC_TIMER_STEP 100
29c7ff0d9cSTAMUKI Shoichi #define PANIC_BLINK_SPD 18
30c7ff0d9cSTAMUKI Shoichi 
312a01bb38SKyle McMartin int panic_on_oops = CONFIG_PANIC_ON_OOPS_VALUE;
3225ddbb18SAndi Kleen static unsigned long tainted_mask;
33dd287796SAndrew Morton static int pause_on_oops;
34dd287796SAndrew Morton static int pause_on_oops_flag;
35dd287796SAndrew Morton static DEFINE_SPINLOCK(pause_on_oops_lock);
365375b708SHATAYAMA Daisuke bool crash_kexec_post_notifiers;
379e3961a0SPrarit Bhargava int panic_on_warn __read_mostly;
381da177e4SLinus Torvalds 
395800dc3cSJason Baron int panic_timeout = CONFIG_PANIC_TIMEOUT;
4081e88fdcSHuang Ying EXPORT_SYMBOL_GPL(panic_timeout);
411da177e4SLinus Torvalds 
42e041c683SAlan Stern ATOMIC_NOTIFIER_HEAD(panic_notifier_list);
431da177e4SLinus Torvalds 
441da177e4SLinus Torvalds EXPORT_SYMBOL(panic_notifier_list);
451da177e4SLinus Torvalds 
46c7ff0d9cSTAMUKI Shoichi static long no_blink(int state)
478aeee85aSAnton Blanchard {
48c7ff0d9cSTAMUKI Shoichi 	return 0;
49c7ff0d9cSTAMUKI Shoichi }
508aeee85aSAnton Blanchard 
51c7ff0d9cSTAMUKI Shoichi /* Returns how long it waited in ms */
52c7ff0d9cSTAMUKI Shoichi long (*panic_blink)(int state);
53c7ff0d9cSTAMUKI Shoichi EXPORT_SYMBOL(panic_blink);
548aeee85aSAnton Blanchard 
5593e13a36SMichael Holzheu /*
5693e13a36SMichael Holzheu  * Stop ourself in panic -- architecture code may override this
5793e13a36SMichael Holzheu  */
5893e13a36SMichael Holzheu void __weak panic_smp_self_stop(void)
5993e13a36SMichael Holzheu {
6093e13a36SMichael Holzheu 	while (1)
6193e13a36SMichael Holzheu 		cpu_relax();
6293e13a36SMichael Holzheu }
6393e13a36SMichael Holzheu 
6458c5661fSHidehiro Kawai /*
6558c5661fSHidehiro Kawai  * Stop ourselves in NMI context if another CPU has already panicked. Arch code
6658c5661fSHidehiro Kawai  * may override this to prepare for crash dumping, e.g. save regs info.
6758c5661fSHidehiro Kawai  */
6858c5661fSHidehiro Kawai void __weak nmi_panic_self_stop(struct pt_regs *regs)
6958c5661fSHidehiro Kawai {
7058c5661fSHidehiro Kawai 	panic_smp_self_stop();
7158c5661fSHidehiro Kawai }
7258c5661fSHidehiro Kawai 
731717f209SHidehiro Kawai atomic_t panic_cpu = ATOMIC_INIT(PANIC_CPU_INVALID);
741717f209SHidehiro Kawai 
751da177e4SLinus Torvalds /**
761da177e4SLinus Torvalds  *	panic - halt the system
771da177e4SLinus Torvalds  *	@fmt: The text string to print
781da177e4SLinus Torvalds  *
791da177e4SLinus Torvalds  *	Display a message, then perform cleanups.
801da177e4SLinus Torvalds  *
811da177e4SLinus Torvalds  *	This function never returns.
821da177e4SLinus Torvalds  */
839402c95fSJoe Perches void panic(const char *fmt, ...)
841da177e4SLinus Torvalds {
851da177e4SLinus Torvalds 	static char buf[1024];
861da177e4SLinus Torvalds 	va_list args;
87c7ff0d9cSTAMUKI Shoichi 	long i, i_next = 0;
88c7ff0d9cSTAMUKI Shoichi 	int state = 0;
891717f209SHidehiro Kawai 	int old_cpu, this_cpu;
901da177e4SLinus Torvalds 
91dc009d92SEric W. Biederman 	/*
92190320c3SVikram Mulukutla 	 * Disable local interrupts. This will prevent panic_smp_self_stop
93190320c3SVikram Mulukutla 	 * from deadlocking the first cpu that invokes the panic, since
94190320c3SVikram Mulukutla 	 * there is nothing to prevent an interrupt handler (that runs
951717f209SHidehiro Kawai 	 * after setting panic_cpu) from invoking panic() again.
96190320c3SVikram Mulukutla 	 */
97190320c3SVikram Mulukutla 	local_irq_disable();
98190320c3SVikram Mulukutla 
99190320c3SVikram Mulukutla 	/*
100c95dbf27SIngo Molnar 	 * It's possible to come here directly from a panic-assertion and
101c95dbf27SIngo Molnar 	 * not have preempt disabled. Some functions called from here want
102dc009d92SEric W. Biederman 	 * preempt to be disabled. No point enabling it later though...
10393e13a36SMichael Holzheu 	 *
10493e13a36SMichael Holzheu 	 * Only one CPU is allowed to execute the panic code from here. For
10593e13a36SMichael Holzheu 	 * multiple parallel invocations of panic, all other CPUs either
10693e13a36SMichael Holzheu 	 * stop themself or will wait until they are stopped by the 1st CPU
10793e13a36SMichael Holzheu 	 * with smp_send_stop().
1081717f209SHidehiro Kawai 	 *
1091717f209SHidehiro Kawai 	 * `old_cpu == PANIC_CPU_INVALID' means this is the 1st CPU which
1101717f209SHidehiro Kawai 	 * comes here, so go ahead.
1111717f209SHidehiro Kawai 	 * `old_cpu == this_cpu' means we came from nmi_panic() which sets
1121717f209SHidehiro Kawai 	 * panic_cpu to this CPU.  In this case, this is also the 1st CPU.
113dc009d92SEric W. Biederman 	 */
1141717f209SHidehiro Kawai 	this_cpu = raw_smp_processor_id();
1151717f209SHidehiro Kawai 	old_cpu  = atomic_cmpxchg(&panic_cpu, PANIC_CPU_INVALID, this_cpu);
1161717f209SHidehiro Kawai 
1171717f209SHidehiro Kawai 	if (old_cpu != PANIC_CPU_INVALID && old_cpu != this_cpu)
11893e13a36SMichael Holzheu 		panic_smp_self_stop();
119dc009d92SEric W. Biederman 
1205b530fc1SAnton Blanchard 	console_verbose();
1211da177e4SLinus Torvalds 	bust_spinlocks(1);
1221da177e4SLinus Torvalds 	va_start(args, fmt);
1231da177e4SLinus Torvalds 	vsnprintf(buf, sizeof(buf), fmt, args);
1241da177e4SLinus Torvalds 	va_end(args);
125d7c0847fSFabian Frederick 	pr_emerg("Kernel panic - not syncing: %s\n", buf);
1265cb27301SIngo Molnar #ifdef CONFIG_DEBUG_BUGVERBOSE
1276e6f0a1fSAndi Kleen 	/*
1286e6f0a1fSAndi Kleen 	 * Avoid nested stack-dumping if a panic occurs during oops processing
1296e6f0a1fSAndi Kleen 	 */
130026ee1f6SJason Wessel 	if (!test_taint(TAINT_DIE) && oops_in_progress <= 1)
1315cb27301SIngo Molnar 		dump_stack();
1325cb27301SIngo Molnar #endif
1331da177e4SLinus Torvalds 
134dc009d92SEric W. Biederman 	/*
135dc009d92SEric W. Biederman 	 * If we have crashed and we have a crash kernel loaded let it handle
136dc009d92SEric W. Biederman 	 * everything else.
137f06e5153SMasami Hiramatsu 	 * If we want to run this after calling panic_notifiers, pass
138f06e5153SMasami Hiramatsu 	 * the "crash_kexec_post_notifiers" option to the kernel.
1397bbee5caSHidehiro Kawai 	 *
1407bbee5caSHidehiro Kawai 	 * Bypass the panic_cpu check and call __crash_kexec directly.
141dc009d92SEric W. Biederman 	 */
142f06e5153SMasami Hiramatsu 	if (!crash_kexec_post_notifiers)
1437bbee5caSHidehiro Kawai 		__crash_kexec(NULL);
144dc009d92SEric W. Biederman 
145dc009d92SEric W. Biederman 	/*
146dc009d92SEric W. Biederman 	 * Note smp_send_stop is the usual smp shutdown function, which
147dc009d92SEric W. Biederman 	 * unfortunately means it may not be hardened to work in a panic
148dc009d92SEric W. Biederman 	 * situation.
149dc009d92SEric W. Biederman 	 */
1501da177e4SLinus Torvalds 	smp_send_stop();
1511da177e4SLinus Torvalds 
1526723734cSKees Cook 	/*
1536723734cSKees Cook 	 * Run any panic handlers, including those that might need to
1546723734cSKees Cook 	 * add information to the kmsg dump output.
1556723734cSKees Cook 	 */
156e041c683SAlan Stern 	atomic_notifier_call_chain(&panic_notifier_list, 0, buf);
1571da177e4SLinus Torvalds 
1586723734cSKees Cook 	kmsg_dump(KMSG_DUMP_PANIC);
1596723734cSKees Cook 
160f06e5153SMasami Hiramatsu 	/*
161f06e5153SMasami Hiramatsu 	 * If you doubt kdump always works fine in any situation,
162f06e5153SMasami Hiramatsu 	 * "crash_kexec_post_notifiers" offers you a chance to run
163f06e5153SMasami Hiramatsu 	 * panic_notifiers and dumping kmsg before kdump.
164f06e5153SMasami Hiramatsu 	 * Note: since some panic_notifiers can make crashed kernel
165f06e5153SMasami Hiramatsu 	 * more unstable, it can increase risks of the kdump failure too.
1667bbee5caSHidehiro Kawai 	 *
1677bbee5caSHidehiro Kawai 	 * Bypass the panic_cpu check and call __crash_kexec directly.
168f06e5153SMasami Hiramatsu 	 */
169f45d85ffSHATAYAMA Daisuke 	if (crash_kexec_post_notifiers)
1707bbee5caSHidehiro Kawai 		__crash_kexec(NULL);
171f06e5153SMasami Hiramatsu 
172d014e889SAaro Koskinen 	bust_spinlocks(0);
173d014e889SAaro Koskinen 
17408d78658SVitaly Kuznetsov 	/*
17508d78658SVitaly Kuznetsov 	 * We may have ended up stopping the CPU holding the lock (in
17608d78658SVitaly Kuznetsov 	 * smp_send_stop()) while still having some valuable data in the console
17708d78658SVitaly Kuznetsov 	 * buffer.  Try to acquire the lock then release it regardless of the
1787625b3a0SVitaly Kuznetsov 	 * result.  The release will also print the buffers out.  Locks debug
1797625b3a0SVitaly Kuznetsov 	 * should be disabled to avoid reporting bad unlock balance when
1807625b3a0SVitaly Kuznetsov 	 * panic() is not being callled from OOPS.
18108d78658SVitaly Kuznetsov 	 */
1827625b3a0SVitaly Kuznetsov 	debug_locks_off();
183*8d91f8b1STejun Heo 	console_flush_on_panic();
18408d78658SVitaly Kuznetsov 
185c7ff0d9cSTAMUKI Shoichi 	if (!panic_blink)
186c7ff0d9cSTAMUKI Shoichi 		panic_blink = no_blink;
187c7ff0d9cSTAMUKI Shoichi 
188dc009d92SEric W. Biederman 	if (panic_timeout > 0) {
1891da177e4SLinus Torvalds 		/*
1901da177e4SLinus Torvalds 		 * Delay timeout seconds before rebooting the machine.
191c95dbf27SIngo Molnar 		 * We can't use the "normal" timers since we just panicked.
1921da177e4SLinus Torvalds 		 */
193d7c0847fSFabian Frederick 		pr_emerg("Rebooting in %d seconds..", panic_timeout);
194c95dbf27SIngo Molnar 
195c7ff0d9cSTAMUKI Shoichi 		for (i = 0; i < panic_timeout * 1000; i += PANIC_TIMER_STEP) {
1961da177e4SLinus Torvalds 			touch_nmi_watchdog();
197c7ff0d9cSTAMUKI Shoichi 			if (i >= i_next) {
198c7ff0d9cSTAMUKI Shoichi 				i += panic_blink(state ^= 1);
199c7ff0d9cSTAMUKI Shoichi 				i_next = i + 3600 / PANIC_BLINK_SPD;
200c7ff0d9cSTAMUKI Shoichi 			}
201c7ff0d9cSTAMUKI Shoichi 			mdelay(PANIC_TIMER_STEP);
2021da177e4SLinus Torvalds 		}
2034302fbc8SHugh Dickins 	}
2044302fbc8SHugh Dickins 	if (panic_timeout != 0) {
205c95dbf27SIngo Molnar 		/*
206c95dbf27SIngo Molnar 		 * This will not be a clean reboot, with everything
2072f048ea8SEric W. Biederman 		 * shutting down.  But if there is a chance of
2082f048ea8SEric W. Biederman 		 * rebooting the system it will be rebooted.
2091da177e4SLinus Torvalds 		 */
2102f048ea8SEric W. Biederman 		emergency_restart();
2111da177e4SLinus Torvalds 	}
2121da177e4SLinus Torvalds #ifdef __sparc__
2131da177e4SLinus Torvalds 	{
2141da177e4SLinus Torvalds 		extern int stop_a_enabled;
215a271c241STom 'spot' Callaway 		/* Make sure the user can actually press Stop-A (L1-A) */
2161da177e4SLinus Torvalds 		stop_a_enabled = 1;
217d7c0847fSFabian Frederick 		pr_emerg("Press Stop-A (L1-A) to return to the boot prom\n");
2181da177e4SLinus Torvalds 	}
2191da177e4SLinus Torvalds #endif
220347a8dc3SMartin Schwidefsky #if defined(CONFIG_S390)
221c95dbf27SIngo Molnar 	{
222c95dbf27SIngo Molnar 		unsigned long caller;
223c95dbf27SIngo Molnar 
224c95dbf27SIngo Molnar 		caller = (unsigned long)__builtin_return_address(0);
2251da177e4SLinus Torvalds 		disabled_wait(caller);
226c95dbf27SIngo Molnar 	}
2271da177e4SLinus Torvalds #endif
228d7c0847fSFabian Frederick 	pr_emerg("---[ end Kernel panic - not syncing: %s\n", buf);
2291da177e4SLinus Torvalds 	local_irq_enable();
230c7ff0d9cSTAMUKI Shoichi 	for (i = 0; ; i += PANIC_TIMER_STEP) {
231c22db941SJan Beulich 		touch_softlockup_watchdog();
232c7ff0d9cSTAMUKI Shoichi 		if (i >= i_next) {
233c7ff0d9cSTAMUKI Shoichi 			i += panic_blink(state ^= 1);
234c7ff0d9cSTAMUKI Shoichi 			i_next = i + 3600 / PANIC_BLINK_SPD;
235c7ff0d9cSTAMUKI Shoichi 		}
236c7ff0d9cSTAMUKI Shoichi 		mdelay(PANIC_TIMER_STEP);
2371da177e4SLinus Torvalds 	}
2381da177e4SLinus Torvalds }
2391da177e4SLinus Torvalds 
2401da177e4SLinus Torvalds EXPORT_SYMBOL(panic);
2411da177e4SLinus Torvalds 
2421da177e4SLinus Torvalds 
24325ddbb18SAndi Kleen struct tnt {
24425ddbb18SAndi Kleen 	u8	bit;
24525ddbb18SAndi Kleen 	char	true;
24625ddbb18SAndi Kleen 	char	false;
24725ddbb18SAndi Kleen };
24825ddbb18SAndi Kleen 
24925ddbb18SAndi Kleen static const struct tnt tnts[] = {
25025ddbb18SAndi Kleen 	{ TAINT_PROPRIETARY_MODULE,	'P', 'G' },
25125ddbb18SAndi Kleen 	{ TAINT_FORCED_MODULE,		'F', ' ' },
2528c90487cSDave Jones 	{ TAINT_CPU_OUT_OF_SPEC,	'S', ' ' },
25325ddbb18SAndi Kleen 	{ TAINT_FORCED_RMMOD,		'R', ' ' },
25425ddbb18SAndi Kleen 	{ TAINT_MACHINE_CHECK,		'M', ' ' },
25525ddbb18SAndi Kleen 	{ TAINT_BAD_PAGE,		'B', ' ' },
25625ddbb18SAndi Kleen 	{ TAINT_USER,			'U', ' ' },
25725ddbb18SAndi Kleen 	{ TAINT_DIE,			'D', ' ' },
25825ddbb18SAndi Kleen 	{ TAINT_OVERRIDDEN_ACPI_TABLE,	'A', ' ' },
25925ddbb18SAndi Kleen 	{ TAINT_WARN,			'W', ' ' },
26026e9a397SLinus Torvalds 	{ TAINT_CRAP,			'C', ' ' },
26192946bc7SBen Hutchings 	{ TAINT_FIRMWARE_WORKAROUND,	'I', ' ' },
2622449b8baSBen Hutchings 	{ TAINT_OOT_MODULE,		'O', ' ' },
26357673c2bSRusty Russell 	{ TAINT_UNSIGNED_MODULE,	'E', ' ' },
26469361eefSJosh Hunt 	{ TAINT_SOFTLOCKUP,		'L', ' ' },
265c5f45465SSeth Jennings 	{ TAINT_LIVEPATCH,		'K', ' ' },
26625ddbb18SAndi Kleen };
26725ddbb18SAndi Kleen 
2681da177e4SLinus Torvalds /**
2691da177e4SLinus Torvalds  *	print_tainted - return a string to represent the kernel taint state.
2701da177e4SLinus Torvalds  *
2711da177e4SLinus Torvalds  *  'P' - Proprietary module has been loaded.
2721da177e4SLinus Torvalds  *  'F' - Module has been forcibly loaded.
2731da177e4SLinus Torvalds  *  'S' - SMP with CPUs not designed for SMP.
2741da177e4SLinus Torvalds  *  'R' - User forced a module unload.
2751da177e4SLinus Torvalds  *  'M' - System experienced a machine check exception.
2761da177e4SLinus Torvalds  *  'B' - System has hit bad_page.
2771da177e4SLinus Torvalds  *  'U' - Userspace-defined naughtiness.
278a8005992SArjan van de Ven  *  'D' - Kernel has oopsed before
2791da177e4SLinus Torvalds  *  'A' - ACPI table overridden.
2801da177e4SLinus Torvalds  *  'W' - Taint on warning.
281061b1bd3SGreg Kroah-Hartman  *  'C' - modules from drivers/staging are loaded.
28292946bc7SBen Hutchings  *  'I' - Working around severe firmware bug.
2832449b8baSBen Hutchings  *  'O' - Out-of-tree module has been loaded.
28457673c2bSRusty Russell  *  'E' - Unsigned module has been loaded.
285bc53a3f4SXie XiuQi  *  'L' - A soft lockup has previously occurred.
286c5f45465SSeth Jennings  *  'K' - Kernel has been live patched.
2871da177e4SLinus Torvalds  *
288fe002a41SRobert P. J. Day  *	The string is overwritten by the next call to print_tainted().
2891da177e4SLinus Torvalds  */
2901da177e4SLinus Torvalds const char *print_tainted(void)
2911da177e4SLinus Torvalds {
29201284764SChen Gang 	static char buf[ARRAY_SIZE(tnts) + sizeof("Tainted: ")];
29325ddbb18SAndi Kleen 
29425ddbb18SAndi Kleen 	if (tainted_mask) {
29525ddbb18SAndi Kleen 		char *s;
29625ddbb18SAndi Kleen 		int i;
29725ddbb18SAndi Kleen 
29825ddbb18SAndi Kleen 		s = buf + sprintf(buf, "Tainted: ");
29925ddbb18SAndi Kleen 		for (i = 0; i < ARRAY_SIZE(tnts); i++) {
30025ddbb18SAndi Kleen 			const struct tnt *t = &tnts[i];
30125ddbb18SAndi Kleen 			*s++ = test_bit(t->bit, &tainted_mask) ?
30225ddbb18SAndi Kleen 					t->true : t->false;
3031da177e4SLinus Torvalds 		}
30425ddbb18SAndi Kleen 		*s = 0;
30525ddbb18SAndi Kleen 	} else
3061da177e4SLinus Torvalds 		snprintf(buf, sizeof(buf), "Not tainted");
307c95dbf27SIngo Molnar 
308c95dbf27SIngo Molnar 	return buf;
3091da177e4SLinus Torvalds }
3101da177e4SLinus Torvalds 
31125ddbb18SAndi Kleen int test_taint(unsigned flag)
31225ddbb18SAndi Kleen {
31325ddbb18SAndi Kleen 	return test_bit(flag, &tainted_mask);
31425ddbb18SAndi Kleen }
31525ddbb18SAndi Kleen EXPORT_SYMBOL(test_taint);
31625ddbb18SAndi Kleen 
31725ddbb18SAndi Kleen unsigned long get_taint(void)
31825ddbb18SAndi Kleen {
31925ddbb18SAndi Kleen 	return tainted_mask;
32025ddbb18SAndi Kleen }
32125ddbb18SAndi Kleen 
322373d4d09SRusty Russell /**
323373d4d09SRusty Russell  * add_taint: add a taint flag if not already set.
324373d4d09SRusty Russell  * @flag: one of the TAINT_* constants.
325373d4d09SRusty Russell  * @lockdep_ok: whether lock debugging is still OK.
326373d4d09SRusty Russell  *
327373d4d09SRusty Russell  * If something bad has gone wrong, you'll want @lockdebug_ok = false, but for
328373d4d09SRusty Russell  * some notewortht-but-not-corrupting cases, it can be set to true.
3299eeba613SFrederic Weisbecker  */
330373d4d09SRusty Russell void add_taint(unsigned flag, enum lockdep_ok lockdep_ok)
331373d4d09SRusty Russell {
332373d4d09SRusty Russell 	if (lockdep_ok == LOCKDEP_NOW_UNRELIABLE && __debug_locks_off())
333d7c0847fSFabian Frederick 		pr_warn("Disabling lock debugging due to kernel taint\n");
3349eeba613SFrederic Weisbecker 
33525ddbb18SAndi Kleen 	set_bit(flag, &tainted_mask);
3361da177e4SLinus Torvalds }
3371da177e4SLinus Torvalds EXPORT_SYMBOL(add_taint);
338dd287796SAndrew Morton 
339dd287796SAndrew Morton static void spin_msec(int msecs)
340dd287796SAndrew Morton {
341dd287796SAndrew Morton 	int i;
342dd287796SAndrew Morton 
343dd287796SAndrew Morton 	for (i = 0; i < msecs; i++) {
344dd287796SAndrew Morton 		touch_nmi_watchdog();
345dd287796SAndrew Morton 		mdelay(1);
346dd287796SAndrew Morton 	}
347dd287796SAndrew Morton }
348dd287796SAndrew Morton 
349dd287796SAndrew Morton /*
350dd287796SAndrew Morton  * It just happens that oops_enter() and oops_exit() are identically
351dd287796SAndrew Morton  * implemented...
352dd287796SAndrew Morton  */
353dd287796SAndrew Morton static void do_oops_enter_exit(void)
354dd287796SAndrew Morton {
355dd287796SAndrew Morton 	unsigned long flags;
356dd287796SAndrew Morton 	static int spin_counter;
357dd287796SAndrew Morton 
358dd287796SAndrew Morton 	if (!pause_on_oops)
359dd287796SAndrew Morton 		return;
360dd287796SAndrew Morton 
361dd287796SAndrew Morton 	spin_lock_irqsave(&pause_on_oops_lock, flags);
362dd287796SAndrew Morton 	if (pause_on_oops_flag == 0) {
363dd287796SAndrew Morton 		/* This CPU may now print the oops message */
364dd287796SAndrew Morton 		pause_on_oops_flag = 1;
365dd287796SAndrew Morton 	} else {
366dd287796SAndrew Morton 		/* We need to stall this CPU */
367dd287796SAndrew Morton 		if (!spin_counter) {
368dd287796SAndrew Morton 			/* This CPU gets to do the counting */
369dd287796SAndrew Morton 			spin_counter = pause_on_oops;
370dd287796SAndrew Morton 			do {
371dd287796SAndrew Morton 				spin_unlock(&pause_on_oops_lock);
372dd287796SAndrew Morton 				spin_msec(MSEC_PER_SEC);
373dd287796SAndrew Morton 				spin_lock(&pause_on_oops_lock);
374dd287796SAndrew Morton 			} while (--spin_counter);
375dd287796SAndrew Morton 			pause_on_oops_flag = 0;
376dd287796SAndrew Morton 		} else {
377dd287796SAndrew Morton 			/* This CPU waits for a different one */
378dd287796SAndrew Morton 			while (spin_counter) {
379dd287796SAndrew Morton 				spin_unlock(&pause_on_oops_lock);
380dd287796SAndrew Morton 				spin_msec(1);
381dd287796SAndrew Morton 				spin_lock(&pause_on_oops_lock);
382dd287796SAndrew Morton 			}
383dd287796SAndrew Morton 		}
384dd287796SAndrew Morton 	}
385dd287796SAndrew Morton 	spin_unlock_irqrestore(&pause_on_oops_lock, flags);
386dd287796SAndrew Morton }
387dd287796SAndrew Morton 
388dd287796SAndrew Morton /*
389c95dbf27SIngo Molnar  * Return true if the calling CPU is allowed to print oops-related info.
390c95dbf27SIngo Molnar  * This is a bit racy..
391dd287796SAndrew Morton  */
392dd287796SAndrew Morton int oops_may_print(void)
393dd287796SAndrew Morton {
394dd287796SAndrew Morton 	return pause_on_oops_flag == 0;
395dd287796SAndrew Morton }
396dd287796SAndrew Morton 
397dd287796SAndrew Morton /*
398dd287796SAndrew Morton  * Called when the architecture enters its oops handler, before it prints
399c95dbf27SIngo Molnar  * anything.  If this is the first CPU to oops, and it's oopsing the first
400c95dbf27SIngo Molnar  * time then let it proceed.
401dd287796SAndrew Morton  *
402c95dbf27SIngo Molnar  * This is all enabled by the pause_on_oops kernel boot option.  We do all
403c95dbf27SIngo Molnar  * this to ensure that oopses don't scroll off the screen.  It has the
404c95dbf27SIngo Molnar  * side-effect of preventing later-oopsing CPUs from mucking up the display,
405c95dbf27SIngo Molnar  * too.
406dd287796SAndrew Morton  *
407c95dbf27SIngo Molnar  * It turns out that the CPU which is allowed to print ends up pausing for
408c95dbf27SIngo Molnar  * the right duration, whereas all the other CPUs pause for twice as long:
409c95dbf27SIngo Molnar  * once in oops_enter(), once in oops_exit().
410dd287796SAndrew Morton  */
411dd287796SAndrew Morton void oops_enter(void)
412dd287796SAndrew Morton {
413bdff7870SThomas Gleixner 	tracing_off();
414c95dbf27SIngo Molnar 	/* can't trust the integrity of the kernel anymore: */
415c95dbf27SIngo Molnar 	debug_locks_off();
416dd287796SAndrew Morton 	do_oops_enter_exit();
417dd287796SAndrew Morton }
418dd287796SAndrew Morton 
419dd287796SAndrew Morton /*
4202c3b20e9SArjan van de Ven  * 64-bit random ID for oopses:
4212c3b20e9SArjan van de Ven  */
4222c3b20e9SArjan van de Ven static u64 oops_id;
4232c3b20e9SArjan van de Ven 
4242c3b20e9SArjan van de Ven static int init_oops_id(void)
4252c3b20e9SArjan van de Ven {
4262c3b20e9SArjan van de Ven 	if (!oops_id)
4272c3b20e9SArjan van de Ven 		get_random_bytes(&oops_id, sizeof(oops_id));
428d6624f99SArjan van de Ven 	else
429d6624f99SArjan van de Ven 		oops_id++;
4302c3b20e9SArjan van de Ven 
4312c3b20e9SArjan van de Ven 	return 0;
4322c3b20e9SArjan van de Ven }
4332c3b20e9SArjan van de Ven late_initcall(init_oops_id);
4342c3b20e9SArjan van de Ven 
435863a6049SAnton Blanchard void print_oops_end_marker(void)
43671c33911SArjan van de Ven {
43771c33911SArjan van de Ven 	init_oops_id();
438d7c0847fSFabian Frederick 	pr_warn("---[ end trace %016llx ]---\n", (unsigned long long)oops_id);
43971c33911SArjan van de Ven }
44071c33911SArjan van de Ven 
4412c3b20e9SArjan van de Ven /*
442dd287796SAndrew Morton  * Called when the architecture exits its oops handler, after printing
443dd287796SAndrew Morton  * everything.
444dd287796SAndrew Morton  */
445dd287796SAndrew Morton void oops_exit(void)
446dd287796SAndrew Morton {
447dd287796SAndrew Morton 	do_oops_enter_exit();
44871c33911SArjan van de Ven 	print_oops_end_marker();
449456b565cSSimon Kagstrom 	kmsg_dump(KMSG_DUMP_OOPS);
450dd287796SAndrew Morton }
4513162f751SArjan van de Ven 
45279b4cc5eSArjan van de Ven #ifdef WANT_WARN_ON_SLOWPATH
4530f6f49a8SLinus Torvalds struct slowpath_args {
4540f6f49a8SLinus Torvalds 	const char *fmt;
455a8f18b90SArjan van de Ven 	va_list args;
4560f6f49a8SLinus Torvalds };
4570f6f49a8SLinus Torvalds 
458b2be0527SBen Hutchings static void warn_slowpath_common(const char *file, int line, void *caller,
459b2be0527SBen Hutchings 				 unsigned taint, struct slowpath_args *args)
4600f6f49a8SLinus Torvalds {
461de7edd31SSteven Rostedt (Red Hat) 	disable_trace_on_warning();
462de7edd31SSteven Rostedt (Red Hat) 
463dcb6b452SAlex Thorlton 	pr_warn("------------[ cut here ]------------\n");
464dcb6b452SAlex Thorlton 	pr_warn("WARNING: CPU: %d PID: %d at %s:%d %pS()\n",
465dcb6b452SAlex Thorlton 		raw_smp_processor_id(), current->pid, file, line, caller);
46674853dbaSArjan van de Ven 
4670f6f49a8SLinus Torvalds 	if (args)
4680f6f49a8SLinus Torvalds 		vprintk(args->fmt, args->args);
469a8f18b90SArjan van de Ven 
4709e3961a0SPrarit Bhargava 	if (panic_on_warn) {
4719e3961a0SPrarit Bhargava 		/*
4729e3961a0SPrarit Bhargava 		 * This thread may hit another WARN() in the panic path.
4739e3961a0SPrarit Bhargava 		 * Resetting this prevents additional WARN() from panicking the
4749e3961a0SPrarit Bhargava 		 * system on this thread.  Other threads are blocked by the
4759e3961a0SPrarit Bhargava 		 * panic_mutex in panic().
4769e3961a0SPrarit Bhargava 		 */
4779e3961a0SPrarit Bhargava 		panic_on_warn = 0;
4789e3961a0SPrarit Bhargava 		panic("panic_on_warn set ...\n");
4799e3961a0SPrarit Bhargava 	}
4809e3961a0SPrarit Bhargava 
481a8f18b90SArjan van de Ven 	print_modules();
482a8f18b90SArjan van de Ven 	dump_stack();
483a8f18b90SArjan van de Ven 	print_oops_end_marker();
484373d4d09SRusty Russell 	/* Just a warning, don't kill lockdep. */
485373d4d09SRusty Russell 	add_taint(taint, LOCKDEP_STILL_OK);
486a8f18b90SArjan van de Ven }
4870f6f49a8SLinus Torvalds 
4880f6f49a8SLinus Torvalds void warn_slowpath_fmt(const char *file, int line, const char *fmt, ...)
4890f6f49a8SLinus Torvalds {
4900f6f49a8SLinus Torvalds 	struct slowpath_args args;
4910f6f49a8SLinus Torvalds 
4920f6f49a8SLinus Torvalds 	args.fmt = fmt;
4930f6f49a8SLinus Torvalds 	va_start(args.args, fmt);
494b2be0527SBen Hutchings 	warn_slowpath_common(file, line, __builtin_return_address(0),
495b2be0527SBen Hutchings 			     TAINT_WARN, &args);
4960f6f49a8SLinus Torvalds 	va_end(args.args);
4970f6f49a8SLinus Torvalds }
49857adc4d2SAndi Kleen EXPORT_SYMBOL(warn_slowpath_fmt);
49957adc4d2SAndi Kleen 
500b2be0527SBen Hutchings void warn_slowpath_fmt_taint(const char *file, int line,
501b2be0527SBen Hutchings 			     unsigned taint, const char *fmt, ...)
502b2be0527SBen Hutchings {
503b2be0527SBen Hutchings 	struct slowpath_args args;
504b2be0527SBen Hutchings 
505b2be0527SBen Hutchings 	args.fmt = fmt;
506b2be0527SBen Hutchings 	va_start(args.args, fmt);
507b2be0527SBen Hutchings 	warn_slowpath_common(file, line, __builtin_return_address(0),
508b2be0527SBen Hutchings 			     taint, &args);
509b2be0527SBen Hutchings 	va_end(args.args);
510b2be0527SBen Hutchings }
511b2be0527SBen Hutchings EXPORT_SYMBOL(warn_slowpath_fmt_taint);
512b2be0527SBen Hutchings 
51357adc4d2SAndi Kleen void warn_slowpath_null(const char *file, int line)
51457adc4d2SAndi Kleen {
515b2be0527SBen Hutchings 	warn_slowpath_common(file, line, __builtin_return_address(0),
516b2be0527SBen Hutchings 			     TAINT_WARN, NULL);
51757adc4d2SAndi Kleen }
51857adc4d2SAndi Kleen EXPORT_SYMBOL(warn_slowpath_null);
51979b4cc5eSArjan van de Ven #endif
52079b4cc5eSArjan van de Ven 
5213162f751SArjan van de Ven #ifdef CONFIG_CC_STACKPROTECTOR
52254371a43SArjan van de Ven 
5233162f751SArjan van de Ven /*
5243162f751SArjan van de Ven  * Called when gcc's -fstack-protector feature is used, and
5253162f751SArjan van de Ven  * gcc detects corruption of the on-stack canary value
5263162f751SArjan van de Ven  */
527a7330c99SAndi Kleen __visible void __stack_chk_fail(void)
5283162f751SArjan van de Ven {
529517a92c4SIngo Molnar 	panic("stack-protector: Kernel stack is corrupted in: %p\n",
530517a92c4SIngo Molnar 		__builtin_return_address(0));
5313162f751SArjan van de Ven }
5323162f751SArjan van de Ven EXPORT_SYMBOL(__stack_chk_fail);
53354371a43SArjan van de Ven 
5343162f751SArjan van de Ven #endif
535f44dd164SRusty Russell 
536f44dd164SRusty Russell core_param(panic, panic_timeout, int, 0644);
537f44dd164SRusty Russell core_param(pause_on_oops, pause_on_oops, int, 0644);
5389e3961a0SPrarit Bhargava core_param(panic_on_warn, panic_on_warn, int, 0644);
539d404ab0aSOlaf Hering 
540f06e5153SMasami Hiramatsu static int __init setup_crash_kexec_post_notifiers(char *s)
541f06e5153SMasami Hiramatsu {
542f06e5153SMasami Hiramatsu 	crash_kexec_post_notifiers = true;
543f06e5153SMasami Hiramatsu 	return 0;
544f06e5153SMasami Hiramatsu }
545f06e5153SMasami Hiramatsu early_param("crash_kexec_post_notifiers", setup_crash_kexec_post_notifiers);
546f06e5153SMasami Hiramatsu 
547d404ab0aSOlaf Hering static int __init oops_setup(char *s)
548d404ab0aSOlaf Hering {
549d404ab0aSOlaf Hering 	if (!s)
550d404ab0aSOlaf Hering 		return -EINVAL;
551d404ab0aSOlaf Hering 	if (!strcmp(s, "panic"))
552d404ab0aSOlaf Hering 		panic_on_oops = 1;
553d404ab0aSOlaf Hering 	return 0;
554d404ab0aSOlaf Hering }
555d404ab0aSOlaf Hering early_param("oops", oops_setup);
556