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