All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] panic: Remove redundant panic-cpu backtrace
@ 2025-09-03 10:04 Petr Mladek
  2025-09-04  2:06 ` Feng Tang
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Petr Mladek @ 2025-09-03 10:04 UTC (permalink / raw)
  To: Sergey Senozhatsky, John Ogness, Andrew Morton
  Cc: Petr Mladek, Feng Tang, linux-kernel

From: Sergey Senozhatsky <senozhatsky@chromium.org>

Backtraces from all CPUs are printed during panic() when
SYS_INFO_ALL_CPU_BT is set. It shows the backtrace for
the panic-CPU even when it has already been explicitly
printed before.

Do not change the legacy code which prints the backtrace
in various context, for example, as part of Oops report,
right after panic message. It will always be visible in
the crash dump.

Instead, remember when the backtrace was printed, and skip
it when dumping the optional backtraces on all CPUs.

Signed-off-by: Sergey Senozhatsky <senozhatsky@chromium.org>
Link: https://lore.kernel.org/r/20250731030314.3818040-1-senozhatsky@chromium.org
[pmladek@suse.com: Handle situations when the backtrace was not printed for the panic CPU.]
Signed-off-by: Petr Mladek <pmladek@suse.com>
---
Hi,

I resend my proposal [1] as a proper patch.

Changes against v1:

  - Handle situations when the backtrace was not printed for
    the panic CPU.

[1] https://lore.kernel.org/all/aJs7p_UjPIfb_XYd@pathway/

kernel/panic.c | 30 +++++++++++++++++++++---------
 1 file changed, 21 insertions(+), 9 deletions(-)

diff --git a/kernel/panic.c b/kernel/panic.c
index 72fcbb5a071b..e3cec9bc05ef 100644
--- a/kernel/panic.c
+++ b/kernel/panic.c
@@ -67,6 +67,7 @@ static unsigned int warn_limit __read_mostly;
 static bool panic_console_replay;
 
 bool panic_triggering_all_cpu_backtrace;
+bool panic_this_cpu_backtrace_printed;
 
 int panic_timeout = CONFIG_PANIC_TIMEOUT;
 EXPORT_SYMBOL_GPL(panic_timeout);
@@ -328,6 +329,19 @@ void check_panic_on_warn(const char *origin)
 		      origin, limit);
 }
 
+static void panic_trigger_all_cpu_backtrace(void)
+{
+	/* Temporary allow non-panic CPUs to write their backtraces. */
+	panic_triggering_all_cpu_backtrace = true;
+
+	if (panic_this_cpu_backtrace_printed)
+		trigger_allbutcpu_cpu_backtrace(raw_smp_processor_id());
+	else
+		trigger_all_cpu_backtrace();
+
+	panic_triggering_all_cpu_backtrace = false;
+}
+
 /*
  * Helper that triggers the NMI backtrace (if set in panic_print)
  * and then performs the secondary CPUs shutdown - we cannot have
@@ -335,12 +349,8 @@ void check_panic_on_warn(const char *origin)
  */
 static void panic_other_cpus_shutdown(bool crash_kexec)
 {
-	if (panic_print & SYS_INFO_ALL_CPU_BT) {
-		/* Temporary allow non-panic CPUs to write their backtraces. */
-		panic_triggering_all_cpu_backtrace = true;
-		trigger_all_cpu_backtrace();
-		panic_triggering_all_cpu_backtrace = false;
-	}
+	if (panic_print & SYS_INFO_ALL_CPU_BT)
+		panic_trigger_all_cpu_backtrace();
 
 	/*
 	 * Note that smp_send_stop() is the usual SMP shutdown function,
@@ -422,13 +432,15 @@ void vpanic(const char *fmt, va_list args)
 		buf[len - 1] = '\0';
 
 	pr_emerg("Kernel panic - not syncing: %s\n", buf);
-#ifdef CONFIG_DEBUG_BUGVERBOSE
 	/*
 	 * Avoid nested stack-dumping if a panic occurs during oops processing
 	 */
-	if (!test_taint(TAINT_DIE) && oops_in_progress <= 1)
+	if (test_taint(TAINT_DIE) || oops_in_progress > 1) {
+		panic_this_cpu_backtrace_printed = true;
+	} else if (IS_ENABLED(CONFIG_DEBUG_BUGVERBOSE)) {
 		dump_stack();
-#endif
+		panic_this_cpu_backtrace_printed = true;
+	}
 
 	/*
 	 * If kgdb is enabled, give it a chance to run before we stop all
-- 
2.50.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH v2] panic: Remove redundant panic-cpu backtrace
  2025-09-03 10:04 [PATCH v2] panic: Remove redundant panic-cpu backtrace Petr Mladek
@ 2025-09-04  2:06 ` Feng Tang
  2025-09-04  2:34 ` Sergey Senozhatsky
  2025-09-05 13:49 ` John Ogness
  2 siblings, 0 replies; 4+ messages in thread
