Skip to content

Commit 7bbee5c

Browse files
h-kawai-ezKAGA-KOKO
authored andcommitted
kexec: Fix race between panic() and crash_kexec()
Currently, panic() and crash_kexec() can be called at the same time. For example (x86 case): CPU 0: oops_end() crash_kexec() mutex_trylock() // acquired nmi_shootdown_cpus() // stop other CPUs CPU 1: panic() crash_kexec() mutex_trylock() // failed to acquire smp_send_stop() // stop other CPUs infinite loop If CPU 1 calls smp_send_stop() before nmi_shootdown_cpus(), kdump fails. In another case: CPU 0: oops_end() crash_kexec() mutex_trylock() // acquired <NMI> io_check_error() panic() crash_kexec() mutex_trylock() // failed to acquire infinite loop Clearly, this is an undesirable result. To fix this problem, this patch changes crash_kexec() to exclude others by using the panic_cpu atomic. Signed-off-by: Hidehiro Kawai <hidehiro.kawai.ez@hitachi.com> Acked-by: Michal Hocko <mhocko@suse.com> Cc: Andrew Morton <akpm@linux-foundation.org> Cc: Baoquan He <bhe@redhat.com> Cc: Dave Young <dyoung@redhat.com> Cc: "Eric W. Biederman" <ebiederm@xmission.com> Cc: HATAYAMA Daisuke <d.hatayama@jp.fujitsu.com> Cc: "H. Peter Anvin" <hpa@zytor.com> Cc: Ingo Molnar <mingo@kernel.org> Cc: Jonathan Corbet <corbet@lwn.net> Cc: kexec@lists.infradead.org Cc: linux-doc@vger.kernel.org Cc: Martin Schwidefsky <schwidefsky@de.ibm.com> Cc: Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com> Cc: Minfei Huang <mnfhuang@gmail.com> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Seth Jennings <sjenning@redhat.com> Cc: Steven Rostedt <rostedt@goodmis.org> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: Vitaly Kuznetsov <vkuznets@redhat.com> Cc: Vivek Goyal <vgoyal@redhat.com> Cc: x86-ml <x86@kernel.org> Link: http://lkml.kernel.org/r/20151210014630.25437.94161.stgit@softrs Signed-off-by: Borislav Petkov <bp@suse.de> Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
1 parent 58c5661 commit 7bbee5c

3 files changed

Lines changed: 37 additions & 3 deletions

File tree

‎include/linux/kexec.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ extern int kexec_purgatory_get_set_symbol(struct kimage *image,
237237
unsigned int size, bool get_value);
238238
extern void *kexec_purgatory_get_symbol_addr(struct kimage *image,
239239
const char *name);
240+
extern void __crash_kexec(struct pt_regs *);
240241
extern void crash_kexec(struct pt_regs *);
241242
int kexec_should_crash(struct task_struct *);
242243
void crash_save_cpu(struct pt_regs *regs, int cpu);
@@ -332,6 +333,7 @@ int __weak arch_kexec_apply_relocations(const Elf_Ehdr *ehdr, Elf_Shdr *sechdrs,
332333
#else /* !CONFIG_KEXEC_CORE */
333334
struct pt_regs;
334335
struct task_struct;
336+
static inline void __crash_kexec(struct pt_regs *regs) { }
335337
static inline void crash_kexec(struct pt_regs *regs) { }
336338
static inline int kexec_should_crash(struct task_struct *p) { return 0; }
337339
#define kexec_in_progress false

‎kernel/kexec_core.c‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -853,7 +853,12 @@ struct kimage *kexec_image;
853853
struct kimage *kexec_crash_image;
854854
int kexec_load_disabled;
855855

856-
void crash_kexec(struct pt_regs *regs)
856+
/*
857+
* No panic_cpu check version of crash_kexec(). This function is called
858+
* only when panic_cpu holds the current CPU number; this is the only CPU
859+
* which processes crash_kexec routines.
860+
*/
861+
void __crash_kexec(struct pt_regs *regs)
857862
{
858863
/* Take the kexec_mutex here to prevent sys_kexec_load
859864
* running on one cpu from replacing the crash kernel
@@ -876,6 +881,29 @@ void crash_kexec(struct pt_regs *regs)
876881
}
877882
}
878883

884+
void crash_kexec(struct pt_regs *regs)
885+
{
886+
int old_cpu, this_cpu;
887+
888+
/*
889+
* Only one CPU is allowed to execute the crash_kexec() code as with
890+
* panic(). Otherwise parallel calls of panic() and crash_kexec()
891+
* may stop each other. To exclude them, we use panic_cpu here too.
892+
*/
893+
this_cpu = raw_smp_processor_id();
894+
old_cpu = atomic_cmpxchg(&panic_cpu, PANIC_CPU_INVALID, this_cpu);
895+
if (old_cpu == PANIC_CPU_INVALID) {
896+
/* This is the 1st CPU which comes here, so go ahead. */
897+
__crash_kexec(regs);
898+
899+
/*
900+
* Reset panic_cpu to allow another panic()/crash_kexec()
901+
* call.
902+
*/
903+
atomic_set(&panic_cpu, PANIC_CPU_INVALID);
904+
}
905+
}
906+
879907
size_t crash_get_memory_size(void)
880908
{
881909
size_t size = 0;

‎kernel/panic.c‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,11 @@ void panic(const char *fmt, ...)
136136
* everything else.
137137
* If we want to run this after calling panic_notifiers, pass
138138
* the "crash_kexec_post_notifiers" option to the kernel.
139+
*
140+
* Bypass the panic_cpu check and call __crash_kexec directly.
139141
*/
140142
if (!crash_kexec_post_notifiers)
141-
crash_kexec(NULL);
143+
__crash_kexec(NULL);
142144

143145
/*
144146
* Note smp_send_stop is the usual smp shutdown function, which
@@ -161,9 +163,11 @@ void panic(const char *fmt, ...)
161163
* panic_notifiers and dumping kmsg before kdump.
162164
* Note: since some panic_notifiers can make crashed kernel
163165
* more unstable, it can increase risks of the kdump failure too.
166+
*
167+
* Bypass the panic_cpu check and call __crash_kexec directly.
164168
*/
165169
if (crash_kexec_post_notifiers)
166-
crash_kexec(NULL);
170+
__crash_kexec(NULL);
167171

168172
bust_spinlocks(0);
169173

0 commit comments

Comments
 (0)