mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH printk v3 0/7] Introduce policies for unsafe flushing
@ 2026-11-18 23:03 John Ogness
  2026-11-18 23:03 ` [PATCH printk v3 1/7] printk: Introduce and specify flush reasons John Ogness
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: John Ogness @ 2026-11-18 23:03 UTC (permalink / raw)
  To: Petr Mladek
  Cc: Sergey Senozhatsky, Steven Rostedt, Ryan Roberts, Breno Leitao,
	printk, linux-kernel, Andrew Morton, Rio, Danilo Krummrich,
	Aaron Tomlin, Greg Kroah-Hartman, Andy Shevchenko,
	Alessandro Carminati, Pnina Feder, Gal Pressman, Baoquan He,
	Mike Rapoport, Pasha Tatashin, Pratyush Yadav, Dave Young,
	Bradley Morgan, Oleg Nesterov, Pavel Tikhomirov, Petr Pavlu,
	Cen Zhang (Microsoft),
	kexec, Gustavo Luiz Duarte, Andrew Lunn, David S. Miller,
	Eric Dumazet, Jakub Kicinski, Paolo Abeni, netdev

v2 is here [0].

Until now unsafe flushing is only performed at the very end of
panic(), before entering the infinite loop. For other situations
(such as panic-kexec or panic-reboot or emergency-reboot) no
unsafe flushing is performed. The reasoning for this is because
an unsafe flush could lead to fireworks, which could prevent the
kexec or reboot from occurring.

However, some users may be willing to take on such risks in order
to see panic output. Indeed, for the netconsole (which does not
support safe atomic printing) it is a regression that such unsafe
printing does not occur. [1] 

This series adds unsafe flushing policy flags per-console to
allow individual consoles to perform unsafe flushing for such
scenarios. The unsafe flushing is hard-code-enabled for the
netconsole in patch 7/7. The justification is that this is not
less safe than the legacy netconsole.

Later these policy flags could be easily exposed to userspace
via /sys/class/consoles/, once the sysfs per-console interface
is available.

For this version I reduced the unsafe flush policies to the
minimum required by the netconsole: panic+restart and
emergency+restart. I also keep nbcon_atomic_flush_unsafe() and
use only this function for specifying the flush reasons. This
allows us to pinpoint where unsafe flushing is evaluated and,
as a nice bonus, to avoid all the legacy madness.

The first 4 patches have no functional changes. Hopefully they
are each small enough that a review is relatively trivial. The
functional changes (5-7) are also compact.

Changes since v2:

- Specify flush reason in nbcon_atomic_flush_unsafe() instead
  of console_flush_on_panic() and printk_trigger_flush(). This
  automatically avoids the need for any legacy handling.

- Remove KDB, PRINTK, NOTHREAD unsafe flush policies. (Not
  needed for now.)

- Add EMERGENCY_RESTART unsafe flush policy to be used for
  emergency_restart() when non-panic.

- Rename console.unsafe_flush_flags to
  console.allow_unsafe_policy.

- Change console_is_usable() to _never_ report
  CON_NBCON_ATOMIC_UNSAFE as usable.

- Introduce nbcon_is_unsafe_usable() as a variant to
  console_is_usable() to report CON_NBCON_ATOMIC_UNSAFE as
  usable. The caller decides if unsafe printing should occur.
  This is only used for nbcon+atomic flushing.

- Perform unsafe flush policy check higher up in
  __nbcon_atomic_flush_pending() instead of
  __nbcon_atomic_flush_pending_con().

John Ogness

[0] https://lore.kernel.org/lkml/20261111181942.593695-1-john.ogness@linutronix.de
[1] https://lore.kernel.org/lkml/20260915100140.3631650-1-ryan.roberts@arm.com

John Ogness (7):
  printk: Introduce and specify flush reasons
  printk: nbcon: Move unsafe takeover check to higher level
  printk: Move unsafe takeover check out of console_is_usable()
  printk: Use @reason instead of global variable to check flush policy
  console: Support unsafe flushing for panic/emergency restarts
  printk: Add unsafe flush points for panic+kexec and emergency_restart
  netconsole: Perform unsafe flushing on panic and emergency restart

 drivers/net/netconsole.c |  2 +
 include/linux/console.h  | 60 ++++++++++++++++++++------
 include/linux/printk.h   | 20 ++++++++-
 kernel/crash_core.c      |  1 +
 kernel/panic.c           |  2 +-
 kernel/printk/nbcon.c    | 91 +++++++++++++++++++++++++++-------------
 kernel/reboot.c          |  1 +
 7 files changed, 131 insertions(+), 46 deletions(-)


base-commit: ef19063ad84f8ed58ee44ebd49ba035fd5159e74
-- 
2.47.3


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH printk v3 1/7] printk: Introduce and specify flush reasons
  2026-11-18 23:03 [PATCH printk v3 0/7] Introduce policies for unsafe flushing John Ogness
@ 2026-11-18 23:03 ` John Ogness
  2026-11-18 23:03 ` [PATCH printk v3 2/7] printk: nbcon: Move unsafe takeover check to higher level John Ogness
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: John Ogness @ 2026-11-18 23:03 UTC (permalink / raw)
  To: Petr Mladek
  Cc: Sergey Senozhatsky, Steven Rostedt, Ryan Roberts, Breno Leitao,
	printk, linux-kernel, Andrew Morton, Rio, Danilo Krummrich,
	Aaron Tomlin, Greg Kroah-Hartman, Andy Shevchenko,
	Alessandro Carminati, Pnina Feder, Gal Pressman

Prepare for per-console unsafe atomic flushing policies by
introducing enum pr_flush_reason to classify various reasons
for performing a printk backlog flush. Specify the reason for
nbcon_atomic_flush_unsafe().

Propagate the reason down to __nbcon_atomic_flush_pending(),
where consoles are selected for atomic flushing.

Update the nbcon_atomic_flush_unsafe() and
__nbcon_atomic_flush_pending() call sites to specify the flush
reason.

Note that PR_FLUSH_RUNTIME is used for cases where the reason
for the flush is not clearly defined.

No functional change.

Signed-off-by: John Ogness <john.ogness@linutronix.de>
---
 include/linux/printk.h | 17 +++++++++++++++--
 kernel/panic.c         |  2 +-
 kernel/printk/nbcon.c  | 11 +++++++----
 3 files changed, 23 insertions(+), 7 deletions(-)

diff --git a/include/linux/printk.h b/include/linux/printk.h
index f594c1266bfd4..68cf51db5f0ea 100644
--- a/include/linux/printk.h
+++ b/include/linux/printk.h
@@ -145,6 +145,19 @@ void early_printk(const char *s, ...) { }
 
 struct dev_printk_info;
 
+/**
+ * enum pr_flush_reason - Reason for a flush
+ * @PR_FLUSH_RUNTIME:	A flush while the kernel is running
+ * @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
+ * performed, based on individual console policies.
+ */
+enum pr_flush_reason {
+	PR_FLUSH_RUNTIME,
+	PR_FLUSH_HANG,
+};
+
 #ifdef CONFIG_PRINTK
 asmlinkage __printf(4, 0)
 int vprintk_emit(int facility, int level,
@@ -207,7 +220,7 @@ void console_try_replay_all(void);
 void printk_legacy_allow_panic_sync(void);
 extern bool nbcon_device_try_acquire(struct console *con);
 extern void nbcon_device_release(struct console *con);
-void nbcon_atomic_flush_unsafe(void);
+void nbcon_atomic_flush_unsafe(enum pr_flush_reason reason);
 bool pr_flush(int timeout_ms, bool reset_on_progress);
 #else
 static inline __printf(1, 0)
@@ -318,7 +331,7 @@ static inline void nbcon_device_release(struct console *con)
 {
 }
 
-static inline void nbcon_atomic_flush_unsafe(void)
+static inline void nbcon_atomic_flush_unsafe(enum pr_flush_reason reason)
 {
 }
 
diff --git a/kernel/panic.c b/kernel/panic.c
index a4dbb6b8cd2bf..21d0ca892c64f 100644
--- a/kernel/panic.c
+++ b/kernel/panic.c
@@ -765,7 +765,7 @@ void vpanic(const char *fmt, va_list args)
 	 * Explicitly flush the kernel log buffer one last time.
 	 */
 	console_flush_on_panic(CONSOLE_FLUSH_PENDING);
-	nbcon_atomic_flush_unsafe();
+	nbcon_atomic_flush_unsafe(PR_FLUSH_HANG);
 
 	local_irq_enable();
 	for (i = 0; ; i += PANIC_TIMER_STEP) {
diff --git a/kernel/printk/nbcon.c b/kernel/printk/nbcon.c
index 5f8afaca88665..72fb49ade6fe5 100644
--- a/kernel/printk/nbcon.c
+++ b/kernel/printk/nbcon.c
@@ -1652,9 +1652,10 @@ static void nbcon_atomic_flush_pending_con(struct console *con, u64 stop_seq)
 /**
  * __nbcon_atomic_flush_pending - Flush all nbcon consoles using their
  *					write_atomic() callback
+ * @reason:			The reason for the flush
  * @stop_seq:			Flush up until this record
  */
-static void __nbcon_atomic_flush_pending(u64 stop_seq)
+static void __nbcon_atomic_flush_pending(enum pr_flush_reason reason, u64 stop_seq)
 {
 	struct console *con;
 	int cookie;
@@ -1688,20 +1689,22 @@ static void __nbcon_atomic_flush_pending(u64 stop_seq)
  */
 void nbcon_atomic_flush_pending(void)
 {
-	__nbcon_atomic_flush_pending(prb_next_reserve_seq(prb));
+	__nbcon_atomic_flush_pending(PR_FLUSH_RUNTIME, prb_next_reserve_seq(prb));
 }
 
 /**
  * nbcon_atomic_flush_unsafe - Flush all nbcon consoles using their
  *	write_atomic() callback and allowing unsafe hostile takeovers
  *
+ * @reason:	The reason for the flush
+ *
  * Flush the backlog up through the currently newest record. Unsafe hostile
  * takeovers will be performed, if necessary.
  */
-void nbcon_atomic_flush_unsafe(void)
+void nbcon_atomic_flush_unsafe(enum pr_flush_reason reason)
 {
 	panic_nbcon_allow_unsafe_takeover = true;
-	__nbcon_atomic_flush_pending(prb_next_reserve_seq(prb));
+	__nbcon_atomic_flush_pending(reason, prb_next_reserve_seq(prb));
 	panic_nbcon_allow_unsafe_takeover = false;
 }
 
-- 
2.47.3


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH printk v3 2/7] printk: nbcon: Move unsafe takeover check to higher level
  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 ` John Ogness
  2026-11-18 23:03 ` [PATCH printk v3 3/7] printk: Move unsafe takeover check out of console_is_usable() John Ogness
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: John Ogness @ 2026-11-18 23:03 UTC (permalink / raw)
  To: Petr Mladek
  Cc: Sergey Senozhatsky, Steven Rostedt, Ryan Roberts, Breno Leitao,
	printk, linux-kernel

Commit 187de7c212e5 ("printk: nbcon: Allow unsafe write_atomic() for
panic") introduced nbcon_allow_unsafe_takeover() to determine if
unsafe takeovers are allowed based on the current context. The call
was placed as deep as possible, in __nbcon_atomic_flush_pending_con(),
to avoid passing down @allow_unsafe_takeover.

Now a flush @reason is available and will be used as additional
information to determine if unsafe takeovers are allowed. For
this the placement of nbcon_allow_unsafe_takeover() is too deep.

Move nbcon_allow_unsafe_takeover() up 2 call levels and restore some
of the @allow_unsafe_takeover passing. This positions the call at an
appropriate level for using the flush @reason.

No functional change.

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

diff --git a/kernel/printk/nbcon.c b/kernel/printk/nbcon.c
index 72fb49ade6fe5..7bc10dcaee7e5 100644
--- a/kernel/printk/nbcon.c
+++ b/kernel/printk/nbcon.c
@@ -1543,6 +1543,7 @@ bool nbcon_legacy_emit_next_record(struct console *con, bool *handover,
  *					write_atomic() callback
  * @con:			The nbcon console to flush
  * @stop_seq:			Flush up until this record
+ * @allow_unsafe_takeover:	True, to allow unsafe hostile takeovers
  *
  * Return:	0 if @con was flushed up to @stop_seq Otherwise, error code on
  *		failure.
@@ -1561,7 +1562,8 @@ bool nbcon_legacy_emit_next_record(struct console *con, bool *handover,
  * returned, it cannot be expected that the unfinalized record will become
  * available.
  */
-static int __nbcon_atomic_flush_pending_con(struct console *con, u64 stop_seq)
+static int __nbcon_atomic_flush_pending_con(struct console *con, u64 stop_seq,
+					    bool allow_unsafe_takeover)
 {
 	struct nbcon_write_context wctxt = { };
 	struct nbcon_context *ctxt = &ACCESS_PRIVATE(&wctxt, ctxt);
@@ -1570,7 +1572,7 @@ static int __nbcon_atomic_flush_pending_con(struct console *con, u64 stop_seq)
 	ctxt->console			= con;
 	ctxt->spinwait_max_us		= 2000;
 	ctxt->prio			= nbcon_get_default_prio();
-	ctxt->allow_unsafe_takeover	= nbcon_allow_unsafe_takeover();
+	ctxt->allow_unsafe_takeover	= allow_unsafe_takeover;
 
 	while (nbcon_seq_read(con) < stop_seq) {
 		/*
@@ -1611,19 +1613,21 @@ static int __nbcon_atomic_flush_pending_con(struct console *con, u64 stop_seq)
  *					write_atomic() callback
  * @con:			The nbcon console to flush
  * @stop_seq:			Flush up until this record
+ * @allow_unsafe_takeover:	True, to allow unsafe hostile takeovers
  *
  * This will stop flushing before @stop_seq if another context has ownership.
  * That context is then responsible for the flushing. Likewise, if new records
  * are added while this context was flushing and there is no other context
  * to handle the printing, this context must also flush those records.
  */
-static void nbcon_atomic_flush_pending_con(struct console *con, u64 stop_seq)
+static void nbcon_atomic_flush_pending_con(struct console *con, u64 stop_seq,
+					   bool allow_unsafe_takeover)
 {
 	struct console_flush_type ft;
 	int err;
 
 again:
-	err = __nbcon_atomic_flush_pending_con(con, stop_seq);
+	err = __nbcon_atomic_flush_pending_con(con, stop_seq, allow_unsafe_takeover);
 
 	/*
 	 * If there was a new owner (-EPERM, -EAGAIN), that context is
@@ -1657,6 +1661,7 @@ static void nbcon_atomic_flush_pending_con(struct console *con, u64 stop_seq)
  */
 static void __nbcon_atomic_flush_pending(enum pr_flush_reason reason, u64 stop_seq)
 {
+	bool allow_unsafe_takeover;
 	struct console *con;
 	int cookie;
 
@@ -1670,10 +1675,12 @@ static void __nbcon_atomic_flush_pending(enum pr_flush_reason reason, u64 stop_s
 		if (!console_is_usable(con, flags, true))
 			continue;
 
+		allow_unsafe_takeover = nbcon_allow_unsafe_takeover();
+
 		if (nbcon_seq_read(con) >= stop_seq)
 			continue;
 
-		nbcon_atomic_flush_pending_con(con, stop_seq);
+		nbcon_atomic_flush_pending_con(con, stop_seq, allow_unsafe_takeover);
 	}
 	console_srcu_read_unlock(cookie);
 }
@@ -1934,7 +1941,7 @@ void nbcon_device_release(struct console *con)
 		 * using the legacy loop.
 		 */
 		if (ft.nbcon_atomic) {
-			__nbcon_atomic_flush_pending_con(con, prb_next_reserve_seq(prb));
+			__nbcon_atomic_flush_pending_con(con, prb_next_reserve_seq(prb), false);
 		} else if (ft.legacy_direct) {
 			if (console_trylock())
 				console_unlock();
@@ -2004,7 +2011,7 @@ void nbcon_kdb_release(struct nbcon_write_context *wctxt)
 	 * The console was locked only when the write_atomic() callback
 	 * was usable.
 	 */
-	__nbcon_atomic_flush_pending_con(ctxt->console, prb_next_reserve_seq(prb));
+	__nbcon_atomic_flush_pending_con(ctxt->console, prb_next_reserve_seq(prb), false);
 }
 
 /**
-- 
2.47.3


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH printk v3 3/7] printk: Move unsafe takeover check out of console_is_usable()
  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
  2026-11-18 23:03 ` [PATCH printk v3 4/7] printk: Use @reason instead of global variable to check flush policy John Ogness
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: John Ogness @ 2026-11-18 23:03 UTC (permalink / raw)
  To: Petr Mladek
  Cc: Sergey Senozhatsky, Steven Rostedt, Ryan Roberts, Breno Leitao,
	printk, linux-kernel, Greg Kroah-Hartman

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


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH printk v3 4/7] printk: Use @reason instead of global variable to check flush policy
  2026-11-18 23:03 [PATCH printk v3 0/7] Introduce policies for unsafe flushing John Ogness
                   ` (2 preceding siblings ...)
  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
  2026-11-18 23:03 ` [PATCH printk v3 5/7] console: Support unsafe flushing for panic/emergency restarts John Ogness
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: John Ogness @ 2026-11-18 23:03 UTC (permalink / raw)
  To: Petr Mladek
  Cc: Sergey Senozhatsky, Steven Rostedt, Ryan Roberts, Breno Leitao,
	printk, linux-kernel

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


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH printk v3 5/7] console: Support unsafe flushing for panic/emergency restarts
  2026-11-18 23:03 [PATCH printk v3 0/7] Introduce policies for unsafe flushing John Ogness
                   ` (3 preceding siblings ...)
  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
  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
  6 siblings, 0 replies; 8+ messages in thread
From: John Ogness @ 2026-11-18 23:03 UTC (permalink / raw)
  To: Petr Mladek
  Cc: Sergey Senozhatsky, Steven Rostedt, Ryan Roberts, Breno Leitao,
	printk, linux-kernel, Greg Kroah-Hartman

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


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH printk v3 6/7] printk: Add unsafe flush points for panic+kexec and emergency_restart
  2026-11-18 23:03 [PATCH printk v3 0/7] Introduce policies for unsafe flushing John Ogness
                   ` (4 preceding siblings ...)
  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 ` John Ogness
  2026-11-18 23:03 ` [PATCH printk v3 7/7] netconsole: Perform unsafe flushing on panic and emergency restart John Ogness
  6 siblings, 0 replies; 8+ messages in thread
