All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v1] printk: Unconditionally unregister boot consoles when any real console appears
@ 2026-05-18  8:42 Xiaochun Li
  2026-05-18 10:16 ` John Ogness
  0 siblings, 1 reply; 5+ messages in thread
From: Xiaochun Li @ 2026-05-18  8:42 UTC (permalink / raw)
  To: pmladek; +Cc: rostedt, john.ogness, senozhatsky, linux-kernel, Xiaochun Li

The design of kernel message printing ensures that boot consoles are
temporary and should be unregistered once a real console appears,
preventing duplicate output without any loss of kernel messages.

But currently, when multiple "console=" parameters are specified
(e.g., console=ttyS0 console=ttyS1) together with a boot console
(earlyprintk=ttyS0 and/or earlycon=uart,io,0x3f8), the boot console
remains active indefinitely. As a result, after the real console
active, each kernel message line is printed twice consecutively that
is not as expected.

Currently, register_console() unregisters boot consoles only when the
newly registered console has CON_CONSDEV (i.e., it is the preferred
console). However, when multiple "console=" arguments are given for
the same driver (e.g., 8250), the try_enable_preferred_console()
matches only the first console_cmdline entry and returns immediately.
The enabled real console (ttyS0) is therefore not the preferred one
and does not set CON_CONSDEV, because preferred_console points to the
last entry (ttyS1). Consequently, the boot console is never unregistered.

Fix this issue by removing the CON_CONSDEV requirement and
unregistering all boot consoles whenever any real console
(i.e., any console without CON_BOOT flag) is registered, unless
keep_bootcon or keep option append to earlyprintk is specified.

This simplification relies on the existing printk buffer replay
mechanism: when a real console is registered, its internal sequence
number ensures it will output all messages from the entire kernel
log, including those printed before it was enabled. Therefore,
no messages are lost even if boot consoles are removed early.

Behavior comparison:

Command line: earlyprintk=ttyS0 console=ttyS0 console=ttyS1
                (ttyS1 is preferred)

- Before fix: ttyS0 is enabled but lacks CON_CONSDEV -> the boot console
	is not unregistered.
- After fix: ttyS0 is enabled -> the boot console is unconditionally
	unregistered.

Command line: earlyprintk=ttyS0 console=ttyS1 console=ttyS0
        (ttyS0 is preferred, order swapped)

- Before fix: ttyS1 is enabled but lacks CON_CONSDEV -> the boot
	console is kept.
- After fix:  ttyS1 is enabled -> the boot console is still unregistered.
        (ttyS1 will also replay the logs on printk buffer if COM2
         was configured correctly, boot console on ttyS0 is
         no longer needed.)

Command line: earlyprintk=ttyS0 console=tty0 console=ttyS0 console=ttyS1
        (ttyS1 is the preferred console)

- Before fix: tty0 (VGA) registers first -> the boot console remains.
        ttyS0 (8250) registers later -> the boot console still remains.

- After fix: tty0 (VGA) registers first -> the boot console
	is unregistered.
        ttyS0 registers later -> CON_PRINTBUFFER is set ->
        it replays the entire ring buffer.

Command line: earlyprintk=ttyS0 keep_bootcon console=ttyS0
or Command line: earlyprintk=ttyS0,keep console=ttyS0

- Before and after: the boot consoles remain.

Signed-off-by: Xiaochun Li <lixiaochun@open-hieco.net>
---
 kernel/printk/printk.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c
index 0323149548f6..920e27f63003 100644
--- a/kernel/printk/printk.c
+++ b/kernel/printk/printk.c
@@ -4207,8 +4207,7 @@ void register_console(struct console *newcon)
 	 */
 	con_printk(KERN_INFO, newcon, "enabled\n");
 	if (bootcon_registered &&
-	    ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV) &&
-	    !keep_bootcon) {
+	    !(newcon->flags & CON_BOOT) && !keep_bootcon) {
 		struct hlist_node *tmp;
 
 		hlist_for_each_entry_safe(con, tmp, &console_list, node) {
-- 
2.18.2


^ permalink raw reply related	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-05-25  1:38 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-05-18  8:42 [PATCH v1] printk: Unconditionally unregister boot consoles when any real console appears Xiaochun Li
2026-05-18 10:16 ` John Ogness
2026-05-20  2:14   ` Xiaochun Li
2026-05-22 14:46   ` Petr Mladek
2026-05-25  1:38     ` Xiaochun Li

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.