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 5/7] console: Support unsafe flushing for panic/emergency restarts
Date: Thu, 19 Nov 2026 00:09:41 +0106	[thread overview]
Message-ID: <20261118230345.808560-6-john.ogness@linutronix.de> (raw)
In-Reply-To: <20261118230345.808560-1-john.ogness@linutronix.de>

Introduce a new nbcon-specific console field @allow_unsafe_policy
to store the unsafe flush policies for a console. The policies are
represented by bits, specified by enum nbcon_unsafe_flush_flags.

Define unsafe flush policies for:

	panic+restart: This is for kexec and reboot from panic.

	emergency+restart: This is for emergency_restart() when in
				the emergency state.

Define a new flush type:

	PR_FLUSH_RESTART: For flushing before kexec and reboot.

Implement the policy check within nbcon_allow_unsafe_takeover().

Since this adds support for unsafe flushing in non-panic situations,
remove the related WARN from nbcon_context_try_acquire_hostile().

Except for the removal of a WARN, no functional change.

Signed-off-by: John Ogness <john.ogness@linutronix.de>
---
 include/linux/console.h | 20 ++++++++++++++++++++
 include/linux/printk.h  |  3 +++
 kernel/printk/nbcon.c   | 11 +++++++----
 3 files changed, 30 insertions(+), 4 deletions(-)

diff --git a/include/linux/console.h b/include/linux/console.h
index 227a7cf296f60..1afeb90692395 100644
--- a/include/linux/console.h
+++ b/include/linux/console.h
@@ -194,6 +194,23 @@ enum cons_flags {
 	CON_NBCON_ATOMIC_UNSAFE	= BIT(9),
 };
 
+/**
+ * enum nbcon_unsafe_flush_flags - Unsafe atomic flush policy flags
+ * @NBCON_PANIC_RESTART_FLUSH:		In panic, unsafe flush before kexec
+ *						or reboot.
+ * @NBCON_EMERGENCY_RESTART_FLUSH:	Unsafe flush before emergency restart.
+ *
+ * NBCON unsafe atomic flushing always occurs at the end of panic() before
+ * the kernel enters an infinite loop. Users may choose to perform unsafe
+ * flushing in additional scenarios where the current kernel is ending in
+ * order to increase the chances of seeing console messages, even if it is
+ * not safe to do so.
+ */
+enum nbcon_unsafe_flush_flags {
+	NBCON_PANIC_RESTART_FLUSH	= BIT(0),
+	NBCON_EMERGENCY_RESTART_FLUSH	= BIT(1),
+};
+
 /**
  * struct nbcon_state - console state for nbcon consoles
  * @atom:	Compound of the state fields for atomic operations
@@ -326,6 +343,7 @@ struct nbcon_write_context {
  * @data:		Driver private data
  * @node:		hlist node for the console list
  *
+ * @allow_unsafe_policy: Unsafe atomic flush policy. See nbcon_unsafe_flush_flags
  * @nbcon_state:	State for nbcon consoles
  * @nbcon_seq:		Sequence number of the next record for nbcon to print
  * @nbcon_device_ctxt:	Context available for non-printing operations
@@ -461,6 +479,8 @@ struct console {
 	 */
 	void (*device_unlock)(struct console *con, unsigned long flags);
 
+	unsigned int		allow_unsafe_policy;
+
 	atomic_t		__private nbcon_state;
 	atomic_long_t		__private nbcon_seq;
 	struct nbcon_context	__private nbcon_device_ctxt;
diff --git a/include/linux/printk.h b/include/linux/printk.h
index 68cf51db5f0ea..267a0a6fd23cf 100644
--- a/include/linux/printk.h
+++ b/include/linux/printk.h
@@ -148,6 +148,8 @@ struct dev_printk_info;
 /**
  * enum pr_flush_reason - Reason for a flush
  * @PR_FLUSH_RUNTIME:	A flush while the kernel is running
+ * @PR_FLUSH_RESTART:	The kernel is ending, but another instance should
+ *			follow (reboot, kexec)
  * @PR_FLUSH_HANG:	The kernel is ending and will enter an infinite loop
  *
  * Flush reasons are used to determine if unsafe atomic flushing should be
@@ -155,6 +157,7 @@ struct dev_printk_info;
  */
 enum pr_flush_reason {
 	PR_FLUSH_RUNTIME,
+	PR_FLUSH_RESTART,
 	PR_FLUSH_HANG,
 };
 
diff --git a/kernel/printk/nbcon.c b/kernel/printk/nbcon.c
index c3307b5b2c922..daf1fe8522117 100644
--- a/kernel/printk/nbcon.c
+++ b/kernel/printk/nbcon.c
@@ -557,10 +557,6 @@ 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. */
-	if (WARN_ON_ONCE(ctxt->prio != NBCON_PRIO_PANIC))
-		return -EPERM;
-
 	/*
 	 * Check that try_acquire_direct() and try_acquire_handover() returned
 	 * -EBUSY in the right situation.
@@ -1465,12 +1461,19 @@ enum nbcon_prio nbcon_get_default_prio(void)
 static bool nbcon_allow_unsafe_takeover(struct console *con, enum pr_flush_reason reason)
 {
 	enum nbcon_prio prio = nbcon_get_default_prio();
+	int unsafe_policy = con->allow_unsafe_policy;
 	bool allow = false;
 
 	switch (reason) {
 	case PR_FLUSH_RUNTIME:
 		/* Never unsafe flush during runtime. */
 		break;
+	case PR_FLUSH_RESTART:
+		if (prio == NBCON_PRIO_PANIC)
+			allow = (unsafe_policy & NBCON_PANIC_RESTART_FLUSH);
+		else if (prio == NBCON_PRIO_EMERGENCY)
+			allow = (unsafe_policy & NBCON_EMERGENCY_RESTART_FLUSH);
+		break;
 	case PR_FLUSH_HANG:
 		/* Always unsafe flush on panic hang. */
 		allow = (prio == NBCON_PRIO_PANIC);
-- 
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 ` [PATCH printk v3 4/7] printk: Use @reason instead of global variable to check flush policy John Ogness
2026-11-18 23:03 ` John Ogness [this message]
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-6-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®