Back to home page

LXR

 
 

    


0001 /*
0002  *  linux/kernel/panic.c
0003  *
0004  *  Copyright (C) 1991, 1992  Linus Torvalds
0005  */
0006 
0007 /*
0008  * This function is used through-out the kernel (including mm and fs)
0009  * to indicate a major problem.
0010  */
0011 #include <linux/debug_locks.h>
0012 #include <linux/interrupt.h>
0013 #include <linux/kmsg_dump.h>
0014 #include <linux/kallsyms.h>
0015 #include <linux/notifier.h>
0016 #include <linux/module.h>
0017 #include <linux/random.h>
0018 #include <linux/ftrace.h>
0019 #include <linux/reboot.h>
0020 #include <linux/delay.h>
0021 #include <linux/kexec.h>
0022 #include <linux/sched.h>
0023 #include <linux/sysrq.h>
0024 #include <linux/init.h>
0025 #include <linux/nmi.h>
0026 #include <linux/console.h>
0027 #include <linux/bug.h>
0028 
0029 #define PANIC_TIMER_STEP 100
0030 #define PANIC_BLINK_SPD 18
0031 
0032 int panic_on_oops = CONFIG_PANIC_ON_OOPS_VALUE;
0033 static unsigned long tainted_mask;
0034 static int pause_on_oops;
0035 static int pause_on_oops_flag;
0036 static DEFINE_SPINLOCK(pause_on_oops_lock);
0037 bool crash_kexec_post_notifiers;
0038 int panic_on_warn __read_mostly;
0039 
0040 int panic_timeout = CONFIG_PANIC_TIMEOUT;
0041 EXPORT_SYMBOL_GPL(panic_timeout);
0042 
0043 ATOMIC_NOTIFIER_HEAD(panic_notifier_list);
0044 
0045 EXPORT_SYMBOL(panic_notifier_list);
0046 
0047 static long no_blink(int state)
0048 {
0049     return 0;
0050 }
0051 
0052 /* Returns how long it waited in ms */
0053 long (*panic_blink)(int state);
0054 EXPORT_SYMBOL(panic_blink);
0055 
0056 /*
0057  * Stop ourself in panic -- architecture code may override this
0058  */
0059 void __weak panic_smp_self_stop(void)
0060 {
0061     while (1)
0062         cpu_relax();
0063 }
0064 
0065 /*
0066  * Stop ourselves in NMI context if another CPU has already panicked. Arch code
0067  * may override this to prepare for crash dumping, e.g. save regs info.
0068  */
0069 void __weak nmi_panic_self_stop(struct pt_regs *regs)
0070 {
0071     panic_smp_self_stop();
0072 }
0073 
0074 /*
0075  * Stop other CPUs in panic.  Architecture dependent code may override this
0076  * with more suitable version.  For example, if the architecture supports
0077  * crash dump, it should save registers of each stopped CPU and disable
0078  * per-CPU features such as virtualization extensions.
0079  */
0080 void __weak crash_smp_send_stop(void)
0081 {
0082     static int cpus_stopped;
0083 
0084     /*
0085      * This function can be called twice in panic path, but obviously
0086      * we execute this only once.
0087      */
0088     if (cpus_stopped)
0089         return;
0090 
0091     /*
0092      * Note smp_send_stop is the usual smp shutdown function, which
0093      * unfortunately means it may not be hardened to work in a panic
0094      * situation.
0095      */
0096     smp_send_stop();
0097     cpus_stopped = 1;
0098 }
0099 
0100 atomic_t panic_cpu = ATOMIC_INIT(PANIC_CPU_INVALID);
0101 
0102 /*
0103  * A variant of panic() called from NMI context. We return if we've already
0104  * panicked on this CPU. If another CPU already panicked, loop in
0105  * nmi_panic_self_stop() which can provide architecture dependent code such
0106  * as saving register state for crash dump.
0107  */
0108 void nmi_panic(struct pt_regs *regs, const char *msg)
0109 {
0110     int old_cpu, cpu;
0111 
0112     cpu = raw_smp_processor_id();
0113     old_cpu = atomic_cmpxchg(&panic_cpu, PANIC_CPU_INVALID, cpu);
0114 
0115     if (old_cpu == PANIC_CPU_INVALID)
0116         panic("%s", msg);
0117     else if (old_cpu != cpu)
0118         nmi_panic_self_stop(regs);
0119 }
0120 EXPORT_SYMBOL(nmi_panic);
0121 
0122 /**
0123  *  panic - halt the system
0124  *  @fmt: The text string to print
0125  *
0126  *  Display a message, then perform cleanups.
0127  *
0128  *  This function never returns.
0129  */
0130 void panic(const char *fmt, ...)
0131 {
0132     static char buf[1024];
0133     va_list args;
0134     long i, i_next = 0;
0135     int state = 0;
0136     int old_cpu, this_cpu;
0137     bool _crash_kexec_post_notifiers = crash_kexec_post_notifiers;
0138 
0139     /*
0140      * Disable local interrupts. This will prevent panic_smp_self_stop
0141      * from deadlocking the first cpu that invokes the panic, since
0142      * there is nothing to prevent an interrupt handler (that runs
0143      * after setting panic_cpu) from invoking panic() again.
0144      */
0145     local_irq_disable();
0146 
0147     /*
0148      * It's possible to come here directly from a panic-assertion and
0149      * not have preempt disabled. Some functions called from here want
0150      * preempt to be disabled. No point enabling it later though...
0151      *
0152      * Only one CPU is allowed to execute the panic code from here. For
0153      * multiple parallel invocations of panic, all other CPUs either
0154      * stop themself or will wait until they are stopped by the 1st CPU
0155      * with smp_send_stop().
0156      *
0157      * `old_cpu == PANIC_CPU_INVALID' means this is the 1st CPU which
0158      * comes here, so go ahead.
0159      * `old_cpu == this_cpu' means we came from nmi_panic() which sets
0160      * panic_cpu to this CPU.  In this case, this is also the 1st CPU.
0161      */
0162     this_cpu = raw_smp_processor_id();
0163     old_cpu  = atomic_cmpxchg(&panic_cpu, PANIC_CPU_INVALID, this_cpu);
0164 
0165     if (old_cpu != PANIC_CPU_INVALID && old_cpu != this_cpu)
0166         panic_smp_self_stop();
0167 
0168     console_verbose();
0169     bust_spinlocks(1);
0170     va_start(args, fmt);
0171     vsnprintf(buf, sizeof(buf), fmt, args);
0172     va_end(args);
0173     pr_emerg("Kernel panic - not syncing: %s\n", buf);
0174 #ifdef CONFIG_DEBUG_BUGVERBOSE
0175     /*
0176      * Avoid nested stack-dumping if a panic occurs during oops processing
0177      */
0178     if (!test_taint(TAINT_DIE) && oops_in_progress <= 1)
0179         dump_stack();
0180 #endif
0181 
0182     /*
0183      * If we have crashed and we have a crash kernel loaded let it handle
0184      * everything else.
0185      * If we want to run this after calling panic_notifiers, pass
0186      * the "crash_kexec_post_notifiers" option to the kernel.
0187      *
0188      * Bypass the panic_cpu check and call __crash_kexec directly.
0189      */
0190     if (!_crash_kexec_post_notifiers) {
0191         printk_nmi_flush_on_panic();
0192         __crash_kexec(NULL);
0193 
0194         /*
0195          * Note smp_send_stop is the usual smp shutdown function, which
0196          * unfortunately means it may not be hardened to work in a
0197          * panic situation.
0198          */
0199         smp_send_stop();
0200     } else {
0201         /*
0202          * If we want to do crash dump after notifier calls and
0203          * kmsg_dump, we will need architecture dependent extra
0204          * works in addition to stopping other CPUs.
0205          */
0206         crash_smp_send_stop();
0207     }
0208 
0209     /*
0210      * Run any panic handlers, including those that might need to
0211      * add information to the kmsg dump output.
0212      */
0213     atomic_notifier_call_chain(&panic_notifier_list, 0, buf);
0214 
0215     /* Call flush even twice. It tries harder with a single online CPU */
0216     printk_nmi_flush_on_panic();
0217     kmsg_dump(KMSG_DUMP_PANIC);
0218 
0219     /*
0220      * If you doubt kdump always works fine in any situation,
0221      * "crash_kexec_post_notifiers" offers you a chance to run
0222      * panic_notifiers and dumping kmsg before kdump.
0223      * Note: since some panic_notifiers can make crashed kernel
0224      * more unstable, it can increase risks of the kdump failure too.
0225      *
0226      * Bypass the panic_cpu check and call __crash_kexec directly.
0227      */
0228     if (_crash_kexec_post_notifiers)
0229         __crash_kexec(NULL);
0230 
0231     bust_spinlocks(0);
0232 
0233     /*
0234      * We may have ended up stopping the CPU holding the lock (in
0235      * smp_send_stop()) while still having some valuable data in the console
0236      * buffer.  Try to acquire the lock then release it regardless of the
0237      * result.  The release will also print the buffers out.  Locks debug
0238      * should be disabled to avoid reporting bad unlock balance when
0239      * panic() is not being callled from OOPS.
0240      */
0241     debug_locks_off();
0242     console_flush_on_panic();
0243 
0244     if (!panic_blink)
0245         panic_blink = no_blink;
0246 
0247     if (panic_timeout > 0) {
0248         /*
0249          * Delay timeout seconds before rebooting the machine.
0250          * We can't use the "normal" timers since we just panicked.
0251          */
0252         pr_emerg("Rebooting in %d seconds..\n", panic_timeout);
0253 
0254         for (i = 0; i < panic_timeout * 1000; i += PANIC_TIMER_STEP) {
0255             touch_nmi_watchdog();
0256             if (i >= i_next) {
0257                 i += panic_blink(state ^= 1);
0258                 i_next = i + 3600 / PANIC_BLINK_SPD;
0259             }
0260             mdelay(PANIC_TIMER_STEP);
0261         }
0262     }
0263     if (panic_timeout != 0) {
0264         /*
0265          * This will not be a clean reboot, with everything
0266          * shutting down.  But if there is a chance of
0267          * rebooting the system it will be rebooted.
0268          */
0269         emergency_restart();
0270     }
0271 #ifdef __sparc__
0272     {
0273         extern int stop_a_enabled;
0274         /* Make sure the user can actually press Stop-A (L1-A) */
0275         stop_a_enabled = 1;
0276         pr_emerg("Press Stop-A (L1-A) to return to the boot prom\n");
0277     }
0278 #endif
0279 #if defined(CONFIG_S390)
0280     {
0281         unsigned long caller;
0282 
0283         caller = (unsigned long)__builtin_return_address(0);
0284         disabled_wait(caller);
0285     }
0286 #endif
0287     pr_emerg("---[ end Kernel panic - not syncing: %s\n", buf);
0288     local_irq_enable();
0289     for (i = 0; ; i += PANIC_TIMER_STEP) {
0290         touch_softlockup_watchdog();
0291         if (i >= i_next) {
0292             i += panic_blink(state ^= 1);
0293             i_next = i + 3600 / PANIC_BLINK_SPD;
0294         }
0295         mdelay(PANIC_TIMER_STEP);
0296     }
0297 }
0298 
0299 EXPORT_SYMBOL(panic);
0300 
0301 /*
0302  * TAINT_FORCED_RMMOD could be a per-module flag but the module
0303  * is being removed anyway.
0304  */
0305 const struct taint_flag taint_flags[TAINT_FLAGS_COUNT] = {
0306     { 'P', 'G', true }, /* TAINT_PROPRIETARY_MODULE */
0307     { 'F', ' ', true }, /* TAINT_FORCED_MODULE */
0308     { 'S', ' ', false },    /* TAINT_CPU_OUT_OF_SPEC */
0309     { 'R', ' ', false },    /* TAINT_FORCED_RMMOD */
0310     { 'M', ' ', false },    /* TAINT_MACHINE_CHECK */
0311     { 'B', ' ', false },    /* TAINT_BAD_PAGE */
0312     { 'U', ' ', false },    /* TAINT_USER */
0313     { 'D', ' ', false },    /* TAINT_DIE */
0314     { 'A', ' ', false },    /* TAINT_OVERRIDDEN_ACPI_TABLE */
0315     { 'W', ' ', false },    /* TAINT_WARN */
0316     { 'C', ' ', true }, /* TAINT_CRAP */
0317     { 'I', ' ', false },    /* TAINT_FIRMWARE_WORKAROUND */
0318     { 'O', ' ', true }, /* TAINT_OOT_MODULE */
0319     { 'E', ' ', true }, /* TAINT_UNSIGNED_MODULE */
0320     { 'L', ' ', false },    /* TAINT_SOFTLOCKUP */
0321     { 'K', ' ', true }, /* TAINT_LIVEPATCH */
0322 };
0323 
0324 /**
0325  *  print_tainted - return a string to represent the kernel taint state.
0326  *
0327  *  'P' - Proprietary module has been loaded.
0328  *  'F' - Module has been forcibly loaded.
0329  *  'S' - SMP with CPUs not designed for SMP.
0330  *  'R' - User forced a module unload.
0331  *  'M' - System experienced a machine check exception.
0332  *  'B' - System has hit bad_page.
0333  *  'U' - Userspace-defined naughtiness.
0334  *  'D' - Kernel has oopsed before
0335  *  'A' - ACPI table overridden.
0336  *  'W' - Taint on warning.
0337  *  'C' - modules from drivers/staging are loaded.
0338  *  'I' - Working around severe firmware bug.
0339  *  'O' - Out-of-tree module has been loaded.
0340  *  'E' - Unsigned module has been loaded.
0341  *  'L' - A soft lockup has previously occurred.
0342  *  'K' - Kernel has been live patched.
0343  *
0344  *  The string is overwritten by the next call to print_tainted().
0345  */
0346 const char *print_tainted(void)
0347 {
0348     static char buf[TAINT_FLAGS_COUNT + sizeof("Tainted: ")];
0349 
0350     if (tainted_mask) {
0351         char *s;
0352         int i;
0353 
0354         s = buf + sprintf(buf, "Tainted: ");
0355         for (i = 0; i < TAINT_FLAGS_COUNT; i++) {
0356             const struct taint_flag *t = &taint_flags[i];
0357             *s++ = test_bit(i, &tainted_mask) ?
0358                     t->c_true : t->c_false;
0359         }
0360         *s = 0;
0361     } else
0362         snprintf(buf, sizeof(buf), "Not tainted");
0363 
0364     return buf;
0365 }
0366 
0367 int test_taint(unsigned flag)
0368 {
0369     return test_bit(flag, &tainted_mask);
0370 }
0371 EXPORT_SYMBOL(test_taint);
0372 
0373 unsigned long get_taint(void)
0374 {
0375     return tainted_mask;
0376 }
0377 
0378 /**
0379  * add_taint: add a taint flag if not already set.
0380  * @flag: one of the TAINT_* constants.
0381  * @lockdep_ok: whether lock debugging is still OK.
0382  *
0383  * If something bad has gone wrong, you'll want @lockdebug_ok = false, but for
0384  * some notewortht-but-not-corrupting cases, it can be set to true.
0385  */
0386 void add_taint(unsigned flag, enum lockdep_ok lockdep_ok)
0387 {
0388     if (lockdep_ok == LOCKDEP_NOW_UNRELIABLE && __debug_locks_off())
0389         pr_warn("Disabling lock debugging due to kernel taint\n");
0390 
0391     set_bit(flag, &tainted_mask);
0392 }
0393 EXPORT_SYMBOL(add_taint);
0394 
0395 static void spin_msec(int msecs)
0396 {
0397     int i;
0398 
0399     for (i = 0; i < msecs; i++) {
0400         touch_nmi_watchdog();
0401         mdelay(1);
0402     }
0403 }
0404 
0405 /*
0406  * It just happens that oops_enter() and oops_exit() are identically
0407  * implemented...
0408  */
0409 static void do_oops_enter_exit(void)
0410 {
0411     unsigned long flags;
0412     static int spin_counter;
0413 
0414     if (!pause_on_oops)
0415         return;
0416 
0417     spin_lock_irqsave(&pause_on_oops_lock, flags);
0418     if (pause_on_oops_flag == 0) {
0419         /* This CPU may now print the oops message */
0420         pause_on_oops_flag = 1;
0421     } else {
0422         /* We need to stall this CPU */
0423         if (!spin_counter) {
0424             /* This CPU gets to do the counting */
0425             spin_counter = pause_on_oops;
0426             do {
0427                 spin_unlock(&pause_on_oops_lock);
0428                 spin_msec(MSEC_PER_SEC);
0429                 spin_lock(&pause_on_oops_lock);
0430             } while (--spin_counter);
0431             pause_on_oops_flag = 0;
0432         } else {
0433             /* This CPU waits for a different one */
0434             while (spin_counter) {
0435                 spin_unlock(&pause_on_oops_lock);
0436                 spin_msec(1);
0437                 spin_lock(&pause_on_oops_lock);
0438             }
0439         }
0440     }
0441     spin_unlock_irqrestore(&pause_on_oops_lock, flags);
0442 }
0443 
0444 /*
0445  * Return true if the calling CPU is allowed to print oops-related info.
0446  * This is a bit racy..
0447  */
0448 int oops_may_print(void)
0449 {
0450     return pause_on_oops_flag == 0;
0451 }
0452 
0453 /*
0454  * Called when the architecture enters its oops handler, before it prints
0455  * anything.  If this is the first CPU to oops, and it's oopsing the first
0456  * time then let it proceed.
0457  *
0458  * This is all enabled by the pause_on_oops kernel boot option.  We do all
0459  * this to ensure that oopses don't scroll off the screen.  It has the
0460  * side-effect of preventing later-oopsing CPUs from mucking up the display,
0461  * too.
0462  *
0463  * It turns out that the CPU which is allowed to print ends up pausing for
0464  * the right duration, whereas all the other CPUs pause for twice as long:
0465  * once in oops_enter(), once in oops_exit().
0466  */
0467 void oops_enter(void)
0468 {
0469     tracing_off();
0470     /* can't trust the integrity of the kernel anymore: */
0471     debug_locks_off();
0472     do_oops_enter_exit();
0473 }
0474 
0475 /*
0476  * 64-bit random ID for oopses:
0477  */
0478 static u64 oops_id;
0479 
0480 static int init_oops_id(void)
0481 {
0482     if (!oops_id)
0483         get_random_bytes(&oops_id, sizeof(oops_id));
0484     else
0485         oops_id++;
0486 
0487     return 0;
0488 }
0489 late_initcall(init_oops_id);
0490 
0491 void print_oops_end_marker(void)
0492 {
0493     init_oops_id();
0494     pr_warn("---[ end trace %016llx ]---\n", (unsigned long long)oops_id);
0495 }
0496 
0497 /*
0498  * Called when the architecture exits its oops handler, after printing
0499  * everything.
0500  */
0501 void oops_exit(void)
0502 {
0503     do_oops_enter_exit();
0504     print_oops_end_marker();
0505     kmsg_dump(KMSG_DUMP_OOPS);
0506 }
0507 
0508 struct warn_args {
0509     const char *fmt;
0510     va_list args;
0511 };
0512 
0513 void __warn(const char *file, int line, void *caller, unsigned taint,
0514         struct pt_regs *regs, struct warn_args *args)
0515 {
0516     disable_trace_on_warning();
0517 
0518     pr_warn("------------[ cut here ]------------\n");
0519 
0520     if (file)
0521         pr_warn("WARNING: CPU: %d PID: %d at %s:%d %pS\n",
0522             raw_smp_processor_id(), current->pid, file, line,
0523             caller);
0524     else
0525         pr_warn("WARNING: CPU: %d PID: %d at %pS\n",
0526             raw_smp_processor_id(), current->pid, caller);
0527 
0528     if (args)
0529         vprintk(args->fmt, args->args);
0530 
0531     if (panic_on_warn) {
0532         /*
0533          * This thread may hit another WARN() in the panic path.
0534          * Resetting this prevents additional WARN() from panicking the
0535          * system on this thread.  Other threads are blocked by the
0536          * panic_mutex in panic().
0537          */
0538         panic_on_warn = 0;
0539         panic("panic_on_warn set ...\n");
0540     }
0541 
0542     print_modules();
0543 
0544     if (regs)
0545         show_regs(regs);
0546     else
0547         dump_stack();
0548 
0549     print_oops_end_marker();
0550 
0551     /* Just a warning, don't kill lockdep. */
0552     add_taint(taint, LOCKDEP_STILL_OK);
0553 }
0554 
0555 #ifdef WANT_WARN_ON_SLOWPATH
0556 void warn_slowpath_fmt(const char *file, int line, const char *fmt, ...)
0557 {
0558     struct warn_args args;
0559 
0560     args.fmt = fmt;
0561     va_start(args.args, fmt);
0562     __warn(file, line, __builtin_return_address(0), TAINT_WARN, NULL,
0563            &args);
0564     va_end(args.args);
0565 }
0566 EXPORT_SYMBOL(warn_slowpath_fmt);
0567 
0568 void warn_slowpath_fmt_taint(const char *file, int line,
0569                  unsigned taint, const char *fmt, ...)
0570 {
0571     struct warn_args args;
0572 
0573     args.fmt = fmt;
0574     va_start(args.args, fmt);
0575     __warn(file, line, __builtin_return_address(0), taint, NULL, &args);
0576     va_end(args.args);
0577 }
0578 EXPORT_SYMBOL(warn_slowpath_fmt_taint);
0579 
0580 void warn_slowpath_null(const char *file, int line)
0581 {
0582     __warn(file, line, __builtin_return_address(0), TAINT_WARN, NULL, NULL);
0583 }
0584 EXPORT_SYMBOL(warn_slowpath_null);
0585 #endif
0586 
0587 #ifdef CONFIG_CC_STACKPROTECTOR
0588 
0589 /*
0590  * Called when gcc's -fstack-protector feature is used, and
0591  * gcc detects corruption of the on-stack canary value
0592  */
0593 __visible void __stack_chk_fail(void)
0594 {
0595     panic("stack-protector: Kernel stack is corrupted in: %p\n",
0596         __builtin_return_address(0));
0597 }
0598 EXPORT_SYMBOL(__stack_chk_fail);
0599 
0600 #endif
0601 
0602 core_param(panic, panic_timeout, int, 0644);
0603 core_param(pause_on_oops, pause_on_oops, int, 0644);
0604 core_param(panic_on_warn, panic_on_warn, int, 0644);
0605 core_param(crash_kexec_post_notifiers, crash_kexec_post_notifiers, bool, 0644);
0606 
0607 static int __init oops_setup(char *s)
0608 {
0609     if (!s)
0610         return -EINVAL;
0611     if (!strcmp(s, "panic"))
0612         panic_on_oops = 1;
0613     return 0;
0614 }
0615 early_param("oops", oops_setup);