Upgrade to 4.4.50-rt62
[kvmfornfv.git] / kernel / kernel / panic.c
1 /*
2  *  linux/kernel/panic.c
3  *
4  *  Copyright (C) 1991, 1992  Linus Torvalds
5  */
6
7 /*
8  * This function is used through-out the kernel (including mm and fs)
9  * to indicate a major problem.
10  */
11 #include <linux/debug_locks.h>
12 #include <linux/interrupt.h>
13 #include <linux/kmsg_dump.h>
14 #include <linux/kallsyms.h>
15 #include <linux/notifier.h>
16 #include <linux/module.h>
17 #include <linux/random.h>
18 #include <linux/ftrace.h>
19 #include <linux/reboot.h>
20 #include <linux/delay.h>
21 #include <linux/kexec.h>
22 #include <linux/sched.h>
23 #include <linux/sysrq.h>
24 #include <linux/init.h>
25 #include <linux/nmi.h>
26 #include <linux/console.h>
27
28 #define PANIC_TIMER_STEP 100
29 #define PANIC_BLINK_SPD 18
30
31 int panic_on_oops = CONFIG_PANIC_ON_OOPS_VALUE;
32 static unsigned long tainted_mask;
33 static int pause_on_oops;
34 static int pause_on_oops_flag;
35 static DEFINE_SPINLOCK(pause_on_oops_lock);
36 bool crash_kexec_post_notifiers;
37 int panic_on_warn __read_mostly;
38
39 int panic_timeout = CONFIG_PANIC_TIMEOUT;
40 EXPORT_SYMBOL_GPL(panic_timeout);
41
42 ATOMIC_NOTIFIER_HEAD(panic_notifier_list);
43
44 EXPORT_SYMBOL(panic_notifier_list);
45
46 static long no_blink(int state)
47 {
48         return 0;
49 }
50
51 /* Returns how long it waited in ms */
52 long (*panic_blink)(int state);
53 EXPORT_SYMBOL(panic_blink);
54
55 /*
56  * Stop ourself in panic -- architecture code may override this
57  */
58 void __weak panic_smp_self_stop(void)
59 {
60         while (1)
61                 cpu_relax();
62 }
63
64 /*
65  * Stop ourselves in NMI context if another CPU has already panicked. Arch code
66  * may override this to prepare for crash dumping, e.g. save regs info.
67  */
68 void __weak nmi_panic_self_stop(struct pt_regs *regs)
69 {
70         panic_smp_self_stop();
71 }
72
73 atomic_t panic_cpu = ATOMIC_INIT(PANIC_CPU_INVALID);
74
75 /*
76  * A variant of panic() called from NMI context. We return if we've already
77  * panicked on this CPU. If another CPU already panicked, loop in
78  * nmi_panic_self_stop() which can provide architecture dependent code such
79  * as saving register state for crash dump.
80  */
81 void nmi_panic(struct pt_regs *regs, const char *msg)
82 {
83         int old_cpu, cpu;
84
85         cpu = raw_smp_processor_id();
86         old_cpu = atomic_cmpxchg(&panic_cpu, PANIC_CPU_INVALID, cpu);
87
88         if (old_cpu == PANIC_CPU_INVALID)
89                 panic("%s", msg);
90         else if (old_cpu != cpu)
91                 nmi_panic_self_stop(regs);
92 }
93 EXPORT_SYMBOL(nmi_panic);
94
95 /**
96  *      panic - halt the system
97  *      @fmt: The text string to print
98  *
99  *      Display a message, then perform cleanups.
100  *
101  *      This function never returns.
102  */
103 void panic(const char *fmt, ...)
104 {
105         static char buf[1024];
106         va_list args;
107         long i, i_next = 0;
108         int state = 0;
109         int old_cpu, this_cpu;
110
111         /*
112          * Disable local interrupts. This will prevent panic_smp_self_stop
113          * from deadlocking the first cpu that invokes the panic, since
114          * there is nothing to prevent an interrupt handler (that runs
115          * after setting panic_cpu) from invoking panic() again.
116          */
117         local_irq_disable();
118
119         /*
120          * It's possible to come here directly from a panic-assertion and
121          * not have preempt disabled. Some functions called from here want
122          * preempt to be disabled. No point enabling it later though...
123          *
124          * Only one CPU is allowed to execute the panic code from here. For
125          * multiple parallel invocations of panic, all other CPUs either
126          * stop themself or will wait until they are stopped by the 1st CPU
127          * with smp_send_stop().
128          *
129          * `old_cpu == PANIC_CPU_INVALID' means this is the 1st CPU which
130          * comes here, so go ahead.
131          * `old_cpu == this_cpu' means we came from nmi_panic() which sets
132          * panic_cpu to this CPU.  In this case, this is also the 1st CPU.
133          */
134         this_cpu = raw_smp_processor_id();
135         old_cpu  = atomic_cmpxchg(&panic_cpu, PANIC_CPU_INVALID, this_cpu);
136
137         if (old_cpu != PANIC_CPU_INVALID && old_cpu != this_cpu)
138                 panic_smp_self_stop();
139
140         console_verbose();
141         bust_spinlocks(1);
142         va_start(args, fmt);
143         vsnprintf(buf, sizeof(buf), fmt, args);
144         va_end(args);
145         pr_emerg("Kernel panic - not syncing: %s\n", buf);
146 #ifdef CONFIG_DEBUG_BUGVERBOSE
147         /*
148          * Avoid nested stack-dumping if a panic occurs during oops processing
149          */
150         if (!test_taint(TAINT_DIE) && oops_in_progress <= 1)
151                 dump_stack();
152 #endif
153
154         /*
155          * If we have crashed and we have a crash kernel loaded let it handle
156          * everything else.
157          * If we want to run this after calling panic_notifiers, pass
158          * the "crash_kexec_post_notifiers" option to the kernel.
159          */
160         if (!crash_kexec_post_notifiers)
161                 crash_kexec(NULL);
162
163         /*
164          * Note smp_send_stop is the usual smp shutdown function, which
165          * unfortunately means it may not be hardened to work in a panic
166          * situation.
167          */
168         smp_send_stop();
169
170         /*
171          * Run any panic handlers, including those that might need to
172          * add information to the kmsg dump output.
173          */
174         atomic_notifier_call_chain(&panic_notifier_list, 0, buf);
175
176         kmsg_dump(KMSG_DUMP_PANIC);
177
178         /*
179          * If you doubt kdump always works fine in any situation,
180          * "crash_kexec_post_notifiers" offers you a chance to run
181          * panic_notifiers and dumping kmsg before kdump.
182          * Note: since some panic_notifiers can make crashed kernel
183          * more unstable, it can increase risks of the kdump failure too.
184          */
185         if (crash_kexec_post_notifiers)
186                 crash_kexec(NULL);
187
188         bust_spinlocks(0);
189
190         /*
191          * We may have ended up stopping the CPU holding the lock (in
192          * smp_send_stop()) while still having some valuable data in the console
193          * buffer.  Try to acquire the lock then release it regardless of the
194          * result.  The release will also print the buffers out.  Locks debug
195          * should be disabled to avoid reporting bad unlock balance when
196          * panic() is not being callled from OOPS.
197          */
198         debug_locks_off();
199         console_flush_on_panic();
200
201         if (!panic_blink)
202                 panic_blink = no_blink;
203
204         if (panic_timeout > 0) {
205                 /*
206                  * Delay timeout seconds before rebooting the machine.
207                  * We can't use the "normal" timers since we just panicked.
208                  */
209                 pr_emerg("Rebooting in %d seconds..", panic_timeout);
210
211                 for (i = 0; i < panic_timeout * 1000; i += PANIC_TIMER_STEP) {
212                         touch_nmi_watchdog();
213                         if (i >= i_next) {
214                                 i += panic_blink(state ^= 1);
215                                 i_next = i + 3600 / PANIC_BLINK_SPD;
216                         }
217                         mdelay(PANIC_TIMER_STEP);
218                 }
219         }
220         if (panic_timeout != 0) {
221                 /*
222                  * This will not be a clean reboot, with everything
223                  * shutting down.  But if there is a chance of
224                  * rebooting the system it will be rebooted.
225                  */
226                 emergency_restart();
227         }
228 #ifdef __sparc__
229         {
230                 extern int stop_a_enabled;
231                 /* Make sure the user can actually press Stop-A (L1-A) */
232                 stop_a_enabled = 1;
233                 pr_emerg("Press Stop-A (L1-A) to return to the boot prom\n");
234         }
235 #endif
236 #if defined(CONFIG_S390)
237         {
238                 unsigned long caller;
239
240                 caller = (unsigned long)__builtin_return_address(0);
241                 disabled_wait(caller);
242         }
243 #endif
244         pr_emerg("---[ end Kernel panic - not syncing: %s\n", buf);
245         local_irq_enable();
246         for (i = 0; ; i += PANIC_TIMER_STEP) {
247                 touch_softlockup_watchdog();
248                 if (i >= i_next) {
249                         i += panic_blink(state ^= 1);
250                         i_next = i + 3600 / PANIC_BLINK_SPD;
251                 }
252                 mdelay(PANIC_TIMER_STEP);
253         }
254 }
255
256 EXPORT_SYMBOL(panic);
257
258
259 struct tnt {
260         u8      bit;
261         char    true;
262         char    false;
263 };
264
265 static const struct tnt tnts[] = {
266         { TAINT_PROPRIETARY_MODULE,     'P', 'G' },
267         { TAINT_FORCED_MODULE,          'F', ' ' },
268         { TAINT_CPU_OUT_OF_SPEC,        'S', ' ' },
269         { TAINT_FORCED_RMMOD,           'R', ' ' },
270         { TAINT_MACHINE_CHECK,          'M', ' ' },
271         { TAINT_BAD_PAGE,               'B', ' ' },
272         { TAINT_USER,                   'U', ' ' },
273         { TAINT_DIE,                    'D', ' ' },
274         { TAINT_OVERRIDDEN_ACPI_TABLE,  'A', ' ' },
275         { TAINT_WARN,                   'W', ' ' },
276         { TAINT_CRAP,                   'C', ' ' },
277         { TAINT_FIRMWARE_WORKAROUND,    'I', ' ' },
278         { TAINT_OOT_MODULE,             'O', ' ' },
279         { TAINT_UNSIGNED_MODULE,        'E', ' ' },
280         { TAINT_SOFTLOCKUP,             'L', ' ' },
281         { TAINT_LIVEPATCH,              'K', ' ' },
282 };
283
284 /**
285  *      print_tainted - return a string to represent the kernel taint state.
286  *
287  *  'P' - Proprietary module has been loaded.
288  *  'F' - Module has been forcibly loaded.
289  *  'S' - SMP with CPUs not designed for SMP.
290  *  'R' - User forced a module unload.
291  *  'M' - System experienced a machine check exception.
292  *  'B' - System has hit bad_page.
293  *  'U' - Userspace-defined naughtiness.
294  *  'D' - Kernel has oopsed before
295  *  'A' - ACPI table overridden.
296  *  'W' - Taint on warning.
297  *  'C' - modules from drivers/staging are loaded.
298  *  'I' - Working around severe firmware bug.
299  *  'O' - Out-of-tree module has been loaded.
300  *  'E' - Unsigned module has been loaded.
301  *  'L' - A soft lockup has previously occurred.
302  *  'K' - Kernel has been live patched.
303  *
304  *      The string is overwritten by the next call to print_tainted().
305  */
306 const char *print_tainted(void)
307 {
308         static char buf[ARRAY_SIZE(tnts) + sizeof("Tainted: ")];
309
310         if (tainted_mask) {
311                 char *s;
312                 int i;
313
314                 s = buf + sprintf(buf, "Tainted: ");
315                 for (i = 0; i < ARRAY_SIZE(tnts); i++) {
316                         const struct tnt *t = &tnts[i];
317                         *s++ = test_bit(t->bit, &tainted_mask) ?
318                                         t->true : t->false;
319                 }
320                 *s = 0;
321         } else
322                 snprintf(buf, sizeof(buf), "Not tainted");
323
324         return buf;
325 }
326
327 int test_taint(unsigned flag)
328 {
329         return test_bit(flag, &tainted_mask);
330 }
331 EXPORT_SYMBOL(test_taint);
332
333 unsigned long get_taint(void)
334 {
335         return tainted_mask;
336 }
337
338 /**
339  * add_taint: add a taint flag if not already set.
340  * @flag: one of the TAINT_* constants.
341  * @lockdep_ok: whether lock debugging is still OK.
342  *
343  * If something bad has gone wrong, you'll want @lockdebug_ok = false, but for
344  * some notewortht-but-not-corrupting cases, it can be set to true.
345  */
346 void add_taint(unsigned flag, enum lockdep_ok lockdep_ok)
347 {
348         if (lockdep_ok == LOCKDEP_NOW_UNRELIABLE && __debug_locks_off())
349                 pr_warn("Disabling lock debugging due to kernel taint\n");
350
351         set_bit(flag, &tainted_mask);
352 }
353 EXPORT_SYMBOL(add_taint);
354
355 static void spin_msec(int msecs)
356 {
357         int i;
358
359         for (i = 0; i < msecs; i++) {
360                 touch_nmi_watchdog();
361                 mdelay(1);
362         }
363 }
364
365 /*
366  * It just happens that oops_enter() and oops_exit() are identically
367  * implemented...
368  */
369 static void do_oops_enter_exit(void)
370 {
371         unsigned long flags;
372         static int spin_counter;
373
374         if (!pause_on_oops)
375                 return;
376
377         spin_lock_irqsave(&pause_on_oops_lock, flags);
378         if (pause_on_oops_flag == 0) {
379                 /* This CPU may now print the oops message */
380                 pause_on_oops_flag = 1;
381         } else {
382                 /* We need to stall this CPU */
383                 if (!spin_counter) {
384                         /* This CPU gets to do the counting */
385                         spin_counter = pause_on_oops;
386                         do {
387                                 spin_unlock(&pause_on_oops_lock);
388                                 spin_msec(MSEC_PER_SEC);
389                                 spin_lock(&pause_on_oops_lock);
390                         } while (--spin_counter);
391                         pause_on_oops_flag = 0;
392                 } else {
393                         /* This CPU waits for a different one */
394                         while (spin_counter) {
395                                 spin_unlock(&pause_on_oops_lock);
396                                 spin_msec(1);
397                                 spin_lock(&pause_on_oops_lock);
398                         }
399                 }
400         }
401         spin_unlock_irqrestore(&pause_on_oops_lock, flags);
402 }
403
404 /*
405  * Return true if the calling CPU is allowed to print oops-related info.
406  * This is a bit racy..
407  */
408 int oops_may_print(void)
409 {
410         return pause_on_oops_flag == 0;
411 }
412
413 /*
414  * Called when the architecture enters its oops handler, before it prints
415  * anything.  If this is the first CPU to oops, and it's oopsing the first
416  * time then let it proceed.
417  *
418  * This is all enabled by the pause_on_oops kernel boot option.  We do all
419  * this to ensure that oopses don't scroll off the screen.  It has the
420  * side-effect of preventing later-oopsing CPUs from mucking up the display,
421  * too.
422  *
423  * It turns out that the CPU which is allowed to print ends up pausing for
424  * the right duration, whereas all the other CPUs pause for twice as long:
425  * once in oops_enter(), once in oops_exit().
426  */
427 void oops_enter(void)
428 {
429         tracing_off();
430         /* can't trust the integrity of the kernel anymore: */
431         debug_locks_off();
432         do_oops_enter_exit();
433 }
434
435 /*
436  * 64-bit random ID for oopses:
437  */
438 static u64 oops_id;
439
440 static int init_oops_id(void)
441 {
442 #ifndef CONFIG_PREEMPT_RT_FULL
443         if (!oops_id)
444                 get_random_bytes(&oops_id, sizeof(oops_id));
445         else
446 #endif
447                 oops_id++;
448
449         return 0;
450 }
451 late_initcall(init_oops_id);
452
453 void print_oops_end_marker(void)
454 {
455         init_oops_id();
456         pr_warn("---[ end trace %016llx ]---\n", (unsigned long long)oops_id);
457 }
458
459 /*
460  * Called when the architecture exits its oops handler, after printing
461  * everything.
462  */
463 void oops_exit(void)
464 {
465         do_oops_enter_exit();
466         print_oops_end_marker();
467         kmsg_dump(KMSG_DUMP_OOPS);
468 }
469
470 #ifdef WANT_WARN_ON_SLOWPATH
471 struct slowpath_args {
472         const char *fmt;
473         va_list args;
474 };
475
476 static void warn_slowpath_common(const char *file, int line, void *caller,
477                                  unsigned taint, struct slowpath_args *args)
478 {
479         disable_trace_on_warning();
480
481         pr_warn("------------[ cut here ]------------\n");
482         pr_warn("WARNING: CPU: %d PID: %d at %s:%d %pS()\n",
483                 raw_smp_processor_id(), current->pid, file, line, caller);
484
485         if (args)
486                 vprintk(args->fmt, args->args);
487
488         if (panic_on_warn) {
489                 /*
490                  * This thread may hit another WARN() in the panic path.
491                  * Resetting this prevents additional WARN() from panicking the
492                  * system on this thread.  Other threads are blocked by the
493                  * panic_mutex in panic().
494                  */
495                 panic_on_warn = 0;
496                 panic("panic_on_warn set ...\n");
497         }
498
499         print_modules();
500         dump_stack();
501         print_oops_end_marker();
502         /* Just a warning, don't kill lockdep. */
503         add_taint(taint, LOCKDEP_STILL_OK);
504 }
505
506 void warn_slowpath_fmt(const char *file, int line, const char *fmt, ...)
507 {
508         struct slowpath_args args;
509
510         args.fmt = fmt;
511         va_start(args.args, fmt);
512         warn_slowpath_common(file, line, __builtin_return_address(0),
513                              TAINT_WARN, &args);
514         va_end(args.args);
515 }
516 EXPORT_SYMBOL(warn_slowpath_fmt);
517
518 void warn_slowpath_fmt_taint(const char *file, int line,
519                              unsigned taint, const char *fmt, ...)
520 {
521         struct slowpath_args args;
522
523         args.fmt = fmt;
524         va_start(args.args, fmt);
525         warn_slowpath_common(file, line, __builtin_return_address(0),
526                              taint, &args);
527         va_end(args.args);
528 }
529 EXPORT_SYMBOL(warn_slowpath_fmt_taint);
530
531 void warn_slowpath_null(const char *file, int line)
532 {
533         warn_slowpath_common(file, line, __builtin_return_address(0),
534                              TAINT_WARN, NULL);
535 }
536 EXPORT_SYMBOL(warn_slowpath_null);
537 #endif
538
539 #ifdef CONFIG_CC_STACKPROTECTOR
540
541 /*
542  * Called when gcc's -fstack-protector feature is used, and
543  * gcc detects corruption of the on-stack canary value
544  */
545 __visible void __stack_chk_fail(void)
546 {
547         panic("stack-protector: Kernel stack is corrupted in: %p\n",
548                 __builtin_return_address(0));
549 }
550 EXPORT_SYMBOL(__stack_chk_fail);
551
552 #endif
553
554 core_param(panic, panic_timeout, int, 0644);
555 core_param(pause_on_oops, pause_on_oops, int, 0644);
556 core_param(panic_on_warn, panic_on_warn, int, 0644);
557
558 static int __init setup_crash_kexec_post_notifiers(char *s)
559 {
560         crash_kexec_post_notifiers = true;
561         return 0;
562 }
563 early_param("crash_kexec_post_notifiers", setup_crash_kexec_post_notifiers);
564
565 static int __init oops_setup(char *s)
566 {
567         if (!s)
568                 return -EINVAL;
569         if (!strcmp(s, "panic"))
570                 panic_on_oops = 1;
571         return 0;
572 }
573 early_param("oops", oops_setup);