Introduce policies for unsafe flushing
Subsystems (12)
John Ogness
maintainerv2 cover Introduce policies for unsafe flushing
v1 is here [0].
Until now unsafe flushing is only performed at the very end of panic(), before entering the infinite loop. For other situations (such as panic-kexec or panic-reboot or emergency-reboot) no unsafe flushing is performed. The reasoning for this is because an unsafe flush could lead to fireworks, which could prevent the kexec or reboot from occurring.
However, some users may be willing to take on such risks in order to see panic output. Indeed, for the netconsole (which does not support safe atomic printing) it is a regression that such unsafe printing does not occur. [1]
This series adds unsafe flushing policy flags per-console to allow individual consoles to perform unsafe flushing for such scenarios. The unsafe flushing is hard-code-enabled for the netconsole in patch 6/6. The justification is that this is not less safe than the legacy netconsole.
Later these policy flags could be easily exposed to userspace via /sys/class/consoles/, once the sysfs per-console interface is available.
Changes since v1:
- Make console.unsafe_flush_flags unsigned.
- Add missing kerneldoc description for printk_trigger_flush().
- Remove the WARN_ON_ONCE() in nbcon_context_try_acquire_hostile() since hostile takeovers are now possible from non-panic contexts.
- Also provide a flush reason for console_flush_on_panic().
- Add a global variable @legacy_flush_reason to track reason for legacy flushing.
- Explicitly set PR_FLUSH_RUNTIME to 0 since it is now used to initialize @legacy_flush_reason.
- Temporarily set @legacy_flush_reason during legacy flushing of console_flush_on_panic() and printk_trigger_flush().
- Also set/check ctxt->allow_unsafe_takeover in nbcon_legacy_emit_next_record() to enforce flush policies for legacy flushing.
John Ogness
[0] https://lore.kernel.org/lkml/[email protected]
[1] https://lore.kernel.org/lkml/[email protected]
John Ogness (6):
printk: Introduce and specify flush reasons
console: Introduce unsafe flush policy flags
printk: Evaluate flush policy in nbcon_allow_unsafe_takeover()
printk: Remove nbcon_atomic_flush_unsafe()
printk: Add panic+kexec flush point
netconsole: Always perform unsafe flushing on panic/kdb/restart
arch/loongarch/kernel/reset.c | 2 +- arch/powerpc/kernel/traps.c | 2 +- arch/powerpc/kernel/watchdog.c | 2 +- drivers/net/netconsole.c | 4 + include/linux/console.h | 39 +++++++--- include/linux/printk.h | 27 +++++-- kernel/crash_core.c | 1 + kernel/debug/debug_core.c | 2 +- kernel/panic.c | 8 +- kernel/printk/internal.h | 5 +- kernel/printk/nbcon.c | 129 ++++++++++++++++++++++----------- kernel/printk/printk.c | 38 +++++++--- kernel/reboot.c | 2 +- lib/nmi_backtrace.c | 2 +- 14 files changed, 181 insertions(+), 82 deletions(-)
base-commit: d01f86a7da8161aac8c6304b377cf58fb75861ca
Signature · 1 line
2.47.3
John Ogness
maintainerPATCH v2 1/6 printk: Introduce and specify flush reasons
Prepare for per-console atomic flushing policies by introducing enum pr_flush_reason to classify various reasons for performing a printk backlog flush. The reasons are specified for printk_trigger_flush() and console_flush_on_panic().
For atomic printing from printk_trigger_flush(), propagate the reason down to __nbcon_atomic_flush_pending_con(), where the actual atomic flushing is executed.
For legacy printing, use a new global variable @legacy_flush_reason to track the reason in case nbcon consoles are using legacy printing.
Update all printk_trigger_flush() and console_flush_on_panic() call sites as well as any explicit lower-level atomic flush calls to specify the reason.
Note that PR_FLUSH_RUNTIME is used for cases where the reason for the flush is not clearly defined.
No functional change.
--- arch/loongarch/kernel/reset.c | 2 +- arch/powerpc/kernel/traps.c | 2 +- arch/powerpc/kernel/watchdog.c | 2 +- include/linux/console.h | 2 +- include/linux/printk.h | 22 ++++++++++++++++++-- kernel/debug/debug_core.c | 2 +- kernel/panic.c | 6 +++--- kernel/printk/internal.h | 5 +++-- kernel/printk/nbcon.c | 28 ++++++++++++++++--------- kernel/printk/printk.c | 38 ++++++++++++++++++++++++---------- kernel/reboot.c | 2 +- lib/nmi_backtrace.c | 2 +- 12 files changed, 78 insertions(+), 35 deletions(-)
arch/loongarch/kernel/reset.c +1 -1
@@ -30,7 +30,7 @@ void machine_halt(void)clear_csr_ecfg (ECFG0_IM );pr_notice ("\n\n** You can safely turn off the power now **\n\n" ); -console_flush_on_panic (CONSOLE_FLUSH_PENDING ); +console_flush_on_panic (CONSOLE_FLUSH_PENDING ,PR_FLUSH_HANG );while (true) { __asm__ __volatile__("idle 0" : : :"memory" );
arch/powerpc/kernel/traps.c +1 -1
@@ -175,7 +175,7 @@ void panic_flush_kmsg_end(void)kmsg_dump (KMSG_DUMP_PANIC );bust_spinlocks (0 );debug_locks_off (); -console_flush_on_panic (CONSOLE_FLUSH_PENDING ); +console_flush_on_panic (CONSOLE_FLUSH_PENDING ,PR_FLUSH_RUNTIME ); }static unsigned long oops_begin (struct pt_regs *regs)
arch/powerpc/kernel/watchdog.c +1 -1
@@ -365,7 +365,7 @@ static void watchdog_timer_interrupt(int cpu)* * See wd_lockup_ipi. */ -printk_trigger_flush (); +printk_trigger_flush (PR_FLUSH_RUNTIME ); } }
include/linux/console.h +1 -1
@@ -713,7 +713,7 @@ extern void console_lock(void);extern int console_trylock (void );extern void console_unlock (void );extern void console_unblank (void ); -extern void console_flush_on_panic (enum con_flush_mode mode); +extern void console_flush_on_panic (enum con_flush_mode mode,enum pr_flush_reason reason);extern struct tty_driver *console_device (int *);extern void console_suspend (struct console *);extern void console_resume (struct console *);
include/linux/printk.h +20 -2
@@ -145,6 +145,24 @@ void early_printk(const char *s, ...) { }struct dev_printk_info ; +/** +* enum pr_flush_reason - Reason for a flush +* @PR_FLUSH_RUNTIME: A flush while the kernel is running +* @PR_FLUSH_RESTART: The kernel is ending, but another instance should +* follow (reboot, kexec) +* @PR_FLUSH_HANG: The kernel is ending and will enter an infinite loop +* @PR_FLUSH_KDB: Printing while KDB is active +* +* Flush reasons are used to determine if unsafe atomic flushing should be +* performed, based on individual console policies. +*/ +enum pr_flush_reason { +PR_FLUSH_RUNTIME =0 , +PR_FLUSH_RESTART , +PR_FLUSH_HANG , +PR_FLUSH_KDB , +}; +#ifdef CONFIG_PRINTK asmlinkage __printf(4, 0) int vprintk_emit(int facility, int level,
@@ -202,7 +220,7 @@ void dump_stack_print_info(const char *log_lvl);void show_regs_print_info (const char *log_lvl); extern asmlinkage void dump_stack_lvl(const char *log_lvl) __cold; extern asmlinkage void dump_stack(void) __cold; -void printk_trigger_flush (void ); +void printk_trigger_flush (enum pr_flush_reason reason);void console_try_replay_all (void );void printk_legacy_allow_panic_sync (void );extern bool nbcon_device_try_acquire (struct console *con);
@@ -298,7 +316,7 @@ static inline void dump_stack_lvl(const char *log_lvl)static inline void dump_stack (void ) { } -static inline void printk_trigger_flush (void ) +static inline void printk_trigger_flush (enum pr_flush_reason reason) { }static inline void console_try_replay_all (void )
kernel/debug/debug_core.c +1 -1
@@ -999,7 +999,7 @@ void kgdb_panic(const char *msg)return ;debug_locks_off (); -console_flush_on_panic (CONSOLE_FLUSH_PENDING ); +console_flush_on_panic (CONSOLE_FLUSH_PENDING ,PR_FLUSH_RUNTIME );if (dbg_kdb_mode)kdb_printf ("PANIC: %s\n" , msg);
kernel/panic.c +3 -3
@@ -707,11 +707,11 @@ void vpanic(const char *fmt, va_list args)* panic() is not being callled from OOPS. */ debug_locks_off (); -console_flush_on_panic (CONSOLE_FLUSH_PENDING ); +console_flush_on_panic (CONSOLE_FLUSH_PENDING ,PR_FLUSH_RUNTIME );if ((panic_print &SYS_INFO_PANIC_CONSOLE_REPLAY ) || panic_console_replay) -console_flush_on_panic (CONSOLE_REPLAY_ALL ); +console_flush_on_panic (CONSOLE_REPLAY_ALL ,PR_FLUSH_RUNTIME );if (!panic_blink) panic_blink = no_blink;
@@ -764,7 +764,7 @@ void vpanic(const char *fmt, va_list args)* defers printing (such as NMI) and irq_work is not available. * Explicitly flush the kernel log buffer one last time. */ -console_flush_on_panic (CONSOLE_FLUSH_PENDING ); +console_flush_on_panic (CONSOLE_FLUSH_PENDING ,PR_FLUSH_HANG );nbcon_atomic_flush_unsafe ();local_irq_enable ();
kernel/printk/internal.h +3 -2
@@ -104,7 +104,7 @@ void nbcon_seq_force(struct console *con, u64 seq);bool nbcon_alloc (struct console *con);void nbcon_free (struct console *con);enum nbcon_prio nbcon_get_default_prio (void ); -void nbcon_atomic_flush_pending (void ); +void nbcon_atomic_flush_pending (enum pr_flush_reason reason);bool nbcon_legacy_emit_next_record (struct console *con,bool *handover,int cookie,bool use_atomic);bool nbcon_kthread_create (struct console *con);
@@ -156,7 +156,7 @@ static inline void nbcon_seq_force(struct console *con, u64 seq) { }static inline bool nbcon_alloc (struct console *con) {return false; }static inline void nbcon_free (struct console *con) { }static inline enum nbcon_prio nbcon_get_default_prio (void ) {return NBCON_PRIO_NONE ; } -static inline void nbcon_atomic_flush_pending (void ) { } +static inline void nbcon_atomic_flush_pending (enum pr_flush_reason reason) { }static inline bool nbcon_legacy_emit_next_record (struct console *con,bool *handover,int cookie,bool use_atomic) {return false; }static inline void nbcon_kthread_wake (struct console *con) { }
@@ -168,6 +168,7 @@ extern bool have_boot_console;extern bool have_nbcon_console;extern bool have_legacy_console;extern bool legacy_allow_panic_sync; +extern enum pr_flush_reason legacy_flush_reason;/** * struct console_flush_type - Define available console flush methods
kernel/printk/nbcon.c +18 -10
@@ -1542,6 +1542,7 @@ bool nbcon_legacy_emit_next_record(struct console *con, bool *handover,* __nbcon_atomic_flush_pending_con - Flush specified nbcon console using its * write_atomic() callback * @con: The nbcon console to flush +* @reason: The reason for the flush * @stop_seq: Flush up until this record * * Return: 0 if @con was flushed up to @stop_seq Otherwise, error code on
@@ -1561,7 +1562,8 @@ bool nbcon_legacy_emit_next_record(struct console *con, bool *handover,* returned, it cannot be expected that the unfinalized record will become * available. */ -static int __nbcon_atomic_flush_pending_con (struct console *con,u64 stop_seq) +static int __nbcon_atomic_flush_pending_con (struct console *con,enum pr_flush_reason reason, +u64 stop_seq) {struct nbcon_write_context wctxt = { };struct nbcon_context *ctxt = &ACCESS_PRIVATE (&wctxt, ctxt);
@@ -1610,6 +1612,7 @@ static int __nbcon_atomic_flush_pending_con(struct console *con, u64 stop_seq)* nbcon_atomic_flush_pending_con - Flush specified nbcon console using its * write_atomic() callback * @con: The nbcon console to flush +* @reason: The reason for the flush * @stop_seq: Flush up until this record * * This will stop flushing before @stop_seq if another context has ownership.
@@ -1617,13 +1620,14 @@ static int __nbcon_atomic_flush_pending_con(struct console *con, u64 stop_seq)* are added while this context was flushing and there is no other context * to handle the printing, this context must also flush those records. */ -static void nbcon_atomic_flush_pending_con (struct console *con,u64 stop_seq) +static void nbcon_atomic_flush_pending_con (struct console *con,enum pr_flush_reason reason, +u64 stop_seq) {struct console_flush_type ft;int err; again: - err =__nbcon_atomic_flush_pending_con (con, stop_seq); + err =__nbcon_atomic_flush_pending_con (con, reason, stop_seq);/* * If there was a new owner (-EPERM, -EAGAIN), that context is
@@ -1652,9 +1656,10 @@ static void nbcon_atomic_flush_pending_con(struct console *con, u64 stop_seq)/** * __nbcon_atomic_flush_pending - Flush all nbcon consoles using their * write_atomic() callback +* @reason: The reason for the flush * @stop_seq: Flush up until this record */ -static void __nbcon_atomic_flush_pending (u64 stop_seq) +static void __nbcon_atomic_flush_pending (enum pr_flush_reason reason,u64 stop_seq) {struct console *con;int cookie;
@@ -1672,7 +1677,7 @@ static void __nbcon_atomic_flush_pending(u64 stop_seq)if (nbcon_seq_read (con) >= stop_seq)continue ; -nbcon_atomic_flush_pending_con (con, stop_seq); +nbcon_atomic_flush_pending_con (con, reason, stop_seq); }console_srcu_read_unlock (cookie); }
@@ -1681,14 +1686,16 @@ static void __nbcon_atomic_flush_pending(u64 stop_seq)* nbcon_atomic_flush_pending - Flush all nbcon consoles using their * write_atomic() callback * +* @reason: The reason for the flush +* * Flush the backlog up through the currently newest record. Any new * records added while flushing will not be flushed if there is another * context available to handle the flushing. This is to avoid one CPU * printing unbounded because other CPUs continue to add records. */ -void nbcon_atomic_flush_pending (void ) +void nbcon_atomic_flush_pending (enum pr_flush_reason reason) { -__nbcon_atomic_flush_pending (prb_next_reserve_seq (prb)); +__nbcon_atomic_flush_pending (reason,prb_next_reserve_seq (prb)); }/**
@@ -1701,7 +1708,7 @@ void nbcon_atomic_flush_pending(void)void nbcon_atomic_flush_unsafe (void ) { panic_nbcon_allow_unsafe_takeover = true; -__nbcon_atomic_flush_pending (prb_next_reserve_seq (prb)); +__nbcon_atomic_flush_pending (PR_FLUSH_HANG ,prb_next_reserve_seq (prb)); panic_nbcon_allow_unsafe_takeover = false; }
@@ -1931,7 +1938,8 @@ void nbcon_device_release(struct console *con)* using the legacy loop. */ if (ft.nbcon_atomic) { -__nbcon_atomic_flush_pending_con (con,prb_next_reserve_seq (prb)); +__nbcon_atomic_flush_pending_con (con,PR_FLUSH_RUNTIME , +prb_next_reserve_seq (prb)); }else if (ft.legacy_direct) {if (console_trylock ())console_unlock ();
@@ -2001,7 +2009,7 @@ void nbcon_kdb_release(struct nbcon_write_context *wctxt)* The console was locked only when the write_atomic() callback * was usable. */ -__nbcon_atomic_flush_pending_con (ctxt->console,prb_next_reserve_seq (prb)); +__nbcon_atomic_flush_pending_con (ctxt->console,PR_FLUSH_KDB ,prb_next_reserve_seq (prb)); }/**
kernel/printk/printk.c +27 -11
@@ -464,6 +464,14 @@ bool have_boot_console;/* See printk_legacy_allow_panic_sync() for details. */ bool legacy_allow_panic_sync; +/* +* While some context is performing a backlog flush, track the reason for +* the flush in case nbcon consoles are using legacy printing. +* Synchronized by the console_lock, except in console_flush_on_panic() +* when the console_lock is ignored. +*/ +enum pr_flush_reason legacy_flush_reason =PR_FLUSH_RUNTIME ; +/* Avoid using irq_work when suspending. */ bool console_irqwork_blocked;
@@ -2455,7 +2463,7 @@ asmlinkage int vprintk_emit(int facility, int level, printed_len =vprintk_store (facility, level, dev_info, fmt, args);if (ft.nbcon_atomic) -nbcon_atomic_flush_pending (); +nbcon_atomic_flush_pending (PR_FLUSH_RUNTIME );if (ft.nbcon_offload)nbcon_kthreads_wake ();
@@ -2870,7 +2878,7 @@ static int console_cpu_notify(unsigned int cpu)if (!cpuhp_tasks_frozen) {printk_get_console_flush_type (&ft);if (ft.nbcon_atomic) -nbcon_atomic_flush_pending (); +nbcon_atomic_flush_pending (PR_FLUSH_RUNTIME );if (ft.legacy_direct) {if (console_trylock ())console_unlock ();
@@ -3530,10 +3538,11 @@ static void __console_rewind_all(void)/** * console_flush_on_panic - flush console content on panic * @mode: flush all messages in buffer or just the pending ones +* @reason: The reason for the flush * * Immediately output all pending messages no matter what. */ -void console_flush_on_panic (enum con_flush_mode mode) +void console_flush_on_panic (enum con_flush_mode mode,enum pr_flush_reason reason) {struct console_flush_type ft;bool handover;
@@ -3561,11 +3570,14 @@ void console_flush_on_panic(enum con_flush_mode mode)printk_get_console_flush_type (&ft);if (ft.nbcon_atomic) -nbcon_atomic_flush_pending (); +nbcon_atomic_flush_pending (PR_FLUSH_RUNTIME );/* Flush legacy consoles once allowed, even when dangerous. */ -if (legacy_allow_panic_sync) +if (legacy_allow_panic_sync) { + legacy_flush_reason = reason;console_flush_all (false, &next_seq, &handover); + legacy_flush_reason =PR_FLUSH_RUNTIME ; + } }/*
@@ -3775,7 +3787,7 @@ static void printk_kthreads_shutdown(void *data)* The threads may have been stopped while printing a * backlog. Flush any records left over. */ -nbcon_atomic_flush_pending (); +nbcon_atomic_flush_pending (PR_FLUSH_RUNTIME ); }console_list_unlock (); }
@@ -4537,7 +4549,7 @@ static bool __pr_flush(struct console *con, int timeout_ms, bool reset_on_progre/* Flush the consoles so that records up to @seq are printed. */ printk_get_console_flush_type (&ft);if (ft.nbcon_atomic) -nbcon_atomic_flush_pending (); +nbcon_atomic_flush_pending (PR_FLUSH_RUNTIME );if (ft.legacy_direct) {console_lock ();console_unlock ();
@@ -4725,6 +4737,7 @@ void defer_console_output(void)/** * printk_trigger_flush - Attempt to flush printk buffer to consoles. +* @reason: The reason for the flush * * If possible, flush the printk buffer to all consoles in the caller's * context. If offloading is available, trigger deferred printing.
@@ -4732,18 +4745,21 @@ void defer_console_output(void)* This is best effort. Depending on the system state, console states, * and caller context, no actual flushing may result from this call. */ -void printk_trigger_flush (void ) +void printk_trigger_flush (enum pr_flush_reason reason) {struct console_flush_type ft;printk_get_console_flush_type (&ft);if (ft.nbcon_atomic) -nbcon_atomic_flush_pending (); +nbcon_atomic_flush_pending (reason);if (ft.nbcon_offload)nbcon_kthreads_wake ();if (ft.legacy_direct) { -if (console_trylock ()) +if (console_trylock ()) { + legacy_flush_reason = reason;console_unlock (); + legacy_flush_reason =PR_FLUSH_RUNTIME ; + } }if (ft.legacy_offload)defer_console_output ();
@@ -5090,7 +5106,7 @@ void console_try_replay_all(void)if (console_trylock ()) {__console_rewind_all ();if (ft.nbcon_atomic) -nbcon_atomic_flush_pending (); +nbcon_atomic_flush_pending (PR_FLUSH_RUNTIME );if (ft.nbcon_offload)nbcon_kthreads_wake ();if (ft.legacy_offload)
kernel/reboot.c +1 -1
@@ -101,7 +101,7 @@ void emergency_restart(void)* the emergency restart. Move them to the atomic mode. */ nbcon_cpu_emergency_enter (); -printk_trigger_flush (); +printk_trigger_flush (PR_FLUSH_RESTART );machine_emergency_restart (); }
lib/nmi_backtrace.c +1 -1
@@ -91,7 +91,7 @@ void nmi_trigger_cpumask_backtrace(const cpumask_t *mask,* Force flush any remote buffers that might be stuck in IRQ context * and therefore could not run their irq_work. */ -printk_trigger_flush (); +printk_trigger_flush (PR_FLUSH_RUNTIME );clear_bit_unlock (0 , &backtrace_flag);put_cpu ();
Signature · 1 line
2.47.3
[email protected]
AI reviewRe: [PATCH printk v2 1/6] printk: Introduce and specify flush reasons
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] printk: unlocked reset of legacy_flush_reason in printk_trigger_flush() --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] printk: unlocked reset of legacy_flush_reason in printk_trigger_flush()
--
commit 05bc755b72e7df0d3ba29e7530e04fba0747844c
Author: John Ogness <[email protected]>
printk: Introduce and specify flush reasons
This commit introduces enum pr_flush_reason and propagates flush reasons through printk_trigger_flush(), console_flush_on_panic(), and the nbcon and legacy console flushing paths.
Quoted 18 lines
kernel/printk/printk.c +8
-0@@ -464,6 +464,14 @@ bool have_boot_console;/* See printk_legacy_allow_panic_sync() for details. */ bool legacy_allow_panic_sync; +/* +* While some context is performing a backlog flush, track the reason for +* the flush in case nbcon consoles are using legacy printing. +* Synchronized by the console_lock, except in console_flush_on_panic() +* when the console_lock is ignored. +*/ +enum pr_flush_reason legacy_flush_reason =PR_FLUSH_RUNTIME ; +/* Avoid using irq_work when suspending. */ bool console_irqwork_blocked;
[ ... ]
Quoted 15 lines
kernel/printk/printk.c +2
-2@@ -4732,18 +4745,21 @@ void defer_console_output(void)* This is best effort. Depending on the system state, console states, * and caller context, no actual flushing may result from this call. */ -void printk_trigger_flush (void ) +void printk_trigger_flush (enum pr_flush_reason reason) {struct console_flush_type ft;printk_get_console_flush_type (&ft);if (ft.nbcon_atomic) -nbcon_atomic_flush_pending (); +nbcon_atomic_flush_pending (reason);if (ft.nbcon_offload)nbcon_kthreads_wake ();
kernel/printk/printk.c void defer_console_output(void) · line 4765
Quoted 10 lines
kernel/printk/printk.c +4
-1if (ft.legacy_direct) { -if (console_trylock ()) +if (console_trylock ()) { + legacy_flush_reason = reason;console_unlock (); + legacy_flush_reason =PR_FLUSH_RUNTIME ; + } }if (ft.legacy_offload)defer_console_output ();
[Severity: Medium]
Does resetting legacy_flush_reason after console_unlock() modify it outside console_lock and race with the next lock owner?
console_unlock() either hands console_lock over to a spinning waiter or releases console_sem in __console_unlock() before returning:
CPU0 CPU1printk_trigger_flush(PR_FLUSH_RUNTIME)
console_trylock()
legacy_flush_reason = PR_FLUSH_RUNTIME
console_unlock()
__console_unlock()
emergency_restart()
printk_trigger_flush(PR_FLUSH_RESTART)
console_trylock() legacy_flush_reason = PR_FLUSH_RESTART
legacy_flush_reason = PR_FLUSH_RUNTIME
console_unlock() <-- flushes with PR_FLUSH_RUNTIME
This overwrites the active flush reason while CPU1 holds console_lock (or while a spinning waiter that took over console_lock inside console_flush_all() is still flushing). Could legacy_flush_reason be reset before __console_unlock() or inside __console_flush_and_unlock() while console_lock is still held?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Petr Mladek
maintainerRe: [PATCH printk v2 1/6] printk: Introduce and specify flush reasons
In reply to John OgnessQuoted 33 lines · John Ogness
Prepare for per-console atomic flushing policies by introducing enum
pr_flush_reasonto classify various reasons for performing a printk backlog flush. The reasons are specified forprintk_trigger_flush()andconsole_flush_on_panic().For atomic printing from
printk_trigger_flush(), propagate the reason down to__nbcon_atomic_flush_pending_con(), where the actual atomic flushing is executed.For legacy printing, use a new global variable @legacy_flush_reason to track the reason in case nbcon consoles are using legacy printing.
Update all
printk_trigger_flush()andconsole_flush_on_panic()call sites as well as any explicit lower-level atomic flush calls to specify the reason.Note that
PR_FLUSH_RUNTIMEis used for cases where the reason for the flush is not clearly defined.include/linux/printk.h +7
-0@@ -145,6 +145,24 @@ void early_printk(const char *s, ...) { }struct dev_printk_info ; +/** +* enum pr_flush_reason - Reason for a flush +* @PR_FLUSH_RUNTIME: A flush while the kernel is running +* @PR_FLUSH_RESTART: The kernel is ending, but another instance should +* follow (reboot, kexec) +* @PR_FLUSH_HANG: The kernel is ending and will enter an infinite loop +* @PR_FLUSH_KDB: Printing while KDB is active
include/linux/printk.h void early_printk(const char *s, ...) { } · line 164
Quoted 10 lines
include/linux/printk.h +10
-0+* +* Flush reasons are used to determine if unsafe atomic flushing should be +* performed, based on individual console policies. +*/ +enum pr_flush_reason { +PR_FLUSH_RUNTIME =0 , +PR_FLUSH_RESTART , +PR_FLUSH_HANG , +PR_FLUSH_KDB , +};
This entire patchset adds a lot of complexity just to enable all 4 new policy flags for netconsole in the last patch.
There already have few dimensions which affect console flushing:
+ console_loglevel
+ nbcon_prio
This patchset adds 8 new terms in pr_flush_reason and nbcon_unsafe_flush_flags just to explicitly set all flush_flags in netconsole.
Let's start with pr_flush_reason. I am not sure if it is worth it:
+ PR_FLUSH_RUNTIME is the default
+ PR_FLUSH_RESTART can be detected by system_state > SYSTEM_RUNNING
+ PR_FLUSH_HANG has only one caller currently distinguished by the global panic_nbcon_allow_unsafe_takeover
+ PR_FLUSH_KDB can be detected by kdb_trap_printk and kdb_printf_cpu, see vprintk() implementation.
IMHO, it makes things worse because nbcon_atomic_flush_pending() has to use PR_FLUSH_RUNTIME in vprintk_emit(). But I think that we would like to flush it the same way as an explicit flush called in the given context.
Best Regards,
Petr
John Ogness
maintainerRe: [PATCH printk v2 1/6] printk: Introduce and specify flush reasons
Quoted 7 lines · Petr Mladek
This entire patchset adds a lot of complexity just to enable all 4 new policy flags for netconsole in the last patch.
There already have few dimensions which affect console flushing:
+
console_loglevel
+nbcon_prio
"Dimensions" is the correct word. These are orthogonal:
+ console_loglevel: which messages to print
+ nbcon_prio: which context is allowed access to hardware
+ pr_flush_reason: reason for atomic flushing
+ unsafe_flush_flags: policy when unsafe flushing is acceptable
I do not see a single property above where a value in one implies a value in another.
Quoted 9 lines
This patchset adds 8 new terms in
pr_flush_reasonandnbcon_unsafe_flush_flagsjust to explicitly set allflush_flagsin netconsole.Let's start with
pr_flush_reason. I am not sure if it is worth it:+
PR_FLUSH_RUNTIMEis the default+
PR_FLUSH_RESTARTcan be detected bysystem_state>SYSTEM_RUNNING
This does not cover the panic-restart case.
Quoted 2 lines
+
PR_FLUSH_HANGhas only one caller currently distinguished by the globalpanic_nbcon_allow_unsafe_takeover
In my series there is another user:
arch/loongarch/kernel/reset.c:machine_halt()
Quoted 2 lines
+
PR_FLUSH_KDBcan be detected bykdb_trap_printkandkdb_printf_cpu, seevprintk()implementation.
Ack. PR_FLUSH_RUNTIME could cover this case.
Quoted 4 lines
IMHO, it makes things worse because
nbcon_atomic_flush_pending()has to usePR_FLUSH_RUNTIMEinvprintk_emit(). But I think that we would like to flush it the same way as an explicit flush called in the given context.
You write it backwards. vprintk_emit() uses nbcon_atomic_flush_pending(). Calling printk() (a single record) is quite different than calling printk_trigger_flush() (flushing a backlog).
Let us discuss this after I respond to your comments on patch 2/6. There you include many details and suggestions that require closer inspection.
John
John Ogness
maintainerPATCH v2 2/6 console: Introduce unsafe flush policy flags
In reply to John OgnessPrepare for per-console atomic flushing policies by introducing a new nbcon-specific console field @unsafe_flush_flags to store the unsafe flush policies for a console. The policies are represented by bits, specified by enum nbcon_unsafe_flush_flags.
Currently it is expected that any @unsafe_flush_flags bits are set prior to console registration and do not change while registered.
No functional change.
--- include/linux/console.h | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+)
include/linux/console.h +26 -0
@@ -194,6 +194,29 @@ enum cons_flags { CON_NBCON_ATOMIC_UNSAFE = BIT(9), }; +/** +* enum nbcon_unsafe_flush_flags - Unsafe atomic flush policy flags +* @NBCON_PANIC_FLUSH: In panic, unsafe flush before kexec or reboot. +* @NBCON_PANIC_PRINTK: In panic, unsafe flush on each printk(). +* @NBCON_NORMAL_NOTHREAD: During normal operation, when printing +* kthreads are unreliable or have been shutdown, +* unsafe flush on each printk() or on explicit +* flushing. +* @NBCON_KDB: Unsafe flush kdb output. +* +* NBCON unsafe atomic flushing always occurs at the end of panic() before +* the kernel enters an infinite loop. Users may choose to perform unsafe +* flushing in additional scenarios where the current kernel is ending in +* order to increase the chances of seeing console messages, even if it is +* not safe to do so. +*/ +enum nbcon_unsafe_flush_flags { +NBCON_PANIC_FLUSH =BIT (0 ), +NBCON_PANIC_PRINTK =BIT (1 ), +NBCON_NORMAL_NOTHREAD =BIT (2 ), +NBCON_KDB =BIT (3 ), +}; +/** * struct nbcon_state - console state for nbcon consoles * @atom: Compound of the state fields for atomic operations
@@ -326,6 +349,7 @@ struct nbcon_write_context {* @data: Driver private data * @node: hlist node for the console list * +* @unsafe_flush_flags: Unsafe atomic flush policy. See enum nbcon_unsafe_flush_flags * @nbcon_state: State for nbcon consoles * @nbcon_seq: Sequence number of the next record for nbcon to print * @nbcon_device_ctxt: Context available for non-printing operations
@@ -461,6 +485,8 @@ struct console {*/ void (*device_unlock)(struct console *con,unsigned long flags); +unsigned int unsafe_flush_flags; +atomic_t __private nbcon_state;atomic_long_t __private nbcon_seq;struct nbcon_context __private nbcon_device_ctxt;
Signature · 1 line
2.47.3
Petr Mladek
maintainerRe: [PATCH printk v2 2/6] console: Introduce unsafe flush policy flags
Quoted 20 lines · John Ogness
Prepare for per-console atomic flushing policies by introducing a new nbcon-specific console field @unsafe_flush_flags to store the unsafe flush policies for a console. The policies are represented by bits, specified by enum
nbcon_unsafe_flush_flags.Currently it is expected that any @unsafe_flush_flags bits are set prior to console registration and do not change while registered.
No functional change.
Signed-off-by John Ogness <[email protected]>--- include/linux/console.h | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+)include/linux/console.h +0
-0
include/linux/console.h enum cons_flags { · line 200
Quoted 7 lines
include/linux/console.h +4
-0@@ -194,6 +194,29 @@ enum cons_flags { CON_NBCON_ATOMIC_UNSAFE = BIT(9), }; +/** +* enum nbcon_unsafe_flush_flags - Unsafe atomic flush policy flags +* @NBCON_PANIC_FLUSH: In panic, unsafe flush before kexec or reboot. +* @NBCON_PANIC_PRINTK: In panic, unsafe flush on each printk().
Do we need to distinguish between flush in printk() and an explicit flush?
IMHO, there is no reason. We usually call the explicit flush because we want to trigger it. But printk(), called in the same context, should trigger/do it as well.
include/linux/console.h enum cons_flags { · line 204
Quoted 4 lines
include/linux/console.h +4
-0+* @NBCON_NORMAL_NOTHREAD: During normal operation, when printing +* kthreads are unreliable or have been shutdown, +* unsafe flush on each printk() or on explicit +* flushing.
Do we need to distinguish between panic and non-panic paths when the kthreads do not work?
IMHO, this is more about that the system is going down and the kthreads do not work.
Also what about emergency context when the system is about to continue?
include/linux/console.h enum cons_flags { · line 205
Quoted 1 line
include/linux/console.h +1
-0+* @NBCON_KDB: Unsafe flush kdb output.
OK, let's assume that we want to set a policy where we allow the unsafe takeover in the following contexts:
NBCON_ALLOW_UNSAFE_NEVER
NBCON_ALLOW_UNSAFE_RESTART
NBCON_ALLOW_UNSAFE_EMERGENCY
NBCON_ALLOW_UNSAFE_KDB
, where
+ NEVER might actually allow it when explicitely enabled in nbcon_panic_flush_unsafe(). The word "never" just has a simple semantic in the context of the other values
+ RESTART would allow the unsafe takeover in printk()/flush() when the system is doing halt/reboot/crash_kexec...
+ EMERGENCY would allow it even in NBCON_PRIO_EMERGENCY context.
+ KDB would allow it in KDB context
And I would get the context the following way:
enum nbcon_allow_unsafe get_nbcon_allow_unsafe_request(void)
{
enum nbcon_prio prio;/*
* The kdb check is copied from vprintk() implementation.
* We should put it into a helper.
*/
#ifdef CONFIG_KGDB_KDB
/* Allow to pass printk() to kdb but avoid a recursion. */
if (unlikely(kdb_trap_printk && kdb_printf_cpu < 0))
return NBCON_ALLOW_UNSAFE_KDB;#endif
prio = nbcon_get_default_prio(); /* FIXME: What about SYSTEM_SUSPEND? if (prio == NBCON_PRIO_PANIC || system_state > SYSTEM_RUNNING) return NBCON_ALLOW_UNSAFE_RESTART; if (prio == NBCON_PRIO_EMERGENCY) return NBCON_ALLOW_UNSAFE_EMERGENCY; return NBCON_ALLOW_UNSAFE_NEVER; }
IMHO, this better fits the existing terminology and design. And it should be much easier to understand by developers and users.
+ *
Quoted 25 lines
include/linux/console.h +14
-0+* NBCON unsafe atomic flushing always occurs at the end of panic() before +* the kernel enters an infinite loop. Users may choose to perform unsafe +* flushing in additional scenarios where the current kernel is ending in +* order to increase the chances of seeing console messages, even if it is +* not safe to do so. +*/ +enum nbcon_unsafe_flush_flags { +NBCON_PANIC_FLUSH =BIT (0 ), +NBCON_PANIC_PRINTK =BIT (1 ), +NBCON_NORMAL_NOTHREAD =BIT (2 ), +NBCON_KDB =BIT (3 ), +}; +/** * struct nbcon_state - console state for nbcon consoles * @atom: Compound of the state fields for atomic operations @@ -326,6 +349,7 @@ struct nbcon_write_context {* @data: Driver private data * @node: hlist node for the console list * +* @unsafe_flush_flags: Unsafe atomic flush policy. See enum nbcon_unsafe_flush_flags * @nbcon_state: State for nbcon consoles * @nbcon_seq: Sequence number of the next record for nbcon to print * @nbcon_device_ctxt: Context available for non-printing operations
include/linux/console.h struct console { · line 488
Quoted 4 lines
include/linux/console.h +1
-0@@ -461,6 +485,8 @@ struct console {*/ void (*device_unlock)(struct console *con,unsigned long flags); +unsigned int unsafe_flush_flags;
I would call it:
unsigned int allow_unsafe_policy;
include/linux/console.h struct console { · line 492
Quoted 4 lines
include/linux/console.h +1
-0+atomic_t __private nbcon_state;atomic_long_t __private nbcon_seq;struct nbcon_context __private nbcon_device_ctxt;
And the net console would get initialized to:
.allow_unsafe_policy = NBCON_ALLOW_UNSAFE_RESTART | NBCON_ALLOW_UNSAFE_KDB;
And nbcon_allow_unsafe_takeover would get implemented as:
static bool nbcon_allow_unsafe_takeover(struct console *con)
{
allow_unsafe_request = get_nbcon_allow_unsafe();
if (con->allow_unsafe_policy & get_nbcon_allow_usafe_request())
return true;
return panic_nbcon_allow_unsafe_takeover;
}What do you think, please?
Best Regards,
Petr
John Ogness
maintainerRe: [PATCH printk v2 2/6] console: Introduce unsafe flush policy flags
Hi Petr,
At the end I suggestion some simplification. But I want to address your questions so that you understand my reasoning... even if we decide we will simplify things.
Quoted 8 lines · Petr Mladek
Quoted 8 lines
include/linux/console.h +0
-0@@ -194,6 +194,29 @@ enum cons_flags { CON_NBCON_ATOMIC_UNSAFE = BIT(9), };
Quoted 6 lines
include/linux/console.h enum cons_flags { · line 200
Quoted 4 lines
include/linux/console.h +4
-0+/** +* enum nbcon_unsafe_flush_flags - Unsafe atomic flush policy flags +* @NBCON_PANIC_FLUSH: In panic, unsafe flush before kexec or reboot. +* @NBCON_PANIC_PRINTK: In panic, unsafe flush on each printk(). Do we need to distinguish between flush in
printk()and an explicit flush?
Please keep in mind that these are _per-console_ policies for _unsafe_ flushing.
Quoted 3 lines
IMHO, there is no reason. We usually call the explicit flush because we want to trigger it. But
printk(), called in the same context, should trigger/do it as well.
Let us use an example: A panic backtrace that is followed by a restart. In my example I have a safe-atomic 8250 UART console and an unsafe-atomic netconsole.
If we handle printk and flush the same, then what are the choices:
1. I can choose to enable unsafe-atomic netconsole printing and attempt unsafe printing each line of the backtrace as printk() is called. This risks hanging the system on the first line, and even via 8250 UART I only get the first line.
2. I can choose not to enable unsafe-atomic netconsole printing and see the full backtrace via 8250 UART but no backtrace via netconsole.
My motivation is to provide a third option:
3. I can choose to enable unsafe-atomic netconsole for backlog flushes. Then I safely see 8250 UART output per line and right before the restart, the netconsole unsafely prints the backlog.
Quoted 10 lines
include/linux/console.h enum cons_flags { · line 204
Quoted 4 lines
include/linux/console.h +4
-0+* @NBCON_NORMAL_NOTHREAD: During normal operation, when printing +* kthreads are unreliable or have been shutdown, +* unsafe flush on each printk() or on explicit +* flushing. Do we need to distinguish between panic and non-panic paths when the kthreads do not work?
IMHO, this is more about that the system is going down and the kthreads do not work.
There are multiple non-panic paths when kthreads will not work. Some of them are because the kthreads are shutdown. Others are because it is an immediate trigger and the kthreads have no chance to act. It was a mistake for me to group both of these together because although one is truly normal, the other is not. I will talk more about this below.
Quoted 2 lines
Also what about emergency context when the system is about to continue?
I do not think it makes sense to offer this policy. We cannot be unsafe and then expect the system to continue correctly.
Quoted 10 lines
include/linux/console.h enum cons_flags { · line 205
Quoted 1 line
include/linux/console.h +1
-0+* @NBCON_KDB: Unsafe flush kdb output. OK, let's assume that we want to set a policy where we allow the unsafe takeover in the following contexts:
NBCON_ALLOW_UNSAFE_NEVER
NBCON_ALLOW_UNSAFE_RESTART
NBCON_ALLOW_UNSAFE_EMERGENCY
NBCON_ALLOW_UNSAFE_KDB
Quoted 5 lines
, where
+ NEVER might actually allow it when explicitely enabled in
nbcon_panic_flush_unsafe(). The word "never" just has a simple semantic in the context of the other values
We do not need a flag to mean "no flag".
Quoted 2 lines
+ RESTART would allow the unsafe takeover in printk()/flush() when the system is doing halt/reboot/crash_kexec...
Above I talked about printk vs flush. And IMHO it is important that users can distinguish between panic and non-panic (even if non-panic is a kind of emergency... for example sysrq-b).
Quoted 1 line
+ EMERGENCY would allow it even in
NBCON_PRIO_EMERGENCYcontext.
This does not make sense if normal operation continues (for example from a WARN()). But it makes sense in the reboot context (sysrq-b).
Quoted 1 line
+ KDB would allow it in KDB context
This was not actually requested so we could drop this policy for now.
Quoted 14 lines
And I would get the context the following way:
enum
nbcon_allow_unsafeget_nbcon_allow_unsafe_request(void){ enum nbcon_prio prio;/*
* The kdb check is copied fromvprintk()implementation.
* We should put it into a helper.
*/#ifdef CONFIG_KGDB_KDB /* Allow to pass printk() to kdb but avoid a recursion. */ if (unlikely(kdb_trap_printk && kdb_printf_cpu < 0)) return NBCON_ALLOW_UNSAFE_KDB;
Quoted 7 lines
#endif
prio = nbcon_get_default_prio(); /* FIXME: What about SYSTEM_SUSPEND? if (prio == NBCON_PRIO_PANIC || system_state > SYSTEM_RUNNING) return NBCON_ALLOW_UNSAFE_RESTART;
I do not agree with putting panic and non-panic under the same policy.
Quoted 2 lines
if (prio == NBCON_PRIO_EMERGENCY) return NBCON_ALLOW_UNSAFE_EMERGENCY;
Too dangerous if the kernel is not ending.
Quoted 6 lines
return NBCON_ALLOW_UNSAFE_NEVER; }IMHO, this better fits the existing terminology and design. And it should be much easier to understand by developers and users.
...
Quoted 29 lines
Quoted 29 lines
+ *
+ * NBCON unsafe atomic flushing always occurs at the end ofpanic()before
+ * the kernel enters an infinite loop. Users may choose to perform unsafe
+ * flushing in additional scenarios where the current kernel is ending in
+ * order to increase the chances of seeing console messages, even if it is
+ * not safe to do so.
+ */include/linux/console.h +8
-0+enum nbcon_unsafe_flush_flags { +NBCON_PANIC_FLUSH =BIT (0 ), +NBCON_PANIC_PRINTK =BIT (1 ), +NBCON_NORMAL_NOTHREAD =BIT (2 ), +NBCON_KDB =BIT (3 ), +}; +/** * struct nbcon_state - console state for nbcon consoles * @atom: Compound of the state fields for atomic operations @@ -326,6 +349,7 @@ struct nbcon_write_context {* @data: Driver private data * @node: hlist node for the console list * +* @unsafe_flush_flags: Unsafe atomic flush policy. See enum nbcon_unsafe_flush_flags * @nbcon_state: State for nbcon consoles * @nbcon_seq: Sequence number of the next record for nbcon to print * @nbcon_device_ctxt: Context available for non-printing operations @@ -461,6 +485,8 @@ struct console {*/ void (*device_unlock)(struct console *con,unsigned long flags);
Quoted 5 lines
include/linux/console.h struct console { · line 488
Quoted 1 line
include/linux/console.h +1
-0+unsigned int unsafe_flush_flags;I would call it:
unsigned int allow_unsafe_policy;
OK.
Quoted 22 lines
include/linux/console.h struct console { · line 492
Quoted 4 lines
include/linux/console.h +1
-0+atomic_t __private nbcon_state;atomic_long_t __private nbcon_seq;struct nbcon_context __private nbcon_device_ctxt;And the net console would get initialized to:
.allow_unsafe_policy = NBCON_ALLOW_UNSAFE_RESTART | NBCON_ALLOW_UNSAFE_KDB;And
nbcon_allow_unsafe_takeoverwould get implemented as:static bool
nbcon_allow_unsafe_takeover(struct console *con){ allow_unsafe_request = get_nbcon_allow_unsafe(); if (con->allow_unsafe_policy & get_nbcon_allow_usafe_request()) return true; return panic_nbcon_allow_unsafe_takeover; }
Quoted 1 line
What do you think, please?
I suggest only supporting explicit flushing with only two unsafe policies for now:
- NBCON_PANIC_RESTART_FLUSH (panic-kexec, panic-reboot)
- NBCON_EMERGENCY_RESTART_FLUSH (non-panic emergency reboot)
Only these were originally requested. But I would like to keep the others in mind because I think they are useful for certain scenarios:
- NBCON_PANIC_PRINTK (printk() calls during panic)
- NBCON_KDB (printk() and flush() during kdb)
- NBCON_NORMAL_NOTHREAD (printk() and flush() during normal kexec, reboot, shutdown)
However, I still think it is necessary to include context in order to properly evaluate the policies. I do not see how this can be effectively implemented without printk_trigger_flush() receiving a @reason argument to provide context. CPU and system states are not enough.
You have given me some new ideas. I would like to post a v3, also to address the mess I made of legacy mode (pointed out by Sashiko).
John
John Ogness
maintainerPATCH v2 3/6 printk: Evaluate flush policy in nbcon_allow_unsafe_takeover()
In reply to John OgnessCurrently nbcon_allow_unsafe_takeover() is a hack, relying on the temporary setting of a global variable @panic_nbcon_allow_unsafe_takeover as a gate to determine if unsafe flushing is allowed. This hack works because unsafe flushing is only performed at the end of panic(). However, this hack is _not_ sufficient if unsafe flushing is to be allowed in other contexts.
Allow unsafe flushing in other contexts by modifying nbcon_allow_unsafe_takeover() to take a console and flush reason as input and, based on the unsafe flush policies of that console, determine if unsafe flushing is allowed. This also allows the global variable @panic_nbcon_allow_unsafe_takeover to be removed.
Remove the hard-coded policy for CON_NBCON_ATOMIC_UNSAFE from console_is_usable() and implement that policy logic within nbcon_allow_unsafe_takeover().
Note that this changes the flushing flow for CON_NBCON_ATOMIC_UNSAFE consoles. Rather than console_is_usable() reporting that the console is not usable, __nbcon_atomic_flush_pending_con() will report that it is unable to acquire ownership.
--- include/linux/console.h | 11 +---- kernel/printk/nbcon.c | 91 +++++++++++++++++++++++++++++++---------- 2 files changed, 72 insertions(+), 30 deletions(-)
include/linux/console.h +2 -9
@@ -179,7 +179,8 @@ static inline void con_debug_leave(void) { }* @CON_NBCON: Console can operate outside of the legacy style console_lock * constraints. * @CON_NBCON_ATOMIC_UNSAFE: The write_atomic() callback is not safe and is -* therefore only used by nbcon_atomic_flush_unsafe(). +* therefore only called when allowed by +* nbcon_allow_unsafe_takeover(). */ enum cons_flags {CON_PRINTBUFFER =BIT (0 ),
@@ -637,7 +638,6 @@ extern void nbcon_write_context_set_buf(struct nbcon_write_context *wctxt,extern bool nbcon_enter_unsafe (struct nbcon_write_context *wctxt);extern bool nbcon_exit_unsafe (struct nbcon_write_context *wctxt);extern void nbcon_reacquire_nobuf (struct nbcon_write_context *wctxt); -extern bool nbcon_allow_unsafe_takeover (void );extern bool nbcon_kdb_try_acquire (struct console *con,struct nbcon_write_context *wctxt);extern void nbcon_kdb_release (struct nbcon_write_context *wctxt);
@@ -670,13 +670,6 @@ static inline bool console_is_usable(struct console *con, short flags, bool use_/* The write_atomic() callback is optional. */ if (!con->write_atomic)return false; - -/* -* An unsafe write_atomic() callback is only usable -* when unsafe takeovers are allowed. -*/ -if ((flags &CON_NBCON_ATOMIC_UNSAFE ) && !nbcon_allow_unsafe_takeover ()) -return false; }/*
kernel/printk/nbcon.c +70 -21
134 lines not on this page · raw message
Signature · 1 line
2.47.3
[email protected]
AI reviewRe: [PATCH printk v2 3/6] printk: Evaluate flush policy in nbcon_allow_unsafe_takeover()
Thank you for your contribution! Sashiko AI review found 5 potential issues to consider: - [High] printk: infinite loop in __console_flush_and_unlock() for atomic-unsafe nbcon - [High] printk…
Thank you for your contribution! Sashiko AI review found 5 potential issues to consider:
- [High] printk: infinite loop in __console_flush_and_unlock() for atomic-unsafe nbcon
- [High] printk: nbcon: non-panic hostile takeovers corrupt pbufs and deadlock reacquire
- [Medium] printk: nbcon: priority checks block non-panic unsafe hostile takeovers
- [Medium] printk: nbcon: unvalidated state in nbcon_context_try_acquire_hostile() loop
- [Medium] printk: nbcon_allow_unsafe_takeover() matches SYSTEM_SUSPEND on shutdown check
--
commit 86f4d1a44fe2165e053c542b740c4c0a437ea5d5
Author: John Ogness <[email protected]>
printk: Evaluate flush policy in nbcon_allow_unsafe_takeover()
This commit replaces the global panic_nbcon_allow_unsafe_takeover flag with per-console policy evaluation in nbcon_allow_unsafe_takeover() based on the flush reason, and moves the CON_NBCON_ATOMIC_UNSAFE check out of console_is_usable() into the nbcon acquire and flush paths.
Quoted 4 lines
include/linux/console.h +0
-0
[ ... ]
Quoted 4 lines
include/linux/console.h +0
-0@@ -670,13 +670,6 @@ static inline bool console_is_usable(struct console *con, short flags, bool use_/* The write_atomic() callback is optional. */ if (!con->write_atomic)return false;
include/linux/console.h static inline bool console_is_usable(struct console *con, short flags, bool use_ · line 675
Quoted 10 lines
include/linux/console.h +0
-7- -/* -* An unsafe write_atomic() callback is only usable -* when unsafe takeovers are allowed. -*/ -if ((flags &CON_NBCON_ATOMIC_UNSAFE ) && !nbcon_allow_unsafe_takeover ()) -return false; }/*
[Severity: High]
Can removing the CON_NBCON_ATOMIC_UNSAFE check from console_is_usable() cause __console_flush_and_unlock() to spin in an infinite loop when flushing via the legacy loop in atomic mode?
When have_boot_console is set, printk_get_console_flush_type() routes nbcon consoles through console_flush_all(). If console_unlock() runs with console_may_schedule == 0 (for example from vprintk_emit() with preemption disabled), console_flush_one_record() calls console_is_usable(con, flags, true), which now returns true for a CON_NBCON_ATOMIC_UNSAFE console and sets any_usable = true:
kernel/printk/printk.c:console_flush_one_record() {
...
if (!console_is_usable(con, flags, !do_cond_resched))
continue;
any_usable = true;
if (flags & CON_NBCON) {
progress = nbcon_legacy_emit_next_record(con, handover, cookie,
!do_cond_resched);
printk_seq = nbcon_seq_read(con);
...
}nbcon_legacy_emit_next_record() then returns false because allow_unsafe_takeover is false, leaving nbcon_seq_read(con) unchanged and setting *next_seq to that unprinted sequence number. If no other usable console advances *next_seq past the ringbuffer head (for instance while unregister_console_locked() is removing the boot console and sleeping in synchronize_srcu() before updating have_boot_console, or when the boot console is suspended), console_flush_all() returns flushed = true with *next_seq still valid in prb, and __console_flush_and_unlock() loops forever on while (prb_read_valid(prb, next_seq, NULL) && console_trylock()).
In addition, unregister_console_locked() checks console_is_usable(console, console->flags, true) before calling __pr_flush(console, 1000, true). When printk kthreads are not running, unregistering a CON_NBCON_ATOMIC_UNSAFE console now stalls for the full 1000ms timeout because neither a kthread nor atomic flushing can advance it.
kernel/printk/nbcon.c static int nbcon_context_try_acquire_hostile(struct nbcon_context *ctxt, · line 563
Quoted 12 lines
kernel/printk/nbcon.c +0
-4@@ -557,10 +557,6 @@ static int nbcon_context_try_acquire_hostile(struct nbcon_context *ctxt,if (!ctxt->allow_unsafe_takeover)return -EPERM ; -/* Ensure caller is allowed to perform unsafe hostile takeovers. */ -if (WARN_ON_ONCE (ctxt->prio !=NBCON_PRIO_PANIC )) -return -EPERM ; -
[Severity: High]
Can allowing hostile takeovers outside the final panic flush corrupt con->pbufs and cause interrupted console drivers to spin forever?
First, nbcon_context_try_acquire() only selects panic_nbcon_pbufs when panic_on_this_cpu() is true, and otherwise assigns ctxt->pbufs = con->pbufs. NBCON_PRIO_NORMAL and NBCON_PRIO_EMERGENCY share con->pbufs and rely on the unsafe section in nbcon_emit_next_record() to avoid clobbering each other's buffer. If a non-panic context (such as emergency_restart() with NBCON_NORMAL_NOTHREAD or KDB with NBCON_KDB) performs a hostile takeover while an owner is in that unsafe section, both contexts concurrently read and write con->pbufs.
Second, a hostile takeover sets cur.unsafe_takeover = 1, which causes __nbcon_context_update_unsafe() and nbcon_context_release() to keep cur.unsafe = 1 permanently. Any later acquire with allow_unsafe_takeover == false then fails. When an interrupted driver (such as serial8250_console_write() or pl011_console_write_thread()) tries to restore hardware state via nbcon_reacquire_nobuf(), or when a caller enters __uart_port_nbcon_acquire() after KDB exits, it spins forever:
CPU0 CPU1serial8250_console_write()
nbcon_enter_unsafe()
kdb / emergency_restart()
...
nbcon_context_try_acquire_hostile()
/* sets unsafe_takeover = 1 */
nbcon_context_release()
/* leaves unsafe = 1 */ nbcon_exit_unsafe() -> false nbcon_reacquire_nobuf()
while (!nbcon_context_try_acquire(ctxt, true))
cpu_relax(); <-- spins forever
kernel/printk/nbcon.c static int nbcon_context_try_acquire_hostile(struct nbcon_context *ctxt, · line 562
Quoted 3 lines
kernel/printk/nbcon.c +0
-0/* * Check that try_acquire_direct() and try_acquire_handover() returned * -EBUSY in the right situation.
[Severity: Medium]
What happens in nbcon_context_try_acquire_hostile() if con->nbcon_state changes on another CPU before nbcon_state_try_cmpxchg() succeeds?
Previously nbcon_context_try_acquire_hostile() only ran at the end of panic() after other CPUs were stopped. Now that it can run while other CPUs are active (such as during early panic printk() calls with NBCON_PANIC_PRINTK or in emergency_restart()), the do-while cmpxchg loop does not recheck cur->unsafe, cur->prio, or cur->req_prio after a failed cmpxchg updates cur:
kernel/printk/nbcon.c:nbcon_context_try_acquire_hostile() {
...
do {
new.atom = cur->atom;
new.cpu = cpu;
new.prio = ctxt->prio;
new.unsafe |= cur->unsafe_takeover;
new.unsafe_takeover |= cur->unsafe;
} while (!nbcon_state_try_cmpxchg(con, cur, &new));
...
}If the previous owner exits its unsafe section or releases the console, or if another CPU enters panic and acquires the console with NBCON_PRIO_PANIC, the next loop iteration will still overwrite con->nbcon_state with ctxt->prio and leave any stale req_prio set.
Quoted 1 line
kernel/printk/nbcon.c +0
-0@@ -1453,24 +1449,60 @@ enum nbcon_prio nbcon_get_default_prio(void)
[ ... ]
Quoted 12 lines
kernel/printk/nbcon.c +10
-1+static bool nbcon_allow_unsafe_takeover (struct console *con,enum pr_flush_reason reason) { -return panic_on_this_cpu () && panic_nbcon_allow_unsafe_takeover; +int unsafe_flags = con->unsafe_flush_flags; +bool in_panic =panic_on_this_cpu (); +bool allow = false; + +switch (reason) { +case PR_FLUSH_RUNTIME : +if (in_panic) { +if (unsafe_flags &NBCON_PANIC_PRINTK ) + allow = true;
kernel/printk/nbcon.c enum nbcon_prio nbcon_get_default_prio(void) · line 1481
Quoted 10 lines
kernel/printk/nbcon.c +10
-0+ + }else { +/* Only when the kthreads have been shutdown. */ +if ((unsafe_flags &NBCON_NORMAL_NOTHREAD ) && + !con->kthread && + system_state >SYSTEM_RUNNING ) { + allow = true; + } + } +break ;
[Severity: Medium]
Does system_state > SYSTEM_RUNNING also match SYSTEM_SUSPEND?
In enum system_states, SYSTEM_SUSPEND is ordered after SYSTEM_RUNNING, and con->kthread is NULL whenever have_boot_console is true (for example with keep_bootcon). If the system suspends or hibernates with no_console_suspend and a boot console registered, any runtime printk() while system_state == SYSTEM_SUSPEND will see !con->kthread && system_state > SYSTEM_RUNNING and allow unsafe takeovers and CON_NBCON_ATOMIC_UNSAFE callbacks during suspend. Checking for an explicit kthread-shutdown flag set by printk_kthreads_shutdown() (or excluding SYSTEM_SUSPEND) would avoid enabling unsafe flushing when the kernel is not shutting down.
Quoted 15 lines
kernel/printk/nbcon.c +15
-0+case PR_FLUSH_RESTART : +/* kexec, reboot */ + +if (in_panic) { +if (unsafe_flags &NBCON_PANIC_FLUSH ) + allow = true; + + }else { +/* Printing kthreads not reliable. */ +if (unsafe_flags &NBCON_NORMAL_NOTHREAD ) + allow = true; + } +break ; +case PR_FLUSH_HANG : +/* Always unsafe flush before hang. */
kernel/printk/nbcon.c enum nbcon_prio nbcon_get_default_prio(void) · line 1506
Quoted 10 lines
kernel/printk/nbcon.c +9
-0+ allow = true; +break ; +case PR_FLUSH_KDB : +if (unsafe_flags &NBCON_KDB ) + allow = true; +break ; + } + +return allow; }
[Severity: Medium]
Can non-panic contexts where nbcon_allow_unsafe_takeover() returns true actually perform a hostile takeover when the console is already owned?
For PR_FLUSH_RUNTIME after kthreads are shut down, PR_FLUSH_RESTART outside an emergency section, PR_FLUSH_HANG outside panic (e.g. machine_halt()), and PR_FLUSH_KDB via nbcon_kdb_release(), nbcon_get_default_prio() sets ctxt->prio to NBCON_PRIO_NORMAL. When the console is already owned (cur->prio >= NBCON_PRIO_NORMAL), nbcon_context_try_acquire_direct() returns -EPERM at the priority check:
kernel/printk/nbcon.c:nbcon_context_try_acquire_direct() {
...
if (ctxt->prio <= cur->prio || ctxt->prio <= cur->req_prio)
return -EPERM;
if (cur->unsafe)
return -EBUSY;
...
}nbcon_context_try_acquire() then aborts on err != -EBUSY before reaching nbcon_context_try_acquire_hostile(). Even at NBCON_PRIO_EMERGENCY (such as in nbcon_kdb_try_acquire() or emergency_restart()), a hostile takeover is rejected with -EPERM if the console is held by another EMERGENCY or PANIC owner (for example when entering KDB from kgdb_panic()), and if KDB runs on a non-panic CPU during panic while cur->unsafe is set, nbcon_context_try_acquire_handover() unconditionally returns -EPERM on panic_on_other_cpu() without checking kdb_printf_on_this_cpu().
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
John Ogness
maintainerPATCH v2 4/6 printk: Remove nbcon_atomic_flush_unsafe()
In reply to John OgnessThere is no need for the special function nbcon_atomic_flush_unsafe(), which is only called at the end of panic(). Its functionality is replaced with printk_trigger_flush(PR_FLUSH_HANG).
--- include/linux/printk.h | 5 ----- kernel/panic.c | 2 +- kernel/printk/nbcon.c | 12 ------------ 3 files changed, 1 insertion(+), 18 deletions(-)
include/linux/printk.h +0 -5
@@ -225,7 +225,6 @@ void console_try_replay_all(void);void printk_legacy_allow_panic_sync (void );extern bool nbcon_device_try_acquire (struct console *con);extern void nbcon_device_release (struct console *con); -void nbcon_atomic_flush_unsafe (void );bool pr_flush (int timeout_ms,bool reset_on_progress);#else static inline __printf(1, 0)
@@ -336,10 +335,6 @@ static inline void nbcon_device_release(struct console *con) { } -static inline void nbcon_atomic_flush_unsafe (void ) -{ -} -static inline bool pr_flush (int timeout_ms,bool reset_on_progress) {return true;
kernel/panic.c +1 -1
@@ -765,7 +765,7 @@ void vpanic(const char *fmt, va_list args)* Explicitly flush the kernel log buffer one last time. */ console_flush_on_panic (CONSOLE_FLUSH_PENDING ,PR_FLUSH_HANG ); -nbcon_atomic_flush_unsafe (); +printk_trigger_flush (PR_FLUSH_HANG );local_irq_enable ();for (i =0 ; ; i +=PANIC_TIMER_STEP ) {
kernel/printk/nbcon.c +0 -12
@@ -1743,18 +1743,6 @@ void nbcon_atomic_flush_pending(enum pr_flush_reason reason)__nbcon_atomic_flush_pending (reason,prb_next_reserve_seq (prb)); } -/** -* nbcon_atomic_flush_unsafe - Flush all nbcon consoles using their -* write_atomic() callback and allowing unsafe hostile takeovers -* -* Flush the backlog up through the currently newest record. Unsafe hostile -* takeovers will be performed, if necessary. -*/ -void nbcon_atomic_flush_unsafe (void ) -{ -__nbcon_atomic_flush_pending (PR_FLUSH_HANG ,prb_next_reserve_seq (prb)); -} -/** * nbcon_cpu_emergency_enter - Enter an emergency section where printk() * messages for that CPU are flushed directly
Signature · 1 line
2.47.3
John Ogness
maintainerPATCH v2 5/6 printk: Add panic+kexec flush point
In reply to John OgnessDuring panic, nbcon consoles transition to atomic printing. However, there are consoles that do not support safe atomic printing (CON_NBCON_ATOMIC_UNSAFE). Such consoles will only flush during panic if their unsafe flush policy allows them to do so.
The unsafe flush policy NBCON_PANIC_FLUSH only allows unsafe flushing when particular flushes are explicitly invoked. Call printk_trigger_flush(PR_FLUSH_RESTART) before performing the kexec in order to give such consoles a chance to perform an unsafe flush.
--- kernel/crash_core.c | 1 + 1 file changed, 1 insertion(+)
kernel/crash_core.c +1 -0
@@ -140,6 +140,7 @@ void __noclone __crash_kexec(struct pt_regs *regs)crash_setup_regs (&fixed_regs, regs);crash_save_vmcoreinfo (); +printk_trigger_flush (PR_FLUSH_RESTART );machine_crash_shutdown (&fixed_regs);crash_cma_clear_pending_dma ();machine_kexec (kexec_crash_image);
Signature · 1 line
2.47.3
[email protected]
AI reviewRe: [PATCH printk v2 5/6] printk: Add panic+kexec flush point
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] crash: printk_trigger_flush() in __crash_kexec() called with wrong panic_cpu --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] crash: printk_trigger_flush() in __crash_kexec() called with wrong panic_cpu
--
commit fef7b57cf45508b52c2322cc5354dee7d0cedbc1
Author: John Ogness <[email protected]>
printk: Add panic+kexec flush point
This commit adds a call to printk_trigger_flush() in __crash_kexec() to allow consoles with the panic flush policy to flush before booting into the crash kernel.
kernel/crash_core.c void __noclone __crash_kexec(struct pt_regs *regs) · line 143
Quoted 9 lines
kernel/crash_core.c +1
-0@@ -140,6 +140,7 @@ void __noclone __crash_kexec(struct pt_regs *regs)crash_setup_regs (&fixed_regs, regs);crash_save_vmcoreinfo (); +printk_trigger_flush (PR_FLUSH_RESTART );
[Severity: Medium]
Can printk_trigger_flush() be called here on CPU0 while panic_cpu still holds a non-zero CPU number or its invalid initial value?
On Hyper-V root partitions, crash_nmi_callback() parks all non-zero CPUs when a panic occurs on another CPU (or when the hypervisor itself crashes with panic_cpu unset), while CPU0 disables the hypervisor and calls hv_crash_handle() -> __crash_kexec() without updating panic_cpu:
arch/x86/hyperv/hv_crash.c:hv_crash_handle() {
...
/* we are now fully in devirtualized normal kernel mode */
__crash_kexec(NULL);
}Because panic_on_this_cpu() is false on CPU0, nbcon_get_default_prio() returns normal priority instead of panic priority. printk_trigger_flush() then skips atomic panic flushing and takes the offload path via defer_console_output() -> irq_work_queue(). When the local tick is stopped, raising the self-interrupt calls hv_send_ipi_self(), which invokes a hypercall after the hypervisor has already been disabled, faulting and aborting machine_kexec().
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
John Ogness
maintainerPATCH v2 6/6 netconsole: Always perform unsafe flushing on panic/kdb/restart
In reply to John OgnessAs a legacy console the netconsole would perform unsafe flushing during panic/kdb/restart. With commit 7eab73b18630 ("netconsole:
convert to NBCON console infrastructure") the netconsole is now an NBCON console, however it uses the CON_NBCON_ATOMIC_UNSAFE flag and this prevents unsafe flushing except for at the very end of panic().
Enable unsafe flushing policies to allow the netconsole to perform unsafe flushing during all panic/kdb/restart variations, just as it did when it was a legacy console.
--- drivers/net/netconsole.c | 4 ++++ 1 file changed, 4 insertions(+)
drivers/net/netconsole.c +4 -0
@@ -2695,6 +2695,8 @@ static void free_param_target(struct netconsole_target *nt)static struct console netconsole_ext = { .name ="netcon_ext" , .flags =CON_ENABLED |CON_EXTENDED |CON_NBCON |CON_NBCON_ATOMIC_UNSAFE , + .unsafe_flush_flags =NBCON_PANIC_FLUSH |NBCON_PANIC_PRINTK |NBCON_KDB | +NBCON_NORMAL_NOTHREAD , .write_thread = netconsole_write_ext, .write_atomic = netconsole_write_ext, .device_lock = netconsole_device_lock,
@@ -2704,6 +2706,8 @@ static struct console netconsole_ext = {static struct console netconsole = { .name ="netcon" , .flags =CON_ENABLED |CON_NBCON |CON_NBCON_ATOMIC_UNSAFE , + .unsafe_flush_flags =NBCON_PANIC_FLUSH |NBCON_PANIC_PRINTK |NBCON_KDB | +NBCON_NORMAL_NOTHREAD , .write_thread = netconsole_write_basic, .write_atomic = netconsole_write_basic, .device_lock = netconsole_device_lock,
Signature · 1 line
2.47.3
[email protected]
AI reviewRe: [PATCH printk v2 6/6] netconsole: Always perform unsafe flushing on panic/kdb/restart
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [High] netconsole: lockless target_list traversal in netconsole_write() - [Low] netconsole: missing Fixes…
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [High] netconsole: lockless target_list traversal in netconsole_write()
- [Low] netconsole: missing Fixes trailer for commit 7eab73b18630 --
commit fe1bd6625625f06f022158329127266eb9d033ce
Author: John Ogness <[email protected]>
Quoted 8 lines
netconsole: Always perform unsafe flushing on panic/kdb/restart
As a legacy console the netconsole would perform unsafe flushing during panic/kdb/restart. With commit
7eab73b18630("netconsole:
convert to NBCON console infrastructure") the netconsole is now an NBCON console, however it uses theCON_NBCON_ATOMIC_UNSAFEflag and this prevents unsafe flushing except for at the very end ofpanic().
Quoted 5 lines
Enable unsafe flushing policies to allow the netconsole to perform unsafe flushing during all panic/kdb/restart variations, just as it did when it was a legacy console.
Signed-off-by John Ogness <[email protected]>
[Severity: Low]
Since this fixes a regression from the NBCON conversion, should it include:
Quoted 14 lines
drivers/net/netconsole.c +2
-0@@ -2695,6 +2695,8 @@ static void free_param_target(struct netconsole_target *nt)static struct console netconsole_ext = { .name ="netcon_ext" , .flags =CON_ENABLED |CON_EXTENDED |CON_NBCON |CON_NBCON_ATOMIC_UNSAFE , + .unsafe_flush_flags =NBCON_PANIC_FLUSH |NBCON_PANIC_PRINTK |NBCON_KDB | +NBCON_NORMAL_NOTHREAD , .write_thread = netconsole_write_ext, .write_atomic = netconsole_write_ext, .device_lock = netconsole_device_lock,
drivers/net/netconsole.c static struct console netconsole_ext = { · line 2713
Quoted 8 lines
drivers/net/netconsole.c +2
-0@@ -2704,6 +2706,8 @@ static struct console netconsole_ext = {static struct console netconsole = { .name ="netcon" , .flags =CON_ENABLED |CON_NBCON |CON_NBCON_ATOMIC_UNSAFE , + .unsafe_flush_flags =NBCON_PANIC_FLUSH |NBCON_PANIC_PRINTK |NBCON_KDB | +NBCON_NORMAL_NOTHREAD , .write_thread = netconsole_write_basic, .write_atomic = netconsole_write_basic, .device_lock = netconsole_device_lock,
[Severity: High]
Can enabling these unsafe flush flags cause netconsole_write() to race with target_list modifications on another CPU?
nbcon_emit_one() only calls con->device_lock() (which acquires target_list_lock) for the threaded path; when use_atomic is true, it invokes con->write_atomic() without holding target_list_lock. Previously, CON_NBCON_ATOMIC_UNSAFE restricted write_atomic() to the end of vpanic() after panic_other_cpus_shutdown() had already stopped other CPUs. With NBCON_PANIC_PRINTK, NBCON_PANIC_FLUSH, and NBCON_NORMAL_NOTHREAD set, write_atomic() runs during early vpanic() before other CPUs are stopped, as well as during emergency_restart() and post-printk_kthreads_shutdown() reboot/halt while other CPUs are still active:
CPU0 CPU1vpanic() / kernel_restart()
...
nbcon_emit_one(..., true)
netconsole_write()
list_for_each_entry(nt,
&target_list, list)
drop_netconsole_target()
spin_lock_irqsave(&target_list_lock)
list_del(&nt->list)
spin_unlock_irqrestore(...)
...
netpoll_cleanup(&nt->np)
np->dev = NULL
config_item_put(&nt->item)
kfree(nt)
netif_running(nt->np.dev) <-- NULL deref / use-after-freeBecause netconsole_write() traverses target_list with non-RCU list_for_each_entry() and dereferences nt->np.dev without target_list_lock, a concurrent target removal or disable in drop_netconsole_target(), enabled_store(), or netconsole_netdev_event() can cause a NULL pointer dereference, use-after-free, or LIST_POISON dereference during panic or reboot flushing.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6