From: John Ogness @ 2026-11-18 23:03 UTC (permalink / raw)
  To: Petr Mladek
  Cc: Sergey Senozhatsky, Steven Rostedt, Ryan Roberts, Breno Leitao,
	printk, linux-kernel, Andrew Morton, Baoquan He, Mike Rapoport,
	Pasha Tatashin, Pratyush Yadav, Dave Young, Bradley Morgan,
	Oleg Nesterov, Pavel Tikhomirov, Petr Pavlu,
	Cen Zhang (Microsoft),
	kexec

During emergency and panic, nbcon consoles transition to atomic printing.
However, there are consoles that do not support safe atomic printing
(CON_NBCON_ATOMIC_UNSAFE). Such consoles will only flush when an unsafe
flush is explicitly invoked and their unsafe policy permits unsafe
flushing for that context.

Call nbcon_atomic_flush_unsafe(PR_FLUSH_RESTART) before performing kexec
and emergency restarts in order to allow such consoles to perform an
unsafe flush (if their unsafe policy permits).

Signed-off-by: John Ogness <john.ogness@linutronix.de>
---
 kernel/crash_core.c | 1 +
 kernel/reboot.c     | 1 +
 2 files changed, 2 insertions(+)

diff --git a/kernel/crash_core.c b/kernel/crash_core.c
index d0bd2d0cf8990..79a67956743ac 100644
--- a/kernel/crash_core.c
+++ b/kernel/crash_core.c
@@ -140,6 +140,7 @@ void __noclone __crash_kexec(struct pt_regs *regs)
 
 			crash_setup_regs(&fixed_regs, regs);
 			crash_save_vmcoreinfo();
