Skip to content

Commit b844e63

Browse files
Jiri Slaby (SUSE)gregkh
authored andcommitted
serial: serial_core: use guard()s
Use guards in the serial_core code. This improves readability, makes error handling easier, and marks locked portions of code explicit. All that while being sure the lock is unlocked. Signed-off-by: Jiri Slaby (SUSE) <jirislaby@kernel.org> Link: https://patch.msgid.link/20251119100140.830761-11-jirislaby@kernel.org Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent f374a33 commit b844e63

1 file changed

Lines changed: 55 additions & 72 deletions

File tree

drivers/tty/serial/serial_core.c

Lines changed: 55 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,21 +1034,17 @@ static int uart_set_info_user(struct tty_struct *tty, struct serial_struct *ss)
10341034
{
10351035
struct uart_state *state = tty->driver_data;
10361036
struct tty_port *port = &state->port;
1037-
int retval;
10381037

1039-
down_write(&tty->termios_rwsem);
1038+
guard(rwsem_write)(&tty->termios_rwsem);
10401039
/*
10411040
* This semaphore protects port->count. It is also
10421041
* very useful to prevent opens. Also, take the
10431042
* port configuration semaphore to make sure that a
10441043
* module insertion/removal doesn't change anything
10451044
* under us.
10461045
*/
1047-
mutex_lock(&port->mutex);
1048-
retval = uart_set_info(tty, port, state, ss);
1049-
mutex_unlock(&port->mutex);
1050-
up_write(&tty->termios_rwsem);
1051-
return retval;
1046+
guard(mutex)(&port->mutex);
1047+
return uart_set_info(tty, port, state, ss);
10521048
}
10531049

10541050
/**
@@ -1562,10 +1558,8 @@ uart_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg)
15621558

15631559
/* This ioctl doesn't rely on the hardware to be present. */
15641560
if (cmd == TIOCSERCONFIG) {
1565-
down_write(&tty->termios_rwsem);
1566-
ret = uart_do_autoconfig(tty, state);
1567-
up_write(&tty->termios_rwsem);
1568-
return ret;
1561+
guard(rwsem_write)(&tty->termios_rwsem);
1562+
return uart_do_autoconfig(tty, state);
15691563
}
15701564

15711565
if (tty_io_error(tty))
@@ -1579,46 +1573,46 @@ uart_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg)
15791573
if (cmd == TIOCSRS485)
15801574
down_write(&tty->termios_rwsem);
15811575

1582-
mutex_lock(&port->mutex);
1583-
uport = uart_port_check(state);
1576+
scoped_guard(mutex, &port->mutex) {
1577+
uport = uart_port_check(state);
15841578

1585-
if (!uport || tty_io_error(tty)) {
1586-
ret = -EIO;
1587-
goto out_up;
1588-
}
1579+
if (!uport || tty_io_error(tty)) {
1580+
ret = -EIO;
1581+
break;
1582+
}
15891583

1590-
/*
1591-
* All these rely on hardware being present and need to be
1592-
* protected against the tty being hung up.
1593-
*/
1584+
/*
1585+
* All these rely on hardware being present and need to be
1586+
* protected against the tty being hung up.
1587+
*/
15941588

1595-
switch (cmd) {
1596-
case TIOCSERGETLSR: /* Get line status register */
1597-
ret = uart_get_lsr_info(tty, state, uarg);
1598-
break;
1589+
switch (cmd) {
1590+
case TIOCSERGETLSR: /* Get line status register */
1591+
ret = uart_get_lsr_info(tty, state, uarg);
1592+
break;
15991593

1600-
case TIOCGRS485:
1601-
ret = uart_get_rs485_config(uport, uarg);
1602-
break;
1594+
case TIOCGRS485:
1595+
ret = uart_get_rs485_config(uport, uarg);
1596+
break;
16031597

1604-
case TIOCSRS485:
1605-
ret = uart_set_rs485_config(tty, uport, uarg);
1606-
break;
1598+
case TIOCSRS485:
1599+
ret = uart_set_rs485_config(tty, uport, uarg);
1600+
break;
16071601

1608-
case TIOCSISO7816:
1609-
ret = uart_set_iso7816_config(state->uart_port, uarg);
1610-
break;
1602+
case TIOCSISO7816:
1603+
ret = uart_set_iso7816_config(state->uart_port, uarg);
1604+
break;
16111605

1612-
case TIOCGISO7816:
1613-
ret = uart_get_iso7816_config(state->uart_port, uarg);
1614-
break;
1615-
default:
1616-
if (uport->ops->ioctl)
1617-
ret = uport->ops->ioctl(uport, cmd, arg);
1618-
break;
1606+
case TIOCGISO7816:
1607+
ret = uart_get_iso7816_config(state->uart_port, uarg);
1608+
break;
1609+
default:
1610+
if (uport->ops->ioctl)
1611+
ret = uport->ops->ioctl(uport, cmd, arg);
1612+
break;
1613+
}
16191614
}
1620-
out_up:
1621-
mutex_unlock(&port->mutex);
1615+
16221616
if (cmd == TIOCSRS485)
16231617
up_write(&tty->termios_rwsem);
16241618

@@ -1634,11 +1628,10 @@ static void uart_set_ldisc(struct tty_struct *tty)
16341628
if (!tty_port_initialized(port))
16351629
return;
16361630

1637-
mutex_lock(&state->port.mutex);
1631+
guard(mutex)(&state->port.mutex);
16381632
uport = uart_port_check(state);
16391633
if (uport && uport->ops->set_ldisc)
16401634
uport->ops->set_ldisc(uport, &tty->termios);
1641-
mutex_unlock(&state->port.mutex);
16421635
}
16431636

