Skip to contentSkip to conversation

kdump: crash_kexec()-smp_send_stop() race in panic

DISCUSSION Michael Holzheu 6 participants 14 messages Oct 24, 2011 – Oct 25, 2011

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

Hello Vivek,

In our tests we ran into the following scenario:

Two CPUs have called panic at the same time. The first CPU called crash_kexec() and the second CPU called smp_send_stop() in panic() before crash_kexec() finished on the first CPU. So the second CPU stopped the first CPU and therefore kdump failed.

1st CPU:
panic()->crash_kexec()->mutex_trylock(&kexec_mutex)-> do kdump

2nd CPU:
panic()->crash_kexec()->kexec_mutex already held by 1st CPU

       ->smp_send_stop()-> stop CPU 1 (stop kdump)

How should we fix this problem? One possibility could be to do smp_send_stop() before we call crash_kexec().

What do you think?

Michael

#1 lore raw
Quoted 12 lines · Michael Holzheu

Hello Vivek,

In our tests we ran into the following scenario:

Two CPUs have called panic at the same time. The first CPU called crash_kexec() and the second CPU called smp_send_stop() in panic() before crash_kexec() finished on the first CPU. So the second CPU stopped the first CPU and therefore kdump failed.

1st CPU:
panic()->crash_kexec()->mutex_trylock(&kexec_mutex)-> do kdump

Quoted 8 lines

2nd CPU:
panic()->crash_kexec()->kexec_mutex already held by 1st CPU

       ->smp_send_stop()-> stop CPU 1 (stop kdump)

How should we fix this problem? One possibility could be to do smp_send_stop() before we call crash_kexec().

What do you think?

smp_send_stop is insufficiently reliable to be used before crash_kexec.

My first reaction would be to test oops_in_progress and wait until oops_in_progress == 1 before calling smp_send_stop.

Eric

#2 lore raw
Quoted 21 lines · Eric W. Biederman
Quoted 19 lines · Michael Holzheu

Hello Vivek,

In our tests we ran into the following scenario:

Two CPUs have called panic at the same time. The first CPU called crash_kexec() and the second CPU called smp_send_stop() in panic() before crash_kexec() finished on the first CPU. So the second CPU stopped the first CPU and therefore kdump failed.

1st CPU:
panic()->crash_kexec()->mutex_trylock(&kexec_mutex)-> do kdump

2nd CPU:
panic()->crash_kexec()->kexec_mutex already held by 1st CPU ->smp_send_stop()-> stop CPU 1 (stop kdump)

How should we fix this problem? One possibility could be to do smp_send_stop() before we call crash_kexec().

Quoted 7 lines
Quoted 1 line

What do you think?

smp_send_stop is insufficiently reliable to be used before crash_kexec.

My first reaction would be to test oops_in_progress and wait until oops_in_progress == 1 before calling smp_send_stop.

+1

One of my colleague mentioned the same problem with me inside RH, given the fact that the race condition window is small, it would not be easy to reproduce this scenario.

Thanks.

#3 lore raw
Quoted 31 lines · Américo Wang
Quoted 21 lines · Eric W. Biederman
Quoted 19 lines · Michael Holzheu

Hello Vivek,

In our tests we ran into the following scenario:

Two CPUs have called panic at the same time. The first CPU called crash_kexec() and the second CPU called smp_send_stop() in panic() before crash_kexec() finished on the first CPU. So the second CPU stopped the first CPU and therefore kdump failed.

1st CPU:
panic()->crash_kexec()->mutex_trylock(&kexec_mutex)-> do kdump

2nd CPU:
panic()->crash_kexec()->kexec_mutex already held by 1st CPU ->smp_send_stop()-> stop CPU 1 (stop kdump)

How should we fix this problem? One possibility could be to do smp_send_stop() before we call crash_kexec().

Quoted 7 lines
Quoted 1 line

What do you think?

smp_send_stop is insufficiently reliable to be used before crash_kexec.

My first reaction would be to test oops_in_progress and wait until oops_in_progress == 1 before calling smp_send_stop.

Quoted 5 lines

+1

One of my colleague mentioned the same problem with me inside RH, given the fact that the race condition window is small, it would not be easy to reproduce this scenario.

As for reproducing it I have a hunch you could hack up something horrible with smp_call_function and kprobes.

On a little more reflection we can't wait until oops_in_progress goes to 1 before calling smp_send_stop. Because if crash_kexec is not involved nothing we will never call smp_send_stop.

