xref: /linux-6.15/kernel/panic.c (revision bcdcd8e7)
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  */
111da177e4SLinus Torvalds #include <linux/module.h>
121da177e4SLinus Torvalds #include <linux/sched.h>
131da177e4SLinus Torvalds #include <linux/delay.h>
141da177e4SLinus Torvalds #include <linux/reboot.h>
151da177e4SLinus Torvalds #include <linux/notifier.h>
161da177e4SLinus Torvalds #include <linux/init.h>
171da177e4SLinus Torvalds #include <linux/sysrq.h>
181da177e4SLinus Torvalds #include <linux/interrupt.h>
191da177e4SLinus Torvalds #include <linux/nmi.h>
20dc009d92SEric W. Biederman #include <linux/kexec.h>
21657b3010SAndrew Morton #include <linux/debug_locks.h>
221da177e4SLinus Torvalds 
231da177e4SLinus Torvalds int panic_on_oops;
241da177e4SLinus Torvalds int tainted;
25dd287796SAndrew Morton static int pause_on_oops;
26dd287796SAndrew Morton static int pause_on_oops_flag;
27dd287796SAndrew Morton static DEFINE_SPINLOCK(pause_on_oops_lock);
281da177e4SLinus Torvalds 
29dd287796SAndrew Morton int panic_timeout;
301da177e4SLinus Torvalds 
31e041c683SAlan Stern ATOMIC_NOTIFIER_HEAD(panic_notifier_list);
321da177e4SLinus Torvalds 
331da177e4SLinus Torvalds EXPORT_SYMBOL(panic_notifier_list);
341da177e4SLinus Torvalds 
351da177e4SLinus Torvalds static int __init panic_setup(char *str)
361da177e4SLinus Torvalds {
371da177e4SLinus Torvalds 	panic_timeout = simple_strtoul(str, NULL, 0);
381da177e4SLinus Torvalds 	return 1;
391da177e4SLinus Torvalds }
401da177e4SLinus Torvalds __setup("panic=", panic_setup);
411da177e4SLinus Torvalds 
421da177e4SLinus Torvalds static long no_blink(long time)
431da177e4SLinus Torvalds {
441da177e4SLinus Torvalds 	return 0;
451da177e4SLinus Torvalds }
461da177e4SLinus Torvalds 
471da177e4SLinus Torvalds /* Returns how long it waited in ms */
481da177e4SLinus Torvalds long (*panic_blink)(long time);
491da177e4SLinus Torvalds EXPORT_SYMBOL(panic_blink);
501da177e4SLinus Torvalds 
511da177e4SLinus Torvalds /**
521da177e4SLinus Torvalds  *	panic - halt the system
531da177e4SLinus Torvalds  *	@fmt: The text string to print
541da177e4SLinus Torvalds  *
551da177e4SLinus Torvalds  *	Display a message, then perform cleanups.
561da177e4SLinus Torvalds  *
571da177e4SLinus Torvalds  *	This function never returns.
581da177e4SLinus Torvalds  */
591da177e4SLinus Torvalds 
601da177e4SLinus Torvalds NORET_TYPE void panic(const char * fmt, ...)
611da177e4SLinus Torvalds {
621da177e4SLinus Torvalds 	long i;
631da177e4SLinus Torvalds 	static char buf[1024];
641da177e4SLinus Torvalds 	va_list args;
65347a8dc3SMartin Schwidefsky #if defined(CONFIG_S390)
661da177e4SLinus Torvalds         unsigned long caller = (unsigned long) __builtin_return_address(0);
671da177e4SLinus Torvalds #endif
681da177e4SLinus Torvalds 
69dc009d92SEric W. Biederman 	/*
70dc009d92SEric W. Biederman 	 * It's possible to come here directly from a panic-assertion and not
71dc009d92SEric W. Biederman 	 * have preempt disabled. Some functions called from here want
72dc009d92SEric W. Biederman 	 * preempt to be disabled. No point enabling it later though...
73dc009d92SEric W. Biederman 	 */
74dc009d92SEric W. Biederman 	preempt_disable();
75dc009d92SEric W. Biederman 
761da177e4SLinus Torvalds 	bust_spinlocks(1);
771da177e4SLinus Torvalds 	va_start(args, fmt);
781da177e4SLinus Torvalds 	vsnprintf(buf, sizeof(buf), fmt, args);
791da177e4SLinus Torvalds 	va_end(args);
801da177e4SLinus Torvalds 	printk(KERN_EMERG "Kernel panic - not syncing: %s\n",buf);
811da177e4SLinus Torvalds 	bust_spinlocks(0);
821da177e4SLinus Torvalds 
83dc009d92SEric W. Biederman 	/*
84dc009d92SEric W. Biederman 	 * If we have crashed and we have a crash kernel loaded let it handle
85dc009d92SEric W. Biederman 	 * everything else.
86dc009d92SEric W. Biederman 	 * Do we want to call this before we try to display a message?
87dc009d92SEric W. Biederman 	 */
886e274d14SAlexander Nyberg 	crash_kexec(NULL);
89dc009d92SEric W. Biederman 
901da177e4SLinus Torvalds #ifdef CONFIG_SMP
91dc009d92SEric W. Biederman 	/*
92dc009d92SEric W. Biederman 	 * Note smp_send_stop is the usual smp shutdown function, which
93dc009d92SEric W. Biederman 	 * unfortunately means it may not be hardened to work in a panic
94dc009d92SEric W. Biederman 	 * situation.
95dc009d92SEric W. Biederman 	 */
961da177e4SLinus Torvalds 	smp_send_stop();
971da177e4SLinus Torvalds #endif
981da177e4SLinus Torvalds 
99e041c683SAlan Stern 	atomic_notifier_call_chain(&panic_notifier_list, 0, buf);
1001da177e4SLinus Torvalds 
1011da177e4SLinus Torvalds 	if (!panic_blink)
1021da177e4SLinus Torvalds 		panic_blink = no_blink;
1031da177e4SLinus Torvalds 
104dc009d92SEric W. Biederman 	if (panic_timeout > 0) {
1051da177e4SLinus Torvalds 		/*
1061da177e4SLinus Torvalds 	 	 * Delay timeout seconds before rebooting the machine.
1071da177e4SLinus Torvalds 		 * We can't use the "normal" timers since we just panicked..
1081da177e4SLinus Torvalds 	 	 */
1091da177e4SLinus Torvalds 		printk(KERN_EMERG "Rebooting in %d seconds..",panic_timeout);
1101da177e4SLinus Torvalds 		for (i = 0; i < panic_timeout*1000; ) {
1111da177e4SLinus Torvalds 			touch_nmi_watchdog();
1121da177e4SLinus Torvalds 			i += panic_blink(i);
1131da177e4SLinus Torvalds 			mdelay(1);
1141da177e4SLinus Torvalds 			i++;
1151da177e4SLinus Torvalds 		}
1162f048ea8SEric W. Biederman 		/*	This will not be a clean reboot, with everything
1172f048ea8SEric W. Biederman 		 *	shutting down.  But if there is a chance of
1182f048ea8SEric W. Biederman 		 *	rebooting the system it will be rebooted.
1191da177e4SLinus Torvalds 		 */
1202f048ea8SEric W. Biederman 		emergency_restart();
1211da177e4SLinus Torvalds 	}
1221da177e4SLinus Torvalds #ifdef __sparc__
1231da177e4SLinus Torvalds 	{
1241da177e4SLinus Torvalds 		extern int stop_a_enabled;
125a271c241STom 'spot' Callaway 		/* Make sure the user can actually press Stop-A (L1-A) */
1261da177e4SLinus Torvalds 		stop_a_enabled = 1;
127a271c241STom 'spot' Callaway 		printk(KERN_EMERG "Press Stop-A (L1-A) to return to the boot prom\n");
1281da177e4SLinus Torvalds 	}
1291da177e4SLinus Torvalds #endif
130347a8dc3SMartin Schwidefsky #if defined(CONFIG_S390)
1311da177e4SLinus Torvalds         disabled_wait(caller);
1321da177e4SLinus Torvalds #endif
1331da177e4SLinus Torvalds 	local_irq_enable();
1341da177e4SLinus Torvalds 	for (i = 0;;) {
135c22db941SJan Beulich 		touch_softlockup_watchdog();
1361da177e4SLinus Torvalds 		i += panic_blink(i);
1371da177e4SLinus Torvalds 		mdelay(1);
1381da177e4SLinus Torvalds 		i++;
1391da177e4SLinus Torvalds 	}
1401da177e4SLinus Torvalds }
1411da177e4SLinus Torvalds 
1421da177e4SLinus Torvalds EXPORT_SYMBOL(panic);
1431da177e4SLinus Torvalds 
1441da177e4SLinus Torvalds /**
1451da177e4SLinus Torvalds  *	print_tainted - return a string to represent the kernel taint state.
1461da177e4SLinus Torvalds  *
1471da177e4SLinus Torvalds  *  'P' - Proprietary module has been loaded.
1481da177e4SLinus Torvalds  *  'F' - Module has been forcibly loaded.
1491da177e4SLinus Torvalds  *  'S' - SMP with CPUs not designed for SMP.
1501da177e4SLinus Torvalds  *  'R' - User forced a module unload.
1511da177e4SLinus Torvalds  *  'M' - Machine had a machine check experience.
1521da177e4SLinus Torvalds  *  'B' - System has hit bad_page.
15334f5a398STheodore Ts'o  *  'U' - Userspace-defined naughtiness.
1541da177e4SLinus Torvalds  *
1551da177e4SLinus Torvalds  *	The string is overwritten by the next call to print_taint().
1561da177e4SLinus Torvalds  */
1571da177e4SLinus Torvalds 
1581da177e4SLinus Torvalds const char *print_tainted(void)
1591da177e4SLinus Torvalds {
1601da177e4SLinus Torvalds 	static char buf[20];
1611da177e4SLinus Torvalds 	if (tainted) {
162*bcdcd8e7SPavel Emelianov 		snprintf(buf, sizeof(buf), "Tainted: %c%c%c%c%c%c%c%c",
1631da177e4SLinus Torvalds 			tainted & TAINT_PROPRIETARY_MODULE ? 'P' : 'G',
1641da177e4SLinus Torvalds 			tainted & TAINT_FORCED_MODULE ? 'F' : ' ',
1651da177e4SLinus Torvalds 			tainted & TAINT_UNSAFE_SMP ? 'S' : ' ',
1661da177e4SLinus Torvalds 			tainted & TAINT_FORCED_RMMOD ? 'R' : ' ',
1671da177e4SLinus Torvalds  			tainted & TAINT_MACHINE_CHECK ? 'M' : ' ',
16834f5a398STheodore Ts'o 			tainted & TAINT_BAD_PAGE ? 'B' : ' ',
169*bcdcd8e7SPavel Emelianov 			tainted & TAINT_USER ? 'U' : ' ',
170*bcdcd8e7SPavel Emelianov 			tainted & TAINT_DIE ? 'D' : ' ');
1711da177e4SLinus Torvalds 	}
1721da177e4SLinus Torvalds 	else
1731da177e4SLinus Torvalds 		snprintf(buf, sizeof(buf), "Not tainted");
1741da177e4SLinus Torvalds 	return(buf);
1751da177e4SLinus Torvalds }
1761da177e4SLinus Torvalds 
1771da177e4SLinus Torvalds void add_taint(unsigned flag)
1781da177e4SLinus Torvalds {
179068c4579SIngo Molnar 	debug_locks = 0; /* can't trust the integrity of the kernel anymore */
1801da177e4SLinus Torvalds 	tainted |= flag;
1811da177e4SLinus Torvalds }
1821da177e4SLinus Torvalds EXPORT_SYMBOL(add_taint);
183dd287796SAndrew Morton 
184dd287796SAndrew Morton static int __init pause_on_oops_setup(char *str)
185dd287796SAndrew Morton {
186dd287796SAndrew Morton 	pause_on_oops = simple_strtoul(str, NULL, 0);
187dd287796SAndrew Morton 	return 1;
188dd287796SAndrew Morton }
189dd287796SAndrew Morton __setup("pause_on_oops=", pause_on_oops_setup);
190dd287796SAndrew Morton 
191dd287796SAndrew Morton static void spin_msec(int msecs)
192dd287796SAndrew Morton {
193dd287796SAndrew Morton 	int i;
194dd287796SAndrew Morton 
195dd287796SAndrew Morton 	for (i = 0; i < msecs; i++) {
196dd287796SAndrew Morton 		touch_nmi_watchdog();
197dd287796SAndrew Morton 		mdelay(1);
198dd287796SAndrew Morton 	}
199dd287796SAndrew Morton }
200dd287796SAndrew Morton 
201dd287796SAndrew Morton /*
202dd287796SAndrew Morton  * It just happens that oops_enter() and oops_exit() are identically
203dd287796SAndrew Morton  * implemented...
204dd287796SAndrew Morton  */
205dd287796SAndrew Morton static void do_oops_enter_exit(void)
206dd287796SAndrew Morton {
207dd287796SAndrew Morton 	unsigned long flags;
208dd287796SAndrew Morton 	static int spin_counter;
209dd287796SAndrew Morton 
210dd287796SAndrew Morton 	if (!pause_on_oops)
211dd287796SAndrew Morton 		return;
212dd287796SAndrew Morton 
213dd287796SAndrew Morton 	spin_lock_irqsave(&pause_on_oops_lock, flags);
214dd287796SAndrew Morton 	if (pause_on_oops_flag == 0) {
215dd287796SAndrew Morton 		/* This CPU may now print the oops message */
216dd287796SAndrew Morton 		pause_on_oops_flag = 1;
217dd287796SAndrew Morton 	} else {
218dd287796SAndrew Morton 		/* We need to stall this CPU */
219dd287796SAndrew Morton 		if (!spin_counter) {
220dd287796SAndrew Morton 			/* This CPU gets to do the counting */
221dd287796SAndrew Morton 			spin_counter = pause_on_oops;
222dd287796SAndrew Morton 			do {
223dd287796SAndrew Morton 				spin_unlock(&pause_on_oops_lock);
224dd287796SAndrew Morton 				spin_msec(MSEC_PER_SEC);
225dd287796SAndrew Morton 				spin_lock(&pause_on_oops_lock);
226dd287796SAndrew Morton 			} while (--spin_counter);
227dd287796SAndrew Morton 			pause_on_oops_flag = 0;
228dd287796SAndrew Morton 		} else {
229dd287796SAndrew Morton 			/* This CPU waits for a different one */
230dd287796SAndrew Morton 			while (spin_counter) {
231dd287796SAndrew Morton 				spin_unlock(&pause_on_oops_lock);
232dd287796SAndrew Morton 				spin_msec(1);
233dd287796SAndrew Morton 				spin_lock(&pause_on_oops_lock);
234dd287796SAndrew Morton 			}
235dd287796SAndrew Morton 		}
236dd287796SAndrew Morton 	}
237dd287796SAndrew Morton 	spin_unlock_irqrestore(&pause_on_oops_lock, flags);
238dd287796SAndrew Morton }
239dd287796SAndrew Morton 
240dd287796SAndrew Morton /*
241dd287796SAndrew Morton  * Return true if the calling CPU is allowed to print oops-related info.  This
242dd287796SAndrew Morton  * is a bit racy..
243dd287796SAndrew Morton  */
244dd287796SAndrew Morton int oops_may_print(void)
245dd287796SAndrew Morton {
246dd287796SAndrew Morton 	return pause_on_oops_flag == 0;
247dd287796SAndrew Morton }
248dd287796SAndrew Morton 
249dd287796SAndrew Morton /*
250dd287796SAndrew Morton  * Called when the architecture enters its oops handler, before it prints
251dd287796SAndrew Morton  * anything.  If this is the first CPU to oops, and it's oopsing the first time
252dd287796SAndrew Morton  * then let it proceed.
253dd287796SAndrew Morton  *
254dd287796SAndrew Morton  * This is all enabled by the pause_on_oops kernel boot option.  We do all this
255dd287796SAndrew Morton  * to ensure that oopses don't scroll off the screen.  It has the side-effect
256dd287796SAndrew Morton  * of preventing later-oopsing CPUs from mucking up the display, too.
257dd287796SAndrew Morton  *
258dd287796SAndrew Morton  * It turns out that the CPU which is allowed to print ends up pausing for the
259dd287796SAndrew Morton  * right duration, whereas all the other CPUs pause for twice as long: once in
260dd287796SAndrew Morton  * oops_enter(), once in oops_exit().
261dd287796SAndrew Morton  */
262dd287796SAndrew Morton void oops_enter(void)
263dd287796SAndrew Morton {
2642c16e9c8SArjan van de Ven 	debug_locks_off(); /* can't trust the integrity of the kernel anymore */
265dd287796SAndrew Morton 	do_oops_enter_exit();
266dd287796SAndrew Morton }
267dd287796SAndrew Morton 
268dd287796SAndrew Morton /*
269dd287796SAndrew Morton  * Called when the architecture exits its oops handler, after printing
270dd287796SAndrew Morton  * everything.
271dd287796SAndrew Morton  */
272dd287796SAndrew Morton void oops_exit(void)
273dd287796SAndrew Morton {
274dd287796SAndrew Morton 	do_oops_enter_exit();
275dd287796SAndrew Morton }
2763162f751SArjan van de Ven 
2773162f751SArjan van de Ven #ifdef CONFIG_CC_STACKPROTECTOR
2783162f751SArjan van de Ven /*
2793162f751SArjan van de Ven  * Called when gcc's -fstack-protector feature is used, and
2803162f751SArjan van de Ven  * gcc detects corruption of the on-stack canary value
2813162f751SArjan van de Ven  */
2823162f751SArjan van de Ven void __stack_chk_fail(void)
2833162f751SArjan van de Ven {
2843162f751SArjan van de Ven 	panic("stack-protector: Kernel stack is corrupted");
2853162f751SArjan van de Ven }
2863162f751SArjan van de Ven EXPORT_SYMBOL(__stack_chk_fail);
2873162f751SArjan van de Ven #endif
288