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