Skip to contentSkip to conversation

panic: call panic handlers before panic_other_cpus_shutdown()

RFC PATCH Superseded Hamza Mahfooz 3 participants 6 messages Feb 20, 2025 – Mar 2, 2025

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 panic: call panic handlers before panic_other_cpus_shutdown()

Since, the panic handlers may require certain cpus to be online to panic gracefully, we should call them before turning off SMP. Without this re-ordering, on Hyper-V hv_panic_vmbus_unload() times out, because the vmbus channel is bound to VMBUS_CONNECT_CPU and unless the crashing cpu is the same as VMBUS_CONNECT_CPU, VMBUS_CONNECT_CPU will be offlined by crash_smp_send_stop() before the vmbus channel can be deconstructed.

---
 kernel/panic.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
kernel/panic.c +2 -2
index fbc59b3b64d0..9712a46dfe27 100644
@@ -372,8 +372,6 @@ void panic(const char *fmt, ...)
 	if (!_crash_kexec_post_notifiers)
 		__crash_kexec(NULL);
 
-	panic_other_cpus_shutdown(_crash_kexec_post_notifiers);
-
 	printk_legacy_allow_panic_sync();
 
 	/*
@@ -382,6 +380,8 @@ void panic(const char *fmt, ...)
 	 */
 	atomic_notifier_call_chain(&panic_notifier_list, 0, buf);
 
+	panic_other_cpus_shutdown(_crash_kexec_post_notifiers);
+
 	panic_print_sys_info(false);
 
 	kmsg_dump_desc(KMSG_DUMP_PANIC, buf);
Signature · 1 line
2.47.1
#1 lore raw

Hi Hamza!

Quoted 17 lines · Hamza Mahfooz

Since, the panic handlers may require certain cpus to be online to panic gracefully, we should call them before turning off SMP. Without this re-ordering, on Hyper-V hv_panic_vmbus_unload() times out, because the vmbus channel is bound to VMBUS_CONNECT_CPU and unless the crashing cpu is the same as VMBUS_CONNECT_CPU, VMBUS_CONNECT_CPU will be offlined by crash_smp_send_stop() before the vmbus channel can be deconstructed.

---
kernel/panic.c | 4 ++--

1 file changed, 2 insertions(+), 2 deletions(-)
kernel/panic.c +0 -0
index fbc59b3b64d0..9712a46dfe27 100644

kernel/panic.c void panic(const char *fmt, ...) · line 375

Quoted 6 lines
kernel/panic.c +0 -2
@@ -372,8 +372,6 @@ void panic(const char *fmt, ...)
 	if (!_crash_kexec_post_notifiers)
 		__crash_kexec(NULL);
 
-	panic_other_cpus_shutdown(_crash_kexec_post_notifiers);
-
 	printk_legacy_allow_panic_sync();

I think printk_legacy_allow_panic_sync() is placed after panic_other_cpus_shutdown() so that it flushes the stored cpus backtraces as described [0].

kernel/panic.c void panic(const char *fmt, ...) · line 384

Quoted 7 lines
kernel/panic.c +2 -0
 	/*
@@ -382,6 +380,8 @@ void panic(const char *fmt, ...)
 	 */
 	atomic_notifier_call_chain(&panic_notifier_list, 0, buf);
 
+	panic_other_cpus_shutdown(_crash_kexec_post_notifiers);
+

So maybe panic_other_cpus_shutdown() should be palced after atomic_notifier_call_chain() along with printk_legacy_allow_panic_sync() like below?

----- BEGIN -----

kernel/panic.c +4 -4
index d8635d5cecb2..7ac40e85ee27 100644
@@ -372,16 +372,16 @@ void panic(const char *fmt, ...)
        if (!_crash_kexec_post_notifiers)
                __crash_kexec(NULL);
 
-       panic_other_cpus_shutdown(_crash_kexec_post_notifiers);
-
-       printk_legacy_allow_panic_sync();
-
        /*
         * Run any panic handlers, including those that might need to
         * add information to the kmsg dump output.
         */
        atomic_notifier_call_chain(&panic_notifier_list, 0, buf);
 
