console: introduce console_is_registered()
authorJohn Ogness <john.ogness@linutronix.de>
Wed, 16 Nov 2022 16:21:33 +0000 (17:27 +0106)
committerPetr Mladek <pmladek@suse.com>
Fri, 2 Dec 2022 10:25:01 +0000 (11:25 +0100)
Currently it is not possible for drivers to detect if they have
already successfully registered their console. Several drivers
have multiple paths that lead to console registration. To avoid
attempting a 2nd registration (which leads to a WARN), drivers
are implementing their own solution.

Introduce console_is_registered() so drivers can easily identify
if their console is currently registered. A _locked() variant
is also provided if the caller is already holding 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-22-john.ogness@linutronix.de
include/linux/console.h
kernel/printk/printk.c

index c1ca461d088aa7a30d4fb41357be0f3ec3c2ae10..f716e1dd9eaf2ceeadf2f7e817ce453e595a0419 100644 (file)
@@ -228,6 +228,34 @@ static inline void console_srcu_write_flags(struct console *con, short flags)
        WRITE_ONCE(con->flags, flags);
 }
 
+/* Variant of console_is_registered() when the console_list_lock is held. */
+static inline bool console_is_registered_locked(const struct console *con)
+{
+       lockdep_assert_console_list_lock_held();
+       return !hlist_unhashed(&con->node);
+}
+
+/*
+ * console_is_registered - Check if the console is registered
+ * @con:       struct console pointer of console to check
+ *
+ * Context: Process context. May sleep while acquiring console list lock.
+ * Return: true if the console is in the console list, otherwise false.
+ *
+ * If false is returned for a console that was previously registered, it
+ * can be assumed that the console's unregistration is fully completed,
+ * including the exit() callback after console list removal.
+ */
+static inline bool console_is_registered(const struct console *con)
+{
+       bool ret;
+
+       console_list_lock();
+       ret = console_is_registered_locked(con);
+       console_list_unlock();
+       return ret;
+}
+
 /**
  * for_each_console_srcu() - Iterator over registered consoles
  * @con:       struct console pointer used as loop cursor
index 101d08d9dc88092c53fc868548fab6030b527ae8..e58a6ccb945cafdbd2ef8259cd8826b967ce7de9 100644 (file)
@@ -3443,7 +3443,7 @@ static int unregister_console_locked(struct console *console)
        /* Disable it unconditionally */
        console_srcu_write_flags(console, console->flags & ~CON_ENABLED);
 
-       if (hlist_unhashed(&console->node)) {
+       if (!console_is_registered_locked(console)) {
                console_unlock();
                return -ENODEV;
        }