From: Feng Tang @ 2025-09-04  2:06 UTC (permalink / raw)
  To: Petr Mladek; +Cc: Sergey Senozhatsky, John Ogness, Andrew Morton, linux-kernel

On Wed, Sep 03, 2025 at 12:04:18PM +0200, Petr Mladek wrote:
> From: Sergey Senozhatsky <senozhatsky@chromium.org>
> 
> Backtraces from all CPUs are printed during panic() when
> SYS_INFO_ALL_CPU_BT is set. It shows the backtrace for
> the panic-CPU even when it has already been explicitly
> printed before.
> 
> Do not change the legacy code which prints the backtrace
> in various context, for example, as part of Oops report,
> right after panic message. It will always be visible in
> the crash dump.
> 
> Instead, remember when the backtrace was printed, and skip
> it when dumping the optional backtraces on all CPUs.
> 
> Signed-off-by: Sergey Senozhatsky <senozhatsky@chromium.org>
> Link: https://lore.kernel.org/r/20250731030314.3818040-1-senozhatsky@chromium.org
> [pmladek@suse.com: Handle situations when the backtrace was not printed for the panic CPU.]
> Signed-off-by: Petr Mladek <pmladek@suse.com>

It worked as expected and had the similar effect as Sergey's
original patch, upon my test. Feel free to add:

Tested-by: Feng Tang <feng.tang@linux.alibaba.com>

Thanks,
Feng

