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