+			nbcon_atomic_flush_unsafe(PR_FLUSH_RESTART);
 			machine_crash_shutdown(&fixed_regs);
 			crash_cma_clear_pending_dma();
 			machine_kexec(kexec_crash_image);
diff --git a/kernel/reboot.c b/kernel/reboot.c
index a93c23b9e1948..b26d131c54151 100644
--- a/kernel/reboot.c
+++ b/kernel/reboot.c
@@ -102,6 +102,7 @@ void emergency_restart(void)
 	 */
 	nbcon_cpu_emergency_enter();
 	printk_trigger_flush();
+	nbcon_atomic_flush_unsafe(PR_FLUSH_RESTART);
 
 	machine_emergency_restart();
 }
-- 
2.47.3


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH printk v3 7/7] netconsole: Perform unsafe flushing on panic and emergency restart
  2026-11-18 23:03 [PATCH printk v3 0/7] Introduce policies for unsafe flushing John Ogness
                   ` (5 preceding siblings ...)
  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 ` John Ogness
  6 siblings, 0 replies; 8+ messages in thread
From: John Ogness @ 2026-11-18 23:03 UTC (permalink / raw)
  To: Petr Mladek
  Cc: Sergey Senozhatsky, Steven Rostedt, Ryan Roberts, Breno Leitao,
	printk, linux-kernel, Gustavo Luiz Duarte, Andrew Lunn,
	David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	netdev

