Re: [PATCH 1/2] printk: nbcon: Flush unsafe consoles from printk() in panic
From: John Ogness
Date: Thu Nov 05 2026 - 09:40:38 EST
On 2026-11-02, Breno Leitao <leitao@xxxxxxxxxx> wrote:
> --- a/kernel/printk/nbcon.c
> +++ b/kernel/printk/nbcon.c
> @@ -1688,7 +1692,15 @@ static void __nbcon_atomic_flush_pending(u64 stop_seq)
> */
> void nbcon_atomic_flush_pending(void)
> {
> - __nbcon_atomic_flush_pending(prb_next_reserve_seq(prb));
> + u64 stop_seq = prb_next_reserve_seq(prb);
> +
> + __nbcon_atomic_flush_pending(stop_seq, false);
> +
> + if (panic_on_this_cpu() && oops_in_progress) {
> + panic_nbcon_allow_unsafe_takeover = true;
> + __nbcon_atomic_flush_pending(stop_seq, true);
> + panic_nbcon_allow_unsafe_takeover = false;
> + }
> }
>
> /**
This part of the patch gave me some ideas. First, I like the idea of
putting the policy handling code inside
nbcon_atomic_flush_pending(). This keeps the code outside of
kernel/printk fairly simple:
1. In emergency sections, printk() is calling
nbcon_atomic_flush_pending(), which always skips
CON_NBCON_ATOMIC_UNSAFE.
2. During panic, printk() is calling nbcon_atomic_flush_pending(), which
via policy could include CON_NBCON_ATOMIC_UNSAFE (in a 2nd internal
loop, after all safe atomic consoles are attempted).
3. After kthreads are shutdown, printk() is calling
nbcon_atomic_flush_pending(), which via policy could include
CON_NBCON_ATOMIC_UNSAFE (in a 2nd internal loop, after all safe atomic
consoles are attempted).
For the above situations, no new flushing calls are needed. But there is
another scenario for CON_NBCON_ATOMIC_UNSAFE (and unsafe printing in
general):
4. If the kernel ends in a way other than the final panic() loop, (for
example kexec, sysrq-r, panic+reboot), the above scenarios do not allow
unsafe printing. For the related code points we could call
nbcon_atomic_flush_unsafe() and it could handle the policy of unsafe
flushing depending on the context, which would allow
CON_NBCON_ATOMIC_UNSAFE to flush here as well.
The unsafe policy flags could be something like:
PANIC_PRINTK
- panic: all printk() calls during panic
- checked in nbcon_atomic_flush_pending() of #2
- if ((policy & PANIC_PRINTK) && panic_in_progress())
NORMAL_FLUSH
- non-panic: atomic flush calls: shutdown, reboot, kexec
- checked in nbcon_atomic_flush_pending() of #3
- checked in nbcon_atomic_flush_unsafe() of #4
- if ((policy & NORMAL_FLUSH) && !panic_in_progress())
PANIC_FLUSH
- panic: unsafe atomic flush calls: reboot, kexec
- checked in nbcon_atomic_flush_unsafe() of #4
- if ((policy & PANIC_FLUSH) && panic_in_progress())
Netconsole originally wanted by default:
.policy = NORMAL_FLUSH | PANIC_FLUSH,
But this last series shows that netconsole actually wants:
.policy = NORMAL_FLUSH | PANIC_FLUSH | PANIC_PRINTK,
These are slightly different policies. But as long as printk supports
all 3 policy flags, we do not care which policies are chosen.
I am happy to code up a full implementation if you think this has merit
or you want to see the patch to decide.
John