1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * linux/kernel/panic.c 4 * 5 * Copyright (C) 1991, 1992 Linus Torvalds 6 */ 7 8 /* 9 * This function is used through-out the kernel (including mm and fs) 10 * to indicate a major problem. 11 */ 12 #include <linux/debug_locks.h> 13 #include <linux/sched/debug.h> 14 #include <linux/interrupt.h> 15 #include <linux/kgdb.h> 16 #include <linux/kmsg_dump.h> 17 #include <linux/kallsyms.h> 18 #include <linux/notifier.h> 19 #include <linux/vt_kern.h> 20 #include <linux/module.h> 21 #include <linux/random.h> 22 #include <linux/ftrace.h> 23 #include <linux/reboot.h> 24 #include <linux/delay.h> 25 #include <linux/kexec.h> 26 #include <linux/panic_notifier.h> 27 #include <linux/sched.h> 28 #include <linux/string_helpers.h> 29 #include <linux/sysrq.h> 30 #include <linux/init.h> 31 #include <linux/nmi.h> 32 #include <linux/console.h> 33 #include <linux/bug.h> 34 #include <linux/ratelimit.h> 35 #include <linux/debugfs.h> 36 #include <linux/sysfs.h> 37 #include <linux/context_tracking.h> 38 #include <linux/seq_buf.h> 39 #include <trace/events/error_report.h> 40 #include <asm/sections.h> 41 42 #define PANIC_TIMER_STEP 100 43 #define PANIC_BLINK_SPD 18 44 45 #ifdef CONFIG_SMP 46 /* 47 * Should we dump all CPUs backtraces in an oops event? 48 * Defaults to 0, can be changed via sysctl. 49 */ 50 static unsigned int __read_mostly sysctl_oops_all_cpu_backtrace; 51 #else 52 #define sysctl_oops_all_cpu_backtrace 0 53 #endif /* CONFIG_SMP */ 54 55 int panic_on_oops = CONFIG_PANIC_ON_OOPS_VALUE; 56 static unsigned long tainted_mask = 57 IS_ENABLED(CONFIG_RANDSTRUCT) ? (1 << TAINT_RANDSTRUCT) : 0; 58 static int pause_on_oops; 59 static int pause_on_oops_flag; 60 static DEFINE_SPINLOCK(pause_on_oops_lock); 61 bool crash_kexec_post_notifiers; 62 int panic_on_warn __read_mostly; 63 unsigned long panic_on_taint; 64 bool panic_on_taint_nousertaint = false; 65 static unsigned int warn_limit __read_mostly; 66 67 bool panic_triggering_all_cpu_backtrace; 68 69 int panic_timeout = CONFIG_PANIC_TIMEOUT; 70 EXPORT_SYMBOL_GPL(panic_timeout); 71 72 #define PANIC_PRINT_TASK_INFO 0x00000001 73 #define PANIC_PRINT_MEM_INFO 0x00000002 74 #define PANIC_PRINT_TIMER_INFO 0x00000004 75 #define PANIC_PRINT_LOCK_INFO 0x00000008 76 #define PANIC_PRINT_FTRACE_INFO 0x00000010 77 #define PANIC_PRINT_ALL_PRINTK_MSG 0x00000020 78 #define PANIC_PRINT_ALL_CPU_BT 0x00000040 79 #define PANIC_PRINT_BLOCKED_TASKS 0x00000080 80 unsigned long panic_print; 81 82 ATOMIC_NOTIFIER_HEAD(panic_notifier_list); 83 84 EXPORT_SYMBOL(panic_notifier_list); 85 86 #ifdef CONFIG_SYSCTL 87 88 /* 89 * Taint values can only be increased 90 * This means we can safely use a temporary. 91 */ 92 static int proc_taint(const struct ctl_table *table, int write, 93 void *buffer, size_t *lenp, loff_t *ppos) 94 { 95 struct ctl_table t; 96 unsigned long tmptaint = get_taint(); 97 int err; 98 99 if (write && !capable(CAP_SYS_ADMIN)) 100 return -EPERM; 101 102 t = *table; 103 t.data = &tmptaint; 104 err = proc_doulongvec_minmax(&t, write, buffer, lenp, ppos); 105 if (err < 0) 106 return err; 107 108 if (write) { 109 int i; 110 111 /* 112 * If we are relying on panic_on_taint not producing 113 * false positives due to userspace input, bail out 114 * before setting the requested taint flags. 115 */ 116 if (panic_on_taint_nousertaint && (tmptaint & panic_on_taint)) 117 return -EINVAL; 118 119 /* 120 * Poor man's atomic or. Not worth adding a primitive 121 * to everyone's atomic.h for this 122 */ 123 for (i = 0; i < TAINT_FLAGS_COUNT; i++) 124 if ((1UL << i) & tmptaint) 125 add_taint(i, LOCKDEP_STILL_OK); 126 } 127 128 return err; 129 } 130 131 static const struct ctl_table kern_panic_table[] = { 132 #ifdef CONFIG_SMP 133 { 134 .procname = "oops_all_cpu_backtrace", 135 .data = &sysctl_oops_all_cpu_backtrace, 136 .maxlen = sizeof(int), 137 .mode = 0644, 138 .proc_handler = proc_dointvec_minmax, 139 .extra1 = SYSCTL_ZERO, 140 .extra2 = SYSCTL_ONE, 141 }, 142 #endif 143 { 144 .procname = "tainted", 145 .maxlen = sizeof(long), 146 .mode = 0644, 147 .proc_handler = proc_taint, 148 }, 149 { 150 .procname = "panic", 151 .data = &panic_timeout, 152 .maxlen = sizeof(int), 153 .mode = 0644, 154 .proc_handler = proc_dointvec, 155 }, 156 { 157 .procname = "panic_on_oops", 158 .data = &panic_on_oops, 159 .maxlen = sizeof(int), 160 .mode = 0644, 161 .proc_handler = proc_dointvec, 162 }, 163 { 164 .procname = "panic_print", 165 .data = &panic_print, 166 .maxlen = sizeof(unsigned long), 167 .mode = 0644, 168 .proc_handler = proc_doulongvec_minmax, 169 }, 170 { 171 .procname = "panic_on_warn", 172 .data = &panic_on_warn, 173 .maxlen = sizeof(int), 174 .mode = 0644, 175 .proc_handler = proc_dointvec_minmax, 176 .extra1 = SYSCTL_ZERO, 177 .extra2 = SYSCTL_ONE, 178 }, 179 { 180 .procname = "warn_limit", 181 .data = &warn_limit, 182 .maxlen = sizeof(warn_limit), 183 .mode = 0644, 184 .proc_handler = proc_douintvec, 185 }, 186 #if (defined(CONFIG_X86_32) || defined(CONFIG_PARISC)) && \ 187 defined(CONFIG_DEBUG_STACKOVERFLOW) 188 { 189 .procname = "panic_on_stackoverflow", 190 .data = &sysctl_panic_on_stackoverflow, 191 .maxlen = sizeof(int), 192 .mode = 0644, 193 .proc_handler = proc_dointvec, 194 }, 195 #endif 196 }; 197 198 static __init int kernel_panic_sysctls_init(void) 199 { 200 register_sysctl_init("kernel", kern_panic_table); 201 return 0; 202 } 203 late_initcall(kernel_panic_sysctls_init); 204 #endif 205 206 static atomic_t warn_count = ATOMIC_INIT(0); 207 208 #ifdef CONFIG_SYSFS 209 static ssize_t warn_count_show(struct kobject *kobj, struct kobj_attribute *attr, 210 char *page) 211 { 212 return sysfs_emit(page, "%d\n", atomic_read(&warn_count)); 213 } 214 215 static struct kobj_attribute warn_count_attr = __ATTR_RO(warn_count); 216 217 static __init int kernel_panic_sysfs_init(void) 218 { 219 sysfs_add_file_to_group(kernel_kobj, &warn_count_attr.attr, NULL); 220 return 0; 221 } 222 late_initcall(kernel_panic_sysfs_init); 223 #endif 224 225 static long no_blink(int state) 226 { 227 return 0; 228 } 229 230 /* Returns how long it waited in ms */ 231 long (*panic_blink)(int state); 232 EXPORT_SYMBOL(panic_blink); 233 234 /* 235 * Stop ourself in panic -- architecture code may override this 236 */ 237 void __weak __noreturn panic_smp_self_stop(void) 238 { 239 while (1) 240 cpu_relax(); 241 } 242 243 /* 244 * Stop ourselves in NMI context if another CPU has already panicked. Arch code 245 * may override this to prepare for crash dumping, e.g. save regs info. 246 */ 247 void __weak __noreturn nmi_panic_self_stop(struct pt_regs *regs) 248 { 249 panic_smp_self_stop(); 250 } 251 252 /* 253 * Stop other CPUs in panic. Architecture dependent code may override this 254 * with more suitable version. For example, if the architecture supports 255 * crash dump, it should save registers of each stopped CPU and disable 256 * per-CPU features such as virtualization extensions. 257 */ 258 void __weak crash_smp_send_stop(void) 259 { 260 static int cpus_stopped; 261 262 /* 263 * This function can be called twice in panic path, but obviously 264 * we execute this only once. 265 */ 266 if (cpus_stopped) 267 return; 268 269 /* 270 * Note smp_send_stop is the usual smp shutdown function, which 271 * unfortunately means it may not be hardened to work in a panic 272 * situation. 273 */ 274 smp_send_stop(); 275 cpus_stopped = 1; 276 } 277 278 atomic_t panic_cpu = ATOMIC_INIT(PANIC_CPU_INVALID); 279 280 /* 281 * A variant of panic() called from NMI context. We return if we've already 282 * panicked on this CPU. If another CPU already panicked, loop in 283 * nmi_panic_self_stop() which can provide architecture dependent code such 284 * as saving register state for crash dump. 285 */ 286 void nmi_panic(struct pt_regs *regs, const char *msg) 287 { 288 int old_cpu, this_cpu; 289 290 old_cpu = PANIC_CPU_INVALID; 291 this_cpu = raw_smp_processor_id(); 292 293 /* atomic_try_cmpxchg updates old_cpu on failure */ 294 if (atomic_try_cmpxchg(&panic_cpu, &old_cpu, this_cpu)) 295 panic("%s", msg); 296 else if (old_cpu != this_cpu) 297 nmi_panic_self_stop(regs); 298 } 299 EXPORT_SYMBOL(nmi_panic); 300 301 static void panic_print_sys_info(bool console_flush) 302 { 303 if (console_flush) { 304 if (panic_print & PANIC_PRINT_ALL_PRINTK_MSG) 305 console_flush_on_panic(CONSOLE_REPLAY_ALL); 306 return; 307 } 308 309 if (panic_print & PANIC_PRINT_TASK_INFO) 310 show_state(); 311 312 if (panic_print & PANIC_PRINT_MEM_INFO) 313 show_mem(); 314 315 if (panic_print & PANIC_PRINT_TIMER_INFO) 316 sysrq_timer_list_show(); 317 318 if (panic_print & PANIC_PRINT_LOCK_INFO) 319 debug_show_all_locks(); 320 321 if (panic_print & PANIC_PRINT_FTRACE_INFO) 322 ftrace_dump(DUMP_ALL); 323 324 if (panic_print & PANIC_PRINT_BLOCKED_TASKS) 325 show_state_filter(TASK_UNINTERRUPTIBLE); 326 } 327 328 void check_panic_on_warn(const char *origin) 329 { 330 unsigned int limit; 331 332 if (panic_on_warn) 333 panic("%s: panic_on_warn set ...\n", origin); 334 335 limit = READ_ONCE(warn_limit); 336 if (atomic_inc_return(&warn_count) >= limit && limit) 337 panic("%s: system warned too often (kernel.warn_limit is %d)", 338 origin, limit); 339 } 340 341 /* 342 * Helper that triggers the NMI backtrace (if set in panic_print) 343 * and then performs the secondary CPUs shutdown - we cannot have 344 * the NMI backtrace after the CPUs are off! 345 */ 346 static void panic_other_cpus_shutdown(bool crash_kexec) 347 { 348 if (panic_print & PANIC_PRINT_ALL_CPU_BT) { 349 /* Temporary allow non-panic CPUs to write their backtraces. */ 350 panic_triggering_all_cpu_backtrace = true; 351 trigger_all_cpu_backtrace(); 352 panic_triggering_all_cpu_backtrace = false; 353 } 354 355 /* 356 * Note that smp_send_stop() is the usual SMP shutdown function, 357 * which unfortunately may not be hardened to work in a panic 358 * situation. If we want to do crash dump after notifier calls 359 * and kmsg_dump, we will need architecture dependent extra 360 * bits in addition to stopping other CPUs, hence we rely on 361 * crash_smp_send_stop() for that. 362 */ 363 if (!crash_kexec) 364 smp_send_stop(); 365 else 366 crash_smp_send_stop(); 367 } 368 369 /** 370 * panic - halt the system 371 * @fmt: The text string to print 372 * 373 * Display a message, then perform cleanups. This function never returns. 374 */ 375 void panic(const char *fmt, ...) 376 { 377 static char buf[1024]; 378 va_list args; 379 long i, i_next = 0, len; 380 int state = 0; 381 int old_cpu, this_cpu; 382 bool _crash_kexec_post_notifiers = crash_kexec_post_notifiers; 383 384 if (panic_on_warn) { 385 /* 386 * This thread may hit another WARN() in the panic path. 387 * Resetting this prevents additional WARN() from panicking the 388 * system on this thread. Other threads are blocked by the 389 * panic_mutex in panic(). 390 */ 391 panic_on_warn = 0; 392 } 393 394 /* 395 * Disable local interrupts. This will prevent panic_smp_self_stop 396 * from deadlocking the first cpu that invokes the panic, since 397 * there is nothing to prevent an interrupt handler (that runs 398 * after setting panic_cpu) from invoking panic() again. 399 */ 400 local_irq_disable(); 401 preempt_disable_notrace(); 402 403 /* 404 * It's possible to come here directly from a panic-assertion and 405 * not have preempt disabled. Some functions called from here want 406 * preempt to be disabled. No point enabling it later though... 407 * 408 * Only one CPU is allowed to execute the panic code from here. For 409 * multiple parallel invocations of panic, all other CPUs either 410 * stop themself or will wait until they are stopped by the 1st CPU 411 * with smp_send_stop(). 412 * 413 * cmpxchg success means this is the 1st CPU which comes here, 414 * so go ahead. 415 * `old_cpu == this_cpu' means we came from nmi_panic() which sets 416 * panic_cpu to this CPU. In this case, this is also the 1st CPU. 417 */ 418 old_cpu = PANIC_CPU_INVALID; 419 this_cpu = raw_smp_processor_id(); 420 421 /* atomic_try_cmpxchg updates old_cpu on failure */ 422 if (atomic_try_cmpxchg(&panic_cpu, &old_cpu, this_cpu)) { 423 /* go ahead */ 424 } else if (old_cpu != this_cpu) 425 panic_smp_self_stop(); 426 427 console_verbose(); 428 bust_spinlocks(1); 429 va_start(args, fmt); 430 len = vscnprintf(buf, sizeof(buf), fmt, args); 431 va_end(args); 432 433 if (len && buf[len - 1] == '\n') 434 buf[len - 1] = '\0'; 435 436 pr_emerg("Kernel panic - not syncing: %s\n", buf); 437 #ifdef CONFIG_DEBUG_BUGVERBOSE 438 /* 439 * Avoid nested stack-dumping if a panic occurs during oops processing 440 */ 441 if (!test_taint(TAINT_DIE) && oops_in_progress <= 1) 442 dump_stack(); 443 #endif 444 445 /* 446 * If kgdb is enabled, give it a chance to run before we stop all 447 * the other CPUs or else we won't be able to debug processes left 448 * running on them. 449 */ 450 kgdb_panic(buf); 451 452 /* 453 * If we have crashed and we have a crash kernel loaded let it handle 454 * everything else. 455 * If we want to run this after calling panic_notifiers, pass 456 * the "crash_kexec_post_notifiers" option to the kernel. 457 * 458 * Bypass the panic_cpu check and call __crash_kexec directly. 459 */ 460 if (!_crash_kexec_post_notifiers) 461 __crash_kexec(NULL); 462 463 panic_other_cpus_shutdown(_crash_kexec_post_notifiers); 464 465 printk_legacy_allow_panic_sync(); 466 467 /* 468 * Run any panic handlers, including those that might need to 469 * add information to the kmsg dump output. 470 */ 471 atomic_notifier_call_chain(&panic_notifier_list, 0, buf); 472 473 panic_print_sys_info(false); 474 475 kmsg_dump_desc(KMSG_DUMP_PANIC, buf); 476 477 /* 478 * If you doubt kdump always works fine in any situation, 479 * "crash_kexec_post_notifiers" offers you a chance to run 480 * panic_notifiers and dumping kmsg before kdump. 481 * Note: since some panic_notifiers can make crashed kernel 482 * more unstable, it can increase risks of the kdump failure too. 483 * 484 * Bypass the panic_cpu check and call __crash_kexec directly. 485 */ 486 if (_crash_kexec_post_notifiers) 487 __crash_kexec(NULL); 488 489 console_unblank(); 490 491 /* 492 * We may have ended up stopping the CPU holding the lock (in 493 * smp_send_stop()) while still having some valuable data in the console 494 * buffer. Try to acquire the lock then release it regardless of the 495 * result. The release will also print the buffers out. Locks debug 496 * should be disabled to avoid reporting bad unlock balance when 497 * panic() is not being callled from OOPS. 498 */ 499 debug_locks_off(); 500 console_flush_on_panic(CONSOLE_FLUSH_PENDING); 501 502 panic_print_sys_info(true); 503 504 if (!panic_blink) 505 panic_blink = no_blink; 506 507 if (panic_timeout > 0) { 508 /* 509 * Delay timeout seconds before rebooting the machine. 510 * We can't use the "normal" timers since we just panicked. 511 */ 512 pr_emerg("Rebooting in %d seconds..\n", panic_timeout); 513 514 for (i = 0; i < panic_timeout * 1000; i += PANIC_TIMER_STEP) { 515 touch_nmi_watchdog(); 516 if (i >= i_next) { 517 i += panic_blink(state ^= 1); 518 i_next = i + 3600 / PANIC_BLINK_SPD; 519 } 520 mdelay(PANIC_TIMER_STEP); 521 } 522 } 523 if (panic_timeout != 0) { 524 /* 525 * This will not be a clean reboot, with everything 526 * shutting down. But if there is a chance of 527 * rebooting the system it will be rebooted. 528 */ 529 if (panic_reboot_mode != REBOOT_UNDEFINED) 530 reboot_mode = panic_reboot_mode; 531 emergency_restart(); 532 } 533 #ifdef __sparc__ 534 { 535 extern int stop_a_enabled; 536 /* Make sure the user can actually press Stop-A (L1-A) */ 537 stop_a_enabled = 1; 538 pr_emerg("Press Stop-A (L1-A) from sun keyboard or send break\n" 539 "twice on console to return to the boot prom\n"); 540 } 541 #endif 542 #if defined(CONFIG_S390) 543 disabled_wait(); 544 #endif 545 pr_emerg("---[ end Kernel panic - not syncing: %s ]---\n", buf); 546 547 /* Do not scroll important messages printed above */ 548 suppress_printk = 1; 549 550 /* 551 * The final messages may not have been printed if in a context that 552 * defers printing (such as NMI) and irq_work is not available. 553 * Explicitly flush the kernel log buffer one last time. 554 */ 555 console_flush_on_panic(CONSOLE_FLUSH_PENDING); 556 nbcon_atomic_flush_unsafe(); 557 558 local_irq_enable(); 559 for (i = 0; ; i += PANIC_TIMER_STEP) { 560 touch_softlockup_watchdog(); 561 if (i >= i_next) { 562 i += panic_blink(state ^= 1); 563 i_next = i + 3600 / PANIC_BLINK_SPD; 564 } 565 mdelay(PANIC_TIMER_STEP); 566 } 567 } 568 569 EXPORT_SYMBOL(panic); 570 571 #define TAINT_FLAG(taint, _c_true, _c_false, _module) \ 572 [ TAINT_##taint ] = { \ 573 .c_true = _c_true, .c_false = _c_false, \ 574 .module = _module, \ 575 .desc = #taint, \ 576 } 577 578 /* 579 * TAINT_FORCED_RMMOD could be a per-module flag but the module 580 * is being removed anyway. 581 */ 582 const struct taint_flag taint_flags[TAINT_FLAGS_COUNT] = { 583 TAINT_FLAG(PROPRIETARY_MODULE, 'P', 'G', true), 584 TAINT_FLAG(FORCED_MODULE, 'F', ' ', true), 585 TAINT_FLAG(CPU_OUT_OF_SPEC, 'S', ' ', false), 586 TAINT_FLAG(FORCED_RMMOD, 'R', ' ', false), 587 TAINT_FLAG(MACHINE_CHECK, 'M', ' ', false), 588 TAINT_FLAG(BAD_PAGE, 'B', ' ', false), 589 TAINT_FLAG(USER, 'U', ' ', false), 590 TAINT_FLAG(DIE, 'D', ' ', false), 591 TAINT_FLAG(OVERRIDDEN_ACPI_TABLE, 'A', ' ', false), 592 TAINT_FLAG(WARN, 'W', ' ', false), 593 TAINT_FLAG(CRAP, 'C', ' ', true), 594 TAINT_FLAG(FIRMWARE_WORKAROUND, 'I', ' ', false), 595 TAINT_FLAG(OOT_MODULE, 'O', ' ', true), 596 TAINT_FLAG(UNSIGNED_MODULE, 'E', ' ', true), 597 TAINT_FLAG(SOFTLOCKUP, 'L', ' ', false), 598 TAINT_FLAG(LIVEPATCH, 'K', ' ', true), 599 TAINT_FLAG(AUX, 'X', ' ', true), 600 TAINT_FLAG(RANDSTRUCT, 'T', ' ', true), 601 TAINT_FLAG(TEST, 'N', ' ', true), 602 TAINT_FLAG(FWCTL, 'J', ' ', true), 603 }; 604 605 #undef TAINT_FLAG 606 607 static void print_tainted_seq(struct seq_buf *s, bool verbose) 608 { 609 const char *sep = ""; 610 int i; 611 612 if (!tainted_mask) { 613 seq_buf_puts(s, "Not tainted"); 614 return; 615 } 616 617 seq_buf_printf(s, "Tainted: "); 618 for (i = 0; i < TAINT_FLAGS_COUNT; i++) { 619 const struct taint_flag *t = &taint_flags[i]; 620 bool is_set = test_bit(i, &tainted_mask); 621 char c = is_set ? t->c_true : t->c_false; 622 623 if (verbose) { 624 if (is_set) { 625 seq_buf_printf(s, "%s[%c]=%s", sep, c, t->desc); 626 sep = ", "; 627 } 628 } else { 629 seq_buf_putc(s, c); 630 } 631 } 632 } 633 634 static const char *_print_tainted(bool verbose) 635 { 636 /* FIXME: what should the size be? */ 637 static char buf[sizeof(taint_flags)]; 638 struct seq_buf s; 639 640 BUILD_BUG_ON(ARRAY_SIZE(taint_flags) != TAINT_FLAGS_COUNT); 641 642 seq_buf_init(&s, buf, sizeof(buf)); 643 644 print_tainted_seq(&s, verbose); 645 646 return seq_buf_str(&s); 647 } 648 649 /** 650 * print_tainted - return a string to represent the kernel taint state. 651 * 652 * For individual taint flag meanings, see Documentation/admin-guide/sysctl/kernel.rst 653 * 654 * The string is overwritten by the next call to print_tainted(), 655 * but is always NULL terminated. 656 */ 657 const char *print_tainted(void) 658 { 659 return _print_tainted(false); 660 } 661 662 /** 663 * print_tainted_verbose - A more verbose version of print_tainted() 664 */ 665 const char *print_tainted_verbose(void) 666 { 667 return _print_tainted(true); 668 } 669 670 int test_taint(unsigned flag) 671 { 672 return test_bit(flag, &tainted_mask); 673 } 674 EXPORT_SYMBOL(test_taint); 675 676 unsigned long get_taint(void) 677 { 678 return tainted_mask; 679 } 680 681 /** 682 * add_taint: add a taint flag if not already set. 683 * @flag: one of the TAINT_* constants. 684 * @lockdep_ok: whether lock debugging is still OK. 685 * 686 * If something bad has gone wrong, you'll want @lockdebug_ok = false, but for 687 * some notewortht-but-not-corrupting cases, it can be set to true. 688 */ 689 void add_taint(unsigned flag, enum lockdep_ok lockdep_ok) 690 { 691 if (lockdep_ok == LOCKDEP_NOW_UNRELIABLE && __debug_locks_off()) 692 pr_warn("Disabling lock debugging due to kernel taint\n"); 693 694 set_bit(flag, &tainted_mask); 695 696 if (tainted_mask & panic_on_taint) { 697 panic_on_taint = 0; 698 panic("panic_on_taint set ..."); 699 } 700 } 701 EXPORT_SYMBOL(add_taint); 702 703 static void spin_msec(int msecs) 704 { 705 int i; 706 707 for (i = 0; i < msecs; i++) { 708 touch_nmi_watchdog(); 709 mdelay(1); 710 } 711 } 712 713 /* 714 * It just happens that oops_enter() and oops_exit() are identically 715 * implemented... 716 */ 717 static void do_oops_enter_exit(void) 718 { 719 unsigned long flags; 720 static int spin_counter; 721 722 if (!pause_on_oops) 723 return; 724 725 spin_lock_irqsave(&pause_on_oops_lock, flags); 726 if (pause_on_oops_flag == 0) { 727 /* This CPU may now print the oops message */ 728 pause_on_oops_flag = 1; 729 } else { 730 /* We need to stall this CPU */ 731 if (!spin_counter) { 732 /* This CPU gets to do the counting */ 733 spin_counter = pause_on_oops; 734 do { 735 spin_unlock(&pause_on_oops_lock); 736 spin_msec(MSEC_PER_SEC); 737 spin_lock(&pause_on_oops_lock); 738 } while (--spin_counter); 739 pause_on_oops_flag = 0; 740 } else { 741 /* This CPU waits for a different one */ 742 while (spin_counter) { 743 spin_unlock(&pause_on_oops_lock); 744 spin_msec(1); 745 spin_lock(&pause_on_oops_lock); 746 } 747 } 748 } 749 spin_unlock_irqrestore(&pause_on_oops_lock, flags); 750 } 751 752 /* 753 * Return true if the calling CPU is allowed to print oops-related info. 754 * This is a bit racy.. 755 */ 756 bool oops_may_print(void) 757 { 758 return pause_on_oops_flag == 0; 759 } 760 761 /* 762 * Called when the architecture enters its oops handler, before it prints 763 * anything. If this is the first CPU to oops, and it's oopsing the first 764 * time then let it proceed. 765 * 766 * This is all enabled by the pause_on_oops kernel boot option. We do all 767 * this to ensure that oopses don't scroll off the screen. It has the 768 * side-effect of preventing later-oopsing CPUs from mucking up the display, 769 * too. 770 * 771 * It turns out that the CPU which is allowed to print ends up pausing for 772 * the right duration, whereas all the other CPUs pause for twice as long: 773 * once in oops_enter(), once in oops_exit(). 774 */ 775 void oops_enter(void) 776 { 777 nbcon_cpu_emergency_enter(); 778 tracing_off(); 779 /* can't trust the integrity of the kernel anymore: */ 780 debug_locks_off(); 781 do_oops_enter_exit(); 782 783 if (sysctl_oops_all_cpu_backtrace) 784 trigger_all_cpu_backtrace(); 785 } 786 787 static void print_oops_end_marker(void) 788 { 789 pr_warn("---[ end trace %016llx ]---\n", 0ULL); 790 } 791 792 /* 793 * Called when the architecture exits its oops handler, after printing 794 * everything. 795 */ 796 void oops_exit(void) 797 { 798 do_oops_enter_exit(); 799 print_oops_end_marker(); 800 nbcon_cpu_emergency_exit(); 801 kmsg_dump(KMSG_DUMP_OOPS); 802 } 803 804 struct warn_args { 805 const char *fmt; 806 va_list args; 807 }; 808 809 void __warn(const char *file, int line, void *caller, unsigned taint, 810 struct pt_regs *regs, struct warn_args *args) 811 { 812 nbcon_cpu_emergency_enter(); 813 814 disable_trace_on_warning(); 815 816 if (file) 817 pr_warn("WARNING: CPU: %d PID: %d at %s:%d %pS\n", 818 raw_smp_processor_id(), current->pid, file, line, 819 caller); 820 else 821 pr_warn("WARNING: CPU: %d PID: %d at %pS\n", 822 raw_smp_processor_id(), current->pid, caller); 823 824 #pragma GCC diagnostic push 825 #ifndef __clang__ 826 #pragma GCC diagnostic ignored "-Wsuggest-attribute=format" 827 #endif 828 if (args) 829 vprintk(args->fmt, args->args); 830 #pragma GCC diagnostic pop 831 832 print_modules(); 833 834 if (regs) 835 show_regs(regs); 836 837 check_panic_on_warn("kernel"); 838 839 if (!regs) 840 dump_stack(); 841 842 print_irqtrace_events(current); 843 844 print_oops_end_marker(); 845 trace_error_report_end(ERROR_DETECTOR_WARN, (unsigned long)caller); 846 847 /* Just a warning, don't kill lockdep. */ 848 add_taint(taint, LOCKDEP_STILL_OK); 849 850 nbcon_cpu_emergency_exit(); 851 } 852 853 #ifdef CONFIG_BUG 854 #ifndef __WARN_FLAGS 855 void warn_slowpath_fmt(const char *file, int line, unsigned taint, 856 const char *fmt, ...) 857 { 858 bool rcu = warn_rcu_enter(); 859 struct warn_args args; 860 861 pr_warn(CUT_HERE); 862 863 if (!fmt) { 864 __warn(file, line, __builtin_return_address(0), taint, 865 NULL, NULL); 866 warn_rcu_exit(rcu); 867 return; 868 } 869 870 args.fmt = fmt; 871 va_start(args.args, fmt); 872 __warn(file, line, __builtin_return_address(0), taint, NULL, &args); 873 va_end(args.args); 874 warn_rcu_exit(rcu); 875 } 876 EXPORT_SYMBOL(warn_slowpath_fmt); 877 #else 878 void __warn_printk(const char *fmt, ...) 879 { 880 bool rcu = warn_rcu_enter(); 881 va_list args; 882 883 pr_warn(CUT_HERE); 884 885 va_start(args, fmt); 886 vprintk(fmt, args); 887 va_end(args); 888 warn_rcu_exit(rcu); 889 } 890 EXPORT_SYMBOL(__warn_printk); 891 #endif 892 893 /* Support resetting WARN*_ONCE state */ 894 895 static int clear_warn_once_set(void *data, u64 val) 896 { 897 generic_bug_clear_once(); 898 memset(__start_once, 0, __end_once - __start_once); 899 return 0; 900 } 901 902 DEFINE_DEBUGFS_ATTRIBUTE(clear_warn_once_fops, NULL, clear_warn_once_set, 903 "%lld\n"); 904 905 static __init int register_warn_debugfs(void) 906 { 907 /* Don't care about failure */ 908 debugfs_create_file_unsafe("clear_warn_once", 0200, NULL, NULL, 909 &clear_warn_once_fops); 910 return 0; 911 } 912 913 device_initcall(register_warn_debugfs); 914 #endif 915 916 #ifdef CONFIG_STACKPROTECTOR 917 918 /* 919 * Called when gcc's -fstack-protector feature is used, and 920 * gcc detects corruption of the on-stack canary value 921 */ 922 __visible noinstr void __stack_chk_fail(void) 923 { 924 unsigned long flags; 925 926 instrumentation_begin(); 927 flags = user_access_save(); 928 929 panic("stack-protector: Kernel stack is corrupted in: %pB", 930 __builtin_return_address(0)); 931 932 user_access_restore(flags); 933 instrumentation_end(); 934 } 935 EXPORT_SYMBOL(__stack_chk_fail); 936 937 #endif 938 939 core_param(panic, panic_timeout, int, 0644); 940 core_param(panic_print, panic_print, ulong, 0644); 941 core_param(pause_on_oops, pause_on_oops, int, 0644); 942 core_param(panic_on_warn, panic_on_warn, int, 0644); 943 core_param(crash_kexec_post_notifiers, crash_kexec_post_notifiers, bool, 0644); 944 945 static int __init oops_setup(char *s) 946 { 947 if (!s) 948 return -EINVAL; 949 if (!strcmp(s, "panic")) 950 panic_on_oops = 1; 951 return 0; 952 } 953 early_param("oops", oops_setup); 954 955 static int __init panic_on_taint_setup(char *s) 956 { 957 char *taint_str; 958 959 if (!s) 960 return -EINVAL; 961 962 taint_str = strsep(&s, ","); 963 if (kstrtoul(taint_str, 16, &panic_on_taint)) 964 return -EINVAL; 965 966 /* make sure panic_on_taint doesn't hold out-of-range TAINT flags */ 967 panic_on_taint &= TAINT_FLAGS_MAX; 968 969 if (!panic_on_taint) 970 return -EINVAL; 971 972 if (s && !strcmp(s, "nousertaint")) 973 panic_on_taint_nousertaint = true; 974 975 pr_info("panic_on_taint: bitmask=0x%lx nousertaint_mode=%s\n", 976 panic_on_taint, str_enabled_disabled(panic_on_taint_nousertaint)); 977 978 return 0; 979 } 980 early_param("panic_on_taint", panic_on_taint_setup); 981