So my second thought is to introduce another atomic variable panic_in_progress, visible only in panic. The cpu that sets increments panic_in_progress can call smp_send_stop. The rest of the cpus can just go into a busy wait. That should stop nasty fights about who is going to come out of smp_send_stop first.

Eric

#4 lore raw
Quoted 47 lines · Eric W. Biederman
Quoted 31 lines · Américo Wang
Quoted 21 lines · Eric W. Biederman
Quoted 19 lines · Michael Holzheu

Hello Vivek,

In our tests we ran into the following scenario:

Two CPUs have called panic at the same time. The first CPU called crash_kexec() and the second CPU called smp_send_stop() in panic() before crash_kexec() finished on the first CPU. So the second CPU stopped the first CPU and therefore kdump failed.

1st CPU:
panic()->crash_kexec()->mutex_trylock(&kexec_mutex)-> do kdump

2nd CPU:
panic()->crash_kexec()->kexec_mutex already held by 1st CPU ->smp_send_stop()-> stop CPU 1 (stop kdump)

How should we fix this problem? One possibility could be to do smp_send_stop() before we call crash_kexec().

Quoted 7 lines
Quoted 1 line

What do you think?

smp_send_stop is insufficiently reliable to be used before crash_kexec.

My first reaction would be to test oops_in_progress and wait until oops_in_progress == 1 before calling smp_send_stop.

Quoted 5 lines

+1

One of my colleague mentioned the same problem with me inside RH, given the fact that the race condition window is small, it would not be easy to reproduce this scenario.

As for reproducing it I have a hunch you could hack up something horrible with smp_call_function and kprobes.

On a little more reflection we can't wait until oops_in_progress goes to 1 before calling smp_send_stop. Because if crash_kexec is not involved nothing we will never call smp_send_stop.

Quoted 5 lines

So my second thought is to introduce another atomic variable panic_in_progress, visible only in panic. The cpu that sets increments panic_in_progress can call smp_send_stop. The rest of the cpus can just go into a busy wait. That should stop nasty fights about who is going to come out of smp_send_stop first.

Introducing panic_on_oops atomic sounds good.

Thanks
Vivek

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec

#5 lore raw

Hi,

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

1st CPU:
panic()->crash_kexec()->mutex_trylock(&kexec_mutex)-> do kdump

2nd CPU:
panic()->crash_kexec()->kexec_mutex already held by 1st CPU

       ->smp_send_stop()-> stop CPU 1 (stop kdump)

How should we fix this problem? One possibility could be to do smp_send_stop() before we call crash_kexec().

http://lkml.org/lkml/2010/9/16/353

I developed a patch solving this issue one year ago. (Just adding local_irq_disable in kexec path.)

I hope this helps.

Seiji

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec

#6 lore raw

Hello Seiji,

Quoted 9 lines · Seiji Aguchi

Hi,

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

1st CPU:
panic()->crash_kexec()->mutex_trylock(&kexec_mutex)-> do kdump

2nd CPU:
panic()->crash_kexec()->kexec_mutex already held by 1st CPU

       ->smp_send_stop()-> stop CPU 1 (stop kdump)
Quoted 7 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

How should we fix this problem? One possibility could be to do smp_send_stop() before we call crash_kexec().

http://lkml.org/lkml/2010/9/16/353

I developed a patch solving this issue one year ago. (Just adding local_irq_disable in kexec path.)

This won't work (at least on s390) because smp_send_stop() will also stop CPUs that have interrupts disabled.

Michael

#7 lore raw
In reply to Eric W. Biederman

Hello Eric,

On Mon, 2011-10-24 at 10:07 -0700, Eric W. Biederman wrote:

[snip]

Quoted 5 lines

So my second thought is to introduce another atomic variable panic_in_progress, visible only in panic. The cpu that sets increments panic_in_progress can call smp_send_stop. The rest of the cpus can just go into a busy wait. That should stop nasty fights about who is going to come out of smp_send_stop first.

So this is a spinlock, no? What about the following patch:

---
 kernel/panic.c |    7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
