xref: /linux-6.15/kernel/panic.c (revision aaf5dcfb)
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>
12b17b0153SIngo Molnar #include <linux/sched/debug.h>
13c95dbf27SIngo Molnar #include <linux/interrupt.h>
14456b565cSSimon Kagstrom #include <linux/kmsg_dump.h>
1579b4cc5eSArjan van de Ven #include <linux/kallsyms.h>
16c95dbf27SIngo Molnar #include <linux/notifier.h>
17c95dbf27SIngo Molnar #include <linux/module.h>
18c95dbf27SIngo Molnar #include <linux/random.h>
19de7edd31SSteven Rostedt (Red Hat) #include <linux/ftrace.h>
20c95dbf27SIngo Molnar #include <linux/reboot.h>
21c95dbf27SIngo Molnar #include <linux/delay.h>
22c95dbf27SIngo Molnar #include <linux/kexec.h>
23c95dbf27SIngo Molnar #include <linux/sched.h>
24c95dbf27SIngo Molnar #include <linux/sysrq.h>
25c95dbf27SIngo Molnar #include <linux/init.h>
26c95dbf27SIngo Molnar #include <linux/nmi.h>
2708d78658SVitaly Kuznetsov #include <linux/console.h>
282553b67aSJosh Poimboeuf #include <linux/bug.h>
297a46ec0eSKees Cook #include <linux/ratelimit.h>
30b1fca27dSAndi Kleen #include <linux/debugfs.h>
31b1fca27dSAndi Kleen #include <asm/sections.h>
321da177e4SLinus Torvalds 
33c7ff0d9cSTAMUKI Shoichi #define PANIC_TIMER_STEP 100
34c7ff0d9cSTAMUKI Shoichi #define PANIC_BLINK_SPD 18
35c7ff0d9cSTAMUKI Shoichi 
362a01bb38SKyle McMartin int panic_on_oops = CONFIG_PANIC_ON_OOPS_VALUE;
3725ddbb18SAndi Kleen static unsigned long tainted_mask;
38dd287796SAndrew Morton static int pause_on_oops;
39dd287796SAndrew Morton static int pause_on_oops_flag;
40dd287796SAndrew Morton static DEFINE_SPINLOCK(pause_on_oops_lock);
415375b708SHATAYAMA Daisuke bool crash_kexec_post_notifiers;
429e3961a0SPrarit Bhargava int panic_on_warn __read_mostly;
431da177e4SLinus Torvalds 
445800dc3cSJason Baron int panic_timeout = CONFIG_PANIC_TIMEOUT;
4581e88fdcSHuang Ying EXPORT_SYMBOL_GPL(panic_timeout);
461da177e4SLinus Torvalds 
47e041c683SAlan Stern ATOMIC_NOTIFIER_HEAD(panic_notifier_list);
481da177e4SLinus Torvalds 
491da177e4SLinus Torvalds EXPORT_SYMBOL(panic_notifier_list);
501da177e4SLinus Torvalds 
51c7ff0d9cSTAMUKI Shoichi static long no_blink(int state)
528aeee85aSAnton Blanchard {
53c7ff0d9cSTAMUKI Shoichi 	return 0;
54c7ff0d9cSTAMUKI Shoichi }
558aeee85aSAnton Blanchard 
56c7ff0d9cSTAMUKI Shoichi /* Returns how long it waited in ms */
57c7ff0d9cSTAMUKI Shoichi long (*panic_blink)(int state);
58c7ff0d9cSTAMUKI Shoichi EXPORT_SYMBOL(panic_blink);
598aeee85aSAnton Blanchard 
6093e13a36SMichael Holzheu /*
6193e13a36SMichael Holzheu  * Stop ourself in panic -- architecture code may override this
6293e13a36SMichael Holzheu  */
6393e13a36SMichael Holzheu void __weak panic_smp_self_stop(void)
6493e13a36SMichael Holzheu {
6593e13a36SMichael Holzheu 	while (1)
6693e13a36SMichael Holzheu 		cpu_relax();
6793e13a36SMichael Holzheu }
6893e13a36SMichael Holzheu 
6958c5661fSHidehiro Kawai /*
7058c5661fSHidehiro Kawai  * Stop ourselves in NMI context if another CPU has already panicked. Arch code
7158c5661fSHidehiro Kawai  * may override this to prepare for crash dumping, e.g. save regs info.
7258c5661fSHidehiro Kawai  */
7358c5661fSHidehiro Kawai void __weak nmi_panic_self_stop(struct pt_regs *regs)
7458c5661fSHidehiro Kawai {
7558c5661fSHidehiro Kawai 	panic_smp_self_stop();
7658c5661fSHidehiro Kawai }
7758c5661fSHidehiro Kawai 
780ee59413SHidehiro Kawai /*
790ee59413SHidehiro Kawai  * Stop other CPUs in panic.  Architecture dependent code may override this
800ee59413SHidehiro Kawai  * with more suitable version.  For example, if the architecture supports
810ee59413SHidehiro Kawai  * crash dump, it should save registers of each stopped CPU and disable
820ee59413SHidehiro Kawai  * per-CPU features such as virtualization extensions.
830ee59413SHidehiro Kawai  */
840ee59413SHidehiro Kawai void __weak crash_smp_send_stop(void)
850ee59413SHidehiro Kawai {
860ee59413SHidehiro Kawai 	static int cpus_stopped;
870ee59413SHidehiro Kawai 
880ee59413SHidehiro Kawai 	/*
890ee59413SHidehiro Kawai 	 * This function can be called twice in panic path, but obviously
900ee59413SHidehiro Kawai 	 * we execute this only once.
910ee59413SHidehiro Kawai 	 */
920ee59413SHidehiro Kawai 	if (cpus_stopped)
930ee59413SHidehiro Kawai 		return;
940ee59413SHidehiro Kawai 
950ee59413SHidehiro Kawai 	/*
960ee59413SHidehiro Kawai 	 * Note smp_send_stop is the usual smp shutdown function, which
970ee59413SHidehiro Kawai 	 * unfortunately means it may not be hardened to work in a panic
980ee59413SHidehiro Kawai 	 * situation.
990ee59413SHidehiro Kawai 	 */
1000ee59413SHidehiro Kawai 	smp_send_stop();
1010ee59413SHidehiro Kawai 	cpus_stopped = 1;
1020ee59413SHidehiro Kawai }
1030ee59413SHidehiro Kawai 
1041717f209SHidehiro Kawai atomic_t panic_cpu = ATOMIC_INIT(PANIC_CPU_INVALID);
1051717f209SHidehiro Kawai 
106ebc41f20SHidehiro Kawai /*
107ebc41f20SHidehiro Kawai  * A variant of panic() called from NMI context. We return if we've already
108ebc41f20SHidehiro Kawai  * panicked on this CPU. If another CPU already panicked, loop in
109ebc41f20SHidehiro Kawai  * nmi_panic_self_stop() which can provide architecture dependent code such
110ebc41f20SHidehiro Kawai  * as saving register state for crash dump.
111ebc41f20SHidehiro Kawai  */
112ebc41f20SHidehiro Kawai void nmi_panic(struct pt_regs *regs, const char *msg)
113ebc41f20SHidehiro Kawai {
114ebc41f20SHidehiro Kawai 	int old_cpu, cpu;
115ebc41f20SHidehiro Kawai 
116ebc41f20SHidehiro Kawai 	cpu = raw_smp_processor_id();
117ebc41f20SHidehiro Kawai 	old_cpu = atomic_cmpxchg(&panic_cpu, PANIC_CPU_INVALID, cpu);
118ebc41f20SHidehiro Kawai 
119ebc41f20SHidehiro Kawai 	if (old_cpu == PANIC_CPU_INVALID)
120ebc41f20SHidehiro Kawai 		panic("%s", msg);
121ebc41f20SHidehiro Kawai 	else if (old_cpu != cpu)
122ebc41f20SHidehiro Kawai 		nmi_panic_self_stop(regs);
123ebc41f20SHidehiro Kawai }
124ebc41f20SHidehiro Kawai EXPORT_SYMBOL(nmi_panic);
125ebc41f20SHidehiro Kawai 
1261da177e4SLinus Torvalds /**
1271da177e4SLinus Torvalds  *	panic - halt the system
1281da177e4SLinus Torvalds  *	@fmt: The text string to print
1291da177e4SLinus Torvalds  *
1301da177e4SLinus Torvalds  *	Display a message, then perform cleanups.
1311da177e4SLinus Torvalds  *
1321da177e4SLinus Torvalds  *	This function never returns.
1331da177e4SLinus Torvalds  */
1349402c95fSJoe Perches void panic(const char *fmt, ...)
1351da177e4SLinus Torvalds {
1361da177e4SLinus Torvalds 	static char buf[1024];
1371da177e4SLinus Torvalds 	va_list args;
138c7ff0d9cSTAMUKI Shoichi 	long i, i_next = 0;
139c7ff0d9cSTAMUKI Shoichi 	int state = 0;
1401717f209SHidehiro Kawai 	int old_cpu, this_cpu;
141b26e27ddSHidehiro Kawai 	bool _crash_kexec_post_notifiers = crash_kexec_post_notifiers;
1421da177e4SLinus Torvalds 
143dc009d92SEric W. Biederman 	/*
144190320c3SVikram Mulukutla 	 * Disable local interrupts. This will prevent panic_smp_self_stop
145190320c3SVikram Mulukutla 	 * from deadlocking the first cpu that invokes the panic, since
146190320c3SVikram Mulukutla 	 * there is nothing to prevent an interrupt handler (that runs
1471717f209SHidehiro Kawai 	 * after setting panic_cpu) from invoking panic() again.
148190320c3SVikram Mulukutla 	 */
149190320c3SVikram Mulukutla 	local_irq_disable();
150190320c3SVikram Mulukutla 
151190320c3SVikram Mulukutla 	/*
152c95dbf27SIngo Molnar 	 * It's possible to come here directly from a panic-assertion and
153c95dbf27SIngo Molnar 	 * not have preempt disabled. Some functions called from here want
154dc009d92SEric W. Biederman 	 * preempt to be disabled. No point enabling it later though...
15593e13a36SMichael Holzheu 	 *
15693e13a36SMichael Holzheu 	 * Only one CPU is allowed to execute the panic code from here. For
15793e13a36SMichael Holzheu 	 * multiple parallel invocations of panic, all other CPUs either
15893e13a36SMichael Holzheu 	 * stop themself or will wait until they are stopped by the 1st CPU
15993e13a36SMichael Holzheu 	 * with smp_send_stop().
1601717f209SHidehiro Kawai 	 *
1611717f209SHidehiro Kawai 	 * `old_cpu == PANIC_CPU_INVALID' means this is the 1st CPU which
1621717f209SHidehiro Kawai 	 * comes here, so go ahead.
1631717f209SHidehiro Kawai 	 * `old_cpu == this_cpu' means we came from nmi_panic() which sets
1641717f209SHidehiro Kawai 	 * panic_cpu to this CPU.  In this case, this is also the 1st CPU.
165dc009d92SEric W. Biederman 	 */
1661717f209SHidehiro Kawai 	this_cpu = raw_smp_processor_id();
1671717f209SHidehiro Kawai 	old_cpu  = atomic_cmpxchg(&panic_cpu, PANIC_CPU_INVALID, this_cpu);
1681717f209SHidehiro Kawai 
1691717f209SHidehiro Kawai 	if (old_cpu != PANIC_CPU_INVALID && old_cpu != this_cpu)
17093e13a36SMichael Holzheu 		panic_smp_self_stop();
171dc009d92SEric W. Biederman 
1725b530fc1SAnton Blanchard 	console_verbose();
1731da177e4SLinus Torvalds 	bust_spinlocks(1);
1741da177e4SLinus Torvalds 	va_start(args, fmt);
1751da177e4SLinus Torvalds 	vsnprintf(buf, sizeof(buf), fmt, args);
1761da177e4SLinus Torvalds 	va_end(args);
177d7c0847fSFabian Frederick 	pr_emerg("Kernel panic - not syncing: %s\n", buf);
1785cb27301SIngo Molnar #ifdef CONFIG_DEBUG_BUGVERBOSE
1796e6f0a1fSAndi Kleen 	/*
1806e6f0a1fSAndi Kleen 	 * Avoid nested stack-dumping if a panic occurs during oops processing
1816e6f0a1fSAndi Kleen 	 */
182026ee1f6SJason Wessel 	if (!test_taint(TAINT_DIE) && oops_in_progress <= 1)
1835cb27301SIngo Molnar 		dump_stack();
1845cb27301SIngo Molnar #endif
1851da177e4SLinus Torvalds 
186dc009d92SEric W. Biederman 	/*
187dc009d92SEric W. Biederman 	 * If we have crashed and we have a crash kernel loaded let it handle
188dc009d92SEric W. Biederman 	 * everything else.
189f06e5153SMasami Hiramatsu 	 * If we want to run this after calling panic_notifiers, pass
190f06e5153SMasami Hiramatsu 	 * the "crash_kexec_post_notifiers" option to the kernel.
1917bbee5caSHidehiro Kawai 	 *
1927bbee5caSHidehiro Kawai 	 * Bypass the panic_cpu check and call __crash_kexec directly.
193dc009d92SEric W. Biederman 	 */
194b26e27ddSHidehiro Kawai 	if (!_crash_kexec_post_notifiers) {
195f92bac3bSSergey Senozhatsky 		printk_safe_flush_on_panic();
1967bbee5caSHidehiro Kawai 		__crash_kexec(NULL);
197dc009d92SEric W. Biederman 
198dc009d92SEric W. Biederman 		/*
199dc009d92SEric W. Biederman 		 * Note smp_send_stop is the usual smp shutdown function, which
2000ee59413SHidehiro Kawai 		 * unfortunately means it may not be hardened to work in a
2010ee59413SHidehiro Kawai 		 * panic situation.
202dc009d92SEric W. Biederman 		 */
2031da177e4SLinus Torvalds 		smp_send_stop();
2040ee59413SHidehiro Kawai 	} else {
2050ee59413SHidehiro Kawai 		/*
2060ee59413SHidehiro Kawai 		 * If we want to do crash dump after notifier calls and
2070ee59413SHidehiro Kawai 		 * kmsg_dump, we will need architecture dependent extra
2080ee59413SHidehiro Kawai 		 * works in addition to stopping other CPUs.
2090ee59413SHidehiro Kawai 		 */
2100ee59413SHidehiro Kawai 		crash_smp_send_stop();
2110ee59413SHidehiro Kawai 	}
2121da177e4SLinus Torvalds 
2136723734cSKees Cook 	/*
2146723734cSKees Cook 	 * Run any panic handlers, including those that might need to
2156723734cSKees Cook 	 * add information to the kmsg dump output.
2166723734cSKees Cook 	 */
217e041c683SAlan Stern 	atomic_notifier_call_chain(&panic_notifier_list, 0, buf);
2181da177e4SLinus Torvalds 
219cf9b1106SPetr Mladek 	/* Call flush even twice. It tries harder with a single online CPU */
220f92bac3bSSergey Senozhatsky 	printk_safe_flush_on_panic();
2216723734cSKees Cook 	kmsg_dump(KMSG_DUMP_PANIC);
2226723734cSKees Cook 
223f06e5153SMasami Hiramatsu 	/*
224f06e5153SMasami Hiramatsu 	 * If you doubt kdump always works fine in any situation,
225f06e5153SMasami Hiramatsu 	 * "crash_kexec_post_notifiers" offers you a chance to run
226f06e5153SMasami Hiramatsu 	 * panic_notifiers and dumping kmsg before kdump.
227f06e5153SMasami Hiramatsu 	 * Note: since some panic_notifiers can make crashed kernel
228f06e5153SMasami Hiramatsu 	 * more unstable, it can increase risks of the kdump failure too.
2297bbee5caSHidehiro Kawai 	 *
2307bbee5caSHidehiro Kawai 	 * Bypass the panic_cpu check and call __crash_kexec directly.
231f06e5153SMasami Hiramatsu 	 */
232b26e27ddSHidehiro Kawai 	if (_crash_kexec_post_notifiers)
2337bbee5caSHidehiro Kawai 		__crash_kexec(NULL);
234f06e5153SMasami Hiramatsu 
235d014e889SAaro Koskinen 	bust_spinlocks(0);
236d014e889SAaro Koskinen 
23708d78658SVitaly Kuznetsov 	/*
23808d78658SVitaly Kuznetsov 	 * We may have ended up stopping the CPU holding the lock (in
23908d78658SVitaly Kuznetsov 	 * smp_send_stop()) while still having some valuable data in the console
24008d78658SVitaly Kuznetsov 	 * buffer.  Try to acquire the lock then release it regardless of the
2417625b3a0SVitaly Kuznetsov 	 * result.  The release will also print the buffers out.  Locks debug
2427625b3a0SVitaly Kuznetsov 	 * should be disabled to avoid reporting bad unlock balance when
2437625b3a0SVitaly Kuznetsov 	 * panic() is not being callled from OOPS.
24408d78658SVitaly Kuznetsov 	 */
2457625b3a0SVitaly Kuznetsov 	debug_locks_off();
2468d91f8b1STejun Heo 	console_flush_on_panic();
24708d78658SVitaly Kuznetsov 
248c7ff0d9cSTAMUKI Shoichi 	if (!panic_blink)
249c7ff0d9cSTAMUKI Shoichi 		panic_blink = no_blink;
250c7ff0d9cSTAMUKI Shoichi 
251dc009d92SEric W. Biederman 	if (panic_timeout > 0) {
2521da177e4SLinus Torvalds 		/*
2531da177e4SLinus Torvalds 		 * Delay timeout seconds before rebooting the machine.
254c95dbf27SIngo Molnar 		 * We can't use the "normal" timers since we just panicked.
2551da177e4SLinus Torvalds 		 */
256ff7a28a0SJiri Slaby 		pr_emerg("Rebooting in %d seconds..\n", panic_timeout);
257c95dbf27SIngo Molnar 
258c7ff0d9cSTAMUKI Shoichi 		for (i = 0; i < panic_timeout * 1000; i += PANIC_TIMER_STEP) {
2591da177e4SLinus Torvalds 			touch_nmi_watchdog();
260c7ff0d9cSTAMUKI Shoichi 			if (i >= i_next) {
261c7ff0d9cSTAMUKI Shoichi 				i += panic_blink(state ^= 1);
262c7ff0d9cSTAMUKI Shoichi 				i_next = i + 3600 / PANIC_BLINK_SPD;
263c7ff0d9cSTAMUKI Shoichi 			}
264c7ff0d9cSTAMUKI Shoichi 			mdelay(PANIC_TIMER_STEP);
2651da177e4SLinus Torvalds 		}
2664302fbc8SHugh Dickins 	}
2674302fbc8SHugh Dickins 	if (panic_timeout != 0) {
268c95dbf27SIngo Molnar 		/*
269c95dbf27SIngo Molnar 		 * This will not be a clean reboot, with everything
2702f048ea8SEric W. Biederman 		 * shutting down.  But if there is a chance of
2712f048ea8SEric W. Biederman 		 * rebooting the system it will be rebooted.
2721da177e4SLinus Torvalds 		 */
2732f048ea8SEric W. Biederman 		emergency_restart();
2741da177e4SLinus Torvalds 	}
2751da177e4SLinus Torvalds #ifdef __sparc__
2761da177e4SLinus Torvalds 	{
2771da177e4SLinus Torvalds 		extern int stop_a_enabled;
278a271c241STom 'spot' Callaway 		/* Make sure the user can actually press Stop-A (L1-A) */
2791da177e4SLinus Torvalds 		stop_a_enabled = 1;
2807db60d05SVijay Kumar 		pr_emerg("Press Stop-A (L1-A) from sun keyboard or send break\n"
2817db60d05SVijay Kumar 			 "twice on console to return to the boot prom\n");
2821da177e4SLinus Torvalds 	}
2831da177e4SLinus Torvalds #endif
284347a8dc3SMartin Schwidefsky #if defined(CONFIG_S390)
285c95dbf27SIngo Molnar 	{
286c95dbf27SIngo Molnar 		unsigned long caller;
287c95dbf27SIngo Molnar 
288c95dbf27SIngo Molnar 		caller = (unsigned long)__builtin_return_address(0);
2891da177e4SLinus Torvalds 		disabled_wait(caller);
290c95dbf27SIngo Molnar 	}
2911da177e4SLinus Torvalds #endif
292d7c0847fSFabian Frederick 	pr_emerg("---[ end Kernel panic - not syncing: %s\n", buf);
2931da177e4SLinus Torvalds 	local_irq_enable();
294c7ff0d9cSTAMUKI Shoichi 	for (i = 0; ; i += PANIC_TIMER_STEP) {
295c22db941SJan Beulich 		touch_softlockup_watchdog();
296c7ff0d9cSTAMUKI Shoichi 		if (i >= i_next) {
297c7ff0d9cSTAMUKI Shoichi 			i += panic_blink(state ^= 1);
298c7ff0d9cSTAMUKI Shoichi 			i_next = i + 3600 / PANIC_BLINK_SPD;
299c7ff0d9cSTAMUKI Shoichi 		}
300c7ff0d9cSTAMUKI Shoichi 		mdelay(PANIC_TIMER_STEP);
3011da177e4SLinus Torvalds 	}
3021da177e4SLinus Torvalds }
3031da177e4SLinus Torvalds 
3041da177e4SLinus Torvalds EXPORT_SYMBOL(panic);
3051da177e4SLinus Torvalds 
3067fd8329bSPetr Mladek /*
3077fd8329bSPetr Mladek  * TAINT_FORCED_RMMOD could be a per-module flag but the module
3087fd8329bSPetr Mladek  * is being removed anyway.
3097fd8329bSPetr Mladek  */
3107fd8329bSPetr Mladek const struct taint_flag taint_flags[TAINT_FLAGS_COUNT] = {
3117fd8329bSPetr Mladek 	{ 'P', 'G', true },	/* TAINT_PROPRIETARY_MODULE */
3127fd8329bSPetr Mladek 	{ 'F', ' ', true },	/* TAINT_FORCED_MODULE */
3137fd8329bSPetr Mladek 	{ 'S', ' ', false },	/* TAINT_CPU_OUT_OF_SPEC */
3147fd8329bSPetr Mladek 	{ 'R', ' ', false },	/* TAINT_FORCED_RMMOD */
3157fd8329bSPetr Mladek 	{ 'M', ' ', false },	/* TAINT_MACHINE_CHECK */
3167fd8329bSPetr Mladek 	{ 'B', ' ', false },	/* TAINT_BAD_PAGE */
3177fd8329bSPetr Mladek 	{ 'U', ' ', false },	/* TAINT_USER */
3187fd8329bSPetr Mladek 	{ 'D', ' ', false },	/* TAINT_DIE */
3197fd8329bSPetr Mladek 	{ 'A', ' ', false },	/* TAINT_OVERRIDDEN_ACPI_TABLE */
3207fd8329bSPetr Mladek 	{ 'W', ' ', false },	/* TAINT_WARN */
3217fd8329bSPetr Mladek 	{ 'C', ' ', true },	/* TAINT_CRAP */
3227fd8329bSPetr Mladek 	{ 'I', ' ', false },	/* TAINT_FIRMWARE_WORKAROUND */
3237fd8329bSPetr Mladek 	{ 'O', ' ', true },	/* TAINT_OOT_MODULE */
3247fd8329bSPetr Mladek 	{ 'E', ' ', true },	/* TAINT_UNSIGNED_MODULE */
3257fd8329bSPetr Mladek 	{ 'L', ' ', false },	/* TAINT_SOFTLOCKUP */
3267fd8329bSPetr Mladek 	{ 'K', ' ', true },	/* TAINT_LIVEPATCH */
32725ddbb18SAndi Kleen };
32825ddbb18SAndi Kleen 
3291da177e4SLinus Torvalds /**
3301da177e4SLinus Torvalds  *	print_tainted - return a string to represent the kernel taint state.
3311da177e4SLinus Torvalds  *
3321da177e4SLinus Torvalds  *  'P' - Proprietary module has been loaded.
3331da177e4SLinus Torvalds  *  'F' - Module has been forcibly loaded.
3341da177e4SLinus Torvalds  *  'S' - SMP with CPUs not designed for SMP.
3351da177e4SLinus Torvalds  *  'R' - User forced a module unload.
3369aa5e993SDaniel Roesen  *  'M' - System experienced a machine check exception.
3371da177e4SLinus Torvalds  *  'B' - System has hit bad_page.
33834f5a398STheodore Ts'o  *  'U' - Userspace-defined naughtiness.
339a8005992SArjan van de Ven  *  'D' - Kernel has oopsed before
34095b570c9SNur Hussein  *  'A' - ACPI table overridden.
34195b570c9SNur Hussein  *  'W' - Taint on warning.
342061b1bd3SGreg Kroah-Hartman  *  'C' - modules from drivers/staging are loaded.
34392946bc7SBen Hutchings  *  'I' - Working around severe firmware bug.
3442449b8baSBen Hutchings  *  'O' - Out-of-tree module has been loaded.
34557673c2bSRusty Russell  *  'E' - Unsigned module has been loaded.
346bc53a3f4SXie XiuQi  *  'L' - A soft lockup has previously occurred.
347c5f45465SSeth Jennings  *  'K' - Kernel has been live patched.
3481da177e4SLinus Torvalds  *
349fe002a41SRobert P. J. Day  *	The string is overwritten by the next call to print_tainted().
3501da177e4SLinus Torvalds  */
3511da177e4SLinus Torvalds const char *print_tainted(void)
3521da177e4SLinus Torvalds {
3537fd8329bSPetr Mladek 	static char buf[TAINT_FLAGS_COUNT + sizeof("Tainted: ")];
35425ddbb18SAndi Kleen 
35525ddbb18SAndi Kleen 	if (tainted_mask) {
35625ddbb18SAndi Kleen 		char *s;
35725ddbb18SAndi Kleen 		int i;
35825ddbb18SAndi Kleen 
35925ddbb18SAndi Kleen 		s = buf + sprintf(buf, "Tainted: ");
3607fd8329bSPetr Mladek 		for (i = 0; i < TAINT_FLAGS_COUNT; i++) {
3617fd8329bSPetr Mladek 			const struct taint_flag *t = &taint_flags[i];
3627fd8329bSPetr Mladek 			*s++ = test_bit(i, &tainted_mask) ?
3635eb7c0d0SLarry Finger 					t->c_true : t->c_false;
3641da177e4SLinus Torvalds 		}
36525ddbb18SAndi Kleen 		*s = 0;
36625ddbb18SAndi Kleen 	} else
3671da177e4SLinus Torvalds 		snprintf(buf, sizeof(buf), "Not tainted");
368c95dbf27SIngo Molnar 
369c95dbf27SIngo Molnar 	return buf;
3701da177e4SLinus Torvalds }
3711da177e4SLinus Torvalds 
37225ddbb18SAndi Kleen int test_taint(unsigned flag)
37325ddbb18SAndi Kleen {
37425ddbb18SAndi Kleen 	return test_bit(flag, &tainted_mask);
37525ddbb18SAndi Kleen }
37625ddbb18SAndi Kleen EXPORT_SYMBOL(test_taint);
37725ddbb18SAndi Kleen 
37825ddbb18SAndi Kleen unsigned long get_taint(void)
37925ddbb18SAndi Kleen {
38025ddbb18SAndi Kleen 	return tainted_mask;
38125ddbb18SAndi Kleen }
38225ddbb18SAndi Kleen 
383373d4d09SRusty Russell /**
384373d4d09SRusty Russell  * add_taint: add a taint flag if not already set.
385373d4d09SRusty Russell  * @flag: one of the TAINT_* constants.
386373d4d09SRusty Russell  * @lockdep_ok: whether lock debugging is still OK.
387373d4d09SRusty Russell  *
388373d4d09SRusty Russell  * If something bad has gone wrong, you'll want @lockdebug_ok = false, but for
389373d4d09SRusty Russell  * some notewortht-but-not-corrupting cases, it can be set to true.
3909eeba613SFrederic Weisbecker  */
391373d4d09SRusty Russell void add_taint(unsigned flag, enum lockdep_ok lockdep_ok)
392373d4d09SRusty Russell {
393373d4d09SRusty Russell 	if (lockdep_ok == LOCKDEP_NOW_UNRELIABLE && __debug_locks_off())
394d7c0847fSFabian Frederick 		pr_warn("Disabling lock debugging due to kernel taint\n");
3959eeba613SFrederic Weisbecker 
39625ddbb18SAndi Kleen 	set_bit(flag, &tainted_mask);
3971da177e4SLinus Torvalds }
3981da177e4SLinus Torvalds EXPORT_SYMBOL(add_taint);
399dd287796SAndrew Morton 
400dd287796SAndrew Morton static void spin_msec(int msecs)
401dd287796SAndrew Morton {
402dd287796SAndrew Morton 	int i;
403dd287796SAndrew Morton 
404dd287796SAndrew Morton 	for (i = 0; i < msecs; i++) {
405dd287796SAndrew Morton 		touch_nmi_watchdog();
406dd287796SAndrew Morton 		mdelay(1);
407dd287796SAndrew Morton 	}
408dd287796SAndrew Morton }
409dd287796SAndrew Morton 
410dd287796SAndrew Morton /*
411dd287796SAndrew Morton  * It just happens that oops_enter() and oops_exit() are identically
412dd287796SAndrew Morton  * implemented...
413dd287796SAndrew Morton  */
414dd287796SAndrew Morton static void do_oops_enter_exit(void)
415dd287796SAndrew Morton {
416dd287796SAndrew Morton 	unsigned long flags;
417dd287796SAndrew Morton 	static int spin_counter;
418dd287796SAndrew Morton 
419dd287796SAndrew Morton 	if (!pause_on_oops)
420dd287796SAndrew Morton 		return;
421dd287796SAndrew Morton 
422dd287796SAndrew Morton 	spin_lock_irqsave(&pause_on_oops_lock, flags);
423dd287796SAndrew Morton 	if (pause_on_oops_flag == 0) {
424dd287796SAndrew Morton 		/* This CPU may now print the oops message */
425dd287796SAndrew Morton 		pause_on_oops_flag = 1;
426dd287796SAndrew Morton 	} else {
427dd287796SAndrew Morton 		/* We need to stall this CPU */
428dd287796SAndrew Morton 		if (!spin_counter) {
429dd287796SAndrew Morton 			/* This CPU gets to do the counting */
430dd287796SAndrew Morton 			spin_counter = pause_on_oops;
431dd287796SAndrew Morton 			do {
432dd287796SAndrew Morton 				spin_unlock(&pause_on_oops_lock);
433dd287796SAndrew Morton 				spin_msec(MSEC_PER_SEC);
434dd287796SAndrew Morton 				spin_lock(&pause_on_oops_lock);
435dd287796SAndrew Morton 			} while (--spin_counter);
436dd287796SAndrew Morton 			pause_on_oops_flag = 0;
437dd287796SAndrew Morton 		} else {
438dd287796SAndrew Morton 			/* This CPU waits for a different one */
439dd287796SAndrew Morton 			while (spin_counter) {
440dd287796SAndrew Morton 				spin_unlock(&pause_on_oops_lock);
441dd287796SAndrew Morton 				spin_msec(1);
442dd287796SAndrew Morton 				spin_lock(&pause_on_oops_lock);
443dd287796SAndrew Morton 			}
444dd287796SAndrew Morton 		}
445dd287796SAndrew Morton 	}
446dd287796SAndrew Morton 	spin_unlock_irqrestore(&pause_on_oops_lock, flags);
447dd287796SAndrew Morton }
448dd287796SAndrew Morton 
449dd287796SAndrew Morton /*
450c95dbf27SIngo Molnar  * Return true if the calling CPU is allowed to print oops-related info.
451c95dbf27SIngo Molnar  * This is a bit racy..
452dd287796SAndrew Morton  */
453dd287796SAndrew Morton int oops_may_print(void)
454dd287796SAndrew Morton {
455dd287796SAndrew Morton 	return pause_on_oops_flag == 0;
456dd287796SAndrew Morton }
457dd287796SAndrew Morton 
458dd287796SAndrew Morton /*
459dd287796SAndrew Morton  * Called when the architecture enters its oops handler, before it prints
460c95dbf27SIngo Molnar  * anything.  If this is the first CPU to oops, and it's oopsing the first
461c95dbf27SIngo Molnar  * time then let it proceed.
462dd287796SAndrew Morton  *
463c95dbf27SIngo Molnar  * This is all enabled by the pause_on_oops kernel boot option.  We do all
464c95dbf27SIngo Molnar  * this to ensure that oopses don't scroll off the screen.  It has the
465c95dbf27SIngo Molnar  * side-effect of preventing later-oopsing CPUs from mucking up the display,
466c95dbf27SIngo Molnar  * too.
467dd287796SAndrew Morton  *
468c95dbf27SIngo Molnar  * It turns out that the CPU which is allowed to print ends up pausing for
469c95dbf27SIngo Molnar  * the right duration, whereas all the other CPUs pause for twice as long:
470c95dbf27SIngo Molnar  * once in oops_enter(), once in oops_exit().
471dd287796SAndrew Morton  */
472dd287796SAndrew Morton void oops_enter(void)
473dd287796SAndrew Morton {
474bdff7870SThomas Gleixner 	tracing_off();
475c95dbf27SIngo Molnar 	/* can't trust the integrity of the kernel anymore: */
476c95dbf27SIngo Molnar 	debug_locks_off();
477dd287796SAndrew Morton 	do_oops_enter_exit();
478dd287796SAndrew Morton }
479dd287796SAndrew Morton 
480dd287796SAndrew Morton /*
4812c3b20e9SArjan van de Ven  * 64-bit random ID for oopses:
4822c3b20e9SArjan van de Ven  */
4832c3b20e9SArjan van de Ven static u64 oops_id;
4842c3b20e9SArjan van de Ven 
4852c3b20e9SArjan van de Ven static int init_oops_id(void)
4862c3b20e9SArjan van de Ven {
4872c3b20e9SArjan van de Ven 	if (!oops_id)
4882c3b20e9SArjan van de Ven 		get_random_bytes(&oops_id, sizeof(oops_id));
489d6624f99SArjan van de Ven 	else
490d6624f99SArjan van de Ven 		oops_id++;
4912c3b20e9SArjan van de Ven 
4922c3b20e9SArjan van de Ven 	return 0;
4932c3b20e9SArjan van de Ven }
4942c3b20e9SArjan van de Ven late_initcall(init_oops_id);
4952c3b20e9SArjan van de Ven 
496863a6049SAnton Blanchard void print_oops_end_marker(void)
49771c33911SArjan van de Ven {
49871c33911SArjan van de Ven 	init_oops_id();
499d7c0847fSFabian Frederick 	pr_warn("---[ end trace %016llx ]---\n", (unsigned long long)oops_id);
50071c33911SArjan van de Ven }
50171c33911SArjan van de Ven 
5022c3b20e9SArjan van de Ven /*
503dd287796SAndrew Morton  * Called when the architecture exits its oops handler, after printing
504dd287796SAndrew Morton  * everything.
505dd287796SAndrew Morton  */
506dd287796SAndrew Morton void oops_exit(void)
507dd287796SAndrew Morton {
508dd287796SAndrew Morton 	do_oops_enter_exit();
50971c33911SArjan van de Ven 	print_oops_end_marker();
510456b565cSSimon Kagstrom 	kmsg_dump(KMSG_DUMP_OOPS);
511dd287796SAndrew Morton }
5123162f751SArjan van de Ven 
5132553b67aSJosh Poimboeuf struct warn_args {
5140f6f49a8SLinus Torvalds 	const char *fmt;
515a8f18b90SArjan van de Ven 	va_list args;
5160f6f49a8SLinus Torvalds };
5170f6f49a8SLinus Torvalds 
5182553b67aSJosh Poimboeuf void __warn(const char *file, int line, void *caller, unsigned taint,
5192553b67aSJosh Poimboeuf 	    struct pt_regs *regs, struct warn_args *args)
5200f6f49a8SLinus Torvalds {
521de7edd31SSteven Rostedt (Red Hat) 	disable_trace_on_warning();
522de7edd31SSteven Rostedt (Red Hat) 
523dcb6b452SAlex Thorlton 	pr_warn("------------[ cut here ]------------\n");
5242553b67aSJosh Poimboeuf 
5252553b67aSJosh Poimboeuf 	if (file)
5262553b67aSJosh Poimboeuf 		pr_warn("WARNING: CPU: %d PID: %d at %s:%d %pS\n",
5272553b67aSJosh Poimboeuf 			raw_smp_processor_id(), current->pid, file, line,
5282553b67aSJosh Poimboeuf 			caller);
5292553b67aSJosh Poimboeuf 	else
5302553b67aSJosh Poimboeuf 		pr_warn("WARNING: CPU: %d PID: %d at %pS\n",
5312553b67aSJosh Poimboeuf 			raw_smp_processor_id(), current->pid, caller);
53274853dbaSArjan van de Ven 
5330f6f49a8SLinus Torvalds 	if (args)
5340f6f49a8SLinus Torvalds 		vprintk(args->fmt, args->args);
535a8f18b90SArjan van de Ven 
5369e3961a0SPrarit Bhargava 	if (panic_on_warn) {
5379e3961a0SPrarit Bhargava 		/*
5389e3961a0SPrarit Bhargava 		 * This thread may hit another WARN() in the panic path.
5399e3961a0SPrarit Bhargava 		 * Resetting this prevents additional WARN() from panicking the
5409e3961a0SPrarit Bhargava 		 * system on this thread.  Other threads are blocked by the
5419e3961a0SPrarit Bhargava 		 * panic_mutex in panic().
5429e3961a0SPrarit Bhargava 		 */
5439e3961a0SPrarit Bhargava 		panic_on_warn = 0;
5449e3961a0SPrarit Bhargava 		panic("panic_on_warn set ...\n");
5459e3961a0SPrarit Bhargava 	}
5469e3961a0SPrarit Bhargava 
547a8f18b90SArjan van de Ven 	print_modules();
5482553b67aSJosh Poimboeuf 
5492553b67aSJosh Poimboeuf 	if (regs)
5502553b67aSJosh Poimboeuf 		show_regs(regs);
5512553b67aSJosh Poimboeuf 	else
552a8f18b90SArjan van de Ven 		dump_stack();
5532553b67aSJosh Poimboeuf 
554a8f18b90SArjan van de Ven 	print_oops_end_marker();
5552553b67aSJosh Poimboeuf 
556373d4d09SRusty Russell 	/* Just a warning, don't kill lockdep. */
557373d4d09SRusty Russell 	add_taint(taint, LOCKDEP_STILL_OK);
558a8f18b90SArjan van de Ven }
5590f6f49a8SLinus Torvalds 
5602553b67aSJosh Poimboeuf #ifdef WANT_WARN_ON_SLOWPATH
5610f6f49a8SLinus Torvalds void warn_slowpath_fmt(const char *file, int line, const char *fmt, ...)
5620f6f49a8SLinus Torvalds {
5632553b67aSJosh Poimboeuf 	struct warn_args args;
5640f6f49a8SLinus Torvalds 
5650f6f49a8SLinus Torvalds 	args.fmt = fmt;
5660f6f49a8SLinus Torvalds 	va_start(args.args, fmt);
5672553b67aSJosh Poimboeuf 	__warn(file, line, __builtin_return_address(0), TAINT_WARN, NULL,
5682553b67aSJosh Poimboeuf 	       &args);
5690f6f49a8SLinus Torvalds 	va_end(args.args);
5700f6f49a8SLinus Torvalds }
57157adc4d2SAndi Kleen EXPORT_SYMBOL(warn_slowpath_fmt);
57257adc4d2SAndi Kleen 
573b2be0527SBen Hutchings void warn_slowpath_fmt_taint(const char *file, int line,
574b2be0527SBen Hutchings 			     unsigned taint, const char *fmt, ...)
575b2be0527SBen Hutchings {
5762553b67aSJosh Poimboeuf 	struct warn_args args;
577b2be0527SBen Hutchings 
578b2be0527SBen Hutchings 	args.fmt = fmt;
579b2be0527SBen Hutchings 	va_start(args.args, fmt);
5802553b67aSJosh Poimboeuf 	__warn(file, line, __builtin_return_address(0), taint, NULL, &args);
581b2be0527SBen Hutchings 	va_end(args.args);
582b2be0527SBen Hutchings }
583b2be0527SBen Hutchings EXPORT_SYMBOL(warn_slowpath_fmt_taint);
584b2be0527SBen Hutchings 
58557adc4d2SAndi Kleen void warn_slowpath_null(const char *file, int line)
58657adc4d2SAndi Kleen {
5872553b67aSJosh Poimboeuf 	__warn(file, line, __builtin_return_address(0), TAINT_WARN, NULL, NULL);
58857adc4d2SAndi Kleen }
58957adc4d2SAndi Kleen EXPORT_SYMBOL(warn_slowpath_null);
59079b4cc5eSArjan van de Ven #endif
59179b4cc5eSArjan van de Ven 
592b1fca27dSAndi Kleen #ifdef CONFIG_BUG
593b1fca27dSAndi Kleen 
594b1fca27dSAndi Kleen /* Support resetting WARN*_ONCE state */
595b1fca27dSAndi Kleen 
596b1fca27dSAndi Kleen static int clear_warn_once_set(void *data, u64 val)
597b1fca27dSAndi Kleen {
598*aaf5dcfbSAndi Kleen 	generic_bug_clear_once();
599b1fca27dSAndi Kleen 	memset(__start_once, 0, __end_once - __start_once);
600b1fca27dSAndi Kleen 	return 0;
601b1fca27dSAndi Kleen }
602b1fca27dSAndi Kleen 
603b1fca27dSAndi Kleen DEFINE_SIMPLE_ATTRIBUTE(clear_warn_once_fops,
604b1fca27dSAndi Kleen 			NULL,
605b1fca27dSAndi Kleen 			clear_warn_once_set,
606b1fca27dSAndi Kleen 			"%lld\n");
607b1fca27dSAndi Kleen 
608b1fca27dSAndi Kleen static __init int register_warn_debugfs(void)
609b1fca27dSAndi Kleen {
610b1fca27dSAndi Kleen 	/* Don't care about failure */
611*aaf5dcfbSAndi Kleen 	debugfs_create_file("clear_warn_once", 0200, NULL,
612b1fca27dSAndi Kleen 			    NULL, &clear_warn_once_fops);
613b1fca27dSAndi Kleen 	return 0;
614b1fca27dSAndi Kleen }
615b1fca27dSAndi Kleen 
616b1fca27dSAndi Kleen device_initcall(register_warn_debugfs);
617b1fca27dSAndi Kleen #endif
618b1fca27dSAndi Kleen 
6193162f751SArjan van de Ven #ifdef CONFIG_CC_STACKPROTECTOR
62054371a43SArjan van de Ven 
6213162f751SArjan van de Ven /*
6223162f751SArjan van de Ven  * Called when gcc's -fstack-protector feature is used, and
6233162f751SArjan van de Ven  * gcc detects corruption of the on-stack canary value
6243162f751SArjan van de Ven  */
625a7330c99SAndi Kleen __visible void __stack_chk_fail(void)
6263162f751SArjan van de Ven {
627517a92c4SIngo Molnar 	panic("stack-protector: Kernel stack is corrupted in: %p\n",
628517a92c4SIngo Molnar 		__builtin_return_address(0));
6293162f751SArjan van de Ven }
6303162f751SArjan van de Ven EXPORT_SYMBOL(__stack_chk_fail);
63154371a43SArjan van de Ven 
6323162f751SArjan van de Ven #endif
633f44dd164SRusty Russell 
6347a46ec0eSKees Cook #ifdef CONFIG_ARCH_HAS_REFCOUNT
6357a46ec0eSKees Cook void refcount_error_report(struct pt_regs *regs, const char *err)
6367a46ec0eSKees Cook {
6377a46ec0eSKees Cook 	WARN_RATELIMIT(1, "refcount_t %s at %pB in %s[%d], uid/euid: %u/%u\n",
6387a46ec0eSKees Cook 		err, (void *)instruction_pointer(regs),
6397a46ec0eSKees Cook 		current->comm, task_pid_nr(current),
6407a46ec0eSKees Cook 		from_kuid_munged(&init_user_ns, current_uid()),
6417a46ec0eSKees Cook 		from_kuid_munged(&init_user_ns, current_euid()));
6427a46ec0eSKees Cook }
6437a46ec0eSKees Cook #endif
6447a46ec0eSKees Cook 
645f44dd164SRusty Russell core_param(panic, panic_timeout, int, 0644);
646f44dd164SRusty Russell core_param(pause_on_oops, pause_on_oops, int, 0644);
6479e3961a0SPrarit Bhargava core_param(panic_on_warn, panic_on_warn, int, 0644);
648b26e27ddSHidehiro Kawai core_param(crash_kexec_post_notifiers, crash_kexec_post_notifiers, bool, 0644);
649f06e5153SMasami Hiramatsu 
650d404ab0aSOlaf Hering static int __init oops_setup(char *s)
651d404ab0aSOlaf Hering {
652d404ab0aSOlaf Hering 	if (!s)
653d404ab0aSOlaf Hering 		return -EINVAL;
654d404ab0aSOlaf Hering 	if (!strcmp(s, "panic"))
655d404ab0aSOlaf Hering 		panic_on_oops = 1;
656d404ab0aSOlaf Hering 	return 0;
657d404ab0aSOlaf Hering }
658d404ab0aSOlaf Hering early_param("oops", oops_setup);
659