xref: /linux-6.15/kernel/panic.c (revision 72c774aa)
1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *  linux/kernel/panic.c
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *  Copyright (C) 1991, 1992  Linus Torvalds
61da177e4SLinus Torvalds  */
71da177e4SLinus Torvalds 
81da177e4SLinus Torvalds /*
91da177e4SLinus Torvalds  * This function is used through-out the kernel (including mm and fs)
101da177e4SLinus Torvalds  * to indicate a major problem.
111da177e4SLinus Torvalds  */
12657b3010SAndrew Morton #include <linux/debug_locks.h>
13b17b0153SIngo Molnar #include <linux/sched/debug.h>
14c95dbf27SIngo Molnar #include <linux/interrupt.h>
157d92bda2SDouglas Anderson #include <linux/kgdb.h>
16456b565cSSimon Kagstrom #include <linux/kmsg_dump.h>
1779b4cc5eSArjan van de Ven #include <linux/kallsyms.h>
18c95dbf27SIngo Molnar #include <linux/notifier.h>
19c7c3f05eSSergey Senozhatsky #include <linux/vt_kern.h>
20c95dbf27SIngo Molnar #include <linux/module.h>
21c95dbf27SIngo Molnar #include <linux/random.h>
22de7edd31SSteven Rostedt (Red Hat) #include <linux/ftrace.h>
23c95dbf27SIngo Molnar #include <linux/reboot.h>
24c95dbf27SIngo Molnar #include <linux/delay.h>
25c95dbf27SIngo Molnar #include <linux/kexec.h>
26f39650deSAndy Shevchenko #include <linux/panic_notifier.h>
27c95dbf27SIngo Molnar #include <linux/sched.h>
285d5dd3e4SAndy Shevchenko #include <linux/string_helpers.h>
29c95dbf27SIngo Molnar #include <linux/sysrq.h>
30c95dbf27SIngo Molnar #include <linux/init.h>
31c95dbf27SIngo Molnar #include <linux/nmi.h>
3208d78658SVitaly Kuznetsov #include <linux/console.h>
332553b67aSJosh Poimboeuf #include <linux/bug.h>
347a46ec0eSKees Cook #include <linux/ratelimit.h>
35b1fca27dSAndi Kleen #include <linux/debugfs.h>
368b05aa26SKees Cook #include <linux/sysfs.h>
375a5d7e9bSPeter Zijlstra #include <linux/context_tracking.h>
38aff1db0eSJani Nikula #include <linux/seq_buf.h>
3923b36fecSMarco Elver #include <trace/events/error_report.h>
40b1fca27dSAndi Kleen #include <asm/sections.h>
411da177e4SLinus Torvalds 
42c7ff0d9cSTAMUKI Shoichi #define PANIC_TIMER_STEP 100
43c7ff0d9cSTAMUKI Shoichi #define PANIC_BLINK_SPD 18
44c7ff0d9cSTAMUKI Shoichi 
4560c958d8SGuilherme G. Piccoli #ifdef CONFIG_SMP
4660c958d8SGuilherme G. Piccoli /*
4760c958d8SGuilherme G. Piccoli  * Should we dump all CPUs backtraces in an oops event?
4860c958d8SGuilherme G. Piccoli  * Defaults to 0, can be changed via sysctl.
4960c958d8SGuilherme G. Piccoli  */
509df91869Stangmeng static unsigned int __read_mostly sysctl_oops_all_cpu_backtrace;
519df91869Stangmeng #else
529df91869Stangmeng #define sysctl_oops_all_cpu_backtrace 0
5360c958d8SGuilherme G. Piccoli #endif /* CONFIG_SMP */
5460c958d8SGuilherme G. Piccoli 
552a01bb38SKyle McMartin int panic_on_oops = CONFIG_PANIC_ON_OOPS_VALUE;
56bc4f2f54SKees Cook static unsigned long tainted_mask =
57595b893eSKees Cook 	IS_ENABLED(CONFIG_RANDSTRUCT) ? (1 << TAINT_RANDSTRUCT) : 0;
58dd287796SAndrew Morton static int pause_on_oops;
59dd287796SAndrew Morton static int pause_on_oops_flag;
60dd287796SAndrew Morton static DEFINE_SPINLOCK(pause_on_oops_lock);
615375b708SHATAYAMA Daisuke bool crash_kexec_post_notifiers;
629e3961a0SPrarit Bhargava int panic_on_warn __read_mostly;
63db38d5c1SRafael Aquini unsigned long panic_on_taint;
64db38d5c1SRafael Aquini bool panic_on_taint_nousertaint = false;
659fc9e278SKees Cook static unsigned int warn_limit __read_mostly;
661da177e4SLinus Torvalds 
67bcc954c6SRyo Takakura bool panic_triggering_all_cpu_backtrace;
68bcc954c6SRyo Takakura 
695800dc3cSJason Baron int panic_timeout = CONFIG_PANIC_TIMEOUT;
7081e88fdcSHuang Ying EXPORT_SYMBOL_GPL(panic_timeout);
711da177e4SLinus Torvalds 
72d999bd93SFeng Tang #define PANIC_PRINT_TASK_INFO		0x00000001
73d999bd93SFeng Tang #define PANIC_PRINT_MEM_INFO		0x00000002
74d999bd93SFeng Tang #define PANIC_PRINT_TIMER_INFO		0x00000004
75d999bd93SFeng Tang #define PANIC_PRINT_LOCK_INFO		0x00000008
76d999bd93SFeng Tang #define PANIC_PRINT_FTRACE_INFO		0x00000010
77de6da1e8SFeng Tang #define PANIC_PRINT_ALL_PRINTK_MSG	0x00000020
788d470a45SGuilherme G. Piccoli #define PANIC_PRINT_ALL_CPU_BT		0x00000040
792e3fc6caSFeng Tang #define PANIC_PRINT_BLOCKED_TASKS	0x00000080
8081c9d43fSFeng Tang unsigned long panic_print;
81d999bd93SFeng Tang 
82e041c683SAlan Stern ATOMIC_NOTIFIER_HEAD(panic_notifier_list);
831da177e4SLinus Torvalds 
841da177e4SLinus Torvalds EXPORT_SYMBOL(panic_notifier_list);
851da177e4SLinus Torvalds 
869360d035SKees Cook #ifdef CONFIG_SYSCTL
871751f872SJoel Granados static const struct ctl_table kern_panic_table[] = {
889360d035SKees Cook #ifdef CONFIG_SMP
899df91869Stangmeng 	{
909df91869Stangmeng 		.procname       = "oops_all_cpu_backtrace",
919df91869Stangmeng 		.data           = &sysctl_oops_all_cpu_backtrace,
929df91869Stangmeng 		.maxlen         = sizeof(int),
939df91869Stangmeng 		.mode           = 0644,
949df91869Stangmeng 		.proc_handler   = proc_dointvec_minmax,
959df91869Stangmeng 		.extra1         = SYSCTL_ZERO,
969df91869Stangmeng 		.extra2         = SYSCTL_ONE,
979df91869Stangmeng 	},
989360d035SKees Cook #endif
999fc9e278SKees Cook 	{
1009fc9e278SKees Cook 		.procname       = "warn_limit",
1019fc9e278SKees Cook 		.data           = &warn_limit,
1029fc9e278SKees Cook 		.maxlen         = sizeof(warn_limit),
1039fc9e278SKees Cook 		.mode           = 0644,
1049fc9e278SKees Cook 		.proc_handler   = proc_douintvec,
1059fc9e278SKees Cook 	},
1069df91869Stangmeng };
1079df91869Stangmeng 
kernel_panic_sysctls_init(void)1089df91869Stangmeng static __init int kernel_panic_sysctls_init(void)
1099df91869Stangmeng {
1109df91869Stangmeng 	register_sysctl_init("kernel", kern_panic_table);
1119df91869Stangmeng 	return 0;
1129df91869Stangmeng }
1139df91869Stangmeng late_initcall(kernel_panic_sysctls_init);
1149df91869Stangmeng #endif
1159df91869Stangmeng 
1168b05aa26SKees Cook static atomic_t warn_count = ATOMIC_INIT(0);
1178b05aa26SKees Cook 
1188b05aa26SKees Cook #ifdef CONFIG_SYSFS
warn_count_show(struct kobject * kobj,struct kobj_attribute * attr,char * page)1198b05aa26SKees Cook static ssize_t warn_count_show(struct kobject *kobj, struct kobj_attribute *attr,
1208b05aa26SKees Cook 			       char *page)
1218b05aa26SKees Cook {
1228b05aa26SKees Cook 	return sysfs_emit(page, "%d\n", atomic_read(&warn_count));
1238b05aa26SKees Cook }
1248b05aa26SKees Cook 
1258b05aa26SKees Cook static struct kobj_attribute warn_count_attr = __ATTR_RO(warn_count);
1268b05aa26SKees Cook 
kernel_panic_sysfs_init(void)1278b05aa26SKees Cook static __init int kernel_panic_sysfs_init(void)
1288b05aa26SKees Cook {
1298b05aa26SKees Cook 	sysfs_add_file_to_group(kernel_kobj, &warn_count_attr.attr, NULL);
1308b05aa26SKees Cook 	return 0;
1318b05aa26SKees Cook }
1328b05aa26SKees Cook late_initcall(kernel_panic_sysfs_init);
1338b05aa26SKees Cook #endif
1348b05aa26SKees Cook 
no_blink(int state)135c7ff0d9cSTAMUKI Shoichi static long no_blink(int state)
1368aeee85aSAnton Blanchard {
137c7ff0d9cSTAMUKI Shoichi 	return 0;
138c7ff0d9cSTAMUKI Shoichi }
1398aeee85aSAnton Blanchard 
140c7ff0d9cSTAMUKI Shoichi /* Returns how long it waited in ms */
141c7ff0d9cSTAMUKI Shoichi long (*panic_blink)(int state);
142c7ff0d9cSTAMUKI Shoichi EXPORT_SYMBOL(panic_blink);
1438aeee85aSAnton Blanchard 
14493e13a36SMichael Holzheu /*
14593e13a36SMichael Holzheu  * Stop ourself in panic -- architecture code may override this
14693e13a36SMichael Holzheu  */
panic_smp_self_stop(void)1477412a60dSJosh Poimboeuf void __weak __noreturn panic_smp_self_stop(void)
14893e13a36SMichael Holzheu {
14993e13a36SMichael Holzheu 	while (1)
15093e13a36SMichael Holzheu 		cpu_relax();
15193e13a36SMichael Holzheu }
15293e13a36SMichael Holzheu 
15358c5661fSHidehiro Kawai /*
15458c5661fSHidehiro Kawai  * Stop ourselves in NMI context if another CPU has already panicked. Arch code
15558c5661fSHidehiro Kawai  * may override this to prepare for crash dumping, e.g. save regs info.
15658c5661fSHidehiro Kawai  */
nmi_panic_self_stop(struct pt_regs * regs)15727dea14cSJosh Poimboeuf void __weak __noreturn nmi_panic_self_stop(struct pt_regs *regs)
15858c5661fSHidehiro Kawai {
15958c5661fSHidehiro Kawai 	panic_smp_self_stop();
16058c5661fSHidehiro Kawai }
16158c5661fSHidehiro Kawai 
1620ee59413SHidehiro Kawai /*
1630ee59413SHidehiro Kawai  * Stop other CPUs in panic.  Architecture dependent code may override this
1640ee59413SHidehiro Kawai  * with more suitable version.  For example, if the architecture supports
1650ee59413SHidehiro Kawai  * crash dump, it should save registers of each stopped CPU and disable
1660ee59413SHidehiro Kawai  * per-CPU features such as virtualization extensions.
1670ee59413SHidehiro Kawai  */
crash_smp_send_stop(void)1680ee59413SHidehiro Kawai void __weak crash_smp_send_stop(void)
1690ee59413SHidehiro Kawai {
1700ee59413SHidehiro Kawai 	static int cpus_stopped;
1710ee59413SHidehiro Kawai 
1720ee59413SHidehiro Kawai 	/*
1730ee59413SHidehiro Kawai 	 * This function can be called twice in panic path, but obviously
1740ee59413SHidehiro Kawai 	 * we execute this only once.
1750ee59413SHidehiro Kawai 	 */
1760ee59413SHidehiro Kawai 	if (cpus_stopped)
1770ee59413SHidehiro Kawai 		return;
1780ee59413SHidehiro Kawai 
1790ee59413SHidehiro Kawai 	/*
1800ee59413SHidehiro Kawai 	 * Note smp_send_stop is the usual smp shutdown function, which
1810ee59413SHidehiro Kawai 	 * unfortunately means it may not be hardened to work in a panic
1820ee59413SHidehiro Kawai 	 * situation.
1830ee59413SHidehiro Kawai 	 */
1840ee59413SHidehiro Kawai 	smp_send_stop();
1850ee59413SHidehiro Kawai 	cpus_stopped = 1;
1860ee59413SHidehiro Kawai }
1870ee59413SHidehiro Kawai 
1881717f209SHidehiro Kawai atomic_t panic_cpu = ATOMIC_INIT(PANIC_CPU_INVALID);
1891717f209SHidehiro Kawai 
190ebc41f20SHidehiro Kawai /*
191ebc41f20SHidehiro Kawai  * A variant of panic() called from NMI context. We return if we've already
192ebc41f20SHidehiro Kawai  * panicked on this CPU. If another CPU already panicked, loop in
193ebc41f20SHidehiro Kawai  * nmi_panic_self_stop() which can provide architecture dependent code such
194ebc41f20SHidehiro Kawai  * as saving register state for crash dump.
195ebc41f20SHidehiro Kawai  */
nmi_panic(struct pt_regs * regs,const char * msg)196ebc41f20SHidehiro Kawai void nmi_panic(struct pt_regs *regs, const char *msg)
197ebc41f20SHidehiro Kawai {
1989734fe4dSUros Bizjak 	int old_cpu, this_cpu;
199ebc41f20SHidehiro Kawai 
2009734fe4dSUros Bizjak 	old_cpu = PANIC_CPU_INVALID;
2019734fe4dSUros Bizjak 	this_cpu = raw_smp_processor_id();
202ebc41f20SHidehiro Kawai 
2039734fe4dSUros Bizjak 	/* atomic_try_cmpxchg updates old_cpu on failure */
2049734fe4dSUros Bizjak 	if (atomic_try_cmpxchg(&panic_cpu, &old_cpu, this_cpu))
205ebc41f20SHidehiro Kawai 		panic("%s", msg);
2069734fe4dSUros Bizjak 	else if (old_cpu != this_cpu)
207ebc41f20SHidehiro Kawai 		nmi_panic_self_stop(regs);
208ebc41f20SHidehiro Kawai }
209ebc41f20SHidehiro Kawai EXPORT_SYMBOL(nmi_panic);
210ebc41f20SHidehiro Kawai 
panic_print_sys_info(bool console_flush)211f953f140SGuilherme G. Piccoli static void panic_print_sys_info(bool console_flush)
212d999bd93SFeng Tang {
213f953f140SGuilherme G. Piccoli 	if (console_flush) {
214de6da1e8SFeng Tang 		if (panic_print & PANIC_PRINT_ALL_PRINTK_MSG)
215de6da1e8SFeng Tang 			console_flush_on_panic(CONSOLE_REPLAY_ALL);
216f953f140SGuilherme G. Piccoli 		return;
217f953f140SGuilherme G. Piccoli 	}
218de6da1e8SFeng Tang 
219d999bd93SFeng Tang 	if (panic_print & PANIC_PRINT_TASK_INFO)
220d999bd93SFeng Tang 		show_state();
221d999bd93SFeng Tang 
222d999bd93SFeng Tang 	if (panic_print & PANIC_PRINT_MEM_INFO)
223527ed4f7SKefeng Wang 		show_mem();
224d999bd93SFeng Tang 
225d999bd93SFeng Tang 	if (panic_print & PANIC_PRINT_TIMER_INFO)
226d999bd93SFeng Tang 		sysrq_timer_list_show();
227d999bd93SFeng Tang 
228d999bd93SFeng Tang 	if (panic_print & PANIC_PRINT_LOCK_INFO)
229d999bd93SFeng Tang 		debug_show_all_locks();
230d999bd93SFeng Tang 
231d999bd93SFeng Tang 	if (panic_print & PANIC_PRINT_FTRACE_INFO)
232d999bd93SFeng Tang 		ftrace_dump(DUMP_ALL);
2332e3fc6caSFeng Tang 
2342e3fc6caSFeng Tang 	if (panic_print & PANIC_PRINT_BLOCKED_TASKS)
2352e3fc6caSFeng Tang 		show_state_filter(TASK_UNINTERRUPTIBLE);
236d999bd93SFeng Tang }
237d999bd93SFeng Tang 
check_panic_on_warn(const char * origin)23879cc1ba7SKees Cook void check_panic_on_warn(const char *origin)
23979cc1ba7SKees Cook {
2407535b832SKees Cook 	unsigned int limit;
2417535b832SKees Cook 
24279cc1ba7SKees Cook 	if (panic_on_warn)
24379cc1ba7SKees Cook 		panic("%s: panic_on_warn set ...\n", origin);
2449fc9e278SKees Cook 
2457535b832SKees Cook 	limit = READ_ONCE(warn_limit);
2467535b832SKees Cook 	if (atomic_inc_return(&warn_count) >= limit && limit)
2479fc9e278SKees Cook 		panic("%s: system warned too often (kernel.warn_limit is %d)",
2487535b832SKees Cook 		      origin, limit);
24979cc1ba7SKees Cook }
25079cc1ba7SKees Cook 
251b905039eSGuilherme G. Piccoli /*
252b905039eSGuilherme G. Piccoli  * Helper that triggers the NMI backtrace (if set in panic_print)
253b905039eSGuilherme G. Piccoli  * and then performs the secondary CPUs shutdown - we cannot have
254b905039eSGuilherme G. Piccoli  * the NMI backtrace after the CPUs are off!
255b905039eSGuilherme G. Piccoli  */
panic_other_cpus_shutdown(bool crash_kexec)256b905039eSGuilherme G. Piccoli static void panic_other_cpus_shutdown(bool crash_kexec)
257b905039eSGuilherme G. Piccoli {
258bcc954c6SRyo Takakura 	if (panic_print & PANIC_PRINT_ALL_CPU_BT) {
259bcc954c6SRyo Takakura 		/* Temporary allow non-panic CPUs to write their backtraces. */
260bcc954c6SRyo Takakura 		panic_triggering_all_cpu_backtrace = true;
261b905039eSGuilherme G. Piccoli 		trigger_all_cpu_backtrace();
262bcc954c6SRyo Takakura 		panic_triggering_all_cpu_backtrace = false;
263bcc954c6SRyo Takakura 	}
264b905039eSGuilherme G. Piccoli 
265b905039eSGuilherme G. Piccoli 	/*
266b905039eSGuilherme G. Piccoli 	 * Note that smp_send_stop() is the usual SMP shutdown function,
267b905039eSGuilherme G. Piccoli 	 * which unfortunately may not be hardened to work in a panic
268b905039eSGuilherme G. Piccoli 	 * situation. If we want to do crash dump after notifier calls
269b905039eSGuilherme G. Piccoli 	 * and kmsg_dump, we will need architecture dependent extra
270b905039eSGuilherme G. Piccoli 	 * bits in addition to stopping other CPUs, hence we rely on
271b905039eSGuilherme G. Piccoli 	 * crash_smp_send_stop() for that.
272b905039eSGuilherme G. Piccoli 	 */
273b905039eSGuilherme G. Piccoli 	if (!crash_kexec)
274b905039eSGuilherme G. Piccoli 		smp_send_stop();
275b905039eSGuilherme G. Piccoli 	else
276b905039eSGuilherme G. Piccoli 		crash_smp_send_stop();
277b905039eSGuilherme G. Piccoli }
278b905039eSGuilherme G. Piccoli 
2791da177e4SLinus Torvalds /**
2801da177e4SLinus Torvalds  *	panic - halt the system
2811da177e4SLinus Torvalds  *	@fmt: The text string to print
2821da177e4SLinus Torvalds  *
2831da177e4SLinus Torvalds  *	Display a message, then perform cleanups.
2841da177e4SLinus Torvalds  *
2851da177e4SLinus Torvalds  *	This function never returns.
2861da177e4SLinus Torvalds  */
panic(const char * fmt,...)2879402c95fSJoe Perches void panic(const char *fmt, ...)
2881da177e4SLinus Torvalds {
2891da177e4SLinus Torvalds 	static char buf[1024];
2901da177e4SLinus Torvalds 	va_list args;
291b49dec1cSBorislav Petkov 	long i, i_next = 0, len;
292c7ff0d9cSTAMUKI Shoichi 	int state = 0;
2931717f209SHidehiro Kawai 	int old_cpu, this_cpu;
294b26e27ddSHidehiro Kawai 	bool _crash_kexec_post_notifiers = crash_kexec_post_notifiers;
2951da177e4SLinus Torvalds 
2961a2383e8STiezhu Yang 	if (panic_on_warn) {
2971a2383e8STiezhu Yang 		/*
2981a2383e8STiezhu Yang 		 * This thread may hit another WARN() in the panic path.
2991a2383e8STiezhu Yang 		 * Resetting this prevents additional WARN() from panicking the
3001a2383e8STiezhu Yang 		 * system on this thread.  Other threads are blocked by the
3011a2383e8STiezhu Yang 		 * panic_mutex in panic().
3021a2383e8STiezhu Yang 		 */
3031a2383e8STiezhu Yang 		panic_on_warn = 0;
3041a2383e8STiezhu Yang 	}
3051a2383e8STiezhu Yang 
306dc009d92SEric W. Biederman 	/*
307190320c3SVikram Mulukutla 	 * Disable local interrupts. This will prevent panic_smp_self_stop
308190320c3SVikram Mulukutla 	 * from deadlocking the first cpu that invokes the panic, since
309190320c3SVikram Mulukutla 	 * there is nothing to prevent an interrupt handler (that runs
3101717f209SHidehiro Kawai 	 * after setting panic_cpu) from invoking panic() again.
311190320c3SVikram Mulukutla 	 */
312190320c3SVikram Mulukutla 	local_irq_disable();
31320bb759aSWill Deacon 	preempt_disable_notrace();
314190320c3SVikram Mulukutla 
315190320c3SVikram Mulukutla 	/*
316c95dbf27SIngo Molnar 	 * It's possible to come here directly from a panic-assertion and
317c95dbf27SIngo Molnar 	 * not have preempt disabled. Some functions called from here want
318dc009d92SEric W. Biederman 	 * preempt to be disabled. No point enabling it later though...
31993e13a36SMichael Holzheu 	 *
32093e13a36SMichael Holzheu 	 * Only one CPU is allowed to execute the panic code from here. For
32193e13a36SMichael Holzheu 	 * multiple parallel invocations of panic, all other CPUs either
32293e13a36SMichael Holzheu 	 * stop themself or will wait until they are stopped by the 1st CPU
32393e13a36SMichael Holzheu 	 * with smp_send_stop().
3241717f209SHidehiro Kawai 	 *
3259734fe4dSUros Bizjak 	 * cmpxchg success means this is the 1st CPU which comes here,
3269734fe4dSUros Bizjak 	 * so go ahead.
3271717f209SHidehiro Kawai 	 * `old_cpu == this_cpu' means we came from nmi_panic() which sets
3281717f209SHidehiro Kawai 	 * panic_cpu to this CPU.  In this case, this is also the 1st CPU.
329dc009d92SEric W. Biederman 	 */
3309734fe4dSUros Bizjak 	old_cpu = PANIC_CPU_INVALID;
3311717f209SHidehiro Kawai 	this_cpu = raw_smp_processor_id();
3321717f209SHidehiro Kawai 
3339734fe4dSUros Bizjak 	/* atomic_try_cmpxchg updates old_cpu on failure */
3349734fe4dSUros Bizjak 	if (atomic_try_cmpxchg(&panic_cpu, &old_cpu, this_cpu)) {
3359734fe4dSUros Bizjak 		/* go ahead */
3369734fe4dSUros Bizjak 	} else if (old_cpu != this_cpu)
33793e13a36SMichael Holzheu 		panic_smp_self_stop();
338dc009d92SEric W. Biederman 
3395b530fc1SAnton Blanchard 	console_verbose();
3401da177e4SLinus Torvalds 	bust_spinlocks(1);
3411da177e4SLinus Torvalds 	va_start(args, fmt);
342b49dec1cSBorislav Petkov 	len = vscnprintf(buf, sizeof(buf), fmt, args);
3431da177e4SLinus Torvalds 	va_end(args);
344b49dec1cSBorislav Petkov 
345b49dec1cSBorislav Petkov 	if (len && buf[len - 1] == '\n')
346b49dec1cSBorislav Petkov 		buf[len - 1] = '\0';
347b49dec1cSBorislav Petkov 
348d7c0847fSFabian Frederick 	pr_emerg("Kernel panic - not syncing: %s\n", buf);
3495cb27301SIngo Molnar #ifdef CONFIG_DEBUG_BUGVERBOSE
3506e6f0a1fSAndi Kleen 	/*
3516e6f0a1fSAndi Kleen 	 * Avoid nested stack-dumping if a panic occurs during oops processing
3526e6f0a1fSAndi Kleen 	 */
353026ee1f6SJason Wessel 	if (!test_taint(TAINT_DIE) && oops_in_progress <= 1)
3545cb27301SIngo Molnar 		dump_stack();
3555cb27301SIngo Molnar #endif
3561da177e4SLinus Torvalds 
357dc009d92SEric W. Biederman 	/*
3587d92bda2SDouglas Anderson 	 * If kgdb is enabled, give it a chance to run before we stop all
3597d92bda2SDouglas Anderson 	 * the other CPUs or else we won't be able to debug processes left
3607d92bda2SDouglas Anderson 	 * running on them.
3617d92bda2SDouglas Anderson 	 */
3627d92bda2SDouglas Anderson 	kgdb_panic(buf);
3637d92bda2SDouglas Anderson 
3647d92bda2SDouglas Anderson 	/*
365dc009d92SEric W. Biederman 	 * If we have crashed and we have a crash kernel loaded let it handle
366dc009d92SEric W. Biederman 	 * everything else.
367f06e5153SMasami Hiramatsu 	 * If we want to run this after calling panic_notifiers, pass
368f06e5153SMasami Hiramatsu 	 * the "crash_kexec_post_notifiers" option to the kernel.
3697bbee5caSHidehiro Kawai 	 *
3707bbee5caSHidehiro Kawai 	 * Bypass the panic_cpu check and call __crash_kexec directly.
371dc009d92SEric W. Biederman 	 */
372b905039eSGuilherme G. Piccoli 	if (!_crash_kexec_post_notifiers)
3737bbee5caSHidehiro Kawai 		__crash_kexec(NULL);
374dc009d92SEric W. Biederman 
375b905039eSGuilherme G. Piccoli 	panic_other_cpus_shutdown(_crash_kexec_post_notifiers);
3761da177e4SLinus Torvalds 
377e35a8884SJohn Ogness 	printk_legacy_allow_panic_sync();
378e35a8884SJohn Ogness 
3796723734cSKees Cook 	/*
3806723734cSKees Cook 	 * Run any panic handlers, including those that might need to
3816723734cSKees Cook 	 * add information to the kmsg dump output.
3826723734cSKees Cook 	 */
383e041c683SAlan Stern 	atomic_notifier_call_chain(&panic_notifier_list, 0, buf);
3841da177e4SLinus Torvalds 
385f953f140SGuilherme G. Piccoli 	panic_print_sys_info(false);
386f953f140SGuilherme G. Piccoli 
387e1a261baSJocelyn Falempe 	kmsg_dump_desc(KMSG_DUMP_PANIC, buf);
3886723734cSKees Cook 
389f06e5153SMasami Hiramatsu 	/*
390f06e5153SMasami Hiramatsu 	 * If you doubt kdump always works fine in any situation,
391f06e5153SMasami Hiramatsu 	 * "crash_kexec_post_notifiers" offers you a chance to run
392f06e5153SMasami Hiramatsu 	 * panic_notifiers and dumping kmsg before kdump.
393f06e5153SMasami Hiramatsu 	 * Note: since some panic_notifiers can make crashed kernel
394f06e5153SMasami Hiramatsu 	 * more unstable, it can increase risks of the kdump failure too.
3957bbee5caSHidehiro Kawai 	 *
3967bbee5caSHidehiro Kawai 	 * Bypass the panic_cpu check and call __crash_kexec directly.
397f06e5153SMasami Hiramatsu 	 */
398b26e27ddSHidehiro Kawai 	if (_crash_kexec_post_notifiers)
3997bbee5caSHidehiro Kawai 		__crash_kexec(NULL);
400f06e5153SMasami Hiramatsu 
401c7c3f05eSSergey Senozhatsky 	console_unblank();
402d014e889SAaro Koskinen 
40308d78658SVitaly Kuznetsov 	/*
40408d78658SVitaly Kuznetsov 	 * We may have ended up stopping the CPU holding the lock (in
40508d78658SVitaly Kuznetsov 	 * smp_send_stop()) while still having some valuable data in the console
40608d78658SVitaly Kuznetsov 	 * buffer.  Try to acquire the lock then release it regardless of the
4077625b3a0SVitaly Kuznetsov 	 * result.  The release will also print the buffers out.  Locks debug
4087625b3a0SVitaly Kuznetsov 	 * should be disabled to avoid reporting bad unlock balance when
4097625b3a0SVitaly Kuznetsov 	 * panic() is not being callled from OOPS.
41008d78658SVitaly Kuznetsov 	 */
4117625b3a0SVitaly Kuznetsov 	debug_locks_off();
412de6da1e8SFeng Tang 	console_flush_on_panic(CONSOLE_FLUSH_PENDING);
41308d78658SVitaly Kuznetsov 
414f953f140SGuilherme G. Piccoli 	panic_print_sys_info(true);
415d999bd93SFeng Tang 
416c7ff0d9cSTAMUKI Shoichi 	if (!panic_blink)
417c7ff0d9cSTAMUKI Shoichi 		panic_blink = no_blink;
418c7ff0d9cSTAMUKI Shoichi 
419dc009d92SEric W. Biederman 	if (panic_timeout > 0) {
4201da177e4SLinus Torvalds 		/*
4211da177e4SLinus Torvalds 		 * Delay timeout seconds before rebooting the machine.
422c95dbf27SIngo Molnar 		 * We can't use the "normal" timers since we just panicked.
4231da177e4SLinus Torvalds 		 */
424ff7a28a0SJiri Slaby 		pr_emerg("Rebooting in %d seconds..\n", panic_timeout);
425c95dbf27SIngo Molnar 
426c7ff0d9cSTAMUKI Shoichi 		for (i = 0; i < panic_timeout * 1000; i += PANIC_TIMER_STEP) {
4271da177e4SLinus Torvalds 			touch_nmi_watchdog();
428c7ff0d9cSTAMUKI Shoichi 			if (i >= i_next) {
429c7ff0d9cSTAMUKI Shoichi 				i += panic_blink(state ^= 1);
430c7ff0d9cSTAMUKI Shoichi 				i_next = i + 3600 / PANIC_BLINK_SPD;
431c7ff0d9cSTAMUKI Shoichi 			}
432c7ff0d9cSTAMUKI Shoichi 			mdelay(PANIC_TIMER_STEP);
4331da177e4SLinus Torvalds 		}
4344302fbc8SHugh Dickins 	}
4354302fbc8SHugh Dickins 	if (panic_timeout != 0) {
436c95dbf27SIngo Molnar 		/*
437c95dbf27SIngo Molnar 		 * This will not be a clean reboot, with everything
4382f048ea8SEric W. Biederman 		 * shutting down.  But if there is a chance of
4392f048ea8SEric W. Biederman 		 * rebooting the system it will be rebooted.
4401da177e4SLinus Torvalds 		 */
441b287a25aSAaro Koskinen 		if (panic_reboot_mode != REBOOT_UNDEFINED)
442b287a25aSAaro Koskinen 			reboot_mode = panic_reboot_mode;
4432f048ea8SEric W. Biederman 		emergency_restart();
4441da177e4SLinus Torvalds 	}
4451da177e4SLinus Torvalds #ifdef __sparc__
4461da177e4SLinus Torvalds 	{
4471da177e4SLinus Torvalds 		extern int stop_a_enabled;
448a271c241STom 'spot' Callaway 		/* Make sure the user can actually press Stop-A (L1-A) */
4491da177e4SLinus Torvalds 		stop_a_enabled = 1;
4507db60d05SVijay Kumar 		pr_emerg("Press Stop-A (L1-A) from sun keyboard or send break\n"
4517db60d05SVijay Kumar 			 "twice on console to return to the boot prom\n");
4521da177e4SLinus Torvalds 	}
4531da177e4SLinus Torvalds #endif
454347a8dc3SMartin Schwidefsky #if defined(CONFIG_S390)
45598587c2dSMartin Schwidefsky 	disabled_wait();
4561da177e4SLinus Torvalds #endif
4575ad75105SBorislav Petkov 	pr_emerg("---[ end Kernel panic - not syncing: %s ]---\n", buf);
458c39ea0b9SFeng Tang 
459c39ea0b9SFeng Tang 	/* Do not scroll important messages printed above */
460c39ea0b9SFeng Tang 	suppress_printk = 1;
461d988d9a9SJohn Ogness 
462d988d9a9SJohn Ogness 	/*
463d988d9a9SJohn Ogness 	 * The final messages may not have been printed if in a context that
464d988d9a9SJohn Ogness 	 * defers printing (such as NMI) and irq_work is not available.
465d988d9a9SJohn Ogness 	 * Explicitly flush the kernel log buffer one last time.
466d988d9a9SJohn Ogness 	 */
467d988d9a9SJohn Ogness 	console_flush_on_panic(CONSOLE_FLUSH_PENDING);
4685dde3b73SJohn Ogness 	nbcon_atomic_flush_unsafe();
469d988d9a9SJohn Ogness 
4701da177e4SLinus Torvalds 	local_irq_enable();
471c7ff0d9cSTAMUKI Shoichi 	for (i = 0; ; i += PANIC_TIMER_STEP) {
472c22db941SJan Beulich 		touch_softlockup_watchdog();
473c7ff0d9cSTAMUKI Shoichi 		if (i >= i_next) {
474c7ff0d9cSTAMUKI Shoichi 			i += panic_blink(state ^= 1);
475c7ff0d9cSTAMUKI Shoichi 			i_next = i + 3600 / PANIC_BLINK_SPD;
476c7ff0d9cSTAMUKI Shoichi 		}
477c7ff0d9cSTAMUKI Shoichi 		mdelay(PANIC_TIMER_STEP);
4781da177e4SLinus Torvalds 	}
4791da177e4SLinus Torvalds }
4801da177e4SLinus Torvalds 
4811da177e4SLinus Torvalds EXPORT_SYMBOL(panic);
4821da177e4SLinus Torvalds 
483f36fc96cSJani Nikula #define TAINT_FLAG(taint, _c_true, _c_false, _module)			\
484f36fc96cSJani Nikula 	[ TAINT_##taint ] = {						\
485f36fc96cSJani Nikula 		.c_true = _c_true, .c_false = _c_false,			\
486f36fc96cSJani Nikula 		.module = _module,					\
4872f183c68SJani Nikula 		.desc = #taint,						\
488f36fc96cSJani Nikula 	}
489f36fc96cSJani Nikula 
4907fd8329bSPetr Mladek /*
4917fd8329bSPetr Mladek  * TAINT_FORCED_RMMOD could be a per-module flag but the module
4927fd8329bSPetr Mladek  * is being removed anyway.
4937fd8329bSPetr Mladek  */
4947fd8329bSPetr Mladek const struct taint_flag taint_flags[TAINT_FLAGS_COUNT] = {
495f36fc96cSJani Nikula 	TAINT_FLAG(PROPRIETARY_MODULE,		'P', 'G', true),
496f36fc96cSJani Nikula 	TAINT_FLAG(FORCED_MODULE,		'F', ' ', true),
497f36fc96cSJani Nikula 	TAINT_FLAG(CPU_OUT_OF_SPEC,		'S', ' ', false),
498f36fc96cSJani Nikula 	TAINT_FLAG(FORCED_RMMOD,		'R', ' ', false),
499f36fc96cSJani Nikula 	TAINT_FLAG(MACHINE_CHECK,		'M', ' ', false),
500f36fc96cSJani Nikula 	TAINT_FLAG(BAD_PAGE,			'B', ' ', false),
501f36fc96cSJani Nikula 	TAINT_FLAG(USER,			'U', ' ', false),
502f36fc96cSJani Nikula 	TAINT_FLAG(DIE,				'D', ' ', false),
503f36fc96cSJani Nikula 	TAINT_FLAG(OVERRIDDEN_ACPI_TABLE,	'A', ' ', false),
504f36fc96cSJani Nikula 	TAINT_FLAG(WARN,			'W', ' ', false),
505f36fc96cSJani Nikula 	TAINT_FLAG(CRAP,			'C', ' ', true),
506f36fc96cSJani Nikula 	TAINT_FLAG(FIRMWARE_WORKAROUND,		'I', ' ', false),
507f36fc96cSJani Nikula 	TAINT_FLAG(OOT_MODULE,			'O', ' ', true),
508f36fc96cSJani Nikula 	TAINT_FLAG(UNSIGNED_MODULE,		'E', ' ', true),
509f36fc96cSJani Nikula 	TAINT_FLAG(SOFTLOCKUP,			'L', ' ', false),
510f36fc96cSJani Nikula 	TAINT_FLAG(LIVEPATCH,			'K', ' ', true),
511f36fc96cSJani Nikula 	TAINT_FLAG(AUX,				'X', ' ', true),
512f36fc96cSJani Nikula 	TAINT_FLAG(RANDSTRUCT,			'T', ' ', true),
513f36fc96cSJani Nikula 	TAINT_FLAG(TEST,			'N', ' ', true),
51425ddbb18SAndi Kleen 	TAINT_FLAG(FWCTL,			'J', ' ', true),
51525ddbb18SAndi Kleen };
516f36fc96cSJani Nikula 
517f36fc96cSJani Nikula #undef TAINT_FLAG
5182f183c68SJani Nikula 
print_tainted_seq(struct seq_buf * s,bool verbose)519aff1db0eSJani Nikula static void print_tainted_seq(struct seq_buf *s, bool verbose)
5202f183c68SJani Nikula {
521aff1db0eSJani Nikula 	const char *sep = "";
522aff1db0eSJani Nikula 	int i;
523aff1db0eSJani Nikula 
524aff1db0eSJani Nikula 	if (!tainted_mask) {
525aff1db0eSJani Nikula 		seq_buf_puts(s, "Not tainted");
526aff1db0eSJani Nikula 		return;
527aff1db0eSJani Nikula 	}
528aff1db0eSJani Nikula 
529aff1db0eSJani Nikula 	seq_buf_printf(s, "Tainted: ");
530aff1db0eSJani Nikula 	for (i = 0; i < TAINT_FLAGS_COUNT; i++) {
531aff1db0eSJani Nikula 		const struct taint_flag *t = &taint_flags[i];
532aff1db0eSJani Nikula 		bool is_set = test_bit(i, &tainted_mask);
533aff1db0eSJani Nikula 		char c = is_set ? t->c_true : t->c_false;
5342f183c68SJani Nikula 
5352f183c68SJani Nikula 		if (verbose) {
5362f183c68SJani Nikula 			if (is_set) {
5372f183c68SJani Nikula 				seq_buf_printf(s, "%s[%c]=%s", sep, c, t->desc);
5382f183c68SJani Nikula 				sep = ", ";
5392f183c68SJani Nikula 			}
540aff1db0eSJani Nikula 		} else {
541aff1db0eSJani Nikula 			seq_buf_putc(s, c);
542aff1db0eSJani Nikula 		}
5432f183c68SJani Nikula 	}
5442f183c68SJani Nikula }
5452f183c68SJani Nikula 
_print_tainted(bool verbose)5462f183c68SJani Nikula static const char *_print_tainted(bool verbose)
5472f183c68SJani Nikula {
5482f183c68SJani Nikula 	/* FIXME: what should the size be? */
5492f183c68SJani Nikula 	static char buf[sizeof(taint_flags)];
5502f183c68SJani Nikula 	struct seq_buf s;
5512f183c68SJani Nikula 
5522f183c68SJani Nikula 	BUILD_BUG_ON(ARRAY_SIZE(taint_flags) != TAINT_FLAGS_COUNT);
5532f183c68SJani Nikula 
5542f183c68SJani Nikula 	seq_buf_init(&s, buf, sizeof(buf));
5552f183c68SJani Nikula 
5562f183c68SJani Nikula 	print_tainted_seq(&s, verbose);
5572f183c68SJani Nikula 
5582f183c68SJani Nikula 	return seq_buf_str(&s);
559aff1db0eSJani Nikula }
5601da177e4SLinus Torvalds 
5611da177e4SLinus Torvalds /**
5621da177e4SLinus Torvalds  * print_tainted - return a string to represent the kernel taint state.
56357043247SMauro Carvalho Chehab  *
5641da177e4SLinus Torvalds  * For individual taint flag meanings, see Documentation/admin-guide/sysctl/kernel.rst
5659c4560e5SKees Cook  *
5669c4560e5SKees Cook  * The string is overwritten by the next call to print_tainted(),
5671da177e4SLinus Torvalds  * but is always NULL terminated.
5681da177e4SLinus Torvalds  */
print_tainted(void)5691da177e4SLinus Torvalds const char *print_tainted(void)
5702f183c68SJani Nikula {
5712f183c68SJani Nikula 	return _print_tainted(false);
57225ddbb18SAndi Kleen }
5732f183c68SJani Nikula 
5742f183c68SJani Nikula /**
5752f183c68SJani Nikula  * print_tainted_verbose - A more verbose version of print_tainted()
5762f183c68SJani Nikula  */
print_tainted_verbose(void)5772f183c68SJani Nikula const char *print_tainted_verbose(void)
5782f183c68SJani Nikula {
5791da177e4SLinus Torvalds 	return _print_tainted(true);
5801da177e4SLinus Torvalds }
58125ddbb18SAndi Kleen 
test_taint(unsigned flag)58225ddbb18SAndi Kleen int test_taint(unsigned flag)
58325ddbb18SAndi Kleen {
58425ddbb18SAndi Kleen 	return test_bit(flag, &tainted_mask);
58525ddbb18SAndi Kleen }
58625ddbb18SAndi Kleen EXPORT_SYMBOL(test_taint);
58725ddbb18SAndi Kleen 
get_taint(void)58825ddbb18SAndi Kleen unsigned long get_taint(void)
58925ddbb18SAndi Kleen {
59025ddbb18SAndi Kleen 	return tainted_mask;
59125ddbb18SAndi Kleen }
592373d4d09SRusty Russell 
593373d4d09SRusty Russell /**
594373d4d09SRusty Russell  * add_taint: add a taint flag if not already set.
595373d4d09SRusty Russell  * @flag: one of the TAINT_* constants.
596373d4d09SRusty Russell  * @lockdep_ok: whether lock debugging is still OK.
597373d4d09SRusty Russell  *
598373d4d09SRusty Russell  * If something bad has gone wrong, you'll want @lockdebug_ok = false, but for
5999eeba613SFrederic Weisbecker  * some notewortht-but-not-corrupting cases, it can be set to true.
600373d4d09SRusty Russell  */
add_taint(unsigned flag,enum lockdep_ok lockdep_ok)601373d4d09SRusty Russell void add_taint(unsigned flag, enum lockdep_ok lockdep_ok)
602373d4d09SRusty Russell {
603d7c0847fSFabian Frederick 	if (lockdep_ok == LOCKDEP_NOW_UNRELIABLE && __debug_locks_off())
6049eeba613SFrederic Weisbecker 		pr_warn("Disabling lock debugging due to kernel taint\n");
60525ddbb18SAndi Kleen 
606db38d5c1SRafael Aquini 	set_bit(flag, &tainted_mask);
607db38d5c1SRafael Aquini 
608db38d5c1SRafael Aquini 	if (tainted_mask & panic_on_taint) {
609db38d5c1SRafael Aquini 		panic_on_taint = 0;
610db38d5c1SRafael Aquini 		panic("panic_on_taint set ...");
6111da177e4SLinus Torvalds 	}
6121da177e4SLinus Torvalds }
613dd287796SAndrew Morton EXPORT_SYMBOL(add_taint);
614dd287796SAndrew Morton 
spin_msec(int msecs)615dd287796SAndrew Morton static void spin_msec(int msecs)
616dd287796SAndrew Morton {
617dd287796SAndrew Morton 	int i;
618dd287796SAndrew Morton 
619dd287796SAndrew Morton 	for (i = 0; i < msecs; i++) {
620dd287796SAndrew Morton 		touch_nmi_watchdog();
621dd287796SAndrew Morton 		mdelay(1);
622dd287796SAndrew Morton 	}
623dd287796SAndrew Morton }
624dd287796SAndrew Morton 
625dd287796SAndrew Morton /*
626dd287796SAndrew Morton  * It just happens that oops_enter() and oops_exit() are identically
627dd287796SAndrew Morton  * implemented...
628dd287796SAndrew Morton  */
do_oops_enter_exit(void)629dd287796SAndrew Morton static void do_oops_enter_exit(void)
630dd287796SAndrew Morton {
631dd287796SAndrew Morton 	unsigned long flags;
632dd287796SAndrew Morton 	static int spin_counter;
633dd287796SAndrew Morton 
634dd287796SAndrew Morton 	if (!pause_on_oops)
635dd287796SAndrew Morton 		return;
636dd287796SAndrew Morton 
637dd287796SAndrew Morton 	spin_lock_irqsave(&pause_on_oops_lock, flags);
638dd287796SAndrew Morton 	if (pause_on_oops_flag == 0) {
639dd287796SAndrew Morton 		/* This CPU may now print the oops message */
640dd287796SAndrew Morton 		pause_on_oops_flag = 1;
641dd287796SAndrew Morton 	} else {
642dd287796SAndrew Morton 		/* We need to stall this CPU */
643dd287796SAndrew Morton 		if (!spin_counter) {
644dd287796SAndrew Morton 			/* This CPU gets to do the counting */
645dd287796SAndrew Morton 			spin_counter = pause_on_oops;
646dd287796SAndrew Morton 			do {
647dd287796SAndrew Morton 				spin_unlock(&pause_on_oops_lock);
648dd287796SAndrew Morton 				spin_msec(MSEC_PER_SEC);
649dd287796SAndrew Morton 				spin_lock(&pause_on_oops_lock);
650dd287796SAndrew Morton 			} while (--spin_counter);
651dd287796SAndrew Morton 			pause_on_oops_flag = 0;
652dd287796SAndrew Morton 		} else {
653dd287796SAndrew Morton 			/* This CPU waits for a different one */
654dd287796SAndrew Morton 			while (spin_counter) {
655dd287796SAndrew Morton 				spin_unlock(&pause_on_oops_lock);
656dd287796SAndrew Morton 				spin_msec(1);
657dd287796SAndrew Morton 				spin_lock(&pause_on_oops_lock);
658dd287796SAndrew Morton 			}
659dd287796SAndrew Morton 		}
660dd287796SAndrew Morton 	}
661dd287796SAndrew Morton 	spin_unlock_irqrestore(&pause_on_oops_lock, flags);
662dd287796SAndrew Morton }
663dd287796SAndrew Morton 
664c95dbf27SIngo Molnar /*
665c95dbf27SIngo Molnar  * Return true if the calling CPU is allowed to print oops-related info.
666dd287796SAndrew Morton  * This is a bit racy..
66779076e12STiezhu Yang  */
oops_may_print(void)668dd287796SAndrew Morton bool oops_may_print(void)
669dd287796SAndrew Morton {
670dd287796SAndrew Morton 	return pause_on_oops_flag == 0;
671dd287796SAndrew Morton }
672dd287796SAndrew Morton 
673dd287796SAndrew Morton /*
674c95dbf27SIngo Molnar  * Called when the architecture enters its oops handler, before it prints
675c95dbf27SIngo Molnar  * anything.  If this is the first CPU to oops, and it's oopsing the first
676dd287796SAndrew Morton  * time then let it proceed.
677c95dbf27SIngo Molnar  *
678c95dbf27SIngo Molnar  * This is all enabled by the pause_on_oops kernel boot option.  We do all
679c95dbf27SIngo Molnar  * this to ensure that oopses don't scroll off the screen.  It has the
680c95dbf27SIngo Molnar  * side-effect of preventing later-oopsing CPUs from mucking up the display,
681dd287796SAndrew Morton  * too.
682c95dbf27SIngo Molnar  *
683c95dbf27SIngo Molnar  * It turns out that the CPU which is allowed to print ends up pausing for
684c95dbf27SIngo Molnar  * the right duration, whereas all the other CPUs pause for twice as long:
685dd287796SAndrew Morton  * once in oops_enter(), once in oops_exit().
686dd287796SAndrew Morton  */
oops_enter(void)687dd287796SAndrew Morton void oops_enter(void)
6884bdfa0d8SJohn Ogness {
689bdff7870SThomas Gleixner 	nbcon_cpu_emergency_enter();
690c95dbf27SIngo Molnar 	tracing_off();
691c95dbf27SIngo Molnar 	/* can't trust the integrity of the kernel anymore: */
692dd287796SAndrew Morton 	debug_locks_off();
69360c958d8SGuilherme G. Piccoli 	do_oops_enter_exit();
69460c958d8SGuilherme G. Piccoli 
69560c958d8SGuilherme G. Piccoli 	if (sysctl_oops_all_cpu_backtrace)
696dd287796SAndrew Morton 		trigger_all_cpu_backtrace();
697dd287796SAndrew Morton }
69863037f74SYue Hu 
print_oops_end_marker(void)69971c33911SArjan van de Ven static void print_oops_end_marker(void)
700e83a4472SSebastian Andrzej Siewior {
70171c33911SArjan van de Ven 	pr_warn("---[ end trace %016llx ]---\n", 0ULL);
70271c33911SArjan van de Ven }
7032c3b20e9SArjan van de Ven 
704dd287796SAndrew Morton /*
705dd287796SAndrew Morton  * Called when the architecture exits its oops handler, after printing
706dd287796SAndrew Morton  * everything.
707dd287796SAndrew Morton  */
oops_exit(void)708dd287796SAndrew Morton void oops_exit(void)
709dd287796SAndrew Morton {
71071c33911SArjan van de Ven 	do_oops_enter_exit();
7114bdfa0d8SJohn Ogness 	print_oops_end_marker();
712456b565cSSimon Kagstrom 	nbcon_cpu_emergency_exit();
713dd287796SAndrew Morton 	kmsg_dump(KMSG_DUMP_OOPS);
7143162f751SArjan van de Ven }
7152553b67aSJosh Poimboeuf 
7160f6f49a8SLinus Torvalds struct warn_args {
717a8f18b90SArjan van de Ven 	const char *fmt;
7180f6f49a8SLinus Torvalds 	va_list args;
7190f6f49a8SLinus Torvalds };
7202553b67aSJosh Poimboeuf 
__warn(const char * file,int line,void * caller,unsigned taint,struct pt_regs * regs,struct warn_args * args)7212553b67aSJosh Poimboeuf void __warn(const char *file, int line, void *caller, unsigned taint,
7220f6f49a8SLinus Torvalds 	    struct pt_regs *regs, struct warn_args *args)
7234833794dSThomas Gleixner {
7244833794dSThomas Gleixner 	nbcon_cpu_emergency_enter();
725de7edd31SSteven Rostedt (Red Hat) 
726de7edd31SSteven Rostedt (Red Hat) 	disable_trace_on_warning();
7272553b67aSJosh Poimboeuf 
7282553b67aSJosh Poimboeuf 	if (file)
7292553b67aSJosh Poimboeuf 		pr_warn("WARNING: CPU: %d PID: %d at %s:%d %pS\n",
7302553b67aSJosh Poimboeuf 			raw_smp_processor_id(), current->pid, file, line,
7312553b67aSJosh Poimboeuf 			caller);
7322553b67aSJosh Poimboeuf 	else
7332553b67aSJosh Poimboeuf 		pr_warn("WARNING: CPU: %d PID: %d at %pS\n",
73474853dbaSArjan van de Ven 			raw_smp_processor_id(), current->pid, caller);
735b73aa539SBaoquan He 
736b73aa539SBaoquan He #pragma GCC diagnostic push
737b73aa539SBaoquan He #ifndef __clang__
738b73aa539SBaoquan He #pragma GCC diagnostic ignored "-Wsuggest-attribute=format"
7390f6f49a8SLinus Torvalds #endif
7400f6f49a8SLinus Torvalds 	if (args)
741b73aa539SBaoquan He 		vprintk(args->fmt, args->args);
742a8f18b90SArjan van de Ven #pragma GCC diagnostic pop
7433f388f28SAlexey Kardashevskiy 
7443f388f28SAlexey Kardashevskiy 	print_modules();
7453f388f28SAlexey Kardashevskiy 
7463f388f28SAlexey Kardashevskiy 	if (regs)
7473f388f28SAlexey Kardashevskiy 		show_regs(regs);
74879cc1ba7SKees Cook 
7499e3961a0SPrarit Bhargava 	check_panic_on_warn("kernel");
7502f31ad64SChristophe Leroy 
751a8f18b90SArjan van de Ven 	if (!regs)
7522553b67aSJosh Poimboeuf 		dump_stack();
7534c281074SSteven Rostedt (VMware) 
7544c281074SSteven Rostedt (VMware) 	print_irqtrace_events(current);
755a8f18b90SArjan van de Ven 
75623b36fecSMarco Elver 	print_oops_end_marker();
7572553b67aSJosh Poimboeuf 	trace_error_report_end(ERROR_DETECTOR_WARN, (unsigned long)caller);
758373d4d09SRusty Russell 
759373d4d09SRusty Russell 	/* Just a warning, don't kill lockdep. */
7604833794dSThomas Gleixner 	add_taint(taint, LOCKDEP_STILL_OK);
7614833794dSThomas Gleixner 
762a8f18b90SArjan van de Ven 	nbcon_cpu_emergency_exit();
7630f6f49a8SLinus Torvalds }
764525bb813SArnd Bergmann 
7652da1ead4SKees Cook #ifdef CONFIG_BUG
766ee871133SKees Cook #ifndef __WARN_FLAGS
warn_slowpath_fmt(const char * file,int line,unsigned taint,const char * fmt,...)767ee871133SKees Cook void warn_slowpath_fmt(const char *file, int line, unsigned taint,
768b2be0527SBen Hutchings 		       const char *fmt, ...)
7695a5d7e9bSPeter Zijlstra {
7702553b67aSJosh Poimboeuf 	bool rcu = warn_rcu_enter();
771b2be0527SBen Hutchings 	struct warn_args args;
772f2f84b05SKees Cook 
773d38aba49SKees Cook 	pr_warn(CUT_HERE);
774d38aba49SKees Cook 
775f2f84b05SKees Cook 	if (!fmt) {
776f2f84b05SKees Cook 		__warn(file, line, __builtin_return_address(0), taint,
777cccd3281SLukas Wunner 		       NULL, NULL);
778f2f84b05SKees Cook 		warn_rcu_exit(rcu);
779f2f84b05SKees Cook 		return;
780f2f84b05SKees Cook 	}
781b2be0527SBen Hutchings 
782b2be0527SBen Hutchings 	args.fmt = fmt;
7832553b67aSJosh Poimboeuf 	va_start(args.args, fmt);
784b2be0527SBen Hutchings 	__warn(file, line, __builtin_return_address(0), taint, NULL, &args);
7855a5d7e9bSPeter Zijlstra 	va_end(args.args);
786b2be0527SBen Hutchings 	warn_rcu_exit(rcu);
787ee871133SKees Cook }
788a7bed27aSKees Cook EXPORT_SYMBOL(warn_slowpath_fmt);
789a7bed27aSKees Cook #else
__warn_printk(const char * fmt,...)790a7bed27aSKees Cook void __warn_printk(const char *fmt, ...)
7915a5d7e9bSPeter Zijlstra {
792a7bed27aSKees Cook 	bool rcu = warn_rcu_enter();
793a7bed27aSKees Cook 	va_list args;
794a7bed27aSKees Cook 
795a7bed27aSKees Cook 	pr_warn(CUT_HERE);
796a7bed27aSKees Cook 
797a7bed27aSKees Cook 	va_start(args, fmt);
798a7bed27aSKees Cook 	vprintk(fmt, args);
7995a5d7e9bSPeter Zijlstra 	va_end(args);
800a7bed27aSKees Cook 	warn_rcu_exit(rcu);
801a7bed27aSKees Cook }
80279b4cc5eSArjan van de Ven EXPORT_SYMBOL(__warn_printk);
80379b4cc5eSArjan van de Ven #endif
804b1fca27dSAndi Kleen 
805b1fca27dSAndi Kleen /* Support resetting WARN*_ONCE state */
806b1fca27dSAndi Kleen 
clear_warn_once_set(void * data,u64 val)807b1fca27dSAndi Kleen static int clear_warn_once_set(void *data, u64 val)
808aaf5dcfbSAndi Kleen {
809b1fca27dSAndi Kleen 	generic_bug_clear_once();
810b1fca27dSAndi Kleen 	memset(__start_once, 0, __end_once - __start_once);
811b1fca27dSAndi Kleen 	return 0;
812b1fca27dSAndi Kleen }
8134169680eSYueHaibing 
814b1fca27dSAndi Kleen DEFINE_DEBUGFS_ATTRIBUTE(clear_warn_once_fops, NULL, clear_warn_once_set,
815b1fca27dSAndi Kleen 			 "%lld\n");
816b1fca27dSAndi Kleen 
register_warn_debugfs(void)817b1fca27dSAndi Kleen static __init int register_warn_debugfs(void)
818b1fca27dSAndi Kleen {
8194169680eSYueHaibing 	/* Don't care about failure */
8204169680eSYueHaibing 	debugfs_create_file_unsafe("clear_warn_once", 0200, NULL, NULL,
821b1fca27dSAndi Kleen 				   &clear_warn_once_fops);
822b1fca27dSAndi Kleen 	return 0;
823b1fca27dSAndi Kleen }
824b1fca27dSAndi Kleen 
825b1fca27dSAndi Kleen device_initcall(register_warn_debugfs);
826b1fca27dSAndi Kleen #endif
827050e9baaSLinus Torvalds 
82854371a43SArjan van de Ven #ifdef CONFIG_STACKPROTECTOR
8293162f751SArjan van de Ven 
8303162f751SArjan van de Ven /*
8313162f751SArjan van de Ven  * Called when gcc's -fstack-protector feature is used, and
8323162f751SArjan van de Ven  * gcc detects corruption of the on-stack canary value
8335916d5f9SThomas Gleixner  */
__stack_chk_fail(void)8343162f751SArjan van de Ven __visible noinstr void __stack_chk_fail(void)
835*72c774aaSJosh Poimboeuf {
836*72c774aaSJosh Poimboeuf 	unsigned long flags;
8375916d5f9SThomas Gleixner 
838*72c774aaSJosh Poimboeuf 	instrumentation_begin();
839*72c774aaSJosh Poimboeuf 	flags = user_access_save();
84095c4fb78SBorislav Petkov 
841517a92c4SIngo Molnar 	panic("stack-protector: Kernel stack is corrupted in: %pB",
842*72c774aaSJosh Poimboeuf 		__builtin_return_address(0));
843*72c774aaSJosh Poimboeuf 
8445916d5f9SThomas Gleixner 	user_access_restore(flags);
8453162f751SArjan van de Ven 	instrumentation_end();
8463162f751SArjan van de Ven }
84754371a43SArjan van de Ven EXPORT_SYMBOL(__stack_chk_fail);
8483162f751SArjan van de Ven 
849f44dd164SRusty Russell #endif
850f44dd164SRusty Russell 
851d999bd93SFeng Tang core_param(panic, panic_timeout, int, 0644);
852f44dd164SRusty Russell core_param(panic_print, panic_print, ulong, 0644);
8539e3961a0SPrarit Bhargava core_param(pause_on_oops, pause_on_oops, int, 0644);
854b26e27ddSHidehiro Kawai core_param(panic_on_warn, panic_on_warn, int, 0644);
855f06e5153SMasami Hiramatsu core_param(crash_kexec_post_notifiers, crash_kexec_post_notifiers, bool, 0644);
856d404ab0aSOlaf Hering 
oops_setup(char * s)857d404ab0aSOlaf Hering static int __init oops_setup(char *s)
858d404ab0aSOlaf Hering {
859d404ab0aSOlaf Hering 	if (!s)
860d404ab0aSOlaf Hering 		return -EINVAL;
861d404ab0aSOlaf Hering 	if (!strcmp(s, "panic"))
862d404ab0aSOlaf Hering 		panic_on_oops = 1;
863d404ab0aSOlaf Hering 	return 0;
864d404ab0aSOlaf Hering }
865db38d5c1SRafael Aquini early_param("oops", oops_setup);
866db38d5c1SRafael Aquini 
panic_on_taint_setup(char * s)867db38d5c1SRafael Aquini static int __init panic_on_taint_setup(char *s)
868db38d5c1SRafael Aquini {
869db38d5c1SRafael Aquini 	char *taint_str;
870db38d5c1SRafael Aquini 
871db38d5c1SRafael Aquini 	if (!s)
872db38d5c1SRafael Aquini 		return -EINVAL;
873db38d5c1SRafael Aquini 
874db38d5c1SRafael Aquini 	taint_str = strsep(&s, ",");
875db38d5c1SRafael Aquini 	if (kstrtoul(taint_str, 16, &panic_on_taint))
876db38d5c1SRafael Aquini 		return -EINVAL;
877db38d5c1SRafael Aquini 
878db38d5c1SRafael Aquini 	/* make sure panic_on_taint doesn't hold out-of-range TAINT flags */
879db38d5c1SRafael Aquini 	panic_on_taint &= TAINT_FLAGS_MAX;
880db38d5c1SRafael Aquini 
881db38d5c1SRafael Aquini 	if (!panic_on_taint)
882db38d5c1SRafael Aquini 		return -EINVAL;
883db38d5c1SRafael Aquini 
884db38d5c1SRafael Aquini 	if (s && !strcmp(s, "nousertaint"))
885db38d5c1SRafael Aquini 		panic_on_taint_nousertaint = true;
8865d5dd3e4SAndy Shevchenko 
8875d5dd3e4SAndy Shevchenko 	pr_info("panic_on_taint: bitmask=0x%lx nousertaint_mode=%s\n",
888db38d5c1SRafael Aquini 		panic_on_taint, str_enabled_disabled(panic_on_taint_nousertaint));
889db38d5c1SRafael Aquini 
890db38d5c1SRafael Aquini 	return 0;
891db38d5c1SRafael Aquini }
892 early_param("panic_on_taint", panic_on_taint_setup);
893