Re: [RFC][PATCH 1/2] printk: lock console_sem before we unregister boot consoles

From: Petr Mladek
Date: Wed Apr 24 2019 - 10:49:30 EST


On Tue 2019-04-23 15:25:10, Sergey Senozhatsky wrote:
> The following pattern is not completely safe:
>
> for_each_console(bcon)
> if (bcon->flags & CON_BOOT)
> unregister_console(bcon);
>
> Because, in theory, console drivers list and console drivers
> can be modified concurrently from another CPU. Take console_sem
> lock, which protects console drivers list and console drivers,
> before we start iterating console drivers list.
>
> diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c
> index 17102fd4c136..b0e361ca1bea 100644
> --- a/kernel/printk/printk.c
> +++ b/kernel/printk/printk.c
> @@ -2777,62 +2819,34 @@ void register_console(struct console *newcon)
> pr_info("%sconsole [%s%d] enabled\n",
> (newcon->flags & CON_BOOT) ? "boot" : "" ,
> newcon->name, newcon->index);
> - if (bcon &&
> - ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV) &&
> - !keep_bootcon) {
> - /* We need to iterate through all boot consoles, to make
> +
> + if (keep_bootcon)
> + return;
> +
> + if (bcon && (newcon->flags & (CON_CONSDEV|CON_BOOT)) == CON_CONSDEV) {
> + console_lock();
> + /*
> + * We need to iterate through all boot consoles, to make
> * sure we print everything out, before we unregister them.
> */

I wondered if moving the console locking could break the above
statement.

It seems that the comment has been invalid since the commit
8259cf4342029aad37660e ("printk: Ensure that "console
enabled" messages are printed on the console").

Could we remove it in this patch? It touches it indirectly anyway.

Otherwise, the patch looks fine to me:

Reviewed-by: Petr Mladek <pmladek@xxxxxxxx>

> for_each_console(bcon)
> if (bcon->flags & CON_BOOT)
> - unregister_console(bcon);
> + __unregister_console(bcon);
> + console_unlock();
> + console_sysfs_notify();
> }
> }
> EXPORT_SYMBOL(register_console);

Best Regards,
Petr