printk, xen: fbfront: create/use safe function for forcing preferred
authorJohn Ogness <john.ogness@linutronix.de>
Wed, 16 Nov 2022 16:21:44 +0000 (17:27 +0106)
committerPetr Mladek <pmladek@suse.com>
Fri, 2 Dec 2022 10:25:02 +0000 (11:25 +0100)
With commit 9e124fe16ff2("xen: Enable console tty by default in domU
if it's not a dummy") a hack was implemented to make sure that the
tty console remains the console behind the /dev/console device. The
main problem with the hack is that, after getting the console pointer
to the tty console, it is assumed the pointer is still valid after
releasing the console_sem. This assumption is incorrect and unsafe.

Make the hack safe by introducing a new function
console_force_preferred_locked() and perform the full operation
under the console_list_lock.

Signed-off-by: John Ogness <john.ogness@linutronix.de>
Reviewed-by: Petr Mladek <pmladek@suse.com>
Signed-off-by: Petr Mladek <pmladek@suse.com>
Link: https://lore.kernel.org/r/20221116162152.193147-33-john.ogness@linutronix.de
drivers/video/fbdev/xen-fbfront.c
include/linux/console.h
kernel/printk/printk.c

index 4d2694d904aab6b065b9f64c5530693a5b11e77d..8752d389e3823f5ec81ee5fc35d268b87b22e0de 100644 (file)
@@ -504,18 +504,14 @@ static void xenfb_make_preferred_console(void)
        if (console_set_on_cmdline)
                return;
 
-       console_lock();
+       console_list_lock();
        for_each_console(c) {
                if (!strcmp(c->name, "tty") && c->index == 0)
                        break;
        }
-       console_unlock();
-       if (c) {
-               unregister_console(c);
-               c->flags |= CON_CONSDEV;
-               c->flags &= ~CON_PRINTBUFFER; /* don't print again */
-               register_console(c);
-       }
+       if (c)
+               console_force_preferred_locked(c);
+       console_list_unlock();
 }
 
 static int xenfb_resume(struct xenbus_device *dev)
index f716e1dd9eaf2ceeadf2f7e817ce453e595a0419..9cea254b34b8eac4420532eba870ce24cfee6937 100644 (file)
@@ -291,6 +291,7 @@ enum con_flush_mode {
 };
 
 extern int add_preferred_console(char *name, int idx, char *options);
+extern void console_force_preferred_locked(struct console *con);
 extern void register_console(struct console *);
 extern int unregister_console(struct console *);
 extern void console_lock(void);
index e58a6ccb945cafdbd2ef8259cd8826b967ce7de9..bda9e8ec6817bca2548136a193318c6e33dbcdb2 100644 (file)
@@ -248,9 +248,10 @@ int devkmsg_sysctl_set_loglvl(struct ctl_table *table, int write,
 void console_list_lock(void)
 {
        /*
-        * In unregister_console(), synchronize_srcu() is called with the
-        * console_list_lock held. Therefore it is not allowed that the
-        * console_list_lock is taken with the srcu_lock held.
+        * In unregister_console() and console_force_preferred_locked(),
+        * synchronize_srcu() is called with the console_list_lock held.
+        * Therefore it is not allowed that the console_list_lock is taken
+        * with the srcu_lock held.
         *
         * Detecting if this context is really in the read-side critical
         * section is only possible if the appropriate debug options are
@@ -3490,6 +3491,48 @@ int unregister_console(struct console *console)
 }
 EXPORT_SYMBOL(unregister_console);
 
+/**
+ * console_force_preferred_locked - force a registered console preferred
+ * @con: The registered console to force preferred.
+ *
+ * Must be called under console_list_lock().
+ */
+void console_force_preferred_locked(struct console *con)
+{
+       struct console *cur_pref_con;
+
+       if (!console_is_registered_locked(con))
+               return;
+
+       cur_pref_con = console_first();
+
+       /* Already preferred? */
+       if (cur_pref_con == con)
+               return;
+
+       /*
+        * Delete, but do not re-initialize the entry. This allows the console
+        * to continue to appear registered (via any hlist_unhashed_lockless()
+        * checks), even though it was briefly removed from the console list.
+        */
+       hlist_del_rcu(&con->node);
+
+       /*
+        * Ensure that all SRCU list walks have completed so that the console
+        * can be added to the beginning of the console list and its forward
+        * list pointer can be re-initialized.
+        */
+       synchronize_srcu(&console_srcu);
+
+       con->flags |= CON_CONSDEV;
+       WARN_ON(!con->device);
+
+       /* Only the new head can have CON_CONSDEV set. */
+       console_srcu_write_flags(cur_pref_con, cur_pref_con->flags & ~CON_CONSDEV);
+       hlist_add_head_rcu(&con->node, &console_list);
+}
+EXPORT_SYMBOL(console_force_preferred_locked);
+
 /*
  * Initialize the console device. This is called *early*, so
  * we can't necessarily depend on lots of kernel help here.