+       panic_other_cpus_shutdown(_crash_kexec_post_notifiers);
+
+       printk_legacy_allow_panic_sync();
+
        panic_print_sys_info(false);
 
        kmsg_dump_desc(KMSG_DUMP_PANIC, buf);

----- END -----

Sincerely,
Ryo Takakura

[0] https://lore.kernel.org/all/[email protected]/

#2 lore raw

Hey Ryo,

Quoted 60 lines · Ryo Takakura

Hi Hamza!

Quoted 17 lines · Hamza Mahfooz

Since, the panic handlers may require certain cpus to be online to panic gracefully, we should call them before turning off SMP. Without this re-ordering, on Hyper-V hv_panic_vmbus_unload() times out, because the vmbus channel is bound to VMBUS_CONNECT_CPU and unless the crashing cpu is the same as VMBUS_CONNECT_CPU, VMBUS_CONNECT_CPU will be offlined by crash_smp_send_stop() before the vmbus channel can be deconstructed.

---
kernel/panic.c | 4 ++--

1 file changed, 2 insertions(+), 2 deletions(-)
kernel/panic.c +0 -0
index fbc59b3b64d0..9712a46dfe27 100644

kernel/panic.c void panic(const char *fmt, ...) · line 375

Quoted 6 lines
kernel/panic.c +0 -2
@@ -372,8 +372,6 @@ void panic(const char *fmt, ...)
 	if (!_crash_kexec_post_notifiers)
 		__crash_kexec(NULL);
 
-	panic_other_cpus_shutdown(_crash_kexec_post_notifiers);
-
 	printk_legacy_allow_panic_sync();

I think printk_legacy_allow_panic_sync() is placed after panic_other_cpus_shutdown() so that it flushes the stored cpus backtraces as described [0].

kernel/panic.c void panic(const char *fmt, ...) · line 384

Quoted 7 lines
kernel/panic.c +2 -0
 	/*
@@ -382,6 +380,8 @@ void panic(const char *fmt, ...)
 	 */
 	atomic_notifier_call_chain(&panic_notifier_list, 0, buf);
 
+	panic_other_cpus_shutdown(_crash_kexec_post_notifiers);
+

So maybe panic_other_cpus_shutdown() should be palced after atomic_notifier_call_chain() along with printk_legacy_allow_panic_sync() like below?

----- BEGIN -----

kernel/panic.c +0 -4
index d8635d5cecb2..7ac40e85ee27 100644
@@ -372,16 +372,16 @@ void panic(const char *fmt, ...)
        if (!_crash_kexec_post_notifiers)
                __crash_kexec(NULL);
 
-       panic_other_cpus_shutdown(_crash_kexec_post_notifiers);
-
-       printk_legacy_allow_panic_sync();
-
        /*
         * Run any panic handlers, including those that might need to
         * add information to the kmsg dump output.
         */

kernel/panic.c void panic(const char *fmt, ...)

Quoted 10 lines
kernel/panic.c +4 -1
        atomic_notifier_call_chain(&panic_notifier_list, 0, buf);
 
+       panic_other_cpus_shutdown(_crash_kexec_post_notifiers);
+
+       printk_legacy_allow_panic_sync();
+
        panic_print_sys_info(false);
 
        kmsg_dump_desc(KMSG_DUMP_PANIC, buf);
----- END -----

Ya, that looks fine to me, that's actually how I had it initally, but I wasn't sure if it had to go before the panic handlers. So, I erred on the side of caution.

BR,
Hamza

Quoted 5 lines

Sincerely,
Ryo Takakura

[0] https://lore.kernel.org/all/[email protected]/

#3 lore raw
Quoted 67 lines · Hamza Mahfooz
Quoted 66 lines · Ryo Takakura
Quoted 17 lines · Hamza Mahfooz

Since, the panic handlers may require certain cpus to be online to panic gracefully, we should call them before turning off SMP. Without this re-ordering, on Hyper-V hv_panic_vmbus_unload() times out, because the vmbus channel is bound to VMBUS_CONNECT_CPU and unless the crashing cpu is the same as VMBUS_CONNECT_CPU, VMBUS_CONNECT_CPU will be offlined by crash_smp_send_stop() before the vmbus channel can be deconstructed.

---
kernel/panic.c | 4 ++--

1 file changed, 2 insertions(+), 2 deletions(-)
kernel/panic.c +0 -0
index fbc59b3b64d0..9712a46dfe27 100644

kernel/panic.c void panic(const char *fmt, ...) · line 375

Quoted 6 lines
kernel/panic.c +0 -2
@@ -372,8 +372,6 @@ void panic(const char *fmt, ...)
 	if (!_crash_kexec_post_notifiers)
 		__crash_kexec(NULL);
 
-	panic_other_cpus_shutdown(_crash_kexec_post_notifiers);
-
 	printk_legacy_allow_panic_sync();

I think printk_legacy_allow_panic_sync() is placed after panic_other_cpus_shutdown() so that it flushes the stored cpus backtraces as described [0].

kernel/panic.c void panic(const char *fmt, ...) · line 384

Quoted 7 lines
kernel/panic.c +2 -0
 	/*
@@ -382,6 +380,8 @@ void panic(const char *fmt, ...)
 	 */
 	atomic_notifier_call_chain(&panic_notifier_list, 0, buf);
 
+	panic_other_cpus_shutdown(_crash_kexec_post_notifiers);
+

So maybe panic_other_cpus_shutdown() should be palced after atomic_notifier_call_chain() along with printk_legacy_allow_panic_sync() like below?

----- BEGIN -----

kernel/panic.c +4 -4
index d8635d5cecb2..7ac40e85ee27 100644
@@ -372,16 +372,16 @@ void panic(const char *fmt, ...)
        if (!_crash_kexec_post_notifiers)
                __crash_kexec(NULL);
 
-       panic_other_cpus_shutdown(_crash_kexec_post_notifiers);
-
-       printk_legacy_allow_panic_sync();
-
        /*
         * Run any panic handlers, including those that might need to
         * add information to the kmsg dump output.
         */
        atomic_notifier_call_chain(&panic_notifier_list, 0, buf);
 
+       panic_other_cpus_shutdown(_crash_kexec_post_notifiers);
+
+       printk_legacy_allow_panic_sync();
+
        panic_print_sys_info(false);
 
Quoted 6 lines

kernel/panic.c void panic(const char *fmt, ...)

Quoted 2 lines
kernel/panic.c +0 -1
        kmsg_dump_desc(KMSG_DUMP_PANIC, buf);
----- END -----

Ya, that looks fine to me, that's actually how I had it initally, but I wasn't sure if it had to go before the panic handlers. So, I erred on the side of caution.

I see, sorry that I was only speaking in relation to stored backtraces. It seems that printk_legacy_allow_panic_sync() is placed before atomic_notifier_call_chain() so that it can handle flushing before calling any panic handlers as described [0].

I'm not really familar with the problems associated with panic handlers so I hope maybe John and Petr can help on this matter...

Sincerely,
Ryo Takakura

Quoted 2 lines

BR,
Hamza

[0] https://lore.kernel.org/lkml/ZeHSgZs9I3Ihvpye@alley/

#4 lore raw
Quoted 37 lines · Ryo Takakura
Quoted 36 lines · Hamza Mahfooz
Quoted 35 lines · Ryo Takakura
Quoted 7 lines · Hamza Mahfooz

Since, the panic handlers may require certain cpus to be online to panic gracefully, we should call them before turning off SMP. Without this re-ordering, on Hyper-V hv_panic_vmbus_unload() times out, because the vmbus channel is bound to VMBUS_CONNECT_CPU and unless the crashing cpu is the same as VMBUS_CONNECT_CPU, VMBUS_CONNECT_CPU will be offlined by crash_smp_send_stop() before the vmbus channel can be deconstructed.

So maybe panic_other_cpus_shutdown() should be palced after atomic_notifier_call_chain() along with printk_legacy_allow_panic_sync() like below?

