[RFC] printk: Centralize nbcon sync console flush decision in console_flush_type
From: Petr Mladek
Date: Tue Jul 14 2026 - 07:53:01 EST
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@xxxxxxxx>
---
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