mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: John Ogness <john.ogness@linutronix.de>
To: Petr Mladek <pmladek@suse.com>
Cc: Sergey Senozhatsky <senozhatsky@chromium.org>,
	Steven Rostedt <rostedt@goodmis.org>,
	Ryan Roberts <ryan.roberts@arm.com>,
	Breno Leitao <leitao@debian.org>,
	printk@lists.linux.dev, linux-kernel@vger.kernel.org
Subject: [PATCH printk v4 5/8] printk: nbcon: Expand unsafe takeover check for general unsafe
Date: Sat, 21 Nov 2026 17:41:53 +0106	[thread overview]
Message-ID: <20261121163557.963029-6-john.ogness@linutronix.de> (raw)
In-Reply-To: <20261121163557.963029-1-john.ogness@linutronix.de>

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@linutronix.de>
---
 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


  parent reply	other threads:[~2026-11-21 16:36 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-21 16:35 [PATCH printk v4 0/8] Introduce policies for unsafe flushing John Ogness
2026-11-21 16:35 ` [PATCH printk v4 1/8] printk: Introduce and specify flush reasons John Ogness
2026-11-21 16:35 ` [PATCH printk v4 2/8] printk: nbcon: Move unsafe takeover check to higher level John Ogness
2026-11-21 16:35 ` [PATCH printk v4 3/8] printk: Move unsafe takeover check out of console_is_usable() John Ogness
2026-11-21 16:35 ` [PATCH printk v4 4/8] printk: nbcon: Use @reason instead of global variable to check flush policy John Ogness
2026-11-21 16:35 ` John Ogness [this message]
2026-11-21 16:35 ` [PATCH printk v4 6/8] console: Support unsafe flushing for panic/emergency restarts John Ogness
2026-11-21 16:35 ` [PATCH printk v4 7/8] printk: Add unsafe flush points for panic+kexec and emergency_restart John Ogness
2026-11-21 16:35 ` [PATCH printk v4 8/8] netconsole: Perform unsafe flushing on panic and emergency restart John Ogness

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261121163557.963029-6-john.ogness@linutronix.de \
    --to=john.ogness@linutronix.de \
    --cc=leitao@debian.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=pmladek@suse.com \
    --cc=printk@lists.linux.dev \
    --cc=rostedt@goodmis.org \
    --cc=ryan.roberts@arm.com \
    --cc=senozhatsky@chromium.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®