----- BEGIN -----

kernel/panic.c +3 -4
index d8635d5cecb2..7ac40e85ee27 100644
@@ -372,16 +372,16 @@ void panic(const char *fmt, ...)
        if (!_crash_kexec_post_notifiers)
                __crash_kexec(NULL);
 
-       panic_other_cpus_shutdown(_crash_kexec_post_notifiers);
-
-       printk_legacy_allow_panic_sync();
-
        /*
         * Run any panic handlers, including those that might need to
         * add information to the kmsg dump output.
         */
        atomic_notifier_call_chain(&panic_notifier_list, 0, buf);
 
+       panic_other_cpus_shutdown(_crash_kexec_post_notifiers);
+
+       printk_legacy_allow_panic_sync();
 
Quoted 8 lines
Quoted 8 lines

kernel/panic.c void panic(const char *fmt, ...)

Quoted 4 lines
kernel/panic.c +0 -1
        panic_print_sys_info(false);
 
        kmsg_dump_desc(KMSG_DUMP_PANIC, buf);
----- END -----

Ya, that looks fine to me, that's actually how I had it initally, but I wasn't sure if it had to go before the panic handlers. So, I erred on the side of caution.

The ordering (stopping CPUs before allowing printk_legacy loop) is important from the printk POV. So, keep it, please.

Quoted 9 lines

I see, sorry that I was only speaking in relation to stored backtraces. It seems that printk_legacy_allow_panic_sync() is placed before atomic_notifier_call_chain() so that it can handle flushing before calling any panic handlers as described [0].

[0] https://lore.kernel.org/lkml/ZeHSgZs9I3Ihvpye@alley/

I'm not really familar with the problems associated with panic handlers so I hope maybe John and Petr can help on this matter...

Honestly, I do not have much experience with failures of the panic notifiers. But I saw a patchset which tried to add filtering of some problematic ones, see
https://lore.kernel.org/lkml/[email protected]/

I did not like the way of ad-hoc filtering. The right solution was to fix the problematic notifiers.

Anyway, it went out that the situation was not that easy. The notifiers do various things. Some of them just printing extra information. Others stopped or suspended some devices or services. Some should be called before and some after crash_dump.

The outcome was a monster-patchset which tried to fix some problematic notifiers and split them into more notifier chains, see
https://lore.kernel.org/all/[email protected]/

Some of the fixes were accepted but the split has never been done.

My opinion:

1. The best solution would be to make the problematic notifier working with stopped CPUs. The discussion around [v2] suggests that the author made it working at least for x86_64, see
https://lore.kernel.org/r/[email protected]

2. Another good solution might be to do the split of the notifier chain, for an example, see
https://lore.kernel.org/lkml/Yn0TnsWVxCcdB2yO@alley/

The problematic notifier can be then added into a chain which is called before stopping CPUs.

3. In the worst case, you could change the ordering as proposed above. I am just afraid that it might bring in new problems. There might be notifiers which were not tested with more running CPUs...

In general, the system is in an unpredictable state when panic() is called. Notifiers should not expect that non-panic CPUs will be able to handle any requests.

Also it looks like a good idea to stop non-panic CPUs as soon as possible. Otherwise, they might create more harm than good.

Best Regards,
Petr

#5 lore raw

Hi Petr!

Quoted 47 lines · Petr Mladek
Quoted 37 lines · Ryo Takakura
Quoted 36 lines · Hamza Mahfooz
Quoted 35 lines · Ryo Takakura
Quoted 7 lines · Hamza Mahfooz

Since, the panic handlers may require certain cpus to be online to panic gracefully, we should call them before turning off SMP. Without this re-ordering, on Hyper-V hv_panic_vmbus_unload() times out, because the vmbus channel is bound to VMBUS_CONNECT_CPU and unless the crashing cpu is the same as VMBUS_CONNECT_CPU, VMBUS_CONNECT_CPU will be offlined by crash_smp_send_stop() before the vmbus channel can be deconstructed.

