From: Sergey Senozhatsky <sergey.senozhatsky.work@gmail.com>
To: Petr Mladek <pmladek@suse.com>, Steven Rostedt <rostedt@goodmis.org>
Cc: Andrew Morton <akpm@linux-foundation.org>,
linux-kernel@vger.kernel.org,
Sergey Senozhatsky <sergey.senozhatsky@gmail.com>,
Sergey Senozhatsky <sergey.senozhatsky.work@gmail.com>
Subject: [RFC][PATCH 1/2] printk: lock console_sem before we unregister boot consoles
Date: Tue, 23 Apr 2019 15:25:10 +0900 [thread overview]
Message-ID: <20190423062511.1118-2-sergey.senozhatsky@gmail.com> (raw)
In-Reply-To: <20190423062511.1118-1-sergey.senozhatsky@gmail.com>
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.
Signed-off-by: Sergey Senozhatsky <sergey.senozhatsky@gmail.com>
---
kernel/printk/printk.c | 98 ++++++++++++++++++++++++------------------
1 file changed, 56 insertions(+), 42 deletions(-)
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
@@ -2605,6 +2605,48 @@ static int __init keep_bootcon_setup(char *str)
early_param("keep_bootcon", keep_bootcon_setup);
+static int __unregister_console(struct console *console)
+{
+ struct console *a, *b;
+ int res;
+
+ pr_info("%sconsole [%s%d] disabled\n",
+ (console->flags & CON_BOOT) ? "boot" : "",
+ console->name, console->index);
+
+ res = _braille_unregister_console(console);
+ if (res)
+ return res;
+
+ res = 1;
+ if (console_drivers == console) {
+ console_drivers = console->next;
+ res = 0;
+ } else if (console_drivers) {
+ for (a = console_drivers->next, b = console_drivers;
+ a; b = a, a = b->next) {
+ if (a == console) {
+ b->next = a->next;
+ res = 0;
+ break;
+ }
+ }
+ }
+
+ if (!res && (console->flags & CON_EXTENDED))
+ nr_ext_console_drivers--;
+
+ /*
+ * If this isn't the last console and it has CON_CONSDEV set, we
+ * need to set it on the next preferred console.
+ */
+ if (console_drivers != NULL && console->flags & CON_CONSDEV)
+ console_drivers->flags |= CON_CONSDEV;
+
+ console->flags &= ~CON_ENABLED;
+ return res;
+}
+
/*
* The console driver calls this routine during kernel initialization
* to register the console printing procedure with printk() and to
@@ -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.
*/
for_each_console(bcon)
if (bcon->flags & CON_BOOT)
- unregister_console(bcon);
+ __unregister_console(bcon);
+ console_unlock();
+ console_sysfs_notify();
}
}
EXPORT_SYMBOL(register_console);
int unregister_console(struct console *console)
{
- struct console *a, *b;
- int res;
-
- pr_info("%sconsole [%s%d] disabled\n",
- (console->flags & CON_BOOT) ? "boot" : "" ,
- console->name, console->index);
-
- res = _braille_unregister_console(console);
- if (res)
- return res;
+ int ret;
- res = 1;
console_lock();
- if (console_drivers == console) {
- console_drivers=console->next;
- res = 0;
- } else if (console_drivers) {
- for (a=console_drivers->next, b=console_drivers ;
- a; b=a, a=b->next) {
- if (a == console) {
- b->next = a->next;
- res = 0;
- break;
- }
- }
- }
-
- if (!res && (console->flags & CON_EXTENDED))
- nr_ext_console_drivers--;
-
- /*
- * If this isn't the last console and it has CON_CONSDEV set, we
- * need to set it on the next preferred console.
- */
- if (console_drivers != NULL && console->flags & CON_CONSDEV)
- console_drivers->flags |= CON_CONSDEV;
-
- console->flags &= ~CON_ENABLED;
+ ret = __unregister_console(console);
console_unlock();
console_sysfs_notify();
- return res;
+ return ret;
}
EXPORT_SYMBOL(unregister_console);
--
2.21.0
next prev parent reply other threads:[~2019-04-23 6:25 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-04-23 6:25 [RFC][PATCH 0/2] Access console drivers list under console_sem Sergey Senozhatsky
2019-04-23 6:25 ` Sergey Senozhatsky [this message]
2019-04-24 14:49 ` [RFC][PATCH 1/2] printk: lock console_sem before we unregister boot consoles Petr Mladek
2019-04-25 3:52 ` Sergey Senozhatsky
2019-04-25 6:43 ` Sergey Senozhatsky
2019-04-25 9:20 ` Petr Mladek
2019-04-25 16:05 ` Steven Rostedt
2019-04-25 7:50 ` Petr Mladek
2019-04-25 7:56 ` Sergey Senozhatsky
2019-04-25 9:37 ` Sergey Senozhatsky
2019-04-23 6:25 ` [RFC][PATCH 2/2] printk: take console_sem when accessing console drivers list Sergey Senozhatsky
2019-04-24 15:13 ` Petr Mladek
2019-04-25 5:19 ` Sergey Senozhatsky
2019-04-25 6:47 ` Sergey Senozhatsky
2019-04-25 8:53 ` Petr Mladek
2019-04-23 13:48 ` [RFC][PATCH 0/2] Access console drivers list under console_sem Steven Rostedt
2019-04-24 5:43 ` Sergey Senozhatsky
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20190423062511.1118-2-sergey.senozhatsky@gmail.com \
--to=sergey.senozhatsky.work@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=pmladek@suse.com \
--cc=rostedt@goodmis.org \
--cc=sergey.senozhatsky@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®