> ---
> Hi,
> 
> I resend my proposal [1] as a proper patch.
> 
> Changes against v1:
> 
>   - Handle situations when the backtrace was not printed for
>     the panic CPU.
> 
> [1] https://lore.kernel.org/all/aJs7p_UjPIfb_XYd@pathway/
> 
> kernel/panic.c | 30 +++++++++++++++++++++---------
>  1 file changed, 21 insertions(+), 9 deletions(-)
> 
> diff --git a/kernel/panic.c b/kernel/panic.c
> index 72fcbb5a071b..e3cec9bc05ef 100644
> --- a/kernel/panic.c
> +++ b/kernel/panic.c
> @@ -67,6 +67,7 @@ static unsigned int warn_limit __read_mostly;
>  static bool panic_console_replay;
>  
>  bool panic_triggering_all_cpu_backtrace;
> +bool panic_this_cpu_backtrace_printed;
>  
>  int panic_timeout = CONFIG_PANIC_TIMEOUT;
>  EXPORT_SYMBOL_GPL(panic_timeout);
> @@ -328,6 +329,19 @@ void check_panic_on_warn(const char *origin)
>  		      origin, limit);
>  }
>  
> +static void panic_trigger_all_cpu_backtrace(void)
> +{
> +	/* Temporary allow non-panic CPUs to write their backtraces. */
> +	panic_triggering_all_cpu_backtrace = true;
> +
> +	if (panic_this_cpu_backtrace_printed)
> +		trigger_allbutcpu_cpu_backtrace(raw_smp_processor_id());
> +	else
> +		trigger_all_cpu_backtrace();
> +
> +	panic_triggering_all_cpu_backtrace = false;
> +}
> +
>  /*
>   * Helper that triggers the NMI backtrace (if set in panic_print)
>   * and then performs the secondary CPUs shutdown - we cannot have
> @@ -335,12 +349,8 @@ void check_panic_on_warn(const char *origin)
>   */
>  static void panic_other_cpus_shutdown(bool crash_kexec)
>  {
> -	if (panic_print & SYS_INFO_ALL_CPU_BT) {
> -		/* Temporary allow non-panic CPUs to write their backtraces. */
> -		panic_triggering_all_cpu_backtrace = true;
> -		trigger_all_cpu_backtrace();
> -		panic_triggering_all_cpu_backtrace = false;
> -	}
> +	if (panic_print & SYS_INFO_ALL_CPU_BT)
> +		panic_trigger_all_cpu_backtrace();
>  
>  	/*
>  	 * Note that smp_send_stop() is the usual SMP shutdown function,
> @@ -422,13 +432,15 @@ void vpanic(const char *fmt, va_list args)
>  		buf[len - 1] = '\0';
>  
>  	pr_emerg("Kernel panic - not syncing: %s\n", buf);
> -#ifdef CONFIG_DEBUG_BUGVERBOSE
>  	/*
>  	 * Avoid nested stack-dumping if a panic occurs during oops processing
>  	 */
> -	if (!test_taint(TAINT_DIE) && oops_in_progress <= 1)
> +	if (test_taint(TAINT_DIE) || oops_in_progress > 1) {
> +		panic_this_cpu_backtrace_printed = true;
> +	} else if (IS_ENABLED(CONFIG_DEBUG_BUGVERBOSE)) {
>  		dump_stack();
> -#endif
> +		panic_this_cpu_backtrace_printed = true;
> +	}
>  
>  	/*
>  	 * If kgdb is enabled, give it a chance to run before we stop all
> -- 
> 2.50.1

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v2] panic: Remove redundant panic-cpu backtrace
  2025-09-03 10:04 [PATCH v2] panic: Remove redundant panic-cpu backtrace Petr Mladek
  2025-09-04  2:06 ` Feng Tang
@ 2025-09-04  2:34 ` Sergey Senozhatsky
  2025-09-05 13:49 ` John Ogness
  2 siblings, 0 replies; 4+ messages in thread
From: Sergey Senozhatsky @ 2025-09-04  2:34 UTC (permalink / raw)
  To: Petr Mladek
  Cc: Sergey Senozhatsky, John Ogness, Andrew Morton, Feng Tang,
	linux-kernel

On (25/09/03 12:04), Petr Mladek wrote:
> Backtraces from all CPUs are printed during panic() when
> SYS_INFO_ALL_CPU_BT is set. It shows the backtrace for
> the panic-CPU even when it has already been explicitly
> printed before.
> 
> Do not change the legacy code which prints the backtrace
> in various context, for example, as part of Oops report,
> right after panic message. It will always be visible in
> the crash dump.
> 
> Instead, remember when the backtrace was printed, and skip
> it when dumping the optional backtraces on all CPUs.
> 
> Signed-off-by: Sergey Senozhatsky <senozhatsky@chromium.org>

That's very kind of you, Petr.  Thanks for working on this.


// As a side note, my involvement was more of a
// Reported-and-tested-by: Sergey Senozhatsky <senozhatsky@chromium.org>
// Petr is the sole author of this patch.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v2] panic: Remove redundant panic-cpu backtrace
  2025-09-03 10:04 [PATCH v2] panic: Remove redundant panic-cpu backtrace Petr Mladek
  2025-09-04  2:06 ` Feng Tang
  2025-09-04  2:34 ` Sergey Senozhatsky
@ 2025-09-05 13:49 ` John Ogness
  2 siblings, 0 replies; 4+ messages in thread
From: John Ogness @ 2025-09-05 13:49 UTC (permalink / raw)
  To: Petr Mladek, Sergey Senozhatsky, Andrew Morton
  Cc: Petr Mladek, Feng Tang, linux-kernel

On 2025-09-03, Petr Mladek <pmladek@suse.com> wrote:

> From: Sergey Senozhatsky <senozhatsky@chromium.org>
>
> Backtraces from all CPUs are printed during panic() when
> SYS_INFO_ALL_CPU_BT is set. It shows the backtrace for
> the panic-CPU even when it has already been explicitly
> printed before.
>
> Do not change the legacy code which prints the backtrace
> in various context, for example, as part of Oops report,
> right after panic message. It will always be visible in
> the crash dump.
>
> Instead, remember when the backtrace was printed, and skip
> it when dumping the optional backtraces on all CPUs.
>
> Signed-off-by: Sergey Senozhatsky <senozhatsky@chromium.org>
> Link: https://lore.kernel.org/r/20250731030314.3818040-1-senozhatsky@chromium.org
> [pmladek@suse.com: Handle situations when the backtrace was not printed for the panic CPU.]
> Signed-off-by: Petr Mladek <pmladek@suse.com>
> ---
> diff --git a/kernel/panic.c b/kernel/panic.c
> index 72fcbb5a071b..e3cec9bc05ef 100644
> --- a/kernel/panic.c
> +++ b/kernel/panic.c
> @@ -422,13 +432,15 @@ void vpanic(const char *fmt, va_list args)
>  		buf[len - 1] = '\0';
>  
>  	pr_emerg("Kernel panic - not syncing: %s\n", buf);
> -#ifdef CONFIG_DEBUG_BUGVERBOSE
>  	/*
>  	 * Avoid nested stack-dumping if a panic occurs during oops processing
>  	 */
> -	if (!test_taint(TAINT_DIE) && oops_in_progress <= 1)
> +	if (test_taint(TAINT_DIE) || oops_in_progress > 1) {
> +		panic_this_cpu_backtrace_printed = true;
> +	} else if (IS_ENABLED(CONFIG_DEBUG_BUGVERBOSE)) {
>  		dump_stack();
> -#endif
> +		panic_this_cpu_backtrace_printed = true;
> +	}

It took me a while to wrap my brain around this new logic. I guess in
part because the comment above only tells a part of the story. I tried
to write a suggestion for the comment, but it ended up being just as
confusing as the code. So, I guess we just leave it...

It seems this patch has already been picked up in the mm-tree (with a
patch on top), but if not:

Reviewed-by: John Ogness <john.ogness@linutronix.de>

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2025-09-05 13:49 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-09-03 10:04 [PATCH v2] panic: Remove redundant panic-cpu backtrace Petr Mladek
2025-09-04  2:06 ` Feng Tang
2025-09-04  2:34 ` Sergey Senozhatsky
2025-09-05 13:49 ` John Ogness

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.