panic, crash_core, sys_info: Introduce crash_kexec_in_memory_sys_info
Subsystems (14)
v2 cover panic, crash_core, sys_info: Introduce crash_kexec_in_memory_sys_info
In modern fleet environments, capturing a full multi-gigabyte vmcore via kdump can occasionally fail or be intentionally skipped due to storage, network, or encryption constraints. In these scenarios, the extracted dmesg buffer (e.g. vmcore-dmesg.txt) is often the sole artefact available for post-mortem triage.
Currently, appending supplemental system state (such as memory usage, held locks, active timers, task states, or ftrace buffers) via panic_print prior to kdump requires enabling crash_kexec_post_notifiers. Doing so exposes the crash path to registered panic notifiers (vastly increasing the risk of hanging or deadlocking) and forces panic_print output to be flushed synchronously to physical consoles. On a slow serial console or IPMI Serial-over-LAN (e.g. 115200 baud), flushing comprehensive sys_info() output (e.g. tasks or stacktraces) can exceed 10 seconds, allowing the hardware timer to expire and reset the system before kdump can initialise. Furthermore, on large systems, high-volume telemetry can wrap the printk ring buffer and overwrite the initial Oops, BUG, or panic stack trace.
This series introduces the "crash_kexec_in_memory_sys_info" kernel command-line parameter to decouple in-memory telemetry collection from both panic notifiers and physical console flushing when kdump is active:
1. Fixes duplicate and dropped all-CPU backtraces when SYS_INFO_ALL_BT is configured in panic_print across early SMP shutdown and __sys_info().
2. Avoids spinning indefinitely on zone->lock in show_free_areas() during NMI, Oops, or panic contexts by falling back to non-blocking trylock acquisitions and skipping contended zones.
3. Avoids blocking on timekeeping seqcounts and hrtimer_cpu_base locks in sysrq_timer_list_show() and print_active_timers() during NMI, Oops, or panic contexts.
4. Adds printk console flush suppression across nbcon, legacy consoles, and klogd, alongside ring buffer tail pinning so pre-crash records can still be recycled while protecting the initial crash output from being overwritten by subsequent telemetry or ftrace dumps.
5. Introduces crash_kexec_in_memory_sys_info and invokes panic_in_memory_sys_info() from __crash_kexec() prior to machine_crash_shutdown() when crash_kexec_post_notifiers is not enabled. It also marks the start of crash diagnostics across bust_spinlocks(), __report_bug(), oops_enter(), and vpanic(), and reorders __sys_info() so compact, high-signal dumps (SYS_INFO_MEM, SYS_INFO_LOCKS, SYS_INFO_TIMERS, SYS_INFO_BLOCKED_TASKS) execute before high-volume dumps (SYS_INFO_ALL_BT, SYS_INFO_TASKS, SYS_INFO_FTRACE).
Changes since v1:
- Split the single patch into a 5-patch series separating prerequisite panic all-CPU backtrace fixes, crash-time lock safety in show_mem() and timer_list, printk/tracing infrastructure, and the crash_kexec integration
- Moved in-memory sys_info() collection from vpanic() into __crash_kexec() via panic_in_memory_sys_info() with local interrupts disabled so all crash_kexec() entry points (including direct Oops/die paths prior to vpanic()) are covered, and kept console flush suppression and tail pinning active through machine_crash_shutdown() and machine_kexec() unless machine_kexec() returns
- Replaced unconditional ring buffer tail freezing with crash-start sequence pinning (printk_mark_panic_start(), printk_clear_panic_start(), and printk_is_tail_full()), allowing pre-crash records to be recycled while preserving the initial Oops/panic trace, finalising the last record when full, and stopping ftrace_dump_one() before consuming unprinted trace entries
- Extended printk_suppress_console_flush() to cover nbcon atomic flushes and kthreads, legacy console_flush_one_record() and kthreads, and wake_up_klogd(), and dropped unnecessary EXPORT_SYMBOL_GPL exports
- Added crash-context trylock and NMI-safe timekeeping handling in show_free_areas(), sysrq_timer_list_show(), and print_active_timers(), and fixed duplicate/dropped all-CPU backtraces when SYS_INFO_ALL_BT is used during panic
- Link to v1: https://lore.kernel.org/lkml/[email protected]/
Aaron Tomlin (5):
panic, sys_info: Fix duplicate and dropped all-CPU backtraces in panic mm/show_mem: Avoid blocking on zone->lock during Oops or panic
timers: Avoid blocking on hrtimer_cpu_base lock during Oops or panic printk, tracing: Support console flush suppression and tail pinning panic, crash_core, sys_info: Introduce crash_kexec_in_memory_sys_info
.../admin-guide/kernel-parameters.txt | 11 ++ include/linux/panic.h | 2 + include/linux/printk.h | 27 +++ include/linux/sched.h | 3 + include/linux/sys_info.h | 4 + kernel/crash_core.c | 6 + kernel/exit.c | 10 ++ kernel/fork.c | 3 + kernel/panic.c | 87 ++++++++- kernel/printk/internal.h | 4 + kernel/printk/nbcon.c | 11 +- kernel/printk/printk.c | 29 ++- kernel/printk/printk_ringbuffer.c | 168 +++++++++++++++++- kernel/time/timer_list.c | 13 +- kernel/trace/trace.c | 20 ++- lib/bug.c | 18 +- lib/bust_spinlocks.c | 22 ++- lib/sys_info.c | 33 ++-- mm/show_mem.c | 15 +- 19 files changed, 447 insertions(+), 39 deletions(-)
Signature · 1 line
2.55.0
RFC PATCH v2 1/5 panic, sys_info: Fix duplicate and dropped all-CPU backtraces in panic
When sys_info() was factored out of kernel/panic.c by commit b76e89e50fc3 ("panic: generalize panic_print's function to show sys info"), __sys_info() gained an unconditional call to trigger_all_cpu_backtrace() when SYS_INFO_ALL_BT is set, while panic_other_cpus_shutdown() retained its own call to panic_trigger_all_cpu_backtrace().
This causes two problems during a kernel panic:
1. vpanic() invokes panic_trigger_all_cpu_backtrace() inside panic_other_cpus_shutdown() before stopping secondary CPUs, and then invokes trigger_all_cpu_backtrace() a second time inside sys_info(panic_print) after secondary CPUs have already been shut down, stalling for up to 10 seconds waiting for offline CPUs to respond to NMIs. Furthermore, if SYS_INFO_ALL_BT is configured via the kernel.kernel_sys_info sysctl rather than panic_print, panic_other_cpus_shutdown() misses it prior to CPU shutdown.
2. If sys_info(SYS_INFO_ALL_BT) is invoked on the panicking CPU prior to panic_other_cpus_shutdown(), calling trigger_all_cpu_backtrace() directly bypasses panic_trigger_all_cpu_backtrace() and leaves panic_triggering_all_cpu_backtrace unset. Because panic_cpu is already active, vprintk_emit() silently drops all NMI backtrace messages originating from non-panic CPUs.
Export panic_trigger_all_cpu_backtrace() with a panic_on_this_cpu() and one-shot guard so all-CPU backtraces are only emitted once before secondary CPUs are stopped, consult sys_info_get_mask(panic_print) in panic_other_cpus_shutdown(), record prior Oops stack dumps in panic_try_start(), and route SYS_INFO_ALL_BT in __sys_info() through panic_trigger_all_cpu_backtrace() on the panicking CPU.
--- include/linux/panic.h | 1 + include/linux/sys_info.h | 1 + kernel/panic.c | 31 +++++++++++++++++++++++++++---- lib/sys_info.c | 17 ++++++++++++++--- 4 files changed, 43 insertions(+), 7 deletions(-)
include/linux/panic.h +1 -0
@@ -57,6 +57,7 @@ void panic_reset(void);bool panic_in_progress (void );bool panic_on_this_cpu (void );bool panic_on_other_cpu (void ); +void panic_trigger_all_cpu_backtrace (void );/* * Only to be used by arch init code. If the user over-wrote the default
include/linux/sys_info.h +1 -0
@@ -18,6 +18,7 @@#define SYS_INFO_BLOCKED_TASKS 0x00000080void sys_info (unsigned long si_mask); +unsigned long sys_info_get_mask (unsigned long si_mask);unsigned long sys_info_parse_param (char *str);#ifdef CONFIG_SYSCTL
kernel/panic.c +27 -4
@@ -70,6 +70,7 @@ static bool panic_console_replay;bool panic_triggering_all_cpu_backtrace;static bool panic_this_cpu_backtrace_printed; +static bool panic_all_cpu_backtrace_printed;int panic_timeout =CONFIG_PANIC_TIMEOUT ;EXPORT_SYMBOL_GPL (panic_timeout);
@@ -451,13 +452,21 @@ bool panic_try_start(void) old_cpu =PANIC_CPU_INVALID ; this_cpu =raw_smp_processor_id (); -return atomic_try_cmpxchg (&panic_cpu, &old_cpu, this_cpu); +if (!atomic_try_cmpxchg (&panic_cpu, &old_cpu, this_cpu)) +return false; + +if (atomic_read (&panic_redirect_cpu) ==PANIC_CPU_INVALID && + (test_taint (TAINT_DIE ) || oops_in_progress)) + panic_this_cpu_backtrace_printed = true; + +return true; }EXPORT_SYMBOL (panic_try_start);void panic_reset (void ) { -atomic_set (&panic_cpu,PANIC_CPU_INVALID ); + panic_this_cpu_backtrace_printed = false; +atomic_set_release (&panic_cpu,PANIC_CPU_INVALID ); }EXPORT_SYMBOL (panic_reset);
@@ -538,8 +547,21 @@ void check_panic_on_warn(const char *origin) origin, limit); } -static void panic_trigger_all_cpu_backtrace (void ) +/** +* panic_trigger_all_cpu_backtrace - trigger NMI backtraces on all CPUs once +* +* Temporarily permit non-panic CPUs to emit messages into the printk ring +* buffer while capturing all-CPU backtraces during a panic. Subsequent calls +* are ignored so backtraces are not re-triggered after secondary CPUs have +* been shut down. +*/ +void panic_trigger_all_cpu_backtrace (void ) { +if (!panic_on_this_cpu () || panic_all_cpu_backtrace_printed) +return ; + + panic_all_cpu_backtrace_printed = true; +/* Temporary allow non-panic CPUs to write their backtraces. */ panic_triggering_all_cpu_backtrace = true;
@@ -558,8 +580,9 @@ static void panic_trigger_all_cpu_backtrace(void)*/ static void panic_other_cpus_shutdown (bool crash_kexec) { -if (panic_print &SYS_INFO_ALL_BT ) +if (sys_info_get_mask (panic_print) &SYS_INFO_ALL_BT )panic_trigger_all_cpu_backtrace (); + panic_all_cpu_backtrace_printed = true;/* * Note that smp_send_stop() is the usual SMP shutdown function,
lib/sys_info.c +14 -3
@@ -7,6 +7,7 @@#include <linux/kernel.h> #include <linux/ftrace.h> #include <linux/nmi.h> +#include <linux/panic.h> #include <linux/sched/debug.h> #include <linux/string.h> #include <linux/sysctl.h>
@@ -153,14 +154,24 @@ static void __sys_info(unsigned long si_mask)if (si_mask &SYS_INFO_FTRACE )ftrace_dump (DUMP_ALL ); -if (si_mask &SYS_INFO_ALL_BT ) -trigger_all_cpu_backtrace (); +if (si_mask &SYS_INFO_ALL_BT ) { +if (panic_on_this_cpu ()) +panic_trigger_all_cpu_backtrace (); +else if (!panic_in_progress ()) +trigger_all_cpu_backtrace (); + }if (si_mask &SYS_INFO_BLOCKED_TASKS )show_state_filter (TASK_UNINTERRUPTIBLE ); } +unsigned long sys_info_get_mask (unsigned long si_mask) +{ +return (si_mask & ~SYS_INFO_PANIC_CONSOLE_REPLAY ) ? : +READ_ONCE (kernel_si_mask); +} +void sys_info (unsigned long si_mask) { -__sys_info (si_mask ? : kernel_si_mask); +__sys_info (sys_info_get_mask (si_mask)); }
Signature · 1 line
2.55.0
RFC PATCH v2 2/5 mm/show_mem: Avoid blocking on zone->lock during Oops or panic
In reply to Aaron TomlinWhen show_free_areas() iterates over populated memory zones, it unconditionally acquires zone->lock via spin_lock_irqsave(). If show_mem() is invoked from an Oops or panic context while the crashing CPU or a wedged remote CPU already holds zone->lock, or from an NMI or hardirq context under CONFIG_PREEMPT_RT where spinlock_t is a sleeping rt_mutex, acquiring zone->lock can self-deadlock or fault before post-mortem recovery (such as kdump) can proceed.
Check can_spin_trylock() and use spin_trylock_irqsave() when !can_spin_trylock(), in_nmi(), oops_in_progress, or panic_in_progress() is active, and emit the zone header after releasing zone->lock, logging a contention notice and skipping the locked zone rather than spinning indefinitely.
--- mm/show_mem.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-)
mm/show_mem.c +12 -3
@@ -367,10 +367,17 @@ static void show_free_areas(unsigned int filter, const nodemask_t *nodemask,continue ;if (show_mem_node_skip (filter,zone_to_nid (zone), nodemask))continue ; -show_node (zone); -printk (KERN_CONT "%s: " , zone->name); -spin_lock_irqsave (&zone->lock, flags); +if (!can_spin_trylock () ||in_nmi () || + oops_in_progress ||panic_in_progress ()) { +if (!can_spin_trylock () || + !spin_trylock_irqsave (&zone->lock, flags)) { +pr_warn ("zone %s lock contended\n" , zone->name); +continue ; + } + }else { +spin_lock_irqsave (&zone->lock, flags); + }for (order =0 ; order <NR_PAGE_ORDERS ; order++) {struct free_area *area = &zone->free_area[order];int type;
@@ -385,6 +392,8 @@ static void show_free_areas(unsigned int filter, const nodemask_t *nodemask, } }spin_unlock_irqrestore (&zone->lock, flags); +show_node (zone); +printk (KERN_CONT "%s: " , zone->name);for (order =0 ; order <NR_PAGE_ORDERS ; order++) {printk (KERN_CONT "%lu*%lukB " , nr[order],K (1UL ) << order);
Signature · 1 line
2.55.0
RFC PATCH v2 3/5 timers: Avoid blocking on hrtimer_cpu_base lock during Oops or panic
In reply to Aaron TomlinWhen sysrq_timer_list_show() dumps active high-resolution timers across all online CPUs, it calls ktime_get() (which spins on tk_core.seq) and print_active_timers() unconditionally acquires each CPU's base->cpu_base->lock via raw_spin_lock_irqsave(). If invoked from an Oops, NMI, or panic context while the crashing CPU or a wedged remote CPU is inside a timekeeping write section or already holds base->cpu_base->lock, sysrq_timer_list_show() will spin indefinitely and prevent panic recovery or kexec from completing.
Use the NMI-safe ktime_get_mono_fast_ns() helper insysrq_timer_list_show() and raw_spin_trylock_irqsave() in print_active_timers() when in_nmi(), oops_in_progress, or panic_in_progress() is active, noting any contended lock and returning cleanly instead of deadlocking.
--- kernel/time/timer_list.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-)
kernel/time/timer_list.c +11 -2
@@ -68,7 +68,15 @@ static void print_active_timers(struct seq_file *m, struct hrtimer_clock_base *btouch_nmi_watchdog (); -raw_spin_lock_irqsave (&base->cpu_base->lock, flags); +if (in_nmi () || oops_in_progress ||panic_in_progress ()) { +if ((!IS_ENABLED (CONFIG_SMP ) &&in_nmi ()) || + !raw_spin_trylock_irqsave (&base->cpu_base->lock, flags)) { +SEQ_printf (m," [cpu_base lock contended]\n" ); +return ; + } + }else { +raw_spin_lock_irqsave (&base->cpu_base->lock, flags); + } curr =timerqueue_linked_first (&base->active);/*
@@ -262,7 +270,8 @@ static inline void timer_list_header(struct seq_file *m, ktime_t now)void sysrq_timer_list_show (void ) { -ktime_t now =ktime_get (); +ktime_t now = (in_nmi () || oops_in_progress ||panic_in_progress ()) ? +ns_to_ktime (ktime_get_mono_fast_ns ()) :ktime_get ();int cpu;timer_list_header (NULL , now);
Signature · 1 line
2.55.0
RFC PATCH v2 4/5 printk, tracing: Support console flush suppression and tail pinning
In reply to Aaron TomlinWhen collecting high-volume diagnostic output into the printk ring buffer during a kernel crash prior to kexec, synchronous console emission can stall the panicking CPU for tens of seconds on slow serial or IPMI Serial-over-LAN links, risking external hardware watchdog resets. At the same time, uninhibited ring buffer rotation can wrap and overwrite the initial Oops, faulting registers, and primary call trace.
Introduce two scoped mechanisms for in-memory crash telemetry collection:
1. Console flush suppression
Provide printk_suppress_console_flush(bool) to clear the console flush mask in printk_get_console_flush_type(), guard legacy record emission and kthread wakeups, and suppress wake_up_klogd() in vprintk_emit(). Records written via vprintk_store() are stored in the in-memory ring buffer while bypassing synchronous hardware console emission and kthread wakeups.
2. Ring buffer tail pinning
Provide printk_mark_panic_start(), printk_clear_panic_start(), printk_freeze_tail(bool), and printk_is_tail_full(). Recording the next reserved sequence number at the start of a crash (or clamping to prb_first_valid_seq(prb) if the crash output itself already wrapped the buffer) allows data_make_reusable() and desc_push_tail() to continue recycling older pre-crash records while refusing to advance the tail past the pinned crash sequence number. Once the watermark is reached, prb_reserve(), prb_reserve_in_last(), and ftrace_dump_one() finalize the last record and stop reserving descriptors or consuming trace entries so the primary crash context and remaining trace buffer remain intact.
--- include/linux/printk.h | 27 +++++ kernel/printk/internal.h | 4 + kernel/printk/nbcon.c | 11 +- kernel/printk/printk.c | 29 +++++- kernel/printk/printk_ringbuffer.c | 168 ++++++++++++++++++++++++++++-- kernel/trace/trace.c | 20 +++- 6 files changed, 244 insertions(+), 15 deletions(-)
include/linux/printk.h +27 -0
@@ -205,6 +205,11 @@ extern asmlinkage void dump_stack(void) __cold;void printk_trigger_flush (void );void console_try_replay_all (void );void printk_legacy_allow_panic_sync (void ); +void printk_suppress_console_flush (bool suppress); +bool printk_mark_panic_start (void ); +void printk_clear_panic_start (void ); +void printk_freeze_tail (bool freeze); +bool printk_is_tail_full (void );extern bool nbcon_device_try_acquire (struct console *con);extern void nbcon_device_release (struct console *con);void nbcon_atomic_flush_unsafe (void );
@@ -309,6 +314,28 @@ static inline void printk_legacy_allow_panic_sync(void) { } +static inline void printk_suppress_console_flush (bool suppress) +{ +} + +static inline bool printk_mark_panic_start (void ) +{ +return false; +} + +static inline void printk_clear_panic_start (void ) +{ +} + +static inline void printk_freeze_tail (bool freeze) +{ +} + +static inline bool printk_is_tail_full (void ) +{ +return false; +} +static inline bool nbcon_device_try_acquire (struct console *con) {return false;
kernel/printk/internal.h +4 -0
@@ -186,6 +186,7 @@ struct console_flush_type { };extern bool console_irqwork_blocked; +extern bool console_flush_suppressed;/* * Identify which console flushing methods should be used in the context of
@@ -195,6 +196,9 @@ static inline void printk_get_console_flush_type(struct console_flush_type *ft) {memset (ft,0 ,sizeof (*ft)); +if (unlikely (READ_ONCE (console_flush_suppressed))) +return ; +switch (nbcon_get_default_prio ()) {case NBCON_PRIO_NORMAL :if (have_nbcon_console && !have_boot_console) {
kernel/printk/nbcon.c +8 -3
@@ -1194,7 +1194,8 @@ static bool nbcon_kthread_should_wakeup(struct console *con, struct nbcon_contex* ownership in the middle of a message. */ if (unlikely (atomic_read (&nbcon_cpu_emergency_cnt)) || -unlikely (panic_in_progress ())) +unlikely (panic_in_progress ()) || +unlikely (READ_ONCE (console_flush_suppressed)))return false; cookie =console_srcu_read_lock ();
@@ -1253,7 +1254,8 @@ static int nbcon_kthread_func(void *__console)* mode. See nbcon_kthread_should_wakeup() for more details. */ if (unlikely (atomic_read (&nbcon_cpu_emergency_cnt)) || -unlikely (panic_in_progress ())) +unlikely (panic_in_progress ()) || +unlikely (READ_ONCE (console_flush_suppressed))) goto wait_for_event; backlog = false;
@@ -1642,7 +1644,7 @@ static void nbcon_atomic_flush_pending_con(struct console *con, u64 stop_seq)* is not available do it. */ printk_get_console_flush_type (&ft); -if (!ft.nbcon_offload && +if (ft.nbcon_atomic && !ft.nbcon_offload &&prb_read_valid (prb,nbcon_seq_read (con),NULL )) { stop_seq =prb_next_reserve_seq (prb); goto again;
@@ -1659,6 +1661,9 @@ static void __nbcon_atomic_flush_pending(u64 stop_seq)struct console *con;int cookie; +if (unlikely (READ_ONCE (console_flush_suppressed))) +return ; + cookie =console_srcu_read_lock ();for_each_console_srcu (con) {short flags =console_srcu_read_flags (con);
kernel/printk/printk.c +28 -1
@@ -467,6 +467,9 @@ bool legacy_allow_panic_sync;/* Avoid using irq_work when suspending. */ bool console_irqwork_blocked; +/* Suppress synchronous console flushing during in-memory panic dumps. */ +bool console_flush_suppressed; +#ifdef CONFIG_PRINTK DECLARE_WAIT_QUEUE_HEAD (log_wait);static DECLARE_WAIT_QUEUE_HEAD (legacy_wait);
@@ -2386,6 +2389,23 @@ int vprintk_store(int facility, int level,return ret; } +/** +* printk_suppress_console_flush - suppress console flushing in printk() +* @suppress: true to suppress console emission and klogd wakeups, false to +* restore normal flushing behaviour +* +* Context: Any context. +*/ +void printk_suppress_console_flush (bool suppress) +{ +WRITE_ONCE (console_flush_suppressed, suppress); +if (!suppress) { +printk_trigger_flush (); +if (!console_irqwork_blocked) +wake_up_klogd (); + } +} +/* * This acts as a one-way switch to allow legacy consoles to print from * the printk() caller context on a panic CPU. It also attempts to flush
@@ -2482,7 +2502,8 @@ asmlinkage int vprintk_emit(int facility, int level,if (ft.legacy_offload)defer_console_output (); -else if (!console_irqwork_blocked) +else if (!console_irqwork_blocked && + !unlikely (READ_ONCE (console_flush_suppressed)))wake_up_klogd ();return printed_len;
@@ -3243,6 +3264,9 @@ static bool console_flush_one_record(bool do_cond_resched, u64 *next_seq, bool * *try_again = false; +if (unlikely (READ_ONCE (console_flush_suppressed))) +return false; +printk_get_console_flush_type (&ft); cookie =console_srcu_read_lock ();
@@ -3665,6 +3689,9 @@ static bool legacy_kthread_should_wakeup(void)if (kthread_should_stop ())return true; +if (unlikely (READ_ONCE (console_flush_suppressed))) +return false; +printk_get_console_flush_type (&ft); cookie =console_srcu_read_lock ();
kernel/printk/printk_ringbuffer.c +161 -7
254 lines not on this page · raw message
kernel/trace/trace.c +16 -4
@@ -9519,6 +9519,9 @@ static void ftrace_dump_one(struct trace_array *tr, enum ftrace_dump_mode dump_melse iter.cpu_file =RING_BUFFER_ALL_CPUS ; +if (printk_is_tail_full ()) + goto out_enable; +if (tr == &global_trace)printk (KERN_TRACE "Dumping ftrace buffer:\n" );else
@@ -9537,10 +9540,13 @@ static void ftrace_dump_one(struct trace_array *tr, enum ftrace_dump_mode dump_m* and then release the locks again. */ -while (!trace_empty (&iter)) { +while (!trace_empty (&iter) && !printk_is_tail_full ()) { -if (!cnt) +if (!cnt) {printk (KERN_TRACE "---------------------------------\n" ); +if (printk_is_tail_full ()) +break ; + } cnt++;
@@ -9551,19 +9557,25 @@ static void ftrace_dump_one(struct trace_array *tr, enum ftrace_dump_mode dump_mint ret; ret =print_trace_line (&iter); +trace_printk_seq (&iter.seq); +if (printk_is_tail_full ()) +break ; +if (ret !=TRACE_TYPE_NO_CONSUME )trace_consume (&iter); - -trace_printk_seq (&iter.seq); }touch_nmi_watchdog (); } +if (printk_is_tail_full ()) + goto out_enable; +if (!cnt)printk (KERN_TRACE " (ftrace buffer empty)\n" );else printk (KERN_TRACE "---------------------------------\n" ); +out_enable: tr->trace_flags |= old_userobj;tracer_tracing_enable (tr);
Signature · 1 line
2.55.0
RFC PATCH v2 5/5 panic, crash_core, sys_info: Introduce crash_kexec_in_memory_sys_info
In reply to Aaron TomlinWhen investigating kernel panics, capturing post-mortem diagnostic telemetry (e.g. memory zone metrics, lock states, active timers, and blocked tasks) is vital for root-cause analysis.
While crash_kexec_post_notifiers allows executing panic notifiers and sys_info() before jumping to the kdump kernel, it is frequently avoided in production environments due to the risk of hardware watchdog timeouts induced by synchronous console emission or fragile third-party panic notifier callbacks. Furthermore, if dump target storage is exhausted and saving the full vmcore fails, utilities such as makedumpfile still extract vmcore-dmesg.txt first, making the in-memory printk ring buffer the sole surviving diagnostic artefact.
Introduce the crash_kexec_in_memory_sys_info boot parameter:
1. In-memory pre-kdump collection in __crash_kexec()
When crash_kexec_in_memory_sys_info is enabled and a crash image is loaded, __crash_kexec() invokes panic_in_memory_sys_info() after saving crash registers and vmcoreinfo and prior to machine_crash_shutdown(). This captures sys_info() directly into the printk ring buffer in RAM with local interrupts disabled, console flushing suppressed, and the ring buffer tail pinned at the start of the crash output (recorded in bust_spinlocks(), __report_bug(), oops_enter(), warning paths, and vpanic(), and cleared on non-fatal warning completion or task exit in make_task_dead()). Because some architectures enter make_task_dead() with unbalanced bust_spinlocks(1) calls while others balance them in die(), track per-task nesting in task_struct->in_oops (using a u8 packed into an existing alignment hole, as normal traps and page faults nest at most twice and even recursive faults inside die() only reach single digits before recursion guards or stack exhaustion halt the CPU) so make_task_dead() unwinds only the dying task's unclosed increments. Similarly, track __warn_printk() nesting and pin ownership in task_struct->in_warn_printk and task_struct->in_warn_pinned across the __WARN_FLAGS() trap. Suppression and tail freezing remain active across machine_crash_shutdown() and machine_kexec(), only being released if machine_kexec() fails and returns.
2. Execution sequence reordering and defaults
Reorder __sys_info() so compact, high-signal subsystems (memory, locks, timers, and blocked tasks) are collected first, leaving high-volume dumps (all-CPU backtraces, full task lists, and ftrace) for last. Default to SYS_INFO_IN_MEMORY_DEFAULT when neither panic_print nor kernel.kernel_sys_info is configured, and avoid duplicate sys_info() execution if machine_kexec() fails and returns to vpanic().
--- .../admin-guide/kernel-parameters.txt | 11 ++++ include/linux/panic.h | 1 + include/linux/sched.h | 3 + include/linux/sys_info.h | 3 + kernel/crash_core.c | 6 ++ kernel/exit.c | 10 ++++ kernel/fork.c | 3 + kernel/panic.c | 56 ++++++++++++++++++- lib/bug.c | 18 +++++- lib/bust_spinlocks.c | 22 +++++++- lib/sys_info.c | 20 +++---- 11 files changed, 139 insertions(+), 14 deletions(-)
Documentation/admin-guide/kernel-parameters.txt +11 -0
@@ -1041,6 +1041,17 @@ Kernel parameters Default is enabled if CONFIG_HOTPLUG_PARALLEL=y. Otherwise the parameter has no effect. + crash_kexec_in_memory_sys_info + [KNL] Collect diagnostic system telemetry (memory, + locks, timers, blocked tasks) into the + printk ring buffer entirely in RAM before jumping to + the kdump kernel. Unlike crash_kexec_post_notifiers, + this option bypasses slow hardware console emission and + avoids running external panic notifiers. The printk ring + buffer tail is pinned at the start of the initial Oops + or panic message during collection to prevent telemetry + output from overwriting the primary crash context. + crash_kexec_post_notifiers Only jump to kdump kernel after running the panic notifiers and dumping kmsg. This option increases
include/linux/panic.h +1 -0
@@ -58,6 +58,7 @@ bool panic_in_progress(void);bool panic_on_this_cpu (void );bool panic_on_other_cpu (void );void panic_trigger_all_cpu_backtrace (void ); +void panic_in_memory_sys_info (void );/* * Only to be used by arch init code. If the user over-wrote the default
include/linux/sched.h +3 -0
@@ -1071,6 +1071,9 @@ struct task_struct { unsigned in_thrashing:1;#endif unsigned in_nf_duplicate:1; + unsigned in_warn_pinned:1; +u8 in_oops; +u8 in_warn_printk;#ifdef CONFIG_PREEMPT_RT struct netdev_xmit net_xmit;#endif
include/linux/sys_info.h +3 -0
@@ -17,6 +17,9 @@#define SYS_INFO_ALL_BT 0x00000040#define SYS_INFO_BLOCKED_TASKS 0x00000080 +#define SYS_INFO_IN_MEMORY_DEFAULT (SYS_INFO_MEM | SYS_INFO_LOCKS | \ + SYS_INFO_TIMERS | SYS_INFO_BLOCKED_TASKS) +void sys_info (unsigned long si_mask);unsigned long sys_info_get_mask (unsigned long si_mask);unsigned long sys_info_parse_param (char *str);
kernel/crash_core.c +6 -0
@@ -140,9 +140,15 @@ void __noclone __crash_kexec(struct pt_regs *regs)crash_setup_regs (&fixed_regs, regs);crash_save_vmcoreinfo (); +if (!crash_kexec_post_notifiers) +panic_in_memory_sys_info ();machine_crash_shutdown (&fixed_regs);crash_cma_clear_pending_dma ();machine_kexec (kexec_crash_image); +if (!crash_kexec_post_notifiers) { +printk_freeze_tail (false); +printk_suppress_console_flush (false); + } }kexec_unlock (); }
kernel/exit.c +10 -0
@@ -71,6 +71,8 @@#include <linux/unwind_deferred.h> #include <linux/uaccess.h> #include <linux/pidfs.h> +#include <linux/panic.h> +#include <linux/printk.h> #include <uapi/linux/wait.h>
@@ -1069,6 +1071,14 @@ void __noreturn make_task_dead(int signr)if (atomic_inc_return (&oops_count) >= limit && limit)panic ("Oopsed too often (kernel.oops_limit is %d)" , limit); +while (tsk->in_oops) +bust_spinlocks (0 ); + +if (!READ_ONCE (oops_in_progress) && !panic_in_progress () && + ((tsk->flags &PF_EXITING ) || !is_global_init (tsk) || +atomic_read (&tsk->signal->live) >1 )) +printk_clear_panic_start (); +/* * We're taking recursive faults here in make_task_dead. Safest is to just * leave this task alone and wait for reboot.
kernel/fork.c +3 -0
@@ -1009,6 +1009,9 @@ static struct task_struct *dup_task_struct(struct task_struct *orig, int node)#ifdef CONFIG_X86_BUS_LOCK_DETECT tsk->reported_split_lock =0 ;#endif + tsk->in_warn_pinned =0 ; + tsk->in_oops =0 ; + tsk->in_warn_printk =0 ;#ifdef CONFIG_SCHED_MM_CID tsk->mm_cid.cid =MM_CID_UNSET ;
kernel/panic.c +55 -1
143 lines not on this page · raw message
lib/bug.c +17 -1
@@ -197,6 +197,7 @@ void __warn_printf(const char *fmt, struct pt_regs *regs)static enum bug_trap_type __report_bug (struct bug_entry *bug,unsigned long bugaddr,struct pt_regs *regs) { +bool clear_panic_start = false;bool warning, once, done, no_cut, has_args;const char *file, *fmt;unsigned int line;
@@ -219,12 +220,22 @@ static enum bug_trap_type __report_bug(struct bug_entry *bug, unsigned long buga no_cut = bug->flags &BUGFLAG_NO_CUT_HERE ; has_args = bug->flags &BUGFLAG_ARGS ; +if (warning && no_cut && current->in_warn_printk && + --current->in_warn_printk ==0 ) { + clear_panic_start = current->in_warn_pinned; + current->in_warn_pinned =0 ; + } +/* * Before the once logic so suppressed warnings do not consume * the single-fire budget of WARN_ON_ONCE(). */ -if (warning &&kunit_is_suppressed_warning (true)) +if (warning &&kunit_is_suppressed_warning (true)) { +if (clear_panic_start && !READ_ONCE (oops_in_progress) && + !panic_in_progress ()) +printk_clear_panic_start ();return BUG_TRAP_TYPE_WARN ; + }disable_trace_on_warning ();
@@ -245,6 +256,8 @@ static enum bug_trap_type __report_bug(struct bug_entry *bug, unsigned long buga* extra debugging message it writes before triggering the handler. */ if (!no_cut) { +if (!READ_ONCE (oops_in_progress) && !panic_in_progress ()) + clear_panic_start =printk_mark_panic_start ();pr_info (CUT_HERE );__warn_printf (fmt, has_args ? regs :NULL ); }
@@ -253,6 +266,9 @@ static enum bug_trap_type __report_bug(struct bug_entry *bug, unsigned long buga/* this is a WARN_ON rather than BUG/BUG_ON */ __warn (file, line, (void *)bugaddr,BUG_GET_TAINT (bug), regs,NULL ); +if (clear_panic_start && !READ_ONCE (oops_in_progress) && + !panic_in_progress ()) +printk_clear_panic_start ();return BUG_TRAP_TYPE_WARN ; }
lib/bust_spinlocks.c +20 -2
@@ -9,8 +9,12 @@* and panic() information from reaching the user. */ +#include <linux/atomic.h> #include <linux/kernel.h> +#include <linux/panic.h> +#include <linux/preempt.h> #include <linux/printk.h> +#include <linux/sched.h> #include <linux/spinlock.h> #include <linux/tty.h> #include <linux/wait.h>
@@ -19,11 +23,25 @@void bust_spinlocks (int yes) { +int old =READ_ONCE (oops_in_progress); +if (yes) { - ++oops_in_progress; +if (in_task ()) + current->in_oops++; +do { + }while (!try_cmpxchg (&oops_in_progress, &old, old +1 )); +if (!old && !panic_in_progress ()) +printk_mark_panic_start (); }else { +if (in_task () && current->in_oops) + current->in_oops--;console_unblank (); -if (--oops_in_progress ==0 ) + old =READ_ONCE (oops_in_progress); +do { +if (unlikely (old <=0 )) +return ; + }while (!try_cmpxchg (&oops_in_progress, &old, old -1 )); +if (old ==1 )wake_up_klogd (); } }
lib/sys_info.c +10 -10
@@ -139,20 +139,17 @@ subsys_initcall(sys_info_sysctl_init);static void __sys_info (unsigned long si_mask) { -if (si_mask &SYS_INFO_TASKS ) -show_state (); -if (si_mask &SYS_INFO_MEM )show_mem (); -if (si_mask &SYS_INFO_TIMERS ) -sysrq_timer_list_show (); -if (si_mask &SYS_INFO_LOCKS )debug_show_all_locks (); -if (si_mask &SYS_INFO_FTRACE ) -ftrace_dump (DUMP_ALL ); +if (si_mask &SYS_INFO_TIMERS ) +sysrq_timer_list_show (); + +if (si_mask &SYS_INFO_BLOCKED_TASKS ) +show_state_filter (TASK_UNINTERRUPTIBLE );if (si_mask &SYS_INFO_ALL_BT ) {if (panic_on_this_cpu ())
@@ -161,8 +158,11 @@ static void __sys_info(unsigned long si_mask)trigger_all_cpu_backtrace (); } -if (si_mask &SYS_INFO_BLOCKED_TASKS ) -show_state_filter (TASK_UNINTERRUPTIBLE ); +if (si_mask &SYS_INFO_TASKS ) +show_state (); + +if (si_mask &SYS_INFO_FTRACE ) +ftrace_dump (DUMP_ALL ); }unsigned long sys_info_get_mask (unsigned long si_mask)
Signature · 1 line
2.55.0
Steven Rostedt
maintainerRe: [RFC PATCH v2 4/5] printk, tracing: Support console flush suppression and tail pinning
In reply to Aaron TomlinQuoted 15 lines · On Mon, 2 Nov 2026 21:44:52 -0500 Aaron Tomlin <[email protected]> wrote:
kernel/trace/trace.c +3
-0@@ -9519,6 +9519,9 @@ static void ftrace_dump_one(struct trace_array *tr, enum ftrace_dump_mode dump_melse iter.cpu_file =RING_BUFFER_ALL_CPUS ; +if (printk_is_tail_full ()) + goto out_enable; +if (tr == &global_trace)printk (KERN_TRACE "Dumping ftrace buffer:\n" );else
kernel/trace/trace.c static void ftrace_dump_one(struct trace_array *tr, enum ftrace_dump_mode dump_m · line 9543
Quoted 5 lines
kernel/trace/trace.c +1
-1@@ -9537,10 +9540,13 @@ static void ftrace_dump_one(struct trace_array *tr, enum ftrace_dump_mode dump_m* and then release the locks again. */ -while (!trace_empty (&iter)) { +while (!trace_empty (&iter) && !printk_is_tail_full ()) {
Wait? Is this to prevent overwriting the printk ring buffer? If so, this is wrong here!
In your change log, you mention not losing the initial oops or registers. But if someone is using ftrace_dump_on_oops, that's not what they are after. They want to know what happened that lead to the crash. That output is at the *end* of the trace.
By stopping the output of the ftrace buffer early, you just lost the information you are looking for.
-- Steve
Quoted 38 lines
kernel/trace/trace.c +12
-3-if (!cnt) +if (!cnt) {printk (KERN_TRACE "---------------------------------\n" ); +if (printk_is_tail_full ()) +break ; + } cnt++;@@ -9551,19 +9557,25 @@ static void ftrace_dump_one(struct trace_array *tr, enum ftrace_dump_mode dump_mint ret; ret =print_trace_line (&iter); +trace_printk_seq (&iter.seq); +if (printk_is_tail_full ()) +break ; +if (ret !=TRACE_TYPE_NO_CONSUME )trace_consume (&iter); - -trace_printk_seq (&iter.seq); }touch_nmi_watchdog (); } +if (printk_is_tail_full ()) + goto out_enable; +if (!cnt)printk (KERN_TRACE " (ftrace buffer empty)\n" );else printk (KERN_TRACE "---------------------------------\n" ); +out_enable: tr->trace_flags |= old_userobj;tracer_tracing_enable (tr);
Re: [RFC PATCH v2 4/5] printk, tracing: Support console flush suppression and tail pinning
Quoted 21 lines · Steven Rostedt
Quoted 18 lines · On Mon, 2 Nov 2026 21:44:52 -0500 Aaron Tomlin <[email protected]> wrote:
kernel/trace/trace.c +3
-0@@ -9519,6 +9519,9 @@ static void ftrace_dump_one(struct trace_array *tr, enum ftrace_dump_mode dump_melse iter.cpu_file =RING_BUFFER_ALL_CPUS ; +if (printk_is_tail_full ()) + goto out_enable; +if (tr == &global_trace)printk (KERN_TRACE "Dumping ftrace buffer:\n" );else @@ -9537,10 +9540,13 @@ static void ftrace_dump_one(struct trace_array *tr, enum ftrace_dump_mode dump_m* and then release the locks again. */
Quoted 5 lines
kernel/trace/trace.c static void ftrace_dump_one(struct trace_array *tr, enum ftrace_dump_mode dump_m · line 9543
Quoted 2 lines
kernel/trace/trace.c +1
-1-while (!trace_empty (&iter)) { +while (!trace_empty (&iter) && !printk_is_tail_full ()) {Wait? Is this to prevent overwriting the printk ring buffer? If so, this is wrong here!
Oops!
Quoted 7 lines
In your change log, you mention not losing the initial oops or registers. But if someone is using
ftrace_dump_on_oops, that's not what they are after. They want to know what happened that lead to the crash. That output is at the *end* of the trace.By stopping the output of the ftrace buffer early, you just lost the information you are looking for.
Agreed. Because ftrace_dump_one() iterates from oldest to newest, stopping once the pinned printk ring buffer fills up retains only the oldest trace entries and discards the end of the trace leading up to the crash.
If a user explicitly enables ftrace via panic_sys_info=ftrace alongside crash_kexec_in_memory_sys_info, freezing the printk ring buffer tail defeats the purpose of dumping the trace buffer for the exact reason you described.
I will drop the kernel/trace/trace.c changes altogether and skip printk_freeze_tail(true) in panic_in_memory_sys_info() when SYS_INFO_FTRACE is requested. That keeps console flush suppression active so the dump still completes at RAM speed prior to kexec, while allowing the printk ring buffer to wrap so the end of the trace is preserved.
Thank you,
Signature · 1 line
Aaron Tomlin
[email protected]
AI reviewRe: [RFC PATCH v2 2/5] mm/show_mem: Avoid blocking on zone->lock during Oops or panic
In reply to Aaron TomlinThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] mm/show_mem: false lock contention warning in show_free_areas() --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] mm/show_mem: false lock contention warning in show_free_areas() --
commit e1c6e11316c0b99b2f700b8251c5ed2b76aad84a
Author: Aaron Tomlin <[email protected]>
mm/show_mem: Avoid blocking on zone->lock during Oops or panic
This commit updates show_free_areas() in mm/show_mem.c to avoid unconditionally calling spin_lock_irqsave() on zone->lock during Oops, panic, or NMI/atomic contexts, skipping zones when acquiring the lock is unsafe or contended.
Quoted 12 lines
mm/show_mem.c +0
-3@@ -367,10 +367,17 @@ static void show_free_areas(unsigned int filter, const nodemask_t *nodemask,continue ;if (show_mem_node_skip (filter,zone_to_nid (zone), nodemask))continue ; -show_node (zone); -printk (KERN_CONT "%s: " , zone->name); -spin_lock_irqsave (&zone->lock, flags);
mm/show_mem.c static void show_free_areas(unsigned int filter, const nodemask_t *nodemask, · line 380
Quoted 10 lines
mm/show_mem.c +10
-0+if (!can_spin_trylock () ||in_nmi () || + oops_in_progress ||panic_in_progress ()) { +if (!can_spin_trylock () || + !spin_trylock_irqsave (&zone->lock, flags)) { +pr_warn ("zone %s lock contended\n" , zone->name); +continue ; + } + }else { +spin_lock_irqsave (&zone->lock, flags); + }
[Severity: Medium]
In show_free_areas(), when can_spin_trylock() returns false (such as in NMI or hardirq context under CONFIG_PREEMPT_RT, or in NMI context under !CONFIG_SMP):
mm/internal.h:can_spin_trylock() {
if (IS_ENABLED(CONFIG_PREEMPT_RT) && (in_nmi() || in_hardirq()))
return false;
/* On UP, spin_trylock() always succeeds even when it is locked */
if (!IS_ENABLED(CONFIG_SMP) && in_nmi())
return false;
return true;
}the inner !can_spin_trylock() check short-circuits before spin_trylock_irqsave() is ever called, yet still prints "zone %s lock contended" for every populated zone even when zone->lock is completely uncontended.
The commit message also states:
Check can_spin_trylock() and use spin_trylock_irqsave() when !can_spin_trylock(), in_nmi(), oops_in_progress, or panic_in_progress() is active
Additionally, because show_node(zone) is moved after spin_unlock_irqrestore() below, skipping a zone here only logs zone->name (for example, Normal or DMA32) without the NUMA node ID on CONFIG_NUMA systems.
Should the !can_spin_trylock() case avoid claiming that zone->lock is contended (and match the commit message description), and should the skipped zone message include the node ID?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
[email protected]
AI reviewRe: [RFC PATCH v2 1/5] panic, sys_info: Fix duplicate and dropped all-CPU backtraces in panic
In reply to Aaron TomlinSashiko has reviewed this patch and found no issues.
Quoted 19 lines
When
sys_info()was factored out ofkernel/panic.cby commitb76e89e50fc3("panic: generalize panic_print's function to show sys info"),__sys_info()gained an unconditional call totrigger_all_cpu_backtrace()whenSYS_INFO_ALL_BTis set, whilepanic_other_cpus_shutdown()retained its own call topanic_trigger_all_cpu_backtrace().This causes two problems during a kernel panic:
1.
vpanic()invokespanic_trigger_all_cpu_backtrace()insidepanic_other_cpus_shutdown()before stopping secondary CPUs, and then invokestrigger_all_cpu_backtrace()a second time insidesys_info(panic_print)after secondary CPUs have already been shut down, stalling for up to 10 seconds waiting for offline CPUs to respond to NMIs. Furthermore, ifSYS_INFO_ALL_BTis
[ ... ]panic_try_start(), and routeSYS_INFO_ALL_BTin__sys_info()throughpanic_trigger_all_cpu_backtrace()on the panicking CPU.
Quoted 2 lines
Fixes b76e89e50fc3 ("panic: generalize panic_print's function to show sys info")Signed-off-by Aaron Tomlin <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
[email protected]
AI reviewRe: [RFC PATCH v2 3/5] timers: Avoid blocking on hrtimer_cpu_base lock during Oops or panic
In reply to Aaron TomlinThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] timers: incomplete !CONFIG_SMP guard in print_active_timers() --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] timers: incomplete !CONFIG_SMP guard in print_active_timers()
--
commit 8e788faa0cf2d16ee117d7196f09432346eeda90
Author: Aaron Tomlin <[email protected]>
timers: Avoid blocking on hrtimer_cpu_base lock during Oops or panic
This commit switches sysrq_timer_list_show() to use ktime_get_mono_fast_ns() and updates print_active_timers() to use raw_spin_trylock_irqsave() on base->cpu_base->lock when in NMI, Oops, or panic contexts so that dumping timer state does not deadlock if a CPU is holding a timekeeping or hrtimer base lock.
Quoted 8 lines
kernel/time/timer_list.c +0
-0@@ -68,7 +68,15 @@ static void print_active_timers(struct seq_file *m, struct hrtimer_clock_base *btouch_nmi_watchdog ();
kernel/time/timer_list.c static void print_active_timers(struct seq_file *m, struct hrtimer_clock_base *b · line 79
Quoted 10 lines
kernel/time/timer_list.c +9
-1-raw_spin_lock_irqsave (&base->cpu_base->lock, flags); +if (in_nmi () || oops_in_progress ||panic_in_progress ()) { +if ((!IS_ENABLED (CONFIG_SMP ) &&in_nmi ()) || + !raw_spin_trylock_irqsave (&base->cpu_base->lock, flags)) { +SEQ_printf (m," [cpu_base lock contended]\n" ); +return ; + } + }else { +raw_spin_lock_irqsave (&base->cpu_base->lock, flags); + }
[Severity: High]
In print_active_timers(), the outer check enters this branch when in_nmi(), oops_in_progress, or panic_in_progress() is true, but the inner !CONFIG_SMP check only tests in_nmi(). What happens on a !CONFIG_SMP build if an Oops or panic occurs in non-NMI context while the CPU already holds base->cpu_base->lock (for example, inside enqueue_hrtimer() or __remove_hrtimer())?
When CONFIG_SMP=n and CONFIG_DEBUG_SPINLOCK=n, _raw_spin_trylock_irqsave() unconditionally returns 1:
include/linux/spinlock_api_up.h:_raw_spin_trylock_irqsave() {
__LOCK_IRQSAVE(lock, *(flags));
return 1;
}Wouldn't that allow print_active_timers() to re-enter the critical section and walk base->active while the timerqueue is in a partially modified state?
And when CONFIG_SMP=n and CONFIG_DEBUG_SPINLOCK=y, since vpanic() invokes sys_info() before debug_locks_off():
kernel/panic.c:vpanic() {
...
sys_info(panic_print);
kmsg_dump_desc(KMSG_DUMP_PANIC, buf);
if (_crash_kexec_post_notifiers)
__crash_kexec(NULL);
console_unblank();
debug_locks_off();
...
}wouldn't do_raw_spin_trylock() hit SPIN_BUG_ON(!ret, lock, "trylock failure on UP") when base->cpu_base->lock is already held?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
[email protected]
AI reviewRe: [RFC PATCH v2 4/5] printk, tracing: Support console flush suppression and tail pinning
In reply to Aaron TomlinSashiko has reviewed this patch and found no issues.
Quoted 20 lines
When collecting high-volume diagnostic output into the printk ring buffer during a kernel crash prior to kexec, synchronous console emission can stall the panicking CPU for tens of seconds on slow serial or IPMI Serial-over-LAN links, risking external hardware watchdog resets. At the same time, uninhibited ring buffer rotation can wrap and overwrite the initial Oops, faulting registers, and primary call trace.
Introduce two scoped mechanisms for in-memory crash telemetry collection:
1. Console flush suppression
Provide
printk_suppress_console_flush(bool)to clear the console flush mask inprintk_get_console_flush_type(), guard legacy record emission and kthread wakeups, and suppress
[ ... ]
record and stop reserving descriptors or consuming trace entries so the primary crash context and remaining trace buffer remain intact.
Quoted 1 line
Signed-off-by Aaron Tomlin <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [Medium] crash_core: unconditional printk state reset in __crash_kexec() on failure - [Medium] panic…
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [Medium] crash_core: unconditional printk state reset in __crash_kexec() on failure
- [Medium] panic: permanent printk_panic_seq pin when die() or oops_end() returns
- [Medium] panic: race in printk_clear_panic_start() unpins active Oops or warning --
commit be914e098a723f2f6a11c63bee2fe8d772bb6b45
Author: Aaron Tomlin <[email protected]>
panic, crash_core, sys_info: Introduce crash_kexec_in_memory_sys_info
This commit introduces the crash_kexec_in_memory_sys_info boot parameter to collect sys_info() telemetry into the in-memory printk ring buffer before jumping to the kdump kernel, while suppressing console flushing and pinning the ring buffer tail at the start of the initial crash message.
Quoted 7 lines
kernel/crash_core.c +0
-0@@ -140,9 +140,15 @@ void __noclone __crash_kexec(struct pt_regs *regs)crash_setup_regs (&fixed_regs, regs);
kernel/crash_core.c void __noclone __crash_kexec(struct pt_regs *regs) · line 151
Quoted 10 lines
kernel/crash_core.c +6
-0crash_save_vmcoreinfo (); +if (!crash_kexec_post_notifiers) +panic_in_memory_sys_info ();machine_crash_shutdown (&fixed_regs);crash_cma_clear_pending_dma ();machine_kexec (kexec_crash_image); +if (!crash_kexec_post_notifiers) { +printk_freeze_tail (false); +printk_suppress_console_flush (false); + }
[Severity: Medium]
In __crash_kexec(), panic_in_memory_sys_info() only calls printk_suppress_console_flush(true) and printk_freeze_tail(true) when crash_kexec_in_memory_sys_info is enabled:
kernel/panic.c:panic_in_memory_sys_info()
if (!crash_kexec_in_memory_sys_info || panic_sys_info_dumped)
return;
...
printk_suppress_console_flush(true);
printk_freeze_tail(true);However, if machine_kexec(kexec_crash_image) fails and returns while crash_kexec_in_memory_sys_info is disabled (the default), __crash_kexec() still unconditionally runs printk_freeze_tail(false) and printk_suppress_console_flush(false).
Calling printk_freeze_tail(false) resets printk_panic_seq to U64_MAX, discarding the initial Oops start sequence recorded by oops_enter() or bust_spinlocks(1), and calling printk_suppress_console_flush(false) invokes printk_trigger_flush() and wake_up_klogd() (queuing irq_work after machine_crash_shutdown() has already disabled interrupts and APICs) even though console flushing was never suppressed.
Should the cleanup after machine_kexec() only run when panic_in_memory_sys_info() actually enabled tail freezing and console flush suppression?
Quoted 4 lines
kernel/exit.c +0
-0
[ ... ]
kernel/exit.c void __noreturn make_task_dead(int signr) · line 1086
Quoted 11 lines
kernel/exit.c +7
-0@@ -1075,6 +1077,14 @@ void __noreturn make_task_dead(int signr)if (atomic_inc_return (&oops_count) >= limit && limit)panic ("Oopsed too often (kernel.oops_limit is %d)" , limit); +while (tsk->in_oops) +bust_spinlocks (0 ); + +if (!READ_ONCE (oops_in_progress) && !panic_in_progress () && + ((tsk->flags &PF_EXITING ) || !is_global_init (tsk) || +atomic_read (&tsk->signal->live) >1 )) +printk_clear_panic_start ();
[Severity: Medium]
On entry to an Oops, oops_enter() in kernel/panic.c and bust_spinlocks(1) in lib/bust_spinlocks.c invoke printk_mark_panic_start(), which pins printk_panic_seq. Neither bust_spinlocks(0) nor oops_exit() clears printk_panic_seq; for Oops paths, printk_clear_panic_start() is only called here in make_task_dead().
Across several architectures, when notify_die(DIE_OOPS, ...) returns NOTIFY_STOP (for example via a registered die notifier such as KGDB/KDB or kprobes), the trap handler finishes bust_spinlocks(0) and oops_exit() and returns without calling make_task_dead() or panic():
arch/x86/kernel/dumpstack.c:oops_end()
bust_spinlocks(0);
...
oops_exit();
...
if (!signr)
return;arch/arm64/kernel/traps.c:die()
bust_spinlocks(0);
oops_exit();
...
if (ret != NOTIFY_STOP)
make_task_dead(SIGSEGV);(And similarly in arch/arm/kernel/traps.c:oops_end(), arch/riscv/kernel/traps.c:die(), and arch/powerpc/kernel/traps.c:oops_end(), while arch/loongarch/kernel/traps.c:die() and arch/hexagon/kernel/traps.c:die() return early on NOTIFY_STOP after bust_spinlocks(1) without calling bust_spinlocks(0), leaving current->in_oops and oops_in_progress incremented as well.)
When die() or oops_end() returns without reaching make_task_dead(), won't printk_panic_seq remain permanently pinned? Once normal printk activity wraps the ring buffer past that stale sequence number ((u64)panic_seq < first_seq), a subsequent panic will clamp printk_panic_seq to first_seq at the current ring buffer tail in printk_mark_panic_start(), leaving zero recyclable space and causing panic_in_memory_sys_info() to drop all sys_info() output.
Quoted 4 lines
kernel/panic.c +0
-0
[ ... ]
Quoted 22 lines
kernel/panic.c +7
-0@@ -1067,6 +1096,8 @@ void oops_enter(void)/* can't trust the integrity of the kernel anymore: */ debug_locks_off ();do_oops_enter_exit (); +if (!panic_in_progress ()) +printk_mark_panic_start ();if (sysctl_oops_all_cpu_backtrace)trigger_all_cpu_backtrace ();@@ -1097,10 +1128,16 @@ struct warn_args {void __warn (const char *file,int line,void *caller,unsigned taint,struct pt_regs *regs,struct warn_args *args) { +bool clear_panic_start = false; +nbcon_cpu_emergency_enter ();disable_trace_on_warning (); +if (!READ_ONCE (oops_in_progress) && !panic_in_progress () && +printk_mark_panic_start ()) + clear_panic_start = true;
[ ... ]
kernel/panic.c void __warn(const char *file, int line, void *caller, unsigned taint, · line 1178
Quoted 6 lines
kernel/panic.c +2
-0@@ -1137,6 +1174,9 @@ void __warn(const char *file, int line, void *caller, unsigned taint,/* Just a warning, don't kill lockdep. */ add_taint (taint,LOCKDEP_STILL_OK ); +if (clear_panic_start && !READ_ONCE (oops_in_progress) && !panic_in_progress ()) +printk_clear_panic_start ();
[Severity: Medium]
Can a finishing non-fatal warning in __warn(), warn_slowpath_fmt(), or __report_bug() (or a task exiting in make_task_dead()) on one CPU race with a fatal Oops or fatal warning on another CPU and unpin printk_panic_seq?
Specifically, oops_in_progress is 0 and panic_in_progress() is false on CPU B during three windows before vpanic() sets panic_cpu:
1. In oops_begin() / die(), oops_enter() calls printk_mark_panic_start() before bust_spinlocks(1) increments oops_in_progress.
2. In oops_end() / die(), bust_spinlocks(0) decrements oops_in_progress back to 0 before oops_exit() and before panic() is called for in_interrupt() or panic_on_oops:
arch/x86/kernel/dumpstack.c:oops_end()
bust_spinlocks(0);
...
oops_exit();
__show_regs(&exec_summary_regs, SHOW_REGS_ALL, KERN_DEFAULT);
if (!signr)
return;
if (in_interrupt())
panic("Fatal exception in interrupt");
if (panic_on_oops)
panic("Fatal exception");3. During a fatal warning (when panic_on_warn, warn_limit, or panic_on_taint is set), printk_mark_panic_start() is called while oops_in_progress is 0 and before check_panic_on_warn() or add_taint() enters panic().
If CPU A finishes a non-fatal warning (having set clear_panic_start = true just before CPU B started, or while CPU B is in a fatal warning) or runs make_task_dead() (which does not check whether current pinned printk_panic_seq) during any of those windows on CPU B, CPU A sees !READ_ONCE(oops_in_progress) && !panic_in_progress() and calls printk_clear_panic_start(). When CPU B then enters vpanic(), printk_mark_panic_start() re-pins printk_panic_seq at the panic banner after CPU B's Oops or warning output has already been emitted, allowing panic_in_memory_sys_info() to overwrite the primary crash context.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
Re: [RFC PATCH v2 2/5] mm/show_mem: Avoid blocking on zone->lock during Oops or panic
In reply to [email protected]Quoted 60 lines · On Wed, Nov 04, 2026 at 02:49:19AM +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium]mm/show_mem: false lock contention warning inshow_free_areas()--commit
e1c6e11316c0b99b2f700b8251c5ed2b76aad84a
Author: Aaron Tomlin <[email protected]>
mm/show_mem: Avoid blocking onzone->lockduring Oops or panicThis commit updates
show_free_areas()inmm/show_mem.cto avoid unconditionally callingspin_lock_irqsave()onzone->lockduring Oops, panic, or NMI/atomic contexts, skipping zones when acquiring the lock is unsafe or contended.Quoted 12 lines
mm/show_mem.c +0
-3@@ -367,10 +367,17 @@ static void show_free_areas(unsigned int filter, const nodemask_t *nodemask,continue ;if (show_mem_node_skip (filter,zone_to_nid (zone), nodemask))continue ; -show_node (zone); -printk (KERN_CONT "%s: " , zone->name); -spin_lock_irqsave (&zone->lock, flags);mm/show_mem.c static void show_free_areas(unsigned int filter, const nodemask_t *nodemask, · line 380
Quoted 10 lines
mm/show_mem.c +10
-0+if (!can_spin_trylock () ||in_nmi () || + oops_in_progress ||panic_in_progress ()) { +if (!can_spin_trylock () || + !spin_trylock_irqsave (&zone->lock, flags)) { +pr_warn ("zone %s lock contended\n" , zone->name); +continue ; + } + }else { +spin_lock_irqsave (&zone->lock, flags); + }[Severity: Medium]
Inshow_free_areas(), whencan_spin_trylock()returns false (such as in NMI or hardirq context underCONFIG_PREEMPT_RT, or in NMI context under !CONFIG_SMP):mm/internal.h:can_spin_trylock() { if (IS_ENABLED(CONFIG_PREEMPT_RT) && (in_nmi() || in_hardirq())) return false; /* On UP, spin_trylock() always succeeds even when it is locked */ if (!IS_ENABLED(CONFIG_SMP) && in_nmi()) return false; return true; }the inner !can_spin_trylock() check short-circuits before
spin_trylock_irqsave()is ever called, yet still prints "zone %s lock contended" for every populated zone even whenzone->lockis completely uncontended.The commit message also states:
Check
can_spin_trylock()and usespin_trylock_irqsave()when !can_spin_trylock(),in_nmi(),oops_in_progress, orpanic_in_progress()is active
Quoted 10 lines
Additionally, because
show_node(zone)is moved afterspin_unlock_irqrestore()below, skipping a zone here only logszone->name(for example, Normal or DMA32) without the NUMA node ID onCONFIG_NUMAsystems.Should the !can_spin_trylock() case avoid claiming that
zone->lockis contended (and match the commit message description), and should the skipped zone message include the node ID?Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
I have updated the commit message in the next revision to accurately describe this two-stage check, and updated the pr_warn() message to include the NUMA node ID via zone_to_nid(zone).
Kind regards,
Signature · 1 line
Aaron Tomlin
Thomas Gleixner
maintainerRe: [RFC PATCH v2 3/5] timers: Avoid blocking on hrtimer_cpu_base lock during Oops or panic
In reply to Aaron TomlinQuoted 29 lines · Aaron Tomlin
When
sysrq_timer_list_show()dumps active high-resolution timers across all online CPUs, it callsktime_get()(which spins on tk_core.seq) andprint_active_timers()unconditionally acquires each CPU'sbase->cpu_base->lockviaraw_spin_lock_irqsave(). If invoked from an Oops, NMI, or panic context while the crashing CPU or a wedged remote CPU is inside a timekeeping write section or already holdsbase->cpu_base->lock,sysrq_timer_list_show()will spin indefinitely and prevent panic recovery or kexec from completing.Use the NMI-safe
ktime_get_mono_fast_ns()helper insysrq_timer_list_show()andraw_spin_trylock_irqsave()inprint_active_timers()whenin_nmi(),oops_in_progress, orpanic_in_progress()is active, noting any contended lock and returning cleanly instead of deadlocking.Fixes d999bd9392de ("panic: add options to print system info when panic happens")Signed-off-by Aaron Tomlin <[email protected]>--- kernel/time/timer_list.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-)kernel/time/timer_list.c +0
-0@@ -68,7 +68,15 @@ static void print_active_timers(struct seq_file *m, struct hrtimer_clock_base *btouch_nmi_watchdog ();
kernel/time/timer_list.c static void print_active_timers(struct seq_file *m, struct hrtimer_clock_base *b · line 79
Quoted 10 lines
kernel/time/timer_list.c +9
-1-raw_spin_lock_irqsave (&base->cpu_base->lock, flags); +if (in_nmi () || oops_in_progress ||panic_in_progress ()) { +if ((!IS_ENABLED (CONFIG_SMP ) &&in_nmi ()) || + !raw_spin_trylock_irqsave (&base->cpu_base->lock, flags)) { +SEQ_printf (m," [cpu_base lock contended]\n" ); +return ; + } + }else { +raw_spin_lock_irqsave (&base->cpu_base->lock, flags); + }
TBH, that's horrible and unreadable gunk.
Clearly the approach of all of this is:
1) Pick some existing code
2) Invoke it
3) Hope that it works
4) If not add bandaids to make it "work"
Seriously?
Each of the callers of sys_info() knows the exact context in which it is invoked, right?
So the obvious and clean solution is to hand exactly that context information into sys_info() and further down into the invoked functions so they can make informed decisions and eventually omit pointless information which is otherwise emitted in proc/sysrq-t.
Something like the uncompiled mockup below should give you an idea.
kernel/time/timer_list.c static inline void timer_list_header(struct seq_file *m, ktime_t now) · line 274
Quoted 9 lines
kernel/time/timer_list.c +2
-1curr =timerqueue_linked_first (&base->active);/* @@ -262,7 +270,8 @@ static inline void timer_list_header(struct seq_file *m, ktime_t now)void sysrq_timer_list_show (void ) { -ktime_t now =ktime_get (); +ktime_t now = (in_nmi () || oops_in_progress ||panic_in_progress ()) ? +ns_to_ktime (ktime_get_mono_fast_ns ()) :ktime_get ();
That's a pointless exercise. Just use ktime_get_mono_fast_ns() unconditionally. It's perfectly fine for the purpose of this in all contexts.
Thanks,
tglx
---
kernel/time/timer_list.c +127 -100
@@ -17,10 +17,16 @@#include "tick-internal.h" +struct tlctx { +struct seq_file *seq_file; +ktime_t ts_now; +enum sysinfo_context si_ctx; +}; +struct timer_list_iter { -int cpu; -bool second_pass; -ktime_t now; +int cpu; +bool second_pass; +ktime_t now; };/*
@@ -28,35 +34,51 @@ struct timer_list_iter {* to the console (on SysRq-Q): */ __printf(2, 3) -static void SEQ_printf (struct seq_file *m,const char *fmt, ...) +static void SEQ_printf (struct tlctx *ctx,const char *fmt, ...) {va_list args;va_start (args, fmt); -if (m) -seq_vprintf (m, fmt, args); +if (ctx->seq_file) +seq_vprintf (ctx->seq_file, fmt, args);else vprintk (fmt, args);va_end (args); } -static void -print_timer (struct seq_file *m,struct hrtimer *taddr,struct hrtimer *timer, -int idx,ktime_t now) -{ -SEQ_printf (m," #%d: <%p>, %ps" , idx, taddr,ACCESS_PRIVATE (timer, function)); -SEQ_printf (m,", S:%02x" , timer->is_queued); -SEQ_printf (m,"\n" ); -SEQ_printf (m," # expires at %lld-%lld nsecs [in %lld to %lld nsecs]\n" , - (long long )hrtimer_get_softexpires (timer), - (long long )hrtimer_get_expires (timer), - (long long )ktime_sub (hrtimer_get_softexpires (timer), now), - (long long )ktime_sub (hrtimer_get_expires (timer), now)); +static void print_timer (struct tlctx *ctx,struct hrtimer *taddr,struct hrtimer *timer, +int idx,ktime_t now) +{ +SEQ_printf (ctx," #%d: <%p>, %ps" , idx, taddr,ACCESS_PRIVATE (timer, function)); +SEQ_printf (ctx,", S:%02x" , timer->is_queued); +SEQ_printf (ctx,"\n" ); +SEQ_printf (ctx," # expires at %lld-%lld nsecs [in %lld to %lld nsecs]\n" , + (long long )hrtimer_get_softexpires (timer), + (long long )hrtimer_get_expires (timer), + (long long )ktime_sub (hrtimer_get_softexpires (timer), now), + (long long )ktime_sub (hrtimer_get_expires (timer), now)); +} + +static bool lock_hrtimer_base (struct tlctx *ctx,struct hrtimer_clock_base *base, +unsigned long *flags) +{ +switch (ctx->si_ctx) { +case SYSINFO_CTX_LOCK_SAFE : +raw_spin_lock_irqsave (&base->cpu_base->lock, *flags); +return true; +case SYSINFO_CTX_TRYLOCK_SAFE : +if (raw_spin_trylock_irqsave (&base->cpu_base->lock, *flags)) +return true; + fallthrough; +default : +SEQ_printf (ctx," Unsafe context: Skip timers.\n" ); +return false; + } } -static void print_active_timers (struct seq_file *m,struct hrtimer_clock_base *base,ktime_t now) +static void print_active_timers (struct tlctx *ctx,struct hrtimer_clock_base *base,ktime_t now) {struct timerqueue_linked_node *curr;struct hrtimer *timer, tmp;
@@ -64,13 +86,14 @@ static void print_active_timers(struct sunsigned long flags; next_one: -if (m &&seq_has_overflowed (m)) +if (ctx->seq_file &&seq_has_overflowed (ctx->seqfile))return ; i =0 ;touch_nmi_watchdog (); -raw_spin_lock_irqsave (&base->cpu_base->lock, flags); +if (!lock_hrtimer_base (ctx, base, &flags)) +return ; curr =timerqueue_linked_first (&base->active);/*
@@ -83,51 +106,48 @@ static void print_active_timers(struct s }if (curr) { - timer = container_of(curr, struct hrtimer, node); tmp = *timer;raw_spin_unlock_irqrestore (&base->cpu_base->lock, flags); -print_timer (m, timer, &tmp, i, now); +print_timer (ctx, timer, &tmp, i, now); next++; goto next_one; }raw_spin_unlock_irqrestore (&base->cpu_base->lock, flags); } -static void -print_base (struct seq_file *m,struct hrtimer_clock_base *base,ktime_t now) +static void print_base (struct tlctx *ctx,struct hrtimer_clock_base *base) { -SEQ_printf (m," .base: %p\n" , base); -SEQ_printf (m," .index: %d\n" , base->index); +SEQ_printf (ctx," .base: %p\n" , base); +SEQ_printf (ctx," .index: %d\n" , base->index); -SEQ_printf (m," .resolution: %u nsecs\n" , hrtimer_resolution); +SEQ_printf (ctx," .resolution: %u nsecs\n" , hrtimer_resolution);#ifdef CONFIG_HIGH_RES_TIMERS -SEQ_printf (m," .offset: %lld nsecs\n" , - (long long ) base->offset); +SEQ_printf (ctx," .offset: %lld nsecs\n" , (long long ) base->offset);#endif -SEQ_printf (m,"active timers:\n" ); -print_active_timers (m, base,ktime_add (now, base->offset)); +SEQ_printf (ctx,"active timers:\n" ); +print_active_timers (ctx, base,ktime_add (ctx->ts_mono, base->offset)); } -static void print_cpu (struct seq_file *m,int cpu,ktime_t now) +static void print_cpu (struct tlctx *ctx,int cpu) {struct hrtimer_cpu_base *cpu_base = &per_cpu (hrtimer_bases, cpu);int i; -SEQ_printf (m,"cpu: %d\n" , cpu); +SEQ_printf (ctx,"cpu: %d\n" , cpu);for (i =0 ; i <HRTIMER_MAX_CLOCK_BASES ; i++) { -SEQ_printf (m," clock %d:\n" , i); -print_base (m, cpu_base->clock_base + i, now); +SEQ_printf (ctx," clock %d:\n" , i); +print_base (ctx, cpu_base->clock_base + i); }#define DIAG_READ (x) data_race(READ_ONCE(x))#define P (x) \ - SEQ_printf(m, " .%-15s: %llu\n", #x, \ + SEQ_printf(ctx, " .%-15s: %llu\n", #x, \ (unsigned long long)DIAG_READ(cpu_base->x))#define P_ktime (x) \ - SEQ_printf(m, " .%-15s: %lld nsecs\n", #x, (long long)DIAG_READ(cpu_base->x)) + SEQ_printf(ctx, " .%-15s: %lld nsecs\n", #x, (long long)DIAG_READ(cpu_base->x))#ifdef CONFIG_HIGH_RES_TIMERS P_ktime (expires_next);
@@ -141,13 +161,12 @@ static void print_cpu(struct seq_file *m#undef P_ktime#ifdef CONFIG_TICK_ONESHOT -# define P (x) \ - SEQ_printf(m, " .%-15s: %llu\n", #x, \ - (unsigned long long)DIAG_READ(ts->x)) -# define P_ktime (x) \ - SEQ_printf(m, " .%-15s: %lld nsecs\n", #x, (long long)DIAG_READ(ts->x)) -# define P_flag (x, f) \ - SEQ_printf(m, " .%-15s: %d\n", #x, !!(DIAG_READ(ts->flags) & (f))) +# define P (x) \ + SEQ_printf(ctx, " .%-15s: %llu\n", #x, (unsigned long long)DIAG_READ(ts->x)) +# define P_ktime (x) \ + SEQ_printf(ctx, " .%-15s: %lld nsecs\n", #x, (long long)DIAG_READ(ts->x)) +# define P_flag (x, f) \ + SEQ_printf(ctx, " .%-15s: %d\n", #x, !!(DIAG_READ(ts->flags) & (f))) {struct tick_sched *ts =tick_get_tick_sched (cpu);
@@ -162,8 +181,7 @@ static void print_cpu(struct seq_file *mP (last_jiffies);P (next_timer);P_ktime (idle_expires); -SEQ_printf (m,"jiffies: %llu\n" , - (unsigned long long)jiffies); + SEQ_printf(ctx, "jiffies: %llu\n", (unsigned long long)jiffies); }#endif
@@ -171,129 +189,138 @@ static void print_cpu(struct seq_file *m#undef P_ktime#undef P_flag#undef DIAG_READ -SEQ_printf (m,"\n" ); +SEQ_printf (ctx,"\n" ); }#ifdef CONFIG_GENERIC_CLOCKEVENTS -static void -print_tickdevice (struct seq_file *m,struct tick_device *td,int cpu) +static void print_tickdevice (struct tlctx *ctx,struct tick_device *td,int cpu) {struct clock_event_device *dev = td->evtdev;touch_nmi_watchdog (); -SEQ_printf (m,"Tick Device: mode: %d\n" , td->mode); +SEQ_printf (ctx,"Tick Device: mode: %d\n" , td->mode);if (cpu <0 ) -SEQ_printf (m,"Broadcast device\n" ); +SEQ_printf (ctx,"Broadcast device\n" );else -SEQ_printf (m,"Per CPU device: %d\n" , cpu); +SEQ_printf (ctx,"Per CPU device: %d\n" , cpu); -SEQ_printf (m,"Clock Event Device: " ); +SEQ_printf (ctx,"Clock Event Device: " );if (!dev) { -SEQ_printf (m,"<NULL>\n" ); +SEQ_printf (ctx,"<NULL>\n" );return ; } -SEQ_printf (m,"%s\n" , dev->name); -SEQ_printf (m," max_delta_ns: %llu\n" , - (unsigned long long ) dev->max_delta_ns); -SEQ_printf (m," min_delta_ns: %llu\n" , - (unsigned long long ) dev->min_delta_ns); -SEQ_printf (m," mult: %u\n" , dev->mult); -SEQ_printf (m," shift: %u\n" , dev->shift); -SEQ_printf (m," mode: %d\n" ,clockevent_get_state (dev)); -SEQ_printf (m," next_event: %lld nsecs\n" , (long long )dev->next_event); +SEQ_printf (ctx,"%s\n" , dev->name); +SEQ_printf (ctx," max_delta_ns: %llu\n" , (unsigned long long ) dev->max_delta_ns); +SEQ_printf (ctx," min_delta_ns: %llu\n" , (unsigned long long ) dev->min_delta_ns); +SEQ_printf (ctx," mult: %u\n" , dev->mult); +SEQ_printf (ctx," shift: %u\n" , dev->shift); +SEQ_printf (ctx," mode: %d\n" ,clockevent_get_state (dev)); +SEQ_printf (ctx," next_event: %lld nsecs\n" , (long long )dev->next_event); -SEQ_printf (m," set_next_event: %ps\n" , dev->set_next_event); +SEQ_printf (ctx," set_next_event: %ps\n" , dev->set_next_event);if (dev->set_state_shutdown) -SEQ_printf (m," shutdown: %ps\n" , - dev->set_state_shutdown); +SEQ_printf (ctx," shutdown: %ps\n" , dev->set_state_shutdown);if (dev->set_state_periodic) -SEQ_printf (m," periodic: %ps\n" , - dev->set_state_periodic); +SEQ_printf (ctx," periodic: %ps\n" , dev->set_state_periodic);if (dev->set_state_oneshot) -SEQ_printf (m," oneshot: %ps\n" , - dev->set_state_oneshot); +SEQ_printf (ctx," oneshot: %ps\n" , dev->set_state_oneshot);if (dev->set_state_oneshot_stopped) -SEQ_printf (m," oneshot stopped: %ps\n" , - dev->set_state_oneshot_stopped); +SEQ_printf (ctx," oneshot stopped: %ps\n" , dev->set_state_oneshot_stopped);if (dev->tick_resume) -SEQ_printf (m," resume: %ps\n" , - dev->tick_resume); +SEQ_printf (ctx," resume: %ps\n" , dev->tick_resume); -SEQ_printf (m," event_handler: %ps\n" , dev->event_handler); -SEQ_printf (m,"\n" ); -SEQ_printf (m," retries: %lu\n" , dev->retries); +SEQ_printf (ctx," event_handler: %ps\n" , dev->event_handler); +SEQ_printf (ctx,"\n" ); +SEQ_printf (ctx," retries: %lu\n" , dev->retries);#ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST if (cpu >=0 ) {const struct clock_event_device *wd =tick_get_wakeup_device (cpu); -SEQ_printf (m,"Wakeup Device: %s\n" , wd ? wd->name :"<NULL>" ); +SEQ_printf (ctx,"Wakeup Device: %s\n" , wd ? wd->name :"<NULL>" ); }#endif -SEQ_printf (m,"\n" ); +SEQ_printf (ctx,"\n" ); }static void timer_list_show_tickdevices_header (struct seq_file *m) {#ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST -print_tickdevice (m,tick_get_broadcast_device (),-1 ); -SEQ_printf (m,"tick_broadcast_mask: %*pb\n" , +print_tickdevice (ctx,tick_get_broadcast_device (),-1 ); +SEQ_printf (ctx,"tick_broadcast_mask: %*pb\n" ,cpumask_pr_args (tick_get_broadcast_mask ()));#ifdef CONFIG_TICK_ONESHOT -SEQ_printf (m,"tick_broadcast_oneshot_mask: %*pb\n" , +SEQ_printf (ctx,"tick_broadcast_oneshot_mask: %*pb\n" ,cpumask_pr_args (tick_get_broadcast_oneshot_mask ()));#endif -SEQ_printf (m,"\n" ); +SEQ_printf (ctx,"\n" );#endif }#endif -static inline void timer_list_header (struct seq_file *m,ktime_t now) +static inline void timer_list_header (struct seq_file *ctx) { -SEQ_printf (m,"Timer List Version: v0.11\n" ); -SEQ_printf (m,"HRTIMER_MAX_CLOCK_BASES: %d\n" ,HRTIMER_MAX_CLOCK_BASES ); -SEQ_printf (m,"now at %lld nsecs\n" , (long long )now); -SEQ_printf (m,"\n" ); +SEQ_printf (ctx,"Timer List Version: v0.11\n" ); +SEQ_printf (ctx,"HRTIMER_MAX_CLOCK_BASES: %d\n" ,HRTIMER_MAX_CLOCK_BASES ); +SEQ_printf (ctx,"now at %lld nsecs\n" , (long long )ctx->ts_mono); +SEQ_printf (ctx,"\n" ); } -void sysrq_timer_list_show (void ) +static void sys_timer_list_show (struct tlctx *ctx) { -ktime_t now =ktime_get (); + ctx->ts_mono =ktime_get_ns_fast ();int cpu; -timer_list_header (NULL , now); +timer_list_header (ctx);for_each_online_cpu (cpu) -print_cpu (NULL , cpu, now); +print_cpu (ctx, cpu);#ifdef CONFIG_GENERIC_CLOCKEVENTS -timer_list_show_tickdevices_header (NULL ); +timer_list_show_tickdevices_header (ctx);for_each_online_cpu (cpu) -print_tickdevice (NULL ,tick_get_device (cpu), cpu); +print_tickdevice (ctx,tick_get_device (cpu), cpu);#endif -return ; +} + +void sysinfo_timer_list_show (enum sysinfo_context si_ctx) +{ +struct tlctx ctx = { .si_ctx = si_ctx, }; + +sys_timer_list_show (&ctx); +} + +void sysrq_timer_list_show (void ) +{ +struct tlctx ctx = {}; + +sys_timer_list_show (&ctx); }#ifdef CONFIG_PROC_FS static int timer_list_show (struct seq_file *m,void *v) {struct timer_list_iter *iter = v; +struct tlctx ctx = { + .seq_file = m, + .ts_mono = iter->now, + };if (iter->cpu ==-1 && !iter->second_pass) -timer_list_header (m, iter->now); +timer_list_header (&ctx, iter->now);else if (!iter->second_pass) -print_cpu (m, iter->cpu, iter->now); +print_cpu (&ctx, iter->cpu);#ifdef CONFIG_GENERIC_CLOCKEVENTS else if (iter->cpu == -1 && iter->second_pass) - timer_list_show_tickdevices_header(m); + timer_list_show_tickdevices_header(&ctx); else - print_tickdevice(m, tick_get_device(iter->cpu), iter->cpu); + print_tickdevice(&ctx, tick_get_device(iter->cpu), iter->cpu);#endif return 0 ; }
Re: [RFC PATCH v2 3/5] timers: Avoid blocking on hrtimer_cpu_base lock during Oops or panic
Quoted 30 lines · Thomas Gleixner
Quoted 5 lines
+++
b/kernel/time/timer_list.c(quoted diff) +0
-0@@ -68,7 +68,15 @@ static void print_active_timers(struct seq_file *m, struct hrtimer_clock_base *b touch_nmi_watchdog();Review static void print_active_timers(struct seq_file *m, struct hrtimer_clock_base *b · line 79
Quoted 10 lines
(quoted diff) +9
-1- raw_spin_lock_irqsave(&base->cpu_base->lock, flags); + if (in_nmi() || oops_in_progress || panic_in_progress()) { + if ((!IS_ENABLED(CONFIG_SMP) && in_nmi()) || + !raw_spin_trylock_irqsave(&base->cpu_base->lock, flags)) { + SEQ_printf(m, " [cpu_base lock contended]\n"); + return; + } + } else { + raw_spin_lock_irqsave(&base->cpu_base->lock, flags); + }TBH, that's horrible and unreadable gunk.
Clearly the approach of all of this is:
1) Pick some existing code
2) Invoke it
3) Hope that it works
4) If not add bandaids to make it "work"Seriously?
Each of the callers of
sys_info()knows the exact context in which it is invoked, right?
Quoted 6 lines
So the obvious and clean solution is to hand exactly that context information into
sys_info()and further down into the invoked functions so they can make informed decisions and eventually omit pointless information which is otherwise emitted in proc/sysrq-t.Something like the uncompiled mockup below should give you an idea.
Hi Thomas,
Fair point. Probing global state ad hoc inside individual helpers is much uglier than passing the caller's execution context explicitly. There are only a handful of sys_info() call sites in the tree, each of which already knows its exact context.
For the next iteration, I will introduce enum sysinfo_context, plumb it through sys_info() down to the consumers, and refactor kernel/time/timer_list.c around struct tlctx and lock_hrtimer_base() along the lines of your mockup.
Kind regards,
Signature · 1 line
Aaron Tomlin
Petr Mladek
maintainerRe: [RFC PATCH v2 1/5] panic, sys_info: Fix duplicate and dropped all-CPU backtraces in panic
In reply to Aaron TomlinQuoted 37 lines · Aaron Tomlin
When
sys_info()was factored out ofkernel/panic.cby commitb76e89e50fc3("panic: generalize panic_print's function to show sys info"),__sys_info()gained an unconditional call totrigger_all_cpu_backtrace()whenSYS_INFO_ALL_BTis set, whilepanic_other_cpus_shutdown()retained its own call topanic_trigger_all_cpu_backtrace().This causes two problems during a kernel panic:
1.
vpanic()invokespanic_trigger_all_cpu_backtrace()insidepanic_other_cpus_shutdown()before stopping secondary CPUs, and then invokestrigger_all_cpu_backtrace()a second time insidesys_info(panic_print)after secondary CPUs have already been shut down, stalling for up to 10 seconds waiting for offline CPUs to respond to NMIs. Furthermore, ifSYS_INFO_ALL_BTis configured via the kernel.kernel_sys_info sysctl rather thanpanic_print,panic_other_cpus_shutdown()misses it prior to CPU shutdown.2. If
sys_info(SYS_INFO_ALL_BT)is invoked on the panicking CPU prior topanic_other_cpus_shutdown(), callingtrigger_all_cpu_backtrace()directly bypassespanic_trigger_all_cpu_backtrace()and leavespanic_triggering_all_cpu_backtraceunset. Becausepanic_cpuis already active,vprintk_emit()silently drops all NMI backtrace messages originating from non-panic CPUs.Export
panic_trigger_all_cpu_backtrace()with apanic_on_this_cpu()and one-shot guard so all-CPU backtraces are only emitted once before secondary CPUs are stopped, consultsys_info_get_mask(panic_print)inpanic_other_cpus_shutdown(), record prior Oops stack dumps inpanic_try_start(), and routeSYS_INFO_ALL_BTin__sys_info()throughpanic_trigger_all_cpu_backtrace()on the panicking CPU.
kernel/panic.c bool panic_try_start(void) · line 460
Quoted 10 lines
kernel/panic.c +6
-1@@ -451,13 +452,21 @@ bool panic_try_start(void) old_cpu =PANIC_CPU_INVALID ; this_cpu =raw_smp_processor_id (); -return atomic_try_cmpxchg (&panic_cpu, &old_cpu, this_cpu); +if (!atomic_try_cmpxchg (&panic_cpu, &old_cpu, this_cpu)) +return false; + +if (atomic_read (&panic_redirect_cpu) ==PANIC_CPU_INVALID && + (test_taint (TAINT_DIE ) || oops_in_progress)) + panic_this_cpu_backtrace_printed = true;
This would be pretty hard to maintain. The complexity of the check shows how many code paths affect the decision. I can't imagine how to synchonize all the pieces this way.
We need another approach. I remember that I have already proposed something some time ago.
The idea was to enhance sys_info() API to remember what has already been printed. trigger_all_cpu_backtrace() and frieds should not longer be called directly. They should be called only via sys_info() API.
See https://lore.kernel.org/all/[email protected]/
for more details.
I think that this would be a better way to go.
What do you think, please?
Best Regards,
Petr
Petr Mladek
maintainerRe: [RFC PATCH v2 4/5] printk, tracing: Support console flush suppression and tail pinning
In reply to Aaron TomlinQuoted 11 lines · Aaron Tomlin
When collecting high-volume diagnostic output into the printk ring buffer during a kernel crash prior to kexec, synchronous console emission can stall the panicking CPU for tens of seconds on slow serial or IPMI Serial-over-LAN links, risking external hardware watchdog resets. At the same time, uninhibited ring buffer rotation can wrap and overwrite the initial Oops, faulting registers, and primary call trace.
Introduce two scoped mechanisms for in-memory crash telemetry collection:
1. Console flush suppression
Nit: We already use the word "suppress" for filtering less important
messages on the console, see suppress_message_printing().
I would prefer to use another name to avoid confusion. We basically want to "block" or "pause" consoles here.
Quoted 8 lines
Provide
printk_suppress_console_flush(bool)to clear the console flush mask inprintk_get_console_flush_type(), guard legacy record emission and kthread wakeups, and suppresswake_up_klogd()invprintk_emit(). Records written viavprintk_store()are stored in the in-memory ring buffer while bypassing synchronous hardware console emission and kthread wakeups.
We already have similar infrastructure for suspend, see console_suspend_all().
We should not use a completely different aproach to achieve the same. It would just complicate the code and design.
I suggest to add CON_PAUSED flag and handle it similar way as CON_SUSPENDED flag. It should be enough to:
+ update console_is_usable().
+ add console_pause_all_in_panic()
, where the console_pause_all_in_panic() would do similar things as console_suspend_all() with the following differences:
+ no flush
+ take console_srcu_read_lock() instead of console_list_lock()
+ no synchronize_srcu()
And I think that it will be only a one-way operation. We do not need a resume part.
Quoted 40 lines
2. Ring buffer tail pinning
Provide
printk_mark_panic_start(),printk_clear_panic_start(),printk_freeze_tail(bool), andprintk_is_tail_full(). Recording the next reserved sequence number at the start of a crash (or clamping toprb_first_valid_seq(prb)if the crash output itself already wrapped the buffer) allowsdata_make_reusable()anddesc_push_tail()to continue recycling older pre-crash records while refusing to advance the tail past the pinned crash sequence number. Once the watermark is reached,prb_reserve(),prb_reserve_in_last(), andftrace_dump_one()finalize the last record and stop reserving descriptors or consuming trace entries so the primary crash context and remaining trace buffer remain intact.kernel/printk/printk_ringbuffer.c +18
-0@@ -377,6 +377,90 @@ static struct prb_data_block *to_block(struct prb_data_ring *data_ring,return (void *)&data_ring->data[DATA_INDEX (data_ring, begin_lpos)]; } +static atomic64_t printk_panic_seq =ATOMIC64_INIT (U64_MAX ); +static bool printk_tail_frozen; +static bool printk_tail_full; + +/** +* printk_mark_panic_start - record the ring buffer sequence at crash start +* +* Pin the next reserved sequence number at the beginning of a top-level Oops, +* fatal warning, or panic so subsequent tail freezing can recycle older +* pre-crash records without overwriting the primary crash output. +* Context: Any context. +* Return: true if this call newly pinned the crash start sequence from +* U64_MAX, false if a crash start sequence was already active. +*/ +bool printk_mark_panic_start (void ) +{ +s64 panic_seq =atomic64_read (&printk_panic_seq); +u64 first_seq, next_seq;
kernel/printk/printk_ringbuffer.c static struct prb_data_block *to_block(struct prb_data_ring *data_ring, · line 408
Quoted 10 lines
kernel/printk/printk_ringbuffer.c +10
-0+bool pinned; + + do { + first_seq = prb_first_valid_seq(prb); + if ((u64)panic_seq == U64_MAX) { + next_seq = prb_next_reserve_seq(prb); + pinned = true; + } else if ((u64)panic_seq < first_seq) { + next_seq = first_seq; + pinned = false;
What scenario does this handle, please?
Why do we need to move the "pin" when there are concurrent callers? What happens when the original caller clears the pin and the other racy caller continues with panic()?
kernel/printk/printk_ringbuffer.c static struct prb_data_block *to_block(struct prb_data_ring *data_ring, · line 412
Quoted 8 lines
kernel/printk/printk_ringbuffer.c +4
-0+ }else { +return false; + } + }while (!atomic64_try_cmpxchg (&printk_panic_seq, &panic_seq, next_seq));
Are the atomic64 operations safe in NMI? I would expect that they use some locking.
I am afraid that we might need another approach.
IMHO, it would be much easier when the caller knows whether it will panic() or not. And the panicing caller would just freeze buffer so that the next_seq will always stay there.
Finally, please split the console pausing and ring buffer freezing into separate patches.
Best Regards,
Petr
Petr Mladek
maintainerRe: [RFC PATCH v2 4/5] printk, tracing: Support console flush suppression and tail pinning
Quoted 64 lines · Petr Mladek
Quoted 40 lines · Aaron Tomlin
2. Ring buffer tail pinning
Provide
printk_mark_panic_start(),printk_clear_panic_start(),printk_freeze_tail(bool), andprintk_is_tail_full(). Recording the next reserved sequence number at the start of a crash (or clamping toprb_first_valid_seq(prb)if the crash output itself already wrapped the buffer) allowsdata_make_reusable()anddesc_push_tail()to continue recycling older pre-crash records while refusing to advance the tail past the pinned crash sequence number. Once the watermark is reached,prb_reserve(),prb_reserve_in_last(), andftrace_dump_one()finalize the last record and stop reserving descriptors or consuming trace entries so the primary crash context and remaining trace buffer remain intact.kernel/printk/printk_ringbuffer.c +18
-0@@ -377,6 +377,90 @@ static struct prb_data_block *to_block(struct prb_data_ring *data_ring,return (void *)&data_ring->data[DATA_INDEX (data_ring, begin_lpos)]; } +static atomic64_t printk_panic_seq =ATOMIC64_INIT (U64_MAX ); +static bool printk_tail_frozen; +static bool printk_tail_full; + +/** +* printk_mark_panic_start - record the ring buffer sequence at crash start +* +* Pin the next reserved sequence number at the beginning of a top-level Oops, +* fatal warning, or panic so subsequent tail freezing can recycle older +* pre-crash records without overwriting the primary crash output. +* Context: Any context. +* Return: true if this call newly pinned the crash start sequence from +* U64_MAX, false if a crash start sequence was already active. +*/ +bool printk_mark_panic_start (void ) +{ +s64 panic_seq =atomic64_read (&printk_panic_seq); +u64 first_seq, next_seq;kernel/printk/printk_ringbuffer.c static struct prb_data_block *to_block(struct prb_data_ring *data_ring, · line 408
Quoted 10 lines
kernel/printk/printk_ringbuffer.c +10
-0+bool pinned; + + do { + first_seq = prb_first_valid_seq(prb); + if ((u64)panic_seq == U64_MAX) { + next_seq = prb_next_reserve_seq(prb); + pinned = true; + } else if ((u64)panic_seq < first_seq) { + next_seq = first_seq; + pinned = false;What scenario does this handle, please?
Why do we need to move the "pin" when there are concurrent callers? What happens when the original caller clears the pin and the other racy caller continues withpanic()?Quoted 3 lines
kernel/printk/printk_ringbuffer.c +3
-0+ }else { +return false; + }
Quoted 4 lines
kernel/printk/printk_ringbuffer.c static struct prb_data_block *to_block(struct prb_data_ring *data_ring, · line 412
Quoted 1 line
kernel/printk/printk_ringbuffer.c +1
-0+ }while (!atomic64_try_cmpxchg (&printk_panic_seq, &panic_seq, next_seq));Are the atomic64 operations safe in NMI? I would expect that they use some locking.
To make it clear. I think that it is not safe on 32-bit system. This is why we used 32-bit lpos values internally in the ring buffer. Also we use struct latched_seq for "clear_seq" in the printk code.
Quoted 5 lines
I am afraid that we might need another approach.
IMHO, it would be much easier when the caller knows whether it will
panic()or not. And the panicing caller would just freeze buffer so that thenext_seqwill always stay there.
I know that your proposed design make the new API easier for use. You just remember next_seq at the beginning of a potential panic() message. And you freeze the buffer when you realized that it really ended in panic(), ...
But the proposed changes in the printk_ringbuffer code look complicated. They are not easy to review, definitely.
I feel that there must be an easier solution. For example, desc_push_tail() reads the state of the tail_id descriptor including the @seq number. It should be enough to compare it with the pinned sequence number and return failure when it is greater or equal.
One more variable might be set when the pinned sequence is reached. And prb_reserve() might then always fail at the very beginning when we could not make any more space reusable.
I am not sure why you touched so many other locations. I guess that you wanted to allow saving short strings when they would fit into the remaining free space. But I think that it is not worth the complexity. This feature should be used with a big log buffer so using few more bytes is negligible. And it might even be misleading when we store some random short string later.
Best Regards,
Petr