Re: [PATCH printk v4 5/8] printk: nbcon: Expand unsafe takeover check for general unsafe

From: Petr Mladek

Date: Wed Nov 25 2026 - 05:22:52 EST


On Sat 2026-11-21 17:41:53, John Ogness wrote:
> Currently nbcon_allow_unsafe_takeover() is identifying if hostile
> takeovers are permitted.
>
> In order to support unsafe flush policies for CON_NBCON_ATOMIC_UNSAFE,
> there needs to be a distinction between atomic flushing using an
> unsafe implementation or atomic flushing via hostile takeover. The
> former could be allowed in non-panic, whereas the latter is only
> allowed on the panic CPU.
>
> Rename nbcon_allow_unsafe_takeover() to nbcon_allow_unsafe() and
> change its purpose to identify if unsafe console printing (in general)
> is allowed. If yes, hostile takeover can be allowed when on the panic
> CPU.
>
> The comments for the WARN verifying no hostile takeover attempts
> during non-panic have been expanded. The detailed description will
> hopefully remind us of the necessity of restricting hostile takeovers
> to the panic CPU.
>
> This opens the door for unsafe flushing (with safe ownership) for
> non-panic situations.
>
> No functional change.
>
> --- a/kernel/printk/nbcon.c
> +++ b/kernel/printk/nbcon.c
> @@ -1682,24 +1689,31 @@ static void __nbcon_atomic_flush_pending(enum pr_flush_reason reason, u64 stop_s
> /*
> * Using variant of console_is_usable() to include consoles
> * with unsafe write_atomic() implementations. They might be
> - * allowed depending on nbcon_allow_unsafe_takeover().
> + * allowed depending on nbcon_allow_unsafe().
> */
> if (!nbcon_is_unsafe_usable(con, flags))
> continue;
>
> - allow_unsafe_takeover = nbcon_allow_unsafe_takeover(con, reason);
> + allow_unsafe = nbcon_allow_unsafe(con, reason);
>
> /*
> * 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)
> + if ((flags & CON_NBCON_ATOMIC_UNSAFE) && !allow_unsafe)
> continue;
>
> if (nbcon_seq_read(con) >= stop_seq)
> continue;
>
> + /*
> + * Even if unsafe console printing is allowed, being able to
> + * perform a hostile takeover additionally requires this CPU
> + * to be in panic.
> + */
> + allow_unsafe_takeover = allow_unsafe && panic_on_this_cpu();

I was a bit confused by this. Also I think that it is error prone. For
example, it allows to use the unsafe_takeover for
CON_NBCON_ATOMIC_UNSAFE consoles anywhere in panic(). But it should
be allowed only for the final nbcon_atomic_flush_unsafe(PR_FLUSH_HANG).

We should make this decision on a single place. I suggest to rename
nbcon_allow_unsafe_takeover() to nbcon_check_unsafe() and pass
*allow_unsafe_takeover as a parameter. I mean something like:

/**
* nbcon_check_unsafe - Check if the console is safe or some unsafe handling
* is allowed for the given context and reason.
*
* @con: The console to check
* @reason: The reason for the unsafe console printing
* @allow_unsafe_takeover: Decision whether unsafe_takeover is allowed (output)
*
* The function decides when either an unsafe write_atomic() callback is
* allowed or when all NBCON consoles are allowed to used the unsafe_takeover.
*
* Return: True, when @con is either safe in all contexts or when it is
* allowed to perform unsafe console printing.
*
* In addition. the @allow_unsafe_takeover variable is set to "true"
* when the console is allowed to perform the unsafe_takeover. Otherwise,
* the variable is set to "false".
*/
static bool nbcon_check_unsafe(struct console *con, enum pr_flush_reason reason,
bool *allow_unsafe_takeover)
{
enum nbcon_prio prio = nbcon_get_default_prio();
bool ret = false;

*allow_unsafe_takeover = false;

switch (reason) {
case PR_FLUSH_RUNTIME:
/* Never unsafe flush during runtime. */
break;
case PR_FLUSH_HANG:
/* Always unsafe flush on panic hang. */
if ((prio == NBCON_PRIO_PANIC) && panic_on_this_cpu()) {
*allow_unsafe_takeover = true;
ret = true;
}
break;
}

return ret;
}

Then we could use the following in __nbcon_atomic_flush_pending():

if (!nbcon_check_unsafe(con, reason, &allow_unsafe_takeover))
continue;

Note 1: We might want to refactor the function when we add the checks
where to allow the unsafe write_atomic() callbacks.

Note 2: I thought about calling this directly in console_is_usable_unsafe().
But it would require passing the *allow_unsafe_takeover as a
parameter which is not needed in most situations when it is called
via the console_is_usable() wrapper. But it still might make
sense if we make the parameter optional (allow NULL).

> +

> nbcon_atomic_flush_pending_con(con, stop_seq, allow_unsafe_takeover);
> }
> console_srcu_read_unlock(cookie);

Best Regards,
Petr