serial_core: replace uart_console_enabled() with uart_console_registered()
authorJohn Ogness <john.ogness@linutronix.de>
Wed, 16 Nov 2022 16:21:34 +0000 (17:27 +0106)
committerPetr Mladek <pmladek@suse.com>
Fri, 2 Dec 2022 10:25:01 +0000 (11:25 +0100)
All users of uart_console_enabled() really want to know if a console
is registered. It is not reliable to check for CON_ENABLED in order
to identify if a console is registered. Use console_is_registered()
instead.

A _locked() variant is provided because uart_set_options() is always
called with the console_list_lock held and must check if a console
is registered in order to synchronize with kgdboc.

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-23-john.ogness@linutronix.de
drivers/tty/serial/8250/8250_core.c
drivers/tty/serial/pic32_uart.c
drivers/tty/serial/serial_core.c
include/linux/serial_core.h

index 94fbf0add2ce25f42c0139c85ec6dc01e22c5799..74568292186fba2dd3adbb91c4ebba801d4b5389 100644 (file)
@@ -565,7 +565,7 @@ serial8250_register_ports(struct uart_driver *drv, struct device *dev)
 
                up->port.dev = dev;
 
-               if (uart_console_enabled(&up->port))
+               if (uart_console_registered(&up->port))
                        pm_runtime_get_sync(up->port.dev);
 
                serial8250_apply_quirks(up);
index 2beada66c8245edac1edf7dbfc270bb73af0d6ee..1183b2a2653927653544eddffdfaaf55769b7642 100644 (file)
@@ -919,7 +919,7 @@ static int pic32_uart_probe(struct platform_device *pdev)
        }
 
 #ifdef CONFIG_SERIAL_PIC32_CONSOLE
-       if (uart_console_enabled(port)) {
+       if (uart_console_registered(port)) {
                /* The peripheral clock has been enabled by console_setup,
                 * so disable it till the port is used.
                 */
index 179ee199df343ef28d00e0327c1909cbddf5fbc3..b9fbbee598b815e8cd41527dc0acec65c8958710 100644 (file)
@@ -2223,11 +2223,11 @@ uart_set_options(struct uart_port *port, struct console *co,
        /*
         * Ensure that the serial-console lock is initialised early.
         *
-        * Note that the console-enabled check is needed because of kgdboc,
-        * which can end up calling uart_set_options() for an already enabled
+        * Note that the console-registered check is needed because
+        * kgdboc can call uart_set_options() for an already registered
         * console via tty_find_polling_driver() and uart_poll_init().
         */
-       if (!uart_console_enabled(port) && !port->console_reinit)
+       if (!uart_console_registered_locked(port) && !port->console_reinit)
                uart_port_spin_lock_init(port);
 
        memset(&termios, 0, sizeof(struct ktermios));
@@ -2573,7 +2573,7 @@ uart_configure_port(struct uart_driver *drv, struct uart_state *state,
                 * successfully registered yet, try to re-register it.
                 * It may be that the port was not available.
                 */
-               if (port->cons && !(port->cons->flags & CON_ENABLED))
+               if (port->cons && !console_is_registered(port->cons))
                        register_console(port->cons);
 
                /*
@@ -2956,7 +2956,7 @@ static ssize_t console_show(struct device *dev,
        mutex_lock(&port->mutex);
        uport = uart_port_check(state);
        if (uport)
-               console = uart_console_enabled(uport);
+               console = uart_console_registered(uport);
        mutex_unlock(&port->mutex);
 
        return sprintf(buf, "%c\n", console ? 'Y' : 'N');
@@ -2978,7 +2978,7 @@ static ssize_t console_store(struct device *dev,
        mutex_lock(&port->mutex);
        uport = uart_port_check(state);
        if (uport) {
-               oldconsole = uart_console_enabled(uport);
+               oldconsole = uart_console_registered(uport);
                if (oldconsole && !newconsole) {
                        ret = unregister_console(uport->cons);
                } else if (!oldconsole && newconsole) {
@@ -3086,7 +3086,7 @@ int uart_add_one_port(struct uart_driver *drv, struct uart_port *uport)
         * If this port is in use as a console then the spinlock is already
         * initialised.
         */
-       if (!uart_console_enabled(uport))
+       if (!uart_console_registered(uport))
                uart_port_spin_lock_init(uport);
 
        if (uport->cons && uport->dev)
index d657f2a42a7b8fadd84d88952e5e2a022b36b32c..91871464b99d36d51d86691e413a3f1b2bcb6408 100644 (file)
@@ -743,9 +743,15 @@ static const bool earlycon_acpi_spcr_enable EARLYCON_USED_OR_UNUSED;
 static inline int setup_earlycon(char *buf) { return 0; }
 #endif
 
-static inline bool uart_console_enabled(struct uart_port *port)
+/* Variant of uart_console_registered() when the console_list_lock is held. */
+static inline bool uart_console_registered_locked(struct uart_port *port)
 {
-       return uart_console(port) && (port->cons->flags & CON_ENABLED);
+       return uart_console(port) && console_is_registered_locked(port->cons);
+}
+
+static inline bool uart_console_registered(struct uart_port *port)
+{
+       return uart_console(port) && console_is_registered(port->cons);
 }
 
 struct uart_port *uart_get_console(struct uart_port *ports, int nr,