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

From: John Ogness

Date: Sat Nov 21 2026 - 11:36:55 EST


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.

Signed-off-by: John Ogness <john.ogness@xxxxxxxxxxxxx>
---
kernel/printk/nbcon.c | 30 ++++++++++++++++++++++--------
1 file changed, 22 insertions(+), 8 deletions(-)

diff --git a/kernel/printk/nbcon.c b/kernel/printk/nbcon.c
index c3307b5b2c922..8b7483bd57188 100644
--- a/kernel/printk/nbcon.c
+++ b/kernel/printk/nbcon.c
@@ -557,7 +557,13 @@ static int nbcon_context_try_acquire_hostile(struct nbcon_context *ctxt,
if (!ctxt->allow_unsafe_takeover)
return -EPERM;

- /* Ensure caller is allowed to perform unsafe hostile takeovers. */
+ /*
+ * Ensure caller is allowed to perform unsafe hostile takeovers.
+ * It is deeply hard-coded that hostile takeovers must only occur
+ * on the panic CPU. For example, the single dedicated buffer
+ * @panic_nbcon_pbufs and the wide use of panic_on_other_cpu() to
+ * satisfy ownership and waiter rules.
+ */
if (WARN_ON_ONCE(ctxt->prio != NBCON_PRIO_PANIC))
return -EPERM;

@@ -1454,15 +1460,15 @@ enum nbcon_prio nbcon_get_default_prio(void)
}

/**
- * nbcon_allow_unsafe_takeover - Check if unsafe console takeovers are allowed
- * for a specified console and reason
+ * nbcon_allow_unsafe - Check if unsafe console printing is allowed for a
+ * specified console and reason
*
* @con: The console to check
- * @reason: The reason for the unsafe takeover
+ * @reason: The reason for the unsafe console printing
*
* Return: True, when @con is allowed to perform unsafe console printing
*/
-static bool nbcon_allow_unsafe_takeover(struct console *con, enum pr_flush_reason reason)
+static bool nbcon_allow_unsafe(struct console *con, enum pr_flush_reason reason)
{
enum nbcon_prio prio = nbcon_get_default_prio();
bool allow = false;
@@ -1670,6 +1676,7 @@ static void __nbcon_atomic_flush_pending(enum pr_flush_reason reason, u64 stop_s
{
bool allow_unsafe_takeover;
struct console *con;
+ bool allow_unsafe;
int cookie;

cookie = console_srcu_read_lock();
@@ -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();
+
nbcon_atomic_flush_pending_con(con, stop_seq, allow_unsafe_takeover);
}
console_srcu_read_unlock(cookie);
--
2.47.3