Skip to content

Commit 5879adb

Browse files
Jiri Slaby (SUSE)gregkh
authored andcommitted
serial: use guards for simple mutex locks
Guards can help to make the code more readable. So use it wherever they do so. On many places labels and 'ret' locals are eliminated completely. Signed-off-by: Jiri Slaby (SUSE) <jirislaby@kernel.org> Link: https://lore.kernel.org/r/20240808103549.429349-2-jirislaby@kernel.org Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 98e24a5 commit 5879adb

1 file changed

Lines changed: 47 additions & 66 deletions

File tree

drivers/tty/serial/serial_core.c

Lines changed: 47 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1104,21 +1104,19 @@ static int uart_tiocmget(struct tty_struct *tty)
11041104
struct uart_state *state = tty->driver_data;
11051105
struct tty_port *port = &state->port;
11061106
struct uart_port *uport;
1107-
int result = -EIO;
1107+
int result;
1108+
1109+
guard(mutex)(&port->mutex);
11081110

1109-
mutex_lock(&port->mutex);
11101111
uport = uart_port_check(state);
1111-
if (!uport)
1112-
goto out;
1112+
if (!uport || tty_io_error(tty))
1113+
return -EIO;
1114+
1115+
uart_port_lock_irq(uport);
1116+
result = uport->mctrl;
1117+
result |= uport->ops->get_mctrl(uport);
1118+
uart_port_unlock_irq(uport);
11131119

1114-
if (!tty_io_error(tty)) {
1115-
uart_port_lock_irq(uport);
1116-
result = uport->mctrl;
1117-
result |= uport->ops->get_mctrl(uport);
1118-
uart_port_unlock_irq(uport);
1119-
}
1120-
out:
1121-
mutex_unlock(&port->mutex);
11221120
return result;
11231121
}
11241122

@@ -1128,40 +1126,34 @@ uart_tiocmset(struct tty_struct *tty, unsigned int set, unsigned int clear)
11281126
struct uart_state *state = tty->driver_data;
11291127
struct tty_port *port = &state->port;
11301128
struct uart_port *uport;
1131-
int ret = -EIO;
11321129

1133-
mutex_lock(&port->mutex);
1130+
guard(mutex)(&port->mutex);
1131+
11341132
uport = uart_port_check(state);
1135-
if (!uport)
1136-
goto out;
1133+
if (!uport || tty_io_error(tty))
1134+
return -EIO;
11371135

1138-
if (!tty_io_error(tty)) {
1139-
uart_update_mctrl(uport, set, clear);
1140-
ret = 0;
1141-
}
1142-
out:
1143-
mutex_unlock(&port->mutex);
1144-
return ret;
1136+
uart_update_mctrl(uport, set, clear);
1137+
1138+
return 0;
11451139
}
11461140

11471141
static int uart_break_ctl(struct tty_struct *tty, int break_state)
11481142
{
11491143
struct uart_state *state = tty->driver_data;
11501144
struct tty_port *port = &state->port;
11511145
struct uart_port *uport;
1152-
int ret = -EIO;
11531146

1154-
mutex_lock(&port->mutex);
1147+
guard(mutex)(&port->mutex);
1148+
11551149
uport = uart_port_check(state);
11561150
if (!uport)
1157-
goto out;
1151+
return -EIO;
11581152

11591153
if (uport->type != PORT_UNKNOWN && uport->ops->break_ctl)
11601154
uport->ops->break_ctl(uport, break_state);
1161-
ret = 0;
1162-
out:
1163-
mutex_unlock(&port->mutex);
1164-
return ret;
1155+
1156+
return 0;
11651157
}
11661158

