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 3/6] printk: Evaluate flush policy in nbcon_allow_unsafe_takeover()
Date: Mon, 9 Nov 2026 13:37:20 +0106 [thread overview]
Message-ID: <20261109123127.489676-4-john.ogness@linutronix.de> (raw)
In-Reply-To: <20261109123127.489676-1-john.ogness@linutronix.de>
Currently nbcon_allow_unsafe_takeover() is a hack, relying on the
temporary setting of a global variable @panic_nbcon_allow_unsafe_takeover
as a gate to determine if unsafe flushing is allowed. This hack works
because unsafe flushing is only performed at the end of panic().
However, this hack is _not_ sufficient if unsafe flushing is to be
allowed in other contexts.
Allow unsafe flushing in other contexts by modifying
nbcon_allow_unsafe_takeover() to take a console a flush reason as input
and, based on the unsafe flush policies of that console, determine if
unsafe flushing is allowed. This also allows the global variable
@panic_nbcon_allow_unsafe_takeover to be removed.
Remove the hard-coded policy for CON_NBCON_ATOMIC_UNSAFE from
console_is_usable() and implement that policy logic within
nbcon_allow_unsafe_takeover().
Note that this changes the flushing flow for CON_NBCON_ATOMIC_UNSAFE
consoles. Rather than console_is_usable() reporting that the console
is not usable, __nbcon_atomic_flush_pending_con() will report that it
is unable to acquire ownership.
Signed-off-by: John Ogness <john.ogness@linutronix.de>
---
include/linux/console.h | 11 ++----
kernel/printk/nbcon.c | 79 ++++++++++++++++++++++++++++++++---------
2 files changed, 64 insertions(+), 26 deletions(-)
diff --git a/include/linux/console.h b/include/linux/console.h
index 0841bf5e88aac..8f5097e7f4741 100644
--- a/include/linux/console.h
+++ b/include/linux/console.h
@@ -179,7 +179,8 @@ static inline void con_debug_leave(void) { }
* @CON_NBCON: Console can operate outside of the legacy style console_lock
* constraints.
* @CON_NBCON_ATOMIC_UNSAFE: The write_atomic() callback is not safe and is
- * therefore only used by nbcon_atomic_flush_unsafe().
+ * therefore only called when allowed by
+ * nbcon_allow_unsafe_takeover().
*/
enum cons_flags {
CON_PRINTBUFFER = BIT(0),
@@ -637,7 +638,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);
@@ -670,13 +670,6 @@ static inline bool console_is_usable(struct console *con, short flags, bool use_
/* The write_atomic() callback is optional. */
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())
- return false;
}
/*
diff --git a/kernel/printk/nbcon.c b/kernel/printk/nbcon.c
index 935fd6b86bac7..513bfd399858c 100644
--- a/kernel/printk/nbcon.c
+++ b/kernel/printk/nbcon.c
@@ -1453,24 +1453,60 @@ 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
*/
-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;
+ int unsafe_flags = con->unsafe_flush_flags;
+ bool in_panic = panic_on_this_cpu();
+ bool allow = false;
+
+ switch (reason) {
+ case PR_FLUSH_RUNTIME:
+ if (in_panic) {
+ if (unsafe_flags & NBCON_PANIC_PRINTK)
+ allow = true;
+
+ } else {
+ /* Only when the kthreads have been shutdown. */
+ if ((unsafe_flags & NBCON_NORMAL_NOTHREAD) &&
+ !con->kthread &&
+ system_state > SYSTEM_RUNNING) {
+ allow = true;
+ }
+ }
+ break;
+ case PR_FLUSH_RESTART:
+ /* kexec, reboot */
+
+ if (in_panic) {
+ if (unsafe_flags & NBCON_PANIC_FLUSH)
+ allow = true;
+
+ } else {
+ /* Printing kthreads not reliable. */
+ if (unsafe_flags & NBCON_NORMAL_NOTHREAD)
+ allow = true;
+ }
+ break;
+ case PR_FLUSH_HANG:
+ /* Always unsafe flush before hang. */
+ allow = true;
+ break;
+ case PR_FLUSH_KDB:
+ if (unsafe_flags & NBCON_KDB)
+ allow = true;
+ break;
+ }
+
+ return allow;
}
/**
@@ -1572,7 +1608,12 @@ static int __nbcon_atomic_flush_pending_con(struct console *con, enum pr_flush_r
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 = nbcon_allow_unsafe_takeover(con, reason);
+
+ if ((console_srcu_read_flags(con) & CON_NBCON_ATOMIC_UNSAFE) &&
+ !ctxt->allow_unsafe_takeover) {
+ return -EPERM;
+ }
while (nbcon_seq_read(con) < stop_seq) {
/*
@@ -1707,9 +1748,7 @@ void nbcon_atomic_flush_pending(enum pr_flush_reason reason)
*/
void nbcon_atomic_flush_unsafe(void)
{
- panic_nbcon_allow_unsafe_takeover = true;
__nbcon_atomic_flush_pending(PR_FLUSH_HANG, prb_next_reserve_seq(prb));
- panic_nbcon_allow_unsafe_takeover = false;
}
/**
@@ -1976,8 +2015,14 @@ bool nbcon_kdb_try_acquire(struct console *con,
struct nbcon_context *ctxt = &ACCESS_PRIVATE(wctxt, ctxt);
memset(ctxt, 0, sizeof(*ctxt));
- ctxt->console = con;
- ctxt->prio = NBCON_PRIO_EMERGENCY;
+ ctxt->console = con;
+ ctxt->prio = NBCON_PRIO_EMERGENCY;
+ ctxt->allow_unsafe_takeover = nbcon_allow_unsafe_takeover(con, PR_FLUSH_KDB);
+
+ if ((console_srcu_read_flags(con) & CON_NBCON_ATOMIC_UNSAFE) &&
+ !ctxt->allow_unsafe_takeover) {
+ return false;
+ }
if (!nbcon_context_try_acquire(ctxt, false))
return false;
--
2.47.3
next prev parent reply other threads:[~2026-11-09 12:31 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-09 12:31 [PATCH printk 0/6] Introduce policies for unsafe flushing John Ogness
2026-11-09 12:31 ` [PATCH printk 1/6] printk: Introduce and specify flush reasons John Ogness
2026-11-09 12:31 ` [PATCH printk 2/6] console: Introduce unsafe flush policy flags John Ogness
2026-11-09 12:31 ` John Ogness [this message]
2026-11-09 12:31 ` [PATCH printk 4/6] printk: Remove nbcon_atomic_flush_unsafe() John Ogness
2026-11-09 12:31 ` [PATCH printk 5/6] printk: Add panic+kexec flush point John Ogness
2026-11-09 12:31 ` [PATCH printk 6/6] netconsole: Always perform unsafe flushing on panic/kdb/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=20261109123127.489676-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®