Skip to contentSkip to conversation

panic: make force_cpu redirect work from an NMI

PATCH Superseded Bradley Morgan 2 participants 2 messages Jul 7 – Jul 8

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 panic: make force_cpu redirect work from an NMI

nmi_panic sets panic_cpu first. Bail only on a different owner, and hand panic_cpu to the target after the IPI.

Found by sashiko [1].

[1] https://sashiko.dev/#/patchset/[email protected]

---
 kernel/panic.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
kernel/panic.c +5 -2
index e46c37b39c40..95ebf2bb0bf9 100644
@@ -396,8 +396,8 @@ static bool panic_try_force_cpu(const char *fmt, va_list args)
 		return false;
 	}
 
-	/* Another panic already in progress */
-	if (panic_in_progress())
+	/* Bail only if a different CPU is already handling it. */
+	if (panic_in_progress() && atomic_read(&panic_cpu) != this_cpu)
 		return false;
 
 	/* Which CPU won the race? */
@@ -440,6 +440,9 @@ static bool panic_try_force_cpu(const char *fmt, va_list args)
 		return false;
 	}
 
+	/* Hand panic_cpu to the target so it wins its own panic_try_start. */
+	atomic_set(&panic_cpu, panic_force_cpu);
+
 	/* IPI/NMI sent, this CPU should stop */
 	return true;
 }
Signature · 1 line
2.53.0
#1 lore raw

Adding more people into Cc.

I wonder if smp_call_function_single_async() can be called safely from NMI. Maybe, we should skip the redirection completely in this case.

Quoted 2 lines · Bradley Morgan

nmi_panic sets panic_cpu first. Bail only on a different owner, and hand panic_cpu to the target after the IPI.

This is quite cryptic.
Who should bail?
What is target?
What IPI?

Please, do more descriptive commit messages and better explain the problematic situation.

Quoted 1 line

Found by sashiko [1].

Better to use:

Quoted 2 lines

[1]
https://sashiko.dev/#/patchset/[email protected]

kernel/panic.c static bool panic_try_force_cpu(const char *fmt, va_list args) · line 400

Quoted 12 lines
kernel/panic.c +2 -2
@@ -396,8 +396,8 @@ static bool panic_try_force_cpu(const char *fmt, va_list args)
 		return false;
 	}
 
-	/* Another panic already in progress */
-	if (panic_in_progress())
+	/* Bail only if a different CPU is already handling it. */
+	if (panic_in_progress() && atomic_read(&panic_cpu) != this_cpu)

There already exist panic_on_other_cpu()...

kernel/panic.c static bool panic_try_force_cpu(const char *fmt, va_list args) · line 401

Quoted 1 line
kernel/panic.c +0 -0
 		return false;

This looks strange. "return false" means that vpanic() will continue with panic. I would expect that this CPU should end up offline.

It would actually work because vpanic() continues with:

	if (panic_try_start()) {
		/* go ahead */
	} else if (panic_on_other_cpu())
		panic_smp_self_stop();

And panic_try_start() would obviously fail while panic_on_other_cpu() succeed.

But still, it would be more straightforward to do here:

	/* Stop this CPU when the panic is already proceed elsewhere. */
	if (panic_on_other_cpu())
		return true;

kernel/panic.c static bool panic_try_force_cpu(const char *fmt, va_list args) · line 444

Quoted 12 lines
kernel/panic.c +2 -2
-	/* Another panic already in progress */
-	if (panic_in_progress())
+	/* Bail only if a different CPU is already handling it. */
+	if (panic_in_progress() && atomic_read(&panic_cpu) != this_cpu)
kernel/panic.c +2 -0
 	/* Which CPU won the race? */
@@ -440,6 +440,9 @@ static bool panic_try_force_cpu(const char *fmt, va_list args)
 		return false;
 	}
 
+	/* Hand panic_cpu to the target so it wins its own panic_try_start. */
+	atomic_set(&panic_cpu, panic_force_cpu);

As I already mentioned, this is too late. The panic_force_cpu might already be offline because it called panic_try_start() earlier it it failed.

The main question is whether we could safely call
smp_call_function_single_async() from NMI at all.

If not then we will need to proceed panic() on this CPU.

If yes then we need to fix this in nmi_panic(). It should call panic_try_force_cpu() first. It should call panic_try_start() only when panic_try_force_cpu() returns false, like when it did not forced another CPU.
r

kernel/panic.c static bool panic_try_force_cpu(const char *fmt, va_list args) · line 447

Quoted 2 lines
kernel/panic.c +0 -0
 	/* IPI/NMI sent, this CPU should stop */
 	return true;

Best Regards,
Petr