[PATCH v3 09/11] printk: Handle pre-enabled consoles in the top-level try_enable_console()

From: Petr Mladek

Date: Tue Jun 02 2026 - 04:57:02 EST


The function try_enable_preferred_console() currently has the
non-obvious side effect of returning success for consoles that are
already pre-enabled. This obscures the logic flow during console
registration.

Move the check for pre-enabled consoles directly into the top-level
try_enable_console(). This change makes the handling of pre-enabled
consoles explicit and easier to follow.

Furthermore, this separation lays the groundwork for future cleanups
where try_enable_preferred_console() can be restricted to cases where
an entry actually exists in the preferred_consoles[] array.

Possible behavior change:

try_enable_preferred_console() will newly be called also with
@user_specified parameter set to "false" when it failed with the "true"
variant. But it looks like the right way to do. It will allow to call
newcon->setup() when the console was preferred by some platform
specific code.

Signed-off-by: Petr Mladek <pmladek@xxxxxxxx>
---
kernel/printk/printk.c | 25 +++++++++++++------------
1 file changed, 13 insertions(+), 12 deletions(-)

diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c
index 97327c27436a..a4eaf157cece 100644
--- a/kernel/printk/printk.c
+++ b/kernel/printk/printk.c
@@ -3993,9 +3993,6 @@ static int console_call_setup(struct console *newcon, char *options)
* the newly registered console with any of the ones selected
* by either the command line or add_preferred_console() and
* setup/enable it.
- *
- * Care need to be taken with consoles that are statically
- * enabled such as netconsole
*/
static int __try_enable_preferred_console(struct console *newcon,
bool user_specified,
@@ -4048,14 +4045,6 @@ static int __try_enable_preferred_console(struct console *newcon,
return 0;
}

- /*
- * Some consoles, such as pstore and netconsole, can be enabled even
- * without matching. Accept the pre-enabled consoles only when match()
- * and setup() had a chance to be called.
- */
- if (newcon->flags & CON_ENABLED && pc->user_specified == user_specified)
- return 0;
-
return -ENOENT;
}

@@ -4135,7 +4124,19 @@ static int try_enable_console(struct console *newcon)
return err;

/* If not, try to match against the platform default(s) */
- return try_enable_preferred_console(newcon, false);
+ err = try_enable_preferred_console(newcon, false);
+ if (err != -ENOENT)
+ return err;
+
+ /*
+ * Some consoles, such as pstore and netconsole, can be enabled even
+ * without matching. Accept them at this stage when they had a chance
+ * to match() and call setup().
+ */
+ if (newcon->flags & CON_ENABLED)
+ err = 0;
+
+ return err;
}

/* Return the starting sequence number for a newly registered console. */
--
2.54.0