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