Re: [PATCH v3 10/11] printk: Try enable preferred consoles only when there are any
From: Petr Mladek
Date: Wed Jun 03 2026 - 12:20:50 EST
On Tue 2026-06-02 10:53:11, Petr Mladek wrote:
> try_enable_preferred_console() used to be always called because it
> had several hidden effects, namely:
>
> - enabled Braille consoles which were ignored by "preferred_dev_console"
> because they were not associated with /dev/console.
>
> - returned success when a console was pre-enabled using CON_ENABLED
> flag.
>
> - 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 2nd fix
> 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 | 46 +++++++++++++++++++++---------------------
> 1 file changed, 23 insertions(+), 23 deletions(-)
>
> diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c
> index a4eaf157cece..d62901a13bf7 100644
> --- a/kernel/printk/printk.c
> +++ b/kernel/printk/printk.c
> @@ -4099,35 +4099,35 @@ 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) {
> try_enable_default_console(newcon);
> }
This will require initializing err variable at the beginnit to prevent
returning a random value. I am going to fix it in v4.
Note that I am working on v4 because we want to fix handling pre-enabled
consoles in a first patch. It will prevent an out-of-bound access,
see
https://lore.kernel.org/all/7sq4tr2nmlz32tvkf6vpsghv6exvqfghsrlvywjcqihzsqqbf7@bspclmti5xg4/
> }
>
> - /* 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;
> -
> /*
> * Some consoles, such as pstore and netconsole, can be enabled even
> * without matching. Accept them at this stage when they had a chance
Best Regards,
Petr
PS: You might want to ignore v3. I am going to send v4 tomorrow after
I double check the reshufling of the patches.