]> rtime.felk.cvut.cz Git - hercules2020/nv-tegra/linux-4.4.git/blob - rt-patches/0335-panic-x86-Fix-re-entrance-problem-due-to-panic-on-NM.patch
rt_patches: required rebase due to printk change
[hercules2020/nv-tegra/linux-4.4.git] / rt-patches / 0335-panic-x86-Fix-re-entrance-problem-due-to-panic-on-NM.patch
1 From cc5ec3c5b53849556efab10b11456ae62fda8382 Mon Sep 17 00:00:00 2001
2 From: Hidehiro Kawai <hidehiro.kawai.ez@hitachi.com>
3 Date: Mon, 14 Dec 2015 11:19:09 +0100
4 Subject: [PATCH 335/366] panic, x86: Fix re-entrance problem due to panic on
5  NMI
6
7 If panic on NMI happens just after panic() on the same CPU, panic() is
8 recursively called. Kernel stalls, as a result, after failing to acquire
9 panic_lock.
10
11 To avoid this problem, don't call panic() in NMI context if we've
12 already entered panic().
13
14 For that, introduce nmi_panic() macro to reduce code duplication. In
15 the case of panic on NMI, don't return from NMI handlers if another CPU
16 already panicked.
17
18 Signed-off-by: Hidehiro Kawai <hidehiro.kawai.ez@hitachi.com>
19 Acked-by: Michal Hocko <mhocko@suse.com>
20 Cc: Aaron Tomlin <atomlin@redhat.com>
21 Cc: Andrew Morton <akpm@linux-foundation.org>
22 Cc: Andy Lutomirski <luto@kernel.org>
23 Cc: Baoquan He <bhe@redhat.com>
24 Cc: Chris Metcalf <cmetcalf@ezchip.com>
25 Cc: David Hildenbrand <dahi@linux.vnet.ibm.com>
26 Cc: Don Zickus <dzickus@redhat.com>
27 Cc: "Eric W. Biederman" <ebiederm@xmission.com>
28 Cc: Frederic Weisbecker <fweisbec@gmail.com>
29 Cc: Gobinda Charan Maji <gobinda.cemk07@gmail.com>
30 Cc: HATAYAMA Daisuke <d.hatayama@jp.fujitsu.com>
31 Cc: "H. Peter Anvin" <hpa@zytor.com>
32 Cc: Ingo Molnar <mingo@kernel.org>
33 Cc: Javi Merino <javi.merino@arm.com>
34 Cc: Jonathan Corbet <corbet@lwn.net>
35 Cc: kexec@lists.infradead.org
36 Cc: linux-doc@vger.kernel.org
37 Cc: lkml <linux-kernel@vger.kernel.org>
38 Cc: Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
39 Cc: Michal Nazarewicz <mina86@mina86.com>
40 Cc: Nicolas Iooss <nicolas.iooss_linux@m4x.org>
41 Cc: Peter Zijlstra <peterz@infradead.org>
42 Cc: Prarit Bhargava <prarit@redhat.com>
43 Cc: Rasmus Villemoes <linux@rasmusvillemoes.dk>
44 Cc: Rusty Russell <rusty@rustcorp.com.au>
45 Cc: Seth Jennings <sjenning@redhat.com>
46 Cc: Steven Rostedt <rostedt@goodmis.org>
47 Cc: Thomas Gleixner <tglx@linutronix.de>
48 Cc: Ulrich Obergfell <uobergfe@redhat.com>
49 Cc: Vitaly Kuznetsov <vkuznets@redhat.com>
50 Cc: Vivek Goyal <vgoyal@redhat.com>
51 Link: http://lkml.kernel.org/r/20151210014626.25437.13302.stgit@softrs
52 [ Cleanup comments, fixup formatting. ]
53 Signed-off-by: Borislav Petkov <bp@suse.de>
54 Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
55 Signed-off-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
56 ---
57  arch/x86/kernel/nmi.c  | 16 ++++++++++++----
58  include/linux/kernel.h | 20 ++++++++++++++++++++
59  kernel/panic.c         | 16 +++++++++++++---
60  kernel/watchdog.c      |  2 +-
61  4 files changed, 46 insertions(+), 8 deletions(-)
62
63 diff --git a/arch/x86/kernel/nmi.c b/arch/x86/kernel/nmi.c
64 index 697f90d..fca8793 100644
65 --- a/arch/x86/kernel/nmi.c
66 +++ b/arch/x86/kernel/nmi.c
67 @@ -231,7 +231,7 @@ pci_serr_error(unsigned char reason, struct pt_regs *regs)
68  #endif
69  
70         if (panic_on_unrecovered_nmi)
71 -               panic("NMI: Not continuing");
72 +               nmi_panic("NMI: Not continuing");
73  
74         pr_emerg("Dazed and confused, but trying to continue\n");
75  
76 @@ -255,8 +255,16 @@ io_check_error(unsigned char reason, struct pt_regs *regs)
77                  reason, smp_processor_id());
78         show_regs(regs);
79  
80 -       if (panic_on_io_nmi)
81 -               panic("NMI IOCK error: Not continuing");
82 +       if (panic_on_io_nmi) {
83 +               nmi_panic("NMI IOCK error: Not continuing");
84 +
85 +               /*
86 +                * If we end up here, it means we have received an NMI while
87 +                * processing panic(). Simply return without delaying and
88 +                * re-enabling NMIs.
89 +                */
90 +               return;
91 +       }
92  
93         /* Re-enable the IOCK line, wait for a few seconds */
94         reason = (reason & NMI_REASON_CLEAR_MASK) | NMI_REASON_CLEAR_IOCHK;
95 @@ -297,7 +305,7 @@ unknown_nmi_error(unsigned char reason, struct pt_regs *regs)
96  
97         pr_emerg("Do you have a strange power saving mode enabled?\n");
98         if (unknown_nmi_panic || panic_on_unrecovered_nmi)
99 -               panic("NMI: Not continuing");
100 +               nmi_panic("NMI: Not continuing");
101  
102         pr_emerg("Dazed and confused, but trying to continue\n");
103  }
104 diff --git a/include/linux/kernel.h b/include/linux/kernel.h
105 index 7d8a0b7..d199f2c 100644
106 --- a/include/linux/kernel.h
107 +++ b/include/linux/kernel.h
108 @@ -452,6 +452,26 @@ extern int sysctl_panic_on_stackoverflow;
109  extern bool crash_kexec_post_notifiers;
110  
111  /*
112 + * panic_cpu is used for synchronizing panic() and crash_kexec() execution. It
113 + * holds a CPU number which is executing panic() currently. A value of
114 + * PANIC_CPU_INVALID means no CPU has entered panic() or crash_kexec().
115 + */
116 +extern atomic_t panic_cpu;
117 +#define PANIC_CPU_INVALID      -1
118 +
119 +/*
120 + * A variant of panic() called from NMI context. We return if we've already
121 + * panicked on this CPU.
122 + */
123 +#define nmi_panic(fmt, ...)                                            \
124 +do {                                                                   \
125 +       int cpu = raw_smp_processor_id();                               \
126 +                                                                       \
127 +       if (atomic_cmpxchg(&panic_cpu, PANIC_CPU_INVALID, cpu) != cpu)  \
128 +               panic(fmt, ##__VA_ARGS__);                              \
129 +} while (0)
130 +
131 +/*
132   * Only to be used by arch init code. If the user over-wrote the default
133   * CONFIG_PANIC_TIMEOUT, honor it.
134   */
135 diff --git a/kernel/panic.c b/kernel/panic.c
136 index 63e4e16..80fcb84 100644
137 --- a/kernel/panic.c
138 +++ b/kernel/panic.c
139 @@ -65,6 +65,8 @@ void __weak panic_smp_self_stop(void)
140                 cpu_relax();
141  }
142  
143 +atomic_t panic_cpu = ATOMIC_INIT(PANIC_CPU_INVALID);
144 +
145  /**
146   *     panic - halt the system
147   *     @fmt: The text string to print
148 @@ -75,17 +77,17 @@ void __weak panic_smp_self_stop(void)
149   */
150  void panic(const char *fmt, ...)
151  {
152 -       static DEFINE_SPINLOCK(panic_lock);
153         static char buf[1024];
154         va_list args;
155         long i, i_next = 0;
156         int state = 0;
157 +       int old_cpu, this_cpu;
158  
159         /*
160          * Disable local interrupts. This will prevent panic_smp_self_stop
161          * from deadlocking the first cpu that invokes the panic, since
162          * there is nothing to prevent an interrupt handler (that runs
163 -        * after the panic_lock is acquired) from invoking panic again.
164 +        * after setting panic_cpu) from invoking panic() again.
165          */
166         local_irq_disable();
167  
168 @@ -98,8 +100,16 @@ void panic(const char *fmt, ...)
169          * multiple parallel invocations of panic, all other CPUs either
170          * stop themself or will wait until they are stopped by the 1st CPU
171          * with smp_send_stop().
172 +        *
173 +        * `old_cpu == PANIC_CPU_INVALID' means this is the 1st CPU which
174 +        * comes here, so go ahead.
175 +        * `old_cpu == this_cpu' means we came from nmi_panic() which sets
176 +        * panic_cpu to this CPU.  In this case, this is also the 1st CPU.
177          */
178 -       if (!spin_trylock(&panic_lock))
179 +       this_cpu = raw_smp_processor_id();
180 +       old_cpu  = atomic_cmpxchg(&panic_cpu, PANIC_CPU_INVALID, this_cpu);
181 +
182 +       if (old_cpu != PANIC_CPU_INVALID && old_cpu != this_cpu)
183                 panic_smp_self_stop();
184  
185         console_verbose();
186 diff --git a/kernel/watchdog.c b/kernel/watchdog.c
187 index 88b20a0..63073a1 100644
188 --- a/kernel/watchdog.c
189 +++ b/kernel/watchdog.c
190 @@ -436,7 +436,7 @@ static void watchdog_overflow_callback(struct perf_event *event,
191  
192                 raw_spin_unlock(&watchdog_output_lock);
193                 if (hardlockup_panic)
194 -                       panic("Hard LOCKUP");
195 +                       nmi_panic("Hard LOCKUP");
196  
197                 __this_cpu_write(hard_watchdog_warn, true);
198                 return;
199 -- 
200 1.9.1
201