From: Petr Mladek <pmladek@suse.com>
To: John Ogness <john.ogness@linutronix.de>
Cc: Sergey Senozhatsky <senozhatsky@chromium.org>,
Steven Rostedt <rostedt@goodmis.org>,
Andrew Murray <amurray@thegoodpenguin.co.uk>,
Chris Down <chris@chrisdown.name>,
linux-kernel@vger.kernel.org,
Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Subject: Re: [PATCH printk 1/3] printk: Introduce console sync mode
Date: Tue, 14 Jul 2026 16:23:31 +0200 [thread overview]
Message-ID: <alZGY5oT96CyBE9G@pathway.suse.cz> (raw)
In-Reply-To: <20260710144609.194487-2-john.ogness@linutronix.de>
On Fri 2026-07-10 16:51:51, John Ogness wrote:
> Sometimes it is desirable that console printing occurs synchronously
> in the context of the printk() caller. Introduce a "sync mode" for
> nbcon consoles that provide safe atomic_write() implementations. A
> new console flag CON_SYNC shows if a console is operating in this
> mode.
>
> When in sync mode, a console will flush directly within vprintk_emit()
> using the same mechanism as emergency printing. If the console
> hardware is currently owned by another context, the flushing will
> occur when that context releases ownership (just as is the case with
> non-panic emergency printing).
>
> If CON_SYNC is set for a legacy console driver or a nbcon console
> driver that does not provide a safe atomic_write(), the flag is
> cleared upon console registration and a message is logged that the
> console does not support sync mode.
>
> --- a/include/linux/console.h
> +++ b/include/linux/console.h
> @@ -180,6 +180,7 @@ static inline void con_debug_leave(void) { }
> * constraints.
> * @CON_NBCON_ATOMIC_UNSAFE: The write_atomic() callback is not safe and is
> * therefore only used by nbcon_atomic_flush_unsafe().
> + * @CON_SYNC: Print using write_atomic() from the printk() calling context.
> */
> enum cons_flags {
> CON_PRINTBUFFER = BIT(0),
> @@ -192,6 +193,7 @@ enum cons_flags {
> CON_SUSPENDED = BIT(7),
> CON_NBCON = BIT(8),
> CON_NBCON_ATOMIC_UNSAFE = BIT(9),
> + CON_SYNC = BIT(10),
My first feeling was that this was an overhead. But it might make
sense to keep only one console synchronous, ...
> diff --git a/kernel/printk/nbcon.c b/kernel/printk/nbcon.c
> index 4b03b019cd5ee..77abb98042648 100644
> --- a/kernel/printk/nbcon.c
> +++ b/kernel/printk/nbcon.c
> @@ -1919,8 +1942,15 @@ void nbcon_device_release(struct console *con)
> * usable throughout flushing.
> */
> cookie = console_srcu_read_lock();
> - printk_get_console_flush_type(&ft);
> - if (console_is_usable(con, console_srcu_read_flags(con), true) &&
> + flags = console_srcu_read_flags(con);
> + if (unlikely(flags & CON_SYNC)) {
> + /* Sync consoles will always perform nbcon_atomic flushing. */
> + memset(&ft, 0, sizeof(ft));
> + ft.nbcon_atomic = true;
They have to use the legacy loop when boot consoles are still
registered, see below.
> + } else {
> + printk_get_console_flush_type(&ft);
> + }
> + if (console_is_usable(con, flags, true) &&
> !ft.nbcon_offload &&
> prb_read_valid(prb, nbcon_seq_read(con), NULL)) {
> /*
> --- a/kernel/printk/printk.c
> +++ b/kernel/printk/printk.c
> @@ -2456,6 +2463,8 @@ asmlinkage int vprintk_emit(int facility, int level,
>
> if (ft.nbcon_atomic)
> nbcon_atomic_flush_pending();
> + else if (have_sync_console)
> + nbcon_sync_flush_pending();
Again. we should not do this when boot consoles are still
registered.
Also I think that we need to flush it in many more situations.
In general, it should be needed everywhere
printk_get_console_flush_type() is called for nbcon consoles
in a context with NBCON_PRIO_NORMAL, see below.
BTW: I wonder if this would work when more CPUs are adding
messages in parallel. I guess that they would fight for
the printing context. But I guess that they will
eventually finish the printing.
It is a bit different from the best effort approach used
by the legacy loop. Synchronous nbcon console will cause
that printk() might serialize CPUs...
It might be acceptable because this mode should not be
normally used. But we should at least document it.
> if (ft.nbcon_offload)
> nbcon_kthreads_wake();
I think that we should integrate this into
printk_get_console_flush_type(). We could solve the problem
with per-console flags the same way as for other flags,
by global variables:
+ have_nbcon_sync_console
+ have_nbcon_async_console
I wondered how it might look like. And I wanted to try AI.
I came with the following patch on top of this patchset.
The first version was generated by Claude, including the commit
message. Later, I modified the code a lot, and the commit message
a bit, by hand.
It is relatively complex. But I think that it is the prize for
the feature. And I think that the modification of
printk_get_console_flush_type() better fits the existing
design and helps to make sure that we handle it well
in all situations.
Feel free to integrate it into the first patch. Or maybe,
we could add the two have_nbcon* variables in a separate
patch, first, ...
Here is the patch:
From 4b1fb1806c69465dd71321f564fcdec700c13d88 Mon Sep 17 00:00:00 2001
From: Petr Mladek <pmladek@suse.com>
Date: Tue, 14 Jul 2026 13:53:01 +0200
Subject: [RFC] printk: Centralize nbcon sync console flush decision in
console_flush_type
The ad-hoc have_sync_console check in vprintk_emit() bypassed
printk_get_console_flush_type() to call nbcon_sync_flush_pending()
directly. This meant the sync-flush decision was scattered rather than
centralized, and was missing from all other call sites such as
console_cpu_notify(), __pr_flush(), printk_trigger_flush(), and
console_try_replay_all().
Introduce a new nbcon_atomic_sync field in struct console_flush_type to
represent the case where sync nbcon consoles (CON_SYNC) should be flushed
atomically in the caller's context. To support this, add a companion global
flag have_nbcon_async_console that tracks whether any non-sync nbcon
console is registered, maintained symmetrically with have_sync_console
in register_console() and unregister_console_locked().
Also rename the flag have_sync_console to have_nbcon_sync_console
to make it more symmetrical with have_nbcon_async_console.
Update printk_get_console_flush_type() NBCON_PRIO_NORMAL to set
nbcon_atomic_sync and nbcon_offload independently based on which console
types are present:
- kthreads running: nbcon_offload if non-sync nbcon consoles exist,
nbcon_atomic_sync if sync consoles exist (both can be set)
- kthreads not running: nbcon_atomic if non-sync consoles exist
(covers all nbcon including sync), else nbcon_atomic_sync
nbcon_atomic_sync is never set when nbcon_atomic is set because nbcon_atomic
already covers all nbcon consoles.
Update all printk_get_console_flush_type() call sites to handle
nbcon_atomic_sync by calling nbcon_sync_flush_pending(). Update the legacy
loop and legacy kthread skip conditions to correctly exclude only the console
type that is being handled atomically or offloaded, rather than all nbcon
consoles unconditionally.
Fix the loop condition in nbcon_atomic_flush_pending_con() to also loop
when nbcon_atomic_sync is set, because the printer thread never handles
sync consoles even when nbcon_offload is true.
Assisted-by: claude-sonnet-4.6
Signed-off-by: Petr Mladek <pmladek@suse.com>
---
kernel/printk/internal.h | 22 ++++++++++++---
kernel/printk/nbcon.c | 24 ++++++++--------
kernel/printk/printk.c | 60 ++++++++++++++++++++++++++++------------
3 files changed, 71 insertions(+), 35 deletions(-)
diff --git a/kernel/printk/internal.h b/kernel/printk/internal.h
index 1c1e0e97e3a0..3acea42b8720 100644
--- a/kernel/printk/internal.h
+++ b/kernel/printk/internal.h
@@ -169,19 +169,26 @@ static inline void nbcon_kthreads_wake(void) { }
extern bool have_boot_console;
extern bool have_nbcon_console;
extern bool have_legacy_console;
+extern bool have_nbcon_sync_console;
+extern bool have_nbcon_async_console;
extern bool legacy_allow_panic_sync;
/**
* struct console_flush_type - Define available console flush methods
* @nbcon_atomic: Flush directly using nbcon_atomic() callback
+ * @nbcon_atomic_sync: Flush sync consoles (CON_SYNC) using nbcon_atomic() callback
* @nbcon_offload: Offload flush to printer thread
* @legacy_direct: Call the legacy loop in this context
* @legacy_offload: Offload the legacy loop into IRQ or legacy thread
*
* Note that the legacy loop also flushes the nbcon consoles.
+ *
+ * @nbcon_atomic_sync is never set when @nbcon_atomic is set because
+ * @nbcon_atomic already covers all nbcon consoles including sync ones.
*/
struct console_flush_type {
bool nbcon_atomic;
+ bool nbcon_atomic_sync;
bool nbcon_offload;
bool legacy_direct;
bool legacy_offload;
@@ -200,10 +207,17 @@ static inline void printk_get_console_flush_type(struct console_flush_type *ft)
switch (nbcon_get_default_prio()) {
case NBCON_PRIO_NORMAL:
if (have_nbcon_console && !have_boot_console) {
- if (printk_kthreads_running && !console_irqwork_blocked)
- ft->nbcon_offload = true;
- else
- ft->nbcon_atomic = true;
+ if (printk_kthreads_running && !console_irqwork_blocked) {
+ if (have_nbcon_async_console)
+ ft->nbcon_offload = true;
+ if (have_nbcon_sync_console)
+ ft->nbcon_atomic_sync = true;
+ } else {
+ if (have_nbcon_async_console)
+ ft->nbcon_atomic = true;
+ else
+ ft->nbcon_atomic_sync = true;
+ }
}
/* Legacy consoles are flushed directly when possible. */
diff --git a/kernel/printk/nbcon.c b/kernel/printk/nbcon.c
index 77abb9804264..093683ee7262 100644
--- a/kernel/printk/nbcon.c
+++ b/kernel/printk/nbcon.c
@@ -1623,6 +1623,7 @@ static int __nbcon_atomic_flush_pending_con(struct console *con, u64 stop_seq)
static void nbcon_atomic_flush_pending_con(struct console *con, u64 stop_seq)
{
struct console_flush_type ft;
+ short flags;
int err;
again:
@@ -1642,10 +1643,11 @@ static void nbcon_atomic_flush_pending_con(struct console *con, u64 stop_seq)
/*
* If flushing was successful but more records are available, this
* context must flush those remaining records if the printer thread
- * is not available do it.
+ * is not available to do it or the console is explicitely synchronous.
*/
printk_get_console_flush_type(&ft);
- if (!ft.nbcon_offload &&
+ flags = console_srcu_read_flags(con);
+ if ((!ft.nbcon_offload || flags & CON_SYNC) &&
prb_read_valid(prb, nbcon_seq_read(con), NULL)) {
stop_seq = prb_next_reserve_seq(prb);
goto again;
@@ -1937,21 +1939,17 @@ void nbcon_device_release(struct console *con)
/*
* This context must flush any new records added while the console
- * was locked if the printer thread is not available to do it. The
- * console_srcu_read_lock must be taken to ensure the console is
- * usable throughout flushing.
+ * was locked if the printer thread is not available to do it or
+ * when the console is explicitely synchonous.
+ *
+ * The console_srcu_read_lock must be taken to ensure the console
+ * is usable throughout flushing.
*/
cookie = console_srcu_read_lock();
flags = console_srcu_read_flags(con);
- if (unlikely(flags & CON_SYNC)) {
- /* Sync consoles will always perform nbcon_atomic flushing. */
- memset(&ft, 0, sizeof(ft));
- ft.nbcon_atomic = true;
- } else {
- printk_get_console_flush_type(&ft);
- }
+ printk_get_console_flush_type(&ft);
if (console_is_usable(con, flags, true) &&
- !ft.nbcon_offload &&
+ (!ft.nbcon_offload || flags & CON_SYNC) &&
prb_read_valid(prb, nbcon_seq_read(con), NULL)) {
/*
* If nbcon_atomic flushing is not available, fallback to
diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c
index e53ac5554e5e..96da8ac04856 100644
--- a/kernel/printk/printk.c
+++ b/kernel/printk/printk.c
@@ -454,11 +454,18 @@ bool have_legacy_console;
bool have_nbcon_console;
/*
- * Specifies if a console is running in sync mode. If any consoles are running
- * in sync mode, printk() will immediately flush such consoles using their
- * write_atomic() callback.
+ * Specifies if an nbcon console is running in sync mode. If any consoles are
+ * running in sync mode, printk() will immediately flush such consoles using
+ * their write_atomic() callback.
*/
-bool have_sync_console;
+bool have_nbcon_sync_console;
+
+/*
+ * Specifies if an nbcon console without sync mode is registered. It there are
+ * any such consoles then printk() has to wake up the related kthreads when
+ * possible.
+ */
+bool have_nbcon_async_console;
/*
* Specifies if a boot console is registered. If boot consoles are present,
@@ -2463,7 +2470,7 @@ asmlinkage int vprintk_emit(int facility, int level,
if (ft.nbcon_atomic)
nbcon_atomic_flush_pending();
- else if (have_sync_console)
+ if (ft.nbcon_atomic_sync)
nbcon_sync_flush_pending();
if (ft.nbcon_offload)
@@ -2954,6 +2961,8 @@ static int console_cpu_notify(unsigned int cpu)
printk_get_console_flush_type(&ft);
if (ft.nbcon_atomic)
nbcon_atomic_flush_pending();
+ if (ft.nbcon_atomic_sync)
+ nbcon_sync_flush_pending();
if (ft.legacy_direct) {
if (console_trylock())
console_unlock();
@@ -3339,7 +3348,8 @@ static bool console_flush_one_record(bool do_cond_resched, u64 *next_seq, bool *
* nbcon consoles when the nbcon consoles cannot print via
* their atomic or threaded flushing.
*/
- if ((flags & CON_NBCON) && (ft.nbcon_atomic || ft.nbcon_offload))
+ if ((flags & CON_NBCON) &&
+ (ft.nbcon_atomic || ft.nbcon_atomic_sync || ft.nbcon_offload))
continue;
if (!console_is_usable(con, flags, !do_cond_resched))
@@ -3753,7 +3763,8 @@ static bool legacy_kthread_should_wakeup(void)
* consoles when the nbcon consoles cannot print via their
* atomic or threaded flushing.
*/
- if ((flags & CON_NBCON) && (ft.nbcon_atomic || ft.nbcon_offload))
+ if ((flags & CON_NBCON) &&
+ (ft.nbcon_atomic || ft.nbcon_atomic_sync || ft.nbcon_offload))
continue;
if (!console_is_usable(con, flags, false))
@@ -4239,11 +4250,13 @@ void register_console(struct console *newcon)
}
}
- if (newcon->flags & CON_SYNC)
- have_sync_console = true;
-
if (newcon->flags & CON_NBCON) {
have_nbcon_console = true;
+ if (newcon->flags & CON_SYNC)
+ have_nbcon_sync_console = true;
+ else
+ have_nbcon_async_console = true;
+
nbcon_seq_force(newcon, init_seq);
} else {
have_legacy_console = true;
@@ -4329,7 +4342,8 @@ static int unregister_console_locked(struct console *console)
bool found_legacy_con = false;
bool found_nbcon_con = false;
bool found_boot_con = false;
- bool found_sync_con = false;
+ bool found_nbcon_sync_con = false;
+ bool found_nbcon_async_con = false;
unsigned long flags;
struct console *c;
int res;
@@ -4394,13 +4408,15 @@ static int unregister_console_locked(struct console *console)
if (c->flags & CON_BOOT)
found_boot_con = true;
- if (c->flags & CON_NBCON)
+ if (c->flags & CON_NBCON) {
found_nbcon_con = true;
- else
+ if (c->flags & CON_SYNC)
+ found_nbcon_sync_con = true;
+ else
+ found_nbcon_async_con = true;
+ } else {
found_legacy_con = true;
-
- if (c->flags & CON_SYNC)
- found_sync_con = true;
+ }
}
if (!found_boot_con)
have_boot_console = found_boot_con;
@@ -4408,8 +4424,10 @@ static int unregister_console_locked(struct console *console)
have_legacy_console = found_legacy_con;
if (!found_nbcon_con)
have_nbcon_console = found_nbcon_con;
- if (!found_sync_con)
- have_sync_console = found_sync_con;
+ if (!found_nbcon_sync_con)
+ have_nbcon_sync_console = found_nbcon_sync_con;
+ if (!found_nbcon_async_con)
+ have_nbcon_async_console = found_nbcon_async_con;
/* @have_nbcon_console must be updated before calling nbcon_free(). */
if (console->flags & CON_NBCON)
@@ -4595,6 +4613,8 @@ static bool __pr_flush(struct console *con, int timeout_ms, bool reset_on_progre
printk_get_console_flush_type(&ft);
if (ft.nbcon_atomic)
nbcon_atomic_flush_pending();
+ if (ft.nbcon_atomic_sync)
+ nbcon_sync_flush_pending();
if (ft.legacy_direct) {
console_lock();
console_unlock();
@@ -4803,6 +4823,8 @@ void printk_trigger_flush(void)
printk_get_console_flush_type(&ft);
if (ft.nbcon_atomic)
nbcon_atomic_flush_pending();
+ if (ft.nbcon_atomic_sync)
+ nbcon_sync_flush_pending();
if (ft.nbcon_offload)
nbcon_kthreads_wake();
if (ft.legacy_direct) {
@@ -5155,6 +5177,8 @@ void console_try_replay_all(void)
__console_rewind_all();
if (ft.nbcon_atomic)
nbcon_atomic_flush_pending();
+ if (ft.nbcon_atomic_sync)
+ nbcon_sync_flush_pending();
if (ft.nbcon_offload)
nbcon_kthreads_wake();
if (ft.legacy_offload)
--
2.55.0
next prev parent reply other threads:[~2026-07-14 14:23 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-07-10 14:45 [PATCH printk 0/3] Introduce sync mode John Ogness
2026-07-10 14:45 ` [PATCH printk 1/3] printk: Introduce console " John Ogness
2026-07-14 14:23 ` Petr Mladek [this message]
2026-07-10 14:45 ` [PATCH printk 2/3] proc: Add console sync support for /proc/consoles John Ogness
2026-07-14 15:03 ` Petr Mladek
2026-07-10 14:45 ` [PATCH printk 3/3] printk: Support setting console sync mode via console= John Ogness
2026-07-10 21:05 ` [PATCH printk 0/3] Introduce sync mode 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=alZGY5oT96CyBE9G@pathway.suse.cz \
--to=pmladek@suse.com \
--cc=amurray@thegoodpenguin.co.uk \
--cc=chris@chrisdown.name \
--cc=gregkh@linuxfoundation.org \
--cc=john.ogness@linutronix.de \
--cc=linux-kernel@vger.kernel.org \
--cc=rostedt@goodmis.org \
--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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.