Commit eabd4600 authored by John Ogness's avatar John Ogness Committed by Petr Mladek

serial: core: Introduce wrapper to set @uart_port->cons

Introduce uart_port_set_cons() as a wrapper to set @cons of a
uart_port. The wrapper sets @cons under the port lock in order
to prevent @cons from disappearing while another context is
holding the port lock. This is necessary for a follow-up
commit relating to the port lock wrappers, which rely on @cons
not changing between lock and unlock.
Signed-off-by: default avatarJohn Ogness <john.ogness@linutronix.de>
Tested-by: Théo Lebrun <theo.lebrun@bootlin.com> # EyeQ5, AMBA-PL011
Acked-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Reviewed-by: default avatarPetr Mladek <pmladek@suse.com>
Reviewed-by: default avatarIlpo Järvinen <ilpo.jarvinen@linux.intel.com>
Link: https://lore.kernel.org/r/20240820063001.36405-12-john.ogness@linutronix.deSigned-off-by: default avatarPetr Mladek <pmladek@suse.com>
parent 77e73c06
...@@ -423,11 +423,11 @@ static int univ8250_console_setup(struct console *co, char *options) ...@@ -423,11 +423,11 @@ static int univ8250_console_setup(struct console *co, char *options)
port = &serial8250_ports[co->index].port; port = &serial8250_ports[co->index].port;
/* link port to console */ /* link port to console */
port->cons = co; uart_port_set_cons(port, co);
retval = serial8250_console_setup(port, options, false); retval = serial8250_console_setup(port, options, false);
if (retval != 0) if (retval != 0)
port->cons = NULL; uart_port_set_cons(port, NULL);
return retval; return retval;
} }
...@@ -485,7 +485,7 @@ static int univ8250_console_match(struct console *co, char *name, int idx, ...@@ -485,7 +485,7 @@ static int univ8250_console_match(struct console *co, char *name, int idx,
continue; continue;
co->index = i; co->index = i;
port->cons = co; uart_port_set_cons(port, co);
return serial8250_console_setup(port, options, true); return serial8250_console_setup(port, options, true);
} }
......
...@@ -2480,7 +2480,7 @@ static int pl011_console_match(struct console *co, char *name, int idx, ...@@ -2480,7 +2480,7 @@ static int pl011_console_match(struct console *co, char *name, int idx,
continue; continue;
co->index = i; co->index = i;
port->cons = co; uart_port_set_cons(port, co);
return pl011_console_setup(co, options); return pl011_console_setup(co, options);
} }
......
...@@ -3176,8 +3176,15 @@ static int serial_core_add_one_port(struct uart_driver *drv, struct uart_port *u ...@@ -3176,8 +3176,15 @@ static int serial_core_add_one_port(struct uart_driver *drv, struct uart_port *u
state->uart_port = uport; state->uart_port = uport;
uport->state = state; uport->state = state;
/*
* If this port is in use as a console then the spinlock is already
* initialised.
*/
if (!uart_console_registered(uport))
uart_port_spin_lock_init(uport);
state->pm_state = UART_PM_STATE_UNDEFINED; state->pm_state = UART_PM_STATE_UNDEFINED;
uport->cons = drv->cons; uart_port_set_cons(uport, drv->cons);
uport->minor = drv->tty_driver->minor_start + uport->line; uport->minor = drv->tty_driver->minor_start + uport->line;
uport->name = kasprintf(GFP_KERNEL, "%s%d", drv->dev_name, uport->name = kasprintf(GFP_KERNEL, "%s%d", drv->dev_name,
drv->tty_driver->name_base + uport->line); drv->tty_driver->name_base + uport->line);
...@@ -3186,13 +3193,6 @@ static int serial_core_add_one_port(struct uart_driver *drv, struct uart_port *u ...@@ -3186,13 +3193,6 @@ static int serial_core_add_one_port(struct uart_driver *drv, struct uart_port *u
goto out; goto out;
} }
/*
* If this port is in use as a console then the spinlock is already
* initialised.
*/
if (!uart_console_registered(uport))
uart_port_spin_lock_init(uport);
if (uport->cons && uport->dev) if (uport->cons && uport->dev)
of_console_check(uport->dev->of_node, uport->cons->name, uport->line); of_console_check(uport->dev->of_node, uport->cons->name, uport->line);
......
...@@ -608,6 +608,23 @@ static inline void __uart_port_unlock_irqrestore(struct uart_port *up, unsigned ...@@ -608,6 +608,23 @@ static inline void __uart_port_unlock_irqrestore(struct uart_port *up, unsigned
spin_unlock_irqrestore(&up->lock, flags); spin_unlock_irqrestore(&up->lock, flags);
} }
/**
* uart_port_set_cons - Safely set the @cons field for a uart
* @up: The uart port to set
* @con: The new console to set to
*
* This function must be used to set @up->cons. It uses the port lock to
* synchronize with the port lock wrappers in order to ensure that the console
* cannot change or disappear while another context is holding the port lock.
*/
static inline void uart_port_set_cons(struct uart_port *up, struct console *con)
{
unsigned long flags;
__uart_port_lock_irqsave(up, &flags);
up->cons = con;
__uart_port_unlock_irqrestore(up, flags);
}
/** /**
* uart_port_lock - Lock the UART port * uart_port_lock - Lock the UART port
* @up: Pointer to UART port structure * @up: Pointer to UART port structure
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment