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
panic_print_deprecated(void)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 */
proc_taint(const struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos)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
sysctl_panic_print_handler(const struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos)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
kernel_panic_sysctls_init(void)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,..." */
setup_panic_sys_info(char * buf)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
warn_count_show(struct kobject * kobj,struct kobj_attribute * attr,char * page)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
kernel_panic_sysfs_init(void)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
no_blink(int state)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 */
panic_smp_self_stop(void)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 */
nmi_panic_self_stop(struct pt_regs * regs)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 */
crash_smp_send_stop(void)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
panic_try_start(void)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
panic_reset(void)319 void panic_reset(void)
320 {
321 atomic_set(&panic_cpu, PANIC_CPU_INVALID);
322 }
323 EXPORT_SYMBOL(panic_reset);
324
panic_in_progress(void)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. */
panic_on_this_cpu(void)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 */
panic_on_other_cpu(void)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 */
nmi_panic(struct pt_regs * regs,const char * msg)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
check_panic_on_warn(const char * origin)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
panic_trigger_all_cpu_backtrace(void)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 */
panic_other_cpus_shutdown(bool crash_kexec)402 static void panic_other_cpus_shutdown(bool crash_kexec)
403 {
404 if (panic_print & SYS_INFO_ALL_CPU_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 */
vpanic(const char * fmt,va_list args)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 */
panic(const char * fmt,...)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, _module) \
632 [ TAINT_##taint ] = { \
633 .c_true = _c_true, .c_false = _c_false, \
634 .module = _module, \
635 .desc = #taint, \
636 }
637
638 /*
639 * TAINT_FORCED_RMMOD could be a per-module flag but the module
640 * is being removed anyway.
641 */
642 const struct taint_flag taint_flags[TAINT_FLAGS_COUNT] = {
643 TAINT_FLAG(PROPRIETARY_MODULE, 'P', 'G', true),
644 TAINT_FLAG(FORCED_MODULE, 'F', ' ', true),
645 TAINT_FLAG(CPU_OUT_OF_SPEC, 'S', ' ', false),
646 TAINT_FLAG(FORCED_RMMOD, 'R', ' ', false),
647 TAINT_FLAG(MACHINE_CHECK, 'M', ' ', false),
648 TAINT_FLAG(BAD_PAGE, 'B', ' ', false),
649 TAINT_FLAG(USER, 'U', ' ', false),
650 TAINT_FLAG(DIE, 'D', ' ', false),
651 TAINT_FLAG(OVERRIDDEN_ACPI_TABLE, 'A', ' ', false),
652 TAINT_FLAG(WARN, 'W', ' ', false),
653 TAINT_FLAG(CRAP, 'C', ' ', true),
654 TAINT_FLAG(FIRMWARE_WORKAROUND, 'I', ' ', false),
655 TAINT_FLAG(OOT_MODULE, 'O', ' ', true),
656 TAINT_FLAG(UNSIGNED_MODULE, 'E', ' ', true),
657 TAINT_FLAG(SOFTLOCKUP, 'L', ' ', false),
658 TAINT_FLAG(LIVEPATCH, 'K', ' ', true),
659 TAINT_FLAG(AUX, 'X', ' ', true),
660 TAINT_FLAG(RANDSTRUCT, 'T', ' ', true),
661 TAINT_FLAG(TEST, 'N', ' ', true),
662 TAINT_FLAG(FWCTL, 'J', ' ', true),
663 };
664
665 #undef TAINT_FLAG
666
print_tainted_seq(struct seq_buf * s,bool verbose)667 static void print_tainted_seq(struct seq_buf *s, bool verbose)
668 {
669 const char *sep = "";
670 int i;
671
672 if (!tainted_mask) {
673 seq_buf_puts(s, "Not tainted");
674 return;
675 }
676
677 seq_buf_printf(s, "Tainted: ");
678 for (i = 0; i < TAINT_FLAGS_COUNT; i++) {
679 const struct taint_flag *t = &taint_flags[i];
680 bool is_set = test_bit(i, &tainted_mask);
681 char c = is_set ? t->c_true : t->c_false;
682
683 if (verbose) {
684 if (is_set) {
685 seq_buf_printf(s, "%s[%c]=%s", sep, c, t->desc);
686 sep = ", ";
687 }
688 } else {
689 seq_buf_putc(s, c);
690 }
691 }
692 }
693
_print_tainted(bool verbose)694 static const char *_print_tainted(bool verbose)
695 {
696 /* FIXME: what should the size be? */
697 static char buf[sizeof(taint_flags)];
698 struct seq_buf s;
699
700 BUILD_BUG_ON(ARRAY_SIZE(taint_flags) != TAINT_FLAGS_COUNT);
701
702 seq_buf_init(&s, buf, sizeof(buf));
703
704 print_tainted_seq(&s, verbose);
705
706 return seq_buf_str(&s);
707 }
708
709 /**
710 * print_tainted - return a string to represent the kernel taint state.
711 *
712 * For individual taint flag meanings, see Documentation/admin-guide/sysctl/kernel.rst
713 *
714 * The string is overwritten by the next call to print_tainted(),
715 * but is always NULL terminated.
716 */
print_tainted(void)717 const char *print_tainted(void)
718 {
719 return _print_tainted(false);
720 }
721
722 /**
723 * print_tainted_verbose - A more verbose version of print_tainted()
724 */
print_tainted_verbose(void)725 const char *print_tainted_verbose(void)
726 {
727 return _print_tainted(true);
728 }
729
test_taint(unsigned flag)730 int test_taint(unsigned flag)
731 {
732 return test_bit(flag, &tainted_mask);
733 }
734 EXPORT_SYMBOL(test_taint);
735
get_taint(void)736 unsigned long get_taint(void)
737 {
738 return tainted_mask;
739 }
740
741 /**
742 * add_taint: add a taint flag if not already set.
743 * @flag: one of the TAINT_* constants.
744 * @lockdep_ok: whether lock debugging is still OK.
745 *
746 * If something bad has gone wrong, you'll want @lockdebug_ok = false, but for
747 * some notewortht-but-not-corrupting cases, it can be set to true.
748 */
add_taint(unsigned flag,enum lockdep_ok lockdep_ok)749 void add_taint(unsigned flag, enum lockdep_ok lockdep_ok)
750 {
751 if (lockdep_ok == LOCKDEP_NOW_UNRELIABLE && __debug_locks_off())
752 pr_warn("Disabling lock debugging due to kernel taint\n");
753
754 set_bit(flag, &tainted_mask);
755
756 if (tainted_mask & panic_on_taint) {
757 panic_on_taint = 0;
758 panic("panic_on_taint set ...");
759 }
760 }
761 EXPORT_SYMBOL(add_taint);
762
spin_msec(int msecs)763 static void spin_msec(int msecs)
764 {
765 int i;
766
767 for (i = 0; i < msecs; i++) {
768 touch_nmi_watchdog();
769 mdelay(1);
770 }
771 }
772
773 /*
774 * It just happens that oops_enter() and oops_exit() are identically
775 * implemented...
776 */
do_oops_enter_exit(void)777 static void do_oops_enter_exit(void)
778 {
779 unsigned long flags;
780 static int spin_counter;
781
782 if (!pause_on_oops)
783 return;
784
785 spin_lock_irqsave(&pause_on_oops_lock, flags);
786 if (pause_on_oops_flag == 0) {
787 /* This CPU may now print the oops message */
788 pause_on_oops_flag = 1;
789 } else {
790 /* We need to stall this CPU */
791 if (!spin_counter) {
792 /* This CPU gets to do the counting */
793 spin_counter = pause_on_oops;
794 do {
795 spin_unlock(&pause_on_oops_lock);
796 spin_msec(MSEC_PER_SEC);
797 spin_lock(&pause_on_oops_lock);
798 } while (--spin_counter);
799 pause_on_oops_flag = 0;
800 } else {
801 /* This CPU waits for a different one */
802 while (spin_counter) {
803 spin_unlock(&pause_on_oops_lock);
804 spin_msec(1);
805 spin_lock(&pause_on_oops_lock);
806 }
807 }
808 }
809 spin_unlock_irqrestore(&pause_on_oops_lock, flags);
810 }
811
812 /*
813 * Return true if the calling CPU is allowed to print oops-related info.
814 * This is a bit racy..
815 */
oops_may_print(void)816 bool oops_may_print(void)
817 {
818 return pause_on_oops_flag == 0;
819 }
820
821 /*
822 * Called when the architecture enters its oops handler, before it prints
823 * anything. If this is the first CPU to oops, and it's oopsing the first
824 * time then let it proceed.
825 *
826 * This is all enabled by the pause_on_oops kernel boot option. We do all
827 * this to ensure that oopses don't scroll off the screen. It has the
828 * side-effect of preventing later-oopsing CPUs from mucking up the display,
829 * too.
830 *
831 * It turns out that the CPU which is allowed to print ends up pausing for
832 * the right duration, whereas all the other CPUs pause for twice as long:
833 * once in oops_enter(), once in oops_exit().
834 */
oops_enter(void)835 void oops_enter(void)
836 {
837 nbcon_cpu_emergency_enter();
838 tracing_off();
839 /* can't trust the integrity of the kernel anymore: */
840 debug_locks_off();
841 do_oops_enter_exit();
842
843 if (sysctl_oops_all_cpu_backtrace)
844 trigger_all_cpu_backtrace();
845 }
846
print_oops_end_marker(void)847 static void print_oops_end_marker(void)
848 {
849 pr_warn("---[ end trace %016llx ]---\n", 0ULL);
850 }
851
852 /*
853 * Called when the architecture exits its oops handler, after printing
854 * everything.
855 */
oops_exit(void)856 void oops_exit(void)
857 {
858 do_oops_enter_exit();
859 print_oops_end_marker();
860 nbcon_cpu_emergency_exit();
861 kmsg_dump(KMSG_DUMP_OOPS);
862 }
863
864 struct warn_args {
865 const char *fmt;
866 va_list args;
867 };
868
__warn(const char * file,int line,void * caller,unsigned taint,struct pt_regs * regs,struct warn_args * args)869 void __warn(const char *file, int line, void *caller, unsigned taint,
870 struct pt_regs *regs, struct warn_args *args)
871 {
872 nbcon_cpu_emergency_enter();
873
874 disable_trace_on_warning();
875
876 if (file)
877 pr_warn("WARNING: CPU: %d PID: %d at %s:%d %pS\n",
878 raw_smp_processor_id(), current->pid, file, line,
879 caller);
880 else
881 pr_warn("WARNING: CPU: %d PID: %d at %pS\n",
882 raw_smp_processor_id(), current->pid, caller);
883
884 #pragma GCC diagnostic push
885 #ifndef __clang__
886 #pragma GCC diagnostic ignored "-Wsuggest-attribute=format"
887 #endif
888 if (args)
889 vprintk(args->fmt, args->args);
890 #pragma GCC diagnostic pop
891
892 print_modules();
893
894 if (regs)
895 show_regs(regs);
896
897 check_panic_on_warn("kernel");
898
899 if (!regs)
900 dump_stack();
901
902 print_irqtrace_events(current);
903
904 print_oops_end_marker();
905 trace_error_report_end(ERROR_DETECTOR_WARN, (unsigned long)caller);
906
907 /* Just a warning, don't kill lockdep. */
908 add_taint(taint, LOCKDEP_STILL_OK);
909
910 nbcon_cpu_emergency_exit();
911 }
912
913 #ifdef CONFIG_BUG
914 #ifndef __WARN_FLAGS
warn_slowpath_fmt(const char * file,int line,unsigned taint,const char * fmt,...)915 void warn_slowpath_fmt(const char *file, int line, unsigned taint,
916 const char *fmt, ...)
917 {
918 bool rcu = warn_rcu_enter();
919 struct warn_args args;
920
921 pr_warn(CUT_HERE);
922
923 if (!fmt) {
924 __warn(file, line, __builtin_return_address(0), taint,
925 NULL, NULL);
926 warn_rcu_exit(rcu);
927 return;
928 }
929
930 args.fmt = fmt;
931 va_start(args.args, fmt);
932 __warn(file, line, __builtin_return_address(0), taint, NULL, &args);
933 va_end(args.args);
934 warn_rcu_exit(rcu);
935 }
936 EXPORT_SYMBOL(warn_slowpath_fmt);
937 #else
__warn_printk(const char * fmt,...)938 void __warn_printk(const char *fmt, ...)
939 {
940 bool rcu = warn_rcu_enter();
941 va_list args;
942
943 pr_warn(CUT_HERE);
944
945 va_start(args, fmt);
946 vprintk(fmt, args);
947 va_end(args);
948 warn_rcu_exit(rcu);
949 }
950 EXPORT_SYMBOL(__warn_printk);
951 #endif
952
953 /* Support resetting WARN*_ONCE state */
954
clear_warn_once_set(void * data,u64 val)955 static int clear_warn_once_set(void *data, u64 val)
956 {
957 generic_bug_clear_once();
958 memset(__start_once, 0, __end_once - __start_once);
959 return 0;
960 }
961
962 DEFINE_DEBUGFS_ATTRIBUTE(clear_warn_once_fops, NULL, clear_warn_once_set,
963 "%lld\n");
964
register_warn_debugfs(void)965 static __init int register_warn_debugfs(void)
966 {
967 /* Don't care about failure */
968 debugfs_create_file_unsafe("clear_warn_once", 0200, NULL, NULL,
969 &clear_warn_once_fops);
970 return 0;
971 }
972
973 device_initcall(register_warn_debugfs);
974 #endif
975
976 #ifdef CONFIG_STACKPROTECTOR
977
978 /*
979 * Called when gcc's -fstack-protector feature is used, and
980 * gcc detects corruption of the on-stack canary value
981 */
__stack_chk_fail(void)982 __visible noinstr void __stack_chk_fail(void)
983 {
984 unsigned long flags;
985
986 instrumentation_begin();
987 flags = user_access_save();
988
989 panic("stack-protector: Kernel stack is corrupted in: %pB",
990 __builtin_return_address(0));
991
992 user_access_restore(flags);
993 instrumentation_end();
994 }
995 EXPORT_SYMBOL(__stack_chk_fail);
996
997 #endif
998
999 core_param(panic, panic_timeout, int, 0644);
1000 core_param(pause_on_oops, pause_on_oops, int, 0644);
1001 core_param(panic_on_warn, panic_on_warn, int, 0644);
1002 core_param(crash_kexec_post_notifiers, crash_kexec_post_notifiers, bool, 0644);
1003 core_param(panic_console_replay, panic_console_replay, bool, 0644);
1004
panic_print_set(const char * val,const struct kernel_param * kp)1005 static int panic_print_set(const char *val, const struct kernel_param *kp)
1006 {
1007 panic_print_deprecated();
1008 return param_set_ulong(val, kp);
1009 }
1010
panic_print_get(char * val,const struct kernel_param * kp)1011 static int panic_print_get(char *val, const struct kernel_param *kp)
1012 {
1013 panic_print_deprecated();
1014 return param_get_ulong(val, kp);
1015 }
1016
1017 static const struct kernel_param_ops panic_print_ops = {
1018 .set = panic_print_set,
1019 .get = panic_print_get,
1020 };
1021 __core_param_cb(panic_print, &panic_print_ops, &panic_print, 0644);
1022
oops_setup(char * s)1023 static int __init oops_setup(char *s)
1024 {
1025 if (!s)
1026 return -EINVAL;
1027 if (!strcmp(s, "panic"))
1028 panic_on_oops = 1;
1029 return 0;
1030 }
1031 early_param("oops", oops_setup);
1032
panic_on_taint_setup(char * s)1033 static int __init panic_on_taint_setup(char *s)
1034 {
1035 char *taint_str;
1036
1037 if (!s)
1038 return -EINVAL;
1039
1040 taint_str = strsep(&s, ",");
1041 if (kstrtoul(taint_str, 16, &panic_on_taint))
1042 return -EINVAL;
1043
1044 /* make sure panic_on_taint doesn't hold out-of-range TAINT flags */
1045 panic_on_taint &= TAINT_FLAGS_MAX;
1046
1047 if (!panic_on_taint)
1048 return -EINVAL;
1049
1050 if (s && !strcmp(s, "nousertaint"))
1051 panic_on_taint_nousertaint = true;
1052
1053 pr_info("panic_on_taint: bitmask=0x%lx nousertaint_mode=%s\n",
1054 panic_on_taint, str_enabled_disabled(panic_on_taint_nousertaint));
1055
1056 return 0;
1057 }
1058 early_param("panic_on_taint", panic_on_taint_setup);
1059