Skip to content

Commit 1c7736d

Browse files
Jiri Slaby (SUSE)gregkh
authored andcommitted
tty: moxa: use guard()s
Use guards in the moxa 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-6-jirislaby@kernel.org Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 8c03bfc commit 1c7736d

1 file changed

Lines changed: 73 additions & 96 deletions

File tree

drivers/tty/moxa.c

Lines changed: 73 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -487,25 +487,20 @@ static void moxa_wait_finish(void __iomem *ofsAddr)
487487

488488
static void moxafunc(void __iomem *ofsAddr, u16 cmd, u16 arg)
489489
{
490-
unsigned long flags;
491-
spin_lock_irqsave(&moxafunc_lock, flags);
490+
guard(spinlock_irqsave)(&moxafunc_lock);
492491
writew(arg, ofsAddr + FuncArg);
493492
writew(cmd, ofsAddr + FuncCode);
494493
moxa_wait_finish(ofsAddr);
495-
spin_unlock_irqrestore(&moxafunc_lock, flags);
496494
}
497495

498496
static int moxafuncret(void __iomem *ofsAddr, u16 cmd, u16 arg)
499497
{
500-
unsigned long flags;
501-
u16 ret;
502-
spin_lock_irqsave(&moxafunc_lock, flags);
498+
guard(spinlock_irqsave)(&moxafunc_lock);
503499
writew(arg, ofsAddr + FuncArg);
504500
writew(cmd, ofsAddr + FuncCode);
505501
moxa_wait_finish(ofsAddr);
506-
ret = readw(ofsAddr + FuncArg);
507-
spin_unlock_irqrestore(&moxafunc_lock, flags);
508-
return ret;
502+
503+
return readw(ofsAddr + FuncArg);
509504
}
510505

511506
static void moxa_low_water_check(void __iomem *ofsAddr)
@@ -1002,11 +997,11 @@ static int moxa_init_board(struct moxa_board_conf *brd, struct device *dev)
1002997
if (ret)
1003998
goto err_free;
1004999

1005-
spin_lock_bh(&moxa_lock);
1006-
brd->ready = 1;
1007-
if (!timer_pending(&moxaTimer))
1008-
mod_timer(&moxaTimer, jiffies + HZ / 50);
1009-
spin_unlock_bh(&moxa_lock);
1000+
scoped_guard(spinlock_bh, &moxa_lock) {
1001+
brd->ready = 1;
1002+
if (!timer_pending(&moxaTimer))
1003+
mod_timer(&moxaTimer, jiffies + HZ / 50);
1004+
}
10101005

