Re: [PATCH printk v4 6/8] console: Support unsafe flushing for panic/emergency restarts

From: Petr Mladek

Date: Wed Nov 25 2026 - 09:02:59 EST


On Sat 2026-11-21 17:41:54, John Ogness wrote:
> Introduce a new nbcon-specific console field @allow_unsafe_policy
> to store the unsafe flush policies for a console. The policies are
> represented by bits, specified by enum nbcon_unsafe_flush_flags.
>
> Define unsafe flush policies for:
>
> panic+restart: This is for kexec and reboot from panic.
>
> emergency+restart: This is for emergency_restart() when in
> the emergency state.
>
> Define a new flush type:
>
> PR_FLUSH_RESTART: For flushing before kexec and reboot.
>
> Implement the policy check within nbcon_allow_unsafe_takeover().
>
> No functional change.
>
> Signed-off-by: John Ogness <john.ogness@xxxxxxxxxxxxx>
> ---
> include/linux/console.h | 20 ++++++++++++++++++++
> include/linux/printk.h | 3 +++
> kernel/printk/nbcon.c | 7 +++++++
> 3 files changed, 30 insertions(+)
>
> diff --git a/include/linux/console.h b/include/linux/console.h
> index 227a7cf296f60..1afeb90692395 100644
> --- a/include/linux/console.h
> +++ b/include/linux/console.h
> @@ -194,6 +194,23 @@ enum cons_flags {
> CON_NBCON_ATOMIC_UNSAFE = BIT(9),
> };
>
> +/**
> + * enum nbcon_unsafe_flush_flags - Unsafe atomic flush policy flags
> + * @NBCON_PANIC_RESTART_FLUSH: In panic, unsafe flush before kexec
> + * or reboot.
> + * @NBCON_EMERGENCY_RESTART_FLUSH: Unsafe flush before emergency restart.
> + *
> + * 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_RESTART_FLUSH = BIT(0),
> + NBCON_EMERGENCY_RESTART_FLUSH = BIT(1),
> +};
>
> /**
> * struct nbcon_state - console state for nbcon consoles
> * @atom: Compound of the state fields for atomic operations
> @@ -326,6 +343,7 @@ struct nbcon_write_context {
> * @data: Driver private data
> * @node: hlist node for the console list
> *
> + * @allow_unsafe_policy: Unsafe atomic flush policy. See 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 +479,8 @@ struct console {
> */
> void (*device_unlock)(struct console *con, unsigned long flags);
>
> + unsigned int allow_unsafe_policy;
> +
> atomic_t __private nbcon_state;
> atomic_long_t __private nbcon_seq;
> struct nbcon_context __private nbcon_device_ctxt;
> diff --git a/include/linux/printk.h b/include/linux/printk.h
> index 68cf51db5f0ea..267a0a6fd23cf 100644
> --- a/include/linux/printk.h
> +++ b/include/linux/printk.h
> @@ -148,6 +148,8 @@ 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
> *
> * Flush reasons are used to determine if unsafe atomic flushing should be
> @@ -155,6 +157,7 @@ struct dev_printk_info;
> */
> enum pr_flush_reason {
> PR_FLUSH_RUNTIME,
> + PR_FLUSH_RESTART,
> PR_FLUSH_HANG,
> };
>
> diff --git a/kernel/printk/nbcon.c b/kernel/printk/nbcon.c
> index 8b7483bd57188..f1789cfbd8b57 100644
> --- a/kernel/printk/nbcon.c
> +++ b/kernel/printk/nbcon.c
> @@ -1471,12 +1471,19 @@ enum nbcon_prio nbcon_get_default_prio(void)
> static bool nbcon_allow_unsafe(struct console *con, enum pr_flush_reason reason)
> {
> enum nbcon_prio prio = nbcon_get_default_prio();
> + int unsafe_policy = con->allow_unsafe_policy;
> bool allow = false;
>
> switch (reason) {
> case PR_FLUSH_RUNTIME:
> /* Never unsafe flush during runtime. */
> break;
> + case PR_FLUSH_RESTART:
> + if (prio == NBCON_PRIO_PANIC)
> + allow = (unsafe_policy & NBCON_PANIC_RESTART_FLUSH);
> + else if (prio == NBCON_PRIO_EMERGENCY)
> + allow = (unsafe_policy & NBCON_EMERGENCY_RESTART_FLUSH);
> + break;

Many different flags are mixed in this function:

1. CON_NBCON_ATOMIC_UNSAFE from enum cons_flags
2. NBCON_PRIO_* from enum nbcon_prio
3. PR_FLUSH_* from enum pr_flush_reason
4. NBCON_*_FLUSH from enum nbcon_unsafe_flush_flags (flush policy)
7
It is easy to get lost. I wonder if we could do better. ;-)

Ad 1st: CON_NBCON_ATOMIC_UNSAFE is not ideal. But it is done. And
I am not sure how to make it better.

Ad 2nd: NBCON_PRIO matches nbcon_prio. It look good.

Ad 3rd: PR_FLUSH is not that good. It would be better to
emphasize that it is a "reason".

I would prefer to use the longer PR_FLUSH_REASON_ prefix.
It will be consistent with "enum pr_flush_reason" type name
and more clear.


Ad 4th: Both NBCON_*_FLUSH and enum nbcon_unsafe_flush_flags are far
from clear. And they are not consistent. Sigh, it would be
great to emphasize "unsafe", "flush" and "policy".

I think about:

enum nbcon_unsafe_flush_policy {
NBCON_UFP_PANIC_RESTART = BIT(0),
NBCON_UFP_EMERGENCY_RESTART = BIT(1),
};

or maybe even better

enum nbcon_unsafe_flush_policy {
NBCON_UF_POLICY_PANIC_RESTART = BIT(0),
NBCON_UF_POLICY_EMERGENCY_RESTART = BIT(1),
};

plus use:

struct console {
[...]
unsigned int unsafe_flush_policy;
[...]
}

and finally the local variable "uf_policy".

};

What do you think, please?

Otherwise, the patch looks good. Note that we do not need to check
here panic_on_this_cpu() because NBCON_PRIO_PANIC is used only on
the panic_cpu.

Best Regards,
Petr

> case PR_FLUSH_HANG:
> /* Always unsafe flush on panic hang. */
> allow = (prio == NBCON_PRIO_PANIC);
> --
> 2.47.3

> LocalWords: PRIO