Skip to contentSkip to conversation

Introduce policies for unsafe flushing

PATCH Superseded John Ogness 2 participants 11 messages Nov 18 – Nov 20

Subsystems (8)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

John Ogness

author maintainer
#0 lore raw

v3 cover Introduce policies for unsafe flushing

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/[email protected]
[1] https://lore.kernel.org/lkml/[email protected]

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

Signature · 1 line
2.47.3
author maintainer
#1 lore raw

PATCH v3 1/7 printk: Introduce and specify flush reasons

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.

---
 include/linux/printk.h | 17 +++++++++++++++--
 kernel/panic.c         |  2 +-
 kernel/printk/nbcon.c  | 11 +++++++----
 3 files changed, 23 insertions(+), 7 deletions(-)
include/linux/printk.h +15 -2
index f594c1266bfd4..68cf51db5f0ea 100644
@@ -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)
 {
 }
 
kernel/panic.c +1 -1
index a4dbb6b8cd2bf..21d0ca892c64f 100644
@@ -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) {
kernel/printk/nbcon.c +7 -4
index 5f8afaca88665..72fb49ade6fe5 100644
@@ -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;
 }
 
Signature · 1 line
2.47.3
author maintainer
#2 lore raw

PATCH v3 2/7 printk: nbcon: Move unsafe takeover check to higher level

In reply to John Ogness

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.

---
 kernel/printk/nbcon.c | 21 ++++++++++++++-------
 1 file changed, 14 insertions(+), 7 deletions(-)
kernel/printk/nbcon.c +14 -7
index 72fb49ade6fe5..7bc10dcaee7e5 100644
@@ -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);
 }
 
 /**
Signature · 1 line
2.47.3
author maintainer
#3 lore raw

PATCH v3 3/7 printk: Move unsafe takeover check out of console_is_usable()

In reply to John Ogness

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().

---
 include/linux/console.h | 40 +++++++++++++++++++++++++++-------------
 kernel/printk/nbcon.c   | 17 +++++++++++++++--
 2 files changed, 42 insertions(+), 15 deletions(-)
include/linux/console.h +27 -13
index df23148987eeb..227a7cf296f60 100644
@@ -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) { }
kernel/printk/nbcon.c +15 -2
index 7bc10dcaee7e5..9073d1f258a89 100644
@@ -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;
 
Signature · 1 line
2.47.3
author maintainer
#4 lore raw

PATCH v3 4/7 printk: Use @reason instead of global variable to check flush policy

In reply to John Ogness

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.

---
 kernel/printk/nbcon.c | 35 ++++++++++++++++++++---------------
 1 file changed, 20 insertions(+), 15 deletions(-)
kernel/printk/nbcon.c +20 -15
index 9073d1f258a89..c3307b5b2c922 100644
@@ -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;
 }
 
 /**
Signature · 1 line
2.47.3
author maintainer
#5 lore raw

PATCH v3 5/7 console: Support unsafe flushing for panic/emergency restarts

In reply to John Ogness

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.

---
 include/linux/console.h | 20 ++++++++++++++++++++
 include/linux/printk.h  |  3 +++
 kernel/printk/nbcon.c   | 11 +++++++----
 3 files changed, 30 insertions(+), 4 deletions(-)
include/linux/console.h +20 -0
index 227a7cf296f60..1afeb90692395 100644
@@ -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;
include/linux/printk.h +3 -0
index 68cf51db5f0ea..267a0a6fd23cf 100644
@@ -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,
 };
 
kernel/printk/nbcon.c +7 -4
index c3307b5b2c922..daf1fe8522117 100644
@@ -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);
Signature · 1 line
2.47.3
#6 lore raw

Re: [PATCH printk v3 5/7] console: Support unsafe flushing for panic/emergency restarts

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Medium] printk: nbcon: shared con->pbufs race in nbcon_context_try_acquire() - [Medium] printk: nbcon…

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Medium] printk: nbcon: shared con->pbufs race in nbcon_context_try_acquire()
- [Medium] printk: nbcon: missing state checks in nbcon_context_try_acquire_hostile()
--

commit f5fa2c4aaf37e7c2f76889c2c2f671c2bb7b3a1a
Author: John Ogness <[email protected]>

console: Support unsafe flushing for panic/emergency restarts

This commit introduces per-console unsafe atomic flush policy flags and a PR_FLUSH_RESTART flush reason so consoles can opt into unsafe hostile takeovers during panic or emergency restarts.

Quoted 5 lines
kernel/printk/nbcon.c +0 -0
index c3307b5b2c922..daf1fe8522117 100644

kernel/printk/nbcon.c static int nbcon_context_try_acquire_hostile(struct nbcon_context *ctxt, · line 562

Quoted 10 lines
kernel/printk/nbcon.c +0 -4
@@ -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.

[Severity: Medium]
Can an NBCON_PRIO_EMERGENCY hostile takeover clobber con->pbufs while the interrupted NBCON_PRIO_NORMAL owner is still using it?

NBCON_PRIO_NORMAL and NBCON_PRIO_EMERGENCY share con->pbufs and rely on the unsafe section in nbcon_emit_next_record() to keep them from writing to it at the same time. Previously only NBCON_PRIO_PANIC could perform a hostile takeover of an unsafe section, and nbcon_context_try_acquire() switched to panic_nbcon_pbufs when panic_on_this_cpu() was true:

kernel/printk/nbcon.c:nbcon_context_try_acquire() {
    ...
	/* Assign the appropriate buffer for this context. */
	if (panic_on_this_cpu())
		ctxt->pbufs = &panic_nbcon_pbufs;
	else
		ctxt->pbufs = con->pbufs;
    ...
}