So maybe panic_other_cpus_shutdown() should be palced after atomic_notifier_call_chain() along with printk_legacy_allow_panic_sync() like below?

----- BEGIN -----

kernel/panic.c +3 -4
index d8635d5cecb2..7ac40e85ee27 100644
@@ -372,16 +372,16 @@ void panic(const char *fmt, ...)
        if (!_crash_kexec_post_notifiers)
                __crash_kexec(NULL);
 
-       panic_other_cpus_shutdown(_crash_kexec_post_notifiers);
-
-       printk_legacy_allow_panic_sync();
-
        /*
         * Run any panic handlers, including those that might need to
         * add information to the kmsg dump output.
         */
        atomic_notifier_call_chain(&panic_notifier_list, 0, buf);
 
+       panic_other_cpus_shutdown(_crash_kexec_post_notifiers);
+
+       printk_legacy_allow_panic_sync();
 
Quoted 8 lines
Quoted 8 lines

kernel/panic.c void panic(const char *fmt, ...)

Quoted 4 lines
kernel/panic.c +0 -1
        panic_print_sys_info(false);
 
        kmsg_dump_desc(KMSG_DUMP_PANIC, buf);
----- END -----

Ya, that looks fine to me, that's actually how I had it initally, but I wasn't sure if it had to go before the panic handlers. So, I erred on the side of caution.

Quoted 2 lines

The ordering (stopping CPUs before allowing printk_legacy loop) is important from the printk POV. So, keep it, please.

Thanks for the check.

Quoted 23 lines
Quoted 9 lines

I see, sorry that I was only speaking in relation to stored backtraces. It seems that printk_legacy_allow_panic_sync() is placed before atomic_notifier_call_chain() so that it can handle flushing before calling any panic handlers as described [0].

[0] https://lore.kernel.org/lkml/ZeHSgZs9I3Ihvpye@alley/

I'm not really familar with the problems associated with panic handlers so I hope maybe John and Petr can help on this matter...

Honestly, I do not have much experience with failures of the panic notifiers. But I saw a patchset which tried to add filtering of some problematic ones, see
https://lore.kernel.org/lkml/[email protected]/

I did not like the way of ad-hoc filtering. The right solution was to fix the problematic notifiers.

Anyway, it went out that the situation was not that easy. The notifiers do various things. Some of them just printing extra information. Others stopped or suspended some devices or services. Some should be called before and some after crash_dump.

Quoted 5 lines

The outcome was a monster-patchset which tried to fix some problematic notifiers and split them into more notifier chains, see
https://lore.kernel.org/all/[email protected]/

Some of the fixes were accepted but the split has never been done.

I see. I went through some of the discussions on the thread [0] and I can see how complicated the subject is...

Quoted 6 lines

My opinion:

1. The best solution would be to make the problematic notifier working with stopped CPUs. The discussion around [v2] suggests that the author made it working at least for x86_64, see
https://lore.kernel.org/r/[email protected]

I agree. But I also like the below solution as well!

Quoted 6 lines

2. Another good solution might be to do the split of the notifier chain, for an example, see
https://lore.kernel.org/lkml/Yn0TnsWVxCcdB2yO@alley/

The problematic notifier can be then added into a chain which is called before stopping CPUs.

Thanks for sharing this! Such an interesting discussion on what and when should be handled in panic path. I think I have a better picture of panic() now :).

Quoted 11 lines

3. In the worst case, you could change the ordering as proposed above. I am just afraid that it might bring in new problems. There might be notifiers which were not tested with more running CPUs...

In general, the system is in an unpredictable state when panic() is called. Notifiers should not expect that non-panic CPUs will be able to handle any requests.

Also it looks like a good idea to stop non-panic CPUs as soon as possible. Otherwise, they might create more harm than good.

I see. I also think that 3. might introduce new problems... It goes against the general statements which you pointed out in which case its not really a problem of handler anymore.

Sincerely,
Ryo Takakura

Quoted 2 lines

Best Regards,
Petr

[0] https://lore.kernel.org/lkml/[email protected]/