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

John Ogness
2025-06-02T15:40:59Z

On 2025-05-31, John Ogness <john.ogness@linutronix.de> wrote:
> diff --git a/kernel/printk/internal.h b/kernel/printk/internal.h
> index 48a24e7b309db..7462a6d179850 100644
> --- a/kernel/printk/internal.h
> +++ b/kernel/printk/internal.h
> @@ -240,7 +240,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/printk.c b/kernel/printk/printk.c
> index 1eea80d0648ed..9c0378dc88c4c 100644
> --- a/kernel/printk/printk.c
> +++ b/kernel/printk/printk.c
> @@ -4072,6 +4072,14 @@ void register_console(struct console *newcon)
> if (newcon->flags & CON_BOOT)
> have_boot_console = true;
>
> + /*
> + * If this is the first console, avoid flushing the backlog
> + * until the printing kthread has had a chance to start via
> + * printk_kthreads_check_locked() below.
> + */
> + if (hlist_empty(&console_list) && (newcon->flags & CON_NBCON))
> + printk_kthread_pending_start = true;
> +
> /*
> * If another context is actively using the hardware of this new
> * console, it will not be aware of the nbcon synchronization. This
> @@ -4115,6 +4123,10 @@ void register_console(struct console *newcon)
>
> console_sysfs_notify();
>
> + /* Changed console list, may require printer threads to start/stop. */
> + printk_kthreads_check_locked();
> + printk_kthread_pending_start = false;
> +
> /*
> * By unregistering the bootconsoles after we enable the real console
> * we get the "console xxx enabled" message on all the consoles -
> @@ -4133,9 +4145,6 @@ void register_console(struct console *newcon)
> unregister_console_locked(con);
> }
> }
> -
> - /* Changed console list, may require printer threads to start/stop. */
> - printk_kthreads_check_locked();

This won't work. printk_kthreads_check_locked() must come after the
boot-console unregister-loop. The kthreads do not start if boot consoles
are registered.

I spent some time thinking about how to get a clean implementation of
this optimization. It is tricky because:

- If the console is registered before printk_kthreads_ready=true, then
the optimization cannot be used (i.e. the console must do the atomic
flushing).

- If the console fails to start its kthread, then it must do the atomic
flush when unregistering.

Perhaps something like this:

diff --git a/kernel/printk/internal.h b/kernel/printk/internal.h
index 48a24e7b309db..7462a6d179850 100644
--- a/kernel/printk/internal.h
+++ b/kernel/printk/internal.h
@@ -240,7 +240,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/printk.c b/kernel/printk/printk.c
index 1eea80d0648ed..d47e8076152e7 100644
--- a/kernel/printk/printk.c
+++ b/kernel/printk/printk.c
@@ -3754,8 +3754,18 @@ static void printk_kthreads_check_locked(void)
if (!(con->flags & CON_NBCON))
continue;

- if (!nbcon_kthread_create(con))
+ if (!nbcon_kthread_create(con)) {
+ /*
+ * The kthread failed to start so it is no longer
+ * allowed to use the boot optimization and expect
+ * offloading to take over. The backlog will be
+ * flushed using atomic printing during unregister.
+ */
+ if (printk_kthread_pending_start)
+ printk_kthread_pending_start = false;
+
unregister_console_locked(con);
+ }
}

printk_kthreads_running = true;
@@ -4072,6 +4082,18 @@ void register_console(struct console *newcon)
if (newcon->flags & CON_BOOT)
have_boot_console = true;

+ /*
+ * Begin boot optimization.
+ * If this is the first console and kthreads are available, avoid
+ * flushing the backlog until the printing kthread has had a chance
+ * to start via printk_kthreads_check_locked() below.
+ */
+ if (hlist_empty(&console_list) &&
+ (newcon->flags & CON_NBCON) &&
+ printk_kthreads_ready) {
+ printk_kthread_pending_start = true;
+ }
+
/*
* If another context is actively using the hardware of this new
* console, it will not be aware of the nbcon synchronization. This
@@ -4136,6 +4158,13 @@ void register_console(struct console *newcon)

/* Changed console list, may require printer threads to start/stop. */
printk_kthreads_check_locked();
+
+ /*
+ * End boot optimization.
+ * The printing kthread had a chance to start.
+ */
+ if (printk_kthread_pending_start)
+ printk_kthread_pending_start = false;
unlock:
console_list_unlock();
}

John