During an emergency_restart() flush at NBCON_PRIO_EMERGENCY, panic_on_this_cpu() is false, so a hostile takeover assigns con->pbufs to the emergency context while the interrupted NBCON_PRIO_NORMAL context may
still be formatting into or transmitting from con->pbufs:

CPU0 CPU1
nbcon_kthread_func()
nbcon_emit_next_record()

    nbcon_context_enter_unsafe()
    printk_get_next_message()
      /* writes con->pbufs */
                                      nbcon_atomic_flush_unsafe()
                                        /* PR_FLUSH_RESTART,
                                           NBCON_PRIO_EMERGENCY */
                                        nbcon_context_try_acquire()
                                          nbcon_context_try_acquire_hostile()
                                          ctxt->pbufs = con->pbufs
                                        nbcon_emit_next_record()
                                          printk_get_next_message()
                                            /* overwrites con->pbufs */
    con->write_thread()
      /* reads corrupted con->pbufs */
Quoted 7 lines
kernel/printk/nbcon.c +1 -0
@@ -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;
 

kernel/printk/nbcon.c enum nbcon_prio nbcon_get_default_prio(void) · line 1476

Quoted 10 lines
kernel/printk/nbcon.c +6 -0
 	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;

[Severity: Medium]
Can an NBCON_PRIO_EMERGENCY caller in nbcon_context_try_acquire_hostile() steal console ownership from a concurrent NBCON_PRIO_PANIC or NBCON_PRIO_EMERGENCY owner if con->nbcon_state changes before the cmpxchg?

When only NBCON_PRIO_PANIC could enter nbcon_context_try_acquire_hostile(), panic_on_other_cpu() blocked all other CPUs from acquiring the console or raising cur->prio or cur->req_prio. With NBCON_PRIO_EMERGENCY allowed to perform a hostile takeover while other CPUs are still active, the retry loop in nbcon_context_try_acquire_hostile() does not re-check cur after a failed cmpxchg:

