linux/kernel/panic.c
<<
>>
Prefs
   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#include <linux/bug.h>
  27
  28#define PANIC_TIMER_STEP 100
  29#define PANIC_BLINK_SPD 18
  30
  31int panic_on_oops = CONFIG_PANIC_ON_OOPS_VALUE;
  32static unsigned long tainted_mask;
  33static int pause_on_oops;
  34static int pause_on_oops_flag;
  35static DEFINE_SPINLOCK(pause_on_oops_lock);
  36bool crash_kexec_post_notifiers;
  37int panic_on_warn __read_mostly;
  38
  39int panic_timeout = CONFIG_PANIC_TIMEOUT;
  40EXPORT_SYMBOL_GPL(panic_timeout);
  41
  42ATOMIC_NOTIFIER_HEAD(panic_notifier_list);
  43
  44EXPORT_SYMBOL(panic_notifier_list);
  45
  46static long no_blink(int state)
  47{
  48        return 0;
  49}
  50
  51/* Returns how long it waited in ms */
  52long (*panic_blink)(int state);
  53EXPORT_SYMBOL(panic_blink);
  54
  55/*
  56 * Stop ourself in panic -- architecture code may override this
  57 */
  58void __weak panic_smp_self_stop(void)
  59{
  60        while (1)
  61                cpu_relax();
  62}
  63
  64/*
  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.
  67 */
  68void __weak nmi_panic_self_stop(struct pt_regs *regs)
  69{
  70        panic_smp_self_stop();
  71}
  72
  73/*
  74 * Stop other CPUs in panic.  Architecture dependent code may override this
  75 * with more suitable version.  For example, if the architecture supports
  76 * crash dump, it should save registers of each stopped CPU and disable
  77 * per-CPU features such as virtualization extensions.
  78 */
  79void __weak crash_smp_send_stop(void)
  80{
  81        static int cpus_stopped;
  82
  83        /*
  84         * This function can be called twice in panic path, but obviously
  85         * we execute this only once.
  86         */
  87        if (cpus_stopped)
  88                return;
  89
  90        /*
  91         * Note smp_send_stop is the usual smp shutdown function, which
  92         * unfortunately means it may not be hardened to work in a panic
  93         * situation.
  94         */
  95        smp_send_stop();
  96        cpus_stopped = 1;
  97}
  98
  99atomic_t panic_cpu = ATOMIC_INIT(PANIC_CPU_INVALID);
 100
 101/*
 102 * A variant of panic() called from NMI context. We return if we've already
 103 * panicked on this CPU. If another CPU already panicked, loop in
 104 * nmi_panic_self_stop() which can provide architecture dependent code such
 105 * as saving register state for crash dump.
 106 */
 107void nmi_panic(struct pt_regs *regs, const char *msg)
 108{
 109        int old_cpu, cpu;
 110
 111        cpu = raw_smp_processor_id();
 112        old_cpu = atomic_cmpxchg(&panic_cpu, PANIC_CPU_INVALID, cpu);
 113
 114        if (old_cpu == PANIC_CPU_INVALID)
 115                panic("%s", msg);
 116        else if (old_cpu != cpu)
 117                nmi_panic_self_stop(regs);
 118}
 119EXPORT_SYMBOL(nmi_panic);
 120
 121/**
 122 *      panic - halt the system
 123 *      @fmt: The text string to print
 124 *
 125 *      Display a message, then perform cleanups.
 126 *
 127 *      This function never returns.
 128 */
 129void panic(const char *fmt, ...)
 130{
 131        static char buf[1024];
 132        va_list args;
 133        long i, i_next = 0;
 134        int state = 0;
 135        int old_cpu, this_cpu;
 136        bool _crash_kexec_post_notifiers = crash_kexec_post_notifiers;
 137
 138        /*
 139         * Disable local interrupts. This will prevent panic_smp_self_stop
 140         * from deadlocking the first cpu that invokes the panic, since
 141         * there is nothing to prevent an interrupt handler (that runs
 142         * after setting panic_cpu) from invoking panic() again.
 143         */
 144        local_irq_disable();
 145
 146        /*
 147         * It's possible to come here directly from a panic-assertion and
 148         * not have preempt disabled. Some functions called from here want
 149         * preempt to be disabled. No point enabling it later though...
 150         *
 151         * Only one CPU is allowed to execute the panic code from here. For
 152         * multiple parallel invocations of panic, all other CPUs either
 153         * stop themself or will wait until they are stopped by the 1st CPU
 154         * with smp_send_stop().
 155         *
 156         * `old_cpu == PANIC_CPU_INVALID' means this is the 1st CPU which
 157         * comes here, so go ahead.
 158         * `old_cpu == this_cpu' means we came from nmi_panic() which sets
 159         * panic_cpu to this CPU.  In this case, this is also the 1st CPU.
 160         */
 161        this_cpu = raw_smp_processor_id();
 162        old_cpu  = atomic_cmpxchg(&panic_cpu, PANIC_CPU_INVALID, this_cpu);
 163
 164        if (old_cpu != PANIC_CPU_INVALID && old_cpu != this_cpu)
 165                panic_smp_self_stop();
 166
 167        console_verbose();
 168        bust_spinlocks(1);
 169        va_start(args, fmt);
 170        vsnprintf(buf, sizeof(buf), fmt, args);
 171        va_end(args);
 172        printk(KERN_EMERG "Kernel panic - not syncing: %s\n",buf);
 173#ifdef CONFIG_DEBUG_BUGVERBOSE
 174        /*
 175         * Avoid nested stack-dumping if a panic occurs during oops processing
 176         */
 177        if (!test_taint(TAINT_DIE) && oops_in_progress <= 1)
 178                dump_stack();
 179#endif
 180
 181        /*
 182         * If we have crashed and we have a crash kernel loaded let it handle
 183         * everything else.
 184         * If we want to run this after calling panic_notifiers, pass
 185         * the "crash_kexec_post_notifiers" option to the kernel.
 186         *
 187         * Bypass the panic_cpu check and call __crash_kexec directly.
 188         */
 189        if (!_crash_kexec_post_notifiers) {
 190                __crash_kexec(NULL);
 191
 192                /*
 193                 * Note smp_send_stop is the usual smp shutdown function, which
 194                 * unfortunately means it may not be hardened to work in a
 195                 * panic situation.
 196                 */
 197                smp_send_stop();
 198        } else {
 199                /*
 200                 * If we want to do crash dump after notifier calls and
 201                 * kmsg_dump, we will need architecture dependent extra
 202                 * works in addition to stopping other CPUs.
 203                 */
 204                crash_smp_send_stop();
 205        }
 206
 207        /*
 208         * Run any panic handlers, including those that might need to
 209         * add information to the kmsg dump output.
 210         */
 211        atomic_notifier_call_chain(&panic_notifier_list, 0, buf);
 212
 213        kmsg_dump(KMSG_DUMP_PANIC);
 214
 215        /*
 216         * If you doubt kdump always works fine in any situation,
 217         * "crash_kexec_post_notifiers" offers you a chance to run
 218         * panic_notifiers and dumping kmsg before kdump.
 219         * Note: since some panic_notifiers can make crashed kernel
 220         * more unstable, it can increase risks of the kdump failure too.
 221         *
 222         * Bypass the panic_cpu check and call __crash_kexec directly.
 223         */
 224        if (_crash_kexec_post_notifiers)
 225                __crash_kexec(NULL);
 226
 227        bust_spinlocks(0);
 228
 229        if (!panic_blink)
 230                panic_blink = no_blink;
 231
 232        if (panic_timeout > 0) {
 233                /*
 234                 * Delay timeout seconds before rebooting the machine.
 235                 * We can't use the "normal" timers since we just panicked.
 236                 */
 237                printk(KERN_EMERG "Rebooting in %d seconds..", panic_timeout);
 238
 239                for (i = 0; i < panic_timeout * 1000; i += PANIC_TIMER_STEP) {
 240                        touch_nmi_watchdog();
 241                        if (i >= i_next) {
 242                                i += panic_blink(state ^= 1);
 243                                i_next = i + 3600 / PANIC_BLINK_SPD;
 244                        }
 245                        mdelay(PANIC_TIMER_STEP);
 246                }
 247        }
 248        if (panic_timeout != 0) {
 249                /*
 250                 * This will not be a clean reboot, with everything
 251                 * shutting down.  But if there is a chance of
 252                 * rebooting the system it will be rebooted.
 253                 */
 254                emergency_restart();
 255        }
 256#ifdef __sparc__
 257        {
 258                extern int stop_a_enabled;
 259                /* Make sure the user can actually press Stop-A (L1-A) */
 260                stop_a_enabled = 1;
 261                printk(KERN_EMERG "Press Stop-A (L1-A) to return to the boot prom\n");
 262        }
 263#endif
 264#if defined(CONFIG_S390)
 265        {
 266                unsigned long caller;
 267
 268                caller = (unsigned long)__builtin_return_address(0);
 269                disabled_wait(caller);
 270        }
 271#endif
 272        local_irq_enable();
 273        for (i = 0; ; i += PANIC_TIMER_STEP) {
 274                touch_softlockup_watchdog();
 275                if (i >= i_next) {
 276                        i += panic_blink(state ^= 1);
 277                        i_next = i + 3600 / PANIC_BLINK_SPD;
 278                }
 279                mdelay(PANIC_TIMER_STEP);
 280        }
 281}
 282
 283EXPORT_SYMBOL(panic);
 284
 285/*
 286 * TAINT_FORCED_RMMOD could be a per-module flag but the module
 287 * is being removed anyway.
 288 */
 289const struct taint_flag taint_flags[TAINT_FLAGS_COUNT] = {
 290        { 'P', 'G', true },     /* TAINT_PROPRIETARY_MODULE */
 291        { 'F', ' ', true },     /* TAINT_FORCED_MODULE */
 292        { 'S', ' ', false },    /* TAINT_CPU_OUT_OF_SPEC */
 293        { 'R', ' ', false },    /* TAINT_FORCED_RMMOD */
 294        { 'M', ' ', false },    /* TAINT_MACHINE_CHECK */
 295        { 'B', ' ', false },    /* TAINT_BAD_PAGE */
 296        { 'U', ' ', false },    /* TAINT_USER */
 297        { 'D', ' ', false },    /* TAINT_DIE */
 298        { 'A', ' ', false },    /* TAINT_OVERRIDDEN_ACPI_TABLE */
 299        { 'W', ' ', false },    /* TAINT_WARN */
 300        { 'C', ' ', true },     /* TAINT_CRAP */
 301        { 'I', ' ', false },    /* TAINT_FIRMWARE_WORKAROUND */
 302        { 'O', ' ', true },     /* TAINT_OOT_MODULE */
 303        { 'E', ' ', true },     /* TAINT_UNSIGNED_MODULE */
 304        { 'L', ' ', false },    /* TAINT_SOFTLOCKUP */
 305        { 'K', ' ', true },     /* TAINT_LIVEPATCH */
 306        { '?', '-', false },    /* TAINT_16 */
 307        { '?', '-', false },    /* TAINT_17 */
 308        { '?', '-', false },    /* TAINT_18 */
 309        { '?', '-', false },    /* TAINT_19 */
 310        { '?', '-', false },    /* TAINT_20 */
 311        { '?', '-', false },    /* TAINT_21 */
 312        { '?', '-', false },    /* TAINT_22 */
 313        { '?', '-', false },    /* TAINT_23 */
 314        { '?', '-', false },    /* TAINT_24 */
 315        { '?', '-', false },    /* TAINT_25 */
 316        { '?', '-', false },    /* TAINT_26 */
 317        { '?', '-', false },    /* TAINT_27 */
 318        { 'H', ' ', false },    /* TAINT_HARDWARE_UNSUPPORTED */
 319        { 'T', ' ', true },     /* TAINT_TECH_PREVIEW */
 320};
 321
 322/**
 323 *      print_tainted - return a string to represent the kernel taint state.
 324 *
 325 *  'P' - Proprietary module has been loaded.
 326 *  'F' - Module has been forcibly loaded.
 327 *  'S' - SMP with CPUs not designed for SMP.
 328 *  'R' - User forced a module unload.
 329 *  'M' - System experienced a machine check exception.
 330 *  'B' - System has hit bad_page.
 331 *  'U' - Userspace-defined naughtiness.
 332 *  'D' - Kernel has oopsed before
 333 *  'A' - ACPI table overridden.
 334 *  'W' - Taint on warning.
 335 *  'C' - modules from drivers/staging are loaded.
 336 *  'I' - Working around severe firmware bug.
 337 *  'O' - Out-of-tree module has been loaded.
 338 *  'E' - Unsigned module has been loaded.
 339 *  'L' - A soft lockup has previously occurred.
 340 *  'K' - Kernel has been live patched.
 341 *
 342 *      The string is overwritten by the next call to print_tainted().
 343 */
 344const char *print_tainted(void)
 345{
 346        static char buf[TAINT_FLAGS_COUNT + sizeof("Tainted: ")];
 347
 348        if (tainted_mask) {
 349                char *s;
 350                int i;
 351
 352                s = buf + sprintf(buf, "Tainted: ");
 353                for (i = 0; i < TAINT_FLAGS_COUNT; i++) {
 354                        const struct taint_flag *t = &taint_flags[i];
 355                        *s++ = test_bit(i, &tainted_mask) ?
 356                                        t->c_true : t->c_false;
 357                }
 358                *s = 0;
 359        } else
 360                snprintf(buf, sizeof(buf), "Not tainted");
 361
 362        return buf;
 363}
 364
 365int test_taint(unsigned flag)
 366{
 367        return test_bit(flag, &tainted_mask);
 368}
 369EXPORT_SYMBOL(test_taint);
 370
 371unsigned long get_taint(void)
 372{
 373        return tainted_mask;
 374}
 375
 376/**
 377 * add_taint: add a taint flag if not already set.
 378 * @flag: one of the TAINT_* constants.
 379 * @lockdep_ok: whether lock debugging is still OK.
 380 *
 381 * If something bad has gone wrong, you'll want @lockdebug_ok = false, but for
 382 * some notewortht-but-not-corrupting cases, it can be set to true.
 383 */
 384void add_taint(unsigned flag, enum lockdep_ok lockdep_ok)
 385{
 386        if (lockdep_ok == LOCKDEP_NOW_UNRELIABLE && __debug_locks_off())
 387                printk(KERN_WARNING
 388                       "Disabling lock debugging due to kernel taint\n");
 389
 390        set_bit(flag, &tainted_mask);
 391}
 392EXPORT_SYMBOL(add_taint);
 393
 394static void spin_msec(int msecs)
 395{
 396        int i;
 397
 398        for (i = 0; i < msecs; i++) {
 399                touch_nmi_watchdog();
 400                mdelay(1);
 401        }
 402}
 403
 404/*
 405 * It just happens that oops_enter() and oops_exit() are identically
 406 * implemented...
 407 */
 408static void do_oops_enter_exit(void)
 409{
 410        unsigned long flags;
 411        static int spin_counter;
 412
 413        if (!pause_on_oops)
 414                return;
 415
 416        spin_lock_irqsave(&pause_on_oops_lock, flags);
 417        if (pause_on_oops_flag == 0) {
 418                /* This CPU may now print the oops message */
 419                pause_on_oops_flag = 1;
 420        } else {
 421                /* We need to stall this CPU */
 422                if (!spin_counter) {
 423                        /* This CPU gets to do the counting */
 424                        spin_counter = pause_on_oops;
 425                        do {
 426                                spin_unlock(&pause_on_oops_lock);
 427                                spin_msec(MSEC_PER_SEC);
 428                                spin_lock(&pause_on_oops_lock);
 429                        } while (--spin_counter);
 430                        pause_on_oops_flag = 0;
 431                } else {
 432                        /* This CPU waits for a different one */
 433                        while (spin_counter) {
 434                                spin_unlock(&pause_on_oops_lock);
 435                                spin_msec(1);
 436                                spin_lock(&pause_on_oops_lock);
 437                        }
 438                }
 439        }
 440        spin_unlock_irqrestore(&pause_on_oops_lock, flags);
 441}
 442
 443/*
 444 * Return true if the calling CPU is allowed to print oops-related info.
 445 * This is a bit racy..
 446 */
 447int oops_may_print(void)
 448{
 449        return pause_on_oops_flag == 0;
 450}
 451
 452/*
 453 * Called when the architecture enters its oops handler, before it prints
 454 * anything.  If this is the first CPU to oops, and it's oopsing the first
 455 * time then let it proceed.
 456 *
 457 * This is all enabled by the pause_on_oops kernel boot option.  We do all
 458 * this to ensure that oopses don't scroll off the screen.  It has the
 459 * side-effect of preventing later-oopsing CPUs from mucking up the display,
 460 * too.
 461 *
 462 * It turns out that the CPU which is allowed to print ends up pausing for
 463 * the right duration, whereas all the other CPUs pause for twice as long:
 464 * once in oops_enter(), once in oops_exit().
 465 */
 466void oops_enter(void)
 467{
 468        tracing_off();
 469        /* can't trust the integrity of the kernel anymore: */
 470        debug_locks_off();
 471        do_oops_enter_exit();
 472}
 473
 474/*
 475 * 64-bit random ID for oopses:
 476 */
 477static u64 oops_id;
 478
 479static int init_oops_id(void)
 480{
 481        if (!oops_id)
 482                get_random_bytes(&oops_id, sizeof(oops_id));
 483        else
 484                oops_id++;
 485
 486        return 0;
 487}
 488late_initcall(init_oops_id);
 489
 490void print_oops_end_marker(void)
 491{
 492        init_oops_id();
 493        printk(KERN_WARNING "---[ end trace %016llx ]---\n",
 494                (unsigned long long)oops_id);
 495}
 496
 497/*
 498 * Called when the architecture exits its oops handler, after printing
 499 * everything.
 500 */
 501void oops_exit(void)
 502{
 503        do_oops_enter_exit();
 504        print_oops_end_marker();
 505        kmsg_dump(KMSG_DUMP_OOPS);
 506}
 507
 508struct warn_args {
 509        const char *fmt;
 510        va_list args;
 511};
 512
 513void __warn(const char *file, int line, void *caller, unsigned taint,
 514            struct pt_regs *regs, struct warn_args *args)
 515{
 516        disable_trace_on_warning();
 517
 518        pr_warn("------------[ cut here ]------------\n");
 519
 520        if (file)
 521                pr_warn("WARNING: CPU: %d PID: %d at %s:%d %pS\n",
 522                        raw_smp_processor_id(), current->pid, file, line,
 523                        caller);
 524        else
 525                pr_warn("WARNING: CPU: %d PID: %d at %pS\n",
 526                        raw_smp_processor_id(), current->pid, caller);
 527
 528        if (args)
 529                vprintk(args->fmt, args->args);
 530
 531        if (panic_on_warn) {
 532                /*
 533                 * This thread may hit another WARN() in the panic path.
 534                 * Resetting this prevents additional WARN() from panicking the
 535                 * system on this thread.  Other threads are blocked by the
 536                 * panic_mutex in panic().
 537                 */
 538                panic_on_warn = 0;
 539                panic("panic_on_warn set ...\n");
 540        }
 541
 542        print_modules();
 543
 544        if (regs)
 545                show_regs(regs);
 546        else
 547                dump_stack();
 548
 549        print_oops_end_marker();
 550
 551        /* Just a warning, don't kill lockdep. */
 552        add_taint(taint, LOCKDEP_STILL_OK);
 553}
 554
 555#ifdef WANT_WARN_ON_SLOWPATH
 556void warn_slowpath_fmt(const char *file, int line, const char *fmt, ...)
 557{
 558        struct warn_args args;
 559
 560        args.fmt = fmt;
 561        va_start(args.args, fmt);
 562        __warn(file, line, __builtin_return_address(0), TAINT_WARN, NULL,
 563               &args);
 564        va_end(args.args);
 565}
 566EXPORT_SYMBOL(warn_slowpath_fmt);
 567
 568void warn_slowpath_fmt_taint(const char *file, int line,
 569                             unsigned taint, const char *fmt, ...)
 570{
 571        struct warn_args args;
 572
 573        args.fmt = fmt;
 574        va_start(args.args, fmt);
 575        __warn(file, line, __builtin_return_address(0), taint, NULL, &args);
 576        va_end(args.args);
 577}
 578EXPORT_SYMBOL(warn_slowpath_fmt_taint);
 579
 580void warn_slowpath_null(const char *file, int line)
 581{
 582        __warn(file, line, __builtin_return_address(0), TAINT_WARN, NULL, NULL);
 583}
 584EXPORT_SYMBOL(warn_slowpath_null);
 585#endif
 586
 587#ifdef CONFIG_CC_STACKPROTECTOR
 588
 589/*
 590 * Called when gcc's -fstack-protector feature is used, and
 591 * gcc detects corruption of the on-stack canary value
 592 */
 593void __stack_chk_fail(void)
 594{
 595        panic("stack-protector: Kernel stack is corrupted in: %p\n",
 596                __builtin_return_address(0));
 597}
 598EXPORT_SYMBOL(__stack_chk_fail);
 599
 600#endif
 601
 602core_param(panic, panic_timeout, int, 0644);
 603core_param(pause_on_oops, pause_on_oops, int, 0644);
 604core_param(panic_on_warn, panic_on_warn, int, 0644);
 605core_param(crash_kexec_post_notifiers, crash_kexec_post_notifiers, bool, 0644);
 606
 607static int __init oops_setup(char *s)
 608{
 609        if (!s)
 610                return -EINVAL;
 611        if (!strcmp(s, "panic"))
 612                panic_on_oops = 1;
 613        return 0;
 614}
 615early_param("oops", oops_setup);
 616