[PATCH v4 11/11] printk: Try enable preferred consoles only when there are any
From: Petr Mladek
Date: Thu Jun 04 2026 - 06:35:57 EST
try_enable_preferred_console() used to be always called because it
had several hidden effects, namely:
- returned success when a console was pre-enabled using CON_ENABLED
flag.
- enabled Braille consoles which were ignored by "preferred_dev_console"
because they were not associated with /dev/console.
- returned success when a console was enabled by default because
try_enable_default_console() did not return success.
The first two hidden effects were removed in previous patches. The already
fixed handling of pre-enabled consoles actually helps even the 3rd case.
try_enable_default_console() sets CON_ENABLED flag on success and
is later handled as pre-enabled.
Prevent any future hidden effects and call try_enable_preferred_console()
only when some console is preferred.
No behavior change.
Signed-off-by: Petr Mladek <pmladek@xxxxxxxx>
---
kernel/printk/printk.c | 51 ++++++++++++++++++++++++------------------
1 file changed, 29 insertions(+), 22 deletions(-)
diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c
index b0df4a23252d..21a9c78f4deb 100644
--- a/kernel/printk/printk.c
+++ b/kernel/printk/printk.c
@@ -4084,7 +4084,7 @@ static int try_enable_default_console(struct console *newcon)
static int try_enable_console(struct console *newcon)
{
- int err;
+ int err = -ENOENT;
/*
* First, try to enable the console driver as a Braille console.
@@ -4104,19 +4104,29 @@ static int try_enable_console(struct console *newcon)
return err;
}
- /*
- * See if we want to enable this console driver by default.
- *
- * Nope when a console is preferred by the command line, device
- * tree, or SPCR.
- *
- * The first real console with tty binding (driver) wins. More
- * consoles might get enabled before the right one is found.
- *
- * Note that a console with tty binding will have CON_CONSDEV
- * flag set and will be first in the list.
- */
- if (preferred_dev_console < 0) {
+ if (preferred_dev_console >= 0) {
+ /* See if this console matches one we selected on the command line */
+ err = try_enable_preferred_console(newcon, true);
+ if (err != -ENOENT)
+ return err;
+
+ /* If not, try to match against the platform default(s) */
+ err = try_enable_preferred_console(newcon, false);
+ if (err != -ENOENT)
+ return err;
+ } else {
+ /*
+ * See if we want to enable this console driver by default.
+ *
+ * Nope when a console is preferred by the command line, device
+ * tree, or SPCR.
+ *
+ * The first real console with tty binding (driver) wins. More
+ * consoles might get enabled before the right one is found.
+ *
+ * Note that a console with tty binding will have CON_CONSDEV
+ * flag set and will be first in the list.
+ */
if (hlist_empty(&console_list) || !console_first()->device ||
console_first()->flags & CON_BOOT) {
err = try_enable_default_console(newcon);
@@ -4125,14 +4135,11 @@ static int try_enable_console(struct console *newcon)
}
}
- /* See if this console matches one we selected on the command line */
- err = try_enable_preferred_console(newcon, true);
- if (err != -ENOENT)
- return err;
-
- /* If not, try to match against the platform default(s) */
- err = try_enable_preferred_console(newcon, false);
- if (err != -ENOENT)
+ /*
+ * Make sure that pre-enabled consoles won't get registered when
+ * something went wrong.
+ */
+ if (WARN_ON(err != -ENOENT))
return err;
/*
--
2.54.0