* [PATCH printk v3 0/7] Introduce policies for unsafe flushing
@ 2026-11-18 23:03 John Ogness
2026-11-18 23:03 ` [PATCH printk v3 6/7] printk: Add unsafe flush points for panic+kexec and emergency_restart John Ogness
0 siblings, 1 reply; 3+ messages in thread
From: John Ogness @ 2026-11-18 23:03 UTC (permalink / raw)
To: Petr Mladek
Cc: Sergey Senozhatsky, Steven Rostedt, Ryan Roberts, Breno Leitao,
printk, linux-kernel, Andrew Morton, Rio, Danilo Krummrich,
Aaron Tomlin, Greg Kroah-Hartman, Andy Shevchenko,
Alessandro Carminati, Pnina Feder, Gal Pressman, Baoquan He,
Mike Rapoport, Pasha Tatashin, Pratyush Yadav, Dave Young,
Bradley Morgan, Oleg Nesterov, Pavel Tikhomirov, Petr Pavlu,
Cen Zhang (Microsoft), kexec, Gustavo Luiz Duarte, Andrew Lunn,
David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
netdev
v2 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 7/7. 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.
For this version I reduced the unsafe flush policies to the
minimum required by the netconsole: panic+restart and
emergency+restart. I also keep nbcon_atomic_flush_unsafe() and
use only this function for specifying the flush reasons. This
allows us to pinpoint where unsafe flushing is evaluated and,
as a nice bonus, to avoid all the legacy madness.
The first 4 patches have no functional changes. Hopefully they
are each small enough that a review is relatively trivial. The
functional changes (5-7) are also compact.
Changes since v2:
- Specify flush reason in nbcon_atomic_flush_unsafe() instead
of console_flush_on_panic() and printk_trigger_flush(). This
automatically avoids the need for any legacy handling.
- Remove KDB, PRINTK, NOTHREAD unsafe flush policies. (Not
needed for now.)
- Add EMERGENCY_RESTART unsafe flush policy to be used for
emergency_restart() when non-panic.
- Rename console.unsafe_flush_flags to
console.allow_unsafe_policy.
- Change console_is_usable() to _never_ report
CON_NBCON_ATOMIC_UNSAFE as usable.
- Introduce nbcon_is_unsafe_usable() as a variant to
console_is_usable() to report CON_NBCON_ATOMIC_UNSAFE as
usable. The caller decides if unsafe printing should occur.
This is only used for nbcon+atomic flushing.
- Perform unsafe flush policy check higher up in
__nbcon_atomic_flush_pending() instead of
__nbcon_atomic_flush_pending_con().
John Ogness
[0] https://lore.kernel.org/lkml/20261111181942.593695-1-john.ogness@linutronix.de
[1] https://lore.kernel.org/lkml/20260915100140.3631650-1-ryan.roberts@arm.com
John Ogness (7):
printk: Introduce and specify flush reasons
printk: nbcon: Move unsafe takeover check to higher level
printk: Move unsafe takeover check out of console_is_usable()
printk: Use @reason instead of global variable to check flush policy
console: Support unsafe flushing for panic/emergency restarts
printk: Add unsafe flush points for panic+kexec and emergency_restart
netconsole: Perform unsafe flushing on panic and emergency restart
drivers/net/netconsole.c | 2 +
include/linux/console.h | 60 ++++++++++++++++++++------
include/linux/printk.h | 20 ++++++++-
kernel/crash_core.c | 1 +
kernel/panic.c | 2 +-
kernel/printk/nbcon.c | 91 +++++++++++++++++++++++++++-------------
kernel/reboot.c | 1 +
7 files changed, 131 insertions(+), 46 deletions(-)
base-commit: ef19063ad84f8ed58ee44ebd49ba035fd5159e74
--
2.47.3
^ permalink raw reply [flat|nested] 3+ messages in thread
* [PATCH printk v3 6/7] printk: Add unsafe flush points for panic+kexec and emergency_restart
2026-11-18 23:03 [PATCH printk v3 0/7] Introduce policies for unsafe flushing John Ogness
@ 2026-11-18 23:03 ` John Ogness
2026-11-19 23:03 ` sashiko-bot
0 siblings, 1 reply; 3+ messages in thread
From: John Ogness @ 2026-11-18 23:03 UTC (permalink / raw)
To: Petr Mladek
Cc: Sergey Senozhatsky, Steven Rostedt, Ryan Roberts, Breno Leitao,
printk, linux-kernel, Andrew Morton, Baoquan He, Mike Rapoport,
Pasha Tatashin, Pratyush Yadav, Dave Young, Bradley Morgan,
Oleg Nesterov, Pavel Tikhomirov, Petr Pavlu,
Cen Zhang (Microsoft), kexec
During emergency and 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 when an unsafe
flush is explicitly invoked and their unsafe policy permits unsafe
flushing for that context.
Call nbcon_atomic_flush_unsafe(PR_FLUSH_RESTART) before performing kexec
and emergency restarts in order to allow such consoles to perform an
unsafe flush (if their unsafe policy permits).
Signed-off-by: John Ogness <john.ogness@linutronix.de>
---
kernel/crash_core.c | 1 +
kernel/reboot.c | 1 +
2 files changed, 2 insertions(+)
diff --git a/kernel/crash_core.c b/kernel/crash_core.c
index d0bd2d0cf8990..79a67956743ac 100644
--- a/kernel/crash_core.c
+++ b/kernel/crash_core.c
@@ -140,6 +140,7 @@ void __noclone __crash_kexec(struct pt_regs *regs)
crash_setup_regs(&fixed_regs, regs);
crash_save_vmcoreinfo();
+ nbcon_atomic_flush_unsafe(PR_FLUSH_RESTART);
machine_crash_shutdown(&fixed_regs);
crash_cma_clear_pending_dma();
machine_kexec(kexec_crash_image);
diff --git a/kernel/reboot.c b/kernel/reboot.c
index a93c23b9e1948..b26d131c54151 100644
--- a/kernel/reboot.c
+++ b/kernel/reboot.c
@@ -102,6 +102,7 @@ void emergency_restart(void)
*/
nbcon_cpu_emergency_enter();
printk_trigger_flush();
+ nbcon_atomic_flush_unsafe(PR_FLUSH_RESTART);
machine_emergency_restart();
}
--
2.47.3
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH printk v3 6/7] printk: Add unsafe flush points for panic+kexec and emergency_restart
2026-11-18 23:03 ` [PATCH printk v3 6/7] printk: Add unsafe flush points for panic+kexec and emergency_restart John Ogness
@ 2026-11-19 23:03 ` sashiko-bot
0 siblings, 0 replies; 3+ messages in thread
From: sashiko-bot @ 2026-11-19 23:03 UTC (permalink / raw)
To: John Ogness, Petr Mladek
Cc: Sergey Senozhatsky, Steven Rostedt, Ryan Roberts, Breno Leitao,
printk, Andrew Morton, Baoquan He, Mike Rapoport, Pasha Tatashin,
Pratyush Yadav, Dave Young, Bradley Morgan, Oleg Nesterov,
Pavel Tikhomirov, Petr Pavlu, Cen Zhang (Microsoft), kexec
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] printk: nbcon: con->pbufs corruption on hostile takeover in
emergency_restart()
--
commit bc98e7fe84227c1b12eaff8bf1021023be510742
Author: John Ogness <john.ogness@linutronix.de>
printk: Add unsafe flush points for panic+kexec and emergency_restart
This commit adds nbcon_atomic_flush_unsafe(PR_FLUSH_RESTART) calls to
__crash_kexec() and emergency_restart() so that CON_NBCON_ATOMIC_UNSAFE
consoles can flush when permitted by their unsafe policy.
> diff --git a/kernel/reboot.c b/kernel/reboot.c
> index a93c23b9e1948..b26d131c54151 100644
> --- a/kernel/reboot.c
> +++ b/kernel/reboot.c
> @@ -102,6 +102,7 @@ void emergency_restart(void)
> */
> nbcon_cpu_emergency_enter();
> printk_trigger_flush();
> + nbcon_atomic_flush_unsafe(PR_FLUSH_RESTART);
>
> machine_emergency_restart();
> }
[Severity: Medium]
Can this corrupt con->pbufs when emergency_restart() performs a hostile
takeover outside of panic()?
When emergency_restart() is invoked directly (for example via SysRq-b)
rather than from panic(), panic_on_this_cpu() is false and other CPUs remain
active. If a console permits unsafe takeover on PR_FLUSH_RESTART (such as
netconsole with NBCON_EMERGENCY_RESTART_FLUSH) while an NBCON_PRIO_NORMAL
context holds it inside an unsafe section, nbcon_context_try_acquire()
performs an unsafe hostile takeover at NBCON_PRIO_EMERGENCY but still
assigns con->pbufs instead of panic_nbcon_pbufs:
kernel/printk/nbcon.c:nbcon_context_try_acquire() {
...
/* Assign the appropriate buffer for this context. */
if (panic_on_this_cpu())
ctxt->pbufs = &panic_nbcon_pbufs;
else
ctxt->pbufs = con->pbufs;
...
}
CPU0 (NBCON_PRIO_NORMAL) CPU1 (emergency_restart)
nbcon_emit_next_record()
nbcon_context_enter_unsafe()
printk_get_next_message()
// writes con->pbufs->outbuf
nbcon_atomic_flush_unsafe(PR_FLUSH_RESTART)
...
nbcon_context_try_acquire_hostile()
ctxt->pbufs = con->pbufs
nbcon_emit_next_record()
printk_get_next_message()
// overwrites con->pbufs->outbuf
con->write_thread()
// reads corrupted con->pbufs->outbuf <-- buffer corruption
Both contexts then concurrently read and write con->pbufs->outbuf and
con->pbufs->scratchbuf. For netconsole_ext, overwriting the formatted
message while send_msg_fragmented() is scanning wctxt->outbuf for the ';'
header delimiter can trigger WARN_ON_ONCE(!msgbody) or match a ';' past
MAX_PRINT_CHUNK, overflowing nt->buf[].
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261118230345.808560-1-john.ogness@linutronix.de?part=6
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-11-19 23:04 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-18 23:03 [PATCH printk v3 0/7] Introduce policies for unsafe flushing John Ogness
2026-11-18 23:03 ` [PATCH printk v3 6/7] printk: Add unsafe flush points for panic+kexec and emergency_restart John Ogness
2026-11-19 23:03 ` sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox