Skip to content

Commit 2fc541e

Browse files
Jiri Slaby (SUSE)gregkh
authored andcommitted
tty: n_tty: use guard()s
Use guards in the n_tty 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-3-jirislaby@kernel.org Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 977e759 commit 2fc541e

1 file changed

Lines changed: 48 additions & 61 deletions

File tree

drivers/tty/n_tty.c

Lines changed: 48 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -324,12 +324,9 @@ static void reset_buffer_flags(struct n_tty_data *ldata)
324324

325325
static void n_tty_packet_mode_flush(struct tty_struct *tty)
326326
{
327-
unsigned long flags;
328-
329327
if (tty->link->ctrl.packet) {
330-
spin_lock_irqsave(&tty->ctrl.lock, flags);
331-
tty->ctrl.pktstatus |= TIOCPKT_FLUSHREAD;
332-
spin_unlock_irqrestore(&tty->ctrl.lock, flags);
328+
scoped_guard(spinlock_irqsave, &tty->ctrl.lock)
329+
tty->ctrl.pktstatus |= TIOCPKT_FLUSHREAD;
333330
wake_up_interruptible(&tty->link->read_wait);
334331
}
335332
}
@@ -349,13 +346,12 @@ static void n_tty_packet_mode_flush(struct tty_struct *tty)
349346
*/
350347
static void n_tty_flush_buffer(struct tty_struct *tty)
351348
{
352-
down_write(&tty->termios_rwsem);
349+
guard(rwsem_write)(&tty->termios_rwsem);
353350
reset_buffer_flags(tty->disc_data);
354351
n_tty_kick_worker(tty);
355352

356353
if (tty->link)
357354
n_tty_packet_mode_flush(tty);
358-
up_write(&tty->termios_rwsem);
359355
}
360356

361357
/**
@@ -737,24 +733,22 @@ static void commit_echoes(struct tty_struct *tty)
737733
size_t nr, old, echoed;
738734
size_t head;
739735

740-
mutex_lock(&ldata->output_lock);
741-
head = ldata->echo_head;
742-
ldata->echo_mark = head;
743-
old = ldata->echo_commit - ldata->echo_tail;
744-
745-
/* Process committed echoes if the accumulated # of bytes
746-
* is over the threshold (and try again each time another
747-
* block is accumulated) */
748-
nr = head - ldata->echo_tail;
749-
if (nr < ECHO_COMMIT_WATERMARK ||
750-
(nr % ECHO_BLOCK > old % ECHO_BLOCK)) {
751-
mutex_unlock(&ldata->output_lock);
752-
return;
753-
}
736+
scoped_guard(mutex, &ldata->output_lock) {
737+
head = ldata->echo_head;
738+
ldata->echo_mark = head;
739+
old = ldata->echo_commit - ldata->echo_tail;
740+
741+
/*
742+
* Process committed echoes if the accumulated # of bytes is over the threshold
743+
* (and try again each time another block is accumulated)
744+
*/
745+
nr = head - ldata->echo_tail;
746+
if (nr < ECHO_COMMIT_WATERMARK || (nr % ECHO_BLOCK > old % ECHO_BLOCK))
747+
return;
754748

755-
ldata->echo_commit = head;
756-
echoed = __process_echoes(tty);
757-
mutex_unlock(&ldata->output_lock);
749+
ldata->echo_commit = head;
750+
echoed = __process_echoes(tty);
751+
}
758752

759753
if (echoed && tty->ops->flush_chars)
760754
tty->ops->flush_chars(tty);
@@ -768,10 +762,10 @@ static void process_echoes(struct tty_struct *tty)
768762
if (ldata->echo_mark == ldata->echo_tail)
769763
return;
770764

771-
mutex_lock(&ldata->output_lock);
772-
ldata->echo_commit = ldata->echo_mark;
773-
echoed = __process_echoes(tty);
774-
mutex_unlock(&ldata->output_lock);
765+
scoped_guard(mutex, &ldata->output_lock) {
766+
ldata->echo_commit = ldata->echo_mark;
767+
echoed = __process_echoes(tty);
768+
}
775769

776770
if (echoed && tty->ops->flush_chars)
777771
tty->ops->flush_chars(tty);
@@ -786,10 +780,9 @@ static void flush_echoes(struct tty_struct *tty)
786780
ldata->echo_commit == ldata->echo_head)
787781
return;
788782

789-
mutex_lock(&ldata->output_lock);
783+
guard(mutex)(&ldata->output_lock);
790784
ldata->echo_commit = ldata->echo_head;
791785
__process_echoes(tty);
792-
mutex_unlock(&ldata->output_lock);
793786
}
794787

795788
/**
@@ -1078,18 +1071,19 @@ static void isig(int sig, struct tty_struct *tty)
10781071
if (L_NOFLSH(tty)) {
10791072
/* signal only */
10801073
__isig(sig, tty);
1074+
return;
1075+
}
10811076

