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