xref: /linux-6.15/kernel/panic.c (revision 7bbee5ca)
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.
139*7bbee5caSHidehiro Kawai 	 *
140*7bbee5caSHidehiro Kawai 	 * Bypass the panic_cpu check and call __crash_kexec directly.
141dc009d92SEric W. Biederman 	 */
142f06e5153SMasami Hiramatsu 	if (!crash_kexec_post_notifiers)
143*7bbee5caSHidehiro 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.
166*7bbee5caSHidehiro Kawai 	 *
167*7bbee5caSHidehiro Kawai 	 * Bypass the panic_cpu check and call __crash_kexec directly.
168f06e5153SMasami Hiramatsu 	 */
169f45d85ffSHATAYAMA Daisuke 	if (crash_kexec_post_notifiers)
170*7bbee5caSHidehiro 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();
18308d78658SVitaly Kuznetsov 	console_trylock();
18408d78658SVitaly Kuznetsov 	console_unlock();
18508d78658SVitaly Kuznetsov 
186c7ff0d9cSTAMUKI Shoichi 	if (!panic_blink)
187c7ff0d9cSTAMUKI Shoichi 		panic_blink = no_blink;
188c7ff0d9cSTAMUKI Shoichi 
189dc009d92SEric W. Biederman 	if (panic_timeout > 0) {
1901da177e4SLinus Torvalds 		/*
1911da177e4SLinus Torvalds 		 * Delay timeout seconds before rebooting the machine.
192c95dbf27SIngo Molnar 		 * We can't use the "normal" timers since we just panicked.
1931da177e4SLinus Torvalds 		 */
194d7c0847fSFabian Frederick 		pr_emerg("Rebooting in %d seconds..", panic_timeout);
195c95dbf27SIngo Molnar 
196c7ff0d9cSTAMUKI Shoichi 		for (i = 0; i < panic_timeout * 1000; i += PANIC_TIMER_STEP) {
1971da177e4SLinus Torvalds 			touch_nmi_watchdog();
198c7ff0d9cSTAMUKI Shoichi 			if (i >= i_next) {
199c7ff0d9cSTAMUKI Shoichi 				i += panic_blink(state ^= 1);
200c7ff0d9cSTAMUKI Shoichi 				i_next = i + 3600 / PANIC_BLINK_SPD;
201c7ff0d9cSTAMUKI Shoichi 			}
202c7ff0d9cSTAMUKI Shoichi 			mdelay(PANIC_TIMER_STEP);
2031da177e4SLinus Torvalds 		}
2044302fbc8SHugh Dickins 	}
2054302fbc8SHugh Dickins 	if (panic_timeout != 0) {
206c95dbf27SIngo Molnar 		/*
207c95dbf27SIngo Molnar 		 * This will not be a clean reboot, with everything
2082f048ea8SEric W. Biederman 		 * shutting down.  But if there is a chance of
2092f048ea8SEric W. Biederman 		 * rebooting the system it will be rebooted.
2101da177e4SLinus Torvalds 		 */
2112f048ea8SEric W. Biederman 		emergency_restart();
2121da177e4SLinus Torvalds 	}
2131da177e4SLinus Torvalds #ifdef __sparc__
2141da177e4SLinus Torvalds 	{
2151da177e4SLinus Torvalds 		extern int stop_a_enabled;
216a271c241STom 'spot' Callaway 		/* Make sure the user can actually press Stop-A (L1-A) */
2171da177e4SLinus Torvalds 		stop_a_enabled = 1;
218d7c0847fSFabian Frederick 		pr_emerg("Press Stop-A (L1-A) to return to the boot prom\n");
2191da177e4SLinus Torvalds 	}
2201da177e4SLinus Torvalds #endif
221347a8dc3SMartin Schwidefsky #if defined(CONFIG_S390)
222c95dbf27SIngo Molnar 	{
223c95dbf27SIngo Molnar 		unsigned long caller;
224c95dbf27SIngo Molnar 
225c95dbf27SIngo Molnar 		caller = (unsigned long)__builtin_return_address(0);
2261da177e4SLinus Torvalds 		disabled_wait(caller);
227c95dbf27SIngo Molnar 	}
2281da177e4SLinus Torvalds #endif
229d7c0847fSFabian Frederick 	pr_emerg("---[ end Kernel panic - not syncing: %s\n", buf);
2301da177e4SLinus Torvalds 	local_irq_enable();
231c7ff0d9cSTAMUKI Shoichi 	for (i = 0; ; i += PANIC_TIMER_STEP) {
232c22db941SJan Beulich 		touch_softlockup_watchdog();
233c7ff0d9cSTAMUKI Shoichi 		if (i >= i_next) {
234c7ff0d9cSTAMUKI Shoichi 			i += panic_blink(state ^= 1);
235c7ff0d9cSTAMUKI Shoichi 			i_next = i + 3600 / PANIC_BLINK_SPD;
236c7ff0d9cSTAMUKI Shoichi 		}
237c7ff0d9cSTAMUKI Shoichi 		mdelay(PANIC_TIMER_STEP);
2381da177e4SLinus Torvalds 	}
2391da177e4SLinus Torvalds }
2401da177e4SLinus Torvalds 
2411da177e4SLinus Torvalds EXPORT_SYMBOL(panic);
2421da177e4SLinus Torvalds 
2431da177e4SLinus Torvalds 
24425ddbb18SAndi Kleen struct tnt {
24525ddbb18SAndi Kleen 	u8	bit;
24625ddbb18SAndi Kleen 	char	true;
24725ddbb18SAndi Kleen 	char	false;
24825ddbb18SAndi Kleen };
24925ddbb18SAndi Kleen 
25025ddbb18SAndi Kleen static const struct tnt tnts[] = {
25125ddbb18SAndi Kleen 	{ TAINT_PROPRIETARY_MODULE,	'P', 'G' },
25225ddbb18SAndi Kleen 	{ TAINT_FORCED_MODULE,		'F', ' ' },
2538c90487cSDave Jones 	{ TAINT_CPU_OUT_OF_SPEC,	'S', ' ' },
25425ddbb18SAndi Kleen 	{ TAINT_FORCED_RMMOD,		'R', ' ' },
25525ddbb18SAndi Kleen 	{ TAINT_MACHINE_CHECK,		'M', ' ' },
25625ddbb18SAndi Kleen 	{ TAINT_BAD_PAGE,		'B', ' ' },
25725ddbb18SAndi Kleen 	{ TAINT_USER,			'U', ' ' },
25825ddbb18SAndi Kleen 	{ TAINT_DIE,			'D', ' ' },
25925ddbb18SAndi Kleen 	{ TAINT_OVERRIDDEN_ACPI_TABLE,	'A', ' ' },
26025ddbb18SAndi Kleen 	{ TAINT_WARN,			'W', ' ' },
26126e9a397SLinus Torvalds 	{ TAINT_CRAP,			'C', ' ' },
26292946bc7SBen Hutchings 	{ TAINT_FIRMWARE_WORKAROUND,	'I', ' ' },
2632449b8baSBen Hutchings 	{ TAINT_OOT_MODULE,		'O', ' ' },
26457673c2bSRusty Russell 	{ TAINT_UNSIGNED_MODULE,	'E', ' ' },
26569361eefSJosh Hunt 	{ TAINT_SOFTLOCKUP,		'L', ' ' },
266c5f45465SSeth Jennings 	{ TAINT_LIVEPATCH,		'K', ' ' },
26725ddbb18SAndi Kleen };
26825ddbb18SAndi Kleen 
2691da177e4SLinus Torvalds /**
2701da177e4SLinus Torvalds  *	print_tainted - return a string to represent the kernel taint state.
2711da177e4SLinus Torvalds  *
2721da177e4SLinus Torvalds  *  'P' - Proprietary module has been loaded.
2731da177e4SLinus Torvalds  *  'F' - Module has been forcibly loaded.
2741da177e4SLinus Torvalds  *  'S' - SMP with CPUs not designed for SMP.
2751da177e4SLinus Torvalds  *  'R' - User forced a module unload.
2761da177e4SLinus Torvalds  *  'M' - System experienced a machine check exception.
2771da177e4SLinus Torvalds  *  'B' - System has hit bad_page.
2781da177e4SLinus Torvalds  *  'U' - Userspace-defined naughtiness.
279a8005992SArjan van de Ven  *  'D' - Kernel has oopsed before
2801da177e4SLinus Torvalds  *  'A' - ACPI table overridden.
2811da177e4SLinus Torvalds  *  'W' - Taint on warning.
282061b1bd3SGreg Kroah-Hartman  *  'C' - modules from drivers/staging are loaded.
28392946bc7SBen Hutchings  *  'I' - Working around severe firmware bug.
2842449b8baSBen Hutchings  *  'O' - Out-of-tree module has been loaded.
28557673c2bSRusty Russell  *  'E' - Unsigned module has been loaded.
286bc53a3f4SXie XiuQi  *  'L' - A soft lockup has previously occurred.
287c5f45465SSeth Jennings  *  'K' - Kernel has been live patched.
2881da177e4SLinus Torvalds  *
289fe002a41SRobert P. J. Day  *	The string is overwritten by the next call to print_tainted().
2901da177e4SLinus Torvalds  */
2911da177e4SLinus Torvalds const char *print_tainted(void)
2921da177e4SLinus Torvalds {
29301284764SChen Gang 	static char buf[ARRAY_SIZE(tnts) + sizeof("Tainted: ")];
29425ddbb18SAndi Kleen 
29525ddbb18SAndi Kleen 	if (tainted_mask) {
29625ddbb18SAndi Kleen 		char *s;
29725ddbb18SAndi Kleen 		int i;
29825ddbb18SAndi Kleen 
29925ddbb18SAndi Kleen 		s = buf + sprintf(buf, "Tainted: ");
30025ddbb18SAndi Kleen 		for (i = 0; i < ARRAY_SIZE(tnts); i++) {
30125ddbb18SAndi Kleen 			const struct tnt *t = &tnts[i];
30225ddbb18SAndi Kleen 			*s++ = test_bit(t->bit, &tainted_mask) ?
30325ddbb18SAndi Kleen 					t->true : t->false;
3041da177e4SLinus Torvalds 		}
30525ddbb18SAndi Kleen 		*s = 0;
30625ddbb18SAndi Kleen 	} else
3071da177e4SLinus Torvalds 		snprintf(buf, sizeof(buf), "Not tainted");
308c95dbf27SIngo Molnar 
309c95dbf27SIngo Molnar 	return buf;
3101da177e4SLinus Torvalds }
3111da177e4SLinus Torvalds 
31225ddbb18SAndi Kleen int test_taint(unsigned flag)
31325ddbb18SAndi Kleen {
31425ddbb18SAndi Kleen 	return test_bit(flag, &tainted_mask);
31525ddbb18SAndi Kleen }
31625ddbb18SAndi Kleen EXPORT_SYMBOL(test_taint);
31725ddbb18SAndi Kleen 
31825ddbb18SAndi Kleen unsigned long get_taint(void)
31925ddbb18SAndi Kleen {
32025ddbb18SAndi Kleen 	return tainted_mask;
32125ddbb18SAndi Kleen }
32225ddbb18SAndi Kleen 
323373d4d09SRusty Russell /**
324373d4d09SRusty Russell  * add_taint: add a taint flag if not already set.
325373d4d09SRusty Russell  * @flag: one of the TAINT_* constants.
326373d4d09SRusty Russell  * @lockdep_ok: whether lock debugging is still OK.
327373d4d09SRusty Russell  *
328373d4d09SRusty Russell  * If something bad has gone wrong, you'll want @lockdebug_ok = false, but for
329373d4d09SRusty Russell  * some notewortht-but-not-corrupting cases, it can be set to true.
3309eeba613SFrederic Weisbecker  */
331373d4d09SRusty Russell void add_taint(unsigned flag, enum lockdep_ok lockdep_ok)
332373d4d09SRusty Russell {
333373d4d09SRusty Russell 	if (lockdep_ok == LOCKDEP_NOW_UNRELIABLE && __debug_locks_off())
334d7c0847fSFabian Frederick 		pr_warn("Disabling lock debugging due to kernel taint\n");
3359eeba613SFrederic Weisbecker 
33625ddbb18SAndi Kleen 	set_bit(flag, &tainted_mask);
3371da177e4SLinus Torvalds }
3381da177e4SLinus Torvalds EXPORT_SYMBOL(add_taint);
339dd287796SAndrew Morton 
340dd287796SAndrew Morton static void spin_msec(int msecs)
341dd287796SAndrew Morton {
342dd287796SAndrew Morton 	int i;
343dd287796SAndrew Morton 
344dd287796SAndrew Morton 	for (i = 0; i < msecs; i++) {
345dd287796SAndrew Morton 		touch_nmi_watchdog();
346dd287796SAndrew Morton 		mdelay(1);
347dd287796SAndrew Morton 	}
348dd287796SAndrew Morton }
349dd287796SAndrew Morton 
350dd287796SAndrew Morton /*
351dd287796SAndrew Morton  * It just happens that oops_enter() and oops_exit() are identically
352dd287796SAndrew Morton  * implemented...
353dd287796SAndrew Morton  */
354dd287796SAndrew Morton static void do_oops_enter_exit(void)
355dd287796SAndrew Morton {
356dd287796SAndrew Morton 	unsigned long flags;
357dd287796SAndrew Morton 	static int spin_counter;
358dd287796SAndrew Morton 
359dd287796SAndrew Morton 	if (!pause_on_oops)
360dd287796SAndrew Morton 		return;
361dd287796SAndrew Morton 
362dd287796SAndrew Morton 	spin_lock_irqsave(&pause_on_oops_lock, flags);
363dd287796SAndrew Morton 	if (pause_on_oops_flag == 0) {
364dd287796SAndrew Morton 		/* This CPU may now print the oops message */
365dd287796SAndrew Morton 		pause_on_oops_flag = 1;
366dd287796SAndrew Morton 	} else {
367dd287796SAndrew Morton 		/* We need to stall this CPU */
368dd287796SAndrew Morton 		if (!spin_counter) {
369dd287796SAndrew Morton 			/* This CPU gets to do the counting */
370dd287796SAndrew Morton 			spin_counter = pause_on_oops;
371dd287796SAndrew Morton 			do {
372dd287796SAndrew Morton 				spin_unlock(&pause_on_oops_lock);
373dd287796SAndrew Morton 				spin_msec(MSEC_PER_SEC);
374dd287796SAndrew Morton 				spin_lock(&pause_on_oops_lock);
375dd287796SAndrew Morton 			} while (--spin_counter);
376dd287796SAndrew Morton 			pause_on_oops_flag = 0;
377dd287796SAndrew Morton 		} else {
378dd287796SAndrew Morton 			/* This CPU waits for a different one */
379dd287796SAndrew Morton 			while (spin_counter) {
380dd287796SAndrew Morton 				spin_unlock(&pause_on_oops_lock);
381dd287796SAndrew Morton 				spin_msec(1);
382dd287796SAndrew Morton 				spin_lock(&pause_on_oops_lock);
383dd287796SAndrew Morton 			}
384dd287796SAndrew Morton 		}
385dd287796SAndrew Morton 	}
386dd287796SAndrew Morton 	spin_unlock_irqrestore(&pause_on_oops_lock, flags);
387dd287796SAndrew Morton }
388dd287796SAndrew Morton 
389dd287796SAndrew Morton /*
390c95dbf27SIngo Molnar  * Return true if the calling CPU is allowed to print oops-related info.
391c95dbf27SIngo Molnar  * This is a bit racy..
392dd287796SAndrew Morton  */
393dd287796SAndrew Morton int oops_may_print(void)
394dd287796SAndrew Morton {
395dd287796SAndrew Morton 	return pause_on_oops_flag == 0;
396dd287796SAndrew Morton }
397dd287796SAndrew Morton 
398dd287796SAndrew Morton /*
399dd287796SAndrew Morton  * Called when the architecture enters its oops handler, before it prints
400c95dbf27SIngo Molnar  * anything.  If this is the first CPU to oops, and it's oopsing the first
401c95dbf27SIngo Molnar  * time then let it proceed.
402dd287796SAndrew Morton  *
403c95dbf27SIngo Molnar  * This is all enabled by the pause_on_oops kernel boot option.  We do all
404c95dbf27SIngo Molnar  * this to ensure that oopses don't scroll off the screen.  It has the
405c95dbf27SIngo Molnar  * side-effect of preventing later-oopsing CPUs from mucking up the display,
406c95dbf27SIngo Molnar  * too.
407dd287796SAndrew Morton  *
408c95dbf27SIngo Molnar  * It turns out that the CPU which is allowed to print ends up pausing for
409c95dbf27SIngo Molnar  * the right duration, whereas all the other CPUs pause for twice as long:
410c95dbf27SIngo Molnar  * once in oops_enter(), once in oops_exit().
411dd287796SAndrew Morton  */
412dd287796SAndrew Morton void oops_enter(void)
413dd287796SAndrew Morton {
414bdff7870SThomas Gleixner 	tracing_off();
415c95dbf27SIngo Molnar 	/* can't trust the integrity of the kernel anymore: */
416c95dbf27SIngo Molnar 	debug_locks_off();
417dd287796SAndrew Morton 	do_oops_enter_exit();
418dd287796SAndrew Morton }
419dd287796SAndrew Morton 
420dd287796SAndrew Morton /*
4212c3b20e9SArjan van de Ven  * 64-bit random ID for oopses:
4222c3b20e9SArjan van de Ven  */
4232c3b20e9SArjan van de Ven static u64 oops_id;
4242c3b20e9SArjan van de Ven 
4252c3b20e9SArjan van de Ven static int init_oops_id(void)
4262c3b20e9SArjan van de Ven {
4272c3b20e9SArjan van de Ven 	if (!oops_id)
4282c3b20e9SArjan van de Ven 		get_random_bytes(&oops_id, sizeof(oops_id));
429d6624f99SArjan van de Ven 	else
430d6624f99SArjan van de Ven 		oops_id++;
4312c3b20e9SArjan van de Ven 
4322c3b20e9SArjan van de Ven 	return 0;
4332c3b20e9SArjan van de Ven }
4342c3b20e9SArjan van de Ven late_initcall(init_oops_id);
4352c3b20e9SArjan van de Ven 
436863a6049SAnton Blanchard void print_oops_end_marker(void)
43771c33911SArjan van de Ven {
43871c33911SArjan van de Ven 	init_oops_id();
439d7c0847fSFabian Frederick 	pr_warn("---[ end trace %016llx ]---\n", (unsigned long long)oops_id);
44071c33911SArjan van de Ven }
44171c33911SArjan van de Ven 
4422c3b20e9SArjan van de Ven /*
443dd287796SAndrew Morton  * Called when the architecture exits its oops handler, after printing
444dd287796SAndrew Morton  * everything.
445dd287796SAndrew Morton  */
446dd287796SAndrew Morton void oops_exit(void)
447dd287796SAndrew Morton {
448dd287796SAndrew Morton 	do_oops_enter_exit();
44971c33911SArjan van de Ven 	print_oops_end_marker();
450456b565cSSimon Kagstrom 	kmsg_dump(KMSG_DUMP_OOPS);
451dd287796SAndrew Morton }
4523162f751SArjan van de Ven 
45379b4cc5eSArjan van de Ven #ifdef WANT_WARN_ON_SLOWPATH
4540f6f49a8SLinus Torvalds struct slowpath_args {
4550f6f49a8SLinus Torvalds 	const char *fmt;
456a8f18b90SArjan van de Ven 	va_list args;
4570f6f49a8SLinus Torvalds };
4580f6f49a8SLinus Torvalds 
459b2be0527SBen Hutchings static void warn_slowpath_common(const char *file, int line, void *caller,
460b2be0527SBen Hutchings 				 unsigned taint, struct slowpath_args *args)
4610f6f49a8SLinus Torvalds {
462de7edd31SSteven Rostedt (Red Hat) 	disable_trace_on_warning();
463de7edd31SSteven Rostedt (Red Hat) 
464dcb6b452SAlex Thorlton 	pr_warn("------------[ cut here ]------------\n");
465dcb6b452SAlex Thorlton 	pr_warn("WARNING: CPU: %d PID: %d at %s:%d %pS()\n",
466dcb6b452SAlex Thorlton 		raw_smp_processor_id(), current->pid, file, line, caller);
46774853dbaSArjan van de Ven 
4680f6f49a8SLinus Torvalds 	if (args)
4690f6f49a8SLinus Torvalds 		vprintk(args->fmt, args->args);
470a8f18b90SArjan van de Ven 
4719e3961a0SPrarit Bhargava 	if (panic_on_warn) {
4729e3961a0SPrarit Bhargava 		/*
4739e3961a0SPrarit Bhargava 		 * This thread may hit another WARN() in the panic path.
4749e3961a0SPrarit Bhargava 		 * Resetting this prevents additional WARN() from panicking the
4759e3961a0SPrarit Bhargava 		 * system on this thread.  Other threads are blocked by the
4769e3961a0SPrarit Bhargava 		 * panic_mutex in panic().
4779e3961a0SPrarit Bhargava 		 */
4789e3961a0SPrarit Bhargava 		panic_on_warn = 0;
4799e3961a0SPrarit Bhargava 		panic("panic_on_warn set ...\n");
4809e3961a0SPrarit Bhargava 	}
4819e3961a0SPrarit Bhargava 
482a8f18b90SArjan van de Ven 	print_modules();
483a8f18b90SArjan van de Ven 	dump_stack();
484a8f18b90SArjan van de Ven 	print_oops_end_marker();
485373d4d09SRusty Russell 	/* Just a warning, don't kill lockdep. */
486373d4d09SRusty Russell 	add_taint(taint, LOCKDEP_STILL_OK);
487a8f18b90SArjan van de Ven }
4880f6f49a8SLinus Torvalds 
4890f6f49a8SLinus Torvalds void warn_slowpath_fmt(const char *file, int line, const char *fmt, ...)
4900f6f49a8SLinus Torvalds {
4910f6f49a8SLinus Torvalds 	struct slowpath_args args;
4920f6f49a8SLinus Torvalds 
4930f6f49a8SLinus Torvalds 	args.fmt = fmt;
4940f6f49a8SLinus Torvalds 	va_start(args.args, fmt);
495b2be0527SBen Hutchings 	warn_slowpath_common(file, line, __builtin_return_address(0),
496b2be0527SBen Hutchings 			     TAINT_WARN, &args);
4970f6f49a8SLinus Torvalds 	va_end(args.args);
4980f6f49a8SLinus Torvalds }
49957adc4d2SAndi Kleen EXPORT_SYMBOL(warn_slowpath_fmt);
50057adc4d2SAndi Kleen 
501b2be0527SBen Hutchings void warn_slowpath_fmt_taint(const char *file, int line,
502b2be0527SBen Hutchings 			     unsigned taint, const char *fmt, ...)
503b2be0527SBen Hutchings {
504b2be0527SBen Hutchings 	struct slowpath_args args;
505b2be0527SBen Hutchings 
506b2be0527SBen Hutchings 	args.fmt = fmt;
507b2be0527SBen Hutchings 	va_start(args.args, fmt);
508b2be0527SBen Hutchings 	warn_slowpath_common(file, line, __builtin_return_address(0),
509b2be0527SBen Hutchings 			     taint, &args);
510b2be0527SBen Hutchings 	va_end(args.args);
511b2be0527SBen Hutchings }
512b2be0527SBen Hutchings EXPORT_SYMBOL(warn_slowpath_fmt_taint);
513b2be0527SBen Hutchings 
51457adc4d2SAndi Kleen void warn_slowpath_null(const char *file, int line)
51557adc4d2SAndi Kleen {
516b2be0527SBen Hutchings 	warn_slowpath_common(file, line, __builtin_return_address(0),
517b2be0527SBen Hutchings 			     TAINT_WARN, NULL);
51857adc4d2SAndi Kleen }
51957adc4d2SAndi Kleen EXPORT_SYMBOL(warn_slowpath_null);
52079b4cc5eSArjan van de Ven #endif
52179b4cc5eSArjan van de Ven 
5223162f751SArjan van de Ven #ifdef CONFIG_CC_STACKPROTECTOR
52354371a43SArjan van de Ven 
5243162f751SArjan van de Ven /*
5253162f751SArjan van de Ven  * Called when gcc's -fstack-protector feature is used, and
5263162f751SArjan van de Ven  * gcc detects corruption of the on-stack canary value
5273162f751SArjan van de Ven  */
528a7330c99SAndi Kleen __visible void __stack_chk_fail(void)
5293162f751SArjan van de Ven {
530517a92c4SIngo Molnar 	panic("stack-protector: Kernel stack is corrupted in: %p\n",
531517a92c4SIngo Molnar 		__builtin_return_address(0));
5323162f751SArjan van de Ven }
5333162f751SArjan van de Ven EXPORT_SYMBOL(__stack_chk_fail);
53454371a43SArjan van de Ven 
5353162f751SArjan van de Ven #endif
536f44dd164SRusty Russell 
537f44dd164SRusty Russell core_param(panic, panic_timeout, int, 0644);
538f44dd164SRusty Russell core_param(pause_on_oops, pause_on_oops, int, 0644);
5399e3961a0SPrarit Bhargava core_param(panic_on_warn, panic_on_warn, int, 0644);
540d404ab0aSOlaf Hering 
541f06e5153SMasami Hiramatsu static int __init setup_crash_kexec_post_notifiers(char *s)
542f06e5153SMasami Hiramatsu {
543f06e5153SMasami Hiramatsu 	crash_kexec_post_notifiers = true;
544f06e5153SMasami Hiramatsu 	return 0;
545f06e5153SMasami Hiramatsu }
546f06e5153SMasami Hiramatsu early_param("crash_kexec_post_notifiers", setup_crash_kexec_post_notifiers);
547f06e5153SMasami Hiramatsu 
548d404ab0aSOlaf Hering static int __init oops_setup(char *s)
549d404ab0aSOlaf Hering {
550d404ab0aSOlaf Hering 	if (!s)
551d404ab0aSOlaf Hering 		return -EINVAL;
552d404ab0aSOlaf Hering 	if (!strcmp(s, "panic"))
553d404ab0aSOlaf Hering 		panic_on_oops = 1;
554d404ab0aSOlaf Hering 	return 0;
555d404ab0aSOlaf Hering }
556d404ab0aSOlaf Hering early_param("oops", oops_setup);
557