LCOV - code coverage report
Current view: top level - kernel - panic.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 4 121 3.3 %
Date: 2014-02-18 Functions: 2 19 10.5 %
Branches: 1 56 1.8 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  *  linux/kernel/panic.c
       3                 :            :  *
       4                 :            :  *  Copyright (C) 1991, 1992  Linus Torvalds
       5                 :            :  */
       6                 :            : 
       7                 :            : /*
       8                 :            :  * This function is used through-out the kernel (including mm and fs)
       9                 :            :  * to indicate a major problem.
      10                 :            :  */
      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                 :            : 
      27                 :            : #define PANIC_TIMER_STEP 100
      28                 :            : #define PANIC_BLINK_SPD 18
      29                 :            : 
      30                 :            : /* Machine specific panic information string */
      31                 :            : char *mach_panic_string;
      32                 :            : 
      33                 :            : int panic_on_oops = CONFIG_PANIC_ON_OOPS_VALUE;
      34                 :            : static unsigned long tainted_mask;
      35                 :            : static int pause_on_oops;
      36                 :            : static int pause_on_oops_flag;
      37                 :            : static DEFINE_SPINLOCK(pause_on_oops_lock);
      38                 :            : 
      39                 :            : #ifndef CONFIG_PANIC_TIMEOUT
      40                 :            : #define CONFIG_PANIC_TIMEOUT 0
      41                 :            : #endif
      42                 :            : int panic_timeout = CONFIG_PANIC_TIMEOUT;
      43                 :            : EXPORT_SYMBOL_GPL(panic_timeout);
      44                 :            : 
      45                 :            : ATOMIC_NOTIFIER_HEAD(panic_notifier_list);
      46                 :            : 
      47                 :            : EXPORT_SYMBOL(panic_notifier_list);
      48                 :            : 
      49                 :          0 : static long no_blink(int state)
      50                 :            : {
      51                 :          0 :         return 0;
      52                 :            : }
      53                 :            : 
      54                 :            : /* Returns how long it waited in ms */
      55                 :            : long (*panic_blink)(int state);
      56                 :            : EXPORT_SYMBOL(panic_blink);
      57                 :            : 
      58                 :            : /*
      59                 :            :  * Stop ourself in panic -- architecture code may override this
      60                 :            :  */
      61                 :          0 : void __weak panic_smp_self_stop(void)
      62                 :            : {
      63                 :            :         while (1)
      64                 :          0 :                 cpu_relax();
      65                 :            : }
      66                 :            : 
      67                 :            : /**
      68                 :            :  *      panic - halt the system
      69                 :            :  *      @fmt: The text string to print
      70                 :            :  *
      71                 :            :  *      Display a message, then perform cleanups.
      72                 :            :  *
      73                 :            :  *      This function never returns.
      74                 :            :  */
      75                 :          0 : void panic(const char *fmt, ...)
      76                 :            : {
      77                 :            :         static DEFINE_SPINLOCK(panic_lock);
      78                 :            :         static char buf[1024];
      79                 :            :         va_list args;
      80                 :            :         long i, i_next = 0;
      81                 :            :         int state = 0;
      82                 :            : 
      83                 :            :         /*
      84                 :            :          * Disable local interrupts. This will prevent panic_smp_self_stop
      85                 :            :          * from deadlocking the first cpu that invokes the panic, since
      86                 :            :          * there is nothing to prevent an interrupt handler (that runs
      87                 :            :          * after the panic_lock is acquired) from invoking panic again.
      88                 :            :          */
      89                 :            :         local_irq_disable();
      90                 :            : 
      91                 :            :         /*
      92                 :            :          * It's possible to come here directly from a panic-assertion and
      93                 :            :          * not have preempt disabled. Some functions called from here want
      94                 :            :          * preempt to be disabled. No point enabling it later though...
      95                 :            :          *
      96                 :            :          * Only one CPU is allowed to execute the panic code from here. For
      97                 :            :          * multiple parallel invocations of panic, all other CPUs either
      98                 :            :          * stop themself or will wait until they are stopped by the 1st CPU
      99                 :            :          * with smp_send_stop().
     100                 :            :          */
     101         [ #  # ]:          0 :         if (!spin_trylock(&panic_lock))
     102                 :          0 :                 panic_smp_self_stop();
     103                 :            : 
     104                 :            :         console_verbose();
     105                 :          0 :         bust_spinlocks(1);
     106                 :          0 :         va_start(args, fmt);
     107                 :          0 :         vsnprintf(buf, sizeof(buf), fmt, args);
     108                 :          0 :         va_end(args);
     109                 :          0 :         printk(KERN_EMERG "Kernel panic - not syncing: %s\n",buf);
     110                 :            : #ifdef CONFIG_DEBUG_BUGVERBOSE
     111                 :            :         /*
     112                 :            :          * Avoid nested stack-dumping if a panic occurs during oops processing
     113                 :            :          */
     114 [ #  # ][ #  # ]:          0 :         if (!test_taint(TAINT_DIE) && oops_in_progress <= 1)
     115                 :          0 :                 dump_stack();
     116                 :            : #endif
     117                 :            : 
     118                 :            :         /*
     119                 :            :          * If we have crashed and we have a crash kernel loaded let it handle
     120                 :            :          * everything else.
     121                 :            :          * Do we want to call this before we try to display a message?
     122                 :            :          */
     123                 :            :         crash_kexec(NULL);
     124                 :            : 
     125                 :            :         /*
     126                 :            :          * Note smp_send_stop is the usual smp shutdown function, which
     127                 :            :          * unfortunately means it may not be hardened to work in a panic
     128                 :            :          * situation.
     129                 :            :          */
     130                 :          0 :         smp_send_stop();
     131                 :            : 
     132                 :            :         /*
     133                 :            :          * Run any panic handlers, including those that might need to
     134                 :            :          * add information to the kmsg dump output.
     135                 :            :          */
     136                 :          0 :         atomic_notifier_call_chain(&panic_notifier_list, 0, buf);
     137                 :            : 
     138                 :          0 :         kmsg_dump(KMSG_DUMP_PANIC);
     139                 :            : 
     140                 :          0 :         bust_spinlocks(0);
     141                 :            : 
     142         [ #  # ]:          0 :         if (!panic_blink)
     143                 :          0 :                 panic_blink = no_blink;
     144                 :            : 
     145         [ #  # ]:          0 :         if (panic_timeout > 0) {
     146                 :            :                 /*
     147                 :            :                  * Delay timeout seconds before rebooting the machine.
     148                 :            :                  * We can't use the "normal" timers since we just panicked.
     149                 :            :                  */
     150                 :          0 :                 printk(KERN_EMERG "Rebooting in %d seconds..", panic_timeout);
     151                 :            : 
     152         [ #  # ]:          0 :                 for (i = 0; i < panic_timeout * 1000; i += PANIC_TIMER_STEP) {
     153                 :            :                         touch_nmi_watchdog();
     154         [ #  # ]:          0 :                         if (i >= i_next) {
     155                 :          0 :                                 i += panic_blink(state ^= 1);
     156                 :          0 :                                 i_next = i + 3600 / PANIC_BLINK_SPD;
     157                 :            :                         }
     158         [ #  # ]:          0 :                         mdelay(PANIC_TIMER_STEP);
     159                 :            :                 }
     160                 :            :         }
     161         [ #  # ]:          0 :         if (panic_timeout != 0) {
     162                 :            :                 /*
     163                 :            :                  * This will not be a clean reboot, with everything
     164                 :            :                  * shutting down.  But if there is a chance of
     165                 :            :                  * rebooting the system it will be rebooted.
     166                 :            :                  */
     167                 :          0 :                 emergency_restart();
     168                 :            :         }
     169                 :            : #ifdef __sparc__
     170                 :            :         {
     171                 :            :                 extern int stop_a_enabled;
     172                 :            :                 /* Make sure the user can actually press Stop-A (L1-A) */
     173                 :            :                 stop_a_enabled = 1;
     174                 :            :                 printk(KERN_EMERG "Press Stop-A (L1-A) to return to the boot prom\n");
     175                 :            :         }
     176                 :            : #endif
     177                 :            : #if defined(CONFIG_S390)
     178                 :            :         {
     179                 :            :                 unsigned long caller;
     180                 :            : 
     181                 :            :                 caller = (unsigned long)__builtin_return_address(0);
     182                 :            :                 disabled_wait(caller);
     183                 :            :         }
     184                 :            : #endif
     185                 :            :         local_irq_enable();
     186                 :          0 :         for (i = 0; ; i += PANIC_TIMER_STEP) {
     187                 :            :                 touch_softlockup_watchdog();
     188         [ #  # ]:          0 :                 if (i >= i_next) {
     189                 :          0 :                         i += panic_blink(state ^= 1);
     190                 :          0 :                         i_next = i + 3600 / PANIC_BLINK_SPD;
     191                 :            :                 }
     192         [ #  # ]:          0 :                 mdelay(PANIC_TIMER_STEP);
     193                 :          0 :         }
     194                 :            : }
     195                 :            : 
     196                 :            : EXPORT_SYMBOL(panic);
     197                 :            : 
     198                 :            : 
     199                 :            : struct tnt {
     200                 :            :         u8      bit;
     201                 :            :         char    true;
     202                 :            :         char    false;
     203                 :            : };
     204                 :            : 
     205                 :            : static const struct tnt tnts[] = {
     206                 :            :         { TAINT_PROPRIETARY_MODULE,     'P', 'G' },
     207                 :            :         { TAINT_FORCED_MODULE,          'F', ' ' },
     208                 :            :         { TAINT_UNSAFE_SMP,             'S', ' ' },
     209                 :            :         { TAINT_FORCED_RMMOD,           'R', ' ' },
     210                 :            :         { TAINT_MACHINE_CHECK,          'M', ' ' },
     211                 :            :         { TAINT_BAD_PAGE,               'B', ' ' },
     212                 :            :         { TAINT_USER,                   'U', ' ' },
     213                 :            :         { TAINT_DIE,                    'D', ' ' },
     214                 :            :         { TAINT_OVERRIDDEN_ACPI_TABLE,  'A', ' ' },
     215                 :            :         { TAINT_WARN,                   'W', ' ' },
     216                 :            :         { TAINT_CRAP,                   'C', ' ' },
     217                 :            :         { TAINT_FIRMWARE_WORKAROUND,    'I', ' ' },
     218                 :            :         { TAINT_OOT_MODULE,             'O', ' ' },
     219                 :            : };
     220                 :            : 
     221                 :            : /**
     222                 :            :  *      print_tainted - return a string to represent the kernel taint state.
     223                 :            :  *
     224                 :            :  *  'P' - Proprietary module has been loaded.
     225                 :            :  *  'F' - Module has been forcibly loaded.
     226                 :            :  *  'S' - SMP with CPUs not designed for SMP.
     227                 :            :  *  'R' - User forced a module unload.
     228                 :            :  *  'M' - System experienced a machine check exception.
     229                 :            :  *  'B' - System has hit bad_page.
     230                 :            :  *  'U' - Userspace-defined naughtiness.
     231                 :            :  *  'D' - Kernel has oopsed before
     232                 :            :  *  'A' - ACPI table overridden.
     233                 :            :  *  'W' - Taint on warning.
     234                 :            :  *  'C' - modules from drivers/staging are loaded.
     235                 :            :  *  'I' - Working around severe firmware bug.
     236                 :            :  *  'O' - Out-of-tree module has been loaded.
     237                 :            :  *
     238                 :            :  *      The string is overwritten by the next call to print_tainted().
     239                 :            :  */
     240                 :          0 : const char *print_tainted(void)
     241                 :            : {
     242                 :            :         static char buf[ARRAY_SIZE(tnts) + sizeof("Tainted: ")];
     243                 :            : 
     244         [ -  + ]:          3 :         if (tainted_mask) {
     245                 :            :                 char *s;
     246                 :            :                 int i;
     247                 :            : 
     248                 :          0 :                 s = buf + sprintf(buf, "Tainted: ");
     249         [ #  # ]:          0 :                 for (i = 0; i < ARRAY_SIZE(tnts); i++) {
     250                 :          0 :                         const struct tnt *t = &tnts[i];
     251         [ #  # ]:          0 :                         *s++ = test_bit(t->bit, &tainted_mask) ?
     252                 :            :                                         t->true : t->false;
     253                 :            :                 }
     254                 :          0 :                 *s = 0;
     255                 :            :         } else
     256                 :          3 :                 snprintf(buf, sizeof(buf), "Not tainted");
     257                 :            : 
     258                 :          3 :         return buf;
     259                 :            : }
     260                 :            : 
     261                 :          0 : int test_taint(unsigned flag)
     262                 :            : {
     263                 :          0 :         return test_bit(flag, &tainted_mask);
     264                 :            : }
     265                 :            : EXPORT_SYMBOL(test_taint);
     266                 :            : 
     267                 :          0 : unsigned long get_taint(void)
     268                 :            : {
     269                 :          2 :         return tainted_mask;
     270                 :            : }
     271                 :            : 
     272                 :            : /**
     273                 :            :  * add_taint: add a taint flag if not already set.
     274                 :            :  * @flag: one of the TAINT_* constants.
     275                 :            :  * @lockdep_ok: whether lock debugging is still OK.
     276                 :            :  *
     277                 :            :  * If something bad has gone wrong, you'll want @lockdebug_ok = false, but for
     278                 :            :  * some notewortht-but-not-corrupting cases, it can be set to true.
     279                 :            :  */
     280                 :          0 : void add_taint(unsigned flag, enum lockdep_ok lockdep_ok)
     281                 :            : {
     282   [ #  #  #  # ]:          0 :         if (lockdep_ok == LOCKDEP_NOW_UNRELIABLE && __debug_locks_off())
     283                 :          0 :                 printk(KERN_WARNING
     284                 :            :                        "Disabling lock debugging due to kernel taint\n");
     285                 :            : 
     286                 :          0 :         set_bit(flag, &tainted_mask);
     287                 :          0 : }
     288                 :            : EXPORT_SYMBOL(add_taint);
     289                 :            : 
     290                 :            : static void spin_msec(int msecs)
     291                 :            : {
     292                 :            :         int i;
     293                 :            : 
     294 [ #  # ][ #  # ]:          0 :         for (i = 0; i < msecs; i++) {
     295                 :            :                 touch_nmi_watchdog();
     296                 :          0 :                 mdelay(1);
     297                 :            :         }
     298                 :            : }
     299                 :            : 
     300                 :            : /*
     301                 :            :  * It just happens that oops_enter() and oops_exit() are identically
     302                 :            :  * implemented...
     303                 :            :  */
     304                 :          0 : static void do_oops_enter_exit(void)
     305                 :            : {
     306                 :            :         unsigned long flags;
     307                 :            :         static int spin_counter;
     308                 :            : 
     309         [ #  # ]:          0 :         if (!pause_on_oops)
     310                 :          0 :                 return;
     311                 :            : 
     312                 :          0 :         spin_lock_irqsave(&pause_on_oops_lock, flags);
     313         [ #  # ]:          0 :         if (pause_on_oops_flag == 0) {
     314                 :            :                 /* This CPU may now print the oops message */
     315                 :          0 :                 pause_on_oops_flag = 1;
     316                 :            :         } else {
     317                 :            :                 /* We need to stall this CPU */
     318         [ #  # ]:          0 :                 if (!spin_counter) {
     319                 :            :                         /* This CPU gets to do the counting */
     320                 :          0 :                         spin_counter = pause_on_oops;
     321                 :            :                         do {
     322                 :            :                                 spin_unlock(&pause_on_oops_lock);
     323                 :            :                                 spin_msec(MSEC_PER_SEC);
     324                 :            :                                 spin_lock(&pause_on_oops_lock);
     325         [ #  # ]:          0 :                         } while (--spin_counter);
     326                 :          0 :                         pause_on_oops_flag = 0;
     327                 :            :                 } else {
     328                 :            :                         /* This CPU waits for a different one */
     329         [ #  # ]:          0 :                         while (spin_counter) {
     330                 :            :                                 spin_unlock(&pause_on_oops_lock);
     331                 :            :                                 spin_msec(1);
     332                 :            :                                 spin_lock(&pause_on_oops_lock);
     333                 :            :                         }
     334                 :            :                 }
     335                 :            :         }
     336                 :            :         spin_unlock_irqrestore(&pause_on_oops_lock, flags);
     337                 :            : }
     338                 :            : 
     339                 :            : /*
     340                 :            :  * Return true if the calling CPU is allowed to print oops-related info.
     341                 :            :  * This is a bit racy..
     342                 :            :  */
     343                 :          0 : int oops_may_print(void)
     344                 :            : {
     345                 :          0 :         return pause_on_oops_flag == 0;
     346                 :            : }
     347                 :            : 
     348                 :            : /*
     349                 :            :  * Called when the architecture enters its oops handler, before it prints
     350                 :            :  * anything.  If this is the first CPU to oops, and it's oopsing the first
     351                 :            :  * time then let it proceed.
     352                 :            :  *
     353                 :            :  * This is all enabled by the pause_on_oops kernel boot option.  We do all
     354                 :            :  * this to ensure that oopses don't scroll off the screen.  It has the
     355                 :            :  * side-effect of preventing later-oopsing CPUs from mucking up the display,
     356                 :            :  * too.
     357                 :            :  *
     358                 :            :  * It turns out that the CPU which is allowed to print ends up pausing for
     359                 :            :  * the right duration, whereas all the other CPUs pause for twice as long:
     360                 :            :  * once in oops_enter(), once in oops_exit().
     361                 :            :  */
     362                 :          0 : void oops_enter(void)
     363                 :            : {
     364                 :          0 :         tracing_off();
     365                 :            :         /* can't trust the integrity of the kernel anymore: */
     366                 :          0 :         debug_locks_off();
     367                 :          0 :         do_oops_enter_exit();
     368                 :          0 : }
     369                 :            : 
     370                 :            : /*
     371                 :            :  * 64-bit random ID for oopses:
     372                 :            :  */
     373                 :            : static u64 oops_id;
     374                 :            : 
     375                 :          0 : static int init_oops_id(void)
     376                 :            : {
     377         [ #  # ]:          0 :         if (!oops_id)
     378                 :          0 :                 get_random_bytes(&oops_id, sizeof(oops_id));
     379                 :            :         else
     380                 :          0 :                 oops_id++;
     381                 :            : 
     382                 :          0 :         return 0;
     383                 :            : }
     384                 :            : late_initcall(init_oops_id);
     385                 :            : 
     386                 :          0 : void print_oops_end_marker(void)
     387                 :            : {
     388                 :          0 :         init_oops_id();
     389                 :            : 
     390         [ #  # ]:          0 :         if (mach_panic_string)
     391                 :          0 :                 printk(KERN_WARNING "Board Information: %s\n",
     392                 :            :                        mach_panic_string);
     393                 :            : 
     394                 :          0 :         printk(KERN_WARNING "---[ end trace %016llx ]---\n",
     395                 :            :                 (unsigned long long)oops_id);
     396                 :          0 : }
     397                 :            : 
     398                 :            : /*
     399                 :            :  * Called when the architecture exits its oops handler, after printing
     400                 :            :  * everything.
     401                 :            :  */
     402                 :          0 : void oops_exit(void)
     403                 :            : {
     404                 :          0 :         do_oops_enter_exit();
     405                 :          0 :         print_oops_end_marker();
     406                 :          0 :         kmsg_dump(KMSG_DUMP_OOPS);
     407                 :          0 : }
     408                 :            : 
     409                 :            : #ifdef WANT_WARN_ON_SLOWPATH
     410                 :            : struct slowpath_args {
     411                 :            :         const char *fmt;
     412                 :            :         va_list args;
     413                 :            : };
     414                 :            : 
     415                 :          0 : static void warn_slowpath_common(const char *file, int line, void *caller,
     416                 :            :                                  unsigned taint, struct slowpath_args *args)
     417                 :            : {
     418                 :          0 :         disable_trace_on_warning();
     419                 :            : 
     420                 :          0 :         pr_warn("------------[ cut here ]------------\n");
     421                 :          0 :         pr_warn("WARNING: CPU: %d PID: %d at %s:%d %pS()\n",
     422                 :            :                 raw_smp_processor_id(), current->pid, file, line, caller);
     423                 :            : 
     424         [ #  # ]:          0 :         if (args)
     425                 :          0 :                 vprintk(args->fmt, args->args);
     426                 :            : 
     427                 :          0 :         print_modules();
     428                 :          0 :         dump_stack();
     429                 :          0 :         print_oops_end_marker();
     430                 :            :         /* Just a warning, don't kill lockdep. */
     431                 :          0 :         add_taint(taint, LOCKDEP_STILL_OK);
     432                 :          0 : }
     433                 :            : 
     434                 :          0 : void warn_slowpath_fmt(const char *file, int line, const char *fmt, ...)
     435                 :            : {
     436                 :            :         struct slowpath_args args;
     437                 :            : 
     438                 :          0 :         args.fmt = fmt;
     439                 :          0 :         va_start(args.args, fmt);
     440                 :          0 :         warn_slowpath_common(file, line, __builtin_return_address(0),
     441                 :            :                              TAINT_WARN, &args);
     442                 :          0 :         va_end(args.args);
     443                 :          0 : }
     444                 :            : EXPORT_SYMBOL(warn_slowpath_fmt);
     445                 :            : 
     446                 :          0 : void warn_slowpath_fmt_taint(const char *file, int line,
     447                 :            :                              unsigned taint, const char *fmt, ...)
     448                 :            : {
     449                 :            :         struct slowpath_args args;
     450                 :            : 
     451                 :          0 :         args.fmt = fmt;
     452                 :          0 :         va_start(args.args, fmt);
     453                 :          0 :         warn_slowpath_common(file, line, __builtin_return_address(0),
     454                 :            :                              taint, &args);
     455                 :          0 :         va_end(args.args);
     456                 :          0 : }
     457                 :            : EXPORT_SYMBOL(warn_slowpath_fmt_taint);
     458                 :            : 
     459                 :          0 : void warn_slowpath_null(const char *file, int line)
     460                 :            : {
     461                 :          0 :         warn_slowpath_common(file, line, __builtin_return_address(0),
     462                 :            :                              TAINT_WARN, NULL);
     463                 :          0 : }
     464                 :            : EXPORT_SYMBOL(warn_slowpath_null);
     465                 :            : #endif
     466                 :            : 
     467                 :            : #ifdef CONFIG_CC_STACKPROTECTOR
     468                 :            : 
     469                 :            : /*
     470                 :            :  * Called when gcc's -fstack-protector feature is used, and
     471                 :            :  * gcc detects corruption of the on-stack canary value
     472                 :            :  */
     473                 :          0 : void __stack_chk_fail(void)
     474                 :            : {
     475                 :          0 :         panic("stack-protector: Kernel stack is corrupted in: %p\n",
     476                 :            :                 __builtin_return_address(0));
     477                 :            : }
     478                 :            : EXPORT_SYMBOL(__stack_chk_fail);
     479                 :            : 
     480                 :            : #endif
     481                 :            : 
     482                 :            : core_param(panic, panic_timeout, int, 0644);
     483                 :            : core_param(pause_on_oops, pause_on_oops, int, 0644);
     484                 :            : 
     485                 :          0 : static int __init oops_setup(char *s)
     486                 :            : {
     487         [ #  # ]:          0 :         if (!s)
     488                 :            :                 return -EINVAL;
     489         [ #  # ]:          0 :         if (!strcmp(s, "panic"))
     490                 :          0 :                 panic_on_oops = 1;
     491                 :            :         return 0;
     492                 :            : }
     493                 :            : early_param("oops", oops_setup);

Generated by: LCOV version 1.9