kernel/printk/nbcon.c:nbcon_context_try_acquire_hostile() {
    ...
	WARN_ON_ONCE(ctxt->prio <= cur->prio || ctxt->prio <= cur->req_prio);
	WARN_ON_ONCE(cur->unsafe != true);

	do {
		new.atom = cur->atom;
		new.cpu			= cpu;
		new.prio		= ctxt->prio;
		new.unsafe		|= cur->unsafe_takeover;
		new.unsafe_takeover	|= cur->unsafe;

	} while (!nbcon_state_try_cmpxchg(con, cur, &new));
    ...
}

If the previous NBCON_PRIO_NORMAL owner releases the console and another CPU acquires it at NBCON_PRIO_PANIC or NBCON_PRIO_EMERGENCY before CPU1's first cmpxchg, the cmpxchg reloads cur and the next iteration overwrites new.prio with NBCON_PRIO_EMERGENCY:

CPU0 CPU1 CPU2
/* owns con at
NBCON_PRIO_NORMAL,
cur.unsafe == 1 */

                                /* NBCON_PRIO_EMERGENCY */
                                try_acquire_direct()
                                  -> -EBUSY
                                try_acquire_handover()
                                  -> -EBUSY
                                try_acquire_hostile()
nbcon_context_exit_unsafe()
nbcon_context_release()
                                                          panic()
                                                            try_acquire_direct()
                                                              /* acquires at
                                                                 NBCON_PRIO_PANIC */
                                  try_cmpxchg() fails,
                                  reloads cur.prio = PANIC
                                  try_cmpxchg() succeeds
                                  /* sets prio = EMERGENCY,
                                     stealing con from PANIC */
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
author maintainer
#7 lore raw

Re: [PATCH printk v3 5/7] console: Support unsafe flushing for panic/emergency restarts

