xref: /linux-6.15/kernel/panic.c (revision bc4f2f54)
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;
37*bc4f2f54SKees Cook static unsigned long tainted_mask =
38*bc4f2f54SKees Cook 	IS_ENABLED(CONFIG_GCC_PLUGIN_RANDSTRUCT) ? (1 << TAINT_RANDSTRUCT) : 0;
39dd287796SAndrew Morton static int pause_on_oops;
40dd287796SAndrew Morton static int pause_on_oops_flag;
41dd287796SAndrew Morton static DEFINE_SPINLOCK(pause_on_oops_lock);
425375b708SHATAYAMA Daisuke bool crash_kexec_post_notifiers;
439e3961a0SPrarit Bhargava int panic_on_warn __read_mostly;
441da177e4SLinus Torvalds 
455800dc3cSJason Baron int panic_timeout = CONFIG_PANIC_TIMEOUT;
4681e88fdcSHuang Ying EXPORT_SYMBOL_GPL(panic_timeout);
471da177e4SLinus Torvalds 
48e041c683SAlan Stern ATOMIC_NOTIFIER_HEAD(panic_notifier_list);
491da177e4SLinus Torvalds 
501da177e4SLinus Torvalds EXPORT_SYMBOL(panic_notifier_list);
511da177e4SLinus Torvalds 
52c7ff0d9cSTAMUKI Shoichi static long no_blink(int state)
538aeee85aSAnton Blanchard {
54c7ff0d9cSTAMUKI Shoichi 	return 0;
55c7ff0d9cSTAMUKI Shoichi }
568aeee85aSAnton Blanchard 
57c7ff0d9cSTAMUKI Shoichi /* Returns how long it waited in ms */
58c7ff0d9cSTAMUKI Shoichi long (*panic_blink)(int state);
59c7ff0d9cSTAMUKI Shoichi EXPORT_SYMBOL(panic_blink);
608aeee85aSAnton Blanchard 
6193e13a36SMichael Holzheu /*
6293e13a36SMichael Holzheu  * Stop ourself in panic -- architecture code may override this
6393e13a36SMichael Holzheu  */
6493e13a36SMichael Holzheu void __weak panic_smp_self_stop(void)
6593e13a36SMichael Holzheu {
6693e13a36SMichael Holzheu 	while (1)
6793e13a36SMichael Holzheu 		cpu_relax();
6893e13a36SMichael Holzheu }
6993e13a36SMichael Holzheu 
7058c5661fSHidehiro Kawai /*
7158c5661fSHidehiro Kawai  * Stop ourselves in NMI context if another CPU has already panicked. Arch code
7258c5661fSHidehiro Kawai  * may override this to prepare for crash dumping, e.g. save regs info.
7358c5661fSHidehiro Kawai  */
7458c5661fSHidehiro Kawai void __weak nmi_panic_self_stop(struct pt_regs *regs)
7558c5661fSHidehiro Kawai {
7658c5661fSHidehiro Kawai 	panic_smp_self_stop();
7758c5661fSHidehiro Kawai }
7858c5661fSHidehiro Kawai 
790ee59413SHidehiro Kawai /*
800ee59413SHidehiro Kawai  * Stop other CPUs in panic.  Architecture dependent code may override this
810ee59413SHidehiro Kawai  * with more suitable version.  For example, if the architecture supports
820ee59413SHidehiro Kawai  * crash dump, it should save registers of each stopped CPU and disable
830ee59413SHidehiro Kawai  * per-CPU features such as virtualization extensions.
840ee59413SHidehiro Kawai  */
850ee59413SHidehiro Kawai void __weak crash_smp_send_stop(void)
860ee59413SHidehiro Kawai {
870ee59413SHidehiro Kawai 	static int cpus_stopped;
880ee59413SHidehiro Kawai 
890ee59413SHidehiro Kawai 	/*
900ee59413SHidehiro Kawai 	 * This function can be called twice in panic path, but obviously
910ee59413SHidehiro Kawai 	 * we execute this only once.
920ee59413SHidehiro Kawai 	 */
930ee59413SHidehiro Kawai 	if (cpus_stopped)
940ee59413SHidehiro Kawai 		return;
950ee59413SHidehiro Kawai 
960ee59413SHidehiro Kawai 	/*
970ee59413SHidehiro Kawai 	 * Note smp_send_stop is the usual smp shutdown function, which
980ee59413SHidehiro Kawai 	 * unfortunately means it may not be hardened to work in a panic
990ee59413SHidehiro Kawai 	 * situation.
1000ee59413SHidehiro Kawai 	 */
1010ee59413SHidehiro Kawai 	smp_send_stop();
1020ee59413SHidehiro Kawai 	cpus_stopped = 1;
1030ee59413SHidehiro Kawai }
1040ee59413SHidehiro Kawai 
1051717f209SHidehiro Kawai atomic_t panic_cpu = ATOMIC_INIT(PANIC_CPU_INVALID);
1061717f209SHidehiro Kawai 
107ebc41f20SHidehiro Kawai /*
108ebc41f20SHidehiro Kawai  * A variant of panic() called from NMI context. We return if we've already
109ebc41f20SHidehiro Kawai  * panicked on this CPU. If another CPU already panicked, loop in
110ebc41f20SHidehiro Kawai  * nmi_panic_self_stop() which can provide architecture dependent code such
111ebc41f20SHidehiro Kawai  * as saving register state for crash dump.
112ebc41f20SHidehiro Kawai  */
113ebc41f20SHidehiro Kawai void nmi_panic(struct pt_regs *regs, const char *msg)
114ebc41f20SHidehiro Kawai {
115ebc41f20SHidehiro Kawai 	int old_cpu, cpu;
116ebc41f20SHidehiro Kawai 
117ebc41f20SHidehiro Kawai 	cpu = raw_smp_processor_id();
118ebc41f20SHidehiro Kawai 	old_cpu = atomic_cmpxchg(&panic_cpu, PANIC_CPU_INVALID, cpu);
119ebc41f20SHidehiro Kawai 
120ebc41f20SHidehiro Kawai 	if (old_cpu == PANIC_CPU_INVALID)
121ebc41f20SHidehiro Kawai 		panic("%s", msg);
122ebc41f20SHidehiro Kawai 	else if (old_cpu != cpu)
123ebc41f20SHidehiro Kawai 		nmi_panic_self_stop(regs);
124ebc41f20SHidehiro Kawai }
125ebc41f20SHidehiro Kawai EXPORT_SYMBOL(nmi_panic);
126ebc41f20SHidehiro Kawai 
1271da177e4SLinus Torvalds /**
1281da177e4SLinus Torvalds  *	panic - halt the system
1291da177e4SLinus Torvalds  *	@fmt: The text string to print
1301da177e4SLinus Torvalds  *
1311da177e4SLinus Torvalds  *	Display a message, then perform cleanups.
1321da177e4SLinus Torvalds  *
1331da177e4SLinus Torvalds  *	This function never returns.
1341da177e4SLinus Torvalds  */
1359402c95fSJoe Perches void panic(const char *fmt, ...)
1361da177e4SLinus Torvalds {
1371da177e4SLinus Torvalds 	static char buf[1024];
1381da177e4SLinus Torvalds 	va_list args;
139c7ff0d9cSTAMUKI Shoichi 	long i, i_next = 0;
140c7ff0d9cSTAMUKI Shoichi 	int state = 0;
1411717f209SHidehiro Kawai 	int old_cpu, this_cpu;
142b26e27ddSHidehiro Kawai 	bool _crash_kexec_post_notifiers = crash_kexec_post_notifiers;
1431da177e4SLinus Torvalds 
144dc009d92SEric W. Biederman 	/*
145190320c3SVikram Mulukutla 	 * Disable local interrupts. This will prevent panic_smp_self_stop
146190320c3SVikram Mulukutla 	 * from deadlocking the first cpu that invokes the panic, since
147190320c3SVikram Mulukutla 	 * there is nothing to prevent an interrupt handler (that runs
1481717f209SHidehiro Kawai 	 * after setting panic_cpu) from invoking panic() again.
149190320c3SVikram Mulukutla 	 */
150190320c3SVikram Mulukutla 	local_irq_disable();
151190320c3SVikram Mulukutla 
152190320c3SVikram Mulukutla 	/*
153c95dbf27SIngo Molnar 	 * It's possible to come here directly from a panic-assertion and
154c95dbf27SIngo Molnar 	 * not have preempt disabled. Some functions called from here want
155dc009d92SEric W. Biederman 	 * preempt to be disabled. No point enabling it later though...
15693e13a36SMichael Holzheu 	 *
15793e13a36SMichael Holzheu 	 * Only one CPU is allowed to execute the panic code from here. For
15893e13a36SMichael Holzheu 	 * multiple parallel invocations of panic, all other CPUs either
15993e13a36SMichael Holzheu 	 * stop themself or will wait until they are stopped by the 1st CPU
16093e13a36SMichael Holzheu 	 * with smp_send_stop().
1611717f209SHidehiro Kawai 	 *
1621717f209SHidehiro Kawai 	 * `old_cpu == PANIC_CPU_INVALID' means this is the 1st CPU which
1631717f209SHidehiro Kawai 	 * comes here, so go ahead.
1641717f209SHidehiro Kawai 	 * `old_cpu == this_cpu' means we came from nmi_panic() which sets
1651717f209SHidehiro Kawai 	 * panic_cpu to this CPU.  In this case, this is also the 1st CPU.
166dc009d92SEric W. Biederman 	 */
1671717f209SHidehiro Kawai 	this_cpu = raw_smp_processor_id();
1681717f209SHidehiro Kawai 	old_cpu  = atomic_cmpxchg(&panic_cpu, PANIC_CPU_INVALID, this_cpu);
1691717f209SHidehiro Kawai 
1701717f209SHidehiro Kawai 	if (old_cpu != PANIC_CPU_INVALID && old_cpu != this_cpu)
17193e13a36SMichael Holzheu 		panic_smp_self_stop();
172dc009d92SEric W. Biederman 
1735b530fc1SAnton Blanchard 	console_verbose();
1741da177e4SLinus Torvalds 	bust_spinlocks(1);
1751da177e4SLinus Torvalds 	va_start(args, fmt);
1761da177e4SLinus Torvalds 	vsnprintf(buf, sizeof(buf), fmt, args);
1771da177e4SLinus Torvalds 	va_end(args);
178d7c0847fSFabian Frederick 	pr_emerg("Kernel panic - not syncing: %s\n", buf);
1795cb27301SIngo Molnar #ifdef CONFIG_DEBUG_BUGVERBOSE
1806e6f0a1fSAndi Kleen 	/*
1816e6f0a1fSAndi Kleen 	 * Avoid nested stack-dumping if a panic occurs during oops processing
1826e6f0a1fSAndi Kleen 	 */
183026ee1f6SJason Wessel 	if (!test_taint(TAINT_DIE) && oops_in_progress <= 1)
1845cb27301SIngo Molnar 		dump_stack();
1855cb27301SIngo Molnar #endif
1861da177e4SLinus Torvalds 
187dc009d92SEric W. Biederman 	/*
188dc009d92SEric W. Biederman 	 * If we have crashed and we have a crash kernel loaded let it handle
189dc009d92SEric W. Biederman 	 * everything else.
190f06e5153SMasami Hiramatsu 	 * If we want to run this after calling panic_notifiers, pass
191f06e5153SMasami Hiramatsu 	 * the "crash_kexec_post_notifiers" option to the kernel.
1927bbee5caSHidehiro Kawai 	 *
1937bbee5caSHidehiro Kawai 	 * Bypass the panic_cpu check and call __crash_kexec directly.
194dc009d92SEric W. Biederman 	 */
195b26e27ddSHidehiro Kawai 	if (!_crash_kexec_post_notifiers) {
196f92bac3bSSergey Senozhatsky 		printk_safe_flush_on_panic();
1977bbee5caSHidehiro Kawai 		__crash_kexec(NULL);
198dc009d92SEric W. Biederman 
199dc009d92SEric W. Biederman 		/*
200dc009d92SEric W. Biederman 		 * Note smp_send_stop is the usual smp shutdown function, which
2010ee59413SHidehiro Kawai 		 * unfortunately means it may not be hardened to work in a
2020ee59413SHidehiro Kawai 		 * panic situation.
203dc009d92SEric W. Biederman 		 */
2041da177e4SLinus Torvalds 		smp_send_stop();
2050ee59413SHidehiro Kawai 	} else {
2060ee59413SHidehiro Kawai 		/*
2070ee59413SHidehiro Kawai 		 * If we want to do crash dump after notifier calls and
2080ee59413SHidehiro Kawai 		 * kmsg_dump, we will need architecture dependent extra
2090ee59413SHidehiro Kawai 		 * works in addition to stopping other CPUs.
2100ee59413SHidehiro Kawai 		 */
2110ee59413SHidehiro Kawai 		crash_smp_send_stop();
2120ee59413SHidehiro Kawai 	}
2131da177e4SLinus Torvalds 
2146723734cSKees Cook 	/*
2156723734cSKees Cook 	 * Run any panic handlers, including those that might need to
2166723734cSKees Cook 	 * add information to the kmsg dump output.
2176723734cSKees Cook 	 */
218e041c683SAlan Stern 	atomic_notifier_call_chain(&panic_notifier_list, 0, buf);
2191da177e4SLinus Torvalds 
220cf9b1106SPetr Mladek 	/* Call flush even twice. It tries harder with a single online CPU */
221f92bac3bSSergey Senozhatsky 	printk_safe_flush_on_panic();
2226723734cSKees Cook 	kmsg_dump(KMSG_DUMP_PANIC);
2236723734cSKees Cook 
224f06e5153SMasami Hiramatsu 	/*
225f06e5153SMasami Hiramatsu 	 * If you doubt kdump always works fine in any situation,
226f06e5153SMasami Hiramatsu 	 * "crash_kexec_post_notifiers" offers you a chance to run
227f06e5153SMasami Hiramatsu 	 * panic_notifiers and dumping kmsg before kdump.
228f06e5153SMasami Hiramatsu 	 * Note: since some panic_notifiers can make crashed kernel
229f06e5153SMasami Hiramatsu 	 * more unstable, it can increase risks of the kdump failure too.
2307bbee5caSHidehiro Kawai 	 *
2317bbee5caSHidehiro Kawai 	 * Bypass the panic_cpu check and call __crash_kexec directly.
232f06e5153SMasami Hiramatsu 	 */
233b26e27ddSHidehiro Kawai 	if (_crash_kexec_post_notifiers)
2347bbee5caSHidehiro Kawai 		__crash_kexec(NULL);
235f06e5153SMasami Hiramatsu 
236d014e889SAaro Koskinen 	bust_spinlocks(0);
237d014e889SAaro Koskinen 
23808d78658SVitaly Kuznetsov 	/*
23908d78658SVitaly Kuznetsov 	 * We may have ended up stopping the CPU holding the lock (in
24008d78658SVitaly Kuznetsov 	 * smp_send_stop()) while still having some valuable data in the console
24108d78658SVitaly Kuznetsov 	 * buffer.  Try to acquire the lock then release it regardless of the
2427625b3a0SVitaly Kuznetsov 	 * result.  The release will also print the buffers out.  Locks debug
2437625b3a0SVitaly Kuznetsov 	 * should be disabled to avoid reporting bad unlock balance when
2447625b3a0SVitaly Kuznetsov 	 * panic() is not being callled from OOPS.
24508d78658SVitaly Kuznetsov 	 */
2467625b3a0SVitaly Kuznetsov 	debug_locks_off();
2478d91f8b1STejun Heo 	console_flush_on_panic();
24808d78658SVitaly Kuznetsov 
249c7ff0d9cSTAMUKI Shoichi 	if (!panic_blink)
250c7ff0d9cSTAMUKI Shoichi 		panic_blink = no_blink;
251c7ff0d9cSTAMUKI Shoichi 
252dc009d92SEric W. Biederman 	if (panic_timeout > 0) {
2531da177e4SLinus Torvalds 		/*
2541da177e4SLinus Torvalds 		 * Delay timeout seconds before rebooting the machine.
255c95dbf27SIngo Molnar 		 * We can't use the "normal" timers since we just panicked.
2561da177e4SLinus Torvalds 		 */
257ff7a28a0SJiri Slaby 		pr_emerg("Rebooting in %d seconds..\n", panic_timeout);
258c95dbf27SIngo Molnar 
259c7ff0d9cSTAMUKI Shoichi 		for (i = 0; i < panic_timeout * 1000; i += PANIC_TIMER_STEP) {
2601da177e4SLinus Torvalds 			touch_nmi_watchdog();
261c7ff0d9cSTAMUKI Shoichi 			if (i >= i_next) {
262c7ff0d9cSTAMUKI Shoichi 				i += panic_blink(state ^= 1);
263c7ff0d9cSTAMUKI Shoichi 				i_next = i + 3600 / PANIC_BLINK_SPD;
264c7ff0d9cSTAMUKI Shoichi 			}
265c7ff0d9cSTAMUKI Shoichi 			mdelay(PANIC_TIMER_STEP);
2661da177e4SLinus Torvalds 		}
2674302fbc8SHugh Dickins 	}
2684302fbc8SHugh Dickins 	if (panic_timeout != 0) {
269c95dbf27SIngo Molnar 		/*
270c95dbf27SIngo Molnar 		 * This will not be a clean reboot, with everything
2712f048ea8SEric W. Biederman 		 * shutting down.  But if there is a chance of
2722f048ea8SEric W. Biederman 		 * rebooting the system it will be rebooted.
2731da177e4SLinus Torvalds 		 */
2742f048ea8SEric W. Biederman 		emergency_restart();
2751da177e4SLinus Torvalds 	}
2761da177e4SLinus Torvalds #ifdef __sparc__
2771da177e4SLinus Torvalds 	{
2781da177e4SLinus Torvalds 		extern int stop_a_enabled;
279a271c241STom 'spot' Callaway 		/* Make sure the user can actually press Stop-A (L1-A) */
2801da177e4SLinus Torvalds 		stop_a_enabled = 1;
2817db60d05SVijay Kumar 		pr_emerg("Press Stop-A (L1-A) from sun keyboard or send break\n"
2827db60d05SVijay Kumar 			 "twice on console to return to the boot prom\n");
2831da177e4SLinus Torvalds 	}
2841da177e4SLinus Torvalds #endif
285347a8dc3SMartin Schwidefsky #if defined(CONFIG_S390)
286c95dbf27SIngo Molnar 	{
287c95dbf27SIngo Molnar 		unsigned long caller;
288c95dbf27SIngo Molnar 
289c95dbf27SIngo Molnar 		caller = (unsigned long)__builtin_return_address(0);
2901da177e4SLinus Torvalds 		disabled_wait(caller);
291c95dbf27SIngo Molnar 	}
2921da177e4SLinus Torvalds #endif
2935ad75105SBorislav Petkov 	pr_emerg("---[ end Kernel panic - not syncing: %s ]---\n", buf);
2941da177e4SLinus Torvalds 	local_irq_enable();
295c7ff0d9cSTAMUKI Shoichi 	for (i = 0; ; i += PANIC_TIMER_STEP) {
296c22db941SJan Beulich 		touch_softlockup_watchdog();
297c7ff0d9cSTAMUKI Shoichi 		if (i >= i_next) {
298c7ff0d9cSTAMUKI Shoichi 			i += panic_blink(state ^= 1);
299c7ff0d9cSTAMUKI Shoichi 			i_next = i + 3600 / PANIC_BLINK_SPD;
300c7ff0d9cSTAMUKI Shoichi 		}
301c7ff0d9cSTAMUKI Shoichi 		mdelay(PANIC_TIMER_STEP);
3021da177e4SLinus Torvalds 	}
3031da177e4SLinus Torvalds }
3041da177e4SLinus Torvalds 
3051da177e4SLinus Torvalds EXPORT_SYMBOL(panic);
3061da177e4SLinus Torvalds 
3077fd8329bSPetr Mladek /*
3087fd8329bSPetr Mladek  * TAINT_FORCED_RMMOD could be a per-module flag but the module
3097fd8329bSPetr Mladek  * is being removed anyway.
3107fd8329bSPetr Mladek  */
3117fd8329bSPetr Mladek const struct taint_flag taint_flags[TAINT_FLAGS_COUNT] = {
31247d4b263SKees Cook 	[ TAINT_PROPRIETARY_MODULE ]	= { 'P', 'G', true },
31347d4b263SKees Cook 	[ TAINT_FORCED_MODULE ]		= { 'F', ' ', true },
31447d4b263SKees Cook 	[ TAINT_CPU_OUT_OF_SPEC ]	= { 'S', ' ', false },
31547d4b263SKees Cook 	[ TAINT_FORCED_RMMOD ]		= { 'R', ' ', false },
31647d4b263SKees Cook 	[ TAINT_MACHINE_CHECK ]		= { 'M', ' ', false },
31747d4b263SKees Cook 	[ TAINT_BAD_PAGE ]		= { 'B', ' ', false },
31847d4b263SKees Cook 	[ TAINT_USER ]			= { 'U', ' ', false },
31947d4b263SKees Cook 	[ TAINT_DIE ]			= { 'D', ' ', false },
32047d4b263SKees Cook 	[ TAINT_OVERRIDDEN_ACPI_TABLE ]	= { 'A', ' ', false },
32147d4b263SKees Cook 	[ TAINT_WARN ]			= { 'W', ' ', false },
32247d4b263SKees Cook 	[ TAINT_CRAP ]			= { 'C', ' ', true },
32347d4b263SKees Cook 	[ TAINT_FIRMWARE_WORKAROUND ]	= { 'I', ' ', false },
32447d4b263SKees Cook 	[ TAINT_OOT_MODULE ]		= { 'O', ' ', true },
32547d4b263SKees Cook 	[ TAINT_UNSIGNED_MODULE ]	= { 'E', ' ', true },
32647d4b263SKees Cook 	[ TAINT_SOFTLOCKUP ]		= { 'L', ' ', false },
32747d4b263SKees Cook 	[ TAINT_LIVEPATCH ]		= { 'K', ' ', true },
32847d4b263SKees Cook 	[ TAINT_AUX ]			= { 'X', ' ', true },
329*bc4f2f54SKees Cook 	[ TAINT_RANDSTRUCT ]		= { 'T', ' ', true },
33025ddbb18SAndi Kleen };
33125ddbb18SAndi Kleen 
3321da177e4SLinus Torvalds /**
3331da177e4SLinus Torvalds  * print_tainted - return a string to represent the kernel taint state.
3341da177e4SLinus Torvalds  *
3359c4560e5SKees Cook  * For individual taint flag meanings, see Documentation/sysctl/kernel.txt
3361da177e4SLinus Torvalds  *
3379c4560e5SKees Cook  * The string is overwritten by the next call to print_tainted(),
3389c4560e5SKees Cook  * but is always NULL terminated.
3391da177e4SLinus Torvalds  */
3401da177e4SLinus Torvalds const char *print_tainted(void)
3411da177e4SLinus Torvalds {
3427fd8329bSPetr Mladek 	static char buf[TAINT_FLAGS_COUNT + sizeof("Tainted: ")];
34325ddbb18SAndi Kleen 
34447d4b263SKees Cook 	BUILD_BUG_ON(ARRAY_SIZE(taint_flags) != TAINT_FLAGS_COUNT);
34547d4b263SKees Cook 
34625ddbb18SAndi Kleen 	if (tainted_mask) {
34725ddbb18SAndi Kleen 		char *s;
34825ddbb18SAndi Kleen 		int i;
34925ddbb18SAndi Kleen 
35025ddbb18SAndi Kleen 		s = buf + sprintf(buf, "Tainted: ");
3517fd8329bSPetr Mladek 		for (i = 0; i < TAINT_FLAGS_COUNT; i++) {
3527fd8329bSPetr Mladek 			const struct taint_flag *t = &taint_flags[i];
3537fd8329bSPetr Mladek 			*s++ = test_bit(i, &tainted_mask) ?
3545eb7c0d0SLarry Finger 					t->c_true : t->c_false;
3551da177e4SLinus Torvalds 		}
35625ddbb18SAndi Kleen 		*s = 0;
35725ddbb18SAndi Kleen 	} else
3581da177e4SLinus Torvalds 		snprintf(buf, sizeof(buf), "Not tainted");
359c95dbf27SIngo Molnar 
360c95dbf27SIngo Molnar 	return buf;
3611da177e4SLinus Torvalds }
3621da177e4SLinus Torvalds 
36325ddbb18SAndi Kleen int test_taint(unsigned flag)
36425ddbb18SAndi Kleen {
36525ddbb18SAndi Kleen 	return test_bit(flag, &tainted_mask);
36625ddbb18SAndi Kleen }
36725ddbb18SAndi Kleen EXPORT_SYMBOL(test_taint);
36825ddbb18SAndi Kleen 
36925ddbb18SAndi Kleen unsigned long get_taint(void)
37025ddbb18SAndi Kleen {
37125ddbb18SAndi Kleen 	return tainted_mask;
37225ddbb18SAndi Kleen }
37325ddbb18SAndi Kleen 
374373d4d09SRusty Russell /**
375373d4d09SRusty Russell  * add_taint: add a taint flag if not already set.
376373d4d09SRusty Russell  * @flag: one of the TAINT_* constants.
377373d4d09SRusty Russell  * @lockdep_ok: whether lock debugging is still OK.
378373d4d09SRusty Russell  *
379373d4d09SRusty Russell  * If something bad has gone wrong, you'll want @lockdebug_ok = false, but for
380373d4d09SRusty Russell  * some notewortht-but-not-corrupting cases, it can be set to true.
3819eeba613SFrederic Weisbecker  */
382373d4d09SRusty Russell void add_taint(unsigned flag, enum lockdep_ok lockdep_ok)
383373d4d09SRusty Russell {
384373d4d09SRusty Russell 	if (lockdep_ok == LOCKDEP_NOW_UNRELIABLE && __debug_locks_off())
385d7c0847fSFabian Frederick 		pr_warn("Disabling lock debugging due to kernel taint\n");
3869eeba613SFrederic Weisbecker 
38725ddbb18SAndi Kleen 	set_bit(flag, &tainted_mask);
3881da177e4SLinus Torvalds }
3891da177e4SLinus Torvalds EXPORT_SYMBOL(add_taint);
390dd287796SAndrew Morton 
391dd287796SAndrew Morton static void spin_msec(int msecs)
392dd287796SAndrew Morton {
393dd287796SAndrew Morton 	int i;
394dd287796SAndrew Morton 
395dd287796SAndrew Morton 	for (i = 0; i < msecs; i++) {
396dd287796SAndrew Morton 		touch_nmi_watchdog();
397dd287796SAndrew Morton 		mdelay(1);
398dd287796SAndrew Morton 	}
399dd287796SAndrew Morton }
400dd287796SAndrew Morton 
401dd287796SAndrew Morton /*
402dd287796SAndrew Morton  * It just happens that oops_enter() and oops_exit() are identically
403dd287796SAndrew Morton  * implemented...
404dd287796SAndrew Morton  */
405dd287796SAndrew Morton static void do_oops_enter_exit(void)
406dd287796SAndrew Morton {
407dd287796SAndrew Morton 	unsigned long flags;
408dd287796SAndrew Morton 	static int spin_counter;
409dd287796SAndrew Morton 
410dd287796SAndrew Morton 	if (!pause_on_oops)
411dd287796SAndrew Morton 		return;
412dd287796SAndrew Morton 
413dd287796SAndrew Morton 	spin_lock_irqsave(&pause_on_oops_lock, flags);
414dd287796SAndrew Morton 	if (pause_on_oops_flag == 0) {
415dd287796SAndrew Morton 		/* This CPU may now print the oops message */
416dd287796SAndrew Morton 		pause_on_oops_flag = 1;
417dd287796SAndrew Morton 	} else {
418dd287796SAndrew Morton 		/* We need to stall this CPU */
419dd287796SAndrew Morton 		if (!spin_counter) {
420dd287796SAndrew Morton 			/* This CPU gets to do the counting */
421dd287796SAndrew Morton 			spin_counter = pause_on_oops;
422dd287796SAndrew Morton 			do {
423dd287796SAndrew Morton 				spin_unlock(&pause_on_oops_lock);
424dd287796SAndrew Morton 				spin_msec(MSEC_PER_SEC);
425dd287796SAndrew Morton 				spin_lock(&pause_on_oops_lock);
426dd287796SAndrew Morton 			} while (--spin_counter);
427dd287796SAndrew Morton 			pause_on_oops_flag = 0;
428dd287796SAndrew Morton 		} else {
429dd287796SAndrew Morton 			/* This CPU waits for a different one */
430dd287796SAndrew Morton 			while (spin_counter) {
431dd287796SAndrew Morton 				spin_unlock(&pause_on_oops_lock);
432dd287796SAndrew Morton 				spin_msec(1);
433dd287796SAndrew Morton 				spin_lock(&pause_on_oops_lock);
434dd287796SAndrew Morton 			}
435dd287796SAndrew Morton 		}
436dd287796SAndrew Morton 	}
437dd287796SAndrew Morton 	spin_unlock_irqrestore(&pause_on_oops_lock, flags);
438dd287796SAndrew Morton }
439dd287796SAndrew Morton 
440dd287796SAndrew Morton /*
441c95dbf27SIngo Molnar  * Return true if the calling CPU is allowed to print oops-related info.
442c95dbf27SIngo Molnar  * This is a bit racy..
443dd287796SAndrew Morton  */
444dd287796SAndrew Morton int oops_may_print(void)
445dd287796SAndrew Morton {
446dd287796SAndrew Morton 	return pause_on_oops_flag == 0;
447dd287796SAndrew Morton }
448dd287796SAndrew Morton 
449dd287796SAndrew Morton /*
450dd287796SAndrew Morton  * Called when the architecture enters its oops handler, before it prints
451c95dbf27SIngo Molnar  * anything.  If this is the first CPU to oops, and it's oopsing the first
452c95dbf27SIngo Molnar  * time then let it proceed.
453dd287796SAndrew Morton  *
454c95dbf27SIngo Molnar  * This is all enabled by the pause_on_oops kernel boot option.  We do all
455c95dbf27SIngo Molnar  * this to ensure that oopses don't scroll off the screen.  It has the
456c95dbf27SIngo Molnar  * side-effect of preventing later-oopsing CPUs from mucking up the display,
457c95dbf27SIngo Molnar  * too.
458dd287796SAndrew Morton  *
459c95dbf27SIngo Molnar  * It turns out that the CPU which is allowed to print ends up pausing for
460c95dbf27SIngo Molnar  * the right duration, whereas all the other CPUs pause for twice as long:
461c95dbf27SIngo Molnar  * once in oops_enter(), once in oops_exit().
462dd287796SAndrew Morton  */
463dd287796SAndrew Morton void oops_enter(void)
464dd287796SAndrew Morton {
465bdff7870SThomas Gleixner 	tracing_off();
466c95dbf27SIngo Molnar 	/* can't trust the integrity of the kernel anymore: */
467c95dbf27SIngo Molnar 	debug_locks_off();
468dd287796SAndrew Morton 	do_oops_enter_exit();
469dd287796SAndrew Morton }
470dd287796SAndrew Morton 
471dd287796SAndrew Morton /*
4722c3b20e9SArjan van de Ven  * 64-bit random ID for oopses:
4732c3b20e9SArjan van de Ven  */
4742c3b20e9SArjan van de Ven static u64 oops_id;
4752c3b20e9SArjan van de Ven 
4762c3b20e9SArjan van de Ven static int init_oops_id(void)
4772c3b20e9SArjan van de Ven {
4782c3b20e9SArjan van de Ven 	if (!oops_id)
4792c3b20e9SArjan van de Ven 		get_random_bytes(&oops_id, sizeof(oops_id));
480d6624f99SArjan van de Ven 	else
481d6624f99SArjan van de Ven 		oops_id++;
4822c3b20e9SArjan van de Ven 
4832c3b20e9SArjan van de Ven 	return 0;
4842c3b20e9SArjan van de Ven }
4852c3b20e9SArjan van de Ven late_initcall(init_oops_id);
4862c3b20e9SArjan van de Ven 
487863a6049SAnton Blanchard void print_oops_end_marker(void)
48871c33911SArjan van de Ven {
48971c33911SArjan van de Ven 	init_oops_id();
490d7c0847fSFabian Frederick 	pr_warn("---[ end trace %016llx ]---\n", (unsigned long long)oops_id);
49171c33911SArjan van de Ven }
49271c33911SArjan van de Ven 
4932c3b20e9SArjan van de Ven /*
494dd287796SAndrew Morton  * Called when the architecture exits its oops handler, after printing
495dd287796SAndrew Morton  * everything.
496dd287796SAndrew Morton  */
497dd287796SAndrew Morton void oops_exit(void)
498dd287796SAndrew Morton {
499dd287796SAndrew Morton 	do_oops_enter_exit();
50071c33911SArjan van de Ven 	print_oops_end_marker();
501456b565cSSimon Kagstrom 	kmsg_dump(KMSG_DUMP_OOPS);
502dd287796SAndrew Morton }
5033162f751SArjan van de Ven 
5042553b67aSJosh Poimboeuf struct warn_args {
5050f6f49a8SLinus Torvalds 	const char *fmt;
506a8f18b90SArjan van de Ven 	va_list args;
5070f6f49a8SLinus Torvalds };
5080f6f49a8SLinus Torvalds 
5092553b67aSJosh Poimboeuf void __warn(const char *file, int line, void *caller, unsigned taint,
5102553b67aSJosh Poimboeuf 	    struct pt_regs *regs, struct warn_args *args)
5110f6f49a8SLinus Torvalds {
512de7edd31SSteven Rostedt (Red Hat) 	disable_trace_on_warning();
513de7edd31SSteven Rostedt (Red Hat) 
514a7bed27aSKees Cook 	if (args)
5152a8358d8SKees Cook 		pr_warn(CUT_HERE);
5162553b67aSJosh Poimboeuf 
5172553b67aSJosh Poimboeuf 	if (file)
5182553b67aSJosh Poimboeuf 		pr_warn("WARNING: CPU: %d PID: %d at %s:%d %pS\n",
5192553b67aSJosh Poimboeuf 			raw_smp_processor_id(), current->pid, file, line,
5202553b67aSJosh Poimboeuf 			caller);
5212553b67aSJosh Poimboeuf 	else
5222553b67aSJosh Poimboeuf 		pr_warn("WARNING: CPU: %d PID: %d at %pS\n",
5232553b67aSJosh Poimboeuf 			raw_smp_processor_id(), current->pid, caller);
52474853dbaSArjan van de Ven 
5250f6f49a8SLinus Torvalds 	if (args)
5260f6f49a8SLinus Torvalds 		vprintk(args->fmt, args->args);
527a8f18b90SArjan van de Ven 
5289e3961a0SPrarit Bhargava 	if (panic_on_warn) {
5299e3961a0SPrarit Bhargava 		/*
5309e3961a0SPrarit Bhargava 		 * This thread may hit another WARN() in the panic path.
5319e3961a0SPrarit Bhargava 		 * Resetting this prevents additional WARN() from panicking the
5329e3961a0SPrarit Bhargava 		 * system on this thread.  Other threads are blocked by the
5339e3961a0SPrarit Bhargava 		 * panic_mutex in panic().
5349e3961a0SPrarit Bhargava 		 */
5359e3961a0SPrarit Bhargava 		panic_on_warn = 0;
5369e3961a0SPrarit Bhargava 		panic("panic_on_warn set ...\n");
5379e3961a0SPrarit Bhargava 	}
5389e3961a0SPrarit Bhargava 
539a8f18b90SArjan van de Ven 	print_modules();
5402553b67aSJosh Poimboeuf 
5412553b67aSJosh Poimboeuf 	if (regs)
5422553b67aSJosh Poimboeuf 		show_regs(regs);
5432553b67aSJosh Poimboeuf 	else
544a8f18b90SArjan van de Ven 		dump_stack();
5452553b67aSJosh Poimboeuf 
5464c281074SSteven Rostedt (VMware) 	print_irqtrace_events(current);
5474c281074SSteven Rostedt (VMware) 
548a8f18b90SArjan van de Ven 	print_oops_end_marker();
5492553b67aSJosh Poimboeuf 
550373d4d09SRusty Russell 	/* Just a warning, don't kill lockdep. */
551373d4d09SRusty Russell 	add_taint(taint, LOCKDEP_STILL_OK);
552a8f18b90SArjan van de Ven }
5530f6f49a8SLinus Torvalds 
5542553b67aSJosh Poimboeuf #ifdef WANT_WARN_ON_SLOWPATH
5550f6f49a8SLinus Torvalds void warn_slowpath_fmt(const char *file, int line, const char *fmt, ...)
5560f6f49a8SLinus Torvalds {
5572553b67aSJosh Poimboeuf 	struct warn_args args;
5580f6f49a8SLinus Torvalds 
5590f6f49a8SLinus Torvalds 	args.fmt = fmt;
5600f6f49a8SLinus Torvalds 	va_start(args.args, fmt);
5612553b67aSJosh Poimboeuf 	__warn(file, line, __builtin_return_address(0), TAINT_WARN, NULL,
5622553b67aSJosh Poimboeuf 	       &args);
5630f6f49a8SLinus Torvalds 	va_end(args.args);
5640f6f49a8SLinus Torvalds }
56557adc4d2SAndi Kleen EXPORT_SYMBOL(warn_slowpath_fmt);
56657adc4d2SAndi Kleen 
567b2be0527SBen Hutchings void warn_slowpath_fmt_taint(const char *file, int line,
568b2be0527SBen Hutchings 			     unsigned taint, const char *fmt, ...)
569b2be0527SBen Hutchings {
5702553b67aSJosh Poimboeuf 	struct warn_args args;
571b2be0527SBen Hutchings 
572b2be0527SBen Hutchings 	args.fmt = fmt;
573b2be0527SBen Hutchings 	va_start(args.args, fmt);
5742553b67aSJosh Poimboeuf 	__warn(file, line, __builtin_return_address(0), taint, NULL, &args);
575b2be0527SBen Hutchings 	va_end(args.args);
576b2be0527SBen Hutchings }
577b2be0527SBen Hutchings EXPORT_SYMBOL(warn_slowpath_fmt_taint);
578b2be0527SBen Hutchings 
57957adc4d2SAndi Kleen void warn_slowpath_null(const char *file, int line)
58057adc4d2SAndi Kleen {
581a7bed27aSKees Cook 	pr_warn(CUT_HERE);
5822553b67aSJosh Poimboeuf 	__warn(file, line, __builtin_return_address(0), TAINT_WARN, NULL, NULL);
58357adc4d2SAndi Kleen }
58457adc4d2SAndi Kleen EXPORT_SYMBOL(warn_slowpath_null);
585a7bed27aSKees Cook #else
586a7bed27aSKees Cook void __warn_printk(const char *fmt, ...)
587a7bed27aSKees Cook {
588a7bed27aSKees Cook 	va_list args;
589a7bed27aSKees Cook 
590a7bed27aSKees Cook 	pr_warn(CUT_HERE);
591a7bed27aSKees Cook 
592a7bed27aSKees Cook 	va_start(args, fmt);
593a7bed27aSKees Cook 	vprintk(fmt, args);
594a7bed27aSKees Cook 	va_end(args);
595a7bed27aSKees Cook }
596a7bed27aSKees Cook EXPORT_SYMBOL(__warn_printk);
59779b4cc5eSArjan van de Ven #endif
59879b4cc5eSArjan van de Ven 
599b1fca27dSAndi Kleen #ifdef CONFIG_BUG
600b1fca27dSAndi Kleen 
601b1fca27dSAndi Kleen /* Support resetting WARN*_ONCE state */
602b1fca27dSAndi Kleen 
603b1fca27dSAndi Kleen static int clear_warn_once_set(void *data, u64 val)
604b1fca27dSAndi Kleen {
605aaf5dcfbSAndi Kleen 	generic_bug_clear_once();
606b1fca27dSAndi Kleen 	memset(__start_once, 0, __end_once - __start_once);
607b1fca27dSAndi Kleen 	return 0;
608b1fca27dSAndi Kleen }
609b1fca27dSAndi Kleen 
610b1fca27dSAndi Kleen DEFINE_SIMPLE_ATTRIBUTE(clear_warn_once_fops,
611b1fca27dSAndi Kleen 			NULL,
612b1fca27dSAndi Kleen 			clear_warn_once_set,
613b1fca27dSAndi Kleen 			"%lld\n");
614b1fca27dSAndi Kleen 
615b1fca27dSAndi Kleen static __init int register_warn_debugfs(void)
616b1fca27dSAndi Kleen {
617b1fca27dSAndi Kleen 	/* Don't care about failure */
618aaf5dcfbSAndi Kleen 	debugfs_create_file("clear_warn_once", 0200, NULL,
619b1fca27dSAndi Kleen 			    NULL, &clear_warn_once_fops);
620b1fca27dSAndi Kleen 	return 0;
621b1fca27dSAndi Kleen }
622b1fca27dSAndi Kleen 
623b1fca27dSAndi Kleen device_initcall(register_warn_debugfs);
624b1fca27dSAndi Kleen #endif
625b1fca27dSAndi Kleen 
6263162f751SArjan van de Ven #ifdef CONFIG_CC_STACKPROTECTOR
62754371a43SArjan van de Ven 
6283162f751SArjan van de Ven /*
6293162f751SArjan van de Ven  * Called when gcc's -fstack-protector feature is used, and
6303162f751SArjan van de Ven  * gcc detects corruption of the on-stack canary value
6313162f751SArjan van de Ven  */
632a7330c99SAndi Kleen __visible void __stack_chk_fail(void)
6333162f751SArjan van de Ven {
6340862ca42SKees Cook 	panic("stack-protector: Kernel stack is corrupted in: %pB\n",
635517a92c4SIngo Molnar 		__builtin_return_address(0));
6363162f751SArjan van de Ven }
6373162f751SArjan van de Ven EXPORT_SYMBOL(__stack_chk_fail);
63854371a43SArjan van de Ven 
6393162f751SArjan van de Ven #endif
640f44dd164SRusty Russell 
6417a46ec0eSKees Cook #ifdef CONFIG_ARCH_HAS_REFCOUNT
6427a46ec0eSKees Cook void refcount_error_report(struct pt_regs *regs, const char *err)
6437a46ec0eSKees Cook {
6447a46ec0eSKees Cook 	WARN_RATELIMIT(1, "refcount_t %s at %pB in %s[%d], uid/euid: %u/%u\n",
6457a46ec0eSKees Cook 		err, (void *)instruction_pointer(regs),
6467a46ec0eSKees Cook 		current->comm, task_pid_nr(current),
6477a46ec0eSKees Cook 		from_kuid_munged(&init_user_ns, current_uid()),
6487a46ec0eSKees Cook 		from_kuid_munged(&init_user_ns, current_euid()));
6497a46ec0eSKees Cook }
6507a46ec0eSKees Cook #endif
6517a46ec0eSKees Cook 
652f44dd164SRusty Russell core_param(panic, panic_timeout, int, 0644);
653f44dd164SRusty Russell core_param(pause_on_oops, pause_on_oops, int, 0644);
6549e3961a0SPrarit Bhargava core_param(panic_on_warn, panic_on_warn, int, 0644);
655b26e27ddSHidehiro Kawai core_param(crash_kexec_post_notifiers, crash_kexec_post_notifiers, bool, 0644);
656f06e5153SMasami Hiramatsu 
657d404ab0aSOlaf Hering static int __init oops_setup(char *s)
658d404ab0aSOlaf Hering {
659d404ab0aSOlaf Hering 	if (!s)
660d404ab0aSOlaf Hering 		return -EINVAL;
661d404ab0aSOlaf Hering 	if (!strcmp(s, "panic"))
662d404ab0aSOlaf Hering 		panic_on_oops = 1;
663d404ab0aSOlaf Hering 	return 0;
664d404ab0aSOlaf Hering }
665d404ab0aSOlaf Hering early_param("oops", oops_setup);
666