16441637
static void uart_set_termios(struct tty_struct *tty,
@@ -1712,9 +1705,8 @@ static void uart_close(struct tty_struct *tty, struct file *filp)
17121705

17131706
state = drv->state + tty->index;
17141707
port = &state->port;
1715-
spin_lock_irq(&port->lock);
1708+
guard(spinlock_irq)(&port->lock);
17161709
--port->count;
1717-
spin_unlock_irq(&port->lock);
17181710
return;
17191711
}
17201712

@@ -1826,28 +1818,25 @@ static void uart_hangup(struct tty_struct *tty)
18261818
struct uart_state *state = tty->driver_data;
18271819
struct tty_port *port = &state->port;
18281820
struct uart_port *uport;
1829-
unsigned long flags;
18301821

18311822
pr_debug("uart_hangup(%d)\n", tty->index);
18321823

1833-
mutex_lock(&port->mutex);
1824+
guard(mutex)(&port->mutex);
18341825
uport = uart_port_check(state);
18351826
WARN(!uport, "hangup of detached port!\n");
18361827

18371828
if (tty_port_active(port)) {
18381829
uart_flush_buffer(tty);
18391830
uart_shutdown(tty, state);
1840-
spin_lock_irqsave(&port->lock, flags);
1841-
port->count = 0;
1842-
spin_unlock_irqrestore(&port->lock, flags);
1831+
scoped_guard(spinlock_irqsave, &port->lock)
1832+
port->count = 0;
18431833
tty_port_set_active(port, false);
18441834
tty_port_tty_set(port, NULL);
18451835
if (uport && !uart_console(uport))
18461836
uart_change_pm(state, UART_PM_STATE_OFF);
18471837
wake_up_interruptible(&port->open_wait);
18481838
wake_up_interruptible(&port->delta_msr_wait);
18491839
}
1850-
mutex_unlock(&port->mutex);
18511840
}
18521841

18531842
/* uport == NULL if uart_port has already been removed */
@@ -2952,11 +2941,11 @@ static ssize_t console_show(struct device *dev,
29522941
struct uart_port *uport;
29532942
bool console = false;
29542943

2955-
mutex_lock(&port->mutex);
2956-
uport = uart_port_check(state);
2957-
if (uport)
2958-
console = uart_console_registered(uport);
2959-
mutex_unlock(&port->mutex);
2944+
scoped_guard(mutex, &port->mutex) {
2945+
uport = uart_port_check(state);
2946+
if (uport)
2947+
console = uart_console_registered(uport);
2948+
}
29602949

29612950
return sprintf(buf, "%c\n", console ? 'Y' : 'N');
29622951
}
@@ -3141,17 +3130,14 @@ static void serial_core_remove_one_port(struct uart_driver *drv,
31413130
struct tty_port *port = &state->port;
31423131
struct uart_port *uart_port;
31433132

3144-
mutex_lock(&port->mutex);
3145-
uart_port = uart_port_check(state);
3146-
if (uart_port != uport)
3147-
dev_alert(uport->dev, "Removing wrong port: %p != %p\n",
3148-
uart_port, uport);
3133+
scoped_guard(mutex, &port->mutex) {
3134+
uart_port = uart_port_check(state);
3135+
if (uart_port != uport)
3136+
dev_alert(uport->dev, "Removing wrong port: %p != %p\n", uart_port, uport);
31493137

3150-
if (!uart_port) {
3151-
mutex_unlock(&port->mutex);
3152-
return;
3138+
if (!uart_port)
3139+
return;
31533140
}
3154-
mutex_unlock(&port->mutex);
31553141

31563142
/*
31573143
* Remove the devices from the tty layer
@@ -3180,11 +3166,10 @@ static void serial_core_remove_one_port(struct uart_driver *drv,
31803166
uport->type = PORT_UNKNOWN;
31813167
uport->port_dev = NULL;
31823168

3183-
mutex_lock(&port->mutex);
3169+
guard(mutex)(&port->mutex);
31843170
WARN_ON(atomic_dec_return(&state->refcount) < 0);
31853171
wait_event(state->remove_wait, !atomic_read(&state->refcount));
31863172
state->uart_port = NULL;
3187-
mutex_unlock(&port->mutex);
31883173
}
31893174

31903175
/**
@@ -3337,7 +3322,7 @@ void serial_core_unregister_port(struct uart_driver *drv, struct uart_port *port
33373322
struct serial_ctrl_device *ctrl_dev = serial_core_get_ctrl_dev(port_dev);
33383323
int ctrl_id = port->ctrl_id;
33393324

3340-
mutex_lock(&port_mutex);
3325+
guard(mutex)(&port_mutex);
33413326

33423327
port->flags |= UPF_DEAD;
33433328

@@ -3349,8 +3334,6 @@ void serial_core_unregister_port(struct uart_driver *drv, struct uart_port *port
33493334
/* Drop the serial core controller device if no ports are using it */
33503335
if (!serial_core_ctrl_find(drv, phys_dev, ctrl_id))
33513336
serial_base_ctrl_device_remove(ctrl_dev);
3352-
3353-
mutex_unlock(&port_mutex);
33543337
}
33553338

33563339
/**

0 commit comments

Comments
 (0)