11671159
static int uart_do_autoconfig(struct tty_struct *tty, struct uart_state *state)
@@ -1178,17 +1170,14 @@ static int uart_do_autoconfig(struct tty_struct *tty, struct uart_state *state)
11781170
* changing, and hence any extra opens of the port while
11791171
* we're auto-configuring.
11801172
*/
1181-
if (mutex_lock_interruptible(&port->mutex))
1182-
return -ERESTARTSYS;
1173+
scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &port->mutex) {
1174+
uport = uart_port_check(state);
1175+
if (!uport)
1176+
return -EIO;
11831177

1184-
uport = uart_port_check(state);
1185-
if (!uport) {
1186-
ret = -EIO;
1187-
goto out;
1188-
}
1178+
if (tty_port_users(port) != 1)
1179+
return -EBUSY;
11891180

1190-
ret = -EBUSY;
1191-
if (tty_port_users(port) == 1) {
11921181
uart_shutdown(tty, state);
11931182

11941183
/*
@@ -1209,14 +1198,15 @@ static int uart_do_autoconfig(struct tty_struct *tty, struct uart_state *state)
12091198
uport->ops->config_port(uport, flags);
12101199

12111200
ret = uart_startup(tty, state, true);
1212-
if (ret == 0)
1213-
tty_port_set_initialized(port, true);
1201+
if (ret < 0)
1202+
return ret;
12141203
if (ret > 0)
1215-
ret = 0;
1204+
return 0;
1205+
1206+
tty_port_set_initialized(port, true);
12161207
}
1217-
out:
1218-
mutex_unlock(&port->mutex);
1219-
return ret;
1208+
1209+
return 0;
12201210
}
12211211

12221212
static void uart_enable_ms(struct uart_port *uport)
@@ -1711,10 +1701,11 @@ static void uart_set_termios(struct tty_struct *tty,
17111701
unsigned int iflag_mask = IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK;
17121702
bool sw_changed = false;
17131703

1714-
mutex_lock(&state->port.mutex);
1704+
guard(mutex)(&state->port.mutex);
1705+
17151706
uport = uart_port_check(state);
17161707
if (!uport)
1717-
goto out;
1708+
return;
17181709

17191710
/*
17201711
* Drivers doing software flow control also need to know
@@ -1737,9 +1728,8 @@ static void uart_set_termios(struct tty_struct *tty,
17371728
tty->termios.c_ospeed == old_termios->c_ospeed &&
17381729
tty->termios.c_ispeed == old_termios->c_ispeed &&
17391730
((tty->termios.c_iflag ^ old_termios->c_iflag) & iflag_mask) == 0 &&
1740-
!sw_changed) {
1741-
goto out;
1742-
}
1731+
!sw_changed)
1732+
return;
17431733

17441734
uart_change_line_settings(tty, state, old_termios);
17451735
/* reload cflag from termios; port driver may have overridden flags */
@@ -1756,8 +1746,6 @@ static void uart_set_termios(struct tty_struct *tty,
17561746
mask |= TIOCM_RTS;
17571747
uart_set_mctrl(uport, mask);
17581748
}
1759-
out:
1760-
mutex_unlock(&state->port.mutex);
17611749
}
17621750

17631751
/*
@@ -2051,10 +2039,11 @@ static void uart_line_info(struct seq_file *m, struct uart_driver *drv, int i)
20512039
unsigned int status;
20522040
int mmio;
20532041

2054-
mutex_lock(&port->mutex);
2042+
guard(mutex)(&port->mutex);
2043+
20552044
uport = uart_port_check(state);
20562045
if (!uport)
2057-
goto out;
2046+
return;
20582047

20592048
mmio = uport->iotype >= UPIO_MEM;
20602049
seq_printf(m, "%d: uart:%s %s%08llX irq:%d",
@@ -2066,7 +2055,7 @@ static void uart_line_info(struct seq_file *m, struct uart_driver *drv, int i)
20662055

20672056
if (uport->type == PORT_UNKNOWN) {
20682057
seq_putc(m, '\n');
2069-
goto out;
2058+
return;
20702059
}
20712060

20722061
if (capable(CAP_SYS_ADMIN)) {
@@ -2117,8 +2106,6 @@ static void uart_line_info(struct seq_file *m, struct uart_driver *drv, int i)
21172106
seq_putc(m, '\n');
21182107
#undef STATBIT
21192108
#undef INFOBIT
2120-
out:
2121-
mutex_unlock(&port->mutex);
21222109
}
21232110

21242111
static int uart_proc_show(struct seq_file *m, void *v)
@@ -2395,13 +2382,12 @@ int uart_suspend_port(struct uart_driver *drv, struct uart_port *uport)
23952382
struct device *tty_dev;
23962383
struct uart_match match = {uport, drv};
23972384

2398-
mutex_lock(&port->mutex);
2385+
guard(mutex)(&port->mutex);
23992386

24002387
tty_dev = device_find_child(&uport->port_dev->dev, &match, serial_match_port);
24012388
if (tty_dev && device_may_wakeup(tty_dev)) {
24022389
enable_irq_wake(uport->irq);
24032390
put_device(tty_dev);
2404-
mutex_unlock(&port->mutex);
24052391
return 0;
24062392
}
24072393
put_device(tty_dev);
@@ -2419,7 +2405,7 @@ int uart_suspend_port(struct uart_driver *drv, struct uart_port *uport)
24192405
uart_port_unlock_irq(uport);
24202406
}
24212407
device_set_awake_path(uport->dev);
2422-
goto unlock;
2408+
return 0;
24232409
}
24242410

24252411
uport->suspended = 1;
@@ -2462,8 +2448,6 @@ int uart_suspend_port(struct uart_driver *drv, struct uart_port *uport)
24622448
console_stop(uport->cons);
24632449

24642450
uart_change_pm(state, UART_PM_STATE_OFF);
2465-
unlock:
2466-
mutex_unlock(&port->mutex);
24672451

24682452
return 0;
24692453
}
@@ -2477,14 +2461,13 @@ int uart_resume_port(struct uart_driver *drv, struct uart_port *uport)
24772461
struct uart_match match = {uport, drv};
24782462
struct ktermios termios;
24792463

2480-
mutex_lock(&port->mutex);
2464+
guard(mutex)(&port->mutex);
24812465

24822466
tty_dev = device_find_child(&uport->port_dev->dev, &match, serial_match_port);
24832467
if (!uport->suspended && device_may_wakeup(tty_dev)) {
24842468
if (irqd_is_wakeup_set(irq_get_irq_data((uport->irq))))
24852469
disable_irq_wake(uport->irq);
24862470
put_device(tty_dev);
2487-
mutex_unlock(&port->mutex);
24882471
return 0;
24892472
}
24902473
put_device(tty_dev);
@@ -2557,8 +2540,6 @@ int uart_resume_port(struct uart_driver *drv, struct uart_port *uport)
25572540
tty_port_set_suspended(port, false);
25582541
}
25592542

2560-
mutex_unlock(&port->mutex);
2561-
25622543
return 0;
25632544
}
25642545
EXPORT_SYMBOL(uart_resume_port);

0 commit comments

Comments
 (0)