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