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 v3 4/7] printk: Use @reason instead of global variable to check flush policy
Date: Thu, 19 Nov 2026 00:09:40 +0106	[thread overview]
Message-ID: <20261118230345.808560-5-john.ogness@linutronix.de> (raw)
In-Reply-To: <20261118230345.808560-1-john.ogness@linutronix.de>

Currently nbcon_allow_unsafe_takeover() is a hack, relying on the
temporary setting of a global variable @panic_nbcon_allow_unsafe_takeover
as a gate to determine if unsafe flushing is allowed. This hack works
because unsafe flushing is only performed at the end of panic().
However, this hack is _not_ sufficient if unsafe flushing is to be
allowed in other contexts.

Since at the end of panic() nbcon_atomic_flush_unsafe() is called with
the @reason PR_FLUSH_HANG, it is possible to identify when unsafe
atomic flushing is allowed using:

	PR_FLUSH_HANG && panic_on_this_cpu()

rather than:

	panic_nbcon_allow_unsafe_takeover && panic_on_this_cpu()

This allows removing the global variable hack and opens the door for
unsafe flushing to be allowed in other contexts.

Provide nbcon_allow_unsafe_takeover() the flush @reason to implement
the check.

No functional change.

Signed-off-by: John Ogness <john.ogness@linutronix.de>
---
 kernel/printk/nbcon.c | 35 ++++++++++++++++++++---------------
 1 file changed, 20 insertions(+), 15 deletions(-)

diff --git a/kernel/printk/nbcon.c b/kernel/printk/nbcon.c
index 9073d1f258a89..c3307b5b2c922 100644
--- a/kernel/printk/nbcon.c
+++ b/kernel/printk/nbcon.c
@@ -1453,24 +1453,31 @@ enum nbcon_prio nbcon_get_default_prio(void)
 	return NBCON_PRIO_NORMAL;
 }
 
-/*
- * Track if it is allowed to perform unsafe hostile takeovers of console
- * ownership. When true, console drivers might perform unsafe actions while
- * printing. It is externally available via nbcon_allow_unsafe_takeover().
- */
-static bool panic_nbcon_allow_unsafe_takeover;
-
 /**
  * nbcon_allow_unsafe_takeover - Check if unsafe console takeovers are allowed
+ *					for a specified console and reason
  *
- * Return:	True, when it is permitted to perform unsafe console printing
+ * @con:	The console to check
+ * @reason:	The reason for the 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).
+ * Return:	True, when @con is allowed to perform unsafe console printing
  */
-static bool nbcon_allow_unsafe_takeover(void)
+static bool nbcon_allow_unsafe_takeover(struct console *con, enum pr_flush_reason reason)
 {
-	return panic_on_this_cpu() && panic_nbcon_allow_unsafe_takeover;
+	enum nbcon_prio prio = nbcon_get_default_prio();
+	bool allow = false;
+
+	switch (reason) {
+	case PR_FLUSH_RUNTIME:
+		/* Never unsafe flush during runtime. */
+		break;
+	case PR_FLUSH_HANG:
+		/* Always unsafe flush on panic hang. */
+		allow = (prio == NBCON_PRIO_PANIC);
+		break;
+	}
+
+	return allow;
 }
 
 /**
@@ -1680,7 +1687,7 @@ static void __nbcon_atomic_flush_pending(enum pr_flush_reason reason, u64 stop_s
 		if (!nbcon_is_unsafe_usable(con, flags))
 			continue;
 
-		allow_unsafe_takeover = nbcon_allow_unsafe_takeover();
+		allow_unsafe_takeover = nbcon_allow_unsafe_takeover(con, reason);
 
 		/*
 		 * Since nbcon_is_unsafe_usable() was used, ensure @con does
@@ -1723,9 +1730,7 @@ void nbcon_atomic_flush_pending(void)
  */
 void nbcon_atomic_flush_unsafe(enum pr_flush_reason reason)
 {
-	panic_nbcon_allow_unsafe_takeover = true;
 	__nbcon_atomic_flush_pending(reason, prb_next_reserve_seq(prb));
-	panic_nbcon_allow_unsafe_takeover = false;
 }
 
 /**
-- 
2.47.3


  parent reply	other threads:[~2026-11-18 23:03 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-18 23:03 [PATCH printk v3 0/7] Introduce policies for unsafe flushing John Ogness
2026-11-18 23:03 ` [PATCH printk v3 1/7] printk: Introduce and specify flush reasons John Ogness
2026-11-18 23:03 ` [PATCH printk v3 2/7] printk: nbcon: Move unsafe takeover check to higher level John Ogness
2026-11-18 23:03 ` [PATCH printk v3 3/7] printk: Move unsafe takeover check out of console_is_usable() John Ogness
2026-11-18 23:03 ` John Ogness [this message]
2026-11-18 23:03 ` [PATCH printk v3 5/7] console: Support unsafe flushing for panic/emergency restarts John Ogness
2026-11-18 23:03 ` [PATCH printk v3 6/7] printk: Add unsafe flush points for panic+kexec and emergency_restart John Ogness
2026-11-18 23:03 ` [PATCH printk v3 7/7] 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=20261118230345.808560-5-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®