10111006
first_idx = (brd - moxa_boards) * MAX_PORTS_PER_BOARD;
10121007
for (i = 0; i < brd->numPorts; i++)
@@ -1026,29 +1021,29 @@ static void moxa_board_deinit(struct moxa_board_conf *brd)
10261021
{
10271022
unsigned int a, opened, first_idx;
10281023

1029-
mutex_lock(&moxa_openlock);
1030-
spin_lock_bh(&moxa_lock);
1031-
brd->ready = 0;
1032-
spin_unlock_bh(&moxa_lock);
1033-
1034-
/* pci hot-un-plug support */
1035-
for (a = 0; a < brd->numPorts; a++)
1036-
if (tty_port_initialized(&brd->ports[a].port))
1037-
tty_port_tty_hangup(&brd->ports[a].port, false);
1038-
1039-
for (a = 0; a < MAX_PORTS_PER_BOARD; a++)
1040-
tty_port_destroy(&brd->ports[a].port);
1024+
scoped_guard(mutex, &moxa_openlock) {
1025+
scoped_guard(spinlock_bh, &moxa_lock)
1026+
brd->ready = 0;
10411027

1042-
while (1) {
1043-
opened = 0;
1028+
/* pci hot-un-plug support */
10441029
for (a = 0; a < brd->numPorts; a++)
10451030
if (tty_port_initialized(&brd->ports[a].port))
1046-
opened++;
1047-
mutex_unlock(&moxa_openlock);
1048-
if (!opened)
1049-
break;
1050-
msleep(50);
1051-
mutex_lock(&moxa_openlock);
1031+
tty_port_tty_hangup(&brd->ports[a].port, false);
1032+
1033+
for (a = 0; a < MAX_PORTS_PER_BOARD; a++)
1034+
tty_port_destroy(&brd->ports[a].port);
1035+
1036+
while (1) {
1037+
opened = 0;
1038+
for (a = 0; a < brd->numPorts; a++)
1039+
if (tty_port_initialized(&brd->ports[a].port))
1040+
opened++;
1041+
if (!opened)
1042+
break;
1043+
mutex_unlock(&moxa_openlock);
1044+
msleep(50);
1045+
mutex_lock(&moxa_openlock);
1046+
}
10521047
}
10531048

10541049
first_idx = (brd - moxa_boards) * MAX_PORTS_PER_BOARD;
@@ -1206,12 +1201,9 @@ static void moxa_shutdown(struct tty_port *port)
12061201
static bool moxa_carrier_raised(struct tty_port *port)
12071202
{
12081203
struct moxa_port *ch = container_of(port, struct moxa_port, port);
1209-
int dcd;
12101204

1211-
spin_lock_irq(&port->lock);
1212-
dcd = ch->DCDState;
1213-
spin_unlock_irq(&port->lock);
1214-
return dcd;
1205+
guard(spinlock_irq)(&port->lock);
1206+
return ch->DCDState;
12151207
}
12161208

12171209
static void moxa_dtr_rts(struct tty_port *port, bool active)
@@ -1225,37 +1217,31 @@ static int moxa_open(struct tty_struct *tty, struct file *filp)
12251217
{
12261218
struct moxa_board_conf *brd;
12271219
struct moxa_port *ch;
1228-
int port;
1229-
1230-
port = tty->index;
1231-
if (mutex_lock_interruptible(&moxa_openlock))
1232-
return -ERESTARTSYS;
1233-
brd = &moxa_boards[port / MAX_PORTS_PER_BOARD];
1234-
if (!brd->ready) {
1235-
mutex_unlock(&moxa_openlock);
1236-
return -ENODEV;
1237-
}
1220+
int port = tty->index;
12381221

1239-
if (port % MAX_PORTS_PER_BOARD >= brd->numPorts) {
1240-
mutex_unlock(&moxa_openlock);
1241-
return -ENODEV;
1242-
}
1243-
1244-
ch = &brd->ports[port % MAX_PORTS_PER_BOARD];
1245-
ch->port.count++;
1246-
tty->driver_data = ch;
1247-
tty_port_tty_set(&ch->port, tty);
1248-
mutex_lock(&ch->port.mutex);
1249-
if (!tty_port_initialized(&ch->port)) {
1250-
ch->statusflags = 0;
1251-
moxa_set_tty_param(tty, &tty->termios);
1252-
MoxaPortLineCtrl(ch, true, true);
1253-
MoxaPortEnable(ch);
1254-
MoxaSetFifo(ch, ch->type == PORT_16550A);
1255-
tty_port_set_initialized(&ch->port, true);
1222+
scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &moxa_openlock) {
1223+
brd = &moxa_boards[port / MAX_PORTS_PER_BOARD];
1224+
if (!brd->ready)
1225+
return -ENODEV;
1226+
1227+
if (port % MAX_PORTS_PER_BOARD >= brd->numPorts)
1228+
return -ENODEV;
1229+
1230+
ch = &brd->ports[port % MAX_PORTS_PER_BOARD];
1231+
ch->port.count++;
1232+
tty->driver_data = ch;
1233+
tty_port_tty_set(&ch->port, tty);
1234+
1235+
guard(mutex)(&ch->port.mutex);
1236+
if (!tty_port_initialized(&ch->port)) {
1237+
ch->statusflags = 0;
1238+
moxa_set_tty_param(tty, &tty->termios);
1239+
MoxaPortLineCtrl(ch, true, true);
1240+
MoxaPortEnable(ch);
1241+
MoxaSetFifo(ch, ch->type == PORT_16550A);
1242+
tty_port_set_initialized(&ch->port, true);
1243+
}
12561244
}
1257-
mutex_unlock(&ch->port.mutex);
1258-
mutex_unlock(&moxa_openlock);
12591245

12601246
return tty_port_block_til_ready(&ch->port, tty, filp);
12611247
}
@@ -1270,15 +1256,13 @@ static void moxa_close(struct tty_struct *tty, struct file *filp)
12701256
static ssize_t moxa_write(struct tty_struct *tty, const u8 *buf, size_t count)
12711257
{
12721258
struct moxa_port *ch = tty->driver_data;
1273-
unsigned long flags;
12741259
int len;
12751260

12761261
if (ch == NULL)
12771262
return 0;
12781263

1279-
spin_lock_irqsave(&moxa_lock, flags);
1280-
len = MoxaPortWriteData(tty, buf, count);
1281-
spin_unlock_irqrestore(&moxa_lock, flags);
1264+
scoped_guard(spinlock_irqsave, &moxa_lock)
1265+
len = MoxaPortWriteData(tty, buf, count);
12821266

12831267
set_bit(LOWWAIT, &ch->statusflags);
12841268
return len;
@@ -1349,12 +1333,10 @@ static int moxa_tiocmset(struct tty_struct *tty,
13491333
bool dtr_active, rts_active;
13501334
struct moxa_port *ch;
13511335

1352-
mutex_lock(&moxa_openlock);
1336+
guard(mutex)(&moxa_openlock);
13531337
ch = tty->driver_data;
1354-
if (!ch) {
1355-
mutex_unlock(&moxa_openlock);
1338+
if (!ch)
13561339
return -EINVAL;
1357-
}
13581340

13591341
MoxaPortGetLineOut(ch, &dtr_active, &rts_active);
13601342
if (set & TIOCM_RTS)
@@ -1366,7 +1348,7 @@ static int moxa_tiocmset(struct tty_struct *tty,
13661348
if (clear & TIOCM_DTR)
13671349
dtr_active = false;
13681350
MoxaPortLineCtrl(ch, dtr_active, rts_active);
1369-
mutex_unlock(&moxa_openlock);
1351+
13701352
return 0;
13711353
}
13721354

@@ -1415,18 +1397,17 @@ static void moxa_hangup(struct tty_struct *tty)
14151397

14161398
static void moxa_new_dcdstate(struct moxa_port *p, u8 dcd)
14171399
{
1418-
unsigned long flags;
14191400
dcd = !!dcd;
14201401

1421-
spin_lock_irqsave(&p->port.lock, flags);
1422-
if (dcd != p->DCDState) {
1423-
p->DCDState = dcd;
1424-
spin_unlock_irqrestore(&p->port.lock, flags);
1425-
if (!dcd)
1426-
tty_port_tty_hangup(&p->port, true);
1402+
scoped_guard(spinlock_irqsave, &p->port.lock) {
1403+
if (dcd == p->DCDState)
1404+
return;
1405+
1406+
p->DCDState = dcd;
14271407
}
1428-
else
1429-
spin_unlock_irqrestore(&p->port.lock, flags);
1408+
1409+
if (!dcd)
1410+
tty_port_tty_hangup(&p->port, true);
14301411
}
14311412

14321413
static int moxa_poll_port(struct moxa_port *p, unsigned int handle,
@@ -1494,7 +1475,7 @@ static void moxa_poll(struct timer_list *unused)
14941475
u16 __iomem *ip;
14951476
unsigned int card, port, served = 0;
14961477

1497-
spin_lock(&moxa_lock);
1478+
guard(spinlock)(&moxa_lock);
14981479
for (card = 0; card < MAX_BOARDS; card++) {
14991480
brd = &moxa_boards[card];
15001481
if (!brd->ready)
@@ -1525,7 +1506,6 @@ static void moxa_poll(struct timer_list *unused)
15251506

15261507
if (served)
15271508
mod_timer(&moxaTimer, jiffies + HZ / 50);
1528-
spin_unlock(&moxa_lock);
15291509
}
15301510

15311511
/******************************************************************************/
@@ -1861,13 +1841,11 @@ static int MoxaPortSetTermio(struct moxa_port *port, struct ktermios *termio,
18611841
baud = MoxaPortSetBaud(port, baud);
18621842

18631843
if (termio->c_iflag & (IXON | IXOFF | IXANY)) {
1864-
spin_lock_irq(&moxafunc_lock);
1844+
guard(spinlock_irq)(&moxafunc_lock);
18651845
writeb(termio->c_cc[VSTART], ofsAddr + FuncArg);
18661846
writeb(termio->c_cc[VSTOP], ofsAddr + FuncArg1);
18671847
writeb(FC_SetXonXoff, ofsAddr + FuncCode);
18681848
moxa_wait_finish(ofsAddr);
1869-
spin_unlock_irq(&moxafunc_lock);
1870-
18711849
}
18721850
return baud;
18731851
}
@@ -2098,13 +2076,13 @@ static int moxa_get_serial_info(struct tty_struct *tty,
20982076

20992077
if (!info)
21002078
return -ENODEV;
2101-
mutex_lock(&info->port.mutex);
2079+
guard(mutex)(&info->port.mutex);
21022080
ss->type = info->type;
21032081
ss->line = info->port.tty->index;
21042082
ss->flags = info->port.flags;
21052083
ss->baud_base = 921600;
21062084
ss->close_delay = jiffies_to_msecs(info->port.close_delay) / 10;
2107-
mutex_unlock(&info->port.mutex);
2085+
21082086
return 0;
21092087
}
21102088

@@ -2120,13 +2098,12 @@ static int moxa_set_serial_info(struct tty_struct *tty,
21202098

21212099
close_delay = msecs_to_jiffies(ss->close_delay * 10);
21222100

2123-
mutex_lock(&info->port.mutex);
2101+
guard(mutex)(&info->port.mutex);
21242102
if (!capable(CAP_SYS_ADMIN)) {
21252103
if (close_delay != info->port.close_delay ||
21262104
ss->type != info->type ||
21272105
((ss->flags & ~ASYNC_USR_MASK) !=
21282106
(info->port.flags & ~ASYNC_USR_MASK))) {
2129-
mutex_unlock(&info->port.mutex);
21302107
return -EPERM;
21312108
}
21322109
} else {
@@ -2136,7 +2113,7 @@ static int moxa_set_serial_info(struct tty_struct *tty,
21362113

21372114
info->type = ss->type;
21382115
}
2139-
mutex_unlock(&info->port.mutex);
2116+
21402117
return 0;
21412118
}
21422119

0 commit comments

Comments
 (0)