.. | .. |
---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
---|
1 | 2 | /* |
---|
2 | 3 | * linux/kernel/panic.c |
---|
3 | 4 | * |
---|
.. | .. |
---|
11 | 12 | #include <linux/debug_locks.h> |
---|
12 | 13 | #include <linux/sched/debug.h> |
---|
13 | 14 | #include <linux/interrupt.h> |
---|
| 15 | +#include <linux/kgdb.h> |
---|
14 | 16 | #include <linux/kmsg_dump.h> |
---|
15 | 17 | #include <linux/kallsyms.h> |
---|
16 | 18 | #include <linux/notifier.h> |
---|
.. | .. |
---|
34 | 36 | #define PANIC_TIMER_STEP 100 |
---|
35 | 37 | #define PANIC_BLINK_SPD 18 |
---|
36 | 38 | |
---|
| 39 | +#ifdef CONFIG_SMP |
---|
| 40 | +/* |
---|
| 41 | + * Should we dump all CPUs backtraces in an oops event? |
---|
| 42 | + * Defaults to 0, can be changed via sysctl. |
---|
| 43 | + */ |
---|
| 44 | +unsigned int __read_mostly sysctl_oops_all_cpu_backtrace; |
---|
| 45 | +#endif /* CONFIG_SMP */ |
---|
| 46 | + |
---|
37 | 47 | int panic_on_oops = CONFIG_PANIC_ON_OOPS_VALUE; |
---|
38 | 48 | static unsigned long tainted_mask = |
---|
39 | 49 | IS_ENABLED(CONFIG_GCC_PLUGIN_RANDSTRUCT) ? (1 << TAINT_RANDSTRUCT) : 0; |
---|
.. | .. |
---|
42 | 52 | static DEFINE_SPINLOCK(pause_on_oops_lock); |
---|
43 | 53 | bool crash_kexec_post_notifiers; |
---|
44 | 54 | int panic_on_warn __read_mostly; |
---|
| 55 | +unsigned long panic_on_taint; |
---|
| 56 | +bool panic_on_taint_nousertaint = false; |
---|
45 | 57 | |
---|
46 | 58 | int panic_timeout = CONFIG_PANIC_TIMEOUT; |
---|
47 | 59 | EXPORT_SYMBOL_GPL(panic_timeout); |
---|
48 | 60 | |
---|
49 | | -ATOMIC_NOTIFIER_HEAD(panic_notifier_list); |
---|
50 | | -EXPORT_SYMBOL(panic_notifier_list); |
---|
| 61 | +#define PANIC_PRINT_TASK_INFO 0x00000001 |
---|
| 62 | +#define PANIC_PRINT_MEM_INFO 0x00000002 |
---|
| 63 | +#define PANIC_PRINT_TIMER_INFO 0x00000004 |
---|
| 64 | +#define PANIC_PRINT_LOCK_INFO 0x00000008 |
---|
| 65 | +#define PANIC_PRINT_FTRACE_INFO 0x00000010 |
---|
| 66 | +#define PANIC_PRINT_ALL_PRINTK_MSG 0x00000020 |
---|
| 67 | +unsigned long panic_print; |
---|
51 | 68 | |
---|
52 | | -void (*vendor_panic_cb)(u64 sp); |
---|
53 | | -EXPORT_SYMBOL_GPL(vendor_panic_cb); |
---|
| 69 | +ATOMIC_NOTIFIER_HEAD(panic_notifier_list); |
---|
| 70 | + |
---|
| 71 | +EXPORT_SYMBOL(panic_notifier_list); |
---|
54 | 72 | |
---|
55 | 73 | static long no_blink(int state) |
---|
56 | 74 | { |
---|
.. | .. |
---|
127 | 145 | } |
---|
128 | 146 | EXPORT_SYMBOL(nmi_panic); |
---|
129 | 147 | |
---|
| 148 | +static void panic_print_sys_info(void) |
---|
| 149 | +{ |
---|
| 150 | + if (panic_print & PANIC_PRINT_ALL_PRINTK_MSG) |
---|
| 151 | + console_flush_on_panic(CONSOLE_REPLAY_ALL); |
---|
| 152 | + |
---|
| 153 | + if (panic_print & PANIC_PRINT_TASK_INFO) |
---|
| 154 | + show_state(); |
---|
| 155 | + |
---|
| 156 | + if (panic_print & PANIC_PRINT_MEM_INFO) |
---|
| 157 | + show_mem(0, NULL); |
---|
| 158 | + |
---|
| 159 | + if (panic_print & PANIC_PRINT_TIMER_INFO) |
---|
| 160 | + sysrq_timer_list_show(); |
---|
| 161 | + |
---|
| 162 | + if (panic_print & PANIC_PRINT_LOCK_INFO) |
---|
| 163 | + debug_show_all_locks(); |
---|
| 164 | + |
---|
| 165 | + if (panic_print & PANIC_PRINT_FTRACE_INFO) |
---|
| 166 | + ftrace_dump(DUMP_ALL); |
---|
| 167 | +} |
---|
| 168 | + |
---|
130 | 169 | /** |
---|
131 | 170 | * panic - halt the system |
---|
132 | 171 | * @fmt: The text string to print |
---|
.. | .. |
---|
138 | 177 | void panic(const char *fmt, ...) |
---|
139 | 178 | { |
---|
140 | 179 | static char buf[1024]; |
---|
| 180 | + va_list args2; |
---|
141 | 181 | va_list args; |
---|
142 | | - long i, i_next = 0; |
---|
| 182 | + long i, i_next = 0, len; |
---|
143 | 183 | int state = 0; |
---|
144 | 184 | int old_cpu, this_cpu; |
---|
145 | 185 | bool _crash_kexec_post_notifiers = crash_kexec_post_notifiers; |
---|
.. | .. |
---|
152 | 192 | */ |
---|
153 | 193 | local_irq_disable(); |
---|
154 | 194 | preempt_disable_notrace(); |
---|
| 195 | + |
---|
| 196 | + console_verbose(); |
---|
| 197 | + pr_emerg("Kernel panic - not syncing:\n"); |
---|
| 198 | + va_start(args2, fmt); |
---|
| 199 | + va_copy(args, args2); |
---|
| 200 | + vprintk(fmt, args2); |
---|
| 201 | + va_end(args2); |
---|
| 202 | +#ifdef CONFIG_DEBUG_BUGVERBOSE |
---|
| 203 | + /* |
---|
| 204 | + * Avoid nested stack-dumping if a panic occurs during oops processing |
---|
| 205 | + */ |
---|
| 206 | + if (!test_taint(TAINT_DIE) && oops_in_progress <= 1) |
---|
| 207 | + dump_stack(); |
---|
| 208 | +#endif |
---|
| 209 | + pr_flush(1000, true); |
---|
155 | 210 | |
---|
156 | 211 | /* |
---|
157 | 212 | * It's possible to come here directly from a panic-assertion and |
---|
.. | .. |
---|
174 | 229 | if (old_cpu != PANIC_CPU_INVALID && old_cpu != this_cpu) |
---|
175 | 230 | panic_smp_self_stop(); |
---|
176 | 231 | |
---|
177 | | - console_verbose(); |
---|
178 | 232 | bust_spinlocks(1); |
---|
179 | | - va_start(args, fmt); |
---|
180 | | - vsnprintf(buf, sizeof(buf), fmt, args); |
---|
| 233 | + len = vscnprintf(buf, sizeof(buf), fmt, args); |
---|
181 | 234 | va_end(args); |
---|
182 | | - if (vendor_panic_cb) |
---|
183 | | - vendor_panic_cb(0); |
---|
184 | | - pr_emerg("Kernel panic - not syncing: %s\n", buf); |
---|
185 | | -#ifdef CONFIG_DEBUG_BUGVERBOSE |
---|
| 235 | + |
---|
| 236 | + if (len && buf[len - 1] == '\n') |
---|
| 237 | + buf[len - 1] = '\0'; |
---|
| 238 | + |
---|
186 | 239 | /* |
---|
187 | | - * Avoid nested stack-dumping if a panic occurs during oops processing |
---|
| 240 | + * If kgdb is enabled, give it a chance to run before we stop all |
---|
| 241 | + * the other CPUs or else we won't be able to debug processes left |
---|
| 242 | + * running on them. |
---|
188 | 243 | */ |
---|
189 | | - if (!test_taint(TAINT_DIE) && oops_in_progress <= 1) |
---|
190 | | - dump_stack(); |
---|
191 | | -#endif |
---|
| 244 | + kgdb_panic(buf); |
---|
192 | 245 | |
---|
193 | 246 | /* |
---|
194 | 247 | * If we have crashed and we have a crash kernel loaded let it handle |
---|
.. | .. |
---|
199 | 252 | * Bypass the panic_cpu check and call __crash_kexec directly. |
---|
200 | 253 | */ |
---|
201 | 254 | if (!_crash_kexec_post_notifiers) { |
---|
202 | | - printk_safe_flush_on_panic(); |
---|
203 | 255 | __crash_kexec(NULL); |
---|
204 | 256 | |
---|
205 | 257 | /* |
---|
.. | .. |
---|
223 | 275 | */ |
---|
224 | 276 | atomic_notifier_call_chain(&panic_notifier_list, 0, buf); |
---|
225 | 277 | |
---|
226 | | - /* Call flush even twice. It tries harder with a single online CPU */ |
---|
227 | | - printk_safe_flush_on_panic(); |
---|
228 | 278 | kmsg_dump(KMSG_DUMP_PANIC); |
---|
229 | 279 | |
---|
230 | 280 | /* |
---|
.. | .. |
---|
253 | 303 | * panic() is not being callled from OOPS. |
---|
254 | 304 | */ |
---|
255 | 305 | debug_locks_off(); |
---|
256 | | - console_flush_on_panic(); |
---|
| 306 | + console_flush_on_panic(CONSOLE_FLUSH_PENDING); |
---|
| 307 | + |
---|
| 308 | + panic_print_sys_info(); |
---|
257 | 309 | |
---|
258 | 310 | if (!panic_blink) |
---|
259 | 311 | panic_blink = no_blink; |
---|
.. | .. |
---|
294 | 346 | } |
---|
295 | 347 | #endif |
---|
296 | 348 | #if defined(CONFIG_S390) |
---|
297 | | - { |
---|
298 | | - unsigned long caller; |
---|
299 | | - |
---|
300 | | - caller = (unsigned long)__builtin_return_address(0); |
---|
301 | | - disabled_wait(caller); |
---|
302 | | - } |
---|
| 349 | + disabled_wait(); |
---|
303 | 350 | #endif |
---|
304 | 351 | pr_emerg("---[ end Kernel panic - not syncing: %s ]---\n", buf); |
---|
| 352 | + |
---|
| 353 | + /* Do not scroll important messages printed above */ |
---|
| 354 | + suppress_printk = 1; |
---|
305 | 355 | local_irq_enable(); |
---|
306 | 356 | for (i = 0; ; i += PANIC_TIMER_STEP) { |
---|
307 | 357 | touch_softlockup_watchdog(); |
---|
.. | .. |
---|
343 | 393 | /** |
---|
344 | 394 | * print_tainted - return a string to represent the kernel taint state. |
---|
345 | 395 | * |
---|
346 | | - * For individual taint flag meanings, see Documentation/sysctl/kernel.txt |
---|
| 396 | + * For individual taint flag meanings, see Documentation/admin-guide/sysctl/kernel.rst |
---|
347 | 397 | * |
---|
348 | 398 | * The string is overwritten by the next call to print_tainted(), |
---|
349 | 399 | * but is always NULL terminated. |
---|
.. | .. |
---|
396 | 446 | pr_warn("Disabling lock debugging due to kernel taint\n"); |
---|
397 | 447 | |
---|
398 | 448 | set_bit(flag, &tainted_mask); |
---|
| 449 | + |
---|
| 450 | + if (tainted_mask & panic_on_taint) { |
---|
| 451 | + panic_on_taint = 0; |
---|
| 452 | + panic("panic_on_taint set ..."); |
---|
| 453 | + } |
---|
399 | 454 | } |
---|
400 | 455 | EXPORT_SYMBOL(add_taint); |
---|
401 | 456 | |
---|
.. | .. |
---|
452 | 507 | * Return true if the calling CPU is allowed to print oops-related info. |
---|
453 | 508 | * This is a bit racy.. |
---|
454 | 509 | */ |
---|
455 | | -int oops_may_print(void) |
---|
| 510 | +bool oops_may_print(void) |
---|
456 | 511 | { |
---|
457 | 512 | return pause_on_oops_flag == 0; |
---|
458 | 513 | } |
---|
.. | .. |
---|
477 | 532 | /* can't trust the integrity of the kernel anymore: */ |
---|
478 | 533 | debug_locks_off(); |
---|
479 | 534 | do_oops_enter_exit(); |
---|
| 535 | + |
---|
| 536 | + if (sysctl_oops_all_cpu_backtrace) |
---|
| 537 | + trigger_all_cpu_backtrace(); |
---|
480 | 538 | } |
---|
481 | 539 | |
---|
482 | 540 | /* |
---|
.. | .. |
---|
486 | 544 | |
---|
487 | 545 | static int init_oops_id(void) |
---|
488 | 546 | { |
---|
489 | | -#ifndef CONFIG_PREEMPT_RT_FULL |
---|
| 547 | +#ifndef CONFIG_PREEMPT_RT |
---|
490 | 548 | if (!oops_id) |
---|
491 | 549 | get_random_bytes(&oops_id, sizeof(oops_id)); |
---|
492 | 550 | else |
---|
.. | .. |
---|
497 | 555 | } |
---|
498 | 556 | late_initcall(init_oops_id); |
---|
499 | 557 | |
---|
500 | | -void print_oops_end_marker(void) |
---|
| 558 | +static void print_oops_end_marker(void) |
---|
501 | 559 | { |
---|
502 | 560 | init_oops_id(); |
---|
503 | 561 | pr_warn("---[ end trace %016llx ]---\n", (unsigned long long)oops_id); |
---|
| 562 | + pr_flush(1000, true); |
---|
504 | 563 | } |
---|
505 | 564 | |
---|
506 | 565 | /* |
---|
.. | .. |
---|
524 | 583 | { |
---|
525 | 584 | disable_trace_on_warning(); |
---|
526 | 585 | |
---|
527 | | - if (args) |
---|
528 | | - pr_warn(CUT_HERE); |
---|
529 | | - |
---|
530 | 586 | if (file) |
---|
531 | 587 | pr_warn("WARNING: CPU: %d PID: %d at %s:%d %pS\n", |
---|
532 | 588 | raw_smp_processor_id(), current->pid, file, line, |
---|
.. | .. |
---|
537 | 593 | |
---|
538 | 594 | if (args) |
---|
539 | 595 | vprintk(args->fmt, args->args); |
---|
| 596 | + |
---|
| 597 | + print_modules(); |
---|
| 598 | + |
---|
| 599 | + if (regs) |
---|
| 600 | + show_regs(regs); |
---|
540 | 601 | |
---|
541 | 602 | if (panic_on_warn) { |
---|
542 | 603 | /* |
---|
.. | .. |
---|
549 | 610 | panic("panic_on_warn set ...\n"); |
---|
550 | 611 | } |
---|
551 | 612 | |
---|
552 | | - print_modules(); |
---|
553 | | - |
---|
554 | | - if (regs) |
---|
555 | | - show_regs(regs); |
---|
556 | | - else |
---|
| 613 | + if (!regs) |
---|
557 | 614 | dump_stack(); |
---|
558 | 615 | |
---|
559 | 616 | print_irqtrace_events(current); |
---|
.. | .. |
---|
564 | 621 | add_taint(taint, LOCKDEP_STILL_OK); |
---|
565 | 622 | } |
---|
566 | 623 | |
---|
567 | | -#ifdef WANT_WARN_ON_SLOWPATH |
---|
568 | | -void warn_slowpath_fmt(const char *file, int line, const char *fmt, ...) |
---|
| 624 | +#ifndef __WARN_FLAGS |
---|
| 625 | +void warn_slowpath_fmt(const char *file, int line, unsigned taint, |
---|
| 626 | + const char *fmt, ...) |
---|
569 | 627 | { |
---|
570 | 628 | struct warn_args args; |
---|
571 | 629 | |
---|
572 | | - args.fmt = fmt; |
---|
573 | | - va_start(args.args, fmt); |
---|
574 | | - __warn(file, line, __builtin_return_address(0), TAINT_WARN, NULL, |
---|
575 | | - &args); |
---|
576 | | - va_end(args.args); |
---|
577 | | -} |
---|
578 | | -EXPORT_SYMBOL(warn_slowpath_fmt); |
---|
| 630 | + pr_warn(CUT_HERE); |
---|
579 | 631 | |
---|
580 | | -void warn_slowpath_fmt_taint(const char *file, int line, |
---|
581 | | - unsigned taint, const char *fmt, ...) |
---|
582 | | -{ |
---|
583 | | - struct warn_args args; |
---|
| 632 | + if (!fmt) { |
---|
| 633 | + __warn(file, line, __builtin_return_address(0), taint, |
---|
| 634 | + NULL, NULL); |
---|
| 635 | + return; |
---|
| 636 | + } |
---|
584 | 637 | |
---|
585 | 638 | args.fmt = fmt; |
---|
586 | 639 | va_start(args.args, fmt); |
---|
587 | 640 | __warn(file, line, __builtin_return_address(0), taint, NULL, &args); |
---|
588 | 641 | va_end(args.args); |
---|
589 | 642 | } |
---|
590 | | -EXPORT_SYMBOL(warn_slowpath_fmt_taint); |
---|
591 | | - |
---|
592 | | -void warn_slowpath_null(const char *file, int line) |
---|
593 | | -{ |
---|
594 | | - pr_warn(CUT_HERE); |
---|
595 | | - __warn(file, line, __builtin_return_address(0), TAINT_WARN, NULL, NULL); |
---|
596 | | -} |
---|
597 | | -EXPORT_SYMBOL(warn_slowpath_null); |
---|
| 643 | +EXPORT_SYMBOL(warn_slowpath_fmt); |
---|
598 | 644 | #else |
---|
599 | 645 | void __warn_printk(const char *fmt, ...) |
---|
600 | 646 | { |
---|
.. | .. |
---|
620 | 666 | return 0; |
---|
621 | 667 | } |
---|
622 | 668 | |
---|
623 | | -DEFINE_SIMPLE_ATTRIBUTE(clear_warn_once_fops, |
---|
624 | | - NULL, |
---|
625 | | - clear_warn_once_set, |
---|
626 | | - "%lld\n"); |
---|
| 669 | +DEFINE_DEBUGFS_ATTRIBUTE(clear_warn_once_fops, NULL, clear_warn_once_set, |
---|
| 670 | + "%lld\n"); |
---|
627 | 671 | |
---|
628 | 672 | static __init int register_warn_debugfs(void) |
---|
629 | 673 | { |
---|
630 | 674 | /* Don't care about failure */ |
---|
631 | | - debugfs_create_file("clear_warn_once", 0200, NULL, |
---|
632 | | - NULL, &clear_warn_once_fops); |
---|
| 675 | + debugfs_create_file_unsafe("clear_warn_once", 0200, NULL, NULL, |
---|
| 676 | + &clear_warn_once_fops); |
---|
633 | 677 | return 0; |
---|
634 | 678 | } |
---|
635 | 679 | |
---|
.. | .. |
---|
642 | 686 | * Called when gcc's -fstack-protector feature is used, and |
---|
643 | 687 | * gcc detects corruption of the on-stack canary value |
---|
644 | 688 | */ |
---|
645 | | -__visible void __stack_chk_fail(void) |
---|
| 689 | +__visible noinstr void __stack_chk_fail(void) |
---|
646 | 690 | { |
---|
| 691 | + instrumentation_begin(); |
---|
647 | 692 | panic("stack-protector: Kernel stack is corrupted in: %pB", |
---|
648 | 693 | __builtin_return_address(0)); |
---|
| 694 | + instrumentation_end(); |
---|
649 | 695 | } |
---|
650 | 696 | EXPORT_SYMBOL(__stack_chk_fail); |
---|
651 | 697 | |
---|
652 | 698 | #endif |
---|
653 | 699 | |
---|
654 | | -#ifdef CONFIG_ARCH_HAS_REFCOUNT |
---|
655 | | -void refcount_error_report(struct pt_regs *regs, const char *err) |
---|
656 | | -{ |
---|
657 | | - WARN_RATELIMIT(1, "refcount_t %s at %pB in %s[%d], uid/euid: %u/%u\n", |
---|
658 | | - err, (void *)instruction_pointer(regs), |
---|
659 | | - current->comm, task_pid_nr(current), |
---|
660 | | - from_kuid_munged(&init_user_ns, current_uid()), |
---|
661 | | - from_kuid_munged(&init_user_ns, current_euid())); |
---|
662 | | -} |
---|
663 | | -#endif |
---|
664 | | - |
---|
665 | 700 | core_param(panic, panic_timeout, int, 0644); |
---|
| 701 | +core_param(panic_print, panic_print, ulong, 0644); |
---|
666 | 702 | core_param(pause_on_oops, pause_on_oops, int, 0644); |
---|
667 | 703 | core_param(panic_on_warn, panic_on_warn, int, 0644); |
---|
668 | 704 | core_param(crash_kexec_post_notifiers, crash_kexec_post_notifiers, bool, 0644); |
---|
.. | .. |
---|
676 | 712 | return 0; |
---|
677 | 713 | } |
---|
678 | 714 | early_param("oops", oops_setup); |
---|
| 715 | + |
---|
| 716 | +static int __init panic_on_taint_setup(char *s) |
---|
| 717 | +{ |
---|
| 718 | + char *taint_str; |
---|
| 719 | + |
---|
| 720 | + if (!s) |
---|
| 721 | + return -EINVAL; |
---|
| 722 | + |
---|
| 723 | + taint_str = strsep(&s, ","); |
---|
| 724 | + if (kstrtoul(taint_str, 16, &panic_on_taint)) |
---|
| 725 | + return -EINVAL; |
---|
| 726 | + |
---|
| 727 | + /* make sure panic_on_taint doesn't hold out-of-range TAINT flags */ |
---|
| 728 | + panic_on_taint &= TAINT_FLAGS_MAX; |
---|
| 729 | + |
---|
| 730 | + if (!panic_on_taint) |
---|
| 731 | + return -EINVAL; |
---|
| 732 | + |
---|
| 733 | + if (s && !strcmp(s, "nousertaint")) |
---|
| 734 | + panic_on_taint_nousertaint = true; |
---|
| 735 | + |
---|
| 736 | + pr_info("panic_on_taint: bitmask=0x%lx nousertaint_mode=%sabled\n", |
---|
| 737 | + panic_on_taint, panic_on_taint_nousertaint ? "en" : "dis"); |
---|
| 738 | + |
---|
| 739 | + return 0; |
---|
| 740 | +} |
---|
| 741 | +early_param("panic_on_taint", panic_on_taint_setup); |
---|