hc
2023-12-09 b22da3d8526a935aa31e086e63f60ff3246cb61c
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>
....@@ -34,6 +36,14 @@
3436 #define PANIC_TIMER_STEP 100
3537 #define PANIC_BLINK_SPD 18
3638
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
+
3747 int panic_on_oops = CONFIG_PANIC_ON_OOPS_VALUE;
3848 static unsigned long tainted_mask =
3949 IS_ENABLED(CONFIG_GCC_PLUGIN_RANDSTRUCT) ? (1 << TAINT_RANDSTRUCT) : 0;
....@@ -42,15 +52,23 @@
4252 static DEFINE_SPINLOCK(pause_on_oops_lock);
4353 bool crash_kexec_post_notifiers;
4454 int panic_on_warn __read_mostly;
55
+unsigned long panic_on_taint;
56
+bool panic_on_taint_nousertaint = false;
4557
4658 int panic_timeout = CONFIG_PANIC_TIMEOUT;
4759 EXPORT_SYMBOL_GPL(panic_timeout);
4860
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;
5168
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);
5472
5573 static long no_blink(int state)
5674 {
....@@ -127,6 +145,27 @@
127145 }
128146 EXPORT_SYMBOL(nmi_panic);
129147
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
+
130169 /**
131170 * panic - halt the system
132171 * @fmt: The text string to print
....@@ -138,8 +177,9 @@
138177 void panic(const char *fmt, ...)
139178 {
140179 static char buf[1024];
180
+ va_list args2;
141181 va_list args;
142
- long i, i_next = 0;
182
+ long i, i_next = 0, len;
143183 int state = 0;
144184 int old_cpu, this_cpu;
145185 bool _crash_kexec_post_notifiers = crash_kexec_post_notifiers;
....@@ -152,6 +192,21 @@
152192 */
153193 local_irq_disable();
154194 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);
155210
156211 /*
157212 * It's possible to come here directly from a panic-assertion and
....@@ -174,21 +229,19 @@
174229 if (old_cpu != PANIC_CPU_INVALID && old_cpu != this_cpu)
175230 panic_smp_self_stop();
176231
177
- console_verbose();
178232 bust_spinlocks(1);
179
- va_start(args, fmt);
180
- vsnprintf(buf, sizeof(buf), fmt, args);
233
+ len = vscnprintf(buf, sizeof(buf), fmt, args);
181234 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
+
186239 /*
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.
188243 */
189
- if (!test_taint(TAINT_DIE) && oops_in_progress <= 1)
190
- dump_stack();
191
-#endif
244
+ kgdb_panic(buf);
192245
193246 /*
194247 * If we have crashed and we have a crash kernel loaded let it handle
....@@ -199,7 +252,6 @@
199252 * Bypass the panic_cpu check and call __crash_kexec directly.
200253 */
201254 if (!_crash_kexec_post_notifiers) {
202
- printk_safe_flush_on_panic();
203255 __crash_kexec(NULL);
204256
205257 /*
....@@ -223,8 +275,6 @@
223275 */
224276 atomic_notifier_call_chain(&panic_notifier_list, 0, buf);
225277
226
- /* Call flush even twice. It tries harder with a single online CPU */
227
- printk_safe_flush_on_panic();
228278 kmsg_dump(KMSG_DUMP_PANIC);
229279
230280 /*
....@@ -253,7 +303,9 @@
253303 * panic() is not being callled from OOPS.
254304 */
255305 debug_locks_off();
256
- console_flush_on_panic();
306
+ console_flush_on_panic(CONSOLE_FLUSH_PENDING);
307
+
308
+ panic_print_sys_info();
257309
258310 if (!panic_blink)
259311 panic_blink = no_blink;
....@@ -294,14 +346,12 @@
294346 }
295347 #endif
296348 #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();
303350 #endif
304351 pr_emerg("---[ end Kernel panic - not syncing: %s ]---\n", buf);
352
+
353
+ /* Do not scroll important messages printed above */
354
+ suppress_printk = 1;
305355 local_irq_enable();
306356 for (i = 0; ; i += PANIC_TIMER_STEP) {
307357 touch_softlockup_watchdog();
....@@ -343,7 +393,7 @@
343393 /**
344394 * print_tainted - return a string to represent the kernel taint state.
345395 *
346
- * For individual taint flag meanings, see Documentation/sysctl/kernel.txt
396
+ * For individual taint flag meanings, see Documentation/admin-guide/sysctl/kernel.rst
347397 *
348398 * The string is overwritten by the next call to print_tainted(),
349399 * but is always NULL terminated.
....@@ -396,6 +446,11 @@
396446 pr_warn("Disabling lock debugging due to kernel taint\n");
397447
398448 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
+ }
399454 }
400455 EXPORT_SYMBOL(add_taint);
401456
....@@ -452,7 +507,7 @@
452507 * Return true if the calling CPU is allowed to print oops-related info.
453508 * This is a bit racy..
454509 */
455
-int oops_may_print(void)
510
+bool oops_may_print(void)
456511 {
457512 return pause_on_oops_flag == 0;
458513 }
....@@ -477,6 +532,9 @@
477532 /* can't trust the integrity of the kernel anymore: */
478533 debug_locks_off();
479534 do_oops_enter_exit();
535
+
536
+ if (sysctl_oops_all_cpu_backtrace)
537
+ trigger_all_cpu_backtrace();
480538 }
481539
482540 /*
....@@ -486,7 +544,7 @@
486544
487545 static int init_oops_id(void)
488546 {
489
-#ifndef CONFIG_PREEMPT_RT_FULL
547
+#ifndef CONFIG_PREEMPT_RT
490548 if (!oops_id)
491549 get_random_bytes(&oops_id, sizeof(oops_id));
492550 else
....@@ -497,10 +555,11 @@
497555 }
498556 late_initcall(init_oops_id);
499557
500
-void print_oops_end_marker(void)
558
+static void print_oops_end_marker(void)
501559 {
502560 init_oops_id();
503561 pr_warn("---[ end trace %016llx ]---\n", (unsigned long long)oops_id);
562
+ pr_flush(1000, true);
504563 }
505564
506565 /*
....@@ -524,9 +583,6 @@
524583 {
525584 disable_trace_on_warning();
526585
527
- if (args)
528
- pr_warn(CUT_HERE);
529
-
530586 if (file)
531587 pr_warn("WARNING: CPU: %d PID: %d at %s:%d %pS\n",
532588 raw_smp_processor_id(), current->pid, file, line,
....@@ -537,6 +593,11 @@
537593
538594 if (args)
539595 vprintk(args->fmt, args->args);
596
+
597
+ print_modules();
598
+
599
+ if (regs)
600
+ show_regs(regs);
540601
541602 if (panic_on_warn) {
542603 /*
....@@ -549,11 +610,7 @@
549610 panic("panic_on_warn set ...\n");
550611 }
551612
552
- print_modules();
553
-
554
- if (regs)
555
- show_regs(regs);
556
- else
613
+ if (!regs)
557614 dump_stack();
558615
559616 print_irqtrace_events(current);
....@@ -564,37 +621,26 @@
564621 add_taint(taint, LOCKDEP_STILL_OK);
565622 }
566623
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, ...)
569627 {
570628 struct warn_args args;
571629
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);
579631
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
+ }
584637
585638 args.fmt = fmt;
586639 va_start(args.args, fmt);
587640 __warn(file, line, __builtin_return_address(0), taint, NULL, &args);
588641 va_end(args.args);
589642 }
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);
598644 #else
599645 void __warn_printk(const char *fmt, ...)
600646 {
....@@ -620,16 +666,14 @@
620666 return 0;
621667 }
622668
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");
627671
628672 static __init int register_warn_debugfs(void)
629673 {
630674 /* 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);
633677 return 0;
634678 }
635679
....@@ -642,27 +686,19 @@
642686 * Called when gcc's -fstack-protector feature is used, and
643687 * gcc detects corruption of the on-stack canary value
644688 */
645
-__visible void __stack_chk_fail(void)
689
+__visible noinstr void __stack_chk_fail(void)
646690 {
691
+ instrumentation_begin();
647692 panic("stack-protector: Kernel stack is corrupted in: %pB",
648693 __builtin_return_address(0));
694
+ instrumentation_end();
649695 }
650696 EXPORT_SYMBOL(__stack_chk_fail);
651697
652698 #endif
653699
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
-
665700 core_param(panic, panic_timeout, int, 0644);
701
+core_param(panic_print, panic_print, ulong, 0644);
666702 core_param(pause_on_oops, pause_on_oops, int, 0644);
667703 core_param(panic_on_warn, panic_on_warn, int, 0644);
668704 core_param(crash_kexec_post_notifiers, crash_kexec_post_notifiers, bool, 0644);
....@@ -676,3 +712,30 @@
676712 return 0;
677713 }
678714 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);