Linux 3.16.56
[linux/fpc-iii.git] / kernel / panic.c
blob4de988c2aaec3253f9fbfc40be5c5dcf3ff4d568
1 /*
2 * linux/kernel/panic.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
7 /*
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 static bool crash_kexec_post_notifiers;
38 int panic_timeout = CONFIG_PANIC_TIMEOUT;
39 EXPORT_SYMBOL_GPL(panic_timeout);
41 ATOMIC_NOTIFIER_HEAD(panic_notifier_list);
43 EXPORT_SYMBOL(panic_notifier_list);
45 static long no_blink(int state)
47 return 0;
50 /* Returns how long it waited in ms */
51 long (*panic_blink)(int state);
52 EXPORT_SYMBOL(panic_blink);
55 * Stop ourself in panic -- architecture code may override this
57 void __weak panic_smp_self_stop(void)
59 while (1)
60 cpu_relax();
64 * Stop other CPUs in panic. Architecture dependent code may override this
65 * with more suitable version. For example, if the architecture supports
66 * crash dump, it should save registers of each stopped CPU and disable
67 * per-CPU features such as virtualization extensions.
69 void __weak crash_smp_send_stop(void)
71 static int cpus_stopped;
74 * This function can be called twice in panic path, but obviously
75 * we execute this only once.
77 if (cpus_stopped)
78 return;
81 * Note smp_send_stop is the usual smp shutdown function, which
82 * unfortunately means it may not be hardened to work in a panic
83 * situation.
85 smp_send_stop();
86 cpus_stopped = 1;
89 /**
90 * panic - halt the system
91 * @fmt: The text string to print
93 * Display a message, then perform cleanups.
95 * This function never returns.
97 void panic(const char *fmt, ...)
99 static DEFINE_SPINLOCK(panic_lock);
100 static char buf[1024];
101 va_list args;
102 long i, i_next = 0;
103 int state = 0;
106 * Disable local interrupts. This will prevent panic_smp_self_stop
107 * from deadlocking the first cpu that invokes the panic, since
108 * there is nothing to prevent an interrupt handler (that runs
109 * after the panic_lock is acquired) from invoking panic again.
111 local_irq_disable();
114 * It's possible to come here directly from a panic-assertion and
115 * not have preempt disabled. Some functions called from here want
116 * preempt to be disabled. No point enabling it later though...
118 * Only one CPU is allowed to execute the panic code from here. For
119 * multiple parallel invocations of panic, all other CPUs either
120 * stop themself or will wait until they are stopped by the 1st CPU
121 * with smp_send_stop().
123 if (!spin_trylock(&panic_lock))
124 panic_smp_self_stop();
126 console_verbose();
127 bust_spinlocks(1);
128 va_start(args, fmt);
129 vsnprintf(buf, sizeof(buf), fmt, args);
130 va_end(args);
131 pr_emerg("Kernel panic - not syncing: %s\n", buf);
132 #ifdef CONFIG_DEBUG_BUGVERBOSE
134 * Avoid nested stack-dumping if a panic occurs during oops processing
136 if (!test_taint(TAINT_DIE) && oops_in_progress <= 1)
137 dump_stack();
138 #endif
141 * If we have crashed and we have a crash kernel loaded let it handle
142 * everything else.
143 * If we want to run this after calling panic_notifiers, pass
144 * the "crash_kexec_post_notifiers" option to the kernel.
146 if (!crash_kexec_post_notifiers) {
147 crash_kexec(NULL);
150 * Note smp_send_stop is the usual smp shutdown function, which
151 * unfortunately means it may not be hardened to work in a
152 * panic situation.
154 smp_send_stop();
155 } else {
157 * If we want to do crash dump after notifier calls and
158 * kmsg_dump, we will need architecture dependent extra
159 * works in addition to stopping other CPUs.
161 crash_smp_send_stop();
165 * Run any panic handlers, including those that might need to
166 * add information to the kmsg dump output.
168 atomic_notifier_call_chain(&panic_notifier_list, 0, buf);
170 kmsg_dump(KMSG_DUMP_PANIC);
173 * If you doubt kdump always works fine in any situation,
174 * "crash_kexec_post_notifiers" offers you a chance to run
175 * panic_notifiers and dumping kmsg before kdump.
176 * Note: since some panic_notifiers can make crashed kernel
177 * more unstable, it can increase risks of the kdump failure too.
179 crash_kexec(NULL);
181 bust_spinlocks(0);
184 * We may have ended up stopping the CPU holding the lock (in
185 * smp_send_stop()) while still having some valuable data in the console
186 * buffer. Try to acquire the lock then release it regardless of the
187 * result. The release will also print the buffers out. Locks debug
188 * should be disabled to avoid reporting bad unlock balance when
189 * panic() is not being callled from OOPS.
191 debug_locks_off();
192 console_flush_on_panic();
194 if (!panic_blink)
195 panic_blink = no_blink;
197 if (panic_timeout > 0) {
199 * Delay timeout seconds before rebooting the machine.
200 * We can't use the "normal" timers since we just panicked.
202 pr_emerg("Rebooting in %d seconds..", panic_timeout);
204 for (i = 0; i < panic_timeout * 1000; i += PANIC_TIMER_STEP) {
205 touch_nmi_watchdog();
206 if (i >= i_next) {
207 i += panic_blink(state ^= 1);
208 i_next = i + 3600 / PANIC_BLINK_SPD;
210 mdelay(PANIC_TIMER_STEP);
213 if (panic_timeout != 0) {
215 * This will not be a clean reboot, with everything
216 * shutting down. But if there is a chance of
217 * rebooting the system it will be rebooted.
219 emergency_restart();
221 #ifdef __sparc__
223 extern int stop_a_enabled;
224 /* Make sure the user can actually press Stop-A (L1-A) */
225 stop_a_enabled = 1;
226 pr_emerg("Press Stop-A (L1-A) to return to the boot prom\n");
228 #endif
229 #if defined(CONFIG_S390)
231 unsigned long caller;
233 caller = (unsigned long)__builtin_return_address(0);
234 disabled_wait(caller);
236 #endif
237 pr_emerg("---[ end Kernel panic - not syncing: %s\n", buf);
238 local_irq_enable();
239 for (i = 0; ; i += PANIC_TIMER_STEP) {
240 touch_softlockup_watchdog();
241 if (i >= i_next) {
242 i += panic_blink(state ^= 1);
243 i_next = i + 3600 / PANIC_BLINK_SPD;
245 mdelay(PANIC_TIMER_STEP);
249 EXPORT_SYMBOL(panic);
252 struct tnt {
253 u8 bit;
254 char true;
255 char false;
258 static const struct tnt tnts[] = {
259 { TAINT_PROPRIETARY_MODULE, 'P', 'G' },
260 { TAINT_FORCED_MODULE, 'F', ' ' },
261 { TAINT_CPU_OUT_OF_SPEC, 'S', ' ' },
262 { TAINT_FORCED_RMMOD, 'R', ' ' },
263 { TAINT_MACHINE_CHECK, 'M', ' ' },
264 { TAINT_BAD_PAGE, 'B', ' ' },
265 { TAINT_USER, 'U', ' ' },
266 { TAINT_DIE, 'D', ' ' },
267 { TAINT_OVERRIDDEN_ACPI_TABLE, 'A', ' ' },
268 { TAINT_WARN, 'W', ' ' },
269 { TAINT_CRAP, 'C', ' ' },
270 { TAINT_FIRMWARE_WORKAROUND, 'I', ' ' },
271 { TAINT_OOT_MODULE, 'O', ' ' },
272 { TAINT_UNSIGNED_MODULE, 'E', ' ' },
276 * print_tainted - return a string to represent the kernel taint state.
278 * 'P' - Proprietary module has been loaded.
279 * 'F' - Module has been forcibly loaded.
280 * 'S' - SMP with CPUs not designed for SMP.
281 * 'R' - User forced a module unload.
282 * 'M' - System experienced a machine check exception.
283 * 'B' - System has hit bad_page.
284 * 'U' - Userspace-defined naughtiness.
285 * 'D' - Kernel has oopsed before
286 * 'A' - ACPI table overridden.
287 * 'W' - Taint on warning.
288 * 'C' - modules from drivers/staging are loaded.
289 * 'I' - Working around severe firmware bug.
290 * 'O' - Out-of-tree module has been loaded.
291 * 'E' - Unsigned module has been loaded.
293 * The string is overwritten by the next call to print_tainted().
295 const char *print_tainted(void)
297 static char buf[ARRAY_SIZE(tnts) + sizeof("Tainted: ")];
299 if (tainted_mask) {
300 char *s;
301 int i;
303 s = buf + sprintf(buf, "Tainted: ");
304 for (i = 0; i < ARRAY_SIZE(tnts); i++) {
305 const struct tnt *t = &tnts[i];
306 *s++ = test_bit(t->bit, &tainted_mask) ?
307 t->true : t->false;
309 *s = 0;
310 } else
311 snprintf(buf, sizeof(buf), "Not tainted");
313 return buf;
316 int test_taint(unsigned flag)
318 return test_bit(flag, &tainted_mask);
320 EXPORT_SYMBOL(test_taint);
322 unsigned long get_taint(void)
324 return tainted_mask;
328 * add_taint: add a taint flag if not already set.
329 * @flag: one of the TAINT_* constants.
330 * @lockdep_ok: whether lock debugging is still OK.
332 * If something bad has gone wrong, you'll want @lockdebug_ok = false, but for
333 * some notewortht-but-not-corrupting cases, it can be set to true.
335 void add_taint(unsigned flag, enum lockdep_ok lockdep_ok)
337 if (lockdep_ok == LOCKDEP_NOW_UNRELIABLE && __debug_locks_off())
338 pr_warn("Disabling lock debugging due to kernel taint\n");
340 set_bit(flag, &tainted_mask);
342 EXPORT_SYMBOL(add_taint);
344 static void spin_msec(int msecs)
346 int i;
348 for (i = 0; i < msecs; i++) {
349 touch_nmi_watchdog();
350 mdelay(1);
355 * It just happens that oops_enter() and oops_exit() are identically
356 * implemented...
358 static void do_oops_enter_exit(void)
360 unsigned long flags;
361 static int spin_counter;
363 if (!pause_on_oops)
364 return;
366 spin_lock_irqsave(&pause_on_oops_lock, flags);
367 if (pause_on_oops_flag == 0) {
368 /* This CPU may now print the oops message */
369 pause_on_oops_flag = 1;
370 } else {
371 /* We need to stall this CPU */
372 if (!spin_counter) {
373 /* This CPU gets to do the counting */
374 spin_counter = pause_on_oops;
375 do {
376 spin_unlock(&pause_on_oops_lock);
377 spin_msec(MSEC_PER_SEC);
378 spin_lock(&pause_on_oops_lock);
379 } while (--spin_counter);
380 pause_on_oops_flag = 0;
381 } else {
382 /* This CPU waits for a different one */
383 while (spin_counter) {
384 spin_unlock(&pause_on_oops_lock);
385 spin_msec(1);
386 spin_lock(&pause_on_oops_lock);
390 spin_unlock_irqrestore(&pause_on_oops_lock, flags);
394 * Return true if the calling CPU is allowed to print oops-related info.
395 * This is a bit racy..
397 int oops_may_print(void)
399 return pause_on_oops_flag == 0;
403 * Called when the architecture enters its oops handler, before it prints
404 * anything. If this is the first CPU to oops, and it's oopsing the first
405 * time then let it proceed.
407 * This is all enabled by the pause_on_oops kernel boot option. We do all
408 * this to ensure that oopses don't scroll off the screen. It has the
409 * side-effect of preventing later-oopsing CPUs from mucking up the display,
410 * too.
412 * It turns out that the CPU which is allowed to print ends up pausing for
413 * the right duration, whereas all the other CPUs pause for twice as long:
414 * once in oops_enter(), once in oops_exit().
416 void oops_enter(void)
418 tracing_off();
419 /* can't trust the integrity of the kernel anymore: */
420 debug_locks_off();
421 do_oops_enter_exit();
425 * 64-bit random ID for oopses:
427 static u64 oops_id;
429 static int init_oops_id(void)
431 if (!oops_id)
432 get_random_bytes(&oops_id, sizeof(oops_id));
433 else
434 oops_id++;
436 return 0;
438 late_initcall(init_oops_id);
440 void print_oops_end_marker(void)
442 init_oops_id();
443 pr_warn("---[ end trace %016llx ]---\n", (unsigned long long)oops_id);
447 * Called when the architecture exits its oops handler, after printing
448 * everything.
450 void oops_exit(void)
452 do_oops_enter_exit();
453 print_oops_end_marker();
454 kmsg_dump(KMSG_DUMP_OOPS);
457 #ifdef WANT_WARN_ON_SLOWPATH
458 struct slowpath_args {
459 const char *fmt;
460 va_list args;
463 static void warn_slowpath_common(const char *file, int line, void *caller,
464 unsigned taint, struct slowpath_args *args)
466 disable_trace_on_warning();
468 pr_warn("------------[ cut here ]------------\n");
469 pr_warn("WARNING: CPU: %d PID: %d at %s:%d %pS()\n",
470 raw_smp_processor_id(), current->pid, file, line, caller);
472 if (args)
473 vprintk(args->fmt, args->args);
475 print_modules();
476 dump_stack();
477 print_oops_end_marker();
478 /* Just a warning, don't kill lockdep. */
479 add_taint(taint, LOCKDEP_STILL_OK);
482 void warn_slowpath_fmt(const char *file, int line, const char *fmt, ...)
484 struct slowpath_args args;
486 args.fmt = fmt;
487 va_start(args.args, fmt);
488 warn_slowpath_common(file, line, __builtin_return_address(0),
489 TAINT_WARN, &args);
490 va_end(args.args);
492 EXPORT_SYMBOL(warn_slowpath_fmt);
494 void warn_slowpath_fmt_taint(const char *file, int line,
495 unsigned taint, const char *fmt, ...)
497 struct slowpath_args args;
499 args.fmt = fmt;
500 va_start(args.args, fmt);
501 warn_slowpath_common(file, line, __builtin_return_address(0),
502 taint, &args);
503 va_end(args.args);
505 EXPORT_SYMBOL(warn_slowpath_fmt_taint);
507 void warn_slowpath_null(const char *file, int line)
509 warn_slowpath_common(file, line, __builtin_return_address(0),
510 TAINT_WARN, NULL);
512 EXPORT_SYMBOL(warn_slowpath_null);
513 #endif
515 #ifdef CONFIG_CC_STACKPROTECTOR
518 * Called when gcc's -fstack-protector feature is used, and
519 * gcc detects corruption of the on-stack canary value
521 __visible void __stack_chk_fail(void)
523 panic("stack-protector: Kernel stack is corrupted in: %p\n",
524 __builtin_return_address(0));
526 EXPORT_SYMBOL(__stack_chk_fail);
528 #endif
530 core_param(panic, panic_timeout, int, 0644);
531 core_param(pause_on_oops, pause_on_oops, int, 0644);
533 static int __init setup_crash_kexec_post_notifiers(char *s)
535 crash_kexec_post_notifiers = true;
536 return 0;
538 early_param("crash_kexec_post_notifiers", setup_crash_kexec_post_notifiers);
540 static int __init oops_setup(char *s)
542 if (!s)
543 return -EINVAL;
544 if (!strcmp(s, "panic"))
545 panic_on_oops = 1;
546 return 0;
548 early_param("oops", oops_setup);