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