4 * Copyright (C) 1991, 1992 Linus Torvalds
8 * This function is used through-out the kernel (including mm and fs)
9 * to indicate a major problem.
11 #include <linux/debug_locks.h>
12 #include <linux/interrupt.h>
13 #include <linux/kmsg_dump.h>
14 #include <linux/kallsyms.h>
15 #include <linux/notifier.h>
16 #include <linux/module.h>
17 #include <linux/random.h>
18 #include <linux/ftrace.h>
19 #include <linux/reboot.h>
20 #include <linux/delay.h>
21 #include <linux/kexec.h>
22 #include <linux/sched.h>
23 #include <linux/sysrq.h>
24 #include <linux/init.h>
25 #include <linux/nmi.h>
26 #include <linux/console.h>
28 #define PANIC_TIMER_STEP 100
29 #define PANIC_BLINK_SPD 18
31 int panic_on_oops = CONFIG_PANIC_ON_OOPS_VALUE;
32 static unsigned long tainted_mask;
33 static int pause_on_oops;
34 static int pause_on_oops_flag;
35 static DEFINE_SPINLOCK(pause_on_oops_lock);
36 bool crash_kexec_post_notifiers;
37 int panic_on_warn __read_mostly;
39 int panic_timeout = CONFIG_PANIC_TIMEOUT;
40 EXPORT_SYMBOL_GPL(panic_timeout);
42 ATOMIC_NOTIFIER_HEAD(panic_notifier_list);
44 EXPORT_SYMBOL(panic_notifier_list);
46 static long no_blink(int state)
51 /* Returns how long it waited in ms */
52 long (*panic_blink)(int state);
53 EXPORT_SYMBOL(panic_blink);
56 * Stop ourself in panic -- architecture code may override this
58 void __weak panic_smp_self_stop(void)
65 * Stop ourselves in NMI context if another CPU has already panicked. Arch code
66 * may override this to prepare for crash dumping, e.g. save regs info.
68 void __weak nmi_panic_self_stop(struct pt_regs *regs)
70 panic_smp_self_stop();
73 atomic_t panic_cpu = ATOMIC_INIT(PANIC_CPU_INVALID);
76 * A variant of panic() called from NMI context. We return if we've already
77 * panicked on this CPU. If another CPU already panicked, loop in
78 * nmi_panic_self_stop() which can provide architecture dependent code such
79 * as saving register state for crash dump.
81 void nmi_panic(struct pt_regs *regs, const char *msg)
85 cpu = raw_smp_processor_id();
86 old_cpu = atomic_cmpxchg(&panic_cpu, PANIC_CPU_INVALID, cpu);
88 if (old_cpu == PANIC_CPU_INVALID)
90 else if (old_cpu != cpu)
91 nmi_panic_self_stop(regs);
93 EXPORT_SYMBOL(nmi_panic);
96 * panic - halt the system
97 * @fmt: The text string to print
99 * Display a message, then perform cleanups.
101 * This function never returns.
103 void panic(const char *fmt, ...)
105 static char buf[1024];
109 int old_cpu, this_cpu;
112 * Disable local interrupts. This will prevent panic_smp_self_stop
113 * from deadlocking the first cpu that invokes the panic, since
114 * there is nothing to prevent an interrupt handler (that runs
115 * after setting panic_cpu) from invoking panic() again.
120 * It's possible to come here directly from a panic-assertion and
121 * not have preempt disabled. Some functions called from here want
122 * preempt to be disabled. No point enabling it later though...
124 * Only one CPU is allowed to execute the panic code from here. For
125 * multiple parallel invocations of panic, all other CPUs either
126 * stop themself or will wait until they are stopped by the 1st CPU
127 * with smp_send_stop().
129 * `old_cpu == PANIC_CPU_INVALID' means this is the 1st CPU which
130 * comes here, so go ahead.
131 * `old_cpu == this_cpu' means we came from nmi_panic() which sets
132 * panic_cpu to this CPU. In this case, this is also the 1st CPU.
134 this_cpu = raw_smp_processor_id();
135 old_cpu = atomic_cmpxchg(&panic_cpu, PANIC_CPU_INVALID, this_cpu);
137 if (old_cpu != PANIC_CPU_INVALID && old_cpu != this_cpu)
138 panic_smp_self_stop();
143 vsnprintf(buf, sizeof(buf), fmt, args);
145 pr_emerg("Kernel panic - not syncing: %s\n", buf);
146 #ifdef CONFIG_DEBUG_BUGVERBOSE
148 * Avoid nested stack-dumping if a panic occurs during oops processing
150 if (!test_taint(TAINT_DIE) && oops_in_progress <= 1)
155 * If we have crashed and we have a crash kernel loaded let it handle
157 * If we want to run this after calling panic_notifiers, pass
158 * the "crash_kexec_post_notifiers" option to the kernel.
160 if (!crash_kexec_post_notifiers)
164 * Note smp_send_stop is the usual smp shutdown function, which
165 * unfortunately means it may not be hardened to work in a panic
171 * Run any panic handlers, including those that might need to
172 * add information to the kmsg dump output.
174 atomic_notifier_call_chain(&panic_notifier_list, 0, buf);
176 kmsg_dump(KMSG_DUMP_PANIC);
179 * If you doubt kdump always works fine in any situation,
180 * "crash_kexec_post_notifiers" offers you a chance to run
181 * panic_notifiers and dumping kmsg before kdump.
182 * Note: since some panic_notifiers can make crashed kernel
183 * more unstable, it can increase risks of the kdump failure too.
185 if (crash_kexec_post_notifiers)
191 * We may have ended up stopping the CPU holding the lock (in
192 * smp_send_stop()) while still having some valuable data in the console
193 * buffer. Try to acquire the lock then release it regardless of the
194 * result. The release will also print the buffers out. Locks debug
195 * should be disabled to avoid reporting bad unlock balance when
196 * panic() is not being callled from OOPS.
199 console_flush_on_panic();
202 panic_blink = no_blink;
204 if (panic_timeout > 0) {
206 * Delay timeout seconds before rebooting the machine.
207 * We can't use the "normal" timers since we just panicked.
209 pr_emerg("Rebooting in %d seconds..", panic_timeout);
211 for (i = 0; i < panic_timeout * 1000; i += PANIC_TIMER_STEP) {
212 touch_nmi_watchdog();
214 i += panic_blink(state ^= 1);
215 i_next = i + 3600 / PANIC_BLINK_SPD;
217 mdelay(PANIC_TIMER_STEP);
220 if (panic_timeout != 0) {
222 * This will not be a clean reboot, with everything
223 * shutting down. But if there is a chance of
224 * rebooting the system it will be rebooted.
230 extern int stop_a_enabled;
231 /* Make sure the user can actually press Stop-A (L1-A) */
233 pr_emerg("Press Stop-A (L1-A) to return to the boot prom\n");
236 #if defined(CONFIG_S390)
238 unsigned long caller;
240 caller = (unsigned long)__builtin_return_address(0);
241 disabled_wait(caller);
244 pr_emerg("---[ end Kernel panic - not syncing: %s\n", buf);
246 for (i = 0; ; i += PANIC_TIMER_STEP) {
247 touch_softlockup_watchdog();
249 i += panic_blink(state ^= 1);
250 i_next = i + 3600 / PANIC_BLINK_SPD;
252 mdelay(PANIC_TIMER_STEP);
256 EXPORT_SYMBOL(panic);
265 static const struct tnt tnts[] = {
266 { TAINT_PROPRIETARY_MODULE, 'P', 'G' },
267 { TAINT_FORCED_MODULE, 'F', ' ' },
268 { TAINT_CPU_OUT_OF_SPEC, 'S', ' ' },
269 { TAINT_FORCED_RMMOD, 'R', ' ' },
270 { TAINT_MACHINE_CHECK, 'M', ' ' },
271 { TAINT_BAD_PAGE, 'B', ' ' },
272 { TAINT_USER, 'U', ' ' },
273 { TAINT_DIE, 'D', ' ' },
274 { TAINT_OVERRIDDEN_ACPI_TABLE, 'A', ' ' },
275 { TAINT_WARN, 'W', ' ' },
276 { TAINT_CRAP, 'C', ' ' },
277 { TAINT_FIRMWARE_WORKAROUND, 'I', ' ' },
278 { TAINT_OOT_MODULE, 'O', ' ' },
279 { TAINT_UNSIGNED_MODULE, 'E', ' ' },
280 { TAINT_SOFTLOCKUP, 'L', ' ' },
281 { TAINT_LIVEPATCH, 'K', ' ' },
285 * print_tainted - return a string to represent the kernel taint state.
287 * 'P' - Proprietary module has been loaded.
288 * 'F' - Module has been forcibly loaded.
289 * 'S' - SMP with CPUs not designed for SMP.
290 * 'R' - User forced a module unload.
291 * 'M' - System experienced a machine check exception.
292 * 'B' - System has hit bad_page.
293 * 'U' - Userspace-defined naughtiness.
294 * 'D' - Kernel has oopsed before
295 * 'A' - ACPI table overridden.
296 * 'W' - Taint on warning.
297 * 'C' - modules from drivers/staging are loaded.
298 * 'I' - Working around severe firmware bug.
299 * 'O' - Out-of-tree module has been loaded.
300 * 'E' - Unsigned module has been loaded.
301 * 'L' - A soft lockup has previously occurred.
302 * 'K' - Kernel has been live patched.
304 * The string is overwritten by the next call to print_tainted().
306 const char *print_tainted(void)
308 static char buf[ARRAY_SIZE(tnts) + sizeof("Tainted: ")];
314 s = buf + sprintf(buf, "Tainted: ");
315 for (i = 0; i < ARRAY_SIZE(tnts); i++) {
316 const struct tnt *t = &tnts[i];
317 *s++ = test_bit(t->bit, &tainted_mask) ?
322 snprintf(buf, sizeof(buf), "Not tainted");
327 int test_taint(unsigned flag)
329 return test_bit(flag, &tainted_mask);
331 EXPORT_SYMBOL(test_taint);
333 unsigned long get_taint(void)
339 * add_taint: add a taint flag if not already set.
340 * @flag: one of the TAINT_* constants.
341 * @lockdep_ok: whether lock debugging is still OK.
343 * If something bad has gone wrong, you'll want @lockdebug_ok = false, but for
344 * some notewortht-but-not-corrupting cases, it can be set to true.
346 void add_taint(unsigned flag, enum lockdep_ok lockdep_ok)
348 if (lockdep_ok == LOCKDEP_NOW_UNRELIABLE && __debug_locks_off())
349 pr_warn("Disabling lock debugging due to kernel taint\n");
351 set_bit(flag, &tainted_mask);
353 EXPORT_SYMBOL(add_taint);
355 static void spin_msec(int msecs)
359 for (i = 0; i < msecs; i++) {
360 touch_nmi_watchdog();
366 * It just happens that oops_enter() and oops_exit() are identically
369 static void do_oops_enter_exit(void)
372 static int spin_counter;
377 spin_lock_irqsave(&pause_on_oops_lock, flags);
378 if (pause_on_oops_flag == 0) {
379 /* This CPU may now print the oops message */
380 pause_on_oops_flag = 1;
382 /* We need to stall this CPU */
384 /* This CPU gets to do the counting */
385 spin_counter = pause_on_oops;
387 spin_unlock(&pause_on_oops_lock);
388 spin_msec(MSEC_PER_SEC);
389 spin_lock(&pause_on_oops_lock);
390 } while (--spin_counter);
391 pause_on_oops_flag = 0;
393 /* This CPU waits for a different one */
394 while (spin_counter) {
395 spin_unlock(&pause_on_oops_lock);
397 spin_lock(&pause_on_oops_lock);
401 spin_unlock_irqrestore(&pause_on_oops_lock, flags);
405 * Return true if the calling CPU is allowed to print oops-related info.
406 * This is a bit racy..
408 int oops_may_print(void)
410 return pause_on_oops_flag == 0;
414 * Called when the architecture enters its oops handler, before it prints
415 * anything. If this is the first CPU to oops, and it's oopsing the first
416 * time then let it proceed.
418 * This is all enabled by the pause_on_oops kernel boot option. We do all
419 * this to ensure that oopses don't scroll off the screen. It has the
420 * side-effect of preventing later-oopsing CPUs from mucking up the display,
423 * It turns out that the CPU which is allowed to print ends up pausing for
424 * the right duration, whereas all the other CPUs pause for twice as long:
425 * once in oops_enter(), once in oops_exit().
427 void oops_enter(void)
430 /* can't trust the integrity of the kernel anymore: */
432 do_oops_enter_exit();
436 * 64-bit random ID for oopses:
440 static int init_oops_id(void)
442 #ifndef CONFIG_PREEMPT_RT_FULL
444 get_random_bytes(&oops_id, sizeof(oops_id));
451 late_initcall(init_oops_id);
453 void print_oops_end_marker(void)
456 pr_warn("---[ end trace %016llx ]---\n", (unsigned long long)oops_id);
460 * Called when the architecture exits its oops handler, after printing
465 do_oops_enter_exit();
466 print_oops_end_marker();
467 kmsg_dump(KMSG_DUMP_OOPS);
470 #ifdef WANT_WARN_ON_SLOWPATH
471 struct slowpath_args {
476 static void warn_slowpath_common(const char *file, int line, void *caller,
477 unsigned taint, struct slowpath_args *args)
479 disable_trace_on_warning();
481 pr_warn("------------[ cut here ]------------\n");
482 pr_warn("WARNING: CPU: %d PID: %d at %s:%d %pS()\n",
483 raw_smp_processor_id(), current->pid, file, line, caller);
486 vprintk(args->fmt, args->args);
490 * This thread may hit another WARN() in the panic path.
491 * Resetting this prevents additional WARN() from panicking the
492 * system on this thread. Other threads are blocked by the
493 * panic_mutex in panic().
496 panic("panic_on_warn set ...\n");
501 print_oops_end_marker();
502 /* Just a warning, don't kill lockdep. */
503 add_taint(taint, LOCKDEP_STILL_OK);
506 void warn_slowpath_fmt(const char *file, int line, const char *fmt, ...)
508 struct slowpath_args args;
511 va_start(args.args, fmt);
512 warn_slowpath_common(file, line, __builtin_return_address(0),
516 EXPORT_SYMBOL(warn_slowpath_fmt);
518 void warn_slowpath_fmt_taint(const char *file, int line,
519 unsigned taint, const char *fmt, ...)
521 struct slowpath_args args;
524 va_start(args.args, fmt);
525 warn_slowpath_common(file, line, __builtin_return_address(0),
529 EXPORT_SYMBOL(warn_slowpath_fmt_taint);
531 void warn_slowpath_null(const char *file, int line)
533 warn_slowpath_common(file, line, __builtin_return_address(0),
536 EXPORT_SYMBOL(warn_slowpath_null);
539 #ifdef CONFIG_CC_STACKPROTECTOR
542 * Called when gcc's -fstack-protector feature is used, and
543 * gcc detects corruption of the on-stack canary value
545 __visible void __stack_chk_fail(void)
547 panic("stack-protector: Kernel stack is corrupted in: %p\n",
548 __builtin_return_address(0));
550 EXPORT_SYMBOL(__stack_chk_fail);
554 core_param(panic, panic_timeout, int, 0644);
555 core_param(pause_on_oops, pause_on_oops, int, 0644);
556 core_param(panic_on_warn, panic_on_warn, int, 0644);
558 static int __init setup_crash_kexec_post_notifiers(char *s)
560 crash_kexec_post_notifiers = true;
563 early_param("crash_kexec_post_notifiers", setup_crash_kexec_post_notifiers);
565 static int __init oops_setup(char *s)
569 if (!strcmp(s, "panic"))
573 early_param("oops", oops_setup);