kernel/panic.c +6 -1
@@ -59,6 +59,7 @@ EXPORT_SYMBOL(panic_blink);
  */
 NORET_TYPE void panic(const char * fmt, ...)
 {
+	static DEFINE_SPINLOCK(panic_lock);
 	static char buf[1024];
 	va_list args;
 	long i, i_next = 0;
@@ -68,8 +69,12 @@ NORET_TYPE void panic(const char * fmt,
 	 * It's possible to come here directly from a panic-assertion and
 	 * not have preempt disabled. Some functions called from here want
 	 * preempt to be disabled. No point enabling it later though...
+	 *
+	 * Only one CPU is allowed to execute the panic code. For multiple
+	 * parallel invocations of panic all other CPUs will wait on the
+	 * panic_lock. They are stopped afterwards by smp_send_stop().
 	 */
-	preempt_disable();
+	spin_lock(&panic_lock);
 
 	console_verbose();
 	bust_spinlocks(1);

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec

#8 lore raw
Quoted 6 lines · Michael Holzheu

Hello Eric,

On Mon, 2011-10-24 at 10:07 -0700, Eric W. Biederman wrote:

[snip]

Quoted 7 lines
Quoted 5 lines

So my second thought is to introduce another atomic variable panic_in_progress, visible only in panic. The cpu that sets increments panic_in_progress can call smp_send_stop. The rest of the cpus can just go into a busy wait. That should stop nasty fights about who is going to come out of smp_send_stop first.

So this is a spinlock, no? What about the following patch:

Do we want both panic printks?

We really only need the mutual exclusion starting just before smp_send_stop so that is where I would be inclined to put it.

But yeah something like the below should work.

Eric

Quoted 28 lines
---
 kernel/panic.c |    7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
kernel/panic.c +6 -1
@@ -59,6 +59,7 @@ EXPORT_SYMBOL(panic_blink);
  */
 NORET_TYPE void panic(const char * fmt, ...)
 {
+	static DEFINE_SPINLOCK(panic_lock);
 	static char buf[1024];
 	va_list args;
 	long i, i_next = 0;
@@ -68,8 +69,12 @@ NORET_TYPE void panic(const char * fmt,
 	 * It's possible to come here directly from a panic-assertion and
 	 * not have preempt disabled. Some functions called from here want
 	 * preempt to be disabled. No point enabling it later though...
+	 *
+	 * Only one CPU is allowed to execute the panic code. For multiple
+	 * parallel invocations of panic all other CPUs will wait on the
+	 * panic_lock. They are stopped afterwards by smp_send_stop().
 	 */
-	preempt_disable();
+	spin_lock(&panic_lock);
 
 	console_verbose();
 	bust_spinlocks(1);
#9 lore raw
Quoted 8 lines · Eric W. Biederman
Quoted 6 lines · Michael Holzheu

Hello Eric,

On Mon, 2011-10-24 at 10:07 -0700, Eric W. Biederman wrote:

[snip]

Quoted 9 lines
Quoted 7 lines
Quoted 5 lines

So my second thought is to introduce another atomic variable panic_in_progress, visible only in panic. The cpu that sets increments panic_in_progress can call smp_send_stop. The rest of the cpus can just go into a busy wait. That should stop nasty fights about who is going to come out of smp_send_stop first.

So this is a spinlock, no? What about the following patch:

Do we want both panic printks?

I guess having printk() from from both the panics would be nice.

Quoted 3 lines

We really only need the mutual exclusion starting just before smp_send_stop so that is where I would be inclined to put it.

How about something just before crash_kexec()? I think there is not much point two cpus trying to execute crash_kexec() together.

Thanks
Vivek

Quoted 31 lines

But yeah something like the below should work.

Eric

Quoted 26 lines
---
 kernel/panic.c |    7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
kernel/panic.c +6 -1
@@ -59,6 +59,7 @@ EXPORT_SYMBOL(panic_blink);
  */
 NORET_TYPE void panic(const char * fmt, ...)
 {
+	static DEFINE_SPINLOCK(panic_lock);
 	static char buf[1024];
 	va_list args;
 	long i, i_next = 0;
@@ -68,8 +69,12 @@ NORET_TYPE void panic(const char * fmt,
 	 * It's possible to come here directly from a panic-assertion and
 	 * not have preempt disabled. Some functions called from here want
 	 * preempt to be disabled. No point enabling it later though...
+	 *
+	 * Only one CPU is allowed to execute the panic code. For multiple
+	 * parallel invocations of panic all other CPUs will wait on the
+	 * panic_lock. They are stopped afterwards by smp_send_stop().
 	 */
-	preempt_disable();
+	spin_lock(&panic_lock);
 
Quoted 2 lines

kernel/panic.c NORET_TYPE void panic(const char * fmt, · line 80

Quoted 2 lines
kernel/panic.c +0 -0
 	console_verbose();
 	bust_spinlocks(1);

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec

#10 lore raw
In reply to Eric W. Biederman
Quoted 8 lines · Eric W. Biederman
Quoted 6 lines · Michael Holzheu

Hello Eric,

On Mon, 2011-10-24 at 10:07 -0700, Eric W. Biederman wrote:

[snip]

Quoted 8 lines
Quoted 7 lines
Quoted 5 lines

So my second thought is to introduce another atomic variable panic_in_progress, visible only in panic. The cpu that sets increments panic_in_progress can call smp_send_stop. The rest of the cpus can just go into a busy wait. That should stop nasty fights about who is going to come out of smp_send_stop first.

So this is a spinlock, no? What about the following patch:

Do we want both panic printks?

Ok, good point. We proably should not change that.

Quoted 2 lines

We really only need the mutual exclusion starting just before smp_send_stop so that is where I would be inclined to put it.

I think to fix the race, at least we have the get the lock before we call crash_kexec().

Is the following patch ok for you?

---
 kernel/panic.c |    8 ++++++++
 1 file changed, 8 insertions(+)
kernel/panic.c +8 -0
@@ -59,6 +59,7 @@ EXPORT_SYMBOL(panic_blink);
  */
 NORET_TYPE void panic(const char * fmt, ...)
 {
+	static DEFINE_SPINLOCK(panic_lock);
 	static char buf[1024];
 	va_list args;
 	long i, i_next = 0;
@@ -82,6 +83,13 @@ NORET_TYPE void panic(const char * fmt,
 #endif
 
 	/*
+	 * Only one CPU is allowed to execute the panic code from here. For
+	 * multiple parallel invocations of panic all other CPUs will wait on
+	 * the panic_lock. They are stopped afterwards by smp_send_stop().
+	 */
+	spin_lock(&panic_lock);
+
+	/*
 	 * If we have crashed and we have a crash kernel loaded let it handle
 	 * everything else.
 	 * Do we want to call this before we try to display a message?
#11 lore raw
Quoted 42 lines · Michael Holzheu
Quoted 8 lines · Eric W. Biederman
Quoted 6 lines · Michael Holzheu

Hello Eric,

On Mon, 2011-10-24 at 10:07 -0700, Eric W. Biederman wrote:

[snip]

Quoted 8 lines
Quoted 7 lines
Quoted 5 lines

So my second thought is to introduce another atomic variable panic_in_progress, visible only in panic. The cpu that sets increments panic_in_progress can call smp_send_stop. The rest of the cpus can just go into a busy wait. That should stop nasty fights about who is going to come out of smp_send_stop first.

So this is a spinlock, no? What about the following patch:

Do we want both panic printks?

Ok, good point. We proably should not change that.

Quoted 2 lines

We really only need the mutual exclusion starting just before smp_send_stop so that is where I would be inclined to put it.

I think to fix the race, at least we have the get the lock before we call crash_kexec().

Is the following patch ok for you?

---
 kernel/panic.c |    8 ++++++++
 1 file changed, 8 insertions(+)
kernel/panic.c +1 -0
@@ -59,6 +59,7 @@ EXPORT_SYMBOL(panic_blink);
  */
 NORET_TYPE void panic(const char * fmt, ...)
 {
+	static DEFINE_SPINLOCK(panic_lock);
 	static char buf[1024];
 	va_list args;
 	long i, i_next = 0;

kernel/panic.c NORET_TYPE void panic(const char * fmt, · line 90

Quoted 8 lines
kernel/panic.c +5 -0
@@ -82,6 +83,13 @@ NORET_TYPE void panic(const char * fmt,
 #endif
 
 	/*
+	 * Only one CPU is allowed to execute the panic code from here. For
+	 * multiple parallel invocations of panic all other CPUs will wait on
+	 * the panic_lock. They are stopped afterwards by smp_send_stop().
+	 */
+	spin_lock(&panic_lock);

Why leave irqs enabled?

Atleast for x86, Don Zickus had a patch to use NMI in smp_send_stop(). So that should work even if interrupts are disabled. (I think that patch is not merged yet).

So are other architectures a concern? If yes, then may be in future we can make it an arch call which can also choose to disable interrupts.

CCing Don also. This lock also brings in the serialization required for panic notifier list and kmsg_dump() infrastructure.

Thanks
Vivek

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec

#12 lore raw
Quoted 3 lines · Vivek Goyal
Quoted 2 lines · Michael Holzheu
Quoted 1 line · Eric W. Biederman

Michael Holzheu <[email protected]> writes:

[snip]

Quoted 28 lines
Quoted 17 lines

Is the following patch ok for you?

---
 kernel/panic.c |    8 ++++++++
 1 file changed, 8 insertions(+)
kernel/panic.c +1 -0
@@ -59,6 +59,7 @@ EXPORT_SYMBOL(panic_blink);
  */
 NORET_TYPE void panic(const char * fmt, ...)
 {
+	static DEFINE_SPINLOCK(panic_lock);
 	static char buf[1024];
 	va_list args;
 	long i, i_next = 0;

kernel/panic.c NORET_TYPE void panic(const char * fmt, · line 90

Quoted 8 lines
kernel/panic.c +5 -0
@@ -82,6 +83,13 @@ NORET_TYPE void panic(const char * fmt,
 #endif
 
 	/*
+	 * Only one CPU is allowed to execute the panic code from here. For
+	 * multiple parallel invocations of panic all other CPUs will wait on
+	 * the panic_lock. They are stopped afterwards by smp_send_stop().
+	 */
+	spin_lock(&panic_lock);

Why leave irqs enabled?

Quoted 6 lines

Atleast for x86, Don Zickus had a patch to use NMI in smp_send_stop(). So that should work even if interrupts are disabled. (I think that patch is not merged yet).

So are other architectures a concern? If yes, then may be in future we can make it an arch call which can also choose to disable interrupts.

For s390 we could disable the interrupts here. smp_send_stop() works also when IRQs are disabled. But as you said - who knows if that is true on all architectures...

Michael

#13 lore raw
In reply to Vivek Goyal
Quoted 58 lines · Vivek Goyal
Quoted 42 lines · Michael Holzheu
Quoted 8 lines · Eric W. Biederman
Quoted 6 lines · Michael Holzheu

Hello Eric,

On Mon, 2011-10-24 at 10:07 -0700, Eric W. Biederman wrote:

[snip]

Quoted 8 lines
Quoted 7 lines
Quoted 5 lines

So my second thought is to introduce another atomic variable panic_in_progress, visible only in panic. The cpu that sets increments panic_in_progress can call smp_send_stop. The rest of the cpus can just go into a busy wait. That should stop nasty fights about who is going to come out of smp_send_stop first.

So this is a spinlock, no? What about the following patch:

Do we want both panic printks?

Ok, good point. We proably should not change that.

Quoted 2 lines

We really only need the mutual exclusion starting just before smp_send_stop so that is where I would be inclined to put it.

I think to fix the race, at least we have the get the lock before we call crash_kexec().

Is the following patch ok for you?

---
 kernel/panic.c |    8 ++++++++
 1 file changed, 8 insertions(+)
kernel/panic.c +1 -0
@@ -59,6 +59,7 @@ EXPORT_SYMBOL(panic_blink);
  */
 NORET_TYPE void panic(const char * fmt, ...)
 {
+	static DEFINE_SPINLOCK(panic_lock);
 	static char buf[1024];
 	va_list args;
 	long i, i_next = 0;

kernel/panic.c NORET_TYPE void panic(const char * fmt, · line 90

Quoted 8 lines
kernel/panic.c +5 -0
@@ -82,6 +83,13 @@ NORET_TYPE void panic(const char * fmt,
 #endif
 
 	/*
+	 * Only one CPU is allowed to execute the panic code from here. For
+	 * multiple parallel invocations of panic all other CPUs will wait on
+	 * the panic_lock. They are stopped afterwards by smp_send_stop().
+	 */
+	spin_lock(&panic_lock);

Why leave irqs enabled?

Atleast for x86, Don Zickus had a patch to use NMI in smp_send_stop(). So that should work even if interrupts are disabled. (I think that patch is not merged yet).

Quoted 5 lines

So are other architectures a concern? If yes, then may be in future we can make it an arch call which can also choose to disable interrupts.

CCing Don also. This lock also brings in the serialization required for panic notifier list and kmsg_dump() infrastructure.

This serializes panics, for kmsg_dump we wanted to serialize the shutdown path, IOW stop all the cpus realiably. This patch solves a different problem.

Cheers,
Don

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec