[PATCHv2 3/4] printk: factor out register_console() code
From: Sergey Senozhatsky
Date: Fri Apr 26 2019 - 01:33:26 EST
We need to take console_sem lock when we iterate console drivers
list. Otherwise, another CPU can concurrently modify console drivers
list or console drivers. Current register_console() has several
race conditions - for_each_console() must be done under console_sem.
Factor out console registration code and hold console_sem throughout
entire registration process. Note that we need to unlock console_sem
and lock it again after we added new console to the list and before
we unregister boot consoles. This might look a bit weird, but this
is how we print pending logbuf messages to all registered and
available consoles.
Signed-off-by: Sergey Senozhatsky <sergey.senozhatsky@xxxxxxxxx>
---
kernel/printk/printk.c | 15 ++++++++++-----
1 file changed, 10 insertions(+), 5 deletions(-)
diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c
index 3ac71701afa3..3b36e26d4a51 100644
--- a/kernel/printk/printk.c
+++ b/kernel/printk/printk.c
@@ -2666,7 +2666,7 @@ static int __unregister_console(struct console *console)
* - Once a "real" console is registered, any attempt to register a
* bootconsoles will be rejected
*/
-void register_console(struct console *newcon)
+static void __register_console(struct console *newcon)
{
int i;
unsigned long flags;
@@ -2771,7 +2771,6 @@ void register_console(struct console *newcon)
* Put this console in the list - keep the
* preferred driver at the head of the list.
*/
- console_lock();
if ((newcon->flags & CON_CONSDEV) || console_drivers == NULL) {
newcon->next = console_drivers;
console_drivers = newcon;
@@ -2818,6 +2817,7 @@ void register_console(struct console *newcon)
console_unlock();
console_sysfs_notify();
+ console_lock();
if (keep_bootcon)
return;
@@ -2830,14 +2830,19 @@ void register_console(struct console *newcon)
* went to the bootconsole (that they do not see on the real console)
*/
if (bcon && (newcon->flags & (CON_CONSDEV|CON_BOOT)) == CON_CONSDEV) {
- console_lock();
for_each_console(bcon)
if (bcon->flags & CON_BOOT)
__unregister_console(bcon);
- console_unlock();
- console_sysfs_notify();
}
}
+
+void register_console(struct console *newcon)
+{
+ console_lock();
+ __register_console(newcon);
+ console_unlock();
+ console_sysfs_notify();
+}
EXPORT_SYMBOL(register_console);
int unregister_console(struct console *console)
--
2.21.0