public inbox for linux-serial@vger.kernel.org
 help / color / mirror / Atom feed
From: John Ogness <john.ogness@linutronix.de>
To: Michael Cobb <mcobb@thegoodpenguin.co.uk>
Cc: pmladek@suse.com, rostedt@goodmis.org, senozhatsky@chromium.org,
	linux-serial@vger.kernel.org
Subject: Re: [PATCH RFC 0/3] printk: Don't flush messages using write_atomic during console registration if kthreads have not been started yet.
Date: Mon, 02 Jun 2025 17:46:57 +0206	[thread overview]
Message-ID: <84iklerw1i.fsf@jogness.linutronix.de> (raw)
In-Reply-To: <847c1xrzib.fsf@jogness.linutronix.de>

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

  reply	other threads:[~2025-06-02 15:40 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-05-14 17:35 [PATCH RFC 0/3] printk: Don't flush messages using write_atomic during console registration if kthreads have not been started yet Michael Cobb
2025-05-14 17:35 ` [PATCH RFC 1/3] " Michael Cobb
2025-05-14 17:35 ` [PATCH RFC 2/3] Reapply "serial: 8250: Switch to nbcon console" Michael Cobb
2025-05-14 17:35 ` [PATCH RFC 3/3] Reapply "serial: 8250: Revert "drop lockdep annotation from serial8250_clear_IER()"" Michael Cobb
2025-05-16  9:44 ` [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-05-30 10:38   ` Michael Cobb
2025-05-31  7:49     ` John Ogness
2025-06-02 15:40       ` John Ogness [this message]
2025-06-03 14:40         ` Petr Mladek
2025-06-03 16:09           ` John Ogness
2025-06-03 16:38             ` Michael Cobb

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=84iklerw1i.fsf@jogness.linutronix.de \
    --to=john.ogness@linutronix.de \
    --cc=linux-serial@vger.kernel.org \
    --cc=mcobb@thegoodpenguin.co.uk \
    --cc=pmladek@suse.com \
    --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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox