Re: [PATCH printk v4 4/8] printk: nbcon: Use @reason instead of global variable to check flush policy
From: Petr Mladek
Date: Tue Nov 24 2026 - 11:28:24 EST
On Sat 2026-11-21 17:41:52, John Ogness wrote:
> Currently 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.
>
> Since at the end of panic() nbcon_atomic_flush_unsafe() is called with
> the @reason PR_FLUSH_HANG, it is possible to identify when unsafe
> atomic flushing is allowed using:
>
> PR_FLUSH_HANG && panic_on_this_cpu()
>
> rather than:
>
> panic_nbcon_allow_unsafe_takeover && panic_on_this_cpu()
>
> This allows removing the global variable hack and opens the door for
> unsafe flushing to be allowed in other contexts.
>
> Provide nbcon_allow_unsafe_takeover() the flush @reason to implement
> the check.
>
> No functional change.
>
> --- a/kernel/printk/nbcon.c
> +++ b/kernel/printk/nbcon.c
> @@ -1453,24 +1453,31 @@ enum nbcon_prio nbcon_get_default_prio(void)
> return NBCON_PRIO_NORMAL;
> }
>
> -/*
> - * Track if it is allowed to perform unsafe hostile takeovers of console
> - * ownership. When true, console drivers might perform unsafe actions while
> - * printing. It is externally available via nbcon_allow_unsafe_takeover().
> - */
> -static bool panic_nbcon_allow_unsafe_takeover;
> -
> /**
> * nbcon_allow_unsafe_takeover - Check if unsafe console takeovers are allowed
> + * for a specified console and reason
> *
> - * Return: True, when it is permitted to perform unsafe console printing
> + * @con: The console to check
> + * @reason: The reason for the unsafe takeover
> *
> - * This is also used by console_is_usable() to determine if it is allowed to
> - * call write_atomic() callbacks flagged as unsafe (CON_NBCON_ATOMIC_UNSAFE).
> + * Return: True, when @con is allowed to perform unsafe console printing
> */
> -static bool nbcon_allow_unsafe_takeover(void)
> +static bool nbcon_allow_unsafe_takeover(struct console *con, enum pr_flush_reason reason)
Nit: It seems that the @con parameter is not used in this version.
So, it might trigger a warning during bisection.
> {
> - return panic_on_this_cpu() && panic_nbcon_allow_unsafe_takeover;
> + enum nbcon_prio prio = nbcon_get_default_prio();
> + bool allow = false;
> +
> + switch (reason) {
> + case PR_FLUSH_RUNTIME:
> + /* Never unsafe flush during runtime. */
> + break;
> + case PR_FLUSH_HANG:
> + /* Always unsafe flush on panic hang. */
> + allow = (prio == NBCON_PRIO_PANIC);
> + break;
> + }
> +
> + return allow;
> }
Otherwise, the logic looks correct.
Best Regards,
Petr