[PATCH v5 16/16] printk: Try enable preferred consoles only when there are any
From: Petr Mladek
Date: Tue Sep 29 2026 - 08:13:51 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 | 26 +++++++++++++++++---------
1 file changed, 17 insertions(+), 9 deletions(-)
diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c
index 1eb749da9e24..06c49042dd8f 100644
--- a/kernel/printk/printk.c
+++ b/kernel/printk/printk.c
@@ -4085,7 +4085,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.
@@ -4124,16 +4124,24 @@ static int try_enable_console(struct console *newcon)
if (err != -ENOENT)
return err;
}
+ } else {
+ /* 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;
}
- /* 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. This is just a paranoid check that all
+ * branches above returned on error.
+ */
+ if (WARN_ON(err != -ENOENT))
return err;
/*
--
2.55.0