[PATCH RFC 1/3] printk: Don't flush messages using write_atomic during console registration if kthreads have not been started yet.

Michael Cobb
2025-05-14T17:35:42Z

Signed-Off-By: Michael Cobb <mcobb@thegoodpenguin.co.uk>
---
kernel/printk/internal.h | 4 +++-
kernel/printk/nbcon.c | 2 ++
kernel/printk/printk.c | 3 +++
3 files changed, 8 insertions(+), 1 deletion(-)

diff --git a/kernel/printk/internal.h b/kernel/printk/internal.h
index 48a24e7b309d..e31ecb2fc81c 100644
--- a/kernel/printk/internal.h
+++ b/kernel/printk/internal.h
@@ -64,6 +64,7 @@ struct dev_printk_info;

extern struct printk_ringbuffer *prb;
extern bool printk_kthreads_running;
+extern bool printk_kthreads_pending_start;
extern bool debug_non_panic_cpus;

__printf(4, 0)
@@ -180,6 +181,7 @@ static inline void nbcon_kthread_wake(struct console *con)
#define PRINTKRB_RECORD_MAX 0

#define printk_kthreads_running (false)
+#define printk_kthreads_pending_start (false)

/*
* In !PRINTK builds we still export console_sem
@@ -240,7 +242,7 @@ 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)
+ if (printk_kthreads_running || printk_kthreads_pending_start)
ft->nbcon_offload = true;
else
ft->nbcon_atomic = true;
diff --git a/kernel/printk/nbcon.c b/kernel/printk/nbcon.c
index fd12efcc4aed..adb53de5c2f2 100644
--- a/kernel/printk/nbcon.c
+++ b/kernel/printk/nbcon.c
@@ -1707,6 +1707,8 @@ bool nbcon_alloc(struct console *con)
con->pbufs = NULL;
return false;
}
+ } else {
+ printk_kthreads_pending_start = true;
}
}

diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c
index 1eea80d0648e..c71bf5c228f2 100644
--- a/kernel/printk/printk.c
+++ b/kernel/printk/printk.c
@@ -501,6 +501,8 @@ static bool syslog_time;
/* True when _all_ printer threads are available for printing. */
bool printk_kthreads_running;

+bool printk_kthreads_pending_start = false;
+
struct latched_seq {
seqcount_latch_t latch;
u64 val[2];
@@ -3758,6 +3760,7 @@ static void printk_kthreads_check_locked(void)
unregister_console_locked(con);
}

+ printk_kthreads_pending_start = false;
printk_kthreads_running = true;
}

--
2.45.2