As a legacy console the netconsole would perform unsafe flushing
during panic and emergency restart. With commit 7eab73b18630
("netconsole: convert to NBCON console infrastructure") the netconsole
is now an NBCON console, however it uses the CON_NBCON_ATOMIC_UNSAFE
flag and this prevents unsafe flushing except for at the very end of
panic().

Enable default unsafe flushing policies to allow the netconsole to
perform unsafe flushing during panic and emergency restart.

Signed-off-by: John Ogness <john.ogness@linutronix.de>
---
 drivers/net/netconsole.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/drivers/net/netconsole.c b/drivers/net/netconsole.c
index 267254f046de9..3300b2bbbd413 100644
--- a/drivers/net/netconsole.c
+++ b/drivers/net/netconsole.c
@@ -2695,6 +2695,7 @@ static void free_param_target(struct netconsole_target *nt)
 static struct console netconsole_ext = {
 	.name = "netcon_ext",
 	.flags = CON_ENABLED | CON_EXTENDED | CON_NBCON | CON_NBCON_ATOMIC_UNSAFE,
+	.allow_unsafe_policy = NBCON_PANIC_RESTART_FLUSH | NBCON_EMERGENCY_RESTART_FLUSH,
 	.write_thread = netconsole_write_ext,
 	.write_atomic = netconsole_write_ext,
 	.device_lock = netconsole_device_lock,
@@ -2704,6 +2705,7 @@ static struct console netconsole_ext = {
 static struct console netconsole = {
 	.name = "netcon",
 	.flags = CON_ENABLED | CON_NBCON | CON_NBCON_ATOMIC_UNSAFE,
+	.allow_unsafe_policy = NBCON_PANIC_RESTART_FLUSH | NBCON_EMERGENCY_RESTART_FLUSH,
 	.write_thread = netconsole_write_basic,
 	.write_atomic = netconsole_write_basic,
 	.device_lock = netconsole_device_lock,
-- 
2.47.3


^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2026-11-18 23:03 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 ` [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

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®