Quoted 8 lines · On 2026-11-19, [email protected] wrote:
Quoted 8 lines
kernel/printk/nbcon.c +0 -0
index c3307b5b2c922..daf1fe8522117 100644
@@ -557,10 +557,6 @@ static int nbcon_context_try_acquire_hostile(struct nbcon_context *ctxt,
 	if (!ctxt->allow_unsafe_takeover)
 		return -EPERM;
 
Quoted 11 lines

kernel/printk/nbcon.c static int nbcon_context_try_acquire_hostile(struct nbcon_context *ctxt, · line 562

Quoted 7 lines
kernel/printk/nbcon.c +0 -4
-	/* 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.

[Severity: Medium]
Can an NBCON_PRIO_EMERGENCY hostile takeover clobber con->pbufs while the interrupted NBCON_PRIO_NORMAL owner is still using it?

Yes, this is not acceptable.

Quoted 7 lines
Quoted 7 lines
kernel/printk/nbcon.c +1 -0
@@ -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;
 
Quoted 10 lines

kernel/printk/nbcon.c enum nbcon_prio nbcon_get_default_prio(void) · line 1476

Quoted 10 lines
kernel/printk/nbcon.c +6 -0
 	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;

Yes, this is also not acceptable.

The current console ownership implementation relies on panic being the only context that can perform hostile takeovers. This is a fundamental rule in the implementation and cannot be changed without significant rework.

My idea is to reduce NBCON_EMERGENCY_RESTART_FLUSH policy to only mean unsafe printing and not hostile takeovers. I think this should be acceptable. The changes against this version:

1. Restore the WARN_ON_ONCE() in nbcon_context_try_acquire_hostile(). (And add more comments to it saying it must not be removed!)

2. Rename nbcon_allow_unsafe_takeover() to nbcon_allow_unsafe_usage() to give it a more appropriate name. The implementation stays as it is in this version.

3. Add an extra "&& extra panic_on_this_cpu()" when setting the @allow_unsafe_takeover argument because that argument is not just about unsafe printing, but also about hostile takeovers. So it looks like this:

allow_unsafe_usage = nbcon_allow_unsafe_usage(con, reason);

if ((flags & CON_NBCON_ATOMIC_UNSAFE) && !allow_unsafe_usage)

	continue;

nbcon_atomic_flush_pending_con(con, stop_seq,

	allow_unsafe_usage && panic_on_this_cpu());

This will allow unsafe flushing CON_NBCON_ATOMIC_UNSAFE consoles based on the policy while avoiding hostile takeovers in non-panic.

Thoughts?

John

author maintainer
#8 lore raw

PATCH v3 6/7 printk: Add unsafe flush points for panic+kexec and emergency_restart

In reply to John Ogness

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).

---
 kernel/crash_core.c | 1 +
 kernel/reboot.c     | 1 +
 2 files changed, 2 insertions(+)
kernel/crash_core.c +1 -0
index d0bd2d0cf8990..79a67956743ac 100644
@@ -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);
kernel/reboot.c +1 -0
index a93c23b9e1948..b26d131c54151 100644
@@ -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();
 }
Signature · 1 line
2.47.3
#9 lore raw

Re: [PATCH printk v3 6/7] printk: Add unsafe flush points for panic+kexec and emergency_restart

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] printk: nbcon: con->pbufs corruption on hostile takeover in emergency_restart() --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] printk: nbcon: con->pbufs corruption on hostile takeover in emergency_restart()
--

commit bc98e7fe84227c1b12eaff8bf1021023be510742
Author: John Ogness <[email protected]>

printk: Add unsafe flush points for panic+kexec and emergency_restart

This commit adds nbcon_atomic_flush_unsafe(PR_FLUSH_RESTART) calls to __crash_kexec() and emergency_restart() so that CON_NBCON_ATOMIC_UNSAFE consoles can flush when permitted by their unsafe policy.

kernel/reboot.c void emergency_restart(void) · line 108

Quoted 12 lines
kernel/reboot.c +1 -0
index a93c23b9e1948..b26d131c54151 100644
@@ -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();
 }

[Severity: Medium]
Can this corrupt con->pbufs when emergency_restart() performs a hostile takeover outside of panic()?

When emergency_restart() is invoked directly (for example via SysRq-b) rather than from panic(), panic_on_this_cpu() is false and other CPUs remain active. If a console permits unsafe takeover on PR_FLUSH_RESTART (such as netconsole with NBCON_EMERGENCY_RESTART_FLUSH) while an NBCON_PRIO_NORMAL context holds it inside an unsafe section, nbcon_context_try_acquire() performs an unsafe hostile takeover at NBCON_PRIO_EMERGENCY but still assigns con->pbufs instead of panic_nbcon_pbufs:

kernel/printk/nbcon.c:nbcon_context_try_acquire() {
	...
	/* Assign the appropriate buffer for this context. */
	if (panic_on_this_cpu())
		ctxt->pbufs = &panic_nbcon_pbufs;
	else
		ctxt->pbufs = con->pbufs;
	...
}

CPU0 (NBCON_PRIO_NORMAL) CPU1 (emergency_restart)
nbcon_emit_next_record()
nbcon_context_enter_unsafe()
printk_get_next_message()

    // writes con->pbufs->outbuf
                                  nbcon_atomic_flush_unsafe(PR_FLUSH_RESTART)
                                    ...
                                    nbcon_context_try_acquire_hostile()
                                    ctxt->pbufs = con->pbufs
                                    nbcon_emit_next_record()
                                      printk_get_next_message()
                                        // overwrites con->pbufs->outbuf
  con->write_thread()
    // reads corrupted con->pbufs->outbuf  <-- buffer corruption

Both contexts then concurrently read and write con->pbufs->outbuf and con->pbufs->scratchbuf. For netconsole_ext, overwriting the formatted message while send_msg_fragmented() is scanning wctxt->outbuf for the ';' header delimiter can trigger WARN_ON_ONCE(!msgbody) or match a ';' past MAX_PRINT_CHUNK, overflowing nt->buf[].

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
author maintainer
#10 lore raw

PATCH v3 7/7 netconsole: Perform unsafe flushing on panic and emergency restart

In reply to John Ogness

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.

---
 drivers/net/netconsole.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/net/netconsole.c +2 -0
index 267254f046de9..3300b2bbbd413 100644
@@ -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,
Signature · 1 line
2.47.3