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

From: John Ogness

Date: Thu Nov 26 2026 - 04:51:28 EST


On 2026-11-24, Petr Mladek <pmladek@xxxxxxxx> wrote:
>> @@ -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 ;-)

The reason for the switch was because this function becomes a helper
that operates in 2 modes: with atomic_unsafe, without atomic_unsafe. So
I felt it was appropriate to make the "mode of operation" the primary
component of the condition. But since you suggest to move away from
making this a helper, I will switch it back.

>> --- 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.

This series establishes that:

unsafe takeover != unsafe usage

At this point in the series that distinction is not yet possible. I was
attempting to:

- keep patch sizes small
- have as many "no functional changes" steps as possible

I will see what I can do here. It might be easier after following your
suggestions.

>> 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.

Agreed. That is clean and fits the end of the series as well.

John