1082-
} else { /* signal and flush */
1083-
up_read(&tty->termios_rwsem);
1084-
down_write(&tty->termios_rwsem);
1085-
1077+
/* signal and flush */
1078+
up_read(&tty->termios_rwsem);
1079+
scoped_guard(rwsem_write, &tty->termios_rwsem) {
10861080
__isig(sig, tty);
10871081

10881082
/* clear echo buffer */
1089-
mutex_lock(&ldata->output_lock);
1090-
ldata->echo_head = ldata->echo_tail = 0;
1091-
ldata->echo_mark = ldata->echo_commit = 0;
1092-
mutex_unlock(&ldata->output_lock);
1083+
scoped_guard(mutex, &ldata->output_lock) {
1084+
ldata->echo_head = ldata->echo_tail = 0;
1085+
ldata->echo_mark = ldata->echo_commit = 0;
1086+
}
10931087

10941088
/* clear output buffer */
10951089
tty_driver_flush_buffer(tty);
@@ -1100,10 +1094,8 @@ static void isig(int sig, struct tty_struct *tty)
11001094
/* notify pty master of flush */
11011095
if (tty->link)
11021096
n_tty_packet_mode_flush(tty);
1103-
1104-
up_write(&tty->termios_rwsem);
1105-
down_read(&tty->termios_rwsem);
11061097
}
1098+
down_read(&tty->termios_rwsem);
11071099
}
11081100

11091101
/**
@@ -1683,7 +1675,7 @@ n_tty_receive_buf_common(struct tty_struct *tty, const u8 *cp, const u8 *fp,
16831675
size_t n, rcvd = 0;
16841676
int room, overflow;
16851677

1686-
down_read(&tty->termios_rwsem);
1678+
guard(rwsem_read)(&tty->termios_rwsem);
16871679

16881680
do {
16891681
/*
@@ -1752,8 +1744,6 @@ n_tty_receive_buf_common(struct tty_struct *tty, const u8 *cp, const u8 *fp,
17521744
n_tty_kick_worker(tty);
17531745
}
17541746

1755-
up_read(&tty->termios_rwsem);
1756-
17571747
return rcvd;
17581748
}
17591749

@@ -1879,10 +1869,9 @@ static void n_tty_close(struct tty_struct *tty)
18791869
if (tty->link)
18801870
n_tty_packet_mode_flush(tty);
18811871

1882-
down_write(&tty->termios_rwsem);
1872+
guard(rwsem_write)(&tty->termios_rwsem);
18831873
vfree(ldata);
18841874
tty->disc_data = NULL;
1885-
up_write(&tty->termios_rwsem);
18861875
}
18871876

18881877
/**
@@ -2247,10 +2236,10 @@ static ssize_t n_tty_read(struct tty_struct *tty, struct file *file, u8 *kbuf,
22472236
u8 cs;
22482237
if (kb != kbuf)
22492238
break;
2250-
spin_lock_irq(&tty->link->ctrl.lock);
2251-
cs = tty->link->ctrl.pktstatus;
2252-
tty->link->ctrl.pktstatus = 0;
2253-
spin_unlock_irq(&tty->link->ctrl.lock);
2239+
scoped_guard(spinlock_irq, &tty->link->ctrl.lock) {
2240+
cs = tty->link->ctrl.pktstatus;
2241+
tty->link->ctrl.pktstatus = 0;
2242+
}
22542243
*kb++ = cs;
22552244
nr--;
22562245
break;
@@ -2357,7 +2346,7 @@ static ssize_t n_tty_write(struct tty_struct *tty, struct file *file,
23572346
return retval;
23582347
}
23592348

2360-
down_read(&tty->termios_rwsem);
2349+
guard(rwsem_read)(&tty->termios_rwsem);
23612350

23622351
/* Write out any echoed characters that are still pending */
23632352
process_echoes(tty);
@@ -2395,9 +2384,8 @@ static ssize_t n_tty_write(struct tty_struct *tty, struct file *file,
23952384
struct n_tty_data *ldata = tty->disc_data;
23962385

23972386
while (nr > 0) {
2398-
mutex_lock(&ldata->output_lock);
2399-
num = tty->ops->write(tty, b, nr);
2400-
mutex_unlock(&ldata->output_lock);
2387+
scoped_guard(mutex, &ldata->output_lock)
2388+
num = tty->ops->write(tty, b, nr);
24012389
if (num < 0) {
24022390
retval = num;
24032391
goto break_out;
@@ -2424,7 +2412,7 @@ static ssize_t n_tty_write(struct tty_struct *tty, struct file *file,
24242412
remove_wait_queue(&tty->write_wait, &wait);
24252413
if (nr && tty->fasync)
24262414
set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
2427-
up_read(&tty->termios_rwsem);
2415+
24282416
return (b - buf) ? b - buf : retval;
24292417
}
24302418

@@ -2498,12 +2486,11 @@ static int n_tty_ioctl(struct tty_struct *tty, unsigned int cmd,
24982486
case TIOCOUTQ:
24992487
return put_user(tty_chars_in_buffer(tty), (int __user *) arg);
25002488
case TIOCINQ:
2501-
down_write(&tty->termios_rwsem);
2502-
if (L_ICANON(tty) && !L_EXTPROC(tty))
2503-
num = inq_canon(ldata);
2504-
else
2505-
num = read_cnt(ldata);
2506-
up_write(&tty->termios_rwsem);
2489+
scoped_guard(rwsem_write, &tty->termios_rwsem)
2490+
if (L_ICANON(tty) && !L_EXTPROC(tty))
2491+
num = inq_canon(ldata);
2492+
else
2493+
num = read_cnt(ldata);
25072494
return put_user(num, (unsigned int __user *) arg);
25082495
default:
25092496
return n_tty_ioctl_helper(tty, cmd, arg);

0 commit comments

Comments
 (0)