hc
2024-12-19 9370bb92b2d16684ee45cf24e879c93c509162da
kernel/kernel/panic.c
....@@ -1,3 +1,4 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * linux/kernel/panic.c
34 *
....@@ -11,6 +12,7 @@
1112 #include <linux/debug_locks.h>
1213 #include <linux/sched/debug.h>
1314 #include <linux/interrupt.h>
15
+#include <linux/kgdb.h>
1416 #include <linux/kmsg_dump.h>
1517 #include <linux/kallsyms.h>
1618 #include <linux/notifier.h>
....@@ -29,10 +31,21 @@
2931 #include <linux/bug.h>
3032 #include <linux/ratelimit.h>
3133 #include <linux/debugfs.h>
34
+#include <linux/sysfs.h>
3235 #include <asm/sections.h>
3336
3437 #define PANIC_TIMER_STEP 100
3538 #define PANIC_BLINK_SPD 18
39
+
40
+#ifdef CONFIG_SMP
41
+/*
42
+ * Should we dump all CPUs backtraces in an oops event?
43
+ * Defaults to 0, can be changed via sysctl.
44
+ */
45
+static unsigned int __read_mostly sysctl_oops_all_cpu_backtrace;
46
+#else
47
+#define sysctl_oops_all_cpu_backtrace 0
48
+#endif /* CONFIG_SMP */
3649
3750 int panic_on_oops = CONFIG_PANIC_ON_OOPS_VALUE;
3851 static unsigned long tainted_mask =
....@@ -42,15 +55,74 @@
4255 static DEFINE_SPINLOCK(pause_on_oops_lock);
4356 bool crash_kexec_post_notifiers;
4457 int panic_on_warn __read_mostly;
58
+unsigned long panic_on_taint;
59
+bool panic_on_taint_nousertaint = false;
60
+static unsigned int warn_limit __read_mostly;
4561
4662 int panic_timeout = CONFIG_PANIC_TIMEOUT;
4763 EXPORT_SYMBOL_GPL(panic_timeout);
4864
65
+#define PANIC_PRINT_TASK_INFO 0x00000001
66
+#define PANIC_PRINT_MEM_INFO 0x00000002
67
+#define PANIC_PRINT_TIMER_INFO 0x00000004
68
+#define PANIC_PRINT_LOCK_INFO 0x00000008
69
+#define PANIC_PRINT_FTRACE_INFO 0x00000010
70
+#define PANIC_PRINT_ALL_PRINTK_MSG 0x00000020
71
+unsigned long panic_print;
72
+
4973 ATOMIC_NOTIFIER_HEAD(panic_notifier_list);
74
+
5075 EXPORT_SYMBOL(panic_notifier_list);
5176
52
-void (*vendor_panic_cb)(u64 sp);
53
-EXPORT_SYMBOL_GPL(vendor_panic_cb);
77
+#ifdef CONFIG_SYSCTL
78
+static struct ctl_table kern_panic_table[] = {
79
+#ifdef CONFIG_SMP
80
+ {
81
+ .procname = "oops_all_cpu_backtrace",
82
+ .data = &sysctl_oops_all_cpu_backtrace,
83
+ .maxlen = sizeof(int),
84
+ .mode = 0644,
85
+ .proc_handler = proc_dointvec_minmax,
86
+ .extra1 = SYSCTL_ZERO,
87
+ .extra2 = SYSCTL_ONE,
88
+ },
89
+#endif
90
+ {
91
+ .procname = "warn_limit",
92
+ .data = &warn_limit,
93
+ .maxlen = sizeof(warn_limit),
94
+ .mode = 0644,
95
+ .proc_handler = proc_douintvec,
96
+ },
97
+ { }
98
+};
99
+
100
+static __init int kernel_panic_sysctls_init(void)
101
+{
102
+ register_sysctl_init("kernel", kern_panic_table);
103
+ return 0;
104
+}
105
+late_initcall(kernel_panic_sysctls_init);
106
+#endif
107
+
108
+static atomic_t warn_count = ATOMIC_INIT(0);
109
+
110
+#ifdef CONFIG_SYSFS
111
+static ssize_t warn_count_show(struct kobject *kobj, struct kobj_attribute *attr,
112
+ char *page)
113
+{
114
+ return sysfs_emit(page, "%d\n", atomic_read(&warn_count));
115
+}
116
+
117
+static struct kobj_attribute warn_count_attr = __ATTR_RO(warn_count);
118
+
119
+static __init int kernel_panic_sysfs_init(void)
120
+{
121
+ sysfs_add_file_to_group(kernel_kobj, &warn_count_attr.attr, NULL);
122
+ return 0;
123
+}
124
+late_initcall(kernel_panic_sysfs_init);
125
+#endif
54126
55127 static long no_blink(int state)
56128 {
....@@ -127,6 +199,40 @@
127199 }
128200 EXPORT_SYMBOL(nmi_panic);
129201
202
+static void panic_print_sys_info(void)
203
+{
204
+ if (panic_print & PANIC_PRINT_ALL_PRINTK_MSG)
205
+ console_flush_on_panic(CONSOLE_REPLAY_ALL);
206
+
207
+ if (panic_print & PANIC_PRINT_TASK_INFO)
208
+ show_state();
209
+
210
+ if (panic_print & PANIC_PRINT_MEM_INFO)
211
+ show_mem(0, NULL);
212
+
213
+ if (panic_print & PANIC_PRINT_TIMER_INFO)
214
+ sysrq_timer_list_show();
215
+
216
+ if (panic_print & PANIC_PRINT_LOCK_INFO)
217
+ debug_show_all_locks();
218
+
219
+ if (panic_print & PANIC_PRINT_FTRACE_INFO)
220
+ ftrace_dump(DUMP_ALL);
221
+}
222
+
223
+void check_panic_on_warn(const char *origin)
224
+{
225
+ unsigned int limit;
226
+
227
+ if (panic_on_warn)
228
+ panic("%s: panic_on_warn set ...\n", origin);
229
+
230
+ limit = READ_ONCE(warn_limit);
231
+ if (atomic_inc_return(&warn_count) >= limit && limit)
232
+ panic("%s: system warned too often (kernel.warn_limit is %d)",
233
+ origin, limit);
234
+}
235
+
130236 /**
131237 * panic - halt the system
132238 * @fmt: The text string to print
....@@ -139,10 +245,20 @@
139245 {
140246 static char buf[1024];
141247 va_list args;
142
- long i, i_next = 0;
248
+ long i, i_next = 0, len;
143249 int state = 0;
144250 int old_cpu, this_cpu;
145251 bool _crash_kexec_post_notifiers = crash_kexec_post_notifiers;
252
+
253
+ if (panic_on_warn) {
254
+ /*
255
+ * This thread may hit another WARN() in the panic path.
256
+ * Resetting this prevents additional WARN() from panicking the
257
+ * system on this thread. Other threads are blocked by the
258
+ * panic_mutex in panic().
259
+ */
260
+ panic_on_warn = 0;
261
+ }
146262
147263 /*
148264 * Disable local interrupts. This will prevent panic_smp_self_stop
....@@ -177,10 +293,12 @@
177293 console_verbose();
178294 bust_spinlocks(1);
179295 va_start(args, fmt);
180
- vsnprintf(buf, sizeof(buf), fmt, args);
296
+ len = vscnprintf(buf, sizeof(buf), fmt, args);
181297 va_end(args);
182
- if (vendor_panic_cb)
183
- vendor_panic_cb(0);
298
+
299
+ if (len && buf[len - 1] == '\n')
300
+ buf[len - 1] = '\0';
301
+
184302 pr_emerg("Kernel panic - not syncing: %s\n", buf);
185303 #ifdef CONFIG_DEBUG_BUGVERBOSE
186304 /*
....@@ -189,6 +307,13 @@
189307 if (!test_taint(TAINT_DIE) && oops_in_progress <= 1)
190308 dump_stack();
191309 #endif
310
+
311
+ /*
312
+ * If kgdb is enabled, give it a chance to run before we stop all
313
+ * the other CPUs or else we won't be able to debug processes left
314
+ * running on them.
315
+ */
316
+ kgdb_panic(buf);
192317
193318 /*
194319 * If we have crashed and we have a crash kernel loaded let it handle
....@@ -253,7 +378,9 @@
253378 * panic() is not being callled from OOPS.
254379 */
255380 debug_locks_off();
256
- console_flush_on_panic();
381
+ console_flush_on_panic(CONSOLE_FLUSH_PENDING);
382
+
383
+ panic_print_sys_info();
257384
258385 if (!panic_blink)
259386 panic_blink = no_blink;
....@@ -294,14 +421,12 @@
294421 }
295422 #endif
296423 #if defined(CONFIG_S390)
297
- {
298
- unsigned long caller;
299
-
300
- caller = (unsigned long)__builtin_return_address(0);
301
- disabled_wait(caller);
302
- }
424
+ disabled_wait();
303425 #endif
304426 pr_emerg("---[ end Kernel panic - not syncing: %s ]---\n", buf);
427
+
428
+ /* Do not scroll important messages printed above */
429
+ suppress_printk = 1;
305430 local_irq_enable();
306431 for (i = 0; ; i += PANIC_TIMER_STEP) {
307432 touch_softlockup_watchdog();
....@@ -343,7 +468,7 @@
343468 /**
344469 * print_tainted - return a string to represent the kernel taint state.
345470 *
346
- * For individual taint flag meanings, see Documentation/sysctl/kernel.txt
471
+ * For individual taint flag meanings, see Documentation/admin-guide/sysctl/kernel.rst
347472 *
348473 * The string is overwritten by the next call to print_tainted(),
349474 * but is always NULL terminated.
....@@ -396,6 +521,11 @@
396521 pr_warn("Disabling lock debugging due to kernel taint\n");
397522
398523 set_bit(flag, &tainted_mask);
524
+
525
+ if (tainted_mask & panic_on_taint) {
526
+ panic_on_taint = 0;
527
+ panic("panic_on_taint set ...");
528
+ }
399529 }
400530 EXPORT_SYMBOL(add_taint);
401531
....@@ -452,7 +582,7 @@
452582 * Return true if the calling CPU is allowed to print oops-related info.
453583 * This is a bit racy..
454584 */
455
-int oops_may_print(void)
585
+bool oops_may_print(void)
456586 {
457587 return pause_on_oops_flag == 0;
458588 }
....@@ -477,6 +607,9 @@
477607 /* can't trust the integrity of the kernel anymore: */
478608 debug_locks_off();
479609 do_oops_enter_exit();
610
+
611
+ if (sysctl_oops_all_cpu_backtrace)
612
+ trigger_all_cpu_backtrace();
480613 }
481614
482615 /*
....@@ -486,18 +619,16 @@
486619
487620 static int init_oops_id(void)
488621 {
489
-#ifndef CONFIG_PREEMPT_RT_FULL
490622 if (!oops_id)
491623 get_random_bytes(&oops_id, sizeof(oops_id));
492624 else
493
-#endif
494625 oops_id++;
495626
496627 return 0;
497628 }
498629 late_initcall(init_oops_id);
499630
500
-void print_oops_end_marker(void)
631
+static void print_oops_end_marker(void)
501632 {
502633 init_oops_id();
503634 pr_warn("---[ end trace %016llx ]---\n", (unsigned long long)oops_id);
....@@ -524,9 +655,6 @@
524655 {
525656 disable_trace_on_warning();
526657
527
- if (args)
528
- pr_warn(CUT_HERE);
529
-
530658 if (file)
531659 pr_warn("WARNING: CPU: %d PID: %d at %s:%d %pS\n",
532660 raw_smp_processor_id(), current->pid, file, line,
....@@ -538,22 +666,14 @@
538666 if (args)
539667 vprintk(args->fmt, args->args);
540668
541
- if (panic_on_warn) {
542
- /*
543
- * This thread may hit another WARN() in the panic path.
544
- * Resetting this prevents additional WARN() from panicking the
545
- * system on this thread. Other threads are blocked by the
546
- * panic_mutex in panic().
547
- */
548
- panic_on_warn = 0;
549
- panic("panic_on_warn set ...\n");
550
- }
551
-
552669 print_modules();
553670
554671 if (regs)
555672 show_regs(regs);
556
- else
673
+
674
+ check_panic_on_warn("kernel");
675
+
676
+ if (!regs)
557677 dump_stack();
558678
559679 print_irqtrace_events(current);
....@@ -564,37 +684,26 @@
564684 add_taint(taint, LOCKDEP_STILL_OK);
565685 }
566686
567
-#ifdef WANT_WARN_ON_SLOWPATH
568
-void warn_slowpath_fmt(const char *file, int line, const char *fmt, ...)
687
+#ifndef __WARN_FLAGS
688
+void warn_slowpath_fmt(const char *file, int line, unsigned taint,
689
+ const char *fmt, ...)
569690 {
570691 struct warn_args args;
571692
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);
693
+ pr_warn(CUT_HERE);
579694
580
-void warn_slowpath_fmt_taint(const char *file, int line,
581
- unsigned taint, const char *fmt, ...)
582
-{
583
- struct warn_args args;
695
+ if (!fmt) {
696
+ __warn(file, line, __builtin_return_address(0), taint,
697
+ NULL, NULL);
698
+ return;
699
+ }
584700
585701 args.fmt = fmt;
586702 va_start(args.args, fmt);
587703 __warn(file, line, __builtin_return_address(0), taint, NULL, &args);
588704 va_end(args.args);
589705 }
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);
706
+EXPORT_SYMBOL(warn_slowpath_fmt);
598707 #else
599708 void __warn_printk(const char *fmt, ...)
600709 {
....@@ -620,16 +729,14 @@
620729 return 0;
621730 }
622731
623
-DEFINE_SIMPLE_ATTRIBUTE(clear_warn_once_fops,
624
- NULL,
625
- clear_warn_once_set,
626
- "%lld\n");
732
+DEFINE_DEBUGFS_ATTRIBUTE(clear_warn_once_fops, NULL, clear_warn_once_set,
733
+ "%lld\n");
627734
628735 static __init int register_warn_debugfs(void)
629736 {
630737 /* Don't care about failure */
631
- debugfs_create_file("clear_warn_once", 0200, NULL,
632
- NULL, &clear_warn_once_fops);
738
+ debugfs_create_file_unsafe("clear_warn_once", 0200, NULL, NULL,
739
+ &clear_warn_once_fops);
633740 return 0;
634741 }
635742
....@@ -642,27 +749,19 @@
642749 * Called when gcc's -fstack-protector feature is used, and
643750 * gcc detects corruption of the on-stack canary value
644751 */
645
-__visible void __stack_chk_fail(void)
752
+__visible noinstr void __stack_chk_fail(void)
646753 {
754
+ instrumentation_begin();
647755 panic("stack-protector: Kernel stack is corrupted in: %pB",
648756 __builtin_return_address(0));
757
+ instrumentation_end();
649758 }
650759 EXPORT_SYMBOL(__stack_chk_fail);
651760
652761 #endif
653762
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
-
665763 core_param(panic, panic_timeout, int, 0644);
764
+core_param(panic_print, panic_print, ulong, 0644);
666765 core_param(pause_on_oops, pause_on_oops, int, 0644);
667766 core_param(panic_on_warn, panic_on_warn, int, 0644);
668767 core_param(crash_kexec_post_notifiers, crash_kexec_post_notifiers, bool, 0644);
....@@ -676,3 +775,30 @@
676775 return 0;
677776 }
678777 early_param("oops", oops_setup);
778
+
779
+static int __init panic_on_taint_setup(char *s)
780
+{
781
+ char *taint_str;
782
+
783
+ if (!s)
784
+ return -EINVAL;
785
+
786
+ taint_str = strsep(&s, ",");
787
+ if (kstrtoul(taint_str, 16, &panic_on_taint))
788
+ return -EINVAL;
789
+
790
+ /* make sure panic_on_taint doesn't hold out-of-range TAINT flags */
791
+ panic_on_taint &= TAINT_FLAGS_MAX;
792
+
793
+ if (!panic_on_taint)
794
+ return -EINVAL;
795
+
796
+ if (s && !strcmp(s, "nousertaint"))
797
+ panic_on_taint_nousertaint = true;
798
+
799
+ pr_info("panic_on_taint: bitmask=0x%lx nousertaint_mode=%sabled\n",
800
+ panic_on_taint, panic_on_taint_nousertaint ? "en" : "dis");
801
+
802
+ return 0;
803
+}
804
+early_param("panic_on_taint", panic_on_taint_setup);