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,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Subject: [PATCH printk v3 3/7] printk: Move unsafe takeover check out of console_is_usable()
Date: Thu, 19 Nov 2026 00:09:39 +0106	[thread overview]
Message-ID: <20261118230345.808560-4-john.ogness@linutronix.de> (raw)
In-Reply-To: <20261118230345.808560-1-john.ogness@linutronix.de>

Change console_is_usable() to never claim a console with
CON_NBCON_ATOMIC_UNSAFE is usable since that is the normal use
case. This allows moving nbcon_allow_unsafe_takeover() out of
console_is_usable().

To support unsafe atomic flushing, provide a variant of
console_is_usable() called nbcon_is_unsafe_usable() that accepts
CON_NBCON_ATOMIC_UNSAFE consoles as usable. However, this adds
responsibility to the caller to determine if unsafe atomic printing
should occur.

__nbcon_atomic_flush_pending() is the only function that enables
unsafe atomic printing by calling nbcon_allow_unsafe_takeover().
Rather that using console_is_usable() to select atomic printing
consoles, use nbcon_is_unsafe_usable() and only select
CON_NBCON_ATOMIC_UNSAFE consoles if the unsafe takeover is allowed.

These changes effectively move the nbcon_allow_unsafe_takeover()
check from console_is_usable() to __nbcon_atomic_flush_pending().
This has no external functional change. Internally within
__nbcon_atomic_flush_pending() it means that
nbcon_allow_unsafe_takeover() is evaluated after the usable check
rather than during. The result is the same.

Since __nbcon_atomic_flush_pending() also knows the @reason for
the flush, this opens the door for the unsafe atomic flushing
policy to be enforced in __nbcon_atomic_flush_pending().

Signed-off-by: John Ogness <john.ogness@linutronix.de>
---
 include/linux/console.h | 40 +++++++++++++++++++++++++++-------------
 kernel/printk/nbcon.c   | 17 +++++++++++++++--
 2 files changed, 42 insertions(+), 15 deletions(-)

diff --git a/include/linux/console.h b/include/linux/console.h
index df23148987eeb..227a7cf296f60 100644
--- a/include/linux/console.h
+++ b/include/linux/console.h
@@ -611,7 +611,6 @@ extern void nbcon_write_context_set_buf(struct nbcon_write_context *wctxt,
 extern bool nbcon_enter_unsafe(struct nbcon_write_context *wctxt);
 extern bool nbcon_exit_unsafe(struct nbcon_write_context *wctxt);
 extern void nbcon_reacquire_nobuf(struct nbcon_write_context *wctxt);
-extern bool nbcon_allow_unsafe_takeover(void);
 extern bool nbcon_kdb_try_acquire(struct console *con,
 				  struct nbcon_write_context *wctxt);
 extern void nbcon_kdb_release(struct nbcon_write_context *wctxt);
@@ -625,13 +624,8 @@ static inline bool nbcon_is_braille(struct console *con)
 	return con && (con->flags & CON_BRL);
 }
 
-/*
- * Check if the given console is currently capable and allowed to print
- * records. Note that this function does not consider the current context,
- * which can also play a role in deciding if @con can be used to print
- * records.
- */
-static inline bool console_is_usable(struct console *con, short flags, bool use_atomic)
+static inline bool __console_is_usable(struct console *con, short flags, bool use_atomic,
+				       bool allow_atomic_unsafe)
 {
 	if (!(flags & CON_ENABLED))
 		return false;
@@ -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))
 				return false;
 		}
 
@@ -675,6 +665,29 @@ static inline bool console_is_usable(struct console *con, short flags, bool use_
 	return true;
 }
 
+/*
+ * Check if the given console is currently capable and allowed to print
+ * records. Note that this function does not consider the current context,
+ * which can also play a role in deciding if @con can be used to print
+ * records.
+ */
+static inline bool console_is_usable(struct console *con, short flags, bool use_atomic)
+{
+	return __console_is_usable(con, flags, use_atomic, false);
+}
+
+/*
+ * Similar to console_is_usable(), but allows atomic unsafe consoles. This is
+ * only interesting for nbcon unsafe atomic printing.
+ *
+ * Note: It is the responsibility of the caller to decide if unsafe printing
+ *       should actually occur.
+ */
+static inline bool nbcon_is_unsafe_usable(struct console *con, short flags)
+{
+	return __console_is_usable(con, flags, true, true);
+}
+
 #else
 static inline void nbcon_cpu_emergency_enter(void) { }
 static inline void nbcon_cpu_emergency_exit(void) { }
@@ -691,6 +704,7 @@ static inline
 bool nbcon_write_context_is_braille(struct nbcon_write_context *wctxt) { return false; }
 static inline bool console_is_usable(struct console *con, short flags,
 				     bool use_atomic) { return false; }
+static inline bool nbcon_is_unsafe_usable(struct console *con, short flags) { return false; }
 static inline bool nbcon_braille_try_acquire(struct console *con,
 					     struct nbcon_write_context *wctxt) { return false; }
 static inline void nbcon_braille_release(struct nbcon_write_context *wctxt) { }
diff --git a/kernel/printk/nbcon.c b/kernel/printk/nbcon.c
index 7bc10dcaee7e5..9073d1f258a89 100644
--- 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;
 
 		allow_unsafe_takeover = nbcon_allow_unsafe_takeover();
 
+		/*
+		 * 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;
+
 		if (nbcon_seq_read(con) >= stop_seq)
 			continue;
 
-- 
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 ` John Ogness [this message]
2026-11-18 23:03 ` [PATCH printk v3 4/7] printk: Use @reason instead of global variable to check flush policy John Ogness
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-4-john.ogness@linutronix.de \
    --to=john.ogness@linutronix.de \
    --cc=gregkh@linuxfoundation.org \
    --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®