Re: [PATCH printk v4 3/8] printk: Move unsafe takeover check out of console_is_usable()

From: Petr Mladek

Date: Tue Nov 24 2026 - 11:14:54 EST


On Sat 2026-11-21 17:41:51, John Ogness wrote:
> Change console_is_usable() to never claim a console with
> CON_NBCON_ATOMIC_UNSAFE is usable since that is the normal use
> case. This allows moving nbcon_allow_unsafe_takeover() out of
> console_is_usable().
>
> To support unsafe atomic flushing, provide a variant of
> console_is_usable() called nbcon_is_unsafe_usable() that accepts
> CON_NBCON_ATOMIC_UNSAFE consoles as usable. However, this adds
> responsibility to the caller to determine if unsafe atomic printing
> should occur.
>
> __nbcon_atomic_flush_pending() is the only function that enables
> unsafe atomic printing by calling nbcon_allow_unsafe_takeover().
> Rather that using console_is_usable() to select atomic printing
> consoles, use nbcon_is_unsafe_usable() and only select
> CON_NBCON_ATOMIC_UNSAFE consoles if the unsafe takeover is allowed.
>
> These changes effectively move the nbcon_allow_unsafe_takeover()
> check from console_is_usable() to __nbcon_atomic_flush_pending().
> This has no external functional change. Internally within
> __nbcon_atomic_flush_pending() it means that
> nbcon_allow_unsafe_takeover() is evaluated after the usable check
> rather than during. The result is the same.
>
> Since __nbcon_atomic_flush_pending() also knows the @reason for
> the flush, this opens the door for the unsafe atomic flushing
> policy to be enforced in __nbcon_atomic_flush_pending().
>
> --- a/include/linux/console.h
> +++ b/include/linux/console.h
> @@ -625,13 +624,8 @@ static inline bool nbcon_is_braille(struct console *con)
> return con && (con->flags & CON_BRL);
> }
>
> -/*
> - * Check if the given console is currently capable and allowed to print
> - * records. Note that this function does not consider the current context,
> - * which can also play a role in deciding if @con can be used to print
> - * records.
> - */
> -static inline bool console_is_usable(struct console *con, short flags, bool use_atomic)
> +static inline bool __console_is_usable(struct console *con, short flags, bool use_atomic,
> + bool allow_atomic_unsafe)
> {
> if (!(flags & CON_ENABLED))
> return false;
> @@ -645,11 +639,7 @@ static inline bool console_is_usable(struct console *con, short flags, bool use_
> 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())
> + if (!allow_atomic_unsafe && (flags & CON_NBCON_ATOMIC_UNSAFE))

This is a good example that different people have different mental mode and
prefers another ordering of the checks ;-)

> return false;
> }
>
> @@ -675,6 +665,29 @@ static inline bool console_is_usable(struct console *con, short flags, bool use_
> return true;
> }
>
> +/*
> + * Check if the given console is currently capable and allowed to print
> + * records. Note that this function does not consider the current context,
> + * which can also play a role in deciding if @con can be used to print
> + * records.
> + */
> +static inline bool console_is_usable(struct console *con, short flags, bool use_atomic)
> +{
> + return __console_is_usable(con, flags, use_atomic, false);
> +}
> +
> +/*
> + * Similar to console_is_usable(), but allows atomic unsafe consoles. This is
> + * only interesting for nbcon unsafe atomic printing.
> + *
> + * Note: It is the responsibility of the caller to decide if unsafe printing
> + * should actually occur.
> + */
> +static inline bool nbcon_is_unsafe_usable(struct console *con, short flags)
> +{
> + return __console_is_usable(con, flags, true, true);
> +}
> +
> #else
> static inline void nbcon_cpu_emergency_enter(void) { }
> static inline void nbcon_cpu_emergency_exit(void) { }
> --- a/kernel/printk/nbcon.c
> +++ b/kernel/printk/nbcon.c
> @@ -1468,7 +1468,7 @@ static bool panic_nbcon_allow_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).
> */
> -bool nbcon_allow_unsafe_takeover(void)
> +static bool nbcon_allow_unsafe_takeover(void)
> {
> return panic_on_this_cpu() && panic_nbcon_allow_unsafe_takeover;
> }
> @@ -1672,11 +1672,24 @@ static void __nbcon_atomic_flush_pending(enum pr_flush_reason reason, u64 stop_s
> if (!(flags & CON_NBCON))
> continue;
>
> - if (!console_is_usable(con, flags, true))
> + /*
> + * Using variant of console_is_usable() to include consoles
> + * with unsafe write_atomic() implementations. They might be
> + * allowed depending on nbcon_allow_unsafe_takeover().
> + */
> + if (!nbcon_is_unsafe_usable(con, flags))
> continue;

The above function pretends that an unsafe takeover is enabled.


> allow_unsafe_takeover = nbcon_allow_unsafe_takeover();

Here we get the information whether it is actually allowed.

> + /*
> + * Since nbcon_is_unsafe_usable() was used, ensure @con does
> + * not have an unsafe write_atomic() if this context expects
> + * safe printing.
> + */
> + if ((flags & CON_NBCON_ATOMIC_UNSAFE) && !allow_unsafe_takeover)
> + continue;

Here we duplicate the check from __console_is_usable() using the updated
information.

It is a bit hairy. It might be better to use the up-to-date
information in console_is_usable*().

I suggest to rename __console_is_usable() -> console_is_usable_unsafe().
It would take 4 parameters, including @allow_unsafe_takeover.

It would be used to implement the most commonly used variant:

/* Variant used where the unsafe_takeover is never allowed. */
static inline bool console_is_usable(struct console *con, short flags, bool use_atomic)
{
return console_is_usable_unsafe(con, flags, use_atomic, false);
}

And this would be the only location where the longer variant is needed:

/* unsafe_takeover might be allowed in this code path. */
allow_unsafe_takeover = nbcon_allow_unsafe_takeover();
if (!nbcon_is_usable_unsafe(con, flags, true, allow_unsafe_takeover))
continue;

> +
> if (nbcon_seq_read(con) >= stop_seq)
> continue;

Note that I have not seen the rest of the patchset yet. It is possible
that I miss something.

Best Regards,
Petr